fix(agent-sdk): preserve original message content in CommandRouter.handle()#1768
Open
fix(agent-sdk): preserve original message content in CommandRouter.handle()#1768
Conversation
…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.
🦋 Changeset detectedLatest commit: 04f2c7d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
@mvanhorn is attempting to deploy a commit to the XMTP Labs Team on Vercel. A member of the Team first needs to authorize it. |
ApprovabilityVerdict: 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 mutatesctx.message.contentin-place instead:This causes two problems:
Middleware chaining: When
CommandRouteris used as middleware viamiddleware()(L125-136), and the message isn't handled (!handled),next()is called - but by that point,ctx.message.contenthas 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.Error recovery: If the handler throws, the original content is permanently lost with no way to restore it.
Changes
Saves
ctx.message.contentbefore mutation and restores it in afinallyblock after the handler completes. Handlers still receive the parsed args asctx.message.content(backward compatible), but the mutation doesn't persist afterhandle()returns.Testing
yarn format:checkpassesyarn typecheckpassesCommandRouter.test.tstests 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 completesSaves
ctx.message.contentbefore replacing it with the arguments text, then restores it in atry/finallyblock after the command handler returns. This ensures the original full message content is always restored even if the handler throws.Macroscope summarized 04f2c7d.