Artifactspublic-v0

ply

main
ply4 git calls400 ms42 KiB2 cachedprotocol v2
188 lines13 KiBRaw
1// The errors-as-values layer: Result helpers, the PlyError constructor, and limits' value API next to its throwing
2// wrappers. Exact values and boundaries, so a mutant of any comparison or field fails here (stryker: vitest.stryker).
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { ok, err, map, mapErr, andThen, match, matchKind, matchOption, assertNever, errorKind, errorKindAttr, some, none, isSome, isNone, resultToOption, optionToResult, transposeResultOption, transposeOptionResult } from '../src/result.js';
6import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
7import { fail, plyError, isPlyCode, retryPolicy, isRetryable, CODES, RETRY } from '../src/errors.js';
8import { checkLimit, parseLimits, limitsOf, lowered, limitExceeded, limitError, withLimits, DEFAULT_LIMITS, WORKER_LIMITS } from '../src/limits.js';
9import { unwrap } from './unwrap.js';
11test('Result: ok/err shapes, map/mapErr/andThen short-circuit, match dispatches on code, assertNever throws', () => {
12 assert.deepEqual(ok(1), { ok: true, value: 1 });
13 assert.deepEqual(err('e'), { ok: false, error: 'e' });
14 assert.deepEqual(map(ok(2), n => n * 3), { ok: true, value: 6 });
15 const e = err('x');
16 assert.equal(map(e, () => assert.fail('mapped an Err')), e);
17 assert.deepEqual(mapErr(err('a'), s => `${s}!`), { ok: false, error: 'a!' });
18 const o = ok(1);
19 assert.equal(mapErr(o, () => assert.fail('mapped an Ok error')), o);
20 assert.deepEqual(andThen(ok(2), n => ok(n + 1)), { ok: true, value: 3 });
21 assert.deepEqual(andThen(ok(2), () => err('late')), { ok: false, error: 'late' });
22 assert.equal(andThen(e, () => assert.fail('continued past an Err')), e);
23 assert.equal(match({ code: 'a', n: 4 }, { a: u => u.n + 1, b: () => 0 }), 5);
24 assert.equal(match({ code: 'b' }, { a: () => 1, b: () => 2 }), 2);
25 assert.equal(matchKind({ kind: 'a', n: 4 }, { a: u => u.n + 1, b: () => 0 }), 5);
26 assert.equal(matchKind({ kind: 'b' }, { a: () => 1, b: () => 2 }), 2);
27 assert.throws(() => assertNever('z'), { message: 'unreachable: z' });
28});
30test('errorKind is the span attribute: miss, then kind, then reason, then code', () => {
31 assert.equal(errorKind({ code: 'not_found', miss: { kind: 'path' }, kind: 'other', reason: 'short_read' }), 'path');
32 assert.equal(errorKind({ code: 'bad_request', kind: 'not_string', reason: 'short_read' }), 'not_string');
33 assert.equal(errorKind({ code: 'remote_error', reason: 'not_our_ref' }), 'not_our_ref');
34 assert.equal(errorKind({ code: 'timeout' }), 'timeout');
35 assert.equal(errorKind({ miss: { kind: 1 }, code: 'not_found' }), 'not_found');
36 assert.equal(errorKind({ code: 7 }), 'Error');
37 assert.equal(errorKind(null), 'Error');
38 assert.equal(errorKind('x'), 'Error');
39 const e = { code: 'not_found', miss: { kind: 'head_absent' } };
40 assert.deepEqual(errorKindAttr(e), { 'error.kind': 'head_absent' });
41 const ex = arrayExporter();
42 const span = new Tracer({ exporter: ex }).start('git.result.kind');
43 span.set(errorKindAttr(e)).error(e, e.code).end();
44 const flat = flattenLine(ex.lines[0])[0];
45 assert.equal(flat.attrs['error.kind'], 'head_absent');
46 assert.equal(flat.attrs['error.type'], 'not_found');
47 assert.equal(flat.name, 'git.result.kind');
48});
50test('Option is some or none, and it composes with Result', () => {
51 assert.deepEqual(some(1), { tag: 'some', value: 1 });
52 assert.deepEqual(none, { tag: 'none' });
53 assert.equal(isSome(some(1)), true);
54 assert.equal(isNone(none), true);
55 assert.equal(isSome(none), false);
56 assert.equal(matchOption(some(2), { some: n => n + 1, none: () => 0 }), 3);
57 assert.equal(matchOption(none, { some: () => 1, none: () => 2 }), 2);
58 assert.deepEqual(resultToOption(ok(3)), some(3));
59 assert.equal(resultToOption(err('e')), none);
60 assert.deepEqual(optionToResult(some(4), 'missing'), ok(4));
61 assert.deepEqual(optionToResult(none, 'missing'), err('missing'));
62 assert.deepEqual(transposeResultOption(ok(some(5))), some(ok(5)));
63 assert.equal(transposeResultOption(ok(none)), none);
64 assert.deepEqual(transposeResultOption(err('e')), some(err('e')));
65 assert.deepEqual(transposeOptionResult(some(ok(6))), ok(some(6)));
66 assert.deepEqual(transposeOptionResult(some(err('e'))), err('e'));
67 assert.deepEqual(transposeOptionResult(none), ok(none));
68});
70// Span.error writes error.kind itself. A caller who stamped a stale kind is overwritten, so the attribute is the value.
71test('Span.error records error.kind beside error.type', () => {
72 const cells = [
73 [{ code: 'not_found', miss: { kind: 'head_absent' }, kind: 'other', reason: 'short_read' }, 'not_found', 'head_absent'],
74 [{ code: 'bad_request', kind: 'not_string', reason: 'short_read' }, 'bad_request', 'not_string'],
75 [{ code: 'remote_error', reason: 'not_our_ref' }, 'remote_error', 'not_our_ref'],
76 [{ code: 'timeout' }, 'timeout', 'timeout'],
77 [{ miss: { kind: 1 }, code: 'not_found' }, 'not_found', 'not_found'],
78 ['plain', undefined, 'Error'],
79 [null, 'bad_request', 'Error'],
80 ];
81 for (const [value, type, kind] of cells) {
82 const ex = arrayExporter();
83 const span = new Tracer({ exporter: ex }).start('git.otel.kind');
84 span.set({ 'error.kind': 'stale' }).error(value, type).end();
85 const flat = flattenLine(ex.lines[0])[0];
86 assert.equal(flat.attrs['error.kind'], kind, kind);
87 assert.equal(flat.attrs['error.type'], type ?? 'Error');
88 assert.equal(flat.name, 'git.otel.kind');
89 }
90});
92test('PlyError: one constructor, an Error carrying its fields; fail wraps it in an Err', () => {
93 const e = plyError('http', 'HTTP 502', { status: 502 });
94 assert.ok(e instanceof Error);
95 assert.equal(e.code, 'http');
96 assert.equal(e.message, 'HTTP 502');
97 assert.equal(e.status, 502);
98 const r = fail('timeout', 't');
99 assert.equal(r.ok, false);
100 assert.equal(r.error.code, 'timeout');
101 assert.equal(r.error.message, 't');
102 assert.throws(() => plyError('nope', 'x'), { message: 'internal: unknown error code nope' });
103 assert.deepEqual(CODES, Object.keys(RETRY));
104 assert.ok(Object.isFrozen(CODES) && Object.isFrozen(RETRY));
105 assert.equal(isPlyCode('network'), true);
106 assert.equal(isPlyCode('nope'), false);
107 assert.equal(isPlyCode(7), false);
108 assert.equal(retryPolicy({ code: 'cas_conflict' }), 'fresh');
109 assert.equal(retryPolicy({ code: 'nope' }), 'no');
110 assert.equal(retryPolicy(null), 'no');
111 assert.equal(isRetryable({ code: 'network' }), true);
112 assert.equal(isRetryable({ code: 'ambiguous' }), false);
113});
115test('limits: checkLimit/parseLimits accept exactly the finite non-negative numbers; the error is an Error with its fields', () => {
116 const bad = (what, got) => ({ ok: false, error: { code: 'bad_request', message: `limits.${what} must be a finite non-negative number`, what, got } });
117 const shape = r => (r.ok ? r : { ok: false, error: { code: r.error.code, message: r.error.message, what: r.error.what, got: r.error.got } });
118 for (const v of [0, 1, 0.5, Number.MAX_SAFE_INTEGER]) {
119 assert.deepEqual(checkLimit(v, 'k'), { ok: true, value: v });
120 assert.equal(unwrap(checkLimit(v, 'k')), v);
121 }
122 for (const v of [-1, -0.5, Number.NaN, Infinity, -Infinity, '5', undefined, null]) {
123 assert.deepEqual(shape(checkLimit(v, 'k')), bad('k', v));
124 assert.throws(() => unwrap(checkLimit(v, 'k')), x => x instanceof Error && x.code === 'bad_request' && x.what === 'k' && Object.is(x.got, v));
125 }
126 // parseLimits (#156): a key given is checked as checkLimit checks it; a key left out (or the missing value) is the
127 // default; null is a value, refused, on every key (#271); keys it does not read are
128 // ignored; the record is total, frozen and checked.
129 assert.equal(unwrap(parseLimits(DEFAULT_LIMITS)), DEFAULT_LIMITS, 'a checked record is returned as it is');
130 assert.deepEqual({ ...unwrap(parseLimits({})) }, { ...DEFAULT_LIMITS });
131 assert.deepEqual({ ...unwrap(parseLimits(null)) }, { ...DEFAULT_LIMITS });
132 assert.deepEqual({ ...unwrap(parseLimits({ maxRefs: undefined })) }, { ...DEFAULT_LIMITS });
133 assert.deepEqual({ ...unwrap(parseLimits({ maxRefs: 3, other: -1 })) }, { ...DEFAULT_LIMITS, maxRefs: 3 });
134 assert.equal(Object.isFrozen(unwrap(parseLimits({ maxRefs: 3 }))), true);
135 assert.deepEqual(shape(parseLimits({ maxRefs: null })), bad('maxRefs', null));
136 for (const key of Object.keys(DEFAULT_LIMITS)) {
137 for (const v of [-1, Number.NaN, Infinity, '5', null]) {
138 assert.deepEqual(shape(parseLimits({ [key]: v })), bad(key, v), key);
139 }
140 // 0 on every key at once: a record with maxInflatedTotal 0 may set any key to 0 (maxResolveWork >= maxInflatedTotal)
141 assert.equal(unwrap(parseLimits({ maxInflatedTotal: 0, [key]: 0 }))[key], 0, key);
142 }
143 // left out, maxResolveWork is derived: by hand, 8 x maxInflatedTotal (256 MiB by default, 1 MiB given) = 2,147,483,648 and 8,388,608
144 assert.equal(unwrap(parseLimits({})).maxResolveWork, 2_147_483_648);
145 assert.equal(unwrap(parseLimits({ maxInflatedTotal: 1_048_576 })).maxResolveWork, 8_388_608);
146 // derived, it is never below maxInflatedTotal, so a record that leaves maxResolveWork out is never refused for it.
147 // By hand: the cap on 8 x is 2^53 - 1 = 9,007,199,254,740,991 (about 9.007e15). 1e18 is above the cap, so its derived
148 // value is 1e18 itself (main gave the cap, 9.007e15, which is below 1e18); Number.MAX_VALUE (8 x it is Infinity) is
149 // itself too. 2^50 x 8 = 2^53, one past the cap, and 2^50 is below the cap: derived is the cap, 9,007,199,254,740,991.
150 assert.equal(unwrap(parseLimits({ maxInflatedTotal: 1e18 })).maxResolveWork, 1e18);
151 assert.equal(unwrap(parseLimits({ maxInflatedTotal: Number.MAX_VALUE })).maxResolveWork, Number.MAX_VALUE);
152 assert.equal(unwrap(parseLimits({ maxInflatedTotal: 2 ** 50 })).maxResolveWork, 9_007_199_254_740_991);
153 assert.equal(unwrap(parseLimits(withLimits({ maxInflatedTotal: 1e18 }))).maxResolveWork, 1e18);
154 // withLimits does not bring the null sentinel back (#271): null is a value there too, refused by parseLimits
155 assert.deepEqual(shape(parseLimits(withLimits({ maxResolveWork: null }))), bad('maxResolveWork', null));
156 // the refusal names maxResolveWork and quotes the bound it fell under (the README Limits contract)
157 const below = (got, total) => ({ ok: false, error: { code: 'bad_request', message: `limits.maxResolveWork must be at least limits.maxInflatedTotal (${total})`, what: 'maxResolveWork', got } });
158 // #271: maxResolveWork below the record's own maxInflatedTotal bounds nothing the resolver would keep; refused, not raised
159 for (const x of [1_048_576, 268_435_456]) {
160 assert.deepEqual(shape(parseLimits({ maxInflatedTotal: x, maxResolveWork: x - 1 })), below(x - 1, x));
161 assert.equal(unwrap(parseLimits({ maxInflatedTotal: x, maxResolveWork: x })).maxResolveWork, x);
162 }
163 // against the default maxInflatedTotal (256 MiB) when the record leaves it out, and against a spread preset's
164 assert.deepEqual(shape(parseLimits({ maxResolveWork: 1 })), below(1, 268_435_456));
165 assert.deepEqual(shape(parseLimits({ ...WORKER_LIMITS, maxInflatedTotal: 1_073_741_824 })), below(536_870_912, 1_073_741_824));
166 // the first bad key in DEFAULT_LIMITS order is the refusal, whatever order the caller wrote
167 const keys = Object.keys(DEFAULT_LIMITS);
168 assert.equal(parseLimits({ [keys.at(-1)]: -1, [keys[0]]: -1 }).error.what, keys[0]);
169 // the brand is the parser's: a spread (or a hand-built total record) is a plain record again, parsed afresh
170 const checked = unwrap(parseLimits({ maxRefs: 3 }));
171 assert.equal(unwrap(parseLimits(checked)), checked);
172 const spread = { ...checked, maxRefs: -1 };
173 assert.deepEqual(shape(parseLimits(spread)), bad('maxRefs', -1));
174 assert.notEqual(unwrap(parseLimits({ ...checked })), checked);
175 assert.deepEqual(Object.keys(checked), keys, 'the brand is not an enumerable key');
176 // lowered: one bound, never raised, never below zero, still checked
177 const low = lowered(checked, 'maxRefs', 2);
178 assert.deepEqual([low.maxRefs, lowered(checked, 'maxRefs', 9).maxRefs, lowered(checked, 'maxRefs', -5).maxRefs, low.maxObjectSize], [2, 3, 0, DEFAULT_LIMITS.maxObjectSize]);
179 assert.equal(unwrap(parseLimits(low)), low);
180 // limitsOf: an entry's options; left out or null is the defaults
181 assert.equal(unwrap(limitsOf({})), DEFAULT_LIMITS);
182 assert.deepEqual({ ...unwrap(limitsOf({ limits: null })) }, { ...DEFAULT_LIMITS });
183 assert.equal(unwrap(limitsOf({ limits: WORKER_LIMITS })), WORKER_LIMITS);
184 assert.deepEqual(shape(limitsOf({ limits: { maxRefs: -2 } })), bad('maxRefs', -2));
185 const x = limitExceeded('refs', 11, 10);
186 assert.deepEqual({ code: x.code, message: x.message, what: x.what, got: x.got, max: x.max }, { code: 'limit_exceeded', message: 'refs 11 exceeds limit 10', what: 'refs', got: 11, max: 10 });
187 assert.equal(limitError, limitExceeded);
188});
Diagnostics4 git calls · 400 ms
CallStatusSizeTime0400 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=395
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2003.8 KiB395 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3851
git.response.cache=MISS
git.response.duration_ms=395
git.fetch.wants=ec540f95632e8a886514113bb1f9b30dd581ad7f
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=3851
git.pkt.payload.max=3830
git.sideband.band1.bytes=3829
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
a5e2138608f3c6f8
Request
d5bdc488…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format