GitHubivarvong

ply

main
ply7 git calls39 ms26 KiB6 cachedprotocol v2
68 lines5.7 KiBRaw
1// The merge-base server behaviours (issue #18, #42): a server answering the commit-only `tree:0` + `deepen` walks of
2// mergeBase with a cut history. Shared by test/fuzz/liar.mjs (`--op mergebase`, random DAGs) and
3// test/mergebase-space.test.js (the enumerated outcome space).
4import { buildPack } from '../../src/pack.js';
5import { TYPE } from '../../src/objects.js';
6import { concat, te } from '../../src/bytes.js';
7import { Rng } from './prng.js';
8import { reachableCommits } from './dag.js';
10export const pktLine = s => { const b = typeof s === 'string' ? te.encode(s) : s; return concat([te.encode((b.length + 4).toString(16).padStart(4, '0')), b]); };
11export const FLUSH = te.encode('0000');
12// a v2 fetch response carrying exactly these objects (full, no deltas) as a sideband pack
13export async function fetchResponse(objs, { shallow = [] } = {}) {
14 const { pack } = await buildPack(objs);
15 const parts = [];
16 if (shallow.length) { parts.push(pktLine('shallow-info\n')); for (const s of shallow) parts.push(pktLine(`shallow ${s}\n`)); parts.push(te.encode('0001')); }
17 parts.push(pktLine('packfile\n'));
18 for (let i = 0; i < pack.length; i += 65000) parts.push(pktLine(concat([Uint8Array.of(1), pack.subarray(i, Math.min(i + 65000, pack.length))])));
19 parts.push(FLUSH);
20 return concat(parts);
23// ---- merge-base lies (issue #18) ----
24// The request names one commit want, `filter tree:0` and `deepen N`; the honest answer is every commit within N of the
25// want, with the commits at the cut as `shallow` lines (dag.js reachableCommits, calibrated against upload-pack). Each
26// lie is a function of the request alone (a sub-Rng seeded from the want and the case, so concurrent fetches of `ours`
27// and `theirs` cannot reorder the randomness) and answers without forwarding; `M.lied` counts the answers that were lies.
28const fetchRequest = body => { const s = Buffer.from(body).toString('latin1'); return { want: /want ([0-9a-f]{40})\n/.exec(s)?.[1] ?? null, deepen: +(/deepen (\d+)\n/.exec(s)?.[1] ?? 0), filter: /filter ([^\n]+)\n/.exec(s)?.[1] ?? null }; };
29const window = (M, want, deepen) => { const { commits, boundary } = reachableCommits(M.model, [want], { depth: deepen }); return { all: [...commits.keys()], boundary: [...boundary] }; };
30const drop = (rng, list, k) => { const out = new Set(); const pool = [...list]; while (out.size < Math.min(k, pool.length)) out.add(pool[rng.int(0, pool.length - 1)]); return out; };
31export const MERGE_LIES = {
32 // control: forwarded untouched → the bases must be git's
33 'mb:honest': () => null,
34 // the issue: ancestors inside the window are omitted and no shallow line admits it. The pack still hash-verifies.
35 'mb:omit-no-shallow': (M, rng, w) => { const gone = drop(rng, w.all.filter(c => c !== w.want), rng.int(1, 3)); return { keep: w.all.filter(c => !gone.has(c)), shallow: [], omitted: gone.size }; },
36 // the honest shallow lines are sent, but commits elsewhere in the window are missing too
37 'mb:omit-keep-shallow': (M, rng, w) => { const gone = drop(rng, w.all.filter(c => c !== w.want && !w.boundary.includes(c)), rng.int(1, 3)); return { keep: w.all.filter(c => !gone.has(c)), shallow: w.boundary, omitted: gone.size }; },
38 // a consistent cut shallower than asked: one commit c declared shallow, everything only reachable through it dropped.
39 // Not a contradiction of the protocol — a server may answer with less depth — so the client must deepen or give up typed.
40 'mb:cut-shallower': (M, rng, w) => {
41 const c = rng.pick(w.all.filter(x => x !== w.want)); if (!c) return null;
42 const seen = new Set([w.want]); const st = [w.want];
43 while (st.length) { const x = st.pop(); if (x === c) continue; for (const p of M.model.commits.get(x).parents) if (!seen.has(p) && w.all.includes(p)) { seen.add(p); st.push(p); } }
44 return { keep: [...seen], shallow: [...new Set([...w.boundary.filter(b => seen.has(b) && b !== c), c])], omitted: w.all.length - seen.size };
45 },
46 // the want itself is missing from an otherwise honest pack
47 'mb:omit-want': (M, rng, w) => ({ keep: w.all.filter(c => c !== w.want), shallow: w.boundary, omitted: 1 }),
48 // an unrequested commit (from an unrelated repo) rides along in an otherwise honest pack
49 'mb:extra-foreign': (M, rng, w) => ({ keep: w.all, shallow: w.boundary, extra: [M.decoy], omitted: 0 }),
50 // a tree object in a tree:0 pack (the want's root tree). Main admits a well-formed, hash-verified tree in a commit-walk
51 // pack (the blob:none fallback carries every tree for the same request; fetch.js checkWide, #47) and the walk reads
52 // commits only, so this is not a contradiction: the answer must still be git's (or a typed error)
53 'mb:tree-in-tree0': (M, rng, w) => ({ keep: w.all, shallow: w.boundary, extra: [{ type: TYPE.TREE, data: M.treeOf(w.want) }], omitted: 0 }),
54};
55export const mergeLie = (M, kind, seed) => ctx => {
56 if (ctx.stage !== 'fetch' || ctx.phase !== 'request') return null;
57 const { want, deepen, filter } = fetchRequest(ctx.reqBody);
58 if (!want || !deepen || filter !== 'tree:0' || !M.model.commits.has(want)) return null;
59 const rng = new Rng((seed ^ parseInt(want.slice(0, 8), 16) ^ (deepen * 2654435761)) >>> 0);
60 if (kind !== 'mb:honest' && deepen === 1 && rng.bool(0.5)) return null; // let some of the three-commit fetches through
61 const w = { ...window(M, want, deepen), want };
62 const plan = MERGE_LIES[kind](M, rng, w);
63 if (!plan || (plan.omitted === 0 && !plan.extra)) return null; // nothing to omit in a one-commit window: the honest answer
64 M.lied++; M.omitted += plan.omitted;
65 const objs = [...plan.keep.map(c => ({ type: TYPE.COMMIT, data: M.commitData.get(c) })), ...(plan.extra ?? [])];
66 return { type: 'bytes', noForward: true, name: kind, bytes: fetchResponse(objs, { shallow: plan.shallow }) };
67};
Diagnostics7 git calls · 39 ms
CallStatusSizeTime039 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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=11990
git.response.cache=HIT
git.response.duration_ms=2
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=3
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=10
Open pack map
fetchtree:0200919 B · hit3 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=3
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
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=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetchtree:0200853 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=25
git.response.bytes=853
git.response.cache=HIT
git.response.duration_ms=4
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
fetch2002.5 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2564
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=c61a8cb433ec93f6a6176d94efcb3eff1ccad14f
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=2564
git.pkt.payload.max=2537
git.sideband.band1.bytes=2537
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
a5e13d7bacf31293
Request
2db4c2fb…
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