@niwa-labs/workflow 0.0.0-stage → 0.0.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 +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,2813 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The workflow module: a named script of steps, run in any harness.
|
|
3
|
+
*
|
|
4
|
+
* - `workflow.register`, `workflow.list`, `workflow.get` — a script registered as a workflow `wf-…`, with
|
|
5
|
+
* versions (the hash of its source closure); every registered workflow is the action `flow.<id>`;
|
|
6
|
+
* - `workflow.start` — start a run `run-…` of a registered workflow by its id, or of a script file once;
|
|
7
|
+
* - `workflow.resume` — start a run again from the top with the file and arguments it was started
|
|
8
|
+
* with, and the calls that finished are answered from the journal; a script that was edited in
|
|
9
|
+
* place is a revision: what it would show is found first, and the run pauses on it;
|
|
10
|
+
* - `workflow.revise` — report what an edited script reuses, reruns and shows, then run it unless
|
|
11
|
+
* something is shown;
|
|
12
|
+
* - `workflow.pause` and `workflow.continue` — hold and release the same invocation at a script boundary;
|
|
13
|
+
* admitted operations can finish, and the pause survives a host restart on the same plugin version.
|
|
14
|
+
* - `workflow.cancel` — end a run, and interrupt the sessions it started; resume, revise, cancel and
|
|
15
|
+
* uninstall are for whoever started the run or made the installation, and for the owner;
|
|
16
|
+
* - `workflow.check` — type-check a script against the actions the caller may call, and read what it declares, without
|
|
17
|
+
* running it; `workflow.start` refuses a script that does not pass;
|
|
18
|
+
* - `workflow.guide` — how to write a script, with the generated typing and the action catalogue;
|
|
19
|
+
* - `workflow.install`, `workflow.uninstall`, `workflow.installation.list`, `workflow.installation.get` — a
|
|
20
|
+
* script with `on` that niwa itself starts, one run per record that matches, under the profile `wf:<sub-…>`.
|
|
21
|
+
*
|
|
22
|
+
* A script is `export default workflow({ input, output, ... }, fn)`: the module is loaded in a
|
|
23
|
+
* disposable process to read its schemas (`describe.ts`), the arguments of a start are held to `input`
|
|
24
|
+
* before a run exists, the result to `output` when it ends (`workflow.run.finished` carries a typed
|
|
25
|
+
* outcome), and the run keeps the hash of its contract: a resume under another one stops on the
|
|
26
|
+
* `revise` pause when what the run recorded no longer passes.
|
|
27
|
+
*
|
|
28
|
+
* A run is an invocation of the plugin's handler, so its calls are steps of that invocation. A
|
|
29
|
+
* failure pauses the run with the choices of the core. The workflow does not repair itself.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
33
|
+
import { readFileSync } from "node:fs";
|
|
34
|
+
|
|
35
|
+
import { z } from "zod";
|
|
36
|
+
|
|
37
|
+
import { AccessGranted, CANCEL_STEPS_ACTION, CORE_TYPES, DESCRIPTION_MAX, DESCRIPTION_MIN, DeniedError, EntityDescription, IdIndex, JsonValueSchema, RecordRefused, RESOLVE_ACTION, action, cancelHolds, keyUnder, runIdOfPrincipal, callerOf, fillRule, grant, isRuleTemplate, jsonOf, narrow, principalOf, record, recordId, rule, seq, stableJson, ruleText, specsOf, storedOutcome, toJsonSchema } from "@niwa-labs/core";
|
|
38
|
+
import { COCKPIT_MODULE, plugin } from "@niwa-labs/plugin";
|
|
39
|
+
import { fileURLToPath } from "node:url";
|
|
40
|
+
import { join } from "node:path";
|
|
41
|
+
import { HOST_KEY_FILE, OWNER_TOKEN_FILE } from "@niwa-labs/core";
|
|
42
|
+
import { BUILTIN_WORKFLOW, PENDING_EXTERNAL, PendingExternalAnswerSchema, SCRIPT_PACKAGE } from "@niwa-labs/plugin";
|
|
43
|
+
import type { ServiceContext, Subscriber } from "@niwa-labs/plugin";
|
|
44
|
+
import type { Action, ActionContext, ArtifactStore, Caller, Grant, Handler, IssuedId, JournalReader, JsonSchema, Principal, RecordType, RuleSpec, Seq, StepsCancelled, StoredRecord } from "@niwa-labs/core";
|
|
45
|
+
import { scriptOn } from "@niwa-labs/script";
|
|
46
|
+
import { requireBox } from "@niwa-labs/sandbox";
|
|
47
|
+
import type { BoxSandboxName } from "@niwa-labs/sandbox";
|
|
48
|
+
import { requestCancel } from "@niwa-labs/requests/records";
|
|
49
|
+
import { SessionEnded, SessionMarked, SessionStarted, SessionStartReserved, sessionEnd, sessionInterrupt } from "@niwa-labs/sessions/records";
|
|
50
|
+
|
|
51
|
+
import { AskCancelled, AskExpired, bridge } from "./bridge.ts";
|
|
52
|
+
import {
|
|
53
|
+
workflowCancel,
|
|
54
|
+
workflowStop,
|
|
55
|
+
workflowCheck,
|
|
56
|
+
workflowContinue,
|
|
57
|
+
workflowExtend,
|
|
58
|
+
workflowPause,
|
|
59
|
+
workflowBuiltinPromote,
|
|
60
|
+
workflowBuiltinTrial,
|
|
61
|
+
workflowGet,
|
|
62
|
+
workflowGuide,
|
|
63
|
+
workflowInspect,
|
|
64
|
+
workflowInstall,
|
|
65
|
+
workflowInstallationGet,
|
|
66
|
+
workflowInstallationList,
|
|
67
|
+
workflowList,
|
|
68
|
+
workflowRegister,
|
|
69
|
+
workflowResume,
|
|
70
|
+
workflowRevise,
|
|
71
|
+
workflowRuns,
|
|
72
|
+
workflowStart,
|
|
73
|
+
workflowUninstall,
|
|
74
|
+
workflowWait,
|
|
75
|
+
} from "./actions.ts";
|
|
76
|
+
import { actionName, holds } from "@niwa-labs/core";
|
|
77
|
+
import { catalogueFor } from "./catalogue.ts";
|
|
78
|
+
import { neutralValue } from "./ask.ts";
|
|
79
|
+
import { isUse, scriptChecker, scriptPhases, scriptReads, scriptUses, useName, withDeclaration } from "./check.ts";
|
|
80
|
+
import { describer } from "./describe-cache.ts";
|
|
81
|
+
import { contractOf, emittedTypesOf } from "./describe.ts";
|
|
82
|
+
import type { Described } from "./describe.ts";
|
|
83
|
+
import type { CheckError, CheckReport } from "./check.ts";
|
|
84
|
+
import { guideFor } from "./guide.ts";
|
|
85
|
+
import { inspectRun } from "./inspect.ts";
|
|
86
|
+
import { SCRIPT_TYPES, WorkflowPhases, agentActionName } from "./language.ts";
|
|
87
|
+
import { sessionAgent } from "./agent.ts";
|
|
88
|
+
import type { HarnessLaunch } from "./agent.ts";
|
|
89
|
+
import {
|
|
90
|
+
Builtin,
|
|
91
|
+
BuiltinsWanted,
|
|
92
|
+
BuiltinPromoted,
|
|
93
|
+
BuiltinTrial,
|
|
94
|
+
InstallationId,
|
|
95
|
+
ContractHash,
|
|
96
|
+
Issue,
|
|
97
|
+
JsonSchemaValue,
|
|
98
|
+
RunFinished,
|
|
99
|
+
RunLog,
|
|
100
|
+
RunOutcome,
|
|
101
|
+
RunPaused,
|
|
102
|
+
RunPauseRequested,
|
|
103
|
+
RunDeadlineExtended,
|
|
104
|
+
RunHoldRequested,
|
|
105
|
+
RunContinued,
|
|
106
|
+
PhaseEntered,
|
|
107
|
+
PhaseLeft,
|
|
108
|
+
ScopeBranched,
|
|
109
|
+
ScopeClosed,
|
|
110
|
+
ScopeOpened,
|
|
111
|
+
StepPhase,
|
|
112
|
+
RunId,
|
|
113
|
+
RunRequested,
|
|
114
|
+
RunStarted,
|
|
115
|
+
ScriptRecord,
|
|
116
|
+
SubscriptionInstalled,
|
|
117
|
+
SubscriptionUninstalled,
|
|
118
|
+
WORKFLOW_TYPES,
|
|
119
|
+
WorkflowRechecked,
|
|
120
|
+
WorkflowRegistered,
|
|
121
|
+
WantedBuiltin,
|
|
122
|
+
WorkflowVersion,
|
|
123
|
+
} from "./records.ts";
|
|
124
|
+
import type { EmittedRecord, FlowCall, Subscription, WorkflowId } from "./records.ts";
|
|
125
|
+
import { builtinOf, builtinOfferOf, declaredBy, dependentsOf, flowActionName, readersOf, versionOf, workflowIdsOf, workflowsOf } from "./registry.ts";
|
|
126
|
+
import { packageRoots } from "./installation.ts";
|
|
127
|
+
import { linkOfferedPackages, offeredPackageOf, storedEntry, storedVersion, storeVersion, workflowsDir } from "./store.ts";
|
|
128
|
+
import type { OfferedPackage } from "./store.ts";
|
|
129
|
+
import type { RegisteredWorkflow } from "./registry.ts";
|
|
130
|
+
import { decide } from "./revise.ts";
|
|
131
|
+
import type { CurrentCall, RecordedCall, RevisionReport } from "./revise.ts";
|
|
132
|
+
import { runScript, stopRun } from "./runner.ts";
|
|
133
|
+
import { StatsFold, workflowStats } from "./stats.ts";
|
|
134
|
+
import type { ScriptBox } from "./runner.ts";
|
|
135
|
+
import { activeHold, activeManualPause, deadlineOf, deadlineReached, holdOver, manualPausesOf, openRequestsOfInvocations, parentRunOf, runEntry, runExists, runIds, runLine, statusOf } from "./status.ts";
|
|
136
|
+
import type { PendingOperations, RunState as RunStateName, RunStatus } from "./status.ts";
|
|
137
|
+
import { installationIdsOf, installationsOf, profileOf, profileRulesOf, standing } from "./subscriptions.ts";
|
|
138
|
+
import type { Installation } from "./subscriptions.ts";
|
|
139
|
+
|
|
140
|
+
/** What the module is given. */
|
|
141
|
+
export interface WorkflowOptions {
|
|
142
|
+
/** The action every agent call goes to. Default: `session.start.<harness>`. A test points it at a stub. */
|
|
143
|
+
agentAction?: Action<any, any>;
|
|
144
|
+
/** The actions agent calls go to, by harness name; `agentAction` serves the harnesses not listed. */
|
|
145
|
+
agents?: Record<string, Action<any, any>>;
|
|
146
|
+
/**
|
|
147
|
+
* The harnesses a script may name.
|
|
148
|
+
*
|
|
149
|
+
* The plugin registers the agent action of each of them — `session.start.<harness>` — so a
|
|
150
|
+
* script that names a harness reaches the agents of that harness with no hand glue. A harness given with the
|
|
151
|
+
* `models` or `efforts` its adapter knows holds a script to them: a model or an effort outside them is an error of
|
|
152
|
+
* the check, and of the call. The options block a harness takes is the one the sessions module binds it to
|
|
153
|
+
* (`harness.list`): the block of another kind is an error of the check, and of the call.
|
|
154
|
+
*/
|
|
155
|
+
harnesses?: ReadonlyArray<string | ({ readonly id: string } & HarnessLaunch)>;
|
|
156
|
+
/** The directory relative script paths are resolved against. */
|
|
157
|
+
root?: string;
|
|
158
|
+
/**
|
|
159
|
+
* The record types a script may `emit`: only the script's own, `wf.<name>.*`.
|
|
160
|
+
*
|
|
161
|
+
* Record types are declared when a plugin is installed, so the types a script writes are given
|
|
162
|
+
* here and the plugin declares them.
|
|
163
|
+
*/
|
|
164
|
+
emits?: readonly RecordType<any>[];
|
|
165
|
+
/**
|
|
166
|
+
* The record types a script may start on (`on`).
|
|
167
|
+
*
|
|
168
|
+
* Handlers are registered when a plugin is installed, so the types a script may subscribe to are
|
|
169
|
+
* given here, as the types it may `emit` are: as types, or by name, which is resolved against the
|
|
170
|
+
* types of the plugins installed before this one. A script that lists another type does not pass
|
|
171
|
+
* its check, and so cannot be installed.
|
|
172
|
+
*
|
|
173
|
+
* The records of scripts, `wf.*`, need no entry: any script may start on what another one emits.
|
|
174
|
+
*/
|
|
175
|
+
triggers?: ReadonlyArray<RecordType<any> | string>;
|
|
176
|
+
/**
|
|
177
|
+
* Run the script processes in an empty box when a start or an installation does not say. Default:
|
|
178
|
+
* no box, a plain child process; the owner usually runs without one.
|
|
179
|
+
*/
|
|
180
|
+
box?: boolean;
|
|
181
|
+
/** The sandbox of a box. Default: `srt`. */
|
|
182
|
+
sandbox?: BoxSandboxName;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Where a run came from: the workflow and version it runs, the installation that started it. */
|
|
186
|
+
interface RunOrigin {
|
|
187
|
+
workflow?: WorkflowId;
|
|
188
|
+
version?: WorkflowVersion;
|
|
189
|
+
installation?: InstallationId;
|
|
190
|
+
directory?: string;
|
|
191
|
+
parent?: { run: string; step: string };
|
|
192
|
+
concurrency?: number;
|
|
193
|
+
limits?: { depth?: number; deadline?: number };
|
|
194
|
+
allow?: { rules: string[]; version: string };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A run as `workflow.run.started` records it. */
|
|
198
|
+
interface RunFacts extends RunOrigin {
|
|
199
|
+
file: string;
|
|
200
|
+
args: unknown;
|
|
201
|
+
label?: string;
|
|
202
|
+
/** The hash of the contract the run began under, when it could be read. */
|
|
203
|
+
contract?: ContractHash;
|
|
204
|
+
/** The hash of the script file at the start of the run, when it could be read. */
|
|
205
|
+
hash?: string;
|
|
206
|
+
/** Whether the script process runs in a box, as the run was born. */
|
|
207
|
+
box?: boolean;
|
|
208
|
+
/** The invocations that ran the script, one per start. */
|
|
209
|
+
invocations: string[];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* What the latest invocation of a run is doing.
|
|
214
|
+
*
|
|
215
|
+
* A run is an invocation of the plugin's handler, and a resume or a revision starts another
|
|
216
|
+
* one: the two would write over the same children of the run. So the state of the latest
|
|
217
|
+
* invocation is what says whether the run may be started again.
|
|
218
|
+
*/
|
|
219
|
+
type RunState = "running" | "waiting" | "paused" | "done";
|
|
220
|
+
|
|
221
|
+
/** What the sync of one built-in answers, and what the retirement of the ones no plugin offers takes and answers. */
|
|
222
|
+
const BuiltinSynced = z.object({ workflow: z.string(), installation: z.string().optional() });
|
|
223
|
+
const BuiltinRetireInput = z.object({ keep: z.array(Builtin) });
|
|
224
|
+
const BuiltinRetired = z.object({ uninstalled: z.array(z.string()) });
|
|
225
|
+
|
|
226
|
+
/** How long `workflow.wait` waits when it is not told, in milliseconds. */
|
|
227
|
+
const DEFAULT_WAIT_MS = 60_000;
|
|
228
|
+
|
|
229
|
+
/** How deep runs may call other workflows: a run whose request has more runs above it does not start. */
|
|
230
|
+
const CALL_DEPTH = 16;
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
/** The plugin of the module. */
|
|
234
|
+
export function workflow(opts: WorkflowOptions = {}) {
|
|
235
|
+
// The agent action of every harness the plugin was given: a script that names a harness
|
|
236
|
+
// reaches the agents of that harness, and nothing else is asked of the host.
|
|
237
|
+
const harnessActions = new Map<string, Action<any, any>>();
|
|
238
|
+
for (const harness of opts.harnesses ?? []) {
|
|
239
|
+
const { id, ...launch } = typeof harness === "string" ? { id: harness } : harness;
|
|
240
|
+
harnessActions.set(id, sessionAgent(id, launch));
|
|
241
|
+
}
|
|
242
|
+
const actionFor = (harness: string | undefined): Action<any, any> | undefined => {
|
|
243
|
+
if (harness === undefined) return opts.agentAction;
|
|
244
|
+
return opts.agents?.[harness] ?? harnessActions.get(harness) ?? opts.agentAction;
|
|
245
|
+
};
|
|
246
|
+
const actionNameOf = (harness: string | undefined): string =>
|
|
247
|
+
String(actionFor(harness)?.name ?? agentActionName(harness));
|
|
248
|
+
|
|
249
|
+
const checkScript = scriptChecker();
|
|
250
|
+
|
|
251
|
+
const emits = new Map<string, RecordType<any>>();
|
|
252
|
+
for (const type of opts.emits ?? []) {
|
|
253
|
+
if (!type.name.startsWith(SCRIPT_TYPES)) {
|
|
254
|
+
throw new TypeError(`workflow({ emits }): a script may emit only "${SCRIPT_TYPES}<name>.*" types, not "${type.name}"`);
|
|
255
|
+
}
|
|
256
|
+
emits.set(type.name, type);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return plugin("workflow", (api) => {
|
|
260
|
+
/** The clock of the host, once it serves: what the deadline of a group, and of a run, is measured by. */
|
|
261
|
+
let hostClock: ServiceContext | undefined;
|
|
262
|
+
/** The moment now, by the clock of the host once it serves. */
|
|
263
|
+
const clockNow = (): number => hostClock?.now() ?? Date.now();
|
|
264
|
+
const scheduleAt = (at: number, fn: () => void): (() => void) => {
|
|
265
|
+
if (hostClock !== undefined) return hostClock.schedule(at, fn);
|
|
266
|
+
const timer = setTimeout(fn, Math.max(0, at - Date.now()));
|
|
267
|
+
return () => clearTimeout(timer);
|
|
268
|
+
};
|
|
269
|
+
// The page of this plugin in the cockpit: a client module, offered by the path of its source.
|
|
270
|
+
api.offer(COCKPIT_MODULE, { id: "workflow", entry: fileURLToPath(new URL("./cockpit/index.tsx", import.meta.url)) });
|
|
271
|
+
api.declare(...WORKFLOW_TYPES, ...emits.values());
|
|
272
|
+
|
|
273
|
+
const triggers = new Map<string, RecordType<any>>();
|
|
274
|
+
for (const entry of opts.triggers ?? []) {
|
|
275
|
+
const type = typeof entry === "string" ? (emits.get(entry) ?? api.recordType(entry)) : entry;
|
|
276
|
+
if (type !== undefined) triggers.set(type.name, type);
|
|
277
|
+
else if (typeof entry === "string" && !entry.startsWith(SCRIPT_TYPES)) {
|
|
278
|
+
throw new TypeError(
|
|
279
|
+
`workflow({ triggers }): "${entry}" is not a record type of a plugin installed before workflow: install the plugin that declares it first`,
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
// The built-ins the plugins installed before this one offer: the types they start on are triggers as the given ones are.
|
|
284
|
+
// A plugin installed after this one that offers a built-in is refused by the host: it would never be installed.
|
|
285
|
+
for (const { plugin: by, value } of api.offers(BUILTIN_WORKFLOW)) {
|
|
286
|
+
for (const entry of value.triggers ?? []) {
|
|
287
|
+
const type = typeof entry === "string" ? api.recordType(entry) : entry;
|
|
288
|
+
if (type === undefined) {
|
|
289
|
+
throw new TypeError(
|
|
290
|
+
`the built-in workflow "${value.name}" of "${by}" starts on "${String(entry)}", a record type no plugin installed before workflow declares`,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
if (!type.name.startsWith(SCRIPT_TYPES)) triggers.set(type.name, type);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The packages the plugins give scripts from outside niwa's tree, read once every plugin is installed: a stored copy
|
|
298
|
+
* finds them, and a box may read them and what they load. An offer that is not a niwa package at its folder, or a
|
|
299
|
+
* second folder for a name, is not given to scripts, and the host says why when it starts.
|
|
300
|
+
*/
|
|
301
|
+
let offered: { packages: OfferedPackage[]; roots: string[]; refused: string[] } | undefined;
|
|
302
|
+
const scriptPackages = (): { packages: OfferedPackage[]; roots: string[]; refused: string[] } => {
|
|
303
|
+
if (offered !== undefined) return offered;
|
|
304
|
+
const packages: OfferedPackage[] = [];
|
|
305
|
+
const refused: string[] = [];
|
|
306
|
+
for (const { plugin: by, value } of api.offers(SCRIPT_PACKAGE)) {
|
|
307
|
+
const found = offeredPackageOf(value.name, value.root);
|
|
308
|
+
const known = typeof found === "string" ? undefined : packages.find((one) => one.name === found.name);
|
|
309
|
+
if (typeof found === "string") refused.push(`the package "${by}" offers scripts is not given to them: ${found}`);
|
|
310
|
+
else if (known !== undefined && known.root !== found.root) refused.push(`${found.name} is offered to scripts from ${known.root}, so not from ${found.root}`);
|
|
311
|
+
else if (known === undefined) packages.push(found);
|
|
312
|
+
}
|
|
313
|
+
offered = { packages, roots: packages.flatMap((one) => packageRoots(one.root)), refused };
|
|
314
|
+
return offered;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// The types a script may start on, with the schema of their data: the triggers, and the records of scripts that have a schema.
|
|
318
|
+
const triggerSchemas = [...new Map([...emits, ...triggers]).values()].map((type) => ({
|
|
319
|
+
name: type.name,
|
|
320
|
+
schema: toJsonSchema(type.schema, "input"),
|
|
321
|
+
}));
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The types a script may start on, wait for and read, with the schema of their data: the ones above, and the record
|
|
325
|
+
* types the registered workflows declare in `emits` (the latest version of each), which grow from the journal as
|
|
326
|
+
* `flow.<id>` does. A type the composition gives keeps its own schema.
|
|
327
|
+
*/
|
|
328
|
+
const triggersNow = (): Array<{ name: string; schema: JsonSchema }> => {
|
|
329
|
+
const known = new Map<string, { name: string; schema: JsonSchema }>(triggerSchemas.map((entry) => [entry.name, entry]));
|
|
330
|
+
if (api.journal?.() === undefined) return [...known.values()];
|
|
331
|
+
for (const registered of workflowsOf(journal())) {
|
|
332
|
+
for (const declared of registered.latest.emits ?? []) {
|
|
333
|
+
if (!known.has(declared.type)) known.set(declared.type, { name: declared.type, schema: declared.schema });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return [...known.values()];
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/** The version of a record type the host knows: what a script that names the type by reference must name. */
|
|
340
|
+
const recordVersion = (name: string): number | undefined => (emits.get(name) ?? api.recordType(name))?.version;
|
|
341
|
+
|
|
342
|
+
/** The schema of the data of a record type the host knows: what a script that waits for it or reads it is typed by. */
|
|
343
|
+
const recordSchema = (name: string): JsonSchema | undefined => {
|
|
344
|
+
const type = emits.get(name) ?? api.recordType(name);
|
|
345
|
+
return type === undefined ? undefined : toJsonSchema(type.schema, "output");
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// The agent actions of the harnesses are installed by the host: a script that names a
|
|
349
|
+
// harness reaches the agents of that harness with no hand glue.
|
|
350
|
+
for (const harnessAction of harnessActions.values()) api.action(harnessAction);
|
|
351
|
+
|
|
352
|
+
/** The box of a script process, when the run is boxed. */
|
|
353
|
+
const boxFor = (boxed: boolean | undefined): ScriptBox | undefined =>
|
|
354
|
+
boxed === true
|
|
355
|
+
? {
|
|
356
|
+
sandbox: opts.sandbox ?? "srt",
|
|
357
|
+
closed: [join(api.home(), OWNER_TOKEN_FILE), join(api.home(), HOST_KEY_FILE)],
|
|
358
|
+
read: [workflowsDir(api.home()), ...scriptPackages().roots],
|
|
359
|
+
}
|
|
360
|
+
: undefined;
|
|
361
|
+
|
|
362
|
+
/** The box a run or an installation asked for, or else the module's own choice. */
|
|
363
|
+
const boxOf = async (asked: boolean | undefined): Promise<boolean> => {
|
|
364
|
+
const boxed = asked ?? opts.box ?? false;
|
|
365
|
+
// A box that cannot be had here stops the start before anything is made of it.
|
|
366
|
+
if (boxed) await requireBox({ sandbox: opts.sandbox ?? "srt", spec: { cwd: process.cwd() } });
|
|
367
|
+
return boxed;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* What a module declares, read once for a version of the script: a start that checked it a moment ago, a run that
|
|
372
|
+
* begins any other way (a resume, a restart, a record) and a version the journal registered do not read it again.
|
|
373
|
+
* Registrations are checked in the box the module is given, so only that box takes what they say.
|
|
374
|
+
*/
|
|
375
|
+
const describe = describer({
|
|
376
|
+
known: (version, boxed) => (boxed === (opts.box ?? false) && api.journal?.() !== undefined ? declaredBy(journal(), version) : undefined),
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* The types a run may emit, by the name the script gives them: what it declares in `emits`, under `wf.<name>.`, with
|
|
381
|
+
* the schema it declares, or the composition's own type of that name.
|
|
382
|
+
*/
|
|
383
|
+
const emitTypesOf = (described: Described | undefined): Map<string, RecordType<unknown>> =>
|
|
384
|
+
new Map(
|
|
385
|
+
described === undefined
|
|
386
|
+
? []
|
|
387
|
+
: emittedTypesOf({ described }).map(({ record: name, type, schema }) => [
|
|
388
|
+
name,
|
|
389
|
+
emits.get(type) ?? record(type, z.fromJSONSchema(schema as Parameters<typeof z.fromJSONSchema>[0])),
|
|
390
|
+
]),
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
/** The script against the actions its caller may call. */
|
|
394
|
+
const checked = async (ctx: ActionContext, source: { file: string; text: string }, boxed: boolean): Promise<CheckReport> => {
|
|
395
|
+
const text = await checkScript(source, { ...(await catalogueFor(ctx, api.rights(ctx.caller))), triggers: triggersNow(), records: recordSchema, versions: recordVersion });
|
|
396
|
+
return withDeclaration(text, describe, boxFor(boxed));
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const journal = (): JournalReader => {
|
|
400
|
+
const reader = api.journal?.();
|
|
401
|
+
if (reader === undefined) throw new Error("the journal is not open yet");
|
|
402
|
+
return reader;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
// The runs by state, for `niwa.host.status`.
|
|
406
|
+
api.summarize(() => {
|
|
407
|
+
const counts: Record<RunStateName, number> = { running: 0, paused: 0, finished: 0 };
|
|
408
|
+
for (const run of runIds(journal())) counts[statusOf(journal(), run).state] += 1;
|
|
409
|
+
return counts;
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const settled = (status: RunStatus, includeManual: boolean) =>
|
|
413
|
+
status.state === "finished" ||
|
|
414
|
+
(status.state === "paused" && (includeManual || status.manualPause === undefined || status.invocation !== undefined));
|
|
415
|
+
|
|
416
|
+
/** The run as it is once it finishes or pauses, or as it is when the time is over. */
|
|
417
|
+
const settle = async (run: string, timeoutMs: number = DEFAULT_WAIT_MS, includeManual = true): Promise<RunStatus> => {
|
|
418
|
+
const deadline = Date.now() + timeoutMs;
|
|
419
|
+
for (;;) {
|
|
420
|
+
const status = statusOf(journal(), run);
|
|
421
|
+
const left = deadline - Date.now();
|
|
422
|
+
if (settled(status, includeManual) || left <= 0) return status;
|
|
423
|
+
await api.untilCommit(left);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The status of a run, with what plugins began outside niwa and have not ended when the run is held: the hold stops
|
|
429
|
+
* the run's own writes, and these go on until someone calls them off. Each plugin's listing is read now, as a step
|
|
430
|
+
* of the call; a plugin that cannot be asked is said so, its operations are not taken for none.
|
|
431
|
+
*/
|
|
432
|
+
const showing = async (status: RunStatus, ctx: ActionContext): Promise<RunStatus> => {
|
|
433
|
+
if (status.hold === undefined) return status;
|
|
434
|
+
const offered = api.offers(PENDING_EXTERNAL);
|
|
435
|
+
if (offered.length === 0) return status;
|
|
436
|
+
const pending: PendingOperations[] = [];
|
|
437
|
+
for (const { plugin: by, value } of offered) {
|
|
438
|
+
const listed = await ctx.step.outcome(actionName(value.list), {});
|
|
439
|
+
const answer = listed.status === "completed" ? PendingExternalAnswerSchema.safeParse(listed.value) : undefined;
|
|
440
|
+
if (answer?.success === true) {
|
|
441
|
+
if (answer.data.items.length > 0) pending.push({ plugin: String(by), label: value.label, items: answer.data.items });
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
const why = listed.status === "completed" ? "it did not answer `{ items }`" : listed.status === "failed" ? listed.error : listed.status === "denied" ? listed.reason : listed.status;
|
|
445
|
+
pending.push({ plugin: String(by), label: value.label, items: [], unavailable: `${value.list}: ${why}` });
|
|
446
|
+
}
|
|
447
|
+
return pending.length === 0 ? status : { ...status, hold: { ...status.hold, pending } };
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const runIndex = new IdIndex("run", () => runsIssuedOf(journal()));
|
|
451
|
+
const workflowIndex = new IdIndex("workflow", () => workflowIdsOf(journal()));
|
|
452
|
+
const installationIndex = new IdIndex("subscription", () => installationIdsOf(journal()));
|
|
453
|
+
|
|
454
|
+
/** The run a text names: its id, or a unique start or end of it, among the runs the caller may act on. */
|
|
455
|
+
const runOf = (text: string, caller: Caller): RunId => {
|
|
456
|
+
const reader = journal();
|
|
457
|
+
const mine = runIndex.resolve(text, (run) => isOwnerOrStarter(caller, ownerOfRun(reader, run)));
|
|
458
|
+
// The whole id of a run of another is named all the same, so the refusal says whose it is; a part of one names nothing.
|
|
459
|
+
return mine ?? runIndex.require(text, (run) => run === text);
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
/** The registered workflow a text names. */
|
|
463
|
+
const workflowOf = (text: string): RegisteredWorkflow => {
|
|
464
|
+
const id = workflowIndex.require(text);
|
|
465
|
+
const found = workflowsOf(journal()).find((one) => one.id === id);
|
|
466
|
+
if (found === undefined) throw new RunRefusal("unknown-workflow", `no workflow "${text}"`);
|
|
467
|
+
return found;
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
/** The copy of a registered version niwa keeps in its home: what the version runs and is checked on. */
|
|
471
|
+
const storedOf = (id: WorkflowId, version: WorkflowVersion): string => {
|
|
472
|
+
const stored = storedVersion(api.home(), id, version);
|
|
473
|
+
if (stored === undefined) throw new RunRefusal("not-stored", `niwa holds no code of the version ${version} of ${id}: register it again`);
|
|
474
|
+
if ("problem" in stored) throw new RunRefusal("not-the-version", stored.problem);
|
|
475
|
+
return stored.entry;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
/** Only a run of the shipped suite can create or finish its own trial. */
|
|
479
|
+
const trialRun = (caller: Caller): { run: RunId; plugin: string } => {
|
|
480
|
+
if (caller.kind !== "client" || caller.principal?.kind !== "run") throw new DeniedError("only a designated built-in suite may manage a trial");
|
|
481
|
+
const run = RunId.parse(runIdOfPrincipal(caller.principal.id));
|
|
482
|
+
const facts = factsOf(journal(), run);
|
|
483
|
+
const builtin = workflowsOf(journal()).find((one) => one.id === facts?.workflow)?.builtin;
|
|
484
|
+
if (builtin !== undefined && facts?.workflow !== undefined && facts.version !== undefined && api.offers(BUILTIN_WORKFLOW).some(({ plugin, value }) =>
|
|
485
|
+
plugin === builtin.plugin && value.name === builtin.name && value.suite === true)) {
|
|
486
|
+
const archived = storedVersion(api.home(), facts.workflow, facts.version);
|
|
487
|
+
if (archived !== undefined && !("problem" in archived) && archived.entry === facts.file) return { run, plugin: builtin.plugin };
|
|
488
|
+
}
|
|
489
|
+
throw new DeniedError("only a designated built-in suite may manage a trial");
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const trialOf = (run: string) => {
|
|
493
|
+
const row = journal().find({ type: BuiltinTrial.name, field: "run", equals: run }).at(-1);
|
|
494
|
+
return row === undefined ? undefined : BuiltinTrial.schema.parse(row.data);
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
/** Trial authority follows the journaled parent of a run. It is absent from workflow inputs. */
|
|
498
|
+
const inheritedTrial = (caller: Caller | undefined) => {
|
|
499
|
+
if (caller?.kind !== "client" || caller.principal?.kind !== "run") return undefined;
|
|
500
|
+
const seen = new Set<string>();
|
|
501
|
+
for (let run: string | undefined = runIdOfPrincipal(caller.principal.id); run !== undefined && !seen.has(run); run = parentRunOf(journal(), run)) {
|
|
502
|
+
seen.add(run);
|
|
503
|
+
const trial = trialOf(run);
|
|
504
|
+
if (trial === undefined) continue;
|
|
505
|
+
try {
|
|
506
|
+
trialRun({ ...caller, principal: principalOf("run", run) });
|
|
507
|
+
return trial;
|
|
508
|
+
} catch (error) {
|
|
509
|
+
if (!(error instanceof DeniedError)) throw error;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return undefined;
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
/** A registered start uses its accepted version, or the version fixed by a trusted trial. */
|
|
516
|
+
const registeredTarget = (registered: RegisteredWorkflow, version?: WorkflowVersion, caller?: Caller): { file: string } & RunOrigin => {
|
|
517
|
+
const trial = registered.builtin === undefined ? undefined : inheritedTrial(caller);
|
|
518
|
+
const pinned = trial?.entries.find((one) => one.workflow === registered.id)?.version;
|
|
519
|
+
if (pinned !== undefined && version !== undefined && version !== pinned) {
|
|
520
|
+
throw new RunRefusal("trial-version", `the suite trial for ${registered.id} pins ${pinned}, not ${version}`);
|
|
521
|
+
}
|
|
522
|
+
const selected = version ?? pinned;
|
|
523
|
+
const target = selected === undefined ? registered.latest : registered.versions.find((one) => one.version === selected);
|
|
524
|
+
if (target === undefined) throw new RunRefusal("unknown-version", `the workflow ${registered.id} has no version ${selected}`);
|
|
525
|
+
if ((target.candidate === true || target.superseded === true) && pinned !== target.version) {
|
|
526
|
+
throw new RunRefusal("candidate", `the version ${target.version} of ${registered.id} is an unaccepted candidate: only its designated suite trial may start it`);
|
|
527
|
+
}
|
|
528
|
+
if (target.broken !== undefined) throw new RunRefusal("workflow-broken", `the version ${target.version} of ${registered.id} is broken, it does not start: ${target.broken}`);
|
|
529
|
+
return { file: storedOf(registered.id, target.version), workflow: registered.id, version: target.version };
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
const known = (run: string): RunFacts => {
|
|
533
|
+
const facts = factsOf(journal(), run);
|
|
534
|
+
if (facts === undefined) throw new Error(`unknown run "${run}"`);
|
|
535
|
+
return facts;
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* The run may be started again only when the invocation that is going has ended.
|
|
540
|
+
*
|
|
541
|
+
* A run is one invocation of the handler, and a resume or a revision starts another one.
|
|
542
|
+
* Two invocations of the same run would write over the same children of the run, so a run
|
|
543
|
+
* that is still going is refused.
|
|
544
|
+
*
|
|
545
|
+
* A run that is paused waits for a decision: the pause is resolved with a cancel as part of
|
|
546
|
+
* the resume, so the old invocation is ended and the new one starts on a clean run.
|
|
547
|
+
*/
|
|
548
|
+
const guardResumable = async (run: string, ctx: ActionContext): Promise<void> => {
|
|
549
|
+
const requested = journal().find({ type: RunRequested.name, field: "run", equals: run }).at(-1);
|
|
550
|
+
const started = journal().find({ type: RunStarted.name, field: "run", equals: run }).at(-1);
|
|
551
|
+
if (requested !== undefined && (started === undefined || started.seq < requested.seq)) {
|
|
552
|
+
throw new RunRefusal("run-is-going", "the requested execution has not started yet");
|
|
553
|
+
}
|
|
554
|
+
const latest = latestInvocationOf(journal(), run);
|
|
555
|
+
if (latest === undefined) return;
|
|
556
|
+
if (latest.state === "done") return;
|
|
557
|
+
if (latest.state === "paused") {
|
|
558
|
+
// The pause is resolved with a cancel: the old invocation ends, and the run starts again.
|
|
559
|
+
await ctx.step(RESOLVE_ACTION, { invocation: latest.invocation, choice: "cancel" });
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
throw new RunRefusal(
|
|
563
|
+
"run-is-going",
|
|
564
|
+
`the run "${run}" is still going: it cannot be resumed or revised until it ends`,
|
|
565
|
+
);
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* The grant of a new run: `narrow(caller, uses)`, and the script's own `wf.*` records.
|
|
570
|
+
*
|
|
571
|
+
* A script with no `uses` asks for nothing, so its run holds only its own records; a caller
|
|
572
|
+
* that nothing limits (the host, a plugin) has no grant to narrow, so its run has none either,
|
|
573
|
+
* unless the script names what it uses. A mask the caller does not hold refuses the run. A target
|
|
574
|
+
* template of `uses` is filled from the arguments of the run first, and `allow` narrows the result.
|
|
575
|
+
*/
|
|
576
|
+
const grantOfRun = async (asker: Caller, text: string | undefined, file: string, args: unknown, allow?: readonly string[]): Promise<Grant | undefined> => {
|
|
577
|
+
const uses = text === undefined ? undefined : await scriptUses({ file, text });
|
|
578
|
+
const masks = uses !== undefined && "masks" in uses ? filledUses(uses.masks.map((use) => use.mask), args) : undefined;
|
|
579
|
+
if (masks === undefined && api.rights(asker) === "unlimited" && allow === undefined) return undefined;
|
|
580
|
+
const narrowed = masks === undefined && api.rights(asker) === "unlimited" ? { rules: grant(allow ?? []) } : api.narrow(asker, masks ?? []);
|
|
581
|
+
if ("refused" in narrowed) throw new UsesRefused(narrowed.refused.map((text) => text.replace(/^call:/, "")));
|
|
582
|
+
if (allow === undefined) return grant([...specsOf(narrowed.rules), `emit:${SCRIPT_TYPES}*`]);
|
|
583
|
+
// `allow` only narrows: what it names must lie within the run's rights, which it then replaces.
|
|
584
|
+
const within = narrow(narrowed.rules, allow, { irreversible: (name) => api.effectOf(name) === "irreversible" });
|
|
585
|
+
if ("refused" in within) {
|
|
586
|
+
throw new RunRefusal("allow-wider", `allow only narrows the run: ${within.refused.map((text) => text.replace(/^call:/, "")).join(", ")} ${within.refused.length === 1 ? "is" : "are"} not the run's`);
|
|
587
|
+
}
|
|
588
|
+
return grant([...specsOf(within.rules), `emit:${SCRIPT_TYPES}*`]);
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
const latestRequest = (run: string) => {
|
|
592
|
+
const row = journal().find({ type: RunRequested.name, field: "run", equals: run }).at(-1);
|
|
593
|
+
return row === undefined ? undefined : RunRequested.schema.parse(row.data);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
/** A revision keeps its original authority ceiling and receives a fresh, independently closable identity. */
|
|
597
|
+
const requestAgain = async (
|
|
598
|
+
ctx: ActionContext,
|
|
599
|
+
request: ReturnType<typeof requestedOf> & { from?: string },
|
|
600
|
+
previous: string,
|
|
601
|
+
script: ReturnType<typeof readScript>,
|
|
602
|
+
) => changeControl(request.run, async () => {
|
|
603
|
+
if ((latestRequest(request.run)?.execution ?? request.run) !== previous) {
|
|
604
|
+
throw new RunRefusal("run-is-going", "another execution has already been requested for this run");
|
|
605
|
+
}
|
|
606
|
+
const initial = journal().find({ type: AccessGranted.name, field: "principal.id", equals: request.run })
|
|
607
|
+
.map(row => AccessGranted.schema.parse(row.data)).find(data => data.principal.kind === "run");
|
|
608
|
+
const parent = initial?.parent === undefined ? ctx.caller : callerOf(initial.parent.principal);
|
|
609
|
+
const policy = latestRequest(request.run)?.allow;
|
|
610
|
+
const intrinsic = `emit:${SCRIPT_TYPES}*`;
|
|
611
|
+
const declared = await grantOfRun(parent, script?.text, request.file, request.args, policy?.rules);
|
|
612
|
+
const phases = script === undefined ? undefined : await scriptPhases({ file: request.file, text: script.text });
|
|
613
|
+
const execution = `${request.run}/${randomUUID()}`;
|
|
614
|
+
let rules = declared;
|
|
615
|
+
if (initial !== undefined) {
|
|
616
|
+
const requested = (declared === undefined ? initial.rules : specsOf(declared)).filter(spec => spec !== intrinsic);
|
|
617
|
+
const original = narrow(grant(initial.rules), requested, { irreversible: name => api.effectOf(name) === "irreversible" });
|
|
618
|
+
if ("refused" in original) throw new UsesRefused(original.refused);
|
|
619
|
+
const current = api.narrow(parent, specsOf(original.rules));
|
|
620
|
+
if ("refused" in current) throw new UsesRefused(current.refused);
|
|
621
|
+
rules = grant([...specsOf(current.rules), intrinsic]);
|
|
622
|
+
}
|
|
623
|
+
await guardResumable(request.run, ctx);
|
|
624
|
+
if (rules !== undefined) api.issueToken("run", execution, { grant: rules, parent });
|
|
625
|
+
await ctx.emit(RunRequested, {
|
|
626
|
+
...request,
|
|
627
|
+
execution,
|
|
628
|
+
...(script === undefined ? {} : { hash: script.hash }),
|
|
629
|
+
...(policy === undefined ? {} : { allow: policy }),
|
|
630
|
+
...(phases === undefined ? {} : { phases }),
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
api.action(
|
|
635
|
+
action(workflowStart, {
|
|
636
|
+
// The run is asked for: a start that was cut off may have begun one, so it is
|
|
637
|
+
// not made again.
|
|
638
|
+
recovery: "do-not-repeat",
|
|
639
|
+
effect: "record",
|
|
640
|
+
world: "niwa",
|
|
641
|
+
run: async (input, ctx) => {
|
|
642
|
+
if ((input.id === undefined) === (input.file === undefined)) {
|
|
643
|
+
throw new RunRefusal("start-what", "a start names the workflow to run: its id, or a script file, one of the two");
|
|
644
|
+
}
|
|
645
|
+
if (input.version !== undefined && input.id === undefined) throw new RunRefusal("start-what", "a version is the version of a registered workflow: name it with `id`");
|
|
646
|
+
const target = input.id === undefined ? { file: resolveFile(opts.root, input.file ?? "") } : registeredTarget(workflowOf(input.id), input.version, ctx.caller);
|
|
647
|
+
const run = await requestRun(ctx, {
|
|
648
|
+
...target,
|
|
649
|
+
args: input.args,
|
|
650
|
+
...(input.label === undefined ? {} : { label: input.label }),
|
|
651
|
+
...(input.box === undefined ? {} : { box: input.box }),
|
|
652
|
+
...(input.directory === undefined ? {} : { directory: input.directory }),
|
|
653
|
+
...(input.concurrency === undefined ? {} : { concurrency: input.concurrency }),
|
|
654
|
+
...(input.limits === undefined ? {} : { limits: input.limits }),
|
|
655
|
+
...(input.allow === undefined ? {} : { allow: { rules: input.allow, version: createHash("sha256").update(JSON.stringify(input.allow)).digest("hex") } }),
|
|
656
|
+
});
|
|
657
|
+
if (input.wait !== true) return { run, finished: false };
|
|
658
|
+
const status = await settle(run, input.timeoutMs);
|
|
659
|
+
return { ...status, finished: status.state === "finished" };
|
|
660
|
+
},
|
|
661
|
+
}),
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Ask for a run: the script is checked, the arguments held to its input, the run given its grant and its id.
|
|
666
|
+
* Nothing is left behind when it is refused.
|
|
667
|
+
*/
|
|
668
|
+
async function requestRun(
|
|
669
|
+
ctx: ActionContext,
|
|
670
|
+
input: { file: string; args: unknown; label?: string; box?: boolean } & RunOrigin,
|
|
671
|
+
): Promise<RunId> {
|
|
672
|
+
const file = input.file;
|
|
673
|
+
// A child run takes the limits of its parent, and is not started deeper than they allow.
|
|
674
|
+
const limits = input.limits ?? (input.parent === undefined ? undefined : factsOf(journal(), input.parent.run)?.limits);
|
|
675
|
+
// The deadline of the whole run, from now; a child run's comes no later than its parent's.
|
|
676
|
+
const deadline = earliest(
|
|
677
|
+
limits?.deadline === undefined ? undefined : clockNow() + limits.deadline,
|
|
678
|
+
input.parent === undefined ? undefined : deadlineOf(journal(), input.parent.run),
|
|
679
|
+
);
|
|
680
|
+
if (input.parent !== undefined && limits?.depth !== undefined) {
|
|
681
|
+
// The run that started the tree is one deep; each child is one deeper than its parent.
|
|
682
|
+
const depth = depthOf(journal(), input.parent.run) + 2;
|
|
683
|
+
if (depth > limits.depth) {
|
|
684
|
+
throw new RunRefusal("too-deep", `the run would be ${String(depth)} deep in its tree, and its limits allow ${String(limits.depth)}`);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
// A child run takes the concurrency of its parent unless it is given its own.
|
|
688
|
+
const concurrency = input.concurrency ?? (input.parent === undefined ? undefined : factsOf(journal(), input.parent.run)?.concurrency);
|
|
689
|
+
const run = runIndex.issue(ctx.operationId);
|
|
690
|
+
// The script is read once: the text that is checked is the text whose hash is pinned. A
|
|
691
|
+
// script that cannot be read is not refused here, the run that reads it fails as it
|
|
692
|
+
// always did.
|
|
693
|
+
const script = readScript(file);
|
|
694
|
+
const boxed = await boxOf(input.box);
|
|
695
|
+
let accepted: unknown = input.args ?? null;
|
|
696
|
+
let contract: ContractHash | undefined;
|
|
697
|
+
let phases: CheckReport["phases"];
|
|
698
|
+
if (script !== undefined) {
|
|
699
|
+
const report = await checked(ctx, { file, text: script.text }, boxed);
|
|
700
|
+
if (!report.ok) throw new ScriptCheckFailed(report.errors);
|
|
701
|
+
// The arguments are held to the input of the script before a run exists: a refusal leaves nothing behind.
|
|
702
|
+
const on = await scriptOn({ file, text: script.text });
|
|
703
|
+
const records = on !== undefined && "subscriptions" in on;
|
|
704
|
+
const issues = inputIssues(report.input, records, input.args);
|
|
705
|
+
if (issues.length > 0) throw new InputInvalid(issues);
|
|
706
|
+
accepted = report.input === undefined || records ? (input.args ?? null) : (input.args ?? {});
|
|
707
|
+
contract = report.contract;
|
|
708
|
+
phases = report.phases;
|
|
709
|
+
}
|
|
710
|
+
// The run is a subject of its own: what it may do is what the caller may do, narrowed to
|
|
711
|
+
// `uses` as its arguments fill it, and it keeps that grant until it ends.
|
|
712
|
+
const born = await grantOfRun(ctx.caller, script?.text, file, accepted, input.allow?.rules);
|
|
713
|
+
if (born !== undefined) api.issueToken("run", run, { grant: born, parent: ctx.caller });
|
|
714
|
+
await ctx.emit(RunRequested, {
|
|
715
|
+
run,
|
|
716
|
+
file,
|
|
717
|
+
args: accepted,
|
|
718
|
+
...(input.label === undefined ? {} : { label: input.label }),
|
|
719
|
+
...(script === undefined ? {} : { hash: script.hash }),
|
|
720
|
+
...(contract === undefined ? {} : { contract }),
|
|
721
|
+
...(phases === undefined ? {} : { phases }),
|
|
722
|
+
...(boxed ? { box: true } : {}),
|
|
723
|
+
...(input.workflow === undefined ? {} : { workflow: input.workflow }),
|
|
724
|
+
...(input.version === undefined ? {} : { version: input.version }),
|
|
725
|
+
...(input.directory === undefined ? {} : { directory: input.directory }),
|
|
726
|
+
...(input.parent === undefined ? {} : { parent: input.parent }),
|
|
727
|
+
...(concurrency === undefined ? {} : { concurrency }),
|
|
728
|
+
...(limits === undefined ? {} : { limits }),
|
|
729
|
+
...(deadline === undefined ? {} : { deadline }),
|
|
730
|
+
...(input.allow === undefined ? {} : { allow: input.allow }),
|
|
731
|
+
});
|
|
732
|
+
watchDeadline(run);
|
|
733
|
+
return run;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
api.action(
|
|
737
|
+
action(workflowWait, {
|
|
738
|
+
// Nothing is written: it may be made again.
|
|
739
|
+
recovery: "repeat",
|
|
740
|
+
effect: "read",
|
|
741
|
+
world: "niwa",
|
|
742
|
+
run: async (input, ctx) => {
|
|
743
|
+
const run = runOf(input.run, ctx.caller);
|
|
744
|
+
if (!runExists(journal(), run)) throw new RunRefusal("unknown-run", `unknown run "${run}"`);
|
|
745
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `wait for the run "${run}"`);
|
|
746
|
+
return showing(await settle(run, input.timeoutMs), ctx);
|
|
747
|
+
},
|
|
748
|
+
}),
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
api.action(
|
|
752
|
+
action(workflowInspect, {
|
|
753
|
+
recovery: "repeat",
|
|
754
|
+
effect: "read",
|
|
755
|
+
world: "niwa",
|
|
756
|
+
run: (input, ctx) => {
|
|
757
|
+
const run = runOf(input.run, ctx.caller);
|
|
758
|
+
const reader = journal();
|
|
759
|
+
mayAct(ctx.caller, ownerOfRun(reader, run), `inspect the run "${run}"`);
|
|
760
|
+
const inspection = inspectRun(reader, run);
|
|
761
|
+
if (inspection === undefined) throw new RunRefusal("unknown-run", `unknown run "${run}"`);
|
|
762
|
+
return inspection;
|
|
763
|
+
},
|
|
764
|
+
}),
|
|
765
|
+
);
|
|
766
|
+
|
|
767
|
+
api.action(
|
|
768
|
+
action(workflowRuns, {
|
|
769
|
+
// Nothing is written: it may be made again.
|
|
770
|
+
recovery: "repeat",
|
|
771
|
+
effect: "read",
|
|
772
|
+
world: "niwa",
|
|
773
|
+
run: (input, ctx) => {
|
|
774
|
+
const reader = journal();
|
|
775
|
+
// The runs of whoever asks: a client that is not the owner sees the runs it started.
|
|
776
|
+
const runs = runIds(reader)
|
|
777
|
+
.filter((run) => isOwnerOrStarter(ctx.caller, ownerOfRun(reader, run)))
|
|
778
|
+
.filter((run) => input.state === undefined || statusOf(reader, run).state === input.state);
|
|
779
|
+
const shown = runs.slice(-(input.limit ?? runs.length));
|
|
780
|
+
return { runs: shown.map((run) => runLine(reader, run)), entries: shown.map((run) => runEntry(reader, run)) };
|
|
781
|
+
},
|
|
782
|
+
}),
|
|
783
|
+
);
|
|
784
|
+
|
|
785
|
+
const stats = new StatsFold();
|
|
786
|
+
|
|
787
|
+
api.action(
|
|
788
|
+
action(workflowStats, {
|
|
789
|
+
// Nothing is written: the fold catches up with the journal and answers.
|
|
790
|
+
recovery: "repeat",
|
|
791
|
+
effect: "read",
|
|
792
|
+
world: "niwa",
|
|
793
|
+
run: (input) => {
|
|
794
|
+
stats.catchUp(journal());
|
|
795
|
+
if (input.id === undefined) {
|
|
796
|
+
if (input.compare !== undefined) throw new RunRefusal("compare-what", "a comparison is of two versions of one workflow: name its id");
|
|
797
|
+
return stats.answer({ ...(input.since === undefined ? {} : { since: input.since }) });
|
|
798
|
+
}
|
|
799
|
+
const id = workflowOf(input.id).id;
|
|
800
|
+
const versionNamed = (text: string): WorkflowVersion => {
|
|
801
|
+
const full = WorkflowVersion.safeParse(text);
|
|
802
|
+
if (full.success) return full.data;
|
|
803
|
+
const found = stats.versionsOf(id).filter((version) => version.startsWith(text));
|
|
804
|
+
const [only] = found;
|
|
805
|
+
if (only === undefined || found.length > 1) {
|
|
806
|
+
throw new RunRefusal("unknown-version", `"${text}" names ${found.length === 0 ? "no version" : "more than one version"} of ${id} that ran`);
|
|
807
|
+
}
|
|
808
|
+
return only;
|
|
809
|
+
};
|
|
810
|
+
return stats.answer({
|
|
811
|
+
id,
|
|
812
|
+
...(input.compare === undefined ? {} : { compare: [versionNamed(input.compare[0]), versionNamed(input.compare[1])] as const }),
|
|
813
|
+
...(input.since === undefined ? {} : { since: input.since }),
|
|
814
|
+
});
|
|
815
|
+
},
|
|
816
|
+
}),
|
|
817
|
+
);
|
|
818
|
+
|
|
819
|
+
api.action(
|
|
820
|
+
action(workflowCheck, {
|
|
821
|
+
// The script is read and checked, and nothing is written: it may be made again.
|
|
822
|
+
recovery: "repeat",
|
|
823
|
+
effect: "read",
|
|
824
|
+
world: "local",
|
|
825
|
+
run: async (input, ctx) => {
|
|
826
|
+
const file = resolveFile(opts.root, input.file);
|
|
827
|
+
const script = readScript(file);
|
|
828
|
+
if (script === undefined) throw new RunRefusal("script-unreadable", `the script "${file}" cannot be read`);
|
|
829
|
+
return checked(ctx, { file, text: script.text }, opts.box ?? false);
|
|
830
|
+
},
|
|
831
|
+
}),
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
api.action(
|
|
835
|
+
action(workflowGuide, {
|
|
836
|
+
// The description is read and nothing is written: it may be made again.
|
|
837
|
+
recovery: "repeat",
|
|
838
|
+
effect: "read",
|
|
839
|
+
world: "niwa",
|
|
840
|
+
run: async (_input, ctx) => {
|
|
841
|
+
const { actions, callable, blocks } = await catalogueFor(ctx, api.rights(ctx.caller));
|
|
842
|
+
return guideFor(actions, callable, triggersNow(), blocks);
|
|
843
|
+
},
|
|
844
|
+
}),
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
// Serialize control changes for the same run while its journal append is in progress.
|
|
848
|
+
const controls = new Map<string, Promise<unknown>>();
|
|
849
|
+
const changeControl = async <T>(run: string, change: () => Promise<T>): Promise<T> => {
|
|
850
|
+
const next = (controls.get(run) ?? Promise.resolve()).then(change, change);
|
|
851
|
+
controls.set(run, next);
|
|
852
|
+
try {
|
|
853
|
+
return await next;
|
|
854
|
+
} finally {
|
|
855
|
+
if (controls.get(run) === next) controls.delete(run);
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
api.action(action(workflowPause, {
|
|
860
|
+
// A re-issued command while there is an outstanding pause returns its original identifier.
|
|
861
|
+
recovery: "do-not-repeat",
|
|
862
|
+
effect: "record",
|
|
863
|
+
world: "niwa",
|
|
864
|
+
run: async (input, ctx) => {
|
|
865
|
+
const run = runOf(input.run, ctx.caller);
|
|
866
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `pause the run "${run}"`);
|
|
867
|
+
if (input.effects === "deny") {
|
|
868
|
+
const held = await changeControl(run, async () => {
|
|
869
|
+
const status = statusOf(journal(), run);
|
|
870
|
+
if (status.state !== "finished" && activeHold(journal(), run) === undefined) {
|
|
871
|
+
await ctx.emit(RunHoldRequested, { run, tree: input.tree === true, effects: "deny" });
|
|
872
|
+
}
|
|
873
|
+
return statusOf(journal(), run);
|
|
874
|
+
});
|
|
875
|
+
return showing(held, ctx);
|
|
876
|
+
}
|
|
877
|
+
await pauseTree(run, ctx);
|
|
878
|
+
return showing(statusOf(journal(), run), ctx);
|
|
879
|
+
},
|
|
880
|
+
}));
|
|
881
|
+
|
|
882
|
+
/** The deadlines that are coming, by run: what stops each from coming. */
|
|
883
|
+
const deadlineTimers = new Map<string, () => void>();
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Wait for the deadline of a run: when it comes and the run has not ended, the run is paused at its next boundary
|
|
887
|
+
* with a pause of the kind `deadline`, which only an extension or a cancel answers. A deadline moved later meanwhile
|
|
888
|
+
* is waited for instead.
|
|
889
|
+
*/
|
|
890
|
+
const watchDeadline = (run: string): void => {
|
|
891
|
+
deadlineTimers.get(run)?.();
|
|
892
|
+
deadlineTimers.delete(run);
|
|
893
|
+
const at = deadlineOf(journal(), run);
|
|
894
|
+
const clock = hostClock;
|
|
895
|
+
if (at === undefined || clock === undefined) return;
|
|
896
|
+
const stop = clock.schedule(at, () =>
|
|
897
|
+
changeControl(run, async () => {
|
|
898
|
+
deadlineTimers.delete(run);
|
|
899
|
+
if (statusOf(journal(), run).state === "finished" || deadlineOf(journal(), run) !== at) return;
|
|
900
|
+
await clock.emit(RunPauseRequested, { run: RunId.parse(run), kind: "deadline", deadline: at });
|
|
901
|
+
}),
|
|
902
|
+
);
|
|
903
|
+
deadlineTimers.set(run, stop);
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Move the deadline of a run to `to`, and of each run it started whose deadline was its own (`from`): a child held
|
|
908
|
+
* to its parent's deadline is given the parent's new one. A pause of a deadline that came is answered: the run goes on.
|
|
909
|
+
*/
|
|
910
|
+
const extendTree = async (run: string, from: number, to: number, ctx: ActionContext): Promise<void> => {
|
|
911
|
+
await changeControl(run, async () => {
|
|
912
|
+
await ctx.emit(RunDeadlineExtended, { run: RunId.parse(run), deadline: to });
|
|
913
|
+
const pause = activeManualPause(journal(), run);
|
|
914
|
+
if (pause !== undefined && deadlineReached(pause) !== undefined) await ctx.emit(RunContinued, { run: RunId.parse(run), pause: String(pause.id) });
|
|
915
|
+
});
|
|
916
|
+
watchDeadline(run);
|
|
917
|
+
for (const child of childrenOf(journal(), run)) {
|
|
918
|
+
if (statusOf(journal(), child).state !== "finished" && deadlineOf(journal(), child) === from) await extendTree(child, from, to, ctx);
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
api.action(action(workflowExtend, {
|
|
923
|
+
// Each extension moves the deadline again: one that was cut off is not made twice.
|
|
924
|
+
recovery: "do-not-repeat",
|
|
925
|
+
effect: "record",
|
|
926
|
+
world: "niwa",
|
|
927
|
+
run: async (input, ctx) => {
|
|
928
|
+
const run = runOf(input.run, ctx.caller);
|
|
929
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `extend the run "${run}"`);
|
|
930
|
+
const extended = await changeControl(run, async () => {
|
|
931
|
+
if (statusOf(journal(), run).state === "finished") throw new RunRefusal("finished", `the run "${run}" has finished`);
|
|
932
|
+
const current = deadlineOf(journal(), run);
|
|
933
|
+
if (current === undefined) throw new RunRefusal("no-deadline", `the run "${run}" has no deadline to extend`);
|
|
934
|
+
return { from: current, to: Math.max(current, clockNow()) + input.by };
|
|
935
|
+
});
|
|
936
|
+
await extendTree(run, extended.from, extended.to, ctx);
|
|
937
|
+
return showing(statusOf(journal(), run), ctx);
|
|
938
|
+
},
|
|
939
|
+
}));
|
|
940
|
+
|
|
941
|
+
/** Pause a run and the runs it started that are going, down the tree. */
|
|
942
|
+
const pauseTree = async (run: RunId, ctx: ActionContext): Promise<void> => {
|
|
943
|
+
await changeControl(run, async () => {
|
|
944
|
+
const status = statusOf(journal(), run);
|
|
945
|
+
if (status.state === "running" && status.manualPause === undefined) await ctx.emit(RunPauseRequested, { run });
|
|
946
|
+
});
|
|
947
|
+
for (const child of childrenOf(journal(), run)) {
|
|
948
|
+
if (statusOf(journal(), child).state !== "finished") await pauseTree(child, ctx);
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
/** Release the manual pauses of the runs a run started, down the tree. */
|
|
953
|
+
const continueChildren = async (run: RunId, ctx: ActionContext): Promise<void> => {
|
|
954
|
+
for (const child of childrenOf(journal(), run)) {
|
|
955
|
+
await changeControl(child, async () => {
|
|
956
|
+
const pause = activeManualPause(journal(), child);
|
|
957
|
+
if (pause !== undefined) await ctx.emit(RunContinued, { run: child, pause: String(pause.id) });
|
|
958
|
+
});
|
|
959
|
+
await continueChildren(child, ctx);
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
api.action(action(workflowContinue, {
|
|
964
|
+
recovery: "repeat",
|
|
965
|
+
effect: "record",
|
|
966
|
+
world: "niwa",
|
|
967
|
+
run: async (input, ctx) => {
|
|
968
|
+
const run = runOf(input.run, ctx.caller);
|
|
969
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `continue the run "${run}"`);
|
|
970
|
+
const released = await changeControl(run, async () => {
|
|
971
|
+
const pause = activeManualPause(journal(), run);
|
|
972
|
+
const hold = activeHold(journal(), run);
|
|
973
|
+
if (hold?.id === input.pause) {
|
|
974
|
+
await ctx.emit(RunContinued, { run, pause: input.pause });
|
|
975
|
+
return false;
|
|
976
|
+
}
|
|
977
|
+
if (pause?.id !== input.pause) return false;
|
|
978
|
+
if (deadlineReached(pause) !== undefined) {
|
|
979
|
+
throw new RunRefusal("deadline", `the run "${run}" reached its deadline: give it more time with workflow.extend, or cancel it`);
|
|
980
|
+
}
|
|
981
|
+
await ctx.emit(RunContinued, { run, pause: input.pause });
|
|
982
|
+
return true;
|
|
983
|
+
});
|
|
984
|
+
if (released) await continueChildren(run, ctx);
|
|
985
|
+
return showing(statusOf(journal(), run), ctx);
|
|
986
|
+
},
|
|
987
|
+
}));
|
|
988
|
+
|
|
989
|
+
api.action(
|
|
990
|
+
action(workflowResume, {
|
|
991
|
+
// The script runs again: a resume that was cut off may have run it, so it is not
|
|
992
|
+
// made again.
|
|
993
|
+
recovery: "do-not-repeat",
|
|
994
|
+
effect: "record",
|
|
995
|
+
world: "niwa",
|
|
996
|
+
run: async (input, ctx) => {
|
|
997
|
+
const run = runOf(input.run, ctx.caller);
|
|
998
|
+
const facts = known(run);
|
|
999
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `resume the run "${run}"`);
|
|
1000
|
+
if (input.choice !== undefined) {
|
|
1001
|
+
// The pause of the run is answered by the run: the step is retried or skipped, or the run is cancelled.
|
|
1002
|
+
const paused = statusOf(journal(), run);
|
|
1003
|
+
if (paused.state !== "paused" || paused.invocation === undefined) {
|
|
1004
|
+
throw new RunRefusal("not-paused", `the run "${run}" is not paused on a step: there is nothing to ${input.choice}`);
|
|
1005
|
+
}
|
|
1006
|
+
// Only what the pause offers: a skip of a pause on no step would run the script again, as a retry does.
|
|
1007
|
+
if (!(paused.choices ?? []).includes(input.choice)) {
|
|
1008
|
+
throw new RunRefusal(
|
|
1009
|
+
"choice-not-offered",
|
|
1010
|
+
`the pause of the run "${run}" does not offer ${input.choice}: it offers ${(paused.choices ?? []).join(", ")}`,
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
await ctx.step(RESOLVE_ACTION, { invocation: paused.invocation, choice: input.choice });
|
|
1014
|
+
return { run, finished: input.choice === "cancel" };
|
|
1015
|
+
}
|
|
1016
|
+
// The run may only be started again when the invocation that is going has ended. A run
|
|
1017
|
+
// that is still going would have two invocations writing over the same children of
|
|
1018
|
+
// the run, so the resume is refused instead.
|
|
1019
|
+
const previous = latestRequest(run)?.execution ?? run;
|
|
1020
|
+
// The script is the file the run was started with: an edit to it makes the resume a
|
|
1021
|
+
// revision, and the answer says what the new start takes from the earlier ones and what runs.
|
|
1022
|
+
const script = readScript(facts.file);
|
|
1023
|
+
const hash = script?.hash;
|
|
1024
|
+
const edited = hash !== undefined && facts.hash !== undefined && hash !== facts.hash;
|
|
1025
|
+
const requested = { ...requestedOf(run, facts, facts.file), ...(input.from === undefined ? {} : { from: input.from }) };
|
|
1026
|
+
if (!edited) {
|
|
1027
|
+
await requestAgain(ctx, requested, previous, script);
|
|
1028
|
+
return { run, finished: false };
|
|
1029
|
+
}
|
|
1030
|
+
const revision = await revisionOf(journal(), api.artifacts, facts, actionNameOf, boxFor(facts.box), facts.file, run, input.from);
|
|
1031
|
+
await requestAgain(ctx, requested, previous, script);
|
|
1032
|
+
return { run, finished: false, revision };
|
|
1033
|
+
},
|
|
1034
|
+
}),
|
|
1035
|
+
);
|
|
1036
|
+
|
|
1037
|
+
api.action(
|
|
1038
|
+
action(workflowRevise, {
|
|
1039
|
+
// The edited script runs again: a revise that was cut off may have run it, so it
|
|
1040
|
+
// is not made again.
|
|
1041
|
+
recovery: "do-not-repeat",
|
|
1042
|
+
effect: "record",
|
|
1043
|
+
world: "niwa",
|
|
1044
|
+
run: async (input, ctx) => {
|
|
1045
|
+
const run = runOf(input.run, ctx.caller);
|
|
1046
|
+
const facts = known(run);
|
|
1047
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `revise the run "${run}"`);
|
|
1048
|
+
// The run may only be started again when the invocation that is going has ended.
|
|
1049
|
+
const previous = latestRequest(run)?.execution ?? run;
|
|
1050
|
+
const file = resolveFile(opts.root, input.file);
|
|
1051
|
+
const revision = await revisionOf(journal(), api.artifacts, facts, actionNameOf, boxFor(facts.box), file, run, input.from);
|
|
1052
|
+
const script = readScript(file);
|
|
1053
|
+
const request = { ...requestedOf(run, facts, file), ...(input.from === undefined ? {} : { from: input.from }) };
|
|
1054
|
+
await requestAgain(ctx, request, previous, script);
|
|
1055
|
+
return { run, finished: false, revision };
|
|
1056
|
+
},
|
|
1057
|
+
}),
|
|
1058
|
+
);
|
|
1059
|
+
|
|
1060
|
+
api.action(
|
|
1061
|
+
action(workflowCancel, {
|
|
1062
|
+
// The run ends once: a cancel that was cut off may have ended it, so it is not
|
|
1063
|
+
// made again.
|
|
1064
|
+
recovery: "do-not-repeat",
|
|
1065
|
+
effect: "record",
|
|
1066
|
+
world: "niwa",
|
|
1067
|
+
run: async (input, ctx) => {
|
|
1068
|
+
const run = runOf(input.run, ctx.caller);
|
|
1069
|
+
known(run);
|
|
1070
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `cancel the run "${run}"`);
|
|
1071
|
+
await cancelTree(run, ctx);
|
|
1072
|
+
return { run, finished: true };
|
|
1073
|
+
},
|
|
1074
|
+
}),
|
|
1075
|
+
);
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* End a run and the runs it started, down the tree.
|
|
1079
|
+
*
|
|
1080
|
+
* The end of the run is written first: the run of the script sees it and ends there. Its process
|
|
1081
|
+
* is stopped, the agents it started are interrupted, and what it asked is withdrawn.
|
|
1082
|
+
*/
|
|
1083
|
+
async function cancelTree(run: RunId, ctx: ActionContext): Promise<void> {
|
|
1084
|
+
const facts = known(run);
|
|
1085
|
+
if (statusOf(journal(), run).state !== "finished") {
|
|
1086
|
+
await changeControl(run, async () => {
|
|
1087
|
+
await ctx.emit(RunFinished, { run, execution: latestRequest(run)?.execution, outcome: { status: "cancelled" } });
|
|
1088
|
+
const pause = activeManualPause(journal(), run);
|
|
1089
|
+
if (pause !== undefined) await ctx.emit(RunContinued, { run, pause: String(pause.id) });
|
|
1090
|
+
});
|
|
1091
|
+
// The child process of the run is stopped: a run that was cancelled is not running
|
|
1092
|
+
// any more, whatever its script was doing.
|
|
1093
|
+
stopRun(run);
|
|
1094
|
+
// The agents it started are not left working for nobody.
|
|
1095
|
+
for (const session of sessionsOf(journal(), facts.invocations)) {
|
|
1096
|
+
await ctx.step.outcome(sessionInterrupt, { session });
|
|
1097
|
+
await ctx.step.outcome(sessionEnd, { session, reason: `the run ${run} that started it was cancelled` });
|
|
1098
|
+
}
|
|
1099
|
+
// What the run asked is not left open: the owner is not asked about a run that ended, and a
|
|
1100
|
+
// session that was asked through a request is no longer waited for. The cancel is made as the
|
|
1101
|
+
// address the request was put to, the one that may withdraw it.
|
|
1102
|
+
for (const { id, to } of openRequestsOfInvocations(journal(), facts.invocations)) {
|
|
1103
|
+
await ctx.step.outcome(requestCancel, { id, reason: "the run was cancelled", by: to });
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
for (const child of childrenOf(journal(), run)) await cancelTree(child, ctx);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/** The end of a child run, as a step of the run whose branch started it: the tree goes down with a cancel of the branch. */
|
|
1110
|
+
const cancelChild = action("workflow.run.cancel-child", {
|
|
1111
|
+
description: "End a run that a cancelled branch of its parent started, with the runs it started.",
|
|
1112
|
+
input: z.object({ run: RunId }),
|
|
1113
|
+
output: z.null(),
|
|
1114
|
+
recovery: "repeat",
|
|
1115
|
+
effect: "record",
|
|
1116
|
+
world: "niwa",
|
|
1117
|
+
run: async (input, ctx) => {
|
|
1118
|
+
await cancelTree(input.run, ctx);
|
|
1119
|
+
return null;
|
|
1120
|
+
},
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
api.action(
|
|
1124
|
+
action(workflowStop, {
|
|
1125
|
+
// The steps are cancelled once: a stop that was cut off may have cancelled them, so it is not made again.
|
|
1126
|
+
recovery: "do-not-repeat",
|
|
1127
|
+
effect: "record",
|
|
1128
|
+
world: "niwa",
|
|
1129
|
+
run: async (input, ctx) => {
|
|
1130
|
+
const run = runOf(input.run, ctx.caller);
|
|
1131
|
+
known(run);
|
|
1132
|
+
mayAct(ctx.caller, ownerOfRun(journal(), run), `stop the run "${run}"`);
|
|
1133
|
+
if (input.at === undefined) {
|
|
1134
|
+
await cancelTree(run, ctx);
|
|
1135
|
+
return { run, finished: true };
|
|
1136
|
+
}
|
|
1137
|
+
const latest = latestInvocationOf(journal(), run);
|
|
1138
|
+
if (latest === undefined || latest.state === "done") {
|
|
1139
|
+
throw new RunRefusal("not-going", `the run "${run}" is not going: there is nothing of it to stop`);
|
|
1140
|
+
}
|
|
1141
|
+
const at = input.at;
|
|
1142
|
+
// A shielded scope under what is stopped is spared, unless the stop goes through shields.
|
|
1143
|
+
const except = input.force === true ? [] : openShieldsOf(journal(), run).filter((open) => open !== at && keyUnder(open, at));
|
|
1144
|
+
const reason = input.reason ?? "stopped by the owner";
|
|
1145
|
+
await ctx.step(CANCEL_STEPS_ACTION, { invocation: latest.invocation, prefixes: [at], ...(except.length === 0 ? {} : { except }), reason });
|
|
1146
|
+
const held = heldBy(journal(), latest.invocation, [at], except);
|
|
1147
|
+
for (const session of held.sessions) await ctx.step.outcome(sessionEnd, { session, reason });
|
|
1148
|
+
for (const mark of held.marks) await ctx.step.outcome(actionName("access.grant.close"), { principal: principalOf("session", mark), reason });
|
|
1149
|
+
for (const child of held.children) await cancelTree(child, ctx);
|
|
1150
|
+
return { run, finished: false };
|
|
1151
|
+
},
|
|
1152
|
+
}),
|
|
1153
|
+
);
|
|
1154
|
+
|
|
1155
|
+
// --- subscriptions -------------------------------------------------------------------
|
|
1156
|
+
|
|
1157
|
+
/** Who an installation was made by, as the caller its runs are born from; the host when nothing limited the installer. */
|
|
1158
|
+
const installerOf = (installation: Installation): Caller =>
|
|
1159
|
+
installation.installer === undefined ? { kind: "host" } : callerOf(installation.installer);
|
|
1160
|
+
|
|
1161
|
+
const emitRule = `emit:${SCRIPT_TYPES}*`;
|
|
1162
|
+
const notEmit = (spec: RuleSpec): boolean => !ruleText(rule(spec)).startsWith("emit:");
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* The part of `requested` that the installer holds now.
|
|
1166
|
+
*
|
|
1167
|
+
* A mask the installer holds whole is kept whole. A mask it holds only in part (`task.*` asked,
|
|
1168
|
+
* `task.create` held) gives the part it holds, so narrowing the installer narrows the runs born
|
|
1169
|
+
* after it instead of stopping them. Nothing is ever widened.
|
|
1170
|
+
*/
|
|
1171
|
+
const withinInstaller = (installer: Caller, requested: readonly RuleSpec[]): RuleSpec[] => {
|
|
1172
|
+
const kept: RuleSpec[] = [];
|
|
1173
|
+
for (const spec of requested) {
|
|
1174
|
+
const whole = api.narrow(installer, [spec]);
|
|
1175
|
+
if (!("refused" in whole)) {
|
|
1176
|
+
kept.push(...specsOf(whole.rules));
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
const rights = api.rights(installer);
|
|
1180
|
+
if (rights === "unlimited") continue;
|
|
1181
|
+
for (const held of specsOf(rights)) {
|
|
1182
|
+
const inside = narrow(grant([spec]), [held], { irreversible: (name) => api.effectOf(name) === "irreversible" });
|
|
1183
|
+
if (!("refused" in inside)) kept.push(...specsOf(inside.rules));
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return kept;
|
|
1187
|
+
};
|
|
1188
|
+
|
|
1189
|
+
/** Set the profile of an installation: the record of the owner's table, and the value a later start reads. */
|
|
1190
|
+
const setProfile = (id: InstallationId, specs: readonly RuleSpec[], by: Caller): void => {
|
|
1191
|
+
api.profile(profileOf(id), () => specs);
|
|
1192
|
+
api.setProfile(profileOf(id), specs, by);
|
|
1193
|
+
};
|
|
1194
|
+
|
|
1195
|
+
/**
|
|
1196
|
+
* The birth of the run a record starts under an installation: its grant, as a step of the handler.
|
|
1197
|
+
*
|
|
1198
|
+
* The grant is what the profile holds, held within what the installer holds now, so a run is never
|
|
1199
|
+
* wider than the installer is at its birth, and the runs that live are not touched by a later
|
|
1200
|
+
* narrowing. Nothing is born under an installation that ended, an installer whose grant was
|
|
1201
|
+
* closed (the installation stays listed until it is uninstalled), a workflow that is broken, or a
|
|
1202
|
+
* script that is not the bytes that were installed. The run's id is issued from the step, so a
|
|
1203
|
+
* step entered again issues the same id, and a run that was given its grant already is not given another.
|
|
1204
|
+
*/
|
|
1205
|
+
const birth = action("workflow.subscription.birth", {
|
|
1206
|
+
description: "Issue the id of the run that a record starts under an installation, and give the run its grant.",
|
|
1207
|
+
input: z.object({ installation: z.number().int(), record: z.string().optional() }),
|
|
1208
|
+
output: z.object({ run: RunId.optional(), refused: z.string().optional(), phases: WorkflowPhases.optional() }),
|
|
1209
|
+
recovery: "repeat",
|
|
1210
|
+
effect: "record",
|
|
1211
|
+
world: "niwa",
|
|
1212
|
+
run: async ({ installation, record: woken }, ctx) => {
|
|
1213
|
+
const found = installationsOf(journal()).find((one) => one.seq === installation);
|
|
1214
|
+
if (found === undefined || found.removed !== undefined) return {};
|
|
1215
|
+
const installer = installerOf(found);
|
|
1216
|
+
// A closed grant, or the emergency switch, refuses with no masks at all.
|
|
1217
|
+
if ("refused" in api.narrow(installer, [])) return {};
|
|
1218
|
+
// A workflow that a change of one it calls broke does not start, and its installation stays.
|
|
1219
|
+
const registered = found.workflow === undefined ? undefined : workflowsOf(journal()).find((one) => one.id === found.workflow);
|
|
1220
|
+
const installedVersion = registered?.versions.find((one) => one.version === found.version);
|
|
1221
|
+
if (installedVersion?.broken !== undefined) return {};
|
|
1222
|
+
const script = readScript(found.file);
|
|
1223
|
+
if (script?.hash !== found.hash || (found.version !== undefined && versionOf(found.file) !== found.version)) {
|
|
1224
|
+
throw new RunRefusal("script-changed", `the installed script "${found.file}" is not the one that was installed: install it again`);
|
|
1225
|
+
}
|
|
1226
|
+
// A target template is filled by the record the run starts on, and the rule it gives is held whole by the
|
|
1227
|
+
// installer: a value that would widen it, or a rule the installer does not hold, starts nothing.
|
|
1228
|
+
const templates = found.uses.filter((use) => isRuleTemplate(use));
|
|
1229
|
+
let filled: RuleSpec[] = [];
|
|
1230
|
+
if (templates.length > 0) {
|
|
1231
|
+
const stored = woken === undefined ? undefined : journal().get(recordId(woken));
|
|
1232
|
+
const args = stored === undefined ? undefined : { id: stored.id, type: stored.type, data: stored.data };
|
|
1233
|
+
try {
|
|
1234
|
+
filled = filledUses(templates, args);
|
|
1235
|
+
} catch (error) {
|
|
1236
|
+
return { refused: (error as Error).message };
|
|
1237
|
+
}
|
|
1238
|
+
const held = api.narrow(installer, filled);
|
|
1239
|
+
if ("refused" in held) return { refused: new UsesRefused(held.refused.map((text) => text.replace(/^call:/, ""))).message };
|
|
1240
|
+
filled = specsOf(held.rules);
|
|
1241
|
+
}
|
|
1242
|
+
// Record the plan with this step's result so a replay keeps the original request payload.
|
|
1243
|
+
// For older installations, take the exact registered version or the verified script bytes.
|
|
1244
|
+
const phases = found.phases ?? installedVersion?.phases ?? await scriptPhases({ file: found.file, text: script.text });
|
|
1245
|
+
const run = runIndex.issue(ctx.operationId);
|
|
1246
|
+
const issued = journal()
|
|
1247
|
+
.find({ type: CORE_TYPES.accessGranted, field: "principal.id", equals: run })
|
|
1248
|
+
.some((row) => (row.data as { principal?: { kind?: unknown } }).principal?.kind === "run");
|
|
1249
|
+
if (!issued) {
|
|
1250
|
+
const profile = profileRulesOf(journal(), found.id) ?? grant([]);
|
|
1251
|
+
const rules = withinInstaller(installer, specsOf(profile).filter(notEmit));
|
|
1252
|
+
api.issueToken("run", run, { grant: grant([...rules, ...filled, emitRule]), parent: installer });
|
|
1253
|
+
}
|
|
1254
|
+
return { run, ...(phases === undefined ? {} : { phases }) };
|
|
1255
|
+
},
|
|
1256
|
+
});
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* The start of a run of one installation on one record, started once. The handler of a record is
|
|
1260
|
+
* one invocation, admitted once and replayed from its steps after a restart, and the run id is a
|
|
1261
|
+
* function of the installation and the record.
|
|
1262
|
+
*/
|
|
1263
|
+
const startRun =
|
|
1264
|
+
(installation: Installation): Handler<unknown> =>
|
|
1265
|
+
async (_data, step, stored) => {
|
|
1266
|
+
const { run, phases } = await step(birth, { installation: installation.seq, record: stored.id }, { key: `birth:${installation.seq}` });
|
|
1267
|
+
if (run === undefined) return;
|
|
1268
|
+
await step.emit(
|
|
1269
|
+
RunRequested,
|
|
1270
|
+
{
|
|
1271
|
+
run,
|
|
1272
|
+
file: installation.file,
|
|
1273
|
+
args: { id: stored.id, type: stored.type, data: stored.data },
|
|
1274
|
+
hash: installation.hash,
|
|
1275
|
+
contract: installation.contract,
|
|
1276
|
+
...(phases === undefined ? {} : { phases }),
|
|
1277
|
+
...(installation.box === true ? { box: true } : {}),
|
|
1278
|
+
...(installation.workflow === undefined ? {} : { workflow: installation.workflow }),
|
|
1279
|
+
...(installation.version === undefined ? {} : { version: installation.version }),
|
|
1280
|
+
installation: installation.id,
|
|
1281
|
+
},
|
|
1282
|
+
{ key: `run:${installation.seq}` },
|
|
1283
|
+
);
|
|
1284
|
+
};
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* Every installation is a subscription of its own: its `on` is what wakes it, the record that
|
|
1288
|
+
* installed it is where it starts, and the record that ended it is where it ends. Being its own
|
|
1289
|
+
* subscription, it is never woken by what its own runs caused, however long the way — a script
|
|
1290
|
+
* that emits what it starts on, or two that start on each other, would otherwise go on for ever.
|
|
1291
|
+
* The installations are read again only when one is made or ended.
|
|
1292
|
+
*/
|
|
1293
|
+
let read: { journal: JournalReader; upTo: Seq; subscribers: Subscriber[] } | undefined;
|
|
1294
|
+
api.subscribers(() => {
|
|
1295
|
+
const reader = api.journal?.();
|
|
1296
|
+
if (reader === undefined) return [];
|
|
1297
|
+
if (read?.journal !== reader) read = { journal: reader, upTo: seq(0), subscribers: [] };
|
|
1298
|
+
const fresh = reader.query({ types: [SubscriptionInstalled.name, SubscriptionUninstalled.name], from: read.upTo });
|
|
1299
|
+
const last = fresh.at(-1);
|
|
1300
|
+
if (last === undefined) return read.subscribers;
|
|
1301
|
+
read.upTo = last.seq;
|
|
1302
|
+
read.subscribers = installationsOf(reader).map(
|
|
1303
|
+
(installation): Subscriber => ({
|
|
1304
|
+
id: `workflow.on:${installation.id}`,
|
|
1305
|
+
queries: installation.on.map((one) => ({ types: [one.type], ...(one.filter === undefined ? {} : { filter: one.filter }) })),
|
|
1306
|
+
from: installation.seq,
|
|
1307
|
+
...(installation.removed === undefined ? {} : { until: installation.removed }),
|
|
1308
|
+
handler: startRun(installation),
|
|
1309
|
+
}),
|
|
1310
|
+
);
|
|
1311
|
+
return read.subscribers;
|
|
1312
|
+
});
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* Install a registered workflow at its latest version, or a script file: the script is checked, its `on` read, and
|
|
1316
|
+
* the profile of the installation is narrow(caller, uses) and the script's own records.
|
|
1317
|
+
*/
|
|
1318
|
+
const installScript = async (
|
|
1319
|
+
ctx: ActionContext,
|
|
1320
|
+
input: { registered?: RegisteredWorkflow; file?: string; description: EntityDescription; box?: boolean; builtin?: Builtin },
|
|
1321
|
+
): Promise<{ id: InstallationId; profile: string; on: Subscription[] }> => {
|
|
1322
|
+
// A broken workflow is installed all the same: its runs do not start until a version passes.
|
|
1323
|
+
const registered = input.registered;
|
|
1324
|
+
if (registered?.latest.candidate === true) {
|
|
1325
|
+
throw new RunRefusal("candidate", `the workflow ${registered.id} has only a candidate version: it is installed once a version is promoted`);
|
|
1326
|
+
}
|
|
1327
|
+
const file = registered === undefined ? resolveFile(opts.root, input.file ?? "") : storedOf(registered.id, registered.latest.version);
|
|
1328
|
+
const script = readScript(file);
|
|
1329
|
+
if (script === undefined) throw new RunRefusal("script-unreadable", `the script "${file}" cannot be read`);
|
|
1330
|
+
const boxed = await boxOf(input.box);
|
|
1331
|
+
const report = await checked(ctx, { file, text: script.text }, boxed);
|
|
1332
|
+
if (!report.ok || report.contract === undefined) throw new ScriptCheckFailed(report.errors);
|
|
1333
|
+
const contract = report.contract;
|
|
1334
|
+
const on = await scriptOn({ file, text: script.text });
|
|
1335
|
+
if (on === undefined || "bad" in on || on.subscriptions.length === 0) {
|
|
1336
|
+
throw new RunRefusal("nothing-to-start-on", `the script "${file}" has no \`on\`: no record would start it`);
|
|
1337
|
+
}
|
|
1338
|
+
const uses = await scriptUses({ file, text: script.text });
|
|
1339
|
+
const masks = uses !== undefined && "masks" in uses ? uses.masks.map((use) => use.mask).filter(isUse) : [];
|
|
1340
|
+
// The profile is what the installer may give: narrow(installer, uses), and the script's own records. A target
|
|
1341
|
+
// template is filled by each record that starts a run, and held to the installer then.
|
|
1342
|
+
const asker = ctx.caller;
|
|
1343
|
+
const narrowed = api.narrow(asker, masks.filter((mask) => !isRuleTemplate(mask)));
|
|
1344
|
+
if ("refused" in narrowed) throw new UsesRefused(narrowed.refused.map((text) => text.replace(/^call:/, "")));
|
|
1345
|
+
const subscriptions = on.subscriptions.map(({ type, filter }) => ({ type, ...(filter === undefined ? {} : { filter }) }));
|
|
1346
|
+
const id = installationIndex.issue(ctx.operationId);
|
|
1347
|
+
setProfile(id, [...specsOf(narrowed.rules), emitRule], ctx.caller);
|
|
1348
|
+
await ctx.emit(SubscriptionInstalled, {
|
|
1349
|
+
installation: id,
|
|
1350
|
+
description: input.description,
|
|
1351
|
+
...(registered === undefined ? {} : { workflow: registered.id, version: registered.latest.version }),
|
|
1352
|
+
file,
|
|
1353
|
+
hash: script.hash,
|
|
1354
|
+
on: subscriptions,
|
|
1355
|
+
uses: masks,
|
|
1356
|
+
...(report.input === undefined ? {} : { input: report.input }),
|
|
1357
|
+
...(report.output === undefined ? {} : { output: report.output }),
|
|
1358
|
+
contract,
|
|
1359
|
+
...(report.phases === undefined ? {} : { phases: report.phases }),
|
|
1360
|
+
...(asker.kind === "client" && asker.principal !== undefined ? { installer: asker.principal } : {}),
|
|
1361
|
+
...(boxed ? { box: true } : {}),
|
|
1362
|
+
...(input.builtin === undefined ? {} : { builtin: input.builtin }),
|
|
1363
|
+
});
|
|
1364
|
+
return { id, profile: profileOf(id), on: subscriptions };
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
api.action(
|
|
1368
|
+
action(workflowInstall, {
|
|
1369
|
+
// The profile and the installation are written: a cut-off install may have made them, so it is not made again.
|
|
1370
|
+
recovery: "do-not-repeat",
|
|
1371
|
+
effect: "record",
|
|
1372
|
+
world: "niwa",
|
|
1373
|
+
run: async (input, ctx) => {
|
|
1374
|
+
if ((input.id === undefined) === (input.file === undefined)) {
|
|
1375
|
+
throw new RunRefusal("install-what", "an installation names the workflow to install: its id, or a script file, one of the two");
|
|
1376
|
+
}
|
|
1377
|
+
return installScript(ctx, {
|
|
1378
|
+
...(input.id === undefined ? { file: input.file ?? "" } : { registered: workflowOf(input.id) }),
|
|
1379
|
+
description: input.description,
|
|
1380
|
+
...(input.box === undefined ? {} : { box: input.box }),
|
|
1381
|
+
});
|
|
1382
|
+
},
|
|
1383
|
+
}),
|
|
1384
|
+
);
|
|
1385
|
+
|
|
1386
|
+
/** End an installation and empty its profile. */
|
|
1387
|
+
const uninstallOne = async (ctx: ActionContext, id: InstallationId, reason: string): Promise<void> => {
|
|
1388
|
+
await ctx.emit(SubscriptionUninstalled, { installation: id, reason });
|
|
1389
|
+
setProfile(id, [], ctx.caller);
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
api.action(
|
|
1393
|
+
action(workflowUninstall, {
|
|
1394
|
+
// The installation ends once: a cut-off uninstall may have ended it, so it is not made again.
|
|
1395
|
+
recovery: "do-not-repeat",
|
|
1396
|
+
effect: "record",
|
|
1397
|
+
world: "niwa",
|
|
1398
|
+
run: async (input, ctx) => {
|
|
1399
|
+
const stands = standing(journal());
|
|
1400
|
+
const id = installationIndex.require(input.id, (one) => stands.some((here) => here.id === one));
|
|
1401
|
+
const installed = stands.find((one) => one.id === id);
|
|
1402
|
+
mayAct(ctx.caller, installed?.installer, `uninstall ${id}`);
|
|
1403
|
+
if (installed?.builtin !== undefined) {
|
|
1404
|
+
throw new RunRefusal(
|
|
1405
|
+
"builtin",
|
|
1406
|
+
`${id} is the built-in "${installed.builtin.name}" of the plugin "${installed.builtin.plugin}": it is installed again when the host starts. Turn it off with the option of the plugin`,
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
await uninstallOne(ctx, id, "uninstalled");
|
|
1410
|
+
return { id };
|
|
1411
|
+
},
|
|
1412
|
+
}),
|
|
1413
|
+
);
|
|
1414
|
+
|
|
1415
|
+
/** An installation as `get` shows it, with why its runs do not start when its workflow is broken. */
|
|
1416
|
+
const installationEntry = (one: Installation) => {
|
|
1417
|
+
const broken =
|
|
1418
|
+
one.workflow === undefined ? undefined : workflowsOf(journal()).find((wf) => wf.id === one.workflow)?.versions.find((v) => v.version === one.version)?.broken;
|
|
1419
|
+
return {
|
|
1420
|
+
id: one.id,
|
|
1421
|
+
description: one.description,
|
|
1422
|
+
...(one.workflow === undefined ? {} : { workflow: one.workflow }),
|
|
1423
|
+
...(one.version === undefined ? {} : { version: one.version }),
|
|
1424
|
+
file: one.file,
|
|
1425
|
+
hash: one.hash,
|
|
1426
|
+
on: one.on,
|
|
1427
|
+
uses: one.uses,
|
|
1428
|
+
...(one.input === undefined ? {} : { input: one.input }),
|
|
1429
|
+
...(one.output === undefined ? {} : { output: one.output }),
|
|
1430
|
+
contract: one.contract,
|
|
1431
|
+
...(broken === undefined ? {} : { broken }),
|
|
1432
|
+
...(one.builtin === undefined ? {} : { builtin: one.builtin }),
|
|
1433
|
+
};
|
|
1434
|
+
};
|
|
1435
|
+
|
|
1436
|
+
api.action(
|
|
1437
|
+
action(workflowInstallationList, {
|
|
1438
|
+
// Nothing is written: it may be made again.
|
|
1439
|
+
recovery: "repeat",
|
|
1440
|
+
effect: "read",
|
|
1441
|
+
world: "niwa",
|
|
1442
|
+
run: () => ({
|
|
1443
|
+
items: standing(journal()).map((one) => ({
|
|
1444
|
+
id: one.id,
|
|
1445
|
+
description: one.description,
|
|
1446
|
+
...(one.workflow === undefined ? {} : { workflow: one.workflow }),
|
|
1447
|
+
file: one.file,
|
|
1448
|
+
hash: one.hash,
|
|
1449
|
+
on: one.on,
|
|
1450
|
+
...(one.builtin === undefined ? {} : { builtin: one.builtin }),
|
|
1451
|
+
})),
|
|
1452
|
+
}),
|
|
1453
|
+
}),
|
|
1454
|
+
);
|
|
1455
|
+
|
|
1456
|
+
api.action(
|
|
1457
|
+
action(workflowInstallationGet, {
|
|
1458
|
+
// Nothing is written: it may be made again.
|
|
1459
|
+
recovery: "repeat",
|
|
1460
|
+
effect: "read",
|
|
1461
|
+
world: "niwa",
|
|
1462
|
+
run: (input) => {
|
|
1463
|
+
const all = installationsOf(journal());
|
|
1464
|
+
const id = installationIndex.require(input.id);
|
|
1465
|
+
const found = all.filter((one) => one.id === id).at(-1);
|
|
1466
|
+
if (found === undefined) throw new RunRefusal("unknown-installation", `no workflow installation "${input.id}"`);
|
|
1467
|
+
return installationEntry(found);
|
|
1468
|
+
},
|
|
1469
|
+
}),
|
|
1470
|
+
);
|
|
1471
|
+
|
|
1472
|
+
// --- registered workflows ---------------------------------------------------------------------
|
|
1473
|
+
|
|
1474
|
+
/**
|
|
1475
|
+
* Check again the workflows that depend on one that changed (it is called by them, or its records are waited for or
|
|
1476
|
+
* read by them): each check is a record, and a version whose latest check failed is broken.
|
|
1477
|
+
*/
|
|
1478
|
+
const recheckDependents = async (
|
|
1479
|
+
ctx: ActionContext,
|
|
1480
|
+
changed: WorkflowId,
|
|
1481
|
+
dependents: readonly RegisteredWorkflow[],
|
|
1482
|
+
): Promise<Array<{ id: WorkflowId; version: WorkflowVersion; ok: boolean }>> => {
|
|
1483
|
+
// The checks do not depend on each other, so they go together (the describer bounds the modules read at once);
|
|
1484
|
+
// the records are written in the order of the dependents.
|
|
1485
|
+
const reports = await Promise.all(
|
|
1486
|
+
dependents.map((dependent) => {
|
|
1487
|
+
const file = storedEntry(api.home(), dependent.id, dependent.latest.version);
|
|
1488
|
+
const script = file === undefined ? undefined : readScript(file);
|
|
1489
|
+
return file === undefined || script === undefined ? undefined : checked(ctx, { file, text: script.text }, opts.box ?? false);
|
|
1490
|
+
}),
|
|
1491
|
+
);
|
|
1492
|
+
const rechecked: Array<{ id: WorkflowId; version: WorkflowVersion; ok: boolean }> = [];
|
|
1493
|
+
for (const [at, dependent] of dependents.entries()) {
|
|
1494
|
+
const latest = dependent.latest;
|
|
1495
|
+
const report = reports[at];
|
|
1496
|
+
const errors = report?.errors ?? [{ file: latest.file, line: 0, column: 0, code: "script-unreadable", message: "the script cannot be read" }];
|
|
1497
|
+
const ok = report?.ok === true;
|
|
1498
|
+
const now = workflowsOf(journal());
|
|
1499
|
+
const calls = latest.calls.map((call): FlowCall => ({ workflow: call.workflow, contract: now.find((one) => one.id === call.workflow)?.latest.contract ?? call.contract }));
|
|
1500
|
+
await ctx.emit(WorkflowRechecked, { workflow: dependent.id, version: latest.version, because: changed, ok, errors, calls });
|
|
1501
|
+
rechecked.push({ id: dependent.id, version: latest.version, ok });
|
|
1502
|
+
}
|
|
1503
|
+
return rechecked;
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
/**
|
|
1507
|
+
* Register a script: a new workflow, or a new version of `existing`. A built-in is registered under the description
|
|
1508
|
+
* its plugin gives; any other takes the one the script declares.
|
|
1509
|
+
*/
|
|
1510
|
+
const registerScript = async (
|
|
1511
|
+
ctx: ActionContext,
|
|
1512
|
+
file: string,
|
|
1513
|
+
existing: RegisteredWorkflow | undefined,
|
|
1514
|
+
builtin?: { builtin: Builtin; description: EntityDescription; candidate?: true },
|
|
1515
|
+
) => {
|
|
1516
|
+
const script = readScript(file);
|
|
1517
|
+
const version = versionOf(file);
|
|
1518
|
+
if (script === undefined || version === undefined) throw new RunRefusal("script-unreadable", `the script "${file}" cannot be read`);
|
|
1519
|
+
const report = await checked(ctx, { file, text: script.text }, opts.box ?? false);
|
|
1520
|
+
if (!report.ok || report.contract === undefined) throw new ScriptCheckFailed(report.errors);
|
|
1521
|
+
const described = EntityDescription.safeParse(builtin?.description ?? report.description);
|
|
1522
|
+
if (!described.success) {
|
|
1523
|
+
throw new RunRefusal(
|
|
1524
|
+
"no-description",
|
|
1525
|
+
`a registered workflow says what it is: give workflow({ description }) a few words, ${String(DESCRIPTION_MIN)} to ${String(DESCRIPTION_MAX)} characters on one line`,
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
const description = described.data;
|
|
1529
|
+
// The records of a script are written under its name: two workflows with one name would be one in the record catalogue.
|
|
1530
|
+
// What the module declares is read once for this version: the check above has just kept it.
|
|
1531
|
+
const declared = await describe(file, boxFor(opts.box ?? false));
|
|
1532
|
+
const name = "described" in declared ? declared.described.name : undefined;
|
|
1533
|
+
const holder = name === undefined ? undefined : workflowsOf(journal()).find((one) => one.id !== existing?.id && one.latest.name === name);
|
|
1534
|
+
if (holder !== undefined) {
|
|
1535
|
+
throw new RunRefusal("name-taken", `the name "${String(name)}" is used by the workflow ${holder.id}: a workflow takes a name of its own, its records are written under it`);
|
|
1536
|
+
}
|
|
1537
|
+
if (existing !== undefined && existing.latest.version === version && existing.latest.description === description) {
|
|
1538
|
+
storeVersion(api.home(), existing.id, version, file);
|
|
1539
|
+
linkOfferedPackages(api.home(), scriptPackages().packages);
|
|
1540
|
+
return { id: existing.id, version, description: existing.latest.description, added: false, rechecked: [] };
|
|
1541
|
+
}
|
|
1542
|
+
const id = existing?.id ?? workflowIndex.issue(ctx.operationId);
|
|
1543
|
+
// The code of the version is kept in the home before it is registered: a registered version always has its copy.
|
|
1544
|
+
storeVersion(api.home(), id, version, file);
|
|
1545
|
+
linkOfferedPackages(api.home(), scriptPackages().packages);
|
|
1546
|
+
const uses = await scriptUses({ file, text: script.text });
|
|
1547
|
+
const reads = await scriptReads({ file, text: script.text });
|
|
1548
|
+
const on = await scriptOn({ file, text: script.text });
|
|
1549
|
+
const subscriptions = on === undefined || "bad" in on ? [] : on.subscriptions.map(({ type, filter }) => ({ type, ...(filter === undefined ? {} : { filter }) }));
|
|
1550
|
+
const masks = uses !== undefined && "masks" in uses ? uses.masks.map((use) => use.mask).filter(isUse) : [];
|
|
1551
|
+
const workflows = workflowsOf(journal());
|
|
1552
|
+
const calls = masks.flatMap((mask): FlowCall[] => {
|
|
1553
|
+
const called = workflows.find((one) => flowActionName(one.id) === useName(mask));
|
|
1554
|
+
return called === undefined ? [] : [{ workflow: called.id, contract: called.latest.contract }];
|
|
1555
|
+
});
|
|
1556
|
+
await ctx.emit(WorkflowRegistered, {
|
|
1557
|
+
workflow: id,
|
|
1558
|
+
version,
|
|
1559
|
+
file,
|
|
1560
|
+
description,
|
|
1561
|
+
...(report.input === undefined ? {} : { input: report.input }),
|
|
1562
|
+
...(report.output === undefined ? {} : { output: report.output }),
|
|
1563
|
+
contract: report.contract,
|
|
1564
|
+
uses: masks,
|
|
1565
|
+
calls,
|
|
1566
|
+
...(name === undefined ? {} : { name }),
|
|
1567
|
+
reads,
|
|
1568
|
+
...(report.emits === undefined ? {} : { emits: report.emits }),
|
|
1569
|
+
...(subscriptions.length === 0 ? {} : { on: subscriptions }),
|
|
1570
|
+
...(report.phases === undefined ? {} : { phases: report.phases }),
|
|
1571
|
+
...(builtin === undefined ? {} : { builtin: builtin.builtin }),
|
|
1572
|
+
...(builtin?.candidate === true ? { candidate: true } : {}),
|
|
1573
|
+
});
|
|
1574
|
+
// A candidate is not what the workflow is yet: the workflows that call it or read its records are not checked against it.
|
|
1575
|
+
if (builtin?.candidate === true) return { id, version, description, added: true, rechecked: [] };
|
|
1576
|
+
// The workflows that call it, when its contract changed, and the ones that wait for or read the records it
|
|
1577
|
+
// writes, when what it writes changed (a new name changes the types as well): each is checked again, once.
|
|
1578
|
+
const before = existing?.latest;
|
|
1579
|
+
const typesOf = (emitted: readonly EmittedRecord[] | undefined): string[] => (emitted ?? []).map((one) => one.type);
|
|
1580
|
+
const sameTypes = before !== undefined && typesOf(before.emits).join() === typesOf(report.emits).join();
|
|
1581
|
+
const contractChanged = before !== undefined && before.contract !== report.contract;
|
|
1582
|
+
const writes = new Set([...typesOf(before?.emits), ...typesOf(report.emits)]);
|
|
1583
|
+
const now = workflowsOf(journal());
|
|
1584
|
+
const dependents = new Map<WorkflowId, RegisteredWorkflow>();
|
|
1585
|
+
if (contractChanged) for (const one of dependentsOf(now, id)) dependents.set(one.id, one);
|
|
1586
|
+
if (contractChanged || !sameTypes) for (const one of readersOf(now, id, writes)) dependents.set(one.id, one);
|
|
1587
|
+
const rechecked = await recheckDependents(ctx, id, [...dependents.values()]);
|
|
1588
|
+
return { id, version, description, added: true, rechecked };
|
|
1589
|
+
};
|
|
1590
|
+
|
|
1591
|
+
api.action(
|
|
1592
|
+
action(workflowRegister, {
|
|
1593
|
+
// A registration is written once: a cut-off one may have made the workflow, so it is not made again.
|
|
1594
|
+
recovery: "do-not-repeat",
|
|
1595
|
+
effect: "record",
|
|
1596
|
+
world: "niwa",
|
|
1597
|
+
run: async (input, ctx) => {
|
|
1598
|
+
const existing = input.id === undefined ? undefined : workflowOf(input.id);
|
|
1599
|
+
if (existing?.builtin !== undefined) {
|
|
1600
|
+
throw new RunRefusal(
|
|
1601
|
+
"builtin",
|
|
1602
|
+
`${existing.id} is the built-in "${existing.builtin.name}" of the plugin "${existing.builtin.plugin}": its versions come with the plugin, a file is not registered under it. Register the file as a workflow of its own`,
|
|
1603
|
+
);
|
|
1604
|
+
}
|
|
1605
|
+
// A new version moves the latest of a workflow: only whoever made it, or the owner, may move it.
|
|
1606
|
+
if (existing !== undefined) mayAct(ctx.caller, makerOf(existing), `register a version of the workflow ${existing.id}`);
|
|
1607
|
+
return registerScript(ctx, resolveFile(opts.root, input.file), existing);
|
|
1608
|
+
},
|
|
1609
|
+
}),
|
|
1610
|
+
);
|
|
1611
|
+
|
|
1612
|
+
/** Who made a workflow: the principal its first registration was written by; none when the host or a handler made it. */
|
|
1613
|
+
const makerOf = (registered: RegisteredWorkflow): Principal | undefined => {
|
|
1614
|
+
const actor = journal().get(registered.record)?.actor;
|
|
1615
|
+
return actor?.kind === "client" ? actor.principal : undefined;
|
|
1616
|
+
};
|
|
1617
|
+
|
|
1618
|
+
/** The installations of a workflow that stand. */
|
|
1619
|
+
const installationsOfWorkflow = (id: WorkflowId): InstallationId[] =>
|
|
1620
|
+
standing(journal()).flatMap((one) => (one.workflow === id ? [one.id] : []));
|
|
1621
|
+
|
|
1622
|
+
api.action(
|
|
1623
|
+
action(workflowList, {
|
|
1624
|
+
// Nothing is written: it may be made again.
|
|
1625
|
+
recovery: "repeat",
|
|
1626
|
+
effect: "read",
|
|
1627
|
+
world: "niwa",
|
|
1628
|
+
run: () => ({
|
|
1629
|
+
items: workflowsOf(journal()).map((one) => ({
|
|
1630
|
+
id: one.id,
|
|
1631
|
+
description: one.latest.description,
|
|
1632
|
+
version: one.latest.version,
|
|
1633
|
+
versions: one.versions.length,
|
|
1634
|
+
...(one.latest.candidate ? { candidate: true as const } : {}),
|
|
1635
|
+
broken: one.latest.broken !== undefined,
|
|
1636
|
+
installations: installationsOfWorkflow(one.id),
|
|
1637
|
+
...(one.latest.phases === undefined ? {} : { phases: one.latest.phases }),
|
|
1638
|
+
...(one.builtin === undefined ? {} : { builtin: one.builtin }),
|
|
1639
|
+
})),
|
|
1640
|
+
}),
|
|
1641
|
+
}),
|
|
1642
|
+
);
|
|
1643
|
+
|
|
1644
|
+
api.action(
|
|
1645
|
+
action(workflowGet, {
|
|
1646
|
+
// Nothing is written: it may be made again.
|
|
1647
|
+
recovery: "repeat",
|
|
1648
|
+
effect: "read",
|
|
1649
|
+
world: "niwa",
|
|
1650
|
+
run: (input) => {
|
|
1651
|
+
const found = workflowOf(input.id);
|
|
1652
|
+
const versions = found.versions.map((one) => {
|
|
1653
|
+
const stored = storedEntry(api.home(), found.id, one.version);
|
|
1654
|
+
return stored === undefined ? one : { ...one, stored };
|
|
1655
|
+
});
|
|
1656
|
+
return {
|
|
1657
|
+
id: found.id,
|
|
1658
|
+
description: found.latest.description,
|
|
1659
|
+
versions,
|
|
1660
|
+
installations: installationsOfWorkflow(found.id),
|
|
1661
|
+
...(found.builtin === undefined ? {} : { builtin: found.builtin }),
|
|
1662
|
+
};
|
|
1663
|
+
},
|
|
1664
|
+
}),
|
|
1665
|
+
);
|
|
1666
|
+
|
|
1667
|
+
// --- built-in workflows -----------------------------------------------------------------
|
|
1668
|
+
|
|
1669
|
+
/** The handler that registers and installs the built-ins: the one caller the sync answers. */
|
|
1670
|
+
const BUILTINS_HANDLER = "builtins";
|
|
1671
|
+
|
|
1672
|
+
/** The built-ins the plugins offer, as the host wants them now: each file read for its version. */
|
|
1673
|
+
const wantedBuiltins = (): WantedBuiltin[] => {
|
|
1674
|
+
const wanted: WantedBuiltin[] = [];
|
|
1675
|
+
for (const { plugin: by, value } of api.offers(BUILTIN_WORKFLOW)) {
|
|
1676
|
+
if (wanted.some((one) => one.plugin === by && one.name === value.name)) {
|
|
1677
|
+
throw new Error(`the plugin "${by}" offers two built-in workflows named "${value.name}": a name names one`);
|
|
1678
|
+
}
|
|
1679
|
+
const version = versionOf(value.file);
|
|
1680
|
+
if (version === undefined) throw new Error(`the built-in workflow "${value.name}" of "${by}" has no script at "${value.file}"`);
|
|
1681
|
+
wanted.push(WantedBuiltin.parse({ plugin: by, name: value.name, file: value.file, description: value.description, version, ...(value.gated === true ? { gated: true } : {}), ...(value.suite === true ? { suite: true } : {}) }));
|
|
1682
|
+
}
|
|
1683
|
+
return wanted;
|
|
1684
|
+
};
|
|
1685
|
+
|
|
1686
|
+
const sameBuiltin = (one: Builtin | undefined, other: Builtin): boolean => one?.plugin === other.plugin && one.name === other.name;
|
|
1687
|
+
|
|
1688
|
+
/** The installation that is this built-in as it stands: of its workflow, at its accepted latest version, under its description. */
|
|
1689
|
+
const isCurrent = (one: Installation, registered: RegisteredWorkflow, item: Builtin): boolean =>
|
|
1690
|
+
sameBuiltin(one.builtin, item) &&
|
|
1691
|
+
one.workflow === registered.id &&
|
|
1692
|
+
one.version === registered.latest.version &&
|
|
1693
|
+
one.description === registered.latest.description;
|
|
1694
|
+
|
|
1695
|
+
/** Whether a built-in stands installed: its latest version is accepted and has records to start on. */
|
|
1696
|
+
const installable = (registered: RegisteredWorkflow): boolean => registered.latest.candidate !== true && (registered.latest.on?.length ?? 0) > 0;
|
|
1697
|
+
|
|
1698
|
+
/**
|
|
1699
|
+
* Whether a wanted built-in is registered as it is wanted: its version is the latest one, or, for a gated built-in,
|
|
1700
|
+
* one of its versions, a candidate until it is promoted.
|
|
1701
|
+
*/
|
|
1702
|
+
const isRegistered = (registered: RegisteredWorkflow, item: WantedBuiltin): boolean => {
|
|
1703
|
+
if (item.gated !== true) return registered.latest.version === item.version && registered.latest.description === item.description;
|
|
1704
|
+
return registered.versions.some((one) => one.version === item.version);
|
|
1705
|
+
};
|
|
1706
|
+
|
|
1707
|
+
/**
|
|
1708
|
+
* Whether the built-ins that stand are exactly the wanted ones: each registered as it is wanted and installed once at
|
|
1709
|
+
* its accepted latest version, a built-in with no accepted version not at all, and no other built-in installed. A host
|
|
1710
|
+
* that starts again with nothing changed writes nothing.
|
|
1711
|
+
*/
|
|
1712
|
+
const builtinsSettled = (reader: JournalReader, wanted: readonly WantedBuiltin[]): boolean => {
|
|
1713
|
+
const workflows = workflowsOf(reader);
|
|
1714
|
+
const stands = standing(reader).filter((one) => one.builtin !== undefined);
|
|
1715
|
+
let installed = 0;
|
|
1716
|
+
const each = wanted.every((item) => {
|
|
1717
|
+
const registered = builtinOf(workflows, item);
|
|
1718
|
+
if (registered === undefined || !isRegistered(registered, item)) return false;
|
|
1719
|
+
if (!installable(registered)) return !stands.some((one) => sameBuiltin(one.builtin, item));
|
|
1720
|
+
installed += 1;
|
|
1721
|
+
return stands.some((one) => isCurrent(one, registered, item));
|
|
1722
|
+
});
|
|
1723
|
+
return each && stands.length === installed;
|
|
1724
|
+
};
|
|
1725
|
+
|
|
1726
|
+
// When the host starts, the built-ins are compared with what stands, and asked for when they differ: a new build of
|
|
1727
|
+
// niwa with a changed script, a plugin that offers a new one or no longer offers one.
|
|
1728
|
+
api.service((ctx) => {
|
|
1729
|
+
hostClock = ctx;
|
|
1730
|
+
// The packages offered now are the ones a stored copy finds: a package no plugin offers any more is not found.
|
|
1731
|
+
const scripts = scriptPackages();
|
|
1732
|
+
for (const problem of scripts.refused) console.error(`niwa: ${problem}`);
|
|
1733
|
+
linkOfferedPackages(api.home(), scripts.packages);
|
|
1734
|
+
// The deadlines of the runs that are going are waited for again: one that passed while the host was away comes now.
|
|
1735
|
+
for (const run of new Set(journal().find({ type: RunRequested.name }).map((row) => RunRequested.schema.parse(row.data).run))) {
|
|
1736
|
+
if (statusOf(journal(), run).state !== "finished") watchDeadline(run);
|
|
1737
|
+
}
|
|
1738
|
+
void (async () => {
|
|
1739
|
+
const wanted = wantedBuiltins();
|
|
1740
|
+
const reader = journal();
|
|
1741
|
+
const current = builtinOfferOf(reader);
|
|
1742
|
+
if (stableJson(wanted) !== stableJson(current?.data.items ?? []) || !builtinsSettled(reader, wanted)) {
|
|
1743
|
+
await ctx.emit(BuiltinsWanted, { items: wanted });
|
|
1744
|
+
}
|
|
1745
|
+
})().catch((error: unknown) => {
|
|
1746
|
+
console.error(`niwa: the built-in workflows could not be installed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1747
|
+
});
|
|
1748
|
+
return () => {
|
|
1749
|
+
for (const stop of deadlineTimers.values()) stop();
|
|
1750
|
+
deadlineTimers.clear();
|
|
1751
|
+
};
|
|
1752
|
+
});
|
|
1753
|
+
|
|
1754
|
+
/**
|
|
1755
|
+
* The installation of a built-in made what its registration says: one at its accepted latest version, and none while
|
|
1756
|
+
* it has only a candidate or nothing to start on (a suite is started by hand). An installation of another version is ended.
|
|
1757
|
+
*/
|
|
1758
|
+
const installBuiltin = async (ctx: ActionContext, registered: RegisteredWorkflow, builtin: Builtin): Promise<{ installation?: string }> => {
|
|
1759
|
+
const stands = standing(journal()).filter((one) => sameBuiltin(one.builtin, builtin));
|
|
1760
|
+
const current = installable(registered) ? stands.find((one) => isCurrent(one, registered, builtin)) : undefined;
|
|
1761
|
+
for (const stale of stands) if (stale !== current) await uninstallOne(ctx, stale.id, "replaced by another version of the built-in");
|
|
1762
|
+
if (!installable(registered)) return {};
|
|
1763
|
+
if (current !== undefined) return { installation: current.id };
|
|
1764
|
+
const installed = await installScript(ctx, { registered, description: registered.latest.description, builtin });
|
|
1765
|
+
return { installation: installed.id };
|
|
1766
|
+
};
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* One built-in made what it is wanted to be: registered from the file its plugin ships (its id found by the plugin and
|
|
1770
|
+
* the name), and installed once at its latest version; an installation of an earlier version is ended. Each part looks
|
|
1771
|
+
* at what stands first, so a sync made again after a cut makes nothing twice.
|
|
1772
|
+
*/
|
|
1773
|
+
const builtinSync = action("workflow.builtin.sync", {
|
|
1774
|
+
description: "Register a built-in workflow from the file its plugin ships and install it at its latest version. For the workflow module itself, when the host starts.",
|
|
1775
|
+
input: WantedBuiltin,
|
|
1776
|
+
output: BuiltinSynced,
|
|
1777
|
+
recovery: "repeat",
|
|
1778
|
+
effect: "record",
|
|
1779
|
+
world: "niwa",
|
|
1780
|
+
run: async (item, ctx) => {
|
|
1781
|
+
const caller = ctx.caller;
|
|
1782
|
+
if (caller.kind !== "handler" || String(caller.handler) !== `workflow/${BUILTINS_HANDLER}`) {
|
|
1783
|
+
throw new DeniedError("only the workflow module installs its built-ins");
|
|
1784
|
+
}
|
|
1785
|
+
const builtin: Builtin = { plugin: item.plugin, name: item.name };
|
|
1786
|
+
const existing = builtinOf(workflowsOf(journal()), builtin);
|
|
1787
|
+
// A gated built-in registers a version it does not know as a candidate; one it knows, accepted or not, stays as it is.
|
|
1788
|
+
const id =
|
|
1789
|
+
item.gated === true && existing !== undefined && isRegistered(existing, item)
|
|
1790
|
+
? existing.id
|
|
1791
|
+
: (await registerScript(ctx, item.file, existing, { builtin, description: item.description, ...(item.gated === true ? { candidate: true as const } : {}) })).id;
|
|
1792
|
+
return { workflow: id, ...(await installBuiltin(ctx, workflowOf(id), builtin)) };
|
|
1793
|
+
},
|
|
1794
|
+
});
|
|
1795
|
+
api.action(builtinSync);
|
|
1796
|
+
|
|
1797
|
+
/** A built-in no plugin offers any more is uninstalled; its registration stays, with its runs. */
|
|
1798
|
+
const builtinRetire = action("workflow.builtin.retire", {
|
|
1799
|
+
description: "Uninstall the built-in workflows no plugin offers any more. For the workflow module itself, when the host starts.",
|
|
1800
|
+
input: BuiltinRetireInput,
|
|
1801
|
+
output: BuiltinRetired,
|
|
1802
|
+
recovery: "repeat",
|
|
1803
|
+
effect: "record",
|
|
1804
|
+
world: "niwa",
|
|
1805
|
+
run: async ({ keep }, ctx) => {
|
|
1806
|
+
const caller = ctx.caller;
|
|
1807
|
+
if (caller.kind !== "handler" || String(caller.handler) !== `workflow/${BUILTINS_HANDLER}`) {
|
|
1808
|
+
throw new DeniedError("only the workflow module uninstalls its built-ins");
|
|
1809
|
+
}
|
|
1810
|
+
const gone = standing(journal()).filter((one) => one.builtin !== undefined && !keep.some((kept) => sameBuiltin(one.builtin, kept)));
|
|
1811
|
+
for (const one of gone) await uninstallOne(ctx, one.id, "its plugin no longer offers it");
|
|
1812
|
+
return { uninstalled: gone.map((one) => one.id) };
|
|
1813
|
+
},
|
|
1814
|
+
});
|
|
1815
|
+
api.action(builtinRetire);
|
|
1816
|
+
|
|
1817
|
+
api.action(action(workflowBuiltinTrial, {
|
|
1818
|
+
recovery: "repeat",
|
|
1819
|
+
effect: "record",
|
|
1820
|
+
world: "niwa",
|
|
1821
|
+
async run(_, ctx) {
|
|
1822
|
+
const { run, plugin } = trialRun(ctx.caller);
|
|
1823
|
+
const existing = trialOf(run);
|
|
1824
|
+
if (existing !== undefined) return { entries: existing.entries };
|
|
1825
|
+
const reader = journal();
|
|
1826
|
+
const offer = builtinOfferOf(reader);
|
|
1827
|
+
if (offer === undefined) throw new RunRefusal("no-builtin-offer", "the built-in offer is not recorded yet");
|
|
1828
|
+
const workflows = workflowsOf(reader);
|
|
1829
|
+
const entries = offer.data.items.filter((item) => item.plugin === plugin && item.gated === true).map((item) => {
|
|
1830
|
+
const registered = builtinOf(workflows, item);
|
|
1831
|
+
const offered = registered?.versions.find((one) => one.version === item.version);
|
|
1832
|
+
if (registered === undefined || offered === undefined) throw new RunRefusal("pending-builtin", `the offered version of ${item.name} is still registering`);
|
|
1833
|
+
const selected = offered.candidate === true ? offered : registered.latest;
|
|
1834
|
+
if (selected.superseded === true || (selected.candidate === true && selected !== offered)) {
|
|
1835
|
+
throw new RunRefusal("superseded-builtin", `the offered version of ${item.name} has already been superseded`);
|
|
1836
|
+
}
|
|
1837
|
+
return { name: item.name, workflow: registered.id, version: selected.version, revision: selected.revision, ...(selected.candidate === true ? { candidate: true as const } : {}) };
|
|
1838
|
+
});
|
|
1839
|
+
try {
|
|
1840
|
+
await ctx.emit(BuiltinTrial, { run, plugin, offer: offer.id, entries });
|
|
1841
|
+
return { entries };
|
|
1842
|
+
} catch (error) {
|
|
1843
|
+
if (!(error instanceof RecordRefused) || error.code !== "builtin-trial-exists") throw error;
|
|
1844
|
+
return { entries: trialOf(run)!.entries };
|
|
1845
|
+
}
|
|
1846
|
+
},
|
|
1847
|
+
}));
|
|
1848
|
+
|
|
1849
|
+
api.action(action(workflowBuiltinPromote, {
|
|
1850
|
+
recovery: "repeat",
|
|
1851
|
+
effect: "record",
|
|
1852
|
+
world: "niwa",
|
|
1853
|
+
async run(_, ctx) {
|
|
1854
|
+
const { run, plugin } = trialRun(ctx.caller);
|
|
1855
|
+
const reader = journal();
|
|
1856
|
+
const previous = reader.find({ type: BuiltinPromoted.name, field: "run", equals: run }).at(-1);
|
|
1857
|
+
if (previous !== undefined) return { status: "promoted", run, versions: BuiltinPromoted.schema.parse(previous.data).versions };
|
|
1858
|
+
const trial = trialOf(run);
|
|
1859
|
+
if (trial === undefined || trial.plugin !== plugin) throw new RunRefusal("no-builtin-trial", "the suite has not pinned an offered set");
|
|
1860
|
+
const versions = trial.entries.filter((entry) => entry.candidate === true).map(({ workflow, version }) => ({ workflow, version }));
|
|
1861
|
+
if (versions.length === 0) return { status: "promoted", run, versions };
|
|
1862
|
+
const offer = builtinOfferOf(reader);
|
|
1863
|
+
if (offer?.id !== trial.offer) return { status: "stale", run, versions: [] };
|
|
1864
|
+
try {
|
|
1865
|
+
await ctx.emitAll([
|
|
1866
|
+
{ type: BuiltinPromoted, data: { ...trial, versions } },
|
|
1867
|
+
{ type: BuiltinsWanted, data: offer.data },
|
|
1868
|
+
]);
|
|
1869
|
+
} catch (error) {
|
|
1870
|
+
if (!(error instanceof RecordRefused) || error.code !== "stale-builtin-trial") throw error;
|
|
1871
|
+
const completed = journal().find({ type: BuiltinPromoted.name, field: "run", equals: run }).at(-1);
|
|
1872
|
+
if (completed !== undefined) return { status: "promoted", run, versions: BuiltinPromoted.schema.parse(completed.data).versions };
|
|
1873
|
+
return { status: "stale", run, versions: [] };
|
|
1874
|
+
}
|
|
1875
|
+
return { status: "promoted", run, versions };
|
|
1876
|
+
},
|
|
1877
|
+
}));
|
|
1878
|
+
|
|
1879
|
+
api.on(BuiltinsWanted, { id: BUILTINS_HANDLER }, async (data, step) => {
|
|
1880
|
+
await step(builtinRetire, { keep: data.items.map(({ plugin, name }) => ({ plugin, name })) }, { key: "retire" });
|
|
1881
|
+
// The built-ins do not wait on each other, and each script is read in a process of its own: they go together. The
|
|
1882
|
+
// steps are keyed by the built-in, so a replay meets each one whatever the order they ran in.
|
|
1883
|
+
await Promise.all(data.items.map((item) => step(builtinSync, item, { key: `sync:${item.plugin}:${item.name}` })));
|
|
1884
|
+
});
|
|
1885
|
+
|
|
1886
|
+
/** The action of a version of a workflow, made once: its schemas are read from the registration, not the script. */
|
|
1887
|
+
const flows = new Map<string, Action<any, any>>();
|
|
1888
|
+
const flowOf = (registered: RegisteredWorkflow): Action<any, any> => {
|
|
1889
|
+
const key = `${registered.id}@${registered.latest.version}@${registered.latest.broken ?? ""}`;
|
|
1890
|
+
const made = flows.get(key);
|
|
1891
|
+
if (made !== undefined) return made;
|
|
1892
|
+
const latest = registered.latest;
|
|
1893
|
+
const flow = action(flowActionName(registered.id), {
|
|
1894
|
+
description: `${latest.description}. Runs the workflow ${registered.id} to its end and answers what it returns.${latest.broken === undefined ? "" : ` Broken, it does not start: ${latest.broken}`}`,
|
|
1895
|
+
input: latest.input === undefined ? z.object({}) : schemaOf(latest.input),
|
|
1896
|
+
output: latest.output === undefined ? z.null() : schemaOf(latest.output),
|
|
1897
|
+
// The run is asked for: a call that was cut off may have begun one, so it is not made again.
|
|
1898
|
+
recovery: "do-not-repeat",
|
|
1899
|
+
effect: "record",
|
|
1900
|
+
world: "niwa",
|
|
1901
|
+
run: async (input: unknown, ctx: ActionContext) => {
|
|
1902
|
+
const current = workflowOf(registered.id);
|
|
1903
|
+
// A run that calls a workflow is the parent of the run it starts: a cancel and a pause go down to it.
|
|
1904
|
+
const caller = ctx.caller.kind === "client" && ctx.caller.principal?.kind === "run" ? runIdOfPrincipal(ctx.caller.principal.id) : undefined;
|
|
1905
|
+
const parent = caller === undefined ? undefined : { run: caller, step: String(ctx.operationId) };
|
|
1906
|
+
// A workflow that takes no input is called with an empty object, and its run is given nothing.
|
|
1907
|
+
const args = current.latest.input === undefined ? undefined : input;
|
|
1908
|
+
const run = await requestRun(ctx, { ...registeredTarget(current), args, ...(parent === undefined ? {} : { parent }) });
|
|
1909
|
+
const status = await settleFully(run);
|
|
1910
|
+
const outcome = status.outcome;
|
|
1911
|
+
if (outcome?.status === "done") return outcome.result;
|
|
1912
|
+
if (outcome?.status === "failed") throw new RunRefusal("flow-failed", `the run ${run} of ${registered.id} failed: ${outcome.error.message}`);
|
|
1913
|
+
if (outcome?.status === "cancelled") throw new RunRefusal("flow-cancelled", `the run ${run} of ${registered.id} was cancelled`);
|
|
1914
|
+
throw new RunRefusal("flow-paused", `the run ${run} of ${registered.id} paused: ${status.reason ?? "a decision is needed"}. Answer it with workflow.resume`);
|
|
1915
|
+
},
|
|
1916
|
+
});
|
|
1917
|
+
flows.set(key, flow);
|
|
1918
|
+
return flow;
|
|
1919
|
+
};
|
|
1920
|
+
// Every registered workflow is an action: the catalog grows from the journal.
|
|
1921
|
+
api.catalog(() => (api.journal?.() === undefined ? [] : workflowsOf(journal()).map(flowOf)));
|
|
1922
|
+
|
|
1923
|
+
/** A calling workflow keeps waiting through a manual pause of its child. */
|
|
1924
|
+
const settleFully = async (run: RunId): Promise<RunStatus> => {
|
|
1925
|
+
for (;;) {
|
|
1926
|
+
const status = await settle(run, DEFAULT_WAIT_MS, false);
|
|
1927
|
+
if (settled(status, false)) return status;
|
|
1928
|
+
}
|
|
1929
|
+
};
|
|
1930
|
+
|
|
1931
|
+
// A session started by an MCP helper has no step in the script's invocation.
|
|
1932
|
+
// The durable ownership record includes these sessions even after a restart.
|
|
1933
|
+
const finishedExecution = (run: string, execution = run) =>
|
|
1934
|
+
journal().find({ type: RunFinished.name, field: "run", equals: run }).some(row => {
|
|
1935
|
+
const finish = RunFinished.schema.parse(row.data);
|
|
1936
|
+
return (finish.execution ?? run) === execution;
|
|
1937
|
+
}) || (latestRequest(run)?.execution ?? run) !== execution;
|
|
1938
|
+
|
|
1939
|
+
const closeExecution = async (run: string, execution: string, step: Parameters<Handler<unknown>>[1], cancelled = false) => {
|
|
1940
|
+
const reason = cancelled ? `the run ${run} that started it was cancelled` : `the run ${run} finished`;
|
|
1941
|
+
for (const row of journal().find({ type: SessionStarted.name, field: "run", equals: run })) {
|
|
1942
|
+
const started = SessionStarted.schema.parse(row.data);
|
|
1943
|
+
if ((started.execution ?? run) === execution) await step.outcome(sessionEnd, { session: started.session, reason }, { key: `end:${digestOf(started.session)}` });
|
|
1944
|
+
}
|
|
1945
|
+
const hasGrant = journal().find({ type: AccessGranted.name, field: "principal.id", equals: execution })
|
|
1946
|
+
.some(row => AccessGranted.schema.parse(row.data).principal.kind === "run");
|
|
1947
|
+
if (hasGrant) {
|
|
1948
|
+
await step.outcome(actionName("access.grant.close"), { principal: principalOf("run", execution), reason }, { key: "close-grant" });
|
|
1949
|
+
} else {
|
|
1950
|
+
for (const row of journal().find({ type: SessionStartReserved.name, field: "run", equals: run })) {
|
|
1951
|
+
const reserved = SessionStartReserved.schema.parse(row.data);
|
|
1952
|
+
if ((reserved.execution ?? run) === execution && reserved.mark !== undefined) await step.outcome(actionName("access.grant.close"), {
|
|
1953
|
+
principal: principalOf("session", reserved.mark), reason,
|
|
1954
|
+
}, { key: `close:${reserved.mark}` });
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
api.on(RunFinished, { id: "end-run-sessions", concurrency: Infinity }, async ({ run, execution, outcome }, step) => {
|
|
1959
|
+
await closeExecution(run, execution ?? run, step, outcome.status === "cancelled");
|
|
1960
|
+
});
|
|
1961
|
+
api.on(RunRequested, { id: "end-superseded-execution", concurrency: Infinity }, async ({ run, execution }, step, stored) => {
|
|
1962
|
+
if (execution === undefined) return;
|
|
1963
|
+
const previous = journal().find({ type: RunRequested.name, field: "run", equals: run }).filter(row => row.seq < stored.seq).at(-1);
|
|
1964
|
+
if (previous === undefined) return;
|
|
1965
|
+
const prior = RunRequested.schema.parse(previous.data).execution ?? run;
|
|
1966
|
+
const finished = journal().find({ type: RunFinished.name, field: "run", equals: run })
|
|
1967
|
+
.some(row => (RunFinished.schema.parse(row.data).execution ?? run) === prior);
|
|
1968
|
+
if (!finished) await closeExecution(run, prior, step);
|
|
1969
|
+
});
|
|
1970
|
+
api.on(SessionStartReserved, { id: "close-late-session-grant", concurrency: Infinity }, async ({ run, execution, mark }, step, stored) => {
|
|
1971
|
+
if (run === undefined || mark === undefined) return;
|
|
1972
|
+
const cancelled = journal().find({ type: CORE_TYPES.stepCancelled }).some(row => {
|
|
1973
|
+
const cancel = row.data as StepsCancelled;
|
|
1974
|
+
const key = sessionStep(stored, cancel.invocation);
|
|
1975
|
+
return key !== undefined && cancelHolds(cancel, key);
|
|
1976
|
+
});
|
|
1977
|
+
const scopeEnded = journal().find({ type: ScopeClosed.name, field: "run", equals: run }).some(row => {
|
|
1978
|
+
if (row.actor.kind !== "handler") return false;
|
|
1979
|
+
const key = sessionStep(stored, row.actor.invocation);
|
|
1980
|
+
return key !== undefined && keyUnder(key, ScopeClosed.schema.parse(row.data).scope);
|
|
1981
|
+
});
|
|
1982
|
+
if (cancelled || scopeEnded || finishedExecution(run, execution)) {
|
|
1983
|
+
await step.outcome(actionName("access.grant.close"), { principal: principalOf("session", mark), reason: cancelled || scopeEnded ? "the workflow scope ended" : `the run ${run} finished` });
|
|
1984
|
+
}
|
|
1985
|
+
});
|
|
1986
|
+
api.on(SessionStarted, { id: "end-late-run-session", concurrency: Infinity }, async ({ run, execution, session }, step) => {
|
|
1987
|
+
if (run !== undefined && finishedExecution(run, execution)) {
|
|
1988
|
+
await step(sessionEnd, { session, reason: `the run ${run} finished` });
|
|
1989
|
+
}
|
|
1990
|
+
});
|
|
1991
|
+
|
|
1992
|
+
// Runs are independent of each other: one that waits for an answer holds no other. A run that calls
|
|
1993
|
+
// another workflow requests a run from inside its own: the chain of calls goes as deep as CALL_DEPTH.
|
|
1994
|
+
api.on(
|
|
1995
|
+
RunRequested,
|
|
1996
|
+
{ concurrency: Number.POSITIVE_INFINITY, reentrant: { depth: CALL_DEPTH } },
|
|
1997
|
+
async (data, step) => {
|
|
1998
|
+
const { run, file } = data;
|
|
1999
|
+
// The run acts as its own principal: its grant is what the script is held to.
|
|
2000
|
+
const principal = principalOf("run", data.execution ?? run);
|
|
2001
|
+
const runCaller = callerOf(principal);
|
|
2002
|
+
const args = data.args ?? null;
|
|
2003
|
+
const script = readScript(file);
|
|
2004
|
+
// The hash of the script as it is now: a later resume that finds another hash knows the
|
|
2005
|
+
// script was edited, and the run it continues is a revision.
|
|
2006
|
+
const hash = data.hash ?? script?.hash;
|
|
2007
|
+
// What the module declares as it is now: the version of the script, with the files it imports, says whether it was read.
|
|
2008
|
+
const read = script === undefined ? undefined : await describe(file, boxFor(data.box), `describe:${run}`);
|
|
2009
|
+
const described = read !== undefined && "described" in read && read.described.problems.length === 0 ? read.described : undefined;
|
|
2010
|
+
const phases = data.phases ?? described?.phases;
|
|
2011
|
+
const contract = described === undefined ? undefined : contractOf(described);
|
|
2012
|
+
const records = script !== undefined && (await scriptOn({ file, text: script.text })) !== undefined;
|
|
2013
|
+
// A contract that is not the one the run was asked under: the arguments it recorded must pass the input it has now.
|
|
2014
|
+
const changed =
|
|
2015
|
+
described !== undefined && data.contract !== undefined && contract !== data.contract ? inputIssues(described.input, records, args) : [];
|
|
2016
|
+
const started = await step.emit(
|
|
2017
|
+
RunStarted,
|
|
2018
|
+
{
|
|
2019
|
+
run,
|
|
2020
|
+
file,
|
|
2021
|
+
args,
|
|
2022
|
+
...(data.label === undefined ? {} : { label: data.label }),
|
|
2023
|
+
...(hash === undefined ? {} : { hash }),
|
|
2024
|
+
// A run that stops on a changed contract keeps the contract it began under: a resume meets the same change.
|
|
2025
|
+
...((changed.length > 0 ? data.contract : contract) === undefined ? {} : { contract: changed.length > 0 ? data.contract : contract }),
|
|
2026
|
+
...(data.box === true ? { box: true } : {}),
|
|
2027
|
+
...(phases === undefined ? {} : { phases }),
|
|
2028
|
+
...originOf(data),
|
|
2029
|
+
},
|
|
2030
|
+
{ key: "started" },
|
|
2031
|
+
);
|
|
2032
|
+
const current = started.actor.kind === "handler" ? started.actor.invocation : undefined;
|
|
2033
|
+
/** The titles of the phases the script declares: what `phase(title)` may name. */
|
|
2034
|
+
const titles = (phases ?? []).map((phase) => phase.title);
|
|
2035
|
+
// The calls the earlier starts of this run finished: a call with the same key and input is
|
|
2036
|
+
// taken from them along the longest unchanged prefix (see revise.ts).
|
|
2037
|
+
const before = factsOf(journal(), run)?.invocations.filter((invocation) => invocation !== current) ?? [];
|
|
2038
|
+
const earlier = recordedCalls(journal(), api.artifacts, before);
|
|
2039
|
+
|
|
2040
|
+
// The notes of the script are emitted one after another, so a replay finds them in order.
|
|
2041
|
+
// Each note has a key of its own, and the key says what the note is about: a run that is
|
|
2042
|
+
// answered and goes on ends differently than the run that paused, and the two must not
|
|
2043
|
+
// write over each other. Every kind counts its own notes, so one kind does not move the
|
|
2044
|
+
// keys of another.
|
|
2045
|
+
let notes: Promise<unknown> = Promise.resolve();
|
|
2046
|
+
const ordinals = new Map<string, number>();
|
|
2047
|
+
const noteAt = <T>(type: Parameters<typeof step.emit<T>>[0], body: T, key: string): void => {
|
|
2048
|
+
notes = notes.then(() => step.emit(type, body, { key }));
|
|
2049
|
+
};
|
|
2050
|
+
const note = <T>(type: Parameters<typeof step.emit<T>>[0], body: T, kind: string): void => {
|
|
2051
|
+
const ordinal = (ordinals.get(kind) ?? 0) + 1;
|
|
2052
|
+
ordinals.set(kind, ordinal);
|
|
2053
|
+
noteAt(type, body, `${kind}:${String(ordinal)}`);
|
|
2054
|
+
};
|
|
2055
|
+
|
|
2056
|
+
// Whether a step of the script did not complete: the core paused the invocation on that
|
|
2057
|
+
// step, and the invocation ends as paused by itself.
|
|
2058
|
+
let stepFailed = false;
|
|
2059
|
+
|
|
2060
|
+
const call = bridge({
|
|
2061
|
+
step,
|
|
2062
|
+
run: principal,
|
|
2063
|
+
runId: run,
|
|
2064
|
+
...(data.concurrency === undefined ? {} : { concurrency: data.concurrency }),
|
|
2065
|
+
schedule: scheduleAt,
|
|
2066
|
+
cancelChild,
|
|
2067
|
+
heldBy: (invocation, prefixes, except) => heldBy(journal(), invocation, prefixes, except),
|
|
2068
|
+
journal,
|
|
2069
|
+
agentAction: actionFor,
|
|
2070
|
+
agentName: actionNameOf,
|
|
2071
|
+
emits: emitTypesOf(described),
|
|
2072
|
+
recordType: (name) => emits.get(name) ?? api.recordType(name),
|
|
2073
|
+
earlier,
|
|
2074
|
+
...(data.from === undefined ? {} : { from: data.from }),
|
|
2075
|
+
emitted: (key) => {
|
|
2076
|
+
// The records an emit writes have the id `<invocation>/emit/<key>`: the latest start that wrote one is the one that counts.
|
|
2077
|
+
const found = before.flatMap((invocation) => {
|
|
2078
|
+
const row = journal().get(recordId(`${invocation}/emit/${key}`));
|
|
2079
|
+
return row === undefined ? [] : [{ invocation, row }];
|
|
2080
|
+
});
|
|
2081
|
+
const last = found.at(-1);
|
|
2082
|
+
return last === undefined ? undefined : { type: last.row.type, data: last.row.data, invocation: last.invocation };
|
|
2083
|
+
},
|
|
2084
|
+
failed: () => {
|
|
2085
|
+
stepFailed = true;
|
|
2086
|
+
},
|
|
2087
|
+
authorize: async (verb, name, how) => {
|
|
2088
|
+
// A hold refuses what would change something: reads and niwa's own records go on.
|
|
2089
|
+
const effect = verb === "call" ? api.effectOf(name) : undefined;
|
|
2090
|
+
if (effect === "write" || effect === "irreversible") {
|
|
2091
|
+
const hold = holdOver(journal(), run);
|
|
2092
|
+
if (hold !== undefined) {
|
|
2093
|
+
throw new DeniedError(`the run is on hold (${String(hold.id)}): "${name}" ${effect === "write" ? "writes" : "cannot be undone"}, and is refused until the hold is released`);
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
// A decision under an `allow` names the version of it, as the start of the run kept it.
|
|
2097
|
+
await api.authorize(runCaller, verb, name, { ...how, ...(data.allow === undefined ? {} : { policy: data.allow.version }) });
|
|
2098
|
+
},
|
|
2099
|
+
visible: (masks) => {
|
|
2100
|
+
const narrowed = api.narrow(runCaller, masks);
|
|
2101
|
+
if ("refused" in narrowed) throw new UsesRefused(narrowed.refused.map((text) => text.replace(/^call:/, "")), "a session of the run");
|
|
2102
|
+
},
|
|
2103
|
+
});
|
|
2104
|
+
|
|
2105
|
+
// A run that was cancelled stays cancelled: a replay after a restart finds the end of the
|
|
2106
|
+
// run in the journal and does not start the script (or its calls) again. Nothing is awaited
|
|
2107
|
+
// between this check and the spawn of the child, so a cancel cannot slip between them.
|
|
2108
|
+
const cancelled = journal()
|
|
2109
|
+
.find({ type: RunFinished.name, field: "run", equals: run })
|
|
2110
|
+
.some((record) => {
|
|
2111
|
+
const finish = RunFinished.schema.parse(record.data);
|
|
2112
|
+
return (finish.execution ?? run) === (data.execution ?? run) && finish.outcome.status === "cancelled";
|
|
2113
|
+
});
|
|
2114
|
+
if (cancelled) return;
|
|
2115
|
+
|
|
2116
|
+
// A pause is a journal-backed wait within the same invocation. Keep one waiter per pause
|
|
2117
|
+
// when several script branches reach the boundary together.
|
|
2118
|
+
const pauseWaits = new Map<string, Promise<unknown>>();
|
|
2119
|
+
const pauseKey = (pause: StoredRecord) => `manual-pause:${pause.seq}`;
|
|
2120
|
+
const waitForContinue = (pause: StoredRecord): Promise<unknown> => {
|
|
2121
|
+
const key = pauseKey(pause);
|
|
2122
|
+
let waiting = pauseWaits.get(key);
|
|
2123
|
+
if (waiting === undefined) {
|
|
2124
|
+
waiting = step.wait(RunContinued, { filter: { run, pause: pause.id }, key });
|
|
2125
|
+
pauseWaits.set(key, waiting);
|
|
2126
|
+
}
|
|
2127
|
+
return waiting;
|
|
2128
|
+
};
|
|
2129
|
+
const atBoundary = async <T>(enter: () => T | Promise<T>): Promise<T> => {
|
|
2130
|
+
for (;;) {
|
|
2131
|
+
const pause = activeManualPause(journal(), run);
|
|
2132
|
+
if (pause === undefined) return enter();
|
|
2133
|
+
await waitForContinue(pause);
|
|
2134
|
+
}
|
|
2135
|
+
};
|
|
2136
|
+
// Settle old waits on replay before checking for a new pause. Otherwise an obsolete intent
|
|
2137
|
+
// would stay open in the host's index when the script subsequently waits for something else.
|
|
2138
|
+
for (const pause of manualPausesOf(journal(), run)) {
|
|
2139
|
+
if (current !== undefined && journal().get(recordId(`${current}/step/${pauseKey(pause)}/intent`)) !== undefined) {
|
|
2140
|
+
await waitForContinue(pause);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
await atBoundary(() => undefined);
|
|
2144
|
+
|
|
2145
|
+
// The script is pinned by the hash the run began with: a file that was edited since, while the
|
|
2146
|
+
// run was away, is not run under the old calls. The run waits for the owner, who revises it.
|
|
2147
|
+
const onDisk = readScript(file)?.hash;
|
|
2148
|
+
if (data.hash !== undefined && onDisk !== undefined && onDisk !== data.hash) {
|
|
2149
|
+
const reason = "the script was edited since the run began: the run needs a revision";
|
|
2150
|
+
await step.emit(RunPaused, { run, reason, choices: ["cancel"] }, { key: `paused:${digestOf(reason)}` });
|
|
2151
|
+
return;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
// The copy of a registered version runs only as its manifest says it is: the run is read through the same manifest as its start.
|
|
2155
|
+
const stored = data.workflow === undefined || data.version === undefined ? undefined : storedVersion(api.home(), data.workflow, data.version);
|
|
2156
|
+
// The module cannot be read for its schemas, or its contract changed so that what the run recorded is no longer its input:
|
|
2157
|
+
// the run waits for the owner, who revises it or cancels it.
|
|
2158
|
+
const stopped =
|
|
2159
|
+
stored !== undefined && "problem" in stored
|
|
2160
|
+
? stored.problem
|
|
2161
|
+
: script !== undefined && described === undefined
|
|
2162
|
+
? `the module cannot be read for its schemas: ${read !== undefined && "failed" in read ? read.failed : (read?.described.problems.map((problem) => problem.message).join("; ") ?? "no reading")}`
|
|
2163
|
+
: changed.length > 0
|
|
2164
|
+
? `contract-changed: the arguments of the run no longer pass the input of the script (${changed.map((issue) => `${issue.path === "" ? "input" : issue.path}: ${issue.message}`).join("; ")})`
|
|
2165
|
+
: undefined;
|
|
2166
|
+
if (stopped !== undefined) {
|
|
2167
|
+
await step.emit(RunPaused, { run, reason: stopped, choices: ["cancel"] }, { key: `paused:${digestOf(stopped)}` });
|
|
2168
|
+
return;
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
const ended = await runScript(
|
|
2172
|
+
{
|
|
2173
|
+
// Commit the preceding phases and log lines before a synchronous call such as now() writes its intent.
|
|
2174
|
+
call: async (request) => {
|
|
2175
|
+
await notes;
|
|
2176
|
+
return atBoundary(async () => {
|
|
2177
|
+
const { phase } = request;
|
|
2178
|
+
// The phase the branch of the step is in, under the key of the step: a replay writes the same record.
|
|
2179
|
+
if (phase !== undefined && titles.includes(phase)) {
|
|
2180
|
+
await step.emit(StepPhase, { run, key: request.key, phase }, { key: `phase:${request.key}` });
|
|
2181
|
+
}
|
|
2182
|
+
return call(request);
|
|
2183
|
+
});
|
|
2184
|
+
},
|
|
2185
|
+
// The child holds the title to the declared ones; a line that is not one of them is not the child's and is not recorded.
|
|
2186
|
+
// Keyed by the branch and the number of the change within it, so a replay writes the same records whatever the
|
|
2187
|
+
// order the branches ran in.
|
|
2188
|
+
phase: ({ branch, ordinal, phase }) => {
|
|
2189
|
+
if (titles.includes(phase)) noteAt(PhaseEntered, { run, branch, phase }, `phase-entered:${branch === "" ? "script" : branch}:${String(ordinal)}`);
|
|
2190
|
+
},
|
|
2191
|
+
left: (branch) => noteAt(PhaseLeft, { run, branch }, `phase-left:${branch}`),
|
|
2192
|
+
branched: ({ scope, key }) => noteAt(ScopeBranched, { run, scope, key }, `scope-branch:${scope}.${key}`),
|
|
2193
|
+
gone: () => call.gone(),
|
|
2194
|
+
log: (text) => note(RunLog, { run, text }, "log"),
|
|
2195
|
+
// A cancelled run's calls that are in flight fail because of the cancel: they pause nothing.
|
|
2196
|
+
stopped: () => step.abandon(),
|
|
2197
|
+
fresh: (key) => call.fresh(key),
|
|
2198
|
+
},
|
|
2199
|
+
file,
|
|
2200
|
+
args,
|
|
2201
|
+
run,
|
|
2202
|
+
boxFor(data.box),
|
|
2203
|
+
// A run that suspends (a wait, a sleep) still writes what its branches said before it stopped: a branch that ended
|
|
2204
|
+
// is not shown in its phase while the run waits.
|
|
2205
|
+
).finally(() => notes.catch(() => undefined));
|
|
2206
|
+
await notes;
|
|
2207
|
+
|
|
2208
|
+
// The run was cancelled while it was going: it ends there, and the child is stopped.
|
|
2209
|
+
// The cancel wrote the end of the run, so nothing else is written here.
|
|
2210
|
+
if (ended.cancelled === true) return;
|
|
2211
|
+
|
|
2212
|
+
// The end of the run is written under a key of its own. The key says how the run ended, so
|
|
2213
|
+
// a run that is answered and goes on does not write over the run that paused, and a retry
|
|
2214
|
+
// that fails another way does not write over the pause before it.
|
|
2215
|
+
if (ended.ok) {
|
|
2216
|
+
await atBoundary(() => step.emit(RunFinished, { run, execution: data.execution, outcome: outcomeOf(described?.output, ended.result) }, { key: "finished" }));
|
|
2217
|
+
// The sessions its agent calls started were kept up for it while it ran: they go with it.
|
|
2218
|
+
for (const session of sessionsOf(journal(), current === undefined ? [] : [current])) {
|
|
2219
|
+
await step.outcome(sessionEnd, { session, reason: `the run ${run} that started it finished` }, { key: `end-session:${digestOf(String(session))}` });
|
|
2220
|
+
}
|
|
2221
|
+
return;
|
|
2222
|
+
}
|
|
2223
|
+
// The script did not catch the end of an `ask` the owner cancelled or nobody answered. A retry
|
|
2224
|
+
// replays the run, and the replay throws the same error again (the end is recorded under
|
|
2225
|
+
// `<key>~ended`): there is nothing to retry or skip. The run waits for the owner, who revises
|
|
2226
|
+
// the script to catch it or cancels the run, as it does for an edited script.
|
|
2227
|
+
const unanswered = stepFailed ? undefined : ASK_ENDINGS.get(ended.errorName ?? "");
|
|
2228
|
+
if (unanswered !== undefined) {
|
|
2229
|
+
await step.emit(RunPaused, { run, reason: unanswered, choices: ["cancel"] }, { key: `paused:${digestOf(unanswered)}` });
|
|
2230
|
+
return;
|
|
2231
|
+
}
|
|
2232
|
+
const reason = ended.error ?? "the script failed";
|
|
2233
|
+
// A step that failed is retried or skipped; an error of the script outside any step has no step
|
|
2234
|
+
// to skip, and its key says so, so a run paused before it was told apart replays without a mismatch.
|
|
2235
|
+
const outside = !stepFailed;
|
|
2236
|
+
await step.emit(
|
|
2237
|
+
RunPaused,
|
|
2238
|
+
{ run, reason, choices: outside ? ["retry", "cancel"] : ["retry", "skip", "cancel"] },
|
|
2239
|
+
{ key: `paused:${digestOf(reason)}${outside ? ":script" : ""}` },
|
|
2240
|
+
);
|
|
2241
|
+
// A script error that no step caused is a failure of the handler, and the core pauses an
|
|
2242
|
+
// invocation whose handler fails: the pause is a request (the run offers retry and cancel), and
|
|
2243
|
+
// the run waits on it. Without it the invocation would end as done, with the run said to be
|
|
2244
|
+
// paused and nobody to answer. A step that did not complete has paused the invocation already.
|
|
2245
|
+
if (!stepFailed) throw new Error(reason);
|
|
2246
|
+
});
|
|
2247
|
+
});
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
/**
|
|
2251
|
+
* Why a run waits when its script did not catch the end of an \`ask\`: by the name of the error the script ended with.
|
|
2252
|
+
*
|
|
2253
|
+
* A replay throws the same error, so the run offers only cancel; the script is revised to catch it.
|
|
2254
|
+
*/
|
|
2255
|
+
const ASK_ENDINGS: ReadonlyMap<string, string> = new Map([
|
|
2256
|
+
[AskCancelled.name, "the owner cancelled the question, and the script does not catch AskCancelled: a retry would get the same cancel, so revise the script or cancel the run"],
|
|
2257
|
+
[AskExpired.name, "nobody answered the question in time, and the script does not catch AskExpired: a retry would get the same end, so revise the script or cancel the run"],
|
|
2258
|
+
]);
|
|
2259
|
+
|
|
2260
|
+
/**
|
|
2261
|
+
* A run cannot do what was asked of it, and the caller is told why.
|
|
2262
|
+
*
|
|
2263
|
+
* The code says what was refused, so a caller can tell one refusal from another.
|
|
2264
|
+
*/
|
|
2265
|
+
export class RunRefusal extends Error {
|
|
2266
|
+
override name = "RunRefusal";
|
|
2267
|
+
/** The code of the refusal, so a caller can tell one from another. */
|
|
2268
|
+
readonly code: string;
|
|
2269
|
+
|
|
2270
|
+
constructor(code: string, message: string) {
|
|
2271
|
+
super(message);
|
|
2272
|
+
this.code = code;
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
/**
|
|
2277
|
+
* The rules `uses` asks for with these arguments: every entry as it is written, a target template
|
|
2278
|
+
* filled from the arguments. A value that would widen a rule, or a placeholder with no value,
|
|
2279
|
+
* refuses the run.
|
|
2280
|
+
*/
|
|
2281
|
+
export function filledUses(uses: readonly string[], args: unknown): string[] {
|
|
2282
|
+
return uses.filter(isUse).map((use) => {
|
|
2283
|
+
if (!isRuleTemplate(use)) return use;
|
|
2284
|
+
try {
|
|
2285
|
+
return fillRule(use, args);
|
|
2286
|
+
} catch (error) {
|
|
2287
|
+
throw new RunRefusal("uses-template", `uses "${use}" cannot be filled from the input: ${(error as Error).message}`);
|
|
2288
|
+
}
|
|
2289
|
+
});
|
|
2290
|
+
}
|
|
2291
|
+
|
|
2292
|
+
/** A run asked for more than whoever starts it holds: nothing is created, and the masks say what was refused. */
|
|
2293
|
+
export class UsesRefused extends RunRefusal {
|
|
2294
|
+
override name = "UsesRefused";
|
|
2295
|
+
/** The masks that are not the caller's. */
|
|
2296
|
+
readonly refused: readonly string[];
|
|
2297
|
+
|
|
2298
|
+
constructor(refused: readonly string[], who = "a script") {
|
|
2299
|
+
super(
|
|
2300
|
+
"wider-than-caller",
|
|
2301
|
+
`${who} cannot have more than whoever starts it: ${refused.join(", ")} ${refused.length === 1 ? "is" : "are"} not held`,
|
|
2302
|
+
);
|
|
2303
|
+
this.refused = refused;
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
/** A script did not pass its check: `workflow.start` does not run it, and the errors say why. */
|
|
2308
|
+
export class ScriptCheckFailed extends RunRefusal {
|
|
2309
|
+
override name = "ScriptCheckFailed";
|
|
2310
|
+
/** The errors of the check, with their places. */
|
|
2311
|
+
readonly errors: readonly CheckError[];
|
|
2312
|
+
|
|
2313
|
+
constructor(errors: readonly CheckError[]) {
|
|
2314
|
+
super(
|
|
2315
|
+
"script-check-failed",
|
|
2316
|
+
`the script does not pass its check (the script was not run):\n${errors.map((e) => `${e.file}:${String(e.line)}:${String(e.column)} ${e.code} ${e.message}`).join("\n")}`,
|
|
2317
|
+
);
|
|
2318
|
+
this.errors = errors;
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
/** The arguments of a start do not pass the input of the script: nothing is created, and the issues say which fields. */
|
|
2323
|
+
export class InputInvalid extends RunRefusal {
|
|
2324
|
+
override name = "InputInvalid";
|
|
2325
|
+
/** The fields that did not pass, with the path of each. */
|
|
2326
|
+
readonly issues: readonly Issue[];
|
|
2327
|
+
|
|
2328
|
+
constructor(issues: readonly Issue[]) {
|
|
2329
|
+
super(
|
|
2330
|
+
"input-invalid",
|
|
2331
|
+
`the arguments do not pass the input of the script:\n${issues.map((issue) => `${issue.path === "" ? "(input)" : issue.path}: ${issue.message}`).join("\n")}`,
|
|
2332
|
+
);
|
|
2333
|
+
this.issues = issues;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
/** A JSON Schema as data, as a zod schema again. */
|
|
2338
|
+
function schemaOf(schema: JsonSchemaValue): z.ZodType {
|
|
2339
|
+
return z.fromJSONSchema(schema as Parameters<typeof z.fromJSONSchema>[0]);
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
/** What a value does not hold of a JSON Schema as data: the fields that did not pass, none when it passes. */
|
|
2343
|
+
function issuesOf(schema: JsonSchemaValue, value: unknown): Issue[] {
|
|
2344
|
+
const parsed = schemaOf(schema).safeParse(value);
|
|
2345
|
+
return parsed.success ? [] : parsed.error.issues.map((issue) => ({ path: issue.path.join("."), message: issue.message }));
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
/**
|
|
2349
|
+
* What the arguments of a run do not hold of the input of its script.
|
|
2350
|
+
*
|
|
2351
|
+
* A script with no `input` takes none; a script that starts on records takes the record, which the
|
|
2352
|
+
* core already holds to the schema of its type; any other takes the fields of its `input`, and an
|
|
2353
|
+
* absent argument is an empty object, so a script whose fields all have defaults needs none.
|
|
2354
|
+
*/
|
|
2355
|
+
function inputIssues(input: JsonSchemaValue | undefined, startsOnRecords: boolean, args: unknown): Issue[] {
|
|
2356
|
+
if (startsOnRecords) return [];
|
|
2357
|
+
if (input === undefined) return args === null || args === undefined ? [] : [{ path: "", message: "this script takes no input" }];
|
|
2358
|
+
return issuesOf(input, args ?? {});
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
/** How a run ends with what its script returned: held to the `output` of the script, which is nothing when it declares none. */
|
|
2362
|
+
function outcomeOf(output: Described["output"], returned: unknown): RunOutcome {
|
|
2363
|
+
const result = z.json().parse(returned ?? null);
|
|
2364
|
+
const issues = output === undefined ? (result === null ? [] : [{ path: "", message: "the script declares no output, and returned a value" }]) : issuesOf(output, result);
|
|
2365
|
+
if (issues.length === 0) return { status: "done", result };
|
|
2366
|
+
const message = `the result does not pass the output of the script: ${issues.map((issue) => `${issue.path === "" ? "(result)" : issue.path}: ${issue.message}`).join("; ")}`;
|
|
2367
|
+
return { status: "failed", error: { code: "output-invalid", message, issues } };
|
|
2368
|
+
}
|
|
2369
|
+
|
|
2370
|
+
function resolveFile(root: string | undefined, file: string): string {
|
|
2371
|
+
return root === undefined || file.startsWith("/") ? file : `${root.replace(/\/$/, "")}/${file}`;
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
/** The run as its start records hold it; the latest start gives the file and the arguments. */
|
|
2375
|
+
function factsOf(journal: JournalReader, run: string): RunFacts | undefined {
|
|
2376
|
+
const starts = journal.find({ type: RunStarted.name, field: "run", equals: run });
|
|
2377
|
+
const last = starts.at(-1);
|
|
2378
|
+
if (last === undefined) return undefined;
|
|
2379
|
+
const data = RunStarted.schema.parse(last.data);
|
|
2380
|
+
return {
|
|
2381
|
+
file: data.file,
|
|
2382
|
+
args: data.args ?? null,
|
|
2383
|
+
...(data.label === undefined ? {} : { label: data.label }),
|
|
2384
|
+
...(data.hash === undefined ? {} : { hash: data.hash }),
|
|
2385
|
+
...(data.contract === undefined ? {} : { contract: data.contract }),
|
|
2386
|
+
...(data.box === true ? { box: true } : {}),
|
|
2387
|
+
...originOf(data),
|
|
2388
|
+
invocations: starts.flatMap((start) => (start.actor.kind === "handler" ? [String(start.actor.invocation)] : [])),
|
|
2389
|
+
};
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
/** The run ids that were issued, each with the first record that asked for its run. */
|
|
2393
|
+
function runsIssuedOf(journal: JournalReader): IssuedId[] {
|
|
2394
|
+
const issued = new Map<string, IssuedId>();
|
|
2395
|
+
for (const row of journal.find({ type: RunRequested.name })) {
|
|
2396
|
+
const run = RunRequested.schema.parse(row.data).run;
|
|
2397
|
+
if (!issued.has(run)) issued.set(run, { id: run, seed: row.id });
|
|
2398
|
+
}
|
|
2399
|
+
return [...issued.values()];
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
/** Who started a run: the principal its grant came from. Absent when nothing limited whoever started it. */
|
|
2403
|
+
function ownerOfRun(journal: JournalReader, run: string): Principal | undefined {
|
|
2404
|
+
for (const row of journal.find({ type: CORE_TYPES.accessGranted, field: "principal.id", equals: run })) {
|
|
2405
|
+
const granted = AccessGranted.schema.parse(row.data);
|
|
2406
|
+
if (granted.principal.kind === "run") return granted.parent?.principal;
|
|
2407
|
+
}
|
|
2408
|
+
return undefined;
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
/**
|
|
2412
|
+
* A run, or an installation, is for whoever started it, and for the owner. A caller nothing limits
|
|
2413
|
+
* (the host, a handler) is the module's own business and passes.
|
|
2414
|
+
*/
|
|
2415
|
+
function mayAct(caller: Caller, started: Principal | undefined, what: string): void {
|
|
2416
|
+
if (!isOwnerOrStarter(caller, started)) throw new DeniedError(`only the owner or whoever made it may ${what}`);
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
function isOwnerOrStarter(caller: Caller, started: Principal | undefined): boolean {
|
|
2420
|
+
if (caller.kind !== "client") return true;
|
|
2421
|
+
const who = caller.principal;
|
|
2422
|
+
if (who?.kind === "owner") return true;
|
|
2423
|
+
return who !== undefined && who.kind === started?.kind && who.id === started.id;
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
/** The earlier of two moments, either of which may be absent. */
|
|
2427
|
+
function earliest(a: number | undefined, b: number | undefined): number | undefined {
|
|
2428
|
+
return a === undefined ? b : b === undefined ? a : Math.min(a, b);
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
/** The sessions the invocations of a run started, that have not ended. */
|
|
2432
|
+
function sessionsOf(journal: JournalReader, invocations: readonly string[]) {
|
|
2433
|
+
const ended = new Set(journal.find({ type: SessionEnded.name }).map((row) => SessionEnded.schema.parse(row.data).session));
|
|
2434
|
+
return journal
|
|
2435
|
+
.find({ type: SessionMarked.name })
|
|
2436
|
+
.filter((row) => row.actor.kind === "handler" && invocations.includes(String(row.actor.invocation)))
|
|
2437
|
+
.map((row) => SessionMarked.schema.parse(row.data).session)
|
|
2438
|
+
.filter((session) => !ended.has(session));
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
/** The runs a run started, by the parent they name. */
|
|
2442
|
+
function childrenOf(journal: JournalReader, run: string): RunId[] {
|
|
2443
|
+
return [
|
|
2444
|
+
...new Set(
|
|
2445
|
+
journal
|
|
2446
|
+
.find({ type: RunRequested.name })
|
|
2447
|
+
.map((row) => RunRequested.schema.parse(row.data))
|
|
2448
|
+
.filter((data) => data.parent?.run === run)
|
|
2449
|
+
.map((data) => data.run),
|
|
2450
|
+
),
|
|
2451
|
+
];
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
/** How deep a run is in its tree: 0 for a run no run started. */
|
|
2455
|
+
function depthOf(journal: JournalReader, run: string): number {
|
|
2456
|
+
let depth = 0;
|
|
2457
|
+
for (let at = factsOf(journal, run)?.parent?.run; at !== undefined && depth < 1_000; at = factsOf(journal, at)?.parent?.run) depth += 1;
|
|
2458
|
+
return depth;
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
/** The shielded scopes of a run that are open: opened and not closed. */
|
|
2462
|
+
function openShieldsOf(journal: JournalReader, run: string): string[] {
|
|
2463
|
+
const after = journal.find({ type: RunRequested.name, field: "run", equals: run }).at(-1)?.seq;
|
|
2464
|
+
const closed = new Set(journal.find({ type: ScopeClosed.name, field: "run", equals: run, after }).map((row) => ScopeClosed.schema.parse(row.data).scope));
|
|
2465
|
+
return [
|
|
2466
|
+
...new Set(
|
|
2467
|
+
journal
|
|
2468
|
+
.find({ type: ScopeOpened.name, field: "run", equals: run, after })
|
|
2469
|
+
.map((row) => ScopeOpened.schema.parse(row.data))
|
|
2470
|
+
.filter((opened) => opened.shield === true && !closed.has(opened.scope))
|
|
2471
|
+
.map((opened) => opened.scope),
|
|
2472
|
+
),
|
|
2473
|
+
];
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
/** Session events emitted by an action retain its host-generated operation in the record ID. */
|
|
2477
|
+
function sessionStep(row: StoredRecord, invocation: string): string | undefined {
|
|
2478
|
+
const id = String(row.id);
|
|
2479
|
+
const operation = id.slice(0, id.lastIndexOf("/emit/"));
|
|
2480
|
+
return operation.startsWith(invocation + "/") ? operation.slice(invocation.length + 1).replace(/#\d+$/, "") : undefined;
|
|
2481
|
+
}
|
|
2482
|
+
|
|
2483
|
+
/**
|
|
2484
|
+
* The sessions and the child runs that steps of an invocation under the prefixes started, and that
|
|
2485
|
+
* are still going: what a cancel of those steps interrupts and ends with them.
|
|
2486
|
+
*/
|
|
2487
|
+
function heldBy(journal: JournalReader, invocation: string, prefixes: readonly string[], except: readonly string[]): { sessions: Array<z.infer<typeof SessionMarked.schema>["session"]>; marks: string[]; children: RunId[] } {
|
|
2488
|
+
const held = (key: string): boolean => prefixes.some((prefix) => keyUnder(key, prefix)) && !except.some((spared) => keyUnder(key, spared));
|
|
2489
|
+
// An operation is `<invocation>/<key>`, with `#<n>` for a later attempt.
|
|
2490
|
+
const keyOf = (operation: string): string | undefined =>
|
|
2491
|
+
operation.startsWith(`${invocation}/`) ? operation.slice(invocation.length + 1).replace(/#\d+$/, "") : undefined;
|
|
2492
|
+
const ended = new Set(journal.find({ type: SessionEnded.name }).map((row) => SessionEnded.schema.parse(row.data).session));
|
|
2493
|
+
const underBranch = (row: StoredRecord): boolean => {
|
|
2494
|
+
const key = sessionStep(row, invocation);
|
|
2495
|
+
return key !== undefined && held(key);
|
|
2496
|
+
};
|
|
2497
|
+
const sessions = journal.find({ type: SessionMarked.name }).filter(underBranch)
|
|
2498
|
+
.map(row => SessionMarked.schema.parse(row.data).session).filter(session => !ended.has(session));
|
|
2499
|
+
const marks = journal.find({ type: SessionStartReserved.name }).filter(underBranch)
|
|
2500
|
+
.flatMap(row => SessionStartReserved.schema.parse(row.data).mark ?? []);
|
|
2501
|
+
const children = [
|
|
2502
|
+
...new Set(
|
|
2503
|
+
journal
|
|
2504
|
+
.find({ type: RunRequested.name })
|
|
2505
|
+
.map((row) => RunRequested.schema.parse(row.data))
|
|
2506
|
+
.filter((data) => {
|
|
2507
|
+
const key = data.parent === undefined ? undefined : keyOf(data.parent.step);
|
|
2508
|
+
return key !== undefined && held(key) && statusOf(journal, data.run).state !== "finished";
|
|
2509
|
+
})
|
|
2510
|
+
.map((data) => data.run),
|
|
2511
|
+
),
|
|
2512
|
+
];
|
|
2513
|
+
return { sessions: [...new Set(sessions)], marks, children };
|
|
2514
|
+
}
|
|
2515
|
+
|
|
2516
|
+
/**
|
|
2517
|
+
* The script file as it is now: its text, and the sha256 of its bytes.
|
|
2518
|
+
*
|
|
2519
|
+
* The hash is what tells a replay from a revision: the same bytes are a replay, other bytes are an
|
|
2520
|
+
* edited script. A file saved again with the same content is not an edit. Absent when the file
|
|
2521
|
+
* cannot be read.
|
|
2522
|
+
*/
|
|
2523
|
+
function readScript(file: string): { text: string; hash: string } | undefined {
|
|
2524
|
+
try {
|
|
2525
|
+
const bytes = readFileSync(file);
|
|
2526
|
+
return { text: bytes.toString("utf8"), hash: createHash("sha256").update(bytes).digest("hex") };
|
|
2527
|
+
} catch {
|
|
2528
|
+
return undefined;
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
|
|
2532
|
+
/**
|
|
2533
|
+
* The state of the latest invocation of a run.
|
|
2534
|
+
*
|
|
2535
|
+
* The state of an invocation is written by the core as it goes, and the latest one is what it is
|
|
2536
|
+
* now: a run whose invocation is running is still going, one that is paused waits for a
|
|
2537
|
+
* decision, and one that is done has ended. An invocation that never wrote a state is running:
|
|
2538
|
+
* it has just been admitted.
|
|
2539
|
+
*/
|
|
2540
|
+
function stateOfInvocation(journal: JournalReader, invocation: string): RunState {
|
|
2541
|
+
const rows = journal.find({ type: CORE_TYPES.invocationState, field: "invocation", equals: invocation });
|
|
2542
|
+
const last = rows.at(-1);
|
|
2543
|
+
if (last === undefined) return "running";
|
|
2544
|
+
const state = (last.data as { state?: unknown }).state;
|
|
2545
|
+
if (state === "waiting" || state === "paused" || state === "done") return state;
|
|
2546
|
+
return "running";
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
/**
|
|
2550
|
+
* The state of the latest invocation of a run, and the invocation itself.
|
|
2551
|
+
*
|
|
2552
|
+
* The latest start of a run is the one that is going, if any is: an earlier invocation of the
|
|
2553
|
+
* same run ended before it, or it would not be the latest.
|
|
2554
|
+
*/
|
|
2555
|
+
function latestInvocationOf(journal: JournalReader, run: string): { invocation: string; state: RunState } | undefined {
|
|
2556
|
+
const starts = journal.find({ type: RunStarted.name, field: "run", equals: run });
|
|
2557
|
+
const last = starts.at(-1);
|
|
2558
|
+
if (last === undefined) return undefined;
|
|
2559
|
+
if (last.actor.kind !== "handler") return undefined;
|
|
2560
|
+
const invocation = String(last.actor.invocation);
|
|
2561
|
+
return { invocation, state: stateOfInvocation(journal, invocation) };
|
|
2562
|
+
}
|
|
2563
|
+
|
|
2564
|
+
/**
|
|
2565
|
+
* A short digest of a text: what an emit key is made of when the text may hold anything.
|
|
2566
|
+
*
|
|
2567
|
+
* A key is part of a record id, so it may not hold whitespace or a separator. The digest is
|
|
2568
|
+
* short and holds neither.
|
|
2569
|
+
*/
|
|
2570
|
+
function digestOf(text: string): string {
|
|
2571
|
+
return createHash("sha256").update(text).digest("hex").slice(0, 16);
|
|
2572
|
+
}
|
|
2573
|
+
|
|
2574
|
+
/** The request that starts the run again, with the file and the arguments it was started with. */
|
|
2575
|
+
function requestedOf(
|
|
2576
|
+
run: RunId,
|
|
2577
|
+
facts: RunFacts,
|
|
2578
|
+
file: string,
|
|
2579
|
+
): { run: RunId; file: string; args: unknown; label?: string; contract?: ContractHash; box?: boolean } & RunOrigin {
|
|
2580
|
+
return {
|
|
2581
|
+
run,
|
|
2582
|
+
file,
|
|
2583
|
+
args: facts.args,
|
|
2584
|
+
...(facts.label === undefined ? {} : { label: facts.label }),
|
|
2585
|
+
...(facts.contract === undefined ? {} : { contract: facts.contract }),
|
|
2586
|
+
...(facts.box === true ? { box: true } : {}),
|
|
2587
|
+
...originOf(facts),
|
|
2588
|
+
};
|
|
2589
|
+
}
|
|
2590
|
+
|
|
2591
|
+
/** The origin a record or the facts of a run carry, the fields that are there. */
|
|
2592
|
+
function originOf(from: RunOrigin): RunOrigin {
|
|
2593
|
+
return {
|
|
2594
|
+
...(from.workflow === undefined ? {} : { workflow: from.workflow }),
|
|
2595
|
+
...(from.version === undefined ? {} : { version: from.version }),
|
|
2596
|
+
...(from.installation === undefined ? {} : { installation: from.installation }),
|
|
2597
|
+
...(from.directory === undefined ? {} : { directory: from.directory }),
|
|
2598
|
+
...(from.parent === undefined ? {} : { parent: from.parent }),
|
|
2599
|
+
...(from.concurrency === undefined ? {} : { concurrency: from.concurrency }),
|
|
2600
|
+
...(from.limits === undefined ? {} : { limits: from.limits }),
|
|
2601
|
+
};
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
/**
|
|
2605
|
+
* What the script as it is now means for the calls the run recorded.
|
|
2606
|
+
*
|
|
2607
|
+
* The script is read by running it in a disposable process with nothing reaching the outside, so
|
|
2608
|
+
* the calls it makes are known before any of them runs.
|
|
2609
|
+
*/
|
|
2610
|
+
async function revisionOf(
|
|
2611
|
+
journal: JournalReader,
|
|
2612
|
+
artifacts: ArtifactStore,
|
|
2613
|
+
facts: RunFacts,
|
|
2614
|
+
actionNameOf: (harness: string | undefined) => string,
|
|
2615
|
+
box: ScriptBox | undefined,
|
|
2616
|
+
file: string = facts.file,
|
|
2617
|
+
run: string = "unknown",
|
|
2618
|
+
from?: string,
|
|
2619
|
+
): Promise<RevisionReport & { unresolved?: string }> {
|
|
2620
|
+
const recorded = recordedCalls(journal, artifacts, facts.invocations);
|
|
2621
|
+
const plan = await planCalls(file, facts.args, recorded, actionNameOf, run, box, from);
|
|
2622
|
+
return { ...plan.report, ...(plan.error === undefined ? {} : { unresolved: plan.error }) };
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
/** What a step intent holds that a revision compares. */
|
|
2626
|
+
const RecordedIntent = z.object({
|
|
2627
|
+
key: z.string(),
|
|
2628
|
+
action: z.string(),
|
|
2629
|
+
input: JsonValueSchema.optional(),
|
|
2630
|
+
recovery: z.enum(["repeat", "repeat-with-key", "do-not-repeat"]).optional(),
|
|
2631
|
+
invocation: z.string(),
|
|
2632
|
+
});
|
|
2633
|
+
|
|
2634
|
+
/** What a step outcome holds that a revision reads: a value kept as an artifact is read back first. */
|
|
2635
|
+
const RecordedOutcome = z.object({
|
|
2636
|
+
key: z.string(),
|
|
2637
|
+
outcome: z.object({ status: z.string(), value: JsonValueSchema.optional() }),
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
/** The calls the given invocations made, by key: the latest call under a key, finished if any attempt answered. */
|
|
2641
|
+
function recordedCalls(journal: JournalReader, artifacts: ArtifactStore, invocations: string[]): Map<string, RecordedCall> {
|
|
2642
|
+
const calls = new Map<string, RecordedCall>();
|
|
2643
|
+
const wanted = new Set(invocations);
|
|
2644
|
+
const sources = (type: string): StoredRecord[] =>
|
|
2645
|
+
journal.find({ type: type as never }).filter((row) => wanted.has(String((row.data as { invocation?: unknown }).invocation)));
|
|
2646
|
+
for (const row of sources("niwa.step.intent")) {
|
|
2647
|
+
const data = RecordedIntent.parse(row.data);
|
|
2648
|
+
calls.set(data.key, {
|
|
2649
|
+
key: data.key,
|
|
2650
|
+
action: data.action,
|
|
2651
|
+
input: data.input ?? null,
|
|
2652
|
+
finished: false,
|
|
2653
|
+
...(data.recovery === undefined ? {} : { recovery: data.recovery }),
|
|
2654
|
+
invocation: data.invocation,
|
|
2655
|
+
});
|
|
2656
|
+
}
|
|
2657
|
+
for (const row of sources("niwa.step.outcome")) {
|
|
2658
|
+
const stored = row.data as { outcome: unknown };
|
|
2659
|
+
const data = RecordedOutcome.parse({ ...stored, outcome: storedOutcome(stored.outcome, artifacts) });
|
|
2660
|
+
const call = calls.get(data.key);
|
|
2661
|
+
if (call !== undefined && data.outcome.status === "completed") {
|
|
2662
|
+
call.finished = true;
|
|
2663
|
+
call.outcome = data.outcome.value;
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
return calls;
|
|
2667
|
+
}
|
|
2668
|
+
|
|
2669
|
+
/**
|
|
2670
|
+
* The calls the edited script makes, read by running it in a disposable process with nothing
|
|
2671
|
+
* reaching the outside. Each call of an action is decided as a run decides it (see revise.ts): one
|
|
2672
|
+
* that is taken gets its recorded answer, any other gets null, and what comes after it runs too.
|
|
2673
|
+
*/
|
|
2674
|
+
async function planCalls(
|
|
2675
|
+
file: string,
|
|
2676
|
+
args: unknown,
|
|
2677
|
+
recorded: Map<string, RecordedCall>,
|
|
2678
|
+
actionNameOf: (harness: string | undefined) => string,
|
|
2679
|
+
run: string,
|
|
2680
|
+
box: ScriptBox | undefined,
|
|
2681
|
+
from: string | undefined,
|
|
2682
|
+
): Promise<{ report: RevisionReport; error?: string }> {
|
|
2683
|
+
const report: RevisionReport = { reused: [], rerun: [] };
|
|
2684
|
+
const fresh = new Set<string>();
|
|
2685
|
+
let orders = 0;
|
|
2686
|
+
const ended = await runScript(
|
|
2687
|
+
{
|
|
2688
|
+
fresh: (key) => fresh.has(key),
|
|
2689
|
+
call: async ({ key, request, phase, after }) => {
|
|
2690
|
+
// Only the calls of actions are read: the other requests carry moments the script cannot
|
|
2691
|
+
// know before it runs, and they answer as if nothing had come.
|
|
2692
|
+
switch (request.op) {
|
|
2693
|
+
case "agent":
|
|
2694
|
+
case "call": {
|
|
2695
|
+
const call: CurrentCall =
|
|
2696
|
+
request.op === "agent"
|
|
2697
|
+
? { key, action: actionNameOf(request.input.harness), input: jsonOf(request.input), agent: request.input.prompt !== undefined }
|
|
2698
|
+
: { key, action: request.action, input: request.input ?? null };
|
|
2699
|
+
const decided = decide(recorded.get(key), call, {
|
|
2700
|
+
after: after === true,
|
|
2701
|
+
recompute: from !== undefined && (keyUnder(key, from) || phase === from),
|
|
2702
|
+
});
|
|
2703
|
+
if (decided.taken) {
|
|
2704
|
+
report.reused.push({ key, action: call.action, input: call.input, outcome: decided.outcome });
|
|
2705
|
+
return decided.outcome;
|
|
2706
|
+
}
|
|
2707
|
+
report.rerun.push({ key, action: call.action, input: call.input, reason: decided.reason });
|
|
2708
|
+
fresh.add(key);
|
|
2709
|
+
return null;
|
|
2710
|
+
}
|
|
2711
|
+
case "wait":
|
|
2712
|
+
return { timedOut: true };
|
|
2713
|
+
case "ask":
|
|
2714
|
+
return neutralValue(request.question);
|
|
2715
|
+
case "read-state":
|
|
2716
|
+
return {};
|
|
2717
|
+
case "read-records":
|
|
2718
|
+
return [];
|
|
2719
|
+
case "sleep":
|
|
2720
|
+
case "emit":
|
|
2721
|
+
return null;
|
|
2722
|
+
case "now":
|
|
2723
|
+
case "random":
|
|
2724
|
+
return 0;
|
|
2725
|
+
case "scope-open":
|
|
2726
|
+
return { known: [] };
|
|
2727
|
+
case "scope-settled":
|
|
2728
|
+
return { order: (orders += 1) - 1 };
|
|
2729
|
+
case "scope-cancel":
|
|
2730
|
+
case "scope-close":
|
|
2731
|
+
return null;
|
|
2732
|
+
case "scope-snapshot":
|
|
2733
|
+
return request.keys;
|
|
2734
|
+
case "scope-deadline":
|
|
2735
|
+
// The reading runs with no clock: a deadline never comes.
|
|
2736
|
+
return new Promise(() => undefined);
|
|
2737
|
+
}
|
|
2738
|
+
},
|
|
2739
|
+
phase: () => undefined,
|
|
2740
|
+
left: () => undefined,
|
|
2741
|
+
log: () => undefined,
|
|
2742
|
+
},
|
|
2743
|
+
file,
|
|
2744
|
+
args,
|
|
2745
|
+
`plan:${run}`,
|
|
2746
|
+
box,
|
|
2747
|
+
);
|
|
2748
|
+
return ended.ok ? { report } : { report, error: ended.error ?? "the script failed" };
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
export {
|
|
2752
|
+
Builtin,
|
|
2753
|
+
BuiltinPromoted,
|
|
2754
|
+
BuiltinTrial,
|
|
2755
|
+
BuiltinsWanted,
|
|
2756
|
+
InstallationId,
|
|
2757
|
+
RunFinished,
|
|
2758
|
+
RunId,
|
|
2759
|
+
RunLog,
|
|
2760
|
+
RunPaused,
|
|
2761
|
+
RunPauseRequested,
|
|
2762
|
+
RunHoldRequested,
|
|
2763
|
+
RunContinued,
|
|
2764
|
+
PhaseEntered,
|
|
2765
|
+
PhaseLeft,
|
|
2766
|
+
StepPhase,
|
|
2767
|
+
RunRequested,
|
|
2768
|
+
RunStarted,
|
|
2769
|
+
Subscription,
|
|
2770
|
+
SubscriptionInstalled,
|
|
2771
|
+
SubscriptionUninstalled,
|
|
2772
|
+
WORKFLOW_TYPES,
|
|
2773
|
+
WorkflowId,
|
|
2774
|
+
WorkflowRechecked,
|
|
2775
|
+
WorkflowRegistered,
|
|
2776
|
+
WorkflowVersion,
|
|
2777
|
+
} from "./records.ts";
|
|
2778
|
+
export { installationsOf, profileOf, standing, standingAt } from "./subscriptions.ts";
|
|
2779
|
+
export type { Installation } from "./subscriptions.ts";
|
|
2780
|
+
export { versionOf, workflowsOf } from "./registry.ts";
|
|
2781
|
+
export { InFlight, RunHold, RunStatus } from "./status.ts";
|
|
2782
|
+
export type { RevisionReport } from "./revise.ts";
|
|
2783
|
+
|
|
2784
|
+
export { durationMs } from "./language.ts";
|
|
2785
|
+
export type { ActionGroup, CallOptions, Duration, ScriptApi, TimedOut } from "@niwa-labs/script";
|
|
2786
|
+
export { sessionAgent } from "./agent.ts";
|
|
2787
|
+
export type { HarnessLaunch } from "./agent.ts";
|
|
2788
|
+
export {
|
|
2789
|
+
workflowBuiltinPromote,
|
|
2790
|
+
workflowBuiltinTrial,
|
|
2791
|
+
workflowCancel,
|
|
2792
|
+
workflowStop,
|
|
2793
|
+
workflowCheck,
|
|
2794
|
+
workflowContinue,
|
|
2795
|
+
workflowPause,
|
|
2796
|
+
workflowGet,
|
|
2797
|
+
workflowGuide,
|
|
2798
|
+
workflowInspect,
|
|
2799
|
+
workflowInstall,
|
|
2800
|
+
workflowInstallationGet,
|
|
2801
|
+
workflowInstallationList,
|
|
2802
|
+
workflowList,
|
|
2803
|
+
workflowRegister,
|
|
2804
|
+
workflowResume,
|
|
2805
|
+
workflowRevise,
|
|
2806
|
+
workflowRuns,
|
|
2807
|
+
workflowStart,
|
|
2808
|
+
workflowUninstall,
|
|
2809
|
+
workflowWait,
|
|
2810
|
+
} from "./actions.ts";
|
|
2811
|
+
export { WorkflowInspection, WorkflowStep } from "./inspect.ts";
|
|
2812
|
+
export { Measures, StatsFold, StatsInput, StatsOutput, VersionStats, workflowStats } from "./stats.ts";
|
|
2813
|
+
export type { CheckError, CheckReport } from "./check.ts";
|