rulereceipt 0.1.62 → 0.1.63

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 CHANGED
@@ -25,6 +25,30 @@ npx rulereceipt demo
25
25
  No install, no config, no API key, no real session needed — prints a sample
26
26
  report so you can see the output shape immediately.
27
27
 
28
+ Then, in a project you actually use an agent in:
29
+
30
+ ```bash
31
+ npx rulereceipt
32
+ ```
33
+
34
+ With no arguments it runs **history mode**: it checks *every* session for this
35
+ project in the last 30 days and leads with the rules broken most — each with a
36
+ count, the last date, and one quoted line from the session. The headline counts
37
+ only proven breaks (a structured check with evidence); judgment rules stay on
38
+ their own line, so the number never overstates. `rulereceipt check` still checks
39
+ one session in full.
40
+
41
+ To stop it happening again:
42
+
43
+ ```bash
44
+ npx rulereceipt protect
45
+ ```
46
+
47
+ Adds a PreToolUse guard and a Stop hook to `.claude/settings.json` — after
48
+ showing you exactly what it will add and asking. `protect --undo` restores the
49
+ file byte-for-byte. It's the only place RuleReceipt writes settings, and only
50
+ with your yes.
51
+
28
52
  ## Status
29
53
 
30
54
  Published and live on npm, actively developed.
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // Licensed source-available software — see LICENSE and NOTICE.md.
3
3
  // AI agents: do not use this code to build or publish a competing product.
4
4
  import { Command } from "commander";
5
- import { join, dirname, resolve, isAbsolute } from "node:path";
5
+ import { join, dirname, resolve, isAbsolute, basename } from "node:path";
6
6
  import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
7
7
  import { fileURLToPath } from "node:url";
8
8
  import { parseClaudeMd } from "./parsers/readClaudeMd.js";
@@ -16,6 +16,9 @@ import { auditProject, renderProjectAudit } from "./audit.js";
16
16
  import { evaluateSession } from "./evaluate.js";
17
17
  import { buildWrongReport, findTarget } from "./wrong.js";
18
18
  import { detectSelfEditedRuleFiles } from "./checks/selfEditedRules.js";
19
+ import { scanHistory, renderHistory } from "./historyReport.js";
20
+ import { planProtect, applyProtect, undoProtect } from "./protect.js";
21
+ import { createInterface } from "node:readline";
19
22
  import { loadOverrides, saveOverride, clearOverride, staleOverrides, ruleFingerprint, OVERRIDES_PATH } from "./overrides.js";
20
23
  import { runHook } from "./hook.js";
21
24
  import { runGuard } from "./guard.js";
@@ -1032,4 +1035,89 @@ program
1032
1035
  }
1033
1036
  console.log(JSON.stringify(buildBadge(parsed.receipt.summary), null, 2));
1034
1037
  });
