fix(core): preserve jsonSchema adapter after Zod v4 tool schema injection - #21187
Conversation
…tion Re-wrap extended Zod v4 input schemas with toStandardSchema after CoreToolBuilder injects background/resume override fields. Without this, safeExtendZodObject can drop a custom ~standard.jsonSchema adapter and schema-invalid tool calls crash instead of returning validation errors. Fixes mastra-ai#21170
|
@Souravrajvi0 is attempting to deploy a commit to the Mastra Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: a5c5898 The changes in this PR will be included in the next version bump. This PR includes changesets to release 25 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 |
PR triageLinked issue check passed (#21170). 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 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
WalkthroughThis change re-wraps extended Zod v4 input schemas with ChangesZod v4 adapter preservation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/tools/tool-builder/background-override-injection.test.ts`:
- Around line 155-180: Extend the regression test around CoreToolBuilder to
invoke the built tool’s validation path with malformed _background data such as
enabled: 'yes', and assert it returns a structured validation error rather than
throwing a TypeError. If resumable-tool coverage is absent, add equivalent
invalid-input checks for suspendedToolRunId and resumeData.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: d84bc5b6-c221-4859-a0b7-17f48593ed29
📒 Files selected for processing (3)
.changeset/coretoolbuilder-zod-v4-jsonschema-adapter.mdpackages/core/src/tools/tool-builder/background-override-injection.test.tspackages/core/src/tools/tool-builder/builder.ts
There was a problem hiding this comment.
Approved. The one-line normalization at packages/core/src/tools/tool-builder/builder.ts:310 correctly restores a Mastra ~standard.jsonSchema adapter after Zod v4 .extend() injection while preserving the existing Zod validator. The regression test directly covers adapter retention and injected JSON Schema properties, and the existing suite covers valid execution plus resume/background field composition.\n\nVerification (credentials stripped):\n- pnpm build:core — passed (14/14 tasks)\n- focused Vitest background-override-injection.test.ts — passed (11/11)\n- git diff --check — passed\n- package check was attempted but is currently blocked by broad pre-existing workspace/type-resolution errors unrelated to this 3-file change; after the required build, the focused test reports no type errors.\n\nSecurity: no dependency, lockfile, workflow, prompt-processing, install-hook, or test-time execution changes. The patch only wraps an in-memory schema and adds a unit test/changeset.\n\nNon-blocking: CodeRabbit's suggestion to exercise malformed _background through the built tool would strengthen end-to-end regression coverage, but adapter presence plus the existing validation path is sufficient for this surgical fix. The two failing Vercel checks are authorization-gated previews for unrelated docs/playground targets.
|
Factory review handoff: APPROVE at head |
Exercise the built tool validation path so the adapter regression cannot return as an unstructured TypeError.\n\nCo-Authored-By: Mastra Code (openai/gpt-5.6-sol) <noreply@mastra.ai>
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
Description
CoreToolBuilderinjects_background/suspendedToolRunId/resumeDataonto Zod v4 tool input schemas viasafeExtendZodObject. That returns a freshZodObjectwhich can drop a custom~standard.jsonSchemaadapter installed bytoStandardSchema/createTool().When downstream code calls
standardSchemaToJSONSchemaon the mutated schema, invalid tool input can crash withCannot read properties of undefined (reading 'input')instead of returning a structured validation error.This mirrors the normalization already done in the non-Zod-v4 JSON Schema fallback path and in
validateToolInput(#19030), but applies it at the point where the schema is mutated.Related issue(s)
Fixes #21170
Type of change
Checklist
ELI5
When the tool adds extra input fields, it now keeps the schema’s validation helper. Invalid inputs return structured validation errors instead of causing a crash.
Changes
toStandardSchema.~standard.jsonSchemaadapters.@mastra/core.