@ftrdotdev/pi-delegates 0.1.0 → 0.2.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.
Files changed (94) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/OPERATIONS.md +79 -6
  3. package/README.md +25 -17
  4. package/ROADMAP.md +21 -0
  5. package/TESTING.md +153 -318
  6. package/package.json +4 -3
  7. package/skills/pi-delegates/SKILL.md +29 -0
  8. package/skills/pi-delegates/references/launch.md +28 -0
  9. package/src/docs-contract.test.ts +106 -0
  10. package/src/domain/definitions.ts +3 -1
  11. package/src/domain/failure.ts +33 -0
  12. package/src/domain/model-source.ts +30 -0
  13. package/src/domain/orchestration/control.test.ts +69 -35
  14. package/src/domain/orchestration/control.ts +110 -89
  15. package/src/domain/orchestration/fanout.test.ts +125 -20
  16. package/src/domain/orchestration/fanout.ts +60 -22
  17. package/src/domain/orchestration/gates.test.ts +14 -15
  18. package/src/domain/orchestration/launch.test.ts +210 -11
  19. package/src/domain/orchestration/launch.ts +142 -32
  20. package/src/domain/orchestration/requests.test.ts +11 -16
  21. package/src/domain/orchestration/requests.ts +14 -1
  22. package/src/domain/orchestration/review.test.ts +89 -11
  23. package/src/domain/orchestration/run-wait.test.ts +4 -1
  24. package/src/domain/orchestration/run-wait.ts +1 -1
  25. package/src/domain/orchestration/workflow.test.ts +157 -16
  26. package/src/domain/orchestration/workflow.ts +269 -196
  27. package/src/domain/run-records.ts +214 -52
  28. package/src/domain/settlement.test.ts +186 -0
  29. package/src/domain/settlement.ts +137 -0
  30. package/src/domain/shapes.test.ts +20 -0
  31. package/src/domain/skills.test.ts +31 -20
  32. package/src/domain/skills.ts +36 -19
  33. package/src/domain/toolkit.test.ts +15 -1
  34. package/src/domain/toolkit.ts +8 -13
  35. package/src/domain/turn-budget.test.ts +37 -0
  36. package/src/domain/turn-budget.ts +23 -0
  37. package/src/domain/view.test.ts +78 -14
  38. package/src/domain/view.ts +67 -35
  39. package/src/extension/commands.test.ts +27 -23
  40. package/src/extension/commands.ts +10 -7
  41. package/src/extension/entry.test.ts +214 -56
  42. package/src/extension/entry.ts +78 -40
  43. package/src/extension/gate-alerts.test.ts +10 -8
  44. package/src/extension/inspector-command.test.ts +173 -21
  45. package/src/extension/inspector-command.ts +100 -53
  46. package/src/extension/prompt.test.ts +2 -4
  47. package/src/extension/retry-observer.test.ts +292 -0
  48. package/src/extension/retry-observer.ts +150 -0
  49. package/src/extension/settle-alerts.test.ts +579 -0
  50. package/src/extension/settle-alerts.ts +175 -0
  51. package/src/extension/shortcut.test.ts +6 -8
  52. package/src/extension/tools.test.ts +325 -24
  53. package/src/extension/tools.ts +61 -13
  54. package/src/extension/version.test.ts +21 -29
  55. package/src/goldens.test.ts +294 -0
  56. package/src/harness-policy.test.ts +13 -11
  57. package/src/layers.test.ts +309 -0
  58. package/src/layers.ts +405 -0
  59. package/src/package-manifest.test.ts +32 -19
  60. package/src/presentation/actions.test.ts +8 -8
  61. package/src/presentation/activity.test.ts +7 -8
  62. package/src/presentation/close.test.ts +14 -16
  63. package/src/presentation/inspector.test.ts +14 -11
  64. package/src/presentation/inspector.ts +27 -25
  65. package/src/presentation/overlay.test.ts +1 -3
  66. package/src/presentation/repaint.test.ts +226 -190
  67. package/src/presentation/snapshot.test.ts +5 -0
  68. package/src/presentation/status.test.ts +43 -0
  69. package/src/presentation/status.ts +30 -0
  70. package/src/presentation/strip.test.ts +13 -6
  71. package/src/presentation/strip.ts +7 -14
  72. package/src/presentation/wording.test.ts +153 -3
  73. package/src/presentation/wording.ts +78 -6
  74. package/src/runtime/bridge.ts +38 -44
  75. package/src/runtime/{timers.test.ts → clock.test.ts} +1 -1
  76. package/src/runtime/clock.ts +31 -0
  77. package/src/runtime/config.test.ts +26 -7
  78. package/src/runtime/config.ts +6 -1
  79. package/src/runtime/execution-env.test.ts +6 -8
  80. package/src/runtime/extensions.test.ts +4 -4
  81. package/src/runtime/extensions.ts +23 -11
  82. package/src/runtime/handle.ts +54 -0
  83. package/src/runtime/log.test.ts +19 -7
  84. package/src/runtime/log.ts +23 -0
  85. package/src/runtime/mcp.test.ts +29 -32
  86. package/src/runtime/mcp.ts +70 -3
  87. package/src/runtime/runtime-state.test.ts +25 -0
  88. package/src/runtime/runtime-state.ts +36 -0
  89. package/src/runtime/session-tag.ts +11 -0
  90. package/src/runtime/session.test.ts +24 -31
  91. package/src/runtime/session.ts +99 -261
  92. package/src/runtime/storage.ts +29 -0
  93. package/src/test-hygiene.test.ts +97 -0
  94. package/src/runtime/timers.ts +0 -10
