#!/usr/bin/env node // #1873 pi-unpin S5: the agent image tag. package.json gives pi a range (no pin); the version the // image installs is the one package-lock.json resolves, since the Containerfile runs npm ci. So the // tag is built from the lock's entry for pi and RELEASE, never from the package.json string: // mosaic-poc-agent:-r. A lock or RELEASE this can't read refuses. // // node scripts/image-tag.mjs [checkout] prints the tag (checkout: default this repository) import { readFileSync, realpathSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; export const PI_PACKAGE = "@earendil-works/pi-coding-agent"; export const PI_LOCK_KEY = `node_modules/${PI_PACKAGE}`; const VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/; const RELEASE = /^\d+\.\d+\.\d+$/; const fail = (message) => { throw new Error(`image tag: ${message}`); }; const readJson = (path, name) => { let text; try { text = readFileSync(path, "utf8"); } catch { fail(`${name} can't be read`); } try { return JSON.parse(text); } catch { fail(`${name} isn't valid JSON`); } }; // The lock's entry for pi: { version, resolved, integrity }. Refuses a lock with no entry, or one // whose version isn't a plain semver. export function lockedPi(root) { const lock = readJson(join(root, "package-lock.json"), "package-lock.json"); const entry = lock?.packages?.[PI_LOCK_KEY]; if (!entry || typeof entry !== "object") fail(`package-lock.json has no entry for ${PI_PACKAGE}`); if (typeof entry.version !== "string" || !VERSION.test(entry.version)) fail(`package-lock.json's ${PI_PACKAGE} version isn't a plain version`); return { version: entry.version, resolved: entry.resolved ?? null, integrity: entry.integrity ?? null }; } export function release(root) { let text; try { text = readFileSync(join(root, "RELEASE"), "utf8"); } catch { fail("RELEASE can't be read"); } const value = text.replace(/\s/g, ""); if (!RELEASE.test(value)) fail("RELEASE must be a version like 0.0.13"); return value; } export const imageTag = (root) => `mosaic-poc-agent:${lockedPi(root).version}-r${release(root)}`; const self = fileURLToPath(import.meta.url); let main = false; try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {} if (main) { try { process.stdout.write(`${imageTag(process.argv[2] ?? join(self, "..", ".."))}\n`); } catch (e) { process.stderr.write(`${e.message}\n`); process.exit(1); } }