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

Defect of #13042: production runs unstamped (version "dev", release null) at main's tip, so the 1.0.0 line cannot pass #15500

Merged opened this

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

Merged

scout/forge:scout/15497-defect-of-13042-production-runs-unstampe → main 0 commits fork

  • Merged. scout/15497-defect-of-13042-production-runs-unstampe was merged into main as 2aec489.

Review

Sign in to review this change.
6 files changed +194 −4
docs/APPS.md +1
@@ -778,6 +778,7 @@ The forge starts the app with:
778778 | `FORGE_APP_DATA` | this environment's data directory, created for it |
779779 | `FORGE_APP_URL` | the public URL this environment is served at |
780780 | `FORGE_APP_COMMIT` | the commit being run |
781 | `FORGE_APP_RELEASE` | on production, the newest `vX.Y.Z` tag at or below that commit, as `x.y.z`; unset when none, and never on a preview |
781782 | `FORGE_APP_PULL` | the pull number, on a preview |
782783 | `FORGE_APP_SSH` | the exact `ssh …` that opens this environment's terminal, when SSH is on |
783784 | `FORGE_URL` | the forge's own base URL |
src/apps/process.ts +11
@@ -75,6 +75,10 @@ export interface StartInput {
7575 readonly feature?: Record<string, string> | undefined;
7676 /** The process's read-only credential into the forge (`apps/token.ts`). */
7777 readonly forgeToken?: string | undefined;
78 /**
79 * The newest release tag at or below the commit, `x.y.z`, on production (`apps/release_at.ts`).
80 */
81 readonly release?: string | undefined;
7882 readonly log: (line: string) => void;
7983 /** Aborting cancels whatever step is in flight and fails the start. */
8084 readonly signal: AbortSignal;
@@ -192,6 +196,8 @@ export function appEnv(input: {
192196 forgeToken?: string | undefined;
193197 /** A pinned toolchain's PATH and homes (`apps/toolchain.ts`). */
194198 toolchain?: Record<string, string> | undefined;
199 /** The release the commit is at, `x.y.z`; production only (`apps/release_at.ts`). */
200 release?: string | undefined;
195201 }): Record<string, string> {
196202 const pull = envPull(input.env);
197203 // The operator's variables go underneath everything the forge names, so
@@ -210,6 +216,10 @@ export function appEnv(input: {
210216 FORGE_APP_DATA: input.dataDir,
211217 FORGE_APP_URL: input.url,
212218 FORGE_APP_COMMIT: input.commitOid,
219 // A preview runs a pull, which is no release: only production is told one.
220 ...(input.env === "production" && input.release !== undefined && /^\d+\.\d+\.\d+$/.test(input.release)
221 ? { FORGE_APP_RELEASE: input.release }
222 : {}),
213223 FORGE_URL: input.forgeUrl,
214224 FORGE_API_URL: forgeApiUrl(input.forgeUrl),
215225 FORGE_APP_IDENTITY_KEY: input.identityKey,
@@ -728,6 +738,7 @@ export async function startProcess(
728738 vars: input.vars,
729739 feature: input.feature,
730740 forgeToken: input.forgeToken,
741 release: input.release,
731742 });
732743 let proc: ReturnType<typeof Bun.spawn<"ignore", "pipe", "pipe">>;
733744 try {
src/apps/release_at.ts added +35
@@ -0,0 +1,35 @@
1 /**
2 * The release a production commit is at (#15497): the newest `vX.Y.Z` tag at or below it.
3 *
4 * An app's tree is exported without its history (`checkout.ts`), so the app cannot read its own
5 * tags; the forge reads them from the bare repository and hands the answer over as
6 * `FORGE_APP_RELEASE` (`process.ts`). Only a production deploy is told: a preview runs a pull,
7 * and a pull is no release, so it never passes for the one beneath it.
8 */
9
10 import { runGit } from "../git/env.ts";
11 import { isSafeOid } from "../git/guard.ts";
12
13 const TAG = /^v(\d+)\.(\d+)\.(\d+)$/;
14
15 /** The newest `x.y.z` among tag names; anything that is not `vX.Y.Z` is passed over. */
16 export function newestRelease(tags: readonly string[]): string | undefined {
17 let best: [number, number, number] | undefined;
18 for (const name of tags) {
19 const m = TAG.exec(name.trim());
20 if (m === null) continue;
21 const v: [number, number, number] = [Number(m[1]), Number(m[2]), Number(m[3])];
22 if (best === undefined || (v[0] - best[0] || v[1] - best[1] || v[2] - best[2]) > 0) best = v;
23 }
24 return best === undefined ? undefined : best.join(".");
25 }
26
27 /**
28 * The newest release tag reachable from `oid` in the bare repository; undefined when none or git
29 * cannot say.
30 */
31 export async function releaseAt(bare: string, oid: string): Promise<string | undefined> {
32 if (!isSafeOid(oid)) return undefined;
33 const out = await runGit(["tag", "--list", "--merged", oid, "v[0-9]*"], { cwd: bare }).catch(() => undefined);
34 return out?.ok === true ? newestRelease(out.value.split("\n")) : undefined;
35 }
src/apps/runtime_bringup.ts +2
@@ -19,6 +19,7 @@ import { exportTree, readFileAt } from "./checkout.ts";
1919 import { Stopwatch } from "./timing.ts";
2020 import { ensureEnvData, envDataDir, envDir, envPull, reseedEmpty, waitedEmptyNote } from "./data.ts";
2121 import { parseManifest } from "./manifest.ts";
22 import { releaseAt } from "./release_at.ts";
2223 import { identityKey } from "./proxy.ts";
2324 import { startProcess, type StartInput } from "./process.ts";
2425 import type { AppsRuntime } from "./runtime.ts";
@@ -119,6 +120,7 @@ export async function bringUp(
119120 env: state.env,
120121 url: rt.urlFor(state.app, state.env),
121122 commitOid: run.commitOid,
123 ...(state.env === "production" ? { release: await releaseAt(bare, run.commitOid) } : {}),
122124 forgeUrl: rt.options.forgeUrl,
123125 identityKey: identityKey(rt.app.config.dataDir, state.app.id),
124126 ssh: rt.sshFor(state.app, state.env),
src/kernel/version.ts +12 −4
@@ -24,6 +24,11 @@ export interface Running {
2424 readonly version: string;
2525 /** The commit that is running, in full, or null when nothing says. */
2626 readonly commit: string | null;
27 /**
28 * The release the commit is at, `x.y.z`, when the forge that runs this one as its production
29 * app says so (`FORGE_APP_RELEASE`, `apps/release_at.ts`, #15497); absent otherwise.
30 */
31 readonly release?: string;
2732 }
2833
2934 /** What an unstamped forge calls itself; the `Dockerfile`'s own default. */
@@ -31,6 +36,7 @@ export const UNSTAMPED = "dev";
3136
3237 const VERSION = /^[0-9A-Za-z][0-9A-Za-z.+_-]{0,63}$/;
3338 const COMMIT = /^[0-9a-f]{7,64}$/;
39 const RELEASE = /^\d+\.\d+\.\d+$/;
3440
3541 export function running(
3642 env: Readonly<Record<string, string | undefined>> = process.env,
@@ -40,9 +46,11 @@ export function running(
4046 const commit = (env["FORGE_COMMIT"] || env["FORGE_APP_COMMIT"] || "")
4147 .trim()
4248 .toLowerCase();
49 const release = (env["FORGE_APP_RELEASE"] ?? "").trim().replace(/^v(?=\d)/, "");
4350 return {
4451 version: VERSION.test(version) ? version : UNSTAMPED,
4552 commit: COMMIT.test(commit) ? commit : null,
53 ...(RELEASE.test(release) ? { release } : {}),
4654 };
4755 }
4856
@@ -56,15 +64,15 @@ export function runningLine(r: Running = running()): string {
5664 /** The short id of a commit, seven characters: the one form every door prints. */
5765 export const shortOf = (commit: string): string => commit.slice(0, 7);
5866
59 const RELEASE = /^\d+\.\d+\.\d+$/;
60
6167 /**
6268 * The release that is running as `x.y.z`, or null when the build names none (#13263): a probe
6369 * reads it as a version, so an unstamped forge's `dev`, or any stamp that is not a release
64 * number, is null here, never a word in a version's place.
70 * number, is null here, never a word in a version's place. An unstamped forge run as another
71 * forge's production app names the release its commit is at (#15497).
6572 */
6673 export function releaseOf(r: Running = running()): string | null {
67 return RELEASE.test(r.version) ? r.version : null;
74 if (RELEASE.test(r.version)) return r.version;
75 return r.release !== undefined && RELEASE.test(r.release) ? r.release : null;
6876 }
6977
7078 /**
test/app-release-15497.test.ts added +133
@@ -0,0 +1,133 @@
1 /**
2 * A forge run as another forge's production app names the release its commit is at (#15497).
3 *
4 * Production ran main's tip as `dev` with `release: null`: the app's tree is exported without
5 * history, so nothing in it could name a tag. The hosting forge now reads the newest `vX.Y.Z` at
6 * or below the commit from the bare repository and hands it over as `FORGE_APP_RELEASE`;
7 * `/api/v1/health` reads it back as `release`. A preview is never told one.
8 */
9
10 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { createApp, type App } from "../src/app.ts";
15 import { serve, type Servers } from "../src/http/server.ts";
16 import { appEnv } from "../src/apps/process.ts";
17 import { newestRelease, releaseAt } from "../src/apps/release_at.ts";
18 import { releaseOf, running } from "../src/kernel/version.ts";
19
20 const git = (cwd: string, ...args: string[]): string => {
21 const r = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "[email protected]", "-c", "tag.gpgSign=false", "-c", "commit.gpgSign=false", ...args], { cwd });
22 if (r.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr.toString()}`);
23 return r.stdout.toString().trim();
24 };
25
26 describe("the release a commit is at", () => {
27 test("the newest x.y.z wins, by number, and nothing else is a release", () => {
28 expect(newestRelease(["v0.7.9", "v0.7.629", "v0.7.62", "v0.0.0-pr.1", "latest", "v1"])).toBe("0.7.629");
29 expect(newestRelease(["v1.0.0", "v0.9.99"])).toBe("1.0.0");
30 expect(newestRelease(["nightly", ""])).toBeUndefined();
31 });
32
33 let repo: string;
34 const at: Record<string, string> = {};
35 beforeAll(async () => {
36 repo = await mkdtemp(join(tmpdir(), "release-at-"));
37 git(repo, "init", "-q", "-b", "main");
38 const commit = (name: string) => {
39 git(repo, "commit", "-q", "--allow-empty", "-m", name);
40 at[name] = git(repo, "rev-parse", "HEAD");
41 };
42 commit("a");
43 git(repo, "tag", "-a", "-m", "r", "v0.7.1");
44 commit("b");
45 git(repo, "tag", "-a", "-m", "r", "v0.7.2");
46 commit("tip");
47 // A release on another line is not below the tip.
48 git(repo, "checkout", "-q", "-b", "side", at["a"]!);
49 commit("side");
50 git(repo, "tag", "-a", "-m", "r", "v0.9.0");
51 git(repo, "checkout", "-q", "main");
52 });
53 afterAll(async () => {
54 await rm(repo, { recursive: true, force: true });
55 });
56
57 test("main's tip, past its last tag, is at that tag; a tagged commit is at its own", async () => {
58 expect(await releaseAt(repo, at["tip"]!)).toBe("0.7.2");
59 expect(await releaseAt(repo, at["b"]!)).toBe("0.7.2");
60 expect(await releaseAt(repo, at["a"]!)).toBe("0.7.1");
61 expect(await releaseAt(repo, at["side"]!)).toBe("0.9.0");
62 });
63
64 test("a commit below every tag, or not a commit id, is at none", async () => {
65 git(repo, "checkout", "-q", "--orphan", "fresh");
66 git(repo, "commit", "-q", "--allow-empty", "-m", "fresh");
67 const fresh = git(repo, "rev-parse", "HEAD");
68 git(repo, "checkout", "-q", "main");
69 expect(await releaseAt(repo, fresh)).toBeUndefined();
70 expect(await releaseAt(repo, "--all")).toBeUndefined();
71 });
72
73 test("production is told it and reads it as its release; a preview is not told", async () => {
74 const release = await releaseAt(repo, at["tip"]!);
75 const input = {
76 port: 1,
77 dataDir: "/d",
78 url: "http://lab.test",
79 commitOid: at["tip"]!,
80 forgeUrl: "http://forge.test",
81 identityKey: "k",
82 release,
83 };
84 const prod = appEnv({ ...input, env: "production" });
85 expect(prod["FORGE_APP_RELEASE"]).toBe("0.7.2");
86 expect(releaseOf(running(prod))).toBe("0.7.2");
87 // The stamp stays what the build says: `dev`, so the keeper never takes this for a cut.
88 expect(running(prod).version).toBe("dev");
89 const preview = appEnv({ ...input, env: "pull-12" });
90 expect(preview["FORGE_APP_RELEASE"]).toBeUndefined();
91 expect(releaseOf(running(preview))).toBeNull();
92 // Only a release number is passed on.
93 expect(appEnv({ ...input, env: "production", release: "dev; rm" })["FORGE_APP_RELEASE"]).toBeUndefined();
94 });
95
96 test("a stamped build's own version still wins over what it is told", () => {
97 expect(releaseOf(running({ FORGE_VERSION: "0.8.0", FORGE_APP_RELEASE: "0.7.2" }))).toBe("0.8.0");
98 expect(releaseOf(running({ FORGE_APP_RELEASE: "nope" }))).toBeNull();
99 });
100 });
101
102 describe("/api/v1/health on an unstamped production app", () => {
103 let dataDir: string;
104 let app: App;
105 let servers: Servers;
106 const keys = ["FORGE_VERSION", "FORGE_COMMIT", "FORGE_APP_COMMIT", "FORGE_APP_RELEASE"] as const;
107 const before = Object.fromEntries(keys.map((k) => [k, process.env[k]]));
108 beforeAll(async () => {
109 delete process.env["FORGE_VERSION"];
110 delete process.env["FORGE_COMMIT"];
111 process.env["FORGE_APP_COMMIT"] = "1701de1790eb21474f584fed5b02accf0b839434";
112 process.env["FORGE_APP_RELEASE"] = "0.7.629";
113 dataDir = await mkdtemp(join(tmpdir(), "app-release-"));
114 app = createApp({ dataDir, port: 0 });
115 servers = await serve(app);
116 });
117 afterAll(async () => {
118 for (const k of keys) {
119 if (before[k] === undefined) delete process.env[k];
120 else process.env[k] = before[k];
121 }
122 servers.stop();
123 app.close();
124 await rm(dataDir, { recursive: true, force: true });
125 });
126
127 test("names the release its commit is at, not null", async () => {
128 const health = (await (await fetch(`${servers.url}/api/v1/health`)).json()) as Record<string, unknown>;
129 expect(health["release"]).toBe("0.7.629");
130 expect(health["commit"]).toBe("1701de1790eb21474f584fed5b02accf0b839434");
131 expect(health["version"]).toBe("dev");
132 });
133 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs