visualproof 0.4.3 → 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 (82) hide show
  1. package/README.md +16 -7
  2. package/dist/assets/latex/visualproof.sty +19 -9
  3. package/dist/assets/tips/tour-agent.webp +0 -0
  4. package/dist/assets/tips/tour-arrange.webp +0 -0
  5. package/dist/assets/tips/tour-artwork.webp +0 -0
  6. package/dist/assets/tips/tour-checks.webp +0 -0
  7. package/dist/assets/tips/tour-keys.webp +0 -0
  8. package/dist/assets/tips/tour-library.webp +0 -0
  9. package/dist/assets/tips/tour-project.webp +0 -0
  10. package/dist/assets/tips/tour-review.webp +0 -0
  11. package/dist/assets/tips/tour-share.webp +0 -0
  12. package/dist/assets/tips/tour-source.webp +0 -0
  13. package/dist/assets/tips/tour-target.webp +0 -0
  14. package/dist/assets/tips/tour-view.webp +0 -0
  15. package/dist/assets/tips/tour.json +117 -112
  16. package/dist/assets/tips/tour.webp +0 -0
  17. package/dist/mcp/visualproof-mcp.mjs +7 -3
  18. package/dist/server/access.js +37 -1
  19. package/dist/server/accesspage.js +130 -0
  20. package/dist/server/agent.js +11 -5
  21. package/dist/server/agentlibrary.js +1 -1
  22. package/dist/server/agentviews.js +1 -1
  23. package/dist/server/artcmd.js +1 -1
  24. package/dist/server/askfollow.js +1 -4
  25. package/dist/server/buildwatch.js +133 -0
  26. package/dist/server/deploy.js +844 -0
  27. package/dist/server/doctor.js +15 -3
  28. package/dist/server/events.js +20 -5
  29. package/dist/server/export.js +17 -4
  30. package/dist/server/fetchurl.js +30 -13
  31. package/dist/server/film.js +1 -1
  32. package/dist/server/history.js +3 -1
  33. package/dist/server/historyapi.js +100 -50
  34. package/dist/server/historythread.js +206 -0
  35. package/dist/server/hover.js +8 -0
  36. package/dist/server/identity.js +25 -1
  37. package/dist/server/library.js +10 -3
  38. package/dist/server/live.js +206 -28
  39. package/dist/server/lostfound.js +3 -2
  40. package/dist/server/main.js +361 -50
  41. package/dist/server/poppler.js +48 -10
  42. package/dist/server/renames.js +10 -5
  43. package/dist/server/review.js +32 -2
  44. package/dist/server/reviewapi.js +1 -1
  45. package/dist/server/status.js +144 -50
  46. package/dist/server/talk.js +7 -2
  47. package/dist/server/usage.js +51 -0
  48. package/dist/server/views.js +21 -7
  49. package/dist/shared/board.js +2 -1
  50. package/dist/shared/collab.js +14 -8
  51. package/dist/shared/deploy.js +35 -0
  52. package/dist/shared/doc/latex.js +5 -3
  53. package/dist/shared/doc/outline.js +6 -3
  54. package/dist/shared/layout.js +297 -70
  55. package/dist/shared/libm.js +366 -0
  56. package/dist/shared/library.js +11 -6
  57. package/dist/shared/palette.js +1 -1
  58. package/dist/shared/review.js +7 -5
  59. package/dist/shared/search.js +7 -3
  60. package/dist/shared/separate.js +556 -32
  61. package/dist/shared/views.js +8 -1
  62. package/dist/web/bundle/app.js +3595 -1520
  63. package/dist/web/bundle/edges.js +44 -16
  64. package/dist/web/bundle/layout.js +1026 -80
  65. package/dist/web/index.html +2 -2
  66. package/dist/web/src/artwork.js +25 -4
  67. package/dist/web/src/axiomring.js +42 -0
  68. package/dist/web/src/bundles.js +52 -16
  69. package/dist/web/src/canvas.js +109 -54
  70. package/dist/web/src/overview.js +4 -1
  71. package/dist/web/src/past.js +28 -0
  72. package/dist/web/src/scene.js +13 -12
  73. package/dist/web/src/selection.js +47 -4
  74. package/dist/web/src/status.js +32 -2
  75. package/dist/web/src/theme.js +108 -0
  76. package/dist/web/style.css +181 -45
  77. package/docs/agent-api/following.md +1 -1
  78. package/docs/agent-api/history.md +3 -0
  79. package/docs/agent-api/library.md +1 -1
  80. package/docs/agent-api/requests.md +4 -1
  81. package/docs/agent-api/views.md +1 -1
  82. package/package.json +1 -1
