dorfl 0.1.2 → 0.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 (364) hide show
  1. package/dist/advance-drivers.d.ts +1 -1
  2. package/dist/advance-drivers.d.ts.map +1 -1
  3. package/dist/advance-drivers.js +15 -3
  4. package/dist/advance-drivers.js.map +1 -1
  5. package/dist/advance-lifecycle-template.d.ts.map +1 -1
  6. package/dist/advance-lifecycle-template.js +76 -4
  7. package/dist/advance-lifecycle-template.js.map +1 -1
  8. package/dist/advance-treeless-publish.d.ts +23 -0
  9. package/dist/advance-treeless-publish.d.ts.map +1 -1
  10. package/dist/advance-treeless-publish.js +41 -0
  11. package/dist/advance-treeless-publish.js.map +1 -1
  12. package/dist/advance.d.ts +59 -7
  13. package/dist/advance.d.ts.map +1 -1
  14. package/dist/advance.js +389 -82
  15. package/dist/advance.js.map +1 -1
  16. package/dist/advancing-lock.d.ts +30 -2
  17. package/dist/advancing-lock.d.ts.map +1 -1
  18. package/dist/advancing-lock.js +50 -0
  19. package/dist/advancing-lock.js.map +1 -1
  20. package/dist/agent-launch.d.ts +12 -0
  21. package/dist/agent-launch.d.ts.map +1 -1
  22. package/dist/agent-launch.js +22 -12
  23. package/dist/agent-launch.js.map +1 -1
  24. package/dist/agent-stop.d.ts +40 -2
  25. package/dist/agent-stop.d.ts.map +1 -1
  26. package/dist/agent-stop.js +30 -2
  27. package/dist/agent-stop.js.map +1 -1
  28. package/dist/apply-decide.d.ts +17 -3
  29. package/dist/apply-decide.d.ts.map +1 -1
  30. package/dist/apply-decide.js +36 -7
  31. package/dist/apply-decide.js.map +1 -1
  32. package/dist/apply-merge-action.d.ts +17 -5
  33. package/dist/apply-merge-action.d.ts.map +1 -1
  34. package/dist/apply-merge-action.js +43 -8
  35. package/dist/apply-merge-action.js.map +1 -1
  36. package/dist/apply-persist.d.ts +57 -26
  37. package/dist/apply-persist.d.ts.map +1 -1
  38. package/dist/apply-persist.js +166 -37
  39. package/dist/apply-persist.js.map +1 -1
  40. package/dist/apply-stuck-action.d.ts +151 -0
  41. package/dist/apply-stuck-action.d.ts.map +1 -0
  42. package/dist/apply-stuck-action.js +125 -0
  43. package/dist/apply-stuck-action.js.map +1 -0
  44. package/dist/brand.d.ts +12 -1
  45. package/dist/brand.d.ts.map +1 -1
  46. package/dist/brand.js +2 -1
  47. package/dist/brand.js.map +1 -1
  48. package/dist/cli-spinner.d.ts +1 -1
  49. package/dist/cli-spinner.js +1 -1
  50. package/dist/cli.d.ts +10 -0
  51. package/dist/cli.d.ts.map +1 -1
  52. package/dist/cli.js +320 -63
  53. package/dist/cli.js.map +1 -1
  54. package/dist/close-job-template.js +1 -1
  55. package/dist/complete.d.ts +6 -3
  56. package/dist/complete.d.ts.map +1 -1
  57. package/dist/complete.js +91 -37
  58. package/dist/complete.js.map +1 -1
  59. package/dist/config.d.ts +55 -9
  60. package/dist/config.d.ts.map +1 -1
  61. package/dist/config.js +56 -4
  62. package/dist/config.js.map +1 -1
  63. package/dist/continue-branch.d.ts.map +1 -1
  64. package/dist/continue-branch.js +22 -0
  65. package/dist/continue-branch.js.map +1 -1
  66. package/dist/decision-engine.d.ts +38 -10
  67. package/dist/decision-engine.d.ts.map +1 -1
  68. package/dist/decision-engine.js +10 -6
  69. package/dist/decision-engine.js.map +1 -1
  70. package/dist/do-autopick.d.ts.map +1 -1
  71. package/dist/do-autopick.js +11 -1
  72. package/dist/do-autopick.js.map +1 -1
  73. package/dist/do-config.d.ts +1 -1
  74. package/dist/do.d.ts +49 -56
  75. package/dist/do.d.ts.map +1 -1
  76. package/dist/do.js +419 -30
  77. package/dist/do.js.map +1 -1
  78. package/dist/env-config.d.ts.map +1 -1
  79. package/dist/env-config.js +12 -4
  80. package/dist/env-config.js.map +1 -1
  81. package/dist/failure-cause.d.ts +3 -2
  82. package/dist/failure-cause.d.ts.map +1 -1
  83. package/dist/failure-cause.js +28 -1
  84. package/dist/failure-cause.js.map +1 -1
  85. package/dist/format.d.ts +6 -6
  86. package/dist/format.d.ts.map +1 -1
  87. package/dist/format.js +10 -27
  88. package/dist/format.js.map +1 -1
  89. package/dist/frontmatter.d.ts +16 -4
  90. package/dist/frontmatter.d.ts.map +1 -1
  91. package/dist/frontmatter.js +27 -1
  92. package/dist/frontmatter.js.map +1 -1
  93. package/dist/gc.d.ts +65 -6
  94. package/dist/gc.d.ts.map +1 -1
  95. package/dist/gc.js +126 -9
  96. package/dist/gc.js.map +1 -1
  97. package/dist/github.d.ts +14 -0
  98. package/dist/github.d.ts.map +1 -1
  99. package/dist/github.js +73 -0
  100. package/dist/github.js.map +1 -1
  101. package/dist/harness.d.ts +25 -0
  102. package/dist/harness.d.ts.map +1 -1
  103. package/dist/harness.js.map +1 -1
  104. package/dist/index.d.ts +4 -1
  105. package/dist/index.d.ts.map +1 -1
  106. package/dist/index.js +2 -1
  107. package/dist/index.js.map +1 -1
  108. package/dist/install-ci-branch-protection.d.ts +132 -38
  109. package/dist/install-ci-branch-protection.d.ts.map +1 -1
  110. package/dist/install-ci-branch-protection.js +190 -48
  111. package/dist/install-ci-branch-protection.js.map +1 -1
  112. package/dist/install-ci-core.d.ts +21 -1
  113. package/dist/install-ci-core.d.ts.map +1 -1
  114. package/dist/install-ci-core.js +10 -3
  115. package/dist/install-ci-core.js.map +1 -1
  116. package/dist/install-ci-github.d.ts +32 -0
  117. package/dist/install-ci-github.d.ts.map +1 -1
  118. package/dist/install-ci-github.js +73 -0
  119. package/dist/install-ci-github.js.map +1 -1
  120. package/dist/install-skills.d.ts +123 -0
  121. package/dist/install-skills.d.ts.map +1 -0
  122. package/dist/install-skills.js +100 -0
  123. package/dist/install-skills.js.map +1 -0
  124. package/dist/intake-trigger-template.js +1 -1
  125. package/dist/intake.d.ts +17 -4
  126. package/dist/intake.d.ts.map +1 -1
  127. package/dist/intake.js +23 -7
  128. package/dist/intake.js.map +1 -1
  129. package/dist/integration-core.d.ts +10 -7
  130. package/dist/integration-core.d.ts.map +1 -1
  131. package/dist/integration-core.js +174 -60
  132. package/dist/integration-core.js.map +1 -1
  133. package/dist/integrator.d.ts +1 -1
  134. package/dist/integrator.d.ts.map +1 -1
  135. package/dist/integrator.js +19 -3
  136. package/dist/integrator.js.map +1 -1
  137. package/dist/isolation.d.ts +2 -2
  138. package/dist/isolation.d.ts.map +1 -1
  139. package/dist/isolation.js +9 -0
  140. package/dist/isolation.js.map +1 -1
  141. package/dist/item-lock.d.ts +196 -129
  142. package/dist/item-lock.d.ts.map +1 -1
  143. package/dist/item-lock.js +329 -254
  144. package/dist/item-lock.js.map +1 -1
  145. package/dist/ledger-write.d.ts +31 -26
  146. package/dist/ledger-write.d.ts.map +1 -1
  147. package/dist/ledger-write.js +100 -122
  148. package/dist/ledger-write.js.map +1 -1
  149. package/dist/lifecycle-gather.d.ts +17 -0
  150. package/dist/lifecycle-gather.d.ts.map +1 -1
  151. package/dist/lifecycle-gather.js +7 -1
  152. package/dist/lifecycle-gather.js.map +1 -1
  153. package/dist/lifecycle-pools.d.ts +40 -3
  154. package/dist/lifecycle-pools.d.ts.map +1 -1
  155. package/dist/lifecycle-pools.js +26 -6
  156. package/dist/lifecycle-pools.js.map +1 -1
  157. package/dist/merge-question-surfacer.d.ts +15 -1
  158. package/dist/merge-question-surfacer.d.ts.map +1 -1
  159. package/dist/merge-question-surfacer.js +18 -3
  160. package/dist/merge-question-surfacer.js.map +1 -1
  161. package/dist/migrate-stuck-locks.d.ts +129 -0
  162. package/dist/migrate-stuck-locks.d.ts.map +1 -0
  163. package/dist/migrate-stuck-locks.js +355 -0
  164. package/dist/migrate-stuck-locks.js.map +1 -0
  165. package/dist/needs-attention.d.ts +293 -16
  166. package/dist/needs-attention.d.ts.map +1 -1
  167. package/dist/needs-attention.js +532 -57
  168. package/dist/needs-attention.js.map +1 -1
  169. package/dist/orphan-sidecar.d.ts +8 -4
  170. package/dist/orphan-sidecar.d.ts.map +1 -1
  171. package/dist/orphan-sidecar.js +35 -2
  172. package/dist/orphan-sidecar.js.map +1 -1
  173. package/dist/pi-harness.d.ts +16 -0
  174. package/dist/pi-harness.d.ts.map +1 -1
  175. package/dist/pi-harness.js +82 -2
  176. package/dist/pi-harness.js.map +1 -1
  177. package/dist/placement.d.ts +3 -3
  178. package/dist/placement.js +1 -1
  179. package/dist/prd-to-spec.d.ts.map +1 -1
  180. package/dist/prd-to-spec.js +9 -5
  181. package/dist/prd-to-spec.js.map +1 -1
  182. package/dist/protocol/CLAIM-PROTOCOL.md +17 -10
  183. package/dist/protocol/REVIEW-PROTOCOL.md +4 -1
  184. package/dist/protocol/SURFACE-PROTOCOL.md +16 -2
  185. package/dist/protocol/TASKING-PROTOCOL.md +3 -1
  186. package/dist/protocol/WORK-CONTRACT.md +22 -18
  187. package/dist/protocol/task-template.md +1 -1
  188. package/dist/reap-branches.d.ts +12 -9
  189. package/dist/reap-branches.d.ts.map +1 -1
  190. package/dist/reap-branches.js +25 -7
  191. package/dist/reap-branches.js.map +1 -1
  192. package/dist/recover-isolated.d.ts +12 -0
  193. package/dist/recover-isolated.d.ts.map +1 -1
  194. package/dist/recover-isolated.js +5 -0
  195. package/dist/recover-isolated.js.map +1 -1
  196. package/dist/repo-config.d.ts +23 -2
  197. package/dist/repo-config.d.ts.map +1 -1
  198. package/dist/repo-config.js +53 -7
  199. package/dist/repo-config.js.map +1 -1
  200. package/dist/repo-mirror.d.ts.map +1 -1
  201. package/dist/repo-mirror.js +18 -2
  202. package/dist/repo-mirror.js.map +1 -1
  203. package/dist/review-verdict.js +1 -1
  204. package/dist/review-verdict.js.map +1 -1
  205. package/dist/run.d.ts +1 -1
  206. package/dist/run.d.ts.map +1 -1
  207. package/dist/run.js +54 -19
  208. package/dist/run.js.map +1 -1
  209. package/dist/scan.d.ts +17 -8
  210. package/dist/scan.d.ts.map +1 -1
  211. package/dist/scan.js +22 -8
  212. package/dist/scan.js.map +1 -1
  213. package/dist/select-priority.d.ts +2 -2
  214. package/dist/select-priority.js +1 -1
  215. package/dist/select-priority.js.map +1 -1
  216. package/dist/sidecar-apply.js +1 -1
  217. package/dist/sidecar-apply.js.map +1 -1
  218. package/dist/sidecar.d.ts +45 -10
  219. package/dist/sidecar.d.ts.map +1 -1
  220. package/dist/sidecar.js +82 -2
  221. package/dist/sidecar.js.map +1 -1
  222. package/dist/skills/answer-questions/SKILL.md +89 -0
  223. package/dist/skills/capture-signal/SKILL.md +52 -0
  224. package/dist/skills/convert-from-prd-to-spec/SKILL.md +90 -0
  225. package/dist/skills/drive-tasks/SKILL.md +218 -0
  226. package/dist/skills/from-idea/SKILL.md +83 -0
  227. package/dist/skills/merge-prs/SKILL.md +70 -0
  228. package/dist/skills/orchestrate/SKILL.md +101 -0
  229. package/dist/skills/promote/SKILL.md +35 -0
  230. package/dist/skills/review/SKILL.md +16 -0
  231. package/dist/skills/setup/SKILL.md +258 -0
  232. package/dist/skills/setup/protocol/ADR-FORMAT.md +47 -0
  233. package/dist/skills/setup/protocol/CLAIM-PROTOCOL.md +224 -0
  234. package/dist/skills/setup/protocol/REVIEW-PROTOCOL.md +122 -0
  235. package/dist/skills/setup/protocol/SURFACE-PROTOCOL.md +135 -0
  236. package/dist/skills/setup/protocol/TASKING-PROTOCOL.md +124 -0
  237. package/dist/skills/setup/protocol/WORK-CONTRACT.md +280 -0
  238. package/dist/skills/setup/protocol/spec-template.md +71 -0
  239. package/dist/skills/setup/protocol/task-template.md +65 -0
  240. package/dist/skills/surface-questions/SKILL.md +16 -0
  241. package/dist/skills/to-spec/SKILL.md +34 -0
  242. package/dist/skills/to-task/SKILL.md +19 -0
  243. package/dist/skills/triage-observations/SKILL.md +78 -0
  244. package/dist/skills/work/SKILL.md +51 -0
  245. package/dist/slug-namespace.d.ts +5 -5
  246. package/dist/slug-namespace.js +5 -5
  247. package/dist/slug-namespace.js.map +1 -1
  248. package/dist/spec-complete.d.ts.map +1 -1
  249. package/dist/spec-complete.js +2 -3
  250. package/dist/spec-complete.js.map +1 -1
  251. package/dist/start.d.ts.map +1 -1
  252. package/dist/start.js +51 -57
  253. package/dist/start.js.map +1 -1
  254. package/dist/surface-gate.d.ts.map +1 -1
  255. package/dist/surface-gate.js +10 -3
  256. package/dist/surface-gate.js.map +1 -1
  257. package/dist/surface-persist.js +1 -1
  258. package/dist/surface-persist.js.map +1 -1
  259. package/dist/tasker-review-loop.d.ts +4 -3
  260. package/dist/tasker-review-loop.d.ts.map +1 -1
  261. package/dist/tasker-review-loop.js.map +1 -1
  262. package/dist/tasking-lock.d.ts +2 -2
  263. package/dist/tasking-lock.d.ts.map +1 -1
  264. package/dist/tasking-lock.js +53 -32
  265. package/dist/tasking-lock.js.map +1 -1
  266. package/dist/tasking.d.ts +40 -6
  267. package/dist/tasking.d.ts.map +1 -1
  268. package/dist/tasking.js +224 -12
  269. package/dist/tasking.js.map +1 -1
  270. package/dist/triage-persist.d.ts +10 -6
  271. package/dist/triage-persist.d.ts.map +1 -1
  272. package/dist/triage-persist.js +34 -4
  273. package/dist/triage-persist.js.map +1 -1
  274. package/dist/vendor/incur/agents.d.ts +58 -0
  275. package/dist/vendor/incur/agents.d.ts.map +1 -0
  276. package/dist/vendor/incur/agents.js +343 -0
  277. package/dist/vendor/incur/agents.js.map +1 -0
  278. package/dist/verify-workflow-template.js +1 -1
  279. package/dist/watch-session.d.ts +11 -3
  280. package/dist/watch-session.d.ts.map +1 -1
  281. package/dist/watch-session.js +94 -7
  282. package/dist/watch-session.js.map +1 -1
  283. package/dist/work-layout.d.ts +7 -3
  284. package/dist/work-layout.d.ts.map +1 -1
  285. package/dist/work-layout.js +6 -3
  286. package/dist/work-layout.js.map +1 -1
  287. package/dist/workspace.d.ts +1 -1
  288. package/package.json +2 -2
  289. package/src/advance-drivers.ts +16 -3
  290. package/src/advance-lifecycle-template.ts +89 -4
  291. package/src/advance-treeless-publish.ts +45 -0
  292. package/src/advance.ts +467 -87
  293. package/src/advancing-lock.ts +96 -1
  294. package/src/agent-launch.ts +37 -12
  295. package/src/agent-stop.ts +60 -2
  296. package/src/apply-decide.ts +36 -7
  297. package/src/apply-merge-action.ts +45 -7
  298. package/src/apply-persist.ts +228 -54
  299. package/src/apply-stuck-action.ts +260 -0
  300. package/src/brand.ts +14 -2
  301. package/src/cli-spinner.ts +1 -1
  302. package/src/cli.ts +401 -70
  303. package/src/close-job-template.ts +1 -1
  304. package/src/complete.ts +110 -52
  305. package/src/config.ts +107 -13
  306. package/src/continue-branch.ts +22 -0
  307. package/src/decision-engine.ts +55 -17
  308. package/src/do-autopick.ts +12 -1
  309. package/src/do-config.ts +1 -1
  310. package/src/do.ts +511 -38
  311. package/src/env-config.ts +12 -4
  312. package/src/failure-cause.ts +30 -1
  313. package/src/format.ts +10 -30
  314. package/src/frontmatter.ts +35 -5
  315. package/src/gc.ts +172 -9
  316. package/src/github.ts +78 -0
  317. package/src/harness.ts +25 -0
  318. package/src/index.ts +10 -0
  319. package/src/install-ci-branch-protection.ts +282 -57
  320. package/src/install-ci-core.ts +30 -3
  321. package/src/install-ci-github.ts +86 -0
  322. package/src/install-skills.ts +166 -0
  323. package/src/intake-trigger-template.ts +1 -1
  324. package/src/intake.ts +46 -11
  325. package/src/integration-core.ts +199 -65
  326. package/src/integrator.ts +22 -9
  327. package/src/isolation.ts +11 -2
  328. package/src/item-lock.ts +485 -341
  329. package/src/ledger-write.ts +135 -150
  330. package/src/lifecycle-gather.ts +29 -6
  331. package/src/lifecycle-pools.ts +67 -9
  332. package/src/merge-question-surfacer.ts +28 -4
  333. package/src/migrate-stuck-locks.ts +451 -0
  334. package/src/needs-attention.ts +857 -84
  335. package/src/orphan-sidecar.ts +47 -6
  336. package/src/pi-harness.ts +82 -2
  337. package/src/placement.ts +3 -3
  338. package/src/prd-to-spec.ts +13 -5
  339. package/src/reap-branches.ts +38 -14
  340. package/src/recover-isolated.ts +17 -0
  341. package/src/repo-config.ts +55 -6
  342. package/src/repo-mirror.ts +23 -1
  343. package/src/review-verdict.ts +1 -1
  344. package/src/run.ts +64 -18
  345. package/src/scan.ts +22 -8
  346. package/src/select-priority.ts +3 -3
  347. package/src/sidecar-apply.ts +1 -1
  348. package/src/sidecar.ts +138 -11
  349. package/src/slug-namespace.ts +7 -7
  350. package/src/spec-complete.ts +2 -3
  351. package/src/start.ts +53 -59
  352. package/src/surface-gate.ts +10 -3
  353. package/src/surface-persist.ts +1 -1
  354. package/src/tasker-review-loop.ts +4 -3
  355. package/src/tasking-lock.ts +52 -36
  356. package/src/tasking.ts +261 -17
  357. package/src/triage-persist.ts +56 -12
  358. package/src/vendor/incur/LICENSE +21 -0
  359. package/src/vendor/incur/README.md +19 -0
  360. package/src/vendor/incur/agents.ts +392 -0
  361. package/src/verify-workflow-template.ts +1 -1
  362. package/src/watch-session.ts +102 -7
  363. package/src/work-layout.ts +6 -3
  364. package/src/workspace.ts +1 -1
