@mostra/runtime 0.8.0 → 0.10.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/dist/bin.js +2879 -8
- package/dist/bin.js.map +1 -1
- package/dist/builder/child/main.js +2 -2
- package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
- package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
- package/dist/deps/child/install.js +1 -1
- package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
- package/dist/directories-BLJjeSPD.js.map +1 -0
- package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
- package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/{instance-BRfg27Rg.js → instance-FExcyENK.js} +3346 -395
- package/dist/instance-FExcyENK.js.map +1 -0
- package/package.json +7 -5
- package/src/actions/index.ts +163 -10
- package/src/bin.ts +78 -1
- package/src/gateway/actions.ts +20 -4
- package/src/gateway/browser.ts +118 -95
- package/src/gateway/capabilities.ts +15 -6
- package/src/gateway/index.ts +112 -15
- package/src/gateway/routes.ts +15 -7
- package/src/gc/index.ts +14 -0
- package/src/instance/index.ts +41 -2
- package/src/interaction/ephemeral.ts +32 -23
- package/src/interaction/events.ts +21 -1
- package/src/interaction/imports.ts +317 -0
- package/src/interaction/index.ts +172 -19
- package/src/interaction/outbox.ts +9 -0
- package/src/interaction/results.ts +22 -1
- package/src/interaction/runs.ts +52 -2
- package/src/interaction/submissions.ts +17 -1
- package/src/interaction/tally.ts +129 -0
- package/src/ledger/index.ts +104 -9
- package/src/ledger/migrations.ts +183 -0
- package/src/ledger/types.ts +8 -0
- package/src/link/binding.ts +289 -0
- package/src/link/hosted.ts +65 -0
- package/src/link/http.ts +136 -0
- package/src/link/index.ts +8 -0
- package/src/link/manager.ts +1385 -0
- package/src/link/relay.ts +512 -0
- package/src/live/index.ts +112 -2
- package/src/platform/filesystems.ts +78 -0
- package/src/platform/index.ts +1 -0
- package/src/protocol-server/index.ts +18 -1
- package/src/protocol-server/server.ts +38 -7
- package/src/protocol-server/websocket.ts +161 -0
- package/src/publish/bundle.ts +398 -0
- package/src/publish/manifest.ts +9 -0
- package/src/publish/methods.ts +12 -1
- package/src/publish/publisher.ts +127 -0
- package/src/registry/index.ts +35 -14
- package/src/server/api.ts +439 -0
- package/src/server/auth.ts +613 -0
- package/src/server/config.ts +221 -0
- package/src/server/data.ts +369 -0
- package/src/server/directory.ts +141 -0
- package/src/server/hosted.ts +822 -0
- package/src/server/index.ts +368 -0
- package/src/server/relay.ts +1163 -0
- package/src/share/index.ts +110 -6
- package/dist/directories-mJ1qlVht.js.map +0 -1
- package/dist/instance-BRfg27Rg.js.map +0 -1
package/src/ledger/index.ts
CHANGED
|
@@ -52,6 +52,17 @@ export interface OpenLedgerOptions {
|
|
|
52
52
|
readonly log: Log;
|
|
53
53
|
/** The migrations to apply. Default: this runtime's {@link MIGRATIONS}. */
|
|
54
54
|
readonly migrations?: readonly Migration[];
|
|
55
|
+
/**
|
|
56
|
+
* `exclusive` (the default) holds SQLite's exclusive lock for as long as the ledger is open:
|
|
57
|
+
* the lock decides who owns a local home (D81). `normal` is a hosted home's (D165 item 0b): the
|
|
58
|
+
* one writer is whoever holds the home's own lock ({@link holdLock}), and a backup replicator
|
|
59
|
+
* may read the ledger from its own process.
|
|
60
|
+
*/
|
|
61
|
+
readonly locking?: "exclusive" | "normal";
|
|
62
|
+
/** SQLite's `wal_autocheckpoint` for a `normal` ledger (a replicator may ask for 0). */
|
|
63
|
+
readonly walAutocheckpoint?: number;
|
|
64
|
+
/** The file's name in `storeDir`. Default: `ledger.sqlite`; a server's directory: `server.sqlite`. */
|
|
65
|
+
readonly fileName?: string;
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
/** The open ledger. Holding this object holds the home's lock. */
|
|
@@ -146,9 +157,9 @@ function unreadable(file: string, why: string): RpcError {
|
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
/** Builds a new ledger file at `file` without ever replacing an existing one (DATA_MODEL §6). */
|
|
149
|
-
function createLedgerFile(storeDir: string, file: string, log: Log): void {
|
|
160
|
+
function createLedgerFile(storeDir: string, file: string, log: Log, fileName = LEDGER_FILE): void {
|
|
150
161
|
const Driver = loadDriver();
|
|
151
|
-
const temp = join(storeDir, `${
|
|
162
|
+
const temp = join(storeDir, `${fileName}.${process.pid}.${Date.now()}.creating`);
|
|
152
163
|
const db = new Driver(temp);
|
|
153
164
|
try {
|
|
154
165
|
db.pragma("journal_mode = WAL");
|
|
@@ -181,7 +192,9 @@ function syncDirectory(dir: string): void {
|
|
|
181
192
|
*/
|
|
182
193
|
function removeAbandonedFiles(storeDir: string): void {
|
|
183
194
|
for (const name of readdirSync(storeDir)) {
|
|
184
|
-
const match = /^ledger\.sqlite\.(\d+)\.\d+\.creating(-wal|-shm|-journal)?$/.exec(
|
|
195
|
+
const match = /^(?:ledger|server)\.sqlite\.(\d+)\.\d+\.creating(-wal|-shm|-journal)?$/.exec(
|
|
196
|
+
name,
|
|
197
|
+
);
|
|
185
198
|
if (match && !processExists(Number(match[1]))) rmSync(join(storeDir, name), { force: true });
|
|
186
199
|
}
|
|
187
200
|
}
|
|
@@ -195,7 +208,8 @@ function processExists(pid: number): boolean {
|
|
|
195
208
|
}
|
|
196
209
|
}
|
|
197
210
|
|
|
198
|
-
|
|
211
|
+
/** A migration's queries over a connection; exported for the schema tests, which migrate in memory. */
|
|
212
|
+
export function queries(db: Database): MigrationDb {
|
|
199
213
|
const cache = new Map<string, BetterSqlite3.Statement>();
|
|
200
214
|
const prepare = (sql: string): BetterSqlite3.Statement => {
|
|
201
215
|
let statement = cache.get(sql);
|
|
@@ -219,6 +233,29 @@ function queries(db: Database): MigrationDb {
|
|
|
219
233
|
exec(sql: string): void {
|
|
220
234
|
db.exec(sql);
|
|
221
235
|
},
|
|
236
|
+
widenCheck(table: string, from: string, to: string): void {
|
|
237
|
+
const row = db
|
|
238
|
+
.prepare("SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?")
|
|
239
|
+
.get(table) as { sql: string } | undefined;
|
|
240
|
+
if (row?.sql.split(from).length !== 2) {
|
|
241
|
+
throw new Error(`widenCheck: ${table}'s definition doesn't hold ${from} exactly once.`);
|
|
242
|
+
}
|
|
243
|
+
const version = db.pragma("schema_version", { simple: true }) as number;
|
|
244
|
+
db.unsafeMode(true);
|
|
245
|
+
try {
|
|
246
|
+
db.pragma("writable_schema = ON");
|
|
247
|
+
db.prepare("UPDATE sqlite_schema SET sql = ? WHERE type = 'table' AND name = ?").run(
|
|
248
|
+
row.sql.replace(from, to),
|
|
249
|
+
table,
|
|
250
|
+
);
|
|
251
|
+
db.pragma(`schema_version = ${version + 1}`);
|
|
252
|
+
} finally {
|
|
253
|
+
db.pragma("writable_schema = OFF");
|
|
254
|
+
db.unsafeMode(false);
|
|
255
|
+
}
|
|
256
|
+
const problems = db.pragma("integrity_check", { simple: true });
|
|
257
|
+
if (problems !== "ok") throw new Error(`widenCheck: ${table}: ${String(problems)}`);
|
|
258
|
+
},
|
|
222
259
|
};
|
|
223
260
|
}
|
|
224
261
|
|
|
@@ -236,20 +273,28 @@ export function openLedger(options: OpenLedgerOptions): LedgerOpening {
|
|
|
236
273
|
const migrations = options.migrations ?? MIGRATIONS;
|
|
237
274
|
const latest = migrations.at(-1)?.version ?? 0;
|
|
238
275
|
mkdirSync(storeDir, { recursive: true, mode: 0o700 });
|
|
239
|
-
const
|
|
240
|
-
|
|
276
|
+
const fileName = options.fileName ?? LEDGER_FILE;
|
|
277
|
+
const file = join(storeDir, fileName);
|
|
278
|
+
if (!existsSync(file)) createLedgerFile(storeDir, file, log, fileName);
|
|
241
279
|
|
|
242
280
|
const Driver = loadDriver();
|
|
243
281
|
let db: Database;
|
|
244
282
|
try {
|
|
245
|
-
|
|
283
|
+
// A normal-locking ledger waits a moment for a replicator's short checkpoint locks.
|
|
284
|
+
db = new Driver(file, {
|
|
285
|
+
timeout: options.locking === "normal" ? 5_000 : 0,
|
|
286
|
+
fileMustExist: true,
|
|
287
|
+
});
|
|
246
288
|
} catch (error) {
|
|
247
289
|
throw diagnose(error, file) ?? error;
|
|
248
290
|
}
|
|
249
291
|
|
|
250
292
|
try {
|
|
251
293
|
// EXCLUSIVE before the first WAL access: no shared-memory index, and no other connection.
|
|
252
|
-
db.pragma("locking_mode = EXCLUSIVE");
|
|
294
|
+
if (options.locking !== "normal") db.pragma("locking_mode = EXCLUSIVE");
|
|
295
|
+
else if (options.walAutocheckpoint !== undefined) {
|
|
296
|
+
db.pragma(`wal_autocheckpoint = ${Math.max(0, Math.trunc(options.walAutocheckpoint))}`);
|
|
297
|
+
}
|
|
253
298
|
const mode = db.pragma("journal_mode = WAL", { simple: true });
|
|
254
299
|
if (mode !== "wal") throw unreadable(file, `its journal mode is ${String(mode)}, not WAL`);
|
|
255
300
|
db.pragma("synchronous = FULL");
|
|
@@ -316,7 +361,7 @@ function migrate(
|
|
|
316
361
|
`SQLite's integrity check failed (${problems.slice(0, 3).join("; ")})`,
|
|
317
362
|
);
|
|
318
363
|
}
|
|
319
|
-
backup = join(storeDir, `${LEDGER_FILE}.bak-${from}-${latest}`);
|
|
364
|
+
backup = join(storeDir, `${options.fileName ?? LEDGER_FILE}.bak-${from}-${latest}`);
|
|
320
365
|
rmSync(backup, { force: true });
|
|
321
366
|
// VACUUM INTO writes a consistent copy through SQLite, fsynced under synchronous=FULL;
|
|
322
367
|
// copying the file with fs would drop this process's lock (DATA_MODEL §1).
|
|
@@ -418,3 +463,53 @@ function wrap(
|
|
|
418
463
|
},
|
|
419
464
|
};
|
|
420
465
|
}
|
|
466
|
+
|
|
467
|
+
/** A lock held by {@link holdLock}. */
|
|
468
|
+
export interface HeldLock {
|
|
469
|
+
readonly file: string;
|
|
470
|
+
/** Releases it. Idempotent. */
|
|
471
|
+
release(): void;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Takes an advisory lock that the operating system releases when this process dies (DATA_MODEL
|
|
476
|
+
* §7.1, D165): `file` is a one-row SQLite database held in `locking_mode=EXCLUSIVE` with a write
|
|
477
|
+
* transaction open, so no other process can open it, and no stale PID file can outlive its
|
|
478
|
+
* owner. Needs no native addon beyond the ledger's own driver. Answers `undefined` when another
|
|
479
|
+
* process holds it.
|
|
480
|
+
*/
|
|
481
|
+
export function holdLock(file: string, owner: LedgerOwner): HeldLock | undefined {
|
|
482
|
+
const Driver = loadDriver();
|
|
483
|
+
const db = new Driver(file, { timeout: 0 });
|
|
484
|
+
try {
|
|
485
|
+
db.pragma("locking_mode = EXCLUSIVE");
|
|
486
|
+
db.pragma("journal_mode = DELETE");
|
|
487
|
+
db.prepare("BEGIN EXCLUSIVE").run();
|
|
488
|
+
db.exec(
|
|
489
|
+
"CREATE TABLE IF NOT EXISTS holder (id INTEGER PRIMARY KEY CHECK (id = 1), pid, start_time, at)",
|
|
490
|
+
);
|
|
491
|
+
db.prepare("INSERT OR REPLACE INTO holder (id, pid, start_time, at) VALUES (1, ?, ?, ?)").run(
|
|
492
|
+
owner.pid,
|
|
493
|
+
owner.startTime,
|
|
494
|
+
new Date().toISOString(),
|
|
495
|
+
);
|
|
496
|
+
// The transaction stays open: its lock is the lock.
|
|
497
|
+
} catch (error) {
|
|
498
|
+
db.close();
|
|
499
|
+
if (sqliteCode(error) === "SQLITE_BUSY") return undefined;
|
|
500
|
+
throw diagnose(error, file) ?? error;
|
|
501
|
+
}
|
|
502
|
+
let held = true;
|
|
503
|
+
return {
|
|
504
|
+
file,
|
|
505
|
+
release() {
|
|
506
|
+
if (!held) return;
|
|
507
|
+
held = false;
|
|
508
|
+
try {
|
|
509
|
+
db.prepare("ROLLBACK").run();
|
|
510
|
+
} finally {
|
|
511
|
+
db.close();
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
};
|
|
515
|
+
}
|
package/src/ledger/migrations.ts
CHANGED
|
@@ -654,4 +654,187 @@ export const MIGRATIONS: readonly Migration[] = [
|
|
|
654
654
|
`);
|
|
655
655
|
},
|
|
656
656
|
},
|
|
657
|
+
{
|
|
658
|
+
version: 12,
|
|
659
|
+
name: "tallies",
|
|
660
|
+
up(db) {
|
|
661
|
+
// D165 item 12, RM-07 (DATA_MODEL §7.4). A count by choice of a fixed-choice field, written
|
|
662
|
+
// only inside the submit transaction, one more for the choice the new submission holds. A
|
|
663
|
+
// count never goes down and a row is never deleted, so what viewers were shown stays true.
|
|
664
|
+
db.exec(`
|
|
665
|
+
CREATE TABLE tally (
|
|
666
|
+
artifact_id TEXT NOT NULL REFERENCES artifact(id),
|
|
667
|
+
field_id TEXT NOT NULL,
|
|
668
|
+
choice TEXT NOT NULL,
|
|
669
|
+
count INTEGER NOT NULL CHECK (count >= 0),
|
|
670
|
+
PRIMARY KEY (artifact_id, field_id, choice)
|
|
671
|
+
);
|
|
672
|
+
CREATE TRIGGER tally_only_grows BEFORE UPDATE ON tally
|
|
673
|
+
WHEN NEW.count < OLD.count OR NEW.artifact_id IS NOT OLD.artifact_id
|
|
674
|
+
OR NEW.field_id IS NOT OLD.field_id OR NEW.choice IS NOT OLD.choice
|
|
675
|
+
BEGIN
|
|
676
|
+
SELECT RAISE(ABORT, 'a tally only grows');
|
|
677
|
+
END;
|
|
678
|
+
CREATE TRIGGER tally_kept BEFORE DELETE ON tally
|
|
679
|
+
BEGIN
|
|
680
|
+
SELECT RAISE(ABORT, 'a tally is kept with its artifact');
|
|
681
|
+
END;
|
|
682
|
+
CREATE TRIGGER tally_not_replaced BEFORE INSERT ON tally
|
|
683
|
+
WHEN EXISTS (SELECT 1 FROM tally WHERE artifact_id = NEW.artifact_id
|
|
684
|
+
AND field_id = NEW.field_id AND choice = NEW.choice)
|
|
685
|
+
BEGIN
|
|
686
|
+
SELECT RAISE(ABORT, 'a tally row is counted, never replaced');
|
|
687
|
+
END;
|
|
688
|
+
`);
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
{
|
|
692
|
+
version: 13,
|
|
693
|
+
name: "remote server and owner link",
|
|
694
|
+
up(db) {
|
|
695
|
+
// D165 (DATA_MODEL §7.2, §7.3). A hosted home's incarnation, rotated in one transaction when
|
|
696
|
+
// the home is restored from a backup. A local home keeps the table empty.
|
|
697
|
+
db.exec(`
|
|
698
|
+
CREATE TABLE home_meta (
|
|
699
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
700
|
+
incarnation TEXT NOT NULL,
|
|
701
|
+
created_at TEXT NOT NULL
|
|
702
|
+
);
|
|
703
|
+
`);
|
|
704
|
+
// Hosted homes have viewers of kind `account` and `anonymous` (D165 item 8), and an owner
|
|
705
|
+
// instance imports its hosted submissions' viewers with their kinds (DATA_MODEL §7.3).
|
|
706
|
+
db.widenCheck(
|
|
707
|
+
"viewer",
|
|
708
|
+
"CHECK (kind IN ('local', 'tailnet', 'token', 'host'))",
|
|
709
|
+
"CHECK (kind IN ('local', 'tailnet', 'token', 'host', 'account', 'anonymous'))",
|
|
710
|
+
);
|
|
711
|
+
// A relayed run whose end its owner never reported is `unknown` (D165 item 4): a state of
|
|
712
|
+
// its own, never final, replaced once by the end the owner reports, never by anything else.
|
|
713
|
+
db.widenCheck(
|
|
714
|
+
"action_run",
|
|
715
|
+
"CHECK (status IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled'))",
|
|
716
|
+
"CHECK (status IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled', 'unknown'))",
|
|
717
|
+
);
|
|
718
|
+
// A hosted shell's session travels in a header (`X-Mostra-Shell`, D165 item 8): no cookie
|
|
719
|
+
// is ever set or read on a hosted host.
|
|
720
|
+
db.widenCheck(
|
|
721
|
+
"session",
|
|
722
|
+
"CHECK (mode IN ('cookie', 'worker'))",
|
|
723
|
+
"CHECK (mode IN ('cookie', 'worker', 'header'))",
|
|
724
|
+
);
|
|
725
|
+
// A share to a remote server (`via = 'remote'`, D165): its link is a token of the hosted
|
|
726
|
+
// home, never of this ledger, so the share names it without a foreign key; a trigger keeps
|
|
727
|
+
// every other share's token one of this home's.
|
|
728
|
+
db.widenCheck(
|
|
729
|
+
"share",
|
|
730
|
+
"via TEXT NOT NULL CHECK (via IN ('tailnet', 'funnel', 'cloudflare'))",
|
|
731
|
+
"via TEXT NOT NULL CHECK (via IN ('tailnet', 'funnel', 'cloudflare', 'remote'))",
|
|
732
|
+
);
|
|
733
|
+
db.widenCheck(
|
|
734
|
+
"share",
|
|
735
|
+
"token_id TEXT NOT NULL REFERENCES token(id)",
|
|
736
|
+
"token_id TEXT NOT NULL",
|
|
737
|
+
);
|
|
738
|
+
// A hosted home's floor for each owner link's relay sequences: the highest relay mark its
|
|
739
|
+
// owner reported, so after a restore the next sequence continues above it and the mark is
|
|
740
|
+
// compared with what the home knows, not only its runs (Opus review of WP-29, 9).
|
|
741
|
+
db.exec(`
|
|
742
|
+
CREATE TABLE relay_floor (
|
|
743
|
+
link_id TEXT PRIMARY KEY,
|
|
744
|
+
floor INTEGER NOT NULL CHECK (floor >= 0)
|
|
745
|
+
);
|
|
746
|
+
`);
|
|
747
|
+
// A followed submission this home doesn't hold, read again until it is imported (Opus review
|
|
748
|
+
// of WP-29, 3): written in the transaction that moves the sync cursor past it.
|
|
749
|
+
db.exec(`
|
|
750
|
+
CREATE TABLE remote_missing (
|
|
751
|
+
server TEXT NOT NULL REFERENCES remote_link(server),
|
|
752
|
+
submission_id TEXT NOT NULL,
|
|
753
|
+
noted_at TEXT NOT NULL,
|
|
754
|
+
PRIMARY KEY (server, submission_id)
|
|
755
|
+
);
|
|
756
|
+
`);
|
|
757
|
+
db.exec(`
|
|
758
|
+
CREATE TRIGGER share_local_token BEFORE INSERT ON share
|
|
759
|
+
WHEN NEW.via != 'remote' AND NOT EXISTS (SELECT 1 FROM token WHERE id = NEW.token_id)
|
|
760
|
+
BEGIN
|
|
761
|
+
SELECT RAISE(ABORT, 'a share through a tunnel names one of this home''s tokens');
|
|
762
|
+
END;
|
|
763
|
+
CREATE TRIGGER action_run_unknown_once BEFORE UPDATE ON action_run
|
|
764
|
+
WHEN OLD.status = 'unknown' AND NEW.status NOT IN ('unknown', 'succeeded', 'failed', 'cancelled')
|
|
765
|
+
BEGIN
|
|
766
|
+
SELECT RAISE(ABORT, 'an unknown run is replaced only by the end its owner reports');
|
|
767
|
+
END;
|
|
768
|
+
`);
|
|
769
|
+
// The owner's side (DATA_MODEL §7.3): its link to each server, its hosted artifacts, the
|
|
770
|
+
// publishes waiting to reach a server, and the relayed runs it accepted. Imported submissions
|
|
771
|
+
// and followed events carry where they came from, and a followed event is appended once.
|
|
772
|
+
db.exec(`
|
|
773
|
+
CREATE TABLE remote_link (
|
|
774
|
+
server TEXT PRIMARY KEY,
|
|
775
|
+
account_id TEXT,
|
|
776
|
+
account_name TEXT,
|
|
777
|
+
instance_id TEXT,
|
|
778
|
+
is_default INTEGER NOT NULL DEFAULT 0 CHECK (is_default IN (0, 1)),
|
|
779
|
+
linked_at TEXT NOT NULL,
|
|
780
|
+
state TEXT NOT NULL CHECK (state IN ('signed-in', 'signed-out')),
|
|
781
|
+
incarnation TEXT,
|
|
782
|
+
follow_cursor TEXT,
|
|
783
|
+
relay_mark INTEGER NOT NULL DEFAULT 0 CHECK (relay_mark >= 0)
|
|
784
|
+
);
|
|
785
|
+
CREATE TABLE hosted_artifact (
|
|
786
|
+
artifact_id TEXT NOT NULL REFERENCES artifact(id),
|
|
787
|
+
server TEXT NOT NULL REFERENCES remote_link(server),
|
|
788
|
+
hosted_home TEXT NOT NULL,
|
|
789
|
+
route_id TEXT NOT NULL,
|
|
790
|
+
label TEXT NOT NULL,
|
|
791
|
+
token_id TEXT,
|
|
792
|
+
url_secret_hint TEXT,
|
|
793
|
+
shared_at TEXT NOT NULL,
|
|
794
|
+
stopped_at TEXT,
|
|
795
|
+
PRIMARY KEY (artifact_id, server)
|
|
796
|
+
);
|
|
797
|
+
CREATE TABLE version_push (
|
|
798
|
+
artifact_id TEXT NOT NULL REFERENCES artifact(id),
|
|
799
|
+
version_id TEXT NOT NULL REFERENCES version(id),
|
|
800
|
+
server TEXT NOT NULL REFERENCES remote_link(server),
|
|
801
|
+
state TEXT NOT NULL CHECK (state IN ('pending', 'complete', 'refused')),
|
|
802
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
803
|
+
next_attempt_at TEXT,
|
|
804
|
+
created_at TEXT NOT NULL,
|
|
805
|
+
-- Why the server refused the push for good (Opus review of WP-29, 10): it waits no
|
|
806
|
+
-- longer, and no later push waits behind it.
|
|
807
|
+
refusal TEXT,
|
|
808
|
+
PRIMARY KEY (artifact_id, version_id, server)
|
|
809
|
+
);
|
|
810
|
+
CREATE TABLE relayed_run (
|
|
811
|
+
run_id TEXT PRIMARY KEY REFERENCES action_run(id),
|
|
812
|
+
server TEXT NOT NULL,
|
|
813
|
+
relay_seq INTEGER NOT NULL,
|
|
814
|
+
artifact_id TEXT NOT NULL,
|
|
815
|
+
version_id TEXT NOT NULL,
|
|
816
|
+
fn_id TEXT NOT NULL,
|
|
817
|
+
seq_sent INTEGER NOT NULL DEFAULT 0,
|
|
818
|
+
started_at TEXT NOT NULL,
|
|
819
|
+
finished_at TEXT
|
|
820
|
+
);
|
|
821
|
+
CREATE TABLE relay_run (
|
|
822
|
+
run_id TEXT PRIMARY KEY REFERENCES action_run(id),
|
|
823
|
+
owner_link_id TEXT NOT NULL,
|
|
824
|
+
relay_seq INTEGER NOT NULL,
|
|
825
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
826
|
+
sent_at TEXT,
|
|
827
|
+
accepted_at TEXT,
|
|
828
|
+
owner_heard_at TEXT,
|
|
829
|
+
offline_limit_at TEXT NOT NULL,
|
|
830
|
+
last_seq INTEGER NOT NULL DEFAULT 0,
|
|
831
|
+
cancel_requested_at TEXT,
|
|
832
|
+
UNIQUE (owner_link_id, relay_seq)
|
|
833
|
+
);
|
|
834
|
+
ALTER TABLE submission ADD COLUMN origin_json TEXT;
|
|
835
|
+
ALTER TABLE event ADD COLUMN origin_json TEXT;
|
|
836
|
+
CREATE UNIQUE INDEX event_origin ON event (origin_json) WHERE origin_json IS NOT NULL;
|
|
837
|
+
`);
|
|
838
|
+
},
|
|
839
|
+
},
|
|
657
840
|
];
|
package/src/ledger/types.ts
CHANGED
|
@@ -25,4 +25,12 @@ export interface LedgerDb {
|
|
|
25
25
|
export interface MigrationDb extends LedgerDb {
|
|
26
26
|
/** Runs one or more statements without parameters. */
|
|
27
27
|
exec(sql: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Widens a table's CHECK constraint in place: replaces `from`, which must occur exactly once in
|
|
30
|
+
* the table's definition, with `to`. SQLite can't alter a CHECK, and a table other tables
|
|
31
|
+
* reference can't be rebuilt inside the migration transaction while foreign keys hold, so this
|
|
32
|
+
* edits the schema text, which SQLite documents for changes that touch no stored row (allowing
|
|
33
|
+
* more values does not); `to` must only allow more than `from` did.
|
|
34
|
+
*/
|
|
35
|
+
widenCheck(table: string, from: string, to: string): void;
|
|
28
36
|
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner's side of the network binding (INTERFACES §18.2, D165 item 2): one WebSocket to a
|
|
3
|
+
* remote server's `/host`, dialled out (so it works behind NAT), opened with the access token and
|
|
4
|
+
* the subprotocol `mostra.host`, carrying the host protocol as one JSON-RPC message per text frame.
|
|
5
|
+
* It keeps the heartbeat (a ping and `mostra/ping` every 20 s; dead after 45 s of silence) and
|
|
6
|
+
* dials again with backoff (1 s doubling to 60 s, with jitter) whenever the connection ends, so an
|
|
7
|
+
* edge or server restart is an ordinary reconnect. Each connection opens its session afresh:
|
|
8
|
+
* `mostra/initialize` with the owner link, then the ports registered again.
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
HOST_PROTOCOL_REVISIONS,
|
|
12
|
+
type JsonRpcParams,
|
|
13
|
+
type MethodDefinition,
|
|
14
|
+
type MostraInitializeParams,
|
|
15
|
+
type MostraInitializeResult,
|
|
16
|
+
mostraInitializeMethod,
|
|
17
|
+
RpcPeer,
|
|
18
|
+
} from "@mostra/protocol";
|
|
19
|
+
import { WebSocket } from "ws";
|
|
20
|
+
import type { Log } from "../log.ts";
|
|
21
|
+
import {
|
|
22
|
+
BINDING_SUBPROTOCOL,
|
|
23
|
+
type HeartbeatOptions,
|
|
24
|
+
keepAlive,
|
|
25
|
+
MAX_FRAME_BYTES,
|
|
26
|
+
readFrame,
|
|
27
|
+
} from "../protocol-server/index.ts";
|
|
28
|
+
|
|
29
|
+
/** The first redial's delay; each later one doubles, up to {@link MAX_REDIAL_MS}. */
|
|
30
|
+
export const FIRST_REDIAL_MS = 1_000;
|
|
31
|
+
/** The longest delay between dials (§18.2). */
|
|
32
|
+
export const MAX_REDIAL_MS = 60_000;
|
|
33
|
+
|
|
34
|
+
/** The delay before dial number `attempt` (1 for the first redial), with up to 25% jitter. */
|
|
35
|
+
export function redialMs(attempt: number, random: () => number = Math.random): number {
|
|
36
|
+
const base = Math.min(MAX_REDIAL_MS, FIRST_REDIAL_MS * 2 ** Math.max(0, attempt - 1));
|
|
37
|
+
return Math.round(base * (0.75 + random() * 0.25));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a binding needs for each connection it opens. */
|
|
41
|
+
export interface BindingOptions {
|
|
42
|
+
/** The server's API origin; the binding dials `<origin>/host` (`ws:` for `http:`, `wss:` for `https:`). */
|
|
43
|
+
readonly server: string;
|
|
44
|
+
/** A fresh access token for each dial (it may refresh it first). */
|
|
45
|
+
readonly accessToken: () => Promise<string>;
|
|
46
|
+
/** The `mostra/initialize` params for each session (the owner link with its marks, the ports). */
|
|
47
|
+
readonly initialize: () => MostraInitializeParams;
|
|
48
|
+
/** Requests the hosted home sends this instance: its port calls. */
|
|
49
|
+
readonly onRequest: (method: string, params: unknown) => unknown;
|
|
50
|
+
/**
|
|
51
|
+
* Notifications the hosted home sends, with the session they came on: one can arrive while the
|
|
52
|
+
* session's own start (`onOpen`) is still under way, before {@link Binding.session} names it
|
|
53
|
+
* (Opus review of WP-29, 2).
|
|
54
|
+
*/
|
|
55
|
+
readonly onNotification?: (method: string, params: unknown, session: BindingSession) => void;
|
|
56
|
+
/** Runs once a session is open, before anything else is sent on it. */
|
|
57
|
+
readonly onOpen?: (session: BindingSession) => void | Promise<void>;
|
|
58
|
+
/** Runs when a connection ends, with why. */
|
|
59
|
+
readonly onClose?: (reason: string) => void;
|
|
60
|
+
/** Told when a dial is refused for its token (401): the caller signs in again or refreshes. */
|
|
61
|
+
readonly onUnauthorized?: () => void;
|
|
62
|
+
readonly log: Log;
|
|
63
|
+
readonly heartbeat?: HeartbeatOptions;
|
|
64
|
+
/** The `Host` to send, for tests that dial a loopback address under a name. */
|
|
65
|
+
readonly connectTo?: { readonly host: string; readonly port?: number | undefined };
|
|
66
|
+
/**
|
|
67
|
+
* Sees every message the binding sends and receives, as its text (tests: the relay's secrets
|
|
68
|
+
* grep, `MOSTRA_TEST_BINDING_TRACE`).
|
|
69
|
+
*/
|
|
70
|
+
readonly trace?: (direction: "out" | "in", text: string) => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One open session on a binding. */
|
|
74
|
+
export interface BindingSession {
|
|
75
|
+
readonly result: MostraInitializeResult;
|
|
76
|
+
call<P, R>(
|
|
77
|
+
definition: MethodDefinition<P, R>,
|
|
78
|
+
params: P,
|
|
79
|
+
options?: { timeoutMs?: number },
|
|
80
|
+
): Promise<R>;
|
|
81
|
+
notify(method: string, params?: JsonRpcParams): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A binding that keeps itself connected until it is stopped. */
|
|
85
|
+
export interface Binding {
|
|
86
|
+
/** The session open now, if any. */
|
|
87
|
+
readonly session: BindingSession | undefined;
|
|
88
|
+
/** Settles with the next open session (now, if one is open). */
|
|
89
|
+
ready(): Promise<BindingSession>;
|
|
90
|
+
/** Drops the current connection; the binding dials again (tests, a token change). */
|
|
91
|
+
reconnect(): void;
|
|
92
|
+
stop(): Promise<void>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Starts a binding: it dials at once and keeps dialling until stopped. */
|
|
96
|
+
export function startBinding(options: BindingOptions): Binding {
|
|
97
|
+
const { log } = options;
|
|
98
|
+
let stopped = false;
|
|
99
|
+
let current: { socket: WebSocket; peer: RpcPeer } | undefined;
|
|
100
|
+
let session: BindingSession | undefined;
|
|
101
|
+
let attempt = 0;
|
|
102
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
103
|
+
let waiters: ((session: BindingSession) => void)[] = [];
|
|
104
|
+
|
|
105
|
+
const url = (): string => {
|
|
106
|
+
const origin = new URL(options.server);
|
|
107
|
+
const scheme = origin.protocol === "https:" ? "wss:" : "ws:";
|
|
108
|
+
if (options.connectTo) {
|
|
109
|
+
const port =
|
|
110
|
+
options.connectTo.port ?? (Number(origin.port) || (scheme === "wss:" ? 443 : 80));
|
|
111
|
+
return `${scheme}//${options.connectTo.host}:${port}/host`;
|
|
112
|
+
}
|
|
113
|
+
return `${scheme}//${origin.host}/host`;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const schedule = (): void => {
|
|
117
|
+
if (stopped || timer) return;
|
|
118
|
+
attempt += 1;
|
|
119
|
+
const delay = redialMs(attempt);
|
|
120
|
+
timer = setTimeout(() => {
|
|
121
|
+
timer = undefined;
|
|
122
|
+
void dial();
|
|
123
|
+
}, delay);
|
|
124
|
+
timer.unref?.();
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const dial = async (): Promise<void> => {
|
|
128
|
+
if (stopped) return;
|
|
129
|
+
let token: string;
|
|
130
|
+
try {
|
|
131
|
+
token = await options.accessToken();
|
|
132
|
+
} catch (error) {
|
|
133
|
+
log.warn("link.token_failed", { server: options.server, error: (error as Error).message });
|
|
134
|
+
options.onClose?.((error as Error).message);
|
|
135
|
+
schedule();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (stopped) return;
|
|
139
|
+
const host = new URL(options.server).host;
|
|
140
|
+
const socket = new WebSocket(url(), BINDING_SUBPROTOCOL, {
|
|
141
|
+
headers: { authorization: `Bearer ${token}`, ...(options.connectTo ? { host } : {}) },
|
|
142
|
+
maxPayload: MAX_FRAME_BYTES,
|
|
143
|
+
perMessageDeflate: false,
|
|
144
|
+
handshakeTimeout: 20_000,
|
|
145
|
+
});
|
|
146
|
+
let opened = false;
|
|
147
|
+
/** This connection's session, from its `mostra/initialize` answer on. */
|
|
148
|
+
let connectionSession: BindingSession | undefined;
|
|
149
|
+
const peer = new RpcPeer({
|
|
150
|
+
send: (message) => {
|
|
151
|
+
if (socket.readyState !== socket.OPEN) return;
|
|
152
|
+
const text = JSON.stringify(message);
|
|
153
|
+
options.trace?.("out", text);
|
|
154
|
+
socket.send(text);
|
|
155
|
+
},
|
|
156
|
+
onRequest: (method, params) => options.onRequest(method, params),
|
|
157
|
+
onNotification: (method, params) => {
|
|
158
|
+
if (method === "mostra/ping") return;
|
|
159
|
+
if (!connectionSession) {
|
|
160
|
+
log.warn("link.notification_before_session", { server: options.server, method });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
options.onNotification?.(method, params, connectionSession);
|
|
164
|
+
},
|
|
165
|
+
onInternalError: (error, method) => log.error("link.internal_error", { method, error }),
|
|
166
|
+
});
|
|
167
|
+
current = { socket, peer };
|
|
168
|
+
socket.on("unexpected-response", (_request, response) => {
|
|
169
|
+
const status = response.statusCode ?? 0;
|
|
170
|
+
log.warn("link.refused", { server: options.server, status });
|
|
171
|
+
if (status === 401) options.onUnauthorized?.();
|
|
172
|
+
socket.terminate();
|
|
173
|
+
});
|
|
174
|
+
socket.on("message", (data, isBinary) => {
|
|
175
|
+
const text = readFrame(socket, data as Buffer, isBinary, log);
|
|
176
|
+
if (text === undefined) return;
|
|
177
|
+
options.trace?.("in", text);
|
|
178
|
+
peer.receiveLine(text);
|
|
179
|
+
});
|
|
180
|
+
socket.on("error", (error) =>
|
|
181
|
+
log.warn("link.socket_error", { server: options.server, error: error.message }),
|
|
182
|
+
);
|
|
183
|
+
socket.once("close", (code, reason) => {
|
|
184
|
+
peer.close();
|
|
185
|
+
if (current?.socket === socket) current = undefined;
|
|
186
|
+
session = undefined;
|
|
187
|
+
const why = `closed (${code}${reason.length > 0 ? `: ${reason.toString()}` : ""})`;
|
|
188
|
+
if (opened) log.info("link.disconnected", { server: options.server, code });
|
|
189
|
+
options.onClose?.(why);
|
|
190
|
+
if (code === 4401 || code === 4403) options.onUnauthorized?.();
|
|
191
|
+
schedule();
|
|
192
|
+
});
|
|
193
|
+
socket.once("open", () => {
|
|
194
|
+
opened = true;
|
|
195
|
+
keepAlive(socket, options.heartbeat);
|
|
196
|
+
void (async () => {
|
|
197
|
+
try {
|
|
198
|
+
const result = await peer.call(mostraInitializeMethod, options.initialize(), {
|
|
199
|
+
timeoutMs: 30_000,
|
|
200
|
+
});
|
|
201
|
+
const open: BindingSession = {
|
|
202
|
+
result,
|
|
203
|
+
call: (definition, params, callOptions) => peer.call(definition, params, callOptions),
|
|
204
|
+
notify: (method, params) => peer.notify(method, params),
|
|
205
|
+
};
|
|
206
|
+
connectionSession = open;
|
|
207
|
+
attempt = 0;
|
|
208
|
+
await options.onOpen?.(open);
|
|
209
|
+
if (socket.readyState !== socket.OPEN) return;
|
|
210
|
+
session = open;
|
|
211
|
+
log.info("link.connected", { server: options.server });
|
|
212
|
+
const ready = waiters;
|
|
213
|
+
waiters = [];
|
|
214
|
+
for (const resolve of ready) resolve(open);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
log.warn("link.initialize_failed", {
|
|
217
|
+
server: options.server,
|
|
218
|
+
error: (error as Error).message,
|
|
219
|
+
});
|
|
220
|
+
options.onClose?.((error as Error).message);
|
|
221
|
+
socket.close(1008, "initialize failed");
|
|
222
|
+
}
|
|
223
|
+
})();
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
void dial();
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
get session() {
|
|
231
|
+
return session;
|
|
232
|
+
},
|
|
233
|
+
ready() {
|
|
234
|
+
if (session) return Promise.resolve(session);
|
|
235
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
236
|
+
},
|
|
237
|
+
reconnect() {
|
|
238
|
+
current?.socket.terminate();
|
|
239
|
+
},
|
|
240
|
+
async stop() {
|
|
241
|
+
stopped = true;
|
|
242
|
+
if (timer) clearTimeout(timer);
|
|
243
|
+
const open = current;
|
|
244
|
+
current = undefined;
|
|
245
|
+
if (open) {
|
|
246
|
+
await new Promise<void>((resolve) => {
|
|
247
|
+
if (open.socket.readyState === open.socket.CLOSED) {
|
|
248
|
+
resolve();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
open.socket.once("close", () => resolve());
|
|
252
|
+
open.socket.close(1000);
|
|
253
|
+
setTimeout(() => {
|
|
254
|
+
open.socket.terminate();
|
|
255
|
+
resolve();
|
|
256
|
+
}, 2_000).unref();
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The `mostra/initialize` params an owner instance sends on its binding (§18.2). */
|
|
264
|
+
export function ownerInitialize(input: {
|
|
265
|
+
readonly name: string;
|
|
266
|
+
readonly version: string;
|
|
267
|
+
readonly instanceId: string;
|
|
268
|
+
readonly relayMark: number;
|
|
269
|
+
readonly followed?: { readonly incarnation: string; readonly cursor: string };
|
|
270
|
+
}): MostraInitializeParams {
|
|
271
|
+
return {
|
|
272
|
+
client: { name: input.name, version: input.version },
|
|
273
|
+
revisions: [...HOST_PROTOCOL_REVISIONS],
|
|
274
|
+
capabilities: { notifications: ["events/notify"] },
|
|
275
|
+
ports: ["submissions", "relay"],
|
|
276
|
+
link: {
|
|
277
|
+
instanceId: input.instanceId,
|
|
278
|
+
relayMark: input.relayMark,
|
|
279
|
+
...(input.followed
|
|
280
|
+
? {
|
|
281
|
+
followed: {
|
|
282
|
+
incarnation: input.followed.incarnation,
|
|
283
|
+
cursor: input.followed.cursor as never,
|
|
284
|
+
},
|
|
285
|
+
}
|
|
286
|
+
: {}),
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
}
|