mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-10 18:35:17 +00:00
Keep last successful usage visible when refresh fails (#7246)
* fix(usage): retain last successful data when polls fail Keep the latest poll outcome separate from its successful snapshot so errors remain visible without discarding known usage or advancing its freshness. Preserve attempt timestamps for response ordering and isolate retained values by scoped login set. * fix(usage): preserve history through discovery failures A failed account-store read does not confirm that an account was removed. Retain its existing host mapping until successful discovery replaces it, without carrying mappings across agent launch environments.
This commit is contained in:
@@ -277,3 +277,40 @@ describe("usage CLI registration", () => {
|
||||
expect(usage.commands[1]!.helpInformation()).toContain("<report-id>");
|
||||
});
|
||||
});
|
||||
|
||||
it("shows retained values with current failures and separates successful time from latest attempt", async () => {
|
||||
const success: UsageReportEntry = { ...reports[0]!, successfulAt: "2026-10-09T12:00:15Z" };
|
||||
const current = await runUsageCommand(
|
||||
options,
|
||||
{ kind: "inspect", id: success.id },
|
||||
host([success]).connect,
|
||||
);
|
||||
expect(render(current)).toContain("Latest attempt: 2026-10-09T12:00:00Z");
|
||||
expect(render(current)).toContain("Last successful: 2026-10-09T12:00:15Z");
|
||||
const currentList = await runUsageCommand(options, { kind: "list" }, host([success]).connect);
|
||||
expect(render(currentList)).toContain("Last successful: 2026-10-09T12:00:15Z");
|
||||
if (success.report.status !== "available") throw new Error("fixture must be available");
|
||||
const retained: UsageReportEntry = {
|
||||
...success,
|
||||
fetchedAt: "2026-10-09T12:06:00Z",
|
||||
report: { status: "error", error: "Rate limited" },
|
||||
successfulAt: undefined,
|
||||
lastSuccessful: { fetchedAt: success.successfulAt!, report: success.report },
|
||||
};
|
||||
const daemon = host([retained]);
|
||||
const listed = await runUsageCommand(options, { kind: "list" }, daemon.connect);
|
||||
expect(render(listed)).toContain("Retained: Five hours: 12.5% used");
|
||||
expect(render(listed)).toContain("Last successful: 2026-10-09T12:00:15Z");
|
||||
expect(render(listed)).toContain("Rate limited");
|
||||
expect(JSON.parse(render(listed, { format: "json" }))).toEqual([retained]);
|
||||
const inspected = await runUsageCommand(
|
||||
options,
|
||||
{ kind: "inspect", id: retained.id },
|
||||
daemon.connect,
|
||||
);
|
||||
expect(render(inspected)).toContain("Latest attempt: 2026-10-09T12:06:00Z");
|
||||
expect(render(inspected)).toContain("Last successful: 2026-10-09T12:00:15Z");
|
||||
expect(render(inspected)).toContain("Retained usage:");
|
||||
expect(render(inspected)).toContain("Five hours: 12.5% used");
|
||||
expect(render(inspected)).toContain("Reason: Rate limited");
|
||||
});
|
||||
|
||||
@@ -44,8 +44,18 @@ function summary(entry: UsageReportEntry): string[] {
|
||||
const lines = [failureText(report)];
|
||||
for (const login of entry.loginErrors ?? [])
|
||||
lines.push(`${login.harness}: ${failureText(login.report)}`);
|
||||
if (entry.lastSuccessful) {
|
||||
lines.push(`Last successful: ${entry.lastSuccessful.fetchedAt}`);
|
||||
lines.push(
|
||||
...availableSummary(entry.lastSuccessful.report).map((line) => `Retained: ${line}`),
|
||||
);
|
||||
} else lines.push("No successful update");
|
||||
return lines;
|
||||
}
|
||||
return [...availableSummary(report), `Last successful: ${successfulTime(entry)}`];
|
||||
}
|
||||
|
||||
function availableSummary(report: Extract<UsageReport, { status: "available" }>): string[] {
|
||||
const lines = report.windows.map(windowText);
|
||||
lines.push(...(report.balances ?? []).map(balanceText));
|
||||
return lines.length ? lines : ["-"];
|
||||
@@ -68,7 +78,7 @@ const summarySchema: OutputSchema<SummaryRow> = {
|
||||
{ header: "ACCOUNT", field: "account" },
|
||||
{ header: "STATUS", field: "status" },
|
||||
{ header: "USAGE", field: "usage" },
|
||||
{ header: "FETCHED", field: "fetched" },
|
||||
{ header: "FETCHED (LATEST ATTEMPT)", field: "fetched" },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -79,30 +89,15 @@ function inspect(entry: UsageReportEntry): string {
|
||||
`Source ID: ${entry.sourceId}`,
|
||||
`Account: ${value(entry.account.label)}`,
|
||||
`Status: ${entry.report.status}`,
|
||||
`Fetched: ${value(entry.fetchedAt)}`,
|
||||
`Latest attempt: ${value(entry.fetchedAt)}`,
|
||||
];
|
||||
const { report } = entry;
|
||||
if (report.status === "available") {
|
||||
lines.push(`Plan: ${value(report.planLabel)}`, "Windows:");
|
||||
if (!report.windows.length) lines.push(" -");
|
||||
for (const window of report.windows) {
|
||||
lines.push(` ${windowText(window)} [${window.id}]`, ` Resets: ${value(window.resetsAt)}`);
|
||||
if (window.runsOutAt != null) lines.push(` Runs out: ${window.runsOutAt}`);
|
||||
if (window.shortfallPct != null) lines.push(` Shortfall: ${percent(window.shortfallPct)}`);
|
||||
}
|
||||
lines.push("Balances:");
|
||||
if (!report.balances?.length) lines.push(" -");
|
||||
for (const balance of report.balances ?? []) {
|
||||
lines.push(
|
||||
` ${balanceText(balance)} [${balance.id}]`,
|
||||
` Resets: ${value(balance.resetsAt)}`,
|
||||
);
|
||||
}
|
||||
lines.push("Details:");
|
||||
if (!report.details?.length) lines.push(" -");
|
||||
for (const detail of report.details ?? []) lines.push(` ${detail.label}: ${detail.value}`);
|
||||
} else {
|
||||
lines.push(`Reason: ${failureText(report)}`);
|
||||
const report = entry.report.status === "available" ? entry.report : entry.lastSuccessful?.report;
|
||||
const successfulAt = successfulTime(entry);
|
||||
lines.push(successfulAt ? `Last successful: ${successfulAt}` : "No successful update");
|
||||
if (entry.report.status !== "available") lines.push(`Reason: ${failureText(entry.report)}`);
|
||||
if (report) {
|
||||
if (entry.report.status !== "available") lines.push("Retained usage:");
|
||||
lines.push(...availableDetails(report));
|
||||
}
|
||||
if (entry.loginErrors?.length) {
|
||||
lines.push("Login failures:");
|
||||
@@ -135,3 +130,33 @@ export const usageSchema: OutputSchema<UsageReportEntry> = {
|
||||
return renderTable<SummaryRow>({ type: "list", data: rows, schema: summarySchema }, options);
|
||||
},
|
||||
};
|
||||
|
||||
function availableDetails(report: Extract<UsageReport, { status: "available" }>): string[] {
|
||||
const lines: string[] = [];
|
||||
lines.push(`Plan: ${value(report.planLabel)}`, "Windows:");
|
||||
if (!report.windows.length) lines.push(" -");
|
||||
for (const window of report.windows) {
|
||||
lines.push(` ${windowText(window)} [${window.id}]`, ` Resets: ${value(window.resetsAt)}`);
|
||||
if (window.runsOutAt != null) lines.push(` Runs out: ${window.runsOutAt}`);
|
||||
if (window.shortfallPct != null) lines.push(` Shortfall: ${percent(window.shortfallPct)}`);
|
||||
}
|
||||
lines.push("Balances:");
|
||||
if (!report.balances?.length) lines.push(" -");
|
||||
for (const balance of report.balances ?? []) {
|
||||
lines.push(
|
||||
` ${balanceText(balance)} [${balance.id}]`,
|
||||
` Resets: ${value(balance.resetsAt)}`,
|
||||
);
|
||||
}
|
||||
lines.push("Details:");
|
||||
if (!report.details?.length) lines.push(" -");
|
||||
for (const detail of report.details ?? []) lines.push(` ${detail.label}: ${detail.value}`);
|
||||
return lines;
|
||||
}
|
||||
|
||||
function successfulTime(entry: UsageReportEntry): string | undefined {
|
||||
// COMPAT(usageSuccessfulAt): added in v0.12.0, remove after 2027-04-17 once daemon floor >= v0.12.0.
|
||||
// Only available reports from older daemons can use their attempt timestamp as freshness.
|
||||
if (entry.report.status === "available") return entry.successfulAt ?? entry.fetchedAt;
|
||||
return entry.lastSuccessful?.fetchedAt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user