@mostra/runtime 0.9.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.
Files changed (65) hide show
  1. package/dist/bin.js +2879 -8
  2. package/dist/bin.js.map +1 -1
  3. package/dist/builder/child/main.js +2 -2
  4. package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
  5. package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
  6. package/dist/deps/child/install.js +1 -1
  7. package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
  8. package/dist/directories-BLJjeSPD.js.map +1 -0
  9. package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
  10. package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -2
  14. package/dist/{instance-EnaZPs14.js → instance-FExcyENK.js} +3341 -396
  15. package/dist/instance-FExcyENK.js.map +1 -0
  16. package/package.json +7 -5
  17. package/src/actions/index.ts +163 -10
  18. package/src/bin.ts +78 -1
  19. package/src/gateway/actions.ts +20 -4
  20. package/src/gateway/browser.ts +118 -95
  21. package/src/gateway/capabilities.ts +15 -6
  22. package/src/gateway/index.ts +112 -15
  23. package/src/gateway/routes.ts +15 -7
  24. package/src/gc/index.ts +14 -0
  25. package/src/instance/index.ts +41 -2
  26. package/src/interaction/ephemeral.ts +32 -23
  27. package/src/interaction/events.ts +21 -1
  28. package/src/interaction/imports.ts +317 -0
  29. package/src/interaction/index.ts +172 -19
  30. package/src/interaction/outbox.ts +9 -0
  31. package/src/interaction/results.ts +8 -1
  32. package/src/interaction/runs.ts +52 -2
  33. package/src/interaction/submissions.ts +17 -1
  34. package/src/interaction/tally.ts +129 -0
  35. package/src/ledger/index.ts +104 -9
  36. package/src/ledger/migrations.ts +183 -0
  37. package/src/ledger/types.ts +8 -0
  38. package/src/link/binding.ts +289 -0
  39. package/src/link/hosted.ts +65 -0
  40. package/src/link/http.ts +136 -0
  41. package/src/link/index.ts +8 -0
  42. package/src/link/manager.ts +1385 -0
  43. package/src/link/relay.ts +512 -0
  44. package/src/live/index.ts +112 -2
  45. package/src/platform/filesystems.ts +78 -0
  46. package/src/platform/index.ts +1 -0
  47. package/src/protocol-server/index.ts +18 -1
  48. package/src/protocol-server/server.ts +38 -7
  49. package/src/protocol-server/websocket.ts +161 -0
  50. package/src/publish/bundle.ts +398 -0
  51. package/src/publish/manifest.ts +9 -0
  52. package/src/publish/methods.ts +12 -1
  53. package/src/publish/publisher.ts +127 -0
  54. package/src/registry/index.ts +35 -14
  55. package/src/server/api.ts +439 -0
  56. package/src/server/auth.ts +613 -0
  57. package/src/server/config.ts +221 -0
  58. package/src/server/data.ts +369 -0
  59. package/src/server/directory.ts +141 -0
  60. package/src/server/hosted.ts +822 -0
  61. package/src/server/index.ts +368 -0
  62. package/src/server/relay.ts +1163 -0
  63. package/src/share/index.ts +110 -6
  64. package/dist/directories-mJ1qlVht.js.map +0 -1
  65. package/dist/instance-EnaZPs14.js.map +0 -1
@@ -1,27 +1,29 @@
1
- import { C as STAGING_FILES, S as LIVE_DECLARATION, _ as DEV_METHOD, a as nameKey, c as removeTree, d as replaceDirectoryLink, f as samePath, g as DEV_CHANGED_METHOD, h as BUILD_METHOD, i as linkTarget, l as renamePath, m as sealTree, o as pathKey, p as sealPlaced, r as linkDirectory, s as realPath, t as fsyncPath, u as renamePathSync, v as DEV_RELOAD_METHOD, y as DEV_SECRET_HEADER } from "./files-FygSJIzK.js";
2
- import { a as pathsOf, c as currentUserSid, d as runtimeRoot$1, f as serviceLauncherPath, i as ownedDirectory, l as helperPath, n as WINDOWS, o as runtimeDirFor, p as stopServiceLauncher, r as defaultHome, s as socketPathLimit, t as OS, u as runHelperAsync } from "./directories-mJ1qlVht.js";
1
+ import { C as STAGING_FILES, S as LIVE_DECLARATION, _ as DEV_METHOD, a as nameKey, c as removeTree, d as replaceDirectoryLink, f as samePath, g as DEV_CHANGED_METHOD, h as BUILD_METHOD, i as linkTarget, l as renamePath, m as sealTree, o as pathKey, p as sealPlaced, r as linkDirectory, s as realPath, t as fsyncPath, u as renamePathSync, v as DEV_RELOAD_METHOD, y as DEV_SECRET_HEADER } from "./files-DGeWuE4I.js";
2
+ import { a as pathsOf, c as currentUserSid, d as runtimeRoot$1, f as serviceLauncherPath, i as ownedDirectory, l as helperPath, n as WINDOWS, o as runtimeDirFor, p as stopServiceLauncher, r as defaultHome, s as socketPathLimit, t as OS, u as runHelperAsync } from "./directories-BLJjeSPD.js";
3
3
  import { builtinModules, createRequire } from "node:module";
4
4
  import { accessSync, appendFileSync, chmodSync, closeSync, constants, createReadStream, createWriteStream, existsSync, fsyncSync, linkSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, watch, writeFileSync, writeSync } from "node:fs";
5
5
  import { basename, delimiter, dirname, extname, join, relative, resolve, sep, win32 } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
- import { ACTION_ARGS_MAX, ACTION_RESULT_MAX, ALL_SCOPES, ARTIFACT_MANIFEST_KEYS, ARTIFACT_PROGRESS, ArtifactId, ArtifactManifest, BROWSER_PROTOCOL_REVISION, BROWSER_PROTOCOL_REVISIONS, BUNDLE_NAME, CLI_CLIENT_NAME, CallRequest, ChooseRequest, DEFAULT_WORKER_LIMITS, DEV_NOTIFY, DeliveryId, EVENTS_NOTIFY, EnterRequest, FRAME_SESSION_HEADER, FieldsRequest, FollowRequest, HOST_PROTOCOL_REVISIONS, LIVE_RING_DEFAULT, LIVE_RING_MAX, LIVE_VALUE_MAX, LineSplitter, LiveRequest, MAX_LINE_LENGTH, MutationId, PUBLIC_SHARE_DEFAULT_TTL, PUBLIC_SHARE_MAX_TTL_MS, RUN_LOG_MAX, RUN_LOG_TRUNCATED, RpcError, RpcPeer, RunAttachRequest, RunCancelRequest, RunId, RunRequest, SECRET_NAME_PATTERN, SECRET_VALUE_MAX, SIGNAL_NAME_PATTERN, SIGNAL_PAYLOAD_MAX_BYTES, ServerEmitParams, ServerLiveValueParams, ServerSecretParams, ServerSignalParams, ServerStateGetParams, ServerStatePatchParams, ShareId, ShellSubmitRequest, SignalRequest, StreamTicketRequest, SubmissionId, SubmissionPolicy, SubmitRequest, TokenId, VERIFY_RUNNER_METHODS, VerifyVerdict, VersionId, ViewId, ViewerId, WORKER_METHODS, WorkerBatchParams, WorkerCallOutcome, WorkerPingResult, WorkerStartResult, ariaSummary, artifactCreateMethod, artifactGetMethod, artifactListMethod, artifactPublishMethod, artifactVerifyMethod, canonicalJson, checkArgumentFrames, checkRequest, createDiagnostic, depsAddMethod, devStartMethod, devStopMethod, durationMs, encodeLine, estimateImageTokens, eventsAckMethod, eventsReadMethod, eventsSubscribeMethod, eventsUnsubscribeMethod, fromBase64, gcRunMethod, instanceCheckMethod, instanceStatusMethod, isFieldId, isFinalRunStatus, livePushMethod, mostraInitializeMethod, mostraShutdownMethod, negotiateRevision, normalizeScopes, parseParams, portAddressingExternalBaseMethod, portDraftsMirrorMethod, portIdentityResolveMethod, portSecretsGetMethod, portSubmissionsDeliverMethod, portTrustedControlsActivatedMethod, portTrustedControlsPolicyMethod, refusal, resultsGetMethod, safeEqual, safePath, scopeAllows, secretDeleteMethod, secretListMethod, secretSetMethod, serializeAll, shareListMethod, shareStartMethod, shareStopMethod, stateGetMethod, statePatchMethod, toBase64, viewDefaultMethod, viewLaunchMethod, viewRevokeMethod, viewRotateMethod, viewUrlMethod, viewportsFor, waitMethod } from "@mostra/protocol";
7
+ import { ACTION_ARGS_MAX, ACTION_RESULT_MAX, ALL_SCOPES, ARTIFACT_MANIFEST_KEYS, ARTIFACT_PROGRESS, ArtifactId, ArtifactManifest, BROWSER_PROTOCOL_REVISION, BROWSER_PROTOCOL_REVISIONS, BUNDLE_NAME, CLI_CLIENT_NAME, CallRequest, ChooseRequest, DEFAULT_WORKER_LIMITS, DEV_NOTIFY, DeliveryId, EVENTS_NOTIFY, EnterRequest, FRAME_SESSION_HEADER, FieldsRequest, FollowRequest, HOST_PROTOCOL_REVISIONS, LIVE_RING_DEFAULT, LIVE_RING_MAX, LIVE_VALUE_MAX, LineSplitter, LiveRequest, MAX_LINE_LENGTH, MutationId, PUBLIC_SHARE_DEFAULT_TTL, PUBLIC_SHARE_MAX_TTL_MS, RUN_LOG_MAX, RUN_LOG_TRUNCATED, RpcError, RpcPeer, RunAttachRequest, RunCancelRequest, RunId, RunRequest, SECRET_NAME_PATTERN, SECRET_VALUE_MAX, SHELL_SESSION_HEADER, SIGNAL_NAME_PATTERN, SIGNAL_PAYLOAD_MAX_BYTES, ServerEmitParams, ServerLiveValueParams, ServerSecretParams, ServerSignalParams, ServerStateGetParams, ServerStatePatchParams, ShareId, ShellSubmitRequest, SignalRequest, StreamTicketRequest, SubmissionEnvelope, SubmissionId, SubmissionPolicy, SubmitRequest, TALLY_LIVE_PREFIX, TokenId, VERIFY_RUNNER_METHODS, VerifyVerdict, VersionId, ViewId, ViewerId, WORKER_METHODS, WorkerBatchParams, WorkerCallOutcome, WorkerPingResult, WorkerStartResult, ariaSummary, artifactCreateMethod, artifactExportMethod, artifactGetMethod, artifactImportMethod, artifactListMethod, artifactPublishMethod, artifactVerifyMethod, canonicalJson, checkArgumentFrames, checkRequest, createDiagnostic, depsAddMethod, devStartMethod, devStopMethod, durationMs, encodeLine, estimateImageTokens, eventsAckMethod, eventsReadMethod, eventsSubscribeMethod, eventsUnsubscribeMethod, fromBase64, gcRunMethod, hostedPushMethod, hostedSubmissionMethod, instanceCheckMethod, instanceStatusMethod, isFieldId, isFinalRunStatus, livePushMethod, manifestProblem, mostraInitializeMethod, mostraShutdownMethod, negotiateRevision, normalizeScopes, parseParams, portAddressingExternalBaseMethod, portDraftsMirrorMethod, portIdentityResolveMethod, portRelayCallMethod, portRelayCancelMethod, portRelayLiveMethod, portRelayReconcileMethod, portRelayRunMethod, portSecretsGetMethod, portSubmissionsDeliverMethod, portTrustedControlsActivatedMethod, portTrustedControlsPolicyMethod, refusal, relayCallBatchMethod, relayEffectMethod, relayLiveValueMethod, relayRunUpdateMethod, remoteLoginMethod, remoteLoginWaitMethod, remoteLogoutMethod, remoteStatusMethod, remoteTokenMethod, resultsGetMethod, safeEqual, safePath, scopeAllows, secretDeleteMethod, secretListMethod, secretSetMethod, serializeAll, shareListMethod, shareStartMethod, shareStopMethod, stateGetMethod, statePatchMethod, toBase64, viewDefaultMethod, viewLaunchMethod, viewRevokeMethod, viewRotateMethod, viewUrlMethod, viewportsFor, waitMethod } from "@mostra/protocol";
8
8
  import { createHash, createHmac, randomBytes, randomInt, randomUUID } from "node:crypto";
9
9
  import { connect, createConnection, createServer, isIP } from "node:net";
10
10
  import { execFile, execFileSync, spawn } from "node:child_process";
11
- import { Duplex, Readable } from "node:stream";
12
- import { access, chmod, copyFile, cp, lstat, mkdir, mkdtemp, readFile, readdir, realpath, rm, stat, unlink, utimes, writeFile } from "node:fs/promises";
13
- import { homedir, networkInterfaces } from "node:os";
11
+ import { Duplex, PassThrough, Readable, Writable } from "node:stream";
12
+ import { access, chmod, copyFile, cp, lstat, mkdir, mkdtemp, open, readFile, readdir, realpath, rm, stat, unlink, utimes, writeFile } from "node:fs/promises";
13
+ import { homedir, hostname, networkInterfaces } from "node:os";
14
14
  import semver from "semver";
15
15
  import { createInterface } from "node:readline";
16
16
  import * as z from "zod";
17
- import { parse, parseDocument } from "yaml";
17
+ import { parse as parse$1, parseDocument } from "yaml";
18
18
  import { pipeline } from "node:stream/promises";
19
- import { createZstdCompress } from "node:zlib";
19
+ import { createZstdCompress, zstdCompressSync, zstdDecompressSync } from "node:zlib";
20
20
  import { createServer as createServer$1, request } from "node:http";
21
21
  import { getRequestListener } from "@hono/node-server";
22
22
  import { Hono } from "hono";
23
23
  import ignore from "ignore";
24
24
  import { AsyncLocalStorage } from "node:async_hooks";
25
+ import { WebSocket } from "ws";
26
+ import { request as request$1 } from "node:https";
25
27
  import { Resolver } from "node:dns/promises";
26
28
  //#region src/log.ts
27
29
  /**
@@ -289,6 +291,9 @@ function createActions(options) {
289
291
  const callRuns = /* @__PURE__ */ new Map();
290
292
  /** Why a run's worker stopped under it, until its `execute` ends it. */
291
293
  const crashCauses = /* @__PURE__ */ new Map();
294
+ /** Where relayed runs' and calls' effects go: by run ID, and by worker call ID. */
295
+ const runEffects = /* @__PURE__ */ new Map();
296
+ const callEffects = /* @__PURE__ */ new Map();
292
297
  let calls = 0;
293
298
  const redact = (text) => options.redactor.redact(text);
