@operad-io/engine 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 +11 -2
- package/config.schema.json +188 -0
- package/keys.schema.json +12 -0
- package/package.json +22 -4
- package/src/config/config.ts +627 -0
- package/src/config/index.ts +1 -0
- package/src/flow/define-flow.ts +57 -0
- package/src/flow/index.ts +5 -0
- package/src/flow/load.ts +41 -0
- package/src/flow/types.ts +107 -0
- package/src/flow/validate.ts +296 -0
- package/src/http/address.ts +142 -0
- package/src/http/index.ts +25 -0
- package/src/http/link.ts +20 -0
- package/src/http/server.ts +920 -0
- package/src/http/token.ts +90 -0
- package/src/index.ts +18 -0
- package/src/kernel/decide.ts +689 -0
- package/src/kernel/index.ts +3 -0
- package/src/kernel/state.ts +275 -0
- package/src/kernel/types.ts +366 -0
- package/src/log/errors.ts +77 -0
- package/src/log/facts.ts +50 -0
- package/src/log/index.ts +6 -0
- package/src/log/json-value.ts +67 -0
- package/src/log/open-log.ts +17 -0
- package/src/log/schema.ts +5 -0
- package/src/log/secret.ts +30 -0
- package/src/log/sqlite.ts +1011 -0
- package/src/log/types.ts +230 -0
- package/src/loop/code-watch.ts +486 -0
- package/src/loop/index.ts +3 -0
- package/src/loop/older-code.ts +58 -0
- package/src/loop/serve.ts +483 -0
- package/src/markdown.ts +451 -0
- package/src/notify/index.ts +10 -0
- package/src/notify/notify.ts +256 -0
- package/src/platform/file-lock.ts +385 -0
- package/src/platform/index.ts +13 -0
- package/src/platform/load.ts +18 -0
- package/src/platform/process-group.ts +675 -0
- package/src/platform/process-watch.ts +152 -0
- package/src/platform/sqlite-open.ts +104 -0
- package/src/platform/step-reap.ts +668 -0
- package/src/platform/watch.ts +158 -0
- package/src/platform/worker-reports.ts +34 -0
- package/src/plugin/action-schema.ts +550 -0
- package/src/plugin/declared-flows.ts +74 -0
- package/src/plugin/define-plugin.ts +12 -0
- package/src/plugin/errors.ts +73 -0
- package/src/plugin/filters.ts +46 -0
- package/src/plugin/harness.ts +50 -0
- package/src/plugin/home.ts +13 -0
- package/src/plugin/index.ts +65 -0
- package/src/plugin/install.ts +268 -0
- package/src/plugin/keys.ts +63 -0
- package/src/plugin/load.ts +256 -0
- package/src/plugin/logger.ts +30 -0
- package/src/plugin/object-facts.ts +132 -0
- package/src/plugin/open-store.ts +32 -0
- package/src/plugin/paging.ts +133 -0
- package/src/plugin/registry.ts +1786 -0
- package/src/plugin/secrets.ts +141 -0
- package/src/plugin/semver.ts +110 -0
- package/src/plugin/settings.ts +64 -0
- package/src/plugin/sqlite-store.ts +756 -0
- package/src/plugin/store.ts +318 -0
- package/src/plugin/types.ts +1243 -0
- package/src/plugin/usage.ts +157 -0
- package/src/plugin/validate.ts +1028 -0
- package/src/runner/answer.ts +156 -0
- package/src/runner/ask.ts +483 -0
- package/src/runner/cancel.ts +51 -0
- package/src/runner/errors.ts +20 -0
- package/src/runner/grants.ts +92 -0
- package/src/runner/index.ts +33 -0
- package/src/runner/limits.ts +403 -0
- package/src/runner/lock.ts +140 -0
- package/src/runner/runner.ts +1175 -0
- package/src/runner/service.ts +184 -0
- package/src/runner/step-worker.ts +303 -0
- package/src/testing/clock.ts +67 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/types.ts +116 -0
- package/src/testing/world.ts +369 -0
- package/src/trigger/cron.ts +193 -0
- package/src/trigger/index.ts +32 -0
- package/src/trigger/kernel.ts +1221 -0
- package/src/trigger/rules.ts +300 -0
- package/src/trigger/store.ts +233 -0
- package/src/trigger/types.ts +156 -0
- package/src/util.ts +50 -0
- package/src/views/history.ts +634 -0
- package/src/views/index.ts +7 -0
- package/src/views/pending.ts +209 -0
- package/src/views/presentation.ts +2609 -0
- package/src/views/running.ts +255 -0
- package/src/views/settings.ts +452 -0
- package/src/views/types.ts +72 -0
- package/src/views/uploads.ts +228 -0
- package/triggers.schema.json +48 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** Reading a run's state and appending one input's facts as one batch. */
|
|
2
|
+
|
|
3
|
+
import type { FlowDefinition } from "../flow/types.ts";
|
|
4
|
+
import { decide, evolve, initialState } from "../kernel/index.ts";
|
|
5
|
+
import type { Decision, Input, KernelState } from "../kernel/types.ts";
|
|
6
|
+
import type { LogBackend } from "../log/types.ts";
|
|
7
|
+
import type { ObjectRef } from "../plugin/types.ts";
|
|
8
|
+
|
|
9
|
+
/** Rebuild a run's state by streaming its log, without holding the facts. */
|
|
10
|
+
export async function readState(
|
|
11
|
+
log: LogBackend,
|
|
12
|
+
runId: string,
|
|
13
|
+
): Promise<KernelState> {
|
|
14
|
+
let state = initialState();
|
|
15
|
+
for await (const fact of log.read(runId)) {
|
|
16
|
+
state = evolve(state, fact);
|
|
17
|
+
}
|
|
18
|
+
return state;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Ends a run no flow can drive, with its own ending fact so history says why.
|
|
23
|
+
* An ended run is only marked ended; a run the log does not know is left alone.
|
|
24
|
+
*/
|
|
25
|
+
export async function endRunDirectly(
|
|
26
|
+
log: LogBackend,
|
|
27
|
+
runId: string,
|
|
28
|
+
status: "failed" | "cancelled",
|
|
29
|
+
reason: string | null,
|
|
30
|
+
): Promise<KernelState | undefined> {
|
|
31
|
+
const run = (await log.runs()).find((candidate) => candidate.runId === runId);
|
|
32
|
+
if (run === undefined) return undefined;
|
|
33
|
+
let state = await readState(log, runId);
|
|
34
|
+
if (state.status !== "ended") {
|
|
35
|
+
const appended = await log.append(runId, state.lastSeq, [
|
|
36
|
+
{
|
|
37
|
+
kind: "run_ended",
|
|
38
|
+
payload: { status, reason },
|
|
39
|
+
cause: `${status}:${runId}`,
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
for (const fact of appended.facts) state = evolve(state, fact);
|
|
43
|
+
}
|
|
44
|
+
await log.endRun(runId);
|
|
45
|
+
return state;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function objectKey(object: ObjectRef | undefined): string {
|
|
49
|
+
return object === undefined
|
|
50
|
+
? "flow"
|
|
51
|
+
: `${object.type}:${object.id}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The object a run belongs to, when its key names one. A run started without
|
|
56
|
+
* an object is keyed `flow:<plugin>/<flow>`, so that one active run per flow
|
|
57
|
+
* holds; `flow` is therefore not an object type and that key names no object.
|
|
58
|
+
*/
|
|
59
|
+
export function parseObjectKey(
|
|
60
|
+
key: string,
|
|
61
|
+
): { readonly type: string; readonly id: string } | undefined {
|
|
62
|
+
const colon = key.indexOf(":");
|
|
63
|
+
if (colon <= 0 || colon === key.length - 1) return undefined;
|
|
64
|
+
if (key.slice(0, colon) === "flow") return undefined;
|
|
65
|
+
return { type: key.slice(0, colon), id: key.slice(colon + 1) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ApplyResult {
|
|
69
|
+
readonly state: KernelState;
|
|
70
|
+
readonly decision: Decision;
|
|
71
|
+
readonly duplicate: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** True when a fact of the run already names the input as its cause. */
|
|
75
|
+
async function inputApplied(
|
|
76
|
+
log: LogBackend,
|
|
77
|
+
runId: string,
|
|
78
|
+
inputId: string,
|
|
79
|
+
): Promise<boolean> {
|
|
80
|
+
for await (const fact of log.read(runId)) {
|
|
81
|
+
if (fact.cause === inputId) return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Records one input, asks `decide` for its facts and appends them as one
|
|
88
|
+
* batch. A repeated (source, key) appends nothing.
|
|
89
|
+
*
|
|
90
|
+
* A crash can leave the input's record behind without its facts. Such a
|
|
91
|
+
* record is not an applied input: the run has no fact caused by it, so its
|
|
92
|
+
* decision still has to be taken and appended.
|
|
93
|
+
*/
|
|
94
|
+
export interface ApplyHooks {
|
|
95
|
+
/**
|
|
96
|
+
* Runs after the ending facts are written, before the run is marked ended.
|
|
97
|
+
* Recording the end event here keeps it recoverable: a crash before the
|
|
98
|
+
* run is ended leaves the run active, so the next pass finishes it again.
|
|
99
|
+
*/
|
|
100
|
+
readonly beforeEnd?:
|
|
101
|
+
| ((runId: string, state: KernelState) => void | Promise<void>)
|
|
102
|
+
| undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Asks `decide` for one input's facts and appends them as one batch, without
|
|
107
|
+
* recording an input. The caller supplies the input id as the cause.
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* Asks `decide` for one input's facts and appends them as one batch, without
|
|
111
|
+
* recording an input. The caller supplies the input id as the cause.
|
|
112
|
+
*/
|
|
113
|
+
export async function appendInput(
|
|
114
|
+
log: LogBackend,
|
|
115
|
+
flow: FlowDefinition,
|
|
116
|
+
runId: string,
|
|
117
|
+
state: KernelState,
|
|
118
|
+
input: Input,
|
|
119
|
+
now: number,
|
|
120
|
+
): Promise<{ readonly state: KernelState; readonly decision: Decision }> {
|
|
121
|
+
const decision = decide(flow, state, input, now);
|
|
122
|
+
const facts = decision.facts;
|
|
123
|
+
let next = state;
|
|
124
|
+
if (facts.length > 0) {
|
|
125
|
+
await log.append(runId, state.lastSeq, facts);
|
|
126
|
+
for (const fact of facts) next = evolve(next, fact);
|
|
127
|
+
}
|
|
128
|
+
return { state: next, decision };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Records one input, asks `decide` for its facts and appends them as one
|
|
133
|
+
* batch. A repeated (source, key) appends nothing.
|
|
134
|
+
*
|
|
135
|
+
* A crash can leave the input's record behind without its facts. Such a
|
|
136
|
+
* record is not an applied input: the run has no fact caused by it, so its
|
|
137
|
+
* decision still has to be taken and appended.
|
|
138
|
+
*/
|
|
139
|
+
export async function applyInput(
|
|
140
|
+
log: LogBackend,
|
|
141
|
+
flow: FlowDefinition,
|
|
142
|
+
runId: string,
|
|
143
|
+
state: KernelState,
|
|
144
|
+
input: Input,
|
|
145
|
+
now: number,
|
|
146
|
+
source: string,
|
|
147
|
+
sourceKey: string,
|
|
148
|
+
hooks: ApplyHooks = {},
|
|
149
|
+
): Promise<ApplyResult> {
|
|
150
|
+
const recorded = await log.recordInput(
|
|
151
|
+
runId,
|
|
152
|
+
source,
|
|
153
|
+
`${runId}:${sourceKey}`,
|
|
154
|
+
{
|
|
155
|
+
kind: input.kind,
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
if (recorded.duplicate) {
|
|
159
|
+
if (await inputApplied(log, runId, recorded.inputId)) {
|
|
160
|
+
return {
|
|
161
|
+
state,
|
|
162
|
+
decision: { facts: [], effects: [] },
|
|
163
|
+
duplicate: true,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
// The record outlived the crash, the facts did not: decide again with the
|
|
167
|
+
// recorded input id so the batch is attributed to the same input.
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const withId: Input = { ...input, inputId: recorded.inputId };
|
|
171
|
+
const { state: next, decision } = await appendInput(
|
|
172
|
+
log,
|
|
173
|
+
flow,
|
|
174
|
+
runId,
|
|
175
|
+
state,
|
|
176
|
+
withId,
|
|
177
|
+
now,
|
|
178
|
+
);
|
|
179
|
+
if (next.status === "ended") {
|
|
180
|
+
await hooks.beforeEnd?.(runId, next);
|
|
181
|
+
await log.endRun(runId);
|
|
182
|
+
}
|
|
183
|
+
return { state: next, decision, duplicate: false };
|
|
184
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs one plugin step in a worker thread, so the run's process can stop it
|
|
3
|
+
* even when the step never yields to the event loop. The worker gets the
|
|
4
|
+
* plugin specifiers and the store root, never a live registry.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
8
|
+
import { messageOf } from "../plugin/errors.ts";
|
|
9
|
+
import { loadPlugin } from "../plugin/load.ts";
|
|
10
|
+
import { PluginRegistry } from "../plugin/registry.ts";
|
|
11
|
+
import type {
|
|
12
|
+
AskOutcome,
|
|
13
|
+
PluginStepResult,
|
|
14
|
+
RunObject,
|
|
15
|
+
StepAsker,
|
|
16
|
+
StepLimiter,
|
|
17
|
+
StepLimitRequest,
|
|
18
|
+
} from "../plugin/types.ts";
|
|
19
|
+
import type { JsonValue } from "../log/types.ts";
|
|
20
|
+
import { SqlitePluginStore } from "../plugin/sqlite-store.ts";
|
|
21
|
+
import type {
|
|
22
|
+
PluginStore,
|
|
23
|
+
PluginStoreLocation,
|
|
24
|
+
} from "../plugin/types.ts";
|
|
25
|
+
import type { StepEvent, TriggerView } from "../trigger/types.ts";
|
|
26
|
+
|
|
27
|
+
export interface StepWorkerRequest {
|
|
28
|
+
readonly sources: readonly string[];
|
|
29
|
+
readonly home: string;
|
|
30
|
+
readonly cwd: string;
|
|
31
|
+
readonly storeRoot: string;
|
|
32
|
+
/** The configured store; absent for a memory store, which cannot be shared. */
|
|
33
|
+
readonly store?: PluginStoreLocation | undefined;
|
|
34
|
+
readonly use: string;
|
|
35
|
+
readonly params: unknown;
|
|
36
|
+
readonly flowParams: JsonValue;
|
|
37
|
+
readonly access?: JsonValue | undefined;
|
|
38
|
+
/** The validated settings of every plugin, keyed by plugin name. */
|
|
39
|
+
readonly settings?: Readonly<Record<string, unknown>> | undefined;
|
|
40
|
+
readonly trigger?: TriggerView | undefined;
|
|
41
|
+
readonly runId: string;
|
|
42
|
+
readonly object?: RunObject | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What a worker posts to and listens to: `MessagePort` in the process entry. */
|
|
46
|
+
export interface StepWorkerPort {
|
|
47
|
+
on(event: "message", listener: (message: unknown) => void): void;
|
|
48
|
+
postMessage(message: unknown): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type StepWorkerReply =
|
|
52
|
+
| { readonly kind: "result"; readonly ok: true; readonly result: PluginStepResult }
|
|
53
|
+
| { readonly kind: "result"; readonly ok: false; readonly error: string };
|
|
54
|
+
|
|
55
|
+
interface EmitAck {
|
|
56
|
+
readonly kind: "emit-result";
|
|
57
|
+
readonly id: number;
|
|
58
|
+
readonly ok: boolean;
|
|
59
|
+
readonly error?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface LimitAck {
|
|
63
|
+
readonly kind: "limit-result";
|
|
64
|
+
readonly id: number;
|
|
65
|
+
readonly ok: boolean;
|
|
66
|
+
readonly error?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface GrantAck {
|
|
70
|
+
readonly kind: "grant-result";
|
|
71
|
+
readonly id: number;
|
|
72
|
+
readonly ok: boolean;
|
|
73
|
+
readonly used?: boolean | undefined;
|
|
74
|
+
readonly error?: string | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface AskAck {
|
|
78
|
+
readonly kind: "ask-result";
|
|
79
|
+
readonly id: number;
|
|
80
|
+
readonly ok: boolean;
|
|
81
|
+
readonly outcome?: AskOutcome | undefined;
|
|
82
|
+
readonly error?: string | undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Opens the store the run configured, so a worker writes where the views read. */
|
|
86
|
+
function openWorkerStore(request: StepWorkerRequest): PluginStore {
|
|
87
|
+
const location = request.store;
|
|
88
|
+
if (location?.backend === "sqlite") {
|
|
89
|
+
return new SqlitePluginStore({ root: location.root, file: location.file });
|
|
90
|
+
}
|
|
91
|
+
return new SqlitePluginStore({ root: request.storeRoot });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function runStepWorker(
|
|
95
|
+
request: StepWorkerRequest,
|
|
96
|
+
parent: StepWorkerPort,
|
|
97
|
+
): Promise<void> {
|
|
98
|
+
const definitions: unknown[] = [];
|
|
99
|
+
for (const source of request.sources) {
|
|
100
|
+
definitions.push(await loadPlugin(source, request.home));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const pendingAcks = new Map<
|
|
104
|
+
number,
|
|
105
|
+
{ resolve: () => void; reject: (error: Error) => void }
|
|
106
|
+
>();
|
|
107
|
+
let nextEmitId = 1;
|
|
108
|
+
const pendingLimits = new Map<
|
|
109
|
+
number,
|
|
110
|
+
{ resolve: () => void; reject: (error: Error) => void }
|
|
111
|
+
>();
|
|
112
|
+
let nextLimitId = 1;
|
|
113
|
+
|
|
114
|
+
/** Asks the run's process, which owns the log, for a place under a key, or to show a wait. */
|
|
115
|
+
const limits: StepLimiter = {
|
|
116
|
+
report: (request) =>
|
|
117
|
+
hold({ use: "", key: request.key, concurrent: 0 }, true, request.ahead),
|
|
118
|
+
acquire: (request) => hold(request, false),
|
|
119
|
+
};
|
|
120
|
+
function hold(
|
|
121
|
+
request: StepLimitRequest,
|
|
122
|
+
report: boolean,
|
|
123
|
+
ahead?: number,
|
|
124
|
+
): Promise<() => Promise<void>> {
|
|
125
|
+
const id = nextLimitId;
|
|
126
|
+
nextLimitId += 1;
|
|
127
|
+
const ask = new Promise<void>((resolve, reject) => {
|
|
128
|
+
pendingLimits.set(id, { resolve, reject });
|
|
129
|
+
parent.postMessage({
|
|
130
|
+
kind: "limit",
|
|
131
|
+
id,
|
|
132
|
+
use: request.use,
|
|
133
|
+
key: request.key,
|
|
134
|
+
concurrent: request.concurrent,
|
|
135
|
+
...(report ? { report: true } : {}),
|
|
136
|
+
...(ahead === undefined ? {} : { ahead }),
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
const cancel = (): void => parent.postMessage({ kind: "limit-cancel", id });
|
|
140
|
+
request.signal?.addEventListener("abort", cancel, { once: true });
|
|
141
|
+
return ask.then(
|
|
142
|
+
() => {
|
|
143
|
+
request.signal?.removeEventListener("abort", cancel);
|
|
144
|
+
return () =>
|
|
145
|
+
new Promise<void>((resolve, reject) => {
|
|
146
|
+
pendingLimits.set(id, { resolve, reject });
|
|
147
|
+
parent.postMessage({ kind: "limit-release", id });
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
(error: unknown) => {
|
|
151
|
+
request.signal?.removeEventListener("abort", cancel);
|
|
152
|
+
throw error;
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const pendingAsks = new Map<
|
|
158
|
+
number,
|
|
159
|
+
{ resolve: (outcome: AskOutcome) => void; reject: (error: Error) => void }
|
|
160
|
+
>();
|
|
161
|
+
let nextAskId = 1;
|
|
162
|
+
|
|
163
|
+
/** Asks the run's process, which owns the log, to put a question to a person. */
|
|
164
|
+
const pendingGrants = new Map<
|
|
165
|
+
number,
|
|
166
|
+
{ resolve: (used: boolean) => void; reject: (error: Error) => void }
|
|
167
|
+
>();
|
|
168
|
+
let nextGrantId = 1;
|
|
169
|
+
|
|
170
|
+
const asker: StepAsker = {
|
|
171
|
+
useGrant: (waitId) => {
|
|
172
|
+
const id = nextGrantId;
|
|
173
|
+
nextGrantId += 1;
|
|
174
|
+
return new Promise<boolean>((resolve, reject) => {
|
|
175
|
+
pendingGrants.set(id, { resolve, reject });
|
|
176
|
+
parent.postMessage({ kind: "grant", id, waitId });
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
ask: (question, signal) => {
|
|
180
|
+
const id = nextAskId;
|
|
181
|
+
nextAskId += 1;
|
|
182
|
+
return new Promise<AskOutcome>((resolve, reject) => {
|
|
183
|
+
const cancel = (): void => parent.postMessage({ kind: "ask-cancel", id });
|
|
184
|
+
pendingAsks.set(id, {
|
|
185
|
+
resolve: (outcome) => {
|
|
186
|
+
signal?.removeEventListener("abort", cancel);
|
|
187
|
+
resolve(outcome);
|
|
188
|
+
},
|
|
189
|
+
reject: (error) => {
|
|
190
|
+
signal?.removeEventListener("abort", cancel);
|
|
191
|
+
reject(error);
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
195
|
+
parent.postMessage({
|
|
196
|
+
kind: "ask",
|
|
197
|
+
id,
|
|
198
|
+
request: {
|
|
199
|
+
prompt: question.prompt,
|
|
200
|
+
...(question.choices === undefined
|
|
201
|
+
? {}
|
|
202
|
+
: { choices: [...question.choices] }),
|
|
203
|
+
...(question.graceSecs === undefined
|
|
204
|
+
? {}
|
|
205
|
+
: { graceSecs: question.graceSecs }),
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
parent.on("message", (message: unknown) => {
|
|
213
|
+
if (typeof message !== "object" || message === null) return;
|
|
214
|
+
if ((message as { kind?: unknown }).kind === "grant-result") {
|
|
215
|
+
const ack = message as GrantAck;
|
|
216
|
+
const pending = pendingGrants.get(ack.id);
|
|
217
|
+
if (pending === undefined) return;
|
|
218
|
+
pendingGrants.delete(ack.id);
|
|
219
|
+
if (ack.ok) pending.resolve(ack.used === true);
|
|
220
|
+
else pending.reject(new Error(ack.error ?? "the permission was refused"));
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if ((message as { kind?: unknown }).kind === "ask-result") {
|
|
224
|
+
const ack = message as AskAck;
|
|
225
|
+
const pending = pendingAsks.get(ack.id);
|
|
226
|
+
if (pending === undefined) return;
|
|
227
|
+
pendingAsks.delete(ack.id);
|
|
228
|
+
if (ack.ok && ack.outcome !== undefined) pending.resolve(ack.outcome);
|
|
229
|
+
else pending.reject(new Error(ack.error ?? "the question was refused"));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if ((message as { kind?: unknown }).kind === "limit-result") {
|
|
233
|
+
const ack = message as LimitAck;
|
|
234
|
+
const pending = pendingLimits.get(ack.id);
|
|
235
|
+
if (pending === undefined) return;
|
|
236
|
+
pendingLimits.delete(ack.id);
|
|
237
|
+
if (ack.ok) pending.resolve();
|
|
238
|
+
else pending.reject(new Error(ack.error ?? "the place was refused"));
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if ((message as { kind?: unknown }).kind === "emit-result") {
|
|
242
|
+
const ack = message as EmitAck;
|
|
243
|
+
const pending = pendingAcks.get(ack.id);
|
|
244
|
+
if (pending === undefined) return;
|
|
245
|
+
pendingAcks.delete(ack.id);
|
|
246
|
+
if (ack.ok) pending.resolve();
|
|
247
|
+
else pending.reject(new Error(ack.error ?? "the event was refused"));
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const emit = (plugin: string, event: StepEvent): Promise<void> => {
|
|
252
|
+
const id = nextEmitId;
|
|
253
|
+
nextEmitId += 1;
|
|
254
|
+
return new Promise<void>((resolve, reject) => {
|
|
255
|
+
pendingAcks.set(id, { resolve, reject });
|
|
256
|
+
parent.postMessage({ kind: "emit", id, plugin, event });
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const registry = new PluginRegistry(definitions, {
|
|
261
|
+
home: request.home,
|
|
262
|
+
cwd: request.cwd,
|
|
263
|
+
store: openWorkerStore(request),
|
|
264
|
+
sources: request.sources,
|
|
265
|
+
...(request.settings === undefined ? {} : { settings: request.settings }),
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
const controller = new AbortController();
|
|
269
|
+
parent.on("message", (message: unknown) => {
|
|
270
|
+
if (message === "abort") controller.abort();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const result = await registry.runStep(request.use, request.params, {
|
|
274
|
+
runId: request.runId,
|
|
275
|
+
params: request.flowParams,
|
|
276
|
+
cwd: request.cwd,
|
|
277
|
+
access: request.access ?? null,
|
|
278
|
+
...(request.object === undefined ? {} : { object: request.object }),
|
|
279
|
+
...(request.trigger === undefined ? {} : { trigger: request.trigger }),
|
|
280
|
+
emit,
|
|
281
|
+
limits,
|
|
282
|
+
asker,
|
|
283
|
+
signal: controller.signal,
|
|
284
|
+
});
|
|
285
|
+
parent.postMessage({ kind: "result", ok: true, result } satisfies StepWorkerReply);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The process entry: one step run, with any failure reported to the parent. */
|
|
289
|
+
export function startStepWorker(
|
|
290
|
+
request: StepWorkerRequest,
|
|
291
|
+
port: StepWorkerPort,
|
|
292
|
+
): void {
|
|
293
|
+
void runStepWorker(request, port).catch((error: unknown) => {
|
|
294
|
+
port.postMessage({
|
|
295
|
+
kind: "result",
|
|
296
|
+
ok: false,
|
|
297
|
+
error: messageOf(error),
|
|
298
|
+
} satisfies StepWorkerReply);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const port = parentPort;
|
|
303
|
+
if (port !== null) startStepWorker(workerData as StepWorkerRequest, port);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** A clock the test moves. */
|
|
2
|
+
|
|
3
|
+
import type { FakeClock } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
interface Timer {
|
|
6
|
+
readonly handle: object;
|
|
7
|
+
readonly due: number;
|
|
8
|
+
readonly handler: () => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Time starts at `start` and moves only when `advance` is called. A timer set
|
|
13
|
+
* for `ms` fires when the clock passes `now() + ms`; timers that fall due in
|
|
14
|
+
* one `advance` fire in the order of their due times, and a timer set by a
|
|
15
|
+
* handler is fired too when it falls inside the same advance.
|
|
16
|
+
*/
|
|
17
|
+
export function fakeClock(start = 0): FakeClock {
|
|
18
|
+
let now = start;
|
|
19
|
+
const timers: Timer[] = [];
|
|
20
|
+
const waiters: Array<() => void> = [];
|
|
21
|
+
|
|
22
|
+
const setTimer = (handler: () => void, ms: number): object => {
|
|
23
|
+
const handle = {};
|
|
24
|
+
timers.push({ handle, due: now + Math.max(0, ms), handler });
|
|
25
|
+
for (const resolve of waiters.splice(0)) resolve();
|
|
26
|
+
return handle;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
now: () => now,
|
|
31
|
+
advance(ms: number): void {
|
|
32
|
+
const target = now + ms;
|
|
33
|
+
for (;;) {
|
|
34
|
+
let next: Timer | undefined;
|
|
35
|
+
for (const timer of timers) {
|
|
36
|
+
if (timer.due <= target && (next === undefined || timer.due < next.due)) {
|
|
37
|
+
next = timer;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (next === undefined) break;
|
|
41
|
+
timers.splice(timers.indexOf(next), 1);
|
|
42
|
+
now = Math.max(now, next.due);
|
|
43
|
+
next.handler();
|
|
44
|
+
}
|
|
45
|
+
now = target;
|
|
46
|
+
},
|
|
47
|
+
get pending(): number {
|
|
48
|
+
return timers.length;
|
|
49
|
+
},
|
|
50
|
+
whenTimer(): Promise<void> {
|
|
51
|
+
if (timers.length > 0) return Promise.resolve();
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
waiters.push(resolve);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
setTimeout: setTimer,
|
|
57
|
+
clearTimeout(handle: unknown): void {
|
|
58
|
+
const index = timers.findIndex((timer) => timer.handle === handle);
|
|
59
|
+
if (index >= 0) timers.splice(index, 1);
|
|
60
|
+
},
|
|
61
|
+
sleep(ms: number): Promise<void> {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
setTimer(resolve, ms);
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@operad-io/engine/testing`: run a plugin's flows in the test's own process
|
|
3
|
+
* tree. It wires the engine's runner, kernel and `serve` pass to a scratch log
|
|
4
|
+
* and store; it has no logic of its own to drift from them.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { FlowDefinition } from "../flow/types.ts";
|
|
8
|
+
import type { JsonValue } from "../log/types.ts";
|
|
9
|
+
import { answerRunWait, cleanup, memoryStore, resumeRun, testWorld } from "./world.ts";
|
|
10
|
+
import type {
|
|
11
|
+
EmitOptions,
|
|
12
|
+
EmitResult,
|
|
13
|
+
FlowRun,
|
|
14
|
+
PluginUnderTest,
|
|
15
|
+
RunFlowOptions,
|
|
16
|
+
} from "./types.ts";
|
|
17
|
+
|
|
18
|
+
export { fakeClock } from "./clock.ts";
|
|
19
|
+
export { cleanup, memoryStore, testWorld };
|
|
20
|
+
export type {
|
|
21
|
+
EmitOptions,
|
|
22
|
+
EmitResult,
|
|
23
|
+
FakeClock,
|
|
24
|
+
FlowEnd,
|
|
25
|
+
FlowRun,
|
|
26
|
+
FlowRunStatus,
|
|
27
|
+
FlowWait,
|
|
28
|
+
PluginUnderTest,
|
|
29
|
+
RunFlowOptions,
|
|
30
|
+
TestWorld,
|
|
31
|
+
WorldOptions,
|
|
32
|
+
} from "./types.ts";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Starts `flow` in a world of its own and runs it to its end or first wait.
|
|
36
|
+
* The run is about `options.object`, or `<first object type>:1`. Give the
|
|
37
|
+
* plugin's module path and its steps run in a worker, as under `serve`.
|
|
38
|
+
*/
|
|
39
|
+
export async function runFlow(
|
|
40
|
+
plugin: PluginUnderTest,
|
|
41
|
+
flow: string | FlowDefinition,
|
|
42
|
+
options: RunFlowOptions = {},
|
|
43
|
+
): Promise<FlowRun> {
|
|
44
|
+
const { object, params, access, ...worldOptions } = options;
|
|
45
|
+
const world = await testWorld(plugin, worldOptions);
|
|
46
|
+
return world.runFlow(flow, {
|
|
47
|
+
...(object === undefined ? {} : { object }),
|
|
48
|
+
...(params === undefined ? {} : { params }),
|
|
49
|
+
...(access === undefined ? {} : { access }),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Answers the person wait `run` is in and runs on to the next end or wait.
|
|
55
|
+
* `waitId` must be the id of the wait the run is in now.
|
|
56
|
+
*/
|
|
57
|
+
export function answer(
|
|
58
|
+
run: FlowRun,
|
|
59
|
+
waitId: string,
|
|
60
|
+
choice: string,
|
|
61
|
+
note?: string,
|
|
62
|
+
): Promise<FlowRun> {
|
|
63
|
+
return answerRunWait(run, waitId, choice, note);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Lets a run go on, for example after the clock passed a time wait. */
|
|
67
|
+
export function resume(run: FlowRun): Promise<FlowRun> {
|
|
68
|
+
return resumeRun(run);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Sends one event through the plugin's trigger rules in a world of its own, as
|
|
73
|
+
* a source plugin would. To send several (a repeated key, a chain of runs), use
|
|
74
|
+
* `testWorld(plugin).emit`.
|
|
75
|
+
*/
|
|
76
|
+
export async function emit(
|
|
77
|
+
plugin: PluginUnderTest,
|
|
78
|
+
source: string,
|
|
79
|
+
type: string,
|
|
80
|
+
data?: JsonValue,
|
|
81
|
+
options: EmitOptions & Parameters<typeof testWorld>[1] = {},
|
|
82
|
+
): Promise<EmitResult> {
|
|
83
|
+
const { key, object, summary, ...worldOptions } = options;
|
|
84
|
+
const world = await testWorld(plugin, worldOptions);
|
|
85
|
+
return world.emit(source, type, data, {
|
|
86
|
+
...(key === undefined ? {} : { key }),
|
|
87
|
+
...(object === undefined ? {} : { object }),
|
|
88
|
+
...(summary === undefined ? {} : { summary }),
|
|
89
|
+
});
|
|
90
|
+
}
|