rungate 0.4.1 → 0.6.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 (87) hide show
  1. package/config/behavioral-sc-map.json +170 -0
  2. package/config/brief-policies.json +28 -0
  3. package/config/ceremony-profiles.json +207 -0
  4. package/config/check-registry.json +114 -0
  5. package/config/matcher-registry.json +142 -0
  6. package/config/prove-evidence.schema.json +65 -0
  7. package/config/spec-policies.json +36 -0
  8. package/config/universal-rules.yaml +15 -0
  9. package/config/workflow-schema.json +279 -0
  10. package/lib/agents-md-sections.ts +144 -0
  11. package/lib/branch-diff.ts +139 -0
  12. package/lib/bun-version.ts +268 -0
  13. package/lib/ci-checkout.ts +1 -1
  14. package/lib/cli-commands.ts +7 -1
  15. package/lib/compliance.ts +13 -11
  16. package/lib/config-loader.ts +12 -0
  17. package/lib/conformity.ts +1147 -116
  18. package/lib/context-docs.ts +143 -28
  19. package/lib/create-brief.ts +2 -1
  20. package/lib/create-sc.ts +90 -4
  21. package/lib/doc-scan-targets.ts +9 -0
  22. package/lib/evidence-command-transport.ts +160 -0
  23. package/lib/evidence-prevalidator.ts +22 -0
  24. package/lib/fallow-availability.ts +129 -0
  25. package/lib/gate-refusal-currency.ts +112 -0
  26. package/lib/gate-timeouts.ts +82 -0
  27. package/lib/generators/agent-briefs.ts +4 -1
  28. package/lib/generators/agents-md.ts +71 -23
  29. package/lib/generators/code-map.ts +2 -1
  30. package/lib/generators/types.ts +8 -0
  31. package/lib/git-dates.ts +55 -0
  32. package/lib/github.ts +195 -0
  33. package/lib/js-top-level-code.ts +27 -2
  34. package/lib/junit-checks.ts +484 -0
  35. package/lib/local-calendar-day.ts +78 -0
  36. package/lib/package-closure.ts +278 -2
  37. package/lib/project-state-shape.ts +59 -0
  38. package/lib/promote-outputs.ts +6 -2
  39. package/lib/release-preflight.ts +196 -0
  40. package/lib/report-provenance.ts +156 -0
  41. package/lib/sc-claim-forms.ts +75 -0
  42. package/lib/sc-id-cross-tree.ts +311 -0
  43. package/lib/scaffold/ai-review-step.ts +180 -0
  44. package/lib/scaffold/defaults.ts +40 -0
  45. package/lib/scaffold/managed-workflow.ts +125 -27
  46. package/lib/scaffold/owned-files.ts +48 -2
  47. package/lib/scaffold/profiles.ts +94 -0
  48. package/lib/scaffold/rehearsal.ts +232 -73
  49. package/lib/scaffold/steps.ts +777 -152
  50. package/lib/scaffold/write-steps.ts +56 -13
  51. package/lib/scanner.ts +38 -12
  52. package/lib/source-anchor.ts +144 -0
  53. package/lib/spec-listing.ts +148 -0
  54. package/lib/spec-requirements.ts +209 -0
  55. package/lib/success-doc.ts +779 -0
  56. package/lib/suite-run-reading.ts +364 -0
  57. package/lib/test-census.ts +199 -0
  58. package/lib/test-command.ts +133 -0
  59. package/lib/test-suite-lock.ts +166 -10
  60. package/lib/tool-probe.ts +120 -0
  61. package/lib/tracked-vs-ignored.ts +234 -0
  62. package/lib/trajectory.ts +377 -0
  63. package/lib/validators/spec-validators.ts +35 -6
  64. package/lib/workflow-security.ts +101 -0
  65. package/lib/workflow-test-harness.ts +349 -0
  66. package/package.json +6 -1
  67. package/scripts/audit-specs.ts +247 -12
  68. package/scripts/audit-transcript.ts +2 -1
  69. package/scripts/convert-spec.ts +2 -1
  70. package/scripts/create-sc.ts +17 -7
  71. package/scripts/create-spec.ts +2 -1
  72. package/scripts/create-success.ts +163 -0
  73. package/scripts/extract-constraints.ts +90 -19
  74. package/scripts/generate-code-map.ts +53 -12
  75. package/scripts/scaffold-project.ts +31 -4
  76. package/scripts/split-spec.ts +2 -1
  77. package/scripts/sync-sc-status.ts +57 -5
  78. package/scripts/sync-spec-tests.ts +2 -1
  79. package/specs/SPEC-TEMPLATE.md +200 -0
  80. package/templates/SUCCESS-TEMPLATE.md +72 -0
  81. package/templates/agent-briefs/_shared.md +9 -0
  82. package/templates/agent-briefs/aditi.md +46 -0
  83. package/templates/agent-briefs/discovery.md +37 -0
  84. package/templates/agent-briefs/marcus.md +39 -0
  85. package/templates/agent-briefs/quinn.md +67 -0
  86. package/templates/agent-briefs/rook.md +40 -0
  87. package/templates/agent-briefs/serena.md +39 -0
