visualproof 0.6.0 → 0.7.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 (60) hide show
  1. package/README.md +2 -2
  2. package/dist/extractor/Extract.lean +13 -3
  3. package/dist/server/agent.js +102 -2879
  4. package/dist/server/agentactivity.js +72 -0
  5. package/dist/server/agentadvice.js +115 -0
  6. package/dist/server/agentdocs.js +1 -1
  7. package/dist/server/agententry.js +180 -0
  8. package/dist/server/agentexport.js +469 -0
  9. package/dist/server/agentfollow.js +384 -0
  10. package/dist/server/agentgraph.js +128 -0
  11. package/dist/server/agentlean.js +144 -0
  12. package/dist/server/agentlibrary.js +1 -1
  13. package/dist/server/agentplaces.js +242 -0
  14. package/dist/server/agentread.js +569 -0
  15. package/dist/server/agenttalk.js +282 -0
  16. package/dist/server/agenttcb.js +378 -0
  17. package/dist/server/agentviews.js +2 -2
  18. package/dist/server/app.js +377 -0
  19. package/dist/server/appaccess.js +127 -0
  20. package/dist/server/appagent.js +110 -0
  21. package/dist/server/appgraph.js +176 -0
  22. package/dist/server/appshare.js +179 -0
  23. package/dist/server/appsite.js +120 -0
  24. package/dist/server/apptalk.js +178 -0
  25. package/dist/server/artcmd.js +1 -1
  26. package/dist/server/events.js +1 -1
  27. package/dist/server/history.js +51 -3
  28. package/dist/server/historyapi.js +34 -2
  29. package/dist/server/hover.js +11 -7
  30. package/dist/server/identity.js +1 -1
  31. package/dist/server/libraryroute.js +3 -3
  32. package/dist/server/live.js +61 -2
  33. package/dist/server/main.js +17 -1746
  34. package/dist/server/options.js +350 -0
  35. package/dist/server/outside.js +311 -0
  36. package/dist/server/pastdoc.js +22 -3
  37. package/dist/server/project.js +28 -4
  38. package/dist/server/send.js +259 -0
  39. package/dist/server/talk.js +13 -7
  40. package/dist/server/tokens.js +3 -3
  41. package/dist/server/view.js +3 -1
  42. package/dist/server/views.js +1 -1
  43. package/dist/shared/artrecipe.js +3 -3
  44. package/dist/shared/collab.js +1 -1
  45. package/dist/shared/comment.js +1 -1
  46. package/dist/shared/doc/build.js +30 -14
  47. package/dist/shared/doc/latex.js +7 -2
  48. package/dist/shared/doc/model.js +4 -2
  49. package/dist/shared/doc/sections.js +21 -4
  50. package/dist/shared/outside.js +16 -0
  51. package/dist/shared/pastgraph.js +11 -2
  52. package/dist/shared/scope.js +1 -1
  53. package/dist/web/bundle/app.js +49580 -49284
  54. package/dist/web/src/selection.js +1 -1
  55. package/dist/web/src/theme.js +1 -1
  56. package/dist/web/src/visitors.js +1 -1
  57. package/docs/agent-api/export.md +8 -5
  58. package/docs/agent-api/history.md +3 -1
  59. package/docs/agent-api/reading.md +8 -2
  60. package/package.json +1 -1
