main
1import assert from 'node:assert/strict'
2import { existsSync } from 'node:fs'
3import { mkdir, mkdtemp, readdir, readFile, writeFile } from 'node:fs/promises'
4import { tmpdir } from 'node:os'
5import { join } from 'node:path'
6import { test } from 'node:test'
7import { gunzipSync } from 'node:zlib'
8import { BLOB_DIR, RunLog } from '../scripts/cd/log.ts'
9import { planRetention, pruneRuns, runStartedAt, statusFromEvents, type RetentionPolicy, type RunEntry } from '../scripts/cd/retention.ts'
11async function temp(): Promise<string> {
12 return mkdtemp(join(tmpdir(), 'ply-cd-retention-'))
13}
15const PAGE = JSON.stringify({ events: Array.from({ length: 200 }, (_, i) => ({ id: i, wall: 12.5 })) }, null, 2)
17async function pollingRun(dir: string, code: number, rolledBack = false): Promise<RunLog> {
18 const log = await RunLog.open(dir, join(dir, '..', 'ledger.jsonl'), 'run')
19 await log.started({ config: {}, tools: { node: 'v24', cf: 'cf', git: 'git' }, plySha: null, plyWebSha: 'abc', host: 'box' })
20 for (let i = 0; i < 3; i++) {
21 await log.noteCommand({ argv: ['cf', 'o11y', 'telemetry', 'query'], cwd: '/w', exitCode: 0, durationMs: 1, stdout: PAGE, stderr: '' })
22 }
23 const ref = await log.observation({ measurement: { source: 'ramp-step', nextRequests: 200 }, query: {}, from: 0, to: 1, raw: JSON.stringify({ pages: [log.digest(PAGE)] }) })
24 await log.decision({ inputs: [ref], rules: [{ name: 'fast_burn', value: 14.4 }], outcome: code === 0 ? 'advance' : 'rollback', reason: 'r' })
25 if (rolledBack) log.patch({ rolledBack: true, rollbackReason: 'burn' })
26 await log.finish(code, code === 0 ? null : 'boom')
27 return log
28}
30test('identical captures are stored once, gzipped, and the digest is the blob name', async () => {
31 const dir = join(await temp(), 'run')
32 const log = await pollingRun(dir, 1)
33 const blobs = await readdir(join(dir, BLOB_DIR))
34 // Three identical pages, one empty stderr, one observation body.
35 assert.equal(blobs.length, 3)
36 const command = log.recorded().find(event => event.kind === 'command')
37 assert.ok(command && command.kind === 'command')
38 assert.equal(command.payload.stdoutPath, `${BLOB_DIR}/${command.payload.stdoutDigest}.gz`)
39 assert.equal(command.payload.stdoutDigest, log.digest(PAGE))
40 const stored = gunzipSync(await readFile(join(dir, command.payload.stdoutPath))).toString('utf8')
41 assert.equal(stored, PAGE)
42 assert.ok((await readFile(join(dir, command.payload.stdoutPath))).length < PAGE.length / 5)
43 assert.equal(existsSync(join(dir, 'captures')), false)
44 assert.equal(existsSync(join(dir, 'raw')), false)
45})
47test('a clean pass drops its blobs at seal; a failure or a rollback keeps them', async () => {
48 const root = await temp()
49 for (const [name, code, rolledBack, kept] of [['pass', 0, false, false], ['fail', 1, false, true], ['rolled', 0, true, true]] as const) {
50 const dir = join(root, name)
51 const log = await pollingRun(dir, code, rolledBack)
52 assert.equal(log.keepsBlobs, kept)
53 await log.seal()
54 assert.equal(existsSync(join(dir, BLOB_DIR)), kept, name)
55 const manifest = JSON.parse(await readFile(join(dir, 'manifest.json'), 'utf8')) as { blobs: string; files: Record<string, string> }
56 assert.equal(manifest.blobs, kept ? 'kept' : 'dropped')
57 assert.equal(Object.keys(manifest.files).some(file => file.startsWith(`${BLOB_DIR}/`)), kept)
58 assert.ok(manifest.files['events.jsonl'])
59 }
60})
62test('status comes from the last run_finished; a rollback is a failure', () => {
63 const line = (payload: object) => JSON.stringify({ seq: 1, kind: 'lifecycle', payload })
64 assert.equal(statusFromEvents(line({ event: 'run_started' })), 'unfinished')
65 assert.equal(statusFromEvents(`${line({ event: 'run_started' })}\n${line({ event: 'run_finished', verdict: 'pass', rolledBack: false })}\n`), 'pass')
66 assert.equal(statusFromEvents(line({ event: 'run_finished', verdict: 'pass', rolledBack: true })), 'fail')
67 assert.equal(statusFromEvents(line({ event: 'run_finished', verdict: 'fail', rolledBack: false })), 'fail')
68 assert.equal(statusFromEvents('{"partial line from a tail read"\n'), 'unfinished')
69 assert.equal(runStartedAt('2026-11-07T17-51-47-638Z'), Date.parse('2026-11-07T17:51:47.638Z'))
70})
72test('the plan keeps the newest passes and failures whole, compacts the next, removes the rest', () => {
73 const policy: RetentionPolicy = { keepPass: 2, keepFail: 1, keepSummaries: 2, graceMs: 3600_000 }
74 const at = (minute: number) => `2026-11-07T10-${String(minute).padStart(2, '0')}-00-000Z`
75 const now = Date.parse('2026-11-07T10:59:00.000Z')
76 const entries: RunEntry[] = [
77 { name: at(10), status: 'pass', compacted: false },
78 { name: at(11), status: 'fail', compacted: false },
79 { name: at(12), status: 'pass', compacted: true },
80 { name: at(13), status: 'unfinished', compacted: false },
81 { name: at(14), status: 'pass', compacted: false },
82 { name: at(15), status: 'fail', compacted: false },
83 { name: at(16), status: 'pass', compacted: false },
84 { name: at(17), status: 'pass', compacted: false },
85 { name: at(18), status: 'unfinished', compacted: false },
86 { name: 'not-a-run', status: 'pass', compacted: false },
87 ]
88 const plan = planRetention(entries, { ...policy, graceMs: 45 * 60_000 }, now, new Set([at(10)]))
89 // 18: unfinished inside the grace window. 10: excluded (being resumed).
90 assert.deepEqual(plan.keep, [at(18), at(15), at(10)])
91 assert.deepEqual(plan.strip, [at(17), at(16)])
92 // 13 is past grace, so it is a failure; the one whole failure slot went to 15.
93 assert.deepEqual(plan.compact, [at(14), at(13)])
94 assert.deepEqual(plan.remove, [at(12), at(11)])
95})
97test('pruneRuns strips, compacts, and removes on disk, and the ledger is never touched', async () => {
98 const root = await temp()
99 const ledger = join(root, 'ledger.jsonl')
100 const names = ['2026-11-01T00-00-00-000Z', '2026-11-02T00-00-00-000Z', '2026-11-03T00-00-00-000Z', '2026-11-04T00-00-00-000Z']
101 for (const name of names) {
102 const dir = join(root, 'runs', name)
103 const log = await RunLog.open(dir, ledger, name)
104 await log.started({ config: {}, tools: { node: 'v24', cf: 'cf', git: 'git' }, plySha: null, plyWebSha: 'abc', host: 'box' })
105 await log.finish(0, null)
106 await log.seal()
107 // The pre-2026-11-07 layout: plain captures and raw copies.
108 await mkdir(join(dir, 'captures'), { recursive: true })
109 await writeFile(join(dir, 'captures', '1.stdout'), 'x'.repeat(10_000))
110 await mkdir(join(dir, 'raw'), { recursive: true })
111 await writeFile(join(dir, 'raw', '2.json'), 'y'.repeat(10_000))
112 await mkdir(join(dir, 'visual'), { recursive: true })
113 await writeFile(join(dir, 'visual', 'a.png'), 'png')
114 await writeFile(join(dir, 'verdict.json'), '{"pass":true}\n')
115 await writeFile(join(dir, 'live-smoke.json'), '{"failed":0}\n')
116 }
117 await mkdir(join(root, 'runs', `${names[0]}.debrief`))
118 const ledgerBefore = await readFile(ledger, 'utf8')
119 const policy: RetentionPolicy = { keepPass: 1, keepFail: 1, keepSummaries: 2, graceMs: 0 }
120 const report = await pruneRuns(join(root, 'runs'), [], policy)
121 assert.deepEqual(report.strip, [names[3]])
122 assert.deepEqual(report.compact, [names[2], names[1]])
123 assert.deepEqual(report.remove, [names[0]])
124 assert.ok(report.bytesFreed > 50_000)
125 const runs = join(root, 'runs')
126 assert.deepEqual((await readdir(join(runs, names[3]))).sort(), ['events.jsonl', 'live-smoke.json', 'manifest.json', 'verdict.json', 'visual'])
127 assert.deepEqual((await readdir(join(runs, names[2]))).sort(), ['live-smoke.json', 'pruned.json', 'verdict.json'])
128 const marker = JSON.parse(await readFile(join(runs, names[2], 'pruned.json'), 'utf8')) as { status: string; removed: string[] }
129 assert.equal(marker.status, 'pass')
130 assert.deepEqual(marker.removed, ['captures', 'events.jsonl', 'manifest.json', 'raw', 'visual'])
131 assert.equal(existsSync(join(runs, names[0])), false)
132 assert.equal(existsSync(join(runs, `${names[0]}.debrief`)), false)
133 assert.equal(await readFile(ledger, 'utf8'), ledgerBefore)
134 // A second pass is stable: compacted runs keep their status and stay.
135 const again = await pruneRuns(runs, [], policy)
136 assert.deepEqual(again.compact, [])
137 assert.deepEqual(again.remove, [])
138 assert.deepEqual(again.keep, [names[2], names[1]])
139 assert.equal(again.bytesFreed, 0)
140})
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 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 · hit7 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=7 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.7 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=2765 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=56bd1722c145d0639493f784f11477771e499c8b 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=2765 git.pkt.payload.max=2744 git.sideband.band1.bytes=2743 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
- a5e30e3199ee8a31
- Request
- aa3a73ea…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format