@mostra/runtime 0.12.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.
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-CBUlOmzG.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") {
@@ -2143,190 +2513,14 @@ function createAuthorize(options) {
2143
2513
  if (!step || flow === void 0 || hashSecret(flow) !== step.flow || !spend(handle)) {
2144
2514
  problem(response, 400, "This sign-in step expired, was used, or belongs to another browser.");
2145
2515
  return true;
2146
- }
2147
- await resolved(response, step.identity, step.steps, step.flow, step.next);
2148
- return true;
2149
- }
2150
- return false;
2151
- }
2152
- };
2153
- }
2154
- //#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);
2516
+ }
2517
+ await resolved(response, step.identity, step.verifiedBy, step.steps, step.flow, step.next);
2518
+ return true;
2519
+ }
2520
+ return false;
2314
2521
  }
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
- };
2522
+ };
2523
+ }
2330
2524
  //#endregion
2331
2525
  //#region src/server/directory.ts
2332
2526
  /** The server directory's migrations: their own list, apart from a home's. */
@@ -2538,6 +2732,43 @@ const SERVER_MIGRATIONS = [
2538
2732
  SELECT rowid, id, port, payload_json, attempts, next_attempt_at FROM outbound;
2539
2733
  DROP TABLE outbound;
2540
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;
2541
2772
  `);
2542
2773
  }
2543
2774
  }
@@ -3174,14 +3405,26 @@ function hostedPages(options) {
3174
3405
  ...tokenId !== void 0 ? { tokenId } : {}
3175
3406
  });
3176
3407
  };
3177
- /** A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9). */
3178
- 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) => {
3179
3419
  if (policy.maxTtl === void 0 && policy.idleExpiry === void 0) return void 0;
3180
- 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
3181
- 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));
3182
3424
  if (!used) return void 0;
3183
3425
  const minted = Date.parse(used.created_at);
3184
- 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));
3185
3428
  const tooOld = policy.maxTtl !== void 0 && now() - minted > durationMs$1(policy.maxTtl);
3186
3429
  const idle = policy.idleExpiry !== void 0 && now() - lastUse > durationMs$1(policy.idleExpiry);
3187
3430
  if (!tooOld && !idle) return void 0;
@@ -3329,7 +3572,7 @@ function hostedPages(options) {
3329
3572
  async admitView(input) {
3330
3573
  if (quarantineOf(input.artifactId, input.versionId) !== void 0) return underReview();
3331
3574
  const policy = await policyFor(input.artifactId, input.tokenId);
3332
- const gone = expired(input.tokenId, policy);
3575
+ const gone = expired(input.tokenId, input.sessionId, policy);
3333
3576
  if (gone) return gone;
3334
3577
  const route = options.routeOf(input.artifactId);
3335
3578
  const verdict = await operator.limit({
@@ -3500,16 +3743,18 @@ function createHostedRelay(options) {
3500
3743
  /** Each link's recent relayed runs and calls, for the per-link limit. */
3501
3744
  const linkUses = /* @__PURE__ */ new Map();
3502
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
+ /**
3503
3751
  * Counts a relayed run or call against its link (D159), refusing it past the limit with
3504
- * `MOSTRA_REMOTE_RATE_LIMITED` and when to try again. A request that joins a run already
3505
- * 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).
3506
3754
  */
3507
- const limitLink = (tokenId, artifactId, input) => {
3755
+ const limitLink = (tokenId) => {
3508
3756
  const limit = options.perLink;
3509
- if (!limit || tokenId === void 0) return;
3510
- if (artifactId !== void 0 && input !== void 0) {
3511
- 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;
3512
- }
3757
+ if (!limit || tokenId === void 0) return () => {};
3513
3758
  const at = now();
3514
3759
  const recent = (linkUses.get(tokenId) ?? []).filter((used) => at - used < limit.perMs);
3515
3760
  if (recent.length >= limit.count) {
@@ -3521,7 +3766,50 @@ function createHostedRelay(options) {
3521
3766
  }
3522
3767
  recent.push(at);
3523
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.");
3524
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}`] : []];
3525
3813
  const checkArgs = (args) => {
3526
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.");
3527
3815
  try {
@@ -3938,15 +4226,86 @@ function createHostedRelay(options) {
3938
4226
  ownerUnreachable: true
3939
4227
  };
3940
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
+ };
3941
4288
  return {
3942
4289
  async call(page, input, signal) {
3943
4290
  checkArgs(input.args);
3944
4291
  const versionId = interaction.viewVersion(page, input.viewId);
3945
4292
  mayTrigger(page.viewerId, functionOf(versionId, input.fnId));
3946
- limitLink(page.tokenId);
3947
- const owner = options.ownerOf(page.artifactId);
3948
- const session = owner === void 0 ? void 0 : sessionFor(owner);
3949
- 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
+ }
3950
4309
  const callId = `call_${crypto.randomUUID()}`;
3951
4310
  const pending = {
3952
4311
  session,
@@ -3993,44 +4352,20 @@ function createHostedRelay(options) {
3993
4352
  };
3994
4353
  return lines();
3995
4354
  },
3996
- startRun(page, input) {
3997
- checkArgs(input.args);
3998
- const versionId = interaction.viewVersion(page, input.viewId);
3999
- const version = functionOf(versionId, input.fnId);
4000
- mayTrigger(page.viewerId, version);
4001
- limitLink(page.tokenId, page.artifactId, input);
4002
- const owner = options.ownerOf(page.artifactId);
4003
- if (owner === void 0) throw failed("MOSTRA_REMOTE_OWNER_OFFLINE", "This page has no owner instance to run it.");
4004
- const connected = sessionFor(owner) !== void 0;
4005
- if (!connected && version.policy === "queue") {
4006
- const waiting = ledger.read((db) => db.get("SELECT COUNT(*) AS n FROM action_run WHERE artifact_id = ? AND status = 'waiting'", page.artifactId)?.n ?? 0);
4007
- 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);
4008
- 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.");
4009
- }
4010
- const { state, row, started } = interaction.startRun(page, input);
4011
- bus.attachRuns(page.artifactId, input.viewId, [state.runId]);
4012
- if (!started) return decorate(state, page.artifactId);
4013
- const recorded = Date.parse(row.started_at);
4014
- const waitingState = ledger.write((db) => {
4015
- const seq = (db.get("SELECT MAX(relay_seq) AS high FROM relay_run WHERE owner_link_id = ?", owner)?.high ?? 0) + 1;
4016
- const floor = Math.max(marks.get(owner) ?? 0, db.get("SELECT floor FROM relay_floor WHERE link_id = ?", owner)?.floor ?? 0);
4017
- 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));
4018
- if (connected) return void 0;
4019
- markWaiting(db, row.id);
4020
- const after = runRow(db, row.id);
4021
- return after ? runState(db, after) : void 0;
4022
- });
4023
- if (connected) {
4024
- const session = sessionFor(owner);
4025
- if (session !== void 0) send(row.id, session);
4026
- 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);
4027
4361
  }