package/CHANGELOG.md CHANGED
@@ -1,5 +1,56 @@
1
1
  # @ftrdotdev/pi-delegates
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Run delegates asynchronously. A launch accepts `"execution": "detached"` and
8
+ returns the run id with its opening status at claim, so a turn can start more
9
+ work while the child runs. When the runs settle, one status notice names them,
10
+ the group and each child's parent; it starts no turn, while a gate or request
11
+ alert still does.
12
+
13
+ A run now carries a turn budget. `maxTurns` resolves from a fanout child, then
14
+ the request, then the agent definition, then `defaults.maxTurns` (120), and every
15
+ value is clamped to `caps.maxTurns` (400); `maxTurns: 0` still runs one round.
16
+
17
+ A failed run keeps its reason. `delegate_status` prints
18
+ `failed (turn_limit): 1 turn`, every failed run logs one `run.failed` line with
19
+ `reason`, `retryable`, `remedy` and `detail`, and each scheduled durable retry
20
+ logs one `run.retry` line. A scripted detached launch answers
21
+ `{ run, answer: "", status: "queued" }`, and a v1 or v2 store opens as v3.
22
+
23
+ - 4fb9f00: Show the inspector's state in Pi's footer. The line reads `delegates off ·
24
+ /delegates` while the inspector is off, `delegates idle · ctrl+space` while it is
25
+ on and quiet, and names the live counters while work is pending. It follows the
26
+ `/delegates` toggle, the restore at session start, and every publication; an
27
+ unchanged publication writes nothing, and a reload or shutdown clears the line
28
+ instead of leaving a stale one. A session that never enabled the inspector shows
29
+ no line at all.
30
+
31
+ The README screenshot is a Markdown image now, so npm renders it.
32
+
33
+ The inspector shortcut now refuses with the same hint `/delegates:overlay`
34
+ prints when the inspector is off. A silent key read as a broken one, while the
35
+ real answer is `/delegates`.
36
+
37
+ The `Delegates` box above the editor now stays mounted from activation to
38
+ teardown instead of appearing only while work runs. An idle box reads `idle —
39
+ ctrl+space`, so the first delegate launch no longer changes whether the box
40
+ exists, only what it says. Zero counters no longer print, in the box or in the
41
+ footer: both read the same formatter, so the two surfaces cannot drift.
42
+
43
+ ### Patch Changes
44
+
45
+ - 0c040e3: Declare every host-provided peer with a `"*"` range, including
46
+ `@earendil-works/pi-coding-agent`, as the other packages and Pi's package guide
47
+ do. The exact `1.6.0` pin rejected a newer host for no runtime benefit: the load
48
+ check already compares the host's `@earendil-works/pi-ai` against the version
49
+ `pi-durable` pins, and Pi suppresses peer installation for managed packages. A
50
+ plain `npm install` still resolves the peer, but no longer to one fixed version.
51
+ - Updated dependencies [a7f50b9]
52
+ - @ftrdotdev/pi-tui@0.9.1
53
+
3
54
  ## 0.1.0
4
55
 
5
56
  ### Minor Changes
package/OPERATIONS.md CHANGED
@@ -6,10 +6,10 @@ agent definitions, see [README.md](./README.md).
6
6
  ## Failure log
7
7
 
8
8
  The package writes `<sessionDir>/delegates.log`, one JSON object per line. It
