@cbartens/bkit 6.1.1 → 6.2.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 (313) hide show
  1. package/.bkit/README.md +14 -12
  2. package/.bkit/core/.agents/docs.md +1 -11
  3. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-gateguard.cjs +30 -9
  4. package/.bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs +25 -27
  5. package/.bkit/core/.agents/rules.md +22 -9
  6. package/.bkit/core/.bkit/README.md +13 -11
  7. package/.bkit/core/.bkit/authoring.md +58 -13
  8. package/.bkit/core/.bkit/cli.md +42 -32
  9. package/.bkit/core/.bkit/cloud.md +93 -30
  10. package/.bkit/core/.bkit/install.md +55 -16
  11. package/.bkit/core/.bkit/jsonl.md +24 -7
  12. package/.bkit/core/.bkit/pipeline.md +8 -8
  13. package/.bkit/core/.bkit/prompts.md +160 -43
  14. package/.bkit/core/.bkit/providers.md +97 -36
  15. package/.bkit/core/.bkit/templates.md +162 -34
  16. package/.bkit/core/.bkit/trackers.md +39 -6
  17. package/.bkit/core/.bkit/workflows.md +344 -136
  18. package/.bkit/core/.claude/hooks/bkit-guardrails.cjs +50 -27
  19. package/.bkit/core/.claude/hooks/bkit-preflight.sh +11 -11
  20. package/.bkit/core/.codex/hooks/bkit-gateguard.cjs +13 -8
  21. package/.bkit/core/.codex/hooks/bkit-guardrails.cjs +14 -25
  22. package/.bkit/core/.cursor/hooks/bkit-guardrails.cjs +20 -9
  23. package/.bkit/core/.cursor/hooks/bkit-preflight.sh +8 -8
  24. package/.bkit/core/.opencode/plugin/bkit-gateguard.js +14 -4
  25. package/.bkit/core/.opencode/plugin/bkit-guardrails.js +114 -0
  26. package/.bkit/core/docs/templates/bkit-audit-accessibility.md +1 -1
  27. package/.bkit/core/docs/templates/bkit-audit-adoption.md +30 -6
  28. package/.bkit/core/docs/templates/bkit-audit-agents.md +1 -1
  29. package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +1 -1
  30. package/.bkit/core/docs/templates/bkit-audit-analytics.md +1 -1
  31. package/.bkit/core/docs/templates/bkit-audit-annotations.md +1 -1
  32. package/.bkit/core/docs/templates/bkit-audit-app-performance.md +1 -1
  33. package/.bkit/core/docs/templates/bkit-audit-app-store.md +1 -1
  34. package/.bkit/core/docs/templates/bkit-audit-architecture.md +1 -1
  35. package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +2 -2
  36. package/.bkit/core/docs/templates/bkit-audit-config.md +3 -2
  37. package/.bkit/core/docs/templates/bkit-audit-cost.md +1 -1
  38. package/.bkit/core/docs/templates/bkit-audit-coverage.md +1 -1
  39. package/.bkit/core/docs/templates/bkit-audit-database.md +3 -3
  40. package/.bkit/core/docs/templates/bkit-audit-dead-code.md +1 -1
  41. package/.bkit/core/docs/templates/bkit-audit-deferrals.md +1 -1
  42. package/.bkit/core/docs/templates/bkit-audit-dependencies.md +3 -3
  43. package/.bkit/core/docs/templates/bkit-audit-documentation.md +32 -2
  44. package/.bkit/core/docs/templates/bkit-audit-duplication.md +1 -1
  45. package/.bkit/core/docs/templates/bkit-audit-failures.md +1 -1
  46. package/.bkit/core/docs/templates/bkit-audit-flakes.md +2 -2
  47. package/.bkit/core/docs/templates/bkit-audit-gates.md +5 -3
  48. package/.bkit/core/docs/templates/bkit-audit-issues.md +1 -1
  49. package/.bkit/core/docs/templates/bkit-audit-llm.md +2 -2
  50. package/.bkit/core/docs/templates/bkit-audit-logs.md +1 -1
  51. package/.bkit/core/docs/templates/bkit-audit-models.md +2 -2
  52. package/.bkit/core/docs/templates/bkit-audit-privacy.md +1 -1
  53. package/.bkit/core/docs/templates/bkit-audit-prompts.md +1 -1
  54. package/.bkit/core/docs/templates/bkit-audit-red-team.md +2 -2
  55. package/.bkit/core/docs/templates/bkit-audit-reliability.md +3 -2
  56. package/.bkit/core/docs/templates/bkit-audit-repo.md +1 -1
  57. package/.bkit/core/docs/templates/bkit-audit-security.md +1 -1
  58. package/.bkit/core/docs/templates/bkit-audit-slop.md +1 -1
  59. package/.bkit/core/docs/templates/bkit-audit-testing.md +3 -2
  60. package/.bkit/core/docs/templates/bkit-audit-usability.md +1 -1
  61. package/.bkit/core/docs/templates/bkit-plan.md +38 -8
  62. package/.bkit/core/engine/checks/after/auth.ts +118 -47
  63. package/.bkit/core/engine/checks/after/github.ts +89 -37
  64. package/.bkit/core/engine/checks/after/index.ts +15 -3
  65. package/.bkit/core/engine/checks/after/tools.ts +245 -1
  66. package/.bkit/core/engine/checks/before/files.ts +59 -1
  67. package/.bkit/core/engine/checks/before/index.ts +8 -0
  68. package/.bkit/core/engine/checks/status-holes.ts +20 -26
  69. package/.bkit/core/engine/checks/tips/catalogue.ts +107 -10
  70. package/.bkit/core/engine/checks/tips/detect.ts +36 -3
  71. package/.bkit/core/engine/checks/tips/index.ts +1 -1
  72. package/.bkit/core/engine/cli.ts +13 -13
  73. package/.bkit/core/engine/commands/ci/ci.ts +64 -11
  74. package/.bkit/core/engine/commands/ci/push-gate.ts +101 -43
  75. package/.bkit/core/engine/commands/install/install.ts +28 -25
  76. package/.bkit/core/engine/commands/install/uninstall.ts +42 -30
  77. package/.bkit/core/engine/commands/install/upgrade.ts +14 -2
  78. package/.bkit/core/engine/commands/land.ts +2 -3
  79. package/.bkit/core/engine/commands/lib.ts +394 -39
  80. package/.bkit/core/engine/commands/registry.ts +1 -1
  81. package/.bkit/core/engine/commands/workflow/branches.ts +351 -78
  82. package/.bkit/core/engine/commands/workflow/challenge.ts +63 -14
  83. package/.bkit/core/engine/commands/workflow/context.ts +71 -10
  84. package/.bkit/core/engine/commands/workflow/counters.ts +298 -23
  85. package/.bkit/core/engine/commands/workflow/decisions.ts +260 -125
  86. package/.bkit/core/engine/commands/workflow/disk.ts +2 -1
  87. package/.bkit/core/engine/commands/workflow/feature-base.ts +66 -0
  88. package/.bkit/core/engine/commands/workflow/issues.ts +312 -17
  89. package/.bkit/core/engine/commands/workflow/junit.ts +204 -0
  90. package/.bkit/core/engine/commands/workflow/ledgers.ts +904 -99
  91. package/.bkit/core/engine/commands/workflow/locks.ts +186 -29
  92. package/.bkit/core/engine/commands/workflow/loops.ts +87 -0
  93. package/.bkit/core/engine/commands/workflow/open-decision.ts +251 -0
  94. package/.bkit/core/engine/commands/workflow/plan-blocks.ts +8 -0
  95. package/.bkit/core/engine/commands/workflow/plans.ts +1 -1
  96. package/.bkit/core/engine/commands/workflow/prs.ts +311 -16
  97. package/.bkit/core/engine/commands/workflow/repo.ts +1 -1
  98. package/.bkit/core/engine/commands/workflow/run-facts.ts +6 -0
  99. package/.bkit/core/engine/commands/workflow/runs.ts +877 -174
  100. package/.bkit/core/engine/commands/workflow/runtime.ts +12 -4
  101. package/.bkit/core/engine/commands/workflow/status.ts +324 -11
  102. package/.bkit/core/engine/commands/workflow/step-table.ts +41 -15
  103. package/.bkit/core/engine/commands/workflow/steps.ts +604 -29
  104. package/.bkit/core/engine/commands/workflow/summary-findings.ts +1 -1
  105. package/.bkit/core/engine/commands/workflow/summary.ts +314 -8
  106. package/.bkit/core/engine/commands/workflow/tasks.ts +400 -27
  107. package/.bkit/core/engine/commands/workflow/tests-base.ts +319 -0
  108. package/.bkit/core/engine/commands/workflow/tests-changed.ts +138 -74
  109. package/.bkit/core/engine/commands/workflow/tests-jump.ts +93 -0
  110. package/.bkit/core/engine/commands/workflow/tests.ts +815 -182
  111. package/.bkit/core/engine/commands/workflow/tracker.ts +1134 -137
  112. package/.bkit/core/engine/commands/workflow/workflows.ts +100 -72
  113. package/.bkit/core/engine/commands/workflow/worktrees.ts +421 -57
  114. package/.bkit/core/engine/install/cloud-env.ts +38 -10
  115. package/.bkit/core/engine/install/managed-block.ts +105 -0
  116. package/.bkit/core/engine/install/manifest.ts +7 -1
  117. package/.bkit/core/engine/install/modules/claude.ts +6 -2
  118. package/.bkit/core/engine/install/modules/cloud.ts +84 -95
  119. package/.bkit/core/engine/install/modules/codex.ts +10 -2
  120. package/.bkit/core/engine/install/modules/core.ts +148 -14
  121. package/.bkit/core/engine/install/modules/opencode.ts +18 -23
  122. package/.bkit/core/engine/install/network-allowlist.ts +8 -3
  123. package/.bkit/core/engine/install/project-docs.ts +650 -0
  124. package/.bkit/core/engine/install/template-copies.ts +58 -5
  125. package/.bkit/core/engine/install/write.ts +128 -7
  126. package/.bkit/core/engine/ledgers/journal.ts +18 -10
  127. package/.bkit/core/engine/ledgers/migrate.ts +20 -8
  128. package/.bkit/core/engine/ledgers/status.ts +604 -8
  129. package/.bkit/core/engine/ledgers/telemetry.ts +115 -13
  130. package/.bkit/core/engine/render/trackers.ts +3 -1
  131. package/.bkit/core/engine/shared/package-manager.ts +7 -3
  132. package/.bkit/core/engine/shared/settings.ts +12 -1
  133. package/.bkit/core/engine/shared/text.ts +14 -0
  134. package/.bkit/core/engine/tools/instructions-sources.ts +1 -0
  135. package/.bkit/core/engine/tools/instructions.ts +116 -20
  136. package/.bkit/core/hooks/antigravity-hook.js +20 -4
  137. package/.bkit/core/hooks/gateguard-edit-payload.js +8 -5
  138. package/.bkit/core/hooks/gateguard-engine.js +249 -23
  139. package/.bkit/core/hooks/guardrails-deny.js +167 -0
  140. package/.bkit/core/hooks/guardrails-read.js +3 -3
  141. package/.bkit/core/hooks/guardrails-shell.js +1122 -167
  142. package/.bkit/core/hooks/guardrails-tiers.js +247 -176
  143. package/.bkit/core/hooks/preflight-tracker.sh +219 -0
  144. package/.bkit/core/prompts/bkit-audit-generate.md +23 -6
  145. package/.bkit/core/prompts/bkit-build-challenge-1.md +5 -4
  146. package/.bkit/core/prompts/bkit-build-challenge-2.md +6 -5
  147. package/.bkit/core/prompts/bkit-build-install-skills.md +2 -2
  148. package/.bkit/core/prompts/bkit-build-plan-convert.md +21 -7
  149. package/.bkit/core/prompts/bkit-build-plan-current.md +5 -4
  150. package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +22 -17
  151. package/.bkit/core/prompts/bkit-build-plan-tasks.md +16 -12
  152. package/.bkit/core/prompts/bkit-build-plan-tests.md +14 -10
  153. package/.bkit/core/prompts/bkit-build-plan-verify.md +21 -8
  154. package/.bkit/core/prompts/bkit-debug-abort-handover.md +10 -5
  155. package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +22 -6
  156. package/.bkit/core/prompts/bkit-debug-failure-remediate.md +19 -3
  157. package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +22 -6
  158. package/.bkit/core/prompts/bkit-debug-failure-soak.md +10 -7
  159. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +3 -3
  160. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +4 -4
  161. package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +3 -3
  162. package/.bkit/core/prompts/bkit-design-generate.md +6 -4
  163. package/.bkit/core/prompts/bkit-design-organise.md +4 -3
  164. package/.bkit/core/prompts/bkit-design-review-1.md +5 -4
  165. package/.bkit/core/prompts/bkit-design-review-2.md +6 -5
  166. package/.bkit/core/prompts/bkit-design-spike.md +4 -3
  167. package/.bkit/core/prompts/bkit-design-update.md +6 -4
  168. package/.bkit/core/prompts/bkit-plan-generate.md +35 -14
  169. package/.bkit/core/prompts/bkit-plan-review-1.md +9 -7
  170. package/.bkit/core/prompts/bkit-plan-review-2.md +9 -7
  171. package/.bkit/core/prompts/bkit-plan-scope-check.md +8 -6
  172. package/.bkit/core/prompts/bkit-plan-update.md +22 -8
  173. package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +11 -8
  174. package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +9 -5
  175. package/.bkit/core/prompts/bkit-ship-update-docs.md +26 -4
  176. package/.bkit/core/prompts/bkit-utility-compress-markdown.md +3 -3
  177. package/.bkit/core/prompts/bkit-utility-update-decisions.md +10 -7
  178. package/.bkit/core/prompts/bkit-utility-update-deferrals.md +8 -5
  179. package/.bkit/core/prompts/bkit-utility-update-documentation.md +31 -7
  180. package/.bkit/core/prompts/bkit-utility-update-failures.md +8 -5
  181. package/.bkit/core/prompts/bkit-utility-update-knowledge.md +10 -7
  182. package/.bkit/core/prompts/bkit-utility-update-roadmap.md +4 -4
  183. package/.bkit/core/prompts/bkit-utility-update-tracker.md +4 -4
  184. package/.bkit/core/workflows/bkit-auto.md +23 -11
  185. package/.bkit/core/workflows/bkit-build.md +331 -100
  186. package/.bkit/core/workflows/bkit-debug.md +135 -64
  187. package/.bkit/core/workflows/bkit-design.md +58 -32
  188. package/.bkit/core/workflows/bkit-plan.md +73 -45
  189. package/.bkit/core/workflows/bkit-ship.md +233 -167
  190. package/.bkit/shared/.agents/plugins/bkit/hooks.json +1 -1
  191. package/.bkit/shared/.bkit/.env.example +5 -0
  192. package/.bkit/shared/.claude/bkit-cloud.md +7 -7
  193. package/.bkit/shared/.claude/settings.json +5 -1
  194. package/.bkit/shared/.cursor/bkit-cloud.md +1 -1
  195. package/.bkit/shared/.cursor/hooks.json +1 -1
  196. package/.bkit/shared/.opencode/opencode.json +11 -2
  197. package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +27 -0
  198. package/.bkit/templates/.cursor/Dockerfile +7 -7
  199. package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-idea.yml +1 -1
  200. package/.bkit/templates/.github/dependabot.yml +5 -0
  201. package/.bkit/templates/.github/renovate.json5 +5 -3
  202. package/.bkit/templates/.github/workflows/bkit-ci.yml +11 -11
  203. package/.bkit/templates/.github/workflows/bkit-cloud.yml +525 -161
  204. package/.bkit/templates/.github/workflows/bkit-renovate.yml +2 -2
  205. package/.bkit/templates/.husky/pre-push +123 -6
  206. package/.bkit/templates/context/bkit-serena.md +1 -1
  207. package/.bkit/templates/docs/architecture.md +98 -0
  208. package/.bkit/templates/docs/capabilities.md +33 -37
  209. package/.bkit/templates/docs/components.md +142 -0
  210. package/.bkit/templates/docs/data-schema.md +87 -0
  211. package/.bkit/{shared → templates}/docs/deployments.md +128 -142
  212. package/.bkit/templates/docs/design.md +117 -0
  213. package/.bkit/templates/docs/noise.md +54 -0
  214. package/.bkit/templates/docs/observability.md +99 -0
  215. package/.bkit/templates/docs/security.md +137 -0
  216. package/.bkit/templates/docs/setup.md +93 -0
  217. package/.bkit/templates/docs/technology.md +70 -0
  218. package/.bkit/{shared → templates}/docs/testing.md +200 -87
  219. package/.bkit/templates/scripts/ci/bkit-machine-lock.sh +511 -103
  220. package/.bkit/templates/scripts/ci/bkit-test-awake.mjs +228 -35
  221. package/.bkit/templates/scripts/ci/bkit-test-awake.test.mjs +270 -2
  222. package/.bkit/templates/scripts/graphify/bkit-graphify-graphml.py +1 -1
  223. package/.bkit/templates/scripts/graphify/bkit-graphify-rollup.py +92 -63
  224. package/.bkit/templates/scripts/graphify/bkit-graphify.sh +59 -11
  225. package/.bkit/templates/slop-scan.config.json +2 -0
  226. package/CHANGELOG.md +64 -0
  227. package/DETAILS.md +24 -19
  228. package/README.md +1 -1
  229. package/dist/checks/after/auth.js +114 -46
  230. package/dist/checks/after/github.js +77 -35
  231. package/dist/checks/after/index.js +12 -3
  232. package/dist/checks/after/tools.js +224 -1
  233. package/dist/checks/before/files.js +59 -1
  234. package/dist/checks/before/index.js +7 -1
  235. package/dist/checks/status-holes.js +20 -21
  236. package/dist/checks/tips/catalogue.js +99 -10
  237. package/dist/checks/tips/detect.js +35 -3
  238. package/dist/checks/tips/index.js +1 -1
  239. package/dist/cli.js +13 -13
  240. package/dist/commands/ci/ci.js +56 -12
  241. package/dist/commands/ci/push-gate.js +79 -44
  242. package/dist/commands/install/install.js +23 -22
  243. package/dist/commands/install/uninstall.js +36 -30
  244. package/dist/commands/install/upgrade.js +10 -2
  245. package/dist/commands/land.js +2 -3
  246. package/dist/commands/lib.js +323 -25
  247. package/dist/commands/workflow/branches.js +312 -72
  248. package/dist/commands/workflow/challenge.js +66 -13
  249. package/dist/commands/workflow/context.js +46 -10
  250. package/dist/commands/workflow/counters.js +231 -23
  251. package/dist/commands/workflow/decisions.js +221 -106
  252. package/dist/commands/workflow/disk.js +2 -1
  253. package/dist/commands/workflow/feature-base.js +60 -0
  254. package/dist/commands/workflow/issues.js +243 -15
  255. package/dist/commands/workflow/junit.js +137 -0
  256. package/dist/commands/workflow/ledgers.js +726 -112
  257. package/dist/commands/workflow/locks.js +150 -29
  258. package/dist/commands/workflow/loops.js +67 -0
  259. package/dist/commands/workflow/open-decision.js +199 -0
  260. package/dist/commands/workflow/plan-blocks.js +6 -0
  261. package/dist/commands/workflow/plans.js +1 -1
  262. package/dist/commands/workflow/prs.js +272 -16
  263. package/dist/commands/workflow/repo.js +1 -1
  264. package/dist/commands/workflow/run-facts.js +6 -0
  265. package/dist/commands/workflow/runs.js +747 -155
  266. package/dist/commands/workflow/runtime.js +8 -4
  267. package/dist/commands/workflow/status.js +259 -13
  268. package/dist/commands/workflow/step-table.js +41 -15
  269. package/dist/commands/workflow/steps.js +530 -26
  270. package/dist/commands/workflow/summary-findings.js +1 -1
  271. package/dist/commands/workflow/summary.js +289 -8
  272. package/dist/commands/workflow/tasks.js +294 -27
  273. package/dist/commands/workflow/tests-base.js +266 -0
  274. package/dist/commands/workflow/tests-changed.js +130 -56
  275. package/dist/commands/workflow/tests-jump.js +77 -0
  276. package/dist/commands/workflow/tests.js +608 -131
  277. package/dist/commands/workflow/tracker.js +920 -112
  278. package/dist/commands/workflow/workflows.js +80 -59
  279. package/dist/commands/workflow/worktrees.js +387 -58
  280. package/dist/install/cloud-env.js +32 -10
  281. package/dist/install/managed-block.js +101 -0
  282. package/dist/install/manifest.js +7 -1
  283. package/dist/install/modules/claude.js +6 -2
  284. package/dist/install/modules/cloud.js +73 -86
  285. package/dist/install/modules/codex.js +9 -2
  286. package/dist/install/modules/core.js +133 -14
  287. package/dist/install/modules/opencode.js +18 -23
  288. package/dist/install/network-allowlist.js +8 -3
  289. package/dist/install/project-docs.js +551 -0
  290. package/dist/install/template-copies.js +55 -5
  291. package/dist/install/write.js +117 -5
  292. package/dist/ledgers/journal.js +10 -8
  293. package/dist/ledgers/migrate.js +20 -9
  294. package/dist/ledgers/status.js +461 -8
  295. package/dist/ledgers/telemetry.js +71 -13
  296. package/dist/render/trackers.js +3 -1
  297. package/dist/shared/package-manager.js +7 -2
  298. package/dist/shared/settings.js +8 -0
  299. package/dist/shared/text.js +13 -0
  300. package/dist/tools/instructions-sources.js +1 -0
  301. package/dist/tools/instructions.js +97 -24
  302. package/package.json +22 -15
  303. package/.bkit/core/hooks/preflight-linear.sh +0 -73
  304. package/.bkit/shared/docs/architecture.md +0 -16
  305. package/.bkit/shared/docs/data-schema.md +0 -16
  306. package/.bkit/shared/docs/design.md +0 -38
  307. package/.bkit/shared/docs/noise.md +0 -20
  308. package/.bkit/shared/docs/observability.md +0 -22
  309. package/.bkit/shared/docs/security.md +0 -62
  310. package/.bkit/shared/docs/setup.md +0 -52
  311. package/.bkit/shared/docs/technology.md +0 -15
  312. /package/.bkit/templates/{.lintstagedrc.json → lintstagedrc.example.json} +0 -0
  313. /package/.bkit/templates/{.prettierignore → prettierignore.example} +0 -0
