Skip to content

Commit 399726a

Browse files
x3M3xlidge-jun
authored andcommitted
preserve opaque compaction ciphertext
(cherry picked from commit 9582fc3)
1 parent a4aba14 commit 399726a

5 files changed

Lines changed: 56 additions & 22 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
22612261
let doneText = "";
22622262
let snapshot = "";
22632263
let usage: OcxUsage | undefined;
2264+
let compactionEncryptedContent: string | undefined;
22642265
for await (const event of decodeServerSentEvents(response.body, { translatorBudget: budget })) {
22652266
let payload: unknown;
22662267
try { payload = JSON.parse(event.data); } catch { continue; }
@@ -2295,6 +2296,12 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
22952296
return;
22962297
case "response.completed":
22972298
{
2299+
const responsePayload = isPlainObject(payload.response) ? payload.response : undefined;
2300+
const output = Array.isArray(responsePayload?.output) ? responsePayload.output : [];
2301+
const compaction = output.find(item => isPlainObject(item) && item.type === "compaction");
2302+
if (isPlainObject(compaction) && typeof compaction.encrypted_content === "string") {
2303+
compactionEncryptedContent = compaction.encrypted_content;
2304+
}
22982305
const next = responsesPayloadText(payload.response);
22992306
const previousBytes = budgetEncoder.encode(snapshot).byteLength;
23002307
const reservation = budget.reserveTransient(budgetEncoder.encode(next).byteLength, { kind: "retained_collectors" });
@@ -2311,7 +2318,11 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
23112318
const text = snapshot || doneText || deltas;
23122319
if (text) yield { type: "text_delta", text };
23132320
budget.releaseRetained(budgetEncoder.encode(deltas).byteLength + budgetEncoder.encode(doneText).byteLength + budgetEncoder.encode(snapshot).byteLength, { kind: "retained_collectors" });
2314-
yield { type: "done", ...(usage ? { usage } : {}) };
2321+
yield {
2322+
type: "done",
2323+
...(usage ? { usage } : {}),
2324+
...(compactionEncryptedContent ? { compactionEncryptedContent } : {}),
2325+
};
23152326
},
23162327

23172328
async parseResponse(response: Response, budget: TranslatorBudget): Promise<AdapterEvent[]> {
@@ -2336,7 +2347,16 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
23362347
return [{ type: "error", message: "upstream compaction returned no summary text" }];
23372348
}
23382349
const usage = usageFromResponsesPayload(payload);
2339-
return [{ type: "text_delta", text }, { type: "done", ...(usage ? { usage } : {}) }];
2350+
const output = Array.isArray(payload.output) ? payload.output : [];
2351+
const compaction = output.find(item => isPlainObject(item) && item.type === "compaction");
2352+
const compactionEncryptedContent = isPlainObject(compaction) && typeof compaction.encrypted_content === "string"
2353+
? compaction.encrypted_content
2354+
: undefined;
2355+
return [{ type: "text_delta", text }, {
2356+
type: "done",
2357+
...(usage ? { usage } : {}),
2358+
...(compactionEncryptedContent ? { compactionEncryptedContent } : {}),
2359+
}];
23402360
},
23412361
};
23422362
}

