Skip to content

fix(agent-sdk): preserve original message content in CommandRouter.handle()#1768

Open
mvanhorn wants to merge 1 commit intoxmtp:mainfrom
mvanhorn:fix/commandrouter-content-mutation
Open

fix(agent-sdk): preserve original message content in CommandRouter.handle()#1768
mvanhorn wants to merge 1 commit intoxmtp:mainfrom
mvanhorn:fix/commandrouter-content-mutation

Conversation

@mvanhorn
Copy link
Copy Markdown

@mvanhorn mvanhorn commented Mar 19, 2026

Summary

Fixes CommandRouter.handle() to restore the original message content after the command handler completes, preventing content mutation from leaking to downstream middleware.

Why this matters

In CommandRouter.ts:108-112, the comment says "Create a new context with modified content" but the code mutates ctx.message.content in-place instead:

// Create a new context with modified content (everything after the command)
const argsText = parts.slice(1).join(" ");
ctx.message.content = argsText;  // mutates the shared context
await entry.handler(ctx);
// original content is now permanently lost

This causes two problems:

  1. Middleware chaining: When CommandRouter is used as middleware via middleware() (L125-136), and the message isn't handled (!handled), next() is called - but by that point, ctx.message.content has already been truncated for any previously matched-then-failed path. More importantly, if a downstream middleware or listener runs after the router, it sees args instead of the full message.

  2. Error recovery: If the handler throws, the original content is permanently lost with no way to restore it.

Changes

Saves ctx.message.content before mutation and restores it in a finally block after the handler completes. Handlers still receive the parsed args as ctx.message.content (backward compatible), but the mutation doesn't persist after handle() returns.

Testing

  • yarn format:check passes
  • yarn typecheck passes
  • Existing CommandRouter.test.ts tests continue to pass (handlers still receive args correctly)

This contribution was developed with AI assistance (Claude Code). I have reviewed all changes and can discuss implementation decisions.

Note

Restore original message content in CommandRouter.handle() after handler completes

Saves ctx.message.content before replacing it with the arguments text, then restores it in a try/finally block after the command handler returns. This ensures the original full message content is always restored even if the handler throws.

Macroscope summarized 04f2c7d.

…ndle()

CommandRouter.handle() mutates ctx.message.content in-place (line 110),
replacing the full message with just the parsed arguments. The comment
on line 108 says "Create a new context with modified content" but the
code mutates the existing context instead.

This means downstream middleware in a chain sees truncated content,
and if the handler throws, the original content is permanently lost.

This fix saves the original content before mutation and restores it
in a finally block after the handler completes, preserving backward
compatibility (handlers still receive args as ctx.message.content)
while ensuring the mutation doesn't leak to other middleware.
@mvanhorn mvanhorn requested review from a team as code owners March 19, 2026 20:10
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 19, 2026

🦋 Changeset detected

Latest commit: 04f2c7d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@xmtp/agent-sdk Patch

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

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 19, 2026

@mvanhorn is attempting to deploy a commit to the XMTP Labs Team on Vercel.

A member of the Team first needs to authorize it.

@macroscopeapp
Copy link
Copy Markdown

macroscopeapp Bot commented Mar 19, 2026

Approvability

Verdict: Needs human review

This is a straightforward bug fix with clear intent and limited scope, but the author does not own either of the changed files (owned by @xmtp/documentation and @xmtp/protocol-sdk). The designated code owners should review these changes per the repository's CODEOWNERS policy.

You can customize Macroscope's approvability policy. Learn more.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant