Skip to content

Commit e9a085d

Browse files
committed
fix(reasoning): hash the request-path credential as routed, not re-resolved
_apiKeyAttempt.reference is provenance for the routed apiKey, not a second source of truth: re-resolving it at record time performs a live env/keychain read, so a rotation between routing and recordUnsupportedReasoningEffort bound the learned refusal to the rotated credential and left the refused one unclamped. credentialIdentity now hashes provider.apiKey directly when a reference exists (the credential that served the request) and only resolves the configured expression on the catalog path, where apiKey is still unresolved.
1 parent 32473c5 commit e9a085d

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎src/providers/reasoning-metadata.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,16 @@ function modelLadderValue(
169169
* The hash input is the resolved wire credential, not the configured expression: the catalog
170170
* path carries the raw config string in apiKey (a keychain:/env reference stays unresolved
171171
* there), while the request path carries the resolved secret in apiKey and the configured
172-
* expression in _apiKeyAttempt.reference. Resolving the configured expression on both sides
173-
* hashes the same secret the upstream sees, so a refusal learned at request time also clamps
174-
* the catalog ladder for keychain/env users -- and a rotation behind a stable reference
175-
* starts clean instead of inheriting the previous credential's refusals.
172+
* expression in _apiKeyAttempt.reference. The request path hashes apiKey exactly as routed --
173+
* the reference is only provenance, and re-resolving it at record time could read a credential
174+
* rotated since the request was served. The catalog path resolves the configured expression,
175+
* so both sides still bind learned refusals to the same wire credential, and a rotation behind
176+
* a stable reference starts clean instead of inheriting the previous credential's refusals.
176177
*/
177178
function credentialIdentity(provider: OcxProviderConfig): string | undefined {
178-
const configured = provider._apiKeyAttempt?.reference ?? provider.apiKey;
179-
const resolved = resolveProviderApiKey(configured);
179+
const resolved = provider._apiKeyAttempt?.reference !== undefined
180+
? provider.apiKey
181+
: resolveProviderApiKey(provider.apiKey);
180182
if (typeof resolved !== "string" || resolved.length === 0) return undefined;
181183
return createHash("sha256").update(resolved).digest("hex");
182184
}

‎tests/codex-integration/reasoning-metadata.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,32 @@ describe("learned rung refusals", () => {
226226
}
227227
});
228228

229+
// The request path must hash the credential that served the request, not a live re-read of
230+
// the reference: a rotation between routing and refusal recording would otherwise bind the
231+
// learned refusal to the rotated credential and leave the refused one unclamped.
232+
test("a refusal learned at request time stays bound to the serving credential after rotation", async () => {
233+
const { effort, metadata } = await load(metadataFile({
234+
"opencode-go": { [DEEPSEEK_FLASH]: { reasoning: true, options: [{ type: "effort", values: ["low", "high", "max"] }] } },
235+
}));
236+
process.env["OCX_REASONING_METADATA_TEST_KEY"] = "serving-secret";
237+
const requestSide = {
238+
...ZEN_GO,
239+
apiKey: "serving-secret",
240+
_apiKeyAttempt: { reference: "${OCX_REASONING_METADATA_TEST_KEY}" },
241+
} as OcxProviderConfig;
242+
// Rotate behind the stable reference after routing but before the refusal is recorded.
243+
process.env["OCX_REASONING_METADATA_TEST_KEY"] = "rotated-secret";
244+
try {
245+
expect(metadata.recordUnsupportedReasoningEffort(requestSide, DEEPSEEK_FLASH, "max")).toBe(true);
246+
const served = { ...ZEN_GO, apiKey: "serving-secret" } as OcxProviderConfig;
247+
const rotated = { ...ZEN_GO, apiKey: "rotated-secret" } as OcxProviderConfig;
248+
expect(effort.configuredReasoningEfforts(served, DEEPSEEK_FLASH)).toEqual(["low", "high"]);
249+
expect(effort.configuredReasoningEfforts(rotated, DEEPSEEK_FLASH)).toEqual(["low", "high", "max"]);
250+
} finally {
251+
delete process.env["OCX_REASONING_METADATA_TEST_KEY"];
252+
}
253+
});
254+
229255
test("ignores legacy destination-wide support rows", async () => {
230256
const legacyRows = { ["opencode-go|" + DEEPSEEK_FLASH + "|max"]: { effort: "max", at: Date.now() } };
231257
const { effort } = await load({

0 commit comments

Comments
 (0)