@nones/agentflow-strategy-workflow 0.1.0 → 0.6.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/LICENSE +21 -0
- package/README.md +152 -8
- package/package.json +7 -3
- package/src/expansion.ts +932 -0
- package/src/index.ts +35 -0
- package/src/internal.ts +43 -3
- package/src/strategy.ts +15 -2
- package/src/templates.ts +648 -0
package/src/expansion.ts
ADDED
|
@@ -0,0 +1,932 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { PlanHandlerStep, RunPlan, WaveSpec } from "@nones/agentflow-core";
|
|
3
|
+
import {
|
|
4
|
+
assertJsonValue,
|
|
5
|
+
assertOnlyKeys,
|
|
6
|
+
cloneJson,
|
|
7
|
+
expectEngineId,
|
|
8
|
+
expectId,
|
|
9
|
+
expectOptionalString,
|
|
10
|
+
expectRecord,
|
|
11
|
+
expectString,
|
|
12
|
+
fail,
|
|
13
|
+
isRecord,
|
|
14
|
+
normalizeArtifacts,
|
|
15
|
+
normalizeRunPlan,
|
|
16
|
+
} from "./internal";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Serialized deterministic phase/round workflow expansion.
|
|
20
|
+
*
|
|
21
|
+
* The expanded document is presentation and planning data: it declares
|
|
22
|
+
* phases, rounds, ordered steps, step kinds, handler references, declared
|
|
23
|
+
* dependencies and input/output schema references. It never carries
|
|
24
|
+
* executable source and its digest is a pure function of its own content.
|
|
25
|
+
*/
|
|
26
|
+
export const WORKFLOW_EXPANSION_VERSION = 1;
|
|
27
|
+
|
|
28
|
+
export type WorkflowStepKind = "agent" | "script" | "gate" | "module";
|
|
29
|
+
|
|
30
|
+
export type WorkflowRoundMode = "barrier" | "foreach";
|
|
31
|
+
|
|
32
|
+
export interface WorkflowStepDependency {
|
|
33
|
+
/** Fully qualified step key (`<phase>/<round>/<step>`). */
|
|
34
|
+
step: string;
|
|
35
|
+
/** Omitted means the dependency must succeed. */
|
|
36
|
+
on?: "succeeded" | "failed" | "completed";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface WorkflowStep {
|
|
40
|
+
/** Stable logical key: `<phase>/<round>/<step>`. Not an engine ID. */
|
|
41
|
+
key: string;
|
|
42
|
+
/** Local step id, unique within its round. */
|
|
43
|
+
id: string;
|
|
44
|
+
kind: WorkflowStepKind;
|
|
45
|
+
phase: string;
|
|
46
|
+
round: string;
|
|
47
|
+
/** Wave ID this step maps to in the engine plan. */
|
|
48
|
+
wave: string;
|
|
49
|
+
/**
|
|
50
|
+
* Engine-facing ID. Present for agent steps and identical to the plan's
|
|
51
|
+
* agent ID; absent for script, gate and module steps that the engine does
|
|
52
|
+
* not execute directly yet.
|
|
53
|
+
*/
|
|
54
|
+
agentId?: string;
|
|
55
|
+
/** 1-based position inside the round. */
|
|
56
|
+
order: number;
|
|
57
|
+
/** Stable item key for a foreach-expanded round. */
|
|
58
|
+
itemKey?: string;
|
|
59
|
+
/** Trusted versioned handler name; never executable source. */
|
|
60
|
+
handler?: string;
|
|
61
|
+
handlerVersion?: string;
|
|
62
|
+
dependencies: WorkflowStepDependency[];
|
|
63
|
+
/** Names of declared artifacts produced by this step. */
|
|
64
|
+
outputs: string[];
|
|
65
|
+
/** JSON Schema for the step input; merged at declaration time only. */
|
|
66
|
+
inputSchema?: Record<string, unknown>;
|
|
67
|
+
/** JSON Schema for the step output. */
|
|
68
|
+
outputSchema?: Record<string, unknown>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface WorkflowRound {
|
|
72
|
+
id: string;
|
|
73
|
+
phase: string;
|
|
74
|
+
/** Wave ID in the engine plan. */
|
|
75
|
+
wave: string;
|
|
76
|
+
index: number;
|
|
77
|
+
mode: WorkflowRoundMode;
|
|
78
|
+
steps: WorkflowStep[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface WorkflowPhase {
|
|
82
|
+
id: string;
|
|
83
|
+
index: number;
|
|
84
|
+
rounds: WorkflowRound[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Provenance fields a caller supplies when registering one expanded step. */
|
|
88
|
+
type StepOrigin = Omit<
|
|
89
|
+
WorkflowStepProvenance,
|
|
90
|
+
"step" | "phase" | "round" | "itemKey" | "reverseDependencies"
|
|
91
|
+
>;
|
|
92
|
+
|
|
93
|
+
/** Where an expanded step came from, for provenance and diagnostics. */
|
|
94
|
+
export interface WorkflowStepProvenance {
|
|
95
|
+
step: string;
|
|
96
|
+
phase: string;
|
|
97
|
+
round: string;
|
|
98
|
+
itemKey?: string;
|
|
99
|
+
source: "agent" | "script" | "gate" | "module" | "foreach";
|
|
100
|
+
/** Index of the declaring phase and step, when declared explicitly. */
|
|
101
|
+
declaredPhase?: number;
|
|
102
|
+
declaredStep?: number;
|
|
103
|
+
expandedFrom?: string;
|
|
104
|
+
reverseDependencies: string[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface WorkflowExpansion {
|
|
108
|
+
version: number;
|
|
109
|
+
/** Waves that the expanded phases map to, in execution order. */
|
|
110
|
+
waves: WaveSpec[];
|
|
111
|
+
phases: WorkflowPhase[];
|
|
112
|
+
/** Stable step order across the whole document. */
|
|
113
|
+
steps: WorkflowStep[];
|
|
114
|
+
provenance: WorkflowStepProvenance[];
|
|
115
|
+
artifacts: string[];
|
|
116
|
+
/** SHA-256 over the canonical document (without this field). */
|
|
117
|
+
digest: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const STEP_PARAM_FIELDS = [
|
|
121
|
+
"id",
|
|
122
|
+
"prompt",
|
|
123
|
+
"profile",
|
|
124
|
+
"model",
|
|
125
|
+
"settings",
|
|
126
|
+
"outputs",
|
|
127
|
+
"inputSchema",
|
|
128
|
+
"outputSchema",
|
|
129
|
+
] as const;
|
|
130
|
+
const SCRIPT_STEP_FIELDS = [
|
|
131
|
+
"id",
|
|
132
|
+
"handler",
|
|
133
|
+
"handlerVersion",
|
|
134
|
+
"dependsOn",
|
|
135
|
+
"outputs",
|
|
136
|
+
"inputSchema",
|
|
137
|
+
"outputSchema",
|
|
138
|
+
] as const;
|
|
139
|
+
const GATE_STEP_FIELDS = ["id", "gate", "dependsOn", "outputs"] as const;
|
|
140
|
+
const MODULE_STEP_FIELDS = ["id", "operation", "dependsOn"] as const;
|
|
141
|
+
const ROUND_FIELDS = [
|
|
142
|
+
"id",
|
|
143
|
+
"mode",
|
|
144
|
+
"foreach",
|
|
145
|
+
"itemKey",
|
|
146
|
+
"agents",
|
|
147
|
+
"scripts",
|
|
148
|
+
"gates",
|
|
149
|
+
"modules",
|
|
150
|
+
] as const;
|
|
151
|
+
const PHASE_FIELDS = ["id", "cwd", "rounds"] as const;
|
|
152
|
+
const EXPANSION_INPUT_FIELDS = [
|
|
153
|
+
"phases",
|
|
154
|
+
"profile",
|
|
155
|
+
"artifacts",
|
|
156
|
+
"sealed",
|
|
157
|
+
] as const;
|
|
158
|
+
|
|
159
|
+
function expand(
|
|
160
|
+
value: unknown,
|
|
161
|
+
path = "workflow expansion input",
|
|
162
|
+
): {
|
|
163
|
+
phases: WorkflowPhase[];
|
|
164
|
+
steps: WorkflowStep[];
|
|
165
|
+
provenance: WorkflowStepProvenance[];
|
|
166
|
+
waves: WaveSpec[];
|
|
167
|
+
artifacts: string[];
|
|
168
|
+
profile?: string;
|
|
169
|
+
sealed?: boolean;
|
|
170
|
+
} {
|
|
171
|
+
const record = expectRecord(value, path);
|
|
172
|
+
assertOnlyKeys(record, EXPANSION_INPUT_FIELDS, path);
|
|
173
|
+
|
|
174
|
+
if (record.phases === undefined) {
|
|
175
|
+
fail(`${path}.phases`, "is required");
|
|
176
|
+
}
|
|
177
|
+
if (!Array.isArray(record.phases)) {
|
|
178
|
+
fail(`${path}.phases`, "must be an array");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const phaseIds = new Set<string>();
|
|
182
|
+
const waveIds = new Set<string>();
|
|
183
|
+
const stepKeys = new Set<string>();
|
|
184
|
+
const agentIds = new Set<string>();
|
|
185
|
+
const provenance: WorkflowStepProvenance[] = [];
|
|
186
|
+
const phases: WorkflowPhase[] = [];
|
|
187
|
+
const steps: WorkflowStep[] = [];
|
|
188
|
+
const waves: WaveSpec[] = [];
|
|
189
|
+
|
|
190
|
+
const registerStep = (step: WorkflowStep, origin: StepOrigin): void => {
|
|
191
|
+
if (stepKeys.has(step.key)) {
|
|
192
|
+
fail(`${path}.phases`, `duplicate expanded step "${step.key}"`);
|
|
193
|
+
}
|
|
194
|
+
stepKeys.add(step.key);
|
|
195
|
+
steps.push(step);
|
|
196
|
+
provenance.push({
|
|
197
|
+
step: step.key,
|
|
198
|
+
phase: step.phase,
|
|
199
|
+
round: step.round,
|
|
200
|
+
...(step.itemKey === undefined ? {} : { itemKey: step.itemKey }),
|
|
201
|
+
reverseDependencies: [],
|
|
202
|
+
...origin,
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
record.phases.forEach((phaseValue, phaseIndex) => {
|
|
207
|
+
const phasePath = `${path}.phases[${phaseIndex}]`;
|
|
208
|
+
const phaseRecord = expectRecord(phaseValue, phasePath);
|
|
209
|
+
assertOnlyKeys(phaseRecord, PHASE_FIELDS, phasePath);
|
|
210
|
+
|
|
211
|
+
const id = expectId(phaseRecord.id, `${phasePath}.id`);
|
|
212
|
+
if (phaseIds.has(id)) {
|
|
213
|
+
fail(`${phasePath}.id`, `duplicate phase id "${id}"`);
|
|
214
|
+
}
|
|
215
|
+
phaseIds.add(id);
|
|
216
|
+
|
|
217
|
+
if (!Array.isArray(phaseRecord.rounds)) {
|
|
218
|
+
fail(`${phasePath}.rounds`, "must be an array");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const phase: WorkflowPhase = { id, index: phaseIndex, rounds: [] };
|
|
222
|
+
const roundIds = new Set<string>();
|
|
223
|
+
|
|
224
|
+
phaseRecord.rounds.forEach((roundValue, roundIndex) => {
|
|
225
|
+
const roundPath = `${phasePath}.rounds[${roundIndex}]`;
|
|
226
|
+
const roundRecord = expectRecord(roundValue, roundPath);
|
|
227
|
+
assertOnlyKeys(roundRecord, ROUND_FIELDS, roundPath);
|
|
228
|
+
|
|
229
|
+
const roundId =
|
|
230
|
+
roundRecord.id === undefined
|
|
231
|
+
? `round-${roundIndex + 1}`
|
|
232
|
+
: expectId(roundRecord.id, `${roundPath}.id`);
|
|
233
|
+
if (roundIds.has(roundId)) {
|
|
234
|
+
fail(`${roundPath}.id`, `duplicate round id "${roundId}"`);
|
|
235
|
+
}
|
|
236
|
+
roundIds.add(roundId);
|
|
237
|
+
|
|
238
|
+
const mode: WorkflowRoundMode =
|
|
239
|
+
roundRecord.mode === undefined
|
|
240
|
+
? "barrier"
|
|
241
|
+
: (roundRecord.mode as WorkflowRoundMode);
|
|
242
|
+
if (mode !== "barrier" && mode !== "foreach") {
|
|
243
|
+
fail(`${roundPath}.mode`, 'must be "barrier" or "foreach"');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const waveId = uniqueWaveId(id, roundId, waveIds);
|
|
247
|
+
|
|
248
|
+
const round: WorkflowRound = {
|
|
249
|
+
id: roundId,
|
|
250
|
+
phase: id,
|
|
251
|
+
wave: waveId,
|
|
252
|
+
index: roundIndex,
|
|
253
|
+
mode,
|
|
254
|
+
steps: [],
|
|
255
|
+
};
|
|
256
|
+
const agents = expandRoundSteps({
|
|
257
|
+
roundRecord,
|
|
258
|
+
roundPath,
|
|
259
|
+
rootPath: path,
|
|
260
|
+
roundId,
|
|
261
|
+
waveId,
|
|
262
|
+
phaseId: id,
|
|
263
|
+
mode,
|
|
264
|
+
phaseIndex,
|
|
265
|
+
cwd: expectString(phaseRecord.cwd, `${phasePath}.cwd`),
|
|
266
|
+
agentIds,
|
|
267
|
+
registerStep,
|
|
268
|
+
onRoundStep: (step) => round.steps.push(step),
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
if (agents.length === 0) {
|
|
272
|
+
fail(
|
|
273
|
+
`${roundPath}`,
|
|
274
|
+
"must contain at least one agent step; script, gate and module steps attach to the round wave",
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
phase.rounds.push(round);
|
|
278
|
+
waves.push({ id: waveId, agents });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
phases.push(phase);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const artifacts =
|
|
285
|
+
record.artifacts === undefined
|
|
286
|
+
? []
|
|
287
|
+
: normalizeArtifacts(record.artifacts, `${path}.artifacts`).map(
|
|
288
|
+
(artifact) => artifact.id,
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
if (record.sealed !== undefined && typeof record.sealed !== "boolean") {
|
|
292
|
+
fail(`${path}.sealed`, "must be a boolean");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
validateDependencies(steps, `${path}`);
|
|
296
|
+
|
|
297
|
+
// Final guard: the expansion must convert into a RunPlan the engine accepts.
|
|
298
|
+
const profile = expectOptionalString(record.profile, `${path}.profile`);
|
|
299
|
+
const sealed = record.sealed as boolean | undefined;
|
|
300
|
+
try {
|
|
301
|
+
normalizeRunPlan(
|
|
302
|
+
{
|
|
303
|
+
waves,
|
|
304
|
+
...(profile === undefined ? {} : { profile }),
|
|
305
|
+
...(sealed === undefined ? {} : { sealed }),
|
|
306
|
+
},
|
|
307
|
+
`${path}`,
|
|
308
|
+
);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
fail(`${path}`, (error as Error).message);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
phases,
|
|
315
|
+
steps,
|
|
316
|
+
provenance,
|
|
317
|
+
waves,
|
|
318
|
+
artifacts,
|
|
319
|
+
profile,
|
|
320
|
+
sealed,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Engine-facing ID: a derived `<a>/<b>` (wave) or `<a>@<b>` (foreach agent)
|
|
326
|
+
* string. The engine accepts these separators; logical step keys keep the same
|
|
327
|
+
* text so inspection and plan IDs stay aligned.
|
|
328
|
+
*/
|
|
329
|
+
function engineId(value: string, path: string): string {
|
|
330
|
+
return expectEngineId(value, path);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Strip the document-root prefix (`workflow expansion input`) from a field
|
|
335
|
+
* path so provenance reports input-relative locations like
|
|
336
|
+
* `phases[1].rounds[0].agents[0]`.
|
|
337
|
+
*/
|
|
338
|
+
function withoutPathPrefix(fieldPath: string, rootPath: string): string {
|
|
339
|
+
const prefix = `${rootPath}.`;
|
|
340
|
+
return fieldPath.startsWith(prefix)
|
|
341
|
+
? fieldPath.slice(prefix.length)
|
|
342
|
+
: fieldPath;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function uniqueWaveId(phaseId: string, roundId: string, waveIds: Set<string>) {
|
|
346
|
+
const waveId = engineId(`${phaseId}/${roundId}`, "wave.id");
|
|
347
|
+
if (waveIds.has(waveId)) {
|
|
348
|
+
fail("wave.id", `duplicate wave id "${waveId}"`);
|
|
349
|
+
}
|
|
350
|
+
waveIds.add(waveId);
|
|
351
|
+
return waveId;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function expandRoundSteps(input: {
|
|
355
|
+
roundRecord: Record<string, unknown>;
|
|
356
|
+
roundPath: string;
|
|
357
|
+
rootPath: string;
|
|
358
|
+
roundId: string;
|
|
359
|
+
waveId: string;
|
|
360
|
+
phaseId: string;
|
|
361
|
+
mode: WorkflowRoundMode;
|
|
362
|
+
phaseIndex: number;
|
|
363
|
+
cwd: string;
|
|
364
|
+
agentIds: Set<string>;
|
|
365
|
+
registerStep: (step: WorkflowStep, origin: StepOrigin) => void;
|
|
366
|
+
onRoundStep: (step: WorkflowStep) => void;
|
|
367
|
+
}): WaveSpec["agents"] {
|
|
368
|
+
const { roundRecord, roundPath, roundId, waveId, phaseId, mode } = input;
|
|
369
|
+
const agents: WaveSpec["agents"] = [];
|
|
370
|
+
const localIds = new Set<string>();
|
|
371
|
+
let order = 0;
|
|
372
|
+
|
|
373
|
+
const nextOrder = (): number => {
|
|
374
|
+
order += 1;
|
|
375
|
+
return order;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const key = (stepId: string): string => `${phaseId}/${roundId}/${stepId}`;
|
|
379
|
+
|
|
380
|
+
const checkLocalId = (id: string, stepPath: string): void => {
|
|
381
|
+
if (localIds.has(id)) {
|
|
382
|
+
fail(stepPath, `duplicate step id "${id}" in round ${roundId}`);
|
|
383
|
+
}
|
|
384
|
+
localIds.add(id);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const list = (field: string): unknown[] => {
|
|
388
|
+
const value = roundRecord[field];
|
|
389
|
+
if (value !== undefined && !Array.isArray(value)) {
|
|
390
|
+
fail(`${roundPath}.${field}`, "must be an array");
|
|
391
|
+
}
|
|
392
|
+
return value === undefined ? [] : value;
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// foreach: the same agent template is expanded once per stable item key.
|
|
396
|
+
const items: string[] = [];
|
|
397
|
+
if (roundRecord.foreach !== undefined) {
|
|
398
|
+
if (!Array.isArray(roundRecord.foreach)) {
|
|
399
|
+
fail(`${roundPath}.foreach`, "must be an array of stable item keys");
|
|
400
|
+
}
|
|
401
|
+
if (mode !== "foreach") {
|
|
402
|
+
fail(`${roundPath}.foreach`, 'requires mode "foreach"');
|
|
403
|
+
}
|
|
404
|
+
for (const [index, item] of (roundRecord.foreach as unknown[]).entries()) {
|
|
405
|
+
items.push(expectId(item, `${roundPath}.foreach[${index}]`));
|
|
406
|
+
}
|
|
407
|
+
if (new Set(items).size !== items.length) {
|
|
408
|
+
fail(`${roundPath}.foreach`, "item keys must be unique");
|
|
409
|
+
}
|
|
410
|
+
} else if (mode === "foreach") {
|
|
411
|
+
fail(`${roundPath}.foreach`, "is required when mode is foreach");
|
|
412
|
+
} else if (roundRecord.itemKey !== undefined) {
|
|
413
|
+
fail(`${roundPath}.itemKey`, 'requires mode "foreach"');
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const agentList = list("agents");
|
|
417
|
+
if (mode === "foreach") {
|
|
418
|
+
if (agentList.length !== 1) {
|
|
419
|
+
fail(
|
|
420
|
+
`${roundPath}.agents`,
|
|
421
|
+
"must contain exactly one template in a foreach round",
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
const templatePath = `${roundPath}.agents[0]`;
|
|
425
|
+
const template = expectRecord(agentList[0], templatePath);
|
|
426
|
+
assertOnlyKeys(template, STEP_PARAM_FIELDS, templatePath);
|
|
427
|
+
const templateId = expectId(template.id, `${templatePath}.id`);
|
|
428
|
+
const prompt = expectString(template.prompt, `${templatePath}.prompt`);
|
|
429
|
+
for (const itemKey of items) {
|
|
430
|
+
const id = engineId(`${templateId}@${itemKey}`, `${templatePath}.id`);
|
|
431
|
+
checkLocalId(id, templatePath);
|
|
432
|
+
if (input.agentIds.has(id)) {
|
|
433
|
+
fail(`${templatePath}.id`, `duplicate agent id "${id}"`);
|
|
434
|
+
}
|
|
435
|
+
input.agentIds.add(id);
|
|
436
|
+
const agent: WaveSpec["agents"][number] = {
|
|
437
|
+
id,
|
|
438
|
+
prompt,
|
|
439
|
+
cwd: input.cwd,
|
|
440
|
+
};
|
|
441
|
+
copyAgentParams(template, agent, templatePath);
|
|
442
|
+
agents.push(agent);
|
|
443
|
+
const step: WorkflowStep = {
|
|
444
|
+
key: key(id),
|
|
445
|
+
id,
|
|
446
|
+
kind: "agent",
|
|
447
|
+
phase: phaseId,
|
|
448
|
+
round: roundId,
|
|
449
|
+
wave: waveId,
|
|
450
|
+
order: nextOrder(),
|
|
451
|
+
itemKey,
|
|
452
|
+
dependencies: foreachDependencies(
|
|
453
|
+
items,
|
|
454
|
+
itemKey,
|
|
455
|
+
phaseId,
|
|
456
|
+
roundId,
|
|
457
|
+
templateId,
|
|
458
|
+
),
|
|
459
|
+
outputs: outputsOf(template, templatePath),
|
|
460
|
+
};
|
|
461
|
+
copySchemas(template, step, templatePath);
|
|
462
|
+
input.onRoundStep(step);
|
|
463
|
+
input.registerStep(step, {
|
|
464
|
+
source: "foreach",
|
|
465
|
+
declaredPhase: input.phaseIndex,
|
|
466
|
+
declaredStep: 0,
|
|
467
|
+
expandedFrom: withoutPathPrefix(templatePath, input.rootPath),
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
assertNoOtherStepKinds(roundRecord, roundPath);
|
|
471
|
+
return agents;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
for (const [index, agentValue] of agentList.entries()) {
|
|
475
|
+
const stepPath = `${roundPath}.agents[${index}]`;
|
|
476
|
+
const record = expectRecord(agentValue, stepPath);
|
|
477
|
+
assertOnlyKeys(record, STEP_PARAM_FIELDS, stepPath);
|
|
478
|
+
const id = expectId(record.id, `${stepPath}.id`);
|
|
479
|
+
checkLocalId(id, stepPath);
|
|
480
|
+
if (input.agentIds.has(id)) {
|
|
481
|
+
fail(`${stepPath}.id`, `duplicate agent id "${id}"`);
|
|
482
|
+
}
|
|
483
|
+
input.agentIds.add(id);
|
|
484
|
+
const agent: WaveSpec["agents"][number] = {
|
|
485
|
+
id,
|
|
486
|
+
prompt: expectString(record.prompt, `${stepPath}.prompt`),
|
|
487
|
+
cwd: input.cwd,
|
|
488
|
+
};
|
|
489
|
+
copyAgentParams(record, agent, stepPath);
|
|
490
|
+
agents.push(agent);
|
|
491
|
+
const step: WorkflowStep = {
|
|
492
|
+
key: key(id),
|
|
493
|
+
id,
|
|
494
|
+
kind: "agent",
|
|
495
|
+
phase: phaseId,
|
|
496
|
+
round: roundId,
|
|
497
|
+
wave: waveId,
|
|
498
|
+
order: nextOrder(),
|
|
499
|
+
// Wave barriers sequence rounds; agent steps inside one round have no
|
|
500
|
+
// explicit dependency unless declared by a later script/gate/module.
|
|
501
|
+
dependencies: [],
|
|
502
|
+
outputs: outputsOf(record, stepPath),
|
|
503
|
+
};
|
|
504
|
+
copySchemas(record, step, stepPath);
|
|
505
|
+
input.onRoundStep(step);
|
|
506
|
+
input.registerStep(step, {
|
|
507
|
+
source: "agent",
|
|
508
|
+
declaredPhase: input.phaseIndex,
|
|
509
|
+
declaredStep: index,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
for (const [index, scriptValue] of list("scripts").entries()) {
|
|
514
|
+
const stepPath = `${roundPath}.scripts[${index}]`;
|
|
515
|
+
const record = expectRecord(scriptValue, stepPath);
|
|
516
|
+
assertOnlyKeys(record, SCRIPT_STEP_FIELDS, stepPath);
|
|
517
|
+
const id = expectId(record.id, `${stepPath}.id`);
|
|
518
|
+
checkLocalId(id, stepPath);
|
|
519
|
+
const step: WorkflowStep = {
|
|
520
|
+
key: key(id),
|
|
521
|
+
id,
|
|
522
|
+
kind: "script",
|
|
523
|
+
phase: phaseId,
|
|
524
|
+
round: roundId,
|
|
525
|
+
wave: waveId,
|
|
526
|
+
order: nextOrder(),
|
|
527
|
+
handler: expectId(record.handler, `${stepPath}.handler`),
|
|
528
|
+
dependencies: dependencyList(
|
|
529
|
+
record.dependsOn,
|
|
530
|
+
`${stepPath}.dependsOn`,
|
|
531
|
+
phaseId,
|
|
532
|
+
roundId,
|
|
533
|
+
),
|
|
534
|
+
outputs: outputsOf(record, stepPath),
|
|
535
|
+
};
|
|
536
|
+
const handlerVersion = expectOptionalString(
|
|
537
|
+
record.handlerVersion,
|
|
538
|
+
`${stepPath}.handlerVersion`,
|
|
539
|
+
);
|
|
540
|
+
if (handlerVersion !== undefined) {
|
|
541
|
+
step.handlerVersion = handlerVersion;
|
|
542
|
+
}
|
|
543
|
+
copySchemas(record, step, stepPath);
|
|
544
|
+
input.onRoundStep(step);
|
|
545
|
+
input.registerStep(step, {
|
|
546
|
+
source: "script",
|
|
547
|
+
declaredPhase: input.phaseIndex,
|
|
548
|
+
declaredStep: index,
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
for (const [index, gateValue] of list("gates").entries()) {
|
|
553
|
+
const stepPath = `${roundPath}.gates[${index}]`;
|
|
554
|
+
const record = expectRecord(gateValue, stepPath);
|
|
555
|
+
assertOnlyKeys(record, GATE_STEP_FIELDS, stepPath);
|
|
556
|
+
const id = expectId(record.id, `${stepPath}.id`);
|
|
557
|
+
checkLocalId(id, stepPath);
|
|
558
|
+
const step: WorkflowStep = {
|
|
559
|
+
key: key(id),
|
|
560
|
+
id,
|
|
561
|
+
kind: "gate",
|
|
562
|
+
phase: phaseId,
|
|
563
|
+
round: roundId,
|
|
564
|
+
wave: waveId,
|
|
565
|
+
order: nextOrder(),
|
|
566
|
+
handler: expectId(record.gate, `${stepPath}.gate`),
|
|
567
|
+
dependencies: dependencyList(
|
|
568
|
+
record.dependsOn,
|
|
569
|
+
`${stepPath}.dependsOn`,
|
|
570
|
+
phaseId,
|
|
571
|
+
roundId,
|
|
572
|
+
),
|
|
573
|
+
outputs: outputsOf(record, stepPath),
|
|
574
|
+
};
|
|
575
|
+
input.onRoundStep(step);
|
|
576
|
+
input.registerStep(step, {
|
|
577
|
+
source: "gate",
|
|
578
|
+
declaredPhase: input.phaseIndex,
|
|
579
|
+
declaredStep: index,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
for (const [index, moduleValue] of list("modules").entries()) {
|
|
584
|
+
const stepPath = `${roundPath}.modules[${index}]`;
|
|
585
|
+
const record = expectRecord(moduleValue, stepPath);
|
|
586
|
+
assertOnlyKeys(record, MODULE_STEP_FIELDS, stepPath);
|
|
587
|
+
const id = expectId(record.id, `${stepPath}.id`);
|
|
588
|
+
checkLocalId(id, stepPath);
|
|
589
|
+
const step: WorkflowStep = {
|
|
590
|
+
key: key(id),
|
|
591
|
+
id,
|
|
592
|
+
kind: "module",
|
|
593
|
+
phase: phaseId,
|
|
594
|
+
round: roundId,
|
|
595
|
+
wave: waveId,
|
|
596
|
+
order: nextOrder(),
|
|
597
|
+
handler: expectId(record.operation, `${stepPath}.operation`),
|
|
598
|
+
dependencies: dependencyList(
|
|
599
|
+
record.dependsOn,
|
|
600
|
+
`${stepPath}.dependsOn`,
|
|
601
|
+
phaseId,
|
|
602
|
+
roundId,
|
|
603
|
+
),
|
|
604
|
+
outputs: [],
|
|
605
|
+
};
|
|
606
|
+
input.onRoundStep(step);
|
|
607
|
+
input.registerStep(step, {
|
|
608
|
+
source: "module",
|
|
609
|
+
declaredPhase: input.phaseIndex,
|
|
610
|
+
declaredStep: index,
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return agents;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function assertNoOtherStepKinds(
|
|
618
|
+
roundRecord: Record<string, unknown>,
|
|
619
|
+
roundPath: string,
|
|
620
|
+
): void {
|
|
621
|
+
for (const field of ["scripts", "gates", "modules"] as const) {
|
|
622
|
+
const value = roundRecord[field];
|
|
623
|
+
if (Array.isArray(value) && value.length > 0) {
|
|
624
|
+
fail(`${roundPath}.${field}`, "is not supported inside a foreach round");
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function copyAgentParams(
|
|
630
|
+
source: Record<string, unknown>,
|
|
631
|
+
target: WaveSpec["agents"][number],
|
|
632
|
+
path: string,
|
|
633
|
+
): void {
|
|
634
|
+
const profile = expectOptionalString(source.profile, `${path}.profile`);
|
|
635
|
+
if (profile !== undefined) target.profile = profile;
|
|
636
|
+
const model = expectOptionalString(source.model, `${path}.model`);
|
|
637
|
+
if (model !== undefined) target.model = model;
|
|
638
|
+
if (source.settings !== undefined) {
|
|
639
|
+
target.settings = cloneJson<Record<string, unknown>>(
|
|
640
|
+
expectRecord(source.settings, `${path}.settings`),
|
|
641
|
+
`${path}.settings`,
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function copySchemas(
|
|
647
|
+
source: Record<string, unknown>,
|
|
648
|
+
step: WorkflowStep,
|
|
649
|
+
path: string,
|
|
650
|
+
): void {
|
|
651
|
+
for (const field of ["inputSchema", "outputSchema"] as const) {
|
|
652
|
+
if (source[field] !== undefined) {
|
|
653
|
+
const schema = expectRecord(source[field], `${path}.${field}`);
|
|
654
|
+
assertJsonValue(schema, `${path}.${field}`);
|
|
655
|
+
step[field] = cloneJson<Record<string, unknown>>(
|
|
656
|
+
schema,
|
|
657
|
+
`${path}.${field}`,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function outputsOf(source: Record<string, unknown>, path: string): string[] {
|
|
664
|
+
if (source.outputs === undefined) {
|
|
665
|
+
return [];
|
|
666
|
+
}
|
|
667
|
+
if (!Array.isArray(source.outputs)) {
|
|
668
|
+
fail(`${path}.outputs`, "must be an array of artifact ids");
|
|
669
|
+
}
|
|
670
|
+
return source.outputs.map((value, index) =>
|
|
671
|
+
expectId(value, `${path}.outputs[${index}]`),
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function dependencyList(
|
|
676
|
+
value: unknown,
|
|
677
|
+
path: string,
|
|
678
|
+
phaseId: string,
|
|
679
|
+
roundId: string,
|
|
680
|
+
): WorkflowStepDependency[] {
|
|
681
|
+
if (value === undefined) {
|
|
682
|
+
return [];
|
|
683
|
+
}
|
|
684
|
+
if (!Array.isArray(value)) {
|
|
685
|
+
fail(path, "must be an array of step ids");
|
|
686
|
+
}
|
|
687
|
+
return value.map((entry, index) => {
|
|
688
|
+
const entryPath = `${path}[${index}]`;
|
|
689
|
+
if (typeof entry === "string") {
|
|
690
|
+
return { step: qualifyStepRef(entry, entryPath, phaseId, roundId) };
|
|
691
|
+
}
|
|
692
|
+
const record = expectRecord(entry, entryPath);
|
|
693
|
+
assertOnlyKeys(record, ["step", "on"], entryPath);
|
|
694
|
+
const dependency: WorkflowStepDependency = {
|
|
695
|
+
step: qualifyStepRef(
|
|
696
|
+
expectString(record.step, `${entryPath}.step`),
|
|
697
|
+
`${entryPath}.step`,
|
|
698
|
+
phaseId,
|
|
699
|
+
roundId,
|
|
700
|
+
),
|
|
701
|
+
};
|
|
702
|
+
if (record.on !== undefined) {
|
|
703
|
+
if (
|
|
704
|
+
record.on !== "succeeded" &&
|
|
705
|
+
record.on !== "failed" &&
|
|
706
|
+
record.on !== "completed"
|
|
707
|
+
) {
|
|
708
|
+
fail(`${entryPath}.on`, 'must be "succeeded", "failed" or "completed"');
|
|
709
|
+
}
|
|
710
|
+
dependency.on = record.on;
|
|
711
|
+
}
|
|
712
|
+
return dependency;
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Accepts a local step id (`build`), a round-qualified reference
|
|
718
|
+
* (`phase/round/step`) or a fully qualified step key and returns the step key.
|
|
719
|
+
*/
|
|
720
|
+
function qualifyStepRef(
|
|
721
|
+
value: string,
|
|
722
|
+
path: string,
|
|
723
|
+
phaseId: string,
|
|
724
|
+
roundId: string,
|
|
725
|
+
): string {
|
|
726
|
+
const parts = value.split("/");
|
|
727
|
+
if (parts.length === 1) {
|
|
728
|
+
return `${phaseId}/${roundId}/${value}`;
|
|
729
|
+
}
|
|
730
|
+
if (parts.length === 3) {
|
|
731
|
+
return value;
|
|
732
|
+
}
|
|
733
|
+
return fail(path, `must be "step" or "phase/round/step" (got "${value}")`);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function foreachDependencies(
|
|
737
|
+
items: string[],
|
|
738
|
+
itemKey: string,
|
|
739
|
+
phaseId: string,
|
|
740
|
+
roundId: string,
|
|
741
|
+
templateId: string,
|
|
742
|
+
): WorkflowStepDependency[] {
|
|
743
|
+
const index = items.indexOf(itemKey);
|
|
744
|
+
if (index <= 0) {
|
|
745
|
+
return [];
|
|
746
|
+
}
|
|
747
|
+
return [{ step: `${phaseId}/${roundId}/${templateId}@${items[index - 1]}` }];
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function validateDependencies(steps: WorkflowStep[], path: string): void {
|
|
751
|
+
const keys = new Set(steps.map((step) => step.key));
|
|
752
|
+
for (const step of steps) {
|
|
753
|
+
for (const dependency of step.dependencies) {
|
|
754
|
+
if (!keys.has(dependency.step)) {
|
|
755
|
+
fail(
|
|
756
|
+
`${path}.steps`,
|
|
757
|
+
`step ${step.key} depends on unknown step ${dependency.step}`,
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
if (dependency.step === step.key) {
|
|
761
|
+
fail(`${path}.steps`, `step ${step.key} depends on itself`);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function attachReverseDependencies(
|
|
768
|
+
provenance: WorkflowStepProvenance[],
|
|
769
|
+
steps: WorkflowStep[],
|
|
770
|
+
): void {
|
|
771
|
+
const byStep = new Map(provenance.map((entry) => [entry.step, entry]));
|
|
772
|
+
for (const step of steps) {
|
|
773
|
+
for (const dependency of step.dependencies) {
|
|
774
|
+
byStep.get(dependency.step)?.reverseDependencies.push(step.key);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Canonical JSON: sorted object keys, stable array order, no whitespace.
|
|
781
|
+
* Suitable for digests and equality checks across processes.
|
|
782
|
+
*/
|
|
783
|
+
export function canonicalJson(value: unknown): string {
|
|
784
|
+
return JSON.stringify(canonicalize(value));
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function canonicalize(value: unknown): unknown {
|
|
788
|
+
if (Array.isArray(value)) {
|
|
789
|
+
return value.map(canonicalize);
|
|
790
|
+
}
|
|
791
|
+
if (value !== null && typeof value === "object") {
|
|
792
|
+
const record = value as Record<string, unknown>;
|
|
793
|
+
const result: Record<string, unknown> = {};
|
|
794
|
+
for (const key of Object.keys(record).sort()) {
|
|
795
|
+
if (record[key] !== undefined) {
|
|
796
|
+
result[key] = canonicalize(record[key]);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return result;
|
|
800
|
+
}
|
|
801
|
+
return value;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* SHA-256 over canonical JSON. A top-level `digest` field is omitted so a
|
|
806
|
+
* published document that carries its own digest hashes back to that digest.
|
|
807
|
+
*/
|
|
808
|
+
export function sha256Digest(value: unknown): string {
|
|
809
|
+
let subject = value;
|
|
810
|
+
if (isRecord(value) && "digest" in value) {
|
|
811
|
+
const { digest: _digest, ...rest } = value;
|
|
812
|
+
subject = rest;
|
|
813
|
+
}
|
|
814
|
+
return createHash("sha256").update(canonicalJson(subject)).digest("hex");
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Expand an untrusted phase/round document into a serialized, deterministic
|
|
819
|
+
* expansion. The result is JSON-serializable, maps phases to core waves and
|
|
820
|
+
* carries stable step IDs, dependencies, provenance and a content digest.
|
|
821
|
+
* Calling it twice with equivalent input yields the same digest.
|
|
822
|
+
*/
|
|
823
|
+
export function expandWorkflow(input: unknown): WorkflowExpansion {
|
|
824
|
+
const expanded = expand(input);
|
|
825
|
+
const provenance = expanded.provenance;
|
|
826
|
+
attachReverseDependencies(provenance, expanded.steps);
|
|
827
|
+
|
|
828
|
+
const document = {
|
|
829
|
+
version: WORKFLOW_EXPANSION_VERSION,
|
|
830
|
+
waves: expanded.waves,
|
|
831
|
+
phases: expanded.phases,
|
|
832
|
+
steps: expanded.steps,
|
|
833
|
+
provenance,
|
|
834
|
+
artifacts: expanded.artifacts,
|
|
835
|
+
};
|
|
836
|
+
assertJsonValue(document, "workflow expansion");
|
|
837
|
+
|
|
838
|
+
const expansion: WorkflowExpansion = {
|
|
839
|
+
...document,
|
|
840
|
+
digest: sha256Digest(document),
|
|
841
|
+
};
|
|
842
|
+
return expansion;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Convert an expansion into the existing wave-based `RunPlan`. Only planning
|
|
847
|
+
* data is carried over; provenance stays inspectable outside the engine.
|
|
848
|
+
*/
|
|
849
|
+
export function expansionToRunPlan(
|
|
850
|
+
expansion: WorkflowExpansion,
|
|
851
|
+
options: { profile?: string; sealed?: boolean } = {},
|
|
852
|
+
): RunPlan {
|
|
853
|
+
const record = expectRecord(expansion, "workflow expansion");
|
|
854
|
+
if (record.version !== WORKFLOW_EXPANSION_VERSION) {
|
|
855
|
+
fail("workflow expansion.version", `must be ${WORKFLOW_EXPANSION_VERSION}`);
|
|
856
|
+
}
|
|
857
|
+
const plan: RunPlan = {
|
|
858
|
+
waves: cloneJson<WaveSpec[]>(record.waves, "workflow expansion.waves"),
|
|
859
|
+
};
|
|
860
|
+
const byKey = new Map(expansion.steps.map((step) => [step.key, step]));
|
|
861
|
+
const completed = (step: WorkflowStep): PlanHandlerStep | undefined => {
|
|
862
|
+
if (step.kind !== "script" && step.kind !== "gate") return undefined;
|
|
863
|
+
if (step.handler === undefined)
|
|
864
|
+
fail(step.key, "must have a registered handler reference");
|
|
865
|
+
return {
|
|
866
|
+
id: step.id,
|
|
867
|
+
waveId: step.wave,
|
|
868
|
+
kind: step.kind,
|
|
869
|
+
handler: step.handler,
|
|
870
|
+
...(step.handlerVersion === undefined
|
|
871
|
+
? {}
|
|
872
|
+
: { handlerVersion: step.handlerVersion }),
|
|
873
|
+
dependsOn: step.dependencies.map((ref) => {
|
|
874
|
+
const dependency = byKey.get(ref.step);
|
|
875
|
+
if (!dependency) fail(step.key, `unknown dependency ${ref.step}`);
|
|
876
|
+
return {
|
|
877
|
+
waveId: dependency.wave,
|
|
878
|
+
id: dependency.agentId ?? dependency.id,
|
|
879
|
+
};
|
|
880
|
+
}),
|
|
881
|
+
};
|
|
882
|
+
};
|
|
883
|
+
plan.scripts = expansion.steps.flatMap((step) =>
|
|
884
|
+
step.kind === "script" ? [completed(step)!] : [],
|
|
885
|
+
);
|
|
886
|
+
plan.gates = expansion.steps.flatMap((step) =>
|
|
887
|
+
step.kind === "gate" ? [completed(step)!] : [],
|
|
888
|
+
);
|
|
889
|
+
plan.metadata = {
|
|
890
|
+
version: WORKFLOW_EXPANSION_VERSION,
|
|
891
|
+
phases: expansion.phases.map((phase) => ({
|
|
892
|
+
id: phase.id,
|
|
893
|
+
index: phase.index,
|
|
894
|
+
mode: "wave",
|
|
895
|
+
waveIds: phase.rounds.map((round) => round.wave),
|
|
896
|
+
})),
|
|
897
|
+
rounds: expansion.phases.flatMap((phase) =>
|
|
898
|
+
phase.rounds.map((round) => ({
|
|
899
|
+
phaseId: phase.id,
|
|
900
|
+
round: round.index,
|
|
901
|
+
})),
|
|
902
|
+
),
|
|
903
|
+
steps: expansion.steps
|
|
904
|
+
.filter((step) => step.kind !== "module")
|
|
905
|
+
.map((step) => ({
|
|
906
|
+
id: step.agentId ?? step.id,
|
|
907
|
+
phaseId: step.phase,
|
|
908
|
+
waveId: step.wave,
|
|
909
|
+
kind:
|
|
910
|
+
step.kind === "script" || step.kind === "gate" ? step.kind : "agent",
|
|
911
|
+
})),
|
|
912
|
+
envelope: {
|
|
913
|
+
id: "workflow",
|
|
914
|
+
version: WORKFLOW_EXPANSION_VERSION,
|
|
915
|
+
payload: {
|
|
916
|
+
unsupported: expansion.steps
|
|
917
|
+
.filter(
|
|
918
|
+
(step) =>
|
|
919
|
+
step.kind === "module" ||
|
|
920
|
+
(step.kind === "agent" && step.dependencies.length > 0) ||
|
|
921
|
+
step.dependencies.some(
|
|
922
|
+
(ref) => ref.on !== undefined && ref.on !== "succeeded",
|
|
923
|
+
),
|
|
924
|
+
)
|
|
925
|
+
.map((step) => step.key),
|
|
926
|
+
},
|
|
927
|
+
},
|
|
928
|
+
};
|
|
929
|
+
if (options.profile !== undefined) plan.profile = options.profile;
|
|
930
|
+
if (options.sealed !== undefined) plan.sealed = options.sealed;
|
|
931
|
+
return normalizeRunPlan(plan, "workflow plan");
|
|
932
|
+
}
|