@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
@@ -17,17 +17,14 @@ mission source stays where the command ran.
17
17
  */
18
18
 
19
19
  import { test, expect, describe, afterEach } from "vitest";
20
- import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
21
- import { existsSync } from "node:fs";
20
+ import { mkdtemp, rm } from "node:fs/promises";
22
21
  import { tmpdir } from "node:os";
23
22
  import { basename, dirname, join } from "node:path";
24
23
 
25
24
  import { ambientIo } from "../../../src/utils/io.ts";
26
25
  import { canonicalRepoDir } from "../../../src/utils/repo-path.ts";
27
- import type { ForgeCommandInput, ForgeRuntimeContext } from "../../../src/types.ts";
28
26
 
29
27
  import { resolveStateRoots } from "../worktree-path.ts";
30
- import { runMissionArchive } from "../../mission/handlers/archive.ts";
31
28
  import { gitExec, initRepo } from "./helpers.ts";
32
29
 
33
30
  const io = ambientIo;
@@ -37,26 +34,6 @@ afterEach(async () => {
37
34
  for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true });
38
35
  });
39
36
 
40
- function makeContext(workspaceRoot: string): ForgeRuntimeContext {
41
- return {
42
- workspaceRoot,
43
- io,
44
- logger: {
45
- info: () => {},
46
- success: () => {},
47
- warn: () => {},
48
- error: () => {},
49
- debug: () => {},
50
- },
51
- outputFormat: "json",
52
- dryRun: false,
53
- } as unknown as ForgeRuntimeContext;
54
- }
55
-
56
- function makeInput(flags: Record<string, unknown> = {}): ForgeCommandInput {
57
- return { commandName: "mission.archive", flags } as unknown as ForgeCommandInput;
58
- }
59
-
60
37
  describe("resolveStateRoots (RFC-1495)", () => {
61
38
  test("single-worktree repo resolves every root to itself", async () => {
62
39
  const repo = await initRepo({ prefix: "state-roots-" });
@@ -100,28 +77,4 @@ describe("resolveStateRoots (RFC-1495)", () => {
100
77
  });
101
78
  });
102
79
 
103
- test("mission.archive run in a session worktree lands in the primary archive", async () => {
104
- const repo = await initRepo({ prefix: "state-roots-" });
105
- dirs.push(repo);
106
- const wt = join(dirname(repo), `${basename(repo)}@session-y`);
107
- const add = await gitExec(repo, ["worktree", "add", "-b", "session-y", wt]);
108
- expect(add.exitCode).toBe(0);
109
- dirs.push(wt);
110
-
111
- // A closed mission exists in the session worktree only.
112
- const missionId = "acme-m000001";
113
- const missionDir = join(wt, "missions", missionId);
114
- await mkdir(missionDir, { recursive: true });
115
- await writeFile(join(missionDir, "mission.yaml"), `missionId: ${missionId}\nstate: closed\n`);
116
-
117
- const result = await runMissionArchive(makeInput(), makeContext(wt));
118
- expect(result.exitCode ?? 0).toBe(0);
119
-
120
- // Durable target: the primary's missions/archive/closed/, never the wt's.
121
- expect(existsSync(join(repo, "missions", "archive", "closed", missionId, "mission.yaml"))).toBe(
122
- true,
123
- );
124
- expect(existsSync(join(wt, "missions", "archive"))).toBe(false);
125
- expect(existsSync(missionDir)).toBe(false);
126
- });
127
80
  });
@@ -6,6 +6,7 @@
6
6
  </non-goals>
7
7
  </MODULE_CONTRACT>
8
8
  <CHANGE_SUMMARY>
9
+ <item>RFC-1639: step 5-6 — acceptance tests, system-hub reference, regenerated manifests</item>
9
10
  </CHANGE_SUMMARY>
10
11
  */
11
12
 
@@ -117,6 +118,47 @@ describe("friction.add — write path", () => {
117
118
  });
118
119
  });
119
120
 
