@warpgogol/forge 9.7.0 → 10.0.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 (327) hide show
  1. package/AGENTS.md +50 -27
  2. package/CHANGELOG.md +52 -0
  3. package/bin/cli.js +0 -0
  4. package/dist/os/adr/handlers/archive.d.ts.map +1 -1
  5. package/dist/os/adr/handlers/archive.js +3 -7
  6. package/dist/os/adr/handlers/archive.js.map +1 -1
  7. package/dist/os/adr/handlers/list-create.d.ts.map +1 -1
  8. package/dist/os/adr/handlers/list-create.js +8 -6
  9. package/dist/os/adr/handlers/list-create.js.map +1 -1
  10. package/dist/os/audit/handlers/archive.d.ts.map +1 -1
  11. package/dist/os/audit/handlers/archive.js +3 -7
  12. package/dist/os/audit/handlers/archive.js.map +1 -1
  13. package/dist/os/codebase/handlers/codebase-measure-symbols.d.ts.map +1 -1
  14. package/dist/os/codebase/handlers/codebase-measure-symbols.js +35 -4
  15. package/dist/os/codebase/handlers/codebase-measure-symbols.js.map +1 -1
  16. package/dist/os/core/archive-regen.d.ts +1 -0
  17. package/dist/os/core/archive-regen.d.ts.map +1 -1
  18. package/dist/os/core/archive-regen.js +31 -6
  19. package/dist/os/core/archive-regen.js.map +1 -1
  20. package/dist/os/core/core.module.d.ts.map +1 -1
  21. package/dist/os/core/core.module.js +6 -69
  22. package/dist/os/core/core.module.js.map +1 -1
  23. package/dist/os/core/handlers/assets-helpers.d.ts.map +1 -1
  24. package/dist/os/core/handlers/assets-helpers.js +2 -3
  25. package/dist/os/core/handlers/assets-helpers.js.map +1 -1
  26. package/dist/os/core/handlers/determinism-check.d.ts.map +1 -1
  27. package/dist/os/core/handlers/determinism-check.js +2 -3
  28. package/dist/os/core/handlers/determinism-check.js.map +1 -1
  29. package/dist/os/core/handlers/pinned-check.d.ts +13 -0
  30. package/dist/os/core/handlers/pinned-check.d.ts.map +1 -1
  31. package/dist/os/core/handlers/pinned-check.js +25 -0
  32. package/dist/os/core/handlers/pinned-check.js.map +1 -1
  33. package/dist/os/core/handlers/pinned-types.d.ts +7 -0
  34. package/dist/os/core/handlers/pinned-types.d.ts.map +1 -1
  35. package/dist/os/core/handlers/pinned-validate.d.ts.map +1 -1
  36. package/dist/os/core/handlers/pinned-validate.js +24 -1
  37. package/dist/os/core/handlers/pinned-validate.js.map +1 -1
  38. package/dist/os/core/handlers/release-prepare.d.ts.map +1 -1
  39. package/dist/os/core/handlers/release-prepare.js +2 -4
  40. package/dist/os/core/handlers/release-prepare.js.map +1 -1
  41. package/dist/os/flow/digest.d.ts +73 -1
  42. package/dist/os/flow/digest.d.ts.map +1 -1
  43. package/dist/os/flow/digest.js +344 -92
  44. package/dist/os/flow/digest.js.map +1 -1
  45. package/dist/os/flow/evidence-lock.d.ts +36 -0
  46. package/dist/os/flow/evidence-lock.d.ts.map +1 -0
  47. package/dist/os/flow/evidence-lock.js +156 -0
  48. package/dist/os/flow/evidence-lock.js.map +1 -0
  49. package/dist/os/flow/flow.module.d.ts.map +1 -1
  50. package/dist/os/flow/flow.module.js +36 -14
  51. package/dist/os/flow/flow.module.js.map +1 -1
  52. package/dist/os/flow/git.d.ts +50 -3
  53. package/dist/os/flow/git.d.ts.map +1 -1
  54. package/dist/os/flow/git.js +144 -12
  55. package/dist/os/flow/git.js.map +1 -1
  56. package/dist/os/flow/handlers/flow-digest.d.ts.map +1 -1
  57. package/dist/os/flow/handlers/flow-digest.js +3 -5
  58. package/dist/os/flow/handlers/flow-digest.js.map +1 -1
  59. package/dist/os/flow/handlers/flow-peers.js +1 -1
  60. package/dist/os/flow/handlers/flow-peers.js.map +1 -1
  61. package/dist/os/flow/handlers/flow-push.d.ts +81 -0
  62. package/dist/os/flow/handlers/flow-push.d.ts.map +1 -1
  63. package/dist/os/flow/handlers/flow-push.js +642 -138
  64. package/dist/os/flow/handlers/flow-push.js.map +1 -1
  65. package/dist/os/flow/locks.d.ts +8 -0
  66. package/dist/os/flow/locks.d.ts.map +1 -1
  67. package/dist/os/flow/locks.js +6 -3
  68. package/dist/os/flow/locks.js.map +1 -1
  69. package/dist/os/{mission → flow}/open-missions.d.ts +1 -2
  70. package/dist/os/flow/open-missions.d.ts.map +1 -0
  71. package/dist/os/flow/open-missions.js +37 -0
  72. package/dist/os/flow/open-missions.js.map +1 -0
  73. package/dist/os/flow/preflight.d.ts.map +1 -1
  74. package/dist/os/flow/preflight.js +5 -3
  75. package/dist/os/flow/preflight.js.map +1 -1
  76. package/dist/os/flow/registry.d.ts +9 -5
  77. package/dist/os/flow/registry.d.ts.map +1 -1
  78. package/dist/os/flow/registry.js +2 -2
  79. package/dist/os/flow/registry.js.map +1 -1
  80. package/dist/os/friction/friction.module.d.ts.map +1 -1
  81. package/dist/os/friction/friction.module.js +30 -5
  82. package/dist/os/friction/friction.module.js.map +1 -1
  83. package/dist/os/friction/handlers/friction-add.d.ts.map +1 -1
  84. package/dist/os/friction/handlers/friction-add.js +10 -17
  85. package/dist/os/friction/handlers/friction-add.js.map +1 -1
  86. package/dist/os/friction/handlers/friction-migrate.d.ts.map +1 -1
  87. package/dist/os/friction/handlers/friction-migrate.js +188 -2
  88. package/dist/os/friction/handlers/friction-migrate.js.map +1 -1
  89. package/dist/os/friction/store.d.ts +4 -0
  90. package/dist/os/friction/store.d.ts.map +1 -1
  91. package/dist/os/friction/store.js +20 -0
  92. package/dist/os/friction/store.js.map +1 -1
  93. package/dist/os/friction/types.d.ts +12 -0
  94. package/dist/os/friction/types.d.ts.map +1 -1
  95. package/dist/os/friction/types.js +8 -0
  96. package/dist/os/friction/types.js.map +1 -1
  97. package/dist/os/gate/handlers/gate-probe-catalog.d.ts.map +1 -1
  98. package/dist/os/gate/handlers/gate-probe-catalog.js +38 -5
  99. package/dist/os/gate/handlers/gate-probe-catalog.js.map +1 -1
  100. package/dist/os/plan/handlers/archive.d.ts.map +1 -1
  101. package/dist/os/plan/handlers/archive.js +3 -7
  102. package/dist/os/plan/handlers/archive.js.map +1 -1
  103. package/dist/os/rfc/handlers/archive.d.ts.map +1 -1
  104. package/dist/os/rfc/handlers/archive.js +3 -7
  105. package/dist/os/rfc/handlers/archive.js.map +1 -1
  106. package/dist/os/rfc/handlers/list-create.d.ts.map +1 -1
  107. package/dist/os/rfc/handlers/list-create.js +15 -7
  108. package/dist/os/rfc/handlers/list-create.js.map +1 -1
  109. package/dist/os/session/handlers/archive.d.ts.map +1 -1
  110. package/dist/os/session/handlers/archive.js +3 -7
  111. package/dist/os/session/handlers/archive.js.map +1 -1
  112. package/dist/os/system/handlers/system-product-register.d.ts +24 -0
  113. package/dist/os/system/handlers/system-product-register.d.ts.map +1 -0
  114. package/dist/os/system/handlers/system-product-register.js +322 -0
  115. package/dist/os/system/handlers/system-product-register.js.map +1 -0
  116. package/dist/os/system/handlers/system-products-status.d.ts +23 -0
  117. package/dist/os/system/handlers/system-products-status.d.ts.map +1 -0
  118. package/dist/os/system/handlers/system-products-status.js +235 -0
  119. package/dist/os/system/handlers/system-products-status.js.map +1 -0
  120. package/dist/os/system/schemas.d.ts +13 -0
  121. package/dist/os/system/schemas.d.ts.map +1 -1
  122. package/dist/os/system/schemas.js +15 -0
  123. package/dist/os/system/schemas.js.map +1 -1
  124. package/dist/os/system/system.module.d.ts.map +1 -1
  125. package/dist/os/system/system.module.js +113 -3
  126. package/dist/os/system/system.module.js.map +1 -1
  127. package/dist/src/command-modules.d.ts.map +1 -1
  128. package/dist/src/command-modules.js +0 -2
  129. package/dist/src/command-modules.js.map +1 -1
  130. package/dist/src/config/forge-config.d.ts +2 -0
  131. package/dist/src/config/forge-config.d.ts.map +1 -1
  132. package/dist/src/config/forge-config.js +11 -10
  133. package/dist/src/config/forge-config.js.map +1 -1
  134. package/dist/src/index.d.ts +0 -2
  135. package/dist/src/index.d.ts.map +1 -1
  136. package/dist/src/index.js +0 -2
  137. package/dist/src/index.js.map +1 -1
  138. package/dist/src/onboarding/create.d.ts.map +1 -1
  139. package/dist/src/onboarding/create.js +27 -2
  140. package/dist/src/onboarding/create.js.map +1 -1
  141. package/dist/src/onboarding/doctor.d.ts +0 -5
  142. package/dist/src/onboarding/doctor.d.ts.map +1 -1
  143. package/dist/src/onboarding/doctor.js +6 -79
  144. package/dist/src/onboarding/doctor.js.map +1 -1
  145. package/dist/src/onboarding/npm-token-check.d.ts +1 -1
  146. package/dist/src/onboarding/npm-token-check.d.ts.map +1 -1
  147. package/dist/src/onboarding/npm-token-check.js +2 -2
  148. package/dist/src/onboarding/npm-token-check.js.map +1 -1
  149. package/dist/src/profiles/stack-profile.js +2 -2
  150. package/dist/src/types.js +2 -2
  151. package/dist/src/utils/hash.d.ts +1 -0
  152. package/dist/src/utils/hash.d.ts.map +1 -1
  153. package/dist/src/utils/hash.js +4 -0
  154. package/dist/src/utils/hash.js.map +1 -1
  155. package/dist/{os/mission → src/utils}/lockfile.d.ts +1 -18
  156. package/dist/src/utils/lockfile.d.ts.map +1 -0
  157. package/dist/src/utils/lockfile.js +70 -0
  158. package/dist/src/utils/lockfile.js.map +1 -0
  159. package/dist/src/utils/verifier-git.d.ts +27 -1
  160. package/dist/src/utils/verifier-git.d.ts.map +1 -1
  161. package/dist/src/utils/verifier-git.js +36 -6
  162. package/dist/src/utils/verifier-git.js.map +1 -1
  163. package/docs/reference/system-hub.md +104 -0
  164. package/os/adr/handlers/archive.ts +3 -11
  165. package/os/adr/handlers/list-create.test.ts +174 -0
  166. package/os/adr/handlers/list-create.ts +16 -8
  167. package/os/audit/handlers/archive.ts +3 -11
  168. package/os/codebase/handlers/codebase-measure-symbols.ts +32 -4
  169. package/os/core/archive-regen.ts +33 -7
  170. package/os/core/core.module.ts +10 -76
  171. package/os/core/docs-archive.test.ts +25 -0
  172. package/os/core/handlers/assets-helpers.ts +2 -4
  173. package/os/core/handlers/determinism-check.ts +2 -3
  174. package/os/core/handlers/lifecycle-handlers.test.ts +529 -0
  175. package/os/core/handlers/pinned-check.ts +30 -0
  176. package/os/core/handlers/pinned-types.ts +7 -0
  177. package/os/core/handlers/pinned-validate.ts +28 -3
  178. package/os/core/handlers/release-prepare.ts +2 -4
  179. package/os/correctness/gate/trust-boundary-cx18.test.ts +1 -1
  180. package/os/flow/digest.ts +436 -95
  181. package/os/flow/evidence-lock.ts +184 -0
  182. package/os/flow/flow.module.ts +37 -14
  183. package/os/flow/git.ts +206 -11
  184. package/os/flow/handlers/flow-digest.ts +12 -8
  185. package/os/flow/handlers/flow-peers.ts +1 -1
  186. package/os/flow/handlers/flow-push.ts +725 -137
  187. package/os/flow/locks.ts +24 -7
  188. package/os/flow/open-missions.ts +45 -0
  189. package/os/flow/preflight.ts +5 -3
  190. package/os/flow/registry.ts +7 -3
  191. package/os/flow/tests/digest-id-inputs.test.ts +106 -0
  192. package/os/flow/tests/digest-persist.test.ts +243 -0
  193. package/os/flow/tests/evidence-preflight-surface-skip.test.ts +218 -0
  194. package/os/flow/tests/flow-push-attest.test.ts +137 -0
  195. package/os/flow/tests/flow-push-review-baseline.test.ts +339 -0
  196. package/os/flow/tests/flow-push-verifier-batch.test.ts +134 -0
  197. package/os/flow/tests/release-lines.test.ts +136 -0
  198. package/os/flow/tests/state-roots.test.ts +1 -48
  199. package/os/friction/friction-add.test.ts +42 -0
  200. package/os/friction/friction-migrate.test.ts +67 -2
  201. package/os/friction/friction.module.ts +33 -5
  202. package/os/friction/handlers/friction-add.ts +10 -15
  203. package/os/friction/handlers/friction-migrate.ts +220 -1
  204. package/os/friction/store.ts +19 -0
  205. package/os/friction/types.ts +8 -0
  206. package/os/gate/handlers/gate-probe-catalog.ts +39 -7
  207. package/os/gate/handlers/gate-probe-ratchet.test.ts +57 -22
  208. package/os/plan/handlers/archive.ts +3 -11
  209. package/os/rfc/handlers/archive.ts +3 -11
  210. package/os/rfc/handlers/list-create.ts +22 -17
  211. package/os/rfc/handlers/validate-corpus.test.ts +149 -0
  212. package/os/rfc/probe-batch.test.ts +253 -0
  213. package/os/rfc/verifier/rfc-1391/ac10-operating-model-policy.acceptance.test.ts +63 -0
  214. package/os/session/handlers/archive.ts +3 -11
  215. package/os/system/handlers/system-product-register.ts +492 -0
  216. package/os/system/handlers/system-products-status.ts +395 -0
  217. package/os/system/schemas.ts +17 -0
  218. package/os/system/system.module.ts +117 -3
  219. package/os/system/tests/system-product-register.test.ts +304 -0
  220. package/os/system/tests/system-products-status.test.ts +154 -0
  221. package/package.json +161 -506
  222. package/profiles/root-agents-system.md +82 -0
  223. package/profiles/system.yaml +1 -0
  224. package/src/command-modules.ts +0 -2
  225. package/src/config/forge-config.ts +16 -11
  226. package/src/index.ts +0 -2
  227. package/src/onboarding/create-superfolder-guard.test.ts +73 -0
  228. package/src/onboarding/create.ts +28 -2
  229. package/src/onboarding/doctor.ts +9 -96
  230. package/src/onboarding/npm-token-check.ts +3 -3
  231. package/src/profiles/stack-profile.ts +2 -2
  232. package/src/tests/evidence-consult-serialization.test.ts +202 -0
  233. package/src/tests/pinned-validate.test.ts +194 -0
  234. package/src/tests/pkg-health-self-scan.test.ts +29 -0
  235. package/src/tests/profile-flow-hooks.test.ts +1 -0
  236. package/src/tests/profile-schema.test.ts +1 -0
  237. package/src/tests/rfc-evidence-sync.test.ts +369 -0
  238. package/src/tests/skill-agents-sync.test.ts +192 -0
  239. package/src/tests/stack-profile.test.ts +1 -0
  240. package/src/types.ts +2 -2
  241. package/src/utils/hash.ts +5 -0
  242. package/src/utils/lockfile.ts +78 -0
  243. package/src/utils/verifier-git.ts +53 -5
  244. package/dist/os/core/handlers/forge-autonomy-validate.d.ts +0 -25
  245. package/dist/os/core/handlers/forge-autonomy-validate.d.ts.map +0 -1
  246. package/dist/os/core/handlers/forge-autonomy-validate.js +0 -208
  247. package/dist/os/core/handlers/forge-autonomy-validate.js.map +0 -1
  248. package/dist/os/mission/handlers/archive.d.ts +0 -4
  249. package/dist/os/mission/handlers/archive.d.ts.map +0 -1
  250. package/dist/os/mission/handlers/archive.js +0 -587
  251. package/dist/os/mission/handlers/archive.js.map +0 -1
  252. package/dist/os/mission/importer-block.d.ts +0 -19
  253. package/dist/os/mission/importer-block.d.ts.map +0 -1
  254. package/dist/os/mission/importer-block.js +0 -124
  255. package/dist/os/mission/importer-block.js.map +0 -1
  256. package/dist/os/mission/index.d.ts +0 -2
  257. package/dist/os/mission/index.d.ts.map +0 -1
  258. package/dist/os/mission/index.js +0 -15
  259. package/dist/os/mission/index.js.map +0 -1
  260. package/dist/os/mission/lockfile.d.ts.map +0 -1
  261. package/dist/os/mission/lockfile.js +0 -138
  262. package/dist/os/mission/lockfile.js.map +0 -1
  263. package/dist/os/mission/mission.module.d.ts +0 -3
  264. package/dist/os/mission/mission.module.d.ts.map +0 -1
  265. package/dist/os/mission/mission.module.js +0 -63
  266. package/dist/os/mission/mission.module.js.map +0 -1
  267. package/dist/os/mission/open-missions.d.ts.map +0 -1
  268. package/dist/os/mission/open-missions.js +0 -42
  269. package/dist/os/mission/open-missions.js.map +0 -1
  270. package/dist/os/mission/types.d.ts +0 -23
  271. package/dist/os/mission/types.d.ts.map +0 -1
  272. package/dist/os/mission/types.js +0 -19
  273. package/dist/os/mission/types.js.map +0 -1
  274. package/dist/os/werkstatt/handlers/lock.d.ts +0 -10
  275. package/dist/os/werkstatt/handlers/lock.d.ts.map +0 -1
  276. package/dist/os/werkstatt/handlers/lock.js +0 -153
  277. package/dist/os/werkstatt/handlers/lock.js.map +0 -1
  278. package/dist/os/werkstatt/handlers/schema.d.ts +0 -33
  279. package/dist/os/werkstatt/handlers/schema.d.ts.map +0 -1
  280. package/dist/os/werkstatt/handlers/schema.js +0 -42
  281. package/dist/os/werkstatt/handlers/schema.js.map +0 -1
  282. package/dist/os/werkstatt/handlers/werkstatt-lock-recover.d.ts +0 -15
  283. package/dist/os/werkstatt/handlers/werkstatt-lock-recover.d.ts.map +0 -1
  284. package/dist/os/werkstatt/handlers/werkstatt-lock-recover.js +0 -171
  285. package/dist/os/werkstatt/handlers/werkstatt-lock-recover.js.map +0 -1
  286. package/dist/os/werkstatt/handlers/werkstatt-lock-status.d.ts +0 -18
  287. package/dist/os/werkstatt/handlers/werkstatt-lock-status.d.ts.map +0 -1
  288. package/dist/os/werkstatt/handlers/werkstatt-lock-status.js +0 -51
  289. package/dist/os/werkstatt/handlers/werkstatt-lock-status.js.map +0 -1
  290. package/dist/os/werkstatt/handlers/werkstatt-operation-validate.d.ts +0 -11
  291. package/dist/os/werkstatt/handlers/werkstatt-operation-validate.d.ts.map +0 -1
  292. package/dist/os/werkstatt/handlers/werkstatt-operation-validate.js +0 -86
  293. package/dist/os/werkstatt/handlers/werkstatt-operation-validate.js.map +0 -1
  294. package/dist/os/werkstatt/handlers/workspace-deps.d.ts +0 -12
  295. package/dist/os/werkstatt/handlers/workspace-deps.d.ts.map +0 -1
  296. package/dist/os/werkstatt/handlers/workspace-deps.js +0 -42
  297. package/dist/os/werkstatt/handlers/workspace-deps.js.map +0 -1
  298. package/dist/os/werkstatt/index.d.ts +0 -7
  299. package/dist/os/werkstatt/index.d.ts.map +0 -1
  300. package/dist/os/werkstatt/index.js +0 -18
  301. package/dist/os/werkstatt/index.js.map +0 -1
  302. package/dist/os/werkstatt/werkstatt.module.d.ts +0 -3
  303. package/dist/os/werkstatt/werkstatt.module.d.ts.map +0 -1
  304. package/dist/os/werkstatt/werkstatt.module.js +0 -91
  305. package/dist/os/werkstatt/werkstatt.module.js.map +0 -1
  306. package/os/core/handlers/forge-autonomy-validate.ts +0 -276
  307. package/os/correctness/gate/trust-boundary-cx19.test.ts +0 -514
  308. package/os/mission/handlers/archive.test.ts +0 -764
  309. package/os/mission/handlers/archive.ts +0 -719
  310. package/os/mission/handlers/ship-inflight-degraded.test.ts +0 -105
  311. package/os/mission/importer-block.ts +0 -119
  312. package/os/mission/index.ts +0 -15
  313. package/os/mission/lockfile.ts +0 -152
  314. package/os/mission/mission.module.ts +0 -68
  315. package/os/mission/open-missions.ts +0 -50
  316. package/os/mission/types.ts +0 -42
  317. package/os/werkstatt/handlers/lock.ts +0 -168
  318. package/os/werkstatt/handlers/schema.ts +0 -47
  319. package/os/werkstatt/handlers/werkstatt-lock-recover.ts +0 -206
  320. package/os/werkstatt/handlers/werkstatt-lock-status.ts +0 -82
  321. package/os/werkstatt/handlers/werkstatt-operation-validate.ts +0 -112
  322. package/os/werkstatt/handlers/workspace-deps.ts +0 -56
  323. package/os/werkstatt/index.ts +0 -39
  324. package/os/werkstatt/werkstatt.module.ts +0 -95
  325. package/src/tests/doctor-autonomy.test.ts +0 -246
  326. package/src/tests/forge-autonomy-validate.test.ts +0 -159
  327. package/src/tests/werkstatt-lock.test.ts +0 -357