@@ -17,21 +17,23 @@ import * as fs from "node:fs";
17
17
  import * as os from "node:os";
18
18
  import * as path from "node:path";
19
19
  import { gitEnv } from "../../ledgers/telemetry.js";
20
- import { actionHash, actionKey, alreadyDone, recordAction } from "../../ledgers/status.js";
21
- import { bodySkipToken, classifyLockExit, CommandError, DEV, EXIT, ghUnavailable, git, isSharedBranch, OUTPUT_LIMIT, parseJson, repoRoot, runGh, runGit, SKIP_CI_TOKEN, testsLockHolder, wholeNumber, } from "../lib.js";
20
+ import { actionHash, actionKey, alreadyDone, recordAction, withTargetLock, } from "../../ledgers/status.js";
21
+ import { bodySkipToken, CommandError, DEV, EXIT, ghUnavailable, git, isSharedBranch, OUTPUT_LIMIT, parseJson, repoRoot, runGh, runGit, SKIP_CI_TOKEN, wholeNumber, } from "../lib.js";
22
22
  import { bkitVersionStatus } from "../../shared/bkit-latest.js";
23
23
  import { parseBkitVersion, serializeBkitVersion, } from "../../install/version-file.js";
24
24
  import { DEV_REFSPEC, fetchDev, landBackoffMs, landDir, pushToDev, sleep } from "../land.js";
