muse-crew 0.14.5 → 0.14.6

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.
@@ -1,462 +0,0 @@
1
- #!/usr/bin/env node
2
- // classify-publish-absence.js — deterministic classifier for publish attempts
3
- // parked with an UNKNOWN outcome (blocker 15, Phase 1, 2026-09-18).
4
- //
5
- // The workflow's fire-and-forget trigger returns no receipt, so a parked
6
- // unknown means "the edit may or may not have gone through and the build
7
- // may or may not have run." This unit decides mechanically from durable
8
- // signals only — never from timing windows or audit-dir counting:
9
- //
10
- // sensor findings (lib/readback-disk.js — the on-disk source read-back)
11
- // × manifest (.space-build/manifest.json — built_at + content_sha256)
12
- // × repo state (git diff base..commit, HEAD)
13
- //
14
- // Decisions (design-room19.md §1.4):
15
- // verified — manifest built_at advanced past the trigger AND
16
- // every discriminating added line PRESENT / removed
17
- // ABSENT (the build landed; route to parent
18
- // verification, which stamps).
19
- // provably-dropped — (a) every discriminating added line ABSENT,
20
- // (b) every discriminating removed line PRESENT,
21
- // (c) manifest built_at NOT advanced past trigger,
22
- // (d) no build in flight (checked twice, bracketing
23
- // the content read), (e) decision time ≥ park +
24
- // quiesce, (f) HEAD == commit. One safe retry.
25
- // applied-not-built — source PRESENT but manifest stale: the platform
26
- // started something but no build completed.
27
- // → ambiguous, park with the diagnosis, NO retry
28
- // (re-applying a possibly mid-apply diff is the
29
- // 2026-09-12 hazard in a new coat).
30
- // ambiguous — partial-apply, binary/mode-only/rename diffs,
31
- // unreadable sensor, manifest-advanced-but-mismatched,
32
- // zero discriminating lines. Terminal, no retry.
33
- // deferred — trigger age < quiesce, or build in flight. No
34
- // budget consumed. Still in flight at park+2h → the
35
- // caller treats it as terminal (pathological).
36
- // superseded — HEAD != commit. Terminal, no retry (a retry would
37
- // regress a newer publish).
38
- //
39
- // "Discriminating" reuses lib/publish-content.js — the verifier's own
40
- // collision-exemption logic. Lines already in the old tree are
41
- // non-discriminating and ignored.
42
- //
43
- // Usage:
44
- // node classify-publish-absence.js --repo-path <path> --commit <sha>
45
- // --base <sha> --slug <slug> --trigger-ts <ISO> --park-ts <ISO>
46
- // [--spaces-root <dir>] [--task-id <uuid>]
47
- // [--quiesce-mins <n>] [--in-flight-window-mins <n>]
48
- //
49
- // --manifest-before is the workflow's pre-trigger manifest snapshot
50
- // (JSON {"built_at":<ISO|null>,"content_sha256":<hex|null>}, design §1.9).
51
- // The verified path requires the current manifest's content_sha256 to
52
- // differ from the baseline (a new build identity, not a replayed
53
- // manifest). When the baseline is unavailable the classifier falls back
54
- // to the time-based advance check and notes it; the Step 4.5 verifier
55
- // (lib/verify-publish.js) is the strict gate and fails closed without a
56
- // baseline before stamping.
57
- //
58
- // Exit 0 with the decision JSON on stdout for every decision (a decision
59
- // is data, not a failure). Exit 2 on usage/validation. Nothing is printed
60
- // to stdout on failure.
61
-
62
- import { execFileSync } from "node:child_process";
63
- import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
64
- import { homedir } from "node:os";
65
- import { join, resolve } from "node:path";
66
- import {
67
- EMPTY_TREE,
68
- parseDiff,
69
- parseFindings,
70
- makeOldCounter,
71
- discriminatingLines,
72
- } from "./publish-content.js";
73
-
74
- // --help: before required-arg parsing (shebang⇔CLI contract).
75
- if (process.argv.slice(2).includes("--help")) {
76
- console.log(
77
- "usage: node classify-publish-absence.js --repo-path <path> --commit <sha>\n" +
78
- " --base <sha> --slug <slug> --trigger-ts <ISO> --park-ts <ISO>\n" +
79
- " [--spaces-root <dir>] [--task-id <uuid>] [--quiesce-mins <n>] [--in-flight-window-mins <n>] [--manifest-before <json>]\n" +
80
- "six-way classifier for publish attempts parked with an UNKNOWN outcome; prints the decision JSON on stdout"
81
- );
82
- process.exit(0);
83
- }
84
-
85
- function arg(name, required = true, def = null) {
86
- const i = process.argv.lastIndexOf(name); // last occurrence wins
87
- if (i < 0 || i + 1 >= process.argv.length) {
88
- if (required && def === null) fail("usage", `${name} is required.`);
89
- return def;
90
- }
91
- return process.argv[i + 1];
92
- }
93
- function fail(code, message) {
94
- process.stderr.write(JSON.stringify({ ok: false, error: code, message }) + "\n");
95
- process.exit(2);
96
- }
97
-
98
- const repoPath = arg("--repo-path");
99
- const commit = arg("--commit");
100
- const base = arg("--base");
101
- const slug = arg("--slug");
102
- const triggerTs = arg("--trigger-ts");
103
- const parkTs = arg("--park-ts");
104
- const spacesRoot = arg("--spaces-root", false, join(homedir(), "workspace", "ts-spaces"));
105
- const taskId = arg("--task-id", false, "classify");
106
- const quiesceMins = parseInt(arg("--quiesce-mins", false, "30"), 10);
107
- const inFlightWindowMins = parseInt(arg("--in-flight-window-mins", false, "15"), 10);
108
- // Pre-trigger manifest baseline (design §1.9), JSON-encoded. Optional —
109
- // the classifier falls back to the time-based check when absent.
110
- // 2026-09-18, should-fix 1: distinguish absent (legacy, no baseline) from
111
- // malformed (supplied but invalid) — malformed is ambiguous, not absent.
112
- // A valid baseline requires a parseable built_at timestamp and a 64-hex
113
- // content_sha256.
114
- let manifestBefore = null;
115
- let manifestBeforeMalformed = false;
116
- const manifestBeforeRaw = arg("--manifest-before", false, null);
117
- if (manifestBeforeRaw) {
118
- try {
119
- const parsed = JSON.parse(manifestBeforeRaw);
120
- const builtAt = parsed && typeof parsed.built_at === "string" ? parsed.built_at : null;
121
- const sha = parsed && typeof parsed.content_sha256 === "string" ? parsed.content_sha256 : null;
122
- const builtAtMs = builtAt ? Date.parse(builtAt) : NaN;
123
- const shaOk = sha && /^[0-9a-f]{64}$/.test(sha);
124
- if (parsed && typeof parsed === "object" && builtAtMs > 0 && shaOk) {
125
- manifestBefore = { built_at: builtAt, content_sha256: sha };
126
- } else {
127
- // Supplied but invalid: fail closed as ambiguous, not absent.
128
- manifestBeforeMalformed = true;
129
- }
130
- } catch {
131
- // Supplied but unparsable: fail closed as ambiguous, not absent.
132
- manifestBeforeMalformed = true;
133
- }
134
- }
135
-
136
- if (!/^[0-9a-f]{40}$/.test(commit)) fail("usage", "commit must be a 40-char hex sha.");
137
- if (!/^[0-9a-f]{40}$/.test(base)) fail("usage", "base must be a 40-char hex sha.");
138
- if (!/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(slug)) fail("usage", "slug is not a safe artifact slug.");
139
- const triggerMs = Date.parse(triggerTs);
140
- const parkMs = Date.parse(parkTs);
141
- if (!(triggerMs > 0)) fail("usage", "trigger-ts is not a valid timestamp.");
142
- if (!(parkMs > 0)) fail("usage", "park-ts is not a valid timestamp.");
143
- if (!(quiesceMins > 0)) fail("usage", "quiesce-mins must be positive.");
144
- if (!(inFlightWindowMins > 0)) fail("usage", "in-flight-window-mins must be positive.");
145
-
146
- function decide(decision, reasons, details = {}) {
147
- process.stdout.write(JSON.stringify({ ok: true, decision, reasons, details }) + "\n");
148
- process.exit(0);
149
- }
150
-
151
- // Resolve the space dir with realpath containment (same rule as
152
- // readback-disk.js — the slug must not escape the spaces root).
153
- function resolveSpaceDir() {
154
- try {
155
- const rootReal = realpathSync(spacesRoot);
156
- const candReal = realpathSync(join(spacesRoot, slug));
157
- if (candReal !== rootReal && !candReal.startsWith(rootReal + "/")) return null;
158
- return statSync(candReal).isDirectory() ? candReal : null;
159
- } catch {
160
- return null;
161
- }
162
- }
163
-
164
- // (d) No build in flight. Signals, both heuristic but conservative:
165
- // - manifest built_at within the window: a build just landed, the
166
- // deployment may still be propagating;
167
- // - any file under .space-build with mtime within the window: the
168
- // platform is actively writing build output.
169
- // A build running longer than the window without touching .space-build is
170
- // pathological; the caller escalates to terminal at park+2h.
171
- // (d) Build-in-flight detection: the manifest is the platform's build
172
- // record. A build is "in flight" for our purposes if the manifest state
173
- // changes during the content read (a build landed mid-read — the read may
174
- // be torn) or if the manifest's built_at is within the settle window (a
175
- // build just completed; the working copy may still be settling). Checked
176
- // twice bracketing the content read (design §1.4d); the double check
177
- // bounds the residual window to the local disk-read duration. Directory
178
- // mtimes are NOT used — they cannot distinguish a running build from a
179
- // recently completed one, and the manifest is the honest record.
180
- function manifestState(spaceDir) {
181
- const m = readManifest(spaceDir);
182
- if (!m) return null;
183
- return { built_at: m.built_at, content_sha256: m.content_sha256 };
184
- }
185
- function buildInFlight(spaceDir, nowMs, beforeState) {
186
- const afterState = manifestState(spaceDir);
187
- // State changed during the read → a build landed mid-read.
188
- if (beforeState && afterState &&
189
- (beforeState.built_at !== afterState.built_at ||
190
- beforeState.content_sha256 !== afterState.content_sha256)) {
191
- return { inFlight: true, afterState, reason: `manifest changed during the content read (before built_at=${beforeState.built_at}, after built_at=${afterState.built_at}) — the read may be torn` };
192
- }
193
- // Manifest built very recently → a build just completed; let it settle.
194
- // 2026-09-18, should-fix 2: enforce 0 <= age < settleMs. A future built_at
195
- // (negative age) is pathological — clock skew or corruption — and must not
196
- // defer forever. It is ambiguous, not in-flight.
197
- const settleMs = inFlightWindowMins * 60 * 1000;
198
- const builtAtMs = afterState && afterState.built_at ? Date.parse(afterState.built_at) : NaN;
199
- if (Number.isFinite(builtAtMs)) {
200
- const ageMs = nowMs - builtAtMs;
201
- if (ageMs < 0) {
202
- return { inFlight: false, afterState, pathological: true, reason: `manifest built_at ${afterState.built_at} is in the future (age ${ageMs}ms) — pathological, not in-flight` };
203
- }
204
- if (ageMs < settleMs) {
205
- return { inFlight: true, afterState, reason: `manifest built_at ${afterState.built_at} is within the ${inFlightWindowMins}min settle window — a build just completed` };
206
- }
207
- }
208
- return { inFlight: false, afterState };
209
- }
210
-
211
- function readManifest(spaceDir) {
212
- if (!spaceDir) return null;
213
- try {
214
- const m = JSON.parse(readFileSync(join(spaceDir, ".space-build", "manifest.json"), "utf8"));
215
- return {
216
- built_at: typeof m.built_at === "string" ? m.built_at : null,
217
- content_sha256: typeof m.content_sha256 === "string" ? m.content_sha256 : null,
218
- };
219
- } catch {
220
- return null;
221
- }
222
- }
223
-
224
- const nowMs = Date.now();
225
- const spaceDir = resolveSpaceDir();
226
-
227
- // (should-fix 1) Malformed supplied baseline → ambiguous, not absent.
228
- // A supplied --manifest-before that fails validation is evidence corruption,
229
- // not a legacy absent baseline. Fail closed.
230
- if (manifestBeforeMalformed) {
231
- decide("ambiguous",
232
- [`supplied --manifest-before failed validation (must be JSON with a parseable built_at and a 64-hex content_sha256)`],
233
- { manifest_before_malformed: true });
234
- }
235
-
236
- // (e) Decision time ≥ park + quiesce. Too young → deferred, no budget
237
- // consumed, no timer advanced.
238
- if (nowMs < parkMs + quiesceMins * 60 * 1000) {
239
- decide("deferred",
240
- [`trigger age below quiesce: park ${parkTs}, quiesce ${quiesceMins}min`],
241
- { park_ts: parkTs, quiesce_mins: quiesceMins });
242
- }
243
-
244
- // (d) First in-flight check, before the content read. Captures the
245
- // manifest state; the second check compares against it.
246
- const manifestBeforeRead = manifestState(spaceDir);
247
- let flight = buildInFlight(spaceDir, nowMs, null);
248
- // Pathological future timestamp → ambiguous, not deferred (should-fix 2).
249
- if (flight.pathological) {
250
- decide("ambiguous",
251
- [`manifest built_at is in the future (pathological): ${flight.reason}`],
252
- { pathological_future_built_at: true, in_flight_reason: flight.reason });
253
- }
254
- if (flight.inFlight) {
255
- if (nowMs > parkMs + 2 * 60 * 60 * 1000) {
256
- decide("ambiguous",
257
- [`build still in flight at park+2h (pathological): ${flight.reason}`],
258
- { in_flight_reason: flight.reason });
259
- }
260
- decide("deferred", [`build in flight (pre-read check): ${flight.reason}`],
261
- { in_flight_reason: flight.reason });
262
- }
263
-
264
- // The content read: the deterministic sensor on the platform's working copy.
265
- const sensorPath = join(new URL(".", import.meta.url).pathname, "readback-disk.js");
266
- let findingsText;
267
- try {
268
- findingsText = execFileSync("node", [
269
- sensorPath,
270
- "--repo-path", repoPath,
271
- "--commit", commit,
272
- "--base", base,
273
- "--slug", slug,
274
- "--task-id", taskId,
275
- "--spaces-root", spacesRoot,
276
- ], { encoding: "utf8", maxBuffer: 8 * 1024 * 1024 });
277
- } catch (e) {
278
- decide("ambiguous",
279
- [`sensor unreadable: ${(e.stderr || e.message || "").toString().slice(0, 200)}`],
280
- { sensor_failed: true });
281
- }
282
-
283
- // (d) Second in-flight check, bracketing the content read. Compares the
284
- // manifest state against the pre-read baseline; a change means a build
285
- // landed mid-read (the read may be torn). The double check bounds the
286
- // residual window to the local disk-read duration.
287
- flight = buildInFlight(spaceDir, Date.now(), manifestBeforeRead);
288
- // Pathological future timestamp → ambiguous, not deferred (should-fix 2).
289
- if (flight.pathological) {
290
- decide("ambiguous",
291
- [`manifest built_at is in the future (pathological, post-read): ${flight.reason}`],
292
- { pathological_future_built_at: true, in_flight_reason: flight.reason });
293
- }
294
- if (flight.inFlight) {
295
- decide("deferred", [`build in flight (post-read check): ${flight.reason}`],
296
- { in_flight_reason: flight.reason });
297
- }
298
-
299
- // 2026-09-18, should-fix 3: use the manifest state already checked by the
300
- // post-read in-flight check (flight.afterState) — do NOT re-read the manifest
301
- // here. A third read reopens the TOCTOU window the double-check just closed.
302
- const manifest = flight.afterState;
303
- const manifestBuiltAtMs = manifest && manifest.built_at ? Date.parse(manifest.built_at) : NaN;
304
- const manifestAdvanced = Number.isFinite(manifestBuiltAtMs) && manifestBuiltAtMs > triggerMs;
305
-
306
- // The expected change, from git (never from anyone's report).
307
- if (base !== EMPTY_TREE) {
308
- try {
309
- execFileSync("git", ["-C", repoPath, "merge-base", "--is-ancestor", base, commit], { stdio: "ignore" });
310
- } catch {
311
- decide("ambiguous", [`base ${base} is not an ancestor of commit ${commit}`], {});
312
- }
313
- }
314
- let diff;
315
- try {
316
- diff = execFileSync("git", ["-C", repoPath, "diff", base, commit, "--"], {
317
- encoding: "utf8", maxBuffer: 4 * 1024 * 1024,
318
- });
319
- } catch (e) {
320
- decide("ambiguous", [`git diff failed: ${e.message.slice(0, 200)}`], {});
321
- }
322
- if (!diff.trim()) {
323
- decide("ambiguous", ["empty diff for commit — nothing to judge"], {});
324
- }
325
- const diffMap = parseDiff(diff);
326
- const oldCount = makeOldCounter(repoPath, base);
327
- const disc = discriminatingLines(diffMap, oldCount);
328
-
329
- // Files the line-based judge cannot certify at all → ambiguous, never a
330
- // retry on zero signal.
331
- for (const [path, d] of disc) {
332
- if (d.unjudgeable) {
333
- decide("ambiguous",
334
- [`unverifiable-content ${path}: ${d.unjudgeable} — the diff carries no discriminating lines for this file`],
335
- { file: path, unjudgeable: d.unjudgeable });
336
- }
337
- }
338
-
339
- // (f) Supersession re-check: a retry against a moved HEAD would regress a
340
- // newer publish.
341
- let head;
342
- try {
343
- head = execFileSync("git", ["-C", repoPath, "rev-parse", "HEAD"], { encoding: "utf8" }).trim();
344
- } catch (e) {
345
- decide("ambiguous", [`git rev-parse HEAD failed: ${e.message.slice(0, 200)}`], {});
346
- }
347
- if (head !== commit) {
348
- decide("superseded",
349
- [`HEAD is ${head}, not ${commit} — a newer publish supersedes this one; no retry`],
350
- { head, commit });
351
- }
352
-
353
- // The findings must cover every discriminating line, or the read is not a
354
- // read of this diff.
355
- const { findings, malformed } = parseFindings(findingsText);
356
- if (malformed) {
357
- decide("ambiguous", [`unreadable findings: ${malformed}`], {});
358
- }
359
- let totalDisc = 0;
360
- let addedPresent = 0, addedAbsent = 0, removedPresent = 0, removedAbsent = 0;
361
- for (const [path, d] of disc) {
362
- const found = findings.get(path);
363
- if (!found) {
364
- decide("ambiguous", [`no findings for changed file ${path}`], { file: path });
365
- }
366
- for (const line of d.addedDisc) {
367
- totalDisc++;
368
- const v = found.added.get(line);
369
- if (v === undefined) decide("ambiguous", [`no ADDED finding for line in ${path}`], { file: path });
370
- if (v === "PRESENT") addedPresent++; else addedAbsent++;
371
- }
372
- for (const line of d.removedDisc) {
373
- totalDisc++;
374
- const v = found.removed.get(line);
375
- if (v === undefined) decide("ambiguous", [`no REMOVED finding for line in ${path}`], { file: path });
376
- if (v === "PRESENT") removedPresent++; else removedAbsent++;
377
- }
378
- }
379
- if (totalDisc === 0) {
380
- decide("ambiguous",
381
- ["zero discriminating lines across the diff — no signal to judge; refusing to decide on vacuous truth"],
382
- {});
383
- }
384
-
385
- const details = {
386
- manifest_built_at: manifest ? manifest.built_at : null,
387
- manifest_advanced: manifestAdvanced,
388
- head,
389
- discriminating_added: addedPresent + addedAbsent,
390
- discriminating_removed: removedPresent + removedAbsent,
391
- added_present: addedPresent,
392
- added_absent: addedAbsent,
393
- removed_present: removedPresent,
394
- removed_absent: removedAbsent,
395
- };
396
-
397
- // Manifest advanced past the trigger: a build ran for this attempt.
398
- if (manifestAdvanced) {
399
- // Verified ⟺ the source shows the full post-edit state. A side with no
400
- // discriminating lines is vacuously satisfied; totalDisc > 0 (checked
401
- // above) guarantees at least one side carries signal.
402
- const addedOk = addedPresent + addedAbsent === 0 || (addedPresent > 0 && addedAbsent === 0);
403
- const removedOk = removedPresent + removedAbsent === 0 || (removedAbsent > 0 && removedPresent === 0);
404
- if (addedOk && removedOk) {
405
- // Design §1.9: the manifest must be a NEW build identity, not a
406
- // replayed manifest with a bumped built_at. When the pre-trigger
407
- // baseline is available, require the content_sha256 to differ; when
408
- // unavailable, fall back to the time-based advance (noted) — the Step
409
- // 4.5 verifier is the strict gate and fails closed without a baseline
410
- // before stamping.
411
- const beforeHash = manifestBefore ? manifestBefore.content_sha256 : null;
412
- const currentHash = manifest ? manifest.content_sha256 : null;
413
- if (manifestBefore && beforeHash && currentHash) {
414
- if (beforeHash === currentHash) {
415
- decide("ambiguous",
416
- [`manifest built_at advanced past trigger but content_sha256 is unchanged from the pre-trigger baseline — a replayed manifest, not a new build; refusing to verify`],
417
- { ...details, manifest_before: manifestBefore });
418
- }
419
- decide("verified",
420
- [`manifest built_at ${manifest.built_at} advanced past trigger ${triggerTs} with a new content_sha256 (differs from pre-trigger baseline); content matches the diff`],
421
- { ...details, manifest_before: manifestBefore });
422
- }
423
- decide("verified",
424
- [`manifest built_at ${manifest.built_at} advanced past trigger ${triggerTs}; content matches the diff (no pre-trigger baseline — freshness by time only; the Step 4.5 verifier fails closed without a baseline)`],
425
- { ...details, manifest_before: null, baseline_absent: true });
426
- }
427
- // A build ran but the content does not match: not ours to claim.
428
- decide("ambiguous",
429
- [`manifest advanced past trigger but content mismatched (added PRESENT ${addedPresent}/${addedPresent + addedAbsent}, removed ABSENT ${removedAbsent}/${removedPresent + removedAbsent}) — a build ran that is not this diff`],
430
- details);
431
- }
432
-
433
- // Manifest stale: no build for this attempt. Judge the source state.
434
- const addedDropped = addedPresent + addedAbsent === 0 || (addedAbsent > 0 && addedPresent === 0);
435
- const removedDropped = removedPresent + removedAbsent === 0 || (removedPresent > 0 && removedAbsent === 0);
436
- const addedApplied = addedPresent + addedAbsent === 0 || (addedPresent > 0 && addedAbsent === 0);
437
- const removedApplied = removedPresent + removedAbsent === 0 || (removedAbsent > 0 && removedPresent === 0);
438
- if (addedDropped && removedDropped) {
439
- decide("provably-dropped",
440
- [
441
- "(a) every discriminating added line ABSENT",
442
- "(b) every discriminating removed line PRESENT",
443
- `(c) manifest built_at ${manifest ? manifest.built_at : "missing"} NOT advanced past trigger ${triggerTs}`,
444
- "(d) no build in flight (checked twice, bracketing the read)",
445
- `(e) decision time past park+${quiesceMins}min quiesce`,
446
- "(f) HEAD == commit",
447
- ],
448
- details);
449
- }
450
- if (addedApplied && removedApplied) {
451
- // J1's shape: the platform started something (the phantom) but no build
452
- // completed — "platform build-emission failure." No retry: re-applying a
453
- // possibly mid-apply diff is the 2026-09-12 hazard in a new coat.
454
- decide("applied-not-built",
455
- [
456
- `source shows the full post-edit state but manifest stale (built_at ${manifest ? manifest.built_at : "missing"}) — platform build-emission failure; no retry`,
457
- ],
458
- details);
459
- }
460
- decide("ambiguous",
461
- [`partial-apply or indeterminate (added PRESENT ${addedPresent}/${addedPresent + addedAbsent}, removed PRESENT ${removedPresent}/${removedPresent + removedAbsent}) with a stale manifest`],
462
- details);
@@ -1,154 +0,0 @@
1
- // publish-content.js — shared deterministic content-judgment primitives.
2
- //
3
- // Parent publish verification (lib/verify-publish.js) and unknown-recovery
4
- // classification (lib/classify-publish-absence.js) both judge the same
5
- // question — "does the on-disk source contain the publish commit's diff?" —
6
- // against the same diff parser, findings parser, and collision-exemption
7
- // rules. Two copies of the collision logic already diverged once (2026-09-15,
8
- // task 00bca4b8); this module is the single source of truth.
9
- //
10
- // ESM (imported by verify-publish.js and classify-publish-absence.js).
11
- // Pure and deterministic: no wall-clock reads, no randomness, no I/O except
12
- // the git subprocesses in makeOldCounter (read-only).
13
-
14
- import { execFileSync } from "node:child_process";
15
-
16
- export const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
17
-
18
- // Parse `git diff base commit` into per-file added/removed lines.
19
- // Byte-identical split to build-readback-request.js and the old
20
- // verify-publish.js inline parser (the verifier looks raw lines up
21
- // verbatim, so all consumers must agree on the split).
22
- export function parseDiff(diffText) {
23
- const expected = new Map(); // path -> { added: [], removed: [], isBinary: bool }
24
- let curFile = null;
25
- for (const line of diffText.split("\n")) {
26
- if (line.startsWith("diff --git")) {
27
- const m = line.match(/^diff --git a\/(.+) b\/(.+)$/);
28
- curFile = m ? m[2] : "unknown";
29
- expected.set(curFile, { added: [], removed: [], isBinary: false });
30
- } else if (curFile && line.startsWith("Binary files ")) {
31
- expected.get(curFile).isBinary = true;
32
- } else if (curFile && line.startsWith("+") && !line.startsWith("+++")) {
33
- expected.get(curFile).added.push(line.slice(1));
34
- } else if (curFile && line.startsWith("-") && !line.startsWith("---")) {
35
- expected.get(curFile).removed.push(line.slice(1));
36
- }
37
- }
38
- return expected;
39
- }
40
-
41
- // Parse the machine-readable findings block:
42
- // FILE: <path>
43
- // ADDED: <line> :: PRESENT|ABSENT
44
- // REMOVED: <line> :: PRESENT|ABSENT
45
- // END_FILE
46
- export function parseFindings(text) {
47
- const findings = new Map(); // path -> { added: Map(line->verdict), removed: Map(line->verdict) }
48
- let cur = null;
49
- let malformed = null;
50
- for (const rawLine of text.split("\n")) {
51
- const line = rawLine.trimEnd();
52
- if (line.startsWith("FILE: ")) {
53
- cur = { added: new Map(), removed: new Map() };
54
- findings.set(line.slice(6).trim(), cur);
55
- } else if (line === "END_FILE") {
56
- cur = null;
57
- } else if (cur && (line.startsWith("ADDED: ") || line.startsWith("REMOVED: "))) {
58
- const kind = line.startsWith("ADDED: ") ? "added" : "removed";
59
- const rest = line.slice(kind === "added" ? 7 : 9);
60
- const sep = rest.lastIndexOf(" :: ");
61
- if (sep < 0) { malformed = `malformed finding line: ${line.slice(0, 80)}`; break; }
62
- const content = rest.slice(0, sep);
63
- const verdict = rest.slice(sep + 4).trim();
64
- if (verdict !== "PRESENT" && verdict !== "ABSENT") {
65
- malformed = `bad verdict: ${verdict}`;
66
- break;
67
- }
68
- cur[kind].set(content, verdict);
69
- }
70
- }
71
- return { findings, malformed };
72
- }
73
-
74
- // Whole-line occurrence counts of a file's content at <base>.
75
- // [] when the base is the empty tree or the file did not exist there.
76
- function oldTreeLines(repoPath, base, path) {
77
- if (base === EMPTY_TREE) return [];
78
- let text;
79
- try {
80
- text = execFileSync("git", ["-C", repoPath, "show", `${base}:${path}`], {
81
- encoding: "utf8", maxBuffer: 4 * 1024 * 1024,
82
- });
83
- } catch {
84
- return [];
85
- }
86
- if (text === "") return [];
87
- const lines = text.split("\n");
88
- if (lines[lines.length - 1] === "") lines.pop(); // drop the trailing-newline artifact
89
- return lines;
90
- }
91
-
92
- // Returns (path, line) => occurrences of line in path's old tree.
93
- // Cached per path. Read-only git subprocesses.
94
- export function makeOldCounter(repoPath, base) {
95
- const cache = new Map(); // path -> Map(line -> count)
96
- return (path, line) => {
97
- let counts = cache.get(path);
98
- if (counts === undefined) {
99
- counts = new Map();
100
- for (const l of oldTreeLines(repoPath, base, path)) {
101
- counts.set(l, (counts.get(l) || 0) + 1);
102
- }
103
- cache.set(path, counts);
104
- }
105
- return counts.get(line) || 0;
106
- };
107
- }
108
-
109
- // Split each file's diff lines into discriminating vs non-discriminating.
110
- //
111
- // A line has zero discriminating power when its presence/absence cannot
112
- // tell "hunk landed" from "hunk dropped":
113
- // - added line already in the old tree (colliding pre-existing line):
114
- // PRESENT whether or not the hunk landed.
115
- // - removed line occurring in the old tree strictly more times than the
116
- // diff removes it: PRESENT whether or not the removal happened.
117
- //
118
- // Returns Map(path -> {
119
- // addedDisc: [...], removedDisc: [...], // discriminating lines
120
- // unjudgeable: null | "binary-or-mode-or-rename" | "fully-colliding-added"
121
- // }).
122
- // unjudgeable marks files the line-based judge cannot certify at all:
123
- // a diff with no content lines (binary, mode-only, rename), or added
124
- // lines that ALL collide (their PRESENT findings prove nothing).
125
- export function discriminatingLines(diffMap, oldCount) {
126
- const out = new Map();
127
- for (const [path, exp] of diffMap) {
128
- const entry = { addedDisc: [], removedDisc: [], unjudgeable: null };
129
- if (exp.added.length === 0 && exp.removed.length === 0) {
130
- entry.unjudgeable = "binary-or-mode-or-rename";
131
- out.set(path, entry);
132
- continue;
133
- }
134
- for (const line of exp.added) {
135
- if (oldCount(path, line) === 0) entry.addedDisc.push(line);
136
- }
137
- // The diff may remove the same line more than once; the old tree must
138
- // account for every removal before a line counts as colliding.
139
- const removedBudget = new Map();
140
- for (const line of exp.removed) {
141
- removedBudget.set(line, (removedBudget.get(line) || 0) + 1);
142
- }
143
- for (const line of exp.removed) {
144
- if (oldCount(path, line) <= removedBudget.get(line)) {
145
- entry.removedDisc.push(line);
146
- }
147
- }
148
- if (exp.added.length > 0 && entry.addedDisc.length === 0) {
149
- entry.unjudgeable = "fully-colliding-added";
150
- }
151
- out.set(path, entry);
152
- }
153
- return out;
154
- }