rungate 0.7.0 → 0.8.1

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.
Files changed (108) hide show
  1. package/gates/.gate-salt +1 -0
  2. package/gates/SCHEMA-GUIDE.md +485 -0
  3. package/gates/ac-quality.test.ts +106 -0
  4. package/gates/adversarial.test.ts +283 -0
  5. package/gates/brief-assembler.test.ts +487 -0
  6. package/gates/brief-assembler.ts +448 -0
  7. package/gates/bunfig.toml +2 -0
  8. package/gates/chain.test.ts +91 -0
  9. package/gates/e2e-smoke.test.ts +234 -0
  10. package/gates/error-classifier.test.ts +250 -0
  11. package/gates/error-classifier.ts +99 -0
  12. package/gates/gate-executor.ts +3071 -0
  13. package/gates/orchestrator.test.ts +968 -0
  14. package/gates/orchestrator.ts +827 -0
  15. package/gates/preload.ts +89 -0
  16. package/gates/prompts/ac-adversary.md +20 -0
  17. package/gates/prompts/evidence-validator.md +22 -0
  18. package/gates/prompts/prove-reproducer.md +53 -0
  19. package/gates/prompts.sha256 +3 -0
  20. package/gates/prove.test.ts +164 -0
  21. package/gates/rook-state.test.ts +108 -0
  22. package/gates/run-attribution.ts +290 -0
  23. package/gates/run-gate.ts +163 -0
  24. package/gates/schema-parity.test.ts +378 -0
  25. package/gates/schema.ts +1209 -0
  26. package/gates/self-heal.test.ts +511 -0
  27. package/gates/self-heal.ts +125 -0
  28. package/gates/ship-orchestrator.test.ts +399 -0
  29. package/gates/ship-orchestrator.ts +384 -0
  30. package/gates/spec-compliance-probe.ts +159 -0
  31. package/gates/test-fixtures/broken-no-environments.json +110 -0
  32. package/gates/test-fixtures/consumer-directory-layout.json +90 -0
  33. package/gates/test-fixtures/valid-standard.json +91 -0
  34. package/gates/test-utils.ts +43 -0
  35. package/gates/typecheck-gate.ts +111 -0
  36. package/gates/witness.ts +349 -0
  37. package/gates/workflow.test.ts +2018 -0
  38. package/hooks/ActionVerificationGate.hook.ts +96 -0
  39. package/hooks/AgentBriefGuard.hook.ts +38 -0
  40. package/hooks/AgentVerdictCapture.hook.ts +87 -0
  41. package/hooks/AutoVerifyGate.hook.ts +79 -0
  42. package/hooks/BashToolGuard.hook.ts +65 -0
  43. package/hooks/BashWriteGuard.hook.ts +60 -0
  44. package/hooks/CommitEnforcement.hook.ts +130 -0
  45. package/hooks/DocHygiene.hook.ts +148 -0
  46. package/hooks/GateEnforcement.hook.ts +58 -0
  47. package/hooks/GateResultProtection.hook.ts +95 -0
  48. package/hooks/HarnessAFKGate.hook.ts +85 -0
  49. package/hooks/HookCurrency.hook.ts +99 -0
  50. package/hooks/IssueCloseGuard.hook.ts +149 -0
  51. package/hooks/MergeGuard.hook.ts +58 -0
  52. package/hooks/PostCompact.hook.ts +60 -0
  53. package/hooks/ProjectStateSync.hook.ts +103 -0
  54. package/hooks/SkillGuard.hook.ts +69 -0
  55. package/hooks/SpecConformityTrigger.hook.ts +43 -0
  56. package/hooks/SpecSCGuard.hook.ts +123 -0
  57. package/hooks/StaleTTLCleanup.hook.ts +141 -0
  58. package/hooks/TaskCompleted.hook.ts +51 -0
  59. package/hooks/TeamInit.hook.ts +85 -0
  60. package/hooks/TestSuiteGuard.hook.ts +70 -0
  61. package/hooks/TestSuiteRelease.hook.ts +85 -0
  62. package/hooks/TopLevelCdGuard.hook.ts +108 -0
  63. package/hooks/VerifyPhaseLock.hook.ts +84 -0
  64. package/hooks/WorkflowStateGuard.hook.ts +64 -0
  65. package/hooks/lib/agent-config.json +9 -0
  66. package/hooks/lib/agentDetection.ts +201 -0
  67. package/hooks/lib/comment-template.ts +86 -0
  68. package/hooks/lib/findWorkflow.ts +101 -0
  69. package/hooks/lib/parseStdin.ts +37 -0
  70. package/hooks/lib/paths.ts +111 -0
  71. package/hooks/lib/utils.ts +338 -0
  72. package/lib/affected-tests.ts +29 -0
  73. package/lib/bash-write-guard.ts +627 -0
  74. package/lib/branch-cleanup.ts +279 -7
  75. package/lib/config-trackable.ts +9 -0
  76. package/lib/distribution-currency.ts +55 -5
  77. package/lib/failure-intake.ts +7 -1
  78. package/lib/gate-enforcement.ts +360 -38
  79. package/lib/gate-names.ts +72 -0
  80. package/lib/gate-result-protection.ts +437 -0
  81. package/lib/generators/code-map.ts +55 -4
  82. package/lib/generators/types.ts +27 -1
  83. package/lib/intake-sources.ts +380 -0
  84. package/lib/js-top-level-code.ts +20 -2
  85. package/lib/junit-checks.ts +239 -1
  86. package/lib/late-env-spawn.ts +331 -0
  87. package/lib/launch-base.ts +128 -5
  88. package/lib/package-closure.ts +114 -0
  89. package/lib/paths.ts +96 -3
  90. package/lib/phase2-decomposition.ts +1 -1
  91. package/lib/published-artifact.ts +162 -18
  92. package/lib/sc-id-cross-tree.ts +84 -8
  93. package/lib/scaffold/ai-review-findings.ts +149 -4
  94. package/lib/scaffold/ai-review-step.ts +1006 -132
  95. package/lib/scaffold/defaults.ts +82 -1
  96. package/lib/scaffold/steps.ts +642 -54
  97. package/lib/scanner.ts +4 -2
  98. package/lib/security-verdict.ts +1 -1
  99. package/lib/shell-path-interpolation.ts +594 -0
  100. package/lib/text-dependents.ts +875 -0
  101. package/lib/transcript-checker.ts +6 -0
  102. package/lib/workflow-security.ts +29 -0
  103. package/lib/workflow-taint.ts +732 -0
  104. package/lib/worktree-cleanup.ts +65 -38
  105. package/lib/worktree-repo.ts +32 -2
  106. package/package.json +4 -1
  107. package/scripts/check-installed-hook-currency.ts +825 -0
  108. package/scripts/generate-code-map.ts +13 -8
