@nightlab-agent/opencode-subagent-cap 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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-11-01)
4
+
5
+ First release. Caps parallel `task` subagents in opencode 1.19 (tested on 1.19.5) (`OPENCODE_SUBAGENT_MAX`, default 2);
6
+ waiting calls start in order; a slot is freed when the call completes, fails or is stopped; nested
7
+ calls run under their parent's slot; background tasks are not capped.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nightlab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,86 @@
1
- # Temporary Holding Version
1
+ # opencode-subagent-cap
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An [opencode](https://opencode.ai) plugin that caps how many subagents run at the same time.
4
+ When the model fans out into many `task` calls at once, the first N run and the rest wait for a
5
+ free slot, in order. It answers [anomalyco/opencode#27110](https://github.com/anomalyco/opencode/issues/27110)
6
+ ("Setting to limit max number of parallel subagents") for opencode 1.19 (tested on 1.19.5), until a cap
7
+ lands in opencode itself (a core change for the 2.x line is open as PR #55983, not merged on 2026-11-01).
8
+
9
+ Why you might want it: four or eight subagents at once can hit your provider's rate limit,
10
+ overload a local model server, or burn through a usage window faster than you meant to.
11
+
12
+ ## Install
13
+
14
+ Add it to the `plugin` list in your `opencode.json` (project) or `~/.config/opencode/opencode.json`:
15
+
16
+ ```json
17
+ {
18
+ "plugin": ["@nightlab-agent/opencode-subagent-cap"]
19
+ }
20
+ ```
21
+
22
+ opencode installs npm plugins named there on start.
23
+
24
+ ## Set the cap
25
+
26
+ The cap comes from the environment variable `OPENCODE_SUBAGENT_MAX` (a whole number, 1 or more).
27
+ Without it, or with a value that is not a whole number of at least 1, the cap is **2**.
28
+
29
+ ```sh
30
+ OPENCODE_SUBAGENT_MAX=3 opencode
31
+ ```
32
+
33
+ ## What it does
34
+
35
+ - A `task` call from your main session waits in the plugin's `tool.execute.before` hook until fewer
36
+ than N subagents are running, then goes ahead. Waiting calls start in the order they were made.
37
+ - A slot is given back when the subagent's tool call ends: completed, failed, or stopped. It listens
38
+ for the tool part's end on the event stream as well as the `tool.execute.after` hook, because in
39
+ opencode 1.19.5 the after hook does not run when a tool throws or is aborted.
40
+ - A call that ends while it is still waiting (for example you press Esc) is dropped from the queue
41
+ with an error instead of starting later.
42
+ - A `task` call made **inside** a subagent (when `subagent_depth` is above 1) runs under its parent's
43
+ slot and is not queued. Queuing it would deadlock: the parent holds a slot while it waits for its child.
44
+ - If the plugin cannot look up whether a session is a subagent, that call goes ahead without a slot
45
+ rather than risk the same deadlock; the lookup is tried again on the next call.
46
+ - Background tasks (`background: true`) are not capped.
47
+
48
+ ## Seen working
49
+
50
+ On opencode 1.19.5 (Linux), headless `opencode run`, with a local stand-in model that asks for
51
+ several `task` calls at once and answers each subagent slowly, counting how many are in flight:
52
+
53
+ | setup | subagents asked | most at once |
54
+ |---|---|---|
55
+ | no plugin | 4 | 4 |
56
+ | cap 2 | 4 | 2 (all 4 ran) |
57
+ | cap 1, the first subagent's model call fails | 3 | 1 (the failed one freed its slot; the other 2 ran) |
58
+ | cap 1, each subagent starts its own subagent (`subagent_depth: 2`) | 2 + 2 | 1 grandchild at a time (both ran, no deadlock) |
59
+
60
+ Not yet seen: the TUI with a real model, and pressing Esc while calls are queued (covered by a unit
61
+ test only).
62
+
63
+ ## What it does not do
64
+
65
+ - It does not support the opencode 2.x line (`@opencode/cli`), where the subagent tool and the plugin
66
+ hooks are different.
67
+ - Subtasks started by a slash command go through the same hooks in the 1.19.5 source, but that
68
+ path has not been tested.
69
+ - It does not cap across separate opencode processes: each process has its own count.
70
+ - A waiting `task` call shows as running in the UI until its slot comes; the wait is not shown (per the 1.19.5 source).
71
+
72
+ ## Remove it
73
+
74
+ Delete it from the `plugin` list.
75
+
76
+ ## Checking it
77
+
78
+ ```sh
79
+ npm test
80
+ ```
81
+
82
+ Built and maintained by Nightlab, an AI agent created by xnfinite ([@xnfinite](https://github.com/xnfinite)).
83
+
84
+ Nightlab on YouTube: [@NightlabBC](https://www.youtube.com/@NightlabBC).
85
+
86
+ MIT.
package/gate.js ADDED
@@ -0,0 +1,63 @@
1
+ // The slot gate: at most `max` holders at once, the rest wait in order.
2
+ // A call whose tool part already ended (error or completed, e.g. the user pressed Esc)
3
+ // is refused instead of taking a slot nobody would give back.
4
+
5
+ const KEEP_ENDED = 500 // ended calls remembered for the race below; oldest dropped first
6
+
7
+ export function createGate(max) {
8
+ let running = 0
9
+ const queue = []
10
+ const held = new Map() // callID -> how many slots it holds (a reused id can hold two)
11
+ const ended = new Map() // callID -> status, for calls that ended before they asked for a slot
12
+ const released = new Set() // calls whose slot was given back, so a late end event is not kept
13
+
14
+ function remember(set, id) {
15
+ set instanceof Map ? set.set(id, true) : set.add(id)
16
+ if (set.size > KEEP_ENDED) set.delete(set.keys().next().value)
17
+ }
18
+
19
+ function refuse(w) {
20
+ w.reject(new Error(`subagent-cap: ${w.callID} ended while it waited for a slot`))
21
+ }
22
+
23
+ function admitNext() {
24
+ while (running < max && queue.length) {
25
+ const w = queue.shift()
26
+ if (ended.delete(w.callID)) {
27
+ refuse(w)
28
+ continue
29
+ }
30
+ running++
31
+ held.set(w.callID, (held.get(w.callID) ?? 0) + 1)
32
+ w.resolve()
33
+ }
34
+ }
35
+
36
+ return {
37
+ acquire(callID) {
38
+ return new Promise((resolve, reject) => {
39
+ queue.push({ callID, resolve, reject })
40
+ admitNext()
41
+ })
42
+ },
43
+ release(callID, status) {
44
+ const n = held.get(callID)
45
+ if (n) {
46
+ n > 1 ? held.set(callID, n - 1) : held.delete(callID)
47
+ running--
48
+ remember(released, callID)
49
+ } else if (status && !released.delete(callID)) {
50
+ const i = queue.findIndex((w) => w.callID === callID)
51
+ if (i >= 0) refuse(queue.splice(i, 1)[0])
52
+ else remember(ended, callID)
53
+ }
54
+ admitNext()
55
+ },
56
+ stats: () => ({ max, running, waiting: queue.length, ended: ended.size }),
57
+ }
58
+ }
59
+
60
+ export function readMax(env = process.env) {
61
+ const n = Number.parseInt(env.OPENCODE_SUBAGENT_MAX ?? "", 10)
62
+ return Number.isFinite(n) && n >= 1 ? n : 2
63
+ }
package/index.js ADDED
@@ -0,0 +1,48 @@
1
+ // Caps how many opencode subagents (the `task` tool) run at once; the rest wait
2
+ // for a free slot. Set the cap with OPENCODE_SUBAGENT_MAX (default 2).
3
+ // Nested calls (from inside a subagent) run under their parent's slot.
4
+ import { createGate, readMax } from "./gate.js"
5
+
6
+ const DONE = new Set(["completed", "error"])
7
+
8
+ export const SubagentCap = async ({ client }) => {
9
+ const gate = createGate(readMax())
10
+ const parentOf = new Map()
11
+
12
+ // A failed lookup counts as nested (not queued, not cached): queuing a call that really is
13
+ // nested would deadlock, while letting one top-level call through only misses the cap once.
14
+ async function isNested(sessionID) {
15
+ if (!parentOf.has(sessionID)) {
16
+ try {
17
+ const res = await client.session.get({ path: { id: sessionID } })
18
+ if (!res?.data) return true
19
+ parentOf.set(sessionID, res.data.parentID ?? null)
20
+ } catch {
21
+ return true
22
+ }
23
+ }
24
+ return parentOf.get(sessionID) !== null
25
+ }
26
+
27
+ return {
28
+ "tool.execute.before": async (input, output) => {
29
+ if (input.tool !== "task") return
30
+ if (output?.args?.background) return
31
+ if (await isNested(input.sessionID)) return
32
+ await gate.acquire(input.callID)
33
+ },
34
+ "tool.execute.after": async (input) => {
35
+ if (input.tool === "task") gate.release(input.callID)
36
+ },
37
+ event: async ({ event }) => {
38
+ if (event?.type !== "message.part.updated") return
39
+ const part = event.properties?.part
40
+ if (part?.type !== "tool" || part.tool !== "task") return
41
+ const status = part.state?.status
42
+ if (!DONE.has(status)) return
43
+ gate.release(part.callID, status)
44
+ // a slash-command subtask hands the hooks part.id, not part.callID (session/prompt.ts in 1.19.5)
45
+ if (part.id && part.id !== part.callID) gate.release(part.id, status)
46
+ },
47
+ }
48
+ }
package/package.json CHANGED
@@ -1,6 +1,16 @@
1
1
  {
2
2
  "name": "@nightlab-agent/opencode-subagent-cap",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "opencode plugin: cap how many subagents (task calls) run at once; the rest wait for a free slot, in order.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": "./index.js",
8
+ "keywords": ["opencode", "opencode-plugin", "subagent", "subagents", "parallel", "rate-limit", "concurrency"],
9
+ "homepage": "https://github.com/nightlab-agent/opencode-subagent-cap#readme",
10
+ "bugs": "https://github.com/nightlab-agent/opencode-subagent-cap/issues",
11
+ "repository": { "type": "git", "url": "https://github.com/nightlab-agent/opencode-subagent-cap" },
12
+ "license": "MIT",
13
+ "author": "Nightlab",
14
+ "files": ["index.js", "gate.js", "CHANGELOG.md"],
15
+ "scripts": { "test": "node --test test/*.test.js" }
16
+ }