fix(core): preserve tool execution error state#17710
Conversation
Persist failed tool executions as output-error history parts while keeping legacy message conversion compatible.\n\nFixes mastra-ai#15569.
🦋 Changeset detectedLatest commit: b87ba02 The changes in this PR will be included in the next version bump. This PR includes changesets to release 20 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 |
|
@littlewwwhite 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 ignored due to path filters (1)
📒 Files selected for processing (8)
WalkthroughTool execution errors are now preserved correctly when conversation history is reloaded. Failed tool calls are stored as ChangesTool Execution Error Preservation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
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 (#15569). 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 |
Summary
output-errorhistory parts instead of successfulresultparts.Fixes #15569.
Testing
pnpm --filter ./packages/core exec vitest run src/loop/workflows/agentic-execution/llm-mapping-step.test.tspnpm --filter ./packages/core exec vitest run src/agent/message-list/conversion/output-converter-provider-executed.test.tspnpm --filter ./packages/core exec vitest run src/agent/message-list/prompt/convert-to-mastra-v1.test.tspnpm --filter ./packages/core exec vitest run src/agent/agent-network.test.ts --testNamePattern "should NOT store redundant toolCalls"pnpm --filter ./packages/core exec vitest run src/loop/loop.test.ts -upnpm --filter ./packages/core exec vitest run src/loop/loop.test.tspnpm --filter ./packages/core checkpnpm test:corepnpm --filter ./packages/core lintpnpm build:coregit diff --checkCodeRabbit CLI was not available locally (
coderabbitandcrwere not found).ELI5
When a tool breaks and throws an error, the system now correctly saves that failure to the conversation history instead of marking it as a successful result. When the conversation is reloaded later, the error stays properly marked as an error—with all the error details preserved—instead of being confusingly converted to a successful output.
Summary
This PR fixes a data persistence issue where tool execution errors were incorrectly stored and loaded from conversation history. Previously, when a tool's
execute()method threw an error, the error was persisted withstate: "result"and the error text in theresultfield. Upon reloading from history, this appeared as a successful tool execution with the error message in the output, losing the semantic meaning that the tool had failed.Changes Overview
Core Fix — llm-mapping-step.ts
Modified the tool error handling in
createLLMMappingStepto persist failed tool executions with the correct state and error field. When a tool throws, it now callsmessageList.updateToolInvocationwith:state: 'output-error'(instead of'result')errorText: reifiedError.message(instead of putting the error in aresultfield)Compatibility Layer — output-converter.ts
Extended
sanitizeAIV4UIMessagesto handleoutput-errortool invocations when converting to legacy AI SDK v4 format. IntroducedtoAIV4CompatibleToolInvocationhelper that mapsoutput-errorinvocations back tostate: 'result'with theerrorTextmoved to theresultfield, ensuring backward compatibility. Also updatedtoolInvocationsfiltering to preserve both successful results and errors.V1 Conversion — convert-to-mastra-v1.ts
Added two helper functions to support tool errors in Mastra v1 message format:
hasToolOutcome: Returns true for tool invocations withstate: 'result'(with a result field) orstate: 'output-error'toToolResultPart: Maps both successful and error tool invocations to v1 tool-result messages, convertingoutput-errorby usingerrorTextas the result contentThese helpers ensure that both successful and failed tool executions are properly included in tool-result messages during v1 conversion.
Tests
llm-mapping-step.test.tsverifying that tool errors are persisted withstate: 'output-error'anderrorTextoutput-converter-provider-executed.test.tsvalidating v4 sanitization convertsoutput-errorto legacyresultformatconvert-to-mastra-v1.test.tsconfirming v1 conversion handlesoutput-errortool invocationsagent-network.test.tsto accept bothtool-resultandtool-errorentries in final resultsChangeset
Added
@mastra/corepatch release note documenting that tool execution errors are now properly preserved in reloaded conversation history (closes#15569).