Skip to content

fix: repair rejected Anthropic thinking replay - #92286

Merged
joshavant merged 5 commits into
mainfrom
fix/anthropic-thinking-signature-repair
Jun 12, 2026
Merged

joshavant merged 5 commits into
mainfrom
fix/anthropic-thinking-signature-repair

Conversation

@joshavant

Copy link
Copy Markdown
Collaborator

Summary

  • Repairs persisted signed-thinking replay state after Anthropic/Bedrock rejects a replayed thinking block signature and the stripped retry succeeds.
  • Keeps the existing current-turn retry behavior, but now removes the poisoned thinking blocks from the active session branch so subsequent turns do not fail on the same replay.
  • Uses the existing transcript rewrite/session update path rather than adding a provider-specific persistent fallback.
  • Intentionally out of scope: changing normal signed-thinking preservation when providers accept the replay.

Reviewers should focus on the recovery gate in thinking.ts, the active-branch rewrite helper, and the runner hook that refreshes in-memory session state after repair.

Linked context

Closes #91983

Related: maintainer-requested investigation and live Crabbox reproduction of invalid Anthropic thinking-signature replay.

Real behavior proof (required for external PRs)

  • Behavior or issue addressed: invalid persisted Anthropic signed-thinking replay could recover the current turn via stripped retry, but leave the bad thinking block in session history and poison later turns.
  • Real environment tested: AWS Crabbox Linux runner cbx_310989ad2e4d with live Anthropic claude-sonnet-4-6 API call.
  • Exact steps or command run after this patch: ran an ad hoc Crabbox script that captured a real signed-thinking assistant message, corrupted its thinkingSignature, reproduced provider rejection, then exercised wrapAnthropicStreamWithRecovery plus repairRejectedThinkingReplayInSessionManager and replayed the repaired transcript again.
  • Evidence after fix (screenshot, recording, terminal capture, console output, redacted runtime log, linked artifact, or copied live output):
LIVE_CAPTURE model=claude-sonnet-4-6 thinkingChars=109 signatureChars=420
REGRESSION_REPRO stopReason=error rejectedInvalidThinkingSignature=true
[agent/embedded] [session-recovery] Anthropic thinking stream error; retrying once without thinking blocks: sessionId=issue-91983-live
[agent/embedded] [session-recovery] stripped thinking blocks after provider rejected replay: repaired=1 sessionKey=unknown
FIX_PROOF stopReason=stop recoveryCalls=1 repairedCount=1 durableThinkingBlocks=0 nextTurnStopReason=stop
run details provider=aws lease=cbx_310989ad2e4d slug=blue-prawn run=run_ccdc1a7188ea type=c7a.8xlarge
  • Observed result after fix: the corrupted replay failed before repair, the wrapper retried successfully after stripping thinking blocks, one persisted session entry was repaired, no thinking blocks remained in the durable branch, and a subsequent live replay turn succeeded.
  • What was not tested: full CI matrix was not complete at PR creation time.
  • Proof limitations or environment constraints: live proof uses a short synthetic transcript to avoid exposing private user data; the Anthropic API key was injected through 1Password/Crabbox secret forwarding and not printed.
  • Before evidence (optional but encouraged): REGRESSION_REPRO stopReason=error rejectedInvalidThinkingSignature=true from the same live proof script.

Tests and validation

  • .agents/skills/autoreview/scripts/autoreview --mode local
    • Final result: autoreview clean: no accepted/actionable findings reported.
    • Earlier accepted findings fixed: propagated the recovery hook into request-local metadata; gated durable repair on non-error/non-aborted retry results.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/thinking.test.ts src/agents/embedded-agent-runner/thinking-replay-repair.test.ts src/agents/embedded-agent-runner/transcript-rewrite.test.ts --reporter=verbose
    • Passed: 51 tests.
  • Live Crabbox proof above.

Regression coverage added:

  • recovery notification fires only after successful stripped retry,
  • recovery notification does not fire on thrown retry failure,
  • recovery notification does not fire on stream-level retry error result,
  • transcript repair strips active-branch thinking blocks, preserves visible text and suffix entries, and skips no-op sessions.