@@ -139,10 +139,19 @@ export interface MergeQuestionSurfaced {
139
139
  prUrl?: string;
140
140
  }
141
141
 
142
- /** One branch the surfacer considered but did not surface (with the reason). */
142
+ /**
143
+ * One branch the surfacer considered but did not surface (with the reason).
144
+ *
145
+ * PROVISIONAL vocabulary. The `reason` union is scoped to this surfacer — no
146
+ * sibling surfacer exists yet, so it is deliberately NOT lifted to a shared
147
+ * skip-reason type. When a second STATE-sourced surfacer lands (e.g. a
148
+ * stuck-lock surfacer), promote this to a shared skip-reason vocabulary via a
149
+ * dedicated decision; until then it may change without notice.
150
+ */
143
151
  export interface MergeQuestionSkipped {
144
152
  ref: string;
145
153
  slug: string;
154
+ /** PROVISIONAL vocabulary — see {@link MergeQuestionSkipped}. */
146
155
  reason: 'no-item-body' | 'already-pending-merge-question' | 'persist-nothing';
147
156
  }
148
157
 
@@ -164,9 +173,19 @@ export class MergeQuestionSurfacerError extends Error {
164
173
  }
165
174
  }
166
175
 
167
- /** The lifecycle folders a task body may rest in (the FOLDERS the persist
168
- * needs to find the item file to set `needsAnswers:true` on). Matches
169
- * `advance.ts`'s `findItemPath` task-folder set. */
176
+ /**
177
+ * The lifecycle folders {@link findTaskItemPath} scans for a task body to flip
178
+ * `needsAnswers:true` on. This set DELIBERATELY DIVERGES from `advance.ts`'s
179
+ * `FOLDERS_FOR_TYPE.task` (`['tasks-backlog','tasks-ready','in-progress','done']`):
180
+ *
181
+ * - OMITS `in-progress` (and `needs-attention`): an unmerged `work/<slug>`
182
+ * branch whose body is mid-build should NOT trigger a merge-question — the
183
+ * build is still active, and surfacing a land-decision now would race the
184
+ * builder. Such tasks fall through to the `no-item-body` skip.
185
+ * - ADDS `cancelled`: a cancelled task with a lingering unmerged `work/*`
186
+ * branch SHOULD surface a merge-question so the operator explicitly decides
187
+ * whether to merge the branch or drop it.
188
+ */
170
189
  const TASK_FOLDERS: readonly WorkFolderKey[] = [
171
190
  'tasks-ready',
172
191
  'tasks-backlog',
@@ -440,6 +459,11 @@ export function listUnmergedWorkBranchesViaGit(
440
459
  * results by their `headRefName` so the surfacer can enrich the matching
441
460
  * branch's question. A non-zero / missing `gh` is treated as "no host
442
461
  * metadata available" — the floor still surfaces every unmerged branch.
462
+ *
463
+ * Best-effort enrichment. The git-reachability FLOOR is authoritative; the
464
+ * `--state open`, `--base <base>`, and `--limit 200` arguments are DELIBERATE
465
+ * ceilings — a PR targeting a non-`main` base (e.g. a stacked PR) or the case
466
+ * of >200 open PRs degrades to floor-only output, never corrupts it.
443
467
  */
444
468
  export function listOpenPullRequestsViaGh(
445
469
  input: ListPullRequestsInput,
@@ -0,0 +1,451 @@
1
+ /**
2
+ * ONE-SHOT migration of pre-existing `stuck` per-item lock refs into the
3
+ * post-`retire-stuck-lock-state` resting shape (surface-on-`main` +
4
+ * `needsAnswers:true` + release the lock).
5
+ *
6
+ * Context (spec `surface-stuck-as-questions-and-retire-stuck-lock-state`,
7
+ * resolved decision #3, user story 5; task
8
+ * `migrate-existing-stuck-locks-one-shot`):
9
+ * - Before the retirement, a bounced item was recorded as a `state: stuck`
10
+ * lock ref (`refs/dorfl/lock/<entry>`) with the bounce reason (+ any
11
+ * agent-surfaced questions) on the ref's blob body.
12
+ * - After the retirement (task `retire-stuck-lock-state`), `LockState`
13
+ * admits only `active`. {@link parseLockEntry} silently COERCES a legacy
14
+ * `state: stuck` blob to `active` on read, and the `stuck`-only body
15
+ * sections (`## Reason` / `## Questions`) are no longer emitted or
16
+ * parsed. So a legacy stuck ref is now invisible to the runner (it looks
17
+ * like a healthy in-flight `active` hold) and its reason/questions would
18
+ * rot forever if the state were not migrated at rollout.
19
+ *
20
+ * This module runs the migration ONCE per arbiter at rollout: for every held
21
+ * lock ref whose RAW blob body still carries `state: stuck` in its
22
+ * frontmatter, it extracts the reason (+ questions) directly from the legacy
23
+ * body (bypassing the coercing {@link parseLockEntry}) and drives the SAME
24
+ * ordered surface-first-release-second transition the new bounce uses
25
+ * ({@link surfaceStuckToNeedsAttention}) — writing the `stuck`-kind
26
+ * `work/questions/<entry>.md` sidecar + flipping `needsAnswers:true` on the
27
+ * item body on `<arbiter>/main`, then releasing the ref. After the migration
28
+ * no `stuck` lock ref remains and every previously-stuck item rests as a
29
+ * `needsAnswers:true` pool item with a sidecar (the same resting shape a
30
+ * fresh bounce produces).
31
+ *
32
+ * IDEMPOTENT by construction: the legacy shape is identified from the raw
33
+ * blob's `state: stuck` frontmatter line, which the new lock module NEVER
34
+ * writes; a re-run therefore finds no legacy stuck ref → the report has zero
35
+ * `migrated` entries and no arbiter mutation occurs. Bare-arbiter safe (a ref
36
+ * is a ref): every operation goes through the same lock-ref plumbing the
37
+ * live path uses, so it works on a `--bare file://` arbiter identically to a
38
+ * real remote.
39
+ *
40
+ * SCOPE FENCE: this migrates ONLY lock entries whose name reverse-derives to
41
+ * a CURRENT item-form (`task-*` / `spec-*` / `observation-*` via
42
+ * {@link hasCurrentItemForm}); a pre-cutover `slice-*` / `prd-*` legacy stuck
43
+ * ref has no on-`main` body to flip and is out of scope for the surface
44
+ * transition — such entries are reported as `skipped-no-item-form` and left
45
+ * for the existing `release-lock --entry <literal>` escape hatch to clear (a
46
+ * human still asserts the clear on those; the migration never `--force`s or
47
+ * deletes them). See the CLI verb `dorfl migrate-stuck-locks` for the
48
+ * user-facing surface + its help text.
49
+ */
50
+
51
+ import {runAsync, type RunResult} from './git.js';
52
+ import {
53
+ itemFromLockEntry,
54
+ itemLockRef,
55
+ hasCurrentItemForm,
56
+ listItemLocks,
57
+ } from './item-lock.js';
58
+ import {
59
+ surfaceStuckToNeedsAttention,
60
+ type SurfaceStuckToNeedsAttentionResult,
61
+ } from './needs-attention.js';
62
+ import {resolveSidecarIdentity} from './sidecar.js';
63
+
64
+ /** Per-lock outcome of the migration sweep. */
65
+ export type MigrateStuckLockOutcome =
66
+ | 'migrated' // legacy stuck ref → surfaced on main + released
67
+ | 'migrated-body-absent' // legacy stuck ref → lock released, but no `main` body existed to flip (bounce D1 probe found nothing); still a no-op-safe drain of the stale lock
68
+ | 'not-stuck' // ref present but its blob is not the legacy `state: stuck` shape (healthy active hold, or already migrated)
69
+ | 'skipped-no-item-form' // legacy stuck ref but its `<entry>` does not reverse-derive to a current item-form (pre-cutover `slice-*` / `prd-*`); clear via `release-lock --entry <literal>`
70
+ | 'lost' // the surface CAS or the release lost the race to a concurrent writer; reported, never `--force`d
71
+ | 'error'; // read/plumbing fault on this ref; reported, ref left untouched
72
+
73
+ /** One lock's disposition in the {@link MigrateStuckLocksReport}. */
74
+ export interface MigrateStuckLockEntry {
75
+ /** The lock-entry name (`<type>-<slug>` or a pre-cutover `slice-*` / `prd-*`). */
76
+ entry: string;
77
+ /** The lock ref (`refs/dorfl/lock/<entry>`). */
78
+ ref: string;
79
+ /**
80
+ * The namespaced item form the entry reverse-derives to (`task:<slug>` /
81
+ * `spec:<slug>` / `observation:<slug>`), or the raw entry when it does not
82
+ * carry a current item-form prefix.
83
+ */
84
+ item: string;
85
+ outcome: MigrateStuckLockOutcome;
86
+ /** Human-readable note (why it was left, or the surface + release result). */
87
+ message: string;
88
+ }
89
+
90
+ /** Result of {@link migrateStuckLocks}. A one-shot sweep report. */
91
+ export interface MigrateStuckLocksReport {
92
+ entries: MigrateStuckLockEntry[];
93
+ /** Count of legacy stuck refs surfaced on main + released. */
94
+ migrated: number;
95
+ /** Count of legacy stuck refs whose lock was released but had no on-`main` body
96
+ * to flip (body-absent D1 probe). Still a successful drain. */
97
+ migratedBodyAbsent: number;
98
+ /** Count of refs that were not the legacy stuck shape (healthy active holds
99
+ * or already-migrated). No mutation. */
100
+ notStuck: number;
101
+ /** Count of legacy stuck refs skipped because their entry has no current
102
+ * item-form (pre-cutover `slice-*` / `prd-*`). */
103
+ skippedNoItemForm: number;
104
+ /** Count of refs whose surface/release lost the CAS race. */
105
+ lost: number;
106
+ /** Count of refs where a read/plumbing fault prevented migration. */
107
+ errors: number;
108
+ }
109
+
110
+ export interface MigrateStuckLocksOptions {
111
+ cwd: string;
112
+ arbiter?: string;
113
+ env?: NodeJS.ProcessEnv;
114
+ /** Optional progress note sink (parity with `surfaceStuckToNeedsAttention`). */
115
+ note?: (message: string) => void;
116
+ }
117
+
118
+ /**
119
+ * Run the one-shot migration against the arbiter. Enumerates every held
120
+ * per-item lock ref, and for each whose RAW blob body carries `state: stuck`
121
+ * (the legacy shape the retired `stuck` state produced) drives the shared
122
+ * surface-first-release-second transition (reason + questions extracted from
123
+ * the legacy body). Idempotent: a re-run finds no legacy stuck refs and is a
124
+ * clean no-op.
125
+ */
126
+ export async function migrateStuckLocks(
127
+ options: MigrateStuckLocksOptions,
128
+ ): Promise<MigrateStuckLocksReport> {
129
+ const cwd = options.cwd;
130
+ const arbiter = options.arbiter ?? 'origin';
131
+ const env = options.env;
132
+ const note = options.note ?? (() => {});
133
+
134
+ const entries: MigrateStuckLockEntry[] = [];
135
+ let migrated = 0;
136
+ let migratedBodyAbsent = 0;
137
+ let notStuck = 0;
138
+ let skippedNoItemForm = 0;
139
+ let lost = 0;
140
+ let errors = 0;
141
+
142
+ // Enumerate every held lock entry on the arbiter. `listItemLocks` fetches the
143
+ // lock refs first (hard-fails on a fetch fault) so the enumeration reads a
144
+ // fresh arbiter snapshot; if the fetch throws we surface it as a single
145
+ // terminal error rather than degrading to `[]` (which would silently claim
146
+ // "nothing to migrate").
147
+ let heldEntries: string[];
148
+ try {
149
+ heldEntries = await listItemLocks(cwd, arbiter, env);
150
+ } catch (err) {
151
+ return {
152
+ entries: [
153
+ {
154
+ entry: '',
155
+ ref: '',
156
+ item: '',
157
+ outcome: 'error',
158
+ message: `failed to enumerate lock refs on ${arbiter}: ${
159
+ err instanceof Error ? err.message : String(err)
160
+ }`,
161
+ },
162
+ ],
163
+ migrated: 0,
164
+ migratedBodyAbsent: 0,
165
+ notStuck: 0,
166
+ skippedNoItemForm: 0,
167
+ lost: 0,
168
+ errors: 1,
169
+ };
170
+ }
171
+
172
+ for (const entry of heldEntries) {
173
+ const ref = itemLockRef(entry);
174
+ const item = itemFromLockEntry(entry);
175
+ let body: string;
176
+ try {
177
+ body = await readLockBlob(ref, cwd, env);
178
+ } catch (err) {
179
+ errors++;
180
+ entries.push({
181
+ entry,
182
+ ref,
183
+ item,
184
+ outcome: 'error',
185
+ message: `read failed: ${err instanceof Error ? err.message : String(err)}`,
186
+ });
187
+ continue;
188
+ }
189
+ if (!isLegacyStuckBlob(body)) {
190
+ notStuck++;
191
+ entries.push({
192
+ entry,
193
+ ref,
194
+ item,
195
+ outcome: 'not-stuck',
196
+ message: `${entry} is not a legacy stuck lock (state != stuck on the ref blob) — left untouched.`,
197
+ });
198
+ continue;
199
+ }
200
+ if (!hasCurrentItemForm(entry)) {
201
+ // A pre-cutover `slice-*` / `prd-*` stuck ref has no current
202
+ // item-form and therefore no on-`main` body path to flip. The
203
+ // existing `release-lock --entry <literal>` escape hatch is the
204
+ // supported clear here; the migration NEVER `--force`s such a ref.
205
+ skippedNoItemForm++;
206
+ entries.push({
207
+ entry,
208
+ ref,
209
+ item,
210
+ outcome: 'skipped-no-item-form',
211
+ message:
212
+ `'${entry}' is a legacy stuck ref with no current item-form ` +
213
+ `(pre-cutover slice-*/prd-*); clear via ` +
214
+ `\`dorfl release-lock --entry ${entry}\` (no auto-force).`,
215
+ });
216
+ continue;
217
+ }
218
+ const {slug} = resolveSidecarIdentity(item);
219
+ const reason = extractLegacyReason(body) ?? '(no reason recorded)';
220
+ const questions = extractLegacyQuestions(body).map((q) => ({question: q}));
221
+ let result: SurfaceStuckToNeedsAttentionResult;
222
+ try {
223
+ result = await surfaceStuckToNeedsAttention({
224
+ cwd,
225
+ slug,
226
+ item,
227
+ reason,
228
+ questions,
229
+ arbiter,
230
+ env,
231
+ note,
232
+ });
233
+ } catch (err) {
234
+ errors++;
235
+ entries.push({
236
+ entry,
237
+ ref,
238
+ item,
239
+ outcome: 'error',
240
+ message: `surface transition threw: ${
241
+ err instanceof Error ? err.message : String(err)
242
+ }`,
243
+ });
244
+ continue;
245
+ }
246
+ if (result.surfaced && result.released) {
247
+ migrated++;
248
+ entries.push({
249
+ entry,
250
+ ref,
251
+ item,
252
+ outcome: 'migrated',
253
+ message:
254
+ `migrated '${entry}' → surfaced on ${arbiter}/main ` +
255
+ '(needsAnswers:true + sidecar) and released the lock.',
256
+ });
257
+ continue;
258
+ }
259
+ if (!result.surfaced && result.released && result.bodyAbsent === true) {
260
+ // D1 body-absent probe: no `main` body existed to flip. The bounce
261
+ // primitive STILL released the lock so the legacy stuck ref no longer
262
+ // dangles; there is just nothing to surface. Counted as a drained
263
+ // migration (the goal — no stuck ref remains).
264
+ migratedBodyAbsent++;
265
+ entries.push({
266
+ entry,
267
+ ref,
268
+ item,
269
+ outcome: 'migrated-body-absent',
270
+ message:
271
+ `released legacy stuck lock '${entry}': no body for '${item}' on ` +
272
+ `${arbiter}/main (D1 probe) — sidecar surface skipped, but the ` +
273
+ 'stale lock was drained (the migration goal).',
274
+ });
275
+ continue;
276
+ }
277
+ // The surface CAS or the release did not converge (contention exhausted,
278
+ // or the leased release lost the race). Reported, never forced.
279
+ lost++;
280
+ entries.push({
281
+ entry,
282
+ ref,
283
+ item,
284
+ outcome: 'lost',
285
+ message:
286
+ result.reasonNotSurfaced ??
287
+ `surface/release for '${entry}' did not converge (surfaced=${result.surfaced}, released=${result.released}); re-run.`,
288
+ });
289
+ }
290
+
291
+ return {
292
+ entries,
293
+ migrated,
294
+ migratedBodyAbsent,
295
+ notStuck,
296
+ skippedNoItemForm,
297
+ lost,
298
+ errors,
299
+ };
300
+ }
301
+
302
+ /**
303
+ * True iff the migration report leaves a state a human should look at: a
304
+ * `lost` (CAS race) or an `error` (read/plumbing fault). A `not-stuck` /
305
+ * `skipped-no-item-form` outcome is INFORMATIONAL (the former is the normal
306
+ * healthy-hold state; the latter is deferred to `release-lock --entry`) and
307
+ * does NOT count. A pure "0 migrated, 0 lost, 0 error" report is exit-0.
308
+ */
309
+ export function migrateStuckLocksNeedsAttention(
310
+ report: MigrateStuckLocksReport,
311
+ ): boolean {
312
+ return report.lost > 0 || report.errors > 0;
313
+ }
314
+
315
+ /** Format the sweep for the terminal. An empty enumeration (no lock refs at
316
+ * all) yields no lines (silent, like the gc lock report). */
317
+ export function formatMigrateStuckLocksReport(
318
+ report: MigrateStuckLocksReport,
319
+ ): string[] {
320
+ if (report.entries.length === 0) {
321
+ return [
322
+ 'Migrate stuck locks: no per-item lock refs held on the arbiter — nothing to migrate.',
323
+ ];
324
+ }
325
+ const lines = [
326
+ `Migrate stuck locks: migrated ${report.migrated}` +
327
+ (report.migratedBodyAbsent > 0
328
+ ? ` (+${report.migratedBodyAbsent} body-absent — lock drained, no on-main body to surface)`
329
+ : '') +
330
+ `, left ${report.notStuck} healthy active hold(s) untouched` +
331
+ (report.skippedNoItemForm > 0
332
+ ? `, skipped ${report.skippedNoItemForm} pre-cutover entry (clear via release-lock --entry)`
333
+ : '') +
334
+ (report.lost > 0
335
+ ? `, ${report.lost} could not converge (lease lost — re-run)`
336
+ : '') +
337
+ (report.errors > 0
338
+ ? `, ${report.errors} error(s) (left untouched)`
339
+ : '') +
340
+ ':',
341
+ ];
342
+ for (const e of report.entries) {
343
+ const tag =
344
+ e.outcome === 'migrated'
345
+ ? '[migrated] '
346
+ : e.outcome === 'migrated-body-absent'
347
+ ? '[migrated:body-absent]'
348
+ : e.outcome === 'not-stuck'
349
+ ? '[healthy] '
350
+ : e.outcome === 'skipped-no-item-form'
351
+ ? '[skipped:no-item-form]'
352
+ : e.outcome === 'lost'
353
+ ? '[lost] '
354
+ : '[error] ';
355
+ lines.push(` ${tag} ${e.entry} ${e.message}`);
356
+ }
357
+ return lines;
358
+ }
359
+
360
+ /** Read the raw `lock.md` blob body from a lock ref, or throw. Bypasses
361
+ * {@link parseLockEntry} on purpose — the caller needs the untouched legacy
362
+ * text (which the coercing parser strips). */
363
+ async function readLockBlob(
364
+ ref: string,
365
+ cwd: string,
366
+ env: NodeJS.ProcessEnv | undefined,
367
+ ): Promise<string> {
368
+ const show: RunResult = await runAsync(
369
+ 'git',
370
+ ['show', `${ref}:lock.md`],
371
+ cwd,
372
+ {env},
373
+ );
374
+ if (show.status !== 0) {
375
+ throw new Error(
376
+ `git show ${ref}:lock.md failed (exit ${show.status}): ${show.stderr.trim()}`,
377
+ );
378
+ }
379
+ return show.stdout;
380
+ }
381
+
382
+ /** True iff `body` is the legacy `state: stuck` shape (the pre-retirement
383
+ * lock-entry blob). Matched on the raw frontmatter line so this is unaffected
384
+ * by the current parser's coercion to `active`. */
385
+ export function isLegacyStuckBlob(body: string): boolean {
386
+ const normalized = body.replace(/\r\n/g, '\n');
387
+ const fm = /^---\n([\s\S]*?)\n---/.exec(normalized);
388
+ if (!fm) {
389
+ return false;
390
+ }
391
+ return /^state:\s*stuck\s*$/m.test(fm[1]);
392
+ }
393
+
394
+ /**
395
+ * Extract the bounce reason from a legacy stuck lock blob. Preference order
396
+ * (the two shapes the retired serialiser produced):
397
+ * 1. A `## Reason` block in the body (multi-line, blank-trimmed).
398
+ * 2. A one-line `reason:` field in the frontmatter (pre-cutover shape).
399
+ * Returns `undefined` when neither is present.
400
+ */
401
+ export function extractLegacyReason(body: string): string | undefined {
402
+ const normalized = body.replace(/\r\n/g, '\n');
403
+ const fm = /^---\n([\s\S]*?)\n---/.exec(normalized);
404
+ const bodyText = fm ? normalized.slice(fm[0].length) : normalized;
405
+ const lines = bodyText.split('\n');
406
+ const start = lines.findIndex((l) => l.trim() === '## Reason');
407
+ if (start !== -1) {
408
+ const collected: string[] = [];
409
+ for (let i = start + 1; i < lines.length; i++) {
410
+ if (/^##\s/.test(lines[i])) {
411
+ break;
412
+ }
413
+ collected.push(lines[i]);
414
+ }
415
+ const text = collected.join('\n').replace(/^\n+/, '').replace(/\n+$/, '');
416
+ if (text !== '') {
417
+ return text;
418
+ }
419
+ }
420
+ if (fm) {
421
+ const m = /^reason:\s*(.*)$/m.exec(fm[1]);
422
+ if (m && m[1].trim() !== '') {
423
+ return m[1].trim();
424
+ }
425
+ }
426
+ return undefined;
427
+ }
428
+
429
+ /** Extract the `## Questions` bulleted list from a legacy stuck lock blob (or
430
+ * `[]` when absent). Each bullet becomes one question string. */
431
+ export function extractLegacyQuestions(body: string): string[] {
432
+ const normalized = body.replace(/\r\n/g, '\n');
433
+ const fm = /^---\n[\s\S]*?\n---/.exec(normalized);
434
+ const bodyText = fm ? normalized.slice(fm[0].length) : normalized;
435
+ const lines = bodyText.split('\n');
436
+ const start = lines.findIndex((l) => l.trim() === '## Questions');
437
+ if (start === -1) {
438
+ return [];
439
+ }
440
+ const questions: string[] = [];
441
+ for (let i = start + 1; i < lines.length; i++) {
442
+ if (/^##\s/.test(lines[i])) {
443
+ break;
444
+ }
445
+ const m = /^-\s+(.*)$/.exec(lines[i].trim());
446
+ if (m) {
447
+ questions.push(m[1]);
448
+ }
449
+ }
450
+ return questions;
451
+ }