Validate Anthropic extended-thinking request invariants (#253)#256
Merged
Conversation
commit: |
Add validateThinkingInvariants: when extended thinking is enabled, a tool-loop continuation assistant turn (a tool_use answered by the next turn's tool_result) must lead with a thinking/redacted_thinking block carrying a non-empty signature (non-empty data for redacted_thinking). Strict mode emits the real Anthropic 400 error shape; non-strict warns and replays. Guards untrusted JSON against null / non-object message and content-block entries. Emits a non-empty placeholder signature on every emitted thinking block (text, content+tool, tool-only shapes) so aimock's own record->replay round-trips stay green under strict mode; the streaming content_block_start signature stays empty per the real wire shape. Tool-only reasoning emission is capability-gated via resolveReasoningForModel, consistent with the other dispatch branches.
Unit tests for validateThinkingInvariants (scope, three violation kinds, multi-turn ordering, malformed-input guards), strict-on/off + header-override integration, round-trip tests across all response shapes, and capability-gating tests for the Claude tool-only reasoning path. Aligns the Anthropic drift contract: non-empty placeholder signature on the assembled block, empty signature on the streaming content_block_start.
589340e to
ec8424f
Compare
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #253. When Anthropic extended thinking is enabled, the real API
400s if a tool-loop continuation request omits the prior assistant turn's leadingthinkingblock (and itssignature). aimock previously accepted such requests and replayed the next fixture — a false green that masked realclaude-sdk-pythonbugs (dropped thinking blocks, last-only retention, un-replayedredacted_thinking).What changed
validateThinkingInvariants(messages.ts): when thinking is enabled, an in-scope tool-loop continuation turn (atool_useanswered by the next turn'stool_result) must (a) lead with athinking/redacted_thinkingblock, (b) carry a non-emptysignatureon a leadingthinkingblock, (c) carry non-emptydataon a leadingredacted_thinkingblock. Text-only/end_turnturns are exempt (no false 400s). Strict mode → real Anthropic400error shape; non-strict → warn + replay (existing suites stay green). Untrusted-JSON hardening: null/non-object message and content-block entries are guarded in both the validator andclaudeToCompletionRequest(no 500 on malformed input — the real API returns 400).signature_delta+ non-streaming assembled block) so aimock's own record→replay stays green under strict mode, while the streamingcontent_block_startsignature stays""per the real Anthropic wire shape. Applied consistently across all three response shapes — text, content+tool, and tool-only (ToolCallResponsegains an optionalreasoningfield).Test plan
validateThinkingInvariants(scope, three violation kinds, multi-turn ordering, malformed-input guards), strict-on/off +X-AIMock-Strictoverride integration, and round-trip tests proving aimock's emitted thinking turns (all shapes) replay cleanly under strict.pnpm test3377 passed;pnpm test:drift80 passed (Anthropic drift contract aligned); prettier + eslint +tsc --noEmit+ build all clean.Follow-ups (out of scope for this PR)
stream-collapse.ts) captures onlythinking_deltatext, not the upstreamsignature_deltavalue orredacted_thinkingdata — recording a real-Anthropic thinking turn loses signature/redacted data.message_startemits fulloutput_tokens;claudeStopReasonpasses unmapped finish reasons through; content+tool builder emits an emptytextblock whencontentis"".