Skip to content

Commit add8a55

Browse files
olddonkeyclaude
andcommitted
fix(responses): keep compaction blobs on the backend that minted them
A replayed compaction item carries an `encrypted_content` blob only its minting backend can decode, and Codex replays it on every later turn. Two paths modified or misrouted it, and because the item outlives the failure in the client transcript, both wedged the session until its history was cleared — the routed compaction turn the proxy itself drives replays the same item. Relay: `scrubOcxCompactionItems` treated every non-`ocx1:` blob as OpenAI's and forwarded it verbatim, with no check that the destination was the issuer. A session that compacted on a canonical route and then switched to a routed provider sent that blob to an upstream that could only answer "Could not decode the compaction blob". Native blobs now travel only to destinations that mint them — forward-auth routes, which relay the caller's own OpenAI credentials to the ChatGPT backend or a relay in front of it, and the official OpenAI API under key auth — and degrade elsewhere to the same opaque note the bridged parser uses. Backfill: the response-side exemption list named `compaction` alone, so `compaction_summary` and `context_compaction` received synthesized ids that the client stored and replayed as "modified from the compact response". That divergence was possible because the compact wire family was enumerated in three places; it is now one predicate in `src/responses/compaction.ts`. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 03735ec commit add8a55

8 files changed

Lines changed: 204 additions & 42 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { createHash } from "node:crypto";
22
import type { IncomingMeta, ProviderAdapter } from "./base";
33
import { namespacedToolName, type AdapterEvent, type OcxParsedRequest, type OcxProviderConfig, type OcxUsage, type TierDecision } from "../types";
44
import { catalogModelSupportsReasoningSummaries } from "../codex/catalog";
5-
import { COMPACT_PROMPT, decodeCompactionSummary, SUMMARY_PREFIX } from "../responses/compaction";
5+
import { COMPACT_PROMPT, compactionItemToText, decodeCompactionSummary, isCompactionItemType } from "../responses/compaction";
66
import { collectResponsesToolGroups } from "../responses/tool-groups";
77
import { isHostedToolUnsupportedForModel } from "../responses/hosted-tool-policy";
88
import { decodeServerSentEvents } from "../lib/sse-decoder";
9-
import { CODEX_FORWARD_BASE_URL, isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
9+
import { CODEX_FORWARD_BASE_URL, destinationDecodesNativeCompactionBlob, isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
1010
import { OCX_REASONING_PREFIX } from "../responses/reasoning-envelope";
1111
import { modelRecordValue } from "../reasoning-effort";
1212
import type { TranslatorBudget } from "../lib/translator-budget";
@@ -143,25 +143,33 @@ function stripItemIdsWhenUnstored(body: unknown): unknown {
143143
}
144144

145145
/**
146-
* Replace proxy-minted compaction items (`encrypted_content` starting with `ocx1:`) with plain
147-
* user messages before forwarding to the ChatGPT backend. Our envelope is transparent base64, not
148-
* OpenAI encryption — the native backend cannot decrypt it and would reject the request. Real
149-
* OpenAI-encrypted compaction items are forwarded untouched.
146+
* Normalize replayed compaction items for the destination backend.
147+
*
148+
* A compaction item carries an `encrypted_content` blob the client replays verbatim on every later
149+
* turn, and only the backend that minted it can decode it. Proxy-minted `ocx1:` envelopes are
150+
* transparent base64 rather than encryption, so no upstream can read them and they always become
151+
* plain user messages. A foreign blob was minted by an OpenAI-operated backend: forwarding it to a
152+
* different destination makes that upstream reject the turn ("Could not decode the compaction
153+
* blob"), and because the item lives in the client transcript the rejection repeats on every later
154+
* turn — including the compaction turn the proxy itself drives — leaving the session unable to
155+
* recover. Off those destinations it degrades to the same note the bridged parser uses.
156+
*
157+
* A bare `context_compaction` marker carries no blob and is forwarded untouched.
150158
*/
151-
function scrubOcxCompactionItems(body: unknown): unknown {
159+
function scrubOcxCompactionItems(body: unknown, destinationDecodesNativeBlob: boolean): unknown {
152160
if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
153161

154162
let changed = false;
155163
const input = body.input.map(item => {
156-
if (!isPlainObject(item)) return item;
157-
if (item.type !== "compaction" && item.type !== "compaction_summary" && item.type !== "context_compaction") return item;
158-
const decoded = typeof item.encrypted_content === "string" ? decodeCompactionSummary(item.encrypted_content) : null;
159-
if (decoded === null) return item;
164+
if (!isPlainObject(item) || !isCompactionItemType(item.type)) return item;
165+
const encrypted = typeof item.encrypted_content === "string" ? item.encrypted_content : undefined;
166+
if (encrypted === undefined) return item;
167+
if (decodeCompactionSummary(encrypted) === null && destinationDecodesNativeBlob) return item;
160168
changed = true;
161169
return {
162170
type: "message",
163171
role: "user",
164-
content: [{ type: "input_text", text: `${SUMMARY_PREFIX}\n\n${decoded}` }],
172+
content: [{ type: "input_text", text: compactionItemToText(encrypted) }],
165173
};
166174
});
167175

@@ -1572,7 +1580,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
15721580
outBody = rewritten.body;
15731581
convertedRoutedToolSearchNames = rewritten.names;
15741582
}
1575-
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody), { preserveRawReasoningContent: provider.preserveResponsesReasoningContent === true })))))));
1583+
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody, destinationDecodesNativeCompactionBlob(provider)), { preserveRawReasoningContent: provider.preserveResponsesReasoningContent === true })))))));
15761584
const finalBody = stripDisabledReasoningSummaries(
15771585
normalizeConfiguredReasoningSummaryDelivery(sanitizedBody, provider, parsed.modelId),
15781586
provider,

‎src/providers/openai-tiers.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ export function supportsNativeResponsesCompactEndpoint(
5454
&& normalizedBaseUrl(provider.baseUrl) === OPENAI_API_BASE_URL;
5555
}
5656

57+
/**
58+
* Whether this destination can decode a native (non-`ocx1:`) compaction blob.
59+
*
60+
* Only the backend that minted a blob can decode it, and only OpenAI-operated Responses surfaces
61+
* mint them. Forward auth relays the caller's own OpenAI credentials, so a forward destination is
62+
* either the ChatGPT backend or a relay standing in front of one — both can read what they issued,
63+
* and a self-hosted relay must not lose its users' compacted history to a guess about its URL. The
64+
* remaining positive case is the official OpenAI API under key auth.
65+
*
66+
* Everything else is a routed provider carrying its own credentials to a non-OpenAI backend, which
67+
* can only reject a blob it never issued. Keyed by destination rather than provider id: a blob's
68+
* issuer is the URL that produced it, not the local config key a replay travels under.
69+
*/
70+
export function destinationDecodesNativeCompactionBlob(provider: OcxProviderConfig): boolean {
71+
if (provider.authMode === "forward") return true;
72+
return provider.adapter === "openai-responses"
73+
&& normalizedBaseUrl(provider.baseUrl) === OPENAI_API_BASE_URL;
74+
}
75+
5776
export interface OpenAiTierMigrationProjection {
5877
config: OcxConfig;
5978
changed: boolean;

‎src/responses/compaction.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ export const SUMMARY_PREFIX = "Another language model started to solve this prob
3333

3434
export const OPAQUE_COMPACTION_NOTE = "[earlier conversation was compacted; the summary is stored in a format this model cannot read]";
3535

36+
/**
37+
* Item types in the compact wire family. Each carries an `encrypted_content` blob the client
38+
* replays verbatim on every later turn, and the minting backend verifies it is unmodified.
39+
*
40+
* Keep this the only enumeration: a copy that listed just `compaction` let the response-side
41+
* field backfill synthesize ids into the other two, which the client then replayed as "modified
42+
* from the compact response".
43+
*/
44+
const COMPACTION_ITEM_TYPES: ReadonlySet<string> = new Set([
45+
"compaction",
46+
"compaction_summary",
47+
"context_compaction",
48+
]);
49+
50+
export function isCompactionItemType(type: unknown): boolean {
51+
return typeof type === "string" && COMPACTION_ITEM_TYPES.has(type);
52+
}
53+
3654
export function encodeCompactionSummary(summary: string): string {
3755
return OCX_COMPACTION_PREFIX + Buffer.from(summary, "utf-8").toString("base64");
3856
}

‎src/responses/parser.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { namespacedToolName, toolChoiceCandidates } from "../types";
1515
import { responsesRequestSchema } from "./schema";
1616
import { providerMetadataFromResponsesFunctionCall } from "./provider-opaque-metadata";
1717
import { lookupReplayThoughtSignature } from "./thought-signature-replay";
18-
import { compactionItemToText } from "./compaction";
18+
import { compactionItemToText, isCompactionItemType } from "./compaction";
1919
import { previousResponseReplayPrefixLength } from "./state";
2020
import { decodeReasoningEnvelope } from "./reasoning-envelope";
2121
import { extractHostedWebSearch, WEB_SEARCH_TOOL_NAME } from "../web-search/synthetic-tool";
@@ -434,7 +434,7 @@ export function parseRequest(
434434
continue;
435435
}
436436

437-
if (effectiveType === "compaction" || effectiveType === "compaction_summary" || effectiveType === "context_compaction") {
437+
if (isCompactionItemType(effectiveType)) {
438438
// A stored summary from a previous compaction. Decode our ocx1 envelope into plain text so
439439
// the routed model keeps the compacted context; real OpenAI-encrypted blobs degrade to a note.
440440
// `context_compaction` (encrypted_content optional) is codex-rs's local-compaction marker;

‎src/server/responses/responses-field-backfill.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
sseDataPayload,
2525
type SseBlockRewrite,
2626
} from "../sse-payload-rewrite";
27+
import { isCompactionItemType } from "../../responses/compaction";
2728

2829
function isPlainObject(value: unknown): value is Record<string, unknown> {
2930
return !!value && typeof value === "object" && !Array.isArray(value);
@@ -119,24 +120,19 @@ function backfillContentArray(content: unknown): unknown {
119120
return changed ? repaired : content;
120121
}
121122

122-
/**
123-
* Item types that are NOT Responses output items and must be returned byte-for-byte.
124-
*
125-
* `compaction` is the `/v1/responses/compact` wire format, not a Responses output item. It has
126-
* no `id` in that contract, so synthesizing one changes a response body the client compares
127-
* exactly. The backfill exists to satisfy strict Responses decoders; a shape those decoders
128-
* never see is outside its remit.
129-
*/
130-
const NON_RESPONSES_ITEM_TYPES: ReadonlySet<string> = new Set(["compaction"]);
131-
132123
/**
133124
* Walk an output item and backfill output_text parts in its content.
134125
* Also backfills a missing required id on the item itself.
135126
* Returns the same object reference if nothing changed.
136127
*/
137128
function backfillOutputItem(item: unknown, slot: ItemIdSlot): unknown {
138129
if (!isPlainObject(item)) return item;
139-
if (typeof item.type === "string" && NON_RESPONSES_ITEM_TYPES.has(item.type)) return item;
130+
// The compact wire family is the `/v1/responses/compact` format, not a Responses output item.
131+
// Those items have no `id` in that contract, so synthesizing one changes a response body the
132+
// client compares exactly — and the client replays the item on every later turn, where the
133+
// minting backend rejects it as modified. The backfill exists to satisfy strict Responses
134+
// decoders; a shape those decoders never see is outside its remit.
135+
if (isCompactionItemType(item.type)) return item;
140136
const content = item.content;
141137
const repaired = backfillContentArray(content);
142138
const withId = backfillItemId(item, slot);

‎structure/04_transports-and-sidecars.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ alone never opt a gateway in.
7070
and before the `/v1/*` guard. Unknown `/v1/*` paths return JSON 404 errors instead of falling through
7171
to GUI static serving.
7272

73+
A replayed compaction item carries an `encrypted_content` blob only its minting backend can decode,
74+
and the client replays it on every later turn. The proxy's own `ocx1:` envelopes are transparent
75+
base64, so they always lower to plain user messages. A native blob is relayed only to destinations
76+
that mint them — forward-auth routes, which relay the caller's own OpenAI credentials to the ChatGPT
77+
backend or a relay in front of it, and the official OpenAI API under key auth. On any other routed
78+
destination it degrades to the same opaque note the bridged parser uses, because forwarding it there
79+
fails the turn and the item outlives the failure in the client transcript, repeating on every later
80+
turn including the compaction turn the proxy itself drives. Compact-wire items are also exempt from
81+
the `store: false` item-id strip and from response-side field backfill: their id is not a stored-item
82+
reference, and editing the item is what the minting backend rejects as modified.
83+
84+
[Decision Log]
85+
- 목적과 의도: Keep a session usable after its history crosses backends, instead of wedging it on a
86+
compaction blob the current upstream cannot decode.
87+
- 기존 구현 및 제약 조건: Compaction handling was binary — `ocx1:` envelopes were ours, everything
88+
else was assumed to be OpenAI's and forwarded verbatim, with no record of which upstream minted a
89+
blob. Response-side field backfill exempted only `compaction`, so its two sibling types received
90+
synthesized ids the client then replayed.
91+
- 검토한 주요 대안: Tag every compaction item with its minting provider/credential/model identity;
92+
drop compaction items on any route change; gate relay on the destination that would decode them.
93+
- 선택한 방식: Relay a native blob only to destinations that mint them and degrade it elsewhere, and
94+
treat the compact wire family as one enumeration so id-bearing passes cannot diverge per type.
95+
- 다른 대안 대신 이 방식을 선택한 이유: Full provenance tagging needs per-conversation state this
96+
boundary does not have, while dropping on any change would discard compacted context that still
97+
round-trips correctly; the destination test is decidable from the request alone.
98+
- 장점, 단점 및 영향: A cross-backend session degrades one compaction summary to a note instead of
99+
failing every later turn. A self-hosted forward relay keeps its blobs. The cost is that a routed
100+
gateway that did mint its own native blob would also see a note — no such gateway exists today,
101+
since routed compaction always produces an `ocx1:` envelope.
102+
73103
### Mixed-wire provider defaults
74104

75105
Registry `modelWireDefaults` select an evidence-backed upstream protocol for an exact model without

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { openaiResponsesUrl } from "../src/adapters/openai-responses-url";
44
import { enrichProviderFromRegistry, providerConfigSeed } from "../src/providers/derive";
55
import { getProviderRegistryEntry } from "../src/providers/registry";
66
import { sanitizeEncryptedContentInPlace } from "../src/server/responses";
7+
import {
8+
encodeCompactionSummary,
9+
OPAQUE_COMPACTION_NOTE,
10+
SUMMARY_PREFIX,
11+
} from "../src/responses/compaction";
712
import { createTranslatorBudget } from "../src/lib/translator-budget";
813
import { withTestTranslatorBudget } from "./helpers/translator-budget";
914

@@ -735,6 +740,7 @@ describe("OpenAI Responses passthrough sanitization", () => {
735740
expect(storedBody.input.map(item => item.id)).toEqual(["msg_abc", "fc_xyz", "rs_123"]);
736741
});
737742

743+
738744
test("drops raw reasoning input content before native GPT passthrough", () => {
739745
const adapter = createResponsesPassthroughAdapter(provider);
740746
const request = adapter.buildRequest({
@@ -2135,6 +2141,87 @@ describe("OpenAI Responses forward-mode unsupported param stripping", () => {
21352141
});
21362142
});
21372143

2144+
describe("replayed compaction blobs", () => {
2145+
type PassthroughProvider = Parameters<typeof createResponsesPassthroughAdapter>[0];
2146+
2147+
// Shaped like a blob minted by an OpenAI-operated backend: opaque, no `ocx1:` envelope.
2148+
const NATIVE_BLOB = "gAAAAAB-openai-minted-compaction-blob";
2149+
const routedProvider: PassthroughProvider = {
2150+
adapter: "openai-responses",
2151+
baseUrl: "https://api.x.ai/v1",
2152+
authMode: "key",
2153+
apiKey: "xai-test",
2154+
};
2155+
const openaiKeyedProvider: PassthroughProvider = {
2156+
adapter: "openai-responses",
2157+
baseUrl: "https://api.openai.com/v1",
2158+
authMode: "key",
2159+
apiKey: "sk-test",
2160+
};
2161+
// Forward auth relays the caller's own OpenAI credentials, so a self-hosted relay is standing in
2162+
// front of an OpenAI backend and must keep its users' compacted history.
2163+
const forwardRelayProvider: PassthroughProvider = {
2164+
adapter: "openai-responses",
2165+
baseUrl: "https://relay.example/backend-api/codex",
2166+
authMode: "forward",
2167+
};
2168+
2169+
function forwardedInput(target: PassthroughProvider, input: unknown[]): Record<string, unknown>[] {
2170+
const request = createResponsesPassthroughAdapter(target).buildRequest({
2171+
modelId: "grok-4.6",
2172+
context: { messages: [] },
2173+
stream: true,
2174+
options: {},
2175+
_rawBody: { model: "grok-4.6", store: false, input },
2176+
}, { headers: new Headers({ authorization: "Bearer token" }) });
2177+
return (JSON.parse(request.body) as { input: Record<string, unknown>[] }).input;
2178+
}
2179+
2180+
// Forwarding a blob to a backend that did not mint it fails the turn, and because the item lives
2181+
// in the client transcript the failure repeats on every later turn — including the compaction turn
2182+
// — so the session cannot recover until its history is cleared.
2183+
test("degrades a foreign blob to a note on a destination that cannot decode it", () => {
2184+
for (const type of ["compaction", "compaction_summary", "context_compaction"]) {
2185+
const forwarded = forwardedInput(routedProvider, [
2186+
{ type, encrypted_content: NATIVE_BLOB },
2187+
]);
2188+
expect(forwarded[0]).toEqual({
2189+
type: "message",
2190+
role: "user",
2191+
content: [{ type: "input_text", text: OPAQUE_COMPACTION_NOTE }],
2192+
});
2193+
expect(JSON.stringify(forwarded)).not.toContain(NATIVE_BLOB);
2194+
}
2195+
});
2196+
2197+
test("forwards a foreign blob untouched to the backends that mint them", () => {
2198+
const item = { type: "compaction", encrypted_content: NATIVE_BLOB };
2199+
for (const target of [provider, openaiKeyedProvider, forwardRelayProvider]) {
2200+
expect(forwardedInput(target, [item])[0]).toEqual(item);
2201+
}
2202+
});
2203+
2204+
// The proxy's own envelope is transparent base64, so no upstream can read it anywhere.
2205+
test("lowers proxy-minted ocx1 envelopes on every destination", () => {
2206+
const item = { type: "compaction", encrypted_content: encodeCompactionSummary("prior work") };
2207+
for (const target of [provider, openaiKeyedProvider, routedProvider]) {
2208+
expect(forwardedInput(target, [item])[0]).toEqual({
2209+
type: "message",
2210+
role: "user",
2211+
content: [{ type: "input_text", text: `${SUMMARY_PREFIX}\n\nprior work` }],
2212+
});
2213+
}
2214+
});
2215+
2216+
// A bare marker carries no blob, so there is nothing to mis-route.
2217+
test("leaves a bare context_compaction marker alone", () => {
2218+
const item = { type: "context_compaction" };
2219+
for (const target of [provider, routedProvider]) {
2220+
expect(forwardedInput(target, [item])[0]).toEqual(item);
2221+
}
2222+
});
2223+
});
2224+
21382225
describe("openaiResponsesUrl", () => {
21392226
test("does not strip mid-path /v1 or a non-endpoint responses suffix", () => {
21402227
expect(openaiResponsesUrl("https://proxy.example.com/v1/relay")).toBe(

0 commit comments

Comments
 (0)