fix(session): sanitize part IDs after plugin hooks#18788
Conversation
…validation crash Plugins that hook into chat.message can push new parts into the parts array with non-compliant IDs (missing the required prt prefix). This causes Session.updatePart() to throw a ZodError since v1.2.25 when branded PartID validation was introduced (anomalyco#16966). After Plugin.trigger(chat.message) returns, re-assign any invalid part IDs with PartID.ascending() so misbehaving plugins cannot crash the core. Fixes anomalyco#18780
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #18780
Type of change
What does this PR do?
Since v1.2.25 (#16966), all part IDs must start with
"prt"(enforced byPartID.zod=z.string().startsWith("prt")). ThecreateUserMessage()function inprompt.tscorrectly generates compliant IDs via theassign()function. However, it then callsPlugin.trigger("chat.message", ..., { message: info, parts }), passing thepartsarray by reference to plugin hooks.Plugins like
opencode-supermemorypush new parts into this array with non-compliant IDs (e.g.,supermemory-nudge-*,supermemory-context-*). WhenSession.updatePart()is called afterwards,fn(MessageV2.Part, ...)runsPartID.zod.parse()and throws a ZodError, crashing the session.This fix adds a sanitization loop after
Plugin.trigger("chat.message")returns: any part with a missing or non-"prt"prefixed ID gets reassigned a validPartID.ascending(). This protects the core against misbehaving plugins without breaking any existing behavior — parts that already have valid IDs are left untouched.How did you verify your code works?
fn()inutil/fn.tsthroughSession.updatePart()back tocreateUserMessage()opencode-supermemoryplugin injects parts withsupermemory-nudge-*andsupermemory-context-*IDs atdist/index.js:14548,14582Screenshots / recordings
Not a UI change.
Checklist