Skip to content

Commit c0ad57a

Browse files
committed
merge: PR #705 follow-up — harden effort restoration boundaries
2 parents 438b8dc + 6d5bea1 commit c0ad57a

5 files changed

Lines changed: 92 additions & 13 deletions

File tree

‎src/config.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,18 +1024,27 @@ function rawClaudeSubagentEffort(rawParsed: unknown): unknown {
10241024
return (claudeCode as Record<string, unknown>).subagentEffort;
10251025
}
10261026

1027+
function normalizePersistedClaudeCode(claudeCode: unknown): OcxConfig["claudeCode"] {
1028+
if (!claudeCode || typeof claudeCode !== "object" || Array.isArray(claudeCode)) {
1029+
return claudeCode as OcxConfig["claudeCode"];
1030+
}
1031+
const normalized = { ...claudeCode } as Record<string, unknown>;
1032+
if (Object.hasOwn(normalized, "subagentEffort") && !isClaudeSubagentEffort(normalized.subagentEffort)) {
1033+
delete normalized.subagentEffort;
1034+
}
1035+
return normalized as OcxConfig["claudeCode"];
1036+
}
1037+
10271038
function normalizeClaudeSubagentEffort(config: OcxConfig, rawParsed: unknown): OcxConfig {
10281039
const rawEffort = rawClaudeSubagentEffort(rawParsed);
10291040
if (rawEffort === undefined || isClaudeSubagentEffort(rawEffort)) return config;
1030-
const normalized = { ...config, claudeCode: { ...config.claudeCode } };
1031-
delete normalized.claudeCode!.subagentEffort;
1032-
return normalized;
1041+
return { ...config, claudeCode: normalizePersistedClaudeCode(config.claudeCode) };
10331042
}
10341043

10351044
function warnDegradedClaudeSubagentEffort(rawParsed: unknown): void {
10361045
const rawEffort = rawClaudeSubagentEffort(rawParsed);
10371046
if (rawEffort !== undefined && !isClaudeSubagentEffort(rawEffort)) {
1038-
console.warn(`⚠️ config.json claudeCode.subagentEffort ${JSON.stringify(rawEffort)} is invalid (expected ${CLAUDE_SUBAGENT_EFFORTS.join(", ")}) — ignoring it. Other settings were preserved.`);
1047+
console.warn(`⚠️ config.json claudeCode.subagentEffort is invalid (expected ${CLAUDE_SUBAGENT_EFFORTS.join(", ")}) — ignoring it. Other settings were preserved.`);
10391048
}
10401049
}
10411050

