返回 CodeWhale
release-baseline-lib.mjs
根目录 / web / scripts / release-baseline-lib.mjs
1 import { existsSync, readFileSync, writeFileSync } from "node:fs";
2 import { spawnSync } from "node:child_process";
3 import { dirname, resolve } from "node:path";
4 import { fileURLToPath } from "node:url";
5 import { REPO_ROOT, countToolSpecs, parseFeatureRows } from "./facts-lib.mjs";
6 import { parseModelCatalog } from "../lib/model-catalog.mjs";
7 import { parseProviderDescriptors } from "../lib/provider-descriptors.mjs";
8
9 const here = dirname(fileURLToPath(import.meta.url));
10 export const BASELINE_PATH = resolve(here, "..", "data", "release-baseline.json");
11 export const BASELINE_FORMAT = "codewhale.release-baseline.v1";
12 const TOOL_DIRECTORIES = ["crates/engine/src/tools", "crates/tui/src/tools"];
13
14 function git(args) {
15 const result = spawnSync("git", ["-C", REPO_ROOT, ...args], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
16 return result.status === 0 ? result.stdout : null;
17 }
18
19 export function tagResolves(tag) {
20 return /^v\d+\.\d+\.\d+$/.test(tag) && git(["rev-parse", "--verify", "--quiet", `${tag}^{commit}`]) !== null;
21 }
22
23 function showAtTag(tag, path) {
24 return git(["show", `${tag}:${path}`]);
25 }
26
27 function toolCountAtTag(tag) {
28 for (const directory of TOOL_DIRECTORIES) {
29 const listing = git(["ls-tree", "--name-only", tag, `${directory}/`]);
30 const files = listing ? listing.split("\n").filter((name) => name.endsWith(".rs")) : [];
31 if (files.length === 0) continue;
32 const bodies = files.map((name) => showAtTag(tag, name));
33 if (bodies.some((body) => body === null)) return null;
34 return countToolSpecs(bodies);
35 }
36 return null;
37 }
38
39 export function deriveReleaseBaseline(release) {
40 if (!release || !tagResolves(release.tag)) return null;
41 const registry = showAtTag(release.tag, "docs/features.toml");
42 const descriptors = parseProviderDescriptors(showAtTag(release.tag, "crates/config/assets/provider_descriptors.json"));
43 const models = parseModelCatalog(showAtTag(release.tag, "crates/config/assets/catalog_corrections.json"));
44 if (!registry || !descriptors || !models) return null;
45 const features = parseFeatureRows(registry).map((row) => ({
46 id: row.id,
47 name: row.name,
48 summary: row.summary,
49 status: row.status,
50 since: typeof row.since === "string" ? row.since : null,
51 }));
52 return {
53 format: BASELINE_FORMAT,
54 tag: release.tag,
55 version: release.version,
56 features,
57 providers: descriptors.providers,
58 models,
59 toolCount: toolCountAtTag(release.tag),
60 };
61 }
62
63 export function readCommittedBaseline() {
64 if (!existsSync(BASELINE_PATH)) return null;
65 try { return JSON.parse(readFileSync(BASELINE_PATH, "utf8")); } catch { return null; }
66 }
67
68 export function serializeBaseline(baseline) {
69 return `${JSON.stringify(baseline, null, 2)}\n`;
70 }
71
72 export function checkReleaseBaseline(committedBaseline, release, derivedBaseline, { allowShallow = false } = {}) {
73 const problems = [];
74 if (!committedBaseline || !release || committedBaseline.tag !== release.tag) {
75 problems.push(`data/release-baseline.json is for ${committedBaseline?.tag ?? "nothing"}, the published release is ${release?.tag ?? "unknown"}; run npm run prebuild in a checkout that has the tag`);
76 return problems;
77 }
78 if (!derivedBaseline) {
79 if (!allowShallow) {
80 problems.push(`tag ${committedBaseline.tag} is not in this checkout, so data/release-baseline.json could not be verified against the tagged source; fetch tags or pass --allow-shallow / CODEWHALE_ALLOW_SHALLOW_CHECKOUT=1`);
81 }
82 return problems;
83 }
84 if (serializeBaseline(derivedBaseline) !== serializeBaseline(committedBaseline)) {
85 problems.push("data/release-baseline.json differs from the tagged source; run npm run prebuild");
86 }
87 return problems;
88 }
89
90 export function refreshReleaseBaseline(release) {
91 const committed = readCommittedBaseline();
92 const fresh = deriveReleaseBaseline(release);
93 if (!fresh) {
94 const note = !release ? "no published release" : committed?.tag === release.tag
95 ? `tag ${release.tag} is not available in this checkout, keeping the committed baseline`
96 : `tag ${release.tag} is not available in this checkout and the committed baseline is for ${committed?.tag ?? "nothing"}; capability rows and counts stay hidden`;
97 console.warn(`[release-baseline] ${note}`);
98 return committed;
99 }
100 const text = serializeBaseline(fresh);
101 if (!existsSync(BASELINE_PATH) || readFileSync(BASELINE_PATH, "utf8") !== text) writeFileSync(BASELINE_PATH, text);
102 return fresh;
103 }
104
104 lines Plain Text