@zhushanwen/pi-subagent-workflow 0.4.1 → 0.4.3

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.
@@ -28,7 +28,9 @@ workflow run parallel --args target="src/auth/login.ts"
28
28
  workflow run parallel --args target="..." --args 'perspectives=["security","readability"]'
29
29
  ```
30
30
 
31
- `perspectives` 默认 `["security","performance","maintainability"]`。每个视角一个并行 agent,各自返回评分+发现的问题;最后再一个 agent 汇总成总体评分+top 问题+共识。
31
+ `perspectives` 默认 `["security","performance","maintainability"]`。每个视角一个并行 agent,各自返回评分+发现的问题,最后纯代码拼接各视角的 findings。
32
+
33
+ > **Note (breaking)**: `outcome.aggregate` is now a concatenated string of each perspective's findings (format: `[perspective] finding1; finding2`, joined by newlines). Previously it was an LLM-produced object `{overallScore, topIssues, consensus}`. If you have generated workflows or downstream tools parsing the old object shape, update them to read `outcome.per_perspective` for structured per-perspective scores/findings, or treat `outcome.aggregate` as plain text.
32
34
 
33
35
  ### scatter-gather — 分发-收集
34
36
 
@@ -36,7 +38,7 @@ workflow run parallel --args target="..." --args 'perspectives=["security","read
36
38
  workflow run scatter-gather --args task="重构认证模块,涉及 session/jwt/oauth 三块"
37
39
  ```
38
40
 
39
- 三段:第一个 agent 把大任务拆成 2-4 个可并行子任务 → `parallel()` 并行处理每个子任务 → 最后一个 agent 合并所有结果。
41
+ 三段:第一个 agent 把大任务拆成 2-4 个可并行子任务 → `parallel()` 并行处理每个子任务 → gather 阶段用 `agent()` 把各子任务结果合并成最终结论(LLM 合并,非纯代码拼接)。
40
42
 
41
43
  ### map-reduce — 映射-归约
42
44
 
@@ -45,7 +47,7 @@ workflow run map-reduce --args 'items=["file1.ts","file2.ts","file3.ts"]' --args
45
47
  workflow run map-reduce --args itemsJson=/path/to/items.json --args operation="..."
