visualproof 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +16 -7
  2. package/dist/assets/tips/tour-agent.webp +0 -0
  3. package/dist/assets/tips/tour-arrange.webp +0 -0
  4. package/dist/assets/tips/tour-artwork.webp +0 -0
  5. package/dist/assets/tips/tour-checks.webp +0 -0
  6. package/dist/assets/tips/tour-keys.webp +0 -0
  7. package/dist/assets/tips/tour-library.webp +0 -0
  8. package/dist/assets/tips/tour-project.webp +0 -0
  9. package/dist/assets/tips/tour-review.webp +0 -0
  10. package/dist/assets/tips/tour-share.webp +0 -0
  11. package/dist/assets/tips/tour-source.webp +0 -0
  12. package/dist/assets/tips/tour-target.webp +0 -0
  13. package/dist/assets/tips/tour-view.webp +0 -0
  14. package/dist/assets/tips/tour.json +117 -112
  15. package/dist/assets/tips/tour.webp +0 -0
  16. package/dist/server/access.js +3 -1
  17. package/dist/server/accesspage.js +130 -0
  18. package/dist/server/agent.js +6 -1
  19. package/dist/server/buildwatch.js +133 -0
  20. package/dist/server/deploy.js +844 -0
  21. package/dist/server/doctor.js +15 -3
  22. package/dist/server/film.js +1 -1
  23. package/dist/server/historyapi.js +100 -50
  24. package/dist/server/historythread.js +206 -0
  25. package/dist/server/hover.js +8 -0
  26. package/dist/server/live.js +187 -36
  27. package/dist/server/main.js +256 -30
  28. package/dist/server/review.js +27 -7
  29. package/dist/server/status.js +119 -46
  30. package/dist/server/usage.js +51 -0
  31. package/dist/shared/board.js +2 -1
  32. package/dist/shared/deploy.js +35 -0
  33. package/dist/shared/layout.js +297 -70
  34. package/dist/shared/libm.js +366 -0
  35. package/dist/shared/palette.js +1 -1
  36. package/dist/shared/separate.js +556 -32
  37. package/dist/web/bundle/app.js +1972 -826
  38. package/dist/web/bundle/edges.js +2 -1
  39. package/dist/web/bundle/layout.js +1026 -80
  40. package/dist/web/src/canvas.js +47 -34
  41. package/dist/web/src/overview.js +4 -1
  42. package/dist/web/src/scene.js +13 -12
  43. package/dist/web/src/status.js +28 -0
  44. package/dist/web/src/theme.js +108 -0
  45. package/dist/web/style.css +102 -35
  46. package/docs/agent-api/history.md +3 -0
  47. package/docs/agent-api/requests.md +4 -3
  48. package/package.json +1 -1
@@ -7,8 +7,8 @@ import { createHash } from "node:crypto";
7
7
  import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
8
8
  import { createServer } from "node:http";
9
9
  import { basename, dirname, extname, join, resolve } from "node:path";
