@zhushanwen/pi-subagent-workflow 0.2.0 → 0.3.1
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 +56 -0
- package/agents/{scout.md → explorer.md} +1 -1
- package/agents/orchestrator.md +48 -0
- package/package.json +1 -1
- package/src/execution/__tests__/agent-registry.test.ts +3 -3
- package/src/execution/__tests__/ask-user-transit-e2e.test.ts +484 -0
- package/src/execution/__tests__/channel-registry-handshake.test.ts +233 -0
- package/src/execution/__tests__/crash-recovery.test.ts +5 -1
- package/src/execution/__tests__/dialog-queue.test.ts +299 -0
- package/src/execution/__tests__/execute-nesting.test.ts +1 -1
- package/src/execution/__tests__/execute-options-mapper.test.ts +22 -3
- package/src/execution/__tests__/finalize-record.test.ts +173 -0
- package/src/execution/__tests__/gui-mode-dispatch.test.ts +2 -3
- package/src/execution/__tests__/helpers/spawn-mock.ts +209 -0
- package/src/execution/__tests__/host-mode.test.ts +87 -0
- package/src/execution/__tests__/index-session-start.test.ts +342 -0
- package/src/execution/__tests__/list-component.test.ts +1 -1
- package/src/execution/__tests__/notifier-flush.test.ts +78 -0
- package/src/execution/__tests__/path-encoding.test.ts +30 -1
- package/src/execution/__tests__/record-store.test.ts +86 -2
- package/src/execution/__tests__/records-cwd-isolation.test.ts +91 -0
- package/src/execution/__tests__/rpc-mode.test.ts +89 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +157 -153
- package/src/execution/__tests__/run-spawn-integration.test.ts +85 -151
- package/src/execution/__tests__/run-spawn-rpc-mode.test.ts +193 -0
- package/src/execution/__tests__/session-file-gc.test.ts +46 -0
- package/src/execution/__tests__/session-start-reaper.test.ts +7 -1
- package/src/execution/__tests__/spawn-args.test.ts +14 -19
- package/src/execution/__tests__/spawn-event-adapter-rpc.test.ts +189 -0
- package/src/execution/__tests__/stdin-writer.test.ts +353 -0
- package/src/execution/__tests__/subagent-service.test.ts +73 -3
- package/src/execution/__tests__/tool-action.test.ts +4 -4
- package/src/execution/__tests__/ui-channels.test.ts +187 -0
- package/src/execution/__tests__/ui-interaction-model.test.ts +67 -0
- package/src/execution/__tests__/ui-request-handler-factory.test.ts +166 -0
- package/src/execution/__tests__/ui-request-handler.test.ts +204 -0
- package/src/execution/__tests__/ui-request-observability.test.ts +101 -0
- package/src/execution/__tests__/ui-request-queue.test.ts +133 -0
- package/src/execution/__tests__/worktree-manager.test.ts +1 -1
- package/src/execution/agent-registry.ts +1 -1
- package/src/execution/channel-registry-access.ts +138 -0
- package/src/execution/dialog-queue.ts +329 -0
- package/src/execution/execute-options-mapper.ts +5 -3
- package/src/execution/finalize-record.ts +160 -0
- package/src/execution/get-state-handshake.ts +104 -0
- package/src/execution/host-mode.ts +52 -0
- package/src/execution/manifest-store.ts +206 -0
- package/src/execution/notifier.ts +5 -1
- package/src/execution/path-encoding.ts +18 -0
- package/src/execution/pi-invocation.ts +1 -1
- package/src/execution/record-store.ts +108 -2
- package/src/execution/session-file-gc.ts +25 -3
- package/src/execution/session-runner.ts +216 -32
- package/src/execution/spawn-event-adapter.ts +219 -6
- package/src/execution/stdin-writer.ts +106 -0
- package/src/execution/subagent-service.ts +167 -197
- package/src/execution/types.ts +6 -6
- package/src/execution/ui-channels.ts +216 -0
- package/src/execution/ui-interaction-model.ts +48 -0
- package/src/execution/ui-request-handler-factory.ts +175 -0
- package/src/execution/ui-request-observability.ts +77 -0
- package/src/execution/ui-request-queue.ts +168 -0
- package/src/index.ts +90 -6
- package/src/interface/__tests__/detectors.test.ts +76 -0
- package/src/interface/__tests__/subagent-tool-prompt.test.ts +27 -4
- package/src/interface/__tests__/workflow-tool-prompt.test.ts +24 -2
- package/src/interface/format.ts +2 -0
- package/src/interface/subagent-actions.ts +21 -11
- package/src/interface/subagent-tool.ts +43 -10
- package/src/interface/tool-workflow.ts +58 -16
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// src/execution/__tests__/channel-registry-handshake.test.ts
|
|
2
|
+
//
|
|
3
|
+
// 决策 D(修复 M4):channel handshake 协议升级测试。
|
|
4
|
+
//
|
|
5
|
+
// 测试对象:extensions/subagent-workflow/src/execution/channel-registry-access.ts
|
|
6
|
+
//
|
|
7
|
+
// 协议契约(与 ask-user 侧严格对齐):
|
|
8
|
+
// key 字面量: "@zhushanwen/pi-subagents.channelHandshake"
|
|
9
|
+
// handshake 形状: { version: 1, registry?: UiChannelRegistry, pending: [{channel,handler}] }
|
|
10
|
+
// canonical 唯一创建点:getOrCreateChannelRegistry(仅 subagent-workflow 侧创建)
|
|
11
|
+
//
|
|
12
|
+
// 关键不变量:
|
|
13
|
+
// 1. canonical registry 永远由 subagent-workflow 创建
|
|
14
|
+
// 2. ask-user 先到时只往 slot.pending 推,subagent-workflow 来时 flush
|
|
15
|
+
// 3. 重复调用返回同一实例(===)
|
|
16
|
+
// 4. version !== 1 时 warn + 丢弃重建
|
|
17
|
+
|
|
18
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
CHANNEL_HANDSHAKE_KEY,
|
|
22
|
+
getOrCreateChannelRegistry,
|
|
23
|
+
type ChannelRegistryHandshake,
|
|
24
|
+
} from "../channel-registry-access.ts";
|
|
25
|
+
|
|
26
|
+
// ── 用例隔离:每个用例前清空 globalThis 槽位 ─────────────────
|
|
27
|
+
// handshake 是 globalThis 单例,跨用例污染会破坏测试确定性。
|
|
28
|
+
function clearSlot(): void {
|
|
29
|
+
Reflect.deleteProperty(globalThis, CHANNEL_HANDSHAKE_KEY);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
clearSlot();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** 读取当前 slot(绕过本模块的 readHandshakeSlot,测试断言用)。 */
|
|
37
|
+
function readRawSlot(): ChannelRegistryHandshake | undefined {
|
|
38
|
+
return Reflect.get(globalThis, CHANNEL_HANDSHAKE_KEY) as ChannelRegistryHandshake | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 手动塞一个 slot(模拟 ask-user 先到的场景)。 */
|
|
42
|
+
function injectSlot(slot: ChannelRegistryHandshake): void {
|
|
43
|
+
Reflect.set(globalThis, CHANNEL_HANDSHAKE_KEY, slot);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("getOrCreateChannelRegistry — 空 globalThis 场景", () => {
|
|
47
|
+
it("空 slot → 创建 canonical + slot,返回的 registry 有 register/resolve/list,slot.version===1,pending 为 []", () => {
|
|
48
|
+
clearSlot();
|
|
49
|
+
const registry = getOrCreateChannelRegistry();
|
|
50
|
+
|
|
51
|
+
// registry 形状:canonical 三方法
|
|
52
|
+
expect(typeof registry.register).toBe("function");
|
|
53
|
+
expect(typeof registry.resolve).toBe("function");
|
|
54
|
+
expect(typeof registry.list).toBe("function");
|
|
55
|
+
|
|
56
|
+
// slot 被填充且形状正确
|
|
57
|
+
const slot = readRawSlot();
|
|
58
|
+
expect(slot).toBeDefined();
|
|
59
|
+
expect(slot!.version).toBe(1);
|
|
60
|
+
expect(slot!.registry).toBe(registry); // slot.registry 指向返回的 registry
|
|
61
|
+
expect(slot!.pending).toEqual([]);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("getOrCreateChannelRegistry — ask-user 先到(registry 未就绪 + pending 非空)", () => {
|
|
66
|
+
it("模拟 ask-user 先到塞 pending → getOrCreate 创建 canonical + flush pending(handler 注册成功 + pending 清空)", () => {
|
|
67
|
+
clearSlot();
|
|
68
|
+
// ask-user 先到:塞 version:1 slot,registry 未就绪,pending 有 1 条
|
|
69
|
+
const handler = vi.fn();
|
|
70
|
+
injectSlot({
|
|
71
|
+
version: 1,
|
|
72
|
+
pending: [{ channel: "ask_user", handler }],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const registry = getOrCreateChannelRegistry();
|
|
76
|
+
|
|
77
|
+
// canonical registry 被创建,pending 被 flush
|
|
78
|
+
expect(registry.resolve("ask_user")).toBe(handler);
|
|
79
|
+
// pending 已清空
|
|
80
|
+
const slot = readRawSlot();
|
|
81
|
+
expect(slot!.pending).toEqual([]);
|
|
82
|
+
// slot.registry 现在指向返回的 registry
|
|
83
|
+
expect(slot!.registry).toBe(registry);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("flush 后 registry.resolve 能拿到 handler(集成验证 resolve 真的能取到)", () => {
|
|
87
|
+
clearSlot();
|
|
88
|
+
const handler = vi.fn();
|
|
89
|
+
injectSlot({
|
|
90
|
+
version: 1,
|
|
91
|
+
pending: [{ channel: "ask_user", handler }],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const registry = getOrCreateChannelRegistry();
|
|
95
|
+
// 调一次 resolve 拿到的 handler,确认是原始 handler 引用
|
|
96
|
+
const resolved = registry.resolve("ask_user");
|
|
97
|
+
expect(resolved).toBe(handler);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("getOrCreateChannelRegistry — 二次调用(registry 已就绪)", () => {
|
|
102
|
+
it("二次调用返回同一实例引用(===),不重建,不重复 flush", () => {
|
|
103
|
+
clearSlot();
|
|
104
|
+
const first = getOrCreateChannelRegistry();
|
|
105
|
+
const second = getOrCreateChannelRegistry();
|
|
106
|
+
expect(second).toBe(first); // 引用相等
|
|
107
|
+
|
|
108
|
+
// slot.registry 也仍指向同一实例
|
|
109
|
+
const slot = readRawSlot();
|
|
110
|
+
expect(slot!.registry).toBe(first);
|
|
111
|
+
expect(slot!.registry).toBe(second);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("二次调用不重复 flush(pending 已清空,再次调用 pending 仍为 [])", () => {
|
|
115
|
+
clearSlot();
|
|
116
|
+
const handler = vi.fn();
|
|
117
|
+
injectSlot({
|
|
118
|
+
version: 1,
|
|
119
|
+
pending: [{ channel: "ask_user", handler }],
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
getOrCreateChannelRegistry(); // 第一次:flush + 清空 pending
|
|
123
|
+
getOrCreateChannelRegistry(); // 第二次:应跳过 flush 分支
|
|
124
|
+
|
|
125
|
+
const slot = readRawSlot();
|
|
126
|
+
expect(slot!.pending).toEqual([]);
|
|
127
|
+
// handler 仍只被注册一次(resolve 仍是原 handler)
|
|
128
|
+
const registry = slot!.registry!;
|
|
129
|
+
expect(registry.resolve("ask_user")).toBe(handler);
|
|
130
|
+
expect(registry.list()).toEqual(["ask_user"]);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("getOrCreateChannelRegistry — canonical registry 三方法(形状验证)", () => {
|
|
135
|
+
it("返回的 registry register 后 resolve 拿到 handler,list 列出 channel 名", () => {
|
|
136
|
+
clearSlot();
|
|
137
|
+
const registry = getOrCreateChannelRegistry();
|
|
138
|
+
const handler = vi.fn();
|
|
139
|
+
registry.register("custom_channel", handler);
|
|
140
|
+
|
|
141
|
+
expect(registry.resolve("custom_channel")).toBe(handler);
|
|
142
|
+
expect(registry.list()).toContain("custom_channel");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("未注册的 channel resolve 返回 undefined", () => {
|
|
146
|
+
clearSlot();
|
|
147
|
+
const registry = getOrCreateChannelRegistry();
|
|
148
|
+
expect(registry.resolve("not_registered")).toBeUndefined();
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("getOrCreateChannelRegistry — 多条 pending flush 顺序与覆盖", () => {
|
|
153
|
+
it("多条 pending(不同 channel)flush 后都被注册,list 顺序保留插入顺序", () => {
|
|
154
|
+
clearSlot();
|
|
155
|
+
const h1 = vi.fn();
|
|
156
|
+
const h2 = vi.fn();
|
|
157
|
+
const h3 = vi.fn();
|
|
158
|
+
injectSlot({
|
|
159
|
+
version: 1,
|
|
160
|
+
pending: [
|
|
161
|
+
{ channel: "alpha", handler: h1 },
|
|
162
|
+
{ channel: "beta", handler: h2 },
|
|
163
|
+
{ channel: "gamma", handler: h3 },
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const registry = getOrCreateChannelRegistry();
|
|
168
|
+
expect(registry.resolve("alpha")).toBe(h1);
|
|
169
|
+
expect(registry.resolve("beta")).toBe(h2);
|
|
170
|
+
expect(registry.resolve("gamma")).toBe(h3);
|
|
171
|
+
expect(registry.list()).toEqual(["alpha", "beta", "gamma"]);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("同名 channel 后者覆盖前者(pending 内重复 channel)", () => {
|
|
175
|
+
clearSlot();
|
|
176
|
+
const old = vi.fn();
|
|
177
|
+
const fresh = vi.fn();
|
|
178
|
+
injectSlot({
|
|
179
|
+
version: 1,
|
|
180
|
+
pending: [
|
|
181
|
+
{ channel: "ask_user", handler: old },
|
|
182
|
+
{ channel: "ask_user", handler: fresh }, // 后者覆盖
|
|
183
|
+
],
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const registry = getOrCreateChannelRegistry();
|
|
187
|
+
expect(registry.resolve("ask_user")).toBe(fresh);
|
|
188
|
+
// list 不重复(Map key 唯一)
|
|
189
|
+
expect(registry.list()).toEqual(["ask_user"]);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("getOrCreateChannelRegistry — version 校验(向前兼容)", () => {
|
|
194
|
+
it("version !== 1(如塞 {version:2,...})→ warn + 重建为新 slot,旧 pending 数据丢弃", () => {
|
|
195
|
+
clearSlot();
|
|
196
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
197
|
+
// 塞一个 version:2 的 slot,含旧数据
|
|
198
|
+
const staleHandler = vi.fn();
|
|
199
|
+
injectSlot({
|
|
200
|
+
version: 2 as unknown as 1, // 故意写错 version
|
|
201
|
+
registry: undefined,
|
|
202
|
+
pending: [{ channel: "ask_user", handler: staleHandler }],
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const registry = getOrCreateChannelRegistry();
|
|
206
|
+
|
|
207
|
+
// warn 被调用
|
|
208
|
+
expect(warnSpy).toHaveBeenCalled();
|
|
209
|
+
// 旧 pending 数据被丢弃(staleHandler 没被注册)
|
|
210
|
+
expect(registry.resolve("ask_user")).toBeUndefined();
|
|
211
|
+
// slot 被重建为 version:1,pending 为空
|
|
212
|
+
const slot = readRawSlot();
|
|
213
|
+
expect(slot!.version).toBe(1);
|
|
214
|
+
expect(slot!.pending).toEqual([]);
|
|
215
|
+
expect(slot!.registry).toBe(registry);
|
|
216
|
+
|
|
217
|
+
warnSpy.mockRestore();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("version !== 1 时 warn 消息包含 got/expected 字样", () => {
|
|
221
|
+
clearSlot();
|
|
222
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
223
|
+
injectSlot({ version: 99 as unknown as 1, pending: [] });
|
|
224
|
+
|
|
225
|
+
getOrCreateChannelRegistry();
|
|
226
|
+
|
|
227
|
+
const msg = warnSpy.mock.calls[0]?.[0] ?? "";
|
|
228
|
+
expect(String(msg)).toMatch(/got/);
|
|
229
|
+
expect(String(msg)).toMatch(/expected/);
|
|
230
|
+
|
|
231
|
+
warnSpy.mockRestore();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -89,6 +89,8 @@ vi.mock("../model-config-service.ts", () => ({
|
|
|
89
89
|
vi.mock("../subagent-service.ts", () => ({
|
|
90
90
|
SubagentService: class {
|
|
91
91
|
initSession = vi.fn();
|
|
92
|
+
// W3: index.ts session_start 注入 UI handler 时调用
|
|
93
|
+
setUiRequestHandler = vi.fn();
|
|
92
94
|
},
|
|
93
95
|
getSubagentService: () => null,
|
|
94
96
|
setSubagentService: vi.fn(),
|
|
@@ -119,10 +121,10 @@ vi.mock("../../interface/commands.ts", () => ({
|
|
|
119
121
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
120
122
|
|
|
121
123
|
import subagentsExtension from "../../index.ts";
|
|
122
|
-
import { WorkflowRun } from "../../orchestration/models/workflow-run.ts";
|
|
123
124
|
import { Budget } from "../../orchestration/models/budget.ts";
|
|
124
125
|
import { Trace } from "../../orchestration/models/trace.ts";
|
|
125
126
|
import type { WorkflowRun as WorkflowRunType } from "../../orchestration/models/workflow-run.ts";
|
|
127
|
+
import { WorkflowRun } from "../../orchestration/models/workflow-run.ts";
|
|
126
128
|
|
|
127
129
|
// ── helpers ──
|
|
128
130
|
|
|
@@ -195,6 +197,8 @@ function createMockPi(overrides: Record<string, unknown> = {}): {
|
|
|
195
197
|
function createMockCtx(): Record<string, unknown> {
|
|
196
198
|
return {
|
|
197
199
|
cwd: "/home/user/project",
|
|
200
|
+
// [Wave1 #21] mode 必填(与 SDK ExtensionContext 契约一致);默认 tui。
|
|
201
|
+
mode: "tui",
|
|
198
202
|
modelRegistry: {
|
|
199
203
|
getAvailable: () => [],
|
|
200
204
|
find: () => undefined,
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// src/__tests__/dialog-queue.test.ts
|
|
2
|
+
//
|
|
3
|
+
// W2 红灯测试:dialog-queue.ts — L2 跨子进程全局 dialog 串行队列。
|
|
4
|
+
//
|
|
5
|
+
// 测试对象:extensions/subagent-workflow/src/execution/dialog-queue.ts(新建)
|
|
6
|
+
// 契约来源:.fix-plans/00-master-summary.md §一 冲突 3(L2 DialogGlobalQueue 设计)
|
|
7
|
+
//
|
|
8
|
+
// DialogGlobalQueue 设计要点:
|
|
9
|
+
// - 进程单例,跨所有子进程共享,串行所有 dialog 类请求(isDialogMethod===true)
|
|
10
|
+
// - enqueue(req, handler): Promise<UiResponse> — 入队,返回 Promise
|
|
11
|
+
// - FIFO 串行:前一个 handler resolve 后才处理下一个
|
|
12
|
+
// - SR-4:入队项带 child 引用,child close 时把该 child 的 pending dialog 全部
|
|
13
|
+
// reject 为 cancelled(防 Promise 永挂 + 内存泄漏)
|
|
14
|
+
// - handler 抛错兜底:catch → appendEntry "subagent:dialog-handler-failed"
|
|
15
|
+
// → 回 {cancelled:true} → 继续处理下一个(不能让一个失败卡死队列)
|
|
16
|
+
// - 调用方约定只对 dialog 类调 enqueue;fire-and-forget 由调用方(factory 层)
|
|
17
|
+
// 直接调 handler 不入队。enqueue 内仍防御性兼容 fire-and-forget(直接调 handler)
|
|
18
|
+
//
|
|
19
|
+
// 4 个 TC-E4 测试 case:
|
|
20
|
+
// 1. 3 个并发 dialog 请求按 FIFO 顺序串行处理
|
|
21
|
+
// 2. SR-4: child close 时 pending dialog reject 为 cancelled
|
|
22
|
+
// 3. handler 抛错后队列继续处理下一个
|
|
23
|
+
// 4. fire-and-forget method 不入队(直接调 handler)
|
|
24
|
+
//
|
|
25
|
+
// 红灯原因:dialog-queue.ts 尚未创建,import 失败。
|
|
26
|
+
|
|
27
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
28
|
+
|
|
29
|
+
import { DialogGlobalQueue, type UiRequest, type UiResponse } from "../dialog-queue.ts";
|
|
30
|
+
|
|
31
|
+
// ── 类型助手 ────────────────────────────────────────────────
|
|
32
|
+
// UiRequest 最小形状(method + id)。dialog 类:select/confirm/input/editor。
|
|
33
|
+
// fire-and-forget 类:notify/setStatus/...。
|
|
34
|
+
|
|
35
|
+
function dialogReq(id: string, method: "select" | "confirm" | "input" | "editor" = "select"): UiRequest {
|
|
36
|
+
return { method, id, title: `q-${id}` };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function fireAndForgetReq(id: string): UiRequest {
|
|
40
|
+
return { method: "notify", id, message: `n-${id}` };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ── 测试 fixture ────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.useFakeTimers();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
vi.useRealTimers();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("DialogGlobalQueue — FIFO 串行(TC-E4 case 1)", () => {
|
|
54
|
+
it("3 个并发 dialog 请求按 FIFO 顺序串行处理(前一个 resolve 后才处理下一个)", async () => {
|
|
55
|
+
const queue = new DialogGlobalQueue();
|
|
56
|
+
const callOrder: string[] = [];
|
|
57
|
+
// 每个请求返回独立的可控 Promise,模拟 handler 延迟
|
|
58
|
+
const resolvers: Array<(v: UiResponse) => void> = [];
|
|
59
|
+
|
|
60
|
+
const handler = vi.fn((req: UiRequest): Promise<UiResponse> => {
|
|
61
|
+
callOrder.push(req.id);
|
|
62
|
+
return new Promise<UiResponse>((resolve) => {
|
|
63
|
+
resolvers.push(resolve);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// 并发入队 3 个 dialog 请求
|
|
68
|
+
const p1 = queue.enqueue(dialogReq("r1"), handler);
|
|
69
|
+
const p2 = queue.enqueue(dialogReq("r2"), handler);
|
|
70
|
+
const p3 = queue.enqueue(dialogReq("r3"), handler);
|
|
71
|
+
|
|
72
|
+
// 只有第一个立即被处理
|
|
73
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
74
|
+
expect(callOrder).toEqual(["r1"]);
|
|
75
|
+
|
|
76
|
+
// resolve 第一个 → 第二个开始
|
|
77
|
+
resolvers[0]({ value: "a1" });
|
|
78
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
79
|
+
expect(handler).toHaveBeenCalledTimes(2);
|
|
80
|
+
expect(callOrder).toEqual(["r1", "r2"]);
|
|
81
|
+
|
|
82
|
+
// resolve 第二个 → 第三个开始
|
|
83
|
+
resolvers[1]({ value: "a2" });
|
|
84
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
85
|
+
expect(handler).toHaveBeenCalledTimes(3);
|
|
86
|
+
expect(callOrder).toEqual(["r1", "r2", "r3"]);
|
|
87
|
+
|
|
88
|
+
// resolve 第三个 → 全部 settle
|
|
89
|
+
resolvers[2]({ value: "a3" });
|
|
90
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
91
|
+
|
|
92
|
+
const [r1, r2, r3] = await Promise.all([p1, p2, p3]);
|
|
93
|
+
expect(r1).toEqual({ value: "a1" });
|
|
94
|
+
expect(r2).toEqual({ value: "a2" });
|
|
95
|
+
expect(r3).toEqual({ value: "a3" });
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("DialogGlobalQueue — SR-4 child close reject(TC-E4 case 2)", () => {
|
|
100
|
+
it("child close 时该 child 的 pending dialog reject 为 cancelled(防 Promise 永挂)", async () => {
|
|
101
|
+
const queue = new DialogGlobalQueue();
|
|
102
|
+
const child = { pid: 20001 };
|
|
103
|
+
|
|
104
|
+
const handler = vi.fn(
|
|
105
|
+
(): Promise<UiResponse> => new Promise<UiResponse>(() => {}),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// 入队一个永远不 resolve 的 dialog,绑定 child
|
|
109
|
+
const pending = queue.enqueue(dialogReq("r1"), handler, { child });
|
|
110
|
+
|
|
111
|
+
// 模拟 child close —— pending 必须被 reject 为 cancelled
|
|
112
|
+
queue.rejectChildDialogs(child);
|
|
113
|
+
|
|
114
|
+
await expect(pending).resolves.toEqual({ cancelled: true });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("child close 不影响其他 child 的 pending dialog", async () => {
|
|
118
|
+
const queue = new DialogGlobalQueue();
|
|
119
|
+
const childA = { pid: 30001 };
|
|
120
|
+
const childB = { pid: 30002 };
|
|
121
|
+
|
|
122
|
+
const handler = vi.fn(
|
|
123
|
+
(): Promise<UiResponse> => new Promise<UiResponse>(() => {}),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const pendingA = queue.enqueue(dialogReq("a1"), handler, { child: childA });
|
|
127
|
+
const pendingB = queue.enqueue(dialogReq("b1"), handler, { child: childB });
|
|
128
|
+
|
|
129
|
+
// close childA —— 只有 pendingA 被 reject,pendingB 仍 pending
|
|
130
|
+
queue.rejectChildDialogs(childA);
|
|
131
|
+
|
|
132
|
+
await expect(pendingA).resolves.toEqual({ cancelled: true });
|
|
133
|
+
// pendingB 不被 reject(仍 pending)—— 用 race + real timer 验证它没 settle。
|
|
134
|
+
// fake timer 下 setTimeout 不自动跑,临时切 real timer 做 10ms 超时探测。
|
|
135
|
+
vi.useRealTimers();
|
|
136
|
+
const settled = await Promise.race([
|
|
137
|
+
pendingB.then(() => true),
|
|
138
|
+
new Promise<boolean>((resolve) => setTimeout(() => resolve(false), 10)),
|
|
139
|
+
]);
|
|
140
|
+
expect(settled).toBe(false);
|
|
141
|
+
vi.useFakeTimers();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("DialogGlobalQueue — handler 抛错兜底(TC-E4 case 3)", () => {
|
|
146
|
+
it("handler 第一次抛错 → 回 cancelled → 队列继续处理下一个(不卡死)", async () => {
|
|
147
|
+
const queue = new DialogGlobalQueue();
|
|
148
|
+
const callOrder: string[] = [];
|
|
149
|
+
|
|
150
|
+
const handler = vi.fn((req: UiRequest): Promise<UiResponse> => {
|
|
151
|
+
callOrder.push(req.id);
|
|
152
|
+
if (req.id === "r1") {
|
|
153
|
+
return Promise.reject(new Error("handler boom"));
|
|
154
|
+
}
|
|
155
|
+
return Promise.resolve<UiResponse>({ value: "ok-" + req.id });
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const p1 = queue.enqueue(dialogReq("r1"), handler);
|
|
159
|
+
const p2 = queue.enqueue(dialogReq("r2"), handler);
|
|
160
|
+
|
|
161
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
162
|
+
|
|
163
|
+
// r1 抛错兜底回 cancelled(不向上抛)
|
|
164
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
165
|
+
// r2 正常处理
|
|
166
|
+
await expect(p2).resolves.toEqual({ value: "ok-r2" });
|
|
167
|
+
|
|
168
|
+
expect(callOrder).toEqual(["r1", "r2"]);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("DialogGlobalQueue — fire-and-forget 防御性兼容(TC-E4 case 4)", () => {
|
|
173
|
+
it("fire-and-forget method(notify)防御性直接调 handler,不串行等待(调用方约定不传此类)", async () => {
|
|
174
|
+
const queue = new DialogGlobalQueue();
|
|
175
|
+
const callOrder: string[] = [];
|
|
176
|
+
|
|
177
|
+
// 第一个 dialog 请求永不 resolve(占住队列)
|
|
178
|
+
const blockingHandler = vi.fn(
|
|
179
|
+
(): Promise<UiResponse> => new Promise<UiResponse>(() => {}),
|
|
180
|
+
);
|
|
181
|
+
const ffHandler = vi.fn((req: UiRequest): Promise<UiResponse> => {
|
|
182
|
+
callOrder.push(req.id);
|
|
183
|
+
return Promise.resolve<UiResponse>({ ack: true });
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// 入队一个 blocking dialog(占住队列)
|
|
187
|
+
queue.enqueue(dialogReq("d1"), blockingHandler);
|
|
188
|
+
// fire-and-forget 必须立即被调用,不等 d1 resolve
|
|
189
|
+
const ffResult = await queue.enqueue(fireAndForgetReq("f1"), ffHandler);
|
|
190
|
+
|
|
191
|
+
expect(ffHandler).toHaveBeenCalledTimes(1);
|
|
192
|
+
expect(callOrder).toEqual(["f1"]);
|
|
193
|
+
expect(ffResult).toEqual({ ack: true });
|
|
194
|
+
// blocking dialog 的 handler 仍只调用一次,ff 没有插入队列等待
|
|
195
|
+
expect(blockingHandler).toHaveBeenCalledTimes(1);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ── A4+C12:rejectAll + #19 单一推进点 + child close 集成语义 ──
|
|
200
|
+
|
|
201
|
+
describe("DialogGlobalQueue — rejectAll settle 全部 pending + 清状态(#10/session_shutdown)", () => {
|
|
202
|
+
it("rejectAll 把 current + 队列所有 pending settle 为 cancelled,并清空 queue/current/processing", async () => {
|
|
203
|
+
const queue = new DialogGlobalQueue();
|
|
204
|
+
const child = { pid: 50001 };
|
|
205
|
+
const blocking = vi.fn((): Promise<UiResponse> => new Promise<UiResponse>(() => {}));
|
|
206
|
+
|
|
207
|
+
// r1 占 current(永不 settle);r2/r3 在队列等(含跨 child 的 r3)
|
|
208
|
+
const p1 = queue.enqueue(dialogReq("r1"), blocking, { child });
|
|
209
|
+
const p2 = queue.enqueue(dialogReq("r2"), blocking, { child });
|
|
210
|
+
const p3 = queue.enqueue(dialogReq("r3"), blocking);
|
|
211
|
+
expect(queue.size).toBe(2);
|
|
212
|
+
|
|
213
|
+
queue.rejectAll();
|
|
214
|
+
|
|
215
|
+
// 全部 settle 为 cancelled(Promise 不永挂)
|
|
216
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
217
|
+
await expect(p2).resolves.toEqual({ cancelled: true });
|
|
218
|
+
await expect(p3).resolves.toEqual({ cancelled: true });
|
|
219
|
+
// 状态清空
|
|
220
|
+
expect(queue.size).toBe(0);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("rejectAll 幂等:重复调用不抛错,状态保持清空,已 settle 的 Promise 不重复 resolve", async () => {
|
|
224
|
+
const queue = new DialogGlobalQueue();
|
|
225
|
+
const blocking = vi.fn((): Promise<UiResponse> => new Promise<UiResponse>(() => {}));
|
|
226
|
+
const p1 = queue.enqueue(dialogReq("r1"), blocking);
|
|
227
|
+
|
|
228
|
+
queue.rejectAll();
|
|
229
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
230
|
+
|
|
231
|
+
// 再次调用——幂等(settled 标志保证只 settle 一次),不抛错
|
|
232
|
+
expect(() => queue.rejectAll()).not.toThrow();
|
|
233
|
+
expect(queue.size).toBe(0);
|
|
234
|
+
// p1 仍是 cancelled(防重复 resolve)
|
|
235
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("rejectAll 后队列仍可正常 enqueue 新请求(状态完全重置,processing 不残留)", async () => {
|
|
239
|
+
const queue = new DialogGlobalQueue();
|
|
240
|
+
const blocking = vi.fn((): Promise<UiResponse> => new Promise<UiResponse>(() => {}));
|
|
241
|
+
queue.enqueue(dialogReq("r1"), blocking);
|
|
242
|
+
queue.rejectAll();
|
|
243
|
+
|
|
244
|
+
// rejectAll 后 enqueue 新 dialog,能正常处理(processing 已重置为 false)
|
|
245
|
+
const okHandler = vi.fn(async (): Promise<UiResponse> => ({ value: "new" }));
|
|
246
|
+
const p = queue.enqueue(dialogReq("after"), okHandler);
|
|
247
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
248
|
+
await expect(p).resolves.toEqual({ value: "new" });
|
|
249
|
+
expect(okHandler).toHaveBeenCalledTimes(1);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe("DialogGlobalQueue — #19 单一推进点 + child close 集成语义", () => {
|
|
254
|
+
it("#19 单一推进点:rejectChildDialogs 取消占住 current 的永不 settle dialog 后,队列下一个 item 被推进处理(不卡死)", async () => {
|
|
255
|
+
const queue = new DialogGlobalQueue();
|
|
256
|
+
const childA = { pid: 60001 };
|
|
257
|
+
const blockingHandler = vi.fn((): Promise<UiResponse> => new Promise<UiResponse>(() => {}));
|
|
258
|
+
const r2Handler = vi.fn(async (): Promise<UiResponse> => ({ value: "r2-ok" }));
|
|
259
|
+
|
|
260
|
+
// r1 占 current(永不 settle,模拟等用户输入卡死);r2(另一 child)排队等待
|
|
261
|
+
const p1 = queue.enqueue(dialogReq("r1"), blockingHandler, { child: childA });
|
|
262
|
+
const p2 = queue.enqueue(dialogReq("r2"), r2Handler);
|
|
263
|
+
expect(blockingHandler).toHaveBeenCalledTimes(1);
|
|
264
|
+
expect(r2Handler).toHaveBeenCalledTimes(0); // r2 还在等 r1 settle
|
|
265
|
+
|
|
266
|
+
// childA close → rejectChildDialogs settle r1;#19:settleItem 内唯一 processNext 推进 r2
|
|
267
|
+
queue.rejectChildDialogs(childA);
|
|
268
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
269
|
+
|
|
270
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
271
|
+
// 关键:r2 被推进(队列没卡死在永不 settle 的 r1)
|
|
272
|
+
expect(r2Handler).toHaveBeenCalledTimes(1);
|
|
273
|
+
await expect(p2).resolves.toEqual({ value: "r2-ok" });
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("child close 集成语义:queue 中同一 child 的多个 pending 被 rejectChildDialogs 一次全部 settle cancelled", async () => {
|
|
277
|
+
// 模拟 session-runner child 'close' 事件 → dialogQueue.rejectChildDialogs(child) 清理路径。
|
|
278
|
+
// 绑定点在 session-runner(非 dialog-queue 内部),本测试验证 rejectChildDialogs 公共方法语义。
|
|
279
|
+
// 用 blocker 占 current(另一 child)使被测 child 的项全排队,聚焦验证 queue 批量 cancel。
|
|
280
|
+
const queue = new DialogGlobalQueue();
|
|
281
|
+
const blocker = { pid: 70000 };
|
|
282
|
+
const child = { pid: 70001 };
|
|
283
|
+
const blocking = vi.fn((): Promise<UiResponse> => new Promise<UiResponse>(() => {}));
|
|
284
|
+
|
|
285
|
+
// blocker 占 current(永不 settle),让 child 的项都进队列
|
|
286
|
+
queue.enqueue(dialogReq("blk"), blocking, { child: blocker });
|
|
287
|
+
const p1 = queue.enqueue(dialogReq("c1"), blocking, { child });
|
|
288
|
+
const p2 = queue.enqueue(dialogReq("c2"), blocking, { child });
|
|
289
|
+
const p3 = queue.enqueue(dialogReq("c3"), blocking, { child });
|
|
290
|
+
expect(queue.size).toBe(3);
|
|
291
|
+
|
|
292
|
+
queue.rejectChildDialogs(child);
|
|
293
|
+
|
|
294
|
+
await expect(p1).resolves.toEqual({ cancelled: true });
|
|
295
|
+
await expect(p2).resolves.toEqual({ cancelled: true });
|
|
296
|
+
await expect(p3).resolves.toEqual({ cancelled: true });
|
|
297
|
+
expect(queue.size).toBe(0);
|
|
298
|
+
});
|
|
299
|
+
});
|
|
@@ -117,9 +117,9 @@ vi.mock("../temp-prompt.ts", () => ({
|
|
|
117
117
|
|
|
118
118
|
import { spawn } from "node:child_process";
|
|
119
119
|
|
|
120
|
+
import { ModelConfigService } from "../model-config-service.ts";
|
|
120
121
|
import type { ModelInfo, ModelRegistryLike } from "../model-resolver.ts";
|
|
121
122
|
import { MAX_FORK_DEPTH } from "../session-context-resolver.ts";
|
|
122
|
-
import { ModelConfigService } from "../model-config-service.ts";
|
|
123
123
|
import { SubagentService } from "../subagent-service.ts";
|
|
124
124
|
|
|
125
125
|
const mockSpawn = vi.mocked(spawn);
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
import { describe, expect, it, vi } from "vitest";
|
|
11
11
|
|
|
12
12
|
import type { AgentCallOpts } from "../../orchestration/models/types.ts";
|
|
13
|
-
import type { ModelInfo } from "../model-resolver.ts";
|
|
14
13
|
import { mapToExecuteOptions, mergeTimeoutSignal } from "../execute-options-mapper.ts";
|
|
14
|
+
import type { ModelInfo } from "../model-resolver.ts";
|
|
15
15
|
|
|
16
16
|
describe("mapToExecuteOptions (D-A2)", () => {
|
|
17
17
|
const baseOpts: AgentCallOpts = {
|
|
@@ -22,6 +22,25 @@ describe("mapToExecuteOptions (D-A2)", () => {
|
|
|
22
22
|
skillPath: "/path/to/skill.md",
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
it("T3.10 slug 截断: description > SLUG_MAX_LENGTH(35) → 截断到 35", () => {
|
|
26
|
+
const opts: AgentCallOpts = { ...baseOpts, description: "a".repeat(36) };
|
|
27
|
+
const result = mapToExecuteOptions(opts);
|
|
28
|
+
expect(result.slug).toHaveLength(35);
|
|
29
|
+
expect(result.slug).toBe("a".repeat(35));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("T3.10 slug 边界: description = 35 → 不截断", () => {
|
|
33
|
+
const opts: AgentCallOpts = { ...baseOpts, description: "a".repeat(35) };
|
|
34
|
+
const result = mapToExecuteOptions(opts);
|
|
35
|
+
expect(result.slug).toHaveLength(35);
|
|
36
|
+
expect(result.slug).toBe("a".repeat(35));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("T3.10 slug 回落: 无 description → agentName", () => {
|
|
40
|
+
const result = mapToExecuteOptions(baseOpts);
|
|
41
|
+
expect(result.slug).toBe("worker");
|
|
42
|
+
});
|
|
43
|
+
|
|
25
44
|
it("T3.4 基本映射: prompt→task, agent→agent, cwd→cwd", () => {
|
|
26
45
|
const result = mapToExecuteOptions(baseOpts);
|
|
27
46
|
expect(result.task).toBe("test task");
|
|
@@ -37,12 +56,12 @@ describe("mapToExecuteOptions (D-A2)", () => {
|
|
|
37
56
|
it("T3.9 schemaEnv 透传 (D-A6 bridge)", () => {
|
|
38
57
|
const opts: AgentCallOpts = { ...baseOpts, schemaEnv: '{"type":"object"}' };
|
|
39
58
|
const result = mapToExecuteOptions(opts);
|
|
40
|
-
expect(
|
|
59
|
+
expect(result.schemaEnv).toBe('{"type":"object"}');
|
|
41
60
|
});
|
|
42
61
|
|
|
43
62
|
it("T3.9 schemaEnv 不传 → schemaEnv undefined", () => {
|
|
44
63
|
const result = mapToExecuteOptions(baseOpts);
|
|
45
|
-
expect(
|
|
64
|
+
expect(result.schemaEnv).toBeUndefined();
|
|
46
65
|
});
|
|
47
66
|
|
|
48
67
|
it("T3.5 model: opts.model 优先(显式 override 透传,不与 ctxModel 混合)", () => {
|