@nanobpm/nano-workforce 0.205.2 → 0.206.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/version.ts CHANGED
@@ -5,8 +5,10 @@
5
5
  // inspecting the working tree at runtime. This module gathers that identity — the app's package
6
6
  // version, the resolved `@nanobpm/urban` version, the git commit (read from `.git`, handling both
7
7
  // an ordinary `.git` directory and the `gitdir:` file pointer used by worktrees/submodules, with
8
- // an env override for detached deploys), plus the Node runtime, pid and start time — so an operator
9
- // debugging a stuck instance can confirm the process is on the code they think it is.
8
+ // an env override for detached deploys AND a `build-info.json` fallback baked into the release
9
+ // package — an npm install has neither `.git` nor the env var, issue #925), plus the Node runtime,
10
+ // pid and start time — so an operator debugging a stuck instance can confirm the process is on the
11
+ // code they think it is.
10
12
  //
11
13
  // Every probe is best-effort: a missing file or unavailable `.git` yields `null` for that field
12
14
  // rather than throwing, so `/app/api/version` never fails just because one source is absent.
@@ -98,17 +100,68 @@ function urbanVersion(): string | null {
98
100
  return typeof pkg?.version === "string" ? pkg.version : null;
99
101
  }
100
102
 
103
+ /**
104
+ * The build identity baked into the release package (`build-info.json` at the package root) by
105
+ * `scripts/write-build-info.ts` during semantic-release's `prepare` step (issue #925). An npm
106
+ * install ships this file but no `.git`, so it is the ONLY source of `gitSha` there. Its `gitSha`
107
+ * is the SOURCE HEAD the release was cut from (the tag commit does not exist yet at prepare time);
108
+ * its `gitTag` names the tag placed on the release commit, whose tree matches that source HEAD
109
+ * except for the version-bump metadata. Returns null on a dev checkout, which has no such file.
110
+ */
111
+ export const SHA_RE = /^[0-9a-f]{40}$/;
112
+
113
+ /** Validate a parsed build-info payload; returns a list of problems (empty = valid). The single
114
+ * validator shared by the writer (scripts/write-build-info.ts) and the reader below. */
115
+ export function buildInfoProblems(info: unknown): string[] {
116
+ const problems: string[] = [];
117
+ if (typeof info !== "object" || info == null) return ["build-info.json is not a JSON object"];
118
+ // biome-ignore lint/plugin: runtime/framework contract boundary for external data shape (parsed JSON)
119
+ const bi = info as Record<string, unknown>;
120
+ if (typeof bi.version !== "string" || !bi.version.trim()) problems.push("version must be a non-blank string");
121
+ if (typeof bi.gitSha !== "string" || !SHA_RE.test(bi.gitSha)) problems.push("gitSha must be a 40-hex string");
122
+ if (typeof bi.gitTag !== "string" || !bi.gitTag.trim()) problems.push("gitTag must be a non-blank string");
123
+ if (typeof bi.builtAt !== "string" || Number.isNaN(Date.parse(bi.builtAt)))
124
+ problems.push("builtAt must be an ISO-8601 timestamp");
125
+ return problems;
126
+ }
127
+
128
+
129
+ function buildInfo(): { version: string; gitSha: string; gitTag: string; builtAt: string } | null {
130
+ const raw = readJson(join(REPO_ROOT, "build-info.json"));
131
+ if (raw == null) return null;
132
+ // Fail closed on ANY malformed field (bad SHA, unparseable builtAt, blank tag/version): a corrupt
133
+ // or tampered build-info.json is ignored wholesale rather than partially surfaced. Uses the SAME
134
+ // validator the writer's --check uses, so reader and writer can never drift.
135
+ if (buildInfoProblems(raw).length > 0) return null;
136
+ return {
137
+ version: String(raw.version),
138
+ gitSha: String(raw.gitSha),
139
+ gitTag: String(raw.gitTag),
140
+ builtAt: String(raw.builtAt),
141
+ };
142
+ }
143
+
101
144
  /**
102
145
  * The git commit the working tree is on, read straight from `.git` so it reflects the ACTUAL
103
146
  * checked-out code — not a value baked at some earlier build. Resolves a symbolic `HEAD`
104
147
  * (`ref: refs/heads/…`) via the loose ref file (checking both the per-worktree and common dirs),
105
- * falling back to `packed-refs`. An explicit `NANO_WORKFORCE_GIT_SHA` env var wins (for deploys
106
- * that ship without a `.git` directory).
148
+ * falling back to `packed-refs`. Resolution order (issue #925): an explicit
149
+ * `NANO_WORKFORCE_GIT_SHA` env var wins (for deploys that ship without a `.git` directory), then
150
+ * the live `.git` checkout, then the release-baked `build-info.json` (the only source available to
151
+ * an npm-installed instance).
107
152
  */