10
- import { tmpdir } from "node:os";
11
- import { gunzipSync, gzip, gzipSync } from "node:zlib";
10
+ import { freemem, tmpdir } from "node:os";
11
+ import { gunzip, 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";
@@ -25,12 +25,13 @@ import { lakeTextHash } from "./status.js";
25
25
  import { invalidTemplate } from "../shared/editor.js";
26
26
  import { packEdges } from "../shared/edgecodec.js";
27
27
  import { Access, hostAllowed, KEY_PATH, loadKey, urlHost } from "./access.js";
28
+ import { asksForPage, sendAccessPage } from "./accesspage.js";
28
29
  import { INVITE_PATH, invalidPersonName, Invites, taken } from "./invites.js";
29
30
  import { AGENT_LEVELS, AgentApi, lower, needOf, refusal } from "./agent.js";
30
31
  import { AgentTokens, bearerOf, DEFAULT_DAYS, invalidAgentName, looksLikeToken, TOKEN_DAYS } from "./tokens.js";
31
32
  import { agentPrompt } from "./prompt.js";
32
33
  import { MAX_PERSON } from "./talk.js";
33
- import { DEFAULT_JOBS, UsagePass, mergeUsage, saveMerged } from "./usage.js";
34
+ import { DEFAULT_JOBS, inGB, jobsAdvice, memoryFloor, onMathlib, UsagePass, mergeUsage, saveMerged, WORKER_PEAK } from "./usage.js";
34
35
  import { extract } from "./extract.js";
35
36
  import { Exporter } from "./export.js";
36
37
  import { Library, libraryApiOf } from "./library.js";
@@ -46,10 +47,12 @@ import { RESTART_STATUS, supervise } from "./supervise.js";
46
47
  import { stopChildren } from "./children.js";
47
48
  import { filmStatus, serveFilm, useFilm } from "./film.js";
48
49
  import { finalAnswer, readOnlyAllows, ReadOnlyBounds } from "./readonlygate.js";
50
+ import { branchProblem, branchToFollow, DEFAULT_POLL_MINUTES, DeployBusy, deployFolder, Deployment, deploySite, ensureClone, isGitAddress, lockDeployment, MIN_POLL_MINUTES, parseRemote, recordedDefault } from "./deploy.js";
51
+ import { shortCommit, slimDeploy } from "../shared/deploy.js";
49
52
  /** The usage, to stderr with status 2 for a command line it cannot read; asked for (`--help`),
50
53
  * to stdout with status 0. */
51
54
  function usage(code = 2) {
52
- (code === 0 ? console.log : console.error)(`usage: visualproof serve <lean-project> [--root Name]... [--port 4173] [--host 127.0.0.1]
55
+ (code === 0 ? console.log : console.error)(`usage: visualproof serve <lean-project | git URL> [--branch name] [--poll ${DEFAULT_POLL_MINUTES}] [--root Name]... [--port 4173] [--host 127.0.0.1]
53
56
  [--site dir] [--cache dir] [--graph saved.json]
54
57
  [--identity-header X-Forwarded-User] [--editor URL-template]
55
58
  [--agents read|advise|full] [--access-key-file file] [--public] [--jobs ${DEFAULT_JOBS}]
@@ -66,6 +69,18 @@ function usage(code = 2) {
66
69
  Reads what \`lake build\` left in <lean-project>/.lake. It never builds the project, and writes
67
70
  nothing into it.
68
71
 
72
+ A git URL where the project goes (https://github.com/owner/repo, ssh://…, git@host:owner/repo, or
73
+ file:///path for a repository on this machine): the server follows that branch of the repository
74
+ in a clone of its own in the cache, never a workspace, and builds each new commit itself —
75
+ \`lake exe cache get\` where the project is on Mathlib, then \`lake build\` — one at a time, the
76
+ newest commit pushed meanwhile the only one built after it; the board shows the last build
77
+ meanwhile. Git's own credentials (an SSH key, a deploy key, a credential helper): VisualProof keeps
78
+ none, and nothing waits on a prompt. Whoever can push to the branch runs code on this machine, as
79
+ the server's user (SECURITY.md): run it as a user of its own, and protect the branch.
80
+ --branch the branch followed (default: the remote's default branch); never a pull request's or
81
+ a fork's
82
+ --poll how often the remote is asked for the branch's head, in minutes (default ${DEFAULT_POLL_MINUTES})
83
+
69
84
  --root what to read: a library of the lakefile (the modules Lake builds of it) or a module;
70
85
  repeatable. By default the lakefile's default targets.
71
86
  --site the site's folder: the review's logs and what this running site accumulates — the
@@ -117,20 +132,24 @@ function usage(code = 2) {
117
132
  --jobs how many modules the usage pass elaborates at once (default ${DEFAULT_JOBS}; 0: no pass). The
118
133
  pass finds which notation each declaration is written in and the exact context it is
119
134
  written in (TCBSpec.md, decisions 2 and 4); its findings follow the extractor's graph
120
- as its next version. Each worker takes about 400 MB of its own and the import of a
121
- module is bound by memory bandwidth: past about a quarter of the cores it gains little.
135
+ as its next version. Each worker takes about 400 MB of its own, several GB on a large
136
+ project on Mathlib (serve and doctor say which --jobs the memory fits); with memory
137
+ short, fewer run at a time. The import of a module is bound by memory bandwidth: past
138
+ about a quarter of the cores it gains little.
122
139
 
123
140
  --access-key-file let in only those given the link http://<host>:<port>/k/<key>, the key being
124
141
  what the file holds (one is made there, readable by its owner alone, if there is none).
125
142
  The link leaves a cookie in the browser; agents send Authorization: Bearer <key>.
126
- Anything else is answered 404. Over plain HTTP the key crosses the network in the
127
- clear: it keeps out who was not given the link, not who can watch the traffic.
143
+ Anything else is answered 404, but a browser asking for a page, which is shown one
144
+ that says the site is private and nothing of it. Over plain HTTP the key crosses the
145
+ network in the clear: it keeps out who was not given the link, not who can watch the
146
+ traffic.
128
147
  Share also gives an AI agent a token of its own (vpa_…), which reaches /api/agent only,
129
148
  and a person a link of their own (/i/vpp_…), which lets them in and says who they are,
130
149
  and anyone a read-only link (/r/vpr_…), which lets them see and change nothing shared.
131
150
 
132
151
  --public with --access-key-file: whoever comes without the key or a link of their own is let
133
- in read-only, rather than answered 404: they see the project, its review and its
152
+ in read-only, rather than turned away: they see the project, its review and its
134
153
  history, change nothing that is shared, and are seen by nobody. For a project anyone
135
154
  may read (a showcase); never for a private one.
136
155
 
@@ -181,7 +200,7 @@ function parseArgs(argv) {
181
200
  console.error(`visualproof: VISUALPROOF_UPDATES=${fromEnv}: say auto, notify or off`);
182
201
  process.exit(2);
183
202
  }
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" };
203
+ 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", remote: null, branch: null, poll: null };
185
204
  for (let i = 0; i < rest.length; i++) {
186
205
  const a = rest[i];
187
206
  const val = () => rest[++i] ?? usage();
@@ -248,6 +267,18 @@ function parseArgs(argv) {
248
267
  usage();
249
268
  o.jobs = n;
250
269
  }
270
+ else if (a === "--branch" && cmd === "serve")
271
+ o.branch = val();
272
+ else if (a === "--poll" && cmd === "serve") {
273
+ const v = val();
274
+ const m = Number(v);
275
+ // said in a line, as --screen is
276
+ if (!/^\d+(\.\d+)?$/.test(v) || m < MIN_POLL_MINUTES || m > 1440) {
277
+ console.error(`visualproof: --poll ${v}: say how often the remote is asked, in minutes, from ${MIN_POLL_MINUTES} to 1440 (a day)`);
278
+ process.exit(2);
279
+ }
280
+ o.poll = m;
281
+ }
251
282
  else if (a === "--updates" && cmd === "serve") {
252
283
  const mode = val();
253
284
  if (!UPDATE_MODES.includes(mode))
@@ -277,6 +308,33 @@ function parseArgs(argv) {
277
308
  }
278
309
  if ((!o.path && cmd !== "doctor") || (cmd === "export" && !o.out) || (cmd === "artwork" && !o.recipe))
279
310
  usage();
311
+ // a git address where the project goes: the branch followed and built (DeploySpec decisions 1,
312
+ // 10 and 11), its address and branch checked before anything is run
313
+ if (o.path && isGitAddress(o.path)) {
314
+ if (cmd !== "serve") {
315
+ console.error(`visualproof ${cmd}: ${o.path.slice(0, 200)} is a git address: only serve follows a branch. Give the folder of its clone, which serve names as it starts (its "clone:" line).`);
316
+ process.exit(2);
317
+ }
318
+ const r = parseRemote(o.path);
319
+ if ("error" in r) {
320
+ console.error(`visualproof: ${r.error}`);
321
+ process.exit(2);
322
+ }
323
+ if (o.graph) {
324
+ console.error("visualproof: --graph does not go with a git address: the server builds what it shows");
325
+ process.exit(2);
326
+ }
327
+ const why = o.branch === null ? null : branchProblem(o.branch);
328
+ if (why) {
329
+ console.error(`visualproof: --branch ${why}`);
330
+ process.exit(2);
331
+ }
332
+ o.remote = r;
333
+ }
334
+ else if (o.branch !== null || o.poll !== null) {
335
+ console.error("visualproof: --branch and --poll go with a git address (serve https://github.com/owner/repo): a folder is read as its owner's builds leave it");
336
+ process.exit(2);
337
+ }
280
338
  // read-only for whoever comes, beside those the key or a link lets in: without a key, everyone is in
281
339
  // already; behind a sign-in, there is nobody else
282
340
  if (o.publicRead && (!o.accessKeyFile || o.identityHeader)) {
@@ -707,13 +765,17 @@ export class App {
707
765
  /** A personal link opened (CollabSpec decision 9): the browser is given the cookie that lets its
708
766
  * holder in, as who the link says, and goes on to the page — the token leaves the address, and a
709
767
  * place after it (a fragment) comes along. A revoked link says so; anything else is answered as
710
- * any address nobody was given. */
711
- enter(req, res, token) {
768
+ * any address nobody was given. On a server with a key, a browser is shown the page of a link
769
+ * that does not open the site instead (CollabSpec decision 7), which says neither which it was
770
+ * nor who revoked it — unless the key or a link of its own lets it in, and it may be told. */
771
+ enter(req, res, token, path) {
712
772
  const c = this.ids.header ? null : this.live.invites.check(token);
713
773
  if (c && "ok" in c) {
714
774
  res.writeHead(303, { location: "/", "set-cookie": this.live.invites.cookie(c.ok), "cache-control": "no-store", "referrer-policy": "no-referrer" });
715
775
  return void res.end();
716
776
  }
777
+ if (this.access && asksForPage(req, path) && (!c || !this.letIn(req)))
778
+ return sendAccessPage(res, "link", this.publicRead);
717
779
  if (c)
718
780
  return sendText(req, res, revokedWords(c.record, "personal") + "\n", "text/plain; charset=utf-8", 410);
719
781
  sendJson(res, { error: "not found" }, 404);
@@ -721,17 +783,25 @@ export class App {
721
783
  /** A read-only link opened (CollabSpec decision 11): the browser is given the cookie that lets
722
784
  * its holder read, and goes on to the page — the token leaves the address, and a place after it
723
785
  * (a fragment) comes along. A revoked link says so; anything else, and any link on a server
724
- * open to all or behind a sign-in, which have none, is answered as an address nobody was given. */
725
- enterReadOnly(req, res, token) {
786
+ * open to all or behind a sign-in, which have none, is answered as an address nobody was given;
787
+ * to a browser, on a server with a key, as a personal link's (enter). */
788
+ enterReadOnly(req, res, token, path) {
726
789
  const c = this.access && !this.ids.header ? this.live.readOnly.check(token) : null;
727
790
  if (c && "ok" in c) {
728
791
  res.writeHead(303, { location: "/", "set-cookie": this.live.readOnly.cookie(c.ok.id), "cache-control": "no-store", "referrer-policy": "no-referrer" });
729
792
  return void res.end();
730
793
  }
794
+ if (this.access && asksForPage(req, path) && (!c || !this.letIn(req)))
795
+ return sendAccessPage(res, "link", this.publicRead);
731
796
  if (c)
732
797
  return sendText(req, res, revokedWords(c.link, "read-only") + "\n", "text/plain; charset=utf-8", 410);
733
798
  sendJson(res, { error: "not found" }, 404);
734
799
  }
800
+ /** Whether the key's cookie or a personal link in force lets a request in: someone who sees the
801
+ * site's people, and may be told who revoked a link. */
802
+ letIn(req) {
803
+ return !!this.ids.person(req) || this.access?.admit(req, "/") === "in";
804
+ }
735
805
  /** Who the reader is (POST /api/identity {name}): the name someone invited by a personal link goes
736
806
  * by from now on — kept with their link, and said in their review log, so that what they wrote
737
807
  * shows under it — or, for a guest, whether a name is theirs to take: one a link's holder goes
@@ -767,8 +837,10 @@ export class App {
767
837
  }
768
838
  /** A request for a name this server without a key does not answer (CollabSpec decision 12): 403,
769
839
  * 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) {
840
+ * for whoever runs it behind a proxy that forwards the name people reach it by. A browser asking
841
+ * for a page is shown a page that says so (the owner's decision of 2026-10-18), which does not
842
+ * repeat the name: it is the requester's, any text at all. */
843
+ refuseHost(req, res, path) {
772
844
  const raw = req.headers.host ?? "";
773
845
  const name = /^\[/.test(raw) ? raw : raw.replace(/:\d{1,5}$/, "");
774
846
  const shown = /^[A-Za-z0-9._\-[\]:]{1,253}$/.test(name) ? name : null;
@@ -776,6 +848,8 @@ export class App {
776
848
  this.refusedHosts.add(shown.toLowerCase());
777
849
  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
850
  }
851
+ if (asksForPage(req, path))
852
+ return sendAccessPage(res, "host");
779
853
  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
854
  }
781
855
  /** The answer to a request that needs a name and has none: the page asks for one (`who`). Where
@@ -865,7 +939,7 @@ export class App {
865
939
  // rebinding) is refused whatever it asks — the page, its data, the event stream, the agents'
866
940
  // API, the MCP server's file
867
941
  if (this.hostNames && !hostAllowed(req.headers.host, this.hostNames))
868
- return this.refuseHost(req, res);
942
+ return this.refuseHost(req, res, path);
869
943
  // every route; whatever one throws, handle() answers
870
944
  {
871
945
  // an agent's own token (server/tokens.ts) lets it into its API alone: the page and the page's
@@ -888,11 +962,11 @@ export class App {
888
962
  // it says, and its cookie does from then on; a revoked one's cookie is forgotten, and lets
889
963
  // nobody in
890
964
  if (path.startsWith(INVITE_PATH))
891
- return this.enter(req, res, path.slice(INVITE_PATH.length));
965
+ return this.enter(req, res, path.slice(INVITE_PATH.length), path);
892
966
  // a read-only link (CollabSpec decision 11, server/readonly.ts): its cookie lets its holder
893
967
  // read, where nothing else lets them in
894
968
  if (path.startsWith(READONLY_PATH))
895
- return this.enterReadOnly(req, res, path.slice(READONLY_PATH.length));
969
+ return this.enterReadOnly(req, res, path.slice(READONLY_PATH.length), path);
896
970
  const link = this.ids.link(req);
897
971
  const cookies = link && "ended" in link ? [live.invites.forget()] : [];
898
972
  const ro = this.access && !this.ids.header ? live.readOnly.ofCookie(req.headers.cookie) : null;
@@ -905,20 +979,39 @@ export class App {
905
979
  const reader = ro && "ok" in ro ? ro.ok.id : this.publicRead && !this.ids.header ? "public" : null;
906
980
  // a page whose link was revoked, which nothing else lets in, is told so (CollabSpec
907
981
  // 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
982
+ // page, reloaded, says the link no longer opens the site (decision 7: to a browser, the
983
+ // page of a link that does not, which names nobody); the cookie is forgotten there, once
984
+ // it has been said
909
985
  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
986
  if (revoked) {
987
+ if (asksForPage(req, path)) {
988
+ res.setHeader("set-cookie", cookies);
989
+ return sendAccessPage(res, "link");
990
+ }
911
991
  if (path === "/" || path === "/index.html") {
912
992
  res.setHeader("set-cookie", cookies);
913
993
  return sendText(req, res, revoked + "\n", "text/plain; charset=utf-8", 410);
914
994
  }
915
995
  return sendJson(res, { error: revoked, revoked: true }, 410);
916
996
  }
997
+ // nothing lets them in: a browser asking for a page is told the site is private, or that
998
+ // its link (`/k/…`, a key replaced) does not open it (CollabSpec decision 7, the owner's
999
+ // decision of 2026-10-18); anything else is answered as if there were nothing here
917
1000
  if (reader === null || this.ids.kind(req) === "agent") {
918
1001
  if (cookies.length)
919
1002
  res.setHeader("set-cookie", cookies);
1003
+ if (asksForPage(req, path))
1004
+ return sendAccessPage(res, path.startsWith(KEY_PATH) ? "link" : "private");
920
1005
  return sendJson(res, { error: "not found" }, 404);
921
1006
  }
1007
+ // a link of the key's that is not the key, opened where the address, or a read-only link,
1008
+ // lets them read: the page of a link that does not open the site, which says whether the
1009
+ // address alone does (--public)
1010
+ if (path.startsWith(KEY_PATH) && asksForPage(req, path)) {
1011
+ if (cookies.length)
1012
+ res.setHeader("set-cookie", cookies);
1013
+ return sendAccessPage(res, "link", this.publicRead);
1014
+ }
922
1015
  this.readOnlyOf.set(req, reader);
923
1016
  // the page renews a read-only link's cookie, as a personal link's
924
1017
  if (reader !== "public" && (path === "/" || path === "/index.html"))
@@ -986,6 +1079,9 @@ export class App {
986
1079
  if (this.readOnlyOf.get(req) !== undefined) {
987
1080
  delete st.log;
988
1081
  st.review = { ...st.review, folder: undefined };
1082
+ // a branch's build log is not a read-only reader's (DeploySpec decision 11)
1083
+ if (st.deploy)
1084
+ st.deploy = slimDeploy(st.deploy);
989
1085
  }
990
1086
  return sendJson(res, st);
991
1087
  }
@@ -1034,7 +1130,7 @@ export class App {
1034
1130
  // name — a private reader's too (CollabSpec decision 8) — with the personal link of someone
1035
1131
  // invited by one, whose moves go to their link's log (decision 9); by an agent at level full
1036
1132
  const person = this.ids.person(req)?.id;
1037
- const a = historyAnswer(live, { method: req.method ?? "GET", path, query: url.searchParams, body, caller: { name: this.ids.who(req), kind: this.ids.kind(req), ...(person ? { person } : {}) } }, (q) => q.caller.kind === "agent" ? refusal(this.agents.level, "full") : null);
1133
+ const a = await historyAnswer(live, { method: req.method ?? "GET", path, query: url.searchParams, body, caller: { name: this.ids.who(req), kind: this.ids.kind(req), ...(person ? { person } : {}) } }, (q) => q.caller.kind === "agent" ? refusal(this.agents.level, "full") : null);
1038
1134
  // a snapshot's graph in parts, as the Time Machine takes it: compressed once, and tagged
1039
1135
  if (a.gz)
1040
1136
  return sendGz(req, res, a.gz.body, a.gz.type);
@@ -1057,9 +1153,9 @@ export class App {
1057
1153
  // an agent may use the page's own endpoints too: the same rules hold there (AgentSpec)
1058
1154
  const agent = this.ids.kind(req) === "agent";
1059
1155
  // the review (ReviewSpec.md): the whole of it, as a page takes it on, with the lines of its
1060
- // logs that cannot be read
1156
+ // logs that cannot be read; tagged with its revision, compressed once a revision
1061
1157
  if (path === "/api/review" && req.method === "GET")
1062
- return sendJsonText(res, live.review.wholeText());
1158
+ return sendReview(req, res, live.review);
1063
1159
  if (path === "/api/review" && req.method === "POST") {
1064
1160
  // an opinion, a target, a trusted prefix or a key declaration is recorded under the
1065
1161
  // reader's name (CollabSpec decision 3), not one in the body; what it is about is
@@ -1149,6 +1245,11 @@ export class App {
1149
1245
  // the project's git source and commit as git says them now, not as the build was read (an
1150
1246
  // artwork's foot, ExportSpec decision 19; the owner's decision of 2026-10-15)
1151
1247
  if (path === "/api/git") {
1248
+ // following a branch, the commit of the build on show, on that branch: what is checked out
1249
+ // meanwhile is the build going on (DeploySpec decision 12)
1250
+ const shown = live.shownCommit();
1251
+ if (shown !== undefined)
1252
+ return sendJson(res, { git: shown && { source: live.deploy.status(false).source, commit: shown.hash, branch: shown.branch } });
1152
1253
  if (!this.gitRead || Date.now() - this.gitRead.at >= this.gitFreshMs)
1153
1254
  this.gitRead = { git: gitSource(live.project.root), at: Date.now() };
1154
1255
  return sendJson(res, { git: this.gitRead.git });
@@ -1388,7 +1489,7 @@ export function sendJson(res, body, code = 200, headers = {}) {
1388
1489
  return;
1389
1490
  sendJsonText(res, JSON.stringify(body), code, headers);
1390
1491
  }
1391
- /** The same, of an answer written as JSON already (the whole review, server/review.ts). */
1492
+ /** The same, of an answer written as JSON already. */
1392
1493
  function sendJsonText(res, text, code = 200, headers = {}) {
1393
1494
  if (res.headersSent || res.writableEnded || refused.has(res))
1394
1495
  return;
@@ -1403,6 +1504,40 @@ function sendJsonText(res, text, code = 200, headers = {}) {
1403
1504
  res.writeHead(code, { "content-type": "application/json", "cache-control": "no-store", ...headers });
1404
1505
  res.end(text);
1405
1506
  }
1507
+ /** the whole review compressed, under the tag of the revision it is of, per review */
1508
+ const reviewGz = new WeakMap();
1509
+ /** The whole review (GET /api/review), tagged with its run and its revision (server/review.ts): a
1510
+ * page that has it is answered 304, which a reload is, and one whose review fell behind is sent it
1511
+ * compressed — once a revision, off the server's thread when it is large, the same bytes for every
1512
+ * page (a review of 5,000 opinions on Sphere-Packing-Lean: 8.5 MB of JSON, 1.1 MB sent). */
1513
+ function sendReview(req, res, review) {
1514
+ if (res.headersSent || res.writableEnded || refused.has(res))
1515
+ return;
1516
+ const tag = review.tag();
1517
+ const head = { "content-type": "application/json", "cache-control": "private, no-cache", etag: tag, vary: "accept-encoding" };
1518
+ if (fresh(req, tag)) {
1519
+ res.writeHead(304, head);
1520
+ return void res.end();
1521
+ }
1522
+ const text = review.wholeText();
1523
+ if (text.length <= JSON_GZIP_FROM || !acceptsGzip(req)) {
1524
+ res.writeHead(200, head);
1525
+ return void res.end(text);
1526
+ }
1527
+ let c = reviewGz.get(review);
1528
+ if (c?.tag !== tag) {
1529
+ const gz = text.length < JSON_GZIP_AWAY_FROM ? Promise.resolve(gzipSync(text)) : new Promise((ok, fail) => gzip(text, (e, b) => (e ? fail(e) : ok(b))));
1530
+ reviewGz.set(review, (c = { tag, gz }));
1531
+ // a compression that failed is tried again by the next page that asks
1532
+ gz.catch(() => reviewGz.get(review)?.gz === gz && reviewGz.delete(review));
1533
+ }
1534
+ c.gz.then((gz) => {
1535
+ if (res.headersSent || res.destroyed)
1536
+ return;
1537
+ res.writeHead(200, { ...head, "content-encoding": "gzip" });
1538
+ res.end(gz);
1539
+ }, (e) => res.destroy(e));
1540
+ }
1406
1541
  /** Send a text that is not JSON (the agents' documentation, the MCP server's file), compressed
1407
1542
  * where the client takes it. */
1408
1543
  function sendText(req, res, text, type, code = 200) {
@@ -1449,18 +1584,26 @@ const JSON_GZIP_FROM = 2048;
1449
1584
  export const JSON_GZIP_AWAY_FROM = 1 << 20;
1450
1585
  /** the graph's parts as sent, each tagged once (a graph's buffers live as long as it does) */
1451
1586
  const gzTags = new WeakMap();
1452
- /** A part of the graph, gzipped already: tagged, so that a page that has it gets 304. */
1587
+ /** A part of the graph, gzipped already: tagged, so that a page that has it gets 304; a client that
1588
+ * does not take gzip (an agent's) is sent it as it is, made so off the server's thread. */
1453
1589
  function sendGz(req, res, gz, type) {
1454
1590
  if (refused.has(res))
1455
1591
  return;
1456
1592
  let etag = gzTags.get(gz);
1457
1593
  if (!etag)
1458
1594
  gzTags.set(gz, (etag = tagOf(gz)));
1459
- const head = { "content-type": type, "cache-control": "private, no-cache", etag };
1595
+ const head = { "content-type": type, "cache-control": "private, no-cache", etag, vary: "accept-encoding" };
1460
1596
  if (fresh(req, etag)) {
1461
1597
  res.writeHead(304, head);
1462
1598
  return void res.end();
1463
1599
  }
1600
+ if (!acceptsGzip(req))
1601
+ return void gunzip(gz, (e, plain) => {
1602
+ if (e)
1603
+ return res.destroy(e);
1604
+ res.writeHead(200, head);
1605
+ res.end(plain);
1606
+ });
1464
1607
  res.writeHead(200, { ...head, "content-encoding": "gzip" });
1465
1608
  res.end(gz);
1466
1609
  }
@@ -1489,7 +1632,7 @@ async function main() {
1489
1632
  // an installed package's server runs under the supervisor, which stays while it restarts into
1490
1633
  // another version (UpdateSpec decision 5); `--updates off`, and a checkout, run it as they are
1491
1634
  if (o.cmd === "serve" && FROM_PACKAGE && o.updates !== "off" && process.env.VISUALPROOF_SUPERVISED !== "1") {
1492
- const site = projectDirs(resolve(o.path), o.site, o.cache).site;
1635
+ const site = o.remote ? deploySiteOf(o) : projectDirs(resolve(o.path), o.site, o.cache).site;
1493
1636
  process.exit(await supervise({ installedRoot: PACKAGE_ROOT, cacheRoot: o.cache ? resolve(o.cache) : cacheHome(), argv: process.argv.slice(2), site, mode: o.updates }));
1494
1637
  }
1495
1638
  // a stop asked before the server can stop gracefully (it reads the project, takes the site) ends
@@ -1506,13 +1649,18 @@ async function main() {
1506
1649
  // whatever fails ends it, with status 1)
1507
1650
  if (o.cmd !== "artwork")
1508
1651
  process.on("unhandledRejection", (e) => console.error("unhandled:", e instanceof Error ? e.message : e));
1652
+ // following a branch (DeploySpec): its clone in the cache, made at the first start, is the project
1653
+ const deploy = o.remote ? await openDeployment(o) : null;
1654
+ if (o.remote && !deploy)
1655
+ return;
1509
1656
  let project;
1510
1657
  try {
1511
- project = openProject(o.path, o.roots, o.graph === null);
1658
+ // a branch's clone is read before its build: the server builds it
1659
+ project = openProject(deploy ? deploy.folder.checkout : o.path, o.roots, !deploy && o.graph === null);
1512
1660
  }
1513
1661
  catch (e) {
1514
1662
  if (e instanceof ProjectError) {
1515
- console.error(`visualproof: ${e.message}`);
1663
+ console.error(`visualproof: ${deploy ? `${deploy.branch} of ${deploy.remote.source ?? deploy.remote.url}, at ${shortCommit(deploy.head)}: ` : ""}${e.message}`);
1516
1664
  return endNotTheCode(1);
1517
1665
  }
1518
1666
  throw e;
@@ -1520,7 +1668,8 @@ async function main() {
1520
1668
  // a picture: the site read, never made, locked or written (ExportSpec decision 21)
1521
1669
  if (o.cmd === "artwork")
1522
1670
  return artworkCommand(project, o);
1523
- const dirs = projectDirs(project.root, o.site, o.cache);
1671
+ // a branch's site is the address's and the branch's, wherever the cache is
1672
+ const dirs = { ...projectDirs(project.root, o.site, o.cache), ...(deploy && o.site === null ? { site: deploySite(deploy.remote, deploy.branch) } : {}) };
1524
1673
  openSite(dirs.site);
1525
1674
  mkdirSync(dirs.cache, { recursive: true });
1526
1675
  // one server at a time writes a site; an export only reads it
@@ -1597,6 +1746,29 @@ async function main() {
1597
1746
  live.invites.close();
1598
1747
  live.readOnly.close();
1599
1748
  });
1749
+ // the branch followed: asked every --poll minutes, each new head built in turn (DeploySpec)
1750
+ const deployment = deploy
1751
+ ? new Deployment({
1752
+ remote: deploy.remote,
1753
+ branch: deploy.branch,
1754
+ folder: deploy.folder,
1755
+ project,
1756
+ roots: o.roots,
1757
+ everyMs: (o.poll ?? DEFAULT_POLL_MINUTES) * 60_000,
1758
+ head: deploy.head,
1759
+ hooks: {
1760
+ reading: () => live.reading,
1761
+ // the usage pass of the build before is cut short: what it finished is kept
1762
+ starting: () => live.usage.cancel(),
1763
+ toolchainChanged: () => hover.toolchainChanged(),
1764
+ // the pages are told at the live loop's next look, within a second
1765
+ changed: () => live.lookSoon(),
1766
+ log: (l) => live.say(l),
1767
+ ended: () => live.reread(),
1768
+ },
1769
+ })
1770
+ : null;
1771
+ live.deploy = deployment;
1600
1772
  const exporter = new Exporter(dirs.cache);
1601
1773
  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);
1602
1774
  const server = createServer((req, res) => app.handle(req, res));
@@ -1620,6 +1792,8 @@ async function main() {
1620
1792
  stopping = true;
1621
1793
  exitStatus = status;
1622
1794
  updates.stop();
1795
+ // a build going on is stopped with the programs, and done again at the next start
1796
+ deployment?.stop();
1623
1797
  server.close();
1624
1798
  live.end(why);
1625
1799
  hover.stop();
@@ -1677,13 +1851,65 @@ async function main() {
1677
1851
  console.log(` editor: ${o.editor ?? "no link unless a reader sets one"}`);
1678
1852
  console.log(` agents: ${o.agents} — their API is http://${o.host}:${o.port}/api/agent`);
1679
1853
  console.log(` usage pass: ${o.graph ? "none for a saved graph" : o.jobs > 0 ? `${o.jobs} module(s) at a time` : "off (--jobs 0)"}`);
1854
+ // what the pass's workers may hold on a project on Mathlib, against what the machine has
1855
+ const memory = o.graph || o.jobs <= 0 ? null : jobsAdvice(o.jobs, onMathlib(project.root));
1856
+ if (memory)
1857
+ console.log(` memory: ${inGB(freemem())} available; on Mathlib the pass's workers may hold up to ${inGB(WORKER_PEAK)} each, ${inGB(memory.need)} at ${o.jobs}: ` +
1858
+ `--jobs ${memory.fits} if memory runs short (it starts fewer while less than ${inGB(memoryFloor())} is available)`);
1680
1859
  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)`}`);
1681
1860
  console.log(` version: ${version}; updates ${updatesInWords(o.updates, version, supervised)}`);
1861
+ if (deployment) {
1862
+ const d = deployment.status(true);
1863
+ console.log(` following: ${d.branch} of ${d.source ?? deployment.remote.url}, asked every ${o.poll ?? DEFAULT_POLL_MINUTES} min`);
1864
+ console.log(` clone: ${deployment.folder.checkout}`);
1865
+ console.log(` builds: ${d.built ? `the tree holds the build of ${shortCommit(d.built)}${deployment.holds() ? ", cut short: it is done again" : ""}` : `none yet: the first, of ${shortCommit(deploy.head)}, starts now, and the page says so until it is done`}; their logs in ${deployment.folder.logs}`);
1866
+ }
1682
1867
  recordVersion(live, version, supervised);
1683
1868
  process.send?.({ vp: "listening", version });
1684
1869
  updates.start();
1685
1870
  });
