Skip to content

Commit c26edb9

Browse files
fix(proxy): promote caller x-session-id to session_id so non-Codex prefixes stay warm
1 parent a141b83 commit c26edb9

7 files changed

Lines changed: 183 additions & 4 deletions

File tree

‎scripts/test-layout/layout.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"provider-antigravity-family-catalog.test.ts": "providers",
99
"claude-subagent-model-force.test.ts": "claude-integration",
1010
"subagent-model-force-api.test.ts": "server",
11+
"caller-session-identity.test.ts": "server",
1112
"agent-subagent-force.test.ts": "cli",
1213
"compaction-event-ownership.test.ts": "responses",
1314
"zed-retained-budget.test.ts": "providers",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Third-party clients of the public endpoints usually speak the OpenAI/Anthropic wire formats
3+
* without any Codex session identity. The ChatGPT Codex backend only reuses a warmed prompt
4+
* prefix for requests that carry `session_id` (#6481), and opencodex reads the same header for
5+
* thread affinity and request-log conversation identity — so such callers pay full price on
6+
* every turn even with byte-identical prefixes (#6520: a 25-turn agent task with stable
7+
* prefixes reported cache_write_tokens: 0 throughout, while Codex clients on the same account
8+
* and model reused 97%+ of their input).
9+
*
10+
* Many non-Codex agents already send a stable `x-session-id` per conversation (ZCode does; the
11+
* proxy's own adapters treat the same header as a session marker). Promote it to `session_id`
12+
* exactly the way the managed Grok surface promotes `x-grok-conv-id` (#6482): an explicit caller
13+
* session header always wins, and values that do not look like an opaque id are left alone.
14+
*/
15+
const SESSION_HEADERS = ["session_id", "session-id", "thread-id"] as const;
16+
const CALLER_SESSION_HEADER = "x-session-id";
17+
// Codex accepts UUIDs and similar opaque ids; anything else is not promoted.
18+
const SAFE_CALLER_SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
19+
20+
export function callerSessionId(headers: Headers): string | undefined {
21+
if (SESSION_HEADERS.some(name => headers.has(name))) return undefined;
22+
const value = headers.get(CALLER_SESSION_HEADER)?.trim();
23+
if (!value || !SAFE_CALLER_SESSION_ID.test(value)) return undefined;
24+
return value;
25+
}
26+
27+
/** Returns `req` unchanged unless the caller's `x-session-id` can stand in for the missing `session_id`. */
28+
export function withCallerSessionIdentity(req: Request): Request {
29+
const sessionId = callerSessionId(req.headers);
30+
if (!sessionId) return req;
31+
const headers = new Headers(req.headers);
32+
headers.set("session_id", sessionId);
33+
// The clone keeps the unread body stream and the caller's abort signal.
34+
return new Request(req, { headers });
35+
}

‎src/server/index/serve-options.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ import { readyProtocolMetadata } from "../../remote/protocol";
203203
import { modelCapabilityFields } from "../models-capabilities";
204204
import { createWebsocketHandler } from "./websocket-handler";
205205
import { withGrokSessionIdentity } from "../../grok/session-identity";
206+
import { withCallerSessionIdentity } from "../caller-session-identity";
206207

207208
export type ServerIngress = "public" | "unauthenticated-loopback" | "hub-management" | "claude-intercept" | "hub-link";
208209

@@ -1477,7 +1478,7 @@ export function createServeOptions(ctx: ServeOptionsContext) {
14771478
return runAdmittedHttpTurn(req, policy, async turnAdmissionLease => {
14781479
let response: Response;
14791480
try {
1480-
response = await handleResponses(withGrokSessionIdentity(req), config, logCtx, {
1481+
response = await handleResponses(withCallerSessionIdentity(withGrokSessionIdentity(req)), config, logCtx, {
14811482
turnAdmissionLease,
14821483
admission,
14831484
onRequestBodyRead: () => disableResponsesRequestTimeout(req, requestServer),
@@ -1555,7 +1556,7 @@ export function createServeOptions(ctx: ServeOptionsContext) {
15551556
// pre-translation stream + native passthrough callbacks) — do not re-wrap the
15561557
// translated Anthropic stream here.
15571558
return runAdmittedHttpTurn(req, policy, async turnAdmissionLease => withCors(
1558-
await handleClaudeMessages(req, config, logCtx, { requestId, start, turnAdmissionLease, admission }, policy, { claudeIntercept: ingress === "claude-intercept" }),
1559+
await handleClaudeMessages(withCallerSessionIdentity(req), config, logCtx, { requestId, start, turnAdmissionLease, admission }, policy, { claudeIntercept: ingress === "claude-intercept" }),
15591560
req,
15601561
policy,
15611562
), { requestId, start, logCtx });

‎tests/fixtures/test-layout-expected.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"provider-antigravity-family-catalog.test.ts": "providers",
66
"claude-subagent-model-force.test.ts": "claude-integration",
77
"subagent-model-force-api.test.ts": "server",
8+
"caller-session-identity.test.ts": "server",
89
"agent-subagent-force.test.ts": "cli",
910
"compaction-event-ownership.test.ts": "responses",
1011
"zed-retained-budget.test.ts": "providers",

‎tests/providers/xai/grok-session-identity.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,5 @@ describe("Grok turns reach the ChatGPT Codex backend with session_id", () => {
129129
test("the /v1/responses route hands handleResponses the Grok-promoted request", () => {
130130
// Pins the one-line wiring; the cases above call the helper directly.
131131
const source = readFileSync(repoPath("src", "server", "index", "serve-options.ts"), "utf8");
132-
expect(source).toContain("await handleResponses(withGrokSessionIdentity(req), config, logCtx, {");
132+
expect(source).toContain("await handleResponses(withCallerSessionIdentity(withGrokSessionIdentity(req)), config, logCtx, {");
133133
});
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { callerSessionId, withCallerSessionIdentity } from "../../src/server/caller-session-identity";
6+
import { withGrokSessionIdentity } from "../../src/grok/session-identity";
7+
import { handleResponses } from "../../src/server/responses/core";
8+
import { tryAdmitTurn } from "../../src/server/lifecycle";
9+
import type { OcxConfig } from "../../src/types";
10+
import { fakeChatGptJwt } from "../helpers/fake-chatgpt-jwt";
11+
import { installIsolatedCodexHome, type IsolatedCodexHome } from "../helpers/isolated-codex-home";
12+
import { removeTreeWithRetry } from "../helpers/remove-tree";
13+
import { repoPath } from "../helpers/repo-root";
14+
import { acquireOwnedSpendHome } from "../helpers/owned-spend-home";
15+
16+
const SESSION = "0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0";
17+
18+
function callerHeaders(extra: Record<string, string> = {}): Headers {
19+
return new Headers({
20+
"content-type": "application/json",
21+
"x-session-id": SESSION,
22+
...extra,
23+
});
24+
}
25+
26+
describe("callerSessionId", () => {
27+
test("promotes the caller's x-session-id when no session header is present", () => {
28+
expect(callerSessionId(callerHeaders())).toBe(SESSION);
29+
expect(callerSessionId(callerHeaders({ "x-session-id": `sess-${SESSION}` }))).toBe(`sess-${SESSION}`);
30+
});
31+
32+
test("does nothing when the caller sent no x-session-id", () => {
33+
const headers = callerHeaders();
34+
headers.delete("x-session-id");
35+
expect(callerSessionId(headers)).toBeUndefined();
36+
});
37+
38+
for (const explicit of ["session_id", "session-id", "thread-id"]) {
39+
test(`an explicit ${explicit} header wins`, () => {
40+
expect(callerSessionId(callerHeaders({ [explicit]: "caller" }))).toBeUndefined();
41+
});
42+
}
43+
44+
test("ignores empty and unsafe session ids", () => {
45+
expect(callerSessionId(callerHeaders({ "x-session-id": "" }))).toBeUndefined();
46+
expect(callerSessionId(callerHeaders({ "x-session-id": "a b" }))).toBeUndefined();
47+
expect(callerSessionId(callerHeaders({ "x-session-id": "x".repeat(200) }))).toBeUndefined();
48+
});
49+
50+
test("the rewritten request keeps its body and abort signal", async () => {
51+
const controller = new AbortController();
52+
const original = new Request("http://localhost/v1/responses", {
53+
method: "POST", headers: callerHeaders(), body: "{\"a\":1}", signal: controller.signal,
54+
});
55+
const rewritten = withCallerSessionIdentity(original);
56+
expect(rewritten.headers.get("session_id")).toBe(SESSION);
57+
expect(original.headers.has("session_id")).toBe(false);
58+
controller.abort();
59+
expect(rewritten.signal.aborted).toBe(true);
60+
expect(await rewritten.text()).toBe("{\"a\":1}");
61+
});
62+
63+
test("returns the same request when nothing is promoted", () => {
64+
const original = new Request("http://localhost/v1/responses", { method: "POST", body: "{}" });
65+
expect(withCallerSessionIdentity(original)).toBe(original);
66+
});
67+
68+
test("composes after the Grok promotion without double-writing", () => {
69+
const grok = withGrokSessionIdentity(new Request("http://localhost/v1/responses", {
70+
method: "POST",
71+
headers: new Headers({ "x-opencodex-grok": "1", "x-grok-conv-id": SESSION }),
72+
body: "{}",
73+
}));
74+
const composed = withCallerSessionIdentity(grok);
75+
expect(composed.headers.get("session_id")).toBe(SESSION);
76+
expect(composed.headers.get("x-grok-conv-id")).toBe(SESSION);
77+
});
78+
});
79+
80+
describe("Non-Codex callers reach the ChatGPT Codex backend with session_id", () => {
81+
const originalFetch = globalThis.fetch;
82+
let isolated: IsolatedCodexHome;
83+
let home: string;
84+
let previousHome: string | undefined;
85+
let releaseSpendHome: (() => void) | undefined;
86+
let token = "";
87+
88+
beforeEach(() => {
89+
previousHome = process.env.OPENCODEX_HOME;
90+
home = mkdtempSync(join(tmpdir(), "ocx-caller-session-"));
91+
process.env.OPENCODEX_HOME = home;
92+
isolated = installIsolatedCodexHome("ocx-caller-session-codex-");
93+
token = fakeChatGptJwt({ exp: Math.floor(Date.now() / 1000) + 86400, chatgpt_account_id: "fixture-caller-main" });
94+
writeFileSync(join(isolated.path, "auth.json"), JSON.stringify({ tokens: { access_token: token, account_id: "fixture-caller-main" } }));
95+
releaseSpendHome = acquireOwnedSpendHome();
96+
});
97+
afterEach(() => {
98+
releaseSpendHome?.();
99+
releaseSpendHome = undefined;
100+
globalThis.fetch = originalFetch;
101+
isolated.restore();
102+
if (previousHome === undefined) delete process.env.OPENCODEX_HOME;
103+
else process.env.OPENCODEX_HOME = previousHome;
104+
removeTreeWithRetry(home);
105+
});
106+
107+
test("the upstream request carries the promoted session_id", async () => {
108+
const cfg = { openaiProviderTierVersion: 2, providers: {
109+
openai: { adapter: "openai-responses", authMode: "forward", codexAccountMode: "direct", baseUrl: "https://chatgpt.com/backend-api/codex", models: ["gpt-5.6-luna"] },
110+
} } as OcxConfig;
111+
const seen: Array<{ url: string; headers: Headers; body: Record<string, unknown> }> = [];
112+
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
113+
seen.push({ url: String(input), headers: new Headers(init?.headers), body: JSON.parse(String(init?.body)) });
114+
return Response.json({ id: "resp_caller", object: "response", status: "completed", output: [],
115+
usage: { input_tokens: 1, output_tokens: 0, total_tokens: 1 } });
116+
}) as typeof fetch;
117+
const lease = tryAdmitTurn();
118+
expect(lease).not.toBeNull();
119+
try {
120+
const req = withCallerSessionIdentity(new Request("http://localhost/v1/responses", {
121+
method: "POST", headers: callerHeaders({ authorization: `Bearer ${token}` }),
122+
body: JSON.stringify({ model: "openai/gpt-5.6-luna", stream: false, store: false, input: "ping" }),
123+
}));
124+
const logCtx = { model: "", provider: "" } as Parameters<typeof handleResponses>[2];
125+
const response = await handleResponses(req, cfg, logCtx, { turnAdmissionLease: lease!, admission: { kind: "loopback", source: "loopback" }, inboundWire: "responses" });
126+
const text = await response.text();
127+
expect(response.status, text).toBe(200);
128+
const wire = seen.at(-1)!;
129+
expect(wire.url).toBe("https://chatgpt.com/backend-api/codex/responses");
130+
expect(wire.headers.get("session_id")).toBe(SESSION);
131+
expect(logCtx.conversationId).toBeTruthy();
132+
} finally { lease?.release(); }
133+
});
134+
});
135+
136+
test("the public routes hand their handlers the caller-promoted request", () => {
137+
// Pins the two-line wiring; the cases above call the helper directly.
138+
const source = readFileSync(repoPath("src", "server", "index", "serve-options.ts"), "utf8");
139+
expect(source).toContain("await handleResponses(withCallerSessionIdentity(withGrokSessionIdentity(req)), config, logCtx, {");
140+
expect(source).toContain("await handleClaudeMessages(withCallerSessionIdentity(req), config, logCtx,");
141+
});

‎tests/server/loopback-listener-admission.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe("loopback listener policy view", () => {
7676
'await handleClaudeCountTokens(req, config, policy, { claudeIntercept: ingress === "claude-intercept" })',
7777
);
7878
expect(source.slice(messagesStart, chatStart)).toContain(
79-
'await handleClaudeMessages(req, config, logCtx, { requestId, start, turnAdmissionLease, admission }, policy, { claudeIntercept: ingress === "claude-intercept" })',
79+
'await handleClaudeMessages(withCallerSessionIdentity(req), config, logCtx, { requestId, start, turnAdmissionLease, admission }, policy, { claudeIntercept: ingress === "claude-intercept" })',
8080
);
8181
for (const branch of [
8282
source.slice(countTokensStart, messagesStart),

0 commit comments

Comments
 (0)