pi-ate-workflow 0.2.4 → 0.2.6
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 +7 -4
- package/subagent-jsonl-signal.ts +76 -0
- package/subagent-spawn-once.ts +31 -5
- package/subagent-spawn-visible.ts +192 -179
- package/subagent-utils.ts +29 -12
- package/subagent.ts +140 -132
- package/tests/proc-guard.test.ts +5 -0
- package/tests/spawn-once-extra-env.test.ts +17 -6
- package/tests/subagent-failure-classification.test.ts +158 -0
- package/tests/subagent-failure-retry.test.ts +148 -0
- package/tests/subagent-find-session-file.test.ts +214 -0
- package/tests/subagent-spawn-once-jsonl-crosscheck.test.ts +243 -0
- package/tests/subagent-spawn-once.test.ts +16 -6
- package/tests/subagent-spawn-visible.test.ts +24 -0
- package/tests/subagent-visible-jsonl-signal.test.ts +113 -0
- package/tests/sweep-stale.test.ts +15 -0
- package/utils.ts +133 -74
|
@@ -63,6 +63,21 @@ describe("sweepStaleVisibleTaskDirs", () => {
|
|
|
63
63
|
} finally { fs.rmSync(root, { recursive: true, force: true }); }
|
|
64
64
|
});
|
|
65
65
|
|
|
66
|
+
// 存活的目录跳过(死透检查)——修复反例:C2 sweep 违反自身不变量
|
|
67
|
+
it("proc.pid 指向活进程的超龄目录跳过不删", () => {
|
|
68
|
+
const root = mkRoot();
|
|
69
|
+
try {
|
|
70
|
+
const alive = path.join(root, "pi-visible-alive");
|
|
71
|
+
fs.mkdirSync(alive);
|
|
72
|
+
fs.writeFileSync(path.join(alive, "task.txt"), "x");
|
|
73
|
+
backdateTree(alive, STALE_VISIBLE_TASK_DIR_MS * 2);
|
|
74
|
+
// 假装该目录属于当前 vitest 进程(两平台语义位同填,必然存活)
|
|
75
|
+
fs.writeFileSync(path.join(alive, "proc.pid"), `${process.pid} ${process.pid}\n`);
|
|
76
|
+
sweepStaleVisibleTaskDirs(root);
|
|
77
|
+
expect(fs.existsSync(alive)).toBe(true);
|
|
78
|
+
} finally { fs.rmSync(root, { recursive: true, force: true }); }
|
|
79
|
+
});
|
|
80
|
+
|
|
66
81
|
it("非 pi-visible-* 前缀目录不触碰", () => {
|
|
67
82
|
const root = mkRoot();
|
|
68
83
|
try {
|
package/utils.ts
CHANGED
|
@@ -1,74 +1,133 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* workflow: 子代理通用工具函数
|
|
3
|
-
*
|
|
4
|
-
* findSessionFile / getSubagentStatusSummary / isSubagentSuccess
|
|
5
|
-
* 所有使用 runSubagent 的扩展共享。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import * as fs from "node:fs";
|
|
9
|
-
import * as path from "node:path";
|
|
10
|
-
import * as os from "node:os";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
let
|
|
43
|
-
let
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* workflow: 子代理通用工具函数
|
|
3
|
+
*
|
|
4
|
+
* findSessionFile / getSubagentStatusSummary / isSubagentSuccess
|
|
5
|
+
* 所有使用 runSubagent 的扩展共享。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from "node:fs";
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import type { SubagentResult } from "./types";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 从 ~/.pi/agent/sessions/ 中找到指定会话 ID 对应的文件路径
|
|
15
|
+
*/
|
|
16
|
+
export function findSessionFile(sessionId: string): string | undefined {
|
|
17
|
+
const sessionDir = path.join(os.homedir(), ".pi", "agent", "sessions");
|
|
18
|
+
if (!fs.existsSync(sessionDir)) return undefined;
|
|
19
|
+
|
|
20
|
+
for (const projectDir of fs.readdirSync(sessionDir)) {
|
|
21
|
+
const projectPath = path.join(sessionDir, projectDir);
|
|
22
|
+
if (!fs.statSync(projectPath).isDirectory()) continue;
|
|
23
|
+
for (const file of fs.readdirSync(projectPath)) {
|
|
24
|
+
if (file.includes(sessionId)) {
|
|
25
|
+
return path.join(projectPath, file);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 读取子代理会话的最新状态摘要
|
|
34
|
+
*/
|
|
35
|
+
export function getSubagentStatusSummary(sessionId: string): string | undefined {
|
|
36
|
+
const filePath = findSessionFile(sessionId);
|
|
37
|
+
if (!filePath) return undefined;
|
|
38
|
+
try {
|
|
39
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
40
|
+
const lines = content.split("\n").filter(Boolean);
|
|
41
|
+
|
|
42
|
+
let msgCount = 0;
|
|
43
|
+
let lastToolName = "";
|
|
44
|
+
let lastTs = "";
|
|
45
|
+
for (const line of lines) {
|
|
46
|
+
try {
|
|
47
|
+
const e = JSON.parse(line);
|
|
48
|
+
if (e.type === "message" && e.message?.role === "assistant") {
|
|
49
|
+
msgCount++;
|
|
50
|
+
lastTs = e.timestamp || lastTs;
|
|
51
|
+
for (const c of e.message.content || []) {
|
|
52
|
+
if (c.type === "toolCall") lastToolName = c.name;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} catch { /* skip */ }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const age = lastTs ? `(${lastTs.slice(11, 19)})` : "";
|
|
59
|
+
const toolInfo = lastToolName ? `,最近动作: ${lastToolName}` : "";
|
|
60
|
+
return `${msgCount}条消息${toolInfo} ${age} [${(content.length / 1024).toFixed(0)}KB]`;
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 判断子代理是否成功执行
|
|
68
|
+
*
|
|
69
|
+
* 成功条件:exitCode=0 且无 error 且未超时,
|
|
70
|
+
* 或者输出足够长(>100字符)且不是空输出且未超时。
|
|
71
|
+
* 系统性失败形态(error 字段 / timedOut / 空占位输出兼 stderr 非空)一律判失败
|
|
72
|
+
* (exit 0 不再单独短路——opsx-subarch-meta-baseline F1 修复,spec: subagent-spawn-runtime)。
|
|
73
|
+
*/
|
|
74
|
+
export function isSubagentSuccess(result: { exitCode: number; output: string; error?: string; timedOut?: boolean; stderr?: string }): boolean {
|
|
75
|
+
if (isSystemicSubagentFailure(result)) return false;
|
|
76
|
+
if (result.exitCode === 0 && !result.error && !result.timedOut) return true;
|
|
77
|
+
return result.output.length > 100 && result.output !== "(no output)" && !result.timedOut;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ─── 系统性失败分类(F1 修复,spec: subagent-spawn-runtime)─────────────
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 空占位输出形态文本(spawn 各分支兜底输出的字面量)。
|
|
84
|
+
* 字面量由测试锁死(subagent-failure-classification.test.ts)——
|
|
85
|
+
* 改动任一字面量须同步 spawn 分支与测试,防文本漂移静默破坏分类。
|
|
86
|
+
*/
|
|
87
|
+
export const EMPTY_OUTPUT_PLACEHOLDERS: readonly string[] = [
|
|
88
|
+
"(no output)",
|
|
89
|
+
"(stalled, no output for 5 min)",
|
|
90
|
+
"(timed out)",
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
/** stderr 尾部透出长度上限(防超长) */
|
|
94
|
+
export const STDERR_TAIL_LIMIT = 2000;
|
|
95
|
+
|
|
96
|
+
/** 判定输出是否为空占位形态:空串/纯空白,或包含任一已知占位字面量(前缀或包含匹配) */
|
|
97
|
+
export function isEmptyPlaceholderOutput(output: string): boolean {
|
|
98
|
+
if (output.trim() === "") return true;
|
|
99
|
+
return EMPTY_OUTPUT_PLACEHOLDERS.some((p) => output.includes(p));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 判定结果是否为系统性失败(spawn 层阵亡,重试无效)。
|
|
104
|
+
* 信号优先级:①error 字段存在;②timedOut 布尔为 true;③空占位输出兼 stderr 非空。
|
|
105
|
+
* 有实质输出时 stderr 不参与判定(噪声不误伤)。
|
|
106
|
+
*/
|
|
107
|
+
export function isSystemicSubagentFailure(
|
|
108
|
+
result: { output: string; error?: string; timedOut?: boolean; stderr?: string },
|
|
109
|
+
): boolean {
|
|
110
|
+
if (result.error) return true;
|
|
111
|
+
if (result.timedOut) return true;
|
|
112
|
+
return isEmptyPlaceholderOutput(result.output) && !!(result.stderr && result.stderr.trim());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 组装系统性失败档结果:跳过重试后返回的呈现形态。
|
|
117
|
+
* - output = 报错说明 + stderr 尾部(截尾防超长)
|
|
118
|
+
* - error 字段必填(失败档闭合标记,isSubagentSuccess 由此判 false)
|
|
119
|
+
* - exitCode 保持子进程原值(不因判失败而篡改)
|
|
120
|
+
*/
|
|
121
|
+
export function buildSystemicFailureResult(result: SubagentResult): SubagentResult {
|
|
122
|
+
const reason = result.error
|
|
123
|
+
? result.error
|
|
124
|
+
: result.timedOut
|
|
125
|
+
? "timed out"
|
|
126
|
+
: "empty placeholder output with non-empty stderr";
|
|
127
|
+
const stderrTail = result.stderr.trim() ? result.stderr.slice(-STDERR_TAIL_LIMIT) : "";
|
|
128
|
+
const lines = [`[子代理系统性失败] ${reason}——已跳过自动重试(系统性失败重试无效)。`];
|
|
129
|
+
if (stderrTail) {
|
|
130
|
+
lines.push("", "—— stderr 尾部 ——", stderrTail);
|
|
131
|
+
}
|
|
132
|
+
return { ...result, output: lines.join("\n"), error: result.error || `systemic failure: ${reason}` };
|
|
133
|
+
}
|