@mark1russell7/workflow-runner 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +37 -2
- package/build/engine-B_0OOo1N.d.ts +229 -0
- package/build/engine-B_0OOo1N.d.ts.map +1 -0
- package/build/index.d.ts +14 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +984 -0
- package/build/index.js.map +1 -0
- package/build/service.d.ts +31 -0
- package/build/service.d.ts.map +1 -0
- package/build/service.js +56 -0
- package/build/service.js.map +1 -0
- package/package.json +53 -3
- package/src/engine.ts +662 -0
- package/src/index.ts +14 -0
- package/src/machines.ts +98 -0
- package/src/record.ts +89 -0
- package/src/service.ts +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/workflow-runner
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package runs a workflow document of `@mark1russell7/workflow`. Each step is one call of a procedure. The runner keeps one append-only record for each run.
|
|
4
|
+
|
|
5
|
+
## Use
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { memoryStore, workflowRunner } from "@mark1russell7/workflow-runner";
|
|
9
|
+
import { realClock } from "@mark1russell7/algebra/clock";
|
|
10
|
+
|
|
11
|
+
const runner = workflowRunner({
|
|
12
|
+
procedures: { "git.clone": { idempotent: true } },
|
|
13
|
+
call: async ({ procedure, inputs, key }) => host.call(procedure, inputs, key),
|
|
14
|
+
access: ({ from, access, target }) => grants.check(from, access, target),
|
|
15
|
+
redact: (value) => secrets.redact(value),
|
|
16
|
+
store: memoryStore(),
|
|
17
|
+
clock: realClock,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const result = await runner.start({ doc, owner: "technical", trigger: { kind: "manual", id: "m-1" } });
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## The ports
|
|
24
|
+
|
|
25
|
+
| Port | Task |
|
|
26
|
+
| --- | --- |
|
|
27
|
+
| `procedures` | The facts of each procedure: its workspace inputs, and `idempotent` when it removes a second call with the same key. |
|
|
28
|
+
| `call` | The host does one call. A secret input stays a reference, and the host reads its value. |
|
|
29
|
+
| `access` | The grant check. A deny, a throw and an unknown procedure each refuse the effect. |
|
|
30
|
+
| `redact` | The host removes each secret value from a value before the record. |
|
|
31
|
+
| `store` | The store of the records. An append at a wrong place gives `RecordConflict`. |
|
|
32
|
+
| `clock` | The clock of the time limits and the retry waits. |
|
|
33
|
+
|
|
34
|
+
## The record
|
|
35
|
+
|
|
36
|
+
The record of a run is also the log of its resume. `runner.resume(run)` answers each recorded effect from the record, then it runs live. A call with a start and no end calls again only for an idempotent procedure. Else the run ends as `in-doubt`.
|
|
37
|
+
|
|
38
|
+
The decision D225 in `docs/DECISIONS.md` gives the reasons and the case against.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { Clock } from "@mark1russell7/algebra/clock";
|
|
2
|
+
import { Machine } from "@mark1russell7/coeval";
|
|
3
|
+
import { ProcedureInfo } from "@mark1russell7/workflow";
|
|
4
|
+
/** The final status of a run. */
|
|
5
|
+
type RunStatus = "succeeded" | "failed" | "refused" | "invalid" | "in-doubt" | "cancelled";
|
|
6
|
+
/** The final status of a step. */
|
|
7
|
+
type StepStatus = "succeeded" | "skipped" | "failed" | "timed-out" | "refused" | "in-doubt" | "cancelled";
|
|
8
|
+
/** The event that started a run. `id` makes the run id: the same event gives the same run. */
|
|
9
|
+
interface TriggerEvent {
|
|
10
|
+
/** The kind of the trigger: `manual`, `schedule`, `event`, `feed` or `call`. */
|
|
11
|
+
readonly kind: string;
|
|
12
|
+
/** The id of the event, for example the id of a delivery or the time of a schedule boundary. */
|
|
13
|
+
readonly id: string;
|
|
14
|
+
/** The data of the event. A reference reads it as `event`. */
|
|
15
|
+
readonly data?: unknown;
|
|
16
|
+
/** For a feed: the workspace that the feed reads. */
|
|
17
|
+
readonly from?: string;
|
|
18
|
+
/** For a call: the workspace of the caller. */
|
|
19
|
+
readonly caller?: string;
|
|
20
|
+
}
|
|
21
|
+
/** The outcome of one effect, after the redaction of the host. */
|
|
22
|
+
type Outcome = {
|
|
23
|
+
readonly ok: true;
|
|
24
|
+
readonly value: unknown;
|
|
25
|
+
} | {
|
|
26
|
+
readonly ok: false;
|
|
27
|
+
readonly code: string;
|
|
28
|
+
readonly why: string;
|
|
29
|
+
};
|
|
30
|
+
/** One entry of the record of a run. The record is append-only, and it is also the log of a resume. */
|
|
31
|
+
type RunEntry = {
|
|
32
|
+
readonly type: "start";
|
|
33
|
+
readonly run: string;
|
|
34
|
+
readonly docHash: string;
|
|
35
|
+
readonly doc: unknown;
|
|
36
|
+
readonly owner: string;
|
|
37
|
+
readonly workspace: unknown;
|
|
38
|
+
readonly trigger: TriggerEvent;
|
|
39
|
+
readonly inputs: unknown;
|
|
40
|
+
readonly time: number;
|
|
41
|
+
} | {
|
|
42
|
+
readonly type: "grant";
|
|
43
|
+
readonly at: string;
|
|
44
|
+
readonly from: string;
|
|
45
|
+
readonly access: "read" | "write";
|
|
46
|
+
readonly target: string;
|
|
47
|
+
readonly ok: boolean;
|
|
48
|
+
readonly why: string;
|
|
49
|
+
readonly time: number;
|
|
50
|
+
} | {
|
|
51
|
+
readonly type: "transition";
|
|
52
|
+
readonly machine: "workflow.run" | "workflow.step";
|
|
53
|
+
readonly instance: string;
|
|
54
|
+
readonly from: string;
|
|
55
|
+
readonly on: string;
|
|
56
|
+
readonly to: string;
|
|
57
|
+
readonly time: number;
|
|
58
|
+
readonly due?: number;
|
|
59
|
+
} | {
|
|
60
|
+
readonly type: "rows";
|
|
61
|
+
readonly job: string;
|
|
62
|
+
readonly rows: readonly unknown[];
|
|
63
|
+
readonly time: number;
|
|
64
|
+
} | {
|
|
65
|
+
readonly type: "effect-start";
|
|
66
|
+
readonly at: string;
|
|
67
|
+
readonly key: string;
|
|
68
|
+
readonly try: number;
|
|
69
|
+
readonly procedure: string;
|
|
70
|
+
readonly payload: unknown;
|
|
71
|
+
readonly time: number;
|
|
72
|
+
} | {
|
|
73
|
+
readonly type: "effect-end";
|
|
74
|
+
readonly at: string;
|
|
75
|
+
readonly outcome: Outcome;
|
|
76
|
+
readonly time: number;
|
|
77
|
+
} | {
|
|
78
|
+
readonly type: "step-end";
|
|
79
|
+
readonly instance: string;
|
|
80
|
+
readonly status: StepStatus;
|
|
81
|
+
readonly value?: unknown;
|
|
82
|
+
readonly time: number;
|
|
83
|
+
} | {
|
|
84
|
+
readonly type: "job-end";
|
|
85
|
+
readonly instance: string;
|
|
86
|
+
readonly status: StepStatus;
|
|
87
|
+
readonly outputs?: unknown;
|
|
88
|
+
readonly time: number;
|
|
89
|
+
} | {
|
|
90
|
+
readonly type: "end";
|
|
91
|
+
readonly status: RunStatus;
|
|
92
|
+
readonly why: string;
|
|
93
|
+
readonly time: number;
|
|
94
|
+
};
|
|
95
|
+
/** The answer of a store to an append that another writer took first. */
|
|
96
|
+
declare class RecordConflict extends Error {
|
|
97
|
+
/** The count of the entries that the store holds. */
|
|
98
|
+
readonly length: number;
|
|
99
|
+
/**
|
|
100
|
+
* This constructor makes the error.
|
|
101
|
+
*
|
|
102
|
+
* @param run - The run id.
|
|
103
|
+
* @param seq - The place that the writer asked for.
|
|
104
|
+
* @param length - The count of the entries that the store holds.
|
|
105
|
+
*/
|
|
106
|
+
constructor(run: string, seq: number, length: number);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The store of the records of the runs. An append gives the place of the entry. When the record has another length,
|
|
110
|
+
* the store refuses with `RecordConflict`. Thus two runners cannot write one run (a fenced append).
|
|
111
|
+
*/
|
|
112
|
+
interface RunStore {
|
|
113
|
+
/** This function appends one entry at the place `seq`, or it refuses with `RecordConflict`. */
|
|
114
|
+
readonly append: (run: string, seq: number, entry: RunEntry) => Promise<void> | void;
|
|
115
|
+
/** This function gives the entries of a run in their order. A run with no record gives an empty list. */
|
|
116
|
+
readonly read: (run: string) => Promise<readonly RunEntry[]> | readonly RunEntry[];
|
|
117
|
+
}
|
|
118
|
+
/** A store in memory, for tests and for a host with no durable store. Each entry is a copy as JSON. */
|
|
119
|
+
declare function memoryStore(): RunStore & {
|
|
120
|
+
readonly runs: () => readonly string[];
|
|
121
|
+
};
|
|
122
|
+
/** The facts of one procedure for the runner: the facts of the check, and the removal of a second call. */
|
|
123
|
+
interface RunnerProcedure extends ProcedureInfo {
|
|
124
|
+
/**
|
|
125
|
+
* `true` when the procedure removes a second call with the same key. Then the runner calls again after a crash or a
|
|
126
|
+
* time limit. Without it, an effect with no outcome ends the step as `in-doubt`.
|
|
127
|
+
*/
|
|
128
|
+
readonly idempotent?: boolean;
|
|
129
|
+
}
|
|
130
|
+
/** One call of a procedure, for the host. */
|
|
131
|
+
interface ProcedureCall {
|
|
132
|
+
/** The path of the procedure, for example `["git", "clone"]`. */
|
|
133
|
+
readonly procedure: readonly string[];
|
|
134
|
+
/** The inputs. A secret stays a reference: the host reads its value. */
|
|
135
|
+
readonly inputs: Readonly<Record<string, unknown>>;
|
|
136
|
+
/** The idempotency key: the same for each try and each resume of one effect. */
|
|
137
|
+
readonly key: string;
|
|
138
|
+
/** The number of the try, from 1. */
|
|
139
|
+
readonly try: number;
|
|
140
|
+
/** The workspace that runs the workflow. */
|
|
141
|
+
readonly workspace: string;
|
|
142
|
+
/** The signal of the time limit of the step. */
|
|
143
|
+
readonly signal: AbortSignal;
|
|
144
|
+
}
|
|
145
|
+
/** One question to the grant check. */
|
|
146
|
+
interface AccessRequest {
|
|
147
|
+
readonly from: string;
|
|
148
|
+
readonly access: "read" | "write";
|
|
149
|
+
readonly target: string;
|
|
150
|
+
}
|
|
151
|
+
/** The answer of the grant check. */
|
|
152
|
+
interface AccessAnswer {
|
|
153
|
+
readonly ok: boolean;
|
|
154
|
+
readonly why: string;
|
|
155
|
+
}
|
|
156
|
+
/** The limits of the runner. A document over a limit is `invalid`, and an outcome over its limit fails its step. */
|
|
157
|
+
interface RunnerLimits {
|
|
158
|
+
/** The most rows of one matrix. The default is 256. */
|
|
159
|
+
readonly matrix?: number;
|
|
160
|
+
/** The most tries of one step. The default is 10. */
|
|
161
|
+
readonly tries?: number;
|
|
162
|
+
/** The most bytes of one outcome as JSON. The default is 1000000. */
|
|
163
|
+
readonly outcomeBytes?: number;
|
|
164
|
+
/** The most job instances that run at the same time. The default is 8. */
|
|
165
|
+
readonly jobs?: number;
|
|
166
|
+
}
|
|
167
|
+
/** The ports of the runner. The runner names no host: the host gives each port. */
|
|
168
|
+
interface RunnerOptions {
|
|
169
|
+
/** The facts of each procedure, by its path with dots. A call of a procedure that is not here is refused. */
|
|
170
|
+
readonly procedures: Readonly<Record<string, RunnerProcedure>>;
|
|
171
|
+
/** The host does one call. A throw is the failure of the call. */
|
|
172
|
+
readonly call: (call: ProcedureCall) => Promise<unknown>;
|
|
173
|
+
/** The grant check. A throw is a refusal. */
|
|
174
|
+
readonly access: (request: AccessRequest) => AccessAnswer | Promise<AccessAnswer>;
|
|
175
|
+
/**
|
|
176
|
+
* The redaction of the host: it removes the value of each secret from a value. The runner gives it each outcome, each
|
|
177
|
+
* error text, each payload and the trigger event, before the record and before a later step reads them.
|
|
178
|
+
*/
|
|
179
|
+
readonly redact: (value: unknown) => unknown;
|
|
180
|
+
/** The store of the records. */
|
|
181
|
+
readonly store: RunStore;
|
|
182
|
+
/** The clock of the time limits and the retry waits. */
|
|
183
|
+
readonly clock: Clock;
|
|
184
|
+
/** The wall time of the record, in milliseconds. The default is `Date.now`. A due time of a retry uses it. */
|
|
185
|
+
readonly wall?: () => number;
|
|
186
|
+
/** The machine of the documents. The default is `workflowMachine()`. */
|
|
187
|
+
readonly machine?: Machine;
|
|
188
|
+
/** The namespaces of calls, besides `procedure`. */
|
|
189
|
+
readonly callNamespaces?: readonly string[];
|
|
190
|
+
/** The limits. */
|
|
191
|
+
readonly limits?: RunnerLimits;
|
|
192
|
+
}
|
|
193
|
+
/** A request to start a run. */
|
|
194
|
+
interface StartRequest {
|
|
195
|
+
/** The workflow document. */
|
|
196
|
+
readonly doc: unknown;
|
|
197
|
+
/** The workspace that holds the document, from the store of the host. The field `workspace` of the document must be the same. */
|
|
198
|
+
readonly owner: string;
|
|
199
|
+
/** The event that starts the run. */
|
|
200
|
+
readonly trigger: TriggerEvent;
|
|
201
|
+
/** The inputs of the run. */
|
|
202
|
+
readonly inputs?: Readonly<Record<string, unknown>>;
|
|
203
|
+
}
|
|
204
|
+
/** The end of a run. */
|
|
205
|
+
interface RunResult {
|
|
206
|
+
readonly run: string;
|
|
207
|
+
readonly status: RunStatus;
|
|
208
|
+
readonly why: string;
|
|
209
|
+
readonly entries: readonly RunEntry[];
|
|
210
|
+
}
|
|
211
|
+
/** The runner of a workflow. */
|
|
212
|
+
interface WorkflowRunner {
|
|
213
|
+
/** This function gives the run id of a request: the same document, owner and event give the same id. */
|
|
214
|
+
readonly runIdOf: (request: StartRequest) => string;
|
|
215
|
+
/** This function starts a run. A request with a run id that has a record resumes that run. */
|
|
216
|
+
readonly start: (request: StartRequest) => Promise<RunResult>;
|
|
217
|
+
/** This function resumes a run from its record. The record holds the document, thus a changed document makes no change. */
|
|
218
|
+
readonly resume: (run: string) => Promise<RunResult>;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* This function makes a runner of workflows. The runner records each run in the store, and each record is the log of
|
|
222
|
+
* a resume. It refuses an effect that the grant check does not permit, and it records each answer.
|
|
223
|
+
*
|
|
224
|
+
* @param options - The ports and the limits.
|
|
225
|
+
* @returns The runner.
|
|
226
|
+
*/
|
|
227
|
+
declare function workflowRunner(options: RunnerOptions): WorkflowRunner;
|
|
228
|
+
export { TriggerEvent as _, RunnerLimits as a, StartRequest as c, Outcome as d, RecordConflict as f, StepStatus as g, RunStore as h, RunResult as i, WorkflowRunner as l, RunStatus as m, AccessRequest as n, RunnerOptions as o, RunEntry as p, ProcedureCall as r, RunnerProcedure as s, AccessAnswer as t, workflowRunner as u, memoryStore as v };
|
|
229
|
+
//# sourceMappingURL=engine-B_0OOo1N.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine-B_0OOo1N.d.ts","names":[],"sources":["../src/record.ts","../src/engine.ts"],"mappings":";;;;KACY;;KAGA;;UAGK;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;;KAIC;WAAqB;WAAmB;;WAA8B;WAAoB;WAAuB;;;KAGjH;WAEG;WACA;WACA;WACA;WACA;WACA;WACA,SAAS;WACT;WACA;;WAEA;WAAwB;WAAqB;WAAuB;WAAmC;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Store, i as RunResult, l as WorkflowRunner, m as RunStatus, n as AccessRequest, o as RunnerOptions, p as RunEntry, r as ProcedureCall, s as RunnerProcedure, t as AccessAnswer, u as workflowRunner, v as memoryStore } from "./engine-B_0OOo1N.js";
|
|
2
|
+
import { Machine } from "@mark1russell7/algebra";
|
|
3
|
+
/**
|
|
4
|
+
* The state machine of a run. A run is queued, then it runs. It ends with one status. The runner records each
|
|
5
|
+
* transition, and a test checks the record against this machine.
|
|
6
|
+
*/
|
|
7
|
+
export declare const RUN_MACHINE: Machine;
|
|
8
|
+
/**
|
|
9
|
+
* The state machine of one step of one job instance. A failed try waits for its retry. A refusal and a doubt end the
|
|
10
|
+
* step at once: no retry and no `continue` changes them.
|
|
11
|
+
*/
|
|
12
|
+
export declare const STEP_MACHINE: Machine;
|
|
13
|
+
export { type AccessAnswer, type AccessRequest, type Outcome, type ProcedureCall, RecordConflict, type RunEntry, type RunResult, type RunStatus, type RunStore, type RunnerLimits, type RunnerOptions, type RunnerProcedure, type StartRequest, type StepStatus, type TriggerEvent, type WorkflowRunner, memoryStore, workflowRunner };
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/machines.ts"],"mappings":";;;;;;qBAQa,aAAa;;;;;qBA4Cb,cAAc"}
|