fix(core): preserve Anthropic signed thinking across memory replay#17602
fix(core): preserve Anthropic signed thinking across memory replay#17602Akash504-ai wants to merge 2 commits into
Conversation
🦋 Changeset detectedLatest commit: a0c1b3e The changes in this PR will be included in the next version bump. This PR includes changesets to release 23 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@Akash504-ai is attempting to deploy a commit to the Mastra Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
WalkthroughThis PR fixes Anthropic "signed thinking" round-trip behavior in multi-turn conversations backed by Memory. When persisted assistant reasoning blocks are replayed, the thinking text and signature now both restore correctly, and invalid legacy blocks (signature present but empty text) are sanitized before forwarding to Anthropic. ChangesAnthropic signed thinking replay and sanitization
🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR triageLinked issue check passed (#17457). Mastra uses CodeRabbit for automated code reviews. Please address all feedback from CodeRabbit by either making changes to your PR or leaving a comment explaining why you disagree with the feedback. Since CodeRabbit is an AI, it may occasionally provide incorrect feedback. PR complexity score
Applied label: Changed test gateChanged Test Gate is pending. The |
Description
Fixes an issue where Anthropic signed thinking blocks could lose their reasoning text during persistence/replay while retaining the original signature.
When replayed in a multi-turn conversation, Anthropic validates the signature against the original thinking content and rejects modified blocks with:
This change:
reasoningordetailsfor backward compatibilityRelated issue(s)
Fixes #17457
Type of change
Checklist
ELI5
Imagine you're having a multi-turn conversation with an AI that thinks out loud (shows its reasoning). When you save that conversation and continue it later, the AI's reasoning was being saved with a special seal (signature) but without the actual thoughts. When you tried to replay that conversation, the AI would reject it because the seal didn't match the empty reasoning—like opening a sealed envelope that's supposed to have a letter inside, but finding it empty. This PR fixes it so the thinking and the seal travel together correctly, and also cleans up old conversations that already have this problem.
Changes Overview
This PR addresses issue
#17457where Anthropic signed thinking blocks lost their reasoning text during persistence/replay cycles while retaining their signatures, causing validation failures. The fix ensures that thinking blocks round-trip correctly through memory and handles legacy data that may already be in a broken state.Core Fix: Preserving Thinking Text
In
AIV5Adapter.ts:reasoningfield ordetailsfragmentsfromUIMessageandfromModelMessagemethods to preserve thinking text in thereasoningfield when an Anthropic signature is present (previously always stored empty strings)toUIMessageto use the new extraction logic for computing text contentIn
build-messages-from-chunks.ts:reasoningfield with aggregated text fromdetailswhen an Anthropic signature is presentBackwards Compatibility & Defensive Sanitization
In
provider-history-compat.ts:anthropicStripEmptySignedReasoningContentthat detects legacy assistant reasoning blocks where the Anthropic signature exists but the text is emptyDEFAULT_COMPAT_RULESfor automatic applicationTesting
New test coverage added:
anthropic-thinking-roundtrip.test.ts: Comprehensive test suite validating signed thinking round-trip through adapters andProviderHistoryCompatbuild-messages-from-chunks.test.ts: New test asserting Anthropic signed reasoning text is preserved in the primaryreasoningfieldChangeset
.changeset/signed-thinking-memory.mddocumenting the patch release for@mastra/core