11# opencode-commands
22
3- An OpenCode V2 plugin for overriding and adding terminal slash commands. It currently ships one
4- command: a ` /clear ` that empties the current session in place instead of starting a new one.
3+ An OpenCode V2 plugin that overrides and adds terminal slash commands. It ships one: a ` /clear ` that
4+ empties the current session in place instead of starting a new one.
55
66Requires OpenCode V2 (verified against 2.0.4) and Bun.
77
88## What it does
99
10- - ** ` /clear ` clears the session you are in.** It deletes every message in the session and leaves you
11- in the same session: same session id, same title, same tab. You can keep prompting immediately.
12- - ** ` /new ` is untouched.** OpenCode ships ` /clear ` as an alias of its ` session.new ` command, so the
13- built-in ` /clear ` starts a fresh session and leaves the old transcript behind. This plugin takes
14- over that alias only. ` /new ` , its command-palette entry, and ` <leader>n ` all still run OpenCode's
15- own new-session code.
16- - ** Your files are left alone.** Clearing removes the conversation, not your work. Edits the agent
17- made to your working tree stay as they are. See the limitation below for the one case where
18- another client can still move files underneath it.
19-
20- Queued prompts and the session's instruction state are cleared along with the messages, because the
21- server drops them at the same boundary.
10+ - ** ` /clear ` clears the session you are in.** Every message is deleted and you stay put: same
11+ session id, title and tab, ready for the next prompt. Queued prompts and instruction state go with
12+ the messages; the server drops them at the same boundary.
13+ - ** Your files are left alone.** The agent's edits to your working tree survive, except in the
14+ shared-session case below.
15+ - ** ` /new ` is untouched.** OpenCode ships ` /clear ` as an alias of ` session.new ` ; the plugin takes
16+ over that alias only. ` /new ` , its command-palette entry, and ` <leader>n ` still run OpenCode's own
17+ new-session code.
2218
2319## Install
2420
2521``` sh
2622opencode plugin add @joonix/opencode-commands
2723```
2824
29- The CLI adds the plugin to your global OpenCode configuration. Append ` @<version> ` to pin a release.
30- The plugin takes no options. It registers ` joonix.commands ` on the server and ` joonix.commands.tui `
31- in the terminal, which is what a ` plugins ` disable selector such as ` -joonix.commands.tui ` or
32- ` -joonix.* ` matches.
25+ Append ` @<version> ` to pin a release. The plugin takes no options. It registers ` joonix.commands ` on
26+ the server and ` joonix.commands.tui ` in the terminal, the ids matched by a ` plugins ` disable
27+ selector such as ` -joonix.commands.tui ` or ` -joonix.* ` .
3328
34- Plugin list changes can take effect one service generation later: after editing the config, restart
35- the background service (` opencode service restart ` ) and expect the new set on the following start.
29+ Plugin list changes can take effect one service generation later: restart the background service
30+ (` opencode service restart ` ) and expect the new set on the following start.
3631
3732## Update
3833
@@ -41,51 +36,43 @@ opencode plugin check # report package plugins that have a newer release
4136opencode plugin update # update package plugins to their latest release
4237```
4338
44- ` update ` applies to package entries. A pinned ` @<version> ` entry stays pinned, and a directory entry
45- tracks the checkout rather than the registry, so neither is changed by it. The running service picks
46- up the new version on its own; restart it only if the change does not appear.
39+ Pinned ` @<version> ` entries and directory entries are left alone. The running service picks up the
40+ new version on its own; restart it only if the change does not appear.
4741
4842## How the override works
4943
5044OpenCode has no delete-message API. Committing a revert staged at the session's first message is the
51- supported equivalent: the server deletes every message and queued inbox item at or after that
52- boundary and resets instruction state, then the TUI prunes its own copy from the same event.
53-
54- Three details make that safe rather than merely working:
55-
56- - ** ` files: false ` when staging.** Otherwise the host restores file snapshots from the boundary and
57- clearing the transcript would revert the agent's edits along with it.
58- - ** A revert that is already staged gets committed first.** Staging restores a pending revert's files
59- from its original snapshot regardless of ` files: false ` , so staging on top of a pending ` /undo `
60- would discard anything edited since. Committing it keeps the working tree as it currently looks,
61- and the messages it excluded are ones the clear is about to delete anyway.
45+ supported equivalent: the server deletes every message and queued inbox item from that boundary on
46+ and resets instruction state, and the TUI prunes its copy from the same event. Four details make
47+ that safe:
48+
49+ - ** Staged with ` files: false ` .** That is what keeps the working tree out of it; otherwise the host
50+ restores file snapshots from the boundary and reverts the agent's edits with the transcript.
51+ - ** An already staged revert is committed first.** Staging restores a pending revert's files from its
52+ original snapshot regardless of ` files: false ` , so staging over a pending ` /undo ` would discard
53+ anything edited since. Committing leaves the working tree as it is and drops only messages the
54+ clear deletes anyway.
6255- ** The boundary comes from the server, not the TUI store.** That store holds only the newest page of
63- messages, so its first entry is not the session's first message. The plugin asks for the oldest
64- message explicitly; using the store would silently leave everything older in place.
65-
66- If the commit fails after staging succeeded, the plugin cancels the staged revert. A revert left
67- staged is committed automatically when the next prompt is submitted, which would delete the
68- transcript later without being asked again. It only cancels a revert that is still its own, so a
69- failure cannot discard work another client staged in the meantime.
56+ messages, so using it would silently leave everything older in place.
57+ - ** A failed commit cancels its own staged revert.** A revert left staged is committed on the next
58+ prompt, deleting the transcript unasked; only the plugin's own revert is cancelled.
7059
71- ## Limitation: a shared session
60+ Slash completion is keyed by command id, not by slash name, so a second command named ` clear ` would
61+ list two ` /clear ` entries. The plugin instead redeclares the host's ` session.new ` in a
62+ higher-priority layer without the ` clear ` alias and returns ` false ` , which rejects that entry so
63+ dispatch falls through to OpenCode's real handler and ` /new ` keeps working.
7264
73- A session can have several clients attached, and OpenCode has no atomic stage-and-commit for
74- reverts. Between staging and committing, another client can replace the staged revert. The plugin
75- checks that the staged revert is still the one it asked for and refuses to commit if it is not, so
76- it will not apply somebody else's boundary or clear only part of a transcript.
65+ ### Limitation: a shared session
7766
78- The one case it cannot defend against is a revert staged by another client in the moment between
79- this plugin reading the session and staging its own: OpenCode restores that revert's recorded files
80- as part of staging, regardless of ` files: false ` . This window is inherent to the revert API and
81- affects the built-in ` /undo ` the same way. In practice it requires two clients acting on one session
82- at the same instant.
67+ Several clients can attach to one session, and OpenCode has no atomic stage-and-commit for reverts.
68+ Between staging and committing, another client can replace the staged revert; the plugin verifies
69+ the staged revert is still its own and refuses to commit otherwise, so it never applies somebody
70+ else's boundary, clears part of a transcript, or discards work another client staged.
8371
84- Taking over the ` /clear ` name needs one extra step. Slash completion is keyed by command id rather
85- than by slash name, so registering a second command named ` clear ` would simply list two ` /clear `
86- entries. Instead the plugin redeclares the host's ` session.new ` in a higher-priority layer, omitting
87- the ` clear ` alias, and returns ` false ` from it. Returning ` false ` rejects that entry, so dispatch
88- continues down the chain to OpenCode's real handler and ` /new ` keeps working normally.
72+ The undefended window is a revert another client stages between the plugin reading the session and
73+ staging its own: OpenCode restores that revert's recorded files as part of staging, regardless of
74+ ` files: false ` . That window is inherent to the revert API, affects the built-in ` /undo ` equally, and
75+ needs two clients acting on one session at the same instant.
8976
9077## Develop
9178
@@ -94,10 +81,9 @@ make test # tsc --noEmit and bun test
9481make test-load # loads the plugin in a throwaway OpenCode config and asserts it loaded
9582```
9683
97- Entry resolution depends on how the plugin is configured. A package entry such as
98- ` @joonix/opencode-commands ` resolves through the ` exports ` map to ` src/index.ts ` and ` src/tui.tsx ` . A
99- directory entry, used when developing against a local checkout, resolves ` <dir> ` instead, so the
100- ` index.ts ` and ` tui.tsx ` files at the package root re-export ` src/ ` to keep that path working.
84+ ` make test ` covers the clear sequence against a fake client. The command wiring (which slash names
85+ appear, and that ` /new ` still falls through) is only exercised by running the TUI.
10186
102- ` make test ` covers the clear sequence against a fake client. The command wiring itself (which slash
103- names appear, and that ` /new ` still falls through) is only exercised by running the TUI.
87+ A package entry such as ` @joonix/opencode-commands ` resolves through the ` exports ` map to
88+ ` src/index.ts ` and ` src/tui.tsx ` ; a directory entry resolves ` <dir> ` , so the ` index.ts ` and
89+ ` tui.tsx ` files at the package root re-export ` src/ ` to keep that path working.
0 commit comments