architecture-harness 0.25.0 → 0.26.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 (244) hide show
  1. package/package.json +3 -22
  2. package/src/cli/ask.mjs +42 -0
  3. package/src/cli/ask.test.mjs +40 -0
  4. package/src/cli/branch-sync.mjs +126 -0
  5. package/src/cli/branch-sync.test.mjs +428 -0
  6. package/src/cli/budget-guard.mjs +74 -5
  7. package/src/cli/budget-guard.test.mjs +185 -2
  8. package/src/cli/budget-report.mjs +85 -0
  9. package/src/cli/budget-report.test.mjs +173 -0
  10. package/src/cli/budget.mjs +8 -5
  11. package/src/cli/check-commands.mjs +123 -11
  12. package/src/cli/check-commands.test.mjs +360 -6
  13. package/src/cli/check-feature-switches.test.mjs +110 -0
  14. package/src/cli/check-full.test.mjs +152 -0
  15. package/src/cli/check-verdict.mjs +46 -0
  16. package/src/cli/check-verdict.test.mjs +32 -0
  17. package/src/cli/check.mjs +137 -4
  18. package/src/cli/check.test.mjs +45 -0
  19. package/src/cli/checkpoint.mjs +143 -0
  20. package/src/cli/checkpoint.test.mjs +146 -0
  21. package/src/cli/child-run.mjs +7 -2
  22. package/src/cli/child-run.test.mjs +17 -2
  23. package/src/cli/ci-failures.mjs +304 -0
  24. package/src/cli/ci-failures.test.mjs +615 -0
  25. package/src/cli/ci-preview.mjs +62 -0
  26. package/src/cli/ci-preview.test.mjs +85 -0
  27. package/src/cli/ci-state.mjs +25 -29
  28. package/src/cli/ci-state.test.mjs +174 -2
  29. package/src/cli/ci-template.test.mjs +86 -0
  30. package/src/cli/claude-core.test.mjs +37 -0
  31. package/src/cli/codex-implement.mjs +580 -47
  32. package/src/cli/codex-implement.test.mjs +1946 -29
  33. package/src/cli/codex-limit.mjs +85 -0
  34. package/src/cli/codex-limit.test.mjs +179 -0
  35. package/src/cli/codex-reset.mjs +78 -0
  36. package/src/cli/codex-reset.test.mjs +192 -0
  37. package/src/cli/codex-verify.mjs +230 -0
  38. package/src/cli/codex-verify.test.mjs +402 -0
  39. package/src/cli/comment-watch.mjs +391 -0
  40. package/src/cli/comment-watch.test.mjs +392 -0
  41. package/src/cli/commit-guard.mjs +219 -0
  42. package/src/cli/commit-guard.test.mjs +809 -0
  43. package/src/cli/context-budgets.mjs +47 -0
  44. package/src/cli/context-budgets.test.mjs +96 -0
  45. package/src/cli/context-watch.mjs +116 -0
  46. package/src/cli/context-watch.test.mjs +395 -0
  47. package/src/cli/contract-history.mjs +1 -1
  48. package/src/cli/controller-guard.mjs +38 -0
  49. package/src/cli/controller-guard.test.mjs +66 -2
  50. package/src/cli/cost-stop.mjs +5 -1
  51. package/src/cli/cost-stop.test.mjs +26 -0
  52. package/src/cli/diff-shape.mjs +162 -0
  53. package/src/cli/diff-shape.test.mjs +200 -0
  54. package/src/cli/direct-lane.mjs +32 -19
  55. package/src/cli/direct-lane.test.mjs +21 -3
  56. package/src/cli/dispatch-guard.mjs +11 -0
  57. package/src/cli/dispatch-guard.test.mjs +145 -10
  58. package/src/cli/dispatch-reminder.mjs +23 -10
  59. package/src/cli/dispatch-reminder.test.mjs +63 -11
  60. package/src/cli/dispatch-route.mjs +54 -0
  61. package/src/cli/dispatch-route.test.mjs +101 -0
  62. package/src/cli/dispatch-slot.mjs +2 -2
  63. package/src/cli/dispatch-slot.test.mjs +18 -19
  64. package/src/cli/dispatch.test.mjs +3 -3
  65. package/src/cli/e2e-affected.mjs +15 -0
  66. package/src/cli/e2e-affected.test.mjs +73 -0
  67. package/src/cli/fleet.mjs +180 -0
  68. package/src/cli/fleet.test.mjs +213 -0
  69. package/src/cli/gate-slot.mjs +2 -1
  70. package/src/cli/gh-merge-command.mjs +96 -31
  71. package/src/cli/gh-merge-command.test.mjs +74 -0
  72. package/src/cli/gh-run.mjs +64 -3
  73. package/src/cli/gh-run.test.mjs +66 -1
  74. package/src/cli/git-hooks.test.mjs +25 -3
  75. package/src/cli/git-read.mjs +2 -1
  76. package/src/cli/integrate-tree.mjs +47 -0
  77. package/src/cli/integrate.mjs +143 -40
  78. package/src/cli/integrate.test.mjs +347 -1
  79. package/src/cli/integration-lane.mjs +62 -0
  80. package/src/cli/integration-lane.test.mjs +213 -0
  81. package/src/cli/ledger.mjs +4 -2
  82. package/src/cli/ledger.test.mjs +12 -3
  83. package/src/cli/legs.mjs +53 -0
  84. package/src/cli/legs.test.mjs +70 -0
  85. package/src/cli/lint-changed.mjs +59 -10
  86. package/src/cli/lint-changed.test.mjs +112 -1
  87. package/src/cli/locked-tests.mjs +134 -0
  88. package/src/cli/locked-tests.test.mjs +188 -0
  89. package/src/cli/merge-guard.mjs +234 -62
  90. package/src/cli/merge-guard.test.mjs +768 -39
  91. package/src/cli/metrics.mjs +53 -0
  92. package/src/cli/metrics.test.mjs +73 -0
  93. package/src/cli/milestone.mjs +297 -0
  94. package/src/cli/milestone.test.mjs +294 -0
  95. package/src/cli/mutate.mjs +203 -0
  96. package/src/cli/mutate.test.mjs +134 -0
  97. package/src/cli/output-redaction.mjs +26 -0
  98. package/src/cli/output-redaction.test.mjs +117 -0
  99. package/src/cli/overview.mjs +131 -0
  100. package/src/cli/overview.test.mjs +137 -0
  101. package/src/cli/pause.mjs +93 -0
  102. package/src/cli/pause.test.mjs +109 -0
  103. package/src/cli/plan-check.mjs +84 -12
  104. package/src/cli/plan-check.test.mjs +152 -2
  105. package/src/cli/plan-stop.mjs +7 -4
  106. package/src/cli/plan-stop.test.mjs +87 -0
  107. package/src/cli/platform-risk.mjs +46 -0
  108. package/src/cli/platform-risk.test.mjs +38 -0
  109. package/src/cli/plugin-skills.test.mjs +25 -0
  110. package/src/cli/pr-guard.mjs +128 -0
  111. package/src/cli/pr-guard.test.mjs +154 -0
  112. package/src/cli/promotion-branch.mjs +2 -0
  113. package/src/cli/qc.mjs +160 -27
  114. package/src/cli/qc.test.mjs +93 -0
  115. package/src/cli/report-stop.mjs +5 -0
  116. package/src/cli/report-stop.test.mjs +23 -1
  117. package/src/cli/retry-failed.mjs +3 -1
  118. package/src/cli/review-gate.mjs +24 -6
  119. package/src/cli/review-gate.test.mjs +116 -0
  120. package/src/cli/review-promotion.mjs +107 -0
  121. package/src/cli/review-promotion.test.mjs +251 -0
  122. package/src/cli/role-budget.mjs +50 -0
  123. package/src/cli/role-budget.test.mjs +91 -0
  124. package/src/cli/run-log.mjs +73 -0
  125. package/src/cli/run-log.test.mjs +121 -0
  126. package/src/cli/secret-patterns.mjs +16 -0
  127. package/src/cli/secret-patterns.test.mjs +73 -0
  128. package/src/cli/setup-block.mjs +24 -0
  129. package/src/cli/setup-block.test.mjs +96 -0
  130. package/src/cli/setup-checks.mjs +65 -0
  131. package/src/cli/setup-checks.test.mjs +158 -0
  132. package/src/cli/setup-compaction.mjs +55 -0
  133. package/src/cli/setup-compaction.test.mjs +115 -0
  134. package/src/cli/setup-skills.mjs +65 -0
  135. package/src/cli/setup-skills.test.mjs +171 -0
  136. package/src/cli/setup.mjs +107 -0
  137. package/src/cli/setup.test.mjs +275 -0
  138. package/src/cli/skill-length.test.mjs +8 -0
  139. package/src/cli/spawn-command.mjs +47 -0
  140. package/src/cli/spawn-command.test.mjs +64 -0
  141. package/src/cli/task-gate-review.test.mjs +21 -2
  142. package/src/cli/task-gate.mjs +25 -12
  143. package/src/cli/task-gate.test.mjs +43 -5
  144. package/src/cli/tokens.mjs +46 -3
  145. package/src/cli/tokens.test.mjs +103 -3
  146. package/src/cli/tools.mjs +49 -0
  147. package/src/cli/tools.test.mjs +89 -0
  148. package/src/cli/transcript-usage.mjs +27 -0
  149. package/src/cli/transcript-usage.test.mjs +34 -0
  150. package/src/cli/user-config-package.mjs +1 -1
  151. package/src/cli/user-config-toml.mjs +63 -7
  152. package/src/cli/user-config.mjs +39 -7
  153. package/src/cli/user-config.test.mjs +238 -5
  154. package/src/cli/watch-arm.mjs +39 -0
  155. package/src/cli/watch-arm.test.mjs +87 -0
  156. package/src/cli/watch.mjs +80 -35
  157. package/src/cli/watch.test.mjs +233 -27
  158. package/src/cli/work-order-guard.mjs +2 -1
  159. package/src/cli/workflow-hooks.test.mjs +18 -4
  160. package/src/cli/workflow-settings.mjs +44 -8
  161. package/src/cli/workflow-settings.test.mjs +124 -1
  162. package/src/cli/worktree.mjs +14 -1
  163. package/src/cli/worktree.test.mjs +20 -1
  164. package/src/config.mjs +174 -7
  165. package/src/config.test.mjs +327 -5
  166. package/src/consumer-package.mjs +31 -0
  167. package/src/consumer-package.test.mjs +54 -0
  168. package/src/descriptions.mjs +2 -0
  169. package/src/eslint/index.mjs +12 -0
  170. package/src/eslint/jsx.mjs +20 -0
  171. package/src/eslint/preset.mjs +26 -2
  172. package/src/eslint/preset.test.mjs +104 -2
  173. package/src/eslint/rules/no-arbitrary-class-value.mjs +32 -0
  174. package/src/eslint/rules/no-literal-color.mjs +94 -0
  175. package/src/eslint/rules/no-static-inline-style.mjs +41 -0
  176. package/src/eslint/rules/one-component-per-file.mjs +70 -0
  177. package/src/eslint/rules/raw-element-in-primitive.mjs +31 -0
  178. package/src/eslint/rules/timeless-comment.mjs +92 -0
  179. package/src/eslint/rules.test.mjs +189 -0
  180. package/src/eslint/ui-regressions.test.mjs +39 -0
  181. package/src/gates/contract-operations.mjs +3 -3
  182. package/src/gates/contract-operations.test.mjs +28 -0
  183. package/src/gates/feature-switches.mjs +19 -0
  184. package/src/gates/feature-switches.test.mjs +55 -0
  185. package/src/gates/plain-language.mjs +396 -0
  186. package/src/gates/plain-language.test.mjs +210 -0
  187. package/src/gates/runtime-imports.test.mjs +114 -0
  188. package/src/gates/saga-key.test.mjs +1 -1
  189. package/src/global-skills.mjs +9 -0
  190. package/src/global-skills.test.mjs +53 -0
  191. package/src/plugin.test.mjs +3 -2
  192. package/src/ui/class-sites.mjs +38 -6
  193. package/src/ui/codemod.mjs +4 -1
  194. package/src/ui/codemod.test.mjs +52 -0
  195. package/src/ui/components.mjs +5 -3
  196. package/src/ui/components.test.mjs +8 -0
  197. package/src/ui/dupes.test.mjs +18 -0
  198. package/templates/docs/enforcement.md +12 -2
  199. package/templates/docs/ui.md +33 -0
  200. package/templates/githooks/pre-commit +1 -0
  201. package/templates/githooks/pre-push +11 -2
  202. package/templates/github/workflows/ci.yml +57 -3
  203. package/user-config/claude/CLAUDE.core.md +28 -0
  204. package/user-config/claude/settings.shared.json +41 -1
  205. package/src/cli/screens-discovery.mjs +0 -60
  206. package/src/cli/screens.mjs +0 -84
  207. package/src/cli/stack.mjs +0 -14
  208. package/src/cli/throwaway-db.mjs +0 -127
  209. package/src/cli/throwaway-db.test.mjs +0 -83
  210. package/src/screens/auth-context.test.mjs +0 -41
  211. package/src/screens/auth-run.test.mjs +0 -147
  212. package/src/screens/auth.mjs +0 -223
  213. package/src/screens/auth.test.mjs +0 -76
  214. package/src/screens/capture-idle.test.mjs +0 -27
  215. package/src/screens/capture.browser-test.mjs +0 -73
  216. package/src/screens/capture.mjs +0 -175
  217. package/src/screens/capture.test.mjs +0 -28
  218. package/src/screens/catalog.browser-test.mjs +0 -32
  219. package/src/screens/catalog.mjs +0 -72
  220. package/src/screens/catalog.test.mjs +0 -36
  221. package/src/screens/chrome.mjs +0 -21
  222. package/src/screens/compare.mjs +0 -40
  223. package/src/screens/compare.test.mjs +0 -30
  224. package/src/screens/discovery.browser-test.mjs +0 -29
  225. package/src/screens/discovery.mjs +0 -158
  226. package/src/screens/discovery.test.mjs +0 -27
  227. package/src/screens/inventory.mjs +0 -69
  228. package/src/screens/inventory.test.mjs +0 -10
  229. package/src/screens/map.mjs +0 -63
  230. package/src/screens/map.test.mjs +0 -35
  231. package/src/screens/open.browser-test.mjs +0 -50
  232. package/src/screens/regions.mjs +0 -137
  233. package/src/screens/regions.test.mjs +0 -38
  234. package/src/screens/session-files.mjs +0 -39
  235. package/src/screens/session-files.test.mjs +0 -39
  236. package/src/screens/setup.browser-test.mjs +0 -49
  237. package/src/screens/setup.mjs +0 -78
  238. package/src/screens/stack-auth.browser-test.mjs +0 -30
  239. package/src/screens/stack-auth.mjs +0 -106
  240. package/src/screens/stack-auth.test.mjs +0 -65
  241. package/src/screens/write-guard.mjs +0 -26
  242. package/src/stack/stack.mjs +0 -131
  243. package/src/stack/stack.test.mjs +0 -40
  244. package/src/stack/supervisor.mjs +0 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "architecture-harness",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "type": "module",
