@mostra/runtime 0.11.0 → 0.13.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 (50) hide show
  1. package/dist/bin.js +930 -458
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/{instance-DCiUHvgZ.js → instance-D5gh-sMM.js} +1231 -46
  5. package/dist/instance-D5gh-sMM.js.map +1 -0
  6. package/dist/reference/cli.md +112 -2
  7. package/dist/reference/diagnostics.json +29 -2
  8. package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
  9. package/dist/reference/errors/MOSTRA_TEAM_PRESENCE_RATE_LIMITED.md +23 -0
  10. package/dist/reference/errors/MOSTRA_TEAM_ROSTER_POLICY.md +23 -0
  11. package/dist/reference/errors/MOSTRA_TEAM_ROSTER_TOO_LARGE.md +23 -0
  12. package/dist/reference/self-hosting.md +1 -1
  13. package/dist/skill/SKILL.md +5 -3
  14. package/dist/skill/references/answers.md +3 -1
  15. package/dist/skill/references/mcp.md +2 -2
  16. package/dist/skill/references/teams.md +78 -0
  17. package/package.json +6 -6
  18. package/src/actions/index.ts +5 -2
  19. package/src/gateway/actions.ts +6 -1
  20. package/src/gateway/browser.ts +84 -1
  21. package/src/gateway/capabilities.ts +5 -1
  22. package/src/gateway/index.ts +43 -1
  23. package/src/gateway/team.ts +181 -0
  24. package/src/instance/index.ts +2 -1
  25. package/src/interaction/ephemeral.ts +21 -0
  26. package/src/interaction/events.ts +17 -0
  27. package/src/interaction/index.ts +265 -20
  28. package/src/interaction/roster.ts +585 -0
  29. package/src/interaction/team.ts +426 -0
  30. package/src/interaction/wait.ts +150 -0
  31. package/src/ledger/migrations.ts +44 -0
  32. package/src/link/hosted.ts +14 -3
  33. package/src/link/manager.ts +31 -1
  34. package/src/server/api.ts +1 -1
  35. package/src/server/auth.ts +83 -16
  36. package/src/server/authorize.ts +27 -4
  37. package/src/server/config.ts +53 -0
  38. package/src/server/directory.ts +73 -0
  39. package/src/server/feed.ts +89 -3
  40. package/src/server/hosted-pages.ts +19 -6
  41. package/src/server/hosted.ts +108 -49
  42. package/src/server/index.ts +72 -15
  43. package/src/server/metering.ts +117 -12
  44. package/src/server/operator.ts +87 -28
  45. package/src/server/providers.ts +61 -9
  46. package/src/server/relay.ts +258 -107
  47. package/src/share/index.ts +50 -2
  48. package/src/verify/index.ts +7 -0
  49. package/src/verify/runner.ts +5 -1
  50. package/dist/instance-DCiUHvgZ.js.map +0 -1
package/dist/bin.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { A as markRunning, B as handler, C as jsonResponse, D as createEphemeralBus, E as createInteraction, F as BINDING_SUBPROTOCOL, G as readFieldCoverage, H as openLedger, I as MAX_FRAME_BYTES, J as coverageMatcher, K as readServerFunctions, L as serveWebSocket, M as markWaiting, N as runState, O as appendRunOutput, P as appendEvent, R as attachInProcess, S as isRefusal, T as openRegistry, U as faultsFrom, V as holdLock, W as noFaults, Y as openFileLog, _ as resetOnClose, a as uninstallService, b as BASE_HEADERS$1, c as resolveHomePath, d as versionIdFor, f as createLive, g as shellAssetResponse, h as locateShell, i as serviceStatus, j as markUnknown, k as finishRun, l as canonicalRef, m as createGateway, n as startInstance, o as checkPlatform, p as liveMethods, q as readProcess, r as installService, s as platformName, t as runtimeVersion, u as listingDigest, v as Capabilities, w as readJsonObject, x as documentHeaders, y as capabilityKey, z as ProtocolServer } from "./instance-DCiUHvgZ.js";
2
+ import { A as markRunning, B as handler, C as jsonResponse, D as createEphemeralBus, E as createInteraction, F as BINDING_SUBPROTOCOL, G as readFieldCoverage, H as openLedger, I as MAX_FRAME_BYTES, J as coverageMatcher, K as readServerFunctions, L as serveWebSocket, M as markWaiting, N as runState, O as appendRunOutput, P as appendEvent, R as attachInProcess, S as isRefusal, T as openRegistry, U as faultsFrom, V as holdLock, W as noFaults, Y as openFileLog, _ as resetOnClose, a as uninstallService, b as BASE_HEADERS$1, c as resolveHomePath, d as versionIdFor, f as createLive, g as shellAssetResponse, h as locateShell, i as serviceStatus, j as markUnknown, k as finishRun, l as canonicalRef, m as createGateway, n as startInstance, o as checkPlatform, p as liveMethods, q as readProcess, r as installService, s as platformName, t as runtimeVersion, u as listingDigest, v as Capabilities, w as readJsonObject, x as documentHeaders, y as capabilityKey, z as ProtocolServer } from "./instance-D5gh-sMM.js";
3
3
  import { c as removeTree, s as realPath } from "./files-DGeWuE4I.js";
4
4
  import { d as runtimeRoot, i as ownedDirectory } from "./directories-BLJjeSPD.js";
5
- import { createWriteStream, existsSync, linkSync, mkdirSync, readFileSync, renameSync, statfsSync, writeFileSync } from "node:fs";
5
+ import { createWriteStream, existsSync, linkSync, mkdirSync, readFileSync, renameSync, statSync, statfsSync, writeFileSync } from "node:fs";
6
6
  import { isAbsolute, join, parse, relative } from "node:path";
7
7
  import { fileURLToPath, pathToFileURL } from "node:url";
8
8
  import { ACTION_ARGS_MAX, AccountId, ArtifactManifest, Duration, HOSTED_OFFLINE_LIMIT_DEFAULT, HomeId, LineSplitter, LinkId, RouteId, RpcError, RpcPeer, TALLY_LIVE_PREFIX, artifactGetMethod, artifactListMethod, artifactPauseMethod, checkArgumentFrames, createDiagnostic, durationMs, encodeLine, hostedPushMethod, hostedSubmissionMethod, isFinalRunStatus, portRelayAuthorMethod, portRelayCallMethod, portRelayCancelMethod, portRelayLiveMethod, portRelayReconcileMethod, portRelayRunMethod, refusal, relayCallBatchMethod, relayEffectMethod, relayLiveValueMethod, relayRunUpdateMethod, remoteTokenMethod, viewDefaultMethod, viewUrlMethod } from "@mostra/protocol";
@@ -386,7 +386,7 @@ function createApi(options) {
386
386
  }
387
387
  if (existsSync(target)) await rm(scratch, { force: true });
388
388
  else renameSync(scratch, target);
389
- home.uploaded(sha);
389
+ home.uploaded(sha, size);
390
390
  options.stored?.(who.homeId, size);
