visualproof 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/assets/latex/visualproof.sty +19 -9
  2. package/dist/mcp/visualproof-mcp.mjs +7 -3
  3. package/dist/server/access.js +34 -0
  4. package/dist/server/agent.js +5 -4
  5. package/dist/server/agentlibrary.js +1 -1
  6. package/dist/server/agentviews.js +1 -1
  7. package/dist/server/artcmd.js +1 -1
  8. package/dist/server/askfollow.js +1 -4
  9. package/dist/server/events.js +20 -5
  10. package/dist/server/export.js +17 -4
  11. package/dist/server/fetchurl.js +30 -13
  12. package/dist/server/history.js +3 -1
  13. package/dist/server/identity.js +25 -1
  14. package/dist/server/library.js +10 -3
  15. package/dist/server/live.js +32 -5
  16. package/dist/server/lostfound.js +3 -2
  17. package/dist/server/main.js +113 -28
  18. package/dist/server/poppler.js +48 -10
  19. package/dist/server/renames.js +10 -5
  20. package/dist/server/review.js +10 -0
  21. package/dist/server/reviewapi.js +1 -1
  22. package/dist/server/status.js +29 -8
  23. package/dist/server/talk.js +7 -2
  24. package/dist/server/views.js +21 -7
  25. package/dist/shared/collab.js +14 -8
  26. package/dist/shared/doc/latex.js +5 -3
  27. package/dist/shared/doc/outline.js +6 -3
  28. package/dist/shared/library.js +11 -6
  29. package/dist/shared/review.js +7 -5
  30. package/dist/shared/search.js +7 -3
  31. package/dist/shared/views.js +8 -1
  32. package/dist/web/bundle/app.js +1646 -717
  33. package/dist/web/bundle/edges.js +42 -15
  34. package/dist/web/index.html +2 -2
  35. package/dist/web/src/artwork.js +25 -4
  36. package/dist/web/src/axiomring.js +42 -0
  37. package/dist/web/src/bundles.js +52 -16
  38. package/dist/web/src/canvas.js +62 -20
  39. package/dist/web/src/past.js +28 -0
  40. package/dist/web/src/selection.js +47 -4
  41. package/dist/web/src/status.js +4 -2
  42. package/dist/web/style.css +79 -10
  43. package/docs/agent-api/following.md +1 -1
  44. package/docs/agent-api/library.md +1 -1
  45. package/docs/agent-api/requests.md +3 -1
  46. package/docs/agent-api/views.md +1 -1
  47. package/package.json +1 -1
@@ -208,31 +208,39 @@
208
208
  % what follows: a section of one declaration takes no page of its own. Its heading: the number and
209
209
  % the title, sans-serif and bold, smaller at each level; the cluster's key beneath, small and mono.
210
210
  % A top section names the running head, as LaTeX's own classes do.
