From cbedd242ff5d2d548447c1afd010ecc40b52dc9d Mon Sep 17 00:00:00 2001 From: panjinhe Date: Mon, 16 Nov 2026 10:08:59 -0800 Subject: [PATCH] fix(crag): strip and retry an Invalid signature 400 when the hop replayed foreign thinking The CRAG branch rebuilds from the unfiltered caller body, so thinking signed outside this pool reached the provider and failed every turn. When the hop carried thinking this pool never issued, grade the signature 400 repairable even with failover.signature_repair off; forged probes on otherwise-own history still get the 400. Refs dofastted/vm2api#1145 --- src/lib/pool/failover-runner.mjs | 2 +- src/lib/protocol/handle-protocol.mjs | 3 ++ test/unit/failover-runner.test.mjs | 67 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/lib/pool/failover-runner.mjs b/src/lib/pool/failover-runner.mjs index c4851fed3..8bec0929e 100644 --- a/src/lib/pool/failover-runner.mjs +++ b/src/lib/pool/failover-runner.mjs @@ -982,7 +982,7 @@ export class FailoverRunner { model, repaired, oauth401CooldownMs: this.config.oauth_401_cooldown_ms, - signatureRepair: signatureRepairEnabled(this.config), + signatureRepair: signatureRepairEnabled(this.config) || attemptMeta?.foreignThinking === true, }, this.scheduler?.accountQuota, ) diff --git a/src/lib/protocol/handle-protocol.mjs b/src/lib/protocol/handle-protocol.mjs index c6361a285..785efaebe 100644 --- a/src/lib/protocol/handle-protocol.mjs +++ b/src/lib/protocol/handle-protocol.mjs @@ -1790,6 +1790,9 @@ export function createHandleProtocol(deps) { sessionId: attemptSessionId, cliHop: true, cragPrint: true, + // CRAG replays caller thinking unfiltered; signatures this pool never + // issued make an "Invalid signature" 400 safe to strip and retry. + foreignThinking: owned.converted + owned.dropped > 0, callerCwd: callerEnvironment?.cwd, preparedHop, transportRouting, diff --git a/test/unit/failover-runner.test.mjs b/test/unit/failover-runner.test.mjs index 1867fafef..f4bcc4e5a 100644 --- a/test/unit/failover-runner.test.mjs +++ b/test/unit/failover-runner.test.mjs @@ -2122,3 +2122,70 @@ test('a slot that cannot refresh its credential hops and cools down', async () = assert.equal(scheduler.cooldowns[0].update.reason, 'credential_refresh_failed') assert.equal(scheduler.cooldowns[0].update.status, 'cooldown') }) + +test('signature 400 on a hop that replayed foreign thinking is stripped and retried with the switch off', async () => { + const scheduler = new Scheduler([candidate(1)]) + const runner = new FailoverRunner({ scheduler }) + const seen = [] + const result = await runner.run({ + requestId: 'req-foreign-sig', + canonicalBody: { + model: 'claude-opus-test', + thinking: { type: 'adaptive' }, + messages: [ + { role: 'user', content: 'q' }, + { + role: 'assistant', + content: [ + { type: 'thinking', thinking: '', signature: 'other-pool-signature' }, + { type: 'text', text: 'keep' }, + ], + }, + { role: 'user', content: 'next' }, + ], + }, + model: 'claude-opus-test', + applyAttempt: (body, _selected, extra) => { + seen.push(extra.repairs) + return { body, meta: { foreignThinking: true } } + }, + callAttempt: ({ body }) => { + if (seen.length === 1) { + return { + ok: false, + status: 400, + terminalState: 'error', + body: { error: { message: 'messages.1.content.0: Invalid `signature` in `thinking` block' } }, + } + } + assert.deepEqual(body.messages[1].content, [{ type: 'text', text: 'keep' }]) + return success('stripped') + }, + }) + assert.equal(result.ok, true) + assert.equal(seen.length, 2) + assert.deepEqual(seen[1], [{ action: 'repair-and-retry', reason: 'signature_repairable' }]) +}) + +test('signature 400 without foreign thinking still surfaces with the switch off', async () => { + const scheduler = new Scheduler([candidate(1)]) + const runner = new FailoverRunner({ scheduler }) + let calls = 0 + const result = await runner.run({ + requestId: 'req-own-sig', + canonicalBody: { model: 'claude-opus-test', messages: [{ role: 'user', content: 'q' }] }, + model: 'claude-opus-test', + applyAttempt: (body) => ({ body, meta: { foreignThinking: false } }), + callAttempt: () => { + calls++ + return { + ok: false, + status: 400, + terminalState: 'error', + body: { error: { message: 'Invalid `signature` in `thinking` block' } }, + } + }, + }) + assert.equal(result.status, 400) + assert.equal(calls, 1) +})