opencode-subagent-tools 0.1.0 → 0.2.1
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.
- package/CHANGELOG.md +36 -0
- package/README.md +23 -18
- package/SECURITY.md +1 -1
- package/SKILL.md +60 -20
- package/docs/development.md +14 -10
- package/docs/installation.md +1 -7
- package/docs/limitations.md +45 -9
- package/docs/reference.md +83 -32
- package/package.json +5 -5
- package/src/access.ts +79 -79
- package/src/background.ts +602 -0
- package/src/config.ts +47 -47
- package/src/cursor.ts +41 -39
- package/src/discovery.ts +77 -69
- package/src/errors.ts +7 -7
- package/src/events.ts +141 -95
- package/src/format.ts +20 -20
- package/src/index.ts +49 -36
- package/src/interrupt.ts +4 -4
- package/src/live-state.ts +64 -41
- package/src/messages.ts +134 -84
- package/src/permissions.ts +18 -18
- package/src/session-row.ts +82 -21
- package/src/session.ts +69 -69
- package/src/skill.ts +24 -24
- package/src/status.ts +64 -64
- package/src/text.ts +16 -16
- package/src/tokens.ts +51 -51
- package/src/tools/cancel-descendants.ts +303 -170
- package/src/tools/cancel.ts +139 -73
- package/src/tools/index.ts +32 -30
- package/src/tools/input.ts +46 -44
- package/src/tools/list.ts +281 -175
- package/src/tools/names.ts +2 -2
- package/src/tools/output.ts +51 -51
- package/src/tools/send.ts +130 -96
- package/src/tools/usage.ts +181 -143
package/docs/reference.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The plugin registers seven tools under a namespace. With the default namespace `subagent`, the names are `subagent_list`, `subagent_usage`, `subagent_cancel`, `subagent_cancel_descendants`, `subagent_steer`, `subagent_queue`, and `subagent_context`.
|
|
4
4
|
|
|
5
|
-
The prefix follows the plugin's `namespace` option. OpenCode normalizes unsupported characters in names and namespaces to `_`.
|
|
5
|
+
The prefix follows the plugin's `namespace` option. OpenCode normalizes unsupported characters in names and namespaces to `_`. In Code Mode, call the same tools as `tools.subagent.list`, `tools.subagent.usage`, and so on; the namespace separator is `.` instead of `_`.
|
|
6
6
|
|
|
7
7
|
Inputs marked `?` are optional; unknown properties are rejected.
|
|
8
8
|
|
|
@@ -16,46 +16,59 @@ List discovered sessions in a subtree, newest launch first. Read-only.
|
|
|
16
16
|
|
|
17
17
|
### Inputs
|
|
18
18
|
|
|
19
|
-
| Name
|
|
20
|
-
|
|
|
21
|
-
| `parentID`
|
|
22
|
-
| `statuses`
|
|
23
|
-
| `
|
|
24
|
-
| `
|
|
19
|
+
| Name | Type | Default | Notes |
|
|
20
|
+
| --------------- | --------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
21
|
+
| `parentID` | string? | caller | Any session ID is accepted. |
|
|
22
|
+
| `statuses` | string[]? | all five | Execution statuses: `running`, `stopped`, `failed`, `finished`, or `unknown`. An empty array, an unknown value, or a non-array is rejected. |
|
|
23
|
+
| `active` | boolean? | — | Filter on the session's own activity. Omit to include both active and inactive rows. |
|
|
24
|
+
| `subtreeActive` | boolean? | — | Filter on the session or any discovered descendant. Omit to include both. |
|
|
25
|
+
| `limit` | integer? | `maxListRows` (20) | 1 to `maxListRows`; values outside this range are rejected. |
|
|
26
|
+
| `cursor` | string? | — | From a previous `list` call; pass it back unchanged. A cursor is bound to the status and activity filters. |
|
|
25
27
|
|
|
26
28
|
### Output
|
|
27
29
|
|
|
28
30
|
Text output is one row per session plus summary and warning lines. JSON output contains:
|
|
29
31
|
|
|
30
32
|
- `root` — subtree root: the caller or `parentID`.
|
|
31
|
-
- `statuses` — applied status filter.
|
|
33
|
+
- `statuses` — applied execution-status filter.
|
|
32
34
|
- `discoveryComplete` — whether discovery walked the whole subtree, not whether all rows fit on this page.
|
|
33
35
|
- `warning?` — set when discovery was incomplete.
|
|
34
|
-
- `known` — all discovered sessions before the status
|
|
36
|
+
- `known` — all discovered sessions before the status and activity filters and cursor.
|
|
37
|
+
- `activeCount` — all discovered sessions with their own active execution or background job, before filters.
|
|
38
|
+
- `subtreeActiveCount` — all discovered sessions that are active or have an active descendant, before filters.
|
|
35
39
|
- `matching` — matches in the current discovery pass, before the cursor.
|
|
36
40
|
- `window` — matches after the cursor; equals `matching` on the first page.
|
|
37
41
|
- `remaining` — matches in the window not returned on this page.
|
|
38
42
|
- `remainingPages` — pages left in the cursor walk.
|
|
39
43
|
- `cursor?` — cursor for the next page.
|
|
40
|
-
- `counts` — counts for all five statuses across all discovered sessions, before the
|
|
44
|
+
- `counts` — counts for all five execution statuses across all discovered sessions, before the status and activity filters and cursor. It sums to `known`.
|
|
41
45
|
- `rows[]` — one entry per session:
|
|
42
46
|
- `id`, `parentID?`, `agent?`, `title?`, `titleTruncated?` (true when the title was truncated).
|
|
43
|
-
- `status
|
|
44
|
-
- `
|
|
47
|
+
- `status` — execution status; `finished` means the last execution succeeded, not that the delegated task is complete.
|
|
48
|
+
- `active` — true when the session itself is running, unknown, or owns a background job. Descendants are excluded.
|
|
49
|
+
- `subtreeActive` — true when the session or any discovered descendant is active.
|
|
50
|
+
- `activeDescendants?` — true only when at least one discovered child is active in its subtree.
|
|
51
|
+
- `backgroundJob?` — the session's own observed work: `kinds` (`shell`, `image`, `subagent`, `codemode`), and optional `stateUnknown` when work may remain but could not be verified.
|
|
52
|
+
- `ageSeconds?` (omitted when the launch time is unknown), `launchedAt`.
|
|
53
|
+
- `usageMayLag?` — true when the row is active or unknown.
|
|
45
54
|
- `usage?` — `costUSD?`, `tokens?`, and `tokensUsed?` when the server provides them.
|
|
46
55
|
|
|
56
|
+
Text output adds `active` or `subtree` markers, `background=<kinds>`, `descendants=active`, and `usage=lagging` when relevant.
|
|
57
|
+
|
|
47
58
|
### Errors and edge cases
|
|
48
59
|
|
|
49
60
|
- A malformed or unreadable `parentID` returns `Tool.Error`.
|
|
50
61
|
- If the caller is unknown and `parentID` is omitted, the tool returns `Tool.Error`.
|
|
51
|
-
- An invalid cursor returns `Tool.Error`; a cursor is bound to the tool, root, and
|
|
62
|
+
- An invalid cursor returns `Tool.Error`; a cursor is bound to the tool, root, status filter, and activity filters.
|
|
63
|
+
- A non-boolean `active` or `subtreeActive` returns `Tool.Error`.
|
|
52
64
|
- Aborting discovery returns `Tool.Error` instead of a partial result.
|
|
53
65
|
- An empty result does not prove that no subagents exist. See [limitations](limitations.md).
|
|
54
66
|
- A new child can increase `known` and `matching` without appearing in the current cursor walk; re-run without a cursor to include it.
|
|
67
|
+
- `statuses` filters executions only. Use `active` or `subtreeActive` for work that is still in progress.
|
|
55
68
|
|
|
56
69
|
## usage
|
|
57
70
|
|
|
58
|
-
Read cost, tokens, current context, and
|
|
71
|
+
Read cost, tokens, current context, status, and activity for a session. Read-only.
|
|
59
72
|
|
|
60
73
|
### Inputs
|
|
61
74
|
|
|
@@ -68,8 +81,12 @@ Read cost, tokens, current context, and status for a session. Read-only.
|
|
|
68
81
|
|
|
69
82
|
Text output is a summary. JSON output contains:
|
|
70
83
|
|
|
71
|
-
- `sessionID`, `parentID?`, `status`.
|
|
72
|
-
- `
|
|
84
|
+
- `sessionID`, `parentID?`, `status`, `active`.
|
|
85
|
+
- `status` — execution status; the caller is reported as `running` while the tool executes.
|
|
86
|
+
- `active` — true when the session itself is running, unknown, or owns a background job.
|
|
87
|
+
- `subtreeActive?` — present only with `includeDescendants: true`; true when the target or any discovered descendant is active. Without the tree, descendant activity is unknown and the field is omitted.
|
|
88
|
+
- `backgroundJob?` — the target's own observed work: `kinds`, and optional `stateUnknown`.
|
|
89
|
+
- `usageMayLag?` — true when the session is active or unknown; values are as of the last persisted step.
|
|
73
90
|
- `costUSD?` — the session's own cost, not a family total.
|
|
74
91
|
- `tokens?` — cumulative `input`, `output`, `reasoning`, `cache.read`, and `cache.write`.
|
|
75
92
|
- `tokensUsed?` — cumulative `input + output + reasoning`.
|
|
@@ -82,7 +99,7 @@ Text output is a summary. JSON output contains:
|
|
|
82
99
|
- `sessions` includes the target; `descendants` excludes it.
|
|
83
100
|
- `costUSD`, `tokens`, and `tokensUsed` include the target. `tokensUsed` is omitted when no session in the tree reports token data.
|
|
84
101
|
- `discoveryComplete` covers the descendant walk.
|
|
85
|
-
- `descendantsMayLag?` flags
|
|
102
|
+
- `descendantsMayLag?` flags active or unknown descendants.
|
|
86
103
|
- `warning?` marks incomplete discovery; it is also mirrored at the top-level `warning`.
|
|
87
104
|
- `warning?` — set when context could not be read or discovery was incomplete.
|
|
88
105
|
|
|
@@ -115,18 +132,23 @@ Text output reports the interrupt result, the current state, and a lag note. JSO
|
|
|
115
132
|
- `sessionID`, `accepted`, `interrupt`.
|
|
116
133
|
- `interrupt` — `accepted` (the server accepted), `rejected` (no active execution), or `unknown` (no definitive server answer; an abort during the interrupt is a `Tool.Error`).
|
|
117
134
|
- `accepted` — alias for `interrupt === "accepted"`.
|
|
118
|
-
- `status` — status after the interrupt and status check.
|
|
135
|
+
- `status` — execution status after the interrupt and status check.
|
|
136
|
+
- `active` — true when the target is running, unknown, or owns observed background work after the call.
|
|
137
|
+
- `backgroundJob?` — the target's own observed work: `kinds` (`shell`, `image`, `subagent`, `codemode`), `mayRemain?` when work may still run after the call, and `stateUnknown?` when a probe failed.
|
|
119
138
|
- `reason?` — the recorded reason.
|
|
120
139
|
- `readBackMayLag` — true when the interrupt was accepted and the status check after cancellation still reports `running` or `unknown`.
|
|
121
140
|
- `cancellationDoesNotCascade` — always `true`; cancellation is non-cascading, not a claim that the target has no children.
|
|
122
141
|
- `costUSD?`, `tokens?`, `tokensUsed?` — usage after the interrupt.
|
|
123
142
|
|
|
143
|
+
Text distinguishes an accepted interrupt with a remaining job ("may continue"), an idle target with a job ("cannot stop the background job"), and an unverified probe ("state could not be verified"). If the target's subtree may still be active and no own background job was observed, the text says so.
|
|
144
|
+
|
|
124
145
|
### Errors and edge cases
|
|
125
146
|
|
|
126
147
|
- An unknown target, a self-target, a malformed ID, or a target outside `sessionControlPolicy` returns `Tool.Error`.
|
|
127
148
|
- Aborting before or during the interrupt returns `Tool.Error`. An abort after the interrupt may return a normal result.
|
|
128
149
|
- Cancelling a stopped, failed, or finished session is not an error.
|
|
129
|
-
-
|
|
150
|
+
- If the target is idle or terminal and no live background job is known, the tool probes `session.context(target)` for running shell messages and shell/image/subagent tool parts. It cannot see a Code Mode job; that requires an idle snapshot. A failed probe, or a probe without an authoritative idle snapshot for the target, sets `backgroundJob.stateUnknown`; it does not claim there is no job.
|
|
151
|
+
- This is a soft interrupt: it does not cascade, a long tool call may finish first, and jobs owned by an idle session keep running. The tool reports those jobs but cannot stop them.
|
|
130
152
|
- OpenCode may wake the parent with a cancellation notice.
|
|
131
153
|
- If `interrupt` is `unknown`, `readBackMayLag` is true, or `status` is `running` or `unknown`, re-run `list`.
|
|
132
154
|
|
|
@@ -150,19 +172,27 @@ Text output is a summary with counts and per-target details. JSON output contain
|
|
|
150
172
|
- `known` — all discovered descendants in the current pass, before the cursor is applied.
|
|
151
173
|
- `considered` — descendants after the cursor.
|
|
152
174
|
- `running` — running descendants among `considered`.
|
|
175
|
+
- `activeCount` — considered descendants with their own active execution or background job.
|
|
176
|
+
- `subtreeActiveCount` — considered descendants that are active or have an active descendant.
|
|
177
|
+
- `backgroundJobCount` — considered descendants with `status !== "running"` that own a background job.
|
|
178
|
+
- `backgroundJobMayRemain?` — true when an observed background job may still run after the call.
|
|
153
179
|
- `beforeCursorRunning?` — running descendants before the cursor window; re-run without a cursor to include them.
|
|
154
|
-
- `targets[]` — one entry per attempted target: `sessionID`, `interrupt`, `readBack`, `status?` (status after the check, when `readBack` is `ok`), and `error?` (failure detail).
|
|
180
|
+
- `targets[]` — one entry per attempted target: `sessionID`, `interrupt`, `readBack`, `status?` (status after the check, when `readBack` is `ok`), `backgroundJob?` (observed work with `mayRemain?`), and `error?` (failure detail).
|
|
155
181
|
- `interrupt` — `accepted` (the server accepted), `rejected` (no active execution), `failed` (the call threw), or `unknown` (aborted or no definitive answer).
|
|
156
182
|
- `readBack` — `ok` (the status check after cancellation returned a status), `failed`, or `not-read` (not attempted or abandoned after an abort).
|
|
157
183
|
- `remaining` — running targets not attempted; use `cursor` to continue, or retry when no cursor is returned.
|
|
158
184
|
- `aborted` — true when the call stopped before the batch completed.
|
|
159
185
|
- `reason?`, `cursor?`, `warning?`.
|
|
160
186
|
|
|
187
|
+
If no execution is running but idle descendants own background jobs, the result is normal with `running: 0` and `backgroundJobCount > 0`; the summary says those jobs cannot be stopped. Idle parents with active descendants are reported through `subtreeActiveCount` and the summary, not interrupted.
|
|
188
|
+
|
|
161
189
|
### Errors and edge cases
|
|
162
190
|
|
|
163
191
|
- An unknown caller returns `Tool.Error`.
|
|
164
192
|
- Aborting before discovery returns `Tool.Error`. Aborting during discovery or the batch returns `aborted: true` with the targets that were attempted and a warning when discovery was incomplete.
|
|
165
193
|
- Only `running` rows are targeted. Use `cancel` for `unknown` rows.
|
|
194
|
+
- Idle rows that own background jobs are counted in `backgroundJobCount`; this tool cannot stop those jobs.
|
|
195
|
+
- Idle parents with active descendants are counted in `subtreeActiveCount`; the running descendant is the target, not the parent.
|
|
166
196
|
- `maxCancelBatch` limits targets per call (20 by default).
|
|
167
197
|
- The whole batch is checked in one permission request.
|
|
168
198
|
- Cursor retries skip failed, unknown, rejected, and not-read targets. Inspect them manually.
|
|
@@ -187,18 +217,21 @@ Text output reports the target and delivery. JSON output contains:
|
|
|
187
217
|
|
|
188
218
|
- `sessionID`, `messageID?`, `delivery`, `chars`, `resume`.
|
|
189
219
|
- `status?` — status observed before the message was accepted; a wake may not be reflected yet.
|
|
220
|
+
- `active` — the observed target was running, unknown, or owned a background job.
|
|
221
|
+
- `backgroundJob?` — the target's own observed work.
|
|
190
222
|
- `pending?` — true when the message was accepted without waking an idle child.
|
|
191
223
|
|
|
192
224
|
### Errors and edge cases
|
|
193
225
|
|
|
194
226
|
- An unknown target, a self-target, empty or overlong text, or a target outside `sessionControlPolicy` returns `Tool.Error`.
|
|
195
227
|
- The message is delivered before the child's next model request; the current tool call is not interrupted.
|
|
196
|
-
-
|
|
228
|
+
- The plugin cannot move a blocking shell to the background, so delivery may wait for that shell. This is a UI parity gap, not a hard failure.
|
|
229
|
+
- OpenCode sends a `<subagent>` report each time the child goes idle. A report can say `state="running"` and `Background work still running` when the execution is idle but background work remains. Read the report; do not resume the child just to read its result.
|
|
197
230
|
- `delivery` is the server-reported delivery, normally `steer`.
|
|
198
231
|
|
|
199
232
|
## queue
|
|
200
233
|
|
|
201
|
-
Queue a message as the next independent turn after the current
|
|
234
|
+
Queue a message as the next independent turn after the current execution settles.
|
|
202
235
|
|
|
203
236
|
### Inputs
|
|
204
237
|
|
|
@@ -214,12 +247,17 @@ Text output reports the target and delivery. JSON output contains:
|
|
|
214
247
|
|
|
215
248
|
- `sessionID`, `messageID?`, `delivery`, `chars`, `resume`.
|
|
216
249
|
- `status?` — status observed before the message was accepted; a wake may not be reflected yet.
|
|
250
|
+
- `active` — the observed target was running, unknown, or owned a background job.
|
|
251
|
+
- `backgroundJob?` — the target's own observed work.
|
|
252
|
+
- `queued?` — true when the message was accepted but has not started yet. Inferred from the observed status and `resume`; it does not inspect the server's existing queue, so other queued prompts may be ahead.
|
|
217
253
|
- `pending?` — true when the message was accepted without waking an idle child.
|
|
218
254
|
|
|
219
255
|
### Errors and edge cases
|
|
220
256
|
|
|
221
257
|
- An unknown target, a self-target, empty or overlong text, or a target outside `sessionControlPolicy` returns `Tool.Error`.
|
|
222
|
-
- The message does not join the current run; it waits
|
|
258
|
+
- The message does not join the current run; it waits for the current execution to settle, then starts a separate turn.
|
|
259
|
+
- Background jobs are not waited for and continue in parallel. The queue is promoted at the idle boundary even when background work remains.
|
|
260
|
+
- `queued` is `true` when the target was running/unknown, when its status could not be observed, or when it was idle with `resume: false`; it is `false` when the target was idle and `resume: true`.
|
|
223
261
|
- `delivery` is the server-reported delivery, normally `queue`.
|
|
224
262
|
|
|
225
263
|
## context
|
|
@@ -241,6 +279,8 @@ Text output reports the target and size. JSON output contains:
|
|
|
241
279
|
- `sessionID`, `messageID?`, `delivery: "next-step"`, `chars`.
|
|
242
280
|
- `description?`.
|
|
243
281
|
- `status?` — status observed before the message was accepted; context does not wake the child.
|
|
282
|
+
- `active` — the observed target was running, unknown, or owned a background job.
|
|
283
|
+
- `backgroundJob?` — the target's own observed work.
|
|
244
284
|
- `pending?` — true when the message was accepted without waking an idle child.
|
|
245
285
|
|
|
246
286
|
### Errors and edge cases
|
|
@@ -250,34 +290,43 @@ Text output reports the target and size. JSON output contains:
|
|
|
250
290
|
- An idle child keeps the message pending until its next run.
|
|
251
291
|
- For an instruction without waking, use `steer` with `resume: false`. For a follow-up turn, use `queue`.
|
|
252
292
|
|
|
253
|
-
## Statuses
|
|
293
|
+
## Statuses and activity
|
|
254
294
|
|
|
255
295
|
- `running` — the session is executing; a live start newer than the last idle marker, or `busy > idle` in the session record, keeps it running.
|
|
256
296
|
- `stopped` — the run was interrupted before it finished.
|
|
257
297
|
- `failed` — the last run failed.
|
|
258
|
-
- `finished` — the last run succeeded.
|
|
298
|
+
- `finished` — the last run succeeded. This does not mean the delegated task is complete.
|
|
259
299
|
- `unknown` — no reliable status is available; the session may still be running.
|
|
260
300
|
|
|
261
|
-
|
|
301
|
+
`status` is execution-oriented. In OpenCode 2.3.0+, shell, image, subagent, or Code Mode work can stay alive while a session is idle, so use the activity fields for "still working":
|
|
262
302
|
|
|
263
|
-
`
|
|
303
|
+
- `active` — the session itself is running, unknown, or owns a background job. Descendants are not included.
|
|
304
|
+
- `subtreeActive` — the session or any discovered descendant is active.
|
|
305
|
+
- `activeDescendants` — at least one discovered child is active. An idle parent with active descendants may wake.
|
|
306
|
+
- `backgroundJob` — the session's own observed work: `kinds` can contain `shell`, `image`, `subagent`, and `codemode`. `stateUnknown` means work may remain but could not be verified.
|
|
307
|
+
- `usageMayLag` — the session is active or unknown, so usage may still grow.
|
|
308
|
+
|
|
309
|
+
Status and activity are observed from live events, the idle hook, shell events, structured context probes, and the session record. Code Mode jobs are visible only through the idle hook. They can lag, and an `unknown` row may still be running. See [limitations](limitations.md).
|
|
310
|
+
|
|
311
|
+
`list` includes all five statuses by default. `statuses` filters executions only; use `active` and `subtreeActive` for work that is still in progress. `counts` always covers all five statuses and sums to `known`.
|
|
264
312
|
|
|
265
313
|
## Discovery and pagination
|
|
266
314
|
|
|
267
|
-
|
|
315
|
+
OpenCode 2.3.0+ is required. `list`, `cancel_descendants`, and `usage` with `includeDescendants: true` walk the relevant subtree through `ctx.session.list`. This finds children launched before the plugin loaded, from the TUI, or by another process.
|
|
268
316
|
|
|
269
317
|
Discovery stops after `maxDiscoveryNodes` nodes, `maxDiscoveryDepth` levels, or `maxDiscoveryPages` pages per parent. The defaults are 200, 8, and 5. An incomplete result includes a warning; an empty result is not proof that no subagents exist. See [configuration](configuration.md) for the limits.
|
|
270
318
|
|
|
271
|
-
`list` and `cancel_descendants` return a cursor for stable paging. A cursor keeps its place while statuses change. A cursor walk sees the sessions found when it started; children launched after the first page may not appear in that walk. Re-run without a cursor to include them.
|
|
319
|
+
`list` and `cancel_descendants` return a cursor for stable paging. A cursor keeps its place while statuses change. A `list` cursor is bound to the status and activity filters. A cursor walk sees the sessions found when it started; children launched after the first page may not appear in that walk. Re-run without a cursor to include them.
|
|
272
320
|
|
|
273
321
|
For `list`:
|
|
274
322
|
|
|
275
|
-
- `known` — all discovered sessions before the status
|
|
323
|
+
- `known` — all discovered sessions before the status and activity filters and cursor.
|
|
324
|
+
- `activeCount`, `subtreeActiveCount` — all discovered sessions with own or subtree activity, before filters.
|
|
276
325
|
- `matching` — matches in the current discovery pass, before the cursor.
|
|
277
326
|
- `window` — matches after the cursor.
|
|
278
327
|
- `remaining` — matches in the window not returned on this page.
|
|
279
328
|
- `remainingPages` — pages left in the cursor walk.
|
|
280
|
-
- `counts` — all discovered sessions by status, before the
|
|
329
|
+
- `counts` — all discovered sessions by status, before the filters and cursor; it sums to `known`.
|
|
281
330
|
|
|
282
331
|
A new child can increase `known` and `matching` without appearing in the current cursor walk. `matching - window` then includes previous pages and new children.
|
|
283
332
|
|
|
@@ -301,7 +350,7 @@ Per-tool defaults (when both formats are allowed):
|
|
|
301
350
|
| `queue` | `text` |
|
|
302
351
|
| `context` | `text` |
|
|
303
352
|
|
|
304
|
-
Use `text` for a quick read. Use `json` when you need fields such as `targets`, `pending`, `usage`, `tree`, or `lastRequest`. See [configuration](configuration.md) for the option.
|
|
353
|
+
Use `text` for a quick read. Use `json` when you need fields such as `targets`, `pending`, `queued`, `active`, `subtreeActive`, `backgroundJob`, `usage`, `tree`, or `lastRequest`. See [configuration](configuration.md) for the option.
|
|
305
354
|
|
|
306
355
|
## Context vs cumulative usage
|
|
307
356
|
|
|
@@ -340,6 +389,8 @@ Abort behavior:
|
|
|
340
389
|
- `cancel` returns `Tool.Error` when the abort happens before or during the interrupt. An abort after the interrupt may still return a normal result.
|
|
341
390
|
- `cancel_descendants` returns `Tool.Error` before discovery. Otherwise it reports `aborted: true` with the targets it attempted, and a warning when discovery was incomplete.
|
|
342
391
|
|
|
392
|
+
In OpenCode 2.3.0+, these `Tool.Error` results are normal call results from a cooperative stop, not interrupted tool calls.
|
|
393
|
+
|
|
343
394
|
## Permissions and configuration
|
|
344
395
|
|
|
345
396
|
Mutations use the `subagent.control` permission action when `enforcePermissions` is enabled; see the [security model](security-model.md) for targeting rules and policy modes.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-subagent-tools",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "OpenCode V2 plugin that gives the agent tools to list, inspect
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "OpenCode V2 plugin that gives the agent tools to list, inspect, cancel, steer, queue, and inject context into its subagent sessions.",
|
|
5
5
|
"author": "Maxim Matsiushonak",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"prepublishOnly": "bun run check"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@opencode/plugin": "^2.
|
|
57
|
+
"@opencode/plugin": "^2.3.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependenciesMeta": {
|
|
60
60
|
"@opencode/plugin": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@opencode/plugin": "^2.2
|
|
65
|
+
"@opencode/plugin": "^2.3.2",
|
|
66
66
|
"@types/bun": "^1.4.3",
|
|
67
67
|
"@types/node": "^26.6.5",
|
|
68
68
|
"oxlint": "1.60.0",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"typescript": "^7.0.2"
|
|
71
71
|
},
|
|
72
72
|
"prettier": {
|
|
73
|
-
"semi":
|
|
73
|
+
"semi": true,
|
|
74
74
|
"printWidth": 90
|
|
75
75
|
}
|
|
76
76
|
}
|
package/src/access.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import type { Plugin } from "@opencode/plugin"
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
2
|
import {
|
|
3
3
|
SESSION_CONTROL_POLICY,
|
|
4
4
|
type ResolvedLimits,
|
|
5
5
|
type SessionControlPolicy,
|
|
6
|
-
} from "./config"
|
|
7
|
-
import { toolError } from "./errors"
|
|
8
|
-
import { normalizeSessionID, type SessionInfoLike } from "./session"
|
|
9
|
-
import type { ToolCtx } from "./tools/input"
|
|
10
|
-
import { TOOL } from "./tools/names"
|
|
6
|
+
} from "./config";
|
|
7
|
+
import { toolError } from "./errors";
|
|
8
|
+
import { normalizeSessionID, type SessionInfoLike } from "./session";
|
|
9
|
+
import type { ToolCtx } from "./tools/input";
|
|
10
|
+
import { TOOL } from "./tools/names";
|
|
11
11
|
|
|
12
12
|
export const OWNERSHIP = {
|
|
13
13
|
owned: "owned",
|
|
14
14
|
foreign: "foreign",
|
|
15
15
|
unknown: "unknown",
|
|
16
|
-
} as const
|
|
16
|
+
} as const;
|
|
17
17
|
|
|
18
|
-
export type Ownership = (typeof OWNERSHIP)[keyof typeof OWNERSHIP]
|
|
18
|
+
export type Ownership = (typeof OWNERSHIP)[keyof typeof OWNERSHIP];
|
|
19
19
|
|
|
20
20
|
export interface GetSessionResult {
|
|
21
|
-
info?: SessionInfoLike
|
|
22
|
-
ok: boolean
|
|
21
|
+
info?: SessionInfoLike;
|
|
22
|
+
ok: boolean;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export interface AccessDeps {
|
|
26
|
-
session: Plugin.Context["session"]
|
|
27
|
-
limits: Pick<ResolvedLimits, "maxDiscoveryDepth"
|
|
28
|
-
policy: SessionControlPolicy
|
|
29
|
-
namespace: string
|
|
26
|
+
session: Plugin.Context["session"];
|
|
27
|
+
limits: Pick<ResolvedLimits, "maxDiscoveryDepth">;
|
|
28
|
+
policy: SessionControlPolicy;
|
|
29
|
+
namespace: string;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
const PARENT_WALK = {
|
|
33
33
|
match: "match",
|
|
34
34
|
root: "root",
|
|
35
35
|
unknown: "unknown",
|
|
36
|
-
} as const
|
|
36
|
+
} as const;
|
|
37
37
|
|
|
38
|
-
type ParentWalk = (typeof PARENT_WALK)[keyof typeof PARENT_WALK]
|
|
38
|
+
type ParentWalk = (typeof PARENT_WALK)[keyof typeof PARENT_WALK];
|
|
39
39
|
|
|
40
40
|
export const createAccess = ({ session, limits, policy, namespace }: AccessDeps) => {
|
|
41
41
|
const getSession = async (
|
|
@@ -48,11 +48,11 @@ export const createAccess = ({ session, limits, policy, namespace }: AccessDeps)
|
|
|
48
48
|
| SessionInfoLike
|
|
49
49
|
| undefined,
|
|
50
50
|
ok: true,
|
|
51
|
-
}
|
|
51
|
+
};
|
|
52
52
|
} catch {
|
|
53
|
-
return { ok: false }
|
|
53
|
+
return { ok: false };
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
};
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Walk a parent chain until `matches` accepts a parent ID or the chain ends.
|
|
@@ -64,27 +64,27 @@ export const createAccess = ({ session, limits, policy, namespace }: AccessDeps)
|
|
|
64
64
|
signal: AbortSignal | undefined,
|
|
65
65
|
matches: (parentID: string) => boolean,
|
|
66
66
|
): Promise<{ relation: ParentWalk; startInfo?: SessionInfoLike }> => {
|
|
67
|
-
const seen = new Set<string>()
|
|
68
|
-
let current = start
|
|
69
|
-
let startInfo: SessionInfoLike | undefined
|
|
67
|
+
const seen = new Set<string>();
|
|
68
|
+
let current = start;
|
|
69
|
+
let startInfo: SessionInfoLike | undefined;
|
|
70
70
|
|
|
71
71
|
for (let depth = 0; depth < limits.maxDiscoveryDepth; depth++) {
|
|
72
|
-
if (signal?.aborted) return { relation: PARENT_WALK.unknown, startInfo }
|
|
73
|
-
if (seen.has(current)) return { relation: PARENT_WALK.unknown, startInfo }
|
|
74
|
-
seen.add(current)
|
|
75
|
-
const { info, ok } = await getSession(current, signal)
|
|
76
|
-
if (!ok || !info) return { relation: PARENT_WALK.unknown, startInfo }
|
|
77
|
-
if (current === start) startInfo = info
|
|
78
|
-
const parentID = normalizeSessionID(info.parentID)
|
|
72
|
+
if (signal?.aborted) return { relation: PARENT_WALK.unknown, startInfo };
|
|
73
|
+
if (seen.has(current)) return { relation: PARENT_WALK.unknown, startInfo };
|
|
74
|
+
seen.add(current);
|
|
75
|
+
const { info, ok } = await getSession(current, signal);
|
|
76
|
+
if (!ok || !info) return { relation: PARENT_WALK.unknown, startInfo };
|
|
77
|
+
if (current === start) startInfo = info;
|
|
78
|
+
const parentID = normalizeSessionID(info.parentID);
|
|
79
79
|
if (info.parentID !== undefined && parentID === undefined) {
|
|
80
|
-
return { relation: PARENT_WALK.unknown, startInfo }
|
|
80
|
+
return { relation: PARENT_WALK.unknown, startInfo };
|
|
81
81
|
}
|
|
82
|
-
if (!parentID) return { relation: PARENT_WALK.root, startInfo }
|
|
83
|
-
if (matches(parentID)) return { relation: PARENT_WALK.match, startInfo }
|
|
84
|
-
current = parentID
|
|
82
|
+
if (!parentID) return { relation: PARENT_WALK.root, startInfo };
|
|
83
|
+
if (matches(parentID)) return { relation: PARENT_WALK.match, startInfo };
|
|
84
|
+
current = parentID;
|
|
85
85
|
}
|
|
86
|
-
return { relation: PARENT_WALK.unknown, startInfo }
|
|
87
|
-
}
|
|
86
|
+
return { relation: PARENT_WALK.unknown, startInfo };
|
|
87
|
+
};
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
90
|
* Confirm that the caller is an ancestor of `target`. A chain that is too
|
|
@@ -95,15 +95,15 @@ export const createAccess = ({ session, limits, policy, namespace }: AccessDeps)
|
|
|
95
95
|
caller: string,
|
|
96
96
|
signal?: AbortSignal,
|
|
97
97
|
): Promise<{ ownership: Ownership; info?: SessionInfoLike }> => {
|
|
98
|
-
const result = await walkParents(target, signal, (parentID) => parentID === caller)
|
|
98
|
+
const result = await walkParents(target, signal, (parentID) => parentID === caller);
|
|
99
99
|
const ownership =
|
|
100
100
|
result.relation === PARENT_WALK.match
|
|
101
101
|
? OWNERSHIP.owned
|
|
102
102
|
: result.relation === PARENT_WALK.root
|
|
103
103
|
? OWNERSHIP.foreign
|
|
104
|
-
: OWNERSHIP.unknown
|
|
105
|
-
return { ownership, info: result.startInfo }
|
|
106
|
-
}
|
|
104
|
+
: OWNERSHIP.unknown;
|
|
105
|
+
return { ownership, info: result.startInfo };
|
|
106
|
+
};
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* Report whether `target` is an ancestor of `caller`. The result is unknown
|
|
@@ -114,95 +114,95 @@ export const createAccess = ({ session, limits, policy, namespace }: AccessDeps)
|
|
|
114
114
|
caller: string,
|
|
115
115
|
signal?: AbortSignal,
|
|
116
116
|
): Promise<"ancestor" | "not-ancestor" | "unknown"> => {
|
|
117
|
-
const result = await walkParents(caller, signal, (parentID) => parentID === target)
|
|
117
|
+
const result = await walkParents(caller, signal, (parentID) => parentID === target);
|
|
118
118
|
return result.relation === PARENT_WALK.match
|
|
119
119
|
? "ancestor"
|
|
120
120
|
: result.relation === PARENT_WALK.root
|
|
121
121
|
? "not-ancestor"
|
|
122
|
-
: "unknown"
|
|
123
|
-
}
|
|
122
|
+
: "unknown";
|
|
123
|
+
};
|
|
124
124
|
|
|
125
|
-
const aborted = (): never => toolError("Aborted before the target could be verified.")
|
|
125
|
+
const aborted = (): never => toolError("Aborted before the target could be verified.");
|
|
126
126
|
|
|
127
127
|
const readAuthorizedTarget = async (
|
|
128
128
|
target: string,
|
|
129
129
|
context: ToolCtx,
|
|
130
130
|
): Promise<SessionInfoLike> => {
|
|
131
|
-
const { info, ok } = await getSession(target, context.signal)
|
|
132
|
-
if (context?.signal?.aborted) return aborted()
|
|
131
|
+
const { info, ok } = await getSession(target, context.signal);
|
|
132
|
+
if (context?.signal?.aborted) return aborted();
|
|
133
133
|
if (!ok || !info) {
|
|
134
|
-
return toolError(`Refusing: ${target} could not be read from the server.`)
|
|
134
|
+
return toolError(`Refusing: ${target} could not be read from the server.`);
|
|
135
135
|
}
|
|
136
|
-
return info
|
|
137
|
-
}
|
|
136
|
+
return info;
|
|
137
|
+
};
|
|
138
138
|
|
|
139
139
|
type PolicyHandler = (
|
|
140
140
|
target: string,
|
|
141
141
|
caller: string,
|
|
142
142
|
context: ToolCtx,
|
|
143
|
-
) => Promise<SessionInfoLike | undefined
|
|
143
|
+
) => Promise<SessionInfoLike | undefined>;
|
|
144
144
|
|
|
145
145
|
const policyHandlers: Readonly<Record<SessionControlPolicy, PolicyHandler>> = {
|
|
146
146
|
[SESSION_CONTROL_POLICY.descendants]: async (target, caller, context) => {
|
|
147
|
-
const result = await verifyDescendant(target, caller, context.signal)
|
|
148
|
-
if (context?.signal?.aborted) return aborted()
|
|
149
|
-
if (result.ownership === OWNERSHIP.owned) return result.info
|
|
147
|
+
const result = await verifyDescendant(target, caller, context.signal);
|
|
148
|
+
if (context?.signal?.aborted) return aborted();
|
|
149
|
+
if (result.ownership === OWNERSHIP.owned) return result.info;
|
|
150
150
|
if (result.ownership === OWNERSHIP.foreign) {
|
|
151
151
|
return toolError(
|
|
152
152
|
`Refusing: ${target} is not a descendant of the calling session (${caller}).`,
|
|
153
|
-
)
|
|
153
|
+
);
|
|
154
154
|
}
|
|
155
155
|
return toolError(
|
|
156
156
|
`Refusing: ${target} could not be verified through the server's session tree. Run ${namespace}_${TOOL.list} and use an ID it reports.`,
|
|
157
|
-
)
|
|
157
|
+
);
|
|
158
158
|
},
|
|
159
159
|
[SESSION_CONTROL_POLICY.allExceptAncestors]: async (target, caller, context) => {
|
|
160
|
-
const info = await readAuthorizedTarget(target, context)
|
|
161
|
-
const relation = await findAncestor(target, caller, context.signal)
|
|
162
|
-
if (context?.signal?.aborted) return aborted()
|
|
160
|
+
const info = await readAuthorizedTarget(target, context);
|
|
161
|
+
const relation = await findAncestor(target, caller, context.signal);
|
|
162
|
+
if (context?.signal?.aborted) return aborted();
|
|
163
163
|
if (relation === "ancestor") {
|
|
164
164
|
return toolError(
|
|
165
165
|
`Refusing: ${target} is an ancestor of the calling session (${caller}); sessionControlPolicy is all-except-ancestors.`,
|
|
166
|
-
)
|
|
166
|
+
);
|
|
167
167
|
}
|
|
168
168
|
if (relation === "unknown") {
|
|
169
169
|
return toolError(
|
|
170
170
|
`Refusing: ${target} could not be verified through the server's session tree. Run ${namespace}_${TOOL.list} and use an ID it reports.`,
|
|
171
|
-
)
|
|
171
|
+
);
|
|
172
172
|
}
|
|
173
|
-
return info
|
|
173
|
+
return info;
|
|
174
174
|
},
|
|
175
175
|
[SESSION_CONTROL_POLICY.all]: (target, _caller, context) =>
|
|
176
176
|
readAuthorizedTarget(target, context),
|
|
177
|
-
}
|
|
177
|
+
};
|
|
178
178
|
|
|
179
179
|
const policyHandlerMap: ReadonlyMap<SessionControlPolicy, PolicyHandler> = new Map(
|
|
180
180
|
Object.entries(policyHandlers) as Array<[SessionControlPolicy, PolicyHandler]>,
|
|
181
|
-
)
|
|
181
|
+
);
|
|
182
182
|
|
|
183
183
|
const authorize = async (
|
|
184
184
|
target: string,
|
|
185
185
|
context: ToolCtx,
|
|
186
186
|
): Promise<SessionInfoLike | undefined> => {
|
|
187
|
-
const caller = context?.sessionID
|
|
188
|
-
if (context?.signal?.aborted) return aborted()
|
|
187
|
+
const caller = context?.sessionID;
|
|
188
|
+
if (context?.signal?.aborted) return aborted();
|
|
189
189
|
if (caller && target === caller) {
|
|
190
|
-
return toolError("Refusing: a session cannot control itself.")
|
|
190
|
+
return toolError("Refusing: a session cannot control itself.");
|
|
191
191
|
}
|
|
192
192
|
if (!caller) {
|
|
193
193
|
return toolError(
|
|
194
194
|
"Refusing: the calling session is unknown, so ownership of the target cannot be verified.",
|
|
195
|
-
)
|
|
195
|
+
);
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
-
const handler = policyHandlerMap.get(policy)
|
|
198
|
+
const handler = policyHandlerMap.get(policy);
|
|
199
199
|
if (!handler) {
|
|
200
200
|
return toolError(
|
|
201
201
|
`Refusing: unknown sessionControlPolicy "${String(policy)}"; the target was not authorized.`,
|
|
202
|
-
)
|
|
202
|
+
);
|
|
203
203
|
}
|
|
204
|
-
return handler(target, caller, context)
|
|
205
|
-
}
|
|
204
|
+
return handler(target, caller, context);
|
|
205
|
+
};
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
208
|
* Read-only resolution for `usage`. Reading is intentionally unrestricted by
|
|
@@ -213,24 +213,24 @@ export const createAccess = ({ session, limits, policy, namespace }: AccessDeps)
|
|
|
213
213
|
context: ToolCtx,
|
|
214
214
|
): Promise<SessionInfoLike | undefined> => {
|
|
215
215
|
if (context?.signal?.aborted) {
|
|
216
|
-
return toolError("Aborted before usage was read.")
|
|
216
|
+
return toolError("Aborted before usage was read.");
|
|
217
217
|
}
|
|
218
|
-
const { info, ok } = await getSession(target, context.signal)
|
|
218
|
+
const { info, ok } = await getSession(target, context.signal);
|
|
219
219
|
if (context?.signal?.aborted) {
|
|
220
|
-
return toolError("Aborted before usage was read.")
|
|
220
|
+
return toolError("Aborted before usage was read.");
|
|
221
221
|
}
|
|
222
222
|
if (!ok || !info) {
|
|
223
|
-
return toolError(`Usage unavailable for ${target}: session could not be read.`)
|
|
223
|
+
return toolError(`Usage unavailable for ${target}: session could not be read.`);
|
|
224
224
|
}
|
|
225
|
-
return info
|
|
226
|
-
}
|
|
225
|
+
return info;
|
|
226
|
+
};
|
|
227
227
|
|
|
228
228
|
return {
|
|
229
229
|
getSession,
|
|
230
230
|
verifyDescendant,
|
|
231
231
|
authorize,
|
|
232
232
|
readTarget: readUsageTarget,
|
|
233
|
-
}
|
|
234
|
-
}
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
235
|
|
|
236
|
-
export type Access = ReturnType<typeof createAccess
|
|
236
|
+
export type Access = ReturnType<typeof createAccess>;
|