mirror of
https://github.com/bitbaum/fleet.git
synced 2026-12-04 06:07:50 +01:00
359 lines
15 KiB
JavaScript
359 lines
15 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* The fleet's INTERNAL packages, kept current by something other than memory.
|
|
*
|
|
* ── The hole this fills, precisely ───────────────────────────────────────────
|
|
*
|
|
* `version-currency.mjs` judges a dependency by its MAJOR (`parseMajor`, and
|
|
* `blessed-versions.json` is literally called `majors`). That is the right
|
|
* instrument for `next`, `react` and `typescript`, where a major is the event
|
|
* and a minor is noise.
|
|
*
|
|
* It is the wrong instrument for our own packages, and the difference is not
|
|
* academic. Measured 2026-09-21, `@bitbaum/ai-kit` was published at **1.10.0**
|
|
* and its consumers read:
|
|
*
|
|
* heidi ^1.10.0 · loki ^1.8.0 · substrata ^1.7.0 · orangecat ^1.6.0
|
|
* aoz-begleitung, botsmann, evig, hirnli, surf-your-life, truthseeker,
|
|
* vitareba ^1.4.0
|
|
* kivvi (packages/ai) ^0.15.0 · sbb-fundbuero ^0.15.0
|
|
* datacat (backend) ^0.13.0
|
|
*
|
|
* THIRTEEN of fourteen are behind — only heidi is current. Ten of those
|
|
* thirteen are on MAJOR 1, so `gapsFor()` reports **nothing at all** about
|
|
* them; the other three are the 0.x strandings below. The ratchet is green
|
|
* while the fleet spans seven minor versions of the package that owns model
|
|
* ids, failover, the three kinds of 429 and now vision routing.
|
|
*
|
|
* (This block first said "eleven of fourteen", which was arithmetic that
|
|
* contradicted its own next sentence: ten on major 1 plus three stranded is
|
|
* thirteen. The first live run counted 13 and tripped a cap set from the wrong
|
|
* number — see MAX_PRS. Corrected against the run, not re-derived by hand.)
|
|
*
|
|
* This is not a flaw in that audit. We ship features in minors, so for our own
|
|
* packages the minor IS the event, and an instrument tuned for `react` cannot
|
|
* see it. Hence a second instrument rather than a change to the first.
|
|
*
|
|
* ── Reporting is what already failed ─────────────────────────────────────────
|
|
*
|
|
* SHARED.md has said "version spread is the metric to watch" and "getting a
|
|
* repo current is the cheapest work in this file and nobody schedules it"
|
|
* since before that table was measured. Both sentences were true and neither
|
|
* moved a version. The fleet's own history is unambiguous: adoption has only
|
|
* ever moved as a side effect of repairing an outage.
|
|
*
|
|
* So this does not report. It opens the pull request. Everything downstream
|
|
* already exists and already works:
|
|
*
|
|
* reconciler → branch + PR → repo CI → auto-merge-sweep → CD → box
|
|
*
|
|
* A bump whose CI is green merges and deploys with nobody in the loop. A bump
|
|
* whose CI is red sits as an open PR that says exactly which repo cannot take
|
|
* the new version — which is the single most useful thing a package author can
|
|
* be told, and today nobody tells them.
|
|
*
|
|
* ── What it deliberately will NOT do ─────────────────────────────────────────
|
|
*
|
|
* Only Bitbaum-owned packages: scoped `@bitbaum/*` names plus unscoped names
|
|
* explicitly present in Fleet's curated npm package register. Auto-bumping
|
|
* `next` or `react` across the fleet on a schedule is a different risk; these
|
|
* remain with `version-currency.mjs`, which reports third-party majors for a
|
|
* human. Git-tag dependencies are intentionally excluded until release tags
|
|
* and their stability channel have a separate, reviewed contract.
|
|
*
|
|
* It does not EDIT anything. It decides, and prints the plan; the workflow
|
|
* does the checkout, the `pnpm update` (so the lockfile is regenerated by the
|
|
* package manager rather than hand-patched) and the PR. Splitting there is
|
|
* what keeps every decision in this file testable without a network.
|
|
*
|
|
* It never fails a pipeline for being behind. A gate that fixes the thing it
|
|
* measures has nothing to gain from also going red — and a red that nobody can
|
|
* clear by hand is the dead end this fleet's own doctrine forbids. It exits
|
|
* non-zero only when it could not do its job.
|
|
*
|
|
* node scripts/ci/currency-reconciler.mjs # the plan, human-readable
|
|
* node scripts/ci/currency-reconciler.mjs --json # the same plan, as a matrix
|
|
*/
|
|
|
|
import { execFileSync } from "node:child_process";
|
|
import { readFileSync } from "node:fs";
|
|
import { listRepos, fetchManifest, listDir, manifestPaths } from "./version-currency.mjs";
|
|
|
|
/** Our own packages, and nobody else's. See "what it will NOT do". */
|
|
export const INTERNAL_SCOPE = "@bitbaum/";
|
|
|
|
/**
|
|
* A cap, for the same reason `deploy-reconciler.mjs` has one.
|
|
*
|
|
* If this wants to open forty pull requests, the overwhelmingly likelier
|
|
* explanation is a bug in the predicate than forty genuinely stale repos —
|
|
* and forty wrong PRs across the org is expensive to undo. Over the cap it
|
|
* prints the plan and opens nothing.
|
|
*
|
|
* RAISED 12 → 20, from a measurement rather than a feeling. The first live
|
|
* run swept 43 repos and found 13 wanting a bump; 12 was set from a docblock
|
|
* that had miscounted the same fleet as eleven. So the cap fired on its very
|
|
* first run against a plan that was entirely correct — the right behaviour for
|
|
* the wrong reason, and exactly the false positive that teaches people to
|
|
* raise a cap without reading it.
|
|
*
|
|
* 20 keeps the guard meaningful: it is well under the 43 repos swept, so "the
|
|
* whole fleet at once" still refuses, while 13 known-genuine bumps pass. Raise
|
|
* it again only against another run's output, and say which run.
|
|
*/
|
|
export const MAX_PRS = 20;
|
|
|
|
/**
|
|
* Split a registry range into its operator and its floor version.
|
|
*
|
|
* Returns null for anything that is not a plain registry range — a git pin, a
|
|
* `workspace:*`, a `file:`, a URL, a tag like `latest`, or a range complex
|
|
* enough that rewriting it mechanically would be guessing (`>=1.2 <2`, `||`).
|
|
* Null means LEAVE IT ALONE, which is the safe direction: this script's worst
|
|
* possible bug is quietly rewriting a range somebody chose deliberately.
|
|
*/
|
|
export function parseRange(range) {
|
|
if (typeof range !== "string") return null;
|
|
const raw = range.trim();
|
|
const m = /^([\^~]?)(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?$/.exec(raw);
|
|
if (!m) return null;
|
|
return { op: m[1], major: Number(m[2]), minor: Number(m[3]), patch: Number(m[4]) };
|
|
}
|
|
|
|
/** A published version string, or null if it is not a plain release. */
|
|
export function parseVersion(value) {
|
|
if (typeof value !== "string") return null;
|
|
const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(value.trim());
|
|
return m ? { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]) } : null;
|
|
}
|
|
|
|
/** -1, 0, 1. Ordinary semver ordering over release versions. */
|
|
export function compareVersions(a, b) {
|
|
if (a.major !== b.major) return a.major < b.major ? -1 : 1;
|
|
if (a.minor !== b.minor) return a.minor < b.minor ? -1 : 1;
|
|
if (a.patch !== b.patch) return a.patch < b.patch ? -1 : 1;
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Is this range's FLOOR behind the latest published version?
|
|
*
|
|
* The floor, not the ceiling, and that distinction is the whole measurement.
|
|
* `^1.4.0` *resolves* to 1.11.0 on a fresh install — but no repo installs
|
|
* fresh: every one pins through a lockfile, which is what makes shared
|
|
* packages safe here and is also why a caret alone changes nothing. The floor
|
|
* is what the manifest COMMITS to, and raising it is what makes `pnpm update`
|
|
* produce a lockfile change somebody can review.
|
|
*
|
|
* `^0.15.0` against 1.11.0 is the severe case and is reported as such: a caret
|
|
* on a 0.x version pins the MINOR, so those repos cannot receive a fix even by
|
|
* running `pnpm update`. They need this deliberate bump or they get nothing,
|
|
* ever.
|
|
*/
|
|
export function behind(range, latest) {
|
|
const have = parseRange(range);
|
|
const want = parseVersion(latest);
|
|
if (!have || !want) return null;
|
|
if (compareVersions(have, want) >= 0) return null;
|
|
return {
|
|
from: range,
|
|
to: bumpRange(range, latest),
|
|
// A 0.x caret cannot cross its own minor, so `pnpm update` will not move
|
|
// it and no ordinary currency sweep ever will either.
|
|
stranded: have.major === 0 && have.major !== want.major,
|
|
crossesMajor: have.major !== want.major,
|
|
};
|
|
}
|
|
|
|
/** The same range, with its floor raised. The operator is preserved. */
|
|
export function bumpRange(range, latest) {
|
|
const have = parseRange(range);
|
|
if (!have) return range;
|
|
return `${have.op}${latest}`;
|
|
}
|
|
|
|
/** Registered Bitbaum npm entries of one manifest, deps and devDeps alike. */
|
|
export function npmInternalNames(packages) {
|
|
return new Set((packages?.packages ?? [])
|
|
.filter((pkg) => pkg?.install?.source === "npm" && pkg?.version && pkg?.name)
|
|
.map((pkg) => pkg.name));
|
|
}
|
|
|
|
/** Resolve a direct internal dependency or an npm alias to its package identity. */
|
|
export function internalDependency(name, specifier, npmNames = new Set()) {
|
|
let packageName = name;
|
|
let range = specifier;
|
|
let alias = false;
|
|
|
|
if (typeof specifier === "string" && specifier.startsWith("npm:")) {
|
|
const target = specifier.slice(4);
|
|
const separator = target.lastIndexOf("@");
|
|
if (separator <= target.indexOf("/")) return null;
|
|
packageName = target.slice(0, separator);
|
|
range = target.slice(separator + 1);
|
|
alias = true;
|
|
}
|
|
|
|
const owned = packageName.startsWith(INTERNAL_SCOPE) || npmNames.has(packageName);
|
|
if (!owned || !parseRange(range)) return null;
|
|
return { packageName, range, alias };
|
|
}
|
|
|
|
/** The fleet's published npm packages from the curated shared registry. */
|
|
export function internalDepsOf(pkg, npmNames = new Set()) {
|
|
const deps = { ...(pkg?.dependencies || {}), ...(pkg?.devDependencies || {}) };
|
|
const out = {};
|
|
for (const [name, range] of Object.entries(deps)) {
|
|
if (internalDependency(name, range, npmNames)) out[name] = range;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Everything one manifest is behind on.
|
|
*
|
|
* Pure: hand it a manifest and a `{name: latestVersion}` map and it answers
|
|
* without touching the network, which is what makes the self-test real.
|
|
*/
|
|
export function planForManifest(repo, path, pkg, latestByName, npmNames = new Set()) {
|
|
const rows = [];
|
|
for (const [name, specifier] of Object.entries(internalDepsOf(pkg, npmNames))) {
|
|
const identity = internalDependency(name, specifier, npmNames);
|
|
if (!identity) continue;
|
|
const latest = latestByName[identity.packageName];
|
|
if (!latest) continue; // never published, or npm could not be asked
|
|
const gap = behind(identity.range, latest);
|
|
if (gap) {
|
|
const to = identity.alias ? `npm:${identity.packageName}@${bumpRange(identity.range, latest)}` : bumpRange(identity.range, latest);
|
|
rows.push({ repo, path, name, packageName: identity.packageName, latest, ...gap, from: specifier, to });
|
|
}
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
/**
|
|
* One branch per repo, named for what it does rather than for when it ran.
|
|
*
|
|
* Stable on purpose. A re-run finds its own open PR and updates that branch
|
|
* instead of opening a second one, so a reconciler that fires every week
|
|
* cannot accumulate a drawer of near-identical pull requests — which is the
|
|
* failure mode that gets bots muted, and a muted bot is worse than no bot.
|
|
*/
|
|
export function branchFor() {
|
|
return "fleet/internal-currency";
|
|
}
|
|
|
|
/** `npm view <name> version`, or null if it could not be asked. */
|
|
export function latestOf(name, { run = npmView } = {}) {
|
|
try {
|
|
const out = run(name);
|
|
return parseVersion(out) ? out.trim() : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function npmView(name) {
|
|
return execFileSync("npm", ["view", name, "version"], {
|
|
encoding: "utf8",
|
|
timeout: 60_000,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
}).trim();
|
|
}
|
|
|
|
/** Group flat rows into one entry per repo — the shape the workflow matrix wants. */
|
|
export function byRepo(rows) {
|
|
const repos = new Map();
|
|
for (const row of rows) {
|
|
if (!repos.has(row.repo)) repos.set(row.repo, { repo: row.repo, bumps: [] });
|
|
repos.get(row.repo).bumps.push(row);
|
|
}
|
|
return [...repos.values()].sort((a, b) => a.repo.localeCompare(b.repo));
|
|
}
|
|
|
|
function describe(row) {
|
|
const flag = row.stranded ? " STRANDED" : row.crossesMajor ? " (major)" : "";
|
|
const where = row.path === "package.json" ? "" : ` ${row.path.replace(/\/package\.json$/, "")}/`;
|
|
return `${row.name} ${row.from} → ${row.to}${flag}${where}`;
|
|
}
|
|
|
|
async function main() {
|
|
const owner = process.env.GH_OWNER || "bitbaum";
|
|
const asJson = process.argv.includes("--json");
|
|
|
|
const { repos } = listRepos(owner, 200);
|
|
const registry = JSON.parse(readFileSync(new URL("../../registers/packages.json", import.meta.url), "utf8"));
|
|
const npmNames = npmInternalNames(registry);
|
|
const rows = [];
|
|
const unreadable = [];
|
|
const latestByName = {};
|
|
|
|
for (const repo of repos) {
|
|
const rootNames = listDir(owner, repo, "");
|
|
// Could not look is NOT the same as nothing there — the distinction this
|
|
// fleet's audits keep having to relearn. An unreadable repo is named, and
|
|
// is never silently counted as current.
|
|
if (rootNames === null) {
|
|
unreadable.push(repo);
|
|
continue;
|
|
}
|
|
|
|
for (const path of manifestPaths(owner, repo, rootNames)) {
|
|
const pkg = fetchManifest(owner, repo, path);
|
|
if (!pkg) continue;
|
|
|
|
for (const [name, specifier] of Object.entries(internalDepsOf(pkg, npmNames))) {
|
|
const identity = internalDependency(name, specifier, npmNames);
|
|
if (identity && !(identity.packageName in latestByName)) {
|
|
latestByName[identity.packageName] = latestOf(identity.packageName);
|
|
}
|
|
}
|
|
rows.push(...planForManifest(repo, path, pkg, latestByName, npmNames));
|
|
}
|
|
}
|
|
|
|
const plan = byRepo(rows);
|
|
|
|
if (asJson) {
|
|
// Over the cap, emit an EMPTY matrix rather than the plan. The workflow
|
|
// acts on whatever this prints, so refusing here is the refusal.
|
|
console.log(JSON.stringify(plan.length > MAX_PRS ? [] : plan));
|
|
return;
|
|
}
|
|
|
|
console.log(`Internal package currency across ${repos.length} repos in ${owner}:\n`);
|
|
|
|
if (plan.length === 0) {
|
|
console.log("✓ every registered shared npm dependency is at the published version.");
|
|
} else {
|
|
for (const entry of plan) {
|
|
console.log(` ${entry.repo}`);
|
|
for (const row of entry.bumps) console.log(` ${describe(row)}`);
|
|
}
|
|
const stranded = rows.filter((r) => r.stranded);
|
|
if (stranded.length) {
|
|
console.log(
|
|
`\n ${stranded.length} STRANDED: a caret on 0.x pins the MINOR, so these cannot` +
|
|
`\n receive a fix even by running \`pnpm update\`. Only a deliberate bump moves them.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (unreadable.length) {
|
|
console.log(`\n UNCHECKED (could not read): ${unreadable.join(", ")}`);
|
|
console.log(" Not a pass. Re-run with a token that can see them.");
|
|
}
|
|
|
|
if (plan.length > MAX_PRS) {
|
|
console.error(
|
|
`\n✗ ${plan.length} repos want a bump, over the cap of ${MAX_PRS}. Opening NONE.` +
|
|
`\n A whole fleet going stale at once is likelier a bug in this predicate than` +
|
|
`\n ${plan.length} genuinely stale repos. Read the plan above before raising the cap.`,
|
|
);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
|
|
const invokedDirectly = process.argv[1] && import.meta.url === `file://${process.argv[1]}`;
|
|
if (invokedDirectly) await main();
|