@warpgogol/forge 9.3.1 → 9.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (509) hide show
  1. package/AGENTS.md +32 -0
  2. package/CHANGELOG.md +40 -0
  3. package/README.md +6 -4
  4. package/README.uk.md +10 -8
  5. package/bin/cli.ts +4 -4
  6. package/dist/bin/cli.js +4 -4
  7. package/dist/bin/cli.js.map +1 -1
  8. package/dist/os/adr/handlers/list-create.d.ts.map +1 -1
  9. package/dist/os/adr/handlers/list-create.js +23 -2
  10. package/dist/os/adr/handlers/list-create.js.map +1 -1
  11. package/dist/os/adr/types.d.ts +2 -0
  12. package/dist/os/adr/types.d.ts.map +1 -1
  13. package/dist/os/adr/types.js +2 -2
  14. package/dist/os/adr/types.js.map +1 -1
  15. package/dist/os/compass/handlers/compass-audit-handler.d.ts.map +1 -1
  16. package/dist/os/compass/handlers/compass-audit-handler.js +3 -1
  17. package/dist/os/compass/handlers/compass-audit-handler.js.map +1 -1
  18. package/dist/os/compass/handlers/compass-header-scaffold-handler.d.ts.map +1 -1
  19. package/dist/os/compass/handlers/compass-header-scaffold-handler.js +6 -12
  20. package/dist/os/compass/handlers/compass-header-scaffold-handler.js.map +1 -1
  21. package/dist/os/compass/handlers/git-revision.d.ts +2 -0
  22. package/dist/os/compass/handlers/git-revision.d.ts.map +1 -1
  23. package/dist/os/compass/handlers/git-revision.js +7 -2
  24. package/dist/os/compass/handlers/git-revision.js.map +1 -1
  25. package/dist/os/compass/handlers/summary-record.d.ts.map +1 -1
  26. package/dist/os/compass/handlers/summary-record.js +24 -14
  27. package/dist/os/compass/handlers/summary-record.js.map +1 -1
  28. package/dist/os/conformance/conformance.module.d.ts.map +1 -1
  29. package/dist/os/conformance/conformance.module.js +21 -4
  30. package/dist/os/conformance/conformance.module.js.map +1 -1
  31. package/dist/os/conformance/handlers/conformance-validate.d.ts.map +1 -1
  32. package/dist/os/conformance/handlers/conformance-validate.js +64 -8
  33. package/dist/os/conformance/handlers/conformance-validate.js.map +1 -1
  34. package/dist/os/core/archive-regen.d.ts +99 -0
  35. package/dist/os/core/archive-regen.d.ts.map +1 -0
  36. package/dist/os/core/archive-regen.js +220 -0
  37. package/dist/os/core/archive-regen.js.map +1 -0
  38. package/dist/os/core/core.module.d.ts.map +1 -1
  39. package/dist/os/core/core.module.js +159 -18
  40. package/dist/os/core/core.module.js.map +1 -1
  41. package/dist/os/core/handlers/assets-helpers.d.ts.map +1 -1
  42. package/dist/os/core/handlers/assets-helpers.js +8 -3
  43. package/dist/os/core/handlers/assets-helpers.js.map +1 -1
  44. package/dist/os/core/handlers/forge-autonomy-validate.d.ts.map +1 -1
  45. package/dist/os/core/handlers/forge-autonomy-validate.js +3 -1
  46. package/dist/os/core/handlers/forge-autonomy-validate.js.map +1 -1
  47. package/dist/os/core/handlers/package-health.d.ts +130 -0
  48. package/dist/os/core/handlers/package-health.d.ts.map +1 -1
  49. package/dist/os/core/handlers/package-health.js +548 -7
  50. package/dist/os/core/handlers/package-health.js.map +1 -1
  51. package/dist/os/core/handlers/release-prepare.d.ts.map +1 -1
  52. package/dist/os/core/handlers/release-prepare.js +5 -2
  53. package/dist/os/core/handlers/release-prepare.js.map +1 -1
  54. package/dist/os/flow/digest.d.ts.map +1 -1
  55. package/dist/os/flow/digest.js +3 -1
  56. package/dist/os/flow/digest.js.map +1 -1
  57. package/dist/os/flow/dirty-classify.d.ts +51 -0
  58. package/dist/os/flow/dirty-classify.d.ts.map +1 -0
  59. package/dist/os/flow/dirty-classify.js +83 -0
  60. package/dist/os/flow/dirty-classify.js.map +1 -0
  61. package/dist/os/flow/flow.module.d.ts.map +1 -1
  62. package/dist/os/flow/flow.module.js +110 -19
  63. package/dist/os/flow/flow.module.js.map +1 -1
  64. package/dist/os/flow/gate-engine.d.ts +13 -0
  65. package/dist/os/flow/gate-engine.d.ts.map +1 -0
  66. package/dist/os/flow/gate-engine.js +32 -0
  67. package/dist/os/flow/gate-engine.js.map +1 -0
  68. package/dist/os/flow/git.d.ts +45 -4
  69. package/dist/os/flow/git.d.ts.map +1 -1
  70. package/dist/os/flow/git.js +95 -27
  71. package/dist/os/flow/git.js.map +1 -1
  72. package/dist/os/flow/handlers/flow-backmerge.d.ts.map +1 -1
  73. package/dist/os/flow/handlers/flow-backmerge.js +12 -5
  74. package/dist/os/flow/handlers/flow-backmerge.js.map +1 -1
  75. package/dist/os/flow/handlers/flow-check.d.ts.map +1 -1
  76. package/dist/os/flow/handlers/flow-check.js +25 -3
  77. package/dist/os/flow/handlers/flow-check.js.map +1 -1
  78. package/dist/os/flow/handlers/flow-cut.d.ts +4 -1
  79. package/dist/os/flow/handlers/flow-cut.d.ts.map +1 -1
  80. package/dist/os/flow/handlers/flow-cut.js +75 -38
  81. package/dist/os/flow/handlers/flow-cut.js.map +1 -1
  82. package/dist/os/flow/handlers/flow-discard.d.ts.map +1 -1
  83. package/dist/os/flow/handlers/flow-discard.js +33 -5
  84. package/dist/os/flow/handlers/flow-discard.js.map +1 -1
  85. package/dist/os/flow/handlers/flow-fix.d.ts.map +1 -1
  86. package/dist/os/flow/handlers/flow-fix.js +13 -10
  87. package/dist/os/flow/handlers/flow-fix.js.map +1 -1
  88. package/dist/os/flow/handlers/flow-hold.d.ts.map +1 -1
  89. package/dist/os/flow/handlers/flow-hold.js +13 -7
  90. package/dist/os/flow/handlers/flow-hold.js.map +1 -1
  91. package/dist/os/flow/handlers/flow-install.d.ts +2 -1
  92. package/dist/os/flow/handlers/flow-install.d.ts.map +1 -1
  93. package/dist/os/flow/handlers/flow-install.js +56 -13
  94. package/dist/os/flow/handlers/flow-install.js.map +1 -1
  95. package/dist/os/flow/handlers/flow-integrate.d.ts +54 -2
  96. package/dist/os/flow/handlers/flow-integrate.d.ts.map +1 -1
  97. package/dist/os/flow/handlers/flow-integrate.js +614 -65
  98. package/dist/os/flow/handlers/flow-integrate.js.map +1 -1
  99. package/dist/os/flow/handlers/flow-open.d.ts +14 -0
  100. package/dist/os/flow/handlers/flow-open.d.ts.map +1 -1
  101. package/dist/os/flow/handlers/flow-open.js +117 -12
  102. package/dist/os/flow/handlers/flow-open.js.map +1 -1
  103. package/dist/os/flow/handlers/flow-peers.d.ts +24 -0
  104. package/dist/os/flow/handlers/flow-peers.d.ts.map +1 -0
  105. package/dist/os/flow/handlers/flow-peers.js +99 -0
  106. package/dist/os/flow/handlers/flow-peers.js.map +1 -0
  107. package/dist/os/flow/handlers/flow-status.d.ts +13 -0
  108. package/dist/os/flow/handlers/flow-status.d.ts.map +1 -1
  109. package/dist/os/flow/handlers/flow-status.js +35 -14
  110. package/dist/os/flow/handlers/flow-status.js.map +1 -1
  111. package/dist/os/flow/index.d.ts +3 -0
  112. package/dist/os/flow/index.d.ts.map +1 -1
  113. package/dist/os/flow/index.js +7 -2
  114. package/dist/os/flow/index.js.map +1 -1
  115. package/dist/os/flow/line-version.d.ts.map +1 -1
  116. package/dist/os/flow/line-version.js +2 -1
  117. package/dist/os/flow/line-version.js.map +1 -1
  118. package/dist/os/flow/locks.d.ts +37 -1
  119. package/dist/os/flow/locks.d.ts.map +1 -1
  120. package/dist/os/flow/locks.js +34 -14
  121. package/dist/os/flow/locks.js.map +1 -1
  122. package/dist/os/flow/ops-registry.d.ts +33 -0
  123. package/dist/os/flow/ops-registry.d.ts.map +1 -0
  124. package/dist/os/flow/ops-registry.js +177 -0
  125. package/dist/os/flow/ops-registry.js.map +1 -0
  126. package/dist/os/flow/preflight.d.ts +157 -0
  127. package/dist/os/flow/preflight.d.ts.map +1 -0
  128. package/dist/os/flow/preflight.js +772 -0
  129. package/dist/os/flow/preflight.js.map +1 -0
  130. package/dist/os/flow/publish.d.ts +7 -0
  131. package/dist/os/flow/publish.d.ts.map +1 -1
  132. package/dist/os/flow/publish.js +165 -3
  133. package/dist/os/flow/publish.js.map +1 -1
  134. package/dist/os/flow/registry.d.ts +26 -1
  135. package/dist/os/flow/registry.d.ts.map +1 -1
  136. package/dist/os/flow/registry.js +27 -6
  137. package/dist/os/flow/registry.js.map +1 -1
  138. package/dist/os/flow/scripts.d.ts +4 -0
  139. package/dist/os/flow/scripts.d.ts.map +1 -1
  140. package/dist/os/flow/scripts.js +8 -2
  141. package/dist/os/flow/scripts.js.map +1 -1
  142. package/dist/os/flow/session-checkout.d.ts +33 -0
  143. package/dist/os/flow/session-checkout.d.ts.map +1 -0
  144. package/dist/os/flow/session-checkout.js +151 -0
  145. package/dist/os/flow/session-checkout.js.map +1 -0
  146. package/dist/os/flow/standalone-invoke.d.ts +11 -0
  147. package/dist/os/flow/standalone-invoke.d.ts.map +1 -0
  148. package/dist/os/flow/standalone-invoke.js +75 -0
  149. package/dist/os/flow/standalone-invoke.js.map +1 -0
  150. package/dist/os/flow/worktree-path.d.ts.map +1 -1
  151. package/dist/os/flow/worktree-path.js +9 -3
  152. package/dist/os/flow/worktree-path.js.map +1 -1
  153. package/dist/os/instructions/handlers/budget-validate.d.ts +1 -0
  154. package/dist/os/instructions/handlers/budget-validate.d.ts.map +1 -1
  155. package/dist/os/instructions/handlers/budget-validate.js +12 -4
  156. package/dist/os/instructions/handlers/budget-validate.js.map +1 -1
  157. package/dist/os/instructions/instructions.module.d.ts.map +1 -1
  158. package/dist/os/instructions/instructions.module.js +4 -1
  159. package/dist/os/instructions/instructions.module.js.map +1 -1
  160. package/dist/os/mission/handlers/archive.d.ts.map +1 -1
  161. package/dist/os/mission/handlers/archive.js +12 -8
  162. package/dist/os/mission/handlers/archive.js.map +1 -1
  163. package/dist/os/mission/importer-block.d.ts +19 -0
  164. package/dist/os/mission/importer-block.d.ts.map +1 -0
  165. package/dist/os/mission/importer-block.js +124 -0
  166. package/dist/os/mission/importer-block.js.map +1 -0
  167. package/dist/os/mission/lockfile.d.ts +17 -0
  168. package/dist/os/mission/lockfile.d.ts.map +1 -1
  169. package/dist/os/mission/lockfile.js +38 -0
  170. package/dist/os/mission/lockfile.js.map +1 -1
  171. package/dist/os/mission/open-missions.d.ts +8 -0
  172. package/dist/os/mission/open-missions.d.ts.map +1 -0
  173. package/dist/os/mission/open-missions.js +42 -0
  174. package/dist/os/mission/open-missions.js.map +1 -0
  175. package/dist/os/queue/manifest.d.ts.map +1 -1
  176. package/dist/os/queue/manifest.js +11 -1
  177. package/dist/os/queue/manifest.js.map +1 -1
  178. package/dist/os/rfc/acceptance.d.ts +14 -0
  179. package/dist/os/rfc/acceptance.d.ts.map +1 -1
  180. package/dist/os/rfc/acceptance.js +18 -10
  181. package/dist/os/rfc/acceptance.js.map +1 -1
  182. package/dist/os/rfc/decision-log.d.ts.map +1 -1
  183. package/dist/os/rfc/decision-log.js +11 -2
  184. package/dist/os/rfc/decision-log.js.map +1 -1
  185. package/dist/os/rfc/frontmatter-io.d.ts +23 -0
  186. package/dist/os/rfc/frontmatter-io.d.ts.map +1 -1
  187. package/dist/os/rfc/frontmatter-io.js +82 -1
  188. package/dist/os/rfc/frontmatter-io.js.map +1 -1
  189. package/dist/os/rfc/handlers/implement-stamp-deviations.d.ts.map +1 -1
  190. package/dist/os/rfc/handlers/implement-stamp-deviations.js +24 -2
  191. package/dist/os/rfc/handlers/implement-stamp-deviations.js.map +1 -1
  192. package/dist/os/rfc/handlers/implement-stamp.d.ts.map +1 -1
  193. package/dist/os/rfc/handlers/implement-stamp.js +16 -3
  194. package/dist/os/rfc/handlers/implement-stamp.js.map +1 -1
  195. package/dist/os/rfc/handlers/index-graph.d.ts.map +1 -1
  196. package/dist/os/rfc/handlers/index-graph.js +5 -4
  197. package/dist/os/rfc/handlers/index-graph.js.map +1 -1
  198. package/dist/os/rfc/handlers/list-create.d.ts.map +1 -1
  199. package/dist/os/rfc/handlers/list-create.js +107 -10
  200. package/dist/os/rfc/handlers/list-create.js.map +1 -1
  201. package/dist/os/rfc/handlers/rfc-evidence-sync.d.ts +48 -0
  202. package/dist/os/rfc/handlers/rfc-evidence-sync.d.ts.map +1 -1
  203. package/dist/os/rfc/handlers/rfc-evidence-sync.js +131 -2
  204. package/dist/os/rfc/handlers/rfc-evidence-sync.js.map +1 -1
  205. package/dist/os/rfc/handlers/validate-rules.d.ts +21 -1
  206. package/dist/os/rfc/handlers/validate-rules.d.ts.map +1 -1
  207. package/dist/os/rfc/handlers/validate-rules.js +77 -5
  208. package/dist/os/rfc/handlers/validate-rules.js.map +1 -1
  209. package/dist/os/rfc/handlers/validate.d.ts.map +1 -1
  210. package/dist/os/rfc/handlers/validate.js +64 -6
  211. package/dist/os/rfc/handlers/validate.js.map +1 -1
  212. package/dist/os/rfc/next-id.d.ts +38 -0
  213. package/dist/os/rfc/next-id.d.ts.map +1 -0
  214. package/dist/os/rfc/next-id.js +165 -0
  215. package/dist/os/rfc/next-id.js.map +1 -0
  216. package/dist/os/rfc/probe-batch.d.ts.map +1 -1
  217. package/dist/os/rfc/probe-batch.js +6 -1
  218. package/dist/os/rfc/probe-batch.js.map +1 -1
  219. package/dist/os/rfc/rfc.module.d.ts.map +1 -1
  220. package/dist/os/rfc/rfc.module.js +36 -8
  221. package/dist/os/rfc/rfc.module.js.map +1 -1
  222. package/dist/os/rfc/types.d.ts +42 -6
  223. package/dist/os/rfc/types.d.ts.map +1 -1
  224. package/dist/os/rfc/types.js +6 -6
  225. package/dist/os/rfc/types.js.map +1 -1
  226. package/dist/os/rfc/verification-evidence.d.ts +1 -1
  227. package/dist/os/rfc/verification-evidence.d.ts.map +1 -1
  228. package/dist/os/rfc/verification-evidence.js +81 -28
  229. package/dist/os/rfc/verification-evidence.js.map +1 -1
  230. package/dist/os/rfc/verification-origin.d.ts +8 -0
  231. package/dist/os/rfc/verification-origin.d.ts.map +1 -1
  232. package/dist/os/rfc/verification-origin.js +47 -0
  233. package/dist/os/rfc/verification-origin.js.map +1 -1
  234. package/dist/os/session/handlers/save.d.ts.map +1 -1
  235. package/dist/os/session/handlers/save.js +31 -7
  236. package/dist/os/session/handlers/save.js.map +1 -1
  237. package/dist/os/spec/spec-schema.d.ts +8 -0
  238. package/dist/os/spec/spec-schema.d.ts.map +1 -1
  239. package/dist/os/spec/spec-schema.js +1 -0
  240. package/dist/os/spec/spec-schema.js.map +1 -1
  241. package/dist/os/spec/spec-status.d.ts +8 -0
  242. package/dist/os/spec/spec-status.d.ts.map +1 -1
  243. package/dist/os/spec/spec-status.js +18 -6
  244. package/dist/os/spec/spec-status.js.map +1 -1
  245. package/dist/os/spec/spec-validate.d.ts.map +1 -1
  246. package/dist/os/spec/spec-validate.js +62 -15
  247. package/dist/os/spec/spec-validate.js.map +1 -1
  248. package/dist/os/spec/spec.module.d.ts.map +1 -1
  249. package/dist/os/spec/spec.module.js +13 -5
  250. package/dist/os/spec/spec.module.js.map +1 -1
  251. package/dist/os/system/handlers/system-map-validate.d.ts.map +1 -1
  252. package/dist/os/system/handlers/system-map-validate.js +2 -1
  253. package/dist/os/system/handlers/system-map-validate.js.map +1 -1
  254. package/dist/os/system/handlers/system-outbox-scan.d.ts +11 -0
  255. package/dist/os/system/handlers/system-outbox-scan.d.ts.map +1 -1
  256. package/dist/os/system/handlers/system-outbox-scan.js +54 -6
  257. package/dist/os/system/handlers/system-outbox-scan.js.map +1 -1
  258. package/dist/os/system/handlers/system-task-collect.d.ts.map +1 -1
  259. package/dist/os/system/handlers/system-task-collect.js +11 -5
  260. package/dist/os/system/handlers/system-task-collect.js.map +1 -1
  261. package/dist/os/system/handlers/system-task-create.d.ts.map +1 -1
  262. package/dist/os/system/handlers/system-task-create.js +6 -1
  263. package/dist/os/system/handlers/system-task-create.js.map +1 -1
  264. package/dist/os/system/handlers/system-versionset-record.d.ts +14 -0
  265. package/dist/os/system/handlers/system-versionset-record.d.ts.map +1 -1
  266. package/dist/os/system/handlers/system-versionset-record.js +246 -6
  267. package/dist/os/system/handlers/system-versionset-record.js.map +1 -1
  268. package/dist/os/system/repo.d.ts +1 -1
  269. package/dist/os/system/repo.d.ts.map +1 -1
  270. package/dist/os/system/repo.js +53 -3
  271. package/dist/os/system/repo.js.map +1 -1
  272. package/dist/os/system/scenario-schemas.d.ts +109 -3
  273. package/dist/os/system/scenario-schemas.d.ts.map +1 -1
  274. package/dist/os/system/scenario-schemas.js +66 -10
  275. package/dist/os/system/scenario-schemas.js.map +1 -1
  276. package/dist/os/system/system.module.d.ts.map +1 -1
  277. package/dist/os/system/system.module.js +69 -17
  278. package/dist/os/system/system.module.js.map +1 -1
  279. package/dist/os/system/tests/contract-fixtures.d.ts +17 -0
  280. package/dist/os/system/tests/contract-fixtures.d.ts.map +1 -0
  281. package/dist/os/system/tests/contract-fixtures.js +109 -0
  282. package/dist/os/system/tests/contract-fixtures.js.map +1 -0
  283. package/dist/src/config/forge-config.d.ts +54 -18
  284. package/dist/src/config/forge-config.d.ts.map +1 -1
  285. package/dist/src/config/forge-config.js +41 -9
  286. package/dist/src/config/forge-config.js.map +1 -1
  287. package/dist/src/conformance/index.d.ts +6 -0
  288. package/dist/src/conformance/index.d.ts.map +1 -1
  289. package/dist/src/conformance/index.js +36 -1
  290. package/dist/src/conformance/index.js.map +1 -1
  291. package/dist/src/formal/proof-runner.d.ts.map +1 -1
  292. package/dist/src/formal/proof-runner.js +10 -2
  293. package/dist/src/formal/proof-runner.js.map +1 -1
  294. package/dist/src/migration-adapters/node-typescript-pnpm/index.js +3 -1
  295. package/dist/src/migration-adapters/node-typescript-pnpm/index.js.map +1 -1
  296. package/dist/src/migration-adapters/phaser-pnpm/index.js +3 -1
  297. package/dist/src/migration-adapters/phaser-pnpm/index.js.map +1 -1
  298. package/dist/src/onboarding/agents-generate.js +4 -4
  299. package/dist/src/onboarding/agents-generate.js.map +1 -1
  300. package/dist/src/onboarding/nested-agents-generate.d.ts.map +1 -1
  301. package/dist/src/onboarding/nested-agents-generate.js +3 -1
  302. package/dist/src/onboarding/nested-agents-generate.js.map +1 -1
  303. package/dist/src/onboarding/prettierignore.d.ts.map +1 -1
  304. package/dist/src/onboarding/prettierignore.js +2 -0
  305. package/dist/src/onboarding/prettierignore.js.map +1 -1
  306. package/dist/src/onboarding/scaffold-project.js +2 -2
  307. package/dist/src/onboarding/scaffold-project.js.map +1 -1
  308. package/dist/src/onboarding/upgrade.d.ts.map +1 -1
  309. package/dist/src/onboarding/upgrade.js +14 -2
  310. package/dist/src/onboarding/upgrade.js.map +1 -1
  311. package/dist/src/onboarding/workspace-discovery.d.ts.map +1 -1
  312. package/dist/src/onboarding/workspace-discovery.js +3 -1
  313. package/dist/src/onboarding/workspace-discovery.js.map +1 -1
  314. package/dist/src/skill-schema.d.ts +3 -0
  315. package/dist/src/skill-schema.d.ts.map +1 -1
  316. package/dist/src/skill-schema.js +9 -2
  317. package/dist/src/skill-schema.js.map +1 -1
  318. package/dist/src/spec/resolve-spec-source.d.ts +45 -0
  319. package/dist/src/spec/resolve-spec-source.d.ts.map +1 -0
  320. package/dist/src/spec/resolve-spec-source.js +153 -0
  321. package/dist/src/spec/resolve-spec-source.js.map +1 -0
  322. package/dist/src/utils/command-inventory.d.ts +28 -0
  323. package/dist/src/utils/command-inventory.d.ts.map +1 -0
  324. package/dist/src/utils/command-inventory.js +84 -0
  325. package/dist/src/utils/command-inventory.js.map +1 -0
  326. package/dist/src/utils/io.d.ts.map +1 -1
  327. package/dist/src/utils/io.js +6 -1
  328. package/dist/src/utils/io.js.map +1 -1
  329. package/dist/src/utils/repo-path.d.ts +10 -0
  330. package/dist/src/utils/repo-path.d.ts.map +1 -0
  331. package/dist/src/utils/repo-path.js +52 -0
  332. package/dist/src/utils/repo-path.js.map +1 -0
  333. package/dist/src/utils/spawn.d.ts +23 -0
  334. package/dist/src/utils/spawn.d.ts.map +1 -0
  335. package/dist/src/utils/spawn.js +83 -0
  336. package/dist/src/utils/spawn.js.map +1 -0
  337. package/docs/branch-model.md +1 -1
  338. package/docs/guides/existing-project.md +1 -1
  339. package/docs/maintainers/publishing.md +1 -1
  340. package/docs/reference/cli.md +8 -2
  341. package/docs/reference/profiles.md +1 -1
  342. package/os/adr/handlers/list-create.ts +26 -2
  343. package/os/adr/types.ts +4 -2
  344. package/os/compass/handlers/compass-audit-handler.ts +6 -1
  345. package/os/compass/handlers/compass-header-scaffold-handler.ts +12 -11
  346. package/os/compass/handlers/git-revision.ts +8 -2
  347. package/os/compass/handlers/summary-record.ts +25 -14
  348. package/os/compass/handlers/tests/compass-invariant-binding.test.ts +3 -2
  349. package/os/conformance/conformance.module.ts +21 -4
  350. package/os/conformance/handlers/conformance-validate.ts +108 -16
  351. package/os/conformance/handlers/tests/conformance-package-consumer.test.ts +200 -0
  352. package/os/core/archive-regen.ts +279 -0
  353. package/os/core/core.module.ts +176 -29
  354. package/os/core/docs-archive.test.ts +413 -3
  355. package/os/core/handlers/assets-helpers.ts +8 -3
  356. package/os/core/handlers/forge-autonomy-validate.ts +3 -1
  357. package/os/core/handlers/package-health.test.ts +418 -4
  358. package/os/core/handlers/package-health.ts +629 -7
  359. package/os/core/handlers/release-prepare.ts +5 -2
  360. package/os/flow/digest.ts +3 -1
  361. package/os/flow/dirty-classify.ts +120 -0
  362. package/os/flow/flow.module.ts +119 -24
  363. package/os/flow/gate-engine.ts +45 -0
  364. package/os/flow/git.ts +117 -29
  365. package/os/flow/handlers/flow-backmerge.ts +14 -4
  366. package/os/flow/handlers/flow-check.ts +27 -2
  367. package/os/flow/handlers/flow-cut.ts +124 -56
  368. package/os/flow/handlers/flow-discard.ts +33 -5
  369. package/os/flow/handlers/flow-fix.ts +22 -10
  370. package/os/flow/handlers/flow-hold.ts +20 -7
  371. package/os/flow/handlers/flow-install.ts +59 -12
  372. package/os/flow/handlers/flow-integrate.ts +933 -180
  373. package/os/flow/handlers/flow-open.ts +135 -11
  374. package/os/flow/handlers/flow-peers.ts +142 -0
  375. package/os/flow/handlers/flow-status.ts +45 -13
  376. package/os/flow/index.ts +8 -2
  377. package/os/flow/line-version.ts +2 -1
  378. package/os/flow/locks.ts +56 -14
  379. package/os/flow/ops-registry.ts +194 -0
  380. package/os/flow/preflight.ts +974 -0
  381. package/os/flow/publish.ts +200 -5
  382. package/os/flow/registry.ts +43 -7
  383. package/os/flow/scripts.ts +13 -2
  384. package/os/flow/session-checkout.ts +186 -0
  385. package/os/flow/standalone-invoke.ts +85 -0
  386. package/os/flow/tests/dirty-classify.test.ts +251 -0
  387. package/os/flow/tests/fixtures/branch-names.yaml +40 -40
  388. package/os/flow/tests/flow-cut.test.ts +21 -0
  389. package/os/flow/tests/flow-discard.test.ts +32 -0
  390. package/os/flow/tests/flow-guard-parity.test.ts +10 -2
  391. package/os/flow/tests/flow-install.test.ts +7 -3
  392. package/os/flow/tests/flow-integrate.gate-report.test.ts +144 -0
  393. package/os/flow/tests/flow-integrate.preflight.test.ts +565 -0
  394. package/os/flow/tests/flow-integrate.reattach.test.ts +197 -0
  395. package/os/flow/tests/flow-integrate.remediation.test.ts +254 -0
  396. package/os/flow/tests/flow-integrate.test.ts +698 -17
  397. package/os/flow/tests/flow-open.pending.test.ts +277 -0
  398. package/os/flow/tests/flow-open.test.ts +52 -9
  399. package/os/flow/tests/flow-push-publish.test.ts +124 -0
  400. package/os/flow/tests/flow-status.test.ts +31 -1
  401. package/os/flow/tests/locks.test.ts +39 -3
  402. package/os/flow/tests/ops-registry.test.ts +136 -0
  403. package/os/flow/tests/scripts.test.ts +5 -2
  404. package/os/flow/tests/state-roots.test.ts +8 -5
  405. package/os/flow/worktree-path.ts +9 -3
  406. package/os/formal/handlers/proof-check.test.ts +6 -3
  407. package/os/instructions/budget.test.ts +31 -0
  408. package/os/instructions/handlers/budget-validate.ts +13 -4
  409. package/os/instructions/instructions.module.ts +4 -1
  410. package/os/mission/handlers/archive.ts +22 -14
  411. package/os/mission/importer-block.ts +119 -0
  412. package/os/mission/lockfile.ts +41 -0
  413. package/os/mission/open-missions.ts +50 -0
  414. package/os/queue/decision-ledger.test.ts +18 -0
  415. package/os/queue/manifest.ts +17 -1
  416. package/os/rfc/acceptance.ts +30 -10
  417. package/os/rfc/decision-log.ts +13 -2
  418. package/os/rfc/frontmatter-io.ts +89 -1
  419. package/os/rfc/handlers/implement-stamp-deviations.test.ts +83 -0
  420. package/os/rfc/handlers/implement-stamp-deviations.ts +22 -2
  421. package/os/rfc/handlers/implement-stamp-stale.test.ts +5 -1
  422. package/os/rfc/handlers/implement-stamp.ts +16 -3
  423. package/os/rfc/handlers/index-graph.ts +5 -4
  424. package/os/rfc/handlers/lifecycle.test.ts +136 -0
  425. package/os/rfc/handlers/list-create.test.ts +126 -0
  426. package/os/rfc/handlers/list-create.ts +130 -10
  427. package/os/rfc/handlers/rfc-evidence-sync.test.ts +504 -0
  428. package/os/rfc/handlers/rfc-evidence-sync.ts +192 -2
  429. package/os/rfc/handlers/tests/index-graph.test.ts +2 -1
  430. package/os/rfc/handlers/validate-rules-rfc1454.test.ts +73 -0
  431. package/os/rfc/handlers/validate-rules.ts +103 -7
  432. package/os/rfc/handlers/validate.ts +68 -4
  433. package/os/rfc/next-id.ts +232 -0
  434. package/os/rfc/probe-batch.ts +6 -1
  435. package/os/rfc/rfc.module.ts +41 -8
  436. package/os/rfc/types.ts +31 -6
  437. package/os/rfc/verification-emit-freshness.test.ts +5 -1
  438. package/os/rfc/verification-emit.test.ts +5 -1
  439. package/os/rfc/verification-evidence.ts +99 -33
  440. package/os/rfc/verification-origin.ts +59 -0
  441. package/os/rfc/verification-refresh.test.ts +5 -1
  442. package/os/rfc/verifier/rfc-1333/imp12-binding.acceptance.test.ts +5 -1
  443. package/os/session/handlers/save.ts +35 -7
  444. package/os/session/tests/session-save.test.ts +154 -0
  445. package/os/spec/spec-schema.ts +3 -0
  446. package/os/spec/spec-status.ts +22 -6
  447. package/os/spec/spec-validate.package-source.test.ts +187 -0
  448. package/os/spec/spec-validate.ts +65 -14
  449. package/os/spec/spec.module.ts +13 -5
  450. package/os/system/handlers/system-map-validate.ts +4 -2
  451. package/os/system/handlers/system-outbox-scan.ts +65 -21
  452. package/os/system/handlers/system-task-collect.ts +11 -5
  453. package/os/system/handlers/system-task-create.ts +6 -1
  454. package/os/system/handlers/system-versionset-record.ts +301 -16
  455. package/os/system/repo.ts +59 -18
  456. package/os/system/scenario-schemas.ts +91 -12
  457. package/os/system/system.module.ts +70 -17
  458. package/os/system/tests/contract-fixtures.ts +139 -0
  459. package/os/system/tests/scenario-pin-drift-check.test.ts +114 -0
  460. package/os/system/tests/scenario-validator-check.test.ts +96 -0
  461. package/os/system/tests/system-task-collect.test.ts +16 -0
  462. package/os/system/tests/system-task-dispatch.test.ts +4 -1
  463. package/os/system/tests/system-versionset.test.ts +7 -3
  464. package/os/system/tests/version-set-v2.test.ts +78 -0
  465. package/package.json +9 -1
  466. package/skills/fo/fo-doctor/SKILL.md +151 -0
  467. package/skills/fo/fo-handoff/SKILL.md +22 -0
  468. package/skills/fo/fo-idea-create-rfc/acceptance-criteria-standard.md +2 -1
  469. package/skills/fo/fo-idea-implement/SKILL.md +5 -2
  470. package/skills/fo/fo-idea-plan/SKILL.md +5 -5
  471. package/skills/fo/fo-session-retro/SKILL.md +45 -13
  472. package/skills/fo/fo-spec-ingest/SKILL.md +8 -0
  473. package/skills/fo/fo-system-dispatch/SKILL.md +1 -0
  474. package/skills/meta/skill-create/SKILL.md +2 -1
  475. package/src/bin-entry.test.ts +3 -1
  476. package/src/config/forge-config.ts +57 -13
  477. package/src/conformance/index.ts +46 -1
  478. package/src/formal/proof-runner.ts +10 -2
  479. package/src/migration-adapters/node-typescript-pnpm/index.ts +3 -1
  480. package/src/migration-adapters/phaser-pnpm/index.ts +3 -1
  481. package/src/onboarding/agents-generate.ts +4 -4
  482. package/src/onboarding/nested-agents-generate.ts +3 -1
  483. package/src/onboarding/prettierignore.ts +2 -0
  484. package/src/onboarding/scaffold-project.ts +2 -2
  485. package/src/onboarding/upgrade.ts +13 -2
  486. package/src/onboarding/workspace-discovery.ts +3 -1
  487. package/src/pipeline-status.test.ts +1 -1
  488. package/src/skill-schema.ts +9 -2
  489. package/src/spec/resolve-spec-source.ts +184 -0
  490. package/src/tests/acceptance-probe-kinds.test.ts +3 -1
  491. package/src/tests/agents-generate-domain.test.ts +3 -2
  492. package/src/tests/agents-generate.test.ts +2 -1
  493. package/src/tests/create-flow.test.ts +3 -1
  494. package/src/tests/create.test.ts +12 -2
  495. package/src/tests/editable-generated-region.test.ts +2 -2
  496. package/src/tests/fixtures/agents-generate-business-before.txt +2 -0
  497. package/src/tests/implement-stamp.test.ts +5 -2
  498. package/src/tests/prettierignore-manage.test.ts +11 -0
  499. package/src/tests/rfc-implement-stamp-governance.test.ts +6 -2
  500. package/src/tests/rfc-verification-verdict-ingest.test.ts +5 -1
  501. package/src/tests/scaffold-project.test.ts +3 -1
  502. package/src/tests/upgrade.test.ts +79 -3
  503. package/src/tests/workspace-discovery.test.ts +4 -3
  504. package/src/utils/command-inventory.ts +105 -0
  505. package/src/utils/io.ts +6 -1
  506. package/src/utils/repo-path.ts +49 -0
  507. package/src/utils/spawn.ts +88 -0
  508. package/src/tests/rfc-evidence-sync.test.ts +0 -369
  509. package/src/tests/skill-agents-sync.test.ts +0 -192
