@mostra/runtime 0.12.0 → 0.14.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 (37) hide show
  1. package/dist/bin.js +2912 -2318
  2. package/dist/bin.js.map +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/{instance-CBUlOmzG.js → instance-CFTw3g-c.js} +227 -25
  5. package/dist/instance-CFTw3g-c.js.map +1 -0
  6. package/dist/reference/diagnostics.json +2 -2
  7. package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
  8. package/dist/reference/self-hosting.md +2 -1
  9. package/dist/skill/SKILL.md +2 -2
  10. package/dist/skill/assets/examples/dashboard/styles.css +27 -9
  11. package/dist/skill/assets/examples/decision/styles.css +27 -9
  12. package/dist/skill/assets/examples/inbox/styles.css +27 -9
  13. package/dist/skill/assets/studio.css +27 -9
  14. package/dist/skill/references/mcp.md +2 -2
  15. package/package.json +7 -6
  16. package/src/actions/index.ts +5 -2
  17. package/src/gateway/actions.ts +6 -1
  18. package/src/gateway/capabilities.ts +5 -1
  19. package/src/gateway/http.ts +15 -19
  20. package/src/gateway/index.ts +70 -1
  21. package/src/gateway/listener.ts +10 -5
  22. package/src/gateway/routes.ts +6 -1
  23. package/src/gateway/theme.ts +238 -0
  24. package/src/ledger/migrations.ts +9 -0
  25. package/src/server/api.ts +79 -87
  26. package/src/server/auth.ts +83 -16
  27. package/src/server/authorize.ts +74 -58
  28. package/src/server/config.ts +98 -0
  29. package/src/server/directory.ts +51 -0
  30. package/src/server/hosted-pages.ts +19 -6
  31. package/src/server/hosted.ts +59 -27
  32. package/src/server/index.ts +144 -17
  33. package/src/server/metering.ts +117 -12
  34. package/src/server/pages.ts +242 -0
  35. package/src/server/providers.ts +61 -9
  36. package/src/server/relay.ts +258 -107
  37. package/dist/instance-CBUlOmzG.js.map +0 -1
@@ -23,11 +23,15 @@ import {
23
23
  type SignInIdentity,
24
24
  SignInIdentity as SignInIdentitySchema,
25
25
  } from "@mostra/protocol/operator";
26
+ import { readAppearance } from "@mostra/themes";
27
+ import { type ServerPages, themePageCsp } from "../gateway/index.ts";
26
28
  import type { Ledger } from "../ledger/index.ts";
27
29
  import type { Log } from "../log.ts";
28
30
  import { type Authorization, canonicalResource, hashSecret } from "./auth.ts";
31
+ import { verifierLabel } from "./config.ts";
29
32
  import { type Clients, type OAuthClient, redirectAllowed } from "./oauth-clients.ts";
30
33
  import type { Operator } from "./operator.ts";
34
+ import { API_PAGE_WORDS, type ApiPages, type PageRequest } from "./pages.ts";
31
35
 
32
36
  /** How long an authorization request, a provider sign-in or a step waits. */
33
37
  const PENDING_MS = 15 * 60_000;
@@ -69,9 +73,24 @@ export interface AuthorizeOptions {
69
73
  /** Names a hosted home's owner, for a page's sign-in ("Grace Hopper's page"). */
70
74
  readonly ownerName: (homeId: string) => string | undefined;
71
75
  readonly log: Log;
76
+ /** The server's own pages (its operator's theme and name, D171) and the API origin's on them. */
77
+ readonly server: ServerPages;
78
+ readonly pages: ApiPages;
72
79
  readonly now?: () => number;
73
80
  }
74
81
 
82
+ /**
83
+ * Where a page answers and what its reader chose (their appearance, D171), from the request a
84
+ * response answers.
85
+ */
86
+ export function pageRequest(response: ServerResponse): PageRequest {
87
+ const request = response.req as IncomingMessage | undefined;
88
+ const cookie = request?.headers.cookie;
89
+ // Without a choice of their own, a person gets the operator's default (the pages know it).
90
+ const chosen = cookie?.includes("mostra-appearance=") ? readAppearance(cookie) : undefined;
91
+ return { ...(chosen ? { appearance: chosen } : {}), path: request?.url ?? "/" };
92
+ }
93
+
75
94
  /** A page's sign-in in progress (INTERFACES §18.4), bound to its route and shell session. */