294
299
  const redactError = (error) => ({
@@ -327,7 +332,9 @@ function createActions(options) {
327
332
  run
328
333
  });
329
334
  /** Runs a recorded run in its version's worker, writing its output as it comes. */
330
- const execute = async (row) => {
335
+ const execute = async (row, relayed) => {
336
+ const observer = relayed?.observer;
337
+ if (relayed?.effect) runEffects.set(row.id, relayed.effect);
331
338
  const entry = { cancelled: false };
332
339
  executing.set(row.id, entry);
333
340
  let pendingLines = [];
@@ -351,6 +358,13 @@ function createActions(options) {
351
358
  pendingProgress = void 0;
352
359
  const written = interaction.runOutput(row.id, output);
353
360
  if (!written) return;
361
+ observer?.output({
362
+ logFrom: written.logFrom,
363
+ log: written.log,
364
+ batchFrom: written.batchFrom,
365
+ batches: written.batches,
366
+ ...progress ? { progress } : {}
367
+ });
354
368
  publish(row.artifact_id, {
355
369
  runId: row.id,
356
370
  status: "running",
@@ -371,17 +385,20 @@ function createActions(options) {
371
385
  ...reason ? { reason } : {},
372
386
  ...error ? { error: redactError(error) } : {}
373
387
  });
374
- if (state) publish(row.artifact_id, {
375
- runId: row.id,
376
- status: state.status,
377
- ...state.reason ? { reason: state.reason } : {},
378
- logFrom: state.log.length,
379
- log: [],
380
- batchFrom: state.batches.length,
381
- batches: [],
382
- ...state.error ? { error: state.error } : {},
383
- ...state.finishedAt ? { finishedAt: state.finishedAt } : {}
384
- });
388
+ if (state) {
389
+ observer?.ended(state);
390
+ publish(row.artifact_id, {
391
+ runId: row.id,
392
+ status: state.status,
393
+ ...state.reason ? { reason: state.reason } : {},
394
+ logFrom: state.log.length,
395
+ log: [],
396
+ batchFrom: state.batches.length,
397
+ batches: [],
398
+ ...state.error ? { error: state.error } : {},
399
+ ...state.finishedAt ? { finishedAt: state.finishedAt } : {}
400
+ });
401
+ }
385
402
  };
386
403
  let ended = false;
387
404
  try {
@@ -409,6 +426,7 @@ function createActions(options) {
409
426
  return;
410
427
  }
411
428
  interaction.runRunning(row.id);
429
+ observer?.running();
412
430
  publish(row.artifact_id, {
413
431
  runId: row.id,
414
432
  status: "running",
@@ -417,14 +435,15 @@ function createActions(options) {
417
435
  batchFrom: 0,
418
436
  batches: []
419
437
  });
420
- const viewer = row.viewer_id ? interaction.viewer(row.viewer_id) : void 0;
438
+ const viewer = relayed?.viewer ?? (row.viewer_id ? interaction.viewer(row.viewer_id) : void 0);
439
+ const viewId = relayed?.viewId ?? row.view_id ?? void 0;
421
440
  const { callId, outcome } = handle.call({
422
441
  fnId: row.action,
423
442
  args: JSON.parse(row.args_json),
424
443
  context: {
425
444
  kind: "run",
426
445
  runId: row.id,
427
- ...row.view_id ? { viewId: row.view_id } : {},
446
+ ...viewId ? { viewId } : {},
428
447
  ...viewer ? { viewer: {
429
448
  id: viewer.id,
430
449
  ...viewer.name ? { name: viewer.name } : {}
@@ -495,11 +514,12 @@ function createActions(options) {
495
514
  });
496
515
  } finally {
497
516
  executing.delete(row.id);
517
+ runEffects.delete(row.id);
498
518
  }
499
519
  };
500
520
  /** A plain call's lines, from a worker that has it. */
501
- async function* stream(page, input, signal, handle) {
502
- const viewer = interaction.viewer(page.viewerId);
521
+ async function* stream(caller, input, signal, handle) {
522
+ const viewer = caller.viewer;
503
523
  const queue = [];
504
524
  let wake;
505
525
  let size = 0;
@@ -509,14 +529,14 @@ function createActions(options) {
509
529
  args: input.args,
510
530
  context: {
511
531
  kind: "call",
512
- viewId: input.viewId,
513
- viewer: {
532
+ ...input.viewId !== void 0 ? { viewId: input.viewId } : {},
533
+ ...viewer ? { viewer: {
514
534
  id: viewer.id,
515
535
  ...viewer.name ? { name: viewer.name } : {}
516
- },
536
+ } } : {},
517
537
  dryRun: options.dryRun !== void 0
518
538
  },
519
- viewerId: page.viewerId
539
+ ...caller.viewerId !== void 0 ? { viewerId: caller.viewerId } : {}
520
540
  }, { onBatch(batch) {
521
541
  if (tooLarge) return;
522
542
  size += JSON.stringify(batch).length;
@@ -529,6 +549,7 @@ function createActions(options) {
529
549
  wake?.();
530
550
  } });
531
551
  calls += 1;
552
+ if (caller.effect) callEffects.set(callId, caller.effect);
532
553
  const abort = () => handle.cancel(callId, "The page went away.");
533
554
  signal.addEventListener("abort", abort, { once: true });
534
555
  let ended;
@@ -581,6 +602,7 @@ function createActions(options) {
581
602
  }
582
603
  } finally {
583
604
  calls -= 1;
605
+ callEffects.delete(callId);
584
606
  signal.removeEventListener("abort", abort);
585
607
  if (!ended) handle.cancel(callId, "The page went away.");
586
608
  }
@@ -589,7 +611,11 @@ function createActions(options) {
589
611
  async call(page, input, signal) {
590
612
  checkArgs(input.args);
591
613
  const versionId = interaction.viewVersion(page, input.viewId);
592
- return stream(page, input, signal, await pool.acquire(functionOf(versionId, input.fnId)));
614
+ const handle = await pool.acquire(functionOf(versionId, input.fnId));
615
+ return stream({
616
+ viewer: interaction.viewer(page.viewerId),
617
+ viewerId: page.viewerId
618
+ }, input, signal, handle);
593
619
  },
594
620
  startRun(page, input) {
595
621
  checkArgs(input.args);
@@ -621,6 +647,8 @@ function createActions(options) {
621
647
  bridge: {
622
648
  async request(worker, method, params, call) {
623
649
  const runId = call?.context.runId;
650
+ const relayedEffect = (runId !== void 0 ? runEffects.get(runId) : void 0) ?? (call ? callEffects.get(call.callId) : void 0);
651
+ const hosted = options.hostedState?.(worker.artifactId);
624
652
  switch (method) {
625
653
  case WORKER_METHODS.secret: {
626
654
  const { name } = ServerSecretParams.parse(params);
@@ -628,6 +656,7 @@ function createActions(options) {
628
656
  }
629
657
  case WORKER_METHODS.signal: {
630
658
  const parsed = ServerSignalParams.parse(params);
659
+ if (relayedEffect) return { cursor: await relayedEffect("signal", parsed.name, redactValue(parsed.payload, options.redactor)) };
631
660
  return { cursor: interaction.serverSignal(worker.artifactId, {
632
661
  name: parsed.name,
633
662
  ...parsed.payload !== void 0 ? { payload: parsed.payload } : {},
@@ -638,6 +667,7 @@ function createActions(options) {
638
667
  case WORKER_METHODS.emit: {
639
668
  const parsed = ServerEmitParams.parse(params);
640
669
  if (!/^host\.[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(parsed.type)) throw failed$2("MOSTRA_ACTION_EVENT_INVALID", `${parsed.type.slice(0, 80)} isn't a host event type.`);
670
+ if (relayedEffect) return { cursor: await relayedEffect("emit", parsed.type, redactValue(parsed.payload, options.redactor)) };
641
671
  return { cursor: interaction.serverEmit(worker.artifactId, {
642
672
  type: parsed.type,
643
673
  ...parsed.payload !== void 0 ? { payload: parsed.payload } : {},
@@ -647,10 +677,17 @@ function createActions(options) {
647
677
  }
648
678
  case WORKER_METHODS.stateGet: {
649
679
  const parsed = ServerStateGetParams.parse(params);
680
+ if (hosted) return hosted.get(parsed.ids);
650
681
  return interaction.serverState(worker.artifactId, parsed.ids);
651
682
  }
652
683
  case WORKER_METHODS.statePatch: {
653
684
  const parsed = ServerStatePatchParams.parse(params);
685
+ if (hosted) return hosted.patch({
686
+ ops: redactValue(parsed.ops, options.redactor),
687
+ mutationId: parsed.mutationId,
688
+ baseRevision: parsed.baseRevision,
689
+ runId
690
+ });
654
691
  return interaction.serverPatch(worker.artifactId, {
655
692
  ops: redactValue(parsed.ops, options.redactor),
656
693
  ...parsed.mutationId !== void 0 ? { mutationId: parsed.mutationId } : {},
@@ -686,6 +723,28 @@ function createActions(options) {
686
723
  },
687
724
  activity() {
688
725
  return executing.size + calls;
726
+ },
727
+ runRelayed(runId, relayed) {
728
+ if (executing.has(runId)) return;
729
+ const state = interaction.runState(runId);
730
+ if (!state || state.status !== "queued" && state.status !== "waiting") return;
731
+ const row = interaction.runRow(runId);
732
+ if (row) execute(row, relayed);
733
+ },
734
+ cancelRelayed(runId) {
735
+ const running = executing.get(runId);
736
+ if (!running) return;
737
+ running.cancelled = true;
738
+ if (running.handle && running.callId) running.handle.cancel(running.callId, "Cancelled from a hosted page.");
739
+ else running.endNow?.();
740
+ },
741
+ async callRelayed(input, signal) {
742
+ checkArgs(input.args);
743
+ const handle = await pool.acquire(functionOf(input.versionId, input.fnId));
744
+ return stream({
745
+ viewer: input.viewer,
746
+ effect: input.effect
747
+ }, input, signal, handle);
689
748
  }
690
749
  };
691
750
  }
@@ -2651,6 +2710,147 @@ const MIGRATIONS = [
2651
2710
  client TEXT NOT NULL,
2652
2711
  updated_at TEXT NOT NULL
2653
2712
  );
2713
+ `);
2714
+ }
2715
+ },
2716
+ {
2717
+ version: 12,
2718
+ name: "tallies",
2719
+ up(db) {
2720
+ db.exec(`
2721
+ CREATE TABLE tally (
2722
+ artifact_id TEXT NOT NULL REFERENCES artifact(id),
2723
+ field_id TEXT NOT NULL,
2724
+ choice TEXT NOT NULL,
2725
+ count INTEGER NOT NULL CHECK (count >= 0),
2726
+ PRIMARY KEY (artifact_id, field_id, choice)
2727
+ );
2728
+ CREATE TRIGGER tally_only_grows BEFORE UPDATE ON tally
2729
+ WHEN NEW.count < OLD.count OR NEW.artifact_id IS NOT OLD.artifact_id
2730
+ OR NEW.field_id IS NOT OLD.field_id OR NEW.choice IS NOT OLD.choice
2731
+ BEGIN
2732
+ SELECT RAISE(ABORT, 'a tally only grows');
2733
+ END;
2734
+ CREATE TRIGGER tally_kept BEFORE DELETE ON tally
2735
+ BEGIN
2736
+ SELECT RAISE(ABORT, 'a tally is kept with its artifact');
2737
+ END;
2738
+ CREATE TRIGGER tally_not_replaced BEFORE INSERT ON tally
2739
+ WHEN EXISTS (SELECT 1 FROM tally WHERE artifact_id = NEW.artifact_id
2740
+ AND field_id = NEW.field_id AND choice = NEW.choice)
2741
+ BEGIN
2742
+ SELECT RAISE(ABORT, 'a tally row is counted, never replaced');
2743
+ END;
2744
+ `);
2745
+ }
2746
+ },
2747
+ {
2748
+ version: 13,
2749
+ name: "remote server and owner link",
2750
+ up(db) {
2751
+ db.exec(`
2752
+ CREATE TABLE home_meta (
2753
+ id INTEGER PRIMARY KEY CHECK (id = 1),
2754
+ incarnation TEXT NOT NULL,
2755
+ created_at TEXT NOT NULL
2756
+ );
2757
+ `);
2758
+ db.widenCheck("viewer", "CHECK (kind IN ('local', 'tailnet', 'token', 'host'))", "CHECK (kind IN ('local', 'tailnet', 'token', 'host', 'account', 'anonymous'))");
2759
+ db.widenCheck("action_run", "CHECK (status IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled'))", "CHECK (status IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled', 'unknown'))");
2760
+ db.widenCheck("session", "CHECK (mode IN ('cookie', 'worker'))", "CHECK (mode IN ('cookie', 'worker', 'header'))");
2761
+ db.widenCheck("share", "via TEXT NOT NULL CHECK (via IN ('tailnet', 'funnel', 'cloudflare'))", "via TEXT NOT NULL CHECK (via IN ('tailnet', 'funnel', 'cloudflare', 'remote'))");
2762
+ db.widenCheck("share", "token_id TEXT NOT NULL REFERENCES token(id)", "token_id TEXT NOT NULL");
2763
+ db.exec(`
2764
+ CREATE TABLE relay_floor (
2765
+ link_id TEXT PRIMARY KEY,
2766
+ floor INTEGER NOT NULL CHECK (floor >= 0)
2767
+ );
2768
+ `);
2769
+ db.exec(`
2770
+ CREATE TABLE remote_missing (
2771
+ server TEXT NOT NULL REFERENCES remote_link(server),
2772
+ submission_id TEXT NOT NULL,
2773
+ noted_at TEXT NOT NULL,
2774
+ PRIMARY KEY (server, submission_id)
2775
+ );
2776
+ `);
2777
+ db.exec(`
2778
+ CREATE TRIGGER share_local_token BEFORE INSERT ON share
2779
+ WHEN NEW.via != 'remote' AND NOT EXISTS (SELECT 1 FROM token WHERE id = NEW.token_id)
2780
+ BEGIN
2781
+ SELECT RAISE(ABORT, 'a share through a tunnel names one of this home''s tokens');
2782
+ END;
2783
+ CREATE TRIGGER action_run_unknown_once BEFORE UPDATE ON action_run
2784
+ WHEN OLD.status = 'unknown' AND NEW.status NOT IN ('unknown', 'succeeded', 'failed', 'cancelled')
2785
+ BEGIN
2786
+ SELECT RAISE(ABORT, 'an unknown run is replaced only by the end its owner reports');
2787
+ END;
2788
+ `);
2789
+ db.exec(`
2790
+ CREATE TABLE remote_link (
2791
+ server TEXT PRIMARY KEY,
2792
+ account_id TEXT,
2793
+ account_name TEXT,
2794
+ instance_id TEXT,
2795
+ is_default INTEGER NOT NULL DEFAULT 0 CHECK (is_default IN (0, 1)),
2796
+ linked_at TEXT NOT NULL,
2797
+ state TEXT NOT NULL CHECK (state IN ('signed-in', 'signed-out')),
2798
+ incarnation TEXT,
2799
+ follow_cursor TEXT,
2800
+ relay_mark INTEGER NOT NULL DEFAULT 0 CHECK (relay_mark >= 0)
2801
+ );
2802
+ CREATE TABLE hosted_artifact (
2803
+ artifact_id TEXT NOT NULL REFERENCES artifact(id),
2804
+ server TEXT NOT NULL REFERENCES remote_link(server),
2805
+ hosted_home TEXT NOT NULL,
2806
+ route_id TEXT NOT NULL,
2807
+ label TEXT NOT NULL,
2808
+ token_id TEXT,
2809
+ url_secret_hint TEXT,
2810
+ shared_at TEXT NOT NULL,
2811
+ stopped_at TEXT,
2812
+ PRIMARY KEY (artifact_id, server)
2813
+ );
2814
+ CREATE TABLE version_push (
2815
+ artifact_id TEXT NOT NULL REFERENCES artifact(id),
2816
+ version_id TEXT NOT NULL REFERENCES version(id),
2817
+ server TEXT NOT NULL REFERENCES remote_link(server),
2818
+ state TEXT NOT NULL CHECK (state IN ('pending', 'complete', 'refused')),
2819
+ attempts INTEGER NOT NULL DEFAULT 0,
2820
+ next_attempt_at TEXT,
2821
+ created_at TEXT NOT NULL,
2822
+ -- Why the server refused the push for good (Opus review of WP-29, 10): it waits no
2823
+ -- longer, and no later push waits behind it.
2824
+ refusal TEXT,
2825
+ PRIMARY KEY (artifact_id, version_id, server)
2826
+ );
2827
+ CREATE TABLE relayed_run (
2828
+ run_id TEXT PRIMARY KEY REFERENCES action_run(id),
2829
+ server TEXT NOT NULL,
2830
+ relay_seq INTEGER NOT NULL,
2831
+ artifact_id TEXT NOT NULL,
2832
+ version_id TEXT NOT NULL,
2833
+ fn_id TEXT NOT NULL,
2834
+ seq_sent INTEGER NOT NULL DEFAULT 0,
2835
+ started_at TEXT NOT NULL,
2836
+ finished_at TEXT
2837
+ );
2838
+ CREATE TABLE relay_run (
2839
+ run_id TEXT PRIMARY KEY REFERENCES action_run(id),
2840
+ owner_link_id TEXT NOT NULL,
2841
+ relay_seq INTEGER NOT NULL,
2842
+ attempt INTEGER NOT NULL DEFAULT 0,
2843
+ sent_at TEXT,
2844
+ accepted_at TEXT,
2845
+ owner_heard_at TEXT,
2846
+ offline_limit_at TEXT NOT NULL,
2847
+ last_seq INTEGER NOT NULL DEFAULT 0,
2848
+ cancel_requested_at TEXT,
2849
+ UNIQUE (owner_link_id, relay_seq)
2850
+ );
2851
+ ALTER TABLE submission ADD COLUMN origin_json TEXT;
2852
+ ALTER TABLE event ADD COLUMN origin_json TEXT;
2853
+ CREATE UNIQUE INDEX event_origin ON event (origin_json) WHERE origin_json IS NOT NULL;
2654
2854
  `);
2655
2855
  }
2656
2856
  }
@@ -2766,9 +2966,9 @@ function unreadable$2(file, why) {
2766
2966
  return new RpcError(createDiagnostic("MOSTRA_HOME_LEDGER_UNREADABLE", { message: `The ledger ${file} can't be read: ${why}.` }));
2767
2967
  }
2768
2968
  /** Builds a new ledger file at `file` without ever replacing an existing one (DATA_MODEL §6). */
2769
- function createLedgerFile(storeDir, file, log) {
2969
+ function createLedgerFile(storeDir, file, log, fileName = LEDGER_FILE) {
2770
2970
  const Driver = loadDriver();
2771
- const temp = join(storeDir, `${LEDGER_FILE}.${process.pid}.${Date.now()}.creating`);
2971
+ const temp = join(storeDir, `${fileName}.${process.pid}.${Date.now()}.creating`);
2772
2972
  const db = new Driver(temp);
2773
2973
  try {
2774
2974
  db.pragma("journal_mode = WAL");
@@ -2797,7 +2997,7 @@ function syncDirectory(dir) {
2797
2997
  */
2798
2998
  function removeAbandonedFiles(storeDir) {
2799
2999
  for (const name of readdirSync(storeDir)) {
2800
- const match = /^ledger\.sqlite\.(\d+)\.\d+\.creating(-wal|-shm|-journal)?$/.exec(name);
3000
+ const match = /^(?:ledger|server)\.sqlite\.(\d+)\.\d+\.creating(-wal|-shm|-journal)?$/.exec(name);
2801
3001
  if (match && !processExists(Number(match[1]))) rmSync(join(storeDir, name), { force: true });
2802
3002
  }
2803
3003
  }
@@ -2809,6 +3009,7 @@ function processExists(pid) {
2809
3009
  return error.code === "EPERM";
2810
3010
  }
2811
3011
  }
3012
+ /** A migration's queries over a connection; exported for the schema tests, which migrate in memory. */
2812
3013
  function queries(db) {
2813
3014
  const cache = /* @__PURE__ */ new Map();
2814
3015
  const prepare = (sql) => {
@@ -2835,6 +3036,22 @@ function queries(db) {
2835
3036
  },
2836
3037
  exec(sql) {
2837
3038
  db.exec(sql);
3039
+ },
3040
+ widenCheck(table, from, to) {
3041
+ const row = db.prepare("SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?").get(table);
3042
+ if (row?.sql.split(from).length !== 2) throw new Error(`widenCheck: ${table}'s definition doesn't hold ${from} exactly once.`);
3043
+ const version = db.pragma("schema_version", { simple: true });
3044
+ db.unsafeMode(true);
3045
+ try {
3046
+ db.pragma("writable_schema = ON");
3047
+ db.prepare("UPDATE sqlite_schema SET sql = ? WHERE type = 'table' AND name = ?").run(row.sql.replace(from, to), table);
3048
+ db.pragma(`schema_version = ${version + 1}`);
3049
+ } finally {
3050
+ db.pragma("writable_schema = OFF");
3051
+ db.unsafeMode(false);
3052
+ }
3053
+ const problems = db.pragma("integrity_check", { simple: true });
3054
+ if (problems !== "ok") throw new Error(`widenCheck: ${table}: ${String(problems)}`);
2838
3055
  }
2839
3056
  };
2840
3057
  }
@@ -2855,20 +3072,22 @@ function openLedger(options) {
2855
3072
  recursive: true,
2856
3073
  mode: 448
2857
3074
  });
2858
- const file = join(storeDir, LEDGER_FILE);
2859
- if (!existsSync(file)) createLedgerFile(storeDir, file, log);
3075
+ const fileName = options.fileName ?? "ledger.sqlite";
3076
+ const file = join(storeDir, fileName);
3077
+ if (!existsSync(file)) createLedgerFile(storeDir, file, log, fileName);
2860
3078
  const Driver = loadDriver();
2861
3079
  let db;
2862
3080
  try {
2863
3081
  db = new Driver(file, {
2864
- timeout: 0,
3082
+ timeout: options.locking === "normal" ? 5e3 : 0,
2865
3083
  fileMustExist: true
2866
3084
  });
2867
3085
  } catch (error) {
2868
3086
  throw diagnose$1(error, file) ?? error;
2869
3087
  }
2870
3088
  try {
2871
- db.pragma("locking_mode = EXCLUSIVE");
3089
+ if (options.locking !== "normal") db.pragma("locking_mode = EXCLUSIVE");
3090
+ else if (options.walAutocheckpoint !== void 0) db.pragma(`wal_autocheckpoint = ${Math.max(0, Math.trunc(options.walAutocheckpoint))}`);
2872
3091
  const mode = db.pragma("journal_mode = WAL", { simple: true });
2873
3092
  if (mode !== "wal") throw unreadable$2(file, `its journal mode is ${String(mode)}, not WAL`);
2874
3093
  db.pragma("synchronous = FULL");
@@ -2912,7 +3131,7 @@ function migrate(db, file, storeDir, latest, migrations, options) {
2912
3131
  if (from > 0) {
2913
3132
  const problems = db.pragma("quick_check").map((row) => row.quick_check).filter((text) => text !== "ok");
2914
3133
  if (problems.length > 0) throw unreadable$2(file, `SQLite's integrity check failed (${problems.slice(0, 3).join("; ")})`);
2915
- backup = join(storeDir, `${LEDGER_FILE}.bak-${from}-${latest}`);
3134
+ backup = join(storeDir, `${options.fileName ?? "ledger.sqlite"}.bak-${from}-${latest}`);
2916
3135
  rmSync(backup, { force: true });
2917
3136
  db.prepare("VACUUM INTO ?").run(backup);
2918
3137
  options.log.info("ledger.backup", {
@@ -3001,6 +3220,40 @@ function wrap(db, file, schemaVersion, migrated) {
3001
3220
  }
3002
3221
  };
3003
3222
  }
3223
+ /**
3224
+ * Takes an advisory lock that the operating system releases when this process dies (DATA_MODEL
3225
+ * §7.1, D165): `file` is a one-row SQLite database held in `locking_mode=EXCLUSIVE` with a write
3226
+ * transaction open, so no other process can open it, and no stale PID file can outlive its
3227
+ * owner. Needs no native addon beyond the ledger's own driver. Answers `undefined` when another
3228
+ * process holds it.
3229
+ */
3230
+ function holdLock(file, owner) {
3231
+ const db = new (loadDriver())(file, { timeout: 0 });
3232
+ try {
3233
+ db.pragma("locking_mode = EXCLUSIVE");
3234
+ db.pragma("journal_mode = DELETE");
3235
+ db.prepare("BEGIN EXCLUSIVE").run();
3236
+ db.exec("CREATE TABLE IF NOT EXISTS holder (id INTEGER PRIMARY KEY CHECK (id = 1), pid, start_time, at)");
3237
+ db.prepare("INSERT OR REPLACE INTO holder (id, pid, start_time, at) VALUES (1, ?, ?, ?)").run(owner.pid, owner.startTime, (/* @__PURE__ */ new Date()).toISOString());
3238
+ } catch (error) {
3239
+ db.close();
3240
+ if (sqliteCode(error) === "SQLITE_BUSY") return void 0;
3241
+ throw diagnose$1(error, file) ?? error;
3242
+ }
3243
+ let held = true;
3244
+ return {
3245
+ file,
3246
+ release() {
3247
+ if (!held) return;
3248
+ held = false;
3249
+ try {
3250
+ db.prepare("ROLLBACK").run();
3251
+ } finally {
3252
+ db.close();
3253
+ }
3254
+ }
3255
+ };
3256
+ }
3004
3257
  //#endregion
3005
3258
  //#region src/processes/environment.ts
3006
3259
  /**
@@ -3618,7 +3871,7 @@ function lockText(lock) {
3618
3871
  }
3619
3872
  /** Reads a lockfile's text. */
3620
3873
  function parseLock(text) {
3621
- const data = parse(text) ?? {};
3874
+ const data = parse$1(text) ?? {};
3622
3875
  return {
3623
3876
  warm: data.warm ?? {},
3624
3877
  packages: data.packages ?? {},
@@ -3631,7 +3884,7 @@ function lockDigest(text) {
3631
3884
  }
3632
3885
  /** Every package, direct or transitive, an installer lockfile pins: `{ name, version }`. */
3633
3886
  function pinnedPackages(pnpm) {
3634
- const data = parse(pnpm) ?? {};
3887
+ const data = parse$1(pnpm) ?? {};
3635
3888
  const result = [];
3636
3889
  for (const key of Object.keys(data.packages ?? {})) {
3637
3890
  const at = key.lastIndexOf("@");
@@ -4596,11 +4849,13 @@ var ProtocolServer = class {
4596
4849
  * Serves one stream connection (stdio, or an accepted socket). `onClose` runs once when it
4597
4850
  * ends.
4598
4851
  */
4599
- serve(kind, input, output, onClose) {
4852
+ serve(kind, input, output, onClose, attributes = {}) {
4600
4853
  const closed = new AbortController();
4601
4854
  const session = {
4602
4855
  id: this.#nextId++,
4603
4856
  kind,
4857
+ attributes,
4858
+ link: void 0,
4604
4859
  client: void 0,
4605
4860
  revision: void 0,
4606
4861
  ports: /* @__PURE__ */ new Set(),
@@ -4753,7 +5008,7 @@ var ProtocolServer = class {
4753
5008
  registrants(port) {
4754
5009
  return [...this.#connections].filter((connection) => connection.session.initialized && connection.session.ports.has(port)).map((connection) => ({
4755
5010
  session: connection.session.id,
4756
- owner: connection.session.client?.name ?? ""
5011
+ owner: connection.session.link?.instanceId ?? (connection.session.kind === "websocket" ? "" : connection.session.client?.name ?? "")
4757
5012
  }));
4758
5013
  }
4759
5014
  /**
@@ -4849,7 +5104,9 @@ var ProtocolServer = class {
4849
5104
  const params = parseParams(mostraInitializeMethod, raw);
4850
5105
  const negotiation = negotiateRevision(params.revisions, this.#options.revisions ?? HOST_PROTOCOL_REVISIONS);
4851
5106
  if (!negotiation.ok) throw new RpcError(negotiation.diagnostic);
4852
- const instance = this.#options.describe();
5107
+ session.link = params.link;
5108
+ this.#options.admit?.(session, params);
5109
+ const instance = this.#options.describe(session);
4853
5110
  if (params.home?.path !== void 0 && !this.#options.servesHome(params.home.path)) throw new RpcError(createDiagnostic("MOSTRA_HOME_MISMATCH", { message: `The client expects the home ${params.home.path}; this instance serves ${instance.home}.` }));
4854
5111
  if (params.home !== void 0) this.#options.configureHome?.(params.home, params.client);
4855
5112
  if ((params.home !== void 0 || params.ports.length > 0) && params.client.name !== CLI_CLIENT_NAME) this.#options.markHostConfigured?.(params.client);
@@ -4951,6 +5208,136 @@ async function attach(path, client, timeoutMs = 5e3) {
4951
5208
  }
4952
5209
  }
4953
5210
  //#endregion
5211
+ //#region src/protocol-server/websocket.ts
5212
+ /**
5213
+ * The host protocol over WebSocket (INTERFACES §18.2, D165 item 2): the network binding between an
5214
+ * owner instance and its hosted home. One JSON-RPC message per text frame, at most
5215
+ * `MAX_LINE_LENGTH` characters, checked on both sides; a binary frame closes the connection
5216
+ * (1003), a longer message too (1009). Each side sends a WebSocket ping and the notification
5217
+ * `mostra/ping` every 20 s, since some proxies count only data as activity, and closes a
5218
+ * connection it heard nothing on for 45 s, inside the 60 s idle limit of the edges a server sits
5219
+ * behind (VERIFIED_FACTS, WP-28).
5220
+ *
5221
+ * The server side adapts a socket to {@link ProtocolServer.serve}'s line streams, so every rule of
5222
+ * the host protocol (initialize first, revisions, `requestId`s, port calls on the registering
5223
+ * connection) is the same code as stdio and the home socket.
5224
+ */
5225
+ /** The subprotocol a binding asks for (`Sec-WebSocket-Protocol`). */
5226
+ const BINDING_SUBPROTOCOL = "mostra.host";
5227
+ /** How often each side pings, and how long a silent connection lives. */
5228
+ const HEARTBEAT = {
5229
+ intervalMs: 2e4,
5230
+ silenceMs: 45e3
5231
+ };
5232
+ /** The largest frame a binding accepts, in bytes: a message's characters can take 4 bytes each. */
5233
+ const MAX_FRAME_BYTES = MAX_LINE_LENGTH * 4;
5234
+ /** The JSON-RPC heartbeat beside the WebSocket ping (a notification nobody answers). */
5235
+ const PING_LINE = JSON.stringify({
5236
+ jsonrpc: "2.0",
5237
+ method: "mostra/ping"
5238
+ });
5239
+ /**
5240
+ * Keeps a binding alive and judges it: pings each way every interval, and terminates the
5241
+ * connection once nothing (a message, a ping or a pong) arrived for the silence limit. Returns a
5242
+ * function that stops it; it stops by itself when the socket closes.
5243
+ */
5244
+ function keepAlive(socket, options = {}) {
5245
+ const intervalMs = options.intervalMs ?? HEARTBEAT.intervalMs;
5246
+ const silenceMs = options.silenceMs ?? HEARTBEAT.silenceMs;
5247
+ let heard = Date.now();
5248
+ const hear = () => {
5249
+ heard = Date.now();
5250
+ };
5251
+ socket.on("message", hear);
5252
+ socket.on("ping", hear);
5253
+ socket.on("pong", hear);
5254
+ const check = setInterval(() => {
5255
+ if (socket.readyState !== socket.OPEN) return;
5256
+ if (Date.now() - heard >= silenceMs) {
5257
+ options.onSilent?.();
5258
+ socket.terminate();
5259
+ return;
5260
+ }
5261
+ }, Math.min(1e3, Math.max(50, silenceMs / 10)));
5262
+ const ping = setInterval(() => {
5263
+ if (socket.readyState !== socket.OPEN) return;
5264
+ socket.ping();
5265
+ socket.send(PING_LINE);
5266
+ }, intervalMs);
5267
+ check.unref();
5268
+ ping.unref();
5269
+ const stop = () => {
5270
+ clearInterval(check);
5271
+ clearInterval(ping);
5272
+ };
5273
+ socket.once("close", stop);
5274
+ return stop;
5275
+ }
5276
+ /**
5277
+ * Checks one frame and returns its text, or closes the socket and returns `undefined`: binary
5278
+ * frames close it with 1003, a message longer than the limit with 1009.
5279
+ */
5280
+ function readFrame(socket, data, isBinary, log) {
5281
+ if (isBinary) {
5282
+ log?.warn("binding.binary_frame", {});
5283
+ socket.close(1003, "binary frames are not part of the binding");
5284
+ return;
5285
+ }
5286
+ const text = Array.isArray(data) ? Buffer.concat(data).toString("utf8") : Buffer.from(data).toString("utf8");
5287
+ if (text.includes("\n")) {
5288
+ log?.warn("binding.frame_with_newline", {});
5289
+ socket.close(1007, "one message per frame");
5290
+ return;
5291
+ }
5292
+ if (text.length > MAX_LINE_LENGTH) {
5293
+ log?.warn("binding.message_too_large", {
5294
+ code: "MOSTRA_HOST_MESSAGE_TOO_LARGE",
5295
+ length: text.length
5296
+ });
5297
+ socket.close(1009, "message too large");
5298
+ return;
5299
+ }
5300
+ return text;
5301
+ }
5302
+ /**
5303
+ * Serves one binding connection with `server`: frames in become lines, lines out become frames.
5304
+ * The heartbeat is kept; `attributes` (the account and grant the access token named) are the
5305
+ * session's. Returns the session.
5306
+ */
5307
+ function serveWebSocket(server, socket, options) {
5308
+ const input = new PassThrough();
5309
+ const output = new Writable({
5310
+ write(chunk, _encoding, done) {
5311
+ const text = (typeof chunk === "string" ? chunk : chunk.toString("utf8")).replace(/\n$/, "");
5312
+ if (socket.readyState !== socket.OPEN) {
5313
+ done();
5314
+ return;
5315
+ }
5316
+ socket.send(text, (error) => done(error ?? null));
5317
+ },
5318
+ final(done) {
5319
+ if (socket.readyState === socket.OPEN) socket.close(1e3);
5320
+ done();
5321
+ }
5322
+ });
5323
+ output.on("error", () => {});
5324
+ socket.on("message", (data, isBinary) => {
5325
+ const text = readFrame(socket, data, isBinary, options.log);
5326
+ if (text === void 0) return;
5327
+ if (text === PING_LINE) return;
5328
+ input.write(`${text}\n`);
5329
+ });
5330
+ socket.once("close", () => {
5331
+ input.end();
5332
+ });
5333
+ socket.on("error", (error) => options.log.warn("binding.socket_error", { error }));
5334
+ keepAlive(socket, {
5335
+ ...options.heartbeat,
5336
+ onSilent: () => options.log.warn("binding.silent", {})
5337
+ });
5338
+ return server.serve("websocket", input, output, options.onClose, options.attributes);
5339
+ }
5340
+ //#endregion
4954
5341
  //#region src/protocol-server/index.ts
4955
5342
  /**
4956
5343
  * The host protocol server (HO-02, D59): one dispatcher for stdio and the home socket, the
@@ -4971,6 +5358,7 @@ function sessionMethods(options) {
4971
5358
  ...options.deliveries ? { deliveries: [...options.deliveries()] } : {},
4972
5359
  ...options.workers ? { workers: [...options.workers()] } : {},
4973
5360
  ...options.shares ? { shares: [...options.shares()] } : {},
5361
+ ...options.links ? { links: [...options.links()] } : {},
4974
5362
  ...(() => {
4975
5363
  const at = options.idleExitAt?.();
4976
5364
  return at === void 0 ? {} : { idleExitAt: at };
@@ -5036,9 +5424,18 @@ function redacted(value) {
5036
5424
  */
5037
5425
  function appendEvent(db, event) {
5038
5426
  const seq = event.artifactId === null ? null : db.get("SELECT COALESCE(MAX(seq), 0) + 1 AS next FROM event WHERE artifact_id = ?", event.artifactId)?.next ?? 1;
5039
- const result = db.run("INSERT INTO event (artifact_id, seq, type, actor_json, causation_json, payload_json, at) VALUES (?, ?, ?, ?, ?, ?, ?)", event.artifactId, seq, event.type, JSON.stringify(event.actor ?? { kind: "agent" }), event.causation === void 0 ? null : JSON.stringify(event.causation), JSON.stringify(redacted(event.payload)), (/* @__PURE__ */ new Date()).toISOString());
5427
+ const result = db.run(event.origin === void 0 ? "INSERT INTO event (artifact_id, seq, type, actor_json, causation_json, payload_json, at) VALUES (?, ?, ?, ?, ?, ?, ?)" : "INSERT INTO event (artifact_id, seq, type, actor_json, causation_json, payload_json, at, origin_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", event.artifactId, seq, event.type, JSON.stringify(event.actor ?? { kind: "agent" }), event.causation === void 0 ? null : JSON.stringify(event.causation), JSON.stringify(redacted(event.payload)), (/* @__PURE__ */ new Date()).toISOString(), ...event.origin === void 0 ? [] : [originJson(event.origin)]);
5040
5428
  return String(result.lastInsertRowid);
5041
5429
  }
5430
+ /** An origin's stored form: its keys in one order, so one origin is one text (the unique index). */
5431
+ function originJson(origin) {
5432
+ return JSON.stringify({
5433
+ server: origin.server,
5434
+ home: origin.home,
5435
+ incarnation: origin.incarnation,
5436
+ cursor: origin.cursor
5437
+ });
5438
+ }
5042
5439
  /** An event row as the wire carries it. */
5043
5440
  function eventRecord(row) {
5044
5441
  return {
@@ -5049,7 +5446,8 @@ function eventRecord(row) {
5049
5446
  at: row.at,
5050
5447
  actor: JSON.parse(row.actor_json),
5051
5448
  ...row.causation_json !== null ? { causation: JSON.parse(row.causation_json) } : {},
5052
- payload: JSON.parse(row.payload_json)
5449
+ payload: JSON.parse(row.payload_json),
5450
+ ...row.origin_json ? { origin: JSON.parse(row.origin_json) } : {}
5053
5451
  };
5054
5452
  }
5055
5453
  /** The newest cursor in the home, or 0 before the first event. */
@@ -5216,7 +5614,7 @@ function liveFields(db, documentId) {
5216
5614
  const rows = db.all("SELECT * FROM field WHERE document_id = ? AND deleted = 0 ORDER BY field_id", documentId);
5217
5615
  return Object.fromEntries(rows.map((row) => [row.field_id, fieldState(row)]));
5218
5616
  }
5219
- function invalid$1(message) {
5617
+ function invalid$4(message) {
5220
5618
  return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }), -32602);
5221
5619
  }
5222
5620
  function tooLarge$1(message) {
@@ -5224,13 +5622,13 @@ function tooLarge$1(message) {
5224
5622
  }
5225
5623
  /** Checks the ops before anything is read: IDs, one change each, JSON values within limits. */
5226
5624
  function checkOps(ops) {
5227
- if (ops.length === 0) throw invalid$1("A mutation needs at least one op.");
5625
+ if (ops.length === 0) throw invalid$4("A mutation needs at least one op.");
5228
5626
  const encoded = /* @__PURE__ */ new Map();
5229
5627
  for (const op of ops) {
5230
5628
  if (!isFieldId(op.id)) throw new RpcError(createDiagnostic("MOSTRA_FIELD_ID_INVALID", { message: `${JSON.stringify(op.id).slice(0, 80)} isn't a field ID.` }));
5231
- if (encoded.has(op.id)) throw invalid$1(`The field ${op.id} appears twice in one mutation.`);
5629
+ if (encoded.has(op.id)) throw invalid$4(`The field ${op.id} appears twice in one mutation.`);
5232
5630
  const deletes = op.delete === true;
5233
- if (deletes === (op.value !== void 0)) throw invalid$1(`The op for ${op.id} needs exactly one of value and delete.`);
5631
+ if (deletes === (op.value !== void 0)) throw invalid$4(`The op for ${op.id} needs exactly one of value and delete.`);
5234
5632
  if (deletes) {
5235
5633
  encoded.set(op.id, void 0);
5236
5634
  continue;
@@ -5240,7 +5638,7 @@ function checkOps(ops) {
5240
5638
  canonicalJson(op.value);
5241
5639
  json = JSON.stringify(op.value);
5242
5640
  } catch {
5243
- throw invalid$1(`The value of ${op.id} isn't JSON.`);
5641
+ throw invalid$4(`The value of ${op.id} isn't JSON.`);
5244
5642
  }
5245
5643
  if (Buffer.byteLength(json, "utf8") > 1048576) throw tooLarge$1(`The value of ${op.id} is larger than 1 MiB of JSON.`);
5246
5644
  encoded.set(op.id, json);
@@ -5420,21 +5818,21 @@ function storedPolicy(policyJson) {
5420
5818
  */
5421
5819
  /** The signal every submission raises (D106); no one else may raise it. */
5422
5820
  const SUBMIT_SIGNAL = "submit";
5423
- function invalid(message) {
5821
+ function invalid$3(message) {
5424
5822
  return new RpcError(createDiagnostic("MOSTRA_SUBMIT_SIGNAL_INVALID", { message }));
5425
5823
  }
5426
5824
  /** Checks a signal's name and payload (D106). */
5427
5825
  function checkSignal(name, payload) {
5428
- if (typeof name !== "string" || !SIGNAL_NAME_PATTERN.test(name)) throw invalid(`${JSON.stringify(name).slice(0, 80)} isn't a signal name: 1 to 64 letters, digits, ".", "_" and "-".`);
5429
- if (name === "submit") throw invalid("The signal name \"submit\" is reserved: a submission raises it.");
5826
+ if (typeof name !== "string" || !SIGNAL_NAME_PATTERN.test(name)) throw invalid$3(`${JSON.stringify(name).slice(0, 80)} isn't a signal name: 1 to 64 letters, digits, ".", "_" and "-".`);
5827
+ if (name === "submit") throw invalid$3("The signal name \"submit\" is reserved: a submission raises it.");
5430
5828
  if (payload === void 0) return;
5431
5829
  let text;
5432
5830
  try {
5433
5831
  text = canonicalJson(payload);
5434
5832
  } catch {
5435
- throw invalid(`The payload of the signal ${name} isn't JSON.`);
5833
+ throw invalid$3(`The payload of the signal ${name} isn't JSON.`);
5436
5834
  }
5437
- if (Buffer.byteLength(text, "utf8") > SIGNAL_PAYLOAD_MAX_BYTES) throw invalid(`The payload of the signal ${name} is larger than 64 KiB of JSON.`);
5835
+ if (Buffer.byteLength(text, "utf8") > SIGNAL_PAYLOAD_MAX_BYTES) throw invalid$3(`The payload of the signal ${name} is larger than 64 KiB of JSON.`);
5438
5836
  }
5439
5837
  /**
5440
5838
  * Appends `signal.raised` inside the caller's transaction and returns its cursor, after checking
@@ -5472,6 +5870,84 @@ function appendSignal(db, input) {
5472
5870
  });
5473
5871
  }
5474
5872
  //#endregion
5873
+ //#region src/interaction/tally.ts
5874
+ /**
5875
+ * Counts by choice (D165 item 12, RM-07; DATA_MODEL §7.4, INTERFACES §18.4). An artifact that
5876
+ * names a fixed-choice field in its manifest's `tally` gets a count of each choice, grown inside the
5877
+ * submit transaction, one more for the choice the new submission holds, and read by viewers under
5878
+ * the reserved live key `$tally/<field>`. Nothing else of a submission reaches viewers this way: a
5879
+ * value outside the listed choices counts nothing, so free text is never shown, and below the
5880
+ * entry's `minTotal` viewers see only the total.
5881
+ */
5882
+ /** The tally entries of a version's manifest; none when it has none or can't be read. */
5883
+ function tallySpecs(db, versionId) {
5884
+ const row = db.get("SELECT manifest_json FROM version WHERE id = ?", versionId);
5885
+ if (!row) return [];
5886
+ try {
5887
+ const parsed = ArtifactManifest.safeParse(JSON.parse(row.manifest_json));
5888
+ return parsed.success ? parsed.data.tally ?? [] : [];
5889
+ } catch {
5890
+ return [];
5891
+ }
5892
+ }
5893
+ /**
5894
+ * Counts a new submission, in its submit transaction: for each tally of the version the
5895
+ * submission names, one more for the choice its draft holds for the field. Returns the fields whose
5896
+ * counts grew, so their viewers can be told after the commit.
5897
+ */
5898
+ function countSubmission(db, input) {
5899
+ const grown = [];
5900
+ for (const spec of tallySpecs(db, input.versionId)) {
5901
+ const value = Object.hasOwn(input.fields, spec.field) ? input.fields[spec.field]?.value : void 0;
5902
+ if (typeof value !== "string" || !spec.choices.includes(value)) continue;
5903
+ if (db.run("UPDATE tally SET count = count + 1 WHERE artifact_id = ? AND field_id = ? AND choice = ?", input.artifactId, spec.field, value).changes === 0) db.run("INSERT INTO tally (artifact_id, field_id, choice, count) VALUES (?, ?, ?, 1)", input.artifactId, spec.field, value);
5904
+ grown.push(spec.field);
5905
+ }
5906
+ return grown;
5907
+ }
5908
+ /** The field a tally's live key reads, or `undefined` for any other key. */
5909
+ function tallyField(key) {
5910
+ return key.startsWith(TALLY_LIVE_PREFIX) ? key.slice(TALLY_LIVE_PREFIX.length) : void 0;
5911
+ }
5912
+ /**
5913
+ * The tally of `field` as viewers see it, by the artifact's latest version's entry: each listed
5914
+ * choice's count (a choice no one chose is 0), and the total, or only the total below the entry's
5915
+ * `minTotal`. `undefined` when the latest version tallies no such field.
5916
+ */
5917
+ function tallyState(db, artifactId, field) {
5918
+ const latest = db.get("SELECT latest_version_id FROM artifact WHERE id = ?", artifactId)?.latest_version_id;
5919
+ if (!latest) return void 0;
5920
+ const spec = tallySpecs(db, latest).find((entry) => entry.field === field);
5921
+ if (!spec) return void 0;
5922
+ const rows = db.all("SELECT choice, count FROM tally WHERE artifact_id = ? AND field_id = ?", artifactId, field);
5923
+ const byChoice = new Map(rows.map((row) => [row.choice, row.count]));
5924
+ const counted = rows.reduce((sum, row) => sum + row.count, 0);
5925
+ const counts = {};
5926
+ let total = 0;
5927
+ for (const choice of spec.choices) {
5928
+ const count = byChoice.get(choice) ?? 0;
5929
+ Object.defineProperty(counts, choice, {
5930
+ value: count,
5931
+ enumerable: true,
5932
+ writable: true,
5933
+ configurable: true
5934
+ });
5935
+ total += count;
5936
+ }
5937
+ const value = total < (spec.minTotal ?? 0) ? { total } : {
5938
+ counts,
5939
+ total
5940
+ };
5941
+ return {
5942
+ revision: String(counted),
5943
+ value,
5944
+ batches: [[{
5945
+ c: 0,
5946
+ v: value
5947
+ }]]
5948
+ };
5949
+ }
5950
+ //#endregion
5475
5951
  //#region src/interaction/viewers.ts
5476
5952
  /**
5477
5953
  * Viewers and views (D104, DATA_MODEL §3). A viewer is a person: the home's local user, or the
@@ -5805,6 +6281,11 @@ function submit(db, input) {
5805
6281
  db.run(`INSERT INTO submission (id, position, artifact_id, version_id, view_id, viewer_id, channel, policy_json, epoch, scope_key,
5806
6282
  previous_id, draft_document_id, draft_revision, state_revision, latest_version_number, snapshot_json, digest, stale_view, request_id, created_at)
5807
6283
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?)`, submissionId, position, input.artifactId, view.version_id, view.id, input.viewerId, input.channel, JSON.stringify(policy), epoch, scopeKey, previous?.id ?? null, draft.id, draft.revision, state?.revision ?? null, staleView && latest ? latest.number : null, snapshotJson, digest, staleView ? 1 : 0, at);
6284
+ const tallied = countSubmission(db, {
6285
+ artifactId: input.artifactId,
6286
+ versionId: view.version_id,
6287
+ fields: snapshot.draft.fields
6288
+ });
5808
6289
  if (policy.policy === "epochs") db.run("UPDATE document SET epoch = ? WHERE id = ?", epoch + 1, draft.id);
5809
6290
  const owners = input.owners.length > 0 ? [...new Set(input.owners)].sort() : [""];
5810
6291
  for (const owner of owners) db.run("INSERT INTO delivery (id, submission_id, position, port_owner, state, attempts, next_attempt_at, updated_at) VALUES (?, ?, ?, ?, 'pending', 0, ?, ?)", DeliveryId.create(), submissionId, position, owner, at, at);
@@ -5817,7 +6298,176 @@ function submit(db, input) {
5817
6298
  });
5818
6299
  return {
5819
6300
  submission: submissionSummary(db, db.get("SELECT * FROM submission WHERE id = ?", submissionId)),
5820
- created: true
6301
+ created: true,
6302
+ ...tallied.length > 0 ? { tallied } : {}
6303
+ };
6304
+ }
6305
+ //#endregion
6306
+ //#region src/interaction/imports.ts
6307
+ /**
6308
+ * The owner instance's copies of its hosted homes' records (D165 item 1, DATA_MODEL §7.3): the
6309
+ * submissions a hosted home delivers to it, stored with their hosted IDs, digest and snapshot and
6310
+ * enqueued to the owner's own hosts in the same transaction, so remote submissions reach local hosts
6311
+ * through the local outbox (RM-02, H-04); and the events the sync cursor follows, each appended
6312
+ * once, with where it came from.
6313
+ */
6314
+ /** The built-in adapter's owner name, as `submit` uses it (D105). */
6315
+ const CROCKFORD$4 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
6316
+ /**
6317
+ * A local delivery's ID for an imported submission and one of the owner's hosts: `dlv_` and 26
6318
+ * Crockford base32 characters of SHA-256(submission ID ‖ 0x00 ‖ host owner), its first two bits
6319
+ * cleared so it reads as a ULID. A re-import (the owner's home restored) repeats it, so a host
6320
+ * that dedupes by delivery ID sees one delivery (DATA_MODEL §7.3, D105's identity).
6321
+ */
6322
+ function importedDeliveryId(submissionId, owner) {
6323
+ const digest = createHash("sha256").update(Buffer.concat([
6324
+ Buffer.from(submissionId, "utf8"),
6325
+ Buffer.from([0]),
6326
+ Buffer.from(owner, "utf8")
6327
+ ])).digest();
6328
+ digest[0] = digest[0] & 63;
6329
+ let out = "";
6330
+ let buffer = 0;
6331
+ let bits = 0;
6332
+ for (const byte of digest) {
6333
+ buffer = buffer << 8 | byte;
6334
+ bits += 8;
6335
+ while (bits >= 5 && out.length < 26) {
6336
+ bits -= 5;
6337
+ out += CROCKFORD$4.charAt(buffer >> bits & 31);
6338
+ }
6339
+ if (out.length >= 26) break;
6340
+ }
6341
+ return `dlv_${out}`;
6342
+ }
6343
+ /**
6344
+ * Stores a delivered hosted submission and its local deliveries in the caller's transaction, and
6345
+ * answers the hosted home as a host does (D105): `accepted` once it is durable here, a repeat of
6346
+ * one already held too; a submission it can never hold is refused for good.
6347
+ */
6348
+ function importSubmission(db, input) {
6349
+ const { envelope, source } = input;
6350
+ const refuse = (reason) => ({
6351
+ accepted: false,
6352
+ reason,
6353
+ retry: false
6354
+ });
6355
+ if (db.get("SELECT 1 FROM submission WHERE id = ?", envelope.submissionId)) return { accepted: true };
6356
+ if (envelope.snapshot === void 0) return refuse("invalid: the delivery carries no snapshot");
6357
+ const digest = createHash("sha256").update(envelope.snapshot, "utf8").digest("hex");
6358
+ if (digest !== envelope.results.submission.digest) return refuse("invalid: the snapshot doesn't match the submission's digest");
6359
+ let snapshot;
6360
+ try {
6361
+ snapshot = JSON.parse(envelope.snapshot);
6362
+ } catch {
6363
+ return refuse("invalid: the snapshot isn't JSON");
6364
+ }
6365
+ if (snapshot.submissionId !== envelope.submissionId || snapshot.artifactId !== envelope.artifactId || typeof snapshot.versionId !== "string" || typeof snapshot.viewId !== "string") return refuse("invalid: the snapshot names another submission");
6366
+ if (!input.owned(db, snapshot.artifactId)) return refuse("undeliverable: this instance doesn't own the artifact");
6367
+ if (!db.get("SELECT 1 FROM version WHERE id = ? AND artifact_id = ?", snapshot.versionId, snapshot.artifactId)) return refuse("undeliverable: this instance doesn't hold the submission's version");
6368
+ const hostedViewer = snapshot.viewer;
6369
+ const kind = hostedViewer.kind === "local" ? "token" : hostedViewer.kind;
6370
+ const ref = `${source.serverKey}:${hostedViewer.ref ?? hostedViewer.id}`;
6371
+ let viewerId = db.get("SELECT id FROM viewer WHERE kind = ? AND external_ref = ?", kind, ref)?.id;
6372
+ if (viewerId === void 0) {
6373
+ viewerId = db.get("SELECT 1 FROM viewer WHERE id = ?", hostedViewer.id) ? ViewerId.create() : hostedViewer.id;
6374
+ db.run("INSERT INTO viewer (id, kind, external_ref, name, created_at) VALUES (?, ?, ?, ?, ?)", viewerId, kind, ref, hostedViewer.name ?? null, snapshot.at);
6375
+ }
6376
+ const view = db.get("SELECT artifact_id FROM view WHERE id = ?", snapshot.viewId);
6377
+ if (view && view.artifact_id !== snapshot.artifactId) return refuse("invalid: the view is another artifact's");
6378
+ if (!view) db.run("INSERT INTO view (id, artifact_id, version_id, viewer_id, token_id, opened_cursor, opened_at) VALUES (?, ?, ?, ?, NULL, NULL, ?)", snapshot.viewId, snapshot.artifactId, snapshot.versionId, viewerId, snapshot.at);
6379
+ const draft = documentFor(db, snapshot.artifactId, viewerId, true);
6380
+ const previous = snapshot.previous !== void 0 && db.get("SELECT 1 FROM submission WHERE id = ?", snapshot.previous) ? snapshot.previous : null;
6381
+ const origin = {
6382
+ server: source.server,
6383
+ home: source.home,
6384
+ incarnation: source.incarnation,
6385
+ cursor: envelope.cursor ?? "0"
6386
+ };
6387
+ const localPosition = Number(appendEvent(db, {
6388
+ artifactId: snapshot.artifactId,
6389
+ type: "submission.created",
6390
+ actor: {
6391
+ kind: "viewer",
6392
+ id: viewerId
6393
+ },
6394
+ causation: { submissionId: snapshot.submissionId },
6395
+ payload: {
6396
+ submissionId: snapshot.submissionId,
6397
+ versionId: snapshot.versionId,
6398
+ version: snapshot.version,
6399
+ viewId: snapshot.viewId,
6400
+ viewerId,
6401
+ channel: snapshot.channel,
6402
+ epoch: snapshot.epoch,
6403
+ staleView: snapshot.staleView,
6404
+ digest,
6405
+ imported: true
6406
+ }
6407
+ }));
6408
+ db.run(`INSERT INTO submission (id, position, artifact_id, version_id, view_id, viewer_id, channel, policy_json, epoch, scope_key,
6409
+ previous_id, draft_document_id, draft_revision, state_revision, latest_version_number, snapshot_json, digest, stale_view,
6410
+ request_id, created_at, origin_json)
6411
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?)`, snapshot.submissionId, localPosition, snapshot.artifactId, snapshot.versionId, snapshot.viewId, viewerId, snapshot.channel, JSON.stringify(snapshot.policy), snapshot.epoch, `remote:${source.serverKey}:${snapshot.submissionId}`, previous, draft.id, Number(snapshot.draft.revision), snapshot.state ? Number(snapshot.state.revision) : null, snapshot.latestVersion ?? null, envelope.snapshot, digest, snapshot.staleView ? 1 : 0, snapshot.at, originJson(origin));
6412
+ const at = (/* @__PURE__ */ new Date()).toISOString();
6413
+ const owners = input.owners.length > 0 ? [...new Set(input.owners)].sort() : [""];
6414
+ for (const owner of owners) db.run("INSERT INTO delivery (id, submission_id, position, port_owner, state, attempts, next_attempt_at, updated_at) VALUES (?, ?, ?, ?, 'pending', 0, ?, ?)", importedDeliveryId(snapshot.submissionId, owner), snapshot.submissionId, localPosition, owner, at, at);
6415
+ return { accepted: true };
6416
+ }
6417
+ /**
6418
+ * Types a follow never copies: the owner publishes its versions itself, and its own deliveries
6419
+ * record their own outcomes. (A hosted `submission.created` is never appended either: its
6420
+ * submission arrives as a delivery, below.)
6421
+ */
6422
+ const NOT_FOLLOWED = /* @__PURE__ */ new Set([
6423
+ "artifact.created",
6424
+ "version.published",
6425
+ "submission.retrying",
6426
+ "submission.delivered",
6427
+ "submission.rejected",
6428
+ "submission.undeliverable"
6429
+ ]);
6430
+ /**
6431
+ * Copies a page of the hosted home's events into this home's log, in the caller's transaction:
6432
+ * only the artifacts this instance owns, each event once (its origin is unique), with local
6433
+ * cursors. A followed `submission.created` this home lacks is reported, so the caller has it
6434
+ * delivered again rather than appending a record without its submission.
6435
+ */
6436
+ function followEvents(db, input) {
6437
+ let appended = 0;
6438
+ const missing = [];
6439
+ for (const event of input.events) {
6440
+ if (event.artifactId === null || !input.owned(db, event.artifactId)) continue;
6441
+ if (event.type === "submission.created") {
6442
+ const id = event.payload["submissionId"];
6443
+ if (typeof id === "string" && !db.get("SELECT 1 FROM submission WHERE id = ?", id)) missing.push(id);
6444
+ continue;
6445
+ }
6446
+ if (NOT_FOLLOWED.has(event.type)) continue;
6447
+ if (event.type.startsWith("action.")) {
6448
+ const runId = event.payload?.runId;
6449
+ if (typeof runId === "string" && db.get("SELECT 1 FROM action_run WHERE id = ?", runId)) continue;
6450
+ }
6451
+ const origin = {
6452
+ server: input.source.server,
6453
+ home: input.source.home,
6454
+ incarnation: input.source.incarnation,
6455
+ cursor: event.cursor
6456
+ };
6457
+ if (db.get("SELECT 1 FROM event WHERE origin_json = ?", originJson(origin))) continue;
6458
+ appendEvent(db, {
6459
+ artifactId: event.artifactId,
6460
+ type: event.type,
6461
+ actor: event.actor,
6462
+ ...event.causation !== void 0 ? { causation: event.causation } : {},
6463
+ payload: event.payload,
6464
+ origin
6465
+ });
6466
+ appended += 1;
6467
+ }
6468
+ return {
6469
+ appended,
6470
+ missing
5821
6471
  };
5822
6472
  }
5823
6473
  //#endregion
@@ -5924,7 +6574,8 @@ function buildResults(db, row) {
5924
6574
  ...row.state_revision !== null ? { stateRevision: String(row.state_revision) } : {},
5925
6575
  digest: row.digest,
5926
6576
  delivery: overallDelivery(deliveries),
5927
- deliveries
6577
+ deliveries,
6578
+ ...row.origin_json ? { origin: JSON.parse(row.origin_json) } : {}
5928
6579
  },
5929
6580
  answers: nestFields(draftFields),
5930
6581
  fields: [...Object.keys(draftFields).sort().map((id) => describe(id, draftFields[id], "viewer")), ...Object.keys(stateFields ?? {}).sort().map((id) => describe(id, stateFields[id], "artifact"))],
@@ -6164,7 +6815,11 @@ var Outbox = class {
6164
6815
  submissionId: submission.id,
6165
6816
  artifactId: submission.artifact_id,
6166
6817
  attempt,
6167
- results: buildResults(db, submission)
6818
+ results: buildResults(db, submission),
6819
+ ...this.#options.snapshots ? {
6820
+ snapshot: submission.snapshot_json,
6821
+ cursor: String(submission.position)
6822
+ } : {}
6168
6823
  };
6169
6824
  });
6170
6825
  let outcome;
@@ -6423,9 +7078,53 @@ function createOrJoinRun(db, input) {
6423
7078
  joined: false
6424
7079
  };
6425
7080
  }
6426
- /** Moves a queued run to `running`, when its worker has it. */
7081
+ /** Moves a queued (or, on a remote server, waiting) run to `running`, when its worker has it. */
6427
7082
  function markRunning(db, runId) {
6428
- db.run("UPDATE action_run SET status = 'running' WHERE id = ? AND status = 'queued'", runId);
7083
+ db.run("UPDATE action_run SET status = 'running' WHERE id = ? AND status IN ('queued', 'waiting')", runId);
7084
+ }
7085
+ /**
7086
+ * A relayed run waits for its owner's instance (D165 item 4): `waiting`, with `action.waiting`.
7087
+ * Returns whether it moved (a queued run only).
7088
+ */
7089
+ function markWaiting(db, runId) {
7090
+ const row = db.get("SELECT * FROM action_run WHERE id = ?", runId);
7091
+ if (row?.status !== "queued") return false;
7092
+ db.run("UPDATE action_run SET status = 'waiting' WHERE id = ?", runId);
7093
+ appendEvent(db, {
7094
+ artifactId: row.artifact_id,
7095
+ type: "action.waiting",
7096
+ actor: { kind: "mostra" },
7097
+ causation: { runId },
7098
+ payload: {
7099
+ runId,
7100
+ action: row.action
7101
+ }
7102
+ });
7103
+ return true;
7104
+ }
7105
+ /**
7106
+ * A relayed run whose owner never reported its end within the offline limit, or no longer knows
7107
+ * it (D165 item 4): `unknown`, with `action.unknown`. It may have taken effect, so it is never
7108
+ * `failed`; the owner's later report replaces it once (a trigger). Returns whether it moved.
7109
+ */
7110
+ function markUnknown(db, runId) {
7111
+ const row = db.get("SELECT * FROM action_run WHERE id = ?", runId);
7112
+ if (!row || row.status !== "queued" && row.status !== "running" && row.status !== "waiting") return false;
7113
+ db.run("UPDATE action_run SET status = 'unknown', reason = 'owner-unreachable', error_json = ? WHERE id = ?", JSON.stringify({
7114
+ code: "MOSTRA_REMOTE_OWNER_OFFLINE",
7115
+ message: "Its owner's machine never said how it ended, so it may have taken effect; it shows here if the owner reports."
7116
+ }), runId);
7117
+ appendEvent(db, {
7118
+ artifactId: row.artifact_id,
7119
+ type: "action.unknown",
7120
+ actor: { kind: "mostra" },
7121
+ causation: { runId },
7122
+ payload: {
7123
+ runId,
7124
+ action: row.action
7125
+ }
7126
+ });
7127
+ return true;
6429
7128
  }
6430
7129
  /**
6431
7130
  * Appends a run's new log lines (until the 1 MB cap, then one marker line) and result batches,
@@ -6878,22 +7577,19 @@ function createEphemeralBus(options = {}) {
6878
7577
  };
6879
7578
  const deliver = (view, message) => {
6880
7579
  for (const listener of view.listeners) listener(message);
6881
- if (view.queue && (view.queueUntil ?? 0) > now()) {
6882
- coalesceInto(view, view.queue, message);
6883
- if (view.queue.length > POLL_QUEUE_MAX) view.queue.splice(0, view.queue.length - POLL_QUEUE_MAX);
6884
- view.wake?.();
6885
- }
7580
+ const poll = view.poll;
7581
+ if (!poll || poll.until <= now()) return;
7582
+ coalesceInto(view, poll.messages, message);
7583
+ if (poll.messages.length > POLL_QUEUE_MAX) poll.messages.splice(0, poll.messages.length - POLL_QUEUE_MAX);
7584
+ for (const wake of [...poll.wakes]) wake();
6886
7585
  };
6887
7586
  const sweep = setInterval(() => {
6888
7587
  const at = now();
6889
7588
  for (const [viewId, view] of views) {
6890
7589
  for (const [runId, until] of view.runs) if (until <= at) view.runs.delete(runId);
6891
7590
  for (const [key, lease] of view.live) if (lease.until <= at) view.live.delete(key);
6892
- if (view.queue && (view.queueUntil ?? 0) <= at) {
6893
- delete view.queue;
6894
- delete view.queueUntil;
6895
- }
6896
- if (view.runs.size === 0 && view.live.size === 0 && view.listeners.size === 0 && !view.queue) views.delete(viewId);
7591
+ if (view.poll && view.poll.until <= at && view.poll.wakes.size === 0) delete view.poll;
7592
+ if (view.runs.size === 0 && view.live.size === 0 && view.listeners.size === 0 && !view.poll) views.delete(viewId);
6897
7593
  }
6898
7594
  interestChanged();
6899
7595
  }, Math.min(5e3, Math.max(100, lease / 4)));
@@ -6939,24 +7635,34 @@ function createEphemeralBus(options = {}) {
6939
7635
  },
6940
7636
  drain(artifactId, viewId) {
6941
7637
  const view = viewOf(artifactId, viewId);
6942
- const taken = view.queue ?? [];
6943
- view.queue = [];
6944
- view.queueUntil = now() + lease;
7638
+ const taken = view.poll ? view.poll.messages.splice(0) : [];
7639
+ if (view.poll) view.poll.until = now() + lease;
7640
+ else view.poll = {
7641
+ messages: [],
7642
+ until: now() + lease,
7643
+ wakes: /* @__PURE__ */ new Set()
7644
+ };
6945
7645
  return taken;
6946
7646
  },
6947
7647
  wait(artifactId, viewId, ms, signal) {
6948
7648
  const view = viewOf(artifactId, viewId);
6949
- if (view.queue && view.queue.length > 0) return Promise.resolve();
7649
+ view.poll ??= {
7650
+ messages: [],
7651
+ until: now() + lease,
7652
+ wakes: /* @__PURE__ */ new Set()
7653
+ };
7654
+ if (view.poll.messages.length > 0) return Promise.resolve();
7655
+ const waiting = view.poll;
6950
7656
  return new Promise((resolve) => {
6951
7657
  const done = () => {
6952
7658
  clearTimeout(timer);
6953
7659
  signal?.removeEventListener("abort", done);
6954
- if (view.wake === done) delete view.wake;
7660
+ waiting.wakes.delete(done);
6955
7661
  resolve();
6956
7662
  };
6957
7663
  const timer = setTimeout(done, ms);
6958
7664
  signal?.addEventListener("abort", done, { once: true });
6959
- view.wake = done;
7665
+ waiting.wakes.add(done);
6960
7666
  });
6961
7667
  },
6962
7668
  liveInterest: currentInterest,
@@ -6969,7 +7675,7 @@ function createEphemeralBus(options = {}) {
6969
7675
  activeViews() {
6970
7676
  const at = now();
6971
7677
  let count = 0;
6972
- for (const view of views.values()) if (view.listeners.size > 0 || (view.queueUntil ?? 0) > at) count += 1;
7678
+ for (const view of views.values()) if (view.listeners.size > 0 || (view.poll?.until ?? 0) > at) count += 1;
6973
7679
  return count;
6974
7680
  },
6975
7681
  close() {
@@ -6991,6 +7697,13 @@ function createEphemeralBus(options = {}) {
6991
7697
  * const { submission } = interaction.submit({ artifactId, viewerId, viewId, expectedRevision, channel });
6992
7698
  * ```
6993
7699
  */
7700
+ /** The delivery events only the owner, its agent and hosts see on a hosted home (D165 item 1a). */
7701
+ const DELIVERY_EVENTS = /* @__PURE__ */ new Set([
7702
+ "submission.retrying",
7703
+ "submission.delivered",
7704
+ "submission.rejected",
7705
+ "submission.undeliverable"
7706
+ ]);
6994
7707
  function runNotFound(runId) {
6995
7708
  return new RpcError(createDiagnostic("MOSTRA_ACTION_RUN_NOT_FOUND", { message: `This artifact has no action run ${String(runId).slice(0, 64)}.` }));
6996
7709
  }
@@ -7073,6 +7786,25 @@ function runVisible(row, page) {
7073
7786
  }
7074
7787
  /** Creates the interaction core. */
7075
7788
  function createInteraction(options) {
7789
+ /** A submission as a page gets it: on a hosted home, sent and nothing about its delivery. */
7790
+ const forPage = (summary) => {
7791
+ if (!options.sentOnly) return summary;
7792
+ const { deliveries: _deliveries, ...sent } = summary;
7793
+ return {
7794
+ ...sent,
7795
+ delivery: "delivered"
7796
+ };
7797
+ };
7798
+ /** Runs a page's write, giving a conflict's submission as the page gets it. */
7799
+ const asPage = (write) => {
7800
+ try {
7801
+ return write();
7802
+ } catch (error) {
7803
+ if (options.sentOnly && error instanceof SubmitConflict) throw new SubmitConflict(error.diagnostic.message, forPage(error.submission));
7804
+ throw error;
7805
+ }
7806
+ };
7807
+ const tallyListeners = /* @__PURE__ */ new Set();
7076
7808
  const { ledger, log, hosts } = options;
7077
7809
  const outbox = new Outbox({
7078
7810
  ledger,
@@ -7080,7 +7812,8 @@ function createInteraction(options) {
7080
7812
  hosts,
7081
7813
  expected: options.expectedHosts,
7082
7814
  faults: options.faults,
7083
- ...options.portTimeoutMs !== void 0 ? { portTimeoutMs: options.portTimeoutMs } : {}
7815
+ ...options.portTimeoutMs !== void 0 ? { portTimeoutMs: options.portTimeoutMs } : {},
7816
+ ...options.deliverSnapshots ? { snapshots: true } : {}
7084
7817
  });
7085
7818
  const subscriptions = new Subscriptions({
7086
7819
  ledger,
@@ -7154,7 +7887,7 @@ function createInteraction(options) {
7154
7887
  replayed: true
7155
7888
  };
7156
7889
  if (input.scope === "viewer") {
7157
- assertDraftOpen(db, page.artifactId, page.viewerId);
7890
+ asPage(() => assertDraftOpen(db, page.artifactId, page.viewerId));
7158
7891
  if (input.ops.some((op) => CHOICE.test(op.id))) {
7159
7892
  if (page.scopes !== void 0 && !scopeAllows(page.scopes, "review")) throw new RpcError(refusal("MOSTRA_HYGIENE_SCOPE_DENIED", "This link doesn't carry the review scope.").diagnostic);
7160
7893
  assertChannel(view, "artifact");
@@ -7172,9 +7905,41 @@ function createInteraction(options) {
7172
7905
  if (outcome.changed && input.scope === "viewer") outbox.mirror(page.artifactId, page.viewerId);
7173
7906
  return result;
7174
7907
  },
7908
+ importSubmission(envelope, source) {
7909
+ const owners = options.deliveryOwners?.(envelope.artifactId) ?? outbox.owners();
7910
+ const answer = ledger.write((db) => importSubmission(db, {
7911
+ envelope,
7912
+ source,
7913
+ owners,
7914
+ owned: (inner, artifactId) => inner.get("SELECT 1 FROM hosted_artifact WHERE artifact_id = ? AND server = ?", artifactId, source.server) !== void 0
7915
+ }));
7916
+ log.info("interaction.imported", {
7917
+ artifactId: envelope.artifactId,
7918
+ submissionId: envelope.submissionId,
7919
+ accepted: answer.accepted
7920
+ });
7921
+ return answer;
7922
+ },
7923
+ envelopeFor(submissionId, owner) {
7924
+ return ledger.read((db) => {
7925
+ const delivery = db.get("SELECT id, attempts FROM delivery WHERE submission_id = ? AND port_owner = ?", submissionId, owner);
7926
+ const submission = db.get("SELECT * FROM submission WHERE id = ?", submissionId);
7927
+ if (!delivery || !submission) return void 0;
7928
+ return {
7929
+ kind: "submission",
7930
+ deliveryId: delivery.id,
7931
+ submissionId: submission.id,
7932
+ artifactId: submission.artifact_id,
7933
+ attempt: Math.max(1, delivery.attempts),
7934
+ results: buildResults(db, submission),
7935
+ snapshot: submission.snapshot_json,
7936
+ cursor: String(submission.position)
7937
+ };
7938
+ });
7939
+ },
7175
7940
  submit(page, input) {
7176
- const owners = outbox.owners();
7177
- const outcome = ledger.write((db) => submit(db, {
7941
+ const owners = options.deliveryOwners?.(page.artifactId) ?? outbox.owners();
7942
+ const outcome = asPage(() => ledger.write((db) => submit(db, {
7178
7943
  artifactId: page.artifactId,
7179
7944
  viewerId: page.viewerId,
7180
7945
  ...page.tokenId !== void 0 ? { tokenId: page.tokenId } : {},
@@ -7183,7 +7948,12 @@ function createInteraction(options) {
7183
7948
  channel: input.channel,
7184
7949
  owners,
7185
7950
  covers: options.coverage
7186
- }));
7951
+ })));
7952
+ if (outcome.tallied) for (const listener of tallyListeners) try {
7953
+ listener(page.artifactId, outcome.tallied);
7954
+ } catch (error) {
7955
+ log.error("interaction.tally_listener_failed", { error });
7956
+ }
7187
7957
  if (outcome.created) log.info("interaction.submitted", {
7188
7958
  artifactId: page.artifactId,
7189
7959
  submissionId: outcome.submission.id,
@@ -7193,11 +7963,11 @@ function createInteraction(options) {
7193
7963
  const row = db.get("SELECT * FROM submission WHERE id = ?", outcome.submission.id);
7194
7964
  return row ? submissionSummary(db, row) : void 0;
7195
7965
  });
7196
- if (!fresh) return outcome.submission;
7197
- return outcome.submission.duplicate ? {
7966
+ if (!fresh) return forPage(outcome.submission);
7967
+ return forPage(outcome.submission.duplicate ? {
7198
7968
  ...fresh,
7199
7969
  duplicate: true
7200
- } : fresh;
7970
+ } : fresh);
7201
7971
  },
7202
7972
  choose(page, input) {
7203
7973
  const actor = pageActor(page);
@@ -7226,7 +7996,7 @@ function createInteraction(options) {
7226
7996
  changed: false,
7227
7997
  replayed: true
7228
7998
  };
7229
- assertDraftOpen(db, page.artifactId, page.viewerId);
7999
+ asPage(() => assertDraftOpen(db, page.artifactId, page.viewerId));
7230
8000
  return applyMutation(db, mutation);
7231
8001
  });
7232
8002
  if (outcome.changed) outbox.mirror(page.artifactId, page.viewerId);
@@ -7282,7 +8052,7 @@ function createInteraction(options) {
7282
8052
  fields: fieldsForPage(state.fields, page.viewerId)
7283
8053
  };
7284
8054
  })(),
7285
- ...latestOwn ? { submission: submissionSummary(db, latestOwn) } : {},
8055
+ ...latestOwn ? { submission: forPage(submissionSummary(db, latestOwn)) } : {},
7286
8056
  ...(() => {
7287
8057
  const latest = db.get(`SELECT version.id, version.number FROM artifact
7288
8058
  JOIN version ON version.id = artifact.latest_version_id WHERE artifact.id = ?`, page.artifactId);
@@ -7296,6 +8066,7 @@ function createInteraction(options) {
7296
8066
  },
7297
8067
  pageEvents(page, after, limit) {
7298
8068
  return ledger.read((db) => {
8069
+ if (after > latestCursor(db)) throw new RpcError(createDiagnostic("MOSTRA_HOST_CURSOR_COMPACTED", { message: "The page's cursor is past this home's head; it reads a fresh snapshot." }));
7299
8070
  const events = [];
7300
8071
  let cursor = after;
7301
8072
  const runs = /* @__PURE__ */ new Map();
@@ -7315,6 +8086,7 @@ function createInteraction(options) {
7315
8086
  cursor: String(cursor)
7316
8087
  };
7317
8088
  cursor = Number(event.cursor);
8089
+ if (options.sentOnly && DELIVERY_EVENTS.has(event.type)) continue;
7318
8090
  if (visibleTo(event, page, runOf)) events.push(eventForPage(event, page.viewerId));
7319
8091
  }
7320
8092
  cursor = Math.max(cursor, Number(read.cursor));
@@ -7346,7 +8118,10 @@ function createInteraction(options) {
7346
8118
  mutationId: params.mutationId,
7347
8119
  ...params.baseRevision !== void 0 ? { baseRevision: Number(params.baseRevision) } : {},
7348
8120
  ops: params.ops,
7349
- actor: clientActor(session, params.as)
8121
+ actor: params.causation !== void 0 ? {
8122
+ kind: "agent",
8123
+ id: params.causation.runId
8124
+ } : clientActor(session, params.as)
7350
8125
  }).result);
7351
8126
  }),
7352
8127
  handler(resultsGetMethod, (params) => {
@@ -7386,7 +8161,7 @@ function createInteraction(options) {
7386
8161
  })
7387
8162
  ],
7388
8163
  connected(session) {
7389
- if (session.ports.has("submissions") && session.client) outbox.registered(session.client.name);
8164
+ if (session.ports.has("submissions") && session.client) outbox.registered(session.link?.instanceId ?? session.client.name);
7390
8165
  },
7391
8166
  disconnected(session) {
7392
8167
  subscriptions.closed(session.id);
@@ -7457,6 +8232,9 @@ function createInteraction(options) {
7457
8232
  openRuns(versionId) {
7458
8233
  return ledger.read((db) => openRuns(db, versionId));
7459
8234
  },
8235
+ runRow(runId) {
8236
+ return ledger.read((db) => db.get("SELECT * FROM action_run WHERE id = ?", runId));
8237
+ },
7460
8238
  runState(runId) {
7461
8239
  return ledger.read((db) => {
7462
8240
  const row = db.get("SELECT * FROM action_run WHERE id = ?", runId);
@@ -7548,6 +8326,10 @@ function createInteraction(options) {
7548
8326
  }
7549
8327
  });
7550
8328
  },
8329
+ onTallied(listener) {
8330
+ tallyListeners.add(listener);
8331
+ return () => tallyListeners.delete(listener);
8332
+ },
7551
8333
  subscriptionCount: () => subscriptions.size
7552
8334
  };
7553
8335
  }
@@ -7687,14 +8469,14 @@ async function openRegistry(options) {
7687
8469
  commit(input) {
7688
8470
  return ledger.write((db) => {
7689
8471
  const now = (/* @__PURE__ */ new Date()).toISOString();
7690
- if (input.manifestId !== void 0) {
8472
+ if (input.manifestId !== void 0 && input.bindingPath !== void 0) {
7691
8473
  const others = db.all("SELECT path FROM source_binding WHERE artifact_id = ? AND path != ?", input.manifestId, input.bindingPath);
7692
8474
  for (const { path } of others) {
7693
8475
  if (input.stillClaims(path, input.manifestId)) throw conflict(`${input.bindingPath} and ${path} both claim the artifact ID ${input.manifestId}.`);
7694
8476
  db.run("DELETE FROM source_binding WHERE path = ?", path);
7695
8477
  }
7696
8478
  }
7697
- if ((input.manifestId ?? boundArtifact(db, input.bindingPath) ?? input.artifactId) !== input.artifactId) throw new RpcError(createDiagnostic("MOSTRA_PUBLISH_SOURCE_CHANGED", { message: `The artifact ${input.bindingPath} belongs to changed while it was being published.` }));
8479
+ if ((input.manifestId ?? (input.bindingPath === void 0 ? void 0 : boundArtifact(db, input.bindingPath)) ?? input.artifactId) !== input.artifactId) throw new RpcError(createDiagnostic("MOSTRA_PUBLISH_SOURCE_CHANGED", { message: `The artifact ${input.bindingPath} belongs to changed while it was being published.` }));
7698
8480
  let artifact = db.get("SELECT * FROM artifact WHERE id = ?", input.artifactId);
7699
8481
  if (!artifact) {
7700
8482
  db.run("INSERT INTO artifact (id, title, family, links_json, policy_json, created_at, latest_version_id) VALUES (?, ?, ?, ?, ?, ?, NULL)", input.artifactId, input.manifest.title, input.manifest.family ?? null, JSON.stringify(input.manifest.links ?? []), null, now);
@@ -7717,7 +8499,7 @@ async function openRegistry(options) {
7717
8499
  if (artifact.latest_version_id === input.versionId) cursor = db.get("SELECT MAX(cursor) AS cursor FROM event WHERE artifact_id = ? AND type = 'version.published' AND json_extract(payload_json, '$.versionId') = ?", input.artifactId, input.versionId)?.cursor?.toString();
7718
8500
  } else {
7719
8501
  if (input.outputDigest === void 0) throw new Error("A new version needs its output digest.");
7720
- number = db.get("SELECT COALESCE(MAX(number), 0) + 1 AS next FROM version WHERE artifact_id = ?", input.artifactId)?.next ?? 1;
8502
+ number = input.number !== void 0 && db.get("SELECT 1 FROM version WHERE artifact_id = ? AND number = ?", input.artifactId, input.number) === void 0 && input.number !== void 0 ? input.number : db.get("SELECT COALESCE(MAX(number), 0) + 1 AS next FROM version WHERE artifact_id = ?", input.artifactId)?.next ?? 1;
7721
8503
  db.run("INSERT INTO version (id, artifact_id, number, source_digest, env_digest, output_digest, manifest_json, forced_codes_json, published_at, verify_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", input.versionId, input.artifactId, number, input.sourceDigest, input.envDigest, input.outputDigest, JSON.stringify(input.manifest), input.forcedCodes.length > 0 ? JSON.stringify(input.forcedCodes) : null, now, input.verify ? JSON.stringify(input.verify) : null);
7722
8504
  }
7723
8505
  if (cursor === void 0) cursor = appendEvent(db, {
@@ -7734,7 +8516,7 @@ async function openRegistry(options) {
7734
8516
  const chosen = input.policy ?? (artifact.policy_chosen_json === null ? void 0 : JSON.parse(artifact.policy_chosen_json));
7735
8517
  const policy = policyInForce(chosen, input.homePolicy, manifest.submission);
7736
8518
  db.run("UPDATE artifact SET title = ?, family = ?, links_json = ?, policy_json = ?, policy_chosen_json = ?, latest_version_id = ? WHERE id = ?", manifest.title, manifest.family ?? null, JSON.stringify(manifest.links ?? []), JSON.stringify(policy), chosen === void 0 ? null : JSON.stringify(chosen), input.versionId, input.artifactId);
7737
- db.run("INSERT INTO source_binding (path, artifact_id, bound_at) VALUES (?, ?, ?) ON CONFLICT(path) DO UPDATE SET artifact_id = excluded.artifact_id, bound_at = excluded.bound_at", input.bindingPath, input.artifactId, now);
8519
+ if (input.bindingPath !== void 0) db.run("INSERT INTO source_binding (path, artifact_id, bound_at) VALUES (?, ?, ?) ON CONFLICT(path) DO UPDATE SET artifact_id = excluded.artifact_id, bound_at = excluded.bound_at", input.bindingPath, input.artifactId, now);
7738
8520
  input.inTransaction?.(db);
7739
8521
  const committed = {
7740
8522
  version: number,
@@ -8180,13 +8962,26 @@ const STATUS$1 = {
8180
8962
  MOSTRA_RUNTIME_WORKER_CRASHED: 503,
8181
8963
  MOSTRA_VERIFY_ACTION_SKIPPED: 409,
8182
8964
  MOSTRA_HOME_DISK_FULL: 507,
8183
- MOSTRA_HOME_GONE: 503
8965
+ MOSTRA_HOME_GONE: 503,
8966
+ MOSTRA_REMOTE_OWNER_OFFLINE: 503,
8967
+ MOSTRA_REMOTE_QUEUE_FULL: 503,
8968
+ MOSTRA_REMOTE_SIGN_IN_REQUIRED: 403,
8969
+ MOSTRA_REMOTE_RATE_LIMITED: 429,
8970
+ MOSTRA_ACTION_PAUSED: 423
8184
8971
  };
8185
8972
  function answer$1(diagnostic, status) {
8186
8973
  return jsonResponse({ ...diagnostic }, { status });
8187
8974
  }
8188
8975
  function failure$1(error) {
8189
- if (error instanceof RpcError) return answer$1(error.diagnostic, STATUS$1[error.diagnostic.code] ?? 500);
8976
+ if (error instanceof RpcError) {
8977
+ const status = STATUS$1[error.diagnostic.code] ?? 500;
8978
+ const retryAfter = error.retryAfter;
8979
+ if (typeof retryAfter === "number") return jsonResponse({ ...error.diagnostic }, {
8980
+ status,
8981
+ headers: { "retry-after": String(retryAfter) }
8982
+ });
8983
+ return answer$1(error.diagnostic, status);
8984
+ }
8190
8985
  throw error;
8191
8986
  }
8192
8987
  function denied(fnId) {
@@ -8610,7 +9405,13 @@ function browserRoutes(options) {
8610
9405
  "x-accel-buffering": "no"
8611
9406
  } });
8612
9407
  };
8613
- const poll = async (context) => {
9408
+ /**
9409
+ * `events/poll`. The frame's poll also answers with the run and live updates its view collected
9410
+ * (LL-04); the shell's poll of the same view leaves them to the frame, which is the one that shows
9411
+ * them: taking them made a page on a quick tunnel learn of a finished run only at its next renewal,
9412
+ * about 20 s later (#119).
9413
+ */
9414
+ const pollFor = (ephemeral) => async (context) => {
8614
9415
  const refused = knownRevision(context);
8615
9416
  if (refused) return refused;
8616
9417
  const page = pageOf(context);
@@ -8622,7 +9423,7 @@ function browserRoutes(options) {
8622
9423
  const waitS = waitParam === null ? 25 : Number(waitParam);
8623
9424
  if (!Number.isInteger(waitS) || waitS < 0 || waitS > 25) return malformed(`\`wait\` must be a whole number of seconds from 0 to 25.`);
8624
9425
  const viewId = viewIdOf(context);
8625
- const bus = options.bus;
9426
+ const bus = ephemeral ? options.bus : void 0;
8626
9427
  let early = bus?.drain(page.artifactId, viewId) ?? [];
8627
9428
  const withEphemeral = (value) => {
8628
9429
  const ephemeral = [...early, ...bus?.drain(page.artifactId, viewId) ?? []];
@@ -8790,7 +9591,8 @@ function browserRoutes(options) {
8790
9591
  submit,
8791
9592
  signal,
8792
9593
  stream,
8793
- poll,
9594
+ poll: pollFor(true),
9595
+ shellPoll: pollFor(false),
8794
9596
  shellSubmit,
8795
9597
  choose,
8796
9598
  follow,
@@ -8808,7 +9610,7 @@ function browserRoutes(options) {
8808
9610
  * for. Every check reads the ledger, so a revocation applies to the very next request.
8809
9611
  */
8810
9612
  /** Crockford base32, the alphabet of every Mostra ID body (D77). */
8811
- const CROCKFORD$2 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
9613
+ const CROCKFORD$3 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
8812
9614
  /** How long a frame ticket works (D98). */
8813
9615
  const TICKET_TTL_MS = 6e4;
8814
9616
  /** How often expired tickets are swept at most. */
@@ -8837,7 +9639,7 @@ function crockford(bytes) {
8837
9639
  value = (value << 8 | byte) & 65535;
8838
9640
  bits += 8;
8839
9641
  while (bits >= 5 && out.length < 26) {
8840
- out += CROCKFORD$2[value >>> bits - 5 & 31];
9642
+ out += CROCKFORD$3[value >>> bits - 5 & 31];
8841
9643
  bits -= 5;
8842
9644
  }
8843
9645
  if (out.length === 26) break;
@@ -9392,7 +10194,7 @@ var Capabilities = class {
9392
10194
  * which nothing else holds. Mostra's cookies never go upstream (D38).
9393
10195
  */
9394
10196
  /** Request headers the gateway passes to the dev server; everything else stays (cookies above all). */
9395
- const FORWARDED = [
10197
+ const FORWARDED$1 = [
9396
10198
  "accept",
9397
10199
  "accept-language",
9398
10200
  "if-none-match",
@@ -9411,20 +10213,20 @@ const RETURNED = [
9411
10213
  "content-length"
9412
10214
  ];
9413
10215
  /** Fetches `path` from the dev server; resolves with its status, headers and body. */
9414
- function devFetch(upstream, request$1, path) {
10216
+ function devFetch(upstream, request$2, path) {
9415
10217
  const headers = {
9416
10218
  host: `127.0.0.1:${upstream.port}`,
9417
10219
  [DEV_SECRET_HEADER]: upstream.secret
9418
10220
  };
9419
- for (const name of FORWARDED) {
9420
- const value = request$1.headers.get(name);
10221
+ for (const name of FORWARDED$1) {
10222
+ const value = request$2.headers.get(name);
9421
10223
  if (value !== null) headers[name] = value;
9422
10224
  }
9423
10225
  return new Promise((resolve, reject) => {
9424
10226
  const outgoing = request({
9425
10227
  host: "127.0.0.1",
9426
10228
  port: upstream.port,
9427
- method: request$1.method,
10229
+ method: request$2.method,
9428
10230
  path,
9429
10231
  headers,
9430
10232
  agent: false
@@ -9782,7 +10584,7 @@ function mount(app, checks, routes) {
9782
10584
  if (route.sse && (checks.streams?.() === false || mode === "worker" && ticket === null)) return refuse(checks, refusal("MOSTRA_HYGIENE_NOT_SERVED", checks.streams?.() === false ? "Event streams don't pass this address; long-poll instead (events/poll)." : "A worker-mode frame opens its event stream with a ticket; long-poll instead.", 503), meta);
9783
10585
  let session;
9784
10586
  if (route.auth === "session" && artifactId !== void 0) {
9785
- const resolved = ticket !== null ? checks.capabilities.redeemStreamTicket(ticket, policy.origin, artifactId, "stream") : mode === "worker" ? checks.capabilities.session([request.headers.get(FRAME_SESSION_HEADER) ?? ""], policy.origin, artifactId, "worker") : checks.capabilities.session(cookieValues(request.headers.get("cookie"), checks.cookieName()), policy.origin, artifactId);
10587
+ const resolved = ticket !== null ? checks.capabilities.redeemStreamTicket(ticket, policy.origin, artifactId, "stream") : mode === "worker" ? checks.capabilities.session([request.headers.get(FRAME_SESSION_HEADER) ?? ""], policy.origin, artifactId, "worker") : mode === "header" ? checks.capabilities.session([request.headers.get(SHELL_SESSION_HEADER) ?? ""], policy.origin, artifactId, "header") : checks.capabilities.session(cookieValues(request.headers.get("cookie"), checks.cookieName()), policy.origin, artifactId);
9786
10588
  if (isRefusal(resolved)) return refuse(checks, resolved, meta);
9787
10589
  session = resolved;
9788
10590
  if (route.intent === "mutation") {
@@ -10583,7 +11385,9 @@ function createGateway(options) {
10583
11385
  revisions: options.revisions ?? BROWSER_PROTOCOL_REVISIONS
10584
11386
  }) : void 0;
10585
11387
  let shellPort;
11388
+ const hosted = options.hosted;
10586
11389
  const shellOrigin = () => {
11390
+ if (hosted) return hosted.shellOrigin();
10587
11391
  if (shellPort === void 0) throw new Error("The gateway hasn't started.");
10588
11392
  return addressing.origin(shellPort);
10589
11393
  };
@@ -10642,7 +11446,7 @@ function createGateway(options) {
10642
11446
  /** Whether an origin is `https:`, where every cookie is `Secure`. */
10643
11447
  const secure = (origin) => origin?.startsWith("https:") === true;
10644
11448
  /** Whether the gateway serves a shell at this origin now: launch tickets go only there. */
10645
- const servesShell = (origin) => origin === shellOrigin() || [...faces.values()].some((entry) => entry.spec.listener === "shell" && entry.origin === origin);
11449
+ const servesShell = (origin) => (hosted ? hosted.servesShell(origin) : origin === shellOrigin()) || [...faces.values()].some((entry) => entry.spec.listener === "shell" && entry.origin === origin);
10646
11450
  /** The answer for a damaged version: a plain page for a navigation, else the diagnostic. */
10647
11451
  const damaged = (number, damage, document, frameAncestors) => refusalResponse({
10648
11452
  status: 500,
@@ -10652,7 +11456,7 @@ function createGateway(options) {
10652
11456
  frameAncestors
10653
11457
  });
10654
11458
  const link = (token) => ({
10655
- url: `${token.shellOrigin}/a/${token.artifactId}${token.pinnedVersion !== void 0 ? `/v/${token.pinnedVersion}` : ""}#t=${token.secret}`,
11459
+ url: `${token.shellOrigin}${hosted ? hosted.shellPath(token.artifactId) : `/a/${token.artifactId}`}${token.pinnedVersion !== void 0 ? `/v/${token.pinnedVersion}` : ""}#t=${token.secret}`,
10656
11460
  tokenId: token.tokenId,
10657
11461
  scopes: token.scopes,
10658
11462
  ...token.expiresAt !== void 0 ? { expiresAt: token.expiresAt } : {},
@@ -10698,7 +11502,7 @@ function createGateway(options) {
10698
11502
  ...options.traceRequests ? { traceRequests: true } : {}
10699
11503
  });
10700
11504
  await endLingering(key);
10701
- const port = await addressing.listenFace(listener.server, {
11505
+ const port = spec.by === "hosted" ? 0 : await addressing.listenFace(listener.server, {
10702
11506
  key,
10703
11507
  lease: spec.lease
10704
11508
  });
@@ -10866,6 +11670,24 @@ function createGateway(options) {
10866
11670
  group: void 0
10867
11671
  };
10868
11672
  }
11673
+ if (shell.by === "hosted") {
11674
+ const origin = hosted?.artifactOrigin(artifactId, shell.origin() ?? "");
11675
+ if (origin === void 0) return refusal("MOSTRA_HYGIENE_NOT_SERVED", "This artifact isn't hosted here.");
11676
+ const entry = await openFace({
11677
+ by: "hosted",
11678
+ group,
11679
+ listener: "artifact",
11680
+ artifactId,
11681
+ via: "token",
11682
+ lease: false,
11683
+ mode: "worker"
11684
+ }, origin);
11685
+ entry.origin = origin;
11686
+ return {
11687
+ way: faceWay(entry),
11688
+ group
11689
+ };
11690
+ }
10869
11691
  if (shell.by === "host") {
10870
11692
  const entry = await hostFace(artifactId, "artifact");
10871
11693
  if (entry?.origin === void 0) return refusal("MOSTRA_HYGIENE_NOT_SERVED", "The host gives this artifact's frame no address here.", 503);
@@ -10894,6 +11716,7 @@ function createGateway(options) {
10894
11716
  capabilities,
10895
11717
  log,
10896
11718
  cookieName: () => `mostra-${way().port()}`,
11719
+ mode: () => way().by === "hosted" ? "header" : "cookie",
10897
11720
  frameAncestors: () => "'none'",
10898
11721
  artifactOf: (params) => {
10899
11722
  const id = params["artifactId"];
@@ -10996,7 +11819,8 @@ function createGateway(options) {
10996
11819
  const browser = browserKey(artifactId, body["browser"]);
10997
11820
  let grant = launch ? capabilities.presentLaunch(body["launch"], artifactId, false, origin) : capabilities.presentToken(body["token"], artifactId, origin);
10998
11821
  if (isRefusal(grant)) return refuse(checks, grant, meta);
10999
- const existing = capabilities.session(cookieValues(context.request.headers.get("cookie"), checks.cookieName()), origin, artifactId);
11822
+ const inHeader = way.by === "hosted";
11823
+ const existing = inHeader ? capabilities.session([context.request.headers.get(SHELL_SESSION_HEADER) ?? ""], origin, artifactId, "header") : capabilities.session(cookieValues(context.request.headers.get("cookie"), checks.cookieName()), origin, artifactId);
11000
11824
  if (!isRefusal(existing)) {
11001
11825
  if (existing.grant.tokenId === grant.tokenId) {
11002
11826
  if (launch) capabilities.spendLaunch(body["launch"]);
@@ -11051,7 +11875,8 @@ function createGateway(options) {
11051
11875
  const session = capabilities.createSession({
11052
11876
  tokenId: grant.tokenId,
11053
11877
  origin,
11054
- viewerId
11878
+ viewerId,
11879
+ ...inHeader ? { mode: "header" } : {}
11055
11880
  });
11056
11881
  if (browser !== void 0) {
11057
11882
  exchangeOrder += 1;
@@ -11067,6 +11892,12 @@ function createGateway(options) {
11067
11892
  artifactId,
11068
11893
  tokenId: grant.tokenId
11069
11894
  });
11895
+ if (inHeader) return jsonResponse({
11896
+ csrf: session.csrf,
11897
+ session: session.cookie,
11898
+ hosted: true,
11899
+ ...sessionFacts(artifactId, grant)
11900
+ });
11070
11901
  return jsonResponse({
11071
11902
  csrf: session.csrf,
11072
11903
  ...sessionFacts(artifactId, grant)
@@ -11246,7 +12077,7 @@ function createGateway(options) {
11246
12077
  path: "/a/:artifactId/_mostra/v/:rev/events/poll",
11247
12078
  intent: "stream",
11248
12079
  auth: "session",
11249
- handler: served.poll
12080
+ handler: served.shellPoll
11250
12081
  },
11251
12082
  {
11252
12083
  method: "POST",
@@ -11708,6 +12539,12 @@ function createGateway(options) {
11708
12539
  async openFace(spec) {
11709
12540
  return handleOf(await openFace(spec));
11710
12541
  },
12542
+ dispatch(key, request, response) {
12543
+ const entry = faces.get(key);
12544
+ if (!entry) return false;
12545
+ entry.listener.server.emit("request", request, response);
12546
+ return true;
12547
+ },
11711
12548
  face(key) {
11712
12549
  const entry = faces.get(key);
11713
12550
  return entry ? handleOf(entry) : void 0;
@@ -11804,6 +12641,18 @@ async function verifyDocument(path, request, headers) {
11804
12641
  * instance still starts, artifact frames are still served, and the shell's pages answer
11805
12642
  * `MOSTRA_ENV_SHELL_MISSING`.
11806
12643
  */
12644
+ /**
12645
+ * Answers a request for one of the shell's own built files (`/_mostra/shell/assets/*`): a remote
12646
+ * server's front serves them on every user-content domain's apex before any hosted home is
12647
+ * known, since they are the same for every route (INTERFACES §18.4).
12648
+ */
12649
+ async function shellAssetResponse(shellDir, request) {
12650
+ const path = safePath(new URL(request.url).pathname);
12651
+ if (path === void 0 || !path.startsWith("/_mostra/shell/assets/")) return void 0;
12652
+ const file = await findFile(shellDir, path.slice(14));
12653
+ if (!file) return void 0;
12654
+ return fileResponse(file, request, { "cache-control": "public, max-age=31536000, immutable" });
12655
+ }
11807
12656
  function locateShell(env) {
11808
12657
  const dir = env["MOSTRA_SHELL_DIR"] ?? resolve(runtimeRoot$1(), "..", "shell", "dist");
11809
12658
  return existsSync(join(dir, "index.html")) ? dir : void 0;
@@ -11848,6 +12697,7 @@ function createLive(options) {
11848
12697
  message: `The artifact's server code keeps crashing, so its live sources stopped (${cause}).`
11849
12698
  });
11850
12699
  let timer;
12700
+ let stopTallies;
11851
12701
  let scheduled = false;
11852
12702
  let stopped = false;
11853
12703
  /** A worker is starting only to say which of its version's sources are `always`. */
@@ -11911,6 +12761,7 @@ function createLive(options) {
11911
12761
  });
11912
12762
  };
11913
12763
  for (const entry of bus.liveInterest()) add(entry.artifactId, entry.key);
12764
+ for (const entry of options.extraInterest?.() ?? []) add(entry.artifactId, entry.key);
11914
12765
  const subscribed = options.interaction.subscribedArtifacts();
11915
12766
  for (const artifactId of options.artifactsWithLive()) {
11916
12767
  const latest = options.latest(artifactId);
@@ -12018,6 +12869,15 @@ function createLive(options) {
12018
12869
  });
12019
12870
  };
12020
12871
  const stateOf = (db, artifactId, key, window) => {
12872
+ const field = tallyField(key);
12873
+ if (field !== void 0) {
12874
+ const tally = tallyState(db, artifactId, field);
12875
+ return tally ? {
12876
+ key,
12877
+ revision: tally.revision,
12878
+ value: tally.batches
12879
+ } : { key };
12880
+ }
12021
12881
  const row = db.get("SELECT revision, value_json, at FROM live_value WHERE artifact_id = ? AND key = ?", artifactId, key);
12022
12882
  const failure = failures.get(keyOf(artifactId, key));
12023
12883
  const latest = options.latest(artifactId);
@@ -12055,6 +12915,7 @@ function createLive(options) {
12055
12915
  return ledger.read((db) => input.keys.map((entry) => stateOf(db, page.artifactId, entry.key, entry.window)));
12056
12916
  },
12057
12917
  async push(artifactId, input, requestKey) {
12918
+ if (tallyField(input.key) !== void 0) throw new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message: `${input.key} is a tally's key: only accepted submissions count it.` }));
12058
12919
  let batches;
12059
12920
  try {
12060
12921
  batches = await serializeAll(input.value, { mode: "argument" });
@@ -12083,12 +12944,31 @@ function createLive(options) {
12083
12944
  publishValue(artifactId, input.key, stored.revision, stored.at, batches);
12084
12945
  return answer;
12085
12946
  },
12947
+ relayed(artifactId, versionId, produced) {
12948
+ if (produced.error) {
12949
+ fail(artifactId, produced.key, produced.error);
12950
+ return {};
12951
+ }
12952
+ const value = produced.value;
12953
+ if (!value) return {};
12954
+ if (JSON.stringify(value).length > LIVE_VALUE_MAX) throw tooLarge(produced.key);
12955
+ const stored = ledger.write((db) => store(db, artifactId, produced.key, value, versionId, ringOf(produced.key, versionId)));
12956
+ failures.delete(keyOf(artifactId, produced.key));
12957
+ publishValue(artifactId, produced.key, stored.revision, stored.at, value);
12958
+ return { revision: String(stored.revision) };
12959
+ },
12086
12960
  produced(worker, params) {
12087
12961
  const parsed = ServerLiveValueParams.safeParse(params);
12088
12962
  if (!parsed.success) return;
12089
12963
  const { key } = parsed.data;
12090
12964
  const producer = running.get(keyOf(worker.artifactId, key));
12091
12965
  if (!producer || producer.versionId !== worker.versionId) return;
12966
+ const outgoing = () => ({
12967
+ key,
12968
+ ...parsed.data.value !== void 0 ? { value: redactBatches(parsed.data.value) } : {},
12969
+ ...parsed.data.error !== void 0 ? { error: options.redactor ? redactValue(parsed.data.error, options.redactor) : parsed.data.error } : {}
12970
+ });
12971
+ if (options.forward?.(worker.artifactId, worker.versionId, outgoing)) return;
12092
12972
  if (parsed.data.error) {
12093
12973
  fail(worker.artifactId, key, parsed.data.error);
12094
12974
  return;
@@ -12160,6 +13040,29 @@ function createLive(options) {
12160
13040
  },
12161
13041
  start() {
12162
13042
  stopped = false;
13043
+ stopTallies ??= options.interaction.onTallied((artifactId, fields) => {
13044
+ for (const field of fields) {
13045
+ const key = `${TALLY_LIVE_PREFIX}${field}`;
13046
+ try {
13047
+ const tally = ledger.read((db) => tallyState(db, artifactId, field));
13048
+ if (!tally) continue;
13049
+ bus.publishLive(artifactId, {
13050
+ kind: "live",
13051
+ live: {
13052
+ key,
13053
+ revision: tally.revision,
13054
+ at: (/* @__PURE__ */ new Date()).toISOString(),
13055
+ value: tally.batches
13056
+ }
13057
+ });
13058
+ } catch (error) {
13059
+ log.error("live.tally_failed", {
13060
+ key,
13061
+ error
13062
+ });
13063
+ }
13064
+ }
13065
+ });
12163
13066
  bus.onLiveInterest(schedule);
12164
13067
  timer = setInterval(schedule, options.reconcileMs ?? 5e3);
12165
13068
  timer.unref?.();
@@ -12168,6 +13071,8 @@ function createLive(options) {
12168
13071
  stop() {
12169
13072
  stopped = true;
12170
13073
  if (timer !== void 0) clearInterval(timer);
13074
+ stopTallies?.();
13075
+ stopTallies = void 0;
12171
13076
  }
12172
13077
  };
12173
13078
  }
@@ -14340,6 +15245,8 @@ function validate(raw, x, at) {
14340
15245
  const path = issue?.path.join(".") || "(manifest)";
14341
15246
  return x.fail("MOSTRA_MANIFEST_INVALID", `The manifest field \`${path}\` is invalid: ${issue?.message ?? "unexpected value"}.`, at);
14342
15247
  }
15248
+ const problem = manifestProblem(checked.data);
15249
+ if (problem) return x.fail("MOSTRA_MANIFEST_INVALID", `The manifest field \`${problem.path}\` is invalid: ${problem.message}.`, at);
14343
15250
  return {
14344
15251
  manifest: checked.data,
14345
15252
  warnings
@@ -14412,6 +15319,344 @@ function extractManifest(file, text) {
14412
15319
  return file.endsWith(".mdx") ? extractFrontmatter(file, text) : extractScript(file, text);
14413
15320
  }
14414
15321
  //#endregion
15322
+ //#region src/publish/tar.ts
15323
+ /**
15324
+ * A minimal, deterministic POSIX tar (pax interchange format) for source snapshots (DATA_MODEL §1):
15325
+ * regular files only, in the order given, with mode 0644 or 0755, owner 0, modification time 0 and
15326
+ * a pax `path` record for any path longer than 100 bytes or outside ASCII. The same tree always
15327
+ * gives the same bytes. {@link readTar} reads what {@link tarFile} writes.
15328
+ */
15329
+ const BLOCK = 512;
15330
+ function octal(value, width) {
15331
+ return `${value.toString(8).padStart(width - 1, "0")}\0`;
15332
+ }
15333
+ function header(name, size, type, mode) {
15334
+ const block = Buffer.alloc(BLOCK, 0);
15335
+ block.write(name, 0, 100, "utf8");
15336
+ block.write(octal(mode, 8), 100, 8, "ascii");
15337
+ block.write(octal(0, 8), 108, 8, "ascii");
15338
+ block.write(octal(0, 8), 116, 8, "ascii");
15339
+ block.write(octal(size, 12), 124, 12, "ascii");
15340
+ block.write(octal(0, 12), 136, 12, "ascii");
15341
+ block.write(" ", 148, 8, "ascii");
15342
+ block.write(type, 156, 1, "ascii");
15343
+ block.write("ustar\0", 257, 6, "ascii");
15344
+ block.write("00", 263, 2, "ascii");
15345
+ let sum = 0;
15346
+ for (const byte of block) sum += byte;
15347
+ block.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8, "ascii");
15348
+ return block;
15349
+ }
15350
+ function padding(size) {
15351
+ const rest = size % BLOCK;
15352
+ return Buffer.alloc(rest === 0 ? 0 : BLOCK - rest, 0);
15353
+ }
15354
+ /** A pax record `"<length> path=<value>\n"`, where the length counts the whole record. */
15355
+ function paxRecord(key, value) {
15356
+ const body = ` ${key}=${value}\n`;
15357
+ const bodyLength = Buffer.byteLength(body, "utf8");
15358
+ let length = bodyLength + 1;
15359
+ while (String(length).length + bodyLength !== length) length = String(length).length + bodyLength;
15360
+ return Buffer.from(`${length}${body}`, "utf8");
15361
+ }
15362
+ /** The archive blocks of one file: an optional pax header, the header, the data and padding. */
15363
+ function tarFile(file) {
15364
+ const mode = file.executable ? 493 : 420;
15365
+ const blocks = [];
15366
+ const plain = /^[\x20-\x7e]*$/.test(file.path) && Buffer.byteLength(file.path) <= 100;
15367
+ if (!plain) {
15368
+ const record = paxRecord("path", file.path);
15369
+ blocks.push(header("PaxHeader", record.length, "x", 420), record, padding(record.length));
15370
+ }
15371
+ const name = plain ? file.path : file.path.replace(/[^\x20-\x7e]/g, "_").slice(-100);
15372
+ blocks.push(header(name, file.data.length, "0", mode), Buffer.from(file.data), padding(file.data.length));
15373
+ return blocks;
15374
+ }
15375
+ /** The end of an archive: two zero blocks. */
15376
+ const TAR_END = Buffer.alloc(BLOCK * 2, 0);
15377
+ /** Reads an archive written by {@link tarFile}. Throws on anything malformed. */
15378
+ function readTar(archive) {
15379
+ const buffer = Buffer.from(archive.buffer, archive.byteOffset, archive.byteLength);
15380
+ const files = [];
15381
+ let offset = 0;
15382
+ let paxPath;
15383
+ while (offset + BLOCK <= buffer.length) {
15384
+ const block = buffer.subarray(offset, offset + BLOCK);
15385
+ if (block.every((byte) => byte === 0)) break;
15386
+ const field = (start, length) => block.subarray(start, start + length).toString("latin1").replace(/\0.*$/s, "");
15387
+ const size = Number.parseInt(field(124, 12), 8);
15388
+ const type = field(156, 1);
15389
+ if (!Number.isSafeInteger(size) || size < 0) throw new Error(`Malformed tar header at ${offset}.`);
15390
+ const dataStart = offset + BLOCK;
15391
+ const data = buffer.subarray(dataStart, dataStart + size);
15392
+ if (data.length !== size) throw new Error("Truncated tar archive.");
15393
+ offset = dataStart + size + padding(size).length;
15394
+ if (type === "x") {
15395
+ paxPath = /(?:^|\n)\d+ path=([^\n]*)\n/.exec(data.toString("utf8"))?.[1];
15396
+ continue;
15397
+ }
15398
+ if (type !== "0" && type !== "") throw new Error(`Unexpected tar entry type "${type}".`);
15399
+ const path = paxPath ?? field(0, 100);
15400
+ paxPath = void 0;
15401
+ const mode = Number.parseInt(field(100, 8), 8);
15402
+ files.push({
15403
+ path,
15404
+ executable: (mode & 73) !== 0,
15405
+ data: new Uint8Array(data)
15406
+ });
15407
+ }
15408
+ return files;
15409
+ }
15410
+ //#endregion
15411
+ //#region src/publish/bundle.ts
15412
+ /**
15413
+ * The `bundle` export class (ARCHITECTURE §3, INTERFACES §15, RM-01): the portable artifact package.
15414
+ * A bundle is one published version, whole: its version directory (`manifest.json`, the client and
15415
+ * server builds, `registry.json`, `lock.yaml`), its source snapshot, and `mostra-bundle.json`,
15416
+ * which names them. Importing it into another home (`artifact/import`, `mostra run <bundle>`)
15417
+ * makes the same version there: the same artifact ID, version ID, digests and, when free, number,
15418
+ * because a version's identity is its artifact ID and digests (D89), and each is checked against
15419
+ * the bundle's own bytes before anything is kept. Its server code runs in the importing home's
15420
+ * workers, against the bundle's lockfile, which that home's launcher installs into its own layout
15421
+ * before a worker starts (D135).
15422
+ *
15423
+ * ```text
15424
+ * <bundle>/ or <bundle>.tar.zst, the same tree in one archive
15425
+ * mostra-bundle.json { schemaVersion, kind, artifactId, versionId, number, title, digests… }
15426
+ * version/ the version directory, as the home stores it
15427
+ * source.tar.zst the source snapshot (DATA_MODEL §1)
15428
+ * ```
15429
+ */
15430
+ const INFO = "mostra-bundle.json";
15431
+ const VERSION = "version";
15432
+ const SOURCE = "source.tar.zst";
15433
+ /** The largest bundle archive read whole into memory: a version's builds and source. */
15434
+ const ARCHIVE_MAX = 2147483648;
15435
+ /** JSON with sorted keys, to compare two manifests. */
15436
+ function canonical$1(value) {
15437
+ return JSON.stringify(value, (_key, inner) => inner !== null && typeof inner === "object" && !Array.isArray(inner) ? Object.fromEntries(Object.entries(inner).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) : inner);
15438
+ }
15439
+ function invalid$2(message) {
15440
+ return new RpcError(createDiagnostic("MOSTRA_ARTIFACT_BUNDLE_INVALID", { message }));
15441
+ }
15442
+ /** Every file under `dir`, as `/`-separated paths relative to it, sorted. */
15443
+ async function filesUnder(dir) {
15444
+ const out = [];
15445
+ const walk = async (current) => {
15446
+ for (const entry of await readdir(current, { withFileTypes: true })) {
15447
+ const path = join(current, entry.name);
15448
+ if (entry.isDirectory()) await walk(path);
15449
+ else if (entry.isFile()) out.push(relative(dir, path).split(/[\\/]/).join("/"));
15450
+ else throw invalid$2(`${relative(dir, path)} isn't a file or a directory.`);
15451
+ }
15452
+ };
15453
+ await walk(dir);
15454
+ return out.sort();
15455
+ }
15456
+ /** The canonical listing of a version directory's output, as `outputDigest` hashes it. */
15457
+ async function outputDigestOf(versionDir) {
15458
+ const files = await filesUnder(versionDir);
15459
+ const listed = [];
15460
+ for (const path of files) if (path === "registry.json" || path.startsWith("client/") || path.startsWith("server/")) listed.push({
15461
+ path,
15462
+ executable: false,
15463
+ sha256: sha256Hex(await readFile(join(versionDir, ...path.split("/"))))
15464
+ });
15465
+ return listingDigest(listed);
15466
+ }
15467
+ /**
15468
+ * Writes the bundle of the version stored in `versionDir`, with its source snapshot `snapshot`,
15469
+ * to `out`: a directory, or one archive when `out` ends in `.tar.zst`. `out` must not exist, or be
15470
+ * an empty directory (`MOSTRA_ARTIFACT_DIR_NOT_EMPTY`).
15471
+ */
15472
+ async function writeBundle(input) {
15473
+ const archive = input.out.endsWith(".tar.zst");
15474
+ if (existsSync(input.out)) {
15475
+ if (!(!archive && (await stat(input.out)).isDirectory() && (await readdir(input.out)).length === 0)) throw new RpcError(createDiagnostic("MOSTRA_ARTIFACT_DIR_NOT_EMPTY", { message: `${input.out} already exists; a bundle is written only to a new path or an empty directory.` }));
15476
+ }
15477
+ const manifest = JSON.parse(await readFile(join(input.versionDir, "manifest.json"), "utf8"));
15478
+ const info = {
15479
+ schemaVersion: 1,
15480
+ kind: "mostra-bundle",
15481
+ artifactId: manifest.artifactId,
15482
+ versionId: manifest.versionId,
15483
+ number: input.number,
15484
+ title: input.title,
15485
+ sourceDigest: manifest.sourceDigest,
15486
+ envDigest: manifest.envDigest,
15487
+ outputDigest: manifest.outputDigest,
15488
+ lockDigest: manifest.environment.lockDigest,
15489
+ server: manifest.server,
15490
+ mostraVersion: input.mostraVersion,
15491
+ exportedAt: (/* @__PURE__ */ new Date()).toISOString()
15492
+ };
15493
+ const entries = [{
15494
+ path: INFO,
15495
+ data: Buffer.from(`${JSON.stringify(info, null, 2)}\n`)
15496
+ }];
15497
+ for (const path of await filesUnder(input.versionDir)) entries.push({
15498
+ path: `${VERSION}/${path}`,
15499
+ data: await readFile(join(input.versionDir, ...path.split("/")))
15500
+ });
15501
+ entries.push({
15502
+ path: SOURCE,
15503
+ data: await readFile(input.snapshot)
15504
+ });
15505
+ if (archive) {
15506
+ await mkdir(dirname(input.out), { recursive: true });
15507
+ const blocks = [];
15508
+ for (const entry of entries) blocks.push(...tarFile({
15509
+ ...entry,
15510
+ executable: false
15511
+ }));
15512
+ blocks.push(TAR_END);
15513
+ const temporary = `${input.out}.${process.pid}.tmp`;
15514
+ await writeFile(temporary, zstdCompressSync(Buffer.concat(blocks)));
15515
+ fsyncPath(temporary);
15516
+ await renamePath(temporary, input.out);
15517
+ fsyncPath(dirname(input.out), true);
15518
+ return [input.out];
15519
+ }
15520
+ for (const entry of entries) {
15521
+ const path = join(input.out, ...entry.path.split("/"));
15522
+ await mkdir(dirname(path), { recursive: true });
15523
+ await writeFile(path, entry.data);
15524
+ }
15525
+ return [input.out];
15526
+ }
15527
+ /** A bundle path inside the tree: relative, `/`-separated, with no empty, `.` or `..` part. */
15528
+ function safeEntry(path) {
15529
+ if (path === "" || path.startsWith("/") || path.includes("\\") || path.includes("\0")) return false;
15530
+ return path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
15531
+ }
15532
+ /**
15533
+ * Reads the bundle at `path` (a directory or a `.tar.zst` archive) into a scratch directory under
15534
+ * `scratchRoot` and checks every link of its identity, refusing it with
15535
+ * `MOSTRA_ARTIFACT_BUNDLE_INVALID` before anything is kept: `mostra-bundle.json` and the version's
15536
+ * own `manifest.json` agree; the lockfile has the environment's `lockDigest`; the environment has
15537
+ * `envDigest`; the source snapshot's listing has `sourceDigest`; the builds have `outputDigest`; and
15538
+ * the version ID is the one the artifact ID and those digests give (D89).
15539
+ */
15540
+ async function readBundle(path, scratchRoot) {
15541
+ let archive;
15542
+ try {
15543
+ const handle = await open(path, "r");
15544
+ try {
15545
+ const info = await handle.stat();
15546
+ if (!info.isDirectory()) {
15547
+ if (info.size > ARCHIVE_MAX) throw invalid$2(`${path} is larger than a bundle can be.`);
15548
+ archive = await handle.readFile();
15549
+ }
15550
+ } finally {
15551
+ await handle.close();
15552
+ }
15553
+ } catch (error) {
15554
+ const code = error.code;
15555
+ if (code === "ENOENT") throw invalid$2(`${path} doesn't exist.`);
15556
+ if (code !== "EISDIR" && code !== "EPERM" && code !== "EACCES") throw error;
15557
+ }
15558
+ await mkdir(scratchRoot, {
15559
+ recursive: true,
15560
+ mode: 448
15561
+ });
15562
+ const scratch = await mkdtemp(join(scratchRoot, "import-"));
15563
+ const dispose = () => removeTree(scratch);
15564
+ try {
15565
+ if (archive === void 0) for (const file of await filesUnder(path)) {
15566
+ const target = join(scratch, ...file.split("/"));
15567
+ await mkdir(dirname(target), { recursive: true });
15568
+ await writeFile(target, await readFile(join(path, ...file.split("/"))));
15569
+ }
15570
+ else {
15571
+ let files;
15572
+ try {
15573
+ files = readTar(zstdDecompressSync(archive));
15574
+ } catch (error) {
15575
+ throw invalid$2(`${path} isn't a bundle archive: ${error.message}`);
15576
+ }
15577
+ const seen = /* @__PURE__ */ new Set();
15578
+ for (const file of files) {
15579
+ if (!safeEntry(file.path) || seen.has(file.path.toLowerCase())) throw invalid$2(`The archive holds an entry it can't: ${JSON.stringify(file.path)}.`);
15580
+ seen.add(file.path.toLowerCase());
15581
+ const target = join(scratch, ...file.path.split("/"));
15582
+ await mkdir(dirname(target), { recursive: true });
15583
+ await writeFile(target, file.data);
15584
+ }
15585
+ }
15586
+ const read = async (file) => {
15587
+ try {
15588
+ return await readFile(join(scratch, ...file.split("/")));
15589
+ } catch {
15590
+ throw invalid$2(`The bundle has no ${file}.`);
15591
+ }
15592
+ };
15593
+ let info;
15594
+ let version;
15595
+ try {
15596
+ info = JSON.parse((await read(INFO)).toString("utf8"));
15597
+ version = JSON.parse((await read(`${VERSION}/manifest.json`)).toString("utf8"));
15598
+ } catch (error) {
15599
+ if (error instanceof RpcError) throw error;
15600
+ throw invalid$2("Its mostra-bundle.json or its version's manifest.json isn't JSON.");
15601
+ }
15602
+ if (info.schemaVersion !== 1 || info.kind !== "mostra-bundle") throw invalid$2("Its mostra-bundle.json isn't one this release reads (schemaVersion 1).");
15603
+ if (!Number.isSafeInteger(info.number) || info.number < 1) throw invalid$2("Its mostra-bundle.json names no version number.");
15604
+ for (const key of [
15605
+ "artifactId",
15606
+ "versionId",
15607
+ "sourceDigest",
15608
+ "envDigest",
15609
+ "outputDigest"
15610
+ ]) if (typeof info[key] !== "string" || info[key] !== version[key]) throw invalid$2(`Its mostra-bundle.json and its version's manifest.json differ in \`${key}\`.`);
15611
+ const manifest = ArtifactManifest.safeParse(version.manifest);
15612
+ if (!manifest.success) throw invalid$2("Its version's artifact manifest isn't valid.");
15613
+ for (const file of await filesUnder(join(scratch, VERSION))) if (!([
15614
+ "manifest.json",
15615
+ "lock.yaml",
15616
+ "registry.json"
15617
+ ].includes(file) || file.startsWith("client/") || file.startsWith("server/"))) throw invalid$2(`Its version holds a file no version has: ${file}.`);
15618
+ if (version.server !== existsSync(join(scratch, VERSION, "server", "entry.mjs"))) throw invalid$2("Its manifest.json and its server build disagree on whether it has server code.");
15619
+ if (sha256Hex(await read(`${VERSION}/lock.yaml`)) !== version.environment?.lockDigest || info.lockDigest !== version.environment.lockDigest) throw invalid$2("Its lock.yaml doesn't have the lockDigest its environment records.");
15620
+ if (environmentDigest(version.environment) !== version.envDigest) throw invalid$2("Its environment doesn't have the envDigest it records.");
15621
+ let sourceFiles;
15622
+ try {
15623
+ sourceFiles = readTar(zstdDecompressSync(await read(SOURCE)));
15624
+ } catch (error) {
15625
+ if (error instanceof RpcError) throw error;
15626
+ throw invalid$2("Its source.tar.zst isn't a source snapshot.");
15627
+ }
15628
+ if (listingDigest(sourceFiles.map((file) => ({
15629
+ path: file.path,
15630
+ executable: file.executable,
15631
+ sha256: sha256Hex(file.data)
15632
+ }))) !== version.sourceDigest) throw invalid$2("Its source snapshot doesn't have the sourceDigest it records.");
15633
+ const entry = sourceFiles.find((file) => file.path === version.entry);
15634
+ if (!entry) throw invalid$2(`Its source snapshot has no entry ${String(version.entry)}.`);
15635
+ let extracted;
15636
+ try {
15637
+ extracted = extractManifest(entry.path, Buffer.from(entry.data).toString("utf8")).manifest;
15638
+ } catch {
15639
+ throw invalid$2("Its entry's manifest can't be read.");
15640
+ }
15641
+ if (extracted?.id !== void 0 && extracted.id !== version.artifactId) throw invalid$2(`Its source names the artifact ${extracted.id}, and its version ${version.artifactId}.`);
15642
+ if (!(extracted === void 0 ? Object.keys(manifest.data).join() === "title" : canonical$1(extracted) === canonical$1(manifest.data))) throw invalid$2("Its manifest.json records another artifact manifest than its source has.");
15643
+ const versionDir = join(scratch, VERSION);
15644
+ if (await outputDigestOf(versionDir) !== version.outputDigest) throw invalid$2("Its builds don't have the outputDigest they record.");
15645
+ if (versionIdFor(version.artifactId, version.sourceDigest, version.envDigest) !== version.versionId) throw invalid$2("Its version ID isn't the one its artifact ID and digests give.");
15646
+ return {
15647
+ info,
15648
+ manifest: manifest.data,
15649
+ versionDir,
15650
+ snapshot: join(scratch, SOURCE),
15651
+ forcedCodes: version.forcedCodes ?? [],
15652
+ dispose
15653
+ };
15654
+ } catch (error) {
15655
+ await dispose().catch(() => {});
15656
+ throw error;
15657
+ }
15658
+ }
15659
+ //#endregion
14415
15660
  //#region src/publish/scaffold.ts
14416
15661
  /** The files a new artifact gets, in the order `artifact/create` reports them. */
14417
15662
  const SCAFFOLD_PATHS = [
@@ -14669,62 +15914,6 @@ async function readEntry(source) {
14669
15914
  }
14670
15915
  }
14671
15916
  //#endregion
14672
- //#region src/publish/tar.ts
14673
- /**
14674
- * A minimal, deterministic POSIX tar (pax interchange format) for source snapshots (DATA_MODEL §1):
14675
- * regular files only, in the order given, with mode 0644 or 0755, owner 0, modification time 0 and
14676
- * a pax `path` record for any path longer than 100 bytes or outside ASCII. The same tree always
14677
- * gives the same bytes. {@link readTar} reads what {@link tarFile} writes.
14678
- */
14679
- const BLOCK = 512;
14680
- function octal(value, width) {
14681
- return `${value.toString(8).padStart(width - 1, "0")}\0`;
14682
- }
14683
- function header(name, size, type, mode) {
14684
- const block = Buffer.alloc(BLOCK, 0);
14685
- block.write(name, 0, 100, "utf8");
14686
- block.write(octal(mode, 8), 100, 8, "ascii");
14687
- block.write(octal(0, 8), 108, 8, "ascii");
14688
- block.write(octal(0, 8), 116, 8, "ascii");
14689
- block.write(octal(size, 12), 124, 12, "ascii");
14690
- block.write(octal(0, 12), 136, 12, "ascii");
14691
- block.write(" ", 148, 8, "ascii");
14692
- block.write(type, 156, 1, "ascii");
14693
- block.write("ustar\0", 257, 6, "ascii");
14694
- block.write("00", 263, 2, "ascii");
14695
- let sum = 0;
14696
- for (const byte of block) sum += byte;
14697
- block.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8, "ascii");
14698
- return block;
14699
- }
14700
- function padding(size) {
14701
- const rest = size % BLOCK;
14702
- return Buffer.alloc(rest === 0 ? 0 : BLOCK - rest, 0);
14703
- }
14704
- /** A pax record `"<length> path=<value>\n"`, where the length counts the whole record. */
14705
- function paxRecord(key, value) {
14706
- const body = ` ${key}=${value}\n`;
14707
- const bodyLength = Buffer.byteLength(body, "utf8");
14708
- let length = bodyLength + 1;
14709
- while (String(length).length + bodyLength !== length) length = String(length).length + bodyLength;
14710
- return Buffer.from(`${length}${body}`, "utf8");
14711
- }
14712
- /** The archive blocks of one file: an optional pax header, the header, the data and padding. */
14713
- function tarFile(file) {
14714
- const mode = file.executable ? 493 : 420;
14715
- const blocks = [];
14716
- const plain = /^[\x20-\x7e]*$/.test(file.path) && Buffer.byteLength(file.path) <= 100;
14717
- if (!plain) {
14718
- const record = paxRecord("path", file.path);
14719
- blocks.push(header("PaxHeader", record.length, "x", 420), record, padding(record.length));
14720
- }
14721
- const name = plain ? file.path : file.path.replace(/[^\x20-\x7e]/g, "_").slice(-100);
14722
- blocks.push(header(name, file.data.length, "0", mode), Buffer.from(file.data), padding(file.data.length));
14723
- return blocks;
14724
- }
14725
- /** The end of an archive: two zero blocks. */
14726
- const TAR_END = Buffer.alloc(BLOCK * 2, 0);
14727
- //#endregion
14728
15917
  //#region src/publish/publisher.ts
14729
15918
  /**
14730
15919
  * Publish (ARCHITECTURE §3, VR-01, VR-02, VR-11): freezes an artifact's source into an immutable,
@@ -14950,15 +16139,15 @@ function failedVerdict(path, diagnostic) {
14950
16139
  timingsMs: { total: 0 }
14951
16140
  };
14952
16141
  }
14953
- const CROCKFORD$1 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
16142
+ const CROCKFORD$2 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
14954
16143
  /**
14955
16144
  * Verify's stand-in artifact ID for a source no manifest `id` or binding names yet: derived from
14956
16145
  * its real path, shaped like a ULID (D77's alphabet), and never committed anywhere.
14957
16146
  */
14958
16147
  function stableProvisional(bindingPath) {
14959
16148
  const hash = createHash("sha256").update(`mostra verify provisional\u0000${bindingPath}`).digest();
14960
- let body = CROCKFORD$1[hash[0] % 8];
14961
- for (let index = 1; index < 26; index += 1) body += CROCKFORD$1[hash[index] % 32];
16149
+ let body = CROCKFORD$2[hash[0] % 8];
16150
+ for (let index = 1; index < 26; index += 1) body += CROCKFORD$2[hash[index] % 32];
14962
16151
  return ArtifactId.parse(`art_${body}`);
14963
16152
  }
14964
16153
  /** Creates the publisher of one instance. */
@@ -15274,26 +16463,116 @@ function createPublisher(options) {
15274
16463
  })),
15275
16464
  verify: (params, progress) => gated(() => verifyOnce(params, progress)),
15276
16465
  create: (params) => queue.run("create", () => create(params)),
15277
- addDependencies: (params) => gated(() => addDependencies(params))
15278
- };
15279
- async function addDependencies(params) {
15280
- {
15281
- const path = pathOf(params.artifact);
15282
- const source = await resolveSource(path);
15283
- return queue.run(`path:${source.bindingPath}`, async () => {
15284
- const manifestId = extractManifest(source.entry, await readEntry(source)).manifest?.id;
15285
- return dependencies.add({
15286
- key: lockKey(manifestId ?? registry.artifactAt(source.bindingPath), source.bindingPath),
15287
- artifactDir: source.root,
15288
- artifactPath: path,
15289
- packages: params.packages,
15290
- request: requestKey("deps/add", params)
15291
- });
15292
- });
15293
- }
15294
- }
15295
- async function create(params) {
15296
- const key = requestKey("artifact/create", params);
16466
+ addDependencies: (params) => gated(() => addDependencies(params)),
16467
+ exportBundle: (params) => gated(() => exportBundle(params)),
16468
+ importBundle: (params) => gated(() => idempotent(requestKey("artifact/import", params), () => importBundle(params)))
16469
+ };
16470
+ async function exportBundle(params) {
16471
+ const detail = registry.get(params.artifact);
16472
+ const wanted = params.version;
16473
+ const summary = wanted === void 0 ? detail.latestVersion : detail.versions.find((version) => typeof wanted === "number" ? version.number === wanted : version.id === wanted);
16474
+ if (!summary) throw new RpcError(createDiagnostic("MOSTRA_ARTIFACT_VERSION_NOT_FOUND", { message: `${detail.title} has no version ${String(wanted ?? "")}.` }));
16475
+ if (summary.collected !== void 0) throw new RpcError(createDiagnostic("MOSTRA_ARTIFACT_VERSION_COLLECTED", { message: `Version ${summary.number} of ${detail.title} was collected; publish its source again to export it.` }));
16476
+ const versionDir = registry.store.versionDir(summary.id);
16477
+ const damage = await versionDamage(versionDir);
16478
+ if (damage !== void 0) throw new RpcError(createDiagnostic("MOSTRA_HOME_INTEGRITY", { message: `Version ${summary.number} of ${detail.title} is damaged: ${damage}.` }));
16479
+ const files = await writeBundle({
16480
+ versionDir,
16481
+ snapshot: registry.store.snapshotPath(detail.id, summary.sourceDigest),
16482
+ number: summary.number,
16483
+ title: detail.title,
16484
+ mostraVersion: options.runtimeVersion,
16485
+ out: params.out
16486
+ });
16487
+ log.info("publish.exported", {
16488
+ artifactId: detail.id,
16489
+ versionId: summary.id
16490
+ });
16491
+ return {
16492
+ files,
16493
+ versionId: summary.id
16494
+ };
16495
+ }
16496
+ async function importBundle(params) {
16497
+ const read = await readBundle(params.bundle, registry.store.stagingDir);
16498
+ const { info } = read;
16499
+ return queue.run(`artifact:${info.artifactId}`, async () => {
16500
+ const undo = [];
16501
+ let committed = false;
16502
+ try {
16503
+ if (!registry.hasVersion(info.versionId)) {
16504
+ const archive = registry.store.snapshotPath(info.artifactId, info.sourceDigest);
16505
+ if (!existsSync(archive)) {
16506
+ await mkdir(dirname(archive), {
16507
+ recursive: true,
16508
+ mode: 448
16509
+ });
16510
+ await copyFile(read.snapshot, archive);
16511
+ fsyncPath(archive);
16512
+ fsyncPath(dirname(archive), true);
16513
+ undo.push(() => removeTree(archive));
16514
+ }
16515
+ undo.push(() => removeTree(registry.store.versionDir(info.versionId)));
16516
+ await registry.store.promote(read.versionDir, info.versionId);
16517
+ }
16518
+ const result = registry.commit({
16519
+ artifactId: info.artifactId,
16520
+ bindingPath: void 0,
16521
+ manifestId: read.manifest.id,
16522
+ versionId: info.versionId,
16523
+ sourceDigest: info.sourceDigest,
16524
+ envDigest: info.envDigest,
16525
+ outputDigest: info.outputDigest,
16526
+ manifest: read.manifest,
16527
+ number: info.number,
16528
+ policy: void 0,
16529
+ homePolicy: options.homePolicy?.(),
16530
+ forcedCodes: read.forcedCodes,
16531
+ stillClaims: () => false,
16532
+ faults,
16533
+ request: requestKey("artifact/import", params)
16534
+ });
16535
+ committed = true;
16536
+ log.info("publish.imported", {
16537
+ artifactId: info.artifactId,
16538
+ versionId: info.versionId,
16539
+ version: result.version,
16540
+ reused: result.reused
16541
+ });
16542
+ return {
16543
+ artifactId: info.artifactId,
16544
+ versionId: info.versionId,
16545
+ version: result.version,
16546
+ cursor: result.cursor,
16547
+ reused: result.reused,
16548
+ ...read.forcedCodes.length > 0 ? { forcedDiagnostics: [...read.forcedCodes] } : {}
16549
+ };
16550
+ } catch (error) {
16551
+ if (!committed) for (const step of undo.reverse()) await step().catch((cleanup) => log.warn("publish.undo_failed", { error: cleanup }));
16552
+ throw error;
16553
+ } finally {
16554
+ await read.dispose().catch(() => {});
16555
+ }
16556
+ });
16557
+ }
16558
+ async function addDependencies(params) {
16559
+ {
16560
+ const path = pathOf(params.artifact);
16561
+ const source = await resolveSource(path);
16562
+ return queue.run(`path:${source.bindingPath}`, async () => {
16563
+ const manifestId = extractManifest(source.entry, await readEntry(source)).manifest?.id;
16564
+ return dependencies.add({
16565
+ key: lockKey(manifestId ?? registry.artifactAt(source.bindingPath), source.bindingPath),
16566
+ artifactDir: source.root,
16567
+ artifactPath: path,
16568
+ packages: params.packages,
16569
+ request: requestKey("deps/add", params)
16570
+ });
16571
+ });
16572
+ }
16573
+ }
16574
+ async function create(params) {
16575
+ const key = requestKey("artifact/create", params);
15297
16576
  if (key !== void 0) {
15298
16577
  const recorded = ledger.read((db) => recordedResult(db, key));
15299
16578
  if (recorded !== void 0) {
@@ -15394,7 +16673,8 @@ async function instanceChecks(inputs) {
15394
16673
  //#region src/publish/methods.ts
15395
16674
  /**
15396
16675
  * The authoring methods of the host protocol (INTERFACES §6): `artifact/create`,
15397
- * `artifact/publish`, `artifact/list`, `artifact/get` and `deps/add`.
16676
+ * `artifact/publish`, `artifact/list`, `artifact/get`, `deps/add`, and a bundle's
16677
+ * `artifact/export` and `artifact/import` (RM-01).
15398
16678
  */
15399
16679
  /**
15400
16680
  * A path as bindings store it (D51): its real path, or, for a path that no longer exists, the real
@@ -15445,6 +16725,18 @@ function artifactMethods(options) {
15445
16725
  }) : void 0;
15446
16726
  return options.publisher().verify(params, progress);
15447
16727
  }),
16728
+ handler(artifactExportMethod, (params) => options.publisher().exportBundle({
16729
+ ...params,
16730
+ artifact: canonicalRef(params.artifact)
16731
+ })),
16732
+ handler(artifactImportMethod, async (params) => {
16733
+ const receipt = await options.publisher().importBundle(params);
16734
+ options.published?.(receipt.artifactId);
16735
+ return options.link ? {
16736
+ ...receipt,
16737
+ ...await options.link(receipt.artifactId)
16738
+ } : receipt;
16739
+ }),
15448
16740
  handler(artifactListMethod, (params) => ({ artifacts: registry.list(params.filter ?? {}) })),
15449
16741
  handler(artifactGetMethod, (params) => {
15450
16742
  return registry.get(canonicalRef(params.artifact));
@@ -15842,12 +17134,12 @@ function createDev(options) {
15842
17134
  * The 1 s comparison notices the removal within this and a second more.
15843
17135
  */
15844
17136
  const MISSING_GRACE_MS = 1500;
15845
- const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
17137
+ const CROCKFORD$1 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
15846
17138
  /** A stand-in artifact ID for a source no manifest `id` or binding names: stable per path. */
15847
17139
  function stableId(bindingPath) {
15848
17140
  const hash = createHash("sha256").update(`mostra dev provisional\u0000${bindingPath}`).digest();
15849
- let body = CROCKFORD[hash[0] % 8];
15850
- for (let index = 1; index < 26; index += 1) body += CROCKFORD[hash[index] % 32];
17141
+ let body = CROCKFORD$1[hash[0] % 8];
17142
+ for (let index = 1; index < 26; index += 1) body += CROCKFORD$1[hash[index] % 32];
15851
17143
  return ArtifactId.parse(`art_${body}`);
15852
17144
  }
15853
17145
  //#endregion
@@ -15959,6 +17251,15 @@ const REFERENCE_SOURCES = [
15959
17251
  covers: ["view.version_id"],
15960
17252
  keep: (db) => column(db, "SELECT version_id AS value FROM view")
15961
17253
  },
17254
+ {
17255
+ name: "pushes and relayed runs",
17256
+ covers: ["version_push.version_id", "relayed_run.version_id"],
17257
+ keep: function* (db) {
17258
+ if (!tableExists(db, "version_push")) return;
17259
+ yield* column(db, "SELECT version_id AS value FROM version_push WHERE state = 'pending'");
17260
+ yield* column(db, "SELECT version_id AS value FROM relayed_run");
17261
+ }
17262
+ },
15962
17263
  {
15963
17264
  name: "comment threads",
15964
17265
  covers: ["thread.version_id", "thread.anchor_json"],
@@ -16337,209 +17638,1776 @@ async function acquireHome(options) {
16337
17638
  socket: socketPath,
16338
17639
  pid: self.pid
16339
17640
  });
16340
- return {
16341
- kind: "owner",
16342
- home: own({
16343
- home,
16344
- socketPath,
16345
- runtimeDir,
16346
- ledger: opening.ledger,
16347
- log,
16348
- redactor,
16349
- self,
16350
- options
16351
- })
16352
- };
16353
- }
16354
- const record = await readLiveInstanceRecord(home);
16355
- if (record) {
16356
- seen = record;
16357
- if (options.mode === "host") throw locked(`The home ${home} is already owned by a Mostra ${record.mode} (PID ${record.pid}); \`mostra host\` needs a home no other instance owns.`);
17641
+ return {
17642
+ kind: "owner",
17643
+ home: own({
17644
+ home,
17645
+ socketPath,
17646
+ runtimeDir,
17647
+ ledger: opening.ledger,
17648
+ log,
17649
+ redactor,
17650
+ self,
17651
+ options
17652
+ })
17653
+ };
17654
+ }
17655
+ const record = await readLiveInstanceRecord(home);
17656
+ if (record) {
17657
+ seen = record;
17658
+ if (options.mode === "host") throw locked(`The home ${home} is already owned by a Mostra ${record.mode} (PID ${record.pid}); \`mostra host\` needs a home no other instance owns.`);
17659
+ try {
17660
+ const { client, result } = await attach(record.socket, options.client, 2e3);
17661
+ log.info("home.attached", {
17662
+ home,
17663
+ owner: record.pid,
17664
+ socket: record.socket
17665
+ });
17666
+ return {
17667
+ kind: "attached",
17668
+ record,
17669
+ connection: client,
17670
+ result
17671
+ };
17672
+ } catch (error) {
17673
+ if (!(error instanceof RpcError) || error.diagnostic.code !== "MOSTRA_HOST_CONNECTION_CLOSED") throw error;
17674
+ }
17675
+ }
17676
+ if (Date.now() >= deadline) {
17677
+ const owner = seen ?? readInstanceRecord(home);
17678
+ throw locked(owner ? `The home ${home} is owned by PID ${owner.pid}, but its socket ${owner.socket} can't be reached.` : `Another process holds the ledger lock of the home ${home} and published no instance record.`);
17679
+ }
17680
+ await delay(100);
17681
+ }
17682
+ }
17683
+ function identity(path) {
17684
+ try {
17685
+ const stats = statSync(path);
17686
+ return `${stats.dev}:${stats.ino}`;
17687
+ } catch {
17688
+ return;
17689
+ }
17690
+ }
17691
+ function own(inputs) {
17692
+ const { home, socketPath, runtimeDir, ledger, log, redactor, self } = inputs;
17693
+ const startedAt = (/* @__PURE__ */ new Date()).toISOString();
17694
+ const homeId = identity(home);
17695
+ const ledgerId = identity(join(home, "store", LEDGER_FILE));
17696
+ let timer;
17697
+ let released = false;
17698
+ return {
17699
+ path: home,
17700
+ socketPath,
17701
+ runtimeDir,
17702
+ ledger,
17703
+ log,
17704
+ redactor,
17705
+ self,
17706
+ startedAt,
17707
+ publish(mode, socket) {
17708
+ writeInstanceRecord(home, {
17709
+ pid: self.pid,
17710
+ startTime: self.startTime,
17711
+ socket,
17712
+ runtimeVersion: inputs.options.runtimeVersion,
17713
+ revisions: [...HOST_PROTOCOL_REVISIONS],
17714
+ mode,
17715
+ startedAt
17716
+ });
17717
+ },
17718
+ watch(onGone, intervalMs = DEFAULT_WATCH_MS) {
17719
+ if (timer) clearInterval(timer);
17720
+ timer = setInterval(() => {
17721
+ const homeNow = identity(home);
17722
+ const ledgerNow = identity(join(home, "store", LEDGER_FILE));
17723
+ if (homeNow === homeId && ledgerNow === ledgerId) return;
17724
+ if (timer) clearInterval(timer);
17725
+ onGone(createDiagnostic("MOSTRA_HOME_GONE", { message: homeNow === homeId ? `The ledger of the home ${home} was removed or replaced while the instance was running.` : `The home ${home} was removed or replaced while the instance was running.` }));
17726
+ }, Math.min(intervalMs, 1e4));
17727
+ timer.unref();
17728
+ },
17729
+ release() {
17730
+ if (released) return;
17731
+ released = true;
17732
+ if (timer) clearInterval(timer);
17733
+ try {
17734
+ removeInstanceRecord(home, self.pid, self.startTime);
17735
+ } catch (error) {
17736
+ log.warn("home.record_remove_failed", { error });
17737
+ }
17738
+ ledger.close();
17739
+ log.info("home.released", { home });
17740
+ }
17741
+ };
17742
+ }
17743
+ //#endregion
17744
+ //#region src/home/platform.ts
17745
+ /**
17746
+ * The unsupported-platform gate (D35, D146, RT-07). Every entry point calls it before it touches
17747
+ * the home: Mostra runs on macOS, Linux and Windows, on x64 and arm64, and fails closed elsewhere.
17748
+ * Windows was built behind a development switch by WP-W1 to WP-W3, and is supported since WP-W3
17749
+ * like the others.
17750
+ */
17751
+ const OPERATING_SYSTEMS = [
17752
+ "darwin",
17753
+ "linux",
17754
+ "win32"
17755
+ ];
17756
+ const ARCHITECTURES = ["x64", "arm64"];
17757
+ /** The platform as `<os>-<arch>`, as `InstanceInfo.platform` reports it. */
17758
+ function platformName(platform = OS, arch = process.arch) {
17759
+ return `${platform}-${arch}`;
17760
+ }
17761
+ /**
17762
+ * Returns `MOSTRA_PLATFORM_UNSUPPORTED` for a platform Mostra doesn't run on, or `undefined` for
17763
+ * a supported one.
17764
+ */
17765
+ function checkPlatform(platform = OS, arch = process.arch) {
17766
+ if (ARCHITECTURES.includes(arch) && OPERATING_SYSTEMS.includes(platform)) return void 0;
17767
+ const name = platform === "win32" ? `Windows (${platformName(platform, arch)})` : platformName(platform, arch);
17768
+ return createDiagnostic("MOSTRA_PLATFORM_UNSUPPORTED", { message: `Mostra doesn't run on ${name}; it supports macOS, Linux and Windows on x64 and arm64.` });
17769
+ }
17770
+ //#endregion
17771
+ //#region src/home/settings.ts
17772
+ /**
17773
+ * The home's settings (`config.json`, DATA_MODEL §1): what hosts set through `HomeConfig` and
17774
+ * what every later client of the home then shares: the dependency policy (D43, D95), the home's
17775
+ * submission policy (D102), the ports each host always provides (D105) and the submit channels the
17776
+ * home accepts (D108).
17777
+ * Written atomically (a temporary file, then a rename); read fresh by whoever needs a setting, so
17778
+ * a host's change applies to the next request.
17779
+ */
17780
+ const Settings = z.looseObject({
17781
+ dependencies: z.enum(["auto", "explicit"]).optional(),
17782
+ submission: SubmissionPolicy.optional(),
17783
+ expectedPorts: z.record(z.string(), z.array(z.string())).optional(),
17784
+ submitChannels: z.array(z.enum(["shell", "artifact"])).min(1).max(2).optional(),
17785
+ hostConfigured: z.object({
17786
+ client: z.string(),
17787
+ at: z.string()
17788
+ }).optional()
17789
+ });
17790
+ /** The settings file of a home. */
17791
+ const SETTINGS_FILE = "config.json";
17792
+ function unreadable(home, reason) {
17793
+ return new RpcError(createDiagnostic("MOSTRA_HOME_INTEGRITY", {
17794
+ message: `The home's ${join(home, SETTINGS_FILE)} can't be read: ${reason}.`,
17795
+ hint: `Repair or remove ${SETTINGS_FILE} in the home; a host sets its settings again when it connects.`
17796
+ }));
17797
+ }
17798
+ /**
17799
+ * The home's settings; none when `config.json` doesn't exist. A file that isn't valid settings
17800
+ * fails closed: guessing the policy could install packages a host forbade.
17801
+ */
17802
+ function readHomeSettings(home) {
17803
+ let text;
17804
+ try {
17805
+ text = readFileSync(join(home, SETTINGS_FILE), "utf8");
17806
+ } catch (error) {
17807
+ if (error.code === "ENOENT") return {};
17808
+ throw unreadable(home, error.code ?? "error");
17809
+ }
17810
+ let data;
17811
+ try {
17812
+ data = JSON.parse(text);
17813
+ } catch {
17814
+ throw unreadable(home, "it isn't JSON");
17815
+ }
17816
+ const parsed = Settings.safeParse(data);
17817
+ if (!parsed.success) throw unreadable(home, "its settings are invalid");
17818
+ return parsed.data;
17819
+ }
17820
+ /** Merges `change` into the home's settings, durably. */
17821
+ function writeHomeSettings(home, change) {
17822
+ const next = {
17823
+ ...readHomeSettings(home),
17824
+ ...change
17825
+ };
17826
+ const file = join(home, SETTINGS_FILE);
17827
+ const temporary = `${file}.${process.pid}.tmp`;
17828
+ writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { mode: 384 });
17829
+ fsyncPath(temporary);
17830
+ renamePathSync(temporary, file);
17831
+ fsyncPath(home, true);
17832
+ }
17833
+ /**
17834
+ * Marks the home as configured by a host (D140 item 4), once: the first host's client name and
17835
+ * when. A home already marked keeps its mark.
17836
+ */
17837
+ function markHostConfigured(home, client, at = /* @__PURE__ */ new Date()) {
17838
+ if (readHomeSettings(home).hostConfigured !== void 0) return false;
17839
+ writeHomeSettings(home, { hostConfigured: {
17840
+ client,
17841
+ at: at.toISOString()
17842
+ } });
17843
+ return true;
17844
+ }
17845
+ //#endregion
17846
+ //#region src/link/binding.ts
17847
+ /**
17848
+ * The owner's side of the network binding (INTERFACES §18.2, D165 item 2): one WebSocket to a
17849
+ * remote server's `/host`, dialled out (so it works behind NAT), opened with the access token and
17850
+ * the subprotocol `mostra.host`, carrying the host protocol as one JSON-RPC message per text frame.
17851
+ * It keeps the heartbeat (a ping and `mostra/ping` every 20 s; dead after 45 s of silence) and
17852
+ * dials again with backoff (1 s doubling to 60 s, with jitter) whenever the connection ends, so an
17853
+ * edge or server restart is an ordinary reconnect. Each connection opens its session afresh:
17854
+ * `mostra/initialize` with the owner link, then the ports registered again.
17855
+ */
17856
+ /** The first redial's delay; each later one doubles, up to {@link MAX_REDIAL_MS}. */
17857
+ const FIRST_REDIAL_MS = 1e3;
17858
+ /** The longest delay between dials (§18.2). */
17859
+ const MAX_REDIAL_MS = 6e4;
17860
+ /** The delay before dial number `attempt` (1 for the first redial), with up to 25% jitter. */
17861
+ function redialMs(attempt, random = Math.random) {
17862
+ const base = Math.min(MAX_REDIAL_MS, FIRST_REDIAL_MS * 2 ** Math.max(0, attempt - 1));
17863
+ return Math.round(base * (.75 + random() * .25));
17864
+ }
17865
+ /** Starts a binding: it dials at once and keeps dialling until stopped. */
17866
+ function startBinding(options) {
17867
+ const { log } = options;
17868
+ let stopped = false;
17869
+ let current;
17870
+ let session;
17871
+ let attempt = 0;
17872
+ let timer;
17873
+ let waiters = [];
17874
+ const url = () => {
17875
+ const origin = new URL(options.server);
17876
+ const scheme = origin.protocol === "https:" ? "wss:" : "ws:";
17877
+ if (options.connectTo) {
17878
+ const port = options.connectTo.port ?? (Number(origin.port) || (scheme === "wss:" ? 443 : 80));
17879
+ return `${scheme}//${options.connectTo.host}:${port}/host`;
17880
+ }
17881
+ return `${scheme}//${origin.host}/host`;
17882
+ };
17883
+ const schedule = () => {
17884
+ if (stopped || timer) return;
17885
+ attempt += 1;
17886
+ const delay = redialMs(attempt);
17887
+ timer = setTimeout(() => {
17888
+ timer = void 0;
17889
+ dial();
17890
+ }, delay);
17891
+ timer.unref?.();
17892
+ };
17893
+ const dial = async () => {
17894
+ if (stopped) return;
17895
+ let token;
17896
+ try {
17897
+ token = await options.accessToken();
17898
+ } catch (error) {
17899
+ log.warn("link.token_failed", {
17900
+ server: options.server,
17901
+ error: error.message
17902
+ });
17903
+ options.onClose?.(error.message);
17904
+ schedule();
17905
+ return;
17906
+ }
17907
+ if (stopped) return;
17908
+ const host = new URL(options.server).host;
17909
+ const socket = new WebSocket(url(), BINDING_SUBPROTOCOL, {
17910
+ headers: {
17911
+ authorization: `Bearer ${token}`,
17912
+ ...options.connectTo ? { host } : {}
17913
+ },
17914
+ maxPayload: MAX_FRAME_BYTES,
17915
+ perMessageDeflate: false,
17916
+ handshakeTimeout: 2e4
17917
+ });
17918
+ let opened = false;
17919
+ /** This connection's session, from its `mostra/initialize` answer on. */
17920
+ let connectionSession;
17921
+ const peer = new RpcPeer({
17922
+ send: (message) => {
17923
+ if (socket.readyState !== socket.OPEN) return;
17924
+ const text = JSON.stringify(message);
17925
+ options.trace?.("out", text);
17926
+ socket.send(text);
17927
+ },
17928
+ onRequest: (method, params) => options.onRequest(method, params),
17929
+ onNotification: (method, params) => {
17930
+ if (method === "mostra/ping") return;
17931
+ if (!connectionSession) {
17932
+ log.warn("link.notification_before_session", {
17933
+ server: options.server,
17934
+ method
17935
+ });
17936
+ return;
17937
+ }
17938
+ options.onNotification?.(method, params, connectionSession);
17939
+ },
17940
+ onInternalError: (error, method) => log.error("link.internal_error", {
17941
+ method,
17942
+ error
17943
+ })
17944
+ });
17945
+ current = {
17946
+ socket,
17947
+ peer
17948
+ };
17949
+ socket.on("unexpected-response", (_request, response) => {
17950
+ const status = response.statusCode ?? 0;
17951
+ log.warn("link.refused", {
17952
+ server: options.server,
17953
+ status
17954
+ });
17955
+ if (status === 401) options.onUnauthorized?.();
17956
+ socket.terminate();
17957
+ });
17958
+ socket.on("message", (data, isBinary) => {
17959
+ const text = readFrame(socket, data, isBinary, log);
17960
+ if (text === void 0) return;
17961
+ options.trace?.("in", text);
17962
+ peer.receiveLine(text);
17963
+ });
17964
+ socket.on("error", (error) => log.warn("link.socket_error", {
17965
+ server: options.server,
17966
+ error: error.message
17967
+ }));
17968
+ socket.once("close", (code, reason) => {
17969
+ peer.close();
17970
+ if (current?.socket === socket) current = void 0;
17971
+ session = void 0;
17972
+ const why = `closed (${code}${reason.length > 0 ? `: ${reason.toString()}` : ""})`;
17973
+ if (opened) log.info("link.disconnected", {
17974
+ server: options.server,
17975
+ code
17976
+ });
17977
+ options.onClose?.(why);
17978
+ if (code === 4401 || code === 4403) options.onUnauthorized?.();
17979
+ schedule();
17980
+ });
17981
+ socket.once("open", () => {
17982
+ opened = true;
17983
+ keepAlive(socket, options.heartbeat);
17984
+ (async () => {
17985
+ try {
17986
+ const open = {
17987
+ result: await peer.call(mostraInitializeMethod, options.initialize(), { timeoutMs: 3e4 }),
17988
+ call: (definition, params, callOptions) => peer.call(definition, params, callOptions),
17989
+ notify: (method, params) => peer.notify(method, params)
17990
+ };
17991
+ connectionSession = open;
17992
+ attempt = 0;
17993
+ await options.onOpen?.(open);
17994
+ if (socket.readyState !== socket.OPEN) return;
17995
+ session = open;
17996
+ log.info("link.connected", { server: options.server });
17997
+ const ready = waiters;
17998
+ waiters = [];
17999
+ for (const resolve of ready) resolve(open);
18000
+ } catch (error) {
18001
+ log.warn("link.initialize_failed", {
18002
+ server: options.server,
18003
+ error: error.message
18004
+ });
18005
+ options.onClose?.(error.message);
18006
+ socket.close(1008, "initialize failed");
18007
+ }
18008
+ })();
18009
+ });
18010
+ };
18011
+ dial();
18012
+ return {
18013
+ get session() {
18014
+ return session;
18015
+ },
18016
+ ready() {
18017
+ if (session) return Promise.resolve(session);
18018
+ return new Promise((resolve) => waiters.push(resolve));
18019
+ },
18020
+ reconnect() {
18021
+ current?.socket.terminate();
18022
+ },
18023
+ async stop() {
18024
+ stopped = true;
18025
+ if (timer) clearTimeout(timer);
18026
+ const open = current;
18027
+ current = void 0;
18028
+ if (open) await new Promise((resolve) => {
18029
+ if (open.socket.readyState === open.socket.CLOSED) {
18030
+ resolve();
18031
+ return;
18032
+ }
18033
+ open.socket.once("close", () => resolve());
18034
+ open.socket.close(1e3);
18035
+ setTimeout(() => {
18036
+ open.socket.terminate();
18037
+ resolve();
18038
+ }, 2e3).unref();
18039
+ });
18040
+ }
18041
+ };
18042
+ }
18043
+ /** The `mostra/initialize` params an owner instance sends on its binding (§18.2). */
18044
+ function ownerInitialize(input) {
18045
+ return {
18046
+ client: {
18047
+ name: input.name,
18048
+ version: input.version
18049
+ },
18050
+ revisions: [...HOST_PROTOCOL_REVISIONS],
18051
+ capabilities: { notifications: ["events/notify"] },
18052
+ ports: ["submissions", "relay"],
18053
+ link: {
18054
+ instanceId: input.instanceId,
18055
+ relayMark: input.relayMark,
18056
+ ...input.followed ? { followed: {
18057
+ incarnation: input.followed.incarnation,
18058
+ cursor: input.followed.cursor
18059
+ } } : {}
18060
+ }
18061
+ };
18062
+ }
18063
+ //#endregion
18064
+ //#region src/link/hosted.ts
18065
+ /**
18066
+ * A hosted artifact's shared state and live values live on its hosted home (D165 item 1;
18067
+ * INTERFACES §18.2): the owner's own `state/get`, `state/patch` and `live/push`, and its server
18068
+ * code's `state`, act there through the link, refused with `MOSTRA_REMOTE_UNREACHABLE` when it
18069
+ * can't be reached, and are never applied here alone.
18070
+ */
18071
+ const FORWARDED = /* @__PURE__ */ new Set([
18072
+ stateGetMethod.method,
18073
+ statePatchMethod.method,
18074
+ livePushMethod.method
18075
+ ]);
18076
+ /** Server code's `state` for an artifact hosted somewhere, else `undefined`. */
18077
+ function hostedState(link, artifactId) {
18078
+ const server = link.hostedOn(artifactId);
18079
+ if (server === void 0) return void 0;
18080
+ const artifact = { artifactId };
18081
+ return {
18082
+ get: (ids) => link.forward(server, stateGetMethod, {
18083
+ artifact,
18084
+ ...ids !== void 0 ? { ids: [...ids] } : {}
18085
+ }),
18086
+ patch: (input) => link.forward(server, statePatchMethod, {
18087
+ artifact,
18088
+ ops: input.ops,
18089
+ mutationId: input.mutationId ?? MutationId.create(),
18090
+ ...input.baseRevision !== void 0 ? { baseRevision: input.baseRevision } : {},
18091
+ as: "agent",
18092
+ ...input.runId !== void 0 ? { causation: { runId: input.runId } } : {}
18093
+ })
18094
+ };
18095
+ }
18096
+ /**
18097
+ * Wraps the instance's `state/get`, `state/patch` and `live/push` so that, for a hosted artifact,
18098
+ * they act on its hosted home: the same params, the artifact named by its ID there.
18099
+ */
18100
+ function forwardHosted(link, resolve, handlers) {
18101
+ return handlers.map((entry) => {
18102
+ if (!FORWARDED.has(entry.definition.method)) return entry;
18103
+ return {
18104
+ definition: entry.definition,
18105
+ handle: (params, session) => {
18106
+ const input = params;
18107
+ const artifactId = resolve(input.artifact);
18108
+ const server = link.hostedOn(artifactId);
18109
+ if (server === void 0) return entry.handle(params, session);
18110
+ return link.forward(server, entry.definition, {
18111
+ ...input,
18112
+ artifact: { artifactId }
18113
+ });
18114
+ }
18115
+ };
18116
+ });
18117
+ }
18118
+ /** The largest answer the instance reads (the API answers JSON; files go the other way). */
18119
+ const MAX_ANSWER_BYTES = 4194304;
18120
+ /** Reads the test override, `host:port`, or nothing. */
18121
+ function testServerAddress(value) {
18122
+ if (!value) return void 0;
18123
+ if (/^[\d.]+$/.test(value)) return { host: value };
18124
+ const at = value.lastIndexOf(":");
18125
+ const port = Number(value.slice(at + 1));
18126
+ if (at <= 0 || !Number.isInteger(port) || port <= 0 || port > 65535) return void 0;
18127
+ return {
18128
+ host: value.slice(0, at).replace(/^\[|\]$/g, ""),
18129
+ port
18130
+ };
18131
+ }
18132
+ /** The diagnostic for a server that can't be reached, naming it (never what was sent). */
18133
+ function unreachable(server, why) {
18134
+ return new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: `${server} can't be reached: ${why}.` }));
18135
+ }
18136
+ /** Sends one request to `server` (an API origin) and reads its answer. */
18137
+ function remoteRequest(server, path, init = {}) {
18138
+ const url = new URL(path, server);
18139
+ const secure = url.protocol === "https:";
18140
+ const send = secure ? request$1 : request;
18141
+ const port = Number(url.port) || (secure ? 443 : 80);
18142
+ return new Promise((resolve, reject) => {
18143
+ const outgoing = send({
18144
+ host: init.connectTo?.host ?? url.hostname,
18145
+ port: init.connectTo?.port ?? port,
18146
+ path: `${url.pathname}${url.search}`,
18147
+ method: init.method ?? "GET",
18148
+ headers: {
18149
+ host: url.host,
18150
+ ...init.body !== void 0 ? { "content-length": String(Buffer.byteLength(init.body)) } : {},
18151
+ ...init.headers
18152
+ },
18153
+ ...secure ? { servername: url.hostname } : {},
18154
+ timeout: init.timeoutMs ?? 3e4
18155
+ }, (incoming) => {
18156
+ const chunks = [];
18157
+ let size = 0;
18158
+ incoming.on("data", (chunk) => {
18159
+ size += chunk.length;
18160
+ if (size > MAX_ANSWER_BYTES) {
18161
+ incoming.destroy();
18162
+ reject(unreachable(server, "its answer was too large"));
18163
+ return;
18164
+ }
18165
+ chunks.push(chunk);
18166
+ });
18167
+ incoming.on("end", () => resolve({
18168
+ status: incoming.statusCode ?? 0,
18169
+ headers: incoming.headers,
18170
+ body: Buffer.concat(chunks)
18171
+ }));
18172
+ incoming.on("error", (error) => reject(unreachable(server, error.message)));
18173
+ });
18174
+ outgoing.on("timeout", () => outgoing.destroy(/* @__PURE__ */ new Error("no answer in time")));
18175
+ outgoing.on("error", (error) => reject(unreachable(server, error.message)));
18176
+ if (init.body !== void 0) outgoing.write(init.body);
18177
+ outgoing.end();
18178
+ });
18179
+ }
18180
+ /** Posts a form and reads a JSON answer, whatever its status. */
18181
+ async function postForm(server, path, fields, connectTo) {
18182
+ const answer = await remoteRequest(server, path, {
18183
+ method: "POST",
18184
+ headers: {
18185
+ "content-type": "application/x-www-form-urlencoded",
18186
+ accept: "application/json"
18187
+ },
18188
+ body: new URLSearchParams(fields).toString(),
18189
+ ...connectTo ? { connectTo } : {}
18190
+ });
18191
+ let json = {};
18192
+ try {
18193
+ const parsed = JSON.parse(answer.body.toString("utf8"));
18194
+ if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) json = parsed;
18195
+ } catch {}
18196
+ return {
18197
+ status: answer.status,
18198
+ json
18199
+ };
18200
+ }
18201
+ //#endregion
18202
+ //#region src/link/relay.ts
18203
+ /**
18204
+ * The relay on the owner's side (D165 item 4; INTERFACES §18.3): the owner instance runs a hosted
18205
+ * artifact's runs and plain calls that its hosted home relays, in its own pool with its own
18206
+ * secrets. A relayed run is recorded here under the hosted run's ID before the answer says it was
18207
+ * accepted, so a repeated `port/relay.run` joins it and starts nothing; its progress, log, result
18208
+ * and end go back as `relay/run.update`, already redacted by the actions module. What a dropped
18209
+ * connection loses, the hosted home's reconcile after the reconnect reads again from this ledger.
18210
+ * No relay message carries a secret: `secret()` is answered here (D129).
18211
+ */
18212
+ /** How long a hosted page's interest in a live source lasts unless renewed (as a view's, D130). */
18213
+ const LIVE_INTEREST_MS = 6e4;
18214
+ function invalid$1(message) {
18215
+ return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }), -32602);
18216
+ }
18217
+ const RELAY_METHODS = /* @__PURE__ */ new Set([
18218
+ portRelayLiveMethod.method,
18219
+ portRelayRunMethod.method,
18220
+ portRelayCancelMethod.method,
18221
+ portRelayReconcileMethod.method,
18222
+ portRelayCallMethod.method
18223
+ ]);
18224
+ /** Creates the owner's relay. */
18225
+ function createOwnerRelay(options) {
18226
+ const { ledger, log } = options;
18227
+ let loseAcks = options.loseAcks ?? 0;
18228
+ /** Hosted pages' interest in live sources: `<server> <artifact> <key>` → until when. */
18229
+ const interest = /* @__PURE__ */ new Map();
18230
+ setInterval(() => {
18231
+ let changed = false;
18232
+ for (const [id, entry] of interest) if (entry.until <= options.now()) {
18233
+ interest.delete(id);
18234
+ changed = true;
18235
+ }
18236
+ if (changed) options.liveChanged?.();
18237
+ }, 5e3).unref();
18238
+ /** Relayed calls in flight per server, so a lost binding stops them. */
18239
+ const calls = /* @__PURE__ */ new Map();
18240
+ /** Each relayed run's last update sequence, from `relayed_run.seq_sent`. */
18241
+ const sequences = /* @__PURE__ */ new Map();
18242
+ /** Each run's updates in order: a run's messages never overtake each other. */
18243
+ const chains = /* @__PURE__ */ new Map();
18244
+ const nextSeq = (runId) => {
18245
+ const seq = (sequences.get(runId) ?? ledger.read((db) => db.get("SELECT seq_sent FROM relayed_run WHERE run_id = ?", runId)?.seq_sent ?? 0)) + 1;
18246
+ sequences.set(runId, seq);
18247
+ ledger.write((db) => db.run("UPDATE relayed_run SET seq_sent = ? WHERE run_id = ?", seq, runId));
18248
+ return seq;
18249
+ };
18250
+ /** A relayed run's or call's signals and host events, appended on its hosted home. */
18251
+ const effectOf = (session, artifactId, of) => async (kind, name, payload) => {
18252
+ const open = await session();
18253
+ if (!open) throw new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: "The page's server can't be reached now, so the signal wasn't raised there." }));
18254
+ return (await open.call(relayEffectMethod, {
18255
+ artifactId,
18256
+ ...of,
18257
+ kind,
18258
+ name,
18259
+ ...payload !== void 0 ? { payload } : {}
18260
+ })).cursor;
18261
+ };
18262
+ /** Sends one update of a run, after the ones before it; one the binding loses is reconciled. */
18263
+ const update = (runId, session, body) => {
18264
+ const next = (chains.get(runId) ?? Promise.resolve()).then(async () => {
18265
+ const open = await session();
18266
+ if (!open) return;
18267
+ try {
18268
+ await open.call(relayRunUpdateMethod, {
18269
+ runId,
18270
+ seq: nextSeq(runId),
18271
+ ...body
18272
+ });
18273
+ } catch (error) {
18274
+ log.warn("relay.update_failed", {
18275
+ runId,
18276
+ error
18277
+ });
18278
+ }
18279
+ });
18280
+ chains.set(runId, next);
18281
+ next.then(() => {
18282
+ if (chains.get(runId) === next) chains.delete(runId);
18283
+ });
18284
+ };
18285
+ const observer = (runId, session) => ({
18286
+ running: () => update(runId, session, { running: true }),
18287
+ output: (flush) => update(runId, session, {
18288
+ log: flush.log,
18289
+ logFrom: flush.logFrom,
18290
+ batches: flush.batches,
18291
+ batchFrom: flush.batchFrom,
18292
+ ...flush.progress ? { progress: flush.progress } : {}
18293
+ }),
18294
+ ended: (state) => {
18295
+ ledger.write((db) => db.run("UPDATE relayed_run SET finished_at = ? WHERE run_id = ?", new Date(options.now()).toISOString(), runId));
18296
+ if (state.status === "succeeded" || state.status === "failed" || state.status === "cancelled") update(runId, session, fullState(runId) ?? { end: {
18297
+ status: state.status,
18298
+ ...state.reason !== void 0 ? { reason: state.reason } : {},
18299
+ ...state.error ? { error: state.error } : {}
18300
+ } });
18301
+ }
18302
+ });
18303
+ /** A run's whole state from this ledger, as one update (its end too, once it ended). */
18304
+ const fullState = (runId) => {
18305
+ const state = ledger.read((db) => {
18306
+ const row = db.get("SELECT * FROM action_run WHERE id = ?", runId);
18307
+ return row ? runState(db, row) : void 0;
18308
+ });
18309
+ if (!state) return void 0;
18310
+ const ended = state.status === "succeeded" || state.status === "failed" || state.status === "cancelled";
18311
+ return {
18312
+ running: state.status !== "queued",
18313
+ log: state.log,
18314
+ logFrom: 0,
18315
+ batches: state.batches,
18316
+ batchFrom: 0,
18317
+ ...state.progress ? { progress: state.progress } : {},
18318
+ ...ended ? { end: {
18319
+ status: state.status,
18320
+ ...state.reason !== void 0 ? { reason: state.reason } : {},
18321
+ ...state.error ? { error: state.error } : {}
18322
+ } } : {}
18323
+ };
18324
+ };
18325
+ /** Records a relayed run under the hosted run's ID, once; then runs it. */
18326
+ const run = (server, session, params, drop) => {
18327
+ if (ledger.write((db) => {
18328
+ const existing = db.get("SELECT id, artifact_id FROM action_run WHERE id = ?", params.runId);
18329
+ if (existing) {
18330
+ if (existing.artifact_id !== params.artifactId) throw invalid$1("That run is another artifact's.");
18331
+ return true;
18332
+ }
18333
+ if (!db.get("SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?", params.artifactId, server)) throw new RpcError(createDiagnostic("MOSTRA_REMOTE_NOT_OWNER_INSTANCE", { message: "This instance doesn't share that artifact to this server." }));
18334
+ if (!db.get("SELECT id FROM version WHERE id = ? AND artifact_id = ?", params.versionId, params.artifactId)) throw invalid$1(`This instance has no version ${params.versionId} to run.`);
18335
+ const startedAt = new Date(options.now()).toISOString();
18336
+ db.run(`INSERT INTO action_run (id, artifact_id, version_id, view_id, viewer_id, action, args_json, key,
18337
+ client_key, status, started_at) VALUES (?, ?, ?, NULL, NULL, ?, ?, NULL, ?, 'queued', ?)`, params.runId, params.artifactId, params.versionId, params.fnId, JSON.stringify(params.args), `relay:${params.runId}`, startedAt);
18338
+ if (!db.get("SELECT 1 FROM event WHERE type = 'action.started' AND origin_json IS NOT NULL AND json_extract(payload_json, '$.runId') = ?", params.runId)) appendEvent(db, {
18339
+ artifactId: params.artifactId,
18340
+ type: "action.started",
18341
+ actor: { kind: "mostra" },
18342
+ causation: { runId: params.runId },
18343
+ payload: {
18344
+ runId: params.runId,
18345
+ action: params.fnId,
18346
+ relayed: true,
18347
+ server
18348
+ }
18349
+ });
18350
+ db.run(`INSERT INTO relayed_run (run_id, server, relay_seq, artifact_id, version_id, fn_id, started_at)
18351
+ VALUES (?, ?, ?, ?, ?, ?, ?)`, params.runId, server, params.seq, params.artifactId, params.versionId, params.fnId, startedAt);
18352
+ db.run("UPDATE remote_link SET relay_mark = MAX(relay_mark, ?) WHERE server = ?", params.seq, server);
18353
+ return false;
18354
+ })) {
18355
+ const state = fullState(params.runId);
18356
+ if (state) update(params.runId, session, state);
18357
+ return { accepted: true };
18358
+ }
18359
+ options.actions.runRelayed(params.runId, {
18360
+ viewer: params.viewer,
18361
+ viewId: params.viewId,
18362
+ observer: observer(params.runId, session),
18363
+ effect: effectOf(session, params.artifactId, { runId: params.runId })
18364
+ });
18365
+ if (loseAcks > 0) {
18366
+ loseAcks -= 1;
18367
+ log.warn("relay.test_ack_lost", { runId: params.runId });
18368
+ drop();
18369
+ throw invalid$1("A test lost this acknowledgement.");
18370
+ }
18371
+ return { accepted: true };
18372
+ };
18373
+ /** Each run the hosted home asks about, from what it holds on. */
18374
+ const reconcile = (params) => {
18375
+ const held = new Map((params.held ?? []).map((entry) => [entry.runId, entry]));
18376
+ return { runs: ledger.read((db) => params.runs.map((runId) => {
18377
+ const row = db.get("SELECT a.* FROM action_run a JOIN relayed_run r ON r.run_id = a.id WHERE a.id = ?", runId);
18378
+ if (!row) return {
18379
+ runId,
18380
+ state: "unknown-to-owner"
18381
+ };
18382
+ const position = held.get(runId);
18383
+ return {
18384
+ runId,
18385
+ state: runState(db, row, void 0, position ? {
18386
+ log: position.log,
18387
+ batches: position.batches
18388
+ } : void 0)
18389
+ };
18390
+ })) };
18391
+ };
18392
+ /** A relayed plain call: started now, its lines streamed back as `relay/call.batch`. */
18393
+ const call = async (server, session, params) => {
18394
+ if (!ledger.read((db) => db.get("SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?", params.artifactId, server))) throw new RpcError(createDiagnostic("MOSTRA_REMOTE_NOT_OWNER_INSTANCE", { message: "This instance doesn't share that artifact to this server." }));
18395
+ const abort = new AbortController();
18396
+ const lines = await options.actions.callRelayed({
18397
+ versionId: params.versionId,
18398
+ fnId: params.fnId,
18399
+ args: params.args,
18400
+ viewer: params.viewer,
18401
+ viewId: params.viewId,
18402
+ effect: effectOf(session, params.artifactId, { callId: params.callId })
18403
+ }, abort.signal);
18404
+ const inFlight = calls.get(server) ?? /* @__PURE__ */ new Map();
18405
+ inFlight.set(params.callId, abort);
18406
+ calls.set(server, inFlight);
18407
+ (async () => {
18408
+ let seq = 0;
18409
+ try {
18410
+ for await (const line of lines) {
18411
+ const open = await session();
18412
+ if (!open) {
18413
+ abort.abort();
18414
+ return;
18415
+ }
18416
+ seq += 1;
18417
+ await open.call(relayCallBatchMethod, "batch" in line ? {
18418
+ callId: params.callId,
18419
+ seq,
18420
+ batches: [line.batch]
18421
+ } : {
18422
+ callId: params.callId,
18423
+ seq,
18424
+ end: line.end
18425
+ });
18426
+ }
18427
+ } catch (error) {
18428
+ log.warn("relay.call_failed", {
18429
+ callId: params.callId,
18430
+ error
18431
+ });
18432
+ abort.abort();
18433
+ } finally {
18434
+ inFlight.delete(params.callId);
18435
+ }
18436
+ })();
18437
+ return { accepted: true };
18438
+ };
18439
+ return {
18440
+ handles: (method) => RELAY_METHODS.has(method),
18441
+ async request(server, session, method, params, drop) {
18442
+ switch (method) {
18443
+ case portRelayRunMethod.method: return run(server, session, portRelayRunMethod.params.parse(params), drop);
18444
+ case portRelayLiveMethod.method: {
18445
+ const parsed = portRelayLiveMethod.params.parse(params);
18446
+ if (!ledger.read((db) => db.get("SELECT artifact_id FROM hosted_artifact WHERE artifact_id = ? AND server = ?", parsed.artifactId, server))) return {};
18447
+ for (const key of parsed.keys) {
18448
+ const id = `${server} ${parsed.artifactId} ${key}`;
18449
+ if (parsed.interest === "stop") interest.delete(id);
18450
+ else interest.set(id, {
18451
+ artifactId: parsed.artifactId,
18452
+ key,
18453
+ until: options.now() + LIVE_INTEREST_MS
18454
+ });
18455
+ }
18456
+ options.liveChanged?.();
18457
+ return {};
18458
+ }
18459
+ case portRelayCancelMethod.method: {
18460
+ const { runId } = portRelayCancelMethod.params.parse(params);
18461
+ options.actions.cancelRelayed(runId);
18462
+ return {};
18463
+ }
18464
+ case portRelayReconcileMethod.method: return reconcile(portRelayReconcileMethod.params.parse(params));
18465
+ case portRelayCallMethod.method: return call(server, session, portRelayCallMethod.params.parse(params));
18466
+ default: throw invalid$1(`The relay serves no method ${method}.`);
18467
+ }
18468
+ },
18469
+ closed(server) {
18470
+ for (const abort of calls.get(server)?.values() ?? []) abort.abort();
18471
+ calls.delete(server);
18472
+ },
18473
+ liveInterest: () => interest.values(),
18474
+ sendLive(server, session, artifactId, versionId, produced) {
18475
+ (async () => {
18476
+ const open = await session();
18477
+ if (!open) return;
18478
+ try {
18479
+ await open.call(relayLiveValueMethod, {
18480
+ artifactId,
18481
+ versionId,
18482
+ key: produced.key,
18483
+ ...produced.value !== void 0 ? { value: produced.value } : {},
18484
+ ...produced.error !== void 0 ? { error: produced.error } : {},
18485
+ at: new Date(options.now()).toISOString()
18486
+ });
18487
+ } catch (error) {
18488
+ log.warn("relay.live_failed", {
18489
+ server,
18490
+ key: produced.key,
18491
+ error
18492
+ });
18493
+ }
18494
+ })();
18495
+ }
18496
+ };
18497
+ }
18498
+ //#endregion
18499
+ //#region src/link/manager.ts
18500
+ /**
18501
+ * The owner instance's links to remote servers (ARCHITECTURE §15.3, INTERFACES §18.2, D165 items 1
18502
+ * to 3): sign-in by the device grant, the refresh token kept in the home and the access token in
18503
+ * memory, one binding per signed-in server, and over it the hosted home's deliveries (imported into
18504
+ * this home's ledger and its own outbox in one transaction), the sync cursor (the hosted home's
18505
+ * events copied once each), and version pushes, queued while a server can't be reached.
18506
+ */
18507
+ /** The CLI's client ID at every server's authorization server (D166 item 5). */
18508
+ const CLIENT_ID = "mostra-cli";
18509
+ const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
18510
+ /** A token is refreshed this long before it expires, so a binding never meets its expiry. */
18511
+ const REFRESH_AHEAD_MS = 3e5;
18512
+ /** The first retry of a push that failed; each later one doubles, up to five minutes. */
18513
+ const PUSH_RETRY_MS = 1e3;
18514
+ const PUSH_RETRY_MAX_MS = 3e5;
18515
+ const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
18516
+ /** A server's short, stable key: 16 Crockford base32 characters of the SHA-256 of its origin. */
18517
+ /** Failures a push is tried again after: the connection, not the server's answer (§11.2). */
18518
+ const PUSH_TRANSPORT_FAILURES = /* @__PURE__ */ new Set([
18519
+ "MOSTRA_HOST_CONNECTION_CLOSED",
18520
+ "MOSTRA_HOST_TIMEOUT",
18521
+ "MOSTRA_REMOTE_UNREACHABLE",
18522
+ "MOSTRA_REMOTE_LOGIN_FAILED",
18523
+ "MOSTRA_REMOTE_OPERATOR_UNAVAILABLE",
18524
+ "MOSTRA_REMOTE_RATE_LIMITED"
18525
+ ]);
18526
+ function serverKey(server) {
18527
+ const digest = createHash("sha256").update(server, "utf8").digest();
18528
+ let out = "";
18529
+ let buffer = 0;
18530
+ let bits = 0;
18531
+ for (const byte of digest) {
18532
+ buffer = (buffer << 8 | byte) & 65535;
18533
+ bits += 8;
18534
+ while (bits >= 5 && out.length < 16) {
18535
+ bits -= 5;
18536
+ out += CROCKFORD.charAt(buffer >> bits & 31);
18537
+ }
18538
+ if (out.length >= 16) break;
18539
+ }
18540
+ return out;
18541
+ }
18542
+ /** An API origin as the instance keeps it: scheme and host, no path. */
18543
+ function serverOrigin(value) {
18544
+ let url;
18545
+ try {
18546
+ url = new URL(value.includes("://") ? value : `https://${value}`);
18547
+ } catch {
18548
+ throw invalid(`${value} isn't a server address.`);
18549
+ }
18550
+ if (url.protocol !== "https:" && url.protocol !== "http:") throw invalid(`${value} isn't an https: address.`);
18551
+ if (url.protocol === "http:" && !loopbackName(url.hostname)) throw invalid(`${value} must be https: (plain http is only for a server on this machine).`);
18552
+ return url.origin;
18553
+ }
18554
+ function loopbackName(host) {
18555
+ return host === "localhost" || host.endsWith(".localhost") || host === "127.0.0.1" || host === "[::1]";
18556
+ }
18557
+ function invalid(message) {
18558
+ return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }));
18559
+ }
18560
+ function loginFailed(message) {
18561
+ return new RpcError(createDiagnostic("MOSTRA_REMOTE_LOGIN_FAILED", { message }));
18562
+ }
18563
+ /** Creates the owner's link manager. */
18564
+ function createLink(options) {
18565
+ const { ledger, log } = options;
18566
+ const now = options.now ?? Date.now;
18567
+ const connectTo = testServerAddress(options.env["MOSTRA_TEST_SERVER_ADDRESS"]);
18568
+ const keysDir = join(options.home, "keys", "remote");
18569
+ const connections = /* @__PURE__ */ new Map();
18570
+ const logins = /* @__PURE__ */ new Map();
18571
+ let stopped = false;
18572
+ const credentialsFile = (server) => join(keysDir, `${serverKey(server)}.json`);
18573
+ const readCredentials = (server) => {
18574
+ const file = credentialsFile(server);
18575
+ if (!existsSync(file)) return void 0;
18576
+ try {
18577
+ const parsed = JSON.parse(readFileSync(file, "utf8"));
18578
+ if (parsed.server !== server || typeof parsed.refreshToken !== "string") return void 0;
18579
+ options.protect(parsed.refreshToken);
18580
+ return parsed;
18581
+ } catch {
18582
+ return;
18583
+ }
18584
+ };
18585
+ const writeCredentials = (credentials) => {
18586
+ mkdirSync(keysDir, {
18587
+ recursive: true,
18588
+ mode: 448
18589
+ });
18590
+ options.protect(credentials.refreshToken);
18591
+ const target = credentialsFile(credentials.server);
18592
+ const temporary = `${target}.${process.pid}.tmp`;
18593
+ writeFileSync(temporary, JSON.stringify(credentials), { mode: 384 });
18594
+ renamePathSync(temporary, target);
18595
+ };
18596
+ const forgetCredentials = (server) => {
18597
+ rmSync(credentialsFile(server), { force: true });
18598
+ };
18599
+ /**
18600
+ * The hosted links this home's remote shares minted, kept beside its credentials (mode 0600,
18601
+ * never in the ledger, D44), so `share/list` shows them again: their secrets are the hosted
18602
+ * home's, which this home can't derive.
18603
+ */
18604
+ const linksFile = (server) => join(keysDir, `${serverKey(server)}.links.json`);
18605
+ const readLinks = (server) => {
18606
+ try {
18607
+ const parsed = JSON.parse(readFileSync(linksFile(server), "utf8"));
18608
+ for (const url of Object.values(parsed)) protectLink(url);
18609
+ return parsed;
18610
+ } catch {
18611
+ return {};
18612
+ }
18613
+ };
18614
+ const writeLinks = (server, links) => {
18615
+ mkdirSync(keysDir, {
18616
+ recursive: true,
18617
+ mode: 448
18618
+ });
18619
+ const target = linksFile(server);
18620
+ const temporary = `${target}.${process.pid}.tmp`;
18621
+ writeFileSync(temporary, JSON.stringify(links), { mode: 384 });
18622
+ renamePathSync(temporary, target);
18623
+ };
18624
+ /** A link's secret is in its fragment: it never reaches a log. */
18625
+ const protectLink = (url) => {
18626
+ const secret = /#t=([^&]+)/.exec(url)?.[1];
18627
+ if (secret !== void 0) options.protect(secret);
18628
+ };
18629
+ const linkRow = (db, server) => db.get("SELECT * FROM remote_link WHERE server = ?", server);
18630
+ const owned = (server) => (db, artifactId) => db.get("SELECT 1 FROM hosted_artifact WHERE artifact_id = ? AND server = ?", artifactId, server) !== void 0;
18631
+ /** A fresh access token, refreshing (and rotating the refresh token) when it's due. */
18632
+ const accessToken = (connection) => {
18633
+ if (connection.access && connection.access.expiresAt - now() > REFRESH_AHEAD_MS) return Promise.resolve(connection.access.token);
18634
+ connection.refreshing ??= (async () => {
18635
+ await Promise.resolve();
18636
+ try {
18637
+ const credentials = readCredentials(connection.server);
18638
+ if (!credentials) throw loginFailed(`This home isn't signed in to ${connection.server}.`);
18639
+ const answer = await postForm(connection.server, "/oauth/token", {
18640
+ client_id: CLIENT_ID,
18641
+ grant_type: "refresh_token",
18642
+ refresh_token: credentials.refreshToken
18643
+ }, connectTo);
18644
+ if (answer.status !== 200 || typeof answer.json["access_token"] !== "string") {
18645
+ const error = String(answer.json["error"] ?? answer.status);
18646
+ if (error === "invalid_grant") signedOut(connection, "the sign-in was revoked or has expired");
18647
+ throw loginFailed(`${connection.server} refused this home's sign-in (${error}).`);
18648
+ }
18649
+ const token = answer.json["access_token"];
18650
+ const refresh = answer.json["refresh_token"];
18651
+ options.protect(token);
18652
+ if (typeof refresh === "string") writeCredentials({
18653
+ ...credentials,
18654
+ refreshToken: refresh
18655
+ });
18656
+ connection.access = {
18657
+ token,
18658
+ expiresAt: now() + Number(answer.json["expires_in"] ?? 3600) * 1e3
18659
+ };
18660
+ scheduleRefresh(connection);
18661
+ return token;
18662
+ } finally {
18663
+ connection.refreshing = void 0;
18664
+ }
18665
+ })();
18666
+ return connection.refreshing;
18667
+ };
18668
+ /** Refreshes the token in band before it expires, so the server never closes the binding. */
18669
+ const scheduleRefresh = (connection) => {
18670
+ if (connection.refreshTimer) clearTimeout(connection.refreshTimer);
18671
+ if (!connection.access) return;
18672
+ const delay = Math.max(1e3, connection.access.expiresAt - now() - REFRESH_AHEAD_MS + 1e3);
18673
+ connection.refreshTimer = setTimeout(() => {
18674
+ connection.refreshTimer = void 0;
18675
+ (async () => {
18676
+ try {
18677
+ const token = await accessToken(connection);
18678
+ const session = connection.binding.session;
18679
+ if (session) await session.call(remoteTokenMethod, { accessToken: token });
18680
+ } catch (error) {
18681
+ log.warn("link.refresh_failed", {
18682
+ server: connection.server,
18683
+ error: error.message
18684
+ });
18685
+ }
18686
+ })();
18687
+ }, delay);
18688
+ connection.refreshTimer.unref?.();
18689
+ };
18690
+ const signedOut = (connection, why) => {
18691
+ connection.state = "signed-out";
18692
+ connection.lastError = why;
18693
+ connection.since = new Date(now()).toISOString();
18694
+ ledger.write((db) => db.run("UPDATE remote_link SET state = 'signed-out' WHERE server = ?", connection.server));
18695
+ connection.binding.stop();
18696
+ log.warn("link.signed_out", {
18697
+ server: connection.server,
18698
+ why
18699
+ });
18700
+ };
18701
+ /** Sessions whose follow failed: their later pages are never copied (Opus review of WP-29, 2). */
18702
+ const broken = /* @__PURE__ */ new WeakSet();
18703
+ /**
18704
+ * Reads again each followed submission this home doesn't hold (restored from a backup taken
18705
+ * before it, or a delivery it committed and then lost) and imports it as a delivery would, keeping
18706
+ * its IDs; its local deliveries repeat their derived IDs (FAILURE_MODEL §11.2). The IDs wait in
18707
+ * `remote_missing`, written with the cursor that passed them, until imported: a read that fails
18708
+ * is tried again on the next page and the next session (Opus review of WP-29, 3).
18709
+ */
18710
+ const reimporting = /* @__PURE__ */ new Set();
18711
+ /** Tests: the one submission whose first read again fails (`MOSTRA_TEST_REIMPORT_FAIL`). */
18712
+ let failOnce = options.env["MOSTRA_TEST_REIMPORT_FAIL"];
18713
+ const reimport = (connection, session, source) => {
18714
+ const missing = ledger.read((db) => db.all("SELECT submission_id FROM remote_missing WHERE server = ? ORDER BY noted_at, submission_id", connection.server).map((row) => row.submission_id));
18715
+ for (const submissionId of missing) {
18716
+ const key = `${connection.server} ${submissionId}`;
18717
+ if (reimporting.has(key)) continue;
18718
+ reimporting.add(key);
18719
+ (async () => {
18720
+ try {
18721
+ if (failOnce === submissionId) {
18722
+ failOnce = void 0;
18723
+ throw new Error("a test's failed read");
18724
+ }
18725
+ const envelope = await session.call(hostedSubmissionMethod, { submissionId }, { timeoutMs: 3e4 });
18726
+ const answer = options.importSubmission(envelope, source);
18727
+ if (answer.accepted || !answer.retry) ledger.write((db) => db.run("DELETE FROM remote_missing WHERE server = ? AND submission_id = ?", connection.server, submissionId));
18728
+ log.info("link.submission_reimported", {
18729
+ server: connection.server,
18730
+ submissionId,
18731
+ accepted: answer.accepted
18732
+ });
18733
+ } catch (error) {
18734
+ log.warn("link.reimport_failed", {
18735
+ server: connection.server,
18736
+ submissionId,
18737
+ error
18738
+ });
18739
+ } finally {
18740
+ reimporting.delete(key);
18741
+ }
18742
+ })();
18743
+ }
18744
+ };
18745
+ /** Copies one page of followed events, then acknowledges it (the sync cursor, §18.2). */
18746
+ const follow = (connection, page, session) => {
18747
+ const home = connection.home;
18748
+ const incarnation = connection.incarnation;
18749
+ if (!home || !incarnation) return;
18750
+ const source = {
18751
+ server: connection.server,
18752
+ serverKey: serverKey(connection.server),
18753
+ home,
18754
+ incarnation
18755
+ };
18756
+ ledger.write((db) => {
18757
+ const followed = followEvents(db, {
18758
+ events: page.events,
18759
+ source,
18760
+ owned: owned(connection.server)
18761
+ });
18762
+ db.run("UPDATE remote_link SET follow_cursor = ?, incarnation = ? WHERE server = ?", page.cursor, incarnation, connection.server);
18763
+ for (const submissionId of followed.missing) db.run("INSERT OR IGNORE INTO remote_missing (server, submission_id, noted_at) VALUES (?, ?, ?)", connection.server, submissionId, new Date(now()).toISOString());
18764
+ return followed;
18765
+ });
18766
+ reimport(connection, session, source);
18767
+ session.notify(eventsAckMethod.method, {
18768
+ subscriptionId: page.subscriptionId,
18769
+ cursor: page.cursor
18770
+ });
18771
+ };
18772
+ /** Every binding message, appended to a file (tests: the relay's secrets grep). */
18773
+ const traceFile = options.env["MOSTRA_TEST_BINDING_TRACE"];
18774
+ const trace = traceFile === void 0 || traceFile === "" ? void 0 : (direction, text) => appendFileSync(traceFile, `${direction} ${Date.now()} ${text}\n`);
18775
+ /**
18776
+ * The binding's session, or the one opening now: a relay message can arrive while the session's
18777
+ * own start (its subscription) is still under way, and an update sent then mustn't be lost.
18778
+ */
18779
+ const readyMs = Number(options.env["MOSTRA_TEST_RELAY_READY_MS"] ?? "") || 1e4;
18780
+ const readySoon = (connection) => {
18781
+ let timer;
18782
+ return Promise.race([connection.binding.ready(), new Promise((resolve) => {
18783
+ timer = setTimeout(() => resolve(void 0), readyMs);
18784
+ })]).finally(() => clearTimeout(timer));
18785
+ };
18786
+ const relay = createOwnerRelay({
18787
+ ledger,
18788
+ log,
18789
+ actions: options.actions,
18790
+ now,
18791
+ loseAcks: Number(options.env["MOSTRA_TEST_RELAY_LOSE_ACK"] ?? "0") || 0,
18792
+ ...options.liveChanged ? { liveChanged: options.liveChanged } : {}
18793
+ });
18794
+ const onRequest = (connection) => (method, params) => {
18795
+ if (relay.handles(method)) return relay.request(connection.server, () => connection.binding.session ?? readySoon(connection), method, params, () => connection.binding.reconnect());
18796
+ if (method === portSubmissionsDeliverMethod.method) {
18797
+ const parsed = SubmissionEnvelope.safeParse(params?.envelope ?? params);
18798
+ if (!parsed.success) return {
18799
+ accepted: false,
18800
+ reason: "invalid: not a submission envelope",
18801
+ retry: false
18802
+ };
18803
+ if (!connection.home || !connection.incarnation) return {
18804
+ accepted: false,
18805
+ reason: "the session isn't open yet",
18806
+ retry: true
18807
+ };
18808
+ return options.importSubmission(parsed.data, {
18809
+ server: connection.server,
18810
+ serverKey: serverKey(connection.server),
18811
+ home: connection.home,
18812
+ incarnation: connection.incarnation
18813
+ });
18814
+ }
18815
+ throw new RpcError(createDiagnostic("MOSTRA_HOST_METHOD_UNKNOWN", { message: `This instance doesn't serve ${method}.` }));
18816
+ };
18817
+ const connect = (server) => {
18818
+ const existing = connections.get(server);
18819
+ if (existing && existing.state !== "signed-out") return existing;
18820
+ const connection = {
18821
+ server,
18822
+ binding: void 0,
18823
+ state: "connecting",
18824
+ since: new Date(now()).toISOString()
18825
+ };
18826
+ connections.set(server, connection);
18827
+ connection.binding = startBinding({
18828
+ ...trace ? { trace } : {},
18829
+ server,
18830
+ ...connectTo ? { connectTo } : {},
18831
+ accessToken: () => accessToken(connection),
18832
+ initialize: () => {
18833
+ const row = ledger.read((db) => linkRow(db, server));
18834
+ return ownerInitialize({
18835
+ name: "mostra-runtime",
18836
+ version: options.runtimeVersion,
18837
+ instanceId: row?.instance_id ?? "",
18838
+ relayMark: row?.relay_mark ?? 0,
18839
+ ...row?.incarnation && row.follow_cursor ? { followed: {
18840
+ incarnation: row.incarnation,
18841
+ cursor: row.follow_cursor
18842
+ } } : {}
18843
+ });
18844
+ },
18845
+ onRequest: onRequest(connection),
18846
+ onNotification: (method, params, session) => {
18847
+ if (method !== "events/notify") return;
18848
+ if (broken.has(session)) return;
18849
+ try {
18850
+ follow(connection, params, session);
18851
+ } catch (error) {
18852
+ broken.add(session);
18853
+ log.warn("link.follow_failed", {
18854
+ server,
18855
+ error
18856
+ });
18857
+ connection.binding.reconnect();
18858
+ }
18859
+ },
18860
+ onOpen: async (session) => {
18861
+ connection.home = session.result.instance.home;
18862
+ connection.incarnation = session.result.instance.incarnation ?? "";
18863
+ const row = ledger.read((db) => linkRow(db, server));
18864
+ const sameIncarnation = row?.incarnation === connection.incarnation;
18865
+ const head = session.result.instance.cursor ?? "0";
18866
+ const regressed = row?.incarnation !== null && row?.incarnation !== void 0 && !sameIncarnation && row.follow_cursor !== null;
18867
+ const after = sameIncarnation ? row?.follow_cursor ?? void 0 : regressed ? String(Math.min(Number(row?.follow_cursor ?? "0"), Number(head))) : void 0;
18868
+ if (regressed) {
18869
+ ledger.write((db) => {
18870
+ appendEvent(db, {
18871
+ artifactId: null,
18872
+ type: "remote.regressed",
18873
+ actor: { kind: "mostra" },
18874
+ payload: {
18875
+ server,
18876
+ home: connection.home,
18877
+ from: row?.follow_cursor,
18878
+ to: head,
18879
+ incarnation: {
18880
+ from: row?.incarnation,
18881
+ to: connection.incarnation
18882
+ }
18883
+ }
18884
+ });
18885
+ db.run("UPDATE remote_link SET follow_cursor = ?, incarnation = ? WHERE server = ?", after ?? null, connection.incarnation ?? null, server);
18886
+ });
18887
+ log.warn("link.home_regressed", {
18888
+ server,
18889
+ from: row?.follow_cursor,
18890
+ to: head
18891
+ });
18892
+ }
18893
+ await session.call(eventsSubscribeMethod, {
18894
+ name: `owner:${row?.instance_id ?? ""}`,
18895
+ ...after !== void 0 ? { after } : {}
18896
+ });
18897
+ connection.state = "connected";
18898
+ connection.since = new Date(now()).toISOString();
18899
+ delete connection.lastError;
18900
+ if (connection.home && connection.incarnation) reimport(connection, session, {
18901
+ server,
18902
+ serverKey: serverKey(server),
18903
+ home: connection.home,
18904
+ incarnation: connection.incarnation
18905
+ });
18906
+ drainPushes(connection);
18907
+ },
18908
+ onClose: (why) => {
18909
+ relay.closed(server);
18910
+ if (connection.state === "signed-out") return;
18911
+ if (connection.state === "connected") connection.since = new Date(now()).toISOString();
18912
+ connection.state = "offline";
18913
+ connection.lastError = why;
18914
+ },
18915
+ onUnauthorized: () => {
18916
+ delete connection.access;
18917
+ },
18918
+ log
18919
+ });
18920
+ return connection;
18921
+ };
18922
+ /** The canonical listing of a version's output, with sizes (DATA_MODEL §2), read from its directory. */
18923
+ const listingOf = (versionDir) => {
18924
+ const entries = [];
18925
+ const walk = (dir, relative) => {
18926
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
18927
+ const path = `${relative}/${entry.name}`;
18928
+ const absolute = join(dir, entry.name);
18929
+ if (entry.isDirectory()) walk(absolute, path);
18930
+ else if (entry.isFile()) {
18931
+ const bytes = readFileSync(absolute);
18932
+ entries.push([
18933
+ path,
18934
+ false,
18935
+ createHash("sha256").update(bytes).digest("hex"),
18936
+ bytes.length
18937
+ ]);
18938
+ }
18939
+ }
18940
+ };
18941
+ for (const top of ["client", "server"]) if (existsSync(join(versionDir, top))) walk(join(versionDir, top), top);
18942
+ const registry = readFileSync(join(versionDir, "registry.json"));
18943
+ entries.push([
18944
+ "registry.json",
18945
+ false,
18946
+ createHash("sha256").update(registry).digest("hex"),
18947
+ registry.length
18948
+ ]);
18949
+ return entries;
18950
+ };
18951
+ /** Pushes one version: the push, then the files the server names as missing, then the push again. */
18952
+ const pushVersion = async (connection, artifactId, versionId) => {
18953
+ const row = ledger.read((db) => db.get("SELECT number, source_digest, env_digest, output_digest FROM version WHERE id = ? AND artifact_id = ?", versionId, artifactId));
18954
+ if (!row) throw invalid(`${artifactId} has no version ${versionId}.`);
18955
+ const dir = options.versionDir(versionId);
18956
+ const listing = listingOf(dir);
18957
+ if (listingDigest(listing.map(([path, executable, sha256]) => ({
18958
+ path,
18959
+ executable,
18960
+ sha256
18961
+ }))) !== row.output_digest) throw new RpcError(createDiagnostic("MOSTRA_HOME_INTEGRITY", { message: `Version ${versionId}'s files don't match its output digest; publish its source again first.` }));
18962
+ const version = {
18963
+ versionId,
18964
+ number: row.number,
18965
+ sourceDigest: row.source_digest,
18966
+ envDigest: row.env_digest,
18967
+ outputDigest: row.output_digest,
18968
+ manifest: readFileSync(join(dir, "manifest.json"), "utf8"),
18969
+ registry: readFileSync(join(dir, "registry.json"), "utf8"),
18970
+ lock: readFileSync(join(dir, "lock.yaml"), "utf8"),
18971
+ listing,
18972
+ ...(() => {
18973
+ const found = ledger.read((db) => db.get("SELECT policy_json FROM artifact WHERE id = ?", artifactId)?.policy_json);
18974
+ return found ? { policy: JSON.parse(found) } : {};
18975
+ })()
18976
+ };
18977
+ const session = await connection.binding.ready();
18978
+ let answer = await session.call(hostedPushMethod, {
18979
+ artifactId,
18980
+ version
18981
+ });
18982
+ for (let round = 0; answer.status === "missing" && round < 3; round += 1) {
18983
+ const wanted = new Set(answer.files);
18984
+ const token = await accessToken(connection);
18985
+ for (const [path, , sha256, size] of listing) {
18986
+ if (!wanted.has(sha256) || !path.startsWith("client/")) continue;
18987
+ wanted.delete(sha256);
18988
+ const body = readFileSync(join(dir, path));
18989
+ if (body.length !== size) throw invalid(`${path} changed while it was pushed.`);
18990
+ const put = await remoteRequest(connection.server, `/files/${sha256}`, {
18991
+ method: "PUT",
18992
+ headers: {
18993
+ authorization: `Bearer ${token}`,
18994
+ "content-type": "application/octet-stream"
18995
+ },
18996
+ body,
18997
+ timeoutMs: 3e5,
18998
+ ...connectTo ? { connectTo } : {}
18999
+ });
19000
+ if (put.status !== 201 && put.status !== 200) throw new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: `${connection.server} refused the file ${path} (${put.status}).` }));
19001
+ }
19002
+ answer = await session.call(hostedPushMethod, {
19003
+ artifactId,
19004
+ version
19005
+ });
19006
+ }
19007
+ if (answer.status !== "complete") throw new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: `${connection.server} still lacks files of ${versionId} after they were sent.` }));
19008
+ ledger.write((db) => {
19009
+ db.run("UPDATE hosted_artifact SET hosted_home = ?, route_id = ?, label = ? WHERE artifact_id = ? AND server = ?", session.result.instance.home, answer.route.routeId, answer.route.label, artifactId, connection.server);
19010
+ db.run("UPDATE version_push SET state = 'complete' WHERE artifact_id = ? AND version_id = ? AND server = ?", artifactId, versionId, connection.server);
19011
+ });
19012
+ log.info("link.pushed", {
19013
+ server: connection.server,
19014
+ artifactId,
19015
+ versionId
19016
+ });
19017
+ return {
19018
+ versionId,
19019
+ number: answer.number,
19020
+ route: answer.route,
19021
+ server: connection.server,
19022
+ home: session.result.instance.home
19023
+ };
19024
+ };
19025
+ /** Sends every pending push of a server, oldest first, retrying with backoff while it fails. */
19026
+ /**
19027
+ * Whether a push failed for good: the server (or this home, for a damaged version) answered it
19028
+ * with a refusal, not a connection that closed or a call that timed out. Such a push is never
19029
+ * tried again, and nothing waits behind it (Opus review of WP-29, 10).
19030
+ */
19031
+ const refusedForGood = (error) => error instanceof RpcError && !PUSH_TRANSPORT_FAILURES.has(error.diagnostic.code);
19032
+ /** Records a push the server refused for good, and says so in `remote/status`. */
19033
+ const refusePush = (connection, artifactId, versionId, error) => {
19034
+ const why = `${error.diagnostic.code}: ${error.diagnostic.message}`;
19035
+ ledger.write((db) => {
19036
+ db.run("UPDATE version_push SET state = 'refused', refusal = ? WHERE artifact_id = ? AND version_id = ? AND server = ? AND state = 'pending'", why, artifactId, versionId, connection.server);
19037
+ db.run("DELETE FROM hosted_artifact WHERE artifact_id = ? AND server = ? AND route_id = ''", artifactId, connection.server);
19038
+ });
19039
+ connection.lastError = `The server refused ${artifactId}'s version ${versionId}: ${why}`;
19040
+ log.warn("link.push_refused", {
19041
+ server: connection.server,
19042
+ artifactId,
19043
+ versionId,
19044
+ code: error.diagnostic.code
19045
+ });
19046
+ };
19047
+ const drainPushes = (connection, attempt = 0) => {
19048
+ if (connection.pushing) return connection.pushing;
19049
+ const drain = (async () => {
19050
+ await Promise.resolve();
19051
+ try {
19052
+ for (;;) {
19053
+ if (stopped || !connection.binding.session) return;
19054
+ const next = ledger.read((db) => db.get("SELECT artifact_id, version_id FROM version_push WHERE server = ? AND state = 'pending' ORDER BY created_at, rowid LIMIT 1", connection.server));
19055
+ if (!next) return;
19056
+ try {
19057
+ await pushVersion(connection, next.artifact_id, next.version_id);
19058
+ } catch (error) {
19059
+ if (!refusedForGood(error)) throw error;
19060
+ refusePush(connection, next.artifact_id, next.version_id, error);
19061
+ }
19062
+ }
19063
+ } catch (error) {
19064
+ log.warn("link.push_failed", {
19065
+ server: connection.server,
19066
+ error: error.message
19067
+ });
19068
+ ledger.write((db) => db.run("UPDATE version_push SET attempts = attempts + 1 WHERE server = ? AND state = 'pending'", connection.server));
19069
+ if (connection.pushTimer || stopped) return;
19070
+ connection.pushTimer = setTimeout(() => {
19071
+ connection.pushTimer = void 0;
19072
+ drainPushes(connection, attempt + 1);
19073
+ }, Math.min(PUSH_RETRY_MAX_MS, PUSH_RETRY_MS * 2 ** attempt));
19074
+ connection.pushTimer.unref?.();
19075
+ }
19076
+ })();
19077
+ connection.pushing = drain;
19078
+ drain.finally(() => {
19079
+ if (connection.pushing === drain) connection.pushing = void 0;
19080
+ });
19081
+ return drain;
19082
+ };
19083
+ const queuePush = (db, artifactId, versionId, server) => {
19084
+ db.run(`INSERT INTO version_push (artifact_id, version_id, server, state, attempts, created_at) VALUES (?, ?, ?, 'pending', 0, ?)
19085
+ ON CONFLICT (artifact_id, version_id, server) DO NOTHING`, artifactId, versionId, server, new Date(now()).toISOString());
19086
+ };
19087
+ const resolveServer = (server) => {
19088
+ if (server !== void 0) return serverOrigin(server);
19089
+ const last = ledger.read((db) => db.get("SELECT server FROM remote_link ORDER BY linked_at DESC LIMIT 1"));
19090
+ if (!last) throw invalid("Name the server to sign in to: mostra login <server>. No server is configured by default.");
19091
+ return last.server;
19092
+ };
19093
+ /** Starts a sign-in: the device grant's code, then polls for the token in the background. */
19094
+ const login = async (requested, makeDefault = false) => {
19095
+ const server = resolveServer(requested);
19096
+ const existing = ledger.read((db) => linkRow(db, server));
19097
+ const started = await postForm(server, "/oauth/device", {
19098
+ client_id: CLIENT_ID,
19099
+ mostra_name: hostname().slice(0, 128) || "Mostra instance",
19100
+ ...existing?.instance_id ? { mostra_instance: existing.instance_id } : {},
19101
+ ...makeDefault ? { mostra_default: "1" } : {}
19102
+ }, connectTo);
19103
+ const deviceCode = started.json["device_code"];
19104
+ const userCode = started.json["user_code"];
19105
+ const uri = started.json["verification_uri"];
19106
+ if (started.status !== 200 || typeof deviceCode !== "string" || typeof userCode !== "string" || typeof uri !== "string") throw loginFailed(`${server} didn't start a sign-in (${String(started.json["error"] ?? started.status)}).`);
19107
+ options.protect(deviceCode);
19108
+ const expiresIn = Number(started.json["expires_in"] ?? 600);
19109
+ const interval = Math.max(1, Number(started.json["interval"] ?? 5));
19110
+ const complete = started.json["verification_uri_complete"];
19111
+ const result = {
19112
+ loginId: `login_${randomUUID()}`,
19113
+ server,
19114
+ verificationUri: uri,
19115
+ ...typeof complete === "string" ? { verificationUriComplete: complete } : {},
19116
+ userCode,
19117
+ expiresAt: new Date(now() + expiresIn * 1e3).toISOString()
19118
+ };
19119
+ const pending = {
19120
+ id: result.loginId,
19121
+ server,
19122
+ result,
19123
+ waiters: []
19124
+ };
19125
+ logins.set(result.loginId, pending);
19126
+ const finish = (outcome) => {
19127
+ pending.outcome = outcome;
19128
+ for (const wake of pending.waiters.splice(0)) wake();
19129
+ setTimeout(() => logins.delete(pending.id), 6e5).unref();
19130
+ };
19131
+ (async () => {
19132
+ let wait = interval * 1e3;
19133
+ const deadline = now() + expiresIn * 1e3;
19134
+ while (!stopped && now() < deadline) {
19135
+ await new Promise((resolve) => setTimeout(resolve, wait).unref());
19136
+ let answer;
19137
+ try {
19138
+ answer = await postForm(server, "/oauth/token", {
19139
+ client_id: CLIENT_ID,
19140
+ grant_type: DEVICE_GRANT,
19141
+ device_code: deviceCode
19142
+ }, connectTo);
19143
+ } catch {
19144
+ continue;
19145
+ }
19146
+ const error = answer.json["error"];
19147
+ if (error === "authorization_pending") continue;
19148
+ if (error === "slow_down") {
19149
+ wait += 5e3;
19150
+ continue;
19151
+ }
19152
+ if (answer.status !== 200 || typeof answer.json["refresh_token"] !== "string") {
19153
+ finish({
19154
+ ok: false,
19155
+ error: loginFailed(`${server} refused the sign-in (${String(error ?? answer.status)}).`)
19156
+ });
19157
+ return;
19158
+ }
19159
+ const account = answer.json["mostra_account"];
19160
+ const instanceId = String(answer.json["mostra_instance"]);
19161
+ const refreshToken = answer.json["refresh_token"];
19162
+ const accessToken = String(answer.json["access_token"]);
19163
+ options.protect(accessToken);
19164
+ writeCredentials({
19165
+ server,
19166
+ refreshToken,
19167
+ instanceId
19168
+ });
19169
+ ledger.write((db) => db.run(`INSERT INTO remote_link (server, account_id, account_name, instance_id, is_default, linked_at, state)
19170
+ VALUES (?, ?, ?, ?, ?, ?, 'signed-in')
19171
+ ON CONFLICT (server) DO UPDATE SET
19172
+ -- Another link (after --forget, or another account) starts with no marks: a mark
19173
+ -- of the old link would make the new one's home look restored (Opus review of
19174
+ -- WP-29, 9).
19175
+ relay_mark = CASE WHEN remote_link.instance_id IS excluded.instance_id
19176
+ AND remote_link.account_id = excluded.account_id THEN remote_link.relay_mark ELSE 0 END,
19177
+ follow_cursor = CASE WHEN remote_link.instance_id IS excluded.instance_id
19178
+ AND remote_link.account_id = excluded.account_id THEN remote_link.follow_cursor ELSE NULL END,
19179
+ incarnation = CASE WHEN remote_link.instance_id IS excluded.instance_id
19180
+ AND remote_link.account_id = excluded.account_id THEN remote_link.incarnation ELSE NULL END,
19181
+ account_id = excluded.account_id, account_name = excluded.account_name,
19182
+ instance_id = excluded.instance_id, is_default = excluded.is_default, linked_at = excluded.linked_at,
19183
+ state = 'signed-in'`, server, account.id, account.name ?? null, instanceId, answer.json["mostra_default"] === true ? 1 : 0, new Date(now()).toISOString()));
19184
+ const previous = connections.get(server);
19185
+ if (previous) {
19186
+ await previous.binding.stop();
19187
+ connections.delete(server);
19188
+ }
19189
+ const connection = connect(server);
19190
+ connection.access = {
19191
+ token: accessToken,
19192
+ expiresAt: now() + Number(answer.json["expires_in"] ?? 3600) * 1e3
19193
+ };
19194
+ scheduleRefresh(connection);
19195
+ log.info("link.signed_in", {
19196
+ server,
19197
+ instanceId
19198
+ });
19199
+ finish({
19200
+ ok: true,
19201
+ account: {
19202
+ id: account.id,
19203
+ ...account.name ? { name: account.name } : {}
19204
+ },
19205
+ instanceId
19206
+ });
19207
+ return;
19208
+ }
19209
+ finish({
19210
+ ok: false,
19211
+ error: loginFailed(`The sign-in to ${server} expired before it was approved.`)
19212
+ });
19213
+ })();
19214
+ return result;
19215
+ };
19216
+ const status = () => ledger.read((db) => {
19217
+ return db.all("SELECT * FROM remote_link ORDER BY linked_at").map((row) => {
19218
+ const connection = connections.get(row.server);
19219
+ const pending = db.get("SELECT COUNT(*) AS n FROM version_push WHERE server = ? AND state = 'pending'", row.server)?.n ?? 0;
19220
+ const state = row.state === "signed-out" ? "signed-out" : connection?.state ?? "offline";
19221
+ return {
19222
+ server: row.server,
19223
+ account: {
19224
+ id: row.account_id ?? "",
19225
+ ...row.account_name ? { name: row.account_name } : {}
19226
+ },
19227
+ instanceId: row.instance_id ?? "",
19228
+ name: hostname().slice(0, 128) || "Mostra instance",
19229
+ default: row.is_default === 1,
19230
+ state,
19231
+ since: connection?.since ?? row.linked_at,
19232
+ pendingPushes: pending,
19233
+ ...connection?.lastError ? { lastError: connection.lastError } : {}
19234
+ };
19235
+ });
19236
+ });
19237
+ const self = {
19238
+ methods() {
19239
+ return [
19240
+ handler(remoteLoginMethod, (params) => login(params.server, params.default === true)),
19241
+ handler(remoteLoginWaitMethod, async (params) => {
19242
+ const pending = logins.get(params.loginId);
19243
+ if (!pending) throw invalid(`No sign-in ${params.loginId} is in progress.`);
19244
+ if (!pending.outcome && params.timeoutMs > 0) await new Promise((resolve) => {
19245
+ const timer = setTimeout(resolve, params.timeoutMs);
19246
+ pending.waiters.push(() => {
19247
+ clearTimeout(timer);
19248
+ resolve();
19249
+ });
19250
+ });
19251
+ const outcome = pending.outcome;
19252
+ if (!outcome) return { status: "pending" };
19253
+ if (!outcome.ok) throw outcome.error;
19254
+ return {
19255
+ status: "signed-in",
19256
+ server: pending.server,
19257
+ account: outcome.account,
19258
+ instanceId: outcome.instanceId
19259
+ };
19260
+ }),
19261
+ handler(remoteLogoutMethod, async (params) => {
19262
+ const servers = params.server !== void 0 ? [serverOrigin(params.server)] : ledger.read((db) => db.all("SELECT server FROM remote_link WHERE state = 'signed-in'")).map((row) => row.server);
19263
+ const done = [];
19264
+ for (const server of servers) {
19265
+ const credentials = readCredentials(server);
19266
+ const connection = connections.get(server);
19267
+ if (connection) {
19268
+ await connection.binding.stop();
19269
+ if (connection.refreshTimer) clearTimeout(connection.refreshTimer);
19270
+ if (connection.pushTimer) clearTimeout(connection.pushTimer);
19271
+ connections.delete(server);
19272
+ }
19273
+ if (credentials) try {
19274
+ await postForm(server, "/oauth/revoke", {
19275
+ token: credentials.refreshToken,
19276
+ client_id: CLIENT_ID
19277
+ }, connectTo);
19278
+ } catch (error) {
19279
+ log.warn("link.revoke_failed", {
19280
+ server,
19281
+ error: error.message
19282
+ });
19283
+ }
19284
+ forgetCredentials(server);
19285
+ ledger.write((db) => db.run(`UPDATE remote_link SET state = 'signed-out'${params.forget ? ", instance_id = NULL" : ""} WHERE server = ?`, server));
19286
+ done.push(server);
19287
+ }
19288
+ return { servers: done };
19289
+ }),
19290
+ handler(remoteStatusMethod, () => ({ links: status() }))
19291
+ ];
19292
+ },
19293
+ start() {
19294
+ const rows = ledger.read((db) => db.all("SELECT server FROM remote_link WHERE state = 'signed-in'"));
19295
+ for (const row of rows) if (readCredentials(row.server)) connect(row.server);
19296
+ },
19297
+ async stop() {
19298
+ stopped = true;
19299
+ for (const connection of connections.values()) {
19300
+ if (connection.refreshTimer) clearTimeout(connection.refreshTimer);
19301
+ if (connection.pushTimer) clearTimeout(connection.pushTimer);
19302
+ await connection.binding.stop();
19303
+ }
19304
+ connections.clear();
19305
+ },
19306
+ resolveServer,
19307
+ async host(artifactId, server) {
19308
+ const latest = ledger.write((db) => {
19309
+ if (linkRow(db, server)?.state !== "signed-in") throw loginFailed(`This home isn't signed in to ${server}: run mostra login ${server} first.`);
19310
+ const artifact = db.get("SELECT latest_version_id FROM artifact WHERE id = ?", artifactId);
19311
+ if (!artifact?.latest_version_id) throw invalid(`${artifactId} has no published version to share.`);
19312
+ db.run(`INSERT INTO hosted_artifact (artifact_id, server, hosted_home, route_id, label, shared_at) VALUES (?, ?, '', '', '', ?)
19313
+ ON CONFLICT (artifact_id, server) DO UPDATE SET stopped_at = NULL`, artifactId, server, new Date(now()).toISOString());
19314
+ queuePush(db, artifactId, artifact.latest_version_id, server);
19315
+ return artifact.latest_version_id;
19316
+ });
19317
+ const connection = connections.get(server) ?? connect(server);
19318
+ let pushed;
16358
19319
  try {
16359
- const { client, result } = await attach(record.socket, options.client, 2e3);
16360
- log.info("home.attached", {
16361
- home,
16362
- owner: record.pid,
16363
- socket: record.socket
16364
- });
16365
- return {
16366
- kind: "attached",
16367
- record,
16368
- connection: client,
16369
- result
16370
- };
19320
+ pushed = await pushVersion(connection, artifactId, latest);
16371
19321
  } catch (error) {
16372
- if (!(error instanceof RpcError) || error.diagnostic.code !== "MOSTRA_HOST_CONNECTION_CLOSED") throw error;
19322
+ if (refusedForGood(error)) refusePush(connection, artifactId, latest, error);
19323
+ throw error;
16373
19324
  }
16374
- }
16375
- if (Date.now() >= deadline) {
16376
- const owner = seen ?? readInstanceRecord(home);
16377
- throw locked(owner ? `The home ${home} is owned by PID ${owner.pid}, but its socket ${owner.socket} can't be reached.` : `Another process holds the ledger lock of the home ${home} and published no instance record.`);
16378
- }
16379
- await delay(100);
16380
- }
16381
- }
16382
- function identity(path) {
16383
- try {
16384
- const stats = statSync(path);
16385
- return `${stats.dev}:${stats.ino}`;
16386
- } catch {
16387
- return;
16388
- }
16389
- }
16390
- function own(inputs) {
16391
- const { home, socketPath, runtimeDir, ledger, log, redactor, self } = inputs;
16392
- const startedAt = (/* @__PURE__ */ new Date()).toISOString();
16393
- const homeId = identity(home);
16394
- const ledgerId = identity(join(home, "store", LEDGER_FILE));
16395
- let timer;
16396
- let released = false;
16397
- return {
16398
- path: home,
16399
- socketPath,
16400
- runtimeDir,
16401
- ledger,
16402
- log,
16403
- redactor,
16404
- self,
16405
- startedAt,
16406
- publish(mode, socket) {
16407
- writeInstanceRecord(home, {
16408
- pid: self.pid,
16409
- startTime: self.startTime,
16410
- socket,
16411
- runtimeVersion: inputs.options.runtimeVersion,
16412
- revisions: [...HOST_PROTOCOL_REVISIONS],
16413
- mode,
16414
- startedAt
19325
+ drainPushes(connection);
19326
+ return pushed;
19327
+ },
19328
+ published(artifactId, versionId) {
19329
+ const servers = ledger.write((db) => {
19330
+ const rows = db.all("SELECT server FROM hosted_artifact WHERE artifact_id = ? AND stopped_at IS NULL", artifactId);
19331
+ for (const row of rows) queuePush(db, artifactId, versionId, row.server);
19332
+ return rows.map((row) => row.server);
16415
19333
  });
19334
+ for (const server of servers) {
19335
+ const connection = connections.get(server);
19336
+ if (connection) drainPushes(connection);
19337
+ }
16416
19338
  },
16417
- watch(onGone, intervalMs = DEFAULT_WATCH_MS) {
16418
- if (timer) clearInterval(timer);
16419
- timer = setInterval(() => {
16420
- const homeNow = identity(home);
16421
- const ledgerNow = identity(join(home, "store", LEDGER_FILE));
16422
- if (homeNow === homeId && ledgerNow === ledgerId) return;
16423
- if (timer) clearInterval(timer);
16424
- onGone(createDiagnostic("MOSTRA_HOME_GONE", { message: homeNow === homeId ? `The ledger of the home ${home} was removed or replaced while the instance was running.` : `The home ${home} was removed or replaced while the instance was running.` }));
16425
- }, Math.min(intervalMs, 1e4));
16426
- timer.unref();
19339
+ async session(server, timeoutMs = 1e4) {
19340
+ const connection = connections.get(server);
19341
+ if (!connection || connection.state === "signed-out") throw loginFailed(`This home isn't signed in to ${server}.`);
19342
+ if (connection.binding.session) return connection.binding.session;
19343
+ let timer;
19344
+ try {
19345
+ return await Promise.race([connection.binding.ready(), new Promise((_resolve, reject) => {
19346
+ timer = setTimeout(() => reject(new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: `${server} can't be reached now${connection.lastError ? `: ${connection.lastError}` : ""}.` }))), timeoutMs);
19347
+ })]);
19348
+ } finally {
19349
+ if (timer) clearTimeout(timer);
19350
+ }
16427
19351
  },
16428
- release() {
16429
- if (released) return;
16430
- released = true;
16431
- if (timer) clearInterval(timer);
19352
+ connected(server) {
19353
+ return connections.get(server)?.binding.session !== void 0;
19354
+ },
19355
+ status: () => status(),
19356
+ liveInterest: () => relay.liveInterest(),
19357
+ forwardLive: (artifactId, versionId, produced) => {
19358
+ const servers = ledger.read((db) => db.all("SELECT server FROM hosted_artifact WHERE artifact_id = ? AND stopped_at IS NULL AND route_id != '' ORDER BY shared_at", artifactId).map((row) => row.server));
19359
+ if (servers.length === 0) return false;
19360
+ const value = produced();
19361
+ for (const server of servers) {
19362
+ const connection = connections.get(server);
19363
+ relay.sendLive(server, () => connection ? connection.binding.session ?? readySoon(connection) : void 0, artifactId, versionId, value);
19364
+ }
19365
+ return true;
19366
+ },
19367
+ hostedOn: (artifactId) => ledger.read((db) => db.get("SELECT server FROM hosted_artifact WHERE artifact_id = ? AND stopped_at IS NULL AND route_id != '' ORDER BY shared_at LIMIT 1", artifactId)?.server),
19368
+ forward: async (server, definition, params) => {
19369
+ let session;
16432
19370
  try {
16433
- removeInstanceRecord(home, self.pid, self.startTime);
19371
+ session = await self.session(server, 3e3);
16434
19372
  } catch (error) {
16435
- log.warn("home.record_remove_failed", { error });
19373
+ if (error instanceof RpcError) throw error;
19374
+ throw new RpcError(createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", { message: `${server} can't be reached now; the hosted artifact's state lives there, so nothing was written.` }));
16436
19375
  }
16437
- ledger.close();
16438
- log.info("home.released", { home });
16439
- }
16440
- };
16441
- }
16442
- //#endregion
16443
- //#region src/home/platform.ts
16444
- /**
16445
- * The unsupported-platform gate (D35, D146, RT-07). Every entry point calls it before it touches
16446
- * the home: Mostra runs on macOS, Linux and Windows, on x64 and arm64, and fails closed elsewhere.
16447
- * Windows was built behind a development switch by WP-W1 to WP-W3, and is supported since WP-W3
16448
- * like the others.
16449
- */
16450
- const OPERATING_SYSTEMS = [
16451
- "darwin",
16452
- "linux",
16453
- "win32"
16454
- ];
16455
- const ARCHITECTURES = ["x64", "arm64"];
16456
- /** The platform as `<os>-<arch>`, as `InstanceInfo.platform` reports it. */
16457
- function platformName(platform = OS, arch = process.arch) {
16458
- return `${platform}-${arch}`;
16459
- }
16460
- /**
16461
- * Returns `MOSTRA_PLATFORM_UNSUPPORTED` for a platform Mostra doesn't run on, or `undefined` for
16462
- * a supported one.
16463
- */
16464
- function checkPlatform(platform = OS, arch = process.arch) {
16465
- if (ARCHITECTURES.includes(arch) && OPERATING_SYSTEMS.includes(platform)) return void 0;
16466
- const name = platform === "win32" ? `Windows (${platformName(platform, arch)})` : platformName(platform, arch);
16467
- return createDiagnostic("MOSTRA_PLATFORM_UNSUPPORTED", { message: `Mostra doesn't run on ${name}; it supports macOS, Linux and Windows on x64 and arm64.` });
16468
- }
16469
- //#endregion
16470
- //#region src/home/settings.ts
16471
- /**
16472
- * The home's settings (`config.json`, DATA_MODEL §1): what hosts set through `HomeConfig` and
16473
- * what every later client of the home then shares: the dependency policy (D43, D95), the home's
16474
- * submission policy (D102), the ports each host always provides (D105) and the submit channels the
16475
- * home accepts (D108).
16476
- * Written atomically (a temporary file, then a rename); read fresh by whoever needs a setting, so
16477
- * a host's change applies to the next request.
16478
- */
16479
- const Settings = z.looseObject({
16480
- dependencies: z.enum(["auto", "explicit"]).optional(),
16481
- submission: SubmissionPolicy.optional(),
16482
- expectedPorts: z.record(z.string(), z.array(z.string())).optional(),
16483
- submitChannels: z.array(z.enum(["shell", "artifact"])).min(1).max(2).optional(),
16484
- hostConfigured: z.object({
16485
- client: z.string(),
16486
- at: z.string()
16487
- }).optional()
16488
- });
16489
- /** The settings file of a home. */
16490
- const SETTINGS_FILE = "config.json";
16491
- function unreadable(home, reason) {
16492
- return new RpcError(createDiagnostic("MOSTRA_HOME_INTEGRITY", {
16493
- message: `The home's ${join(home, SETTINGS_FILE)} can't be read: ${reason}.`,
16494
- hint: `Repair or remove ${SETTINGS_FILE} in the home; a host sets its settings again when it connects.`
16495
- }));
16496
- }
16497
- /**
16498
- * The home's settings; none when `config.json` doesn't exist. A file that isn't valid settings
16499
- * fails closed: guessing the policy could install packages a host forbade.
16500
- */
16501
- function readHomeSettings(home) {
16502
- let text;
16503
- try {
16504
- text = readFileSync(join(home, SETTINGS_FILE), "utf8");
16505
- } catch (error) {
16506
- if (error.code === "ENOENT") return {};
16507
- throw unreadable(home, error.code ?? "error");
16508
- }
16509
- let data;
16510
- try {
16511
- data = JSON.parse(text);
16512
- } catch {
16513
- throw unreadable(home, "it isn't JSON");
16514
- }
16515
- const parsed = Settings.safeParse(data);
16516
- if (!parsed.success) throw unreadable(home, "its settings are invalid");
16517
- return parsed.data;
16518
- }
16519
- /** Merges `change` into the home's settings, durably. */
16520
- function writeHomeSettings(home, change) {
16521
- const next = {
16522
- ...readHomeSettings(home),
16523
- ...change
19376
+ return session.call(definition, params, { timeoutMs: 3e4 });
19377
+ },
19378
+ shares: () => ({
19379
+ mint: async (input) => {
19380
+ const server = resolveServer(input.server);
19381
+ await self.host(input.artifactId, server);
19382
+ const minted = await (await self.session(server)).call(viewUrlMethod, {
19383
+ artifact: { artifactId: input.artifactId },
19384
+ ...input.scopes !== void 0 ? { scopes: input.scopes } : {},
19385
+ ...input.ttl !== void 0 ? { ttl: input.ttl } : {},
19386
+ ...input.viewer !== void 0 ? { viewer: input.viewer } : {}
19387
+ });
19388
+ protectLink(minted.url);
19389
+ writeLinks(server, {
19390
+ ...readLinks(server),
19391
+ [minted.tokenId]: minted.url
19392
+ });
19393
+ return {
19394
+ server,
19395
+ url: minted.url,
19396
+ tokenId: minted.tokenId,
19397
+ hostname: new URL(minted.url).host,
19398
+ ...minted.expiresAt !== void 0 ? { expiresAt: minted.expiresAt } : {}
19399
+ };
19400
+ },
19401
+ url: (server, tokenId) => readLinks(server)[tokenId],
19402
+ revoke: async (server, tokenId) => {
19403
+ await (await self.session(server)).call(viewRevokeMethod, { tokenId });
19404
+ const links = readLinks(server);
19405
+ delete links[tokenId];
19406
+ writeLinks(server, links);
19407
+ }
19408
+ })
16524
19409
  };
16525
- const file = join(home, SETTINGS_FILE);
16526
- const temporary = `${file}.${process.pid}.tmp`;
16527
- writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { mode: 384 });
16528
- fsyncPath(temporary);
16529
- renamePathSync(temporary, file);
16530
- fsyncPath(home, true);
16531
- }
16532
- /**
16533
- * Marks the home as configured by a host (D140 item 4), once: the first host's client name and
16534
- * when. A home already marked keeps its mark.
16535
- */
16536
- function markHostConfigured(home, client, at = /* @__PURE__ */ new Date()) {
16537
- if (readHomeSettings(home).hostConfigured !== void 0) return false;
16538
- writeHomeSettings(home, { hostConfigured: {
16539
- client,
16540
- at: at.toISOString()
16541
- } });
16542
- return true;
19410
+ return self;
16543
19411
  }
16544
19412
  //#endregion
16545
19413
  //#region src/secrets/index.ts
@@ -17652,8 +20520,9 @@ function createShares(options) {
17652
20520
  const iso = (ms) => new Date(ms).toISOString();
17653
20521
  const row = (shareId) => ledger.read((db) => db.get("SELECT * FROM share WHERE id = ?", shareId));
17654
20522
  const toShare = (found) => {
17655
- const tokenId = capabilities.current(found.token_id);
17656
- const url = found.stopped_at === null ? gateway.reveal(tokenId) : void 0;
20523
+ const server = JSON.parse(found.plan_json).server;
20524
+ const tokenId = server === void 0 ? capabilities.current(found.token_id) : found.token_id;
20525
+ const url = found.stopped_at !== null ? void 0 : server === void 0 ? gateway.reveal(tokenId) : options.remote?.url(server, tokenId);
17657
20526
  const artifactKey = JSON.parse(found.plan_json).faces.find((face) => face.listener === "artifact")?.key;
17658
20527
  const artifactOrigin = found.stopped_at === null && artifactKey !== void 0 ? gateway.face(artifactKey)?.origin() : void 0;
17659
20528
  return {
@@ -17909,6 +20778,47 @@ function createShares(options) {
17909
20778
  budgetMs: budget
17910
20779
  });
17911
20780
  };
20781
+ /** A share to a remote server: hosted there, its link minted there (INTERFACES §18.2). */
20782
+ const startRemote = async (params, detail, key) => {
20783
+ const remote = options.remote;
20784
+ if (!remote) throw new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message: "This instance shares to no remote server." }), -32602);
20785
+ const minted = await remote.mint({
20786
+ artifactId: detail.id,
20787
+ server: params.server,
20788
+ scopes: params.scopes,
20789
+ ttl: params.ttl,
20790
+ viewer: params.viewer
20791
+ });
20792
+ const shareId = ShareId.create();
20793
+ const startedAt = options.now();
20794
+ ledger.write((db) => {
20795
+ db.run("INSERT INTO share (id, artifact_id, via, hostname, token_id, mode, plan_json, started_at, expires_at, stopped_at, stop_reason) VALUES (?, ?, 'remote', ?, ?, 'worker', ?, ?, ?, NULL, NULL)", shareId, detail.id, minted.hostname, minted.tokenId, JSON.stringify({
20796
+ faces: [],
20797
+ server: minted.server
20798
+ }), iso(startedAt), minted.expiresAt ?? null);
20799
+ appendEvent(db, {
20800
+ artifactId: detail.id,
20801
+ type: "share.started",
20802
+ payload: {
20803
+ shareId,
20804
+ via: "remote",
20805
+ hostname: minted.hostname,
20806
+ server: minted.server
20807
+ }
20808
+ });
20809
+ if (key !== void 0) recordResult(db, key, { shareId });
20810
+ });
20811
+ schedule(shareId, minted.expiresAt);
20812
+ log.info("share.started", {
20813
+ shareId,
20814
+ artifactId: detail.id,
20815
+ via: "remote"
20816
+ });
20817
+ return {
20818
+ ...toShare(row(shareId)),
20819
+ url: minted.url
20820
+ };
20821
+ };
17912
20822
  const start = async (params) => {
17913
20823
  const detail = options.artifacts.get(params.artifact);
17914
20824
  if (!detail.latestVersion && detail.versions.length === 0) throw new RpcError(createDiagnostic("MOSTRA_ARTIFACT_VERSION_NOT_FOUND", { message: `The artifact ${detail.id} has no published version to share.` }));
@@ -17918,6 +20828,7 @@ function createShares(options) {
17918
20828
  const found = replay ? row(replay.shareId) : void 0;
17919
20829
  if (found) return withUrl(toShare(found));
17920
20830
  }
20831
+ if (params.via === "remote") return startRemote(params, detail, key);
17921
20832
  const ttlMs = lifetime(params.via, params.ttl);
17922
20833
  const shareId = ShareId.create();
17923
20834
  const { shell, artifact, plan, hostname } = await setUp(shareId, detail.id, params.via, void 0);
@@ -17991,10 +20902,12 @@ function createShares(options) {
17991
20902
  const stop = async (shareId, reason) => {
17992
20903
  const found = row(shareId);
17993
20904
  if (!found || found.stopped_at !== null) return void 0;
20905
+ const server = JSON.parse(found.plan_json).server;
20906
+ if (server !== void 0 && reason !== "expired" && reason !== "revoked") await options.remote?.revoke(server, found.token_id);
17994
20907
  const at = options.now();
17995
20908
  ledger.write((db) => {
17996
20909
  db.run("UPDATE share SET stopped_at = ?, stop_reason = ? WHERE id = ?", iso(at), reason, shareId);
17997
- if (reason !== "expired" && reason !== "revoked") capabilities.revoke(capabilities.current(found.token_id), db);
20910
+ if (server === void 0 && reason !== "expired" && reason !== "revoked") capabilities.revoke(capabilities.current(found.token_id), db);
17998
20911
  appendEvent(db, {
17999
20912
  artifactId: found.artifact_id,
18000
20913
  type: "share.stopped",
@@ -18021,8 +20934,12 @@ function createShares(options) {
18021
20934
  /** Ends shares whose link expired or was revoked since (`view/revoke` of a share's link). */
18022
20935
  const sweep = async () => {
18023
20936
  for (const found of activeRows()) {
18024
- if (capabilities.grant(capabilities.current(found.token_id))?.live) continue;
18025
20937
  const expired = found.expires_at !== null && Date.parse(found.expires_at) <= options.now();
20938
+ if (found.via === "remote") {
20939
+ if (expired) await serial(() => stop(found.id, "expired"));
20940
+ continue;
20941
+ }
20942
+ if (capabilities.grant(capabilities.current(found.token_id))?.live) continue;
18026
20943
  await serial(() => stop(found.id, expired ? "expired" : "revoked"));
18027
20944
  }
18028
20945
  };
@@ -18100,8 +21017,13 @@ function createShares(options) {
18100
21017
  async recover() {
18101
21018
  const pending = [];
18102
21019
  for (const found of activeRows()) {
18103
- const grant = capabilities.grant(capabilities.current(found.token_id));
18104
21020
  const expired = found.expires_at !== null && Date.parse(found.expires_at) <= options.now();
21021
+ if (found.via === "remote") {
21022
+ if (expired) await serial(() => stop(found.id, "expired"));
21023
+ else schedule(found.id, found.expires_at ?? void 0);
21024
+ continue;
21025
+ }
21026
+ const grant = capabilities.grant(capabilities.current(found.token_id));
18105
21027
  if (expired || !grant?.live) await serial(() => stop(found.id, expired ? "expired" : "revoked"));
18106
21028
  else if (found.via === "cloudflare") await serial(() => stop(found.id, "restart"));
18107
21029
  else pending.push(restore(found));
@@ -18486,6 +21408,7 @@ async function assemble(home, options) {
18486
21408
  ...Number(environment["MOSTRA_TEST_WORKER_BACKOFF_MS"]) > 0 ? { backoffMinMs: Number(environment["MOSTRA_TEST_WORKER_BACKOFF_MS"]) } : {}
18487
21409
  });
18488
21410
  const actions = createActions({
21411
+ hostedState: (artifactId) => hostedState(link, artifactId),
18489
21412
  interaction,
18490
21413
  pool,
18491
21414
  bus,
@@ -18518,6 +21441,8 @@ async function assemble(home, options) {
18518
21441
  } : void 0;
18519
21442
  };
18520
21443
  const live = createLive({
21444
+ extraInterest: () => link.liveInterest(),
21445
+ forward: (artifactId, versionId, produced) => link.forwardLive(artifactId, versionId, produced),
18521
21446
  ledger: home.ledger,
18522
21447
  log,
18523
21448
  bus,
@@ -18621,6 +21546,20 @@ async function assemble(home, options) {
18621
21546
  }
18622
21547
  }
18623
21548
  });
21549
+ const link = createLink({
21550
+ home: home.path,
21551
+ ledger: home.ledger,
21552
+ log,
21553
+ env: environment,
21554
+ runtimeVersion: runtimeVersion(),
21555
+ protect: (value) => home.redactor.protect(value),
21556
+ importSubmission: (envelope, source) => interaction.importSubmission(envelope, source),
21557
+ versionDir: (versionId) => registry.store.versionDir(versionId),
21558
+ actions,
21559
+ liveChanged: () => live.reconcile(),
21560
+ now: () => Date.now() + clockOffset
21561
+ });
21562
+ const stopPushingPublishes = interaction.onPublished((artifactId, versionId) => link.published(artifactId, versionId));
18624
21563
  const shares = createShares({
18625
21564
  ledger: home.ledger,
18626
21565
  log,
@@ -18633,6 +21572,7 @@ async function assemble(home, options) {
18633
21572
  toolEnvironment: artifactEnvironment(environment),
18634
21573
  cacheDir: shareCacheDir(home.path),
18635
21574
  now: () => Date.now() + clockOffset,
21575
+ remote: link.shares(),
18636
21576
  ...environment["MOSTRA_TEST_SHARE_PROBE_MS"] !== void 0 ? { probeMs: Number(environment["MOSTRA_TEST_SHARE_PROBE_MS"]) } : {},
18637
21577
  ...environment["MOSTRA_TEST_SHARE_SWEEP_MS"] !== void 0 ? { sweepMs: Number(environment["MOSTRA_TEST_SHARE_SWEEP_MS"]) } : {}
18638
21578
  });
@@ -18717,7 +21657,8 @@ async function assemble(home, options) {
18717
21657
  ...dev.methods(),
18718
21658
  ...gateway.methods(),
18719
21659
  ...shares.methods(),
18720
- ...interaction.methods(),
21660
+ ...link.methods(),
21661
+ ...forwardHosted(link, (ref) => registry.get(canonicalRef(ref)).id, interaction.methods()),
18721
21662
  ...secretMethods(secrets, (ref) => registry.get(canonicalRef(ref)).id),
18722
21663
  ...gcMethods(createGc({
18723
21664
  ledger: home.ledger,
@@ -18730,7 +21671,7 @@ async function assemble(home, options) {
18730
21671
  locksInUse: () => dev.locks(),
18731
21672
  faults
18732
21673
  })),
18733
- ...liveMethods(live, (ref) => registry.get(canonicalRef(ref)).id),
21674
+ ...forwardHosted(link, (ref) => registry.get(canonicalRef(ref)).id, liveMethods(live, (ref) => registry.get(canonicalRef(ref)).id)),
18734
21675
  ...sessionMethods({
18735
21676
  describe: () => info,
18736
21677
  startedAt: home.startedAt,
@@ -18747,6 +21688,7 @@ async function assemble(home, options) {
18747
21688
  faces: [...gateway.faces()]
18748
21689
  } : void 0,
18749
21690
  shares: () => shares.active(),
21691
+ links: () => link.status(),
18750
21692
  deliveries: () => interaction.deliveries(),
18751
21693
  workers: () => pool.status(),
18752
21694
  idleExitAt: () => idle?.exitAt(),
@@ -18778,6 +21720,8 @@ async function assemble(home, options) {
18778
21720
  });
18779
21721
  idle?.stop();
18780
21722
  stopWatchingPublishes();
21723
+ stopPushingPublishes();
21724
+ await link.stop();
18781
21725
  live.stop();
18782
21726
  interaction.stop();
18783
21727
  await server.close();
@@ -18840,6 +21784,7 @@ async function assemble(home, options) {
18840
21784
  });
18841
21785
  interaction.start();
18842
21786
  live.start();
21787
+ link.start();
18843
21788
  shares.recover().catch((error) => log.error("share.recover_failed", { error }));
18844
21789
  home.publish(options.mode, server.socketPath ?? home.socketPath);
18845
21790
  if (options.mode === "host" && markHostConfigured(home.path, "mostra-runtime host")) log.info("home.host_configured", { client: "mostra-runtime host" });
@@ -18937,6 +21882,6 @@ function sourceDirectory(path) {
18937
21882
  }
18938
21883
  }
18939
21884
  //#endregion
18940
- export { uninstallService as a, serviceStatus as i, startInstance as n, checkPlatform as o, installService as r, resolveHomePath as s, runtimeVersion as t };
21885
+ export { MAX_FRAME_BYTES as A, coverageMatcher as B, finishRun as C, runState as D, markWaiting as E, openLedger as F, noFaults as I, readFieldCoverage as L, ProtocolServer as M, handler as N, appendEvent as O, holdLock as P, readServerFunctions as R, appendRunOutput as S, markUnknown as T, openFileLog as V, Capabilities as _, uninstallService as a, createInteraction as b, resolveHomePath as c, versionIdFor as d, createLive as f, shellAssetResponse as g, locateShell as h, serviceStatus as i, serveWebSocket as j, BINDING_SUBPROTOCOL as k, canonicalRef as l, createGateway as m, startInstance as n, checkPlatform as o, liveMethods as p, installService as r, platformName as s, runtimeVersion as t, listingDigest as u, capabilityKey as v, markRunning as w, createEphemeralBus as x, openRegistry as y, readProcess as z };
18941
21886
 
18942
- //# sourceMappingURL=instance-EnaZPs14.js.map
21887
+ //# sourceMappingURL=instance-FExcyENK.js.map