4028
- if (version.policy === "fail") {
4029
- endOffline(row.id);
4030
- 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);
4031
4368
  }
4032
- if (waitingState) publish(waitingState, page.artifactId);
4033
- return waitingState ?? state;
4034
4369
  },
4035
4370
  attach(page, input) {
4036
4371
  const states = interaction.runsFor(page, input);
@@ -4384,6 +4719,7 @@ async function openHostedHome(options) {
4384
4719
  now: () => Date.now(),
4385
4720
  shellOrigin: () => originOf(options.domains.active),
4386
4721
  servesShell,
4722
+ ...options.sessionTouchMs !== void 0 ? { touchIntervalMs: options.sessionTouchMs } : {},
4387
4723
  onMinted: ({ tokenId, artifactId }) => {
4388
4724
  const route = routeOf(artifactId);
4389
4725
  if (!route) return;
@@ -4431,6 +4767,18 @@ async function openHostedHome(options) {
4431
4767
  ...options.relay?.sweepMs !== void 0 ? { sweepMs: options.relay.sweepMs } : {},
4432
4768
  ...options.relay?.liveRenewMs !== void 0 ? { liveRenewMs: options.relay.liveRenewMs } : {},
4433
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
+ } } : {},
4434
4782
  latestVersion: (artifactId) => ledger.read((db) => db.get("SELECT latest_version_id FROM artifact WHERE id = ?", artifactId)?.latest_version_id ?? void 0),
4435
4783
  storeLive: (artifactId, versionId, produced) => live.relayed(artifactId, versionId, produced)
4436
4784
  });
