Skip to content

Commit cf2e6a9

Browse files
committed
fix: align prompt cache metrics with fetch attempts
1 parent 1f9222d commit cf2e6a9

3 files changed

Lines changed: 42 additions & 20 deletions

File tree

index.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ import { paintUiText, formatUiBadge, formatUiHeader, formatUiItem, formatUiKeyVa
158158
import {
159159
buildBeginnerChecklist,
160160
buildBeginnerDoctorFindings,
161-
formatPromptCacheKey,
161+
formatPromptCacheSnapshot,
162162
recommendBeginnerNextAction,
163163
summarizeBeginnerAccounts,
164164
type BeginnerAccountSnapshot,
@@ -2036,11 +2036,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
20362036
);
20372037
runtimeMetrics.lastRequestAt = Date.now();
20382038
runtimeMetrics.lastPromptCacheKey = promptCacheKey ?? null;
2039-
if (promptCacheKey) {
2040-
runtimeMetrics.promptCacheEnabledRequests += 1;
2041-
} else {
2042-
runtimeMetrics.promptCacheMissingRequests += 1;
2043-
}
20442039
const retryBudget = new RetryBudgetTracker(retryBudgetLimits);
20452040
const consumeRetryBudget = (
20462041
bucket: RetryBudgetClass,
@@ -2329,17 +2324,25 @@ while (attempted.size < Math.max(1, accountCount)) {
23292324
: null;
23302325

23312326
if (abortSignal?.aborted) {
2332-
clearTimeout(fetchTimeoutId);
2333-
fetchController.abort(abortSignal.reason ?? new Error("Aborted by user"));
2334-
} else if (abortSignal && onUserAbort) {
2335-
abortSignal.addEventListener("abort", onUserAbort, { once: true });
2336-
}
2327+
clearTimeout(fetchTimeoutId);
2328+
fetchController.abort(abortSignal.reason ?? new Error("Aborted by user"));
2329+
} else if (abortSignal && onUserAbort) {
2330+
abortSignal.addEventListener("abort", onUserAbort, { once: true });
2331+
}
23372332

2338-
try {
2339-
runtimeMetrics.totalRequests++;
2340-
response = await fetch(url, {
2341-
...requestInit,
2342-
headers,
2333+
try {
2334+
// Request metrics are tracked at the fetch boundary, so retries and account
2335+
// rotation are counted consistently. This is in-memory only: no filesystem I/O,
2336+
// no token persistence, and prompt-cache keys stay redacted in doctor output.
2337+
runtimeMetrics.totalRequests++;
2338+
if (promptCacheKey) {
2339+
runtimeMetrics.promptCacheEnabledRequests++;
2340+
} else {
2341+
runtimeMetrics.promptCacheMissingRequests++;
2342+
}
2343+
response = await fetch(url, {
2344+
...requestInit,
2345+
headers,
23432346
signal: fetchController.signal,
23442347
});
23452348
} catch (networkError) {
@@ -5217,9 +5220,7 @@ while (attempted.size < Math.max(1, accountCount)) {
52175220
formatUiKeyValue(
52185221
ui,
52195222
"Prompt cache",
5220-
runtime.promptCacheEnabledRequests > 0
5221-
? `enabled=${runtime.promptCacheEnabledRequests}, missing=${runtime.promptCacheMissingRequests}, lastKey=${formatPromptCacheKey(runtime.lastPromptCacheKey)}`
5222-
: `enabled=0, missing=${runtime.promptCacheMissingRequests}, lastKey=none`,
5223+
formatPromptCacheSnapshot(runtime),
52235224
"muted",
52245225
),
52255226
);
@@ -5263,7 +5264,7 @@ while (attempted.size < Math.max(1, accountCount)) {
52635264
` Runtime failures: failed=${runtime.failedRequests}, rateLimited=${runtime.rateLimitedResponses}, authRefreshFailed=${runtime.authRefreshFailures}, server=${runtime.serverErrors}, network=${runtime.networkErrors}`,
52645265
);
52655266
lines.push(
5266-
` Prompt cache: enabled=${runtime.promptCacheEnabledRequests}, missing=${runtime.promptCacheMissingRequests}, lastKey=${formatPromptCacheKey(runtime.lastPromptCacheKey)}`,
5267+
` Prompt cache: ${formatPromptCacheSnapshot(runtime)}`,
52675268
);
52685269
}
52695270
return lines.join("\n");

lib/ui/beginner.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export function formatPromptCacheKey(value: string | null | undefined): string {
5757
return `${normalized.slice(0, 8)}...`;
5858
}
5959

60+
export function formatPromptCacheSnapshot(runtime: Pick<
61+
BeginnerRuntimeSnapshot,
62+
"promptCacheEnabledRequests" | "promptCacheMissingRequests" | "lastPromptCacheKey"
63+
>): string {
64+
return `enabled=${runtime.promptCacheEnabledRequests}, missing=${runtime.promptCacheMissingRequests}, lastKey=${formatPromptCacheKey(runtime.lastPromptCacheKey)}`;
65+
}
66+
6067
export function summarizeBeginnerAccounts(
6168
accounts: BeginnerAccountSnapshot[],
6269
now: number,

test/beginner-ui.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
buildBeginnerDoctorFindings,
55
explainRuntimeErrorCategory,
66
formatPromptCacheKey,
7+
formatPromptCacheSnapshot,
78
recommendBeginnerNextAction,
89
summarizeBeginnerAccounts,
910
type BeginnerAccountSnapshot,
@@ -227,3 +228,16 @@ describe("formatPromptCacheKey", () => {
227228
expect(formatPromptCacheKey("ses_prompt_cache_key_123")).toBe("ses_prom...");
228229
});
229230
});
231+
232+
describe("formatPromptCacheSnapshot", () => {
233+
it("renders a redacted prompt cache snapshot string", () => {
234+
const rendered = formatPromptCacheSnapshot({
235+
promptCacheEnabledRequests: 4,
236+
promptCacheMissingRequests: 1,
237+
lastPromptCacheKey: "ses_prompt_cache_key_123",
238+
});
239+
240+
expect(rendered).toBe("enabled=4, missing=1, lastKey=ses_prom...");
241+
expect(rendered).not.toContain("ses_prompt_cache_key_123");
242+
});
243+
});

0 commit comments

Comments
 (0)