@zhushanwen/pi-subagent-workflow 7.4.0 → 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 +15 -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__/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/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 +270 -68
- 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 +200 -19
- 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 +17 -4
- package/src/interface/__tests__/tool-render.test.ts +10 -8
- 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 +43 -16
- 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 +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/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 +130 -61
- 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
|
@@ -21,8 +21,6 @@ import type { WorkflowScript } from "../models/workflow-script.ts";
|
|
|
21
21
|
vi.mock("../lifecycle.ts", () => ({
|
|
22
22
|
runWorkflow: vi.fn(),
|
|
23
23
|
abortRun: vi.fn(async () => {}),
|
|
24
|
-
pauseRun: vi.fn(async () => {}),
|
|
25
|
-
resumeRun: vi.fn(async () => {}),
|
|
26
24
|
scheduleTimeBudget: vi.fn(() => undefined),
|
|
27
25
|
}));
|
|
28
26
|
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* lifecycle —
|
|
2
|
+
* lifecycle — runWorkflow/abortRun/terminateRunningRuns/scheduleTimeBudget/evict 测试。
|
|
3
3
|
*
|
|
4
4
|
* 用真实 WorkflowRun(含真实状态机 + I1/I2 不变式守卫)+ mock RunRuntime(释放副作用
|
|
5
5
|
* 可控)+ mock LifecycleDeps(store/workerHost/eventBus 可观察)。这样能真正测到
|
|
6
6
|
* transition/assignRuntime/releaseRuntime 的状态机逻辑,而非全 mock 聚合根。
|
|
7
7
|
*
|
|
8
8
|
* 覆盖:
|
|
9
|
-
* - pauseRun:running → paused(releaseRuntime:worker terminate + controller abort)
|
|
10
|
-
* + 在飞 call 清理 + store.save
|
|
11
|
-
* - resumeRun:paused → running(workerHost.start 重建 + assignRuntime + 时间预算重排)
|
|
12
|
-
* - scheduleTimeBudget:定时器到期 → abortRun(done,time_limited)(用 fake timers)
|
|
13
9
|
* - runWorkflow:spec → 创建 run + workerHost.start + store.save + emit pending:register
|
|
14
|
-
* - abortRun:done no-op / running→done
|
|
10
|
+
* - abortRun:done no-op / running→done + emit pending:unregister
|
|
11
|
+
* - terminateRunningRuns:session 切换/关闭时仅 running 被终止(done,failed 落盘)
|
|
12
|
+
* - scheduleTimeBudget:定时器到期 → abortRun(done,time_limited)(用 fake timers)
|
|
13
|
+
* - evictDoneRunsBeyondCap:done run 内存淘汰白名单/排序/tie
|
|
15
14
|
*/
|
|
16
15
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
17
16
|
|
|
18
17
|
import {
|
|
19
18
|
abortRun,
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
evictDoneRunsBeyondCap,
|
|
20
|
+
MAX_RETAINED_DONE_RUNS,
|
|
22
21
|
runWorkflow,
|
|
23
22
|
scheduleTimeBudget,
|
|
23
|
+
terminateRunningRuns,
|
|
24
24
|
} from "../lifecycle.ts";
|
|
25
25
|
import { ArgsValidationError } from "../args-validator.ts";
|
|
26
26
|
import { Budget } from "../models/budget.ts";
|
|
@@ -53,8 +53,9 @@ function makeSpec(opts: {
|
|
|
53
53
|
/**
|
|
54
54
|
* 构造一个 status="running" 的真实 WorkflowRun,注入 mock RunRuntime。
|
|
55
55
|
*
|
|
56
|
-
* 流程:new WorkflowRun(status=
|
|
57
|
-
* mock runtime 的 worker.terminate / controller.abort /
|
|
56
|
+
* 流程:new WorkflowRun(status=running,I1 构造期跳过)→ assignRuntime
|
|
57
|
+
* (注入 runtime,恢复 I1)。mock runtime 的 worker.terminate / controller.abort /
|
|
58
|
+
* release 均可观察。
|
|
58
59
|
*/
|
|
59
60
|
/** flush microtask 队列多次,让 void .then().catch() + async 链跑完。
|
|
60
61
|
*
|
|
@@ -75,7 +76,7 @@ function makeRunningRealRun(
|
|
|
75
76
|
runId,
|
|
76
77
|
spec,
|
|
77
78
|
{
|
|
78
|
-
status: "
|
|
79
|
+
status: "running",
|
|
79
80
|
budget: new Budget({ maxTokens: 1000 }),
|
|
80
81
|
calls: new Map(),
|
|
81
82
|
trace: new Trace(),
|
|
@@ -124,144 +125,6 @@ afterEach(() => {
|
|
|
124
125
|
vi.useRealTimers();
|
|
125
126
|
});
|
|
126
127
|
|
|
127
|
-
// ── pauseRun ─────────────────────────────────────────────────
|
|
128
|
-
|
|
129
|
-
describe("pauseRun", () => {
|
|
130
|
-
it("running run → paused:releaseRuntime(worker terminate + controller abort)+ store.save", async () => {
|
|
131
|
-
const { run, terminate, abort } = makeRunningRealRun("wf-pause-1");
|
|
132
|
-
const deps = makeDeps();
|
|
133
|
-
deps.runs.set("wf-pause-1", run);
|
|
134
|
-
|
|
135
|
-
await pauseRun("wf-pause-1", deps);
|
|
136
|
-
|
|
137
|
-
expect(run.state.status).toBe("paused");
|
|
138
|
-
expect(run.meta.pausedAt).toBeDefined();
|
|
139
|
-
// releaseRuntime 释放了 worker + controller
|
|
140
|
-
expect(terminate).toHaveBeenCalledTimes(1);
|
|
141
|
-
expect(abort).toHaveBeenCalledTimes(1);
|
|
142
|
-
// runtime 已解绑(I1:paused ⟺ runtime undefined)
|
|
143
|
-
expect(run.runtime).toBeUndefined();
|
|
144
|
-
// 持久化
|
|
145
|
-
expect(deps.store.save).toHaveBeenCalledTimes(1);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it("清留在飞 call(status !== done)及其 trace 节点", async () => {
|
|
149
|
-
const { run } = makeRunningRealRun("wf-pause-2");
|
|
150
|
-
// 注入一个未完成的在飞 call
|
|
151
|
-
run.state.calls.set(7, {
|
|
152
|
-
id: 7,
|
|
153
|
-
status: "running",
|
|
154
|
-
attempts: 1,
|
|
155
|
-
} as never);
|
|
156
|
-
// 注入一个已完成 call(应保留)
|
|
157
|
-
run.state.calls.set(8, {
|
|
158
|
-
id: 8,
|
|
159
|
-
status: "done",
|
|
160
|
-
result: { content: "ok" },
|
|
161
|
-
} as never);
|
|
162
|
-
const deps = makeDeps();
|
|
163
|
-
deps.runs.set("wf-pause-2", run);
|
|
164
|
-
|
|
165
|
-
await pauseRun("wf-pause-2", deps);
|
|
166
|
-
|
|
167
|
-
// 在飞 call(running)被移除;已完成 call(done)保留
|
|
168
|
-
expect(run.state.calls.has(7)).toBe(false);
|
|
169
|
-
expect(run.state.calls.has(8)).toBe(true);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it("runId 不存在 → 抛错", async () => {
|
|
173
|
-
const deps = makeDeps();
|
|
174
|
-
await expect(pauseRun("wf-missing", deps)).rejects.toThrow("not found");
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("status !== running → 抛错(只允许 running 暂停)", async () => {
|
|
178
|
-
const { run } = makeRunningRealRun("wf-pause-3");
|
|
179
|
-
run.transition("paused"); // 先 pause → paused
|
|
180
|
-
const deps = makeDeps();
|
|
181
|
-
deps.runs.set("wf-pause-3", run);
|
|
182
|
-
|
|
183
|
-
await expect(pauseRun("wf-pause-3", deps)).rejects.toThrow("only 'running' can be paused");
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// ── resumeRun ────────────────────────────────────────────────
|
|
188
|
-
|
|
189
|
-
describe("resumeRun", () => {
|
|
190
|
-
it("paused → running:workerHost.start 重建 worker + assignRuntime + 时间预算重排", async () => {
|
|
191
|
-
const { run } = makeRunningRealRun("wf-resume-1", { budgetTimeMs: 5000 });
|
|
192
|
-
run.transition("paused"); // → paused
|
|
193
|
-
const deps = makeDeps();
|
|
194
|
-
deps.runs.set("wf-resume-1", run);
|
|
195
|
-
|
|
196
|
-
await resumeRun("wf-resume-1", deps);
|
|
197
|
-
|
|
198
|
-
expect(run.state.status).toBe("running");
|
|
199
|
-
expect(run.runtime).toBeDefined(); // assignRuntime 绑定新 runtime
|
|
200
|
-
// workerHost.start 被调(重建 worker)
|
|
201
|
-
expect(deps.workerHost.start).toHaveBeenCalledTimes(1);
|
|
202
|
-
// 时间预算重排(budgetTimeMs > 0):resumeRun 内调本文件 scheduleTimeBudget,
|
|
203
|
-
// 结果存入 run.runtime.timeBudgetTimer。budgetTimeMs <= 0 时为 undefined。
|
|
204
|
-
expect(run.runtime!.timeBudgetTimer).toBeDefined();
|
|
205
|
-
// 持久化
|
|
206
|
-
expect(deps.store.save).toHaveBeenCalledTimes(1);
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it("TC13: resume 重建 worker 收到 coerce 后 args(WQ2 一致性不变式)", async () => {
|
|
210
|
-
// runWorkflow 首行校验 + coerceTypes 原地规范化 spec.args → pause → resume 重建
|
|
211
|
-
// worker 用同一 args 对象(run.spec === spec)→ start 收到 boolean false。
|
|
212
|
-
const deps = makeDeps();
|
|
213
|
-
const spec = makeSpec({
|
|
214
|
-
parameters: {
|
|
215
|
-
type: "object",
|
|
216
|
-
properties: { autoCommit: { type: "boolean" } },
|
|
217
|
-
required: [],
|
|
218
|
-
},
|
|
219
|
-
args: { autoCommit: "false" },
|
|
220
|
-
});
|
|
221
|
-
const runId = await runWorkflow(spec, deps);
|
|
222
|
-
expect(spec.args.autoCommit).toBe(false); // 原地 coerce 生效
|
|
223
|
-
const run = deps.runs.get(runId)!;
|
|
224
|
-
run.transition("paused");
|
|
225
|
-
deps.runs.set(runId, run);
|
|
226
|
-
deps.workerHost.start.mockClear();
|
|
227
|
-
|
|
228
|
-
await resumeRun(runId, deps);
|
|
229
|
-
|
|
230
|
-
const startArgs = deps.workerHost.start.mock.calls[0]?.[1] as
|
|
231
|
-
| Record<string, unknown>
|
|
232
|
-
| undefined;
|
|
233
|
-
expect(startArgs).toBeDefined();
|
|
234
|
-
expect(startArgs!.autoCommit).toBe(false);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it("无 budgetTimeMs 时 resume 不调度时间预算计时器", async () => {
|
|
238
|
-
const { run } = makeRunningRealRun("wf-resume-no-budget");
|
|
239
|
-
run.transition("paused");
|
|
240
|
-
const deps = makeDeps();
|
|
241
|
-
deps.runs.set("wf-resume-no-budget", run);
|
|
242
|
-
|
|
243
|
-
await resumeRun("wf-resume-no-budget", deps);
|
|
244
|
-
|
|
245
|
-
expect(run.state.status).toBe("running");
|
|
246
|
-
// budgetTimeMs 未设 → timeBudgetTimer 为 undefined
|
|
247
|
-
expect(run.runtime!.timeBudgetTimer).toBeUndefined();
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
it("runId 不存在 → 抛错", async () => {
|
|
251
|
-
const deps = makeDeps();
|
|
252
|
-
await expect(resumeRun("wf-missing", deps)).rejects.toThrow("not found");
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it("status !== paused → 抛错(只允许 paused 恢复)", async () => {
|
|
256
|
-
const { run } = makeRunningRealRun("wf-resume-2");
|
|
257
|
-
// 仍 running
|
|
258
|
-
const deps = makeDeps();
|
|
259
|
-
deps.runs.set("wf-resume-2", run);
|
|
260
|
-
|
|
261
|
-
await expect(resumeRun("wf-resume-2", deps)).rejects.toThrow("only 'paused' can be resumed");
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
128
|
// ── scheduleTimeBudget ───────────────────────────────────────
|
|
266
129
|
|
|
267
130
|
describe("scheduleTimeBudget", () => {
|
|
@@ -341,6 +204,44 @@ describe("runWorkflow", () => {
|
|
|
341
204
|
});
|
|
342
205
|
});
|
|
343
206
|
|
|
207
|
+
it("创建即 running:构造即 running,runs.set 在 assignRuntime 后(I1 窗口对外不可见)", async () => {
|
|
208
|
+
const deps = makeDeps();
|
|
209
|
+
// worker.start 被调时探测 runs 注册状态——证明 runs.set 在 assignRuntime 之后
|
|
210
|
+
let runsSizeAtWorkerStart = -1;
|
|
211
|
+
deps.workerHost.start = vi.fn(() => {
|
|
212
|
+
runsSizeAtWorkerStart = deps.runs.size;
|
|
213
|
+
return { postMessage: vi.fn(), terminate: vi.fn(async () => {}) };
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const runId = await runWorkflow(makeSpec(), deps);
|
|
217
|
+
|
|
218
|
+
// worker.start 执行时 run 尚未注册(I1 跳过窗口不外泄)
|
|
219
|
+
expect(runsSizeAtWorkerStart).toBe(0);
|
|
220
|
+
// 完成后已注册,且构造即 running + runtime 已注入(I1 成立)
|
|
221
|
+
expect(deps.runs.has(runId)).toBe(true);
|
|
222
|
+
const run = deps.runs.get(runId)!;
|
|
223
|
+
expect(run.state.status).toBe("running");
|
|
224
|
+
expect(run.runtime).toBeDefined();
|
|
225
|
+
// save 落盘的是恢复 I1 后的聚合(status running + runtime 已绑定)
|
|
226
|
+
const savedRun = deps.store.save.mock.calls[0]![0] as WorkflowRun;
|
|
227
|
+
expect(savedRun).toBe(run);
|
|
228
|
+
expect(savedRun.state.status).toBe("running");
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("worker.start 抛错 → runWorkflow 拒绝且 runs 无孤儿注册", async () => {
|
|
232
|
+
const deps = makeDeps();
|
|
233
|
+
deps.workerHost.start = vi.fn(() => {
|
|
234
|
+
throw new Error("worker boot failed");
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
await expect(runWorkflow(makeSpec(), deps)).rejects.toThrow("worker boot failed");
|
|
238
|
+
|
|
239
|
+
// 无孤儿:启动失败时 run 未注册进 deps.runs(runs.set 在 start 之后)
|
|
240
|
+
expect(deps.runs.size).toBe(0);
|
|
241
|
+
expect(deps.store.save).not.toHaveBeenCalled();
|
|
242
|
+
expect(deps.eventBus.emit).not.toHaveBeenCalled();
|
|
243
|
+
});
|
|
244
|
+
|
|
344
245
|
it("带 budgetTimeMs 时调度时间预算计时器", async () => {
|
|
345
246
|
const deps = makeDeps();
|
|
346
247
|
const scheduleTimeBudgetSpy = vi.fn(() => undefined);
|
|
@@ -440,3 +341,285 @@ describe("abortRun", () => {
|
|
|
440
341
|
await expect(abortRun("wf-missing", deps)).rejects.toThrow("not found");
|
|
441
342
|
});
|
|
442
343
|
});
|
|
344
|
+
|
|
345
|
+
// ── terminateRunningRuns ─────────────────────────────────────
|
|
346
|
+
|
|
347
|
+
describe("terminateRunningRuns", () => {
|
|
348
|
+
it("多 run 中仅 running 被终止(done run 不动)", async () => {
|
|
349
|
+
const { run: running1 } = makeRunningRealRun("wf-term-1");
|
|
350
|
+
const { run: running2 } = makeRunningRealRun("wf-term-2");
|
|
351
|
+
const { run: doneRun } = makeRunningRealRun("wf-term-done");
|
|
352
|
+
doneRun.transition("done", "completed");
|
|
353
|
+
const deps = makeDeps();
|
|
354
|
+
deps.runs.set("wf-term-1", running1);
|
|
355
|
+
deps.runs.set("wf-term-2", running2);
|
|
356
|
+
deps.runs.set("wf-term-done", doneRun);
|
|
357
|
+
|
|
358
|
+
await terminateRunningRuns(deps, "Session switched: run terminated");
|
|
359
|
+
|
|
360
|
+
// running 全部转 done,failed
|
|
361
|
+
expect(running1.state.status).toBe("done");
|
|
362
|
+
expect(running1.state.reason).toBe("failed");
|
|
363
|
+
expect(running2.state.status).toBe("done");
|
|
364
|
+
expect(running2.state.reason).toBe("failed");
|
|
365
|
+
// done run 不被重写(保留 completed)
|
|
366
|
+
expect(doneRun.state.reason).toBe("completed");
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("每个被终止的 run 发 pending:unregister(reason=failed)且不调 onRunDone", async () => {
|
|
370
|
+
const { run: r1 } = makeRunningRealRun("wf-term-3");
|
|
371
|
+
const { run: r2 } = makeRunningRealRun("wf-term-4");
|
|
372
|
+
const deps = makeDeps();
|
|
373
|
+
deps.runs.set("wf-term-3", r1);
|
|
374
|
+
deps.runs.set("wf-term-4", r2);
|
|
375
|
+
|
|
376
|
+
await terminateRunningRuns(deps, "Session shutdown: run terminated");
|
|
377
|
+
|
|
378
|
+
expect(deps.eventBus.emit).toHaveBeenCalledWith("pending:unregister", {
|
|
379
|
+
id: "wf-term-3",
|
|
380
|
+
reason: "failed",
|
|
381
|
+
});
|
|
382
|
+
expect(deps.eventBus.emit).toHaveBeenCalledWith("pending:unregister", {
|
|
383
|
+
id: "wf-term-4",
|
|
384
|
+
reason: "failed",
|
|
385
|
+
});
|
|
386
|
+
// 对齐 session_start 恢复先例:主 agent 已离开本 session,不发完成通知
|
|
387
|
+
expect(deps.onRunDone).not.toHaveBeenCalled();
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it("state.error = reason、reason 字段 = failed、run 落盘(releaseRuntime 解绑 runtime)", async () => {
|
|
391
|
+
const { run, terminate } = makeRunningRealRun("wf-term-5");
|
|
392
|
+
const deps = makeDeps();
|
|
393
|
+
deps.runs.set("wf-term-5", run);
|
|
394
|
+
|
|
395
|
+
await terminateRunningRuns(deps, "Session switched: run terminated");
|
|
396
|
+
|
|
397
|
+
expect(run.state.error).toBe("Session switched: run terminated");
|
|
398
|
+
expect(run.state.reason).toBe("failed");
|
|
399
|
+
expect(run.runtime).toBeUndefined();
|
|
400
|
+
expect(terminate).toHaveBeenCalledTimes(1);
|
|
401
|
+
expect(deps.store.save).toHaveBeenCalledTimes(1);
|
|
402
|
+
expect(deps.store.save).toHaveBeenCalledWith(run);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("单 run save 抛错不中断其余(其余 run 仍落盘 + unregister)", async () => {
|
|
406
|
+
const { run: bad } = makeRunningRealRun("wf-term-err");
|
|
407
|
+
const { run: good } = makeRunningRealRun("wf-term-ok");
|
|
408
|
+
const deps = makeDeps();
|
|
409
|
+
deps.runs.set("wf-term-err", bad);
|
|
410
|
+
deps.runs.set("wf-term-ok", good);
|
|
411
|
+
deps.store.save = vi.fn(async (r: WorkflowRun) => {
|
|
412
|
+
if (r.runId === "wf-term-err") throw new Error("disk full");
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
await terminateRunningRuns(deps, "Session shutdown: run terminated");
|
|
416
|
+
|
|
417
|
+
// 抛错的 run:transition 先于 save,状态已转 done,但 unregister 被 save 失败短路
|
|
418
|
+
expect(bad.state.status).toBe("done");
|
|
419
|
+
expect(bad.state.reason).toBe("failed");
|
|
420
|
+
expect(deps.eventBus.emit).not.toHaveBeenCalledWith("pending:unregister", {
|
|
421
|
+
id: "wf-term-err",
|
|
422
|
+
reason: "failed",
|
|
423
|
+
});
|
|
424
|
+
// 其余 run 正常走完落盘 + unregister(单 run 失败不中断批量终止)
|
|
425
|
+
expect(good.state.status).toBe("done");
|
|
426
|
+
expect(deps.store.save).toHaveBeenCalledWith(good);
|
|
427
|
+
expect(deps.eventBus.emit).toHaveBeenCalledWith("pending:unregister", {
|
|
428
|
+
id: "wf-term-ok",
|
|
429
|
+
reason: "failed",
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// ── evictDoneRunsBeyondCap ────────────────────────────────────
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* 构造可重水合的 WorkflowRun 快照(对齐 crash-recovery.test.ts makeRun 模式——
|
|
438
|
+
* WorkflowRun.reconstruct 与构造同语义:I1 构造期跳过,running 快照合法)。
|
|
439
|
+
*
|
|
440
|
+
* @param runId run 标识
|
|
441
|
+
* @param opts.completedAt 完成时刻 ISO 串(缺省=缺失场景,模拟旧格式/异常快照)
|
|
442
|
+
* @param opts.status 状态(默认 done;running 用于白名单验证)
|
|
443
|
+
*/
|
|
444
|
+
function makeEvictableRun(
|
|
445
|
+
runId: string,
|
|
446
|
+
opts: { completedAt?: string; status?: "running" | "done" } = {},
|
|
447
|
+
): WorkflowRun {
|
|
448
|
+
const status = opts.status ?? "done";
|
|
449
|
+
return WorkflowRun.reconstruct(
|
|
450
|
+
runId,
|
|
451
|
+
{
|
|
452
|
+
scriptSource: "execute() {}",
|
|
453
|
+
args: {},
|
|
454
|
+
scriptName: "test",
|
|
455
|
+
scriptPath: "/fake/test.js",
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
status,
|
|
459
|
+
reason: status === "done" ? "completed" : undefined,
|
|
460
|
+
budget: new Budget({ maxTokens: 1000 }),
|
|
461
|
+
calls: new Map(),
|
|
462
|
+
trace: new Trace(),
|
|
463
|
+
errorLogs: [],
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
startedAt: opts.completedAt ?? "2026-08-15T00:00:00.000Z",
|
|
467
|
+
...(opts.completedAt !== undefined ? { completedAt: opts.completedAt } : {}),
|
|
468
|
+
},
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** ISO 基准时刻(过去时刻),isoAt(n) = 基准 + n 分钟。 */
|
|
473
|
+
const EVICT_T0 = Date.parse("2020-01-01T00:00:00.000Z");
|
|
474
|
+
function isoAt(min: number): string {
|
|
475
|
+
return new Date(EVICT_T0 + min * 60_000).toISOString();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
describe("evictDoneRunsBeyondCap(done run 内存淘汰,K=MAX_RETAINED_DONE_RUNS)", () => {
|
|
479
|
+
it("W3TC1: 状态白名单——仅 done 可淘汰,running 永不删", () => {
|
|
480
|
+
const runs = new Map<string, WorkflowRun>();
|
|
481
|
+
runs.set("wf-done-1", makeEvictableRun("wf-done-1", { completedAt: isoAt(1) }));
|
|
482
|
+
runs.set("wf-done-2", makeEvictableRun("wf-done-2", { completedAt: isoAt(2) }));
|
|
483
|
+
runs.set("wf-done-3", makeEvictableRun("wf-done-3", { completedAt: isoAt(3) }));
|
|
484
|
+
runs.set("wf-run-1", makeEvictableRun("wf-run-1", { status: "running" }));
|
|
485
|
+
runs.set("wf-run-2", makeEvictableRun("wf-run-2", { status: "running" }));
|
|
486
|
+
|
|
487
|
+
const evicted = evictDoneRunsBeyondCap(runs, 2);
|
|
488
|
+
|
|
489
|
+
// 3 done 超保留数 2 → 淘汰最旧 1 个
|
|
490
|
+
expect(evicted).toBe(1);
|
|
491
|
+
// running 全部仍在 Map(白名单外,即使 completedAt 缺失也不参与排序淘汰)
|
|
492
|
+
for (const id of ["wf-run-1", "wf-run-2"]) {
|
|
493
|
+
expect(runs.has(id)).toBe(true);
|
|
494
|
+
}
|
|
495
|
+
// done 仅剩 t2/t3 两个(t1 最旧被淘汰)
|
|
496
|
+
expect(runs.has("wf-done-1")).toBe(false);
|
|
497
|
+
expect(runs.has("wf-done-2")).toBe(true);
|
|
498
|
+
expect(runs.has("wf-done-3")).toBe(true);
|
|
499
|
+
expect(runs.size).toBe(4);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("W3TC2: 按 meta.completedAt 升序淘汰最旧超限项 + 返回淘汰计数(非插入序)", () => {
|
|
503
|
+
const runs = new Map<string, WorkflowRun>();
|
|
504
|
+
// 插入序刻意与 completedAt 序无关:先倒序插入 t24→t0,再 delete+set 重排几个
|
|
505
|
+
// 中段条目(移到 Map 尾部)模拟一般性「完成序≠插入序」——若实现退化为按 Map
|
|
506
|
+
// 迭代首 excess 个淘汰,删的将不是 t0-t4,本用例失败
|
|
507
|
+
for (let i = 24; i >= 0; i--) {
|
|
508
|
+
runs.set(`wf-t${i}`, makeEvictableRun(`wf-t${i}`, { completedAt: isoAt(i) }));
|
|
509
|
+
}
|
|
510
|
+
for (const i of [5, 15, 20]) {
|
|
511
|
+
const run = runs.get(`wf-t${i}`)!;
|
|
512
|
+
runs.delete(`wf-t${i}`);
|
|
513
|
+
runs.set(`wf-t${i}`, run);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const evicted = evictDoneRunsBeyondCap(runs, MAX_RETAINED_DONE_RUNS);
|
|
517
|
+
|
|
518
|
+
expect(evicted).toBe(5);
|
|
519
|
+
expect(runs.size).toBe(MAX_RETAINED_DONE_RUNS);
|
|
520
|
+
// 被删的恰是 completedAt 最小的 5 个(t0-t4)
|
|
521
|
+
for (let i = 0; i < 5; i++) {
|
|
522
|
+
expect(runs.has(`wf-t${i}`)).toBe(false);
|
|
523
|
+
}
|
|
524
|
+
for (let i = 5; i < 25; i++) {
|
|
525
|
+
expect(runs.has(`wf-t${i}`)).toBe(true);
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
it("W3TC3: 反序 fixture——创建最早、完成最晚的 run 不被淘汰(GAP-1 回归锚点)", () => {
|
|
530
|
+
// 嵌套 workflow 竞态背景:父 run 创建最早(Map 首元素)、完成最晚(completedAt
|
|
531
|
+
// 全局最新)。同 session 累计 21 个 done 时父 run 完成瞬间触发同步裁剪——若按
|
|
532
|
+
// Map 插入序淘汰,父 run 被淘汰,runAndWait 轮询窗口内 get 不到 → 误返
|
|
533
|
+
// "Run not found"。completedAt 排序结构性消除。
|
|
534
|
+
const runs = new Map<string, WorkflowRun>();
|
|
535
|
+
// 插入序 = 创建序:wf-parent 最先插入,completedAt=t100 全局最新(先创建后完成)
|
|
536
|
+
runs.set("wf-parent", makeEvictableRun("wf-parent", { completedAt: isoAt(100) }));
|
|
537
|
+
for (let i = 0; i < 20; i++) {
|
|
538
|
+
// child(i+1) 后插入,completedAt=t0..t19(父 run 之前完成)
|
|
539
|
+
runs.set(`wf-child${i + 1}`, makeEvictableRun(`wf-child${i + 1}`, { completedAt: isoAt(i) }));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const evicted = evictDoneRunsBeyondCap(runs, MAX_RETAINED_DONE_RUNS);
|
|
543
|
+
|
|
544
|
+
expect(evicted).toBe(1);
|
|
545
|
+
// 被淘汰的是 child1(completedAt 最旧 t0),非 Map 首元素 wf-parent
|
|
546
|
+
expect(runs.has("wf-child1")).toBe(false);
|
|
547
|
+
expect(runs.has("wf-parent")).toBe(true);
|
|
548
|
+
for (let i = 2; i <= 20; i++) {
|
|
549
|
+
expect(runs.has(`wf-child${i}`)).toBe(true);
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("W3TC4: completedAt 缺失 fallback 视为最旧(排序键空串字典序最小)", () => {
|
|
554
|
+
const runs = new Map<string, WorkflowRun>();
|
|
555
|
+
// 插入序 [runC, runA, runB]——缺失者 runA 居中,防「碰巧首元素」假通过
|
|
556
|
+
runs.set("wf-runC", makeEvictableRun("wf-runC", { completedAt: isoAt(2) }));
|
|
557
|
+
runs.set("wf-runA", makeEvictableRun("wf-runA"));
|
|
558
|
+
runs.set("wf-runB", makeEvictableRun("wf-runB", { completedAt: isoAt(1) }));
|
|
559
|
+
|
|
560
|
+
const evicted = evictDoneRunsBeyondCap(runs, 2);
|
|
561
|
+
|
|
562
|
+
// runA 缺失 completedAt = 空串排序键 → 字典序最小=最旧 → 先被淘汰
|
|
563
|
+
expect(evicted).toBe(1);
|
|
564
|
+
expect(runs.has("wf-runA")).toBe(false);
|
|
565
|
+
expect(runs.has("wf-runB")).toBe(true);
|
|
566
|
+
expect(runs.has("wf-runC")).toBe(true);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it("W3TC5: tie 稳定排序——同 completedAt 按 Map 插入序确定性淘汰", () => {
|
|
570
|
+
function buildTieFixture(): Map<string, WorkflowRun> {
|
|
571
|
+
const runs = new Map<string, WorkflowRun>();
|
|
572
|
+
const tie = isoAt(50);
|
|
573
|
+
for (const id of ["wf-w1", "wf-w2", "wf-w3", "wf-w4"]) {
|
|
574
|
+
runs.set(id, makeEvictableRun(id, { completedAt: tie }));
|
|
575
|
+
}
|
|
576
|
+
return runs;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const runs = buildTieFixture();
|
|
580
|
+
const evicted = evictDoneRunsBeyondCap(runs, 2);
|
|
581
|
+
|
|
582
|
+
// tie 组内先插入者视为更旧先被淘汰
|
|
583
|
+
expect(evicted).toBe(2);
|
|
584
|
+
expect(runs.has("wf-w1")).toBe(false);
|
|
585
|
+
expect(runs.has("wf-w2")).toBe(false);
|
|
586
|
+
expect(runs.has("wf-w3")).toBe(true);
|
|
587
|
+
expect(runs.has("wf-w4")).toBe(true);
|
|
588
|
+
|
|
589
|
+
// 确定性:同 fixture 二次运行断言同一淘汰集合
|
|
590
|
+
const runs2 = buildTieFixture();
|
|
591
|
+
const evicted2 = evictDoneRunsBeyondCap(runs2, 2);
|
|
592
|
+
expect(evicted2).toBe(2);
|
|
593
|
+
expect(runs2.has("wf-w1")).toBe(false);
|
|
594
|
+
expect(runs2.has("wf-w2")).toBe(false);
|
|
595
|
+
expect(runs2.has("wf-w3")).toBe(true);
|
|
596
|
+
expect(runs2.has("wf-w4")).toBe(true);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it("W3TC6: done 数 <= keepDone 时 no-op 返回 0(恰好 K / 不足 K 两场景)", () => {
|
|
600
|
+
// 场景①:恰好 20 个 done(=K)
|
|
601
|
+
const exact = new Map<string, WorkflowRun>();
|
|
602
|
+
for (let i = 0; i < MAX_RETAINED_DONE_RUNS; i++) {
|
|
603
|
+
exact.set(`wf-ex-${i}`, makeEvictableRun(`wf-ex-${i}`, { completedAt: isoAt(i) }));
|
|
604
|
+
}
|
|
605
|
+
const evictedExact = evictDoneRunsBeyondCap(exact, MAX_RETAINED_DONE_RUNS);
|
|
606
|
+
expect(evictedExact).toBe(0);
|
|
607
|
+
expect(exact.size).toBe(MAX_RETAINED_DONE_RUNS);
|
|
608
|
+
for (let i = 0; i < MAX_RETAINED_DONE_RUNS; i++) {
|
|
609
|
+
expect(exact.has(`wf-ex-${i}`)).toBe(true);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// 场景②:5 个 done(<K)+ 混入 running
|
|
613
|
+
const under = new Map<string, WorkflowRun>();
|
|
614
|
+
for (let i = 0; i < 5; i++) {
|
|
615
|
+
under.set(`wf-un-${i}`, makeEvictableRun(`wf-un-${i}`, { completedAt: isoAt(i) }));
|
|
616
|
+
}
|
|
617
|
+
under.set("wf-un-run", makeEvictableRun("wf-un-run", { status: "running" }));
|
|
618
|
+
const evictedUnder = evictDoneRunsBeyondCap(under, MAX_RETAINED_DONE_RUNS);
|
|
619
|
+
expect(evictedUnder).toBe(0);
|
|
620
|
+
expect(under.size).toBe(6);
|
|
621
|
+
for (const id of ["wf-un-run"]) {
|
|
622
|
+
expect(under.has(id)).toBe(true);
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
});
|