Artifactspublic-v0

ply

main
ply4 git calls31 ms43 KiB3 cachedprotocol v2
178 lines14 KiBRaw
1// Cells observed from git: a real `git receive-pack` (git http-backend behind test/server.js) answers ply's real HTTP
2// push. For each scenario the response bytes are captured and must equal the cell's rendering (band-2 text stripped,
3// runs of keepalives collapsed), ply's outcome must be the table's, and the server's ref state must agree with it:
4// landed → every ref moved; rejected → none; partial → exactly the landed ones; ambiguous → recorded (the truth ply did
5// not claim). Faults are injected in the request (a corrupted pack, a cut body), never in git's answer.
6import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
7import { join } from 'node:path';
8import { git, tmpRoot, makeBareRepo, startGitServer } from '../server.js';
9import { GitHttp } from '../../src/client.js';
10import { Tracer, arrayExporter } from '../../src/otel.js';
11import { encodeTree, encodeCommit, hashObject, TYPE } from '../../src/objects.js';
12import { pktInit, pktStep, FLUSH } from '../../src/pktline.js';
13import { concat, te, indexOfBytes } from '../../src/bytes.js';
14import { ZERO_OID } from '../../src/push.js';
15import * as space from './push-outcome.space.js';
16import { unwrap, unwrapAsync } from '../unwrap.js';
18const ev = (name, tag, extra = {}) => {
19 const vs = space.axis(name).variants.filter(v => v.tag === tag && Object.entries(extra).every(([k, x]) => (typeof x === 'string' && typeof v[k] === 'object' ? v[k].tag === x : v[k] === x)));
20 if (vs.length !== 1) throw new Error(`no single variant ${name}.${tag} ${JSON.stringify(extra)}`);
21 return vs[0];
22};
23// cellOf({ refs: 'two', spread: ['mixed', { at: 1, odd: 'ngHook' }], ... }); unspecified axes take their first variant
24export const cellOf = spec => Object.fromEntries(space.AXES.map(a => {
25 const s = spec[a.name] ?? a.variants[0].tag;
26 return [a.name, Array.isArray(s) ? ev(a.name, s[0], s[1]) : ev(a.name, s)];
27}));
29const commit = (msg, parents) => {
30 const blob = te.encode(`${msg}\n`);
31 const tree = unwrap(encodeTree([{ mode: '100644', name: 'f', oid: hashObject('blob', blob) }]));
32 const c = unwrap(encodeCommit({ tree: hashObject('tree', tree), parents, author: 'a <a@x> 1700000000 +0000', message: `${msg}\n` }));
33 return { oid: hashObject('commit', c), objects: [{ type: TYPE.COMMIT, data: c }, { type: TYPE.TREE, data: tree }, { type: TYPE.BLOB, data: blob }] };
34};
35const C0 = commit('c0', []), C1 = commit('c1', [C0.oid]);
36const hook = (bare, name, body) => { const p = join(bare, 'hooks', name); mkdirSync(join(bare, 'hooks'), { recursive: true }); writeFileSync(p, `#!/bin/sh\n${body}\n`); chmodSync(p, 0o755); };
37const SETUP = {
38 none: () => {},
39 updateRejectsR1: b => hook(b, 'update', 'echo "update $1" >&2\n[ "$1" = refs/heads/r1 ] && exit 1\nexit 0'),
40 preReceiveRejects: b => hook(b, 'pre-receive', 'echo "policy: no" >&2\nexit 1'),
41 hideR1: b => git(b, ['config', 'receive.hideRefs', 'refs/heads/r1']),
42 noisy: b => { hook(b, 'pre-receive', 'echo "pre-receive says hi" >&2'); hook(b, 'post-receive', 'echo "post-receive says bye" >&2'); },
43 slowHooks: b => { git(b, ['config', 'receive.keepAlive', '1']); hook(b, 'pre-receive', 'sleep 2'); hook(b, 'post-receive', 'sleep 2'); },
44};
45// request faults, applied by the fetchImpl wrapper to the receive-pack POST
46const FAULT = {
47 none: null,
48 corruptPack: body => { const b = body.slice(); b[b.length - 5] ^= 1; return { body: b }; },
49 dropBeforePack: body => ({ body: body.subarray(0, packStart(body)), drop: true }),
50 dropMidPack: body => ({ body: body.subarray(0, packStart(body) + 20), drop: true }),
51 dropAfterPack: body => ({ body, drop: true }),
52};
53const packStart = body => indexOfBytes(body, te.encode('PACK'));
54// the compare-and-swap reasons of rejectionCode (src/push.js isCas)
55const CAS = /^(failed to update ref|failed to lock|atomic push failure|bad ref|stale ref|incorrect old value provided)$|fetch first|non-fast-forward|already exists/;
57// [name, cell spec, { setup, fault, stale }]. `stale`: refs commanded with an old oid they are not at.
58const N = { one: 1, two: 2, three: 3 };
59export const SCENARIOS = [
60 ...['one', 'two', 'three'].flatMap(refs => ['plain', 'sideband'].flatMap(framing => ['v1', 'v2'].flatMap(format => ['off', 'on'].map(atomic =>
61 [`all ok ${refs}/${framing}/${format}/atomic ${atomic}`, { refs, framing, format, atomic }, {}]))) ),
62 ...['two', 'three'].flatMap(refs => ['plain', 'sideband'].flatMap(framing => ['v1', 'v2'].flatMap(format => [
63 [`update hook refuses r1 ${refs}/${framing}/${format}`, { refs, framing, format, event: framing === 'sideband' ? 'progress' : 'clean', spread: ['mixed', { at: 1, odd: 'ngHook' }] }, { setup: 'updateRejectsR1' }],
64 [`update hook refuses r1, atomic ${refs}/${framing}/${format}`, { refs, framing, format, atomic: 'on', event: framing === 'sideband' ? 'progress' : 'clean', status: 'ngAtomic', spread: ['mixed', { at: 1, odd: 'ngHook' }] }, { setup: 'updateRejectsR1' }],
65 ]))),
66 ...['plain', 'sideband'].flatMap(framing => [
67 [`pre-receive refuses ${framing}`, { refs: 'two', framing, event: framing === 'sideband' ? 'progress' : 'clean', status: 'ngHook' }, { setup: 'preReceiveRejects', reasons: { ngHook: 'pre-receive hook declined' } }],
68 [`stale old oid ${framing}`, { framing, event: framing === 'sideband' ? 'progress' : 'clean', status: 'ngCas' }, { stale: [0] }],
69 [`stale old oid, two refs ${framing}`, { refs: 'two', framing, event: framing === 'sideband' ? 'progress' : 'clean', spread: ['mixed', { at: 0, odd: 'ngCas' }] }, { stale: [0] }],
70 [`corrupted pack ${framing}`, { framing, event: framing === 'sideband' ? 'progress' : 'clean', unpack: 'gitError', status: 'ngUnpacker' }, { fault: 'corruptPack' }],
71 [`corrupted pack, two refs, atomic ${framing}`, { refs: 'two', atomic: 'on', framing, event: framing === 'sideband' ? 'progress' : 'clean', unpack: 'gitError', status: 'ngUnpacker' }, { fault: 'corruptPack' }],
72 [`delete ${framing}`, { commands: 'deleteOnly', framing }, {}],
73 [`delete two, one stale ${framing}`, { refs: 'two', commands: 'deleteOnly', framing, event: framing === 'sideband' ? 'progress' : 'clean', spread: ['mixed', { at: 1, odd: 'ngCas' }] }, { stale: [1] }],
74 // update(): a delete whose old oid names an object the server lacks is applied regardless ("allowing deletion of
75 // corrupt ref"): git's report says ok, and the ref is gone. The report is truthful, the CAS is not.
76 [`delete with an unknown old oid ${framing}`, { commands: 'deleteOnly', framing, event: framing === 'sideband' ? 'progress' : 'clean' }, { stale: [0], unknownOld: true }],
77 [`hidden ref, non-atomic ${framing}`, { refs: 'two', framing, spread: ['mixed', { at: 1, odd: 'ngHook' }] }, { setup: 'hideR1', reasons: { ngHook: 'deny updating a hidden ref' } }],
78 [`hidden ref, atomic ${framing}`, { refs: 'two', atomic: 'on', framing, spread: ['mixed', { at: 1, odd: 'ngHook' }] }, { setup: 'hideR1', reasons: { ngHook: 'deny updating a hidden ref' } }],
79 ]),
80 ['noisy hooks, band 2 before and after the report', { refs: 'two', framing: 'sideband', event: 'progress' }, { setup: 'noisy' }],
81 ['slow hooks: keepalives before and after the report', { framing: 'sideband', event: 'keepalive' }, { setup: 'slowHooks' }],
82 ...['dropBeforePack', 'dropMidPack', 'dropAfterPack'].map(fault => [`connection fails: ${fault}`, { event: fault, framing: 'plain' }, { fault }]),
83];
85// band 2 removed; runs of empty band-1 packets (keepalives) collapsed to one
86function normalize(bytes, sideband) {
87 if (!sideband) return bytes;
88 const r = unwrap(pktStep(pktInit(), bytes));
89 const out = []; let prevKeepalive = false;
90 for (const f of r.frames) {
91 if (f.kind === 'flush') { out.push(FLUSH); prevKeepalive = false; continue; }
92 if (f.payload[0] === 2) continue;
93 const ka = f.payload.length === 1 && f.payload[0] === 1;
94 if (ka && prevKeepalive) continue;
95 prevKeepalive = ka;
96 const len = (f.payload.length + 4).toString(16).padStart(4, '0');
97 out.push(te.encode(len), f.payload);
98 }
99 if (r.state.carry) out.push(r.state.carry);
100 return concat(out);
103// what besides the report git sent on the side-band: band-2 frames, and empty band-1 keepalives before / after the report
104function extras(bytes) {
105 const r = unwrap(pktStep(pktInit(), bytes));
106 const x = { band2: 0, kaBefore: 0, kaAfter: 0 }; let reported = false;
107 for (const f of r.frames) {
108 if (f.kind !== 'data') continue;
109 if (f.payload[0] === 2) x.band2++;
110 else if (f.payload[0] === 1 && f.payload.length === 1) x[reported ? 'kaAfter' : 'kaBefore']++;
111 else if (f.payload[0] === 1) reported = true;
112 }
113 return x;
115// the cell's event must be what git did: clean → nothing besides the report; progress → band 2; keepalive → keepalives
116// on both sides of the report (the claim that post-receive keepalives follow the report is checked here)
117const eventMatches = (event, x) => ({ clean: x.band2 === 0 && x.kaBefore + x.kaAfter === 0, progress: x.band2 > 0, keepalive: x.kaBefore > 0 && x.kaAfter > 0 })[event] ?? true;
119const outcomeOf = (e, commands) => {
120 if (!e) return { tag: 'landed', refs: commands.map(c => c.ref) };
121 if (e.code === 'ambiguous') return { tag: 'ambiguous' };
122 if (e.code === 'cas_conflict' || e.code === 'push_rejected') return { tag: 'rejected', code: e.code };
123 if (e.code === 'push_partial') return { tag: 'partial', landed: e.landed, rejected: e.rejected.map(r => r.ref) };
124 return { tag: 'other', code: e.code, message: e.message };
125};
127export async function observeAll() {
128 const { root, cleanup } = tmpRoot();
129 const srv = await startGitServer(root);
130 const results = [];
131 try {
132 let id = 0;
133 for (const [name, spec, { setup = 'none', fault = 'none', stale = [], reasons = {}, unknownOld = false }] of SCENARIOS) {
134 const cell = cellOf(spec);
135 const n = N[cell.refs.tag];
136 const bare = makeBareRepo(root, `s${id}`);
137 const seed = unwrap(GitHttp.create(`${srv.url}/s${id}.git`, { tracer: new Tracer({ exporter: arrayExporter() }) }));
138 id++;
139 // r0..r2 at C0; C1 exists on the server (refs/keep/c1) so a stale old oid names a known object, unless unknownOld
140 await unwrapAsync(seed.push({ commands: [...space.REFS.map(ref => ({ ref, oldOid: ZERO_OID, newOid: C0.oid })), ...(unknownOld ? [] : [{ ref: 'refs/keep/c1', oldOid: ZERO_OID, newOid: C1.oid }])], objects: unknownOld ? C0.objects : [...C0.objects, ...C1.objects], sideband: false }));
141 SETUP[setup](bare);
142 const del = cell.commands.tag === 'deleteOnly';
143 const commands = space.REFS.slice(0, n).map((ref, i) => ({ ref, oldOid: stale.includes(i) ? C1.oid : C0.oid, newOid: del ? ZERO_OID : C1.oid }));
144 let captured = null;
145 const fetchImpl = async (url, init) => {
146 if (init?.method !== 'POST' || !url.endsWith('/git-receive-pack')) return fetch(url, init);
147 const f = FAULT[fault]?.(init.body) ?? { body: init.body };
148 const res = await fetch(url, { ...init, body: f.body });
149 const bytes = new Uint8Array(await res.arrayBuffer());
150 captured = bytes;
151 if (f.drop) throw new TypeError('fetch failed (injected: connection reset)');
152 return new Response(bytes, { status: res.status, headers: res.headers });
153 };
154 const client = unwrap(GitHttp.create(`${srv.url}/s${id - 1}.git`, { fetchImpl, tracer: new Tracer({ exporter: arrayExporter() }) }));
155 const before = git(bare, ['for-each-ref', '--format=%(refname) %(objectname)']);
156 let err = null;
157 try { await unwrapAsync(client.push({ commands, objects: del ? [] : C1.objects, sideband: cell.framing.tag === 'sideband', reportV2: cell.format.tag === 'v2', atomic: cell.atomic.tag === 'on' })); } catch (e) { err = e; }
158 const after = git(bare, ['for-each-ref', '--format=%(refname) %(objectname)']);
159 const at = (text, ref) => text.split('\n').find(l => l.startsWith(`${ref} `)) ?? null;
160 const moved = commands.map(c => c.ref).filter(ref => at(before, ref) !== at(after, ref));
161 // the cell as git's text writes it: git's own ng reasons substituted into the variants' payloads, each checked to be
162 // of the variant's class (git versions word a stale ref differently: "failed to update ref", "incorrect old value provided")
163 const said = new Map([...Buffer.from(captured ?? []).toString('latin1').matchAll(/ng (refs\/\S+) ([^\n]*)\n/g)].map(m => [m[1], m[2]]));
164 const st = space.statuses(cell);
165 const reasonOf = tag => { const i = st.findIndex(v => v.tag === tag); return i < 0 ? undefined : said.get(space.REFS[i]); };
166 const classOk = (tag, r) => r === undefined || ({ ngCas: CAS.test(r), ngHook: !CAS.test(r), ngAtomic: r === 'atomic push failure', ngUnpacker: r === 'unpacker error' })[tag] !== false;
167 const withReason = v => { const r = reasonOf(v.tag) ?? reasons[v.tag]; if (!classOk(v.tag, r)) throw new Error(`${name}: git's reason ${JSON.stringify(r)} is not of class ${v.tag}`); return r === undefined || !('reason' in v) ? v : { ...v, reason: r }; };
168 const withReasons = { ...cell, status: withReason(cell.status), spread: cell.spread.tag === 'mixed' ? { ...cell.spread, odd: withReason(cell.spread.odd) } : cell.spread };
169 const rendered = space.render(withReasons).response;
170 const sideband = cell.framing.tag === 'sideband';
171 const bytesMatch = rendered?.bytes ? Buffer.compare(Buffer.from(normalize(captured ?? new Uint8Array(0), sideband)), Buffer.from(normalize(rendered.bytes, sideband))) === 0 : null;
172 const sent = captured && sideband ? extras(captured) : { band2: 0, kaBefore: 0, kaAfter: 0 };
173 results.push({ name, cell, cellId: space.cellId(cell), expected: space.expected(cell), got: outcomeOf(err, commands), moved, captured, bytesMatch, eventMatch: captured ? eventMatches(cell.event.tag, sent) : true, sent, rendered: rendered?.bytes ?? null, error: err?.message ?? null });
174 rmSync(bare, { recursive: true, force: true });
175 }
176 } finally { await srv.close(); cleanup(); }
177 return results;
Diagnostics4 git calls · 31 ms
CallStatusSizeTime031 ms
discover200152 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=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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=2
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=55
Open pack map
fetch2004.9 KiB · hit26 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5049
git.response.cache=HIT
git.response.duration_ms=26
git.fetch.wants=b176e77042b7ce0a5630db986fc7fd74f0c42ef3
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=5049
git.pkt.payload.max=5028
git.sideband.band1.bytes=5027
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
a5e327bf4e494628
Request
41a57ec0…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format