@zhushanwen/pi-subagent-workflow 0.1.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/agents/context-builder.md +17 -0
- package/agents/general-purpose.md +16 -0
- package/agents/oracle.md +17 -0
- package/agents/planner.md +17 -0
- package/agents/researcher.md +17 -0
- package/agents/reviewer.md +17 -0
- package/agents/scout.md +17 -0
- package/agents/worker.md +16 -0
- package/examples/README.md +43 -0
- package/examples/chain.example.js +92 -0
- package/examples/map-reduce.example.js +99 -0
- package/examples/parallel.example.js +82 -0
- package/examples/scatter-gather.example.js +106 -0
- package/index.ts +1 -0
- package/package.json +66 -0
- package/skills/workflow-script-format/SKILL.md +328 -0
- package/src/execution/__tests__/agent-registry.test.ts +164 -0
- package/src/execution/__tests__/agent-result-mapper.test.ts +128 -0
- package/src/execution/__tests__/alive-store.test.ts +147 -0
- package/src/execution/__tests__/bg-notify-render.test.ts +256 -0
- package/src/execution/__tests__/concurrency-pool.test.ts +217 -0
- package/src/execution/__tests__/config.test.ts +110 -0
- package/src/execution/__tests__/crash-recovery.test.ts +311 -0
- package/src/execution/__tests__/execute-nesting.test.ts +359 -0
- package/src/execution/__tests__/execute-options-mapper.test.ts +138 -0
- package/src/execution/__tests__/execution-record.test.ts +959 -0
- package/src/execution/__tests__/finalized-marker.test.ts +82 -0
- package/src/execution/__tests__/format-schema-instruction.test.ts +135 -0
- package/src/execution/__tests__/format.test.ts +320 -0
- package/src/execution/__tests__/helpers/mock-extension-api.ts +30 -0
- package/src/execution/__tests__/list-component.test.ts +347 -0
- package/src/execution/__tests__/model-resolver.test.ts +356 -0
- package/src/execution/__tests__/output-collector.test.ts +61 -0
- package/src/execution/__tests__/path-encoding.test.ts +75 -0
- package/src/execution/__tests__/pi-invocation.test.ts +73 -0
- package/src/execution/__tests__/record-store.test.ts +545 -0
- package/src/execution/__tests__/run-spawn-edges.test.ts +439 -0
- package/src/execution/__tests__/run-spawn-integration.test.ts +897 -0
- package/src/execution/__tests__/sdk-contract.test.ts +272 -0
- package/src/execution/__tests__/session-context-resolver.test.ts +167 -0
- package/src/execution/__tests__/session-file-gc.test.ts +247 -0
- package/src/execution/__tests__/session-reconstructor.test.ts +359 -0
- package/src/execution/__tests__/session-runner-schema-env.test.ts +314 -0
- package/src/execution/__tests__/session-start-reaper.test.ts +227 -0
- package/src/execution/__tests__/spawn-args.test.ts +244 -0
- package/src/execution/__tests__/spawn-event-adapter.test.ts +167 -0
- package/src/execution/__tests__/subagent-service.test.ts +678 -0
- package/src/execution/__tests__/subprocess-agent-runner.test.ts +389 -0
- package/src/execution/__tests__/temp-prompt.test.ts +53 -0
- package/src/execution/__tests__/timeout-integration.test.ts +381 -0
- package/src/execution/__tests__/tombstone-store.test.ts +73 -0
- package/src/execution/__tests__/tool-action.test.ts +330 -0
- package/src/execution/__tests__/turn-limiter.test.ts +65 -0
- package/src/execution/__tests__/worktree-manager.test.ts +423 -0
- package/src/execution/__tests__/worktree-registry.test.ts +161 -0
- package/src/execution/agent-registry.ts +252 -0
- package/src/execution/agent-result-mapper.ts +84 -0
- package/src/execution/alive-store.ts +92 -0
- package/src/execution/best-effort.ts +30 -0
- package/src/execution/concurrency-pool.ts +84 -0
- package/src/execution/config.ts +73 -0
- package/src/execution/execute-options-mapper.ts +86 -0
- package/src/execution/execution-record.ts +778 -0
- package/src/execution/finalized-marker.ts +51 -0
- package/src/execution/model-config-service.ts +225 -0
- package/src/execution/model-resolver.ts +247 -0
- package/src/execution/notifier.ts +168 -0
- package/src/execution/output-collector.ts +88 -0
- package/src/execution/path-encoding.ts +34 -0
- package/src/execution/pi-invocation.ts +70 -0
- package/src/execution/record-store.ts +350 -0
- package/src/execution/session-context-resolver.ts +64 -0
- package/src/execution/session-file-gc.ts +98 -0
- package/src/execution/session-reconstructor.ts +450 -0
- package/src/execution/session-runner.ts +725 -0
- package/src/execution/spawn-event-adapter.ts +150 -0
- package/src/execution/subagent-service.ts +973 -0
- package/src/execution/subprocess-agent-runner.ts +108 -0
- package/src/execution/temp-prompt.ts +57 -0
- package/src/execution/tombstone-store.ts +72 -0
- package/src/execution/turn-limiter.ts +88 -0
- package/src/execution/types.ts +634 -0
- package/src/execution/worktree-manager.ts +285 -0
- package/src/execution/worktree-registry.ts +144 -0
- package/src/index.ts +454 -0
- package/src/interface/bg-notify-render.ts +286 -0
- package/src/interface/commands.ts +157 -0
- package/src/interface/format.ts +501 -0
- package/src/interface/gui-adapter.ts +136 -0
- package/src/interface/helpers.ts +110 -0
- package/src/interface/list-component.ts +643 -0
- package/src/interface/list-shared.ts +84 -0
- package/src/interface/list-view.ts +373 -0
- package/src/interface/reentry-guard.ts +30 -0
- package/src/interface/subagent-actions.ts +294 -0
- package/src/interface/subagent-tool.ts +294 -0
- package/src/interface/subagents.ts +30 -0
- package/src/interface/tool-render.ts +333 -0
- package/src/interface/tool-workflow-script.ts +351 -0
- package/src/interface/tool-workflow.ts +485 -0
- package/src/interface/views/WorkflowsView.ts +944 -0
- package/src/interface/views/detail-content.ts +298 -0
- package/src/interface/views/format.ts +320 -0
- package/src/orchestration/__tests__/concurrency-gate.test.ts +125 -0
- package/src/orchestration/__tests__/config-loader.test.ts +381 -0
- package/src/orchestration/__tests__/error-recovery-handlers.test.ts +332 -0
- package/src/orchestration/__tests__/error-recovery-workflow-call.test.ts +166 -0
- package/src/orchestration/__tests__/launcher-nested-workflow.test.ts +248 -0
- package/src/orchestration/__tests__/lifecycle.test.ts +385 -0
- package/src/orchestration/__tests__/script-lint.test.ts +347 -0
- package/src/orchestration/__tests__/worker-script-builder.test.ts +42 -0
- package/src/orchestration/__tests__/workflow-nesting-e2e.test.ts +319 -0
- package/src/orchestration/agent-opts-resolver.ts +128 -0
- package/src/orchestration/concurrency-gate.ts +69 -0
- package/src/orchestration/config-loader.ts +313 -0
- package/src/orchestration/error-recovery.ts +578 -0
- package/src/orchestration/execute-agent-call.ts +174 -0
- package/src/orchestration/jsonl-run-store.ts +292 -0
- package/src/orchestration/launcher.ts +368 -0
- package/src/orchestration/lifecycle.ts +373 -0
- package/src/orchestration/models/__tests__/budget.test.ts +367 -0
- package/src/orchestration/models/agent-call.ts +76 -0
- package/src/orchestration/models/budget.ts +148 -0
- package/src/orchestration/models/ports.ts +165 -0
- package/src/orchestration/models/run-runtime.ts +91 -0
- package/src/orchestration/models/run-spec.ts +54 -0
- package/src/orchestration/models/run-state.ts +44 -0
- package/src/orchestration/models/trace.ts +102 -0
- package/src/orchestration/models/types.ts +242 -0
- package/src/orchestration/models/workflow-run.ts +275 -0
- package/src/orchestration/models/workflow-script-registry.ts +32 -0
- package/src/orchestration/models/workflow-script.ts +90 -0
- package/src/orchestration/node-ops.ts +192 -0
- package/src/orchestration/script-lint.ts +387 -0
- package/src/orchestration/skill-discovery.ts +60 -0
- package/src/orchestration/worker-handle.ts +115 -0
- package/src/orchestration/worker-host.ts +93 -0
- package/src/orchestration/worker-script-builder.ts +281 -0
- package/src/orchestration/workflow-files.ts +85 -0
- package/src/orchestration/workflow-script-registry-impl.ts +128 -0
- package/src/shared/__tests__/resource-discovery.test.ts +226 -0
- package/src/shared/agent-event.ts +13 -0
- package/src/shared/resource-discovery.ts +535 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workflow Extension — launcher
|
|
3
|
+
*
|
|
4
|
+
* runAndWait free function(D-12)。跨扩展编程入口(pi.__workflowRun)——
|
|
5
|
+
* 阻塞至 run 到达 done 终态。
|
|
6
|
+
*
|
|
7
|
+
* **D-8 签名**:返回 WorkflowRunResult({status:"done", reason, ...})——
|
|
8
|
+
* status 恒为 "done",具体原因由 reason 区分
|
|
9
|
+
* (completed/failed/aborted/budget_limited/time_limited)。
|
|
10
|
+
*
|
|
11
|
+
* **C.7**:timeout → transition done,time_limited(仅返回 timeout 标记但不转终态
|
|
12
|
+
* 会让 workflow 仍 running,资源泄漏)。
|
|
13
|
+
*
|
|
14
|
+
* 流程:
|
|
15
|
+
* 1. registry.get(name) → WorkflowScript(未找到返回 failed)
|
|
16
|
+
* 2. script.validate(lint 检查)→ 失败抛错(不进 runWorkflow)
|
|
17
|
+
* 3. script.toExecutable → 可执行源
|
|
18
|
+
* 4. 构建 RunSpec + runWorkflow(spec, deps, signal)
|
|
19
|
+
* 5. 轮询至 done(间隔 STATUS_POLL_INTERVAL_MS)
|
|
20
|
+
* 6. timeout → abortRun + transition done,time_limited
|
|
21
|
+
* 7. signal.aborted → abortRun + reason=aborted
|
|
22
|
+
*
|
|
23
|
+
* 层归属:Engine。依赖 registry + runWorkflow/abortRun + LifecycleDeps。
|
|
24
|
+
*
|
|
25
|
+
* 参考:domain-models.md §D-8(WorkflowRunResult 签名)、clarification.md C.7。
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { abortRun, runWorkflow } from "./lifecycle.ts";
|
|
29
|
+
import type { LifecycleDeps } from "./models/ports.ts";
|
|
30
|
+
import type { RunSpec } from "./models/run-spec.ts";
|
|
31
|
+
import type { DoneReason } from "./models/types.ts";
|
|
32
|
+
import type { WorkflowRun } from "./models/workflow-run.ts";
|
|
33
|
+
import type { WorkflowScriptRegistry } from "./models/workflow-script-registry.ts";
|
|
34
|
+
|
|
35
|
+
// ── 常量 ─────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
/** 默认 runAndWait 超时(10 分钟)。 */
|
|
38
|
+
const DEFAULT_RUNANDWAIT_TIMEOUT_MS = 600_000;
|
|
39
|
+
|
|
40
|
+
/** 轮询间隔(500ms)。 */
|
|
41
|
+
const STATUS_POLL_INTERVAL_MS = 500;
|
|
42
|
+
|
|
43
|
+
// ── 类型 ─────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* runAndWait 的返回(D-8 签名)。
|
|
47
|
+
*
|
|
48
|
+
* status 恒为 "done"(runAndWait 阻塞至 done 才返回);具体原因由 reason 区分
|
|
49
|
+
* (completed/failed/aborted/budget_limited/time_limited)。
|
|
50
|
+
*/
|
|
51
|
+
export interface WorkflowRunResult {
|
|
52
|
+
/** 恒为 "done"(runAndWait 阻塞至 done)。 */
|
|
53
|
+
status: "done";
|
|
54
|
+
/** 终态原因(completed/failed/aborted/budget_limited/time_limited)。 */
|
|
55
|
+
reason: DoneReason;
|
|
56
|
+
/** 脚本返回值(reason==="completed" 时有)。 */
|
|
57
|
+
scriptResult?: unknown;
|
|
58
|
+
/** 错误信息(reason!=="completed" 时可有)。 */
|
|
59
|
+
error?: string;
|
|
60
|
+
/** run 标识。 */
|
|
61
|
+
runId: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Launcher 依赖:LifecycleDeps + registry(脚本发现)。
|
|
66
|
+
*
|
|
67
|
+
* registry 是「发现依赖」(文件系统扫描),与 LifecycleDeps 的 3 个 port
|
|
68
|
+
* (执行依赖:子进程/线程/持久化)性质不同——故单独扩展,不进 LifecycleDeps。
|
|
69
|
+
*/
|
|
70
|
+
export interface LauncherDeps extends LifecycleDeps {
|
|
71
|
+
/** workflow 脚本仓库。 */
|
|
72
|
+
registry: WorkflowScriptRegistry;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── 内部 helper ──────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
/** 轮询间隔 Promise。 */
|
|
78
|
+
function pollInterval(): Promise<void> {
|
|
79
|
+
return new Promise((resolve) => setTimeout(resolve, STATUS_POLL_INTERVAL_MS));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 从 WorkflowRun 构建 WorkflowRunResult(D-8)。
|
|
84
|
+
*
|
|
85
|
+
* reason 取 run.state.reason(done 时必有,WorkflowRun 不变式 I2 保证),
|
|
86
|
+
* 防御性 fallback "failed"(理论不可达——I2 保证 done 时 reason 已设)。
|
|
87
|
+
*/
|
|
88
|
+
function toResult(run: WorkflowRun): WorkflowRunResult {
|
|
89
|
+
return {
|
|
90
|
+
status: "done",
|
|
91
|
+
reason: run.state.reason ?? "failed",
|
|
92
|
+
scriptResult: run.state.scriptResult,
|
|
93
|
+
error: run.state.error,
|
|
94
|
+
runId: run.runId,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ── pollRunToResult(runAndWait + executeNestedWorkflow 共用轮询) ────
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 轮询 run 至 done 终态并返回 WorkflowRunResult。
|
|
102
|
+
*
|
|
103
|
+
* runAndWait 与 executeNestedWorkflow 共用的轮询逻辑(D-12 后去重):
|
|
104
|
+
* - signal.aborted → 先查 run 是否已 done(避免二次 safeAbort 写不同 error 造成
|
|
105
|
+
* 非确定性),否则 safeAbort(aborted) + 返回 aborted 结果
|
|
106
|
+
* - run 丢失 → failed 结果
|
|
107
|
+
* - run done → toResult
|
|
108
|
+
* - deadline 到 → 先查 done,否则 safeAbort(time_limited) + 返回 timeout 结果
|
|
109
|
+
*
|
|
110
|
+
* @param abortReason signal abort 时写入 run.state.error 的原因串。runAndWait 传
|
|
111
|
+
* "Aborted by signal";executeNestedWorkflow 传 "Aborted by parent signal"。
|
|
112
|
+
*/
|
|
113
|
+
async function pollRunToResult(
|
|
114
|
+
runId: string,
|
|
115
|
+
deps: LauncherDeps,
|
|
116
|
+
signal: AbortSignal | undefined,
|
|
117
|
+
timeoutMs: number,
|
|
118
|
+
abortReason: string,
|
|
119
|
+
): Promise<WorkflowRunResult> {
|
|
120
|
+
const deadline = Date.now() + timeoutMs;
|
|
121
|
+
while (Date.now() < deadline) {
|
|
122
|
+
if (signal?.aborted) {
|
|
123
|
+
const runBeforeAbort = deps.runs.get(runId);
|
|
124
|
+
if (runBeforeAbort?.state.status === "done") return toResult(runBeforeAbort);
|
|
125
|
+
await safeAbort(runId, deps, abortReason, "aborted");
|
|
126
|
+
const run = deps.runs.get(runId);
|
|
127
|
+
return run
|
|
128
|
+
? toResult(run)
|
|
129
|
+
: { status: "done", reason: "aborted", error: abortReason, runId };
|
|
130
|
+
}
|
|
131
|
+
const run = deps.runs.get(runId);
|
|
132
|
+
if (!run) return { status: "done", reason: "failed", error: "Run not found", runId };
|
|
133
|
+
if (run.state.status === "done") return toResult(run);
|
|
134
|
+
await pollInterval();
|
|
135
|
+
}
|
|
136
|
+
const runBeforeTimeout = deps.runs.get(runId);
|
|
137
|
+
if (runBeforeTimeout?.state.status === "done") return toResult(runBeforeTimeout);
|
|
138
|
+
await safeAbort(runId, deps, `Workflow timed out after ${timeoutMs}ms`, "time_limited");
|
|
139
|
+
const finalRun = deps.runs.get(runId);
|
|
140
|
+
return finalRun
|
|
141
|
+
? toResult(finalRun)
|
|
142
|
+
: {
|
|
143
|
+
status: "done",
|
|
144
|
+
reason: "time_limited",
|
|
145
|
+
error: `Workflow timed out after ${timeoutMs}ms`,
|
|
146
|
+
runId,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── runAndWait ───────────────────────────────────────────────
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 同步运行 workflow 至终态(跨扩展编程入口)。
|
|
154
|
+
*
|
|
155
|
+
* 阻塞至 run 到达 done,返回 WorkflowRunResult。用于 pi.__workflowRun 等
|
|
156
|
+
* 编程式调用——非交互场景(交互用 run + lifecycle tools)。
|
|
157
|
+
*
|
|
158
|
+
* **超时处理(C.7)**:timeout → abortRun + 返回 reason=time_limited。
|
|
159
|
+
* 旧代码返回 status:"timeout" 但 workflow 可能仍 running(资源泄漏);
|
|
160
|
+
* 本实现确保 timeout 转 done,time_limited 终态。
|
|
161
|
+
*
|
|
162
|
+
* **signal abort**:signal.aborted → abortRun + 返回 reason=aborted。
|
|
163
|
+
*
|
|
164
|
+
* **脚本未找到**:返回 reason=failed(不抛错——编程调用方据 reason 判断)。
|
|
165
|
+
*
|
|
166
|
+
* @param name workflow 脚本名(registry.get 查找)
|
|
167
|
+
* @param args 调用参数(worker 内 $ARGS 访问)
|
|
168
|
+
* @param deps LauncherDeps(LifecycleDeps + registry)
|
|
169
|
+
* @param signal 外部 abort signal(可选)
|
|
170
|
+
* @param timeoutMs 超时上限(默认 10 分钟)
|
|
171
|
+
* @returns WorkflowRunResult(status 恒 "done")
|
|
172
|
+
*/
|
|
173
|
+
export async function runAndWait(
|
|
174
|
+
name: string,
|
|
175
|
+
args: Record<string, unknown>,
|
|
176
|
+
deps: LauncherDeps,
|
|
177
|
+
signal?: AbortSignal,
|
|
178
|
+
timeoutMs: number = DEFAULT_RUNANDWAIT_TIMEOUT_MS,
|
|
179
|
+
): Promise<WorkflowRunResult> {
|
|
180
|
+
// 1. registry 查找脚本
|
|
181
|
+
const script = await deps.registry.get(name);
|
|
182
|
+
if (!script) {
|
|
183
|
+
return {
|
|
184
|
+
status: "done",
|
|
185
|
+
reason: "failed",
|
|
186
|
+
error: `Workflow '${name}' not found`,
|
|
187
|
+
runId: "",
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 2. lint 校验(失败抛错——脚本本身有问题,不应静默吞)
|
|
192
|
+
const lintResult = script.validate();
|
|
193
|
+
if (!lintResult.valid) {
|
|
194
|
+
const errors = lintResult.findings
|
|
195
|
+
.filter((f) => f.severity === "error")
|
|
196
|
+
.map((f) => `L${f.line}: ${f.message}`)
|
|
197
|
+
.join("; ");
|
|
198
|
+
throw new Error(`Workflow script '${name}' has lint errors: ${errors}`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 3. 构建 RunSpec
|
|
202
|
+
// 不设 budgetTimeMs:runAndWait 自身用轮询 deadline(pollRunToResult 内 while + safeAbort)
|
|
203
|
+
// 实施 timeout,并产出「Workflow timed out after Xms」的具体错误信息。spec 级
|
|
204
|
+
// 时间预算(lifecycle.scheduleTimeBudget)服务于 fire-and-forget 的交互式 run
|
|
205
|
+
// (tool-workflow actionRun),若在此也设会与轮询 deadline 同时触发产生竞态。
|
|
206
|
+
const spec: RunSpec = {
|
|
207
|
+
scriptSource: script.toExecutable(),
|
|
208
|
+
args,
|
|
209
|
+
budgetTokens: undefined,
|
|
210
|
+
scriptName: script.name,
|
|
211
|
+
scriptPath: script.path,
|
|
212
|
+
description: script.meta.description,
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// 4. 启动 workflow + 5. 轮询至 done(含 6. timeout → abortRun,C.7)
|
|
216
|
+
// pending-notification 的 register/unregister 由 runWorkflow(启动注册)+
|
|
217
|
+
// transition("done") 路径(完成注销)统一处理,runAndWait 不再重复 emit。
|
|
218
|
+
const runId = await runWorkflow(spec, deps, signal);
|
|
219
|
+
return pollRunToResult(runId, deps, signal, timeoutMs, "Aborted by signal");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 安全 abort——run 可能已终态(abortRun 对 done no-op,但防御兜底)。
|
|
224
|
+
*/
|
|
225
|
+
async function safeAbort(
|
|
226
|
+
runId: string,
|
|
227
|
+
deps: LauncherDeps,
|
|
228
|
+
reason: string,
|
|
229
|
+
doneReason: DoneReason,
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
try {
|
|
232
|
+
await abortRun(runId, deps, reason, doneReason);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
// run 可能已终态或不存在——忽略,调用方据 toResult 判断
|
|
235
|
+
void err;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ── executeNestedWorkflow(workflow() 嵌套调用实现) ────────
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* workflow() 嵌套调用的 Engine 实现。
|
|
243
|
+
*
|
|
244
|
+
* Worker 脚本内调 workflow(name, args) 时,error-recovery.dispatchWorkflowCall 路由
|
|
245
|
+
* 到 deps.onWorkflowCall,后者(Interface 层 makeDeps 注入)委托本函数。
|
|
246
|
+
*
|
|
247
|
+
* 流程(6 步):
|
|
248
|
+
* 1. 循环检测——name 已在 parentWorkflowChain 中则拒绝(防 A→B→A 死循环)
|
|
249
|
+
* 2. signal 继承——子 run 响应父 run abort(parentController → childController)
|
|
250
|
+
* 3. registry.get + lint——失败返回 error result(不抛错,让脚本 soft-fail)
|
|
251
|
+
* 4. 构建 RunSpec(共享父 Budget 引用 + parentWorkflowChain 延长)+ runWorkflow
|
|
252
|
+
* 5. pollRunToResult 轮询至 done(复用 runAndWait 的轮询逻辑)
|
|
253
|
+
* 6. 结果转换(budget 已通过共享引用实时同步)
|
|
254
|
+
*
|
|
255
|
+
* 不走 runAndWait:runAndWait 内部构建 RunSpec 不支持 parentWorkflowChain 与 budget
|
|
256
|
+
* 共享引用,故直接构建 spec + runWorkflow + pollRunToResult。
|
|
257
|
+
*
|
|
258
|
+
* @param name 子 workflow 脚本名(registry.get 查找)
|
|
259
|
+
* @param args 调用参数(子 worker 内 $ARGS 访问)
|
|
260
|
+
* @param parentRun 发起嵌套调用的父 WorkflowRun(budget 共享 + 循环链源)
|
|
261
|
+
* @param deps LauncherDeps(与 runAndWait 同一组依赖 + registry)
|
|
262
|
+
* @returns { content, parsedOutput?, error? }——dispatchWorkflowCall 原样 postMessage 回 worker
|
|
263
|
+
*/
|
|
264
|
+
export async function executeNestedWorkflow(
|
|
265
|
+
name: string,
|
|
266
|
+
args: Record<string, unknown>,
|
|
267
|
+
parentRun: WorkflowRun,
|
|
268
|
+
deps: LauncherDeps,
|
|
269
|
+
): Promise<{ content: string; parsedOutput?: unknown; error?: string }> {
|
|
270
|
+
// Step 1: 循环检测——parentWorkflowChain 不存在时为 [](顶层 run)
|
|
271
|
+
const chain = [
|
|
272
|
+
...(parentRun.spec.parentWorkflowChain ?? []),
|
|
273
|
+
parentRun.spec.scriptName,
|
|
274
|
+
];
|
|
275
|
+
if (chain.includes(name)) {
|
|
276
|
+
return {
|
|
277
|
+
content: "",
|
|
278
|
+
error: `Circular workflow call detected: ${[...chain, name].join(" → ")}`,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Step 2: signal 继承——子 run 响应父 run abort
|
|
283
|
+
// [L-2] 提取命名 onParentAbort 以便 finally removeEventListener,防子 run 完成后
|
|
284
|
+
// parentSignal 上残留 listener(多次嵌套调用会累积)。
|
|
285
|
+
const childController = new AbortController();
|
|
286
|
+
const parentSignal = parentRun.runtime?.controller.signal;
|
|
287
|
+
const onParentAbort = (): void => childController.abort();
|
|
288
|
+
if (parentSignal) {
|
|
289
|
+
if (parentSignal.aborted) {
|
|
290
|
+
childController.abort();
|
|
291
|
+
} else {
|
|
292
|
+
parentSignal.addEventListener("abort", onParentAbort, { once: true });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Step 3: registry 查找 + lint(失败返回 error result,不抛错)
|
|
297
|
+
const script = await deps.registry.get(name);
|
|
298
|
+
if (!script) {
|
|
299
|
+
return { content: "", error: `Workflow '${name}' not found` };
|
|
300
|
+
}
|
|
301
|
+
const lintResult = script.validate();
|
|
302
|
+
if (!lintResult.valid) {
|
|
303
|
+
const errors = lintResult.findings
|
|
304
|
+
.filter((f) => f.severity === "error")
|
|
305
|
+
.map((f) => `L${f.line}: ${f.message}`)
|
|
306
|
+
.join("; ");
|
|
307
|
+
return {
|
|
308
|
+
content: "",
|
|
309
|
+
error: `Workflow script '${name}' has lint errors: ${errors}`,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Step 4: 构建 RunSpec(共享父 Budget + 循环链)+ 启动子 workflow
|
|
314
|
+
// budget 共享(F-7 方案 B):子 run 直接复用父 Budget 引用(budgetRef),consume 实时
|
|
315
|
+
// 累加到父 Budget,消除并行嵌套下的超支窗口,无需 Step 6 的 sync-back。
|
|
316
|
+
const spec: RunSpec = {
|
|
317
|
+
scriptSource: script.toExecutable(),
|
|
318
|
+
args,
|
|
319
|
+
budgetRef: parentRun.state.budget,
|
|
320
|
+
scriptName: script.name,
|
|
321
|
+
scriptPath: script.path,
|
|
322
|
+
description: script.meta.description,
|
|
323
|
+
parentWorkflowChain: chain,
|
|
324
|
+
};
|
|
325
|
+
const runId = await runWorkflow(spec, deps, childController.signal);
|
|
326
|
+
|
|
327
|
+
// Step 5: 轮询至 done(复用 runAndWait 的轮询逻辑)
|
|
328
|
+
// [H-1] 嵌套 workflow timeout 从父 run 继承:父 spec.budgetTimeMs 存在时取
|
|
329
|
+
// min(父 budget, DEFAULT),让子 run 不超出父 run 的剩余时间预算;否则用 DEFAULT。
|
|
330
|
+
// budgetRef(共享 Budget)已在 Step 4 透传给子 run 处理 token/cost 预算,
|
|
331
|
+
// 此处的 budgetTimeMs 只服务 pollRunToResult 的轮询 deadline(wall-clock 兜底)。
|
|
332
|
+
const nestedTimeoutMs = parentRun.spec.budgetTimeMs
|
|
333
|
+
? Math.min(parentRun.spec.budgetTimeMs, DEFAULT_RUNANDWAIT_TIMEOUT_MS)
|
|
334
|
+
: DEFAULT_RUNANDWAIT_TIMEOUT_MS;
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
const result = await pollRunToResult(
|
|
338
|
+
runId,
|
|
339
|
+
deps,
|
|
340
|
+
childController.signal,
|
|
341
|
+
nestedTimeoutMs,
|
|
342
|
+
"Aborted by parent signal",
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
// Step 6: 结果转换(budget 已通过共享 budgetRef 实时同步,无需 sync-back)
|
|
346
|
+
if (result.reason === "completed") {
|
|
347
|
+
const scriptResult = result.scriptResult;
|
|
348
|
+
return {
|
|
349
|
+
content:
|
|
350
|
+
typeof scriptResult === "string"
|
|
351
|
+
? scriptResult
|
|
352
|
+
: JSON.stringify(scriptResult ?? ""),
|
|
353
|
+
parsedOutput:
|
|
354
|
+
typeof scriptResult === "object" && scriptResult !== null
|
|
355
|
+
? scriptResult
|
|
356
|
+
: undefined,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
content: "",
|
|
361
|
+
error: result.error ?? `Workflow '${name}' ended: ${result.reason}`,
|
|
362
|
+
};
|
|
363
|
+
} finally {
|
|
364
|
+
// [L-2] 子 run done 后移除 parentSignal listener,避免累积({ once: true } 在
|
|
365
|
+
// 正常完成路径下不会自动触发,listener 残留;多次嵌套调用会泄漏到 parentSignal)。
|
|
366
|
+
parentSignal?.removeEventListener("abort", onParentAbort);
|
|
367
|
+
}
|
|
368
|
+
}
|