1686
1871
  live.start();
1872
+ deployment?.start();
1873
+ }
1874
+ /** The site a branch's server keeps, as the supervisor finds it (UpdateSpec decision 7): `--site`,
1875
+ * else the address's and the branch's — the default branch as it was last asked, without the network;
1876
+ * null at a first start without --branch, which the server alone resolves. */
1877
+ function deploySiteOf(o) {
1878
+ if (o.site !== null)
1879
+ return resolve(o.site);
1880
+ const branch = o.branch ?? recordedDefault(o.cache ? resolve(o.cache) : cacheHome(), o.remote.url);
1881
+ return branch ? deploySite(o.remote, branch) : null;
1882
+ }
1883
+ /** A branch's deployment as the server starts (DeploySpec decisions 1–2): the branch (the remote's
1884
+ * default where none is given), its folder in the cache taken for this server, its clone made at
1885
+ * the first start. Null where it cannot be had: said, and the server ends, which is not its code. */
1886
+ async function openDeployment(o) {
1887
+ const remote = o.remote;
1888
+ const cacheRoot = o.cache ? resolve(o.cache) : cacheHome();
1889
+ const branch = await branchToFollow(remote, o.branch, cacheRoot);
1890
+ if (typeof branch !== "string") {
1891
+ console.error(`visualproof: ${remote.source ?? remote.url}: ${branch.error}`);
1892
+ endNotTheCode(1);
1893
+ return null;
1894
+ }
1895
+ const folder = deployFolder(cacheRoot, remote, branch);
1896
+ try {
1897
+ process.on("exit", lockDeployment(folder, remote, branch));
1898
+ }
1899
+ catch (e) {
1900
+ if (!(e instanceof DeployBusy))
1901
+ throw e;
1902
+ console.error(`visualproof: ${e.message}`);
1903
+ endNotTheCode(1);
1904
+ return null;
1905
+ }
1906
+ const c = await ensureClone(remote, branch, folder, (l) => console.log(l));
1907
+ if ("error" in c) {
1908
+ console.error(`visualproof: ${c.error}`);
1909
+ endNotTheCode(1);
1910
+ return null;
1911
+ }
1912
+ return { remote, branch, folder, head: c.commit };
1687
1913
  }
