visualproof 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/assets/latex/visualproof.sty +19 -9
  2. package/dist/mcp/visualproof-mcp.mjs +7 -3
  3. package/dist/server/access.js +34 -0
  4. package/dist/server/agent.js +5 -4
  5. package/dist/server/agentlibrary.js +1 -1
  6. package/dist/server/agentviews.js +1 -1
  7. package/dist/server/artcmd.js +1 -1
  8. package/dist/server/askfollow.js +1 -4
  9. package/dist/server/events.js +20 -5
  10. package/dist/server/export.js +17 -4
  11. package/dist/server/fetchurl.js +30 -13
  12. package/dist/server/history.js +3 -1
  13. package/dist/server/identity.js +25 -1
  14. package/dist/server/library.js +10 -3
  15. package/dist/server/live.js +32 -5
  16. package/dist/server/lostfound.js +3 -2
  17. package/dist/server/main.js +113 -28
  18. package/dist/server/poppler.js +48 -10
  19. package/dist/server/renames.js +10 -5
  20. package/dist/server/review.js +10 -0
  21. package/dist/server/reviewapi.js +1 -1
  22. package/dist/server/status.js +29 -8
  23. package/dist/server/talk.js +7 -2
  24. package/dist/server/views.js +21 -7
  25. package/dist/shared/collab.js +14 -8
  26. package/dist/shared/doc/latex.js +5 -3
  27. package/dist/shared/doc/outline.js +6 -3
  28. package/dist/shared/library.js +11 -6
  29. package/dist/shared/review.js +7 -5
  30. package/dist/shared/search.js +7 -3
  31. package/dist/shared/views.js +8 -1
  32. package/dist/web/bundle/app.js +1646 -717
  33. package/dist/web/bundle/edges.js +42 -15
  34. package/dist/web/index.html +2 -2
  35. package/dist/web/src/artwork.js +25 -4
  36. package/dist/web/src/axiomring.js +42 -0
  37. package/dist/web/src/bundles.js +52 -16
  38. package/dist/web/src/canvas.js +62 -20
  39. package/dist/web/src/past.js +28 -0
  40. package/dist/web/src/selection.js +47 -4
  41. package/dist/web/src/status.js +4 -2
  42. package/dist/web/style.css +79 -10
  43. package/docs/agent-api/following.md +1 -1
  44. package/docs/agent-api/library.md +1 -1
  45. package/docs/agent-api/requests.md +3 -1
  46. package/docs/agent-api/views.md +1 -1
  47. package/package.json +1 -1
@@ -8,7 +8,7 @@ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "no
8
8
  import { createServer } from "node:http";
9
9
  import { basename, dirname, extname, join, resolve } from "node:path";
10
10
  import { tmpdir } from "node:os";
11
- import { gunzipSync, gzipSync } from "node:zlib";
11
+ import { gunzipSync, gzip, gzipSync } from "node:zlib";
12
12
  import { WEB, WEBFONTS, bundleDir, bundleDirIn, ensureBundle, prepare, useBundleDir } from "./pipeline.js";
13
13
  import { makeArtwork, pngOf, projectView } from "./artcmd.js";
14
14
  import { SiteBusy, cacheHome, dataHome, lockSite, openSite, projectDirs } from "./dirs.js";
@@ -19,12 +19,12 @@ import { isDeclName, LeanHover } from "./hover.js";
19
19
  import { Identities, layoutAuthor } from "./identity.js";
20
20
  import { LAYOUT_VERSION } from "../shared/graph.js";
21
21
  import { MAX_MEMORY_BYTES, readProjectView, Views } from "./views.js";
22
- import { cleanView, defaultAgentName, validSession, withoutArtwork } from "../shared/collab.js";
22
+ import { cleanView, defaultAgentName, nameKey, validSession, withoutArtwork } from "../shared/collab.js";
23
23
  import { writeSnapshot } from "./snapshot.js";
24
24
  import { lakeTextHash } from "./status.js";
25
25
  import { invalidTemplate } from "../shared/editor.js";
26
26
  import { packEdges } from "../shared/edgecodec.js";
27
- import { Access, KEY_PATH, loadKey } from "./access.js";
27
+ import { Access, hostAllowed, KEY_PATH, loadKey, urlHost } from "./access.js";
28
28
  import { INVITE_PATH, invalidPersonName, Invites, taken } from "./invites.js";
29
29
  import { AGENT_LEVELS, AgentApi, lower, needOf, refusal } from "./agent.js";
30
30
  import { AgentTokens, bearerOf, DEFAULT_DAYS, invalidAgentName, looksLikeToken, TOKEN_DAYS } from "./tokens.js";