Risk checklist

Did user-visible behavior change? (Yes/No): Yes

Did config, environment, or migration behavior change? (Yes/No): No

Did security, auth, secrets, network, or tool execution behavior change? (Yes/No): No

What is the highest-risk area?

Persisted session branch rewrite after a provider replay rejection.

How is that risk mitigated?

Repair only runs after the thinking-signature recovery path and only after the stripped retry returns a successful assistant result. It uses the existing transcript rewrite helper and preserves visible assistant content, falling back to the existing omitted-reasoning placeholder for thinking-only turns.

Current review state

Next action: watch CI and fix any failures related to this branch.

Still waiting on: CI.

Which bot or reviewer comments were addressed? None yet.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Jun 11, 2026
@clawsweeper

clawsweeper Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 12, 2026, 12:44 PM ET / 16:44 UTC.

Summary
The branch adds a successful-retry notification to Anthropic thinking recovery, durably strips thinking blocks from the active persisted session branch, refreshes the runner’s in-memory session state, and adds helper and integration regression tests.

PR surface: Source +181, Tests +459. Total +640 across 7 files.

Reproducibility: yes. The live Anthropic procedure captures a real signed-thinking response, corrupts its signature, reproduces provider rejection, exercises the patched durable repair, and verifies a later replay succeeds.

Review metrics: 1 noteworthy metric.

  • Persistent repair surface: 1 active-branch rewrite path added. The PR changes durable session history only after a provider-confirmed rejection and successful stripped retry, which is the decision maintainers must explicitly own.

Stored data model
Persistent data-model change detected: migration/backfill/repair: src/agents/embedded-agent-runner/run/attempt.ts, migration/backfill/repair: src/agents/embedded-agent-runner/thinking-replay-repair.test.ts, migration/backfill/repair: src/agents/embedded-agent-runner/thinking-replay-repair.ts, unknown-data-model-change: src/agents/embedded-agent-runner/thinking-replay-repair.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Risk before merge

  • [P1] A provider rejection identifies that the replayed thinking payload is invalid but not which persisted block caused it, so the repair removes all thinking and redacted-thinking blocks from the active branch, including historical blocks that may still be valid.
  • [P1] The persisted rewrite intentionally discards private reasoning history while preserving visible assistant content; this is the recovery tradeoff maintainers must accept rather than an uncertainty that tests or CI can eliminate.

Maintainer options:

  1. Accept the proven recovery tradeoff (recommended)
    Approve the active-branch cleanup because the provider cannot identify the rejected block and the live plus runner proof shows durable recovery while preserving visible history and fresh retry reasoning.
  2. Pause for precise rejected-block identity
    Hold the PR if retaining historical private reasoning is required until the provider or transcript contract can reliably identify only the rejected persisted block.

Next step before merge

  • [P2] No contributor-facing code repair remains; a session-state owner should explicitly accept or reject the intentional active-branch history cleanup before merge.

Security
Cleared: The diff adds no dependencies, workflow or package-resolution changes, secret access, permission expansion, downloaded code, or new external execution path.

Review details

Best possible solution:

Land the existing-path durable repair if session-state owners accept provider-confirmed active-branch reasoning removal as the canonical recovery policy, while retaining fresh reasoning generated by the successful retry and avoiding signature-size heuristics, runtime fallback readers, or a second persistence path.

Do we have a high-confidence way to reproduce the issue?

Yes. The live Anthropic procedure captures a real signed-thinking response, corrupts its signature, reproduces provider rejection, exercises the patched durable repair, and verifies a later replay succeeds.

Is this the best way to solve the issue?

Yes, subject to explicit policy acceptance. The PR extends the existing request-local recovery through the canonical transcript rewrite and runner refresh paths, and a narrower repair is not available from the current provider error because it does not identify the rejected block.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against f385491c230c.

Label changes