@@ -4570,6 +4918,15 @@ async function openHostedHome(options) {
4570
4918
  const key = await ensureFrame(at.domain, artifactId, route.label);
4571
4919
  if (!gateway.dispatch(key, request, response)) notServed(response);
4572
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
+ };
4573
4930
  /** The client files of a listing this home doesn't hold yet. */
4574
4931
  const missingOf = (files) => directory.read((db) => files.filter((sha) => !db.get("SELECT 1 FROM home_file WHERE home_id = ? AND sha256 = ?", id, sha)));
4575
4932
  /**
@@ -4705,6 +5062,7 @@ async function openHostedHome(options) {
4705
5062
  home: id,
4706
5063
  artifact: params.artifactId,
4707
5064
  routeId: route.routeId,
5065
+ versionId: version.versionId,
4708
5066
  operation,
4709
5067
  quantity
4710
5068
  });
@@ -4773,7 +5131,7 @@ async function openHostedHome(options) {
4773
5131
  stillClaims: () => false
4774
5132
  });
4775
5133
  directory.write((db) => {
4776
- 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));
4777
5135
  });
4778
5136
  log.info("server.version_pushed", {
4779
5137
  home: id,
@@ -4920,10 +5278,8 @@ async function openHostedHome(options) {
4920
5278
  if (size !== void 0) return size;
4921
5279
  }
4922
5280
  },
4923
- uploaded(sha) {
4924
- directory.write((db) => {
4925
- 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);
4926
- });
5281
+ uploaded(sha, size) {
5282
+ directory.write((db) => holdFile(db, sha, size));
4927
5283
  for (const files of pending.values()) files.delete(sha);
4928
5284
  },
4929
5285
  pause: (artifactId, change) => interaction.pause(artifactId, change),
@@ -5281,7 +5637,10 @@ function createRemoteMcp(options) {
5281
5637
  * Metering for an operator (INTERFACES §18.6, D165 item 9): counters kept in memory for a window,
5282
5638
  * then handed to the operator's `metering.record` as one batch with a stable `batchId`, through
5283
5639
  * the durable outbound queue, so a batch is delivered at least once across restarts. With no
5284
- * 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}.
5285
5644
  *
5286
5645
  * Views carry `viewerKey`, a monthly pseudonymous key: an HMAC of the viewer's subject under a key
5287
5646
  * the server makes for each month (`<data>/keys/metering-<YYYY-MM>`), so an operator can count
@@ -5291,6 +5650,10 @@ function createRemoteMcp(options) {
5291
5650
  */
5292
5651
  /** How long one batch's window lasts. */
5293
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;
5294
5657
  /** Creates the meter. */
5295
5658
  function createMeter(options) {
5296
5659
  const on = options.operator.has.metering;
@@ -5299,6 +5662,9 @@ function createMeter(options) {
5299
5662
  const counters = /* @__PURE__ */ new Map();
5300
5663
  let from = now();
5301
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;
5302
5668
  const monthKey = () => {
5303
5669
  const month = new Date(now()).toISOString().slice(0, 7);
5304
5670
  let key = monthKeys.get(month);
@@ -5329,25 +5695,36 @@ function createMeter(options) {
5329
5695
  account: active.account,
5330
5696
  home: active.home
5331
5697
  }, seconds);
5332
- if (counters.size === 0) {
5333
- from = to;
5334
- return;
5335
- }
5336
- const batch = {
5337
- batchId: randomUUID(),
5338
- from: new Date(from).toISOString(),
5339
- to: new Date(to).toISOString(),
5340
- counters: [...counters.values()].map(({ counter, value }) => ({
5341
- ...counter,
5342
- value
5343
- }))
5344
- };
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];
5345
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);
5346
5727
  from = to;
