@yandy0725/pi-subagents 0.12.0 → 1.0.1
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 +130 -20
- package/README.zh.md +126 -20
- package/package.json +1 -1
- package/src/config/agent-types.ts +13 -9
- package/src/config/custom-agents.ts +17 -7
- package/src/config/default-agents.ts +9 -3
- package/src/config/tool-names.ts +99 -0
- package/src/lifecycle/create-subagent-session.ts +11 -8
- package/src/lifecycle/parent-snapshot.ts +7 -0
- package/src/session/session-config.ts +16 -3
- package/src/tools/agent-tool.ts +4 -5
- package/src/tools/get-result-tool.ts +1 -1
- package/src/types.ts +11 -1
package/README.md
CHANGED
|
@@ -10,14 +10,14 @@ A [pi](https://pi.dev) extension providing a focused, in-process sub-agent core
|
|
|
10
10
|
- **Parent-session accounting** — child model usage joins the parent footer, `/session`, and `/usage` totals, including background and resumed agents
|
|
11
11
|
- **Custom agent types** — define agents in `.pi/agents/<name>.md` with YAML frontmatter: system prompts, model, thinking, tools
|
|
12
12
|
- **Mid-run steering** — inject messages into running agents to redirect work without restarting
|
|
13
|
-
- **Session resume** —
|
|
13
|
+
- **Session resume** — re-prompt the same child session before it is released (10 minutes after the agent finishes), keeping its context
|
|
14
14
|
- **Graceful turn limits** — agents get a "wrap up" warning before hard abort
|
|
15
15
|
- **Case-insensitive types** — `"explore"`, `"Explore"`, `"EXPLORE"` all work
|
|
16
16
|
- **Fuzzy model selection** — specify models by name (`"haiku"`, `"sonnet"`) instead of full IDs
|
|
17
17
|
- **Model tiers** — map `fast` / `standard` / `advanced` to real models in config; the agent is guided to pick the configured model per task
|
|
18
|
-
- **Context inheritance** — optionally
|
|
18
|
+
- **Context inheritance** — optionally prepend the parent conversation, rendered as text, to the sub-agent's prompt
|
|
19
19
|
- **Styled notifications** — background results render as themed notification boxes
|
|
20
|
-
- **Event bus** —
|
|
20
|
+
- **Event bus** — parent-level and child-session lifecycle events via `pi.events` (see [Events](#events))
|
|
21
21
|
|
|
22
22
|
## Install
|
|
23
23
|
|
|
@@ -46,7 +46,7 @@ Foreground agents block until complete. Background agents return an ID immediate
|
|
|
46
46
|
|
|
47
47
|
| Type | Tools | Model | Description |
|
|
48
48
|
|------|-------|-------|-------------|
|
|
49
|
-
| `general-purpose` |
|
|
49
|
+
| `general-purpose` | inherit the parent's tools, dispatch tools stripped | inherit | Harness-rendered prompt for the child's own tool set — same conventions, plus the sub-agent bridge |
|
|
50
50
|
| `Explore` | read, bash, grep, find, ls | inherit (overridable per call) | Fast codebase exploration (read-only) |
|
|
51
51
|
|
|
52
52
|
## Custom Agents
|
|
@@ -66,10 +66,55 @@ timeout: 300
|
|
|
66
66
|
You are a security auditor. Review code for vulnerabilities...
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
Agents are discovered from
|
|
69
|
+
Agents are discovered from `<cwd>/.pi/agents/<name>.md` (project) and `$PI_CODING_AGENT_DIR/agents/<name>.md` (global, default `~/.pi/agent/agents/`). Project-level overrides global; the file name is the agent type name.
|
|
70
|
+
|
|
71
|
+
### Frontmatter fields
|
|
72
|
+
|
|
73
|
+
| Field | Value | Default | Meaning |
|
|
74
|
+
|-------|-------|---------|---------|
|
|
75
|
+
| `description` | string | file name | One-line description shown in the `subagent` tool's type list |
|
|
76
|
+
| `display_name` | string | type name | Short name used in the widget, notifications, and results |
|
|
77
|
+
| `tools` | CSV / `+name` / `-name` / `none` | inherit parent | Tool set — see below |
|
|
78
|
+
| `model` | string | inherit parent | `provider/modelId` or fuzzy name; when pinned, wins over the per-call `model` |
|
|
79
|
+
| `thinking` | string | inherit parent | off / minimal / low / medium / high / xhigh |
|
|
80
|
+
| `max_turns` | integer | unlimited | Cap for this type; wins over the per-call `max_turns` |
|
|
81
|
+
| `timeout` | integer | unlimited | Wall-clock seconds cap (`0` = unlimited); wins over the per-call `timeout` |
|
|
82
|
+
| `prompt_mode` | `append` / `replace` | `append` | How the body relates to the base prompt — see the next section |
|
|
83
|
+
| `inherit_context` | boolean | unset | Default when a call omits `inherit_context` |
|
|
84
|
+
| `run_in_background` | boolean | unset | Default when a call omits `run_in_background` |
|
|
85
|
+
| `enabled` | boolean | `true` | `false` keeps the type in the registry but makes it unspawnable and hides it from type lists |
|
|
86
|
+
|
|
87
|
+
`inherit_context` / `run_in_background` set this type's strategy, and a value written here **locks** it: like `max_turns` and `timeout`, the frontmatter wins over the per-call argument. The built-in agents deliberately omit both so the caller decides; omitted, they default to `false`.
|
|
70
88
|
|
|
71
89
|
`max_turns` and `timeout` (wall-clock seconds, `0` = unlimited) lock their limits for the agent type: a frontmatter value wins over the per-call parameter.
|
|
72
90
|
|
|
91
|
+
### The `tools` allowlist
|
|
92
|
+
|
|
93
|
+
`tools` decides the agent's tool set. There are four spellings:
|
|
94
|
+
|
|
95
|
+
| Frontmatter | Agent gets |
|
|
96
|
+
|-------------|------------|
|
|
97
|
+
| field omitted | The parent session's **currently active** tools, minus this package's dispatch tools (`subagent`, `get_subagent_result`, `steer_subagent`) |
|
|
98
|
+
| `tools: none`, or `tools:` (empty) | **Zero tools**: the model only produces text and no tool call is accepted |
|
|
99
|
+
| `tools: read, grep` | Exactly those tools, by exact name |
|
|
100
|
+
| `tools: -edit, -write, +powershell` | The inherited set edited in order (`-name` removes, `+name` adds), then the dispatch tools stripped |
|
|
101
|
+
|
|
102
|
+
There is no "hand the model every built-in tool" fallback: an omitted field inherits what the parent session has bound at that moment, so a tool the parent hid with `/tools` or `--tools` is not available to the child either.
|
|
103
|
+
|
|
104
|
+
Every list entry must be an exact tool name — `*` is rejected. Mixing plain names with `+name` / `-name` entries is rejected as well. Both errors name the agent type and are raised before any session is created.
|
|
105
|
+
|
|
106
|
+
That rule and the new default are breaking for agents written against the previous release: an omitted `tools` used to mean a fixed list of built-in tool names rather than inheritance, and a `*` entry used to be forwarded to the host verbatim, where it silently matched. If an agent relied on the old omitted-field list, state the set, or narrow the inherited one with `-name`.
|
|
107
|
+
|
|
108
|
+
The dispatch tools of this package are stripped from every form, so a child can never spawn children. An empty list is never treated as "unset" and never falls back to anything.
|
|
109
|
+
|
|
110
|
+
```markdown
|
|
111
|
+
---
|
|
112
|
+
tools: -edit, -write, -bash # read-only research: inherit everything except the writers
|
|
113
|
+
---
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Inheriting keeps the child in step with the parent, at the cost of listing those tools in its prompt; naming a list (or `none`) pins the capability to the agent type.
|
|
117
|
+
|
|
73
118
|
### Prompt mode
|
|
74
119
|
|
|
75
120
|
`prompt_mode` decides how the agent body relates to the system prompt of the agent that invokes it:
|
|
@@ -99,8 +144,8 @@ Launch a sub-agent.
|
|
|
99
144
|
| `max_turns` | number | no | Max agentic turns (unlimited by default) |
|
|
100
145
|
| `timeout` | number | no | Hard wall-clock limit in seconds; the agent is killed at the deadline and partial output is returned (unlimited by default) |
|
|
101
146
|
| `run_in_background` | boolean | no | Run without blocking |
|
|
102
|
-
| `resume` | string | no | Agent ID to resume a previous session |
|
|
103
|
-
| `inherit_context` | boolean | no |
|
|
147
|
+
| `resume` | string | no | Agent ID to resume a previous session (limits in "Session resume" below) |
|
|
148
|
+
| `inherit_context` | boolean | no | Prepend the parent conversation, rendered as text, to this prompt (not a fork of the parent branch) |
|
|
104
149
|
|
|
105
150
|
### `get_subagent_result`
|
|
106
151
|
|
|
@@ -109,7 +154,7 @@ Check status and retrieve results from a background agent.
|
|
|
109
154
|
| Parameter | Type | Required | Description |
|
|
110
155
|
|-----------|------|----------|-------------|
|
|
111
156
|
| `agent_id` | string | yes | Agent ID to check |
|
|
112
|
-
| `wait` | boolean | no | Wait for completion |
|
|
157
|
+
| `wait` | boolean | no | Wait for completion; only effective while the agent is **running** — a queued agent returns immediately |
|
|
113
158
|
| `verbose` | boolean | no | Include full conversation log |
|
|
114
159
|
|
|
115
160
|
### `steer_subagent`
|
|
@@ -135,7 +180,7 @@ tier when a task calls for a specific cost or capability level — there is no
|
|
|
135
180
|
| `standard` | Balanced default (sonnet class) |
|
|
136
181
|
| `advanced` | Most capable, most expensive (opus class) |
|
|
137
182
|
|
|
138
|
-
The mapping lives in `subagents.json` — `~/.pi/agent/subagents.json`
|
|
183
|
+
The mapping lives in `subagents.json` — `$PI_CODING_AGENT_DIR/subagents.json` (global, default `~/.pi/agent/subagents.json`) and
|
|
139
184
|
`<cwd>/.pi/subagents.json` (project). The two layers merge **per tier**, so a project can
|
|
140
185
|
override one tier without restating the others:
|
|
141
186
|
|
|
@@ -181,31 +226,96 @@ Background agents are subject to a configurable concurrency limit (default: 4).
|
|
|
181
226
|
|
|
182
227
|
## Persistent Settings
|
|
183
228
|
|
|
184
|
-
Settings set via `/subagents:settings` persist in `<cwd>/.pi/subagents.json` (project) and can be overridden globally in `~/.pi/agent/subagents.json
|
|
229
|
+
Settings set via `/subagents:settings` persist in `<cwd>/.pi/subagents.json` (project) and can be overridden globally in `$PI_CODING_AGENT_DIR/subagents.json` (default `~/.pi/agent/subagents.json`). The same files also hold the [`modelTiers`](#model-tiers) mapping; saving other settings preserves it.
|
|
185
230
|
|
|
186
231
|
| Setting | Meaning |
|
|
187
232
|
|---------|---------|
|
|
188
233
|
| `maxConcurrent` | Background agents allowed to run at once |
|
|
189
234
|
| `defaultMaxTurns` | Default max agentic turns (`0` = unlimited) |
|
|
190
|
-
| `defaultTimeoutSeconds` | Default hard wall-clock timeout in seconds (`0` = unlimited
|
|
235
|
+
| `defaultTimeoutSeconds` | Default hard wall-clock timeout in seconds (`0` = unlimited; the settings file requires an integer in `0`–`86400`, out-of-range values are dropped) |
|
|
191
236
|
| `graceTurns` | Turns allowed after the wrap-up steer |
|
|
192
237
|
|
|
193
238
|
`defaultMaxTurns` / `defaultTimeoutSeconds` sit at the bottom of the precedence chain: agent frontmatter wins over the per-call parameter, which wins over these defaults.
|
|
194
239
|
|
|
240
|
+
The `86400` ceiling only constrains the **settings file** (`sanitize` drops non-integers or values outside `0`–`86400`). The per-call `timeout` parameter only requires ≥ 1 and frontmatter only a non-negative integer; neither is capped.
|
|
241
|
+
|
|
195
242
|
## Events
|
|
196
243
|
|
|
197
|
-
|
|
244
|
+
Two levels of events are emitted via `pi.events` for other extensions to consume.
|
|
245
|
+
|
|
246
|
+
Agent-level events (state changes of one agent run):
|
|
247
|
+
|
|
248
|
+
| Event | When | Scope |
|
|
249
|
+
|-------|------|-------|
|
|
250
|
+
| `subagents:created` | Agent registered | **Background only** (before queue admission) |
|
|
251
|
+
| `subagents:started` | Agent transitions to running (including out of the queue) | Background and foreground |
|
|
252
|
+
| `subagents:completed` | Agent finished successfully | **Background only** |
|
|
253
|
+
| `subagents:failed` | Agent errored, stopped, or aborted (also when aborting a queued agent) | **Background only** |
|
|
254
|
+
| `subagents:steered` | Steering message sent | Running agents, foreground or background |
|
|
255
|
+
| `subagents:compacted` | Agent session compacted | Background and foreground |
|
|
256
|
+
| `subagents:settings_loaded` | Persisted settings applied | Once at extension load |
|
|
257
|
+
| `subagents:settings_changed` | Settings mutation applied | Every settings write |
|
|
258
|
+
|
|
259
|
+
Foreground runs never emit `completed` / `failed`: the `subagent` tool returns their result directly.
|
|
260
|
+
Use a background agent when you need completion events.
|
|
261
|
+
|
|
262
|
+
Child-session events (the child session itself being created and torn down — not the same thing as a run):
|
|
198
263
|
|
|
199
264
|
| Event | When |
|
|
200
265
|
|-------|------|
|
|
201
|
-
| `subagents:
|
|
202
|
-
| `subagents:
|
|
203
|
-
| `subagents:completed` |
|
|
204
|
-
| `subagents:
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
266
|
+
| `subagents:child:spawning` | A child run starts, before its session exists |
|
|
267
|
+
| `subagents:child:session-created` | Child session created, immediately before extension binding (payload carries `sessionId`; subscribers must register synchronously) |
|
|
268
|
+
| `subagents:child:completed` | The child prompt resolved (normal, steered to wrap up, or hard-aborted) |
|
|
269
|
+
| `subagents:child:disposed` | The child session record is torn down (eviction sweep, session switch, extension shutdown, bind failure) — **not** emitted at the end of every run |
|
|
270
|
+
|
|
271
|
+
## Session resume
|
|
272
|
+
|
|
273
|
+
`resume` can only continue a session that is **still alive in memory**: once the record is cleaned up (see below)
|
|
274
|
+
the call returns not found, and an agent whose session was never created returns no active session. Resuming just
|
|
275
|
+
re-prompts the same session, so these parameters have **no effect** on that call: `model`, `thinking`,
|
|
276
|
+
`max_turns`, `timeout`, `inherit_context` (the session was created with the original configuration). Resume also has
|
|
277
|
+
no turn or wall-clock guard — only the parent's abort signal applies.
|
|
278
|
+
|
|
279
|
+
## Transcripts and cleanup
|
|
280
|
+
|
|
281
|
+
A child transcript lives inside the parent session directory:
|
|
282
|
+
`<parent session dir>/<parent file name without .jsonl>/tasks/`. When the parent session is not persisted
|
|
283
|
+
(headless / API mode) it falls back to `$TMPDIR/pi-subagents-<uid>/<encoded cwd>/tasks/`.
|
|
284
|
+
|
|
285
|
+
10 minutes after an agent finishes it is cleaned up: the session is disposed (emitting `subagents:child:disposed`).
|
|
286
|
+
Only sessions that reached disk leave a navigable record behind for `/subagents:sessions` to re-read.
|
|
287
|
+
|
|
288
|
+
## Execution constraints for children
|
|
289
|
+
|
|
290
|
+
- Children cannot spawn grandchildren: `subagent` / `get_subagent_result` / `steer_subagent` are removed from the active
|
|
291
|
+
tool set at bind time, including when another extension registered them.
|
|
292
|
+
- Children always run in the parent session's cwd; the `subagent` tool has no `cwd` parameter.
|
|
293
|
+
|
|
294
|
+
## Cross-extension API
|
|
295
|
+
|
|
296
|
+
Other extensions can drive the sub-agent core without the tools (the package exports an entry point):
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
const { getSubagentsService } = await import("@yandy0725/pi-subagents");
|
|
300
|
+
const svc = getSubagentsService();
|
|
301
|
+
const id = svc?.spawn("Explore", "Check for stale TODOs", { description: "Find stale TODOs", foreground: true });
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
| Method | Description |
|
|
305
|
+
|--------|-------------|
|
|
306
|
+
| `spawn(type, prompt, options?)` | Start an agent and return its ID immediately (throws outside a session) |
|
|
307
|
+
| `getRecord(id)` | Serializable snapshot of current state (no live session objects) |
|
|
308
|
+
| `listAgents()` | All tracked agents, most recent first |
|
|
309
|
+
| `abort(id)` | Abort a running or queued agent |
|
|
310
|
+
| `steer(id, message)` | Send a steering message to a running agent |
|
|
311
|
+
| `waitForAll()` | Wait for all running and queued agents to complete |
|
|
312
|
+
| `hasRunning()` | Whether any agents are running or queued |
|
|
313
|
+
|
|
314
|
+
`SpawnOptions`: `description`, `model`, `maxTurns`, `timeoutSeconds` (`0`/omitted = unlimited, normalized at the API
|
|
315
|
+
boundary), `thinkingLevel`, `inheritContext`, `foreground`, `bypassQueue`. The two queue-related options differ:
|
|
316
|
+
`bypassQueue` only skips the concurrency limiter, while `foreground` also means "not background" (no
|
|
317
|
+
`created` / `completed` / `failed` events and no completion notification). `spawn` still returns the ID immediately
|
|
318
|
+
in both cases; read results through `getRecord`.
|
|
209
319
|
|
|
210
320
|
## Development
|
|
211
321
|
|
package/README.zh.md
CHANGED
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
- **父会话记账** — 子代理模型用量计入父会话 footer、`/session` 和 `/usage` 总花销,覆盖后台与恢复运行的代理
|
|
11
11
|
- **自定义代理类型** — 在 `.pi/agents/<name>.md` 中通过 YAML frontmatter 定义:系统提示词、模型、思考等级、工具限制
|
|
12
12
|
- **运行中转向** — 向运行中的代理注入消息,无需重启即可改变工作方向
|
|
13
|
-
- **会话恢复** —
|
|
13
|
+
- **会话恢复** — 在子会话被释放前(默认结束后 10 分钟)继续同一会话,保留完整上下文
|
|
14
14
|
- **优雅轮次限制** — 代理在硬中止前收到"收尾"警告
|
|
15
15
|
- **大小写不敏感** — `"explore"`、`"Explore"`、`"EXPLORE"` 均可
|
|
16
16
|
- **模糊模型选择** — 按名称(`"haiku"`、`"sonnet"`)而非完整 ID 指定模型
|
|
17
17
|
- **模型分层** — 在配置里把 `fast` / `standard` / `advanced` 档位映射到真实模型,代理按提示选择对应模型
|
|
18
|
-
- **上下文继承** —
|
|
18
|
+
- **上下文继承** — 可选择把父对话摘要以文本形式前置到子代理的提示词
|
|
19
19
|
- **风格化通知** — 后台结果渲染为主题化通知框
|
|
20
|
-
- **事件总线** — 通过 `pi.events`
|
|
20
|
+
- **事件总线** — 通过 `pi.events` 发出父级与子级生命周期事件(见[事件](#事件))
|
|
21
21
|
|
|
22
22
|
## 安装
|
|
23
23
|
|
|
@@ -46,7 +46,7 @@ subagent({
|
|
|
46
46
|
|
|
47
47
|
| 类型 | 工具 | 模型 | 描述 |
|
|
48
48
|
|------|------|------|------|
|
|
49
|
-
| `general-purpose` |
|
|
49
|
+
| `general-purpose` | 继承父会话工具集(剔除 subagent 相关工具) | 继承父级 | harness 按子代理自身工具集渲染的提示词——沿用相同约定,外加子代理桥接段 |
|
|
50
50
|
| `Explore` | read, bash, grep, find, ls | 继承父级模型(可按调用覆盖) | 快速代码库探索(只读) |
|
|
51
51
|
|
|
52
52
|
## 自定义代理
|
|
@@ -66,10 +66,55 @@ timeout: 300
|
|
|
66
66
|
你是一名安全审计员。审查代码漏洞...
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
代理从项目级 `<cwd>/.pi/agents/<name>.md` 和全局级 `$PI_CODING_AGENT_DIR/agents/<name>.md`(默认 `~/.pi/agent/agents/`)发现。项目级覆盖全局;文件名即代理类型名。
|
|
70
|
+
|
|
71
|
+
### frontmatter 字段
|
|
72
|
+
|
|
73
|
+
| 字段 | 取值 | 默认 | 含义 |
|
|
74
|
+
|------|------|------|------|
|
|
75
|
+
| `description` | string | 文件名 | 一句话描述,出现在 `subagent` 工具的可用类型列表里 |
|
|
76
|
+
| `display_name` | string | 类型名 | widget、通知与结果里显示的短名字 |
|
|
77
|
+
| `tools` | CSV / `+name` / `-name` / `none` | 继承父会话 | 工具集,见下方 |
|
|
78
|
+
| `model` | string | 继承父级 | `provider/modelId` 或模糊名称;写死时优先于每次调用的 `model` |
|
|
79
|
+
| `thinking` | string | 继承父级 | off / minimal / low / medium / high / xhigh |
|
|
80
|
+
| `max_turns` | integer | 无限制 | 该类型的轮次上限,优先于每次调用的 `max_turns` |
|
|
81
|
+
| `timeout` | integer | 无限制 | 墙钟秒数上限(`0` = 无限制),优先于每次调用的 `timeout` |
|
|
82
|
+
| `prompt_mode` | `append` / `replace` | `append` | 正文与基础提示词的关系,见下节 |
|
|
83
|
+
| `inherit_context` | boolean | 不指定 | 每次调用没有 `inherit_context` 时的默认值 |
|
|
84
|
+
| `run_in_background` | boolean | 不指定 | 每次调用没有 `run_in_background` 时的默认值 |
|
|
85
|
+
| `enabled` | boolean | `true` | `false` 时该类型仍在注册表里,但不可启动、也不出现在类型列表中 |
|
|
86
|
+
|
|
87
|
+
`inherit_context` / `run_in_background` 决定该类型的策略,写在 frontmatter 里的值会**锁定**它:和 `max_turns`、`timeout` 一样,frontmatter 优先于每次调用传入的参数。两个内置代理刻意不写它们,把决定权留给调用方;不写时默认 `false`。
|
|
70
88
|
|
|
71
89
|
`max_turns` 与 `timeout`(墙钟秒数,`0` = 无限制)会锁定该代理类型的上限:frontmatter 中的值优先于每次调用传入的参数。
|
|
72
90
|
|
|
91
|
+
### `tools` 白名单
|
|
92
|
+
|
|
93
|
+
`tools` 决定这个代理类型的工具集,共四种写法:
|
|
94
|
+
|
|
95
|
+
| frontmatter | 子代理拿到 |
|
|
96
|
+
|-------------|------------|
|
|
97
|
+
| 不写该字段 | 父会话**当前活动**工具集,剔除本包的派发工具(`subagent`、`get_subagent_result`、`steer_subagent`) |
|
|
98
|
+
| `tools: none`,或 `tools:`(空值) | **零个工具**:模型只能产出文本,任何工具调用都不会被接受 |
|
|
99
|
+
| `tools: read, grep` | 恰好这些工具,按精确名字 |
|
|
100
|
+
| `tools: -edit, -write, +powershell` | 继承集按顺序增删(`-name` 移除,`+name` 添加),再剔除派发工具 |
|
|
101
|
+
|
|
102
|
+
不存在“把内置工具全给”这种回退:不写字段就是继承父会话此刻绑定的工具集,所以父会话用 `/tools` 或 `--tools` 隐藏的工具,子代理也拿不到。
|
|
103
|
+
|
|
104
|
+
列表条目必须是精确的工具名——`*` 一律报错。普通名字与 `+name` / `-name` 混写同样报错。这两种错误都会点出是哪个代理类型,且在任何会话创建之前抛出。
|
|
105
|
+
|
|
106
|
+
这条规则和新的默认值对按上一版写的代理类型都是破坏性变更:不写 `tools` 以前表示一份固定的内置工具名单,不是继承;`*` 条目以前会原样转交给宿主,静默生效。如果某个代理依赖过去那种“不写就是固定名单”的行为,请自己写出工具集,或用 `-name` 收窄继承来的集合。
|
|
107
|
+
|
|
108
|
+
无论哪种形式,本包的派发工具都会被剔除,因此子代理永远不能再起子代理。空列表不会被当成“未配置”,不会回落到任何东西。
|
|
109
|
+
|
|
110
|
+
```markdown
|
|
111
|
+
---
|
|
112
|
+
tools: -edit, -write, -bash # 只读调研:继承一切,去掉会写文件/起 shell 的工具
|
|
113
|
+
---
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
继承让子代理与父会话保持一致,代价是这些工具会进到它的提示词里;写一份列表(或 `none`)则把能力钉死在这个代理类型上。
|
|
117
|
+
|
|
73
118
|
### 提示词模式
|
|
74
119
|
|
|
75
120
|
`prompt_mode` 决定代理正文与“调用它的代理的系统提示词”之间的关系:
|
|
@@ -99,8 +144,8 @@ timeout: 300
|
|
|
99
144
|
| `max_turns` | number | 否 | 最大代理轮次(默认无限制) |
|
|
100
145
|
| `timeout` | number | 否 | 墙钟硬性时限(秒);到点即终止代理并返回已产出的部分输出(默认无限制) |
|
|
101
146
|
| `run_in_background` | boolean | 否 | 后台运行不阻塞 |
|
|
102
|
-
| `resume` | string | 否 | 恢复之前会话的代理 ID |
|
|
103
|
-
| `inherit_context` | boolean | 否 |
|
|
147
|
+
| `resume` | string | 否 | 恢复之前会话的代理 ID(限制见下文「会话恢复」) |
|
|
148
|
+
| `inherit_context` | boolean | 否 | 把父对话摘要以文本形式前置到该次提示词(不是分叉父分支) |
|
|
104
149
|
|
|
105
150
|
### `get_subagent_result`
|
|
106
151
|
|
|
@@ -109,7 +154,7 @@ timeout: 300
|
|
|
109
154
|
| 参数 | 类型 | 必填 | 描述 |
|
|
110
155
|
|------|------|------|------|
|
|
111
156
|
| `agent_id` | string | 是 | 要检查的代理 ID |
|
|
112
|
-
| `wait` | boolean | 否 |
|
|
157
|
+
| `wait` | boolean | 否 | 等待完成;只对**正在运行**的代理生效,排队中的代理会立即返回 |
|
|
113
158
|
| `verbose` | boolean | 否 | 包含完整对话日志 |
|
|
114
159
|
|
|
115
160
|
### `steer_subagent`
|
|
@@ -133,7 +178,7 @@ timeout: 300
|
|
|
133
178
|
| `standard` | 均衡的默认档(sonnet 级) |
|
|
134
179
|
| `advanced` | 能力最强、最贵(opus 级) |
|
|
135
180
|
|
|
136
|
-
映射写在 `subagents.json`:全局 `~/.pi/agent/subagents.json
|
|
181
|
+
映射写在 `subagents.json`:全局 `$PI_CODING_AGENT_DIR/subagents.json`(默认 `~/.pi/agent/subagents.json`),项目 `<cwd>/.pi/subagents.json`。
|
|
137
182
|
两层**按档位合并**,项目只改一档时无需重复其余档位:
|
|
138
183
|
|
|
139
184
|
```json
|
|
@@ -173,31 +218,92 @@ timeout: 300
|
|
|
173
218
|
|
|
174
219
|
## 持久化设置
|
|
175
220
|
|
|
176
|
-
通过 `/subagents:settings` 设置的参数持久化到 `<cwd>/.pi/subagents.json
|
|
221
|
+
通过 `/subagents:settings` 设置的参数持久化到 `<cwd>/.pi/subagents.json`(项目级),也可在全局 `$PI_CODING_AGENT_DIR/subagents.json`(默认 `~/.pi/agent/subagents.json`)中设置全局默认值。同一批文件也保存 [`modelTiers`](#模型分层) 映射;保存其他设置时会保留它。
|
|
177
222
|
|
|
178
223
|
| 设置 | 含义 |
|
|
179
224
|
|------|------|
|
|
180
225
|
| `maxConcurrent` | 允许同时运行的后台代理数 |
|
|
181
226
|
| `defaultMaxTurns` | 默认最大代理轮次(`0` = 无限制) |
|
|
182
|
-
| `defaultTimeoutSeconds` | 默认墙钟硬性超时秒数(`0` =
|
|
227
|
+
| `defaultTimeoutSeconds` | 默认墙钟硬性超时秒数(`0` = 无限制;设置文件里必须是 `0`–`86400` 的整数,超范围的值会被丢弃) |
|
|
183
228
|
| `graceTurns` | 收到"收尾"转向后仍允许的轮次 |
|
|
184
229
|
|
|
185
230
|
`defaultMaxTurns` / `defaultTimeoutSeconds` 位于优先级链最末端:代理 frontmatter 优先于每次调用参数,每次调用参数又优先于这两个默认值。
|
|
186
231
|
|
|
232
|
+
`86400` 这个上限只约束**设置文件**(`sanitize` 丢弃非整数或超出 `0`–`86400` 的值);每次调用的 `timeout` 参数只要求 ≥ 1,frontmatter 只要求非负整数,两者不设上限,会原样生效。
|
|
233
|
+
|
|
187
234
|
## 事件
|
|
188
235
|
|
|
189
|
-
通过 `pi.events`
|
|
236
|
+
通过 `pi.events` 发出两级事件,供其他扩展消费。
|
|
237
|
+
|
|
238
|
+
代理级事件(每个代理的状态变化):
|
|
239
|
+
|
|
240
|
+
| 事件 | 触发时机 | 作用域 |
|
|
241
|
+
|------|----------|--------|
|
|
242
|
+
| `subagents:created` | 代理已注册 | **仅后台**(在排队准入之前) |
|
|
243
|
+
| `subagents:started` | 代理转入运行状态(含从队列转出) | 后台与前台 |
|
|
244
|
+
| `subagents:completed` | 代理成功结束 | **仅后台** |
|
|
245
|
+
| `subagents:failed` | 代理出错/停止/中止(含中止排队中的代理) | **仅后台** |
|
|
246
|
+
| `subagents:steered` | 转向消息已发送 | 运行中的代理(前后台均可) |
|
|
247
|
+
| `subagents:compacted` | 代理会话已压缩 | 后台与前台 |
|
|
248
|
+
| `subagents:settings_loaded` | 持久化设置已应用 | 扩展加载时一次 |
|
|
249
|
+
| `subagents:settings_changed` | 设置变更已生效 | 每次设置写入 |
|
|
250
|
+
|
|
251
|
+
前台代理的结束不会发 `completed` / `failed`:前台调用的结果直接由 `subagent` 工具返回,
|
|
252
|
+
需要结束事件的场景请用后台代理。
|
|
253
|
+
|
|
254
|
+
子会话级事件(子会话本身的创建与拆除,与“运行”不是一回事):
|
|
190
255
|
|
|
191
256
|
| 事件 | 触发时机 |
|
|
192
257
|
|------|----------|
|
|
193
|
-
| `subagents:
|
|
194
|
-
| `subagents:
|
|
195
|
-
| `subagents:completed` |
|
|
196
|
-
| `subagents:
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
258
|
+
| `subagents:child:spawning` | 一次子运行开始、会话还未创建时 |
|
|
259
|
+
| `subagents:child:session-created` | 子会话已创建、扩展绑定之前(载荷含 `sessionId`,订阅者必须同步登记) |
|
|
260
|
+
| `subagents:child:completed` | 子提示词已结束(正常、被转向收尾或硬中止) |
|
|
261
|
+
| `subagents:child:disposed` | 子会话记录被拆除(退出清理、会话切换、扩展关闭、绑定失败);**不是**每次运行结束时都发 |
|
|
262
|
+
|
|
263
|
+
## 会话恢复
|
|
264
|
+
|
|
265
|
+
`resume` 只能续跑**仍在内存中存活**的会话:记录被清理后(见下节)会返回 not found,会话尚未建立的代理会返回 no active session。
|
|
266
|
+
续跑实际上只是向同一个会话重发提示词,因此这些参数在这次调用里**都不生效**:`model`、
|
|
267
|
+
`thinking`、`max_turns`、`timeout`、`inherit_context`(会话已按首次启动时的配置创建)。
|
|
268
|
+
续跑也没有轮次与墙钟保护,只受父会话中断信号约束。
|
|
269
|
+
|
|
270
|
+
## 会话记录与清理
|
|
271
|
+
|
|
272
|
+
子代理的 transcript 写在父会话目录内:`<父会话所在目录>/<父会话文件名去掉 .jsonl>/tasks/`。
|
|
273
|
+
父会话未持久化时(headless / API 模式)退到 `$TMPDIR/pi-subagents-<uid>/<编码后的 cwd>/tasks/`。
|
|
274
|
+
|
|
275
|
+
代理结束 10 分钟后被清理:释放会话(触发 `subagents:child:disposed`)。只有已落盘的会话会留下一条可导航记录,
|
|
276
|
+
供 `/subagents:sessions` 重新读取。
|
|
277
|
+
|
|
278
|
+
## 子代理的执行约束
|
|
279
|
+
|
|
280
|
+
- 子代理不能再起子代理:绑定时从活动工具集剔除 `subagent` / `get_subagent_result` / `steer_subagent`(扩展注册的工具也不例外)。
|
|
281
|
+
- 子代理固定运行在父会话的 cwd;`subagent` 工具没有 `cwd` 参数。
|
|
282
|
+
|
|
283
|
+
## 跨扩展 API
|
|
284
|
+
|
|
285
|
+
其他扩展可以绕过工具直接调用子代理核心(包已导出入口):
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
const { getSubagentsService } = await import("@yandy0725/pi-subagents");
|
|
289
|
+
const svc = getSubagentsService();
|
|
290
|
+
const id = svc?.spawn("Explore", "Check for stale TODOs", { description: "Find stale TODOs", foreground: true });
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
| 方法 | 说明 |
|
|
294
|
+
|------|------|
|
|
295
|
+
| `spawn(type, prompt, options?)` | 启动代理并立即返回 ID(不在无会话时调用,会抛错) |
|
|
296
|
+
| `getRecord(id)` | 当前状态的序列化快照(无活动会话对象) |
|
|
297
|
+
| `listAgents()` | 所有受管代理,最近的在前 |
|
|
298
|
+
| `abort(id)` | 中止运行中或排队中的代理 |
|
|
299
|
+
| `steer(id, message)` | 向运行中的代理投递转向消息 |
|
|
300
|
+
| `waitForAll()` | 等待所有运行中/排队中的代理结束 |
|
|
301
|
+
| `hasRunning()` | 是否有代理仍在运行或排队 |
|
|
302
|
+
|
|
303
|
+
`SpawnOptions`:`description`、`model`、`maxTurns`、`timeoutSeconds`(`0`/省略 = 无限制,在 API 边界归一化)、
|
|
304
|
+
`thinkingLevel`、`inheritContext`、`foreground`、`bypassQueue`。两个队列相关选项语义不同:
|
|
305
|
+
`bypassQueue` 只跳过并发限流;`foreground` 同时表示“非后台”(不发 `created` / `completed` / `failed` 事件,也不推完成通知),
|
|
306
|
+
但 `spawn` 仍然立即返回 ID,结果通过 `getRecord` 获取。
|
|
201
307
|
|
|
202
308
|
## 开发
|
|
203
309
|
|
package/package.json
CHANGED
|
@@ -16,7 +16,8 @@ import type { AgentConfig } from "../types";
|
|
|
16
16
|
*/
|
|
17
17
|
export interface AgentConfigLookup {
|
|
18
18
|
resolveAgentConfig(type: string): AgentConfig;
|
|
19
|
-
|
|
19
|
+
/** Tool allowlist, or `undefined` to inherit the parent session's tools. */
|
|
20
|
+
getToolNamesForType(type: string): string[] | undefined;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
// ── AgentTypeRegistry class ──────────────────────────────────────────────────
|
|
@@ -84,13 +85,20 @@ export class AgentTypeRegistry implements AgentConfigLookup {
|
|
|
84
85
|
return this.agents.get(key)?.enabled !== false;
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Get the tool allowlist for a type (case-insensitive).
|
|
90
|
+
*
|
|
91
|
+
* `undefined` = inherit: the child gets the parent's captured active set,
|
|
92
|
+
* minus this package's dispatch tools. When the parent's active set is
|
|
93
|
+
* unknown, resolution falls back to the host's default set plus every loaded
|
|
94
|
+
* extension tool. `[]` = explicitly no tools. A non-empty list = exactly
|
|
95
|
+
* those names.
|
|
96
|
+
*/
|
|
97
|
+
getToolNamesForType(type: string): string[] | undefined {
|
|
89
98
|
const key = this.resolveKey(type);
|
|
90
99
|
const raw = key ? this.agents.get(key) : undefined;
|
|
91
100
|
const config = raw?.enabled !== false ? raw : undefined;
|
|
92
|
-
|
|
93
|
-
return names;
|
|
101
|
+
return config?.toolNames === undefined ? undefined : [...config.toolNames];
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
/** Resolve agent config with guaranteed non-null return. Falls back: unknown → general-purpose → absolute fallback. */
|
|
@@ -107,7 +115,6 @@ export class AgentTypeRegistry implements AgentConfigLookup {
|
|
|
107
115
|
name: type,
|
|
108
116
|
displayName: "Agent",
|
|
109
117
|
description: "General-purpose agent for complex, multi-step tasks",
|
|
110
|
-
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
111
118
|
systemPrompt: "",
|
|
112
119
|
promptMode: "append",
|
|
113
120
|
};
|
|
@@ -122,6 +129,3 @@ export class AgentTypeRegistry implements AgentConfigLookup {
|
|
|
122
129
|
return undefined;
|
|
123
130
|
}
|
|
124
131
|
}
|
|
125
|
-
|
|
126
|
-
/** All known built-in tool names. */
|
|
127
|
-
export const BUILTIN_TOOL_NAMES: string[] = ["read", "bash", "edit", "write", "grep", "find", "ls"];
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
6
6
|
import { basename, join } from "node:path";
|
|
7
7
|
import { getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent";
|
|
8
|
-
import { BUILTIN_TOOL_NAMES } from "../config/agent-types";
|
|
9
8
|
import { debugLog } from "../debug";
|
|
10
9
|
import type { AgentConfig, ThinkingLevel } from "../types";
|
|
11
10
|
|
|
@@ -57,7 +56,7 @@ function loadFromDir(dir: string, agents: Map<string, AgentConfig>, source: "pro
|
|
|
57
56
|
name,
|
|
58
57
|
displayName: str(fm.display_name),
|
|
59
58
|
description: str(fm.description) ?? name,
|
|
60
|
-
|
|
59
|
+
toolNames: parseToolList(fm.tools),
|
|
61
60
|
model: str(fm.model),
|
|
62
61
|
thinking: str(fm.thinking) as ThinkingLevel | undefined,
|
|
63
62
|
maxTurns: nonNegativeInt(fm.max_turns),
|
|
@@ -86,7 +85,7 @@ function nonNegativeInt(val: unknown): number | undefined {
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
/**
|
|
89
|
-
* Parse a
|
|
88
|
+
* Parse a comma-separated list field into items, or undefined if absent/empty/"none".
|
|
90
89
|
*/
|
|
91
90
|
function parseCsvField(val: unknown): string[] | undefined {
|
|
92
91
|
if (val === undefined || val === null) return undefined;
|
|
@@ -101,10 +100,21 @@ function parseCsvField(val: unknown): string[] | undefined {
|
|
|
101
100
|
}
|
|
102
101
|
|
|
103
102
|
/**
|
|
104
|
-
* Parse
|
|
105
|
-
*
|
|
103
|
+
* Parse the `tools` field, preserving the difference between "not configured"
|
|
104
|
+
* and "explicitly no tools":
|
|
105
|
+
*
|
|
106
|
+
* omitted (key absent) → undefined (inherit the parent session's tools)
|
|
107
|
+
* "none" / empty value → [] (the agent gets zero tools)
|
|
108
|
+
* csv of tool names → an explicit allowlist
|
|
109
|
+
* csv of +name / -name → an inheriting list with those deltas applied
|
|
110
|
+
*
|
|
111
|
+
* An explicitly written key with no value (`tools:` on its own line) reaches
|
|
112
|
+
* here as YAML `null` and must mean zero tools, not inherit. Entries are passed
|
|
113
|
+
* through untouched; the assembler rejects the invalid combinations (plain
|
|
114
|
+
* names mixed with modifiers, or a pattern).
|
|
106
115
|
*/
|
|
107
|
-
function
|
|
108
|
-
if (val === undefined
|
|
116
|
+
function parseToolList(val: unknown): string[] | undefined {
|
|
117
|
+
if (val === undefined) return undefined;
|
|
118
|
+
if (val === null) return [];
|
|
109
119
|
return parseCsvField(val) ?? [];
|
|
110
120
|
}
|
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
import type { AgentConfig } from "../types";
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
// Read-only exploration. The host's read-only tools plus `bash`: exploration leans
|
|
10
|
+
// on read-only shell commands (`git log`, `rg`), and the system prompt below forbids
|
|
11
|
+
// every write. A hardcoded list (rather than "all built-ins") keeps the capability
|
|
12
|
+
// a property of this agent instead of a snapshot of the host's tool set.
|
|
13
|
+
const EXPLORE_TOOL_NAMES = ["read", "bash", "grep", "find", "ls"];
|
|
10
14
|
|
|
11
15
|
export const DEFAULT_AGENTS: Map<string, AgentConfig> = new Map([
|
|
12
16
|
[
|
|
@@ -15,7 +19,9 @@ export const DEFAULT_AGENTS: Map<string, AgentConfig> = new Map([
|
|
|
15
19
|
name: "general-purpose",
|
|
16
20
|
displayName: "Agent",
|
|
17
21
|
description: "General-purpose agent for complex, multi-step tasks",
|
|
18
|
-
//
|
|
22
|
+
// toolNames omitted — inherit the parent session's tools (the dispatch tools
|
|
23
|
+
// are stripped after binding by the recursion guard), so the child matches
|
|
24
|
+
// the caller instead of a package-local snapshot of tool names.
|
|
19
25
|
// inheritContext / runInBackground omitted — strategy fields, callers decide per-call.
|
|
20
26
|
// Setting them to false would lock callsite intent (see resolveAgentInvocationConfig in invocation-config.ts).
|
|
21
27
|
systemPrompt: "",
|
|
@@ -29,7 +35,7 @@ export const DEFAULT_AGENTS: Map<string, AgentConfig> = new Map([
|
|
|
29
35
|
name: "Explore",
|
|
30
36
|
displayName: "Explore",
|
|
31
37
|
description: "Fast codebase exploration agent (read-only)",
|
|
32
|
-
|
|
38
|
+
toolNames: EXPLORE_TOOL_NAMES,
|
|
33
39
|
// model omitted — inherit the parent model, and let callers override it per call.
|
|
34
40
|
// A hardcoded default would win over the `model` param and, when the exact
|
|
35
41
|
// "provider/modelId" is not in the available set, fuzzy-match a different model.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-names.ts — Tool-set resolution for a child session.
|
|
3
|
+
*
|
|
4
|
+
* `tools` in frontmatter has four spellings (see the design doc): unset inherits
|
|
5
|
+
* the parent session's active set, `none`/empty grants nothing, a plain list is an
|
|
6
|
+
* exact allowlist, and a list of only `+name`/`-name` entries starts from the
|
|
7
|
+
* inherited set. Everything here is pure; the caller supplies the parent's names.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Tools registered by this package that a child must never be granted. */
|
|
11
|
+
export const DISPATCH_TOOL_NAMES: readonly string[] = ["subagent", "get_subagent_result", "steer_subagent"];
|
|
12
|
+
|
|
13
|
+
/** A `+name` / `-name` entry modifies the inherited set instead of naming a tool. */
|
|
14
|
+
function isModifierEntry(entry: string): boolean {
|
|
15
|
+
return entry.startsWith("+") || entry.startsWith("-");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Validate a `tools` list before it is resolved.
|
|
20
|
+
*
|
|
21
|
+
* A list made only of `+name` / `-name` entries is an inheriting list; mixing
|
|
22
|
+
* those with plain names is invalid, and every entry must be an exact tool name.
|
|
23
|
+
* The SDK rejects both cases too, but it cannot say which agent type is at
|
|
24
|
+
* fault — check here so the error names the type.
|
|
25
|
+
*/
|
|
26
|
+
export function validateToolList(type: string, names: string[] | undefined): void {
|
|
27
|
+
if (!names || names.length === 0) return;
|
|
28
|
+
|
|
29
|
+
const modifierCount = names.filter(isModifierEntry).length;
|
|
30
|
+
if (modifierCount > 0 && modifierCount < names.length) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`Agent "${type}" has an invalid "tools" list: plain tool names cannot be mixed with +name/-name entries (got: ${names.join(", ")}).`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const pattern = names.find((entry) => entry.includes("*"));
|
|
37
|
+
if (pattern) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Agent "${type}" has an invalid "tools" entry "${pattern}": patterns are not supported; list exact tool names.`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Apply `+name` / `-name` entries to `base` left to right, mirroring the host. */
|
|
45
|
+
function applyModifiers(base: string[], entries: string[]): string[] {
|
|
46
|
+
const result = [...base];
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
const name = entry.slice(1);
|
|
49
|
+
const index = result.indexOf(name);
|
|
50
|
+
if (entry.startsWith("-")) {
|
|
51
|
+
if (index !== -1) result.splice(index, 1);
|
|
52
|
+
} else if (index === -1) {
|
|
53
|
+
result.push(name);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Remove duplicates, keeping the first occurrence. */
|
|
60
|
+
function dedupe(names: string[]): string[] {
|
|
61
|
+
return [...new Set(names)];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Remove this package's dispatch tools, which a child must never be granted. */
|
|
65
|
+
function stripDispatch(names: string[]): string[] {
|
|
66
|
+
return names.filter((name) => !DISPATCH_TOOL_NAMES.includes(name));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Resolve the tool names a child session should be created with.
|
|
71
|
+
*
|
|
72
|
+
* `configured` is the parsed `tools` list (frontmatter splitting has already
|
|
73
|
+
* happened): `undefined` (unset: inherit the parent's active set), `[]` (no tools
|
|
74
|
+
* at all), a plain list (exact allowlist), or a list of only `+name` / `-name`
|
|
75
|
+
* entries (the inherited set with those entries applied). Mixing plain names
|
|
76
|
+
* with entries is rejected by `validateToolList`.
|
|
77
|
+
*
|
|
78
|
+
* When the parent's active set is known, a returned list is final: duplicates
|
|
79
|
+
* are collapsed and this package's dispatch tools are removed. When it is
|
|
80
|
+
* unknown, a modifier-only list is returned untouched (the host applies the
|
|
81
|
+
* deltas to its own defaults). The return `undefined` means the parent's active
|
|
82
|
+
* set is unknown — let the host resolve the child's tools.
|
|
83
|
+
*/
|
|
84
|
+
export function resolveToolNames(
|
|
85
|
+
type: string,
|
|
86
|
+
configured: string[] | undefined,
|
|
87
|
+
parentActive: string[] | undefined,
|
|
88
|
+
): string[] | undefined {
|
|
89
|
+
validateToolList(type, configured);
|
|
90
|
+
|
|
91
|
+
if (configured === undefined) {
|
|
92
|
+
return parentActive === undefined ? undefined : stripDispatch(dedupe(parentActive));
|
|
93
|
+
}
|
|
94
|
+
if (configured.length === 0) return [];
|
|
95
|
+
if (configured.every(isModifierEntry)) {
|
|
96
|
+
return parentActive === undefined ? [...configured] : stripDispatch(dedupe(applyModifiers(parentActive, configured)));
|
|
97
|
+
}
|
|
98
|
+
return stripDispatch(dedupe(configured));
|
|
99
|
+
}
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import type { Model } from "@earendil-works/pi-ai";
|
|
15
15
|
import type { AgentSession, SettingsManager } from "@earendil-works/pi-coding-agent";
|
|
16
16
|
import type { AgentConfigLookup } from "../config/agent-types";
|
|
17
|
+
import { DISPATCH_TOOL_NAMES } from "../config/tool-names";
|
|
17
18
|
import type { ChildLifecyclePublisher } from "../lifecycle/child-lifecycle";
|
|
18
19
|
import type { ParentSnapshot } from "../lifecycle/parent-snapshot";
|
|
19
20
|
import { SubagentSession } from "../lifecycle/subagent-session";
|
|
@@ -21,16 +22,16 @@ import type { EnvInfo } from "../session/env";
|
|
|
21
22
|
import { type AssemblerIO, assembleSessionConfig, type SessionConfig } from "../session/session-config";
|
|
22
23
|
import type { ParentSessionInfo, ShellExec, SubagentType, ThinkingLevel } from "../types";
|
|
23
24
|
|
|
24
|
-
/** Names of tools registered by this extension that subagents must NOT inherit. */
|
|
25
|
-
const EXCLUDED_TOOL_NAMES = ["subagent", "get_subagent_result", "steer_subagent"];
|
|
26
|
-
|
|
27
25
|
/**
|
|
28
|
-
* Apply the recursion guard: remove this
|
|
29
|
-
* child's active set. Runs after
|
|
30
|
-
*
|
|
26
|
+
* Apply the recursion guard: remove this package's dispatch tools
|
|
27
|
+
* (`DISPATCH_TOOL_NAMES`) from the child's active set. Runs after
|
|
28
|
+
* `bindExtensions` so extension-registered tools are also covered — an
|
|
29
|
+
* inheriting child (`tools` unset) gets every tool the parent's extensions
|
|
30
|
+
* register, including these, which is exactly why the removal has to happen
|
|
31
|
+
* post-bind. Unconditional: children always load the parent's extensions.
|
|
31
32
|
*/
|
|
32
33
|
function applyRecursionGuard(session: AgentSession): void {
|
|
33
|
-
const filtered = session.getActiveToolNames().filter((t) => !
|
|
34
|
+
const filtered = session.getActiveToolNames().filter((t) => !DISPATCH_TOOL_NAMES.includes(t));
|
|
34
35
|
session.setActiveToolsByName(filtered);
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -68,7 +69,8 @@ export interface CreateSessionOptions {
|
|
|
68
69
|
settingsManager: SettingsManager;
|
|
69
70
|
modelRegistry: unknown;
|
|
70
71
|
model?: unknown;
|
|
71
|
-
|
|
72
|
+
/** Resolved tool list; `undefined` = leave resolution to the host. */
|
|
73
|
+
tools?: string[];
|
|
72
74
|
resourceLoader: ResourceLoaderLike;
|
|
73
75
|
thinkingLevel?: ThinkingLevel;
|
|
74
76
|
}
|
|
@@ -181,6 +183,7 @@ export async function createSubagentSession(
|
|
|
181
183
|
cwd: snapshot.cwd,
|
|
182
184
|
parentModel: snapshot.model,
|
|
183
185
|
modelRegistry: snapshot.modelRegistry,
|
|
186
|
+
activeToolNames: snapshot.activeToolNames,
|
|
184
187
|
},
|
|
185
188
|
{
|
|
186
189
|
model: params.model,
|
|
@@ -32,6 +32,12 @@ export interface ParentSnapshot {
|
|
|
32
32
|
recordUsage?: (entry: ParentUsageRecord) => void;
|
|
33
33
|
/** Pre-built parent conversation text (when inheritContext was requested). */
|
|
34
34
|
parentContext?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Names of the tools declared to the parent's model, captured at spawn time.
|
|
37
|
+
* `undefined` = the host cannot report them; `[]` = the active set is known to be empty.
|
|
38
|
+
* The snapshot owns its own copy, so later host mutations cannot reach it.
|
|
39
|
+
*/
|
|
40
|
+
activeToolNames?: string[];
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
@@ -54,5 +60,6 @@ export function buildParentSnapshot(ctx: SessionContext, inheritContext?: boolea
|
|
|
54
60
|
: undefined,
|
|
55
61
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- || intentional: converts empty string to undefined as well as null/undefined
|
|
56
62
|
parentContext: parentContext || undefined,
|
|
63
|
+
activeToolNames: ctx.getActiveTools?.()?.slice(),
|
|
57
64
|
};
|
|
58
65
|
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import type { AgentConfigLookup } from "../config/agent-types";
|
|
14
|
+
import { resolveToolNames } from "../config/tool-names";
|
|
14
15
|
import type { EnvInfo } from "../session/env";
|
|
15
16
|
import type { AgentPromptConfig, SubagentType, ThinkingLevel } from "../types";
|
|
16
17
|
|
|
@@ -48,6 +49,11 @@ export interface AssemblerContext {
|
|
|
48
49
|
find(provider: string, modelId: string): unknown;
|
|
49
50
|
getAvailable?(): Array<{ provider: string; id: string }>;
|
|
50
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* Names of the tools declared to the parent's model, captured at spawn time.
|
|
54
|
+
* `undefined` = the host cannot report them, so resolution is left to the host.
|
|
55
|
+
*/
|
|
56
|
+
activeToolNames?: string[];
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
/**
|
|
@@ -73,8 +79,15 @@ export interface SessionConfig {
|
|
|
73
79
|
promptMode: "replace" | "append";
|
|
74
80
|
/** Mode-specific text: the complete system prompt for "replace", the appended section for "append". */
|
|
75
81
|
promptSection: string;
|
|
76
|
-
/**
|
|
77
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Resolved tool names for the child session.
|
|
84
|
+
* `undefined` = leave resolution to the host (the parent's active set is
|
|
85
|
+
* unknown), `[]` = no tools. When the parent's active set is known, a
|
|
86
|
+
* non-empty value is the final allowlist (deduped, with this package's
|
|
87
|
+
* dispatch tools stripped); when it is unknown, a modifier-only list is
|
|
88
|
+
* passed through untouched for the host to apply.
|
|
89
|
+
*/
|
|
90
|
+
toolNames: string[] | undefined;
|
|
78
91
|
/**
|
|
79
92
|
* Resolved model instance (undefined → use parent model as passed to SDK).
|
|
80
93
|
* Opaque handle — the assembler passes it through without inspection.
|
|
@@ -144,7 +157,7 @@ export function assembleSessionConfig(
|
|
|
144
157
|
): SessionConfig {
|
|
145
158
|
const agentConfig = registry.resolveAgentConfig(type);
|
|
146
159
|
|
|
147
|
-
const toolNames = registry.getToolNamesForType(type);
|
|
160
|
+
const toolNames = resolveToolNames(type, registry.getToolNamesForType(type), ctx.activeToolNames);
|
|
148
161
|
|
|
149
162
|
// Assemble the prompt text: "replace" owns the whole prompt, "append" only
|
|
150
163
|
// contributes a section on top of the harness-rendered base prompt.
|
package/src/tools/agent-tool.ts
CHANGED
|
@@ -185,9 +185,7 @@ ${typeListText}`,
|
|
|
185
185
|
}),
|
|
186
186
|
model: Type.Optional(
|
|
187
187
|
Type.String({
|
|
188
|
-
description: tierTable
|
|
189
|
-
? `${MODEL_PARAM_DESCRIPTION} Configured tiers: ${tierTable}.`
|
|
190
|
-
: MODEL_PARAM_DESCRIPTION,
|
|
188
|
+
description: tierTable ? `${MODEL_PARAM_DESCRIPTION} Configured tiers: ${tierTable}.` : MODEL_PARAM_DESCRIPTION,
|
|
191
189
|
}),
|
|
192
190
|
),
|
|
193
191
|
thinking: Type.Optional(
|
|
@@ -216,12 +214,13 @@ ${typeListText}`,
|
|
|
216
214
|
),
|
|
217
215
|
resume: Type.Optional(
|
|
218
216
|
Type.String({
|
|
219
|
-
description:
|
|
217
|
+
description:
|
|
218
|
+
"Optional agent ID: re-prompts that agent's live session (released ~10 min after it finishes); per-call model, thinking and limits do not apply.",
|
|
220
219
|
}),
|
|
221
220
|
),
|
|
222
221
|
inherit_context: Type.Optional(
|
|
223
222
|
Type.Boolean({
|
|
224
|
-
description: "If true,
|
|
223
|
+
description: "If true, prepend the parent conversation as text. Default: false (fresh context).",
|
|
225
224
|
}),
|
|
226
225
|
),
|
|
227
226
|
}),
|
|
@@ -101,7 +101,7 @@ export class GetResultTool {
|
|
|
101
101
|
}),
|
|
102
102
|
wait: Type.Optional(
|
|
103
103
|
Type.Boolean({
|
|
104
|
-
description: "If true, wait for the agent to
|
|
104
|
+
description: "If true, wait for the agent to finish. Only applies while it is running. Default: false.",
|
|
105
105
|
}),
|
|
106
106
|
),
|
|
107
107
|
verbose: Type.Optional(
|
package/src/types.ts
CHANGED
|
@@ -46,7 +46,15 @@ export interface AgentPromptConfig {
|
|
|
46
46
|
|
|
47
47
|
/** Unified agent configuration — used for both default and user-defined agents. */
|
|
48
48
|
export interface AgentConfig extends AgentIdentity, AgentPromptConfig {
|
|
49
|
-
|
|
49
|
+
/**
|
|
50
|
+
* `tools` for this agent.
|
|
51
|
+
* `undefined` = inherit the parent session's captured active set, minus this
|
|
52
|
+
* package's dispatch tools; `[]` = explicitly no tools; a plain list =
|
|
53
|
+
* exactly those names; a list of only `+name` / `-name` entries = inherit,
|
|
54
|
+
* then add/remove. When the parent's active set is unknown, `undefined`
|
|
55
|
+
* falls back to the host's default set plus every loaded extension tool.
|
|
56
|
+
*/
|
|
57
|
+
toolNames?: string[];
|
|
50
58
|
model?: string;
|
|
51
59
|
thinking?: ThinkingLevel;
|
|
52
60
|
maxTurns?: number;
|
|
@@ -94,6 +102,8 @@ export interface SessionContext {
|
|
|
94
102
|
/** Persist non-context usage so child LLM calls join footer and session totals. */
|
|
95
103
|
appendUsage?(kind: string, provider: string, model: string, usage: Usage, note?: string): unknown;
|
|
96
104
|
};
|
|
105
|
+
/** Names of the tools currently declared to the parent's model, when the host reports them. */
|
|
106
|
+
getActiveTools?(): string[];
|
|
97
107
|
}
|
|
98
108
|
|
|
99
109
|
/**
|