rungate 0.6.0 → 0.8.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 (164) hide show
  1. package/hooks/ActionVerificationGate.hook.ts +96 -0
  2. package/hooks/AgentBriefGuard.hook.ts +38 -0
  3. package/hooks/AgentVerdictCapture.hook.ts +87 -0
  4. package/hooks/AutoVerifyGate.hook.ts +79 -0
  5. package/hooks/BashToolGuard.hook.ts +65 -0
  6. package/hooks/BashWriteGuard.hook.ts +60 -0
  7. package/hooks/CommitEnforcement.hook.ts +130 -0
  8. package/hooks/DocHygiene.hook.ts +148 -0
  9. package/hooks/GateEnforcement.hook.ts +58 -0
  10. package/hooks/GateResultProtection.hook.ts +69 -0
  11. package/hooks/HarnessAFKGate.hook.ts +85 -0
  12. package/hooks/HookCurrency.hook.ts +99 -0
  13. package/hooks/IssueCloseGuard.hook.ts +149 -0
  14. package/hooks/MergeGuard.hook.ts +58 -0
  15. package/hooks/PostCompact.hook.ts +60 -0
  16. package/hooks/ProjectStateSync.hook.ts +103 -0
  17. package/hooks/SkillGuard.hook.ts +69 -0
  18. package/hooks/SpecConformityTrigger.hook.ts +43 -0
  19. package/hooks/SpecSCGuard.hook.ts +123 -0
  20. package/hooks/StaleTTLCleanup.hook.ts +141 -0
  21. package/hooks/TaskCompleted.hook.ts +51 -0
  22. package/hooks/TeamInit.hook.ts +85 -0
  23. package/hooks/TestSuiteGuard.hook.ts +70 -0
  24. package/hooks/TestSuiteRelease.hook.ts +85 -0
  25. package/hooks/TopLevelCdGuard.hook.ts +108 -0
  26. package/hooks/VerifyPhaseLock.hook.ts +84 -0
  27. package/hooks/WorkflowStateGuard.hook.ts +40 -0
  28. package/hooks/lib/agent-config.json +9 -0
  29. package/hooks/lib/agentDetection.ts +201 -0
  30. package/hooks/lib/comment-template.ts +86 -0
  31. package/hooks/lib/findWorkflow.ts +101 -0
  32. package/hooks/lib/parseStdin.ts +37 -0
  33. package/hooks/lib/paths.ts +111 -0
  34. package/hooks/lib/utils.ts +338 -0
  35. package/lib/affected-tests.ts +427 -0
  36. package/lib/agent-audit.ts +2 -3
  37. package/lib/agent-worktree.ts +287 -0
  38. package/lib/agents-md-sections.ts +81 -16
  39. package/lib/bash-write-guard.ts +373 -0
  40. package/lib/behavioral-cache.ts +150 -2
  41. package/lib/branch-cleanup.ts +279 -7
  42. package/lib/branch-protection.ts +790 -0
  43. package/lib/brief-ac-alignment.ts +104 -0
  44. package/lib/brief-references.ts +205 -0
  45. package/lib/bun-version.ts +93 -14
  46. package/lib/check-remedy.ts +140 -0
  47. package/lib/ci-checkout.ts +17 -0
  48. package/lib/ci-history-depth.ts +159 -0
  49. package/lib/ci-setup-step.ts +96 -0
  50. package/lib/claude-code-action.ts +85 -0
  51. package/lib/claude-tool-vocabulary.ts +188 -0
  52. package/lib/cli-commands.ts +15 -0
  53. package/lib/commit-sha.ts +42 -0
  54. package/lib/commit-subject.ts +90 -0
  55. package/lib/compliance-report.ts +377 -45
  56. package/lib/compliance.ts +191 -17
  57. package/lib/config-loader.ts +39 -1
  58. package/lib/config-trackable.ts +400 -0
  59. package/lib/conformity.ts +1087 -100
  60. package/lib/containment.ts +238 -0
  61. package/lib/content-hash.ts +24 -0
  62. package/lib/create-sc.ts +249 -16
  63. package/lib/dev-liveness.ts +180 -0
  64. package/lib/discovery-branch-points.ts +432 -0
  65. package/lib/discovery-stability.ts +481 -0
  66. package/lib/distribution-currency.ts +236 -0
  67. package/lib/doc-routing.ts +157 -0
  68. package/lib/evidence-prevalidator.ts +224 -2
  69. package/lib/evidence-satisfiability.ts +534 -12
  70. package/lib/failure-intake.ts +323 -0
  71. package/lib/fallow-availability.ts +157 -0
  72. package/lib/fs-write-capabilities.ts +232 -0
  73. package/lib/gate-enforcement.ts +222 -28
  74. package/lib/gate-names.ts +72 -0
  75. package/lib/gate-result-protection.ts +427 -0
  76. package/lib/gate-run-split.ts +147 -0
  77. package/lib/generators/agents-md.ts +140 -32
  78. package/lib/git-isolation.ts +140 -0
  79. package/lib/github.ts +15 -0
  80. package/lib/google-auth-action.ts +93 -0
  81. package/lib/harness-project.ts +48 -0
  82. package/lib/harness-provenance.ts +255 -0
  83. package/lib/hook-evidence.ts +235 -0
  84. package/lib/hook-scopes.ts +386 -0
  85. package/lib/hook-target-tree.ts +398 -0
  86. package/lib/hook-trees.ts +447 -0
  87. package/lib/intake-sources.ts +380 -0
  88. package/lib/js-top-level-code.ts +78 -10
  89. package/lib/junit-checks.ts +52 -0
  90. package/lib/late-env-spawn.ts +331 -0
  91. package/lib/launch-base.ts +376 -0
  92. package/lib/line-citation.ts +372 -0
  93. package/lib/orphan-reaper.ts +468 -0
  94. package/lib/parity-binding.ts +205 -0
  95. package/lib/paths.ts +96 -3
  96. package/lib/per-test-timeouts.ts +219 -0
  97. package/lib/phase2-decomposition.ts +1 -1
  98. package/lib/plan-coverage.ts +151 -0
  99. package/lib/prior-branch.ts +201 -8
  100. package/lib/process-table.ts +146 -0
  101. package/lib/project-state-derivation.ts +219 -0
  102. package/lib/project-state-root.ts +258 -0
  103. package/lib/published-artifact.ts +669 -0
  104. package/lib/readdir-sorted.ts +45 -0
  105. package/lib/review-execution-reading.ts +206 -0
  106. package/lib/review-tool-agreement.ts +433 -0
  107. package/lib/rule-promotion.ts +234 -0
  108. package/lib/rule-retirement.ts +525 -0
  109. package/lib/run-branch.ts +612 -0
  110. package/lib/rungate-schema.ts +19 -0
  111. package/lib/runner-home.ts +274 -0
  112. package/lib/sc-id-cross-tree.ts +103 -15
  113. package/lib/sc-ids.ts +121 -13
  114. package/lib/sc-reservations.ts +289 -0
  115. package/lib/scaffold/ai-review-findings.ts +409 -0
  116. package/lib/scaffold/ai-review-step.ts +5406 -64
  117. package/lib/scaffold/defaults.ts +82 -1
  118. package/lib/scaffold/managed-workflow.ts +587 -192
  119. package/lib/scaffold/owned-files.ts +152 -45
  120. package/lib/scaffold/pre-push-hook.ts +49 -5
  121. package/lib/scaffold/steps.ts +1836 -275
  122. package/lib/scaffold/workflow-census.ts +217 -0
  123. package/lib/scaffold/workflow-drift.ts +217 -0
  124. package/lib/scaffold/write-steps.ts +4 -0
  125. package/lib/scan-budget.ts +247 -0
  126. package/lib/scanner.ts +43 -51
  127. package/lib/scratch-roots.ts +50 -5
  128. package/lib/screenshot-discovery.ts +121 -0
  129. package/lib/screenshot-upload.ts +41 -1
  130. package/lib/secret-patterns.ts +511 -8
  131. package/lib/security-verdict.ts +1 -1
  132. package/lib/shell-path-interpolation.ts +594 -0
  133. package/lib/ship-mode.ts +219 -0
  134. package/lib/source-slice.ts +294 -0
  135. package/lib/stale-cleanup.ts +37 -3
  136. package/lib/suite-flake.ts +278 -0
  137. package/lib/suite-measurement.ts +23 -4
  138. package/lib/suite-run-reading.ts +241 -1
  139. package/lib/suite-timeout-margin.ts +193 -0
  140. package/lib/test-case-bodies.ts +883 -0
  141. package/lib/test-case-deletion.ts +653 -0
  142. package/lib/test-census.ts +97 -29
  143. package/lib/test-command-positions.ts +274 -0
  144. package/lib/test-suite-lock.ts +159 -16
  145. package/lib/text-dependents.ts +875 -0
  146. package/lib/tool-probe.ts +31 -9
  147. package/lib/top-level-cd.ts +393 -0
  148. package/lib/tree-entries.ts +89 -0
  149. package/lib/ts-ast.ts +200 -0
  150. package/lib/walk-test-files.ts +97 -0
  151. package/lib/workflow-actions.ts +263 -0
  152. package/lib/workflow-security.ts +29 -0
  153. package/lib/workflow-taint.ts +732 -0
  154. package/lib/worktree-cleanup.ts +65 -38
  155. package/lib/worktree-collect.ts +177 -5
  156. package/lib/worktree-repo.ts +68 -3
  157. package/package.json +4 -1
  158. package/scripts/ai-review-findings.ts +45 -0
  159. package/scripts/check-installed-hook-currency.ts +825 -0
  160. package/scripts/create-sc.ts +21 -1
  161. package/scripts/extract-constraints.ts +107 -7
  162. package/scripts/generate-code-map.ts +23 -30
  163. package/scripts/scaffold-project.ts +8 -1
  164. package/scripts/sync-sc-status.ts +77 -16