5347
- options.operator.enqueue({
5348
- port: "metering",
5349
- batch
5350
- });
5351
5728
  };
5352
5729
  const add = (name, scope, value = 1) => {
5353
5730
  if (!on || scope.account === "" || value <= 0) return;
@@ -5391,6 +5768,39 @@ function createMeter(options) {
5391
5768
  }
5392
5769
  };
5393
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
+ }
5394
5804
  //#endregion
5395
5805
  //#region src/server/operator.ts
5396
5806
  /**
@@ -5686,9 +6096,10 @@ async function loadOperator(options) {
5686
6096
  *
5687
6097
  * - `oidc`: any OpenID Connect provider, by its issuer's discovery document; the authorization
5688
6098
  * code flow with PKCE (S256) and a nonce, and the ID token checked (signature, issuer, audience,
5689
- * expiry, nonce) by `openid-client`.
6099
+ * expiry, nonce) by `openid-client`; the claims its entry names (`claims`) go into `facts`.
5690
6100
  * - `github`: GitHub's OAuth web flow with PKCE, then `GET /user` for the account's stable ID, its
5691
- * 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`).
5692
6103
  * - `development`: a page that asks for a name and trusts it, for tests and a self-hoster's first
5693
6104
  * run; its names are never shown as verified.
5694
6105
  *
@@ -5703,17 +6114,22 @@ const GITHUB = {
5703
6114
  token: "https://github.com/login/oauth/access_token",
5704
6115
  user: "https://api.github.com/user"
5705
6116
  };
5706
- 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) {
5707
6123
  return new RpcError(createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", { message }));
5708
6124
  }
5709
6125
  function secretOf(config) {
5710
- 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.`);
5711
6127
  try {
5712
6128
  const secret = readFileSync(config.clientSecretFile, "utf8").trim();
5713
6129
  if (secret === "") throw new Error("empty");
5714
6130
  return secret;
5715
6131
  } catch {
5716
- 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.`);
5717
6133
  }
5718
6134
  }
5719
6135
  /** A fetch bounded in time, for a provider's own endpoints. */
@@ -5723,13 +6139,37 @@ async function timedFetch(url, init) {
5723
6139
  signal: AbortSignal.timeout(PROVIDER_TIMEOUT_MS)
5724
6140
  });
5725
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
+ }
5726
6166
  /** The OpenID Connect adapter. Discovery happens at the first sign-in and is kept. */
5727
6167
  function oidcProvider(config, env) {
5728
- 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\`.`);
5729
6169
  const secret = secretOf(config);
5730
6170
  const issuer = new URL(config.issuer);
5731
6171
  const insecure = issuer.protocol === "http:" && env["MOSTRA_TEST_OIDC_INSECURE"] === "1" && (issuer.hostname === "127.0.0.1" || issuer.hostname === "localhost");
5732
- 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.`);
5733
6173
  let discovered;
5734
6174
  const configuration = () => {
5735
6175
  discovered ??= oidc.discovery(issuer, config.clientId, void 0, oidc.ClientSecretPost(secret), insecure ? {
@@ -5745,7 +6185,7 @@ function oidcProvider(config, env) {
5745
6185
  id: config.id,
5746
6186
  label: config.label ?? config.id,
5747
6187
  kind: "oidc",
5748
- verifies: true,
6188
+ ...verifiesOf(config),
5749
6189
  async authorize(state, pkce, context) {
5750
6190
  return oidc.buildAuthorizationUrl(await configuration(), {
5751
6191
  redirect_uri: context.redirectUri,
@@ -5772,14 +6212,19 @@ function oidcProvider(config, env) {
5772
6212
  subject: claims.sub,
5773
6213
  ...typeof claims["email"] === "string" ? { email: claims["email"] } : {},
5774
6214
  ...typeof claims["email_verified"] === "boolean" ? { emailVerified: claims["email_verified"] } : {},
5775
- ...name !== void 0 ? { name } : {}
6215
+ ...name !== void 0 ? { name } : {},
6216
+ ...chosen(claims, config.claims) ?? {}
5776
6217
  };
5777
6218
  }
5778
6219
  };
5779
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
+ }
5780
6225
  /** The GitHub adapter: an OAuth app's web flow with PKCE (VERIFIED_FACTS, WP-30). */
5781
6226
  function githubProvider(config, env) {
5782
- 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\`.`);
5783
6228
  const secret = secretOf(config);
