GitHubivarvong

ply

main
ply7 git calls150 ms27 KiB4 cachedprotocol v2
112 lines8.7 KiBRaw
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;
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;
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;
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)) : '-'}`); }
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); }
Diagnostics7 git calls · 150 ms
CallStatusSizeTime0150 ms
discover200191 B5 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=5
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 KiB66 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=65
git.response.bytes=11990
git.response.cache=EXPIRED
git.response.duration_ms=66
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 · 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=4347
git.response.cache=HIT
git.response.duration_ms=3
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=10
Open 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=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 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=5724
git.response.cache=HIT
git.response.duration_ms=3
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=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetchtree:0200853 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=853
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=fb781d4e4fd58ededa1f1078d937de6f4b1316a2
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=853
git.pkt.payload.max=826
git.sideband.band1.bytes=826
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=826
git.pack.trailer=c89bc485f6ad08d875fc515f3fdd69897096847b
Open pack map
fetch2003.6 KiB74 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=74
git.response.bytes=3657
git.response.cache=MISS
git.response.duration_ms=74
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3657
git.pkt.payload.max=3630
git.sideband.band1.bytes=3630
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
a5e2f42618e67089
Request
0331100f…
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