fix(session): preserve Anthropic thinking for errored same-model replay (fixes #38620) - #45951
fix(session): preserve Anthropic thinking for errored same-model replay (fixes #38620)#45951optamus-ai wants to merge 1 commit into
Conversation
…ay (fixes anomalyco#38620) Errored assistant messages dropped all reasoning provider metadata (anthropic.signature / redactedData) while replaying tool_use, violating Anthropic invariant that tool_use requires preceding thinking/redacted_thinking when thinking is enabled. This caused 400 on follow-up request after interrupt/failure mid-step. Fix: relax reuseProviderMetadata guard only when message contains Anthropic thinking and sameModel holds. For same-model replay, keep hasAnthropicThinking check so signed/redacted thinking is retained alongside tool_use. Preserves deliberate guard for other providers (OpenAI test still expects drop) and does not silently discard valid provider state. Add regression test session-replay-38620.test.ts covering: - errored Anthropic thinking + tool_use preserved with sameModel - errored OpenAI still drops (existing behavior) - different model drops even for Anthropic Closes anomalyco#38620
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found several related PRs that address similar issues around Anthropic thinking preservation and session replay: Potential related PRs:
Why they may be related: These PRs all tackle similar problems in the session replay pipeline involving Anthropic's extended thinking feature, provider metadata preservation, and handling of tool_use parts alongside reasoning blocks during failed/errored turns. PR #44054 appears to be the most directly related, as it shares the exact same goal of preserving reasoning provider metadata on errored turns for valid Anthropic replay scenarios. |
Issue for this PR
Closes #38620
Type of change
What does this PR do?
Fixes V2 replay failure where errored assistant messages lose Anthropic thinking metadata while retaining tool_use, producing invalid follow-up requests when extended thinking is enabled.
Root cause:
packages/core/src/session/runner/to-llm-message.ts:73requiredmessage.error === undefinedforreuseProviderMetadata. An aborted/failed step therefore dropped all reasoning provider state (anthropic.signature/anthropic.redactedData) while still replayingtool_useparts. Anthropic requirestool_useto be preceded by itsthinking/redacted_thinkingblock when thinking is enabled → 400.Fix (minimal, preserves replay safety): Relax guard only when same-model and message contains Anthropic thinking. For
sameModel && (error === undefined || hasAnthropicThinking), reuse provider metadata so signed/redacted thinking is retained alongside tool_use. Preserves deliberate guard for other providers (existingsession-runner-message.test.tsexpects OpenAI drop) and does not silently discard valid provider state. Alternative of dropping tool calls would discard valid state.Preserved:
sameModelcheck,ToolOutputhandling,SessionCompactiontoken estimation, security/permission unchanged.How did you verify your code works?
Anthropicthinkingsig_test+tool_usewith same model → before fix, reasoning lost signature; after fix, both preserved andreasoningIdx < toolIdx.session-runner-message.test.tsstill passes (6 tests), includingdrops provider-native continuation metadata from failed assistant turnsfor OpenAI (still drops).session-replay-38620.test.tswith 3 cases: errored Anthropic same-model preserved, errored OpenAI still drops, different model drops even for Anthropic — all pass viabun test.git diff— 1 fileto-llm-message.ts12+1, 1 new test file131lines,Buffer.byteLengthnot needed here.git clone --depth 1,checkout -b,commit 4bd9bbc,pushtooptamus-ai/opencode,gh pr create— historydf35e84preserved.Screenshots / recordings
N/A — non-UI replay invariant.
Checklist