opencode-subagent-tools 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,238 @@
1
+ # Configuration
2
+
3
+ Pass options with the object form of a `plugins` entry in `opencode.json(c)`. Options belong to the entry that loads the plugin and are read from `ctx.options` during `setup`. If you installed with `opencode plugin add`, replace the string entry with the object form to add options.
4
+
5
+ A plugin auto-discovered from a `plugins/` directory has no entry, so it always uses defaults. To pass options, load it through `opencode.json(c)`.
6
+
7
+ Local paths in `plugins` must be directories. A path to a single `.ts` file is skipped. Point `package` at the directory that contains `package.json` with `name` and `exports`.
8
+
9
+ ## Full example
10
+
11
+ The example shows the defaults and uses the npm package name. You can omit any option you do not need. For a local checkout, use the directory path instead; see [Global vs project configuration](#global-vs-project-configuration).
12
+
13
+ ```jsonc
14
+ {
15
+ "$schema": "https://opencode.ai/config.json",
16
+ "plugins": [
17
+ {
18
+ "package": "opencode-subagent-tools",
19
+ "options": {
20
+ "namespace": "subagent",
21
+ "enforcePermissions": true,
22
+ "sessionControlPolicy": "descendants",
23
+ "allowedOutputFormats": ["text", "json"],
24
+ "registerSkill": true,
25
+ "limits": {
26
+ "maxDiscoveryNodes": 200,
27
+ "maxDiscoveryDepth": 8,
28
+ "maxDiscoveryPages": 5,
29
+ "maxListRows": 20,
30
+ "maxCancelBatch": 20,
31
+ "maxTextLength": 16000,
32
+ },
33
+ },
34
+ },
35
+ ],
36
+ }
37
+ ```
38
+
39
+ ## Options
40
+
41
+ | Option | Type | Default | Effect |
42
+ | ---------------------- | -------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
43
+ | `namespace` | string | `"subagent"` | Tool namespace. Tools become `<namespace>_list`, `<namespace>_usage`, `<namespace>_cancel`, `<namespace>_cancel_descendants`, `<namespace>_steer`, `<namespace>_queue`, and `<namespace>_context`. |
44
+ | `enforcePermissions` | boolean | `true` | Checks every mutation with `ctx.permission.assert` using action `subagent.control`. A denial returns a tool error. Read-only `list` and `usage` do not request `subagent.control`. Set to `false` to skip permission evaluation entirely; ownership still follows `sessionControlPolicy`. |
45
+ | `sessionControlPolicy` | string | `"descendants"` | Who **mutating** tools may target: `"descendants"` (caller subtree), `"all-except-ancestors"` (any except caller/ancestors), or `"all"` (any session, including the caller's ancestors, except the caller itself). Read-only `list`/`usage` are not restricted. `cancel_descendants` always stays in the caller's subtree. |
46
+ | `allowedOutputFormats` | string[] | `["text", "json"]` | Formats the tools may return. `["text"]` returns text only; `["json"]` always returns structured output. When both are allowed, tools also accept a per-call `format`; with one format the input is omitted. Unknown values are ignored; a non-array, an empty list, or a list with no recognized values falls back to the default, while a partially valid list keeps the recognized values. |
47
+ | `registerSkill` | boolean | `true` | Registers the bundled `SKILL.md` as a skill for the model. Set to `false` to skip skill registration; the tools still work. |
48
+
49
+ The permission action is always `subagent.control`, even if you change `namespace`. See [Permissions](security-model.md#permissions).
50
+
51
+ ## Session control policy
52
+
53
+ `sessionControlPolicy` applies only to **mutating** tools. Read-only `list` and `usage` may read any session, including the caller and its ancestors.
54
+
55
+ | Tool | Mutating | Uses `subagent.control` | Policy applies |
56
+ | -------------------- | -------- | ----------------------- | ------------------------- |
57
+ | `list` | no | no | no; any `parentID` |
58
+ | `usage` | no | no | no; any session |
59
+ | `cancel` | yes | yes | yes |
60
+ | `cancel_descendants` | yes | yes | no; always caller subtree |
61
+ | `steer` / `queue` | yes | yes | yes |
62
+ | `context` | yes | yes | yes |
63
+
64
+ | Value | Mutations may target |
65
+ | ------------------------- | ------------------------------------------------------------------------ |
66
+ | `"descendants"` (default) | Only the calling session's subtree. |
67
+ | `"all-except-ancestors"` | Any session except the caller and its ancestors. |
68
+ | `"all"` | Any session, including the caller's ancestors, except the caller itself. |
69
+
70
+ Cross-tree control without allowing upward control:
71
+
72
+ ```jsonc
73
+ {
74
+ "plugins": [
75
+ {
76
+ "package": "opencode-subagent-tools",
77
+ "options": { "sessionControlPolicy": "all-except-ancestors" },
78
+ },
79
+ ],
80
+ }
81
+ ```
82
+
83
+ Full control, including ancestors:
84
+
85
+ ```jsonc
86
+ {
87
+ "plugins": [
88
+ {
89
+ "package": "opencode-subagent-tools",
90
+ "options": { "sessionControlPolicy": "all" },
91
+ },
92
+ ],
93
+ }
94
+ ```
95
+
96
+ `"all"` is security-sensitive: a subagent can target its parent or root if it knows the ID. Read-only tools are unrestricted in every mode and do not request `subagent.control`. `cancel_descendants` never widens beyond the caller's subtree.
97
+
98
+ ## Global vs project configuration
99
+
100
+ Options are scoped to the config file that contains the `plugins` entry:
101
+
102
+ - **Global** — `~/.config/opencode/opencode.json(c)`: applies to every project.
103
+ - **Project** — `./opencode.json(c)` or `./.opencode/opencode.json(c)`: applies to that project only.
104
+ - **System** — `/etc/opencode/opencode.json(c)` on Linux, `/Library/Application Support/opencode` on macOS, or `%ProgramData%\opencode` on Windows: managed settings with the highest precedence.
105
+
106
+ ### Global example
107
+
108
+ ```jsonc
109
+ // ~/.config/opencode/opencode.jsonc
110
+ {
111
+ "$schema": "https://opencode.ai/config.json",
112
+ "plugins": [
113
+ {
114
+ "package": "opencode-subagent-tools",
115
+ "options": {
116
+ "namespace": "subagent",
117
+ "enforcePermissions": true,
118
+ "sessionControlPolicy": "descendants",
119
+ },
120
+ },
121
+ ],
122
+ }
123
+ ```
124
+
125
+ For a local checkout, use the absolute path to the plugin directory instead of the npm name:
126
+
127
+ ```jsonc
128
+ {
129
+ "plugins": [
130
+ {
131
+ "package": "C:/Users/<you>/opencode-subagent-tools",
132
+ "options": { "sessionControlPolicy": "all-except-ancestors" },
133
+ },
134
+ ],
135
+ }
136
+ ```
137
+
138
+ ### Project example
139
+
140
+ From `./opencode.jsonc`:
141
+
142
+ ```jsonc
143
+ {
144
+ "$schema": "https://opencode.ai/config.json",
145
+ "plugins": [
146
+ {
147
+ "package": "./opencode-subagent-tools",
148
+ "options": {
149
+ "namespace": "subagent",
150
+ "enforcePermissions": true,
151
+ },
152
+ },
153
+ ],
154
+ }
155
+ ```
156
+
157
+ From `./.opencode/opencode.jsonc`, the path is relative to that file, so use one extra `../`:
158
+
159
+ ```jsonc
160
+ {
161
+ "$schema": "https://opencode.ai/config.json",
162
+ "plugins": [
163
+ {
164
+ "package": "../opencode-subagent-tools",
165
+ "options": {
166
+ "namespace": "subagent",
167
+ "enforcePermissions": true,
168
+ },
169
+ },
170
+ ],
171
+ }
172
+ ```
173
+
174
+ The package path is a directory containing `package.json` and `exports`, not a `.ts` file.
175
+
176
+ ### Choosing a scope
177
+
178
+ Use a global entry when the same options should apply everywhere. Use a project entry when only that project needs the option.
179
+
180
+ Plugin arrays from applicable config files are merged from lowest to highest precedence; they are not replaced. OpenCode rejects duplicate plugin IDs: a second entry is reported as failed with `Duplicate plugin ID` and does not load. Load the plugin in exactly one scope. If you need different options per project, load it only from the project config.
181
+
182
+ ### Auto-discovered directory installs
183
+
184
+ Copying the package directory (with `package.json`, `src/`, and `SKILL.md`) into a `plugins/` directory is still supported, but that path is auto-discovered and has no `plugins` entry. It always uses default options. To pass options, use a configured directory package and a `plugins` entry as above.
185
+
186
+ ## Limits
187
+
188
+ All limits are integers and are clamped to the allowed range. Invalid values fall back to the default.
189
+
190
+ | Option | Default | Range | Effect |
191
+ | ------------------- | ------- | ------- | -------------------------------------------------------------------- |
192
+ | `maxDiscoveryNodes` | `200` | 1–5000 | Maximum nodes visited in one server discovery pass. |
193
+ | `maxDiscoveryDepth` | `8` | 1–32 | Maximum parent-chain depth walked during discovery and verification. |
194
+ | `maxDiscoveryPages` | `5` | 1–50 | Maximum `session.list` pages requested per parent. |
195
+ | `maxListRows` | `20` | 1–50 | Maximum rows returned by `list` per call. |
196
+ | `maxCancelBatch` | `20` | 1–50 | Maximum children interrupted by `cancel_descendants` per call. |
197
+ | `maxTextLength` | `16000` | 1–32000 | Maximum characters accepted by `steer`, `queue`, and `context`. |
198
+
199
+ `list` and `cancel_descendants` are independent: raising `maxListRows` does not change how many children `cancel_descendants` may stop, and vice versa. The `session.list` page size stays at 100.
200
+
201
+ Discovery follows the server cursor:
202
+
203
+ - A short page with a cursor continues.
204
+ - A page without a cursor ends the walk.
205
+ - A full page without a cursor is treated as potentially truncated.
206
+ - A repeated cursor is treated as incomplete.
207
+
208
+ ## Tuning notes
209
+
210
+ - Raising `maxDiscoveryNodes`, `maxDiscoveryDepth`, or `maxDiscoveryPages` increases the number of `session.list` calls and discovery latency.
211
+ - Raising `maxListRows` increases the number of rows in `list` output. Raising `maxCancelBatch` increases the number of interrupts and status checks per `cancel_descendants` call. `cancel_descendants` checks the whole batch in one permission request, so an `ask` policy prompts once per call, not once per child.
212
+ - `list` and `cancel_descendants` use a cursor to page. A cursor keeps its place while statuses change, but it sees the sessions found when the walk started; children launched after the first page may not appear.
213
+ - `list` filters by the last observed status. Include `unknown` in `statuses` if you also want to see sessions whose state is not known yet.
214
+ - Discovery stops at the configured node, depth, and page limits and can miss sessions. An empty or partial result is not proof that no subagents exist; see [limitations](limitations.md).
215
+ - `sessionControlPolicy: "all"` is security-sensitive; see the [security model](security-model.md).
216
+
217
+ ## Power-user example
218
+
219
+ ```jsonc
220
+ {
221
+ "$schema": "https://opencode.ai/config.json",
222
+ "plugins": [
223
+ {
224
+ "package": "opencode-subagent-tools",
225
+ "options": {
226
+ "limits": {
227
+ "maxDiscoveryNodes": 1000,
228
+ "maxDiscoveryDepth": 16,
229
+ "maxDiscoveryPages": 10,
230
+ "maxListRows": 50,
231
+ "maxCancelBatch": 10,
232
+ "maxTextLength": 24000,
233
+ },
234
+ },
235
+ },
236
+ ],
237
+ }
238
+ ```
@@ -0,0 +1,78 @@
1
+ # Development
2
+
3
+ ## Dependencies
4
+
5
+ The plugin imports only `@opencode/plugin` plus Node built-ins:
6
+
7
+ ```ts
8
+ import { Plugin, Skill } from "@opencode/plugin"
9
+ import { readFile } from "node:fs/promises"
10
+ import { fileURLToPath } from "node:url"
11
+ ```
12
+
13
+ - `@opencode/plugin` is the plugin API package; see the README for the supported OpenCode V2 version. It ships the Promise plugin API used here (`Plugin.define`, `ctx.tool.transform`, `ctx.event`, `ctx.session`, `ctx.permission`, `ctx.model`, `ctx.skill`, `ctx.options`), including the typed `ctx.session.list`.
14
+ - At runtime **OpenCode resolves `@opencode/plugin` itself**, so a user who copies the package into a plugins directory does not need to install anything for the plugin to work.
15
+ - `package.json` declares `@opencode/plugin` as an **optional peer dependency** (and as a dev dependency for local typechecking and tests), not a bundled runtime dependency. OpenCode provides it at runtime, so installing the plugin does not need to pull in a second copy.
16
+ - There are **no third-party runtime dependencies** in the plugin source.
17
+ - `ctx.session.list` is required. `setup` fails with a clear error on a build that does not expose it; there is no event-only fallback.
18
+ - The optional `limits` options are validated and clamped in `config.ts`; see [configuration](configuration.md) for the public defaults and ranges.
19
+
20
+ ## Source layout
21
+
22
+ `src/index.ts` is the composition root. It resolves configuration, constructs the runtime services, registers tools and the skill, starts the event stream, and returns the cleanup function. Domain modules own one concept each:
23
+
24
+ | Module | Owns |
25
+ | ---------------- | ----------------------------------------------------------------------- |
26
+ | `config.ts` | Public `Options`/`Limits`, defaults, and clamping |
27
+ | `status.ts` | Status vocabulary and session-record status derivation |
28
+ | `session.ts` | Session-shaped types and `session.list` response adapters |
29
+ | `live-state.ts` | Process-local live evidence and freshness rules |
30
+ | `session-row.ts` | `SessionRow` projection used by discovery and tools |
31
+ | `discovery.ts` | Bounded subtree walk and row discovery |
32
+ | `access.ts` | `session.get` read-back and parent-chain ownership verification |
33
+ | `permissions.ts` | `subagent.control` permission gate |
34
+ | `messages.ts` | Shared steer/queue/context admission path |
35
+ | `interrupt.ts` | Shared interrupt outcome mapping |
36
+ | `cursor.ts` | Opaque paging cursor encode/decode/compare |
37
+ | `tokens.ts` | Token normalization, totals, context formula, usage schema |
38
+ | `format.ts` | Human-readable age, cost, and compact-number formatting |
39
+ | `text.ts` | Clipping, Unicode length, control-character, and length-error helpers |
40
+ | `errors.ts` | `Tool.Error` helper and the OpenCode capability message |
41
+ | `events.ts` | Event names, reducer, reconnect loop |
42
+ | `skill.ts` | Bundled `SKILL.md` parsing and registration |
43
+ | `tools/*.ts` | Tool registration grouped by shared behavior, plus input/output helpers |
44
+
45
+ `SKILL.md` sits at the package root, beside `src/`; `skill.ts` resolves it with `new URL("../SKILL.md", import.meta.url)`.
46
+
47
+ ## Tests
48
+
49
+ The suite lives in `tests/` and mirrors the feature areas: `list`, `usage`, `actions`, `status`, `mutations`, `permissions`, `policy`, `cancel-descendants`, `discovery`, and `options`, plus focused unit tests for config, cursor, events, messages, output, schema validation, and tokens. `tests/harness.ts` builds the mocked plugin context and registers the per-test cleanup hook. Tests exercise the plugin through its public `setup`/tool surface.
50
+
51
+ CI runs the unit suite only. Loading the packed package into a real OpenCode server is a manual release check, not a CI job.
52
+
53
+ `tests/`, `.oxlintrc.json`, and `bun.lock` are repository-only; the npm package ships the source tree, `SKILL.md`, `SECURITY.md`, and the user-facing docs.
54
+
55
+ ## Commands
56
+
57
+ ```sh
58
+ bun install
59
+ bun run lint
60
+ bun run format:check
61
+ bun run typecheck
62
+ bun test
63
+ ```
64
+
65
+ `bun run check` runs lint, format checking, typecheck, and the test suite. `bun run format` rewrites files with Prettier. The lint configuration is in `.oxlintrc.json`; it enables Oxlint's `correctness` and `suspicious` categories plus the `Reflect` restriction.
66
+
67
+ The tests cover:
68
+
69
+ - Discovery through `ctx.session.list`: pagination, depth/node/page limits, bare arrays, malformed responses, failures, and incomplete-discovery warnings.
70
+ - Status filtering and cursor pagination for `list`: `known`/`matching`/`window`/`remaining`, empty-status rejection, new children between pages, and cursor scoping.
71
+ - `cancel_descendants`: `targets`, `remaining`, cursor, before-cursor running children, abort, and failed/unknown/read-back outcomes.
72
+ - Configurable limits, `sessionControlPolicy` modes, and ancestor verification, including depth exhaustion reported as unverified.
73
+ - Cancellation, steering, queueing, and context injection.
74
+ - Usage freshness markers (`usageMayLag` and tree), context unavailable/zero handling, and `Tool.Error` refusals.
75
+ - Live status and freshness (`activeAt`), stale live terminal reconciliation, out-of-order event rejection, event-stream reconnect, and event subscription filtering.
76
+ - Permission routing and denial identity, cleanup, and the `ctx.session.list` capability guard.
77
+
78
+ Discovery, ownership checks, interrupts, prompt/synthetic admission, and read-backs accept the tool's `context.signal`; stopping a tool call stops the underlying work where the client supports it.
@@ -0,0 +1,137 @@
1
+ # Installation
2
+
3
+ The standard install uses npm or GitHub; see the [README quick start](../README.md#quick-start).
4
+ This page covers manual and local installation, path rules, and the difference
5
+ between server and CLI plugins.
6
+
7
+ ## Requirements
8
+
9
+ - An OpenCode V2 build with `ctx.session.list`; see the [README](../README.md) for the supported version.
10
+ - The plugin is a TypeScript directory package: `package.json` exports
11
+ `./src/index.ts`, and OpenCode loads the sources directly. There is no build
12
+ step.
13
+ - Keep `SKILL.md` beside `src/` if you want the bundled skill. Without it, the
14
+ tools still load, but the skill is skipped.
15
+
16
+ ## Manual installation
17
+
18
+ Use this when you want to run a local checkout or pin the plugin yourself.
19
+
20
+ ### Global plugins directory
21
+
22
+ OpenCode discovers immediate plugin package directories under the OpenCode
23
+ config directory:
24
+
25
+ ```text
26
+ <global-config-dir>/plugins/
27
+ ```
28
+
29
+ On Windows the global config directory is `%USERPROFILE%\.config\opencode`. Copy
30
+ the package as a directory named `subagent-tools`:
31
+
32
+ ```powershell
33
+ $target = "$env:USERPROFILE\.config\opencode\plugins\subagent-tools"
34
+ New-Item -ItemType Directory -Force $target | Out-Null
35
+ Copy-Item .\package.json, .\SKILL.md $target
36
+ Copy-Item .\src $target -Recurse
37
+ ```
38
+
39
+ OpenCode watches this directory and reloads plugins automatically. If the new
40
+ package is not picked up, run `opencode reload`; a full restart is not normally
41
+ needed.
42
+
43
+ This layout loads the plugin with its default options. An auto-discovered
44
+ package has no `plugins` entry, so there is nowhere to attach `options`. Use the
45
+ directory package layout below when you need to pass options.
46
+
47
+ > Project-scoped plugins live in `.opencode/plugins/`. A `plugins/` directory
48
+ > beside a project-root `opencode.json(c)` is **not** auto-discovered.
49
+
50
+ ### From a local directory package
51
+
52
+ Global config lives at `~/.config/opencode/opencode.jsonc`; project config lives
53
+ at `./opencode.jsonc` or `./.opencode/opencode.jsonc`. Relative paths resolve
54
+ from the config file that contains the entry, so a path that works in
55
+ `./opencode.jsonc` needs one extra `../` in `./.opencode/opencode.jsonc`.
56
+
57
+ `opencode.json(c)` can load the plugin from a directory that contains a
58
+ `package.json` with `name` and `exports`. Point `package` at the directory.
59
+
60
+ Global config — absolute path:
61
+
62
+ ```jsonc
63
+ {
64
+ "$schema": "https://opencode.ai/config.json",
65
+ "plugins": ["C:/Users/<you>/opencode-subagent-tools"],
66
+ }
67
+ ```
68
+
69
+ Object form — required if you want to pass options:
70
+
71
+ ```jsonc
72
+ {
73
+ "$schema": "https://opencode.ai/config.json",
74
+ "plugins": [
75
+ {
76
+ "package": "C:/Users/<you>/opencode-subagent-tools",
77
+ "options": {
78
+ "namespace": "subagent",
79
+ "enforcePermissions": true,
80
+ "sessionControlPolicy": "descendants",
81
+ },
82
+ },
83
+ ],
84
+ }
85
+ ```
86
+
87
+ Project config — relative path from `./opencode.jsonc`:
88
+
89
+ ```jsonc
90
+ {
91
+ "$schema": "https://opencode.ai/config.json",
92
+ "plugins": [
93
+ {
94
+ "package": "./opencode-subagent-tools",
95
+ "options": {
96
+ "namespace": "subagent",
97
+ "enforcePermissions": true,
98
+ },
99
+ },
100
+ ],
101
+ }
102
+ ```
103
+
104
+ From `./.opencode/opencode.jsonc`, use `"package": "../opencode-subagent-tools"`
105
+ or an absolute path.
106
+
107
+ The `plugins` array also accepts npm package names, GitHub specifications, local
108
+ directories, and `file://` URLs. Local paths must be directories; a path to a
109
+ single file is skipped.
110
+
111
+ > **Do not load the plugin twice.** If the package directory is already
112
+ > auto-discovered from a `plugins/` directory, adding a second `plugins` entry is
113
+ > rejected by OpenCode as a duplicate plugin ID; the duplicate is reported as
114
+ > failed and does not load. Load the plugin either as an auto-discovered package
115
+ > (defaults only) or as a configured directory package (options), not both.
116
+
117
+ > **Windows paths.** Tilde expansion is not guaranteed for plugin paths on
118
+ > Windows; use an absolute forward-slash path such as `C:/Users/<you>/...`.
119
+ > `%USERPROFILE%` is not expanded inside JSON config strings.
120
+
121
+ > **Server plugin vs CLI plugin.** This plugin uses the server plugin API
122
+ > (`ctx.tool`, `ctx.session`, `ctx.event`, `ctx.permission`), so load it as a
123
+ > server plugin — from the global plugins directory or from `plugins` in
124
+ > `opencode.json(c)`. The separate `cli.json` is for CLI-only (TUI) plugins and
125
+ > is not needed here.
126
+
127
+ ## After installation
128
+
129
+ Run `opencode reload`, then ask the agent to call `subagent_list`. If the tool is
130
+ missing, check that `ctx.session.list` is available in your OpenCode build and
131
+ that the plugin loaded without errors.
132
+
133
+ ## Options
134
+
135
+ See [configuration](configuration.md) for all options, defaults, ranges, and
136
+ examples. For manual installs, the object form of the `plugins` entry is required
137
+ to pass options; an auto-discovered package always uses defaults.
@@ -0,0 +1,120 @@
1
+ # Limitations
2
+
3
+ Read these before you rely on the plugin for anything destructive. The tool
4
+ contracts are in [reference](reference.md); this page covers what can go wrong
5
+ and what to do about it.
6
+
7
+ ## Discovery and status
8
+
9
+ - **Discovery can miss sessions.** The plugin refreshes the subtree through
10
+ `ctx.session.list` before `list`, `cancel_descendants`, and `usage` with
11
+ `includeDescendants: true`, so it finds children launched before it loaded,
12
+ from the TUI, or by another process. The defaults
13
+ are 200 nodes, 8 levels, and 5 pages per parent; raise these limits in
14
+ [configuration](configuration.md) for bigger trees; partial results include a
15
+ warning, and empty results are inconclusive.
16
+ - **Ordering can skip a child.** OpenCode orders `session.list` by
17
+ `time.updated`, so a child that updates between pages can move and be missed;
18
+ `discoveryComplete` does not show it, so re-run discovery before a large batch.
19
+ - **Status can be stale.** `running` comes from live events and the session
20
+ record, and an `unknown` row may still be running. A resumed child can look
21
+ finished if the server omits its busy/idle timestamps.
22
+ - **Live state is not persisted.** After a restart, the plugin rediscovers
23
+ sessions through `ctx.session.list` and infers status from `session.get`; live
24
+ events fill it again.
25
+
26
+ ## Cancellation
27
+
28
+ - **`cancel` is a soft interrupt.** It uses `session.interrupt({ resume: false })`.
29
+ A long tool call may finish first, and there is no hard abort.
30
+ - **Idle jobs keep running.** OpenCode can keep shell, subagent, or image jobs
31
+ alive while a session is idle; `session.interrupt` does nothing there. When the
32
+ session is running, an interrupt cancels its jobs as the session stops; for an
33
+ idle session, use the job's own controls.
34
+ - **`cancel` does not cascade.** Interrupting a child leaves its own children
35
+ running. Use `cancel_descendants` for the caller's subtree, or cancel each
36
+ child.
37
+ - **The status check after `cancel` can lag.** The session record may still say
38
+ `running` after the interrupt. Re-run `list` if `readBackMayLag` is true or
39
+ the status is `running` or `unknown`.
40
+ - **Cancelling a child can wake the parent.** OpenCode may deliver a
41
+ `<subagent state="cancelled">` notice to the parent, which can resume, continue
42
+ its work, and spend more tokens. This is OpenCode behavior, not the plugin's;
43
+ see [issue #36423](https://github.com/anomalyco/opencode/issues/36423).
44
+ - **`cancel_descendants` stops running descendants in batches.** It interrupts
45
+ at most 20 per call (`maxCancelBatch`) and returns `remaining`; use
46
+ the cursor when one is returned to continue, and use `cancel` for `unknown`
47
+ rows. The status check can lag and an aborted call can leave part of the batch
48
+ running, so re-run `list` and inspect `targets` and `remaining`.
49
+
50
+ ## Usage and context
51
+
52
+ - **Usage can lag.** `usage` and `list` read the session record; cost and tokens
53
+ update per step, so a child can show `0` or stale values until its step
54
+ completes or it is cancelled. A child cancelled before its first step is saved
55
+ can keep `0`, so re-query when `usageMayLag` is true (it is set while the
56
+ session is running or unknown).
57
+ - **Context resets after compaction.** `contextTokens` is the last assistant step
58
+ after the last completed compaction and can drop. `tokens` and `tokensUsed` are
59
+ cumulative; use them for total usage, not current context.
60
+ - **Context after revert.** The UI reads the full loaded message list, while
61
+ `usage` counts from the latest completed compaction; if a staged revert
62
+ boundary predates that compaction, the two can differ. This is uncommon.
63
+ - **`contextTokens: 0` is not always missing.** Zero is valid when no assistant
64
+ step exists after the last compaction. If context cannot be read, or the
65
+ staged revert boundary is missing, the field is omitted and `warning` explains
66
+ why.
67
+ - **`usage.tree` includes the target.** `tree.sessions`, `tree.costUSD`, and the
68
+ token totals cover the target as well as its descendants, so do not
69
+ double-count. A running or unknown descendant can make the totals lag, and
70
+ incomplete discovery can leave sessions out; see [reference](reference.md#usage)
71
+ for the fields.
72
+
73
+ ## Targeting and permissions
74
+
75
+ - **Reads are unrestricted; mutations follow policy.** `list` and `usage` can
76
+ read any session, including the caller and its ancestors. Mutations follow
77
+ `sessionControlPolicy`, and `cancel_descendants` always stays in the caller's
78
+ subtree; see the [security model](security-model.md).
79
+ - **A mutation cannot target the caller.** `usage` can read it.
80
+ - **Permissions are not prompted by default.** Mutations request
81
+ `subagent.control`; a denial returns a tool error, and
82
+ `enforcePermissions: false` skips the check entirely. Add an `ask` or `deny`
83
+ rule if you rely on approval; see the [security model](security-model.md).
84
+
85
+ ## Nested subagents
86
+
87
+ - **Nested subagents need an OpenCode setting.** Set
88
+ `experimental.subagent_depth` to at least `2`; the default is `1`. This is an
89
+ OpenCode setting, not a plugin option.
90
+ - **Grandchild transcripts are not exposed.** The plugin can list, inspect,
91
+ steer, queue, and stop a grandchild, but it cannot read its transcript. Use the
92
+ intermediate parent as a relay, or have the grandchild write a handoff file.
93
+ - **Nested cancellation can cost tokens.** In a nested tree, the cancelled
94
+ session's parent is another subagent; it can resume after the interrupt, spend
95
+ tokens, and continue up the branch. Re-run `list` after `cancel_descendants`
96
+ and cancel that parent again if it resumed.
97
+
98
+ ## Pagination and output
99
+
100
+ - **A cursor does not see new sessions.** `list` and `cancel_descendants` return
101
+ a cursor for the next page, and a cursor sees the sessions found when the walk
102
+ started. Children launched later may not appear, so re-run without a cursor to
103
+ include them; retrying `cancel_descendants` with the cursor skips failed,
104
+ unknown, rejected, and not-read targets, so inspect them manually.
105
+ - **`list` is limited by `maxListRows`.** It returns at most 20 rows by default.
106
+ Raise `maxListRows` in [configuration](configuration.md) if you need more rows
107
+ per page; the per-call `limit` must be between 1 and `maxListRows`, and the
108
+ cursor still pages through the rest.
109
+ - **`list` filters on the last observed status.** Include `unknown` if you also
110
+ want sessions whose state is not known yet. An empty `statuses` array is
111
+ rejected; omit it to include all five.
112
+
113
+ ## Compatibility
114
+
115
+ - **An OpenCode V2 build with `ctx.session.list` is required.** There is no
116
+ event-only fallback. The plugin uses only public V2 plugin APIs: `ctx.tool`,
117
+ `ctx.session`, `ctx.event`, `ctx.permission`, `ctx.model`, `ctx.skill`, and
118
+ `ctx.options`. See the README for the supported version.
119
+ - **No fork or build step.** The plugin runs from source and uses the plugin API
120
+ that OpenCode provides.