211
+ % Headings that follow one another (a section, then its first subsection) ask for that room once,
212
+ % the first of them: until the first line after the last, nothing asks again (\vp@need) nor lets
213
+ % the page break (\vp@keep), as a subsection's own \needspace under its section's heading would
214
+ % break the page there and leave that heading alone at its foot.
215
+ \newif\ifvp@afterhead
216
+ \newcommand\vp@need[1]{\ifvp@afterhead\else\needspace{#1}\fi}
217
+ \newcommand\vp@keep{\ifvp@afterhead\nopagebreak\fi}
218
+ \newcommand\vp@headdone{\global\vp@afterheadtrue\everypar{\global\vp@afterheadfalse\everypar{}}}
211
219
  \newcommand\vp@sectionbreak[1]{\par\addvspace{\ifcase#1\or 22pt plus 8pt minus 4pt\or 16pt plus 6pt minus 3pt\or
212
- 12pt plus 4pt minus 2pt\else 10pt plus 3pt minus 2pt\fi}\needspace{\ifnum#1<3 14\else 10\fi\baselineskip}}
220
+ 12pt plus 4pt minus 2pt\else 10pt plus 3pt minus 2pt\fi}\vp@need{\ifnum#1<3 14\else 10\fi\baselineskip}}
213
221
  \newcommand\vpSection[7]{%
214
222
  \vp@sectionbreak{#2}%
215
- \phantomsection\hypertarget{#1}{}\label{#1}\bookmark[dest=#1,level=#6]{#7}%
223
+ \phantomsection\hypertarget{#1}{}\label{#1}\bookmark[dest=#1,level=#6]{#7}\vp@keep
216
224
  {\raggedright\sffamily\bfseries\ifcase#2\or\LARGE\or\Large\or\large\else\normalsize\fi
217
225
  \ifstrempty{#3}{}{#3\quad}#4\par}\nopagebreak
218
226
  \vspace{1pt}{\footnotesize\color{vpMuted}\vpName{#5}\par}\nopagebreak
219
227
  \ifnum#2=1 \markright{\ifstrempty{#3}{}{#3\enspace}#4}\fi
220
- \vspace{\ifnum#2<3 4pt\else 2pt\fi}}
228
+ \vspace{\ifnum#2<3 4pt\else 2pt\fi}\vp@headdone}
221
229
  % a section's own declarations and notes again after one of its sections, as the board's order may
222
230
  % have them: its title at the level of its sections, unnumbered, "continued" after it
223
231
  \newcommand\vpSectionResume[2]{%
224
232
  \vp@sectionbreak{#1}%
225
233
  {\raggedright\sffamily\bfseries\ifcase#1\or\LARGE\or\Large\or\large\else\normalsize\fi
226
234
  #2\hskip 0.5em{\mdseries\small\color{vpMuted}continued}\par}\nopagebreak
227
- \vspace{\ifnum#1<3 4pt\else 2pt\fi}}
235
+ \vspace{\ifnum#1<3 4pt\else 2pt\fi}\vp@headdone}
228
236
 
229
237
  % ------------------------------------------------------------------------------ a declaration
230
238
 
231
239
  % {id}{short}{kind}: the short name, the kind on the right
232
240
  \newenvironment{vpDecl}[3]{%
233
- \par\needspace{8\baselineskip}%
241
+ \par\vp@need{8\baselineskip}%
234
242
  \vspace{10pt plus 4pt}%
235
- \phantomsection\hypertarget{#1}{}\label{#1}%
243
+ \phantomsection\hypertarget{#1}{}\label{#1}\vp@keep
236
244
  \noindent{\sffamily\bfseries #2}%
237
245
  \hfill\vpKind{#3}\par\nopagebreak\vspace{1pt}}{\par}
238
246
  \newcommand\vpFullName[1]{\noindent{\footnotesize\vpName{#1}}}
@@ -261,14 +269,16 @@
261
269
  % by its number
262
270
  \newenvironment{vpReferences}{\par\addvspace{22pt plus 8pt minus 4pt}\needspace{6\baselineskip}%
263
271
  \phantomsection\hypertarget{vp-references}{}\label{vp-references}\bookmark[dest=vp-references,level=0]{References}%
264
- {\raggedright\sffamily\bfseries\Large References\par}\nopagebreak\vspace{6pt}\small\setlength\parskip{4pt}}{\par}
272
+ {\raggedright\sffamily\bfseries\Large References\par}\nopagebreak\vspace{6pt}\small\setlength\parskip{4pt}%
273
+ % ragged: a title, an upload name and a hash break nowhere, and justified they spread the rest
274
+ \raggedright}{\par}
265
275
  \newcommand\vpReference[5]{\par\noindent\hangindent=2em\hangafter=1
266
276
  \makebox[2em][l]{(#1)}\textit{#2}. #4; #3.\hskip 0.5em{\footnotesize\color{vpMuted}SHA-256 \vpMono{#5}…}\par}
267
277
 
268
278
  % ------------------------------------------------------------------------------ notes and figures
269
279
 
270
280
  % a note of the view, as the document's prose: body text, with room around it
271
- \newenvironment{vpNote}[1]{\par\addvspace{6pt plus 2pt}\phantomsection\hypertarget{#1}{}}{\par\addvspace{6pt plus 2pt}}
281
+ \newenvironment{vpNote}[1]{\par\addvspace{6pt plus 2pt}\phantomsection\hypertarget{#1}{}\vp@keep}{\par\addvspace{6pt plus 2pt}}
272
282
  % a declaration a note names, linked to where the document prints it, with its page
273
283
  \newcommand\vpMention[2]{\hyperlink{#1}{\vpInlineCode{#2}}\nobreak\hskip 0.2em{\footnotesize\color{vpMuted}(p.~\pageref*{#1})}}
274
284
  % an image or a clip a note carries, where the note is (no float): an image at its own size, a clip at
@@ -278,7 +288,7 @@
278
288
  \newsavebox\vp@figbox
279
289
  \newlength\vp@figw
280
290
  \newcommand\vpFigure[5]{%
281
- \par\needspace{8\baselineskip}\vspace{4pt}\noindent
291
+ \par\vp@need{8\baselineskip}\vspace{4pt}\vp@keep\noindent
282
292
  \begin{minipage}{\linewidth}\centering
283
293
  \ifstrempty{#2}{\sbox\vp@figbox{\includegraphics{#1}}\setlength\vp@figw{\wd\vp@figbox}}{\setlength\vp@figw{#2}}%
284
294
  \ifdim\vp@figw>\linewidth \setlength\vp@figw{\linewidth}\fi
@@ -124,7 +124,10 @@ function fetchOffer() {
124
124
  const r = await fetch(`${URL_}/api/agent/mcp/tools`, { headers: auth(), signal: AbortSignal.timeout(15_000) });
125
125
  const body = await r.json().catch(() => null);
126
126
  if (!r.ok || !body || !Array.isArray(body.tools)) {
127
- offerError = `${URL_} answered ${r.status}: ${body?.error ?? "no tools"}${body?.hint ? ` — ${body.hint}` : ""}`;
127
+ // a server that lets in only those given its link answers a token it does not know as it
128
+ // answers anyone: 404, saying nothing more
129
+ const unknown = r.status === 404 && !body?.hint ? " — a server that lets in only those given its link answers so to a token it does not know: check that VISUALPROOF_TOKEN is the whole token from Share (vpa_…), and VISUALPROOF_URL the server it came with" : "";
130
+ offerError = `${URL_} answered ${r.status}: ${body?.error ?? "no tools"}${body?.hint ? ` — ${body.hint}` : ""}${unknown}`;
128
131
  return;
129
132
  }
130
133
  offer = body;
@@ -461,14 +464,15 @@ function received(line) {
461
464
  }
462
465
 
463
466
  /** The client went: what is under way ends, and VisualProof is told the agent left (offline at
464
- * once), within a second and a half. */
467
+ * once), within a second and a half — by this session, so that a session a newer one replaced
468
+ * (its wait answered `replaced`) leaves the newer one listening. */
465
469
  let leaving = false;
466
470
  async function leave() {
467
471
  if (leaving) return;
468
472
  leaving = true;
469
473
  for (const ctl of running.values()) ctl.abort();
470
474
  if (URL_ && TOKEN && !stopped)
471
- await fetch(`${URL_}/api/agent/view`, { method: "POST", headers: { ...auth(), "content-type": "application/json" }, body: '{"leave":true}', signal: AbortSignal.timeout(1500) }).catch(() => {});
475
+ await fetch(`${URL_}/api/agent/view`, { method: "POST", headers: { ...auth(), "content-type": "application/json", "x-visualproof-session": SESSION }, body: '{"leave":true}', signal: AbortSignal.timeout(1500) }).catch(() => {});
472
476
  process.exit(0);
473
477
  }
474
478
 
@@ -12,8 +12,15 @@
12
12
  // Cookies are not kept per port: every server on one host shares a browser's cookie jar. So the
13
13
  // cookie is named per instance, by a digest of its key, and two servers on one host do not put
14
14
  // each other's readers out; its value is made from the key too, never the key itself.
15
+ //
16
+ // Without a key, whoever reaches the port is in — and a web page any browser on this machine opens
17
+ // can reach it too, by DNS rebinding: its name made to resolve to this machine, the page's scripts
18
+ // then ask this server as their own origin, and read the answers. So a server without a key answers
19
+ // only the names it is reached at (CollabSpec decision 12, the owner's decision of 2026-10-18):
20
+ // hostAllowed, below.
15
21
  import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
16
22
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
23
+ import { isIPv4, isIPv6 } from "node:net";
17
24
  /** where the link points: `/k/<key>` */
18
25
  export const KEY_PATH = "/k/";
19
26
  /** what every cookie's name starts with; before 2026-10-08 it was the whole name, for every server,
@@ -93,3 +100,30 @@ function cookieOf(req, name) {
93
100
  }
94
101
  return null;
95
102
  }
103
+ /** A name as Host headers and the options give it, compared: lowercase, without the dot of a name
104
+ * written fully qualified (`example.org.`). */
105
+ const hostKey = (name) => name.toLowerCase().replace(/\.$/, "");
106
+ /** Whether a server without an access key answers a request for `host`, its Host header
107
+ * (CollabSpec decision 12, the owner's decision of 2026-10-18): an IP address (IPv6 in brackets),
108
+ * which a page's scripts reach only as that address, never as a name they made resolve to it;
109
+ * `localhost` and the names under it, which browsers resolve to this machine themselves; and the
110
+ * names in `names` — `--public-url`'s host and every `--allowed-host`. Any port; any case. A
111
+ * request without a Host is not answered, nor is a Host that is no name. */
112
+ export function hostAllowed(host, names) {
113
+ const m = /^(?:\[([0-9A-Fa-f:.]+)\]|([A-Za-z0-9._-]+))(?::\d{1,5})?$/.exec(host ?? "");
114
+ if (!m)
115
+ return false;
116
+ if (m[1] !== undefined)
117
+ return isIPv6(m[1]);
118
+ const name = hostKey(m[2]);
119
+ if (!name)
120
+ return false;
121
+ if (isIPv4(name) || name === "localhost" || name.endsWith(".localhost"))
122
+ return true;
123
+ return names.some((n) => hostKey(n) === name);
124
+ }
125
+ /** The name `--public-url` gives, as hostAllowed compares it (its port is not part of it). */
126
+ export function urlHost(url) {
127
+ const h = new URL(url).hostname;
128
+ return h.startsWith("[") ? h.slice(1, -1) : h;
129
+ }
@@ -772,7 +772,7 @@ export class AgentApi {
772
772
  if (job.state === "running")
773
773
  return fail(409, "That document is still being typeset.", `Ask again in a few seconds, or wait for GET /api/agent/export/job?id=${job.id} to say done.`, { job }, { "retry-after": "2" });
774
774
  if (job.state === "failed")
775
- return fail(409, `That document could not be typeset: ${job.error ?? "XeLaTeX failed"}.`, "Its job has the end of XeLaTeX's log (GET /api/agent/export/job).", { job });
775
+ return fail(409, `That document could not be typeset: ${(job.error ?? "XeLaTeX failed").replace(/\.$/, "")}.`, "Its job has the end of XeLaTeX's log (GET /api/agent/export/job).", { job });
776
776
  const f = ex.file(id, w);
777
777
  if (!f)
778
778
  return fail(404, "That document has left the cache.", "Ask for it again: POST /api/agent/export.");
@@ -2334,10 +2334,11 @@ export class AgentApi {
2334
2334
  const me = whoOf(a.caller);
2335
2335
  const b = body(a);
2336
2336
  if (b.leave === true) {
2337
+ // an agent with a token that leaves is offline until it listens or asks again — unless its
2338
+ // MCP server says it is a session that a newer one replaced, which goes on
2339
+ if (a.caller.token && this.world.talk && !this.world.talk.left(a.caller.token.id, a.session ?? null))
2340
+ return ok({ ok: true, left: false, note: "Another session of your token listens now: it stays." });
2337
2341
  this.world.presence.drop(agentKey(me));
2338
- // an agent with a token that leaves is offline until it listens or asks again
2339
- if (a.caller.token)
2340
- this.world.talk?.left(a.caller.token.id);
2341
2342
  return ok({ ok: true, left: true });
2342
2343
  }
2343
2344
  const p = this.goingTo(a, b);
@@ -108,7 +108,7 @@ export function libraryEndpoints(world, refuse) {
108
108
  },
109
109
  {
110
110
  method: "POST", path: "/api/agent/library/url", needs: "advise",
111
- about: `Add the file at an address to the library: the server fetches it once and keeps it (an arXiv abstract's address, arxiv.org/abs/…, gives its PDF). http and https only; never an address of a private or local network; at most ${MAX_FILE / (1024 * 1024)} MB; the type is read from the bytes: a PDF, PNG, JPEG, WebP or GIF, never SVG. The same bytes are stored once: a file already in the library is answered as it is (existed: true), restored where it was archived.`,
111
+ about: `Add the file at an address to the library: the server fetches it once and keeps it (an arXiv abstract's address, arxiv.org/abs/…, gives its PDF). http and https only; never an address of a private or local network; at most ${MAX_FILE / (1024 * 1024)} MB; the type is read from the bytes: a PDF, PNG, JPEG, WebP or GIF, never SVG. The same bytes are stored once: a file already in the library is answered as it is (existed: true), restored where it was archived (another's at level full only).`,
112
112
  params: { url: "the file's address" },
113
113
  returns: "{ok, file, existed}",
114
114
  run: write((l, a, who) => l.addUrl(body(a).url, who)),
@@ -289,7 +289,7 @@ export function viewEndpoints(world, refuseFull) {
289
289
  return fail(400, q, "Send the scope as its text or its JSON form.");
290
290
  const c = V.book.setScope(v.id, q, a.caller.name);
291
291
  if (typeof c === "string")
292
- return fail(400, c, "");
292
+ return fail(400, c, fixedScope(v) ? "POST views/duplicate {id: \"project\"} makes a copy of it to rescope." : "");
293
293
  if (c)
294
294
  V.changed(c, true);
295
295
  there(whoOf(a.caller), v);
@@ -204,7 +204,7 @@ export function makeArtwork(prep, review, project, ask) {
204
204
  },
205
205
  targets: s.targets === "chosen" ? s.shownTargets : s.targets === "plain" ? [] : idx.map((j) => g.nodes[j].name),
206
206
  selection: sel,
207
- notesLeftOut: s.notes ? (project.notes ?? []).length : 0,
207
+ notesLeftOut: s.notes ? (project.notes ?? []).length + (ask.projectNotes ?? 0) : 0,
208
208
  svg,
209
209
  file: (kind) => artName(title, kind),
210
210
  };
@@ -16,13 +16,10 @@
16
16
  // The agents' endpoint (POST /api/agent/ask-follow) is server/agent.ts's, in its shape
17
17
  // (server/agentviews.ts, AgentEndpoint), as the notes' are (server/agentnotes.ts).
18
18
  import { randomBytes } from "node:crypto";
19
- import { sameName } from "../shared/collab.js";
19
+ import { nameKey, sameName } from "../shared/collab.js";
20
20
  import { ASK_GRACE_MS, ASK_MS, ASK_WORDS, askLine, cleanAnswer, foldAnswers } from "../shared/askfollow.js";
21
21
  import { fail, whoOf } from "./agent.js";
22
22
  import { agentKey } from "./presence.js";
23
- /** The key a name counts under: whatever the case, however Unicode writes it (shared/collab.ts,
24
- * sameName). */
25
- const nameKey = (s) => s.trim().normalize("NFKC").toLowerCase();
26
23
  export class Asks {
27
24
  port;
28
25
  ms;
@@ -129,26 +129,41 @@ export class Timeline {
129
129
  /** A new graph: every target's TCB is compared with the one it had, and a change is an event.
130
130
  * `renamed`: the renames found against the build before (HistorySpec.md), old name → new: a
131
131
  * declaration renamed is the same one, neither removed nor added, and a target renamed keeps its
132
- * baseline. */
132
+ * baseline. So are the renames in force (the review's `renamed`, followed on to the name the
133
+ * graph has): a build's first graph waits for the usage pass and is not compared, so the graph
134
+ * that is (the usage pass's, the build read again) finds no renames of its own; and a move made
135
+ * by hand renames too. */
133
136
  graphChanged(prep, review, renamed = new Map()) {
134
137
  const meta = JSON.parse(gunzipSync(prep.metaGz).toString("utf8"));
135
138
  const raw = gunzipSync(prep.edgesGz);
136
139
  const edges = new Int32Array(new Uint8Array(raw).buffer);
137
- this.index = new TcbIndex(meta.nodes, edges);
140
+ const index = (this.index = new TcbIndex(meta.nodes, edges));
138
141
  // a baseline kept before versions holds the kernel's hashes (server/versions.ts): one that is
139
142
  // the kernel's hash of the content there now is that content's version
140
143
  const kernel = new Map();
141
144
  for (const v of meta.nodes)
142
145
  if (v.kernel && v.hash)
143
146
  kernel.set(v.name, v.kernel);
144
- if (renamed.size) {
147
+ const ahead = new Map(renamed);
148
+ for (const [to, from] of Object.entries(review.renamed ?? {}))
149
+ if (!ahead.has(from))
150
+ ahead.set(from, to);
151
+ /** a name of a baseline as the graph has it now: renamed on where it is gone (a loop, which a log
152
+ * edited by hand could make, is cut short) */
153
+ const nameNow = (k) => {
154
+ let n = k;
155
+ for (let i = 0; i < 1000 && !index.byName.has(n) && ahead.has(n); i++)
156
+ n = ahead.get(n);
157
+ return index.byName.has(n) ? n : k;
158
+ };
159
+ if (ahead.size) {
145
160
  for (const t of Object.keys(this.baselines)) {
146
161
  const b = this.baselines[t];
147
162
  const now = byName();
148
163
  for (const k of Object.keys(b))
149
- now[renamed.get(k) ?? k] = b[k];
164
+ now[nameNow(k)] = b[k];
150
165
  delete this.baselines[t];
151
- this.baselines[renamed.get(t) ?? t] = now;
166
+ this.baselines[nameNow(t)] = now;
152
167
  }
153
168
  }
154
169
  for (const target of Object.keys(review.targets)) {
@@ -90,7 +90,7 @@ export class Exporter {
90
90
  delete j.notes;
91
91
  return j;
92
92
  };
93
- const known = this.jobs.get(id);
93
+ const known = this.known(id);
94
94
  if (known && known.state !== "failed")
95
95
  return note(known);
96
96
  const kept = this.fromCache(id);
@@ -121,11 +121,22 @@ export class Exporter {
121
121
  job(id) {
122
122
  if (!/^[0-9a-f]{32}$/.test(id))
123
123
  return null;
124
- const j = this.jobs.get(id) ?? this.fromCache(id);
124
+ const j = this.known(id) ?? this.fromCache(id);
125
125
  if (j)
126
126
  this.remember(j);
127
127
  return j;
128
128
  }
129
+ /** A job this server knows, unless it is done and its files have left the cache since (newer
130
+ * documents took their place): that one is forgotten, so that the same document asked for again
131
+ * is typeset again rather than answered with files that are gone. */
132
+ known(id) {
133
+ const j = this.jobs.get(id);
134
+ if (j?.state !== "done" || existsSync(join(this.cacheDir, id, "doc.pdf")))
135
+ return j;
136
+ this.jobs.delete(id);
137
+ this.ends.delete(id);
138
+ return undefined;
139
+ }
129
140
  /** The job once it is over. */
130
141
  wait(id) {
131
142
  return this.ends.get(id) ?? Promise.resolve(this.job(id));
@@ -317,14 +328,16 @@ export class Exporter {
317
328
  return zip(files);
318
329
  }
319
330
  }
320
- const README = `This is a document VisualProof exported, as LaTeX.
331
+ /** The sources' README: how to typeset them again, and the macros a document is written in, each
332
+ * one the style file defines (a test checks it). */
333
+ export const README = `This is a document VisualProof exported, as LaTeX.
321
334
 
322
335
  Typeset it with XeLaTeX, twice (the second run resolves page references and bookmarks):
323
336
 
324
337
  xelatex doc.tex
325
338
  xelatex doc.tex
326
339
 
327
- doc.tex holds the content only, as semantic macros (\\vpPart, \\begin{vpDecl}, \\vpFullName, \\begin{vpCode}, ...).
340
+ doc.tex holds the content only, as semantic macros (\\vpSection, \\begin{vpDecl}, \\vpFullName, \\begin{vpCode}, ...).
328
341
  How it looks is visualproof.sty's: change it, replace it, or load a package of your own after it
329
342
  and \\renewcommand what you want different. Its head lists every macro a document uses.
330
343
 
@@ -12,7 +12,7 @@ import { request as httpRequest } from "node:http";
12
12
  import { request as httpsRequest } from "node:https";
13
13
  import { isIP } from "node:net";
14
14
  import { networkInterfaces } from "node:os";
15
- import { MAX_FILE } from "../shared/library.js";
15
+ import { MAX_FILE, sizeWords } from "../shared/library.js";
16
16
  const DEFAULTS = { maxBytes: MAX_FILE, timeoutMs: 60_000, maxRedirects: 5 };
17
17
  /** the most of a page that is read, and the longest it may take: an HTML page is far smaller than a
18
18
  * file, and what it says of one is not worth a long wait */
@@ -64,13 +64,17 @@ function blockedV4(b) {
64
64
  (a === 172 && c >= 16 && c <= 31) || // private
65
65
  (a === 192 && c === 168) || // private
66
66
  (a === 192 && c === 0 && b[2] === 0) || // IETF protocol assignments
67
+ (a === 192 && c === 0 && b[2] === 2) || // documentation (TEST-NET-1)
68
+ (a === 192 && c === 88 && b[2] === 99) || // 6to4 relays, deprecated
67
69
  (a === 198 && (c === 18 || c === 19)) || // benchmarking
70
+ (a === 198 && c === 51 && b[2] === 100) || // documentation (TEST-NET-2)
71
+ (a === 203 && c === 0 && b[2] === 113) || // documentation (TEST-NET-3)
68
72
  a >= 224 // multicast, reserved, broadcast
69
73
  );
70
74
  }
71
75
  /** Whether the server refuses to fetch from an address: loopback, private, link-local, unique-local,
72
- * multicast, reserved or unspecified, an IPv4 one written as IPv6 (mapped, NAT64) included. Anything
73
- * that is not an address at all is refused too. */
76
+ * multicast, reserved, documentation or unspecified, an IPv4 one written as IPv6 (mapped, NAT64,
77
+ * 6to4) included. Anything that is not an address at all is refused too. */
74
78
  export function blockedAddress(address) {
75
79
  const kind = isIP(address.replace(/%.*$/, ""));
76
80
  if (kind === 4)
@@ -91,6 +95,8 @@ export function blockedAddress(address) {
91
95
  return true; // IPv4-compatible, deprecated
92
96
  if (p[0] === 0x64 && p[1] === 0xff9b && p.slice(2, 6).every((x) => x === 0))
93
97
  return blockedV4(v4(p[6], p[7])); // NAT64
98
+ if (p[0] === 0x2002)
99
+ return blockedV4(v4(p[1], p[2])); // 6to4: the IPv4 address it carries
94
100
  if ((p[0] & 0xfe00) === 0xfc00)
95
101
  return true; // unique local
96
102
  if ((p[0] & 0xffc0) === 0xfe80)
@@ -211,7 +217,7 @@ async function guardedFetch(raw, kind, o, allowed) {
211
217
  return { ok: false, status: 400, error: `${host} cannot be found.` };
212
218
  const refused = addresses.find((a) => !allowed(a.address));
213
219
  if (refused)
214
- return { ok: false, status: 403, error: `${host} is ${refused.address}, an address of a private or local network or of this server: the server does not fetch from there.` };
220
+ return { ok: false, status: 403, error: `${host === refused.address ? host : `${host} is ${refused.address}, which`} is an address of a private or local network or of this server: the server does not fetch from there.` };
215
221
  const left = deadline - Date.now();
216
222
  if (left <= 0)
217
223
  return { ok: false, status: 504, error: "The address took too long to answer." };
@@ -224,8 +230,11 @@ async function guardedFetch(raw, kind, o, allowed) {
224
230
  return { ok: false, status: 502, error: `The address could not be fetched: ${e instanceof Error ? e.message : String(e)}.` };
225
231
  }
226
232
  const status = res.statusCode ?? 0;
233
+ // what is not the file is never read: a redirect's or an error's body, drained, could run on
234
+ // past the bytes and the time a fetch may take, the fetch answered long since
235
+ if (status !== 200)
236
+ res.destroy();
227
237
  if ([301, 302, 303, 307, 308].includes(status)) {
228
- res.resume();
229
238
  const to = res.headers.location;
230
239
  if (!to)
231
240
  return { ok: false, status: 502, error: `The address answered ${status} without saying where to.` };
@@ -239,26 +248,28 @@ async function guardedFetch(raw, kind, o, allowed) {
239
248
  }
240
249
  continue;
241
250
  }
242
- if (status !== 200) {
243
- res.resume();
251
+ if (status !== 200)
244
252
  return { ok: false, status: 502, error: `The address answered ${status}, not the ${noun}.` };
245
- }
246
253
  const length = Number(res.headers["content-length"] ?? NaN);
247
254
  if (Number.isFinite(length) && length > o.maxBytes) {
248
255
  res.destroy();
249
- return { ok: false, status: 413, error: `The ${noun} is ${Math.round(length / 1e6)} MB: ${most} is at most ${Math.round(o.maxBytes / 1e6)} MB.` };
256
+ return { ok: false, status: 413, error: `The ${noun} is ${sizeWords(length)}: ${most} is at most ${sizeWords(o.maxBytes)}.` };
250
257
  }
251
258
  const body = await readCapped(res, o.maxBytes, deadline);
252
259
  if (typeof body === "string")
253
- return { ok: false, status: body === "large" ? 413 : 504, error: body === "large" ? `The ${noun} is larger than ${Math.round(o.maxBytes / 1e6)} MB, the most ${most} may be.` : `The address took too long to send the ${noun}.` };
260
+ return { ok: false, status: body === "large" ? 413 : 504, error: body === "large" ? `The ${noun} is larger than ${sizeWords(o.maxBytes)}, the most ${most} may be.` : `The address took too long to send the ${noun}.` };
254
261
  const disposition = res.headers["content-disposition"];
255
262
  return { ok: true, bytes: body, url, disposition };
256
263
  }
257
264
  }
258
- /** A request to `url`, connected to the address checked for it and to no other; its answer's head. */
265
+ /** A request to `url`, connected to the address checked for it and to no other; its answer's head,
266
+ * within `ms` in all. */
259
267
  function connect(url, at, ms, accept) {
260
268
  return new Promise((ok, no) => {
261
269
  const make = url.protocol === "https:" ? httpsRequest : httpRequest;
270
+ // the socket's `timeout` is idleness alone: a head sent a byte at a time is never idle, and
271
+ // would hold the fetch past its time for as long as its sender likes
272
+ const late = setTimeout(() => req.destroy(new Error("no answer in time")), ms);
262
273
  const req = make(url, {
263
274
  method: "GET",
264
275
  headers: { "user-agent": "VisualProof (adds a file to a project's library)", accept },
@@ -271,9 +282,15 @@ function connect(url, at, ms, accept) {
271
282
  cb(null, at.address, at.family);
272
283
  }),
273
284
  timeout: ms,
274
- }, ok);
285
+ }, (res) => {
286
+ clearTimeout(late);
287
+ ok(res);
288
+ });
275
289
  req.on("timeout", () => req.destroy(new Error("no answer in time")));
276
- req.on("error", no);
290
+ req.on("error", (e) => {
291
+ clearTimeout(late);
292
+ no(e);
293
+ });
277
294
  req.end();
278
295
  });
279
296
  }
@@ -32,6 +32,7 @@ import { childEnv } from "./children.js";
32
32
  import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
33
33
  import { join } from "node:path";
34
34
  import { gunzipSync, gzipSync } from "node:zlib";
35
+ import { endsLine } from "./lostfound.js";
35
36
  import { SourceStore, summarize } from "./sourcestore.js";
36
37
  import { ownName, refIdentity, versionOf } from "./versions.js";
37
38
  const sha = (s) => createHash("sha256").update(s).digest("hex").slice(0, 32);
@@ -493,7 +494,8 @@ export class History {
493
494
  }
494
495
  save(info) {
495
496
  this.infos.set(info.id, info);
496
- appendFileSync(this.indexFile, JSON.stringify(info) + "\n");
497
+ // after a line cut short, on a line of its own: the line cut short alone is lost
498
+ appendFileSync(this.indexFile, (endsLine(this.indexFile) ? "" : "\n") + JSON.stringify(info) + "\n");
497
499
  }
498
500
  readGz(file) {
499
501
  return JSON.parse(gunzipSync(readFileSync(file)).toString("utf8"));
@@ -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)
@@ -11,6 +11,7 @@
11
11
  // written in, the exact contexts — come as the next version of the graph. Where every module's
12
12
  // result is cached, the merged graph is the first one served.
13
13
  import { randomBytes } from "node:crypto";
14
+ import { sameName } from "../shared/collab.js";
14
15
  import { prepare } from "./pipeline.js";
15
16
  import { extract, loadSaved } from "./extract.js";
16
17
  import { DEFAULT_JOBS, UsagePass, mergeUsage, saveMerged } from "./usage.js";
@@ -928,6 +929,12 @@ export class Live {
928
929
  const r = this.invites.holder(name);
929
930
  return r ? { id: r.id, name: r.name } : undefined;
930
931
  }
932
+ /** Whether `name` is an agent's now (sameName): a token in force has it, or an agent on the page
933
+ * gives it — nobody's opinion is recorded for an agent, even one that has written nothing yet
934
+ * (ReviewSpec decision 8, AgentSpec decision 2). */
935
+ agentNamed(name) {
936
+ return this.tokens.inForce().some((r) => sameName(r.name, name)) || this.presence.list().some((p) => p.agent && p.name !== null && sameName(p.name, name));
937
+ }
931
938
  /** The usage pass's contexts of a module (shared/context.ts), when the graph on show has them. */
932
939
  contextsOf(module) {
933
940
  return this.graph?.prep.usage?.contexts[module] ?? null;
@@ -1056,7 +1063,7 @@ export class Live {
1056
1063
  connection: "keep-alive",
1057
1064
  });
1058
1065
  res.write("retry: 2000\n\n");
1059
- const st = { res, id: randomBytes(9).toString("base64url"), token: randomBytes(24).toString("base64url"), session, private: false, person, readOnly, toldAll };
1066
+ const st = { res, id: randomBytes(9).toString("base64url"), token: randomBytes(24).toString("base64url"), session, private: false, person, readOnly, toldAll, room: Infinity, after: 0 };
1060
1067
  this.streams.set(res, st);
1061
1068
  this.byToken.set(st.token, st);
1062
1069
  this.presence.open(st.id);
@@ -1073,7 +1080,7 @@ export class Live {
1073
1080
  if (this.build)
1074
1081
  this.write(res, "build", this.build);
1075
1082
  // the whole review once; what changes after comes as deltas
1076
- this.write(res, "review", this.review.whole());
1083
+ this.push(st, this.reviewHello());
1077
1084
  const g = this.graphInfo();
1078
1085
  if (g)
1079
1086
  this.write(res, "graph", g);
@@ -1085,6 +1092,18 @@ export class Live {
1085
1092
  this.write(res, "lost", lostNews(this.lostSent));
1086
1093
  else
1087
1094
  this.lostChanged();
1095
+ st.room = MAX_BUFFERED + res.writableLength;
1096
+ st.after = 0;
1097
+ }
1098
+ /** the whole review as a stream's hello says it, in bytes, and the text it was made from */
1099
+ reviewBytes = null;
1100
+ /** The whole review as every stream's hello says it: made into bytes once a revision, not once a
1101
+ * stream (a hundred pages opening at once, a review of 7.6 MB, had the server 5 s encoding it). */
1102
+ reviewHello() {
1103
+ const text = this.review.wholeText();
1104
+ if (this.reviewBytes?.text !== text)
1105
+ this.reviewBytes = { text, bytes: Buffer.from(`event: review\ndata: ${text}\n\n`) };
1106
+ return this.reviewBytes.bytes;
1088
1107
  }
1089
1108
  write(res, event, data) {
1090
1109
  const st = this.streams.get(res);
@@ -1100,15 +1119,23 @@ export class Live {
1100
1119
  for (const st of [...this.streams.values()])
1101
1120
  this.push(st, text);
1102
1121
  }
1103
- /** Write to a stream, unless what it has not read yet passed MAX_BUFFERED: then it is closed
1104
- * instead, and its page reconnects to the present state. */
1122
+ /** Write to a stream, unless what it has not read yet passed MAX_BUFFERED beyond its hello (`room`):
1123
+ * then it is closed instead, and its page reconnects to the present state. A review of 5,000
1124
+ * opinions on Sphere-Packing-Lean is 7 MB: counted against MAX_BUFFERED, it closed every page's
1125
+ * stream as it opened, and the pages reconnected every two seconds, never live. */
1105
1126
  push(st, text) {
1106
- if (st.res.writableLength > MAX_BUFFERED) {
1127
+ const waiting = st.res.writableLength;
1128
+ // all that waits was written since the hello: the hello is read
1129
+ if (st.room > MAX_BUFFERED && st.room !== Infinity && waiting <= st.after)
1130
+ st.room = MAX_BUFFERED;
1131
+ if (waiting > st.room) {
1107
1132
  this.dropped++;
1108
1133
  this.close(st);
1109
1134
  return;
1110
1135
  }
1111
1136
  st.res.write(text);
1137
+ if (st.room > MAX_BUFFERED)
1138
+ st.after += text.length;
1112
1139
  }
1113
1140
  /** A stream is over: its page leaves the list, its secret means nothing any more. */
1114
1141
  /** How many read-only readers' streams are open. */
@@ -72,8 +72,9 @@ export class Archive {
72
72
  return lines;
73
73
  }
74
74
  }
75
- /** Whether a file is empty, missing, or ends with a line break. */
76
- function endsLine(file) {
75
+ /** Whether a file is empty, missing, or ends with a line break: what is appended to one that does not
76
+ * (a line cut short as the server stopped) starts a line of its own (server/renames.ts too). */
77
+ export function endsLine(file) {
77
78
  let fd;
78
79
  try {
79
80
  fd = openSync(file, "r");