diff --git a/src/lib/pool/failover-runner.mjs b/src/lib/pool/failover-runner.mjs index 76c29995a..b9a579336 100644 --- a/src/lib/pool/failover-runner.mjs +++ b/src/lib/pool/failover-runner.mjs @@ -1026,7 +1026,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 3652ebad6..aae954e06 100644 --- a/src/lib/protocol/handle-protocol.mjs +++ b/src/lib/protocol/handle-protocol.mjs @@ -1826,6 +1826,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 a8380c4a6..a0b7b31c6 100644 --- a/test/unit/failover-runner.test.mjs +++ b/test/unit/failover-runner.test.mjs @@ -2232,3 +2232,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) +})