@@ -52,8 +52,14 @@
52
52
  */
53
53
 
54
54
  import { createHash } from "crypto";
55
+ import { PROJECT_STATE_OWNERSHIP } from "../project-state-derivation";
55
56
 
56
- import { preserveSections } from "../agents-md-sections";
57
+ import {
58
+ BRIEF_CONSTRAINTS_SECTION,
59
+ HARD_CONSTRAINTS_SECTION,
60
+ preserveSections,
61
+ type SectionSpec,
62
+ } from "../agents-md-sections";
57
63
 
58
64
  /** How the generator writes one owned file. */
59
65
  export type WriteStrategy =
@@ -76,6 +82,32 @@ export interface OwnedFile {
76
82
  customize: string;
77
83
  /** The thing not to do. */
78
84
  avoid: string;
85
+ /**
86
+ * Which section carries forward. REQUIRED on a `section-preserving` row,
87
+ * and asserted as such by `test/scaffold-ownership.test.ts` — a row that
88
+ * declared the strategy and no section would silently merge against
89
+ * AGENTS.md's heading, which is *Silent Default Substitution* in the one
90
+ * place the declaration is supposed to be the whole answer.
91
+ */
92
+ sections?: SectionSpec;
93
+ /**
94
+ * The binding that implements this row's strategy, when it is NOT
95
+ * `decideOwnedWrite` (#759).
96
+ *
97
+ * `decideOwnedWrite` is the one decider for files written through
98
+ * `regenerateOwned`, and for a while the table read as though it were the
99
+ * one decider for EVERY owned file. It is not: `.claude/rules/*.md` go
100
+ * through `decideRuleWrite` because a markdown file has no `//` comments,
101
+ * and the two workflow rows go through `writeManagedFile` because YAML
102
+ * jobs merge rather than replace.
103
+ *
104
+ * A row that names no `servedBy` is claiming `decideOwnedWrite` handles
105
+ * it, and `test/owned-strategy-reachability.test.ts` refuses that claim
106
+ * when the function has no branch for the strategy. Unset is therefore a
107
+ * claim, not an absence — which is the direction this has to be wrong in,
108
+ * because the silent version is what #759 is about.
109
+ */
110
+ servedBy?: string;
79
111
  }
