Public: anyone can read this; the crew can change it.
Every way the release tick declines to cut records a heartbeat with a named reason, proven by one test per path #15470
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15468-every-way-the-release-tick-declines-to-c → main 0 commits fork 1 thread · 1 unresolved
4 files
changed +140
−6
src/keeper/beat.ts +12 −2
| @@ -33,8 +33,17 @@ export interface Beat { | ||
| 33 | 33 | readonly reason: string; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | /** | |
| 37 | * The reason as one line of at most {@link REASON_MAX}; never empty (#15468): a | |
| 38 | * step that named nothing says so, so a stop is never a blank. | |
| 39 | */ | |
| 40 | function beatReason(outcome: Outcome, reason: string): string { | |
| 41 | const said = reason.replace(/\s+/g, " ").trim(); | |
| 42 | return (said === "" ? `the release tick ${outcome} without naming why` : said).slice(0, REASON_MAX); | |
| 43 | } | |
| 44 | ||
| 36 | 45 | export function recordBeat(db: Database, outcome: Outcome, reason: string, at: Date = new Date()): Beat { |
| 37 | const beat: Beat = { at: at.toISOString(), outcome, reason: reason.replace(/\s+/g, " ").trim().slice(0, REASON_MAX) }; | |
| 46 | const beat: Beat = { at: at.toISOString(), outcome, reason: beatReason(outcome, reason) }; | |
| 38 | 47 | db.transaction(() => { |
| 39 | 48 | db.query("INSERT INTO keeper_beat_tick (at, outcome, reason) VALUES (?1, ?2, ?3)").run(beat.at, beat.outcome, beat.reason); |
| 40 | 49 | db.query("DELETE FROM keeper_beat_tick WHERE id <= (SELECT MAX(id) FROM keeper_beat_tick) - ?1").run(KEPT); |
| @@ -59,7 +68,8 @@ export async function stepWithBeat(db: Database, step: () => Promise<Step>, now: | ||
| 59 | 68 | try { |
| 60 | 69 | done = await step(); |
| 61 | 70 | } catch (error) { |
| 62 | writeBeat(db, "failed", `step failed: ${error instanceof Error ? error.message : String(error)}`, now()); | |
| 71 | const said = error instanceof Error ? error.message || error.name : String(error); | |
| 72 | writeBeat(db, "failed", `step failed: ${said || "an error with no message"}`, now()); | |
| 63 | 73 | throw error; |
| 64 | 74 | } |
| 65 | 75 | writeBeat(db, done.outcome, done.line, now()); |
src/keeper/keeper.ts +2 −2
| @@ -276,8 +276,8 @@ export async function tickStep(p: KeeperPorts): Promise<Step> { | ||
| 276 | 276 | return { line: await rollBack(p, flying, `${flying.version} was not live ${ROLL_DEADLINE_MS / 3_600_000} h after its cut`), outcome: "failed" }; |
| 277 | 277 | // A promote that did not take (the API refused, or something set the image back) is sent again. |
| 278 | 278 | const want = refFor(p, flying.knownGood.image, flying.version, flying.sha); |
| 279 | if (flying.stage === "rolling" && (await p.deployer.image()) !== want) await promote(p, flying.version, want); | |
| 280 | return { line: `${flying.version} is ${flying.stage}`, outcome: "skipped" }; | |
| 279 | const sent = flying.stage === "rolling" && (await p.deployer.image()) !== want ? `; ${await promote(p, flying.version, want)}` : ""; | |
| 280 | return { line: `${flying.version} is ${flying.stage}${sent}`, outcome: "skipped" }; | |
| 281 | 281 | } |
| 282 | 282 | const main = await p.main(); |
| 283 | 283 | const newest = await p.newest(); |
test/keeper-heartbeat.test.ts +125 −1
| @@ -7,7 +7,10 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 7 | 7 | import { createToken } from "../src/core/tokens.ts"; |
| 8 | 8 | import { createUser } from "../src/core/users.ts"; |
| 9 | 9 | import { serve, type Servers } from "../src/http/server.ts"; |
| 10 | import { KEPT, lastBeat, recordBeat, stepWithBeat } from "../src/keeper/beat.ts"; | |
| 10 | import { KEPT, REASON_MAX, lastBeat, recordBeat, stepWithBeat, type Beat } from "../src/keeper/beat.ts"; | |
| 11 | import { setHold } from "../src/keeper/hold.ts"; | |
| 12 | import { tickStep, type KeeperPorts } from "../src/keeper/keeper.ts"; | |
| 13 | import { keeperForge } from "./helpers/keeper.ts"; | |
| 11 | 14 | import { keeperAnswer } from "../src/keeper/landed.ts"; |
| 12 | 15 | |
| 13 | 16 | describe("release heartbeat", () => { |
| @@ -114,3 +117,124 @@ describe("release heartbeat", () => { | ||
| 114 | 117 | expect(lastBeat(app.db)?.reason).toBe("tick 51"); |
| 115 | 118 | }); |
| 116 | 119 | }); |
| 120 | ||
| 121 | /** | |
| 122 | * Every way the release tick declines to cut leaves a beat naming why (#15468): | |
| 123 | * each test clears the beats, drives one path through the wiring main.ts runs | |
| 124 | * (`stepWithBeat` over `tickStep`), and reads the beat back — a path that | |
| 125 | * returned without one reads null and fails. | |
| 126 | */ | |
| 127 | describe("every path of the tick leaves a named beat", () => { | |
| 128 | type Forge = Awaited<ReturnType<typeof keeperForge>>; | |
| 129 | const forges: Forge[] = []; | |
| 130 | afterAll(async () => { | |
| 131 | for (const f of forges) { | |
| 132 | f.app.close(); | |
| 133 | await rm(f.app.config.dataDir, { recursive: true, force: true }); | |
| 134 | } | |
| 135 | }); | |
| 136 | const forge = async () => { | |
| 137 | const f = await keeperForge(); | |
| 138 | forges.push(f); | |
| 139 | f.app.db.query("DELETE FROM keeper_beat_tick").run(); | |
| 140 | return f; | |
| 141 | }; | |
| 142 | /** | |
| 143 | * One tick as main.ts runs it; the beat it left, asserted present, one line and within | |
| 144 | * REASON_MAX. | |
| 145 | */ | |
| 146 | const beatOf = async (f: Forge, ports: KeeperPorts = f.ports): Promise<Beat> => { | |
| 147 | f.app.db.query("DELETE FROM keeper_beat_tick").run(); | |
| 148 | const at = new Date(f.world.now); | |
| 149 | await stepWithBeat(f.app.db, () => tickStep(ports), () => at); | |
| 150 | const beat = lastBeat(f.app.db); | |
| 151 | expect(beat).not.toBeNull(); | |
| 152 | expect(beat!.at).toBe(at.toISOString()); | |
| 153 | expect(beat!.reason.length).toBeGreaterThan(0); | |
| 154 | expect(beat!.reason.length).toBeLessThanOrEqual(REASON_MAX); | |
| 155 | expect(beat!.reason).not.toMatch(/[\n\r]/); | |
| 156 | return beat!; | |
| 157 | }; | |
| 158 | ||
| 159 | test("the decision not to cut: main has not moved", async () => { | |
| 160 | const f = await forge(); | |
| 161 | f.world.main = f.world.newest!.sha; | |
| 162 | expect(await beatOf(f)).toMatchObject({ outcome: "skipped", reason: "main is still 0.7.283" }); | |
| 163 | }); | |
| 164 | ||
| 165 | test("main unreadable", async () => { | |
| 166 | const f = await forge(); | |
| 167 | const ports: KeeperPorts = { ...f.ports, main: async () => undefined }; | |
| 168 | expect(await beatOf(f, ports)).toMatchObject({ outcome: "failed", reason: "main is unreadable" }); | |
| 169 | }); | |
| 170 | ||
| 171 | test("the deployer refuses the tree", async () => { | |
| 172 | const f = await forge(); | |
| 173 | const ports: KeeperPorts = { ...f.ports, deployer: { ...f.ports.deployer, admits: async () => "dependencies changed since 0.7.283" } }; | |
| 174 | expect(await beatOf(f, ports)).toMatchObject({ outcome: "failed", reason: "not cut: dependencies changed since 0.7.283" }); | |
| 175 | expect(f.world.tags).toEqual([]); | |
| 176 | }); | |
| 177 | ||
| 178 | test("the tag is refused", async () => { | |
| 179 | const f = await forge(); | |
| 180 | f.world.refuseTag = "the remote refused v0.7.284"; | |
| 181 | expect(await beatOf(f)).toMatchObject({ outcome: "failed", reason: "not cut: the remote refused v0.7.284" }); | |
| 182 | }); | |
| 183 | ||
| 184 | test("a hold stands", async () => { | |
| 185 | const f = await forge(); | |
| 186 | setHold(f.app.db, { kind: "owner", version: "0.7.283", reason: "freeze for the migration", at: f.world.now }); | |
| 187 | const beat = await beatOf(f); | |
| 188 | expect(beat.outcome).toBe("skipped"); | |
| 189 | expect(beat.reason).toBe("held: the owner holds releases: freeze for the migration"); | |
| 190 | }); | |
| 191 | ||
| 192 | test("the disk is low", async () => { | |
| 193 | const f = await forge(); | |
| 194 | const beat = await beatOf(f, { ...f.ports, diskUsed: () => 0.93 }); | |
| 195 | expect(beat.outcome).toBe("skipped"); | |
| 196 | expect(beat.reason).toContain("the disk is 93% full"); | |
| 197 | }); | |
| 198 | ||
| 199 | test("a release is flying: rolling, not live yet", async () => { | |
| 200 | const f = await forge(); | |
| 201 | expect(await beatOf(f)).toMatchObject({ outcome: "cut" }); | |
| 202 | expect(await beatOf(f)).toMatchObject({ outcome: "skipped", reason: "0.7.284 is rolling" }); | |
| 203 | }); | |
| 204 | ||
| 205 | test("a flying release whose promote is refused says so in the beat", async () => { | |
| 206 | const f = await forge(); | |
| 207 | expect((await beatOf(f)).outcome).toBe("cut"); | |
| 208 | f.state.image = "reg.test/o/forge:0.7.283"; | |
| 209 | const ports: KeeperPorts = { ...f.ports, deployer: { ...f.ports.deployer, setImage: async () => { throw new Error("the API said 409"); } } }; | |
| 210 | const beat = await beatOf(f, ports); | |
| 211 | expect(beat.outcome).toBe("skipped"); | |
| 212 | expect(beat.reason).toBe("0.7.284 is rolling; promote to reg.test/o/forge:0.7.284 refused (the API said 409); sent again next tick"); | |
| 213 | }); | |
| 214 | ||
| 215 | test("a verify already running", async () => { | |
| 216 | const f = await forge(); | |
| 217 | expect((await beatOf(f)).outcome).toBe("cut"); | |
| 218 | f.world.live = "0.7.284"; | |
| 219 | const first = tickStep(f.ports); | |
| 220 | const beat = await beatOf(f); | |
| 221 | expect(beat).toMatchObject({ outcome: "skipped", reason: "verifying 0.7.284" }); | |
| 222 | expect((await first).outcome).toBe("cut"); | |
| 223 | }); | |
| 224 | ||
| 225 | test("a throw inside the tick records a failed beat with the error's message, not the last beat", async () => { | |
| 226 | const f = await forge(); | |
| 227 | recordBeat(f.app.db, "skipped", "an old beat", new Date(f.world.now - 60_000)); | |
| 228 | const ports: KeeperPorts = { ...f.ports, newest: async () => { throw new Error("git said: bad object HEAD"); } }; | |
| 229 | await expect(stepWithBeat(f.app.db, () => tickStep(ports), () => new Date(f.world.now))).rejects.toThrow("bad object HEAD"); | |
| 230 | expect(lastBeat(f.app.db)).toEqual({ at: new Date(f.world.now).toISOString(), outcome: "failed", reason: "step failed: git said: bad object HEAD" }); | |
| 231 | }); | |
| 232 | ||
| 233 | test("a step that names nothing, or throws an empty error, still leaves a named reason", async () => { | |
| 234 | const f = await forge(); | |
| 235 | await stepWithBeat(f.app.db, async () => ({ line: " ", outcome: "skipped" })); | |
| 236 | expect(lastBeat(f.app.db)?.reason).toBe("the release tick skipped without naming why"); | |
| 237 | await expect(stepWithBeat(f.app.db, async () => { throw new Error(""); })).rejects.toThrow(); | |
| 238 | expect(lastBeat(f.app.db)).toMatchObject({ outcome: "failed", reason: "step failed: Error" }); | |
| 239 | }); | |
| 240 | }); |
test/release-keeper-cadence.test.ts +1 −1
| @@ -142,7 +142,7 @@ describe("cadence", () => { | ||
| 142 | 142 | // The process died after the row and before the tag. |
| 143 | 143 | recordCut(f.app.db, { version: "0.7.284", sha: B, knownGood: { version: "0.7.283", image: "reg.test/o/forge:0.7.283" }, at: f.world.now }); |
| 144 | 144 | expect(f.world.tags).toHaveLength(0); |
| 145 | expect(await tick(f.ports)).toBe("0.7.284 is rolling"); | |
| 145 | expect(await tick(f.ports)).toBe("0.7.284 is rolling; promoted to reg.test/o/forge:0.7.284"); | |
| 146 | 146 | expect(f.world.tags).toEqual([{ version: "0.7.284", sha: B }]); |
| 147 | 147 | expect(f.state.set).toEqual(["reg.test/o/forge:0.7.284"]); |
| 148 | 148 | // Tagged once: the next tick does not tag it again. |
new above
offline
The first tick is started and not awaited, so the second tick sees verifying only if the scheduler lets it. Hold the first on an explicit promise the test resolves, so the order is fixed.