rungate 0.2.1 → 0.4.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.
Files changed (129) hide show
  1. package/bin/rungate.ts +70 -0
  2. package/lib/aes-calculator.ts +66 -0
  3. package/lib/agent-audit.ts +139 -0
  4. package/lib/agent-brief-validation.ts +359 -0
  5. package/lib/agents-md-refs.ts +90 -0
  6. package/lib/bash-file-read.ts +197 -0
  7. package/lib/behavioral-cache.ts +78 -0
  8. package/lib/branch-cleanup.ts +234 -0
  9. package/lib/brief-context-parser.ts +35 -0
  10. package/lib/brief-precedence.ts +114 -0
  11. package/lib/canary.ts +117 -0
  12. package/lib/check-registry.ts +226 -0
  13. package/lib/ci-checkout.ts +426 -0
  14. package/lib/cli-commands.ts +137 -0
  15. package/lib/compliance-backfill.ts +232 -0
  16. package/lib/compliance-report.ts +605 -0
  17. package/lib/compliance.ts +361 -0
  18. package/lib/config-loader.ts +255 -0
  19. package/lib/config-readers.ts +86 -0
  20. package/lib/conformity.ts +2161 -137
  21. package/lib/container-provenance.ts +230 -0
  22. package/lib/create-brief.ts +150 -0
  23. package/lib/create-sc.ts +267 -0
  24. package/lib/decomposition-waves.ts +197 -0
  25. package/lib/deep-merge.ts +69 -0
  26. package/lib/dev-liveness.ts +214 -0
  27. package/lib/directive-extractor.ts +210 -0
  28. package/lib/distribution-standing.ts +113 -0
  29. package/lib/doc-hygiene.ts +238 -0
  30. package/lib/eval-criteria.ts +6 -0
  31. package/lib/evidence-portability.ts +123 -0
  32. package/lib/evidence-prevalidator.ts +390 -0
  33. package/lib/evidence-required.ts +150 -0
  34. package/lib/evidence-satisfiability.ts +288 -0
  35. package/lib/frontmatter.ts +163 -0
  36. package/lib/gap-scanner.ts +348 -0
  37. package/lib/gate-check-audit.ts +190 -0
  38. package/lib/gate-enforcement.ts +366 -0
  39. package/lib/generators/agent-briefs.ts +136 -0
  40. package/lib/generators/agents-md.ts +511 -0
  41. package/lib/generators/code-map.ts +60 -0
  42. package/lib/generators/types.ts +184 -0
  43. package/lib/git-ref-normalizer.ts +107 -0
  44. package/lib/github.ts +539 -0
  45. package/lib/harness-identity.ts +63 -0
  46. package/lib/hill-climb.ts +164 -0
  47. package/lib/instruction-effectiveness.ts +131 -0
  48. package/lib/issue-closure-audit.ts +225 -0
  49. package/lib/issue-create-override.ts +343 -0
  50. package/lib/issue-extraction.ts +368 -0
  51. package/lib/js-top-level-code.ts +250 -0
  52. package/lib/liveness-contradiction.ts +149 -0
  53. package/lib/organize.ts +505 -0
  54. package/lib/paths.ts +197 -6
  55. package/lib/phase2-decomposition.ts +270 -0
  56. package/lib/post-fix-verify.ts +157 -0
  57. package/lib/prior-branch.ts +512 -0
  58. package/lib/project-state-shape.ts +166 -0
  59. package/lib/promote-outputs.ts +156 -0
  60. package/lib/prove-label-gate.ts +128 -0
  61. package/lib/reachability.ts +302 -0
  62. package/lib/release-preflight.ts +288 -0
  63. package/lib/rule-health-pipeline.ts +264 -0
  64. package/lib/rule-health-trend.ts +161 -0
  65. package/lib/rule-provenance.ts +149 -0
  66. package/lib/rule-registry.ts +128 -0
  67. package/lib/rungate-schema.ts +127 -5
  68. package/lib/sc-claim-forms.ts +179 -0
  69. package/lib/sc-cover.ts +150 -0
  70. package/lib/sc-guard.ts +157 -0
  71. package/lib/sc-ids.ts +344 -0
  72. package/lib/scaffold/defaults.ts +165 -0
  73. package/lib/scaffold/managed-workflow.ts +370 -0
  74. package/lib/scaffold/owned-files.ts +265 -0
  75. package/lib/scaffold/pre-push-hook.ts +165 -0
  76. package/lib/scaffold/steps.ts +2621 -0
  77. package/lib/scanner.ts +701 -0
  78. package/lib/scope-denylist.ts +258 -0
  79. package/lib/screenshot-upload.ts +73 -0
  80. package/lib/secret-patterns.ts +112 -0
  81. package/lib/security-verdict.ts +616 -0
  82. package/lib/signal-phrases.ts +1 -0
  83. package/lib/spec-change-conformity.ts +80 -0
  84. package/lib/spec-files.ts +62 -0
  85. package/lib/spec-registry.ts +133 -0
  86. package/lib/spec-updater.ts +96 -0
  87. package/lib/stale-cleanup.ts +224 -0
  88. package/lib/stale-issue-scanner.ts +36 -0
  89. package/lib/strip-js-comments.ts +269 -0
  90. package/lib/suite-measurement.ts +228 -0
  91. package/lib/task-completion-checks.ts +224 -0
  92. package/lib/test-suite-lock.ts +1136 -0
  93. package/lib/transcript-checker.ts +1140 -0
  94. package/lib/typescript-project.ts +233 -0
  95. package/lib/validators/spec-validators.ts +171 -0
  96. package/lib/verdict-capture.ts +73 -0
  97. package/lib/verified-push.ts +535 -0
  98. package/lib/workflow-security.ts +174 -0
  99. package/lib/worktree-cleanup.ts +432 -0
  100. package/lib/worktree-collect.ts +283 -0
  101. package/lib/worktree-isolation.ts +82 -0
  102. package/lib/worktree-repo.ts +300 -0
  103. package/package.json +27 -3
  104. package/prompts/ac-adversary.md +14 -2
  105. package/prompts/aditi.md +5 -0
  106. package/prompts/blast-radius.md +5 -0
  107. package/prompts/container-verify.md +5 -0
  108. package/prompts/discovery.md +4 -0
  109. package/prompts/environment.md +5 -0
  110. package/prompts/evidence-hierarchy.md +5 -0
  111. package/prompts/evidence-validator.md +11 -0
  112. package/prompts/marcus.md +5 -0
  113. package/prompts/prevention.md +5 -0
  114. package/prompts/prove-reproducer.md +12 -0
  115. package/prompts/quinn-decision-tree.md +5 -0
  116. package/prompts/quinn-ui-brief.md +5 -0
  117. package/prompts/quinn.md +5 -0
  118. package/prompts/rca.md +5 -0
  119. package/prompts/read-before-write.md +4 -0
  120. package/prompts/regression.md +5 -0
  121. package/prompts/serena.md +5 -0
  122. package/scripts/audit-specs.ts +464 -0
  123. package/scripts/convert-spec.ts +293 -0
  124. package/scripts/create-sc.ts +92 -0
  125. package/scripts/create-spec.ts +166 -0
  126. package/scripts/extract-constraints.ts +4 -3
  127. package/scripts/generate-code-map.ts +1 -1
  128. package/scripts/scaffold-project.ts +138 -1426
  129. package/scripts/sync-spec-tests.ts +211 -126
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Recover the compliance grades that every ship run produced and then threw away.
3
+ *
4
+ * Each run writes `compliance-grade.json` into its work directory under
5
+ * `~/.rungate/<slug>/`. Nothing ever moved those into
6
+ * `~/.rungate/compliance-history.jsonl`, which is the file that
7
+ * `generateComplianceReport` reads for every trend line, every "declining"
8
+ * verdict, and every hill-climb decision. The history held four entries, all
9
+ * synthetic, all named `#compliance-test-N`, while 149 real role-records sat
10
+ * on disk.
11
+ *
12
+ * So: the compliance system has been reporting trends over a fixture, and the
13
+ * real measurements were discarded at the end of each run.
14
+ *
15
+ * WHAT THIS MODULE IS CAREFUL ABOUT
16
+ *
17
+ * - **Order.** `generateComplianceReport` treats the LAST five entries per role
18
+ * as the recent window. Appending September records to a file ending in
19
+ * October would make the oldest runs the newest, producing confident trend
20
+ * lines that run backwards. `mergeHistory` sorts; it never appends.
21
+ * - **Re-runs.** This is the sort of script someone runs twice. Identity is
22
+ * `source + role`, so a second pass adds nothing — and the two roles inside
23
+ * one grade file stay distinct rather than one being eaten as a duplicate.
24
+ * - **Provenance.** Backfilled entries are marked `backfilled: true` and keep
25
+ * their `slug`. The slug matters because #114 means runs were filed under a
26
+ * hardcoded `ddb-` prefix regardless of repo; normalising it away would
27
+ * destroy the only record of which runs were misattributed.
28
+ * - **Nothing silently skipped.** Unparseable and grade-less files are returned
29
+ * in their own buckets and the counts are made to add up, rather than a quiet
30
+ * `continue` that leaves an unexplained gap between 59 files and 57 results.
31
+ */
32
+
33
+ import { existsSync, readFileSync, readdirSync, statSync } from "fs";
34
+ import { basename, dirname, join, relative } from "path";
35
+
36
+ import { COMPLIANCE_HISTORY_SCHEMA, type ComplianceEntry } from "./compliance-report";
37
+
38
+ /** A grade file on disk, before it becomes history entries. */
39
+ interface GradeRule {
40
+ id?: string;
41
+ verdict?: string;
42
+ // #116 — these three were on disk the whole time and the backfill dropped
43
+ // them, so a reconstructed entry was as unsourced as a live one.
44
+ rule?: string;
45
+ evidence?: string;
46
+ category?: string;
47
+ }
48
+
49
+ interface Grade {
50
+ role: string;
51
+ total: number;
52
+ followed: number;
53
+ rules?: GradeRule[];
54
+ flagged?: string[];
55
+ }
56
+
57
+ export interface BackfillPlan {
58
+ /** Grade files examined, whatever became of them. */
59
+ scanned: number;
60
+ /** New entries, in no particular order — pass them through mergeHistory. */
61
+ added: BackfilledEntry[];
62
+ /** Role-records already represented in the supplied history. */
63
+ alreadyPresent: number;
64
+ /** Paths whose JSON did not parse. */
65
+ unparseable: string[];
66
+ /** Paths that parsed but carried no grades — 2 of the 59 real files. */
67
+ emptyGrades: string[];
68
+ }
69
+
70
+ export interface BackfilledEntry extends ComplianceEntry {
71
+ /** Work-directory name, e.g. `ddb-588-stale`. Evidence for #114. */
72
+ slug: string;
73
+ /** Path of the grade file this was reconstructed from, relative to the root. */
74
+ source: string;
75
+ /**
76
+ * Which grading of this role within this run, 0-based.
77
+ *
78
+ * A run can grade the same role more than once — 57 usable files hold 91
79
+ * marcus records. Keying identity on `source + role` alone silently dropped
80
+ * 35 of 149, and every count still looked plausible.
81
+ */
82
+ ordinal: number;
83
+ /** Always true here. Distinguishes a reconstruction from a live measurement. */
84
+ backfilled: true;
85
+ }
86
+
87
+ /** Identical to scripts/persist-compliance.ts:69. Two rounding rules in one
88
+ * trend line would silently put backfilled and live entries on different
89
+ * scales, and `0/0` as NaN serialises to null and poisons every average. */
90
+ const pct = (g: Grade) => (g.total > 0 ? Math.round((100 * g.followed) / g.total) : 0);
91
+
92
+ /**
93
+ * `source + role + ordinal` — the only key under which 149 records stay 149.
94
+ *
95
+ * `source + role` looks sufficient and is not: a run can grade the same role
96
+ * repeatedly (hill-climb iterations), and 57 usable files hold 91 marcus
97
+ * records. The narrower key dropped 35 of them with no error and no gap in any
98
+ * count that was being printed.
99
+ */
100
+ const identity = (e: { source?: string; slug?: string; role: string; ordinal?: number }) =>
101
+ `${e.source ?? e.slug ?? ""}::${e.role}::${e.ordinal ?? 0}`;
102
+
103
+ /**
104
+ * Every `compliance-grade.json` one level under the root, INCLUDING `.archive/`.
105
+ *
106
+ * The archive is a dot-directory, which a shell glob and Python's `glob` both
107
+ * skip by default. Counting it wrongly is not hypothetical: two sessions
108
+ * miscounted this exact directory on the way here, in opposite directions. So
109
+ * it is named explicitly rather than left to a pattern.
110
+ */
111
+ function findGradeFiles(root: string): string[] {
112
+ const out: string[] = [];
113
+ const sweep = (dir: string) => {
114
+ if (!existsSync(dir)) return;
115
+ for (const name of readdirSync(dir)) {
116
+ const workDir = join(dir, name);
117
+ let isDir = false;
118
+ try { isDir = statSync(workDir).isDirectory(); } catch { continue; }
119
+ if (!isDir) continue;
120
+ const grade = join(workDir, "compliance-grade.json");
121
+ if (existsSync(grade)) out.push(grade);
122
+ }
123
+ };
124
+ sweep(root);
125
+ sweep(join(root, ".archive"));
126
+ return out.sort();
127
+ }
128
+
129
+ /**
130
+ * Issue number for a work directory.
131
+ *
132
+ * `workflow-state.json` wins because it is what the run itself recorded; the
133
+ * slug is what the harness guessed. They disagree in 5 of the 57 real cases.
134
+ * A slug with no trailing number yields `#unknown` rather than being dropped —
135
+ * dropping it would shrink the dataset this exists to recover.
136
+ */
137
+ function resolveIssue(workDir: string, slug: string): string {
138
+ const wsPath = join(workDir, "workflow-state.json");
139
+ if (existsSync(wsPath)) {
140
+ try {
141
+ const issue = JSON.parse(readFileSync(wsPath, "utf-8"))?.issue;
142
+ if (issue !== undefined && issue !== null && `${issue}`.trim() !== "") return `#${issue}`;
143
+ } catch {
144
+ // Fall through to the slug. A corrupt workflow-state.json is a reason to
145
+ // use the weaker source, not a reason to lose the grades next to it.
146
+ }
147
+ }
148
+ const trailing = slug.match(/(\d+)(?!.*\d)/);
149
+ return trailing ? `#${trailing[1]}` : "#unknown";
150
+ }
151
+
152
+ export function planComplianceBackfill(root: string, existing: ComplianceEntry[]): BackfillPlan {
153
+ const seen = new Set(existing.map(e => identity(e as BackfilledEntry)));
154
+ const plan: BackfillPlan = { scanned: 0, added: [], alreadyPresent: 0, unparseable: [], emptyGrades: [] };
155
+
156
+ for (const gradePath of findGradeFiles(root)) {
157
+ plan.scanned++;
158
+ const workDir = dirname(gradePath);
159
+ const slug = basename(workDir);
160
+ const source = relative(root, gradePath);
161
+
162
+ let parsed: { grades?: Grade[] };
163
+ try {
164
+ parsed = JSON.parse(readFileSync(gradePath, "utf-8"));
165
+ } catch {
166
+ plan.unparseable.push(source);
167
+ continue;
168
+ }
169
+
170
+ const grades = parsed?.grades ?? [];
171
+ if (grades.length === 0) {
172
+ plan.emptyGrades.push(source);
173
+ continue;
174
+ }
175
+
176
+ // The run did not stamp a time, so the grade file's mtime is the best
177
+ // available. It is the moment grading finished, which is what a trend
178
+ // over runs wants.
179
+ const timestamp = new Date(statSync(gradePath).mtime).toISOString();
180
+ const issue = resolveIssue(workDir, slug);
181
+
182
+ const roleSeq: Record<string, number> = {};
183
+ for (const g of grades) {
184
+ const ordinal = roleSeq[g.role] ?? 0;
185
+ roleSeq[g.role] = ordinal + 1;
186
+ const entry: BackfilledEntry = {
187
+ timestamp,
188
+ issue,
189
+ role: g.role,
190
+ schema: COMPLIANCE_HISTORY_SCHEMA,
191
+ rules: (g.rules ?? [])
192
+ .filter(r => r.id)
193
+ .map(r => ({
194
+ id: r.id as string,
195
+ verdict: r.verdict || "N/A",
196
+ rule: r.rule,
197
+ evidence: r.evidence,
198
+ category: r.category,
199
+ })),
200
+ total: g.total,
201
+ followed: g.followed,
202
+ pct: pct(g),
203
+ flagged: g.flagged ?? [],
204
+ slug,
205
+ source,
206
+ ordinal,
207
+ backfilled: true,
208
+ };
209
+ if (seen.has(identity(entry))) {
210
+ plan.alreadyPresent++;
211
+ continue;
212
+ }
213
+ seen.add(identity(entry));
214
+ plan.added.push(entry);
215
+ }
216
+ }
217
+
218
+ return plan;
219
+ }
220
+
221
+ /**
222
+ * Chronological merge. Never an append.
223
+ *
224
+ * The synthetic `#compliance-test-N` entries are kept: they are the only record
225
+ * of what the trend system was reporting before the backfill, and deleting data
226
+ * to make a graph look better is not a backfill.
227
+ */
228
+ export function mergeHistory(existing: ComplianceEntry[], added: ComplianceEntry[]): ComplianceEntry[] {
229
+ return [...existing, ...added].sort(
230
+ (a, b) => Date.parse(a.timestamp) - Date.parse(b.timestamp),
231
+ );
232
+ }