@zhushanwen/pi-subagent-workflow 7.3.4 → 8.0.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/README.md +39 -12
- package/agents/analyst.md +61 -0
- package/agents/coder.md +70 -0
- package/agents/debugger.md +67 -0
- package/agents/doc-reviewer.md +3 -3
- package/agents/explorer.md +50 -18
- package/agents/general-purpose.md +19 -8
- package/agents/orchestrator.md +37 -32
- package/agents/planner.md +45 -11
- package/agents/researcher.md +53 -11
- package/agents/reviewer.md +74 -0
- package/package.json +1 -1
- package/skills/workflow-script-format/SKILL.md +1 -1
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +1 -0
- package/src/execution/__tests__/agent-registry.test.ts +13 -11
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +10 -4
- package/src/execution/__tests__/before-agent-start-injection.test.ts +132 -0
- package/src/execution/__tests__/bg-notify-render.test.ts +15 -15
- package/src/execution/__tests__/chatmode-first-round-closure-service.test.ts +365 -0
- package/src/execution/__tests__/chatmode-first-round-closure-spawn.test.ts +190 -0
- package/src/execution/__tests__/chatmode-round-notify-real-chain.test.ts +215 -0
- package/src/execution/__tests__/conversation-wiring.test.ts +198 -0
- package/src/execution/__tests__/crash-recovery.test.ts +8 -2
- package/src/execution/__tests__/delivery-methods.test.ts +385 -0
- package/src/execution/__tests__/epipe-fallback.test.ts +241 -0
- package/src/execution/__tests__/execute-and-await-worktree.test.ts +49 -2
- package/src/execution/__tests__/execute-nesting.test.ts +20 -72
- package/src/execution/__tests__/execution-record.test.ts +199 -0
- package/src/execution/__tests__/finalize-record.test.ts +170 -14
- package/src/execution/__tests__/format.test.ts +131 -7
- package/src/execution/__tests__/gc-timer.test.ts +184 -0
- package/src/execution/__tests__/get-record-for-action-restart.test.ts +254 -0
- package/src/execution/__tests__/helpers/spawn-mock.ts +25 -7
- package/src/execution/__tests__/index-session-start-identity.test.ts +371 -0
- package/src/execution/__tests__/index-session-start.test.ts +257 -5
- package/src/execution/__tests__/lifecycle-manager-lock.test.ts +211 -0
- package/src/execution/__tests__/lifecycle-manager.test.ts +337 -0
- package/src/execution/__tests__/lifecycle-predicates.test.ts +116 -0
- package/src/execution/__tests__/list-component.test.ts +59 -5
- package/src/execution/__tests__/list-fields.test.ts +109 -0
- package/src/execution/__tests__/model-resolver.test.ts +38 -1
- package/src/execution/__tests__/nested-visibility-env-propagation.test.ts +287 -0
- package/src/execution/__tests__/nested-visibility.test.ts +325 -0
- package/src/execution/__tests__/notifier-flush.test.ts +209 -7
- package/src/execution/__tests__/one-shot-upgrade.test.ts +205 -0
- package/src/execution/__tests__/parent-child-matrix.test.ts +336 -0
- package/src/execution/__tests__/record-store.test.ts +158 -52
- package/src/execution/__tests__/recursive-visibility-baseline.test.ts +11 -12
- package/src/execution/__tests__/recursive-visibility-env.test.ts +18 -20
- package/src/execution/__tests__/resource-policy.test.ts +109 -0
- package/src/execution/__tests__/run-and-finalize-chatmode.test.ts +267 -0
- package/src/execution/__tests__/run-spawn-chatmode-settled.test.ts +253 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +18 -25
- package/src/execution/__tests__/run-spawn-integration.test.ts +29 -25
- package/src/execution/__tests__/run-spawn-resume.test.ts +322 -0
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +14 -11
- package/src/execution/__tests__/session-pending.test.ts +61 -2
- package/src/execution/__tests__/session-reconstructor.test.ts +4 -4
- package/src/execution/__tests__/session-runner-epipe.test.ts +178 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +15 -21
- package/src/execution/__tests__/session-start-reaper.test.ts +10 -8
- package/src/execution/__tests__/spawn-args.test.ts +127 -49
- package/src/execution/__tests__/spawn-worktree-guidance.test.ts +1 -0
- package/src/execution/__tests__/spawned-children.test.ts +92 -0
- package/src/execution/__tests__/status-refactor.test.ts +345 -0
- package/src/execution/__tests__/stdin-writer.test.ts +97 -0
- package/src/execution/__tests__/subagent-service-message-close.test.ts +598 -0
- package/src/execution/__tests__/subagent-service-parent-guard.test.ts +180 -0
- package/src/execution/__tests__/subagent-service.test.ts +49 -11
- package/src/execution/__tests__/timeout-integration.test.ts +27 -13
- package/src/execution/__tests__/tool-action.test.ts +12 -10
- package/src/execution/__tests__/truncline-snapshot.test.ts +81 -0
- package/src/execution/__tests__/turn-limiter-semantics.test.ts +194 -0
- package/src/execution/__tests__/worktree-manager.test.ts +292 -89
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +13 -12
- package/src/execution/argv-mirror.ts +21 -2
- package/src/execution/execution-record.ts +126 -9
- package/src/execution/finalize-record.ts +90 -13
- package/src/execution/host-mode.ts +1 -1
- package/src/execution/lifecycle-manager.ts +484 -0
- package/src/execution/lifecycle-predicates.ts +65 -0
- package/src/execution/manifest-store.ts +61 -16
- package/src/execution/model-resolver.ts +26 -5
- package/src/execution/notifier.ts +69 -12
- package/src/execution/pi-invocation.ts +21 -1
- package/src/execution/record-store.ts +554 -107
- package/src/execution/session-pending.ts +116 -45
- package/src/execution/session-reconstructor.ts +224 -7
- package/src/execution/session-runner.ts +290 -75
- package/src/execution/sessions-index.ts +304 -0
- package/src/execution/stdin-writer.ts +93 -7
- package/src/execution/stream-sink.ts +20 -3
- package/src/execution/subagent-service.ts +867 -138
- package/src/execution/turn-limiter.ts +14 -0
- package/src/execution/types.ts +204 -22
- package/src/execution/worktree-manager.ts +128 -49
- package/src/execution/worktree-registry.ts +13 -2
- package/src/index.ts +277 -19
- package/src/injectors/subagent-list-injector.ts +26 -8
- package/src/injectors/workflow-list-injector.ts +25 -8
- package/src/interface/__tests__/subagent-tool-prompt.test.ts +18 -5
- package/src/interface/__tests__/tool-render.test.ts +15 -13
- package/src/interface/bg-notify-render.ts +32 -8
- package/src/interface/command-actions.ts +26 -7
- package/src/interface/commands.ts +21 -22
- package/src/interface/format.ts +44 -17
- package/src/interface/gui-mappers.ts +6 -8
- package/src/interface/helpers.ts +170 -10
- package/src/interface/list-component.ts +53 -14
- package/src/interface/subagent-actions.ts +235 -17
- package/src/interface/subagent-tool.ts +82 -17
- package/src/interface/subagents.ts +2 -1
- package/src/interface/tool-render.ts +11 -24
- package/src/interface/tool-workflow.ts +20 -35
- package/src/interface/views/WorkflowsView.ts +89 -32
- package/src/interface/views/__tests__/WorkflowsView-signature.test.ts +264 -0
- package/src/interface/views/__tests__/detail-content-session-file.test.ts +1 -1
- package/src/interface/views/format.ts +3 -3
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +325 -0
- package/src/orchestration/__tests__/args-validator.test.ts +1 -1
- package/src/orchestration/__tests__/config-loader.test.ts +38 -0
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +394 -4
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +4 -4
- package/src/orchestration/__tests__/execute-agent-call.test.ts +95 -0
- package/src/orchestration/__tests__/jsonl-run-store-session-file.test.ts +657 -18
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +0 -2
- package/src/orchestration/__tests__/lifecycle.test.ts +332 -149
- package/src/orchestration/__tests__/skill-discovery.test.ts +157 -0
- package/src/orchestration/__tests__/test-mocks.ts +191 -0
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +98 -0
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +0 -2
- package/src/orchestration/__tests__/workflow-script-lint-memo.test.ts +110 -0
- package/src/orchestration/__tests__/workflows-e2e.test.ts +1 -1
- package/src/orchestration/agent-opts-resolver.ts +4 -1
- package/src/orchestration/args-validator.ts +2 -2
- package/src/orchestration/config-loader.ts +30 -1
- package/src/orchestration/error-recovery.ts +133 -29
- package/src/orchestration/execute-agent-call.ts +31 -7
- package/src/orchestration/jsonl-run-store.ts +287 -40
- package/src/orchestration/launcher.ts +7 -1
- package/src/orchestration/lifecycle.ts +135 -132
- package/src/orchestration/models/__tests__/trace.test.ts +408 -0
- package/src/orchestration/models/budget.ts +1 -1
- package/src/orchestration/models/run-runtime.ts +15 -17
- package/src/orchestration/models/run-spec.ts +2 -2
- package/src/orchestration/models/run-state.ts +3 -3
- package/src/orchestration/models/trace.ts +95 -15
- package/src/orchestration/models/types.ts +8 -9
- package/src/orchestration/models/workflow-run.ts +50 -71
- package/src/orchestration/models/workflow-script.ts +32 -1
- package/src/orchestration/skill-discovery.ts +30 -0
- package/src/orchestration/worker-handle.ts +1 -1
- package/src/orchestration/worker-host.ts +1 -1
- package/src/orchestration/worker-script-builder.ts +29 -10
- package/src/shared/__tests__/agent-ref.test.ts +34 -0
- package/src/shared/__tests__/resource-discovery-manifest-cache.test.ts +280 -0
- package/src/shared/__tests__/resource-discovery.test.ts +55 -0
- package/src/shared/__tests__/schema-jsonify.test.ts +81 -0
- package/src/shared/agent-ref.ts +16 -0
- package/src/shared/resource-discovery.ts +147 -58
- package/src/shared/schema-jsonify.ts +53 -0
- package/workflows/README.md +4 -4
- package/agents/code-reviewer.md +0 -47
- package/agents/context-builder.md +0 -21
- package/agents/oracle.md +0 -34
- package/agents/worker.md +0 -20
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// mock 工厂 + FakeChild + 工具函数共享自 helpers/spawn-mock.ts(详见该文件头注释)。
|
|
12
12
|
// vi.mock 必须各文件独立声明(文件作用域),工厂内用 `await import` 取回 FakeChild。
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import { spawn } from "node:child_process";
|
|
15
15
|
import * as fs from "node:fs";
|
|
16
16
|
|
|
17
17
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
@@ -20,7 +20,15 @@ vi.mock("node:child_process", async () => {
|
|
|
20
20
|
const { FakeChild } = await import("./helpers/spawn-mock.ts");
|
|
21
21
|
return {
|
|
22
22
|
spawn: vi.fn(() => new FakeChild()),
|
|
23
|
-
|
|
23
|
+
// buildEnvBlock 的 git branch 调用(execFile 异步):默认 err-first 兜底 → catch → branch=""
|
|
24
|
+
execFile: vi.fn(
|
|
25
|
+
(
|
|
26
|
+
_cmd: string,
|
|
27
|
+
_args: readonly string[],
|
|
28
|
+
_opts: unknown,
|
|
29
|
+
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
30
|
+
) => cb(new Error("execFile not configured in this test")),
|
|
31
|
+
),
|
|
24
32
|
};
|
|
25
33
|
});
|
|
26
34
|
|
|
@@ -75,7 +83,6 @@ import {
|
|
|
75
83
|
} from "./helpers/spawn-mock.ts";
|
|
76
84
|
|
|
77
85
|
const mockSpawn = vi.mocked(spawn);
|
|
78
|
-
const mockExec = vi.mocked(execFileSync);
|
|
79
86
|
const mockExistsSync = vi.mocked(fs.existsSync);
|
|
80
87
|
|
|
81
88
|
// 绑定到本文件 mockSpawn 的 lastSpawnedChild/waitForSpawn(需读 mockSpawn.mock.results)
|
|
@@ -89,8 +96,6 @@ const waitForSpawn = (timeoutMs = 1000): Promise<void> => waitForSpawnOf(mockSpa
|
|
|
89
96
|
describe("runSpawn", () => {
|
|
90
97
|
beforeEach(() => {
|
|
91
98
|
vi.clearAllMocks();
|
|
92
|
-
// execFileSync 默认返回空串(git branch 兜底)
|
|
93
|
-
mockExec.mockReturnValue("");
|
|
94
99
|
// existsSync 默认 false(sessionFile 不存在兜底路径)
|
|
95
100
|
mockExistsSync.mockReturnValue(false);
|
|
96
101
|
});
|
|
@@ -159,23 +164,15 @@ describe("runSpawn", () => {
|
|
|
159
164
|
|
|
160
165
|
it("多个未退出 child(sync + bg)→ killAllSpawnedChildren 全部 kill", async () => {
|
|
161
166
|
// spawn 两个 child(模拟 sync + background 并发),都未退出。
|
|
162
|
-
|
|
163
|
-
// 第二次 spawn 需等待 results.length 递增到 2,否则 lastSpawnedChild 取回的是 c1。
|
|
164
|
-
const beforeCount = mockSpawn.mock.results.length;
|
|
165
|
-
|
|
166
|
-
const rec1 = makeRecord();
|
|
167
|
+
const rec1 = makeRecord("spawn-c1");
|
|
167
168
|
const p1 = runSpawn(rec1, "Task: c1", makeOpts(), makeCtx());
|
|
168
169
|
await waitForSpawn();
|
|
169
170
|
const c1 = lastSpawnedChild();
|
|
170
171
|
|
|
171
|
-
const rec2 = makeRecord();
|
|
172
|
+
const rec2 = makeRecord("spawn-c2");
|
|
172
173
|
const p2 = runSpawn(rec2, "Task: c2", makeOpts(), makeCtx());
|
|
173
|
-
//
|
|
174
|
-
|
|
175
|
-
while (mockSpawn.mock.results.length < beforeCount + 2) {
|
|
176
|
-
if (Date.now() - start > 1000) throw new Error("second spawn not called");
|
|
177
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
178
|
-
}
|
|
174
|
+
// waitForSpawn 是快照语义(记 baseline 等新 spawn),天然支持二次等待
|
|
175
|
+
await waitForSpawn();
|
|
179
176
|
const c2 = lastSpawnedChild();
|
|
180
177
|
|
|
181
178
|
// c1 和 c2 是不同实例
|
|
@@ -211,19 +208,15 @@ describe("runSpawn", () => {
|
|
|
211
208
|
expect(spawnedChildren.size).toBe(0);
|
|
212
209
|
|
|
213
210
|
// spawn 两个未 close 的 child(模拟 close 事件漏触发的极端累积场景)
|
|
214
|
-
const rec1 = makeRecord();
|
|
211
|
+
const rec1 = makeRecord("clear-c1");
|
|
215
212
|
const p1 = runSpawn(rec1, "Task: clear-1", makeOpts(), makeCtx());
|
|
216
213
|
await waitForSpawn();
|
|
217
214
|
const c1 = lastSpawnedChild();
|
|
218
215
|
|
|
219
|
-
const
|
|
220
|
-
const rec2 = makeRecord();
|
|
216
|
+
const rec2 = makeRecord("clear-c2");
|
|
221
217
|
const p2 = runSpawn(rec2, "Task: clear-2", makeOpts(), makeCtx());
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (Date.now() - start > 1000) throw new Error("second spawn not called");
|
|
225
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
226
|
-
}
|
|
218
|
+
// 快照语义二次等待:等 rec2 的 runSpawn 调到 spawn
|
|
219
|
+
await waitForSpawn();
|
|
227
220
|
const c2 = lastSpawnedChild();
|
|
228
221
|
|
|
229
222
|
// 两个 child 都在 Set 中
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
// mock 策略(参考 worktree-manager.test.ts 的 mock 模式):
|
|
9
9
|
// - node:child_process.spawn → 返回 FakeChild(EventEmitter + PassThrough),
|
|
10
10
|
// 测试用控制器 emit data/close/error 控制时序。
|
|
11
|
-
// - node:child_process.
|
|
11
|
+
// - node:child_process.execFile → err-first callback 默认兜底(buildEnvBlock 的 git
|
|
12
|
+
// branch 调用失败 → catch → branch="")。
|
|
12
13
|
// - node:fs 同步方法 → mock(mkdirSync/existsSync/appendFileSync/writeFileSync 等),
|
|
13
14
|
// 避免 sessionDir/sessionFile 触碰真实文件系统。
|
|
14
15
|
// - fs.promises.* → 保留真实实现(temp-prompt 整体被 mock,不触发真实 I/O)。
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
// `await import("./helpers/spawn-mock.ts")` 取回 FakeChild(绕开 vitest 的 hoisting 限制——
|
|
22
23
|
// 工厂函数体不能引用顶层 import 变量,但 async 工厂内的 await import 是运行时求值)。
|
|
23
24
|
|
|
24
|
-
import {
|
|
25
|
+
import { spawn } from "node:child_process";
|
|
25
26
|
import * as fs from "node:fs";
|
|
26
27
|
|
|
27
28
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
@@ -32,7 +33,15 @@ vi.mock("node:child_process", async () => {
|
|
|
32
33
|
const { FakeChild } = await import("./helpers/spawn-mock.ts");
|
|
33
34
|
return {
|
|
34
35
|
spawn: vi.fn(() => new FakeChild()),
|
|
35
|
-
|
|
36
|
+
// buildEnvBlock 的 git branch 调用(execFile 异步):默认 err-first 兜底 → catch → branch=""
|
|
37
|
+
execFile: vi.fn(
|
|
38
|
+
(
|
|
39
|
+
_cmd: string,
|
|
40
|
+
_args: readonly string[],
|
|
41
|
+
_opts: unknown,
|
|
42
|
+
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
43
|
+
) => cb(new Error("execFile not configured in this test")),
|
|
44
|
+
),
|
|
36
45
|
};
|
|
37
46
|
});
|
|
38
47
|
|
|
@@ -86,7 +95,6 @@ import {
|
|
|
86
95
|
} from "./helpers/spawn-mock.ts";
|
|
87
96
|
|
|
88
97
|
const mockSpawn = vi.mocked(spawn);
|
|
89
|
-
const mockExec = vi.mocked(execFileSync);
|
|
90
98
|
const mockExistsSync = vi.mocked(fs.existsSync);
|
|
91
99
|
const mockAppendFileSync = vi.mocked(fs.appendFileSync);
|
|
92
100
|
const mockMkdirSync = vi.mocked(fs.mkdirSync);
|
|
@@ -103,8 +111,6 @@ const mockSessionFileExists = (p: string): void => mockSessionFileExistsOf(mockE
|
|
|
103
111
|
describe("runSpawn", () => {
|
|
104
112
|
beforeEach(() => {
|
|
105
113
|
vi.clearAllMocks();
|
|
106
|
-
// execFileSync 默认返回空串(git branch 兜底)
|
|
107
|
-
mockExec.mockReturnValue("");
|
|
108
114
|
// existsSync 默认 false(sessionFile 不存在兜底路径)
|
|
109
115
|
mockExistsSync.mockReturnValue(false);
|
|
110
116
|
});
|
|
@@ -404,9 +410,9 @@ describe("runSpawn", () => {
|
|
|
404
410
|
});
|
|
405
411
|
});
|
|
406
412
|
|
|
407
|
-
// ── 7. identity
|
|
408
|
-
describe("identity
|
|
409
|
-
it("正常 close 后
|
|
413
|
+
// ── 7. identity 写入职责(M4 / V2 决策 5:迁移到子进程 session_start)──
|
|
414
|
+
describe("identity 写入职责", () => {
|
|
415
|
+
it("正常 close 后 session-runner 不再 fs 补写 identity(M4 迁移到子进程 session_start)", async () => {
|
|
410
416
|
const record = makeRecord();
|
|
411
417
|
const promise = runSpawn(record, "Task: identity", makeOpts(), makeCtx());
|
|
412
418
|
|
|
@@ -425,17 +431,11 @@ describe("runSpawn", () => {
|
|
|
425
431
|
|
|
426
432
|
await promise;
|
|
427
433
|
|
|
428
|
-
//
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
);
|
|
434
|
-
// 写入内容含 record.id / agent
|
|
435
|
-
const written = mockAppendFileSync.mock.calls[0]?.[1] as string;
|
|
436
|
-
expect(written).toContain(record.id);
|
|
437
|
-
expect(written).toContain(record.agent);
|
|
438
|
-
expect(written).toContain('"type":"custom"');
|
|
434
|
+
// [M4 / V2 决策 5] identity 不再由 session-runner fs.appendFileSync 补写——
|
|
435
|
+
// 改由子进程 session_start hook 用 pi.appendEntry 写(pi 自动生成 id/parentId,
|
|
436
|
+
// 修复旧 fs 补写缺 id/parentId 污染 _buildIndex 的 bug)。identity 写入由
|
|
437
|
+
// index-session-start-identity.test.ts 覆盖;此处守护 session-runner 不再 fs 写。
|
|
438
|
+
expect(mockAppendFileSync).not.toHaveBeenCalled();
|
|
439
439
|
});
|
|
440
440
|
|
|
441
441
|
it("sessionFile 不存在(existsSync=false)→ 不补写 identity(不调 appendFileSync)", async () => {
|
|
@@ -625,18 +625,22 @@ describe("runSpawn", () => {
|
|
|
625
625
|
});
|
|
626
626
|
|
|
627
627
|
/**
|
|
628
|
-
* fake timers 下推进时间直到 spawn 被调用(替代 waitForSpawn
|
|
628
|
+
* fake timers 下推进时间直到 spawn 被调用(替代 waitForSpawn,返回 child 控制器)。
|
|
629
|
+
*
|
|
630
|
+
* [快照语义] 同 helpers/spawn-mock.ts 的 waitForSpawn:记调用时 baseline,等待其后的
|
|
631
|
+
* **新** spawn——支持同一测试/文件内多次 runSpawn。
|
|
629
632
|
*
|
|
630
633
|
* runSpawn 在 mkdirSync + writePromptToTempFile(真实 fs.promises I/O)之后才调 spawn。
|
|
631
|
-
* 每次推进 10ms
|
|
632
|
-
*
|
|
634
|
+
* 每次推进 10ms 让轮询 setTimeout 触发;同时 advanceTimersByTimeAsync flush 已 resolve
|
|
635
|
+
* 的 I/O promise,使 runSpawn 继续走到 spawn。
|
|
633
636
|
*/
|
|
634
637
|
async function waitForSpawnFake(timeoutSteps = 200): Promise<FakeChild> {
|
|
638
|
+
const baseline = mockSpawn.mock.results.length;
|
|
635
639
|
for (let i = 0; i < timeoutSteps; i++) {
|
|
636
|
-
if (mockSpawn.mock.results.length >
|
|
640
|
+
if (mockSpawn.mock.results.length > baseline) break;
|
|
637
641
|
await vi.advanceTimersByTimeAsync(10);
|
|
638
642
|
}
|
|
639
|
-
if (mockSpawn.mock.results.length
|
|
643
|
+
if (mockSpawn.mock.results.length <= baseline) {
|
|
640
644
|
throw new Error("spawn was not called (fake timers did not progress to spawn)");
|
|
641
645
|
}
|
|
642
646
|
return lastSpawnedChild();
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// src/__tests__/run-spawn-resume.test.ts
|
|
2
|
+
//
|
|
3
|
+
// runSpawn 的 resume 路径测试(M1 基建:重开已结束 session 继续对话)。
|
|
4
|
+
//
|
|
5
|
+
// 覆盖 M1 拆分 1:runSpawn 第 5 参数 resume(SpawnResumeOpts)→ buildSpawnArgs 追加
|
|
6
|
+
// --session <file> + model/thinkingLevel 覆盖 + record.sessionFile 提前锁定(handshake 不覆盖)。
|
|
7
|
+
//
|
|
8
|
+
// mock 模式与 run-spawn-rpc-mode.test.ts 一致(vi.mock 必须各文件独立声明,工厂内用
|
|
9
|
+
// `await import` 取回 FakeChild)。
|
|
10
|
+
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
|
|
14
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
15
|
+
|
|
16
|
+
vi.mock("node:child_process", async () => {
|
|
17
|
+
const { FakeChild } = await import("./helpers/spawn-mock.ts");
|
|
18
|
+
return {
|
|
19
|
+
spawn: vi.fn(() => new FakeChild()),
|
|
20
|
+
// buildEnvBlock 的 git branch 调用(execFile 异步):默认 err-first 兜底 → catch → branch=""
|
|
21
|
+
execFile: vi.fn(
|
|
22
|
+
(
|
|
23
|
+
_cmd: string,
|
|
24
|
+
_args: readonly string[],
|
|
25
|
+
_opts: unknown,
|
|
26
|
+
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
27
|
+
) => cb(new Error("execFile not configured in this test")),
|
|
28
|
+
),
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
vi.mock("node:fs", async () => {
|
|
33
|
+
const actual = await import("node:fs");
|
|
34
|
+
return {
|
|
35
|
+
default: {
|
|
36
|
+
...actual,
|
|
37
|
+
mkdirSync: vi.fn(),
|
|
38
|
+
existsSync: vi.fn(() => false),
|
|
39
|
+
appendFileSync: vi.fn(),
|
|
40
|
+
writeFileSync: vi.fn(),
|
|
41
|
+
readdirSync: vi.fn(() => []),
|
|
42
|
+
},
|
|
43
|
+
mkdirSync: vi.fn(),
|
|
44
|
+
existsSync: vi.fn(() => false),
|
|
45
|
+
appendFileSync: vi.fn(),
|
|
46
|
+
writeFileSync: vi.fn(),
|
|
47
|
+
readdirSync: vi.fn(() => []),
|
|
48
|
+
promises: actual.promises,
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
vi.mock("../alive-store.ts", () => ({
|
|
53
|
+
writeAliveMarker: vi.fn(),
|
|
54
|
+
}));
|
|
55
|
+
|
|
56
|
+
vi.mock("../temp-prompt.ts", () => ({
|
|
57
|
+
writePromptToTempFile: vi.fn(async (agent: string) => {
|
|
58
|
+
const safeName = agent.replace(/[^\w.-]+/g, "_");
|
|
59
|
+
return { dir: `/tmp/fake-${safeName}`, filePath: `/tmp/fake-${safeName}/prompt-${safeName}.md` };
|
|
60
|
+
}),
|
|
61
|
+
cleanupTempPrompt: vi.fn(async () => {}),
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
import { getChildByRecord, runSpawn, spawnedChildren } from "../session-runner.ts";
|
|
65
|
+
import {
|
|
66
|
+
emitStdoutLine,
|
|
67
|
+
type FakeChild,
|
|
68
|
+
lastSpawnedChild as lastSpawnedChildOf,
|
|
69
|
+
makeCtx,
|
|
70
|
+
makeOpts,
|
|
71
|
+
makeRecord,
|
|
72
|
+
waitForSpawn as waitForSpawnOf,
|
|
73
|
+
} from "./helpers/spawn-mock.ts";
|
|
74
|
+
|
|
75
|
+
const mockSpawn = vi.mocked(spawn);
|
|
76
|
+
const mockExistsSync = vi.mocked(fs.existsSync);
|
|
77
|
+
|
|
78
|
+
const lastSpawnedChild = (): FakeChild => lastSpawnedChildOf(mockSpawn);
|
|
79
|
+
const waitForSpawn = (timeoutMs = 1000): Promise<void> => waitForSpawnOf(mockSpawn, timeoutMs);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 取最近一次 spawn 调用收到的 args 数组。
|
|
83
|
+
*
|
|
84
|
+
* getPiInvocation 在测试环境(existsSync 全 mock 默认 false + node 通用运行时)走分支 3,
|
|
85
|
+
* invocation.args === buildSpawnArgs 的完整输出,故此处直接读 spawn 的第二参数。
|
|
86
|
+
*/
|
|
87
|
+
const lastSpawnArgs = (): string[] => {
|
|
88
|
+
const call = mockSpawn.mock.calls.at(-1);
|
|
89
|
+
if (!call) throw new Error("spawn was not called yet");
|
|
90
|
+
return call[1] as string[];
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
describe("runSpawn resume(M1 基建)", () => {
|
|
94
|
+
beforeEach(() => {
|
|
95
|
+
vi.clearAllMocks();
|
|
96
|
+
mockExistsSync.mockReturnValue(false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
afterEach(() => {
|
|
100
|
+
vi.restoreAllMocks();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("resume 全参数:--session + model(:thinkingLevel) 进 args,且覆盖 opts.resolved", async () => {
|
|
104
|
+
const record = makeRecord();
|
|
105
|
+
const resumeSessionFile = "/sessions/sub/resume-target.jsonl";
|
|
106
|
+
const promise = runSpawn(record, "继续上轮对话", makeOpts(), makeCtx(), {
|
|
107
|
+
sessionFile: resumeSessionFile,
|
|
108
|
+
model: "anthropic/claude-sonnet",
|
|
109
|
+
thinkingLevel: "high",
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await waitForSpawn();
|
|
113
|
+
const child = lastSpawnedChild();
|
|
114
|
+
|
|
115
|
+
// resume.sessionFile 文件存在(resume 前提)→ 让 existsSync 对它返回 true,
|
|
116
|
+
// 避免进程退出后兜底查找报错 + identity 补写路径走通。
|
|
117
|
+
mockExistsSync.mockImplementation((p: unknown) => String(p) === resumeSessionFile);
|
|
118
|
+
// 消费 stdin(含 get_state 握手命令 + prompt 命令),避免背压
|
|
119
|
+
child.stdin.on("data", () => {});
|
|
120
|
+
|
|
121
|
+
emitStdoutLine(child, { type: "turn_end" });
|
|
122
|
+
child.stdout.end();
|
|
123
|
+
child.emit("close", 0);
|
|
124
|
+
|
|
125
|
+
await promise;
|
|
126
|
+
|
|
127
|
+
const args = lastSpawnArgs();
|
|
128
|
+
// --session <resumeSessionFile> 紧跟 --session-dir
|
|
129
|
+
const sessionIdx = args.indexOf("--session");
|
|
130
|
+
expect(sessionIdx).toBeGreaterThan(-1);
|
|
131
|
+
expect(args[sessionIdx + 1]).toBe(resumeSessionFile);
|
|
132
|
+
const sessionDirIdx = args.indexOf("--session-dir");
|
|
133
|
+
expect(sessionIdx).toBe(sessionDirIdx + 2);
|
|
134
|
+
// --model 用 resume.model(覆盖 opts.resolved 的 test/test-model),thinkingLevel 作后缀
|
|
135
|
+
const modelIdx = args.indexOf("--model");
|
|
136
|
+
expect(modelIdx).toBeGreaterThan(-1);
|
|
137
|
+
expect(args[modelIdx + 1]).toBe("anthropic/claude-sonnet:high");
|
|
138
|
+
// 不应出现 opts.resolved 的 model(证明 resume.model 覆盖生效)
|
|
139
|
+
expect(args).not.toContain("test/test-model");
|
|
140
|
+
|
|
141
|
+
// record.sessionFile 提前锁定为 resume 值
|
|
142
|
+
expect(record.sessionFile).toBe(resumeSessionFile);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("resume 不传 model/thinkingLevel → --model 回退 opts.resolved", async () => {
|
|
146
|
+
const record = makeRecord();
|
|
147
|
+
const resumeSessionFile = "/sessions/sub/no-model.jsonl";
|
|
148
|
+
const promise = runSpawn(record, "继续", makeOpts(), makeCtx(), {
|
|
149
|
+
sessionFile: resumeSessionFile,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await waitForSpawn();
|
|
153
|
+
const child = lastSpawnedChild();
|
|
154
|
+
|
|
155
|
+
mockExistsSync.mockImplementation((p: unknown) => String(p) === resumeSessionFile);
|
|
156
|
+
child.stdin.on("data", () => {});
|
|
157
|
+
|
|
158
|
+
emitStdoutLine(child, { type: "turn_end" });
|
|
159
|
+
child.stdout.end();
|
|
160
|
+
child.emit("close", 0);
|
|
161
|
+
|
|
162
|
+
await promise;
|
|
163
|
+
|
|
164
|
+
const args = lastSpawnArgs();
|
|
165
|
+
// --session 仍追加
|
|
166
|
+
const sessionIdx = args.indexOf("--session");
|
|
167
|
+
expect(sessionIdx).toBeGreaterThan(-1);
|
|
168
|
+
expect(args[sessionIdx + 1]).toBe(resumeSessionFile);
|
|
169
|
+
// --model 回退 opts.resolved(test/test-model)
|
|
170
|
+
const modelIdx = args.indexOf("--model");
|
|
171
|
+
expect(modelIdx).toBeGreaterThan(-1);
|
|
172
|
+
expect(args[modelIdx + 1]).toBe("test/test-model");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("resume 时 handshake 仍调但 sessionFile 不被 response 覆盖(提前锁定)", async () => {
|
|
176
|
+
const record = makeRecord();
|
|
177
|
+
const resumeSessionFile = "/sessions/sub/locked.jsonl";
|
|
178
|
+
const promise = runSpawn(record, "继续", makeOpts(), makeCtx(), {
|
|
179
|
+
sessionFile: resumeSessionFile,
|
|
180
|
+
model: "openai/gpt-4o",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
await waitForSpawn();
|
|
184
|
+
const child = lastSpawnedChild();
|
|
185
|
+
|
|
186
|
+
mockExistsSync.mockImplementation((p: unknown) => String(p) === resumeSessionFile);
|
|
187
|
+
|
|
188
|
+
// 捕获握手发出的 get_state 命令(证明 handshake 仍调),并 emit 一个 sessionFile 不同的
|
|
189
|
+
// response(模拟 handshake 返回新文件路径)→ 验证 record.sessionFile 不被覆盖。
|
|
190
|
+
let getStateSeen = false;
|
|
191
|
+
const conflictingSessionFile = "/sessions/sub/SHOULD-NOT-OVERWRITE.jsonl";
|
|
192
|
+
child.stdin.on("data", (data: Buffer | string) => {
|
|
193
|
+
const text = typeof data === "string" ? data : data.toString();
|
|
194
|
+
for (const line of text.split("\n")) {
|
|
195
|
+
if (!line.trim()) continue;
|
|
196
|
+
try {
|
|
197
|
+
const cmd = JSON.parse(line) as { type?: string; id?: string };
|
|
198
|
+
if (cmd.type === "get_state" && cmd.id) {
|
|
199
|
+
getStateSeen = true;
|
|
200
|
+
emitStdoutLine(child, {
|
|
201
|
+
type: "response",
|
|
202
|
+
command: "get_state",
|
|
203
|
+
success: true,
|
|
204
|
+
id: cmd.id,
|
|
205
|
+
data: { sessionFile: conflictingSessionFile, sessionId: "rpc-sess" },
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
} catch {
|
|
209
|
+
// 非 JSON 行(prompt 命令等)忽略
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// 等 stdin listener 触发 + response 经 stdout pump 处理 → finishHandshake 执行
|
|
215
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
216
|
+
|
|
217
|
+
emitStdoutLine(child, { type: "turn_end" });
|
|
218
|
+
child.stdout.end();
|
|
219
|
+
child.emit("close", 0);
|
|
220
|
+
|
|
221
|
+
await promise;
|
|
222
|
+
|
|
223
|
+
// handshake 确实被调(get_state 命令已发出)
|
|
224
|
+
expect(getStateSeen).toBe(true);
|
|
225
|
+
// 但 record.sessionFile 仍是 resume 锁定值,未被 response 覆盖
|
|
226
|
+
expect(record.sessionFile).toBe(resumeSessionFile);
|
|
227
|
+
expect(record.sessionFile).not.toBe(conflictingSessionFile);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// ============================================================
|
|
232
|
+
// [M4] spawnedChildren 记账竞态:旧 child close/error 晚于 resume spawn
|
|
233
|
+
// ============================================================
|
|
234
|
+
|
|
235
|
+
describe("[M4] spawnedChildren 记账竞态守卫(旧 child close 晚于 resume spawn)", () => {
|
|
236
|
+
beforeEach(() => {
|
|
237
|
+
vi.clearAllMocks();
|
|
238
|
+
mockExistsSync.mockReturnValue(false);
|
|
239
|
+
spawnedChildren.clear();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
afterEach(() => {
|
|
243
|
+
spawnedChildren.clear();
|
|
244
|
+
vi.restoreAllMocks();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 竞态时序(PR #173 review 组 D):idle timer 对旧 child 发 SIGTERM(killed=true 但
|
|
249
|
+
* close 事件异步未到)→ deliverMessage 判 child.killed 走冷路径 resumeRound → 新 child
|
|
250
|
+
* spawn 并 set 覆盖注册 → 旧 child 的 close 事件此刻到达。守卫前:close handler 无条件
|
|
251
|
+
* delete(record.id) 误删新注册 → 新活进程脱离记账(killAllSpawnedChildren 漏杀 +
|
|
252
|
+
* 二次 resume 双写 session 文件)。守卫后:按句相等跳过删除。
|
|
253
|
+
*/
|
|
254
|
+
it("旧 child close 晚于 resume spawn 到达:不误删新 child 注册(按值守卫)", async () => {
|
|
255
|
+
const record = makeRecord("m4-race-1");
|
|
256
|
+
// 首轮 runSpawn(注册 child1)
|
|
257
|
+
const p1 = runSpawn(record, "first round", makeOpts(), makeCtx());
|
|
258
|
+
await waitForSpawn();
|
|
259
|
+
const child1 = lastSpawnedChild();
|
|
260
|
+
child1.stdin.on("data", () => {});
|
|
261
|
+
expect(getChildByRecord(record.id)).toBe(child1);
|
|
262
|
+
|
|
263
|
+
// 模拟 idle timer SIGTERM:killed=true 但 close 事件**未派发**(异步窗口)
|
|
264
|
+
child1.kill("SIGTERM");
|
|
265
|
+
|
|
266
|
+
// 冷路径 resume:同 record 第二次 runSpawn,spawn child2 覆盖注册
|
|
267
|
+
const resumeFile = "/sessions/sub/m4-race.jsonl";
|
|
268
|
+
const p2 = runSpawn(record, "resume round", makeOpts(), makeCtx(), {
|
|
269
|
+
sessionFile: resumeFile,
|
|
270
|
+
});
|
|
271
|
+
await waitForSpawn();
|
|
272
|
+
const child2 = lastSpawnedChild();
|
|
273
|
+
child2.stdin.on("data", () => {});
|
|
274
|
+
expect(getChildByRecord(record.id)).toBe(child2);
|
|
275
|
+
|
|
276
|
+
// 旧 child1 的 close 事件此刻到达——守卫必须跳过删除
|
|
277
|
+
emitStdoutLine(child1, { type: "turn_end" });
|
|
278
|
+
child1.stdout.end();
|
|
279
|
+
child1.emit("close", 0);
|
|
280
|
+
await p1;
|
|
281
|
+
|
|
282
|
+
// 关键断言:新 child2 的注册未被旧 child 的迟到 close 误删
|
|
283
|
+
expect(getChildByRecord(record.id)).toBe(child2);
|
|
284
|
+
|
|
285
|
+
// child2 正常退出:close 后注册清理(守卫对当前句柄仍生效)
|
|
286
|
+
mockExistsSync.mockImplementation((p: unknown) => String(p) === resumeFile);
|
|
287
|
+
emitStdoutLine(child2, { type: "turn_end" });
|
|
288
|
+
child2.stdout.end();
|
|
289
|
+
child2.emit("close", 0);
|
|
290
|
+
await p2;
|
|
291
|
+
expect(getChildByRecord(record.id)).toBeUndefined();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("旧 child spawn error 晚于 resume spawn 到达:同样不误删新 child 注册", async () => {
|
|
295
|
+
const record = makeRecord("m4-race-2");
|
|
296
|
+
const p1 = runSpawn(record, "first round", makeOpts(), makeCtx());
|
|
297
|
+
await waitForSpawn();
|
|
298
|
+
const child1 = lastSpawnedChild();
|
|
299
|
+
child1.stdin.on("data", () => {});
|
|
300
|
+
|
|
301
|
+
const p2 = runSpawn(record, "resume round", makeOpts(), makeCtx(), {
|
|
302
|
+
sessionFile: "/sessions/sub/m4-race-2.jsonl",
|
|
303
|
+
});
|
|
304
|
+
await waitForSpawn();
|
|
305
|
+
const child2 = lastSpawnedChild();
|
|
306
|
+
child2.stdin.on("data", () => {});
|
|
307
|
+
expect(getChildByRecord(record.id)).toBe(child2);
|
|
308
|
+
|
|
309
|
+
// 旧 child1 的 error 事件迟到(spawn 失败回调晚到)
|
|
310
|
+
child1.emit("error", new Error("spawn ENOENT (late)"));
|
|
311
|
+
await p1;
|
|
312
|
+
|
|
313
|
+
expect(getChildByRecord(record.id)).toBe(child2);
|
|
314
|
+
|
|
315
|
+
mockExistsSync.mockImplementation((p: unknown) => String(p) === "/sessions/sub/m4-race-2.jsonl");
|
|
316
|
+
emitStdoutLine(child2, { type: "turn_end" });
|
|
317
|
+
child2.stdout.end();
|
|
318
|
+
child2.emit("close", 0);
|
|
319
|
+
await p2;
|
|
320
|
+
expect(getChildByRecord(record.id)).toBeUndefined();
|
|
321
|
+
});
|
|
322
|
+
});
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// mock 工厂 + FakeChild + 工具函数共享自 helpers/spawn-mock.ts(详见该文件头注释)。
|
|
9
9
|
// vi.mock 必须各文件独立声明(文件作用域),工厂内用 `await import` 取回 FakeChild。
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
12
|
import * as fs from "node:fs";
|
|
13
13
|
|
|
14
14
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
@@ -17,7 +17,15 @@ vi.mock("node:child_process", async () => {
|
|
|
17
17
|
const { FakeChild } = await import("./helpers/spawn-mock.ts");
|
|
18
18
|
return {
|
|
19
19
|
spawn: vi.fn(() => new FakeChild()),
|
|
20
|
-
|
|
20
|
+
// buildEnvBlock 的 git branch 调用(execFile 异步):默认 err-first 兜底 → catch → branch=""
|
|
21
|
+
execFile: vi.fn(
|
|
22
|
+
(
|
|
23
|
+
_cmd: string,
|
|
24
|
+
_args: readonly string[],
|
|
25
|
+
_opts: unknown,
|
|
26
|
+
cb: (err: Error | null, stdout?: string, stderr?: string) => void,
|
|
27
|
+
) => cb(new Error("execFile not configured in this test")),
|
|
28
|
+
),
|
|
21
29
|
};
|
|
22
30
|
});
|
|
23
31
|
|
|
@@ -66,7 +74,6 @@ import {
|
|
|
66
74
|
} from "./helpers/spawn-mock.ts";
|
|
67
75
|
|
|
68
76
|
const mockSpawn = vi.mocked(spawn);
|
|
69
|
-
const mockExec = vi.mocked(execFileSync);
|
|
70
77
|
const mockExistsSync = vi.mocked(fs.existsSync);
|
|
71
78
|
const mockAppendFileSync = vi.mocked(fs.appendFileSync);
|
|
72
79
|
|
|
@@ -82,7 +89,6 @@ const mockSessionFileExists = (p: string): void => mockSessionFileExistsOf(mockE
|
|
|
82
89
|
describe("runSpawn", () => {
|
|
83
90
|
beforeEach(() => {
|
|
84
91
|
vi.clearAllMocks();
|
|
85
|
-
mockExec.mockReturnValue("");
|
|
86
92
|
mockExistsSync.mockReturnValue(false);
|
|
87
93
|
});
|
|
88
94
|
|
|
@@ -131,7 +137,7 @@ describe("runSpawn", () => {
|
|
|
131
137
|
});
|
|
132
138
|
}
|
|
133
139
|
|
|
134
|
-
it("无 header + get_state response 回填 sessionFile
|
|
140
|
+
it("无 header + get_state response 回填 sessionFile(identity 不再由 session-runner fs 写)", async () => {
|
|
135
141
|
const record = makeRecord();
|
|
136
142
|
const promise = runSpawn(record, "Task: rpc-no-header", makeOpts(), makeCtx());
|
|
137
143
|
|
|
@@ -158,12 +164,9 @@ describe("runSpawn", () => {
|
|
|
158
164
|
expect(result.success).toBe(true);
|
|
159
165
|
expect(record.sessionFile).toBe(expectedSessionFile);
|
|
160
166
|
expect(result.sessionFile).toBe(expectedSessionFile);
|
|
161
|
-
// identity
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
expect.stringContaining('"customType":"subagent-identity"'),
|
|
165
|
-
"utf-8",
|
|
166
|
-
);
|
|
167
|
+
// [M4 / V2 决策 5] identity 不再由 session-runner fs 补写(迁移到子进程 session_start,
|
|
168
|
+
// 见 index-session-start-identity.test.ts)。此处仅守护 session-runner 不 fs 写 identity。
|
|
169
|
+
expect(mockAppendFileSync).not.toHaveBeenCalled();
|
|
167
170
|
});
|
|
168
171
|
|
|
169
172
|
it("无 header + get_state 无响应 → close 主动 settle 不阻塞,identity 不写入", async () => {
|
|
@@ -7,9 +7,9 @@ import * as fs from "node:fs";
|
|
|
7
7
|
import * as os from "node:os";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
|
|
10
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
11
11
|
|
|
12
|
-
import { readActivePendingFromSessionFile } from "../session-pending.ts";
|
|
12
|
+
import { readActivePendingFromSessionFile, clearPendingCursors } from "../session-pending.ts";
|
|
13
13
|
|
|
14
14
|
function makeTmpSessionFile(lines: string[]): string {
|
|
15
15
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "session-pending-test-"));
|
|
@@ -136,3 +136,62 @@ describe("readActivePendingFromSessionFile", () => {
|
|
|
136
136
|
expect(readActivePendingFromSessionFile(file)).toEqual({ count: 1, recentUnregister: false });
|
|
137
137
|
});
|
|
138
138
|
});
|
|
139
|
+
|
|
140
|
+
// [perf] 增量游标行为:同文件重复判定(层主被多个后代唤醒 N 次 → N 次 agent_end)
|
|
141
|
+
// 只读上次 offset 之后的新增行,累计差集语义与全量读一致。
|
|
142
|
+
describe("readActivePendingFromSessionFile — 增量游标 [perf]", () => {
|
|
143
|
+
let dir: string;
|
|
144
|
+
let file: string;
|
|
145
|
+
|
|
146
|
+
beforeEach(() => {
|
|
147
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), "session-pending-incr-"));
|
|
148
|
+
file = path.join(dir, "session.jsonl");
|
|
149
|
+
clearPendingCursors();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
afterEach(() => {
|
|
153
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("首次全量 + append 后增量:unregister 只抵消对应 id(与全量读一致)", () => {
|
|
157
|
+
fs.writeFileSync(file, mkRegister("d1") + "\n" + mkRegister("d2") + "\n");
|
|
158
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(2);
|
|
159
|
+
|
|
160
|
+
fs.appendFileSync(file, mkUnregister("d1", "completed") + "\n");
|
|
161
|
+
const r = readActivePendingFromSessionFile(file);
|
|
162
|
+
expect(r.error).toBeUndefined();
|
|
163
|
+
expect(r.count).toBe(1);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("EOF 半行(append 竞态)不入账,补全换行后入账", () => {
|
|
167
|
+
fs.writeFileSync(file, mkRegister("d1") + "\n");
|
|
168
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(1);
|
|
169
|
+
|
|
170
|
+
// 半行:无尾换行 → offset 不推进该行
|
|
171
|
+
const full = mkUnregister("d1", "completed", new Date().toISOString());
|
|
172
|
+
fs.appendFileSync(file, full.slice(0, full.length - 4));
|
|
173
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(1);
|
|
174
|
+
|
|
175
|
+
// 补全(剩余字符 + 换行)→ 消费
|
|
176
|
+
fs.appendFileSync(file, full.slice(-4) + "\n");
|
|
177
|
+
const r = readActivePendingFromSessionFile(file);
|
|
178
|
+
expect(r.count).toBe(0);
|
|
179
|
+
expect(r.recentUnregister).toBe(true);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("文件 truncate/重建(size 回退)→ 重置游标从头全读", () => {
|
|
183
|
+
fs.writeFileSync(file, mkRegister("d1") + "\n" + mkRegister("d2") + "\n");
|
|
184
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(2);
|
|
185
|
+
|
|
186
|
+
fs.writeFileSync(file, mkRegister("d3") + "\n");
|
|
187
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(1);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("clearPendingCursors 后重新全量(累计 entries 不残留旧账)", () => {
|
|
191
|
+
fs.writeFileSync(file, mkRegister("d1") + "\n");
|
|
192
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(1);
|
|
193
|
+
clearPendingCursors();
|
|
194
|
+
fs.appendFileSync(file, mkRegister("d2") + "\n");
|
|
195
|
+
expect(readActivePendingFromSessionFile(file).count).toBe(2);
|
|
196
|
+
});
|
|
197
|
+
});
|