@punks/cli 5.1.1 → 5.2.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 (110) hide show
  1. package/AGENTS.md +28 -10
  2. package/dist/BASELINE_CHANGELOG.md +19 -1
  3. package/dist/CHANGELOG.md +49 -0
  4. package/dist/baseline/scaffold-baseline/baseline.json +5 -10
  5. package/dist/baseline/scaffold-baseline/data/catalog/packs.json +1 -1
  6. package/dist/baseline/scaffold-baseline/data/catalog/skills.json +0 -8
  7. package/dist/baseline/scaffold-baseline/data/hooks/format-edited-file.mjs +408 -309
  8. package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.d.ts +1 -0
  9. package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.mjs +206 -58
  10. package/dist/baseline/scaffold-baseline/data/scripts/commit-gate-runner.test.ts +496 -29
  11. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.d.mts +55 -0
  12. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.mjs +323 -0
  13. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint-runner.test.ts +821 -0
  14. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.d.mts +97 -0
  15. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.mjs +658 -0
  16. package/dist/baseline/scaffold-baseline/data/scripts/managed-lint.test.ts +614 -0
  17. package/dist/baseline/scaffold-baseline/data/scripts/sync-subagents-validator.test.ts +39 -0
  18. package/dist/baseline/scaffold-baseline/data/scripts/sync-subagents.mjs +8 -2
  19. package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/SKILL.md +2 -0
  20. package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/commands.md +6 -7
  21. package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/managed-lint.md +165 -0
  22. package/dist/baseline/scaffold-baseline/skills/agnostic/cli/hi-cli/references/post-command-flow.md +20 -19
  23. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/phases/define-handoffs.md +1 -1
  24. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/phases/model-state.md +1 -1
  25. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/phases/test-routes.md +1 -1
  26. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/phases/write-phases.md +2 -2
  27. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/phases/write-router.md +1 -1
  28. package/dist/baseline/scaffold-baseline/skills/agnostic/docs/write-graph-based-skills/references/authoring-contract.md +8 -8
  29. package/dist/baseline/scaffold-baseline/skills/agnostic/quality/autoreview/SKILL.md +1 -1
  30. package/dist/baseline/scaffold-baseline/skills/agnostic/quality/make-tsuite/SKILL.md +2 -2
  31. package/dist/baseline/scaffold-baseline/skills/agnostic/requirements/brainstorm/SKILL.md +21 -11
  32. package/dist/baseline/scaffold-baseline/skills/phases/debugging-phase/SKILL.md +3 -4
  33. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/SKILL.md +2 -2
  34. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/debug.md +2 -2
  35. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/human-steering.md +25 -0
  36. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/implement.md +2 -2
  37. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/review.md +2 -2
  38. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/router.md +3 -2
  39. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/references/context-continuity.md +1 -1
  40. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/references/failure-continuity.md +1 -1
  41. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/references/phase-handoff.md +4 -3
  42. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/AUTHORING-HANDOFF.md +9 -9
  43. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/SKILL.md +1 -1
  44. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/ensure-fog.md +1 -1
  45. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/grilling.md +1 -1
  46. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/human-steering.md +25 -0
  47. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/prototype.md +1 -1
  48. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/reconcile.md +1 -1
  49. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/research.md +1 -1
  50. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/router.md +1 -1
  51. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/references/state-graph.md +1 -1
  52. package/dist/baseline/scaffold-baseline/skills/phases/review-phase/phases/return-route.md +6 -5
  53. package/dist/baseline/scaffold-baseline/skills/phases/review-phase/phases/router.md +1 -1
  54. package/dist/baseline/scaffold-baseline/skills/phases/review-phase/phases/run-review.md +2 -1
  55. package/dist/baseline/scaffold-baseline/skills/phases/review-phase/references/state-graph.md +3 -3
  56. package/dist/baseline/scaffold-baseline.json +5 -5
  57. package/dist/baseline/scaffold-baseline.tgz +0 -0
  58. package/dist/data/AGENTS.md +22 -5
  59. package/dist/data/hooks/format-edited-file.mjs +237 -325
  60. package/dist/data/scripts/commit-gate-runner.mjs +206 -58
  61. package/dist/data/scripts/managed-lint-runner.d.mts +55 -0
  62. package/dist/data/scripts/managed-lint-runner.mjs +323 -0
  63. package/dist/data/scripts/managed-lint.d.mts +97 -0
  64. package/dist/data/scripts/managed-lint.mjs +658 -0
  65. package/dist/data/scripts/sync-subagents.mjs +8 -2
  66. package/dist/index.js +131 -153
  67. package/dist/skills/agnostic/cli/hi-cli/SKILL.md +2 -0
  68. package/dist/skills/agnostic/cli/hi-cli/references/commands.md +6 -7
  69. package/dist/skills/agnostic/cli/hi-cli/references/managed-lint.md +165 -0
  70. package/dist/skills/agnostic/cli/hi-cli/references/post-command-flow.md +6 -5
  71. package/dist/skills/agnostic/docs/write-graph-based-skills/phases/define-handoffs.md +1 -1
  72. package/dist/skills/agnostic/docs/write-graph-based-skills/phases/model-state.md +1 -1
  73. package/dist/skills/agnostic/docs/write-graph-based-skills/phases/test-routes.md +1 -1
  74. package/dist/skills/agnostic/docs/write-graph-based-skills/phases/write-phases.md +2 -2
  75. package/dist/skills/agnostic/docs/write-graph-based-skills/phases/write-router.md +1 -1
  76. package/dist/skills/agnostic/docs/write-graph-based-skills/references/authoring-contract.md +8 -8
  77. package/dist/skills/agnostic/quality/autoreview/SKILL.md +1 -1
  78. package/dist/skills/agnostic/quality/make-tsuite/SKILL.md +2 -2
  79. package/dist/skills/agnostic/requirements/brainstorm/SKILL.md +21 -11
  80. package/dist/skills/phases/debugging-phase/SKILL.md +3 -4
  81. package/dist/skills/phases/delivery-phase/SKILL.md +2 -2
  82. package/dist/skills/phases/delivery-phase/phases/debug.md +2 -2
  83. package/dist/skills/phases/delivery-phase/phases/human-steering.md +25 -0
  84. package/dist/skills/phases/delivery-phase/phases/implement.md +2 -2
  85. package/dist/skills/phases/delivery-phase/phases/review.md +2 -2
  86. package/dist/skills/phases/delivery-phase/phases/router.md +3 -2
  87. package/dist/skills/phases/delivery-phase/references/context-continuity.md +1 -1
  88. package/dist/skills/phases/delivery-phase/references/failure-continuity.md +1 -1
  89. package/dist/skills/phases/delivery-phase/references/phase-handoff.md +4 -3
  90. package/dist/skills/phases/finder-phase/AUTHORING-HANDOFF.md +6 -6
  91. package/dist/skills/phases/finder-phase/SKILL.md +1 -1
  92. package/dist/skills/phases/finder-phase/phases/ensure-fog.md +1 -1
  93. package/dist/skills/phases/finder-phase/phases/grilling.md +1 -1
  94. package/dist/skills/phases/finder-phase/phases/human-steering.md +25 -0
  95. package/dist/skills/phases/finder-phase/phases/prototype.md +1 -1
  96. package/dist/skills/phases/finder-phase/phases/reconcile.md +1 -1
  97. package/dist/skills/phases/finder-phase/phases/research.md +1 -1
  98. package/dist/skills/phases/finder-phase/phases/router.md +1 -1
  99. package/dist/skills/phases/finder-phase/references/state-graph.md +1 -1
  100. package/dist/skills/phases/review-phase/phases/return-route.md +6 -5
  101. package/dist/skills/phases/review-phase/phases/router.md +1 -1
  102. package/dist/skills/phases/review-phase/phases/run-review.md +2 -1
  103. package/dist/skills/phases/review-phase/references/state-graph.md +3 -3
  104. package/package.json +12 -6
  105. package/dist/baseline/scaffold-baseline/skills/agnostic/generic/handback/SKILL.md +0 -27
  106. package/dist/baseline/scaffold-baseline/skills/phases/delivery-phase/phases/handback.md +0 -27
  107. package/dist/baseline/scaffold-baseline/skills/phases/finder-phase/phases/handback.md +0 -29
  108. package/dist/skills/agnostic/generic/handback/SKILL.md +0 -27
  109. package/dist/skills/phases/delivery-phase/phases/handback.md +0 -27
  110. package/dist/skills/phases/finder-phase/phases/handback.md +0 -29
