@oona/vlo 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +53 -2
- package/dist/agent.d.ts +107 -0
- package/dist/agent.js +324 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +1540 -0
- package/dist/define-DwM_Qhbw.js +74 -0
- package/dist/domain-Bs8TeDCv.js +338 -0
- package/dist/domain-CJN6n_JD.d.ts +860 -0
- package/dist/host.d.ts +844 -0
- package/dist/host.js +314 -0
- package/dist/index-CIIcdblQ.d.ts +824 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/roles-D25bG980.js +3869 -0
- package/dist/src-DVCYkPA7.js +734 -0
- package/dist/testing.d.ts +58 -0
- package/dist/testing.js +511 -0
- package/dist/types-BLQQMmkc.d.ts +746 -0
- package/dist/vlo_kernel-CtzHHfHA.js +276 -0
- package/package.json +93 -4
- package/wasm/vlo_kernel.d.ts +82 -0
- package/wasm/vlo_kernel.js +382 -0
- package/wasm/vlo_kernel_bg.wasm +0 -0
- package/wasm/vlo_kernel_bg.wasm.d.ts +15 -0
|
@@ -0,0 +1,860 @@
|
|
|
1
|
+
import { C as KernelState, D as RetryPolicy, E as Reduction$1, O as RunStatus, R as RunRef, S as Json, T as ReducerEvent, V as Transition, _ as Cause, b as EventInput, j as Usage, n as Journal, v as Command } from "./types-BLQQMmkc.js";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
/** Kernel build information; the host refuses a protocol number it does not speak. */
|
|
4
|
+
interface KernelVersion {
|
|
5
|
+
/** Kernel crate version. */
|
|
6
|
+
version: string;
|
|
7
|
+
/** Protocol number. */
|
|
8
|
+
protocol: number;
|
|
9
|
+
/** Feature flags the build supports. */
|
|
10
|
+
features: string[];
|
|
11
|
+
}
|
|
12
|
+
/** An error returned by the kernel; errors are values, never exceptions. */
|
|
13
|
+
interface KernelError {
|
|
14
|
+
/** One of the kernel's closed set of error codes. */
|
|
15
|
+
code: string;
|
|
16
|
+
/** Human-readable detail. */
|
|
17
|
+
message: string;
|
|
18
|
+
/** Sequence number the error concerns, when there is one. */
|
|
19
|
+
seq?: number;
|
|
20
|
+
/** Step id the error concerns, when there is one. */
|
|
21
|
+
step_id?: string;
|
|
22
|
+
}
|
|
23
|
+
/** Every kernel call returns either a value or a {@link KernelError}. */
|
|
24
|
+
type KernelResult<T> = {
|
|
25
|
+
ok: true;
|
|
26
|
+
value: T;
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
error: KernelError;
|
|
30
|
+
};
|
|
31
|
+
/** Input of `prepareEvent`. */
|
|
32
|
+
interface PrepareEventInput {
|
|
33
|
+
/** The run row. */
|
|
34
|
+
run: RunRef;
|
|
35
|
+
/** Kernel state at the current checkpoint; absent before `started`. */
|
|
36
|
+
kernel_state?: KernelState;
|
|
37
|
+
/**
|
|
38
|
+
* The current user-state checkpoint, byte-exact. Lifecycle transitions echo and hash
|
|
39
|
+
* it without calling the reducer, so pass it for any event that is not `started`.
|
|
40
|
+
*/
|
|
41
|
+
state?: string;
|
|
42
|
+
/** The event to apply. */
|
|
43
|
+
event: EventInput;
|
|
44
|
+
}
|
|
45
|
+
/** Output of `prepareEvent`: either call the reducer, or apply a lifecycle transition as is. */
|
|
46
|
+
type Prepared = {
|
|
47
|
+
/** The reducer must run. */
|
|
48
|
+
mode: "reduce";
|
|
49
|
+
/** The event in reducer form. */
|
|
50
|
+
reducer_event: ReducerEvent;
|
|
51
|
+
/** The workflow input, on `started`. */
|
|
52
|
+
init_input?: Json;
|
|
53
|
+
/** Kernel state to reduce against. */
|
|
54
|
+
kernel_state: KernelState;
|
|
55
|
+
} | {
|
|
56
|
+
/** No reducer call: the transition is complete. */
|
|
57
|
+
mode: "lifecycle";
|
|
58
|
+
/** The write-set to apply. */
|
|
59
|
+
transition: Transition;
|
|
60
|
+
};
|
|
61
|
+
/** Input of `reduceStep`: the reducer's output, to be validated and turned into a write-set. */
|
|
62
|
+
interface ReduceStepInput {
|
|
63
|
+
/** The run row. */
|
|
64
|
+
run: RunRef;
|
|
65
|
+
/** Kernel state the reducer ran against. */
|
|
66
|
+
kernel_state: KernelState;
|
|
67
|
+
/** The event that was reduced. */
|
|
68
|
+
event: EventInput;
|
|
69
|
+
/** What the reducer returned. */
|
|
70
|
+
reduction: Reduction$1;
|
|
71
|
+
}
|
|
72
|
+
/** Input of `planRetry`. */
|
|
73
|
+
interface PlanRetryInput {
|
|
74
|
+
/** The activity's retry policy. */
|
|
75
|
+
policy: RetryPolicy;
|
|
76
|
+
/** The attempt that just failed. */
|
|
77
|
+
attempt: number;
|
|
78
|
+
/** Why it failed. */
|
|
79
|
+
cause: Cause;
|
|
80
|
+
/** Journaled randomness for jitter, as a hex string, so a re-fold plans the same delay. */
|
|
81
|
+
random: string;
|
|
82
|
+
}
|
|
83
|
+
/** Output of `planRetry`. */
|
|
84
|
+
type RetryPlan = {
|
|
85
|
+
/** Retry after `delay_ms`. */
|
|
86
|
+
retry: true;
|
|
87
|
+
/** The next attempt number. */
|
|
88
|
+
next_attempt: number;
|
|
89
|
+
/** Delay before it, in milliseconds. */
|
|
90
|
+
delay_ms: number;
|
|
91
|
+
} | {
|
|
92
|
+
/** Give up. */
|
|
93
|
+
retry: false;
|
|
94
|
+
/** Never set. */
|
|
95
|
+
next_attempt?: undefined;
|
|
96
|
+
/** Never set. */
|
|
97
|
+
delay_ms?: undefined;
|
|
98
|
+
};
|
|
99
|
+
/** A state hash at a sequence number, as `refoldCheck` compares them. */
|
|
100
|
+
interface HashAtSeq {
|
|
101
|
+
/** Event sequence number. */
|
|
102
|
+
seq: number;
|
|
103
|
+
/** State hash after that event. */
|
|
104
|
+
hash: string;
|
|
105
|
+
}
|
|
106
|
+
/** Output of `refoldCheck`. */
|
|
107
|
+
type RefoldCheckOutput = {
|
|
108
|
+
/** Every hash matched. */
|
|
109
|
+
matches: true;
|
|
110
|
+
/** Never set. */
|
|
111
|
+
seq?: undefined;
|
|
112
|
+
/** Never set. */
|
|
113
|
+
expected?: undefined;
|
|
114
|
+
/** Never set. */
|
|
115
|
+
actual?: undefined;
|
|
116
|
+
} | {
|
|
117
|
+
/** A hash diverged. */
|
|
118
|
+
matches: false;
|
|
119
|
+
/** First divergent sequence number. */
|
|
120
|
+
seq: number;
|
|
121
|
+
/** The journaled hash. */
|
|
122
|
+
expected: string;
|
|
123
|
+
/** The re-folded hash. */
|
|
124
|
+
actual: string;
|
|
125
|
+
};
|
|
126
|
+
/** Output of `deriveIds`. */
|
|
127
|
+
interface DeriveIdsOutput {
|
|
128
|
+
/** Task id for the step attempt. */
|
|
129
|
+
task_id: string;
|
|
130
|
+
/** Idempotency key for the step attempt. */
|
|
131
|
+
idempotency_key: string;
|
|
132
|
+
/** Child run id for the step. */
|
|
133
|
+
child_run_id: string;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The kernel as the host sees it. Implemented by the wasm loader (`load.ts`) and by a
|
|
137
|
+
* test double. Every method is synchronous and pure.
|
|
138
|
+
*/
|
|
139
|
+
interface Kernel {
|
|
140
|
+
/** Kernel build, protocol number and feature flags; the host refuses a protocol mismatch. */
|
|
141
|
+
version(): KernelVersion;
|
|
142
|
+
/** Field and enum checks on an envelope, returning the normalised form. */
|
|
143
|
+
validateEvent(event: EventInput): KernelResult<{
|
|
144
|
+
normalized: EventInput;
|
|
145
|
+
}>;
|
|
146
|
+
/** Classifies an event: call the reducer, or apply a lifecycle transition without it. */
|
|
147
|
+
prepareEvent(input: PrepareEventInput): KernelResult<Prepared>;
|
|
148
|
+
/** Validates a reduction's commands and builds the write-set the journal applies. */
|
|
149
|
+
reduceStep(input: ReduceStepInput): KernelResult<Transition>;
|
|
150
|
+
/** Hash of kernel state plus user state, as stored on every checkpoint and event. */
|
|
151
|
+
stateHash(input: {
|
|
152
|
+
kernel_state: KernelState;
|
|
153
|
+
state: string;
|
|
154
|
+
}): KernelResult<{
|
|
155
|
+
hash: string;
|
|
156
|
+
}>;
|
|
157
|
+
/** Whether and when to retry an attempt, from the policy, cause and journaled randomness. */
|
|
158
|
+
planRetry(input: PlanRetryInput): KernelResult<RetryPlan>;
|
|
159
|
+
/** Compares a re-fold's hashes with the journal's and names the first divergent seq. */
|
|
160
|
+
refoldCheck(input: {
|
|
161
|
+
expected: HashAtSeq[];
|
|
162
|
+
actual: HashAtSeq[];
|
|
163
|
+
}): KernelResult<RefoldCheckOutput>;
|
|
164
|
+
/** Deterministic task id, idempotency key and child run id for a step attempt. */
|
|
165
|
+
deriveIds(input: {
|
|
166
|
+
run_id: string;
|
|
167
|
+
step_id: string;
|
|
168
|
+
attempt: number;
|
|
169
|
+
}): KernelResult<DeriveIdsOutput>;
|
|
170
|
+
}
|
|
171
|
+
/** Workflow-state reduction: the next state plus the commands it asks the engine to run. */
|
|
172
|
+
type Reduction<S> = readonly [S, readonly Command[]];
|
|
173
|
+
type EventOf<T extends ReducerEvent["type"]> = Extract<ReducerEvent, {
|
|
174
|
+
type: T;
|
|
175
|
+
}>;
|
|
176
|
+
/**
|
|
177
|
+
* One pure handler per {@link ReducerEvent} type. Each receives the current state and the
|
|
178
|
+
* event and returns a {@link Reduction}. A run receiving an event with no handler fails.
|
|
179
|
+
*/
|
|
180
|
+
interface Handlers<S> {
|
|
181
|
+
/** The first event of a run: `state` is what `init` built from the start input. */
|
|
182
|
+
started(state: S, event: EventOf<"started">): Reduction<S>;
|
|
183
|
+
/** A task reached a terminal status; `event.output` is set when it `succeeded`. */
|
|
184
|
+
task_done(state: S, event: EventOf<"task_done">): Reduction<S>;
|
|
185
|
+
/** A signal the run was waiting on was delivered. */
|
|
186
|
+
signal?(state: S, event: EventOf<"signal">): Reduction<S>;
|
|
187
|
+
/** A timer opened by `cmd.timer` fired. */
|
|
188
|
+
timer?(state: S, event: EventOf<"timer">): Reduction<S>;
|
|
189
|
+
/** A child run started by `cmd.spawn` closed. */
|
|
190
|
+
child_done?(state: S, event: EventOf<"child_done">): Reduction<S>;
|
|
191
|
+
}
|
|
192
|
+
/** What {@link defineWorkflow} takes: how to build the initial state and the handlers. */
|
|
193
|
+
interface WorkflowDef<S, I> {
|
|
194
|
+
/** Builds the initial state from the start input. Called once, just before `on.started`. */
|
|
195
|
+
init(input: I): S;
|
|
196
|
+
/** The event handlers. */
|
|
197
|
+
on: Handlers<S>;
|
|
198
|
+
}
|
|
199
|
+
/** The wire form of one reduction: state as byte-exact JSON, plus plain `Command` objects. */
|
|
200
|
+
interface ReductionWire {
|
|
201
|
+
/** The next state, as `JSON.stringify` produced it. */
|
|
202
|
+
state: string;
|
|
203
|
+
/** The commands the handler returned, copied. */
|
|
204
|
+
commands: Command[];
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A defined workflow: the value {@link defineWorkflow} returns and the host registers.
|
|
208
|
+
* `S` is the state type, `I` the start-input type.
|
|
209
|
+
*/
|
|
210
|
+
interface Workflow<S = unknown, I = unknown> {
|
|
211
|
+
/** Discriminator the config loader uses to find workflows among a module's exports. */
|
|
212
|
+
readonly kind: "workflow";
|
|
213
|
+
/** The name given to {@link defineWorkflow}; what a run is started by. */
|
|
214
|
+
readonly name: string;
|
|
215
|
+
/** Content hash of `name`; runs pin it as their component digest. */
|
|
216
|
+
readonly digest: string;
|
|
217
|
+
/** The definition, as given. */
|
|
218
|
+
readonly def: WorkflowDef<S, I>;
|
|
219
|
+
/**
|
|
220
|
+
* Drive one event. Pass `initInput` for the `started` event (state is built by `init`);
|
|
221
|
+
* otherwise `stateJson` is the checkpoint to fold onto. Throws when no handler is
|
|
222
|
+
* registered for the event type.
|
|
223
|
+
*/
|
|
224
|
+
reduce(stateJson: string | null, event: ReducerEvent, initInput?: Json): ReductionWire;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Fails the run. There is no `cmd.fail`: a run fails when its reducer throws, which keeps
|
|
228
|
+
* failure out of the command vocabulary and off the happy path.
|
|
229
|
+
*
|
|
230
|
+
* Host contract: the host catches a throw from `Workflow.reduce`, appends a `failed` event
|
|
231
|
+
* (`cause: "app_error"`, `error` = this message) and marks the run `failed`. The state
|
|
232
|
+
* checkpoint from before the event is kept, so the run can be rewound and forked.
|
|
233
|
+
*/
|
|
234
|
+
declare class WorkflowFailure extends Error {
|
|
235
|
+
/** Structured context for the failure, as given; `undefined` when none was. */
|
|
236
|
+
readonly details: Json | undefined;
|
|
237
|
+
/** @param details - Anything JSON that helps explain the failure, e.g. the step id and cause. */
|
|
238
|
+
constructor(message: string, details?: Json);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Defines a workflow: a named, pure reducer over the run's events.
|
|
242
|
+
*
|
|
243
|
+
* `def` is either a hand-written {@link WorkflowDef} or the output of {@link sequence}.
|
|
244
|
+
* Handlers must not read a clock, generate randomness or do I/O; time and randomness arrive
|
|
245
|
+
* on the event. State must be JSON-serialisable: it is stringified after every reduction.
|
|
246
|
+
* A handler that throws fails the run (see {@link WorkflowFailure}); so does an event
|
|
247
|
+
* whose handler is not registered, or a handler returning anything but `[state, Command[]]`.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { cmd, defineWorkflow } from "@oona/vlo";
|
|
252
|
+
* import { work } from "./activities.ts";
|
|
253
|
+
*
|
|
254
|
+
* export default defineWorkflow("uppercase", {
|
|
255
|
+
* init: (input: { item: string }) => ({ item: input.item }),
|
|
256
|
+
* on: {
|
|
257
|
+
* started: (s) => [s, [cmd.task(work, { item: s.item })]],
|
|
258
|
+
* task_done: (s, e) => [s, [cmd.complete(e.output ?? null)]],
|
|
259
|
+
* },
|
|
260
|
+
* });
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
declare function defineWorkflow<S, I>(name: string, def: WorkflowDef<S, I>): Workflow<S, I>;
|
|
264
|
+
/** The statuses a run never leaves: `completed`, `failed`, `cancelled`, `continued`. */
|
|
265
|
+
declare const TERMINAL_RUN_STATUSES: ReadonlySet<RunStatus>;
|
|
266
|
+
/** True when the run accepts no further events. */
|
|
267
|
+
declare function isTerminalRunStatus(status: RunStatus): boolean;
|
|
268
|
+
/** Authoring form of `Usage` (camelCase, all optional); the wire form is snake_case. */
|
|
269
|
+
interface UsageInput {
|
|
270
|
+
tokens?: number;
|
|
271
|
+
costMicroUsd?: number;
|
|
272
|
+
cpuMs?: number;
|
|
273
|
+
}
|
|
274
|
+
/** What a handler gets beside its input: identity of the attempt and the controls for it. */
|
|
275
|
+
interface TaskContext {
|
|
276
|
+
/** Task id, `${run_id}:${step_id}`; stable across attempts. */
|
|
277
|
+
taskId: string;
|
|
278
|
+
/** The run the task belongs to. */
|
|
279
|
+
runId: string;
|
|
280
|
+
/** Attempt number, starting at 1. `${taskId}:${attempt}` is the idempotency key. */
|
|
281
|
+
attempt: number;
|
|
282
|
+
/** Extends the lease. Rejects once the lease is gone, so a long task can stop. */
|
|
283
|
+
heartbeat(): Promise<void>;
|
|
284
|
+
/**
|
|
285
|
+
* Accumulates what this attempt spent. The total travels with the attempt's result, the
|
|
286
|
+
* kernel banks it, and every reducer event then carries the run's `usage_total`.
|
|
287
|
+
* Reported on a failed attempt too: tokens burned before a throw are still spent.
|
|
288
|
+
*/
|
|
289
|
+
reportUsage(usage: UsageInput): void;
|
|
290
|
+
/** Aborted when the lease is lost, the activity times out, or the worker shuts down. */
|
|
291
|
+
signal: AbortSignal;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* The implementation of one activity. `input` has already passed the activity's input
|
|
295
|
+
* schema; the return value is validated against its output schema by the host. A throw
|
|
296
|
+
* fails the attempt as retryable; aborting on `ctx.signal` is how a handler cooperates
|
|
297
|
+
* with a timeout, a lost lease or a shutdown.
|
|
298
|
+
*/
|
|
299
|
+
type ActivityHandler<I = Json, O = unknown> = (input: I, ctx: TaskContext) => O | Promise<O>;
|
|
300
|
+
/** Handler inputs are contravariant, so `never` accepts any concretely typed handler. */
|
|
301
|
+
type ActivityImpls = Record<string, ActivityHandler<never, unknown>>;
|
|
302
|
+
/** A defined worker: the value {@link defineWorker} returns and the host or config loader registers. */
|
|
303
|
+
interface Worker {
|
|
304
|
+
/** Discriminator the config loader uses to find workers among a module's exports. */
|
|
305
|
+
readonly kind: "worker";
|
|
306
|
+
/** Queues this worker serves. */
|
|
307
|
+
readonly queues: readonly string[];
|
|
308
|
+
/** Handlers by activity name. */
|
|
309
|
+
readonly handlers: ReadonlyMap<string, ActivityHandler<Json, unknown>>;
|
|
310
|
+
}
|
|
311
|
+
/** Options for {@link defineWorker}. */
|
|
312
|
+
interface WorkerOptions {
|
|
313
|
+
/** Queues the worker serves. Default `[DEFAULT_QUEUE]`. */
|
|
314
|
+
queues?: readonly string[];
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Defines a worker: a map from activity name to {@link ActivityHandler} plus the queues it
|
|
318
|
+
* serves. Passed to `createHost({ workers })` for in-process execution, `runPullWorker` for
|
|
319
|
+
* long-polling, or found by the config loader's `workers` globs.
|
|
320
|
+
*
|
|
321
|
+
* The worker holds no schemas; validation happens in the reducer (input) and on the host
|
|
322
|
+
* (output). Throws a `TypeError` when `impls` is empty.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* import { defineWorker } from "@oona/vlo";
|
|
327
|
+
*
|
|
328
|
+
* export default defineWorker(
|
|
329
|
+
* {
|
|
330
|
+
* "example.plan": (input: { n: number }) => ({
|
|
331
|
+
* items: Array.from({ length: input.n }, (_, i) => `item-${i}`),
|
|
332
|
+
* }),
|
|
333
|
+
* "example.work": async (input: { item: string }, ctx) => {
|
|
334
|
+
* await ctx.heartbeat();
|
|
335
|
+
* ctx.reportUsage({ tokens: 12 });
|
|
336
|
+
* return { upper: input.item.toUpperCase() };
|
|
337
|
+
* },
|
|
338
|
+
* },
|
|
339
|
+
* { queues: ["default"] },
|
|
340
|
+
* );
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
declare function defineWorker(impls: ActivityImpls, options?: WorkerOptions): Worker;
|
|
344
|
+
/**
|
|
345
|
+
* Wraps a handler so attempts after the first receive `shape(input, attempt)` instead of the
|
|
346
|
+
* journaled input. Use it when repeating the same ask would fail the same way — a model
|
|
347
|
+
* looping on a long prompt, say — and a smaller or different ask has a chance.
|
|
348
|
+
*
|
|
349
|
+
* The journaled task input is untouched; only this process's view of it changes, and
|
|
350
|
+
* `attempt` is journaled, so the same attempt always sees the same input. `shape` must be
|
|
351
|
+
* pure. The shaped input is not re-validated against the activity's input schema.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* defineWorker({
|
|
356
|
+
* "brand.synthesize": withRetryInput(synthesize, (input) => ({ ...input, compact: true })),
|
|
357
|
+
* });
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
declare function withRetryInput<I, O>(handler: ActivityHandler<I, O>, shape: (input: I, attempt: number) => I): ActivityHandler<I, O>;
|
|
361
|
+
/** The first worker in this process that serves both the queue and the activity. */
|
|
362
|
+
declare function findWorker(workers: readonly Worker[], queue: string, activity: string): Worker | undefined;
|
|
363
|
+
/**
|
|
364
|
+
* Content-addressed storage for anything too big to inline in the journal
|
|
365
|
+
* (payloads and checkpoints over 256 KB).
|
|
366
|
+
*/
|
|
367
|
+
interface BlobStore {
|
|
368
|
+
/** Stores the bytes and returns their sha256, lowercase hex. Idempotent. */
|
|
369
|
+
put(bytes: Uint8Array): Promise<string>;
|
|
370
|
+
/** The bytes stored under that digest. Rejects when there are none. */
|
|
371
|
+
get(sha256Hex: string): Promise<Uint8Array>;
|
|
372
|
+
}
|
|
373
|
+
/** The task as a worker receives it — the HTTP push body, byte for byte. */
|
|
374
|
+
interface PushedTask {
|
|
375
|
+
/** The task's id; the callback URL is built from it. */
|
|
376
|
+
task_id: string;
|
|
377
|
+
/** The run the task belongs to. */
|
|
378
|
+
run_id: string;
|
|
379
|
+
/** The workflow step that scheduled it. */
|
|
380
|
+
step_id: string;
|
|
381
|
+
/** Attempt number, starting at 1. Every report must carry it back. */
|
|
382
|
+
attempt: number;
|
|
383
|
+
/** Activity name and, when registered, the schema version the output is checked against. */
|
|
384
|
+
activity: {
|
|
385
|
+
name: string;
|
|
386
|
+
schema_ref?: string;
|
|
387
|
+
};
|
|
388
|
+
/** Stable per attempt: a worker that sees it twice is being re-pushed, not asked to run again. */
|
|
389
|
+
idempotency_key: string;
|
|
390
|
+
/** The activity input. */
|
|
391
|
+
input: Json;
|
|
392
|
+
/** Lease length in milliseconds; each heartbeat extends it by this much. */
|
|
393
|
+
lease_ms: number;
|
|
394
|
+
/** How often to heartbeat, in milliseconds: a third of the lease, at least 1 s. */
|
|
395
|
+
heartbeat_ms: number;
|
|
396
|
+
/** Wall-clock budget for this attempt. The worker aborts the handler at it. */
|
|
397
|
+
timeout_ms?: number;
|
|
398
|
+
/** Base URL for `${complete_url}/complete|fail|heartbeat`. Absent for in-process work. */
|
|
399
|
+
complete_url?: string;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* What a transport's `push` answers. `accepted` means the task is now the worker's: the
|
|
403
|
+
* dispatcher records it as running. `inlineOutput` is a result the worker returned at once.
|
|
404
|
+
*/
|
|
405
|
+
type PushResult = {
|
|
406
|
+
accepted: true;
|
|
407
|
+
inlineOutput?: Json;
|
|
408
|
+
} |
|
|
409
|
+
/** `fatal` means the worker rejected the task itself; retrying would not help. */
|
|
410
|
+
{
|
|
411
|
+
accepted: false;
|
|
412
|
+
reason: string;
|
|
413
|
+
fatal?: boolean;
|
|
414
|
+
};
|
|
415
|
+
/** Delivers a scheduled task to whatever runs it. The dispatcher picks one per queue. */
|
|
416
|
+
interface TaskTransport {
|
|
417
|
+
/** A label for warnings, e.g. `http-push` or `in-process`. */
|
|
418
|
+
readonly kind: string;
|
|
419
|
+
/** Hands the task over. Must not throw for a refused push; report it in the result. */
|
|
420
|
+
push(task: PushedTask): Promise<PushResult>;
|
|
421
|
+
}
|
|
422
|
+
/** The subset of `fetch` the push needs; injected by tests. */
|
|
423
|
+
type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
|
|
424
|
+
/** Where and how to push one queue's tasks. */
|
|
425
|
+
interface HttpPushOptions {
|
|
426
|
+
/** The worker's push endpoint, typically `<worker base>/v1/tasks/push`. */
|
|
427
|
+
url: string;
|
|
428
|
+
/** HMAC secret shared with the worker. Required: an empty secret is refused. */
|
|
429
|
+
secret: string;
|
|
430
|
+
/** Clock for the signed timestamp, epoch milliseconds. Default `Date.now`. */
|
|
431
|
+
now?: () => number;
|
|
432
|
+
/** `fetch` implementation. Default `globalThis.fetch`. */
|
|
433
|
+
fetch?: FetchLike;
|
|
434
|
+
/** Per-push request timeout in milliseconds. Default 5 s. */
|
|
435
|
+
timeoutMs?: number;
|
|
436
|
+
}
|
|
437
|
+
/** Pushes tasks to a remote worker over signed HTTP. Built by the dispatcher per configured queue. */
|
|
438
|
+
declare class HttpPushTransport implements TaskTransport {
|
|
439
|
+
/** Always `http-push`. */
|
|
440
|
+
readonly kind = "http-push";
|
|
441
|
+
private readonly url;
|
|
442
|
+
private readonly secret;
|
|
443
|
+
private readonly now;
|
|
444
|
+
private readonly fetchImpl;
|
|
445
|
+
private readonly timeoutMs;
|
|
446
|
+
/** Throws a plain `Error` when `secret` is empty: tasks are never pushed unsigned. */
|
|
447
|
+
constructor(options: HttpPushOptions);
|
|
448
|
+
/**
|
|
449
|
+
* POSTs the task with the four `vlo-*` headers. Never throws: a network error, a timeout
|
|
450
|
+
* or a 5xx is `accepted: false` without `fatal`; a non-retryable 4xx sets `fatal`.
|
|
451
|
+
*/
|
|
452
|
+
push(task: PushedTask): Promise<PushResult>;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* `loop` ticks on an interval and on every `kick()`. `manual` never does either: the
|
|
456
|
+
* process it runs in has no long-lived timer to hang a tick on, so something outside —
|
|
457
|
+
* a cron hitting `POST /v1/timers/tick` — decides when the outbox moves.
|
|
458
|
+
*
|
|
459
|
+
* `kick()` stays a no-op in `manual` mode on purpose. A kick is unattended and unbounded:
|
|
460
|
+
* inside a serverless request it would start work the platform is free to freeze the moment
|
|
461
|
+
* the response is sent, leaving tasks marked `running` with nobody behind them. `Host.drain`
|
|
462
|
+
* (or `POST /v1/timers/tick?drain=1`) is the explicit, awaited, time-boxed form of the same
|
|
463
|
+
* thing — the caller stays alive for it and knows when it stopped.
|
|
464
|
+
*/
|
|
465
|
+
type DispatcherMode = "loop" | "manual";
|
|
466
|
+
/** Tuning for the outbox dispatcher. Every default suits a single laptop host. */
|
|
467
|
+
interface DispatcherOptions {
|
|
468
|
+
/** `loop` (default) ticks on a timer and on `kick()`; `manual` only ticks when asked. */
|
|
469
|
+
mode?: DispatcherMode;
|
|
470
|
+
/** Interval between passes in `loop` mode. Default 500 ms. */
|
|
471
|
+
tickMs?: number;
|
|
472
|
+
/** Rows one stage claims per pass. Default 50. */
|
|
473
|
+
batch?: number;
|
|
474
|
+
/** How long a claim is held before another orchestrator may take the row. */
|
|
475
|
+
claimHoldMs?: number;
|
|
476
|
+
/** First backoff step after a refused push; doubles per dispatch attempt. */
|
|
477
|
+
backoffBaseMs?: number;
|
|
478
|
+
/** Ceiling on the push backoff. Default 60 s. */
|
|
479
|
+
maxBackoffMs?: number;
|
|
480
|
+
/** Backoff used when no transport serves the task's queue at all. */
|
|
481
|
+
unroutableBackoffMs?: number;
|
|
482
|
+
/** `fetch` used for HTTP pushes. Default `globalThis.fetch`; injected by tests. */
|
|
483
|
+
fetch?: FetchLike;
|
|
484
|
+
/** Recorded as `worker_id` on tasks this process dispatches. Default `vlo-<8 hex>`. */
|
|
485
|
+
workerId?: string;
|
|
486
|
+
}
|
|
487
|
+
/** What one tick moved, per stage. The tick route answers with exactly this. */
|
|
488
|
+
interface TickCounts {
|
|
489
|
+
/** `created` runs that received their `started` event. */
|
|
490
|
+
started: number;
|
|
491
|
+
/** Tasks a transport accepted and the journal recorded as running. */
|
|
492
|
+
pushed: number;
|
|
493
|
+
/** Timers that fired. */
|
|
494
|
+
timers: number;
|
|
495
|
+
/** Attempts failed for a lapsed lease. */
|
|
496
|
+
reaped: number;
|
|
497
|
+
/** Attempts the backstop failed for outliving `timeout_ms`. */
|
|
498
|
+
timeouts: number;
|
|
499
|
+
/** Closed child runs whose parent received its `child` event. */
|
|
500
|
+
notified: number;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* The outbox dispatcher. `createHost` builds one per host holding the `orchestrator` role;
|
|
504
|
+
* only tests and custom hosts construct it directly.
|
|
505
|
+
*/
|
|
506
|
+
declare class Dispatcher {
|
|
507
|
+
private readonly ctx;
|
|
508
|
+
private readonly options;
|
|
509
|
+
private readonly mode;
|
|
510
|
+
private readonly tickMs;
|
|
511
|
+
private readonly batch;
|
|
512
|
+
private readonly claimHoldMs;
|
|
513
|
+
private readonly backoffBaseMs;
|
|
514
|
+
private readonly maxBackoffMs;
|
|
515
|
+
private readonly unroutableBackoffMs;
|
|
516
|
+
private readonly workerId;
|
|
517
|
+
private readonly deadlines;
|
|
518
|
+
private readonly transports;
|
|
519
|
+
/** Queues served by pollers: the push stage must leave their tasks unclaimed. */
|
|
520
|
+
private readonly pullQueues;
|
|
521
|
+
private timer;
|
|
522
|
+
private current;
|
|
523
|
+
private again;
|
|
524
|
+
private stopped;
|
|
525
|
+
/** Passes still running, whoever started them — the loop, a tick route, a drain. */
|
|
526
|
+
private readonly passes;
|
|
527
|
+
/** Reads the options once; nothing runs until {@link Dispatcher.start}. */
|
|
528
|
+
constructor(ctx: HostContext, options?: DispatcherOptions);
|
|
529
|
+
/** Begins ticking (`loop` mode) or merely arms `tick()` (`manual` mode). Idempotent. */
|
|
530
|
+
start(): void;
|
|
531
|
+
/**
|
|
532
|
+
* Stops ticking and waits for every pass still in flight. The wait is what makes shutdown
|
|
533
|
+
* safe: the host closes the journal next, and a stage halfway through a claim must not be
|
|
534
|
+
* left reading a connection that is gone.
|
|
535
|
+
*/
|
|
536
|
+
stop(): Promise<void>;
|
|
537
|
+
/** Runs a tick soon. Safe to call from inside a tick: it queues one more pass. */
|
|
538
|
+
kick(): void;
|
|
539
|
+
/** One pass over every stage. The tick route and the tests drive the outbox with it. */
|
|
540
|
+
tick(): Promise<TickCounts>;
|
|
541
|
+
private pass;
|
|
542
|
+
private schedule;
|
|
543
|
+
private drain;
|
|
544
|
+
private startRuns;
|
|
545
|
+
private pushTasks;
|
|
546
|
+
/** True when the task was accepted by a worker and recorded as running. */
|
|
547
|
+
private pushOne;
|
|
548
|
+
private backoff;
|
|
549
|
+
private defer;
|
|
550
|
+
private transportFor;
|
|
551
|
+
private buildTransport;
|
|
552
|
+
private notifyParents;
|
|
553
|
+
private notifyParent;
|
|
554
|
+
private markNotified;
|
|
555
|
+
/**
|
|
556
|
+
* Never throws and never writes to stdout: hosts listen on `events` if they care.
|
|
557
|
+
* A closed journal is shutdown, not a fault — the host stopped while this was in flight —
|
|
558
|
+
* so it is dropped rather than reported.
|
|
559
|
+
*/
|
|
560
|
+
private warn;
|
|
561
|
+
}
|
|
562
|
+
/** Wall-clock budget of a drain when `maxMs` is absent: 25 s, under a typical serverless `maxDuration`. */
|
|
563
|
+
declare const DEFAULT_DRAIN_MAX_MS = 25000;
|
|
564
|
+
interface DrainOptions {
|
|
565
|
+
/** Wall-clock budget for the whole drain. Default 25 s. */
|
|
566
|
+
maxMs?: number;
|
|
567
|
+
}
|
|
568
|
+
interface DrainResult {
|
|
569
|
+
/** How many dispatcher passes ran. */
|
|
570
|
+
passes: number;
|
|
571
|
+
/** True when the drain stopped because there was nothing left, false when it ran out of time. */
|
|
572
|
+
idle: boolean;
|
|
573
|
+
/** The counts of the last pass. */
|
|
574
|
+
counts: TickCounts;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Ticks until a pass is quiet or the budget runs out. `Host.drain` wraps this and additionally
|
|
578
|
+
* shares one drain between concurrent callers; call that unless you hold a `HostContext`.
|
|
579
|
+
* Throws a plain `Error` on a context without the `orchestrator` role.
|
|
580
|
+
*
|
|
581
|
+
* @example
|
|
582
|
+
* ```ts
|
|
583
|
+
* // Inside a request on a `manual`-mode host: carry the run as far as it goes right now.
|
|
584
|
+
* const result = await host.drain({ maxMs: 20_000 });
|
|
585
|
+
* if (!result.idle) console.warn(`still busy after ${result.passes} passes`);
|
|
586
|
+
* ```
|
|
587
|
+
*/
|
|
588
|
+
declare function drainHost(ctx: HostContext, options?: DrainOptions): Promise<DrainResult>;
|
|
589
|
+
/** Where a result goes: back into this process, or over HTTP to the host that pushed. */
|
|
590
|
+
interface TaskReporter {
|
|
591
|
+
/** Reports the attempt's output. */
|
|
592
|
+
complete(taskId: string, attempt: number, output: unknown, usage?: Usage): Promise<void>;
|
|
593
|
+
/** Reports a failure; `retryable: false` skips the retry policy. `cause` defaults to `app_error`. */
|
|
594
|
+
fail(taskId: string, attempt: number, error: string, retryable: boolean, usage?: Usage, cause?: Cause): Promise<void>;
|
|
595
|
+
/** Extends the lease. Rejects once the lease is gone. */
|
|
596
|
+
heartbeat(taskId: string, attempt: number): Promise<void>;
|
|
597
|
+
}
|
|
598
|
+
/** Knobs for {@link handleTask}. */
|
|
599
|
+
interface HandleTaskOptions {
|
|
600
|
+
/** The worker's own lifetime. Aborting it tells in-flight handlers to stop. */
|
|
601
|
+
signal?: AbortSignal;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Runs one pushed task on a worker and reports exactly one terminal result. Heartbeats every
|
|
605
|
+
* `heartbeat_ms`; a refused heartbeat, `timeout_ms` elapsing or `options.signal` aborting
|
|
606
|
+
* all abort the handler's `ctx.signal`. A handler throw is reported as a retryable failure;
|
|
607
|
+
* a missing handler as a non-retryable one. Resolves once the result was reported, and
|
|
608
|
+
* rejects only when the reporter itself fails.
|
|
609
|
+
*/
|
|
610
|
+
declare function handleTask(worker: Worker, task: PushedTask, reporter: TaskReporter, options?: HandleTaskOptions): Promise<void>;
|
|
611
|
+
/**
|
|
612
|
+
* Counts handlers this process still owes a result for. One tracker is shared by every
|
|
613
|
+
* in-process transport a host builds, because `Host.drain` has to tell "the dispatcher
|
|
614
|
+
* moved nothing" apart from "the dispatcher moved nothing *yet*: a handler is running and
|
|
615
|
+
* its result will schedule the next task".
|
|
616
|
+
*/
|
|
617
|
+
declare class InProcessTracker {
|
|
618
|
+
private running;
|
|
619
|
+
private done;
|
|
620
|
+
private readonly waiting;
|
|
621
|
+
/** Handlers started and not yet reported. */
|
|
622
|
+
get inFlight(): number;
|
|
623
|
+
/** Monotonic count of handlers that reported. A change across a tick means work landed. */
|
|
624
|
+
get finished(): number;
|
|
625
|
+
/** Counts a handler as started. */
|
|
626
|
+
enter(): void;
|
|
627
|
+
/** Counts a handler as reported; wakes `settled()` waiters when none are left. */
|
|
628
|
+
leave(): void;
|
|
629
|
+
/** Resolves once no in-process handler is outstanding. */
|
|
630
|
+
settled(): Promise<void>;
|
|
631
|
+
}
|
|
632
|
+
/** Knobs for {@link InProcessTransport}. */
|
|
633
|
+
interface InProcessOptions {
|
|
634
|
+
/** Shared with the host, so `Host.drain` can wait for handlers this transport started. */
|
|
635
|
+
tracker?: InProcessTracker;
|
|
636
|
+
/**
|
|
637
|
+
* Where a failure to *report* goes. The handler's own throw is a task failure and the
|
|
638
|
+
* reporter turns it into one; this is the reporter itself failing — a journal that closed
|
|
639
|
+
* under a shutdown, typically — and without a home it would be an unhandled rejection.
|
|
640
|
+
*/
|
|
641
|
+
onError?: (error: unknown) => void;
|
|
642
|
+
}
|
|
643
|
+
/** Runs tasks on a worker in this process. Built by the dispatcher for each `worker:<queue>` role. */
|
|
644
|
+
declare class InProcessTransport implements TaskTransport {
|
|
645
|
+
private readonly worker;
|
|
646
|
+
private readonly reporter;
|
|
647
|
+
private readonly options;
|
|
648
|
+
/** Always `in-process`. */
|
|
649
|
+
readonly kind = "in-process";
|
|
650
|
+
/** `reporter` is where the handler's result goes; usually `localReporter(ctx)`. */
|
|
651
|
+
constructor(worker: Worker, reporter: TaskReporter, options?: InProcessOptions);
|
|
652
|
+
/** Always accepts. The handler starts on the next macrotask, after the dispatcher has committed `running`. */
|
|
653
|
+
push(task: PushedTask): Promise<PushResult>;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* The parts of a Zod schema the host actually uses. Structural, so `Activity` and
|
|
657
|
+
* `SignalDef` satisfy it without the host importing zod — and without fighting the
|
|
658
|
+
* invariance of `z.ZodType` when the registry erases the schema's type.
|
|
659
|
+
*/
|
|
660
|
+
interface Validator {
|
|
661
|
+
/** Zod's `safeParse` shape: never throws, reports issues on failure. */
|
|
662
|
+
safeParse(value: unknown): {
|
|
663
|
+
success: true;
|
|
664
|
+
data: unknown;
|
|
665
|
+
} | {
|
|
666
|
+
success: false;
|
|
667
|
+
error: {
|
|
668
|
+
message: string;
|
|
669
|
+
issues: readonly {
|
|
670
|
+
path: PropertyKey[];
|
|
671
|
+
message: string;
|
|
672
|
+
}[];
|
|
673
|
+
};
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* An activity as the host knows it: a name and a validator for the worker's output.
|
|
678
|
+
* `hash` and `jsonSchema` are present on real `Activity` values and drive registry
|
|
679
|
+
* upserts on boot; a bare `{ name, output }` still validates.
|
|
680
|
+
*/
|
|
681
|
+
interface ActivityRegistration {
|
|
682
|
+
/** Activity name, as the workflow schedules it. */
|
|
683
|
+
readonly name: string;
|
|
684
|
+
/** Validates a worker's output before it is folded; a failure fails the attempt with `schema_violation`. */
|
|
685
|
+
readonly output: Validator;
|
|
686
|
+
/** Content hash of the activity definition; the registry version. */
|
|
687
|
+
readonly hash?: string;
|
|
688
|
+
/** JSON Schema for both sides, written to the registry on boot when `hash` is also present. */
|
|
689
|
+
readonly jsonSchema?: {
|
|
690
|
+
input: unknown;
|
|
691
|
+
output: unknown;
|
|
692
|
+
};
|
|
693
|
+
/** Retry policy, journaled next to the schema as byte-exact JSON. */
|
|
694
|
+
readonly retry?: unknown;
|
|
695
|
+
}
|
|
696
|
+
/** A signal as the host knows it: a name and a validator for incoming payloads. */
|
|
697
|
+
interface SignalRegistration {
|
|
698
|
+
/** Signal name, as the workflow waits for it. */
|
|
699
|
+
readonly name: string;
|
|
700
|
+
/** Validates a payload before delivery; a failure is a `BadRequest`. */
|
|
701
|
+
readonly schema: Validator;
|
|
702
|
+
/** Content hash of the signal definition; the registry version. */
|
|
703
|
+
readonly hash?: string;
|
|
704
|
+
/** JSON Schema of the payload, written to the registry on boot when `hash` is also present. */
|
|
705
|
+
readonly jsonSchema?: unknown;
|
|
706
|
+
}
|
|
707
|
+
/** How tasks on one queue leave this host. */
|
|
708
|
+
interface QueueConfig {
|
|
709
|
+
/** Where to push tasks on this queue. Absent ⇒ in-process only. */
|
|
710
|
+
url?: string;
|
|
711
|
+
/** HMAC secret shared with the queue's worker. Required for HTTP push. */
|
|
712
|
+
secret?: string;
|
|
713
|
+
/** `push` (default) dispatches from the outbox; `pull` leaves tasks for `POST /v1/tasks/poll`. */
|
|
714
|
+
mode?: "push" | "pull";
|
|
715
|
+
}
|
|
716
|
+
/** Every credential a host reads. All optional; an empty config is an open host. */
|
|
717
|
+
interface AuthConfig {
|
|
718
|
+
/** Bearer token required on the control-plane routes. Absent ⇒ open host. */
|
|
719
|
+
token?: string;
|
|
720
|
+
/** HMAC secret this host's worker role verifies inbound pushes with. */
|
|
721
|
+
pushSecret?: string;
|
|
722
|
+
/** Bearer token this host's worker role sends when it calls a task back. */
|
|
723
|
+
callbackToken?: string;
|
|
724
|
+
/**
|
|
725
|
+
* HMAC secret for signed action links. Falls back to `token`, then `pushSecret`; with
|
|
726
|
+
* none of the three configured, minting and verifying links is refused.
|
|
727
|
+
*/
|
|
728
|
+
linkSecret?: string;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* One of the engine's roles: `orchestrator` moves the outbox, `api` serves the control
|
|
732
|
+
* plane, `worker:<queue>` runs that queue's activities in-process and accepts pushes for it.
|
|
733
|
+
*/
|
|
734
|
+
type Role = "orchestrator" | "api" | `worker:${string}`;
|
|
735
|
+
/** The shared state every host function takes as its first argument. Built by `createHost`. */
|
|
736
|
+
interface HostContext {
|
|
737
|
+
/** Durable state and task queue. */
|
|
738
|
+
journal: Journal;
|
|
739
|
+
/** Where payloads over the inline limit are offloaded. */
|
|
740
|
+
blob: BlobStore;
|
|
741
|
+
/** The wasm kernel. A proxy that throws until `start()` has loaded it. */
|
|
742
|
+
kernel: Kernel;
|
|
743
|
+
/** Clock for journaled timestamps, epoch milliseconds. */
|
|
744
|
+
now: () => number;
|
|
745
|
+
/** Namespace new runs are created in. */
|
|
746
|
+
namespace: string;
|
|
747
|
+
/** Registered workflows by name. */
|
|
748
|
+
workflows: Map<string, Workflow>;
|
|
749
|
+
/** Registered activities by name. */
|
|
750
|
+
activities: Map<string, ActivityRegistration>;
|
|
751
|
+
/** Registered signals by name. */
|
|
752
|
+
signals: Map<string, SignalRegistration>;
|
|
753
|
+
/** Workers this process runs in-process, by the queues they serve. */
|
|
754
|
+
workers: Worker[];
|
|
755
|
+
/** Per-queue delivery configuration. */
|
|
756
|
+
queues: Record<string, QueueConfig>;
|
|
757
|
+
/** Credentials this host checks and sends. */
|
|
758
|
+
auth: AuthConfig;
|
|
759
|
+
/** Roles this process holds. */
|
|
760
|
+
roles: ReadonlySet<Role>;
|
|
761
|
+
/** Base URL other processes can reach this host on; fills `complete_url` on a push. */
|
|
762
|
+
publicUrl?: string;
|
|
763
|
+
/**
|
|
764
|
+
* Path prefix this host is mounted under, normalised to `/api/vlo`. The router strips it
|
|
765
|
+
* before matching and every URL built from `publicUrl` carries it.
|
|
766
|
+
*/
|
|
767
|
+
basePath?: string;
|
|
768
|
+
/** How often an open SSE stream looks for new journal events. */
|
|
769
|
+
streamPollMs: number;
|
|
770
|
+
/**
|
|
771
|
+
* In-memory side channel. `emit` carries `cmd.emit` payloads, `warning` carries the
|
|
772
|
+
* dispatcher's, and `task_scheduled` wakes a long-poll the moment a task is written.
|
|
773
|
+
*/
|
|
774
|
+
events: EventEmitter;
|
|
775
|
+
/** Set when this process holds the `orchestrator` role. */
|
|
776
|
+
kick: () => void;
|
|
777
|
+
/** One dispatcher pass. Present only when this process holds the `orchestrator` role. */
|
|
778
|
+
tick?: () => Promise<TickCounts>;
|
|
779
|
+
/** Ticks until idle. Present only when this process holds the `orchestrator` role. */
|
|
780
|
+
drain?: (options?: DrainOptions) => Promise<DrainResult>;
|
|
781
|
+
/** In-process handlers still owing a result; `drain` waits on it between passes. */
|
|
782
|
+
inProcess: InProcessTracker;
|
|
783
|
+
}
|
|
784
|
+
/** The `task_scheduled` payload: enough for a poller to tell whether it cares. */
|
|
785
|
+
interface TaskScheduled {
|
|
786
|
+
/** The run the task belongs to. */
|
|
787
|
+
run_id: string;
|
|
788
|
+
/** The scheduled task. */
|
|
789
|
+
task_id: string;
|
|
790
|
+
/** The queue it was scheduled on. */
|
|
791
|
+
queue: string;
|
|
792
|
+
}
|
|
793
|
+
/** What {@link defineDomain} takes. Every registry is optional. */
|
|
794
|
+
interface DomainDef {
|
|
795
|
+
/** Name for diagnostics; two domains with the same name are refused. */
|
|
796
|
+
name: string;
|
|
797
|
+
/** Workflows this domain owns. */
|
|
798
|
+
workflows?: readonly Workflow[];
|
|
799
|
+
/** Workers run in-process for this domain. Each adds a `worker:<queue>` role when `roles` is absent. */
|
|
800
|
+
workers?: readonly Worker[];
|
|
801
|
+
/** Activities whose output the host validates. */
|
|
802
|
+
activities?: readonly ActivityRegistration[];
|
|
803
|
+
/** Signals whose payload the host validates. */
|
|
804
|
+
signals?: readonly SignalRegistration[];
|
|
805
|
+
}
|
|
806
|
+
/** A defined domain: the value {@link defineDomain} returns and `createHost({ domains })` flattens. */
|
|
807
|
+
interface Domain {
|
|
808
|
+
/** Discriminator, like `Worker.kind`. */
|
|
809
|
+
readonly kind: "domain";
|
|
810
|
+
/** The domain's name. */
|
|
811
|
+
readonly name: string;
|
|
812
|
+
/** Workflows this domain owns. */
|
|
813
|
+
readonly workflows: readonly Workflow[];
|
|
814
|
+
/** Workers run in-process for this domain. */
|
|
815
|
+
readonly workers: readonly Worker[];
|
|
816
|
+
/** Activities whose output the host validates. */
|
|
817
|
+
readonly activities: readonly ActivityRegistration[];
|
|
818
|
+
/** Signals whose payload the host validates. */
|
|
819
|
+
readonly signals: readonly SignalRegistration[];
|
|
820
|
+
}
|
|
821
|
+
/** The four registries a host is built from, flattened. */
|
|
822
|
+
interface Registries {
|
|
823
|
+
/** Every workflow, across domains and the flat option. */
|
|
824
|
+
workflows: readonly Workflow[];
|
|
825
|
+
/** Every worker. */
|
|
826
|
+
workers: readonly Worker[];
|
|
827
|
+
/** Every activity registration. */
|
|
828
|
+
activities: readonly ActivityRegistration[];
|
|
829
|
+
/** Every signal registration. */
|
|
830
|
+
signals: readonly SignalRegistration[];
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Groups one area's workflows, workers, activities and signals under a name, for
|
|
834
|
+
* `createHost({ domains: [...] })`.
|
|
835
|
+
*
|
|
836
|
+
* @example
|
|
837
|
+
* ```ts
|
|
838
|
+
* import { defineDomain } from "@oona/vlo";
|
|
839
|
+
* import { brandActivities } from "./activities.ts";
|
|
840
|
+
* import { brandResearch, brandWeekly } from "./workflows.ts";
|
|
841
|
+
* import { brandWorker } from "./worker.ts";
|
|
842
|
+
*
|
|
843
|
+
* export const brandDomain = defineDomain({
|
|
844
|
+
* name: "brand",
|
|
845
|
+
* workflows: [brandResearch, brandWeekly],
|
|
846
|
+
* workers: [brandWorker],
|
|
847
|
+
* activities: brandActivities,
|
|
848
|
+
* });
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
declare function defineDomain(def: DomainDef): Domain;
|
|
852
|
+
/**
|
|
853
|
+
* Flattens `domains` onto the flat registries. Throws a `TypeError` when two domains share
|
|
854
|
+
* a name, or when two different workflow or activity values are registered under one name;
|
|
855
|
+
* the same value registered twice is fine.
|
|
856
|
+
*/
|
|
857
|
+
declare function mergeDomains(options: Partial<Registries> & {
|
|
858
|
+
domains?: readonly Domain[];
|
|
859
|
+
}): Registries;
|
|
860
|
+
export { DeriveIdsOutput as $, HttpPushTransport as A, WorkerOptions as B, drainHost as C, TickCounts as D, DispatcherOptions as E, ActivityHandler as F, isTerminalRunStatus as G, findWorker as H, ActivityImpls as I, ReductionWire as J, Handlers as K, TaskContext as L, PushedTask as M, TaskTransport as N, FetchLike as O, BlobStore as P, defineWorkflow as Q, UsageInput as R, DrainResult as S, DispatcherMode as T, withRetryInput as U, defineWorker as V, TERMINAL_RUN_STATUSES as W, WorkflowDef as X, Workflow as Y, WorkflowFailure as Z, HandleTaskOptions as _, mergeDomains as a, PlanRetryInput as at, DEFAULT_DRAIN_MAX_MS as b, HostContext as c, ReduceStepInput as ct, SignalRegistration as d, HashAtSeq as et, TaskScheduled as f, InProcessTransport as g, InProcessTracker as h, defineDomain as i, KernelVersion as it, PushResult as j, HttpPushOptions as k, QueueConfig as l, RefoldCheckOutput as lt, InProcessOptions as m, DomainDef as n, KernelError as nt, ActivityRegistration as o, PrepareEventInput as ot, Validator as p, Reduction as q, Registries as r, KernelResult as rt, AuthConfig as s, Prepared as st, Domain as t, Kernel as tt, Role as u, RetryPlan as ut, TaskReporter as v, Dispatcher as w, DrainOptions as x, handleTask as y, Worker as z };
|