5
5
  "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
6
  "license": "MIT",
@@ -39,7 +39,6 @@
39
39
  "./eslint": "./src/eslint/index.mjs",
40
40
  "./preset": "./src/eslint/preset.mjs",
41
41
  "./config": "./src/config.mjs",
42
- "./screens": "./src/screens/catalog.mjs",
43
42
  "./gates/*": "./src/gates/*.mjs",
44
43
  "./package.json": "./package.json",
45
44
  "./ui/*": "./src/ui/*.mjs"
@@ -52,7 +51,7 @@
52
51
  ],
53
52
  "scripts": {
54
53
  "prepack": "node src/cli/user-config-package.mjs",
55
- "test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\"",
54
+ "test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\" ../../evals/agents/run.test.mjs",
56
55
  "test:browser": "node --test \"src/**/*.browser-test.mjs\""
57
56
  },
58
57
  "peerDependencies": {
@@ -64,10 +63,7 @@
64
63
  "@commitlint/lint": ">=19",
65
64
  "@commitlint/config-conventional": ">=19",
66
65
  "yaml": ">=2",
67
- "typescript": ">=5",
68
- "pixelmatch": ">=8",
69
- "playwright": ">=1.64",
70
- "pngjs": ">=7"
66
+ "typescript": ">=5"
71
67
  },