46
48
  ```
47
49
 
48
- `items` 直接传 JSON 数组,或 `itemsJson` 传 JSON 文件路径(二选一)。`parallel()` 对每个 item 并行执行 `operation` → 一个 agent 把所有结果归约成单一结论。
50
+ `items` 直接传 JSON 数组,或 `itemsJson` 传 JSON 文件路径(二选一)。`parallel()` 对每个 item 并行执行 `operation` → reduce 阶段用 `agent()` 把各 item 的 map 结果归约成单一结论(LLM 归约,非纯代码拼接)。
49
51
 
50
52
  ## 编排 API
51
53
 
@@ -19,8 +19,8 @@ const meta = {
19
19
 
20
20
  // ── 入参($ARGS)──────────────────────────────────────────────────
21
21
  const task = $ARGS.task;
22
- if (!task) {
23
- throw new Error("chain 缺少必需参数 task。用法:workflow run chain --args task=\"<任务描述>\"");
22
+ if (typeof task !== "string" || task.trim() === "") {
23
+ throw new Error("chain 缺少必需参数 task(非空字符串)。用法:workflow run chain --args task=\"<描述>\"");
24
24
  }
25
25
 
26
26
  log("chain 开始,task=" + task);
@@ -82,12 +82,12 @@ try {
82
82
  let mapFailed = 0;
83
83
  for (let i = 0; i < mappedRaw.length; i++) {
84
84
  const r = mappedRaw[i];
85
- if (!r || r.error) {
85
+ if (!r || r.status === "failed" || r.error) {
86
86
  mapped.push({
87
87
  itemIndex: i,
88
88
  item: items[i],
89
89
  status: "failed",
90
- error: r ? r.error : "agent 无返回",
90
+ error: r ? (r.error || "agent 返回 failed 状态") : "agent 无返回",
91
91
  });
92
92
  mapFailed++;
93
93
  } else {
@@ -95,7 +95,7 @@ try {
95
95
  itemIndex: i,
96
96
  item: items[i],
97
97
  status: "ok",
98
- mapped: r.mapped,
98
+ mapped: (typeof r.mapped === "string" ? r.mapped : "(无结果)"),
99
99
  });
100
100
  }
101
101
  }
@@ -104,10 +104,11 @@ try {
104
104
  }
105
105
  log("map 完成:ok=" + (items.length - mapFailed) + " failed=" + mapFailed);
106
106
 
107
- // ── 段 2:reduce(agent 聚合所有 map 结果)──────────────────────
107
+ // ── 段 2:reduce(agent 归约所有 map 结果)──────────────────────
108
108
  phase("reduce");
109
109
  currentPhase = "reduce";
110
- const reduced = await agent({
110
+
111
+ const reducedResult = await agent({
111
112
  prompt:
112
113
  "以下是对 " + items.length + " 个 item 执行「" + operation + "」的结果,请归约成单一结论:\n\n" +
113
114
  JSON.stringify(mapped, null, 2),
@@ -127,7 +128,10 @@ try {
127
128
  phases_run: ["map", "reduce"],
128
129
  items_total: items.length,
129
130
  items_mapped: items.length - mapFailed,
130
- reduced: { reduced: (reduced?.reduced ?? "(归约无结果)"), stats: (reduced?.stats ?? "(归约无结果)") },
131
+ reduced: {
132
+ reduced: (reducedResult?.reduced ?? "(归约无结果)"),
133
+ stats: (reducedResult?.stats ?? "(归约无结果)"),
134
+ },
131
135
  message: "map-reduce 完成:map " + items.length + " 项(失败 " + mapFailed + ")→ reduce",
132
136
  };
133
137
  } catch (err) {
@@ -28,6 +28,9 @@ if (!target) {
28
28
  const perspectives = Array.isArray($ARGS.perspectives) && $ARGS.perspectives.length > 0
29
29
  ? $ARGS.perspectives
30
30
  : ["security", "performance", "maintainability"];
31
+ if (perspectives.some((p) => typeof p !== "string")) {
32
+ throw new Error("parallel 参数 perspectives 必须是字符串数组,实际含非字符串元素");
33
+ }
31
34
 
32
35
  log("parallel 开始,target=" + target + " perspectives=" + JSON.stringify(perspectives));
33
36
 
@@ -68,15 +71,20 @@ try {
68
71
  let failedCount = 0;
69
72
  for (let i = 0; i < perPerspectiveRaw.length; i++) {
70
73
  const r = perPerspectiveRaw[i];
71
- if (!r || r.error) {
74
+ if (!r || r.status === "failed" || r.error) {
72
75
  perPerspective.push({
73
76
  perspective: perspectives[i],
74
77
  status: "failed",
75
- error: r ? r.error : "agent 无返回",
78
+ error: r ? (r.error || "agent 返回 failed 状态") : "agent 无返回",
76
79
  });
77
80
  failedCount++;
78
81
  } else {
79
- perPerspective.push({ perspective: perspectives[i], status: "ok", ...r });
82
+ perPerspective.push({
83
+ perspective: perspectives[i],
84
+ status: "ok",
85
+ score: typeof r.score === "number" ? r.score : undefined,
86
+ findings: Array.isArray(r.findings) ? r.findings : [],
87
+ });
80
88
  }
81
89
  }
82
90
  if (failedCount === perspectives.length) {
@@ -87,36 +95,18 @@ try {
87
95
  // ── 段 2:aggregate(汇总多视角结果)────────────────────────────
88
96
  phase("aggregate");
89
97
  currentPhase = "aggregate";
90
- const aggregate = await agent({
91
- prompt:
92
- "以下是多视角分析结果,请综合出总体评分、top 问题和共识:\n\n" +
93
- JSON.stringify(perPerspective, null, 2),
94
- schema: {
95
- type: "object",
96
- properties: {
97
- overallScore: { type: "number", description: "综合评分 0-10" },
98
- topIssues: {
99
- type: "array",
100
- items: { type: "string" },
101
- description: "最关键的问题(按严重度排序)",
102
- },
103
- consensus: { type: "string", description: "多视角共识总结" },
104
- },
105
- required: ["overallScore", "topIssues", "consensus"],
106
- },
107
- description: "parallel-aggregate",
108
- });
98
+
99
+ // 纯代码合并:拼接各视角发现的问题(不调用 LLM)
100
+ const aggregateResult = perPerspective
101
+ .map((p) => "[" + (p.perspective || "?") + "] " + (p.findings ? p.findings.join("; ") : "(no findings)"))
102
+ .join("\n");
109
103
 
110
104
  outcome = {
111
105
  status: failedCount > 0 ? "partial" : "ok",
112
106
  phases_run: ["parallel-analyze", "aggregate"],
113
107
  perspectives_analyzed: perspectives.length,
114
108
  per_perspective: perPerspective,
115
- aggregate: {
116
- overallScore: (aggregate?.overallScore ?? "(聚合无结果)"),
117
- topIssues: (aggregate?.topIssues ?? []),
118
- consensus: (aggregate?.consensus ?? "(聚合无结果)"),
119
- },
109
+ aggregate: aggregateResult,
120
110
  message: "parallel 完成:" + perspectives.length + " 视角(失败 " + failedCount + ")→ 聚合",
121
111
  };
122
112
  } catch (err) {
@@ -62,6 +62,9 @@ try {
62
62
  if (subtasks.length === 0) {
63
63
  throw new Error("scatter 返回的 subtasks 为空");
64
64
  }
65
+ if (subtasks.some((s) => !s || typeof s.name !== "string")) {
66
+ throw new Error("scatter 返回的 subtasks 每项需含 name 字符串字段");
67
+ }
65
68
  log("scatter 出 " + subtasks.length + " 个子任务");
66
69
 
67
70
  // ── 段 2:process(parallel 并行处理每个子任务)──────────────────
@@ -89,15 +92,19 @@ try {
89
92
  let failedCount = 0;
90
93
  for (let i = 0; i < processedRaw.length; i++) {
91
94
  const r = processedRaw[i];
92
- if (!r || r.error) {
95
+ if (!r || r.status === "failed" || r.error) {
93
96
  processed.push({
94
97
  subtask: subtasks[i].name,
95
98
  status: "failed",
96
- error: r ? r.error : "agent 无返回",
99
+ error: r ? (r.error || "agent 返回 failed 状态") : "agent 无返回",
97
100
  });
98
101
  failedCount++;
99
102
  } else {
100
- processed.push({ subtask: subtasks[i].name, status: "ok", result: r.result });
103
+ processed.push({
104
+ subtask: subtasks[i].name,
105
+ status: "ok",
106
+ result: (typeof r.result === "string" ? r.result : "(无结果)"),
107
+ });
101
108
  }
102
109
  }
103
110
  if (failedCount === subtasks.length) {
@@ -105,10 +112,11 @@ try {
105
112
  }
106
113
  log("process 完成:ok=" + (subtasks.length - failedCount) + " failed=" + failedCount);
107
114
 
108
- // ── 段 3:gather(合并所有子任务结果)───────────────────────────
115
+ // ── 段 3:gather(agent 合并所有子任务结果)─────────────────────
109
116
  phase("gather");
110
117
  currentPhase = "gather";
111
- const gathered = await agent({
118
+
119
+ const gatheredResult = await agent({
112
120
  prompt:
113
121
  "以下是各子任务的处理结果,请合并成一个完整、一致的最终结果:\n\n" +
114
122
  JSON.stringify(processed, null, 2),
@@ -129,8 +137,8 @@ try {
129
137
  subtasks_total: subtasks.length,
130
138
  subtasks_processed: subtasks.length - failedCount,
131
139
  gathered: {
132
- mergedResult: (gathered?.mergedResult ?? "(合并无结果)"),
133
- completeness: (gathered?.completeness ?? "(合并无结果)"),
140
+ mergedResult: (gatheredResult?.mergedResult ?? "(合并无结果)"),
141
+ completeness: (gatheredResult?.completeness ?? "(合并无结果)"),
134
142
  },
135
143
  message: "scatter-gather 完成:split " + subtasks.length + " → process(失败 " + failedCount + ")→ merge",
136
144
  };