@zhushanwen/pi-subagent-workflow 7.4.0 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/package.json +7 -4
- package/scripts/rfl.mjs +308 -0
- package/skills/workflow-script-format/SKILL.md +1 -1
- package/src/execution/__tests__/__fixtures__/truncline.snapshot.json +1 -0
- 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 +197 -15
- 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 +37 -10
- 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 +442 -54
- 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 +629 -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 +300 -90
- package/src/execution/__tests__/worktree-pid-registration.integration.test.ts +17 -13
- package/src/execution/__tests__/worktree-reconcile.integration.test.ts +181 -0
- package/src/execution/__tests__/worktree-registry.test.ts +72 -34
- package/src/execution/agent-result-mapper.ts +4 -1
- package/src/execution/argv-mirror.ts +21 -2
- package/src/execution/channel-registry-access.ts +3 -1
- package/src/execution/execution-record.ts +126 -9
- package/src/execution/finalize-record.ts +99 -13
- package/src/execution/idle-gc.ts +47 -0
- package/src/execution/lifecycle-manager.ts +491 -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-entry.ts +118 -0
- package/src/execution/record-store.ts +844 -108
- package/src/execution/session-pending.ts +121 -49
- package/src/execution/session-reconstructor.ts +224 -7
- package/src/execution/session-runner.ts +713 -316
- 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 +917 -138
- package/src/execution/temp-prompt.ts +8 -3
- package/src/execution/turn-limiter.ts +14 -0
- package/src/execution/types.ts +218 -19
- package/src/execution/worktree-manager.ts +449 -59
- package/src/execution/worktree-registry.ts +97 -29
- package/src/index.ts +318 -20
- 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 +17 -4
- package/src/interface/__tests__/tool-render.test.ts +10 -8
- package/src/interface/__tests__/tool-workflow-script-generate.test.ts +103 -26
- package/src/interface/__tests__/tool-workflow-throw-paths.test.ts +179 -0
- 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 +53 -20
- 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 +81 -16
- package/src/interface/subagents.ts +2 -1
- package/src/interface/tool-render.ts +21 -27
- package/src/interface/tool-workflow-script.ts +29 -33
- package/src/interface/tool-workflow.ts +67 -100
- package/src/interface/views/WorkflowsView.ts +89 -32
- package/src/interface/views/__tests__/WorkflowsView-signature.test.ts +264 -0
- package/src/interface/views/detail-content.ts +1 -1
- package/src/interface/views/format.ts +3 -3
- package/src/orchestration/__tests__/__fixtures__/worker-template.snapshot.txt +333 -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 +144 -1
- package/src/orchestration/__tests__/jsonl-run-store-loadall-sources.test.ts +171 -0
- package/src/orchestration/__tests__/jsonl-run-store-session-file.test.ts +820 -19
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +0 -2
- package/src/orchestration/__tests__/lifecycle-runid-injection.test.ts +96 -0
- package/src/orchestration/__tests__/lifecycle.test.ts +332 -149
- package/src/orchestration/__tests__/review-fix-loop-e2e.test.ts +1108 -19
- package/src/orchestration/__tests__/skill-discovery.test.ts +130 -61
- package/src/orchestration/__tests__/test-mocks.ts +197 -0
- package/src/orchestration/__tests__/worker-returnmeta-passthrough.test.ts +164 -0
- package/src/orchestration/__tests__/worker-script-template-snapshot.test.ts +110 -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 +38 -40
- 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 +137 -30
- package/src/orchestration/execute-agent-call.ts +44 -10
- package/src/orchestration/jsonl-run-store.ts +397 -71
- package/src/orchestration/launcher.ts +7 -1
- package/src/orchestration/lifecycle.ts +145 -133
- 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 +38 -11
- 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 +79 -0
- package/src/shared/__tests__/schema-jsonify.test.ts +81 -0
- package/src/shared/agent-ref.ts +22 -1
- package/src/shared/resource-discovery.ts +162 -59
- package/src/shared/schema-jsonify.ts +56 -0
- package/workflows/review-fix-loop-utils.cjs +542 -32
- package/workflows/review-fix-loop.js +462 -109
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// src/execution/__tests__/run-spawn-chatmode-settled.test.ts
|
|
2
|
+
//
|
|
3
|
+
// [V2 模块 3] chatMode agent_settled 信号换挂单测(Step 3)。
|
|
4
|
+
//
|
|
5
|
+
// 验证 session-runner 的 V2 改动(5 处):
|
|
6
|
+
// ① chatMode record agent_end 不触发 SIGTERM(进程保活,等 agent_settled)
|
|
7
|
+
// ② chatMode record agent_settled 触发 armIdleTimer(fake timer + 超时后 kill)
|
|
8
|
+
// ③ chatMode agent_settled 调 ctx.onRoundSettled(挂载点,mock 断言被调)
|
|
9
|
+
// ④ 非 chatMode agent_settled 不 armIdleTimer、不调 onRoundSettled
|
|
10
|
+
//
|
|
11
|
+
// mock 结构与 run-spawn-edges.test.ts 一致(FakeChild + session-pending 受控 count=0)。
|
|
12
|
+
// lifecycle-manager 用真实模块(hasIdleTimer / _resetLifecycleState 可观测 + 隔离)。
|
|
13
|
+
|
|
14
|
+
import { spawn } from "node:child_process";
|
|
15
|
+
import * as fs from "node:fs";
|
|
16
|
+
|
|
17
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
18
|
+
|
|
19
|
+
vi.mock("node:child_process", async () => {
|
|
20
|
+
const { FakeChild } = await import("./helpers/spawn-mock.ts");
|
|
21
|
+
return {
|
|
22
|
+
spawn: vi.fn(() => new FakeChild()),
|
|
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
|
+
),
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
vi.mock("node:fs", async () => {
|
|
36
|
+
const actual = await import("node:fs");
|
|
37
|
+
return {
|
|
38
|
+
default: {
|
|
39
|
+
...actual,
|
|
40
|
+
mkdirSync: vi.fn(),
|
|
41
|
+
existsSync: vi.fn(() => false),
|
|
42
|
+
appendFileSync: vi.fn(),
|
|
43
|
+
writeFileSync: vi.fn(),
|
|
44
|
+
readdirSync: vi.fn(() => []),
|
|
45
|
+
},
|
|
46
|
+
mkdirSync: vi.fn(),
|
|
47
|
+
existsSync: vi.fn(() => false),
|
|
48
|
+
appendFileSync: vi.fn(),
|
|
49
|
+
writeFileSync: vi.fn(),
|
|
50
|
+
readdirSync: vi.fn(() => []),
|
|
51
|
+
promises: actual.promises,
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
vi.mock("../alive-store.ts", () => ({
|
|
56
|
+
writeAliveMarker: vi.fn(),
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
// chatMode 测试不依赖后代判定(chatMode agent_end 早返回跳过 readActivePending),统一 count=0。
|
|
60
|
+
// 非 chatMode 用例 ④ 的 agent_end 走正常 kill 分支(count=0 → SIGTERM)。
|
|
61
|
+
vi.mock("../session-pending.ts", () => ({
|
|
62
|
+
readActivePendingFromSessionFile: vi.fn(() => ({ count: 0 })),
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
vi.mock("../temp-prompt.ts", () => ({
|
|
66
|
+
writePromptToTempFile: vi.fn(async (agent: string) => {
|
|
67
|
+
const safeName = agent.replace(/[^\w.-]+/g, "_");
|
|
68
|
+
return { dir: `/tmp/fake-${safeName}`, filePath: `/tmp/fake-${safeName}/prompt-${safeName}.md` };
|
|
69
|
+
}),
|
|
70
|
+
cleanupTempPrompt: vi.fn(async () => {}),
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
import { runSpawn } from "../session-runner.ts";
|
|
74
|
+
import type { SessionRunnerContext } from "../session-runner.ts";
|
|
75
|
+
import {
|
|
76
|
+
DEFAULT_IDLE_TIMEOUT_MS,
|
|
77
|
+
hasIdleTimer,
|
|
78
|
+
_resetLifecycleState,
|
|
79
|
+
} from "../lifecycle-manager.ts";
|
|
80
|
+
import { createRecord } from "../execution-record.ts";
|
|
81
|
+
import type { ExecutionRecord } from "../types.ts";
|
|
82
|
+
import {
|
|
83
|
+
emitStdoutLine,
|
|
84
|
+
type FakeChild,
|
|
85
|
+
lastSpawnedChild as lastSpawnedChildOf,
|
|
86
|
+
makeCtx,
|
|
87
|
+
makeOpts,
|
|
88
|
+
makeRecord,
|
|
89
|
+
sessionHeader,
|
|
90
|
+
waitForSpawn as waitForSpawnOf,
|
|
91
|
+
} from "./helpers/spawn-mock.ts";
|
|
92
|
+
|
|
93
|
+
const mockSpawn = vi.mocked(spawn);
|
|
94
|
+
|
|
95
|
+
const lastSpawnedChild = (): FakeChild => lastSpawnedChildOf(mockSpawn);
|
|
96
|
+
const waitForSpawn = (timeoutMs = 1000): Promise<void> => waitForSpawnOf(mockSpawn, timeoutMs);
|
|
97
|
+
|
|
98
|
+
/** 构造 chatMode record(spawn-mock.makeRecord 默认非 chatMode,此处显式开启)。 */
|
|
99
|
+
function makeChatModeRecord(id = "sa-chat"): ExecutionRecord {
|
|
100
|
+
return createRecord(id, {
|
|
101
|
+
agent: "general-purpose",
|
|
102
|
+
model: "test-model",
|
|
103
|
+
mode: "sync",
|
|
104
|
+
task: "chat task",
|
|
105
|
+
slug: "chat",
|
|
106
|
+
startedAt: 1_000_000,
|
|
107
|
+
rootSessionId: "root-session",
|
|
108
|
+
parentRecordId: undefined,
|
|
109
|
+
depth: 0,
|
|
110
|
+
chatMode: true,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
describe("[V2 模块 3] chatMode agent_settled 信号换挂", () => {
|
|
115
|
+
beforeEach(() => {
|
|
116
|
+
vi.clearAllMocks();
|
|
117
|
+
// lifecycle-manager 是模块级单例(idleTimers Map 跨用例共享),每用例前清空防泄漏。
|
|
118
|
+
_resetLifecycleState();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
afterEach(() => {
|
|
122
|
+
vi.restoreAllMocks();
|
|
123
|
+
vi.useRealTimers();
|
|
124
|
+
_resetLifecycleState();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("① chatMode agent_end → 不触发 SIGTERM(进程保活,等 agent_settled)", async () => {
|
|
128
|
+
const record = makeChatModeRecord("sa-chat-end");
|
|
129
|
+
const promise = runSpawn(record, "Task: chatmode-end", makeOpts(), makeCtx());
|
|
130
|
+
|
|
131
|
+
await waitForSpawn();
|
|
132
|
+
const child = lastSpawnedChild();
|
|
133
|
+
|
|
134
|
+
emitStdoutLine(child, sessionHeader());
|
|
135
|
+
emitStdoutLine(child, { type: "agent_end", willRetry: false });
|
|
136
|
+
// 让事件 pump 处理 agent_end(stream flush 异步)
|
|
137
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
138
|
+
|
|
139
|
+
// [V2 决策 1] chatMode agent_end 不 kill——进程不因轮次死,等 agent_settled。
|
|
140
|
+
expect(child.killed).toBe(false);
|
|
141
|
+
// agent_end 不 arm idle timer(arm 点是 agent_settled,不是 agent_end)
|
|
142
|
+
expect(hasIdleTimer(record.id)).toBe(false);
|
|
143
|
+
|
|
144
|
+
// 收尾:手动 close 让 runSpawn resolve(模拟后续 idle timer 超时 kill / 外部终止)
|
|
145
|
+
child.stdout.end();
|
|
146
|
+
child.stderr.end();
|
|
147
|
+
child.emit("close", 143);
|
|
148
|
+
const result = await promise;
|
|
149
|
+
expect(result.success).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("② chatMode agent_settled → armIdleTimer,idle 超时后 SIGTERM kill", async () => {
|
|
153
|
+
const record = makeChatModeRecord("sa-chat-settled");
|
|
154
|
+
const promise = runSpawn(record, "Task: chatmode-settled", makeOpts(), makeCtx());
|
|
155
|
+
|
|
156
|
+
await waitForSpawn();
|
|
157
|
+
const child = lastSpawnedChild();
|
|
158
|
+
|
|
159
|
+
// 必须在 emit agent_settled 前启用 fake timers——armIdleTimer 内 setTimeout 在 agent_settled
|
|
160
|
+
// 处理器里新建,新建时已是 fake timer 才可 advance。不 fake setImmediate:stream flush 靠
|
|
161
|
+
// 真实事件循环。对齐 run-spawn-edges MF-3/MF-4 模式。
|
|
162
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
|
|
163
|
+
try {
|
|
164
|
+
emitStdoutLine(child, sessionHeader());
|
|
165
|
+
emitStdoutLine(child, { type: "agent_end", willRetry: false });
|
|
166
|
+
await new Promise((r) => setImmediate(r));
|
|
167
|
+
emitStdoutLine(child, { type: "agent_settled" });
|
|
168
|
+
await new Promise((r) => setImmediate(r));
|
|
169
|
+
|
|
170
|
+
// agent_settled → arm idle timer(DEFAULT_IDLE_TIMEOUT_MS = 5min)
|
|
171
|
+
expect(hasIdleTimer(record.id)).toBe(true);
|
|
172
|
+
// 进程仍存活(idle timer 未到期)
|
|
173
|
+
expect(child.killed).toBe(false);
|
|
174
|
+
|
|
175
|
+
// 未到 idle timeout(5min - 1ms):不 kill
|
|
176
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_IDLE_TIMEOUT_MS - 1);
|
|
177
|
+
expect(child.killed).toBe(false);
|
|
178
|
+
|
|
179
|
+
// idle timeout 到期:onTimeout → child.kill("SIGTERM")(复用现有 kill 路径)
|
|
180
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
181
|
+
expect(child.killed).toBe(true);
|
|
182
|
+
expect(child.killSignal).toBe("SIGTERM");
|
|
183
|
+
// timer 触发后从 Map 移除(armIdleTimer 内 setTimeout 回调 idleTimers.delete)
|
|
184
|
+
expect(hasIdleTimer(record.id)).toBe(false);
|
|
185
|
+
} finally {
|
|
186
|
+
vi.useRealTimers();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 收尾:close 让 runSpawn resolve
|
|
190
|
+
child.stdout.end();
|
|
191
|
+
child.stderr.end();
|
|
192
|
+
child.emit("close", 143);
|
|
193
|
+
const result = await promise;
|
|
194
|
+
expect(result.success).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("③ chatMode agent_settled → 调 ctx.onRoundSettled(Step 3 挂载点,回调被调 1 次)", async () => {
|
|
198
|
+
const record = makeChatModeRecord("sa-chat-cb");
|
|
199
|
+
const onRoundSettled = vi.fn();
|
|
200
|
+
const ctx: SessionRunnerContext = makeCtx({ onRoundSettled });
|
|
201
|
+
const promise = runSpawn(record, "Task: chatmode-cb", makeOpts(), ctx);
|
|
202
|
+
|
|
203
|
+
await waitForSpawn();
|
|
204
|
+
const child = lastSpawnedChild();
|
|
205
|
+
|
|
206
|
+
emitStdoutLine(child, sessionHeader());
|
|
207
|
+
emitStdoutLine(child, { type: "agent_end", willRetry: false });
|
|
208
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
209
|
+
emitStdoutLine(child, { type: "agent_settled" });
|
|
210
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
211
|
+
|
|
212
|
+
// agent_settled → onRoundSettled 被调,入参是当前 record
|
|
213
|
+
expect(onRoundSettled).toHaveBeenCalledTimes(1);
|
|
214
|
+
expect(onRoundSettled).toHaveBeenCalledWith(record);
|
|
215
|
+
|
|
216
|
+
// 收尾:手动 close(idle timer 仍 armed,由 afterEach _resetLifecycleState 清理)
|
|
217
|
+
child.stdout.end();
|
|
218
|
+
child.stderr.end();
|
|
219
|
+
child.emit("close", 143);
|
|
220
|
+
const result = await promise;
|
|
221
|
+
expect(result.success).toBe(true);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("④ 非 chatMode agent_settled → 不 armIdleTimer、不调 onRoundSettled", async () => {
|
|
225
|
+
// makeRecord 默认非 chatMode(createRecord 未传 chatMode)
|
|
226
|
+
const record = makeRecord("sa-nonchat-settled");
|
|
227
|
+
const onRoundSettled = vi.fn();
|
|
228
|
+
const ctx: SessionRunnerContext = makeCtx({ onRoundSettled });
|
|
229
|
+
const promise = runSpawn(record, "Task: nonchat-settled", makeOpts(), ctx);
|
|
230
|
+
|
|
231
|
+
await waitForSpawn();
|
|
232
|
+
const child = lastSpawnedChild();
|
|
233
|
+
|
|
234
|
+
emitStdoutLine(child, sessionHeader());
|
|
235
|
+
// 直接 emit agent_settled(非 chatMode:handler 应忽略,不 arm、不调回调)
|
|
236
|
+
emitStdoutLine(child, { type: "agent_settled" });
|
|
237
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
238
|
+
|
|
239
|
+
expect(hasIdleTimer(record.id)).toBe(false);
|
|
240
|
+
expect(onRoundSettled).not.toHaveBeenCalled();
|
|
241
|
+
|
|
242
|
+
// 收尾:emit agent_end 走正常 kill 分支(count=0 → SIGTERM)→ close resolve
|
|
243
|
+
emitStdoutLine(child, { type: "agent_end", willRetry: false });
|
|
244
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
245
|
+
expect(child.killed).toBe(true); // 非 chatMode agent_end → kill(现有行为,V2 不动)
|
|
246
|
+
|
|
247
|
+
child.stdout.end();
|
|
248
|
+
child.stderr.end();
|
|
249
|
+
child.emit("close", 143);
|
|
250
|
+
const result = await promise;
|
|
251
|
+
expect(result.success).toBe(true);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -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();
|