@@ -48,7 +48,7 @@
48
48
  */
49
49
 
50
50
  import { spawnSync } from "child_process";
51
- import { mkdtempSync, rmSync, lstatSync, readdirSync, readlinkSync, realpathSync } from "fs";
51
+ import { chmodSync, mkdirSync, mkdtempSync, rmSync, lstatSync, readdirSync, readlinkSync, realpathSync, statSync } from "fs";
52
52
  import { tmpdir } from "os";
53
53
  import { join, resolve, sep } from "path";
54
54
 
@@ -82,6 +82,18 @@ export interface Rehearsal {
82
82
  parent: string;
83
83
  /** `clone` when copy-on-write was used, `copy` when it fell back. */
84
84
  method: "clone" | "copy";
85
+ /**
86
+ * The root the operator actually named, kept so the notice can say what it
87
+ * was SUBSTITUTED for (#481).
88
+ *
89
+ * `dir` always comes from `mkdtemp` and is therefore always absolute. So the
90
+ * audit takes the absolute code path no matter what the operator typed, and
91
+ * `bunx rungate scaffold .` was rehearsed as an absolute invocation, passed,
92
+ * and then threw in the real run on every repo-root owned file. The
93
+ * rehearsal was not wrong about the steps; it was silent about the input it
94
+ * had replaced.
95
+ */
96
+ source: string;
85
97
  }
86
98
 