@@ -0,0 +1,206 @@
1
+ // What the Time Machine and the agents read of the history (server/historyapi.ts) — a snapshot's
2
+ // graph in the page's two parts, its whole graph, what changed between two snapshots — made off the
3
+ // server's thread. On NavierStokesAndEuler (52,648 declarations, 5 M edges) the Time Machine's first
4
+ // look at a build held every request for 4 s: the snapshot read from its files (1 s), its two parts
5
+ // made and compressed (1.6 s), what changed since (0.8 s). A worker reads the history from the site's
6
+ // files as the server's own does, and only reads them; it keeps what it read while the history's
7
+ // index stays as it was (every snapshot written, read again or given its texts adds a line to it),
8
+ // answers one job at a time, and goes after a while without one, its memory with it. Should no worker
9
+ // start, or one fail, the job is done here: slower to answer others meanwhile, never wrong. This
10
+ // module is its own worker, as server/layoutthread.ts is.
11
+ import { statSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
14
+ import { gzipSync } from "node:zlib";
15
+ import { packEdges } from "../shared/edgecodec.js";
16
+ import { identityIn } from "../shared/moves.js";
17
+ import { History, diffGraphs, pageGraph } from "./history.js";
18
+ import { ownName } from "./versions.js";
19
+ /** The modules whose text snapshot `id` keeps, each with how it was matched to its build
20
+ * (server/sourcestore.ts): what the page says of a declaration's source then before it asks. */
21
+ function keptTexts(h, id) {
22
+ const out = {};
23
+ for (const [m, r] of Object.entries(h.sourcesOf(id) ?? {}))
24
+ if (r.blob && r.check)
25
+ out[m] = r.check;
26
+ return out;
27
+ }
28
+ /** Do a job on history `h`, under `identity` (the renames in force), here. */
29
+ export function runJob(h, identity, job) {
30
+ if (job.kind === "diff") {
31
+ const [a, b] = [h.graph(job.from), h.graph(job.to)];
32
+ if (!a || !b)
33
+ throw new Error(`There is no snapshot ${a ? job.to : job.from}.`);
34
+ const diff = { from: job.from, to: job.to, ...diffGraphs(a, b, identity, new Map(job.renames), job.most) };
35
+ return { kind: "diff", infos: [a.info, b.info], diff };
36
+ }
37
+ const g = h.graph(job.id);
38
+ if (!g)
39
+ throw new Error(`There is no snapshot ${job.id}.`);
40
+ if (job.kind === "graph")
41
+ return { kind: "graph", info: g.info, gz: gzipSync(JSON.stringify(pageGraph(g, identity))) };
42
+ const { edges: _flat, ...rest } = pageGraph({ ...g, edges: [] }, identity);
43
+ const E = new Int32Array(3 * g.edges.length);
44
+ for (let k = 0; k < g.edges.length; k++) {
45
+ const [s, d, m] = g.edges[k];
46
+ E[3 * k] = s;
47
+ E[3 * k + 1] = d;
48
+ E[3 * k + 2] = m;
49
+ }
50
+ const nodes = gzipSync(JSON.stringify({ ...rest, edgeCount: g.edges.length, sources: keptTexts(h, job.id) }));
51
+ return { kind: "parts", info: g.info, parts: { nodes, edges: gzipSync(packEdges(E, g.nodes.length, 2), { level: 9 }) } };
52
+ }
53
+ /** The identity of a unit under renames `renamed` (shared/moves.ts), as the server's own is
54
+ * (server/live.ts, identity): to → from, made again without a prototype. */
55
+ export function identityOf(renamed) {
56
+ if (!renamed)
57
+ return ownName;
58
+ const r = Object.create(null);
59
+ for (const [to, from] of renamed)
60
+ r[to] = from;
61
+ return (u) => identityIn(r, u);
62
+ }
63
+ /** how long the worker stays without a job: the Time Machine steps from build to build, each a job
64
+ * or three */
65
+ const IDLE_MS = 60_000;
66
+ let worker = null;
67
+ let idleTimer = null;
68
+ let asked = 0;
69
+ /** how many jobs were done in the worker and how many here, since the start (the tests read it) */
70
+ export const jobsDone = { worker: 0, here: 0 };
71
+ /** the jobs out, by number: each with what does it here, should the worker fail */
72
+ const out = new Map();
73
+ /** Every job out done here instead, and the worker forgotten: it stopped, or failed. */
74
+ function lost(w) {
75
+ if (worker !== w)
76
+ return;
77
+ worker = null;
78
+ const jobs = [...out.values()];
79
+ out.clear();
80
+ for (const j of jobs) {
81
+ try {
82
+ j.ok(j.here());
83
+ }
84
+ catch (e) {
85
+ j.fail(e);
86
+ }
87
+ }
88
+ }
89
+ function spawn() {
90
+ if (worker)
91
+ return worker;
92
+ let w;
93
+ try {
94
+ w = new Worker(new URL(import.meta.url), { workerData: { vpHistory: true } });
95
+ }
96
+ catch {
97
+ return null;
98
+ }
99
+ w.on("message", (a) => {
100
+ const j = out.get(a.n);
101
+ out.delete(a.n);
102
+ if (j) {
103
+ if ("done" in a) {
104
+ jobsDone.worker++;
105
+ j.ok(revive(a.done));
106
+ }
107
+ else {
108
+ // what the worker could not do is no worker's failure: tried here, it says why
109
+ try {
110
+ j.ok(j.here());
111
+ }
112
+ catch (e) {
113
+ j.fail(e);
114
+ }
115
+ }
116
+ }
117
+ if (!out.size)
118
+ rest(w);
119
+ });
120
+ w.on("error", () => lost(w));
121
+ w.on("exit", () => lost(w));
122
+ // held only while a job is out (offThread): an idle worker keeps no process running. After its
123
+ // listeners, which hold it again as they are added
124
+ w.unref();
125
+ worker = w;
126
+ return w;
127
+ }
128
+ /** The worker holds nothing any more, and goes after IDLE_MS without a job. */
129
+ function rest(w) {
130
+ w.unref();
131
+ if (idleTimer)
132
+ clearTimeout(idleTimer);
133
+ idleTimer = setTimeout(() => {
134
+ idleTimer = null;
135
+ if (out.size || !worker)
136
+ return;
137
+ const w = worker;
138
+ worker = null;
139
+ void w.terminate();
140
+ }, IDLE_MS);
141
+ idleTimer.unref();
142
+ }
143
+ /** Buffers come back from the worker as bytes: Buffers again. */
144
+ function revive(d) {
145
+ const buf = (u) => Buffer.from(u.buffer, u.byteOffset, u.byteLength);
146
+ if (d.kind === "parts")
147
+ return { ...d, parts: { nodes: buf(d.parts.nodes), edges: buf(d.parts.edges) } };
148
+ if (d.kind === "graph")
149
+ return { ...d, gz: buf(d.gz) };
150
+ return d;
151
+ }
152
+ /** Do a job on the history of site `site` in the worker, under the renames in force `renamed`;
153
+ * `here` does it on this thread, where no worker can. */
154
+ export function offThread(site, renamed, job, doHere) {
155
+ const here = () => (jobsDone.here++, doHere());
156
+ return new Promise((ok, fail) => {
157
+ const w = spawn();
158
+ if (!w) {
159
+ try {
160
+ return ok(here());
161
+ }
162
+ catch (e) {
163
+ return fail(e);
164
+ }
165
+ }
166
+ if (idleTimer)
167
+ clearTimeout(idleTimer);
168
+ idleTimer = null;
169
+ const n = ++asked;
170
+ out.set(n, { ok, fail, here });
171
+ w.ref();
172
+ w.postMessage({ n, site, renamed, job });
173
+ });
174
+ }
175
+ // in a worker: the jobs asked, one at a time, on the history as its files are now
176
+ if (!isMainThread && workerData?.vpHistory) {
177
+ let held = null;
178
+ /** the history of `site`, read again when its index changed since the last job */
179
+ const historyOf = (site) => {
180
+ let stamp = "";
181
+ try {
182
+ const st = statSync(join(site, "history", "index.jsonl"));
183
+ stamp = `${st.size}:${st.mtimeMs}:${st.ino}`;
184
+ }
185
+ catch {
186
+ // no history yet: nothing to keep
187
+ }
188
+ if (!held || held.site !== site || held.stamp !== stamp || !stamp)
189
+ held = { site, stamp, h: new History(site) };
190
+ return held.h;
191
+ };
192
+ parentPort.on("message", (a) => {
193
+ let answer;
194
+ try {
195
+ answer = { n: a.n, done: runJob(historyOf(a.site), identityOf(a.renamed), a.job) };
196
+ }
197
+ catch (e) {
198
+ answer = { n: a.n, error: e instanceof Error ? e.message : String(e) };
199
+ }
200
+ const d = "done" in answer ? answer.done : null;
201
+ const bytes = d?.kind === "parts" ? [d.parts.nodes, d.parts.edges] : d?.kind === "graph" ? [d.gz] : [];
202
+ // the compressed bytes are handed over, not copied, where they are a buffer of their own
203
+ const transfer = bytes.filter((b) => b.byteOffset === 0 && b.byteLength === b.buffer.byteLength).map((b) => b.buffer);
204
+ parentPort.postMessage(answer, transfer);
205
+ });
206
+ }
@@ -591,6 +591,14 @@ export class LeanHover {
591
591
  if (p)
592
592
  end(p);
593
593
  }
594
+ /** The project's toolchain changed (a new commit of the branch the server follows:
595
+ * server/deploy.ts): Lean stops, and starts at the next question with the new toolchain's `lean`
596
+ * and LEAN_PATH. */
597
+ toolchainChanged() {
598
+ this.stop();
599
+ this.env = null;
600
+ this.failure = null;
601
+ }
594
602
  }
