Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

chore(review): trip a breaker after five incidents in seven days

youxi7983619f4744f58
diff --git a/scripts/risk-tier.mjs b/scripts/risk-tier.mjs
index 174893ff..04f9f39b 100644
--- a/scripts/risk-tier.mjs
+++ b/scripts/risk-tier.mjs
@@ -17,6 +17,7 @@
// tests/unit/risk-tier.test.ts runs it.
import { spawnSync } from "node:child_process";
+import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
@@ -153,6 +154,75 @@ export function deadPatterns(
);
}
+// Circuit breaker: once users met BREAKER_ROWS defects within BREAKER_DAYS (docs/incidents.md, by Reported
+// date), every change that reaches a tier also needs the full gate, and the layers those defects slipped past are
+// named so they are tightened first. It clears on its own as the rows age out; a future date is never counted.
+export const BREAKER_DAYS = 7;
+export const BREAKER_ROWS = 5;
+export const BREAKER_GATE =
+ "the whole `pnpm run release:check` and `cargo test --lib` instead of single tests, and the maintainer reading each listed fix before the next release";
+
+const DAY = 86_400_000;
+const utcDay = (iso) =>
+ /^\d{4}-\d{2}-\d{2}$/.test(iso) ? Date.parse(`${iso}T00:00:00Z`) : NaN;
+
+export function recentIncidents({
+ today = new Date().toLocaleDateString("sv"),
+ ledger = path.join(ROOT, "docs", "incidents.md"),
+} = {}) {
+ let text;
+ try {
+ text = fs.readFileSync(ledger, "utf8");
+ } catch {
+ return [];
+ }
+ const now = utcDay(today);
+ let header = null;
+ const found = [];
+ for (const line of (text.split("\n## Incidents")[1] ?? text).split("\n")) {
+ if (!line.startsWith("|")) continue;
+ const cells = line
+ .trim()
+ .replace(/^\||\|$/g, "")
+ .split("|")
+ .map((cell) => cell.trim());
+ if (!header) {
+ header = cells;
+ continue;
+ }
+ if (/^[-: ]*$/.test(cells.join(""))) continue;
+ const row = Object.fromEntries(header.map((name, i) => [name, cells[i]]));
+ const age = (now - utcDay(row.Reported ?? "")) / DAY;
+ if (age >= 0 && age < BREAKER_DAYS) found.push([row.ID, row.Layer]);
+ }
+ return found;
+}
+
+export function breakerLine(recent) {
+ if (recent.length < BREAKER_ROWS) return null;
+ const counts = new Map();
+ for (const [, layer] of recent)
+ counts.set(layer, (counts.get(layer) ?? 0) + 1);
+ const layers = [...counts]
+ .sort((a, b) => b[1] - a[1])
+ .map(([layer, count]) => `${layer} ${count}`)
+ .join(", ");
+ const ids = recent.map(([id]) => id).join(", ");
+ return `risk: breaker (${recent.length} incidents in the last ${BREAKER_DAYS} days: ${ids}): until the rate drops every tiered change needs ${BREAKER_GATE}; tighten first the layers they slipped past (${layers})`;
+}
+
+export function report(paths, recent = recentIncidents()) {
+ const hits = tiersFor(paths);
+ const lines = hits.map((tier) => {
+ const more =
+ tier.matched.length > 1 ? ` and ${tier.matched.length - 1} more` : "";
+ return `risk: ${tier.name} (${tier.matched[0]}${more}) needs ${tier.gates}`;
+ });
+ const breaker = breakerLine(recent);
+ if (hits.length && breaker) lines.push(breaker);
+ return lines;
+}
+
if (
process.argv[1] &&
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
@@ -164,11 +234,5 @@ if (
process.exit(dead.length ? 1 : 0);
}
const paths = args.length ? args : pendingPaths();
- for (const tier of tiersFor(paths)) {
- const more =
- tier.matched.length > 1 ? ` and ${tier.matched.length - 1} more` : "";
- console.log(
- `risk: ${tier.name} (${tier.matched[0]}${more}) needs ${tier.gates}`,
- );
- }
+ for (const line of report(paths)) console.log(line);
}
diff --git a/tests/unit/risk-tier.test.ts b/tests/unit/risk-tier.test.ts
index d058ded2..af9f282d 100644
--- a/tests/unit/risk-tier.test.ts
+++ b/tests/unit/risk-tier.test.ts
@@ -1,9 +1,17 @@
import { describe, it, expect } from 'vitest';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
import {
TIERS,
tiersFor,
deadPatterns,
matches,
+ recentIncidents,
+ breakerLine,
+ report,
+ BREAKER_ROWS,
+ BREAKER_GATE,
} from '../../scripts/risk-tier.mjs';
const names = (paths: string[]) => tiersFor(paths).map((tier) => tier.name);
@@ -54,3 +62,66 @@ describe('risk tiers', () => {
expect(matches('xaction.yml', 'action.yml')).toBe(false);
});
});
+
+describe('incident breaker', () => {
+ const ledger = (rows: [string, string][]) => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pake-breaker-'));
+ const file = path.join(dir, 'incidents.md');
+ const body = rows
+ .map(
+ ([day, layer], i) =>
+ `| I-${String(i + 1).padStart(2, '0')} | ${day} | s | unknown | abc1234 | ${layer} | \`t\` |`,
+ )
+ .join('\n');
+ fs.writeFileSync(
+ file,
+ '# Incidents\n\n| ID | Reported | Symptom |\n|---|---|---|\n| X-01 | 2026-10-27 | outside |\n\n' +
+ '## Incidents\n\n| ID | Reported | Symptom | Introduced | Fixed | Layer | Caught by |\n' +
+ `|---|---|---|---|---|---|---|\n${body}\n`,
+ );
+ return file;
+ };
+ const recent = (rows: [string, string][]) =>
+ recentIncidents({ today: '2026-10-27', ledger: ledger(rows) });
+
+ it('counts only rows reported in the last seven days', () => {
+ const rows = recent([
+ ['2026-10-27', 'unit'],
+ ['2026-10-21', 'unit'],
+ ['2026-10-20', 'unit'],
+ ['2026-10-28', 'unit'],
+ ]);
+ // Day 0 and day 6 count; day 7 has aged out; a future date is never counted.
+ expect(rows.map(([id]) => id)).toEqual(['I-01', 'I-02']);
+ });
+
+ it('trips at five rows and names the layers by count', () => {
+ expect(
+ breakerLine(recent(Array(4).fill(['2026-10-26', 'unit']))),
+ ).toBeNull();
+ const line = breakerLine(
+ recent([
+ ...Array(3).fill(['2026-10-26', 'review']),
+ ...Array(2).fill(['2026-10-25', 'unit']),
+ ]),
+ );
+ expect(line).toContain('5 incidents in the last 7 days');
+ expect(line).toContain(BREAKER_GATE);
+ expect(line?.endsWith('(review 3, unit 2)')).toBe(true);
+ });
+
+ it('never trips on a missing ledger', () => {
+ expect(
+ recentIncidents({
+ today: '2026-10-27',
+ ledger: '/nonexistent/incidents.md',
+ }),
+ ).toEqual([]);
+ });
+
+ it('is printed only beside a tier', () => {
+ const tripped = Array(BREAKER_ROWS).fill(['I-01', 'unit']);
+ expect(report(['docs/faq.md'], tripped)).toEqual([]);
+ expect(report(['package.json'], tripped).at(-1)).toMatch(/^risk: breaker /);
+ });
+});