@@ -20,25 +20,29 @@ at cleanup without moving develop again.</purpose>
20
20
  <item>Task-file preservation runs inside the cleanup branch BEFORE the worktree-teardown hook and only for owner.kind task — a `git clean -fdx` teardown would destroy `.forge/task/` first, and a copy failure refuses cleanup (FLOW-INT-04) so the report is never lost with the worktree (RFC-1509 Q-16).</item>
21
21
  </KEY_DECISIONS>
22
22
  <CHANGE_SUMMARY>
23
- <item>RFC-1504: steps 2-3 — holderIsDead, boot-id payload, dead-holder break in withFlowLock</item>
24
- <item>RFC-1509: implement RFC-1509 — system task queue, dispatch briefs and task reports</item>
25
- <item>RFC-1509: record task-file preservation decisions in flow handlers</item>
26
- <item>RFC-1509: close RFC-1509 review r1 findings — mark-failed escape, open-error refusal, dry-run coverage</item>
27
- <item>RFC-1509: record RFC-1509 preserve-before-teardown ordering in key decisions</item>
28
- <history>RFC-1487, RFC-1490, RFC-1491</history>
23
+ <item>RFC-1531: remediation flags --reattach/--auto-clean/--autocommit on the classified dirty set, FLOW-INT-17 remediation-failure refusals, classified FLOW-INT-01 data</item>
24
+ <item>RFC-1532: persist integration-gate reports per attempt and link them from the flow journal</item>
25
+ <item>RFC-1545: standalone generated-conflict regeneration in flow.integrate</item>
26
+ <item>RFC-1543: attribute shared-checkout dirt to live ops and expose peers (RFC-1543)</item>
27
+ <item>RFC-1544: attribute mission lockfile churn to owning checkout and auto-resolve foreign importer diffs (RFC-1544)</item>
28
+ <history>RFC-1487, RFC-1490, RFC-1491, RFC-1504, RFC-1509, RFC-1515, RFC-1528, RFC-1536, RFC-1537, RFC-1538</history>
29
29
  </CHANGE_SUMMARY>