9
- records every harness report, every config diagnostic, an unverified version
10
- note, a swallowed tool or command error, a launch with no host model, a control
11
- refusal for a toolkit name that no longer resolves, and every wait outcome other
12
- than `settled`.
9
+ records harness reports, config diagnostics, unverified version notes, swallowed
10
+ tool and command errors, a launch with no host model, a control refusal for a
11
+ stale toolkit name, every wait outcome other than `settled`, every scheduled
12
+ durable retry, every failed run and every settle notice.
13
13
 
14
14
  Nothing rotates the file. A session writes short lines, so it stays small. A log
15
15
  that reaches 5 MB is a roadmap item, not a current behaviour.
@@ -33,9 +33,33 @@ Each entry has `ts`, `level`, `code` and primitive fields. `level` is `debug`,
33
33
  - `tool.not_ready`
34
34
  - `launch.host_model_missing`
35
35
  - `control.missing_names`
36
+ - `run.retry`, `run.failed` and `run.settled` for a run's lifecycle.
37
+ - `settle.notify_failed`, `retry.observer_failed` and `settle.scan_truncated`
38
+ for the observers' own narrow failures.
36
39
 
37
- `detail` holds the error message, truncated to 200 characters. It is for a
38
- person to read. No code path parses it or classifies state from it.
40
+ ### Run lifecycle codes
41
+
42
+ - `run.retry` — `warn`: one warning per scheduled retry attempt, so a retried
43
+ generation leaves as many lines as attempts were scheduled (durable's own
44
+ retry). Fields: `runId`, `attempt`, `at`, `detail`.
45
+ - `run.failed` — `error`: exactly one line per failed run. Fields: `runId`,
46
+ `reason`, the canonical 200-character `detail`, `retryable` (derived from the
47
+ observed retry count) and `remedy` (`relaunch`, `narrow` or `fix_config`). A
48
+ fanout or review group writes no second line: the failed child writes its own.
49
+ - `run.settled` — `info`: the headless settle notice, one line per coalesced
50
+ batch. Fields: `runs`, `detail`.
51
+ - `settle.notify_failed` — `warn`: a settle message the host refused
52
+ synchronously; the runs stay unannounced and the next window retries.
53
+ `pi.sendMessage` is fire-and-forget, so a later rejection is not visible here.
54
+ - `retry.observer_failed` — `warn`: the retry observer could not start; no retry
55
+ is counted in that session.
56
+ - `settle.scan_truncated` — `info`: the start scan looked at the newest 200 run
57
+ ids only, and `omitted` names how many it skipped.
58
+
59
+ `detail` holds the error message, at most 200 characters. It is for a person to
60
+ read. No code path parses it or classifies state from it. A failure detail keeps
61
+ both ends — 120 characters from the head, then `...`, then the tail — because a
62
+ provider error leads with the status that matters.
39
63
 
40
64
  An entry never holds the request, the task text, an answer, a prompt or a
41
65
  command.
@@ -55,3 +79,52 @@ and runs no task.
55
79
 
56
80
  There is no cross-process locking and no store takeover. The roadmap covers a
57
81
  second writer, retention and migration between package versions.
