@srobroek/pi-autopilot 0.0.0-stage → 1.4.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,415 @@
1
- # Temporary Holding Version
1
+ # autopilot
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
+ Runs an OpenCode or pi session unattended. `/autopilot` asks its setup
4
+ questions in one round and then arms the session. After that, the session asks
5
+ no more questions. The session records decisions and blockers in a journal. A
6
+ 4-minute tick keeps the session moving. The run stops on an explicit or an
7
+ implicit condition and leaves a debrief and a handoff bead. The sections below
8
+ describe OpenCode; [pi](#pi) lists what differs there.
9
+
10
+ ## Commands
11
+
12
+ | Command | Does |
13
+ | --- | --- |
14
+ | `/autopilot [scope] [flags]` | Starts a run. The scope is an epic or bead id, a goal in plain words, or nothing for the `bd ready` queue. |
15
+ | `/autopilot resume [run-id]` | Arms an unfinished run again in this session, from its latest state file. |
16
+ | `/autopilot review [run-id]` | Loads a stopped run's summary and asks one question per needs-input item. |
17
+ | `/autopilot stop` | Stops the run in this session and writes the debrief. |
18
+
19
+ Flags:
20
+
21
+ - `--full` or `--no-full`
22
+ - `--allow merge,deploy,self-review` or `--allow none`
23
+ - `--lanes N`
24
+ - `--for 3h` or `--until-time 18:00`
25
+ - `--until "<condition>"`, or `--until ""` for no condition
26
+
27
+ `--allow self-review` (default off, recommended no) lets a merge rest on the
28
+ plugin-run review instead of a GitHub approval; see "Plugin-run review" below.
29
+
30
+ The setup round asks for each value that the command-line flags leave out.
31
+ `--allow merge`, `--allow deploy` and `--full` govern what the agent decides to
32
+ do on its own and what the plugin itself does. Only `--allow merge` reaches the
33
+ permission hook, and only for `autopilot_merge`. The
34
+ number of parallel write lanes has no upper limit. The setup round recommends 3
35
+ lanes, and `--lanes N` skips that question. Read-only subagents, such as
36
+ `explore` and the reviewers, take no lane.
37
+
38
+ Only the user starts a run. OpenCode does not load the `autopilot` skill on its
39
+ own, and only the command arms the hooks.
40
+
41
+ ## Rules while a run is armed
42
+
43
+ The plugin enforces these rules for the lead session and every subagent under
44
+ it:
45
+
46
+ - The plugin denies these tools:
47
+ - `question`
48
+ - `parley_ask`
49
+ - `parley_wait`
50
+ - `parley_end`
51
+ - When a turn ends with a question to the user, the plugin re-prompts the
52
+ session to decide, log the decision and continue. After two re-prompts in a
53
+ row, the plugin logs an issue and waits for the next tick.
54
+ - The plugin denies every permission `ask` and journals it as a blocker, with
55
+ its permission kind and the command or pattern it asked about. Your own
56
+ OpenCode permission rules already allow what you trust, and an `ask` is what
57
+ you wanted to be asked about. Nobody is there to answer it. Two asks are
58
+ exceptions, and only an `ask` is ever changed:
59
+ - An `external_directory` ask for exactly `<root>/*`. At a tick, the plugin
60
+ records the directory of each writer lane (not `explore`, the reviewers,
61
+ `debug` or another read-only agent) when it is a linked, non-primary
62
+ worktree that `worktree.list` shows for this project. It stores the real
63
+ path with the device and inode. When the ask arrives, the plugin checks
64
+ that the path is still a plain directory, not a symlink, with the same
65
+ device, inode and real path, and that the project still lists it. No
66
+ wildcard may appear in `<root>`. A symlink alias of the root matches. A
67
+ sibling, a subdirectory, a symlink leading elsewhere, or a root that was
68
+ removed and recreated does not. A lane is recorded at the first tick after
69
+ it starts.
70
+ - An `autopilot_merge` ask for one PR and SHA, under `--allow merge`.
71
+ - An `external_directory` ask for `<dir>/*`, where `<dir>` is the one export
72
+ directory the run made last (`review/<head>/<manifest hash>/`, see
73
+ `autopilot_export_review`) or a directory under it. The plugin records
74
+ that export in its storage, compares real paths, and requires the
75
+ directory to be a plain directory, not a symlink. A wildcard, a `..`
76
+ climb, a sibling, `review/` itself, an older export and a symlink leading
77
+ outside do not match. The allow is for the directory, not for reads: the
78
+ ask carries no read/write distinction, and where the user's rules allow
79
+ `edit` for a path, this ask is the only prompt in the way. The directory
80
+ is model-writable. What a reviewer reads is trusted only through the
81
+ export's manifest hash, not through this permission.
82
+ Shell, edit, MCP and deploy asks stay denied.
83
+ - After compaction, the plugin queues a notice with the rules and a fresh
84
+ status.
85
+
86
+ The plugin keeps armed runs in its storage, so a run survives a server restart.
87
+ Every state change holds a lease file in the run directory, so two plugin
88
+ instances take turns. The lease does not decide which write wins. Each state
89
+ write creates a new numbered file, `state.<N>.json`, and never replaces an
90
+ existing one. When two writers race for the same number, one of them wins and
91
+ the other fails, and the losing call starts again from the latest state. So a
92
+ second plugin instance, or a writer that stalled and lost its lease, never
93
+ overwrites a newer state. When a run file is missing, the plugin treats it as absent. When
94
+ a run file is unreadable or corrupt, the plugin reports the error and logs a
95
+ blocker. The plugin never reads such a file as an empty run.
96
+
97
+ Each tick carries a token and the session that the run is bound to. After
98
+ `/autopilot resume`, the old automation's ticks are stale. They do nothing and
99
+ end that automation, and the resume prompt names the old automation for
100
+ deletion.
101
+
102
+ A stop first moves the run to `stopping`, and the plugin then writes
103
+ `summary.md` and the handoff bead. If either write fails, the run stays
104
+ `stopping`. The next tick or status call retries the missing one. The plugin
105
+ looks up the handoff bead by title before it creates one, so it never creates
106
+ two. When that lookup fails, the plugin creates nothing and retries later.
107
+
108
+ ## Tools
109
+
110
+ `autopilot_status` rebuilds the run state with no model. It reads these
111
+ sources:
112
+
113
+ - `bd`
114
+ - git
115
+ - child sessions
116
+ - the journal
117
+
118
+ The tool then evaluates the stop conditions. It returns JSON, the ping line and
119
+ the actions due. Its actions are:
120
+
121
+ - `status`
122
+ - `arm`
123
+ - `tick`
124
+ - `fallback`
125
+ - `stop`
126
+
127
+ Code Mode can call `autopilot_status`, and the tick automation calls it.
128
+
129
+ `autopilot_log` appends one validated journal entry. Each entry has one of
130
+ these types:
131
+
132
+ - decision
133
+ - blocker
134
+ - issue
135
+ - ambiguity
136
+ - stop
137
+ - progress
138
+ - action
139
+
140
+ Without `--full`, `autopilot_log` also labels a blocked bead `human`.
141
+
142
+ `autopilot_export_review({ pr, base, head })` prepares a review round for
143
+ reviewers that cannot run git while the run is armed. `pr` is a positive
144
+ integer that keys the stored manifest hash. The plugin runs git itself, as
145
+ argument arrays with no shell and with `--no-ext-diff` and `--no-textconv`, in
146
+ the run's repository. It rejects a revision that is not a string, starts with
147
+ `-` or holds whitespace, then resolves `base` and `head` to full SHAs with
148
+ `git rev-parse --verify <rev>^{commit}`. It writes, under
149
+ `<run dir>/review/<head-sha>/<manifest-hash>/`:
150
+
151
+ - `round.diff`: the three-dot diff `base...head`, what `head` changed since it
152
+ left `base`, as a pull request shows it. Work that landed on `base` since is
153
+ not in it.
154
+ - `diffstat.txt`: the diffstat of the same range.
155
+ - `files/<repo-relative path>`: each file the diff touches (deleted files
156
+ excluded), as it is at `head`.
157
+ - `manifest.json`: `base`, `head`, `mergeBase`, the SHA-256 and size of the
158
+ diff, the diffstat and every file, and the skipped files.
159
+
160
+ It returns the directory, the file list, the skipped files and `manifestHash`
161
+ (the SHA-256 of `manifest.json`). The hash is stored in plugin storage under
162
+ `review/<pr>@<head>`, and the export becomes the run's current one, the only
163
+ directory the hook opens.
164
+
165
+ Binary files, files above 512 KiB, submodules and unsafe paths (`..`,
166
+ absolute, `.git`) are skipped and listed. A range with more than 300 changed
167
+ files, a diff above 4 MiB or more than 8 MiB of file content is refused and
168
+ nothing is written. Exports are immutable: a published directory is never
169
+ overwritten or deleted. Exporting the same content again returns the same
170
+ directory if it still verifies; if it was damaged, the damaged directory is
171
+ renamed to `.rejected-<id>` and a fresh one is published under the same hash.
172
+
173
+ `verifyExport(dir, storedManifestHash)` is the check for whoever hands the
174
+ export to a reviewer (the plugin, before it prompts one). It refuses unless
175
+ `manifest.json` hashes to the stored value and every file, the diff and the
176
+ diffstat match their recorded SHA-256 and size, and there is no extra or
177
+ missing file or directory, no symlink and no non-regular file. The allowed path
178
+ is model-writable, so the manifest hash is the integrity anchor; the
179
+ permission is not.
180
+
181
+ `autopilot_merge({ pr, sha, method })` merges one PR. It asks the host for the
182
+ action `autopilot_merge` on `<pr>@<sha>`, which the hook allows only under
183
+ `--allow merge`. The tool then reads the PR from `gh` and refuses unless all of
184
+ these hold:
185
+
186
+ - The PR is open and its head is `sha`.
187
+ - A GitHub review in state `APPROVED` on commit `sha` exists from an account
188
+ other than the PR author, and no reviewer's latest review requests changes.
189
+ A comment never counts.
190
+ - Every check run and status on `sha` is success, neutral or skipped, and at
191
+ least one exists. Each context that branch protection or a ruleset requires
192
+ is also present and passing. If either source cannot be read, the tool
193
+ refuses; a branch GitHub reports as "Branch not protected" counts as read.
194
+ - The base branch has no merge queue (a `merge_queue` ruleset rule). Merge
195
+ queues are unsupported and the tool refuses, journaling an issue.
196
+ - `method` (`squash`, `rebase` or `merge`) is one the repository allows.
197
+
198
+ Just before merging, it re-reads the PR's base branch and head. It refuses if
199
+ the base is not the one whose protection and rulesets it read, or if the head is
200
+ not `sha`. It then merges through the synchronous REST endpoint, as an
201
+ argument array with no shell:
202
+ `gh api -X PUT repos/{owner}/{repo}/pulls/<pr>/merge -f sha=<sha> -f merge_method=<method>`.
203
+ It never calls `gh pr merge`, so it can neither enable auto-merge nor use
204
+ `--admin`, and GitHub enforces its own protection. Success needs a response
205
+ with `merged: true` and a `sha`. Any non-2xx response is a refusal, for example
206
+ 405 for a queue-required or protected branch and 409 for a head mismatch; the
207
+ journal issue carries the status and message. As a final check, the tool
208
+ re-reads the PR (`state,headRefOid,mergeCommit,autoMergeRequest`) and reports
209
+ success only when it is `MERGED` at `sha` with a merge commit and no auto-merge
210
+ request. It journals the result: an `action` on success, an `issue` on a
211
+ refusal. GitHub only. The approval and check judgments live in
212
+ `lib/src/pr-approval.ts`, which `delivery_land` shares.
213
+
214
+ ## Plugin-run review
215
+
216
+ `autopilot_review({ pr, sha })` starts a review that a merge can rest on without
217
+ a GitHub approval, because the model that asks for the merge cannot write the
218
+ verdict. It refuses unless a run is armed, the PR is open at `sha`, and the
219
+ `code-reviewer` agent exists. It then exports the round (as `autopilot_export_review`
220
+ does), verifies the export against its stored manifest hash, and creates a child
221
+ of the lead session with agent `code-reviewer`, metadata
222
+ `{ source, pr, sha, nonce }` and permissions that deny `edit`, `shell`,
223
+ `subagent` and `autopilot_*`. It verifies the export once more right before it
224
+ sends the one prompt: the diff (at most 256 KiB, else the call refuses) between
225
+ lines with a random delimiter, marked as untrusted, and the closing line
226
+ `VERDICT: APPROVE <sha>` or `VERDICT: REQUEST_CHANGES <sha>`. The call returns
227
+ at once. Plugin storage keeps only a pointer (`pr`, `sha`, session, nonce,
228
+ prompt hash, manifest hash, start time), never a verdict.
229
+
230
+ `autopilot_status` (and `autopilot_merge`) resolve each review from the host:
231
+ the session's parent, agent and metadata match; the transcript holds exactly
232
+ one user message and its hash is the prompt's; there is no synthetic message,
233
+ agent switch or later activity; the last idle and the session outcome are
234
+ `succeeded`; and the reviewer's final assistant text, outside any code fence,
235
+ ends in a verdict line naming `sha`. A follow-up message, a failed or
236
+ interrupted turn or a verdict for another SHA voids it. A reviewer that is not
237
+ done 30 minutes after the start is interrupted and voided, and so is a reviewer
238
+ whose transcript shows it finished later than that. The verdict line must be the
239
+ last non-blank line of the reply. The base of the export is the base commit SHA
240
+ GitHub reports for the PR, fetched if needed; the call refuses when it is
241
+ missing or the export is empty while GitHub reports changed files.
242
+
243
+ A head is reviewed at most once, however the review ends. Plugin storage backs
244
+ the claim files: a start is also refused when storage holds a pointer for the
245
+ head under any PR number or lead, and a storage read that fails refuses the
246
+ start, is journaled as a blocker and appears as `storageErrors` in
247
+ `autopilot_status`, labelled by operation and resource and cleared only by the
248
+ same operation on the same resource succeeding (a sweep cursor that cannot be
249
+ read stops the sweep, and a merge under the grant refuses when the review record
250
+ cannot be read); it is never read as "no record". The export is also refused
251
+ when its diff is empty or its changed-file count differs from the `changed_files`
252
+ that GitHub reports (a rename counts once, as `git diff --name-status -M`
253
+ lists it). Each status call resolves the newest 20 reviews and the next 25
254
+ pointers after a cursor kept in plugin storage, so every deadline is enforced
255
+ over successive calls. Before any reviewer
256
+ exists the call creates claim files (`pr-<pr>@<sha>` and `head-<sha>`) in
257
+ `<git common dir>/opencode-scratch/autopilot/review-claims/`, with the same
258
+ create-exclusive link the run state uses. They are released only when the start
259
+ is refused, never after a verdict, void, deleted session or follow-up, so a
260
+ verdict cannot be re-rolled.
261
+
262
+ Under `--allow self-review`, `autopilot_merge` skips only the GitHub approval,
263
+ and only when the resolved review approves exactly the head being merged.
264
+ Checks, required contexts, the head and base re-reads and the repository's
265
+ merge method all still apply.
266
+
267
+ Residual risks:
268
+
269
+ - Prompt injection through the diff can talk the reviewer into `APPROVE`. The
270
+ delimiter and the fence-aware parse stop a forged verdict line, not a
271
+ persuaded reviewer.
272
+ - Same-user writes are out of scope: a process running as you can edit the
273
+ scratch area (claim files, run state), the host database
274
+ (`~/.local/share/opencode/opencode.db`) and call the OpenCode server API, and
275
+ so can create or edit sessions and forge a record the plugin accepts.
276
+ - `--allow self-review` is weaker than a second GitHub account. The reviewer is
277
+ a model the same installation runs, with the same user and the same host.
278
+ - Branch protection that requires approving reviews still refuses the merge:
279
+ GitHub does not know about the plugin's review.
280
+
281
+ ## Tick
282
+
283
+ When the agent arms a run, it creates an OpenCode automation with
284
+ `tools.opencode.automation_create`. The automation runs every 4 minutes until
285
+ the deadline. It is a code action, so a tick runs no model. A tick calls
286
+ `autopilot_status`. The tick sends nothing while the host's session record
287
+ shows the lead session as busy. Otherwise, the tick sends a ping:
288
+
289
+ ```text
290
+ ♥ 14:32 · 2/4 lanes · ready 4 · blocked 2 (+1 unblocked) · parked 1 · 1h12m left
291
+ ```
292
+
293
+ If `automation_create` fails, an in-process timer prompts the session instead.
294
+ The plugin unrefs the timer and shares it across the process. The timer also
295
+ skips a tick while the session is busy.
296
+
297
+ ## Stop conditions
298
+
299
+ Explicit conditions:
300
+
301
+ - The scope has no open beads left.
302
+ - The run reaches its wall budget or clock deadline.
303
+ - The free-text condition holds.
304
+ - The user runs `/autopilot stop`.
305
+
306
+ Implicit conditions:
307
+
308
+ - Six ticks in a row pass without progress. Progress means one of these:
309
+ - a closed bead
310
+ - a commit
311
+ - a logged progress entry
312
+ - a finished subagent
313
+ - For a bead or queue scope, every remaining bead is blocked or parked.
314
+
315
+ Some failures park an item and the run goes on:
316
+
317
+ - A bead that the agents attempt 3 times without closing.
318
+ - An item that hits the same error signature twice in a row.
319
+ - A lane that stays silent for 12 minutes. The plugin first asks the lead to
320
+ nudge it, then to restart it once, and then parks it.
321
+
322
+ ## Run directory
323
+
324
+ `$(git rev-parse --git-common-dir)/opencode-scratch/autopilot/<run-id>/` holds:
325
+
326
+ - `journal.jsonl`: the append-only journal
327
+ - `state.<N>.json`: the run state, one file per write. The highest number is
328
+ the state, and the plugin keeps the latest five. A corrupt latest file is a
329
+ blocker; the plugin never falls back to an older one.
330
+ - `state.json`: a symlink to the latest `state.<N>.json`, for reading by hand.
331
+ The plugin never reads it.
332
+ - `summary.md`: the debrief, written at stop
333
+
334
+ `autopilot/latest` links to the newest run. At stop, the plugin also creates a
335
+ bead with the label `autopilot-handoff`. That bead links the run directory and
336
+ lists the items that need input. The plugin's own `bd` writes take the embedded
337
+ store's write lock, as the beads plugin's writes do.
338
+
339
+ ## pi
340
+
341
+ `pi/extensions/autopilot.js` is the same run as a pi extension, built from
342
+ `pi-src/autopilot.ts` over the run code in `shared/`, which the OpenCode plugin
343
+ also runs. The run directory, the journal, the state files, the stop
344
+ conditions and the handoff bead are the same, so `/autopilot review` reads a
345
+ run that either host wrote.
346
+
347
+ Install it with `pi install npm:@srobroek/pi-autopilot`, or load the file
348
+ with `pi -e plugins/autopilot/pi/extensions/autopilot.js`. It needs
349
+ [pi-subagents](https://www.npmjs.com/package/pi-subagents): the run's write
350
+ lanes and reviewers are its `subagent` children. parley's pi package
351
+ (`npm:@srobroek/pi-parley-rounds`) asks the setup round in one panel, and the
352
+ beads pi package (`npm:@srobroek/pi-beads-enforcer`) gates the lanes' `bd`
353
+ calls. Both are optional.
354
+
355
+ A foreground pi-subagents child loads none of the parent's extensions. For the
356
+ gates to cover the lanes, list this extension for every child in pi's
357
+ `settings.json`:
358
+
359
+ ```json
360
+ {
361
+ "subagents": {
362
+ "defaultSubagentOnlyExtensions": ["/absolute/path/to/autopilot/pi/extensions/autopilot.js"]
363
+ }
364
+ }
365
+ ```
366
+
367
+ ### What pi has and what it lacks
368
+
369
+ | OpenCode | pi |
370
+ | --- | --- |
371
+ | `autopilot_status`, `autopilot_log`, `autopilot_merge`, `autopilot_export_review` | Ported with the same names and input schemas. |
372
+ | `autopilot_review` and `--allow self-review` | Not ported. See below. |
373
+ | `/autopilot [scope] [flags] \| resume \| review \| stop` | Ported. The command sends its prompt as a user message. |
374
+ | `question` and `parley_*` denied while armed | Ported as a `tool_call` block, for the lead and for each child that loads the extension. |
375
+ | Every permission `ask` denied and journaled | pi has no permission rules. A dialog that another extension opens while a run is armed is journaled as a blocker. The plugin cannot answer or close it. |
376
+ | `external_directory` allowed for lane worktrees and the review export | Not needed: pi has no directory permission. |
377
+ | `autopilot_merge` checked by the permission hook | The plugin decides as the hook decides an `ask`. In a running run, `--allow merge` allows the merge, and without it the merge is denied and journaled. Outside a run, the user confirms in a dialog. A session that cannot show one is refused. |
378
+ | Re-prompt after a turn that ends in a question | Ported on `agent_settled`, as a plugin message that starts a turn. A turn the user aborted is not re-prompted. |
379
+ | Notice after compaction | Ported on `session_compact`, delivered after the current turn. |
380
+ | Tick automation | pi has no automations. Arming and `/autopilot resume` start the plugin's in-process timer for the lead session. It ticks every 4 minutes and skips while the session is busy. `pi -p` exits after its prompt, so a print-mode run gets no ticks. |
381
+ | Child sessions in the tick | pi lists no child sessions to an extension. The ping counts 0 busy lanes. The stall ladder (nudge, restart, park) and "a finished subagent" as progress do not apply. "Everything left is blocked or parked" never stops a run. The other stop conditions apply. |
382
+ | Plugin storage | One JSON file per key under `$PI_CODING_AGENT_DIR/autopilot/storage/` (default `~/.pi/agent`), which every pi process reads. |
383
+
384
+ Every pi session runs in its own extension runtime. A subagent child finds its
385
+ run through its session headers: each child's `parentSession` names its
386
+ parent's session file, up to the armed lead. Each `question` or `parley_*` call
387
+ reads the armed runs from storage again, so a child sees a run that its lead
388
+ armed after the child started. The gate fails closed in two cases:
389
+
390
+ - A parent header that cannot be read counts as an unknown lineage. Its asks
391
+ are blocked while any run is armed.
392
+ - Storage that cannot be read blocks every ask.
393
+
394
+ `autopilot_review` is not ported. On OpenCode, the verdict that a merge rests
395
+ on comes from a reviewer session that the host keeps. The plugin creates that
396
+ session with fixed permissions and re-reads its record on every status call:
397
+ parent, agent, metadata, a single prompt with a known hash, and the outcome.
398
+ It also interrupts a reviewer that runs past its deadline. pi has no host
399
+ session API for any of that. A pi-subagents reviewer's answer would live only
400
+ in the extension's memory and in files that the model can write. On pi,
401
+ `/autopilot --allow self-review` and an arm with `self-review` are refused, and
402
+ every merge needs a GitHub approval.
403
+
404
+ The pi tree ships `SKILL.pi.md` as its skill. It describes the timer tick, the
405
+ missing child view and the merge rules above.
406
+
407
+ ## Works with
408
+
409
+ This package works with these pi packages. Install them next to it.
410
+
411
+ | Package | Required | For | Install |
412
+ | --- | --- | --- | --- |
413
+ | `pi-subagents` | yes | the subagent tool that runs the run's write lanes and reviewers | `pi install npm:pi-subagents` |
414
+ | `@srobroek/pi-parley-rounds` | no | parley_ask, which asks the setup round's questions in one panel | `pi install npm:@srobroek/pi-parley-rounds` |
415
+ | `@srobroek/pi-beads-enforcer` | no | the bd gates and per-session actors the lanes' bead claims rely on | `pi install npm:@srobroek/pi-beads-enforcer` |
@@ -0,0 +1,21 @@
1
+ {
2
+ "plugin": "autopilot",
3
+ "prerequisites": [],
4
+ "companions": [
5
+ {
6
+ "package": "pi-subagents",
7
+ "for": "the subagent tool that runs the run's write lanes and reviewers",
8
+ "required": true
9
+ },
10
+ {
11
+ "package": "@srobroek/pi-parley-rounds",
12
+ "for": "parley_ask, which asks the setup round's questions in one panel",
13
+ "required": false
14
+ },
15
+ {
16
+ "package": "@srobroek/pi-beads-enforcer",
17
+ "for": "the bd gates and per-session actors the lanes' bead claims rely on",
18
+ "required": false
19
+ }
20
+ ]
21
+ }