391
391
  send(response, 201, {
392
392
  sha256: sha,
@@ -789,6 +789,343 @@ function createApi(options) {
789
789
  };
790
790
  }
791
791
  //#endregion
792
+ //#region src/server/clients.ts
793
+ /** An address as a number (IPv4 in 32 bits, IPv6 in 128), or `undefined` when it isn't one. */
794
+ function addressBits(address) {
795
+ let text = address.trim();
796
+ if (text.startsWith("[") && text.endsWith("]")) text = text.slice(1, -1);
797
+ const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(text);
798
+ if (mapped) text = mapped[1];
799
+ if (isIPv4(text)) return {
800
+ v6: false,
801
+ bits: text.split(".").map(Number).reduce((sum, part) => sum << 8n | BigInt(part), 0n)
802
+ };
803
+ if (!isIPv6(text)) return void 0;
804
+ const zone = text.indexOf("%");
805
+ if (zone !== -1) text = text.slice(0, zone);
806
+ const last = text.slice(text.lastIndexOf(":") + 1);
807
+ if (last.includes(".")) {
808
+ if (!isIPv4(last)) return void 0;
809
+ const parts = last.split(".").map(Number);
810
+ const high = (parts[0] << 8 | parts[1]).toString(16);
811
+ const low = (parts[2] << 8 | parts[3]).toString(16);
812
+ text = `${text.slice(0, -last.length)}${high}:${low}`;
813
+ }
814
+ const [head, tail] = text.split("::");
815
+ const left = head === "" ? [] : head.split(":");
816
+ const right = tail === void 0 ? [] : tail === "" ? [] : tail.split(":");
817
+ const groups = tail === void 0 ? left : [
818
+ ...left,
819
+ ...new Array(8 - left.length - right.length).fill("0"),
820
+ ...right
821
+ ];
822
+ if (groups.length !== 8) return void 0;
823
+ return {
824
+ v6: true,
825
+ bits: groups.reduce((sum, group) => sum << 16n | BigInt(Number.parseInt(group, 16)), 0n)
826
+ };
827
+ }
828
+ function parseRange(text) {
829
+ const [address, prefixText] = text.split("/");
830
+ const found = addressBits(address);
831
+ if (!found) return void 0;
832
+ const width = found.v6 ? 128 : 32;
833
+ const prefix = prefixText === void 0 ? width : Number(prefixText);
834
+ if (!Number.isInteger(prefix) || prefix < 0 || prefix > width) return void 0;
835
+ return {
836
+ v6: found.v6,
837
+ bits: found.bits,
838
+ prefix
839
+ };
840
+ }
841
+ function inRange(range, address) {
842
+ if (range.v6 !== address.v6) return false;
843
+ const shift = (range.v6 ? 128n : 32n) - BigInt(range.prefix);
844
+ return range.bits >> shift === address.bits >> shift;
845
+ }
846
+ /** Whether every entry of `trustedProxies` is an address or a CIDR block. */
847
+ function validProxies(entries) {
848
+ return entries.find((entry) => parseRange(entry) === void 0);
849
+ }
850
+ /** Builds the resolver; an entry that isn't an address or a block is ignored (checked at start). */
851
+ function clientAddresses(options) {
852
+ const proxies = (options.trustedProxies ?? []).map(parseRange).filter((range) => range !== void 0);
853
+ const trusted = (address) => {
854
+ const bits = addressBits(address);
855
+ return bits !== void 0 && proxies.some((range) => inRange(range, bits));
856
+ };
857
+ const ipv6Prefix = options.ipv6Prefix ?? 64;
858
+ const seen = /* @__PURE__ */ new WeakMap();
859
+ return {
860
+ of(incoming) {
861
+ if (seen.has(incoming)) return seen.get(incoming);
862
+ const peer = incoming.socket.remoteAddress;
863
+ let client = peer;
864
+ if (peer !== void 0 && trusted(peer)) {
865
+ const header = incoming.headers["x-forwarded-for"];
866
+ const entries = (Array.isArray(header) ? header.join(",") : header ?? "").split(",").map((entry) => entry.trim()).filter((entry) => entry !== "");
867
+ for (let index = entries.length - 1; index >= 0; index--) {
868
+ const entry = entries[index];
869
+ if (addressBits(entry) === void 0) break;
870
+ client = entry;
871
+ if (!trusted(entry)) break;
872
+ }
873
+ }
874
+ seen.set(incoming, client);
875
+ return client;
876
+ },
877
+ keyOf(address) {
878
+ if (address === void 0) return "unknown";
879
+ const bits = addressBits(address);
880
+ if (!bits) return `other:${address}`;
881
+ if (!bits.v6) return `v4:${bits.bits.toString(16)}`;
882
+ const shift = 128n - BigInt(ipv6Prefix);
883
+ return `v6:${(bits.bits >> shift).toString(16)}/${ipv6Prefix}`;
884
+ }
885
+ };
886
+ }
887
+ /**
888
+ * A token-bucket rate limiter over keys, in memory, bounded in the keys it keeps: each key holds up
889
+ * to `count` tokens and earns them back at `count` per `perMs`, so a burst of `count` passes and
890
+ * the sustained rate is exactly the rate.
891
+ */
892
+ var RateLimiter = class {
893
+ #buckets = /* @__PURE__ */ new Map();
894
+ #now;
895
+ #maxKeys;
896
+ constructor(options = {}) {
897
+ this.#now = options.now ?? (() => Date.now());
898
+ this.#maxKeys = options.maxKeys ?? 1e5;
899
+ }
900
+ /** A key's bucket, refilled to now and marked most recently used, so the bound drops the least. */
901
+ #bucket(key, rate, now) {
902
+ const perToken = rate.perMs / rate.count;
903
+ let bucket = this.#buckets.get(key);
904
+ if (bucket) {
905
+ bucket.tokens = Math.min(rate.count, bucket.tokens + (now - bucket.at) / perToken);
906
+ bucket.at = now;
907
+ this.#buckets.delete(key);
908
+ } else {
909
+ if (this.#buckets.size >= this.#maxKeys) this.#buckets.delete(this.#buckets.keys().next().value);
910
+ bucket = {
911
+ tokens: rate.count,
912
+ at: now
913
+ };
914
+ }
915
+ this.#buckets.set(key, bucket);
916
+ return bucket;
917
+ }
918
+ /** How long until every bucket holds a token: 0 when they all do now. */
919
+ #wait(wanted, now) {
920
+ let waitMs = 0;
921
+ const buckets = wanted.map(({ key, rate }) => {
922
+ const bucket = this.#bucket(key, rate, now);
923
+ if (bucket.tokens < 1) waitMs = Math.max(waitMs, (1 - bucket.tokens) * (rate.perMs / rate.count));
924
+ return bucket;
925
+ });
926
+ return {
927
+ waitMs,
928
+ buckets
929
+ };
930
+ }
931
+ /**
932
+ * Takes one token under `key` when there is one. Without one, nothing is taken and the answer
933
+ * says when to try again, in whole seconds (`Retry-After`).
934
+ */
935
+ take(key, rate) {
936
+ return this.takeAll([{
937
+ key,
938
+ rate
939
+ }]);
940
+ }
941
+ /**
942
+ * Gives back the token `takeAll` took under every key, as far as each bucket's size: an attempt
943
+ * someone refused after the rates admitted it (the operator's `limits`) costs nothing (second
944
+ * Fable review of WP-30, 2).
945
+ */
946
+ refundAll(wanted) {
947
+ const now = this.#now();
948
+ for (const { key, rate } of wanted) {
949
+ const bucket = this.#bucket(key, rate, now);
950
+ bucket.tokens = Math.min(rate.count, bucket.tokens + 1);
951
+ }
952
+ }
953
+ /**
954
+ * Takes one token under every key, or none: when any bucket is empty, nothing is taken from the
955
+ * others, and the answer says when the slowest one has a token again (Fable review, 8).
956
+ */
957
+ takeAll(wanted) {
958
+ const { waitMs, buckets } = this.#wait(wanted, this.#now());
959
+ if (waitMs > 0) return {
960
+ ok: false,
961
+ retryAfterS: Math.max(1, Math.ceil(waitMs / 1e3))
962
+ };
963
+ for (const bucket of buckets) bucket.tokens -= 1;
964
+ return { ok: true };
965
+ }
966
+ };
967
+ //#endregion
968
+ //#region src/server/config.ts
969
+ /**
970
+ * The remote server's configuration (INTERFACES §18.1): a JSON file (`--config`) and
971
+ * `MOSTRA_SERVER_<KEY>` variables, read before the server serves anything. A configuration it
972
+ * can't use stops it, naming the key (`MOSTRA_REMOTE_CONFIG_INVALID`). Domains are configuration,
973
+ * never code (D137, D165 item 0a): the dossier and this code name only `example` hosts.
974
+ */
975
+ /** A rate: `count` per `per` (a duration such as `1m`, as links' lifetimes are written). */
976
+ const RateSchema = z.object({
977
+ count: z.int().min(1),
978
+ per: Duration
979
+ });
980
+ /** A host name a link can use: lower-case labels of letters, digits and hyphens. */
981
+ const HostName = z.string().regex(/^(?=.{1,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)(\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*(:\d{1,5})?$/, { message: "must be a lower-case host name, with a port only for a local test" });
982
+ /**
983
+ * An origin the server is reached at: `https:`, or `http:` for loopback and `*.localhost` (tests
984
+ * and a self-hoster's first run).
985
+ */
986
+ const Origin = z.string().refine((value) => {
987
+ try {
988
+ const url = new URL(value);
989
+ if (url.origin !== value) return false;
990
+ if (url.protocol === "https:") return true;
991
+ return url.protocol === "http:" && (url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname.endsWith(".localhost") || url.hostname === "[::1]");
992
+ } catch {
993
+ return false;
994
+ }
995
+ }, { message: "must be an https: origin (http: only on loopback), with no path" });
996
+ /**
997
+ * The label a configured provider's verified names carry on a hosted page's sender line ("verified
998
+ * with GitHub"): its `label`, else GitHub's name or its ID; none for the development provider, which
999
+ * takes the typed name on trust (D161). Recorded on each identity the provider links (MT-03).
1000
+ */
1001
+ function configuredVerifier(provider) {
1002
+ if (provider.kind === "development") return void 0;
1003
+ return verifierLabel(provider.label ?? (provider.kind === "github" ? "GitHub" : provider.id));
1004
+ }
1005
+ /**
1006
+ * A verifier's label as the sender line may show it (MT-03, and its Opus review): text of 1 to 64
1007
+ * characters with no control, format (bidirectional overrides, zero-width joiners), line or
1008
+ * paragraph separator characters, which could make one label read as another's ("verified with
1009
+ * GitHub" from reversed text); anything else is no label.
1010
+ */
1011
+ function verifierLabel(label) {
1012
+ if (typeof label !== "string") return void 0;
1013
+ if (label.trim() === "" || label.length > 64) return void 0;
1014
+ if (/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(label)) return void 0;
1015
+ return label;
1016
+ }
1017
+ /** Claim names that would reach an object's prototype rather than a key of its own. */
1018
+ const RESERVED_CLAIMS = /* @__PURE__ */ new Set([
1019
+ "__proto__",
1020
+ "constructor",
1021
+ "prototype"
1022
+ ]);
1023
+ /** The schema of {@link RemoteServerConfig}. */
1024
+ const Config = z.strictObject({
1025
+ apiOrigin: Origin,
1026
+ domains: z.strictObject({
1027
+ active: HostName,
1028
+ spares: z.array(HostName).optional()
1029
+ }),
1030
+ /**
1031
+ * The scheme the user-content domain is served on: `https` behind a TLS terminator, `http`
1032
+ * for a loopback test. Default `https`.
1033
+ */
1034
+ scheme: z.enum(["https", "http"]).optional(),
1035
+ trustedProxies: z.array(z.string()).optional(),
1036
+ identityProviders: z.array(z.strictObject({
1037
+ id: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/),
1038
+ kind: z.enum([
1039
+ "development",
1040
+ "oidc",
1041
+ "github"
1042
+ ]),
1043
+ label: z.string().min(1).optional(),
1044
+ issuer: z.string().optional(),
1045
+ clientId: z.string().optional(),
1046
+ clientSecretFile: z.string().optional(),
1047
+ claims: z.array(z.string().regex(/^[A-Za-z_][A-Za-z0-9_.:-]{0,63}$/, { message: "must be claim names of at most 64 letters, digits and `_.:-`" }).refine((name) => !RESERVED_CLAIMS.has(name), { message: "can't name `__proto__`, `constructor` or `prototype`" })).min(1).max(8).optional()
1048
+ })).optional(),
1049
+ rateLimits: z.strictObject({
1050
+ perIp: z.strictObject({
1051
+ submissions: RateSchema.optional(),
1052
+ requests: RateSchema.optional()
1053
+ }).optional(),
1054
+ perLink: z.strictObject({
1055
+ submissions: RateSchema.optional(),
1056
+ relayedRuns: RateSchema.optional()
1057
+ }).optional(),
1058
+ ipv6Prefix: z.int().min(1).max(128).optional()
1059
+ }).optional(),
1060
+ ledger: z.strictObject({ walAutocheckpoint: z.int().min(0).optional() }).optional(),
1061
+ relay: z.strictObject({
1062
+ waitingRunsPerArtifact: z.int().min(1).optional(),
1063
+ drainPerSecond: z.number().positive().optional()
1064
+ }).optional(),
1065
+ homes: z.strictObject({ idleCloseMinutes: z.number().positive().optional() }).optional(),
1066
+ framing: z.strictObject({ frameAncestors: z.literal("'none'") }).optional(),
1067
+ operator: z.string().optional(),
1068
+ chatHosts: z.record(z.string(), z.unknown()).optional()
1069
+ });
1070
+ /** The configuration keys, and the variable each also comes from. */
1071
+ const KEYS = Object.keys(Config.shape);
1072
+ /** `apiOrigin` → `MOSTRA_SERVER_API_ORIGIN`. */
1073
+ function variableOf(key) {
1074
+ return `MOSTRA_SERVER_${key.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase()}`;
1075
+ }
1076
+ function invalid$1(message) {
1077
+ return new RpcError(createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", { message }));
1078
+ }
1079
+ /**
1080
+ * Reads the configuration: the file, then each `MOSTRA_SERVER_<KEY>` (a string for a string key,
1081
+ * JSON for the others) over it. Throws `MOSTRA_REMOTE_CONFIG_INVALID` naming the first key it
1082
+ * can't use.
1083
+ */
1084
+ function readServerConfig(file, env) {
1085
+ let raw = {};
1086
+ if (file !== void 0) {
1087
+ let text;
1088
+ try {
1089
+ text = readFileSync(file, "utf8");
1090
+ } catch (error) {
1091
+ throw invalid$1(`The configuration file ${file} can't be read (${error.code ?? "error"}).`);
1092
+ }
1093
+ try {
1094
+ const parsed = JSON.parse(text);
1095
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("not an object");
1096
+ raw = parsed;
1097
+ } catch {
1098
+ throw invalid$1(`The configuration file ${file} isn't a JSON object.`);
1099
+ }
1100
+ }
1101
+ for (const key of KEYS) {
1102
+ const variable = variableOf(key);
1103
+ const value = env[variable];
1104
+ if (value === void 0 || value === "") continue;
1105
+ if (key === "apiOrigin" || key === "operator" || key === "scheme") raw[key] = value;
1106
+ else try {
1107
+ raw[key] = JSON.parse(value);
1108
+ } catch {
1109
+ throw invalid$1(`${variable} isn't JSON.`);
1110
+ }
1111
+ }
1112
+ const checked = Config.safeParse(raw);
1113
+ if (!checked.success) {
1114
+ const issue = checked.error.issues[0];
1115
+ throw invalid$1(`The configuration key \`${issue?.path.join(".") || "(configuration)"}\` can't be used: ${issue?.message ?? "invalid"}.`);
1116
+ }
1117
+ const config = checked.data;
1118
+ const domains = [config.domains.active, ...config.domains.spares ?? []];
1119
+ const api = new URL(config.apiOrigin).host;
1120
+ if (domains.some((domain) => domain === api || api.endsWith(`.${domain}`))) throw invalid$1("The configuration key `apiOrigin` can't be on a user-content domain: sign-in and the binding live apart from tenant pages (D165 item 7).");
1121
+ const claimed = (config.identityProviders ?? []).findIndex((provider) => provider.claims !== void 0 && provider.kind !== "oidc");
1122
+ if (claimed >= 0) throw invalid$1(`The configuration key \`identityProviders.${claimed}.claims\` can't be used: only an \`oidc\` provider passes on its ID token's claims.`);
1123
+ const proxy = validProxies(config.trustedProxies ?? []);
1124
+ if (proxy !== void 0) throw invalid$1(`The configuration key \`trustedProxies\` names ${JSON.stringify(proxy)}, which is neither an address nor a CIDR block.`);
1125
+ if (new Set(domains).size !== domains.length) throw invalid$1("The configuration key `domains` names one domain twice.");
1126
+ return config;
1127
+ }
1128
+ //#endregion
792
1129
  //#region src/server/auth.ts
793
1130
  /**
794
1131
  * The remote server's authorization server, as far as WP-29 builds it (INTERFACES §18.5, D165
@@ -868,23 +1205,42 @@ function createAuthorization(options) {
868
1205
  const verificationUri = `${config.apiOrigin}/device`;
869
1206
  directory.write((db) => {
870
1207
  db.run("INSERT OR IGNORE INTO oauth_client (id, kind, metadata_json, created_at) VALUES (?, 'cli', ?, ?)", CLI_CLIENT_ID, JSON.stringify({ client_name: "Mostra CLI" }), iso(now()));
1208
+ const at = iso(now());
1209
+ for (const provider of config.identityProviders ?? []) {
1210
+ const label = configuredVerifier(provider);
1211
+ if (label === void 0) continue;
1212
+ db.run(`UPDATE identity SET verified_by = ?, verified_recorded_at = ?
1213
+ WHERE provider = ? AND verified_recorded_at IS NULL
1214
+ AND (name IS NULL OR subject != lower(name))
1215
+ AND (? != 'github' OR (subject != '' AND subject NOT GLOB '*[^0-9]*'))`, label, at, provider.id, provider.kind);
1216
+ }
1217
+ db.run("UPDATE identity SET verified_recorded_at = ? WHERE verified_recorded_at IS NULL", at);
871
1218
  });
872
1219
  const deviceRow = (db, hash) => db.get("SELECT data_json, expires_at FROM token WHERE hash = ? AND kind = 'device'", hash);
873
1220
  /** An account the operator names: kept in the directory under the operator's own ID. */
874
- const operatorAccount = (db, identity, account, name, status) => {
1221
+ const operatorAccount = (db, identity, account, name, status, verifiedBy) => {
875
1222
  db.run(`INSERT INTO account (id, name, status, created_at) VALUES (?, ?, ?, ?)
876
1223
  ON CONFLICT (id) DO UPDATE SET name = COALESCE(excluded.name, name), status = excluded.status`, account, name ?? identity.name ?? null, status, iso(now()));
877
- db.run(`INSERT INTO identity (provider, subject, account_id, email, name, linked_at) VALUES (?, ?, ?, ?, ?, ?)
1224
+ db.run(`INSERT INTO identity (provider, subject, account_id, email, name, linked_at, verified_by, verified_recorded_at)
1225
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
878
1226
  ON CONFLICT (provider, subject) DO UPDATE SET account_id = excluded.account_id,
879
- email = excluded.email, name = excluded.name`, identity.provider, identity.subject, account, identity.email ?? null, identity.name ?? null, iso(now()));
1227
+ email = excluded.email, name = excluded.name, verified_by = excluded.verified_by,
1228
+ verified_recorded_at = excluded.verified_recorded_at`, identity.provider, identity.subject, account, identity.email ?? null, identity.name ?? null, iso(now()), verifiedBy ?? null, iso(now()));
880
1229
  };
881
- /** The account for a sign-in identity, made on the first sign-in. */
882
- const accountFor = (db, identity) => {
1230
+ /**
1231
+ * The account for a sign-in identity, made on the first sign-in. The identity records who
1232
+ * verified the person's name as it signs in (`verifiedBy`, the provider's verifier label, or none),
1233
+ * so a provider renamed or retired later changes no sender line (MT-03).
1234
+ */
1235
+ const accountFor = (db, identity, verifiedBy) => {
883
1236
  const found = db.get("SELECT account_id FROM identity WHERE provider = ? AND subject = ?", identity.provider, identity.subject);
884
- if (found) return found.account_id;
1237
+ if (found) {
1238
+ db.run("UPDATE identity SET verified_by = ?, verified_recorded_at = ? WHERE provider = ? AND subject = ?", verifiedBy ?? null, iso(now()), identity.provider, identity.subject);
1239
+ return found.account_id;
1240
+ }
885
1241
  const account = AccountId.create();
886
1242
  db.run("INSERT INTO account (id, name, status, created_at) VALUES (?, ?, 'active', ?)", account, identity.name ?? null, iso(now()));
887
- db.run("INSERT INTO identity (provider, subject, account_id, name, linked_at) VALUES (?, ?, ?, ?, ?)", identity.provider, identity.subject, account, identity.name ?? null, iso(now()));
1243
+ db.run("INSERT INTO identity (provider, subject, account_id, name, linked_at, verified_by, verified_recorded_at) VALUES (?, ?, ?, ?, ?, ?, ?)", identity.provider, identity.subject, account, identity.name ?? null, iso(now()), verifiedBy ?? null, iso(now()));
888
1244
  return account;
889
1245
  };
890
1246
  /** The account's hosted home, made on its first link (one home per owner per server). */
@@ -1157,11 +1513,11 @@ function createAuthorization(options) {
1157
1513
  return { ok: true };
1158
1514
  });
1159
1515
  },
1160
- async resolveAccount(identity, steps) {
1516
+ async resolveAccount(identity, steps, verifiedBy) {
1161
1517
  const verdict = options.operator ? await options.operator.resolve(identity, steps) : { default: true };
1162
1518
  if (verdict === "unavailable") return { kind: "unavailable" };
1163
1519
  if ("default" in verdict) {
1164
- const accountId = directory.write((db) => accountFor(db, identity));
1520
+ const accountId = directory.write((db) => accountFor(db, identity, verifiedBy));
1165
1521
  if (directory.read((db) => db.get("SELECT status FROM account WHERE id = ?", accountId)?.status) === "suspended") return { kind: "suspended" };
1166
1522
  return {
1167
1523
  kind: "account",
@@ -1177,7 +1533,7 @@ function createAuthorization(options) {
1177
1533
  kind: "refused",
1178
1534
  message: verdict.refused.message
1179
1535
  };
1180
- directory.write((db) => operatorAccount(db, identity, verdict.account, verdict.name, verdict.status));
1536
+ directory.write((db) => operatorAccount(db, identity, verdict.account, verdict.name, verdict.status, verifiedBy));
1181
1537
  if (verdict.status === "suspended") return { kind: "suspended" };
1182
1538
  return {
1183
1539
  kind: "account",
@@ -1187,11 +1543,14 @@ function createAuthorization(options) {
1187
1543
  },
1188
1544
  accountName(accountId) {
1189
1545
  return directory.read((db) => {
1190
- const row = db.get(`SELECT a.name, (SELECT i.provider FROM identity i WHERE i.account_id = a.id
1191
- ORDER BY i.linked_at LIMIT 1) AS provider FROM account a WHERE a.id = ?`, accountId);
1546
+ const row = db.get(`SELECT a.name, i.provider, i.verified_by FROM account a
1547
+ LEFT JOIN identity i ON i.rowid = (SELECT rowid FROM identity WHERE account_id = a.id
1548
+ ORDER BY linked_at, rowid LIMIT 1)
1549
+ WHERE a.id = ?`, accountId);
1192
1550
  return {
1193
1551
  ...row?.name ? { name: row.name } : {},
1194
- ...row?.provider ? { provider: row.provider } : {}
1552
+ ...row?.provider ? { provider: row.provider } : {},
1553
+ ...row?.verified_by ? { verifiedBy: row.verified_by } : {}
1195
1554
  };
1196
1555
  });
1197
1556
  },
@@ -1306,7 +1665,7 @@ function createAuthorization(options) {
1306
1665
  }), row.hash);
1307
1666
  return { ok: true };
1308
1667
  }
1309
- const account = accountFor(db, input.identity);
1668
+ const account = accountFor(db, input.identity, void 0);
1310
1669
  db.run("INSERT INTO consent (account_id, client_id, granted_at) VALUES (?, ?, ?) ON CONFLICT (account_id, client_id) DO UPDATE SET revoked_at = NULL", account, CLI_CLIENT_ID, iso(now()));
1311
1670
  db.run("UPDATE token SET data_json = ? WHERE hash = ?", JSON.stringify({
1312
1671
  ...state,
@@ -1764,6 +2123,16 @@ function createAuthorize(options) {
1764
2123
  return setCookie(sessionCookie, value, Math.floor(SESSION_MS / 1e3));
1765
2124
  };
1766
2125
  const csrf = (sessionHash, purpose) => createHash("sha256").update(`mostra csrf ${sessionHash} ${purpose}`).digest("base64url");
2126
+ /**
2127
+ * The label a provider's verified names carry (`IdentityProvider.verifies`, MT-03): text of 1 to
2128
+ * 64 characters, else none, since an operator's provider is checked as every operator answer is.
2129
+ */
2130
+ const verifierOf = (provider) => {
2131
+ const raw = provider.verifies?.label;
2132
+ const label = verifierLabel(raw);
2133
+ if (label === void 0 && provider.verifies !== void 0) log.warn("signin.verifier_refused", { provider: provider.id });
2134
+ return label;
2135
+ };
1767
2136
  /** The providers now: configured ones, then the operator's (asked each time, D167). */
1768
2137
  const providers = async () => {
1769
2138
  const supplied = await operator.providers();
@@ -1778,8 +2147,8 @@ function createAuthorize(options) {
1778
2147
  /** Where a finished sign-in continues: an address of the API origin only. */
1779
2148
  const safeNext = (next) => next?.startsWith("/") && !next.startsWith("//") ? next : "/";
1780
2149
  /** A resolved identity: a session and on to `next`, a step, or why not. */
1781
- const resolved = async (response, identity, steps, flow, next) => {
1782
- const account = await auth.resolveAccount(identity, steps);
2150
+ const resolved = async (response, identity, verifiedBy, steps, flow, next) => {
2151
+ const account = await auth.resolveAccount(identity, steps, verifiedBy);
1783
2152
  if (account.kind === "unavailable") {
1784
2153
  problem(response, 503, "Sign-in is unavailable for a moment; try again shortly.");
1785
2154
  return;
@@ -1800,6 +2169,7 @@ function createAuthorize(options) {
1800
2169
  const handle = token();
1801
2170
  keep("step", handle, {
1802
2171
  identity,
2172
+ ...verifiedBy !== void 0 ? { verifiedBy } : {},
1803
2173
  steps: steps + 1,
1804
2174
  flow,
1805
2175
  next
@@ -2133,7 +2503,7 @@ function createAuthorize(options) {
2133
2503
  problem(response, 400, `${provider.label} didn't sign you in: ${error.message}`);
2134
2504
  return true;
2135
2505
  }
2136
- await resolved(response, identity, 0, pending.flow, pending.next);
2506
+ await resolved(response, identity, verifierOf(provider), 0, pending.flow, pending.next);
2137
2507
  return true;
2138
2508
  }
2139
2509
  if (method === "GET" && path === "/signin/return") {
@@ -2144,7 +2514,7 @@ function createAuthorize(options) {
2144
2514
  problem(response, 400, "This sign-in step expired, was used, or belongs to another browser.");
2145
2515
  return true;
2146
2516
  }
2147
- await resolved(response, step.identity, step.steps, step.flow, step.next);
2517
+ await resolved(response, step.identity, step.verifiedBy, step.steps, step.flow, step.next);
2148
2518
  return true;
2149
2519
  }
2150
2520
  return false;
@@ -2152,182 +2522,6 @@ function createAuthorize(options) {
2152
2522
  };
2153
2523
  }
2154
2524
  //#endregion
2155
- //#region src/server/clients.ts
2156
- /** An address as a number (IPv4 in 32 bits, IPv6 in 128), or `undefined` when it isn't one. */
2157
- function addressBits(address) {
2158
- let text = address.trim();
2159
- if (text.startsWith("[") && text.endsWith("]")) text = text.slice(1, -1);
2160
- const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(text);
2161
- if (mapped) text = mapped[1];
2162
- if (isIPv4(text)) return {
2163
- v6: false,
2164
- bits: text.split(".").map(Number).reduce((sum, part) => sum << 8n | BigInt(part), 0n)
2165
- };
2166
- if (!isIPv6(text)) return void 0;
2167
- const zone = text.indexOf("%");
2168
- if (zone !== -1) text = text.slice(0, zone);
2169
- const last = text.slice(text.lastIndexOf(":") + 1);
2170
- if (last.includes(".")) {
2171
- if (!isIPv4(last)) return void 0;
2172
- const parts = last.split(".").map(Number);
2173
- const high = (parts[0] << 8 | parts[1]).toString(16);
2174
- const low = (parts[2] << 8 | parts[3]).toString(16);
2175
- text = `${text.slice(0, -last.length)}${high}:${low}`;
2176
- }
2177
- const [head, tail] = text.split("::");
2178
- const left = head === "" ? [] : head.split(":");
2179
- const right = tail === void 0 ? [] : tail === "" ? [] : tail.split(":");
2180
- const groups = tail === void 0 ? left : [
2181
- ...left,
2182
- ...new Array(8 - left.length - right.length).fill("0"),
2183
- ...right
2184
- ];
2185
- if (groups.length !== 8) return void 0;
2186
- return {
2187
- v6: true,
2188
- bits: groups.reduce((sum, group) => sum << 16n | BigInt(Number.parseInt(group, 16)), 0n)
2189
- };
2190
- }
2191
- function parseRange(text) {
2192
- const [address, prefixText] = text.split("/");
2193
- const found = addressBits(address);
2194
- if (!found) return void 0;
2195
- const width = found.v6 ? 128 : 32;
2196
- const prefix = prefixText === void 0 ? width : Number(prefixText);
2197
- if (!Number.isInteger(prefix) || prefix < 0 || prefix > width) return void 0;
2198
- return {
2199
- v6: found.v6,
2200
- bits: found.bits,
2201
- prefix
2202
- };
2203
- }
2204
- function inRange(range, address) {
2205
- if (range.v6 !== address.v6) return false;
2206
- const shift = (range.v6 ? 128n : 32n) - BigInt(range.prefix);
2207
- return range.bits >> shift === address.bits >> shift;
2208
- }
2209
- /** Whether every entry of `trustedProxies` is an address or a CIDR block. */
2210
- function validProxies(entries) {
2211
- return entries.find((entry) => parseRange(entry) === void 0);
2212
- }
2213
- /** Builds the resolver; an entry that isn't an address or a block is ignored (checked at start). */
2214
- function clientAddresses(options) {
2215
- const proxies = (options.trustedProxies ?? []).map(parseRange).filter((range) => range !== void 0);
2216
- const trusted = (address) => {
2217
- const bits = addressBits(address);
2218
- return bits !== void 0 && proxies.some((range) => inRange(range, bits));
2219
- };
2220
- const ipv6Prefix = options.ipv6Prefix ?? 64;
2221
- const seen = /* @__PURE__ */ new WeakMap();
2222
- return {
2223
- of(incoming) {
2224
- if (seen.has(incoming)) return seen.get(incoming);
2225
- const peer = incoming.socket.remoteAddress;
2226
- let client = peer;
2227
- if (peer !== void 0 && trusted(peer)) {
2228
- const header = incoming.headers["x-forwarded-for"];
2229
- const entries = (Array.isArray(header) ? header.join(",") : header ?? "").split(",").map((entry) => entry.trim()).filter((entry) => entry !== "");
2230
- for (let index = entries.length - 1; index >= 0; index--) {
2231
- const entry = entries[index];
2232
- if (addressBits(entry) === void 0) break;
2233
- client = entry;
2234
- if (!trusted(entry)) break;
2235
- }
2236
- }
2237
- seen.set(incoming, client);
2238
- return client;
2239
- },
2240
- keyOf(address) {
2241
- if (address === void 0) return "unknown";
2242
- const bits = addressBits(address);
2243
- if (!bits) return `other:${address}`;
2244
- if (!bits.v6) return `v4:${bits.bits.toString(16)}`;
2245
- const shift = 128n - BigInt(ipv6Prefix);
2246
- return `v6:${(bits.bits >> shift).toString(16)}/${ipv6Prefix}`;
2247
- }
2248
- };
2249
- }
2250
- /**
2251
- * A token-bucket rate limiter over keys, in memory, bounded in the keys it keeps: each key holds up
2252
- * to `count` tokens and earns them back at `count` per `perMs`, so a burst of `count` passes and
2253
- * the sustained rate is exactly the rate.
2254
- */
2255
- var RateLimiter = class {
2256
- #buckets = /* @__PURE__ */ new Map();
2257
- #now;
2258
- #maxKeys;
2259
- constructor(options = {}) {
2260
- this.#now = options.now ?? (() => Date.now());
2261
- this.#maxKeys = options.maxKeys ?? 1e5;
2262
- }
2263
- /** A key's bucket, refilled to now and marked most recently used, so the bound drops the least. */
2264
- #bucket(key, rate, now) {
2265
- const perToken = rate.perMs / rate.count;
2266
- let bucket = this.#buckets.get(key);
2267
- if (bucket) {
2268
- bucket.tokens = Math.min(rate.count, bucket.tokens + (now - bucket.at) / perToken);
2269
- bucket.at = now;
2270
- this.#buckets.delete(key);
2271
- } else {
2272
- if (this.#buckets.size >= this.#maxKeys) this.#buckets.delete(this.#buckets.keys().next().value);
2273
- bucket = {
2274
- tokens: rate.count,
2275
- at: now
2276
- };
2277
- }
2278
- this.#buckets.set(key, bucket);
2279
- return bucket;
2280
- }
2281
- /** How long until every bucket holds a token: 0 when they all do now. */
2282
- #wait(wanted, now) {
2283
- let waitMs = 0;
2284
- const buckets = wanted.map(({ key, rate }) => {
2285
- const bucket = this.#bucket(key, rate, now);
2286
- if (bucket.tokens < 1) waitMs = Math.max(waitMs, (1 - bucket.tokens) * (rate.perMs / rate.count));
2287
- return bucket;
2288
- });
2289
- return {
2290
- waitMs,
2291
- buckets
2292
- };
2293
- }
2294
- /**
2295
- * Takes one token under `key` when there is one. Without one, nothing is taken and the answer
2296
- * says when to try again, in whole seconds (`Retry-After`).
2297
- */
2298
- take(key, rate) {
2299
- return this.takeAll([{
2300
- key,
2301
- rate
2302
- }]);
2303
- }
2304
- /**
2305
- * Gives back the token `takeAll` took under every key, as far as each bucket's size: an attempt
2306
- * someone refused after the rates admitted it (the operator's `limits`) costs nothing (second
2307
- * Fable review of WP-30, 2).
2308
- */
2309
- refundAll(wanted) {
2310
- const now = this.#now();
2311
- for (const { key, rate } of wanted) {
2312
- const bucket = this.#bucket(key, rate, now);
2313
- bucket.tokens = Math.min(rate.count, bucket.tokens + 1);
2314
- }
2315
- }
2316
- /**
2317
- * Takes one token under every key, or none: when any bucket is empty, nothing is taken from the
2318
- * others, and the answer says when the slowest one has a token again (Fable review, 8).
2319
- */
2320
- takeAll(wanted) {
2321
- const { waitMs, buckets } = this.#wait(wanted, this.#now());
2322
- if (waitMs > 0) return {
2323
- ok: false,
2324
- retryAfterS: Math.max(1, Math.ceil(waitMs / 1e3))
2325
- };
2326
- for (const bucket of buckets) bucket.tokens -= 1;
2327
- return { ok: true };
2328
- }
2329
- };
2330
- //#endregion
2331
2525
  //#region src/server/directory.ts
2332
2526
  /** The server directory's migrations: their own list, apart from a home's. */
2333
2527
  const SERVER_MIGRATIONS = [
@@ -2521,6 +2715,62 @@ const SERVER_MIGRATIONS = [
2521
2715
  up(db) {
2522
2716
  db.exec("ALTER TABLE artifact_route ADD COLUMN removing_json TEXT");
2523
2717
  }
2718
+ },
2719
+ {
2720
+ version: 4,
2721
+ name: "invitations in the outbound queue",
2722
+ up(db) {
2723
+ db.exec(`
2724
+ CREATE TABLE outbound_next (
2725
+ id TEXT PRIMARY KEY,
2726
+ port TEXT NOT NULL CHECK (port IN ('metering', 'events', 'invitations')),
2727
+ payload_json TEXT NOT NULL,
2728
+ attempts INTEGER NOT NULL DEFAULT 0,
2729
+ next_attempt_at TEXT NOT NULL
2730
+ );
2731
+ INSERT INTO outbound_next (rowid, id, port, payload_json, attempts, next_attempt_at)
2732
+ SELECT rowid, id, port, payload_json, attempts, next_attempt_at FROM outbound;
2733
+ DROP TABLE outbound;
2734
+ ALTER TABLE outbound_next RENAME TO outbound;
2735
+ `);
2736
+ }
2737
+ },
2738
+ {
2739
+ version: 5,
2740
+ name: "the verifier on each identity",
2741
+ up(db) {
2742
+ db.exec(`
2743
+ ALTER TABLE identity ADD COLUMN verified_by TEXT;
2744
+ ALTER TABLE identity ADD COLUMN verified_recorded_at TEXT;
2745
+ `);
2746
+ }
2747
+ },
2748
+ {
2749
+ version: 6,
2750
+ name: "what each home holds",
2751
+ up(db) {
2752
+ db.exec(`
2753
+ ALTER TABLE home_file ADD COLUMN size INTEGER CHECK (size IS NULL OR size >= 0);
2754
+ ALTER TABLE home ADD COLUMN held_bytes INTEGER NOT NULL DEFAULT 0 CHECK (held_bytes >= 0);
2755
+ ALTER TABLE home ADD COLUMN held_reported INTEGER;
2756
+ ALTER TABLE home ADD COLUMN held_dirty INTEGER NOT NULL DEFAULT 0 CHECK (held_dirty IN (0, 1));
2757
+ CREATE INDEX home_held_dirty ON home (held_dirty) WHERE held_dirty = 1;
2758
+ CREATE TRIGGER home_file_held_insert AFTER INSERT ON home_file WHEN NEW.size IS NOT NULL
2759
+ BEGIN
2760
+ UPDATE home SET held_bytes = held_bytes + NEW.size, held_dirty = 1 WHERE id = NEW.home_id;
2761
+ END;
2762
+ CREATE TRIGGER home_file_held_delete AFTER DELETE ON home_file WHEN OLD.size IS NOT NULL
2763
+ BEGIN
2764
+ UPDATE home SET held_bytes = held_bytes - OLD.size, held_dirty = 1 WHERE id = OLD.home_id;
2765
+ END;
2766
+ CREATE TRIGGER home_file_held_size AFTER UPDATE OF size ON home_file
2767
+ WHEN COALESCE(OLD.size, -1) != COALESCE(NEW.size, -1)
2768
+ BEGIN
2769
+ UPDATE home SET held_bytes = held_bytes - COALESCE(OLD.size, 0) + COALESCE(NEW.size, 0),
2770
+ held_dirty = 1 WHERE id = NEW.home_id;
2771
+ END;
2772
+ `);
2773
+ }
2524
2774
  }
2525
2775
  ];
2526
2776
  //#endregion
@@ -2930,7 +3180,7 @@ const RUN_ENDS = /* @__PURE__ */ new Map([
2930
3180
  /** Starts the feed of a home. */
2931
3181
  function createOperatorFeed(options) {
2932
3182
  const { ledger, operator, meter, log } = options;
2933
- const wanted = operator.has.events || meter.on;
3183
+ const wanted = operator.has.events || meter.on || operator.has.invitations;
2934
3184
  let stopped = false;
2935
3185
  let scheduled = false;
2936
3186
  const cursorOf = () => ledger.read((db) => db.get("SELECT cursor FROM operator_cursor WHERE id = 1")?.cursor ?? -1);
@@ -2974,6 +3224,15 @@ function createOperatorFeed(options) {
2974
3224
  number: payload["number"]
2975
3225
  }
2976
3226
  };
3227
+ if (row.type === "roster.completed") return {
3228
+ ...base,
3229
+ kind: "roster.completed",
3230
+ payload: {
3231
+ round: payload["round"],
3232
+ respondents: payload["respondents"],
3233
+ submissions: payload["submissions"]
3234
+ }
3235
+ };
2977
3236
  const status = RUN_ENDS.get(row.type);
2978
3237
  if (status !== void 0) return {
2979
3238
  ...base,
@@ -2983,7 +3242,52 @@ function createOperatorFeed(options) {
2983
3242
  action: payload["action"],
2984
3243
  status
2985
3244
  }
2986
- };
3245
+ };
3246
+ };
3247
+ /**
3248
+ * The invitations a roster change calls for (D168): one per respondent it added or gave a new
3249
+ * link, who has an email and is still expected, keyed by their link's token, so a repeated read
3250
+ * enqueues nothing new and a later link gets an invitation of its own.
3251
+ */
3252
+ const invite = (row) => {
3253
+ if (row.artifact_id === null) return;
3254
+ const route = options.routeOf(row.artifact_id);
3255
+ if (!route) return;
3256
+ const payload = JSON.parse(row.payload_json);
3257
+ const refs = /* @__PURE__ */ new Set([...(payload.added ?? []).flatMap((entry) => typeof entry.ref === "string" ? [entry.ref] : []), ...(payload.relinked ?? []).filter((ref) => typeof ref === "string")]);
3258
+ if (refs.size === 0) return;
3259
+ const artifactId = row.artifact_id;
3260
+ const found = ledger.read((db) => ({
3261
+ title: db.get("SELECT title FROM artifact WHERE id = ?", artifactId)?.title,
3262
+ entries: [...refs].flatMap((ref) => {
3263
+ const entry = db.get("SELECT ref, name, email, token_id FROM roster_entry WHERE artifact_id = ? AND ref = ?", artifactId, ref);
3264
+ return entry && entry.email !== null && entry.token_id !== null ? [entry] : [];
3265
+ })
3266
+ }));
3267
+ const sender = options.sender?.() ?? {};
3268
+ for (const entry of found.entries) operator.enqueue({
3269
+ port: "invitations",
3270
+ invitation: {
3271
+ invitationId: `${options.homeId}:${entry.token_id}`,
3272
+ account: options.account(),
3273
+ home: options.homeId,
3274
+ routeId: route.id,
3275
+ label: route.label,
3276
+ artifactId,
3277
+ title: found.title ?? "",
3278
+ respondent: {
3279
+ ref: entry.ref,
3280
+ ...entry.name !== null ? { name: entry.name } : {},
3281
+ email: entry.email
3282
+ },
3283
+ sender: {
3284
+ ...sender.name !== void 0 ? { name: sender.name } : {},
3285
+ ...sender.verifiedBy !== void 0 ? { verifiedBy: sender.verifiedBy } : {}
3286
+ },
3287
+ tokenId: entry.token_id,
3288
+ at: row.at
3289
+ }
3290
+ });
2987
3291
  };
2988
3292
  const scan = () => {
2989
3293
  scheduled = false;
@@ -2998,9 +3302,14 @@ function createOperatorFeed(options) {
2998
3302
  for (;;) {
2999
3303
  const rows = ledger.read((db) => db.all(`SELECT cursor, artifact_id, type, payload_json, at FROM event WHERE cursor > ?
3000
3304
  AND type IN ('submission.created', 'version.published', 'action.succeeded',
3001
- 'action.failed', 'action.cancelled', 'action.unknown')
3305
+ 'action.failed', 'action.cancelled', 'action.unknown',
3306
+ 'roster.changed', 'roster.completed')
3002
3307
  ORDER BY cursor LIMIT ?`, cursor, PAGE));
3003
3308
  for (const row of rows) {
3309
+ if (row.type === "roster.changed") {
3310
+ if (operator.has.invitations) invite(row);
3311
+ continue;
3312
+ }
3004
3313
  const event = toEvent(row);
3005
3314
  if (!event) continue;
3006
3315
  if (event.kind === "submission.accepted") meter.add("submissions", {
@@ -3096,14 +3405,26 @@ function hostedPages(options) {
3096
3405
  ...tokenId !== void 0 ? { tokenId } : {}
3097
3406
  });
3098
3407
  };
3099
- /** A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9). */
3100
- const expired = (tokenId, policy) => {
3408
+ /**
3409
+ * A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9).
3410
+ * "Idle" is as `LinkPolicy.idleExpiry` says, without a view: the link's last use before this
3411
+ * visit is the later of its last view (a frame ticket) and the activity of its other sessions
3412
+ * (`last_seen_at`, which a page's every request keeps, so a page open and in use for longer
3413
+ * than the idle time keeps its link). The session asking for this ticket is left out, since its
3414
+ * own exchange, or any request of a returning browser, has just written it: counted, an idle
3415
+ * link would open for every new visit (MT-03, and its Opus review). A session's activity is
3416
+ * written at most every `touchMs` (an hour), so a link lapses at most that much early.
3417
+ */
3418
+ const expired = (tokenId, asking, policy) => {
3101
3419
  if (policy.maxTtl === void 0 && policy.idleExpiry === void 0) return void 0;
3102
- const used = ledger.read((db) => db.get(`SELECT t.created_at, (SELECT MAX(last_seen_at) FROM session s WHERE s.token_id = t.id) AS last
3103
- FROM token t WHERE t.id = ?`, tokenId));
3420
+ const used = ledger.read((db) => db.get(`SELECT t.created_at,
3421
+ (SELECT MAX(v.opened_at) FROM view v WHERE v.token_id = t.id) AS viewed,
3422
+ (SELECT MAX(s.last_seen_at) FROM session s WHERE s.token_id = t.id AND s.id != ?) AS active
3423
+ FROM token t WHERE t.id = ?`, asking, tokenId));
3104
3424
  if (!used) return void 0;
3105
3425
  const minted = Date.parse(used.created_at);
3106
- const lastUse = Math.max(minted, used.last === null ? 0 : Date.parse(used.last));
3426
+ const at = (value) => value === null ? 0 : Date.parse(value);
3427
+ const lastUse = Math.max(minted, at(used.viewed), at(used.active));
3107
3428
  const tooOld = policy.maxTtl !== void 0 && now() - minted > durationMs$1(policy.maxTtl);
3108
3429
  const idle = policy.idleExpiry !== void 0 && now() - lastUse > durationMs$1(policy.idleExpiry);
3109
3430
  if (!tooOld && !idle) return void 0;
@@ -3251,7 +3572,7 @@ function hostedPages(options) {
3251
3572
  async admitView(input) {
3252
3573
  if (quarantineOf(input.artifactId, input.versionId) !== void 0) return underReview();
3253
3574
  const policy = await policyFor(input.artifactId, input.tokenId);
3254
- const gone = expired(input.tokenId, policy);
3575
+ const gone = expired(input.tokenId, input.sessionId, policy);
3255
3576
  if (gone) return gone;
3256
3577
  const route = options.routeOf(input.artifactId);
3257
3578
  const verdict = await operator.limit({
@@ -3422,16 +3743,18 @@ function createHostedRelay(options) {
3422
3743
  /** Each link's recent relayed runs and calls, for the per-link limit. */
3423
3744
  const linkUses = /* @__PURE__ */ new Map();
3424
3745
  /**
3746
+ * Whether a run request joins a run already recorded (its client key, or its key): it starts
3747
+ * nothing, so it is neither counted against its link nor asked of the operator.
3748
+ */
3749
+ const joinsRun = (artifactId, input) => ledger.read((db) => db.get("SELECT 1 FROM action_run WHERE artifact_id = ? AND (client_key = ? OR (key IS NOT NULL AND key = ? AND action = ?))", artifactId, input.clientKey, input.key ?? null, input.fnId) !== void 0);
3750
+ /**
3425
3751
  * Counts a relayed run or call against its link (D159), refusing it past the limit with
3426
- * `MOSTRA_REMOTE_RATE_LIMITED` and when to try again. A request that joins a run already
3427
- * recorded (its client key, or its key) starts nothing, so it is never counted.
3752
+ * `MOSTRA_REMOTE_RATE_LIMITED` and when to try again. Answers how to give the use back, for a
3753
+ * request the operator then refuses (as a refused submit gets its tokens back, D167 item 24).
3428
3754
  */
3429
- const limitLink = (tokenId, artifactId, input) => {
3755
+ const limitLink = (tokenId) => {
3430
3756
  const limit = options.perLink;
3431
- if (!limit || tokenId === void 0) return;
3432
- if (artifactId !== void 0 && input !== void 0) {
3433
- if (ledger.read((db) => db.get("SELECT 1 FROM action_run WHERE artifact_id = ? AND (client_key = ? OR (key IS NOT NULL AND key = ? AND action = ?))", artifactId, input.clientKey, input.key ?? null, input.fnId) !== void 0)) return;
3434
- }
3757
+ if (!limit || tokenId === void 0) return () => {};
3435
3758
  const at = now();
3436
3759
  const recent = (linkUses.get(tokenId) ?? []).filter((used) => at - used < limit.perMs);
3437
3760
  if (recent.length >= limit.count) {
@@ -3443,7 +3766,50 @@ function createHostedRelay(options) {
3443
3766
  }
3444
3767
  recent.push(at);
3445
3768
  linkUses.set(tokenId, recent);
3769
+ let given = false;
3770
+ return () => {
3771
+ if (given) return;
3772
+ given = true;
3773
+ const uses = linkUses.get(tokenId);
3774
+ const index = uses?.indexOf(at) ?? -1;
3775
+ if (uses && index >= 0) uses.splice(index, 1);
3776
+ };
3777
+ };
3778
+ /**
3779
+ * Asks the operator's `limits` for a relayed action (D165 item 9). A refusal of `run` or `call`
3780
+ * answers 403 `MOSTRA_REMOTE_LIMIT_REACHED` and one of `run.wait` 503 `MOSTRA_REMOTE_QUEUE_FULL`,
3781
+ * each with the operator's message (and `Retry-After` when it gives one); no answer lets the
3782
+ * action through, since it is viewer input (INTERFACES §18.6).
3783
+ */
3784
+ const admit = async (artifactId, operation) => {
3785
+ if (!options.limit) return;
3786
+ const verdict = await options.limit(artifactId, operation);
3787
+ if (verdict.allowed !== false) return;
3788
+ const error = failed(operation === "run.wait" ? "MOSTRA_REMOTE_QUEUE_FULL" : "MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
3789
+ if (verdict.retryAfterMs !== void 0) error.retryAfter = Math.max(1, Math.ceil(verdict.retryAfterMs / 1e3));
3790
+ throw error;
3791
+ };
3792
+ /**
3793
+ * What may have changed while the operator answered (MT-03, the Opus review): a page paused, or
3794
+ * its link revoked or expired, runs nothing it asked for before, so both are judged again after
3795
+ * every wait, in the same synchronous stretch as the record (D165 item 14, D97 item 5).
3796
+ */
3797
+ const stillAdmitted = (page) => {
3798
+ const pause = interaction.pauseOf(page.artifactId);
3799
+ if (pause) throw failed("MOSTRA_ACTION_PAUSED", `This page is paused${pause.by === "operator" ? " by its server's operator" : ""}: its actions don't run until it is resumed.`);
3800
+ const tokenId = page.tokenId;
3801
+ if (tokenId === void 0) return;
3802
+ const token = ledger.read((db) => db.get("SELECT revoked_at, expires_at FROM token WHERE id = ?", tokenId));
3803
+ if (!token || token.revoked_at !== null) throw failed("MOSTRA_HYGIENE_TOKEN_REVOKED", "This link was revoked.");
3804
+ if (token.expires_at !== null && Date.parse(token.expires_at) <= now()) throw failed("MOSTRA_HYGIENE_TOKEN_REVOKED", "This link has expired.");
3446
3805
  };
3806
+ /**
3807
+ * Run requests still on their way to a record, by the client key and the key they would join:
3808
+ * a duplicate sent meanwhile waits for the first one's outcome, then joins what it recorded
3809
+ * (asking nothing) or, if it recorded nothing, goes on as itself (MT-03, the Opus review).
3810
+ */
3811
+ const inflight = /* @__PURE__ */ new Map();
3812
+ const inflightKeys = (artifactId, input) => [`${artifactId}\u0000c\u0000${input.clientKey}`, ...input.key !== void 0 ? [`${artifactId}\u0000k\u0000${input.fnId}\u0000${input.key}`] : []];
3447
3813
  const checkArgs = (args) => {
3448
3814
  if (JSON.stringify(args).length > ACTION_ARGS_MAX) throw failed("MOSTRA_ACTION_TOO_LARGE", "The serialized arguments are larger than 16 MiB; pass less data, such as identifiers.");
3449
3815
  try {
@@ -3860,15 +4226,86 @@ function createHostedRelay(options) {
3860
4226
  ownerUnreachable: true
3861
4227
  };
3862
4228
  };
4229
+ /** Starts or joins a relayed run: `startRun` without its guard against concurrent duplicates. */
4230
+ const beginRun = async (page, input) => {
4231
+ checkArgs(input.args);
4232
+ const versionId = interaction.viewVersion(page, input.viewId);
4233
+ const version = functionOf(versionId, input.fnId);
4234
+ mayTrigger(page.viewerId, version);
4235
+ const joins = joinsRun(page.artifactId, input);
4236
+ const giveBack = joins ? () => {} : limitLink(page.tokenId);
4237
+ const owner = options.ownerOf(page.artifactId);
4238
+ /**
4239
+ * Past the cap a run is refused before anything is recorded (D165 item 4). A request that
4240
+ * joins a run already recorded is never refused.
4241
+ */
4242
+ const capped = (link) => {
4243
+ if (sessionFor(link) !== void 0 || version.policy !== "queue") return;
4244
+ if (ledger.read((db) => db.get("SELECT COUNT(*) AS n FROM action_run WHERE artifact_id = ? AND status = 'waiting'", page.artifactId)?.n ?? 0) >= cap && !joinsRun(page.artifactId, input)) throw failed("MOSTRA_REMOTE_QUEUE_FULL", "Too many actions are waiting for this page's owner; try again once it is back.");
4245
+ };
4246
+ try {
4247
+ if (owner === void 0) throw failed("MOSTRA_REMOTE_OWNER_OFFLINE", "This page has no owner instance to run it.");
4248
+ capped(owner);
4249
+ if (!joins) {
4250
+ await admit(page.artifactId, "run");
4251
+ if (sessionFor(owner) === void 0 && version.policy === "queue") await admit(page.artifactId, "run.wait");
4252
+ capped(owner);
4253
+ }
4254
+ stillAdmitted(page);
4255
+ } catch (error) {
4256
+ giveBack();
4257
+ throw error;
4258
+ }
4259
+ const connected = sessionFor(owner) !== void 0;
4260
+ const { state, row, started } = interaction.startRun(page, input);
4261
+ bus.attachRuns(page.artifactId, input.viewId, [state.runId]);
4262
+ if (!started) {
4263
+ giveBack();
4264
+ return decorate(state, page.artifactId);
4265
+ }
4266
+ const recorded = Date.parse(row.started_at);
4267
+ const waitingState = ledger.write((db) => {
4268
+ const seq = (db.get("SELECT MAX(relay_seq) AS high FROM relay_run WHERE owner_link_id = ?", owner)?.high ?? 0) + 1;
4269
+ const floor = Math.max(marks.get(owner) ?? 0, db.get("SELECT floor FROM relay_floor WHERE link_id = ?", owner)?.floor ?? 0);
4270
+ db.run("INSERT INTO relay_run (run_id, owner_link_id, relay_seq, offline_limit_at) VALUES (?, ?, ?, ?)", row.id, owner, Math.max(seq, floor + 1), iso(recorded + version.limitMs));
4271
+ if (connected) return void 0;
4272
+ markWaiting(db, row.id);
4273
+ const after = runRow(db, row.id);
4274
+ return after ? runState(db, after) : void 0;
4275
+ });
4276
+ if (connected) {
4277
+ const session = sessionFor(owner);
4278
+ if (session !== void 0) send(row.id, session);
4279
+ return state;
4280
+ }
4281
+ if (version.policy === "fail") {
4282
+ endOffline(row.id);
4283
+ return interaction.runState(row.id) ?? state;
4284
+ }
4285
+ if (waitingState) publish(waitingState, page.artifactId);
4286
+ return waitingState ?? state;
4287
+ };
3863
4288
  return {
3864
4289
  async call(page, input, signal) {
3865
4290
  checkArgs(input.args);
3866
4291
  const versionId = interaction.viewVersion(page, input.viewId);
3867
4292
  mayTrigger(page.viewerId, functionOf(versionId, input.fnId));
3868
- limitLink(page.tokenId);
3869
- const owner = options.ownerOf(page.artifactId);
3870
- const session = owner === void 0 ? void 0 : sessionFor(owner);
3871
- if (session === void 0) throw failed("MOSTRA_REMOTE_OWNER_OFFLINE", "The page's owner is offline, so its server code can't answer now; try again later.");
4293
+ const giveBack = limitLink(page.tokenId);
4294
+ const offline = () => failed("MOSTRA_REMOTE_OWNER_OFFLINE", "The page's owner is offline, so its server code can't answer now; try again later.");
4295
+ let session;
4296
+ try {
4297
+ const owner = options.ownerOf(page.artifactId);
4298
+ if (owner === void 0 || sessionFor(owner) === void 0) throw offline();
4299
+ await admit(page.artifactId, "call");
4300
+ if (signal.aborted) throw failed("MOSTRA_ACTION_CANCELLED", "The page stopped waiting for this call.");
4301
+ stillAdmitted(page);
4302
+ const current = sessionFor(owner);
4303
+ if (current === void 0) throw offline();
4304
+ session = current;
4305
+ } catch (error) {
4306
+ giveBack();
4307
+ throw error;
4308
+ }
3872
4309
  const callId = `call_${crypto.randomUUID()}`;
3873
4310
  const pending = {
3874
4311
  session,
@@ -3915,44 +4352,20 @@ function createHostedRelay(options) {
3915
4352
  };
3916
4353
  return lines();
3917
4354
  },
3918
- startRun(page, input) {
3919
- checkArgs(input.args);
3920
- const versionId = interaction.viewVersion(page, input.viewId);
3921
- const version = functionOf(versionId, input.fnId);
3922
- mayTrigger(page.viewerId, version);
3923
- limitLink(page.tokenId, page.artifactId, input);
3924
- const owner = options.ownerOf(page.artifactId);
3925
- if (owner === void 0) throw failed("MOSTRA_REMOTE_OWNER_OFFLINE", "This page has no owner instance to run it.");
3926
- const connected = sessionFor(owner) !== void 0;
3927
- if (!connected && version.policy === "queue") {
3928
- const waiting = ledger.read((db) => db.get("SELECT COUNT(*) AS n FROM action_run WHERE artifact_id = ? AND status = 'waiting'", page.artifactId)?.n ?? 0);
3929
- const joins = ledger.read((db) => db.get(`SELECT id FROM action_run WHERE artifact_id = ? AND (client_key = ? OR (key IS NOT NULL AND key = ? AND action = ?))`, page.artifactId, input.clientKey, input.key ?? null, input.fnId) !== void 0);
3930
- if (waiting >= cap && !joins) throw failed("MOSTRA_REMOTE_QUEUE_FULL", "Too many actions are waiting for this page's owner; try again once it is back.");
3931
- }
3932
- const { state, row, started } = interaction.startRun(page, input);
3933
- bus.attachRuns(page.artifactId, input.viewId, [state.runId]);
3934
- if (!started) return decorate(state, page.artifactId);
3935
- const recorded = Date.parse(row.started_at);
3936
- const waitingState = ledger.write((db) => {
3937
- const seq = (db.get("SELECT MAX(relay_seq) AS high FROM relay_run WHERE owner_link_id = ?", owner)?.high ?? 0) + 1;
3938
- const floor = Math.max(marks.get(owner) ?? 0, db.get("SELECT floor FROM relay_floor WHERE link_id = ?", owner)?.floor ?? 0);
3939
- db.run("INSERT INTO relay_run (run_id, owner_link_id, relay_seq, offline_limit_at) VALUES (?, ?, ?, ?)", row.id, owner, Math.max(seq, floor + 1), iso(recorded + version.limitMs));
3940
- if (connected) return void 0;
3941
- markWaiting(db, row.id);
3942
- const after = runRow(db, row.id);
3943
- return after ? runState(db, after) : void 0;
3944
- });
3945
- if (connected) {
3946
- const session = sessionFor(owner);
3947
- if (session !== void 0) send(row.id, session);
3948
- return state;
4355
+ async startRun(page, input) {
4356
+ const keys = inflightKeys(page.artifactId, input);
4357
+ for (;;) {
4358
+ const prior = keys.map((key) => inflight.get(key)).find((found) => found !== void 0);
4359
+ if (prior === void 0) break;
4360
+ await prior.catch(() => void 0);
3949
4361
  }
3950
- if (version.policy === "fail") {
3951
- endOffline(row.id);
3952
- return interaction.runState(row.id) ?? state;
4362
+ const started = beginRun(page, input);
4363
+ for (const key of keys) inflight.set(key, started);
4364
+ try {
4365
+ return await started;
4366
+ } finally {
4367
+ for (const key of keys) if (inflight.get(key) === started) inflight.delete(key);
3953
4368
  }
3954
- if (waitingState) publish(waitingState, page.artifactId);
3955
- return waitingState ?? state;
3956
4369
  },
3957
4370
  attach(page, input) {
3958
4371
  const states = interaction.runsFor(page, input);
@@ -4241,6 +4654,7 @@ async function openHostedHome(options) {
4241
4654
  let protocol;
4242
4655
  const interaction = createInteraction({
4243
4656
  sentOnly: true,
4657
+ isOwner: (viewer) => options.services !== void 0 && viewer.kind === "account" && viewer.ref === options.services.owner(id).account,
4244
4658
  ledger,
4245
4659
  log,
4246
4660
  faults: noFaults,
@@ -4305,6 +4719,7 @@ async function openHostedHome(options) {
4305
4719
  now: () => Date.now(),
4306
4720
  shellOrigin: () => originOf(options.domains.active),
4307
4721
  servesShell,
4722
+ ...options.sessionTouchMs !== void 0 ? { touchIntervalMs: options.sessionTouchMs } : {},
4308
4723
  onMinted: ({ tokenId, artifactId }) => {
4309
4724
  const route = routeOf(artifactId);
4310
4725
  if (!route) return;
@@ -4352,6 +4767,18 @@ async function openHostedHome(options) {
4352
4767
  ...options.relay?.sweepMs !== void 0 ? { sweepMs: options.relay.sweepMs } : {},
4353
4768
  ...options.relay?.liveRenewMs !== void 0 ? { liveRenewMs: options.relay.liveRenewMs } : {},
4354
4769
  ...options.relay?.perLink !== void 0 ? { perLink: options.relay.perLink } : {},
4770
+ ...options.services ? { limit: (artifactId, operation) => {
4771
+ const services = options.services;
4772
+ const route = routeOf(artifactId);
4773
+ return services.operator.limit({
4774
+ account: services.owner(id).account,
4775
+ home: id,
4776
+ artifact: artifactId,
4777
+ ...route ? { routeId: route.id } : {},
4778
+ operation,
4779
+ quantity: 1
4780
+ });
4781
+ } } : {},
4355
4782
  latestVersion: (artifactId) => ledger.read((db) => db.get("SELECT latest_version_id FROM artifact WHERE id = ?", artifactId)?.latest_version_id ?? void 0),
4356
4783
  storeLive: (artifactId, versionId, produced) => live.relayed(artifactId, versionId, produced)
4357
4784
  });
@@ -4368,6 +4795,7 @@ async function openHostedHome(options) {
4368
4795
  shellDir: options.shellDir,
4369
4796
  interaction,
4370
4797
  bus,
4798
+ linkLimit: (artifactId, count) => linkLimit(artifactId, count),
4371
4799
  actions: relay,
4372
4800
  live: { read(page, input) {
4373
4801
  const states = live.read(page, input);
@@ -4490,6 +4918,15 @@ async function openHostedHome(options) {
4490
4918
  const key = await ensureFrame(at.domain, artifactId, route.label);
4491
4919
  if (!gateway.dispatch(key, request, response)) notServed(response);
4492
4920
  };
4921
+ /**
4922
+ * Records that this home holds a version file (DATA_MODEL §7.1's `home_file`), counted once in
4923
+ * `file_ref`, with its size in bytes (the size its push gave, held to the bytes), which metering's
4924
+ * `bytes.held` sums (MT-03). A row recorded before sizes were kept takes it now.
4925
+ */
4926
+ const holdFile = (db, sha, size) => {
4927
+ if (db.run("INSERT OR IGNORE INTO home_file (home_id, sha256, size) VALUES (?, ?, ?)", id, sha, size ?? null).changes > 0) db.run("INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL", sha);
4928
+ else if (size !== void 0) db.run("UPDATE home_file SET size = ? WHERE home_id = ? AND sha256 = ? AND size IS NULL", size, id, sha);
4929
+ };
4493
4930
  /** The client files of a listing this home doesn't hold yet. */
4494
4931
  const missingOf = (files) => directory.read((db) => files.filter((sha) => !db.get("SELECT 1 FROM home_file WHERE home_id = ? AND sha256 = ?", id, sha)));
4495
4932
  /**
@@ -4507,27 +4944,30 @@ async function openHostedHome(options) {
4507
4944
  });
4508
4945
  };
4509
4946
  /**
4510
- * `view/url` on a hosted home under the operator's `limits` (`link`): refused with its words, and
4511
- * refused for now when the operator doesn't answer, since a link creates something (D165 item 9).
4947
+ * New links on a hosted home under the operator's `limits` (`link`): `view/url`'s one, and the
4948
+ * personal links `roster/add` mints (D168); refused with the operator's words, and refused for now
4949
+ * when it doesn't answer, since a link creates something (D165 item 9).
4512
4950
  */
4951
+ const linkLimit = async (artifactId, quantity) => {
4952
+ const services = options.services;
4953
+ if (!services) return;
4954
+ const route = routeOf(artifactId);
4955
+ const verdict = await services.operator.limit({
4956
+ account: services.owner(id).account,
4957
+ home: id,
4958
+ artifact: artifactId,
4959
+ ...route ? { routeId: route.id } : {},
4960
+ operation: "link",
4961
+ ...quantity !== void 0 ? { quantity } : {}
4962
+ });
4963
+ if (verdict.allowed === "unavailable") throw refuse("MOSTRA_REMOTE_OPERATOR_UNAVAILABLE", "The server's operator didn't answer in time; try minting the link again.");
4964
+ if (verdict.allowed === false) throw refuse("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
4965
+ };
4513
4966
  const limitedLinks = (method) => ({
4514
4967
  definition: method.definition,
4515
4968
  async handle(params, session) {
4516
- const services = options.services;
4517
- if (services) {
4518
- const ref = params.artifact;
4519
- const artifactId = registry.get(canonicalRef(ref)).id;
4520
- const route = routeOf(artifactId);
4521
- const verdict = await services.operator.limit({
4522
- account: services.owner(id).account,
4523
- home: id,
4524
- artifact: artifactId,
4525
- ...route ? { routeId: route.id } : {},
4526
- operation: "link"
4527
- });
4528
- if (verdict.allowed === "unavailable") throw refuse("MOSTRA_REMOTE_OPERATOR_UNAVAILABLE", "The server's operator didn't answer in time; try minting the link again.");
4529
- if (verdict.allowed === false) throw refuse("MOSTRA_REMOTE_LIMIT_REACHED", verdict.message);
4530
- }
4969
+ const ref = params.artifact;
4970
+ await linkLimit(registry.get(canonicalRef(ref)).id);
4531
4971
  return method.handle(params, session);
4532
4972
  }
4533
4973
  });
@@ -4622,6 +5062,7 @@ async function openHostedHome(options) {
4622
5062
  home: id,
4623
5063
  artifact: params.artifactId,
4624
5064
  routeId: route.routeId,
5065
+ versionId: version.versionId,
4625
5066
  operation,
4626
5067
  quantity
4627
5068
  });
@@ -4690,7 +5131,7 @@ async function openHostedHome(options) {
4690
5131
  stillClaims: () => false
4691
5132
  });
4692
5133
  directory.write((db) => {
4693
- for (const sha of new Set(clientFiles.map(([, , file]) => file))) if (db.run("INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)", id, sha).changes > 0) db.run("INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL", sha);
5134
+ for (const sha of new Set(clientFiles.map(([, , file]) => file))) holdFile(db, sha, sizes.get(sha));
4694
5135
  });
4695
5136
  log.info("server.version_pushed", {
4696
5137
  home: id,
@@ -4812,6 +5253,13 @@ async function openHostedHome(options) {
4812
5253
  operator: options.services.operator,
4813
5254
  meter: options.services.meter,
4814
5255
  account: () => options.services.owner(id).account,
5256
+ sender: () => {
5257
+ const owner = options.services.owner(id);
5258
+ return {
5259
+ ...owner.name !== void 0 ? { name: owner.name } : {},
5260
+ ...owner.verifiedBy !== void 0 ? { verifiedBy: owner.verifiedBy } : {}
5261
+ };
5262
+ },
4815
5263
  routeOf,
4816
5264
  incarnation: () => incarnation,
4817
5265
  log
@@ -4830,10 +5278,8 @@ async function openHostedHome(options) {
4830
5278
  if (size !== void 0) return size;
4831
5279
  }
4832
5280
  },
4833
- uploaded(sha) {
4834
- directory.write((db) => {
4835
- if (db.run("INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)", id, sha).changes > 0) db.run("INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL", sha);
4836
- });
5281
+ uploaded(sha, size) {
5282
+ directory.write((db) => holdFile(db, sha, size));
4837
5283
  for (const files of pending.values()) files.delete(sha);
4838
5284
  },
4839
5285
  pause: (artifactId, change) => interaction.pause(artifactId, change),
@@ -4843,6 +5289,16 @@ async function openHostedHome(options) {
4843
5289
  },
4844
5290
  revokeLinks,
4845
5291
  revokeLink: (tokenId) => capabilities.revoke(tokenId) !== "unknown",
5292
+ invitationLink(tokenId) {
5293
+ const grant = capabilities.grant(tokenId);
5294
+ if (!grant?.live) return void 0;
5295
+ const url = gateway.reveal(tokenId);
5296
+ if (url === void 0) return void 0;
5297
+ return {
5298
+ url,
5299
+ ...grant.expiresAt !== void 0 ? { expiresAt: grant.expiresAt } : {}
5300
+ };
5301
+ },
4846
5302
  async remove(artifactId) {
4847
5303
  /** The client files a version's listing names. */
4848
5304
  const clientFilesOf = async (versionId) => {
@@ -5181,7 +5637,10 @@ function createRemoteMcp(options) {
5181
5637
  * Metering for an operator (INTERFACES §18.6, D165 item 9): counters kept in memory for a window,
5182
5638
  * then handed to the operator's `metering.record` as one batch with a stable `batchId`, through
5183
5639
  * the durable outbound queue, so a batch is delivered at least once across restarts. With no
5184
- * metering port nothing is counted.
5640
+ * metering port nothing is counted. Besides the sums, windows carry one level, `bytes.held`: what
5641
+ * a home holds now, sent when it changes (and 0 once when it holds nothing any more), and for every
5642
+ * home that holds anything at the first window after a start and once a day after (MT-03). A
5643
+ * window's counters go out in batches of at most {@link MAX_COUNTERS}.
5185
5644
  *
5186
5645
  * Views carry `viewerKey`, a monthly pseudonymous key: an HMAC of the viewer's subject under a key
5187
5646
  * the server makes for each month (`<data>/keys/metering-<YYYY-MM>`), so an operator can count
@@ -5191,6 +5650,10 @@ function createRemoteMcp(options) {
5191
5650
  */
5192
5651
  /** How long one batch's window lasts. */
5193
5652
  const WINDOW_MS = 6e4;
5653
+ /** How often every home that holds anything reports its level again, changed or not. */
5654
+ const SNAPSHOT_MS = 864e5;
5655
+ /** The most counters one batch carries; a window with more sends several batches. */
5656
+ const MAX_COUNTERS = 1e3;
5194
5657
  /** Creates the meter. */
5195
5658
  function createMeter(options) {
5196
5659
  const on = options.operator.has.metering;
@@ -5199,6 +5662,9 @@ function createMeter(options) {
5199
5662
  const counters = /* @__PURE__ */ new Map();
5200
5663
  let from = now();
5201
5664
  const monthKeys = /* @__PURE__ */ new Map();
5665
+ const snapshotMs = options.snapshotMs ?? SNAPSHOT_MS;
5666
+ /** When every level was last sent; never, so the first window after a start sends them all. */
5667
+ let snapshotAt = Number.NEGATIVE_INFINITY;
5202
5668
  const monthKey = () => {
5203
5669
  const month = new Date(now()).toISOString().slice(0, 7);
5204
5670
  let key = monthKeys.get(month);
@@ -5229,25 +5695,36 @@ function createMeter(options) {
5229
5695
  account: active.account,
5230
5696
  home: active.home
5231
5697
  }, seconds);
5232
- if (counters.size === 0) {
5233
- from = to;
5234
- return;
5235
- }
5236
- const batch = {
5237
- batchId: randomUUID(),
5238
- from: new Date(from).toISOString(),
5239
- to: new Date(to).toISOString(),
5240
- counters: [...counters.values()].map(({ counter, value }) => ({
5241
- ...counter,
5242
- value
5243
- }))
5244
- };
5698
+ const full = to - snapshotAt >= snapshotMs;
5699
+ const levels = options.held?.pending(full) ?? [];
5700
+ if (full) snapshotAt = to;
5701
+ const held = levels.filter((level) => level.account !== "" && level.bytes >= 0).map((level) => ({
5702
+ name: "bytes.held",
5703
+ account: level.account,
5704
+ home: level.home,
5705
+ value: level.bytes
5706
+ }));
5707
+ const all = [...[...counters.values()].map(({ counter, value }) => ({
5708
+ ...counter,
5709
+ value
5710
+ })), ...held];
5245
5711
  counters.clear();
5712
+ if (all.length > 0) {
5713
+ const window = {
5714
+ from: new Date(from).toISOString(),
5715
+ to: new Date(to).toISOString()
5716
+ };
5717
+ for (let start = 0; start < all.length; start += MAX_COUNTERS) options.operator.enqueue({
5718
+ port: "metering",
5719
+ batch: {
5720
+ batchId: randomUUID(),
5721
+ ...window,
5722
+ counters: all.slice(start, start + MAX_COUNTERS)
5723
+ }
5724
+ });
5725
+ }
5726
+ if (levels.length > 0) options.held?.reported(levels);
5246
5727
  from = to;
5247
- options.operator.enqueue({
5248
- port: "metering",
5249
- batch
5250
- });
5251
5728
  };
5252
5729
  const add = (name, scope, value = 1) => {
5253
5730
  if (!on || scope.account === "" || value <= 0) return;
@@ -5291,6 +5768,39 @@ function createMeter(options) {
5291
5768
  }
5292
5769
  };
5293
5770
  }
5771
+ /**
5772
+ * The held levels of a server's homes, from its directory (MT-03): each home's running total in
5773
+ * `home.held_bytes`, which `home_file`'s triggers keep in the transaction of every change, and
5774
+ * `held_dirty`, set by them until the level is queued, so a change made just before a stop is
5775
+ * still reported after the restart. A level is marked reported only while it is still the home's.
5776
+ */
5777
+ function heldLevels(directory) {
5778
+ return {
5779
+ pending: (full) => directory.write((db) => {
5780
+ const rows = db.all(`SELECT id AS home, owner_account_id AS account, held_bytes AS bytes,
5781
+ held_reported AS reported
5782
+ FROM home
5783
+ WHERE held_dirty = 1${full ? " OR held_bytes > 0 OR COALESCE(held_reported, 0) > 0" : ""}`);
5784
+ const levels = [];
5785
+ for (const row of rows) {
5786
+ if (!full && row.bytes === (row.reported ?? 0)) {
5787
+ db.run("UPDATE home SET held_dirty = 0 WHERE id = ? AND held_bytes = ?", row.home, row.bytes);
5788
+ continue;
5789
+ }
5790
+ if (row.reported === null && row.bytes === 0) continue;
5791
+ levels.push({
5792
+ home: row.home,
5793
+ account: row.account,
5794
+ bytes: row.bytes
5795
+ });
5796
+ }
5797
+ return levels;
5798
+ }),
5799
+ reported: (levels) => directory.write((db) => {
5800
+ for (const level of levels) db.run("UPDATE home SET held_reported = ?, held_dirty = 0 WHERE id = ? AND held_bytes = ?", level.bytes, level.home, level.bytes);
5801
+ })
5802
+ };
5803
+ }
5294
5804
  //#endregion
5295
5805
  //#region src/server/operator.ts
5296
5806
  /**
@@ -5300,9 +5810,10 @@ function createMeter(options) {
5300
5810
  * schemas; a call that throws, times out or answers what the schema refuses counts as no answer,
5301
5811
  * and each port's caller says what no answer means (`limits`: operations that create fail closed,
5302
5812
  * viewer input proceeds; `routing`: nothing is served; `policy`: nothing is added). Outbound
5303
- * deliveries (`metering`, `events`) are kept in the directory's `outbound` table and retried with
5304
- * backoff until the operator takes them, at least once and idempotent by their IDs, across
5305
- * restarts.
5813
+ * deliveries (`metering`, `events`, `invitations`) are kept in the directory's `outbound` table and
5814
+ * retried with backoff until the operator takes them, at least once and idempotent by their IDs,
5815
+ * across restarts. An invitation is kept by its link's token and handed over with the link built
5816
+ * then, so the queue never holds a capability secret (D168).
5306
5817
  *
5307
5818
  * Without an operator module every port is its no-op default, and this module does nothing but
5308
5819
  * answer "allowed".
@@ -5395,7 +5906,20 @@ async function loadOperator(options) {
5395
5906
  const deliverOne = async (row) => {
5396
5907
  const payload = JSON.parse(row.payload_json);
5397
5908
  try {
5398
- await within(OPERATOR_TIME_LIMITS.outbound, () => row.port === "events" ? ports.events?.deliver(payload) : ports.metering?.record(payload));
5909
+ if (row.port === "invitations") {
5910
+ const { tokenId, ...invitation } = payload;
5911
+ const link = await within(OPERATOR_TIME_LIMITS.outbound, () => options.invitationLink?.(invitation.home, tokenId));
5912
+ if (link === void 0) {
5913
+ directory.write((db) => db.run("DELETE FROM outbound WHERE id = ?", row.id));
5914
+ log.info("operator.invitation_dropped", { invitationId: invitation.invitationId });
5915
+ return;
5916
+ }
5917
+ await within(OPERATOR_TIME_LIMITS.outbound, () => ports.invitations?.send({
5918
+ ...invitation,
5919
+ url: link.url,
5920
+ ...link.expiresAt !== void 0 ? { expiresAt: link.expiresAt } : {}
5921
+ }));
5922
+ } else await within(OPERATOR_TIME_LIMITS.outbound, () => row.port === "events" ? ports.events?.deliver(payload) : ports.metering?.record(payload));
5399
5923
  directory.write((db) => db.run("DELETE FROM outbound WHERE id = ?", row.id));
5400
5924
  } catch (error) {
5401
5925
  const delay = Math.min(OUTBOUND_MAX_BACKOFF_MS, 1e3 * 2 ** Math.min(row.attempts, 20));
@@ -5412,8 +5936,8 @@ async function loadOperator(options) {
5412
5936
  await Promise.resolve();
5413
5937
  try {
5414
5938
  for (;;) {
5415
- const rows = directory.read((db) => ["events", "metering"].flatMap((port) => db.all("SELECT id, port, payload_json, attempts, next_attempt_at FROM outbound WHERE port = ? ORDER BY rowid LIMIT 50", port)));
5416
5939
  const due = new Date(now()).toISOString();
5940
+ const rows = directory.read((db) => [...["events", "metering"].flatMap((port) => db.all("SELECT id, port, payload_json, attempts, next_attempt_at FROM outbound WHERE port = ? ORDER BY rowid LIMIT 50", port)), ...db.all("SELECT id, port, payload_json, attempts, next_attempt_at FROM outbound WHERE port = 'invitations' AND next_attempt_at <= ? ORDER BY rowid LIMIT 50", due)]);
5417
5941
  if (rows.every((row) => row.next_attempt_at > due)) return;
5418
5942
  const blocked = /* @__PURE__ */ new Set();
5419
5943
  let delivered = 0;
@@ -5426,8 +5950,9 @@ async function loadOperator(options) {
5426
5950
  const before = row.attempts;
5427
5951
  await deliverOne(row);
5428
5952
  const still = directory.read((db) => db.get("SELECT attempts FROM outbound WHERE id = ?", row.id));
5429
- if (still && still.attempts > before) blocked.add(row.port);
5430
- else delivered += 1;
5953
+ if (still && still.attempts > before) {
5954
+ if (row.port !== "invitations") blocked.add(row.port);
5955
+ } else delivered += 1;
5431
5956
  }
5432
5957
  if (delivered === 0) return;
5433
5958
  }
@@ -5437,7 +5962,7 @@ async function loadOperator(options) {
5437
5962
  })();
5438
5963
  return dispatching;
5439
5964
  };
5440
- const timer = ports.events || ports.metering ? setInterval(() => void dispatch(), options.dispatchMs ?? 1e3) : void 0;
5965
+ const timer = ports.events || ports.metering || ports.invitations ? setInterval(() => void dispatch(), options.dispatchMs ?? 1e3) : void 0;
5441
5966
  timer?.unref();
5442
5967
  const has = {
5443
5968
  limits: typeof ports.limits?.check === "function",
@@ -5451,7 +5976,8 @@ async function loadOperator(options) {
5451
5976
  routing: typeof ports.routing?.resolve === "function",
5452
5977
  linkHost: typeof ports.routing?.linkHost === "function",
5453
5978
  events: typeof ports.events?.deliver === "function",
5454
- identity: typeof ports.identity?.providers === "function"
5979
+ identity: typeof ports.identity?.providers === "function",
5980
+ invitations: typeof ports.invitations?.send === "function"
5455
5981
  };
5456
5982
  return {
5457
5983
  has,
@@ -5549,9 +6075,9 @@ async function loadOperator(options) {
5549
6075
  });
5550
6076
  },
5551
6077
  enqueue(item) {
5552
- if (item.port === "events" ? !has.events : !has.metering) return;
5553
- const id = item.port === "events" ? item.event.eventId : item.batch.batchId;
5554
- const payload = item.port === "events" ? item.event : item.batch;
6078
+ if (!has[item.port]) return;
6079
+ const id = item.port === "events" ? item.event.eventId : item.port === "metering" ? item.batch.batchId : item.invitation.invitationId;
6080
+ const payload = item.port === "events" ? item.event : item.port === "metering" ? item.batch : item.invitation;
5555
6081
  directory.write((db) => db.run("INSERT OR IGNORE INTO outbound (id, port, payload_json, attempts, next_attempt_at) VALUES (?, ?, ?, 0, ?)", `${item.port}:${id}`, item.port, JSON.stringify(payload), new Date(now()).toISOString()));
5556
6082
  dispatch();
5557
6083
  },
@@ -5570,9 +6096,10 @@ async function loadOperator(options) {
5570
6096
  *
5571
6097
  * - `oidc`: any OpenID Connect provider, by its issuer's discovery document; the authorization
5572
6098
  * code flow with PKCE (S256) and a nonce, and the ID token checked (signature, issuer, audience,
5573
- * expiry, nonce) by `openid-client`.
6099
+ * expiry, nonce) by `openid-client`; the claims its entry names (`claims`) go into `facts`.
5574
6100
  * - `github`: GitHub's OAuth web flow with PKCE, then `GET /user` for the account's stable ID, its
5575
- * login and name, and its age (`facts.createdAt`) for an operator's policy.
6101
+ * login and name, and for an operator's policy its age (`facts.createdAt`) and public counts
6102
+ * (`facts.publicRepos`, `publicGists`, `followers`).
5576
6103
  * - `development`: a page that asks for a name and trusts it, for tests and a self-hoster's first
5577
6104
  * run; its names are never shown as verified.
5578
6105
  *
@@ -5587,17 +6114,22 @@ const GITHUB = {
5587
6114
  token: "https://github.com/login/oauth/access_token",
5588
6115
  user: "https://api.github.com/user"
5589
6116
  };
5590
- function invalid$1(message) {
6117
+ /** The `verifies` of a configured provider: the label its verified names carry, if any. */
6118
+ function verifiesOf(config) {
6119
+ const label = configuredVerifier(config);
6120
+ return label === void 0 ? {} : { verifies: { label } };
6121
+ }
6122
+ function invalid(message) {
5591
6123
  return new RpcError(createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", { message }));
5592
6124
  }
5593
6125
  function secretOf(config) {
5594
- if (config.clientSecretFile === void 0) throw invalid$1(`The identity provider \`${config.id}\` needs \`clientSecretFile\`, a file holding its client secret.`);
6126
+ if (config.clientSecretFile === void 0) throw invalid(`The identity provider \`${config.id}\` needs \`clientSecretFile\`, a file holding its client secret.`);
5595
6127
  try {
5596
6128
  const secret = readFileSync(config.clientSecretFile, "utf8").trim();
5597
6129
  if (secret === "") throw new Error("empty");
5598
6130
  return secret;
5599
6131
  } catch {
5600
- throw invalid$1(`The identity provider \`${config.id}\`'s \`clientSecretFile\` (${config.clientSecretFile}) can't be read, or is empty.`);
6132
+ throw invalid(`The identity provider \`${config.id}\`'s \`clientSecretFile\` (${config.clientSecretFile}) can't be read, or is empty.`);
5601
6133
  }
5602
6134
  }
5603
6135
  /** A fetch bounded in time, for a provider's own endpoints. */
@@ -5607,13 +6139,37 @@ async function timedFetch(url, init) {
5607
6139
  signal: AbortSignal.timeout(PROVIDER_TIMEOUT_MS)
5608
6140
  });
5609
6141
  }
6142
+ /** The longest a chosen claim's value may be, as JSON: a policy reads names, not documents. */
6143
+ const CLAIM_MAX = 4096;
6144
+ /**
6145
+ * The ID token's claims a configuration chose (`claims`), as `facts` under their own names: each
6146
+ * one present whose value is JSON of at most 4 KiB (MT-03). A claim the token doesn't carry is left
6147
+ * out; with none chosen, nothing is added.
6148
+ */
6149
+ function chosen(claims, names) {
6150
+ if (names === void 0) return void 0;
6151
+ const facts = {};
6152
+ for (const name of names) {
6153
+ if (!Object.hasOwn(claims, name)) continue;
6154
+ const value = claims[name];
6155
+ const text = JSON.stringify(value);
6156
+ if (text === void 0 || text.length > CLAIM_MAX) continue;
6157
+ Object.defineProperty(facts, name, {
6158
+ value: JSON.parse(text),
6159
+ enumerable: true,
6160
+ writable: true,
6161
+ configurable: true
6162
+ });
6163
+ }
6164
+ return Object.keys(facts).length > 0 ? { facts } : void 0;
6165
+ }
5610
6166
  /** The OpenID Connect adapter. Discovery happens at the first sign-in and is kept. */
5611
6167
  function oidcProvider(config, env) {
5612
- if (config.issuer === void 0 || config.clientId === void 0) throw invalid$1(`The identity provider \`${config.id}\` (oidc) needs \`issuer\` and \`clientId\`.`);
6168
+ if (config.issuer === void 0 || config.clientId === void 0) throw invalid(`The identity provider \`${config.id}\` (oidc) needs \`issuer\` and \`clientId\`.`);
5613
6169
  const secret = secretOf(config);
5614
6170
  const issuer = new URL(config.issuer);
5615
6171
  const insecure = issuer.protocol === "http:" && env["MOSTRA_TEST_OIDC_INSECURE"] === "1" && (issuer.hostname === "127.0.0.1" || issuer.hostname === "localhost");
5616
- if (issuer.protocol !== "https:" && !insecure) throw invalid$1(`The identity provider \`${config.id}\`'s \`issuer\` must be an https: URL.`);
6172
+ if (issuer.protocol !== "https:" && !insecure) throw invalid(`The identity provider \`${config.id}\`'s \`issuer\` must be an https: URL.`);
5617
6173
  let discovered;
5618
6174
  const configuration = () => {
5619
6175
  discovered ??= oidc.discovery(issuer, config.clientId, void 0, oidc.ClientSecretPost(secret), insecure ? {
@@ -5629,7 +6185,7 @@ function oidcProvider(config, env) {
5629
6185
  id: config.id,
5630
6186
  label: config.label ?? config.id,
5631
6187
  kind: "oidc",
5632
- verifies: true,
6188
+ ...verifiesOf(config),
5633
6189
  async authorize(state, pkce, context) {
5634
6190
  return oidc.buildAuthorizationUrl(await configuration(), {
5635
6191
  redirect_uri: context.redirectUri,
@@ -5656,14 +6212,19 @@ function oidcProvider(config, env) {
5656
6212
  subject: claims.sub,
5657
6213
  ...typeof claims["email"] === "string" ? { email: claims["email"] } : {},
5658
6214
  ...typeof claims["email_verified"] === "boolean" ? { emailVerified: claims["email_verified"] } : {},
5659
- ...name !== void 0 ? { name } : {}
6215
+ ...name !== void 0 ? { name } : {},
6216
+ ...chosen(claims, config.claims) ?? {}
5660
6217
  };
5661
6218
  }
5662
6219
  };
5663
6220
  }
6221
+ /** A count GitHub gave, under its name in `facts`: a non-negative integer, or nothing. */
6222
+ function count(name, value) {
6223
+ return Number.isSafeInteger(value) && value >= 0 ? { [name]: value } : {};
6224
+ }
5664
6225
  /** The GitHub adapter: an OAuth app's web flow with PKCE (VERIFIED_FACTS, WP-30). */
5665
6226
  function githubProvider(config, env) {
5666
- if (config.clientId === void 0) throw invalid$1(`The identity provider \`${config.id}\` (github) needs \`clientId\`.`);
6227
+ if (config.clientId === void 0) throw invalid(`The identity provider \`${config.id}\` (github) needs \`clientId\`.`);
5667
6228
  const secret = secretOf(config);
5668
6229
  const base = env["MOSTRA_TEST_GITHUB_BASE"];
5669
6230
  const endpoints = base !== void 0 && /^http:\/\/(127\.0\.0\.1|localhost):\d+$/.test(base) ? {
@@ -5675,7 +6236,7 @@ function githubProvider(config, env) {
5675
6236
  id: config.id,
5676
6237
  label: config.label ?? "GitHub",
5677
6238
  kind: "github",
5678
- verifies: true,
6239
+ ...verifiesOf(config),
5679
6240
  authorize(state, pkce, context) {
5680
6241
  const url = new URL(endpoints.authorize);
5681
6242
  url.searchParams.set("client_id", config.clientId);
@@ -5721,7 +6282,10 @@ function githubProvider(config, env) {
5721
6282
  name: found.name ?? found.login ?? String(found.id),
5722
6283
  facts: {
5723
6284
  login: found.login ?? null,
5724
- ...found.created_at !== void 0 ? { createdAt: found.created_at } : {}
6285
+ ...found.created_at !== void 0 ? { createdAt: found.created_at } : {},
6286
+ ...count("publicRepos", found.public_repos),
6287
+ ...count("publicGists", found.public_gists),
6288
+ ...count("followers", found.followers)
5725
6289
  }
5726
6290
  };
5727
6291
  }
@@ -5736,7 +6300,6 @@ function developmentProvider(config, apiOrigin) {
5736
6300
  id: config.id,
5737
6301
  label: config.label ?? "Development sign-in",
5738
6302
  kind: "development",
5739
- verifies: false,
5740
6303
  authorize(state) {
5741
6304
  const url = new URL(`${apiOrigin}/signin/${config.id}/name`);
5742
6305
  url.searchParams.set("state", state);
@@ -5757,7 +6320,7 @@ function developmentProvider(config, apiOrigin) {
5757
6320
  function builtInProviders(configs, options) {
5758
6321
  const seen = /* @__PURE__ */ new Set();
5759
6322
  return configs.map((config) => {
5760
- if (seen.has(config.id)) throw invalid$1(`Two identity providers are named \`${config.id}\`.`);
6323
+ if (seen.has(config.id)) throw invalid(`Two identity providers are named \`${config.id}\`.`);
5761
6324
  seen.add(config.id);
5762
6325
  const provider = config.kind === "oidc" ? oidcProvider(config, options.env) : config.kind === "github" ? githubProvider(config, options.env) : developmentProvider(config, options.apiOrigin);
5763
6326
  options.log.info("signin.provider", {
@@ -5926,137 +6489,6 @@ function createRouting(options) {
5926
6489
  };
5927
6490
  }
5928
6491
  //#endregion
5929
- //#region src/server/config.ts
5930
- /**
5931
- * The remote server's configuration (INTERFACES §18.1): a JSON file (`--config`) and
5932
- * `MOSTRA_SERVER_<KEY>` variables, read before the server serves anything. A configuration it
5933
- * can't use stops it, naming the key (`MOSTRA_REMOTE_CONFIG_INVALID`). Domains are configuration,
5934
- * never code (D137, D165 item 0a): the dossier and this code name only `example` hosts.
5935
- */
5936
- /** A rate: `count` per `per` (a duration such as `1m`, as links' lifetimes are written). */
5937
- const RateSchema = z.object({
5938
- count: z.int().min(1),
5939
- per: Duration
5940
- });
5941
- /** A host name a link can use: lower-case labels of letters, digits and hyphens. */
5942
- const HostName = z.string().regex(/^(?=.{1,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)(\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*(:\d{1,5})?$/, { message: "must be a lower-case host name, with a port only for a local test" });
5943
- /**
5944
- * An origin the server is reached at: `https:`, or `http:` for loopback and `*.localhost` (tests
5945
- * and a self-hoster's first run).
5946
- */
5947
- const Origin = z.string().refine((value) => {
5948
- try {
5949
- const url = new URL(value);
5950
- if (url.origin !== value) return false;
5951
- if (url.protocol === "https:") return true;
5952
- return url.protocol === "http:" && (url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname.endsWith(".localhost") || url.hostname === "[::1]");
5953
- } catch {
5954
- return false;
5955
- }
5956
- }, { message: "must be an https: origin (http: only on loopback), with no path" });
5957
- /** The schema of {@link RemoteServerConfig}. */
5958
- const Config = z.strictObject({
5959
- apiOrigin: Origin,
5960
- domains: z.strictObject({
5961
- active: HostName,
5962
- spares: z.array(HostName).optional()
5963
- }),
5964
- /**
5965
- * The scheme the user-content domain is served on: `https` behind a TLS terminator, `http`
5966
- * for a loopback test. Default `https`.
5967
- */
5968
- scheme: z.enum(["https", "http"]).optional(),
5969
- trustedProxies: z.array(z.string()).optional(),
5970
- identityProviders: z.array(z.strictObject({
5971
- id: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/),
5972
- kind: z.enum([
5973
- "development",
5974
- "oidc",
5975
- "github"
5976
- ]),
5977
- label: z.string().min(1).optional(),
5978
- issuer: z.string().optional(),
5979
- clientId: z.string().optional(),
5980
- clientSecretFile: z.string().optional()
5981
- })).optional(),
5982
- rateLimits: z.strictObject({
5983
- perIp: z.strictObject({
5984
- submissions: RateSchema.optional(),
5985
- requests: RateSchema.optional()
5986
- }).optional(),
5987
- perLink: z.strictObject({
5988
- submissions: RateSchema.optional(),
5989
- relayedRuns: RateSchema.optional()
5990
- }).optional(),
5991
- ipv6Prefix: z.int().min(1).max(128).optional()
5992
- }).optional(),
5993
- ledger: z.strictObject({ walAutocheckpoint: z.int().min(0).optional() }).optional(),
5994
- relay: z.strictObject({
5995
- waitingRunsPerArtifact: z.int().min(1).optional(),
5996
- drainPerSecond: z.number().positive().optional()
5997
- }).optional(),
5998
- homes: z.strictObject({ idleCloseMinutes: z.number().positive().optional() }).optional(),
5999
- framing: z.strictObject({ frameAncestors: z.literal("'none'") }).optional(),
6000
- operator: z.string().optional(),
6001
- chatHosts: z.record(z.string(), z.unknown()).optional()
6002
- });
6003
- /** The configuration keys, and the variable each also comes from. */
6004
- const KEYS = Object.keys(Config.shape);
6005
- /** `apiOrigin` → `MOSTRA_SERVER_API_ORIGIN`. */
6006
- function variableOf(key) {
6007
- return `MOSTRA_SERVER_${key.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase()}`;
6008
- }
6009
- function invalid(message) {
6010
- return new RpcError(createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", { message }));
6011
- }
6012
- /**
6013
- * Reads the configuration: the file, then each `MOSTRA_SERVER_<KEY>` (a string for a string key,
6014
- * JSON for the others) over it. Throws `MOSTRA_REMOTE_CONFIG_INVALID` naming the first key it
6015
- * can't use.
6016
- */
6017
- function readServerConfig(file, env) {
6018
- let raw = {};
6019
- if (file !== void 0) {
6020
- let text;
6021
- try {
6022
- text = readFileSync(file, "utf8");
6023
- } catch (error) {
6024
- throw invalid(`The configuration file ${file} can't be read (${error.code ?? "error"}).`);
6025
- }
6026
- try {
6027
- const parsed = JSON.parse(text);
6028
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("not an object");
6029
- raw = parsed;
6030
- } catch {
6031
- throw invalid(`The configuration file ${file} isn't a JSON object.`);
6032
- }
6033
- }
6034
- for (const key of KEYS) {
6035
- const variable = variableOf(key);
6036
- const value = env[variable];
6037
- if (value === void 0 || value === "") continue;
6038
- if (key === "apiOrigin" || key === "operator" || key === "scheme") raw[key] = value;
6039
- else try {
6040
- raw[key] = JSON.parse(value);
6041
- } catch {
6042
- throw invalid(`${variable} isn't JSON.`);
6043
- }
6044
- }
6045
- const checked = Config.safeParse(raw);
6046
- if (!checked.success) {
6047
- const issue = checked.error.issues[0];
6048
- throw invalid(`The configuration key \`${issue?.path.join(".") || "(configuration)"}\` can't be used: ${issue?.message ?? "invalid"}.`);
6049
- }
6050
- const config = checked.data;
6051
- const domains = [config.domains.active, ...config.domains.spares ?? []];
6052
- const api = new URL(config.apiOrigin).host;
6053
- if (domains.some((domain) => domain === api || api.endsWith(`.${domain}`))) throw invalid("The configuration key `apiOrigin` can't be on a user-content domain: sign-in and the binding live apart from tenant pages (D165 item 7).");
6054
- const proxy = validProxies(config.trustedProxies ?? []);
6055
- if (proxy !== void 0) throw invalid(`The configuration key \`trustedProxies\` names ${JSON.stringify(proxy)}, which is neither an address nor a CIDR block.`);
6056
- if (new Set(domains).size !== domains.length) throw invalid("The configuration key `domains` names one domain twice.");
6057
- return config;
6058
- }
6059
- //#endregion
6060
6492
  //#region src/server/index.ts
6061
6493
  /**
6062
6494
  * The remote server (ARCHITECTURE §15, INTERFACES §18, D165): one process holding many hosted
@@ -6122,18 +6554,23 @@ async function startServer(options) {
6122
6554
  * health check is never counted. Without the setting there is no limit.
6123
6555
  */
6124
6556
  const perIpRequests = rateOf(config.rateLimits?.perIp?.requests);
6125
- /** Who owns a home, as its pages' sender line names them (D161). */
6557
+ /**
6558
+ * Who owns a home, as its pages' sender line names them (D161): the account's name, and the
6559
+ * verifier its first identity recorded when its provider named the person (MT-03), never the
6560
+ * configuration now, so a provider renamed or retired since changes no sender line. A name the
6561
+ * development provider took on trust, or one no provider verified, carries none.
6562
+ */
6126
6563
  const ownerOf = (homeId) => {
6127
- const row = data.directory.read((db) => db.get(`SELECT a.id AS account, a.name, (SELECT i.provider FROM identity i WHERE i.account_id = a.id
6128
- ORDER BY i.linked_at LIMIT 1) AS provider
6129
- FROM home h JOIN account a ON a.id = h.owner_account_id WHERE h.id = ?`, homeId));
6564
+ const row = data.directory.read((db) => db.get(`SELECT a.id AS account, a.name, i.verified_by
6565
+ FROM home h JOIN account a ON a.id = h.owner_account_id
6566
+ LEFT JOIN identity i ON i.rowid = (SELECT rowid FROM identity WHERE account_id = a.id
6567
+ ORDER BY linked_at, rowid LIMIT 1)
6568
+ WHERE h.id = ?`, homeId));
6130
6569
  if (!row) return { account: "" };
6131
- const provider = config.identityProviders?.find((candidate) => candidate.id === row.provider);
6132
- const verifiedBy = provider && provider.kind !== "development" ? provider.label ?? (provider.kind === "github" ? "GitHub" : provider.id) : void 0;
6133
6570
  return {
6134
6571
  account: row.account,
6135
6572
  ...row.name !== null ? { name: row.name } : {},
6136
- ...verifiedBy !== void 0 ? { verifiedBy } : {}
6573
+ ...row.verified_by !== null ? { verifiedBy: row.verified_by } : {}
6137
6574
  };
6138
6575
  };
6139
6576
  const services = () => ({
@@ -6178,6 +6615,7 @@ async function startServer(options) {
6178
6615
  },
6179
6616
  shellDir,
6180
6617
  services: services(),
6618
+ ...Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) > 0 ? { sessionTouchMs: Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) } : {},
6181
6619
  relay: {
6182
6620
  waitingRunsPerArtifact: config.relay?.waitingRunsPerArtifact,
6183
6621
  drainPerSecond: config.relay?.drainPerSecond,
@@ -6210,12 +6648,17 @@ async function startServer(options) {
6210
6648
  handle,
6211
6649
  directory: data.directory,
6212
6650
  log,
6651
+ invitationLink: async (homeId, tokenId) => {
6652
+ if (data.directory.read((db) => db.get("SELECT 1 FROM home WHERE id = ?", homeId)) === void 0) return void 0;
6653
+ return (await data.home(homeId)).assembled.invitationLink(tokenId);
6654
+ },
6213
6655
  ...Number(options.env["MOSTRA_TEST_OPERATOR_DISPATCH_MS"]) > 0 ? { dispatchMs: Number(options.env["MOSTRA_TEST_OPERATOR_DISPATCH_MS"]) } : {}
6214
6656
  });
6215
6657
  } catch (error) {
6216
6658
  await data.close();
6217
6659
  throw error;
6218
6660
  }
6661
+ fillHeldSizes(data.directory, join(options.data, "files"), log);
6219
6662
  routing = createRouting({
6220
6663
  apiHost: new URL(config.apiOrigin).host,
6221
6664
  domains: () => [config.domains.active, ...config.domains.spares ?? []],
@@ -6232,7 +6675,9 @@ async function startServer(options) {
6232
6675
  activeHomes: () => data.open().map((home) => ({
6233
6676
  home,
6234
6677
  account: ownerOf(home).account
6235
- }))
6678
+ })),
6679
+ held: heldLevels(data.directory),
6680
+ ...Number(options.env["MOSTRA_TEST_HELD_SNAPSHOT_MS"]) > 0 ? { snapshotMs: Number(options.env["MOSTRA_TEST_HELD_SNAPSHOT_MS"]) } : {}
6236
6681
  });
6237
6682
  auth = createAuthorization({
6238
6683
  directory: data.directory,
@@ -6533,6 +6978,33 @@ async function startServer(options) {
6533
6978
  }
6534
6979
  };
6535
6980
  }
6981
+ /**
6982
+ * Gives each `home_file` row recorded before sizes were kept (directory migration 6) the size of
6983
+ * its stored file, so `bytes.held` counts it (MT-03); the triggers add it to the home's total. A
6984
+ * file that is gone holds nothing on disk, so its row takes 0 and is never read again.
6985
+ */
6986
+ function fillHeldSizes(directory, filesDir, log) {
6987
+ const rows = directory.read((db) => db.all("SELECT home_id, sha256 FROM home_file WHERE size IS NULL"));
6988
+ if (rows.length === 0) return;
6989
+ const sizes = /* @__PURE__ */ new Map();
6990
+ let missing = 0;
6991
+ for (const row of rows) {
6992
+ if (sizes.has(row.sha256)) continue;
6993
+ try {
6994
+ sizes.set(row.sha256, statSync(join(filesDir, row.sha256)).size);
6995
+ } catch {
6996
+ sizes.set(row.sha256, 0);
6997
+ missing += 1;
6998
+ }
6999
+ }
7000
+ directory.write((db) => {
7001
+ for (const row of rows) db.run("UPDATE home_file SET size = ? WHERE home_id = ? AND sha256 = ? AND size IS NULL", sizes.get(row.sha256) ?? 0, row.home_id, row.sha256);
7002
+ });
7003
+ log.info("server.held_sizes_filled", {
7004
+ files: sizes.size,
7005
+ missing
7006
+ });
7007
+ }
6536
7008
  //#endregion
6537
7009
  //#region src/bin.ts
6538
7010
  /**