80
112
 
81
113
  /**
@@ -94,28 +126,53 @@ export const OWNED_FILES: OwnedFile[] = [
94
126
  strategy: "section-preserving",
95
127
  customize: "Everything but `## Hard Constraints` is generated from the scan and `.claude/rungate/`; put non-inferable project rules in that section and they carry forward",
96
128
  avoid: "Don't hand-edit the generated sections — a re-scaffold rewrites them without reporting it",
129
+ sections: HARD_CONSTRAINTS_SECTION,
97
130
  },
98
131
  {
99
132
  path: ".github/workflows/ci.yml",
100
133
  strategy: "job-preserving",
134
+ servedBy: "writeManagedFile (lib/scaffold/steps.ts) via mergeManagedWorkflow — the harness owns named jobs, not the file",
101
135
  customize: "Set `ci` fields in `.claude/rungate/config.json`; add your own jobs alongside the generated `test` job",
102
136
  avoid: "Don't edit the generated `test` job",
103
137
  },
104
138
  {
105
139
  path: ".github/workflows/gates.yml",
106
140
  strategy: "job-preserving",
141
+ servedBy: "writeManagedFile (lib/scaffold/steps.ts) via mergeManagedWorkflow — the harness owns named jobs, not the file",
107
142
  customize: "Settings from `.claude/rungate/config.json`; add your own jobs alongside the generated `gates` job",
108
143
  avoid: "Don't edit the generated `gates` job",
109
144
  },
110
145
  {
146
+ // Absent from this list, and from the prose table, for the whole life of
147
+ // the file (#630 through #631). `createCiWorkflows` has written it since
148
+ // #630 — through the same `writeManagedFile` its two siblings above use
149
+ // — so a consumer's hand edit to the `ai-review` job was overwritten on
150
+ // every scaffold and reported as nothing, while the two files beside it
151
+ // reported the overwrite by name. The ownership did not travel with the
152
+ // bytes, which is #722's property met from the other side.
153
+ path: ".github/workflows/ai-review.yml",
154
+ strategy: "job-preserving",
155
+ servedBy: "writeManagedFile (lib/scaffold/steps.ts) via mergeManagedWorkflow — the harness owns named jobs, not the file",
156
+ customize: "Set `ci.vertex` in `.claude/rungate/config.json` to authenticate on Vertex with Workload Identity Federation; add your own jobs alongside the generated `ai-review` job",
157
+ avoid: "Don't edit the generated `ai-review` job",
158
+ },
159
+ {
160
+ // `regenerate` until #655, and the declaration was never consulted:
161
+ // `generateAgentBriefsStep` called `writeFileSync` directly, so the row
162
+ // described behaviour that a bare overwrite happened to coincide with.
163
+ // The cost was that a rule mined from a project's own compliance history
164
+ // had nowhere in that project to live — every brief surface was rebuilt
165
+ // from harness-wide templates on the next scaffold.
111
166
  path: ".claude/agents/*.md",
112
- strategy: "regenerate",
113
- customize: "Settings from `.claude/rungate/roles.json`",
114
- avoid: "Don't edit briefs",
167
+ strategy: "section-preserving",
168
+ customize: "Everything but `## Project Constraints` is generated from `.claude/rungate/roles.json` and the templates; put rules mined from this repo's own history in that section and they carry forward",
169
+ avoid: "Don't hand-edit the generated sections — a re-scaffold rewrites them without reporting it",
170
+ sections: BRIEF_CONSTRAINTS_SECTION,
115
171
  },
116
172
  {
117
173
  path: ".claude/rules/*.md",
118
174
  strategy: "provenance",
175
+ servedBy: "decideRuleWrite (lib/rule-provenance.ts) — a frontmatter `generated-sha:` key, because a markdown file has no line comments",
119
176
  customize: "Edit freely — a hand-edited rule is preserved and reported (#370)",
120
177
  avoid: "Don't expect edits to survive if you also delete the `generated-sha` marker",
121
178
  },
@@ -140,7 +197,10 @@ export const OWNED_FILES: OwnedFile[] = [
140
197
  {
141
198
  path: "PROJECT-STATE.md",
142
199
  strategy: "create-only",
143
- customize: "DA-owned (#296) — generated from project-state.json by the DA",
200
+ // #781: whether this project GENERATES the file is a reading, not a
201
+ // constant — see lib/project-state-derivation.ts. This table has no
202
+ // root to scan, so it states only the part that is true everywhere.
203
+ customize: `${PROJECT_STATE_OWNERSHIP} — maintained by the DA; whether this project derives it from the state JSON is reported per project in .claude/rules/key-files.md`,
144
204
  avoid: "Don't edit directly; don't expect re-scaffold to update it",
145
205
  },
146
206
  {
@@ -151,6 +211,24 @@ export const OWNED_FILES: OwnedFile[] = [
151
211
  },
152
212
  ];
153
213
 
214
+ /**
215
+ * The declared row for a label, or null.
216
+ *
217
+ * One matcher. `ownedStrategy` and `ownedSections` are two questions about
218
+ * one row, and a second copy of the glob rule would be a second answer to
219
+ * "which row is this" that nothing compares against the first.
220
+ */
221
+ export function ownedFileFor(label: string): OwnedFile | null {
222
+ for (const owned of OWNED_FILES) {
223
+ if (owned.path === label) return owned;
224
+ if (owned.path.includes("*")) {
225
+ const [prefix, suffix] = owned.path.split("*");
226
+ if (label.startsWith(prefix) && label.endsWith(suffix)) return owned;
227
+ }
228
+ }
229
+ return null;
230
+ }
231
+
154
232
  /**
155
233
  * The markdown table for `.claude/rules/harness-managed.md`, generated.
156
234
  *
@@ -191,33 +269,38 @@ export function bytesIdentical(existing: string | null, generated: string): bool
191
269
  }
192
270
 
193
271
  /**
194
- * The provenance marker for a file whose comment syntax is `//`.
272
+ * The provenance marker for one line-comment syntax.
195
273
  *
196
- * The frontmatter form in `lib/rule-provenance.ts` is the same idea and
197
- * cannot be reused verbatim: a TypeScript file has no `---` block. Both
198
- * stamp a digest of the body the generator wrote, so the next run can ask
199
- * "has anyone touched this since?" with nothing that can desync from the
200
- * tree it describes.
274
+ * A factory rather than a second copy, because `.github/workflows/*.yml`
275
+ * needs the same marker with `#` and the digest logic is the part that must
276
+ * not fork (#722). Two independent implementations of "has anyone touched
277
+ * this since" are two deciders answering one question, and a mutation of
278
+ * either leaves the other holding the case up
279
+ * (`.claude/rules/checks-must-be-able-to-fail.md`, fourth shape).
201
280
  */