@@ -1,719 +0,0 @@
1
- /*
2
- <MODULE_CONTRACT>
3
- <purpose>
4
- Mission archive handler — moves terminal-state mission directories (state: closed
5
- or state: aborted in mission.yaml) into status-specific subdirectories under
6
- missions/archive/<state>/ and moves non-terminal (open) directories found in
7
- archive subdirectories back to missions/.
8
- </purpose>
9
- <non-goals>
10
- <item>Does not validate mission manifest schema — reads only the state field.</item>
11
- <item>Does not change mission.yaml — only moves directories on disk.</item>
12
- <item>Does not import from @warpgogol/werkstatt-engine — @warpgogol/werkstatt-shared is the exempt shared boundary (FORGE-AUTONOMY-01).</item>
13
- </non-goals>
14
- </MODULE_CONTRACT>
15
- <CHANGE_SUMMARY>
16
- <item>RFC-1495: step 1 - forge state roots</item>
17
- <item>RFC-1505: steps 3-5 — tracked lockfile restore, no side-effect commits</item>
18
- <item>RFC-1505: review r1 — durable projections and lockfile restore hardening</item>
19
- <item>RFC-1505: review r2 — clone-only sternsystem enumeration and mission-gated lockfile net</item>
20
- <item>RFC-1544: attribute mission lockfile churn to owning checkout and auto-resolve foreign importer diffs (RFC-1544)</item>
21
- <history>RFC-0573, RFC-0733, RFC-0801, RFC-0804, RFC-0982, RFC-1097, RFC-1138, RFC-1397, warpgogol-m000175</history>
22
- </CHANGE_SUMMARY>
23
- */
24
-
25
- import { ambientIo as fs } from "../../../src/utils/io.ts";
26
- import { existsSync, lstatSync } from "../../../src/utils/sync-fs.ts";
27
- import path from "node:path";
28
- import { execSync } from "../../../src/utils/sync-fs.ts";
29
- import { parse as parseYaml } from "yaml";
30
- import type {
31
- ForgeCommandInput,
32
- ForgeCommandResult,
33
- ForgeRuntimeContext,
34
- } from "../../../src/types.ts";
35
- import { trashPath } from "../../../src/utils/fs-trash.ts";
36
- import { writeFileIfChanged } from "../../../src/utils/fs-idempotent.ts";
37
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
38
- // @ts-ignore — workspace dep, may not be installed when forge is used standalone from npm
39
- import type { ShipInflightResult } from "@warpgogol/werkstatt-shared/kernel/ship-inflight";
40
- import type { WorkspaceIO } from "../../../src/types.ts";
41
- import {
42
- MISSIONS_DIR,
43
- ARCHIVE_DIR_NAME,
44
- MISSION_TERMINAL_STATUSES,
45
- type MissionArchiveMove,
46
- type MissionArchiveSkip,
47
- type MissionArchiveResult,
48
- } from "../types.ts";
49
- import { loadPinnedManifest, isPinned, isIntraDirMove } from "../../core/handlers/pinned-check.ts";
50
- import { pruneMissionImportersFromLockfile, execFileSyncAdapter } from "../lockfile.ts";
51
- import { resolveStateRoots } from "../../flow/worktree-path.ts";
52
-
53
- const SERVICE_FOLDERS = [
54
- "node_modules",
55
- "dist",
56
- ".astro",
57
- ".wrangler",
58
- ".cache",
59
- ".turbo",
60
- ] as const;
61
-
62
- async function cleanServiceFolders(
63
- workpieceDir: string,
64
- keep: readonly string[] = [],
65
- ): Promise<string[]> {
66
- const removed: string[] = [];
67
- for (const folder of SERVICE_FOLDERS) {
68
- if (keep.includes(folder)) continue;
69
- const target = path.join(workpieceDir, folder);
70
- if (existsSync(target)) {
71
- await fs.rm(target);
72
- removed.push(folder);
73
- }
74
- }
75
- return removed;
76
- }
77
-
78
- // RFC-0982: Shared cache-entry predicate — prevents filter logic divergence.
79
- function isCacheEntry(name: string): boolean {
80
- return name.startsWith(".") || name === "node_modules" || name === "dist";
81
- }
82
-
83
- async function readMissionReleaseId(missionDir: string): Promise<string | null> {
84
- const manifestPath = path.join(missionDir, "mission.yaml");
85
- try {
86
- const raw = await fs.readFile(manifestPath);
87
- const parsed = parseYaml(raw) as Record<string, unknown>;
88
- const releaseId = parsed?.releaseId;
89
- if (typeof releaseId === "string" && releaseId.trim()) return releaseId.trim();
90
- } catch {
91
- // fall through
92
- }
93
- return null;
94
- }
95
-
96
- async function readMissionSystemId(missionDir: string): Promise<string | null> {
97
- const manifestPath = path.join(missionDir, "mission.yaml");
98
- try {
99
- const raw = await fs.readFile(manifestPath);
100
- const parsed = parseYaml(raw) as Record<string, unknown>;
101
- const systemId = parsed?.systemId;
102
- if (typeof systemId === "string" && systemId.trim()) return systemId.trim();
103
- } catch {
104
- // fall through — name-prefix derivation is the fallback
105
- }
106
- return null;
107
- }
108
-
109
- async function readMissionState(missionDir: string): Promise<string | null> {
110
- const manifestPath = path.join(missionDir, "mission.yaml");
111
- try {
112
- const raw = await fs.readFile(manifestPath);
113
- const parsed = parseYaml(raw) as Record<string, unknown>;
114
- const state = parsed?.state;
115
- if (typeof state === "string") return state.trim();
116
- } catch {
117
- // Fall through to secondary checks
118
- }
119
-
120
- // RFC-0982: Secondary — check for workpiece/.closed marker
121
- const closedMarker = path.join(missionDir, "workpiece", ".closed");
122
- if (existsSync(closedMarker)) return "closed";
123
-
124
- // RFC-0982: Tertiary — if workpiece/ exists and contains only cache entries,
125
- // treat as closed (post-close remnant pattern)
126
- const workpieceDir = path.join(missionDir, "workpiece");
127
- if (existsSync(workpieceDir)) {
128
- const entries = await fs.readdir(workpieceDir);
129
- const nonCacheEntries = entries.filter((e) => !isCacheEntry(e.name));
130
- if (nonCacheEntries.length === 0) {
131
- return "closed";
132
- }
133
- }
134
-
135
- return null;
136
- }
137
-
138
- // RFC-0982: Check if a mission directory is an orphaned remnant — no mission.yaml
139
- // and workpiece/ contains only cache entries (or no workpiece at all).
140
- async function isOrphanedRemnant(missionDir: string): Promise<boolean> {
141
- const manifestPath = path.join(missionDir, "mission.yaml");
142
- if (existsSync(manifestPath)) return false;
143
-
144
- const workpieceDir = path.join(missionDir, "workpiece");
145
- if (!existsSync(workpieceDir)) return true;
146
-
147
- const entries = await fs.readdir(workpieceDir);
148
- const nonCacheEntries = entries.filter((e) => !isCacheEntry(e.name));
149
- return nonCacheEntries.length === 0;
150
- }
151
-
152
- // RFC-0982: Check if a mission directory has non-cache content (source files,
153
- // configs, etc.) that warrants manual inspection rather than auto-cleanup.
154
- async function hasNonCacheContent(missionDir: string): Promise<boolean> {
155
- const entries = await fs.readdir(missionDir);
156
- for (const entry of entries) {
157
- if (entry.name === "workpiece") continue;
158
- if (entry.isDirectory && isCacheEntry(entry.name)) {
159
- continue;
160
- }
161
- // Any file or non-cache directory at mission root is content
162
- return true;
163
- }
164
-
165
- // Check workpiece/ for non-cache content
166
- const workpieceDir = path.join(missionDir, "workpiece");
167
- if (existsSync(workpieceDir)) {
168
- const wpEntries = await fs.readdir(workpieceDir);
169
- const nonCacheEntries = wpEntries.filter((e) => !isCacheEntry(e.name));
170
- if (nonCacheEntries.length > 0) return true;
171
- }
172
-
173
- return false;
174
- }
175
-
176
- interface MoveAttempt {
177
- moved: MissionArchiveMove | null;
178
- skip: MissionArchiveSkip | null;
179
- }
180
-
181
- async function moveMissionDir(
182
- sourcePath: string,
183
- targetPath: string,
184
- targetParentDir: string,
185
- missionId: string,
186
- state: string,
187
- sourceRel: string,
188
- targetRel: string,
189
- direction: "into-archive" | "out-of-archive",
190
- dryRun: boolean,
191
- logger?: { info: (msg: string) => void; warn: (msg: string) => void },
192
- ): Promise<MoveAttempt> {
193
- if (existsSync(targetPath)) {
194
- return { moved: null, skip: { missionId, dir: sourceRel, reason: "destination exists" } };
195
- }
196
-
197
- if (!dryRun) {
198
- if (targetParentDir) {
199
- await fs.mkdir(targetParentDir);
200
- }
201
-
202
- // RFC-0801: Clean service folders from workpiece before move.
203
- // node_modules/, dist/, .astro/, .wrangler/, .cache/, .turbo/ contain
204
- // broken symlinks and stale build artifacts that waste disk space in archive.
205
- // Aborted missions may not have a workpiece/ directory — skip gracefully.
206
- const workpieceDir = path.join(sourcePath, "workpiece");
207
- if (existsSync(workpieceDir)) {
208
- // Deferred-release guard: a closed mission without a releaseId still
209
- // needs release.prepare (ADR-0085 closed-workpiece rebuild path), and
210
- // archived dirs fall outside the pnpm-workspace `missions/*/workpiece`
211
- // glob — node_modules cannot be reinstalled in place. Keep it so the
212
- // archived workpiece stays release-ready.
213
- const keepFolders =
214
- direction === "into-archive" &&
215
- state === "closed" &&
216
- !(await readMissionReleaseId(sourcePath))
217
- ? ["node_modules"]
218
- : [];
219
- try {
220
- const removed = await cleanServiceFolders(workpieceDir, keepFolders);
221
- if (removed.length > 0 && logger) {
222
- logger.info(` cleaned service folders: ${removed.join(", ")}`);
223
- }
224
- if (keepFolders.length > 0 && logger) {
225
- logger.info(
226
- " kept node_modules — mission has no releaseId; release.prepare can still run on the archived workpiece",
227
- );
228
- }
229
- } catch (cleanupErr) {
230
- // Non-fatal: log warning and continue with move
231
- if (logger) {
232
- logger.warn(
233
- ` Warning: service folder cleanup failed for ${missionId}: ${cleanupErr instanceof Error ? cleanupErr.message : String(cleanupErr)}`,
234
- );
235
- }
236
- }
237
- }
238
-
239
- try {
240
- await fs.rename(sourcePath, targetPath);
241
- } catch (err) {
242
- if ((err as NodeJS.ErrnoException).code === "ENOENT") {
243
- return {
244
- moved: null,
245
- skip: { missionId, dir: sourceRel, reason: "already moved by another process" },
246
- };
247
- }
248
- throw err;
249
- }
250
-
251
- // Post-rename cleanup: a watcher or IDE may recreate stale cache
252
- // directories (e.g. .astro/) at the source path after the rename.
253
- // Remove the resurrected source so it doesn't leave orphan directories.
254
- if (existsSync(sourcePath)) {
255
- await trashPath(sourcePath);
256
- }
257
- }
258
-
259
- return {
260
- moved: { missionId, state, from: sourceRel, to: targetRel, direction },
261
- skip: null,
262
- };
263
- }
264
-
265
- export async function runMissionArchive(
266
- input: ForgeCommandInput,
267
- context: ForgeRuntimeContext,
268
- ): Promise<ForgeCommandResult<MissionArchiveResult>> {
269
- const { workspaceRoot, logger, outputFormat } = context;
270
- const missionsPath = path.join(workspaceRoot, MISSIONS_DIR);
271
-
272
- const dryRun = context.dryRun || input.flags["dry-run"] === true;
273
- const statusFilter = input.flags["status"] as string | undefined;
274
- const cleanOrphans = input.flags["clean-orphans"] === true;
275
-
276
- if (statusFilter && !MISSION_TERMINAL_STATUSES.includes(statusFilter as never)) {
277
- throw new Error(
278
- `Invalid --status "${statusFilter}". Must be one of: ${MISSION_TERMINAL_STATUSES.join(", ")}`,
279
- );
280
- }
281
-
282
- const moved: MissionArchiveMove[] = [];
283
- const skipped: MissionArchiveSkip[] = [];
284
-
285
- // RFC-1397: per-candidate ship-in-flight guard. Lazy bridge like
286
- // workspace-deps.ts: werkstatt-shared is a devDependency — absent on
287
- // standalone forge installs, where ship journals cannot exist anyway, so an
288
- // import failure degrades to warn-once + proceed (the guard is vacuous
289
- // there). --ship-op exempts the ship's own terminal archive step (internal
290
- // marker, never passed by hand).
291
- const shipOp = typeof input.flags["ship-op"] === "string" ? input.flags["ship-op"] : undefined;
292
- const io: WorkspaceIO = context.io ?? fs;
293
-
294
- // RFC-1495: open missions live in the worktree they were opened in, but the
295
- // archive is durable state — it belongs to the primary checkout. In a
296
- // single-worktree repo both roots coincide and nothing changes.
297
- const stateRoots = await resolveStateRoots(workspaceRoot, io).catch(() => ({
298
- worktreeRoot: workspaceRoot,
299
- primaryRoot: workspaceRoot,
300
- isPrimary: true,
301
- }));
302
- const archiveRoot = path.join(stateRoots.primaryRoot, MISSIONS_DIR, ARCHIVE_DIR_NAME);
303
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
304
- // @ts-ignore — workspace dep, may not be installed when forge is used standalone from npm
305
- let shipInflightMod: typeof import("@warpgogol/werkstatt-shared/kernel/ship-inflight") | null =
306
- null;
307
- try {
308
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
309
- // @ts-ignore — workspace dep, may not be installed when forge is used standalone from npm
310
- shipInflightMod = await import("@warpgogol/werkstatt-shared/kernel/ship-inflight");
311
- } catch {
312
- logger.warn(
313
- " ship-inflight guard unavailable (@warpgogol/werkstatt-shared not installed) — proceeding without it",
314
- );
315
- }
316
- const shipInflightFor = async (
317
- missionId: string,
318
- missionDir: string,
319
- ): Promise<ShipInflightResult | null> => {
320
- if (!shipInflightMod) return null;
321
- // Resolution chain: DNA-46 dir name → mission.yaml systemId → journal glob.
322
- // The glob is exhaustive — when it finds nothing, no ship journal exists
323
- // for this missionId anywhere, so the mission is definitively not in flight.
324
- const systemId =
325
- shipInflightMod.systemIdFromMissionDirName(missionId) ??
326
- (await readMissionSystemId(missionDir)) ??
327
- (await shipInflightMod.systemIdFromShipJournals(workspaceRoot, missionId, io));
328
- if (!systemId) return null;
329
- const res = await shipInflightMod.isShipInFlight(workspaceRoot, systemId, missionId, io, {
330
- ignoreOpId: shipOp,
331
- });
332
- return res.inFlight ? res : null;
333
- };
334
- const inflightMessage = (missionId: string, m: ShipInflightResult): string =>
335
- shipInflightMod
336
- ? shipInflightMod.shipInflightMessage(missionId, m)
337
- : `SHIP-INFLIGHT-01 mission ${missionId} is being shipped`;
338
-
339
- // RFC-0733: Load pinned manifest once per invocation
340
- let pinnedManifest = null;
341
- try {
342
- pinnedManifest = await loadPinnedManifest(workspaceRoot);
343
- } catch {
344
- // Malformed manifest — skip pre-check
345
- }
346
-
347
- if (!existsSync(missionsPath)) {
348
- if (outputFormat === "pretty") {
349
- if (dryRun) {
350
- logger.info("[dry-run] mission.archive: no missions/ directory — nothing to do");
351
- } else {
352
- logger.info("mission.archive: no missions/ directory — nothing to do");
353
- }
354
- }
355
- return {
356
- data: { command: "mission.archive", status: "ok", moved, skipped, dryRun },
357
- summary: dryRun
358
- ? "[dry-run] No missions/ directory — nothing to do"
359
- : "No missions/ directory — nothing to do",
360
- };
361
- }
362
-
363
- // Phase 1: Scan missions/ for terminal-state missions to move into archive
364
- const rootEntries = await fs.readdir(missionsPath);
365
-
366
- // Stale symlink cleanup: IDEs or file watchers may recreate symlinks to
367
- // archived mission directories after mission.archive has moved them.
368
- // These symlinks break pnpm workspace resolution and mission number derivation.
369
- // Trash them before they can cause downstream issues.
370
- for (const e of rootEntries) {
371
- if (
372
- lstatSync(path.join(missionsPath, e.name)).isSymbolicLink() &&
373
- e.name !== ARCHIVE_DIR_NAME
374
- ) {
375
- const symlinkPath = path.join(missionsPath, e.name);
376
- const sourceRel = `${MISSIONS_DIR}/${e.name}`;
377
- if (!dryRun) {
378
- await trashPath(symlinkPath);
379
- }
380
- skipped.push({
381
- missionId: e.name,
382
- dir: sourceRel,
383
- reason: "stale symlink — trashed",
384
- });
385
- if (outputFormat === "pretty") {
386
- logger.warn(` stale symlink: trashed ${sourceRel}`);
387
- }
388
- }
389
- }
390
-
391
- const rootDirs = rootEntries
392
- .filter(
393
- (e) =>
394
- e.isDirectory &&
395
- !lstatSync(path.join(missionsPath, e.name)).isSymbolicLink() &&
396
- e.name !== ARCHIVE_DIR_NAME,
397
- )
398
- .map((e) => e.name);
399
-
400
- for (const missionId of rootDirs) {
401
- const missionDir = path.join(missionsPath, missionId);
402
- const sourceRel = `${MISSIONS_DIR}/${missionId}`;
403
-
404
- // RFC-0982: --clean-orphans — trash orphaned remnant directories before state
405
- // detection. This catches dirs where mission.yaml is missing and only cache
406
- // entries remain, regardless of whether readMissionState returns "closed" or null.
407
- if (cleanOrphans && (await isOrphanedRemnant(missionDir))) {
408
- // RFC-1397: even an orphan-looking dir may be mid-ship (name prefix
409
- // resolves the systemId without a manifest) — check before trashing.
410
- const orphanInflight = await shipInflightFor(missionId, missionDir);
411
- if (orphanInflight && !orphanInflight.stale) {
412
- skipped.push({
413
- missionId,
414
- dir: sourceRel,
415
- reason: inflightMessage(missionId, orphanInflight),
416
- });
417
- if (outputFormat === "pretty") {
418
- logger.warn(` ${inflightMessage(missionId, orphanInflight)}`);
419
- }
420
- continue;
421
- }
422
- if (orphanInflight?.stale) {
423
- logger.warn(` ${inflightMessage(missionId, orphanInflight)}`);
424
- }
425
- if (!dryRun) {
426
- await trashPath(missionDir);
427
- }
428
- moved.push({
429
- missionId,
430
- state: "closed",
431
- from: sourceRel,
432
- to: "(trashed)",
433
- direction: "trashed-orphan",
434
- });
435
- continue;
436
- }
437
-
438
- const state = await readMissionState(missionDir);
439
-
440
- if (state === null) {
441
- const hasContent = await hasNonCacheContent(missionDir);
442
- skipped.push({
443
- missionId,
444
- dir: `${MISSIONS_DIR}/${missionId}`,
445
- reason: hasContent
446
- ? "unreadable manifest — manually inspect or add mission.yaml"
447
- : "empty remnant — use --clean-orphans to remove",
448
- });
449
- continue;
450
- }
451
-
452
- const isTerminal = MISSION_TERMINAL_STATUSES.includes(state as never);
453
-
454
- if (!isTerminal) {
455
- skipped.push({
456
- missionId,
457
- dir: `${MISSIONS_DIR}/${missionId}`,
458
- reason: `state ${state} is non-terminal`,
459
- });
460
- continue;
461
- }
462
-
463
- if (statusFilter && state !== statusFilter) {
464
- skipped.push({
465
- missionId,
466
- dir: `${MISSIONS_DIR}/${missionId}`,
467
- reason: `state ${state} does not match --status ${statusFilter}`,
468
- });
469
- continue;
470
- }
471
-
472
- // RFC-1397: never move a mission whose leitstand.ship op is still open.
473
- const inflight = await shipInflightFor(missionId, missionDir);
474
- if (inflight && !inflight.stale) {
475
- skipped.push({ missionId, dir: sourceRel, reason: inflightMessage(missionId, inflight) });
476
- if (outputFormat === "pretty") {
477
- logger.warn(` ${inflightMessage(missionId, inflight)}`);
478
- }
479
- continue;
480
- }
481
- if (inflight?.stale) {
482
- logger.warn(` ${inflightMessage(missionId, inflight)}`);
483
- }
484
-
485
- // Warn when archiving a closed mission without a release — operator likely
486
- // forgot to run release.prepare before archiving. Non-blocking: some missions
487
- // (e.g. aborted, content-only) legitimately have no release. The workpiece
488
- // node_modules are preserved in this case so the archived mission stays
489
- // release-ready (see deferred-release guard in moveMissionDir).
490
- if (state === "closed" && !dryRun) {
491
- const releaseId = await readMissionReleaseId(missionDir);
492
- if (!releaseId && outputFormat === "pretty") {
493
- logger.warn(
494
- ` ⚠ ${missionId}: closed mission has no releaseId — keeping workpiece node_modules so release.prepare can still run on the archived workpiece`,
495
- );
496
- }
497
- }
498
-
499
- const targetDir = path.join(archiveRoot, state);
500
- const targetPath = path.join(targetDir, missionId);
501
- const targetRel = `${MISSIONS_DIR}/${ARCHIVE_DIR_NAME}/${state}/${missionId}`;
502
-
503
- // RFC-0733: Check if mission directory is pinned before moving
504
- // Gap fix: exempt intra-directory moves (dir stays within the same pinned parent)
505
- if (
506
- pinnedManifest &&
507
- isPinned(pinnedManifest, sourceRel) &&
508
- !isIntraDirMove(pinnedManifest, sourceRel, targetRel)
509
- ) {
510
- skipped.push({
511
- missionId,
512
- dir: sourceRel,
513
- reason: "pinned (protected by .forge/pinned.yaml)",
514
- });
515
- if (outputFormat === "pretty") {
516
- logger.warn(` pinned: skipping ${sourceRel} (protected)`);
517
- }
518
- continue;
519
- }
520
-
521
- const result = await moveMissionDir(
522
- missionDir,
523
- targetPath,
524
- targetDir,
525
- missionId,
526
- state,
527
- sourceRel,
528
- targetRel,
529
- "into-archive",
530
- dryRun,
531
- logger,
532
- );
533
- if (result.skip) {
534
- skipped.push(result.skip);
535
- continue;
536
- }
537
- if (result.moved) moved.push(result.moved);
538
- }
539
-
540
- // Phase 2: Scan missions/archive/<state>/ for non-terminal missions to move back
541
- const archivePath = archiveRoot;
542
- if (existsSync(archivePath)) {
543
- const stateDirs = await fs.readdir(archivePath);
544
- const stateDirNames = stateDirs.filter((e) => e.isDirectory).map((e) => e.name);
545
-
546
- for (const stateDirName of stateDirNames) {
547
- const stateDirPath = path.join(archivePath, stateDirName);
548
- const archivedMissions = await fs.readdir(stateDirPath);
549
- const archivedDirNames = archivedMissions.filter((e) => e.isDirectory).map((e) => e.name);
550
-
551
- for (const missionId of archivedDirNames) {
552
- const archivedMissionDir = path.join(stateDirPath, missionId);
553
- const state = await readMissionState(archivedMissionDir);
554
-
555
- if (state === null) {
556
- skipped.push({
557
- missionId,
558
- dir: `${MISSIONS_DIR}/${ARCHIVE_DIR_NAME}/${stateDirName}/${missionId}`,
559
- reason: "unreadable manifest",
560
- });
561
- continue;
562
- }
563
-
564
- const isTerminal = MISSION_TERMINAL_STATUSES.includes(state as never);
565
-
566
- if (isTerminal) {
567
- skipped.push({
568
- missionId,
569
- dir: `${MISSIONS_DIR}/${ARCHIVE_DIR_NAME}/${stateDirName}/${missionId}`,
570
- reason: `already archived (${state})`,
571
- });
572
- continue;
573
- }
574
-
575
- // RFC-1397: moving back out of archive is still a mid-ship move —
576
- // same guard as Phase 1.
577
- const inflightBack = await shipInflightFor(missionId, archivedMissionDir);
578
- if (inflightBack && !inflightBack.stale) {
579
- skipped.push({
580
- missionId,
581
- dir: `${MISSIONS_DIR}/${ARCHIVE_DIR_NAME}/${stateDirName}/${missionId}`,
582
- reason: inflightMessage(missionId, inflightBack),
583
- });
584
- if (outputFormat === "pretty") {
585
- logger.warn(` ${inflightMessage(missionId, inflightBack)}`);
586
- }
587
- continue;
588
- }
589
- if (inflightBack?.stale) {
590
- logger.warn(` ${inflightMessage(missionId, inflightBack)}`);
591
- }
592
-
593
- // Non-terminal in archive — move back to missions/
594
- const targetPath = path.join(missionsPath, missionId);
595
- const targetRel = `${MISSIONS_DIR}/${missionId}`;
596
- const sourceRel = `${MISSIONS_DIR}/${ARCHIVE_DIR_NAME}/${stateDirName}/${missionId}`;
597
-
598
- // RFC-0733: Check if mission directory is pinned before moving
599
- // Gap fix: exempt intra-directory moves (dir stays within the same pinned parent)
600
- if (
601
- pinnedManifest &&
602
- isPinned(pinnedManifest, sourceRel) &&
603
- !isIntraDirMove(pinnedManifest, sourceRel, targetRel)
604
- ) {
605
- skipped.push({
606
- missionId,
607
- dir: sourceRel,
608
- reason: "pinned (protected by .forge/pinned.yaml)",
609
- });
610
- if (outputFormat === "pretty") {
611
- logger.warn(` pinned: skipping ${sourceRel} (protected)`);
612
- }
613
- continue;
614
- }
615
-
616
- const result = await moveMissionDir(
617
- archivedMissionDir,
618
- targetPath,
619
- "",
620
- missionId,
621
- state,
622
- sourceRel,
623
- targetRel,
624
- "out-of-archive",
625
- dryRun,
626
- logger,
627
- );
628
- if (result.skip) {
629
- skipped.push(result.skip);
630
- continue;
631
- }
632
- if (result.moved) moved.push(result.moved);
633
- }
634
- }
635
- }
636
-
637
- if (outputFormat === "pretty") {
638
- if (dryRun) {
639
- logger.info(
640
- `[dry-run] mission.archive: would move ${moved.length} mission(s), skip ${skipped.length}`,
641
- );
642
- } else {
643
- logger.success(
644
- `mission.archive: moved ${moved.length} mission(s), skipped ${skipped.length}`,
645
- );
646
- }
647
- for (const m of moved) {
648
- logger.info(` ${m.direction}: ${m.missionId} (${m.state}) ${m.from} → ${m.to}`);
649
- }
650
- }
651
-
652
- // RFC-0804: Post-move dependency prune. RFC-1505: the local install may churn
653
- // pnpm-lock.yaml — that churn is mission-local state and is never committed;
654
- // the tracked lockfile is restored to HEAD bytes instead.
655
- if (!dryRun && moved.length > 0) {
656
- try {
657
- // maxBuffer matches restoreTrackedLockfile's adapter — install output on
658
- // a pruned workspace can exceed the ~1 MiB execSync default.
659
- execSync("pnpm install", {
660
- cwd: workspaceRoot,
661
- stdio: ["pipe", "pipe", "pipe"],
662
- timeout: 120_000,
663
- maxBuffer: 64 * 1024 * 1024,
664
- });
665
- } catch (installErr) {
666
- logger.warn(
667
- ` pnpm install failed after archive — dependencies may be stale. Run 'pnpm install' manually: ${installErr instanceof Error ? installErr.message : String(installErr)}`,
668
- );
669
- }
670
- try {
671
- // RFC-1544 AC-5: the moved missions' importer blocks are legitimate
672
- // tracked removals — HEAD-minus-importers is staged for the archive
673
- // bookkeeping commit; every other install churn is still discarded.
674
- const pruned = await pruneMissionImportersFromLockfile(
675
- workspaceRoot,
676
- moved.map((m) => m.missionId),
677
- {
678
- ...execFileSyncAdapter(),
679
- writeFile: async (target, content) => {
680
- await writeFileIfChanged(target, content);
681
- },
682
- },
683
- );
684
- if (pruned.pruned.length > 0) {
685
- logger.info(
686
- ` Pruned importer(s) ${pruned.pruned.map((id) => `missions/${id}/workpiece`).join(", ")} from pnpm-lock.yaml` +
687
- (pruned.staged
688
- ? " — staged for the bookkeeping commit"
689
- : " — staging failed; prune left in the working tree"),
690
- );
691
- } else {
692
- logger.info(" Restored pnpm-lock.yaml to HEAD bytes (install churn discarded)");
693
- }
694
- } catch (refreshErr) {
695
- logger.warn(
696
- ` Lockfile restore failed after archive — lockfile may be stale. Run 'pnpm install' manually: ${refreshErr instanceof Error ? refreshErr.message : String(refreshErr)}`,
697
- );
698
- }
699
- }
700
-
701
- return {
702
- data: {
703
- command: "mission.archive",
704
- status: "ok",
705
- moved,
706
- skipped,
707
- dryRun,
708
- },
709
- summary: dryRun
710
- ? `[dry-run] Would move ${moved.length} mission(s), skip ${skipped.length}`
711
- : `Moved ${moved.length} mission(s), skipped ${skipped.length}`,
712
- nextSteps: [
713
- {
714
- action: "Run: pnpm exec werkstatt run mission.list --json to verify active missions",
715
- kind: "optional",
716
- },
717
- ],
718
- };
719
- }