openclaw/scripts/check-database-worker-ratchet.mts
Peter Steinberger cc14a17e5f
perf(ci): overlap baseline ratchets and reuse identical source snapshots
Run the same checks in two managed slots, retaining aggregate failures and
fatal cancellation cleanup. Keep frozen targets on their original commands.

Reuse fresh database-worker rows only when the complete current and historical
source maps match. Always validate current credential writers; changed snapshots
retain full historical analysis and the existing cache identity checks.

The local frozen-input full command fell from 713.31s to 505.67s, with 29% less
recorded CPU work. Both runs retain the same three inherited UI max-lines
failures. Focused boundary, queue and planner tests, typecheck, lint, formatting,
actionlint, script erasability and independent P2 review passed.
2026-10-20 22:59:31 -07:00

67 lines
2.7 KiB
TypeScript

import { inventory, loadInventorySources } from "./database-worker-inventory.mjs";
import { baseInventory } from "./lib/database-worker-inventory-cache.mts";
import { isDirectRunUrl } from "./lib/direct-run.mjs";
import {
compareRatchetCounts,
parseRatchetArgs,
reportRatchetFailures,
resolveRatchetBase,
} from "./lib/shrink-ratchet.mts";
export function main(root = process.cwd(), argv = process.argv.slice(2)) {
try {
const args = parseRatchetArgs(argv);
if (args.prune) {
throw new Error("SQLite worker ratchet has no baseline to prune.");
}
const base = resolveRatchetBase(root, args);
if (!base) {
throw new Error("SQLite worker ratchet requires a Git base commit.");
}
const counts = (rows: ReturnType<typeof inventory>, tier = "T1") =>
new Map(rows.filter((row) => row.tier === tier).map((row) => [row.file, row.calls.length]));
const sources = loadInventorySources(root, "", args.staged);
const head = inventory(root, "", args.staged, sources);
const baseline = baseInventory(root, base, { sources, rows: head });
const before = counts(baseline);
const after = counts(head);
const total = (files: ReadonlyMap<string, number>) =>
[...files.values()].reduce((sum, count) => sum + count, 0);
console.log(
`SQLite T1 live calls: ${total(before)} -> ${total(after)}; ` +
`T1-compat deprecated-only calls: ${total(counts(baseline, "T1-compat"))} -> ${total(counts(head, "T1-compat"))}.`,
);
const { increased } = compareRatchetCounts(after, before);
if (
total(after) > total(before) &&
reportRatchetFailures(
[
{
title: `Main-thread SQLite T1 comparison total grew: ${total(before)} -> ${total(after)}`,
entries: increased.flatMap(({ entry, allowed, current }) =>
[`${entry}: ${allowed} -> ${current}`].concat(
head
.filter((row) => row.file === entry)
.flatMap((row) => row.calls)
.map((call) => `${entry}:${call.line}:${call.column} ${call.primitive}`),
),
),
},
],
"Move SQL behind the owner's worker operation: docs/reference/database-schemas/worker-access.md\n" +
"If the file only executes inside a worker, name it *.worker.ts or add it to workerModules in scripts/database-worker-inventory.mjs with caller evidence.",
)
) {
return 1;
}
console.log("SQLite worker ratchet OK: no T1 call-count growth against " + base + ".");
return 0;
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
return 1;
}
}
if (isDirectRunUrl(process.argv[1], import.meta.url)) {
process.exitCode = main();
}