perf(web): deduplicate control plane gate state
This commit is contained in:
21
tools/web/wbv2-failure-ledger.mjs
Normal file
21
tools/web/wbv2-failure-ledger.mjs
Normal file
@@ -0,0 +1,21 @@
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const ledgerPath = (root) => path.join(root, "tests/golden/WBV2/failure-ledger.json");
|
||||
const keyFor = (entry) => crypto.createHash("sha256").update(JSON.stringify({ task: entry.task, phase: entry.phase, reason: entry.reason, childExit: entry.childExit ?? null })).digest("hex");
|
||||
|
||||
export function recordFailure(root, entry) {
|
||||
const file = ledgerPath(root);
|
||||
let ledger = { schemaVersion: 1, entries: [] };
|
||||
try { ledger = JSON.parse(fs.readFileSync(file, "utf8")); }
|
||||
catch { /* First failure or an interrupted write. */ }
|
||||
const key = keyFor(entry);
|
||||
if (ledger.entries.some((candidate) => candidate.key === key)) return false;
|
||||
ledger.entries = [...ledger.entries, { ...entry, key, recordedAt: new Date().toISOString() }].slice(-200);
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true });
|
||||
const temporary = `${file}.tmp-${process.pid}`;
|
||||
fs.writeFileSync(temporary, `${JSON.stringify(ledger, null, 2)}\n`);
|
||||
fs.renameSync(temporary, file);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user