@join-pantheon/cli 0.33.11 → 0.34.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/relay.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createServer } from "node:http";
2
2
  import { createHash, randomBytes, timingSafeEqual as timingSafeEqualBuf } from "node:crypto";
3
- import { copyFileSync, readdirSync, readFileSync, renameSync, rmSync, statSync, existsSync, writeFileSync, openSync, readSync, closeSync, fstatSync, promises as fsp } from "node:fs";
3
+ import { copyFileSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, existsSync, writeFileSync, openSync, readSync, closeSync, fstatSync, promises as fsp } from "node:fs";
4
4
  import { spawn } from "node:child_process";
5
5
  import { monitorEventLoopDelay } from "node:perf_hooks";
6
6
  import { basename, join } from "node:path";
@@ -11,18 +11,24 @@ import { compareVersions, INSTALLER_MIN_CLI, isVersion, installerScript, Install
11
11
  import { installerLine } from "./managed.js";
12
12
  import { Ledger, SpaceFull } from "./ledger.js";
13
13
  import { fingerprint, newNonce, verifyChallenge, verifyActivity, verifyAccount, verifyRead, actPayload, goalPayload, verifyBytes, verifyDelegate, canonicalJson } from "./identity.js";
14
- import { Accounts, normalizeUsername, validEmail, validUsername } from "./accounts.js";
14
+ import { ADMIN_SESSION_MS, Accounts, normalizeEmail, normalizeUsername, validEmail, validUsername, VIEWER_DEFAULT_DAYS, VIEWER_MAX_DAYS, VIEWER_SESSION_MAX_MS } from "./accounts.js";
15
+ import { renderViewerBusy, renderViewerUnreadable, renderViewerEnded, renderViewerHome, renderViewerNeedsHttps, renderViewerSignin, renderViewerTeam } from "./viewer-pages.js";
15
16
  import { emailSenderFromEnv, maskEmail } from "./email.js";
16
17
  import { Allowlist, RosterUnreadable } from "./roster-file.js";
17
18
  import { StateStore, ensurePrivateDir, setWaitForDisk, takeSlowestDiskWait, waitsForDisk } from "./store.js";
18
19
  import { countGoal, countMessage, emptyUsage, parseUsage, usageAnswer, USAGE_KEEP_DAYS, USAGE_SHOW_DAYS } from "./usage.js";
19
20
  import { busyWriter, DiskBusy, flushWrites, onWriteError, pendingView, recordWrites, whenLanded, writerFor } from "./disk-writer.js";
20
- import { activity, mergeActivity, feed, noteRefusal, refusalsSince, countActs, spaceHealth, fleetReport, ESCALATIONS, MESSAGE_ACTS, ACT_TONE, DAY } from "./admin-stats.js";
21
+ import { feed, noteRefusal, spaceHealth, fleetReport, isMessage, stuckBehind, versionBehind, MESSAGE_ACTS, DAY } from "./admin-stats.js";
22
+ import { flowIndexAsync, flowOf, flowOfAsync, seriesOf, toFlowGoals, toFlowObligations, toFlowRecords } from "./flow-stats.js";
23
+ import { newNonce as newUiNonce, pageHeaders, rangeOf } from "./admin-ui.js";
24
+ import { renderAdminLogin, renderAudit, renderOverview, renderPeople, renderPlans, renderSecurity, renderTeam, renderTeamUnreadable } from "./admin-pages.js";
21
25
  import { fmtLimit, inviteLimit, parseLimits, planLimits, PLAN_NAME } from "./plans.js";
22
26
  import { FEEDBACK_ADDRESS } from "./feedback.js";
23
27
  import { relayShellArg, teamNameProblem } from "./team-name.js";
24
28
  import { reservedBy, routeDecision } from "./profile.js";
25
29
  import { namedMarker } from "./decision-ui.js";
30
+ import { ByteLru } from "./byte-lru.js";
31
+ import { DELETED_NAME_HOLD_MS, TEAM_FILES, classifyLeftover, teamStems } from "./leftovers.js";
26
32
  const presentSessions = (m) => [...m.sessions.values()].filter((s) => !s.quiet);
27
33
  function workState(m, now = Date.now(), busyTtlMs = LIMITS.activityBusyTtlMs, pruneMs = LIMITS.activityPruneMs) {
28
34
  let known = false;
@@ -116,62 +122,18 @@ function clientIp(req) {
116
122
  const trusted = Math.max(1, Number(process.env.PANTHEON_TRUSTED_HOPS ?? 1) || 1);
117
123
  return hops.length ? hops[Math.max(0, hops.length - trusted)] : req.socket.remoteAddress || "unknown";
118
124
  }
125
+ function forwardedProto(req) {
126
+ if (process.env.PANTHEON_TRUSTED_HOPS?.trim() === "0")
127
+ return "";
128
+ const raw = req.headers["x-forwarded-proto"];
129
+ const hops = (Array.isArray(raw) ? raw.join(",") : raw ?? "").split(",").map((x) => x.trim().toLowerCase()).filter(Boolean);
130
+ const trusted = Math.max(1, Number(process.env.PANTHEON_TRUSTED_HOPS ?? 1) || 1);
131
+ return hops.length ? hops[Math.max(0, hops.length - trusted)] : "";
132
+ }
119
133
  function secretEq(a, b) {
120
134
  const x = createHash("sha256").update(a).digest(), y = createHash("sha256").update(b).digest();
121
135
  return timingSafeEqualBuf(x, y);
122
136
  }
123
- const ADMIN_CSS = `<style>
124
- :root{--bg:#0b0c0e;--card:#14161a;--card2:#1b1e24;--line:#262a30;--fg:#e7e9ec;--dim:#9aa1a9;--accent:#5b9dff;--warn:#e8b45c;--bad:#ef8b80;--ok:#7bd39a;color-scheme:dark}
125
- @media(prefers-color-scheme:light){:root{--bg:#f6f7f9;--card:#fff;--card2:#f1f3f6;--line:#e3e6ea;--fg:#1a1c1f;--dim:#59616e;--accent:#2563eb;--warn:#b45309;--bad:#b91c1c;--ok:#15803d;color-scheme:light}}
126
- *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:14px/1.55 system-ui,-apple-system,"Segoe UI",sans-serif}
127
- a{color:var(--accent);text-decoration:none}a:hover{text-decoration:underline}
128
- header{position:sticky;top:0;z-index:5;background:var(--bg);padding:14px 28px;border-bottom:1px solid var(--line);display:flex;align-items:center;gap:14px;flex-wrap:wrap}
129
- h1{font-size:17px;margin:0;font-weight:650}.dim{color:var(--dim)}.meta{color:var(--dim);font-size:12.5px}
130
- .links{margin-left:auto;display:flex;gap:14px;font-size:13px;flex-wrap:wrap}
131
- nav.jump{display:flex;gap:4px;flex-wrap:wrap;padding:8px 22px;border-bottom:1px solid var(--line)}
132
- nav.jump a{color:var(--dim);font-size:13px;padding:5px 10px;border-radius:6px}nav.jump a:hover{background:var(--card2);color:var(--fg);text-decoration:none}
133
- main{padding:0 28px 48px;max-width:1560px}
134
- section{margin-top:34px;scroll-margin-top:70px}
135
- .sec{display:flex;align-items:baseline;gap:12px;margin:0 0 10px;flex-wrap:wrap}.sec h2{font-size:16px;margin:0;font-weight:650}.sec .count{color:var(--dim);font-size:13px}
136
- .sec input[type=search]{margin-left:auto;font:13px system-ui;padding:6px 10px;border:1px solid var(--line);border-radius:7px;background:var(--card);color:var(--fg);width:240px}
137
- .kpis{display:grid;grid-template-columns:repeat(auto-fit,minmax(178px,1fr));gap:12px;margin-top:22px}
138
- .kpi{background:var(--card);border:1px solid var(--line);border-radius:10px;padding:14px 16px 12px}
139
- .kpi .l{color:var(--dim);font-size:11.5px;letter-spacing:.04em;text-transform:uppercase;font-weight:600}
140
- .kpi .v{font-size:26px;font-weight:650;font-variant-numeric:tabular-nums;line-height:1.2;margin:6px 0 3px}
141
- .kpi .v small{font-size:13.5px;color:var(--dim);font-weight:500;margin-left:5px}
142
- .kpi .s{color:var(--dim);font-size:12.5px}.kpi.warn .v{color:var(--warn)}.kpi.bad .v{color:var(--bad)}
143
- .wrap{overflow-x:auto;border:1px solid var(--line);border-radius:10px;background:var(--card)}
144
- table{border-collapse:separate;border-spacing:0;width:100%;font-variant-numeric:tabular-nums}
145
- th{position:sticky;top:0;z-index:1;background:var(--card2);color:var(--dim);font-weight:600;font-size:12px;text-align:left;padding:9px 12px;border-bottom:1px solid var(--line);white-space:nowrap}
146
- th.group{text-align:center;color:var(--fg);border-left:1px solid var(--line);font-weight:650}th.first{border-left:1px solid var(--line)}
147
- td{padding:10px 12px;border-bottom:1px solid var(--line);vertical-align:top;white-space:nowrap}
148
- tr:last-child>td{border-bottom:0}tbody tr:hover>td{background:var(--card2)}
149
- td.num,th.num{text-align:right}td.wrapc{white-space:normal;min-width:280px;max-width:760px;overflow-wrap:anywhere}
150
- .cell b{font-weight:600}.sub{display:block;color:var(--dim);font-size:12px;margin-top:2px;white-space:nowrap}
151
- code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}
152
- .pill{display:inline-block;font-size:11px;line-height:1.7;padding:0 7px;border-radius:999px;border:1px solid var(--line);color:var(--dim);white-space:nowrap;vertical-align:middle}
153
- .pill.ok{color:var(--ok);border-color:var(--ok)}.pill.bad{color:var(--bad);border-color:var(--bad)}.pill.warn{color:var(--warn);border-color:var(--warn)}.pill.info{color:var(--accent);border-color:var(--accent)}.pill.dim{color:var(--dim)}
154
- .inl{display:inline}.act{font:12px system-ui;padding:4px 9px;border-radius:6px;border:1px solid var(--line);background:transparent;color:var(--fg);cursor:pointer}
155
- .act:hover{border-color:var(--accent)}.act.danger{color:var(--bad)}.act.danger:hover{border-color:var(--bad)}
156
- td.actions{text-align:right;white-space:nowrap}td.actions .inl+.inl,td.actions .act+.act{margin-left:6px}
157
- input[type=number],input[type=text],input[type=search],select{font:13px system-ui;padding:5px 8px;border:1px solid var(--line);border-radius:6px;background:var(--bg);color:var(--fg)}input[type=number]{width:74px}
158
- .note{color:var(--dim);font-size:12.5px;margin:8px 0 0;max-width:84ch}
159
- .done{margin:16px 28px 0;padding:10px 14px;border:1px solid var(--ok);color:var(--ok);border-radius:10px;background:var(--card)}
160
- :focus-visible{outline:2px solid var(--accent);outline-offset:2px}input::placeholder{color:var(--dim);opacity:1}
161
- details.limits>summary{cursor:pointer;color:var(--accent);font-size:12px;list-style:none;display:inline}details.limits>summary::-webkit-details-marker{display:none}
162
- .limitform{display:flex;gap:10px;align-items:flex-end;flex-wrap:wrap;background:var(--card2);padding:8px 10px;border-radius:8px;margin-top:8px}
163
- .limitform label{display:flex;flex-direction:column;gap:3px;font-size:11px;color:var(--dim)}.limitform label.chk{flex-direction:row;align-items:center;gap:6px;padding-bottom:7px}
164
- .filters{display:flex;gap:10px;flex-wrap:wrap;align-items:flex-end;margin:0 0 12px}
165
- .filters label{display:flex;flex-direction:column;gap:3px;font-size:12px;color:var(--dim)}.filters label.chk{flex-direction:row;align-items:center;gap:6px;padding-bottom:7px}
166
- .more{font:12px system-ui;color:var(--accent);background:none;border:0;padding:0;cursor:pointer;white-space:nowrap}
167
- tr.detail>td{background:var(--card2);white-space:normal;padding:14px 16px 16px}.body{white-space:pre-wrap;max-width:92ch;font-size:13.5px;line-height:1.6}
168
- .kv{display:grid;grid-template-columns:max-content 1fr;gap:4px 16px;font-size:12.5px;color:var(--dim);margin-top:12px;max-width:92ch}.kv b{color:var(--fg);font-weight:500;overflow-wrap:anywhere}
169
- .pager{display:flex;justify-content:space-between;margin:12px 0}
170
- .empty{padding:22px;color:var(--dim)}
171
- .tone-warn{color:var(--warn)}.tone-bad{color:var(--bad)}.tone-ok{color:var(--ok)}
172
- .crumbs a{color:var(--dim)}
173
- @media(max-width:700px){header,nav.jump{padding-left:16px;padding-right:16px}main{padding:0 16px 40px}.sec input[type=search]{width:100%;margin-left:0}.done{margin:12px 16px 0}}
174
- </style>`;
175
137
  const clipText = (v, max) => { const t = v.replace(/\s+/g, " ").trim(); if (t.length <= max)
176
138
  return t; const cut = t.slice(0, max - 1); const sp = cut.lastIndexOf(" "); return `${(sp > max * 0.6 ? cut.slice(0, sp) : cut).trimEnd()}…`; };
177
139
  const escHtml = (v) => String(v ?? "").replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c]));
@@ -182,43 +144,106 @@ const fmtAgo = (t) => {
182
144
  const m = Math.round((Date.now() - t) / 60_000);
183
145
  return m < 1 ? "just now" : m < 60 ? `${m}m ago` : m < 1440 ? `${Math.round(m / 60)}h ago` : `${Math.round(m / 1440)}d ago`;
184
146
  };
185
- const safeName = (name) => name.replace(/[^a-z0-9._-]/gi, "_");
186
- export function printableLine(s, max) {
187
- return s.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, " ").replace(/\s+/g, " ").trim().slice(0, max);
147
+ const LEDGER_CACHE_BYTES = 256 * 1024 * 1024;
148
+ const VIEW_CACHE_BYTES = 64 * 1024 * 1024;
149
+ const COLD_MAX = 2;
150
+ const VIEW_INFLIGHT_PER_SESSION = 2;
151
+ const SLEEPING_REPLAY_MAX = 2_000;
152
+ const maxOrNull = (a, b) => (a === null ? b : b === null ? a : Math.max(a, b));
153
+ function compactedFromGaps(records) {
154
+ let through = null;
155
+ let prev = 0;
156
+ for (const r of records) {
157
+ if (r.seq > prev + 1 && (through === null || r.ts > through))
158
+ through = r.ts;
159
+ prev = r.seq;
160
+ }
161
+ return through;
188
162
  }