108
153
  function gitSha(): string | null {
109
154
  const env = envVar("NANO_WORKFORCE_GIT_SHA");
110
155
  if (env) return env;
111
156
 
157
+ const fromCheckout = gitShaFromCheckout();
158
+ if (fromCheckout != null) return fromCheckout;
159
+
160
+ return buildInfo()?.gitSha ?? null;
161
+ }
162
+
163
+ /** The commit SHA read from the live `.git` checkout, or null when there is none (npm install). */
164
+ function gitShaFromCheckout(): string | null {
112
165
  const dirs = resolveGitDirs();
113
166
  if (dirs == null) return null;
114
167
  const head = readText(join(dirs.gitDir, "HEAD"));
@@ -160,6 +213,10 @@ export interface VersionInfo {
160
213
  urbanVersion: string | null;
161
214
  gitSha: string | null;
162
215
  gitBranch: string | null;
216
+ /** The release tag (e.g. "v0.206.0") from the baked `build-info.json`; null on a dev checkout. */
217
+ gitTag: string | null;
218
+ /** ISO-8601 time the release package was built (from `build-info.json`); null on a dev checkout. */
219
+ builtAt: string | null;
163
220
  runtime: string;
164
221
  pid: number | null;
165
222
  startedAt: string;
@@ -172,12 +229,15 @@ export interface VersionInfo {
172
229
  */
173
230
  const STATIC: Omit<VersionInfo, "uptimeSeconds"> = (() => {
174
231
  const pkg = appPackage();
232
+ const build = buildInfo();
175
233
  return Object.freeze({
176
234
  name: appName(pkg),
177
235
  version: appVersion(pkg),
178
236
  urbanVersion: urbanVersion(),
179
237
  gitSha: gitSha(),
180
238
  gitBranch: gitBranch(),
239
+ gitTag: build?.gitTag || null,
240
+ builtAt: build?.builtAt || null,
181
241
  runtime: runtime(),
182
242
  pid: typeof process.pid === "number" ? process.pid : null,
183
243
  startedAt: STARTED_AT.toISOString(),
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "0.206.1",
3
+ "gitSha": "973c161d8b279db21915601647c5e8c43f3cac2f",
4
+ "gitTag": "v0.206.1",
5
+ "builtAt": "2026-11-08T20:26:54.809Z"
6
+ }
@@ -26,6 +26,11 @@ curl -sS __BASE__/version | jq # tool: getVersion: app/urban version, git sha,
26
26
  curl -sS __BASE__/status | jq # tool: listActivePrs: every PR in flight
27
27
  ```
28
28
 
29
+ `/status` also carries the running app's build identity as a top-level `version` object
30
+ (`{ version, gitSha }`, the same source as `/version` — env override, then the live `.git`
31
+ checkout, then the release-baked `build-info.json` an npm install ships), so the endpoint
32
+ everyone polls answers "is the fix live?" in one call.
33
+
29
34
  Each `/status` entry carries `prKey` (`owner/repo#123`), `status`, `round`, `processKey`
30
35
  (the **engine process-instance key**, the bridge to §5), `openEscalation`,
31
36
  `activeWorker`/`leaseUntil` (an agent holds the round, or the job is just queued), and
package/openapi.yaml CHANGED
@@ -195,6 +195,7 @@ components:
195
195
  - count
196
196
  - prs
197
197
  - deliveries
198
+ - version
198
199
  properties:
199
200
  count:
200
201
  type: integer
@@ -209,6 +210,26 @@ components:
209
210
  first (issue #889 §1 — the opt-in is shown for each PR AND each delivery).
210
211
  items:
211
212
  $ref: "#/components/schemas/ActiveDelivery"
213
+ version:
214
+ type: object
215
+ additionalProperties: false
216
+ required:
217
+ - version
218
+ - gitSha
219
+ description: >-
220
+ The running app's build identity (issue #925), derived from the same app/version.ts
221
+ getter as GET /version — so the endpoint everyone polls answers "is the fix live?" in
222
+ one call. gitSha resolves from NANO_WORKFORCE_GIT_SHA, then the .git checkout, then the
223
+ release-baked build-info.json (the only source an npm install has).
224
+ properties:
225
+ version:
226
+ type: string
227
+ nullable: true
228
+ description: The app's package version.
229
+ gitSha:
230
+ type: string
231
+ nullable: true
232
+ description: The commit the instance runs; null only when no source (env, .git, build-info.json) knows it.
212
233
  ActiveDelivery:
213
234
  type: object
214
235
  description: A delivery-graph run currently in flight, as reported by the status endpoint.
@@ -1561,6 +1582,8 @@ components:
1561
1582
  - urbanVersion
1562
1583
  - gitSha
1563
1584
  - gitBranch
1585
+ - gitTag
1586
+ - builtAt
1564
1587
  - runtime
1565
1588
  - pid
1566
1589
  - startedAt
@@ -1577,9 +1600,24 @@ components:
1577
1600
  gitSha:
1578
1601
  type: string
1579
1602
  nullable: true
1603
+ description: >-
1604
+ The commit the instance runs. Resolution order (issue #925): NANO_WORKFORCE_GIT_SHA env,
1605
+ then the live .git checkout, then the release-baked build-info.json (the only source an
1606
+ npm install has — this is the source HEAD the release was cut from).
1580
1607
  gitBranch:
1581
1608
  type: string
1582
1609
  nullable: true
1610
+ gitTag:
1611
+ type: string
1612
+ nullable: true
1613
+ description: >-
1614
+ The release tag (e.g. "v0.206.0") naming the release commit, from the baked
1615
+ build-info.json; null on a dev checkout. The tag commit's tree matches gitSha's tree
1616
+ except for the version-bump metadata.
1617
+ builtAt:
1618
+ type: string
1619
+ nullable: true
1620
+ description: ISO-8601 time the release package was built (from build-info.json); null on a dev checkout.
1583
1621
  runtime:
1584
1622
  type: string
1585
1623
  pid:
@@ -34,6 +34,9 @@ test("returns 200 with the app identity", async () => {
34
34
  assert("urbanVersion" in r.body);
35
35
  assert("gitSha" in r.body);
36
36
  assert("gitBranch" in r.body);
37
+ // Release-baked identity fields (issue #925): always present, null on a dev checkout.
38
+ assert("gitTag" in r.body);
39
+ assert("builtAt" in r.body);
37
40
  assert(typeof r.body.runtime === "string" && r.body.runtime.length > 0);
38
41
  assert(typeof r.body.startedAt === "string");
39
42
  assert(typeof r.body.uptimeSeconds === "number");
@@ -66,6 +66,21 @@ test("returns 200 with a count + projected active PRs", async () => {
66
66
  assertEquals(r.body.prs[0].processKey, "9");
67
67
  });
68
68
 
69
+ test("carries the running app's identity as a top-level version object (issue #925)", async () => {
70
+ // `/status` is the endpoint everyone polls, so it must answer "is the fix live?" on its own:
71
+ // version.version + version.gitSha, derived from the SAME app/version.ts getter /version uses.
72
+ const app = memApp([]);
73
+ const res = (await handler(input(), app)) as any;
74
+ assertEquals(res.status, 200);
75
+ assert("version" in res.body, "the status payload carries a top-level version object");
76
+ assert("version" in res.body.version, "version.version is present (string|null)");
77
+ assert("gitSha" in res.body.version, "version.gitSha is present (string|null)");
78
+ const { buildVersionInfo } = await import("../app/version.ts");
79
+ const identity = buildVersionInfo();
80
+ assertEquals(res.body.version.version, identity.version, "derived from the app/version.ts getter, not re-read");
81
+ assertEquals(res.body.version.gitSha, identity.gitSha, "derived from the app/version.ts getter, not re-read");
82
+ });
83
+
69
84
  test("surfaces the structured openEscalation from the user_tasks read model (both loops)", async () => {
70
85
  // Issue #666: openEscalation is derived from the ONE `user_tasks` read model (the same surface
71
86
  // `listEscalations` and the Convergence page consume), so `/status` carries the completable
@@ -8,7 +8,7 @@
8
8
  // callers must present it via the x-hook-secret header. Unset → open (unchanged default).
9
9
 
10
10
  import { activeDeliveries, activePrs } from "../app/service.ts";
11
- import { envVar } from "../app/version.ts";
11
+ import { buildVersionInfo, envVar } from "../app/version.ts";
12
12
  import { defineOperation } from "../nano-generated/operations.ts";
13
13
 
14
14
  // The optional shared-secret guard: when NANO_PR_WEBHOOK_SECRET is set, callers must present it via
@@ -21,5 +21,8 @@ export default defineOperation("listActivePrs", async ({ req }, app) => {
21
21
  return { status: 401, body: { error: "unauthorized" } };
22
22
  }
23
23
  const [prs, deliveries] = await Promise.all([activePrs(app.data, app.engine), activeDeliveries(app.data)]);
24
- return { status: 200, body: { count: prs.length, prs, deliveries } };
24
+ // The running app's identity, derived from the SAME app/version.ts getter /version uses (never
25
+ // re-read), so the endpoint everyone polls answers "is the fix live?" in one call (issue #925).
26
+ const { version, gitSha } = buildVersionInfo();
27
+ return { status: 200, body: { count: prs.length, prs, deliveries, version: { version, gitSha } } };
25
28
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanobpm/nano-workforce",
3
- "version": "0.205.2",
3
+ "version": "0.206.1",
4
4
  "description": "Nano Workforce — an Agent Graph Orchestration application for Agentic SDLC: durable BPMN processes that coordinate a graph of AI agents across the software delivery lifecycle.",
5
5
  "type": "module",
6
6
  "main": "main.ts",
@@ -74,6 +74,7 @@
74
74
  "@nanobpm/urban-testkit": "^1.6.0",
75
75
  "@nanobpm/workflow": "^0.15.0",
76
76
  "@semantic-release/changelog": "^7.0.0",
77
+ "@semantic-release/exec": "^7.1.0",
77
78
  "@semantic-release/git": "^11.0.0",
78
79
  "@semantic-release/npm": "^13.1.5",
79
80
  "@types/node": "^24.0.0",
@@ -0,0 +1,87 @@
1
+ // Tests for scripts/write-build-info.ts — the release-time bake of `build-info.json` (issue #925).
2
+ //
3
+ // The acceptance criterion is end-to-end: an `npm pack` of a tree whose build-info.json was written
4
+ // by this script CONTAINS the file, with a 40-hex gitSha — i.e. the published tarball carries the
5
+ // identity an npm-installed instance falls back to. Plus unit coverage of the payload the script
6
+ // composes (source-HEAD sha, `v<version>` tag, ISO builtAt) and its validation.
7
+ import { execFileSync } from "node:child_process";
8
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { dirname, join } from "node:path";
11
+ import { fileURLToPath } from "node:url";
12
+ import { test } from "node:test";
13
+ import { assert, assertEquals, assertThrows } from "#test-assert";
14
+ import { buildInfoProblems, composeBuildInfo } from "./write-build-info.ts";
15
+
16
+ const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
17
+ const BUILD_INFO = join(REPO_ROOT, "build-info.json");
18
+ const SHA_RE = /^[0-9a-f]{40}$/;
19
+
20
+ test("composeBuildInfo records the checkout HEAD (40-hex), the v<version> tag, and an ISO builtAt", () => {
21
+ const head = execFileSync("git", ["rev-parse", "HEAD"], { cwd: REPO_ROOT, encoding: "utf8" }).trim();
22
+ const info = composeBuildInfo("1.2.3", new Date("2026-01-02T03:04:05.000Z"));
23
+ assertEquals(info.gitSha, head);
24
+ assert(SHA_RE.test(info.gitSha));
25
+ assertEquals(info.gitTag, "v1.2.3");
26
+ assertEquals(info.builtAt, "2026-01-02T03:04:05.000Z");
27
+ assertEquals(buildInfoProblems(info), []);
28
+ });
29
+
30
+ test("composeBuildInfo honours an explicit source SHA instead of the checkout HEAD", () => {
31
+ // The manual OIDC-recovery path checks out the release TAG, whose HEAD is the metadata-only
32
+ // release commit — so it passes the source HEAD explicitly. The explicit SHA must win over
33
+ // `git rev-parse HEAD` and must be validated against the 40-hex contract.
34
+ const sourceHead = "b".repeat(40);
35
+ const info = composeBuildInfo("2.0.0", new Date("2026-01-02T03:04:05.000Z"), sourceHead);
36
+ assertEquals(info.gitSha, sourceHead);
37
+ assertEquals(info.gitTag, "v2.0.0");
38
+ assertEquals(buildInfoProblems(info), []);
39
+ });
40
+
41
+ test("composeBuildInfo rejects a malformed explicit source SHA (fail closed)", () => {
42
+ assertThrows(() => composeBuildInfo("2.0.0", new Date(), "not-a-sha"), Error, "non-SHA");
43
+ assertThrows(() => composeBuildInfo("2.0.0", new Date(), "abc123"), Error, "non-SHA");
44
+ });
45
+
46
+ test("buildInfoProblems rejects malformed payloads", () => {
47
+ assert(buildInfoProblems(null).length > 0);
48
+ assert(buildInfoProblems({ version: "1.0.0", gitSha: "not-a-sha", gitTag: "v1.0.0", builtAt: "2026-01-02T03:04:05.000Z" }).length > 0);
49
+ assert(buildInfoProblems({ version: "1.0.0", gitSha: "a".repeat(40), gitTag: "", builtAt: "2026-01-02T03:04:05.000Z" }).length > 0);
50
+ assert(buildInfoProblems({ version: "1.0.0", gitSha: "a".repeat(40), gitTag: "v1.0.0", builtAt: "not-a-date" }).length > 0);
51
+ });
52
+
53
+ test("an npm pack of the package contains build-info.json with a 40-hex SHA", () => {
54
+ // Write the file exactly as the release pipeline does (prepare step), pack, inspect, then clean
55
+ // up — build-info.json is a build artifact and MUST NOT be committed (a stale committed copy
56
+ // would mislabel every later dev checkout), so the test restores the prior state in `finally`.
57
+ const prior = (() => {
58
+ try {
59
+ return readFileSync(BUILD_INFO, "utf8");
60
+ } catch {
61
+ return null;
62
+ }
63
+ })();
64
+ const packDir = mkdtempSync(join(tmpdir(), "nwf-pack-"));
65
+ try {
66
+ execFileSync("node", ["--experimental-strip-types", "scripts/write-build-info.ts", "0.0.0-packtest"], {
67
+ cwd: REPO_ROOT,
68
+ stdio: "pipe",
69
+ });
70
+ const out = execFileSync("npm", ["pack", "--pack-destination", packDir], { cwd: REPO_ROOT, encoding: "utf8" });
71
+ const tarball = join(packDir, out.trim().split("\n").pop() ?? "");
72
+ const listing = execFileSync("tar", ["-tzf", tarball], { encoding: "utf8" });
73
+ assert(
74
+ listing.split("\n").some((f) => f === "package/build-info.json" || f === "build-info.json"),
75
+ `the tarball must contain build-info.json; got:\n${listing.split("\n").slice(0, 5).join("\n")}…`,
76
+ );
77
+ const raw = execFileSync("tar", ["-xzOf", tarball, "package/build-info.json"], { encoding: "utf8" });
78
+ const packed: unknown = JSON.parse(raw);
79
+ assertEquals(buildInfoProblems(packed), [], `packed build-info.json must be well-formed: ${raw}`);
80
+ const gitSha = (packed as { gitSha: string }).gitSha;
81
+ assert(SHA_RE.test(gitSha), `packed gitSha must be 40-hex, got ${gitSha}`);
82
+ } finally {
83
+ if (prior == null) rmSync(BUILD_INFO, { force: true });
84
+ else writeFileSync(BUILD_INFO, prior);
85
+ rmSync(packDir, { recursive: true, force: true });
86
+ }
87
+ });
@@ -0,0 +1,126 @@
1
+ // write-build-info — bake the release's build identity into `build-info.json` at the package root
2
+ // (issue #925).
3
+ //
4
+ // WHY: an npm-installed instance has no `.git` directory and no `NANO_WORKFORCE_GIT_SHA` env var,
5
+ // so `app/version.ts` reported `gitSha: null` on exactly the deploys an operator most needs to
6
+ // identify ("is the fix live on merlin?"). The release pipeline writes this file during
7
+ // semantic-release's `prepare` step — AFTER @semantic-release/npm bumps `package.json` to the
8
+ // release version and BEFORE it packs/publishes the tarball — so every published package carries
9
+ // its own identity and `app/version.ts` can fall back to it (env → .git → build-info.json).
10
+ //
11
+ // WHAT IT RECORDS (and why): `gitSha` is the SOURCE HEAD the release was cut from
12
+ // (`git rev-parse HEAD` in the release checkout), NOT the `chore(release): X.Y.Z` commit
13
+ // @semantic-release/git creates afterwards — that commit does not exist yet at prepare time, so it
14
+ // cannot be recorded here. The release TAG `vX.Y.Z` is later placed on that release commit, whose
15
+ // tree is identical to the source HEAD's tree except for the version-bump metadata (CHANGELOG.md,
16
+ // package.json, package-lock.json). `gitTag` carries the tag name so an operator can resolve the
17
+ // exact release commit (`git rev-list -1 <tag>`) when they need it; `gitSha` answers the
18
+ // day-to-day question "which source code is running".
19
+ //
20
+ // USAGE:
21
+ // node --experimental-strip-types scripts/write-build-info.ts <version> [--sha <40-hex>] # release pipeline
22
+ // node --experimental-strip-types scripts/write-build-info.ts --check # test: validate on disk
23
+ //
24
+ // `<version>` alone records the current checkout HEAD — correct at semantic-release `prepare` time.
25
+ // The manual OIDC-recovery path runs from a fresh checkout of the release TAG, whose HEAD is the
26
+ // metadata-only `chore(release)` commit, NOT the source HEAD; it passes `--sha "$(git rev-parse
27
+ // v<version>^")"` so the recorded gitSha still names the source code (see release.yml's recovery
28
+ // instructions).
29
+ //
30
+ // `--check` validates the committed/generated `build-info.json` (present, well-formed, 40-hex
31
+ // gitSha) — used by the pack test to assert the release artifact contract without cutting a
32
+ // release.
33
+ import { execFileSync } from "node:child_process";
34
+ import { writeFileSync } from "node:fs";
35
+ import { dirname, join } from "node:path";
36
+ import { fileURLToPath, pathToFileURL } from "node:url";
37
+
38
+ const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
39
+ export const BUILD_INFO_PATH = join(REPO_ROOT, "build-info.json");
40
+
41
+ export interface BuildInfo {
42
+ version: string;
43
+ gitSha: string;
44
+ gitTag: string;
45
+ builtAt: string;
46
+ }
47
+
48
+ // The ONE payload validator is shared with the reader (app/version.ts) so writer and reader can
49
+ // never disagree on what a valid build-info.json is.
50
+ export { buildInfoProblems } from "../app/version.ts";
51
+
52
+ import { buildInfoProblems, SHA_RE } from "../app/version.ts";
53
+
54
+ /**
55
+ * Compose the build-info payload for a release of `version`. `gitSha` is the SOURCE HEAD the
56
+ * release was cut from. By default it is read from the current checkout HEAD (`git rev-parse HEAD`)
57
+ * — correct at semantic-release `prepare` time, when HEAD is still the source commit. Callers that
58
+ * run LATER (the manual OIDC-recovery path, which checks out the release TAG) must pass `sha`
59
+ * explicitly as the source HEAD (`git rev-parse v<version>^`): by then HEAD is the metadata-only
60
+ * `chore(release)` commit, and recording it would mislabel the package with a commit whose only
61
+ * change is the version bump. An explicit `sha` is validated against the 40-hex contract.
62
+ */
63
+ export function composeBuildInfo(version: string, now: Date = new Date(), sha?: string): BuildInfo {
64
+ const gitSha = (
65
+ sha ?? execFileSync("git", ["rev-parse", "HEAD"], { cwd: REPO_ROOT, encoding: "utf8" })
66
+ ).trim();
67
+ if (!SHA_RE.test(gitSha)) throw new Error(`git rev-parse HEAD returned a non-SHA: ${JSON.stringify(gitSha)}`);
68
+ return {
69
+ version,
70
+ gitSha,
71
+ // The tag semantic-release will place on the release commit for this version.
72
+ gitTag: `v${version}`,
73
+ builtAt: now.toISOString(),
74
+ };
75
+ }
76
+
77
+ function main(argv: string[]): void {
78
+ const arg = argv[2];
79
+ if (arg === "--check") {
80
+ // Lazy import so the write path never needs fs/readFileSync beyond writeFileSync.
81
+ import("node:fs").then(({ readFileSync }) => {
82
+ let parsed: unknown;
83
+ try {
84
+ parsed = JSON.parse(readFileSync(BUILD_INFO_PATH, "utf8"));
85
+ } catch (err) {
86
+ // biome-ignore lint/plugin: narrowing a caught unknown to Error for its message
87
+ console.error(`build-info.json is missing or invalid JSON: ${(err as Error).message}`);
88
+ process.exit(1);
89
+ }
90
+ const problems = buildInfoProblems(parsed);
91
+ if (problems.length > 0) {
92
+ console.error(`build-info.json failed validation:\n - ${problems.join("\n - ")}`);
93
+ process.exit(1);
94
+ }
95
+ console.log(`build-info.json OK: ${JSON.stringify(parsed)}`);
96
+ });
97
+ return;
98
+ }
99
+ const version = arg?.trim();
100
+ if (!version) {
101
+ console.error("usage: write-build-info.ts <version> [--sha <40-hex>] | --check");
102
+ process.exit(1);
103
+ }
104
+ // Optional explicit source SHA (see composeBuildInfo). The manual OIDC-recovery path checks out
105
+ // the release TAG, whose HEAD is the release commit — so it passes the source HEAD
106
+ // (`git rev-parse v<version>^`) explicitly to keep gitSha pointing at the source code.
107
+ let sha: string | undefined;
108
+ const shaIdx = argv.indexOf("--sha");
109
+ if (shaIdx !== -1) {
110
+ sha = argv[shaIdx + 1]?.trim();
111
+ if (!sha || !SHA_RE.test(sha)) {
112
+ console.error(`--sha must be a 40-hex commit SHA, got ${JSON.stringify(argv[shaIdx + 1])}`);
113
+ process.exit(1);
114
+ }
115
+ }
116
+ const info = composeBuildInfo(version, new Date(), sha);
117
+ writeFileSync(BUILD_INFO_PATH, `${JSON.stringify(info, null, 2)}\n`);
118
+ console.log(`wrote build-info.json: version=${info.version} gitSha=${info.gitSha} gitTag=${info.gitTag}`);
119
+ }
120
+
121
+ // Run the CLI only when executed directly — importing this module (e.g. from the test) must not
122
+ // parse argv or write files.
123
+ const invokedAs = process.argv[1] ? pathToFileURL(process.argv[1]).href : "";
124
+ if (invokedAs === import.meta.url) {
125
+ main(process.argv);
126
+ }