GitHubivarvong
ply
main
1// Fail-site recorder (gate c). Preloaded with `--import ./test/space/failsite-hook.mjs` (through NODE_OPTIONS, so every
2// process and worker thread the suite starts loads it too) when PLY_FAILSITES names a directory; a no-op otherwise.
3// It never touches src/: src/errors.ts is rewritten as it is loaded (test/space/failsite-instrument.mjs), here by a
4// module-load hook for code node loads itself, and by the vitest plugin in vitest.config.ts for code vitest transforms.
5//
6// Why a load hook and not a branch in plyError: a `globalThis` check in errors.ts would ship in every bundle and run on
7// every error; V8 coverage would also work but says only that a range ran, not through which call chain, so a wrapper
8// (`badTree(why)`, `take(..., code)`) would hide the decision that called it. A stack names every call site on the way,
9// so the wrapper's caller is recorded as the site it is.
10//
11// Reached is not judged: a test that triggers an error and asserts nothing reaches the site too. So the recorder also
12// notes which errors were looked at: the error plyError returns gets its `code` served through an accessor (same
13// value, same enumerability), and the first read made by code outside src/ (a test, a helper, node's assert) marks
14// every src/ frame of that error's stack as observed. ply reading its own error (a classifier) does not count. Only
15// `code`: `message` stays the data property it is, because a structured clone (postMessage from a worker) copies an
16// Error's message only from a data property; `code` is not cloned either way. An error whose frames are all observed already is left as plyError built it, so a space
17// that makes the same error a million times pays for the first one only.
18//
19// Records: <dir>/<pid>-<thread>-<runner>.json = { runner, platform, frames: { "src/x.ts:L:C": n }, observed: [frame] },
20// one count per src/ frame of every recorded stack, rewritten whenever the thread reaches a frame it had not reached (a vitest fork or a
21// worker thread may be stopped without an exit event) and at exit. The runner is the test file: the vitest setup file
22// (test/space/failsite-setup.js) names it and exports it as PLY_FAILSITE_RUNNER to the workers and children it starts;
23// a plain node process is its own script. test/space/holes.js maps frames to inventory sites.
24import { registerHooks } from 'node:module';
25import { writeFileSync, mkdirSync, renameSync } from 'node:fs';
26import { join, relative, isAbsolute } from 'node:path';
27import { fileURLToPath } from 'node:url';
28import { isMainThread, threadId } from 'node:worker_threads';
29import { ROOT, ERRORS_URL, KEY, instrument } from './failsite-instrument.mjs';
31const DIR = process.env.PLY_FAILSITES;
33if (DIR && !globalThis[KEY]) {
34 const out = isAbsolute(DIR) ? DIR : join(ROOT, DIR);
35 mkdirSync(out, { recursive: true });
36 const SRC = `${join(ROOT, 'src')}/`;
37 const byRunner = new Map(); // runner -> Map(frame -> n)
38 const observedBy = new Map(); // runner -> Set(frame): frames of an error whose code or message was read outside src/
39 const FRAME = /\(?((?:file:\/\/)?\/[^\s()]+):(\d+):(\d+)\)?$/;
40 const fileOf = m => (m[1].startsWith('file://') ? fileURLToPath(m[1]) : m[1]);
41 let pending = null; // the stack recorded at the head of the plyError call that is running: { r, keys }
42 let named = null;
43 const runner = () => named ?? process.env.PLY_FAILSITE_RUNNER ?? (process.argv[1] ? relative(ROOT, process.argv[1]) : '(eval)');
44 const thread = isMainThread ? 'm' : `w${threadId}`;
45 // written to a temporary name and renamed (atomic on one filesystem): a process killed mid-write (a test's timeout,
46 // a worker terminated) leaves its previous record or none, never a truncated one the gate cannot parse
47 const save = r => {
48 const file = join(out, `${process.pid}-${thread}-${r.replace(/[^\w.-]+/g, '_')}.json`);
49 writeFileSync(`${file}.tmp`, JSON.stringify({ runner: r, platform: process.platform, frames: Object.fromEntries(byRunner.get(r) ?? []), observed: [...(observedBy.get(r) ?? [])] }));
50 renameSync(`${file}.tmp`, file);
51 };
52 const recorder = holder => {
53 const limit = Error.stackTraceLimit;
54 Error.stackTraceLimit = 40;
55 const o = {};
56 Error.captureStackTrace(o, holder);
57 Error.stackTraceLimit = limit;
58 const r = runner();
59 if (!byRunner.has(r)) byRunner.set(r, new Map());
60 const frames = byRunner.get(r);
61 const seen = new Set();
62 let fresh = false;
63 // node prints file:// URLs; vitest's source-mapped stacks print paths
64 for (const line of o.stack.split('\n').slice(1)) {
65 const m = FRAME.exec(line);
66 if (!m) continue;
67 const file = fileOf(m);
68 if (!file.startsWith(SRC)) continue;
69 const key = `${relative(ROOT, file)}:${m[2]}:${m[3]}`;
70 if (seen.has(key)) continue;
71 seen.add(key);
72 if (!frames.has(key)) fresh = true;
73 frames.set(key, (frames.get(key) ?? 0) + 1);
74 }
75 pending = { r, keys: [...seen] };
76 if (fresh) save(r);
77 };
78 // the error that plyError call built. Its code is read through an accessor until code outside src/ reads it; the
79 // reader is the frame that called the accessor (one frame, captured only while still unobserved)
80 recorder.made = e => {
81 const p = pending;
82 pending = null;
83 if (!p || !p.keys.length) return e;
84 if (!observedBy.has(p.r)) observedBy.set(p.r, new Set());
85 const observed = observedBy.get(p.r);
86 if (p.keys.every(k => observed.has(k))) return e;
87 let open = true;
88 const read = getter => {
89 if (!open) return;
90 const limit = Error.stackTraceLimit;
91 Error.stackTraceLimit = 1;
92 const o = {};
93 Error.captureStackTrace(o, getter);
94 Error.stackTraceLimit = limit;
95 const m = FRAME.exec(o.stack.split('\n')[1] ?? '');
96 if (m && fileOf(m).startsWith(SRC)) return;
97 open = false;
98 let fresh = false;
99 for (const k of p.keys) if (!observed.has(k)) { observed.add(k); fresh = true; }
100 if (fresh) save(p.r);
101 };
102 for (const key of ['code']) {
103 const d = Object.getOwnPropertyDescriptor(e, key);
104 if (!d || !d.configurable || !('value' in d)) continue;
105 let value = d.value;
106 const get = () => { read(get); return value; };
107 Object.defineProperty(e, key, { enumerable: d.enumerable, configurable: true, get, set: v => { value = v; } });
108 }
109 return e;
110 };
111 // the vitest setup file names the test file this process (or thread) is running; written at once, even with no
112 // frames: the record also says which runner ran on which platform (the Linux gate)
113 recorder.runner = r => { named = r; if (!byRunner.has(r)) byRunner.set(r, new Map()); save(r); };
114 globalThis[KEY] = recorder;
115 process.on('exit', () => {
116 // a process that recorded nothing is still a record (it ran), unless it is a tool: npx, vitest's own process
117 const r = runner();
118 if (!byRunner.size && !r.startsWith('..') && !r.startsWith('node_modules')) byRunner.set(r, new Map());
119 for (const k of byRunner.keys()) save(k);
120 });
121 registerHooks({
122 load(url, context, next) {
123 const r = next(url, context);
124 if (url !== ERRORS_URL) return r;
125 const source = typeof r.source === 'string' ? r.source : new TextDecoder().decode(r.source);
126 return { ...r, source: instrument(source) };
127 },
128 });
129}
Diagnostics7 git calls · 212 ms
CallStatusSizeTime0212 ms
discover200191 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=919 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetchtree:02001.4 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1428 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=74d7bb09987bbe78a9c0ea8f43a1b5a4e079f538 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1428 git.pkt.payload.max=1401 git.sideband.band1.bytes=1401 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1401 git.pack.trailer=65ea1a3aeef333a80a2dfa3f441e629d90497e8eOpen pack map
fetch2003.0 KiB200 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=200 git.response.bytes=3094 git.response.cache=MISS git.response.duration_ms=200 git.fetch.wants=199c6d62f4a512fbf2bbd38e94e38f44a6a15d5b git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3094 git.pkt.payload.max=3067 git.sideband.band1.bytes=3067 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e1a59bcb1b5e44
- Request
- 2b00f185…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format