@oona/vlo 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +83 -2
- package/dist/agent.d.ts +151 -0
- package/dist/agent.js +445 -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 +95 -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,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers, published as `@oona/vlo/testing`.
|
|
3
|
+
*
|
|
4
|
+
* `journalConformance` is the contract every journal implementation has to
|
|
5
|
+
* satisfy; point it at your own `Journal` and the durability promises the host
|
|
6
|
+
* relies on are checked for you.
|
|
7
|
+
*/
|
|
8
|
+
import { n as Journal } from "./types-BLQQMmkc.js";
|
|
9
|
+
/**
|
|
10
|
+
* Registers the journal conformance suite as a vitest `describe` block named `name`.
|
|
11
|
+
*
|
|
12
|
+
* `open` is called before every case and must return a fresh, empty journal; the suite
|
|
13
|
+
* runs `migrate()` on it and `close()`s it afterwards. The cases check the durability
|
|
14
|
+
* promises the host relies on: gap-free `seq` under concurrent transactions, all-or-nothing
|
|
15
|
+
* transitions, stale-attempt and terminal-task rejection, lease and timeout reaping,
|
|
16
|
+
* once-only timers, schema upserts, signal buffering, run queries and idempotent migration.
|
|
17
|
+
* Call it from a test file, not from application code.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { openJournal } from "@oona/vlo/host";
|
|
22
|
+
* import { journalConformance } from "@oona/vlo/testing";
|
|
23
|
+
*
|
|
24
|
+
* let index = 0;
|
|
25
|
+
* journalConformance("sqlite journal", async () =>
|
|
26
|
+
* openJournal(`sqlite://./.vlo/conformance-${++index}.db`),
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function journalConformance(name: string, open: () => Promise<Journal>): void;
|
|
31
|
+
/** A clock a test moves by hand; `now` is what a host built with `now: clock.now` sees. */
|
|
32
|
+
interface Clock {
|
|
33
|
+
/** Current time as epoch milliseconds. */
|
|
34
|
+
now(): number;
|
|
35
|
+
/** Moves the clock forward by `ms` milliseconds. Never runs backwards. */
|
|
36
|
+
advance(ms: number): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A {@link Clock} that starts at `startMs` and only moves when `advance` is called, so
|
|
40
|
+
* leases, timers and timeouts expire exactly when a test says so.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { fakeClock } from "@oona/vlo/testing";
|
|
45
|
+
*
|
|
46
|
+
* const clock = fakeClock(1_700_000_000_000);
|
|
47
|
+
* const host = createHost({
|
|
48
|
+
* journal: "sqlite://:memory:",
|
|
49
|
+
* workflows: [workflow],
|
|
50
|
+
* now: clock.now,
|
|
51
|
+
* dispatcher: { tickMs: 10 },
|
|
52
|
+
* });
|
|
53
|
+
* await host.start();
|
|
54
|
+
* clock.advance(60_001); // one whole default lease and a millisecond
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function fakeClock(startMs: number): Clock;
|
|
58
|
+
export type { Clock };
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
// src/journal/fixtures.ts
|
|
3
|
+
/**
|
|
4
|
+
* A {@link Clock} that starts at `startMs` and only moves when `advance` is called, so
|
|
5
|
+
* leases, timers and timeouts expire exactly when a test says so.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { fakeClock } from "@oona/vlo/testing";
|
|
10
|
+
*
|
|
11
|
+
* const clock = fakeClock(1_700_000_000_000);
|
|
12
|
+
* const host = createHost({
|
|
13
|
+
* journal: "sqlite://:memory:",
|
|
14
|
+
* workflows: [workflow],
|
|
15
|
+
* now: clock.now,
|
|
16
|
+
* dispatcher: { tickMs: 10 },
|
|
17
|
+
* });
|
|
18
|
+
* await host.start();
|
|
19
|
+
* clock.advance(60_001); // one whole default lease and a millisecond
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function fakeClock(startMs) {
|
|
23
|
+
let current = startMs;
|
|
24
|
+
return {
|
|
25
|
+
now: () => current,
|
|
26
|
+
advance: (ms) => {
|
|
27
|
+
current += ms;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const T0 = 17e11;
|
|
32
|
+
const RETRY = {
|
|
33
|
+
max_attempts: 3,
|
|
34
|
+
backoff: "exponential",
|
|
35
|
+
initial_ms: 1e3,
|
|
36
|
+
max_ms: 6e4,
|
|
37
|
+
factor: 2,
|
|
38
|
+
jitter: "full"
|
|
39
|
+
};
|
|
40
|
+
let counter = 0;
|
|
41
|
+
const nextRunId = () => `run-${++counter}-${Math.random().toString(36).slice(2, 8)}`;
|
|
42
|
+
function newRun(runId, workflow = "example") {
|
|
43
|
+
return {
|
|
44
|
+
run_id: runId,
|
|
45
|
+
workflow,
|
|
46
|
+
component_digest: "digest-1",
|
|
47
|
+
status: "created"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function event(runId, seq) {
|
|
51
|
+
return {
|
|
52
|
+
run_id: runId,
|
|
53
|
+
seq,
|
|
54
|
+
step_id: `step-${seq}`,
|
|
55
|
+
attempt: 1,
|
|
56
|
+
kind: "marker",
|
|
57
|
+
status: "succeeded",
|
|
58
|
+
ts_ms: T0 + seq,
|
|
59
|
+
random: "0".repeat(32),
|
|
60
|
+
state_hash: `hash-${seq}`
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function newTask(runId, stepId, attempt = 1, queue = "default", timeoutMs) {
|
|
64
|
+
return {
|
|
65
|
+
task_id: `${runId}:${stepId}`,
|
|
66
|
+
step_id: stepId,
|
|
67
|
+
attempt,
|
|
68
|
+
activity: "echo",
|
|
69
|
+
queue,
|
|
70
|
+
input: { stepId },
|
|
71
|
+
idempotency_key: `${runId}:${stepId}:${attempt}`,
|
|
72
|
+
retry: RETRY,
|
|
73
|
+
lease_ttl_ms: 3e4,
|
|
74
|
+
...timeoutMs !== void 0 ? { timeout_ms: timeoutMs } : {}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** A minimal well-formed write-set carrying one event at `seq`. */
|
|
78
|
+
function transitionAt(runId, seq, parts = {}) {
|
|
79
|
+
const status = parts.status ?? "running";
|
|
80
|
+
return {
|
|
81
|
+
run: {
|
|
82
|
+
status,
|
|
83
|
+
last_seq: seq
|
|
84
|
+
},
|
|
85
|
+
kernel_state: {
|
|
86
|
+
v: 1,
|
|
87
|
+
status,
|
|
88
|
+
step_no: seq,
|
|
89
|
+
steps: {},
|
|
90
|
+
closed_steps: 0,
|
|
91
|
+
usage_total: {
|
|
92
|
+
cpu_ms: 0,
|
|
93
|
+
tokens: 0,
|
|
94
|
+
cost_micro_usd: 0
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
state: JSON.stringify({ seq }),
|
|
98
|
+
state_hash: `hash-${seq}`,
|
|
99
|
+
state_inline: true,
|
|
100
|
+
events: [event(runId, seq)],
|
|
101
|
+
tasks: parts.tasks ?? [],
|
|
102
|
+
task_updates: parts.task_updates ?? [],
|
|
103
|
+
timers: parts.timers ?? [],
|
|
104
|
+
spawns: [],
|
|
105
|
+
signal_waits: parts.signal_waits ?? [],
|
|
106
|
+
emits: [],
|
|
107
|
+
...parts.continue_as_new !== void 0 ? { continue_as_new: parts.continue_as_new } : {}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// src/journal/conformance-claims.ts
|
|
111
|
+
/**
|
|
112
|
+
* The claim half of the conformance suite: the promises the outbox dispatcher relies on.
|
|
113
|
+
*
|
|
114
|
+
* Every claim is the same bargain — concurrent claimers get disjoint sets, a claimed row is
|
|
115
|
+
* invisible until its hold lapses, and the terminal mark hides it for good. Tasks, timers and
|
|
116
|
+
* closed child runs each keep it with their own column.
|
|
117
|
+
*/
|
|
118
|
+
function claimCases(suite) {
|
|
119
|
+
it("hands concurrent dispatchers disjoint task claims", async () => {
|
|
120
|
+
const journal = suite.journal();
|
|
121
|
+
const runId = await suite.seedTasks(4);
|
|
122
|
+
const clock = fakeClock(1700000001e3);
|
|
123
|
+
const claim = () => journal.transaction((tx) => tx.claimUndispatchedTasks(clock.now(), 4, 3e4));
|
|
124
|
+
const [first, second] = await Promise.all([claim(), claim()]);
|
|
125
|
+
const mine = [...first, ...second].filter((t) => t.run_id === runId);
|
|
126
|
+
const claimed = mine.map((t) => t.task_id);
|
|
127
|
+
expect(claimed).toHaveLength(4);
|
|
128
|
+
expect(new Set(claimed).size).toBe(4);
|
|
129
|
+
expect(mine.every((t) => t.dispatch_attempts === 1)).toBe(true);
|
|
130
|
+
clock.advance(1);
|
|
131
|
+
const again = await journal.transaction((tx) => tx.claimUndispatchedTasks(clock.now(), 4, 3e4));
|
|
132
|
+
expect(again.filter((t) => t.run_id === runId)).toHaveLength(0);
|
|
133
|
+
});
|
|
134
|
+
it("claims one queue, or every queue but the excluded ones", async () => {
|
|
135
|
+
const journal = suite.journal();
|
|
136
|
+
const runId = await suite.seed();
|
|
137
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { tasks: [
|
|
138
|
+
newTask(runId, "d0", 1, "default"),
|
|
139
|
+
newTask(runId, "m0", 1, "media"),
|
|
140
|
+
newTask(runId, "m1", 1, "media")
|
|
141
|
+
] }), { nowMs: T0 }));
|
|
142
|
+
const claim = (nowMs, filter) => journal.transaction((tx) => tx.claimUndispatchedTasks(nowMs, 10, 0, filter)).then((rows) => rows.filter((r) => r.run_id === runId).map((r) => r.step_id).sort());
|
|
143
|
+
expect(await claim(0x18bcfe56801, { queue: "media" })).toEqual(["m0", "m1"]);
|
|
144
|
+
expect(await claim(0x18bcfe56802, { excludeQueues: ["media"] })).toEqual(["d0"]);
|
|
145
|
+
expect(await claim(0x18bcfe56803, { excludeQueues: ["media", "default"] })).toEqual([]);
|
|
146
|
+
expect(await claim(0x18bcfe56804, {
|
|
147
|
+
queue: "media",
|
|
148
|
+
excludeQueues: ["media"]
|
|
149
|
+
})).toEqual([]);
|
|
150
|
+
expect(await claim(0x18bcfe56805)).toEqual([
|
|
151
|
+
"d0",
|
|
152
|
+
"m0",
|
|
153
|
+
"m1"
|
|
154
|
+
]);
|
|
155
|
+
});
|
|
156
|
+
it("returns due timers in fire order and claims them once", async () => {
|
|
157
|
+
const journal = suite.journal();
|
|
158
|
+
const runId = await suite.seed();
|
|
159
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { timers: [
|
|
160
|
+
{
|
|
161
|
+
step_id: "late",
|
|
162
|
+
fire_at_ms: 0x18bcfe5692c
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
step_id: "early",
|
|
166
|
+
fire_at_ms: 0x18bcfe56864
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
step_id: "middle",
|
|
170
|
+
fire_at_ms: 0x18bcfe568c8
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
step_id: "future",
|
|
174
|
+
fire_at_ms: 1700000009e3
|
|
175
|
+
}
|
|
176
|
+
] }), { nowMs: T0 }));
|
|
177
|
+
const due = await journal.dueTimers(0x18bcfe5695e, 2);
|
|
178
|
+
expect(due.map((t) => t.step_id)).toEqual(["early", "middle"]);
|
|
179
|
+
const steps = async (limit) => {
|
|
180
|
+
return (await journal.transaction((tx) => tx.claimDueTimers(0x18bcfe5695e, limit, 6e4))).filter((t) => t.run_id === runId).map((t) => t.step_id).sort();
|
|
181
|
+
};
|
|
182
|
+
expect(await steps(2)).toEqual(["early", "middle"]);
|
|
183
|
+
expect(await steps(10)).toEqual(["late"]);
|
|
184
|
+
expect(await steps(10)).toEqual([]);
|
|
185
|
+
});
|
|
186
|
+
it("walks the run outbox: created runs, then closed children", async () => {
|
|
187
|
+
const journal = suite.journal();
|
|
188
|
+
const parent = await suite.seed();
|
|
189
|
+
const children = [nextRunId(), nextRunId()].sort();
|
|
190
|
+
await journal.transaction((tx) => tx.applyTransition(parent, transitionAt(parent, 1), {
|
|
191
|
+
nowMs: T0,
|
|
192
|
+
childRuns: children.map((runId, i) => ({
|
|
193
|
+
...newRun(runId),
|
|
194
|
+
parent_run_id: parent,
|
|
195
|
+
parent_step_id: `spawn-${i}`
|
|
196
|
+
}))
|
|
197
|
+
}));
|
|
198
|
+
const created = await journal.transaction((tx) => tx.claimCreatedRuns(T0, 50, 3e4));
|
|
199
|
+
expect(created.map((r) => r.run_id)).toEqual(expect.arrayContaining(children));
|
|
200
|
+
const heldBack = await journal.transaction((tx) => tx.claimCreatedRuns(0x18bcfe56801, 50, 3e4));
|
|
201
|
+
expect(heldBack.map((r) => r.run_id)).not.toEqual(expect.arrayContaining(children));
|
|
202
|
+
for (const childId of children) await journal.transaction((tx) => tx.applyTransition(childId, {
|
|
203
|
+
...transitionAt(childId, 1),
|
|
204
|
+
run: {
|
|
205
|
+
status: "completed",
|
|
206
|
+
last_seq: 1
|
|
207
|
+
},
|
|
208
|
+
completion: {
|
|
209
|
+
status: "completed",
|
|
210
|
+
output: { ok: true }
|
|
211
|
+
}
|
|
212
|
+
}, { nowMs: 0x18bcfe56864 }));
|
|
213
|
+
const claim = (nowMs) => journal.transaction((tx) => tx.claimClosedUnnotified(nowMs, 50, 3e4));
|
|
214
|
+
const [first, second] = await Promise.all([claim(0x18bcfe568c8), claim(0x18bcfe568c8)]);
|
|
215
|
+
expect([...first, ...second].map((r) => r.run_id).sort()).toEqual(children);
|
|
216
|
+
expect(await claim(0x18bcfe5692c)).toHaveLength(0);
|
|
217
|
+
const afterHold = await claim(0x18bcfe5ddf9);
|
|
218
|
+
expect(afterHold.map((r) => r.run_id).sort()).toEqual(children);
|
|
219
|
+
for (const childId of children) await journal.transaction((tx) => tx.markParentNotified(childId, 0x18bcfe5692c));
|
|
220
|
+
expect(await claim(17000001e5)).toHaveLength(0);
|
|
221
|
+
const child = await journal.getRun(children[0]);
|
|
222
|
+
expect(child?.output).toBe("{\"ok\":true}");
|
|
223
|
+
expect(child?.closed_at_ms).toBe(0x18bcfe56864);
|
|
224
|
+
expect(child?.parent_notified_at_ms).toBe(0x18bcfe5692c);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
// src/journal/conformance.ts
|
|
228
|
+
/**
|
|
229
|
+
* The journal conformance suite.
|
|
230
|
+
*
|
|
231
|
+
* Every `Journal` implementation must pass it unchanged: these are the
|
|
232
|
+
* durability promises the host builds on — gap-free sequencing, all-or-nothing
|
|
233
|
+
* transitions, disjoint outbox claims, deduped timers and buffered signals.
|
|
234
|
+
* The suite is kernel-independent; hashes here are opaque strings.
|
|
235
|
+
*
|
|
236
|
+
* The outbox claims live in `conformance-claims.ts`, run from the same block.
|
|
237
|
+
*/
|
|
238
|
+
/**
|
|
239
|
+
* Registers the journal conformance suite as a vitest `describe` block named `name`.
|
|
240
|
+
*
|
|
241
|
+
* `open` is called before every case and must return a fresh, empty journal; the suite
|
|
242
|
+
* runs `migrate()` on it and `close()`s it afterwards. The cases check the durability
|
|
243
|
+
* promises the host relies on: gap-free `seq` under concurrent transactions, all-or-nothing
|
|
244
|
+
* transitions, stale-attempt and terminal-task rejection, lease and timeout reaping,
|
|
245
|
+
* once-only timers, schema upserts, signal buffering, run queries and idempotent migration.
|
|
246
|
+
* Call it from a test file, not from application code.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* import { openJournal } from "@oona/vlo/host";
|
|
251
|
+
* import { journalConformance } from "@oona/vlo/testing";
|
|
252
|
+
*
|
|
253
|
+
* let index = 0;
|
|
254
|
+
* journalConformance("sqlite journal", async () =>
|
|
255
|
+
* openJournal(`sqlite://./.vlo/conformance-${++index}.db`),
|
|
256
|
+
* );
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
function journalConformance(name, open) {
|
|
260
|
+
describe(name, () => {
|
|
261
|
+
let journal;
|
|
262
|
+
beforeEach(async () => {
|
|
263
|
+
journal = await open();
|
|
264
|
+
await journal.migrate();
|
|
265
|
+
});
|
|
266
|
+
afterEach(async () => {
|
|
267
|
+
await journal.close();
|
|
268
|
+
});
|
|
269
|
+
const seed = async (workflow) => {
|
|
270
|
+
const runId = nextRunId();
|
|
271
|
+
await journal.transaction((tx) => tx.createRun(newRun(runId, workflow), T0));
|
|
272
|
+
return runId;
|
|
273
|
+
};
|
|
274
|
+
/** Seeds a run with `count` scheduled tasks and returns the run id. */
|
|
275
|
+
const seedTasks = async (count) => {
|
|
276
|
+
const runId = await seed();
|
|
277
|
+
const tasks = Array.from({ length: count }, (_, i) => newTask(runId, `s${i}`));
|
|
278
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { tasks }), { nowMs: T0 }));
|
|
279
|
+
return runId;
|
|
280
|
+
};
|
|
281
|
+
claimCases({
|
|
282
|
+
journal: () => journal,
|
|
283
|
+
seed,
|
|
284
|
+
seedTasks
|
|
285
|
+
});
|
|
286
|
+
it("appends gap-free, strictly increasing seq under concurrent transactions", async () => {
|
|
287
|
+
const runId = await seed();
|
|
288
|
+
const appends = Array.from({ length: 50 }, () => journal.transaction(async (tx) => {
|
|
289
|
+
const seq = ((await tx.lockRun(runId))?.last_seq ?? 0) + 1;
|
|
290
|
+
await tx.applyTransition(runId, transitionAt(runId, seq), { nowMs: T0 + seq });
|
|
291
|
+
}));
|
|
292
|
+
await Promise.all(appends);
|
|
293
|
+
const events = await journal.readEvents(runId);
|
|
294
|
+
expect(events.map((e) => e.seq)).toEqual(Array.from({ length: 50 }, (_, i) => i + 1));
|
|
295
|
+
expect((await journal.getRun(runId))?.last_seq).toBe(50);
|
|
296
|
+
expect((await journal.getState(runId))?.seq).toBe(50);
|
|
297
|
+
expect(await journal.readEvents(runId, 48)).toHaveLength(2);
|
|
298
|
+
});
|
|
299
|
+
it("rolls a transition back when the transaction fails", async () => {
|
|
300
|
+
const runId = await seed();
|
|
301
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1), { nowMs: T0 }));
|
|
302
|
+
await expect(journal.transaction(async (tx) => {
|
|
303
|
+
await tx.applyTransition(runId, transitionAt(runId, 2, { tasks: [newTask(runId, "s0")] }), { nowMs: T0 });
|
|
304
|
+
throw new Error("boom");
|
|
305
|
+
})).rejects.toThrow("boom");
|
|
306
|
+
expect(await journal.readEvents(runId)).toHaveLength(1);
|
|
307
|
+
expect((await journal.getRun(runId))?.last_seq).toBe(1);
|
|
308
|
+
expect(await journal.listTasks(runId)).toHaveLength(0);
|
|
309
|
+
await expect(journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1), { nowMs: T0 }))).rejects.toThrow();
|
|
310
|
+
expect(await journal.readEvents(runId)).toHaveLength(1);
|
|
311
|
+
});
|
|
312
|
+
it("refuses markDispatched on a stale attempt", async () => {
|
|
313
|
+
const taskId = `${await seedTasks(1)}:s0`;
|
|
314
|
+
const now = 1700000001e3;
|
|
315
|
+
expect(await journal.transaction((tx) => tx.markDispatched(taskId, 2, now, 1700000031e3))).toBe(false);
|
|
316
|
+
expect(await journal.transaction((tx) => tx.markDispatched(taskId, 1, now, 1700000031e3, "w1"))).toBe(true);
|
|
317
|
+
expect(await journal.transaction((tx) => tx.markDispatched(taskId, 1, now, 1700000031e3))).toBe(false);
|
|
318
|
+
const task = await journal.getTask(taskId);
|
|
319
|
+
expect(task?.status).toBe("running");
|
|
320
|
+
expect(task?.worker_id).toBe("w1");
|
|
321
|
+
expect(task?.lease_until_ms).toBe(1700000031e3);
|
|
322
|
+
const expired = await journal.expiredLeases(1700000061e3, 10);
|
|
323
|
+
expect(expired.map((t) => t.task_id)).toContain(taskId);
|
|
324
|
+
});
|
|
325
|
+
it("finds running attempts past their timeout, and only those", async () => {
|
|
326
|
+
const runId = await seed();
|
|
327
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { tasks: [newTask(runId, "bounded", 1, "default", 5e3), newTask(runId, "unbounded", 1, "default")] }), { nowMs: T0 }));
|
|
328
|
+
const bounded = `${runId}:bounded`;
|
|
329
|
+
const due = (nowMs) => journal.expiredTimeouts(nowMs, 10).then((rows) => rows.filter((r) => r.run_id === runId).map((r) => r.task_id));
|
|
330
|
+
expect(await due(1700001e6)).toEqual([]);
|
|
331
|
+
await journal.transaction((tx) => tx.markDispatched(bounded, 1, T0, 170000003e4, "w1"));
|
|
332
|
+
await journal.transaction((tx) => tx.markDispatched(`${runId}:unbounded`, 1, T0, 170000003e4, "w1"));
|
|
333
|
+
expect(await due(0x18bcfe57b87)).toEqual([]);
|
|
334
|
+
expect(await due(0x18bcfe57b89)).toEqual([bounded]);
|
|
335
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 2, { task_updates: [{
|
|
336
|
+
task_id: bounded,
|
|
337
|
+
attempt: 1,
|
|
338
|
+
status: "succeeded"
|
|
339
|
+
}] }), { nowMs: 1700000006e3 }));
|
|
340
|
+
expect(await due(17000001e5)).toEqual([]);
|
|
341
|
+
});
|
|
342
|
+
it("stops heartbeats once the task is terminal", async () => {
|
|
343
|
+
const runId = await seedTasks(1);
|
|
344
|
+
const taskId = `${runId}:s0`;
|
|
345
|
+
const now = 1700000001e3;
|
|
346
|
+
await journal.transaction((tx) => tx.markDispatched(taskId, 1, now, 1700000031e3));
|
|
347
|
+
expect(await journal.transaction((tx) => tx.heartbeat(taskId, 1, 1700000061e3, now))).toBe(true);
|
|
348
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 2, { task_updates: [{
|
|
349
|
+
task_id: taskId,
|
|
350
|
+
attempt: 1,
|
|
351
|
+
status: "succeeded"
|
|
352
|
+
}] }), { nowMs: now }));
|
|
353
|
+
expect(await journal.transaction((tx) => tx.heartbeat(taskId, 1, 1700000091e3, now))).toBe(false);
|
|
354
|
+
const task = await journal.getTask(taskId);
|
|
355
|
+
expect(task?.status).toBe("succeeded");
|
|
356
|
+
expect(task?.lease_until_ms).toBeUndefined();
|
|
357
|
+
});
|
|
358
|
+
it("clears dispatch and lease state when a task is rescheduled", async () => {
|
|
359
|
+
const runId = await seedTasks(1);
|
|
360
|
+
const taskId = `${runId}:s0`;
|
|
361
|
+
const now = 1700000001e3;
|
|
362
|
+
await journal.transaction((tx) => tx.markDispatched(taskId, 1, now, 1700000031e3, "w1"));
|
|
363
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 2, { task_updates: [{
|
|
364
|
+
task_id: taskId,
|
|
365
|
+
attempt: 2,
|
|
366
|
+
status: "scheduled",
|
|
367
|
+
not_before_ms: 1700000006e3
|
|
368
|
+
}] }), { nowMs: now }));
|
|
369
|
+
const task = await journal.getTask(taskId);
|
|
370
|
+
expect(task?.status).toBe("scheduled");
|
|
371
|
+
expect(task?.attempt).toBe(2);
|
|
372
|
+
expect(task?.not_before_ms).toBe(1700000006e3);
|
|
373
|
+
expect(task?.dispatched_at_ms).toBeUndefined();
|
|
374
|
+
expect(task?.lease_until_ms).toBeUndefined();
|
|
375
|
+
expect(task?.worker_id).toBeUndefined();
|
|
376
|
+
const tooEarly = await journal.transaction((tx) => tx.claimUndispatchedTasks(1700000002e3, 10, 3e4));
|
|
377
|
+
expect(tooEarly.map((t) => t.task_id)).not.toContain(taskId);
|
|
378
|
+
const dueNow = await journal.transaction((tx) => tx.claimUndispatchedTasks(1700000006e3, 10, 3e4));
|
|
379
|
+
expect(dueNow.map((t) => t.task_id)).toContain(taskId);
|
|
380
|
+
});
|
|
381
|
+
it("fires each timer once", async () => {
|
|
382
|
+
const runId = await seed();
|
|
383
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { timers: [{
|
|
384
|
+
step_id: "t1",
|
|
385
|
+
fire_at_ms: 0x18bcfe56864
|
|
386
|
+
}] }), { nowMs: T0 }));
|
|
387
|
+
expect(await journal.transaction((tx) => tx.markTimerFired(runId, "t1", 0x18bcfe56864))).toBe(true);
|
|
388
|
+
expect(await journal.transaction((tx) => tx.markTimerFired(runId, "t1", 0x18bcfe568c8))).toBe(false);
|
|
389
|
+
expect(await journal.dueTimers(1700000001e3, 10)).toHaveLength(0);
|
|
390
|
+
});
|
|
391
|
+
it("stores and reads activity schemas", async () => {
|
|
392
|
+
await journal.transaction((tx) => tx.upsertSchema({
|
|
393
|
+
name: "echo",
|
|
394
|
+
version: "v1",
|
|
395
|
+
json_schema: "{\"type\":\"object\"}"
|
|
396
|
+
}, T0));
|
|
397
|
+
await journal.transaction((tx) => tx.upsertSchema({
|
|
398
|
+
name: "echo",
|
|
399
|
+
version: "v1",
|
|
400
|
+
json_schema: "{\"type\":\"string\"}",
|
|
401
|
+
retry_policy: "{}"
|
|
402
|
+
}, 0x18bcfe56801));
|
|
403
|
+
const stored = await journal.transaction((tx) => tx.getSchema("echo", "v1"));
|
|
404
|
+
expect(stored?.json_schema).toBe("{\"type\":\"string\"}");
|
|
405
|
+
expect(stored?.retry_policy).toBe("{}");
|
|
406
|
+
expect(await journal.transaction((tx) => tx.getSchema("echo", "v2"))).toBeNull();
|
|
407
|
+
});
|
|
408
|
+
it("buffers signals that arrive before the wait and delivers the oldest first", async () => {
|
|
409
|
+
const runId = await seed();
|
|
410
|
+
await journal.transaction(async (tx) => {
|
|
411
|
+
await tx.putSignal({
|
|
412
|
+
signal_id: "sig-a",
|
|
413
|
+
run_id: runId,
|
|
414
|
+
name: "approve"
|
|
415
|
+
}, 0x18bcfe5680a);
|
|
416
|
+
await tx.putSignal({
|
|
417
|
+
signal_id: "sig-b",
|
|
418
|
+
run_id: runId,
|
|
419
|
+
name: "approve",
|
|
420
|
+
payload: "{\"ok\":true}"
|
|
421
|
+
}, 0x18bcfe56814);
|
|
422
|
+
await tx.putSignal({
|
|
423
|
+
signal_id: "sig-c",
|
|
424
|
+
run_id: runId,
|
|
425
|
+
name: "other"
|
|
426
|
+
}, 0x18bcfe5681e);
|
|
427
|
+
});
|
|
428
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, { signal_waits: [{
|
|
429
|
+
step_id: "w1",
|
|
430
|
+
name: "approve"
|
|
431
|
+
}] }), { nowMs: 0x18bcfe56828 }));
|
|
432
|
+
await journal.transaction(async (tx) => {
|
|
433
|
+
expect((await tx.getSignalWaits(runId)).map((w) => w.step_id)).toEqual(["w1"]);
|
|
434
|
+
const first = await tx.takeSignal(runId, "approve");
|
|
435
|
+
expect(first?.signal_id).toBe("sig-a");
|
|
436
|
+
expect((await tx.takeSignal(runId, "approve"))?.signal_id).toBe("sig-a");
|
|
437
|
+
await tx.markSignalDelivered("sig-a", "w1", 0x18bcfe56832);
|
|
438
|
+
const second = await tx.takeSignal(runId, "approve");
|
|
439
|
+
expect(second?.signal_id).toBe("sig-b");
|
|
440
|
+
expect(second?.payload).toBe("{\"ok\":true}");
|
|
441
|
+
await tx.removeSignalWait(runId, "w1");
|
|
442
|
+
expect(await tx.getSignalWaits(runId)).toHaveLength(0);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
it("filters queries by status and workflow", async () => {
|
|
446
|
+
const running = await seed("alpha");
|
|
447
|
+
await journal.transaction((tx) => tx.applyTransition(running, transitionAt(running, 1), { nowMs: T0 }));
|
|
448
|
+
const created = await seed("alpha");
|
|
449
|
+
await seed("beta");
|
|
450
|
+
const byStatus = await journal.query({
|
|
451
|
+
status: "created",
|
|
452
|
+
workflow: "alpha"
|
|
453
|
+
});
|
|
454
|
+
expect(byStatus.map((r) => r.run_id)).toEqual([created]);
|
|
455
|
+
const byWorkflow = await journal.query({ workflow: "alpha" });
|
|
456
|
+
expect(byWorkflow.map((r) => r.run_id).sort()).toEqual([created, running].sort());
|
|
457
|
+
expect(await journal.query({
|
|
458
|
+
workflow: "alpha",
|
|
459
|
+
limit: 1
|
|
460
|
+
})).toHaveLength(1);
|
|
461
|
+
expect(await journal.query({ namespace: "nowhere" })).toHaveLength(0);
|
|
462
|
+
});
|
|
463
|
+
it("creates runs idempotently", async () => {
|
|
464
|
+
const runId = nextRunId();
|
|
465
|
+
await journal.transaction(async (tx) => {
|
|
466
|
+
await tx.createRun(newRun(runId, "alpha"), T0);
|
|
467
|
+
await tx.createRun({
|
|
468
|
+
...newRun(runId, "beta"),
|
|
469
|
+
input: "{\"n\":1}"
|
|
470
|
+
}, 0x18bcfe56801);
|
|
471
|
+
});
|
|
472
|
+
const run = await journal.getRun(runId);
|
|
473
|
+
expect(run?.workflow).toBe("alpha");
|
|
474
|
+
expect(run?.input).toBeUndefined();
|
|
475
|
+
expect(run?.status).toBe("created");
|
|
476
|
+
expect(run?.namespace).toBe("default");
|
|
477
|
+
});
|
|
478
|
+
it("links a continued run to the successor inserted in the same transition", async () => {
|
|
479
|
+
const runId = await seed("alpha");
|
|
480
|
+
const successorId = `${runId}-next`;
|
|
481
|
+
await journal.transaction((tx) => tx.applyTransition(runId, transitionAt(runId, 1, {
|
|
482
|
+
status: "continued",
|
|
483
|
+
continue_as_new: { input: { n: 2 } }
|
|
484
|
+
}), {
|
|
485
|
+
nowMs: T0,
|
|
486
|
+
successorRun: {
|
|
487
|
+
...newRun(successorId, "alpha"),
|
|
488
|
+
continued_from: runId,
|
|
489
|
+
input: "{\"n\":2}"
|
|
490
|
+
}
|
|
491
|
+
}));
|
|
492
|
+
const original = await journal.getRun(runId);
|
|
493
|
+
expect(original?.status).toBe("continued");
|
|
494
|
+
expect(original?.continued_to).toBe(successorId);
|
|
495
|
+
expect(original?.closed_at_ms).toBe(T0);
|
|
496
|
+
const successor = await journal.getRun(successorId);
|
|
497
|
+
expect(successor?.status).toBe("created");
|
|
498
|
+
expect(successor?.continued_from).toBe(runId);
|
|
499
|
+
expect(successor?.input).toBe("{\"n\":2}");
|
|
500
|
+
const claimed = await journal.transaction((tx) => tx.claimCreatedRuns(T0, 10, 1e3));
|
|
501
|
+
expect(claimed.map((r) => r.run_id)).toContain(successorId);
|
|
502
|
+
});
|
|
503
|
+
it("migrates twice without effect", async () => {
|
|
504
|
+
const runId = await seed();
|
|
505
|
+
await journal.migrate();
|
|
506
|
+
await journal.migrate();
|
|
507
|
+
expect(await journal.getRun(runId)).not.toBeNull();
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
export { fakeClock, journalConformance };
|