82
+
83
+ ## Composition
84
+
85
+ One session is one Effect layer graph, built in one scope by
86
+ [`src/layers.ts`](./src/layers.ts). The order is seams, then harness, then
87
+ bridge, then runtime. `makeSessionSeams` provides the seven environment-specific
88
+ services that `SessionSeams` ([`src/runtime/handle.ts`](./src/runtime/handle.ts))
89
+ lists: `StorageService`, `ClockService`, `TimersService`, `LogSinkService`,
90
+ `ModelSourceService`, `SkillsCatalogue` and `McpHostService`. A seam is a tag
91
+ whose binding is environment-specific.
92
+ [`docs/effect-conventions.md`](../../docs/effect-conventions.md) holds the layer
93
+ and naming rules. A doubled seam
94
+ (`SeamDoubles` in [`src/layers.ts`](./src/layers.ts)) replaces the production
95
+ default in place, so a test builds the same graph the entry does and every other
96
+ seam stays production. The harness opens the store and runs the tasks. The bridge
97
+ is the one path every harness call takes. The runtime is the session's public
98
+ value: the store, the harness, the bridge and the effects a call site reads.
99
+
100
+ `openSessionRuntime` owns the graph. It returns a `SessionHandle`
101
+ ([`src/runtime/handle.ts`](./src/runtime/handle.ts)), which holds `runtime`,
102
+ `run` (one effect inside the graph's context) and `close`. The extension entry
103
+ keeps the current handle in a `Ref` and never holds the `ManagedRuntime`. `close`
104
+ is that runtime's `dispose`. `closeWithin`
105
+ ([`src/extension/revocation.ts`](./src/extension/revocation.ts)) bounds the wait,
106
+ and `close` can be awaited twice.
107
+
108
+ The inspector does not join the graph. `openInspectorActivation`
109
+ ([`src/layers.ts`](./src/layers.ts)) builds `Inspector.layer` fresh in its own
110
+ `Scope` through `handle.run`, so a keypress after a teardown gets a new instance
111
+ whose `finish` has not run. `teardownActivation`
112
+ ([`src/extension/inspector-command.ts`](./src/extension/inspector-command.ts))
113
+ closes the overlay, unmounts the strip, runs the remaining sinks, interrupts the
114
+ subscription, then closes the activation's scope.
115
+
116
+ `src/layers.ts` is the only file that builds the session graph. It defines no
117
+ service construction of its own: each service defines its own layer as a static
118
+ member (`ClockService.layer(now)`, `McpHostService.layer(options)`,
119
+ `McpHostService.adopt(host)`). The file merges those layers with the
120
+ graph-internal `SessionCore`, the bridge and the runtime layer, and it is the only
121
+ production file that holds a `ManagedRuntime`. `test-support/platform.ts` holds
122
+ its own for the platform helpers.
123
+
124
+ A new seam costs seven edits under `src`, plus one in the test fixture. Add a
125
+ member to `SessionSeams` ([`src/runtime/handle.ts`](./src/runtime/handle.ts)), a
126
+ field to `SessionConfig` and one to `SeamDoubles`, a line to `makeSessionSeams`
127
+ (all three in [`src/layers.ts`](./src/layers.ts)), a field to `HarnessParts` and
128
+ one to `SessionRuntime`, and a line to `buildSessionRuntime` (all three in
129
+ [`src/runtime/session.ts`](./src/runtime/session.ts)). The fixture helper
130
+ `test-support/seams.ts` names the new field so a test can double it.
package/README.md CHANGED
@@ -8,29 +8,24 @@ gate decisions and dispositions are still there.
8
8
 
9
9
  Package: `@ftrdotdev/pi-delegates` · [MIT](LICENSE)
10
10
 
11
- <img src="https://raw.githubusercontent.com/felixnorden/pi-mono/main/docs/images/pi-delegates.png" width="880" alt="The Delegates inspector overlay: a fanout run with three children, one vault-scout waiting on the vault-access gate and two scouts answered after 5 and 12 seconds, the gate row, the lane counts, and the key line">
11
+ ![The Delegates inspector overlay: a fanout run with three children, one vault-scout waiting on the vault-access gate and two scouts answered after 5 and 12 seconds, the gate row, the lane counts, and the key line](https://raw.githubusercontent.com/felixnorden/pi-mono/main/docs/images/pi-delegates.png)
12
12
 
13
13
  The inspector overlay (`ctrl+space`): one fanout run, its three children, the
14
14
  gate one child is parked on, and the lane counts. Every row is live.
15
15
 
16
- ## Requirements
17
-
18
- - Pi `1.6.0`, declared as a peer dependency.
19
- - Node `22.19.0` or newer. The store uses `node:sqlite`.
20
-
21
- On load, the package checks that Pi resolved the same `@earendil-works/pi-ai`
22
- version that pi-durable pins. When the versions differ, it refuses to load and
23
- prints both versions.
24
-
25
- `pi-subagents` and `@ftrdotdev/pi-delegates` both provide delegation tools, so
26
- `@ftrdotdev/pi-delegates` refuses to start while the `pi-subagents` extension is
27
- loaded. Disable it for the run, or remove it:
16
+ ## Installation
28
17
 
29
18
  ```bash
30
- pi remove npm:pi-subagents
31
19
  pi install npm:@ftrdotdev/pi-delegates
32
20
  ```
33
21
 
22
+ ## Requirements
23
+
24
+ - Pi `1.6.0` with a matching `@earendil-works/pi-ai`, the version `pi-durable`
25
+ pins. On load the package compares the two; when they differ it refuses to
26
+ start and prints both versions.
27
+ - Node `22.19.0` or newer. The store uses `node:sqlite`.
28
+
34
29
  ## Inspector shortcut
35
30
 
36
31
  `ctrl+space` opens the inspector overlay while the inspector is active. Change
@@ -53,7 +48,16 @@ approves a gate, `d` denies one, and `escape` closes. A waiting child with one
53
48
  gate also accepts `a` or `d`: the write goes to the parent run that owns the
54
49
  gate. Finished rows show `took <duration>`; live rows show their current
55
50
  activity or age. An expanded row keeps its details indented beneath it.
56
- `ctrl+space` only opens the overlay; it never acts on a row.
51
+ `ctrl+space` only opens the overlay; it never acts on a row. While the
52
+ inspector is off, the key prints a hint that names `/delegates`, the same notice
53
+ `/delegates:overlay` prints, instead of doing nothing. The footer carries the
54
+ state in one line: `delegates off · /delegates` when off, `delegates idle ·
55
+ ctrl+space` when on and quiet, and the live counters (`delegates running 2 ·
56
+ gates 1 · ctrl+space`) while work is pending. A session that never enabled the
57
+ inspector shows no footer line. The `Delegates` box above the editor stays
58
+ mounted from activation to teardown: `idle — ctrl+space` when nothing is
59
+ running, then the non-zero counters and up to two live child lines while work
60
+ runs.
57
61
 
58
62
  When a gate becomes pending, one warning notice names the gate and any waiting
59
63
  child, and one message enters the parent session. The message tells the agent to
@@ -423,11 +427,15 @@ diagnostic.
423
427
 
424
428
  ```json
425
429
  {
426
- "caps": { "laneCap": 4, "sessionCeiling": 8 },
427
- "defaults": { "maxTurns": 40, "launchWaitMs": 600000 }
430
+ "caps": { "laneCap": 4, "sessionCeiling": 8, "maxTurns": 400 },
431
+ "defaults": { "maxTurns": 120, "launchWaitMs": 600000 }
428
432
  }
429
433
  ```
430
434
 
435
+ `defaults.maxTurns` is the budget a child gets when no request or definition
436
+ names one. `caps.maxTurns` is the ceiling every resolved budget is clamped to,
437
+ and no budget goes below one round.
438
+
431
439
  ## Failure log
432
440
 
433
441
  The package writes `<sessionDir>/delegates.log`, one JSON object per line. It
package/ROADMAP.md CHANGED
@@ -48,6 +48,14 @@ that work is not deferred.
48
48
 
49
49
  - **Store retention and cleanup.** Stores live beside the session file and are
50
50
  never pruned. A later changeset adds a retention command.
51
+ - **A project-local store.** `storePathFor` puts the SQLite file under the Pi
52
+ sessions directory (`<sessionDir>/delegates-<sessionId>.sqlite`), a hashed
53
+ slug away from the checkout it describes, while the sibling package keeps
54
+ `.pi/subagents/` inside the project. Moving the store to
55
+ `<cwd>/.pi/delegates/<sessionId>.sqlite` needs the `.pi/mcp.json` trust gate,
56
+ a fallback open of the old path for a session that is mid-upgrade, a
57
+ documented `.gitignore` line, and the failure log and MCP result files moved
58
+ with it. `docs/issues/project-local-store.md` records the details.
51
59
  - **Store migration between package versions.** Durable task and document
52
60
  migrations are supported today. A store migration between
53
61
  `@ftrdotdev/pi-delegates` versions is not.
@@ -67,6 +75,14 @@ that work is not deferred.
67
75
  with the old rules.
68
76
  - **Spend enforcement.** The package reports no spend. Budgets and goal limits
69
77
  are not enforced.
78
+ - **A turn budget the task chooses.** `maxTurns` is one global value from
79
+ `delegates.json` (40 by default), applied to every child. Neither the launch
80
+ request nor the agent definition can raise it, and the skill says nothing
81
+ about sizing a task to it, so a child that reads a long guide, edits a
82
+ package and verifies the result ends `turn_limit` with its report unwritten.
83
+ What it needs: a higher default, `maxTurns` on the request and the
84
+ definition, the resolved value in the run record, and a `Turn budget`
85
+ section in the skill. `docs/issues/turn-budget.md` records the details.
70
86
  - **A live approval dialog.** Gates are committed records and parked calls.
71
87
  They are not interactive prompts inside a running delegate.
72
88
 
@@ -76,6 +92,11 @@ that work is not deferred.
76
92
  stable `code` and a 200-character `detail`. It adds no telemetry context, no
77
93
  trace links and no counters, because one sink has one reader. Log rotation
78
94
  waits until a session log reaches 5 MB.
95
+ - **A failed run's reason without its detail.** `delegate_status` prints
96
+ `failed (model_error)` and drops the provider text that the store keeps in
97
+ `RunFailure.detail`, and no log code covers a run failure at all, so a 429
98
+ reads exactly like a malformed request. `docs/issues/failure-detail.md`
99
+ records the details.
79
100
  - **Telemetry, metrics and trace links.** Version 1 writes the JSONL file and
80
101
  nothing else: no Effect logger, no `pi-telemetry` context, no counters and no
81
102
  span links. A second reader or a trace view is the trigger; until then it