GitHubivarvong
ply
main
1// ply#274: the fetch filter-refusal predicate and readConfig's key/value line, both rewritten to run in linear time.
2//
3// The oracle for WHAT each one classifies is the pattern main (8e3939d) applied, run here by the regex engine on short
4// inputs where its cost does not matter, plus the real refusal texts (Cloudflare Artifacts, observed 2026-10-23; git's
5// upload-pack, upload-pack.c "filter '%s' not supported") and `git config --file … --list` for the config forms ply reads.
6// The oracle for the COST is a ratio of two times measured the same way (the issue's generator), never a time.
7import { spawnSync } from 'node:child_process';
8import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
9import { tmpdir } from 'node:os';
10import { join } from 'node:path';
11import { test } from 'vitest';
12import assert from 'node:assert/strict';
13import { isFilterRefusal, isFilterRejection } from '../src/fetch.js';
14import { readConfig } from '../src/repo-node.js';
16const OLD_FILTER = /only blob:none is supported|unsupported (git )?fetch filter|filter .* not (supported|allowed)/i;
17const OLD_KV = /^([A-Za-z][\w-]*)\s*(=\s*(.*))?$/u;
19test('the refusal texts servers really send are refusals at 400 and nothing else', () => {
20 const real = [
21 'Unsupported Git fetch filter; only blob:none is supported.',
22 'HTTP 400 git-upload-pack: Unsupported Git fetch filter; only blob:none is supported.',
23 "filter 'tree' not supported",
24 "filter 'sparse:oid' not supported",
25 "git upload-pack: filter 'blob:limit' not supported",
26 'filter blob:limit=1 not allowed',
27 'filter tree:0 not supported',
28 'unsupported fetch filter',
29 'unsupported git fetch filter',
30 ];
31 for (const t of real) {
32 assert.equal(isFilterRefusal(400, t), true, t);
33 assert.equal(isFilterRefusal(500, t), false, t);
34 assert.equal(isFilterRejection({ code: 'remote_error', message: t }), true, t);
35 }
36 for (const t of ['filter not allowed', 'filter\nnot supported', 'filter x\r not supported', 'not supported filter ', 'fetch filter=blob:none', '']) {
37 assert.equal(isFilterRefusal(400, t), false, JSON.stringify(t));
38 }
39});
41test('isFilterRefusal classifies every message of up to 4 tokens exactly as main\'s regex did', () => {
42 const tokens = ['filter ', 'FiLtEr ', ' not supported', ' NOT Allowed', ' not', ' ', 'x', '\n', '\r', '
', '
', '\0',
43 'unsupported git fetch filter', 'Unsupported Git fetch filter', 'only blob:none is supported', 'ONLY blob:None is supported', 'İ', 'K', 'filte'];
44 let checked = 0;
45 const walk = (text, left) => {
46 assert.equal(isFilterRefusal(400, text), OLD_FILTER.test(text), JSON.stringify(text));
47 checked += 1;
48 if (left > 0) for (const t of tokens) walk(text + t, left - 1);
49 };
50 walk('', 4);
51 assert.ok(checked > 137000); // 1 + 19 + 19^2 + 19^3 + 19^4 = 137,561
52});
54// readConfig as main had it, the config-line regex the only change.
55const oldReadConfig = text => {
56 const out = new Map();
57 let section = '';
58 for (const raw of text.split('\n')) {
59 const line = raw.trim();
60 const head = /^\[([^\]\s"]+)(\s+"([^"]*)")?\]$/u.exec(line);
61 if (head !== null) { section = head[3] !== undefined ? `${head[1].toLowerCase()}.${head[3]}` : head[1].toLowerCase(); continue; }
62 const kv = OLD_KV.exec(line);
63 if (kv !== null && section !== '') out.set(`${section}.${kv[1].toLowerCase()}`, (kv[3] ?? 'true').trim());
64 }
65 return out;
66};
68test('readConfig reads every key/value line of up to 6 characters exactly as main\'s regex did', () => {
69 const alphabet = ['a', '=', ' ', '\t', '\r', '
', '
', ' ', 'x', '-'];
70 let checked = 0;
71 const walk = (line, left) => {
72 if (!line.includes('#') && !line.includes(';')) {
73 assert.deepEqual([...readConfig(`[s]\n${line}`)], [...oldReadConfig(`[s]\n${line}`)], JSON.stringify(line));
74 checked += 1;
75 }
76 if (left > 0) for (const c of alphabet) walk(line + c, left - 1);
77 };
78 walk('', 6);
79 assert.ok(checked > 1000000);
80});
82test('readConfig agrees with git config --list on the forms ply reads', () => {
83 const dir = mkdtempSync(join(tmpdir(), 'ply-274-'));
84 try {
85 const file = join(dir, 'config');
86 const git = (...a) => spawnSync('git', ['config', '--file', file, ...a], { encoding: 'utf8' });
87 git('core.bare', 'false');
88 git('extensions.objectFormat', 'sha1');
89 git('extensions.refStorage', 'files');
90 git('remote.origin.url', 'https://example.com/a b.git');
91 git('remote.origin.fetch', '+refs/heads/*:refs/remotes/origin/*');
92 git('branch.main.remote', 'origin');
93 const text = `${readFileSync(file, 'utf8')}\tbare2 = spaced value \n\tflag\n`;
94 const listed = spawnSync('git', ['config', '-f', '-', '--list'], { input: text, encoding: 'utf8' }).stdout.trim().split('\n')
95 .map(l => { const i = l.indexOf('='); return i < 0 ? [l, 'true'] : [l.slice(0, i), l.slice(i + 1)]; });
96 const ours = readConfig(text);
97 for (const [k, v] of listed) {
98 const dot = k.lastIndexOf('.');
99 assert.equal(ours.get(`${k.slice(0, dot)}.${k.slice(dot + 1).toLowerCase()}`), v, k);
100 }
101 } finally {
102 rmSync(dir, { recursive: true, force: true });
103 }
104});
106const ratio = (f, gen, n) => {
107 const time = m => { const s = gen(m); let best = Infinity; for (let i = 0; i < 7; i++) { const t0 = performance.now(); f(s); best = Math.min(best, performance.now() - t0); } return best; };
108 return time(4 * n) / time(n);
109};
111test('both stay linear on the issue\'s generators (4x the input costs well under 16x the time)', () => {
112 for (const [name, f, gen] of [
113 ['filter', s => isFilterRefusal(400, s), n => 'filter '.repeat(n)],
114 ['config', s => readConfig(`[s]\n${s}`), n => `a=${' '.repeat(n)}x\ry`],
115 ]) {
116 let best = Infinity;
117 for (let a = 0; a < 3 && best >= 9; a++) best = Math.min(best, ratio(f, gen, 20000));
118 assert.ok(best < 9, `${name}: ${best.toFixed(1)}x`);
119 }
120});
122// Every BMP code unit put into each slot of a refusal and of a near-miss, against main's regex: a hand case fold or a
123// line-end test that is off for one code unit (a control character that ORs to a space, a line separator) shows here.
124test('isFilterRefusal agrees with main\'s regex on every single-code-unit edit of a refusal', () => {
125 const bases = ['filter x not supported', 'filter a\nfilter x not allowed'];
126 let n = 0;
127 for (let c = 0; c < 0x10000; c++) {
128 const ch = String.fromCharCode(c);
129 for (const base of bases) for (let i = 0; i <= base.length; i++) for (const t of [base.slice(0, i) + ch + base.slice(i), base.slice(0, i) + ch + base.slice(i + 1)]) {
130 if (isFilterRefusal(400, t) !== OLD_FILTER.test(t)) assert.fail(JSON.stringify(t));
131 n += 1;
132 }
133 }
134 assert.ok(n > 6000000);
135});
137// The common case is a non-refusal: an HTTP 400 page. Its cost is compared with main's regex on the same body, measured
138// the same way (interleaved, best of rounds), so the bound is a ratio, never a time.
139test('a typical 4 KiB non-refusal 400 body costs about what main\'s regex did (under 10x)', () => {
140 const html = `${'<html><body>Bad Request: the request could not be processed by this server. ' + 'x'.repeat(60)}\n`.repeat(30).slice(0, 4096);
141 const each = (f, k) => { const t0 = performance.now(); for (let i = 0; i < k; i++) f(); return performance.now() - t0; };
142 let ours = Infinity;
143 let main = Infinity;
144 for (let r = 0; r < 15; r++) {
145 ours = Math.min(ours, each(() => isFilterRefusal(400, html), 500));
146 main = Math.min(main, each(() => OLD_FILTER.test(html), 500));
147 }
148 assert.equal(isFilterRefusal(400, html), false);
149 assert.ok(ours / main < 10, `${(ours / main).toFixed(1)}x main`);
150});
Diagnostics6 git calls · 199 ms
CallStatusSizeTime0199 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 · hit3 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=HIT 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 · 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=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=4 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=187 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=4 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
fetch2003.0 KiB187 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3097 git.response.cache=MISS git.response.duration_ms=187 git.fetch.wants=19e19624f31505bbe073ada4bb65338f033e13f1 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=3097 git.pkt.payload.max=3070 git.sideband.band1.bytes=3070 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
- a5e13e373e261293
- Request
- 85742faa…
- 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