1688
1914
  /** What the server does with its releases, for its first lines (UpdateSpec decisions 2–4, 13). */
1689
1915
  function updatesInWords(mode, version, supervised) {
@@ -20,7 +20,8 @@
20
20
  //
21
21
  // What changed is told as a change (`ReviewChange`): the events that came after everything replayed
22
22
  // so far, which a page applies to its own review — or, when an event came in between (older events
23
- // restored from a backup, a rewrite), the whole review again.
23
+ // restored from a backup, a rewrite), none: the page reads the whole review again, which its tag
24
+ // (the run and the revision) keeps from being sent to a page that has it.
24
25
  import { createHash, randomBytes } from "node:crypto";
25
26
  import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, statSync, writeFileSync, writeSync } from "node:fs";
26
27
  import { isAbsolute, join, resolve } from "node:path";
@@ -70,16 +71,29 @@ export class ReviewStore {
70
71
  whole() {
71
72
  return { epoch: this.epoch, rev: this.rev, data: this.data, problems: this.problems, error: this.error };
72
73
  }
73
- /** the whole review as JSON, and the revision it is of */
74
+ /** the review's data as JSON, and the whole review, each with the revision it is of */
75
+ dataJson = null;
74
76
  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). */
77
+ /** The review's data as JSON, written once a revision. */
78
+ dataText() {
79
+ if (this.dataJson?.rev !== this.rev)
80
+ this.dataJson = { rev: this.rev, text: JSON.stringify(this.data) };
81
+ return this.dataJson.text;
82
+ }
83
+ /** The whole review as JSON (`whole()`), written once a revision: every page that loads asks for
84
+ * it, and every page whose review fell behind (GET /api/review), and with thousands of opinions it
85
+ * is megabytes (7.6 MB for 5,000 on Sphere-Packing-Lean: 50 ms each time, a hundred pages opening
86
+ * held the server 5 s). */
78
87
  wholeText() {
79
88
  if (this.wholeJson?.rev !== this.rev)
80
- this.wholeJson = { rev: this.rev, text: JSON.stringify(this.whole()) };
89
+ this.wholeJson = { rev: this.rev, text: `{"epoch":${JSON.stringify(this.epoch)},"rev":${this.rev},"data":${this.dataText()},"problems":${JSON.stringify(this.problems)},"error":${JSON.stringify(this.error)}}` };
81
90
  return this.wholeJson.text;
82
91
  }