202
- const MARKER = /^\/\/ generated-sha: ([0-9a-f]{16})\n/;
203
-
204
- export function commentBody(text: string): string {
205
- return text.replace(MARKER, "");
281
+ export function lineCommentProvenance(prefix: string) {
282
+ const marker = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\\/]/g, "\\$&")} generated-sha: ([0-9a-f]{16})\\n`);
283
+ const body = (text: string): string => text.replace(marker, "");
284
+ const sha = (text: string): string =>
285
+ createHash("sha256").update(body(text)).digest("hex").slice(0, 16);
286
+ return {
287
+ body,
288
+ sha,
289
+ declared: (text: string): string | null => {
290
+ const m = marker.exec(text);
291
+ return m ? m[1] : null;
292
+ },
293
+ /** Idempotent: stamping an already-stamped document re-stamps the body. */
294
+ stamp: (generated: string): string => {
295
+ const b = body(generated);
296
+ return `${prefix} generated-sha: ${sha(b)}\n${b}`;
297
+ },
298
+ };
206
299
  }
207
300
 
208
- export function commentSha(text: string): string {
209
- return createHash("sha256").update(commentBody(text)).digest("hex").slice(0, 16);
210
- }
211
301
 
212
- export function declaredCommentSha(text: string): string | null {
213
- const m = MARKER.exec(text);
214
- return m ? m[1] : null;
215
- }
216
302
 
217
- export function stampComment(generated: string): string {
218
- const body = commentBody(generated);
219
- return `// generated-sha: ${commentSha(body)}\n${body}`;
220
- }
303
+
221
304
 
