/* * WHICH SERVER PACKAGE IS RUNNING — a LEAF module, importing nothing from * `tools/`. * * It lived in `transport.ts`, and `capabilities.ts` imported it from there. The * moment `status` needs to REPORT capabilities, that becomes * transport -> capabilities -> transport: a cycle, and the same one Task 9 * produced when the kind registry sat inside `record-events.ts`. That one only * worked because `tools/index.ts` happened to import in the surviving order — * a load-ORDER dependency, invisible until something imported a module * directly, which is what a consumer or a test does. * * A cycle that works by luck is not a working cycle, so the shared fact moves * out rather than the edge being added on top of it. * * NOTE WHAT THIS REPORTS AND WHAT IT DOES NOT. `version` is the label in the * package.json this process loaded from — CONTEXT for a reader, never evidence * of a capability. A published tarball has already been observed carrying a * change its version said it could not. Capability questions are answered by * `capabilities`, which CALLS the code. */ import { existsSync, readFileSync } from "node:fs"; import { spawnSync } from "node:child_process"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { isGitRepo } from "./tools/scopes.js"; /** Running server package + optional git identity. `fromDir` is a test seam. */ export function resolveServerIdentity(fromDir?: string): { path: string; version: string; branch?: string; sha?: string; } { const start = fromDir ?? path.dirname(fileURLToPath(import.meta.url)); let dir = start; let pkgFile: string | undefined; for (let i = 0; i < 8; i++) { const candidate = path.join(dir, "package.json"); if (existsSync(candidate)) { pkgFile = candidate; break; } const parent = path.dirname(dir); if (parent === dir) break; dir = parent; } const pkgPath = pkgFile ?? path.resolve(start, "..", "..", "package.json"); const pkgDir = path.dirname(pkgPath); let version = "unknown"; try { const raw = JSON.parse(readFileSync(pkgPath, "utf8")) as { version?: string }; if (typeof raw.version === "string" && raw.version) version = raw.version; } catch { /* leave unknown — never invent a version */ } const out: { path: string; version: string; branch?: string; sha?: string } = { path: pkgDir, version, }; dir = pkgDir; let gitRoot: string | undefined; for (let i = 0; i < 10; i++) { if (existsSync(path.join(dir, ".git"))) { gitRoot = dir; break; } const parent = path.dirname(dir); if (parent === dir) break; dir = parent; } if (gitRoot && isGitRepo(gitRoot)) { const branch = spawnSync("git", ["-C", gitRoot, "rev-parse", "--abbrev-ref", "HEAD"], { encoding: "utf8" }); const sha = spawnSync("git", ["-C", gitRoot, "rev-parse", "HEAD"], { encoding: "utf8" }); const b = branch.status === 0 ? String(branch.stdout).trim() : ""; const s = sha.status === 0 ? String(sha.stdout).trim() : ""; if (b) out.branch = b; if (s) out.sha = s; } return out; }