76
95
  export interface ViewerSignin {
77
96
  readonly homeId: string;
@@ -121,21 +140,10 @@ const BASE = {
121
140
  "x-frame-options": "DENY",
122
141
  } as const;
123
142
 
124
- function escapeHtml(text: string): string {
125
- return text.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`);
126
- }
127
-
128
- function shell(title: string, body: string): string {
129
- return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="robots" content="noindex">
130
- <meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(title)}</title>
131
- <style>body{font:16px/1.5 system-ui,sans-serif;max-width:34rem;margin:3rem auto;padding:0 1rem}
132
- input{font:inherit;padding:.3rem}button,a.button{font:inherit;padding:.4rem .9rem;margin:.2rem .5rem .2rem 0;display:inline-block}
133
- .muted{color:#555}</style></head><body><h1>${escapeHtml(title)}</h1>${body}</body></html>`;
134
- }
135
-
136
143
  /** Builds the pages. */
137
144
  export function createAuthorize(options: AuthorizeOptions): Authorize {
138
- const { apiOrigin, directory, auth, clients, operator, log } = options;
145
+ const { apiOrigin, directory, auth, clients, operator, log, pages } = options;
146
+ const at = (response: ServerResponse): PageRequest => pageRequest(response);
139
147
  const now = options.now ?? (() => Date.now());
140
148
  const secure = apiOrigin.startsWith("https:");
141
149
  const sessionCookie = secure ? "__Host-mostra-signin" : "mostra-signin";
@@ -167,8 +175,13 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
167
175
  response.writeHead(status, {
168
176
  ...BASE,
169
177
  "content-type": "text/html; charset=utf-8",
170
- // A form posts here, and its answer may redirect to a client: those origins only.
171
- "content-security-policy": `default-src 'none'; style-src 'unsafe-inline'; form-action 'self' ${formTargets.join(" ")}; frame-ancestors 'none'; base-uri 'none'`,
178
+ // The page loads its theme's files from this origin and runs nothing. A form posts here,
179
+ // and its answer may redirect to a client: those origins only.
180
+ "content-security-policy": themePageCsp(
181
+ options.server.pages,
182
+ "'none'",
183
+ ["'self'", ...formTargets].join(" "),
184
+ ),
172
185
  // A form's POST must carry this page's Origin (D166 item 10).
173
186
  "referrer-policy": "same-origin",
174
187
  ...extra,
@@ -184,11 +197,7 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
184
197
  response.end();
185
198
  };
186
199
  const problem = (response: ServerResponse, status: number, message: string): void =>
187
- send(
188
- response,
189
- status,
190
- shell("Sign-in can't continue", `<p role="alert">${escapeHtml(message)}</p>`),
191
- );
200
+ send(response, status, pages.problem(at(response), message));
192
201
 
193
202
  // ── in-progress records ─────────────────────────────────────────────────────────────────
194
203
 
@@ -253,6 +262,19 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
253
262
 
254
263
  // ── providers ───────────────────────────────────────────────────────────────────────────
255
264
 
265
+ /**
266
+ * The label a provider's verified names carry (`IdentityProvider.verifies`, MT-03): text of 1 to
267
+ * 64 characters, else none, since an operator's provider is checked as every operator answer is.
268
+ */
269
+ const verifierOf = (provider: IdentityProvider): string | undefined => {
270
+ const raw: unknown = (provider as { verifies?: { label?: unknown } }).verifies?.label;
271
+ const label = verifierLabel(raw);
272
+ if (label === undefined && provider.verifies !== undefined) {
273
+ log.warn("signin.verifier_refused", { provider: provider.id });
274
+ }
275
+ return label;
276
+ };
277
+
256
278
  /** The providers now: configured ones, then the operator's (asked each time, D167). */
257
279
  const providers = async (): Promise<IdentityProvider[]> => {
258
280
  const supplied = await operator.providers();
@@ -270,6 +292,8 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
270
292
  }
271
293
  interface StepRecord {
272
294
  readonly identity: SignInIdentity;
295
+ /** The verifier label of the provider that named the person (MT-03). */
296
+ readonly verifiedBy?: string;
273
297
  readonly steps: number;
274
298
  readonly flow: string;
275
299
  readonly next: string;
@@ -282,18 +306,14 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
282
306
  message?: string,
283
307
  ): Promise<void> => {
284
308
  const list = await providers();
285
- const links = list
286
- .map(
287
- (provider) =>
288
- `<p><a class="button" href="/signin/${encodeURIComponent(provider.id)}/start?next=${encodeURIComponent(next)}">Continue with ${escapeHtml(provider.label)}</a></p>`,
289
- )
290
- .join("");
291
309
  send(
292
310
  response,
293
311
  200,
294
- shell(
295
- "Sign in to Mostra",
296
- `${message ? `<p role="alert">${escapeHtml(message)}</p>` : ""}${links || "<p>This server has no identity provider configured, so nobody can sign in yet.</p>"}`,
312
+ pages.signIn(
313
+ at(response),
314
+ list.map((provider) => ({ id: provider.id, label: provider.label })),
315
+ next,
316
+ message,
297
317
  ),
298
318
  );
299
319
  };
@@ -306,11 +326,12 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
306
326
  const resolved = async (
307
327
  response: ServerResponse,
308
328
  identity: SignInIdentity,
329
+ verifiedBy: string | undefined,
309
330
  steps: number,
310
331
  flow: string,
311
332
  next: string,
312
333
  ): Promise<void> => {
313
- const account = await auth.resolveAccount(identity, steps);
334
+ const account = await auth.resolveAccount(identity, steps, verifiedBy);
314
335
  if (account.kind === "unavailable") {
315
336
  problem(response, 503, "Sign-in is unavailable for a moment; try again shortly.");
316
337
  return;
@@ -329,7 +350,13 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
329
350
  return;
330
351
  }
331
352
  const handle = token();
332
- keep("step", handle, { identity, steps: steps + 1, flow, next } satisfies StepRecord);
353
+ keep("step", handle, {
354
+ identity,
355
+ ...(verifiedBy !== undefined ? { verifiedBy } : {}),
356
+ steps: steps + 1,
357
+ flow,
358
+ next,
359
+ } satisfies StepRecord);
333
360
  const url = new URL(account.url);
334
361
  url.searchParams.set("mostra_return", `${apiOrigin}/signin/return?step=${handle}`);
335
362
  redirect(response, url.href);
@@ -469,26 +496,24 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
469
496
  signed: { accountId: string; hash: string },
470
497
  homeId: string | undefined,
471
498
  ): void => {
472
- const who = auth.accountName(signed.accountId).name ?? "your account";
499
+ const who = auth.accountName(signed.accountId).name ?? API_PAGE_WORDS.yourAccount;
473
500
  const redirectHost = new URL(pending.redirectUri).host;
474
501
  const loopback = /^(127\.0\.0\.1|\[::1\]|localhost)(:\d+)?$/.test(redirectHost);
475
- const body =
476
- client.kind === "shell"
477
- ? `<p>Continue as <strong>${escapeHtml(who)}</strong> to answer <strong>${escapeHtml(
478
- (homeId !== undefined ? options.ownerName(homeId) : undefined) ?? "this person",
479
- )}</strong>'s page? They will see your name with your answers.</p>`
480
- : `<p><strong>${escapeHtml(client.name)}</strong>${client.unverified ? ' <span class="muted">(a name the app gives itself, not checked)</span>' : ""} asks to use Mostra as <strong>${escapeHtml(who)}</strong>: to read, create and change your hosted pages and their answers.</p>
481
- <p>It will send you back to <strong>${escapeHtml(redirectHost)}</strong>.${loopback ? " That is a program on your own computer: continue only if you started this there." : ""}</p>`;
502
+ const form = {
503
+ action: `/oauth/authorize/continue?authorize=${encodeURIComponent(handle)}`,
504
+ csrf: csrf(signed.hash, `consent ${handle}`),
505
+ };
482
506
  send(
483
507
  response,
484
508
  200,
485
- shell(
486
- client.kind === "shell" ? "Answer as yourself" : "Allow access",
487
- `${body}<form method="post" action="/oauth/authorize/continue?authorize=${encodeURIComponent(handle)}">
488
- <input type="hidden" name="csrf" value="${csrf(signed.hash, `consent ${handle}`)}">
489
- <button name="decision" value="approve">${client.kind === "shell" ? "Continue" : "Allow"}</button>
490
- <button name="decision" value="deny">Cancel</button></form>`,
491
- ),
509
+ client.kind === "shell"
510
+ ? pages.answerAs(
511
+ at(response),
512
+ form,
513
+ who,
514
+ homeId !== undefined ? options.ownerName(homeId) : undefined,
515
+ )
516
+ : pages.allow(at(response), form, client, who, redirectHost, loopback),
492
517
  {},
493
518
  [new URL(pending.redirectUri).origin],
494
519
  );
@@ -715,16 +740,7 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
715
740
  if (name && method === "GET") {
716
741
  // The development provider's page: a name, trusted as typed (D166 item 4).
717
742
  const state = url.searchParams.get("state") ?? "";
718
- send(
719
- response,
720
- 200,
721
- shell(
722
- "Development sign-in",
723
- `<p class="muted">This server signs people in with a development provider, which trusts the name you type. Use it only on a server you run for yourself or for tests.</p>
724
- <form method="post" action="/signin/${escapeHtml(id)}/name"><input type="hidden" name="state" value="${escapeHtml(state)}">
725
- <label>Your name <input name="name" required maxlength="64"></label> <button>Continue</button></form>`,
726
- ),
727
- );
743
+ send(response, 200, pages.developmentName(at(response), id, state));
728
744
  return true;
729
745
  }
730
746
  let params: Record<string, string>;
@@ -792,7 +808,7 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
792
808
  );
793
809
  return true;
794
810
  }
795
- await resolved(response, identity, 0, pending.flow, pending.next);
811
+ await resolved(response, identity, verifierOf(provider), 0, pending.flow, pending.next);
796
812
  return true;
797
813
  }
798
814
  if (method === "GET" && path === "/signin/return") {
@@ -807,7 +823,7 @@ export function createAuthorize(options: AuthorizeOptions): Authorize {
807
823
  );
808
824
  return true;
809
825
  }
810
- await resolved(response, step.identity, step.steps, step.flow, step.next);
826
+ await resolved(response, step.identity, step.verifiedBy, step.steps, step.flow, step.next);
811
827
  return true;
812
828
  }
813
829
  return false;
@@ -6,7 +6,9 @@
6
6
  */
7
7
  import { readFileSync } from "node:fs";
8
8
  import { createDiagnostic, Duration, RpcError } from "@mostra/protocol";
9
+ import { safeHref } from "@mostra/themes";
9
10
  import * as z from "zod";
11
+ import type { AppearanceConfig } from "../gateway/index.ts";
10
12
  import type { Environment } from "../log.ts";
11
13
  import { validProxies } from "./clients.ts";
12
14
 
@@ -50,6 +52,18 @@ const Origin: z.ZodType<string> = z.string().refine(
50
52
  { message: "must be an https: origin (http: only on loopback), with no path" },
51
53
  );
52
54
 
55
+ /**
56
+ * A name or a short line a server's pages show (D171): 1 to 64 characters with no control,
57
+ * format or separator characters, as `verifierLabel` holds labels, so one can't read as another.
58
+ */
59
+ const PageName: z.ZodType<string> = z
60
+ .string()
61
+ .min(1)
62
+ .max(64)
63
+ .refine((value) => value.trim() !== "" && !/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(value), {
64
+ message: "must be 1 to 64 characters with no control or format characters",
65
+ });
66
+
53
67
  /** A rate limit: `count` per `per`. */
54
68
  export interface Rate {
55
69
  readonly count: number;
@@ -64,8 +78,39 @@ export interface IdentityProviderConfig {
64
78
  readonly issuer?: string | undefined;
65
79
  readonly clientId?: string | undefined;
66
80
  readonly clientSecretFile?: string | undefined;
81
+ /**
82
+ * An `oidc` provider's ID-token claims that go into `SignInIdentity.facts` under their own names,
83
+ * when present (a Google Workspace account's domain, `hd`): at most 8 short names (MT-03).
84
+ */
85
+ readonly claims?: string[] | undefined;
86
+ }
87
+
88
+ /**
89
+ * The label a configured provider's verified names carry on a hosted page's sender line ("verified
90
+ * with GitHub"): its `label`, else GitHub's name or its ID; none for the development provider, which
91
+ * takes the typed name on trust (D161). Recorded on each identity the provider links (MT-03).
92
+ */
93
+ export function configuredVerifier(provider: IdentityProviderConfig): string | undefined {
94
+ if (provider.kind === "development") return undefined;
95
+ return verifierLabel(provider.label ?? (provider.kind === "github" ? "GitHub" : provider.id));
67
96
  }
68
97
 
98
+ /**
99
+ * A verifier's label as the sender line may show it (MT-03, and its Opus review): text of 1 to 64
100
+ * characters with no control, format (bidirectional overrides, zero-width joiners), line or
101
+ * paragraph separator characters, which could make one label read as another's ("verified with
102
+ * GitHub" from reversed text); anything else is no label.
103
+ */
104
+ export function verifierLabel(label: unknown): string | undefined {
105
+ if (typeof label !== "string") return undefined;
106
+ if (label.trim() === "" || label.length > 64) return undefined;
107
+ if (/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(label)) return undefined;
108
+ return label;
109
+ }
110
+
111
+ /** Claim names that would reach an object's prototype rather than a key of its own. */
112
+ const RESERVED_CLAIMS = new Set(["__proto__", "constructor", "prototype"]);
113
+
69
114
  /** The configuration a remote server runs with (INTERFACES §18.1). */
70
115
  export interface RemoteServerConfig {
71
116
  readonly apiOrigin: string;
@@ -95,6 +140,8 @@ export interface RemoteServerConfig {
95
140
  readonly framing?: { readonly frameAncestors: "'none'" } | undefined;
96
141
  readonly operator?: string | undefined;
97
142
  readonly chatHosts?: Record<string, unknown> | undefined;
143
+ /** How the server's own pages look and name themselves (D171). */
144
+ readonly appearance?: AppearanceConfig | undefined;
98
145
  }
99
146
 
100
147
  /** The schema of {@link RemoteServerConfig}. */
@@ -120,6 +167,20 @@ const Config: z.ZodType<RemoteServerConfig> & { readonly shape: Record<string, u
120
167
  issuer: z.string().optional(),
121
168
  clientId: z.string().optional(),
122
169
  clientSecretFile: z.string().optional(),
170
+ claims: z
171
+ .array(
172
+ z
173
+ .string()
174
+ .regex(/^[A-Za-z_][A-Za-z0-9_.:-]{0,63}$/, {
175
+ message: "must be claim names of at most 64 letters, digits and `_.:-`",
176
+ })
177
+ .refine((name) => !RESERVED_CLAIMS.has(name), {
178
+ message: "can't name `__proto__`, `constructor` or `prototype`",
179
+ }),
180
+ )
181
+ .min(1)
182
+ .max(8)
183
+ .optional(),
123
184
  }),
124
185
  )
125
186
  .optional(),
@@ -145,6 +206,35 @@ const Config: z.ZodType<RemoteServerConfig> & { readonly shape: Record<string, u
145
206
  framing: z.strictObject({ frameAncestors: z.literal("'none'") }).optional(),
146
207
  operator: z.string().optional(),
147
208
  chatHosts: z.record(z.string(), z.unknown()).optional(),
209
+ appearance: z
210
+ .strictObject({
211
+ theme: z.enum(["studio", "mostra"]).optional(),
212
+ name: PageName.nullable().optional(),
213
+ logo: z
214
+ .strictObject({
215
+ file: z.string().regex(/\.(svg|png|webp)$/i, {
216
+ message: "must be an .svg, .png or .webp file",
217
+ }),
218
+ alt: PageName,
219
+ width: z.int().min(1).max(400),
220
+ height: z.int().min(1).max(120),
221
+ })
222
+ .optional(),
223
+ stageLine: PageName.optional(),
224
+ default: z.enum(["system", "light", "dark"]).optional(),
225
+ footer: z
226
+ .array(
227
+ z.strictObject({
228
+ href: z.string().refine((href) => safeHref(href) !== undefined, {
229
+ message: "must be an https: address or a path of this server",
230
+ }),
231
+ text: PageName,
232
+ }),
233
+ )
234
+ .max(6)
235
+ .optional(),
236
+ })
237
+ .optional(),
148
238
  });
