@sanity/workflow-engine 0.2.0 → 0.4.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/dist/_chunks-cjs/schema.cjs +405 -0
- package/dist/_chunks-cjs/schema.cjs.map +1 -0
- package/dist/_chunks-es/schema.js +390 -0
- package/dist/_chunks-es/schema.js.map +1 -0
- package/dist/define.cjs +483 -477
- package/dist/define.cjs.map +1 -1
- package/dist/define.d.cts +7404 -4450
- package/dist/define.d.ts +7404 -4450
- package/dist/define.js +480 -473
- package/dist/define.js.map +1 -1
- package/dist/index.cjs +1911 -1619
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11783 -6206
- package/dist/index.d.ts +11783 -6206
- package/dist/index.js +1908 -1615
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/define.js
CHANGED
|
@@ -1,506 +1,512 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
+
import { GROQ_IDENTIFIER, formatValidationError, issuesFromValibot, AuthoringWorkflowSchema, AuthoringStageSchema, AuthoringTaskSchema, AuthoringActionSchema, AuthoringTransitionSchema, AuthoringStateEntrySchema, AuthoringOpSchema, GuardSchema, EffectSchema } from "./_chunks-es/schema.js";
|
|
3
|
+
function groq(strings, ...values) {
|
|
4
|
+
return strings.reduce((out, part, i) => i === 0 ? part : `${out}${serializeGroqValue(values[i - 1])}${part}`, "");
|
|
5
|
+
}
|
|
6
|
+
function serializeGroqValue(value) {
|
|
7
|
+
const serialized = JSON.stringify(value);
|
|
8
|
+
if (serialized === void 0)
|
|
9
|
+
throw new Error(
|
|
10
|
+
`groq tag cannot serialize ${typeof value} \u2014 interpolate JSON-representable values only`
|
|
11
|
+
);
|
|
12
|
+
return serialized;
|
|
13
|
+
}
|
|
14
|
+
function desugarWorkflow(authoring) {
|
|
15
|
+
const ctx = { issues: [], claimStates: /* @__PURE__ */ new Map(), claimedStates: /* @__PURE__ */ new Set() }, workflowState = desugarStateEntries(authoring.state, ["state"], ctx), workflowLayer = layerOf(workflowState), stages = authoring.stages.map((stage, i) => {
|
|
16
|
+
const path = ["stages", i], stageState = desugarStateEntries(stage.state, [...path, "state"], ctx), stageEnv = {
|
|
17
|
+
layers: [
|
|
18
|
+
{ scope: "stage", entries: layerOf(stageState) },
|
|
19
|
+
{ scope: "workflow", entries: workflowLayer }
|
|
20
|
+
]
|
|
21
|
+
}, tasks = (stage.tasks ?? []).map(
|
|
22
|
+
(task, j) => desugarTask(task, [...path, "tasks", j], stageEnv, ctx)
|
|
23
|
+
), transitions = (stage.transitions ?? []).map(
|
|
24
|
+
(transition, k) => desugarTransition(transition, [...path, "transitions", k], stageEnv, ctx)
|
|
25
|
+
);
|
|
26
|
+
return {
|
|
27
|
+
...stripUndefined({
|
|
28
|
+
name: stage.name,
|
|
29
|
+
title: stage.title,
|
|
30
|
+
description: stage.description,
|
|
31
|
+
guards: stage.guards
|
|
32
|
+
}),
|
|
33
|
+
...stageState ? { state: stageState } : {},
|
|
34
|
+
...tasks.length > 0 ? { tasks } : {},
|
|
35
|
+
...transitions.length > 0 ? { transitions } : {}
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
return checkUnclaimedClaimStates(ctx), { definition: {
|
|
39
|
+
...stripUndefined({
|
|
40
|
+
name: authoring.name,
|
|
41
|
+
version: authoring.version,
|
|
42
|
+
title: authoring.title,
|
|
43
|
+
description: authoring.description,
|
|
44
|
+
initialStage: authoring.initialStage,
|
|
45
|
+
predicates: authoring.predicates
|
|
46
|
+
}),
|
|
47
|
+
...workflowState ? { state: workflowState } : {},
|
|
48
|
+
stages
|
|
49
|
+
}, issues: ctx.issues };
|
|
50
|
+
}
|
|
51
|
+
function desugarStateEntries(entries, path, ctx) {
|
|
52
|
+
return !entries || entries.length === 0 ? entries === void 0 ? void 0 : [] : entries.map((entry, i) => {
|
|
53
|
+
if (entry.type !== "claim") return entry;
|
|
54
|
+
const desugared = {
|
|
55
|
+
...stripUndefined({ name: entry.name, title: entry.title, description: entry.description }),
|
|
56
|
+
type: "value.actor",
|
|
57
|
+
source: { type: "write" }
|
|
58
|
+
};
|
|
59
|
+
return ctx.claimStates.set(desugared, { name: entry.name, path: [...path, i] }), desugared;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function layerOf(entries) {
|
|
63
|
+
return new Map((entries ?? []).map((entry) => [entry.name, entry]));
|
|
64
|
+
}
|
|
65
|
+
function desugarTask(task, path, stageEnv, ctx) {
|
|
66
|
+
const taskState = desugarStateEntries(task.state, [...path, "state"], ctx), env = {
|
|
67
|
+
layers: [{ scope: "task", entries: layerOf(taskState) }, ...stageEnv.layers]
|
|
68
|
+
}, ops = desugarOps(task.ops, [...path, "ops"], env, task.name, ctx), actions = (task.actions ?? []).map(
|
|
69
|
+
(action, a) => desugarAction(action, [...path, "actions", a], env, task.name, ctx)
|
|
70
|
+
);
|
|
71
|
+
return {
|
|
72
|
+
...stripUndefined({
|
|
73
|
+
name: task.name,
|
|
74
|
+
title: task.title,
|
|
75
|
+
description: task.description,
|
|
76
|
+
filter: task.filter,
|
|
77
|
+
completeWhen: task.completeWhen,
|
|
78
|
+
failWhen: task.failWhen,
|
|
79
|
+
effects: task.effects,
|
|
80
|
+
subworkflows: task.subworkflows
|
|
81
|
+
}),
|
|
82
|
+
activation: task.activation ?? "manual",
|
|
83
|
+
...taskState ? { state: taskState } : {},
|
|
84
|
+
...ops ? { ops } : {},
|
|
85
|
+
...actions.length > 0 ? { actions } : {}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function desugarAction(action, path, env, taskName, ctx) {
|
|
89
|
+
if ("type" in action)
|
|
90
|
+
return desugarClaimAction(action, path, env, ctx);
|
|
91
|
+
const filter = composeFilter([rolesCondition(action.roles), action.filter]), ops = desugarOps(action.ops, [...path, "ops"], env, taskName, ctx) ?? [];
|
|
92
|
+
return action.status !== void 0 && ops.push({ type: "status.set", task: taskName, status: action.status }), {
|
|
93
|
+
...stripUndefined({
|
|
94
|
+
name: action.name,
|
|
95
|
+
title: action.title,
|
|
96
|
+
description: action.description,
|
|
97
|
+
params: action.params,
|
|
98
|
+
effects: action.effects
|
|
99
|
+
}),
|
|
100
|
+
...filter ? { filter } : {},
|
|
101
|
+
...ops.length > 0 ? { ops } : {}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function desugarClaimAction(action, path, env, ctx) {
|
|
105
|
+
const ref = typeof action.state == "string" ? { state: action.state } : action.state, resolved = resolveRef(ref, env, [...path, "state"], ctx);
|
|
106
|
+
if (resolved) {
|
|
107
|
+
const entry = entryAt(env, resolved);
|
|
108
|
+
entry && entry.type !== "value.actor" && ctx.issues.push({
|
|
109
|
+
path: [...path, "state"],
|
|
110
|
+
message: `claim action "${action.name}" references "${resolved.state}" of kind "${entry.type}" \u2014 a claim pair needs an actor-valued entry (a "claim" or "value.actor" state declaration)`
|
|
111
|
+
}), checkShadowedClaimTarget(action.name, ref.state, resolved, env, [...path, "state"], ctx), entry && ctx.claimedStates.add(entry);
|
|
112
|
+
}
|
|
113
|
+
const noSteal = `!defined($state.${ref.state})`, filter = composeFilter([noSteal, rolesCondition(action.roles), action.filter]), ops = resolved ? [{ type: "state.set", target: resolved, value: { type: "actor" } }] : [];
|
|
114
|
+
return {
|
|
115
|
+
...stripUndefined({
|
|
116
|
+
name: action.name,
|
|
117
|
+
title: action.title,
|
|
118
|
+
description: action.description,
|
|
119
|
+
params: action.params,
|
|
120
|
+
effects: action.effects
|
|
121
|
+
}),
|
|
122
|
+
filter,
|
|
123
|
+
...ops.length > 0 ? { ops } : {}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function checkShadowedClaimTarget(actionName, state, resolved, env, path, ctx) {
|
|
127
|
+
const nearest = env.layers.find((l) => l.entries.has(state));
|
|
128
|
+
nearest === void 0 || nearest.scope === resolved.scope || ctx.issues.push({
|
|
129
|
+
path,
|
|
130
|
+
message: `claim action "${actionName}" targets "${state}" at scope "${resolved.scope}", but a nearer ${nearest.scope}-scope entry of the same name shadows it in $state \u2014 the no-steal filter would read the shadowing entry while the claim writes the "${resolved.scope}" one. Rename one of the entries or claim the nearer one`
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function checkUnclaimedClaimStates(ctx) {
|
|
134
|
+
for (const [entry, { name, path }] of ctx.claimStates)
|
|
135
|
+
ctx.claimedStates.has(entry) || ctx.issues.push({
|
|
136
|
+
path,
|
|
137
|
+
message: `claim state "${name}" is never referenced by a claim action \u2014 you announced the pattern and wrote half of it. Declare a defineAction({ type: "claim", state: "${name}" }) or use a raw value.actor entry`
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function desugarTransition(transition, path, stageEnv, ctx) {
|
|
141
|
+
const ops = desugarOps(transition.ops, [...path, "ops"], stageEnv, void 0, ctx);
|
|
142
|
+
return {
|
|
143
|
+
...stripUndefined({
|
|
144
|
+
name: transition.name,
|
|
145
|
+
title: transition.title,
|
|
146
|
+
description: transition.description,
|
|
147
|
+
to: transition.to,
|
|
148
|
+
effects: transition.effects
|
|
149
|
+
}),
|
|
150
|
+
filter: transition.filter ?? "$allTasksDone",
|
|
151
|
+
...ops ? { ops } : {}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function desugarOps(ops, path, env, firingTask, ctx) {
|
|
155
|
+
if (ops)
|
|
156
|
+
return ops.map((op, i) => desugarOp(op, [...path, i], env, firingTask, ctx));
|
|
157
|
+
}
|
|
158
|
+
function desugarOp(op, path, env, firingTask, ctx) {
|
|
159
|
+
if (op.type === "status.set") {
|
|
160
|
+
const task = op.task ?? firingTask;
|
|
161
|
+
return task === void 0 && ctx.issues.push({
|
|
162
|
+
path: [...path, "task"],
|
|
163
|
+
message: "status.set outside an action must name its target task"
|
|
164
|
+
}), { type: "status.set", task: task ?? "", status: op.status };
|
|
165
|
+
}
|
|
166
|
+
if (op.type === "audit") return desugarAuditOp(op, path, env, ctx);
|
|
167
|
+
const target = resolveRef(op.target, env, [...path, "target"], ctx) ?? fallbackRef(op.target);
|
|
168
|
+
return { ...op, target };
|
|
169
|
+
}
|
|
170
|
+
const AUDIT_STAMPS = { actor: { type: "actor" }, at: { type: "now" } };
|
|
171
|
+
function desugarAuditOp(op, path, env, ctx) {
|
|
172
|
+
const target = resolveRef(op.target, env, [...path, "target"], ctx) ?? fallbackRef(op.target);
|
|
173
|
+
if (op.value.type !== "object")
|
|
174
|
+
return ctx.issues.push({
|
|
175
|
+
path: [...path, "value"],
|
|
176
|
+
message: `audit value must be an object source carrying the domain fields (got "${op.value.type}")`
|
|
177
|
+
}), { type: "state.append", target, value: op.value };
|
|
178
|
+
const actorField = op.stampFields?.actor ?? "actor", atField = op.stampFields?.at ?? "at", fields = { ...op.value.fields };
|
|
179
|
+
for (const [stamp, source] of [
|
|
180
|
+
[actorField, AUDIT_STAMPS.actor],
|
|
181
|
+
[atField, AUDIT_STAMPS.at]
|
|
182
|
+
]) {
|
|
183
|
+
if (stamp in fields) {
|
|
184
|
+
ctx.issues.push({
|
|
185
|
+
path: [...path, "value", "fields", stamp],
|
|
186
|
+
message: `audit stamp field "${stamp}" collides with an authored domain field \u2014 rename yours or remap the stamp via stampFields`
|
|
187
|
+
});
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
fields[stamp] = source;
|
|
191
|
+
}
|
|
192
|
+
return { type: "state.append", target, value: { type: "object", fields } };
|
|
193
|
+
}
|
|
194
|
+
function resolveRef(ref, env, path, ctx) {
|
|
195
|
+
const layers = ref.scope === void 0 ? env.layers : env.layers.filter((l) => l.scope === ref.scope);
|
|
196
|
+
for (const layer of layers)
|
|
197
|
+
if (layer.entries.has(ref.state)) return { scope: layer.scope, state: ref.state };
|
|
198
|
+
const reachable = env.layers.flatMap((l) => [...l.entries.keys()].map((n) => `${l.scope}:${n}`));
|
|
199
|
+
ctx.issues.push({
|
|
200
|
+
path,
|
|
201
|
+
message: `state reference "${ref.state}"${ref.scope ? ` (scope "${ref.scope}")` : ""} does not resolve to a declared entry. Reachable: ${reachable.join(", ") || "(none)"}`
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
function entryAt(env, ref) {
|
|
205
|
+
return env.layers.find((l) => l.scope === ref.scope)?.entries.get(ref.state);
|
|
206
|
+
}
|
|
207
|
+
function fallbackRef(ref) {
|
|
208
|
+
return { scope: ref.scope ?? "workflow", state: ref.state };
|
|
209
|
+
}
|
|
210
|
+
function rolesCondition(roles) {
|
|
211
|
+
if (!(!roles || roles.length === 0))
|
|
212
|
+
return groq`count($actor.roles[@ in ${roles}]) > 0`;
|
|
213
|
+
}
|
|
214
|
+
function composeFilter(parts) {
|
|
215
|
+
const present = parts.filter((p) => p !== void 0);
|
|
216
|
+
if (present.length !== 0)
|
|
217
|
+
return present.length === 1 ? present[0] : present.map((p) => `(${p})`).join(" && ");
|
|
218
|
+
}
|
|
219
|
+
function stripUndefined(obj) {
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const [k, value] of Object.entries(obj))
|
|
222
|
+
value !== void 0 && (out[k] = value);
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
const RESERVED_CONDITION_VARS = [
|
|
226
|
+
"state",
|
|
227
|
+
"actor",
|
|
228
|
+
"assigned",
|
|
229
|
+
"can",
|
|
230
|
+
"row",
|
|
231
|
+
"now",
|
|
232
|
+
"effects",
|
|
233
|
+
"tasks",
|
|
234
|
+
"subworkflows",
|
|
235
|
+
"self",
|
|
236
|
+
"stage",
|
|
237
|
+
"parent",
|
|
238
|
+
"ancestors",
|
|
239
|
+
"allTasksDone",
|
|
240
|
+
"anyTaskFailed"
|
|
241
|
+
], PREDICATE_CALLER_VARS = ["actor", "assigned", "can"];
|
|
2
242
|
function knownList(ids) {
|
|
3
243
|
return [...ids].map((s) => `"${s}"`).join(", ");
|
|
4
244
|
}
|
|
5
|
-
function
|
|
6
|
-
const
|
|
7
|
-
for (const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
245
|
+
function checkDuplicates(names, what, issues) {
|
|
246
|
+
const seen = /* @__PURE__ */ new Set();
|
|
247
|
+
for (const { name, path } of names)
|
|
248
|
+
seen.has(name) && issues.push({ path, message: `duplicate ${what} "${name}" (already declared earlier)` }), seen.add(name);
|
|
249
|
+
return seen;
|
|
250
|
+
}
|
|
251
|
+
function checkStages(def, issues) {
|
|
252
|
+
const stageNames = checkDuplicates(
|
|
253
|
+
def.stages.map((stage, i) => ({ name: stage.name, path: ["stages", i, "name"] })),
|
|
254
|
+
"stage name",
|
|
255
|
+
issues
|
|
256
|
+
);
|
|
257
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
258
|
+
const taskNames = checkDuplicates(
|
|
259
|
+
(stage.tasks ?? []).map((task, j) => ({
|
|
260
|
+
name: task.name,
|
|
261
|
+
path: ["stages", i, "tasks", j, "name"]
|
|
262
|
+
})),
|
|
263
|
+
`task name in stage "${stage.name}"`,
|
|
264
|
+
issues
|
|
265
|
+
);
|
|
266
|
+
checkDuplicates(
|
|
267
|
+
(stage.transitions ?? []).map((t, k) => ({
|
|
268
|
+
name: t.name,
|
|
269
|
+
path: ["stages", i, "transitions", k, "name"]
|
|
270
|
+
})),
|
|
271
|
+
`transition name in stage "${stage.name}"`,
|
|
272
|
+
issues
|
|
273
|
+
), checkTasks(def, i, taskNames, issues);
|
|
274
|
+
}
|
|
275
|
+
return stageNames;
|
|
276
|
+
}
|
|
277
|
+
function checkTasks(def, i, taskNames, issues) {
|
|
278
|
+
const stage = def.stages[i];
|
|
279
|
+
for (const [j, task] of (stage.tasks ?? []).entries()) {
|
|
280
|
+
const path = ["stages", i, "tasks", j];
|
|
281
|
+
checkDuplicates(
|
|
282
|
+
(task.actions ?? []).map((action, a) => ({
|
|
283
|
+
name: action.name,
|
|
284
|
+
path: [...path, "actions", a, "name"]
|
|
285
|
+
})),
|
|
286
|
+
`action name in task "${task.name}"`,
|
|
287
|
+
issues
|
|
288
|
+
), checkStatusSetTargets(task, taskNames, path, issues);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function checkStatusSetTargets(task, taskNames, path, issues) {
|
|
292
|
+
checkStatusSetOps(task.ops, taskNames, [...path, "ops"], issues);
|
|
293
|
+
for (const [a, action] of (task.actions ?? []).entries())
|
|
294
|
+
checkStatusSetOps(action.ops, taskNames, [...path, "actions", a, "ops"], issues);
|
|
295
|
+
}
|
|
296
|
+
function checkStatusSetOps(ops, taskNames, path, issues) {
|
|
297
|
+
for (const [o, op] of (ops ?? []).entries())
|
|
298
|
+
op.type !== "status.set" || taskNames.has(op.task) || issues.push({
|
|
299
|
+
path: [...path, o, "task"],
|
|
300
|
+
message: `status.set targets task "${op.task}" which is not declared in this stage. Known tasks: ${knownList(taskNames)}`
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
function checkInitialStage(def, stageNames, issues) {
|
|
304
|
+
stageNames.has(def.initialStage) || issues.push({
|
|
305
|
+
path: ["initialStage"],
|
|
306
|
+
message: `initialStage "${def.initialStage}" is not a declared stage. Known stages: ${knownList(stageNames)}`
|
|
29
307
|
});
|
|
30
308
|
}
|
|
31
|
-
function checkTransitionTargets(def,
|
|
309
|
+
function checkTransitionTargets(def, stageNames, issues) {
|
|
32
310
|
for (const [i, stage] of def.stages.entries())
|
|
33
311
|
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
34
|
-
|
|
312
|
+
stageNames.has(t.to) || issues.push({
|
|
35
313
|
path: ["stages", i, "transitions", k, "to"],
|
|
36
|
-
message: `transition target "${t.to}" is not a declared stage. Known stages: ${knownList(
|
|
314
|
+
message: `transition target "${t.to}" is not a declared stage. Known stages: ${knownList(stageNames)}`
|
|
37
315
|
});
|
|
38
316
|
}
|
|
39
|
-
function
|
|
40
|
-
const
|
|
41
|
-
for (const [i, p] of (def.predicates ?? []).entries())
|
|
42
|
-
predicateIds.has(p.id) && issues.push({
|
|
43
|
-
path: ["predicates", i, "id"],
|
|
44
|
-
message: `duplicate predicate id "${p.id}"`
|
|
45
|
-
}), predicateIds.add(p.id);
|
|
46
|
-
return predicateIds;
|
|
47
|
-
}
|
|
48
|
-
function checkFilterRefs(def, predicateIds, issues) {
|
|
49
|
-
const visit = (g, path) => {
|
|
50
|
-
g === void 0 || typeof g == "string" || predicateIds.has(g.ref) || issues.push({
|
|
51
|
-
path: [...path, "ref"],
|
|
52
|
-
message: `Filter references predicate "${g.ref}" which is not declared. Known predicates: ${knownList(predicateIds) || "(none)"}`
|
|
53
|
-
});
|
|
54
|
-
};
|
|
317
|
+
function checkEffectNames(def, issues) {
|
|
318
|
+
const sites = [];
|
|
55
319
|
for (const [i, stage] of def.stages.entries()) {
|
|
56
|
-
visit(stage.completion, ["stages", i, "completion"]);
|
|
57
320
|
for (const [j, task] of (stage.tasks ?? []).entries()) {
|
|
58
|
-
|
|
321
|
+
collectEffects(task.effects, ["stages", i, "tasks", j, "effects"], sites);
|
|
59
322
|
for (const [a, action] of (task.actions ?? []).entries())
|
|
60
|
-
|
|
323
|
+
collectEffects(action.effects, ["stages", i, "tasks", j, "actions", a, "effects"], sites);
|
|
61
324
|
}
|
|
62
325
|
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
63
|
-
|
|
326
|
+
collectEffects(t.effects, ["stages", i, "transitions", k, "effects"], sites);
|
|
64
327
|
}
|
|
328
|
+
checkDuplicates(sites, "effect name (registry key \u2014 unique per definition)", issues);
|
|
65
329
|
}
|
|
66
|
-
function
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
options,
|
|
82
|
-
`Invalid option: expected one of ${options.map((o) => `"${o}"`).join("|")}`
|
|
330
|
+
function collectEffects(effects, path, sites) {
|
|
331
|
+
for (const [e, effect] of (effects ?? []).entries())
|
|
332
|
+
sites.push({ name: effect.name, path: [...path, e, "name"] });
|
|
333
|
+
}
|
|
334
|
+
function checkGuardNames(def, issues) {
|
|
335
|
+
const sites = def.stages.flatMap(
|
|
336
|
+
(stage, i) => (stage.guards ?? []).map((guard, g) => ({
|
|
337
|
+
name: guard.name,
|
|
338
|
+
path: ["stages", i, "guards", g, "name"]
|
|
339
|
+
}))
|
|
340
|
+
);
|
|
341
|
+
checkDuplicates(
|
|
342
|
+
sites,
|
|
343
|
+
"guard name (the guard lake _id derives from it \u2014 unique per definition)",
|
|
344
|
+
issues
|
|
83
345
|
);
|
|
84
346
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
347
|
+
function stateScopes(def) {
|
|
348
|
+
const scopes = [{ entries: def.state, path: ["state"], label: "workflow state" }];
|
|
349
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
350
|
+
scopes.push({
|
|
351
|
+
entries: stage.state,
|
|
352
|
+
path: ["stages", i, "state"],
|
|
353
|
+
label: `stage "${stage.name}" state`
|
|
354
|
+
});
|
|
355
|
+
for (const [j, task] of (stage.tasks ?? []).entries())
|
|
356
|
+
scopes.push({
|
|
357
|
+
entries: task.state,
|
|
358
|
+
path: ["stages", i, "tasks", j, "state"],
|
|
359
|
+
label: `task "${task.name}" state`
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return scopes;
|
|
363
|
+
}
|
|
364
|
+
function checkStateEntryNames(def, issues) {
|
|
365
|
+
for (const { entries, path, label } of stateScopes(def))
|
|
366
|
+
checkDuplicates(
|
|
367
|
+
(entries ?? []).map((entry, n) => ({ name: entry.name, path: [...path, n, "name"] })),
|
|
368
|
+
`state entry name in ${label}`,
|
|
369
|
+
issues
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
function checkPredicates(def, issues) {
|
|
373
|
+
const reserved = new Set(RESERVED_CONDITION_VARS), names = Object.keys(def.predicates ?? {});
|
|
374
|
+
for (const name of names)
|
|
375
|
+
reserved.has(name) && issues.push({
|
|
376
|
+
path: ["predicates", name],
|
|
377
|
+
message: `predicate "${name}" shadows the built-in $${name} \u2014 pick another name`
|
|
378
|
+
});
|
|
379
|
+
for (const [name, groq2] of Object.entries(def.predicates ?? {}))
|
|
380
|
+
checkPredicateBody(name, groq2, names, issues);
|
|
381
|
+
}
|
|
382
|
+
function checkPredicateBody(name, groq2, names, issues) {
|
|
383
|
+
for (const caller of PREDICATE_CALLER_VARS)
|
|
384
|
+
referencesVar(groq2, caller) && issues.push({
|
|
385
|
+
path: ["predicates", name],
|
|
386
|
+
message: `predicate "${name}" reads $${caller} \u2014 predicates are instance-level (pre-evaluated once, without a caller); compose caller gates at the condition site instead`
|
|
387
|
+
});
|
|
388
|
+
for (const other of names)
|
|
389
|
+
other === name || !referencesVar(groq2, other) || issues.push({
|
|
390
|
+
path: ["predicates", name],
|
|
391
|
+
message: `predicate "${name}" references predicate $${other} \u2014 predicates may read built-in vars only (no cross-references; compose at the condition site instead)`
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function referencesVar(groq2, name) {
|
|
395
|
+
return GROQ_IDENTIFIER.test(name) ? new RegExp(`\\$${name}\\b`).test(groq2) : !1;
|
|
396
|
+
}
|
|
397
|
+
function checkCanOutsideActionFilters(def, issues) {
|
|
398
|
+
for (const site of nonActionFilterConditionSites(def))
|
|
399
|
+
referencesVar(site.groq, "can") && issues.push({
|
|
400
|
+
path: site.path,
|
|
401
|
+
message: `${site.label} reads $can \u2014 $can (the caller's grants) is bound only when an action fires, so it may appear in action filters only`
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
function nonActionFilterConditionSites(def) {
|
|
405
|
+
const sites = [];
|
|
406
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
407
|
+
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
408
|
+
sites.push({
|
|
409
|
+
groq: t.filter,
|
|
410
|
+
path: ["stages", i, "transitions", k, "filter"],
|
|
411
|
+
label: `transition "${t.name}" filter`
|
|
412
|
+
}), collectBindingSites(
|
|
413
|
+
t.effects,
|
|
414
|
+
["stages", i, "transitions", k, "effects"],
|
|
415
|
+
`transition "${t.name}"`,
|
|
416
|
+
sites
|
|
417
|
+
);
|
|
418
|
+
for (const [j, task] of (stage.tasks ?? []).entries())
|
|
419
|
+
collectTaskConditionSites(task, ["stages", i, "tasks", j], sites);
|
|
420
|
+
}
|
|
421
|
+
return sites;
|
|
422
|
+
}
|
|
423
|
+
function collectTaskConditionSites(task, path, sites) {
|
|
424
|
+
for (const field of ["filter", "completeWhen", "failWhen"]) {
|
|
425
|
+
const groq2 = task[field];
|
|
426
|
+
groq2 !== void 0 && sites.push({ groq: groq2, path: [...path, field], label: `task "${task.name}".${field}` });
|
|
427
|
+
}
|
|
428
|
+
collectBindingSites(task.effects, [...path, "effects"], `task "${task.name}"`, sites);
|
|
429
|
+
for (const [a, action] of (task.actions ?? []).entries())
|
|
430
|
+
collectBindingSites(
|
|
431
|
+
action.effects,
|
|
432
|
+
[...path, "actions", a, "effects"],
|
|
433
|
+
`action "${action.name}"`,
|
|
434
|
+
sites
|
|
435
|
+
);
|
|
436
|
+
collectSubworkflowSites(task, path, sites);
|
|
437
|
+
}
|
|
438
|
+
function collectSubworkflowSites(task, path, sites) {
|
|
439
|
+
const sub = task.subworkflows;
|
|
440
|
+
if (sub === void 0) return;
|
|
441
|
+
const subPath = [...path, "subworkflows"];
|
|
442
|
+
sites.push({
|
|
443
|
+
groq: sub.forEach,
|
|
444
|
+
path: [...subPath, "forEach"],
|
|
445
|
+
label: `task "${task.name}".subworkflows.forEach`
|
|
446
|
+
});
|
|
447
|
+
for (const [group, record] of [
|
|
448
|
+
["with", sub.with],
|
|
449
|
+
["context", sub.context]
|
|
145
450
|
])
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
})
|
|
173
|
-
]), StateSlotSourceSchema = v.union([
|
|
174
|
-
v.strictObject({ kind: v.literal("init") }),
|
|
175
|
-
v.strictObject({
|
|
176
|
-
kind: v.literal("query"),
|
|
177
|
-
query: NonEmpty,
|
|
178
|
-
resource: v.optional(v.union([v.literal("workflow"), v.literal("subject"), GdrSchema]))
|
|
179
|
-
}),
|
|
180
|
-
v.strictObject({ kind: v.literal("write") }),
|
|
181
|
-
/**
|
|
182
|
-
* Pre-populate the slot at resolution time with a constant value.
|
|
183
|
-
* Useful for stage- or task-scope slots that should start non-empty
|
|
184
|
-
* (e.g. a default headline, a starting tally).
|
|
185
|
-
*/
|
|
186
|
-
v.strictObject({ kind: v.literal("literal"), value: v.unknown() }),
|
|
187
|
-
/**
|
|
188
|
-
* Pre-populate by reading another already-resolved slot. `scope:
|
|
189
|
-
* "workflow"` reads from the workflow-scope `state[]` (resolved at
|
|
190
|
-
* startInstance). `scope: "stage"` reads from an earlier slot in
|
|
191
|
-
* this same stage entry's `state[]` (sequential resolution).
|
|
192
|
-
* Optional `path` selects a nested field (e.g. `path: "id"` on a
|
|
193
|
-
* doc.ref slot).
|
|
194
|
-
*/
|
|
195
|
-
v.strictObject({
|
|
196
|
-
kind: v.literal("stateRead"),
|
|
197
|
-
scope: v.union([v.literal("workflow"), v.literal("stage")]),
|
|
198
|
-
slotId: NonEmpty,
|
|
199
|
-
path: v.optional(v.string())
|
|
200
|
-
})
|
|
201
|
-
]), StateSlotKindSchema = picklist([
|
|
202
|
-
"workflow.state.doc.ref",
|
|
203
|
-
"workflow.state.doc.refs",
|
|
204
|
-
// Release reference. A workflow declares this slot to say "I target a
|
|
205
|
-
// Content Release"; the runtime fills it with a specific release at
|
|
206
|
-
// start time, and the engine auto-derives the instance's read
|
|
207
|
-
// perspective from it.
|
|
208
|
-
"workflow.state.release.ref",
|
|
209
|
-
"workflow.state.query",
|
|
210
|
-
"workflow.state.value.string",
|
|
211
|
-
"workflow.state.value.url",
|
|
212
|
-
"workflow.state.value.number",
|
|
213
|
-
"workflow.state.value.boolean",
|
|
214
|
-
"workflow.state.value.dateTime",
|
|
215
|
-
"workflow.state.value.actor",
|
|
216
|
-
"workflow.state.checklist",
|
|
217
|
-
"workflow.state.notes",
|
|
218
|
-
"workflow.state.assignees"
|
|
219
|
-
]), StateSlotSchema = v.strictObject({
|
|
220
|
-
type: StateSlotKindSchema,
|
|
221
|
-
id: NonEmpty,
|
|
222
|
-
title: v.optional(v.string()),
|
|
223
|
-
description: v.optional(v.string()),
|
|
224
|
-
source: StateSlotSourceSchema
|
|
225
|
-
}), FilterSchema = v.union([
|
|
226
|
-
// Inline GROQ — only reserved params allowed at runtime.
|
|
227
|
-
NonEmpty,
|
|
228
|
-
// Reference to a named predicate, optionally with named args.
|
|
229
|
-
v.strictObject({
|
|
230
|
-
ref: NonEmpty,
|
|
231
|
-
args: v.optional(v.record(v.string(), v.unknown()))
|
|
232
|
-
})
|
|
233
|
-
]), PredicateParamSchema = v.strictObject({
|
|
234
|
-
id: NonEmpty,
|
|
235
|
-
title: v.optional(v.string()),
|
|
236
|
-
description: v.optional(v.string()),
|
|
237
|
-
type: picklist(["string", "number", "boolean", "string[]", "reference"]),
|
|
238
|
-
enum: v.optional(v.array(v.string()))
|
|
239
|
-
}), PredicateSchema = v.strictObject({
|
|
240
|
-
type: v.optional(v.literal("workflow.predicate")),
|
|
241
|
-
id: NonEmpty,
|
|
242
|
-
title: v.optional(v.string()),
|
|
243
|
-
description: v.optional(v.string()),
|
|
244
|
-
groq: NonEmpty,
|
|
245
|
-
params: v.optional(v.array(PredicateParamSchema))
|
|
246
|
-
}), EffectSchema = v.strictObject({
|
|
247
|
-
type: v.optional(v.literal("workflow.effect")),
|
|
248
|
-
id: NonEmpty,
|
|
249
|
-
title: v.optional(v.string()),
|
|
250
|
-
description: v.optional(v.string()),
|
|
251
|
-
// Bindings resolve typed Source values at commit time, producing
|
|
252
|
-
// concrete JSON in the queued effect's `params`. NO GROQ.
|
|
253
|
-
bindings: v.optional(v.record(v.string(), SourceSchema)),
|
|
254
|
-
input: v.optional(v.record(v.string(), v.unknown()))
|
|
255
|
-
}), TerminalTaskStatus = picklist(["done", "skipped", "failed"]), PermissionSchema = picklist(DOCUMENT_VALUE_PERMISSIONS), ActionParamSchema = v.strictObject({
|
|
256
|
-
type: v.optional(v.literal("workflow.action.param")),
|
|
257
|
-
id: NonEmpty,
|
|
258
|
-
title: v.optional(v.string()),
|
|
259
|
-
description: v.optional(v.string()),
|
|
260
|
-
paramType: picklist([
|
|
261
|
-
"string",
|
|
262
|
-
"number",
|
|
263
|
-
"boolean",
|
|
264
|
-
"url",
|
|
265
|
-
"dateTime",
|
|
266
|
-
"actor",
|
|
267
|
-
"doc.ref",
|
|
268
|
-
"doc.refs",
|
|
269
|
-
"json"
|
|
270
|
-
]),
|
|
271
|
-
required: v.optional(v.boolean())
|
|
272
|
-
}), ActionSchema = v.strictObject({
|
|
273
|
-
type: v.optional(v.literal("workflow.action")),
|
|
274
|
-
id: NonEmpty,
|
|
275
|
-
title: v.optional(v.string()),
|
|
276
|
-
description: v.optional(v.string()),
|
|
277
|
-
filter: v.optional(FilterSchema),
|
|
278
|
-
/** Caller-supplied params, validated before ops/effects run. */
|
|
279
|
-
params: v.optional(v.array(ActionParamSchema)),
|
|
280
|
-
/**
|
|
281
|
-
* Status the task flips to when this action fires. Omit to leave
|
|
282
|
-
* the task status unchanged — used by intermediate `claim` /
|
|
283
|
-
* `assign` / partial-update actions where the task should stay
|
|
284
|
-
* active so a later action can fire on it. Declared values must
|
|
285
|
-
* be terminal (`done` / `skipped` / `failed`); the engine never
|
|
286
|
-
* mid-flip back to `active` outside of the `workflow.op.status.set`
|
|
287
|
-
* op explicit path.
|
|
288
|
-
*/
|
|
289
|
-
setStatus: v.optional(TerminalTaskStatus),
|
|
290
|
-
/**
|
|
291
|
-
* Inline state-mutation primitives executed during the action commit.
|
|
292
|
-
* Each op runs against the in-memory mutation (deterministic, no
|
|
293
|
-
* external I/O). Resolved Sources turn into concrete JSON before the
|
|
294
|
-
* op applies. Logged on `history[]` as `workflow.history.opApplied`
|
|
295
|
-
* and surfaced on the `ActionResult.ranOps`.
|
|
296
|
-
*/
|
|
297
|
-
ops: v.optional(v.array(OpSchema)),
|
|
298
|
-
effects: v.optional(v.array(EffectSchema)),
|
|
299
|
-
/**
|
|
300
|
-
* Role gating — OR-match against `Actor.roles[]`. If omitted, no role
|
|
301
|
-
* gate. `"*"` in Actor.roles always matches (bench / all-access).
|
|
302
|
-
*/
|
|
303
|
-
roles: v.optional(v.array(NonEmpty)),
|
|
304
|
-
/**
|
|
305
|
-
* When true, the actor must appear in the task's `assignees[]` (as a
|
|
306
|
-
* user-kind assignee matching by id) for the action to be allowed.
|
|
307
|
-
*/
|
|
308
|
-
requireAssignment: v.optional(v.boolean()),
|
|
309
|
-
/**
|
|
310
|
-
* Which permission on the workflow.instance document the actor must
|
|
311
|
-
* hold (per supplied `grants`). Defaults to "update". Only checked
|
|
312
|
-
* when grants are supplied at evaluate/fireAction time.
|
|
313
|
-
*/
|
|
314
|
-
requiredPermission: v.optional(PermissionSchema)
|
|
315
|
-
}), CompletionPolicySchema = v.union([
|
|
316
|
-
v.strictObject({ kind: v.literal("all") }),
|
|
317
|
-
v.strictObject({ kind: v.literal("any") }),
|
|
318
|
-
v.strictObject({
|
|
319
|
-
kind: v.literal("count"),
|
|
320
|
-
n: PositiveInt
|
|
321
|
-
})
|
|
322
|
-
]), LogicalDefinitionRefSchema = v.strictObject({
|
|
323
|
-
workflowId: NonEmpty,
|
|
324
|
-
version: v.optional(v.union([PositiveInt, v.literal("latest")]))
|
|
325
|
-
}), SpawnForEachSchema = v.strictObject({
|
|
326
|
-
groq: NonEmpty,
|
|
327
|
-
as: v.strictObject({
|
|
328
|
-
initialState: v.optional(
|
|
329
|
-
v.array(
|
|
330
|
-
v.strictObject({
|
|
331
|
-
type: StateSlotKindSchema,
|
|
332
|
-
id: NonEmpty,
|
|
333
|
-
value: SourceSchema
|
|
334
|
-
})
|
|
335
|
-
)
|
|
336
|
-
),
|
|
337
|
-
effectsContext: v.optional(v.record(v.string(), SourceSchema))
|
|
338
|
-
})
|
|
339
|
-
}), SpawnSchema = v.strictObject({
|
|
340
|
-
type: v.optional(v.literal("workflow.spawn")),
|
|
341
|
-
id: v.optional(v.string()),
|
|
342
|
-
title: v.optional(v.string()),
|
|
343
|
-
description: v.optional(v.string()),
|
|
344
|
-
definitionRef: LogicalDefinitionRefSchema,
|
|
345
|
-
forEach: SpawnForEachSchema,
|
|
346
|
-
completionPolicy: v.optional(CompletionPolicySchema)
|
|
347
|
-
}), AssigneeSchema = v.variant("kind", [
|
|
348
|
-
v.strictObject({ kind: v.literal("user"), id: NonEmpty }),
|
|
349
|
-
v.strictObject({ kind: v.literal("role"), role: NonEmpty })
|
|
350
|
-
]), TaskSchema = v.strictObject({
|
|
351
|
-
type: v.optional(v.literal("workflow.task")),
|
|
352
|
-
id: NonEmpty,
|
|
353
|
-
title: v.optional(v.string()),
|
|
354
|
-
description: v.optional(v.string()),
|
|
355
|
-
assignees: v.optional(v.array(AssigneeSchema)),
|
|
356
|
-
filter: v.optional(FilterSchema),
|
|
357
|
-
invokeOn: v.optional(picklist(["stageEnter", "manual"])),
|
|
358
|
-
/**
|
|
359
|
-
* Auto-completion predicate — same shape as `transition.filter`. When
|
|
360
|
-
* truthy at activation or on any subsequent cascade, the engine
|
|
361
|
-
* flips the task to `done` with a `{ kind: "system", id: "engine.completeWhen" }`
|
|
362
|
-
* actor. A full filter, not just a wall-clock deadline.
|
|
363
|
-
*/
|
|
364
|
-
completeWhen: v.optional(FilterSchema),
|
|
365
|
-
/**
|
|
366
|
-
* Auto-failure predicate — symmetric to `completeWhen`. Same shape,
|
|
367
|
-
* same evaluation cadence (activation + every cascade), but flips
|
|
368
|
-
* the task to `failed` (not `done`) with a `{ kind: "system",
|
|
369
|
-
* id: "engine.failWhen" }` actor. When both `completeWhen` and
|
|
370
|
-
* `failWhen` resolve truthy on the same evaluation, `failWhen`
|
|
371
|
-
* wins — failure is the more notable signal and should not be
|
|
372
|
-
* silently swallowed.
|
|
373
|
-
*/
|
|
374
|
-
failWhen: v.optional(FilterSchema),
|
|
375
|
-
effects: v.optional(v.array(EffectSchema)),
|
|
376
|
-
actions: v.optional(v.array(ActionSchema)),
|
|
377
|
-
spawns: v.optional(SpawnSchema),
|
|
378
|
-
contentRefs: v.optional(v.array(v.unknown())),
|
|
379
|
-
/** Task-scoped state slots. Resolved at task activation time. */
|
|
380
|
-
state: v.optional(v.array(StateSlotSchema))
|
|
381
|
-
}), TransitionSchema = v.strictObject({
|
|
382
|
-
type: v.optional(v.literal("workflow.transition")),
|
|
383
|
-
id: NonEmpty,
|
|
384
|
-
title: v.optional(v.string()),
|
|
385
|
-
description: v.optional(v.string()),
|
|
386
|
-
to: NonEmpty,
|
|
387
|
-
on: v.optional(v.string()),
|
|
388
|
-
filter: v.optional(FilterSchema),
|
|
389
|
-
effects: v.optional(v.array(EffectSchema))
|
|
390
|
-
}), GuardActionSchema = picklist(MUTATION_GUARD_ACTIONS), GuardMatchSchema = v.strictObject({
|
|
391
|
-
/** Subject `_type`(s); empty matches any type. */
|
|
392
|
-
types: v.optional(v.array(NonEmpty)),
|
|
393
|
-
/** Target docs as `Source`s resolved at deploy to bare ids + the resource. */
|
|
394
|
-
idRefs: v.optional(v.array(SourceSchema)),
|
|
395
|
-
/** Glob id patterns (bare, resource-local). */
|
|
396
|
-
idPatterns: v.optional(v.array(NonEmpty)),
|
|
397
|
-
actions: v.pipe(
|
|
398
|
-
v.array(GuardActionSchema),
|
|
399
|
-
v.minLength(1, "a guard must match at least one action")
|
|
400
|
-
)
|
|
401
|
-
}), GuardSchema = v.strictObject({
|
|
402
|
-
name: v.optional(v.string()),
|
|
403
|
-
description: v.optional(v.string()),
|
|
404
|
-
match: GuardMatchSchema,
|
|
405
|
-
/**
|
|
406
|
-
* Lake GROQ predicate. Reads `document.before`/`document.after`, `mutation`,
|
|
407
|
-
* `guard`, and `identity()`. Bare ids/fields only — no GDRs. Omitted or
|
|
408
|
-
* empty means UNCONDITIONAL DENY.
|
|
409
|
-
*/
|
|
410
|
-
predicate: v.optional(v.string()),
|
|
411
|
-
/**
|
|
412
|
-
* Projected workflow state the predicate reads as `guard.metadata.*`. Values
|
|
413
|
-
* are `Source`s resolved at deploy and re-synced by the engine. Copied data,
|
|
414
|
-
* never a cross-resource pointer.
|
|
415
|
-
*/
|
|
416
|
-
metadata: v.optional(v.record(NonEmpty, SourceSchema))
|
|
417
|
-
}), StageKindSchema = picklist(["initial", "intermediate", "terminal", "waiting"]), StageSchema = v.strictObject({
|
|
418
|
-
type: v.optional(v.literal("workflow.stage")),
|
|
419
|
-
id: NonEmpty,
|
|
420
|
-
title: v.optional(v.string()),
|
|
421
|
-
description: v.optional(v.string()),
|
|
422
|
-
kind: v.optional(StageKindSchema),
|
|
423
|
-
tasks: v.optional(v.array(TaskSchema)),
|
|
424
|
-
transitions: v.optional(v.array(TransitionSchema)),
|
|
425
|
-
completion: v.optional(FilterSchema),
|
|
426
|
-
onEnter: v.optional(v.array(EffectSchema)),
|
|
427
|
-
onExit: v.optional(v.array(EffectSchema)),
|
|
428
|
-
/**
|
|
429
|
-
* Lake mutation guards active while this stage holds. Each compiles to a
|
|
430
|
-
* `temp.system.guard` doc deployed on stage entry and retracted on exit.
|
|
431
|
-
* Plural because one stage's intent may span resources (one guard each).
|
|
432
|
-
*/
|
|
433
|
-
guards: v.optional(v.array(GuardSchema)),
|
|
434
|
-
contentRefs: v.optional(v.array(v.unknown())),
|
|
435
|
-
/** Stage-scoped state slots. Resolved at stage entry and persisted on
|
|
436
|
-
* the StageEntry. */
|
|
437
|
-
state: v.optional(v.array(StateSlotSchema))
|
|
438
|
-
}), WorkflowDefinitionSchema = v.strictObject({
|
|
439
|
-
workflowId: NonEmpty,
|
|
440
|
-
version: PositiveInt,
|
|
441
|
-
title: NonEmpty,
|
|
442
|
-
description: v.optional(v.string()),
|
|
443
|
-
initialStageId: NonEmpty,
|
|
444
|
-
/**
|
|
445
|
-
* Workflow-level state slots. Persist for the instance lifetime.
|
|
446
|
-
* Every stage's filters/ops can read them via
|
|
447
|
-
* `*[_id == $self][0].state[key == "<key>"][0].value`. Init-sourced
|
|
448
|
-
* slots are supplied at `startInstance` via `initialState`.
|
|
449
|
-
*/
|
|
450
|
-
state: v.optional(v.array(StateSlotSchema)),
|
|
451
|
-
stages: v.pipe(v.array(StageSchema), v.minLength(1, "must declare at least one stage")),
|
|
452
|
-
predicates: v.optional(v.array(PredicateSchema))
|
|
453
|
-
});
|
|
454
|
-
function formatValidationError(label, issues) {
|
|
455
|
-
const lines = issues.map((issue) => ` - ${issue.path.length === 0 ? "(root)" : formatPath(issue.path)}: ${issue.message}`);
|
|
456
|
-
return `${label} failed validation (${issues.length} issue${issues.length === 1 ? "" : "s"}):
|
|
457
|
-
${lines.join(`
|
|
458
|
-
`)}`;
|
|
459
|
-
}
|
|
460
|
-
function issuesFromValibot(issues) {
|
|
461
|
-
return issues.map((issue) => ({
|
|
462
|
-
path: issue.path ? issue.path.map((item) => item.key) : [],
|
|
463
|
-
message: issue.message
|
|
464
|
-
}));
|
|
465
|
-
}
|
|
466
|
-
function formatPath(path) {
|
|
467
|
-
let out = "";
|
|
468
|
-
for (const seg of path)
|
|
469
|
-
typeof seg == "number" ? out += `[${seg}]` : out += out.length === 0 ? String(seg) : `.${String(seg)}`;
|
|
470
|
-
return out;
|
|
451
|
+
for (const [key, groq2] of Object.entries(record ?? {}))
|
|
452
|
+
sites.push({
|
|
453
|
+
groq: groq2,
|
|
454
|
+
path: [...subPath, group, key],
|
|
455
|
+
label: `task "${task.name}".subworkflows.${group} "${key}"`
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
function collectBindingSites(effects, path, label, sites) {
|
|
459
|
+
for (const [e, effect] of (effects ?? []).entries())
|
|
460
|
+
for (const [key, groq2] of Object.entries(effect.bindings ?? {}))
|
|
461
|
+
sites.push({
|
|
462
|
+
groq: groq2,
|
|
463
|
+
path: [...path, e, "bindings", key],
|
|
464
|
+
label: `${label} effect "${effect.name}" binding "${key}"`
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
function checkAssigneesEntries(def, issues) {
|
|
468
|
+
for (const { entries, path } of stateScopes(def))
|
|
469
|
+
(entries ?? []).filter((e) => e.type === "assignees").length <= 1 || issues.push({
|
|
470
|
+
path,
|
|
471
|
+
message: "at most one assignees-kind state entry per scope \u2014 the inbox reads it by kind"
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
function checkWorkflowInvariants(def) {
|
|
475
|
+
const issues = [], stageNames = checkStages(def, issues);
|
|
476
|
+
return checkInitialStage(def, stageNames, issues), checkTransitionTargets(def, stageNames, issues), checkEffectNames(def, issues), checkGuardNames(def, issues), checkStateEntryNames(def, issues), checkPredicates(def, issues), checkCanOutsideActionFilters(def, issues), checkAssigneesEntries(def, issues), issues;
|
|
471
477
|
}
|
|
472
478
|
function defineWorkflow(definition) {
|
|
473
|
-
const
|
|
479
|
+
const label = labelFor("defineWorkflow", definition), parsed = parseOrThrow(AuthoringWorkflowSchema, definition, label), { definition: stored, issues: desugarIssues } = desugarWorkflow(parsed), issues = [...desugarIssues, ...checkWorkflowInvariants(stored)];
|
|
474
480
|
if (issues.length > 0) throw new Error(formatValidationError(label, issues));
|
|
475
|
-
return
|
|
481
|
+
return stored;
|
|
476
482
|
}
|
|
477
483
|
function defineStage(stage) {
|
|
478
|
-
return parseOrThrow(
|
|
484
|
+
return parseOrThrow(AuthoringStageSchema, stage, labelFor("defineStage", stage));
|
|
479
485
|
}
|
|
480
486
|
function defineTask(task) {
|
|
481
|
-
return parseOrThrow(
|
|
487
|
+
return parseOrThrow(AuthoringTaskSchema, task, labelFor("defineTask", task));
|
|
482
488
|
}
|
|
483
489
|
function defineAction(action) {
|
|
484
|
-
return parseOrThrow(
|
|
490
|
+
return parseOrThrow(AuthoringActionSchema, action, labelFor("defineAction", action));
|
|
485
491
|
}
|
|
486
492
|
function defineTransition(transition) {
|
|
487
|
-
return parseOrThrow(
|
|
488
|
-
TransitionSchema,
|
|
489
|
-
transition,
|
|
490
|
-
typeof transition.id == "string" ? `defineTransition("${transition.id}")` : typeof transition.to == "string" ? `defineTransition(\u2192 "${transition.to}")` : "defineTransition"
|
|
491
|
-
);
|
|
493
|
+
return parseOrThrow(AuthoringTransitionSchema, transition, transitionLabel(transition));
|
|
492
494
|
}
|
|
493
|
-
function
|
|
494
|
-
return parseOrThrow(
|
|
495
|
+
function defineState(entry) {
|
|
496
|
+
return parseOrThrow(AuthoringStateEntrySchema, entry, labelFor("defineState", entry));
|
|
495
497
|
}
|
|
496
|
-
function
|
|
497
|
-
return parseOrThrow(
|
|
498
|
+
function defineOp(op) {
|
|
499
|
+
return parseOrThrow(AuthoringOpSchema, op, "defineOp");
|
|
498
500
|
}
|
|
499
|
-
function
|
|
500
|
-
return parseOrThrow(
|
|
501
|
+
function defineGuard(guard) {
|
|
502
|
+
return parseOrThrow(GuardSchema, guard, labelFor("defineGuard", guard));
|
|
501
503
|
}
|
|
502
504
|
function defineEffect(effect) {
|
|
503
|
-
return parseOrThrow(EffectSchema, effect, labelFor("defineEffect", effect
|
|
505
|
+
return parseOrThrow(EffectSchema, effect, labelFor("defineEffect", effect));
|
|
506
|
+
}
|
|
507
|
+
function transitionLabel(transition) {
|
|
508
|
+
const { name, to } = transition;
|
|
509
|
+
return typeof name == "string" ? `defineTransition("${name}")` : typeof to == "string" ? `defineTransition(\u2192 "${to}")` : "defineTransition";
|
|
504
510
|
}
|
|
505
511
|
function defineEffectDescriptor(descriptor) {
|
|
506
512
|
if (!descriptor.name) throw new Error("EffectDescriptor missing name");
|
|
@@ -512,23 +518,24 @@ function parseOrThrow(schema, input, label) {
|
|
|
512
518
|
throw new Error(formatValidationError(label, issuesFromValibot(result.issues)));
|
|
513
519
|
return result.output;
|
|
514
520
|
}
|
|
515
|
-
function labelFor(fn, value
|
|
516
|
-
if (value && typeof value == "object" &&
|
|
517
|
-
const raw = value
|
|
521
|
+
function labelFor(fn, value) {
|
|
522
|
+
if (value && typeof value == "object" && "name" in value) {
|
|
523
|
+
const raw = value.name;
|
|
518
524
|
if (typeof raw == "string") return `${fn}("${raw}")`;
|
|
519
525
|
}
|
|
520
526
|
return fn;
|
|
521
527
|
}
|
|
522
528
|
export {
|
|
523
529
|
defineAction,
|
|
524
|
-
defineAssignee,
|
|
525
530
|
defineEffect,
|
|
526
531
|
defineEffectDescriptor,
|
|
527
|
-
|
|
528
|
-
|
|
532
|
+
defineGuard,
|
|
533
|
+
defineOp,
|
|
529
534
|
defineStage,
|
|
535
|
+
defineState,
|
|
530
536
|
defineTask,
|
|
531
537
|
defineTransition,
|
|
532
|
-
defineWorkflow
|
|
538
|
+
defineWorkflow,
|
|
539
|
+
groq
|
|
533
540
|
};
|
|
534
541
|
//# sourceMappingURL=define.js.map
|