@xzzpig/pi-subagents 0.1.0 → 0.2.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/CHANGELOG.md +79 -0
- package/README.md +2 -0
- package/agents/reviewer.md +3 -4
- package/docs/agents.md +342 -0
- package/docs/configuration.md +328 -0
- package/docs/extension-api.md +308 -0
- package/docs/missions.md +119 -0
- package/docs/models.md +192 -0
- package/docs/observability.md +174 -0
- package/docs/tool-reference.md +343 -0
- package/docs/watchdog.md +176 -0
- package/docs/workflows.md +163 -0
- package/package.json +13 -11
- package/skills/pi-subagents/references/constraints-and-recipes.md +1 -1
- package/skills/pi-subagents/references/execution-controls.md +7 -7
- package/skills/pi-subagents/references/management-authoring-rpc.md +1 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +1 -1
- package/src/agents/agents.ts +17 -8
- package/src/agents/frontmatter.ts +7 -3
- package/src/agents/skills.ts +2 -9
- package/src/api/project-panes.ts +30 -0
- package/src/extension/config.ts +28 -1
- package/src/extension/fanout-child.ts +5 -4
- package/src/extension/index.ts +75 -30
- package/src/extension/rpc.ts +7 -6
- package/src/extension/schemas.ts +29 -8
- package/src/extension/subagent-guide.ts +39 -0
- package/src/extension/tool-description.ts +30 -12
- package/src/inspectors/herdr/project-panes.ts +459 -63
- package/src/intercom/native-supervisor-channel.ts +102 -4
- package/src/missions/actions.ts +25 -2
- package/src/missions/lifecycle.ts +21 -2
- package/src/missions/store.ts +79 -2
- package/src/missions/types.ts +33 -0
- package/src/missions/workflow-state.ts +19 -13
- package/src/runs/background/active-run-index.ts +42 -0
- package/src/runs/background/async-execution.ts +17 -6
- package/src/runs/background/async-job-tracker.ts +293 -133
- package/src/runs/background/async-resume.ts +19 -3
- package/src/runs/background/async-status.ts +33 -5
- package/src/runs/background/completion-replay.ts +267 -0
- package/src/runs/background/control-channel.ts +95 -54
- package/src/runs/background/result-watcher.ts +113 -10
- package/src/runs/background/resume-guidance.ts +33 -0
- package/src/runs/background/run-status.ts +18 -0
- package/src/runs/background/scheduled-runs.ts +25 -1
- package/src/runs/background/stale-run-reconciler.ts +12 -24
- package/src/runs/background/subagent-runner.ts +69 -11
- package/src/runs/background/subagent-wait.ts +5 -2
- package/src/runs/background/wait-completions.ts +39 -5
- package/src/runs/background/wait-subscriptions.ts +21 -5
- package/src/runs/foreground/async-dismiss-action.ts +85 -0
- package/src/runs/foreground/async-steering-action.ts +7 -8
- package/src/runs/foreground/chain-execution.ts +3 -0
- package/src/runs/foreground/execution.ts +7 -0
- package/src/runs/foreground/foreground-history.ts +137 -0
- package/src/runs/foreground/subagent-executor.ts +466 -61
- package/src/runs/foreground/workflow-foreground-steering.ts +187 -0
- package/src/runs/shared/dynamic-fanout.ts +1 -1
- package/src/runs/shared/model-fallback.ts +8 -4
- package/src/runs/shared/model-scope.ts +12 -2
- package/src/runs/shared/parallel-utils.ts +1 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +94 -9
- package/src/runs/shared/worktree.ts +3 -2
- package/src/shared/artifacts.ts +15 -15
- package/src/shared/display-text.ts +100 -0
- package/src/shared/file-coalescer.ts +9 -0
- package/src/shared/fork-context.ts +13 -0
- package/src/shared/formatters.ts +4 -6
- package/src/shared/prompt-resources.ts +51 -0
- package/src/shared/settings.ts +15 -2
- package/src/shared/types.ts +45 -2
- package/src/shared/utf8.ts +11 -0
- package/src/shared/utils.ts +43 -33
- package/src/slash/prompt-workflows.ts +2 -15
- package/src/slash/slash-commands.ts +22 -2
- package/src/tui/fleet-status.ts +22 -12
- package/src/tui/fleet.ts +135 -25
- package/src/tui/render.ts +150 -33
- package/src/watchdog/change-signature.ts +4 -3
- package/src/workflows/scripted-workflow.ts +210 -13
|
@@ -10,6 +10,9 @@ const { inspect } = require("node:util");
|
|
|
10
10
|
let nextCallId = 0;
|
|
11
11
|
const pending = new Map();
|
|
12
12
|
const runKeyPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
13
|
+
const trackedPromisePatch = Symbol("trackedPromisePatch");
|
|
14
|
+
const trackedPromiseObservations = new WeakMap();
|
|
15
|
+
let suppressRunObservation = 0;
|
|
13
16
|
|
|
14
17
|
function stableRunJson(value) {
|
|
15
18
|
if (Array.isArray(value)) return "[" + value.map(stableRunJson).join(",") + "]";
|
|
@@ -17,12 +20,124 @@ function stableRunJson(value) {
|
|
|
17
20
|
return JSON.stringify(value) ?? "undefined";
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
function isDirectWorkflowScriptPromiseHandlerCall() {
|
|
24
|
+
const stack = new Error().stack;
|
|
25
|
+
if (typeof stack !== "string") return false;
|
|
26
|
+
const frame = stack.split("\n").slice(1).map((line) => line.trim()).find((line) =>
|
|
27
|
+
line &&
|
|
28
|
+
!line.includes("isDirectWorkflowScriptPromiseHandlerCall") &&
|
|
29
|
+
!line.includes("markObserved") &&
|
|
30
|
+
!line.includes("promise.then") &&
|
|
31
|
+
!line.includes("promise.catch") &&
|
|
32
|
+
!line.includes("promise.finally")
|
|
33
|
+
);
|
|
34
|
+
return frame ? frame.includes("workflow-script.js") && !frame.includes("at async ") : false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function mergeObservations(...groups) {
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
const merged = [];
|
|
40
|
+
for (const group of groups) {
|
|
41
|
+
for (const observation of group) {
|
|
42
|
+
if (!observation || typeof observation.callId !== "number" || typeof observation.key !== "string" || seen.has(observation.callId)) continue;
|
|
43
|
+
seen.add(observation.callId);
|
|
44
|
+
merged.push(observation);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return merged;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function trackedObservations(value) {
|
|
51
|
+
return value && (typeof value === "object" || typeof value === "function") ? trackedPromiseObservations.get(value) ?? [] : [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function withSuppressedRunObservation(callback) {
|
|
55
|
+
suppressRunObservation += 1;
|
|
56
|
+
try {
|
|
57
|
+
return callback();
|
|
58
|
+
} finally {
|
|
59
|
+
suppressRunObservation -= 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function trackRunObservation(observations, promise) {
|
|
64
|
+
const merged = mergeObservations(trackedObservations(promise), observations);
|
|
65
|
+
if (merged.length === 0 || !promise || typeof promise.then !== "function") return promise;
|
|
66
|
+
trackedPromiseObservations.set(promise, merged);
|
|
67
|
+
if (promise[trackedPromisePatch]) return promise;
|
|
68
|
+
|
|
69
|
+
const observedCallIds = new Set();
|
|
70
|
+
const markObserved = () => {
|
|
71
|
+
if (suppressRunObservation > 0 || isDirectWorkflowScriptPromiseHandlerCall()) return;
|
|
72
|
+
for (const observation of trackedObservations(promise)) {
|
|
73
|
+
if (observedCallIds.has(observation.callId)) continue;
|
|
74
|
+
observedCallIds.add(observation.callId);
|
|
75
|
+
parentPort.postMessage({ type: "runObserved", callId: observation.callId, key: observation.key });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const originalThen = promise.then.bind(promise);
|
|
79
|
+
const originalCatch = promise.catch.bind(promise);
|
|
80
|
+
const originalFinally = promise.finally.bind(promise);
|
|
81
|
+
Object.defineProperty(promise, trackedPromisePatch, { value: true });
|
|
82
|
+
promise.then = (onFulfilled, onRejected) => {
|
|
83
|
+
markObserved();
|
|
84
|
+
return trackRunObservation(trackedObservations(promise), originalThen(onFulfilled, onRejected));
|
|
85
|
+
};
|
|
86
|
+
promise.catch = (onRejected) => {
|
|
87
|
+
markObserved();
|
|
88
|
+
return trackRunObservation(trackedObservations(promise), originalCatch(onRejected));
|
|
89
|
+
};
|
|
90
|
+
promise.finally = (onFinally) => {
|
|
91
|
+
markObserved();
|
|
92
|
+
return trackRunObservation(trackedObservations(promise), originalFinally(onFinally));
|
|
93
|
+
};
|
|
94
|
+
return promise;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function trackPromiseCombinator(items, createPromise) {
|
|
98
|
+
const values = Array.from(items);
|
|
99
|
+
const observations = mergeObservations(...values.map(trackedObservations));
|
|
100
|
+
const promise = withSuppressedRunObservation(() => createPromise(values));
|
|
101
|
+
return observations.length > 0 ? trackRunObservation(observations, promise) : promise;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const workflowPromise = new Proxy(Promise, {
|
|
105
|
+
construct(target, args) {
|
|
106
|
+
return new target(...args);
|
|
107
|
+
},
|
|
108
|
+
get(target, prop) {
|
|
109
|
+
if (prop === "all") return (items) => trackPromiseCombinator(items, (values) => target.all(values));
|
|
110
|
+
if (prop === "allSettled") return (items) => trackPromiseCombinator(items, (values) => target.allSettled(values));
|
|
111
|
+
if (prop === "race") return (items) => trackPromiseCombinator(items, (values) => target.race(values));
|
|
112
|
+
if (prop === "any") return (items) => trackPromiseCombinator(items, (values) => target.any(values));
|
|
113
|
+
if (prop === "resolve") return (value) => {
|
|
114
|
+
const observations = trackedObservations(value);
|
|
115
|
+
const promise = withSuppressedRunObservation(() => target.resolve(value));
|
|
116
|
+
return observations.length > 0 ? trackRunObservation(observations, promise) : promise;
|
|
117
|
+
};
|
|
118
|
+
const value = target[prop];
|
|
119
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
function hostCall(method, args, observation) {
|
|
124
|
+
const callId = ++nextCallId;
|
|
125
|
+
const promise = new Promise((resolve, reject) => {
|
|
23
126
|
pending.set(callId, { resolve, reject });
|
|
24
127
|
parentPort.postMessage({ type: "call", callId, method, args });
|
|
25
128
|
});
|
|
129
|
+
return observation && typeof observation.key === "string"
|
|
130
|
+
? trackRunObservation([{ key: observation.key, callId }], promise)
|
|
131
|
+
: promise;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function runHostCall(key, params, collectFailure) {
|
|
135
|
+
const callId = ++nextCallId;
|
|
136
|
+
const promise = new Promise((resolve, reject) => {
|
|
137
|
+
pending.set(callId, { resolve, reject });
|
|
138
|
+
parentPort.postMessage({ type: "call", callId, method: "run", args: { key, params, ...(collectFailure ? { collectFailure: true } : {}) } });
|
|
139
|
+
});
|
|
140
|
+
return { key, callId, promise };
|
|
26
141
|
}
|
|
27
142
|
|
|
28
143
|
function formatRef(result) {
|
|
@@ -58,7 +173,7 @@ function validateRunCall(key, params, label, fingerprints) {
|
|
|
58
173
|
const runs = Object.freeze({
|
|
59
174
|
run(key, params) {
|
|
60
175
|
validateRunCall(key, params, "runs.run", runFingerprints);
|
|
61
|
-
return hostCall("run", { key, params });
|
|
176
|
+
return hostCall("run", { key, params }, { key });
|
|
62
177
|
},
|
|
63
178
|
all(items) {
|
|
64
179
|
if (!Array.isArray(items)) throw new Error("runs.all(items) requires an array.");
|
|
@@ -73,7 +188,8 @@ const runs = Object.freeze({
|
|
|
73
188
|
calls.push({ key, params });
|
|
74
189
|
}
|
|
75
190
|
for (const { key, params } of calls) runFingerprints.set(key, stableRunJson(params));
|
|
76
|
-
|
|
191
|
+
const launched = calls.map(({ key, params }) => runHostCall(key, params, true));
|
|
192
|
+
return trackRunObservation(launched.map(({ key, callId }) => ({ key, callId })), Promise.all(launched.map(({ promise }) => promise)));
|
|
77
193
|
},
|
|
78
194
|
status(keyOrRunId) { return hostCall("status", { keyOrRunId }); },
|
|
79
195
|
ref: formatRef,
|
|
@@ -97,6 +213,14 @@ const state = Object.freeze({
|
|
|
97
213
|
},
|
|
98
214
|
});
|
|
99
215
|
|
|
216
|
+
const prompts = Object.freeze({
|
|
217
|
+
render(ref, vars) {
|
|
218
|
+
if (typeof ref !== "string" || !ref.trim()) throw new Error("prompts.render(ref, vars) requires a non-empty ref string.");
|
|
219
|
+
if (vars !== undefined) assertJsonValue(vars, "prompts.render vars");
|
|
220
|
+
return hostCall("prompts.render", { ref, vars });
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
100
224
|
let contextObjectPrototype;
|
|
101
225
|
|
|
102
226
|
const capturedConsole = Object.freeze(Object.fromEntries(
|
|
@@ -105,6 +229,17 @@ const capturedConsole = Object.freeze(Object.fromEntries(
|
|
|
105
229
|
}]),
|
|
106
230
|
));
|
|
107
231
|
|
|
232
|
+
function formatWorkflowScriptSyntaxError(error) {
|
|
233
|
+
const details = error && error.stack ? error.stack : String(error);
|
|
234
|
+
return [
|
|
235
|
+
"workflowScript must be valid JavaScript.",
|
|
236
|
+
"If task text contains Markdown fences or backticks, use an array joined with \"\\n\" or escaped strings instead of a raw backtick template literal.",
|
|
237
|
+
"",
|
|
238
|
+
"Original SyntaxError:",
|
|
239
|
+
details,
|
|
240
|
+
].join("\n");
|
|
241
|
+
}
|
|
242
|
+
|
|
108
243
|
function assertJsonValue(value, path = "emit", seen = new Set()) {
|
|
109
244
|
if (value === null || typeof value === "string" || typeof value === "boolean") return;
|
|
110
245
|
if (typeof value === "number") {
|
|
@@ -128,6 +263,25 @@ function assertJsonValue(value, path = "emit", seen = new Set()) {
|
|
|
128
263
|
seen.delete(value);
|
|
129
264
|
}
|
|
130
265
|
|
|
266
|
+
function isPlainWorkflowObject(value) {
|
|
267
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
268
|
+
const prototype = Object.getPrototypeOf(value);
|
|
269
|
+
return prototype === null || prototype === Object.prototype || prototype === contextObjectPrototype;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function omitUndefinedWorkflowValues(value, seen = new Set()) {
|
|
273
|
+
if (value === null || typeof value !== "object") return value;
|
|
274
|
+
if (seen.has(value)) return value;
|
|
275
|
+
seen.add(value);
|
|
276
|
+
const normalized = Array.isArray(value)
|
|
277
|
+
? value.map((entry) => entry === undefined ? null : omitUndefinedWorkflowValues(entry, seen))
|
|
278
|
+
: isPlainWorkflowObject(value) && Object.getOwnPropertySymbols(value).length === 0
|
|
279
|
+
? Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => entry === undefined ? [] : [[key, omitUndefinedWorkflowValues(entry, seen)]]))
|
|
280
|
+
: value;
|
|
281
|
+
seen.delete(value);
|
|
282
|
+
return normalized;
|
|
283
|
+
}
|
|
284
|
+
|
|
131
285
|
parentPort.on("message", async (message) => {
|
|
132
286
|
if (message.type === "response") {
|
|
133
287
|
const entry = pending.get(message.callId);
|
|
@@ -139,13 +293,20 @@ parentPort.on("message", async (message) => {
|
|
|
139
293
|
}
|
|
140
294
|
if (message.type !== "start") return;
|
|
141
295
|
try {
|
|
142
|
-
const sandbox = { runs, emit(value) { assertJsonValue(value); parentPort.postMessage({ type: "emit", value }); }, console: capturedConsole };
|
|
296
|
+
const sandbox = { runs, prompts, Promise: workflowPromise, emit(value) { assertJsonValue(value); parentPort.postMessage({ type: "emit", value }); }, console: capturedConsole };
|
|
143
297
|
if (message.stateEnabled) sandbox.state = state;
|
|
144
298
|
const context = vm.createContext(sandbox, { codeGeneration: { strings: false, wasm: false } });
|
|
145
299
|
contextObjectPrototype = vm.runInContext("Object.prototype", context);
|
|
146
|
-
|
|
300
|
+
let compiled;
|
|
301
|
+
try {
|
|
302
|
+
compiled = new vm.Script("(async () => {\n" + message.script + "\n})()", { filename: "workflow-script.js" });
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
305
|
+
parentPort.postMessage({ type: "error", error: formatWorkflowScriptSyntaxError(error) });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
147
308
|
const value = await compiled.runInContext(context);
|
|
148
|
-
const persistedValue = value === undefined ? null : value;
|
|
309
|
+
const persistedValue = value === undefined ? null : omitUndefinedWorkflowValues(value);
|
|
149
310
|
assertJsonValue(persistedValue, "return");
|
|
150
311
|
parentPort.postMessage({ type: "complete", value: persistedValue });
|
|
151
312
|
} catch (error) {
|
|
@@ -157,6 +318,8 @@ parentPort.on("message", async (message) => {
|
|
|
157
318
|
export interface WorkflowScriptChildResult {
|
|
158
319
|
key: string;
|
|
159
320
|
ok: boolean;
|
|
321
|
+
/** Canonical child agent name when launch resolution produced one. */
|
|
322
|
+
agent?: string;
|
|
160
323
|
runId?: string;
|
|
161
324
|
output: string;
|
|
162
325
|
error?: string;
|
|
@@ -169,6 +332,8 @@ export interface WorkflowScriptTraceEntry {
|
|
|
169
332
|
operation: "run" | "status";
|
|
170
333
|
key: string;
|
|
171
334
|
state: "started" | "completed" | "failed" | "reused";
|
|
335
|
+
/** Canonical child agent name when resolved launch or result data is available. */
|
|
336
|
+
agent?: string;
|
|
172
337
|
runId?: string;
|
|
173
338
|
durationMs?: number;
|
|
174
339
|
phase?: string;
|
|
@@ -204,6 +369,9 @@ export interface RunWorkflowScriptOptions {
|
|
|
204
369
|
get: (key: string) => unknown | Promise<unknown>;
|
|
205
370
|
set: (key: string, value: unknown) => void | Promise<void>;
|
|
206
371
|
};
|
|
372
|
+
prompts?: {
|
|
373
|
+
render: (ref: string, vars?: unknown) => string | Promise<string>;
|
|
374
|
+
};
|
|
207
375
|
onTrace?: (trace: WorkflowScriptTraceEntry[]) => void;
|
|
208
376
|
onEmit?: (emits: unknown[]) => void;
|
|
209
377
|
}
|
|
@@ -301,10 +469,13 @@ function validateKey(value: unknown, owner = "runs.run"): string {
|
|
|
301
469
|
return value;
|
|
302
470
|
}
|
|
303
471
|
|
|
304
|
-
function workflowStringMetadata(params: Record<string, unknown>): Pick<WorkflowScriptTraceEntry, "phase" | "label"> {
|
|
472
|
+
function workflowStringMetadata(params: Record<string, unknown>): Pick<WorkflowScriptTraceEntry, "phase" | "label" | "agent"> {
|
|
305
473
|
return {
|
|
306
474
|
...(typeof params.phase === "string" && params.phase.trim() ? { phase: params.phase.trim() } : {}),
|
|
307
475
|
...(typeof params.label === "string" && params.label.trim() ? { label: params.label.trim() } : {}),
|
|
476
|
+
// Requested agent name, so a child is identifiable while it runs. Launch
|
|
477
|
+
// resolution overwrites this with the canonical name on the terminal entry.
|
|
478
|
+
...(typeof params.agent === "string" && params.agent.trim() ? { agent: params.agent.trim() } : {}),
|
|
308
479
|
};
|
|
309
480
|
}
|
|
310
481
|
|
|
@@ -318,7 +489,8 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
318
489
|
const trace: WorkflowScriptTraceEntry[] = [];
|
|
319
490
|
const children = new Map<string, WorkflowScriptChildResult>();
|
|
320
491
|
const childOrder: string[] = [];
|
|
321
|
-
const launches = new Map<string, { fingerprint: string; promise: Promise<WorkflowScriptChildResult
|
|
492
|
+
const launches = new Map<string, { fingerprint: string; promise: Promise<WorkflowScriptChildResult>; observed: boolean }>();
|
|
493
|
+
const observedRunCalls = new Set<number>();
|
|
322
494
|
const childController = new AbortController();
|
|
323
495
|
let settled = false;
|
|
324
496
|
|
|
@@ -335,8 +507,13 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
335
507
|
if (timer) clearTimeout(timer);
|
|
336
508
|
options.signal?.removeEventListener("abort", onAbort);
|
|
337
509
|
void worker.terminate();
|
|
338
|
-
|
|
510
|
+
const unobservedKeys = "value" in outcome ? [...launches].filter(([, launch]) => !launch.observed).map(([key]) => key) : [];
|
|
511
|
+
const completionError = unobservedKeys.length > 0
|
|
512
|
+
? new Error(`workflowScript completed with unawaited runs.run launch(es): ${unobservedKeys.map((key) => `'${key}'`).join(", ")}. Await or return each launch.`)
|
|
513
|
+
: undefined;
|
|
514
|
+
childController.abort("error" in outcome ? outcome.error : completionError ?? new Error("Workflow script completed."));
|
|
339
515
|
if ("error" in outcome) reject(new WorkflowScriptError(outcome.error.message, partial()));
|
|
516
|
+
else if (completionError) reject(new WorkflowScriptError(completionError.message, partial()));
|
|
340
517
|
else resolve({ value: outcome.value, ...partial() });
|
|
341
518
|
};
|
|
342
519
|
const onAbort = () => finish({ error: new Error("Workflow script aborted.") });
|
|
@@ -381,6 +558,13 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
381
558
|
return finish({ value: message.value });
|
|
382
559
|
}
|
|
383
560
|
if (message.type === "error") return finish({ error: new Error(typeof message.error === "string" ? message.error : "Workflow script failed.") });
|
|
561
|
+
if (message.type === "runObserved" && typeof message.callId === "number") {
|
|
562
|
+
const key = typeof message.key === "string" ? message.key : undefined;
|
|
563
|
+
const launch = key ? launches.get(key) : undefined;
|
|
564
|
+
if (launch) launch.observed = true;
|
|
565
|
+
else observedRunCalls.add(message.callId);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
384
568
|
if (message.type !== "call" || typeof message.callId !== "number" || typeof message.method !== "string" || !isRecord(message.args)) return;
|
|
385
569
|
|
|
386
570
|
const respond = (promise: Promise<unknown>) => {
|
|
@@ -394,6 +578,17 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
394
578
|
);
|
|
395
579
|
};
|
|
396
580
|
|
|
581
|
+
if (message.method === "prompts.render") {
|
|
582
|
+
if (!options.prompts) return respond(Promise.reject(new Error("Workflow prompt rendering is unavailable.")));
|
|
583
|
+
const ref = message.args.ref;
|
|
584
|
+
const vars = message.args.vars;
|
|
585
|
+
if (typeof ref !== "string" || !ref.trim()) return respond(Promise.reject(new Error("prompts.render(ref, vars) requires a non-empty ref string.")));
|
|
586
|
+
return respond(Promise.resolve().then(() => options.prompts!.render(ref, vars)).then((rendered) => {
|
|
587
|
+
if (typeof rendered !== "string") throw new Error("prompts.render must return task text.");
|
|
588
|
+
return rendered;
|
|
589
|
+
}));
|
|
590
|
+
}
|
|
591
|
+
|
|
397
592
|
if (message.method === "state.get" || message.method === "state.set") {
|
|
398
593
|
if (!options.state) return respond(Promise.reject(new Error("Workflow state is unavailable without a mission.")));
|
|
399
594
|
let key: string;
|
|
@@ -464,6 +659,7 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
464
659
|
return respond(Promise.reject(new Error(`runs.run('${key}') resume requires a non-empty task follow-up.`)));
|
|
465
660
|
}
|
|
466
661
|
const collectFailure = message.args.collectFailure === true;
|
|
662
|
+
const callObserved = observedRunCalls.delete(message.callId);
|
|
467
663
|
const deliver = (promise: Promise<WorkflowScriptChildResult>) => collectFailure
|
|
468
664
|
? promise
|
|
469
665
|
: promise.then((result) => {
|
|
@@ -474,6 +670,7 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
474
670
|
const existing = launches.get(key);
|
|
475
671
|
if (existing) {
|
|
476
672
|
if (existing.fingerprint !== fingerprint) return respond(Promise.reject(new Error(`Duplicate workflow key '${key}' used with incompatible launch params.`)));
|
|
673
|
+
if (callObserved) existing.observed = true;
|
|
477
674
|
trace.push({ operation: "run", key, state: "reused", ...workflowStringMetadata(params) });
|
|
478
675
|
traceChanged();
|
|
479
676
|
return respond(deliver(existing.promise));
|
|
@@ -486,7 +683,7 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
486
683
|
const promise = Promise.resolve().then(() => options.launch(key, { ...params, async: params.async ?? false }, childController.signal)).then((result) => {
|
|
487
684
|
const normalized = !result.ok && !result.error ? { ...result, error: result.output } : result;
|
|
488
685
|
children.set(key, normalized);
|
|
489
|
-
trace.push({ operation: "run", key, state: normalized.ok ? "completed" : "failed", durationMs: Date.now() - startedAt, ...workflowStringMetadata(params), ...(normalized.runId ? { runId: normalized.runId } : {}), ...(!normalized.ok ? { error: normalized.error ?? normalized.output } : {}) });
|
|
686
|
+
trace.push({ operation: "run", key, state: normalized.ok ? "completed" : "failed", durationMs: Date.now() - startedAt, ...workflowStringMetadata(params), ...(normalized.agent ? { agent: normalized.agent } : {}), ...(normalized.runId ? { runId: normalized.runId } : {}), ...(!normalized.ok ? { error: normalized.error ?? normalized.output } : {}) });
|
|
490
687
|
traceChanged();
|
|
491
688
|
return normalized;
|
|
492
689
|
}, (error: unknown) => {
|
|
@@ -497,7 +694,7 @@ export async function runWorkflowScript(options: RunWorkflowScriptOptions): Prom
|
|
|
497
694
|
traceChanged();
|
|
498
695
|
return failure;
|
|
499
696
|
});
|
|
500
|
-
launches.set(key, { fingerprint, promise });
|
|
697
|
+
launches.set(key, { fingerprint, promise, observed: callObserved });
|
|
501
698
|
respond(deliver(promise));
|
|
502
699
|
});
|
|
503
700
|
|