30
30
  */
31
31
  import { resolveIo } from "../../../src/utils/io.js";
32
- import { branchExists, currentBranch, git, isAncestor, requireGit, statusDirty, statusTracked, topLevel, worktreeList, } from "../git.js";
33
- import { LockTimeoutError, withFlowLock } from "../locks.js";
34
- import { appendJournal, commonGitDir, FlowRegistrySchemaError, INTEGRATE_LOCK_RELATIVE_PATH, readRegistry, registryPath, updateRegistry, } from "../registry.js";
32
+ import { AUTO_CLEAN_PASS_CLASSES, SWEEP_COMMIT_SUBJECT, classifyDirtyPath, classifyDirtyTree, summarizeClasses, } from "../dirty-classify.js";
33
+ import { branchExists, currentBranch, git, gitCommit, isAncestor, requireGit, statusDirty, statusTracked, topLevel, worktreeAdd, worktreeList, } from "../git.js";
34
+ import { describeHeldLock, LockTimeoutError, withFlowLock, } from "../locks.js";
35
+ import { appendJournal, readJournal, commonGitDir, FlowRegistrySchemaError, INTEGRATE_LOCK_RELATIVE_PATH, readRegistry, registryPath, updateRegistry, } from "../registry.js";
35
36
  import { runFlowScript } from "../scripts.js";