92
+ /** The tag of the whole review as it is now (GET /api/review's ETag): its run and its revision,
93
+ * which every change moves. */
94
+ tag() {
95
+ return `"r${this.epoch}.${this.rev}"`;
96
+ }
83
97
  /** The file a writer's events go to (shared/review.ts, logFileName). */
84
98
  fileOf(w) {
85
99
  return w.server ? SERVER_LOG : logFileName(w.name, w.agent, w.person ?? null);
@@ -197,13 +211,17 @@ export class ReviewStore {
197
211
  /** Replay what was read, and say what changed: the new events alone where they all come after
198
212
  * what was replayed before, else the whole review. A log read whole again has events of its own
199
213
  * (new objects), and one gone has none: either way what was replayed is no longer where it was,
200
- * and the review is replayed whole. */
214
+ * and the review is replayed whole. A replay whole that comes to the review it replaced (a new
215
+ * graph's reading that binds every opinion as before, a log rewritten as it was) is told as a
216
+ * change of no events: no page reads the whole review again for it. */
201
217
  commit() {
202
218
  const logs = [...this.files.values()].map((f) => ({ file: f.file, events: f.tail?.event ? [...f.events, f.tail.event] : f.events }));
203
219
  const next = merged(logs).map((x) => this.readAs(x.e));
204
220
  const before = this.order;
205
221
  const appended = !this.again && next.length >= before.length && before.every((e, k) => next[k] === e);
206
222
  this.again = false;
223
+ // what the review was, where it is replayed whole: compared with what the replay makes
224
+ const was = appended ? null : this.dataText();
207
225
  const base = this.rev;
208
226
  this.rev++;
209
227
  this.order = next;
@@ -217,6 +235,8 @@ export class ReviewStore {
217
235
  this.data = emptyReview();
218
236
  for (const e of next)
219
237
  applyEvent(this.data, e);
238
+ if (this.dataText() === was)
239
+ events = [];
220
240
  }
221
241
  const c = { epoch: this.epoch, base, rev: this.rev, events, problems: this.problems };
222
242
  this.onChange?.(c);