pi-ate-workflow 0.2.3 → 0.2.5
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/index.ts +2 -1
- package/package.json +1 -1
- package/proc-guard.ts +171 -0
- package/subagent-jsonl-signal.ts +43 -0
- package/subagent-spawn-visible.ts +192 -181
- package/subagent-utils.ts +9 -1
- package/subagent.ts +140 -132
- package/tests/create-visible-taskdir.test.ts +30 -2
- package/tests/proc-guard.test.ts +192 -0
- package/tests/spawn-once-extra-env.test.ts +6 -1
- package/tests/subagent-failure-classification.test.ts +158 -0
- package/tests/subagent-failure-retry.test.ts +148 -0
- package/tests/subagent-model.test.ts +11 -9
- package/tests/subagent-spawn-visible.test.ts +47 -2
- package/tests/subagent-visible-jsonl-signal.test.ts +113 -0
- package/tests/sweep-stale.test.ts +116 -0
- package/tests/visible-cleanup.test.ts +152 -0
- package/utils.ts +133 -74
- package/visible-cleanup.ts +58 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试 F1 失败档修复(opsx-subarch-meta-baseline,spec: subagent-spawn-runtime)
|
|
3
|
+
*
|
|
4
|
+
* 本文件覆盖纯函数层:
|
|
5
|
+
* 1. 占位文本字面量锁死(防文本漂移静默破坏分类)
|
|
6
|
+
* 2. isSystemicSubagentFailure 三级信号优先级
|
|
7
|
+
* 3. isSubagentSuccess 端到端锁死(系统性失败结果 === false)
|
|
8
|
+
* 4. buildSystemicFailureResult 组装契约(error 必填/exitCode 原值/stderr 尾部透出)
|
|
9
|
+
* 5. barrel 断言(新导出同步 index.ts)
|
|
10
|
+
*
|
|
11
|
+
* runSubagent spec 五 scenario 在 subagent-failure-retry.test.ts;
|
|
12
|
+
* visible 模式 jsonl 三形态用例在 subagent-spawn-visible.test.ts。
|
|
13
|
+
*/
|
|
14
|
+
import { describe, it, expect } from "vitest";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
EMPTY_OUTPUT_PLACEHOLDERS,
|
|
18
|
+
STDERR_TAIL_LIMIT,
|
|
19
|
+
isEmptyPlaceholderOutput,
|
|
20
|
+
isSystemicSubagentFailure,
|
|
21
|
+
buildSystemicFailureResult,
|
|
22
|
+
isSubagentSuccess,
|
|
23
|
+
} from "../utils";
|
|
24
|
+
|
|
25
|
+
describe("F1 字面量锁死", () => {
|
|
26
|
+
it("EMPTY_OUTPUT_PLACEHOLDERS 逐字锁定(含 spawn 分支兜底文本)", () => {
|
|
27
|
+
expect(EMPTY_OUTPUT_PLACEHOLDERS).toEqual([
|
|
28
|
+
"(no output)",
|
|
29
|
+
"(stalled, no output for 5 min)",
|
|
30
|
+
"(timed out)",
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("isEmptyPlaceholderOutput:空串/空白/占位前缀或包含均命中,实质输出不命中", () => {
|
|
35
|
+
expect(isEmptyPlaceholderOutput("")).toBe(true);
|
|
36
|
+
expect(isEmptyPlaceholderOutput(" \n ")).toBe(true);
|
|
37
|
+
expect(isEmptyPlaceholderOutput("(no output)")).toBe(true);
|
|
38
|
+
// formatOutput 包装后的前缀形态(如折行后仍在场)
|
|
39
|
+
expect(isEmptyPlaceholderOutput("(no output)\n")).toBe(true);
|
|
40
|
+
expect(isEmptyPlaceholderOutput("prefix junk (timed out) suffix")).toBe(true);
|
|
41
|
+
expect(isEmptyPlaceholderOutput("(stalled, no output for 5 min)")).toBe(true);
|
|
42
|
+
expect(isEmptyPlaceholderOutput("这是实质输出,虽然很短")).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("isSystemicSubagentFailure:三级信号优先级", () => {
|
|
47
|
+
it("① error 字段存在 → 系统性失败(含 visible jsonl 信号源形态)", () => {
|
|
48
|
+
expect(isSystemicSubagentFailure({ output: "anything", error: "spawn ENOENT" })).toBe(true);
|
|
49
|
+
expect(
|
|
50
|
+
isSystemicSubagentFailure({
|
|
51
|
+
output: "(no output)",
|
|
52
|
+
error: "visible 模式信号源=jsonl 终结形态:session.jsonl 缺失(子进程未建立会话)",
|
|
53
|
+
}),
|
|
54
|
+
).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("① 空串 error 视为无 error(不命中)", () => {
|
|
58
|
+
expect(isSystemicSubagentFailure({ output: "ok", error: "" })).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("② timedOut 布尔 → 系统性失败", () => {
|
|
62
|
+
expect(isSystemicSubagentFailure({ output: "ok", timedOut: true })).toBe(true);
|
|
63
|
+
expect(isSystemicSubagentFailure({ output: "ok", timedOut: false })).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("③ 空占位输出兼 stderr 非空 → 系统性失败(R4 实证形态:exit 0 静默退出)", () => {
|
|
67
|
+
expect(
|
|
68
|
+
isSystemicSubagentFailure({ output: "(no output)", stderr: "Error: Cannot find module './judge'" }),
|
|
69
|
+
).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("③ 空占位但 stderr 空 → 不命中(正常完成零输出档,回落现状)", () => {
|
|
73
|
+
expect(isSystemicSubagentFailure({ output: "(no output)", stderr: "" })).toBe(false);
|
|
74
|
+
expect(isSystemicSubagentFailure({ output: "(no output)" })).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("③ 有实质输出时 stderr 噪声不参与判定(不误伤)", () => {
|
|
78
|
+
expect(isSystemicSubagentFailure({ output: "x".repeat(200), stderr: "some non-fatal warning" })).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("isSubagentSuccess 端到端锁死", () => {
|
|
83
|
+
it("系统性失败结果(buildSystemicFailureResult 组装后)=== false", () => {
|
|
84
|
+
const result = buildSystemicFailureResult({
|
|
85
|
+
exitCode: 0,
|
|
86
|
+
output: "(no output)",
|
|
87
|
+
stderr: "Error: Cannot find module './judge'",
|
|
88
|
+
});
|
|
89
|
+
expect(isSubagentSuccess(result)).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("原始系统性失败形态(exit 0 + 空占位 + stderr 非空)=== false —— 修复前此处为 true", () => {
|
|
93
|
+
expect(
|
|
94
|
+
isSubagentSuccess({ exitCode: 0, output: "(no output)", stderr: "Error: Cannot find module './judge'" }),
|
|
95
|
+
).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("有实质输出 + stderr 噪声 → 成功(现状不变)", () => {
|
|
99
|
+
expect(isSubagentSuccess({ exitCode: 0, output: "x".repeat(200), stderr: "warn noise" })).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("正常完成零输出 + stderr 空 → 维持既有判定(现状语义:exit 0 即成功)", () => {
|
|
103
|
+
expect(isSubagentSuccess({ exitCode: 0, output: "(no output)", stderr: "" })).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("buildSystemicFailureResult 组装契约", () => {
|
|
108
|
+
it("error 必填 + output 含报错与 stderr 尾部 + exitCode 保持原值 0", () => {
|
|
109
|
+
const result = buildSystemicFailureResult({
|
|
110
|
+
exitCode: 0,
|
|
111
|
+
output: "(no output)",
|
|
112
|
+
stderr: "line1: extension load failed\nline2: Cannot find module './judge'",
|
|
113
|
+
});
|
|
114
|
+
expect(result.exitCode).toBe(0);
|
|
115
|
+
expect(result.error).toBeTruthy();
|
|
116
|
+
expect(result.output).toContain("[子代理系统性失败]");
|
|
117
|
+
expect(result.output).toContain("stderr 尾部");
|
|
118
|
+
expect(result.output).toContain("Cannot find module './judge'");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("stderr 超长截尾(保留尾部)", () => {
|
|
122
|
+
const stderr = `${"a".repeat(STDERR_TAIL_LIMIT + 500)}TAIL_MARKER`;
|
|
123
|
+
const result = buildSystemicFailureResult({ exitCode: 1, output: "(no output)", stderr });
|
|
124
|
+
expect(result.output).toContain("TAIL_MARKER");
|
|
125
|
+
expect(result.stderr.length).toBe(stderr.length); // stderr 原文不动(不剥离)
|
|
126
|
+
expect(result.output.length).toBeLessThan(stderr.length); // output 中只透出尾部
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("visible jsonl 信号源的 error 注入后透传进报错行", () => {
|
|
130
|
+
const result = buildSystemicFailureResult({
|
|
131
|
+
exitCode: 0,
|
|
132
|
+
output: "(no output)",
|
|
133
|
+
stderr: "",
|
|
134
|
+
error: "visible 模式信号源=jsonl 终结形态:session.jsonl 无 message 事件(未进入对话阶段)",
|
|
135
|
+
});
|
|
136
|
+
expect(result.output).toContain("visible 模式信号源=jsonl 终结形态");
|
|
137
|
+
expect(result.error).toContain("visible 模式信号源=jsonl 终结形态");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("timedOut 形态:error 标注 timed out", () => {
|
|
141
|
+
const result = buildSystemicFailureResult({ exitCode: 1, output: "(timed out)", stderr: "", timedOut: true });
|
|
142
|
+
expect(result.output).toContain("[子代理系统性失败] timed out");
|
|
143
|
+
expect(result.error).toBeTruthy();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ─── barrel 断言(新导出同步 index.ts)───────────────────────────────────
|
|
148
|
+
|
|
149
|
+
describe("barrel 导出断言", () => {
|
|
150
|
+
it("index.ts 显式导出 F1 失败分类 API", async () => {
|
|
151
|
+
const api = await import("../index");
|
|
152
|
+
expect(typeof api.isSystemicSubagentFailure).toBe("function");
|
|
153
|
+
expect(typeof api.buildSystemicFailureResult).toBe("function");
|
|
154
|
+
expect(typeof api.isEmptyPlaceholderOutput).toBe("function");
|
|
155
|
+
expect(Array.isArray(api.EMPTY_OUTPUT_PLACEHOLDERS)).toBe(true);
|
|
156
|
+
expect(typeof api.checkSessionJsonlTermination).toBe("function");
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试 F1 失败档修复的 runSubagent 层(opsx-subarch-meta-baseline,spec: subagent-spawn-runtime)
|
|
3
|
+
*
|
|
4
|
+
* spec 五 scenario(mock spawnOnce/spawnVisible):
|
|
5
|
+
* 1. 系统性失败不重试且透出 stderr(exit 0 + 占位 + stderr 非空)
|
|
6
|
+
* 2. 个体违约保留重试(违约 + stderr 空)
|
|
7
|
+
* 3. 正常完成零输出不误伤(3a 无 constraints 原样返回 / 3b 有 constraints 走违约重试档)
|
|
8
|
+
* 4. 有实质输出时 stderr 不影响成功判定
|
|
9
|
+
* 5.(库侧)visible jsonl 信号源 error 注入后走系统性失败档
|
|
10
|
+
*
|
|
11
|
+
* 纯函数层在 subagent-failure-classification.test.ts;
|
|
12
|
+
* visible 模式 jsonl 三形态用例在 subagent-spawn-visible.test.ts。
|
|
13
|
+
*/
|
|
14
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
15
|
+
|
|
16
|
+
vi.mock("@pi-atelier/shared-utils", () => ({
|
|
17
|
+
loadAgentDef: vi.fn(),
|
|
18
|
+
getEffectiveConfig: vi.fn(() => ({ config: {}, sources: {} })),
|
|
19
|
+
}));
|
|
20
|
+
vi.mock("../timeout-config", () => ({
|
|
21
|
+
resolveSubagentTimeout: vi.fn(() => 30 * 60 * 1000),
|
|
22
|
+
DEFAULT_SUBAGENT_TIMEOUT_MS: 30 * 60 * 1000,
|
|
23
|
+
SUBAGENT_TIMEOUT_DEFAULTS: { timeoutMs: 1800000, agentTimeouts: {} },
|
|
24
|
+
}));
|
|
25
|
+
vi.mock("../intercom-bridge", () => ({
|
|
26
|
+
buildIntercomBridge: vi.fn(() => ({ PI_SUBAGENT_RUN_ID: "run-test" })),
|
|
27
|
+
resolveOrchestratorName: vi.fn(async () => undefined),
|
|
28
|
+
setOrchestratorNameResolver: vi.fn(),
|
|
29
|
+
}));
|
|
30
|
+
vi.mock("../subagent-utils", () => ({
|
|
31
|
+
writeTempPrompt: vi.fn().mockResolvedValue("/tmp/pi-pv-xxx/system-prompt.md"),
|
|
32
|
+
getPiCommand: vi.fn(),
|
|
33
|
+
}));
|
|
34
|
+
vi.mock("../subagent-spawn-once", () => ({ spawnOnce: vi.fn() }));
|
|
35
|
+
vi.mock("../subagent-spawn-visible", () => ({ spawnVisible: vi.fn() }));
|
|
36
|
+
|
|
37
|
+
import { loadAgentDef } from "@pi-atelier/shared-utils";
|
|
38
|
+
import { spawnOnce } from "../subagent-spawn-once";
|
|
39
|
+
import { spawnVisible } from "../subagent-spawn-visible";
|
|
40
|
+
import { runSubagent } from "../subagent";
|
|
41
|
+
|
|
42
|
+
const GOOD_CONSTRAINT = [{
|
|
43
|
+
rule: "必须包含 GOOD",
|
|
44
|
+
validate: (s: string) => (s.includes("GOOD") ? null : "缺少 GOOD"),
|
|
45
|
+
}];
|
|
46
|
+
|
|
47
|
+
describe("runSubagent 失败档(spec: subagent-spawn-runtime)", () => {
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
vi.clearAllMocks();
|
|
50
|
+
(loadAgentDef as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
51
|
+
name: "test-agent",
|
|
52
|
+
tools: ["read"],
|
|
53
|
+
systemPrompt: "You are a test agent",
|
|
54
|
+
});
|
|
55
|
+
delete process.env.TMUX;
|
|
56
|
+
delete process.env.WEZTERM_PANE;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("Scenario 1:系统性失败不重试且透出 stderr(exit 0 + 占位 + stderr 非空)", async () => {
|
|
60
|
+
(spawnOnce as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
61
|
+
exitCode: 0,
|
|
62
|
+
output: "(no output)",
|
|
63
|
+
stderr: "Error: Cannot find module './judge'\n at require (node:internal)",
|
|
64
|
+
});
|
|
65
|
+
const result = await runSubagent(
|
|
66
|
+
"test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, GOOD_CONSTRAINT, false,
|
|
67
|
+
);
|
|
68
|
+
// 重试计数为零:spawn 只被调用 1 次
|
|
69
|
+
expect(spawnOnce).toHaveBeenCalledTimes(1);
|
|
70
|
+
// 失败档闭合标记
|
|
71
|
+
expect(result.error).toBeTruthy();
|
|
72
|
+
// 报错信息含 stderr 尾部原文
|
|
73
|
+
expect(result.output).toContain("Cannot find module './judge'");
|
|
74
|
+
// exitCode 保持子进程原值(本场景即 0)
|
|
75
|
+
expect(result.exitCode).toBe(0);
|
|
76
|
+
// stderr 不被剥离
|
|
77
|
+
expect(result.stderr).toContain("Cannot find module './judge'");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("Scenario 2:个体违约保留重试(违约 + stderr 空 → 下一轮修正)", async () => {
|
|
81
|
+
(spawnOnce as ReturnType<typeof vi.fn>)
|
|
82
|
+
.mockResolvedValueOnce({ exitCode: 0, output: "bad format", stderr: "" })
|
|
83
|
+
.mockResolvedValueOnce({ exitCode: 0, output: "GOOD format", stderr: "" });
|
|
84
|
+
const result = await runSubagent(
|
|
85
|
+
"test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, GOOD_CONSTRAINT, false,
|
|
86
|
+
);
|
|
87
|
+
expect(spawnOnce).toHaveBeenCalledTimes(2);
|
|
88
|
+
expect(result.output).toBe("GOOD format");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("Scenario 3a:正常完成零输出不误伤(无 constraints 原样返回)", async () => {
|
|
92
|
+
(spawnOnce as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
93
|
+
exitCode: 0,
|
|
94
|
+
output: "(no output)",
|
|
95
|
+
stderr: "",
|
|
96
|
+
});
|
|
97
|
+
const result = await runSubagent("test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, undefined, false);
|
|
98
|
+
expect(result.output).toBe("(no output)");
|
|
99
|
+
expect(result.error).toBeUndefined();
|
|
100
|
+
expect(spawnOnce).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("Scenario 3b:正常完成零输出 + constraints → 按违约走个体失败重试档(现状)", async () => {
|
|
104
|
+
(spawnOnce as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
105
|
+
exitCode: 0,
|
|
106
|
+
output: "(no output)",
|
|
107
|
+
stderr: "",
|
|
108
|
+
});
|
|
109
|
+
const result = await runSubagent(
|
|
110
|
+
"test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, GOOD_CONSTRAINT, false,
|
|
111
|
+
);
|
|
112
|
+
// 现状行为:占位输出不满足约束 → 重派满 MAX_RETRIES=2
|
|
113
|
+
expect(spawnOnce).toHaveBeenCalledTimes(3);
|
|
114
|
+
expect(result.output).toBe("(no output)");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("Scenario 4:有实质输出时 stderr 不影响成功判定", async () => {
|
|
118
|
+
(spawnOnce as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
119
|
+
exitCode: 0,
|
|
120
|
+
output: "GOOD substantial output",
|
|
121
|
+
stderr: "non-fatal deprecation warning",
|
|
122
|
+
});
|
|
123
|
+
const result = await runSubagent(
|
|
124
|
+
"test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, GOOD_CONSTRAINT, false,
|
|
125
|
+
);
|
|
126
|
+
expect(spawnOnce).toHaveBeenCalledTimes(1);
|
|
127
|
+
expect(result.error).toBeUndefined();
|
|
128
|
+
expect(result.output).toBe("GOOD substantial output");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("Scenario 5(库侧):visible jsonl 信号源 error 注入后走系统性失败档", async () => {
|
|
132
|
+
// spawnVisible 被 mock:直接产出 finish() 注入信号后的形态(error 含信号源注明)
|
|
133
|
+
(spawnVisible as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
134
|
+
exitCode: 0,
|
|
135
|
+
output: "(no output)",
|
|
136
|
+
stderr: "",
|
|
137
|
+
error: "visible 模式信号源=jsonl 终结形态:session.jsonl 无 message 事件(未进入对话阶段)",
|
|
138
|
+
});
|
|
139
|
+
process.env.TMUX = "1";
|
|
140
|
+
const result = await runSubagent(
|
|
141
|
+
"test-agent", "task", "/cwd", undefined, undefined, undefined, undefined, GOOD_CONSTRAINT,
|
|
142
|
+
);
|
|
143
|
+
delete process.env.TMUX;
|
|
144
|
+
expect(spawnVisible).toHaveBeenCalledTimes(1); // 不重试
|
|
145
|
+
expect(result.error).toContain("visible 模式信号源=jsonl 终结形态");
|
|
146
|
+
expect(result.output).toContain("visible 模式信号源=jsonl 终结形态");
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 读:getEffectiveConfig 三层合并;写:patchSettingsSectionWithBackup
|
|
5
5
|
* 禁止裸 readFileSync/writeFileSync 整文件读写。
|
|
6
6
|
*/
|
|
7
|
-
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
7
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
8
8
|
|
|
9
9
|
const { patchMock } = vi.hoisted(() => ({
|
|
10
10
|
patchMock: vi.fn(),
|
|
@@ -157,35 +157,37 @@ describe("barrel 导出防回归", () => {
|
|
|
157
157
|
|
|
158
158
|
describe("persist 失败留痕护栏(harden-subagent-model-observability D2/D4)", () => {
|
|
159
159
|
beforeEach(() => {
|
|
160
|
-
|
|
160
|
+
// resetAllMocks 清除 implementation(含上 describe 残留的 throw),避免埋雷式跨界污染
|
|
161
|
+
vi.resetAllMocks();
|
|
162
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
161
163
|
setSubagentModelRestored(false);
|
|
162
164
|
});
|
|
165
|
+
afterEach(() => {
|
|
166
|
+
// 统一恢复 spy:断言失败时也不泄漏全局 mock(mockRestore 不在断言成功路径上)
|
|
167
|
+
vi.restoreAllMocks();
|
|
168
|
+
});
|
|
163
169
|
|
|
164
170
|
it("留痕传原始错误对象(stack/cause 不剥离)", () => {
|
|
165
|
-
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
166
171
|
const boom = new Error("lock contention", { cause: new Error("ENOENT") });
|
|
167
172
|
patchMock.mockImplementation(() => {
|
|
168
173
|
throw boom;
|
|
169
174
|
});
|
|
170
175
|
setSubagentModel("zai-coding-cn/glm-5.4");
|
|
171
|
-
expect(
|
|
176
|
+
expect(console.error).toHaveBeenCalledTimes(1);
|
|
172
177
|
// 第二参数必须是原 Error 对象(含 stack/cause),不是 e.message 字符串
|
|
173
|
-
expect(
|
|
178
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
174
179
|
"[subagent-model] persist 失败:",
|
|
175
180
|
boom,
|
|
176
181
|
);
|
|
177
|
-
errSpy.mockRestore();
|
|
178
182
|
});
|
|
179
183
|
|
|
180
184
|
it("留痕回退被拦截:静默吞时本用例变红(护栏)", () => {
|
|
181
|
-
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
182
185
|
patchMock.mockImplementation(() => {
|
|
183
186
|
throw new Error("boom");
|
|
184
187
|
});
|
|
185
188
|
setSubagentModel("zai-coding-cn/glm-5.4");
|
|
186
189
|
// 若 catch 改回静默吞,这里的 toHaveBeenCalledTimes(1) 失败 → 回归可见
|
|
187
|
-
expect(
|
|
188
|
-
errSpy.mockRestore();
|
|
190
|
+
expect(console.error).toHaveBeenCalledTimes(1);
|
|
189
191
|
});
|
|
190
192
|
|
|
191
193
|
it("越界防御:labels 短于 items 时按索引取值(D4)", () => {
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* 终端不可用 fallback / pane 消失后读取结果 / 超时 / abort。
|
|
6
6
|
*/
|
|
7
7
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
8
|
+
import { SubagentSession } from "../subagent-session";
|
|
8
9
|
|
|
9
10
|
vi.mock("../subagent-utils", () => ({
|
|
10
11
|
getPiCommand: vi.fn(() => ({ command: "node", args: ["/usr/local/bin/pi"] })),
|
|
@@ -20,6 +21,17 @@ vi.mock("../subagent-spawn-once", () => ({
|
|
|
20
21
|
spawnOnce: vi.fn(() => Promise.resolve({ exitCode: 0, output: "fallback", stderr: "" })),
|
|
21
22
|
}));
|
|
22
23
|
|
|
24
|
+
const { safeCleanupMock, createSafeCleanupMock } = vi.hoisted(() => ({
|
|
25
|
+
safeCleanupMock: vi.fn(() => Promise.resolve()),
|
|
26
|
+
createSafeCleanupMock: vi.fn((_opts: unknown) => null as any),
|
|
27
|
+
}));
|
|
28
|
+
vi.mock("../visible-cleanup", () => ({
|
|
29
|
+
createSafeCleanup: (opts: any) => {
|
|
30
|
+
createSafeCleanupMock(opts);
|
|
31
|
+
return safeCleanupMock;
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
|
|
23
35
|
const { backendMock, termDetect } = vi.hoisted(() => {
|
|
24
36
|
const backend = {
|
|
25
37
|
splitPane: vi.fn(() => "%42"),
|
|
@@ -58,6 +70,7 @@ vi.mock("node:fs", async (importOriginal) => {
|
|
|
58
70
|
import { spawnVisible } from "../subagent-spawn-visible";
|
|
59
71
|
import { spawnOnce } from "../subagent-spawn-once";
|
|
60
72
|
import { createVisibleTaskDir } from "../subagent-utils";
|
|
73
|
+
import { createSafeCleanup } from "../visible-cleanup";
|
|
61
74
|
|
|
62
75
|
describe("spawnVisible", () => {
|
|
63
76
|
beforeEach(() => {
|
|
@@ -66,6 +79,8 @@ describe("spawnVisible", () => {
|
|
|
66
79
|
backendMock.splitPane.mockReturnValue("%42");
|
|
67
80
|
backendMock.isPaneAlive.mockReturnValue(true);
|
|
68
81
|
backendMock.killPane.mockClear();
|
|
82
|
+
safeCleanupMock.mockClear();
|
|
83
|
+
createSafeCleanupMock.mockClear();
|
|
69
84
|
fsMock.existsSync.mockReturnValue(false);
|
|
70
85
|
fsMock.readFileSync.mockReturnValue("");
|
|
71
86
|
});
|
|
@@ -103,6 +118,31 @@ describe("spawnVisible", () => {
|
|
|
103
118
|
|
|
104
119
|
const result = await resultPromise;
|
|
105
120
|
expect(result.output).toContain("hello");
|
|
121
|
+
// 正常结束路径:finish 后走 safeCleanup(kill=false)(等死透后删目录,不主动杀)
|
|
122
|
+
expect(safeCleanupMock).toHaveBeenCalledWith(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// C1 修复防护:半行推进丢行反例——写侧未写完的行不解析、不丢失
|
|
126
|
+
it("半行不推进:未写完的行留到下一轮补读", async () => {
|
|
127
|
+
backendMock.isPaneAlive.mockReturnValue(true);
|
|
128
|
+
fsMock.existsSync.mockReturnValue(true);
|
|
129
|
+
// 第一轮:尾行是半行(无换行符)
|
|
130
|
+
fsMock.readFileSync.mockReturnValueOnce('{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"first"}]}}\n{"type":"par');
|
|
131
|
+
const parseSpy = vi.spyOn(SubagentSession.prototype, "parseSessionLine");
|
|
132
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
133
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
134
|
+
expect(parseSpy).toHaveBeenCalledTimes(1); // 只有完整行被解析
|
|
135
|
+
// 第二轮:写侧补全了第二行
|
|
136
|
+
fsMock.readFileSync.mockReturnValueOnce('{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"first"}]}}\n{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"second"}]}}\n');
|
|
137
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
138
|
+
expect(parseSpy).toHaveBeenCalledTimes(2);
|
|
139
|
+
// 收尾:pane 消失走 finish
|
|
140
|
+
backendMock.isPaneAlive.mockReturnValue(false);
|
|
141
|
+
fsMock.readFileSync.mockReturnValue('{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"second"}]}}\n');
|
|
142
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
143
|
+
const result = await resultPromise;
|
|
144
|
+
expect(result.output).toContain("second");
|
|
145
|
+
parseSpy.mockRestore();
|
|
106
146
|
});
|
|
107
147
|
|
|
108
148
|
it("超时时返回 timedOut 并清理 pane", async () => {
|
|
@@ -116,7 +156,12 @@ describe("spawnVisible", () => {
|
|
|
116
156
|
|
|
117
157
|
const result = await resultPromise;
|
|
118
158
|
expect(result.timedOut).toBe(true);
|
|
119
|
-
|
|
159
|
+
// 有序清理:超时路径交由 safeCleanup(kill=true)(内部先 killPane 再等死透后删目录)
|
|
160
|
+
expect(safeCleanupMock).toHaveBeenCalledWith(true);
|
|
161
|
+
expect(createSafeCleanupMock).toHaveBeenCalledWith(expect.objectContaining({
|
|
162
|
+
taskDir: "/tmp/pi-visible-test",
|
|
163
|
+
paneId: "%42",
|
|
164
|
+
}));
|
|
120
165
|
});
|
|
121
166
|
|
|
122
167
|
it("signal abort 时终止并清理", async () => {
|
|
@@ -131,7 +176,7 @@ describe("spawnVisible", () => {
|
|
|
131
176
|
const result = await resultPromise;
|
|
132
177
|
expect(result.stderr).toContain("aborted");
|
|
133
178
|
expect(result.exitCode).toBe(1);
|
|
134
|
-
expect(
|
|
179
|
+
expect(safeCleanupMock).toHaveBeenCalledWith(true);
|
|
135
180
|
});
|
|
136
181
|
|
|
137
182
|
it("extraEnv 透传到 createVisibleTaskDir", async () => {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试 F1 修复的 visible 模式 jsonl 终结形态判定
|
|
3
|
+
* (opsx-subarch-meta-baseline,spec: subagent-spawn-runtime,D-C1-4b)
|
|
4
|
+
*
|
|
5
|
+
* visible spawn 的 finish()(close 分支)信号源 = 子会话 session.jsonl:
|
|
6
|
+
* 三形态 + 超时分支不受干扰的防护用例。
|
|
7
|
+
* mock 骨架对齐 subagent-spawn-visible.test.ts(term-backend/fs/subagent-utils)。
|
|
8
|
+
*/
|
|
9
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
10
|
+
|
|
11
|
+
vi.mock("../subagent-utils", () => ({
|
|
12
|
+
getPiCommand: vi.fn(() => ({ command: "node", args: ["/usr/local/bin/pi"] })),
|
|
13
|
+
createVisibleTaskDir: vi.fn(() => ({
|
|
14
|
+
taskDir: "/tmp/pi-visible-xxx",
|
|
15
|
+
taskFile: "/tmp/pi-visible-xxx/task.txt",
|
|
16
|
+
sessionFile: "/tmp/pi-visible-xxx/session.jsonl",
|
|
17
|
+
launchScript: "/tmp/pi-visible-xxx/launch.sh",
|
|
18
|
+
})),
|
|
19
|
+
injectParentSession: vi.fn(),
|
|
20
|
+
}));
|
|
21
|
+
vi.mock("../subagent-spawn-once", () => ({
|
|
22
|
+
spawnOnce: vi.fn(() => Promise.resolve({ exitCode: 0, output: "fallback", stderr: "" })),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
const { backendMock, termDetect } = vi.hoisted(() => {
|
|
26
|
+
const backend = {
|
|
27
|
+
splitPane: vi.fn(() => "%42"),
|
|
28
|
+
killPane: vi.fn(),
|
|
29
|
+
isPaneAlive: vi.fn(() => false),
|
|
30
|
+
};
|
|
31
|
+
let detect: string | null = "tmux";
|
|
32
|
+
return {
|
|
33
|
+
backendMock: backend,
|
|
34
|
+
termDetect: {
|
|
35
|
+
set: (v: string | null) => { detect = v; },
|
|
36
|
+
get: () => detect,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
vi.mock("../term-backend", () => ({
|
|
41
|
+
detectTerminal: () => termDetect.get(),
|
|
42
|
+
getBackend: () => backendMock,
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
const { fsMock } = vi.hoisted(() => ({
|
|
46
|
+
fsMock: {
|
|
47
|
+
existsSync: vi.fn(() => false),
|
|
48
|
+
readFileSync: vi.fn(() => ""),
|
|
49
|
+
writeFileSync: vi.fn(),
|
|
50
|
+
rmSync: vi.fn(),
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
vi.mock("node:fs", () => ({ default: fsMock, ...fsMock }));
|
|
54
|
+
|
|
55
|
+
const { safeCleanupMock } = vi.hoisted(() => ({ safeCleanupMock: vi.fn(() => Promise.resolve()) }));
|
|
56
|
+
vi.mock("../visible-cleanup", () => ({
|
|
57
|
+
createSafeCleanup: () => safeCleanupMock,
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
import { spawnVisible } from "../subagent-spawn-visible";
|
|
61
|
+
|
|
62
|
+
describe("spawnVisible jsonl 终结形态判定(visible 失败信号源,D-C1-4b)", () => {
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
65
|
+
termDetect.set("tmux");
|
|
66
|
+
backendMock.splitPane.mockClear().mockReturnValue("%42");
|
|
67
|
+
backendMock.killPane.mockClear();
|
|
68
|
+
backendMock.isPaneAlive.mockReturnValue(false); // pane 消失 → close → finish()
|
|
69
|
+
safeCleanupMock.mockClear();
|
|
70
|
+
fsMock.existsSync.mockReturnValue(false);
|
|
71
|
+
fsMock.readFileSync.mockReturnValue("");
|
|
72
|
+
});
|
|
73
|
+
afterEach(() => {
|
|
74
|
+
vi.useRealTimers();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("形态①:jsonl 缺失 → error 注入「信号源=jsonl 终结形态」系统性失败档", async () => {
|
|
78
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
79
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
80
|
+
const result = await resultPromise;
|
|
81
|
+
expect(result.error).toContain("visible 模式信号源=jsonl 终结形态");
|
|
82
|
+
expect(result.error).toContain("session.jsonl 缺失");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("形态②:jsonl 存在但无 message 事件 → error 注入系统性失败档", async () => {
|
|
86
|
+
fsMock.existsSync.mockReturnValue(true);
|
|
87
|
+
// 只有 session 头事件,无 message(未进入对话阶段 = spawn 层阵亡)
|
|
88
|
+
fsMock.readFileSync.mockReturnValue('{"type":"session","id":"abc"}\n');
|
|
89
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
90
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
91
|
+
const result = await resultPromise;
|
|
92
|
+
expect(result.error).toContain("visible 模式信号源=jsonl 终结形态");
|
|
93
|
+
expect(result.error).toContain("无 message 事件");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("形态③:jsonl 正常终结(有 message 事件)→ 无 error,维持现状零输出档", async () => {
|
|
97
|
+
fsMock.existsSync.mockReturnValue(true);
|
|
98
|
+
fsMock.readFileSync.mockReturnValue('{"type":"message","message":{"role":"assistant","content":[]}}\n');
|
|
99
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
100
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
101
|
+
const result = await resultPromise;
|
|
102
|
+
expect(result.error).toBeUndefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("防护:超时分支不受 jsonl 判定干扰(timedOut 布尔是原有信号,非 error 字段)", async () => {
|
|
106
|
+
backendMock.isPaneAlive.mockReturnValue(true); // pane 存活 → 超时分支
|
|
107
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, undefined, 1000);
|
|
108
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
109
|
+
const result = await resultPromise;
|
|
110
|
+
expect(result.timedOut).toBe(true);
|
|
111
|
+
expect(result.error).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
});
|