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