Public: anyone can read this; the crew can change it.

The behind-main alarm fires when main is hundreds of commits past the newest release tag, proven by a test that drives the 366-commit case #15447

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15445-the-behind-main-alarm-fires-when-main-is → main 0 commits fork

  • Merged. torque/15445-the-behind-main-alarm-fires-when-main-is was merged into main as db0edd6.

Review

Sign in to review this change.
3 files changed +174 −5
src/release/behind.ts +65 −3
@@ -9,10 +9,20 @@
99 * any cause, and it clears when the versions match. #2574 proves a release
1010 * after it boots; this sees the release that never boots.
1111 *
12 * A tag alone cannot see main moving on with no release after it (#15445:
13 * production served 0.7.629, the newest tag, while main stood 369 commits
14 * past it). So the read also counts main's commits after the newest tag, from
15 * the same git store; when production serves that tag and main has been past
16 * it for over {@link AHEAD_MS}, the same one cause says so ("production is
17 * 0.7.629; main is 369 commits ahead, 21 h"). One cause, never two: a version
18 * behind the tag is said first. Cutting and serving a release at main's tip
19 * clears it.
20 *
1221 * Plain data in, plain data out: the clock and the reader are injected, the
1322 * answer is kept in memory (it is derived from git and rebuilt on boot).
1423 */
1524
25 import { runGit } from "../git/env.ts";
1626 import { UNSTAMPED } from "../kernel/version.ts";
1727 import { readTagged, setTagged, type Shipped } from "./shipped.ts";
1828
@@ -20,11 +30,26 @@ import { readTagged, setTagged, type Shipped } from "./shipped.ts";
2030 export const CHECK_EVERY_MS = 10 * 60_000;
2131 /** How long a release may sit on main before production not serving it is a cause. */
2232 export const GRACE_MS = 60 * 60_000;
33 /**
34 * How long main may run ahead of the newest release, served, before it is a cause: 12 h, half
35 * the daily release tick's cadence, so a day with commits and no release is always said.
36 */
37 export const AHEAD_MS = 12 * 60 * 60_000;
38
39 /**
40 * Main's commits after the newest release, and when the oldest of them was committed (epoch ms).
41 */
42 export interface Ahead {
43 readonly commits: number;
44 readonly since: number;
45 }
2346
2447 /** The newest release and when it was cut (epoch ms). */
2548 export interface Newest {
2649 readonly version: string;
2750 readonly at: number;
51 /** Main past the release; absent when main's tip is the release or it could not be read. */
52 readonly ahead?: Ahead;
2853 }
2954
3055 export interface BehindNeed {
@@ -45,7 +70,28 @@ export function newestOf(tagged: readonly Shipped[]): Newest | undefined {
4570 export async function readNewest(dir: string): Promise<Newest | undefined> {
4671 const tagged = await readTagged(dir);
4772 setTagged(tagged);
48 return newestOf(tagged);
73 return withAhead(dir, newestOf(tagged));
74 }
75
76 /**
77 * Main's commits after release `version` (tag `v<version>`) in the bare repository at `dir`,
78 * read from its git store: undefined when main's tip is the release or git cannot say. The
79 * version is a release record's, already held to `[0-9A-Za-z][0-9A-Za-z.+_-]*`.
80 */
81 export async function readAhead(dir: string, version: string): Promise<Ahead | undefined> {
82 // One committer time per line, newest first; the last line is the oldest commit past the tag.
83 const out = await runGit(["log", "--format=%ct", `refs/tags/v${version}..HEAD`], { cwd: dir }).catch(() => undefined);
84 if (out === undefined || !out.ok || out.value === "") return undefined;
85 const lines = out.value.split("\n");
86 const since = Number(lines[lines.length - 1]) * 1000;
87 return Number.isFinite(since) && since > 0 ? { commits: lines.length, since } : undefined;
88 }
89
90 /** `newest` with how far main is past it, when it is. */
91 export async function withAhead(dir: string, newest: Newest | undefined): Promise<Newest | undefined> {
92 if (newest === undefined) return undefined;
93 const ahead = await readAhead(dir, newest.version);
94 return ahead === undefined ? newest : { ...newest, ahead };
4995 }
5096
5197 /** "40 min ago", "2 h ago", "3 d ago". */
@@ -56,9 +102,25 @@ function ago(ms: number): string {
56102 return `${Math.floor(minutes / (24 * 60))} d ago`;
57103 }
58104
59 /** The cause, or undefined while the forge serves the newest release or it is still new. */
105 /** "40 min", "21 h", "3 d". */
106 function span(ms: number): string {
107 return ago(ms).replace(" ago", "");
108 }
109
110 /**
111 * The cause, or undefined while the forge serves the newest release and main is not long past it,
112 * or the release is still new.
113 */
60114 export function behindNeed(live: string, newest: Newest | undefined, now: number): BehindNeed | undefined {
61 if (newest === undefined || live === UNSTAMPED || live === newest.version) return undefined;
115 if (newest === undefined || live === UNSTAMPED) return undefined;
116 if (live === newest.version) {
117 const ahead = newest.ahead;
118 if (ahead === undefined || ahead.commits < 1) return undefined;
119 const age = now - ahead.since;
120 if (!(age > AHEAD_MS)) return undefined;
121 const commits = `${ahead.commits} ${ahead.commits === 1 ? "commit" : "commits"}`;
122 return { sentence: `production is ${live}; main is ${commits} ahead, ${span(age)}`, since: new Date(ahead.since).toISOString() };
123 }
62124 const age = now - newest.at;
63125 if (age <= GRACE_MS) return undefined;
64126 const at = new Date(newest.at).toISOString();
src/release/own_tags.ts +5 −2
@@ -16,7 +16,7 @@ import { err, type Result } from "../kernel/result.ts";
1616 import { getRepo } from "../core/repos.ts";
1717 import { repoDir } from "../git/repos.ts";
1818 import { readTags, setTagged, type Shipped } from "./shipped.ts";
19 import { newestOf, type Newest } from "./behind.ts";
19 import { newestOf, withAhead, type Newest } from "./behind.ts";
2020
2121 /** The release records on the tags of the repository `named` (`owner/name`) on this forge; why not when there are none to read. */
2222 export async function readOwnTags(
@@ -49,6 +49,9 @@ export function ownTagsReader(
4949 const top = newestOf(read.value);
5050 if (top?.version !== newest) app.log.info("release tags read", { repo: named, tags: read.value.length, newest: top?.version ?? "none" });
5151 newest = top?.version;
52 return top;
52 // How far main is past the newest tag, from the same git store (#15445).
53 const [owner = "", name = ""] = named.split("/");
54 const repo = getRepo(app.db, owner, name);
55 return repo === undefined ? top : withAhead(repoDir(app.reposRoot, repo.ownerName, repo.name), top);
5356 };
5457 }
test/release-behind-main-ahead.test.ts added +104
@@ -0,0 +1,104 @@
1 /**
2 * Production serving the newest release tag while main stands hundreds of commits past it is the
3 * one `release:behind` cause, read from the git store; a release cut at main's tip and served
4 * clears it (#15445: v0.7.629 live, main at v0.7.629-369).
5 */
6
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { vitals } from "../src/kernel/vitals.ts";
13 import { AHEAD_MS, behindNeed, readAhead, readNewest, setBehind, watchBehind } from "../src/release/behind.ts";
14 import { setTagged } from "../src/release/shipped.ts";
15
16 const T0 = Date.parse("2026-11-22T12:00:00Z");
17 const H = 3_600_000;
18 const secs = (ms: number) => Math.floor(ms / 1000);
19
20 let dataDir: string;
21 let app: App;
22 let dir: string;
23 const env = { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" };
24
25 async function git(args: string[], stdin?: string, date?: number): Promise<void> {
26 const p = Bun.spawn(["git", ...args], {
27 cwd: dir,
28 env: { ...env, ...(date === undefined ? {} : { GIT_COMMITTER_DATE: `${secs(date)} +0000` }) },
29 stdout: "ignore",
30 stderr: "pipe",
31 stdin: stdin === undefined ? "ignore" : new Blob([stdin]),
32 });
33 if ((await p.exited) !== 0) throw new Error(`git ${args[0]}: ${await new Response(p.stderr).text()}`);
34 }
35
36 function record(version: string): string {
37 return JSON.stringify({ version, migrations: 1, ids: [] });
38 }
39
40 beforeAll(async () => {
41 dataDir = await mkdtemp(join(tmpdir(), "forge-behind-ahead-"));
42 app = createApp({ dataDir, port: 0 });
43 dir = join(dataDir, "forge.git");
44 await Bun.$`mkdir -p ${dir}`.quiet();
45 await git(["init", "-q", "--bare", "-b", "main"]);
46 // The release commit 30 h ago, then 369 commits from 21 h ago to 1 h ago: one fast-import.
47 const lines: string[] = [];
48 const commit = (n: number, at: number) => {
49 const msg = `c${n}`;
50 lines.push("commit refs/heads/main", `mark :${n}`, `committer t <t@t> ${secs(at)} +0000`, `data ${msg.length}`, msg);
51 if (n > 1) lines.push(`from :${n - 1}`);
52 lines.push("");
53 };
54 commit(1, T0 - 30 * H);
55 for (let i = 0; i < 369; i++) commit(i + 2, T0 - 21 * H + Math.floor((i * 20 * H) / 368));
56 await git(["fast-import", "--quiet"], lines.join("\n") + "\n");
57 await git(["tag", "-a", "-F", "-", "v0.7.629", "main~369"], record("0.7.629"), T0 - 30 * H);
58 });
59
60 afterAll(async () => {
61 setBehind(undefined);
62 setTagged([]);
63 app.db.close();
64 await rm(dataDir, { recursive: true, force: true });
65 });
66
67 function causes(now: number) {
68 return vitals({ db: app.db, signals: app.signals, dataDir, now }).degraded.filter((d) => d.cause === "release:behind");
69 }
70
71 test("main 369 commits and 21 h past the served release is exactly one cause naming the count and the age", async () => {
72 expect(await readAhead(dir, "0.7.629")).toEqual({ commits: 369, since: secs(T0 - 21 * H) * 1000 });
73 const state = { live: "0.7.629" };
74 const watch = watchBehind({ live: () => state.live, read: () => readNewest(dir), now: () => T0 });
75 setBehind(watch);
76 await watch.tick();
77 const found = causes(T0);
78 expect(found).toHaveLength(1);
79 expect(found[0]).toMatchObject({
80 sentence: "production is 0.7.629; main is 369 commits ahead, 21 h.",
81 since: new Date(secs(T0 - 21 * H) * 1000).toISOString(),
82 });
83
84 // A release cut at main's tip, not yet served: still one cause, the version behind said first.
85 await git(["tag", "-a", "-F", "-", "v0.7.630", "main"], record("0.7.630"), T0 - 2 * H);
86 await watch.tick();
87 const cut = causes(T0);
88 expect(cut).toHaveLength(1);
89 expect(cut[0]!.sentence).toBe("production is 0.7.629; main released 0.7.630 2 h ago.");
90
91 // Served: main's tip is the release, nothing past it, and the cause clears.
92 state.live = "0.7.630";
93 await watch.tick();
94 expect(await readAhead(dir, "0.7.630")).toBeUndefined();
95 expect(causes(T0)).toHaveLength(0);
96 });
97
98 test("main past the served release for 12 h or less says nothing; one minute more is the cause", () => {
99 const newest = { version: "0.7.629", at: T0 - 30 * H, ahead: { commits: 1, since: T0 - AHEAD_MS } };
100 expect(behindNeed("0.7.629", newest, T0)).toBeUndefined();
101 expect(behindNeed("0.7.629", newest, T0 + 60_000)?.sentence).toBe("production is 0.7.629; main is 1 commit ahead, 12 h");
102 // An unstamped forge says nothing, however far main is.
103 expect(behindNeed("dev", newest, T0 + 60_000)).toBeUndefined();
104 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs