main
1import { MESSAGE_MAX } from './baseline.ts'
2import { asRecord } from './json.ts'
4export const VERSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
6export type VersionShare = {
7 readonly versionId: string
8 readonly percentage: number
9}
11export type CanaryPlan =
12 | { readonly kind: 'ok'; readonly live: string; readonly dropped: string | null }
13 | { readonly kind: 'refuse'; readonly reason: string }
14 /** Another version holds the 0% slot and the operator did not name it for replacement. Traffic is not changed. */
15 | { readonly kind: 'conflict'; readonly reason: string; readonly sitting: string }
17/**
18 * Where to put `next` at 0%. A different version already at 0% is someone else's canary (another run, or an agent's
19 * measurement version: run 2026-11-07T18-30-47-501Z replaced b494b751 that way), so it is a conflict unless
20 * `replace` names exactly that version (`--replace-canary <id>`).
21 */
22export function planCanary(versions: readonly VersionShare[], next: string, replace: string | null = null): CanaryPlan {
23 if (!VERSION_ID.test(next)) return { kind: 'refuse', reason: 'new version id is not a uuid' }
24 if (versions.length === 0) return { kind: 'refuse', reason: 'no deployment' }
25 const live = versions.filter(v => v.percentage === 100)
26 const zero = versions.filter(v => v.percentage === 0)
27 const other = versions.filter(v => v.percentage !== 100 && v.percentage !== 0)
28 if (other.length > 0) return { kind: 'refuse', reason: 'deployment is not a 0/100 split' }
29 if (live.length !== 1) {
30 return { kind: 'refuse', reason: live.length === 0 ? 'no version at 100%' : 'more than one version at 100%' }
31 }
32 const current = live[0]
33 if (!current) return { kind: 'refuse', reason: 'no version at 100%' }
34 if (current.versionId === next) return { kind: 'refuse', reason: 'new version is already at 100%' }
35 if (versions.length > 2) return { kind: 'refuse', reason: 'more than two versions in the deployment' }
36 if (zero.length > 1) return { kind: 'refuse', reason: 'more than one version at 0%' }
37 const sitting = zero[0]
38 const dropped = sitting && sitting.versionId !== next ? sitting.versionId : null
39 if (dropped !== null && dropped !== replace) {
40 const named = replace === null ? '' : ` (--replace-canary names ${replace})`
41 return {
42 kind: 'conflict',
43 sitting: dropped,
44 reason: `${dropped} is already at 0% and this run did not create it${named}; to replace it pass --replace-canary ${dropped}`,
45 }
46 }
47 return { kind: 'ok', live: current.versionId, dropped }
48}
50const splitBrand = Symbol('TrafficSplit')
52/**
53 * A deployment body that sums to 100 and names real version ids.
54 * An empty list, a non-integer percent, or two copies of one id cannot be built.
55 */
56export type TrafficSplit = {
57 readonly [splitBrand]: 'TrafficSplit'
58 readonly versions: readonly VersionShare[]
59}
61export type SplitRefusal = {
62 readonly ok: false
63 readonly reason: string
64}
66function seal(versions: readonly VersionShare[]): TrafficSplit {
67 if (versions.length === 0) throw new Error('empty split')
68 const total = versions.reduce((sum, share) => sum + share.percentage, 0)
69 if (total !== 100) throw new Error('split must sum to 100')
70 const ids = new Set(versions.map(share => share.versionId))
71 if (ids.size !== versions.length) throw new Error('split versions must differ')
72 for (const share of versions) {
73 if (!Number.isInteger(share.percentage) || share.percentage < 0 || share.percentage > 100) {
74 throw new Error('split percentage is not an integer 0..100')
75 }
76 }
77 return { [splitBrand]: 'TrafficSplit', versions }
78}
80export function isTrafficSplit(value: TrafficSplit | SplitRefusal): value is TrafficSplit {
81 return !('ok' in value)
82}
84/** 0 keeps the new version dark. 100 drops the previous version. Anything else is both, summing to 100. */
85export function trafficSplit(percent: number, next: string, previous: string): TrafficSplit | SplitRefusal {
86 if (!VERSION_ID.test(next)) return { ok: false, reason: 'new version id is not a uuid' }
87 if (!VERSION_ID.test(previous)) return { ok: false, reason: 'previous version id is not a uuid' }
88 if (next === previous) return { ok: false, reason: 'split versions must differ' }
89 if (!Number.isInteger(percent) || percent < 0 || percent > 100) return { ok: false, reason: 'percent must be an integer 0..100' }
90 if (percent === 100) return seal([{ versionId: next, percentage: 100 }])
91 return seal([
92 { versionId: next, percentage: percent },
93 { versionId: previous, percentage: 100 - percent },
94 ])
95}
97/** The previous version alone at 100%. */
98export function fullSplit(versionId: string): TrafficSplit | SplitRefusal {
99 if (!VERSION_ID.test(versionId)) return { ok: false, reason: 'version id is not a uuid' }
100 return seal([{ versionId, percentage: 100 }])
101}
103export function deploymentBody(message: string, split: TrafficSplit) {
104 return {
105 strategy: 'percentage' as const,
106 versions: split.versions.map(version => ({ version_id: version.versionId, percentage: version.percentage })),
107 annotations: { 'workers/message': message.slice(0, MESSAGE_MAX) },
108 }
109}
111function uuidIn(value: unknown): string | null {
112 if (typeof value !== 'string' || !VERSION_ID.test(value)) return null
113 return value
114}
116export type ListedVersion = {
117 readonly id: string
118 readonly tag: string
119}
121export type VersionLookup =
122 | { readonly ok: true; readonly versionId: string }
123 | { readonly ok: false; readonly kind: 'version-not-found'; readonly tag: string }
125function tagOf(rec: Record<string, unknown>): string | null {
126 if (typeof rec.tag === 'string' && rec.tag !== '') return rec.tag
127 const annotations = asRecord(rec.annotations)
128 const tagged = annotations?.['workers/tag']
129 return typeof tagged === 'string' && tagged !== '' ? tagged : null
130}
132/** Versions in a `cf workers versions list` JSON payload. A string is TTY text and yields nothing. */
133export function versionsFromList(payload: unknown): ListedVersion[] {
134 if (typeof payload === 'string') return []
135 const out: ListedVersion[] = []
136 const visit = (node: unknown) => {
137 if (Array.isArray(node)) {
138 for (const item of node) visit(item)
139 return
140 }
141 const rec = asRecord(node)
142 if (!rec) return
143 const id = uuidIn(rec.id) ?? uuidIn(rec.version_id)
144 const tag = tagOf(rec)
145 if (id && tag && !out.some(version => version.id === id && version.tag === tag)) out.push({ id, tag })
146 for (const item of Object.values(rec)) visit(item)
147 }
148 visit(payload)
149 return out
150}
152/** The version whose tag is `tag`. Missing is a typed error. TTY banners are not read. */
153export function versionByTag(payload: unknown, tag: string): VersionLookup {
154 const found = versionsFromList(payload).find(version => version.tag === tag)
155 if (!found) return { ok: false, kind: 'version-not-found', tag }
156 return { ok: true, versionId: found.id }
157}
159/** True when the two deployments name the same ids at the same percentages. */
160export function sameSplit(actual: readonly VersionShare[], expected: readonly VersionShare[]): boolean {
161 if (actual.length !== expected.length) return false
162 for (const want of expected) {
163 const got = actual.find(version => version.versionId === want.versionId)
164 if (!got || got.percentage !== want.percentage) return false
165 }
166 return true
167}
169function shareFrom(value: unknown): VersionShare | null {
170 const rec = asRecord(value)
171 if (!rec) return null
172 const versionId = uuidIn(rec.version_id) ?? uuidIn(rec.versionId) ?? uuidIn(rec.id)
173 const percentage = typeof rec.percentage === 'number' ? rec.percentage : Number(rec.percentage)
174 if (!versionId || !Number.isFinite(percentage)) return null
175 return { versionId, percentage }
176}
178function sharesFrom(value: unknown): VersionShare[] | null {
179 if (!Array.isArray(value)) return null
180 const shares: VersionShare[] = []
181 for (const item of value) {
182 const share = shareFrom(item)
183 if (share) shares.push(share)
184 }
185 return shares
186}
188/** The latest deployment. Accepts the shapes cf has returned. `id` is null when the payload has none. */
189export function parseDeployment(value: unknown): DeploymentView {
190 const rec = asRecord(value)
191 if (!rec) return { id: null, versions: [] }
192 const result = asRecord(rec.result)
193 const deployments = Array.isArray(rec.deployments)
194 ? rec.deployments
195 : result && Array.isArray(result.deployments)
196 ? result.deployments
197 : null
198 if (deployments && deployments.length > 0) {
199 const first = asRecord(deployments[0])
200 const versions = first ? sharesFrom(first.versions) : null
201 if (versions) {
202 const id = first && typeof first.id === 'string' && first.id !== '' ? first.id : null
203 return { id, versions }
204 }
205 }
206 return { id: null, versions: sharesFrom(rec.versions) ?? (result ? sharesFrom(result.versions) : null) ?? [] }
207}
209/** The latest deployment's versions. Accepts the shapes cf has returned. */
210export function parseDeploymentList(value: unknown): VersionShare[] {
211 return [...parseDeployment(value).versions]
212}
214export type DeploymentView = {
215 readonly id: string | null
216 readonly versions: readonly VersionShare[]
217}
219/** Null when the live deployment matches the split. Otherwise the mismatch. */
220export function assertSplit(versions: readonly VersionShare[], want: TrafficSplit): string | null {
221 const expected = want.versions
222 if (versions.length !== expected.length) return `deployment has ${versions.length} versions, wanted ${expected.length}`
223 for (const wanted of expected) {
224 const got = versions.find(version => version.versionId === wanted.versionId)
225 if (!got) return `deployment is missing ${wanted.versionId}`
226 if (got.percentage !== wanted.percentage) return `${wanted.versionId} is at ${got.percentage}%, wanted ${wanted.percentage}%`
227 }
228 return null
229}
231export function isFullRollback(versions: readonly VersionShare[], previous: string): boolean {
232 return versions.length === 1 && versions[0]?.versionId === previous && versions[0].percentage === 100
233}
Diagnostics4 git calls · 23 ms
CallStatusSizeTime023 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=61Open pack map
fetch2002.9 KiB · hit18 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=18 git.response.bytes=3010 git.response.cache=HIT git.response.duration_ms=18 git.fetch.wants=0b041576f4bb80bf012deb4a2bced892eb7f6bed 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=3010 git.pkt.payload.max=2989 git.sideband.band1.bytes=2988 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
- a5e2c9e00a3e4b67
- Request
- d3a5ed18…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format