main
1// The line merge against git itself. Each case is three versions of one file (base, ours, theirs) built as commits with
2// plumbing in one bare repo; git's answer is `git merge-tree --write-tree` (merge-ort: histogram diff, zealous level).
3// Verdicts per case:
4// agree-clean both clean, identical bytes
5// agree-conflict both conflict
6// conservative git clean, we conflict (allowed: superset of git's conflicts) — split by our reason
7// WRONG we clean and git conflicts, or both clean with different bytes — must be zero
8// A conservative case fails the run too (exit 1) unless a documented rule covers it: --rediff undecided, or the step
9// budget shown spent by the merge's own meter. Otherwise a merge that refuses everything would pass.
10// node test/fuzz/linemerge.mjs [--n 2000] [--seed 1] [--big|--runs] [--rediff skip]
11// --rediff skip: model git without the compaction re-diff (2.43, the oracle here) — then conservative must be 0 too
12import { spawnSync } from 'node:child_process';
13import { mkdtempSync, rmSync } from 'node:fs';
14import { tmpdir } from 'node:os';
15import { join } from 'node:path';
16import { Rng } from './prng.js';
17import { mergeLines } from '../../src/linemerge.js';
18import { LIMIT_MERGE_WORK } from '../../src/limits.js';
20import { oracleRediff } from '../gitversion.js';
21const REDIFF = process.argv.includes('--rediff') ? process.argv[process.argv.indexOf('--rediff') + 1] : oracleRediff(); // default: whatever the oracle git does
22const GIT_BIN = process.env.GIT_BIN ?? 'git'; // GIT_BIN=/path/to/git: judge against another git build
23const arg = (k, d) => { const i = process.argv.indexOf(`--${k}`); return i < 0 ? d : +process.argv[i + 1]; };
24const N = arg('n', 2000), SEED = arg('seed', 1), ONLY = arg('case', -1);
25// --driver union|binary: the file carries `merge=<driver>` in a .gitattributes git reads via attr.tree=<ours>
26const DRIVER = process.argv.includes('--driver') ? process.argv[process.argv.indexOf('--driver') + 1] : 'text';
27const dir = mkdtempSync(join(tmpdir(), 'lm-'));
28const ENV = { ...process.env, GIT_AUTHOR_NAME: 'w', GIT_AUTHOR_EMAIL: 'w@x', GIT_COMMITTER_NAME: 'w', GIT_COMMITTER_EMAIL: 'w@x', GIT_AUTHOR_DATE: '1700000000 +0000', GIT_COMMITTER_DATE: '1700000000 +0000' };
29const git = (args, input) => { const r = spawnSync(GIT_BIN, ['-C', dir, ...args], { input, env: ENV, maxBuffer: 1 << 26 }); return { status: r.status, out: r.stdout, err: r.stderr.toString() }; };
30git(['init', '-q', '--bare']);
31const commit = (bytes, parent) => {
32 const blob = git(['hash-object', '-w', '--stdin'], bytes).out.toString().trim();
33 const attrs = DRIVER === 'text' ? '' : `100644 blob ${git(['hash-object', '-w', '--stdin'], `f.txt merge=${DRIVER}\n`).out.toString().trim()}\t.gitattributes\n`;
34 const tree = git(['mktree'], `${attrs}100644 blob ${blob}\tf.txt\n`).out.toString().trim();
35 return git(['commit-tree', tree, ...(parent ? ['-p', parent] : []), '-m', 'c']).out.toString().trim();
36};
38// Line vocabularies chosen to stress alignment: many repeated lines (braces, blanks), a few unique ones, CRLF, no final newline.
39const VOCAB = ['{\n', '}\n', '\n', '\n', ' return x;\n', 'a\n', 'b\n', 'c\n', '// x\n', 'end\n'];
40const BIG = process.argv.includes('--big'), RUNS = process.argv.includes('--runs'); // --runs: long runs of a few lines → histogram's Myers fallback // long files with >64 copies of a line: histogram's own Myers fallback (max_chain_length)
41function genRuns(rng) {
42 const lines = []; const k = rng.int(1, 6);
43 for (let r = 0; r < k; r++) { const l = rng.pick(['x\n', 'y\n', '\n', '}\n', `u${rng.int(0, 5)}\n`]); for (let j = rng.int(1, 120); j > 0; j--) lines.push(l); }
44 return lines;
45}
46function genFile(rng) {
47 if (RUNS) return genRuns(rng);
48 const n = BIG ? rng.int(100, 400) : rng.int(0, 40), lines = [];
49 for (let i = 0; i < n; i++) {
50 const k = rng.int(0, 9);
51 if (BIG && (k < 9 || rng.int(0, 3))) { lines.push(rng.pick(['{\n', '}\n', '\n'])); continue; } // a low-variety file: every line repeats >64 times
52 lines.push(k < 5 ? rng.pick(VOCAB) : k < 8 ? `line ${rng.int(0, 60)}\n` : k < 9 ? `crlf ${rng.int(0, 9)}\r\n` : `u${rng.int(0, 1e6)}\n`);
53 }
54 if (lines.length && rng.int(0, 5) === 0) lines[lines.length - 1] = lines[lines.length - 1].replace(/\r?\n$/, '');
55 return lines;
56}
57function edit(rng, base, other) {
58 const out = base.slice(); const ops = rng.int(1, 4);
59 for (let k = 0; k < ops; k++) {
60 const at = rng.int(0, out.length), len = rng.int(0, 3), kind = rng.int(0, 5);
61 const fresh = () => (rng.int(0, 2) === 0 ? rng.pick(VOCAB) : `new ${rng.int(0, 40)}\n`);
62 if (kind === 0) out.splice(at, len); // delete
63 else if (kind === 1) out.splice(at, 0, ...Array.from({ length: rng.int(1, 3) }, fresh)); // insert
64 else if (kind === 2) out.splice(at, len, ...Array.from({ length: rng.int(1, 3) }, fresh)); // replace
65 else if (kind === 3 && other) { const s = rng.int(0, other.length); out.splice(at, len, ...other.slice(s, s + rng.int(1, 3))); } // borrow lines
66 else if (kind === 4 && out.length) { const s = rng.int(0, out.length - 1); out.splice(at, 0, out[s]); } // duplicate a line
67 }
68 return out;
69}
70const bytes = lines => new TextEncoder().encode(lines.join(''));
71const eq = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
73const counts = { 'agree-clean': 0, 'agree-conflict': 0, conservative: 0, WRONG: 0 }; const why = {}; const wrong = []; const refused = [];
74const rng0 = new Rng(SEED);
75for (let i = 0; i < N; i++) {
76 const seed = rng0.u32(); if (ONLY >= 0 && i !== ONLY) continue;
77 const rng = new Rng(seed);
78 const baseL = genFile(rng); const oursL = edit(rng, baseL); const sameAsOurs = rng.int(0, 9) === 0;
79 const theirsL = sameAsOurs ? oursL.slice() : edit(rng, baseL, oursL);
80 const B = bytes(baseL), O = bytes(oursL), T = bytes(theirsL);
81 if (eq(B, O) || eq(B, T) || eq(O, T)) continue; // trivial: git never runs a content merge
82 const cb = commit(B), co = commit(O, cb), ct = commit(T, cb);
83 const g = git(['-c', `attr.tree=${co}`, 'merge-tree', '--write-tree', '--name-only', co, ct]);
84 // exit 0 clean, 1 conflicts; anything else is git failing, which must not count as agreeing with a ply conflict
85 if (g.status !== 0 && g.status !== 1) { console.log(`case ${i}: git merge-tree failed (exit ${g.status}): ${g.err}`); process.exit(1); }
86 const gitClean = g.status === 0;
87 const cat = gitClean ? git(['cat-file', 'blob', `${g.out.toString().split('\n')[0]}:f.txt`]) : null;
88 if (cat && cat.status !== 0) { console.log(`case ${i}: git cat-file failed: ${cat.err}`); process.exit(1); }
89 const gitBytes = cat ? cat.out : null;
90 const meter = { steps: 0 };
91 const m = mergeLines(B, O, T, { rediff: REDIFF, driver: DRIVER, meter });
92 let v;
93 if (m.clean && gitClean) v = eq(m.data, new Uint8Array(gitBytes)) ? 'agree-clean' : 'WRONG';
94 else if (!m.clean && !gitClean) v = 'agree-conflict';
95 else if (!m.clean && gitClean) {
96 v = 'conservative'; const k = m.detail ? `${m.reason}: ${m.detail}` : m.reason; why[k] = (why[k] ?? 0) + 1;
97 // A refusal where git merges cleanly passes only under a documented rule: --rediff undecided (the compaction
98 // re-diff), or the step budget (limits.ts), proven spent by the meter against the limit's own arithmetic.
99 const budget = LIMIT_MERGE_WORK;
100 const ruled = (REDIFF === 'undecided' && k === 'undecided: compaction needs the classic-diff fallback') || (m.reason === 'too large to merge' && meter.steps > budget);
101 if (!ruled) { refused.push(i); if (refused.length <= 3) console.log(`REFUSED case ${i}: git merges cleanly, ours ${k} after ${meter.steps} steps (budget ${budget})`); }
102 }
103 else v = 'WRONG';
104 counts[v]++;
105 if (v === 'WRONG') { wrong.push(i); if (wrong.length <= 3) console.log(`WRONG case ${i}: ours ${m.clean ? 'clean' : m.reason}, git ${gitClean ? 'clean' : 'conflict'}\n base=${JSON.stringify(baseL.join(''))}\n ours=${JSON.stringify(oursL.join(''))}\n theirs=${JSON.stringify(theirsL.join(''))}\n gitout=${gitBytes ? JSON.stringify(gitBytes.toString()) : '-'}\n ourout=${m.clean ? JSON.stringify(new TextDecoder().decode(m.data)) : '-'}`); }
106}
107rmSync(dir, { recursive: true, force: true });
108const total = Object.values(counts).reduce((a, b) => a + b, 0);
109console.log(`${total} non-trivial cases (seed ${SEED}, rediff ${REDIFF}, driver ${DRIVER}): ${JSON.stringify(counts)} conservative by reason: ${JSON.stringify(why)}`);
110if (wrong.length) { console.log(`WRONG cases: ${wrong.slice(0, 20).join(',')} — repro: --seed ${SEED} --case <i>`); process.exit(1); }
111// a refusal is not a pass: a merge that always conflicts would otherwise agree with nothing and fail nothing
112if (refused.length) { console.log(`REFUSED cases (git clean, ours conflict, no documented rule): ${refused.slice(0, 20).join(',')} — repro: --seed ${SEED} --case <i>`); process.exit(1); }
Diagnostics4 git calls · 241 ms
CallStatusSizeTime0241 ms
discover200152 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=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 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=38434 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false 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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2003.6 KiB236 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=236 git.response.bytes=3658 git.response.cache=MISS git.response.duration_ms=236 git.fetch.wants=ef2056e73e40e081edc55a447615f0b5034e054e 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3658 git.pkt.payload.max=3637 git.sideband.band1.bytes=3636 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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e31a0889c63e6e
- Request
- 38b6a2bf…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format