25
25
  import { TIMEOUTS } from "../../shared/timeouts.js";
26
- import { activeRecord, commitBookkeeping, markRecordPushed, recordOnBranch, } from "./ledgers.js";
26
+ import { activeRecord, bookPathspecs, checkOnlyBookkeeping, commitBookkeeping, markRecordPushed, recordOnBranch, } from "./ledgers.js";
27
27
  import { releaseLock, resolveMonth, workflowLockPath } from "./locks.js";
28
+ import { underTestsLock } from "./junit.js";
28
29
  import { EXIT_BASE_NOT_DEV, FORCE_BUILD, closeTransportRow, crossCheckRows, mergePr, openPrFor, probePr, } from "./prs.js";
29
30
  import { AUTO_LABEL, AUTO_ROUTE_SUFFIX } from "./issues.js";
30
31
  import { appendFeature } from "./roadmap.js";
31
32
  import { detect } from "./runtime.js";
33
+ import { defaultRange } from "./feature-base.js";
32
34
  import { readRuns, readStatusRows, readStatusRowsAt } from "./status.js";
33
35
  import { declaredName, testSource } from "./test-blocks.js";
34
- import { addAllButCopied, copyExcludes, hasLeftovers, recordRebase, removeWorktree, worktreeDir, } from "./worktrees.js";
36
+ import { addAllButCopied, assertShipEligible, copyExcludes, hasLeftovers, notOnDev, rebaseNoteFile, recordRebase, removeWorktree, worktreeDir, } from "./worktrees.js";
35
37
  import { COULD_NOT_RUN_EXIT_CODES, LOCKFILE_REGENERATE, installCommand, scriptCommand, } from "../../shared/package-manager.js";
