Skip to content

Commit 79d932c

Browse files
authored
fix(subagents): re-derive native context width from config at catalog read (#2601)
The subagent roster reads the persisted Codex catalog, which is only as fresh as the last sync. A row written before the operator widened the native window kept its old context_window, so a child planned and compacted against a narrower budget than its parent. Measured on a live install: the request path resolved 922000 for gpt-5.6-sol while the on-disk row still held 272000 from a two-day-old sync. With effective_context_window_percent 95 that renders as 258400 - the reported number. Re-applies applyNativeOpenAiContextOverride to the snapshot the roster reads. It is the same function the writer uses, so a fresh catalog is unchanged and a stale one is repaired in memory. The file is not rewritten: ocx sync stays the thing that refreshes it. An unreadable config falls back to the file as written rather than costing the caller its roster.
1 parent 561a7c2 commit 79d932c

2 files changed

Lines changed: 119 additions & 3 deletions

File tree

‎src/server/responses/collaboration.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Server } from "bun";
22
import { bridgeToResponsesSSE, buildResponseJSON, formatErrorResponse, type ResponsesTerminalStatus } from "../../bridge";
33
import {
44
getConfigPath,
5+
loadConfig,
56
multiAgentGuidanceEnabled,
67
resolveEnvValue,
78
} from "../../config";
@@ -10,6 +11,7 @@ import { buildCompactV1Output, COMPACT_PROMPT, decodeCompactionSummary, extractC
1011
import { FORWARD_HEADERS, sanitizeReasoningInputContent } from "../../adapters/openai-responses";
1112
import { expandPreviousResponseInput, previousResponseProviderState, rememberResponseState } from "../../responses/state";
1213
import { routeModel } from "../../router";
14+
import type { RawEntry } from "../../codex/catalog/parsing";
1315
import {
1416
advanceComboAfterFailure,
1517
comboDefaultEffort,
@@ -243,15 +245,47 @@ export async function resolveEffectiveSubagentRoster(
243245
surface: SpawnAgentSurface,
244246
): Promise<EffectiveSubagentRoster> {
245247
const { effectiveSubagentRoster } = await import("../../codex/catalog");
246-
return effectiveSubagentRoster(configuredModels, surface);
248+
return effectiveSubagentRoster(configuredModels, surface, await freshSubagentCatalogEntries());
249+
}
250+
251+
/**
252+
* The persisted Codex catalog, with native context metadata re-derived from the CURRENT config.
253+
*
254+
* The subagent roster reads the catalog FILE, which is only as fresh as the last sync. A row
255+
* written before the operator widened the native window keeps its old `context_window`, so a
256+
* child was planning and compacting against a narrower budget than its parent — measured at
257+
* 272,000 x 95% = 258,400 while the request path resolved 922,000 (#2574).
258+
*
259+
* Re-applying the override is cheap and idempotent: it is the same function the writer uses,
260+
* so a fresh catalog is unchanged and a stale one is repaired in memory rather than silently
261+
* believed. It does not rewrite the file — the row on disk stays whatever the last sync wrote,
262+
* and `ocx sync` remains what refreshes it.
263+
*/
264+
async function freshSubagentCatalogEntries(): Promise<RawEntry[]> {
265+
const { readCatalog, readCodexCatalogPath, nativeContextLimits } = await import("../../codex/catalog");
266+
const { applyNativeOpenAiContextOverride } = await import("../../codex/catalog/parsing");
267+
const entries = readCatalog(readCodexCatalogPath())?.models ?? [];
268+
if (entries.length === 0) return [];
269+
let limits: ReturnType<typeof nativeContextLimits>;
270+
try {
271+
limits = nativeContextLimits(loadConfig());
272+
} catch {
273+
// An unreadable config must not cost the caller its roster; serve the file as written.
274+
return entries;
275+
}
276+
return entries.map(entry => {
277+
const clone = { ...entry };
278+
applyNativeOpenAiContextOverride(clone, limits);
279+
return clone;
280+
});
247281
}
248282

249283
/** Reuse one parsed catalog snapshot across every roster projection for this request. */
250284
async function createRequestScopedSubagentRosterResolver(): Promise<NonNullable<
251285
MultiAgentGuidanceDeps["resolveEffectiveSubagentRoster"]
252286
>> {
253-
const { effectiveSubagentRoster, readCatalog, readCodexCatalogPath } = await import("../../codex/catalog");
254-
const catalogEntries = readCatalog(readCodexCatalogPath())?.models ?? [];
287+
const { effectiveSubagentRoster } = await import("../../codex/catalog");
288+
const catalogEntries = await freshSubagentCatalogEntries();
255289
return (configuredModels, surface) =>
256290
effectiveSubagentRoster(configuredModels, surface, catalogEntries);
257291
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+
import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { resolveEffectiveSubagentRoster } from "../src/server/responses/collaboration";
6+
import {
7+
NATIVE_GPT56_CONTEXT_WINDOW,
8+
NATIVE_GPT56_OPT_IN_CONTEXT_WINDOW,
9+
} from "../src/codex/catalog";
10+
11+
/**
12+
* #2574: the subagent roster reads the persisted Codex catalog, which is only as fresh as the
13+
* last sync. A row written before the operator widened the native window kept its old
14+
* `context_window`, so a child planned and compacted against a narrower budget than its
15+
* parent — measured at 272,000 x 95% = 258,400 while the request path resolved 922,000.
16+
*/
17+
const originalHome = process.env.OPENCODEX_HOME;
18+
const originalCodexHome = process.env.CODEX_HOME;
19+
let home: string;
20+
let codexHome: string;
21+
22+
function writeStaleCatalog(): void {
23+
mkdirSync(codexHome, { recursive: true });
24+
writeFileSync(join(codexHome, "opencodex-catalog.json"), JSON.stringify({
25+
models: [
26+
{
27+
slug: "gpt-5.6-sol",
28+
// The width a pre-opt-in sync wrote.
29+
context_window: NATIVE_GPT56_CONTEXT_WINDOW,
30+
effective_context_window_percent: 95,
31+
visibility: "list",
32+
priority: 1,
33+
},
34+
],
35+
}));
36+
}
37+
38+
function writeConfig(): void {
39+
mkdirSync(home, { recursive: true });
40+
writeFileSync(join(home, "config.json"), JSON.stringify({
41+
port: 10100,
42+
defaultProvider: "openai",
43+
providers: { openai: { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" } },
44+
// The 1M opt-in is a raised provider cap, not a per-model window.
45+
providerContextCaps: { openai: 1_050_000 },
46+
}));
47+
}
48+
49+
beforeEach(() => {
50+
home = mkdtempSync(join(tmpdir(), "ocx-subagent-ctx-"));
51+
codexHome = mkdtempSync(join(tmpdir(), "ocx-subagent-codex-"));
52+
process.env.OPENCODEX_HOME = home;
53+
process.env.CODEX_HOME = codexHome;
54+
writeConfig();
55+
writeStaleCatalog();
56+
});
57+
58+
afterEach(() => {
59+
if (originalHome === undefined) delete process.env.OPENCODEX_HOME;
60+
else process.env.OPENCODEX_HOME = originalHome;
61+
if (originalCodexHome === undefined) delete process.env.CODEX_HOME;
62+
else process.env.CODEX_HOME = originalCodexHome;
63+
rmSync(home, { recursive: true, force: true });
64+
rmSync(codexHome, { recursive: true, force: true });
65+
});
66+
67+
describe("#2574 a subagent does not inherit a stale catalog width", () => {
68+
test("the roster still lists the model from a stale catalog", async () => {
69+
// The repair must not cost the caller its roster.
70+
const roster = await resolveEffectiveSubagentRoster(["gpt-5.6-sol"], "default");
71+
expect(roster.candidates.map(c => c.model)).toContain("gpt-5.6-sol");
72+
});
73+
74+
test("the arithmetic that produced the reported number is pinned", () => {
75+
// 272,000 x 95% = 258,400 is exactly what was observed; the opt-in width is 3.4x larger,
76+
// which is why the symptom reads as premature compaction rather than a rounding error.
77+
expect(Math.floor(NATIVE_GPT56_CONTEXT_WINDOW * 0.95)).toBe(258_400);
78+
expect(NATIVE_GPT56_OPT_IN_CONTEXT_WINDOW).toBe(922_000);
79+
expect(Math.floor(NATIVE_GPT56_OPT_IN_CONTEXT_WINDOW * 0.95)).toBe(875_900);
80+
});
81+
});
82+

0 commit comments

Comments
 (0)