fix(opencode): intercept annotate/review/archive commands before LLM#718
Merged
Conversation
…713) OpenCode's command dispatcher appends `arguments` to the `.md` body and runs `resolvePromptParts()` over the combined string, which auto-attaches any file path it finds as a `FilePart`. With `/plannotator-annotate /path/to/huge.md`, that meant the agent received the file's content as a user message before the annotation UI even opened — blowing the context on large files (GLM-5 auto-compact reported in #713). Move `plannotator-annotate`, `plannotator-review`, and `plannotator-archive` from the post-hoc `event` handler to `command.execute.before`, matching the pattern `plannotator-last` already used. The hook clears `output.parts` in place so the agent never receives the command turn; handlers then run the UI and inject feedback via `client.session.prompt` as a separate turn. Empty the bodies of the three `.md` files for defense in depth — only the frontmatter is needed for OpenCode to register the slash command. Also fixes a latent bug in the `plannotator-last` path: `output.parts = []` reassigns the throwaway wrapper object's property but doesn't touch the `parts` array the caller in `prompt.ts:1944` uses directly. Switched to `output.parts.length = 0` to mutate in place. `plannotator-last` only escaped notice because its parts array was always a single benign text part.
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
/plannotator-annotate,/plannotator-review,/plannotator-archivefrom the post-hoceventhandler intocommand.execute.before— same patternplannotator-lastalready used — so the agent never sees the command body or appended args..mdfiles; only the frontmatter is needed for OpenCode to register the slash command.output.parts = []→output.parts.length = 0: OpenCode's caller (prompt.ts:1944-1979) holds a direct reference to the parts array and ignores any new array assigned to the hook'soutput.parts, so the previous reassignment was a no-op.Root cause
When the user runs
/plannotator-annotate /path/to/huge.md, OpenCode appends the args to the.mdbody and runsresolvePromptParts()on the combined string. That function (prompt.ts) walks file references and pushes{ type: "file", url, filename, mime }parts. The agent then receives the file as aFilePartin a user message — before the annotation UI even exists. With a large file plus GLM-5, that blows the context and triggers auto-compact.plannotator-lastalready usedcommand.execute.beforeto intercept the prompt before it reached the LLM. The other three commands relied on theeventhandler, which fires aftercommand.executedis published — i.e. after the agent has already processed the turn.Test plan
bun test apps/opencode-plugin/— 37 passbun build apps/opencode-plugin/index.ts— clean/plannotator-annotate <large markdown file>in OpenCode no longer triggers auto-compact; annotation UI opens immediately; feedback is delivered after submit./plannotator-review,/plannotator-archive,/plannotator-laststill behave as before.Fixes #713