@protobase/workflows 0.76.3 → 0.77.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/definition/graph.js +14 -1
- package/dist/definition/schema.d.ts +3 -0
- package/dist/definition/schema.js +6 -1
- package/dist/definition/types.d.ts +4 -2
- package/dist/definition/visible.js +1 -13
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -2
- package/dist/limits.d.ts +1 -1
- package/dist/limits.js +3 -3
- package/dist/run/port.d.ts +79 -23
- package/dist/run/position.js +79 -6
- package/dist/run/replay-match.js +8 -12
- package/dist/run/replay-of.d.ts +3 -3
- package/dist/run/replay-of.js +6 -18
- package/dist/run/replay.js +4 -4
- package/dist/run/rerun-from.d.ts +21 -0
- package/dist/run/rerun-from.js +51 -0
- package/dist/run/rerun.d.ts +14 -23
- package/dist/run/rerun.js +31 -72
- package/dist/run/result-states.js +28 -11
- package/dist/run/runner.d.ts +14 -6
- package/dist/run/runner.js +42 -21
- package/dist/run/scope.js +37 -15
- package/dist/run/seeded-random.js +49 -0
- package/dist/run/segment.d.ts +18 -11
- package/dist/run/segment.js +41 -26
- package/dist/run/step-functions.js +40 -0
- package/dist/run/step-input.js +12 -5
- package/package.json +2 -2
package/dist/run/segment.js
CHANGED
|
@@ -5,25 +5,31 @@ import { replayOf } from "./replay-of.js";
|
|
|
5
5
|
import { runScope } from "./scope.js";
|
|
6
6
|
//#region src/run/segment.ts
|
|
7
7
|
/**
|
|
8
|
-
* Runs a run's steps from where its
|
|
9
|
-
* (a Wait, a retry's delay), or yields once its time is up
|
|
8
|
+
* Runs a run's steps from where its checkpoints leave it to its next wait: it ends or fails, waits until a time
|
|
9
|
+
* (a Wait, a retry's delay), or yields once its time is up, saving its checkpoints on the way and as it pauses.
|
|
10
|
+
* `superseded` when another segment saved a newer checkpoint first.
|
|
10
11
|
*/
|
|
11
|
-
const runSegment = async ({ runId, definition, input, startedAt, secrets, redactor, saved, save, tasks, cache, replayed, signal, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (steps) steps.push(step);
|
|
16
|
-
else byScope.set(step.scope, [step]);
|
|
17
|
-
}
|
|
12
|
+
const runSegment = async ({ runId, definition, input, startedAt, secrets, redactor, saved, save, keep, tasks, cache, replayed, signal, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
13
|
+
const latest = new Map(saved.map((checkpoint) => [checkpoint.scope, checkpoint]));
|
|
14
|
+
const after = new Map(saved.map((checkpoint) => [checkpoint.scope, checkpoint.seq]));
|
|
15
|
+
const steps = [];
|
|
18
16
|
let superseded = false;
|
|
19
|
-
|
|
20
|
-
let kept = [];
|
|
17
|
+
let kept = /* @__PURE__ */ new Map();
|
|
21
18
|
const write = async () => {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
const checkpoints = [...kept.values()].map((checkpoint) => ({
|
|
20
|
+
...checkpoint,
|
|
21
|
+
after: after.get(checkpoint.scope) ?? -1
|
|
22
|
+
}));
|
|
23
|
+
kept = /* @__PURE__ */ new Map();
|
|
24
|
+
if (checkpoints.length === 0) return;
|
|
25
|
+
const ran = steps.splice(0);
|
|
26
|
+
if (ran.length > 0) await keep(ran);
|
|
27
|
+
if (await save(checkpoints)) {
|
|
28
|
+
for (const checkpoint of checkpoints) after.set(checkpoint.scope, checkpoint.seq);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
25
31
|
superseded = true;
|
|
26
|
-
throw new SupersededError(`
|
|
32
|
+
throw new SupersededError(`A checkpoint of scope "${checkpoints[0].scope}" was saved by another segment`);
|
|
27
33
|
};
|
|
28
34
|
const context = {
|
|
29
35
|
runId,
|
|
@@ -40,27 +46,36 @@ const runSegment = async ({ runId, definition, input, startedAt, secrets, redact
|
|
|
40
46
|
store: cache,
|
|
41
47
|
nameSecrets: secretNamer(secrets)
|
|
42
48
|
} },
|
|
43
|
-
...
|
|
49
|
+
...replayed && { replay: replayOf(replayed) },
|
|
44
50
|
signal,
|
|
45
51
|
now,
|
|
46
52
|
deadline: Date.now() + budgetMs,
|
|
47
|
-
saved:
|
|
48
|
-
|
|
49
|
-
kept.
|
|
50
|
-
|
|
51
|
-
if (checkpoint) await write();
|
|
53
|
+
saved: latest,
|
|
54
|
+
checkpoint: async (checkpoint, save) => {
|
|
55
|
+
kept.set(checkpoint.scope, checkpoint);
|
|
56
|
+
if (save) await write();
|
|
52
57
|
},
|
|
58
|
+
trace: (step) => void steps.push(step),
|
|
53
59
|
superseded: () => superseded,
|
|
54
|
-
steps: { count: saved.
|
|
60
|
+
steps: { count: saved.reduce((count, checkpoint) => count + checkpoint.seq + 1, 0) },
|
|
55
61
|
maxSteps
|
|
56
62
|
};
|
|
57
63
|
try {
|
|
58
|
-
const outcome = await runScope(context, definition.stateMachine,
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
const outcome = await runScope(context, definition.stateMachine, {
|
|
65
|
+
scope: "",
|
|
66
|
+
logical: ""
|
|
67
|
+
}, input, {}, startedAt);
|
|
68
|
+
if (outcome.kind === "blocked" || outcome.kind === "yielded") await write();
|
|
69
|
+
return {
|
|
70
|
+
...outcome,
|
|
71
|
+
steps
|
|
72
|
+
};
|
|
61
73
|
} catch (error) {
|
|
62
74
|
if (!(error instanceof SupersededError)) throw error;
|
|
63
|
-
return {
|
|
75
|
+
return {
|
|
76
|
+
kind: "superseded",
|
|
77
|
+
steps
|
|
78
|
+
};
|
|
64
79
|
}
|
|
65
80
|
};
|
|
66
81
|
//#endregion
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { seededRandom } from "./seeded-random.js";
|
|
2
|
+
import jsonata from "jsonata";
|
|
3
|
+
//#region src/run/step-functions.ts
|
|
4
|
+
const format = jsonata("$fromMillis($millis, $picture, $timezone)");
|
|
5
|
+
/** The names `stepFunctions` binds, which a scope does not pass on to a Map's items as variables. */
|
|
6
|
+
const stepFunctionNames = [
|
|
7
|
+
"now",
|
|
8
|
+
"millis",
|
|
9
|
+
"random",
|
|
10
|
+
"shuffle"
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* JSONata's functions that would read the time or chance, made to give the same value each time a step runs:
|
|
14
|
+
* `$now()` and `$millis()` give the scope's clock, and `$random()` and `$shuffle()` draw from a generator seeded by
|
|
15
|
+
* the step's place (`key`: the run, the scope, the step's number and its try), so Map items running at once each get
|
|
16
|
+
* their own numbers, and a step run again after a crash gets the same.
|
|
17
|
+
*/
|
|
18
|
+
const stepFunctions = (clock, key) => {
|
|
19
|
+
const random = seededRandom(key);
|
|
20
|
+
return {
|
|
21
|
+
now: (picture, timezone) => format.evaluate(void 0, {
|
|
22
|
+
millis: clock.getTime(),
|
|
23
|
+
picture,
|
|
24
|
+
timezone
|
|
25
|
+
}),
|
|
26
|
+
millis: () => clock.getTime(),
|
|
27
|
+
random,
|
|
28
|
+
shuffle: (items) => {
|
|
29
|
+
if (items === void 0) return void 0;
|
|
30
|
+
const shuffled = Array.isArray(items) ? [...items] : [items];
|
|
31
|
+
for (let index = shuffled.length - 1; index > 0; index--) {
|
|
32
|
+
const other = Math.floor(random() * (index + 1));
|
|
33
|
+
[shuffled[index], shuffled[other]] = [shuffled[other], shuffled[index]];
|
|
34
|
+
}
|
|
35
|
+
return shuffled;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
export { stepFunctionNames, stepFunctions };
|
package/dist/run/step-input.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
+
import { stepFunctionNames, stepFunctions } from "./step-functions.js";
|
|
1
2
|
//#region src/run/step-input.ts
|
|
2
3
|
/**
|
|
3
|
-
* The names an expression reads in a scope: the outer scopes' variables, this one's,
|
|
4
|
-
*
|
|
4
|
+
* The names an expression reads in a scope: the outer scopes' variables, this one's, `$states` with the run's context
|
|
5
|
+
* and the state's input as `$states.input`, which the check lets only a Map item's first state read, and the step's own
|
|
6
|
+
* `$now`, `$millis`, `$random` and `$shuffle`.
|
|
5
7
|
*/
|
|
6
|
-
const bindingsOf = (context, outer, position) => ({
|
|
8
|
+
const bindingsOf = (context, outer, scope, position) => ({
|
|
7
9
|
...outer,
|
|
8
10
|
...position.variables,
|
|
11
|
+
...stepFunctions(position.clock, `${context.runId}/${scope}/${position.seq}/${position.attempt}`),
|
|
9
12
|
states: {
|
|
10
13
|
input: position.input,
|
|
11
14
|
context: context.stateContext
|
|
12
15
|
}
|
|
13
16
|
});
|
|
14
|
-
/** The variables an inner scope sees: every one in scope here, without `$states
|
|
15
|
-
const visibleVariables = (
|
|
17
|
+
/** The variables an inner scope sees: every one in scope here, without `$states` and the step's functions, which each scope adds itself. */
|
|
18
|
+
const visibleVariables = (bindings) => {
|
|
19
|
+
const { states: _, ...variables } = bindings;
|
|
20
|
+
for (const name of stepFunctionNames) delete variables[name];
|
|
21
|
+
return variables;
|
|
22
|
+
};
|
|
16
23
|
/**
|
|
17
24
|
* The key of an inner scope: item `index` of the Map `name` that was step `seq` of `scope`, such as `Each.3[0]`. The
|
|
18
25
|
* name is URI-encoded, so a `/` or `[` in a state's name cannot make two scopes' keys the same.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/workflows",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Workflows as state machines with JSONata expressions: their check, a runner that saves every step, and the plugin interface for trigger and step types.",
|
|
3
|
+
"version": "0.77.0",
|
|
4
|
+
"description": "Workflows as state machines with JSONata expressions: their check, a runner that saves only what a run needs to go on and traces every step, and the plugin interface for trigger and step types.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|