@@ -0,0 +1,350 @@
1
+ // The command line, as server/main.ts reads it: each command's options, the usage `--help` prints,
2
+ // and what is checked before anything runs — what goes only with a git address, with a key or with
3
+ // a command, a branch's name, a template that cannot make a link — each said in a line, with status 2.
4
+ import { join, resolve } from "node:path";
5
+ import { AGENT_LEVELS } from "./agent.js";
6
+ import { branchProblem, DEFAULT_BUILD_HOURS, DEFAULT_POLL_MINUTES, isGitAddress, MATHLIB_CACHES, MAX_BUILD_HOURS, MIN_BUILD_HOURS, MIN_POLL_MINUTES, parseBuildHours, parseRemote } from "./deploy.js";
7
+ import { cacheHome, dataHome } from "./dirs.js";
8
+ import { packageVersion } from "./doctor.js";
9
+ import { UPDATE_MODES } from "./updates.js";
10
+ import { DEFAULT_JOBS } from "./usage.js";
11
+ import { invalidTemplate } from "../shared/editor.js";
12
+ import { DEFAULT_LIBRARY_LIMIT, validSha } from "../shared/library.js";
13
+ /** The usage, to stderr with status 2 for a command line it cannot read; asked for (`--help`),
14
+ * to stdout with status 0. */
15
+ function usage(code = 2) {
16
+ (code === 0 ? console.log : console.error)(`usage: visualproof serve <lean-project | git URL> [--branch name] [--poll ${DEFAULT_POLL_MINUTES}] [--build-timeout ${DEFAULT_BUILD_HOURS}]
17
+ [--mathlib-cache shared|own] [--root Name]... [--port 4173] [--host 127.0.0.1]
18
+ [--site dir] [--cache dir] [--graph saved.json]
19
+ [--identity-header X-Forwarded-User] [--editor URL-template]
20
+ [--agents read|advise|full] [--access-key-file file] [--public] [--jobs ${DEFAULT_JOBS}]
21
+ [--public-url https://host] [--allowed-host name]... [--library-limit GB]
22
+ [--updates auto|notify|off]
23
+ visualproof purge-file <lean-project> <sha256> [--site dir] [--cache dir]
24
+ visualproof export <lean-project> -o page.html [--label text] [--root Name]... [--graph saved.json]
25
+ [--jobs ${DEFAULT_JOBS}] [--site dir] [--cache dir]
26
+ visualproof artwork <lean-project> --recipe recipe.json [-o name] [--png] [--svg] [--root Name]...
27
+ [--site dir] [--cache dir] [--jobs ${DEFAULT_JOBS}] [--screen 1920x1200] [--graph saved.json]
28
+ visualproof doctor [<lean-project>] [--root Name]... [--site dir] [--cache dir]
29
+ visualproof --version
30
+
31
+ Reads what \`lake build\` left in <lean-project>/.lake. It never builds the project, and writes
32
+ nothing into it.
33
+
34
+ A git URL where the project goes (https://github.com/owner/repo, ssh://…, git@host:owner/repo, or
35
+ file:///path for a repository on this machine): the server follows that branch of the repository
36
+ in a clone of its own in the cache, never a workspace, and builds each new commit itself —
37
+ \`lake exe cache get\` where the project is on Mathlib, then \`lake build\` — one at a time, the
38
+ newest commit pushed meanwhile the only one built after it; the board shows the last build
39
+ meanwhile. Git's own credentials (an SSH key, a deploy key, a credential helper): VisualProof keeps
40
+ none, and nothing waits on a prompt. Whoever can push to the branch runs code on this machine, as
41
+ the server's user (SECURITY.md): run it as a user of its own, and protect the branch.
42
+ --branch the branch followed (default: the remote's default branch); never a pull request's or
43
+ a fork's
44
+ --poll how often the remote is asked for the branch's head, in minutes (default ${DEFAULT_POLL_MINUTES})
45
+ --build-timeout the hours past which \`lake build\` is stopped and the build failed, its commit
46
+ not tried again (default ${DEFAULT_BUILD_HOURS}; ${MIN_BUILD_HOURS} to ${MAX_BUILD_HOURS}, fractions too: 1.5)
47
+ --mathlib-cache where \`lake exe cache get\` keeps Mathlib's files: shared (the default) — the
48
+ user's, as their own builds' (~/.cache/mathlib, or MATHLIB_CACHE_DIR), so that a build
49
+ fetches only what they have not; own — the deployment's, in its folder of the cache
50
+ (deploy/<repo>-<hash>/mathlib-cache/): about 0.45 GB, the version of Mathlib checked
51
+ out alone (\`lake exe cache clean\` after each fetch), removed with that folder
52
+
53
+ --root what to read: a library of the lakefile (the modules Lake builds of it) or a module;
54
+ repeatable. By default the lakefile's default targets.
55
+ --site the site's folder: the review's logs and what this running site accumulates — the
56
+ history of builds, the library's files, the views and their layouts, the timeline,
57
+ conversations with agents, agents' tokens and personal links. It cannot be derived
58
+ again and holds secrets: back it up, never commit it. One server at a time uses a
59
+ site. Relative to the current directory, or absolute; default:
60
+ ${join(dataHome(), "sites", "<project>-<hash>")}
61
+ --cache the root of the caches, which a rebuild makes again: the project's (extractions, the
62
+ usage pass's results, prepared views, typeset documents, Lean's language server's
63
+ folder) in projects/<project>-<hash>/, the Lean programs compiled per toolchain in
64
+ lean/, the page's bundle in web/, VisualProof's own releases in updates.json and the
65
+ patches downloaded in versions/ (code the servers run: keep the root yours alone),
66
+ and the tips' tour film in media/.
67
+ Default: ${cacheHome()}
68
+ (XDG_DATA_HOME and XDG_CACHE_HOME move both homes, on any platform)
69
+
70
+ --graph use a saved extraction (a projects/*/extract/*.json of the cache) instead of reading
71
+ the build; the project path still supplies the sources
72
+ export writes one self-contained HTML file: the page, its data and the sources, frozen
73
+
74
+ artwork makes the picture an artwork recipe says (the agents' API's, docs/agent-api/export.md:
75
+ the size, the look, names by a rule, the targets, the selection), as the page's
76
+ artwork mode makes it, without a browser: an SVG and a PNG (--png or --svg for one),
77
+ named by -o (a name, or a path without its extension), else the recipe's title, else
78
+ the project. With --site, the board as that site keeps it (its layout, pins and folds)
79
+ and its review's targets; without, the board as a new site would lay it out, and the
80
+ targets the recipe names. --screen: the screen the board is drawn on, as a reader's
81
+ (default 1920x1200, at least 640x480): how large names are in the picture. It reads
82
+ the site, never writes it; it writes the files and the cache's extraction. Anything
83
+ that fails ends it with status 1. Notes are not drawn yet.
84
+
85
+ --identity-header behind a proxy that authenticates (Tailscale, oauth2-proxy, …): take each
86
+ reader's name from this header instead of the name the page gives. Only use it when
87
+ the proxy is the only way to reach the server: the header is believed as it comes.
88
+
89
+ --editor link each declaration to its place in an editor, through a URL template:
90
+ "vscode://file/{path}:{line}:{col}" for VS Code on this machine, or
91
+ "vscode://vscode-remote/ssh-remote+HOST{path}:{line}" for VS Code over SSH to it.
92
+ {path} is the file on this machine, {rel} the file in the project, {line} and {col}
93
+ where the declaration starts. Each reader can set their own in the page (?).
94
+
95
+ --agents what AI agents (requests with X-VisualProof-Kind: agent; their API is /api/agent) may
96
+ change: read — nothing; advise — give advice and leave notes; full (the default) — also
97
+ whatever a person can do through the page (targets, key declarations, trust, views,
98
+ pins), each recorded as the agent's. An agent's opinion is advice whatever the level:
99
+ shown apart, never counted; agent tokens stay with people.
100
+
101
+ --jobs how many modules the usage pass elaborates at once (default ${DEFAULT_JOBS}; 0: no pass). The
102
+ pass finds which notation each declaration is written in and the exact context it is
103
+ written in (TCBSpec.md, decisions 2 and 4); its findings follow the extractor's graph
104
+ as its next version. Each worker takes about 400 MB of its own, several GB on a large
105
+ project on Mathlib (serve and doctor say which --jobs the memory fits); with memory
106
+ short, fewer run at a time. The import of a module is bound by memory bandwidth: past
107
+ about a quarter of the cores it gains little.
108
+
109
+ --access-key-file let in only those given the link http://<host>:<port>/k/<key>, the key being
110
+ what the file holds (one is made there, readable by its owner alone, if there is none).
111
+ The link leaves a cookie in the browser; agents send Authorization: Bearer <key>.
112
+ Anything else is answered 404, but a browser asking for a page, which is shown one
113
+ that says the site is private and nothing of it. Over plain HTTP the key crosses the
114
+ network in the clear: it keeps out who was not given the link, not who can watch the
115
+ traffic.
116
+ Share also gives an AI agent a token of its own (vpa_…), which reaches /api/agent only,
117
+ and a person a link of their own (/i/vpp_…), which lets them in and says who they are,
118
+ and anyone a read-only link (/r/vpr_…), which lets them see and change nothing shared.
119
+
120
+ --public with --access-key-file: whoever comes without the key or a link of their own is let
121
+ in read-only, rather than turned away: they see the project, its review and its
122
+ history, change nothing that is shared, and are seen by nobody. For a project anyone
123
+ may read (a showcase); never for a private one.
124
+
125
+ --public-url the address people reach this server at, when it is not the one their browser
126
+ sends (a proxy in front): the links Share and Copy link make, and the prompt Share
127
+ makes for an agent, carry it.
128
+
129
+ --allowed-host without --access-key-file, a name this server is reached by; repeatable. A
130
+ server without a key answers only requests for an IP address, localhost (and the names
131
+ under it), --public-url's host and these: any other name is answered 403, so that a web
132
+ page that makes its own name resolve to this machine (DNS rebinding) cannot use it. Give
133
+ the names people reach it by: a proxy's in front of it, this machine's own. With a key,
134
+ the key keeps everyone else out, whatever the name.
135
+
136
+ --updates what the server does with VisualProof's own releases, asked of the npm registry
137
+ (through npm) at start and once a day: auto (the default) — a patch of the version it
138
+ runs (0.4.x) is downloaded into the cache and taken at a quiet moment, nobody on a page,
139
+ open pages reloading where they were; a newer version (0.5.0) is told of, in the log
140
+ and the help, with the command that installs it, after which the server restarts into
141
+ it by itself; notify — told of, never downloaded; off — never asked (and no
142
+ supervisor process), and the tips' tour film never fetched: the server reaches the
143
+ network for nothing of its own. VISUALPROOF_UPDATES says the same. From a checkout,
144
+ never asked.
145
+
146
+ --library-limit the most the project's library of papers and images may take, in GB (default
147
+ ${DEFAULT_LIBRARY_LIMIT / 1024 ** 3}); its files are kept in the site directory, by their hash.
148
+
149
+ purge-file deletes a file's bytes from the library for good (a file added by mistake), and what
150
+ was read of it; its record and its links stay in the review, saying this server lacks it.
151
+ Nothing else deletes a file: removing one from the page archives it.
152
+
153
+ doctor checks what VisualProof needs on this machine (Node, elan, Poppler, XeLaTeX) and, given
154
+ a project, its toolchain, its build and its site, and says what to do; it changes
155
+ nothing.`);
156
+ process.exit(code);
157
+ }
158
+ export function parseArgs(argv) {
159
+ const [cmd, ...rest] = argv;
160
+ if (cmd === "--help" || cmd === "-h" || cmd === "help")
161
+ usage(0);
162
+ if (cmd === "--version" || cmd === "-v") {
163
+ console.log(packageVersion());
164
+ process.exit(0);
165
+ }
166
+ if (cmd !== "serve" && cmd !== "export" && cmd !== "purge-file" && cmd !== "doctor" && cmd !== "artwork")
167
+ usage();
168
+ const fromEnv = process.env.VISUALPROOF_UPDATES;
169
+ if (cmd === "serve" && fromEnv !== undefined && !UPDATE_MODES.includes(fromEnv)) {
170
+ console.error(`visualproof: VISUALPROOF_UPDATES=${fromEnv}: say auto, notify or off`);
171
+ process.exit(2);
172
+ }
173
+ 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, buildHours: null, mathlibCache: null };
174
+ for (let i = 0; i < rest.length; i++) {
175
+ const a = rest[i];
176
+ const val = () => rest[++i] ?? usage();
177
+ if (a === "--root")
178
+ o.roots.push(val());
179
+ else if (a === "--port")
180
+ o.port = Number(val());
181
+ else if (a === "--host")
182
+ o.host = val();
183
+ else if (a === "--site")
184
+ o.site = val();
185
+ else if (a === "--cache")
186
+ o.cache = val();
187
+ else if (a === "--graph")
188
+ o.graph = resolve(val());
189
+ else if (a === "-o" || a === "--out")
190
+ o.out = resolve(val());
191
+ else if (a === "--label")
192
+ o.label = val();
193
+ else if (a === "--identity-header")
194
+ o.identityHeader = val();
195
+ else if (a === "--editor")
196
+ o.editor = val();
197
+ else if (a === "--access-key-file")
198
+ o.accessKeyFile = resolve(val());
199
+ else if (a === "--public")
200
+ o.publicRead = true;
201
+ else if (a === "--recipe" && cmd === "artwork")
202
+ o.recipe = resolve(val());
203
+ else if (a === "--png" && cmd === "artwork")
204
+ o.png = true;
205
+ else if (a === "--svg" && cmd === "artwork")
206
+ o.svg = true;
207
+ else if (a === "--screen" && cmd === "artwork") {
208
+ const v = val();
209
+ const m = /^(\d+)x(\d+)$/.exec(v);
210
+ // said in a line, as --public-url is, rather than the whole usage
211
+ if (!m || +m[1] < 640 || +m[2] < 480 || +m[1] > 10000 || +m[2] > 10000) {
212
+ console.error(`visualproof artwork: --screen ${v}: give the screen's width and height in pixels, e.g. 1920x1200, from 640x480 to 10000x10000`);
213
+ process.exit(2);
214
+ }
215
+ o.screen = { w: +m[1], h: +m[2] };
216
+ }
217
+ else if (a === "--public-url") {
218
+ const u = val();
219
+ if (!/^https?:\/\/[^\s/?#]+\/?$/.test(u)) {
220
+ console.error(`visualproof: --public-url ${u}: give the scheme and the host (and port), e.g. https://example.org`);
221
+ process.exit(2);
222
+ }
223
+ o.publicUrl = u.replace(/\/+$/, "");
224
+ }
225
+ else if (a === "--allowed-host" && cmd === "serve") {
226
+ const h = val();
227
+ // a name, as a Host header gives it without its port (CollabSpec decision 12)
228
+ 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)) {
229
+ 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`);
230
+ process.exit(2);
231
+ }
232
+ o.allowedHosts.push(h);
233
+ }
234
+ else if (a === "--jobs") {
235
+ const n = Number(val());
236
+ if (!Number.isInteger(n) || n < 0 || n > 256)
237
+ usage();
238
+ o.jobs = n;
239
+ }
240
+ else if (a === "--branch" && cmd === "serve")
241
+ o.branch = val();
242
+ else if (a === "--poll" && cmd === "serve") {
243
+ const v = val();
244
+ const m = Number(v);
245
+ // said in a line, as --screen is
246
+ if (!/^\d+(\.\d+)?$/.test(v) || m < MIN_POLL_MINUTES || m > 1440) {
247
+ console.error(`visualproof: --poll ${v}: say how often the remote is asked, in minutes, from ${MIN_POLL_MINUTES} to 1440 (a day)`);
248
+ process.exit(2);
249
+ }
250
+ o.poll = m;
251
+ }
252
+ else if (a === "--build-timeout" && cmd === "serve") {
253
+ const h = parseBuildHours(val());
254
+ if (typeof h !== "number") {
255
+ console.error(`visualproof: ${h.error}`);
256
+ process.exit(2);
257
+ }
258
+ o.buildHours = h;
259
+ }
260
+ else if (a === "--mathlib-cache" && cmd === "serve") {
261
+ const v = val();
262
+ if (!MATHLIB_CACHES.includes(v)) {
263
+ console.error(`visualproof: --mathlib-cache ${v.slice(0, 40)}: say shared (the user's Mathlib cache, as their builds': the default) or own (the deployment's, in its folder of the cache)`);
264
+ process.exit(2);
265
+ }
266
+ o.mathlibCache = v;
267
+ }
268
+ else if (a === "--updates" && cmd === "serve") {
269
+ const mode = val();
270
+ if (!UPDATE_MODES.includes(mode))
271
+ usage();
272
+ o.updates = mode;
273
+ }
274
+ else if (a === "--agents") {
275
+ const level = val();
276
+ if (!AGENT_LEVELS.includes(level))
277
+ usage();
278
+ o.agents = level;
279
+ }
280
+ else if (a === "--library-limit") {
281
+ const gb = Number(val());
282
+ if (!Number.isFinite(gb) || gb <= 0 || gb > 10000)
283
+ usage();
284
+ o.libraryLimit = Math.round(gb * 1024 ** 3);
285
+ }
286
+ else if (a.startsWith("-"))
287
+ usage();
288
+ else if (!o.path)
289
+ o.path = a;
290
+ else if (cmd === "purge-file" && !o.sha)
291
+ o.sha = a;
292
+ else
293
+ usage();
294
+ }
295
+ if ((!o.path && cmd !== "doctor") || (cmd === "export" && !o.out) || (cmd === "artwork" && !o.recipe))
296
+ usage();
297
+ // a git address where the project goes: the branch followed and built (DeploySpec decisions 1,
298
+ // 10 and 11), its address and branch checked before anything is run
299
+ if (o.path && isGitAddress(o.path)) {
300
+ if (cmd !== "serve") {
301
+ 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).`);
302
+ process.exit(2);
303
+ }
304
+ const r = parseRemote(o.path);
305
+ if ("error" in r) {
306
+ console.error(`visualproof: ${r.error}`);
307
+ process.exit(2);
308
+ }
309
+ if (o.graph) {
310
+ console.error("visualproof: --graph does not go with a git address: the server builds what it shows");
311
+ process.exit(2);
312
+ }
313
+ const why = o.branch === null ? null : branchProblem(o.branch);
314
+ if (why) {
315
+ console.error(`visualproof: --branch ${why}`);
316
+ process.exit(2);
317
+ }
318
+ o.remote = r;
319
+ }
320
+ else {
321
+ // what is about following a branch and building it: a folder is never built (DeploySpec
322
+ // decision 15)
323
+ const given = [["--branch", o.branch], ["--poll", o.poll], ["--build-timeout", o.buildHours], ["--mathlib-cache", o.mathlibCache]].filter(([, v]) => v !== null).map(([n]) => n);
324
+ if (given.length) {
325
+ console.error(`visualproof: ${given.length > 1 ? `${given.slice(0, -1).join(", ")} and ${given[given.length - 1]} go` : `${given[0]} goes`} with a git address (serve https://github.com/owner/repo): a folder is read as its owner's builds leave it, never built`);
326
+ process.exit(2);
327
+ }
328
+ }
329
+ // read-only for whoever comes, beside those the key or a link lets in: without a key, everyone is in
330
+ // already; behind a sign-in, there is nobody else
331
+ if (o.publicRead && (!o.accessKeyFile || o.identityHeader)) {
332
+ console.error("visualproof: --public lets in read-only whoever has no key: it needs --access-key-file, and does not go with --identity-header");
333
+ process.exit(2);
334
+ }
335
+ // the names a server without a key answers: with one, the key keeps everyone else out whatever
336
+ // the name (CollabSpec decision 12)
337
+ if (o.allowedHosts.length && o.accessKeyFile)
338
+ console.warn("visualproof: --allowed-host changes nothing with --access-key-file: the key lets in whoever has it, by any name");
339
+ if (cmd === "purge-file" && !validSha(o.sha)) {
340
+ console.error("visualproof purge-file: give the file's SHA-256, 64 hex digits, as the page's library shows it.");
341
+ process.exit(2);
342
+ }
343
+ // a template that cannot make a link is said at the start, not found out by a reader's click
344
+ const why = o.editor === null ? null : invalidTemplate(o.editor);
345
+ if (why) {
346
+ console.error(`visualproof: --editor ${o.editor}: ${why}`);
347
+ process.exit(2);
348
+ }
349
+ return o;
350
+ }
@@ -0,0 +1,311 @@
1
+ // Where a declaration outside the project is written (ReviewSpec, "The source window", the owner's
2
+ // decision of 2026-10-20): in a package of the project's lake-manifest (`.lake/packages/`), or in
3
+ // Lean's core (`Init`, `Std`, `Lean`, `Lake`), whose sources the toolchain carries in `src/lean/`.
4
+ // The source window shows it as it shows the project's files, with Lean's hovers, and links it to
5
+ // GitHub at the package's revision (DeploySpec decision 13).
6
+ //
7
+ // A module is found as Lake and Lean find it, never by a path a request gives: the package whose
8
+ // build holds its `.olean` (Lean's search path is the packages' `lib/lean` folders, then the
9
+ // toolchain's), then its source under that package's source folders as its lakefile sets them (the
10
+ // package's `srcDir`, and each library's under it: Lake's layout), the libraries that hold the
11
+ // module first; for core, the toolchain's `src/lean/`. Only a `.lean` file that is there, resolved
12
+ // with its symbolic links, inside the package's folder (or the toolchain's sources) is ever read: a
13
+ // module name is checked part by part before it is made a path, and the server asks only of modules
14
+ // the graph's outside declarations are in (server/live.ts, `outsideFile`). The lakefiles are read
15
+ // as text, never elaborated (server/project.ts).
16
+ //
17
+ // The revisions are what the lake-manifest pins (`rev`), and for core the toolchain: a past build's
18
+ // outside source is the present one only where its package is at the same revision now (HistorySpec
19
+ // decision 24; server/history.ts keeps each build's).
20
+ import { spawnSync } from "node:child_process";
21
+ import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
22
+ import { homedir } from "node:os";
23
+ import { join, relative, resolve, sep } from "node:path";
24
+ import { splitName } from "../shared/layout.js";
25
+ import { childEnv } from "./children.js";
26
+ import { parseRemote } from "./deploy.js";
27
+ import { readLakefileLean, readLakefileToml } from "./project.js";
28
+ import { lakeTextHash } from "./status.js";
29
+ /** A module name's parts as a path's: each a name Lake would make a file of, never `.`, `..`, a
30
+ * separator or a control character; null where one is not. */
31
+ export function modulePath(module) {
32
+ if (!module || module.length > 1000)
33
+ return null;
34
+ const parts = splitName(module).map((p) => (p.startsWith("«") && p.endsWith("»") ? p.slice(1, -1) : p));
35
+ for (const p of parts)
36
+ if (!p || p === "." || p === ".." || /[/\\\0-\x1f\x7f«»]/.test(p) || p.length > 255)
37
+ return null;
38
+ return parts;
39
+ }
40
+ /** Whether `path` is `dir` or inside it, both resolved. */
41
+ const inside = (path, dir) => path === dir || path.startsWith(dir.endsWith(sep) ? dir : dir + sep);
42
+ /** The resolved path of what is there, or null. */
43
+ function real(p) {
44
+ try {
45
+ return realpathSync(p);
46
+ }
47
+ catch {
48
+ return null;
49
+ }
50
+ }
51
+ /** A toolchain's revision in Lean's repository: a release's tag (`leanprover/lean4:v4.35.0`, or
52
+ * `v4.35.0` alone); null for any other (a nightly, a pull request's build). */
53
+ export function toolchainTag(toolchain) {
54
+ const m = /^(?:leanprover\/lean4:)?(v\d+\.\d+\.\d+(?:-rc\d+)?)$/.exec(toolchain.trim());
55
+ return m ? m[1] : null;
56
+ }
57
+ /** Whether a library holds a module, as Lake decides it: a root is a prefix of it, or a glob
58
+ * matches it. */
59
+ function holds(lib, module) {
60
+ const under = (base) => module === base || module.startsWith(`${base}.`);
61
+ if ((lib.roots ?? [lib.name]).some(under))
62
+ return true;
63
+ return (lib.globs ?? []).some((g) => {
64
+ const m = /^(.*)\.([+*])$/.exec(g);
65
+ return m ? (m[2] === "*" ? under(m[1]) : module.startsWith(`${m[1]}.`)) : module === g;
66
+ });
67
+ }
68
+ /** The project's packages and Lean's core as places of outside sources, read again when the
69
+ * manifest or the toolchain changes (a branch followed builds a new commit), and each module's
70
+ * file once found. */
71
+ export class OutsideSources {
72
+ root;
73
+ toolchain;
74
+ /** the manifest's packages as read, and of what: its time and size */
75
+ read = null;
76
+ /** the core, per toolchain: found only when a module of it is asked for */
77
+ cores = new Map();
78
+ found = new Map();
79
+ foundFor = "";
80
+ /** how the toolchain's folder is found (`toolchainDir`; a test gives its own) */
81
+ sysroot;
82
+ constructor(root, toolchain, sysroot = toolchainDir) {
83
+ this.root = root;
84
+ this.toolchain = toolchain;
85
+ this.sysroot = sysroot;
86
+ }
87
+ /** Forget what was read and found: a new graph is read (a build may have fetched a package, or
88
+ * installed a toolchain, the manifest unchanged), and what was not found may be there now. */
89
+ forget() {
90
+ this.read = null;
91
+ this.cores.clear();
92
+ this.found.clear();
93
+ }
94
+ /** The packages the manifest lists, read again where it changed since. */
95
+ packages() {
96
+ const manifest = join(this.root, "lake-manifest.json");
97
+ let stamp = "none";
98
+ try {
99
+ const st = statSync(manifest);
100
+ stamp = `${st.mtimeMs}:${st.size}`;
101
+ }
102
+ catch {
103
+ // no manifest: no packages, the core alone
104
+ }
105
+ if (this.read?.key === stamp)
106
+ return this.read.packages;
107
+ this.read = { key: stamp, packages: this.fromManifest(manifest) };
108
+ return this.read.packages;
109
+ }
110
+ /** The packages the manifest lists, each where Lake keeps it: `packagesDir/<name>` (and its
111
+ * `subDir`) for a git dependency, `dir` for a path one. One that is not there, or whose folder
112
+ * cannot be resolved, is left out. */
113
+ fromManifest(file) {
114
+ let m;
115
+ try {
116
+ m = JSON.parse(readFileSync(file, "utf8"));
117
+ }
118
+ catch {
119
+ return [];
120
+ }
121
+ const packagesDir = typeof m.packagesDir === "string" ? m.packagesDir : join(".lake", "packages");
122
+ const out = [];
123
+ for (const p of (Array.isArray(m.packages) ? m.packages : [])) {
124
+ if (typeof p?.name !== "string" || !p.name)
125
+ continue;
126
+ const str = (x) => (typeof x === "string" && x ? x : null);
127
+ const sub = str(p.subDir);
128
+ const repoDir = p.type === "path" ? str(p.dir) && resolve(this.root, str(p.dir)) : resolve(this.root, packagesDir, p.name);
129
+ if (!repoDir)
130
+ continue;
131
+ const dir = real(sub ? join(repoDir, sub) : repoDir);
132
+ if (!dir)
133
+ continue;
134
+ const rev = str(p.rev);
135
+ const remote = p.type === "git" && str(p.url) ? parseRemote(str(p.url)) : null;
136
+ const repo = remote && !("error" in remote) ? remote.github : null;
137
+ out.push({
138
+ name: p.name,
139
+ core: false,
140
+ dir,
141
+ ...this.layout(dir, str(p.configFile)),
142
+ rev,
143
+ github: repo && rev && /^[0-9a-f]{7,64}$/.test(rev) ? { repo, dir: sub ? sub.replace(/\\/g, "/").replace(/^\.\/|\/$/g, "") : "" } : null,
144
+ });
145
+ }
146
+ return out;
147
+ }
148
+ /** A package's layout from its lakefile, read as text: where Lake builds it (`buildDir`, by
149
+ * default `.lake/build`) and its source folders — each library's `srcDir` under the package's
150
+ * own, and the package's — resolved, those that leave its folder left out. */
151
+ layout(dir, configFile) {
152
+ const pick = [configFile, "lakefile.toml", "lakefile.lean"].find((f) => f && /^lakefile\.(toml|lean)$/.test(f) && existsSync(join(dir, f)));
153
+ let lf = { libs: [] };
154
+ try {
155
+ if (pick)
156
+ lf = (pick.endsWith(".toml") ? readLakefileToml : readLakefileLean)(readFileSync(join(dir, pick), "utf8"));
157
+ }
158
+ catch {
159
+ // an unreadable lakefile: Lake's defaults
160
+ }
161
+ const within = (p) => {
162
+ const r = real(p);
163
+ return r && inside(r, dir) ? r : null;
164
+ };
165
+ const libDirs = [...new Set([lf.buildDir, join(".lake", "build")].filter((b) => !!b).map((b) => join(dir, b, "lib", "lean")))];
166
+ const pkgSrc = join(dir, lf.srcDir ?? ".");
167
+ const srcDirs = [];
168
+ const add = (p, libs) => {
169
+ const r = within(p);
170
+ if (!r)
171
+ return;
172
+ const had = srcDirs.find((s) => s.dir === r);
173
+ if (!had)
174
+ srcDirs.push({ dir: r, libs });
175
+ else if (had.libs && libs)
176
+ had.libs.push(...libs);
177
+ };
178
+ for (const lib of lf.libs)
179
+ add(join(pkgSrc, lib.srcDir ?? "."), [lib]);
180
+ add(pkgSrc, null);
181
+ return { libDirs, srcDirs };
182
+ }
183
+ /** Lean's core: the toolchain's `lib/lean` and its sources, `src/lean/` (Lake's own under
184
+ * `src/lean/lake/`), at the toolchain's release in `leanprover/lean4`, whose `src/` they are;
185
+ * none where the toolchain's folder is not found. */
186
+ core() {
187
+ const tc = this.toolchain();
188
+ if (this.cores.has(tc))
189
+ return this.cores.get(tc);
190
+ const sys = this.sysroot(this.root, tc);
191
+ const dir = sys ? real(join(sys, "src", "lean")) : null;
192
+ const tag = toolchainTag(tc);
193
+ const core = sys && dir
194
+ ? {
195
+ name: "Lean",
196
+ core: true,
197
+ dir,
198
+ libDirs: [join(sys, "lib", "lean")],
199
+ srcDirs: [dir, real(join(dir, "lake"))].filter((d) => !!d && inside(d, dir)).map((d) => ({ dir: d, libs: null })),
200
+ rev: tag,
201
+ github: tag ? { repo: "leanprover/lean4", dir: "src" } : null,
202
+ }
203
+ : null;
204
+ this.cores.set(tc, core);
205
+ return core;
206
+ }
207
+ /** Module `module`'s file: in the package whose build holds its `.olean`, under the source folder
208
+ * of the library that holds it (else any of the package's), resolved inside the package's
209
+ * folder; null where there is none. */
210
+ find(module) {
211
+ const pkgs = this.packages();
212
+ // what was found is for this manifest and toolchain
213
+ const key = `${this.read?.key}\0${this.toolchain()}`;
214
+ if (key !== this.foundFor) {
215
+ this.found.clear();
216
+ this.foundFor = key;
217
+ }
218
+ if (this.found.has(module))
219
+ return this.found.get(module);
220
+ const parts = modulePath(module);
221
+ const result = parts ? this.locate(pkgs, module, parts) : null;
222
+ this.found.set(module, result);
223
+ if (this.found.size > 50_000)
224
+ this.found.delete(this.found.keys().next().value);
225
+ return result;
226
+ }
227
+ locate(pkgs, module, parts) {
228
+ const rel = join(...parts);
229
+ // Lean's search path: the packages' builds, then the toolchain's
230
+ const has = (p) => p.libDirs.some((d) => existsSync(`${join(d, rel)}.olean`));
231
+ let pkg = pkgs.find(has);
232
+ if (!pkg) {
233
+ const core = this.core();
234
+ pkg = core && has(core) ? core : undefined;
235
+ }
236
+ if (!pkg)
237
+ return null;
238
+ // the folders of the libraries that hold it first, then the others, the package's own last
239
+ const order = [...pkg.srcDirs.filter((s) => s.libs?.some((l) => holds(l, module))), ...pkg.srcDirs.filter((s) => !s.libs?.some((l) => holds(l, module)))];
240
+ for (const s of order) {
241
+ const path = real(`${join(s.dir, rel)}.lean`);
242
+ if (!path || !path.endsWith(".lean") || !inside(path, pkg.dir))
243
+ continue;
244
+ try {
245
+ if (!statSync(path).isFile())
246
+ continue;
247
+ }
248
+ catch {
249
+ continue;
250
+ }
251
+ const file = relative(pkg.dir, path).split(sep).join("/");
252
+ const repoPath = pkg.github ? [pkg.github.dir, file].filter(Boolean).join("/") : null;
253
+ return {
254
+ module,
255
+ path,
256
+ file,
257
+ package: { name: pkg.name, core: pkg.core, rev: pkg.rev },
258
+ github: pkg.github && pkg.rev && repoPath ? { repo: pkg.github.repo, rev: pkg.rev, path: repoPath } : null,
259
+ };
260
+ }
261
+ return null;
262
+ }
263
+ /** The package that holds module `module` now, by name (core: "Lean"), and its revision: what a
264
+ * past build's outside source is compared by (HistorySpec decision 24). */
265
+ packageOf(module) {
266
+ return this.find(module)?.package ?? null;
267
+ }
268
+ /** Each package's revision as the manifest pins it now: what a snapshot records (HistorySpec
269
+ * decision 24); the core's is the toolchain, which a snapshot has already. */
270
+ revisions() {
271
+ const out = {};
272
+ for (const p of this.packages())
273
+ if (p.rev)
274
+ out[p.name] = p.rev;
275
+ return out;
276
+ }
277
+ }
278
+ /** Where an outside file is, as the page and the agents are told it; `changed`: its package is at
279
+ * another revision than when the graph was read. */
280
+ export function whereOf(f, changed = false) {
281
+ return { package: f.package.name, core: f.package.core, rev: f.package.rev, github: f.github, ...(changed ? { changed: true } : {}) };
282
+ }
283
+ /** An outside module's text as GET /api/source answers it, as it does a project file's: its file in
284
+ * its package, the text, its Lake hash, and where it is (shared/outside.ts); null where it cannot be
285
+ * read. */
286
+ export function outsideSource(f, changed = false) {
287
+ let text;
288
+ try {
289
+ text = readFileSync(f.path, "utf8");
290
+ }
291
+ catch {
292
+ return null;
293
+ }
294
+ return { module: f.module, file: f.file, text, hash: lakeTextHash(text), outside: whereOf(f, changed) };
295
+ }
296
+ /** The toolchain's folder (Lean's sysroot): elan's, where elan installed it (`$ELAN_HOME`, by
297
+ * default `~/.elan`, `toolchains/<its name>`: `leanprover/lean4:v4.35.0` is
298
+ * `leanprover--lean4---v4.35.0`); else what `lean --print-prefix` says in the project's folder, a
299
+ * Lean of its own, never one elan would install first. Null where neither is there. */
300
+ export function toolchainDir(root, toolchain) {
301
+ const full = /^v?\d+\.\d+\.\d+/.test(toolchain) ? `leanprover/lean4:${toolchain.startsWith("v") ? toolchain : `v${toolchain}`}` : toolchain;
302
+ const elan = process.env.ELAN_HOME || join(homedir(), ".elan");
303
+ const dir = join(elan, "toolchains", full.replace(/\//g, "--").replace(/:/g, "---"));
304
+ if (existsSync(join(dir, "lib", "lean")))
305
+ return dir;
306
+ if (existsSync(join(elan, "toolchains")))
307
+ return null;
308
+ const r = spawnSync("lean", ["--print-prefix"], { cwd: root, encoding: "utf8", timeout: 60_000, env: childEnv() });
309
+ const out = r.status === 0 ? r.stdout.trim() : "";
310
+ return out && existsSync(join(out, "lib", "lean")) ? out : null;
311
+ }