wachiman 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -101
- package/package.json +1 -1
- package/skills/wachiman-loop/SKILL.md +91 -0
- package/src/call-message.ts +26 -3
- package/src/cli.ts +81 -5
- package/src/config.ts +19 -0
- package/src/connectors/local-git.ts +101 -13
- package/src/cost.ts +20 -0
- package/src/doctor/checks.ts +1 -1
- package/src/engine.ts +532 -55
- package/src/estimate.ts +64 -0
- package/src/fidelity/block.ts +263 -0
- package/src/fidelity/claims-call.ts +172 -0
- package/src/fidelity/claims.ts +69 -0
- package/src/fidelity/digest.ts +80 -0
- package/src/fidelity/grade.ts +242 -0
- package/src/fidelity/inert.ts +25 -0
- package/src/fidelity/rules.ts +166 -0
- package/src/fidelity/sink.ts +135 -0
- package/src/fidelity/summary.ts +16 -0
- package/src/fidelity/types.ts +93 -0
- package/src/formats.ts +16 -3
- package/src/history/fingerprint.ts +1 -1
- package/src/history/store.ts +39 -1
- package/src/init/options.ts +1 -1
- package/src/local.ts +20 -2
- package/src/loop/command.ts +441 -0
- package/src/loop/state.ts +88 -0
- package/src/loop/stop-rule.ts +98 -0
- package/src/providers/claude-code.ts +47 -12
- package/src/providers/codex.ts +3 -2
- package/src/providers/index.ts +5 -2
- package/src/providers/types.ts +10 -0
- package/src/sinks/json.ts +13 -2
- package/src/skills/install.ts +4 -4
- package/src/store/command.ts +109 -0
- package/src/store/figures.ts +153 -0
- package/src/store/html.ts +138 -0
- package/src/store/index.ts +298 -0
- package/templates/caller-workflow.yml +45 -15
- package/templates/wachiman-review-caller.yml +8 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the dashboard shows, read from the index with plain queries. Everything here is a figure the
|
|
3
|
+
* run files record; a figure they do not record is marked `not recorded` rather than guessed.
|
|
4
|
+
*/
|
|
5
|
+
import { Database } from "bun:sqlite";
|
|
6
|
+
import { GRADES, type Grade } from "../fidelity/types";
|
|
7
|
+
import { VERDICT_WORDS } from "../history/types";
|
|
8
|
+
|
|
9
|
+
export const SEVERITY_ORDER = ["blocker", "fix", "consider", "info"] as const;
|
|
10
|
+
export const LOOP_STATUSES = ["new", "open", "probably-fixed", "not-repeated", "unknown"] as const;
|
|
11
|
+
const NOT_RECORDED = "not recorded";
|
|
12
|
+
|
|
13
|
+
export interface SpendDay {
|
|
14
|
+
day: string;
|
|
15
|
+
runs: number;
|
|
16
|
+
costUsdEstimate: number;
|
|
17
|
+
}
|
|
18
|
+
export interface SpendRepository {
|
|
19
|
+
key: string;
|
|
20
|
+
name: string;
|
|
21
|
+
runs: number;
|
|
22
|
+
costUsdEstimate: number;
|
|
23
|
+
}
|
|
24
|
+
export interface LoopBranch {
|
|
25
|
+
repositoryKey: string;
|
|
26
|
+
repository: string;
|
|
27
|
+
branch: string;
|
|
28
|
+
base: string;
|
|
29
|
+
outcome: "open" | "converged";
|
|
30
|
+
/** The loop state keeps only open or converged; why a loop stopped is printed by `loop` and never saved. */
|
|
31
|
+
exitReason: typeof NOT_RECORDED;
|
|
32
|
+
rounds: number;
|
|
33
|
+
firstRoundAt?: string;
|
|
34
|
+
lastRoundAt?: string;
|
|
35
|
+
ledger: { total: number; byStatus: Record<(typeof LOOP_STATUSES)[number], number> };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type PrConfidence =
|
|
39
|
+
| { status: typeof NOT_RECORDED; note: string }
|
|
40
|
+
| { status: "recorded"; gradedRuns: number; byGrade: Record<Grade, number> };
|
|
41
|
+
|
|
42
|
+
export interface Figures {
|
|
43
|
+
totals: { repositories: number; runs: number; costUsdEstimate: number };
|
|
44
|
+
spend: { byDay: SpendDay[]; byRepository: SpendRepository[] };
|
|
45
|
+
findings: {
|
|
46
|
+
total: number;
|
|
47
|
+
bySeverity: Record<(typeof SEVERITY_ORDER)[number], number>;
|
|
48
|
+
byVerdict: Record<"correct" | "partial" | "wrong" | "unjudged", number>;
|
|
49
|
+
};
|
|
50
|
+
loops: {
|
|
51
|
+
branches: LoopBranch[];
|
|
52
|
+
totals: { branches: number; rounds: number; converged: number; open: number };
|
|
53
|
+
};
|
|
54
|
+
prConfidence: PrConfidence;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Dollars are summed as floating point; six decimals hide the noise and lose no cent. */
|
|
58
|
+
const money = (n: number) => Math.round(n * 1e6) / 1e6;
|
|
59
|
+
|
|
60
|
+
export function readFigures(indexPath: string): Figures {
|
|
61
|
+
const db = new Database(indexPath, { readonly: true });
|
|
62
|
+
try {
|
|
63
|
+
const all = <T>(sql: string, ...params: string[]) => db.query(sql).all(...params) as T[];
|
|
64
|
+
|
|
65
|
+
const totals = db.query("SELECT COUNT(*) AS runs, COALESCE(SUM(cost_usd), 0) AS cost FROM runs").get() as { runs: number; cost: number };
|
|
66
|
+
const repositories = (db.query("SELECT COUNT(*) AS n FROM repositories").get() as { n: number }).n;
|
|
67
|
+
|
|
68
|
+
const byDay = all<{ day: string; runs: number; cost: number }>("SELECT day, COUNT(*) AS runs, SUM(cost_usd) AS cost FROM runs GROUP BY day ORDER BY day").map(
|
|
69
|
+
(r): SpendDay => ({ day: r.day, runs: r.runs, costUsdEstimate: money(r.cost) }),
|
|
70
|
+
);
|
|
71
|
+
const byRepository = all<{ key: string; name: string; runs: number; cost: number }>(
|
|
72
|
+
`SELECT r.key AS key, r.name AS name, COUNT(u.repo_key) AS runs, COALESCE(SUM(u.cost_usd), 0) AS cost
|
|
73
|
+
FROM repositories r LEFT JOIN runs u ON u.repo_key = r.key GROUP BY r.key ORDER BY cost DESC, r.key`,
|
|
74
|
+
).map((r): SpendRepository => ({ key: r.key, name: r.name, runs: r.runs, costUsdEstimate: money(r.cost) }));
|
|
75
|
+
|
|
76
|
+
const bySeverity = Object.fromEntries(SEVERITY_ORDER.map((s) => [s, 0])) as Figures["findings"]["bySeverity"];
|
|
77
|
+
for (const r of all<{ severity: string; n: number }>("SELECT severity, COUNT(*) AS n FROM findings GROUP BY severity")) {
|
|
78
|
+
if (r.severity in bySeverity) bySeverity[r.severity as keyof typeof bySeverity] = r.n;
|
|
79
|
+
}
|
|
80
|
+
const byVerdict = { correct: 0, partial: 0, wrong: 0, unjudged: 0 };
|
|
81
|
+
for (const r of all<{ verdict: string | null; n: number }>("SELECT verdict, COUNT(*) AS n FROM findings GROUP BY verdict")) {
|
|
82
|
+
const word = r.verdict ?? "unjudged";
|
|
83
|
+
if (word === "unjudged" || (VERDICT_WORDS as readonly string[]).includes(word)) byVerdict[word as keyof typeof byVerdict] = r.n;
|
|
84
|
+
}
|
|
85
|
+
const findingsTotal = (db.query("SELECT COUNT(*) AS n FROM findings").get() as { n: number }).n;
|
|
86
|
+
|
|
87
|
+
const ledgerRows = all<{ repo_key: string; branch: string; status: string; count: number }>("SELECT repo_key, branch, status, count FROM loop_ledger");
|
|
88
|
+
const branches = all<{
|
|
89
|
+
repo_key: string;
|
|
90
|
+
name: string;
|
|
91
|
+
branch: string;
|
|
92
|
+
base: string;
|
|
93
|
+
outcome: "open" | "converged";
|
|
94
|
+
rounds: number;
|
|
95
|
+
first_round_at: string | null;
|
|
96
|
+
last_round_at: string | null;
|
|
97
|
+
}>(
|
|
98
|
+
`SELECT l.repo_key, COALESCE(r.name, l.repo_key) AS name, l.branch, l.base, l.outcome, l.rounds, l.first_round_at, l.last_round_at
|
|
99
|
+
FROM loops l LEFT JOIN repositories r ON r.key = l.repo_key ORDER BY l.repo_key, l.branch`,
|
|
100
|
+
).map((l): LoopBranch => {
|
|
101
|
+
const byStatus = Object.fromEntries(LOOP_STATUSES.map((s) => [s, 0])) as LoopBranch["ledger"]["byStatus"];
|
|
102
|
+
let total = 0;
|
|
103
|
+
for (const e of ledgerRows) {
|
|
104
|
+
if (e.repo_key !== l.repo_key || e.branch !== l.branch) continue;
|
|
105
|
+
total += e.count;
|
|
106
|
+
if (e.status in byStatus) byStatus[e.status as keyof typeof byStatus] += e.count;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
repositoryKey: l.repo_key,
|
|
110
|
+
repository: l.name,
|
|
111
|
+
branch: l.branch,
|
|
112
|
+
base: l.base,
|
|
113
|
+
outcome: l.outcome,
|
|
114
|
+
exitReason: NOT_RECORDED,
|
|
115
|
+
rounds: l.rounds,
|
|
116
|
+
...(l.first_round_at ? { firstRoundAt: l.first_round_at } : {}),
|
|
117
|
+
...(l.last_round_at ? { lastRoundAt: l.last_round_at } : {}),
|
|
118
|
+
ledger: { total, byStatus },
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const grades = all<{ grade: string; n: number }>("SELECT grade, COUNT(*) AS n FROM runs WHERE grade IS NOT NULL GROUP BY grade");
|
|
123
|
+
let prConfidence: PrConfidence;
|
|
124
|
+
if (grades.length === 0) {
|
|
125
|
+
prConfidence = {
|
|
126
|
+
status: NOT_RECORDED,
|
|
127
|
+
note: "not recorded: only GitHub pull request reviews compute a grade, and they do not write the local review history, so no run file here carries one.",
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
const byGrade = Object.fromEntries(GRADES.map((g) => [g, 0])) as Record<Grade, number>;
|
|
131
|
+
for (const g of grades) byGrade[g.grade as Grade] = g.n;
|
|
132
|
+
prConfidence = { status: "recorded", gradedRuns: grades.reduce((n, g) => n + g.n, 0), byGrade };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
totals: { repositories, runs: totals.runs, costUsdEstimate: money(totals.cost) },
|
|
137
|
+
spend: { byDay, byRepository },
|
|
138
|
+
findings: { total: findingsTotal, bySeverity, byVerdict },
|
|
139
|
+
loops: {
|
|
140
|
+
branches,
|
|
141
|
+
totals: {
|
|
142
|
+
branches: branches.length,
|
|
143
|
+
rounds: branches.reduce((n, b) => n + b.rounds, 0),
|
|
144
|
+
converged: branches.filter((b) => b.outcome === "converged").length,
|
|
145
|
+
open: branches.filter((b) => b.outcome === "open").length,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
prConfidence,
|
|
149
|
+
};
|
|
150
|
+
} finally {
|
|
151
|
+
db.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dashboard as one static HTML file: inline CSS, no script, no font, no image, no link and no
|
|
3
|
+
* request of any kind, so it opens offline from a file. A page security policy (`default-src 'none'`)
|
|
4
|
+
* says the same to the browser. Every string that came from a history file goes through `esc`.
|
|
5
|
+
*/
|
|
6
|
+
import { GRADES } from "../fidelity/types";
|
|
7
|
+
import { SEVERITY_ORDER, type Figures } from "./figures";
|
|
8
|
+
|
|
9
|
+
export interface DashboardDocument extends Figures {
|
|
10
|
+
format: string;
|
|
11
|
+
generatedAt: string;
|
|
12
|
+
scope: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Text for an element or a quoted attribute. */
|
|
16
|
+
export function esc(text: unknown): string {
|
|
17
|
+
return String(text)
|
|
18
|
+
.replace(/&/g, "&")
|
|
19
|
+
.replace(/</g, "<")
|
|
20
|
+
.replace(/>/g, ">")
|
|
21
|
+
.replace(/"/g, """)
|
|
22
|
+
.replace(/'/g, "'");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const usd = (n: number) => `$${n.toFixed(2)}`;
|
|
26
|
+
const SEVERITY_WORD: Record<string, string> = { blocker: "Blocker", fix: "Fix", consider: "Consider", info: "Info" };
|
|
27
|
+
|
|
28
|
+
/** A bar as wide as `value` is of `max`, drawn as inline SVG; `max` 0 gives an empty bar. */
|
|
29
|
+
function bar(value: number, max: number): string {
|
|
30
|
+
const pct = max > 0 ? Math.max(0, Math.min(100, (value / max) * 100)) : 0;
|
|
31
|
+
return `<svg class="bar" viewBox="0 0 100 8" preserveAspectRatio="none" aria-hidden="true"><rect class="track" width="100" height="8"/><rect class="fill" width="${pct.toFixed(2)}" height="8"/></svg>`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function table(head: string[], rows: string[][], numeric: number[] = []): string {
|
|
35
|
+
const cell = (tag: string, c: string, i: number) => `<${tag}${numeric.includes(i) ? ' class="num"' : ""}>${c}</${tag}>`;
|
|
36
|
+
return `<table><thead><tr>${head.map((h, i) => cell("th", esc(h), i)).join("")}</tr></thead><tbody>${rows
|
|
37
|
+
.map((r) => `<tr>${r.map((c, i) => cell("td", c, i)).join("")}</tr>`)
|
|
38
|
+
.join("")}</tbody></table>`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const empty = (text: string) => `<p class="muted">${esc(text)}</p>`;
|
|
42
|
+
|
|
43
|
+
export function renderHtml(doc: DashboardDocument): string {
|
|
44
|
+
const { totals, spend, findings, loops, prConfidence } = doc;
|
|
45
|
+
|
|
46
|
+
const maxDay = Math.max(0, ...spend.byDay.map((d) => d.costUsdEstimate));
|
|
47
|
+
const maxRepo = Math.max(0, ...spend.byRepository.map((r) => r.costUsdEstimate));
|
|
48
|
+
const maxSeverity = Math.max(0, ...Object.values(findings.bySeverity));
|
|
49
|
+
const maxVerdict = Math.max(0, ...Object.values(findings.byVerdict));
|
|
50
|
+
|
|
51
|
+
const spendDay = spend.byDay.length
|
|
52
|
+
? table(["Day", "Runs", "Estimated spend", ""], spend.byDay.map((d) => [esc(d.day), String(d.runs), usd(d.costUsdEstimate), bar(d.costUsdEstimate, maxDay)]), [1, 2])
|
|
53
|
+
: empty("No local run kept yet.");
|
|
54
|
+
const spendRepo = spend.byRepository.length
|
|
55
|
+
? table(["Repository", "Runs", "Estimated spend", ""], spend.byRepository.map((r) => [`${esc(r.name)} <span class="muted">${esc(r.key)}</span>`, String(r.runs), usd(r.costUsdEstimate), bar(r.costUsdEstimate, maxRepo)]), [1, 2])
|
|
56
|
+
: empty("No repository in the history.");
|
|
57
|
+
|
|
58
|
+
const bySeverity = table(
|
|
59
|
+
["Severity", "Findings", ""],
|
|
60
|
+
SEVERITY_ORDER.map((s) => [esc(SEVERITY_WORD[s]), String(findings.bySeverity[s]), bar(findings.bySeverity[s], maxSeverity)]),
|
|
61
|
+
[1],
|
|
62
|
+
);
|
|
63
|
+
const byVerdict = table(
|
|
64
|
+
["Your verdict", "Findings", ""],
|
|
65
|
+
(["correct", "partial", "wrong", "unjudged"] as const).map((v) => [esc(v), String(findings.byVerdict[v]), bar(findings.byVerdict[v], maxVerdict)]),
|
|
66
|
+
[1],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const loopTable = loops.branches.length
|
|
70
|
+
? table(
|
|
71
|
+
["Repository", "Branch", "Rounds", "Outcome", "Exit reason", "Findings seen"],
|
|
72
|
+
loops.branches.map((b) => [esc(b.repository), esc(b.branch), String(b.rounds), esc(b.outcome), esc(b.exitReason), String(b.ledger.total)]),
|
|
73
|
+
[2, 5],
|
|
74
|
+
)
|
|
75
|
+
: empty("No loop kept yet: `wachiman loop --base <ref>` starts one.");
|
|
76
|
+
|
|
77
|
+
const confidence =
|
|
78
|
+
prConfidence.status === "recorded"
|
|
79
|
+
? `${table(["Grade", "Runs"], [...GRADES].reverse().map((g) => [esc(g), String(prConfidence.byGrade[g])]), [1])}<p class="muted">${prConfidence.gradedRuns} graded run${prConfidence.gradedRuns === 1 ? "" : "s"}.</p>`
|
|
80
|
+
: empty(prConfidence.note);
|
|
81
|
+
|
|
82
|
+
return `<!doctype html>
|
|
83
|
+
<html lang="en">
|
|
84
|
+
<head>
|
|
85
|
+
<meta charset="utf-8">
|
|
86
|
+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'">
|
|
87
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
88
|
+
<title>Wachiman dashboard</title>
|
|
89
|
+
<style>
|
|
90
|
+
:root { color-scheme: light dark; --bg: #fbfbf9; --fg: #1d1d1b; --muted: #6b6b66; --line: #dcdcd6; --fill: #2f6f9f; --track: #ecece6; }
|
|
91
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #161615; --fg: #ecece8; --muted: #9a9a94; --line: #34342f; --fill: #6fa8d6; --track: #262623; } }
|
|
92
|
+
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, sans-serif; }
|
|
93
|
+
main { max-width: 56rem; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
|
|
94
|
+
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
|
|
95
|
+
h2 { font-size: 1.05rem; margin: 2.25rem 0 .5rem; }
|
|
96
|
+
p { margin: .25rem 0 .75rem; }
|
|
97
|
+
.muted { color: var(--muted); }
|
|
98
|
+
.tiles { display: flex; flex-wrap: wrap; gap: 1.5rem; margin: 1.25rem 0 0; }
|
|
99
|
+
.tile b { display: block; font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums; }
|
|
100
|
+
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
|
|
101
|
+
th, td { text-align: left; padding: .35rem .6rem .35rem 0; border-bottom: 1px solid var(--line); vertical-align: middle; }
|
|
102
|
+
th { font-weight: 500; color: var(--muted); font-size: .85rem; }
|
|
103
|
+
.num { text-align: right; }
|
|
104
|
+
.bar { width: 100%; min-width: 6rem; height: .5rem; display: block; }
|
|
105
|
+
.track { fill: var(--track); }
|
|
106
|
+
.fill { fill: var(--fill); }
|
|
107
|
+
</style>
|
|
108
|
+
</head>
|
|
109
|
+
<body>
|
|
110
|
+
<main>
|
|
111
|
+
<h1>Wachiman dashboard</h1>
|
|
112
|
+
<p class="muted">${esc(doc.scope)}</p>
|
|
113
|
+
<p class="muted">Generated ${esc(doc.generatedAt)} from the local history. Spend is an API-equivalent estimate, not a bill. Nothing on this page was sent anywhere.</p>
|
|
114
|
+
<div class="tiles">
|
|
115
|
+
<div class="tile"><b>${totals.runs}</b>runs</div>
|
|
116
|
+
<div class="tile"><b>${usd(totals.costUsdEstimate)}</b>estimated spend</div>
|
|
117
|
+
<div class="tile"><b>${totals.repositories}</b>repositories</div>
|
|
118
|
+
<div class="tile"><b>${findings.total}</b>findings</div>
|
|
119
|
+
<div class="tile"><b>${loops.totals.rounds}</b>loop rounds</div>
|
|
120
|
+
</div>
|
|
121
|
+
<h2>Spend per day</h2>
|
|
122
|
+
${spendDay}
|
|
123
|
+
<h2>Spend per repository</h2>
|
|
124
|
+
${spendRepo}
|
|
125
|
+
<h2>Findings by severity</h2>
|
|
126
|
+
${bySeverity}
|
|
127
|
+
<h2>Findings by your verdict</h2>
|
|
128
|
+
${byVerdict}
|
|
129
|
+
<h2>Loops</h2>
|
|
130
|
+
${loopTable}
|
|
131
|
+
<p class="muted">${loops.totals.branches} loop${loops.totals.branches === 1 ? "" : "s"}: ${loops.totals.converged} converged, ${loops.totals.open} open. The loop state keeps only open or converged, so the reason a loop stopped is not recorded.</p>
|
|
132
|
+
<h2>PR Confidence grades</h2>
|
|
133
|
+
${confidence}
|
|
134
|
+
</main>
|
|
135
|
+
</body>
|
|
136
|
+
</html>
|
|
137
|
+
`;
|
|
138
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The local index (ADR 0035): one SQLite file under the Wachiman home, filled by replaying the history
|
|
3
|
+
* run files, `verdicts.jsonl` and the loop state files. It holds nothing that cannot be rebuilt from
|
|
4
|
+
* them, and no other command reads it: `wachiman dashboard` is the only reader. A missing, outdated or
|
|
5
|
+
* unreadable index is deleted and built again, never repaired; it is written to a temporary file and
|
|
6
|
+
* renamed, so a reader sees all of it or none, at 0600. Bun's built-in `bun:sqlite`: no dependency.
|
|
7
|
+
*/
|
|
8
|
+
import { Database } from "bun:sqlite";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync } from "node:fs";
|
|
11
|
+
import { basename, dirname, join } from "node:path";
|
|
12
|
+
import { GRADES } from "../fidelity/types";
|
|
13
|
+
import { historyRoot, listKeys, loopFiles, readRuns, readVerdicts, runFiles } from "../history/store";
|
|
14
|
+
import { replay } from "../history/match";
|
|
15
|
+
|
|
16
|
+
/** Bump when a table or a column changes: an index of another version is rebuilt, never read. */
|
|
17
|
+
export const INDEX_SCHEMA_VERSION = 1;
|
|
18
|
+
|
|
19
|
+
export const INDEX_FILE = "index.sqlite";
|
|
20
|
+
export const indexPath = (wachimanHome: string) => join(wachimanHome, INDEX_FILE);
|
|
21
|
+
|
|
22
|
+
const SCHEMA = `
|
|
23
|
+
CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
24
|
+
CREATE TABLE repositories (key TEXT PRIMARY KEY, name TEXT NOT NULL);
|
|
25
|
+
CREATE TABLE runs (
|
|
26
|
+
repo_key TEXT NOT NULL,
|
|
27
|
+
saved_at TEXT NOT NULL,
|
|
28
|
+
day TEXT NOT NULL,
|
|
29
|
+
head_sha TEXT NOT NULL,
|
|
30
|
+
model TEXT NOT NULL,
|
|
31
|
+
cost_usd REAL NOT NULL,
|
|
32
|
+
findings INTEGER NOT NULL,
|
|
33
|
+
grade TEXT
|
|
34
|
+
);
|
|
35
|
+
CREATE TABLE findings (
|
|
36
|
+
repo_key TEXT NOT NULL,
|
|
37
|
+
first_id TEXT NOT NULL,
|
|
38
|
+
severity TEXT NOT NULL,
|
|
39
|
+
topic TEXT NOT NULL,
|
|
40
|
+
status TEXT NOT NULL,
|
|
41
|
+
verdict TEXT,
|
|
42
|
+
last_seen_at TEXT NOT NULL
|
|
43
|
+
);
|
|
44
|
+
CREATE TABLE verdicts (repo_key TEXT NOT NULL, first_id TEXT NOT NULL, at TEXT, verdict TEXT NOT NULL);
|
|
45
|
+
CREATE TABLE loops (
|
|
46
|
+
repo_key TEXT NOT NULL,
|
|
47
|
+
branch TEXT NOT NULL,
|
|
48
|
+
base TEXT NOT NULL,
|
|
49
|
+
outcome TEXT NOT NULL,
|
|
50
|
+
rounds INTEGER NOT NULL,
|
|
51
|
+
first_round_at TEXT,
|
|
52
|
+
last_round_at TEXT
|
|
53
|
+
);
|
|
54
|
+
CREATE TABLE loop_ledger (repo_key TEXT NOT NULL, branch TEXT NOT NULL, status TEXT NOT NULL, count INTEGER NOT NULL);
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const vanished = (error: unknown) => (error as NodeJS.ErrnoException)?.code === "ENOENT";
|
|
58
|
+
|
|
59
|
+
/** Runs `fn`; when a file or directory it listed was removed meanwhile (a prune, a forget, another dashboard), once more. */
|
|
60
|
+
function retryOnce<T>(fn: () => T): T {
|
|
61
|
+
try {
|
|
62
|
+
return fn();
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (!vanished(error)) throw error;
|
|
65
|
+
return fn();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Stat of every file the index is built from: when it equals the one stored in the index, nothing
|
|
71
|
+
* was added, edited or removed since, and the index is reused. Size and modification time in
|
|
72
|
+
* nanoseconds; no file is read. A file that vanishes between the listing and the stat is absent.
|
|
73
|
+
*/
|
|
74
|
+
function sourceSignature(root: string, afterListing?: () => void): string {
|
|
75
|
+
const listed: { name: string; path: string }[] = [];
|
|
76
|
+
for (const key of listKeys(root)) {
|
|
77
|
+
for (const [sub, names] of [["runs", runFiles(root, key)], ["loops", loopFiles(root, key)]] as const) {
|
|
78
|
+
for (const path of names) listed.push({ name: `${key}/${sub}/${basename(path)}`, path });
|
|
79
|
+
}
|
|
80
|
+
const verdicts = join(root, key, "verdicts.jsonl");
|
|
81
|
+
if (existsSync(verdicts)) listed.push({ name: `${key}/verdicts.jsonl`, path: verdicts });
|
|
82
|
+
}
|
|
83
|
+
afterListing?.();
|
|
84
|
+
const lines: string[] = [];
|
|
85
|
+
for (const { name, path } of listed) {
|
|
86
|
+
try {
|
|
87
|
+
const s = statSync(path, { bigint: true });
|
|
88
|
+
lines.push(`${name}\t${s.size}\t${s.mtimeNs}`);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (!vanished(error)) throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return createHash("sha256").update(lines.sort().join("\n")).digest("hex");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** One probe per table: it fails when the table or any column the figures read is missing. */
|
|
97
|
+
const PROBES = [
|
|
98
|
+
"SELECT key, value FROM meta LIMIT 0",
|
|
99
|
+
"SELECT key, name FROM repositories LIMIT 0",
|
|
100
|
+
"SELECT repo_key, saved_at, day, head_sha, model, cost_usd, findings, grade FROM runs LIMIT 0",
|
|
101
|
+
"SELECT repo_key, first_id, severity, topic, status, verdict, last_seen_at FROM findings LIMIT 0",
|
|
102
|
+
"SELECT repo_key, first_id, at, verdict FROM verdicts LIMIT 0",
|
|
103
|
+
"SELECT repo_key, branch, base, outcome, rounds, first_round_at, last_round_at FROM loops LIMIT 0",
|
|
104
|
+
"SELECT repo_key, branch, status, count FROM loop_ledger LIMIT 0",
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
/** Whether the file at `path` is a readable index of this schema version built from sources that look like `signature`. */
|
|
108
|
+
function upToDate(path: string, signature: string): boolean {
|
|
109
|
+
let db: Database | undefined;
|
|
110
|
+
try {
|
|
111
|
+
db = new Database(path, { readonly: true });
|
|
112
|
+
const version = (db.query("PRAGMA user_version").get() as { user_version: number }).user_version;
|
|
113
|
+
if (version !== INDEX_SCHEMA_VERSION) return false;
|
|
114
|
+
const check = db.query("PRAGMA quick_check").get() as { quick_check: string };
|
|
115
|
+
if (check.quick_check !== "ok") return false;
|
|
116
|
+
for (const probe of PROBES) db.query(probe).all();
|
|
117
|
+
const row = db.query("SELECT value FROM meta WHERE key = 'signature'").get() as { value: string } | null;
|
|
118
|
+
return row?.value === signature;
|
|
119
|
+
} catch {
|
|
120
|
+
return false;
|
|
121
|
+
} finally {
|
|
122
|
+
db?.close();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface LoopRecord {
|
|
127
|
+
branch: string;
|
|
128
|
+
base: string;
|
|
129
|
+
outcome: "open" | "converged";
|
|
130
|
+
rounds: { at: string }[];
|
|
131
|
+
ledger: { status: string }[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A loop state file read leniently: only the fields the index stores. Undefined when its content is
|
|
136
|
+
* not a loop state (it does not parse or has the wrong shape; the caller warns and skips it). Null
|
|
137
|
+
* when the file vanished while it was read, which counts as absent (read once more first). Any other
|
|
138
|
+
* read error (permissions, I/O) is thrown, not reported as "not a loop state".
|
|
139
|
+
*/
|
|
140
|
+
function readLoopRecord(path: string): LoopRecord | undefined | null {
|
|
141
|
+
let text: string;
|
|
142
|
+
try {
|
|
143
|
+
text = retryOnce(() => readFileSync(path, "utf8"));
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (vanished(error)) return null;
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
const s = JSON.parse(text);
|
|
150
|
+
if (!s || typeof s !== "object" || s.format !== "wachiman.loop-state/1") return undefined;
|
|
151
|
+
if (typeof s.branch !== "string" || typeof s.base !== "string" || (s.status !== "open" && s.status !== "converged")) return undefined;
|
|
152
|
+
if (!Array.isArray(s.rounds) || !s.rounds.every((r: any) => r && typeof r.at === "string")) return undefined;
|
|
153
|
+
const ledger = Array.isArray(s.ledger) ? s.ledger.filter((e: any) => e && typeof e.status === "string") : [];
|
|
154
|
+
return { branch: s.branch, base: s.base, outcome: s.status, rounds: s.rounds, ledger };
|
|
155
|
+
} catch {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Fills the tables; returns how many loop files were skipped because their content is not a loop state. */
|
|
161
|
+
function fill(db: Database, root: string, warn: (line: string) => void): number {
|
|
162
|
+
let skipped = 0;
|
|
163
|
+
const repo = db.prepare("INSERT INTO repositories (key, name) VALUES (?, ?)");
|
|
164
|
+
const run = db.prepare("INSERT INTO runs (repo_key, saved_at, day, head_sha, model, cost_usd, findings, grade) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
165
|
+
const finding = db.prepare("INSERT INTO findings (repo_key, first_id, severity, topic, status, verdict, last_seen_at) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
166
|
+
const verdict = db.prepare("INSERT INTO verdicts (repo_key, first_id, at, verdict) VALUES (?, ?, ?, ?)");
|
|
167
|
+
const loop = db.prepare("INSERT INTO loops (repo_key, branch, base, outcome, rounds, first_round_at, last_round_at) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
168
|
+
const ledger = db.prepare("INSERT INTO loop_ledger (repo_key, branch, status, count) VALUES (?, ?, ?, ?)");
|
|
169
|
+
|
|
170
|
+
for (const key of listKeys(root)) {
|
|
171
|
+
const runs = readRuns(root, key, warn);
|
|
172
|
+
const verdicts = readVerdicts(root, key);
|
|
173
|
+
const loops = loopFiles(root, key).flatMap((path) => {
|
|
174
|
+
const record = readLoopRecord(path);
|
|
175
|
+
if (record === undefined) {
|
|
176
|
+
skipped++;
|
|
177
|
+
warn(`dashboard: ${path} is not a loop state; skipped`);
|
|
178
|
+
}
|
|
179
|
+
return record ? [record] : [];
|
|
180
|
+
});
|
|
181
|
+
if (runs.length === 0 && loops.length === 0) continue;
|
|
182
|
+
repo.run(key, runs.at(-1)?.repository.name ?? key);
|
|
183
|
+
for (const r of runs) {
|
|
184
|
+
const cost = r.result.execution?.costUsdEstimate;
|
|
185
|
+
const grade = (r.result as { fidelity?: { grade?: unknown } }).fidelity?.grade;
|
|
186
|
+
run.run(
|
|
187
|
+
key,
|
|
188
|
+
r.savedAt,
|
|
189
|
+
r.savedAt.slice(0, 10),
|
|
190
|
+
r.result.change.head.sha,
|
|
191
|
+
r.result.provider?.model ?? "",
|
|
192
|
+
typeof cost === "number" && Number.isFinite(cost) ? cost : 0,
|
|
193
|
+
r.result.findings.length,
|
|
194
|
+
typeof grade === "string" && (GRADES as readonly string[]).includes(grade) ? grade : null,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
for (const f of replay(runs, verdicts).findings) {
|
|
198
|
+
finding.run(key, f.firstId, f.severity, f.topic, f.status, f.verdict?.verdict ?? null, f.lastSeenAt);
|
|
199
|
+
}
|
|
200
|
+
for (const v of verdicts.values()) verdict.run(key, v.firstId, typeof v.at === "string" ? v.at : null, v.verdict);
|
|
201
|
+
for (const l of loops) {
|
|
202
|
+
loop.run(key, l.branch, l.base, l.outcome, l.rounds.length, l.rounds[0]?.at ?? null, l.rounds.at(-1)?.at ?? null);
|
|
203
|
+
const counts = new Map<string, number>();
|
|
204
|
+
for (const e of l.ledger) counts.set(e.status, (counts.get(e.status) ?? 0) + 1);
|
|
205
|
+
for (const [status, count] of counts) ledger.run(key, l.branch, status, count);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return skipped;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const unlinkQuiet = (path: string) => {
|
|
212
|
+
try {
|
|
213
|
+
unlinkSync(path);
|
|
214
|
+
} catch {
|
|
215
|
+
// Not there.
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
/** A database file and the journal files SQLite may keep beside it. */
|
|
219
|
+
const removeAll = (path: string) => [path, `${path}-journal`, `${path}-wal`, `${path}-shm`].forEach(unlinkQuiet);
|
|
220
|
+
|
|
221
|
+
const STALE_TMP_MS = 86_400_000;
|
|
222
|
+
|
|
223
|
+
/** Temporary files a crashed rebuild left behind: `index.sqlite.tmp-<pid>` untouched for a day. */
|
|
224
|
+
function sweepStaleTemporaries(home: string) {
|
|
225
|
+
for (const name of readdirSync(home)) {
|
|
226
|
+
if (!name.startsWith(`${INDEX_FILE}.tmp-`)) continue;
|
|
227
|
+
try {
|
|
228
|
+
if (Date.now() - statSync(join(home, name)).mtimeMs > STALE_TMP_MS) removeAll(join(home, name));
|
|
229
|
+
} catch {
|
|
230
|
+
// Gone already.
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface RefreshHooks {
|
|
236
|
+
/** For tests: runs after the signature was taken and before the replay reads the files. */
|
|
237
|
+
beforeReplay?: () => void;
|
|
238
|
+
/** For tests: runs between the listing of the source files and their stat. */
|
|
239
|
+
afterListing?: () => void;
|
|
240
|
+
/** Rebuild even when the index looks current. */
|
|
241
|
+
force?: boolean;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Makes `<home>/index.sqlite` current and returns its path; `rebuilt` says whether the file was
|
|
246
|
+
* written. Reading the run files has the side effects `history` has: a damaged one is moved aside
|
|
247
|
+
* with a warning. The signature of the sources is taken before the replay and stored with it, so a
|
|
248
|
+
* file written while the index is being built makes the next call rebuild. The old index is replaced
|
|
249
|
+
* by the rename and not before: a rebuild that fails leaves it where it was. A missing-file error
|
|
250
|
+
* (ENOENT) that escapes the scan or the replay, for example a history directory forgotten while it
|
|
251
|
+
* is listed, makes the whole refresh run once more. A run or loop file removed between the listing
|
|
252
|
+
* and the parse does not reach that retry: a loop file counts as absent, a run file is skipped with a
|
|
253
|
+
* warning, and the next call rebuilds because the sources no longer match the stored signature. A loop file that cannot be read for another reason
|
|
254
|
+
* (permissions, I/O) makes the refresh throw. A loop file whose content is not a loop state is skipped
|
|
255
|
+
* with a warning, and the index is stored with a signature that never matches, so every later call
|
|
256
|
+
* rebuilds and warns again until the file is fixed or removed. A run file that readRuns leaves in
|
|
257
|
+
* place (another format version, kept so a downgrade loses no history, or one that could not be
|
|
258
|
+
* moved aside) is warned about only by the call that rebuilt the index: it does not force a rebuild
|
|
259
|
+
* on every later call.
|
|
260
|
+
*/
|
|
261
|
+
export function refreshIndex(wachimanHome: string, warn: (line: string) => void, hooks: RefreshHooks = {}): { path: string; rebuilt: boolean } {
|
|
262
|
+
const root = historyRoot(wachimanHome);
|
|
263
|
+
const path = indexPath(wachimanHome);
|
|
264
|
+
return retryOnce(() => {
|
|
265
|
+
const signature = sourceSignature(root, hooks.afterListing);
|
|
266
|
+
if (!hooks.force && existsSync(path) && upToDate(path, signature)) return { path, rebuilt: false };
|
|
267
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
268
|
+
sweepStaleTemporaries(dirname(path));
|
|
269
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
270
|
+
removeAll(tmp);
|
|
271
|
+
// Created 0600 before SQLite writes a byte; an empty file is a valid new database.
|
|
272
|
+
closeSync(openSync(tmp, "w", 0o600));
|
|
273
|
+
chmodSync(tmp, 0o600);
|
|
274
|
+
const db = new Database(tmp);
|
|
275
|
+
try {
|
|
276
|
+
hooks.beforeReplay?.();
|
|
277
|
+
db.run(SCHEMA);
|
|
278
|
+
const skipped = db.transaction(() => fill(db, root, warn))();
|
|
279
|
+
// An index that left a loop file out must not look current: a stored value that no computed
|
|
280
|
+
// signature equals makes the next call rebuild, and warn again, until the file is fixed.
|
|
281
|
+
db.prepare("INSERT INTO meta (key, value) VALUES ('signature', ?)").run(skipped > 0 ? `incomplete:${signature}` : signature);
|
|
282
|
+
db.run(`PRAGMA user_version = ${INDEX_SCHEMA_VERSION}`);
|
|
283
|
+
db.close();
|
|
284
|
+
renameSync(tmp, path);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
try {
|
|
287
|
+
db.close();
|
|
288
|
+
} catch {
|
|
289
|
+
// Already closed.
|
|
290
|
+
}
|
|
291
|
+
removeAll(tmp);
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
// Journal files of the index that was just replaced belong to it, not to the new one.
|
|
295
|
+
for (const stale of [`${path}-journal`, `${path}-wal`, `${path}-shm`]) unlinkQuiet(stale);
|
|
296
|
+
return { path, rebuilt: true };
|
|
297
|
+
});
|
|
298
|
+
}
|