189
- const quoteTeam = (name) => relayShellArg(name);
190
- function parseLines(text) {
163
+ class UnboundTeam extends Error {
164
+ team;
165
+ constructor(team) {
166
+ super(`"${team}" has no team token bound yet`);
167
+ this.team = team;
168
+ }
169
+ }
170
+ class LedgerUnreadable extends Error {
171
+ code;
172
+ constructor(code) {
173
+ super(`the ledger cannot be read (${code})`);
174
+ this.code = code;
175
+ }
176
+ }
177
+ const unreadableCode = (e) => (e instanceof LedgerUnreadable ? e.code : null);
178
+ async function parseSliced(text) {
191
179
  const records = [];
192
- for (const line of text.split("\n")) {
193
- if (!line.trim())
194
- continue;
195
- try {
196
- records.push(JSON.parse(line));
180
+ for (let at = 0, n = 0; at < text.length; n++) {
181
+ let nl = text.indexOf("\n", at);
182
+ if (nl === -1)
183
+ nl = text.length;
184
+ const line = text.slice(at, nl);
185
+ at = nl + 1;
186
+ if (line.trim()) {
187
+ try {
188
+ records.push(JSON.parse(line));
189
+ }
190
+ catch { }
197
191
  }
198
- catch { }
192
+ if (n % 5_000 === 4_999)
193
+ await new Promise((x) => setImmediate(x));
199
194
  }
200
195
  return records;
201
196
  }
197
+ function estimateViewBytes(v) {
198
+ let n = 4_000 + v.members.length * 400 + v.flow.open.length * 450 + v.flow.askStatus.length * 120 + v.flow.carried.length * 250
199
+ + v.flow.goals.length * 400 + v.flow.threads.length * 900 + v.refusals.recent.length * 150 + v.invites.length * 200 + v.repos.length * 200
200
+ + (v.flow.answerMs.length + v.flow.pickupMs.length + v.flow.decidedMs.length) * 16;
201
+ for (const g of v.gists.values())
202
+ n += 2 * g.length + 80;
203
+ for (const t of v.goalTitles.values())
204
+ n += 2 * t.length + 80;
205
+ return n;
206
+ }
207
+ const safeName = (name) => name.replace(/[^a-z0-9._-]/gi, "_");
208
+ export function printableLine(s, max) {
209
+ return s.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, " ").replace(/\s+/g, " ").trim().slice(0, max);
210
+ }
211
+ const quoteTeam = (name) => relayShellArg(name);
202
212
  const spaceKey = (name) => safeName(name).toLowerCase();
203
213
  function firstLedgerName(file) {
204
214
  let fd = null;
205
215
  try {
206
216
  fd = openSync(file, "r");
207
- const chunks = [];
208
217
  const buf = Buffer.allocUnsafe(64 * 1024);
209
- for (let pos = 0, total = 0; total < 1 << 20;) {
218
+ let carry = Buffer.alloc(0), pos = 0, lines = 0;
219
+ const named = (line) => {
220
+ const t = line.toString("utf8").trim();
221
+ if (!t)
222
+ return null;
223
+ lines++;
224
+ try {
225
+ const r = JSON.parse(t);
226
+ return typeof r?.space === "string" && r.space ? r.space : null;
227
+ }
228
+ catch {
229
+ return null;
230
+ }
231
+ };
232
+ while (pos < 1 << 20 && lines < 64) {
210
233
  const n = readSync(fd, buf, 0, buf.length, pos);
211
234
  if (n <= 0)
212
235
  break;
213
- const nl = buf.subarray(0, n).indexOf(10);
214
- chunks.push(Buffer.from(buf.subarray(0, nl === -1 ? n : nl)));
215
- if (nl !== -1)
216
- break;
217
236
  pos += n;
218
- total += n;
237
+ let data = Buffer.concat([carry, buf.subarray(0, n)]);
238
+ for (let nl = data.indexOf(10); nl !== -1 && lines < 64; nl = data.indexOf(10)) {
239
+ const hit = named(data.subarray(0, nl));
240
+ if (hit)
241
+ return hit;
242
+ data = data.subarray(nl + 1);
243
+ }
244
+ carry = Buffer.from(data);
219
245
  }
220
- const r = JSON.parse(Buffer.concat(chunks).toString("utf8"));
221
- return typeof r.space === "string" && r.space ? r.space : null;
246
+ return lines < 64 && carry.length ? named(carry) : null;
222
247
  }
223
248
  catch {
224
249
  return null;
@@ -254,6 +279,7 @@ const DEVICE_OTHER_ACCOUNT = [
254
279
  const UPGRADE_NOTE = "This team now needs Pantheon 0.30 or newer to read its history — run: pantheon upgrade";
255
280
  const UPGRADE_NOTE_ID = "a_pantheon_upgrade_required";
256
281
  const RELAY_MEMBER = "pantheon";
282
+ const VIEW_CODE_ANSWER_MS = 400;
257
283
  function ledgerTailSeq(file) {
258
284
  let fd = null;
259
285
  try {
@@ -445,7 +471,7 @@ export function tombTeams(t) {
445
471
  const hashes = [...(t.tokenHash ? [t.tokenHash] : []), ...(t.retiredTokenHashes ?? [])];
446
472
  return [{ name: t.name, at: t.at, hashes, ...(t.by ? { by: t.by } : {}), ...(t.how ? { how: t.how } : {}), ...(t.id ? { id: t.id } : {}) }];
447
473
  }
448
- export const DELETED_NAME_HOLD_MS = 3_600_000;
474
+ export { DELETED_NAME_HOLD_MS };
449
475
  const UTC_MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
450
476
  const dayOf = (at) => { const d = new Date(at); return `${d.getUTCDate()} ${UTC_MONTHS[d.getUTCMonth()]}, ${String(d.getUTCHours()).padStart(2, "0")}:${String(d.getUTCMinutes()).padStart(2, "0")} UTC`; };
451
477
  export function goneAnswer(t) {
@@ -455,6 +481,8 @@ export function deletedWords(t) {
455
481
  const who = t.by && t.by !== "operator" ? ` by its founder ${t.by}` : t.how === "operator" || t.by === "operator" ? " by the relay operator" : "";
456
482
  return `the team "${t.name}" was deleted${who} on ${dayOf(t.at)}; it no longer exists on this relay. Remove it from this computer: pantheon leave ${t.id ?? relayShellArg(t.name)}`;
457
483
  }
484
+ const coveredNameWords = (name) => `the name "${name}" belongs to a deleted team whose files are still here; the relay operator can move them to quarantine, or use a different name.`;
485
+ const invitesHeldWords = (name) => `invites to "${name}" cannot be used right now: a team of this name was deleted on this relay, and the relay cannot tell this one apart from it. Ask the team's founder or the relay operator.`;
458
486
  const deletedNameWords = (name) => `the space "${name}" was deleted. Start a new one with a different name, or contact the relay operator.`;
459
487
  const removedWords = (member, name, by) => `"${member}" was removed from "${name}"${by ? ` by ${by}` : ""}. Only a member of the team can let you back in.`;
460
488
  export class Relay {
@@ -567,8 +595,20 @@ export class Relay {
567
595
  this.adminAllow = (process.env.PANTHEON_ADMIN_ALLOW_IPS ?? "").split(",").map((x) => x.trim()).filter(Boolean);
568
596
  const wantStore = this.requireAccounts || this.adminToken !== null;
569
597
  this.accounts = opts.accounts ?? (wantStore ? new Accounts(join(this.dataDir, "accounts.db")) : null);
598
+ try {
599
+ this.reportLeftovers();
600
+ }
601
+ catch (e) {
602
+ this.log(`! checking for deleted teams' leftover files: ${e.message}`);
603
+ }
570
604
  if (this.accounts) {
571
605
  this.backfillSpaceOwners();
606
+ try {
607
+ this.bindViewerGrants();
608
+ }
609
+ catch (e) {
610
+ this.log(`! binding viewer grants to team ids: ${e.message}`);
611
+ }
572
612
  const closed = this.accounts.endAllOpenSessions();
573
613
  if (closed)
574
614
  this.log(`ended ${closed} session${closed === 1 ? "" : "s"} left open by the previous relay process`);
@@ -686,10 +726,13 @@ export class Relay {
686
726
  catch {
687
727
  return;
688
728
  }
729
+ const tombs = new Set(files.filter((f) => f.endsWith(".deleted.json")).map((f) => f.slice(0, -".deleted.json".length).toLowerCase()));
689
730
  for (const f of files) {
690
731
  const safe = f.endsWith(".members.json") ? f.slice(0, -".members.json".length) : f.endsWith(".jsonl") ? f.slice(0, -".jsonl".length) : null;
691
732
  if (safe === null || this.unbound.has(safe))
692
733
  continue;
734
+ if (tombs.has(safe.toLowerCase()) && this.leftoverState(safe).state !== "live")
735
+ continue;
693
736
  try {
694
737
  if (this.unboundOnDisk(safe))
695
738
  this.unbound.add(safe);
@@ -722,6 +765,8 @@ export class Relay {
722
765
  onHttp(req, res) {
723
766
  const url = new URL(req.url ?? "/", "http://x");
724
767
  const ip = clientIp(req);
768
+ if (url.pathname === "/view" || url.pathname.startsWith("/view/"))
769
+ return this.onViewHttp(req, res, url, ip);
725
770
  if (url.pathname === "/" || url.pathname.startsWith("/auth/") || url.pathname.startsWith("/account/")
726
771
  || url.pathname === "/audit" || url.pathname.startsWith("/admin")) {
727
772
  return this.onAuthHttp(req, res, url, ip);
@@ -916,7 +961,7 @@ export class Relay {
916
961
  onAuthHttp(req, res, url, ip) {
917
962
  if (url.pathname === "/")
918
963
  return this.landingPage(req, res);
919
- if (url.pathname === "/admin" || url.pathname === "/admin/space" || url.pathname === "/admin/export" || url.pathname === "/admin/backup" || url.pathname === "/admin/action" || url.pathname === "/admin/login" || url.pathname === "/admin/logout" || url.pathname === "/admin/report") {
964
+ if (url.pathname === "/admin" || url.pathname === "/admin/space" || url.pathname === "/admin/export" || url.pathname === "/admin/backup" || url.pathname === "/admin/action" || url.pathname === "/admin/login" || url.pathname === "/admin/logout" || url.pathname === "/admin/report" || url.pathname === "/admin/leftovers") {
920
965
  const A0 = this.accounts;
921
966
  if (!A0 || !this.adminToken)
922
967
  return this.json(res, 404, { error: "not found" });
@@ -925,6 +970,7 @@ export class Relay {
925
970
  const tokenOk = (t) => { const a = Buffer.from(t), b = Buffer.from(this.adminToken); return a.length === b.length && timingSafeEqualBuf(a, b); };
926
971
  const fromCookie = cookieVal(req, "pantheon_admin");
927
972
  const fromQuery = url.searchParams.get("token") ?? req.headers["authorization"]?.replace(/^Bearer\s+/i, "") ?? "";
973
+ const tokenFp = this.adminTokenFp();
928
974
  if (url.pathname === "/admin/login" && req.method === "POST") {
929
975
  if (!this.adminLoginOk(ip)) {
930
976
  res.writeHead(429, { "content-type": "text/html; charset=utf-8", "retry-after": "60" });
@@ -937,16 +983,26 @@ export class Relay {
937
983
  return this.adminLoginPage(res, true);
938
984
  }
939
985
  A0.audit({ event: "admin_login", ip });
940
- const secure = req.headers["x-forwarded-proto"] === "https" ? "; Secure" : "";
941
- res.writeHead(302, { "set-cookie": `pantheon_admin=${encodeURIComponent(body.token)}; HttpOnly; SameSite=Strict; Path=/admin; Max-Age=43200${secure}`, location: "/admin" });
986
+ const secure = forwardedProto(req) === "https" ? "; Secure" : "";
987
+ const secret = A0.createAdminSession(tokenFp, ip);
988
+ res.writeHead(302, { "set-cookie": `pantheon_admin=${secret}; HttpOnly; SameSite=Strict; Path=/admin; Max-Age=${ADMIN_SESSION_MS / 1000}${secure}`, location: "/admin" });
942
989
  return void res.end();
943
990
  });
944
991
  }
945
992
  if (url.pathname === "/admin/logout") {
993
+ A0.endAdminSession(fromCookie);
946
994
  res.writeHead(302, { "set-cookie": "pantheon_admin=; HttpOnly; SameSite=Strict; Path=/admin; Max-Age=0", location: "/admin" });
947
995
  return void res.end();
948
996
  }
949
- const authed = (fromCookie && tokenOk(fromCookie)) || (fromQuery && tokenOk(fromQuery));
997
+ const byToken = !!fromQuery && tokenOk(fromQuery);
998
+ const bySession = !byToken && !!fromCookie && A0.adminSessionOk(fromCookie, tokenFp);
999
+ const authed = byToken || bySession;
1000
+ const csrf = bySession ? this.sessionCsrf(fromCookie) : this.adminCsrf();
1001
+ if (url.pathname === "/admin/leftovers") {
1002
+ if (!authed)
1003
+ return this.json(res, 401, { error: "admin token required" });
1004
+ return this.json(res, 200, { relay: VERSION, generated: Date.now(), leftovers: this.leftovers() });
1005
+ }
950
1006
  if (url.pathname === "/admin/report") {
951
1007
  if (!authed)
952
1008
  return this.json(res, 401, { error: "admin token required" });
@@ -958,12 +1014,19 @@ export class Relay {
958
1014
  this.json(res, 500, { error: "the usage report could not be made" }); });
959
1015
  return;
960
1016
  }
961
- const rows = this.fleetHealth();
962
- const relayAgeMs = Date.now() - this.startedAt;
963
- if (url.searchParams.get("format") === "json")
964
- return this.json(res, 200, { relay: VERSION, relay_age_ms: relayAgeMs, generated: Date.now(), spaces: rows });
965
- res.writeHead(200, { "content-type": "text/plain; charset=utf-8" });
966
- return void res.end(fleetReport(rows, this.upgradeHold ?? VERSION, Date.now(), relayAgeMs) + "\n");
1017
+ void (async () => {
1018
+ const { rows, unreadable } = await this.fleetHealthAsync();
1019
+ if (res.destroyed)
1020
+ return;
1021
+ const relayAgeMs = Date.now() - this.startedAt;
1022
+ const cannot = unreadable.map((u) => ({ name: u.name, verdict: "unreadable", says: `history can't be read right now (${u.code})`, unreadable: u.code }));
1023
+ if (url.searchParams.get("format") === "json")
1024
+ return this.json(res, 200, { relay: VERSION, relay_age_ms: relayAgeMs, generated: Date.now(), spaces: [...rows, ...cannot] });
1025
+ res.writeHead(200, { "content-type": "text/plain; charset=utf-8" });
1026
+ res.end(fleetReport(rows, this.upgradeHold ?? VERSION, Date.now(), relayAgeMs, unreadable) + "\n");
1027
+ })().catch((e) => { this.log(`! report failed: ${e.message}`); if (!res.headersSent)
1028
+ this.json(res, 500, { error: "the report could not be made" }); });
1029
+ return;
967
1030
  }
968
1031
  if (url.pathname === "/admin/export") {
969
1032
  if (!authed)
@@ -987,18 +1050,18 @@ export class Relay {
987
1050
  if (url.pathname === "/admin/action" && req.method === "POST") {
988
1051
  if (!authed)
989
1052
  return this.json(res, 403, { error: "sign in at /admin" });
990
- return this.readForm(req, res, (body) => this.adminAction(res, A0, body, ip));
1053
+ return this.readForm(req, res, (body) => this.adminAction(res, A0, body, ip, csrf));
991
1054
  }
992
1055
  if (url.pathname === "/admin/space") {
993
1056
  if (!authed)
994
1057
  return this.adminLoginPage(res, false);
995
1058
  const name = url.searchParams.get("name") ?? "";
996
1059
  A0.audit({ event: "admin_space_view", space: name, detail: url.search.replace(/^\?/, "").replace(/(^|&)token=[^&]*/g, "$1token=…") || null, ip });
997
- return this.adminSpace(res, url, name);
1060
+ return this.adminSpace(res, url, name, csrf, A0, ip);
998
1061
  }
999
1062
  if (!authed)
1000
1063
  return this.adminLoginPage(res, false);
1001
- return this.adminDashboard(res, A0, url);
1064
+ return this.adminDashboard(res, A0, url, csrf);
1002
1065
  }
1003
1066
  const A = this.accounts;
1004
1067
  if (!A)
@@ -1430,25 +1493,9 @@ export class Relay {
1430
1493
  });
1431
1494
  }
1432
1495
  adminLoginPage(res, wrong) {
1433
- res.writeHead(wrong ? 401 : 200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "referrer-policy": "no-referrer" });
1434
- res.end(`<!doctype html><html lang=en><meta charset=utf-8><title>pantheon · admin</title>
1435
- <meta name=viewport content="width=device-width,initial-scale=1">
1436
- ${ADMIN_CSS}
1437
- <style>
1438
- body{min-height:100dvh;display:grid;place-items:center;padding:20px}
1439
- main{width:100%;max-width:360px}form{padding:24px;border:1px solid var(--line);border-radius:12px;background:var(--card)}
1440
- h1{margin:0 0 8px}p{color:var(--dim);margin:0 0 20px}label{display:block;margin-bottom:6px}
1441
- input{width:100%;padding:10px 12px;border-radius:8px;border:1px solid var(--line);background:var(--bg);color:var(--fg);font:inherit}
1442
- button{width:100%;margin-top:16px;padding:10px;border:0;border-radius:8px;background:#2563eb;color:#fff;font:inherit;font-weight:600;cursor:pointer}
1443
- button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1444
- </style>
1445
- <main><form method=POST action="/admin/login">
1446
- <h1>pantheon · admin</h1><p>Sign in to manage your relay.</p>
1447
- <label for=admin-token>Admin token</label>
1448
- <input id=admin-token type=password name=token required autofocus autocomplete=current-password${wrong ? ' aria-invalid="true" aria-describedby="login-error"' : ''}>
1449
- <button>Sign in</button>
1450
- ${wrong ? '<div id=login-error class=err role=alert>That token was not accepted. Check your admin token and try again.</div>' : ''}
1451
- </form></main>`);
1496
+ const nonce = newUiNonce();
1497
+ res.writeHead(wrong ? 401 : 200, pageHeaders(nonce));
1498
+ res.end(renderAdminLogin({ nonce, wrong, host: this.opts.host ?? "this relay" }));
1452
1499
  }
1453
1500
  adminIpOk(ip) {
1454
1501
  if (!this.adminAllow.length)
@@ -1473,11 +1520,17 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1473
1520
  adminCsrf() {
1474
1521
  return createHash("sha256").update("pantheon-admin-csrf " + (this.adminToken ?? "")).digest("hex").slice(0, 32);
1475
1522
  }
1523
+ sessionCsrf(secret) {
1524
+ return createHash("sha256").update("pantheon-admin-csrf-session " + secret).digest("hex").slice(0, 32);
1525
+ }
1526
+ adminTokenFp() {
1527
+ return createHash("sha256").update("pantheon-admin-fp " + (this.adminToken ?? "")).digest("hex").slice(0, 32);
1528
+ }
1476
1529
  teamOfInvite(token) {
1477
1530
  const exact = (al) => { const inv = al.findInvite(token); return !!inv && inv.token === token; };
1478
1531
  for (const sp of this.spaces.values())
1479
1532
  if (exact(sp.allow))
1480
- return sp.name;
1533
+ return this.leftoverState(safeName(sp.name)).state === "live" ? sp.name : null;
1481
1534
  const safeOf = (name) => name.replace(/[^a-z0-9._-]/gi, "_");
1482
1535
  const live = new Set(this.spaces.keys());
1483
1536
  let files = [];
@@ -1493,10 +1546,13 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1493
1546
  const safe = f.slice(0, -".members.json".length);
1494
1547
  if (live.has(safe.toLowerCase()))
1495
1548
  continue;
1549
+ if (this.leftoverState(safe).state !== "live")
1550
+ continue;
1496
1551
  try {
1497
- if (!exact(new Allowlist(this.dataDir, safe)))
1552
+ const al = new Allowlist(this.dataDir, safe);
1553
+ if (!exact(al))
1498
1554
  continue;
1499
- const named = this.sleepingRecords(safe).find((r) => r.space)?.space;
1555
+ const named = al.recordedName() ?? firstLedgerName(join(this.dataDir, `${safe}.jsonl`));
1500
1556
  if (named)
1501
1557
  return named;
1502
1558
  if (safeOf(safe) === safe && !/_/.test(safe))
@@ -1507,38 +1563,36 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1507
1563
  }
1508
1564
  return null;
1509
1565
  }
1510
- fleetHealth(now = Date.now()) {
1566
+ async fleetHealthAsync(now = Date.now()) {
1511
1567
  const out = [];
1512
- const seen = new Set();
1513
- const safeOf = (name) => name.replace(/[^a-z0-9._-]/gi, "_");
1514
- for (const sp of this.spaces.values()) {
1515
- seen.add(safeOf(sp.name));
1516
- const sessions = [...sp.members.values()].flatMap((m) => presentSessions(m));
1517
- out.push(spaceHealth({
1518
- name: sp.name, records: sp.ledger.all(), members: sp.allow.all(),
1519
- online: sessions.length, interactive: sessions.filter((x) => x.role === "interactive").length,
1520
- relayPkg: this.upgradeHold ?? VERSION, now, relayAgeMs: now - this.startedAt,
1521
- }));
1522
- }
1523
- let files = [];
1524
- try {
1525
- files = readdirSync(this.dataDir);
1526
- }
1527
- catch { }
1528
- for (const f of files) {
1529
- if (!f.endsWith(".members.json"))
1568
+ const unreadable = [];
1569
+ for (const n of await this.allTeamNamesAsync()) {
1570
+ const sp = this.spaces.get(spaceKey(n));
1571
+ if (sp) {
1572
+ const sessions = [...sp.members.values()].flatMap((m) => presentSessions(m));
1573
+ out.push(spaceHealth({ name: sp.name, records: this.healthSlice(sp.ledger.all(), now), members: sp.allow.all(), online: sessions.length, interactive: sessions.filter((x) => x.role === "interactive").length, relayPkg: this.upgradeHold ?? VERSION, now, relayAgeMs: now - this.startedAt }));
1530
1574
  continue;
1531
- const safe = f.slice(0, -".members.json".length);
1532
- if (seen.has(safe))
1533
- continue;
1534
- let members = [];
1575
+ }
1576
+ const safe = safeName(n);
1535
1577
  try {
1536
- members = new Allowlist(this.dataDir, safe).all();
1578
+ out.push(await this.coldSlot(async () => {
1579
+ const records = await this.ledgerRecordsAsync(safe);
1580
+ let members = [];
1581
+ try {
1582
+ members = new Allowlist(this.dataDir, safe).all();
1583
+ }
1584
+ catch { }
1585
+ return spaceHealth({ name: safe, records: this.healthSlice(records, now), members, online: 0, interactive: 0, relayPkg: this.upgradeHold ?? VERSION, now, relayAgeMs: now - this.startedAt });
1586
+ }));
1587
+ }
1588
+ catch (e) {
1589
+ const code = unreadableCode(e);
1590
+ if (!code)
1591
+ throw e;
1592
+ unreadable.push({ name: safe, code });
1537
1593
  }
1538
- catch { }
1539
- out.push(spaceHealth({ name: safe, records: this.sleepingRecords(safe), members, online: 0, interactive: 0, relayPkg: this.upgradeHold ?? VERSION, now, relayAgeMs: now - this.startedAt }));
1540
1594
  }
1541
- return out;
1595
+ return { rows: out, unreadable };
1542
1596
  }
1543
1597
  async usageOfAll(days, only) {
1544
1598
  const out = [];
@@ -1574,92 +1628,10 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1574
1628
  }
1575
1629
  return out.sort((a, b) => a.space.localeCompare(b.space));
1576
1630
  }
1577
- spacesOverview() {
1578
- const now = Date.now();
1579
- const out = new Map();
1580
- const safeOf = (name) => name.replace(/[^a-z0-9._-]/gi, "_");
1581
- const size = (safe) => [".jsonl", ".state.json", ".members.json", ".usage.json"].reduce((n, suf) => { try {
1582
- return n + statSync(join(this.dataDir, `${safe}${suf}`)).size;
1583
- }
1584
- catch {
1585
- return n;
1586
- } }, 0);
1587
- for (const sp of this.spaces.values()) {
1588
- const name = sp.name;
1589
- out.set(safeOf(name), {
1590
- name, live: true, id: sp.allow.peekId(),
1591
- members: sp.allow.all().filter((e) => e.admitted).length, online: [...sp.members.values()].filter((m) => presentSessions(m).length > 0).length,
1592
- acts: sp.ledger.head, goals: sp.goals.size, open_goals: [...sp.goals.values()].filter((g) => g.state !== "CLOSED").length, obligations: sp.obligations.size,
1593
- last: sp.lastTouch, bytes: size(safeOf(name)),
1594
- activity: activity(sp.ledger.all(), now, (r) => `${name}/${r.from}`), refusals: sp.store.refusals,
1595
- });
1596
- }
1597
- let files = [];
1598
- try {
1599
- files = readdirSync(this.dataDir);
1600
- }
1601
- catch { }
1602
- for (const f of files) {
1603
- if (!f.endsWith(".members.json"))
1604
- continue;
1605
- const safe = f.slice(0, -".members.json".length);
1606
- if (out.has(safe))
1607
- continue;
1608
- let members = 0, last = null, id = null;
1609
- try {
1610
- const al = new Allowlist(this.dataDir, safe);
1611
- members = al.all().filter((e) => e.admitted).length;
1612
- id = al.peekId();
1613
- }
1614
- catch { }
1615
- try {
1616
- last = statSync(join(this.dataDir, `${safe}.jsonl`)).mtimeMs;
1617
- }
1618
- catch {
1619
- try {
1620
- last = statSync(join(this.dataDir, f)).mtimeMs;
1621
- }
1622
- catch { }
1623
- }
1624
- const records = this.sleepingRecords(safe);
1625
- const st = this.sleepingState(safe);
1626
- out.set(safe, {
1627
- name: safe, live: false, id, members, online: 0,
1628
- acts: records.length ? records[records.length - 1].seq : 0,
1629
- goals: st.goals.size, open_goals: [...st.goals.values()].filter((g) => g.state !== "CLOSED").length, obligations: 0,
1630
- last, bytes: size(safe),
1631
- activity: activity(records, now, (r) => `${safe}/${r.from}`), refusals: st.refusals,
1632
- });
1633
- }
1634
- return [...out.values()].sort((a, b) => (b.last ?? 0) - (a.last ?? 0));
1635
- }
1636
- sleepingCache = new Map();
1637
- sleepingRecords(safe) {
1638
- const file = join(this.dataDir, `${safe}.jsonl`);
1639
- const pending = pendingView(this.dataDir, safe, file);
1640
- if (pending !== undefined)
1641
- return parseLines(pending?.toString("utf8") ?? "");
1642
- let st;
1643
- try {
1644
- st = statSync(file);
1645
- }
1646
- catch {
1647
- return [];
1648
- }
1649
- const key = `${st.size}:${st.mtimeMs}`;
1650
- const hit = this.sleepingCache.get(safe);
1651
- if (hit && hit.key === key)
1652
- return hit.records;
1653
- const records = parseLines(readFileSync(file, "utf8"));
1654
- this.sleepingCache.set(safe, { key, records });
1655
- if (this.sleepingCache.size > 200)
1656
- this.sleepingCache.delete(this.sleepingCache.keys().next().value);
1657
- return records;
1658
- }
1659
- sleepingState(safe) {
1660
- const st = new StateStore(this.dataDir, safe);
1661
- const { goals } = st.load();
1662
- return { goals, refusals: st.refusals };
1631
+ readBounds = { ledgerBytes: LEDGER_CACHE_BYTES, viewBytes: VIEW_CACHE_BYTES, cold: COLD_MAX, viewInflight: VIEW_INFLIGHT_PER_SESSION };
1632
+ sleepingCache = new ByteLru(() => this.readBounds.ledgerBytes);
1633
+ putLedger(safe, entry) {
1634
+ this.sleepingCache.set(safe, { key: entry.key, records: entry.records }, entry.bytes);
1663
1635
  }
1664
1636
  resolveTeam(typed) {
1665
1637
  const read = (stem) => { try {
@@ -1814,12 +1786,21 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1814
1786
  this.hangUp(s.ws, "SPACE_DELETED");
1815
1787
  this.spaces.delete(spaceKey(sp.name));
1816
1788
  }
1817
- setTimeout(() => this.deleteSpaceFiles(team.stem), 600).unref?.();
1789
+ this.deleting.set(team.stem, performance.now() + 5_000);
1790
+ setTimeout(() => { this.deleteSpaceFiles(team.stem); this.verifyRemoved(team.stem, 0); }, 600).unref?.();
1818
1791
  this.deleteSpaceFiles(team.stem);
1792
+ if (this.accounts) {
1793
+ for (const g of this.accounts.revokeViewersOfTeam(team.name, id, "the team was deleted")) {
1794
+ this.accounts.audit({ event: "viewer_revoked", space: team.name, target: g.email, detail: "the team was deleted" });
1795
+ }
1796
+ this.teamViews.deleteWhere((k) => k.startsWith(`${team.stem.toLowerCase()}|`));
1797
+ this.sleepStates.delete(team.stem);
1798
+ this.sleepingCache.delete(team.stem);
1799
+ }
1819
1800
  return team.name;
1820
1801
  }
1821
- adminAction(res, A, body, ip) {
1822
- if (body.csrf !== this.adminCsrf())
1802
+ adminAction(res, A, body, ip, csrf) {
1803
+ if (!secretEq(body.csrf ?? "", csrf))
1823
1804
  return this.json(res, 403, { error: "stale form — reload the dashboard and try again" });
1824
1805
  const act = body.action ?? "", id = (body.id ?? "").trim();
1825
1806
  const writes = recordWrites();
@@ -1964,6 +1945,19 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1964
1945
  A.audit({ event: "admin_set_plan_limits", target: id, detail: `${prev ? JSON.stringify(prev) : "(new plan)"} → ${JSON.stringify(lim)}`, ip });
1965
1946
  return done(`${id} plan: ${fmtLimit(lim.spaces)} spaces · ${fmtLimit(lim.members)} people · ${fmtLimit(inviteLimit(lim, id))} open invite links · unattended workers ${lim.wake ? "on" : "off"} — applies to the next create, invite or admit`);
1966
1947
  }
1948
+ case "quarantine_leftover": {
1949
+ const stem = body.id ?? "";
1950
+ if ((body.confirm ?? "") !== stem)
1951
+ return done(`not moved: type the file name exactly ("${stem}") to confirm`);
1952
+ const r = this.quarantineLeftover(stem, `by the operator from ${ip}`);
1953
+ if (!r.ok) {
1954
+ if (r.moved !== undefined)
1955
+ A.audit({ event: r.moved.length ? "admin_quarantine_partial" : "admin_quarantine_failed", space: stem, detail: `${r.error}${r.moved.length ? ` (moved: ${r.moved.join(", ")}; put back: ${r.rolledBack ? "yes" : "NO"})` : ""}; reads ${r.state ?? "?"}`, ip });
1956
+ return done(`not moved: ${r.error}`);
1957
+ }
1958
+ A.audit({ event: "admin_quarantine", space: id, detail: `${r.moved.join(", ")} → ${r.to}`, ip });
1959
+ return done(`moved ${r.moved.length} file${r.moved.length === 1 ? "" : "s"} of "${id}" to ${r.to}`);
1960
+ }
1967
1961
  case "delete_plan": {
1968
1962
  if (id === "free")
1969
1963
  return this.json(res, 400, { error: "the free plan is every account's default and cannot be removed" });
@@ -1978,6 +1972,57 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
1978
1972
  A.audit({ event: "admin_delete_plan", target: id, ip });
1979
1973
  return done(`plan "${id}" removed`);
1980
1974
  }
1975
+ case "grant_viewer": {
1976
+ const back = (msg) => this.replyLanded(res, writes, () => { res.writeHead(303, { location: `/admin/space?name=${encodeURIComponent(id)}&done=${encodeURIComponent(msg)}#viewers` }); res.end(); });
1977
+ const live0 = this.live(id);
1978
+ const held = this.leftoverState(safeName(id));
1979
+ if (held.state !== "live")
1980
+ return back(`not granted: "${id}" has files under a deleted name (${held.state} — ${held.why}). Sort that out first (Leftover files of deleted teams, on the overview).`);
1981
+ const al0 = live0 ? live0.allow : this.sleepingOnDisk(id, safeName(id)) ? this.rosterOf(id) : null;
1982
+ if (!al0)
1983
+ return back(`no team "${id}" on this relay`);
1984
+ const ts = { name: live0?.name ?? al0.recordedName() ?? id, sp: live0 ?? null, allow: al0 };
1985
+ const email = normalizeEmail(body.email ?? "");
1986
+ if (!validEmail(email))
1987
+ return back("that is not an email address");
1988
+ const days = Math.min(VIEWER_MAX_DAYS, Math.max(1, Math.floor(Number(body.days) || VIEWER_DEFAULT_DAYS)));
1989
+ let spaceId = null;
1990
+ try {
1991
+ spaceId = (ts.sp?.allow ?? ts.allow).spaceId();
1992
+ }
1993
+ catch {
1994
+ spaceId = ts.allow.peekId();
1995
+ }
1996
+ if (!spaceId)
1997
+ return back(`"${ts.name}" could not be opened to give access; try again`);
1998
+ const g = A.grantViewer({ space: ts.name, spaceId, email, days, grantedBy: "operator" });
1999
+ if (!g)
2000
+ return back("that is not an email address");
2001
+ const until = new Date(g.expires_at).toUTCString().slice(5, 16);
2002
+ A.audit({ event: "viewer_granted", space: ts.name, target: email, detail: `${days} days, until ${new Date(g.expires_at).toISOString().slice(0, 10)}, by the operator`, ip });
2003
+ this.noteViewerChange(ts.name, `${email} can now see this team's hand-offs, read-only (who asked whom, when, and what waits on whom; no message text, code or files). Given by the relay's operator, until ${until}.`, { email, state: "granted", until: g.expires_at });
2004
+ if (this.email.sendNotice)
2005
+ void this.email.sendNotice(email, `You can now see how the team "${ts.name}" works on Pantheon`, `The operator of the Pantheon relay ${this.opts.host ?? ""} gave you read-only access to the team "${ts.name}" until ${until}.
2006
+
2007
+ You can see who handed work to whom, who picked it up and how long it took. Message text, code and files stay with the team. The team was told.
2008
+
2009
+ Sign in with a code sent to this address at /view on the relay.`).catch(() => false);
2010
+ this.log(`[admin] viewer ${email} granted on ${ts.name} for ${days}d from ${ip}`);
2011
+ return back(`${email} can see ${ts.name} until ${until}; the team was told`);
2012
+ }
2013
+ case "revoke_viewer": {
2014
+ const g = A.viewerGrant(id);
2015
+ const back = (msg) => this.replyLanded(res, writes, () => { res.writeHead(303, { location: `/admin/space?name=${encodeURIComponent(g?.space ?? body.space ?? "")}&done=${encodeURIComponent(msg)}#viewers` }); res.end(); });
2016
+ if (!g)
2017
+ return back("no such access");
2018
+ const r = A.revokeViewer(id, "operator");
2019
+ if (!r)
2020
+ return back(`${g.email}'s access had already ended`);
2021
+ A.audit({ event: "viewer_revoked", space: g.space, target: g.email, detail: "by the operator", ip });
2022
+ this.noteViewerChange(g.space, `${g.email} can no longer see this team's hand-offs: the relay's operator ended the access.`, { email: g.email, state: "revoked" });
2023
+ this.log(`[admin] viewer ${g.email} revoked on ${g.space} from ${ip}`);
2024
+ return back(`${g.email} can no longer see ${g.space}`);
2025
+ }
1981
2026
  default: return this.json(res, 400, { error: `unknown action "${act}"` });
1982
2027
  }
1983
2028
  }
@@ -2055,11 +2100,17 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2055
2100
  }
2056
2101
  }
2057
2102
  ensureToken(sp) {
2103
+ if (!sp.allow.hasToken)
2104
+ throw new UnboundTeam(sp.name);
2058
2105
  if (!this.knownToken(sp))
2059
2106
  this.rotateSpaceToken(sp, "its current token was not in memory");
2060
2107
  return sp.token;
2061
2108
  }
2062
2109
  rotateSpaceToken(sp, why) {
2110
+ if (!sp.allow.hasToken) {
2111
+ this.log(`[${sp.name}] ! token not rotated (${why}): the team has none bound yet`);
2112
+ return;
2113
+ }
2063
2114
  const next = randomBytes(16).toString("hex");
2064
2115
  sp.allow.rotateToken(next);
2065
2116
  sp.token = next;
@@ -2080,8 +2131,12 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2080
2131
  return live;
2081
2132
  if (!name || this.aliasOf(name) || !this.spaceOnDisk(name))
2082
2133
  return null;
2134
+ if (this.leftoverState(safeName(name)).state !== "live")
2135
+ return null;
2083
2136
  try {
2084
2137
  const allow = new Allowlist(this.dataDir, safeName(name));
2138
+ if (!allow.hasToken)
2139
+ return null;
2085
2140
  let token = allow.currentTokenFromInvites();
2086
2141
  if (!token) {
2087
2142
  token = randomBytes(16).toString("hex");
@@ -2123,6 +2178,8 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2123
2178
  const stem = f.slice(0, -".members.json".length);
2124
2179
  if (seen.has(stem))
2125
2180
  continue;
2181
+ if (this.leftoverState(stem).state !== "live")
2182
+ continue;
2126
2183
  try {
2127
2184
  const m = busyWriter(this.dataDir, stem) ? -1 : statSync(join(this.dataDir, f)).mtimeMs;
2128
2185
  let c = this.rosterCache.get(stem);
@@ -2178,10 +2235,17 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2178
2235
  if (!name)
2179
2236
  return { status: 400, body: { error: "which team? pantheon recover <team>" } };
2180
2237
  const noSeat = { status: 404, body: { reason: "no_seat", error: `you (${u}) have no seat in a team called "${name}" on this relay — pantheon recover lists the ones you have` } };
2238
+ if (this.live(name) || this.spaceOnDisk(name)) {
2239
+ const ls = this.leftoverState(safeName(name));
2240
+ if (ls.state !== "live")
2241
+ return { status: 410, body: { reason: "deleted", error: ls.state === "covered" ? coveredNameWords(name) : `a seat in "${name}" cannot be moved now: a team of this name was deleted on this relay and the relay cannot tell this one apart from it — ask the relay operator` } };
2242
+ }
2181
2243
  const roster = this.rosterOf(name);
2182
2244
  const seat = roster?.get(u);
2183
2245
  if (!roster || !seat?.admitted)
2184
2246
  return noSeat;
2247
+ if (!roster.hasToken)
2248
+ return { status: 409, body: { reason: "unbound", error: `"${name}" has no team token yet (it was made by an old relay): a member has to connect to it once from a machine that already has it, then the seat can be moved` } };
2185
2249
  const known = seat.publicKey === device.pubkey || roster.recoveryOf(u)?.pubkey === device.pubkey;
2186
2250
  if (!known) {
2187
2251
  const refused = this.recoveryProofRefusal(A, acct, device, name, seat, roster.founder() === u);
@@ -2437,6 +2501,157 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2437
2501
  }
2438
2502
  return this.tombstoneStems;
2439
2503
  }
2504
+ leftoverState(stem) {
2505
+ const t = this.tombstoneRecord(stem);
2506
+ const view = t ? {
2507
+ at: t.at, sealed: !!t.sealed, corrupt: !!t.corrupt, unreadable: t.unreadable ?? null,
2508
+ teams: t.corrupt || t.unreadable ? [] : tombTeams(t).map((x) => ({ at: x.at, hashes: x.hashes, ...(x.id ? { id: x.id } : {}) })),
2509
+ } : null;
2510
+ let r = classifyLeftover(this.dataDir, stem, view);
2511
+ const until = this.deleting.get(stem);
2512
+ if (until !== undefined && performance.now() < until && r.state === "unsure" && /^quarantine or delete incomplete/.test(r.why))
2513
+ r = { stem, state: "live", why: "being deleted" };
2514
+ if (r.state !== "live" && !this.leftoverSaid.has(`${stem}|${r.state}`)) {
2515
+ this.leftoverSaid.add(`${stem}|${r.state}`);
2516
+ this.log(`! "${stem}" has a member list under a deleted name: ${r.state} — ${r.why}. Its invites admit nobody${r.state === "covered" ? ", and it is not opened" : ""}; nothing is removed (the operator can move it to quarantine on /admin).`);
2517
+ }
2518
+ return r;
2519
+ }
2520
+ leftoverSaid = new Set();
2521
+ deleting = new Map();
2522
+ leftovers() {
2523
+ let files = [];
2524
+ try {
2525
+ files = readdirSync(this.dataDir);
2526
+ }
2527
+ catch {
2528
+ return [];
2529
+ }
2530
+ const tombs = new Set(files.filter((f) => f.endsWith(".deleted.json")).map((f) => f.slice(0, -".deleted.json".length).toLowerCase()));
2531
+ return teamStems(files)
2532
+ .filter((st) => tombs.has(st.toLowerCase()) || this.deletedSpaces.has(st))
2533
+ .map((st) => this.leftoverState(st)).filter((l) => l.state !== "live").sort((a, b) => a.stem.localeCompare(b.stem));
2534
+ }
2535
+ quarantineLeftover(stem, why) {
2536
+ if (!stem || safeName(stem) !== stem || /^\.+$/.test(stem))
2537
+ return { ok: false, error: "not a team's file name" };
2538
+ if (this.quarantining.has(stem))
2539
+ return { ok: false, error: `"${stem}" is already being moved to quarantine` };
2540
+ const st = this.leftoverState(stem);
2541
+ if (st.state === "live") {
2542
+ let done;
2543
+ try {
2544
+ done = readdirSync(join(this.dataDir, "quarantine")).filter((d) => d.startsWith(`${stem}-`)).sort().pop();
2545
+ }
2546
+ catch { }
2547
+ const nothing = !TEAM_FILES.some((x) => existsSync(join(this.dataDir, `${stem}${x}`)));
2548
+ return { ok: false, error: done && nothing ? `"${stem}" was already moved to quarantine/${done}` : `"${stem}" reads as a live team (${st.why}): not moved` };
2549
+ }
2550
+ if (this.spaces.get(spaceKey(stem)))
2551
+ return { ok: false, error: `"${stem}" has members connected right now: not moved` };
2552
+ let rosterId = null, rosterName = null;
2553
+ try {
2554
+ const j = JSON.parse(readFileSync(join(this.dataDir, `${stem}.members.json`), "utf8"));
2555
+ rosterId = typeof j?.id === "string" ? j.id : null;
2556
+ rosterName = typeof j?.name === "string" ? j.name : null;
2557
+ }
2558
+ catch { }
2559
+ this.quarantining.add(stem);
2560
+ let moved;
2561
+ try {
2562
+ moved = this.moveToQuarantine(stem);
2563
+ }
2564
+ finally {
2565
+ this.quarantining.delete(stem);
2566
+ }
2567
+ if (!moved.ok) {
2568
+ const after = this.leftoverState(stem).state;
2569
+ if (after !== st.state)
2570
+ this.log(`! QUARANTINE LEFT "${stem}" READING ${after.toUpperCase()} (was ${st.state}): ${moved.error}`);
2571
+ else
2572
+ this.log(`! quarantine of "${stem}" failed: ${moved.error}`);
2573
+ return { ...moved, state: after };
2574
+ }
2575
+ this.log(`[admin] leftover files of "${stem}" (${st.state}: ${st.why}) moved to ${moved.to} — ${why}`);
2576
+ if (this.accounts) {
2577
+ for (const g of this.accounts.revokeViewersOfTeam(rosterName ?? stem, rosterId, "its files were moved to quarantine")) {
2578
+ this.accounts.audit({ event: "viewer_revoked", space: g.space, target: g.email, detail: "its files were moved to quarantine" });
2579
+ }
2580
+ }
2581
+ return moved;
2582
+ }
2583
+ quarantining = new Set();
2584
+ moveToQuarantine(stem) {
2585
+ const sufs = [".members.json", ".members.json.tmp", ".jsonl", ".jsonl.tmp", ".state.json", ".state.json.tmp", ".usage.json", ".usage.json.tmp"];
2586
+ let files = [];
2587
+ try {
2588
+ files = readdirSync(this.dataDir);
2589
+ }
2590
+ catch (e) {
2591
+ return { ok: false, error: e.code ?? "unreadable", moved: [], rolledBack: true };
2592
+ }
2593
+ const mine = [
2594
+ ...sufs.flatMap((x) => (files.includes(`${stem}${x}`) ? [`${stem}${x}`] : [])),
2595
+ ...files.filter((f) => f.startsWith(`${stem}.usage.json.corrupt-`)),
2596
+ ];
2597
+ if (!mine.length)
2598
+ return { ok: false, error: `no files of "${stem}" to move`, moved: [], rolledBack: true };
2599
+ const name = `${stem}-${new Date().toISOString().replace(/[:.]/g, "-")}`;
2600
+ const to = join(this.dataDir, "quarantine", name);
2601
+ const moved = [];
2602
+ try {
2603
+ mkdirSync(to, { recursive: true, mode: 0o700 });
2604
+ for (const f of mine) {
2605
+ renameSync(join(this.dataDir, f), join(to, f));
2606
+ moved.push(f);
2607
+ }
2608
+ }
2609
+ catch (e) {
2610
+ const why = e.code ?? "failed";
2611
+ let rolledBack = true;
2612
+ for (const f of [...moved].reverse()) {
2613
+ try {
2614
+ renameSync(join(to, f), join(this.dataDir, f));
2615
+ }
2616
+ catch {
2617
+ rolledBack = false;
2618
+ }
2619
+ }
2620
+ if (rolledBack) {
2621
+ try {
2622
+ rmSync(to, { recursive: true });
2623
+ }
2624
+ catch { }
2625
+ }
2626
+ return { ok: false, error: rolledBack ? `${why} moving ${mine[moved.length]}; nothing moved (what had moved was put back)` : `${why} moving ${mine[moved.length]}, and not everything could be put back — see quarantine/${name}`, moved, rolledBack };
2627
+ }
2628
+ this.teamViews.deleteWhere((k) => k.startsWith(`${stem.toLowerCase()}|`));
2629
+ this.sleepStates.delete(stem);
2630
+ this.sleepingCache.delete(stem);
2631
+ return { ok: true, to: `quarantine/${name}`, moved };
2632
+ }
2633
+ verifyRemoved(stem, attempt) {
2634
+ void writerFor(this.dataDir, stem).whenWritten().then(() => {
2635
+ const left = [".jsonl", ".state.json", ".members.json", ".usage.json"].filter((x) => existsSync(join(this.dataDir, `${stem}${x}`)));
2636
+ if (!left.length)
2637
+ return;
2638
+ const m = this.moveToQuarantine(stem);
2639
+ if (m.ok) {
2640
+ this.log(`! DELETE INCOMPLETE: "${stem}" left ${m.moved.join(", ")} on disk; moved to ${m.to}`);
2641
+ return;
2642
+ }
2643
+ this.log(`! DELETE INCOMPLETE: "${stem}" still has ${left.map((x) => stem + x).join(", ")} on disk after ${attempt + 1} tr${attempt ? "ies" : "y"} (${m.error}) — its invites admit nobody meanwhile`);
2644
+ if (attempt < 2)
2645
+ setTimeout(() => this.verifyRemoved(stem, attempt + 1), 3_000).unref?.();
2646
+ }).catch(() => { });
2647
+ }
2648
+ reportLeftovers() {
2649
+ const l = this.leftovers();
2650
+ if (!l.length)
2651
+ return;
2652
+ const n = (s) => l.filter((x) => x.state === s).length;
2653
+ this.log(`! boot: ${l.length} member list${l.length === 1 ? "" : "s"} under deleted names (${n("covered")} covered, ${n("unsure")} unsure): nothing removed; see /admin (leftovers)`);
2654
+ }
2440
2655
  noteTombstoneStem(stem) {
2441
2656
  const idx = this.tombstoneStems ?? this.tombstoneIndex();
2442
2657
  const k = stem.toLowerCase();
@@ -2666,217 +2881,245 @@ button:hover{background:#1d4ed8}.err{color:var(--bad);margin-top:12px}
2666
2881
  catch { }
2667
2882
  }
2668
2883
  }
2669
- adminDashboard(res, A, url) {
2670
- const c = A.counts();
2671
- const esc = (s) => String(s ?? "").replace(/[&<>"]/g, (m) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[m]));
2672
- const when = (t) => t ? new Date(t).toISOString().slice(0, 19).replace("T", " ") : "—";
2673
- const ago = (t) => { if (!t)
2674
- return "—"; const m = Math.round((Date.now() - t) / 60_000); return m < 1 ? "just now" : m < 60 ? `${m}m ago` : m < 1440 ? `${Math.round(m / 60)}h ago` : `${Math.round(m / 1440)}d ago`; };
2675
- const kb = (n) => n < 1024 ? `${n} B` : n < 1048576 ? `${(n / 1024).toFixed(1)} KB` : `${(n / 1048576).toFixed(1)} MB`;
2676
- const accounts = A.allAccounts();
2677
- const devices = A.allDevices();
2678
- const sessions = A.liveSessions(200);
2679
- const endedToday = A.endedSessionsSince(Date.now() - DAY);
2680
- const audit = A.auditLog({ limit: 300 });
2681
- const blocks = A.allBlocks();
2682
- const verifs = A.recentVerifications(50);
2683
- const spaces = this.spacesOverview();
2684
- const act = mergeActivity(spaces.map((x) => x.activity));
2685
- const dayAgo = Date.now() - DAY;
2686
- const refused24 = spaces.reduce((n, x) => n + refusalsSince(x.refusals, dayAgo), 0);
2687
- const refusedAll = spaces.reduce((n, x) => n + (x.refusals?.total ?? 0), 0);
2688
- const plans = A.plans();
2689
- const planCounts = A.planCounts();
2690
- const owners = A.spaceOwners();
2691
- const ownedCounts = A.ownedCounts();
2692
- const limitHits = A.planLimitHits();
2693
- const acctById = new Map(accounts.map((a) => [a.id, a]));
2694
- const planOptions = (sel) => Object.keys(plans).map((n) => `<option value="${esc(n)}"${n === sel ? " selected" : ""}>${esc(n)}</option>`).join("");
2695
- const devByAcct = new Map();
2696
- for (const d of devices)
2697
- devByAcct.set(d.account_id, (devByAcct.get(d.account_id) ?? 0) + 1);
2698
- const emailCap = Number(process.env.PANTHEON_EMAIL_MAX_PER_HOUR) || 200;
2699
- const emailsHour = this.emailSends.filter((s) => Date.now() - s.at < 3_600_000).length;
2700
- let snapshots = [];
2701
- try {
2702
- snapshots = readdirSync(join(this.dataDir, "backups")).filter((f) => f.startsWith("accounts-") && f.endsWith(".db")).sort();
2884
+ viewHits = new Map();
2885
+ viewIpHits = new Map();
2886
+ viewInflight = new Map();
2887
+ viewDenyHits = new Map();
2888
+ viewAudited = new Map();
2889
+ viewCookie(name, value, maxAge, secure = "; Secure") {
2890
+ return `${name}=${value}; HttpOnly${secure}; SameSite=Strict; Path=/view; Max-Age=${maxAge}`;
2891
+ }
2892
+ hitOk(m, key, windowMs, max) {
2893
+ const now = Date.now();
2894
+ const recent = (m.get(key) ?? []).filter((t) => now - t < windowMs);
2895
+ recent.push(now);
2896
+ m.set(key, recent);
2897
+ if (m.size > 10_000)
2898
+ for (const [k, v] of m) {
2899
+ if (!v.some((t) => now - t < windowMs))
2900
+ m.delete(k);
2901
+ }
2902
+ return recent.length <= max;
2903
+ }
2904
+ onViewHttp(req, res, url, ip) {
2905
+ const A = this.accounts;
2906
+ if (!A)
2907
+ return this.json(res, 404, { error: "not found" });
2908
+ const send = (status, html, nonce, extra = {}) => { res.writeHead(status, { ...pageHeaders(nonce), ...extra }); res.end(html); };
2909
+ const path = url.pathname.replace(/\/+$/, "") || "/view";
2910
+ const https = !!req.socket.encrypted
2911
+ || forwardedProto(req) === "https";
2912
+ const host = String(req.headers.host ?? "").replace(/:\d+$/, "").replace(/^\[|\]$/g, "").toLowerCase();
2913
+ const local = host === "localhost" || host.endsWith(".localhost") || /^127\./.test(host) || host === "::1";
2914
+ const secureFlag = https || local ? "; Secure" : "";
2915
+ if (!https && !local && path !== "/view/signout") {
2916
+ const nonce = newUiNonce();
2917
+ return send(400, renderViewerNeedsHttps({ nonce }), nonce);
2918
+ }
2919
+ const secret = cookieVal(req, "pantheon_view");
2920
+ const session = secret ? A.viewerSession(secret) : null;
2921
+ const csrf = secret ? createHash("sha256").update("pantheon-view-csrf " + secret).digest("hex").slice(0, 32) : "";
2922
+ const tryNonce = () => {
2923
+ const n = cookieVal(req, "pantheon_view_try");
2924
+ if (/^[A-Za-z0-9_-]{16,64}$/.test(n))
2925
+ return { nonce: n, cookie: null };
2926
+ const fresh = randomBytes(18).toString("base64url");
2927
+ return { nonce: fresh, cookie: this.viewCookie("pantheon_view_try", fresh, 1800, secureFlag) };
2928
+ };
2929
+ const signinPage = (status, step, email, error, setCookie = []) => {
2930
+ const nonce = newUiNonce();
2931
+ return send(status, renderViewerSignin({ nonce, step, email, error }), nonce, setCookie.length ? { "set-cookie": setCookie } : {});
2932
+ };
2933
+ if ((path === "/view" || path === "/view/signin") && req.method === "GET") {
2934
+ if (path === "/view" && session) {
2935
+ const grants = A.liveViewerGrants(session.email).filter((g) => this.grantTeam(g));
2936
+ if (grants.length === 1) {
2937
+ res.writeHead(303, { location: `/view/team?name=${encodeURIComponent(grants[0].space)}` });
2938
+ return void res.end();
2939
+ }
2940
+ const nonce = newUiNonce();
2941
+ return send(200, renderViewerHome({ nonce, email: session.email, csrf, teams: grants.map((g) => ({ name: g.space, expiresAt: g.expires_at })) }), nonce);
2942
+ }
2943
+ const t = tryNonce();
2944
+ return signinPage(200, "email", (url.searchParams.get("email") ?? "").slice(0, 254), null, t.cookie ? [t.cookie] : []);
2703
2945
  }
2704
- catch { }
2705
- const lastBackupPull = audit.find((e) => e.event === "admin_backup");
2706
- const upMin = Math.round((Date.now() - this.startedAt) / 60_000);
2707
- const csrf = this.adminCsrf();
2708
- const done = url?.searchParams.get("done");
2709
- const rows = (arr) => arr.join("");
2710
- const form = (action, id, label, extra = "", cls = "") => `<form method=POST action="/admin/action" class=inl onsubmit="return confirmAct(this)"><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value="${action}"><input type=hidden name=id value="${esc(id)}">${extra}<button class="act ${cls}">${label}</button></form>`;
2711
- const paid = Object.entries(planCounts).filter(([n]) => n !== "free").reduce((s, [, v]) => s + v, 0);
2712
- const beating = sessions.filter((s) => Date.now() - s.last_seen <= 15 * 60_000).length;
2713
- const obj24 = countActs(act.by_act_24h, ["OBJECT"]), esc24 = countActs(act.by_act_24h, ESCALATIONS), del24 = countActs(act.by_act_24h, ["DELIVERED"]);
2714
- const staleBackup = !lastBackupPull || Date.now() - lastBackupPull.ts > 36 * 3_600_000;
2715
- const GROUPS = [
2716
- ["Talk", ["NOTE", "ASK", "ANSWER"]], ["Agreements", ["PROPOSE", "ACCEPT", "OBJECT", "CONTRACT", "COMMIT"]],
2717
- ["Delivery", ["DELIVERED"]], ["Needs a human", ["ESCALATE", "HUMAN_REQUIRED"]],
2718
- ["Housekeeping", MESSAGE_ACTS.filter((x) => !["NOTE", "ASK", "ANSWER", "PROPOSE", "ACCEPT", "OBJECT", "CONTRACT", "COMMIT", "DELIVERED", "ESCALATE", "HUMAN_REQUIRED"].includes(x))],
2719
- ];
2720
- const cols = GROUPS.flatMap(([, xs]) => xs);
2721
- const actRow = (label, win, by, refused) => `<tr><td class=dim>${label}</td>${cols.map((x, i) => `<td class="num${GROUPS.some(([, xs]) => xs[0] === x) && i > 0 ? " first" : ""}" data-act="${x}" data-window="${win}">${by[x] ?? 0}</td>`).join("")}<td class="num first${refused ? " tone-warn" : ""}">${refused}</td></tr>`;
2722
- const limitsOf = (a) => A.limitsFor(a);
2723
- const limitText = (l) => `${fmtLimit(l.spaces)} space${l.spaces === 1 ? "" : "s"} · ${fmtLimit(l.members)} ${l.members === 1 ? "person" : "people"} per space · unattended workers ${l.wake ? "on" : "off"}`;
2724
- const html = `<!doctype html><html lang=en><meta charset=utf-8><title>pantheon · admin</title>
2725
- <meta name=viewport content="width=device-width,initial-scale=1">
2726
- ${ADMIN_CSS}
2727
- <header>
2728
- <h1>pantheon · admin</h1>
2729
- <span class=meta>${esc(this.opts.host ?? "relay")} · pkg ${VERSION}${this.upgradeHold ? ` · upgrades held at ${esc(this.upgradeHold)}` : ""} · up ${upMin < 60 ? upMin + "m" : Math.round(upMin / 60) + "h"}</span>
2730
- <span class=links><a href="/admin/export?table=audit&format=csv">export audit CSV</a><a href="/admin/export?table=accounts&format=json">accounts JSON</a><a href="/admin/backup">download backup</a><a href="/admin/logout">sign out</a></span>
2731
- </header>
2732
- <nav class=jump aria-label="Sections"><a href="#s-activity">Activity</a><a href="#s-spaces">Spaces</a><a href="#s-accounts">Accounts</a><a href="#s-plans">Plans</a><a href="#s-sessions">Sessions</a><a href="#s-blocks">Blocked</a><a href="#s-codes">Codes</a><a href="#s-audit">Audit log</a></nav>
2733
- ${done ? `<div class=done role=status>${esc(done)}</div>` : ""}
2734
- <main>
2735
- <div class=kpis>
2736
- <div class=kpi><div class=l>People</div><div class=v>${c.accounts}<small>accounts</small></div><div class=s>${c.devices} devices · ${paid} on a paid plan</div></div>
2737
- <div class=kpi><div class=l>Teams</div><div class=v>${spaces.filter((s) => s.live).length}<small>/ ${spaces.length} live</small></div><div class=s>capacity ${this.limits.maxSpaces} spaces</div></div>
2738
- <div class=kpi><div class=l>Connections</div><div class=v>${beating}<small>/ ${c.live_sessions} open</small></div><div class=s>${endedToday} connection${endedToday === 1 ? "" : "s"} came and went in the last 24 hours</div></div>
2739
- <div class=kpi><div class=l>Messages today</div><div class=v>${act.messages_24h}<small>/ ${act.messages} retained</small></div><div class=s>${act.senders_24h.length} delegates active today · ${act.sessions_24h} sessions</div></div>
2740
- <div class="kpi${obj24 + esc24 ? " warn" : ""}"><div class=l>Needs a look today</div><div class=v>${obj24 + esc24}</div><div class=s>${obj24} objection${obj24 === 1 ? "" : "s"} · ${esc24} escalation${esc24 === 1 ? "" : "s"} · ${del24} deliver${del24 === 1 ? "y" : "ies"}</div></div>
2741
- <div class="kpi${refused24 ? " warn" : ""}"><div class=l>Refused by the relay</div><div class=v><span data-refused="${refusedAll}">${refused24}</span><small>today / ${refusedAll} total</small></div><div class=s>wrong turn, missing reference, foreign repo, plan or rate limit</div></div>
2742
- <div class="kpi${staleBackup ? " warn" : ""}"><div class=l>Last backup</div><div class=v>${lastBackupPull ? ago(lastBackupPull.ts) : "never"}<small>off-host</small></div><div class=s>${snapshots.length} snapshots · ${emailsHour}/${emailCap} emails this hour · ${c.audit} audit events</div></div>
2743
- </div>
2744
-
2745
- <section id=s-activity>
2746
- <div class=sec><h2>Activity</h2><span class=count>every space · today and retained history</span></div>
2747
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-activity>
2748
- <thead>
2749
- <tr><th></th>${GROUPS.map(([g, xs]) => `<th class=group colspan=${xs.length}>${g}</th>`).join("")}<th class="group">Refused</th></tr>
2750
- <tr><th></th>${cols.map((x, i) => `<th class="num${GROUPS.some(([, xs]) => xs[0] === x) && i > 0 ? " first" : ""}">${x}</th>`).join("")}<th class="num first">by relay</th></tr>
2751
- </thead>
2752
- <tbody>${actRow("today", "24h", act.by_act_24h, refused24)}${actRow("retained", "all", act.by_act, refusedAll)}</tbody>
2753
- </table></div>
2754
- <p class=note>Messages are acts delegates sent; arrivals and departures are not counted. Retained is what each space's ledger still holds: the most recent ${LIMITS.ledgerKeepRecent.toLocaleString()} acts plus everything in an open goal.</p>
2755
- </section>
2756
-
2757
- <section id=s-spaces>
2758
- <div class=sec><h2>Spaces</h2><span class=count>${spaces.length} · click a space for its goals, members and every message</span><input type=search aria-label="Filter spaces" placeholder="Filter spaces" data-filter="t-spaces"></div>
2759
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-spaces>
2760
- <thead><tr><th>space</th><th>people</th><th>activity today</th><th>needs a look</th><th>goals</th><th>last activity</th><th class=num>on disk</th><th></th></tr></thead>
2761
- <tbody>${rows(spaces.map((s) => {
2762
- const o = owners.get(s.name) ? acctById.get(owners.get(s.name)) : undefined;
2763
- const obj = s.activity.by_act_24h.OBJECT ?? 0, ref = refusalsSince(s.refusals, dayAgo);
2764
- return `<tr><td class=cell><a href="/admin/space?name=${encodeURIComponent(s.name)}"><code>${esc(s.name)}</code></a> <span class="pill ${s.live ? "ok" : ""}">${s.live ? "live" : "sleeping"}</span><span class=sub>${o ? `owner <code>${esc(o.username)}</code> <span class=pill data-plan="${esc(o.plan)}">${esc(o.plan)}</span>` : "no owner on record — no plan limits apply"}</span></td>`
2765
- + `<td class=cell><b>${s.members}</b> member${s.members === 1 ? "" : "s"}<span class=sub>${s.online} online</span></td>`
2766
- + `<td class=cell><b>${s.activity.messages_24h}</b> message${s.activity.messages_24h === 1 ? "" : "s"}<span class=sub>${s.activity.senders_24h.length} delegate${s.activity.senders_24h.length === 1 ? "" : "s"} · ${s.acts} acts retained</span></td>`
2767
- + `<td class="cell${obj || ref ? " tone-warn" : ""}"><b>${obj}</b> objection${obj === 1 ? "" : "s"} · <b>${ref}</b> refused<span class=sub>${s.live ? `${s.obligations} owed right now` : "asleep"} · ${s.activity.by_act.OBJECT ?? 0} objections, ${s.refusals?.total ?? 0} refused all time</span></td>`
2768
- + `<td class=cell><b>${s.open_goals}</b> open<span class=sub>${s.goals} total</span></td>`
2769
- + `<td class=dim title="${when(s.last)}">${ago(s.last)}</td><td class="num dim">${kb(s.bytes)}</td>`
2770
- + `<td class=actions>${form("delete_space", s.name, "delete", `<input type=hidden name=confirm value="">`, "danger")}</td></tr>`;
2771
- }))}${spaces.length ? "" : "<tr><td colspan=8 class=dim>no spaces yet</td></tr>"}</tbody>
2772
- </table></div>
2773
- <p class=note>Sleeping spaces are on disk but idle and reload on the next connect. Delete is irreversible and disconnects every member of that space.</p>
2774
- </section>
2775
-
2776
- <section id=s-accounts>
2777
- <div class=sec><h2>Accounts</h2><span class=count>${accounts.length}</span><input type=search aria-label="Filter accounts" placeholder="Filter accounts" data-filter="t-accounts"></div>
2778
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-accounts>
2779
- <thead><tr><th>account</th><th>status</th><th>plan</th><th>limits</th><th class=num>spaces</th><th class=num>limit hits</th><th class=num>devices</th><th>created</th><th></th></tr></thead>
2780
- <tbody>${rows(accounts.map((a) => {
2781
- const lim = limitsOf(a);
2782
- const custom = !!a.limits_json;
2783
- return `<tr><td class=cell><code>${esc(a.username)}</code><span class=sub>${esc(a.email)}</span></td>`
2784
- + `<td><span class="pill ${a.status === "suspended" ? "bad" : "ok"}">${esc(a.status)}</span></td>`
2785
- + `<td><form method=POST action="/admin/action" class=inl onsubmit="return confirmAct(this)"><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value=set_plan><input type=hidden name=id value="${esc(a.id)}"><select name=plan aria-label="Plan for ${esc(a.username)}">${planOptions(a.plan)}</select> <button class=act>set</button></form></td>`
2786
- + `<td class=cell>${custom ? `<span class="pill warn">custom</span> ` : ""}${limitText(lim)}<details class=limits><summary>${custom ? "edit custom limits" : "set custom limits"}</summary>`
2787
- + `<form method=POST action="/admin/action" class=limitform><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value=set_limits><input type=hidden name=id value="${esc(a.id)}">`
2788
- + `<label>spaces<input type=number name=spaces min=0 value="${lim.spaces}" aria-label="Spaces for ${esc(a.username)}"></label>`
2789
- + `<label>people per space<input type=number name=members min=1 value="${lim.members}" aria-label="People per space for ${esc(a.username)}"></label>`
2790
- + `<label class=chk><input type=checkbox name=wake value=on${lim.wake ? " checked" : ""}> unattended workers</label>`
2791
- + `<button class=act>save</button>${custom ? `<button class=act name=clear value=1>back to ${esc(a.plan)} plan</button>` : ""}</form></details></td>`
2792
- + `<td class=num>${ownedCounts.get(a.id) ?? 0}<span class=dim> / ${fmtLimit(lim.spaces)}</span></td>`
2793
- + `<td class=num>${limitHits.get(a.id) ? `<span class="pill warn">${limitHits.get(a.id)}</span>` : "<span class=dim>0</span>"}</td>`
2794
- + `<td class=num>${devByAcct.get(a.id) ?? 0}</td><td class=dim>${when(a.created_at)}</td>`
2795
- + `<td class=actions>${a.status === "suspended" ? form("reinstate", a.id, "reinstate") : form("suspend", a.id, "suspend", "", "danger")} ${form("block", a.email, "block email", "", "danger")}</td></tr>`;
2796
- }))}${accounts.length ? "" : "<tr><td colspan=9 class=dim>no accounts yet</td></tr>"}</tbody>
2797
- </table></div>
2798
- <p class=note>Suspending signs the account out everywhere and refuses its handshakes until reinstated; its messages stay in the ledgers. A plan or limits change takes effect at the account's next space, invite or admit; nothing they already have is removed. Limit hits count the times the relay refused them for a plan limit.</p>
2799
- </section>
2800
-
2801
- <section id=s-plans>
2802
- <div class=sec><h2>Plans</h2><span class=count>limits the relay enforces · the owner's plan governs a space</span></div>
2803
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-plans>
2804
- <thead><tr><th>plan</th><th class=num>accounts</th><th>spaces created</th><th>people per space</th><th>open invite links</th><th>unattended workers</th><th></th></tr></thead>
2805
- <tbody>${rows(Object.entries(plans).map(([n, l]) => { const fid = `plan-${esc(n)}`; return `<tr><td><form id="${fid}" method=POST action="/admin/action" class=inl><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value=set_plan_limits><input type=hidden name=id value="${esc(n)}"></form><code>${esc(n)}</code></td><td class=num>${planCounts[n] ?? 0}</td><td><input form="${fid}" name=spaces aria-label="Maximum spaces" type=number min=0 value="${l.spaces}"></td><td><input form="${fid}" name=members aria-label="Maximum people per space" type=number min=1 value="${l.members}"></td><td><input form="${fid}" name=invites aria-label="Open invite links per space" type=number min=0 value="${inviteLimit(l, n)}"></td><td><label><input form="${fid}" type=checkbox name=wake value=on${l.wake ? " checked" : ""}> allowed</label></td><td class=actions><button form="${fid}" class=act>save</button> ${n === "free" ? "" : form("delete_plan", n, "remove", "", "danger")}</td></tr>`; }))}
2806
- <tr><td><form id="plan-new" method=POST action="/admin/action" class=inl><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value=set_plan_limits></form><input form="plan-new" type=text name=id aria-label="New plan name" placeholder="new plan name" pattern="[a-z][a-z0-9_-]{0,23}" required style="width:150px"></td><td class="num dim">—</td><td><input form="plan-new" name=spaces aria-label="Maximum spaces" type=number min=0 value=3></td><td><input form="plan-new" name=members aria-label="Maximum people per space" type=number min=1 value=5></td><td><input form="plan-new" name=invites aria-label="Open invite links per space" type=number min=0 value=5></td><td><label><input form="plan-new" type=checkbox name=wake value=on checked> allowed</label></td><td class=actions><button form="plan-new" class=act>add plan</button></td></tr></tbody>
2807
- </table></div>
2808
- <p class=note>Edits apply immediately to the next create, invite or admit of every account on that plan. Unused invites count toward people per space, so nobody mints five at once. Every change is audited.</p>
2809
- </section>
2810
-
2811
- <section id=s-sessions>
2812
- <div class=sec><h2>Live connections</h2><span class=count>${sessions.filter((s) => !s.revoked_at).length} open · ${endedToday} connection${endedToday === 1 ? "" : "s"} came and went in the last 24 hours</span><input type=search aria-label="Filter sessions" placeholder="Filter connections" data-filter="t-sessions"></div>
2813
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-sessions>
2814
- <thead><tr><th>account</th><th>device</th><th>role</th><th>space</th><th>ip</th><th>started</th><th>last seen</th><th>session</th><th></th></tr></thead>
2815
- <tbody>${rows(sessions.filter((s) => !s.revoked_at).map((s) => `<tr><td><code>${esc(accounts.find((a) => a.id === s.account_id)?.username ?? s.account_id)}</code></td><td class=dim>${esc(devices.find((d) => d.id === s.device_id)?.label ?? s.device_id.slice(0, 10))}</td><td>${s.role ? `<span class=pill>${esc(s.role)}</span>` : "<span class=dim>—</span>"}</td><td>${esc(s.space) || "<span class=dim>—</span>"}</td><td class=dim>${esc(s.ip)}</td><td class=dim>${when(s.created_at)}</td><td class=dim>${ago(s.last_seen)}${Date.now() - s.last_seen > 15 * 60_000 ? ' <span class="pill warn" title="no heartbeat for 15+ minutes — usually a socket that died with a restart">stale</span>' : ""}</td><td><code class=dim>${esc(s.id)}</code></td><td class=actions>${form("revoke_session", s.id, "sign out", "", "danger")} ${form("revoke_device", s.device_id, "remove device", "", "danger")}</td></tr>`))}${sessions.some((s) => !s.revoked_at) ? "" : "<tr><td colspan=9 class=dim>no open connections right now</td></tr>"}</tbody>
2816
- </table></div>
2817
- <p class=note>One row per open connection: an agent window, a worker, or a CLI command while it runs. Rows leave when the socket closes. Stale means no heartbeat for 15 minutes on a socket that has not closed, usually a machine that went to sleep.</p>
2818
- </section>
2819
-
2820
- <section id=s-blocks>
2821
- <div class=sec><h2>Blocked emails and domains</h2><span class=count>${blocks.length}</span></div>
2822
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-blocks>
2823
- <thead><tr><th>pattern</th><th>kind</th><th>since</th><th></th></tr></thead>
2824
- <tbody>${rows(blocks.map((b) => `<tr><td><code>${esc(b.pattern)}</code></td><td>${esc(b.kind)}</td><td class=dim>${when(b.created_at)}</td><td class=actions>${form("unblock", b.pattern, "unblock")}</td></tr>`))}${blocks.length ? "" : "<tr><td colspan=4 class=dim>nothing blocked</td></tr>"}</tbody>
2825
- </table></div>
2826
- <form method=POST action="/admin/action" class=filters style="margin-top:10px"><input type=hidden name=csrf value="${csrf}"><input type=hidden name=action value=block><label>email or domain<input type=text name=id aria-label="Email or domain to block" placeholder="someone@spam.example or spam.example" required style="width:320px"></label><button class=act>block</button></form>
2827
- <p class=note>A blocked email or domain cannot sign up or sign in. Existing accounts are not touched; suspend those separately.</p>
2828
- </section>
2829
-
2830
- <section id=s-codes>
2831
- <div class=sec><h2>Recent verification codes</h2><span class=count>support: "I never got my code"</span></div>
2832
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-verifs>
2833
- <thead><tr><th>email</th><th>purpose</th><th>requested</th><th>expires</th><th class=num>wrong tries</th><th>state</th></tr></thead>
2834
- <tbody>${rows(verifs.map((v) => `<tr><td>${esc(v.email)}</td><td>${esc(v.purpose)}</td><td class=dim>${when(v.created_at)}</td><td class=dim>${when(v.expires_at)}</td><td class=num>${v.attempts}</td><td><span class="pill ${v.expires_at < Date.now() ? "bad" : "ok"}">${v.expires_at < Date.now() ? "expired" : "pending"}</span></td></tr>`))}${verifs.length ? "" : "<tr><td colspan=6 class=dim>no pending codes</td></tr>"}</tbody>
2835
- </table></div>
2836
- <p class=note>A row here means the relay generated a code and handed it to the email provider. No row means the request never reached the relay. Codes themselves are never stored.</p>
2837
- </section>
2838
-
2839
- <section id=s-audit>
2840
- <div class=sec><h2>Audit log</h2><span class=count>latest ${audit.length}</span><input type=search aria-label="Filter audit" placeholder="Filter audit log" data-filter="t-audit"></div>
2841
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-audit>
2842
- <thead><tr><th>time</th><th>event</th><th>space</th><th>target</th><th>detail</th><th>ip</th></tr></thead>
2843
- <tbody>${rows(audit.map((e) => `<tr><td class=dim>${when(e.ts)}</td><td>${e.event.startsWith("admin_") ? `<span class="pill info">${esc(e.event)}</span>` : esc(e.event)}</td><td>${esc(e.space) || ""}</td><td><code>${esc(e.target) || ""}</code></td><td class="dim wrapc">${esc(e.detail) || ""}</td><td class=dim>${esc(e.ip) || ""}</td></tr>`))}${audit.length ? "" : "<tr><td colspan=6 class=dim>nothing yet</td></tr>"}</tbody>
2844
- </table></div>
2845
- </section>
2846
- </main>
2847
- <script>
2848
- document.querySelectorAll("input[data-filter]").forEach(function (inp) {
2849
- var table = document.getElementById(inp.dataset.filter);
2850
- var result = document.createElement("p");
2851
- result.className = "note";
2852
- result.setAttribute("role", "status");
2853
- table.parentElement.insertAdjacentElement("afterend", result);
2854
- inp.addEventListener("input", function () {
2855
- var q = inp.value.trim().toLowerCase(), rowsEl = table.querySelectorAll("tbody tr"), shown = 0;
2856
- for (var i = 0; i < rowsEl.length; i++) {
2857
- var match = (!q || rowsEl[i].textContent.toLowerCase().indexOf(q) > -1);
2858
- var empty = rowsEl[i].querySelector("td[colspan]");
2859
- rowsEl[i].hidden = !match;
2860
- if (match && !empty) shown++;
2861
- }
2862
- result.textContent = q ? (shown ? shown + " matching rows" : "No matches. Try another search or clear the filter.") : "";
2863
- });
2864
- });
2865
- function confirmAct(f) {
2866
- var a = f.querySelector("[name=action]").value, id = f.querySelector("[name=id]").value;
2867
- if (a === "set_plan") return confirm("Move " + f.closest("tr").querySelector("code").textContent + " to the " + f.querySelector("[name=plan]").value + " plan? Takes effect on their next space, invite or admit.");
2868
- if (a === "delete_plan") return confirm('Remove the plan "' + id + '"?');
2869
- if (a === "delete_space") { var typed = prompt("Delete space \\"" + id + "\\" and disconnect its members? Type the name to confirm."); if (typed !== id) return false; f.querySelector("[name=confirm]").value = typed; return true; }
2870
- if (a === "suspend" || a === "revoke_session" || a === "revoke_device" || a === "block") return confirm(a.replace("_", " ") + " " + id + "?");
2871
- return true;
2872
- }
2873
- </script>`;
2874
- res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "referrer-policy": "no-referrer" });
2875
- res.end(html);
2946
+ if (path === "/view/code" && req.method === "POST") {
2947
+ if (!this.authRateOk(ip))
2948
+ return signinPage(429, "email", "", "Too many tries from your network. Wait a minute, then try again.");
2949
+ const started = Date.now();
2950
+ const t = tryNonce();
2951
+ return this.readForm(req, res, (body) => {
2952
+ const email = normalizeEmail(String(body.email ?? "")).slice(0, 254);
2953
+ res.once("finish", () => setImmediate(() => { void this.sendViewerCode(A, email, t.nonce, ip).catch((e) => this.log(`! viewer code: ${e.message}`)); }));
2954
+ setTimeout(() => signinPage(200, "code", email, null, t.cookie ? [t.cookie] : []), Math.max(0, VIEW_CODE_ANSWER_MS - (Date.now() - started)));
2955
+ });
2956
+ }
2957
+ if (path === "/view/verify" && req.method === "POST") {
2958
+ if (!this.authRateOk(ip))
2959
+ return signinPage(429, "code", "", "Too many tries from your network. Wait a minute, then try again.");
2960
+ const n = cookieVal(req, "pantheon_view_try");
2961
+ return this.readForm(req, res, (body) => {
2962
+ const email = normalizeEmail(String(body.email ?? "")).slice(0, 254);
2963
+ const ok = validEmail(email) && A.verifyViewerCode(email, String(body.code ?? ""), n, this.localTrusted(ip) ? null : netOf(ip));
2964
+ if (!ok || !A.liveViewerGrants(email).length)
2965
+ return signinPage(401, "code", email, "That code did not work. Check it, or ask for a new one.");
2966
+ const s = A.createViewerSession(email, ip);
2967
+ A.audit({ event: "viewer_signin", target: email, ip });
2968
+ res.writeHead(303, { location: "/view", "set-cookie": [this.viewCookie("pantheon_view", s, VIEWER_SESSION_MAX_MS / 1000, secureFlag), this.viewCookie("pantheon_view_try", "", 0, secureFlag)], "cache-control": "no-store" });
2969
+ res.end();
2970
+ });
2971
+ }
2972
+ if (path === "/view/signout" && (req.method === "POST" || req.method === "GET")) {
2973
+ if (secret)
2974
+ A.endViewerSession(secret);
2975
+ const done = () => { res.writeHead(303, { location: "/view/signin", "set-cookie": this.viewCookie("pantheon_view", "", 0, secureFlag), "cache-control": "no-store" }); res.end(); };
2976
+ if (req.method === "GET")
2977
+ return done();
2978
+ return this.readForm(req, res, () => done());
2979
+ }
2980
+ if (path === "/view/team" && req.method === "GET") {
2981
+ if (!session) {
2982
+ res.writeHead(303, { location: "/view/signin" });
2983
+ return void res.end();
2984
+ }
2985
+ if (!this.hitOk(this.viewHits, session.idHash, 60_000, 60) || !this.hitOk(this.viewIpHits, ip, 3_600_000, 600)) {
2986
+ res.writeHead(429, { "content-type": "text/plain; charset=utf-8", "retry-after": "60", "cache-control": "no-store" });
2987
+ return void res.end("Too many page loads. Wait a minute, then reload.");
2988
+ }
2989
+ for (const g of A.takeExpiredViewerGrants(session.email))
2990
+ A.audit({ event: "viewer_expired", space: g.space, target: g.email, detail: `granted ${new Date(g.granted_at).toISOString().slice(0, 10)}`, ip });
2991
+ const want = url.searchParams.get("name") ?? "";
2992
+ const grant = A.liveViewerGrants(session.email).find((g) => g.space === want);
2993
+ const ts0 = grant ? this.grantTeam(grant) : false;
2994
+ const nonce = newUiNonce();
2995
+ if (!grant || !ts0) {
2996
+ if (!this.hitOk(this.viewDenyHits, session.idHash, 60_000, 10)) {
2997
+ res.writeHead(429, { "content-type": "text/plain; charset=utf-8", "retry-after": "60", "cache-control": "no-store" });
2998
+ return void res.end("Too many requests for teams you cannot see. Wait a minute.");
2999
+ }
3000
+ const dk = `deny|${session.idHash}`;
3001
+ if (Date.now() - (this.viewAudited.get(dk) ?? 0) > 10 * 60_000) {
3002
+ this.viewAudited.set(dk, Date.now());
3003
+ A.audit({ event: "viewer_denied", space: null, target: session.email, detail: `asked for "${printableLine(want, 60)}": ${grant ? "that team is gone or was replaced" : "no live access"}`, ip });
3004
+ }
3005
+ return send(403, renderViewerEnded({ nonce, email: session.email, csrf }), nonce);
3006
+ }
3007
+ const days = rangeOf(url.searchParams.get("range"));
3008
+ const now = Date.now();
3009
+ const inflight = this.viewInflight.get(session.idHash) ?? 0;
3010
+ if (inflight >= this.readBounds.viewInflight) {
3011
+ const tries = Math.max(0, Math.min(99, Math.floor(Number(url.searchParams.get("busy")) || 0)));
3012
+ return send(429, renderViewerBusy({ nonce, email: session.email, csrf, team: grant.space, days, tries }), nonce, { "retry-after": "2", "cache-control": "no-store" });
3013
+ }
3014
+ this.viewInflight.set(session.idHash, inflight + 1);
3015
+ res.once("close", () => { const n = (this.viewInflight.get(session.idHash) ?? 1) - 1; if (n > 0)
3016
+ this.viewInflight.set(session.idHash, n);
3017
+ else
3018
+ this.viewInflight.delete(session.idHash); });
3019
+ void this.teamViewAsync(grant.space, days, now).then(async (v) => {
3020
+ await this.renderTurn();
3021
+ if (res.destroyed)
3022
+ return;
3023
+ if (!v)
3024
+ return send(403, renderViewerEnded({ nonce, email: session.email, csrf }), nonce);
3025
+ const ak = `${session.idHash}|${spaceKey(grant.space)}`;
3026
+ if (now - (this.viewAudited.get(ak) ?? 0) > 10 * 60_000) {
3027
+ this.viewAudited.set(ak, now);
3028
+ if (this.viewAudited.size > 10_000)
3029
+ this.viewAudited.delete(this.viewAudited.keys().next().value);
3030
+ A.audit({ event: "viewer_view", space: grant.space, target: session.email, detail: `hand-offs, ${days === 1 ? "24 hours" : `${days} days`}`, ip });
3031
+ }
3032
+ A.noteViewerView(grant.id);
3033
+ const series14 = v.series14.map((d, i, a) => ({ ts: Date.parse(`${d.day}T12:00:00Z`), worker: d.worker, person: d.person, today: i === a.length - 1 }));
3034
+ send(200, renderViewerTeam({ nonce, csrf, email: session.email, team: grant.space, grantedBy: grant.granted_by, grantedAt: grant.granted_at, expiresAt: grant.expires_at, days, flow: v.flow, series14, stateNote: v.stateNote, live: v.live, asOf: v.asOf, now: Date.now() }), nonce);
3035
+ }).catch((e) => {
3036
+ const code = unreadableCode(e);
3037
+ if (code && !res.headersSent)
3038
+ return send(503, renderViewerUnreadable({ nonce, email: session.email, csrf, team: grant.space, code }), nonce, { "retry-after": "30" });
3039
+ this.log(`! viewer page failed: ${e.message}`);
3040
+ if (!res.headersSent)
3041
+ this.json(res, 500, { error: "the page could not be made" });
3042
+ });
3043
+ return;
3044
+ }
3045
+ return this.json(res, 404, { error: "not found" });
2876
3046
  }
2877
- adminSpace(res, url, name) {
2878
- const safe = name.replace(/[^a-z0-9._-]/gi, "_");
3047
+ bindViewerGrants() {
3048
+ const A = this.accounts;
3049
+ for (const g of A.unboundViewerGrants()) {
3050
+ const al = this.sleepingOnDisk(g.space, safeName(g.space)) ? this.rosterOf(g.space) : null;
3051
+ const tomb = this.tombstoneRecord(g.space);
3052
+ const born = al ? al.all().reduce((min, e) => Math.min(min, e.first_seen), Infinity) : Infinity;
3053
+ const deletedSince = !!tomb && !tomb.corrupt && !tomb.unreadable && tomb.at >= g.granted_at;
3054
+ const id = al && (al.recordedName() ?? g.space) === g.space && born <= g.granted_at && !deletedSince ? al.peekId() : null;
3055
+ if (id) {
3056
+ A.bindViewerGrant(g.id, id);
3057
+ continue;
3058
+ }
3059
+ A.revokeViewer(g.id, "the relay: its team could not be found");
3060
+ A.audit({ event: "viewer_revoked", space: g.space, target: g.email, detail: "its team could not be found when grants were bound to team ids" });
3061
+ }
3062
+ }
3063
+ grantTeam(g) {
3064
+ if (!g.space_id)
3065
+ return false;
3066
+ const live = this.live(g.space);
3067
+ if (live)
3068
+ return live.name === g.space && live.allow.peekId() === g.space_id;
3069
+ if (!this.sleepingOnDisk(g.space, safeName(g.space)))
3070
+ return false;
3071
+ const al = this.rosterOf(g.space);
3072
+ return !!al && (al.recordedName() ?? g.space) === g.space && al.peekId() === g.space_id;
3073
+ }
3074
+ async sendViewerCode(A, email, nonce, ip) {
3075
+ if (!validEmail(email) || !A.liveViewerGrants(email).length)
3076
+ return;
3077
+ if (this.emailRefusal("login", email, ip, `viewer:${nonce}`, false))
3078
+ return;
3079
+ const started = A.startViewerCode(email, nonce);
3080
+ if (!started.ok)
3081
+ return;
3082
+ const text = `Your code to see your team's work on Pantheon (read-only) is ${started.code}. It works for 10 minutes, in the browser that asked for it.\n\nIf you did not ask for it, ignore this email. Nobody can sign in without the code.`;
3083
+ const sent = this.email.sendNotice
3084
+ ? await this.email.sendNotice(email, `Your Pantheon team view code: ${started.code}`, text)
3085
+ : await this.email.sendCode(email, started.code, "login");
3086
+ if (sent)
3087
+ this.noteEmailSent("login", email, ip, `viewer:${nonce}`, false);
3088
+ A.audit({ event: "viewer_code_sent", target: email, ip });
3089
+ }
3090
+ noteViewerChange(name, text, viewer) {
3091
+ const sp = this.live(name) ?? (!name || this.aliasOf(name) || !this.spaceOnDisk(name) || this.leftoverState(safeName(name)).state !== "live" ? null : this.getOrCreateSpace(name, "", { relayOnly: true }));
3092
+ if (!sp)
3093
+ return false;
3094
+ this.commit(sp, { from: RELAY_MEMBER, act: "NOTE", to: null, goal: null, ref: null, body: text, meta: { notice: "viewer_access", viewer } });
3095
+ this.teamViews.deleteWhere((k) => k.startsWith(`${safeName(sp.name).toLowerCase()}|`));
3096
+ return true;
3097
+ }
3098
+ async teamStateAsync(name) {
3099
+ const safe = safeName(name);
2879
3100
  const sp = this.spaces.get(spaceKey(name)) ?? null;
3101
+ if (sp)
3102
+ return this.liveTeamState(sp);
3103
+ if (!this.sleepingOnDisk(name, safe))
3104
+ return null;
3105
+ const sig = await this.teamFilesSig(safe);
3106
+ const records = await this.ledgerRecordsAsync(safe);
3107
+ const hit = this.sleepStates.get(safe);
3108
+ if (hit && hit.sig === sig)
3109
+ return { ...hit.st, records };
3110
+ let byId = null;
3111
+ const needsIndex = this.sleepingNeedsReplay(safe, records);
3112
+ if (needsIndex)
3113
+ byId = await this.indexInSlices(records);
3114
+ return this.sleepingTeamState(name, safe, records, sig, () => byId ?? new Map());
3115
+ }
3116
+ liveTeamState(sp) {
3117
+ const rec = sp.ledger.all();
3118
+ return { name: sp.name, safe: safeName(sp.name), sp, records: rec, goals: sp.goals, obligations: sp.obligations, firstClaims: sp.store.firstClaims, usage: sp.store.usage, refusals: sp.store.refusals, allow: sp.allow, compactedThrough: maxOrNull(sp.store.compactedThrough, compactedFromGaps(rec)), stateNote: null };
3119
+ }
3120
+ sleepingOnDisk(name, safe) {
3121
+ if (this.leftoverState(safe).state === "covered")
3122
+ return false;
2880
3123
  const rec = this.tombstoneRecord(name);
2881
3124
  const tomb = rec && !rec.corrupt && !rec.unreadable ? rec.at : undefined;
2882
3125
  const onDisk = [".jsonl", ".state.json", ".members.json"].some((suf) => {
@@ -2884,172 +3127,639 @@ ${done ? `<div class=done role=status>${esc(done)}</div>` : ""}
2884
3127
  const queued = pendingView(this.dataDir, safe, f);
2885
3128
  return queued !== undefined ? queued !== null : existsSync(f);
2886
3129
  });
2887
- if (!name || (!sp && (!onDisk || (tomb && Date.now() - tomb < 3_600_000))))
2888
- return this.json(res, 404, { error: `no space "${name}" on this relay` });
2889
- const records = sp ? sp.ledger.all() : this.sleepingRecords(safe);
2890
- const state = sp ? { goals: sp.goals, refusals: sp.store.refusals } : this.sleepingState(safe);
2891
- const allow = sp ? sp.allow : new Allowlist(this.dataDir, safe);
2892
- const now = Date.now(), dayAgo = now - DAY;
2893
- const a = activity(records, now);
2894
- const esc = escHtml, when = fmtWhen, ago = fmtAgo;
2895
- const q = {
2896
- act: url.searchParams.get("act") ?? "", member: url.searchParams.get("member") ?? "", goal: url.searchParams.get("goal") ?? "",
2897
- q: url.searchParams.get("q") ?? "", presence: url.searchParams.get("presence") === "1", before: Number(url.searchParams.get("before")) || undefined,
3130
+ return !!name && onDisk && !(tomb && Date.now() - tomb < 3_600_000);
3131
+ }
3132
+ sleepStates = new Map();
3133
+ sleepingNeedsReplay(safe, records) {
3134
+ try {
3135
+ const st = new StateStore(this.dataDir, safe);
3136
+ const l = st.load({ expectHistory: records.length > 0 });
3137
+ const head = records.length ? records[records.length - 1].seq : 0;
3138
+ return !l.unclean && !l.rebuilt && l.ledgerHead !== undefined && head > l.ledgerHead && head - l.ledgerHead <= SLEEPING_REPLAY_MAX;
3139
+ }
3140
+ catch {
3141
+ return false;
3142
+ }
3143
+ }
3144
+ async indexInSlices(records) {
3145
+ const m = new Map();
3146
+ for (let i = 0; i < records.length; i++) {
3147
+ const r = records[i];
3148
+ if (!m.has(r.id))
3149
+ m.set(r.id, r);
3150
+ if (i % 20_000 === 19_999)
3151
+ await new Promise((x) => setImmediate(x));
3152
+ }
3153
+ return m;
3154
+ }
3155
+ sleepingTeamState(name, safe, records, sig, index) {
3156
+ const st = new StateStore(this.dataDir, safe);
3157
+ const loaded = st.load({ expectHistory: records.length > 0 });
3158
+ const allow = new Allowlist(this.dataDir, safe);
3159
+ const teamName = allow.recordedName() ?? records.find((r) => r.space)?.space ?? name;
3160
+ let savedAt = null;
3161
+ try {
3162
+ savedAt = statSync(st.file).mtimeMs;
3163
+ }
3164
+ catch { }
3165
+ const woken = this.sleepingAsWoken(teamName, records, allow, loaded, savedAt, index);
3166
+ const out = {
3167
+ name: teamName, safe, sp: null, goals: woken.goals, obligations: woken.obligations, firstClaims: st.firstClaims, usage: st.usage, refusals: st.refusals, allow,
3168
+ compactedThrough: maxOrNull(st.compactedThrough, compactedFromGaps(records)),
3169
+ stateNote: woken.note,
2898
3170
  };
2899
- const { rows: feedRows, next } = feed(records, { ...q, limit: 100 });
2900
- const filtered = !!(q.act || q.member || q.goal || q.q || q.presence);
2901
- const link = (extra) => {
2902
- const p = new URLSearchParams({ name });
2903
- for (const [k, v] of Object.entries({ act: q.act, member: q.member, goal: q.goal, q: q.q, presence: q.presence ? "1" : "", ...extra }))
2904
- if (v !== undefined && String(v) !== "")
2905
- p.set(k, String(v));
2906
- return `/admin/space?${p.toString()}`;
3171
+ this.sleepStates.set(safe, { sig, st: out });
3172
+ if (this.sleepStates.size > 1_000)
3173
+ this.sleepStates.delete(this.sleepStates.keys().next().value);
3174
+ return { ...out, records };
3175
+ }
3176
+ memberLive(ts, now = Date.now()) {
3177
+ const founder = ts.allow.founder();
3178
+ const relayPkg = this.upgradeHold ?? VERSION;
3179
+ return ts.allow.all().filter((e) => !e.revoked_at).map((e) => {
3180
+ const m = ts.sp?.members.get(e.member);
3181
+ const present = m ? presentSessions(m) : [];
3182
+ const windows = present.filter((s) => s.role === "interactive").length;
3183
+ const work = m && windows ? this.work(m) : undefined;
3184
+ const w = m?.worker && now - m.worker.at < 15 * 60_000 && present.some((s) => s.role === "worker") ? m.worker : null;
3185
+ const build = present.find((s) => s.pkg)?.pkg ?? e.last_pkg ?? null;
3186
+ let unseen = null;
3187
+ if (m && m.seen_seq !== undefined && m.delivered_seq > m.seen_seq) {
3188
+ unseen = ts.records.filter((r) => r.seq > m.seen_seq && r.seq <= m.delivered_seq && r.from !== e.member && (r.to === null || r.to.includes(e.member)) && isMessage(r)).length;
3189
+ }
3190
+ else if (m && m.seen_seq !== undefined)
3191
+ unseen = 0;
3192
+ return {
3193
+ name: e.member, agent: m?.agent ?? e.last_agent ?? "", founder: founder === e.member, admitted: e.admitted, mayWake: e.may_wake,
3194
+ online: present.length > 0, windows, busy: work ? work.busy : null,
3195
+ worker: w ? { state: w.state, ...(w.reason ? { reason: w.reason } : {}), at: w.at } : null,
3196
+ build, behind: !!build && stuckBehind(build, relayPkg, now - this.startedAt),
3197
+ lastSeen: present.length ? now : (m?.last_seen || e.last_seen || null), unseen,
3198
+ };
3199
+ });
3200
+ }
3201
+ flowFor(ts, days, members, now = Date.now()) {
3202
+ return this.flowOfSlice(ts, days, members, now, toFlowRecords(this.windowSlice(ts, days, now)));
3203
+ }
3204
+ async flowForAsync(ts0, days, members, now = Date.now()) {
3205
+ const ts = ts0.sp ? {
3206
+ ...ts0, records: ts0.records.slice(), obligations: new Map([...ts0.obligations].map(([k, o]) => [k, { ...o }])),
3207
+ goals: new Map([...ts0.goals].map(([k, g]) => [k, { ...g }])), firstClaims: new Map(ts0.firstClaims),
3208
+ } : ts0;
3209
+ const slice = this.windowSlice(ts, days, now, false);
3210
+ const projected = [];
3211
+ for (let i = 0; i < slice.length; i += 10_000) {
3212
+ for (const r of toFlowRecords(slice.slice(i, i + 10_000)))
3213
+ projected.push(r);
3214
+ await new Promise((x) => setImmediate(x));
3215
+ }
3216
+ const index = await flowIndexAsync(projected);
3217
+ const since = now - days * DAY, before = since - days * DAY;
3218
+ const withdrawn = new Set();
3219
+ let prevCount = 0;
3220
+ const recs = ts.records;
3221
+ for (let i = recs.length - 1, older = 0; i >= 0 && older < 200; i--) {
3222
+ const r = recs[i];
3223
+ if (r.ts >= before) {
3224
+ older = 0;
3225
+ if (r.act === "RETRACT" && r.ref)
3226
+ withdrawn.add(r.ref);
3227
+ if (r.ts < since && isMessage(r) && !withdrawn.has(r.id))
3228
+ prevCount++;
3229
+ }
3230
+ else
3231
+ older++;
3232
+ if (i % 20_000 === 0)
3233
+ await new Promise((x) => setImmediate(x));
3234
+ }
3235
+ return flowOfAsync(this.flowInput(ts, days, members, now, projected, index, prevCount));
3236
+ }
3237
+ windowSlice(ts, days, now, withBefore = true) {
3238
+ const from = now - (withBefore ? 2 : 1) * days * DAY;
3239
+ const recs = ts.records;
3240
+ let cut = recs.length;
3241
+ for (let i = recs.length - 1, older = 0; i >= 0 && older < 200; i--) {
3242
+ if (recs[i].ts >= from) {
3243
+ cut = i;
3244
+ older = 0;
3245
+ }
3246
+ else
3247
+ older++;
3248
+ }
3249
+ const want = new Set([...ts.obligations.values()].map((o) => o.ref));
3250
+ for (const c of ts.firstClaims.values())
3251
+ if (c.at >= now - days * DAY)
3252
+ want.add(c.ref);
3253
+ const older = [];
3254
+ if (want.size)
3255
+ for (let i = 0; i < cut; i++) {
3256
+ const r = recs[i];
3257
+ if (want.has(r.id))
3258
+ older.push(r);
3259
+ }
3260
+ return older.length ? [...older, ...recs.slice(cut)] : recs.slice(cut);
3261
+ }
3262
+ flowOfSlice(ts, days, members, now, records, index, prevCount) {
3263
+ return flowOf(this.flowInput(ts, days, members, now, records, index, prevCount));
3264
+ }
3265
+ flowInput(ts, days, members, now, records, index, prevCount) {
3266
+ const sp = ts.sp;
3267
+ return {
3268
+ records, ...(index ? { index } : {}), ...(prevCount !== undefined ? { prevCount } : {}),
3269
+ obligations: toFlowObligations(ts.obligations.values(), ts.firstClaims, (o) => !!sp && !!o.claimed_by && this.claimHolds(sp, o.claimed_by, now)),
3270
+ goals: toFlowGoals(ts.goals.values()),
3271
+ members: members.filter((m) => m.admitted).map((m) => ({ name: m.name, agent: m.agent, online: m.online, founder: m.founder })),
3272
+ series: seriesOf(ts.usage, Math.max(14, days * 2), now),
3273
+ firstClaims: [...ts.firstClaims.values()],
3274
+ compactedThrough: ts.compactedThrough,
3275
+ days, now,
2907
3276
  };
2908
- const pill = (x) => `<span class="pill ${ACT_TONE[x]}">${x}</span>`;
2909
- const goalTitle = (id, short = false) => id
2910
- ? `<a href="${link({ goal: id, before: undefined })}" title="${esc(id)}">${esc(short ? clipText(state.goals.get(id)?.title ?? id, 48) : state.goals.get(id)?.title ?? id)}</a>`
2911
- : "<span class=dim>—</span>";
3277
+ }
3278
+ healthSlice(records, now) {
3279
+ const from = now - 28 * DAY;
3280
+ let cut = records.length;
3281
+ for (let i = records.length - 1, older = 0; i >= 0 && older < 200; i--) {
3282
+ if (records[i].ts >= from) {
3283
+ cut = i;
3284
+ older = 0;
3285
+ }
3286
+ else
3287
+ older++;
3288
+ }
3289
+ const out = records.slice(cut);
3290
+ if (!out.some(isMessage))
3291
+ for (let i = cut - 1; i >= 0; i--)
3292
+ if (isMessage(records[i])) {
3293
+ out.unshift(records[i]);
3294
+ break;
3295
+ }
3296
+ return out;
3297
+ }
3298
+ allTeamNames() {
3299
+ const out = [];
3300
+ const seen = new Set();
3301
+ for (const sp of this.spaces.values()) {
3302
+ seen.add(safeName(sp.name).toLowerCase());
3303
+ out.push(sp.name);
3304
+ }
3305
+ let files = [];
3306
+ try {
3307
+ files = readdirSync(this.dataDir);
3308
+ }
3309
+ catch { }
3310
+ for (const f of files) {
3311
+ if (!f.endsWith(".members.json"))
3312
+ continue;
3313
+ const safe = f.slice(0, -".members.json".length);
3314
+ if (seen.has(safe.toLowerCase()))
3315
+ continue;
3316
+ seen.add(safe.toLowerCase());
3317
+ if (this.leftoverState(safe).state === "covered")
3318
+ continue;
3319
+ out.push(safe);
3320
+ }
3321
+ return out;
3322
+ }
3323
+ async teamFilesSig(safe) {
3324
+ const files = [".jsonl", ".state.json", ".members.json", ".usage.json"].map((suf) => join(this.dataDir, `${safe}${suf}`));
3325
+ if (files.some((f) => pendingView(this.dataDir, safe, f) !== undefined))
3326
+ return `pending:${Date.now()}:${Math.random()}`;
3327
+ const st = await Promise.all(files.map((f) => fsp.stat(f).then((x) => `${x.size}:${x.mtimeMs}`, () => "-")));
3328
+ return st.join("|");
3329
+ }
3330
+ ledgerReads = new Map();
3331
+ async ledgerRecordsAsync(safe) {
3332
+ const file = join(this.dataDir, `${safe}.jsonl`);
3333
+ const queued = pendingView(this.dataDir, safe, file);
3334
+ if (queued !== undefined)
3335
+ return parseSliced(queued?.toString("utf8") ?? "");
3336
+ let st;
3337
+ try {
3338
+ st = await fsp.stat(file);
3339
+ }
3340
+ catch (e) {
3341
+ if (e.code === "ENOENT")
3342
+ return [];
3343
+ throw new LedgerUnreadable(e.code ?? "EIO");
3344
+ }
3345
+ const key = `${st.size}:${st.mtimeMs}`;
3346
+ const hit = this.sleepingCache.get(safe);
3347
+ if (hit && hit.key === key)
3348
+ return hit.records;
3349
+ const flight = `${safe}|${key}`;
3350
+ const running = this.ledgerReads.get(flight);
3351
+ if (running)
3352
+ return running;
3353
+ const p = (async () => {
3354
+ let text;
3355
+ try {
3356
+ text = await fsp.readFile(file, "utf8");
3357
+ }
3358
+ catch (e) {
3359
+ if (e.code === "ENOENT")
3360
+ return [];
3361
+ throw new LedgerUnreadable(e.code ?? "EIO");
3362
+ }
3363
+ const records = await parseSliced(text);
3364
+ this.putLedger(safe, { key, records, bytes: st.size });
3365
+ return records;
3366
+ })();
3367
+ this.ledgerReads.set(flight, p);
3368
+ try {
3369
+ return await p;
3370
+ }
3371
+ finally {
3372
+ this.ledgerReads.delete(flight);
3373
+ }
3374
+ }
3375
+ renderQueue = Promise.resolve();
3376
+ renderTurn() {
3377
+ const turn = this.renderQueue.then(() => new Promise((r) => setImmediate(r)));
3378
+ this.renderQueue = turn;
3379
+ return turn;
3380
+ }
3381
+ coldActive = 0;
3382
+ coldQueue = [];
3383
+ async coldSlot(fn) {
3384
+ while (this.coldActive >= this.readBounds.cold)
3385
+ await new Promise((r) => this.coldQueue.push(r));
3386
+ this.coldActive++;
3387
+ try {
3388
+ return await fn();
3389
+ }
3390
+ finally {
3391
+ this.coldActive--;
3392
+ this.coldQueue.shift()?.();
3393
+ }
3394
+ }
3395
+ teamViews = new ByteLru(() => this.readBounds.viewBytes);
3396
+ async teamViewAsync(name, days, now) {
3397
+ const live = this.spaces.get(spaceKey(name));
3398
+ const safe = live ? safeName(live.name) : safeName(name);
3399
+ const key = `${safe.toLowerCase()}|${days}`;
3400
+ const sig = live ? "live" : await this.teamFilesSig(safe);
3401
+ const hit = this.teamViews.get(key);
3402
+ if (hit && hit.sig === sig && now - hit.at < (live ? 15_000 : 30_000))
3403
+ return { ...hit.view, asOf: hit.at };
3404
+ const flight = `${key}|${sig}`;
3405
+ const running = this.viewFlights.get(flight);
3406
+ if (running)
3407
+ return running;
3408
+ const p = this.coldSlot(() => this.buildTeamView(name, days, Date.now(), key, sig, !!live));
3409
+ this.viewFlights.set(flight, p);
3410
+ try {
3411
+ return await p;
3412
+ }
3413
+ finally {
3414
+ this.viewFlights.delete(flight);
3415
+ }
3416
+ }
3417
+ viewFlights = new Map();
3418
+ async buildTeamView(name, days, now, key, sig, isLive) {
3419
+ const live = isLive ? this.spaces.get(spaceKey(name)) : undefined;
3420
+ const ts = await this.teamStateAsync(live ? live.name : name);
3421
+ if (!ts) {
3422
+ this.teamViews.delete(key);
3423
+ return null;
3424
+ }
3425
+ const members = this.memberLive(ts, now);
3426
+ const flow = await this.flowForAsync(ts, days, members, now);
3427
+ const health = spaceHealth({
3428
+ name: ts.name, records: this.healthSlice(ts.records, now), members: ts.allow.all(),
3429
+ online: members.reduce((n, m) => n + (m.online ? 1 : 0), 0), interactive: members.reduce((n, m) => n + m.windows, 0),
3430
+ relayPkg: this.upgradeHold ?? VERSION, now, relayAgeMs: now - this.startedAt,
3431
+ });
3432
+ const wantGist = new Set([...flow.open.map((o) => o.ref), ...flow.threads.map((t) => t.root)]);
3433
+ const gists = new Map();
2912
3434
  const firstLine = (s) => s.split("\n").find((l) => l.trim()) ?? "";
2913
- const rowsHtml = feedRows.map((r) => {
2914
- const meta = (r.meta ?? {});
2915
- const kv = [["Message ID", `<code>${esc(r.id)}</code>`]];
2916
- if (r.ref)
2917
- kv.push(["Reply to", `<code>${esc(r.ref)}</code>`]);
2918
- if (r.goal)
2919
- kv.push(["Goal", `${goalTitle(r.goal)} <code class=dim>${esc(r.goal)}</code>`]);
2920
- if (r.role)
2921
- kv.push(["Session role", esc(r.role)]);
2922
- const agent = sp?.members.get(r.from)?.agent;
2923
- if (agent)
2924
- kv.push(["Agent", esc(agent)]);
2925
- if (r.summary)
2926
- kv.push(["Summary", esc(r.summary)]);
3435
+ for (let i = ts.records.length - 1; i >= 0 && gists.size < wantGist.size; i--) {
3436
+ const r = ts.records[i];
3437
+ if (wantGist.has(r.id) && !gists.has(r.id))
3438
+ gists.set(r.id, clipText(r.summary ?? firstLine(r.body), 150));
3439
+ }
3440
+ const entries = ts.allow.all();
3441
+ const view = {
3442
+ name: ts.name, live: !!ts.sp, id: ts.allow.peekId(), flow, members, verdict: health.verdict, says: health.says, behind: health.behind,
3443
+ createdAt: entries.reduce((min, e) => (min === null || e.first_seen < min ? e.first_seen : min), null),
3444
+ series14: seriesOf(ts.usage, 14, now), series30: seriesOf(ts.usage, 30, now),
3445
+ refusals: { total: ts.refusals.total, recent: [...ts.refusals.recent] },
3446
+ gists, goalTitles: new Map([...ts.goals.values()].map((g) => [g.id, g.title])),
3447
+ admitted: entries.filter((e) => e.admitted && !e.revoked_at).map((e) => e.member),
3448
+ pending: ts.allow.pendingCount(),
3449
+ invites: ts.allow.openInvites().map((i) => ({ tail: i.token.slice(-6), by: i.created_by, expires: i.expires_at, mayWake: i.may_wake })),
3450
+ repos: ts.allow.repoList().map((r) => ({ origin: r.origin ?? null, fingerprint: r.fingerprint, by: r.added_by, at: r.at })),
3451
+ retained: ts.records.length, stateNote: ts.stateNote, asOf: now,
3452
+ };
3453
+ this.teamViews.set(key, { sig, at: now, view }, estimateViewBytes(view));
3454
+ return view;
3455
+ }
3456
+ async teamRecordsAsync(name) {
3457
+ const sp = this.spaces.get(spaceKey(name));
3458
+ if (sp)
3459
+ return sp.ledger.all();
3460
+ const safe = safeName(name);
3461
+ return this.ledgerRecordsAsync(safe);
3462
+ }
3463
+ async overviewEntry(name, days, now) {
3464
+ let v;
3465
+ try {
3466
+ v = await this.teamViewAsync(name, days, now);
3467
+ }
3468
+ catch (e) {
3469
+ const code = unreadableCode(e);
3470
+ if (!code)
3471
+ throw e;
3472
+ return { name, code };
3473
+ }
3474
+ if (!v)
3475
+ return null;
3476
+ const plan = this.spacePlan(v.name);
3477
+ const since = now - days * DAY;
3478
+ const refused = new Map();
3479
+ for (const r of v.refusals.recent)
3480
+ if (r.ts >= since)
3481
+ refused.set(r.code, (refused.get(r.code) ?? 0) + 1);
3482
+ return {
3483
+ summary: {
3484
+ name: v.name, live: v.live, id: v.id, verdict: v.verdict, says: v.says,
3485
+ owner: plan?.owner?.username ?? null, plan: plan?.name ?? null, behind: v.behind,
3486
+ people: v.members.filter((m) => m.admitted).length, online: v.members.filter((m) => m.online).length,
3487
+ spark14: v.series14.map((d) => d.worker + d.person), flow: v.flow, members: v.members, createdAt: v.createdAt,
3488
+ stateNote: v.stateNote, asOf: v.asOf,
3489
+ },
3490
+ series30: v.series30, refused, admitted: v.admitted,
3491
+ };
3492
+ }
3493
+ async allTeamNamesAsync() {
3494
+ const out = [];
3495
+ const seen = new Set();
3496
+ for (const sp of this.spaces.values()) {
3497
+ seen.add(safeName(sp.name).toLowerCase());
3498
+ out.push(sp.name);
3499
+ }
3500
+ let files = [];
3501
+ try {
3502
+ files = await fsp.readdir(this.dataDir);
3503
+ }
3504
+ catch { }
3505
+ for (const f of files) {
3506
+ if (!f.endsWith(".members.json"))
3507
+ continue;
3508
+ const safe = f.slice(0, -".members.json".length);
3509
+ if (seen.has(safe.toLowerCase()))
3510
+ continue;
3511
+ seen.add(safe.toLowerCase());
3512
+ if (this.leftoverState(safe).state === "covered")
3513
+ continue;
3514
+ out.push(safe);
3515
+ }
3516
+ return out;
3517
+ }
3518
+ adminTabDone(url) { return url?.searchParams.get("done") ?? null; }
3519
+ adminDashboard(res, A, url, csrf) {
3520
+ const nonce = newUiNonce();
3521
+ const tab = url?.searchParams.get("tab") ?? "overview";
3522
+ const done = this.adminTabDone(url);
3523
+ const now = Date.now();
3524
+ const accounts = A.allAccounts();
3525
+ const devices = A.allDevices();
3526
+ const send = (html) => { res.writeHead(200, pageHeaders(nonce)); res.end(html); };
3527
+ const teamNames = this.allTeamNames();
3528
+ const teamsOf = new Map();
3529
+ if (tab === "people")
3530
+ for (const n of teamNames) {
3531
+ const sp = this.spaces.get(spaceKey(n));
3532
+ let entries = [];
3533
+ try {
3534
+ entries = sp ? sp.allow.all() : new Allowlist(this.dataDir, safeName(n)).all();
3535
+ }
3536
+ catch { }
3537
+ const label = sp?.name ?? n;
3538
+ for (const e of entries)
3539
+ if (e.admitted && !e.revoked_at)
3540
+ teamsOf.set(e.member, [...(teamsOf.get(e.member) ?? []), label]);
3541
+ }
3542
+ const lastSeenOf = new Map();
3543
+ const devCount = new Map();
3544
+ for (const d of devices) {
3545
+ devCount.set(d.account_id, (devCount.get(d.account_id) ?? 0) + 1);
3546
+ lastSeenOf.set(d.account_id, Math.max(lastSeenOf.get(d.account_id) ?? 0, d.last_seen));
3547
+ }
3548
+ const plans = A.plans();
3549
+ if (tab === "people") {
3550
+ const ownedCounts = A.ownedCounts();
3551
+ const hits = A.planLimitHits();
3552
+ return send(renderPeople({
3553
+ nonce, csrf, done, plans: Object.keys(plans), teams: teamNames.length,
3554
+ accounts: accounts.map((a) => {
3555
+ const lim = A.limitsFor(a);
3556
+ return {
3557
+ id: a.id, username: a.username, email: a.email, status: a.status, plan: a.plan, custom: !!a.limits_json,
3558
+ limits: { spaces: lim.spaces, members: lim.members, wake: lim.wake },
3559
+ limitText: `${fmtLimit(lim.spaces)} team${lim.spaces === 1 ? "" : "s"} · ${fmtLimit(lim.members)} per team · workers ${lim.wake ? "on" : "off"}`,
3560
+ owned: ownedCounts.get(a.id) ?? 0, limitHits: hits.get(a.id) ?? 0, devices: devCount.get(a.id) ?? 0,
3561
+ created: a.created_at, lastSeen: lastSeenOf.get(a.id) ?? null, teams: teamsOf.get(a.username) ?? [],
3562
+ };
3563
+ }),
3564
+ }));
3565
+ }
3566
+ if (tab === "plans") {
3567
+ const counts = A.planCounts();
3568
+ return send(renderPlans({ nonce, csrf, done, teams: teamNames.length, people: accounts.length,
3569
+ plans: Object.entries(plans).map(([n, l]) => ({ name: n, accounts: counts[n] ?? 0, spaces: l.spaces, members: l.members, invites: inviteLimit(l, n), wake: l.wake })) }));
3570
+ }
3571
+ if (tab === "security") {
3572
+ const byAcct = new Map(accounts.map((a) => [a.id, a.username]));
3573
+ const byDev = new Map(devices.map((d) => [d.id, d.label]));
3574
+ let snapshots = 0;
3575
+ try {
3576
+ snapshots = readdirSync(join(this.dataDir, "backups")).filter((f) => f.startsWith("accounts-") && f.endsWith(".db")).length;
3577
+ }
3578
+ catch { }
3579
+ return send(renderSecurity({
3580
+ nonce, csrf, done, now, teams: teamNames.length, people: accounts.length,
3581
+ sessions: A.liveSessions(500).map((s) => ({ id: s.id, username: byAcct.get(s.account_id) ?? s.account_id, device: byDev.get(s.device_id) ?? s.device_id.slice(0, 10), deviceId: s.device_id, role: s.role, space: s.space, ip: s.ip, created: s.created_at, lastSeen: s.last_seen })),
3582
+ endedToday: A.endedSessionsSince(now - DAY), blocks: A.allBlocks(), codes: A.recentVerifications(50),
3583
+ snapshots, backupAt: A.auditLog({ limit: 1, event: "admin_backup" })[0]?.ts ?? null,
3584
+ }));
3585
+ }
3586
+ const byAcct = new Map(accounts.map((a) => [a.id, a.username]));
3587
+ const auditLine = (e) => ({
3588
+ ts: e.ts, event: e.event, space: e.space,
3589
+ who: e.event.startsWith("admin_") ? "operator" : e.event.startsWith("viewer_") ? `${e.target ?? "a viewer"} (read-only)` : (e.actor_account ? byAcct.get(e.actor_account) ?? "a deleted account" : e.target ?? "relay"),
3590
+ detail: e.event.startsWith("viewer_") ? e.detail : [e.event.startsWith("admin_") ? e.target : null, e.detail].filter(Boolean).join(" · ") || null,
3591
+ });
3592
+ if (tab === "audit") {
3593
+ return send(renderAudit({ nonce, teams: teamNames.length, people: accounts.length, total: A.counts().audit, rows: A.auditLog({ limit: 500 }).map(auditLine) }));
3594
+ }
3595
+ const days = rangeOf(url?.searchParams.get("range") ?? null);
3596
+ void (async () => {
3597
+ const names = await this.allTeamNamesAsync();
3598
+ const entries = [];
3599
+ const unreadable = [];
3600
+ for (const n of names) {
3601
+ const e = await this.overviewEntry(n, days, now);
3602
+ if (e && "code" in e)
3603
+ unreadable.push(e);
3604
+ else if (e)
3605
+ entries.push(e);
3606
+ await new Promise((r) => setImmediate(r));
3607
+ }
3608
+ await this.renderTurn();
3609
+ if (res.destroyed)
3610
+ return;
3611
+ const teams = entries.map((e) => e.summary);
3612
+ for (const e of entries)
3613
+ for (const m of e.admitted)
3614
+ teamsOf.set(m, [...(teamsOf.get(m) ?? []), e.summary.name]);
3615
+ const series30 = [];
3616
+ {
3617
+ const s = entries.map((e) => e.series30);
3618
+ const days30 = seriesOf(null, 30, now);
3619
+ for (let i = 0; i < 30; i++) {
3620
+ const ts = Date.parse(`${days30[i].day}T12:00:00Z`);
3621
+ series30.push({ ts, worker: s.reduce((n, x) => n + (x[i]?.worker ?? 0), 0), person: s.reduce((n, x) => n + (x[i]?.person ?? 0), 0), today: i === 29 });
3622
+ }
3623
+ }
3624
+ const relayPkg = this.upgradeHold ?? VERSION;
3625
+ const builds = new Map();
3626
+ for (const t of teams)
3627
+ for (const m of t.members)
3628
+ if (m.admitted && m.build)
3629
+ builds.set(m.build, (builds.get(m.build) ?? 0) + 1);
3630
+ const buildRows = [...builds.entries()].sort((a, b) => (versionBehind(a[0], b[0]) ? 1 : versionBehind(b[0], a[0]) ? -1 : 0)).map(([v, n]) => ({
3631
+ version: v, n,
3632
+ kind: (v === relayPkg ? "current" : versionBehind(v, relayPkg) ? (stuckBehind(v, relayPkg, now - this.startedAt) ? "missed" : "upgrading") : "newer"),
3633
+ }));
3634
+ const refusals = new Map();
3635
+ for (const e of entries)
3636
+ for (const [c, n] of e.refused)
3637
+ refusals.set(c, (refusals.get(c) ?? 0) + n);
3638
+ const counts = A.counts();
3639
+ const planCounts = A.planCounts();
3640
+ const backupAt = A.auditLog({ limit: 1, event: "admin_backup" })[0]?.ts ?? null;
3641
+ let snapshots = 0;
3642
+ try {
3643
+ snapshots = readdirSync(join(this.dataDir, "backups")).filter((f) => f.startsWith("accounts-") && f.endsWith(".db")).length;
3644
+ }
3645
+ catch { }
3646
+ const sf = this.storageFailure && now - this.storageFailure.at < STORAGE_UNHEALTHY_MS ? this.storageFailure : null;
3647
+ send(renderOverview({
3648
+ nonce, csrf, done, days, now, teams, unreadable, leftovers: this.leftovers(), series30, builds: buildRows,
3649
+ refusals: [...refusals.entries()].sort((a, b) => b[1] - a[1]).map(([code, n]) => ({ code, n })),
3650
+ health: {
3651
+ version: VERSION, hold: this.upgradeHold, upMs: now - this.startedAt, loopP99: this.loopDelay ? this.loopStats().p99 : null,
3652
+ storage: sf ? sf.where : null, teams: names.length, maxTeams: this.limits.maxSpaces,
3653
+ codesHour: this.emailSends.filter((s) => now - s.at < 3_600_000).length, codesCap: Number(process.env.PANTHEON_EMAIL_MAX_PER_HOUR) || 200,
3654
+ backupAt, snapshots,
3655
+ },
3656
+ accounts: {
3657
+ total: accounts.length, paid: Object.entries(planCounts).filter(([n]) => n !== "free").reduce((s, [, v]) => s + v, 0),
3658
+ top: [...accounts].sort((a, b) => (lastSeenOf.get(b.id) ?? 0) - (lastSeenOf.get(a.id) ?? 0)).slice(0, 5).map((a) => ({
3659
+ username: a.username, email: a.email, teams: teamsOf.get(a.username) ?? [], plan: a.plan, devices: devCount.get(a.id) ?? 0, lastSeen: lastSeenOf.get(a.id) ?? null, status: a.status,
3660
+ })),
3661
+ },
3662
+ audit: { total: counts.audit, latest: A.auditLog({ limit: 5 }).map(auditLine) },
3663
+ }));
3664
+ })().catch((e) => { this.log(`! admin overview failed: ${e.message}`); if (!res.headersSent)
3665
+ this.json(res, 500, { error: "the overview could not be made" }); });
3666
+ }
3667
+ adminSpace(res, url, name, csrf, A, ip) {
3668
+ void this.adminSpaceAsync(res, url, name, csrf, A, ip).catch((e) => { this.log(`! team page failed: ${e.message}`); if (!res.headersSent)
3669
+ this.json(res, 500, { error: "the team page could not be made" }); });
3670
+ }
3671
+ async adminSpaceAsync(res, url, name, csrf, A, ip) {
3672
+ const now = Date.now();
3673
+ const days = rangeOf(url.searchParams.get("range"));
3674
+ let v, records;
3675
+ try {
3676
+ v = await this.teamViewAsync(name, days, now);
3677
+ if (!v)
3678
+ return this.json(res, 404, { error: `no space "${name}" on this relay` });
3679
+ records = await this.teamRecordsAsync(v.live ? v.name : name);
3680
+ }
3681
+ catch (e) {
3682
+ const code = unreadableCode(e);
3683
+ if (!code)
3684
+ throw e;
3685
+ const nonce = newUiNonce();
3686
+ res.writeHead(503, { ...pageHeaders(nonce), "retry-after": "30" });
3687
+ return void res.end(renderTeamUnreadable({ nonce, name, code }));
3688
+ }
3689
+ const { members, flow } = v;
3690
+ await this.renderTurn();
3691
+ if (res.destroyed)
3692
+ return;
3693
+ const firstLine = (s) => s.split("\n").find((l) => l.trim()) ?? "";
3694
+ const gist = (id) => v.gists.get(id) ?? "";
3695
+ const q = {
3696
+ act: url.searchParams.get("act") ?? "", member: url.searchParams.get("member") ?? "", goal: url.searchParams.get("goal") ?? "",
3697
+ q: url.searchParams.get("q") ?? "", presence: url.searchParams.get("presence") === "1",
3698
+ ...(Number(url.searchParams.get("before")) ? { before: Number(url.searchParams.get("before")) } : {}),
3699
+ };
3700
+ const { rows, next } = feed(records, { ...q, limit: 100 });
3701
+ const readId = url.searchParams.get("read");
3702
+ let readRec = null;
3703
+ if (readId)
3704
+ for (let i = records.length - 1; i >= 0; i--)
3705
+ if (records[i].id === readId) {
3706
+ readRec = records[i];
3707
+ break;
3708
+ }
3709
+ if (readRec)
3710
+ A.audit({ event: "admin_message_read", space: v.name, target: readRec.id, detail: `#${readRec.seq} ${readRec.act} from ${readRec.from}`, ip });
3711
+ let read = null;
3712
+ if (readRec) {
3713
+ const meta = (readRec.meta ?? {});
3714
+ const kv = [["Message ID", readRec.id]];
3715
+ if (readRec.ref)
3716
+ kv.push(["Reply to", readRec.ref]);
3717
+ if (readRec.goal)
3718
+ kv.push(["Goal", `${v.goalTitles.get(readRec.goal) ?? ""} (${readRec.goal})`]);
3719
+ if (readRec.role)
3720
+ kv.push(["Session role", readRec.role]);
3721
+ if (readRec.summary)
3722
+ kv.push(["Summary", readRec.summary]);
2927
3723
  for (const k of ["branch", "commit", "remote", "base", "repo_origin", "decision_domains", "defers_to", "affects", "authority", "unverified_domains"])
2928
3724
  if (meta[k] !== undefined)
2929
- kv.push([k.replace(/_/g, " "), esc(typeof meta[k] === "string" ? meta[k] : JSON.stringify(meta[k]))]);
2930
- const gist = clipText(r.summary ?? firstLine(r.body), 150);
2931
- return `<tr><td class="num dim">#${r.seq}</td><td class=dim title="${when(r.ts)} UTC">${ago(r.ts)}</td>`
2932
- + `<td class=cell><a href="${link({ member: r.from, before: undefined })}"><code>${esc(r.from)}</code></a><span class=sub>→ ${r.to?.length ? r.to.map((t) => esc(t)).join(", ") : "everyone"}${r.role && r.role !== "interactive" ? ` · ${esc(r.role)}` : ""}</span></td>`
2933
- + `<td>${pill(r.act)}</td><td class=dim>${r.goal ? goalTitle(r.goal, true) : "—"}</td>`
2934
- + `<td class=wrapc>${esc(gist) || "<span class=dim>(empty message)</span>"}</td>`
2935
- + `<td class=actions><button type=button class=more aria-expanded=false onclick="toggleRow(this)"><span class=rm>Read full message</span><span class=rl hidden>Show less</span></button></td></tr>`
2936
- + `<tr class=detail hidden><td colspan=7><div class=body>${esc(r.body) || "<span class=dim>(empty message)</span>"}</div><div class=kv>${kv.map(([k, v]) => `<span>${k}</span><b>${v}</b>`).join("")}</div></td></tr>`;
2937
- }).join("");
2938
- const entries = allow.all();
2939
- const openInvites = allow.openInvites();
2940
- const csrfTok = this.adminCsrf();
2941
- const revokeForm = (token) => `<form method=POST action="/admin/action" class=inl onsubmit="return confirm('Revoke this invite? The link stops working immediately.')"><input type=hidden name=csrf value="${csrfTok}"><input type=hidden name=action value=revoke_invite><input type=hidden name=id value="${esc(name)}"><input type=hidden name=token value="${esc(token.slice(-6))}"><button class="act danger">revoke</button></form>`;
2942
- const memberRows = entries.map((e) => {
2943
- const m = sp?.members.get(e.member);
2944
- const online = m ? presentSessions(m).length : 0;
2945
- return `<tr><td><a href="${link({ member: e.member, before: undefined })}"><code>${esc(e.member)}</code></a></td><td>${e.admitted ? '<span class="pill ok">admitted</span>' : '<span class="pill">pending</span>'}</td><td>${e.may_wake ? "yes" : "no"}</td><td>${online ? `<span class="pill ok">${online} session${online === 1 ? "" : "s"}</span> ${esc(m.agent)}` : "<span class=dim>offline</span>"}</td><td class=dim>${m ? ago(m.last_seen) : when(e.first_seen)}</td></tr>`;
2946
- }).join("");
2947
- const repos = allow.repoList();
2948
- const refusals = state.refusals;
2949
- const refusalRows = [...refusals.recent].reverse().map((r) => `<tr><td class=dim>${when(r.ts)}</td><td><code>${esc(r.member)}</code></td><td>${esc(r.act)}</td><td><span class="pill bad">${esc(r.code)}</span></td></tr>`).join("");
2950
- const byCode = Object.entries(refusals.by_code).sort((x, y) => y[1] - x[1]).map(([c, n]) => `<code>${esc(c)}</code> ${n}`).join(" · ");
2951
- const goals = [...state.goals.values()];
2952
- const open = goals.filter((g) => g.state !== "CLOSED").length;
2953
- const lastIn = new Map();
2954
- for (const r of records)
2955
- if (r.goal)
2956
- lastIn.set(r.goal, Math.max(lastIn.get(r.goal) ?? 0, r.ts));
2957
- const owedIn = new Map();
2958
- if (sp)
2959
- for (const o of sp.obligations.values())
2960
- if (o.goal)
2961
- owedIn.set(o.goal, (owedIn.get(o.goal) ?? 0) + 1);
2962
- const goalRows = goals.sort((x, y) => (x.state === "CLOSED" ? 1 : 0) - (y.state === "CLOSED" ? 1 : 0) || (lastIn.get(y.id) ?? 0) - (lastIn.get(x.id) ?? 0)).map((g) => `<tr><td class="cell wrapc"><a href="${link({ goal: g.id, before: undefined })}">${esc(g.title)}</a><span class=sub><code>${esc(g.id)}</code></span></td>`
2963
- + `<td><span class="pill ${g.state === "OPEN" ? "ok" : g.state === "CLOSED" ? "dim" : "warn"}">${esc(g.state)}</span></td>`
2964
- + `<td class=num>${g.acts}<span class=dim> / ${g.budget}</span></td><td>${g.members.map((m) => `<code>${esc(m)}</code>`).join(", ")}</td>`
2965
- + `<td class="num${owedIn.get(g.id) ? " tone-warn" : ""}">${sp ? owedIn.get(g.id) ?? 0 : "<span class=dim>—</span>"}</td><td class=dim title="${when(lastIn.get(g.id) ?? null)}">${ago(lastIn.get(g.id) ?? null)}</td></tr>`).join("");
2966
- const options = MESSAGE_ACTS.map((x) => `<option value="${x}"${q.act === x ? " selected" : ""}>${x}</option>`).join("");
2967
- const obj24 = a.by_act_24h.OBJECT ?? 0, esc24 = countActs(a.by_act_24h, ESCALATIONS), ref24 = refusalsSince(refusals, dayAgo);
2968
- const html = `<!doctype html><html lang=en><meta charset=utf-8><title>pantheon · admin · ${esc(name)}</title>
2969
- <meta name=viewport content="width=device-width,initial-scale=1">
2970
- ${ADMIN_CSS}
2971
- <header>
2972
- <h1><span class=crumbs><a href="/admin">admin</a> › </span>${esc(name)} <span class="pill ${sp ? "ok" : ""}">${sp ? "live" : "sleeping"}</span></h1>
2973
- <span class=meta>${esc(allow.peekId() ?? "no id yet")} · ${records.length} acts retained${a.first ? ` since ${when(a.first)}` : ""}</span>
2974
- <span class=links><a href="/admin">← dashboard</a><a href="/admin/logout">sign out</a></span>
2975
- </header>
2976
- <nav class=jump aria-label="Sections"><a href="#s-goals">Goals</a><a href="#conversation">Messages</a><a href="#s-team">Team &amp; diagnostics</a></nav>
2977
- ${url.searchParams.get("done") ? `<div class=done role=status>${esc(url.searchParams.get("done"))}</div>` : ""}
2978
- <main>
2979
- <div class=kpis>
2980
- <div class=kpi><div class=l>Members</div><div class=v>${entries.filter((e) => e.admitted).length}<small>admitted</small></div><div class=s>${sp ? [...sp.members.values()].filter((m) => presentSessions(m).length).length : 0} online now · ${openInvites.length} unused invite${openInvites.length === 1 ? "" : "s"}</div></div>
2981
- <div class=kpi><div class=l>Goals</div><div class=v>${open}<small>/ ${goals.length} open</small></div><div class=s>${sp ? `${sp.obligations.size} obligation${sp.obligations.size === 1 ? "" : "s"} owed right now` : "asleep — reloads on the next connect"}</div></div>
2982
- <div class=kpi><div class=l>Messages today</div><div class=v>${a.messages_24h}<small>/ ${a.messages} retained</small></div><div class=s>${a.senders_24h.length} delegate${a.senders_24h.length === 1 ? "" : "s"} active today · ${a.sessions_24h} sessions</div></div>
2983
- <div class="kpi${obj24 + esc24 ? " warn" : ""}"><div class=l>Needs a look today</div><div class=v>${obj24 + esc24}</div><div class=s>${obj24} objection${obj24 === 1 ? "" : "s"} · ${esc24} escalation${esc24 === 1 ? "" : "s"} · ${a.by_act_24h.DELIVERED ?? 0} deliver${(a.by_act_24h.DELIVERED ?? 0) === 1 ? "y" : "ies"}</div></div>
2984
- <div class="kpi${ref24 ? " warn" : ""}"><div class=l>Refused by the relay</div><div class=v>${ref24}<small>today / ${refusals.total} total</small></div><div class=s>${byCode || "nothing refused yet"}</div></div>
2985
- </div>
2986
-
2987
- <section id=s-goals>
2988
- <div class=sec><h2>Goals</h2><span class=count>${goals.length} · open first, most recent activity first · click a goal to see only its messages</span></div>
2989
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-goals>
2990
- <thead><tr><th>goal</th><th>state</th><th class=num>acts / budget</th><th>members</th><th class=num>owed</th><th>last activity</th></tr></thead>
2991
- <tbody>${goalRows || "<tr><td colspan=6 class=dim>no goals yet</td></tr>"}</tbody>
2992
- </table></div>
2993
- </section>
2994
-
2995
- <section id=conversation>
2996
- <div class=sec><h2>Messages</h2><span class=count>${feedRows.length} shown${filtered ? " · filtered" : ""} · newest first</span></div>
2997
- <form class=filters method=GET action="/admin/space"><input type=hidden name=name value="${esc(name)}">
2998
- <label>type<select name=act><option value="">All types</option>${options}</select></label>
2999
- <label>member<input type=text name=member placeholder="Any member" value="${esc(q.member)}" size=12></label>
3000
- <label>goal<input type=text name=goal placeholder="Any goal ID" value="${esc(q.goal)}" size=16></label>
3001
- <label>search<input type=search name=q placeholder="Find words or phrases" value="${esc(q.q)}" size=26></label>
3002
- <label class=chk><input type=checkbox name=presence value=1${q.presence ? " checked" : ""}> include arrivals and departures</label>
3003
- <button class=act>Apply filters</button>${filtered ? `<a href="/admin/space?name=${encodeURIComponent(name)}" style="padding-bottom:7px">Clear filters</a>` : ""}
3004
- </form>
3005
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-messages>
3006
- <thead><tr><th class=num>#</th><th>when</th><th>from → to</th><th>act</th><th>goal</th><th>what was said</th><th></th></tr></thead>
3007
- <tbody>${rowsHtml || `<tr><td colspan=7 class=empty>${filtered ? "No messages match these filters. Try a different search or clear the filters." : "No messages yet. They appear here when this team's agents start working together."}</td></tr>`}</tbody>
3008
- </table></div>
3009
- <nav class=pager aria-label="Message pages">${q.before ? `<a href="${link({ before: undefined })}">← Latest messages</a>` : "<span></span>"}${next !== null ? `<a href="${link({ before: next })}">Older messages →</a>` : ""}</nav>
3010
- <p class=note>Retained history only: the relay keeps the most recent ${LIMITS.ledgerKeepRecent.toLocaleString()} acts of a space plus everything in an open goal. Messages are shown as sent; nothing is edited or summarised. Read full message shows the whole text with its ids and delivery references.</p>
3011
- </section>
3012
-
3013
- <section id=s-team>
3014
- <div class=sec><h2>Team details and diagnostics</h2><span class=count>${entries.length} member${entries.length === 1 ? "" : "s"} · ${open} open goal${open === 1 ? "" : "s"}</span></div>
3015
- <h3 style="font-size:14px;margin:14px 0 8px">Members</h3>
3016
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table>
3017
- <thead><tr><th>member</th><th>state</th><th>may wake</th><th>online</th><th>last seen</th></tr></thead>
3018
- <tbody>${memberRows || "<tr><td colspan=5 class=dim>nobody yet</td></tr>"}</tbody>
3019
- </table></div>
3020
-
3021
- <h3 style="font-size:14px;margin:22px 0 8px">Open invites <span class=dim>(${openInvites.length} unused)</span></h3>
3022
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table>
3023
- <thead><tr><th>token</th><th>minted by</th><th>expires</th><th>may wake</th><th></th></tr></thead>
3024
- <tbody>${openInvites.map((i) => `<tr><td><code>…${esc(i.token.slice(-6))}</code></td><td><code>${esc(i.created_by)}</code></td><td class=dim>${when(i.expires_at)}</td><td>${i.may_wake ? "yes" : "no"}</td><td class=actions>${revokeForm(i.token)}</td></tr>`).join("") || "<tr><td colspan=5 class=dim>none open</td></tr>"}</tbody>
3025
- </table></div>
3026
- <p class=note>An unused invite counts toward the plan's people limit until it expires. Revoking makes the link inert immediately; members see theirs with <code>pantheon invite --list</code> and cancel with <code>--revoke</code>.</p>
3027
- ${repos.length ? `<h3 style="font-size:14px;margin:22px 0 8px">Repos this team works from</h3><div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table><thead><tr><th>origin</th><th>fingerprint</th><th>added by</th><th>when</th></tr></thead><tbody>${repos.map((r) => `<tr><td>${esc(r.origin ?? "(no origin)")}</td><td><code>${esc(r.fingerprint)}</code></td><td><code>${esc(r.added_by)}</code></td><td class=dim>${when(r.at)}</td></tr>`).join("")}</tbody></table></div>` : ""}
3028
-
3029
- <h3 style="font-size:14px;margin:22px 0 8px">Acts <span class=dim>(today · retained)</span></h3>
3030
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table id=t-acts>
3031
- <thead><tr><th></th>${MESSAGE_ACTS.map((x) => `<th class=num>${x}</th>`).join("")}</tr></thead>
3032
- <tbody><tr><td class=dim>today</td>${MESSAGE_ACTS.map((x) => `<td class=num data-act="${x}" data-window="24h">${a.by_act_24h[x] ?? 0}</td>`).join("")}</tr>
3033
- <tr><td class=dim>retained</td>${MESSAGE_ACTS.map((x) => `<td class=num data-act="${x}" data-window="all">${a.by_act[x] ?? 0}</td>`).join("")}</tr></tbody>
3034
- </table></div>
3035
-
3036
- <h3 style="font-size:14px;margin:22px 0 8px">Refused by the relay <span class=dim>(latest ${refusals.recent.length} of ${refusals.total})</span></h3>
3037
- <div class=wrap tabindex=0 role=region aria-label="Scrollable table"><table>
3038
- <thead><tr><th>when</th><th>member</th><th>request</th><th>reason</th></tr></thead>
3039
- <tbody>${refusalRows || "<tr><td colspan=4 class=dim>nothing refused</td></tr>"}</tbody>
3040
- </table></div>
3041
- ${byCode ? `<p class=note>All time: ${byCode}</p>` : ""}
3042
- </section>
3043
- </main>
3044
- <script>
3045
- function toggleRow(btn) {
3046
- var row = btn.closest("tr"), detail = row.nextElementSibling, open = detail.hasAttribute("hidden");
3047
- if (open) detail.removeAttribute("hidden"); else detail.setAttribute("hidden", "");
3048
- btn.setAttribute("aria-expanded", open ? "true" : "false");
3049
- btn.querySelector(".rm").hidden = open; btn.querySelector(".rl").hidden = !open;
3050
- }
3051
- </script>`;
3052
- res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "referrer-policy": "no-referrer" });
3725
+ kv.push([k.replace(/_/g, " "), typeof meta[k] === "string" ? meta[k] : JSON.stringify(meta[k])]);
3726
+ read = { id: readRec.id, body: readRec.body, kv };
3727
+ }
3728
+ const plan = this.spacePlan(v.name);
3729
+ const since = now - days * DAY;
3730
+ const byCode = new Map();
3731
+ for (const r of v.refusals.recent) {
3732
+ if (r.ts < since)
3733
+ continue;
3734
+ const c = byCode.get(r.code) ?? { n: 0, last: { member: r.member, ts: r.ts } };
3735
+ c.n++;
3736
+ if (r.ts >= c.last.ts)
3737
+ c.last = { member: r.member, ts: r.ts };
3738
+ byCode.set(r.code, c);
3739
+ }
3740
+ const nonce = newUiNonce();
3741
+ const html = renderTeam({
3742
+ nonce, csrf, done: url.searchParams.get("done"), days, now,
3743
+ name: v.name, live: v.live, id: v.id, verdict: v.verdict, says: v.says,
3744
+ owner: plan?.owner?.username ?? null, plan: plan?.name ?? null, peopleLimit: plan ? plan.limits.members : null,
3745
+ createdAt: v.createdAt,
3746
+ flow, gist, members, stateNote: v.stateNote, asOf: v.asOf,
3747
+ series14: v.series14.map((d, i, a) => ({ ts: Date.parse(`${d.day}T12:00:00Z`), worker: d.worker, person: d.person, today: i === a.length - 1 })),
3748
+ ...((al) => al ? {
3749
+ pending: al.pendingCount(),
3750
+ invites: al.openInvites().map((i) => ({ tail: i.token.slice(-6), by: i.created_by, expires: i.expires_at, mayWake: i.may_wake })),
3751
+ repos: al.repoList().map((r) => ({ origin: r.origin ?? null, fingerprint: r.fingerprint, by: r.added_by, at: r.at })),
3752
+ } : { pending: v.pending, invites: v.invites, repos: v.repos })(this.live(v.name)?.allow ?? this.rosterOf(v.name)),
3753
+ refusals: { total: v.refusals.total, inRange: [...byCode.entries()].sort((a, b) => b[1].n - a[1].n).map(([code, v]) => ({ code, ...v })) },
3754
+ viewers: A.viewerGrants(v.id ?? "").map((g) => ({ id: g.id, email: g.email, grantedBy: g.granted_by, grantedAt: g.granted_at, expiresAt: g.expires_at, lastView: g.last_view_at, revokedAt: g.revoked_at, revokedBy: g.revoked_by })),
3755
+ feed: {
3756
+ rows: rows.map((r) => ({ id: r.id, seq: r.seq, ts: r.ts, from: r.from, to: r.to, role: r.role ?? null, act: r.act, goal: r.goal, goalTitle: r.goal ? v.goalTitles.get(r.goal) ?? null : null, gist: clipText(r.summary ?? firstLine(r.body), 150) })),
3757
+ next, q, filtered: !!(q.act || q.member || q.goal || q.q || q.presence), read, acts: [...MESSAGE_ACTS],
3758
+ },
3759
+ retained: records.length,
3760
+ allOpen: url.searchParams.get("open") === "all",
3761
+ });
3762
+ res.writeHead(200, pageHeaders(nonce));
3053
3763
  res.end(html);
3054
3764
  }
3055
3765
  adminExport(res, A, url) {
@@ -3325,11 +4035,28 @@ pantheon start</code></pre>
3325
4035
  sp = this.live(m.space) ?? null;
3326
4036
  if (!sp && this.aliasOf(m.space))
3327
4037
  return reject("NO_SUCH_SPACE", `no space "${m.space}" on this relay`);
4038
+ const ls = this.leftoverState(safeName(m.space));
4039
+ if (ls.state !== "live") {
4040
+ this.log(`! REFUSED ${m.member}: an invite to "${m.space}" (${ls.state}: ${ls.why})`);
4041
+ return reject("SPACE_DELETED", ls.state === "covered" ? deletedNameWords(m.space) : invitesHeldWords(m.space), { gone: null });
4042
+ }
4043
+ const unboundNow = sp ? !sp.allow.hasToken : this.spaceOnDisk(m.space) && (() => { try {
4044
+ return !new Allowlist(this.dataDir, safeName(m.space)).hasToken;
4045
+ }
4046
+ catch {
4047
+ return false;
4048
+ } })();
4049
+ if (unboundNow) {
4050
+ this.log(`! REFUSED ${m.member}: an invite to "${m.space}", which has no team token bound yet`);
4051
+ return reject("UNBOUND_SPACE", `"${m.space}" has no team token yet (it was made by an old relay): a member has to connect to it once from a machine that already has it — then this invite works`);
4052
+ }
3328
4053
  if (!sp && this.spaceOnDisk(m.space)) {
3329
4054
  const inv = new Allowlist(this.dataDir, safeName(m.space)).findInvite(m.enroll);
3330
4055
  if (inv?.token === m.enroll && inv.space_token)
3331
4056
  sp = this.getOrCreateSpace(m.space, inv.space_token, { claim: "invite", retired: true });
3332
4057
  }
4058
+ if (!sp && !this.spaceOnDisk(m.space) && this.tombstoneRecord(m.space))
4059
+ return reject("SPACE_DELETED", deletedNameWords(m.space), { gone: null });
3333
4060
  if (!sp)
3334
4061
  return reject("NO_SUCH_SPACE", `no space "${m.space}" on this relay`);
3335
4062
  }
@@ -3337,6 +4064,22 @@ pantheon start</code></pre>
3337
4064
  const found = this.tombstoneRecord(m.space);
3338
4065
  const otherSpelling = !!found && !found.corrupt && !found.unreadable && safeName(found.name) !== safeName(m.space);
3339
4066
  const tomb = otherSpelling && (this.live(m.space) || this.spaceOnDisk(m.space)) ? null : found;
4067
+ if (!this.live(m.space) && !this.spaceOnDisk(m.space) && this.leftoverState(safeName(m.space)).state !== "live") {
4068
+ this.log(`! REFUSED ${m.member}: "${m.space}" still has a deleted team's files without their member list`);
4069
+ return reject("SPACE_DELETED", coveredNameWords(m.space), { gone: null });
4070
+ }
4071
+ if (!this.live(m.space) && this.spaceOnDisk(m.space) && this.leftoverState(safeName(m.space)).state === "covered") {
4072
+ const own = this.tombstoneTeamFor(m.space, m.token ?? "", { member: m.member, publicKey: m.pubkey });
4073
+ this.log(`! REFUSED ${m.member}: "${m.space}" was deleted (its member list had been left on disk)`);
4074
+ if (own) {
4075
+ const a = this.deletedAnswerFor(own, m.member, m.pubkey);
4076
+ if (a.kind === "member")
4077
+ return reject("SPACE_DELETED", deletedWords(own), { gone: goneAnswer(own) });
4078
+ if (a.kind === "removed")
4079
+ return reject("REVOKED", removedWords(m.member, own.name, a.removedBy));
4080
+ }
4081
+ return reject("SPACE_DELETED", coveredNameWords(m.space), { gone: null });
4082
+ }
3340
4083
  if (tomb) {
3341
4084
  const own = this.tombstoneTeamFor(m.space, m.token ?? "", { member: m.member, publicKey: m.pubkey });
3342
4085
  if (own) {
@@ -3458,6 +4201,10 @@ pantheon start</code></pre>
3458
4201
  if (m.member.toLowerCase() === RELAY_MEMBER && !sp.allow.get(m.member)) {
3459
4202
  return reject("RESERVED_NAME", `"${m.member}" is the name the relay itself speaks as — choose another name`);
3460
4203
  }
4204
+ if (!enrolled && !sp.allow.empty && !sp.allow.get(m.member) && this.leftoverState(safeName(sp.name)).state !== "live") {
4205
+ this.log(`[${sp.name}] ! REFUSED ${m.member}: a new name, and this team cannot be told apart from a deleted one`);
4206
+ return reject("SPACE_DELETED", invitesHeldWords(m.space), { gone: null });
4207
+ }
3461
4208
  const verdict = enrolled ? "ok" : sp.allow.present(m.member, m.pubkey);
3462
4209
  if (verdict === "pending-full") {
3463
4210
  this.log(`[${sp.name}] ! REFUSED ${m.member}: too many names already waiting to be admitted`);
@@ -3672,6 +4419,9 @@ pantheon start</code></pre>
3672
4419
  }
3673
4420
  if (m.wake_only && (m.revoke || m.forget || !target.admitted))
3674
4421
  return fail("NOT_ADMITTED", `"${m.member}" is not in "${space.name}" now, so there is no wake permission to change`, m.cid);
4422
+ if (!m.revoke && !m.forget && !m.wake_only && !target.admitted && this.leftoverState(safeName(space.name)).state !== "live") {
4423
+ return fail("SPACE_HELD", `nobody new can be admitted to "${space.name}" now: a team of this name was deleted on this relay and the relay cannot tell this one apart from it. Ask the relay operator.`, m.cid);
4424
+ }
3675
4425
  if (!m.revoke && !m.forget) {
3676
4426
  const plan = this.spacePlan(space.name);
3677
4427
  if (plan && m.may_wake && !plan.limits.wake)
@@ -3765,6 +4515,9 @@ pantheon start</code></pre>
3765
4515
  const self = space.allow.get(me.id);
3766
4516
  if (!self?.admitted)
3767
4517
  return fail("NOT_ADMITTED", "only an admitted member can invite", m.cid);
4518
+ if (this.leftoverState(safeName(space.name)).state !== "live") {
4519
+ return fail("SPACE_HELD", `no invite can be made for "${space.name}" now: a team of this name was deleted on this relay and the relay cannot tell this one apart from it, so nobody new may join. Ask the relay operator.`, m.cid);
4520
+ }
3768
4521
  const plan = this.spacePlan(space.name);
3769
4522
  if (plan) {
3770
4523
  if (m.may_wake && !plan.limits.wake)
@@ -3775,6 +4528,8 @@ pantheon start</code></pre>
3775
4528
  return this.planRefusal(space, plan, "invite", `"${space.name}" already has ${open} unused invite link${open === 1 ? "" : "s"}, the most the ${plan.name} plan allows open at once. Cancel them (pantheon invite --revoke all) or wait for them to expire, or upgrade: ${FEEDBACK_ADDRESS}`, m.cid, fail, acct?.id ?? null, ip);
3776
4529
  }
3777
4530
  }
4531
+ if (!space.allow.hasToken)
4532
+ return fail("UNBOUND_SPACE", `"${space.name}" has no team token bound yet, so no invite can carry one — reconnect once (pantheon status), then invite`, m.cid);
3778
4533
  if (!this.writeOk(me))
3779
4534
  return fail("RATE_LIMIT", "too many invites too fast — wait a few seconds", m.cid);
3780
4535
  if (space.allow.openInvites().length >= LIMITS_SPACE.openInvites)
@@ -3935,6 +4690,7 @@ pantheon start</code></pre>
3935
4690
  }
3936
4691
  }
3937
4692
  o.claimed_by = { session: sessionId, role, at: Date.now() };
4693
+ space.store.noteFirstClaim({ ob: o.id, ref: o.ref, owner: o.owner, at: o.claimed_by.at, by: role === "worker" ? "worker" : "person" });
3938
4694
  this.broadcast(space, { t: "claimed", obligation: o.id, session: sessionId, member: me.id });
3939
4695
  this.pushObligations(space);
3940
4696
  this.send(ws, { t: "ack", ...(m.cid ? { cid: m.cid } : {}), id: o.id, seq: space.ledger.head });
@@ -4850,25 +5606,47 @@ pantheon start</code></pre>
4850
5606
  if (existing) {
4851
5607
  if (existing.name !== name)
4852
5608
  return null;
5609
+ if (opts.relayOnly)
5610
+ return existing;
4853
5611
  const st = this.tokenState(existing, token);
4854
- return st === "current" || (retired && st === "retired") ? existing : null;
5612
+ if (st === "current" || (retired && st === "retired"))
5613
+ return existing;
5614
+ if (!existing.allow.hasToken && token && claim && claim !== "invite") {
5615
+ const e = existing.allow.get(claim.member);
5616
+ if (e?.admitted && e.publicKey === claim.pubkey) {
5617
+ existing.allow.bindToken(token);
5618
+ existing.token = token;
5619
+ this.unbound.delete(safeName(existing.name));
5620
+ this.log(`[${name}] bound a token to a space that had none, on the reconnect of its admitted member ${claim.member}`);
5621
+ return existing;
5622
+ }
5623
+ }
5624
+ return null;
4855
5625
  }
4856
5626
  if (this.aliasOf(name))
4857
5627
  return null;
4858
5628
  const safe = name.replace(/[^a-z0-9._-]/gi, "_");
5629
+ {
5630
+ const ls = this.leftoverState(safe);
5631
+ if (ls.state === "covered" || (ls.state === "unsure" && !this.spaceOnDisk(name)))
5632
+ return null;
5633
+ }
4859
5634
  const allow = new Allowlist(this.dataDir, safe);
4860
5635
  const match = allow.tokenMatches(token);
4861
5636
  let raw = token;
4862
- if (match === false) {
5637
+ if (opts.relayOnly) {
5638
+ raw = match === true ? token : allow.currentTokenFromInvites() ?? "";
5639
+ }
5640
+ else if (match === false) {
4863
5641
  if (!(retired && allow.tokenRetired(token)))
4864
5642
  return null;
4865
5643
  raw = allow.currentTokenFromInvites() ?? "";
4866
5644
  }
4867
- if (match === null) {
5645
+ if (match === null && !opts.relayOnly) {
4868
5646
  if (this.unboundOnDisk(safe)) {
4869
5647
  const who = claim && claim !== "invite" ? claim : null;
4870
5648
  const e = who ? allow.get(who.member) : undefined;
4871
- const ok = claim === "invite" || (!!who && !!e?.admitted && e.publicKey === who.pubkey);
5649
+ const ok = !!who && !!e?.admitted && e.publicKey === who.pubkey;
4872
5650
  if (!ok) {
4873
5651
  this.unbound.add(safe);
4874
5652
  return null;
@@ -4930,22 +5708,7 @@ pantheon start</code></pre>
4930
5708
  }
4931
5709
  else if (restored.ledgerHead !== undefined && ledger.head > restored.ledgerHead) {
4932
5710
  const after = restored.ledgerHead;
4933
- const missed = ledger.all().filter((r) => r.seq > after);
4934
- const had = new Set(sp.obligations.keys());
4935
- let opened = 0;
4936
- for (const r of missed) {
4937
- if (r.goal && !sp.goals.has(r.goal)) {
4938
- const g = this.goalFromOpening(r);
4939
- if (g) {
4940
- sp.goals.set(g.id, g);
4941
- opened++;
4942
- }
4943
- }
4944
- this.effects(sp, r);
4945
- }
4946
- for (const o of sp.obligations.values())
4947
- if (!had.has(o.id))
4948
- o.created_at = ledger.find(o.ref)?.ts ?? o.created_at;
5711
+ const { missed, opened } = this.replayMissed(sp, after);
4949
5712
  this.log(`! [${name}] the snapshot was ${missed.length} act(s) behind the ledger (seq ${after} < ${ledger.head}) — replayed them${opened ? `, and made again ${opened} goal(s) opened among them` : ""}`);
4950
5713
  try {
4951
5714
  store.flush(sp.goals, sp.obligations, sp.profiles);
@@ -5007,6 +5770,48 @@ pantheon start</code></pre>
5007
5770
  sp.recent.delete(k);
5008
5771
  }
5009
5772
  }
5773
+ replayMissed(sp, after) {
5774
+ const missed = sp.ledger.all().filter((r) => r.seq > after);
5775
+ const had = new Set(sp.obligations.keys());
5776
+ let opened = 0;
5777
+ for (const r of missed) {
5778
+ if (r.goal && !sp.goals.has(r.goal)) {
5779
+ const g = this.goalFromOpening(r);
5780
+ if (g) {
5781
+ sp.goals.set(g.id, g);
5782
+ opened++;
5783
+ }
5784
+ }
5785
+ this.effects(sp, r);
5786
+ }
5787
+ for (const o of sp.obligations.values())
5788
+ if (!had.has(o.id))
5789
+ o.created_at = sp.ledger.find(o.ref)?.ts ?? o.created_at;
5790
+ return { missed, opened };
5791
+ }
5792
+ sleepingAsWoken(name, records, allow, loaded, savedAt, index) {
5793
+ const head = records.length ? records[records.length - 1].seq : 0;
5794
+ const behind = loaded.ledgerHead !== undefined && head > loaded.ledgerHead;
5795
+ const ahead = loaded.ledgerHead !== undefined && loaded.ledgerHead > head && records.length > 0;
5796
+ if (loaded.unclean || ahead) {
5797
+ return { goals: loaded.unclean ? new Map() : loaded.goals, obligations: new Map(), note: { kind: "rebuild" } };
5798
+ }
5799
+ if (loaded.rebuilt || !behind)
5800
+ return { goals: loaded.goals, obligations: loaded.obligations, note: null };
5801
+ const missed = head - loaded.ledgerHead;
5802
+ if (missed > SLEEPING_REPLAY_MAX) {
5803
+ return { goals: loaded.goals, obligations: loaded.obligations, note: { kind: "stale", asOf: savedAt ?? 0, behind: missed } };
5804
+ }
5805
+ const byId = index(records);
5806
+ const ledger = { all: () => records, find: (id) => byId.get(id), head };
5807
+ const sp = {
5808
+ name, token: "", ledger, allow, store: null, members: new Map(), recent: new Map(),
5809
+ goals: new Map([...loaded.goals].map(([k, g]) => [k, { ...g }])), obligations: new Map([...loaded.obligations].map(([k, o]) => [k, { ...o }])), profiles: loaded.profiles,
5810
+ lastActor: new Map(), contLocks: new Map(), createdAt: 0, lastTouch: 0,
5811
+ };
5812
+ this.replayMissed(sp, loaded.ledgerHead);
5813
+ return { goals: sp.goals, obligations: sp.obligations, note: null };
5814
+ }
5010
5815
  rebuildFromLedger(sp) {
5011
5816
  sp.goals.clear();
5012
5817
  sp.obligations.clear();
@@ -5089,8 +5894,15 @@ pantheon start</code></pre>
5089
5894
  };
5090
5895
  const referenced = new Set([...sp.obligations.values()].map((o) => o.ref));
5091
5896
  const before = sp.ledger.all().map((r) => r.id);
5092
- const { dropped, kept } = sp.ledger.compact({ keepRecent: this.opts.ledgerKeepRecent ?? LIMITS.ledgerKeepRecent, isLive, keep: (id) => referenced.has(id) });
5897
+ const { dropped, kept, newestDropped } = sp.ledger.compact({ keepRecent: this.opts.ledgerKeepRecent ?? LIMITS.ledgerKeepRecent, isLive, keep: (id) => referenced.has(id) });
5093
5898
  if (dropped) {
5899
+ sp.store.noteCompacted(newestDropped);
5900
+ try {
5901
+ sp.store.flush(sp.goals, sp.obligations, sp.profiles);
5902
+ }
5903
+ catch (e) {
5904
+ this.noteStorageFailure(`[${sp.name}] recording a compaction`, e);
5905
+ }
5094
5906
  const now = Date.now();
5095
5907
  const d = (sp.droppedIds ??= new Map());
5096
5908
  for (const [id, at] of d)