Label justifications:

  • P1: Invalid persisted thinking signatures can poison a session and make every subsequent Anthropic turn fail until its history is repaired.
  • merge-risk: 🚨 session-state: Merging intentionally rewrites persisted active-branch assistant history and removes private reasoning blocks after recovery.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body contains redacted after-fix live Anthropic output from a real Crabbox environment showing the initial rejection, successful durable repair, and a successful subsequent replay.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body contains redacted after-fix live Anthropic output from a real Crabbox environment showing the initial rejection, successful durable repair, and a successful subsequent replay.
Evidence reviewed

PR surface:

Source +181, Tests +459. Total +640 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 4 208 27 +181
Tests 3 460 1 +459
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 668 28 +640

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/agents/embedded-agent-runner/thinking.test.ts src/agents/embedded-agent-runner/thinking-replay-repair.test.ts src/agents/embedded-agent-runner/transcript-rewrite.test.ts --reporter=verbose.
  • [P1] Review the live Crabbox proof for run run_ccdc1a7188ea against the exact PR head.

What I checked:

  • Current-main gap: Current main retries a rejected Anthropic request with all thinking blocks removed from the request context, but the wrapper has no callback or persistent transcript update, so later turns can replay the same rejected state. (src/agents/embedded-agent-runner/thinking.ts:662, f385491c230c)
  • Recovery gate: The PR notifies durable repair only after the stripped retry resolves to a non-error, non-aborted assistant result, and catches repair-hook failure without converting a successful model response into a failed turn. (src/agents/embedded-agent-runner/thinking.ts:582, 40ccd86852d3)
  • Canonical persistence path: The repair collects changed active-branch messages, delegates persistence to the existing transcript rewrite helper, and emits the existing transcript-update event rather than adding a provider-specific store or fallback. (src/agents/embedded-agent-runner/thinking-replay-repair.ts:20, 40ccd86852d3)
  • Runner state synchronization: After a successful rewrite, the attempt runner refreshes the owned session snapshot and later rebuilds active messages, preventing the current process from continuing with the pre-repair branch while retaining newly generated retry reasoning. (src/agents/embedded-agent-runner/run/attempt.ts:2746, 40ccd86852d3)
  • Real behavior proof: The PR body records a live claude-sonnet-4-6 run that first reproduces invalid-signature rejection, then reports one durable repair, zero remaining thinking blocks, and a successful subsequent replay on Crabbox run run_ccdc1a7188ea. (40ccd86852d3)
  • Feature provenance: Blame and symbol history tie the existing Anthropic replay-recovery implementation to commit eebcb10, providing the main feature-history routing signal for the new persistent extension. (src/agents/embedded-agent-runner/thinking.ts:650, eebcb100b8)

Likely related people:

  • Marcus Castro: Git blame and -S history identify commit eebcb10 as the introduction of the current Anthropic thinking replay-recovery wrapper that this PR extends. (role: introduced behavior; confidence: high; commits: eebcb100b8; files: src/agents/embedded-agent-runner/thinking.ts)
  • Josh Avant: Current-main history shows recent work in the embedded attempt runner, and the PR’s integration follows that runner’s session refresh and context-engine boundaries. (role: recent adjacent contributor; confidence: medium; commits: 462092936a; files: src/agents/embedded-agent-runner/run/attempt.ts)
  • Vincent Koc: Shortlog history includes prior contributions to the central embedded-runner recovery/session files, making this a secondary routing candidate where ownership is shared. (role: adjacent area contributor; confidence: low; files: src/agents/embedded-agent-runner/run/attempt.ts, src/agents/embedded-agent-runner/thinking.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 11, 2026
@joshavant

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. and removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jun 12, 2026
@joshavant

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 12, 2026
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jun 12, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 13, 2026
* fix: repair rejected Anthropic thinking replay

* fix: narrow recovered retry result

* test: satisfy thinking recovery lint

* test: prove thinking retry preserves fresh reasoning

* test: type narrow thinking retry proof
wangmiao0668000666 pushed a commit to wangmiao0668000666/openclaw that referenced this pull request Jun 17, 2026
* fix: repair rejected Anthropic thinking replay

* fix: narrow recovered retry result

* test: satisfy thinking recovery lint

* test: prove thinking retry preserves fresh reasoning

* test: type narrow thinking retry proof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

1 participant