GitHubivarvong
ply
main
1// Servers that behave like Cloudflare Artifacts (observed 2026-10-23, lab_notebook/ply-artifacts-eval-2026-10-23.md):
2// 1. `fetch=filter` advertised, but every filter except blob:none → HTTP 400 "Unsupported Git fetch filter; only
3// blob:none is supported." ply falls back to blob:none (learned from that answer, remembered on the transport).
4// 2. every ref-update rejection is `ng <ref> stale ref` → must classify as cas_conflict (editFile's fresh retry fires).
5// Mimicked by test/proxy.js in front of git http-backend; git is the judge of every result.
6import { test, beforeAll as before, afterAll as after } from 'vitest';
7import assert from 'node:assert/strict';
8import { join } from 'node:path';
9import { mkdirSync } from 'node:fs';
10import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, commitFiles, fsckProblems, initRepo } from './server.js';
11import { startProxy } from './proxy.js';
12import { editFile } from '../src/edit.js';
13import { readFile, listTree } from '../src/read.js';
14import { mergeBranch } from '../src/merge.js';
15import { rebaseBranch, cherryPick, revertCommit } from '../src/rebase.js';
16import { readLog } from '../src/log.js';
17import { checkoutTree } from '../src/checkout.js';
18import { forgetQuirks } from '../src/transport.js';
19import { classifyReport, decideOutcome } from '../src/push.js';
20import { pkt, FLUSH } from '../src/pktline.js';
21import { concat } from '../src/bytes.js';
22import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
23import { unwrap, unwrapAsync } from './unwrap.js';
25const REJECT = 'Unsupported Git fetch filter; only blob:none is supported.';
26const filterOf = body => /filter ([^\n]+)\n/.exec(Buffer.from(body).toString('latin1'))?.[1] ?? null;
27// Artifacts mode: any filter other than blob:none is a 400 (the upstream's answer is discarded)
28const artifactsFault = (extra = () => null) => ctx => extra(ctx) ?? (ctx.stage === 'fetch' && filterOf(ctx.reqBody) && filterOf(ctx.reqBody) !== 'blob:none' ? { type: 'status', status: 400, contentType: 'text/plain; charset=utf-8', body: REJECT } : { type: 'pass' });
30let root, cleanup, srv, n = 0;
31before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
32after(async () => { await srv.close(); cleanup(); });
33const A = { name: 'c', email: 'c@x' };
34async function repo(files, fault = artifactsFault()) {
35 const name = `a${n++}`; const bare = makeBareRepo(root, name); const work = join(root, `w${name}`); mkdirSync(work); initRepo(work);
36 commitFiles(work, bare, files, 'seed');
37 const proxy = await startProxy(srv.url, fault);
38 return { name, bare, work, url: `${proxy.url}/${name}.git`, proxy };
39}
40const spansOf = ex => ex.lines.flatMap(flattenLine);
41const tree = (bare, rev) => git(bare, ['rev-parse', `${rev}^{tree}`]).trim();
43// the rejection code of an all-ng report (the refs it names are the commands)
44const outcomeOf = lines => decideOutcome(classifyReport(['unpack ok', ...lines].map(l => new TextEncoder().encode(`${l}\n`)), lines.map(l => ({ ref: l.split(' ')[1] })))).code;
45test('stale ref (Artifacts wording) is cas_conflict; git\'s wordings unchanged', () => {
46 assert.equal(outcomeOf(['ng refs/heads/main stale ref']), 'cas_conflict');
47 assert.equal(outcomeOf(['ng refs/heads/a stale ref', 'ng refs/heads/b stale ref']), 'cas_conflict', 'every multi-ref push is atomic on Artifacts');
48 assert.equal(outcomeOf(['ng refs/heads/main failed to update ref']), 'cas_conflict');
49 assert.equal(outcomeOf(['ng refs/heads/main incorrect old value provided']), 'cas_conflict');
50 assert.equal(outcomeOf(['ng refs/heads/main refname conflict']), 'push_rejected');
51 assert.equal(outcomeOf(['ng refs/heads/main pre-receive hook declined']), 'push_rejected');
52 assert.equal(outcomeOf(['ng refs/heads/main stale ref', 'ng refs/heads/x pre-receive hook declined']), 'push_rejected');
53});
55test('editFile: a stale-ref rejection on the first push is retried from a fresh ls-refs and lands', async () => {
56 let pushes = 0;
57 const staleOnce = ctx => {
58 if (ctx.stage !== 'receive-pack' || ctx.phase !== 'request' || pushes++ > 0) return null;
59 const report = concat([unwrap(pkt('unpack ok\n')), unwrap(pkt('ng refs/heads/main stale ref\n')), FLUSH]);
60 return { type: 'bytes', noForward: true, name: 'stale-ref', bytes: concat([unwrap(pkt(concat([new Uint8Array([1]), report]))), FLUSH]) };
61 };
62 const r = await repo({ 'f.txt': 'MARK\n' }, artifactsFault(staleOnce));
63 try {
64 const res = await unwrapAsync(editFile(r.url, { path: 'f.txt', find: 'MARK', replace: 'DONE', author: A, message: 'm' }, { tracer: new Tracer() }));
65 assert.equal(res.attempt, 1, 'retried once');
66 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), res.newCommit);
67 assert.equal(gitBuf(r.bare, ['cat-file', 'blob', 'main:f.txt']).toString(), 'DONE\n');
68 } finally { await r.proxy.close(); }
69});
71test('blob:none fallback: edit (deep path) and read match git; learned once, remembered via quirks, extra bytes on the span', async () => {
72 const files = { 'a/b/c/d/deep.txt': 'deep MARK\n', 'README.md': '# hi\n', 'big.bin': Buffer.alloc(200_000, 7) };
73 for (let i = 0; i < 40; i++) files[`many/d${i}/f.txt`] = `f${i}\n`;
74 const r = await repo(files);
75 try {
76 const quirks = {}; const ex = arrayExporter(); const tracer = new Tracer({ exporter: ex });
77 const res = await unwrapAsync(editFile(r.url, { path: 'a/b/c/d/deep.txt', find: 'MARK', replace: 'EDITED', author: A, message: 'm' }, { tracer, quirks, verify: true }));
78 assert.equal(quirks.treeFilter, 'blob:none');
79 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), res.newCommit);
80 assert.equal(gitBuf(r.bare, ['cat-file', 'blob', 'main:a/b/c/d/deep.txt']).toString(), 'deep EDITED\n');
81 assert.deepEqual(fsckProblems(r.bare), []);
82 const fetches = spansOf(ex).filter(s => s.name === 'git.upload-pack fetch');
83 const wide = fetches.filter(s => s.attrs['git.fetch.filter.fallback'] === 'blob:none');
84 assert.equal(wide.length, 1, 'one wide commit fetch; every tree level came from the harvest');
85 assert.equal(wide[0].attrs['git.fetch.filter.learned'], true);
86 assert.ok(wide[0].attrs['git.fetch.wide.extra_objects'] > 40 && wide[0].attrs['git.fetch.wide.extra_bytes'] > 0, JSON.stringify(wide[0].attrs));
87 assert.equal(wide[0].attrs['git.fetch.wide.blobs'], 0);
88 const rejected = r.proxy.log.filter(l => l.spec === 'status').length;
89 assert.equal(rejected, 1, 'one 400, then remembered');
90 // second operation, same quirks object: no 400 at all; read == git show
91 const before400 = rejected;
92 const rd = await unwrapAsync(readFile(r.url, { path: 'a/b/c/d/deep.txt' }, { tracer: new Tracer(), quirks }));
93 const chunks = []; for await (const c of rd.stream) chunks.push(Buffer.from(c));
94 assert.ok(Buffer.concat(chunks).equals(gitBuf(r.bare, ['show', 'main:a/b/c/d/deep.txt'])));
95 const ls = await unwrapAsync(listTree(r.url, { path: 'many' }, { tracer: new Tracer(), quirks }));
96 assert.equal(ls.entries.length, 40); assert.equal(ls.tree, git(r.bare, ['rev-parse', 'main:many']).trim());
97 assert.equal(r.proxy.log.filter(l => l.spec === 'status').length, before400, 'no further 400s');
98 // without a quirks object: the remote's remembered record (#136). The calls above kept theirs in the caller's
99 // object, so the remote's record learns it once (one more 400), and every later call without one pays none
100 const statuses = () => r.proxy.log.filter(l => l.spec === 'status').length;
101 for (const want of [before400 + 1, before400 + 1]) {
102 const rd2 = await unwrapAsync(readFile(r.url, { path: 'README.md' }, { tracer: new Tracer() }));
103 for await (const _ of rd2.stream); // eslint-disable-line no-unused-vars
104 assert.equal(statuses(), want, 'remembered per remote');
105 }
106 // a caller's own fresh record opts out: it learns again (one more 400)
107 const rd3 = await unwrapAsync(readFile(r.url, { path: 'README.md' }, { tracer: new Tracer(), quirks: {} }));
108 for await (const _ of rd3.stream); // eslint-disable-line no-unused-vars
109 assert.equal(statuses(), before400 + 2);
110 } finally { await r.proxy.close(); }
111});
113test('blob:none fallback: merge, rebase, cherry-pick, revert trees equal git merge-tree', async () => {
114 const r = await repo({ 'x.txt': 'x1\nx2\nx3\n', 'y.txt': 'y1\n', 'd/z.txt': 'z\n' });
115 try {
116 const { work, bare, url } = r; const quirks = {};
117 const { writeFileSync } = await import('node:fs');
118 const commitTo = (ref, files, msg) => { for (const [p, v] of Object.entries(files)) writeFileSync(join(work, p), v); git(work, ['add', '-A']); git(work, ['-c', 'user.name=s', '-c', 'user.email=s@x', 'commit', '-q', '-m', msg]); git(work, ['push', '-q', '-f', bare, `HEAD:${ref}`]); return git(work, ['rev-parse', 'HEAD']).trim(); };
119 const base = git(work, ['rev-parse', 'HEAD']).trim();
120 git(work, ['checkout', '-q', '-b', 'feat']); const f1 = commitTo('refs/heads/feat', { 'x.txt': 'x1\nx2 feat\nx3\n' }, 'feat');
121 git(work, ['checkout', '-q', 'main']);
122 const m1 = commitTo('refs/heads/main', { 'y.txt': 'y1 main\n' }, 'main');
123 const opts = () => ({ tracer: new Tracer(), quirks });
124 const mg = await unwrapAsync(mergeBranch(url, { ref: 'refs/heads/main', from: 'refs/heads/feat', author: A, message: 'merge' }, opts()));
125 assert.equal(mg.outcome, 'merge');
126 assert.equal(mg.tree, git(bare, ['merge-tree', '--write-tree', m1, f1]).trim().split('\n')[0]);
127 assert.equal(tree(bare, 'main'), mg.tree);
128 // rebase feat2 (one commit on base) onto main
129 git(work, ['checkout', '-q', '-b', 'feat2', base]); const f2 = commitTo('refs/heads/feat2', { 'd/z.txt': 'z feat2\n' }, 'feat2');
130 const rb = await unwrapAsync(rebaseBranch(url, { ref: 'refs/heads/feat2', onto: 'refs/heads/main', committer: A }, opts()));
131 assert.equal(rb.outcome, 'rebase');
132 assert.equal(tree(bare, 'feat2'), git(bare, ['merge-tree', '--write-tree', `--merge-base=${base}`, mg.oid, f2]).trim().split('\n')[0]);
133 // cherry-pick f2 onto main, then revert it
134 const cp = await unwrapAsync(cherryPick(url, { ref: 'refs/heads/main', commit: f2, committer: A }, opts()));
135 assert.equal(cp.tree, git(bare, ['merge-tree', '--write-tree', `--merge-base=${base}`, mg.oid, f2]).trim().split('\n')[0]);
136 const rv = await unwrapAsync(revertCommit(url, { ref: 'refs/heads/main', commit: cp.oid, committer: A }, opts()));
137 assert.equal(rv.tree, mg.tree, 'revert restores the pre-pick tree');
138 assert.deepEqual(fsckProblems(bare), []);
139 assert.equal(quirks.treeFilter, 'blob:none');
140 } finally { await r.proxy.close(); }
141});
143test('git-default server: no fallback attributes, tree:0 throughout (the fallback never triggers by itself)', async () => {
144 const r = await repo({ 'a/b.txt': 'MARK\n' }, () => ({ type: 'pass' }));
145 try {
146 const ex = arrayExporter(); const quirks = {};
147 await unwrapAsync(editFile(r.url, { path: 'a/b.txt', find: 'MARK', replace: 'X', author: A, message: 'm' }, { tracer: new Tracer({ exporter: ex }), quirks }));
148 assert.equal(quirks.treeFilter, 'tree:0'); // the caller's record states what the server took
149 for (const s of spansOf(ex).filter(s => s.name === 'git.upload-pack fetch')) assert.ok(!('git.fetch.filter.fallback' in s.attrs));
150 } finally { await r.proxy.close(); }
151});
153// #136: with no quirks passed, the refused filter is asked for at most once per remote, whatever the order of the
154// operations. Counted at the fetchImpl: every upload-pack request that carries `filter tree:0` (Artifacts' 400).
155// The contrast: a fresh `quirks: {}` per operation (each call its own record, ply before #136) pays one per operation.
156const OPS = ['edit', 'read', 'checkout', 'log', 'merge'];
157const orders = [...OPS.map((_, i) => [...OPS.slice(i), ...OPS.slice(0, i)]), [...OPS].reverse()];
158async function sweep(order, perCall) {
159 const r = await repo({ 'a/f.txt': 'MARK\n', 'README.md': '# hi\n' });
160 git(r.work, ['checkout', '-q', '-b', 'feat']); git(r.work, ['rm', '-q', 'README.md']);
161 git(r.work, ['-c', 'user.name=s', '-c', 'user.email=s@x', 'commit', '-q', '-m', 'feat']); git(r.work, ['push', '-q', r.bare, 'HEAD:refs/heads/feat']);
162 let refused = 0;
163 const fetchImpl = (url, init) => {
164 if (String(url).endsWith('/git-upload-pack') && init?.body && Buffer.from(init.body).toString('latin1').includes('filter tree:0')) refused++;
165 return globalThis.fetch(url, init);
166 };
167 const o = () => ({ tracer: new Tracer(), fetchImpl, ...(perCall ? { quirks: {} } : {}) });
168 const run = {
169 edit: async () => assert.ok((await unwrapAsync(editFile(r.url, { path: 'a/f.txt', find: 'MARK', replace: 'DONE', author: A, message: 'e' }, { ...o(), verify: true }))).newCommit),
170 read: async () => { const rd = await unwrapAsync(readFile(r.url, { path: 'a/f.txt' }, o())); for await (const _ of rd.stream); }, // eslint-disable-line no-unused-vars
171 checkout: async () => { const co = await unwrapAsync(checkoutTree(r.url, { ref: 'main' }, o())); for await (const f of co.files) if (f.stream) for await (const _ of f.stream); }, // eslint-disable-line no-unused-vars
172 log: async () => assert.ok((await unwrapAsync(readLog(r.url, { ref: 'main', limit: 5 }, o()))).commits.length >= 1),
173 merge: async () => assert.ok((await unwrapAsync(mergeBranch(r.url, { ref: 'refs/heads/main', from: 'refs/heads/feat', author: A, message: 'm' }, o()))).outcome),
174 };
175 try {
176 for (const op of order) await run[op]();
177 assert.deepEqual(fsckProblems(r.bare), []);
178 return { refused, status: r.proxy.log.filter(l => l.spec === 'status').length };
179 } finally { await r.proxy.close(); }
180}
181test('#136: no quirks passed, any order of edit/read/checkout/log/merge: tree:0 is refused at most once per remote', async () => {
182 forgetQuirks();
183 for (const order of orders) {
184 const { refused, status } = await sweep(order, false);
185 assert.equal(refused, 1, `${order}: one tree:0 request for the remote`);
186 assert.equal(status, 1, `${order}: one 400`);
187 }
188 const before = await sweep(OPS, true);
189 assert.ok(before.refused >= 4, `a fresh record per call pays per operation: ${before.refused}`);
190 assert.equal(before.status, before.refused);
191});
193test('#136: forgetQuirks clears one remote or all; the key never holds the credential', async () => {
194 const { quirksKey, rememberedQuirks } = await import('../src/transport.js');
195 assert.equal(quirksKey('https://user:tok@h.example/o/r.git/?x=1#y'), 'https://h.example/o/r.git');
196 assert.equal(quirksKey('not a url'), null);
197 const q = rememberedQuirks(quirksKey('https://h.example/o/r.git'));
198 q.treeFilter = 'blob:none';
199 assert.equal(rememberedQuirks(quirksKey('https://other:x@h.example/o/r.git/')), q, 'same remote, other credential: same record');
200 forgetQuirks('https://h.example/o/r.git');
201 assert.notEqual(rememberedQuirks(quirksKey('https://h.example/o/r.git')), q);
202 assert.equal(rememberedQuirks(quirksKey('https://h.example/o/r.git')).treeFilter, 'tree:0');
203 forgetQuirks();
204});
Diagnostics6 git calls · 215 ms
CallStatusSizeTime0215 ms
discover200191 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=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 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=UPDATING git.response.duration_ms=3 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=10Open 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=3 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=5323b9266eea5ded637285d1c5320699f12be76eOpen 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2004.7 KiB203 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=203 git.response.bytes=4776 git.response.cache=MISS git.response.duration_ms=203 git.fetch.wants=6d0259225e07227b2dc52428c8d8cc90a900abbb 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=4776 git.pkt.payload.max=4749 git.sideband.band1.bytes=4749 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
- a5e2897a29a24af4
- Request
- f185e855…
- 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