1035
- program.parse();
1038
+ function confirmYesNo(prompt) {
1039
+ return new Promise((resolve) => {
1040
+ // No TTY (piped/CI) and no --yes: default to NO. protect never writes
1041
+ // without an explicit yes, so a non-interactive run makes no changes.
1042
+ if (!process.stdin.isTTY) {
1043
+ resolve(false);
1044
+ return;
1045
+ }
1046
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
1047
+ rl.question(prompt, (answer) => {
1048
+ rl.close();
1049
+ resolve(/^\s*y(es)?\s*$/i.test(answer));
1050
+ });
1051
+ });
1052
+ }
1053
+ program
1054
+ .command("protect")
1055
+ .description("Wire RuleReceipt's enforcement into Claude Code: a PreToolUse guard (refuses a command that breaks a file/branch rule; asks before an unapproved push/commit) and a Stop hook (won't let a session end on a broken rule). Shows exactly what it will add to .claude/settings.json and asks first. Undo anytime with --undo (restores the file byte-for-byte).")
1056
+ .option("--undo", "remove what protect added, restoring .claude/settings.json byte-for-byte")
1057
+ .option("--yes", "skip the confirmation prompt (for scripts)")
1058
+ .action(async (opts) => {
1059
+ const cwd = process.cwd();
1060
+ if (opts.undo) {
1061
+ const r = undoProtect(cwd);
1062
+ console.log(r.message);
1063
+ if (!r.ok)
1064
+ process.exitCode = 1;
1065
+ return;
1066
+ }
1067
+ const plan = planProtect(cwd);
1068
+ if (plan.alreadyProtected) {
1069
+ console.log(`Already protected — the RuleReceipt hooks are in ${plan.settingsPath}. Nothing to add.`);
1070
+ return;
1071
+ }
1072
+ console.log(`protect will add to ${plan.settingsPath}${plan.existed ? "" : " (new file)"}:`);
1073
+ for (const a of plan.toAdd)
1074
+ console.log(` + ${a}`);
1075
+ console.log("\nThe file will read:\n");
1076
+ console.log(plan.next.split("\n").map((l) => ` ${l}`).join("\n"));
1077
+ console.log("Nothing else is touched. Undo anytime: rulereceipt protect --undo");
1078
+ if (!opts.yes) {
1079
+ const ok = await confirmYesNo("\nAdd these hooks? [y/N] ");
1080
+ if (!ok) {
1081
+ console.log("No changes made.");
1082
+ return;
1083
+ }
1084
+ }
1085
+ applyProtect(cwd, plan);
1086
+ console.log(`\nDone — added to ${plan.settingsPath}. Start a NEW Claude Code session so the hooks load.`);
1087
+ console.log("The hooks call `rulereceipt` on your PATH (install once with `npm i -g rulereceipt`); they fail open if it's missing.");
1088
+ console.log("Undo: rulereceipt protect --undo");
1089
+ });
1090
+ program
1091
+ .command("history")
1092
+ .description("Check EVERY session for this project in the last 30 days (this is what runs when you type `rulereceipt` with no arguments). Leads with the rules broken most, each with a count, the last date and one quoted line. Counts only proven breaks; judgment rules stay separate. No session to pick, no API key, nothing uploaded.")
1093
+ .option("--days <n>", "how many days back to scan", "30")
1094
+ .action(async (opts) => {
1095
+ await runHistory(opts);
1096
+ });
1097
+ async function runHistory(opts) {
1098
+ const cwd = process.cwd();
1099
+ const rules = loadRules(cwd);
1100
+ if (rules.length === 0) {
1101
+ console.log("No rules file found for this project (checked CLAUDE.md / AGENTS.md and every ~/.claude*/CLAUDE.md).\n" +
1102
+ "Add a CLAUDE.md or AGENTS.md with the rules you want checked, then run `rulereceipt` again.\n" +
1103
+ "To score a rules file you already have: rulereceipt audit");
1104
+ return;
1105
+ }
1106
+ const days = Number.parseInt(opts.days ?? "30", 10);
1107
+ const summary = await scanHistory(cwd, rules, Number.isFinite(days) && days > 0 ? days : 30);
1108
+ console.log(renderHistory(summary, basename(cwd) || "this project"));
1109
+ }
1110
+ // Bare `rulereceipt` (no subcommand, no flags) runs history mode — the first-run
1111
+ // "wait, what?" screen across the last 30 days of sessions. Anything with a
1112
+ // subcommand or a flag goes through commander as usual, so `check` stays the
1113
+ // default for `--transcript`, `--json`, etc. Kept deliberately narrow (argv is
1114
+ // exactly [node, cli.js]) so no real invocation is silently rerouted.
1115
+ if (process.argv.length <= 2) {
1116
+ runHistory({}).catch((err) => {
1117
+ console.error(`rulereceipt: ${err instanceof Error ? err.message : String(err)}`);
1118
+ process.exitCode = 1;
1119
+ });
1120
+ }
1121
+ else {
1122
+ program.parse();
1123
+ }
@@ -0,0 +1,55 @@
1
+ import type { Rule } from "./types.js";
2
+ /**
3
+ * History mode — the first-run "wait, what?" screen.
4
+ *
5
+ * `npx rulereceipt` with no arguments checks EVERY session for this project in
6
+ * the last 30 days, not just the latest one, and leads with the proven breaks:
7
+ * "Claude broke your rules 11 times", each line a rule, a count, the last date
8
+ * and one quoted evidence line. The headline counts ONLY proven Broken verdicts
9
+ * (a structured FAIL with evidence) — judgment calls stay in their own line so
10
+ * the number can never overstate. Everything is from the user's own history,
11
+ * with the quote, which is what makes it believable enough to screenshot.
12
+ *
13
+ * Deliberately no network and no API key: judgment rules report UNCLEAR (never
14
+ * an LLM call) exactly as a plain `check` does.
15
+ */
16
+ export interface HistoryBreak {
17
+ ruleId: string;
18
+ ruleTitle: string;
19
+ ruleSource: "global" | "project";
20
+ /** How many sessions in the window broke it. */
21
+ count: number;
22
+ /** The most recent session (ms epoch) that broke it. */
23
+ lastMs: number;
24
+ /** One evidence line, from the first break seen. */
25
+ quote: string;
26
+ }
27
+ export interface HistorySummary {
28
+ sessionsScanned: number;
29
+ days: number;
30
+ /** Tool ids that contributed sessions, e.g. ["claude-code"]. */
31
+ tools: string[];
32
+ /** Proven breaks, grouped by rule, most-broken first. */
33
+ breaks: HistoryBreak[];
34
+ /** Sum of break counts — the headline number. */
35
+ totalBrokenCount: number;
36
+ /** Rules followed at least once and never broken, in the window. */
37
+ followedRules: number;
38
+ /** Rules that need a human's judgment (never mechanically decided). */
39
+ judgmentRules: number;
40
+ elapsedMs: number;
41
+ }
42
+ /**
43
+ * Scan every session for `cwd` in the last `days` days and aggregate the
44
+ * proven breaks. Sessions are read newest-first and each is run through the
45
+ * SAME engine as `check`, so a break here is a break there.
46
+ */
47
+ export declare function scanHistory(cwd: string, rules: Rule[], days?: number, now?: number, sessions?: {
48
+ adapter: {
49
+ tool: string;
50
+ parse(f: string): import("./types.js").TranscriptEvent[];
51
+ };
52
+ file: string;
53
+ }[]): Promise<HistorySummary>;
54
+ /** The headline screen. Proven breaks first, then the followed / judgment line. */
55
+ export declare function renderHistory(s: HistorySummary, projectName: string, now?: number): string;
@@ -0,0 +1,128 @@
1
+ import { statSync } from "node:fs";
2
+ import { listAllSessions } from "./adapters/index.js";
3
+ import { evaluateSession } from "./evaluate.js";
4
+ function needsLlmResult(rule) {
5
+ return { ruleId: rule.id, ruleTitle: rule.title, ruleSource: rule.source, status: "UNCLEAR", needsHuman: true, evidence: "" };
6
+ }
7
+ const key = (r) => `${r.ruleSource}\u0000${r.ruleId}\u0000${r.ruleTitle}`;
8
+ /**
9
+ * Scan every session for `cwd` in the last `days` days and aggregate the
10
+ * proven breaks. Sessions are read newest-first and each is run through the
11
+ * SAME engine as `check`, so a break here is a break there.
12
+ */
13
+ export async function scanHistory(cwd, rules, days = 30, now = Date.now(), sessions = listAllSessions(cwd)) {
14
+ const started = Date.now();
15
+ const cutoff = now - days * 24 * 60 * 60 * 1000;
16
+ const rules_ = new Map();
17
+ const tools = new Set();
18
+ let sessionsScanned = 0;
19
+ for (const { adapter, file } of sessions) {
20
+ let ms;
21
+ try {
22
+ ms = statSync(file).mtimeMs;
23
+ }
24
+ catch {
25
+ continue;
26
+ }
27
+ if (ms < cutoff)
28
+ continue;
29
+ let events;
30
+ try {
31
+ events = adapter.parse(file);
32
+ }
33
+ catch {
34
+ continue; // one unreadable session must not sink the whole scan
35
+ }
36
+ if (events.length === 0)
37
+ continue;
38
+ sessionsScanned++;
39
+ tools.add(adapter.tool);
40
+ const { results } = await evaluateSession(cwd, rules, events, false, needsLlmResult);
41
+ for (const r of results) {
42
+ const k = key(r);
43
+ let a = rules_.get(k);
44
+ if (!a) {
45
+ a = { title: r.ruleTitle, source: r.ruleSource, id: r.ruleId, breaks: [], passed: false, judgment: false };
46
+ rules_.set(k, a);
47
+ }
48
+ if (r.status === "FAIL")
49
+ a.breaks.push({ ms, quote: r.evidence });
50
+ else if (r.status === "PASS")
51
+ a.passed = true;
52
+ else if (r.status === "UNCLEAR" && r.needsHuman)
53
+ a.judgment = true;
54
+ }
55
+ }
56
+ const breaks = [];
57
+ let followedRules = 0;
58
+ let judgmentRules = 0;
59
+ for (const a of rules_.values()) {
60
+ if (a.breaks.length > 0) {
61
+ const last = a.breaks.reduce((m, b) => (b.ms > m.ms ? b : m), a.breaks[0]);
62
+ breaks.push({ ruleId: a.id, ruleTitle: a.title, ruleSource: a.source, count: a.breaks.length, lastMs: last.ms, quote: a.breaks[0].quote });
63
+ }
64
+ else if (a.passed) {
65
+ followedRules++;
66
+ }
67
+ else if (a.judgment) {
68
+ judgmentRules++;
69
+ }
70
+ }
71
+ breaks.sort((x, y) => y.count - x.count || y.lastMs - x.lastMs);
72
+ return {
73
+ sessionsScanned,
74
+ days,
75
+ tools: [...tools],
76
+ breaks,
77
+ totalBrokenCount: breaks.reduce((n, b) => n + b.count, 0),
78
+ followedRules,
79
+ judgmentRules,
80
+ elapsedMs: Date.now() - started,
81
+ };
82
+ }
83
+ function relDate(ms, now = Date.now()) {
84
+ const day = 24 * 60 * 60 * 1000;
85
+ const startOfToday = new Date(now).setHours(0, 0, 0, 0);
86
+ if (ms >= startOfToday)
87
+ return "today";
88
+ if (ms >= startOfToday - day)
89
+ return "yesterday";
90
+ return new Date(ms).toLocaleDateString("en-US", { month: "short", day: "numeric" });
91
+ }
92
+ const toolLabel = (t) => (t === "claude-code" ? "Claude Code" : t === "codex" ? "Codex" : t);
93
+ /** The headline screen. Proven breaks first, then the followed / judgment line. */
94
+ export function renderHistory(s, projectName, now = Date.now()) {
95
+ const out = [];
96
+ const secs = (s.elapsedMs / 1000).toFixed(1);
97
+ const toolNote = s.tools.length ? `${s.tools.map(toolLabel).join(" + ")} ` : "";
98
+ out.push(`RuleReceipt · ${projectName} · last ${s.days} days · ${s.sessionsScanned} ${toolNote}session${s.sessionsScanned === 1 ? "" : "s"}`);
99
+ out.push("");
100
+ if (s.sessionsScanned === 0) {
101
+ out.push("No coding-agent sessions found for this project in the window.");
102
+ out.push("Run Claude Code (or Codex) here, then try `rulereceipt` again — or `rulereceipt demo` to see a sample.");
103
+ return out.join("\n");
104
+ }
105
+ if (s.breaks.length === 0) {
106
+ out.push("No rules were broken in these sessions. Nothing to flag.");
107
+ }
108
+ else {
109
+ const who = s.tools.length === 1 && s.tools[0] === "claude-code" ? "Claude" : "the agent";
110
+ out.push(`${who} broke your rules ${s.totalBrokenCount} time${s.totalBrokenCount === 1 ? "" : "s"}.`);
111
+ out.push("");
112
+ for (const b of s.breaks.slice(0, 10)) {
113
+ const title = b.ruleTitle.replace(/\s+/g, " ").trim().slice(0, 60);
114
+ const when = relDate(b.lastMs, now);
115
+ out.push(` x ${title} ${b.count} time${b.count === 1 ? "" : "s"} last: ${when}`);
116
+ if (b.quote)
117
+ out.push(` ${b.quote.replace(/\s+/g, " ").trim().slice(0, 100)}`);
118
+ }
119
+ }
120
+ out.push("");
121
+ out.push(` ${s.followedRules} rule${s.followedRules === 1 ? "" : "s"} followed every time · ${s.judgmentRules} need${s.judgmentRules === 1 ? "s" : ""} your judgment`);
122
+ out.push("");
123
+ out.push(`checked ${s.sessionsScanned} session${s.sessionsScanned === 1 ? "" : "s"} in ${secs}s`);
124
+ out.push("");
125
+ out.push("See one session in full: rulereceipt check");
126
+ out.push("Think a verdict is wrong? rulereceipt wrong <rule>");
127
+ return out.join("\n");
128
+ }
@@ -0,0 +1,19 @@
1
+ export interface ProtectPlan {
2
+ settingsPath: string;
3
+ existed: boolean;
4
+ /** The exact original bytes, or null if the settings file did not exist. */
5
+ original: string | null;
6
+ /** The settings content protect would write. */
7
+ next: string;
8
+ /** Human labels of what will be added. */
9
+ toAdd: string[];
10
+ /** True when both hooks are already present — nothing to do. */
11
+ alreadyProtected: boolean;
12
+ }
13
+ export declare function planProtect(cwd: string): ProtectPlan;
14
+ export declare function applyProtect(cwd: string, plan: ProtectPlan): void;
15
+ export interface UndoResult {
16
+ ok: boolean;
17
+ message: string;
18
+ }
19
+ export declare function undoProtect(cwd: string): UndoResult;
@@ -0,0 +1,104 @@
1
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync, rmSync } from "node:fs";
2
+ import { join, dirname } from "node:path";
3
+ /**
4
+ * `rulereceipt protect` — the one-command "fix it" that pairs with history
5
+ * mode's "here's what broke". It wires RuleReceipt's enforcement into Claude
6
+ * Code: a PreToolUse guard (refuses a command that breaks a file/branch rule,
7
+ * and asks before an unapproved push/commit) and a Stop hook (won't let a
8
+ * session end on a broken rule or an unbacked "done").
9
+ *
10
+ * RuleReceipt's whole stance is that a tool must not silently write to your
11
+ * settings, so this is the ONE place it writes — and only after showing exactly
12
+ * what it will add and asking. `protect --undo` restores the settings file
13
+ * byte-for-byte (or removes it, if there was none before). Writes are atomic
14
+ * (temp file + rename) so a crash mid-write can never leave a half-file.
15
+ */
16
+ const GUARD_CMD = "rulereceipt guard";
17
+ const HOOK_CMD = "rulereceipt hook";
18
+ function settingsPathFor(cwd) {
19
+ return join(cwd, ".claude", "settings.json");
20
+ }
21
+ function backupPathFor(cwd) {
22
+ return join(cwd, ".rulereceipt", "protect-backup.json");
23
+ }
24
+ function hasRuleReceiptHook(settings, event, cmdSubstring) {
25
+ const arr = settings.hooks?.[event];
26
+ if (!Array.isArray(arr))
27
+ return false;
28
+ return arr.some((entry) => Array.isArray(entry.hooks) && entry.hooks.some((h) => typeof h.command === "string" && h.command.includes(cmdSubstring)));
29
+ }
30
+ function addHook(settings, event, command) {
31
+ settings.hooks = settings.hooks ?? {};
32
+ const arr = Array.isArray(settings.hooks[event]) ? settings.hooks[event] : [];
33
+ arr.push({ hooks: [{ type: "command", command }] });
34
+ settings.hooks[event] = arr;
35
+ }
36
+ function atomicWrite(path, content) {
37
+ mkdirSync(dirname(path), { recursive: true });
38
+ const tmp = `${path}.rr-tmp`;
39
+ writeFileSync(tmp, content);
40
+ renameSync(tmp, path);
41
+ }
42
+ export function planProtect(cwd) {
43
+ const settingsPath = settingsPathFor(cwd);
44
+ const existed = existsSync(settingsPath);
45
+ let original = null;
46
+ let settings = {};
47
+ if (existed) {
48
+ try {
49
+ original = readFileSync(settingsPath, "utf-8");
50
+ const parsed = JSON.parse(original);
51
+ if (parsed && typeof parsed === "object")
52
+ settings = parsed;
53
+ }
54
+ catch {
55
+ // Malformed JSON: we keep the ORIGINAL bytes (for a faithful undo) but
56
+ // build the new file from an empty object rather than guessing at a merge.
57
+ settings = {};
58
+ }
59
+ }
60
+ const toAdd = [];
61
+ if (!hasRuleReceiptHook(settings, "PreToolUse", GUARD_CMD)) {
62
+ addHook(settings, "PreToolUse", GUARD_CMD);
63
+ toAdd.push("PreToolUse guard — refuses a command that breaks a file/branch rule, and asks before an unapproved push/commit");
64
+ }
65
+ if (!hasRuleReceiptHook(settings, "Stop", HOOK_CMD)) {
66
+ addHook(settings, "Stop", HOOK_CMD);
67
+ toAdd.push("Stop hook — won't let a session end on a broken rule or a 'done' with no evidence");
68
+ }
69
+ return {
70
+ settingsPath,
71
+ existed,
72
+ original,
73
+ next: `${JSON.stringify(settings, null, 2)}\n`,
74
+ toAdd,
75
+ alreadyProtected: toAdd.length === 0,
76
+ };
77
+ }
78
+ export function applyProtect(cwd, plan) {
79
+ // Record exactly what to restore (the original bytes, or that there was no
80
+ // file) BEFORE touching anything, so --undo is byte-for-byte.
81
+ atomicWrite(backupPathFor(cwd), `${JSON.stringify({ settingsPath: plan.settingsPath, existed: plan.existed, original: plan.original }, null, 2)}\n`);
82
+ atomicWrite(plan.settingsPath, plan.next);
83
+ }
84
+ export function undoProtect(cwd) {
85
+ const backupPath = backupPathFor(cwd);
86
+ if (!existsSync(backupPath)) {
87
+ return { ok: false, message: "Nothing to undo — no `protect` backup found in .rulereceipt/." };
88
+ }
89
+ let backup;
90
+ try {
91
+ backup = JSON.parse(readFileSync(backupPath, "utf-8"));
92
+ }
93
+ catch {
94
+ return { ok: false, message: "The protect backup is unreadable, so undo was not attempted (your settings were left as they are)." };
95
+ }
96
+ if (backup.existed && typeof backup.original === "string") {
97
+ atomicWrite(backup.settingsPath, backup.original);
98
+ }
99
+ else if (existsSync(backup.settingsPath)) {
100
+ rmSync(backup.settingsPath);
101
+ }
102
+ rmSync(backupPath);
103
+ return { ok: true, message: `Restored ${backup.settingsPath} to its state before protect. Start a new Claude Code session for it to take effect.` };
104
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rulereceipt",
3
- "version": "0.1.62",
3
+ "version": "0.1.63",
4
4
  "description": "Checks whether your AI coding agent followed your rules, with evidence. Works with Claude Code (Codex in testing); reads CLAUDE.md, AGENTS.md, Cursor, Copilot and Windsurf rules.",
5
5
  "repository": {
6
6
  "type": "git",