main
1import assert from 'node:assert/strict'
2import { mkdtemp, readFile } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { join } from 'node:path'
5import { test } from 'node:test'
6import { REPO_PREFIX } from '../scripts/cd/baseline.ts'
7import {
8 appendCleanupFile,
9 classifyDelete,
10 cleanupLeakNames,
11 confirmDelete,
12 recordCleanup,
13 reposFromList,
14 sweepLeaks,
15 type DeleteCommand,
16} from '../scripts/cd/cleanup.ts'
17import { asRecord } from '../scripts/cd/json.ts'
18import { parseEvent, RunLog } from '../scripts/cd/log.ts'
19import { DEFAULT_POLICY_PATH, loadPolicy } from '../scripts/cd/policy.ts'
21const TIMEOUT = 'APIError: timeout'
22const ABSENT = 'APIError: 404 [10200] Repository not found'
23const REPO = `${REPO_PREFIX}20261101172943-ab12cd`
25function command(code: number, stderr: string, latencyMs: number): DeleteCommand {
26 return { code, stdout: '', stderr, latencyMs }
27}
29function scripted(commands: readonly DeleteCommand[]): () => Promise<DeleteCommand> {
30 const pending = [...commands]
31 return async () => {
32 const next = pending.shift()
33 if (!next) throw new Error('no more delete commands')
34 return next
35 }
36}
38test('a timeout then a 404 confirms the delete and keeps the 103s latency', async () => {
39 const policy = loadPolicy(DEFAULT_POLICY_PATH).policy.cleanup
40 const waits: number[] = []
41 const result = await confirmDelete(REPO, { attempts: policy.attempts, backoffMs: policy.backoffMs }, {
42 once: scripted([
43 command(1, TIMEOUT, policy.calibration.latencyMs),
44 command(1, ABSENT, 400),
45 ]),
46 sleep: async ms => {
47 waits.push(ms)
48 },
49 })
50 assert.equal(result.kind, 'deleted')
51 assert.equal(result.repo, REPO)
52 assert.equal(result.attempts.length, 2)
53 const first = result.attempts[0]
54 const second = result.attempts[1]
55 if (!first || !second) throw new Error('missing attempt')
56 assert.equal(first.kind, 'transient')
57 assert.equal(first.latencyMs, 103_000)
58 assert.match(first.kind === 'transient' ? first.detail : '', /timeout/)
59 assert.equal(second.kind, 'absent')
60 assert.equal(second.latencyMs, 400)
61 assert.deepEqual(waits, [policy.backoffMs])
62})
64test('a 404 on the first try is deleted, and exit 0 is deleted', () => {
65 const absent = classifyDelete(command(1, ABSENT, 180))
66 assert.equal(absent.kind, 'absent')
67 assert.equal(absent.latencyMs, 180)
68 const gone = classifyDelete(command(1, 'Repository not found', 20))
69 assert.equal(gone.kind, 'absent')
70 const code = classifyDelete(command(1, 'error [10200]', 20))
71 assert.equal(code.kind, 'absent')
72 const deleted = classifyDelete(command(0, TIMEOUT, 103_000))
73 assert.equal(deleted.kind, 'deleted')
74 assert.equal(deleted.latencyMs, 103_000)
75})
77test('a persistent timeout records a cleanup_leak and does not fail a pass', async () => {
78 const policy = loadPolicy(DEFAULT_POLICY_PATH).policy.cleanup
79 const waits: number[] = []
80 const result = await confirmDelete(REPO, { attempts: policy.attempts, backoffMs: policy.backoffMs }, {
81 once: scripted([
82 command(1, TIMEOUT, 103_000),
83 command(1, TIMEOUT, 103_000),
84 command(1, TIMEOUT, 103_000),
85 command(1, ABSENT, 10),
86 ]),
87 sleep: async ms => {
88 waits.push(ms)
89 },
90 })
91 assert.equal(result.kind, 'leak')
92 if (result.kind !== 'leak') return
93 assert.equal(result.repo, REPO)
94 assert.equal(result.attempts.length, policy.attempts)
95 assert.deepEqual(waits, [policy.backoffMs, policy.backoffMs * 2])
96 const dir = await mkdtemp(join(tmpdir(), 'ply-cd-cleanup-'))
97 const ledger = join(dir, 'ledger.jsonl')
98 const runDir = join(dir, 'run')
99 const log = await RunLog.open(runDir, ledger, 'run-cleanup')
100 await recordCleanup(log, result, { attempts: policy.attempts, backoffMs: policy.backoffMs })
101 await appendCleanupFile(runDir, [result.repo])
102 const stored = cleanupLeakNames(JSON.parse(await readFile(join(runDir, 'cleanup.json'), 'utf8')) as unknown)
103 assert.deepEqual(stored, [REPO])
104 const verdict = { kind: 'pass' as const, failures: [] as const, cleanup_leak: stored }
105 assert.equal(verdict.kind, 'pass')
106 assert.deepEqual(verdict.cleanup_leak, [REPO])
107 const lines = (await readFile(ledger, 'utf8')).trim().split('\n')
108 let sawLatency = false
109 let sawLeak = false
110 for (const line of lines) {
111 const event = parseEvent(JSON.parse(line) as unknown)
112 if (event.kind === 'observation') {
113 const measurement = asRecord(event.payload.measurement)
114 if (!measurement) continue
115 if (measurement.source === 'repo-delete' && measurement.latencyMs === 103_000 && measurement.repo === REPO) sawLatency = true
116 if (measurement.source === 'cleanup-leak' && measurement.repo === REPO) sawLeak = true
117 }
118 if (event.kind === 'decision') {
119 assert.equal(event.payload.outcome, 'cleanup_leak')
120 assert.match(event.payload.reason, new RegExp(REPO))
121 assert.match(event.payload.reason, /103000ms transient/)
122 }
123 }
124 assert.equal(sawLatency, true)
125 assert.equal(sawLeak, true)
126})
128test('sweep deletes only stale prefixed repos and records a leak when delete stays unconfirmed', async () => {
129 const now = Date.parse('2026-11-01T17:29:43.122Z')
130 const hour = 60 * 60 * 1000
131 const listed = reposFromList([
132 { name: `${REPO_PREFIX}old`, created_at: new Date(now - hour - 1).toISOString() },
133 { name: `${REPO_PREFIX}fresh`, created_at: new Date(now - 60_000).toISOString() },
134 { name: `${REPO_PREFIX}stuck`, created_at: new Date(now - 2 * hour).toISOString() },
135 { name: `${REPO_PREFIX}gone`, created_at: new Date(now - 3 * hour).toISOString() },
136 { name: 'other-repo', created_at: new Date(now - 3 * hour).toISOString() },
137 { name: `${REPO_PREFIX}undated` },
138 { name: '', created_at: new Date(now - 3 * hour).toISOString() },
139 ])
140 const called: string[] = []
141 const report = await sweepLeaks({
142 listed,
143 nowMs: now,
144 staleMs: hour,
145 prefix: REPO_PREFIX,
146 bound: { attempts: 2, backoffMs: 0 },
147 once: async name => {
148 called.push(name)
149 if (name.endsWith('stuck')) return command(1, TIMEOUT, 103_000)
150 if (name.endsWith('gone')) return command(1, ABSENT, 250)
151 return command(0, '', 30)
152 },
153 sleep: async () => {},
154 note: async () => {},
155 })
156 assert.deepEqual(called.filter((name, index) => called.indexOf(name) === index), [
157 `${REPO_PREFIX}old`,
158 `${REPO_PREFIX}stuck`,
159 `${REPO_PREFIX}gone`,
160 ])
161 assert.deepEqual(report.deleted, [`${REPO_PREFIX}old`, `${REPO_PREFIX}gone`])
162 assert.deepEqual(report.leaks, [`${REPO_PREFIX}stuck`])
163 const stuckCalls = called.filter(name => name.endsWith('stuck'))
164 assert.equal(stuckCalls.length, 2)
165})
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=ls-refs 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 · 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=145 git.response.cache=HIT git.response.duration_ms=3 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 1020040 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=41349 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=526b305930b8ef382f2d7a1f9c0238730a585921 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=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2002.0 KiB · hit25 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=2079 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=805a6953b9d5147fa43b658c755c34360169f76e 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=2079 git.pkt.payload.max=2058 git.sideband.band1.bytes=2057 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-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2fa576ecf90c0
- Request
- 0c878640…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format