@operad-io/engine 0.1.1 → 0.3.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/config.schema.json +2 -1
- package/package.json +1 -1
- package/src/config/config.ts +74 -45
- package/src/flow/types.ts +4 -0
- package/src/flow/validate.ts +5 -0
- package/src/http/index.ts +2 -0
- package/src/http/plugins.ts +68 -0
- package/src/http/server.ts +342 -48
- package/src/kernel/decide.ts +115 -30
- package/src/kernel/state.ts +9 -0
- package/src/kernel/types.ts +25 -40
- package/src/log/open-log.ts +2 -5
- package/src/log/sqlite.ts +127 -85
- package/src/log/types.ts +35 -9
- package/src/loop/code-watch.ts +108 -2
- package/src/loop/older-code.ts +3 -4
- package/src/loop/serve.ts +122 -95
- package/src/notify/notify.ts +2 -6
- package/src/platform/atomic-write.ts +163 -0
- package/src/platform/file-lock.ts +43 -21
- package/src/platform/git-program.ts +108 -0
- package/src/platform/index.ts +3 -0
- package/src/platform/kept-workers.ts +290 -0
- package/src/platform/process-group.ts +11 -3
- package/src/platform/sqlite-open.ts +20 -6
- package/src/platform/sqlite-schema.ts +30 -0
- package/src/platform/step-reap.ts +33 -18
- package/src/platform/watch.ts +1 -11
- package/src/plugin/action-schema.ts +59 -1
- package/src/plugin/choice-info.ts +133 -0
- package/src/plugin/context.ts +279 -0
- package/src/plugin/declared-flows.ts +3 -10
- package/src/plugin/errors.ts +9 -0
- package/src/plugin/freeze.ts +12 -0
- package/src/plugin/harness.ts +28 -0
- package/src/plugin/index.ts +8 -7
- package/src/plugin/install.ts +265 -17
- package/src/plugin/keys.ts +2 -12
- package/src/plugin/load.ts +47 -23
- package/src/plugin/logger.ts +14 -0
- package/src/plugin/open-store.ts +0 -6
- package/src/plugin/question-form.ts +323 -0
- package/src/plugin/registry.ts +519 -76
- package/src/plugin/secrets.ts +14 -24
- package/src/plugin/semver.ts +52 -0
- package/src/plugin/settings.ts +1 -9
- package/src/plugin/sqlite-store.ts +94 -64
- package/src/plugin/store.ts +5 -0
- package/src/plugin/types.ts +575 -21
- package/src/plugin/validate.ts +402 -26
- package/src/runner/answer.ts +26 -68
- package/src/runner/ask.ts +227 -28
- package/src/runner/cancel.ts +10 -7
- package/src/runner/grants.ts +2 -2
- package/src/runner/index.ts +1 -3
- package/src/runner/limits.ts +2 -2
- package/src/runner/runner.ts +15 -14
- package/src/runner/service.ts +52 -19
- package/src/runner/step-worker.ts +9 -0
- package/src/testing/answer.ts +69 -0
- package/src/testing/clock-manual.ts +152 -0
- package/src/testing/config.ts +16 -0
- package/src/testing/fs.ts +172 -0
- package/src/testing/home.ts +43 -0
- package/src/testing/index.ts +19 -0
- package/src/testing/types.ts +2 -0
- package/src/testing/world.ts +9 -2
- package/src/trigger/kernel.ts +13 -46
- package/src/trigger/rules.ts +22 -12
- package/src/trigger/types.ts +2 -6
- package/src/util.ts +99 -0
- package/src/views/event-line.ts +22 -0
- package/src/views/history.ts +36 -19
- package/src/views/live-logs.ts +313 -0
- package/src/views/pending.ts +23 -5
- package/src/views/presentation.ts +629 -81
- package/src/views/running.ts +5 -5
- package/src/views/settings.ts +205 -66
- package/src/views/types.ts +8 -2
- package/src/views/uploads.ts +15 -7
- package/triggers.schema.json +4 -0
package/src/log/sqlite.ts
CHANGED
|
@@ -4,13 +4,16 @@
|
|
|
4
4
|
* Every append and every startRun is one `BEGIN IMMEDIATE` transaction, so a
|
|
5
5
|
* process killed mid-write leaves either the whole batch or none of it. WAL
|
|
6
6
|
* lets a reader keep a consistent snapshot of a run while another process
|
|
7
|
-
* appends to it.
|
|
7
|
+
* appends to it. Writes go through the platform's `writeImmediate`: a full
|
|
8
|
+
* store shared by serve, its run processes and other commands retries while
|
|
9
|
+
* another connection is committing instead of giving up at the busy timeout.
|
|
8
10
|
*/
|
|
9
11
|
|
|
12
|
+
import { messageOf } from "../plugin/errors.ts";
|
|
10
13
|
import { Database } from "bun:sqlite";
|
|
11
14
|
import { randomUUID } from "node:crypto";
|
|
12
15
|
import { accessSync, constants, existsSync, mkdirSync } from "node:fs";
|
|
13
|
-
import { dirname
|
|
16
|
+
import { dirname } from "node:path";
|
|
14
17
|
import {
|
|
15
18
|
ActiveRunError,
|
|
16
19
|
CorruptLogError,
|
|
@@ -35,8 +38,11 @@ import {
|
|
|
35
38
|
enableWal,
|
|
36
39
|
resolveSubscriptionWatch,
|
|
37
40
|
setBusyTimeout,
|
|
41
|
+
writeImmediate,
|
|
42
|
+
type ImmediateWriter,
|
|
38
43
|
type SubscriptionWatch,
|
|
39
44
|
} from "../platform/index.ts";
|
|
45
|
+
import { hasTable, readSchemaVersion } from "../platform/sqlite-schema.ts";
|
|
40
46
|
import type {
|
|
41
47
|
AppendResult,
|
|
42
48
|
Fact,
|
|
@@ -47,10 +53,10 @@ import type {
|
|
|
47
53
|
InputsAfter,
|
|
48
54
|
JsonValue,
|
|
49
55
|
LogBackend,
|
|
50
|
-
LogBackendFactory,
|
|
51
56
|
LogBackendOptions,
|
|
52
57
|
LogChange,
|
|
53
58
|
NewFact,
|
|
59
|
+
ReadOptions,
|
|
54
60
|
RecordInputResult,
|
|
55
61
|
RecordOutcomeResult,
|
|
56
62
|
RunInfo,
|
|
@@ -199,10 +205,6 @@ interface RunSnapshot {
|
|
|
199
205
|
readonly ended: boolean;
|
|
200
206
|
}
|
|
201
207
|
|
|
202
|
-
interface VersionRow {
|
|
203
|
-
readonly version: number;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
208
|
interface TableColumn {
|
|
207
209
|
readonly name: string;
|
|
208
210
|
readonly notnull: number;
|
|
@@ -218,16 +220,6 @@ function tableColumns(database: Database, table: string): TableColumn[] {
|
|
|
218
220
|
}
|
|
219
221
|
}
|
|
220
222
|
|
|
221
|
-
function hasTable(database: Database, table: string): boolean {
|
|
222
|
-
return (
|
|
223
|
-
database
|
|
224
|
-
.query<{ name: string }, [string]>(
|
|
225
|
-
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?",
|
|
226
|
-
)
|
|
227
|
-
.get(table) !== null
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
223
|
function hasIndex(database: Database, index: string): boolean {
|
|
232
224
|
return (
|
|
233
225
|
database
|
|
@@ -268,7 +260,7 @@ interface DataVersionRow {
|
|
|
268
260
|
}
|
|
269
261
|
|
|
270
262
|
function openFailure(path: string, error: unknown): LogBackendError {
|
|
271
|
-
const detail =
|
|
263
|
+
const detail = messageOf(error);
|
|
272
264
|
return new LogBackendError(`cannot open the sqlite log at ${path}: ${detail}`);
|
|
273
265
|
}
|
|
274
266
|
|
|
@@ -290,18 +282,6 @@ function configure(database: Database): void {
|
|
|
290
282
|
enableWal(database);
|
|
291
283
|
}
|
|
292
284
|
|
|
293
|
-
/** The recorded version, or undefined when the schema has not been created yet. */
|
|
294
|
-
function readSchemaVersion(database: Database): number | undefined {
|
|
295
|
-
try {
|
|
296
|
-
const row = database
|
|
297
|
-
.query<VersionRow, []>("SELECT version FROM schema_version WHERE id = 1")
|
|
298
|
-
.get();
|
|
299
|
-
return row && Number.isInteger(row.version) ? row.version : undefined;
|
|
300
|
-
} catch {
|
|
301
|
-
return undefined;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
285
|
/**
|
|
306
286
|
* True when an open would have to change the schema. The checks are read-only,
|
|
307
287
|
* so opening an up-to-date store takes no write lock while another process is
|
|
@@ -316,6 +296,7 @@ function schemaNeedsWork(database: Database): boolean {
|
|
|
316
296
|
!hasTable(database, "outcomes") ||
|
|
317
297
|
!hasIndex(database, "runs_one_active") ||
|
|
318
298
|
!hasIndex(database, "runs_object") ||
|
|
299
|
+
!hasIndex(database, "runs_start_input") ||
|
|
319
300
|
inputsNeedMigration(database) ||
|
|
320
301
|
!tableColumns(database, "runs").some((item) => item.name === "start_input_id") ||
|
|
321
302
|
!tableColumns(database, "global_facts").some((item) => item.name === "unique_key") ||
|
|
@@ -328,12 +309,15 @@ function ensureSchema(database: Database, path: string): void {
|
|
|
328
309
|
if (version === undefined) {
|
|
329
310
|
// One immediate transaction for the whole schema: a second process opening
|
|
330
311
|
// the file at the same time waits in the busy handler, then finds it done.
|
|
331
|
-
database
|
|
312
|
+
writeImmediate(database, () => {
|
|
332
313
|
database.exec(SCHEMA);
|
|
314
|
+
database.exec(
|
|
315
|
+
"CREATE INDEX IF NOT EXISTS runs_start_input ON runs (start_input_id)",
|
|
316
|
+
);
|
|
333
317
|
database.run("INSERT OR IGNORE INTO schema_version (id, version) VALUES (1, ?)", [
|
|
334
318
|
SCHEMA_VERSION,
|
|
335
319
|
]);
|
|
336
|
-
})
|
|
320
|
+
});
|
|
337
321
|
version = readSchemaVersion(database);
|
|
338
322
|
}
|
|
339
323
|
|
|
@@ -360,7 +344,7 @@ function ensureSchema(database: Database, path: string): void {
|
|
|
360
344
|
// and an older runs table that could not name the input that made the run.
|
|
361
345
|
// A current store needs none of it, so opening it stays read-only.
|
|
362
346
|
if (!schemaNeedsWork(database)) return;
|
|
363
|
-
database
|
|
347
|
+
writeImmediate(database, () => {
|
|
364
348
|
// Recreate any base table or index a store lost; every statement is
|
|
365
349
|
// `IF NOT EXISTS`, so an existing object is left as it is.
|
|
366
350
|
database.exec(SCHEMA);
|
|
@@ -371,6 +355,9 @@ function ensureSchema(database: Database, path: string): void {
|
|
|
371
355
|
if (!runsHaveStartInput) {
|
|
372
356
|
database.exec("ALTER TABLE runs ADD COLUMN start_input_id TEXT");
|
|
373
357
|
}
|
|
358
|
+
database.exec(
|
|
359
|
+
"CREATE INDEX IF NOT EXISTS runs_start_input ON runs (start_input_id)",
|
|
360
|
+
);
|
|
374
361
|
// A store written before global facts could be unique gains the column.
|
|
375
362
|
const globalHasUniqueKey = tableColumns(database, "global_facts").some(
|
|
376
363
|
(item) => item.name === "unique_key",
|
|
@@ -382,7 +369,7 @@ function ensureSchema(database: Database, path: string): void {
|
|
|
382
369
|
"CREATE UNIQUE INDEX IF NOT EXISTS global_facts_unique_key " +
|
|
383
370
|
"ON global_facts (unique_key) WHERE unique_key IS NOT NULL",
|
|
384
371
|
);
|
|
385
|
-
})
|
|
372
|
+
});
|
|
386
373
|
}
|
|
387
374
|
|
|
388
375
|
/**
|
|
@@ -394,7 +381,7 @@ function migrateSchema(database: Database, path: string, from: number): number {
|
|
|
394
381
|
let version = from;
|
|
395
382
|
while (version < SCHEMA_VERSION) {
|
|
396
383
|
if (version === 1) {
|
|
397
|
-
database
|
|
384
|
+
writeImmediate(database, () => {
|
|
398
385
|
const columns = database
|
|
399
386
|
.query<{ readonly name: string }, []>("PRAGMA table_info(runs)")
|
|
400
387
|
.all();
|
|
@@ -402,7 +389,7 @@ function migrateSchema(database: Database, path: string, from: number): number {
|
|
|
402
389
|
database.run("ALTER TABLE runs ADD COLUMN access TEXT");
|
|
403
390
|
}
|
|
404
391
|
database.run("UPDATE schema_version SET version = ? WHERE id = 1", [2]);
|
|
405
|
-
})
|
|
392
|
+
});
|
|
406
393
|
version = 2;
|
|
407
394
|
continue;
|
|
408
395
|
}
|
|
@@ -415,6 +402,7 @@ function migrateSchema(database: Database, path: string, from: number): number {
|
|
|
415
402
|
|
|
416
403
|
class SqliteLogBackend implements LogBackend {
|
|
417
404
|
readonly #database: Database;
|
|
405
|
+
readonly #immediateWriter: ((database: Database) => ImmediateWriter) | undefined;
|
|
418
406
|
readonly #clock: () => number;
|
|
419
407
|
readonly #newRunId: () => string;
|
|
420
408
|
readonly #path: string;
|
|
@@ -434,9 +422,11 @@ class SqliteLogBackend implements LogBackend {
|
|
|
434
422
|
path: string,
|
|
435
423
|
subscriptionWatch: SubscriptionWatch,
|
|
436
424
|
onSnapshot?: () => void,
|
|
425
|
+
immediateWriter?: (database: Database) => ImmediateWriter,
|
|
437
426
|
) {
|
|
438
427
|
this.#onSnapshot = onSnapshot;
|
|
439
428
|
this.#database = database;
|
|
429
|
+
this.#immediateWriter = immediateWriter;
|
|
440
430
|
this.#clock = clock;
|
|
441
431
|
this.#newRunId = newRunId;
|
|
442
432
|
this.#path = path;
|
|
@@ -453,7 +443,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
453
443
|
assertName(object, "object");
|
|
454
444
|
if (access !== undefined) assertJsonValue(access, "run access");
|
|
455
445
|
this.#assertOpen();
|
|
456
|
-
return this.#
|
|
446
|
+
return this.#write(() => {
|
|
457
447
|
const active = this.#database
|
|
458
448
|
.query<{ run_id: string }, [string, string]>(
|
|
459
449
|
"SELECT run_id FROM runs WHERE flow = ? AND object = ? AND ended_at IS NULL",
|
|
@@ -488,13 +478,13 @@ class SqliteLogBackend implements LogBackend {
|
|
|
488
478
|
...(access === undefined ? {} : { access }),
|
|
489
479
|
startInputId,
|
|
490
480
|
};
|
|
491
|
-
})
|
|
481
|
+
});
|
|
492
482
|
}
|
|
493
483
|
|
|
494
484
|
async endRun(runId: string): Promise<void> {
|
|
495
485
|
assertRunId(runId);
|
|
496
486
|
this.#assertOpen();
|
|
497
|
-
this.#
|
|
487
|
+
this.#write(() => {
|
|
498
488
|
const row = this.#runRow(runId);
|
|
499
489
|
if (!row) throw new RunNotFoundError(runId);
|
|
500
490
|
if (row.ended_at !== null) return;
|
|
@@ -503,7 +493,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
503
493
|
runId,
|
|
504
494
|
]);
|
|
505
495
|
this.#notify({ runId, object: row.object });
|
|
506
|
-
})
|
|
496
|
+
});
|
|
507
497
|
}
|
|
508
498
|
|
|
509
499
|
async recordInput(
|
|
@@ -517,7 +507,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
517
507
|
assertName(sourceKey, "sourceKey");
|
|
518
508
|
assertJsonValue(payload, "input payload");
|
|
519
509
|
this.#assertOpen();
|
|
520
|
-
return this.#
|
|
510
|
+
return this.#write(() => {
|
|
521
511
|
if (runId !== null) {
|
|
522
512
|
const row = this.#runRow(runId);
|
|
523
513
|
if (!row) throw new RunNotFoundError(runId);
|
|
@@ -531,7 +521,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
531
521
|
[inputId, runId, source, sourceKey, JSON.stringify(payload), this.#clock()],
|
|
532
522
|
);
|
|
533
523
|
return { duplicate: result.changes === 0, inputId };
|
|
534
|
-
})
|
|
524
|
+
});
|
|
535
525
|
}
|
|
536
526
|
|
|
537
527
|
async append(
|
|
@@ -544,7 +534,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
544
534
|
for (const fact of facts) validateNewFact(fact);
|
|
545
535
|
this.#assertOpen();
|
|
546
536
|
|
|
547
|
-
return this.#
|
|
537
|
+
return this.#write(() => {
|
|
548
538
|
const row = this.#runRow(runId);
|
|
549
539
|
if (!row) throw new RunNotFoundError(runId);
|
|
550
540
|
if (row.ended_at !== null) throw new RunEndedError(runId);
|
|
@@ -582,27 +572,35 @@ class SqliteLogBackend implements LogBackend {
|
|
|
582
572
|
}
|
|
583
573
|
this.#notify({ runId, object: row.object });
|
|
584
574
|
return { lastSeq: row.last_seq + stored.length, facts: stored };
|
|
585
|
-
})
|
|
575
|
+
});
|
|
586
576
|
}
|
|
587
577
|
|
|
588
|
-
read(runId: string): AsyncIterable<Fact> {
|
|
578
|
+
read(runId: string, options: ReadOptions = {}): AsyncIterable<Fact> {
|
|
589
579
|
assertRunId(runId);
|
|
590
|
-
return this.#read(runId);
|
|
580
|
+
return this.#read(runId, options.after ?? 0);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
async run(runId: string): Promise<RunInfo | undefined> {
|
|
584
|
+
this.#assertOpen();
|
|
585
|
+
const row = this.#runRow(runId);
|
|
586
|
+
return row === null ? undefined : this.#toRunInfo(row);
|
|
591
587
|
}
|
|
592
588
|
|
|
593
589
|
async runs(options: RunsOptions = {}): Promise<RunInfo[]> {
|
|
594
590
|
this.#assertOpen();
|
|
595
591
|
const activeOnly = options.active === true ? 1 : 0;
|
|
596
592
|
const object = options.object ?? null;
|
|
593
|
+
const startInputId = options.startInputId ?? null;
|
|
597
594
|
const rows = this.#database
|
|
598
|
-
.query<RunRow, [number, string | null, string | null]>(
|
|
595
|
+
.query<RunRow, [number, string | null, string | null, string | null, string | null]>(
|
|
599
596
|
"SELECT r.run_id, r.flow, r.object, r.started_at, r.ended_at, r.start_input_id, r.access, " +
|
|
600
597
|
"COALESCE((SELECT MAX(seq) FROM facts WHERE run_id = r.run_id), 0) AS last_seq " +
|
|
601
598
|
"FROM runs r " +
|
|
602
599
|
"WHERE (? = 0 OR r.ended_at IS NULL) AND (? IS NULL OR r.object = ?) " +
|
|
600
|
+
"AND (? IS NULL OR r.start_input_id = ?) " +
|
|
603
601
|
"ORDER BY r.started_at, r.run_id",
|
|
604
602
|
)
|
|
605
|
-
.all(activeOnly, object, object);
|
|
603
|
+
.all(activeOnly, object, object, startInputId, startInputId);
|
|
606
604
|
return rows.map((row) => this.#toRunInfo(row));
|
|
607
605
|
}
|
|
608
606
|
|
|
@@ -618,7 +616,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
618
616
|
validateNewFact(fact);
|
|
619
617
|
if (uniqueKey !== undefined) assertName(uniqueKey, "uniqueKey");
|
|
620
618
|
this.#assertOpen();
|
|
621
|
-
return this.#
|
|
619
|
+
return this.#write(() => {
|
|
622
620
|
if (uniqueKey !== undefined) {
|
|
623
621
|
const existing = this.#database
|
|
624
622
|
.query<GlobalFactRow, [string]>(
|
|
@@ -653,7 +651,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
653
651
|
time,
|
|
654
652
|
...(uniqueKey === undefined ? {} : { uniqueKey }),
|
|
655
653
|
};
|
|
656
|
-
})
|
|
654
|
+
});
|
|
657
655
|
}
|
|
658
656
|
|
|
659
657
|
readGlobal(): AsyncIterable<GlobalFact> {
|
|
@@ -666,11 +664,13 @@ class SqliteLogBackend implements LogBackend {
|
|
|
666
664
|
"SELECT input_id, run_id, source, source_key, payload, time " +
|
|
667
665
|
"FROM inputs ORDER BY time, input_id",
|
|
668
666
|
);
|
|
667
|
+
let rows: InputRow[];
|
|
669
668
|
try {
|
|
670
|
-
|
|
669
|
+
rows = statement.all();
|
|
671
670
|
} finally {
|
|
672
671
|
statement.finalize();
|
|
673
672
|
}
|
|
673
|
+
for (const row of rows) yield inputOfRow(row);
|
|
674
674
|
}
|
|
675
675
|
|
|
676
676
|
async readInputsAfter(position: number): Promise<InputsAfter> {
|
|
@@ -728,7 +728,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
728
728
|
assertName(kind, "outcome kind");
|
|
729
729
|
assertJsonValue(payload, "outcome payload");
|
|
730
730
|
this.#assertOpen();
|
|
731
|
-
return this.#
|
|
731
|
+
return this.#write(() => {
|
|
732
732
|
const existing = this.#outcomeRow(inputId, kind);
|
|
733
733
|
if (existing !== undefined) {
|
|
734
734
|
return { duplicate: true, outcome: outcomeOfRow(existing) };
|
|
@@ -748,7 +748,7 @@ class SqliteLogBackend implements LogBackend {
|
|
|
748
748
|
duplicate: false,
|
|
749
749
|
outcome: { inputId, kind, payload, seq, time },
|
|
750
750
|
};
|
|
751
|
-
})
|
|
751
|
+
});
|
|
752
752
|
}
|
|
753
753
|
|
|
754
754
|
async *readOutcomes(): AsyncGenerator<InputOutcome> {
|
|
@@ -756,13 +756,13 @@ class SqliteLogBackend implements LogBackend {
|
|
|
756
756
|
const statement = this.#database.prepare<OutcomeRow, []>(
|
|
757
757
|
"SELECT input_id, kind, payload, seq, time FROM outcomes ORDER BY seq",
|
|
758
758
|
);
|
|
759
|
+
let rows: OutcomeRow[];
|
|
759
760
|
try {
|
|
760
|
-
|
|
761
|
-
yield outcomeOfRow(row);
|
|
762
|
-
}
|
|
761
|
+
rows = statement.all();
|
|
763
762
|
} finally {
|
|
764
763
|
statement.finalize();
|
|
765
764
|
}
|
|
765
|
+
for (const row of rows) yield outcomeOfRow(row);
|
|
766
766
|
}
|
|
767
767
|
|
|
768
768
|
#outcomeRow(inputId: string, kind: string): OutcomeRow | undefined {
|
|
@@ -774,18 +774,52 @@ class SqliteLogBackend implements LogBackend {
|
|
|
774
774
|
.get(inputId, kind) ?? undefined;
|
|
775
775
|
}
|
|
776
776
|
|
|
777
|
+
async openCandidateRoutes(): Promise<InputOutcome[]> {
|
|
778
|
+
this.#assertOpen();
|
|
779
|
+
return this.#database
|
|
780
|
+
.query<OutcomeRow, []>(
|
|
781
|
+
"SELECT o.input_id, o.kind, o.payload, o.seq, o.time FROM outcomes o " +
|
|
782
|
+
"WHERE o.kind = 'route' " +
|
|
783
|
+
"AND json_extract(o.payload, '$.value') = 'candidate' " +
|
|
784
|
+
"AND NOT EXISTS (SELECT 1 FROM outcomes r " +
|
|
785
|
+
"WHERE r.input_id = o.input_id AND r.kind = 'resolve') " +
|
|
786
|
+
"ORDER BY o.seq",
|
|
787
|
+
)
|
|
788
|
+
.all()
|
|
789
|
+
.map(outcomeOfRow);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
async startRoutesWithoutRun(): Promise<InputOutcome[]> {
|
|
793
|
+
this.#assertOpen();
|
|
794
|
+
return this.#database
|
|
795
|
+
.query<OutcomeRow, []>(
|
|
796
|
+
"SELECT o.input_id, o.kind, o.payload, o.seq, o.time FROM outcomes o " +
|
|
797
|
+
"WHERE o.kind = 'route' " +
|
|
798
|
+
"AND (json_extract(o.payload, '$.value') = 'started' " +
|
|
799
|
+
" OR (json_extract(o.payload, '$.value') = 'candidate' " +
|
|
800
|
+
" AND EXISTS (SELECT 1 FROM outcomes r " +
|
|
801
|
+
" WHERE r.input_id = o.input_id AND r.kind = 'resolve' " +
|
|
802
|
+
" AND json_extract(r.payload, '$.value') = 'started'))) " +
|
|
803
|
+
"AND NOT EXISTS (SELECT 1 FROM runs n " +
|
|
804
|
+
"WHERE n.start_input_id = o.input_id) " +
|
|
805
|
+
"ORDER BY o.seq",
|
|
806
|
+
)
|
|
807
|
+
.all()
|
|
808
|
+
.map(outcomeOfRow);
|
|
809
|
+
}
|
|
810
|
+
|
|
777
811
|
async *#readGlobal(): AsyncGenerator<GlobalFact> {
|
|
778
812
|
this.#assertOpen();
|
|
779
813
|
const statement = this.#database.prepare<GlobalFactRow, []>(
|
|
780
814
|
"SELECT seq, kind, payload, cause, time, unique_key FROM global_facts ORDER BY seq",
|
|
781
815
|
);
|
|
816
|
+
let rows: GlobalFactRow[];
|
|
782
817
|
try {
|
|
783
|
-
|
|
784
|
-
yield globalFactOfRow(row);
|
|
785
|
-
}
|
|
818
|
+
rows = statement.all();
|
|
786
819
|
} finally {
|
|
787
820
|
statement.finalize();
|
|
788
821
|
}
|
|
822
|
+
for (const row of rows) yield globalFactOfRow(row);
|
|
789
823
|
}
|
|
790
824
|
|
|
791
825
|
/** Tells a listener in this process when a run changes. */
|
|
@@ -895,32 +929,47 @@ class SqliteLogBackend implements LogBackend {
|
|
|
895
929
|
return snapshot;
|
|
896
930
|
}
|
|
897
931
|
|
|
898
|
-
async *#read(runId: string): AsyncGenerator<Fact> {
|
|
932
|
+
async *#read(runId: string, after: number): AsyncGenerator<Fact> {
|
|
899
933
|
this.#assertOpen();
|
|
900
934
|
if (!this.#runRow(runId)) throw new RunNotFoundError(runId);
|
|
901
935
|
|
|
902
|
-
const statement = this.#database.prepare<FactRow, [string]>(
|
|
903
|
-
"SELECT seq, kind, payload, cause, time FROM facts
|
|
936
|
+
const statement = this.#database.prepare<FactRow, [string, number]>(
|
|
937
|
+
"SELECT seq, kind, payload, cause, time FROM facts " +
|
|
938
|
+
"WHERE run_id = ? AND seq > ? ORDER BY seq",
|
|
904
939
|
);
|
|
905
|
-
|
|
940
|
+
// Read every row before the first yield. A statement left between two
|
|
941
|
+
// facts holds a read transaction on this connection, and the next write
|
|
942
|
+
// then fails without waiting while another connection works:
|
|
943
|
+
// SQLITE_BUSY_SNAPSHOT once it has committed, or plain SQLITE_BUSY when it
|
|
944
|
+
// holds the write lock. The crash tests' restarted child saw the first;
|
|
945
|
+
// T-0627 saw the second in `endRun`. An async read is suspended between
|
|
946
|
+
// facts, so nothing may stay open there.
|
|
947
|
+
let rows: FactRow[];
|
|
906
948
|
try {
|
|
907
|
-
|
|
908
|
-
if (row.seq !== expectedSeq) {
|
|
909
|
-
throw new CorruptLogError(runId, `sequence ${row.seq} follows ${expectedSeq - 1}`);
|
|
910
|
-
}
|
|
911
|
-
expectedSeq += 1;
|
|
912
|
-
yield {
|
|
913
|
-
runId,
|
|
914
|
-
seq: row.seq,
|
|
915
|
-
kind: row.kind,
|
|
916
|
-
payload: JSON.parse(row.payload) as JsonValue,
|
|
917
|
-
cause: JSON.parse(row.cause) as FactCause,
|
|
918
|
-
time: row.time,
|
|
919
|
-
};
|
|
920
|
-
}
|
|
949
|
+
rows = statement.all(runId, after);
|
|
921
950
|
} finally {
|
|
922
951
|
statement.finalize();
|
|
923
952
|
}
|
|
953
|
+
let expectedSeq = after + 1;
|
|
954
|
+
for (const row of rows) {
|
|
955
|
+
if (row.seq !== expectedSeq) {
|
|
956
|
+
throw new CorruptLogError(runId, `sequence ${row.seq} follows ${expectedSeq - 1}`);
|
|
957
|
+
}
|
|
958
|
+
expectedSeq += 1;
|
|
959
|
+
yield {
|
|
960
|
+
runId,
|
|
961
|
+
seq: row.seq,
|
|
962
|
+
kind: row.kind,
|
|
963
|
+
payload: JSON.parse(row.payload) as JsonValue,
|
|
964
|
+
cause: JSON.parse(row.cause) as FactCause,
|
|
965
|
+
time: row.time,
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
#write<T>(work: () => T): T {
|
|
971
|
+
const writer = this.#immediateWriter?.(this.#database) ?? this.#database;
|
|
972
|
+
return writeImmediate(writer, work);
|
|
924
973
|
}
|
|
925
974
|
|
|
926
975
|
#assertOpen(): void {
|
|
@@ -999,13 +1048,6 @@ export function openSqliteLogBackend(
|
|
|
999
1048
|
path,
|
|
1000
1049
|
resolveSubscriptionWatch(options),
|
|
1001
1050
|
options.onSnapshot,
|
|
1051
|
+
options.immediateWriter,
|
|
1002
1052
|
);
|
|
1003
1053
|
}
|
|
1004
|
-
|
|
1005
|
-
export const sqliteBackend: LogBackendFactory = {
|
|
1006
|
-
name: "sqlite",
|
|
1007
|
-
open: (directory, options) =>
|
|
1008
|
-
openSqliteLogBackend(join(directory, SQLITE_FILE_NAME), options),
|
|
1009
|
-
};
|
|
1010
|
-
|
|
1011
|
-
export default sqliteBackend;
|
package/src/log/types.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** The public shapes of the append-only fact log. */
|
|
2
2
|
|
|
3
|
+
import type { Database } from "bun:sqlite";
|
|
4
|
+
import type { ImmediateWriter } from "../platform/sqlite-open.ts";
|
|
3
5
|
import type { FollowOptions } from "../platform/watch.ts";
|
|
4
6
|
|
|
5
7
|
/** A value that JSON can represent without losing anything. */
|
|
@@ -31,6 +33,12 @@ export interface Fact extends NewFact {
|
|
|
31
33
|
readonly time: number;
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
/** Which facts of a run to read. */
|
|
37
|
+
export interface ReadOptions {
|
|
38
|
+
/** The last sequence already seen; only facts with a greater sequence are read. */
|
|
39
|
+
readonly after?: number | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
34
42
|
/** A fact that belongs to no run, such as an event the kernel only observed. */
|
|
35
43
|
export interface GlobalFact extends NewFact {
|
|
36
44
|
readonly seq: number;
|
|
@@ -101,6 +109,8 @@ export interface RunsOptions {
|
|
|
101
109
|
readonly active?: boolean;
|
|
102
110
|
/** Only the runs of this object key, such as `task:42`. */
|
|
103
111
|
readonly object?: string;
|
|
112
|
+
/** Only the runs whose start input has this id. */
|
|
113
|
+
readonly startInputId?: string;
|
|
104
114
|
}
|
|
105
115
|
|
|
106
116
|
export interface LogBackendOptions extends FollowOptions {
|
|
@@ -133,6 +143,13 @@ export interface LogBackendOptions extends FollowOptions {
|
|
|
133
143
|
directory: string,
|
|
134
144
|
onChange: (filename: string | null) => void,
|
|
135
145
|
) => () => void;
|
|
146
|
+
/**
|
|
147
|
+
* Internal, for tests only; not part of the backend contract. Wraps the connection an immediate write goes through, so a test
|
|
148
|
+
* can act between reading `data_version` and taking the write lock.
|
|
149
|
+
*/
|
|
150
|
+
readonly immediateWriter?:
|
|
151
|
+
| ((database: Database) => ImmediateWriter)
|
|
152
|
+
| undefined;
|
|
136
153
|
}
|
|
137
154
|
|
|
138
155
|
/** A change to a run: a start, an end, or one appended batch. */
|
|
@@ -182,9 +199,11 @@ export interface LogBackend extends Partial<LogSubscription> {
|
|
|
182
199
|
expectedSeq: number,
|
|
183
200
|
facts: readonly NewFact[],
|
|
184
201
|
): Promise<AppendResult>;
|
|
185
|
-
/** Stream the facts of a run in sequence order. */
|
|
186
|
-
read(runId: string): AsyncIterable<Fact>;
|
|
187
|
-
/**
|
|
202
|
+
/** Stream the facts of a run in sequence order, optionally after a sequence. */
|
|
203
|
+
read(runId: string, options?: ReadOptions): AsyncIterable<Fact>;
|
|
204
|
+
/** One run by id, or undefined when the log does not know it. */
|
|
205
|
+
run(runId: string): Promise<RunInfo | undefined>;
|
|
206
|
+
/** List runs, optionally only the active ones or those of one object or start input. */
|
|
188
207
|
runs(options?: RunsOptions): Promise<RunInfo[]>;
|
|
189
208
|
/**
|
|
190
209
|
* Append one fact that belongs to no run. Global facts keep their order.
|
|
@@ -220,11 +239,18 @@ export interface LogBackend extends Partial<LogSubscription> {
|
|
|
220
239
|
): Promise<RecordOutcomeResult>;
|
|
221
240
|
/** Stream every recorded outcome in the order it was written. */
|
|
222
241
|
readOutcomes(): AsyncIterable<InputOutcome>;
|
|
242
|
+
/**
|
|
243
|
+
* The route outcomes that are still open candidates: the route value is
|
|
244
|
+
* `candidate` and no resolve outcome was written for its input. In sequence
|
|
245
|
+
* order, so the caller sees the same order as `readOutcomes`.
|
|
246
|
+
*/
|
|
247
|
+
openCandidateRoutes(): Promise<InputOutcome[]>;
|
|
248
|
+
/**
|
|
249
|
+
* The route outcomes that start (the route value is `started`, or it is
|
|
250
|
+
* `candidate` and its resolve value is `started`) and that no run names as
|
|
251
|
+
* its start input. In sequence order, so the caller sees the same order as
|
|
252
|
+
* `readOutcomes`.
|
|
253
|
+
*/
|
|
254
|
+
startRoutesWithoutRun(): Promise<InputOutcome[]>;
|
|
223
255
|
close(): Promise<void>;
|
|
224
256
|
}
|
|
225
|
-
|
|
226
|
-
/** A backend implementation, as a storage directory plus its name. */
|
|
227
|
-
export interface LogBackendFactory {
|
|
228
|
-
readonly name: string;
|
|
229
|
-
open(directory: string, options?: LogBackendOptions): LogBackend | Promise<LogBackend>;
|
|
230
|
-
}
|