72
68
  "peerDependenciesMeta": {
73
69
  "@typescript-eslint/eslint-plugin": {
@@ -93,15 +89,6 @@
93
89
  },
94
90
  "typescript": {
95
91
  "optional": true
96
- },
97
- "pixelmatch": {
98
- "optional": true
99
- },
100
- "playwright": {
101
- "optional": true
102
- },
103
- "pngjs": {
104
- "optional": true
105
92
  }
106
93
  },
107
94
  "devDependencies": {
@@ -112,13 +99,7 @@
112
99
  "eslint": "^9",
113
100
  "eslint-plugin-boundaries": "^5",
114
101
  "eslint-plugin-sonarjs": "^3",
115
- "pixelmatch": "^8.0.0",
116
- "playwright": "^1.64.1",
117
- "pngjs": "^7.0.0",
118
102
  "typescript": "^5",
119
103
  "yaml": "^2.9.1"
120
- },
121
- "dependencies": {
122
- "cross-spawn": "7.0.6"
123
104
  }
124
105
  }
@@ -0,0 +1,42 @@
1
+ import { runGhResult } from "./gh-run.mjs";
2
+ import { originOf } from "./git-read.mjs";
3
+
4
+ const USAGE = 'Usage: qc ask --issue <n> "<question>"';
5
+
6
+ /** Label an issue and post one question in the checkout's repository. */
7
+ export function askIssue(issue, question, { cwd = process.cwd(), repo = originOf(cwd), gh } = {}) {
8
+ if (!/^[1-9]\d*$/.test(String(issue)) || typeof question !== "string" || !question.trim()) throw new Error(USAGE);
9
+ if (!repo) throw new Error("qc ask: cannot read the repository from origin");
10
+ const invoke = (args) => {
11
+ const command = [...args, "-R", repo];
12
+ if (gh) {
13
+ const answer = gh(command);
14
+ if (answer == null) throw new Error("gh returned no answer");
15
+ return answer;
16
+ }
17
+ const result = runGhResult(command, { cwd });
18
+ if (result.status !== 0) throw new Error((result.stderr?.trim() || result.error?.message || `gh exited ${result.status}`).split(/\r?\n/)[0]);
19
+ return result.stdout;
20
+ };
21
+ const labels = JSON.parse(invoke(["label", "list", "--search", "needs-user", "--json", "name", "--limit", "100"]));
22
+ if (!labels.some((label) => label.name === "needs-user")) {
23
+ invoke(["label", "create", "needs-user", "--color", "D876E3", "--description", "Needs an answer from the user"]);
24
+ }
25
+ invoke(["issue", "edit", String(issue), "--add-label", "needs-user"]);
26
+ invoke(["issue", "comment", String(issue), "--body", question]);
27
+ }
28
+
29
+ /** `qc ask --issue <n> "<question>"`. */
30
+ export function runAsk(args, { warn = console.error, ...deps } = {}) {
31
+ if (args.length !== 3 || args[0] !== "--issue" || !/^[1-9]\d*$/.test(args[1]) || !args[2].trim() || args[2].startsWith("--")) {
32
+ warn(USAGE);
33
+ return 1;
34
+ }
35
+ try {
36
+ askIssue(args[1], args[2], deps);
37
+ return 0;
38
+ } catch (error) {
39
+ warn(String(error.message).split(/\r?\n/)[0]);
40
+ return 1;
41
+ }
42
+ }
@@ -0,0 +1,40 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { askIssue, runAsk } from "./ask.mjs";
4
+
5
+ test("ask creates a missing needs-user label and posts one question on the explicit issue", () => {
6
+ for (const exists of [false, true]) {
7
+ const calls = [];
8
+ const question = "Which option?\nKeep the existing data?";
9
+ assert.equal(runAsk(["--issue", "227", question], {
10
+ repo: "local/repo", say: () => {},
11
+ gh: (args) => { calls.push(args); return args[1] === "list" ? JSON.stringify(exists ? [{ name: "needs-user" }] : []) : ""; },
12
+ }), 0);
13
+ assert.equal(calls.filter((args) => args[0] === "label" && args[1] === "create").length, exists ? 0 : 1);
14
+ assert.ok(calls.every((args) => args[args.indexOf("-R") + 1] === "local/repo"));
15
+ assert.deepEqual(calls.filter((args) => args[0] === "issue"), [
16
+ ["issue", "edit", "227", "--add-label", "needs-user", "-R", "local/repo"],
17
+ ["issue", "comment", "227", "--body", question, "-R", "local/repo"],
18
+ ]);
19
+ }
20
+ });
21
+
22
+ test("ask rejects malformed arguments before calling GitHub", () => {
23
+ for (const args of [[], ["--issue", "0", "why?"], ["--issue", "2x", "why?"], ["--issue", "2", " "], ["--issue", "2", "why?", "extra"], ["--issue", "2", "--unknown"]]) {
24
+ assert.equal(runAsk(args, { gh: () => assert.fail("invalid arguments reached gh"), warn: () => {} }), 1);
25
+ }
26
+ });
27
+
28
+ test("ask exits 1 with the first GitHub error line and stops after the failed call", () => {
29
+ for (let failAt = 0; failAt < 4; failAt++) {
30
+ const warnings = [];
31
+ let calls = 0;
32
+ assert.equal(runAsk(["--issue", "227", "Which option?"], {
33
+ repo: "local/repo", warn: (line) => warnings.push(line),
34
+ gh: () => { if (calls++ === failAt) throw new Error("GitHub unavailable\nprivate details"); return "[]"; },
35
+ }), 1);
36
+ assert.equal(calls, failAt + 1);
37
+ assert.deepEqual(warnings, ["GitHub unavailable"]);
38
+ }
39
+ assert.throws(() => askIssue(227, "Why?", { repo: "local/repo", gh: () => null }), /gh/);
40
+ });
@@ -0,0 +1,126 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { realpathSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { parseArgs } from "node:util";
5
+ import { globMatcher } from "../glob.mjs";
6
+ import { runGateSlot } from "./gate-slot.mjs";
7
+ import { git, lines, runShell, tryGit } from "./integrate.mjs";
8
+ import { posixDir, renumber } from "./migrations.mjs";
9
+ import { pauseRefusal } from "./pause.mjs";
10
+
11
+ export const USAGE = "qc branch sync <worktree> [--base <ref>] [--no-push]\nexit 5 paused or cannot read the operator pause";
12
+
13
+ /** A fragment: hold a machine slot while an action runs in its worktree. */
14
+ const inGateSlot = (words, _cwd, action) => runGateSlot(["--", ...words], { run: action });
15
+
16
+ /** A branch: refuse changes when the operator pause exists or cannot be read. */
17
+ function assertRunning(worktree) {
18
+ let refusal;
19
+ try {
20
+ refusal = pauseRefusal(worktree);
21
+ } catch (error) {
22
+ refusal = `cannot read the operator pause (${error.message}). Wait for the operator to run \`qc resume\`.`;
23
+ }
24
+ if (refusal) throw Object.assign(new Error(refusal), { exitCode: 5 });
25
+ }
26
+
27
+ /** A branch: dispatch a sync and push its changes through a machine gate slot. */
28
+ export async function runBranch(config, argv, deps = {}) {
29
+ const {
30
+ say = (line) => console.log(line),
31
+ run,
32
+ withSlot = inGateSlot,
33
+ runSlot = (words, cwd) => withSlot(words, cwd, () =>
34
+ spawnSync(words[0], words.slice(1), { cwd, stdio: "inherit", windowsHide: true }).status ?? 1),
35
+ } = deps;
36
+ let values;
37
+ let positionals;
38
+ try {
39
+ if (argv[0] !== "sync") throw new Error("expected the sync sub-command");
40
+ ({ values, positionals } = parseArgs({
41
+ args: argv.slice(1),
42
+ options: { base: { type: "string" }, "no-push": { type: "boolean" } },
43
+ strict: true,
44
+ allowPositionals: true,
45
+ }));
46
+ if (positionals.length !== 1) throw new Error("name exactly one worktree");
47
+ } catch (error) {
48
+ say(`qc branch: ${error.message}\n\n${USAGE}`);
49
+ return 2;
50
+ }
51
+ try {
52
+ const worktree = path.resolve(config.root, positionals[0]);
53
+ const result = await syncBranch(config, worktree, { base: values.base, say, run, withSlot });
54
+ if (result.code !== 0 || !result.changed || values["no-push"]) return result.code;
55
+ assertRunning(config.root);
56
+ say(`push ${result.branch}`);
57
+ return await runSlot(["git", "push", "-u", "origin", result.branch], worktree);
58
+ } catch (error) {
59
+ say(`qc branch: ${error.message}`);
60
+ return error.exitCode === 5 ? 5 : 1;
61
+ }
62
+ }
63
+
64
+ /** A pipeline: merge the base, renumber the branch's migrations, and commit generated files. */
65
+ export async function syncBranch(config, worktree, options = {}) {
66
+ assertRunning(config.root);
67
+ const { say = (line) => console.log(line), run = runShell, withSlot = inGateSlot } = options;
68
+ const base = options.base ?? config.worktree?.base ?? "origin/main";
69
+ if (tryGit(worktree, ["rev-parse", "-q", "--verify", "MERGE_HEAD"]).ok) throw new Error("a merge is in progress: resolve it and commit it first");
70
+ const branch = git(worktree, ["branch", "--show-current"]).trim();
71
+ if (!branch) throw new Error("HEAD is detached: switch to a branch first");
72
+ if ((config.swarm?.isolation?.protectedBranches ?? []).includes(branch)) throw new Error(`cannot sync protected branch ${branch}`);
73
+ if (git(worktree, ["status", "--porcelain"]).trim() !== "") throw new Error("the working tree has changes: commit or stash them first");
74
+ const [targetCommon, rootCommon] = [worktree, config.root].map((directory) => {
75
+ const common = realpathSync.native(git(directory, ["rev-parse", "--path-format=absolute", "--git-common-dir"]).trim());
76
+ return process.platform === "win32" ? common.toLowerCase() : common;
77
+ });
78
+ if (targetCommon !== rootCommon) throw new Error("the target belongs to another repository: choose a worktree of the repository root");
79
+ tryGit(worktree, ["fetch", "--quiet", "origin"]);
80
+ if (!tryGit(worktree, ["rev-parse", "-q", "--verify", `${base}^{commit}`]).ok) throw new Error(`unknown base ${base}`);
81
+ const alreadyMerged = tryGit(worktree, ["merge-base", "--is-ancestor", base, "HEAD"]).ok;
82
+ if (alreadyMerged) {
83
+ say(`OK ${branch} already holds ${base}`);
84
+ const upstream = tryGit(worktree, ["rev-parse", "--verify", "@{upstream}"]);
85
+ if (upstream.ok && git(worktree, ["rev-list", "--count", `${upstream.out.trim()}..HEAD`]).trim() === "0") {
86
+ return { code: 0, changed: false };
87
+ }
88
+ }
89
+
90
+ if (!alreadyMerged) {
91
+ const merged = tryGit(worktree, ["merge", "--no-ff", "--no-commit", base]);
92
+ if (!merged.ok) {
93
+ const conflicts = lines(git(worktree, ["diff", "--name-only", "--diff-filter=U"]));
94
+ if (conflicts.length === 0) throw new Error(`git merge ${base}: ${merged.err}`);
95
+ const generated = globMatcher(config.generated?.globs ?? []);
96
+ const byHand = conflicts.filter((file) => !generated(file) || !config.generated?.command);
97
+ if (byHand.length > 0) {
98
+ say(`CONFLICT merging ${base}; the merge is left in progress. Files:`);
99
+ for (const file of byHand) say(` ${file}`);
100
+ if (!config.generated?.command && conflicts.some(generated)) say("Set generated.command or resolve the generated files by hand.");
101
+ say(`Resolve them, run \`qc migrations renumber --onto ${base}\`, commit, and run qc branch sync again.`);
102
+ return { code: 1 };
103
+ }
104
+ git(worktree, ["checkout", "--ours", "--", ...conflicts]);
105
+ git(worktree, ["add", "--", ...conflicts]);
106
+ }
107
+ const dir = posixDir(config.paths?.migrations);
108
+ if (dir) {
109
+ for (const { from, to } of renumber(worktree, dir, { onto: base, branch: "HEAD" }).plan) say(`rename ${dir}/${from} -> ${to}`);
110
+ }
111
+ const commit = ["commit", "-q", "--no-edit"];
112
+ await withSlot(["git", ...commit], worktree, () => git(worktree, commit));
113
+ }
114
+
115
+ const command = config.generated?.command;
116
+ if (command) {
117
+ if (await withSlot([command], worktree, () => run(command, worktree)) !== 0) throw new Error(`generated.command failed: ${command} failed`);
118
+ if (git(worktree, ["status", "--porcelain"]).trim() !== "") {
119
+ git(worktree, ["add", "-A"]);
120
+ const issue = /^[a-z]+\/(\d+)-/.exec(branch)?.[1];
121
+ const commit = ["commit", "-q", "-m", "chore: regenerate the generated files", ...(issue ? ["-m", `Refs #${issue}`] : [])];
122
+ await withSlot(["git", ...commit], worktree, () => git(worktree, commit));
123
+ }
124
+ }
125
+ return { code: 0, changed: true, branch };
126
+ }