pi-herdr-subagents-lite 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +28 -27
- package/README.zh-CN.md +31 -30
- package/package.json +1 -1
- package/src/config/index.ts +3 -9
- package/src/config/schema.ts +1 -3
- package/src/extension/index.ts +2 -6
- package/src/herdr/agent.ts +5 -5
- package/src/herdr/cli.ts +45 -12
- package/src/herdr/index.ts +10 -9
- package/src/herdr/pane.ts +19 -29
- package/src/profile/fields.ts +91 -0
- package/src/profile/frontmatter.ts +19 -89
- package/src/profile/layers.ts +3 -6
- package/src/run/errors.ts +9 -0
- package/src/run/index.ts +8 -5
- package/src/run/pane.ts +12 -5
- package/src/run/paths.ts +13 -0
- package/src/run/prune.ts +29 -0
- package/src/run/resume.ts +117 -0
- package/src/run/retry.ts +70 -0
- package/src/run/send.ts +46 -77
- package/src/run/settle.ts +9 -0
- package/src/run/slot.ts +3 -2
- package/src/run/start.ts +33 -82
- package/src/run/state.ts +4 -1
- package/src/run/watch.ts +38 -20
- package/src/session-file/index.ts +1 -1
- package/src/session-file/jsonl.ts +1 -1
- package/src/tool/index.ts +3 -3
- package/src/turn/classify.ts +125 -0
- package/src/turn/index.ts +11 -236
- package/src/turn/store.ts +112 -0
- package/src/bridge/index.ts +0 -24
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录已发布版本的用户可见变更。更细的过程见 git log。破坏性变更标 **BREAKING**。0.5.2 及更早的变更见旧仓 `setwhite/pi-herdr-subagents-lite` 的 tag `v0.3.0`–`v0.5.2`。
|
|
4
4
|
|
|
5
|
+
## 0.11.0 — 2026-11-24
|
|
6
|
+
|
|
7
|
+
- **BREAKING**:删掉 bridge。本包不再监听 `ui_prompt_*`,不再 emit `herdr:blocked`,`bridge` 配置键变成未知键(老配置加载失败)。要上报 blocked,装会开模态对话框并自己 emit `herdr:blocked` 的扩展(DEC-0070)。
|
|
8
|
+
- 修:`pi-todo-lite` 的装饰 overlay 走 `ctx.ui.custom`,pi 对非捕获 overlay 也发 `ui_prompt_start`,bridge 把它翻成 `herdr:blocked`,pane 从 pi 启动起一直红,`herdr agent start` 还以 `agent_not_ready` 拒绝它。
|
|
9
|
+
|
|
10
|
+
## 0.10.0 — 2026-11-21
|
|
11
|
+
|
|
12
|
+
- **BREAKING**:profile 里出现不认识的字段不再忽略。该 profile 不加载,`run` 返回 `profile_invalid` 并指出字段(DEC-0069)。拼错 `auto_exit` 这类键以前是静默失效。
|
|
13
|
+
- **BREAKING**:新增配置 `runRetentionMs`(默认 30 天,`0` = 永不清理)。`runs/` 下的 brief、identity、inbox、回合事件、artifact 按修改时间在会话启动时清理(DEC-0068)。过期后结果消息里的 `Full result:` 路径失效。
|
|
14
|
+
- 修:子代理还在启动(pane 或 agent 没就绪)时,存活对账会提前判它失去联系,父模型收到一条假的 `lost contact; no result`。run 新增 `starting` 状态(DEC-0066)。
|
|
15
|
+
- 修:回合已收尾的子代理随后退出,会再给父模型补一条「stopped before reporting a result」。现在只静默关 pane。
|
|
16
|
+
- 修:用户中止工具调用时,启动流程照旧跑完并建出 pane,父侧却记为中止。abort 信号现在贯穿到 herdr 调用(DEC-0067)。
|
|
17
|
+
- 修:herdr CLI 卡住会吃掉整轮工具调用。查询 20 秒、变更 120 秒上限,变更超时报 `herdr_timeout` 并提示可能已生效。
|
|
18
|
+
- 修:并行两条 `send` 打同一个待重启的 run 会在同一个 pane 上起两次 agent。第二条现在报 `spawn_failed`。
|
|
19
|
+
- 修:会话关闭时聚合器还会把排队结果投给已经结束的会话。
|
|
20
|
+
- 修:`listAgents` 慢于轮询间隔时存活对账会叠加,把缺席多记一遍,三拍就误判。
|
|
21
|
+
- `turn` 模块拆成 `classify.ts`(纯判定)与 `store.ts`(读写),`send` 拆出 `resume.ts`,启动的换名重试拆出 `retry.ts`。
|
|
22
|
+
|
|
5
23
|
## 0.9.0 — 2026-11-12
|
|
6
24
|
|
|
7
25
|
- **BREAKING**:完成不再从 session 尾部推断。子代理在每个 `agent_settled` 写一个回合事件(`<runBase>.turns/<n>.json`),父代理只认这个文件。失败轮、中断轮不会再被当成完成;消息明确写 `failed` / `interrupted`,并附已有部分文本。
|
package/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[简体中文](README.zh-CN.md) | **English**
|
|
4
4
|
|
|
5
|
-
The extension runs
|
|
5
|
+
The extension runs each subagent as a separate pi process in its own herdr pane. The tool has two actions: `run` starts a subagent, `send` delivers a follow-up message. Results come back asynchronously. Behavior follows the [v1 contract](docs/specs/0001-v1-contract.md).
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- A subagent is a separate process
|
|
10
|
-
- Profiles have two layers
|
|
11
|
-
-
|
|
12
|
-
- Questions stay with the human. A
|
|
13
|
-
- Nesting is off by default
|
|
9
|
+
- A subagent is a separate process, and a human can take over its pane. When the child cwd matches the parent cwd, it opens a tab in the same workspace. Otherwise it opens a new workspace.
|
|
10
|
+
- Profiles have two layers, global and per project, each a Markdown file with frontmatter.
|
|
11
|
+
- `run` returns at once. Each finished turn reports one `subagent_result`. Results inside `aggregateWindowMs` merge into one message, and an oversize result goes to an artifact file.
|
|
12
|
+
- Questions stay with the human. A dialog in the child pane never reaches the parent, and the human answers there. Reporting `blocked` to herdr belongs to the extension that opens the dialog (DEC-0070).
|
|
13
|
+
- Nesting is off by default, and the extension is no sandbox. Unless the profile names this tool, a child tool set drops it.
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -20,17 +20,19 @@ pi install /path/to/pi-herdr-subagents-lite # local checkout
|
|
|
20
20
|
pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # this process only
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
The old repository moved into this monorepo
|
|
23
|
+
The old repository moved into this monorepo, so install from npm.
|
|
24
24
|
|
|
25
|
-
Do not pass `-l`. `-l` writes project settings only, so
|
|
25
|
+
Do not pass `-l`. `-l` writes project settings only, so a child started in another workspace never loads the package. `pi -e` applies to the current process only.
|
|
26
26
|
|
|
27
|
-
Tested with herdr `0.9.5` and pi `1.4.0
|
|
27
|
+
Requires Node `>= 22.19`. Tested with herdr `0.9.5` and pi `1.4.0`. The parent must run inside a herdr pane; otherwise calls fail with `not_in_herdr`.
|
|
28
|
+
|
|
29
|
+
Upgrade with `pi update --extensions`, remove with `pi remove <source>`.
|
|
28
30
|
|
|
29
31
|
## Configuration
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
The extension reads one global file: `<agent-dir>/pi-herdr-subagents-lite/config.json`. `<agent-dir>` defaults to `~/.pi/agent` and follows `PI_CODING_AGENT_DIR`. There is no project config.
|
|
32
34
|
|
|
33
|
-
Defaults and clamp
|
|
35
|
+
Keys and their meaning: [contract §1](docs/specs/0001-v1-contract.md#1-配置). Defaults and clamp bounds: `src/config/index.ts` and `test/config/config.test.ts`.
|
|
34
36
|
|
|
35
37
|
### Minimal config
|
|
36
38
|
|
|
@@ -44,11 +46,11 @@ A missing file means all defaults. To change one value, write only that value:
|
|
|
44
46
|
|
|
45
47
|
### Full configuration
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
Every key follows, and each value is the default.
|
|
48
50
|
|
|
49
51
|
```jsonc
|
|
50
52
|
{
|
|
51
|
-
// Default true. false = no subagent tool for the model.
|
|
53
|
+
// Default true. false = no subagent tool for the model.
|
|
52
54
|
"enabled": true,
|
|
53
55
|
// Default "subagent". Must match ^[a-z][a-z0-9_]{0,31}$. The name prefixes each guideline line.
|
|
54
56
|
"toolName": "subagent",
|
|
@@ -68,6 +70,8 @@ The block below lists every key. Each value is the default.
|
|
|
68
70
|
"inboxPollMs": 250,
|
|
69
71
|
// Default 15000, range 1000 to 300000 milliseconds. How often a running child is checked for liveness. Three absences in a row mark it lost.
|
|
70
72
|
"livenessPollMs": 15000,
|
|
73
|
+
// Default 2592000000 (30 days), range 0 to 31536000000 milliseconds. Run files older than this are deleted at session start. 0 = never.
|
|
74
|
+
"runRetentionMs": 2592000000,
|
|
71
75
|
// Tool text overrides. Each key defaults to unset, which keeps the built-in text shown below.
|
|
72
76
|
"prompts": {
|
|
73
77
|
// Replaces the built-in description.
|
|
@@ -80,30 +84,25 @@ The block below lists every key. Each value is the default.
|
|
|
80
84
|
"send only delivers; no state tracking.",
|
|
81
85
|
"Available profiles:\n{{profiles}}"
|
|
82
86
|
]
|
|
83
|
-
},
|
|
84
|
-
// Turn pi prompt events into herdr:blocked events.
|
|
85
|
-
"bridge": {
|
|
86
|
-
// Default true. false = no herdr:blocked events.
|
|
87
|
-
"enabled": true,
|
|
88
|
-
// Default all five. Prompt kinds that count as blocked: custom, select, confirm, input, editor.
|
|
89
|
-
"blockedOn": ["custom", "select", "confirm", "input", "editor"]
|
|
90
87
|
}
|
|
91
88
|
}
|
|
92
89
|
```
|
|
93
90
|
|
|
94
|
-
The file
|
|
91
|
+
The file must be strict JSON, and the extension reads it once at load. Delete the comments and restart pi after a change.
|
|
92
|
+
|
|
93
|
+
An unknown key, a wrong type or an unknown enum value stops the extension at load: pi prints `invalid config <path>: <field>: unknown key` and exits. A number outside its range clamps to the boundary without an error.
|
|
95
94
|
|
|
96
95
|
## Usage
|
|
97
96
|
|
|
98
97
|
1. Start `pi` in a herdr pane.
|
|
99
|
-
2. Write a profile (next section). `run` takes the frontmatter `name
|
|
98
|
+
2. Write a profile (see the next section). `run` takes the profile frontmatter `name`.
|
|
100
99
|
3. Ask the model to call the tool:
|
|
101
100
|
|
|
102
101
|
```json
|
|
103
102
|
{"action":"run","profile":"explore","task":"Read README.md and answer with its first heading."}
|
|
104
103
|
```
|
|
105
104
|
|
|
106
|
-
The call returns `{"state":"running",...}`. The subagent runs in its own tab
|
|
105
|
+
The call returns `{"state":"running",...}`. The subagent runs in its own tab, and a `[subagent explore-1] done` message arrives later. Do not wait for it in the same turn. Deliver a follow-up with:
|
|
107
106
|
|
|
108
107
|
```json
|
|
109
108
|
{"action":"send","name":"explore-1","message":"Also check the Windows branch."}
|
|
@@ -111,7 +110,7 @@ The call returns `{"state":"running",...}`. The subagent runs in its own tab. A
|
|
|
111
110
|
|
|
112
111
|
### Profile
|
|
113
112
|
|
|
114
|
-
Global profiles live in `<agent-dir>/pi-herdr-subagents-lite/profiles/*.md`. Project profiles live in `.pi/pi-herdr-subagents-lite/profiles/*.md` and load once the project is trusted.
|
|
113
|
+
Global profiles live in `<agent-dir>/pi-herdr-subagents-lite/profiles/*.md`. Project profiles live in `.pi/pi-herdr-subagents-lite/profiles/*.md` and load once the project is trusted. A project profile wins over a global profile with the same `name`.
|
|
115
114
|
|
|
116
115
|
```markdown
|
|
117
116
|
---
|
|
@@ -144,9 +143,11 @@ disable-model-invocation: false
|
|
|
144
143
|
You are a read-only explorer. Answer with file paths and line numbers.
|
|
145
144
|
```
|
|
146
145
|
|
|
147
|
-
Error codes: [contract section 2](docs/specs/0001-v1-contract.md#2-profile). Defaults: `src/profile/frontmatter.ts` and `test/profile/`.
|
|
146
|
+
Error codes: [contract section 2](docs/specs/0001-v1-contract.md#2-profile). Defaults: `src/profile/frontmatter.ts` and `test/profile/`.
|
|
147
|
+
|
|
148
|
+
A profile stays out of the list when it has no `name`, an invalid or unknown field, or a duplicate name in its layer. `run` on it returns `profile_invalid` with the file, the field and the reason.
|
|
148
149
|
|
|
149
|
-
The extension ships
|
|
150
|
+
The extension ships one example, with file name and `name` both `worker`. Loading reads `profiles/*.md` only, and on every load it makes `profiles/examples/` match the bundled example: it rewrites the bundled files and deletes extra ones. So copy the example one level up:
|
|
150
151
|
|
|
151
152
|
```bash
|
|
152
153
|
cd <agent-dir>/pi-herdr-subagents-lite/profiles && cp examples/worker.md worker.md
|
|
@@ -161,7 +162,7 @@ cd <agent-dir>/pi-herdr-subagents-lite/profiles && cp examples/worker.md worker.
|
|
|
161
162
|
| [modem-dev/pi-herdr-subagents](https://github.com/modem-dev/pi-herdr-subagents) | herdr launch and lifecycle; derived end states |
|
|
162
163
|
| [purboo/pi-durable-subagents](https://github.com/purboo/pi-durable-subagents) | Async delivery, idempotency, report-once results |
|
|
163
164
|
|
|
164
|
-
|
|
165
|
+
This extension builds on these projects. Thanks to their authors.
|
|
165
166
|
|
|
166
167
|
## Docs
|
|
167
168
|
|
package/README.zh-CN.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
**[简体中文](README.zh-CN.md)** | [English](README.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
本扩展把子代理跑成独立的 pi 进程,一个子代理占一个 herdr pane。工具有两个 action:`run` 起子代理,`send` 投递后续消息。结果异步回投。行为以 [v1 契约](docs/specs/0001-v1-contract.md) 为准。
|
|
6
6
|
|
|
7
7
|
## 功能
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
9
|
+
- 子代理是独立进程,人可以接管它的 pane。cwd 与父会话相同时,它在同一 workspace 新开 tab;不同则新建 workspace。
|
|
10
|
+
- profile 两层:全局一层,项目一层,都是 Markdown 加 frontmatter。
|
|
11
|
+
- `run` 立即返回。每个回合结束投一条 `subagent_result`,落在 `aggregateWindowMs` 内的合并成一条,超长的落 artifact 文件。
|
|
12
|
+
- 反问留在子 pane。子代理弹对话框,父会话收不到,人在子 pane 里回答。向 herdr 上报 `blocked` 归开对话框的扩展(DEC-0070)。
|
|
13
|
+
- 默认不嵌套,也不是沙箱。profile 不点名,子代理的工具集就没有本工具。
|
|
14
14
|
|
|
15
15
|
## 安装
|
|
16
16
|
|
|
@@ -20,21 +20,23 @@ pi install /path/to/pi-herdr-subagents-lite # 本地源码
|
|
|
20
20
|
pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
旧仓已并入 monorepo
|
|
23
|
+
旧仓已并入 monorepo,请从 npm 装。
|
|
24
24
|
|
|
25
|
-
不要加 `-l`。`-l`
|
|
25
|
+
不要加 `-l`。`-l` 只写项目设置,别的 workspace 里起的子代理就加载不到。`pi -e` 同样只对当前进程生效。
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
要求 Node `>= 22.19`。在 herdr `0.9.5` 与 pi `1.4.0` 上测过。父会话必须跑在 herdr pane 里,否则工具返回 `not_in_herdr`。
|
|
28
|
+
|
|
29
|
+
升级用 `pi update --extensions`,卸载用 `pi remove <source>`。
|
|
28
30
|
|
|
29
31
|
## 配置
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
全局只有一个文件:`<agent-dir>/pi-herdr-subagents-lite/config.json`。`<agent-dir>` 默认 `~/.pi/agent`,跟随 `PI_CODING_AGENT_DIR`。扩展不读项目级配置。
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
键与语义见[契约 §1](docs/specs/0001-v1-contract.md#1-配置)。默认值与 clamp 边界见 `src/config/index.ts` 与 `test/config/config.test.ts`。
|
|
34
36
|
|
|
35
37
|
### 最小配置
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
文件不存在时全部取默认值。只改一项,就只写这一项:
|
|
38
40
|
|
|
39
41
|
```json
|
|
40
42
|
{
|
|
@@ -44,11 +46,11 @@ pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
|
44
46
|
|
|
45
47
|
### 完整配置
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
下面列出全部键,每个取值就是默认值。
|
|
48
50
|
|
|
49
51
|
```jsonc
|
|
50
52
|
{
|
|
51
|
-
// 默认 true。false = 模型看不到本工具。
|
|
53
|
+
// 默认 true。false = 模型看不到本工具。
|
|
52
54
|
"enabled": true,
|
|
53
55
|
// 默认 "subagent"。须匹配 ^[a-z][a-z0-9_]{0,31}$。它会作为每条 guideline 的前缀。
|
|
54
56
|
"toolName": "subagent",
|
|
@@ -68,6 +70,8 @@ pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
|
68
70
|
"inboxPollMs": 250,
|
|
69
71
|
// 默认 15000,范围 1000 到 300000,单位毫秒。多久查一次进行中的子代理还活着没有。连续三次缺席判为失去联系。
|
|
70
72
|
"livenessPollMs": 15000,
|
|
73
|
+
// 默认 2592000000(30 天),范围 0 到 31536000000,单位毫秒。超过保留期的 run 附属文件在会话启动时删除。0 = 永不清理。
|
|
74
|
+
"runRetentionMs": 2592000000,
|
|
71
75
|
// 工具文案覆盖。每个键默认未设置,用下面这份内置文案。
|
|
72
76
|
"prompts": {
|
|
73
77
|
// 替换内置描述。
|
|
@@ -80,30 +84,25 @@ pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
|
80
84
|
"send only delivers; no state tracking.",
|
|
81
85
|
"Available profiles:\n{{profiles}}"
|
|
82
86
|
]
|
|
83
|
-
},
|
|
84
|
-
// 把 pi 对话框事件转成 herdr:blocked 事件。
|
|
85
|
-
"bridge": {
|
|
86
|
-
// 默认 true。false = 不发 herdr:blocked。
|
|
87
|
-
"enabled": true,
|
|
88
|
-
// 默认全五种。算作 blocked 的对话框类型:custom、select、confirm、input、editor。
|
|
89
|
-
"blockedOn": ["custom", "select", "confirm", "input", "editor"]
|
|
90
87
|
}
|
|
91
88
|
}
|
|
92
89
|
```
|
|
93
90
|
|
|
94
|
-
文件必须是严格 JSON
|
|
91
|
+
文件必须是严格 JSON,扩展只在加载时读一次。改完删掉注释,重启 pi。
|
|
92
|
+
|
|
93
|
+
未知键、类型不对或枚举值非法都会让扩展加载失败:pi 打出 `invalid config <路径>: <字段>: unknown key` 后退出。越界的数字夹到边界,不报错。
|
|
95
94
|
|
|
96
95
|
## 用法
|
|
97
96
|
|
|
98
97
|
1. 在 herdr pane 里启动 `pi`。
|
|
99
|
-
2. 写一个 profile
|
|
98
|
+
2. 写一个 profile(见下一节)。frontmatter 的 `name` 就是 `run` 的寻址名。
|
|
100
99
|
3. 让模型调用工具:
|
|
101
100
|
|
|
102
101
|
```json
|
|
103
102
|
{"action":"run","profile":"explore","task":"读 README.md,回答它的第一个标题。"}
|
|
104
103
|
```
|
|
105
104
|
|
|
106
|
-
工具返回 `{"state":"running",...}`。子代理在自己的 tab
|
|
105
|
+
工具返回 `{"state":"running",...}`。子代理在自己的 tab 里跑,稍后收到 `[subagent explore-1] done`。不要在同一轮里等结果。之后向它投递消息:
|
|
107
106
|
|
|
108
107
|
```json
|
|
109
108
|
{"action":"send","name":"explore-1","message":"也看一下 Windows 那条分支。"}
|
|
@@ -111,7 +110,7 @@ pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
|
111
110
|
|
|
112
111
|
### Profile
|
|
113
112
|
|
|
114
|
-
全局 profile
|
|
113
|
+
全局 profile 放 `<agent-dir>/pi-herdr-subagents-lite/profiles/*.md`。项目 profile 放 `.pi/pi-herdr-subagents-lite/profiles/*.md`,项目受信后才读。同名时项目覆盖全局。
|
|
115
114
|
|
|
116
115
|
```markdown
|
|
117
116
|
---
|
|
@@ -144,9 +143,11 @@ disable-model-invocation: false
|
|
|
144
143
|
你是一名只读探索者。回答时给出文件路径与行号。
|
|
145
144
|
```
|
|
146
145
|
|
|
147
|
-
错误码见[契约 §2](docs/specs/0001-v1-contract.md#2-profile)。默认值见 `src/profile/frontmatter.ts` 与 `test/profile
|
|
146
|
+
错误码见[契约 §2](docs/specs/0001-v1-contract.md#2-profile)。默认值见 `src/profile/frontmatter.ts` 与 `test/profile/`。
|
|
147
|
+
|
|
148
|
+
没写 `name`、字段非法、字段名不认识、同层同名的 profile 都不进列表。`run` 点到它时返回 `profile_invalid`,带文件、字段与原因。
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
扩展自带一个示例,文件名和 `name` 都是 `worker`。加载只读 `profiles/*.md`,同时把 `profiles/examples/` 对齐到包内示例:重写自带文件,删掉多余文件。所以把示例复制到上一层:
|
|
150
151
|
|
|
151
152
|
```bash
|
|
152
153
|
cd <agent-dir>/pi-herdr-subagents-lite/profiles && cp examples/worker.md worker.md
|
|
@@ -157,11 +158,11 @@ cd <agent-dir>/pi-herdr-subagents-lite/profiles && cp examples/worker.md worker.
|
|
|
157
158
|
| 项目 | 借鉴内容 |
|
|
158
159
|
|---|---|
|
|
159
160
|
| [earendil-works/pi](https://github.com/earendil-works/pi) | 扩展 API、包布局、官方示例 |
|
|
160
|
-
| [fyang93/pi-herdr-subagents](https://github.com/fyang93/pi-herdr-subagents) | profile
|
|
161
|
-
| [modem-dev/pi-herdr-subagents](https://github.com/modem-dev/pi-herdr-subagents) | herdr
|
|
161
|
+
| [fyang93/pi-herdr-subagents](https://github.com/fyang93/pi-herdr-subagents) | profile 字段形状;隔离 herdr session 的集成测试 |
|
|
162
|
+
| [modem-dev/pi-herdr-subagents](https://github.com/modem-dev/pi-herdr-subagents) | herdr 启动与生命周期;终态归类 |
|
|
162
163
|
| [purboo/pi-durable-subagents](https://github.com/purboo/pi-durable-subagents) | 异步投递、幂等、只上报一次 |
|
|
163
164
|
|
|
164
|
-
|
|
165
|
+
本扩展建在这些项目之上。谢谢各位作者。
|
|
165
166
|
|
|
166
167
|
## 文档
|
|
167
168
|
|
package/package.json
CHANGED
package/src/config/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import {
|
|
4
|
-
BLOCKED_KINDS,
|
|
5
|
-
BRIDGE_KEYS,
|
|
6
4
|
boolean,
|
|
7
5
|
CONFIG_KEYS,
|
|
8
6
|
enumList,
|
|
@@ -22,7 +20,6 @@ import {
|
|
|
22
20
|
|
|
23
21
|
export { isRaw, type Raw } from "./schema.ts";
|
|
24
22
|
|
|
25
|
-
export type BlockedKind = (typeof BLOCKED_KINDS)[number];
|
|
26
23
|
export type ShutdownReason = (typeof SHUTDOWN_REASONS)[number];
|
|
27
24
|
|
|
28
25
|
interface PromptOverrides {
|
|
@@ -41,9 +38,9 @@ export interface Config {
|
|
|
41
38
|
closeChildrenOn: ShutdownReason[];
|
|
42
39
|
inboxPollMs: number;
|
|
43
40
|
livenessPollMs: number;
|
|
41
|
+
runRetentionMs: number;
|
|
44
42
|
closeOnFailure: boolean;
|
|
45
43
|
prompts: PromptOverrides;
|
|
46
|
-
bridge: { enabled: boolean; blockedOn: BlockedKind[] };
|
|
47
44
|
}
|
|
48
45
|
|
|
49
46
|
function promptOverrides(scope: Scope): PromptOverrides {
|
|
@@ -66,7 +63,6 @@ function parse(raw: Raw, file: string): Config {
|
|
|
66
63
|
const problems: string[] = [];
|
|
67
64
|
const top: Scope = { raw, prefix: "", problems };
|
|
68
65
|
unknownKeys(top, CONFIG_KEYS);
|
|
69
|
-
const bridge = object(top, "bridge", BRIDGE_KEYS);
|
|
70
66
|
|
|
71
67
|
const config: Config = {
|
|
72
68
|
enabled: boolean(top, "enabled", true),
|
|
@@ -79,11 +75,9 @@ function parse(raw: Raw, file: string): Config {
|
|
|
79
75
|
closeOnFailure: boolean(top, "closeOnFailure", false),
|
|
80
76
|
inboxPollMs: integer(top, "inboxPollMs", 250, 50, 5000),
|
|
81
77
|
livenessPollMs: integer(top, "livenessPollMs", 15000, 1000, 300000),
|
|
78
|
+
// 0 = 永不清理。默认 30 天。
|
|
79
|
+
runRetentionMs: integer(top, "runRetentionMs", 2592000000, 0, 31536000000),
|
|
82
80
|
prompts: promptOverrides(top),
|
|
83
|
-
bridge: {
|
|
84
|
-
enabled: boolean(bridge, "enabled", true),
|
|
85
|
-
blockedOn: enumList(bridge, "blockedOn", BLOCKED_KINDS, BLOCKED_KINDS),
|
|
86
|
-
},
|
|
87
81
|
};
|
|
88
82
|
|
|
89
83
|
if (problems.length > 0) {
|
package/src/config/schema.ts
CHANGED
|
@@ -9,12 +9,10 @@ export const CONFIG_KEYS = [
|
|
|
9
9
|
"closeOnFailure",
|
|
10
10
|
"inboxPollMs",
|
|
11
11
|
"livenessPollMs",
|
|
12
|
+
"runRetentionMs",
|
|
12
13
|
"prompts",
|
|
13
|
-
"bridge",
|
|
14
14
|
];
|
|
15
|
-
export const BRIDGE_KEYS = ["enabled", "blockedOn"];
|
|
16
15
|
export const PROMPT_KEYS = ["description", "snippet", "guidelines"];
|
|
17
|
-
export const BLOCKED_KINDS = ["custom", "select", "confirm", "input", "editor"] as const;
|
|
18
16
|
export const SHUTDOWN_REASONS = ["new", "resume", "fork", "quit", "reload"] as const;
|
|
19
17
|
const TOOL_NAME = /^[a-z][a-z0-9_]{0,31}$/;
|
|
20
18
|
|
package/src/extension/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { SessionStartEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
import type { BlockedKind, Config } from "../config/index.ts";
|
|
2
|
+
import type { Config } from "../config/index.ts";
|
|
4
3
|
import { errorText } from "../error/index.ts";
|
|
5
4
|
import type { HerdrHost } from "../herdr/index.ts";
|
|
6
5
|
import { drain, hasInbox, pumpInbox } from "../inbox/index.ts";
|
|
@@ -35,14 +34,11 @@ export type { SubagentTool };
|
|
|
35
34
|
|
|
36
35
|
type SessionStartHandler = (event: SessionStartEvent, ctx: SessionContext) => void;
|
|
37
36
|
type SettledHandler = (event: { type: "agent_settled"; aborted: boolean }, ctx: SessionContext) => void;
|
|
38
|
-
type PromptHandler = (event: { kind: BlockedKind; title?: string }) => void;
|
|
39
37
|
|
|
40
38
|
export interface ExtensionRegistrar {
|
|
41
|
-
events: { emit(channel: string, data: unknown): void };
|
|
42
39
|
on(event: "session_start", handler: SessionStartHandler): void;
|
|
43
40
|
on(event: "session_shutdown", handler: ShutdownHandler): void;
|
|
44
41
|
on(event: "agent_settled", handler: SettledHandler): void;
|
|
45
|
-
on(event: "ui_prompt_start" | "ui_prompt_end", handler: PromptHandler): void;
|
|
46
42
|
registerTool(tool: SubagentTool): void;
|
|
47
43
|
sendMessage(message: ResultMessage, options?: { triggerTurn?: boolean }): void;
|
|
48
44
|
sendUserMessage(content: string, options?: { deliverAs?: "steer" | "followUp" }): void;
|
|
@@ -57,7 +53,6 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
57
53
|
let service: RunService | undefined;
|
|
58
54
|
let stopPump: (() => void) | undefined;
|
|
59
55
|
let inboxFailures = 0;
|
|
60
|
-
createBridge(pi, config.bridge);
|
|
61
56
|
watchReport(pi, { activeChildren: () => service?.active().length ?? 0 });
|
|
62
57
|
|
|
63
58
|
const stopInbox = () => {
|
|
@@ -119,6 +114,7 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
119
114
|
maxResultChars: config.maxResultChars,
|
|
120
115
|
aggregateWindowMs: config.aggregateWindowMs,
|
|
121
116
|
livenessPollMs: config.livenessPollMs,
|
|
117
|
+
runRetentionMs: config.runRetentionMs,
|
|
122
118
|
closeOnFailure: config.closeOnFailure,
|
|
123
119
|
toolName: config.toolName,
|
|
124
120
|
onProblem: (message) => ctx.ui.notify(message, "error"),
|
package/src/herdr/agent.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExecHerdr } from "./cli.ts";
|
|
1
|
+
import type { ExecHerdr, HerdrCall } from "./cli.ts";
|
|
2
2
|
|
|
3
3
|
export type AgentStatus = "idle" | "working" | "blocked" | "done" | "unknown";
|
|
4
4
|
|
|
@@ -51,12 +51,12 @@ function sessionPath(agent: AgentInfo): string | null {
|
|
|
51
51
|
|
|
52
52
|
export function createAgentCommands(exec: ExecHerdr) {
|
|
53
53
|
return {
|
|
54
|
-
async startAgent(input: StartAgentInput) {
|
|
55
|
-
await exec(agentStartCommand(input));
|
|
54
|
+
async startAgent(input: StartAgentInput, call: HerdrCall = {}) {
|
|
55
|
+
await exec(agentStartCommand(input), { kind: "mutation", signal: call.signal });
|
|
56
56
|
},
|
|
57
57
|
|
|
58
|
-
async listAgents(): Promise<AgentEntry[]> {
|
|
59
|
-
const result = await exec<{ agents: AgentInfo[] }>(["agent", "list"]);
|
|
58
|
+
async listAgents(call: HerdrCall = {}): Promise<AgentEntry[]> {
|
|
59
|
+
const result = await exec<{ agents: AgentInfo[] }>(["agent", "list"], { kind: "query", signal: call.signal });
|
|
60
60
|
return result.agents.map((agent) => ({
|
|
61
61
|
name: agent.name ?? null,
|
|
62
62
|
paneId: agent.pane_id,
|
package/src/herdr/cli.ts
CHANGED
|
@@ -19,6 +19,12 @@ const HOST_ERROR_CODES: Record<string, string> = {
|
|
|
19
19
|
agent_name_taken: "name_taken",
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
// 查询超时重查一次就行;变更超时留下「不知道生效没有」,所以要给足余量。
|
|
23
|
+
const TIMEOUT_MS = { query: 20_000, mutation: 120_000 } as const;
|
|
24
|
+
|
|
25
|
+
export type HerdrCall = { signal?: AbortSignal };
|
|
26
|
+
export type ExecOptions = HerdrCall & { kind: keyof typeof TIMEOUT_MS };
|
|
27
|
+
|
|
22
28
|
function hostError(code: string, message: string): HerdrHostError {
|
|
23
29
|
return new HerdrHostError(HOST_ERROR_CODES[code] ?? code, message);
|
|
24
30
|
}
|
|
@@ -35,21 +41,50 @@ function parseHerdrError(text: string): HerdrErrorBody | null {
|
|
|
35
41
|
}
|
|
36
42
|
}
|
|
37
43
|
|
|
38
|
-
function
|
|
44
|
+
function commandLine(args: string[]): string {
|
|
45
|
+
return `herdr ${args.join(" ")}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 每条命令都有上限:卡住的 CLI 不能吃掉整轮工具调用。
|
|
49
|
+
export function execCommand(
|
|
50
|
+
bin: string,
|
|
51
|
+
args: string[],
|
|
52
|
+
options: { signal?: AbortSignal; timeoutMs?: number },
|
|
53
|
+
): Promise<string> {
|
|
54
|
+
const timeoutMs = options.timeoutMs ?? TIMEOUT_MS.mutation;
|
|
39
55
|
return new Promise((resolve, reject) => {
|
|
40
56
|
execFile(
|
|
41
57
|
bin,
|
|
42
58
|
args,
|
|
43
|
-
{
|
|
59
|
+
{
|
|
60
|
+
encoding: "utf8",
|
|
61
|
+
windowsHide: true,
|
|
62
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
63
|
+
timeout: timeoutMs,
|
|
64
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
65
|
+
},
|
|
44
66
|
(error, stdout, stderr) => {
|
|
45
67
|
if (!error) {
|
|
46
68
|
resolve(stdout);
|
|
47
69
|
return;
|
|
48
70
|
}
|
|
49
71
|
const parsed = parseHerdrError(stderr) ?? parseHerdrError(stdout);
|
|
72
|
+
if (parsed) {
|
|
73
|
+
reject(hostError(parsed.code ?? "herdr_failed", parsed.message ?? parsed.code ?? "herdr failed"));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (options.signal?.aborted === true) {
|
|
77
|
+
reject(new HerdrHostError("aborted", `${commandLine(args)} was cancelled`));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const failure = error as NodeJS.ErrnoException & { killed?: boolean };
|
|
81
|
+
const timedOut = failure.killed === true && failure.code !== "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
|
|
50
82
|
reject(
|
|
51
|
-
|
|
52
|
-
?
|
|
83
|
+
timedOut
|
|
84
|
+
? new HerdrHostError(
|
|
85
|
+
"herdr_timeout",
|
|
86
|
+
`${commandLine(args)} timed out; it may have taken effect. Check the tab before retrying.`,
|
|
87
|
+
)
|
|
53
88
|
: new HerdrHostError("herdr_failed", error.message),
|
|
54
89
|
);
|
|
55
90
|
},
|
|
@@ -57,31 +92,29 @@ function execText(bin: string, args: string[]): Promise<string> {
|
|
|
57
92
|
});
|
|
58
93
|
}
|
|
59
94
|
|
|
60
|
-
export type ExecHerdr = <T>(args: string[]) => Promise<T>;
|
|
95
|
+
export type ExecHerdr = <T>(args: string[], options: ExecOptions) => Promise<T>;
|
|
61
96
|
|
|
62
97
|
export interface HerdrExec {
|
|
63
98
|
execJson: ExecHerdr;
|
|
64
99
|
}
|
|
65
100
|
|
|
66
101
|
export function createHerdrExec(bin: string): HerdrExec {
|
|
67
|
-
return {
|
|
68
|
-
execJson: async <T>(args: string[]): Promise<T> => execJson<T>(bin, args),
|
|
69
|
-
};
|
|
102
|
+
return { execJson: (args, options) => execJson(bin, args, options) };
|
|
70
103
|
}
|
|
71
104
|
|
|
72
|
-
async function execJson<T>(bin: string, args: string[]): Promise<T> {
|
|
73
|
-
const stdout = await
|
|
105
|
+
async function execJson<T>(bin: string, args: string[], options: ExecOptions): Promise<T> {
|
|
106
|
+
const stdout = await execCommand(bin, args, { signal: options.signal, timeoutMs: TIMEOUT_MS[options.kind] });
|
|
74
107
|
let parsed: { result?: T; error?: HerdrErrorBody };
|
|
75
108
|
try {
|
|
76
109
|
parsed = JSON.parse(stdout) as { result?: T; error?: HerdrErrorBody };
|
|
77
110
|
} catch {
|
|
78
|
-
throw new HerdrHostError("herdr_protocol",
|
|
111
|
+
throw new HerdrHostError("herdr_protocol", `${commandLine(args)}: invalid JSON`);
|
|
79
112
|
}
|
|
80
113
|
if (parsed.error?.code) {
|
|
81
114
|
throw hostError(parsed.error.code, parsed.error.message ?? parsed.error.code);
|
|
82
115
|
}
|
|
83
116
|
if (parsed.result === undefined) {
|
|
84
|
-
throw new HerdrHostError("herdr_protocol",
|
|
117
|
+
throw new HerdrHostError("herdr_protocol", `${commandLine(args)}: no result`);
|
|
85
118
|
}
|
|
86
119
|
return parsed.result;
|
|
87
120
|
}
|
package/src/herdr/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentEntry, AgentSnapshot, StartAgentInput } from "./agent.ts";
|
|
2
2
|
import { createAgentCommands } from "./agent.ts";
|
|
3
|
-
import { createHerdrExec, HerdrHostError } from "./cli.ts";
|
|
3
|
+
import { createHerdrExec, type HerdrCall, HerdrHostError } from "./cli.ts";
|
|
4
4
|
import type {
|
|
5
5
|
CreatedTab,
|
|
6
6
|
CreatedWorkspace,
|
|
@@ -11,17 +11,18 @@ import type {
|
|
|
11
11
|
} from "./pane.ts";
|
|
12
12
|
import { createPaneCommands } from "./pane.ts";
|
|
13
13
|
|
|
14
|
+
export type { HerdrCall };
|
|
14
15
|
export { HerdrHostError };
|
|
15
16
|
|
|
16
17
|
export interface HerdrHost {
|
|
17
|
-
createWorkspace(input: CreateWorkspaceInput): Promise<CreatedWorkspace>;
|
|
18
|
-
createTab(input: CreateTabInput): Promise<CreatedTab>;
|
|
19
|
-
renameTab(input: RenameTabInput): Promise<void>;
|
|
20
|
-
renamePane(input: RenamePaneInput): Promise<void>;
|
|
21
|
-
startAgent(input: StartAgentInput): Promise<void>;
|
|
22
|
-
hasPane(paneId: string): Promise<boolean>;
|
|
23
|
-
listAgents(): Promise<AgentEntry[]>;
|
|
24
|
-
closePane(paneId: string): Promise<void>;
|
|
18
|
+
createWorkspace(input: CreateWorkspaceInput, call?: HerdrCall): Promise<CreatedWorkspace>;
|
|
19
|
+
createTab(input: CreateTabInput, call?: HerdrCall): Promise<CreatedTab>;
|
|
20
|
+
renameTab(input: RenameTabInput, call?: HerdrCall): Promise<void>;
|
|
21
|
+
renamePane(input: RenamePaneInput, call?: HerdrCall): Promise<void>;
|
|
22
|
+
startAgent(input: StartAgentInput, call?: HerdrCall): Promise<void>;
|
|
23
|
+
hasPane(paneId: string, call?: HerdrCall): Promise<boolean>;
|
|
24
|
+
listAgents(call?: HerdrCall): Promise<AgentEntry[]>;
|
|
25
|
+
closePane(paneId: string, call?: HerdrCall): Promise<void>;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
export type { AgentEntry, AgentSnapshot, StartAgentInput };
|