Skip to content

Commit fd76d13

Browse files
committed
fix: preserve native compaction completion ownership
1 parent 399726a commit fd76d13

4 files changed

Lines changed: 101 additions & 10 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,12 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
23002300
const output = Array.isArray(responsePayload?.output) ? responsePayload.output : [];
23012301
const compaction = output.find(item => isPlainObject(item) && item.type === "compaction");
23022302
if (isPlainObject(compaction) && typeof compaction.encrypted_content === "string") {
2303-
compactionEncryptedContent = compaction.encrypted_content;
2303+
const nextEncryptedContent = compaction.encrypted_content;
2304+
const previousBytes = budgetEncoder.encode(compactionEncryptedContent ?? "").byteLength;
2305+
const reservation = budget.reserveTransient(budgetEncoder.encode(nextEncryptedContent).byteLength, { kind: "retained_collectors" });
2306+
compactionEncryptedContent = nextEncryptedContent;
2307+
reservation.commitRetained();
2308+
budget.releaseRetained(previousBytes, { kind: "retained_collectors" });
23042309
}
23052310
const next = responsesPayloadText(payload.response);
23062311
const previousBytes = budgetEncoder.encode(snapshot).byteLength;
@@ -2340,19 +2345,19 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
23402345
if (payload.status === "incomplete") {
23412346
return [{ type: "incomplete", reason: responsesErrorMessage(payload) }];
23422347
}
2343-
const text = responsesPayloadText(payload);
2344-
if (!text) {
2345-
// A completed turn with no usable text cannot become a summary; saying so is
2346-
// better than installing an empty compaction as replacement history.
2347-
return [{ type: "error", message: "upstream compaction returned no summary text" }];
2348-
}
23492348
const usage = usageFromResponsesPayload(payload);
23502349
const output = Array.isArray(payload.output) ? payload.output : [];
23512350
const compaction = output.find(item => isPlainObject(item) && item.type === "compaction");
23522351
const compactionEncryptedContent = isPlainObject(compaction) && typeof compaction.encrypted_content === "string"
23532352
? compaction.encrypted_content
23542353
: undefined;
2355-
return [{ type: "text_delta", text }, {
2354+
const text = responsesPayloadText(payload);
2355+
if (!text && !compactionEncryptedContent) {
2356+
// A completed turn with neither text nor a native compaction blob cannot become a
2357+
// replacement-history item. A ciphertext-only native completion is valid, though.
2358+
return [{ type: "error", message: "upstream compaction returned no summary text" }];
2359+
}
2360+
return [...(text ? [{ type: "text_delta" as const, text }] : []), {
23562361
type: "done",
23572362
...(usage ? { usage } : {}),
23582363
...(compactionEncryptedContent ? { compactionEncryptedContent } : {}),

‎src/server/responses/compact.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,8 @@ export async function handleResponsesCompact(
10771077
// Native Responses backends return a real opaque OpenAI-encrypted compaction item. OCX cannot
10781078
// and should not decrypt it; preserve that item for /responses/compact callers. Synthetic
10791079
// routed summaries are our `ocx1:` envelope and must be decoded into v1 history items.
1080-
if (accountGatedCompactWireModel || (typeof compactionItems[0]!.encrypted_content === "string"
1081-
&& !compactionItems[0]!.encrypted_content.startsWith("ocx1:"))) {
1080+
if (typeof compactionItems[0]!.encrypted_content === "string"
1081+
&& !compactionItems[0]!.encrypted_content.startsWith("ocx1:")) {
10821082
const result = new Response(JSON.stringify({ output: compactionItems }), {
10831083
headers: { "Content-Type": "application/json" },
10841084
});

‎tests/responses-compaction.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, test } from "bun:test";
22
import { bridgeToResponsesSSE, buildResponseJSON } from "../src/bridge";
33
import { createResponsesPassthroughAdapter as createResponsesPassthroughAdapterProduction } from "../src/adapters/openai-responses";
4+
import { createTranslatorBudget } from "../src/lib/translator-budget";
45
import { CODEX_FORWARD_BASE_URL } from "../src/providers/openai-tiers";
56
import { parseRequest } from "../src/responses/parser";
67
import {
@@ -165,6 +166,70 @@ describe("buildResponseJSON compaction mode", () => {
165166
});
166167
});
167168

169+
describe("native Responses compaction passthrough", () => {
170+
const provider = {
171+
adapter: "openai-responses",
172+
baseUrl: "https://responses.example/v1",
173+
authMode: "key" as const,
174+
apiKey: "test-key",
175+
};
176+
177+
test("buffered ciphertext-only completion yields done without a text delta", async () => {
178+
const adapter = createResponsesPassthroughAdapterProduction(provider);
179+
const encryptedContent = "gAAAAABm-native-buffered-ciphertext";
180+
const budget = createTranslatorBudget();
181+
try {
182+
const events = await adapter.parseResponse!(Response.json({
183+
status: "completed",
184+
output: [{ type: "compaction", encrypted_content: encryptedContent }],
185+
}), budget);
186+
187+
expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]);
188+
} finally {
189+
budget.dispose();
190+
}
191+
});
192+
193+
test("streaming ciphertext is charged before the compaction item takes ownership", async () => {
194+
const adapter = createResponsesPassthroughAdapterProduction(provider);
195+
const encryptedContent = "gAAAAABm-native-streaming-ciphertext";
196+
const budget = createTranslatorBudget();
197+
try {
198+
const events: AdapterEvent[] = [];
199+
const stream = [
200+
"event: response.completed",
201+
`data: ${JSON.stringify({
202+
type: "response.completed",
203+
response: {
204+
status: "completed",
205+
output: [{ type: "compaction", encrypted_content: encryptedContent }],
206+
},
207+
})}`,
208+
"",
209+
"",
210+
].join("\n");
211+
for await (const event of adapter.parseStream(new Response(stream, {
212+
headers: { "content-type": "text/event-stream" },
213+
}), budget)) events.push(event);
214+
215+
expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]);
216+
expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(encryptedContent));
217+
218+
const json = buildResponseJSON(events, "test/model", {
219+
compaction: true,
220+
translatorBudget: budget,
221+
}) as { output: Array<{ type: string; encrypted_content?: string }> };
222+
expect(json.output).toEqual([expect.objectContaining({
223+
type: "compaction",
224+
encrypted_content: encryptedContent,
225+
})]);
226+
expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(JSON.stringify(json.output[0])));
227+
} finally {
228+
budget.dispose();
229+
}
230+
});
231+
});
232+
168233
describe("COMPACT_PROMPT", () => {
169234
test("mirrors the codex-rs checkpoint instruction", () => {
170235
expect(COMPACT_PROMPT).toContain("CONTEXT CHECKPOINT COMPACTION");

‎tests/server-combo-failover-e2e.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,27 @@ describe("combo compact failover", () => {
30263026
expect(attempts[1]).toMatchObject({ provider: "backup", adapter: "openai-chat", status: 200 });
30273027
});
30283028

3029+
test("account-gated first target failover decodes the backup ocx1 compaction", async () => {
3030+
const b = serve(() => chatStream("mixed combo backup summary"));
3031+
const { config } = canonicalPoolConfig([
3032+
{ provider: "openai-apikey", model: "gpt-daybreak-blue-latest" },
3033+
{ provider: "backup", model: "m1" },
3034+
], baseUrl(b));
3035+
globalThis.fetch = (async (input: unknown, init?: RequestInit) => {
3036+
const url = typeof input === "object" && input !== null && "url" in input ? String((input as Request).url) : String(input);
3037+
if (url.includes("api.openai.com")) {
3038+
return Response.json({ error: { message: "rate limited" } }, { status: 429 });
3039+
}
3040+
return originalFetch(input as RequestInfo, init);
3041+
}) as typeof fetch;
3042+
3043+
const response = await postCompactLogged(config);
3044+
expect(response.status).toBe(200);
3045+
const json = await response.json() as { output?: unknown[] };
3046+
expect(JSON.stringify(json.output)).toContain("mixed combo backup summary");
3047+
expect(JSON.stringify(json.output)).not.toContain("ocx1:");
3048+
});
3049+
30293050
test("combo compact runs the synthetic turn as SSE so a canonical child can serve it", async () => {
30303051
const bodies: Array<Record<string, unknown>> = [];
30313052
const { config } = canonicalPoolConfig([{ provider: "openai-apikey", model: "gpt-5.4" }]);

0 commit comments

Comments
 (0)