@@ -1517,10 +1526,11 @@ export function saveConfigPreservingClaudeCode(config: OcxConfig): void {
15171526
if (claudeCodeBaseline.has(config)) {
15181527
if (onDisk !== undefined) {
15191528
const baseline = claudeCodeBaseline.get(config);
1520-
const diskChanged = !deepEqual(onDisk.claudeCode, baseline);
1529+
const persistedClaudeCode = normalizePersistedClaudeCode(onDisk.claudeCode);
1530+
const diskChanged = !deepEqual(persistedClaudeCode, baseline);
15211531
const weChanged = !deepEqual(config.claudeCode, baseline);
15221532
if (diskChanged && !weChanged) {
1523-
config.claudeCode = onDisk.claudeCode as OcxConfig["claudeCode"];
1533+
config.claudeCode = persistedClaudeCode;
15241534
}
15251535
}
15261536
}

‎src/server/claude-messages.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ export async function handleClaudeMessages(
523523
let anthropicBody: unknown;
524524
let internalBody: Rec;
525525
let cacheKeySource: ClaudeCacheKeySource = null;
526+
let effortOverride: ReturnType<typeof extractOcxEffortDirective> = null;
526527
try {
527528
anthropicBody = await readAnthropicBody(req);
528529
// Defensive [1m] strip (devlog 138): clients normally remove the context-variant
@@ -539,11 +540,7 @@ export async function handleClaudeMessages(
539540
const routeOverride = extractOcxRouteDirective(anthropicBody);
540541
if (routeOverride && typeof anthropicBody.model === "string") {
541542
anthropicBody.model = stripOneMillionMarker(routeOverride);
542-
const effortOverride = extractOcxEffortDirective(anthropicBody);
543-
if (effortOverride) {
544-
anthropicBody.output_config = { effort: effortOverride };
545-
delete anthropicBody.thinking;
546-
}
543+
effortOverride = extractOcxEffortDirective(anthropicBody);
547544
}
548545
}
549546
// Debug capture (opt-in allowlist scalars) BEFORE the passthrough branch so
@@ -572,6 +569,10 @@ export async function handleClaudeMessages(
572569
if (isRec(anthropicBody) && wantsNativePassthrough(req, config, anthropicBody.model)) {
573570
return await anthropicNativePassthrough(req, config, logCtx, logIds, anthropicBody, "/v1/messages");
574571
}
572+
if (isRec(anthropicBody) && effortOverride) {
573+
anthropicBody.output_config = { effort: effortOverride };
574+
delete anthropicBody.thinking;
575+
}
575576
const translation = anthropicToResponsesTranslation(anthropicBody, config.claudeCode);
576577
internalBody = translation.body;
577578
cacheKeySource = translation.cacheKeySource;

‎tests/claude-messages-endpoint.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,57 @@ test("non-streaming /v1/messages returns an Anthropic message JSON", async () =>
171171
}
172172
});
173173

174+
test("native generated-agent passthrough preserves legacy thinking", async () => {
175+
let captured: Record<string, unknown> | null = null;
176+
const upstream = Bun.serve({
177+
port: 0,
178+
async fetch(req) {
179+
captured = await req.json() as Record<string, unknown>;
180+
return Response.json({
181+
id: "msg_test",
182+
type: "message",
183+
role: "assistant",
184+
model: "claude-haiku-4-5",
185+
content: [{ type: "text", text: "ok" }],
186+
stop_reason: "end_turn",
187+
stop_sequence: null,
188+
usage: { input_tokens: 1, output_tokens: 1 },
189+
});
190+
},
191+
});
192+
const config = mockConfig("http://127.0.0.1:1/v1", {
193+
anthropicBaseUrl: upstream.url.toString().replace(/\/$/, ""),
194+
});
195+
saveConfig(config);
196+
const server = startServer(0);
197+
try {
198+
const response = await fetch(new URL("/v1/messages", server.url), {
199+
method: "POST",
200+
headers: { "content-type": "application/json", "x-api-key": "sk-ant-test" },
201+
body: JSON.stringify({
202+
model: "claude-haiku-4-5",
203+
max_tokens: 16,
204+
system: [
205+
{ type: "text", text: "<!-- ocx-route: claude-haiku-4-5 -->" },
206+
{ type: "text", text: "<!-- ocx-effort: max -->" },
207+
],
208+
thinking: { type: "enabled", budget_tokens: 31999 },
209+
messages: [{ role: "user", content: "hi" }],
210+
}),
211+
});
212+
expect(response.status).toBe(200);
213+
await response.text();
214+
expect(captured).toMatchObject({
215+
model: "claude-haiku-4-5",
216+
thinking: { type: "enabled", budget_tokens: 31999 },
217+
});
218+
expect(captured).not.toHaveProperty("output_config");
219+
} finally {
220+
server.stop(true);
221+
upstream.stop(true);
222+
}
223+
});
224+
174225
test("native Anthropic passthrough clears the header deadline before streaming the body", async () => {
175226
const encoder = new TextEncoder();
176227
const upstream = Bun.serve({

‎tests/config-user-edits.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ test("an unrelated save does not clobber the hand edit", () => {
7373
expect(diskConfig().disabledModels).toEqual(["test/one"]);
7474
});
7575

76+
test("an unrelated save does not resurrect an invalid persisted subagent effort", () => {
77+
writeDiskConfig({ claudeCode: { authMode: "subscription", subagentEffort: "ultra" } });
78+
const live = loadConfig();
79+
armClaudeCodeBaseline(live);
80+
81+
live.disabledModels = ["test/one"];
82+
saveConfigPreservingClaudeCode(live);
83+
84+
expect(live.claudeCode).toEqual({ authMode: "subscription" });
85+
expect(diskConfig().claudeCode).toEqual({ authMode: "subscription" });
86+
});
87+
7688
// R3-2: arming must be eager. A lazy "arm on first save" loses exactly this edit.
7789
test("an edit made before the first save still survives", () => {
7890
const live = loadConfig();

‎tests/config.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ describe("opencodex config defaults", () => {
162162
});
163163
});
164164

165-
test("an invalid persisted Claude Code subagent effort is ignored without wiping config", () => {
165+
test("an invalid persisted Claude Code subagent effort is ignored without wiping config or logging its value", () => {
166+
const invalidEffort = "credential-like-value";
167+
const warnSpy = spyOn(console, "warn").mockImplementation(() => {});
166168
writeConfig({
167169
port: 12345,
168170
defaultProvider: "custom",
169171
providers: { custom: { adapter: "openai-chat", baseUrl: "https://example.test/v1", apiKey: "upstream-secret" } },
170172
apiKeys: [{ id: "key-1", name: "default", key: "ocx_persisted", createdAt: "2026-07-28T00:00:00.000Z" }],
171-
claudeCode: { subagentEffort: "ultra" },
173+
claudeCode: { subagentEffort: invalidEffort },
172174
});
173175

174176
const config = loadConfig();
@@ -188,6 +190,9 @@ describe("opencodex config defaults", () => {
188190
});
189191
expect(diagnostics.config.claudeCode?.subagentEffort).toBeUndefined();
190192
expect(backupNames()).toEqual([]);
193+
expect(warnSpy).toHaveBeenCalled();
194+
expect(warnSpy.mock.calls.flat().join(" ")).not.toContain(invalidEffort);
195+
warnSpy.mockRestore();
191196
});
192197

193198
test("a blank hostname already on disk degrades without wiping providers or keys", () => {

0 commit comments

Comments
 (0)