36
38
  import { readPackageJson } from "../../shared/package-json.js";
37
39
  import { str } from "./status.js";
@@ -472,12 +474,16 @@ export function resolveRebase(top, onLog = () => undefined) {
472
474
  return refuse("REFUSE — git rebase --skip failed", null, "git rebase --skip failed");
473
475
  }
474
476
  }
475
- else if (!quietGit(top, ["rebase", "--continue"]).ok) {
477
+ else {
478
+ const cont = quietGit(top, ["rebase", "--continue"]);
476
479
  // The next commit conflicted (next round handles it), or the rebase is stuck.
477
- if (unmergedPaths(top).length === 0) {
478
- return refuse("REFUSE — git rebase --continue failed with no unmerged paths", null, "git rebase --continue failed with no unmerged paths");
480
+ if (!cont.ok && unmergedPaths(top).length === 0) {
481
+ const why = (cont.stderr || cont.stdout).replace(/\s*\n\s*/g, " ");
482
+ const reason = `git rebase --continue failed with no unmerged paths: ${why || "git gave no reason"}`;
483
+ return refuse(`REFUSE — ${reason}`, null, reason);
479
484
  }
480
- continue;
485
+ if (!cont.ok)
486
+ continue;
481
487
  }
482
488
  if (!rebaseInProgress(top)) {
483
489
  log(`COMPLETE — rebase finished after ${result.rounds} auto-resolved round(s)`);
@@ -601,12 +607,23 @@ function topLevel(cwd) {
601
607
  throw new CommandError(EXIT.USAGE, `not inside a git work tree: ${cwd}`);
602
608
  return top;
603
609
  }
604
- function appendLog(ctx, file, lines) {
605
- if (file === undefined || lines.length === 0)
610
+ /**
611
+ * Appends the resolution lines to the run's log, after the note `worktrees setup`
612
+ * held outside the tree while the rebase was paused (`rebaseNoteFile`), then drops
613
+ * the note. Called only once the rebase has finished or been aborted.
614
+ */
615
+ function appendLog(ctx, top, file, lines) {
616
+ if (file === undefined)
617
+ return;
618
+ const noteFile = rebaseNoteFile(top);
619
+ const note = noteFile !== null && fs.existsSync(noteFile) ? fs.readFileSync(noteFile, "utf-8") : "";
620
+ if (note === "" && lines.length === 0)
606
621
  return;
607
622
  const abs = path.resolve(ctx.cwd, file);
608
623
  fs.mkdirSync(path.dirname(abs), { recursive: true });
609
- fs.appendFileSync(abs, lines.map((l) => `[bkit-rebase] ${l}\n`).join(""));
624
+ fs.appendFileSync(abs, note + lines.map((l) => `[bkit-rebase] ${l}\n`).join(""));
625
+ if (noteFile !== null)
626
+ fs.rmSync(noteFile, { force: true });
610
627
  }
611
628
  function rebaseVerb(ctx) {
612
629
  const top = topLevel(ctx.cwd);
@@ -639,7 +656,7 @@ function rebaseVerb(ctx) {
639
656
  ctx.log(line);
640
657
  });
641
658
  }
642
- appendLog(ctx, str(ctx.flags.logFile), result.log);
659
+ appendLog(ctx, top, str(ctx.flags.logFile), result.log);
643
660
  if (!result.ok && sentinelPath) {
644
661
  fs.mkdirSync(path.dirname(sentinelPath), { recursive: true });
645
662
  fs.writeFileSync(sentinelPath, "");
@@ -674,23 +691,23 @@ function rebaseOrigHead(top) {
674
691
  }
675
692
  return null;
676
693
  }
677
- /** Git collapses a failing pre-push hook's status; classify its exact tests-lock markers. */
694
+ /** Git collapses a failing pre-push hook's status, so the hook's tests-lock status file alone decides. */
678
695
  function runPush(cwd, args, branch) {
679
696
  const who = `bkit branches push ${branch}`;
680
697
  const wait = wholeNumber(process.env.BKIT_LOCK_WAIT_S, "BKIT_LOCK_WAIT_S", 60);
681
- const res = runGit(cwd, args, {
682
- LOCAL_STACK_LOCK_WAIT_S: String(wait),
683
- LOCAL_STACK_LOCK_WHO: who,
698
+ let res;
699
+ const { status, lock } = underTestsLock((lockEnv) => {
700
+ res = runGit(cwd, args, {
701
+ LOCAL_STACK_LOCK_WAIT_S: String(wait),
702
+ LOCAL_STACK_LOCK_WHO: who,
703
+ ...lockEnv,
704
+ });
705
+ return res.ok ? 0 : null;
684
706
  });
685
- const log = [res.stdout, res.stderr].filter(Boolean).join("\n");
686
- const lockHolder = res.ok ? null : testsLockHolder(log, who);
687
- const outcome = res.ok
688
- ? "gate"
689
- : classifyLockExit({ exitCode: lockHolder === null ? 73 : 75, log, who });
690
707
  return {
691
708
  ...res,
692
- lockHolder,
693
- lockExit: outcome === "lock-wait" ? EXIT.AGENT_WAIT : outcome === "could-not-run" ? 4 : null,
709
+ lockHolder: lock === "lock-wait" ? (status?.holder ?? null) : null,
710
+ lockExit: lock === "lock-wait" ? EXIT.AGENT_WAIT : lock === "could-not-run" ? 4 : null,
694
711
  };
695
712
  }
696
713
  /**
@@ -1351,7 +1368,7 @@ export function disabledTestsIn(diff, root) {
1351
1368
  return out;
1352
1369
  }
1353
1370
  function scanDiffVerb(ctx) {
1354
- const base = String(ctx.flags.base);
1371
+ const explicitBase = str(ctx.flags.base);
1355
1372
  const pattern = str(ctx.flags.paths);
1356
1373
  const marker = str(ctx.flags.marker);
1357
1374
  const code = ctx.flags.code === true;
@@ -1372,7 +1389,9 @@ function scanDiffVerb(ctx) {
1372
1389
  }
1373
1390
  }
1374
1391
  const top = topLevel(ctx.cwd);
1375
- const fetched = fetchRemoteRef(top, base);
1392
+ const fallback = explicitBase === undefined ? defaultRange(top, ctx.log) : null;
1393
+ const base = explicitBase ?? fallback.base;
1394
+ const fetched = explicitBase === undefined ? null : fetchRemoteRef(top, base);
1376
1395
  if (fetched !== null && !fetched.ok) {
1377
1396
  ctx.log(`git fetch for ${base} failed: ${fetched.stderr} — diffing against the local ref`);
1378
1397
  }
@@ -1380,7 +1399,7 @@ function scanDiffVerb(ctx) {
1380
1399
  ctx.log(`unknown base: ${base}`);
1381
1400
  return EXIT_DIFF_FAILED;
1382
1401
  }
1383
- const range = `${base}...HEAD`;
1402
+ const range = fallback?.range ?? `${base}...HEAD`;
1384
1403
  if (disabled) {
1385
1404
  const diff = quietGit(top, ["diff", "--no-renames", range, "--", ".", ":(exclude)docs/plans"]);
1386
1405
  if (!diff.ok) {
@@ -1827,6 +1846,8 @@ function mergeDev(top, o, log) {
1827
1846
  before: null,
1828
1847
  };
1829
1848
  const full = o.policy === "full";
1849
+ const target = o.target ?? UPSTREAM;
1850
+ const targetName = o.target ?? DEV;
1830
1851
  const fid = o.fid ?? o.branch.replace(/^feat\//, "");
1831
1852
  const refuse = (why) => {
1832
1853
  const abort = runGit(top, ["merge", "--abort"]);
@@ -1835,27 +1856,33 @@ function mergeDev(top, o, log) {
1835
1856
  return { ...out, code: EXIT_MERGE_CONFLICT, outcome: "refused" };
1836
1857
  };
1837
1858
  if (mergeInProgress(top)) {
1859
+ const merging = git(top, ["rev-parse", "--verify", "MERGE_HEAD"]);
1860
+ const expected = git(top, ["rev-parse", "--verify", `${target}^{commit}`]);
1861
+ if (merging === null || expected === null || merging !== expected) {
1862
+ log(`the pending merge targets ${merging ?? "an unreadable ref"}, not ${target}; finish that merge first`);
1863
+ return { ...out, code: EXIT_REFUSED, outcome: "refused" };
1864
+ }
1838
1865
  out.before = git(top, ["rev-parse", "--verify", "--quiet", "ORIG_HEAD"]);
1839
- log(`a merge of ${UPSTREAM} is in progress — continuing it`);
1866
+ log(`a merge of ${target} is in progress — continuing it`);
1840
1867
  }
1841
1868
  else {
1842
1869
  if (o.current)
1843
1870
  return { ...out, outcome: "current" };
1844
1871
  if (full && hasLeftovers(top)) {
1845
- const message = `chore(${fid}): carry uncommitted work before merging ${DEV}`;
1872
+ const message = `chore(${fid}): carry uncommitted work before merging ${targetName}`;
1846
1873
  if (!addAllButCopied(top).ok || !quietGit(top, ["commit", "-q", "-m", message]).ok) {
1847
1874
  log("the uncommitted work could not be committed before the merge");
1848
1875
  return { ...out, code: EXIT_REFUSED, outcome: "refused" };
1849
1876
  }
1850
1877
  out.carried = git(top, ["rev-parse", "HEAD"]);
1851
- log(`committed the uncommitted work as ${out.carried} before merging ${UPSTREAM}`);
1878
+ log(`committed the uncommitted work as ${out.carried} before merging ${target}`);
1852
1879
  }
1853
1880
  out.before = git(top, ["rev-parse", "HEAD"]);
1854
- const message = `chore(${fid}): merge ${DEV}`;
1855
- const merge = quietGit(top, ["merge", "--no-edit", "-m", message, UPSTREAM]);
1881
+ const message = `chore(${fid}): merge ${targetName}`;
1882
+ const merge = quietGit(top, ["merge", "--no-edit", "-m", message, target]);
1856
1883
  if (!merge.ok && !mergeInProgress(top)) {
1857
1884
  // Not a conflict: git refused to start the merge at all.
1858
- log(`git merge ${UPSTREAM} failed: ${merge.stderr.trim()}`);
1885
+ log(`git merge ${target} failed: ${merge.stderr.trim()}`);
1859
1886
  return { ...out, code: EXIT_MERGE_CONFLICT, outcome: "refused" };
1860
1887
  }
1861
1888
  }
@@ -1881,9 +1908,9 @@ function mergeDev(top, o, log) {
1881
1908
  return refuse("the merge could not be committed");
1882
1909
  }
1883
1910
  out.merged = true;
1884
- log(`merged ${UPSTREAM} into ${o.branch} as ${git(top, ["rev-parse", "HEAD"])}`);
1911
+ log(`merged ${target} into ${o.branch} as ${git(top, ["rev-parse", "HEAD"])}`);
1885
1912
  if (o.generate) {
1886
- out.rebuilt = rebuildRegisters(top, out.resolved, (files) => `chore(${fid}): rebuild ${files.join(" ")} after merging ${DEV}`, log);
1913
+ out.rebuilt = rebuildRegisters(top, out.resolved, (files) => `chore(${fid}): rebuild ${files.join(" ")} after merging ${targetName}`, log);
1887
1914
  out.generated = regenerate(top, o.branch, log, { fid });
1888
1915
  if (out.generated === false)
1889
1916
  out.code = EXIT_GENERATE_FAILED;
@@ -1905,9 +1932,192 @@ function recordMerge(statusFile, before, after) {
1905
1932
  ref: after,
1906
1933
  });
1907
1934
  }
1935
+ function reconcileFeature(ctx, top, branch, observed) {
1936
+ if (branch === "" || isSharedBranch(branch)) {
1937
+ throw new CommandError(EXIT.USAGE, "feature reconciliation needs a checked-out feature branch");
1938
+ }
1939
+ const remote = `${REMOTE}/${branch}`;
1940
+ const probeResult = spawnSync("git", ["ls-remote", "--exit-code", "--heads", REMOTE, branch], {
1941
+ cwd: top,
1942
+ encoding: "utf-8",
1943
+ env: gitEnv(),
1944
+ });
1945
+ const probe = { ok: probeResult.status === 0, stderr: probeResult.stderr ?? "" };
1946
+ if (!probe.ok && probeResult.status !== 2) {
1947
+ ctx.log(`cannot fetch ${remote}: ${probe.stderr}`);
1948
+ return EXIT_FETCH_FAILED;
1949
+ }
1950
+ if (probe.ok) {
1951
+ const fetched = fetchRemoteRef(top, remote);
1952
+ if (fetched === null || !fetched.ok) {
1953
+ ctx.log(`git fetch ${remote} failed: ${fetched?.stderr ?? "unknown remote ref"}`);
1954
+ return EXIT_FETCH_FAILED;
1955
+ }
1956
+ observed.remoteHead = git(top, [
1957
+ "rev-parse",
1958
+ "--verify",
1959
+ "--quiet",
1960
+ `refs/remotes/${remote}^{commit}`,
1961
+ ]);
1962
+ if (observed.remoteHead === null) {
1963
+ ctx.log(`cannot read the freshly fetched feature ${remote}`);
1964
+ return EXIT_FETCH_FAILED;
1965
+ }
1966
+ }
1967
+ assertShipEligible(top, "HEAD");
1968
+ if (probe.ok)
1969
+ assertShipEligible(top, remote);
1970
+ const headFlag = str(ctx.flags.head);
1971
+ if (headFlag !== undefined &&
1972
+ git(top, ["rev-parse", "--verify", "--quiet", `${headFlag}^{commit}`]) === null) {
1973
+ throw new CommandError(EXIT.USAGE, `--head ${headFlag} does not name a commit`);
1974
+ }
1975
+ if (!mergeInProgress(top) && hasLeftovers(top)) {
1976
+ const statusFile = str(ctx.flags.file);
1977
+ const relative = statusFile && path.relative(top, path.resolve(ctx.cwd, statusFile)).split(path.sep).join("/");
1978
+ const scope = relative && /^docs\/plans\/(\d{6})\/logs\/([^/]+)\/status\.jsonl$/.exec(relative);
1979
+ const fid = str(ctx.flags.fid) || branch.replace(/^feat\//, "");
1980
+ if (!scope || scope[2] !== fid) {
1981
+ ctx.log("feature branch has uncommitted work — commit that work before reconciliation");
1982
+ return EXIT_REFUSED;
1983
+ }
1984
+ const outside = runGit(top, [
1985
+ "status",
1986
+ "--porcelain=v1",
1987
+ "-z",
1988
+ "--untracked-files=all",
1989
+ "--",
1990
+ ".",
1991
+ ...copyExcludes(top),
1992
+ ...bookPathspecs(scope[1], fid, "core").map((spec) => `:(exclude)${spec}`),
1993
+ ]);
1994
+ if (!outside.ok || outside.stdout !== "") {
1995
+ ctx.log("feature branch has unrelated uncommitted work — commit that work before reconciliation");
1996
+ return EXIT_REFUSED;
1997
+ }
1998
+ const carried = commitBookkeeping({
1999
+ root: top,
2000
+ month: scope[1],
2001
+ fid,
2002
+ scope: "core",
2003
+ mode: "commit",
2004
+ message: `chore(${fid}): carry ship bookkeeping before reconciliation`,
2005
+ }, ctx.log);
2006
+ if (carried.outcome !== "committed") {
2007
+ ctx.log("the workflow bookkeeping could not be committed before reconciliation");
2008
+ return EXIT_REFUSED;
2009
+ }
2010
+ }
2011
+ const fid = str(ctx.flags.fid) || branch.replace(/^feat\//, "");
2012
+ const merge = (target, current) => {
2013
+ const m = mergeDev(top, { branch, target, policy: "full", fid, generate: true, current }, ctx.log);
2014
+ const after = git(top, ["rev-parse", "HEAD"]);
2015
+ const file = str(ctx.flags.file);
2016
+ if (m.merged && file !== undefined)
2017
+ recordMerge(path.resolve(ctx.cwd, file), m.before, after);
2018
+ if (m.code !== EXIT.OK) {
2019
+ ctx.emit({ branch, head: after, remoteHead: observed.remoteHead, ...m, target });
2020
+ }
2021
+ return m.code;
2022
+ };
2023
+ if (mergeInProgress(top)) {
2024
+ const pending = git(top, ["rev-parse", "--verify", "MERGE_HEAD"]);
2025
+ if (pending === null ||
2026
+ !probe.ok ||
2027
+ !runGit(top, ["merge-base", "--is-ancestor", pending, remote]).ok) {
2028
+ ctx.log("the pending merge is not the fetched feature merge — finish it before reconciliation");
2029
+ return EXIT_REFUSED;
2030
+ }
2031
+ const code = merge(pending, false);
2032
+ if (code !== EXIT.OK)
2033
+ return code;
2034
+ }
2035
+ if (probe.ok) {
2036
+ const local = git(top, ["rev-parse", "HEAD"]);
2037
+ if (local === null)
2038
+ return EXIT_REFUSED;
2039
+ const ancestor = (a, b) => spawnSync("git", ["merge-base", "--is-ancestor", a, b], { cwd: top, env: gitEnv() }).status;
2040
+ const behind = ancestor("HEAD", remote);
2041
+ const ahead = ancestor(remote, "HEAD");
2042
+ if ((behind !== 0 && behind !== 1) || (ahead !== 0 && ahead !== 1)) {
2043
+ ctx.log(`cannot compare the local feature with ${remote}`);
2044
+ return EXIT_REFUSED;
2045
+ }
2046
+ if (behind === 0 && ahead !== 0) {
2047
+ const ff = quietGit(top, ["merge", "--ff-only", remote]);
2048
+ if (!ff.ok) {
2049
+ ctx.log(`cannot fast-forward the feature: ${ff.stderr}`);
2050
+ return EXIT_REFUSED;
2051
+ }
2052
+ const file = str(ctx.flags.file);
2053
+ if (file !== undefined)
2054
+ recordMerge(path.resolve(ctx.cwd, file), local, git(top, ["rev-parse", "HEAD"]));
2055
+ }
2056
+ else if (ahead !== 0) {
2057
+ const code = merge(remote, false);
2058
+ if (code !== EXIT.OK)
2059
+ return code;
2060
+ }
2061
+ }
2062
+ assertShipEligible(top, "HEAD");
2063
+ const unpublished = !probe.ok || commitsAhead(top, remote) > 0;
2064
+ if (unpublished && SKIP_CI_TOKEN.test(git(top, ["log", "-1", "--format=%B"]) ?? "")) {
2065
+ const trigger = quietGit(top, [
2066
+ "commit",
2067
+ "--allow-empty",
2068
+ "-m",
2069
+ `chore(${fid}): validate reconciled feature`,
2070
+ ]);
2071
+ if (!trigger.ok) {
2072
+ ctx.log(`cannot create a CI-running feature head: ${trigger.stderr}`);
2073
+ return EXIT_REFUSED;
2074
+ }
2075
+ }
2076
+ return EXIT.OK;
2077
+ }
2078
+ /** Merge-aware coverage identity; omit a dev merge only when Git proves its result automatic. */
2079
+ function featureCodeState(top, head, month, fid) {
2080
+ const commits = runGit(top, ["rev-list", "--parents", `${UPSTREAM}..${head}`]);
2081
+ if (!commits.ok)
2082
+ throw new CommandError(EXIT_DIFF_FAILED, commits.stderr);
2083
+ const retained = [];
2084
+ for (const line of commits.stdout.split("\n").filter(Boolean)) {
2085
+ const [sha, first, second, ...other] = line.split(" ");
2086
+ if (!sha || !first)
2087
+ throw new CommandError(EXIT_DIFF_FAILED, "feature coverage needs readable commit parents");
2088
+ const bookkeeping = checkOnlyBookkeeping(top, { month, fid, from: first, to: sha });
2089
+ if (bookkeeping.kind === "ok")
2090
+ continue;
2091
+ if (bookkeeping.kind !== "outside")
2092
+ throw new CommandError(EXIT_DIFF_FAILED, `feature coverage classification: ${bookkeeping.kind}`);
2093
+ if (second && other.length === 0) {
2094
+ const devParent = runGit(top, ["merge-base", "--is-ancestor", second, UPSTREAM]);
2095
+ if (devParent.ok) {
2096
+ const automatic = git(top, ["merge-tree", "--write-tree", first, second]);
2097
+ const tree = git(top, ["rev-parse", `${sha}^{tree}`]);
2098
+ if (automatic && tree && automatic.split("\n")[0] === tree)
2099
+ continue;
2100
+ }
2101
+ }
2102
+ retained.push(sha);
2103
+ }
2104
+ return `v1:${retained.sort().join(",")}`;
2105
+ }
1908
2106
  function syncVerb(ctx) {
1909
2107
  const mergeDocs = ctx.flags.mergeDocs === true;
1910
2108
  const merge = ctx.flags.merge === true;
2109
+ const reconcile = ctx.flags.reconcileFeature === true;
2110
+ const featureState = ctx.flags.featureState === true;
2111
+ const month = str(ctx.flags.month);
2112
+ const coverageFid = str(ctx.flags.fid);
2113
+ if (featureState &&
2114
+ (mergeDocs || merge || reconcile || !month || !/^\d{6}$/.test(month) || !coverageFid))
2115
+ throw new CommandError(EXIT.USAGE, "--feature-state needs --month=YYYYMM and --fid, separately from reconciliation or merging");
2116
+ const targetFlag = str(ctx.flags.target);
2117
+ if (targetFlag !== undefined && (!merge || targetFlag === "" || targetFlag.startsWith("-")))
2118
+ throw new CommandError(EXIT.USAGE, "--target needs --merge and a commit ref");
2119
+ if (reconcile && (mergeDocs || merge))
2120
+ throw new CommandError(EXIT.USAGE, "give --reconcile-feature separately from --merge or --merge-docs");
1911
2121
  const headFlag = str(ctx.flags.head);
1912
2122
  if ((mergeDocs || merge) && headFlag !== undefined) {
1913
2123
  throw new CommandError(EXIT.USAGE, "--merge and --merge-docs merge into HEAD; give --head or one of them, not both");
@@ -1919,15 +2129,31 @@ function syncVerb(ctx) {
1919
2129
  if (merge && sentinelPath)
1920
2130
  fs.rmSync(sentinelPath, { force: true });
1921
2131
  const top = topLevel(ctx.cwd);
2132
+ const target = targetFlag === undefined
2133
+ ? undefined
2134
+ : git(top, ["rev-parse", "--verify", "--quiet", `${targetFlag}^{commit}`]);
2135
+ if (targetFlag !== undefined && !target)
2136
+ throw new CommandError(EXIT.USAGE, `--target ${targetFlag} does not name a commit`);
1922
2137
  const fetch = fetchDev(top);
1923
2138
  if (!fetch.ok) {
1924
2139
  ctx.log(`git fetch ${REMOTE} ${DEV} failed: ${fetch.stderr}`);
1925
2140
  return EXIT_FETCH_FAILED;
1926
2141
  }
1927
- const head = git(top, ["rev-parse", "--verify", "--quiet", `${headFlag ?? "HEAD"}^{commit}`]);
2142
+ const branch = currentBranch(top);
2143
+ const observed = { remoteHead: null };
2144
+ if (reconcile) {
2145
+ const code = reconcileFeature(ctx, top, branch, observed);
2146
+ if (code !== EXIT.OK)
2147
+ return code;
2148
+ }
2149
+ const head = git(top, [
2150
+ "rev-parse",
2151
+ "--verify",
2152
+ "--quiet",
2153
+ `${reconcile ? "HEAD" : (headFlag ?? "HEAD")}^{commit}`,
2154
+ ]);
1928
2155
  if (!head)
1929
2156
  throw new CommandError(EXIT.USAGE, `--head ${headFlag ?? "HEAD"} does not name a commit`);
1930
- const branch = currentBranch(top);
1931
2157
  const ancestor = spawnSync("git", ["merge-base", "--is-ancestor", UPSTREAM, head], {
1932
2158
  cwd: top,
1933
2159
  env: gitEnv(),
@@ -1941,6 +2167,10 @@ function syncVerb(ctx) {
1941
2167
  const out = {
1942
2168
  branch: branch === "" ? null : branch,
1943
2169
  head,
2170
+ ...(reconcile ? { remoteHead: observed.remoteHead, reconciledHead: head } : {}),
2171
+ ...(featureState
2172
+ ? { featureCodeState: featureCodeState(top, head, month, coverageFid) }
2173
+ : {}),
1944
2174
  basePath: ancestor.status === 0 ? "A" : "B",
1945
2175
  behind,
1946
2176
  docsBehind,
@@ -1975,7 +2205,16 @@ function syncVerb(ctx) {
1975
2205
  throw new CommandError(EXIT.USAGE, `--merge merges ${UPSTREAM} into a feature branch, never into ${branch}`);
1976
2206
  }
1977
2207
  const fid = str(ctx.flags.fid) || branch.replace(/^feat\//, "");
1978
- const m = mergeDev(top, { branch, policy: "full", fid, generate: true, current: out.basePath === "A" }, ctx.log);
2208
+ const m = mergeDev(top, {
2209
+ branch,
2210
+ policy: "full",
2211
+ fid,
2212
+ generate: true,
2213
+ ...(target ? { target } : {}),
2214
+ current: target
2215
+ ? runGit(top, ["merge-base", "--is-ancestor", target, "HEAD"]).ok
2216
+ : out.basePath === "A",
2217
+ }, ctx.log);
1979
2218
  const refused = m.outcome === "refused";
1980
2219
  if (refused && sentinelPath) {
1981
2220
  fs.mkdirSync(path.dirname(sentinelPath), { recursive: true });
@@ -1984,6 +2223,7 @@ function syncVerb(ctx) {
1984
2223
  const newHead = git(top, ["rev-parse", "HEAD"]) ?? head;
1985
2224
  ctx.emit({
1986
2225
  ...out,
2226
+ ...(target ? { target } : {}),
1987
2227
  head: newHead,
1988
2228
  merged: m.merged,
1989
2229
  resolved: m.resolved,
@@ -2004,7 +2244,7 @@ function syncVerb(ctx) {
2004
2244
  return EXIT.OK;
2005
2245
  }
2006
2246
  /**
2007
- * Ship Step 13: after the merge, bring local dev up to origin/dev by a pure
2247
+ * Ship Step 11: after the merge, bring local dev up to origin/dev by a pure
2008
2248
  * fast-forward without checking anything out, then a frozen install when the
2009
2249
  * main checkout is on dev and moved. Never pushes.
2010
2250
  */
@@ -2092,14 +2332,18 @@ function restoreBranchRows(dir, branch) {
2092
2332
  for (const p of changed) {
2093
2333
  const before = new Set(revLines(dir, base, p));
2094
2334
  const file = path.join(dir, p);
2095
- const text = fs.existsSync(file) ? fs.readFileSync(file, "utf-8") : "";
2096
- const have = new Set(text.split("\n").map((l) => l.trim()));
2097
- const missing = revLines(dir, branch, p).filter((l) => !before.has(l) && !have.has(l));
2098
- if (missing.length === 0)
2335
+ const wrote = withTargetLock(file, () => {
2336
+ const text = fs.existsSync(file) ? fs.readFileSync(file, "utf-8") : "";
2337
+ const have = new Set(text.split("\n").map((l) => l.trim()));
2338
+ const missing = revLines(dir, branch, p).filter((l) => !before.has(l) && !have.has(l));
2339
+ if (missing.length === 0)
2340
+ return false;
2341
+ const sep = text !== "" && !text.endsWith("\n") ? "\n" : "";
2342
+ fs.appendFileSync(file, `${sep}${missing.join("\n")}\n`);
2343
+ return true;
2344
+ });
2345
+ if (!wrote)
2099
2346
  continue;
2100
- const sep = text !== "" && !text.endsWith("\n") ? "\n" : "";
2101
- fs.mkdirSync(path.dirname(file), { recursive: true });
2102
- fs.appendFileSync(file, `${sep}${missing.join("\n")}\n`);
2103
2347
  runGit(dir, ["add", "--", p]);
2104
2348
  }
2105
2349
  }
@@ -2330,30 +2574,6 @@ function fastForwardDev(root, sha, log) {
2330
2574
  }
2331
2575
  return "fast-forwarded";
2332
2576
  }
2333
- /**
2334
- * What `branch` changed that origin/dev does not hold, compared file by file
2335
- * against the branch's fork point: a file whose dev copy differs from the
2336
- * branch's, or (a `.jsonl` ledger, union-merged) a line the branch added that
2337
- * dev's copy lacks. `docs/roadmap.md` is re-rendered on every close, so it is
2338
- * not compared. The branch and its worktree are deleted only when this is empty.
2339
- */
2340
- function notOnDev(root, branch) {
2341
- const base = git(root, ["merge-base", UPSTREAM, branch]);
2342
- if (base === null)
2343
- return [`${branch}: no common history with ${UPSTREAM}`];
2344
- const lines = (rev, p) => revLines(root, rev, p);
2345
- return (git(root, ["diff", "--name-only", "--no-renames", "--diff-filter=d", base, branch]) ?? "")
2346
- .split("\n")
2347
- .filter((p) => p !== "" && p !== "docs/roadmap.md")
2348
- .filter((p) => {
2349
- if (!p.endsWith(".jsonl"))
2350
- return (git(root, ["rev-parse", `${branch}:${p}`]) !==
2351
- git(root, ["rev-parse", "-q", "--verify", `${UPSTREAM}:${p}`]));
2352
- const before = new Set(lines(base, p));
2353
- const onDev = new Set(lines(UPSTREAM, p));
2354
- return lines(branch, p).some((l) => !before.has(l) && !onDev.has(l));
2355
- });
2356
- }
2357
2577
  /**
2358
2578
  * Those of `paths` whose content at `spec(p)` is already local `dev`'s: work the
2359
2579
  * branch carries because `dev` is unpushed, not work a run in this branch made.
@@ -2644,6 +2864,17 @@ export const branchesCommand = {
2644
2864
  summary: `Fetch ${UPSTREAM}; report whether it is an ancestor of HEAD (Path A/B) and how far docs/ and the ledgers are behind; optionally merge it in.`,
2645
2865
  use: "workflow",
2646
2866
  flags: [
2867
+ {
2868
+ name: "feature-state",
2869
+ type: "boolean",
2870
+ help: "report merge-aware feature coverage, excluding bookkeeping and proven automatic dev-only merges; needs --month and --fid",
2871
+ },
2872
+ { name: "month", type: "string", help: "--feature-state: bookkeeping month YYYYMM" },
2873
+ {
2874
+ name: "reconcile-feature",
2875
+ type: "boolean",
2876
+ help: "ship: fetch and reconcile the feature branch before classifying its base; preserve divergent work through a merge and ensure unpublished work has a CI-running head",
2877
+ },
2647
2878
  {
2648
2879
  name: "head",
2649
2880
  type: "string",
@@ -2654,6 +2885,11 @@ export const branchesCommand = {
2654
2885
  type: "boolean",
2655
2886
  help: `ship Step 4: merge ${UPSTREAM} into the checked-out feature branch — the run's uncommitted work is committed first, ledger conflicts union, a lockfile regenerates from ${DEV}'s copy, a generated register takes ${DEV}'s copy and is rebuilt after the merge, a doc embedding the register's generated block takes ${DEV}'s block and merges the rest, the install record (.bkit/version) merges entry by entry with ${DEV}'s value where both changed, any other conflict stays in the tree with the merge in progress for a hand resolution (exit 24; run again after git add), then a declared bkit:generate runs and its output is committed; never a rebase or force-push, never on ${DEV} / main`,
2656
2887
  },
2888
+ {
2889
+ name: "target",
2890
+ type: "string",
2891
+ help: "--merge: exact recorded merge target (default origin/dev); continuation refuses a different MERGE_HEAD",
2892
+ },
2657
2893
  {
2658
2894
  name: "merge-docs",
2659
2895
  type: "boolean",
@@ -2678,6 +2914,10 @@ export const branchesCommand = {
2678
2914
  output: {
2679
2915
  branch: "current branch (null when detached)",
2680
2916
  head: "commit compared (after a merge: the new HEAD)",
2917
+ remoteHead: "--reconcile-feature: the freshly observed remote feature commit, or null when absent",
2918
+ reconciledHead: "--reconcile-feature: the resulting local feature commit used for base classification",
2919
+ featureCodeState: "--feature-state: versioned identity of feature commits and semantic merge results",
2920
+ target: "--merge --target: the resolved commit being merged or continued",
2681
2921
  basePath: `A when ${UPSTREAM} is an ancestor of head (nothing to integrate), else B`,
2682
2922
  behind: `commits on ${UPSTREAM} not in head`,
2683
2923
  docsBehind: `of those, commits touching docs/ or a ledger path (LEDGERS)`,
@@ -2692,6 +2932,7 @@ export const branchesCommand = {
2692
2932
  sentinel: "--merge: the sentinel written on refusal (null otherwise)",
2693
2933
  },
2694
2934
  exitCodes: {
2935
+ [EXIT_DIFF_FAILED]: "--feature-state: commit ancestry or bookkeeping classification could not be read",
2695
2936
  [EXIT_REFUSED]: "--merge: the uncommitted work could not be committed before the merge",
2696
2937
  [EXIT_DETACHED]: "--merge / --merge-docs with no branch checked out",
2697
2938
  [EXIT_FETCH_FAILED]: `git fetch ${REMOTE} ${DEV} failed`,
@@ -2733,7 +2974,7 @@ export const branchesCommand = {
2733
2974
  run: regenerateVerb,
2734
2975
  },
2735
2976
  push: {
2736
- summary: "Push the current branch with exactly one git push: plain for dev/main, --force-with-lease otherwise; 81 tests lock held.",
2977
+ summary: "Push the current branch with exactly one git push: plain for dev/main, --force-with-lease otherwise; when the pre-push hook's lock status file says so, 81 tests lock held, 4 tests lock refused the run or stopped it at the hold limit.",
2737
2978
  use: "workflow",
2738
2979
  flags: [
2739
2980
  {
@@ -2830,8 +3071,7 @@ export const branchesCommand = {
2830
3071
  {
2831
3072
  name: "base",
2832
3073
  type: "string",
2833
- required: true,
2834
- help: "diff <base>...HEAD; an origin/<branch> base is fetched first",
3074
+ help: "diff <base>...HEAD; an origin/<branch> base is fetched first (default: the feature base, where the branch's own work starts)",
2835
3075
  },
2836
3076
  {
2837
3077
  name: "paths",