87
99
  /**
@@ -177,8 +189,16 @@ export function cloneForRehearsal(root: string): Rehearsal {
177
189
  const parent = mkdtempSync(join(tmpdir(), "rungate-rehearsal-"));
178
190
  const dir = join(parent, "tree");
179
191
  const fail = (why: string): never => {
180
- rmSync(parent, { recursive: true, force: true });
181
- throw new Error(`audit did NOT run: ${why}`);
192
+ // Through `removeTree`: what a failed copy leaves behind is whatever
193
+ // made it fail, and a bare `rmSync` here throws that errno in place of
194
+ // the refusal below — so the caller is told the audit crashed rather
195
+ // than why it declined. A clone we cannot clean up is still a decline,
196
+ // so a leftover is appended to the sentence rather than replacing it.
197
+ const leaked = removeTree(parent);
198
+ throw new Error(
199
+ `audit did NOT run: ${why}` +
200
+ (leaked === null ? "" : ` (and the clone is still on disk: ${parent} — ${leaked})`),
201
+ );
182
202
  };
183
203
 
184
204
  // `-c` asks for an APFS clonefile. It fails on filesystems that cannot do
@@ -194,29 +214,13 @@ export function cloneForRehearsal(root: string): Rehearsal {
194
214
  // on, so no test could ever reach the second branch. One shared helper
195
215
  // removes the ordering rather than testing it.
196
216
  let method: Rehearsal["method"] = "clone";
197
- if (attemptCopy(["-Rc", root, dir], 300_000) !== 0) {
198
- if (attemptCopy(["-R", root, dir], 600_000) !== 0) {
217
+ if (attemptCopy(root, dir, "-Rc", 300_000) !== 0) {
218
+ if (attemptCopy(root, dir, "-R", 600_000) !== 0) {
199
219
  return fail("the tree could not be copied");
200
220
  }
201
221
  method = "copy";
202
222
  }
203
223
 
204
- // Pruning happens through `rmSync(..., { recursive: true })`, which
205
- // follows a symlinked PARENT. `.claude/worktrees` in a tree where
206
- // `.claude -> /real/.claude` therefore resolves outside the clone and
207
- // deletes the consumer's real worktrees — a destructive path traversal
208
- // performed by the mode that promises to write nothing. Found by the
209
- // commit security review.
210
- //
211
- // So nothing is deleted through a symlink. Each excluded path is walked
212
- // component by component; the moment a component is a link, the link
213
- // itself is unlinked (never followed) and the walk stops. `node_modules`
214
- // being a symlink is the common case — the pre-push hook makes one — and
215
- // must stay cheap rather than refusing the audit.
216
- for (const rel of REHEARSAL_EXCLUDES) {
217
- prunePathWithoutFollowingLinks(dir, rel);
218
- }
219
-
220
224
  const missing = missingTopLevel(root, dir);
221
225
  if (missing.length > 0) {
222
226
  return fail(`the copy is incomplete — ${missing.length} top-level entr(ies) missing: ${missing.slice(0, 5).join(", ")}`);
@@ -232,19 +236,161 @@ export function cloneForRehearsal(root: string): Rehearsal {
232
236
  );
233
237
  }
234
238
 
235
- return { dir, parent, method };
239
+ return { dir, parent, method, source: root };
236
240
  }
237
241
 
238
242
  /**
239
- * Copy into `dir`, having first removed whatever is there.
243
+ * One `cp` invocation. Injectable so a test can record what was copied
244
+ * rather than infer it from the result — "this path is absent from the
245
+ * clone" is true under BOTH orderings, so absence cannot witness the fix.
246
+ */
247
+ export type CopyRunner = (args: string[], timeout: number) => number;
248
+
249
+ const realCp: CopyRunner = (args, timeout) =>
250
+ spawnSync("cp", args, { encoding: "utf-8", timeout }).status ?? 1;
251
+
252
+ /**
253
+ * Copy `src` to `dst`, never reading an excluded path (#549).
254
+ *
255
+ * The exclusion used to be applied AFTER the clone: `cp -R` copied the whole
256
+ * tree and `prunePathWithoutFollowingLinks` then deleted
257
+ * `.claude/worktrees`. On this repository that directory is **737 MB across
258
+ * 104 worktrees**, and the recursive `rm` died with `EFAULT: bad address in
259
+ * system call argument`. So `bunx rungate scaffold .` could not run on
260
+ * rungate — the harness could not scaffold itself, which is SUCCESS.md
261
+ * claim 5 pointed inward. The exclude list was right the whole time; the
262
+ * ORDERING defeated it, and a consumer with a merely LARGE gitignored
263
+ * directory got a preview that silently spent minutes copying files it was
264
+ * about to delete.
265
+ *
266
+ * ## How the level walk works, and why it is not a full recursive copy
267
+ *
268
+ * Only directories that are an ANCESTOR of an exclude are walked. Everything
269
+ * else is handed to `cp` whole, so the APFS clonefile still applies to the
270
+ * subtree and the cost stays bounded by metadata. On this repo that is two
271
+ * walked directories — the root and `.claude` — and one `cp` per top-level
272
+ * entry.
273
+ *
274
+ * ## Symlinks keep the answers `prunePathWithoutFollowingLinks` worked out
275
+ *
276
+ * They were paid for by a commit security review and the reasons have not
277
+ * changed; what changed is that they are now decisions about what to COPY
278
+ * rather than about what to delete, which is strictly safer because a
279
+ * decision not to copy cannot escape the clone.
280
+ *
281
+ * - **An excluded path that is itself a link** (`node_modules -> /elsewhere`,
282
+ * which the pre-push hook creates): skipped. Previously it was copied and
283
+ * then unlinked; now it is never read. Same clone, one fewer traversal.
284
+ * - **A link that is an ANCESTOR of an exclude** (`.claude -> /real/.claude`
285
+ * while excluding `.claude/worktrees`): copied AS A LINK and not descended
286
+ * into. Descending would read the consumer's real tree, and deleting
287
+ * through it is what the old code's `rmSync(..., { recursive: true })`
288
+ * would have done. The link then reaches `escapingSymlinks`, which refuses
289
+ * the audit — which is what an ancestor pointing out of the clone is for.
290
+ *
291
+ * Returns the first non-zero `cp` status, or 0.
292
+ */
293
+ /**
294
+ * Remove a tree we own, clearing what makes a removal refuse first.
295
+ * Returns `null` when the path is gone, or the reason it is not.
240
296
  *
241
- * The clear is unconditional so there is one code path rather than two with
242
- * an ordering between them. See `cloneForRehearsal` for why.
297
+ * Two things stop `rm` on a tree this module created, and neither is a
298
+ * permission the owner actually lacks:
299
+ *
300
+ * - **BSD file flags.** `cp -Rc` is a clonefile and a clonefile carries
301
+ * flags, so a clone of this repo holds the `uchg` files it sets on
302
+ * `gates/prompts/*.md`. `rm` answers "Operation not permitted" and bun
303
+ * reports it as `EFAULT: bad address in system call argument`, naming
304
+ * neither the file nor the flag. That is #549's error message.
305
+ * - **A directory with no owner-read or owner-execute bit.** An unreadable
306
+ * directory is one `cp` cannot copy, so it is exactly what a failed first
307
+ * attempt leaves behind — and `rmSync` cannot list it to empty it. The
308
+ * owner can always chmod it; `rm -rf` never does.
309
+ *
310
+ * Both are best-effort: `chflags` does not exist on Linux, where there are
311
+ * no BSD flags either, and a failing `chmod` is a tree we do not own, which
312
+ * the removal below will report in its own words.
243
313
  */
