main
ply4 git calls24 ms42 KiB3 cachedprotocol v2
142 lines5.4 KiBRaw
1import { readdir, readFile } from 'node:fs/promises'
2import { join } from 'node:path'
3import type { ListedVersion, VersionShare } from './deploy-plan.ts'
4import { asRecord } from './json.ts'
5import { readEvents, type CdEvent } from './log.ts'
6import { isGitSha, releaseTag, sameRelease, type ReleaseId, type ReleasePair } from './release.ts'
8export type Reconcile =
9 | { readonly stage: 'upload' }
10 | { readonly stage: 'smoke-canary'; readonly versionId: string; readonly previous: string }
11 | { readonly stage: 'ramp'; readonly versionId: string; readonly previous: string; readonly percent: number }
12 | { readonly stage: 'smoke-live'; readonly versionId: string; readonly previous: string | null }
13 | { readonly stage: 'done'; readonly versionId: string }
14 | { readonly stage: 'refuse'; readonly reason: string }
16/**
17 * Next stage for a release. Depends only on the live split, whether a version
18 * tagged with this ply sha and this ply-web sha exists, and whether live smoke
19 * already passed for it. A ply-only tag is not this release.
20 */
21export function reconcile(input: {
22 readonly release: ReleaseId
23 readonly versions: readonly ListedVersion[]
24 readonly split: readonly VersionShare[]
25 readonly liveSmokePassed: boolean
26 readonly rampPercents: readonly number[]
27}): Reconcile {
28 const tag = releaseTag(input.release)
29 const versionId = input.versions.find(version => version.tag === tag)?.id ?? null
30 if (versionId === null) return { stage: 'upload' }
31 const mine = input.split.filter(share => share.versionId === versionId)
32 if (mine.length === 0) return { stage: 'upload' }
33 const me = mine[0]
34 if (!me || mine.length !== 1) return { stage: 'refuse', reason: refusal() }
35 const others = input.split.filter(share => share.versionId !== versionId)
36 if (me.percentage === 0) {
37 const previous = others[0]
38 if (others.length !== 1 || !previous || previous.percentage !== 100) return { stage: 'refuse', reason: refusal() }
39 return { stage: 'smoke-canary', versionId, previous: previous.versionId }
40 }
41 if (me.percentage === 100) {
42 if (others.length !== 0) return { stage: 'refuse', reason: refusal() }
43 if (input.liveSmokePassed) return { stage: 'done', versionId }
44 return { stage: 'smoke-live', versionId, previous: null }
45 }
46 if (!input.rampPercents.includes(me.percentage)) return { stage: 'refuse', reason: refusal() }
47 const previous = others[0]
48 if (others.length !== 1 || !previous || previous.percentage !== 100 - me.percentage) {
49 return { stage: 'refuse', reason: refusal() }
50 }
51 return { stage: 'ramp', versionId, previous: previous.versionId, percent: me.percentage }
54function refusal(): string {
55 return 'deployment is not a canary, a ramp step, or live for this release'
58export function reconcileReason(decision: Reconcile): string {
59 switch (decision.stage) {
60 case 'upload':
61 return 'upload'
62 case 'smoke-canary':
63 return 'pinned smoke'
64 case 'ramp':
65 return `ramp from ${decision.percent}%`
66 case 'smoke-live':
67 return 'live smoke'
68 case 'done':
69 return 'done: live smoke already passed'
70 case 'refuse':
71 return decision.reason
72 }
75function pairField(value: unknown): ReleasePair | null {
76 const rec = asRecord(value)
77 if (!rec) return null
78 const ply = rec.ply
79 const web = rec.web
80 if (typeof ply !== 'string' || typeof web !== 'string') return null
81 if (!isGitSha(ply) || !isGitSha(web)) return null
82 return { ply, web }
85function namesRelease(value: unknown, release: ReleaseId): boolean {
86 const pair = pairField(value)
87 return pair !== null && sameRelease(release, pair)
90function eventRelease(event: CdEvent, release: ReleaseId): boolean {
91 if (event.kind === 'lifecycle' && event.payload.event === 'run_started') return namesRelease(event.payload.release, release)
92 if (event.kind === 'lifecycle' && event.payload.event === 'resumed') return namesRelease(event.payload.release, release)
93 if (event.kind === 'policy') return namesRelease(event.payload.release, release)
94 if (event.kind === 'observation') {
95 const measurement = asRecord(event.payload.measurement)
96 return measurement?.source === 'reconcile' && namesRelease(measurement.release, release)
97 }
98 return false
101/** The latest unfinished run directory whose log names this release. A ply-only run is not a match. */
102export async function unfinishedRunDir(root: string, release: ReleaseId): Promise<string | null> {
103 let names: string[]
104 try {
105 names = await readdir(root)
106 } catch {
107 return null
108 }
109 const matches: string[] = []
110 for (const name of names) {
111 const dir = join(root, name)
112 let events: CdEvent[]
113 try {
114 events = await readEvents(dir)
115 } catch {
116 continue
117 }
118 if (events.some(event => event.kind === 'lifecycle' && event.payload.event === 'run_finished')) continue
119 if (events.some(event => eventRelease(event, release))) matches.push(dir)
120 }
121 matches.sort()
122 return matches.at(-1) ?? null
125/** True when some run under `root` recorded a passing live smoke for this version. */
126export async function liveSmokeAlreadyPassed(root: string, versionId: string): Promise<boolean> {
127 let names: string[]
128 try {
129 names = await readdir(root)
130 } catch {
131 return false
132 }
133 for (const name of names) {
134 try {
135 const rec = asRecord(JSON.parse(await readFile(join(root, name, 'live-smoke.json'), 'utf8')))
136 if (rec?.version === versionId && rec.failed === 0) return true
137 } catch {
138 continue
139 }
140 }
141 return false
Diagnostics4 git calls · 24 ms
CallStatusSizeTime024 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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
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=61
Open pack map
fetch2001.6 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=1677
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.wants=89d250e9ca836d6acddc3c671458b148a49d3f14
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=1677
git.pkt.payload.max=1656
git.sideband.band1.bytes=1655
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
a5e2d51fcb1d689d
Request
820126f3…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format