pi-herdr-subagents-lite 0.7.2 → 0.9.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 +17 -0
- package/README.md +14 -12
- package/README.zh-CN.md +14 -12
- package/package.json +1 -1
- package/src/config/index.ts +5 -5
- package/src/config/schema.ts +2 -2
- package/src/extension/index.ts +29 -25
- package/src/extension/report.ts +43 -0
- package/src/extension/shutdown.ts +17 -9
- package/src/herdr/agent.ts +0 -4
- package/src/herdr/index.ts +2 -4
- package/src/herdr/pane.ts +7 -35
- package/src/inbox/index.ts +14 -14
- package/src/index.ts +2 -4
- package/src/run/aggregate.ts +33 -18
- package/src/run/artifact.ts +4 -4
- package/src/run/close.ts +49 -21
- package/src/run/index.ts +21 -27
- package/src/run/pane.ts +3 -4
- package/src/run/paths.ts +18 -1
- package/src/run/result.ts +23 -28
- package/src/run/send.ts +32 -45
- package/src/run/settle.ts +65 -0
- package/src/run/spawn-args.ts +0 -2
- package/src/run/start.ts +19 -13
- package/src/run/state.ts +21 -24
- package/src/run/watch.ts +95 -0
- package/src/session-file/index.ts +12 -14
- package/src/session-file/jsonl.ts +6 -34
- package/src/tool/index.ts +5 -12
- package/src/tool/message.ts +14 -11
- package/src/turn/index.ts +236 -0
- package/src/extension/auto-exit.ts +0 -35
- package/src/run/poll-result.ts +0 -84
- package/src/run/poll.ts +0 -127
- package/src/session-file/receipt.ts +0 -39
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,23 @@
|
|
|
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.9.0 — 2026-11-12
|
|
6
|
+
|
|
7
|
+
- **BREAKING**:完成不再从 session 尾部推断。子代理在每个 `agent_settled` 写一个回合事件(`<runBase>.turns/<n>.json`),父代理只认这个文件。失败轮、中断轮不会再被当成完成;消息明确写 `failed` / `interrupted`,并附已有部分文本。
|
|
8
|
+
- **BREAKING**:子代理不再自杀。`auto-exit` 变成父代理的关闭判据:`done` 且没有活着的子代理才关。删掉 `PI_SUBAGENT_AUTO_EXIT`。
|
|
9
|
+
- **BREAKING**:配置项 `pollIntervalMs` 与 `idlePollIntervalMs` 删除,改为 `livenessPollMs`(默认 15000)。`closeChildrenOn` 默认改为 `["quit", "reload"]`,新增 `closeOnFailure`(默认 false)。
|
|
10
|
+
- 不再每秒起 `herdr agent list` 进程。回合用 500ms 一次本地目录扫描;存活只对进行中的 run 每 15 秒对账,连续三次缺席才判失去联系。
|
|
11
|
+
- 并发上限只数进行中的回合。收工等 `send` 的子代理不再占槽。
|
|
12
|
+
- 关 pane 重试 3 次、间隔 250ms;失败只通知一次。
|
|
13
|
+
|
|
14
|
+
## 0.8.0 — 2026-11-03
|
|
15
|
+
|
|
16
|
+
- 模型不再看到子 session 路径与 pane id:`run` 与 `send` 的工具返回只剩 `state` 与 `name`,`paneId` 与 `sessionFile` 留在 `details`。`unknown` 结果消息不再附 `Session file:`。`send` 的错误只报子代理名。目的是不让父代理顺着路径去读子会话正文。
|
|
17
|
+
- **BREAKING**:文件位置改了。子 session 落在父会话桶的 `runs/` 子目录,brief、identity、inbox、exit 回执、结果 artifact 落在 `<agent-dir>/pi-herdr-subagents-lite/runs/` 一层平铺,共用 `<name>-<runId>` 前缀。超长结果的 `Full result:` 指向新位置。路径只影响新 run。
|
|
18
|
+
- 修:子代理收工、会话关掉后 tab 留在原地。关闭改用 `pane close`,不再先核对 tab 里只有一个 pane —— 那个核对撞上 herdr 重登记 pane 的窗口就会静默放弃。同 tab 里人开的 pane 不受影响。
|
|
19
|
+
- 关 pane 失败不再是终态:补一条 `close_failed`,子代理死亡时再关一次。
|
|
20
|
+
- 修:新回合写出和上一轮逐字相同的答案时,结果被当成重复丢掉,也不关 tab。判据改成「session 文件尾部换过没有」。
|
|
21
|
+
|
|
5
22
|
## 0.7.2 — 2026-11-03
|
|
6
23
|
|
|
7
24
|
- 内置工具文案改为讲用法:`description` 说何时用 `run`、何时用 `send`。
|
package/README.md
CHANGED
|
@@ -54,20 +54,20 @@ The block below lists every key. Each value is the default.
|
|
|
54
54
|
"toolName": "subagent",
|
|
55
55
|
// Default "herdr". A plain name resolves from PATH; an absolute path pins a version.
|
|
56
56
|
"herdrBin": "herdr",
|
|
57
|
-
// Default 16, range 1 and up. At the limit run returns max_concurrent.
|
|
57
|
+
// Default 16, range 1 and up. At the limit run returns max_concurrent. Only a running turn holds a slot.
|
|
58
58
|
"maxConcurrent": 16,
|
|
59
59
|
// Default 16384, range 1024 to 1048576. A longer text goes to an artifact file. The message keeps a truncated head and the path.
|
|
60
60
|
"maxResultChars": 16384,
|
|
61
61
|
// Default 500, range 0 to 10000 milliseconds. 0 = deliver each result at once. A larger value merges more results.
|
|
62
62
|
"aggregateWindowMs": 500,
|
|
63
|
-
// Default
|
|
64
|
-
"closeChildrenOn": [],
|
|
63
|
+
// Default ["quit", "reload"]. Values: new, resume, fork, quit, reload. A listed reason closes the child panes on parent shutdown.
|
|
64
|
+
"closeChildrenOn": ["quit", "reload"],
|
|
65
|
+
// Default false. true = close the pane after a failed or interrupted turn. false keeps it so send can resume.
|
|
66
|
+
"closeOnFailure": false,
|
|
65
67
|
// Default 250, range 50 to 5000 milliseconds. A larger value wakes less often and delivers later.
|
|
66
68
|
"inboxPollMs": 250,
|
|
67
|
-
// Default
|
|
68
|
-
"
|
|
69
|
-
// Default 8000, range 100 to 60000 milliseconds. Use when every live child only waits for send. A larger value notices a dead pane later.
|
|
70
|
-
"idlePollIntervalMs": 8000,
|
|
69
|
+
// 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
|
+
"livenessPollMs": 15000,
|
|
71
71
|
// Tool text overrides. Each key defaults to unset, which keeps the built-in text shown below.
|
|
72
72
|
"prompts": {
|
|
73
73
|
// Replaces the built-in description.
|
|
@@ -134,7 +134,7 @@ skills: ""
|
|
|
134
134
|
# task: body is the task text, no identity file
|
|
135
135
|
system-prompt: append
|
|
136
136
|
|
|
137
|
-
#
|
|
137
|
+
# Close the pane after this run's done turn. Leave it false to keep the child resumable with send
|
|
138
138
|
auto-exit: true
|
|
139
139
|
|
|
140
140
|
# true = hidden from the guidelines; an explicit run still works
|
|
@@ -167,10 +167,12 @@ Thank you for the code.
|
|
|
167
167
|
|
|
168
168
|
| Document | Content |
|
|
169
169
|
|---|---|
|
|
170
|
-
| [
|
|
171
|
-
| [
|
|
172
|
-
| [
|
|
173
|
-
| [
|
|
170
|
+
| [ARCHITECTURE](docs/ARCHITECTURE.md) | Module boundaries, dependency direction, test seams |
|
|
171
|
+
| [GLOSSARY](docs/GLOSSARY.md) | The only source for terms |
|
|
172
|
+
| [Modules](docs/modules/) | Usage and trade-offs per module |
|
|
173
|
+
| [Decisions](docs/decisions/) | One file per decision |
|
|
174
|
+
| [Spec](docs/specs/0001-v1-contract.md) | The v1 contract |
|
|
175
|
+
| [Research](docs/research/) | Measurements of herdr and pi |
|
|
174
176
|
| [CHANGELOG.md](CHANGELOG.md) | Release notes |
|
|
175
177
|
|
|
176
178
|
## License
|
package/README.zh-CN.md
CHANGED
|
@@ -54,20 +54,20 @@ pi -e /path/to/pi-herdr-subagents-lite/src/index.ts # 只对当前进程生效
|
|
|
54
54
|
"toolName": "subagent",
|
|
55
55
|
// 默认 "herdr"。裸名从 PATH 解析。绝对路径锁定版本。
|
|
56
56
|
"herdrBin": "herdr",
|
|
57
|
-
// 默认 16,下限 1。到上限时 run 返回 max_concurrent
|
|
57
|
+
// 默认 16,下限 1。到上限时 run 返回 max_concurrent。只有进行中的回合占槽。
|
|
58
58
|
"maxConcurrent": 16,
|
|
59
59
|
// 默认 16384,范围 1024 到 1048576。超上限时全文落 artifact 文件。消息里留截断的头和路径。
|
|
60
60
|
"maxResultChars": 16384,
|
|
61
61
|
// 默认 500,范围 0 到 10000,单位毫秒。0 = 逐条立即投递。调大合并更多结果。
|
|
62
62
|
"aggregateWindowMs": 500,
|
|
63
|
-
//
|
|
64
|
-
"closeChildrenOn": [],
|
|
63
|
+
// 默认 ["quit", "reload"]。取值:new、resume、fork、quit、reload。列出的原因会在父会话退出时关掉子 pane。
|
|
64
|
+
"closeChildrenOn": ["quit", "reload"],
|
|
65
|
+
// 默认 false。true = 失败或中断的回合结束后关 pane。false 留着,send 可以续。
|
|
66
|
+
"closeOnFailure": false,
|
|
65
67
|
// 默认 250,范围 50 到 5000,单位毫秒。调大省唤醒,投递变慢。
|
|
66
68
|
"inboxPollMs": 250,
|
|
67
|
-
// 默认
|
|
68
|
-
"
|
|
69
|
-
// 默认 8000,范围 100 到 60000,单位毫秒。所有 live 子代理都只在等 send 时用它。调大后,发现 pane 消失更晚。
|
|
70
|
-
"idlePollIntervalMs": 8000,
|
|
69
|
+
// 默认 15000,范围 1000 到 300000,单位毫秒。多久查一次进行中的子代理还活着没有。连续三次缺席判为失去联系。
|
|
70
|
+
"livenessPollMs": 15000,
|
|
71
71
|
// 工具文案覆盖。每个键默认未设置,用下面这份内置文案。
|
|
72
72
|
"prompts": {
|
|
73
73
|
// 替换内置描述。
|
|
@@ -134,7 +134,7 @@ skills: ""
|
|
|
134
134
|
# task:正文就是任务文本,不写身份文件
|
|
135
135
|
system-prompt: append
|
|
136
136
|
|
|
137
|
-
#
|
|
137
|
+
# 这个 run 的 done 回合结束后关掉 pane。false 则留着,send 还能续
|
|
138
138
|
auto-exit: true
|
|
139
139
|
|
|
140
140
|
# true = 不出现在 guidelines 的 profile 列表里。显式 run 仍然可用
|
|
@@ -167,10 +167,12 @@ cd <agent-dir>/pi-herdr-subagents-lite/profiles && cp examples/worker.md worker.
|
|
|
167
167
|
|
|
168
168
|
| 文档 | 内容 |
|
|
169
169
|
|---|---|
|
|
170
|
-
| [
|
|
171
|
-
| [
|
|
172
|
-
| [
|
|
173
|
-
| [
|
|
170
|
+
| [ARCHITECTURE](docs/ARCHITECTURE.md) | 模块边界、依赖方向、测试 seam |
|
|
171
|
+
| [GLOSSARY](docs/GLOSSARY.md) | 术语唯一来源 |
|
|
172
|
+
| [Modules](docs/modules/) | 各模块的用法与取舍 |
|
|
173
|
+
| [Decisions](docs/decisions/) | 一条决策一份文件 |
|
|
174
|
+
| [Spec](docs/specs/0001-v1-contract.md) | v1 契约 |
|
|
175
|
+
| [Research](docs/research/) | herdr 与 pi 的实测记录 |
|
|
174
176
|
| [CHANGELOG.md](CHANGELOG.md) | 发布记录 |
|
|
175
177
|
|
|
176
178
|
## License
|
package/package.json
CHANGED
package/src/config/index.ts
CHANGED
|
@@ -40,8 +40,8 @@ export interface Config {
|
|
|
40
40
|
aggregateWindowMs: number;
|
|
41
41
|
closeChildrenOn: ShutdownReason[];
|
|
42
42
|
inboxPollMs: number;
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
livenessPollMs: number;
|
|
44
|
+
closeOnFailure: boolean;
|
|
45
45
|
prompts: PromptOverrides;
|
|
46
46
|
bridge: { enabled: boolean; blockedOn: BlockedKind[] };
|
|
47
47
|
}
|
|
@@ -75,10 +75,10 @@ function parse(raw: Raw, file: string): Config {
|
|
|
75
75
|
maxConcurrent: integer(top, "maxConcurrent", 16, 1, Number.POSITIVE_INFINITY),
|
|
76
76
|
maxResultChars: integer(top, "maxResultChars", 16384, 1024, 1048576),
|
|
77
77
|
aggregateWindowMs: integer(top, "aggregateWindowMs", 500, 0, 10000),
|
|
78
|
-
closeChildrenOn: enumList(top, "closeChildrenOn", SHUTDOWN_REASONS, []),
|
|
78
|
+
closeChildrenOn: enumList(top, "closeChildrenOn", SHUTDOWN_REASONS, ["quit", "reload"]),
|
|
79
|
+
closeOnFailure: boolean(top, "closeOnFailure", false),
|
|
79
80
|
inboxPollMs: integer(top, "inboxPollMs", 250, 50, 5000),
|
|
80
|
-
|
|
81
|
-
idlePollIntervalMs: integer(top, "idlePollIntervalMs", 8000, 100, 60000),
|
|
81
|
+
livenessPollMs: integer(top, "livenessPollMs", 15000, 1000, 300000),
|
|
82
82
|
prompts: promptOverrides(top),
|
|
83
83
|
bridge: {
|
|
84
84
|
enabled: boolean(bridge, "enabled", true),
|
package/src/config/schema.ts
CHANGED
package/src/extension/index.ts
CHANGED
|
@@ -5,17 +5,16 @@ import { errorText } from "../error/index.ts";
|
|
|
5
5
|
import type { HerdrHost } from "../herdr/index.ts";
|
|
6
6
|
import { drain, hasInbox, pumpInbox } from "../inbox/index.ts";
|
|
7
7
|
import type { ProfileCatalog, ProfileSources } from "../profile/index.ts";
|
|
8
|
-
import { createRunService, type RunService } from "../run/index.ts";
|
|
9
|
-
import type {
|
|
8
|
+
import { childSessionsDir, createRunService, type RunService, runBaseOf } from "../run/index.ts";
|
|
9
|
+
import type { SessionTailReader } from "../session-file/index.ts";
|
|
10
10
|
import { createTool, type ResultMessage, type SubagentTool, sendResults } from "../tool/index.ts";
|
|
11
|
-
import {
|
|
11
|
+
import type { TurnEntry } from "../turn/index.ts";
|
|
12
|
+
import { watchReport } from "./report.ts";
|
|
12
13
|
import { type ShutdownHandler, watchShutdown } from "./shutdown.ts";
|
|
13
14
|
|
|
14
15
|
export interface ExtensionDeps {
|
|
15
16
|
createHost: (config: Config) => HerdrHost;
|
|
16
|
-
|
|
17
|
-
readExitReceipt: ExitReceiptReader;
|
|
18
|
-
writeExitReceipt: ExitReceiptWriter;
|
|
17
|
+
readSessionTail: SessionTailReader;
|
|
19
18
|
loadConfig: () => Config;
|
|
20
19
|
loadProfiles: (sources: ProfileSources) => ProfileCatalog;
|
|
21
20
|
}
|
|
@@ -23,7 +22,10 @@ export interface ExtensionDeps {
|
|
|
23
22
|
export interface SessionContext {
|
|
24
23
|
cwd: string;
|
|
25
24
|
isProjectTrusted(): boolean;
|
|
26
|
-
sessionManager: {
|
|
25
|
+
sessionManager: {
|
|
26
|
+
getSessionFile(): string | undefined;
|
|
27
|
+
getBranch(): TurnEntry[];
|
|
28
|
+
};
|
|
27
29
|
isIdle(): boolean;
|
|
28
30
|
ui: { notify(message: string, type?: "info" | "warning" | "error"): void };
|
|
29
31
|
shutdown(): void;
|
|
@@ -32,7 +34,7 @@ export interface SessionContext {
|
|
|
32
34
|
export type { SubagentTool };
|
|
33
35
|
|
|
34
36
|
type SessionStartHandler = (event: SessionStartEvent, ctx: SessionContext) => void;
|
|
35
|
-
type SettledHandler = (event: { type: "agent_settled" }, ctx: SessionContext) => void;
|
|
37
|
+
type SettledHandler = (event: { type: "agent_settled"; aborted: boolean }, ctx: SessionContext) => void;
|
|
36
38
|
type PromptHandler = (event: { kind: BlockedKind; title?: string }) => void;
|
|
37
39
|
|
|
38
40
|
export interface ExtensionRegistrar {
|
|
@@ -56,7 +58,7 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
56
58
|
let stopPump: (() => void) | undefined;
|
|
57
59
|
let inboxFailures = 0;
|
|
58
60
|
createBridge(pi, config.bridge);
|
|
59
|
-
|
|
61
|
+
watchReport(pi, { activeChildren: () => service?.active().length ?? 0 });
|
|
60
62
|
|
|
61
63
|
const stopInbox = () => {
|
|
62
64
|
stopPump?.();
|
|
@@ -65,7 +67,6 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
65
67
|
|
|
66
68
|
watchShutdown(pi, {
|
|
67
69
|
config,
|
|
68
|
-
writeExitReceipt: deps.writeExitReceipt,
|
|
69
70
|
stopInbox,
|
|
70
71
|
takeService() {
|
|
71
72
|
const current = service;
|
|
@@ -76,7 +77,11 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
76
77
|
|
|
77
78
|
const startInbox = (ctx: SessionContext) => {
|
|
78
79
|
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
79
|
-
if (
|
|
80
|
+
if (sessionFile === undefined) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const base = runBaseOf(sessionFile);
|
|
84
|
+
if (!hasInbox(base)) {
|
|
80
85
|
return;
|
|
81
86
|
}
|
|
82
87
|
inboxFailures = 0;
|
|
@@ -85,7 +90,7 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
85
90
|
};
|
|
86
91
|
stopPump = pumpInbox({
|
|
87
92
|
pollMs: config.inboxPollMs,
|
|
88
|
-
tick: () => drain(
|
|
93
|
+
tick: () => drain(base, deliver),
|
|
89
94
|
onCycle(error) {
|
|
90
95
|
if (error === undefined) {
|
|
91
96
|
inboxFailures = 0;
|
|
@@ -106,24 +111,30 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
106
111
|
service?.shutdown();
|
|
107
112
|
service = createRunService({
|
|
108
113
|
host,
|
|
109
|
-
|
|
110
|
-
|
|
114
|
+
sessionsDir: childSessionsDir(ctx.sessionManager.getSessionFile()),
|
|
115
|
+
readSessionTail: deps.readSessionTail,
|
|
111
116
|
loadProfiles: () => deps.loadProfiles(sources),
|
|
112
117
|
trusted: sources.trusted,
|
|
113
118
|
maxConcurrent: config.maxConcurrent,
|
|
114
119
|
maxResultChars: config.maxResultChars,
|
|
115
120
|
aggregateWindowMs: config.aggregateWindowMs,
|
|
116
|
-
|
|
117
|
-
|
|
121
|
+
livenessPollMs: config.livenessPollMs,
|
|
122
|
+
closeOnFailure: config.closeOnFailure,
|
|
118
123
|
toolName: config.toolName,
|
|
119
|
-
|
|
124
|
+
onProblem: (message) => ctx.ui.notify(message, "error"),
|
|
120
125
|
});
|
|
121
126
|
service.onResult((results) => sendResults(pi, results));
|
|
122
127
|
};
|
|
123
128
|
|
|
129
|
+
const currentService = (): RunService => {
|
|
130
|
+
if (!service) {
|
|
131
|
+
throw new Error("subagent service is not started");
|
|
132
|
+
}
|
|
133
|
+
return service;
|
|
134
|
+
};
|
|
135
|
+
|
|
124
136
|
pi.on("session_start", (_event, ctx) => {
|
|
125
137
|
stopInbox();
|
|
126
|
-
autoExit.reset();
|
|
127
138
|
const sources = { cwd: ctx.cwd, trusted: ctx.isProjectTrusted() };
|
|
128
139
|
startService(sources, ctx);
|
|
129
140
|
if (config.enabled) {
|
|
@@ -132,12 +143,5 @@ export function createExtension(deps: ExtensionDeps): (pi: ExtensionRegistrar) =
|
|
|
132
143
|
}
|
|
133
144
|
startInbox(ctx);
|
|
134
145
|
});
|
|
135
|
-
|
|
136
|
-
const currentService = (): RunService => {
|
|
137
|
-
if (!service) {
|
|
138
|
-
throw new Error("subagent service is not started");
|
|
139
|
-
}
|
|
140
|
-
return service;
|
|
141
|
-
};
|
|
142
146
|
};
|
|
143
147
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { errorText } from "../error/index.ts";
|
|
2
|
+
import { hasInbox } from "../inbox/index.ts";
|
|
3
|
+
import { runBaseOf } from "../run/index.ts";
|
|
4
|
+
import { classifyTurn, lastTurnOf, nextTurn, writeTurn } from "../turn/index.ts";
|
|
5
|
+
import type { ExtensionRegistrar } from "./index.ts";
|
|
6
|
+
|
|
7
|
+
export interface ReportDeps {
|
|
8
|
+
activeChildren: () => number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// 子代理唯一的结果出口:每个结束的回合写一个回合事件。
|
|
12
|
+
export function watchReport(pi: ExtensionRegistrar, deps: ReportDeps): void {
|
|
13
|
+
pi.on("agent_settled", (event, ctx) => {
|
|
14
|
+
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
15
|
+
if (sessionFile === undefined) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const base = runBaseOf(sessionFile);
|
|
19
|
+
if (!hasInbox(base)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const slice = lastTurnOf(ctx.sessionManager.getBranch());
|
|
24
|
+
const verdict = classifyTurn({
|
|
25
|
+
stopReason: slice.stopReason,
|
|
26
|
+
pendingToolCall: slice.pendingToolCall,
|
|
27
|
+
aborted: event.aborted || !slice.found,
|
|
28
|
+
errorMessage: slice.errorMessage,
|
|
29
|
+
});
|
|
30
|
+
writeTurn(base, {
|
|
31
|
+
turn: nextTurn(base),
|
|
32
|
+
outcome: verdict.outcome,
|
|
33
|
+
stopReason: slice.stopReason ?? "",
|
|
34
|
+
...(verdict.errorMessage === undefined ? {} : { errorMessage: verdict.errorMessage }),
|
|
35
|
+
...(slice.text === null ? {} : { text: slice.text }),
|
|
36
|
+
children: deps.activeChildren(),
|
|
37
|
+
at: Date.now(),
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
ctx.ui.notify(`subagent turn not reported: ${errorText(error)}`, "error");
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Config, ShutdownReason } from "../config/index.ts";
|
|
2
|
+
import { errorText } from "../error/index.ts";
|
|
2
3
|
import { hasInbox } from "../inbox/index.ts";
|
|
3
|
-
import type
|
|
4
|
-
import
|
|
4
|
+
import { type RunService, runBaseOf } from "../run/index.ts";
|
|
5
|
+
import { nextTurn, writeTurn } from "../turn/index.ts";
|
|
5
6
|
import type { ExtensionRegistrar, SessionContext } from "./index.ts";
|
|
6
7
|
|
|
7
8
|
export type ShutdownHandler = (
|
|
@@ -11,7 +12,6 @@ export type ShutdownHandler = (
|
|
|
11
12
|
|
|
12
13
|
export interface ShutdownDeps {
|
|
13
14
|
config: Config;
|
|
14
|
-
writeExitReceipt: ExitReceiptWriter;
|
|
15
15
|
stopInbox(): void;
|
|
16
16
|
takeService(): RunService | undefined;
|
|
17
17
|
}
|
|
@@ -21,14 +21,22 @@ export function watchShutdown(pi: ExtensionRegistrar, deps: ShutdownDeps): void
|
|
|
21
21
|
deps.stopInbox();
|
|
22
22
|
const closing = deps.takeService();
|
|
23
23
|
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const base = sessionFile === undefined ? undefined : runBaseOf(sessionFile);
|
|
25
|
+
if (base !== undefined && event.reason === "quit" && hasInbox(base)) {
|
|
26
|
+
try {
|
|
27
|
+
writeTurn(base, {
|
|
28
|
+
turn: nextTurn(base),
|
|
29
|
+
outcome: "quit",
|
|
30
|
+
stopReason: "quit",
|
|
31
|
+
children: 0,
|
|
32
|
+
at: Date.now(),
|
|
33
|
+
});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
ctx.ui.notify(`subagent exit not reported: ${errorText(error)}`, "error");
|
|
36
|
+
}
|
|
26
37
|
}
|
|
27
38
|
if (closing && deps.config.closeChildrenOn.includes(event.reason)) {
|
|
28
|
-
const {
|
|
29
|
-
for (const name of kept) {
|
|
30
|
-
ctx.ui.notify(`child "${name}" kept running: its tab has more than one pane`, "warning");
|
|
31
|
-
}
|
|
39
|
+
const { failed } = await closing.closeChildren();
|
|
32
40
|
for (const failure of failed) {
|
|
33
41
|
ctx.ui.notify(`could not close child "${failure.name}": ${failure.reason}`, "error");
|
|
34
42
|
}
|
package/src/herdr/agent.ts
CHANGED
|
@@ -4,7 +4,6 @@ export type AgentStatus = "idle" | "working" | "blocked" | "done" | "unknown";
|
|
|
4
4
|
|
|
5
5
|
export interface AgentSnapshot {
|
|
6
6
|
status: AgentStatus;
|
|
7
|
-
completionSeq: number;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
export interface AgentEntry extends AgentSnapshot {
|
|
@@ -22,8 +21,6 @@ export interface StartAgentInput {
|
|
|
22
21
|
|
|
23
22
|
interface AgentInfo {
|
|
24
23
|
agent_status: AgentStatus;
|
|
25
|
-
completion_seq?: number | null;
|
|
26
|
-
state_change_seq?: number;
|
|
27
24
|
name?: string | null;
|
|
28
25
|
pane_id: string;
|
|
29
26
|
agent_session?: { kind?: string; value?: string } | null;
|
|
@@ -65,7 +62,6 @@ export function createAgentCommands(exec: ExecHerdr) {
|
|
|
65
62
|
paneId: agent.pane_id,
|
|
66
63
|
sessionFile: sessionPath(agent),
|
|
67
64
|
status: agent.agent_status,
|
|
68
|
-
completionSeq: agent.completion_seq ?? agent.state_change_seq ?? 0,
|
|
69
65
|
}));
|
|
70
66
|
},
|
|
71
67
|
};
|
package/src/herdr/index.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type { AgentEntry, AgentSnapshot, StartAgentInput } from "./agent.ts";
|
|
|
2
2
|
import { createAgentCommands } from "./agent.ts";
|
|
3
3
|
import { createHerdrExec, HerdrHostError } from "./cli.ts";
|
|
4
4
|
import type {
|
|
5
|
-
CloseTabInput,
|
|
6
|
-
CloseTabResult,
|
|
7
5
|
CreatedTab,
|
|
8
6
|
CreatedWorkspace,
|
|
9
7
|
CreateTabInput,
|
|
@@ -23,10 +21,10 @@ export interface HerdrHost {
|
|
|
23
21
|
startAgent(input: StartAgentInput): Promise<void>;
|
|
24
22
|
hasPane(paneId: string): Promise<boolean>;
|
|
25
23
|
listAgents(): Promise<AgentEntry[]>;
|
|
26
|
-
|
|
24
|
+
closePane(paneId: string): Promise<void>;
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
export type { AgentEntry, AgentSnapshot,
|
|
27
|
+
export type { AgentEntry, AgentSnapshot, StartAgentInput };
|
|
30
28
|
|
|
31
29
|
export function createHerdrHost(bin: string): HerdrHost {
|
|
32
30
|
const { execJson } = createHerdrExec(bin);
|
package/src/herdr/pane.ts
CHANGED
|
@@ -8,30 +8,17 @@ export interface CreatedTab {
|
|
|
8
8
|
export interface CreateTabInput {
|
|
9
9
|
cwd: string;
|
|
10
10
|
label: string;
|
|
11
|
-
env: Record<string, string>;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
export interface CreateWorkspaceInput {
|
|
15
14
|
cwd: string;
|
|
16
15
|
label: string;
|
|
17
|
-
env: Record<string, string>;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
export interface CreatedWorkspace extends CreatedTab {
|
|
21
19
|
workspaceId: string;
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
function envArgs(env: Record<string, string>): string[] {
|
|
25
|
-
return Object.entries(env).flatMap(([key, value]) => ["--env", `${key}=${value}`]);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface CloseTabInput {
|
|
29
|
-
tabId: string;
|
|
30
|
-
paneId: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type CloseTabResult = "closed" | "kept" | "missing";
|
|
34
|
-
|
|
35
22
|
export interface RenameTabInput {
|
|
36
23
|
tabId: string;
|
|
37
24
|
label: string;
|
|
@@ -48,18 +35,15 @@ interface WorkspaceInfo {
|
|
|
48
35
|
|
|
49
36
|
interface TabInfo {
|
|
50
37
|
tab_id: string;
|
|
51
|
-
workspace_id: string;
|
|
52
|
-
pane_count: number;
|
|
53
38
|
}
|
|
54
39
|
|
|
55
40
|
interface PaneInfo {
|
|
56
41
|
pane_id: string;
|
|
57
|
-
tab_id: string;
|
|
58
42
|
}
|
|
59
43
|
|
|
60
44
|
export function createPaneCommands(exec: ExecHerdr) {
|
|
61
45
|
return {
|
|
62
|
-
async createWorkspace({ cwd, label
|
|
46
|
+
async createWorkspace({ cwd, label }: CreateWorkspaceInput): Promise<CreatedWorkspace> {
|
|
63
47
|
const result = await exec<{ workspace: WorkspaceInfo; tab: TabInfo; root_pane: PaneInfo }>([
|
|
64
48
|
"workspace",
|
|
65
49
|
"create",
|
|
@@ -67,7 +51,6 @@ export function createPaneCommands(exec: ExecHerdr) {
|
|
|
67
51
|
cwd,
|
|
68
52
|
"--label",
|
|
69
53
|
label,
|
|
70
|
-
...envArgs(env),
|
|
71
54
|
"--no-focus",
|
|
72
55
|
]);
|
|
73
56
|
return {
|
|
@@ -77,7 +60,7 @@ export function createPaneCommands(exec: ExecHerdr) {
|
|
|
77
60
|
};
|
|
78
61
|
},
|
|
79
62
|
|
|
80
|
-
async createTab({ cwd, label
|
|
63
|
+
async createTab({ cwd, label }: CreateTabInput): Promise<CreatedTab> {
|
|
81
64
|
const result = await exec<{ tab: TabInfo; root_pane: PaneInfo }>([
|
|
82
65
|
"tab",
|
|
83
66
|
"create",
|
|
@@ -85,7 +68,6 @@ export function createPaneCommands(exec: ExecHerdr) {
|
|
|
85
68
|
cwd,
|
|
86
69
|
"--label",
|
|
87
70
|
label,
|
|
88
|
-
...envArgs(env),
|
|
89
71
|
"--no-focus",
|
|
90
72
|
]);
|
|
91
73
|
return { tabId: result.tab.tab_id, paneId: result.root_pane.pane_id };
|
|
@@ -104,24 +86,14 @@ export function createPaneCommands(exec: ExecHerdr) {
|
|
|
104
86
|
return result.panes.some((pane) => pane.pane_id === paneId);
|
|
105
87
|
},
|
|
106
88
|
|
|
107
|
-
|
|
89
|
+
// 只关自己记下的 pane,不看 tab 里还有什么。pane 已经不在了也算关掉。
|
|
90
|
+
async closePane(paneId: string): Promise<void> {
|
|
108
91
|
try {
|
|
109
|
-
|
|
110
|
-
if (tab.pane_count !== 1) {
|
|
111
|
-
return "kept";
|
|
112
|
-
}
|
|
113
|
-
const panes = await exec<{ panes: PaneInfo[] }>(["pane", "list", "--workspace", tab.workspace_id]);
|
|
114
|
-
const tabPanes = panes.panes.filter((pane) => pane.tab_id === tabId);
|
|
115
|
-
if (tabPanes.length !== 1 || tabPanes[0]?.pane_id !== paneId) {
|
|
116
|
-
return "kept";
|
|
117
|
-
}
|
|
118
|
-
await exec(["tab", "close", tabId]);
|
|
119
|
-
return "closed";
|
|
92
|
+
await exec(["pane", "close", paneId]);
|
|
120
93
|
} catch (error) {
|
|
121
|
-
if (error instanceof HerdrHostError && error.code === "
|
|
122
|
-
|
|
94
|
+
if (!(error instanceof HerdrHostError && error.code === "pane_not_found")) {
|
|
95
|
+
throw error;
|
|
123
96
|
}
|
|
124
|
-
throw error;
|
|
125
97
|
}
|
|
126
98
|
},
|
|
127
99
|
};
|
package/src/inbox/index.ts
CHANGED
|
@@ -4,13 +4,13 @@ import { join } from "node:path";
|
|
|
4
4
|
|
|
5
5
|
const MESSAGE_SUFFIX = ".md";
|
|
6
6
|
|
|
7
|
-
function inboxDir(
|
|
8
|
-
return `${
|
|
7
|
+
function inboxDir(base: string): string {
|
|
8
|
+
return `${base}.inbox`;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export function hasInbox(
|
|
11
|
+
export function hasInbox(base: string): boolean {
|
|
12
12
|
try {
|
|
13
|
-
return statSync(inboxDir(
|
|
13
|
+
return statSync(inboxDir(base)).isDirectory();
|
|
14
14
|
} catch (error) {
|
|
15
15
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
16
16
|
return false;
|
|
@@ -19,12 +19,12 @@ export function hasInbox(sessionFile: string): boolean {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function createInbox(
|
|
23
|
-
mkdirSync(inboxDir(
|
|
22
|
+
export function createInbox(base: string): void {
|
|
23
|
+
mkdirSync(inboxDir(base), { recursive: true });
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export function removeInbox(
|
|
27
|
-
rmSync(inboxDir(
|
|
26
|
+
export function removeInbox(base: string): void {
|
|
27
|
+
rmSync(inboxDir(base), { recursive: true, force: true });
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
function pendingNames(dir: string): string[] {
|
|
@@ -40,12 +40,12 @@ function pendingNames(dir: string): string[] {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export function inboxCount(
|
|
44
|
-
return pendingNames(inboxDir(
|
|
43
|
+
export function inboxCount(base: string): number {
|
|
44
|
+
return pendingNames(inboxDir(base)).length;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export function enqueue(
|
|
48
|
-
const dir = inboxDir(
|
|
47
|
+
export function enqueue(base: string, text: string): void {
|
|
48
|
+
const dir = inboxDir(base);
|
|
49
49
|
mkdirSync(dir, { recursive: true });
|
|
50
50
|
const name = `${String(process.hrtime.bigint()).padStart(20, "0")}-${randomUUID()}${MESSAGE_SUFFIX}`;
|
|
51
51
|
const temp = join(dir, `.${randomUUID()}.tmp`);
|
|
@@ -53,8 +53,8 @@ export function enqueue(sessionFile: string, text: string): void {
|
|
|
53
53
|
renameSync(temp, join(dir, name));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
export function drain(
|
|
57
|
-
const dir = inboxDir(
|
|
56
|
+
export function drain(base: string, deliver: (text: string) => void): number {
|
|
57
|
+
const dir = inboxDir(base);
|
|
58
58
|
let delivered = 0;
|
|
59
59
|
for (const name of pendingNames(dir)) {
|
|
60
60
|
const path = join(dir, name);
|
package/src/index.ts
CHANGED
|
@@ -3,17 +3,15 @@ import { loadConfig } from "./config/index.ts";
|
|
|
3
3
|
import { createExtension } from "./extension/index.ts";
|
|
4
4
|
import { createHerdrHost } from "./herdr/index.ts";
|
|
5
5
|
import { agentDir, globalProfilesDir, loadProfiles, seedExampleProfile } from "./profile/index.ts";
|
|
6
|
-
import {
|
|
6
|
+
import { readSessionTail } from "./session-file/index.ts";
|
|
7
7
|
|
|
8
8
|
seedExampleProfile(globalProfilesDir());
|
|
9
9
|
|
|
10
10
|
const extension: ExtensionFactory = createExtension({
|
|
11
11
|
createHost: (config) => createHerdrHost(config.herdrBin),
|
|
12
|
-
|
|
13
|
-
readExitReceipt,
|
|
12
|
+
readSessionTail,
|
|
14
13
|
loadConfig: () => loadConfig(agentDir()),
|
|
15
14
|
loadProfiles,
|
|
16
|
-
writeExitReceipt,
|
|
17
15
|
});
|
|
18
16
|
|
|
19
17
|
export default extension;
|