‎src/bridge.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,10 +1223,12 @@ export function bridgeToResponsesSSE(
12231223
// Exactly one compaction item per turn; codex-rs takes the first and fatals on 0.
12241224
const item = {
12251225
type: "compaction", id: `cmp_${uuid()}`,
1226-
encrypted_content: encodeCompactionSummary(compactionText),
1226+
encrypted_content: event.compactionEncryptedContent ?? encodeCompactionSummary(compactionText),
12271227
};
12281228
emit("response.output_item.done", { output_index: outputIndex, item });
1229-
retainFinishedItem(item as OutputItem, compactionTextBytes);
1229+
retainFinishedItem(item as OutputItem, event.compactionEncryptedContent
1230+
? bytesOf(event.compactionEncryptedContent)
1231+
: compactionTextBytes);
12301232
outputIndex++;
12311233
}
12321234
// Recognize every adapter's truncation vocabulary, not just the canonical pair.
@@ -1574,6 +1576,7 @@ function buildResponseJSONWithBudget(
15741576
let sawTerminal = false;
15751577
let compactionText = "";
15761578
let compactionTextBytes = 0;
1579+
let compactionEncryptedContent: string | undefined;
15771580

15781581
let currentText = "";
15791582
let currentTextBytes = 0;
@@ -1915,6 +1918,7 @@ function buildResponseJSONWithBudget(
19151918
break;
19161919
case "done":
19171920
usage = e.usage;
1921+
compactionEncryptedContent = e.compactionEncryptedContent;
19181922
sawTerminal = true;
19191923
endTurn = e.endTurn;
19201924
cleanDone = e.stopReason === undefined;
@@ -1967,7 +1971,11 @@ function buildResponseJSONWithBudget(
19671971
&& sawTerminal
19681972
&& !isTruncatedStopReason(rawStopReason)
19691973
) {
1970-
pushOutput({ type: "compaction", id: `cmp_${uuid()}`, encrypted_content: encodeCompactionSummary(compactionText) }, compactionTextBytes);
1974+
const item = {
1975+
type: "compaction", id: `cmp_${uuid()}`,
1976+
encrypted_content: compactionEncryptedContent ?? encodeCompactionSummary(compactionText),
1977+
};
1978+
pushOutput(item, compactionEncryptedContent ? bytesOf(compactionEncryptedContent) : compactionTextBytes);
19711979
}
19721980

19731981
const failure = errorEvent ? adapterFailureFromEvent(errorEvent) : undefined;

‎src/server/responses/compact.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,11 @@ export async function handleResponsesCompact(
10741074
`compaction turn produced ${compactionItems.length} compaction items, expected exactly 1`,
10751075
);
10761076
}
1077-
// The canonical Responses stream returns a real OpenAI-encrypted compaction item. OCX cannot
1078-
// and should not decrypt it; /responses/compact callers can consume that item directly.
1079-
if (accountGatedCompactWireModel) {
1077+
// Native Responses backends return a real opaque OpenAI-encrypted compaction item. OCX cannot
1078+
// and should not decrypt it; preserve that item for /responses/compact callers. Synthetic
1079+
// 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:"))) {
10801082
const result = new Response(JSON.stringify({ output: compactionItems }), {
10811083
headers: { "Content-Type": "application/json" },
10821084
});

‎src/types/request.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ export type AdapterEvent =
333333
| {
334334
type: "done";
335335
usage?: OcxUsage;
336+
/** Native opaque compaction ciphertext returned by a Responses backend. */
337+
compactionEncryptedContent?: string;
336338
stopReason?: string;
337339
endTurn?: boolean;
338340
providerState?: OcxProviderContinuationState;

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,9 +2975,9 @@ describe("combo compact failover", () => {
29752975
backupUrl?: string,
29762976
): { config: OcxConfig } {
29772977
const config = comboConfig({
2978-
openai: {
2978+
"openai-apikey": {
29792979
adapter: "openai-responses",
2980-
baseUrl: "https://chatgpt.com/backend-api/codex",
2980+
baseUrl: "https://api.openai.com/v1",
29812981
authMode: "key",
29822982
apiKey: "combo-compact-key",
29832983
},
@@ -2993,12 +2993,12 @@ describe("combo compact failover", () => {
29932993
return chatStream("compact backup");
29942994
});
29952995
const { config } = canonicalPoolConfig([
2996-
{ provider: "openai", model: "gpt-5.4" },
2996+
{ provider: "openai-apikey", model: "gpt-5.4" },
29972997
{ provider: "backup", model: "m1" },
29982998
], baseUrl(b));
29992999
globalThis.fetch = (async (input: unknown, init?: RequestInit) => {
30003000
const url = typeof input === "object" && input !== null && "url" in input ? String((input as Request).url) : String(input);
3001-
if (url.includes("chatgpt.com")) {
3001+
if (url.includes("api.openai.com")) {
30023002
return Response.json({ error: { message: "rate limited" } }, { status: 429 });
30033003
}
30043004
return originalFetch(input as RequestInfo, init);
@@ -3019,7 +3019,7 @@ describe("combo compact failover", () => {
30193019
const attempts = log.attempts as Array<Record<string, unknown>>;
30203020
expect(attempts).toHaveLength(2);
30213021
expect(attempts[0]).toMatchObject({
3022-
provider: "openai",
3022+
provider: "openai-apikey",
30233023
adapter: "openai-responses",
30243024
status: 429,
30253025
});
@@ -3028,15 +3028,17 @@ describe("combo compact failover", () => {
30283028

30293029
test("combo compact runs the synthetic turn as SSE so a canonical child can serve it", async () => {
30303030
const bodies: Array<Record<string, unknown>> = [];
3031-
const { config } = canonicalPoolConfig([{ provider: "openai", model: "gpt-5.4" }]);
3031+
const { config } = canonicalPoolConfig([{ provider: "openai-apikey", model: "gpt-5.4" }]);
30323032
globalThis.fetch = (async (input: unknown, init?: RequestInit) => {
3033-
const url = input instanceof Request ? input.url : String(input);
3034-
if (!url.includes("chatgpt.com")) {
3033+
const url = typeof input === "object" && input !== null && "url" in input
3034+
? String((input as Request).url)
3035+
: String(input);
3036+
if (!url.includes("api.openai.com")) {
30353037
return originalFetch(input as RequestInfo, init);
30363038
}
30373039
// Only the codex/responses child turn is under test; side probes (e.g. the
30383040
// wham/usage quota check) just get a tolerated non-2xx.
3039-
if (!url.includes("backend-api/codex/responses")) {
3041+
if (!url.includes("api.openai.com/v1/responses")) {
30403042
return Response.json({ error: { message: "probe not under test" } }, { status: 403 });
30413043
}
30423044
const body = JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>;
@@ -3051,7 +3053,7 @@ describe("combo compact failover", () => {
30513053
response: {
30523054
id: "resp_compact",
30533055
status: "completed",
3054-
output: [{ type: "compaction", encrypted_content: encodeCompactionSummary("compact summary") }],
3056+
output: [{ type: "compaction", encrypted_content: "gAAAAABm-native-openai-ciphertext" }],
30553057
},
30563058
};
30573059
return new Response([
@@ -3068,11 +3070,11 @@ describe("combo compact failover", () => {
30683070
const response = await postCompactLogged(config);
30693071
expect(response.status).toBe(200);
30703072
const json = await response.json() as { output?: unknown[] };
3071-
expect(JSON.stringify(json.output)).toContain("compact summary");
3073+
expect(json.output).toEqual([expect.objectContaining({
3074+
type: "compaction", encrypted_content: "gAAAAABm-native-openai-ciphertext",
3075+
})]);
30723076
expect(bodies).toHaveLength(1);
30733077
expect(bodies[0]!.stream).toBe(true);
3074-
// Canonical children keep the native compaction_trigger item — only
3075-
// non-native targets get the summarizer prompt injected (covered above).
3076-
expect(JSON.stringify(bodies[0]!.input)).toContain("compaction_trigger");
3078+
expect(JSON.stringify(bodies[0]!.input)).toContain("CONTEXT CHECKPOINT COMPACTION");
30773079
});
30783080
});

0 commit comments

Comments
 (0)