121
+ describe("friction.add — --level (RFC-1639 AC-5)", () => {
122
+ test("--level folder stamps level: folder on a ledger-minted record", async () => {
123
+ const repo = await initRepo({ prefix: "friction-level-", branch: "develop" });
124
+ try {
125
+ const result = await runFrictionAdd(
126
+ { argv: [], flags: { ...VALID_FLAGS, session: "s-1", level: "folder" } },
127
+ ctx(repo),
128
+ );
129
+ expect(result.exitCode).toBe(0);
130
+ const rec = YAML.parse(
131
+ await fs.readFile(path.join(repo, FRICTION_DIR, "F-0001.yaml"), "utf8"),
132
+ );
133
+ expect(rec.level).toBe("folder");
134
+ } finally {
135
+ await fs.rm(repo, { recursive: true, force: true });
136
+ }
137
+ });
138
+
139
+ test("a level outside the catalog refuses without writing", async () => {
140
+ const result = await runFrictionAdd(
141
+ { argv: [], flags: { ...VALID_FLAGS, level: "global" } },
142
+ ctx(tmpDir),
143
+ );
144
+ expect(result.exitCode).toBe(1);
145
+ expect(result.data?.errors[0]?.ruleId).toBe("FRICTION-03");
146
+ expect(await recordFiles(tmpDir)).toEqual([]);
147
+ });
148
+
149
+ test("records without --level omit the field (repo is the default)", async () => {
150
+ const result = await runFrictionAdd(
151
+ { argv: [], flags: { ...VALID_FLAGS, session: "s-1" } },
152
+ ctx(tmpDir),
153
+ );
154
+ expect(result.exitCode).toBe(0);
155
+ const rec = YAML.parse(
156
+ await fs.readFile(path.join(tmpDir, FRICTION_DIR, "F-0001.yaml"), "utf8"),
157
+ );
158
+ expect(rec.level).toBeUndefined();
159
+ });
160
+ });
161
+
120
162
  describe("friction.add — concurrent ids (AC-2)", () => {
121
163
  test("two worktrees of one repo allocate different ids", async () => {
122
164
  const primary = await initRepo({ prefix: "friction-add-", branch: "develop" });
@@ -6,6 +6,7 @@
6
6
  </non-goals>
7
7
  </MODULE_CONTRACT>
8
8
  <CHANGE_SUMMARY>
9
+ <item>RFC-1639: step 5-6 — acceptance tests, system-hub reference, regenerated manifests</item>
9
10
  </CHANGE_SUMMARY>
10
11
  */
11
12
 
@@ -16,6 +17,7 @@ import os from "node:os";
16
17
  import YAML from "yaml";
17
18
 
18
19
  import type { ForgeFlagValue, ForgeRuntimeContext } from "../../src/types.ts";
20
+ import { initRepo } from "../flow/tests/helpers.ts";
19
21
  import { runFrictionMigrate } from "./handlers/friction-migrate.ts";
20
22
  import { LEGACY_FRICTION_FILE, loadFrictions, loadFrictionSources } from "./store.ts";
21
23
  import { ambientIo } from "../../src/utils/io.ts";
@@ -32,14 +34,17 @@ afterEach(async () => {
32
34
  await fs.rm(tmpDir, { recursive: true, force: true });
33
35
  });
34
36
 
35
- function ctx(flags: Record<string, ForgeFlagValue> = {}): {
37
+ function ctxFor(
38
+ root: string,
39
+ flags: Record<string, ForgeFlagValue> = {},
40
+ ): {
36
41
  input: { argv: string[]; flags: Record<string, ForgeFlagValue> };
37
42
  context: ForgeRuntimeContext;
38
43
  } {
39
44
  return {
40
45
  input: { argv: [], flags },
41
46
  context: {
42
- workspaceRoot: tmpDir,
47
+ workspaceRoot: root,
43
48
  dryRun: false,
44
49
  outputFormat: "json",
45
50
  logger: {
@@ -53,6 +58,10 @@ function ctx(flags: Record<string, ForgeFlagValue> = {}): {
53
58
  };
54
59
  }
55
60
 
61
+ function ctx(flags: Record<string, ForgeFlagValue> = {}) {
62
+ return ctxFor(tmpDir, flags);
63
+ }
64
+
56
65
  const LEGACY_RECORDS: FrictionEntry[] = [1, 2, 3].map((n) => ({
57
66
  id: `F-000${n}`,
58
67
  created: "2026-10-29",
@@ -71,6 +80,15 @@ async function writeLegacyFile(): Promise<void> {
71
80
  await fs.writeFile(path.join(tmpDir, LEGACY_FRICTION_FILE), YAML.stringify(LEGACY_RECORDS));
72
81
  }
73
82
 
83
+ async function fileExistsAbs(abs: string): Promise<boolean> {
84
+ try {
85
+ await fs.access(abs);
86
+ return true;
87
+ } catch {
88
+ return false;
89
+ }
90
+ }
91
+
74
92
  async function fileExists(rel: string): Promise<boolean> {
75
93
  try {
76
94
  await fs.stat(path.join(tmpDir, rel));
@@ -136,3 +154,50 @@ describe("friction.migrate — legacy split (AC-5)", () => {
136
154
  expect(s.dir).toEqual([]);
137
155
  });
138
156
  });
157
+
158
+ describe("friction.migrate --from — external ingest (RFC-1639 AC-4)", () => {
159
+ test("ingests external entries with provenance and removes the source", async () => {
160
+ const repo = await initRepo({ prefix: "friction-ingest-", branch: "develop" });
161
+ try {
162
+ const src = await fs.mkdtemp(path.join(os.tmpdir(), "friction-src-"));
163
+ const srcFile = path.join(src, "docs", "frictions.yaml");
164
+ await fs.mkdir(path.dirname(srcFile), { recursive: true });
165
+ await fs.writeFile(srcFile, YAML.stringify({ entries: LEGACY_RECORDS }));
166
+ const { input, context } = ctxFor(repo, { from: srcFile, session: "s-ingest" });
167
+ const result = await runFrictionMigrate(input, context);
168
+ expect(result.exitCode).toBe(0);
169
+ expect(result.data?.migrated).toBe(3);
170
+ expect(await fileExistsAbs(srcFile)).toBe(false);
171
+
172
+ const recs = await loadFrictions(repo, ambientIo);
173
+ expect(recs).toHaveLength(3);
174
+ // Legacy ids renumber onto the shared ledger sequence; provenance is
175
+ // stamped on every record.
176
+ expect(recs.map((r) => r.migratedFrom)).toEqual(["F-0001", "F-0002", "F-0003"]);
177
+ expect(recs.map((r) => r.origin)).toEqual([
178
+ path.basename(src),
179
+ path.basename(src),
180
+ path.basename(src),
181
+ ]);
182
+ expect(recs.map((r) => r.level)).toEqual(["folder", "folder", "folder"]);
183
+ expect(recs.map((r) => r.id)).toEqual(["F-0001", "F-0002", "F-0003"]);
184
+
185
+ // Re-run is deterministic — recreating the removed source (a
186
+ // crash-before-removal shape) skips provenance-marked records.
187
+ await fs.writeFile(srcFile, YAML.stringify({ entries: LEGACY_RECORDS }));
188
+ const rerun = await runFrictionMigrate(input, context);
189
+ expect(rerun.exitCode).toBe(0);
190
+ expect(rerun.data?.migrated).toBe(0);
191
+ expect(rerun.data?.skipped).toHaveLength(3);
192
+ } finally {
193
+ await fs.rm(repo, { recursive: true, force: true });
194
+ }
195
+ });
196
+
197
+ test("--from inside the workspace refuses", async () => {
198
+ const { input, context } = ctx({ from: "docs/frictions.yaml" });
199
+ const result = await runFrictionMigrate(input, context);
200
+ expect(result.exitCode).toBe(1);
201
+ expect(result.data?.errors.some((e) => e.ruleId === "FRICTION-01")).toBe(true);
202
+ });
203
+ });
@@ -6,14 +6,14 @@
6
6
  </non-goals>
7
7
  </MODULE_CONTRACT>
8
8
  <CHANGE_SUMMARY>
9
- <item>RFC-1461: declare rules[] severity via CommandRuleDecl workspace-wide (RFC-1461)</item>
10
- <item>RFC-1460: recurring frictions escalate to mechanical measures — FRICTION-06/07/08, registry flips, session-retro escalation</item>
11
9
  <item>RFC-1590: store append-only shared ledgers as one file per record</item>
12
10
  <item>RFC-1601: ledger writers assign + refusals teach the violated rule (RFC-1601)
13
11
 
14
12
  FRICTION-11 flags any out-of-vocabulary friction field value at validate, naming field+value+closed set — the mechanical detector for hand-edit corruption (F-0206/F-0244). queue.decision is the new decision-ledger writer: schema-gated append stamping createdAt/answeredAt, next-free Q-id default, QUEUE-05/QUEUE-DEC-01 refusals carrying the minimal shape. ecosystem.commit --rfc/--adr normalize bare digits or refuse EC-35 with the format statement — the F-0248 silent-injection-skip class is closed, and a summary-record failure now names the id + skipped injection. RFC-IMP-10 refusal leads with the rule statement; pipeline conventions and guides point at the command write path.</item>
15
13
  <item>RFC-1592: retire 3 unprobeable rules (F-0288 recurrence) unblocking this worktree's RFC-1592 ratchet</item>
16
- <history>RFC-1433</history>
14
+ <item>RFC-1639: step 3-4 — friction.migrate --from ingest, friction.add --level, CREATE-SUPERFOLDER-01 guard</item>
15
+ <item>RFC-1639: review — SYSTEM-MAP-01 rules decl, git-probe warnings, sibling kind/path discriminants, friction.add provenance, ingest hardening</item>
16
+ <history>RFC-1433, RFC-1460, RFC-1461</history>
17
17
  </CHANGE_SUMMARY>
18
18
  */
19
19
 
@@ -150,6 +150,12 @@ export async function createForgeFrictionModule(): Promise<ForgeModule> {
150
150
  description:
151
151
  "Existing record id — increments recurrences and sets lastSeen instead of writing a new record.",
152
152
  },
153
+ level: {
154
+ kind: "string",
155
+ required: false,
156
+ description:
157
+ "Record level: repo (default — omitted) or folder (RFC-1639 — the record concerns the superfolder, not this repo).",
158
+ },
153
159
  },
154
160
  execute: runFrictionAdd,
155
161
  },
@@ -160,10 +166,15 @@ export async function createForgeFrictionModule(): Promise<ForgeModule> {
160
166
  `files under ${FRICTION_DIR}/, remove the legacy file in the ` +
161
167
  "same run, and move every .agents/skills/*/qa-log.md to " +
162
168
  "qa-log/legacy.md. Idempotent — with no legacy file and no " +
163
- "qa-log.md files it reports migrated: 0.",
169
+ "qa-log.md files it reports migrated: 0. RFC-1639: --from " +
170
+ "<external frictions.yaml> ingests a registry from outside the " +
171
+ "workspace (ledger-minted ids, migratedFrom/origin/level provenance).",
164
172
  scope: "workspace",
165
173
  contract: "frictions",
166
- rules: [{ id: "FRICTION-01", severity: "error" }],
174
+ rules: [
175
+ { id: "FRICTION-01", severity: "error" },
176
+ { id: "FRICTION-03", severity: "error" },
177
+ ],
167
178
  mutatesState: true,
168
179
  idempotency: "rerun-safe",
169
180
  reads: [`${FRICTION_DIR}/**`, LEGACY_FRICTION_FILE, ".agents/skills/**"],
@@ -175,6 +186,23 @@ export async function createForgeFrictionModule(): Promise<ForgeModule> {
175
186
  required: false,
176
187
  description: "Report the split and shard moves without writing.",
177
188
  },
189
+ from: {
190
+ kind: "string",
191
+ required: false,
192
+ description:
193
+ "RFC-1639: ingest an EXTERNAL legacy frictions.yaml — records mint at the next F-ids via the shared ledger with migratedFrom+origin provenance; the source file is removed last. Must point outside the workspace.",
194
+ },
195
+ level: {
196
+ kind: "string",
197
+ required: false,
198
+ description: "Level stamped on --from-ingested records: folder (default) or repo.",
199
+ },
200
+ session: {
201
+ kind: "string",
202
+ required: false,
203
+ description:
204
+ "Session id to attribute the --from ledger allocation to — defaults to the current branch name.",
205
+ },
178
206
  },
179
207
  execute: runFrictionMigrate,
180
208
  },
@@ -22,6 +22,7 @@ and lastSeen is set — per-record files only.</purpose>
22
22
  <item>RFC-1590: friction.add — governance-reserved ids, write-time
23
23
  vocabulary gate (FRICTION-01..05 reuse), --recurrence-of counter path.</item>
24
24
  <item>RFC-1590: store append-only shared ledgers as one file per record</item>
25
+ <item>RFC-1639: step 3-4 — friction.migrate --from ingest, friction.add --level, CREATE-SUPERFOLDER-01 guard</item>
25
26
  </CHANGE_SUMMARY>
26
27
  */
27
28
 
@@ -34,9 +35,9 @@ import type {
34
35
  } from "../../../src/types.ts";
35
36
 
36
37
  import { reserveNextGovernanceId } from "../../rfc/next-id.ts";
37
- import { git } from "../../flow/git.ts";
38
38
  import {
39
39
  LegacyRecordMutationError,
40
+ defaultSession,
40
41
  frictionSourceMax,
41
42
  loadFrictionSources,
42
43
  updateFriction,
@@ -90,19 +91,6 @@ function refuse(
90
91
  };
91
92
  }
92
93
 
93
- /** Session attribution default — the current branch name; detached or
94
- * non-git invocations record "manual". */
95
- async function defaultSession(cwd: string): Promise<string> {
96
- try {
97
- const r = await git(cwd, fs, ["branch", "--show-current"]);
98
- const name = r.stdout.trim();
99
- if (r.exitCode === 0 && name.length > 0) return name;
100
- } catch {
101
- /* non-git cwd — manual attribution */
102
- }
103
- return "manual";
104
- }
105
-
106
94
  export async function runFrictionAdd(
107
95
  input: ForgeCommandInput,
108
96
  context: ForgeRuntimeContext,
@@ -161,6 +149,7 @@ export async function runFrictionAdd(
161
149
  const measure = flag(input, "measure");
162
150
  const measureKind = flag(input, "measure-kind");
163
151
  const status = flag(input, "status") ?? "open";
152
+ const level = flag(input, "level");
164
153
  const artifactsRaw = flag(input, "artifacts");
165
154
  const artifacts =
166
155
  artifactsRaw === undefined
@@ -200,12 +189,17 @@ export async function runFrictionAdd(
200
189
  `status "${status}" is outside the closed catalog (${FRICTION_STATUSES.join(", ")})`,
201
190
  );
202
191
  }
192
+ // RFC-1639: --level marks records about the superfolder (folder) vs the
193
+ // repo holding the registry (repo, the default — the field is omitted).
194
+ if (level !== undefined && level !== "repo" && level !== "folder") {
195
+ return refuse("FRICTION-03", `level "${level}" is outside the closed catalog (repo, folder)`);
196
+ }
203
197
 
204
198
  // ── reserve the id on the shared surface — allocate+append under one
205
199
  // lock so two concurrent adds can never pick the same id (AC-2).
206
200
  const sources = await loadFrictionSources(workspaceRoot, fs);
207
201
  const worktreeMax = frictionSourceMax(sources);
208
- const session = flag(input, "session") ?? (await defaultSession(workspaceRoot));
202
+ const session = flag(input, "session") ?? (await defaultSession(workspaceRoot, fs));
209
203
  const allocation = await reserveNextGovernanceId(
210
204
  fs,
211
205
  workspaceRoot,
@@ -224,6 +218,7 @@ export async function runFrictionAdd(
224
218
  signature,
225
219
  measure,
226
220
  ...(measureKind !== undefined ? { measureKind: measureKind as FrictionMeasureKind } : {}),
221
+ ...(level === "folder" ? { level: "folder" as const } : {}),
227
222
  artifacts,
228
223
  status: status as FrictionStatus,
229
224
  recurrences: 0,
@@ -23,10 +23,14 @@ Idempotent: no legacy file and no qa-log.md files reports migrated: 0.</purpose>
23
23
  <item>RFC-1590: friction.migrate — legacy split + removal, qa-log.md
24
24
  shard moves under .agents/skills/, --dry-run reporting.</item>
25
25
  <item>RFC-1590: store append-only shared ledgers as one file per record</item>
26
+ <item>RFC-1639: step 3-4 — friction.migrate --from ingest, friction.add --level, CREATE-SUPERFOLDER-01 guard</item>
27
+ <item>RFC-1639: review — SYSTEM-MAP-01 rules decl, git-probe warnings, sibling kind/path discriminants, friction.add provenance, ingest hardening</item>
26
28
  </CHANGE_SUMMARY>
27
29
  */
28
30
 
29
- import { join } from "node:path";
31
+ import path, { join } from "node:path";
32
+
33
+ import { parse as yamlParse } from "yaml";
30
34
 
31
35
  import { ambientIo as fs } from "../../../src/utils/io.ts";
32
36
  import type {
@@ -36,12 +40,16 @@ import type {
36
40
  ForgeRuntimeContext,
37
41
  } from "../../../src/types.ts";
38
42
 
43
+ import { reserveNextGovernanceId } from "../../rfc/next-id.ts";
39
44
  import {
40
45
  FRICTION_DIR,
41
46
  LEGACY_FRICTION_FILE,
47
+ defaultSession,
48
+ frictionSourceMax,
42
49
  loadFrictionSources,
43
50
  writeFriction,
44
51
  } from "../store.ts";
52
+ import { FRICTION_ID_PATTERN } from "../types.ts";
45
53
  import type { FrictionEntry } from "../types.ts";
46
54
 
47
55
  export interface FrictionMigrateResult {
@@ -82,10 +90,25 @@ async function listQaLogFiles(root: string): Promise<string[]> {
82
90
  return found;
83
91
  }
84
92
 
93
+ function flag(input: ForgeCommandInput, name: string): string | undefined {
94
+ const v = input.flags[name];
95
+ if (typeof v === "string" && v.trim().length > 0) return v.trim();
96
+ if (typeof v === "number") return String(v);
97
+ return undefined;
98
+ }
99
+
85
100
  export async function runFrictionMigrate(
86
101
  input: ForgeCommandInput,
87
102
  context: ForgeRuntimeContext,
88
103
  ): Promise<ForgeCommandResult<FrictionMigrateResult>> {
104
+ // RFC-1639: --from ingests an EXTERNAL legacy frictions.yaml into this
105
+ // workspace's registry — the superfolder-level file lands inside the
106
+ // system hub at the next F-ids with migratedFrom+origin provenance.
107
+ const from = flag(input, "from");
108
+ if (from !== undefined) {
109
+ return runIngest(input, context, from);
110
+ }
111
+
89
112
  const { workspaceRoot, logger, outputFormat } = context;
90
113
  const dryRun = input.flags["dry-run"] === true;
91
114
  const files: string[] = [];
@@ -204,3 +227,199 @@ export async function runFrictionMigrate(
204
227
  exitCode: errors.length === 0 ? 0 : 1,
205
228
  };
206
229
  }
230
+
231
+ // ── --from ingest (RFC-1639) ─────────────────────────────────────────────
232
+
233
+ /**
234
+ * Ingest an external legacy frictions.yaml into this workspace's registry.
235
+ * Records mint at the next ids via the shared git-dir ledger (never
236
+ * dir-scan), carry migratedFrom + origin + level provenance, and the source
237
+ * file is removed last so a crash-interrupted re-run re-derives the same
238
+ * mapping and skips already-ingested entries.
239
+ */
240
+ async function runIngest(
241
+ input: ForgeCommandInput,
242
+ context: ForgeRuntimeContext,
243
+ fromRaw: string,
244
+ ): Promise<ForgeCommandResult<FrictionMigrateResult>> {
245
+ const { workspaceRoot, logger, outputFormat } = context;
246
+ const dryRun = input.flags["dry-run"] === true;
247
+ const files: string[] = [];
248
+ const skipped: Array<{ id: string | null; reason: string }> = [];
249
+ const errors: Diagnostic[] = [];
250
+ let migrated = 0;
251
+ let legacyRemoved = false;
252
+
253
+ const finish = (summary: string): ForgeCommandResult<FrictionMigrateResult> => ({
254
+ data: {
255
+ command: "friction.migrate",
256
+ status: errors.length === 0 ? "pass" : "fail",
257
+ dryRun,
258
+ migrated,
259
+ files,
260
+ legacyRemoved,
261
+ qaLogMoved: [],
262
+ qaLogMerged: [],
263
+ skipped,
264
+ errors,
265
+ },
266
+ summary,
267
+ exitCode: errors.length === 0 ? 0 : 1,
268
+ });
269
+ const refuse = (message: string): ForgeCommandResult<FrictionMigrateResult> => {
270
+ errors.push({ ruleId: "FRICTION-01", severity: "error", message, file: fromRaw });
271
+ return finish(`friction.migrate --from refused: ${message}`);
272
+ };
273
+
274
+ const resolved = path.isAbsolute(fromRaw)
275
+ ? path.normalize(fromRaw)
276
+ : path.resolve(workspaceRoot, fromRaw);
277
+ const rootNorm = path.resolve(workspaceRoot);
278
+ if (resolved === rootNorm || resolved.startsWith(rootNorm + path.sep)) {
279
+ return refuse(
280
+ "--from must point outside the workspace — the no-flag path owns docs/frictions.yaml",
281
+ );
282
+ }
283
+ if (!(await fs.exists(resolved))) {
284
+ return refuse(`source file not found: ${resolved}`);
285
+ }
286
+
287
+ // origin — the source registry's dir basename, unwrapping a trailing
288
+ // docs/ so <repo>/docs/frictions.yaml records origin as <repo>.
289
+ const parent = path.dirname(resolved);
290
+ const origin =
291
+ path.basename(parent) === "docs" ? path.basename(path.dirname(parent)) : path.basename(parent);
292
+ if (origin.length === 0) {
293
+ return refuse(
294
+ `cannot derive an origin name from ${resolved} — pass a file inside a named folder`,
295
+ );
296
+ }
297
+
298
+ const levelFlag = flag(input, "level");
299
+ if (levelFlag !== undefined && levelFlag !== "repo" && levelFlag !== "folder") {
300
+ errors.push({
301
+ ruleId: "FRICTION-03",
302
+ severity: "error",
303
+ message: `level "${levelFlag}" is outside the closed catalog (repo, folder)`,
304
+ file: fromRaw,
305
+ });
306
+ return finish(`friction.migrate --from refused: invalid --level ${levelFlag}`);
307
+ }
308
+
309
+ // Parse — accept both legacy shapes ({entries: [...]} and a bare array).
310
+ let rows: Record<string, unknown>[];
311
+ try {
312
+ const parsed = yamlParse(await fs.readFile(resolved)) as unknown;
313
+ const list = Array.isArray(parsed)
314
+ ? parsed
315
+ : parsed !== null &&
316
+ typeof parsed === "object" &&
317
+ Array.isArray((parsed as Record<string, unknown>)["entries"])
318
+ ? ((parsed as Record<string, unknown>)["entries"] as unknown[])
319
+ : null;
320
+ if (list === null) {
321
+ return refuse(`${resolved} is not a legacy frictions registry (no entries array)`);
322
+ }
323
+ rows = list as Record<string, unknown>[];
324
+ } catch (e) {
325
+ return refuse(
326
+ `${resolved} is not readable YAML: ${e instanceof Error ? e.message : String(e)}`,
327
+ );
328
+ }
329
+
330
+ // Every row must name an F-NNNN id — the run refuses before writing when
331
+ // one cannot be renumbered deterministically.
332
+ for (const [i, row] of rows.entries()) {
333
+ const id = row?.["id"];
334
+ if (typeof id !== "string" || !FRICTION_ID_PATTERN.test(id)) {
335
+ return refuse(
336
+ `source entry at index ${i} carries no readable F-NNNN id — repair the source registry before ingesting`,
337
+ );
338
+ }
339
+ }
340
+
341
+ const sources = await loadFrictionSources(workspaceRoot, fs);
342
+ const ingested = new Set(
343
+ sources.dir
344
+ .map((r) => r.raw as Record<string, unknown> | undefined)
345
+ .filter(
346
+ (r): r is Record<string, unknown> =>
347
+ r !== undefined && r["origin"] === origin && typeof r["migratedFrom"] === "string",
348
+ )
349
+ .map((r) => r["migratedFrom"] as string),
350
+ );
351
+
352
+ let max = frictionSourceMax(sources);
353
+ const session = flag(input, "session") ?? (await defaultSession(workspaceRoot, fs));
354
+ const level = levelFlag === "repo" ? ("repo" as const) : ("folder" as const);
355
+
356
+ for (const row of rows) {
357
+ const oldId = row["id"] as string;
358
+ if (ingested.has(oldId)) {
359
+ skipped.push({ id: oldId, reason: `already ingested (origin: ${origin})` });
360
+ continue;
361
+ }
362
+ if (dryRun) {
363
+ files.push(join(FRICTION_DIR, `F-${String(max + 1).padStart(4, "0")}.yaml`));
364
+ max += 1;
365
+ continue;
366
+ }
367
+ const allocation = await reserveNextGovernanceId(fs, workspaceRoot, "friction", max, session);
368
+ max = Math.max(max, allocation.id);
369
+ const id = `F-${String(allocation.id).padStart(4, "0")}`;
370
+ const file = join(FRICTION_DIR, `${id}.yaml`);
371
+ files.push(file);
372
+ const record = {
373
+ ...row,
374
+ id,
375
+ migratedFrom: oldId,
376
+ origin,
377
+ level,
378
+ } as FrictionEntry;
379
+ try {
380
+ await writeFriction(workspaceRoot, record, fs);
381
+ migrated++;
382
+ ingested.add(oldId);
383
+ for (const w of allocation.warnings) {
384
+ if (outputFormat === "pretty") logger.warn(` ${w}`);
385
+ }
386
+ } catch (e) {
387
+ errors.push({
388
+ ruleId: "FRICTION-01",
389
+ severity: "error",
390
+ message: e instanceof Error ? e.message : String(e),
391
+ file,
392
+ });
393
+ break;
394
+ }
395
+ }
396
+
397
+ // Source removal is LAST — a crash before this point re-runs cleanly.
398
+ if (!dryRun && errors.length === 0) {
399
+ try {
400
+ await fs.rm(resolved);
401
+ legacyRemoved = true;
402
+ } catch (e) {
403
+ errors.push({
404
+ ruleId: "FRICTION-01",
405
+ severity: "error",
406
+ message: `records written but the source could not be removed: ${e instanceof Error ? e.message : String(e)}`,
407
+ file: resolved,
408
+ });
409
+ }
410
+ }
411
+
412
+ if (outputFormat === "pretty") {
413
+ logger.section("friction.migrate --from");
414
+ logger.info(
415
+ ` ${resolved} → ${files.length} record(s) under ${FRICTION_DIR}/ (origin: ${origin})` +
416
+ (dryRun ? " (dry-run)" : `, source ${legacyRemoved ? "removed" : "kept"}`),
417
+ );
418
+ for (const s of skipped) logger.info(` skipped ${s.id}: ${s.reason}`);
419
+ for (const e of errors) logger.error(` ${e.ruleId}: ${e.message}`);
420
+ }
421
+
422
+ return finish(
423
+ `friction.migrate --from: ${migrated} record(s) ingested from ${origin}${skipped.length > 0 ? `, ${skipped.length} skipped` : ""}${dryRun ? " (dry-run)" : ""}`,
424
+ );
425
+ }
@@ -21,6 +21,8 @@ registry goes through this module.</purpose>
21
21
  layout, legacy-fallback loader, duplicate-id error, writeFriction and
22
22
  updateFriction mutation surface.</item>
23
23
  <item>RFC-1590: store append-only shared ledgers as one file per record</item>
24
+ <item>RFC-1639: step 3-4 — friction.migrate --from ingest, friction.add --level, CREATE-SUPERFOLDER-01 guard</item>
25
+ <item>RFC-1639: review — SYSTEM-MAP-01 rules decl, git-probe warnings, sibling kind/path discriminants, friction.add provenance, ingest hardening</item>
24
26
  </CHANGE_SUMMARY>
25
27
  */
26
28
 
@@ -35,6 +37,23 @@ import type { FrictionEntry } from "./types.ts";
35
37
  export const FRICTION_DIR = "docs/frictions";
36
38
  export const LEGACY_FRICTION_FILE = "docs/frictions.yaml";
37
39
 
40
+ /** Session attribution default — the current branch name; detached or
41
+ * non-git invocations record "manual". Shared by friction.add and the
42
+ * friction.migrate --from ingest (RFC-1639). */
43
+ export async function defaultSession(cwd: string, io: WorkspaceIO): Promise<string> {
44
+ try {
45
+ // Lazy import — keeps the store leaf-shaped for consumers that never
46
+ // mint ids (friction-validate) and never needed the flow/git surface.
47
+ const { git } = await import("../flow/git.ts");
48
+ const r = await git(cwd, io, ["branch", "--show-current"]);
49
+ const name = r.stdout.trim();
50
+ if (r.exitCode === 0 && name.length > 0) return name;
51
+ } catch {
52
+ /* non-git cwd — manual attribution */
53
+ }
54
+ return "manual";
55
+ }
56
+
38
57
  /** `F-NNNN.yaml` file-name shape (prefix match — suffixes stay legal). */
39
58
  export const FRICTION_FILE_PATTERN = /^F-(\d{4})/;
40
59