@operad-io/engine 0.0.0-stage → 0.0.1

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.
Files changed (102) hide show
  1. package/LICENSE +21 -0
  2. package/config.schema.json +188 -0
  3. package/keys.schema.json +12 -0
  4. package/package.json +22 -4
  5. package/src/config/config.ts +627 -0
  6. package/src/config/index.ts +1 -0
  7. package/src/flow/define-flow.ts +57 -0
  8. package/src/flow/index.ts +5 -0
  9. package/src/flow/load.ts +41 -0
  10. package/src/flow/types.ts +107 -0
  11. package/src/flow/validate.ts +296 -0
  12. package/src/http/address.ts +142 -0
  13. package/src/http/index.ts +25 -0
  14. package/src/http/link.ts +20 -0
  15. package/src/http/server.ts +920 -0
  16. package/src/http/token.ts +85 -0
  17. package/src/index.ts +16 -0
  18. package/src/kernel/decide.ts +689 -0
  19. package/src/kernel/index.ts +3 -0
  20. package/src/kernel/state.ts +275 -0
  21. package/src/kernel/types.ts +366 -0
  22. package/src/log/errors.ts +77 -0
  23. package/src/log/facts.ts +50 -0
  24. package/src/log/index.ts +6 -0
  25. package/src/log/json-value.ts +67 -0
  26. package/src/log/open-log.ts +17 -0
  27. package/src/log/schema.ts +5 -0
  28. package/src/log/secret.ts +30 -0
  29. package/src/log/sqlite.ts +1011 -0
  30. package/src/log/types.ts +230 -0
  31. package/src/loop/code-watch.ts +486 -0
  32. package/src/loop/index.ts +3 -0
  33. package/src/loop/older-code.ts +58 -0
  34. package/src/loop/serve.ts +483 -0
  35. package/src/markdown.ts +451 -0
  36. package/src/notify/index.ts +10 -0
  37. package/src/notify/notify.ts +256 -0
  38. package/src/platform/file-lock.ts +385 -0
  39. package/src/platform/index.ts +13 -0
  40. package/src/platform/load.ts +18 -0
  41. package/src/platform/process-group.ts +635 -0
  42. package/src/platform/process-watch.ts +152 -0
  43. package/src/platform/sqlite-open.ts +104 -0
  44. package/src/platform/step-reap.ts +668 -0
  45. package/src/platform/watch.ts +158 -0
  46. package/src/platform/worker-reports.ts +34 -0
  47. package/src/plugin/action-schema.ts +550 -0
  48. package/src/plugin/declared-flows.ts +74 -0
  49. package/src/plugin/define-plugin.ts +12 -0
  50. package/src/plugin/errors.ts +73 -0
  51. package/src/plugin/filters.ts +46 -0
  52. package/src/plugin/harness.ts +50 -0
  53. package/src/plugin/home.ts +13 -0
  54. package/src/plugin/index.ts +65 -0
  55. package/src/plugin/install.ts +268 -0
  56. package/src/plugin/keys.ts +63 -0
  57. package/src/plugin/load.ts +256 -0
  58. package/src/plugin/logger.ts +30 -0
  59. package/src/plugin/object-facts.ts +132 -0
  60. package/src/plugin/open-store.ts +32 -0
  61. package/src/plugin/paging.ts +133 -0
  62. package/src/plugin/registry.ts +1783 -0
  63. package/src/plugin/secrets.ts +141 -0
  64. package/src/plugin/semver.ts +110 -0
  65. package/src/plugin/settings.ts +64 -0
  66. package/src/plugin/sqlite-store.ts +756 -0
  67. package/src/plugin/store.ts +318 -0
  68. package/src/plugin/types.ts +1236 -0
  69. package/src/plugin/usage.ts +157 -0
  70. package/src/plugin/validate.ts +1028 -0
  71. package/src/runner/answer.ts +156 -0
  72. package/src/runner/ask.ts +483 -0
  73. package/src/runner/cancel.ts +51 -0
  74. package/src/runner/errors.ts +20 -0
  75. package/src/runner/grants.ts +92 -0
  76. package/src/runner/index.ts +33 -0
  77. package/src/runner/limits.ts +403 -0
  78. package/src/runner/lock.ts +140 -0
  79. package/src/runner/runner.ts +1175 -0
  80. package/src/runner/service.ts +184 -0
  81. package/src/runner/step-worker.ts +303 -0
  82. package/src/testing/clock.ts +67 -0
  83. package/src/testing/index.ts +90 -0
  84. package/src/testing/types.ts +116 -0
  85. package/src/testing/world.ts +369 -0
  86. package/src/trigger/cron.ts +193 -0
  87. package/src/trigger/index.ts +32 -0
  88. package/src/trigger/kernel.ts +1212 -0
  89. package/src/trigger/rules.ts +300 -0
  90. package/src/trigger/store.ts +233 -0
  91. package/src/trigger/types.ts +156 -0
  92. package/src/util.ts +50 -0
  93. package/src/views/history.ts +634 -0
  94. package/src/views/index.ts +7 -0
  95. package/src/views/pending.ts +209 -0
  96. package/src/views/presentation.ts +2551 -0
  97. package/src/views/running.ts +255 -0
  98. package/src/views/settings.ts +452 -0
  99. package/src/views/types.ts +72 -0
  100. package/src/views/uploads.ts +228 -0
  101. package/triggers.schema.json +48 -0
  102. package/README.md +0 -3
