@srobroek/pi-beads-enforcer 0.0.0-stage → 0.9.3

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/README.md CHANGED
@@ -1,3 +1,481 @@
1
- # Temporary Holding Version
1
+ # beads
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
+ [Beads](https://github.com/steveyegge/beads) (`bd`) issue tracking for OpenCode V2.
4
+ Ported from the Oh My Pi `beads` plugin in
5
+ [omp-plugins](https://github.com/srobroek/omp-plugins).
6
+
7
+ ## What it does
8
+
9
+ On every `shell` call (`tool.hook("execute.before")`), in this order, with one
10
+ 25-second budget for the whole call:
11
+
12
+ 1. Binary Git hooks: `bd hooks install` is refused and `bd init` gains
13
+ `--skip-hooks` when an existing hook is a binary executable.
14
+ 2. Gate admission: a mutating `bd` command waits for its store's automatic gates
15
+ to be verified (`bd gate list`, plus `bd gate check` when one could resolve).
16
+ 3. An unparseable command is refused.
17
+ 4. `bd edit` is refused: it opens `$EDITOR` and hangs the session.
18
+ 5. `bd update --status closed|done` is refused in favour of `bd close --reason`.
19
+ 6. `bd close` on a gate bead is refused (looked up with `bd show`).
20
+ 7. `bd unclaim` without `--if-assignee` is refused.
21
+ 8. A subagent command that clears or blanks the injected actor gets an advisory in the
22
+ tool result. A main session chooses its own actor and gets none.
23
+ 9. The plugin refuses a `bd` write (any verb the write lock treats as one, such as `bd create` or `bd heartbeat`) when the ledger belongs to a different git repository
24
+ than the command's directory. Reads, `bd init` and `bd bootstrap` pass. So does a command
25
+ that assigns `BEADS_DIR=` itself (`BEADS_DIR=/path bd create ...` or an earlier
26
+ `export BEADS_DIR=...`), and so does the project's `options.store`. A `BEADS_DIR`
27
+ that only arrives through the environment is not a choice. One example is a variable
28
+ inherited from the shell that started a shared OpenCode server. `-C`, `--db`,
29
+ `--database` and `--global` name their own target and pass. The Claude Code hook has
30
+ no `options.store`, so a ledger in another repository needs the literal `BEADS_DIR=`
31
+ there. A `bd` the gate cannot place (behind `exec`, `env`, `xargs`, a shell string or a
32
+ substitution, quoted or not) is refused too while the default ledger is foreign, because it
33
+ may be a write. The same holds for a command that defines an `alias` or a shell function.
34
+ It also holds for a command name spelled with quotes or a backslash (`"b"d`, `\bd`).
35
+ Run each `bd` as its own simple command. The `bd-embedded-write-lock` gate controls this check. An unquoted `bd` operand of any
36
+ other command counts as a launch, so the write lock fails closed on `flock X bd create`.
37
+ A quoted `'bd'` operand names it and passes: `git diff -- 'bd'`.
38
+
39
+ Threat model: this gate stops a ledger inherited by accident, such as a `BEADS_DIR`
40
+ that leaked from the shell that started the server. It does not stop deliberate shell
41
+ obfuscation. Out of scope are `eval`, indirect expansion, sourced scripts, aliases
42
+ defined outside the command and unknown wrapper binaries. The real fix is a clean
43
+ server environment.
44
+ Under a foreign ledger, a `bd` that follows a recognised wrapper (`flock`, `taskset`,
45
+ `timeout`, `sudo`, `xargs` and the like) is refused without parsing the wrapper's
46
+ arguments. A literal `BEADS_DIR=<path>` on the command line is the override.
47
+ 10. `gh pr create` without a `Bead:`, `Closes-Bead:` or `Bead-Id:` trailer is
48
+ refused in a repository with a live ledger that you can write to. The gate does not
49
+ decode shell: it reads one exact form (`gh pr create` or `gh pr new` alone, with one
50
+ `--body` or a relative regular `--body-file`) and refuses any other command that may
51
+ reach `gh pr create`, such as a chain, a wrapper, `env -S`, a `$'...'` word, a short-flag
52
+ cluster, `--fill`, or an alias. The refusal prints the accepted form.
53
+ 11. Each `bd` write to an embedded store is rewritten to run under
54
+ `bd-embedded-write-runner`, which holds the store's write lock for exactly the
55
+ life of the `bd` process. The lock files are shared with the omp plugin.
56
+ 12. Every shell call of a subagent session starts with
57
+ `unset BEADS_ACTOR; export BD_ACTOR='opencode/<root-session>/<agent>/<session>'`.
58
+ A main session gets no prefix. The prefix is fixed text, not a parse of the command:
59
+ anything the command sets later wins through shell order and bd's precedence
60
+ (`--actor`, then `BEADS_ACTOR`, then `BD_ACTOR`). The actor gate only warns when a
61
+ command ends with no actor. Claim tooling should match an actor on its final
62
+ `/`-separated segment, the session id; the root session and agent name are context.
63
+
64
+ A refusal is thrown, so the model sees it as the tool error.
65
+
66
+ `shell.hook("create.before")` sets `BEADS_DIR` (the checkout's `.beads`, or the
67
+ primary checkout's from a linked worktree) and clears `BEADS_DOLT_SHARED_SERVER`
68
+ in the child environment. An explicitly set `BEADS_DIR` is left alone, and it is the default ledger for
69
+ claim tracking: a plain claim is tracked under that store (see Claim tracking).
70
+ The pin also sets `HARNESS_BEADS_PIN` to the same path. A process can inherit a pinned
71
+ shell's environment, for example an OpenCode server restarted from that shell. When the
72
+ two variables match, the plugin drops the inherited pin and pins each session to its own
73
+ checkout's store. A `BEADS_DIR` that the user sets in a pinned shell differs from the
74
+ marker, so the plugin keeps it. To select the marker's own path on purpose, clear the
75
+ marker as well: `unset HARNESS_BEADS_PIN; export BEADS_DIR=/path/.beads`. With
76
+ `beads-dir-pin` off, the hook still drops an inherited pin but sets no new one. Sessions
77
+ started before the upgrade keep the old marker until restarted.
78
+
79
+ Every gate judges a `bd` write at the store the write lock runs it in. The lock's
80
+ planner follows what the command does before the write:
81
+
82
+ - a `cd`
83
+ - an `export BEADS_DIR=...` or `unset BEADS_DIR`
84
+ - a command-local `BEADS_DIR=`
85
+
86
+ Gate admission, the close gate's `bd show` lookup and the write lock share one
87
+ resolution of that target, by the store's real path. The runner checks every write's
88
+ store path again just before `bd` starts.
89
+
90
+ The gates refuse these writes:
91
+
92
+ - A write whose store the shell chooses at run time, such as after
93
+ `export BEADS_DIR=$X`, even when no ledger is in reach.
94
+ - A write after an `export`, `unset` or `BEADS_DIR=` that may not run: behind `&&` or
95
+ `||`, or inside `if`, `case` or a loop.
96
+ - A write that the gates cannot read, such as inside `$(...)`, while a ledger is in reach.
97
+ - A write whose store changed between the check and the run: a link that moved, or a
98
+ directory replaced by a link to another store. The runner also stops when the store no
99
+ longer resolves. It stops before `bd` starts, and the command needs a second run.
100
+
101
+ The gates still let reads through in these cases.
102
+
103
+ `tool.hook("execute.after")` records successful claims per session in plugin
104
+ storage, confirming each claim from the claim command's own `--json` output (a claim
105
+ bd does not confirm that way is not tracked, with a warning), and
106
+ appends the contention-retry and stale-import advisories.
107
+
108
+ `session.hook("context")` adds `bd prime` to the system prompt. The text is read
109
+ once and frozen per session, so the prompt stays cacheable. A root session's frozen
110
+ text also carries its session-start advisories, once: a binary Git hook that Beads
111
+ modified, the verdict a detached `bd dolt push` left in `.beads/last-push.log`
112
+ (consumed when read), and an inherited `BEADS_DIR` that points at another
113
+ repository's store.
114
+
115
+ Only mutating `bd` commands wait for gate admission. A `subagent` spawn, a shell command
116
+ that writes no bd store, and every read or edit pass without waiting on the ledger.
117
+
118
+ ## Session lifecycle
119
+
120
+ The plugin follows `session.execution.*` events (`ctx.event.subscribe`). Every
121
+ message it sends is a `session.synthetic` with `metadata.source: "srobroek.beads"`.
122
+
123
+ - **Claim tracking.** The plugin tracks a claim only when it knows the ledger and the outcome.
124
+ It resolves the ledger once, in bd 1.3.1's order for store commands such as `bd update` and
125
+ `bd create`: `--db`, then `BEADS_DIR`, then the `.beads` found from the directory (the session
126
+ pin), then `BEADS_DB` only when no `.beads` is found. A `BEADS_DB` that is a `.beads` directory
127
+ names that store; any other value, such as a database file, selects the directory that holds
128
+ it. The plugin pins `BEADS_DIR` to the resolved store on its shells and its own `bd` calls, so
129
+ a later change to `BEADS_DB` cannot move a claim. Commands that skip bd's store init
130
+ (`bd where`, `bd dolt set`, `bd init`) use another order: `--db`, then `BEADS_DB`, then
131
+ `BD_DB`, then `BEADS_DIR`. The embedded write lock refuses such a write when `BEADS_DB` or
132
+ `BD_DB` is set or appears in the command, unless `--db` names the store. An inherited
133
+ `BD_DB`, `BD_SOCKET` or `BEADS_DOLT_*`/`BEADS_PROXIED_SERVER_*` variable leaves the claim
134
+ untracked, because bd's store commands do not read `BD_DB` as a store path and the others
135
+ name a server. A command that overrides it is not tracked: a ledger selector anywhere
136
+ in it (a word starting `-C`, `--db`, `--database`, `--directory`, `--global` or `--dolt-`),
137
+ or a `BEADS_*`/`BD_*` assignment other than the actor. The one exception is an inline
138
+ assignment of a ledger variable to the plugin's own value, compared as canonical paths
139
+ (symlinks resolved, trailing slash ignored). The invocation must be one plain `bd` command (no
140
+ `;`, `&&`, `||`, pipe, subshell, substitution, wrapper, embedded-write runner, quoted
141
+ assignment, background job or shell-computed word), and bd's own `--json` output must show the
142
+ claimed bead `in_progress` under an actor that invocation could have used. The plugin adds
143
+ `--json` to such a claim itself and says so, so the result is bd's JSON rather than a status
144
+ line. Any other claim is not tracked: no stop nudge, no release, no heartbeat, and the result
145
+ carries a one-line warning (variable names only, never values). Run the claim as its own simple
146
+ command to have it tracked. A missed heartbeat is the safe failure; a release of the wrong
147
+ ledger or actor is not. An assignment (`bd assign`, `--assignee`) never starts tracking a claim.
148
+ - **Stop nudge.** When a root session's execution succeeds while it holds tracked
149
+ claims, it gets one queued message listing each claim with its guarded release
150
+ command, and resumes. A real user prompt (`session.hook("prompt")`, not one from
151
+ this plugin or OpenCode's built-in goal) re-arms the nudge. Nothing happens when the
152
+ execution failed or was interrupted. While the built-in goal is active (the
153
+ `opencode.goal` RPC's `get`) the stop is deferred: the nudge runs when the goal ends
154
+ (`rpc.opencode.goal.changed`), unless a new execution starts first.
155
+ - **Release.** When the nudged session stops again still holding claims, they are
156
+ released in the background (at most 120 s): `bd show`, then
157
+ `bd unclaim <id> --if-assignee <holder>`, then, for a parked claim,
158
+ `bd update <id> --status blocked|deferred --if-status open --if-assignee ''`. A claim
159
+ bd no longer shows as the holder's is forgotten without a write. The session gets a
160
+ message, without resuming, naming what was released and what could not be verified.
161
+ - **Subagents.** A subagent's claims are released when it ends successfully or its
162
+ session is deleted. A failed or interrupted subagent keeps them so it can be resumed.
163
+ For a foreground `subagent` call the release runs in the
164
+ parent's `execute.after` (at most 25 s) and its report is appended to the tool
165
+ result. For a background call the child's end triggers the release, and the parent
166
+ gets the report as a message that does not wake it. The release also asks bd what the
167
+ subagent still holds: `bd list --assignee <its actor> --status in_progress --json` in
168
+ each store its shell calls ran against. A bead listed there joins the tracked claims, so
169
+ a claim made inside a function, script or pipeline is released too. A list that fails is
170
+ reported in the release message.
171
+ - **Lease heartbeat.** Each tracked claim with a known actor and store is renewed with
172
+ `bd heartbeat <id> --json` every 60 s while its session executes, under the
173
+ embedded write lock. Timers pause when an execution ends and resume (renewing at
174
+ once) when the next starts, so an idle session lets its leases lapse. A close, an
175
+ unclaim, a status other than `in_progress`, or `session.deleted` ends the lease. A
176
+ failed heartbeat ends it and tells the session once. Leases live in plugin storage
177
+ (`leases/<sessionID>`); timers are process-wide, so two plugin instances never renew
178
+ one lease twice, and each instance clears its own on cleanup.
179
+ - **Fork notice.** On OpenCode 2.3.0 and later, the experimental `session.fork` hook
180
+ tells a forked session which beads the original holds (at most 10, then "and N more"),
181
+ so it does not assume them. The fork inherits no claims; the original's claims and
182
+ leases are left alone. With no claims nothing is sent, and a host without the hook is
183
+ unaffected. Gated by `beads-session-advisories`.
184
+
185
+ ## Skills
186
+
187
+ Only `beads-guide` is listed to the model in OpenCode: it is a router whose body maps each
188
+ situation to the other beads skills, which set `opencode/autoinvoke: false` and load by id. One listing
189
+ entry instead of one per skill keeps the per-request skills listing short.
190
+
191
+ Registered through `ctx.skill.transform` from `skills/` (a user skill of the same
192
+ name wins): `beads-ledger`, `beads-evidence`, `beads-setup`,
193
+ `beads-contention-retry` and `beads-preflight` (a read-only `preflight.py`). They are
194
+ not exported to the Claude Code marketplaces, because they describe this
195
+ plugin's write lock and prime context.
196
+
197
+ ## Options
198
+
199
+ Every gate is on by default. Switch one off in the plugin options:
200
+
201
+ ```json
202
+ { "gates": { "pr-bead-link-gate": { "enabled": false } } }
203
+ ```
204
+
205
+ Gates: `beads-gate-admission`, `binary-git-hooks`, `bd-update-close-gate`,
206
+ `bd-close-gate`, `bd-unclaim-gate`, `bd-actor-gate`, `pr-bead-link-gate`,
207
+ `bd-embedded-write-lock`, `bd-editor-gate`, `beads-dir-pin`, `beads-prime`,
208
+ `beads-claim-tracking`, `beads-stop-nudge` (also the root-session release that
209
+ follows it), `beads-claim-release`, `bd-lease-heartbeat`, `beads-session-advisories`,
210
+ `beads-skills`. The lifecycle and the heartbeat read the claims that
211
+ `beads-claim-tracking` records.
212
+
213
+ ### Busy-ledger wait
214
+
215
+ An embedded-store write that finds the write lock held waits for its turn, polling
216
+ with exponential backoff and jitter, for `embeddedWriteWaitMs` (default `30000`)
217
+ before it is refused. The refusal says the ledger is busy and to retry the command
218
+ as its own simple command; it only advises removing the lock when the holder is gone.
219
+
220
+ ```json
221
+ { "embeddedWriteWaitMs": 60000 }
222
+ ```
223
+
224
+ ## Process-global state
225
+
226
+ Some state lives on `globalThis` so every instance of the plugin in one process shares
227
+ it: the lifecycle (`srobroek.beads.lifecycle`), the heartbeat timers
228
+ (`srobroek.beads.heartbeats`) and the embedded write lock
229
+ (`com.srobroek.beads.embedded-write-lock.v1`). After a plugin update an older instance
230
+ can still be loaded, and it may have created that object before the new version
231
+ existed. The rules:
232
+
233
+ - The object must stay compatible across plugin versions in one process. Read it
234
+ as possibly old-shaped.
235
+ - A new field needs a backfill (`state.field ??= ...`) in the accessor, on the
236
+ existing object. Never replace the object, and never assume it has the new field.
237
+ - A new field needs a test that pre-seeds `globalThis` with the old shape, runs the
238
+ new code and checks that nothing throws.
239
+ - A lock key never changes and is never versioned. Two versions that used two keys
240
+ would each hold the same store, and embedded-store writes would stop being serialised.
241
+
242
+ ## Permissions
243
+
244
+ OpenCode evaluates shell permissions on the rewritten command. A write that runs
245
+ under the lock runner starts with `BEADS_DOLT_SHARED_SERVER= BEADS_DIR=... '<bun>'
246
+ '<plugin>/dist/bd-embedded-write-runner.js' ... -- bd ...`, so a rule that allows
247
+ `bd *` does not cover it. Add a rule for the runner if your shell permission is
248
+ not `allow`.
249
+
250
+ ## Requirements
251
+
252
+ `bd` and `bun` on `PATH` (or `BUN_INSTALL`, or mise). The runner is a Bun script.
253
+
254
+ ## Subagent routing (FO-5)
255
+
256
+ When the location has a store, a second `execute.before` hook checks each new
257
+ `subagent` call against delivery rule FO-5:
258
+
259
+ 1. It finds bead ids in the prompt. Ids that start with the ledger prefix (the
260
+ name of the store's repository directory) win. If there are none, a token
261
+ counts when its hash part contains a digit or it has a `.N` child suffix.
262
+ At most 8 ids are read.
263
+ 2. It reads them in one `bd show <ids> --json`, with a 3-second limit.
264
+ 3. If a named bead sets `metadata.execution_agent_type` and the spawn's agent
265
+ matches none of them, the call is refused. The refusal names each bead's agent
266
+ and its `execution_reasoning_effort`. A namespaced agent (`delivery:implementer`)
267
+ matches `implementer`.
268
+ 4. A spawn to `implementer-high` that no named bead asks for goes ahead, with an
269
+ advisory appended to the result: use `implementer` unless the brief states an
270
+ implementer-high criterion.
271
+
272
+ Orchestrator spawns are exempt, because an orchestrator names the many beads it
273
+ will delegate: a spawn to the `general` agent, or with tier `orchestrate` (or the
274
+ legacy `role: "orchestrate"`), skips the check and the bead lookup. Every other
275
+ spawn is judged as above. To switch the gate off for all spawns, set
276
+ `{ "gates": { "bead-agent-routing": { "enabled": false } } }`.
277
+
278
+ A resumed call (with `sessionID`), a prompt with no ids, a failed or slow `bd`
279
+ read, and a location without a store are all allowed. Gate name:
280
+ `bead-agent-routing`.
281
+
282
+ ## Install
283
+ Install the Pi package from npm:
284
+
285
+ ```sh
286
+ pi install npm:@srobroek/pi-beads-enforcer
287
+ ```
288
+
289
+ ```sh
290
+ opencode plugin add 'github:srobroek/harness-plugins::path:plugins/beads/opencode'
291
+ ```
292
+
293
+ ## Claude Code
294
+
295
+ The plugin is also in this repository's Claude Code marketplace. Install it with these
296
+ two commands:
297
+
298
+ ```text
299
+ /plugin marketplace add srobroek/harness-plugins
300
+ /plugin install beads@harness-plugins
301
+ ```
302
+
303
+ It ships the skills and the two hooks in `hooks/claude-hooks.json`. Both hooks run
304
+ `bun dist/claude-hook.js`:
305
+
306
+ - `SessionStart` adds `bd prime` and the session-start advisories as context, within
307
+ Claude Code's 10,000-character limit. It runs on startup, resume, clear and compact.
308
+ It also writes the store pin to `CLAUDE_ENV_FILE`: `BEADS_DIR` with its
309
+ `HARNESS_BEADS_PIN` marker, and an empty `BEADS_DOLT_SHARED_SERVER`. It drops an
310
+ inherited pin first, so `bd prime`, the advisories and the pin share one store. In a
311
+ directory without a store, it writes `unset BEADS_DIR HARNESS_BEADS_PIN`, so Bash
312
+ leaves the inherited ledger too.
313
+ - `PreToolUse` on `Bash` runs the shell gates above, with every gate on. It skips a
314
+ command that names none of `bd`, `git` and `gh`. A refusal denies the call. A rewrite replaces
315
+ `command`. An advisory becomes context. Every Bash command of a subagent, gated or not,
316
+ starts with
317
+ `unset BEADS_ACTOR; export BD_ACTOR='claude/<session>/<agent type>/<agent id>';`, so a script
318
+ it runs writes under that actor. A main session gets no injected actor and writes under the
319
+ actor its own environment or command chooses.
320
+
321
+ These parts stay OpenCode-only:
322
+
323
+ - claim tracking and lease heartbeats
324
+ - the stop nudge and guarded claim release
325
+ - subagent routing
326
+ - plugin options
327
+
328
+ Without `bun`, session start falls back to `bd prime` and no gate runs. A hook that fails
329
+ exits 0, so Claude Code carries on without it.
330
+
331
+ ## pi
332
+
333
+ `pi/extensions/beads.js` (source `pi-src/beads.ts`) is a pi extension. It runs the shell gates
334
+ above on every `bash` call with every gate on, and it adds three pi-only gates and the
335
+ `beads_preflight` tool. It injects no context: install
336
+ [`npm:pi-beads-companion`](https://www.npmjs.com/package/pi-beads-companion) beside it for
337
+ `bd prime` and the selected issue.
338
+
339
+ A refusal blocks the call with `<cause>; <fix>. (beads gate <name>)`. A call that passes
340
+ gets two prefixes:
341
+
342
+ - The store pin: `export` lines for `BEADS_DIR`, its `HARNESS_BEADS_PIN` marker and an empty
343
+ - The actor: `unset BEADS_ACTOR; export BD_ACTOR='pi/<root session>/.../<this session>';`.
344
+
345
+ Every pi session gets its own actor, the interactive root session included. pi runs a
346
+ foreground subagent inside the parent's process. The process environment therefore cannot
347
+ tell the child's writes from the parent's, but each session's `parentSession` header can. The
348
+ actor lists the session ids from the root down. A `--actor` flag or `BEADS_ACTOR=` in the
349
+ command still wins. An embedded-store write runs under `bd-embedded-write-runner.js` beside
350
+ the extension, which needs `bun` on `PATH`.
351
+
352
+ ### Gates only pi has
353
+
354
+ The OpenCode plugin and the Claude Code hook do not run these three gates.
355
+
356
+ 1. `bd-claim-gate`: these commands need each bead they name to be `in_progress` under this
357
+ session's actor or under a descendant's:
358
+
359
+ - `bd close` and `bd done`
360
+ - `bd update` without `--claim`
361
+ - `bd comment` and `bd comments add`
362
+
363
+ `pi/<root>/<child>` descends from `pi/<root>`. A parent can therefore close the bead its
364
+ child claimed, but the child cannot close the parent's. Name the ids as literal words. The
365
+ gate reads the claim with `bd show --json` from the store the write lands in. It refuses
366
+ the command when bd cannot answer.
367
+ 2. `bd-close-evidence-gate`: a close needs `--reason` (or `-r`, or a readable `--reason-file`)
368
+ that names checkable evidence, as `rule://beads-evidence` asks. The gate accepts any of
369
+ these:
370
+
371
+ - a backticked command, or a tool command such as `bun test x.test.ts` (a git command
372
+ counts when it names git, or a wrapper listed in `gitWrappers`)
373
+ - a path or a file name
374
+ - a commit SHA, a PR or issue reference, or a URL
375
+ - a test count or an exit code
376
+
377
+ A reason such as `done` or `N/A` fails this gate.
378
+
379
+ pi passes no options to an extension, so this gate reads `gitWrappers` from `beads.json`
380
+ in pi's agent directory (`~/.pi/agent/beads.json`, or the same file under
381
+ `PI_CODING_AGENT_DIR`): `{ "gitWrappers": ["git", "<wrapper>"] }`. The default is
382
+ `["git"]` and a list replaces it. It is the only option pi reads; an unreadable or
383
+ malformed file leaves only `git` and warns at session start.
384
+ 3. `write-claim-gate`: in a pi-subagents child, `edit` and `write` need a claim. The rule
385
+ covers a file in a checkout with a Beads store: the child must hold at least one
386
+ `in_progress` claim in that store. The gate asks
387
+ `bd list --assignee <actor> --status in_progress`. A confirmed claim counts for 30 seconds,
388
+ and the gate asks again after any `bd` write. The gate leaves the interactive root session
389
+ and its `/fork` or `/clone` copies alone. A session counts as a child in two cases:
390
+
391
+ - it runs in a detached pi-subagents runner (`PI_SUBAGENT_CHILD=1`)
392
+ - its session file sits under its parent's pi-subagents session directory.
393
+
394
+ ### `beads_preflight`
395
+
396
+ The tool runs the checks of the `beads-preflight` skill and returns its JSON report:
397
+ `{ ok, summary, checks: [{ id, status, detail, fix }] }`. Its parameters follow the script's
398
+ flags: `only` (a list of check ids), `include_slow`, `timeout` (seconds) and `apply`. The
399
+ checks see what the session's `bash` calls see, including its store pin and actor. The logic
400
+ is `shared/preflight.ts`, a port of `preflight.py`. OpenCode and Claude Code still run the
401
+ Python script, so a change to either copy must reach the other.
402
+
403
+ ### Subagents
404
+
405
+ A foreground pi-subagents child loads none of the parent's extensions. List this extension
406
+ for every child in pi's `settings.json` (user `~/.pi/agent/settings.json` or the project's
407
+ `.pi/settings.json`):
408
+
409
+ ```json
410
+ {
411
+ "subagents": {
412
+ "defaultSubagentOnlyExtensions": ["/absolute/path/to/beads/pi/extensions/beads.js"]
413
+ }
414
+ }
415
+ ```
416
+
417
+ An agent whose frontmatter sets `subagentOnlyExtensions` replaces this default, so add the
418
+ path to that list as well. A background child can load the extension twice, through ambient
419
+ discovery and through this list. In that case the second copy skips any call that the first
420
+ copy already gated. See the `extensions` and `subagentOnlyExtensions` fields in the
421
+ [pi-subagents agent docs](https://github.com/nicobailon/pi-subagents/blob/main/docs/agents.md).
422
+
423
+ These parts stay OpenCode-only:
424
+
425
+ - claim tracking and lease heartbeats
426
+ - the stop nudge and guarded claim release
427
+ - subagent routing (`bead-agent-routing`) and the fork notice
428
+ - session-start advisories and the plugin options (pi reads only `gitWrappers`, above)
429
+
430
+ # Project ledger store
431
+
432
+ Set `options.store` when the project's ledger lives outside its checkout:
433
+
434
+ ```json
435
+ {
436
+ "$schema": "https://opencode.ai/config.json",
437
+ "plugins": [
438
+ {
439
+ "package": "@srobroek/opencode-beads",
440
+ "options": { "store": "/absolute/path/to/ledger/.beads" }
441
+ }
442
+ ]
443
+ }
444
+ ```
445
+
446
+ Relative store paths resolve from OpenCode's project root
447
+ (`location.project.directory`), not from the config file or a shell call's
448
+ working directory. The plugin resolves symlinks and requires a directory with
449
+ `metadata.json` or `embeddeddolt/`. For invalid values the plugin installs refusal
450
+ hooks that block shell tools, shell creation, dispatch and session context with
451
+ an `Invalid beads store option` error. It runs no internal `bd` commands.
452
+ Fix the config before running ledger writes.
453
+
454
+ For ordinary `bd` calls, precedence is a literal command-local `BEADS_DIR=`,
455
+ then `options.store`, then the server's `BEADS_DIR`, then checkout discovery.
456
+ Explicit database/directory CLI flags retain their existing semantics.
457
+ The configured store supplies `BEADS_DIR` to shell children and every internal
458
+ `bd` command, including admission checks and `bd prime`. This explicit selection
459
+ also applies when the automatic `beads-dir-pin` gate is disabled. The write
460
+ runner uses the canonical store path, so projects and worktrees sharing a ledger
461
+ use its same `omp-embedded-write.lock` file.
462
+
463
+ The plugin skips an inherited pin in the server's environment, as
464
+ `shell.hook("create.before")` describes. The close gate's `bd show` lookup also
465
+ uses a command-local `BEADS_DIR=`.
466
+
467
+ OpenCode V2 searches project config through all ancestors, including above a Git
468
+ root. Either `opencode.json` or `.opencode/opencode.json` works; all discovered
469
+ `.opencode` configs take precedence over direct configs. Plugin arrays are
470
+ processed from lower to higher precedence. In the current V2 implementation,
471
+ a later entry with the **same package target** replaces its earlier options;
472
+ it does not deep-merge them. Repeat any global gate options you want to retain,
473
+ and match the global target exactly, including its version or Git specification.
474
+ Different targets exporting the same plugin ID can be reported as duplicates,
475
+ with the first instance retained. Use the package target, not `srobroek.beads`,
476
+ to supply options: an ID entry only enables the plugin.
477
+
478
+ Sources: [V2 config](https://opencode.ai/v2/docs/config),
479
+ [V2 plugins](https://opencode.ai/v2/docs/plugins), and
480
+ [`PluginSupervisor.resolve`](https://github.com/anomalyco/opencode/blob/dev/packages/core/src/plugin/supervisor.ts)
481
+ with [`PluginModule.load`](https://github.com/anomalyco/opencode/blob/dev/packages/core/src/plugin/module.ts).