@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/LICENSE +21 -0
  3. package/OPERATIONS.md +57 -0
  4. package/README.md +448 -2
  5. package/ROADMAP.md +158 -0
  6. package/TESTING.md +1061 -0
  7. package/agents.ts +8 -0
  8. package/examples/README.md +114 -0
  9. package/examples/gated.ts +28 -0
  10. package/index.ts +9 -0
  11. package/package.json +93 -4
  12. package/skills/pi-delegates/SKILL.md +147 -0
  13. package/skills/pi-delegates/references/control.md +159 -0
  14. package/skills/pi-delegates/references/definitions.md +163 -0
  15. package/skills/pi-delegates/references/gates.md +175 -0
  16. package/skills/pi-delegates/references/launch.md +156 -0
  17. package/src/domain/definitions.test.ts +41 -0
  18. package/src/domain/definitions.ts +161 -0
  19. package/src/domain/ids.ts +13 -0
  20. package/src/domain/names.ts +35 -0
  21. package/src/domain/orchestration/admission.ts +78 -0
  22. package/src/domain/orchestration/control.test.ts +1298 -0
  23. package/src/domain/orchestration/control.ts +436 -0
  24. package/src/domain/orchestration/fanout.test.ts +419 -0
  25. package/src/domain/orchestration/fanout.ts +334 -0
  26. package/src/domain/orchestration/gates.test.ts +686 -0
  27. package/src/domain/orchestration/gates.ts +360 -0
  28. package/src/domain/orchestration/launch.test.ts +173 -0
  29. package/src/domain/orchestration/launch.ts +581 -0
  30. package/src/domain/orchestration/requests.test.ts +700 -0
  31. package/src/domain/orchestration/requests.ts +769 -0
  32. package/src/domain/orchestration/review.test.ts +214 -0
  33. package/src/domain/orchestration/review.ts +168 -0
  34. package/src/domain/orchestration/run-wait.test.ts +262 -0
  35. package/src/domain/orchestration/run-wait.ts +256 -0
  36. package/src/domain/orchestration/workflow-section.test.ts +62 -0
  37. package/src/domain/orchestration/workflow-section.ts +99 -0
  38. package/src/domain/orchestration/workflow.test.ts +1425 -0
  39. package/src/domain/orchestration/workflow.ts +705 -0
  40. package/src/domain/run-records.test.ts +27 -0
  41. package/src/domain/run-records.ts +527 -0
  42. package/src/domain/shapes.test.ts +165 -0
  43. package/src/domain/skills.test.ts +466 -0
  44. package/src/domain/skills.ts +97 -0
  45. package/src/domain/tool-schema.test.ts +46 -0
  46. package/src/domain/tool-schema.ts +20 -0
  47. package/src/domain/toolkit.test.ts +201 -0
  48. package/src/domain/toolkit.ts +379 -0
  49. package/src/domain/view.test.ts +739 -0
  50. package/src/domain/view.ts +459 -0
  51. package/src/example.test.ts +15 -0
  52. package/src/extension/commands.test.ts +481 -0
  53. package/src/extension/commands.ts +227 -0
  54. package/src/extension/entry.test.ts +1118 -0
  55. package/src/extension/entry.ts +342 -0
  56. package/src/extension/gate-alerts.test.ts +235 -0
  57. package/src/extension/gate-alerts.ts +143 -0
  58. package/src/extension/guard.test.ts +48 -0
  59. package/src/extension/guard.ts +31 -0
  60. package/src/extension/host-signals.test.ts +20 -0
  61. package/src/extension/host-signals.ts +13 -0
  62. package/src/extension/inspector-command.test.ts +1191 -0
  63. package/src/extension/inspector-command.ts +638 -0
  64. package/src/extension/prompt.test.ts +110 -0
  65. package/src/extension/prompt.ts +50 -0
  66. package/src/extension/request-alerts.test.ts +183 -0
  67. package/src/extension/request-alerts.ts +181 -0
  68. package/src/extension/revocation.test.ts +81 -0
  69. package/src/extension/revocation.ts +84 -0
  70. package/src/extension/shortcut.test.ts +64 -0
  71. package/src/extension/shortcut.ts +105 -0
  72. package/src/extension/tools.test.ts +993 -0
  73. package/src/extension/tools.ts +505 -0
  74. package/src/extension/version.test.ts +123 -0
  75. package/src/extension/version.ts +97 -0
  76. package/src/harness-policy.test.ts +74 -0
  77. package/src/package-manifest.test.ts +157 -0
  78. package/src/presentation/actions.test.ts +221 -0
  79. package/src/presentation/actions.ts +85 -0
  80. package/src/presentation/activity.test.ts +387 -0
  81. package/src/presentation/activity.ts +66 -0
  82. package/src/presentation/close.test.ts +361 -0
  83. package/src/presentation/inspector.test.ts +679 -0
  84. package/src/presentation/inspector.ts +440 -0
  85. package/src/presentation/overlay.test.ts +555 -0
  86. package/src/presentation/overlay.ts +229 -0
  87. package/src/presentation/repaint.test.ts +329 -0
  88. package/src/presentation/shortcut.ts +2 -0
  89. package/src/presentation/snapshot.test.ts +530 -0
  90. package/src/presentation/snapshot.ts +306 -0
  91. package/src/presentation/strip.test.ts +196 -0
  92. package/src/presentation/strip.ts +74 -0
  93. package/src/presentation/wording.test.ts +238 -0
  94. package/src/presentation/wording.ts +222 -0
  95. package/src/runtime/bridge.test.ts +131 -0
  96. package/src/runtime/bridge.ts +134 -0
  97. package/src/runtime/config.test.ts +44 -0
  98. package/src/runtime/config.ts +87 -0
  99. package/src/runtime/context.test.ts +19 -0
  100. package/src/runtime/context.ts +9 -0
  101. package/src/runtime/execution-env.test.ts +35 -0
  102. package/src/runtime/execution-env.ts +15 -0
  103. package/src/runtime/extensions.test.ts +86 -0
  104. package/src/runtime/extensions.ts +140 -0
  105. package/src/runtime/log.test.ts +128 -0
  106. package/src/runtime/log.ts +68 -0
  107. package/src/runtime/mcp.test.ts +1453 -0
  108. package/src/runtime/mcp.ts +938 -0
  109. package/src/runtime/models.test.ts +145 -0
  110. package/src/runtime/models.ts +259 -0
  111. package/src/runtime/observe.test.ts +50 -0
  112. package/src/runtime/observe.ts +150 -0
  113. package/src/runtime/session.test.ts +359 -0
  114. package/src/runtime/session.ts +425 -0
  115. package/src/runtime/timers.test.ts +31 -0
  116. package/src/runtime/timers.ts +10 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,77 @@