@@ -0,0 +1,275 @@
1
+ import { END } from "../flow/types.ts";
2
+ import type { NewFact } from "../log/types.ts";
3
+ import type {
4
+ CrashLoopStoppedPayload,
5
+ RunStartedPayload,
6
+ EffectRequestedPayload,
7
+ KernelState,
8
+ PendingEffect,
9
+ PendingWait,
10
+ PersonAnsweredPayload,
11
+ RunEndedPayload,
12
+ StepEnteredPayload,
13
+ StepInterruptedPayload,
14
+ StepResultPayload,
15
+ WaitingForPersonPayload,
16
+ } from "./types.ts";
17
+
18
+ function read<T>(fact: NewFact): T {
19
+ return fact.payload as unknown as T;
20
+ }
21
+
22
+ /** The empty state, before the first fact. */
23
+ export function initialState(): KernelState {
24
+ return {
25
+ status: "idle",
26
+ params: null,
27
+ access: null,
28
+ hops: 0,
29
+ started: false,
30
+ step: null,
31
+ stepKind: null,
32
+ endedStatus: null,
33
+ wait: null,
34
+ effect: null,
35
+ effectStarted: null,
36
+ visits: {},
37
+ recoveries: {},
38
+ effects: {},
39
+ results: {},
40
+ answeredWaits: {},
41
+ answeredAsk: null,
42
+ lastSeq: 0,
43
+ };
44
+ }
45
+
46
+ /** Mark the outstanding effect as abandoned and forget it. */
47
+ function abandonEffect(
48
+ state: KernelState,
49
+ effectId: string | null,
50
+ ): Pick<KernelState, "effect" | "effects"> {
51
+ if (effectId === null) return { effect: state.effect, effects: state.effects };
52
+ if (state.effects[effectId] !== "outstanding") {
53
+ return {
54
+ effect: state.effect !== null && state.effect.id === effectId ? null : state.effect,
55
+ effects: state.effects,
56
+ };
57
+ }
58
+ return {
59
+ effect: state.effect !== null && state.effect.id === effectId ? null : state.effect,
60
+ effects: { ...state.effects, [effectId]: "abandoned" },
61
+ };
62
+ }
63
+
64
+ /** Apply one fact to one state. This is the only writer of kernel state. */
65
+ export function evolve(state: KernelState, fact: NewFact): KernelState {
66
+ const lastSeq = state.lastSeq + 1;
67
+
68
+ switch (fact.kind) {
69
+ case "run_started": {
70
+ if (state.started) return { ...state, lastSeq };
71
+ const payload = read<RunStartedPayload>(fact);
72
+ return {
73
+ ...state,
74
+ started: true,
75
+ status: "running",
76
+ params: payload.params ?? null,
77
+ access: payload.access ?? null,
78
+ hops: payload.hops ?? 0,
79
+ effectStarted: null,
80
+ lastSeq,
81
+ };
82
+ }
83
+
84
+ case "step_entered": {
85
+ const payload = read<StepEnteredPayload>(fact);
86
+ const abandoned = abandonEffect(state, state.effect === null ? null : state.effect.id);
87
+ return {
88
+ ...state,
89
+ ...abandoned,
90
+ status: "running",
91
+ step: payload.step,
92
+ stepKind: payload.kind,
93
+ wait: null,
94
+ effectStarted: null,
95
+ visits: { ...state.visits, [payload.step]: payload.visit },
96
+ lastSeq,
97
+ };
98
+ }
99
+
100
+ case "effect_requested": {
101
+ const payload = read<EffectRequestedPayload>(fact);
102
+ const effect: PendingEffect = {
103
+ id: payload.effectId,
104
+ kind: payload.effect,
105
+ step: payload.step,
106
+ use: payload.use,
107
+ params: payload.params,
108
+ check: payload.check,
109
+ due: payload.due,
110
+ replay: payload.replay,
111
+ ...(payload.grants === undefined ? {} : { grants: payload.grants }),
112
+ };
113
+ const recoveries = payload.recovery
114
+ ? {
115
+ ...state.recoveries,
116
+ [payload.effectId]: (state.recoveries[payload.effectId] ?? 0) + 1,
117
+ }
118
+ : state.recoveries;
119
+ return {
120
+ ...state,
121
+ status: payload.effect === "run_plugin_step" ? "running" : "waiting",
122
+ step: payload.step,
123
+ effect,
124
+ // The step that asked is running again: the answer is delivered.
125
+ answeredAsk:
126
+ state.answeredAsk?.step === payload.step ? null : state.answeredAsk,
127
+ effectStarted: null,
128
+ wait: null,
129
+ effects: { ...state.effects, [payload.effectId]: "outstanding" },
130
+ recoveries,
131
+ lastSeq,
132
+ };
133
+ }
134
+
135
+ case "step_started": {
136
+ const payload = read<{ readonly effectId: string }>(fact);
137
+ return { ...state, effectStarted: payload.effectId, lastSeq };
138
+ }
139
+
140
+ case "step_result": {
141
+ const payload = read<StepResultPayload>(fact);
142
+ if (state.effects[payload.effectId] !== "outstanding") {
143
+ return { ...state, lastSeq };
144
+ }
145
+ return {
146
+ ...state,
147
+ status: "running",
148
+ step: payload.step,
149
+ effect: state.effect !== null && state.effect.id === payload.effectId ? null : state.effect,
150
+ effectStarted:
151
+ state.effectStarted === payload.effectId ? null : state.effectStarted,
152
+ effects: { ...state.effects, [payload.effectId]: "done" },
153
+ results: {
154
+ ...state.results,
155
+ [payload.effectId]: { result: payload.result, data: payload.data },
156
+ },
157
+ lastSeq,
158
+ };
159
+ }
160
+
161
+ case "edge_taken": {
162
+ const payload = read<{ readonly target: string }>(fact);
163
+ const abandoned = abandonEffect(state, state.effect === null ? null : state.effect.id);
164
+ return {
165
+ ...state,
166
+ ...abandoned,
167
+ step: payload.target === END ? null : payload.target,
168
+ stepKind: null,
169
+ effectStarted: null,
170
+ lastSeq,
171
+ };
172
+ }
173
+
174
+ case "waiting_for_person": {
175
+ const payload = read<WaitingForPersonPayload>(fact);
176
+ const wait: PendingWait = {
177
+ id: payload.waitId,
178
+ step: payload.step,
179
+ prompt: payload.prompt,
180
+ choices: payload.choices,
181
+ kind: payload.waitKind,
182
+ effectId: payload.effectId,
183
+ ...(payload.ask === undefined ? {} : { ask: payload.ask }),
184
+ };
185
+ return {
186
+ ...state,
187
+ status: "waiting_person",
188
+ step: payload.step,
189
+ stepKind: payload.waitKind === "step" ? "wait_person" : state.stepKind,
190
+ wait,
191
+ lastSeq,
192
+ };
193
+ }
194
+
195
+ case "person_answered": {
196
+ const payload = read<PersonAnsweredPayload>(fact);
197
+ if (state.wait === null || state.wait.id !== payload.waitId) {
198
+ return { ...state, lastSeq };
199
+ }
200
+ const effectId = state.wait.effectId;
201
+ const recoveries =
202
+ payload.waitKind === "crash_loop" && effectId !== null
203
+ ? { ...state.recoveries, [effectId]: 0 }
204
+ : state.recoveries;
205
+ return {
206
+ ...state,
207
+ status: "running",
208
+ step: payload.step,
209
+ wait: null,
210
+ recoveries,
211
+ answeredWaits: { ...state.answeredWaits, [payload.waitId]: true },
212
+ answeredAsk:
213
+ payload.ask === undefined
214
+ ? state.answeredAsk
215
+ : {
216
+ waitId: payload.waitId,
217
+ step: payload.ask.step,
218
+ prompt: payload.ask.prompt,
219
+ resume: payload.ask.resume,
220
+ choice: payload.choice,
221
+ note: payload.note,
222
+ },
223
+ lastSeq,
224
+ };
225
+ }
226
+
227
+ case "run_ended": {
228
+ const payload = read<RunEndedPayload>(fact);
229
+ const abandoned = abandonEffect(state, state.effect === null ? null : state.effect.id);
230
+ return {
231
+ ...state,
232
+ ...abandoned,
233
+ status: "ended",
234
+ endedStatus: payload.status,
235
+ step: null,
236
+ stepKind: null,
237
+ wait: null,
238
+ effectStarted: null,
239
+ lastSeq,
240
+ };
241
+ }
242
+
243
+ case "step_interrupted": {
244
+ const payload = read<StepInterruptedPayload>(fact);
245
+ const abandoned = abandonEffect(state, payload.effectId);
246
+ return { ...state, ...abandoned, status: "running", effectStarted: null, lastSeq };
247
+ }
248
+
249
+ case "crash_loop_stopped": {
250
+ const payload = read<CrashLoopStoppedPayload>(fact);
251
+ return {
252
+ ...state,
253
+ recoveries: { ...state.recoveries, [payload.effectId]: payload.recoveries },
254
+ lastSeq,
255
+ };
256
+ }
257
+
258
+ case "visit_limit_reached": {
259
+ return { ...state, lastSeq };
260
+ }
261
+
262
+ default: {
263
+ return { ...state, lastSeq };
264
+ }
265
+ }
266
+ }
267
+
268
+ /** Rebuild the state from a log, which is the only source of truth. */
269
+ export function replay(facts: readonly NewFact[]): KernelState {
270
+ let state = initialState();
271
+ for (const fact of facts) {
272
+ state = evolve(state, fact);
273
+ }
274
+ return state;
275
+ }
@@ -0,0 +1,366 @@
1
+ import type { EndStatus, Replay, StepKind } from "../flow/types.ts";
2
+ import type { FactCause, JsonValue, NewFact } from "../log/types.ts";
3
+ import type { Actor } from "../plugin/types.ts";
4
+
5
+ /** The kinds of fact the kernel writes. */
6
+ export type FactKind =
7
+ | "run_started"
8
+ | "step_entered"
9
+ | "effect_requested"
10
+ | "step_started"
11
+ | "step_queued"
12
+ | "step_slot"
13
+ | "step_released"
14
+ | "process_replaced"
15
+ | "step_result"
16
+ | "edge_taken"
17
+ | "waiting_for_person"
18
+ | "person_answered"
19
+ | "ask_opened"
20
+ | "ask_answered"
21
+ | "ask_timed_out"
22
+ | "ask_refused"
23
+ | "grant_used"
24
+ | "run_ended"
25
+ | "step_interrupted"
26
+ | "crash_loop_stopped"
27
+ | "visit_limit_reached"
28
+ | "trigger_received"
29
+ | "trigger_skipped";
30
+
31
+ /** One fact produced by `decide`; the log adds the run id, seq and time. */
32
+ export interface KernelFact extends NewFact {
33
+ readonly kind: FactKind;
34
+ }
35
+
36
+ /** The descriptions a caller turns into work; the kernel performs no work. */
37
+ export type EffectKind = "run_plugin_step" | "notify_pending" | "schedule_wake" | "poll_check";
38
+
39
+ /** Effects that stay outstanding until a result or a wake. */
40
+ export type PendingEffectKind = Exclude<EffectKind, "notify_pending">;
41
+
42
+ /** Ask the plugin to run one of its step types. */
43
+ export interface RunPluginStepEffect {
44
+ readonly kind: "run_plugin_step";
45
+ readonly effectId: string;
46
+ readonly step: string;
47
+ readonly use: string;
48
+ readonly params: JsonValue;
49
+ readonly replay: Replay;
50
+ }
51
+
52
+ /** Tell the user a person is needed. */
53
+ export interface NotifyPendingEffect {
54
+ readonly kind: "notify_pending";
55
+ readonly waitId: string;
56
+ readonly step: string;
57
+ readonly prompt: string;
58
+ readonly choices: readonly string[];
59
+ readonly waitKind: WaitKind;
60
+ }
61
+
62
+ /** Wake the run again at a time. */
63
+ export interface ScheduleWakeEffect {
64
+ readonly kind: "schedule_wake";
65
+ readonly effectId: string;
66
+ readonly step: string;
67
+ readonly due: number;
68
+ }
69
+
70
+ /** Ask a plugin to check its condition again. */
71
+ export interface PollCheckEffect {
72
+ readonly kind: "poll_check";
73
+ readonly effectId: string;
74
+ readonly step: string;
75
+ readonly check: string;
76
+ /** The wait's own params for the check; `null` when it gave none. */
77
+ readonly params: JsonValue;
78
+ }
79
+
80
+ export type Effect =
81
+ | RunPluginStepEffect
82
+ | NotifyPendingEffect
83
+ | ScheduleWakeEffect
84
+ | PollCheckEffect;
85
+
86
+ /** Inputs the caller may give `decide`. */
87
+ export type Input =
88
+ | StartInput
89
+ | StepResultInput
90
+ | PersonAnswerInput
91
+ | TickInput
92
+ | RecoverInput
93
+ | CancelInput;
94
+
95
+ export interface InputBase {
96
+ /** The id the log gave this input. Facts caused by it point here. */
97
+ readonly inputId?: string;
98
+ }
99
+
100
+ export interface StartInput extends InputBase {
101
+ readonly kind: "start";
102
+ /** Fixed JSON the flow was started with, read by its steps. */
103
+ readonly params?: JsonValue | undefined;
104
+ /** The access profile the run starts with, opaque to the kernel. */
105
+ readonly access?: JsonValue | undefined;
106
+ /** Hops of the event that started the run; 0 when no event started it. */
107
+ readonly hops?: number;
108
+ /** Where the run came from, in words the history view can print. */
109
+ readonly trigger?: StartTrigger;
110
+ }
111
+
112
+ /** The event that started a run, without any of its text. */
113
+ export interface StartTrigger {
114
+ readonly source: string;
115
+ readonly type: string;
116
+ readonly key?: string;
117
+ }
118
+
119
+ export interface StepResultInput extends InputBase {
120
+ readonly kind: "step_result";
121
+ readonly effectId: string;
122
+ readonly result: string;
123
+ readonly data?: JsonValue;
124
+ }
125
+
126
+ export interface PersonAnswerInput extends InputBase {
127
+ readonly kind: "person_answer";
128
+ readonly waitId: string;
129
+ readonly choice: string;
130
+ readonly note?: string;
131
+ /** Who answered: a person through a client. */
132
+ readonly actor: Actor;
133
+ }
134
+
135
+ export interface TickInput extends InputBase {
136
+ readonly kind: "tick";
137
+ }
138
+
139
+ export interface RecoverInput extends InputBase {
140
+ readonly kind: "recover";
141
+ }
142
+
143
+ export interface CancelInput extends InputBase {
144
+ readonly kind: "cancel";
145
+ }
146
+
147
+ /** Who is waiting: a flow step, or the crash-loop decision. */
148
+ export type WaitKind = "step" | "crash_loop";
149
+
150
+ export type RunStatus = "idle" | "running" | "waiting" | "waiting_person" | "ended";
151
+
152
+ export type EffectStatus = "outstanding" | "done" | "abandoned";
153
+
154
+ /**
155
+ * A one-time permission an answer to a question gives: the exact command or
156
+ * path the question was about. It lives in the effect that continues the step,
157
+ * never in the run's own access profile.
158
+ */
159
+ export interface Grant {
160
+ readonly waitId: string;
161
+ readonly kind: "command" | "path";
162
+ readonly value: string;
163
+ /** The tool the person approved; a path grant covers that tool only. */
164
+ readonly tool?: string;
165
+ }
166
+
167
+ /** A question that was answered, waiting for the step that asked to run again. */
168
+ export interface AnsweredAskState {
169
+ readonly waitId: string;
170
+ readonly step: string;
171
+ readonly prompt: string;
172
+ readonly resume: JsonValue;
173
+ readonly choice: string;
174
+ readonly note: string | null;
175
+ }
176
+
177
+ /** What a flow wait took over from a step that asked and ran out of grace. */
178
+ export interface AskedWait {
179
+ /** The step that asked; the answer sends the run back to it. */
180
+ readonly step: string;
181
+ /** What the step left to carry on with: a session and the call asked about. */
182
+ readonly resume: JsonValue;
183
+ }
184
+
185
+ export interface PendingEffect {
186
+ readonly id: string;
187
+ readonly kind: PendingEffectKind;
188
+ readonly step: string;
189
+ readonly use: string | null;
190
+ readonly params: JsonValue;
191
+ readonly check: string | null;
192
+ readonly due: number | null;
193
+ readonly replay: Replay;
194
+ /** Permissions for this run of the step only, from an answered question. */
195
+ readonly grants?: readonly Grant[] | undefined;
196
+ }
197
+
198
+ export interface PendingWait {
199
+ readonly id: string;
200
+ readonly step: string;
201
+ readonly prompt: string;
202
+ readonly choices: readonly string[];
203
+ readonly kind: WaitKind;
204
+ readonly effectId: string | null;
205
+ readonly ask?: AskedWait | undefined;
206
+ }
207
+
208
+ export interface StepResultValue {
209
+ readonly result: string;
210
+ readonly data: JsonValue;
211
+ }
212
+
213
+ /** Everything the kernel knows, derived from the fact log alone. */
214
+ export interface KernelState {
215
+ readonly status: RunStatus;
216
+ /** Fixed JSON the flow was started with, or null. */
217
+ readonly params: JsonValue;
218
+ /** The access profile recorded at the start; null when there is none. */
219
+ readonly access: JsonValue;
220
+ /** Hops of the event that started the run; 0 when no event started it. */
221
+ readonly hops: number;
222
+ readonly started: boolean;
223
+ /** The one step a run is in, or null. */
224
+ readonly step: string | null;
225
+ readonly stepKind: StepKind | null;
226
+ readonly endedStatus: EndStatus | null;
227
+ readonly wait: PendingWait | null;
228
+ readonly effect: PendingEffect | null;
229
+ /** The effect a runner has started; null when nothing has run yet. */
230
+ readonly effectStarted: string | null;
231
+ readonly visits: Readonly<Record<string, number>>;
232
+ readonly recoveries: Readonly<Record<string, number>>;
233
+ readonly effects: Readonly<Record<string, EffectStatus>>;
234
+ readonly results: Readonly<Record<string, StepResultValue>>;
235
+ readonly answeredWaits: Readonly<Record<string, true>>;
236
+ /** The answer to a question, kept until the step that asked runs again. */
237
+ readonly answeredAsk: AnsweredAskState | null;
238
+ readonly lastSeq: number;
239
+ }
240
+
241
+ export interface Decision {
242
+ readonly facts: readonly KernelFact[];
243
+ readonly effects: readonly Effect[];
244
+ }
245
+
246
+ /* Fact payloads, exported so callers and tests can name what they read. */
247
+
248
+ export interface RunStartedPayload {
249
+ readonly flow: string;
250
+ readonly params?: JsonValue | undefined;
251
+ readonly access?: JsonValue | undefined;
252
+ readonly hops?: number;
253
+ readonly trigger?: StartTrigger;
254
+ }
255
+
256
+ export interface TriggerReceivedPayload {
257
+ readonly source: string;
258
+ readonly type: string;
259
+ readonly key: string;
260
+ readonly summary?: string;
261
+ readonly link?: string;
262
+ readonly hops: number;
263
+ }
264
+
265
+ export interface TriggerSkippedPayload {
266
+ readonly source: string;
267
+ readonly type: string;
268
+ readonly key: string;
269
+ readonly hops: number;
270
+ readonly reason: string;
271
+ }
272
+
273
+ export interface StepEnteredPayload {
274
+ readonly step: string;
275
+ readonly visit: number;
276
+ readonly kind: StepKind;
277
+ }
278
+
279
+ /** A runner began executing an effect; `pid` is the process that did. */
280
+ export interface StepStartedPayload {
281
+ readonly step: string;
282
+ readonly effectId: string;
283
+ readonly pid: number;
284
+ }
285
+
286
+ export interface EffectRequestedPayload {
287
+ readonly effectId: string;
288
+ readonly effect: PendingEffectKind;
289
+ readonly step: string;
290
+ readonly replay: Replay;
291
+ readonly recovery: boolean;
292
+ readonly use: string | null;
293
+ readonly params: JsonValue;
294
+ readonly check: string | null;
295
+ readonly due: number | null;
296
+ readonly grants?: readonly Grant[];
297
+ }
298
+
299
+ export interface StepResultPayload {
300
+ readonly effectId: string;
301
+ readonly step: string;
302
+ readonly result: string;
303
+ readonly data: JsonValue;
304
+ }
305
+
306
+ export interface EdgeTakenPayload {
307
+ readonly step: string;
308
+ readonly result: string;
309
+ readonly target: string;
310
+ }
311
+
312
+ export interface WaitingForPersonPayload {
313
+ readonly waitId: string;
314
+ readonly step: string;
315
+ readonly prompt: string;
316
+ readonly choices: string[];
317
+ readonly waitKind: WaitKind;
318
+ readonly effectId: string | null;
319
+ readonly ask?: AskedWait;
320
+ }
321
+
322
+ export interface PersonAnsweredPayload {
323
+ readonly waitId: string;
324
+ readonly step: string;
325
+ readonly choice: string;
326
+ readonly note: string | null;
327
+ readonly waitKind: WaitKind;
328
+ readonly actor: Actor | null;
329
+ /** Set when the wait took over a question a step asked. */
330
+ readonly ask?: {
331
+ readonly step: string;
332
+ readonly prompt: string;
333
+ readonly resume: JsonValue;
334
+ };
335
+ }
336
+
337
+ export interface RunEndedPayload {
338
+ readonly status: EndStatus;
339
+ readonly reason: string | null;
340
+ }
341
+
342
+ export interface StepInterruptedPayload {
343
+ readonly step: string;
344
+ readonly effectId: string | null;
345
+ readonly reason: string;
346
+ readonly result: string | null;
347
+ /** What the step said when it stopped with a result the flow does not expect. */
348
+ readonly message?: string | undefined;
349
+ }
350
+
351
+ export interface CrashLoopStoppedPayload {
352
+ readonly step: string;
353
+ readonly effectId: string;
354
+ readonly recoveries: number;
355
+ }
356
+
357
+ export interface VisitLimitReachedPayload {
358
+ readonly step: string;
359
+ readonly visits: number;
360
+ readonly limit: number;
361
+ }
362
+
363
+ /** The cause a fact gets when an input produces it directly. */
364
+ export function inputCause(input: Input): FactCause {
365
+ return input.inputId ?? input.kind;
366
+ }
@@ -0,0 +1,77 @@
1
+ /** Errors a log backend reports to its callers. */
2
+
3
+ export class LogBackendError extends Error {
4
+ constructor(message: string) {
5
+ super(message);
6
+ this.name = new.target.name;
7
+ }
8
+ }
9
+
10
+ export class StoreClosedError extends LogBackendError {
11
+ constructor() {
12
+ super("the log store is closed");
13
+ }
14
+ }
15
+
16
+ export class ActiveRunError extends LogBackendError {
17
+ readonly flow: string;
18
+ readonly object: string;
19
+ readonly runId: string;
20
+
21
+ constructor(flow: string, object: string, runId: string) {
22
+ super(`flow "${flow}" already has an active run for object "${object}" (${runId})`);
23
+ this.flow = flow;
24
+ this.object = object;
25
+ this.runId = runId;
26
+ }
27
+ }
28
+
29
+ export class RunNotFoundError extends LogBackendError {
30
+ readonly runId: string;
31
+
32
+ constructor(runId: string) {
33
+ super(`run ${runId} does not exist`);
34
+ this.runId = runId;
35
+ }
36
+ }
37
+
38
+ export class RunEndedError extends LogBackendError {
39
+ readonly runId: string;
40
+
41
+ constructor(runId: string) {
42
+ super(`run ${runId} has already ended`);
43
+ this.runId = runId;
44
+ }
45
+ }
46
+
47
+ export class SeqConflictError extends LogBackendError {
48
+ readonly runId: string;
49
+ readonly expectedSeq: number;
50
+ readonly actualSeq: number;
51
+
52
+ constructor(runId: string, expectedSeq: number, actualSeq: number) {
53
+ super(`run ${runId} is at sequence ${actualSeq}, not ${expectedSeq}`);
54
+ this.runId = runId;
55
+ this.expectedSeq = expectedSeq;
56
+ this.actualSeq = actualSeq;
57
+ }
58
+ }
59
+
60
+ export class SchemaVersionError extends LogBackendError {}
61
+
62
+ export class SecretSerializationError extends LogBackendError {
63
+ constructor() {
64
+ super("a Secret cannot be serialised: store a reference to it, not the secret itself");
65
+ }
66
+ }
67
+
68
+ export class InvalidPayloadError extends LogBackendError {}
69
+
70
+ export class CorruptLogError extends LogBackendError {
71
+ readonly runId: string;
72
+
73
+ constructor(runId: string, detail: string) {
74
+ super(`run ${runId} has a corrupt log: ${detail}`);
75
+ this.runId = runId;
76
+ }
77
+ }