@@ -53,7 +53,8 @@ function usage(code = 2) {
53
53
  [--site dir] [--cache dir] [--graph saved.json]
54
54
  [--identity-header X-Forwarded-User] [--editor URL-template]
55
55
  [--agents read|advise|full] [--access-key-file file] [--public] [--jobs ${DEFAULT_JOBS}]
56
- [--public-url https://host] [--library-limit GB] [--updates auto|notify|off]
56
+ [--public-url https://host] [--allowed-host name]... [--library-limit GB]
57
+ [--updates auto|notify|off]
57
58
  visualproof purge-file <lean-project> <sha256> [--site dir] [--cache dir]
58
59
  visualproof export <lean-project> -o page.html [--label text] [--root Name]... [--graph saved.json]
59
60
  [--jobs ${DEFAULT_JOBS}] [--site dir] [--cache dir]
@@ -136,6 +137,13 @@ function usage(code = 2) {
136
137
  --public-url the address people reach this server at, when it is not the one their browser
137
138
  sends (a proxy in front): the prompt Share makes for an agent tells it this address.
138
139
 
140
+ --allowed-host without --access-key-file, a name this server is reached by; repeatable. A
141
+ server without a key answers only requests for an IP address, localhost (and the names
142
+ under it), --public-url's host and these: any other name is answered 403, so that a web
143
+ page that makes its own name resolve to this machine (DNS rebinding) cannot use it. Give
144
+ the names people reach it by: a proxy's in front of it, this machine's own. With a key,
145
+ the key keeps everyone else out, whatever the name.
146
+
139
147
  --updates what the server does with VisualProof's own releases, asked of the npm registry
140
148
  (through npm) at start and once a day: auto (the default) — a patch of the version it
141
149
  runs (0.4.x) is downloaded into the cache and taken at a quiet moment, nobody on a page,
@@ -173,7 +181,7 @@ function parseArgs(argv) {
173
181
  console.error(`visualproof: VISUALPROOF_UPDATES=${fromEnv}: say auto, notify or off`);
174
182
  process.exit(2);
175
183
  }
176
- const o = { cmd, path: "", roots: [], port: 4173, host: "127.0.0.1", site: null, cache: null, graph: null, out: null, label: "VisualProof", identityHeader: null, editor: null, agents: "full", accessKeyFile: null, jobs: DEFAULT_JOBS, publicUrl: null, libraryLimit: DEFAULT_LIBRARY_LIMIT, sha: null, publicRead: false, recipe: null, png: false, svg: false, screen: { w: 1920, h: 1200 }, updates: UPDATE_MODES.includes(fromEnv) ? fromEnv : "auto" };
184
+ const o = { cmd, path: "", roots: [], port: 4173, host: "127.0.0.1", site: null, cache: null, graph: null, out: null, label: "VisualProof", identityHeader: null, editor: null, agents: "full", accessKeyFile: null, jobs: DEFAULT_JOBS, publicUrl: null, allowedHosts: [], libraryLimit: DEFAULT_LIBRARY_LIMIT, sha: null, publicRead: false, recipe: null, png: false, svg: false, screen: { w: 1920, h: 1200 }, updates: UPDATE_MODES.includes(fromEnv) ? fromEnv : "auto" };
177
185
  for (let i = 0; i < rest.length; i++) {
178
186
  const a = rest[i];
179
187
  const val = () => rest[++i] ?? usage();
@@ -208,9 +216,13 @@ function parseArgs(argv) {
208
216
  else if (a === "--svg" && cmd === "artwork")
209
217
  o.svg = true;
210
218
  else if (a === "--screen" && cmd === "artwork") {
211
- const m = /^(\d+)x(\d+)$/.exec(val());
212
- if (!m || +m[1] < 640 || +m[2] < 480 || +m[1] > 10000 || +m[2] > 10000)
213
- usage();
219
+ const v = val();
220
+ const m = /^(\d+)x(\d+)$/.exec(v);
221
+ // said in a line, as --public-url is, rather than the whole usage
222
+ if (!m || +m[1] < 640 || +m[2] < 480 || +m[1] > 10000 || +m[2] > 10000) {
223
+ console.error(`visualproof artwork: --screen ${v}: give the screen's width and height in pixels, e.g. 1920x1200, from 640x480 to 10000x10000`);
224
+ process.exit(2);
225
+ }
214
226
  o.screen = { w: +m[1], h: +m[2] };
215
227
  }
216
228
  else if (a === "--public-url") {
@@ -221,6 +233,15 @@ function parseArgs(argv) {
221
233
  }
222
234
  o.publicUrl = u.replace(/\/+$/, "");
223
235
  }
236
+ else if (a === "--allowed-host" && cmd === "serve") {
237
+ const h = val();
238
+ // a name, as a Host header gives it without its port (CollabSpec decision 12)
239
+ if (!/^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?)*\.?$/.test(h)) {
240
+ console.error(`visualproof: --allowed-host ${h}: give a name this server is reached by, without a scheme or a port, e.g. vp.example.org`);
241
+ process.exit(2);
242
+ }
243
+ o.allowedHosts.push(h);
244
+ }
224
245
  else if (a === "--jobs") {
225
246
  const n = Number(val());
226
247
  if (!Number.isInteger(n) || n < 0 || n > 256)
@@ -262,6 +283,10 @@ function parseArgs(argv) {
262
283
  console.error("visualproof: --public lets in read-only whoever has no key: it needs --access-key-file, and does not go with --identity-header");
263
284
  process.exit(2);
264
285
  }
286
+ // the names a server without a key answers: with one, the key keeps everyone else out whatever
287
+ // the name (CollabSpec decision 12)
288
+ if (o.allowedHosts.length && o.accessKeyFile)
289
+ console.warn("visualproof: --allowed-host changes nothing with --access-key-file: the key lets in whoever has it, by any name");
265
290
  if (cmd === "purge-file" && !validSha(o.sha)) {
266
291
  console.error("visualproof purge-file: give the file's SHA-256, 64 hex digits, as the page's library shows it.");
267
292
  process.exit(2);
@@ -296,6 +321,13 @@ export class App {
296
321
  /** --public: whoever comes without the key or a link of their own reads only (CollabSpec
297
322
  * decision 11) */
298
323
  publicRead;
324
+ /** without a key, the names this server answers besides its addresses and localhost
325
+ * (server/access.ts, hostAllowed): --public-url's host and every --allowed-host; null: every
326
+ * name, the key keeping everyone else out */
327
+ hostNames;
328
+ /** the names refused as Host, said once each in the server's console (a few: whoever asks
329
+ * chooses them) */
330
+ refusedHosts = new Set();
299
331
  /** the read-only readers' requests, by the read-only link they came by, or "public" */
300
332
  readOnlyOf = new WeakMap();
301
333
  /** what read-only readers may cost the server: Lean's answers, their documents */
@@ -303,7 +335,7 @@ export class App {
303
335
  /** VisualProof's own releases as this server follows them (server/updates.ts); null: the
304
336
  * version alone is told (a test's server) */
305
337
  updates = null;
306
- constructor(live, hover, ids, agents, access, exporter = null, publicUrl = null, publicRead = false) {
338
+ constructor(live, hover, ids, agents, access, exporter = null, publicUrl = null, publicRead = false, allowedHosts = []) {
307
339
  this.live = live;
308
340
  this.hover = hover;
309
341
  this.ids = ids;
@@ -317,6 +349,7 @@ export class App {
317
349
  agents.exporter = exporter;
318
350
  this.publicUrl = publicUrl;
319
351
  this.publicRead = publicRead && access !== null;
352
+ this.hostNames = access ? null : [...(publicUrl ? [urlHost(publicUrl)] : []), ...allowedHosts];
320
353
  this.bounds = new ReadOnlyBounds((id) => this.exporter?.job(id)?.state === "running");
321
354
  }
322
355
  /** Whether a request is told all of the server's own releases (UpdateSpec decision 11): someone
@@ -531,7 +564,7 @@ export class App {
531
564
  return sendJson(res, { error: "to is a reader's id, as presence lists them; left out, everyone." }, 400);
532
565
  const person = this.ids.person(req);
533
566
  // one request every 30 s per person — their personal link, else their name — and per page
534
- const key = person ? `p:${person.id}` : `n:${me.trim().normalize("NFKC").toLowerCase()}`;
567
+ const key = person ? `p:${person.id}` : `n:${nameKey(me)}`;
535
568
  const a = live.asks.ask({ name: me, verified: !!person, agent: false, from: page.id, stream: b.token, rate: [key, `s:${b.token}`] }, typeof b.to === "string" ? { page: b.to } : { everyone: true });
536
569
  if ("ok" in a) {
537
570
  const { report: _, ...sent } = a.ok;
@@ -681,10 +714,8 @@ export class App {
681
714
  res.writeHead(303, { location: "/", "set-cookie": this.live.invites.cookie(c.ok), "cache-control": "no-store", "referrer-policy": "no-referrer" });
682
715
  return void res.end();
683
716
  }
684
- if (c) {
685
- const r = c.record;
686
- return sendText(req, res, `This personal link was revoked by ${r.revokedBy} on ${r.revoked.slice(0, 10)}. Ask whoever invited you for a new one.\n`, "text/plain; charset=utf-8", 410);
687
- }
717
+ if (c)
718
+ return sendText(req, res, revokedWords(c.record, "personal") + "\n", "text/plain; charset=utf-8", 410);
688
719
  sendJson(res, { error: "not found" }, 404);
689
720
  }
690
721
  /** A read-only link opened (CollabSpec decision 11): the browser is given the cookie that lets
@@ -697,10 +728,8 @@ export class App {
697
728
  res.writeHead(303, { location: "/", "set-cookie": this.live.readOnly.cookie(c.ok.id), "cache-control": "no-store", "referrer-policy": "no-referrer" });
698
729
  return void res.end();
699
730
  }
700
- if (c) {
701
- const l = c.link;
702
- return sendText(req, res, `This read-only link was revoked by ${l.revokedBy} on ${l.revoked.slice(0, 10)}. Ask whoever gave it to you for a new one.\n`, "text/plain; charset=utf-8", 410);
703
- }
731
+ if (c)
732
+ return sendText(req, res, revokedWords(c.link, "read-only") + "\n", "text/plain; charset=utf-8", 410);
704
733
  sendJson(res, { error: "not found" }, 404);
705
734
  }
706
735
  /** Who the reader is (POST /api/identity {name}): the name someone invited by a personal link goes
@@ -736,6 +765,19 @@ export class App {
736
765
  this.live.review.named(p.id, done.name);
737
766
  return sendJson(res, { ok: true, name: done.name, person: Invites.info(done) });
738
767
  }
768
+ /** A request for a name this server without a key does not answer (CollabSpec decision 12): 403,
769
+ * saying which option lets the name in; and, once per name, the server's console says it too,
770
+ * for whoever runs it behind a proxy that forwards the name people reach it by. */
771
+ refuseHost(req, res) {
772
+ const raw = req.headers.host ?? "";
773
+ const name = /^\[/.test(raw) ? raw : raw.replace(/:\d{1,5}$/, "");
774
+ const shown = /^[A-Za-z0-9._\-[\]:]{1,253}$/.test(name) ? name : null;
775
+ if (shown && this.refusedHosts.size < 20 && !this.refusedHosts.has(shown.toLowerCase())) {
776
+ this.refusedHosts.add(shown.toLowerCase());
777
+ console.error(`refused a request for ${shown}: without an access key this server answers only its addresses, localhost, --public-url's host and the --allowed-host names; --allowed-host ${shown} lets this one in`);
778
+ }
779
+ sendText(req, res, `This server answers only the names it is meant to be reached by${shown ? `, and ${shown} is not one of them` : ""}. Whoever runs it can let a name in with --allowed-host${shown ? ` ${shown}` : " <name>"}.\n`, "text/plain; charset=utf-8", 403);
780
+ }
739
781
  /** The answer to a request that needs a name and has none: the page asks for one (`who`). Where
740
782
  * the name the page gives is a personal link holder's, it says so, and the page asks for
741
783
  * another. */
@@ -818,6 +860,12 @@ export class App {
818
860
  const url = new URL(req.url ?? "/", "http://localhost");
819
861
  const path = url.pathname;
820
862
  const live = this.live;
863
+ // without a key, only the names this server is reached at (CollabSpec decision 12, the owner's
864
+ // decision of 2026-10-18): a page whose own name was made to resolve to this machine (DNS
865
+ // rebinding) is refused whatever it asks — the page, its data, the event stream, the agents'
866
+ // API, the MCP server's file
867
+ if (this.hostNames && !hostAllowed(req.headers.host, this.hostNames))
868
+ return this.refuseHost(req, res);
821
869
  // every route; whatever one throws, handle() answers
822
870
  {
823
871
  // an agent's own token (server/tokens.ts) lets it into its API alone: the page and the page's
@@ -855,6 +903,17 @@ export class App {
855
903
  if (a === "out") {
856
904
  // nothing lets them in to write: their read-only link does, or --public, to read
857
905
  const reader = ro && "ok" in ro ? ro.ok.id : this.publicRead && !this.ids.header ? "public" : null;
906
+ // a page whose link was revoked, which nothing else lets in, is told so (CollabSpec
907
+ // decisions 9 and 11): its requests are answered 410 with who revoked it and when, and the
908
+ // page, reloaded, says it in words; the cookie is forgotten there, once it has been said
909
+ const revoked = reader === null && this.ids.kind(req) !== "agent" ? (link && "ended" in link ? revokedWords(link.record, "personal") : ro && "ended" in ro ? revokedWords(ro.link, "read-only") : null) : null;
910
+ if (revoked) {
911
+ if (path === "/" || path === "/index.html") {
912
+ res.setHeader("set-cookie", cookies);
913
+ return sendText(req, res, revoked + "\n", "text/plain; charset=utf-8", 410);
914
+ }
915
+ return sendJson(res, { error: revoked, revoked: true }, 410);
916
+ }
858
917
  if (reader === null || this.ids.kind(req) === "agent") {
859
918
  if (cookies.length)
860
919
  res.setHeader("set-cookie", cookies);
@@ -983,14 +1042,24 @@ export class App {
983
1042
  }
984
1043
  // the library of papers and images, and the links into them (AttachmentSpec.md,
985
1044
  // server/libraryroute.ts)
986
- if (path === "/api/library" || path.startsWith("/api/library/"))
1045
+ if (path === "/api/library" || path.startsWith("/api/library/")) {
1046
+ // a read-only reader (CollabSpec decision 11) is drawn what the notes show of a PDF, kept in
1047
+ // the cache as it is drawn, and no other page or region of one: each would be drawn by
1048
+ // Poppler and kept anew, whoever comes asking
1049
+ if (reader !== undefined && (path === "/api/library/page" || path === "/api/library/clip")) {
1050
+ const q = url.searchParams;
1051
+ const region = path === "/api/library/clip" ? (q.get("region") ?? "").split(",").map(Number) : null;
1052
+ if (!live.views.shows(q.get("sha") ?? "", Number(q.get("page")), region))
1053
+ return sendJson(res, { error: "A read-only page is drawn what the notes show of the library: open the file itself.", readOnly: true }, 403);
1054
+ }
987
1055
  return libraryRoute({ ids: this.ids, level: this.agents.level, api: this.library() }, req, res, url);
1056
+ }
988
1057
  // an agent may use the page's own endpoints too: the same rules hold there (AgentSpec)
989
1058
  const agent = this.ids.kind(req) === "agent";
990
1059
  // the review (ReviewSpec.md): the whole of it, as a page takes it on, with the lines of its
991
1060
  // logs that cannot be read
992
1061
  if (path === "/api/review" && req.method === "GET")
993
- return sendJson(res, live.review.whole());
1062
+ return sendJsonText(res, live.review.wholeText());
994
1063
  if (path === "/api/review" && req.method === "POST") {
995
1064
  // an opinion, a target, a trusted prefix or a key declaration is recorded under the
996
1065
  // reader's name (CollabSpec decision 3), not one in the body; what it is about is
@@ -1199,6 +1268,10 @@ export class App {
1199
1268
  }
1200
1269
  }
1201
1270
  }
1271
+ /** What a revoked link's holder is told, opening it or on a page it had opened. */
1272
+ function revokedWords(r, kind) {
1273
+ return `This ${kind} link was revoked by ${r.revokedBy} on ${(r.revoked ?? "").slice(0, 10)}. Ask whoever ${kind === "personal" ? "invited you" : "gave it to you"} for a new one.`;
1274
+ }
1202
1275
  /** an artwork's font file (assets/webfonts/) */
1203
1276
  const FONT_PATH = /^\/fonts\/(source-sans-3|source-serif-4|jost)-[a-z-]+\.woff2$/;
1204
1277
  const TYPES = {
@@ -1310,14 +1383,22 @@ function tooLarge(req, res, body) {
1310
1383
  }
1311
1384
  /** Send a JSON answer, once: a handler that answers after something else already did (a body too
1312
1385
  * large, answered as it was read) is not heard. */
1313
- function sendJson(res, body, code = 200, headers = {}) {
1386
+ export function sendJson(res, body, code = 200, headers = {}) {
1314
1387
  if (res.headersSent || res.writableEnded || refused.has(res))
1315
1388
  return;
1316
- const text = JSON.stringify(body);
1317
- // a small answer is not worth compressing; a status with its build log, a source file, are
1389
+ sendJsonText(res, JSON.stringify(body), code, headers);
1390
+ }
1391
+ /** The same, of an answer written as JSON already (the whole review, server/review.ts). */
1392
+ function sendJsonText(res, text, code = 200, headers = {}) {
1393
+ if (res.headersSent || res.writableEnded || refused.has(res))
1394
+ return;
1395
+ // a small answer is not worth compressing; a status with its build log, a source file, are; a
1396
+ // large one is compressed off the server's thread, which meanwhile answers others
1318
1397
  if (text.length > JSON_GZIP_FROM && acceptsGzip(res.req)) {
1319
1398
  res.writeHead(code, { "content-type": "application/json", "cache-control": "no-store", "content-encoding": "gzip", vary: "accept-encoding", ...headers });
1320
- return void res.end(gzipSync(text));
1399
+ if (text.length < JSON_GZIP_AWAY_FROM)
1400
+ return void res.end(gzipSync(text));
1401
+ return void gzip(text, (e, gz) => (e ? res.destroy(e) : res.end(gz)));
1321
1402
  }
1322
1403
  res.writeHead(code, { "content-type": "application/json", "cache-control": "no-store", ...headers });
1323
1404
  res.end(text);
@@ -1362,6 +1443,10 @@ export function reachedAt(req) {
1362
1443
  }
1363
1444
  /** characters from which a JSON answer is sent compressed */
1364
1445
  const JSON_GZIP_FROM = 2048;
1446
+ /** characters from which a JSON answer is compressed off the server's thread: a snapshot's whole
1447
+ * graph on NavierStokesAndEuler (GET /api/agent/history/graph, 105 MB of JSON) was 2 to 3 s of
1448
+ * compressing, every page and agent waiting meanwhile */
1449
+ export const JSON_GZIP_AWAY_FROM = 1 << 20;
1365
1450
  /** the graph's parts as sent, each tagged once (a graph's buffers live as long as it does) */
1366
1451
  const gzTags = new WeakMap();
1367
1452
  /** A part of the graph, gzipped already: tagged, so that a page that has it gets 304. */
@@ -1513,7 +1598,7 @@ async function main() {
1513
1598
  live.readOnly.close();
1514
1599
  });
1515
1600
  const exporter = new Exporter(dirs.cache);
1516
- const app = new App(live, hover, ids, new AgentApi(live, hover, o.agents), key ? new Access(key.key) : null, exporter, o.publicUrl, o.publicRead);
1601
+ const app = new App(live, hover, ids, new AgentApi(live, hover, o.agents), key ? new Access(key.key) : null, exporter, o.publicUrl, o.publicRead, o.allowedHosts);
1517
1602
  const server = createServer((req, res) => app.handle(req, res));
1518
1603
  const version = packageVersion();
1519
1604
  // run by the supervisor, which starts it again in another version (UpdateSpec decision 5)
@@ -1592,7 +1677,7 @@ async function main() {
1592
1677
  console.log(` editor: ${o.editor ?? "no link unless a reader sets one"}`);
1593
1678
  console.log(` agents: ${o.agents} — their API is http://${o.host}:${o.port}/api/agent`);
1594
1679
  console.log(` usage pass: ${o.graph ? "none for a saved graph" : o.jobs > 0 ? `${o.jobs} module(s) at a time` : "off (--jobs 0)"}`);
1595
- console.log(` access: ${key ? `by its link only, /k/<the key in ${o.accessKeyFile}>${key.made ? " (a new key was made there)" : ""}${o.publicRead ? "; anyone else read-only (--public)" : ""}` : "anyone who reaches the port"}`);
1680
+ console.log(` access: ${key ? `by its link only, /k/<the key in ${o.accessKeyFile}>${key.made ? " (a new key was made there)" : ""}${o.publicRead ? "; anyone else read-only (--public)" : ""}` : `anyone who reaches the port, by its address, localhost${[...(o.publicUrl ? [urlHost(o.publicUrl)] : []), ...o.allowedHosts].map((n) => `, ${n}`).join("")} (other names are refused: --allowed-host)`}`);
1596
1681
  console.log(` version: ${version}; updates ${updatesInWords(o.updates, version, supervised)}`);
1597
1682
  recordVersion(live, version, supervised);
1598
1683
  process.send?.({ vp: "listening", version });
@@ -1696,11 +1781,11 @@ async function artwork(project, o, fail) {
1696
1781
  // the site's layout memory read and never written, nor the views laid out written to the cache;
1697
1782
  // without a site, none read: the board as a new site lays it out
1698
1783
  const prep = await prepare(project, dirs, o.graph, log, { ...(ex ? { extraction: ex } : {}), readOnlySite: true, ...(site ? {} : { freshLayout: true }) });
1699
- const kept = site ? readProjectView(site, LAYOUT_VERSION) : { view: null };
1784
+ const kept = site ? readProjectView(site, LAYOUT_VERSION) : { view: null, notes: 0 };
1700
1785
  if (kept.why)
1701
1786
  console.error(`visualproof artwork: the site's views cannot be read (${kept.why}): the Project view as a new site has it`);
1702
1787
  const review = site ? new ReviewStore(project.root, site).data : null;
1703
- const a = makeArtwork(prep, review, projectView(kept.view), { recipe, screen: o.screen, name: outBase ? basename(outBase) : null, root: project.root });
1788
+ const a = makeArtwork(prep, review, projectView(kept.view), { recipe, screen: o.screen, name: outBase ? basename(outBase) : null, root: project.root, projectNotes: kept.notes });
1704
1789
  if (!("svg" in a)) {
1705
1790
  for (const p of a.problems)
1706
1791
  console.error(`visualproof artwork: ${p}`);
@@ -11,10 +11,10 @@
11
11
  // most two at once.
12
12
  import { execFile } from "node:child_process";
13
13
  import { tracked } from "./children.js";
14
- import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
14
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
15
15
  import { join } from "node:path";
16
16
  import { gunzipSync, gzipSync } from "node:zlib";
17
- import { wordsText } from "../shared/library.js";
17
+ import { plainPunctuation, wordsText } from "../shared/library.js";
18
18
  /** the longest each tool may run, in ms */
19
19
  const INFO_MS = 20_000;
20
20
  const TEXT_MS = 120_000;
@@ -111,16 +111,17 @@ export function parseBboxLayout(xhtml) {
111
111
  export function pageText(p) {
112
112
  return wordsText(p.words, p.lines);
113
113
  }
114
- /** Find `q` in every page's text: case and runs of white space read loosely; at most `most`
115
- * matches, in page order. */
114
+ /** Find `q` in every page's text: case, runs of white space and typographic quotes and dashes read
115
+ * loosely; at most `most` matches, in page order. */
116
116
  export function searchWords(pages, q, most = 100) {
117
- const want = q.replace(/\s+/g, " ").trim().toLowerCase();
117
+ // typed as a keyboard types it: ' finds ’, - finds – (shared/library.ts, plainPunctuation)
118
+ const want = plainPunctuation(q.replace(/\s+/g, " ").trim().toLowerCase());
118
119
  const matches = [];
119
120
  if (!want)
120
121
  return { matches, more: false };
121
122
  for (const p of pages) {
122
123
  const { text, starts } = pageText(p);
123
- const flat = text.replace(/\n/g, " ").toLowerCase();
124
+ const flat = plainPunctuation(text.replace(/\n/g, " ").toLowerCase());
124
125
  for (let at = flat.indexOf(want); at >= 0; at = flat.indexOf(want, at + 1)) {
125
126
  if (matches.length >= most)
126
127
  return { matches, more: true };
@@ -150,13 +151,20 @@ function upper(starts, at) {
150
151
  /** The widths a page or a clip is drawn at: a few, so that what is kept stays small. */
151
152
  const WIDTHS = [400, 800, 1200, 1600, 2000];
152
153
  export const clampWidth = (w) => WIDTHS.find((x) => x >= w) ?? WIDTHS[WIDTHS.length - 1];
154
+ /** The most clips kept of a file, the oldest going first. A page is kept at a few widths, bounded by
155
+ * the file; but anyone who reads — a read-only reader, whoever comes to a `--public` site — may ask
156
+ * for any region of any page, and each was kept for good, filling the disk. The clips notes carry
157
+ * are asked again and again, and stay. */
158
+ export const MAX_CLIPS = 500;
153
159
  /** A PDF's words, pages and images, kept in `cacheDir/<sha>/`. */
154
160
  export class PdfReader {
155
161
  cacheDir;
156
162
  words = new Map();
157
163
  infos = new Map();
158
- constructor(cacheDir) {
164
+ maxClips;
165
+ constructor(cacheDir, maxClips = MAX_CLIPS) {
159
166
  this.cacheDir = cacheDir;
167
+ this.maxClips = maxClips;
160
168
  }
161
169
  dir(sha) {
162
170
  const d = join(this.cacheDir, sha);
@@ -220,8 +228,17 @@ export class PdfReader {
220
228
  const w = clampWidth(width);
221
229
  const r = region.map((x) => Math.round(x * 1e4) / 1e4);
222
230
  const kept = join(this.dir(sha), `clip-${page}-${r.join("_")}-w${w}.png`);
223
- if (existsSync(kept))
224
- return readFileSync(kept);
231
+ if (existsSync(kept)) {
232
+ try {
233
+ // asked for again: the last to go (atime is not kept on every system)
234
+ const now = new Date();
235
+ utimesSync(kept, now, now);
236
+ return readFileSync(kept);
237
+ }
238
+ catch {
239
+ // pruned meanwhile: drawn again
240
+ }
241
+ }
225
242
  // the resolution at which the region is `w` pixels wide, within reason
226
243
  const dpi = Math.min(600, Math.max(36, (w / ((r[2] - r[0]) * size.w)) * 72));
227
244
  const px = (v, of) => Math.round((v * of * dpi) / 72);
@@ -235,7 +252,28 @@ export class PdfReader {
235
252
  finally {
236
253
  rmSync(`${tmp}.png`, { force: true });
237
254
  }
238
- return readFileSync(kept);
255
+ const out = readFileSync(kept);
256
+ this.pruneClips(sha);
257
+ return out;
258
+ }
259
+ /** The clips kept of a file beyond the most there may be go, those drawn or asked for longest
260
+ * ago first (a clip asked for again is touched). */
261
+ pruneClips(sha) {
262
+ const d = join(this.cacheDir, sha);
263
+ let clips;
264
+ try {
265
+ clips = readdirSync(d)
266
+ .filter((f) => f.startsWith("clip-"))
267
+ .map((f) => ({ f, t: statSync(join(d, f)).mtimeMs }));
268
+ }
269
+ catch {
270
+ return;
271
+ }
272
+ if (clips.length <= this.maxClips)
273
+ return;
274
+ clips.sort((a, b) => a.t - b.t);
275
+ for (const c of clips.slice(0, clips.length - this.maxClips))
276
+ rmSync(join(d, c.f), { force: true });
239
277
  }
240
278
  /** What is kept of a file goes (its bytes purged). */
241
279
  forget(sha) {
@@ -16,6 +16,7 @@
16
16
  // What a module of either build that could not be read held is set aside: it did not vanish.
17
17
  import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
18
18
  import { join } from "node:path";
19
+ import { endsLine } from "./lostfound.js";
19
20
  import { refIdentity, versionOf } from "./versions.js";
20
21
  /** What stands for the declaration itself in its own references, so that a declaration can be
21
22
  * paired before its own rename is known. */
@@ -202,8 +203,7 @@ export class Renames {
202
203
  record(snap, pairs) {
203
204
  if (!pairs.length)
204
205
  return;
205
- mkdirSync(join(this.pairFile, ".."), { recursive: true });
206
- appendFileSync(this.pairFile, JSON.stringify({ snap, at: new Date().toISOString(), pairs }) + "\n");
206
+ append(this.pairFile, { snap, at: new Date().toISOString(), pairs });
207
207
  this.add(snap, pairs);
208
208
  }
209
209
  get(id) {
@@ -212,8 +212,7 @@ export class Renames {
212
212
  /** A migration made: kept, under the next id. */
213
213
  migrated(m) {
214
214
  const full = { id: `m${this.migrations.length + 1}`, ...m };
215
- mkdirSync(join(this.migFile, ".."), { recursive: true });
216
- appendFileSync(this.migFile, JSON.stringify(full) + "\n");
215
+ append(this.migFile, full);
217
216
  this.migrations.push(full);
218
217
  this.byId.set(full.id, full);
219
218
  return full;
@@ -225,7 +224,7 @@ export class Renames {
225
224
  return;
226
225
  const at = new Date().toISOString();
227
226
  const who = { by, ...(agent ? { agent: true } : {}), ...(person ? { person } : {}) };
228
- appendFileSync(this.migFile, JSON.stringify({ undo: id, ...who, at }) + "\n");
227
+ append(this.migFile, { undo: id, ...who, at });
229
228
  m.undone = { ...who, at };
230
229
  }
231
230
  /** The renames of high confidence found from snapshot `from` to snapshot `to`, composed along the
@@ -250,6 +249,12 @@ export class Renames {
250
249
  return out;
251
250
  }
252
251
  }
252
+ /** A line appended to one of the files: after a line cut short (the server stopped while writing),
253
+ * on a line of its own, so that the line cut short alone is lost, never the one written after it. */
254
+ function append(file, o) {
255
+ mkdirSync(join(file, ".."), { recursive: true });
256
+ appendFileSync(file, (endsLine(file) ? "" : "\n") + JSON.stringify(o) + "\n");
257
+ }
253
258
  function lines(file) {
254
259
  if (!existsSync(file))
255
260
  return [];
@@ -70,6 +70,16 @@ export class ReviewStore {
70
70
  whole() {
71
71
  return { epoch: this.epoch, rev: this.rev, data: this.data, problems: this.problems, error: this.error };
72
72
  }
73
+ /** the whole review as JSON, and the revision it is of */
74
+ wholeJson = null;
75
+ /** The whole review as JSON, written once a revision: every page's event stream opens with it and
76
+ * every page that loads asks for it, and with thousands of opinions it is megabytes (7.6 MB for
77
+ * 5,000 on Sphere-Packing-Lean: 50 ms each time, a hundred pages opening held the server 5 s). */
78
+ wholeText() {
79
+ if (this.wholeJson?.rev !== this.rev)
80
+ this.wholeJson = { rev: this.rev, text: JSON.stringify(this.whole()) };
81
+ return this.wholeJson.text;
82
+ }
73
83
  /** The file a writer's events go to (shared/review.ts, logFileName). */
74
84
  fileOf(w) {
75
85
  return w.server ? SERVER_LOG : logFileName(w.name, w.agent, w.person ?? null);
@@ -199,7 +199,7 @@ export class ReviewApi {
199
199
  ...Object.values(d.targets).map((t) => ({ by: t.by, agent: !!t.agent, person: !!t.person })),
200
200
  ...Object.values(d.keys).flatMap((marks) => Object.values(marks).map((m) => ({ by: m.by, agent: !!m.agent, person: !!m.person }))),
201
201
  ];
202
- if (writers.some((w) => w.agent && sameName(w.by, name)))
202
+ if (this.world.agentNamed?.(name) || writers.some((w) => w.agent && sameName(w.by, name)))
203
203
  return refuse(400, `${name} is an agent: nobody records an opinion for an agent.`, "Record it for a person, or as your own.");
204
204
  if (held)
205
205
  return { for: held.name, forPerson: held.id };
@@ -227,7 +227,9 @@ export class BuildTracker {
227
227
  p;
228
228
  /** when outputs were last seen changing, to bridge the gaps between compile processes */
229
229
  lastChange = 0;
230
- lastStamp = "";
230
+ /** each module's outputs as last seen (outputsMoved), and when; null before the first look */
231
+ seen = null;
232
+ seenAt = 0;
231
233
  buildStart = 0;
232
234
  expected = null;
233
235
  constructor(p) {
@@ -237,13 +239,8 @@ export class BuildTracker {
237
239
  const p = this.p;
238
240
  const now = Date.now();
239
241
  const running = await compiling(p);
240
- // only outputs moving count as a build: an edit to a source is not one
241
- const st = outputStamp(p, mods);
242
- if (st !== this.lastStamp) {
243
- if (this.lastStamp)
244
- this.lastChange = now;
245
- this.lastStamp = st;
246
- }
242
+ if (this.outputsMoved(mods, now))
243
+ this.lastChange = now;
247
244
  const active = running.size > 0 || now - this.lastChange < 2500;
248
245
  const probes = new Map();
249
246
  for (const m of mods.values())
@@ -341,6 +338,30 @@ export class BuildTracker {
341
338
  counts[f.state]++;
342
339
  return { active, progress, files, counts };
343
340
  }
341
+ /** Whether the build's outputs moved since the last look: only outputs moving count as a build.
342
+ * An edit to a source is not one, nor a file the project gains or loses while nothing is written
343
+ * (an import added of a file not built yet, one taken out): a module new to the list moved only
344
+ * where what it has was written since the last look. The first look is no change. */
345
+ outputsMoved(mods, now) {
346
+ const was = this.seen;
347
+ const since = this.seenAt;
348
+ const next = new Map();
349
+ let moved = false;
350
+ for (const m of mods.values()) {
351
+ const base = join(this.p.libDir, ...m.name.split("."));
352
+ const olean = mtime(base + ".olean"), trace = mtime(base + ".trace");
353
+ const s = `${olean}:${trace}`;
354
+ next.set(m.name, s);
355
+ if (!was)
356
+ continue;
357
+ const before = was.get(m.name);
358
+ if (before === undefined ? Math.max(olean ?? 0, trace ?? 0) > since : before !== s)
359
+ moved = true;
360
+ }
361
+ this.seen = next;
362
+ this.seenAt = now;
363
+ return moved;
364
+ }
344
365
  }
345
366
  /** The modules that can be imported right now: built, with everything under them built. */
346
367
  export function importable(p, mods) {
@@ -147,9 +147,13 @@ export class Talk {
147
147
  }
148
148
  /** The agent said it leaves (POST /api/agent/view {leave: true}; its MCP server does as it
149
149
  * stops): offline at once, its wait ended, until it waits or asks again; its next wait is a new
150
- * session's. */
151
- left(token, now = Date.now()) {
150
+ * session's. A leave from another session than the one that waited last (`session`, as
151
+ * X-VisualProof-Session says it) is one a newer session replaced, whose client goes now: it
152
+ * leaves nothing, the newer one listening on (false). */
153
+ left(token, session = null, now = Date.now()) {
152
154
  const a = this.agent(token);
155
+ if (session !== null && a.session !== null && a.session !== session)
156
+ return false;
153
157
  a.wait?.end({ gone: true });
154
158
  a.lastRequest = 0;
155
159
  a.graceUntil = 0;
@@ -158,6 +162,7 @@ export class Talk {
158
162
  a.away = true;
159
163
  a.before = undefined;
160
164
  this.update(token, now);
165
+ return true;
161
166
  }
162
167
  /** A request of the agent's: it is not offline, and `doing` (when it says something) is what it
163
168
  * does now. */