Skip to content

fix(ai-sdk): only warn about requestContext conflict when route context has entries#17654

Open
Aayush-engineer wants to merge 2 commits into
mastra-ai:mainfrom
Aayush-engineer:fix/workflow-route-requestcontext-warning
Open

fix(ai-sdk): only warn about requestContext conflict when route context has entries#17654
Aayush-engineer wants to merge 2 commits into
mastra-ai:mainfrom
Aayush-engineer:fix/workflow-route-requestcontext-warning

Conversation

@Aayush-engineer

@Aayush-engineer Aayush-engineer commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Description

The requestContext warning in workflowRoute was firing on every request that included a requestContext in the body, even when there was no real conflict.

Root cause: the server middleware at server-adapter/index.ts always sets c.set('requestContext', c.env?.requestContext ?? new RequestContext()) — so contextRequestContext is never undefined, it's at least an empty RequestContext instance. This made the contextRequestContext && params.requestContext check always truthy when a body requestContext was present.

Fix: only warn when contextRequestContext actually has entries (size() > 0), meaning it was explicitly populated — not just defaulted to an empty instance by the middleware.

Related issue(s)

Fixes #17653

Type of change

  • Bug fix (non-breaking change that fixes an issue)

Checklist

  • I have linked the related issue(s) in the description above
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works
  • I have addressed all Coderabbit comments on this PR

ELI5

The system was warning that request info from the request body would be ignored even when there was no real conflict. The fix: only show that warning when the route's requestContext actually contains entries, not when it's just an empty default.

Summary

This PR fixes a spurious warning in workflowRoute that was emitted whenever a request body included requestContext, even if the route's requestContext was only the middleware-provided empty default.

  • Root cause: server middleware always sets c.set('requestContext', c.env?.requestContext ?? new RequestContext()), so contextRequestContext was never undefined and the previous check (contextRequestContext && params.requestContext) became truthy whenever the body included requestContext.
  • Fix: tighten the warning condition to require contextRequestContext.size() > 0 in addition to params.requestContext, so the warning is only logged when the route context's requestContext contains actual entries.
  • File changed: client-sdks/ai-sdk/src/workflow-route.ts — warning condition updated in the route handler.

Related Issue: fixes #17653
Type: Bug fix

@changeset-bot

changeset-bot Bot commented Jun 7, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d6c2fea

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel

vercel Bot commented Jun 7, 2026

Copy link
Copy Markdown

@Aayush-engineer is attempting to deploy a commit to the Mastra Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b4719922-64fb-4386-92de-ed3041d3395b

📥 Commits

Reviewing files that changed from the base of the PR and between 45fc600 and d6c2fea.

📒 Files selected for processing (1)
  • client-sdks/ai-sdk/src/workflow-route.ts
💤 Files with no reviewable changes (1)
  • client-sdks/ai-sdk/src/workflow-route.ts

Walkthrough

The workflow route handler's warning for ignored requestContext now requires contextRequestContext.size() > 0 in addition to existence before logging; other route behavior is unchanged.

Changes

Request Context Warning Refinement

Layer / File(s) Summary
Tighten warning condition check
client-sdks/ai-sdk/src/workflow-route.ts
The "requestContext from request body will be ignored" warning now emits only when contextRequestContext is non-undefined and contains entries (size() > 0), preventing spurious warnings when the option is present but empty.

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the specific fix: tightening the warning condition to only trigger when route context has entries, directly addressing the issue.
Linked Issues check ✅ Passed The code change directly implements the fix for issue #17653 by adding the size() > 0 check to prevent false-positive warnings when route context is empty.
Out of Scope Changes check ✅ Passed The change is narrowly scoped to fixing the warning condition in workflowRoute, directly addressing the linked issue with no extraneous modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dane-ai-mastra

dane-ai-mastra Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

PR triage

Linked issue check passed (#17653).

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

Factor Value Score impact
Files changed 1 +2
Lines changed 8 +0
Author merged PRs 14 -14
Test files changed No -0
Final score -12

Applied label: complexity: low


Changed test gate

Changed Test Gate is pending. The Changed Test Gate / changed-tests check will update the test label when it completes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
client-sdks/ai-sdk/src/workflow-route.ts (1)

242-254: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Critical: Incomplete fix - merge logic must also check size() to prevent silent data loss.

The warning condition at line 242 correctly checks contextRequestContext.size() > 0, but the merge logic at line 254 still uses contextRequestContext || params.requestContext. Since an empty RequestContext instance is truthy, line 254 will use the empty contextRequestContext instead of params.requestContext, silently ignoring the user-provided context without any warning.

This defeats the purpose of the fix. Per the PR objective, when contextRequestContext is empty (defaulted by middleware), it should be treated as "not set" and params.requestContext should be used.

🔧 Proposed fix to align merge logic with warning condition
-      if (contextRequestContext && params.requestContext && contextRequestContext.size() > 0) {
+      const hasRouteContext = contextRequestContext && contextRequestContext.size() > 0;
+      if (hasRouteContext && params.requestContext) {
         mastra
           .getLogger()
           ?.warn(
             `"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`,
           );
       }
       const handlerOptions = {
         mastra,
         workflowId: workflowToUse,
         params: {
           ...params,
-          requestContext: contextRequestContext || params.requestContext,
+          requestContext: hasRouteContext ? contextRequestContext : params.requestContext,
         },
         includeTextStreamParts,
         sendReasoning,
         sendSources,
       };
🤖 Prompt for 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.

In `@client-sdks/ai-sdk/src/workflow-route.ts` around lines 242 - 254, The merge
logic currently uses contextRequestContext || params.requestContext which will
pick an empty RequestContext instance (truthy) and discard
params.requestContext; change the assignment in
handlerOptions.params.requestContext to only use contextRequestContext when it
has entries by replacing the fallback with a size() check (e.g., use
contextRequestContext when contextRequestContext && contextRequestContext.size()
> 0, otherwise use params.requestContext) so it matches the earlier warning
condition in workflow-route.ts (mastra.getLogger().warn).
🤖 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.

Outside diff comments:
In `@client-sdks/ai-sdk/src/workflow-route.ts`:
- Around line 242-254: The merge logic currently uses contextRequestContext ||
params.requestContext which will pick an empty RequestContext instance (truthy)
and discard params.requestContext; change the assignment in
handlerOptions.params.requestContext to only use contextRequestContext when it
has entries by replacing the fallback with a size() check (e.g., use
contextRequestContext when contextRequestContext && contextRequestContext.size()
> 0, otherwise use params.requestContext) so it matches the earlier warning
condition in workflow-route.ts (mastra.getLogger().warn).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9e469823-fb11-4ebb-ae16-07ad22e5cf95

📥 Commits

Reviewing files that changed from the base of the PR and between 9d7188b and 45fc600.

📒 Files selected for processing (1)
  • client-sdks/ai-sdk/src/workflow-route.ts

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

Labels

complexity: low Low-complexity PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] requestContext warning always printed in workflowRoute when using requestContext in body

2 participants