@@ -0,0 +1,448 @@
1
+ import { readFileSync, writeFileSync, existsSync } from "fs";
2
+ import { join, basename, dirname } from "path";
3
+ import { fileURLToPath } from "url";
4
+ import { plantCanaries, getDefaultCanaries } from "../lib/canary.js";
5
+ import { tryLoadRungateConfig } from "../lib/config-loader";
6
+ import {
7
+ classifyContextDoc, classifyContextDocs, contextDocLines, contextDocNotes, hasBriefUnsafeChars,
8
+ type ContextDocVerdict,
9
+ } from "../lib/context-docs";
10
+
11
+ // ── Gate contract interfaces (SC-373) ─────────────────────────────────────
12
+
13
+ export interface AssembleBriefInput {
14
+ slug: string;
15
+ workDir: string;
16
+ projectRoot: string;
17
+ }
18
+
19
+ export interface AssembleResult {
20
+ briefPath: string;
21
+ acCount: number;
22
+ contextFileCount: number;
23
+ validated: boolean;
24
+ }
25
+
26
+ /**
27
+ * The brief policies as the package ships them.
28
+ *
29
+ * #1174: this was `$HOME/.claude/skills/ship/brief-policies.json` and nothing
30
+ * else — a path that exists on exactly one machine — while the
31
+ * `config/brief-policies.json` the tarball actually carries was never read.
32
+ * "A control that can only run on one laptop is the same fail-open as #140",
33
+ * which is what `lib/issue-closure-audit.ts` says about its own
34
+ * `~/.rungate/{slug}/run/`.
35
+ *
36
+ * Resolved from this module's own location, so it follows the package to
37
+ * wherever it is installed. `lib/package-closure.ts` reads this expression
38
+ * statically and fails the suite if `config/` ever leaves `files[]`.
39
+ */
40
+ const PACKAGED_BRIEF_POLICIES = join(
41
+ dirname(fileURLToPath(import.meta.url)),
42
+ "..",
43
+ "config",
44
+ "brief-policies.json",
45
+ );
46
+
47
+ /** A local edit layered over the shipped policies. Absent is the normal case. */
48
+ const BRIEF_POLICIES_OVERRIDE = join(
49
+ process.env.HOME || "",
50
+ ".claude",
51
+ "skills",
52
+ "ship",
53
+ "brief-policies.json",
54
+ );
55
+
56
+ export async function assembleBrief(opts: AssembleBriefInput): Promise<AssembleResult> {
57
+ const { slug, workDir, projectRoot } = opts;
58
+
59
+ const sf = join(workDir, "workflow-state.json");
60
+ if (!existsSync(sf)) {
61
+ throw new Error(`workflow-state.json not found at ${sf}`);
62
+ }
63
+
64
+ const state = JSON.parse(readFileSync(sf, "utf-8"));
65
+ const acs: any[] = state.acs || [];
66
+
67
+ const contextFiles = deduplicateContextFiles(acs);
68
+ const verifyCommands = extractVerifyCommands(acs);
69
+ const scopeOut: string[] = state.scopeOut || [];
70
+
71
+ // #439: through the one loader, so a project on the DIRECTORY layout is
72
+ // read, and so a malformed config THROWS rather than reading as absent.
73
+ // The previous `catch { return null }` turned a trailing comma into "no
74
+ // docs declared" — `.claude/rules/config-access.md` names both.
75
+ //
76
+ // #311 SC-A9 asks for the THROWING loader here, per ADR-001 D2's caller
77
+ // table ("a gate run without config is meaningless, not degraded"). It is
78
+ // NOT used, and the conflict is recorded rather than resolved quietly.
79
+ //
80
+ // ADR-001 predates #439, which shipped SC-1614 — *malformed config throws;
81
+ // absent config does not* — with the two halves asserted separately
82
+ // precisely so nobody could conflate them again. Throwing here conflates
83
+ // them from the other direction: an unscaffolded project and a project with
84
+ // a trailing comma would once more produce one outcome, which is the
85
+ // corollary ADR-001 itself exists to defend. SC-1617 pins this call in
86
+ // source text for the same reason.
87
+ //
88
+ // The worry behind SC-A9 is real — a silently empty context section reads
89
+ // like a project that declared no docs. It is not answered by throwing; it
90
+ // is answered by the brief SAYING so, which is #497's question and not
91
+ // this issue's.
92
+ const harness = tryLoadRungateConfig(projectRoot);
93
+ const projectContextDocs = classifyContextDocs(projectRoot, harness?.contextDocs);
94
+ const hasAgents = existsSync(join(projectRoot, "AGENTS.md"));
95
+
96
+ const sections: string[] = [];
97
+
98
+ sections.push(buildContextSection(projectRoot, projectContextDocs));
99
+ sections.push(buildSpecAlignment(state.governingSpec));
100
+ sections.push(buildTaskSection(state.issueGoal, acs));
101
+ if (state.issueType === "bug-fix" || state.issueType === "bug") {
102
+ sections.push(buildRCASection(state.rca));
103
+ }
104
+ sections.push(buildGitProtocol(state.issue));
105
+ sections.push(buildFilesSection(projectRoot, contextFiles));
106
+ sections.push(buildScopeSection(scopeOut, harness?.forbiddenCommands));
107
+ sections.push(buildVerifySection(verifyCommands));
108
+ sections.push(buildReportSection());
109
+
110
+ let brief = sections.join("\n\n");
111
+
112
+ // Plant canary values in the brief for behavioral verification (D-5, SC-409)
113
+ const canaries = getDefaultCanaries().filter((c) => c.location === "brief");
114
+ if (canaries.length > 0) {
115
+ const { brief: briefWithCanaries, planted } = plantCanaries(brief, canaries);
116
+ brief = briefWithCanaries;
117
+ writeFileSync(join(workDir, "canaries.json"), JSON.stringify(planted.map(c => ({
118
+ id: c.id, phrase: c.phrase, location: c.location,
119
+ })), null, 2));
120
+ }
121
+
122
+ const briefPath = join(workDir, "marcus-brief.md");
123
+ writeFileSync(briefPath, brief);
124
+
125
+ const validated = validateBrief(brief);
126
+
127
+ const projectContextCount =
128
+ projectContextDocs.filter(d => d.kind === "included").length + (hasAgents ? 1 : 0);
129
+
130
+ return {
131
+ briefPath,
132
+ acCount: acs.length,
133
+ contextFileCount: contextFiles.length + projectContextCount,
134
+ validated,
135
+ };
136
+ }
137
+
138
+ function deduplicateContextFiles(acs: any[]): Array<{ path: string; reason: string }> {
139
+ const seen = new Set<string>();
140
+ const result: Array<{ path: string; reason: string }> = [];
141
+
142
+ for (const ac of acs) {
143
+ for (const cf of ac.contextFiles || []) {
144
+ if (!seen.has(cf.path)) {
145
+ seen.add(cf.path);
146
+ result.push({ path: cf.path, reason: cf.reason || "" });
147
+ }
148
+ }
149
+ }
150
+
151
+ return result;
152
+ }
153
+
154
+ function extractVerifyCommands(acs: any[]): string[] {
155
+ const commands: string[] = [];
156
+ for (const ac of acs) {
157
+ const cmd = ac.evidenceMethod?.command;
158
+ if (cmd) commands.push(cmd);
159
+ }
160
+ return commands;
161
+ }
162
+
163
+ // `loadProjectHarness` and `extractContextDocs` were deleted in #439. The
164
+ // first read `.claude/rungate.json` directly and swallowed parse errors; the
165
+ // second filtered out absolute and traversing paths with no record that it
166
+ // had. `lib/config-loader.ts` and `lib/context-docs.ts` own those now, and
167
+ // each owns it once.
168
+
169
+ function buildContextSection(projectRoot: string, docs: ContextDocVerdict[]): string {
170
+ const lines = [
171
+ "## Context (read first, in order)",
172
+ `1. ${projectRoot}/CLAUDE.md (if exists)`,
173
+ // "(if exists)" like entry 1, and for the same reason (#272). This said
174
+ // PRIMARY with no qualifier while entries 3 and 4 were both conditional on
175
+ // its absence — so in a project without MODEL.md, which includes this one,
176
+ // the list opened by naming a missing file as the primary source.
177
+ `2. ${projectRoot}/MODEL.md (if exists; PRIMARY when present — system flow, three rules, troubleshooting map)`,
178
+ `3. ${projectRoot}/PRINCIPLES.md (ONLY if MODEL.md doesn't exist)`,
179
+ `4. ${projectRoot}/ARCHITECTURE.md (ONLY if MODEL.md doesn't exist)`,
180
+ ];
181
+
182
+ let idx = 5;
183
+
184
+ // Include AGENTS.md from project repo when present
185
+ const agentsPath = join(projectRoot, "AGENTS.md");
186
+ if (existsSync(agentsPath)) {
187
+ lines.push(`${idx}. ${projectRoot}/AGENTS.md (agent role definitions)`);
188
+ idx++;
189
+ }
190
+
191
+ // contextDocs, classified rather than filtered: every declared entry either
192
+ // appears here or is named in the notes below. Nothing is dropped silently.
193
+ lines.push(...contextDocLines(docs, idx));
194
+ lines.push(...contextDocNotes(docs));
195
+
196
+ return lines.join("\n");
197
+ }
198
+
199
+ function buildSpecAlignment(governingSpec?: { path: string; detectedFrom?: string }): string {
200
+ if (!governingSpec) {
201
+ return [
202
+ "## Spec Alignment",
203
+ "- **Governing spec:** None detected",
204
+ "- **Verified no contradiction:** N/A",
205
+ ].join("\n");
206
+ }
207
+
208
+ return [
209
+ "## Spec Alignment (MANDATORY)",
210
+ `- **Governing spec:** ${governingSpec.path}`,
211
+ `- **Detected from:** ${governingSpec.detectedFrom || "unknown"}`,
212
+ "- **Verified no contradiction:** DA verified before brief generation",
213
+ ].join("\n");
214
+ }
215
+
216
+ function buildTaskSection(issueGoal: string, acs: any[]): string {
217
+ const lines = ["## Task", "", `**Goal:** ${issueGoal}`, ""];
218
+
219
+ lines.push("### Acceptance Criteria", "");
220
+ if (acs.length === 0) {
221
+ lines.push("- (no acceptance criteria defined yet)");
222
+ } else {
223
+ for (const ac of acs) {
224
+ const threshold = ac.threshold
225
+ ? ` (${ac.threshold.op} ${ac.threshold.value}${ac.threshold.unit ? " " + ac.threshold.unit : ""})`
226
+ : "";
227
+ lines.push(`- **${ac.id}** [${ac.type || "CODE"}]: ${ac.statement}${threshold}`);
228
+ }
229
+ }
230
+
231
+ return lines.join("\n");
232
+ }
233
+
234
+ function buildGitProtocol(issue: number): string {
235
+ return [
236
+ "## Git protocol",
237
+ `- Branch: \`${issue}-deep-modules\` (create from main if not in a worktree)`,
238
+ "- Before committing, rebase onto latest main",
239
+ "- Commit all changes before reporting back",
240
+ "- Push with `-u` before reporting",
241
+ "- Never commit to main directly, never force push",
242
+ ].join("\n");
243
+ }
244
+
245
+ function buildFilesSection(
246
+ projectRoot: string,
247
+ contextFiles: Array<{ path: string; reason: string }>,
248
+ ): string {
249
+ const lines = ["## Files to read before touching anything"];
250
+
251
+ if (contextFiles.length === 0) {
252
+ lines.push("- (no context files specified)");
253
+ } else {
254
+ for (const cf of contextFiles) {
255
+ // The same classifier `contextDocs` uses, not a second copy of its
256
+ // rules (#497). These paths come from the AC schema and had no
257
+ // filtering of any kind before BUG-439-A, and an externality control on
258
+ // one of the two doors into a brief is the thing #439 correctly said
259
+ // the old filter was not.
260
+ //
261
+ // The reason string goes through the character predicate separately:
262
+ // it is rendered on the same line and the classifier only sees paths.
263
+ const verdict = classifyContextDoc(projectRoot, cf.path);
264
+ if (verdict.kind === "rejected") {
265
+ lines.push(`- (REJECTED ${JSON.stringify(cf.path)}: ${verdict.reason})`);
266
+ continue;
267
+ }
268
+ if (hasBriefUnsafeChars(cf.reason ?? "")) {
269
+ lines.push(`- (REJECTED ${JSON.stringify(cf.path)}: its reason contains a newline or control character)`);
270
+ continue;
271
+ }
272
+ // Rendered as declared, not as resolved: these are repo-relative paths
273
+ // an agent is meant to recognise, and rewriting them to absolute would
274
+ // change every existing brief for no gain.
275
+ lines.push(`- ${cf.path}${cf.reason ? " — " + cf.reason : ""}`);
276
+ }
277
+ }
278
+
279
+ return lines.join("\n");
280
+ }
281
+
282
+ function buildScopeSection(scopeOut: string[], forbiddenCommands?: string[]): string {
283
+ const lines = ["## Scope — do NOT touch"];
284
+
285
+ if (scopeOut.length === 0) {
286
+ lines.push("- (no explicit scope boundaries — stay within task scope)");
287
+ } else {
288
+ for (const item of scopeOut) {
289
+ lines.push(`- ${item}`);
290
+ }
291
+ }
292
+
293
+ // #343: this said "Never run `make rebuild`" — one consumer's forbidden
294
+ // command, written into every consumer's brief. The command comes from the
295
+ // consumer's own `forbiddenCommands` now, and nothing is emitted when none
296
+ // is configured. A guessed prohibition in a brief is worse than silence:
297
+ // the agent obeys it, and nobody finds out it was invented.
298
+ for (const cmd of forbiddenCommands ?? []) {
299
+ lines.push(`- Never run \`${cmd}\` — the DA runs it, not you`);
300
+ }
301
+ // Harness-general, not consumer-specific: these files are DA-owned for
302
+ // every project, because parallel worktrees each writing their own copy
303
+ // produce conflicting state (#296).
304
+ lines.push("- Never modify `project-state.json` or `PROJECT-STATE.md` — DA manages these");
305
+
306
+ return lines.join("\n");
307
+ }
308
+
309
+ function buildVerifySection(commands: string[]): string {
310
+ const lines = ["## Verify"];
311
+
312
+ if (commands.length === 0) {
313
+ lines.push("- `bunx tsc --noEmit`");
314
+ } else {
315
+ for (const cmd of commands) {
316
+ lines.push(`- \`${cmd}\``);
317
+ }
318
+ lines.push("- `bunx tsc --noEmit`");
319
+ }
320
+
321
+ return lines.join("\n");
322
+ }
323
+
324
+ function buildRCASection(rca?: { rootCause?: string; prediction?: string; predictionVerified?: boolean }): string {
325
+ return [
326
+ "## Root Cause Analysis (bug-fix)",
327
+ `- **Root cause:** ${rca?.rootCause || "(investigate and fill)"}`,
328
+ `- **Prediction:** ${rca?.prediction || "(what will the fix change?)"}`,
329
+ `- **Prediction verified:** ${rca?.predictionVerified ?? "(verify after fix)"}`,
330
+ ].join("\n");
331
+ }
332
+
333
+ export interface JourneyStep {
334
+ action: string;
335
+ wait_for?: string;
336
+ assertion?: { type: string; target?: string; expected?: string };
337
+ on_fail?: string;
338
+ }
339
+
340
+ export const MAX_JOURNEY_STEPS = 8;
341
+
342
+ export function validateJourneySteps(steps: JourneyStep[]): { valid: boolean; splitRequired: boolean } {
343
+ if (steps.length <= MAX_JOURNEY_STEPS) return { valid: true, splitRequired: false };
344
+ return { valid: false, splitRequired: true };
345
+ }
346
+
347
+ export function splitJourney(steps: JourneyStep[]): JourneyStep[][] {
348
+ const chunks: JourneyStep[][] = [];
349
+ for (let i = 0; i < steps.length; i += MAX_JOURNEY_STEPS) {
350
+ chunks.push(steps.slice(i, i + MAX_JOURNEY_STEPS));
351
+ }
352
+ return chunks;
353
+ }
354
+
355
+ function buildReportSection(): string {
356
+ return [
357
+ "## Report back",
358
+ "For each acceptance criterion, provide evidence:",
359
+ "- AC-N: → file.ts:L## [description] (code presence)",
360
+ "- AC-N: → grep: \"pattern\" returned N matches (absence/presence)",
361
+ "- AC-N: → test: test-name PASS (test-verified)",
362
+ "Plus: files changed (paths + line numbers), test output, scope conflicts",
363
+ ].join("\n");
364
+ }
365
+
366
+ /**
367
+ * Shipped policies, with the `$HOME` copy layered over them when it exists.
368
+ *
369
+ * Deliberately unguarded. The previous reader wrapped the parse in `catch {}`
370
+ * and fell through to a hardcoded second copy of the patterns, so a trailing
371
+ * comma in the policy file read as "no policies" and the brief was then
372
+ * reported `validated: true` — UNMEASURED passing itself off as a pass.
373
+ * `.claude/rules/config-access.md` names that Silent Default Substitution and
374
+ * is explicit that a corrupt config is never a missing config.
375
+ */
376
+ export function loadBriefPolicies(): any {
377
+ if (!existsSync(PACKAGED_BRIEF_POLICIES)) {
378
+ throw new Error(
379
+ `brief policies not found at ${PACKAGED_BRIEF_POLICIES}. The package ships ` +
380
+ `config/brief-policies.json; an installation without it cannot validate a brief, ` +
381
+ `and UNMEASURED refuses rather than passing.`,
382
+ );
383
+ }
384
+ const shipped = JSON.parse(readFileSync(PACKAGED_BRIEF_POLICIES, "utf-8"));
385
+ if (!existsSync(BRIEF_POLICIES_OVERRIDE)) return shipped;
386
+
387
+ // Per agent type, so an override naming only `marcus` does not delete the
388
+ // shipped `quinn` and `rook` entries.
389
+ const override = JSON.parse(readFileSync(BRIEF_POLICIES_OVERRIDE, "utf-8"));
390
+ return {
391
+ ...shipped,
392
+ ...override,
393
+ policies: { ...shipped.policies, ...override.policies },
394
+ };
395
+ }
396
+
397
+ function validateBrief(brief: string): boolean {
398
+ return validateBriefAgainst(brief, loadBriefPolicies());
399
+ }
400
+
401
+ /**
402
+ * Split from the loader so the refusal below has an input a test can hand it.
403
+ * A refusal nothing can reach is a refusal nobody has watched fire — see
404
+ * `.claude/rules/checks-must-be-able-to-fail.md`.
405
+ */
406
+ export function validateBriefAgainst(brief: string, policies: any): boolean {
407
+ const required = policies?.policies?.marcus?.requiredPatterns;
408
+
409
+ // An empty policy set validates every brief, which is indistinguishable
410
+ // from not validating at all — the exact state the issue asks be refused.
411
+ if (!Array.isArray(required) || required.length === 0) {
412
+ throw new Error(
413
+ `brief policies declare no requiredPatterns for "marcus". An empty policy set passes ` +
414
+ `every brief and cannot be told apart from no validation at all, so this refuses.`,
415
+ );
416
+ }
417
+
418
+ for (const pattern of required) {
419
+ let p = pattern.pattern as string;
420
+ let flags = "";
421
+ if (p.startsWith("(?i)")) {
422
+ p = p.slice(4);
423
+ flags = "i";
424
+ }
425
+ const re = new RegExp(p, flags);
426
+ if (!re.test(brief)) return false;
427
+ }
428
+
429
+ return true;
430
+ }
431
+
432
+ // CLI entry point
433
+ if (import.meta.main) {
434
+ const args = process.argv.slice(2);
435
+ let slug = "", workDir = "", projectRoot = "";
436
+ for (let i = 0; i < args.length; i++) {
437
+ if (args[i] === "--slug" && args[i + 1]) slug = args[++i];
438
+ if (args[i] === "--work-dir" && args[i + 1]) workDir = args[++i];
439
+ if (args[i] === "--project-root" && args[i + 1]) projectRoot = args[++i];
440
+ }
441
+ if (!slug || !workDir || !projectRoot) {
442
+ console.error("Usage: bun run brief-assembler.ts --slug SLUG --work-dir DIR --project-root DIR");
443
+ process.exit(1);
444
+ }
445
+ assembleBrief({ slug, workDir, projectRoot })
446
+ .then(r => console.log(`Brief written: ${r.briefPath} (${r.acCount} ACs, validated=${r.validated})`))
447
+ .catch(e => { console.error(e.message); process.exit(1); });
448
+ }
@@ -0,0 +1,2 @@
1
+ [test]
2
+ preload = ["./preload.ts"]
@@ -0,0 +1,91 @@
1
+ import { test, expect, describe } from "bun:test";
2
+ import { readFileSync, existsSync } from "fs";
3
+ import { GoalRecordSchema, ShipEvidenceSchema, ProveEvidenceSchema } from "./schema";
4
+
5
+ const TEST_DIR = process.env.TEST_WORK_DIR || "";
6
+
7
+ function tryRead(filename: string) {
8
+ const path = `${TEST_DIR}/${filename}`;
9
+ if (!existsSync(path)) return null;
10
+ return JSON.parse(readFileSync(path, "utf-8"));
11
+ }
12
+
13
+ describe("chain: goal → ship → prove artifacts", () => {
14
+ test("goal-record.json validates if present", () => {
15
+ const data = tryRead("goal-record.json");
16
+ if (!data) return;
17
+ const result = GoalRecordSchema.safeParse(data);
18
+ if (!result.success) {
19
+ const issues = result.error.issues.map(i => `${i.path.join(".")}: ${i.message}`);
20
+ expect(issues).toEqual([]);
21
+ }
22
+ });
23
+
24
+ test("goal-record has non-empty success criteria", () => {
25
+ const data = tryRead("goal-record.json");
26
+ if (!data) return;
27
+ expect(data.successCriteria.length).toBeGreaterThan(0);
28
+ });
29
+
30
+ test("goal-record SCs all have thresholds", () => {
31
+ const data = tryRead("goal-record.json");
32
+ if (!data) return;
33
+ const missing = data.successCriteria.filter((sc: any) => !sc.threshold);
34
+ expect(missing.map((sc: any) => sc.id)).toEqual([]);
35
+ });
36
+
37
+ test("ship-evidence.json validates if present", () => {
38
+ const data = tryRead("ship-evidence.json");
39
+ if (!data) return;
40
+ const result = ShipEvidenceSchema.safeParse(data);
41
+ if (!result.success) {
42
+ const issues = result.error.issues.map(i => `${i.path.join(".")}: ${i.message}`);
43
+ expect(issues).toEqual([]);
44
+ }
45
+ });
46
+
47
+ test("ship-evidence has no FAIL results when status is PASS", () => {
48
+ const data = tryRead("ship-evidence.json");
49
+ if (!data || data.gateOut.status !== "PASS") return;
50
+ const fails = data.gateOut.evidence.filter((e: any) => e.result === "FAIL");
51
+ expect(fails.map((e: any) => e.criterion)).toEqual([]);
52
+ });
53
+
54
+ test("prove-evidence.json validates if present", () => {
55
+ const data = tryRead("prove-evidence.json");
56
+ if (!data) return;
57
+ const result = ProveEvidenceSchema.safeParse(data);
58
+ if (!result.success) {
59
+ const issues = result.error.issues.map(i => `${i.path.join(".")}: ${i.message}`);
60
+ expect(issues).toEqual([]);
61
+ }
62
+ });
63
+
64
+ test("prove verdict consistent with before/after evidence", () => {
65
+ const data = tryRead("prove-evidence.json");
66
+ if (!data) return;
67
+ if (data.verdict === "PROVEN") {
68
+ expect(data.afterEvidence.description.length).toBeGreaterThan(0);
69
+ expect(data.comparisonSummary.length).toBeGreaterThan(0);
70
+ }
71
+ });
72
+
73
+ test("prove PROVEN has no unresolved gaps", () => {
74
+ const data = tryRead("prove-evidence.json");
75
+ if (!data || data.verdict !== "PROVEN") return;
76
+ const unresolved = (data.gaps || []).filter((g: any) => g.status === "UNPROVEN");
77
+ expect(unresolved.map((g: any) => g.ac)).toEqual([]);
78
+ });
79
+
80
+ test("chain consistency: issue numbers match across artifacts", () => {
81
+ const goal = tryRead("goal-record.json");
82
+ const ship = tryRead("ship-evidence.json");
83
+ const prove = tryRead("prove-evidence.json");
84
+ if (!goal || !ship) return;
85
+ const goalNum = parseInt(goal.id.replace("issue-", ""));
86
+ expect(ship.issueNumber).toBe(goalNum);
87
+ if (prove) {
88
+ expect(prove.issueNumber).toBe(goalNum);
89
+ }
90
+ });
91
+ });