@@ -1,5 +1,5 @@
1
- import { execFileSync } from "node:child_process";
2
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
1
+ import { spawnSync } from "node:child_process";
2
+ import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { tmpdir } from "node:os";
5
5
  import { afterEach, describe, expect, it } from "vitest";
@@ -9,32 +9,74 @@ interface ExecResult {
9
9
  readonly stderr: string;
10
10
  readonly stdout: string;
11
11
  }
12
- const isRecord = (value: unknown): value is Record<string, unknown> =>
13
- typeof value === "object" && value !== null;
12
+ const authorSelection = async (root: string, contracts: readonly unknown[]) => {
13
+ const scopes = [
14
+ ...new Set([
15
+ ".",
16
+ ...contracts.map((contract) => {
17
+ if (typeof contract !== "object" || contract === null) {
18
+ throw new Error("Invalid test contract");
19
+ }
20
+ return "path" in contract && typeof contract.path === "string"
21
+ ? path.posix.normalize(contract.path)
22
+ : ".";
23
+ }),
24
+ ]),
25
+ ];
26
+ await mkdir(path.join(root, ".devpunks/specs/lint"), { recursive: true });
27
+ await writeFile(path.join(root, ".devpunks/settings.json"), JSON.stringify({ lint: { scopes } }));
28
+ await Promise.all(
29
+ scopes.map(async (scope) => {
30
+ try {
31
+ await writeFile(path.join(root, scope, "package.json"), "{}");
32
+ } catch {
33
+ /* intentional missing owner */
34
+ }
35
+ }),
36
+ );
37
+ await writeFile(
38
+ path.join(root, ".devpunks/specs/lint/selection.json"),
39
+ JSON.stringify({
40
+ managedLint: {
41
+ exclude: [],
42
+ routes: scopes.map((scope) => ({
43
+ config: path.posix.join(scope, "oxlint.config.ts"),
44
+ inputs: ["shared-policy.json"],
45
+ maxWarnings: 0,
46
+ scope,
47
+ tool: { package: "oxlint", version: "1.80.0" },
48
+ })),
49
+ scopes,
50
+ version: 1,
51
+ },
52
+ }),
53
+ );
54
+ };
55
+ const runner = path.resolve(import.meta.dirname, "commit-gate-runner.mjs");
14
56
  const run = async (
15
57
  command: string,
16
58
  args: readonly string[],
17
59
  options: { readonly cwd: string },
18
60
  ): Promise<ExecResult> => {
19
- await Promise.resolve();
20
- try {
21
- return {
22
- code: 0,
23
- stderr: "",
24
- stdout: execFileSync(command, args, {
25
- cwd: options.cwd,
26
- encoding: "utf-8",
27
- stdio: ["ignore", "pipe", "pipe"],
28
- }),
29
- };
30
- } catch (error) {
31
- const record = isRecord(error) ? error : {};
32
- return {
33
- code: typeof record.status === "number" ? record.status : 1,
34
- stderr: typeof record.stderr === "string" ? record.stderr : "",
35
- stdout: typeof record.stdout === "string" ? record.stdout : "",
36
- };
61
+ if (command === "node" && args[0] === runner) {
62
+ const contracts: unknown = JSON.parse(
63
+ await readFile(path.join(options.cwd, ".devpunks/contracts.json"), "utf-8"),
64
+ );
65
+ if (!Array.isArray(contracts)) {
66
+ throw new TypeError("Invalid test contracts");
67
+ }
68
+ await authorSelection(options.cwd, contracts);
37
69
  }
70
+ const result = spawnSync(command, args, {
71
+ cwd: options.cwd,
72
+ encoding: "utf-8",
73
+ stdio: ["ignore", "pipe", "pipe"],
74
+ });
75
+ return {
76
+ code: result.status ?? 1,
77
+ stderr: result.stderr ?? result.error?.message ?? "",
78
+ stdout: result.stdout ?? "",
79
+ };
38
80
  };
39
81
 
40
82
  const runGit = async (args: readonly string[], cwd: string): Promise<void> => {
@@ -43,7 +85,6 @@ const runGit = async (args: readonly string[], cwd: string): Promise<void> => {
43
85
  throw new Error(`git ${args.join(" ")} failed (${result.code}): ${result.stderr}`);
44
86
  }
45
87
  };
46
- const runner = path.resolve(import.meta.dirname, "commit-gate-runner.mjs");
47
88
  const roots = new Set<string>();
48
89
  const linkedWorktrees = new Map<string, string>();
49
90
  const markerScript =
@@ -60,10 +101,16 @@ const fixture = async (repoName?: string) => {
60
101
  await runGit(["config", "--local", "user.name", "Commit Gate Test"], root);
61
102
  await runGit(["config", "--local", "user.email", "commit-gate-test@example.com"], root);
62
103
  await mkdir(path.join(root, ".devpunks"), { recursive: true });
104
+ await writeFile(path.join(root, ".git/info/exclude"), "/marker.mjs\n/.devpunks/\n");
63
105
  await writeFile(path.join(root, "marker.mjs"), markerScript);
64
106
  return root;
65
107
  };
66
108
 
109
+ const runGate = async (root: string, contracts: readonly unknown[]) => {
110
+ await writeFile(path.join(root, ".devpunks/contracts.json"), JSON.stringify(contracts));
111
+ return await run("node", [runner, "--contracts", ".devpunks/contracts.json"], { cwd: root });
112
+ };
113
+
67
114
  describe("Commit Gate runner", () => {
68
115
  afterEach(async () => {
69
116
  const worktreeResults = await Promise.all(
@@ -90,6 +137,422 @@ describe("Commit Gate runner", () => {
90
137
  }
91
138
  });
92
139
 
140
+ it.each([false, true])(
141
+ "dispatches root repository lint for child-only changes when root selected=%s",
142
+ async (rootSelected) => {
143
+ const root = await fixture();
144
+ await mkdir(path.join(root, "apps/web/fixtures/demo"), { recursive: true });
145
+ await mkdir(path.join(root, "wiki"));
146
+ await writeFile(path.join(root, "apps/web/source.ts"), "export const source = 1;");
147
+ await writeFile(path.join(root, "apps/web/fixtures/demo/package.json"), "{}");
148
+ await writeFile(path.join(root, "apps/web/fixtures/demo/ignored.ts"), "console.log(1)");
149
+ await writeFile(path.join(root, "wiki/ignored.ts"), "console.log(2)");
150
+ await runGit(["add", "apps", "wiki"], root);
151
+ await authorSelection(root, [{ path: "apps/web" }]);
152
+ if (!rootSelected) {
153
+ await writeFile(
154
+ path.join(root, ".devpunks/settings.json"),
155
+ JSON.stringify({ lint: { scopes: ["apps/web"] } }),
156
+ );
157
+ await writeFile(
158
+ path.join(root, ".devpunks/specs/lint/selection.json"),
159
+ JSON.stringify({
160
+ managedLint: {
161
+ exclude: [],
162
+ routes: [
163
+ {
164
+ config: "apps/web/oxlint.config.ts",
165
+ inputs: [],
166
+ maxWarnings: 0,
167
+ scope: "apps/web",
168
+ tool: { package: "oxlint", version: "1.80.0" },
169
+ },
170
+ ],
171
+ scopes: ["apps/web"],
172
+ version: 1,
173
+ },
174
+ }),
175
+ );
176
+ }
177
+ await writeFile(
178
+ path.join(root, "record.mjs"),
179
+ 'console.log(process.argv[2]+":"+process.env.HI_STAGED_FILES)',
180
+ );
181
+ await writeFile(
182
+ path.join(root, ".devpunks/contracts.json"),
183
+ JSON.stringify([
184
+ {
185
+ execution: { formatCheck: "files", lint: "repository" },
186
+ formatCheck: "exit 91",
187
+ lint: "node record.mjs lint",
188
+ path: ".",
189
+ },
190
+ {
191
+ execution: { formatCheck: "files", lint: "files" },
192
+ formatCheck: "node ../../record.mjs format {files}",
193
+ lint: "exit 92",
194
+ path: "apps/web",
195
+ },
196
+ ]),
197
+ );
198
+ const result = spawnSync("node", [runner, "--contracts", ".devpunks/contracts.json"], {
199
+ cwd: root,
200
+ encoding: "utf-8",
201
+ });
202
+ expect({ code: result.status, stderr: result.stderr, stdout: result.stdout }).toStrictEqual({
203
+ code: 0,
204
+ stderr: "",
205
+ stdout: "lint:apps/web/source.ts\nformat:source.ts\n",
206
+ });
207
+ },
208
+ );
209
+
210
+ it("skips wiki-only staging before command coverage and execution", async () => {
211
+ const root = await fixture();
212
+ await mkdir(path.join(root, "wiki"));
213
+ await writeFile(path.join(root, "wiki/page.ts"), "export const wiki = true;");
214
+ await runGit(["add", "wiki/page.ts"], root);
215
+ const result = await runGate(root, [{ formatCheck: "exit 19", lint: "exit 19" }]);
216
+ expect(result.code).toBe(0);
217
+ });
218
+
219
+ it("fans shared policy edits out to dependent owners using readonly check commands", async () => {
220
+ const root = await fixture();
221
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
222
+ await writeFile(path.join(root, "apps/web/source.ts"), "export const web=1;");
223
+ await writeFile(path.join(root, "shared-policy.json"), "{}");
224
+ await runGit(["add", "shared-policy.json"], root);
225
+ const result = await runGate(root, [
226
+ {
227
+ execution: { formatCheck: "files", lint: "files" },
228
+ formatCheck: "node ../../marker.mjs {files}",
229
+ lint: "node ../../marker.mjs {files}",
230
+ path: "apps/web",
231
+ },
232
+ ]);
233
+ expect(result.code).toBe(0);
234
+ await expect(readFile(path.join(root, "apps/web/marker.out"), "utf-8")).resolves.toContain(
235
+ "source.ts",
236
+ );
237
+ });
238
+
239
+ it("preserves successful output once on its original streams in contract and command order", async () => {
240
+ const root = await fixture();
241
+ await writeFile(path.join(root, "a.ts"), "a");
242
+ await runGit(["add", "a.ts"], root);
243
+ await writeFile(
244
+ path.join(root, "diagnostics.mjs"),
245
+ "const [label, delay] = process.argv.slice(2); setTimeout(() => { process.stdout.write(label + ':out\\n'); process.stderr.write(label + ':err\\n'); }, Number(delay));",
246
+ );
247
+ const result = await runGate(root, [
248
+ {
249
+ formatCheck: "node diagnostics.mjs first-format 40",
250
+ lint: "node diagnostics.mjs first-lint 60",
251
+ },
252
+ {
253
+ formatCheck: "node diagnostics.mjs second-format 0",
254
+ lint: "node diagnostics.mjs second-lint 20",
255
+ },
256
+ ]);
257
+ expect(result).toStrictEqual({
258
+ code: 0,
259
+ stderr: "first-lint:err\nfirst-format:err\nsecond-lint:err\nsecond-format:err\n",
260
+ stdout: "first-lint:out\nfirst-format:out\nsecond-lint:out\nsecond-format:out\n",
261
+ });
262
+ await expect(runGate(root, [{ formatCheck: "true", lint: "true" }])).resolves.toStrictEqual({
263
+ code: 0,
264
+ stderr: "",
265
+ stdout: "",
266
+ });
267
+ });
268
+
269
+ it("runs explicit owner commands verbatim and expands only explicit file placeholders", async () => {
270
+ const root = await fixture();
271
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
272
+ const file = "a 'quoted'.ts";
273
+ await writeFile(path.join(root, "apps/web", file), "a");
274
+ await runGit(["add", "apps/web"], root);
275
+ await writeFile(
276
+ path.join(root, "arguments.mjs"),
277
+ "const [label, ...args] = process.argv.slice(2); process.stdout.write(JSON.stringify({ label, args, files: process.env.HI_STAGED_FILES }) + '\\n');",
278
+ );
279
+ const result = await runGate(root, [
280
+ {
281
+ execution: { formatCheck: "files", lint: "owner" },
282
+ formatCheck: "node ../../arguments.mjs files {files} {files}",
283
+ lint: "node ../../arguments.mjs owner . '{files}'",
284
+ owner: "web",
285
+ path: "apps/web",
286
+ },
287
+ ]);
288
+ expect(result.code).toBe(0);
289
+ expect(result.stderr).toBe("");
290
+ expect(
291
+ result.stdout
292
+ .trim()
293
+ .split("\n")
294
+ .map((line): unknown => JSON.parse(line)),
295
+ ).toStrictEqual([
296
+ { args: [".", "{files}"], files: file, label: "owner" },
297
+ { args: [file, file], files: file, label: "files" },
298
+ ]);
299
+ });
300
+
301
+ it.each(["delete", "rename"])(
302
+ "triggers aggregate coverage for %s source paths and nested owners without passing deleted names",
303
+ async (change) => {
304
+ const root = await fixture();
305
+ await mkdir(path.join(root, "apps/source/nested"), { recursive: true });
306
+ await mkdir(path.join(root, "apps/destination"), { recursive: true });
307
+ const oldFile = "apps/source/nested/old\nname.ts";
308
+ const newFile = "apps/destination/new.ts";
309
+ await writeFile(path.join(root, oldFile), "export const value = 1;\n");
310
+ await runGit(["add", oldFile], root);
311
+ await runGit(["commit", "-qm", "seed"], root);
312
+ await (change === "rename"
313
+ ? rename(path.join(root, oldFile), path.join(root, newFile))
314
+ : rm(path.join(root, oldFile)));
315
+ await runGit(["add", "-A"], root);
316
+ await writeFile(
317
+ path.join(root, "arguments.mjs"),
318
+ "process.stdout.write(JSON.stringify({ args: process.argv.slice(2), files: process.env.HI_STAGED_FILES }) + '\\n');",
319
+ );
320
+ const owners = ["apps/source", "apps/source/nested", "apps/destination"];
321
+ const result = await runGate(
322
+ root,
323
+ owners.map((owner) => ({
324
+ execution: { formatCheck: "owner", lint: "owner" },
325
+ formatCheck: `node ${path.relative(owner, "arguments.mjs")} ${owner}-format .`,
326
+ lint: `node ${path.relative(owner, "arguments.mjs")} ${owner}-lint .`,
327
+ owner,
328
+ path: owner,
329
+ })),
330
+ );
331
+ expect(result.code).toBe(0);
332
+ expect(result.stderr).toBe("");
333
+ expect(
334
+ result.stdout
335
+ .trim()
336
+ .split("\n")
337
+ .map((line): unknown => JSON.parse(line)),
338
+ ).toStrictEqual(
339
+ owners.slice(1, change === "rename" ? 3 : 2).flatMap((owner) =>
340
+ ["lint", "format"].map((kind) => ({
341
+ args: [`${owner}-${kind}`, "."],
342
+ files: owner === "apps/destination" ? "new.ts" : "",
343
+ })),
344
+ ),
345
+ );
346
+ },
347
+ );
348
+
349
+ it.each(["root", "root-with-owner", "empty", "deleted-legacy", "deleted-files"])(
350
+ "fails actionably when %s changes have no command coverage",
351
+ async (change) => {
352
+ const root = await fixture();
353
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
354
+ const file = change.startsWith("deleted") ? "apps/web/gone.ts" : "root.ts";
355
+ await writeFile(path.join(root, file), "a");
356
+ await runGit(["add", file], root);
357
+ if (change === "root-with-owner") {
358
+ await writeFile(path.join(root, "apps/web/a.ts"), "a");
359
+ await runGit(["add", "apps/web/a.ts"], root);
360
+ }
361
+ if (change.startsWith("deleted")) {
362
+ await runGit(["commit", "-qm", "seed"], root);
363
+ await runGit(["rm", "-q", file], root);
364
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
365
+ }
366
+ const result = await runGate(
367
+ root,
368
+ change === "empty"
369
+ ? []
370
+ : [
371
+ {
372
+ formatCheck: "node ../../marker.mjs {files}",
373
+ lint: "node ../../marker.mjs {files}",
374
+ owner: "web",
375
+ path: "apps/web",
376
+ ...(change === "deleted-files"
377
+ ? { execution: { formatCheck: "files", lint: "files" } }
378
+ : {}),
379
+ },
380
+ ],
381
+ );
382
+ expect(result.code).toBe(1);
383
+ expect(result.stderr).toContain(file);
384
+ expect(result.stderr).toMatch(
385
+ /coverage.*lint.*formatCheck.*commitGateChecks.repository.*hi update/su,
386
+ );
387
+ await expect(readFile(path.join(root, "apps/web/marker.out"), "utf-8")).rejects.toThrow(
388
+ "ENOENT",
389
+ );
390
+ },
391
+ );
392
+
393
+ it("lets explicit repository authority subsume only the same kind of subordinate check", async () => {
394
+ const root = await fixture();
395
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
396
+ await writeFile(path.join(root, "apps/web/gone.ts"), "gone");
397
+ await runGit(["add", "apps/web/gone.ts"], root);
398
+ await runGit(["commit", "-qm", "seed"], root);
399
+ await runGit(["rm", "-q", "apps/web/gone.ts"], root);
400
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
401
+ await writeFile(path.join(root, "root.ts"), "root");
402
+ await runGit(["add", "root.ts"], root);
403
+ const subordinate = {
404
+ execution: { formatCheck: "owner", lint: "files" },
405
+ formatCheck: "printf web-format",
406
+ lint: "false",
407
+ owner: "web",
408
+ path: "apps/web",
409
+ };
410
+ const repository = {
411
+ execution: { formatCheck: "owner", lint: "repository" },
412
+ formatCheck: "printf root-format",
413
+ lint: "printf root-lint",
414
+ };
415
+ await expect(runGate(root, [subordinate, repository])).resolves.toStrictEqual({
416
+ code: 0,
417
+ stderr: "",
418
+ stdout: "web-formatroot-lintroot-format",
419
+ });
420
+ await expect(
421
+ runGate(root, [
422
+ { ...subordinate, formatCheck: "false" },
423
+ { ...repository, execution: { formatCheck: "repository", lint: "repository" } },
424
+ ]),
425
+ ).resolves.toStrictEqual({ code: 0, stderr: "", stdout: "root-lintroot-format" });
426
+ const ownerOnly = await runGate(root, [
427
+ { ...subordinate, execution: { formatCheck: "owner", lint: "owner" } },
428
+ { ...repository, execution: { formatCheck: "owner", lint: "owner" } },
429
+ ]);
430
+ expect(ownerOnly.code).toBe(1);
431
+ expect(ownerOnly.stderr).toContain("lint failed for web");
432
+ });
433
+
434
+ it("executes identical final invocations once while retaining distinct custom commands and working directories", async () => {
435
+ const root = await fixture();
436
+ await mkdir(path.join(root, "apps/web"), { recursive: true });
437
+ await writeFile(path.join(root, "apps/web/a.ts"), "a");
438
+ await runGit(["add", "apps/web/a.ts"], root);
439
+ await writeFile(
440
+ path.join(root, "record.mjs"),
441
+ "import fs from 'node:fs'; const label = process.argv[2]; fs.appendFileSync('calls.out', label + '\\n'); process.stdout.write(label + ':' + process.env.HI_STAGED_FILES + '\\n');",
442
+ );
443
+ const first = `node '${path.join(root, "record.mjs")}' first`;
444
+ const second = `node '${path.join(root, "record.mjs")}' second`;
445
+ const execution = { formatCheck: "owner", lint: "owner" };
446
+ await expect(
447
+ runGate(root, [
448
+ { execution, formatCheck: first, lint: first },
449
+ { execution, formatCheck: second, lint: first, path: "" },
450
+ { execution, formatCheck: first, lint: first, path: "apps/web" },
451
+ ]),
452
+ ).resolves.toStrictEqual({
453
+ code: 0,
454
+ stderr: "",
455
+ stdout: "first:a.ts\n",
456
+ });
457
+ await expect(readFile(path.join(root, "calls.out"), "utf-8")).rejects.toThrow("ENOENT");
458
+ await expect(readFile(path.join(root, "apps/web/calls.out"), "utf-8")).resolves.toBe("first\n");
459
+ });
460
+
461
+ it.each(["missing-placeholder", "unknown-mode", "partial-execution"])(
462
+ "rejects %s execution metadata instead of guessing coverage",
463
+ async (configuration) => {
464
+ const root = await fixture();
465
+ await writeFile(path.join(root, "a.ts"), "a");
466
+ await runGit(["add", "a.ts"], root);
467
+ const execution =
468
+ configuration === "partial-execution"
469
+ ? { formatCheck: "owner" }
470
+ : { formatCheck: "owner", lint: configuration === "unknown-mode" ? "shell" : "files" };
471
+ const result = await runGate(root, [
472
+ {
473
+ execution,
474
+ formatCheck: "true",
475
+ lint: "node marker.mjs .",
476
+ },
477
+ ]);
478
+ expect(result.code).toBe(1);
479
+ expect(result.stderr).toMatch(/coverage.*lint.*hi update/su);
480
+ expect(result.stderr).toContain(
481
+ configuration === "missing-placeholder" ? "{files}" : "execution mode",
482
+ );
483
+ await expect(readFile(path.join(root, "marker.out"), "utf-8")).rejects.toThrow("ENOENT");
484
+ },
485
+ );
486
+
487
+ it("deduplicates legacy templates after expanding their final file arguments", async () => {
488
+ const root = await fixture();
489
+ await writeFile(path.join(root, "a.ts"), "a");
490
+ await runGit(["add", "a.ts"], root);
491
+ await writeFile(
492
+ path.join(root, "count.mjs"),
493
+ "import fs from 'node:fs'; fs.appendFileSync('calls.out', process.argv.slice(2).join(',') + '\\n');",
494
+ );
495
+ await expect(
496
+ runGate(root, [{ formatCheck: "node count.mjs {files}", lint: "node count.mjs ." }]),
497
+ ).resolves.toStrictEqual({
498
+ code: 0,
499
+ stderr: "",
500
+ stdout: "",
501
+ });
502
+ await expect(readFile(path.join(root, "calls.out"), "utf-8")).resolves.toBe("a.ts\n");
503
+ });
504
+
505
+ it("rejects a rename with uncovered source coverage even when destination file checks are available", async () => {
506
+ const root = await fixture();
507
+ await mkdir(path.join(root, "apps/source"), { recursive: true });
508
+ await mkdir(path.join(root, "apps/destination"), { recursive: true });
509
+ await writeFile(path.join(root, "apps/source/a.ts"), "a");
510
+ await runGit(["add", "apps/source/a.ts"], root);
511
+ await runGit(["commit", "-qm", "seed"], root);
512
+ await runGit(["mv", "apps/source/a.ts", "apps/destination/a.ts"], root);
513
+ const result = await runGate(
514
+ root,
515
+ ["apps/source", "apps/destination"].map((owner) => ({
516
+ execution: { formatCheck: "files", lint: "files" },
517
+ formatCheck: "node ../../marker.mjs {files}",
518
+ lint: "node ../../marker.mjs {files}",
519
+ owner,
520
+ path: owner,
521
+ })),
522
+ );
523
+ expect(result.code).toBe(1);
524
+ expect(result.stderr).toContain("coverage");
525
+ expect(result.stderr).toContain("apps/source/a.ts");
526
+ expect(result.stderr).toContain("hi update");
527
+ });
528
+
529
+ it("passes only present staged names to explicit file commands after deletions and renames", async () => {
530
+ const root = await fixture();
531
+ await Promise.all(
532
+ ["old.ts", "gone.ts"].map(async (file) => {
533
+ await writeFile(path.join(root, file), file);
534
+ }),
535
+ );
536
+ await runGit(["add", "old.ts", "gone.ts"], root);
537
+ await runGit(["commit", "-qm", "seed"], root);
538
+ await runGit(["mv", "old.ts", "new.ts"], root);
539
+ await runGit(["rm", "-q", "gone.ts"], root);
540
+ await writeFile(
541
+ path.join(root, "arguments.mjs"),
542
+ "process.stdout.write(JSON.stringify({ args: process.argv.slice(2), files: process.env.HI_STAGED_FILES }));",
543
+ );
544
+ const result = await runGate(root, [
545
+ {
546
+ execution: { formatCheck: "owner", lint: "files" },
547
+ formatCheck: "true",
548
+ lint: "node arguments.mjs {files}",
549
+ },
550
+ ]);
551
+ expect(result.code).toBe(0);
552
+ expect(result.stderr).toBe("");
553
+ expect(JSON.parse(result.stdout)).toStrictEqual({ args: ["new.ts"], files: "new.ts" });
554
+ });
555
+
93
556
  it("ignores a stale legacy root and runs from the active worktree", async () => {
94
557
  const root = await fixture();
95
558
  await writeFile(path.join(root, "a.ts"), "a");
@@ -227,8 +690,7 @@ describe("Commit Gate runner", () => {
227
690
  expect(result.code).toBe(1);
228
691
  expect(result.stderr).toContain("missing-owner");
229
692
  expect(result.stderr).toContain("lint");
230
- expect(result.stderr).toContain(missingOwner);
231
- expect(result.stderr).toMatch(/ENOENT|no such file/u);
693
+ expect(result.stderr).toContain("Selected owner is missing");
232
694
  });
233
695
 
234
696
  it("does not spawn quality commands when no files are staged", async () => {
@@ -247,7 +709,7 @@ describe("Commit Gate runner", () => {
247
709
  const result = await run("node", [runner, "--contracts", ".devpunks/contracts.json"], {
248
710
  cwd: root,
249
711
  });
250
- expect(result.stdout).toBe("");
712
+ expect(result).toStrictEqual({ code: 0, stderr: "", stdout: "" });
251
713
  await expect(readFile(path.join(root, "marker.out"), "utf-8")).rejects.toThrow("ENOENT");
252
714
  });
253
715
 
@@ -255,7 +717,10 @@ describe("Commit Gate runner", () => {
255
717
  const root = await fixture();
256
718
  await writeFile(path.join(root, "a.ts"), "a");
257
719
  await run("git", ["add", "."], { cwd: root });
258
- await writeFile(path.join(root, "fail.mjs"), "process.exit(1);");
720
+ await writeFile(
721
+ path.join(root, "fail.mjs"),
722
+ "process.stdout.write('failure-out\\n'); process.stderr.write('failure-err\\n'); process.exit(7);",
723
+ );
259
724
  await writeFile(
260
725
  path.join(root, ".devpunks/contracts.json"),
261
726
  JSON.stringify([
@@ -266,8 +731,10 @@ describe("Commit Gate runner", () => {
266
731
  cwd: root,
267
732
  });
268
733
  expect(result.code).toBe(1);
269
- expect(result.stderr).toContain("lint");
270
- expect(result.stderr).toContain("format");
734
+ expect(result.stderr).toMatch(/lint.*exit 7.*format.*exit 7/su);
735
+ expect(result.stdout).toBe("");
736
+ expect(result.stderr.match(/failure-out/gu)).toHaveLength(1);
737
+ expect(result.stderr.match(/failure-err/gu)).toHaveLength(1);
271
738
  });
272
739
 
273
740
  it("passes root-relative staged files and excludes deletions", async () => {
@@ -0,0 +1,55 @@
1
+ import type { ManagedLintResolution, ManagedLintCommand } from "./managed-lint.mjs";
2
+ import type { SpawnSyncReturns } from "node:child_process";
3
+
4
+ export interface ManagedLintRunOptions {
5
+ readonly root: string;
6
+ readonly scope?: string | undefined;
7
+ readonly paths?: readonly string[] | undefined;
8
+ readonly output?: "human" | "json" | undefined;
9
+ }
10
+ export interface ManagedLintDiagnostic {
11
+ readonly message: string;
12
+ readonly filename?: string;
13
+ readonly code?: string;
14
+ readonly severity?: string;
15
+ readonly [key: string]: unknown;
16
+ }
17
+ export type ReadyManagedLintCommand = Extract<ManagedLintCommand, { readonly status: "ready" }>;
18
+ export interface ManagedLintExecutionOptions {
19
+ readonly root: string;
20
+ /** A resolver-produced command, optionally with --fix for a controlled mirror. */
21
+ readonly command: ReadyManagedLintCommand;
22
+ readonly output?: "human" | "json";
23
+ }
24
+ /** Execute and classify one local command using the same rules as root/package runs. */
25
+ export function executeManagedLintCommand(
26
+ options: ManagedLintExecutionOptions,
27
+ ): ManagedLintExecution;
28
+ export interface ManagedLintExecution extends Readonly<
29
+ Pick<SpawnSyncReturns<string>, "status" | "signal" | "error">
30
+ > {
31
+ /** Normalized classification; native process evidence remains in status/signal/error. */
32
+ readonly code: 0 | 1 | 2;
33
+ readonly command: ReadyManagedLintCommand;
34
+ readonly scope: string;
35
+ readonly stdout: string;
36
+ readonly stderr: string;
37
+ readonly diagnostics?: readonly ManagedLintDiagnostic[];
38
+ readonly failure?: {
39
+ readonly kind: "config-load" | "process" | "malformed-output";
40
+ readonly phase: "config" | "lint";
41
+ };
42
+ }
43
+ export interface ManagedLintRunResult {
44
+ readonly code: 0 | 1 | 2;
45
+ readonly status: "clean" | "findings" | "operational-failure" | "conflict" | "selection-needed";
46
+ readonly diagnostics?: readonly string[];
47
+ readonly results?: readonly ManagedLintExecution[];
48
+ }
49
+ export function loadManagedLint(
50
+ root: string,
51
+ paths?: readonly string[],
52
+ scope?: string,
53
+ ): Promise<ManagedLintResolution>;
54
+ export function managedLintEnvironment(root: string, cwd: string): NodeJS.ProcessEnv;
55
+ export function runManagedLint(options: ManagedLintRunOptions): Promise<ManagedLintRunResult>;