595
603
  /** Null after `ms`: what a question for Lean is raced against. */
596
604
  function after(ms) {
@@ -71,7 +71,9 @@ export class Identities {
71
71
  let s = (Array.isArray(raw) ? raw[0] : raw)?.trim();
72
72
  if (!s)
73
73
  return null;
74
- if (!this.header) {
74
+ if (this.header)
75
+ s = headerText(s);
76
+ else {
75
77
  try {
76
78
  s = decodeURIComponent(s);
77
79
  }
@@ -102,6 +104,28 @@ export class Identities {
102
104
  return validSession(s) ? s : "";
103
105
  }
104
106
  }
107
+ /** A proxy's header as the text it carries. Node reads a header's bytes one character each
108
+ * (Latin-1), so a name a proxy writes in UTF-8 — José, 许英特 — is read again as UTF-8 where its
109
+ * bytes are UTF-8 (a Latin-1 "José" is not, and stays); and a value that is MIME encoded-words
110
+ * (RFC 2047, `=?utf-8?q?Jos=C3=A9?=`), as Tailscale writes a name beyond ASCII, is decoded. */
111
+ export function headerText(s) {
112
+ let bytes = null;
113
+ if (/^(?:=\?utf-8\?[bq]\?[^?\s]*\?=\s*)+$/i.test(s)) {
114
+ // the words one after the other, the spaces between them nothing (RFC 2047, section 6.2)
115
+ const word = ([, enc, text]) => enc.toLowerCase() === "b" ? Buffer.from(text, "base64") : Buffer.from(text.replace(/_/g, " ").replace(/=([0-9a-f]{2})/gi, (_, h) => String.fromCharCode(parseInt(h, 16))), "latin1");
116
+ bytes = Buffer.concat([...s.matchAll(/=\?utf-8\?([bq])\?([^?\s]*)\?=/gi)].map(word));
117
+ }
118
+ else if (/[\u0080-\u00ff]/.test(s) && !/[^\u0000-\u00ff]/.test(s))
119
+ bytes = Buffer.from(s, "latin1");
120
+ if (!bytes)
121
+ return s;
122
+ try {
123
+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
124
+ }
125
+ catch {
126
+ return s;
127
+ }
128
+ }
105
129
  /** The name a change to the layout is recorded under (CollabSpec decision 8): the caller's, or none
106
130
  * for a private reader, who shows as "someone" — on the boards, in Activity, among pin authors and
107
131
  * in the agents' answers. This is where a private reader's anonymity lives: whatever arranges a
@@ -123,9 +123,12 @@ function readPlace(b, type, pages) {
123
123
  place.page = b.page;
124
124
  }
125
125
  if (b.region !== undefined && b.region !== null) {
126
- if (!validRegion(b.region))
127
- return "region is [left, top, right, bottom], fractions of the page from its top left (0 to 1), left < right and top < bottom.";
128
- place.region = roundRegion(b.region);
126
+ // checked as it is kept, to four decimals: a region narrower than that is none, and its event
127
+ // would be refused as it is written ("region"), with nothing to say why
128
+ const r = validRegion(b.region) ? roundRegion(b.region) : null;
129
+ if (!r || !validRegion(r))
130
+ return "region is [left, top, right, bottom], fractions of the page from its top left (0 to 1), left < right and top < bottom, at least 0.0001 apart.";
131
+ place.region = r;
129
132
  }
130
133
  if (b.quote !== undefined && b.quote !== null) {
131
134
  const q = typeof b.quote === "string" ? { text: b.quote } : b.quote;
@@ -202,6 +205,10 @@ export class LibraryApi {
202
205
  if (had) {
203
206
  let restored = {};
204
207
  if (had.archived) {
208
+ // the same bytes again restore the file, as Restore does: what others made, an agent
209
+ // restores at level full only (decision 20), whichever way it asks
210
+ if (this.mayChange(had, who, "This file"))
211
+ return fail(403, `${had.title} is in the library already, archived by ${had.archived.by}; it was added by ${had.by}: an agent restores what others made at level full only.`, "Ask a person to restore it from the library's archive, or for a token at level full.");
205
212
  const r = this.record({ op: "file-restore", sha: put.sha }, who);
206
213
  const no = this.written(r);
207
214
  if (no)