37
+ import { standaloneInvoke } from "../standalone-invoke.js";
36
38
  import { preserveTaskFiles } from "../task-files.js";
37
- import { primaryWorktree } from "../worktree-path.js";
38
- import { join } from "node:path";
39
+ import { flowWorktreePath, primaryWorktree } from "../worktree-path.js";
40
+ import { pruneReservationsForSession } from "../../rfc/next-id.js";
41
+ import { rm } from "node:fs/promises";
42
+ import { dirname, join } from "node:path";
39
43
  export const INTEGRATE_LOCK_WAIT_MS = 30 * 60_000;
40
44
  export const INTEGRATE_LOCK_STALE_MS = 2 * 3_600_000;
41
- function refuse(ruleId, message, fixHint, partial) {
45
+ function refuse(ruleId, message, fixHint, partial, diagData) {
42
46
  return {
43
47
  branch: partial?.branch ?? null,
44
48
  integrated: partial?.integrated ?? false,
@@ -47,7 +51,21 @@ function refuse(ruleId, message, fixHint, partial) {
47
51
  rebased: partial?.rebased ?? false,
48
52
  gate: partial?.gate ?? null,
49
53
  cleanedUp: partial?.cleanedUp ?? false,
50
- diagnostics: [{ ruleId, severity: "error", message, fixHint }],
54
+ ...(partial?.toleratedState !== undefined ? { toleratedState: partial.toleratedState } : {}),
55
+ ...(partial?.toleratedDirt !== undefined ? { toleratedDirt: partial.toleratedDirt } : {}),
56
+ ...(partial?.reattachedTo !== undefined ? { reattachedTo: partial.reattachedTo } : {}),
57
+ ...(partial?.autoCleaned !== undefined ? { autoCleaned: partial.autoCleaned } : {}),
58
+ ...(partial?.autocommitSha !== undefined ? { autocommitSha: partial.autocommitSha } : {}),
59
+ ...(partial?.restoredImporters !== undefined
60
+ ? { restoredImporters: partial.restoredImporters }
61
+ : {}),
62
+ ...(partial?.staleImporterCommitSha !== undefined
63
+ ? { staleImporterCommitSha: partial.staleImporterCommitSha }
64
+ : {}),
65
+ ...(partial?.lock !== undefined ? { lock: partial.lock } : {}),
66
+ diagnostics: [
67
+ { ruleId, severity: "error", message, fixHint, ...(diagData ? { data: diagData } : {}) },
68
+ ],
51
69
  };
52
70
  }
53
71
  async function resolveEntry(repoRoot, io, branch) {
@@ -56,9 +74,10 @@ async function resolveEntry(repoRoot, io, branch) {
56
74
  return { common, entry: registry.branches.find((b) => b.name === branch) ?? null };
57
75
  }
58
76
  /** Step 6 — teardown, worktree removal, branch delete, registry, journal. */
59
- async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowIso, journalEvent = "integrate", journalBase) {
77
+ async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowIso, journalEvent = "integrate", journalBase, journalToleratedDirt, journalAutoResolved, journalRemediation, journalGateReport) {
60
78
  const branchExistsNow = await branchExists(primary, io, entry.name);
61
79
  let teardownRan = null;
80
+ let toleratedPaths = [];
62
81
  if (worktree !== null && (await io.exists(worktree))) {
63
82
  // RFC-1509 Q-16: preserve BEFORE the teardown hook — a `git clean -fdx`
64
83
  // teardown would destroy .forge/task/ with the rest of the worktree.
@@ -101,6 +120,18 @@ async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowI
101
120
  ],
102
121
  };
103
122
  }
123
+ // RFC-1515: `statusDirty` predicts `git worktree remove` — allowlisted
124
+ // forge-local state is untracked-not-ignored, so a tolerated-only tree
125
+ // still refuses removal. Delete the tolerated paths first (declared-
126
+ // disposable state; task payloads are already preserved above); a
127
+ // fresh check showing real entries leaves remove to report them.
128
+ const residual = await statusDirty(worktree, io);
129
+ if (residual.entries.length === 0 && residual.toleratedState.length > 0) {
130
+ toleratedPaths = residual.toleratedState;
131
+ for (const rel of residual.toleratedState) {
132
+ await rm(join(worktree, rel), { force: true, recursive: true });
133
+ }
134
+ }
104
135
  const remove = await git(primary, io, ["worktree", "remove", worktree]);
105
136
  if (remove.exitCode !== 0) {
106
137
  return {
@@ -118,7 +149,15 @@ async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowI
118
149
  }
119
150
  }
120
151
  if (branchExistsNow) {
121
- const del = await git(primary, io, ["branch", "-d", entry.name]);
152
+ // RFC-1515: a vanished worktree leaves a stale `git worktree`
153
+ // registration that pins the branch — prune it before deletion. Merged
154
+ // state is already proven by isAncestor, so `-D` is the verified delete
155
+ // (`-d` re-derives containment in the current worktree context, which
156
+ // the F-0093 *.stale race corrupted).
157
+ if (worktree !== null && !(await io.exists(worktree))) {
158
+ await git(primary, io, ["worktree", "prune"]);
159
+ }
160
+ const del = await git(primary, io, ["branch", "-D", entry.name]);
122
161
  if (del.exitCode !== 0) {
123
162
  return {
124
163
  cleanedUp: false,
@@ -146,6 +185,30 @@ async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowI
146
185
  sha,
147
186
  detail: journalEvent === "backmerge" ? `back-merged into develop` : `integrated into develop`,
148
187
  base: journalBase,
188
+ ...(toleratedPaths.length > 0 ? { toleratedState: toleratedPaths } : {}),
189
+ ...(journalToleratedDirt !== undefined && journalToleratedDirt.length > 0
190
+ ? { toleratedDirt: journalToleratedDirt }
191
+ : {}),
192
+ ...(journalAutoResolved !== undefined && journalAutoResolved.length > 0
193
+ ? { autoResolved: journalAutoResolved }
194
+ : {}),
195
+ ...(journalRemediation?.reattachedTo !== undefined
196
+ ? { reattachedTo: journalRemediation.reattachedTo }
197
+ : {}),
198
+ ...(journalRemediation?.autoCleaned !== undefined && journalRemediation.autoCleaned.length > 0
199
+ ? { autoCleaned: journalRemediation.autoCleaned }
200
+ : {}),
201
+ ...(journalRemediation?.autocommitSha !== undefined
202
+ ? { autocommitSha: journalRemediation.autocommitSha }
203
+ : {}),
204
+ ...(journalRemediation?.restoredImporters !== undefined &&
205
+ journalRemediation.restoredImporters.length > 0
206
+ ? { restoredImporters: journalRemediation.restoredImporters }
207
+ : {}),
208
+ ...(journalRemediation?.staleImporterCommitSha !== undefined
209
+ ? { staleImporterCommitSha: journalRemediation.staleImporterCommitSha }
210
+ : {}),
211
+ ...(journalGateReport !== undefined ? { gateReport: journalGateReport } : {}),
149
212
  });
150
213
  }
151
214
  catch (error) {
@@ -170,8 +233,37 @@ async function cleanupIntegrated(primary, worktree, entry, common, sha, io, nowI
170
233
  /**
171
234
  * Core of flow.integrate — callable with an injected clock. Refusal paths
172
235
  * return diagnostics; `integrated`/`cleanedUp` report what really happened.
236
+ * The exported wrapper journals every refusal (RFC-1537); this attempt body
237
+ * stays pure.
173
238
  */
174
239
  export async function integrateFlowBranch(repoRoot, io, opts) {
240
+ const data = await integrateFlowBranchAttempt(repoRoot, io, opts);
241
+ const error = data.diagnostics.find((d) => d.severity === "error");
242
+ if (error !== undefined && data.branch !== null) {
243
+ // Best-effort bookkeeping — a journal write failure never adds a refusal.
244
+ try {
245
+ const common = await commonGitDir(io, repoRoot);
246
+ const sha = (await git(repoRoot, io, ["rev-parse", "--verify", "-q", `refs/heads/${data.branch}`])).stdout.trim() || "unknown";
247
+ await appendJournal(io, common, {
248
+ event: "integrate-refusal",
249
+ branch: data.branch,
250
+ sha,
251
+ detail: error.message,
252
+ rule: error.ruleId,
253
+ ...(data.toleratedState !== undefined ? { toleratedState: data.toleratedState } : {}),
254
+ ...(data.toleratedDirt !== undefined ? { toleratedDirt: data.toleratedDirt } : {}),
255
+ // RFC-1532: the refused attempt's report links here too — the journal
256
+ // answers "which gate report belongs to this refusal" in one field.
257
+ ...(data.gate?.reportPath ? { gateReport: data.gate.reportPath } : {}),
258
+ });
259
+ }
260
+ catch {
261
+ /* journal is audit, not gate */
262
+ }
263
+ }
264
+ return data;
265
+ }
266
+ async function integrateFlowBranchAttempt(repoRoot, io, opts) {
175
267
  const now = opts.now ?? new Date();
176
268
  const branch = opts.branch ?? (await currentBranch(repoRoot, io));
177
269
  if (branch === null) {
@@ -189,6 +281,34 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
189
281
  }
190
282
  const { common, entry } = resolved;
191
283
  if (entry === null) {
284
+ // RFC-1538: a second integrate after a successful one finds no entry —
285
+ // report "already-integrated" (exit 0) instead of FLOW-INT-06 when the
286
+ // journal proves the branch landed or develop already contains its tip.
287
+ let contained = false;
288
+ try {
289
+ const journal = await readJournal(io, common);
290
+ contained = journal.some((e) => e.branch === branch && (e.event === "integrate" || e.event === "backmerge"));
291
+ }
292
+ catch {
293
+ contained = false;
294
+ }
295
+ if (!contained && (await branchExists(repoRoot, io, branch))) {
296
+ const tip = (await git(repoRoot, io, ["rev-parse", branch])).stdout.trim();
297
+ contained = await isAncestor(repoRoot, io, tip, "develop");
298
+ }
299
+ if (contained) {
300
+ return {
301
+ branch,
302
+ integrated: true,
303
+ developBefore: null,
304
+ developAfter: null,
305
+ rebased: false,
306
+ gate: null,
307
+ cleanedUp: true,
308
+ disposition: "already-integrated",
309
+ diagnostics: [],
310
+ };
311
+ }
192
312
  return refuse("FLOW-INT-06", `${branch} is not a registered session/task branch`, "register it with flow.open, or check flow.status for live entries", { branch });
193
313
  }
194
314
  if (entry.kind === "fix") {
@@ -203,6 +323,13 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
203
323
  const live = await worktreeList(primary, io);
204
324
  const wtPath = live.find((w) => w.branch === branch)?.path ?? entry.worktree;
205
325
  if (wtPath === null || !(await io.exists(wtPath))) {
326
+ // RFC-1531: report the reattach target --reattach would use — nothing
327
+ // is created under --dry-run (AC-7).
328
+ const wouldReattach = opts.reattach === true
329
+ ? entry.worktree !== null && (await io.exists(dirname(entry.worktree)))
330
+ ? entry.worktree
331
+ : flowWorktreePath(primary, branch)
332
+ : undefined;
206
333
  return {
207
334
  branch,
208
335
  integrated: false,
@@ -211,17 +338,26 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
211
338
  rebased: false,
212
339
  gate: null,
213
340
  cleanedUp: false,
341
+ ...(wouldReattach !== undefined ? { wouldReattach } : {}),
214
342
  diagnostics: [
215
343
  {
216
344
  ruleId: "FLOW-07",
217
345
  severity: "error",
218
- message: `registry entry for ${branch} names worktree ${wtPath}, which does not exist`,
219
- fixHint: "inspect with flow.status — a stale entry may need flow.discard",
346
+ message: `registry entry for ${branch} names worktree ${wtPath}, which does not exist (vanished)` +
347
+ (wouldReattach !== undefined
348
+ ? ` — --reattach would recreate it at ${wouldReattach}`
349
+ : ""),
350
+ fixHint: "re-run with --reattach to recreate the worktree at its recorded path, or run flow.discard to drop the registry entry and force-delete the branch",
351
+ data: { vanished: true, ...(wouldReattach !== undefined ? { wouldReattach } : {}) },
220
352
  },
221
353
  ],
222
354
  };
223
355
  }
224
356
  const dirty = await statusDirty(wtPath, io);
357
+ // RFC-1531: the dry-run diagnostic carries the classified dirty set so
358
+ // the operator reads the remedy (--auto-clean scope) without mutating.
359
+ const classified = classifyDirtyTree(dirty.entries);
360
+ const disposable = classified.filter((c) => c.class === "disposable").map((c) => c.path);
225
361
  return {
226
362
  branch,
227
363
  integrated: false,
@@ -230,13 +366,24 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
230
366
  rebased: false,
231
367
  gate: null,
232
368
  cleanedUp: false,
233
- diagnostics: dirty.length > 0
369
+ ...(dirty.toleratedState.length > 0 ? { toleratedState: dirty.toleratedState } : {}),
370
+ diagnostics: dirty.entries.length > 0
234
371
  ? [
235
372
  {
236
373
  ruleId: "FLOW-INT-01",
237
374
  severity: "error",
238
- message: `worktree ${wtPath} has ${dirty.length} uncommitted change(s)`,
239
- fixHint: "commit or revert the uncommitted changes inside the session worktree",
375
+ message: `worktree ${wtPath} has ${dirty.entries.length} uncommitted change(s)` +
376
+ (dirty.toleratedState.length > 0
377
+ ? ` — tolerated forge-local state: ${dirty.toleratedState.join(", ")}`
378
+ : ""),
379
+ fixHint: "commit or revert the uncommitted changes inside the session worktree, or remediate with --auto-clean / --autocommit, then re-run",
380
+ data: {
381
+ entries: classified,
382
+ classes: summarizeClasses(classified),
383
+ toleratedState: dirty.toleratedState,
384
+ ...(opts.autoClean === true ? { wouldAutoClean: disposable } : {}),
385
+ ...(opts.autocommit === true ? { wouldAutocommit: true } : {}),
386
+ },
240
387
  },
241
388
  ]
242
389
  : [],
@@ -268,15 +415,267 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
268
415
  diagnostics: [],
269
416
  };
270
417
  }
418
+ // RFC-1538: post-lock revalidation — the registry observed pre-lock can
419
+ // have changed while this run waited on the integration lock (F-0138:
420
+ // a racing integrate's teardown deleted entry+worktree under us).
421
+ const liveRegistry = await readRegistry(io, common);
422
+ const liveEntry = liveRegistry.branches.find((b) => b.name === branch);
423
+ if (liveEntry === undefined) {
424
+ // Registration vanished mid-wait. Contained work already landed → the
425
+ // winner completed; report idempotently. Otherwise mid-teardown.
426
+ const liveBranch = await branchExists(primary, io, branch);
427
+ const liveContained = liveBranch &&
428
+ (await isAncestor(primary, io, (await git(primary, io, ["rev-parse", branch])).stdout.trim(), "develop"));
429
+ if (liveContained || !liveBranch) {
430
+ return {
431
+ branch,
432
+ integrated: true,
433
+ developBefore,
434
+ developAfter: developBefore,
435
+ rebased: false,
436
+ gate: null,
437
+ cleanedUp: true,
438
+ disposition: "already-integrated",
439
+ diagnostics: [],
440
+ };
441
+ }
442
+ return refuse("FLOW-INT-15", `registry entry for ${branch} vanished while waiting on the integration lock, but the branch is not contained in develop (mid-teardown state)`, "run flow.status for the live view; recover the orphaned entry with flow.discard", { branch, developBefore });
443
+ }
271
444
  // (2) the branch worktree must exist and be clean.
272
445
  const worktrees = await worktreeList(primary, io);
273
- const branchWorktree = worktrees.find((w) => w.branch === branch)?.path ?? entry.worktree;
446
+ // `live` is the worktree git currently associates with the branch — a
447
+ // STALE registration keeps naming the dead recorded path until pruned,
448
+ // so `live !== null` does not prove the directory exists.
449
+ const live = worktrees.find((w) => w.branch === branch)?.path ?? null;
450
+ let branchWorktree = live ?? liveEntry.worktree;
451
+ let reattachedTo;
452
+ // Occupied: the recorded path exists on disk but git associates no live
453
+ // worktree with the branch — a foreign tree squats there.
454
+ const occupied = live === null && liveEntry.worktree !== null && (await io.exists(liveEntry.worktree));
455
+ const vanished = !occupied && (branchWorktree === null || !(await io.exists(branchWorktree)));
456
+ if (opts.reattach === true && occupied) {
457
+ return refuse("FLOW-INT-17", `registry worktree path ${liveEntry.worktree} exists on disk but is not a worktree of ${branch} (occupied)`, "remove or rename the occupant, then re-run flow.integrate --reattach", { branch, developBefore }, { occupied: true });
458
+ }
459
+ if (opts.reattach === true && vanished) {
460
+ // RFC-1531 --reattach: recreate the vanished worktree inside the lock,
461
+ // before the dirty/preflight checks. The recorded registry path is
462
+ // preferred when its parent exists (operators learned that location);
463
+ // the canonical <repo>@<branch> sibling is the fallback.
464
+ const recorded = liveEntry.worktree;
465
+ const target = recorded !== null && (await io.exists(dirname(recorded)))
466
+ ? recorded
467
+ : flowWorktreePath(primary, branch);
468
+ // F-0093: a vanished worktree can leave a stale `git worktree`
469
+ // registration that pins the branch — prune before adding (mirrors
470
+ // cleanupIntegrated's ordering).
471
+ await git(primary, io, ["worktree", "prune"]);
472
+ const add = await worktreeAdd(primary, io, target, branch);
473
+ if (add.exitCode !== 0) {
474
+ return refuse("FLOW-INT-17", `git worktree add ${target} ${branch} failed: ${add.stderr.trim()}`, "resolve the git error (stale registration? occupied path?) and re-run flow.integrate --reattach", { branch, developBefore });
475
+ }
476
+ reattachedTo = target;
477
+ branchWorktree = target;
478
+ opts.onProgress?.(`reattached ${branch} at ${target}`);
479
+ }
274
480
  if (branchWorktree === null || !(await io.exists(branchWorktree))) {
275
- return refuse("FLOW-07", `registry entry for ${branch} names worktree ${branchWorktree}, which does not exist`, "inspect with flow.status — a stale entry may need flow.discard", { branch, developBefore });
481
+ return refuse("FLOW-INT-15", `registry entry for ${branch} names worktree ${branchWorktree}, which does not exist (vanished mid-teardown state — the branch is not contained in develop)`, "re-run with --reattach to recreate the worktree, or run flow.status for the live view and flow.discard to drop the entry", { branch, developBefore }, { vanished: true });
276
482
  }
277
- const dirty = await statusDirty(branchWorktree, io);
278
- if (dirty.length > 0) {
279
- return refuse("FLOW-INT-01", `worktree ${branchWorktree} has ${dirty.length} uncommitted change(s): ${dirty.slice(0, 10).join(", ")}`, "commit or revert the uncommitted changes inside the session worktree, then re-run", { branch, developBefore });
483
+ let dirty = await statusDirty(branchWorktree, io);
484
+ if (dirty.toleratedState.length > 0) {
485
+ opts.onProgress?.(`tolerated forge-local state: ${dirty.toleratedState.join(", ")}`);
486
+ }
487
+ // RFC-1544 AC-2: referent-aware importer-prune resolution — before any
488
+ // remediation, so --autocommit can never sweep a foreign mission's live
489
+ // importer into a commit. `foreign-open-mission` restores the committed
490
+ // importer subtree (never a whole-file checkout — sibling lockfile diffs
491
+ // keep their own classification); `importer-prune-stale` accepts the
492
+ // prune as cleanup and commits it so dead residue stops churning.
493
+ let restoredImporters;
494
+ let staleImporterCommitSha;
495
+ if (dirty.entries.some((line) => line.slice(3).trim() === "pnpm-lock.yaml")) {
496
+ const { classifyDirt, resolveImporterReferents } = await import("../preflight.js");
497
+ const { extractImporterBlock, insertImporterBlock } = await import("../../mission/importer-block.js");
498
+ const pruneVerdict = await classifyDirt(branchWorktree, io, dirty.entries, { runGit: git }, [], (ids) => resolveImporterReferents(branchWorktree, io, ids));
499
+ const lockTolerated = pruneVerdict.tolerated.find((t) => t.path === "pnpm-lock.yaml");
500
+ if (lockTolerated?.class === "foreign-open-mission" &&
501
+ lockTolerated.missionIds !== undefined) {
502
+ const head = await git(branchWorktree, io, ["show", "HEAD:pnpm-lock.yaml"]);
503
+ if (head.exitCode === 0) {
504
+ let next = await io.readFile(join(branchWorktree, "pnpm-lock.yaml"));
505
+ const restored = [];
506
+ for (const id of lockTolerated.missionIds) {
507
+ const key = `missions/${id}/workpiece`;
508
+ const block = extractImporterBlock(head.stdout, key);
509
+ if (block === null)
510
+ continue;
511
+ const spliced = insertImporterBlock(next, key, block);
512
+ if (spliced === null)
513
+ continue;
514
+ next = spliced;
515
+ restored.push(id);
516
+ }
517
+ if (restored.length > 0) {
518
+ await io.writeFile(join(branchWorktree, "pnpm-lock.yaml"), next);
519
+ restoredImporters = restored;
520
+ opts.onProgress?.(`restored foreign mission importer(s): ${restored.map((id) => `missions/${id}/workpiece`).join(", ")}`);
521
+ dirty = await statusDirty(branchWorktree, io);
522
+ }
523
+ }
524
+ }
525
+ else if (lockTolerated?.class === "importer-prune-stale") {
526
+ const add = await git(branchWorktree, io, ["add", "pnpm-lock.yaml"]);
527
+ const commit = add.exitCode === 0
528
+ ? await gitCommit(branchWorktree, io, ["commit", "-m", "chore(flow): prune stale mission importer"], "integrate")
529
+ : add;
530
+ if (commit.exitCode === 0) {
531
+ staleImporterCommitSha = (await git(branchWorktree, io, ["rev-parse", "HEAD"])).stdout.trim();
532
+ opts.onProgress?.(`committed stale importer prune ${staleImporterCommitSha.slice(0, 9)} (${(lockTolerated.missionIds ?? []).map((id) => `missions/${id}/workpiece`).join(", ")})`);
533
+ dirty = await statusDirty(branchWorktree, io);
534
+ }
535
+ }
536
+ }
537
+ // RFC-1531 remediation flags — before the dirty checks: --auto-clean
538
+ // deletes ONLY disposable-class paths (closed allowlist — a work-class
539
+ // member in the set deletes nothing and still refuses FLOW-INT-01, and
540
+ // an importer-prune lockfile refuses too: restoring/committing it is a
541
+ // content decision, AC-4), then --autocommit sweeps the remainder into
542
+ // the fixed-subject commit. Remediation failures refuse FLOW-INT-17.
543
+ let autoCleaned;
544
+ let autocommitSha;
545
+ if (dirty.entries.length > 0 && (opts.autoClean === true || opts.autocommit === true)) {
546
+ const { classifyDirtyEntries } = await import("../preflight.js");
547
+ const classified = await classifyDirtyEntries(branchWorktree, io, dirty.entries);
548
+ // Pass = AUTO_CLEAN_PASS_CLASSES (tolerated family incl. the RFC-1537
549
+ // regenerable classes) or disposable. Blocker = work or importer-prune
550
+ // — AC-4 names importer-prune a refusal class: restoring/committing a
551
+ // lockfile is a content decision, never a cleanup step.
552
+ const blocker = classified.filter((c) => c.class !== "disposable" && !AUTO_CLEAN_PASS_CLASSES.includes(c.class));
553
+ if (opts.autoClean === true) {
554
+ // A blocker refuses only when nothing downstream will commit it:
555
+ // with --autocommit also set, work members sweep into the commit —
556
+ // the RFC's composed order is clean → commit → integrate.
557
+ if (blocker.length > 0 && opts.autocommit !== true) {
558
+ return refuse("FLOW-INT-01", `worktree ${branchWorktree} has ${blocker.length} uncommitted change(s) outside the disposable set: ${blocker
559
+ .slice(0, 10)
560
+ .map((b) => `${b.path} (${b.class})`)
561
+ .join(", ")} — --auto-clean deletes nothing on a mixed set`, "commit or revert the work-class entries inside the session worktree, or re-run with --autocommit to sweep them into a commit", { branch, developBefore, toleratedState: dirty.toleratedState }, {
562
+ entries: classified,
563
+ classes: summarizeClasses(classified),
564
+ toleratedState: dirty.toleratedState,
565
+ });
566
+ }
567
+ const toDelete = classified.filter((c) => c.class === "disposable");
568
+ autoCleaned = [];
569
+ for (const entry of toDelete) {
570
+ try {
571
+ await rm(join(branchWorktree, entry.path), { force: true, recursive: true });
572
+ autoCleaned.push(entry.path);
573
+ opts.onProgress?.(`auto-clean: deleted ${entry.path}`);
574
+ }
575
+ catch (error) {
576
+ return refuse("FLOW-INT-17", `--auto-clean could not delete ${entry.path}: ${error instanceof Error ? error.message : String(error)}`, "delete the path manually inside the session worktree, then re-run flow.integrate", { branch, developBefore, autoCleaned });
577
+ }
578
+ }
579
+ dirty = await statusDirty(branchWorktree, io);
580
+ }
581
+ if (opts.autocommit === true && dirty.entries.length > 0) {
582
+ // Stage exactly the remaining entry paths — never `git add -A`:
583
+ // tolerated FORGE_STATE paths are journaled, never committed.
584
+ const paths = dirty.entries.map((line) => line.slice(3).trim());
585
+ const add = await git(branchWorktree, io, ["add", "-A", "--", ...paths]);
586
+ if (add.exitCode !== 0) {
587
+ return refuse("FLOW-INT-17", `--autocommit staging failed: ${add.stderr.trim()}`, "inspect the session worktree state, then re-run flow.integrate", { branch, developBefore, autoCleaned });
588
+ }
589
+ const commit = await gitCommit(branchWorktree, io, ["commit", "-m", SWEEP_COMMIT_SUBJECT], "integrate");
590
+ if (commit.exitCode !== 0) {
591
+ return refuse("FLOW-INT-17", `--autocommit commit failed: ${commit.stderr.trim()}`, "the staging stays for the operator to inspect — resolve and commit or reset inside the session worktree, then re-run", { branch, developBefore, autoCleaned });
592
+ }
593
+ autocommitSha = (await git(branchWorktree, io, ["rev-parse", "HEAD"])).stdout.trim();
594
+ opts.onProgress?.(`autocommit: swept ${paths.length} path(s) into ${autocommitSha.slice(0, 9)} (${SWEEP_COMMIT_SUBJECT})`);
595
+ dirty = await statusDirty(branchWorktree, io);
596
+ }
597
+ }
598
+ // RFC-1537: declared regenerable dirt tolerated on this attempt —
599
+ // surfaced in data + journaled on integrate/refusal, never committed.
600
+ let toleratedDirt = [];
601
+ // RFC-1528: bounded preflight — dirty-tree checks with lockfile
602
+ // classification, then the four validators that used to fire only inside
603
+ // the expensive integration gate. Non-port path only (port entries keep
604
+ // their own FLOW-INT-09/10 semantics); first failure refuses before the
605
+ // rebase and before the gate spawns.
606
+ if (entry.port === null) {
607
+ const { runIntegratePreflight } = await import("../preflight.js");
608
+ const preflight = await runIntegratePreflight({
609
+ primary,
610
+ branchWorktree,
611
+ branch,
612
+ io,
613
+ worktreeDirty: dirty,
614
+ invoke: opts.invoke,
615
+ commandRegistry: opts.commandRegistry,
616
+ waitForeignSeconds: opts.waitForeignSeconds,
617
+ onProgress: opts.onProgress,
618
+ });
619
+ toleratedDirt = preflight.tolerated;
620
+ for (const skipped of preflight.skipped) {
621
+ opts.onProgress?.(skipped.reason === "no-invoke"
622
+ ? `preflight: ${skipped.check} skipped — no invoke port (standalone CLI)`
623
+ : `preflight: ${skipped.check} skipped — command not in the live inventory`);
624
+ }
625
+ for (const t of preflight.tolerated) {
626
+ opts.onProgress?.(`tolerated regenerable state: ${t.path} (${t.class})`);
627
+ }
628
+ if (!preflight.ok && preflight.refuse) {
629
+ return refuse(preflight.refuse.ruleId, preflight.refuse.message, preflight.refuse.fixHint, {
630
+ branch,
631
+ developBefore,
632
+ toleratedState: dirty.toleratedState,
633
+ toleratedDirt: preflight.tolerated,
634
+ reattachedTo,
635
+ autoCleaned,
636
+ autocommitSha,
637
+ restoredImporters,
638
+ staleImporterCommitSha,
639
+ }, preflight.refuse.data);
640
+ }
641
+ }
642
+ else if (dirty.entries.length > 0) {
643
+ // Port path keeps its own refusal surface but the same regenerable
644
+ // classes still tolerate (RFC-1537) — an importer-materializing port
645
+ // worktree is no harder to integrate than a session one.
646
+ const { classifyDirt } = await import("../preflight.js");
647
+ const verdict = await classifyDirt(branchWorktree, io, dirty.entries);
648
+ toleratedDirt = verdict.tolerated;
649
+ for (const t of verdict.tolerated) {
650
+ opts.onProgress?.(`tolerated regenerable state: ${t.path} (${t.class})`);
651
+ }
652
+ if (verdict.unattributed.length > 0) {
653
+ const entries = verdict.unattributed.map((line) => {
654
+ const path = line.slice(3).trim();
655
+ return { path, class: classifyDirtyPath(path) };
656
+ });
657
+ return refuse("FLOW-INT-01", `worktree ${branchWorktree} has ${verdict.unattributed.length} uncommitted change(s): ${verdict.unattributed.slice(0, 10).join(", ")}` +
658
+ (verdict.tolerated.length > 0
659
+ ? ` — tolerated regenerable state: ${verdict.tolerated.map((t) => `${t.path} (${t.class})`).join(", ")}`
660
+ : "") +
661
+ (dirty.toleratedState.length > 0
662
+ ? ` — tolerated forge-local state: ${dirty.toleratedState.join(", ")}`
663
+ : ""), "commit or revert the uncommitted changes inside the session worktree, or remediate with --auto-clean / --autocommit, then re-run", {
664
+ branch,
665
+ developBefore,
666
+ toleratedState: dirty.toleratedState,
667
+ toleratedDirt: verdict.tolerated,
668
+ reattachedTo,
669
+ autoCleaned,
670
+ autocommitSha,
671
+ restoredImporters,
672
+ staleImporterCommitSha,
673
+ }, {
674
+ entries,
675
+ classes: summarizeClasses(entries),
676
+ toleratedState: dirty.toleratedState,
677
+ });
678
+ }
280
679
  }
281
680
  // Port entries (RFC-1490): develop must not have moved since the port
282
681
  // opened, and the tip must be the fixed-message merge whose second
@@ -294,27 +693,96 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
294
693
  }
295
694
  }
296
695
  // (3) rebase onto the current develop — never for port entries.
696
+ // RFC-1538: a conflict confined to generated artifacts resolves via its
697
+ // owner command (checkout --theirs + regenerate + continue) — the
698
+ // generated-file doctrine applied inside the rebase path; anything else
699
+ // still refuses FLOW-INT-02 for a human.
297
700
  const rebaseBase = (await git(primary, io, ["rev-parse", "develop"])).stdout.trim();
298
- const rebase = entry.port !== null
299
- ? { exitCode: 0, stdout: "", stderr: "" }
300
- : await git(branchWorktree, io, ["rebase", "develop"]);
301
- if (rebase.exitCode !== 0) {
302
- const conflicts = await git(branchWorktree, io, [
303
- "diff",
304
- "--name-only",
305
- "--diff-filter=U",
306
- ]);
307
- const paths = conflicts.stdout
308
- .split("\n")
309
- .map((l) => l.trim())
310
- .filter((l) => l.length > 0);
311
- await git(branchWorktree, io, ["rebase", "--abort"]);
312
- return refuse("FLOW-INT-02", `rebase onto develop conflicts in: ${paths.join(", ") || "(unknown paths)"}`, "resolve the rebase inside the session worktree, commit, and re-run flow.integrate", { branch, developBefore });
313
- }
314
- // (4) integration gate — the primary's copy gates the branch.
701
+ const autoResolved = [];
702
+ if (entry.port === null) {
703
+ let rebase = await git(branchWorktree, io, ["rebase", "develop"]);
704
+ let guard = 0;
705
+ while (rebase.exitCode !== 0 && guard++ < 8) {
706
+ const conflicts = await git(branchWorktree, io, ["diff", "--name-only", "--diff-filter=U"]);
707
+ const paths = conflicts.stdout
708
+ .split("\n")
709
+ .map((l) => l.trim())
710
+ .filter((l) => l.length > 0);
711
+ const { classifyRebaseConflicts } = await import("../preflight.js");
712
+ const verdict = await classifyRebaseConflicts(branchWorktree, io, paths);
713
+ // RFC-1545 AC-2: without a kernel invoke port the standalone adapter
714
+ // spawns the allowlisted owner commands in the worktree — the
715
+ // resolution contract is identical either way.
716
+ const invoker = opts.invoke ??
717
+ standaloneInvoke(io, branchWorktree, verdict.owners.map((o) => o.command));
718
+ const resolvable = paths.length > 0 &&
719
+ verdict.nonGenerated.length === 0 &&
720
+ verdict.owners.length === paths.length;
721
+ if (!resolvable) {
722
+ await git(branchWorktree, io, ["rebase", "--abort"]);
723
+ return refuse("FLOW-INT-02", `rebase onto develop conflicts in: ${paths.join(", ") || "(unknown paths)"}` +
724
+ (verdict.owners.length > 0 ? " — mixed generated/non-generated conflict set" : ""), "resolve the rebase inside the session worktree, commit, and re-run flow.integrate", { branch, developBefore });
725
+ }
726
+ let failed = false;
727
+ for (const o of verdict.owners) {
728
+ // `git checkout --theirs` is blocked by the agent-session git
729
+ // wrapper — materialize the stage-3 (theirs) blob through the
730
+ // read-only `git show` + io write instead (RFC-1545).
731
+ const theirs = await git(branchWorktree, io, ["show", `:3:${o.path}`]);
732
+ if (theirs.exitCode === 0) {
733
+ await io.writeFile(join(branchWorktree, o.path), theirs.stdout);
734
+ }
735
+ const regen = await invoker(o.command, o.flags);
736
+ if (regen.ok === false || (regen.exitCode !== undefined && regen.exitCode !== 0)) {
737
+ await git(branchWorktree, io, ["rebase", "--abort"]);
738
+ failed = true;
739
+ return refuse("FLOW-INT-02", `auto-resolve of generated conflict ${o.path} failed: ${o.command} — ${regen.summary ?? `exit ${regen.exitCode}`}`, "resolve the rebase inside the session worktree manually, commit, and re-run flow.integrate", { branch, developBefore });
740
+ }
741
+ // RFC-1545 AC-4: a successful generator that left conflict markers
742
+ // behind must not be committed — escalate like a failure.
743
+ const regenerated = await io.readFile(join(branchWorktree, o.path));
744
+ if (/^(<{7}|={7}|>{7})/m.test(regenerated)) {
745
+ await git(branchWorktree, io, ["rebase", "--abort"]);
746
+ failed = true;
747
+ return refuse("FLOW-INT-02", `auto-resolve of generated conflict ${o.path} failed: ${o.command} left conflict markers in the regenerated output`, "resolve the rebase inside the session worktree manually, commit, and re-run flow.integrate", { branch, developBefore });
748
+ }
749
+ await git(branchWorktree, io, ["add", "--", o.path]);
750
+ opts.onProgress?.(`conflict: ${o.path} — regenerated via ${o.command}`);
751
+ autoResolved.push(o.path);
752
+ }
753
+ if (failed)
754
+ break;
755
+ rebase = await git(branchWorktree, io, ["-c", "core.editor=true", "rebase", "--continue"]);
756
+ }
757
+ if (rebase.exitCode !== 0) {
758
+ const conflicts = await git(branchWorktree, io, ["diff", "--name-only", "--diff-filter=U"]);
759
+ const paths = conflicts.stdout
760
+ .split("\n")
761
+ .map((l) => l.trim())
762
+ .filter((l) => l.length > 0);
763
+ await git(branchWorktree, io, ["rebase", "--abort"]);
764
+ return refuse("FLOW-INT-02", `rebase onto develop conflicts in: ${paths.join(", ") || "(unknown paths)"}`, "resolve the rebase inside the session worktree, commit, and re-run flow.integrate", { branch, developBefore });
765
+ }
766
+ }
767
+ // (4) integration gate — the primary's script, executed by the session
768
+ // engine's build when the worktree carries one (RFC-1538): the code
769
+ // under review judges itself; the checks it faces stay primary-owned.
770
+ const { resolveGateEngine } = await import("../gate-engine.js");
771
+ const engine = await resolveGateEngine(branchWorktree, primary, io);
772
+ const warnings = [];
773
+ if (engine.source === "primary" &&
774
+ (await io.exists(join(primary, "hooks", "integration-gate.sh")))) {
775
+ warnings.push({
776
+ ruleId: "FLOW-INT-16",
777
+ severity: "warning",
778
+ message: `no session engine build under ${branchWorktree} — the integration gate runs the primary engine`,
779
+ fixHint: "run pnpm install inside the session worktree to have its own build judged; the F-0137 self-referential escape is a backport worktree",
780
+ });
781
+ }
782
+ opts.onProgress?.(`gate engine: ${engine.source} (${engine.root})`);
315
783
  const gate = await runFlowScript(io, "integration-gate", {
316
784
  primary,
317
- cwd: branchWorktree,
785
+ cwd: engine.root,
318
786
  env: {
319
787
  FLOW_PRIMARY: primary,
320
788
  FLOW_BRANCH: branch,
@@ -323,15 +791,40 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
323
791
  },
324
792
  });
325
793
  if (gate.status === "failed") {
326
- return refuse("FLOW-INT-05", `hooks/integration-gate.sh refused the integration (exit ${gate.exitCode}): ${gate.stderrTail}`, `fix what ${primary}/hooks/integration-gate.sh demands, then re-run`, { branch, developBefore, rebased: entry.port === null, gate });
794
+ return refuse("FLOW-INT-05", `hooks/integration-gate.sh refused the integration (exit ${gate.exitCode}): ${gate.stderrTail}`, `fix what ${primary}/hooks/integration-gate.sh demands, then re-run`, { branch, developBefore, rebased: entry.port === null, gate },
795
+ // RFC-1532 AC-4: name the persisted report so the fixing agent reads
796
+ // full check rows instead of re-running the gate.
797
+ gate.reportPath ? { reportPath: gate.reportPath } : undefined);
327
798
  }
328
- // (5) primary must sit clean on develop, then fast-forward.
799
+ // (5) primary must sit clean on develop, then fast-forward. Mid-gate
800
+ // importer churn from another session's open mission tolerates here too
801
+ // (RFC-1537) — the same classifier the preflight ran.
329
802
  const primaryBranch = await currentBranch(primary, io);
330
803
  const primaryDirty = await statusTracked(primary, io);
331
- if (primaryBranch !== "develop" || primaryDirty.length > 0) {
332
- return refuse("FLOW-INT-03", primaryBranch !== "develop"
333
- ? `primary worktree is on ${primaryBranch ?? "detached HEAD"}, not develop`
334
- : `primary worktree has ${primaryDirty.length} uncommitted change(s)`, "switch the primary to develop and commit or revert its tracked changes, then re-run — the branch stays rebased", { branch, developBefore, rebased: entry.port === null, gate });
804
+ if (primaryBranch !== "develop" || primaryDirty.entries.length > 0) {
805
+ let unattributed = primaryDirty.entries;
806
+ if (primaryDirty.entries.length > 0) {
807
+ const { classifyDirt } = await import("../preflight.js");
808
+ const verdict = await classifyDirt(primary, io, primaryDirty.entries);
809
+ unattributed = verdict.unattributed;
810
+ if (verdict.tolerated.length > 0) {
811
+ toleratedDirt = [...toleratedDirt, ...verdict.tolerated];
812
+ for (const t of verdict.tolerated) {
813
+ opts.onProgress?.(`tolerated regenerable state on primary: ${t.path} (${t.class})`);
814
+ }
815
+ }
816
+ }
817
+ if (primaryBranch !== "develop" || unattributed.length > 0) {
818
+ return refuse("FLOW-INT-03", primaryBranch !== "develop"
819
+ ? `primary worktree is on ${primaryBranch ?? "detached HEAD"}, not develop`
820
+ : `primary worktree has ${unattributed.length} uncommitted change(s)`, "switch the primary to develop and commit or revert its tracked changes, then re-run — the branch stays rebased", {
821
+ branch,
822
+ developBefore,
823
+ rebased: entry.port === null,
824
+ gate,
825
+ ...(toleratedDirt.length > 0 ? { toleratedDirt } : {}),
826
+ });
827
+ }
335
828
  }
336
829
  const developNow = (await git(primary, io, ["rev-parse", "develop"])).stdout.trim();
337
830
  if (!(await isAncestor(primary, io, developNow, branch))) {
@@ -350,38 +843,70 @@ export async function integrateFlowBranch(repoRoot, io, opts) {
350
843
  rebased: entry.port === null,
351
844
  gate,
352
845
  cleanedUp: false,
353
- diagnostics: [],
846
+ ...(dirty.toleratedState.length > 0 ? { toleratedState: dirty.toleratedState } : {}),
847
+ ...(toleratedDirt.length > 0 ? { toleratedDirt } : {}),
848
+ ...(autoResolved.length > 0 ? { autoResolved } : {}),
849
+ ...(reattachedTo !== undefined ? { reattachedTo } : {}),
850
+ ...(autoCleaned !== undefined && autoCleaned.length > 0 ? { autoCleaned } : {}),
851
+ ...(autocommitSha !== undefined ? { autocommitSha } : {}),
852
+ ...(restoredImporters !== undefined && restoredImporters.length > 0
853
+ ? { restoredImporters }
854
+ : {}),
855
+ ...(staleImporterCommitSha !== undefined ? { staleImporterCommitSha } : {}),
856
+ gateEngine: engine.source,
857
+ diagnostics: warnings,
354
858
  };
355
859
  };
356
860
  let inner;
357
861
  try {
358
- inner = opts.lockHeld === true
359
- ? await integrateInner()
360
- : await withFlowLock(lockPath, {
361
- waitMs: INTEGRATE_LOCK_WAIT_MS,
362
- staleMs: INTEGRATE_LOCK_STALE_MS,
363
- progressEveryMs: 60_000,
364
- onProgress: opts.onProgress,
365
- onStale: (l) => opts.onProgress?.(l.reason === "dead-holder"
366
- ? `broke dead-holder integration lock (pid ${l.pid ?? "?"} on ${l.host ?? "?"}, held since ${l.at})`
367
- : `broke stale integration lock (held since ${l.at}, ${Math.round(l.ageMs / 60000)} min old)`),
368
- }, integrateInner, io);
862
+ inner =
863
+ opts.lockHeld === true
864
+ ? await integrateInner()
865
+ : await withFlowLock(lockPath, {
866
+ waitMs: opts.lockWaitMs ?? INTEGRATE_LOCK_WAIT_MS,
867
+ staleMs: INTEGRATE_LOCK_STALE_MS,
868
+ progressEveryMs: 60_000,
869
+ onProgress: opts.onProgress,
870
+ onStale: (l) => opts.onProgress?.(l.reason === "dead-holder"
871
+ ? `broke dead-holder integration lock (pid ${l.pid ?? "?"} on ${l.host ?? "?"}, held since ${l.at})`
872
+ : `broke stale integration lock (held since ${l.at}, ${Math.round(l.ageMs / 60000)} min old)`),
873
+ }, integrateInner, io);
369
874
  }
370
875
  catch (error) {
371
876
  if (error instanceof LockTimeoutError) {
372
- return refuse("FLOW-INT-08", `integration lock ${error.lockPath} is held by another integration`, "wait for the running integration to finish, or break the stale lock after 2 h automatically", { branch });
877
+ // RFC-1515 AC-4: a refused attempt reports the holder on the first
878
+ // try — the lock payload names pid/host/at/heldMs plus a disposition
879
+ // computed by the same dead-holder classifier withFlowLock uses.
880
+ const holder = error.holder;
881
+ const lock = holder === undefined ? undefined : describeHeldLock(holder, INTEGRATE_LOCK_STALE_MS);
882
+ return refuse("FLOW-INT-08", lock === undefined
883
+ ? `integration lock ${error.lockPath} is held by another integration (holder unknown)`
884
+ : `integration lock ${error.lockPath} is held by pid ${lock.pid} on ${lock.host ?? "unknown host"} since ${lock.at} (${lock.disposition})`, "run forge werkstatt.lock.status for the live view, or forge werkstatt.lock.recover to break a dead holder's lock", { branch, lock });
373
885
  }
374
886
  if (error instanceof FlowRegistrySchemaError) {
375
- return refuse("FLOW-07", error.message, `repair or remove ${registryPath(common)}`, { branch });
887
+ return refuse("FLOW-07", error.message, `repair or remove ${registryPath(common)}`, {
888
+ branch,
889
+ });
376
890
  }
377
891
  throw error;
378
892
  }
379
- if (!inner.integrated || inner.diagnostics.length > 0)
893
+ // Only error-severity diagnostics are refusals — warnings (e.g. the
894
+ // FLOW-INT-16 primary-engine notice) must not skip cleanup and journaling.
895
+ if (!inner.integrated || inner.diagnostics.some((d) => d.severity === "error"))
380
896
  return inner;
381
897
  // (6) cleanup — outside the lock; every git call runs with -C primary.
898
+ // RFC-1536: prune this session's own id reservations once the branch
899
+ // landed — other sessions' entries stay untouched.
900
+ await pruneReservationsForSession(io, primary, branch);
382
901
  const worktrees = await worktreeList(primary, io);
383
902
  const branchWorktree = worktrees.find((w) => w.branch === branch)?.path ?? entry.worktree;
384
- const cleanup = await cleanupIntegrated(primary, branchWorktree, entry, common, inner.developAfter ?? inner.developBefore ?? "", io, now.toISOString(), entry.port !== null ? "backmerge" : "integrate", inner.developBefore ?? undefined);
903
+ const cleanup = await cleanupIntegrated(primary, branchWorktree, entry, common, inner.developAfter ?? inner.developBefore ?? "", io, now.toISOString(), entry.port !== null ? "backmerge" : "integrate", inner.developBefore ?? undefined, inner.toleratedDirt, inner.autoResolved, {
904
+ reattachedTo: inner.reattachedTo,
905
+ autoCleaned: inner.autoCleaned,
906
+ autocommitSha: inner.autocommitSha,
907
+ restoredImporters: inner.restoredImporters,
908
+ staleImporterCommitSha: inner.staleImporterCommitSha,
909
+ }, inner.gate?.reportPath ?? undefined);
385
910
  return {
386
911
  ...inner,
387
912
  gate: inner.gate,
@@ -403,6 +928,15 @@ export async function runFlowIntegrate(input, context) {
403
928
  branch: typeof branchFlag === "string" ? branchFlag : null,
404
929
  dryRun: context.dryRun || input.flags["dry-run"] === true,
405
930
  onProgress: (m) => logger.info(` ${m}`),
931
+ invoke: context.invoke,
932
+ commandRegistry: context.commandRegistry,
933
+ reattach: input.flags["reattach"] === true,
934
+ autoClean: input.flags["auto-clean"] === true,
935
+ autocommit: input.flags["autocommit"] === true,
936
+ waitForeignSeconds: typeof input.flags["wait-foreign"] === "string" ||
937
+ typeof input.flags["wait-foreign"] === "number"
938
+ ? Number(input.flags["wait-foreign"])
939
+ : undefined,
406
940
  });
407
941
  const refused = data.diagnostics.find((d) => d.severity === "error");
408
942
  if (outputFormat === "pretty") {
@@ -415,6 +949,21 @@ export async function runFlowIntegrate(input, context) {
415
949
  logger.info(` integrated: ${data.integrated}${data.rebased ? " (rebased)" : ""}`);
416
950
  logger.info(` develop: ${data.developBefore?.slice(0, 9)} → ${data.developAfter?.slice(0, 9)}`);
417
951
  logger.info(` cleanedUp: ${data.cleanedUp}`);
952
+ if (data.toleratedDirt !== undefined && data.toleratedDirt.length > 0) {
953
+ logger.info(` tolerated: ${data.toleratedDirt.map((t) => `${t.path} (${t.class})`).join(", ")}`);
954
+ }
955
+ if (data.toleratedState !== undefined && data.toleratedState.length > 0) {
956
+ logger.info(` tolerated forge state: ${data.toleratedState.join(", ")}`);
957
+ }
958
+ if (data.reattachedTo !== undefined) {
959
+ logger.info(` reattached: ${data.reattachedTo}`);
960
+ }
961
+ if (data.autoCleaned !== undefined && data.autoCleaned.length > 0) {
962
+ logger.info(` auto-cleaned: ${data.autoCleaned.join(", ")}`);
963
+ }
964
+ if (data.autocommitSha !== undefined) {
965
+ logger.info(` autocommit: ${data.autocommitSha.slice(0, 9)} (${SWEEP_COMMIT_SUBJECT})`);
966
+ }
418
967
  }
419
968
  }
420
969
  return {