244
- function attemptCopy(args: string[], timeout: number): number {
245
- const dest = args[args.length - 1]!;
246
- rmSync(dest, { recursive: true, force: true });
247
- return spawnSync("cp", args, { encoding: "utf-8", timeout }).status ?? 1;
314
+ function removeTree(path: string): string | null {
315
+ spawnSync("chflags", ["-R", "nouchg,noschg", path], { timeout: 60_000 });
316
+ spawnSync("chmod", ["-R", "u+rwX", path], { timeout: 60_000 });
317
+ try {
318
+ rmSync(path, { recursive: true, force: true });
319
+ return null;
320
+ } catch (err) {
321
+ if (spawnSync("rm", ["-rf", path], { encoding: "utf-8", timeout: 120_000 }).status === 0) {
322
+ return null;
323
+ }
324
+ return (err as Error).message;
325
+ }
326
+ }
327
+
328
+ export function copyTreeExcluding(
329
+ src: string,
330
+ dst: string,
331
+ excludes: readonly string[],
332
+ cpFlag: string,
333
+ timeout: number,
334
+ run: CopyRunner = realCp,
335
+ ): number {
336
+ // Unconditional, so there is one code path rather than two with an
337
+ // ordering between them: a failed `cp -Rc` may have copied part of the
338
+ // tree, and falling back on top of that leaves a PARTIAL clone whose
339
+ // steps report less than `--fix` will do.
340
+ //
341
+ // Through `removeTree`, not a bare `rmSync`. What the first attempt left
342
+ // behind is whatever made it fail — on Linux CI an unreadable directory
343
+ // `cp -R` could not copy and `rmSync` then cannot remove — and a throw
344
+ // here escapes `cloneForRehearsal` as a raw EACCES instead of its refusal
345
+ // sentence. That is the same defect as the one in `discardRehearsal`
346
+ // below, one function earlier.
347
+ if (removeTree(dst) !== null) return 1;
348
+ const paths = excludes.map(e => e.split("/").filter(Boolean)).filter(p => p.length > 0);
349
+ return copyLevel(src, dst, paths, cpFlag, timeout, run);
350
+ }
351
+
352
+ function copyLevel(
353
+ src: string,
354
+ dst: string,
355
+ excludes: string[][],
356
+ cpFlag: string,
357
+ timeout: number,
358
+ run: CopyRunner,
359
+ ): number {
360
+ mkdirSync(dst, { recursive: true });
361
+ // The mode of a directory we created ourselves rather than let `cp`
362
+ // create. Without this an 0o700 `.claude` becomes 0o755 in the clone, and
363
+ // a step that reads permissions would see a tree that is not the
364
+ // consumer's.
365
+ try { chmodSync(dst, statSync(src).mode & 0o7777); } catch { /* best effort */ }
366
+
367
+ let entries;
368
+ try {
369
+ entries = readdirSync(src, { withFileTypes: true });
370
+ } catch {
371
+ return 1;
372
+ }
373
+
374
+ for (const e of entries) {
375
+ if (excludes.some(p => p.length === 1 && p[0] === e.name)) continue;
376
+ const deeper = excludes.filter(p => p.length > 1 && p[0] === e.name).map(p => p.slice(1));
377
+ // `isDirectory()` is false for a symlink, which is the behaviour this
378
+ // relies on: an ancestor link falls through to the `cp` below and is
379
+ // copied as a link rather than walked.
380
+ if (deeper.length > 0 && e.isDirectory()) {
381
+ const st = copyLevel(join(src, e.name), join(dst, e.name), deeper, cpFlag, timeout, run);
382
+ if (st !== 0) return st;
383
+ continue;
384
+ }
385
+ const st = run([cpFlag, join(src, e.name), join(dst, e.name)], timeout);
386
+ if (st !== 0) return st;
387
+ }
388
+ return 0;
389
+ }
390
+
391
+ /** Copy into `dir`, excluding what the clone does not want. */
392
+ function attemptCopy(root: string, dir: string, cpFlag: string, timeout: number): number {
393
+ return copyTreeExcluding(root, dir, REHEARSAL_EXCLUDES, cpFlag, timeout);
248
394
  }
249
395
 
250
396
  /**
@@ -269,62 +415,75 @@ export function missingTopLevel(root: string, dir: string): string[] {
269
415
  }
270
416
 
271
417
  /**
272
- * Remove `root/rel`, never passing through a symlink on the way.
273
- *
274
- * Two different answers, depending on WHERE the link is:
275
- *
276
- * - **The link is the path itself** (`node_modules -> /elsewhere/mods`):
277
- * unlink it. `rmSync` on a link removes the link and leaves the target
278
- * alone, and an excluded directory is one we did not want in the clone
279
- * anyway. This is the common case — the pre-push hook makes exactly this
280
- * link — and it has to stay cheap rather than refusing the audit.
281
- *
282
- * - **The link is an ANCESTOR** (`.claude -> /real/.claude`, pruning
283
- * `.claude/worktrees`): leave it entirely alone. Unlinking it would throw
284
- * away `.claude` — rules, agents, config — and the steps would then run
285
- * over a tree missing the directory half of them read, reporting less than
286
- * `--fix` will do. That is #398 itself, reintroduced by its own fix. It is
287
- * also invisible to the completeness check, which cannot ask for `.claude`
288
- * back while `.claude/worktrees` is on the exclude list.
289
- *
290
- * Deleting through it is worse still: `rmSync(..., { recursive: true })`
291
- * follows a symlinked parent, so the consumer's real worktrees go. So
292
- * neither — the link stays, and `escapingSymlinks` refuses the audit a
293
- * moment later because an ancestor pointing out of the clone is exactly
294
- * what it is for. Found by the commit security review.
418
+ * Throw the clone away. Safe to call twice. Returns why it could not, or
419
+ * `null`.
420
+ *
421
+ * ## The clone can contain files that refuse to be deleted (#549)
422
+ *
423
+ * `cp -Rc` is a clonefile, and a clonefile carries the source's BSD FILE
424
+ * FLAGS. This repository sets `uchg` on `gates/prompts/*.md` — that is the
425
+ * prompt-immutability feature working as designed — so the clone contains
426
+ * immutable files and `rm` refuses them:
427
+ *
428
+ * ```
429
+ * rm: .../tree/gates/prompts/evidence-validator.md: Operation not permitted
430
+ * ```
431
+ *
432
+ * bun reports that as `EFAULT: bad address in system call argument, rm`,
433
+ * which names neither the file nor the flag, and the THROW took down a
434
+ * scaffold run whose report had already printed. That is the whole of #549's
435
+ * symptom: `bunx rungate scaffold .` could not complete on rungate itself.
436
+ * The 737 MB `.claude/worktrees` directory the issue identified made it
437
+ * slower and held most of the flagged copies, and excluding it from the copy
438
+ * is right and necessary — but the thing `rm` was actually refusing was a
439
+ * flag, not a size.
440
+ *
441
+ * So the flags are cleared in the CLONE before it is removed. Only in the
442
+ * clone: `r.parent` always comes from `mkdtemp`, the consumer's own files
443
+ * are never touched, and the flags are deliberately left intact DURING the
444
+ * rehearsal so the steps see the tree the consumer has rather than a more
445
+ * permissive one.
446
+ *
447
+ * ## A cleanup failure is reported, not thrown
448
+ *
449
+ * The report is already printed by the time this runs. Turning a leaked temp
450
+ * directory into a non-zero exit loses the audit; saying nothing loses the
451
+ * leak, which is #437. So it returns the sentence and the caller prints it.
295
452
  */
296
- function prunePathWithoutFollowingLinks(root: string, rel: string): void {
297
- let current = root;
298
- const parts = rel.split("/").filter(Boolean);
299
- for (let i = 0; i < parts.length; i++) {
300
- current = join(current, parts[i]!);
301
- let st;
302
- try { st = lstatSync(current); } catch { return; } // absent: nothing to prune
303
- const last = i === parts.length - 1;
304
- if (st.isSymbolicLink()) {
305
- if (last) rmSync(current, { force: true });
306
- return; // an ancestor link: prune nothing, and let the refusal catch it
307
- }
308
- if (!last && !st.isDirectory()) return;
309
- }
310
- rmSync(current, { recursive: true, force: true });
311
- }
312
-
313
- /** Throw the clone away. Safe to call twice. */
314
- export function discardRehearsal(r: Rehearsal): void {
453
+ export function discardRehearsal(r: Rehearsal): string | null {
315
454
  // `r.parent`, not `join(r.dir, "..")`. Deriving a deletion target by
316
455
  // walking up from a path is how a cleanup ends up removing something it
317
456
  // does not own; the owner is recorded at creation instead. Leaving the
318
457
  // mkdtemp behind is the leak #437 is about, so it is the parent that goes.
319
- rmSync(r.parent, { recursive: true, force: true });
458
+ //
459
+ // One binding with the copy's own cleanup, so the flag-clearing and the
460
+ // mode-clearing cannot drift apart between the two places that delete a
461
+ // tree this module made.
462
+ const why = removeTree(r.parent);
463
+ if (why === null) return null;
464
+ return `the rehearsal clone could not be removed and is still on disk: ${r.parent} — ` +
465
+ `${why}. Remove it by hand; nothing in the consumer's tree was touched.`;
320
466
  }
321
467
 
322
468
  /**
323
469
  * The line audit prints so a reader knows the report was produced by running
324
470
  * the real thing, not by predicting it — and knows what it did not run.
325
471
  */
472
+ /**
473
+ * What the rehearsal did, and — the half #481 was missing — what it did NOT.
474
+ *
475
+ * A rehearsal substitutes one input by construction: the root. Steps run
476
+ * against `r.dir`, which `mkdtemp` makes absolute, so nothing that depends on
477
+ * the FORM of the operator's argument is exercised. That is not a bug to fix;
478
+ * a rehearsal has to run somewhere else or it is not a rehearsal. It is a
479
+ * thing the notice has to SAY, because an unrehearsed input reported as
480
+ * rehearsed is `UNMEASURED is not SKIP` wearing a dry run's clothes: the
481
+ * audit printed `CREATED: project-state.json` for a write that threw.
482
+ */
326
483
  export function rehearsalNotice(r: Rehearsal): string {
327
484
  const skipped = [...STEPS_NOT_REHEARSED.keys()].join(", ");
328
485
  return `Mode: audit — rehearsed against a ${r.method} of the tree, which was then discarded. ` +
329
- `Not rehearsed: ${skipped}. Pass --fix to apply.`;
486
+ `Not rehearsed: ${skipped}. ` +
487
+ `Root SUBSTITUTED: the steps ran against ${r.dir}, not ${r.source} — anything that depends ` +
488
+ `on the form of that argument is UNMEASURED by this audit. Pass --fix to apply.`;
330
489
  }