Skip to content

Commit 2ae3b5f

Browse files
committed
fix(compact): report upstream usage for native compact turns
The native branch buffers the upstream compact JSON and returns it without inspecting the body, so the request log row lands with no usage. Lift usage and response metadata from the buffered body the same way the routed branch gets it through handleResponses.
1 parent 6a7351b commit 2ae3b5f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/server/responses/compact.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ export async function handleResponsesCompact(
482482
// Always record the real upstream status: a local buffering failure after a
483483
// 200 upstream response must not soft-avoid a healthy account or rotate a thread.
484484
recordCompactPoolOutcome(outcomeCtx, upstream.status, { retryAfter, resetAt });
485+
// Lift usage and response metadata from the buffered upstream JSON into the
486+
// request log; the routed branch gets the same through handleResponses. The
487+
// synthetic buffer errors are not upstream bodies and stay uninspected.
488+
if (buffered.ok) inspectResponseLogJson(logCtx, await buffered.clone().text());
485489
return buffered;
486490
}
487491

‎tests/responses-compaction-routing.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
resolveCodexAuthContext,
2424
} from "../src/codex/auth-context";
2525
import { supportsNativeResponsesCompactEndpoint } from "../src/providers/openai-tiers";
26+
import type { RequestLogContext } from "../src/server/request-log";
2627
import type { OcxConfig, OcxProviderConfig } from "../src/types";
2728

2829
const originalFetch = globalThis.fetch;
@@ -164,6 +165,33 @@ describe("supportsNativeResponsesCompactEndpoint (#422)", () => {
164165
});
165166
});
166167

168+
describe("native compact usage reporting", () => {
169+
test("the buffered upstream body fills the request log usage and stays intact for the client", async () => {
170+
const config = {
171+
defaultProvider: "openai-apikey",
172+
providers: {
173+
"openai-apikey": {
174+
adapter: "openai-responses",
175+
baseUrl: "https://api.openai.com/v1",
176+
authMode: "key",
177+
apiKey: "sk-test",
178+
},
179+
},
180+
} as unknown as OcxConfig;
181+
globalThis.fetch = (async () => jsonResponse(completedPayload("native summary"))) as typeof fetch;
182+
const logCtx: RequestLogContext = { model: "", provider: "" };
183+
const response = await handleResponsesCompact(
184+
compactionRequest(baseCompactionBody({ model: "openai-apikey/gpt-5.5" })),
185+
config,
186+
logCtx,
187+
);
188+
expect(response.status).toBe(200);
189+
const body = await response.json() as { usage?: Record<string, unknown> };
190+
expect(body.usage).toMatchObject({ input_tokens: 10, output_tokens: 5, total_tokens: 15 });
191+
expect(logCtx.usage).toMatchObject({ inputTokens: 10, outputTokens: 5, totalTokens: 15 });
192+
});
193+
});
194+
167195
describe("native Codex pool compaction", () => {
168196
test("keeps a Spark reset cooldown separate from a Terra compact request (#590)", async () => {
169197
const testDir = mkdtempSync(join(tmpdir(), "ocx-compact-scope-"));

0 commit comments

Comments
 (0)