149
239
 
150
240
  /** The configuration keys, and the variable each also comes from. */
@@ -215,6 +305,14 @@ export function readServerConfig(file: string | undefined, env: Environment): Re
215
305
  "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).",
216
306
  );
217
307
  }
308
+ const claimed = (config.identityProviders ?? []).findIndex(
309
+ (provider) => provider.claims !== undefined && provider.kind !== "oidc",
310
+ );
311
+ if (claimed >= 0) {
312
+ throw invalid(
313
+ `The configuration key \`identityProviders.${claimed}.claims\` can't be used: only an \`oidc\` provider passes on its ID token's claims.`,
314
+ );
315
+ }
218
316
  const proxy = validProxies(config.trustedProxies ?? []);
219
317
  if (proxy !== undefined) {
220
318
  throw invalid(
@@ -229,4 +229,55 @@ export const SERVER_MIGRATIONS: readonly Migration[] = [
229
229
  `);
230
230
  },
231
231
  },
232
+ {
233
+ version: 5,
234
+ name: "the verifier on each identity",
235
+ up(db) {
236
+ // MT-03 (DATA_MODEL §7.1, additive). Who verified the person's name, as the provider that
237
+ // links or signs in an identity says (`IdentityProvider.verifies`), recorded on the identity:
238
+ // a hosted page's sender line reads it from here, so renaming or retiring a provider later
239
+ // turns no verified name into an unverified one. `verified_recorded_at` is when it was
240
+ // recorded; an identity linked before this has none until the server, at its next start,
241
+ // gives it the verifier its configuration names for its provider then (the server's own
242
+ // accounts' sender lines unchanged by the upgrade).
243
+ db.exec(`
244
+ ALTER TABLE identity ADD COLUMN verified_by TEXT;
245
+ ALTER TABLE identity ADD COLUMN verified_recorded_at TEXT;
246
+ `);
247
+ },
248
+ },
249
+ {
250
+ version: 6,
251
+ name: "what each home holds",
252
+ up(db) {
253
+ // MT-03 (DATA_MODEL §7.1, additive). The bytes of each version file a home holds, as its push
254
+ // gave them (held to the bytes uploaded), and each home's running total, kept by triggers in
255
+ // the transaction of every change to `home_file`, so metering's `bytes.held` reads one row a
256
+ // home and falls when a removal frees files. `held_dirty` marks a home whose total changed
257
+ // since `held_reported` (the level last queued for the operator), so a change is reported
258
+ // after a restart too. A row from before this has no size until the server, at its next
259
+ // start, reads the stored file's (0 for one that is gone, so it is read once).
260
+ db.exec(`
261
+ ALTER TABLE home_file ADD COLUMN size INTEGER CHECK (size IS NULL OR size >= 0);
262
+ ALTER TABLE home ADD COLUMN held_bytes INTEGER NOT NULL DEFAULT 0 CHECK (held_bytes >= 0);
263
+ ALTER TABLE home ADD COLUMN held_reported INTEGER;
264
+ ALTER TABLE home ADD COLUMN held_dirty INTEGER NOT NULL DEFAULT 0 CHECK (held_dirty IN (0, 1));
265
+ CREATE INDEX home_held_dirty ON home (held_dirty) WHERE held_dirty = 1;
266
+ CREATE TRIGGER home_file_held_insert AFTER INSERT ON home_file WHEN NEW.size IS NOT NULL
267
+ BEGIN
268
+ UPDATE home SET held_bytes = held_bytes + NEW.size, held_dirty = 1 WHERE id = NEW.home_id;
269
+ END;
270
+ CREATE TRIGGER home_file_held_delete AFTER DELETE ON home_file WHEN OLD.size IS NOT NULL
271
+ BEGIN
272
+ UPDATE home SET held_bytes = held_bytes - OLD.size, held_dirty = 1 WHERE id = OLD.home_id;
273
+ END;
274
+ CREATE TRIGGER home_file_held_size AFTER UPDATE OF size ON home_file
275
+ WHEN COALESCE(OLD.size, -1) != COALESCE(NEW.size, -1)
276
+ BEGIN
277
+ UPDATE home SET held_bytes = held_bytes - COALESCE(OLD.size, 0) + COALESCE(NEW.size, 0),
278
+ held_dirty = 1 WHERE id = NEW.home_id;
279
+ END;
280
+ `);
281
+ },
282
+ },
232
283
  ];
@@ -152,19 +152,32 @@ export function hostedPages(
152
152
  });
153
153
  };
154
154
 
155
- /** A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9). */
156
- const expired = (tokenId: string, policy: LinkPolicy): Refusal | undefined => {
155
+ /**
156
+ * A link past the operator's longest lifetime or idle limit: revoked now, 410 (D165 item 9).
157
+ * "Idle" is as `LinkPolicy.idleExpiry` says, without a view: the link's last use before this
158
+ * visit is the later of its last view (a frame ticket) and the activity of its other sessions
159
+ * (`last_seen_at`, which a page's every request keeps, so a page open and in use for longer
160
+ * than the idle time keeps its link). The session asking for this ticket is left out, since its
161
+ * own exchange, or any request of a returning browser, has just written it: counted, an idle
162
+ * link would open for every new visit (MT-03, and its Opus review). A session's activity is
163
+ * written at most every `touchMs` (an hour), so a link lapses at most that much early.
164
+ */
165
+ const expired = (tokenId: string, asking: string, policy: LinkPolicy): Refusal | undefined => {
157
166
  if (policy.maxTtl === undefined && policy.idleExpiry === undefined) return undefined;
158
167
  const used = ledger.read((db) =>
159
- db.get<{ created_at: string; last: string | null }>(
160
- `SELECT t.created_at, (SELECT MAX(last_seen_at) FROM session s WHERE s.token_id = t.id) AS last
168
+ db.get<{ created_at: string; viewed: string | null; active: string | null }>(
169
+ `SELECT t.created_at,
170
+ (SELECT MAX(v.opened_at) FROM view v WHERE v.token_id = t.id) AS viewed,
171
+ (SELECT MAX(s.last_seen_at) FROM session s WHERE s.token_id = t.id AND s.id != ?) AS active
161
172
  FROM token t WHERE t.id = ?`,
173
+ asking,
162
174
  tokenId,
163
175
  ),
164
176
  );
165
177
  if (!used) return undefined;
166
178
  const minted = Date.parse(used.created_at);
167
- const lastUse = Math.max(minted, used.last === null ? 0 : Date.parse(used.last));
179
+ const at = (value: string | null): number => (value === null ? 0 : Date.parse(value));
180
+ const lastUse = Math.max(minted, at(used.viewed), at(used.active));
168
181
  const tooOld = policy.maxTtl !== undefined && now() - minted > durationMs(policy.maxTtl);
169
182
  const idle = policy.idleExpiry !== undefined && now() - lastUse > durationMs(policy.idleExpiry);
170
183
  if (!tooOld && !idle) return undefined;
@@ -411,7 +424,7 @@ export function hostedPages(
411
424
  async admitView(input) {
412
425
  if (quarantineOf(input.artifactId, input.versionId) !== undefined) return underReview();
413
426
  const policy = await policyFor(input.artifactId, input.tokenId);
414
- const gone = expired(input.tokenId, policy);
427
+ const gone = expired(input.tokenId, input.sessionId, policy);
415
428
  if (gone) return gone;
416
429
  const route = options.routeOf(input.artifactId);
417
430
  // `view` proceeds when the operator doesn't answer: nothing a person sees is held up by it.
@@ -42,10 +42,11 @@ import {
42
42
  createGateway,
43
43
  type FaceSpec,
44
44
  type Gateway,
45
+ type ServerPages,
45
46
  } from "../gateway/index.ts";
46
47
  import { platformName } from "../home/index.ts";
47
48
  import { createEphemeralBus, createInteraction, type Interaction } from "../interaction/index.ts";
48
- import type { Ledger } from "../ledger/index.ts";
49
+ import type { Ledger, LedgerDb } from "../ledger/index.ts";
49
50
  import { createLive, liveMethods } from "../live/index.ts";
50
51
  import type { Log } from "../log.ts";
51
52
  import { removeTree } from "../platform/index.ts";
@@ -114,7 +115,7 @@ export interface HostedHome {
114
115
  */
115
116
  wanted(sha256: string): number | undefined;
116
117
  /** Records a file this home uploaded (`PUT /files/<sha256>`, §18.2). */
117
- uploaded(sha256: string): void;
118
+ uploaded(sha256: string, size: number): void;
118
119
  /** Pauses or resumes an artifact as its owner or the operator (D165 item 14). */
119
120
  pause(
120
121
  artifactId: string,
@@ -183,6 +184,8 @@ export interface HostedHomeOptions {
183
184
  /** The relay's server settings (`relay` in the configuration, D165 item 4). */
184
185
  /** The test fault points (`MOSTRA_TEST_FAULTS`); none by default. */
185
186
  readonly faults?: Faults;
187
+ /** How often a session's activity is written at most (tests shorten it; an hour by default). */
188
+ readonly sessionTouchMs?: number | undefined;
186
189
  readonly relay?: {
187
190
  readonly waitingRunsPerArtifact?: number | undefined;
188
191
  readonly drainPerSecond?: number | undefined;
@@ -207,6 +210,8 @@ export interface HostedServices {
207
210
  readonly owner: (homeId: string) => HomeOwner;
208
211
  readonly signInOffered: () => boolean;
209
212
  readonly madeWith?: { readonly href?: string } | undefined;
213
+ /** The server's own pages, in its operator's theme and name (D171). */
214
+ readonly pages?: (() => ServerPages) | undefined;
210
215
  /** The server's routing by exact `Host`: operator hosts, their frames and link hosts (D167). */
211
216
  readonly routing: Routing;
212
217
  /** The server's meter: views here, submissions through the operator feed. */
@@ -380,6 +385,7 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
380
385
  now: () => Date.now(),
381
386
  shellOrigin: () => originOf(options.domains.active),
382
387
  servesShell,
388
+ ...(options.sessionTouchMs !== undefined ? { touchIntervalMs: options.sessionTouchMs } : {}),
383
389
  // Every hosted link is indexed in the directory, so the operator's `revokeLink` finds its home.
384
390
  onMinted: ({ tokenId, artifactId }) => {
385
391
  const route = routeOf(artifactId);
@@ -456,6 +462,23 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
456
462
  ...(options.relay?.sweepMs !== undefined ? { sweepMs: options.relay.sweepMs } : {}),
457
463
  ...(options.relay?.liveRenewMs !== undefined ? { liveRenewMs: options.relay.liveRenewMs } : {}),
458
464
  ...(options.relay?.perLink !== undefined ? { perLink: options.relay.perLink } : {}),
465
+ // The operator's `limits` for a page's relayed actions (D165 item 9), each with quantity 1.
466
+ ...(options.services
467
+ ? {
468
+ limit: (artifactId: string, operation: "run" | "run.wait" | "call") => {
469
+ const services = options.services as HostedServices;
470
+ const route = routeOf(artifactId);
471
+ return services.operator.limit({
472
+ account: services.owner(id).account,
473
+ home: id,
474
+ artifact: artifactId,
475
+ ...(route ? { routeId: route.id } : {}),
476
+ operation,
477
+ quantity: 1,
478
+ });
479
+ },
480
+ }
481
+ : {}),
459
482
  latestVersion: (artifactId) =>
460
483
  ledger.read(
461
484
  (db) =>
@@ -477,7 +500,9 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
477
500
  );
478
501
  },
479
502
  };
503
+ const pages = options.services?.pages?.();
480
504
  const gateway: Gateway = createGateway({
505
+ ...(pages ? { pages } : {}),
481
506
  ledger,
482
507
  log,
483
508
  addressing,
@@ -633,6 +658,33 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
633
658
  if (!gateway.dispatch(key, request, response)) notServed(response);
634
659
  };
635
660
 
661
+ /**
662
+ * Records that this home holds a version file (DATA_MODEL §7.1's `home_file`), counted once in
663
+ * `file_ref`, with its size in bytes (the size its push gave, held to the bytes), which metering's
664
+ * `bytes.held` sums (MT-03). A row recorded before sizes were kept takes it now.
665
+ */
666
+ const holdFile = (db: LedgerDb, sha: string, size: number | undefined): void => {
667
+ const added = db.run(
668
+ "INSERT OR IGNORE INTO home_file (home_id, sha256, size) VALUES (?, ?, ?)",
669
+ id,
670
+ sha,
671
+ size ?? null,
672
+ );
673
+ if (added.changes > 0) {
674
+ db.run(
675
+ "INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL",
676
+ sha,
677
+ );
678
+ } else if (size !== undefined) {
679
+ db.run(
680
+ "UPDATE home_file SET size = ? WHERE home_id = ? AND sha256 = ? AND size IS NULL",
681
+ size,
682
+ id,
683
+ sha,
684
+ );
685
+ }
686
+ };
687
+
636
688
  /** The client files of a listing this home doesn't hold yet. */
637
689
  const missingOf = (files: readonly string[]): string[] =>
638
690
  directory.read((db) =>
@@ -868,6 +920,8 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
868
920
  home: id,
869
921
  artifact: params.artifactId,
870
922
  routeId: route.routeId,
923
+ // The version itself, so a push that takes several rounds is one version (MT-03).
924
+ versionId: version.versionId,
871
925
  operation,
872
926
  quantity,
873
927
  });
@@ -939,17 +993,7 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
939
993
  });
940
994
  directory.write((db) => {
941
995
  for (const sha of new Set(clientFiles.map(([, , file]) => file))) {
942
- const added = db.run(
943
- "INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)",
944
- id,
945
- sha,
946
- );
947
- if (added.changes > 0) {
948
- db.run(
949
- "INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL",
950
- sha,
951
- );
952
- }
996
+ holdFile(db, sha, sizes.get(sha));
953
997
  }
954
998
  });
955
999
  log.info("server.version_pushed", {
@@ -1165,20 +1209,8 @@ export async function openHostedHome(options: HostedHomeOptions): Promise<Hosted
1165
1209
  }
1166
1210
  return undefined;
1167
1211
  },
1168
- uploaded(sha) {
1169
- directory.write((db) => {
1170
- const added = db.run(
1171
- "INSERT OR IGNORE INTO home_file (home_id, sha256) VALUES (?, ?)",
1172
- id,
1173
- sha,
1174
- );
1175
- if (added.changes > 0) {
1176
- db.run(
1177
- "INSERT INTO file_ref (sha256, homes) VALUES (?, 1) ON CONFLICT (sha256) DO UPDATE SET homes = homes + 1, unreferenced_at = NULL",
1178
- sha,
1179
- );
1180
- }
1181
- });
1212
+ uploaded(sha, size) {
1213
+ directory.write((db) => holdFile(db, sha, size));
1182
1214
  for (const files of pending.values()) files.delete(sha);
1183
1215
  },
1184
1216
  pause: (artifactId, change) => interaction.pause(artifactId, change),