@davesheffer/hunch 0.11.3 → 0.12.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/dist/cli/index.js +63 -70
- package/dist/core/checkreport.js +113 -0
- package/dist/extractors/git.js +12 -0
- package/dist/integrations/ciAction.js +81 -0
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -24,12 +24,14 @@ import { indexRepo } from "../extractors/indexer.js";
|
|
|
24
24
|
import { syncCommit, recordFailure, captureTestRun } from "../synthesis/synthesize.js";
|
|
25
25
|
import { parseTestReport } from "../extractors/testreport.js";
|
|
26
26
|
import { selectProvider } from "../synthesis/provider.js";
|
|
27
|
-
import { isGitRepo, headSha, logSince, lastChangeDate, stagedFiles, commitFiles, asOfDate, stagedDiff, commitDiff } from "../extractors/git.js";
|
|
27
|
+
import { isGitRepo, headSha, logSince, lastChangeDate, stagedFiles, commitFiles, asOfDate, stagedDiff, commitDiff, rangeFiles, rangeDiff } from "../extractors/git.js";
|
|
28
28
|
import { analyzeDiff } from "../extractors/diff.js";
|
|
29
29
|
import { isStrictBlocker } from "../core/strictgate.js";
|
|
30
|
+
import { renderText, renderMarkdown, reportFailsStrict } from "../core/checkreport.js";
|
|
30
31
|
import { installPostCommitHook, installPreCommitHook } from "../integrations/hooks.js";
|
|
31
32
|
import { installMergeDriver } from "../integrations/mergeDriver.js";
|
|
32
33
|
import { ensureGitignore } from "../integrations/gitignore.js";
|
|
34
|
+
import { writeCiWorkflow } from "../integrations/ciAction.js";
|
|
33
35
|
import { updateClaudeMd } from "../integrations/claudemd.js";
|
|
34
36
|
import { writeMcpJson, writeSlashCommands, installClaudeHooks } from "../integrations/scaffold.js";
|
|
35
37
|
import { scaffoldProviders } from "../integrations/providers.js";
|
|
@@ -534,54 +536,76 @@ program
|
|
|
534
536
|
// ---- check (constraint enforcement) ---------------------------------------
|
|
535
537
|
program
|
|
536
538
|
.command("check")
|
|
537
|
-
.description("Flag changes that touch a do-not-break invariant
|
|
539
|
+
.description("Flag changes that touch a do-not-break invariant — the local guardrail AND the CI/PR Constraint Guard.")
|
|
538
540
|
.option("--staged", "check git staged files (default)")
|
|
539
541
|
.option("--commit <sha>", "check a specific commit's files")
|
|
542
|
+
.option("--base <ref>", "check a PR/branch: files changed vs <ref> (e.g. origin/main) — for CI")
|
|
540
543
|
.option("--strict", "exit non-zero ONLY on a direct, high-confidence, non-stale blocking invariant (near/stale/low-confidence stay advisory)")
|
|
544
|
+
.option("--format <fmt>", "output: text (default) | markdown (a PR comment)", "text")
|
|
541
545
|
.option("--blast", "also print the dependency blast radius of the changed files")
|
|
542
546
|
.action((opts) => {
|
|
543
|
-
|
|
544
|
-
|
|
547
|
+
const sources = [opts.commit && "--commit", opts.base && "--base", opts.staged && "--staged"].filter(Boolean);
|
|
548
|
+
if (sources.length > 1)
|
|
549
|
+
return fail(`pick one of --staged / --commit / --base (got ${sources.join(", ")})`);
|
|
550
|
+
const markdown = opts.format === "markdown";
|
|
551
|
+
const emptyReport = { fileCount: 0, strict: !!opts.strict, direct: [], near: [], regressions: [], strictBlockers: 0, regBlocking: 0 };
|
|
545
552
|
const { store, root } = storeFor();
|
|
546
553
|
store.reindex(); // blast radius walks the edge graph — make the index current
|
|
547
|
-
const files = opts.commit ? commitFiles(opts.commit, root)
|
|
554
|
+
const files = opts.commit ? commitFiles(opts.commit, root)
|
|
555
|
+
: opts.base ? rangeFiles(opts.base, root)
|
|
556
|
+
: stagedFiles(root);
|
|
548
557
|
if (!files.length) {
|
|
549
|
-
console.log("No changed files to check.");
|
|
558
|
+
console.log(markdown ? renderMarkdown(emptyReport) : "No changed files to check.");
|
|
550
559
|
store.close();
|
|
551
560
|
return;
|
|
552
561
|
}
|
|
553
|
-
const mark = (s) => (s === "blocking" ? "⛔" : s === "warning" ? "⚠" : "·");
|
|
554
562
|
// 1) DIRECT — a changed file matches a constraint's scope.
|
|
555
563
|
const direct = new Map();
|
|
556
|
-
for (const f of files)
|
|
564
|
+
for (const f of files)
|
|
557
565
|
for (const c of store.checkConstraints(f)) {
|
|
558
566
|
const e = direct.get(c.id) ?? { c, files: [] };
|
|
559
567
|
e.files.push(f);
|
|
560
568
|
direct.set(c.id, e);
|
|
561
569
|
}
|
|
562
|
-
|
|
563
|
-
// 2) NEAR — a changed file's blast radius reaches a file an invariant guards:
|
|
564
|
-
// you didn't touch the invariant, but you touched something it depends on.
|
|
570
|
+
// 2) NEAR — reached only through the blast radius (a guarded dependency changed).
|
|
565
571
|
const near = new Map();
|
|
566
|
-
for (const f of files)
|
|
567
|
-
for (const b of store.blastRadiusFiles(f))
|
|
572
|
+
for (const f of files)
|
|
573
|
+
for (const b of store.blastRadiusFiles(f))
|
|
568
574
|
for (const c of store.checkConstraints(b.file)) {
|
|
569
575
|
if (direct.has(c.id))
|
|
570
|
-
continue; // already
|
|
576
|
+
continue; // already a direct hit
|
|
571
577
|
const e = near.get(c.id) ?? { c, via: [] };
|
|
572
578
|
e.via.push(`${f} → ${b.file} (${b.via}, depth ${b.depth})`);
|
|
573
579
|
near.set(c.id, e);
|
|
574
580
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
// 3) REGRESSION — does the diff RE-ADD something an in-force decision removed?
|
|
578
|
-
// (e.g. re-introducing a symbol/dep that was deliberately deleted). Warn
|
|
579
|
-
// always; only a blocking-linked resurrection fails the commit under strict.
|
|
580
|
-
const diff = opts.commit ? commitDiff(opts.commit, root) : stagedDiff(root);
|
|
581
|
+
// 3) REGRESSION — does the diff RE-ADD something an in-force decision retired?
|
|
582
|
+
const diff = opts.commit ? commitDiff(opts.commit, root) : opts.base ? rangeDiff(opts.base, root) : stagedDiff(root);
|
|
581
583
|
const an = analyzeDiff(diff);
|
|
582
584
|
const regHits = store.regressionHits({ symbols: an.addedSymbols.map((s) => s.name), deps: an.addedDeps }, files);
|
|
583
|
-
|
|
584
|
-
|
|
585
|
+
// Hardened strict gate (strictgate.ts): only DIRECT + high-confidence + non-stale
|
|
586
|
+
// can fail. near/stale/low-confidence stay advisory — safe on a shared repo / PR.
|
|
587
|
+
const staleConstraintIds = opts.strict
|
|
588
|
+
? new Set(store.staleness((f) => lastChangeDate(f, root)).filter((s) => s.kind === "constraint").map((s) => s.id))
|
|
589
|
+
: new Set();
|
|
590
|
+
const directReport = [...direct.values()].map(({ c, files: fs }) => {
|
|
591
|
+
const stale = staleConstraintIds.has(c.id);
|
|
592
|
+
const strictBlocks = isStrictBlocker(c, stale);
|
|
593
|
+
return {
|
|
594
|
+
id: c.id, severity: c.severity ?? "advisory", statement: c.statement, rationale: c.rationale ?? "",
|
|
595
|
+
files: fs, strictBlocks,
|
|
596
|
+
downgrade: (c.severity === "blocking" && !strictBlocks ? (stale ? "stale" : "low-confidence") : undefined),
|
|
597
|
+
};
|
|
598
|
+
});
|
|
599
|
+
const report = {
|
|
600
|
+
fileCount: files.length,
|
|
601
|
+
strict: !!opts.strict,
|
|
602
|
+
direct: directReport,
|
|
603
|
+
near: [...near.values()].map(({ c, via }) => ({ id: c.id, severity: c.severity ?? "advisory", statement: c.statement, via })),
|
|
604
|
+
regressions: regHits.map((h) => ({ kind: h.kind, name: h.name, decision: h.decision, title: h.title, reason: h.reason, blocking: h.blocking })),
|
|
605
|
+
strictBlockers: directReport.filter((d) => d.strictBlocks).length,
|
|
606
|
+
regBlocking: regHits.filter((h) => h.blocking).length,
|
|
607
|
+
};
|
|
608
|
+
if (opts.blast && !markdown) {
|
|
585
609
|
console.log(`Blast radius of ${files.length} changed file(s):`);
|
|
586
610
|
for (const f of files) {
|
|
587
611
|
const b = store.blastRadiusFiles(f);
|
|
@@ -590,58 +614,27 @@ program
|
|
|
590
614
|
}
|
|
591
615
|
console.log("");
|
|
592
616
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
store.close();
|
|
596
|
-
return;
|
|
597
|
-
}
|
|
598
|
-
// --strict may FAIL a commit ONLY on a DIRECT, high-confidence, non-stale
|
|
599
|
-
// blocking invariant (see strictgate.ts) — never on a blast-radius ("near")
|
|
600
|
-
// guess or a stale/low-confidence record. Those weaker hits still print, as
|
|
601
|
-
// advisory, so strict mode is safe to enable on a shared repo.
|
|
602
|
-
const staleConstraintIds = opts.strict
|
|
603
|
-
? new Set(store.staleness((f) => lastChangeDate(f, root)).filter((s) => s.kind === "constraint").map((s) => s.id))
|
|
604
|
-
: new Set();
|
|
605
|
-
let strictBlockers = 0;
|
|
606
|
-
if (direct.size) {
|
|
607
|
-
console.log(`Directly touches ${direct.size} invariant(s):\n`);
|
|
608
|
-
for (const { c, files: fs } of direct.values()) {
|
|
609
|
-
const blocks = isStrictBlocker(c, staleConstraintIds.has(c.id));
|
|
610
|
-
if (blocks)
|
|
611
|
-
strictBlockers++;
|
|
612
|
-
const note = opts.strict && c.severity === "blocking" && !blocks
|
|
613
|
-
? staleConstraintIds.has(c.id) ? " (advisory: stale)" : " (advisory: low confidence)"
|
|
614
|
-
: "";
|
|
615
|
-
console.log(` ${mark(c.severity)} [${c.severity}] ${c.statement}${note}\n ${c.id} · in: ${fs.join(", ")}\n rationale: ${c.rationale || "—"}`);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
if (near.size) {
|
|
619
|
-
console.log(`${direct.size ? "\n" : ""}Near ${near.size} invariant(s) via blast radius (a guarded dependency changed — review; never blocks):\n`);
|
|
620
|
-
for (const { c, via } of near.values()) {
|
|
621
|
-
console.log(` ${mark(c.severity)} [${c.severity}] ${c.statement}\n ${c.id}\n ${via.slice(0, 4).join("\n ")}${via.length > 4 ? `\n …+${via.length - 4} more path(s)` : ""}`);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
if (regHits.length) {
|
|
625
|
-
console.log(`${direct.size || near.size ? "\n" : ""}Re-introduces ${regHits.length} deliberately-retired item(s):\n`);
|
|
626
|
-
for (const h of regHits) {
|
|
627
|
-
console.log(` ${h.blocking ? "⛔" : "⚠"} re-adds ${h.kind} \`${h.name}\` — ${h.decision} removed it${h.blocking ? " (blocking-linked)" : ""}\n “${h.title}”\n ${h.reason}`);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
if (opts.strict && (strictBlockers || regBlocking)) {
|
|
631
|
-
const reasons = [
|
|
632
|
-
strictBlockers ? `${strictBlockers} high-confidence blocking invariant(s) directly in scope` : "",
|
|
633
|
-
regBlocking ? `${regBlocking} blocking-linked regression(s)` : "",
|
|
634
|
-
].filter(Boolean).join(" + ");
|
|
635
|
-
console.log(`\n✗ ${reasons} — review before committing.`);
|
|
617
|
+
console.log(markdown ? renderMarkdown(report) : renderText(report));
|
|
618
|
+
if (reportFailsStrict(report))
|
|
636
619
|
process.exitCode = 1;
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
620
|
+
store.close();
|
|
621
|
+
});
|
|
622
|
+
// ---- ci (scaffold the CI Constraint Guard) --------------------------------
|
|
623
|
+
program
|
|
624
|
+
.command("ci")
|
|
625
|
+
.description("Scaffold the CI Constraint Guard: a GitHub Action that runs `hunch check` on PRs, comments the result, and fails on a blocking invariant.")
|
|
626
|
+
.action(() => {
|
|
627
|
+
const root = findRoot();
|
|
628
|
+
const r = writeCiWorkflow(root);
|
|
629
|
+
if (r.action === "created") {
|
|
630
|
+
console.log(`✓ wrote ${rel(root, r.path)}`);
|
|
631
|
+
console.log(" Runs on every PR: comments the affected invariants/decisions and fails on a direct,");
|
|
632
|
+
console.log(" high-confidence, non-stale blocking invariant. Commit it, then (optionally) make");
|
|
633
|
+
console.log(' "Hunch Guard" a required status check in branch protection to enforce on merge.');
|
|
640
634
|
}
|
|
641
635
|
else {
|
|
642
|
-
console.log(
|
|
636
|
+
console.log(`· ${rel(root, r.path)} already exists — left untouched. Delete it to regenerate.`);
|
|
643
637
|
}
|
|
644
|
-
store.close();
|
|
645
638
|
});
|
|
646
639
|
// ---- context (surgical retrieval) -----------------------------------------
|
|
647
640
|
program
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/** Rendering for `hunch check` — pure, so the terminal output and the CI/PR
|
|
2
|
+
* comment share one source of truth and stay unit-testable. The CLI builds a
|
|
3
|
+
* CheckReport from the store (direct/near/regression + the hardened strict gate),
|
|
4
|
+
* then renders it as text (terminal, unchanged) or markdown (a PR comment posted
|
|
5
|
+
* by the GitHub Action). The exit-code decision lives with the caller. */
|
|
6
|
+
export function reportIsClean(r) {
|
|
7
|
+
return r.direct.length === 0 && r.near.length === 0 && r.regressions.length === 0;
|
|
8
|
+
}
|
|
9
|
+
/** True when --strict should FAIL the commit/PR. */
|
|
10
|
+
export function reportFailsStrict(r) {
|
|
11
|
+
return r.strict && (r.strictBlockers > 0 || r.regBlocking > 0);
|
|
12
|
+
}
|
|
13
|
+
const mark = (s) => (s === "blocking" ? "⛔" : s === "warning" ? "⚠" : "·");
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Terminal text (unchanged from the inline CLI output it replaces)
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
export function renderText(r) {
|
|
18
|
+
if (reportIsClean(r)) {
|
|
19
|
+
return `✓ ${r.fileCount} changed file(s) touch no recorded invariants (directly or via blast radius) and re-introduce nothing deliberately retired.`;
|
|
20
|
+
}
|
|
21
|
+
const out = [];
|
|
22
|
+
if (r.direct.length) {
|
|
23
|
+
out.push(`Directly touches ${r.direct.length} invariant(s):\n`);
|
|
24
|
+
for (const c of r.direct) {
|
|
25
|
+
const note = r.strict && c.severity === "blocking" && !c.strictBlocks
|
|
26
|
+
? c.downgrade === "stale" ? " (advisory: stale)" : " (advisory: low confidence)"
|
|
27
|
+
: "";
|
|
28
|
+
out.push(` ${mark(c.severity)} [${c.severity}] ${c.statement}${note}\n ${c.id} · in: ${c.files.join(", ")}\n rationale: ${c.rationale || "—"}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (r.near.length) {
|
|
32
|
+
out.push(`${r.direct.length ? "\n" : ""}Near ${r.near.length} invariant(s) via blast radius (a guarded dependency changed — review; never blocks):\n`);
|
|
33
|
+
for (const c of r.near) {
|
|
34
|
+
out.push(` ${mark(c.severity)} [${c.severity}] ${c.statement}\n ${c.id}\n ${c.via.slice(0, 4).join("\n ")}${c.via.length > 4 ? `\n …+${c.via.length - 4} more path(s)` : ""}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (r.regressions.length) {
|
|
38
|
+
out.push(`${r.direct.length || r.near.length ? "\n" : ""}Re-introduces ${r.regressions.length} deliberately-retired item(s):\n`);
|
|
39
|
+
for (const h of r.regressions) {
|
|
40
|
+
out.push(` ${h.blocking ? "⛔" : "⚠"} re-adds ${h.kind} \`${h.name}\` — ${h.decision} removed it${h.blocking ? " (blocking-linked)" : ""}\n “${h.title}”\n ${h.reason}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (reportFailsStrict(r)) {
|
|
44
|
+
const reasons = [
|
|
45
|
+
r.strictBlockers ? `${r.strictBlockers} high-confidence blocking invariant(s) directly in scope` : "",
|
|
46
|
+
r.regBlocking ? `${r.regBlocking} blocking-linked regression(s)` : "",
|
|
47
|
+
].filter(Boolean).join(" + ");
|
|
48
|
+
out.push(`\n✗ ${reasons} — review before committing.`);
|
|
49
|
+
}
|
|
50
|
+
else if (r.strict) {
|
|
51
|
+
out.push(`\nReview these — none are a direct, high-confidence, non-stale blocking invariant, so the commit is NOT blocked.`);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
out.push(`\nReview that these invariants still hold. (Advisory — run with --strict to fail on direct, high-confidence, non-stale blocking invariants.)`);
|
|
55
|
+
}
|
|
56
|
+
return out.join("\n");
|
|
57
|
+
}
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// Markdown (a PR comment posted by the CI Constraint Guard)
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
export function renderMarkdown(r) {
|
|
62
|
+
const H = "## 🧠 Hunch — Engineering Memory Guard";
|
|
63
|
+
if (reportIsClean(r)) {
|
|
64
|
+
return `${H}\n\n✅ This PR touches **no recorded invariants** (directly or via blast radius) and re-introduces nothing deliberately retired across ${r.fileCount} changed file(s).`;
|
|
65
|
+
}
|
|
66
|
+
const out = [H, ""];
|
|
67
|
+
if (r.direct.length) {
|
|
68
|
+
out.push(`### ⛔ Invariants directly in scope`);
|
|
69
|
+
for (const c of r.direct) {
|
|
70
|
+
const note = r.strict && c.severity === "blocking" && !c.strictBlocks
|
|
71
|
+
? c.downgrade === "stale" ? " _(advisory: record is stale)_" : " _(advisory: low confidence)_"
|
|
72
|
+
: "";
|
|
73
|
+
out.push(`- **[${c.severity}] ${c.statement}** — \`${c.id}\`${note}`);
|
|
74
|
+
out.push(` - in: ${c.files.map((f) => `\`${f}\``).join(", ")}`);
|
|
75
|
+
if (c.rationale)
|
|
76
|
+
out.push(` - _${c.rationale}_`);
|
|
77
|
+
}
|
|
78
|
+
out.push("");
|
|
79
|
+
}
|
|
80
|
+
if (r.near.length) {
|
|
81
|
+
out.push(`### ⚠ Near-invariants (reached via blast radius — review, never blocks)`);
|
|
82
|
+
for (const c of r.near) {
|
|
83
|
+
out.push(`- **[${c.severity}] ${c.statement}** — \`${c.id}\``);
|
|
84
|
+
out.push(` - ${c.via.slice(0, 3).join("\n - ")}${c.via.length > 3 ? `\n - …+${c.via.length - 3} more path(s)` : ""}`);
|
|
85
|
+
}
|
|
86
|
+
out.push("");
|
|
87
|
+
}
|
|
88
|
+
if (r.regressions.length) {
|
|
89
|
+
out.push(`### ♻️ Re-introduces deliberately-retired code`);
|
|
90
|
+
for (const h of r.regressions) {
|
|
91
|
+
out.push(`- ${h.blocking ? "⛔" : "⚠"} re-adds ${h.kind} \`${h.name}\` — \`${h.decision}\` removed it${h.blocking ? " **(blocking-linked)**" : ""}`);
|
|
92
|
+
out.push(` - _${h.title}_`);
|
|
93
|
+
}
|
|
94
|
+
out.push("");
|
|
95
|
+
}
|
|
96
|
+
out.push("---");
|
|
97
|
+
if (reportFailsStrict(r)) {
|
|
98
|
+
const reasons = [
|
|
99
|
+
r.strictBlockers ? `${r.strictBlockers} high-confidence blocking invariant(s) directly in scope` : "",
|
|
100
|
+
r.regBlocking ? `${r.regBlocking} blocking-linked regression(s)` : "",
|
|
101
|
+
].filter(Boolean).join(" + ");
|
|
102
|
+
out.push(`❌ **This PR breaks ${reasons}.** Resolve or supersede the decision before merge.`);
|
|
103
|
+
}
|
|
104
|
+
else if (r.strict) {
|
|
105
|
+
out.push(`ℹ️ Nothing here is a direct, high-confidence, non-stale blocking invariant — **not blocking** this PR.`);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
out.push(`ℹ️ Advisory — review that these invariants still hold.`);
|
|
109
|
+
}
|
|
110
|
+
out.push(`\n<sub>🧠 Hunch · engineering memory · run \`hunch why <file>\` for the full reasoning.</sub>`);
|
|
111
|
+
return out.join("\n");
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=checkreport.js.map
|
package/dist/extractors/git.js
CHANGED
|
@@ -153,6 +153,18 @@ export function stagedFiles(cwd) {
|
|
|
153
153
|
const out = gitSafe(["diff", "--cached", "--name-only", "--diff-filter=ACMR"], cwd);
|
|
154
154
|
return out ? out.split("\n").filter(Boolean) : [];
|
|
155
155
|
}
|
|
156
|
+
/** Files a PR/branch changes vs `base` (3-dot: changes on HEAD since the merge-base,
|
|
157
|
+
* i.e. exactly the PR's own commits — the CI Constraint Guard's surface). */
|
|
158
|
+
export function rangeFiles(base, cwd, head = "HEAD") {
|
|
159
|
+
const out = gitSafe(["diff", "--name-only", "--diff-filter=ACMR", `${base}...${head}`], cwd);
|
|
160
|
+
return out ? out.split("\n").filter(Boolean) : [];
|
|
161
|
+
}
|
|
162
|
+
/** The PR's unified diff vs `base` (3-dot), for the Regression Guard's structural
|
|
163
|
+
* analysis. Same noise-exclusion + truncation budget as commit/staged diffs. */
|
|
164
|
+
export function rangeDiff(base, cwd, head = "HEAD", maxBytes = 60_000) {
|
|
165
|
+
const out = gitSafe(["diff", "--no-color", "--unified=2", `${base}...${head}`, "--", ...DIFF_NOISE], cwd);
|
|
166
|
+
return out.length > maxBytes ? out.slice(0, maxBytes) + "\n…(diff truncated)…" : out;
|
|
167
|
+
}
|
|
156
168
|
/** Unified diff of the staged changes (for the Regression Guard's structural
|
|
157
169
|
* analysis). Excludes machine-generated noise and truncates at the SAME budget as
|
|
158
170
|
* commitDiff, so the staged and `--commit` guard paths can't diverge on big diffs. */
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** The CI Constraint Guard — Hunch's enforcement edge. A GitHub Actions workflow
|
|
2
|
+
* that runs `hunch check` over a pull request's diff, posts the result as a
|
|
3
|
+
* sticky PR comment (citing the con_/dec_ ids), and FAILS the check on a direct,
|
|
4
|
+
* high-confidence, non-stale blocking invariant (the hardened strict gate). This
|
|
5
|
+
* turns the engineering memory from advisory into a merge gate — and it's
|
|
6
|
+
* structurally uncopyable: it reasons over the diff PLUS the constraints
|
|
7
|
+
* committed in the same git history. Idempotent: never clobbers a user's edits.
|
|
8
|
+
*/
|
|
9
|
+
import { mkdirSync, writeFileSync, existsSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
// `\${{ … }}` keeps GitHub Actions expressions literal inside this template
|
|
12
|
+
// literal (a bare `${` would be JS interpolation).
|
|
13
|
+
export function ciWorkflowYaml() {
|
|
14
|
+
return `# Hunch — CI Constraint Guard. Blocks a PR that breaks a recorded invariant,
|
|
15
|
+
# re-adds deliberately-retired code, or contradicts an in-force decision, and
|
|
16
|
+
# comments with the why (con_/dec_ ids). Make this a required check to enforce.
|
|
17
|
+
name: Hunch Guard
|
|
18
|
+
|
|
19
|
+
on:
|
|
20
|
+
pull_request:
|
|
21
|
+
|
|
22
|
+
permissions:
|
|
23
|
+
contents: read
|
|
24
|
+
pull-requests: write
|
|
25
|
+
|
|
26
|
+
jobs:
|
|
27
|
+
hunch-guard:
|
|
28
|
+
runs-on: ubuntu-latest
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v4
|
|
31
|
+
with:
|
|
32
|
+
fetch-depth: 0 # full history: the guard diffs base...head and reads git log
|
|
33
|
+
|
|
34
|
+
- uses: actions/setup-node@v4
|
|
35
|
+
with:
|
|
36
|
+
node-version: 20
|
|
37
|
+
|
|
38
|
+
- name: Install Hunch
|
|
39
|
+
run: npm install -g @davesheffer/hunch
|
|
40
|
+
|
|
41
|
+
- name: Run Constraint Guard
|
|
42
|
+
id: guard
|
|
43
|
+
run: |
|
|
44
|
+
set +e
|
|
45
|
+
hunch check --base "origin/\${{ github.base_ref }}" --strict --format markdown > hunch-report.md
|
|
46
|
+
echo "exit=$?" >> "$GITHUB_OUTPUT"
|
|
47
|
+
set -e
|
|
48
|
+
|
|
49
|
+
- name: Comment on PR
|
|
50
|
+
if: always()
|
|
51
|
+
uses: actions/github-script@v7
|
|
52
|
+
with:
|
|
53
|
+
script: |
|
|
54
|
+
const fs = require('fs');
|
|
55
|
+
const body = fs.readFileSync('hunch-report.md', 'utf8').trim();
|
|
56
|
+
const marker = '<!-- hunch-guard -->';
|
|
57
|
+
const { owner, repo } = context.repo;
|
|
58
|
+
const issue_number = context.payload.pull_request.number;
|
|
59
|
+
const all = await github.paginate(github.rest.issues.listComments, { owner, repo, issue_number });
|
|
60
|
+
const existing = all.find(c => c.body && c.body.includes(marker));
|
|
61
|
+
const out = marker + '\\n' + body;
|
|
62
|
+
if (existing) await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body: out });
|
|
63
|
+
else await github.rest.issues.createComment({ owner, repo, issue_number, body: out });
|
|
64
|
+
|
|
65
|
+
- name: Enforce (fail on a blocking invariant)
|
|
66
|
+
if: always()
|
|
67
|
+
run: exit \${{ steps.guard.outputs.exit }}
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
/** Write .github/workflows/hunch-guard.yml. Never overwrites an existing file
|
|
71
|
+
* (respects user edits) — reports "exists" instead. */
|
|
72
|
+
export function writeCiWorkflow(root) {
|
|
73
|
+
const dir = join(root, ".github", "workflows");
|
|
74
|
+
const path = join(dir, "hunch-guard.yml");
|
|
75
|
+
if (existsSync(path))
|
|
76
|
+
return { path, action: "exists" };
|
|
77
|
+
mkdirSync(dir, { recursive: true });
|
|
78
|
+
writeFileSync(path, ciWorkflowYaml());
|
|
79
|
+
return { path, action: "created" };
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ciAction.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davesheffer/hunch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dave Sheffer <dave.sheffer1@gmail.com>",
|
|
6
6
|
"description": "Hunch — an Engineering Memory OS: a persistent, git-native reasoning graph over a codebase, exposed to Claude Code via MCP.",
|