5784
6229
  const base = env["MOSTRA_TEST_GITHUB_BASE"];
5785
6230
  const endpoints = base !== void 0 && /^http:\/\/(127\.0\.0\.1|localhost):\d+$/.test(base) ? {
@@ -5791,7 +6236,7 @@ function githubProvider(config, env) {
5791
6236
  id: config.id,
5792
6237
  label: config.label ?? "GitHub",
5793
6238
  kind: "github",
5794
- verifies: true,
6239
+ ...verifiesOf(config),
5795
6240
  authorize(state, pkce, context) {
5796
6241
  const url = new URL(endpoints.authorize);
5797
6242
  url.searchParams.set("client_id", config.clientId);
@@ -5837,7 +6282,10 @@ function githubProvider(config, env) {
5837
6282
  name: found.name ?? found.login ?? String(found.id),
5838
6283
  facts: {
5839
6284
  login: found.login ?? null,
5840
- ...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)
5841
6289
  }
5842
6290
  };
5843
6291
  }
@@ -5852,7 +6300,6 @@ function developmentProvider(config, apiOrigin) {
5852
6300
  id: config.id,
5853
6301
  label: config.label ?? "Development sign-in",
5854
6302
  kind: "development",
5855
- verifies: false,
5856
6303
  authorize(state) {
5857
6304
  const url = new URL(`${apiOrigin}/signin/${config.id}/name`);
5858
6305
  url.searchParams.set("state", state);
@@ -5873,7 +6320,7 @@ function developmentProvider(config, apiOrigin) {
5873
6320
  function builtInProviders(configs, options) {
5874
6321
  const seen = /* @__PURE__ */ new Set();
5875
6322
  return configs.map((config) => {
5876
- 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}\`.`);
5877
6324
  seen.add(config.id);
5878
6325
  const provider = config.kind === "oidc" ? oidcProvider(config, options.env) : config.kind === "github" ? githubProvider(config, options.env) : developmentProvider(config, options.apiOrigin);
5879
6326
  options.log.info("signin.provider", {
@@ -6042,137 +6489,6 @@ function createRouting(options) {
6042
6489
  };
6043
6490
  }
6044
6491
  //#endregion
6045
- //#region src/server/config.ts
6046
- /**
6047
- * The remote server's configuration (INTERFACES §18.1): a JSON file (`--config`) and
6048
- * `MOSTRA_SERVER_<KEY>` variables, read before the server serves anything. A configuration it
6049
- * can't use stops it, naming the key (`MOSTRA_REMOTE_CONFIG_INVALID`). Domains are configuration,
6050
- * never code (D137, D165 item 0a): the dossier and this code name only `example` hosts.
6051
- */
6052
- /** A rate: `count` per `per` (a duration such as `1m`, as links' lifetimes are written). */
6053
- const RateSchema = z.object({
6054
- count: z.int().min(1),
6055
- per: Duration
6056
- });
6057
- /** A host name a link can use: lower-case labels of letters, digits and hyphens. */
6058
- 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" });
6059
- /**
6060
- * An origin the server is reached at: `https:`, or `http:` for loopback and `*.localhost` (tests
6061
- * and a self-hoster's first run).
6062
- */
6063
- const Origin = z.string().refine((value) => {
6064
- try {
6065
- const url = new URL(value);
6066
- if (url.origin !== value) return false;
6067
- if (url.protocol === "https:") return true;
6068
- return url.protocol === "http:" && (url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname.endsWith(".localhost") || url.hostname === "[::1]");
6069
- } catch {
6070
- return false;
6071
- }
6072
- }, { message: "must be an https: origin (http: only on loopback), with no path" });
6073
- /** The schema of {@link RemoteServerConfig}. */
6074
- const Config = z.strictObject({
6075
- apiOrigin: Origin,
6076
- domains: z.strictObject({
6077
- active: HostName,
6078
- spares: z.array(HostName).optional()
6079
- }),
6080
- /**
6081
- * The scheme the user-content domain is served on: `https` behind a TLS terminator, `http`
6082
- * for a loopback test. Default `https`.
6083
- */
6084
- scheme: z.enum(["https", "http"]).optional(),
6085
- trustedProxies: z.array(z.string()).optional(),
6086
- identityProviders: z.array(z.strictObject({
6087
- id: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/),
6088
- kind: z.enum([
6089
- "development",
6090
- "oidc",
6091
- "github"
6092
- ]),
6093
- label: z.string().min(1).optional(),
6094
- issuer: z.string().optional(),
6095
- clientId: z.string().optional(),
6096
- clientSecretFile: z.string().optional()
6097
- })).optional(),
6098
- rateLimits: z.strictObject({
6099
- perIp: z.strictObject({
6100
- submissions: RateSchema.optional(),
6101
- requests: RateSchema.optional()
6102
- }).optional(),
6103
- perLink: z.strictObject({
6104
- submissions: RateSchema.optional(),
6105
- relayedRuns: RateSchema.optional()
6106
- }).optional(),
6107
- ipv6Prefix: z.int().min(1).max(128).optional()
6108
- }).optional(),
6109
- ledger: z.strictObject({ walAutocheckpoint: z.int().min(0).optional() }).optional(),
6110
- relay: z.strictObject({
6111
- waitingRunsPerArtifact: z.int().min(1).optional(),
6112
- drainPerSecond: z.number().positive().optional()
6113
- }).optional(),
6114
- homes: z.strictObject({ idleCloseMinutes: z.number().positive().optional() }).optional(),
6115
- framing: z.strictObject({ frameAncestors: z.literal("'none'") }).optional(),
6116
- operator: z.string().optional(),
6117
- chatHosts: z.record(z.string(), z.unknown()).optional()
6118
- });
6119
- /** The configuration keys, and the variable each also comes from. */
6120
- const KEYS = Object.keys(Config.shape);
6121
- /** `apiOrigin` → `MOSTRA_SERVER_API_ORIGIN`. */
6122
- function variableOf(key) {
6123
- return `MOSTRA_SERVER_${key.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase()}`;
6124
- }
6125
- function invalid(message) {
6126
- return new RpcError(createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", { message }));
6127
- }
6128
- /**
6129
- * Reads the configuration: the file, then each `MOSTRA_SERVER_<KEY>` (a string for a string key,
6130
- * JSON for the others) over it. Throws `MOSTRA_REMOTE_CONFIG_INVALID` naming the first key it
6131
- * can't use.
6132
- */
6133
- function readServerConfig(file, env) {
6134
- let raw = {};
6135
- if (file !== void 0) {
6136
- let text;
6137
- try {
6138
- text = readFileSync(file, "utf8");
6139
- } catch (error) {
6140
- throw invalid(`The configuration file ${file} can't be read (${error.code ?? "error"}).`);
6141
- }
6142
- try {
6143
- const parsed = JSON.parse(text);
6144
- if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("not an object");
6145
- raw = parsed;
6146
- } catch {
6147
- throw invalid(`The configuration file ${file} isn't a JSON object.`);
6148
- }
6149
- }
6150
- for (const key of KEYS) {
6151
- const variable = variableOf(key);
6152
- const value = env[variable];
6153
- if (value === void 0 || value === "") continue;
6154
- if (key === "apiOrigin" || key === "operator" || key === "scheme") raw[key] = value;
6155
- else try {
6156
- raw[key] = JSON.parse(value);
6157
- } catch {
6158
- throw invalid(`${variable} isn't JSON.`);
6159
- }
6160
- }
6161
- const checked = Config.safeParse(raw);
6162
- if (!checked.success) {
6163
- const issue = checked.error.issues[0];
6164
- throw invalid(`The configuration key \`${issue?.path.join(".") || "(configuration)"}\` can't be used: ${issue?.message ?? "invalid"}.`);
6165
- }
6166
- const config = checked.data;
6167
- const domains = [config.domains.active, ...config.domains.spares ?? []];
6168
- const api = new URL(config.apiOrigin).host;
6169
- 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).");
6170
- const proxy = validProxies(config.trustedProxies ?? []);
6171
- if (proxy !== void 0) throw invalid(`The configuration key \`trustedProxies\` names ${JSON.stringify(proxy)}, which is neither an address nor a CIDR block.`);
6172
- if (new Set(domains).size !== domains.length) throw invalid("The configuration key `domains` names one domain twice.");
6173
- return config;
6174
- }
6175
- //#endregion
6176
6492
  //#region src/server/index.ts
6177
6493
  /**
6178
6494
  * The remote server (ARCHITECTURE §15, INTERFACES §18, D165): one process holding many hosted
@@ -6238,18 +6554,23 @@ async function startServer(options) {
6238
6554
  * health check is never counted. Without the setting there is no limit.
6239
6555
  */
6240
6556
  const perIpRequests = rateOf(config.rateLimits?.perIp?.requests);
6241
- /** 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
+ */
6242
6563
  const ownerOf = (homeId) => {
6243
- 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
6244
- ORDER BY i.linked_at LIMIT 1) AS provider
6245
- 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));
6246
6569
  if (!row) return { account: "" };
6247
- const provider = config.identityProviders?.find((candidate) => candidate.id === row.provider);
6248
- const verifiedBy = provider && provider.kind !== "development" ? provider.label ?? (provider.kind === "github" ? "GitHub" : provider.id) : void 0;
6249
6570
  return {
6250
6571
  account: row.account,
6251
6572
  ...row.name !== null ? { name: row.name } : {},
6252
- ...verifiedBy !== void 0 ? { verifiedBy } : {}
6573
+ ...row.verified_by !== null ? { verifiedBy: row.verified_by } : {}
6253
6574
  };
6254
6575
  };
6255
6576
  const services = () => ({
@@ -6294,6 +6615,7 @@ async function startServer(options) {
6294
6615
  },
6295
6616
  shellDir,
6296
6617
  services: services(),
6618
+ ...Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) > 0 ? { sessionTouchMs: Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) } : {},
6297
6619
  relay: {
6298
6620
  waitingRunsPerArtifact: config.relay?.waitingRunsPerArtifact,
6299
6621
  drainPerSecond: config.relay?.drainPerSecond,
@@ -6336,6 +6658,7 @@ async function startServer(options) {
6336
6658
  await data.close();
6337
6659
  throw error;
6338
6660
  }
6661
+ fillHeldSizes(data.directory, join(options.data, "files"), log);
6339
6662
  routing = createRouting({
6340
6663
  apiHost: new URL(config.apiOrigin).host,
6341
6664
  domains: () => [config.domains.active, ...config.domains.spares ?? []],
@@ -6352,7 +6675,9 @@ async function startServer(options) {
6352
6675
  activeHomes: () => data.open().map((home) => ({
6353
6676
  home,
6354
6677
  account: ownerOf(home).account
6355
- }))
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"]) } : {}
6356
6681
  });
6357
6682
  auth = createAuthorization({
6358
6683
  directory: data.directory,
@@ -6653,6 +6978,33 @@ async function startServer(options) {
6653
6978
  }
6654
6979
  };
6655
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
+ }
6656
7008
  //#endregion
6657
7009
  //#region src/bin.ts
6658
7010
  /**