222
305
  export interface WriteDecision {
223
306
  action: "create" | "unchanged" | "changed" | "preserve" | "refuse";
@@ -246,16 +329,54 @@ export interface WriteDecision {
246
329
  * never reports a hand-edit, because there is no edit to report — the
247
330
  * content was never the harness's.
248
331
  */
332
+ /**
333
+ * The strategies `decideOwnedWrite` actually has a branch for.
334
+ *
335
+ * Declared once and asserted against the branches by
336
+ * `test/owned-strategy-reachability.test.ts`, so a strategy cannot be added
337
+ * to the union, declared on a row, and left with no code behind it.
338
+ */
339
+ export const DECIDED_HERE: readonly WriteStrategy[] = [
340
+ "regenerate",
341
+ "create-only",
342
+ "section-preserving",
343
+ ];
344
+
249
345
  export function decideOwnedWrite(
250
346
  strategy: WriteStrategy,
251
347
  existing: string | null,
252
348
  generated: string,
349
+ sections: SectionSpec = HARD_CONSTRAINTS_SECTION,
253
350
  ): WriteDecision {
351
+ // Refuse a strategy this function does not implement, BEFORE anything
352
+ // else looks at the file (#759).
353
+ //
354
+ // `job-preserving` and `provenance` are served elsewhere — `servedBy` on
355
+ // their rows says where — and neither has a branch below. Until this
356
+ // throw they fell through to the regenerate tail, so a caller that let
357
+ // `ownedStrategy()` supply the default for `.github/workflows/ci.yml`
358
+ // would have replaced a consumer's whole file, every job in it, and
359
+ // reported `CHANGED: regenerated output differs`. Nothing does that
360
+ // today; `regenerateOwned` defaults to exactly that expression, so it is
361
+ // one forgotten argument away.
362
+ //
363
+ // A dead branch and a silently wrong one look identical in a green suite.
364
+ // This is the loud version.
365
+ if (!DECIDED_HERE.includes(strategy)) {
366
+ const row = OWNED_FILES.find(f => f.strategy === strategy);
367
+ throw new Error(
368
+ `decideOwnedWrite does not implement the '${strategy}' strategy. ` +
369
+ (row?.servedBy
370
+ ? `It is served by ${row.servedBy}. Route this write there, or pass a strategy this function implements.`
371
+ : `No OWNED_FILES row says what serves it, which means nothing does.`),
372
+ );
373
+ }
374
+
254
375
  if (existing === null) {
255
376
  return {
256
377
  action: "create",
257
378
  reason: "no such file",
258
- content: strategy === "provenance" ? stampComment(generated) : generated,
379
+ content: generated,
259
380
  };
260
381
  }
261
382
 
@@ -267,13 +388,14 @@ export function decideOwnedWrite(
267
388
  // The merge is in `lib/agents-md-sections.ts`; what belongs HERE is the
268
389
  // decision, so a strategy cannot be implemented at a call site and left
269
390
  // out of the one binding every owned file routes through.
270
- const merged = preserveSections(existing, generated);
391
+ const merged = preserveSections(existing, generated, sections);
271
392
  if (merged.lost.length > 0) {
272
393
  return {
273
394
  action: "refuse",
274
395
  reason:
275
- `${merged.lost.length} line(s) of user-owned content would be lost, and the spec ` +
276
- `guarantees they survive regeneration: ${merged.lost.join(" | ")}`,
396
+ `${merged.lost.length} line(s) of user-owned content would be lost from ` +
397
+ `${sections.label}, and the spec guarantees they survive regeneration: ` +
398
+ merged.lost.join(" | "),
277
399
  };
278
400
  }
279
401
  if (bytesIdentical(existing, merged.content)) {
@@ -289,21 +411,6 @@ export function decideOwnedWrite(
289
411
  };
290
412
  }
291
413
 
292
- if (strategy === "provenance") {
293
- const stamped = stampComment(generated);
294
- if (bytesIdentical(existing, stamped)) {
295
- return { action: "unchanged", reason: "identical to generator output" };
296
- }
297
- const declared = declaredCommentSha(existing);
298
- if (declared === null) {
299
- return { action: "preserve", reason: "hand-authored — no harness provenance marker" };
300
- }
301
- if (declared !== commentSha(existing)) {
302
- return { action: "preserve", reason: "hand-edited since the harness wrote it" };
303
- }
304
- return { action: "changed", reason: "harness-written and untouched since", content: stamped };
305
- }
306
-
307
414
  if (bytesIdentical(existing, generated)) {
308
415
  return { action: "unchanged", reason: "regenerated output is byte-identical" };
309
416
  }
@@ -30,6 +30,22 @@
30
30
  * message is wrong either way — a developer reading "conformity failed" goes
31
31
  * and looks at conformity, which is green.
32
32
  *
33
+ * ## What was wrong the second time (#728)
34
+ *
35
+ * It asked `--show-toplevel` where the dependencies are. That answers "where
36
+ * is this working tree", and in a LINKED worktree that is not where
37
+ * `node_modules` lives — it is gitignored, so it exists only in the main
38
+ * checkout. `ln -s` does not look at its target, so the link succeeded, the
39
+ * suite could not resolve its imports, and every worktree push was refused as
40
+ * UNMEASURED. The refusal was right and is unchanged; what it could not say
41
+ * is that the cause was the worktree, so three agent sessions in agentgrit
42
+ * reached for `--no-verify` instead.
43
+ *
44
+ * The same wrong root made the lockfile arm vacuous — it compared the pushed
45
+ * commit's lockfile with its own copy in the temp checkout. `MODULES_ROOT`
46
+ * answers the right question, and `link_modules` refuses by name rather than
47
+ * leaving a dangling link.
48
+ *
33
49
  * ## The three outcomes, and why UNMEASURED blocks
34
50
  *
35
51
  * | what happened | exit | message |
@@ -69,10 +85,31 @@ ${PRE_PUSH_MANAGED_MARKER}
69
85
  REFUSE=${PRE_PUSH_REFUSE_EXIT}
70
86
  ZERO=0000000000000000000000000000000000000000
71
87
  TOPLEVEL=$(git rev-parse --show-toplevel) || exit $REFUSE
88
+ # Where this repository's installed dependencies are, which is NOT "where is
89
+ # this working tree" (#728). node_modules is gitignored, so a LINKED worktree
90
+ # has none and never will. --git-common-dir answers the main checkout's .git
91
+ # from any worktree and plain .git in the main checkout itself, so the
92
+ # non-worktree case is unchanged.
93
+ MODULES_ROOT=$(cd "$TOPLEVEL" && cd "$(git rev-parse --git-common-dir)/.." && pwd) || exit $REFUSE
72
94
  conformity=0
73
95
  unmeasured=0
74
96
  why=""
75
97
 
98
+ # Link the main checkout's modules into the temp checkout, or say why not.
99
+ #
100
+ # 'ln -s' does not look at its target: a link to a path that does not exist
101
+ # succeeds, and the suite then dies on an import error that names no cause.
102
+ # That is what a worktree push did before #728 — "the conformity suite did not
103
+ # load" over a repository whose dependencies were installed the whole time.
104
+ link_modules() {
105
+ if [ ! -d "$MODULES_ROOT/node_modules" ]; then
106
+ unmeasured=1
107
+ why="no node_modules at $MODULES_ROOT/node_modules — run bun install there"
108
+ return 0
109
+ fi
110
+ ln -s "$MODULES_ROOT/node_modules" "$1/node_modules" 2>/dev/null
111
+ }
112
+
76
113
  while read -r _lref lsha _rref _rsha; do
77
114
  # Branch deletion: there is no commit to check out.
78
115
  [ "$lsha" = "$ZERO" ] && continue
@@ -91,13 +128,20 @@ while read -r _lref lsha _rref _rsha; do
91
128
  # checkout has no lockfile" as "the dependencies differ" and installs. But a
92
129
  # lockfile absent from the checkout is a lockfile git does not track — it is
93
130
  # gitignored, or the project has none — and then there is nothing in this
94
- # push that can change the dependency graph. The working tree's modules are
95
- # the right ones by definition, and installing is both slower and less
131
+ # push that can change the dependency graph. The main checkout's modules
132
+ # are the right ones by definition, and installing is both slower and less
96
133
  # accurate.
134
+ #
135
+ # The lockfile read here is the one beside the modules being linked, for
136
+ # the same reason (#728). Rooted at the working tree, a worktree push
137
+ # compared the PUSHED commit's lockfile against its own copy in the temp
138
+ # checkout — equal whenever you push what you committed, so the arm that
139
+ # exists to check the installed modules match the pushed ones never looked
140
+ # at the installed modules at all.
97
141
  if [ ! -f "$tmp/bun.lock" ]; then
98
- ln -s "$TOPLEVEL/node_modules" "$tmp/node_modules" 2>/dev/null
99
- elif [ -f "$TOPLEVEL/bun.lock" ] && cmp -s "$TOPLEVEL/bun.lock" "$tmp/bun.lock"; then
100
- ln -s "$TOPLEVEL/node_modules" "$tmp/node_modules" 2>/dev/null
142
+ link_modules "$tmp"
143
+ elif [ -f "$MODULES_ROOT/bun.lock" ] && cmp -s "$MODULES_ROOT/bun.lock" "$tmp/bun.lock"; then
144
+ link_modules "$tmp"
101
145
  else
102
146
  echo "pre-push: dependencies differ from the working tree — installing from the pushed lockfile" >&2
103
147
  if ! ( cd "$tmp" && bun install --frozen-lockfile ) >&2; then