Skip to content

fix(session): preserve Anthropic thinking for errored same-model replay (fixes #38620) - #45951

Open
optamus-ai wants to merge 1 commit into
anomalyco:devfrom
optamus-ai:optamus/fix-replay-38620
Open

fix(session): preserve Anthropic thinking for errored same-model replay (fixes #38620)#45951
optamus-ai wants to merge 1 commit into
anomalyco:devfrom
optamus-ai:optamus/fix-replay-38620

Conversation

@optamus-ai

Copy link
Copy Markdown

Issue for this PR

Closes #38620

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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:73 required message.error === undefined for reuseProviderMetadata. An aborted/failed step therefore dropped all reasoning provider state (anthropic.signature / anthropic.redactedData) while still replaying tool_use parts. Anthropic requires tool_use to be preceded by its thinking/redacted_thinking block 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 (existing session-runner-message.test.ts expects OpenAI drop) and does not silently discard valid provider state. Alternative of dropping tool calls would discard valid state.

Preserved: sameModel check, ToolOutput handling, SessionCompaction token estimation, security/permission unchanged.

How did you verify your code works?

  • Reproduced with deterministic test: errored Anthropic thinking sig_test + tool_use with same model → before fix, reasoning lost signature; after fix, both preserved and reasoningIdx < toolIdx.
  • Verified existing session-runner-message.test.ts still passes (6 tests), including drops provider-native continuation metadata from failed assistant turns for OpenAI (still drops).
  • Added session-replay-38620.test.ts with 3 cases: errored Anthropic same-model preserved, errored OpenAI still drops, different model drops even for Anthropic — all pass via bun test.
  • git diff — 1 file to-llm-message.ts 12+1, 1 new test file 131 lines, Buffer.byteLength not needed here.
  • Real git clone --depth 1, checkout -b, commit 4bd9bbc, push to optamus-ai/opencode, gh pr create — history df35e84 preserved.

Screenshots / recordings

N/A — non-UI replay invariant.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…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
@github-actions

Copy link
Copy Markdown
Contributor

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:

  1. PR fix(core): keep reasoning provider metadata on errored turns for valid Anthropic replay #44054 - fix(core): keep reasoning provider metadata on errored turns for valid Anthropic replay

  2. PR fix(core): drop tool parts when a failed turn loses its reasoning block #40157 - fix(core): drop tool parts when a failed turn loses its reasoning block

    • Relates to handling tool parts when reasoning blocks are lost during failed turns
  3. PR fix(core): replay thinking safely and drop unsettled tool calls from errored messages #42735 - fix(core): replay thinking safely and drop unsettled tool calls from errored messages

    • Addresses replaying thinking and managing tool calls in errored messages
  4. PR fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms #23755 - fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms

    • Related to preserving thinking/redacted_thinking blocks in Anthropic messages
  5. PR fix(opencode): drop truncated reasoning from replayed history #40148 - fix(opencode): drop truncated reasoning from replayed history

    • Addresses reasoning handling in replay scenarios

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V2: errored-message replay can 400 with Anthropic thinking + tool use

1 participant