1
+ # @ftrdotdev/pi-delegates
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b3c7f62: Turn the inspector on automatically after the first delegate launch in a TUI
8
+ session. An explicit `/delegates` off stays off for later launches. Reload,
9
+ resume and branch switch still restore an active inspector.
10
+ - 735b991: Add `@ftrdotdev/pi-delegates`, a durable agent-delegation extension on `@earendil-works/pi-durable` 1.6.0.
11
+
12
+ The Review 4 minors were re-derived by a conformance audit; it added three missing example tests.
13
+
14
+ Review 5 closed the documented launch call, the `/delegates` gate listing, the version guard, the
15
+ admission reservation, per-child gate settlement and fanout resume.
16
+
17
+ Add `codemode: "runs"` workflows: a `kind: "workflow"` launch runs the task script through durable's
18
+ `codemode` tool, and the script reaches eight `runs` tools over a `WorkflowService`. A scripted child
19
+ is owned by the workflow run task, inherits the workflow's lane, records `parentRunId`, and appears
20
+ nested under the workflow. A `launch_many` child records the same parent and parks its approval gates
21
+ under the workflow run. A script that steers a stopped child gets a refusal, not a thrown mistake.
22
+ A model or thinking-level switch updates the host the script's children use. A restart replays the
23
+ script from durable's journal and reattaches each nested call by key, so no child is duplicated, and
24
+ `delegate_control resume` refuses a workflow run. The `codemode` field is owner-only, and `codemode`
25
+ and `runs` are reserved extension names.
26
+
27
+ A run document is version 2: an older one fills a missing mode with `single` and a missing parent
28
+ with `null`, and keeps both when it already carries them. The session status prints the run mode
29
+ and omits `single`, and `tools.status` refuses an unknown run. A restart with a pending script gate
30
+ reuses the same gate document instead of refusing a duplicate. A failed open closes the session
31
+ sandbox. The lane cap binds `launch_many` groups only, and the script the model passes is trusted
32
+ rather than compared with the launch script.
33
+
34
+ Add MCP servers: the package reads `mcp.json` from the agent directory and, in a trusted project,
35
+ `.pi/mcp.json`, with the host's override rules. Only servers a registered definition references
36
+ connect. Each registers one `mcp:<server>` extension and one durable tool per visible server tool,
37
+ selected by exact name or a `mcp__<server>__*` pattern; `exposure` and `toolExposure` map to
38
+ `callers` and `deferred`, and a selected tool auto-selects `codemode` or `tool-search`. Stdio and
39
+ streamable HTTP transports expand `${NAME}` in `env` and `headers` and never run a command. A server
40
+ that fails to connect degrades with one diagnostic and one warning; calls race the server's
41
+ `timeout` and the run's cancellation, and a result over 20 KiB is cut with the full text saved
42
+ under the session directory.
43
+
44
+ Add questions from a delegate: a definition that selects `questions` in `extensions` and `ask` in
45
+ `tools` gets a tool that commits one request document and parks. `delegate_control answer` and
46
+ `decline` answer it, `/delegates:answer` does the same without the model, and `question` is optional
47
+ because the injected message degrades to plain text. A request also ends the parent's wait: one
48
+ attention racer over the gate index and the request index resolves on the commit, so a 600-second
49
+ block becomes a follow-up turn. A request expires after 24 hours, `stop` cancels it, and a decided
50
+ record is pruned 7 days after it was requested. The inspector, the overlay and `/delegates:list`
51
+ name it; the log keeps ids only.
52
+
53
+ - 416472d: Add a live inspector: `/delegates` toggles a bordered status strip pushed from durable document observations, and `ctrl+space` opens a floating overlay with a parent/child run tree, pending gates, stop/approve/deny, and an `enter`/`i` detail expansion. Waiting children route approve/deny to the run that owns their gate; new gates send one UI warning and one parent-agent message from a session-level watcher, even while the inspector is off. Announced gates are session entries, so a reload does not repeat them. Finished rows show `took <duration>`; expanded detail is indented under a rule.
54
+
55
+ ### Patch Changes
56
+
57
+ - 7b07d56: Name the inspector attach failure in the log.
58
+
59
+ `@ftrdotdev/pi-delegates`: `describeFailure` unwrapped only `InspectorReadFailed`, so an
60
+ `InspectorAttachFailed` produced an empty `detail` in the log. Both errors now
61
+ unwrap, and the log names the attach failure's message.
62
+
63
+ - a4befb4: Document the declarative definition and gate contract in the shipped skill and
64
+ source, and add a runnable gated fanout example.
65
+ - 5cc484c: Declare `@earendil-works/pi-ai` as a peer dependency with a `*` range, so the
66
+ extension loader hands the extension the host's copy instead of installing a
67
+ second one. The loader warns about the old dependency entry on every launch.
68
+ - f8094f4: Show the inspector overlay in the README, and point `homepage` at the package
69
+ README instead of the monorepo root.
70
+ - Updated dependencies [891568b]
71
+ - Updated dependencies [9b1fcd6]
72
+ - Updated dependencies [eeb018a]
73
+ - Updated dependencies [81c4a61]
74
+ - Updated dependencies [f8094f4]
75
+ - Updated dependencies [305cdb7]
76
+ - Updated dependencies [36e53c0]
77
+ - @ftrdotdev/pi-tui@0.9.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Felix Nordén
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/OPERATIONS.md ADDED
@@ -0,0 +1,57 @@
1
+ # Operating pi-delegates
2
+
3
+ This page holds runtime details for operators. For install, configuration and
4
+ agent definitions, see [README.md](./README.md).
5
+
6
+ ## Failure log
7
+
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`.
13
+
14
+ Nothing rotates the file. A session writes short lines, so it stays small. A log
15
+ that reaches 5 MB is a roadmap item, not a current behaviour.
16
+
17
+ ### Fields
18
+
19
+ Each entry has `ts`, `level`, `code` and primitive fields. `level` is `debug`,
20
+ `info`, `warn` or `error`.
21
+
22
+ `code` is stable and dot-namespaced. The prefixes are:
23
+
24
+ - `report.<kind>` for a harness report.
25
+ - `config.<problem>` for a configuration diagnostic.
26
+ - `wait.<outcome>` for a launch wait that did not settle.
27
+ - `request.<kind>` for a request that is announced or aborted (`request.pending`, `request.cancelled`).
28
+ - `requests.<kind>` for the start-up prune (`requests.pruned`, `requests.prune_failed`).
29
+ - `inspector.*` for inspector observations, actions and notifications.
30
+ - `version.unverified`
31
+ - `command.viewer_failed`
32
+ - `command.answer_failed`
33
+ - `tool.not_ready`
34
+ - `launch.host_model_missing`
35
+ - `control.missing_names`
36
+
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.
39
+
40
+ An entry never holds the request, the task text, an answer, a prompt or a
41
+ command.
42
+
43
+ ### Guarantees
44
+
45
+ - A broken log path never fails a call. The write is dropped.
46
+ - A logger never throws.
47
+ - `detail` is display text, not machine-readable state.
48
+
49
+ ## Session store
50
+
51
+ The session store is at `<sessionDir>/delegates-<sessionId>.sqlite`. The Pi
52
+ process that owns the session is the only writer. No shipped command opens the
53
+ store. [`openReadOnlyRuntime`](./src/runtime/session.ts) opens a store read-only
54
+ and runs no task.
55
+
56
+ There is no cross-process locking and no store takeover. The roadmap covers a
57
+ second writer, retention and migration between package versions.
package/README.md CHANGED
@@ -1,3 +1,449 @@
1
- # Temporary Holding Version
1
+ # pi-delegates
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
+ pi-delegates is a Pi extension for durable agent delegation. A delegated agent
4
+ is a child conversation. Its toolkit (model, thinking level, tools, extensions,
5
+ instructions, working directory and approval rules) is committed when the run
6
+ starts. Close Pi, reopen the session, and the runs, conversations, checkpoints,
7
+ gate decisions and dispositions are still there.
8
+
9
+ Package: `@ftrdotdev/pi-delegates` · [MIT](LICENSE)
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">
12
+
13
+ The inspector overlay (`ctrl+space`): one fanout run, its three children, the
14
+ gate one child is parked on, and the lane counts. Every row is live.
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:
28
+
29
+ ```bash
30
+ pi remove npm:pi-subagents
31
+ pi install npm:@ftrdotdev/pi-delegates
32
+ ```
33
+
34
+ ## Inspector shortcut
35
+
36
+ `ctrl+space` opens the inspector overlay while the inspector is active. Change
37
+ it in `~/.pi/agent/delegates.json`:
38
+
39
+ ```json
40
+ { "inspectorShortcut": "ctrl+alt+i" }
41
+ ```
42
+
43
+ Or set `PI_DELEGATES_INSPECTOR_SHORTCUT=ctrl+alt+i` for one run. `ctrl+i` is
44
+ not used: legacy terminals send it as Tab. `ctrl+shift+...` bindings need a
45
+ terminal with Kitty keyboard protocol support. If the OS reserves
46
+ `ctrl+space` (macOS input switching, for example), use `ctrl+alt+i` or `f2`.
47
+ Run `/reload` after editing the file.
48
+
49
+ In the overlay, `enter` or `i` expands the selected row. On a `fanout` or
50
+ review parent it shows the child rows; the Gates section stays visible below the
51
+ tree. `enter` or `i` on a child shows its details. `s` stops a run, `a`
52
+ approves a gate, `d` denies one, and `escape` closes. A waiting child with one
53
+ gate also accepts `a` or `d`: the write goes to the parent run that owns the
54
+ gate. Finished rows show `took <duration>`; live rows show their current
55
+ 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.
57
+
58
+ When a gate becomes pending, one warning notice names the gate and any waiting
59
+ child, and one message enters the parent session. The message tells the agent to
60
+ ask you for the decision; it does not approve or deny the gate. These alerts run
61
+ with the session, not with the inspector: turning `/delegates` off does not
62
+ silence them. An alert is not repeated after an inspector toggle or a reload.
63
+ The same alert path announces a delegate's question with one warning and one
64
+ parent message. See [Questions from a delegate](#questions-from-a-delegate).
65
+
66
+ `/delegates` toggles the inspector. The first delegate launch in a TUI session
67
+ turns it on automatically. An explicit off stays off for later launches, and
68
+ reload, resume and branch switch restore an active inspector.
69
+
70
+ ## Agent skill
71
+
72
+ The package ships a skill for agents. It covers when to delegate, launch
73
+ shapes, definition fields, run status, control, gates and gate automation. Pi
74
+ loads it when a task matches, or run `/skill:pi-delegates`.
75
+
76
+ ## Tools
77
+
78
+ | Tool | Purpose |
79
+ | ------------------ | ------------------------------------------------------------------------- |
80
+ | `delegate_launch` | Start a single agent, a fanout in a lane, or a panel of reviewers |
81
+ | `delegate_control` | `steer`, `stop`, `resume`, `approve`, `deny`, `answer` or `decline` a run |
82
+ | `delegate_status` | Show runs, lanes, pending gates and requests; with no argument, newest 50 |
83
+
84
+ `delegate_status` accepts `run` or `lane` to narrow the view. With `run` it
85
+ reads only that run, prints what it waits on and its review rows, and prints no
86
+ lane line. Without `run` it returns the newest 50 runs, prints how many older
87
+ runs it left out, and lists what needs a person first.
88
+
89
+ A launch needs a host model. When the session has no model and the definition
90
+ sets none, the launch fails with `HostModelMissing` before a task or a run is
91
+ written. The error names the definition, `model.provider` and `model.modelId`.
92
+
93
+ A launch waits up to `defaults.launchWaitMs`, which is 10 minutes by default.
94
+ It returns the answer for a single run, or the group summary for a fanout or
95
+ review. The result names the run id. When the child parks on an approval gate,
96
+ it returns `waiting` with the gate names. When the deadline passes, it returns
97
+ `running` or `queued` with the run id, and the run keeps working.
98
+
99
+ The prompt section lists the built-in presets:
100
+
101
+ | Preset | Tools |
102
+ | ---------- | ------------------------------- |
103
+ | `scout` | `read` |
104
+ | `builder` | `read`, `bash`, `write`, `edit` |
105
+ | `reviewer` | `read` |
106
+
107
+ Presets use the host session's model and thinking level at launch. They carry
108
+ no approval rules. Gates are opt-in and belong to the user who registers the
109
+ definition. See [Register an agent with a gate](#register-an-agent-with-a-gate).
110
+
111
+ ## MCP servers
112
+
113
+ The package reads `mcp.json` from the agent directory and, when Pi marks the
114
+ project trusted, `<project>/.pi/mcp.json`. Project entries replace global
115
+ entries with the same name. A project entry without `command`, `url` or `type`
116
+ overrides only `enabled`, `exposure` and `toolExposure` of the global server. A
117
+ project entry may not set `auth` beside `url`: a repository cannot choose where
118
+ a credential goes.
119
+
120
+ ```json
121
+ {
122
+ "mcpServers": {
123
+ "demo": { "command": "node", "args": ["server.js"] },
124
+ "docs": {
125
+ "url": "https://example.test/mcp",
126
+ "headers": { "Authorization": "Bearer ${DOCS_TOKEN}" }
127
+ }
128
+ }
129
+ }
130
+ ```
131
+
132
+ `${NAME}` in `env` and `headers` expands from the process environment. A
133
+ missing variable keeps its literal and writes `mcp.env_missing`. A value that
134
+ starts with `!` writes `mcp.env_unsupported`, and the server is skipped: the
135
+ package never runs a command to build a server environment.
136
+
137
+ Only a server that a registered definition references connects: a `tools` entry
138
+ under `mcp__<server>__`, or an `extensions` entry `mcp:<server>`. References are
139
+ read at session start, so an inline definition can use a connected server's
140
+ tools but cannot bring in a server that no registered definition references.
141
+ Each connected server registers one extension `mcp:<server>` with the namespace
142
+ `mcp__<server>` and one tool per visible server tool, named
143
+ `mcp__<server>__<tool>`.
144
+
145
+ A definition selects those tools by exact name or by a pattern that names its
146
+ server:
147
+
148
+ ```json
149
+ { "tools": ["read", "mcp__demo__*"] }
150
+ ```
151
+
152
+ `mcp__<server>__*` and `mcp__<server>__<prefix>*` are the only accepted shapes;
153
+ `mcp__*` and a partial namespace are refused with a message that states the
154
+ grammar. An unknown namespace fails the launch with `ToolkitUnresolved`, which
155
+ lists the available namespaces and the configured servers that are not
156
+ connected.
157
+
158
+ `exposure` picks how a server's tools reach the child: `codemode` (default,
159
+ scripts only), `direct` (the model and scripts), `deferred` (the model after
160
+ `tool_search`, and scripts), or `hidden` (no tool is registered unless
161
+ `toolExposure` re-exposes it). A `toolExposure` entry overrides one tool name
162
+ or `*` pattern. A selected codemode tool also selects the `codemode`
163
+ extension; a selected deferred tool selects `tool-search`.
164
+
165
+ A server that cannot connect degrades: no tools, one `mcp.connect_failed`,
166
+ `mcp.connect_timeout` or `mcp.list_tools_failed` line in `delegates.log`, one
167
+ aggregated warning at session start, and the launch continues. A connect waits
168
+ at most 10 seconds. A tool call is bounded by the server's `timeout` (default 60
169
+ seconds) and by the run's cancellation; a timeout returns an error naming the
170
+ server. A result over 20 KiB is cut, and the full text is saved under the
171
+ session directory.
172
+
173
+ ## Workflows
174
+
175
+ A registered definition may set `codemode: "runs"`. Such a definition runs
176
+ workflows only: a `single`, `fanout` or `review` launch of it fails with
177
+ `WorkflowOnly`, and a workflow launch of a definition without the field fails
178
+ with `WorkflowNotEnabled`, both before a run exists. The model-authored inline
179
+ definition cannot set the field.
180
+
181
+ ```ts
182
+ const composer = defineAgent({
183
+ name: "composer",
184
+ role: "Workflow composer",
185
+ prompt: { role: "You compose workflows from delegated runs." },
186
+ tools: ["read"],
187
+ extensions: ["coding-tools"],
188
+ approval: { gates: [] },
189
+ codemode: "runs",
190
+ });
191
+ ```
192
+
193
+ Launch it with `kind: "workflow"`. The `script` is JavaScript that the child's
194
+ model passes verbatim to durable's `codemode` tool:
195
+
196
+ ```json
197
+ {
198
+ "kind": "workflow",
199
+ "agent": "composer",
200
+ "script": "const a = await tools.launch({ agent: 'scout', task: 'a' }); return a.answer"
201
+ }
202
+ ```
203
+
204
+ A script may be at most 65536 UTF-8 bytes; a larger one fails with
205
+ `WorkflowTooLarge` before a run is claimed. The run records mode `workflow`, and
206
+ its lane defaults to `workflow`. The child conversation selects `codemode` and
207
+ `runs` beside the definition's own tools and extensions, so the model sees
208
+ `codemode` and none of the run tools.
209
+
210
+ The script reaches eight tools under the `runs` namespace:
211
+
212
+ | Tool | Purpose |
213
+ | ------------- | ------------------------------------------------------------------------------------- |
214
+ | `launch` | Start one child in the workflow's lane, wait for it, return `{ run, answer, status }` |
215
+ | `launch_many` | Start a group in one lane, wait for all, return one outcome per child |
216
+ | `lane` | Read `{ name, running, queued, answered }` for one lane |
217
+ | `status` | Read every run of the session as `{ run, definition, lane, status }` |
218
+ | `ref` | Read `{ run, conversationId, gateRunId }` for one run |
219
+ | `steer` | Send a message to a child |
220
+ | `stop` | Stop a child |
221
+ | `gate` | Ask a person to approve or deny `script:<name>`, then return the decision |
222
+
223
+ A child run is owned by the workflow run task, inherits the workflow's lane, and
224
+ records `parentRunId`, so it appears nested under the workflow. A `launch_many`
225
+ child records the workflow as its parent and gate run too, so its approval gate
226
+ is listed under the workflow, not under the group. A script cannot stop or steer
227
+ its own run; that call returns an error result naming the run. A refused control
228
+ call returns an error result that names the reason; the script catches it to
229
+ branch. An unknown agent or a missing host model fails the call, so the script
230
+ raises.
231
+
232
+ The lane cap bounds a `launch_many` group. `tools.launch` claims its child
233
+ without admission, like `kind: "single"`; a script that wants parallel children
234
+ calls `launch_many`. The script the model passes is trusted: the toolkit does
235
+ not compare the `codemode` code with the launch script, so the `workflow-script`
236
+ section and the definition prompt are the only instruction.
237
+
238
+ A restart resumes the script from durable's journal: each nested call reattaches
239
+ by key, so an answered child is not started again. `delegate_control resume`
240
+ refuses a workflow run; reopen the session instead. `tools.gate` parks the
241
+ workflow on `script:<name>` until `delegate_control approve` or `deny` releases
242
+ it.
243
+
244
+ ## Questions from a delegate
245
+
246
+ A delegate can put one question to you. The child needs the `questions`
247
+ extension and the `ask` tool in its definition:
248
+
249
+ ```ts
250
+ const envScout = defineAgent({
251
+ name: "env-scout",
252
+ role: "Environment scout",
253
+ prompt: { role: "Ask which environment to inspect, then inspect it." },
254
+ tools: ["read", "ask"],
255
+ extensions: ["coding-tools", "questions"],
256
+ approval: { gates: [] },
257
+ });
258
+ ```
259
+
260
+ `ask` takes `{ prompt, options?, allowOther? }`. With two to five options it
261
+ asks for a choice; with none it asks for free text. `allowOther` defaults to
262
+ true, and a request with no options must allow free text, so `allowOther: false`
263
+ needs at least two options. A prompt is at most 4 KiB, a label at most 128
264
+ characters, a description at most 1 KiB, and an answer at most 4 KiB; a refusal
265
+ names the field and its bound. At most one option may be recommended.
266
+
267
+ The child commits the request and parks its tool call. The child never opens a
268
+ socket or runs a command for IPC: the durable store is the channel, as it is for
269
+ gates, so an answer survives a restart.
270
+
271
+ In Pi, one warning reads `request <id> asks: <prompt>`, truncated to 120
272
+ characters, and one message enters the parent session with the exact `questions`
273
+ parameter. The message tells the model to ask you with the `question` tool when
274
+ that tool exists, and in plain text otherwise; there is no dependency on
275
+ `@ftrdotdev/pi-inquiry`. The model answers with:
276
+
277
+ ```json
278
+ { "action": "answer", "run": "<run id>", "request": "<request id>", "answer": "staging" },
279
+ ```
280
+
281
+ and declines a cancelled dialog with:
282
+
283
+ ```json
284
+ { "action": "decline", "run": "<run id>", "request": "<request id>" }
285
+ ```
286
+
287
+ `answer` accepts either the run id or a fanout child's run id. The answer is
288
+ normalized before it is stored: ignore case, trim, strip a leading `N. `, accept
289
+ an in-range option number, and store the canonical label. An answer outside the
290
+ options is refused and names the labels. A second answer, an unknown request, a
291
+ wrong run, a declined request and an expired request are refused. `decline`
292
+ cancels the request with reason `declined`; the child gets an error naming the
293
+ reason. A declined request settles the run `denied`, not `answered`: the row and
294
+ the lane counts read denied, and the wait text names it, `Run <id> was denied:
295
+ request <request id> was declined.`
296
+
297
+ After an answer the verb waits like a launch: the run settles, or it asks its
298
+ next request. `delegate_launch` and `delegate_control answer` return at a
299
+ request instead of blocking. `approve` and `deny` still wait for the task, so a
300
+ question that follows a decision reaches you through the alert or
301
+ `/delegates:answer`. A run that waits on a gate and a request names both: `Run
302
+ <id> waits for gate <gates> and request <requests>.`
303
+
304
+ `/delegates:answer` answers without the model. With no arguments it lists the
305
+ pending requests, then asks for a label or free text; cancelling the answer
306
+ dialog declines the request, and cancelling the request picker leaves every
307
+ request pending. With `<run> <request> <answer…>` it answers directly. A
308
+ request that is no longer pending is refused.
309
+
310
+ The deadline is 24 hours from the first call. A record past its deadline reads
311
+ as `expired` without a write, the child's `ask` returns an error, and a late
312
+ answer is refused. `stop` cancels the run's pending requests with reason
313
+ `stopped`; a resume does not resurrect one. Seven days after a request is
314
+ made, the next live start retires the record and its index row. A read-only
315
+ viewer lists pending requests and never announces, answers or prunes.
316
+
317
+ A headless or sandboxed host starts `startRequestAlerts` with its own
318
+ `RequestAlertHost`, or reads the same records with `requestRecords` from the
319
+ store. Without that watcher a request stays pending and the store still lists
320
+ it. The package default, `logOnlyRequestHost`, writes one id-only log line per
321
+ request; the prompt and the answer never enter the log.
322
+
323
+ ## `/delegates`
324
+
325
+ `/delegates` toggles the live inspector in a TUI session. `/delegates:list`
326
+ prints the registered agents, the session's runs, the lane counts, the pending
327
+ gates and the pending requests. Each agent line names its tools and gates.
328
+ `/delegates` and `/delegates:list` only read. The three tools and
329
+ `/delegates:answer` are the writers.
330
+
331
+ ## Register an agent with a gate
332
+
333
+ Presets carry no gates. To gate a tool, register your own definition from a Pi
334
+ extension. From a checkout, [examples/gated.ts](./examples/gated.ts) is a
335
+ one-file version of this recipe; see [examples/README.md](./examples/README.md).
336
+
337
+ Create a local package with a manifest at `./delegates-local/package.json`:
338
+
339
+ ```json
340
+ {
341
+ "name": "delegates-local",
342
+ "private": true,
343
+ "keywords": ["pi-package"],
344
+ "dependencies": {
345
+ "@ftrdotdev/pi-delegates": "^0.1.0"
346
+ },
347
+ "pi": {
348
+ "extensions": ["./deployer.ts"]
349
+ }
350
+ }
351
+ ```
352
+
353
+ The `pi.extensions` field lists `./deployer.ts`. Pi loads that file.
354
+
355
+ Build the definition and pass it to `createDelegatesExtension`:
356
+
357
+ ```ts
358
+ import { createDelegatesExtension } from "@ftrdotdev/pi-delegates";
359
+ import { defineAgent } from "@ftrdotdev/pi-delegates/agents";
360
+
361
+ const deployer = defineAgent({
362
+ name: "deployer",
363
+ role: "Deploy agent",
364
+ prompt: { role: "You deploy the service and report the result." },
365
+ tools: ["read", "bash"],
366
+ extensions: ["coding-tools"],
367
+ approval: {
368
+ gates: [{ name: "deploy", tools: ["bash"], match: "deploy" }],
369
+ },
370
+ });
371
+
372
+ export default createDelegatesExtension({ definitions: [deployer] });
373
+ ```
374
+
375
+ Install the checkout as the local package's dependency. Before the first
376
+ release, the npm name does not resolve. After the release, install the npm
377
+ name:
378
+
379
+ ```bash
380
+ cd ./delegates-local
381
+ # Before the release is published:
382
+ npm install <checkout>/packages/delegates
383
+ # After the release:
384
+ # npm install @ftrdotdev/pi-delegates
385
+ pi install ./delegates-local
386
+ ```
387
+
388
+ Do not also install the checkout as a Pi package. The local extension registers
389
+ the delegate tools, so a second copy registers them twice.
390
+
391
+ Call `delegate_launch`:
392
+
393
+ ```json
394
+ { "request": { "kind": "single", "agent": "deployer", "task": "deploy the service" } }
395
+ ```
396
+
397
+ Repeating the same request in one session returns the run that request already
398
+ started. The run id covers the session and the request content, so a retried
399
+ tool call never spawns a second run. Change the task text to launch again.
400
+
401
+ `match` is a case-sensitive substring of the raw `bash` command and applies to
402
+ `bash` only. A rule with `match` on `read` never parks; omit `match` to park
403
+ every call of a listed tool. The gate name is scoped to the run, so one
404
+ decision covers every child that parks on that name.
405
+
406
+ A matching `bash` command parks the run. Decide the run with the run id that
407
+ the launch returned. Call `delegate_control`:
408
+
409
+ ```json
410
+ { "action": "approve", "run": "<run id>", "gate": "deploy" }
411
+ ```
412
+
413
+ `coding-tools` is the built-in coding extension. The package adds the
414
+ `gate-guard` extension when the definition has rules. A gate belongs to the
415
+ registered definition. An inline definition of the same name can narrow the
416
+ tools, but it cannot remove the rule.
417
+
418
+ ## Configuration
419
+
420
+ `delegates.json` in the Pi agent directory overrides the defaults. A missing
421
+ file uses the defaults. An invalid value falls back to its default and adds a
422
+ diagnostic.
423
+
424
+ ```json
425
+ {
426
+ "caps": { "laneCap": 4, "sessionCeiling": 8 },
427
+ "defaults": { "maxTurns": 40, "launchWaitMs": 600000 }
428
+ }
429
+ ```
430
+
431
+ ## Failure log
432
+
433
+ The package writes `<sessionDir>/delegates.log`, one JSON object per line. It
434
+ records harness reports, config diagnostics, version notes, swallowed errors and
435
+ wait outcomes that are not `settled`. It never holds the request, the task text,
436
+ an answer, a prompt or a command. A broken log path never fails a call.
437
+
438
+ See [OPERATIONS.md](./OPERATIONS.md) for the fields and stable codes.
439
+
440
+ ## Session store
441
+
442
+ The Pi process that owns a session is its only writer. Do not open its SQLite
443
+ store yourself. See [OPERATIONS.md](./OPERATIONS.md) for the path and read-only
444
+ rules.
445
+
446
+ ## Development
447
+
448
+ See [TESTING.md](./TESTING.md) to run the extension from a checkout, and
449
+ [ROADMAP.md](./ROADMAP.md) for deferred work.