Skip to content

Commit e01b2fb

Browse files
committed
fix(codex): route Daybreak compaction through responses
1 parent 4acb884 commit e01b2fb

4 files changed

Lines changed: 102 additions & 7 deletions

File tree

‎src/server/responses/compact.ts‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import type { EffectiveSubagentRoster, SpawnAgentSurface } from "../../codex/cat
126126
import { codexAuthContextLogLabel } from "../../codex/account-label";
127127

128128
import {
129+
codexAccountGatedCanonicalWireModel,
129130
decodeRequestErrorResponse,
130131
handleResponses,
131132
preAuthUpstreamHostCircuitKey,
@@ -304,6 +305,7 @@ export async function handleResponsesCompact(
304305
return formatErrorResponse(404, "invalid_request_error", err instanceof Error ? err.message : String(err));
305306
}
306307
const selectedModelId = route.modelId;
308+
const accountGatedCompactWireModel = codexAccountGatedCanonicalWireModel(String(raw.model));
307309
logCtx.requestedModel = raw.model;
308310
logCtx.model = selectedModelId;
309311
logCtx.routeDecision = route.routeDecision;
@@ -334,7 +336,7 @@ export async function handleResponsesCompact(
334336
// Native /responses/compact exists on the canonical ChatGPT backend and on the
335337
// official OpenAI API. Any other Responses-shaped gateway must take the routed
336338
// summarizer path below, or compaction fails against an endpoint it never had (#422).
337-
if (supportsNativeResponsesCompactEndpoint(route.providerName, route.provider)) {
339+
if (supportsNativeResponsesCompactEndpoint(route.providerName, route.provider) && !accountGatedCompactWireModel) {
338340
if (req.signal.aborted) {
339341
return formatErrorResponse(499, "client_cancelled", "Client cancelled compact request");
340342
}
@@ -666,7 +668,10 @@ export async function handleResponsesCompact(
666668
const inputItems = Array.isArray(raw.input) ? (raw.input as unknown[]) : [];
667669
const internalBody = {
668670
...raw,
669-
stream: false,
671+
// Canonical ChatGPT Responses rejects non-streaming turns. Daybreak cannot use the
672+
// native compact endpoint either, so run its synthetic compaction as SSE and collapse
673+
// the completed event back into the v1 compact JSON contract below.
674+
stream: accountGatedCompactWireModel ? true : false,
670675
input: [...inputItems, { type: "compaction_trigger" }],
671676
};
672677
const internalHeaders = new Headers({ "content-type": "application/json" });
@@ -682,10 +687,36 @@ export async function handleResponsesCompact(
682687
const response = await handleResponses(internalReq, config, logCtx, { abortSignal: req.signal, turnAdmissionLease, ...(admission ? { admission } : {}) });
683688
if (!response.ok) return response;
684689
let json: { output?: unknown[]; status?: unknown; error?: unknown };
685-
try {
686-
json = await response.json() as { output?: unknown[]; status?: unknown; error?: unknown };
687-
} catch {
688-
return formatErrorResponse(502, "server_error", "compaction turn returned a non-JSON response");
690+
if (response.headers.get("content-type")?.includes("text/event-stream")) {
691+
if (!response.body) {
692+
return formatErrorResponse(502, "server_error", "compaction turn returned an empty event stream");
693+
}
694+
const terminal = { status: "incomplete" as "completed" | "failed" | "incomplete" };
695+
let completed: { id?: unknown; output?: unknown; status?: unknown } | undefined;
696+
await new Promise<void>(resolve => {
697+
consumeForInspection(
698+
response.body!,
699+
status => { terminal.status = status; },
700+
req.signal,
701+
resolve,
702+
undefined,
703+
undefined,
704+
value => { completed = value; },
705+
);
706+
});
707+
if (req.signal.aborted) {
708+
return formatErrorResponse(499, "client_cancelled", "Client cancelled compact request");
709+
}
710+
if (terminal.status !== "completed" || !completed) {
711+
return formatErrorResponse(502, "upstream_error", `compaction turn did not complete (status: ${terminal.status})`);
712+
}
713+
json = completed as { output?: unknown[]; status?: unknown; error?: unknown };
714+
} else {
715+
try {
716+
json = await response.json() as { output?: unknown[]; status?: unknown; error?: unknown };
717+
} catch {
718+
return formatErrorResponse(502, "server_error", "compaction turn returned a non-JSON response");
719+
}
689720
}
690721
// The internal turn answers 200 even when it failed or was truncated, so the body
691722
// has to be inspected. Reporting a failure beats installing "(no summary
@@ -714,6 +745,13 @@ export async function handleResponsesCompact(
714745
`compaction turn produced ${compactionItems.length} compaction items, expected exactly 1`,
715746
);
716747
}
748+
// The canonical Responses stream returns a real OpenAI-encrypted compaction item. OCX cannot
749+
// and should not decrypt it; /responses/compact callers can consume that item directly.
750+
if (accountGatedCompactWireModel) {
751+
return new Response(JSON.stringify({ output: compactionItems }), {
752+
headers: { "Content-Type": "application/json" },
753+
});
754+
}
717755
const encrypted = compactionItems[0]!.encrypted_content;
718756
const decoded = typeof encrypted === "string" ? decodeCompactionSummary(encrypted) : null;
719757
// An empty `ocx1:` envelope decodes to "" rather than null, so length is what matters.

‎src/server/responses/core.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,18 @@ const CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS: ReadonlyMap<string, string> = n
545545
["gpt-daybreak-blue-latest", "gpt-5.6-sol"],
546546
]);
547547

548+
export function codexAccountGatedCanonicalWireModel(modelId: string): string | undefined {
549+
const exact = CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS.get(modelId);
550+
if (exact) return exact;
551+
for (const [selector, wireModel] of CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS) {
552+
if (slugsEquivalent(modelId, selector)) return wireModel;
553+
}
554+
return undefined;
555+
}
556+
548557
function applyCodexAccountGatedWireNormalization(parsed: OcxParsedRequest, route: RouteResult): void {
549558
if (!isCanonicalOpenAiForwardProvider(route.provider)) return;
550-
const wireModel = CODEX_ACCOUNT_GATED_CANONICAL_WIRE_MODELS.get(route.modelId);
559+
const wireModel = codexAccountGatedCanonicalWireModel(route.modelId);
551560
if (!wireModel) return;
552561

553562
parsed.modelId = wireModel;

‎structure/08_openai-provider-tiers.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ preserving a stale one would block every later migration.
128128

129129
- `gpt-daybreak-blue-latest` remains the catalog and entitlement identity, but the canonical
130130
ChatGPT wire uses `gpt-5.6-sol`, the serving id reported by successful Daybreak responses.
131+
Daybreak compaction uses the existing synthetic `/responses` compaction path instead of the
132+
native `/responses/compact` endpoint, whose model support is selector-specific.
131133
The optional `prompt_cache_retention` hint is removed on this route because Daybreak's
132134
authenticated catalog does not advertise it and upstream rejects it before execution.
133135

‎tests/server-auth.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,52 @@ describe("server local API auth", () => {
21712171
}
21722172
});
21732173

2174+
test("#2097: Daybreak compact uses the stable wire model without retention", async () => {
2175+
const model = "gpt-daybreak-blue-latest";
2176+
let upstreamBody: Record<string, unknown> | undefined;
2177+
let upstreamUrl = "";
2178+
const harness = await startPoolRetryHarness(
2179+
async (_accountId, request) => {
2180+
upstreamUrl = request.url;
2181+
upstreamBody = await request.json() as Record<string, unknown>;
2182+
return new Response([
2183+
'event: response.output_item.done',
2184+
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"compaction","encrypted_content":"gAAAAAB-test-opaque"}}',
2185+
'',
2186+
'event: response.completed',
2187+
'data: {"type":"response.completed","response":{"status":"completed","output":[]}}',
2188+
'',
2189+
'data: [DONE]',
2190+
'',
2191+
].join("\n"), {
2192+
headers: { "content-type": "text/event-stream" },
2193+
});
2194+
},
2195+
{
2196+
secondAccount: false,
2197+
modelRosterByAccount: { "acct-pool-a": ["gpt-5.6-sol", model] },
2198+
},
2199+
);
2200+
try {
2201+
const response = await harness.request({
2202+
model,
2203+
path: "/v1/responses/compact",
2204+
extraBody: { prompt_cache_retention: "24h" },
2205+
});
2206+
expect(response.status).toBe(200);
2207+
expect(await response.json()).toEqual({
2208+
output: [{ type: "compaction", encrypted_content: "gAAAAAB-test-opaque" }],
2209+
});
2210+
expect(upstreamUrl).toEndWith("/responses");
2211+
expect(upstreamBody?.model).toBe("gpt-5.6-sol");
2212+
expect(upstreamBody?.stream).toBe(true);
2213+
expect(upstreamBody).not.toHaveProperty("prompt_cache_retention");
2214+
expect(harness.dispatches).toEqual(["acct-pool-a"]);
2215+
} finally {
2216+
await stopPoolRetryHarness(harness);
2217+
}
2218+
});
2219+
21742220
test("#2097: a confirmed entitled account survives two transient unsupported-model 400s in place", async () => {
21752221
const model = "gpt-daybreak-blue-latest";
21762222
let attempts = 0;

0 commit comments

Comments
 (0)