@narumitw/pi-sync 0.70.6 → 0.71.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 (734) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +34 -105
  3. package/dist/chunks/agent-plan-A6CS3XXA.ts +337 -0
  4. package/dist/chunks/agent-plan-A6CS3XXA.ts.map +7 -0
  5. package/dist/chunks/chunk-2BMTS5OW.ts +229 -0
  6. package/dist/chunks/chunk-2BMTS5OW.ts.map +7 -0
  7. package/dist/chunks/chunk-2G6GTIWW.ts +188 -0
  8. package/dist/chunks/chunk-2G6GTIWW.ts.map +7 -0
  9. package/dist/chunks/chunk-2YHXXWUF.ts +18 -0
  10. package/dist/chunks/chunk-2YHXXWUF.ts.map +7 -0
  11. package/dist/chunks/chunk-3GL7LBNB.ts +3 -0
  12. package/dist/chunks/chunk-3M3KQFSQ.ts +215 -0
  13. package/dist/chunks/chunk-3M3KQFSQ.ts.map +7 -0
  14. package/dist/chunks/chunk-3PFWVSV3.ts +3 -0
  15. package/dist/chunks/chunk-4S5CGN4M.ts +54 -0
  16. package/dist/chunks/chunk-4S5CGN4M.ts.map +7 -0
  17. package/dist/chunks/{chunk-H7VOTY5N.ts → chunk-53AFFVTT.ts} +2 -24
  18. package/dist/chunks/{chunk-H7VOTY5N.ts.map → chunk-53AFFVTT.ts.map} +1 -1
  19. package/dist/chunks/chunk-53IKSWLZ.ts +3 -0
  20. package/dist/chunks/{chunk-ZWP6HAYV.ts → chunk-5ERTMRNE.ts} +1 -1
  21. package/dist/chunks/chunk-5ERTMRNE.ts.map +7 -0
  22. package/dist/chunks/chunk-5WUWHDCJ.ts +12 -0
  23. package/dist/chunks/chunk-5WUWHDCJ.ts.map +7 -0
  24. package/dist/chunks/{chunk-WRS3SUNZ.ts → chunk-6VGQ6THO.ts} +8 -10
  25. package/dist/chunks/{chunk-WRS3SUNZ.ts.map → chunk-6VGQ6THO.ts.map} +1 -1
  26. package/dist/chunks/chunk-6WVJIIXQ.ts +154 -0
  27. package/dist/chunks/chunk-6WVJIIXQ.ts.map +7 -0
  28. package/dist/chunks/chunk-A3NEEVNA.ts +3 -0
  29. package/dist/chunks/chunk-A7KJUFLX.ts +2120 -0
  30. package/dist/chunks/chunk-A7KJUFLX.ts.map +7 -0
  31. package/dist/chunks/{chunk-4A34OGXY.ts → chunk-AA2G3HTW.ts} +12 -16
  32. package/dist/chunks/{chunk-4A34OGXY.ts.map → chunk-AA2G3HTW.ts.map} +1 -1
  33. package/dist/chunks/chunk-AITZUACB.ts +3 -0
  34. package/dist/chunks/chunk-AVLYLWA7.ts +1414 -0
  35. package/dist/chunks/chunk-AVLYLWA7.ts.map +7 -0
  36. package/dist/chunks/chunk-B4JNUYZ2.ts +12 -0
  37. package/dist/chunks/chunk-B4JNUYZ2.ts.map +7 -0
  38. package/dist/chunks/chunk-BEO3THWZ.ts +3 -0
  39. package/dist/chunks/chunk-BEO3THWZ.ts.map +7 -0
  40. package/dist/chunks/chunk-C2YD64T6.ts +167 -0
  41. package/dist/chunks/chunk-C2YD64T6.ts.map +7 -0
  42. package/dist/chunks/chunk-C66HLRRZ.ts +72 -0
  43. package/dist/chunks/chunk-C66HLRRZ.ts.map +7 -0
  44. package/dist/chunks/chunk-CFXAA65U.ts +53 -0
  45. package/dist/chunks/chunk-CFXAA65U.ts.map +7 -0
  46. package/dist/chunks/chunk-D34Y5JSL.ts +152 -0
  47. package/dist/chunks/chunk-D34Y5JSL.ts.map +7 -0
  48. package/dist/chunks/chunk-D3DMJSFU.ts +125 -0
  49. package/dist/chunks/chunk-D3DMJSFU.ts.map +7 -0
  50. package/dist/chunks/chunk-DFC3WJP5.ts +3 -0
  51. package/dist/chunks/chunk-DFC3WJP5.ts.map +7 -0
  52. package/dist/chunks/chunk-DH52CVRD.ts +2499 -0
  53. package/dist/chunks/chunk-DH52CVRD.ts.map +7 -0
  54. package/dist/chunks/chunk-DLA5RPFP.ts +50 -0
  55. package/dist/chunks/chunk-DLA5RPFP.ts.map +7 -0
  56. package/dist/chunks/chunk-ES4FIIHZ.ts +50 -0
  57. package/dist/chunks/chunk-ES4FIIHZ.ts.map +7 -0
  58. package/dist/chunks/chunk-ESUMO4TW.ts +55 -0
  59. package/dist/chunks/chunk-ESUMO4TW.ts.map +7 -0
  60. package/dist/chunks/chunk-FIEW5453.ts +77 -0
  61. package/dist/chunks/chunk-FIEW5453.ts.map +7 -0
  62. package/dist/chunks/chunk-FT45DX6E.ts +163 -0
  63. package/dist/chunks/chunk-FT45DX6E.ts.map +7 -0
  64. package/dist/chunks/chunk-G7U6BPP4.ts +137 -0
  65. package/dist/chunks/chunk-G7U6BPP4.ts.map +7 -0
  66. package/dist/chunks/{chunk-5BE6LBDC.ts → chunk-GC5XDUVA.ts} +25 -34
  67. package/dist/chunks/{chunk-5BE6LBDC.ts.map → chunk-GC5XDUVA.ts.map} +3 -3
  68. package/dist/chunks/{chunk-VERMBZGL.ts → chunk-GDA35HWZ.ts} +12 -14
  69. package/dist/chunks/{chunk-VERMBZGL.ts.map → chunk-GDA35HWZ.ts.map} +1 -1
  70. package/dist/chunks/chunk-HOFEC5FH.ts +54 -0
  71. package/dist/chunks/chunk-HOFEC5FH.ts.map +7 -0
  72. package/dist/chunks/{chunk-WE3KJ7JS.ts → chunk-IFJXUHHC.ts} +7 -7
  73. package/dist/chunks/chunk-INUH2UXN.ts +53 -0
  74. package/dist/chunks/chunk-INUH2UXN.ts.map +7 -0
  75. package/dist/chunks/chunk-JPLT2DG6.ts +122 -0
  76. package/dist/chunks/chunk-JPLT2DG6.ts.map +7 -0
  77. package/dist/chunks/chunk-KDTDGAOM.ts +76 -0
  78. package/dist/chunks/chunk-KDTDGAOM.ts.map +7 -0
  79. package/dist/chunks/chunk-LBDUDSHD.ts +3 -0
  80. package/dist/chunks/chunk-LBDUDSHD.ts.map +7 -0
  81. package/dist/chunks/{chunk-VQTYTUBU.ts → chunk-LHQIQIWL.ts} +4 -22
  82. package/dist/chunks/chunk-LHQIQIWL.ts.map +7 -0
  83. package/dist/chunks/chunk-MHERAG42.ts +150 -0
  84. package/dist/chunks/chunk-MHERAG42.ts.map +7 -0
  85. package/dist/chunks/chunk-MS6HLJFR.ts +61 -0
  86. package/dist/chunks/chunk-MS6HLJFR.ts.map +7 -0
  87. package/dist/chunks/{chunk-AXN3B5KC.ts → chunk-MTUR4H7S.ts} +31 -41
  88. package/dist/chunks/chunk-MTUR4H7S.ts.map +7 -0
  89. package/dist/chunks/chunk-NFW5HLKA.ts +84 -0
  90. package/dist/chunks/chunk-NFW5HLKA.ts.map +7 -0
  91. package/dist/chunks/chunk-NFZMKZ2D.ts +345 -0
  92. package/dist/chunks/chunk-NFZMKZ2D.ts.map +7 -0
  93. package/dist/chunks/chunk-OTI4XVCS.ts +618 -0
  94. package/dist/chunks/chunk-OTI4XVCS.ts.map +7 -0
  95. package/dist/chunks/chunk-QIL27OLJ.ts +28 -0
  96. package/dist/chunks/chunk-QIL27OLJ.ts.map +7 -0
  97. package/dist/chunks/chunk-QJTU5SCJ.ts +27 -0
  98. package/dist/chunks/chunk-QJTU5SCJ.ts.map +7 -0
  99. package/dist/chunks/chunk-QNU4RFCN.ts +225 -0
  100. package/dist/chunks/chunk-QNU4RFCN.ts.map +7 -0
  101. package/dist/chunks/chunk-QRSESWT2.ts +26 -0
  102. package/dist/chunks/chunk-QRSESWT2.ts.map +7 -0
  103. package/dist/chunks/chunk-RFJWUIV5.ts +1622 -0
  104. package/dist/chunks/chunk-RFJWUIV5.ts.map +7 -0
  105. package/dist/chunks/chunk-RRFW2UAD.ts +347 -0
  106. package/dist/chunks/chunk-RRFW2UAD.ts.map +7 -0
  107. package/dist/chunks/chunk-SD3D6BTF.ts +3 -0
  108. package/dist/chunks/chunk-SD3D6BTF.ts.map +7 -0
  109. package/dist/chunks/chunk-SPRHJBJC.ts +17 -0
  110. package/dist/chunks/chunk-SPRHJBJC.ts.map +7 -0
  111. package/dist/chunks/chunk-T22OVZSI.ts +347 -0
  112. package/dist/chunks/chunk-T22OVZSI.ts.map +7 -0
  113. package/dist/chunks/chunk-TAOYC2YG.ts +113 -0
  114. package/dist/chunks/chunk-TAOYC2YG.ts.map +7 -0
  115. package/dist/chunks/chunk-U6CORBHU.ts +205 -0
  116. package/dist/chunks/chunk-U6CORBHU.ts.map +7 -0
  117. package/dist/chunks/chunk-ULNEZ53E.ts +105 -0
  118. package/dist/chunks/chunk-ULNEZ53E.ts.map +7 -0
  119. package/dist/chunks/chunk-UPP54ATP.ts +3 -0
  120. package/dist/chunks/chunk-UPP54ATP.ts.map +7 -0
  121. package/dist/chunks/chunk-V66NV2HZ.ts +70 -0
  122. package/dist/chunks/chunk-V66NV2HZ.ts.map +7 -0
  123. package/dist/chunks/chunk-W3YS4Y2Q.ts +545 -0
  124. package/dist/chunks/chunk-W3YS4Y2Q.ts.map +7 -0
  125. package/dist/chunks/chunk-WETDHZAD.ts +815 -0
  126. package/dist/chunks/chunk-WETDHZAD.ts.map +7 -0
  127. package/dist/chunks/chunk-WT2722L4.ts +61 -0
  128. package/dist/chunks/chunk-WT2722L4.ts.map +7 -0
  129. package/dist/chunks/chunk-XHJ3AVIT.ts +12 -0
  130. package/dist/chunks/chunk-XHJ3AVIT.ts.map +7 -0
  131. package/dist/chunks/{chunk-HFMGK6A6.ts → chunk-YLVRV6NM.ts} +5 -5
  132. package/dist/chunks/chunk-YV776ZPN.ts +49 -0
  133. package/dist/chunks/chunk-YV776ZPN.ts.map +7 -0
  134. package/dist/chunks/chunk-YVHXQZIH.ts +411 -0
  135. package/dist/chunks/chunk-YVHXQZIH.ts.map +7 -0
  136. package/dist/chunks/chunk-ZWJMVLQ3.ts +3 -0
  137. package/dist/chunks/chunk-ZWJMVLQ3.ts.map +7 -0
  138. package/dist/chunks/chunk-ZWOKNJDT.ts +25 -0
  139. package/dist/chunks/chunk-ZWOKNJDT.ts.map +7 -0
  140. package/dist/chunks/{command-handler-GERC7LOE.ts → command-handler-SMARANCT.ts} +128 -155
  141. package/dist/chunks/command-handler-SMARANCT.ts.map +7 -0
  142. package/dist/chunks/{chunk-LNZRNXNQ.ts → config-PUZS2ZBP.ts} +15 -6
  143. package/dist/chunks/config-PUZS2ZBP.ts.map +7 -0
  144. package/dist/chunks/conflict-artifacts-77PI6IXK.ts +33 -0
  145. package/dist/chunks/conflict-artifacts-77PI6IXK.ts.map +7 -0
  146. package/dist/chunks/conflict-review-HHGVL5TP.ts +188 -0
  147. package/dist/chunks/conflict-review-HHGVL5TP.ts.map +7 -0
  148. package/dist/chunks/conflict-review-OO57TGMR.ts +40 -0
  149. package/dist/chunks/conflict-review-OO57TGMR.ts.map +7 -0
  150. package/dist/chunks/continuous-record-QIIFQNKP.ts +277 -0
  151. package/dist/chunks/continuous-record-QIIFQNKP.ts.map +7 -0
  152. package/dist/chunks/debug-check-ETFFIDW5.ts +14 -0
  153. package/dist/chunks/debug-check-ETFFIDW5.ts.map +7 -0
  154. package/dist/chunks/debug-log-QGBO6XHF.ts +17 -0
  155. package/dist/chunks/debug-log-QGBO6XHF.ts.map +7 -0
  156. package/dist/chunks/debug-tool-execution-AIQAGPMJ.ts +95 -0
  157. package/dist/chunks/debug-tool-execution-AIQAGPMJ.ts.map +7 -0
  158. package/dist/chunks/{diff-review-GB4NO53B.ts → diff-review-QRHOHG5C.ts} +12 -5
  159. package/dist/chunks/diff-review-QRHOHG5C.ts.map +7 -0
  160. package/dist/chunks/{file-selection-DGC5UJVI.ts → file-selection-ZEBPDRHG.ts} +33 -16
  161. package/dist/chunks/{file-selection-DGC5UJVI.ts.map → file-selection-ZEBPDRHG.ts.map} +1 -1
  162. package/dist/chunks/git-backend-I2IJ5ULJ.ts +1151 -0
  163. package/dist/chunks/git-backend-I2IJ5ULJ.ts.map +7 -0
  164. package/dist/chunks/initialization-XGYWRNPQ.ts +106 -0
  165. package/dist/chunks/initialization-XGYWRNPQ.ts.map +7 -0
  166. package/dist/chunks/local-diff-review-OGZM3TA7.ts +525 -0
  167. package/dist/chunks/local-diff-review-OGZM3TA7.ts.map +7 -0
  168. package/dist/chunks/local-fields-PIKOEXWR.ts +31 -0
  169. package/dist/chunks/local-fields-PIKOEXWR.ts.map +7 -0
  170. package/dist/chunks/manager-result-dispatcher-72YGSLHL.ts +42 -0
  171. package/dist/chunks/manager-result-dispatcher-72YGSLHL.ts.map +7 -0
  172. package/dist/chunks/{manager-ui-3CQU37XY.ts → manager-ui-5SXPS4SE.ts} +130 -92
  173. package/dist/chunks/manager-ui-5SXPS4SE.ts.map +7 -0
  174. package/dist/chunks/merge-baseline-store-MI7SHGDH.ts +26 -0
  175. package/dist/chunks/merge-baseline-store-MI7SHGDH.ts.map +7 -0
  176. package/dist/chunks/merge-journal-46QC5N7D.ts +38 -0
  177. package/dist/chunks/merge-journal-46QC5N7D.ts.map +7 -0
  178. package/dist/chunks/{merge-review-DXSERTCK.ts → merge-review-RMDYVNBK.ts} +2 -2
  179. package/dist/chunks/remote-selection-ui-4BXJAZUD.ts +41 -0
  180. package/dist/chunks/remote-selection-ui-4BXJAZUD.ts.map +7 -0
  181. package/dist/chunks/s3-backend-ZRMOWH5R.ts +866 -0
  182. package/dist/chunks/s3-backend-ZRMOWH5R.ts.map +7 -0
  183. package/dist/chunks/session-push-55IV4IW3.ts +101 -0
  184. package/dist/chunks/session-push-55IV4IW3.ts.map +7 -0
  185. package/dist/chunks/setup-switch-DXDUBDQI.ts +43 -0
  186. package/dist/chunks/setup-switch-DXDUBDQI.ts.map +7 -0
  187. package/dist/chunks/{setup-wizard-FRIR2EOR.ts → setup-wizard-ICPQERAE.ts} +34 -18
  188. package/dist/chunks/{setup-wizard-FRIR2EOR.ts.map → setup-wizard-ICPQERAE.ts.map} +1 -1
  189. package/dist/chunks/{snapshot-ZUJJZCFW.ts → snapshot-HDARHOQZ.ts} +14 -8
  190. package/dist/chunks/snapshot-HDARHOQZ.ts.map +7 -0
  191. package/dist/chunks/snapshot-LZ6XKFA4.ts +63 -0
  192. package/dist/chunks/snapshot-LZ6XKFA4.ts.map +7 -0
  193. package/dist/chunks/snapshot-transaction-LYR5OFLR.ts +27 -0
  194. package/dist/chunks/snapshot-transaction-LYR5OFLR.ts.map +7 -0
  195. package/dist/chunks/sync-inspection-N6TELULU.ts +29 -0
  196. package/dist/chunks/sync-inspection-N6TELULU.ts.map +7 -0
  197. package/dist/chunks/sync-inspection-PMEXXLDK.ts +26 -0
  198. package/dist/chunks/sync-inspection-PMEXXLDK.ts.map +7 -0
  199. package/dist/chunks/sync-operations-KKSIS2CI.ts +69 -0
  200. package/dist/chunks/sync-operations-KKSIS2CI.ts.map +7 -0
  201. package/dist/chunks/sync-operations-VSMA5GSA.ts +47 -0
  202. package/dist/chunks/sync-operations-VSMA5GSA.ts.map +7 -0
  203. package/dist/chunks/{sync-state-QIT64TKV.ts → sync-state-72ZDKUQW.ts} +13 -5
  204. package/dist/chunks/sync-state-72ZDKUQW.ts.map +7 -0
  205. package/dist/chunks/{sync-state-store-TZE7XDV5.ts → sync-state-store-YTMAZQ3Z.ts} +8 -2
  206. package/dist/chunks/sync-state-store-YTMAZQ3Z.ts.map +7 -0
  207. package/dist/chunks/webdav-backend-3AZOECVQ.ts +948 -0
  208. package/dist/chunks/webdav-backend-3AZOECVQ.ts.map +7 -0
  209. package/dist/index.ts +286 -421
  210. package/dist/index.ts.map +4 -4
  211. package/docs/agent-continuous-hardening.md +3 -1
  212. package/docs/agent-quality-verification.md +3 -1
  213. package/docs/continuous-sync-verification.md +1 -1
  214. package/docs/daemon-migration.md +3 -1
  215. package/docs/daemon-retirement.md +15 -0
  216. package/docs/merge-implementation-audit.md +2 -0
  217. package/docs/settings-merge-audit.md +1 -1
  218. package/docs/tui-contract.md +12 -14
  219. package/package.json +1 -3
  220. package/skills/configure-pi-sync/SKILL.md +14 -60
  221. package/skills/configure-pi-sync/references/configuration.md +38 -46
  222. package/src/backends/backend-factory.ts +2 -2
  223. package/src/backends/backend-identity.ts +2 -2
  224. package/src/backends/backend-types.ts +1 -1
  225. package/src/backends/git/git-backend.ts +2 -2
  226. package/src/backends/git/git-config.ts +2 -2
  227. package/src/backends/git/git-runner.ts +2 -2
  228. package/src/backends/git/git-storage.ts +2 -2
  229. package/src/backends/s3/s3-backend.ts +2 -2
  230. package/src/backends/s3/s3-client.ts +2 -2
  231. package/src/backends/s3/s3-conditions.ts +2 -2
  232. package/src/backends/snapshot-pointer.ts +2 -2
  233. package/src/backends/sync-backend.ts +2 -2
  234. package/src/backends/webdav/webdav-backend.ts +2 -2
  235. package/src/backends/webdav/webdav-client.ts +2 -2
  236. package/src/backends/webdav/webdav-config.ts +2 -2
  237. package/src/commands/command-execution.ts +66 -62
  238. package/src/commands/command-handler.ts +3 -10
  239. package/src/commands/command-types.ts +1 -0
  240. package/src/commands/command.ts +3 -24
  241. package/src/engine/backends/backend-factory.ts +23 -0
  242. package/src/engine/backends/backend-identity.ts +40 -0
  243. package/src/engine/backends/backend-types.ts +75 -0
  244. package/src/engine/backends/git/git-backend.ts +863 -0
  245. package/src/engine/backends/git/git-config.ts +130 -0
  246. package/src/engine/backends/git/git-runner.ts +260 -0
  247. package/src/engine/backends/git/git-storage.ts +295 -0
  248. package/src/engine/backends/publication-encoding.ts +19 -0
  249. package/src/engine/backends/s3/s3-backend.ts +528 -0
  250. package/src/engine/backends/s3/s3-client.ts +398 -0
  251. package/src/engine/backends/s3/s3-conditions.ts +64 -0
  252. package/src/engine/backends/snapshot-pointer.ts +26 -0
  253. package/src/engine/backends/sync-backend.ts +133 -0
  254. package/src/engine/backends/webdav/webdav-backend.ts +691 -0
  255. package/src/engine/backends/webdav/webdav-client.ts +390 -0
  256. package/src/engine/backends/webdav/webdav-config.ts +82 -0
  257. package/src/engine/commands/command-types.ts +37 -0
  258. package/src/engine/debug-check.ts +83 -0
  259. package/src/engine/debug-log.ts +272 -0
  260. package/src/engine/diff-review-types.ts +16 -0
  261. package/src/engine/foreground-transfer.ts +15 -0
  262. package/src/engine/host.ts +95 -0
  263. package/src/engine/paths.ts +90 -0
  264. package/src/engine/record.ts +3 -0
  265. package/src/engine/settings/config-errors.ts +6 -0
  266. package/src/engine/settings/config-file.ts +907 -0
  267. package/src/engine/settings/config.ts +315 -0
  268. package/src/engine/settings/retired-ignore-policy.ts +15 -0
  269. package/src/engine/settings/retired-ignore-recovery.ts +18 -0
  270. package/src/engine/settings/settings-management.ts +261 -0
  271. package/src/engine/settings/settings-store.ts +48 -0
  272. package/src/engine/settings/settings-types.ts +164 -0
  273. package/src/engine/settings/settings-validation.ts +472 -0
  274. package/src/engine/snapshot/session-paths.ts +179 -0
  275. package/src/engine/snapshot/snapshot-apply.ts +443 -0
  276. package/src/engine/snapshot/snapshot-bounds.ts +23 -0
  277. package/src/engine/snapshot/snapshot-capture-limit.ts +110 -0
  278. package/src/engine/snapshot/snapshot-case-replacement.ts +77 -0
  279. package/src/engine/snapshot/snapshot-codec.ts +69 -0
  280. package/src/engine/snapshot/snapshot-difference.ts +25 -0
  281. package/src/engine/snapshot/snapshot-entry-projection.ts +101 -0
  282. package/src/engine/snapshot/snapshot-exclude-safety.ts +203 -0
  283. package/src/engine/snapshot/snapshot-mutation-queues.ts +152 -0
  284. package/src/engine/snapshot/snapshot-paths.ts +11 -0
  285. package/src/engine/snapshot/snapshot-payload.ts +50 -0
  286. package/src/engine/snapshot/snapshot-reviewed-preimages.ts +68 -0
  287. package/src/engine/snapshot/snapshot-root-transition.ts +170 -0
  288. package/src/engine/snapshot/snapshot-transaction-directory.ts +16 -0
  289. package/src/engine/snapshot/snapshot-transaction-images.ts +424 -0
  290. package/src/engine/snapshot/snapshot-transaction-plan.ts +69 -0
  291. package/src/engine/snapshot/snapshot-transaction-policy.ts +278 -0
  292. package/src/engine/snapshot/snapshot-transaction-stage.ts +342 -0
  293. package/src/engine/snapshot/snapshot-transaction-verification.ts +109 -0
  294. package/src/engine/snapshot/snapshot-transaction.ts +961 -0
  295. package/src/engine/snapshot/snapshot-types.ts +67 -0
  296. package/src/engine/snapshot/snapshot.ts +847 -0
  297. package/src/engine/state/json-file.ts +46 -0
  298. package/src/engine/state/lock.ts +421 -0
  299. package/src/engine/state/lockfile-fs.ts +25 -0
  300. package/src/engine/state/merge-baseline-store.ts +213 -0
  301. package/src/engine/state/mutation-directory-sync.ts +33 -0
  302. package/src/engine/state/operation-availability.ts +59 -0
  303. package/src/engine/state/state-directory.ts +245 -0
  304. package/src/engine/state/state-types.ts +26 -0
  305. package/src/engine/state/sync-state-store.ts +77 -0
  306. package/src/engine/sync/agent-candidate.ts +252 -0
  307. package/src/engine/sync/agent-plan.ts +114 -0
  308. package/{dist/daemon/dist/engine/sync/agent-policy.js → src/engine/sync/agent-policy.ts} +5 -3
  309. package/src/engine/sync/agent-refusal-retry.ts +53 -0
  310. package/src/engine/sync/agent-request.ts +17 -0
  311. package/src/engine/sync/agent-runner.ts +208 -0
  312. package/src/engine/sync/agent-store.ts +175 -0
  313. package/src/engine/sync/agent-sync.ts +145 -0
  314. package/src/engine/sync/apply-review.ts +63 -0
  315. package/src/engine/sync/conflict-artifact-ownership.ts +58 -0
  316. package/src/engine/sync/conflict-artifacts.ts +193 -0
  317. package/src/engine/sync/conflict-groups.ts +67 -0
  318. package/src/engine/sync/conflict-preview.ts +54 -0
  319. package/src/engine/sync/conflict-resolution.ts +93 -0
  320. package/src/engine/sync/conflict-retention.ts +71 -0
  321. package/src/engine/sync/conflict-review.ts +95 -0
  322. package/src/engine/sync/content-conflicts.ts +73 -0
  323. package/src/engine/sync/continuous-errors.ts +91 -0
  324. package/src/engine/sync/continuous-lease.ts +213 -0
  325. package/src/engine/sync/continuous-record.ts +255 -0
  326. package/src/engine/sync/continuous-storage.ts +63 -0
  327. package/src/engine/sync/continuous-sync.ts +646 -0
  328. package/src/engine/sync/continuous-watch.ts +123 -0
  329. package/src/engine/sync/diff-review-capture.ts +259 -0
  330. package/src/engine/sync/diff-review-preview.ts +111 -0
  331. package/src/engine/sync/diff-review-store.ts +72 -0
  332. package/src/engine/sync/directional-action.ts +169 -0
  333. package/src/engine/sync/directional-conflict.ts +53 -0
  334. package/src/engine/sync/exclude-policy.ts +88 -0
  335. package/src/engine/sync/field-migration.ts +39 -0
  336. package/src/engine/sync/file-merge-planner.ts +138 -0
  337. package/src/engine/sync/initialization.ts +81 -0
  338. package/src/engine/sync/json-document.ts +179 -0
  339. package/src/engine/sync/local-field-policy.ts +28 -0
  340. package/src/engine/sync/local-fields.ts +171 -0
  341. package/src/engine/sync/merge-acceptance.ts +56 -0
  342. package/src/engine/sync/merge-apply.ts +585 -0
  343. package/src/engine/sync/merge-journal.ts +259 -0
  344. package/src/engine/sync/merge-transfer-error.ts +68 -0
  345. package/src/engine/sync/merged-sync.ts +912 -0
  346. package/src/engine/sync/mutation-owner.ts +22 -0
  347. package/src/engine/sync/package-merge.ts +81 -0
  348. package/src/engine/sync/partial-progress.ts +56 -0
  349. package/src/engine/sync/pull-outcome.ts +16 -0
  350. package/src/engine/sync/push-review.ts +47 -0
  351. package/src/engine/sync/remote-snapshot.ts +185 -0
  352. package/src/engine/sync/resolver-stream.ts +34 -0
  353. package/src/engine/sync/session-merge.ts +192 -0
  354. package/src/engine/sync/session-push.ts +68 -0
  355. package/src/engine/sync/settings-conflicts.ts +64 -0
  356. package/src/engine/sync/settings-merge.ts +118 -0
  357. package/src/engine/sync/signals.ts +8 -0
  358. package/src/engine/sync/startup-recovery-evidence.ts +37 -0
  359. package/src/engine/sync/startup-timings.ts +135 -0
  360. package/src/engine/sync/sync-decision.ts +134 -0
  361. package/src/engine/sync/sync-error-guidance.ts +56 -0
  362. package/src/engine/sync/sync-errors.ts +50 -0
  363. package/src/engine/sync/sync-inspection.ts +61 -0
  364. package/src/engine/sync/sync-loaders.ts +14 -0
  365. package/src/engine/sync/sync-local.ts +117 -0
  366. package/src/engine/sync/sync-mutations.ts +940 -0
  367. package/src/engine/sync/sync-operations.ts +10 -0
  368. package/src/engine/sync/sync-policy.ts +695 -0
  369. package/src/engine/sync/sync-queries.ts +404 -0
  370. package/src/engine/sync/sync-rollback.ts +245 -0
  371. package/src/engine/sync/sync-state.ts +197 -0
  372. package/src/engine/sync/text-merge.ts +132 -0
  373. package/src/engine/sync/transfer-freshness.ts +151 -0
  374. package/src/engine/sync/transfer-size-limits.ts +100 -0
  375. package/src/engine/types.ts +45 -0
  376. package/src/engine/ui/merge-review.ts +14 -0
  377. package/src/engine/ui/merged-sync-summary.ts +111 -0
  378. package/src/engine/ui/sync-format.ts +222 -0
  379. package/src/engine/ui/sync-status.ts +29 -0
  380. package/src/engine/ui/terminal-text.ts +9 -0
  381. package/src/engine/ui/transfer-changes.ts +32 -0
  382. package/src/engine/ui/transfer-review.ts +28 -0
  383. package/src/paths.ts +2 -2
  384. package/src/runtime/identity.ts +152 -0
  385. package/src/runtime/legacy-owner.ts +223 -0
  386. package/src/runtime/lifecycle.ts +17 -0
  387. package/src/runtime/local-host.ts +157 -0
  388. package/src/{daemon/session-presence.ts → runtime/session-presence-binding.ts} +1 -2
  389. package/src/runtime/session-presence.ts +258 -0
  390. package/src/settings/config-errors.ts +2 -2
  391. package/src/settings/config-file.ts +2 -2
  392. package/src/settings/config.ts +2 -2
  393. package/src/settings/settings-management.ts +2 -2
  394. package/src/settings/settings-store.ts +2 -2
  395. package/src/settings/settings-types.ts +1 -1
  396. package/src/settings/settings-validation.ts +2 -2
  397. package/src/snapshot/session-paths.ts +2 -2
  398. package/src/snapshot/snapshot-apply.ts +2 -2
  399. package/src/snapshot/snapshot-case-replacement.ts +2 -2
  400. package/src/snapshot/snapshot-codec.ts +2 -2
  401. package/src/snapshot/snapshot-exclude-safety.ts +2 -2
  402. package/src/snapshot/snapshot-mutation-queues.ts +2 -2
  403. package/src/snapshot/snapshot-paths.ts +2 -2
  404. package/src/snapshot/snapshot-root-transition.ts +2 -2
  405. package/src/snapshot/snapshot-transaction-directory.ts +2 -2
  406. package/src/snapshot/snapshot-transaction-plan.ts +2 -2
  407. package/src/snapshot/snapshot-transaction.ts +2 -2
  408. package/src/snapshot/snapshot-types.ts +1 -1
  409. package/src/snapshot/snapshot.ts +2 -2
  410. package/src/state/json-file.ts +2 -2
  411. package/src/state/lock-policy.ts +2 -2
  412. package/src/state/lock.ts +2 -2
  413. package/src/state/lockfile-fs.ts +2 -2
  414. package/src/state/merge-baseline-store.ts +2 -2
  415. package/src/state/mutation-directory-sync.ts +2 -2
  416. package/src/state/operation-availability.ts +2 -2
  417. package/src/state/state-directory.ts +2 -2
  418. package/src/state/state-types.ts +1 -1
  419. package/src/state/sync-state-store.ts +2 -2
  420. package/src/sync/agent-candidate.ts +2 -2
  421. package/src/sync/agent-plan.ts +2 -2
  422. package/src/sync/agent-policy.ts +2 -2
  423. package/src/sync/agent-refusal-retry.ts +2 -2
  424. package/src/sync/agent-request.ts +2 -2
  425. package/src/sync/agent-runner.ts +2 -2
  426. package/src/sync/agent-store.ts +2 -2
  427. package/src/sync/agent-sync.ts +2 -2
  428. package/src/sync/apply-review.ts +2 -2
  429. package/src/sync/automatic-sync.ts +58 -120
  430. package/src/sync/conflict-artifact-ownership.ts +2 -2
  431. package/src/sync/conflict-artifacts.ts +2 -2
  432. package/src/sync/conflict-groups.ts +2 -2
  433. package/src/sync/conflict-preview.ts +2 -2
  434. package/src/sync/conflict-resolution.ts +2 -2
  435. package/src/sync/conflict-retention.ts +2 -2
  436. package/src/sync/conflict-review.ts +5 -2
  437. package/src/sync/content-conflicts.ts +2 -2
  438. package/src/sync/continuous-errors.ts +2 -2
  439. package/src/sync/continuous-lease.ts +2 -2
  440. package/src/sync/continuous-record.ts +2 -2
  441. package/src/sync/continuous-storage.ts +2 -2
  442. package/src/sync/continuous-sync.ts +2 -2
  443. package/src/sync/continuous-watch.ts +2 -2
  444. package/src/sync/debug-tool-execution.ts +21 -22
  445. package/src/sync/debug-tool.ts +12 -15
  446. package/src/sync/exclude-policy.ts +2 -2
  447. package/src/sync/field-migration.ts +2 -2
  448. package/src/sync/file-merge-planner.ts +2 -2
  449. package/src/sync/json-document.ts +2 -2
  450. package/src/sync/local-diff-review.ts +115 -0
  451. package/src/sync/local-field-policy.ts +2 -2
  452. package/src/sync/local-fields.ts +2 -2
  453. package/src/sync/local-recovery.ts +53 -0
  454. package/src/sync/merge-apply.ts +2 -2
  455. package/src/sync/merge-journal.ts +2 -2
  456. package/src/sync/merge-transfer-error.ts +2 -2
  457. package/src/sync/merged-sync.ts +2 -2
  458. package/src/sync/mutation-owner.ts +2 -2
  459. package/src/sync/package-merge.ts +2 -2
  460. package/src/sync/partial-progress.ts +2 -2
  461. package/src/sync/remote-snapshot.ts +2 -2
  462. package/src/sync/session-merge.ts +2 -2
  463. package/src/sync/settings-conflicts.ts +2 -2
  464. package/src/sync/settings-merge.ts +2 -2
  465. package/src/sync/signals.ts +2 -2
  466. package/src/sync/startup-timings.ts +2 -2
  467. package/src/sync/sync-decision.ts +2 -2
  468. package/src/sync/sync-error-guidance.ts +2 -2
  469. package/src/sync/sync-errors.ts +2 -2
  470. package/src/sync/sync-inspection.ts +46 -11
  471. package/src/sync/sync-local.ts +2 -2
  472. package/src/sync/sync-mutations.ts +2 -2
  473. package/src/sync/sync-operations.ts +143 -33
  474. package/src/sync/sync-policy.ts +2 -2
  475. package/src/sync/sync-queries.ts +2 -2
  476. package/src/sync/sync-state.ts +2 -2
  477. package/src/sync/text-merge.ts +2 -2
  478. package/src/sync-extension.ts +193 -278
  479. package/src/ui/cancellable-operation.ts +1 -1
  480. package/src/ui/diff-review.ts +4 -3
  481. package/src/ui/manager-ui.ts +2 -22
  482. package/dist/chunks/chunk-6YKOHBSC.ts +0 -18
  483. package/dist/chunks/chunk-6YKOHBSC.ts.map +0 -7
  484. package/dist/chunks/chunk-A43CJYG4.ts +0 -26
  485. package/dist/chunks/chunk-A43CJYG4.ts.map +0 -7
  486. package/dist/chunks/chunk-APZVJAEW.ts +0 -34
  487. package/dist/chunks/chunk-APZVJAEW.ts.map +0 -7
  488. package/dist/chunks/chunk-AXN3B5KC.ts.map +0 -7
  489. package/dist/chunks/chunk-GELYN4UC.ts +0 -14
  490. package/dist/chunks/chunk-GELYN4UC.ts.map +0 -7
  491. package/dist/chunks/chunk-IBXEM35O.ts +0 -34
  492. package/dist/chunks/chunk-IBXEM35O.ts.map +0 -7
  493. package/dist/chunks/chunk-KN4NY2VV.ts +0 -1524
  494. package/dist/chunks/chunk-KN4NY2VV.ts.map +0 -7
  495. package/dist/chunks/chunk-LNZRNXNQ.ts.map +0 -7
  496. package/dist/chunks/chunk-NXWHPVXR.ts +0 -26
  497. package/dist/chunks/chunk-NXWHPVXR.ts.map +0 -7
  498. package/dist/chunks/chunk-P4GI7LTT.ts +0 -20
  499. package/dist/chunks/chunk-P4GI7LTT.ts.map +0 -7
  500. package/dist/chunks/chunk-R3SAMXB6.ts +0 -60
  501. package/dist/chunks/chunk-R3SAMXB6.ts.map +0 -7
  502. package/dist/chunks/chunk-R7PN3KIY.ts +0 -22
  503. package/dist/chunks/chunk-R7PN3KIY.ts.map +0 -7
  504. package/dist/chunks/chunk-T3LABNHF.ts +0 -20
  505. package/dist/chunks/chunk-T3LABNHF.ts.map +0 -7
  506. package/dist/chunks/chunk-UMPA5S7X.ts +0 -11
  507. package/dist/chunks/chunk-UMPA5S7X.ts.map +0 -7
  508. package/dist/chunks/chunk-VQTYTUBU.ts.map +0 -7
  509. package/dist/chunks/chunk-ZO52JM4H.ts +0 -15
  510. package/dist/chunks/chunk-ZO52JM4H.ts.map +0 -7
  511. package/dist/chunks/chunk-ZWP6HAYV.ts.map +0 -7
  512. package/dist/chunks/client-debug-F4WFP2YJ.ts +0 -40
  513. package/dist/chunks/client-debug-F4WFP2YJ.ts.map +0 -7
  514. package/dist/chunks/command-handler-GERC7LOE.ts.map +0 -7
  515. package/dist/chunks/conflict-review-XZI7RYZX.ts +0 -18
  516. package/dist/chunks/conflict-review-XZI7RYZX.ts.map +0 -7
  517. package/dist/chunks/debug-tool-execution-7N4H6K3X.ts +0 -67
  518. package/dist/chunks/debug-tool-execution-7N4H6K3X.ts.map +0 -7
  519. package/dist/chunks/diff-review-FLWC5VOT.ts +0 -68
  520. package/dist/chunks/diff-review-FLWC5VOT.ts.map +0 -7
  521. package/dist/chunks/diff-review-GB4NO53B.ts.map +0 -7
  522. package/dist/chunks/manager-result-dispatcher-6MOJPLIS.ts +0 -20
  523. package/dist/chunks/manager-ui-3CQU37XY.ts.map +0 -7
  524. package/dist/chunks/ownership-recovery-5KXAMFSL.ts +0 -59
  525. package/dist/chunks/ownership-recovery-5KXAMFSL.ts.map +0 -7
  526. package/dist/chunks/remote-selection-ui-2V53UBHM.ts +0 -19
  527. package/dist/chunks/setup-switch-WSKVGW7L.ts +0 -27
  528. package/dist/chunks/snapshot-ZUJJZCFW.ts.map +0 -7
  529. package/dist/chunks/snapshot-transaction-directory-F46JWRAU.ts +0 -13
  530. package/dist/chunks/snapshot-transaction-directory-F46JWRAU.ts.map +0 -7
  531. package/dist/chunks/state-directory-CMAIF34J.ts +0 -17
  532. package/dist/chunks/sync-inspection-ECI4BY5N.ts +0 -23
  533. package/dist/chunks/sync-inspection-ECI4BY5N.ts.map +0 -7
  534. package/dist/chunks/sync-operations-KWXZZEPT.ts +0 -52
  535. package/dist/chunks/sync-operations-KWXZZEPT.ts.map +0 -7
  536. package/dist/chunks/sync-state-QIT64TKV.ts.map +0 -7
  537. package/dist/daemon/LICENSE +0 -21
  538. package/dist/daemon/dist/build-id.json +0 -1
  539. package/dist/daemon/dist/build-version.js +0 -28
  540. package/dist/daemon/dist/child-sdk.mjs +0 -17
  541. package/dist/daemon/dist/cli.js +0 -156
  542. package/dist/daemon/dist/client-transport.js +0 -77
  543. package/dist/daemon/dist/client.js +0 -950
  544. package/dist/daemon/dist/consent.js +0 -304
  545. package/dist/daemon/dist/daemon-debug.js +0 -40
  546. package/dist/daemon/dist/daemon-recovery.js +0 -160
  547. package/dist/daemon/dist/daemon-startup.js +0 -350
  548. package/dist/daemon/dist/daemon-status.js +0 -57
  549. package/dist/daemon/dist/daemon-upgrade-error.js +0 -14
  550. package/dist/daemon/dist/diff-review-binding.js +0 -11
  551. package/dist/daemon/dist/diff-review-protocol.js +0 -85
  552. package/dist/daemon/dist/diff-review-rpc.js +0 -37
  553. package/dist/daemon/dist/diff-review-store.js +0 -89
  554. package/dist/daemon/dist/document.js +0 -6
  555. package/dist/daemon/dist/endpoint.js +0 -91
  556. package/dist/daemon/dist/engine/backends/backend-factory.js +0 -16
  557. package/dist/daemon/dist/engine/backends/backend-identity.js +0 -38
  558. package/dist/daemon/dist/engine/backends/backend-types.js +0 -1
  559. package/dist/daemon/dist/engine/backends/git/git-backend.js +0 -775
  560. package/dist/daemon/dist/engine/backends/git/git-config.js +0 -118
  561. package/dist/daemon/dist/engine/backends/git/git-runner.js +0 -237
  562. package/dist/daemon/dist/engine/backends/git/git-storage.js +0 -227
  563. package/dist/daemon/dist/engine/backends/publication-encoding.js +0 -19
  564. package/dist/daemon/dist/engine/backends/s3/s3-backend.js +0 -453
  565. package/dist/daemon/dist/engine/backends/s3/s3-client.js +0 -362
  566. package/dist/daemon/dist/engine/backends/s3/s3-conditions.js +0 -66
  567. package/dist/daemon/dist/engine/backends/snapshot-pointer.js +0 -22
  568. package/dist/daemon/dist/engine/backends/sync-backend.js +0 -39
  569. package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +0 -605
  570. package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +0 -349
  571. package/dist/daemon/dist/engine/backends/webdav/webdav-config.js +0 -73
  572. package/dist/daemon/dist/engine/commands/command-types.js +0 -1
  573. package/dist/daemon/dist/engine/debug-check.js +0 -59
  574. package/dist/daemon/dist/engine/debug-log.js +0 -252
  575. package/dist/daemon/dist/engine/host.js +0 -20
  576. package/dist/daemon/dist/engine/paths.js +0 -77
  577. package/dist/daemon/dist/engine/settings/config-errors.js +0 -4
  578. package/dist/daemon/dist/engine/settings/config-file.js +0 -784
  579. package/dist/daemon/dist/engine/settings/config.js +0 -246
  580. package/dist/daemon/dist/engine/settings/retired-ignore-policy.js +0 -16
  581. package/dist/daemon/dist/engine/settings/retired-ignore-recovery.js +0 -14
  582. package/dist/daemon/dist/engine/settings/settings-management.js +0 -196
  583. package/dist/daemon/dist/engine/settings/settings-store.js +0 -41
  584. package/dist/daemon/dist/engine/settings/settings-types.js +0 -1
  585. package/dist/daemon/dist/engine/settings/settings-validation.js +0 -390
  586. package/dist/daemon/dist/engine/snapshot/session-paths.js +0 -146
  587. package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +0 -347
  588. package/dist/daemon/dist/engine/snapshot/snapshot-bounds.js +0 -20
  589. package/dist/daemon/dist/engine/snapshot/snapshot-capture-limit.js +0 -99
  590. package/dist/daemon/dist/engine/snapshot/snapshot-case-replacement.js +0 -82
  591. package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +0 -62
  592. package/dist/daemon/dist/engine/snapshot/snapshot-difference.js +0 -13
  593. package/dist/daemon/dist/engine/snapshot/snapshot-entry-projection.js +0 -78
  594. package/dist/daemon/dist/engine/snapshot/snapshot-exclude-safety.js +0 -205
  595. package/dist/daemon/dist/engine/snapshot/snapshot-mutation-queues.js +0 -153
  596. package/dist/daemon/dist/engine/snapshot/snapshot-paths.js +0 -11
  597. package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +0 -42
  598. package/dist/daemon/dist/engine/snapshot/snapshot-reviewed-preimages.js +0 -60
  599. package/dist/daemon/dist/engine/snapshot/snapshot-root-transition.js +0 -138
  600. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-directory.js +0 -16
  601. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +0 -415
  602. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-plan.js +0 -68
  603. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-policy.js +0 -202
  604. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-stage.js +0 -303
  605. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-verification.js +0 -78
  606. package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +0 -830
  607. package/dist/daemon/dist/engine/snapshot/snapshot-types.js +0 -3
  608. package/dist/daemon/dist/engine/snapshot/snapshot.js +0 -695
  609. package/dist/daemon/dist/engine/state/json-file.js +0 -49
  610. package/dist/daemon/dist/engine/state/lock.js +0 -399
  611. package/dist/daemon/dist/engine/state/lockfile-fs.js +0 -13
  612. package/dist/daemon/dist/engine/state/merge-baseline-store.js +0 -198
  613. package/dist/daemon/dist/engine/state/mutation-directory-sync.js +0 -34
  614. package/dist/daemon/dist/engine/state/operation-availability.js +0 -40
  615. package/dist/daemon/dist/engine/state/state-directory.js +0 -227
  616. package/dist/daemon/dist/engine/state/state-types.js +0 -1
  617. package/dist/daemon/dist/engine/state/sync-state-store.js +0 -58
  618. package/dist/daemon/dist/engine/sync/agent-candidate.js +0 -207
  619. package/dist/daemon/dist/engine/sync/agent-plan.js +0 -84
  620. package/dist/daemon/dist/engine/sync/agent-refusal-retry.js +0 -39
  621. package/dist/daemon/dist/engine/sync/agent-request.js +0 -13
  622. package/dist/daemon/dist/engine/sync/agent-runner.js +0 -183
  623. package/dist/daemon/dist/engine/sync/agent-store.js +0 -177
  624. package/dist/daemon/dist/engine/sync/agent-sync.js +0 -139
  625. package/dist/daemon/dist/engine/sync/apply-review.js +0 -52
  626. package/dist/daemon/dist/engine/sync/conflict-artifact-ownership.js +0 -53
  627. package/dist/daemon/dist/engine/sync/conflict-artifacts.js +0 -158
  628. package/dist/daemon/dist/engine/sync/conflict-groups.js +0 -76
  629. package/dist/daemon/dist/engine/sync/conflict-preview.js +0 -53
  630. package/dist/daemon/dist/engine/sync/conflict-resolution.js +0 -53
  631. package/dist/daemon/dist/engine/sync/conflict-retention.js +0 -61
  632. package/dist/daemon/dist/engine/sync/conflict-review.js +0 -73
  633. package/dist/daemon/dist/engine/sync/content-conflicts.js +0 -70
  634. package/dist/daemon/dist/engine/sync/continuous-errors.js +0 -72
  635. package/dist/daemon/dist/engine/sync/continuous-lease.js +0 -206
  636. package/dist/daemon/dist/engine/sync/continuous-record.js +0 -199
  637. package/dist/daemon/dist/engine/sync/continuous-storage.js +0 -59
  638. package/dist/daemon/dist/engine/sync/continuous-sync.js +0 -549
  639. package/dist/daemon/dist/engine/sync/continuous-watch.js +0 -137
  640. package/dist/daemon/dist/engine/sync/diff-review-capture.js +0 -210
  641. package/dist/daemon/dist/engine/sync/diff-review-preview.js +0 -114
  642. package/dist/daemon/dist/engine/sync/directional-action.js +0 -101
  643. package/dist/daemon/dist/engine/sync/directional-conflict.js +0 -29
  644. package/dist/daemon/dist/engine/sync/exclude-policy.js +0 -86
  645. package/dist/daemon/dist/engine/sync/field-migration.js +0 -20
  646. package/dist/daemon/dist/engine/sync/file-merge-planner.js +0 -123
  647. package/dist/daemon/dist/engine/sync/initialization.js +0 -81
  648. package/dist/daemon/dist/engine/sync/json-document.js +0 -172
  649. package/dist/daemon/dist/engine/sync/local-field-policy.js +0 -26
  650. package/dist/daemon/dist/engine/sync/local-fields.js +0 -161
  651. package/dist/daemon/dist/engine/sync/merge-acceptance.js +0 -29
  652. package/dist/daemon/dist/engine/sync/merge-apply.js +0 -530
  653. package/dist/daemon/dist/engine/sync/merge-journal.js +0 -204
  654. package/dist/daemon/dist/engine/sync/merge-transfer-error.js +0 -64
  655. package/dist/daemon/dist/engine/sync/merged-sync.js +0 -655
  656. package/dist/daemon/dist/engine/sync/mutation-owner.js +0 -18
  657. package/dist/daemon/dist/engine/sync/package-merge.js +0 -82
  658. package/dist/daemon/dist/engine/sync/partial-progress.js +0 -41
  659. package/dist/daemon/dist/engine/sync/push-review.js +0 -18
  660. package/dist/daemon/dist/engine/sync/remote-snapshot.js +0 -119
  661. package/dist/daemon/dist/engine/sync/resolver-stream.js +0 -34
  662. package/dist/daemon/dist/engine/sync/session-merge.js +0 -178
  663. package/dist/daemon/dist/engine/sync/session-push.js +0 -56
  664. package/dist/daemon/dist/engine/sync/settings-conflicts.js +0 -56
  665. package/dist/daemon/dist/engine/sync/settings-merge.js +0 -93
  666. package/dist/daemon/dist/engine/sync/signals.js +0 -8
  667. package/dist/daemon/dist/engine/sync/startup-bootstrap.js +0 -105
  668. package/dist/daemon/dist/engine/sync/startup-recovery-evidence.js +0 -37
  669. package/dist/daemon/dist/engine/sync/startup-timings.js +0 -116
  670. package/dist/daemon/dist/engine/sync/sync-decision.js +0 -81
  671. package/dist/daemon/dist/engine/sync/sync-error-guidance.js +0 -74
  672. package/dist/daemon/dist/engine/sync/sync-errors.js +0 -38
  673. package/dist/daemon/dist/engine/sync/sync-inspection.js +0 -42
  674. package/dist/daemon/dist/engine/sync/sync-loaders.js +0 -8
  675. package/dist/daemon/dist/engine/sync/sync-local.js +0 -96
  676. package/dist/daemon/dist/engine/sync/sync-mutations.js +0 -723
  677. package/dist/daemon/dist/engine/sync/sync-operations.js +0 -3
  678. package/dist/daemon/dist/engine/sync/sync-policy.js +0 -530
  679. package/dist/daemon/dist/engine/sync/sync-queries.js +0 -316
  680. package/dist/daemon/dist/engine/sync/sync-rollback.js +0 -180
  681. package/dist/daemon/dist/engine/sync/sync-state.js +0 -119
  682. package/dist/daemon/dist/engine/sync/text-merge.js +0 -124
  683. package/dist/daemon/dist/engine/sync/transfer-freshness.js +0 -79
  684. package/dist/daemon/dist/engine/sync/transfer-size-limits.js +0 -88
  685. package/dist/daemon/dist/engine/types.js +0 -2
  686. package/dist/daemon/dist/engine/ui/merge-review.js +0 -7
  687. package/dist/daemon/dist/engine/ui/merged-sync-summary.js +0 -86
  688. package/dist/daemon/dist/engine/ui/sync-format.js +0 -123
  689. package/dist/daemon/dist/engine/ui/sync-status.js +0 -30
  690. package/dist/daemon/dist/engine/ui/terminal-text.js +0 -9
  691. package/dist/daemon/dist/engine/ui/transfer-changes.js +0 -24
  692. package/dist/daemon/dist/engine/ui/transfer-review.js +0 -18
  693. package/dist/daemon/dist/engine-service.js +0 -506
  694. package/dist/daemon/dist/foreground-queue-outcome.js +0 -9
  695. package/dist/daemon/dist/foreground-transfer.js +0 -10
  696. package/dist/daemon/dist/identity.js +0 -275
  697. package/dist/daemon/dist/idle-exit.js +0 -77
  698. package/dist/daemon/dist/index.js +0 -27
  699. package/dist/daemon/dist/legacy-consent-guard.js +0 -34
  700. package/dist/daemon/dist/lifecycle-log.js +0 -54
  701. package/dist/daemon/dist/lifecycle.js +0 -21
  702. package/dist/daemon/dist/model-signature.js +0 -7
  703. package/dist/daemon/dist/paged-review.js +0 -177
  704. package/dist/daemon/dist/participant-admission.js +0 -30
  705. package/dist/daemon/dist/participant-ledger.js +0 -149
  706. package/dist/daemon/dist/protocol.js +0 -108
  707. package/dist/daemon/dist/resolver-host.js +0 -78
  708. package/dist/daemon/dist/server.js +0 -1374
  709. package/dist/daemon/dist/service-host.js +0 -260
  710. package/dist/daemon/dist/session-presence.js +0 -207
  711. package/dist/daemon/dist/state-wait-jobs.js +0 -23
  712. package/dist/daemon/dist/state-wait.js +0 -34
  713. package/dist/daemon/dist/upgrade-repair.js +0 -55
  714. package/dist/daemon/package.json +0 -3
  715. package/dist/package.json +0 -6
  716. package/src/daemon/apply-gate.ts +0 -146
  717. package/src/daemon/client-debug.ts +0 -50
  718. package/src/daemon/daemon-status-ui.ts +0 -64
  719. package/src/daemon/diff-review.ts +0 -68
  720. package/src/daemon/directional-recovery.ts +0 -106
  721. package/src/daemon/ownership-recovery.ts +0 -50
  722. package/src/daemon/pi-client.ts +0 -1300
  723. package/src/daemon/queue-release-ui.ts +0 -88
  724. package/src/daemon/recovery.ts +0 -103
  725. package/src/daemon/startup-bootstrap-client.ts +0 -26
  726. /package/dist/chunks/{manager-result-dispatcher-6MOJPLIS.ts.map → chunk-3GL7LBNB.ts.map} +0 -0
  727. /package/dist/chunks/{remote-selection-ui-2V53UBHM.ts.map → chunk-3PFWVSV3.ts.map} +0 -0
  728. /package/dist/chunks/{setup-switch-WSKVGW7L.ts.map → chunk-53IKSWLZ.ts.map} +0 -0
  729. /package/dist/chunks/{state-directory-CMAIF34J.ts.map → chunk-A3NEEVNA.ts.map} +0 -0
  730. /package/dist/chunks/{sync-state-store-TZE7XDV5.ts.map → chunk-AITZUACB.ts.map} +0 -0
  731. /package/dist/chunks/{chunk-WE3KJ7JS.ts.map → chunk-IFJXUHHC.ts.map} +0 -0
  732. /package/dist/chunks/{chunk-HFMGK6A6.ts.map → chunk-YLVRV6NM.ts.map} +0 -0
  733. /package/dist/chunks/{merge-review-DXSERTCK.ts.map → merge-review-RMDYVNBK.ts.map} +0 -0
  734. /package/{dist/daemon/dist/engine/state/lock-policy.js → src/engine/state/lock-policy.ts} +0 -0
@@ -0,0 +1,961 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { withSessionWriteProtection } from "../host.js";
5
+ import { syncDirectory, writeJson } from "../state/json-file.js";
6
+ import { withLock } from "../state/lock.js";
7
+ import { syncMutationParents } from "../state/mutation-directory-sync.js";
8
+ import { stateDir } from "../state/state-directory.js";
9
+ import { normalizeSyncExclude } from "../sync/exclude-policy.js";
10
+ import { mergePathIdentity } from "../sync/file-merge-planner.js";
11
+ import { includeFromSelectionConfig, selectionForSnapshot } from "../sync/sync-policy.js";
12
+ import { agentDir, configuredSessionDir } from "./session-paths.js";
13
+ import { agentDirectoryConfiguredSessionDir, scanAgentDirectoryInventory } from "./snapshot.js";
14
+ import { caseReplacementSpelling, coalesceCaseReplacements } from "./snapshot-case-replacement.js";
15
+ import { assertProjectedAgentDirectoryBudget, recheckAgentDirectoryBudget } from "./snapshot-entry-projection.js";
16
+ import { assertUnexcludedTargets } from "./snapshot-exclude-safety.js";
17
+ import { createMutationParents, withMissingTargetQueues, withTargetQueues } from "./snapshot-mutation-queues.js";
18
+ import { sessionStorageRoot } from "./snapshot-paths.js";
19
+ import {
20
+ prepareSessionRootTransition,
21
+ resolveTransitionSessionRoot,
22
+ stableAgentDirectorySettingsImage,
23
+ } from "./snapshot-root-transition.js";
24
+ import { pendingTransactionEntries, transactionRoot } from "./snapshot-transaction-directory.js";
25
+ import {
26
+ legacyImage,
27
+ MAX_TRANSACTION_JOURNAL_BYTES,
28
+ ownedScopedPostTree,
29
+ readTransactionJournal,
30
+ recheckUnchangedFile,
31
+ removeScopedImage,
32
+ scopedDescendant,
33
+ scopedIdentity,
34
+ scopedPathIdentity,
35
+ scopedTransactionImage,
36
+ stableScopedIdentity,
37
+ verifyUnchangedFile,
38
+ } from "./snapshot-transaction-images.js";
39
+ import { indexTransactionPlan } from "./snapshot-transaction-plan.js";
40
+ import {
41
+ AGENT_DIRECTORY_JOURNAL_VERSION,
42
+ assertAllowedTarget,
43
+ assertRecoveryPolicy,
44
+ assertSafeParents,
45
+ isScopedJournal,
46
+ isStrictlyInside,
47
+ type TransactionEntry,
48
+ type TransactionJournal,
49
+ type TransactionOptions,
50
+ touchesProtectedTarget,
51
+ transactionHasSessionTargets,
52
+ validateJournal,
53
+ validateJournalPolicy,
54
+ } from "./snapshot-transaction-policy.js";
55
+ import {
56
+ assertNoUnclaimedLegacyStage,
57
+ buildScopedRecoveryStage,
58
+ cleanScopedLegacyStage,
59
+ cleanScopedWorkspace,
60
+ inspectScopedRecoveryStage,
61
+ prepareLegacyRecoveryStage,
62
+ recoveryStagePath,
63
+ } from "./snapshot-transaction-stage.js";
64
+ import { verifyTarget } from "./snapshot-transaction-verification.js";
65
+ import type { SnapshotApplyPlan } from "./snapshot-types.js";
66
+
67
+ const JOURNAL_VERSION = 4;
68
+ class ScopedRemovalError extends Error {}
69
+
70
+ export type SkippedSnapshotWrite = SnapshotApplyPlan["writes"][number] & { preimage: string };
71
+
72
+ export async function applySnapshotTransaction(
73
+ plan: SnapshotApplyPlan,
74
+ options: TransactionOptions = {},
75
+ skipped: readonly SkippedSnapshotWrite[] = [],
76
+ ) {
77
+ options.validateMutation?.();
78
+ const root = path.resolve(agentDir());
79
+ if (options.scope === "agent-directory") {
80
+ const sessionDir = options.sessionDir ?? (await agentDirectoryConfiguredSessionDir(options.signal, root));
81
+ options = { ...options, sessionDir: sessionStorageRoot(root, sessionDir) };
82
+ }
83
+ await recoverPendingSnapshotTransactions(options);
84
+ const coalesced = await coalesceCaseReplacements(root, plan);
85
+ options.validateMutation?.();
86
+ options.signal?.throwIfAborted();
87
+ const targets = [...new Set([...coalesced.plan.deletes, ...coalesced.plan.writes.map((item) => item.target)])].sort();
88
+ if (targets.length > 16_384)
89
+ throw new Error("Snapshot transaction exceeds its target bound; review a smaller transfer.");
90
+ return withTargetQueues(targets, options, async (heldKeys) => {
91
+ await assertUnexcludedTargets(root, [...targets, ...coalesced.replacements.values()], options);
92
+ options.signal?.throwIfAborted();
93
+ if (targets.some((target) => touchesProtectedTarget(target, options.protectedTargets)))
94
+ throw new Error("Snapshot transaction targets the current session; review is required.");
95
+ for (const [before, after] of coalesced.replacements) {
96
+ if ((await caseReplacementSpelling(before, after)) !== before)
97
+ throw new Error("Case replacement changed before preparation; review is required.");
98
+ options.validateMutation?.();
99
+ options.signal?.throwIfAborted();
100
+ }
101
+ const originalTargets = new Map([...coalesced.replacements].map(([before, after]) => [after, before]));
102
+ const scoped = options.scope === "agent-directory";
103
+ const expectedPreimages = new Map(
104
+ [...(options.expectedPreimages ?? [])]
105
+ .filter(([target]) => !scoped || !originalTargets.has(target))
106
+ .map(([target, expected]) => [originalTargets.get(target) ?? target, expected]),
107
+ );
108
+ for (const [target, expected] of scoped ? [] : (options.expectedPreimages ?? [])) {
109
+ if (originalTargets.has(target)) continue;
110
+ const variants = coalesced.plan.deletes.filter(
111
+ (candidate) =>
112
+ candidate !== target &&
113
+ path.dirname(candidate) === path.dirname(target) &&
114
+ mergePathIdentity(candidate) === mergePathIdentity(target),
115
+ );
116
+ if (variants.length > 1) throw new Error("Ambiguous reviewed file preimage spellings; review required.");
117
+ const original = variants[0];
118
+ if (!original) continue;
119
+ const destination = await legacyImage(target);
120
+ options.validateMutation?.();
121
+ options.signal?.throwIfAborted();
122
+ if (destination !== "missing" && destination !== expected)
123
+ throw new Error("Reviewed destination preimage changed before apply.");
124
+ expectedPreimages.set(original, expected);
125
+ expectedPreimages.set(target, destination);
126
+ }
127
+ if (options.scope === "agent-directory") {
128
+ const evidence = await stableAgentDirectorySettingsImage(root, plan, options);
129
+ if (evidence) {
130
+ const target = originalTargets.get(evidence.target) ?? evidence.target;
131
+ const reviewed = expectedPreimages.get(target);
132
+ if (reviewed !== undefined && reviewed !== evidence.image)
133
+ throw new Error("Reviewed Pi settings changed before apply; snapshot installation refused.");
134
+ expectedPreimages.set(target, evidence.image);
135
+ }
136
+ }
137
+ const skippedPreimages = new Map(skipped.map(({ target, preimage }) => [target, preimage]));
138
+ for (const [target, expected] of expectedPreimages) {
139
+ if (skippedPreimages.get(target) === expected) continue;
140
+ if (scoped) await assertUnexcludedTargets(root, [target], options);
141
+ const current = scoped ? (await scopedTransactionImage(target, options)).value : await legacyImage(target);
142
+ options.validateMutation?.();
143
+ options.signal?.throwIfAborted();
144
+ if (current !== expected)
145
+ throw new Error("Reviewed file preimage changed before apply; snapshot installation refused.");
146
+ }
147
+
148
+ const mutationTargets = skipped.length ? targets.filter((target) => !skippedPreimages.has(target)) : targets;
149
+ const writes = skipped.length ? plan.writes.filter(({ target }) => !skippedPreimages.has(target)) : plan.writes;
150
+ if (mutationTargets.length === 0) {
151
+ if (options.validateApplyReview) await options.validateApplyReview();
152
+ if (scoped) {
153
+ const pendingStateEntry = stateDir(root);
154
+ const inventory = await scanAgentDirectoryInventory(root, options, pendingStateEntry);
155
+ options.validateMutation?.();
156
+ options.signal?.throwIfAborted();
157
+ assertProjectedAgentDirectoryBudget(
158
+ inventory,
159
+ { deletes: [], writes: [{ target: pendingStateEntry }] },
160
+ options,
161
+ );
162
+ }
163
+
164
+ await verifySkippedWrites(root, skipped, heldKeys, options);
165
+ options.validateMutation?.();
166
+ options.signal?.throwIfAborted();
167
+ return;
168
+ }
169
+ const mutationPlan = skipped.length
170
+ ? { ...coalesced.plan, writes: coalesced.plan.writes.filter(({ target }) => !skippedPreimages.has(target)) }
171
+ : coalesced.plan;
172
+ const transaction = await prepareTransaction(
173
+ mutationPlan,
174
+ mutationTargets,
175
+ { ...options, expectedPreimages },
176
+ coalesced.replacements,
177
+ );
178
+ const entriesByTarget = new Map(
179
+ transaction.journal.entries.flatMap((entry) => [
180
+ [entry.target, entry] as const,
181
+ ...(entry.afterTarget ? [[entry.afterTarget, entry] as const] : []),
182
+ ]),
183
+ );
184
+ const changesQueueKey = transaction.journal.entries.some(
185
+ (entry) => !heldKeys.has(entry.target) || (entry.afterTarget && !heldKeys.has(entry.afterTarget)),
186
+ );
187
+ try {
188
+ if (options.validateApplyReview) await options.validateApplyReview();
189
+ if (scoped && transaction.deletes.length) await recheckAgentDirectoryBudget(root, plan, options, stateDir(root));
190
+ for (const target of transaction.deletes.sort((a, b) => a.length - b.length)) {
191
+ options.signal?.throwIfAborted();
192
+ const entry = entriesByTarget.get(target);
193
+ if (!entry) throw new Error("Unowned transaction target.");
194
+ await removeOwnedTarget(transaction.directory, entry, transaction.journal, options, { apply: true });
195
+ await syncDirectory(path.dirname(target));
196
+ }
197
+ await withMissingTargetQueues(
198
+ transaction.journal.entries
199
+ .filter((entry) => entry.removalPending)
200
+ .flatMap((entry) => [entry.target, ...(entry.afterTarget ? [entry.afterTarget] : [])]),
201
+ heldKeys,
202
+ options,
203
+ async () => {
204
+ if (options.validateApplyReview && transaction.journal.entries.some((entry) => entry.removalPending))
205
+ await options.validateApplyReview();
206
+
207
+ if (scoped && plan.writes.length)
208
+ await recheckAgentDirectoryBudget(root, { deletes: [], writes: plan.writes }, options);
209
+
210
+ await withSessionWriteProtection(
211
+ writes.map(({ target }) => target),
212
+ async () => {
213
+ await retireRemovalIntent(
214
+ transaction.directory,
215
+ transaction.journal,
216
+ transaction.journal.entries.filter(
217
+ (entry) =>
218
+ entry.removalPending && (entry.afterImage !== "missing" || (entry.postFiles?.length ?? 0) > 0),
219
+ ),
220
+ options,
221
+ );
222
+ const uninstalledWrites = new Set(transaction.deletedWrites);
223
+ for (const item of writes) {
224
+ options.signal?.throwIfAborted();
225
+ const entry = entriesByTarget.get(item.target);
226
+ if (!entry) throw new Error("Unowned transaction target.");
227
+ const deletedByThisCall = uninstalledWrites.delete(entry.target);
228
+ if (options.scope === "agent-directory")
229
+ await assertUnexcludedTargets(transaction.journal.root, [item.target], options);
230
+ await verifyTarget(transaction.directory, entry, transaction.journal, options, deletedByThisCall);
231
+ options.validateMutation?.();
232
+ options.signal?.throwIfAborted();
233
+
234
+ await createMutationParents(item.target, options);
235
+ options.validateMutation?.();
236
+ options.signal?.throwIfAborted();
237
+ const temp = path.join(path.dirname(item.target), `.pi-sync.json.${randomUUID()}.apply`);
238
+ try {
239
+ let mode = 0o600;
240
+ try {
241
+ const stat = await fs.lstat(item.target);
242
+ if (stat.isFile()) mode = stat.mode & 0o777;
243
+ } catch (error) {
244
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
245
+ }
246
+ const handle = await fs.open(temp, "wx", mode);
247
+ try {
248
+ await handle.writeFile(item.content);
249
+ await handle.sync();
250
+ } finally {
251
+ await handle.close();
252
+ }
253
+ await verifyTarget(transaction.directory, entry, transaction.journal, options, deletedByThisCall);
254
+ options.validateMutation?.();
255
+ options.signal?.throwIfAborted();
256
+ await fs.rename(temp, item.target);
257
+ await syncDirectory(path.dirname(item.target));
258
+ } finally {
259
+ await fs.rm(temp, { force: true });
260
+ }
261
+ }
262
+ },
263
+ );
264
+ options.validateMutation?.();
265
+ options.signal?.throwIfAborted();
266
+ await syncMutationParents(
267
+ mutationTargets,
268
+ [transaction.journal.root, ...(transaction.journal.sessionRoot ? [transaction.journal.sessionRoot] : [])],
269
+ options,
270
+ );
271
+ options.validateMutation?.();
272
+ options.signal?.throwIfAborted();
273
+ },
274
+ );
275
+
276
+ await verifySkippedWrites(root, skipped, heldKeys, options);
277
+ options.validateMutation?.();
278
+ options.signal?.throwIfAborted();
279
+ } catch (error) {
280
+ if (options.signal?.aborted)
281
+ throw new Error("Snapshot apply cancelled; guarded transaction evidence retained for review.");
282
+ if (error instanceof ScopedRemovalError) throw error;
283
+ if (changesQueueKey)
284
+ throw new Error("Snapshot apply failed; guarded transaction evidence retained for review.", {
285
+ cause: error,
286
+ });
287
+ try {
288
+ await restoreTransaction(transaction.directory, transaction.journal, options, heldKeys);
289
+ } catch (recoveryError) {
290
+ throw new AggregateError(
291
+ [error, recoveryError],
292
+ "Snapshot apply failed and guarded recovery requires review. Transaction and backup retained; newer bytes were not restored over.",
293
+ );
294
+ }
295
+ throw error;
296
+ }
297
+ await completeTransaction(transaction.directory, transaction.journal);
298
+ });
299
+ }
300
+
301
+ async function verifySkippedWrites(
302
+ root: string,
303
+ skipped: readonly SkippedSnapshotWrite[],
304
+ heldKeys: ReadonlySet<string>,
305
+ options: TransactionOptions,
306
+ ) {
307
+ if (!skipped.length) return;
308
+ await assertUnexcludedTargets(
309
+ root,
310
+ skipped.map(({ target }) => target),
311
+ options,
312
+ { recursive: false },
313
+ );
314
+ const sessionRoot = options.sessionDir ? sessionStorageRoot(root, options.sessionDir) : undefined;
315
+ const verified = [];
316
+ for (const { target, content } of skipped) {
317
+ if (!heldKeys.has(target) || touchesProtectedTarget(target, options.protectedTargets))
318
+ throw new Error("Skipped file lost its transaction queue or session protection changed; review is required.");
319
+ await assertSafeParents(root, sessionRoot, target);
320
+ verified.push({ target, stat: await verifyUnchangedFile(target, content, options) });
321
+ }
322
+ for (const { target, stat } of verified) await recheckUnchangedFile(target, stat, options);
323
+ }
324
+
325
+ export async function recoverSnapshotTransactionsOnStartup(options: TransactionOptions = {}) {
326
+ if (!(await pendingTransactionEntries()).some((entry) => entry.isDirectory())) return;
327
+ options.validateMutation?.();
328
+ options.signal?.throwIfAborted();
329
+ await withLock(
330
+ "recovery",
331
+ () => recoverPendingSnapshotTransactions({ ...options, resolveConfiguredSessionDir: true }),
332
+ {
333
+ reclaimStale: true,
334
+ },
335
+ );
336
+ }
337
+
338
+ export async function recoverPendingSnapshotTransactions(options: TransactionOptions = {}) {
339
+ for (const entry of (await pendingTransactionEntries()).sort((a, b) => a.name.localeCompare(b.name))) {
340
+ if (!entry.isDirectory()) continue;
341
+ const directory = path.join(transactionRoot(), entry.name);
342
+ let journal: TransactionJournal;
343
+ try {
344
+ const content = await readTransactionJournal(path.join(directory, "journal.json"), options);
345
+ if (content === undefined) continue;
346
+ journal = JSON.parse(content) as TransactionJournal;
347
+ } catch {
348
+ options.validateMutation?.();
349
+ options.signal?.throwIfAborted();
350
+ throw new Error("Cannot recover malformed pi-sync transaction; preserve private evidence for review.");
351
+ }
352
+ validateJournalPolicy(journal);
353
+ if (!journal.completed) {
354
+ assertRecoveryPolicy(journal, options);
355
+ }
356
+ let sessionDir = options.sessionDir;
357
+ if (sessionDir === undefined && !journal.completed) {
358
+ if (options.scope === "agent-directory")
359
+ sessionDir = await agentDirectoryConfiguredSessionDir(options.signal, agentDir());
360
+ else if (options.resolveConfiguredSessionDir && transactionHasSessionTargets(journal))
361
+ sessionDir = await configuredSessionDir();
362
+ }
363
+ options.validateMutation?.();
364
+ options.signal?.throwIfAborted();
365
+ if (journal.sessionRootTransition && !journal.completed) {
366
+ validateJournal(directory, journal, journal.sessionRoot);
367
+ sessionDir = await resolveTransitionSessionRoot(
368
+ directory,
369
+ journal.root,
370
+ journal.sessionRoot as string,
371
+ journal.sessionRootTransition,
372
+ journal.entries,
373
+ path.resolve(sessionStorageRoot(journal.root, sessionDir)),
374
+ options,
375
+ );
376
+ options.validateMutation?.();
377
+ options.signal?.throwIfAborted();
378
+ }
379
+ validateJournal(directory, journal, sessionDir);
380
+ if (journal.completed) {
381
+ await removeTransaction(directory);
382
+ continue;
383
+ }
384
+ await withTargetQueues(journal.entries.map((entry) => entry.target).sort(), options, (heldKeys) =>
385
+ restoreTransaction(directory, journal, { ...options, sessionDir }, heldKeys),
386
+ );
387
+ }
388
+ }
389
+
390
+ async function prepareTransaction(
391
+ plan: SnapshotApplyPlan,
392
+ targets: string[],
393
+ options: TransactionOptions,
394
+ replacements = new Map<string, string>(),
395
+ ) {
396
+ const { sessionDir } = options;
397
+ const root = path.resolve(agentDir());
398
+ const wholeDirectory = options.scope === "agent-directory";
399
+ const sessionRoot = sessionDir || wholeDirectory ? path.resolve(sessionStorageRoot(root, sessionDir)) : undefined;
400
+ const indexed = indexTransactionPlan(plan, targets);
401
+ const directory = path.join(transactionRoot(), randomUUID());
402
+ const backupDirectory = path.join(directory, "before");
403
+ await fs.mkdir(backupDirectory, { recursive: true, mode: 0o700 });
404
+ const entries: TransactionEntry[] = [];
405
+ for (const [index, target] of targets.entries()) {
406
+ options.signal?.throwIfAborted();
407
+ options.validateMutation?.();
408
+ assertAllowedTarget(root, sessionRoot, target);
409
+ await assertSafeParents(root, sessionRoot, target);
410
+ if (wholeDirectory) await assertUnexcludedTargets(root, [target], options);
411
+ const entry: TransactionEntry = { target, backupName: `${index}`, kind: "missing" };
412
+ if (replacements.has(target)) entry.afterTarget = replacements.get(target);
413
+ const backup = path.join(backupDirectory, entry.backupName);
414
+ if (wholeDirectory) {
415
+ const captured = await scopedTransactionImage(target, options, backup, (source) =>
416
+ assertUnexcludedTargets(root, [source], options, { recursive: false }),
417
+ );
418
+ entry.kind = captured.kind;
419
+ entry.beforeImage = captured.value;
420
+ } else {
421
+ await captureLegacyBackup(target, backup, entry, options);
422
+ }
423
+ options.validateMutation?.();
424
+ options.signal?.throwIfAborted();
425
+ const expected = options.expectedPreimages?.get(target);
426
+ if (wholeDirectory && entry.kind === "directory" && expected === undefined)
427
+ throw new Error("Scoped directory replacement requires a reviewed preimage.");
428
+ if (expected !== undefined && entry.beforeImage !== expected)
429
+ throw new Error("Reviewed file preimage changed during backup; private preparation evidence retained.");
430
+ if (entry.afterTarget) {
431
+ if (entry.kind !== "file" || (await caseReplacementSpelling(target, entry.afterTarget)) !== target)
432
+ throw new Error("Case replacement preimage changed during backup; evidence retained for review.");
433
+ options.validateMutation?.();
434
+ options.signal?.throwIfAborted();
435
+ }
436
+ entry.afterImage = indexed.writeImages.get(target) ?? "missing";
437
+ entry.postFiles = indexed.postFiles.get(target) ?? [];
438
+ entries.push(entry);
439
+ }
440
+ await syncDirectory(backupDirectory);
441
+ const effectiveRoot = sessionRoot ?? path.join(root, "sessions");
442
+ const sessionRootTransition = wholeDirectory
443
+ ? undefined
444
+ : await prepareSessionRootTransition(directory, root, effectiveRoot, entries, plan, options);
445
+ const journal: TransactionJournal = {
446
+ version: wholeDirectory
447
+ ? AGENT_DIRECTORY_JOURNAL_VERSION
448
+ : sessionRootTransition
449
+ ? 7
450
+ : replacements.size
451
+ ? 5
452
+ : JOURNAL_VERSION,
453
+ root,
454
+ ...(wholeDirectory
455
+ ? { selection: selectionForSnapshot(includeFromSelectionConfig(options), options.exclude, options) }
456
+ : options.exclude === undefined
457
+ ? {}
458
+ : { exclude: normalizeSyncExclude(options.exclude) }),
459
+ sessionRoot: sessionRootTransition ? effectiveRoot : sessionRoot,
460
+ sessionRootTransition,
461
+ entries,
462
+ };
463
+ options.signal?.throwIfAborted();
464
+ options.validateMutation?.();
465
+ await writeJson(path.join(directory, "journal.json"), journal, { maxBytes: MAX_TRANSACTION_JOURNAL_BYTES });
466
+ await syncDirectory(transactionRoot());
467
+ await syncDirectory(stateDir());
468
+ await syncDirectory(path.dirname(stateDir()));
469
+ return { directory, journal, deletes: indexed.deletes, deletedWrites: indexed.deletedWrites };
470
+ }
471
+
472
+ async function captureLegacyBackup(
473
+ target: string,
474
+ backup: string,
475
+ entry: TransactionEntry,
476
+ options: TransactionOptions,
477
+ ) {
478
+ try {
479
+ const stat = await fs.lstat(target);
480
+ if (stat.isSymbolicLink()) {
481
+ entry.kind = "symlink";
482
+ entry.linkTarget = await fs.readlink(target);
483
+ } else if (stat.isDirectory()) {
484
+ entry.kind = "directory";
485
+ await fs.cp(target, backup, {
486
+ recursive: true,
487
+ dereference: false,
488
+ verbatimSymlinks: true,
489
+ preserveTimestamps: true,
490
+ filter: () => {
491
+ options.signal?.throwIfAborted();
492
+ options.validateMutation?.();
493
+ return true;
494
+ },
495
+ });
496
+ } else if (stat.isFile()) {
497
+ entry.kind = "file";
498
+ await fs.copyFile(target, backup);
499
+ await fs.chmod(backup, stat.mode);
500
+ } else throw new Error("Unsupported existing snapshot target.");
501
+ } catch (error) {
502
+ const code = (error as NodeJS.ErrnoException).code;
503
+ if (code !== "ENOENT" && code !== "ENOTDIR") throw error;
504
+ }
505
+ entry.beforeImage =
506
+ entry.kind === "symlink"
507
+ ? `symlink:${entry.linkTarget}`
508
+ : entry.kind === "missing"
509
+ ? "missing"
510
+ : await legacyImage(backup, true);
511
+ }
512
+
513
+ async function restoreTransaction(
514
+ directory: string,
515
+ journal: TransactionJournal,
516
+ options: TransactionOptions,
517
+ heldKeys: ReadonlySet<string>,
518
+ ) {
519
+ validateJournal(directory, journal, options.sessionDir);
520
+ const missingSpellings: string[] = [];
521
+ for (const entry of journal.entries) {
522
+ if (!entry.afterTarget) continue;
523
+ options.validateMutation?.();
524
+ options.signal?.throwIfAborted();
525
+ const spelling = await caseReplacementSpelling(entry.target, entry.afterTarget);
526
+ options.validateMutation?.();
527
+ options.signal?.throwIfAborted();
528
+ if (spelling === undefined) missingSpellings.push(entry.target, entry.afterTarget);
529
+ }
530
+ return withMissingTargetQueues(missingSpellings, heldKeys, options, (reservedKeys) =>
531
+ restoreQueuedTransaction(directory, journal, options, reservedKeys),
532
+ );
533
+ }
534
+
535
+ async function restoreQueuedTransaction(
536
+ directory: string,
537
+ journal: TransactionJournal,
538
+ options: TransactionOptions,
539
+ heldKeys: ReadonlySet<string>,
540
+ ) {
541
+ validateJournal(directory, journal, options.sessionDir);
542
+ const scoped = isScopedJournal(journal);
543
+ options.validateMutation?.();
544
+ options.signal?.throwIfAborted();
545
+
546
+ await assertUnexcludedTargets(
547
+ journal.root,
548
+ journal.entries.flatMap((entry) => [entry.target, ...(entry.afterTarget ? [entry.afterTarget] : [])]),
549
+ options,
550
+ );
551
+ for (const entry of journal.entries) {
552
+ if (touchesProtectedTarget(entry.target, options.protectedTargets))
553
+ throw new Error(
554
+ "Pending transaction touches the current session; resume a different session before guarded recovery.",
555
+ );
556
+ const { value, before } = await verifyTarget(directory, entry, journal, options);
557
+ if (scoped) {
558
+ await assertNoUnclaimedLegacyStage(directory, entry, options);
559
+ const stage = await inspectScopedRecoveryStage(directory, entry, options);
560
+ if (entry.recoveryWorkspace && !stage && value !== before)
561
+ throw new Error("Recovery workspace is missing; preserve evidence for review.");
562
+ if (entry.vacantInstall && value === "missing") {
563
+ if (
564
+ !stage ||
565
+ stage.image.kind === "missing" ||
566
+ stage.image.value !== before ||
567
+ stage.partial.kind !== "missing" ||
568
+ scopedIdentity(stage.image.stat) !== entry.vacantInstall
569
+ )
570
+ throw new Error("Recovery vacancy has no verified staging image; preserve evidence for review.");
571
+ }
572
+ }
573
+ }
574
+ for (const entry of journal.entries) {
575
+ if (!entry.recoveryStaged) continue;
576
+ options.validateMutation?.();
577
+ options.signal?.throwIfAborted();
578
+ await assertSafeParents(journal.root, journal.sessionRoot, entry.target);
579
+ options.validateMutation?.();
580
+ options.signal?.throwIfAborted();
581
+ if (scoped) await cleanScopedLegacyStage(directory, entry, options);
582
+ else await fs.rm(recoveryStagePath(directory, entry), { recursive: true, force: true });
583
+ options.validateMutation?.();
584
+ options.signal?.throwIfAborted();
585
+ }
586
+ if (scoped && journal.version < AGENT_DIRECTORY_JOURNAL_VERSION) {
587
+ journal.version = AGENT_DIRECTORY_JOURNAL_VERSION;
588
+ for (const entry of journal.entries) delete entry.recoveryStaged;
589
+ options.validateMutation?.();
590
+ options.signal?.throwIfAborted();
591
+ await persistJournal(directory, journal);
592
+ }
593
+ for (const entry of [...journal.entries].sort((a, b) => a.target.length - b.target.length)) {
594
+ const { value: current, before } = await verifyTarget(directory, entry, journal, options);
595
+ const spellingMatches = async () =>
596
+ !entry.afterTarget || (await caseReplacementSpelling(entry.target, entry.afterTarget)) === entry.target;
597
+ if (current === before && (await spellingMatches())) {
598
+ if (entry.recoveryWorkspace) {
599
+ await cleanScopedWorkspace(directory, entry, options);
600
+ }
601
+ continue;
602
+ }
603
+ options.signal?.throwIfAborted();
604
+ options.validateMutation?.();
605
+ if (entry.kind === "missing") {
606
+ await removeOwnedTarget(directory, entry, journal, options, { restoringBefore: before });
607
+ continue;
608
+ }
609
+ if (scoped) {
610
+ await restoreScopedEntry(directory, journal, entry, options, heldKeys, before, spellingMatches);
611
+ continue;
612
+ }
613
+ const temporary = await prepareLegacyRecoveryStage(directory, entry, options, () =>
614
+ persistJournal(directory, journal),
615
+ );
616
+ if (entry.kind === "file") {
617
+ try {
618
+ options.validateMutation?.();
619
+ options.signal?.throwIfAborted();
620
+ await withSessionWriteProtection([entry.target], () => createMutationParents(entry.target, options));
621
+ await fs.copyFile(path.join(directory, "before", entry.backupName), temporary);
622
+ const handle = await fs.open(temporary, process.platform === "win32" ? "r+" : "r");
623
+ try {
624
+ await handle.sync();
625
+ } finally {
626
+ await handle.close();
627
+ }
628
+ const { value: verified } = await verifyTarget(directory, entry, journal, options);
629
+ options.validateMutation?.();
630
+ options.signal?.throwIfAborted();
631
+ if (verified === before && (await spellingMatches())) continue;
632
+ const needsCaseQueue =
633
+ entry.afterTarget !== undefined &&
634
+ (await caseReplacementSpelling(entry.target, entry.afterTarget)) === entry.afterTarget;
635
+ if (needsCaseQueue) {
636
+ await removeOwnedTarget(directory, entry, journal, options);
637
+ } else if (
638
+ verified.startsWith("directory:") &&
639
+ !(await removeOwnedTarget(directory, entry, journal, options, { restoringBefore: before }))
640
+ )
641
+ continue;
642
+ await withMissingTargetQueues(
643
+ entry.afterTarget && (needsCaseQueue || verified === "missing")
644
+ ? [entry.target, entry.afterTarget]
645
+ : verified.startsWith("directory:")
646
+ ? [entry.target]
647
+ : [],
648
+ heldKeys,
649
+ options,
650
+ async () => {
651
+ const { value: atRename } = await verifyTarget(directory, entry, journal, options);
652
+ options.validateMutation?.();
653
+ options.signal?.throwIfAborted();
654
+ if (atRename === before && (await spellingMatches())) return;
655
+ await withSessionWriteProtection([entry.target], async () => {
656
+ const { value: atInstall } = await verifyTarget(
657
+ directory,
658
+ entry,
659
+ journal,
660
+ options,
661
+ atRename === "missing",
662
+ );
663
+ if (atInstall === before && (await spellingMatches())) return;
664
+ await retireRestorationIntent(directory, journal, entry, options);
665
+ const { value: armed } = await verifyTarget(directory, entry, journal, options, atRename === "missing");
666
+ if (armed === before && (await spellingMatches())) return;
667
+ options.validateMutation?.();
668
+ options.signal?.throwIfAborted();
669
+ await fs.rename(temporary, entry.target);
670
+ });
671
+ await syncDirectory(path.dirname(entry.target));
672
+ },
673
+ );
674
+ } finally {
675
+ await fs.rm(temporary, { force: true });
676
+ }
677
+ continue;
678
+ }
679
+ try {
680
+ options.validateMutation?.();
681
+ options.signal?.throwIfAborted();
682
+ await withSessionWriteProtection([entry.target], () => createMutationParents(entry.target, options));
683
+ await assertSafeParents(journal.root, journal.sessionRoot, entry.target);
684
+ if (entry.kind === "directory") {
685
+ await fs.cp(path.join(directory, "before", entry.backupName), temporary, {
686
+ recursive: true,
687
+ dereference: false,
688
+ verbatimSymlinks: true,
689
+ preserveTimestamps: true,
690
+ filter: () => {
691
+ options.validateMutation?.();
692
+ options.signal?.throwIfAborted();
693
+ return true;
694
+ },
695
+ });
696
+ } else if (entry.linkTarget !== undefined) await fs.symlink(entry.linkTarget, temporary);
697
+ if ((await legacyImage(temporary, true)) !== before)
698
+ throw new Error("Staged recovery image differs from the backup; evidence retained.");
699
+ options.validateMutation?.();
700
+ options.signal?.throwIfAborted();
701
+ if (!(await removeOwnedTarget(directory, entry, journal, options, { restoringBefore: before }))) continue;
702
+ await withMissingTargetQueues([entry.target], heldKeys, options, async () => {
703
+ const { value: atRename } = await verifyTarget(directory, entry, journal, options);
704
+ options.validateMutation?.();
705
+ options.signal?.throwIfAborted();
706
+ if (atRename === before) return;
707
+ await withSessionWriteProtection([entry.target], async () => {
708
+ const { value: atInstall } = await verifyTarget(directory, entry, journal, options, atRename === "missing");
709
+ if (atInstall === before) return;
710
+ await retireRestorationIntent(directory, journal, entry, options);
711
+ const { value: armed } = await verifyTarget(directory, entry, journal, options, atRename === "missing");
712
+ if (armed === before) return;
713
+ options.validateMutation?.();
714
+ options.signal?.throwIfAborted();
715
+ await fs.rename(temporary, entry.target);
716
+ });
717
+ await syncDirectory(path.dirname(entry.target));
718
+ });
719
+ } finally {
720
+ await fs.rm(temporary, { recursive: true, force: true });
721
+ }
722
+ }
723
+ await syncMutationParents(
724
+ journal.entries.map((entry) => entry.target),
725
+ [journal.root, ...(journal.sessionRoot ? [journal.sessionRoot] : [])],
726
+ options,
727
+ );
728
+ options.validateMutation?.();
729
+ options.signal?.throwIfAborted();
730
+
731
+ await completeTransaction(directory, journal);
732
+ }
733
+
734
+ async function restoreScopedEntry(
735
+ directory: string,
736
+ journal: TransactionJournal,
737
+ entry: TransactionEntry,
738
+ options: TransactionOptions,
739
+ heldKeys: ReadonlySet<string>,
740
+ before: string,
741
+ spellingMatches: () => Promise<boolean>,
742
+ ) {
743
+ options.validateMutation?.();
744
+ options.signal?.throwIfAborted();
745
+
746
+ await withSessionWriteProtection([entry.target], () => createMutationParents(entry.target, options));
747
+ await assertSafeParents(journal.root, journal.sessionRoot, entry.target);
748
+ if (!entry.vacantInstall) {
749
+ await buildScopedRecoveryStage(directory, entry, options, () => persistJournal(directory, journal));
750
+ }
751
+ const verifyStage = async () => {
752
+ const stage = await inspectScopedRecoveryStage(directory, entry, options, true);
753
+ if (!stage || stage.image.kind === "missing" || stage.image.value !== before || stage.partial.kind !== "missing")
754
+ throw new Error("Staged recovery image changed; evidence retained.");
755
+ if (entry.vacantInstall && scopedIdentity(stage.image.stat) !== entry.vacantInstall)
756
+ throw new Error("Recovery vacancy has a different staging image; preserve evidence for review.");
757
+ const identity = stableScopedIdentity(stage.payload, stage.image.stat);
758
+ return { payload: stage.payload, identity, image: stage.image };
759
+ };
760
+ await verifyStage();
761
+ const { value: verified } = await verifyTarget(directory, entry, journal, options);
762
+ if (verified !== before || !(await spellingMatches())) {
763
+ const needsCaseQueue =
764
+ entry.afterTarget !== undefined &&
765
+ (await caseReplacementSpelling(entry.target, entry.afterTarget)) === entry.afterTarget;
766
+ const needsRemoval =
767
+ verified !== "missing" && (needsCaseQueue || verified.startsWith("directory:") || entry.kind === "directory");
768
+ const needsVacantTarget = needsRemoval || verified === "missing";
769
+ if (
770
+ !needsRemoval ||
771
+ (await removeOwnedTarget(directory, entry, journal, options, {
772
+ restoringBefore: needsCaseQueue ? undefined : before,
773
+ }))
774
+ ) {
775
+ const queueTargets = needsVacantTarget ? [entry.target, ...(entry.afterTarget ? [entry.afterTarget] : [])] : [];
776
+ await withMissingTargetQueues(queueTargets, heldKeys, options, async () => {
777
+ const { value: atRename } = await verifyTarget(directory, entry, journal, options);
778
+ if (atRename === before && (await spellingMatches())) return;
779
+ if (needsVacantTarget && atRename !== "missing")
780
+ throw new Error("Transaction target changed after removal; preserve evidence for review.");
781
+ const stage = await verifyStage();
782
+ if (needsVacantTarget && !entry.vacantInstall) {
783
+ entry.vacantInstall = stage.identity;
784
+ delete entry.removalPending;
785
+ delete entry.replacementStarted;
786
+ delete entry.scopedRemoval;
787
+ for (const child of journal.entries) {
788
+ if (isStrictlyInside(entry.target, child.target)) delete child.scopedRemoval;
789
+ }
790
+ options.validateMutation?.();
791
+ options.signal?.throwIfAborted();
792
+ await syncDirectory(path.dirname(entry.target));
793
+ options.validateMutation?.();
794
+ options.signal?.throwIfAborted();
795
+ await persistJournal(directory, journal);
796
+ await verifyStage();
797
+ }
798
+ await withSessionWriteProtection([entry.target], async () => {
799
+ if ((await scopedPathIdentity(stage.payload, options)) !== stage.identity)
800
+ throw new Error("Staged recovery image changed before restoration; preserve evidence for review.");
801
+ const atInstall = await verifyTarget(directory, entry, journal, options);
802
+ if (atInstall.value === before && (await spellingMatches())) return;
803
+ if (atInstall.value !== atRename)
804
+ throw new Error("Transaction target changed before restoration; preserve evidence for review.");
805
+ options.validateMutation?.();
806
+ options.signal?.throwIfAborted();
807
+ await fs.rename(stage.payload, entry.target);
808
+ });
809
+ await syncDirectory(path.dirname(entry.target));
810
+ await syncDirectory(path.dirname(stage.payload));
811
+ if ((await verifyTarget(directory, entry, journal, options)).value !== before)
812
+ throw new Error("Restored transaction image changed; evidence retained.");
813
+ });
814
+ }
815
+ }
816
+
817
+ await cleanScopedWorkspace(directory, entry, options);
818
+ }
819
+
820
+ async function removeOwnedTarget(
821
+ directory: string,
822
+ entry: TransactionEntry,
823
+ journal: TransactionJournal,
824
+ options: TransactionOptions,
825
+ { apply, restoringBefore }: { apply?: boolean; restoringBefore?: string } = {},
826
+ ) {
827
+ const scoped = isScopedJournal(journal);
828
+ try {
829
+ await assertUnexcludedTargets(
830
+ journal.root,
831
+ [entry.target, ...(entry.afterTarget ? [entry.afterTarget] : [])],
832
+ options,
833
+ );
834
+ const current = await verifyTarget(directory, entry, journal, options);
835
+ options.validateMutation?.();
836
+ options.signal?.throwIfAborted();
837
+ if (restoringBefore !== undefined && current.value === restoringBefore) return false;
838
+ if (scoped && apply && current.value !== current.before)
839
+ throw new Error("Reviewed removal image changed; preserve evidence for review.");
840
+ upgradeRemovalEvidence(journal);
841
+ for (const affected of journal.entries) {
842
+ if (affected.target !== entry.target && !isStrictlyInside(entry.target, affected.target)) continue;
843
+ affected.removalPending = true;
844
+ affected.replacementStarted = false;
845
+ if (affected.vacantInstall)
846
+ throw new Error("Transaction removal overlaps a recovery vacancy; preserve evidence for review.");
847
+ if (!current.scoped) continue;
848
+ const relative = path.relative(entry.target, affected.target);
849
+ const observed = scopedDescendant(current.scoped, relative);
850
+ if (!observed || observed.kind === "missing") continue;
851
+ const observedPath = path.join(entry.target, relative);
852
+ const mayChange = observed.kind === "directory" && observed.children.length > 0;
853
+ if (affected.scopedRemoval) {
854
+ if (mayChange) stableScopedIdentity(observedPath, observed.stat);
855
+ continue;
856
+ }
857
+ const image =
858
+ observed.value === affected.beforeImage
859
+ ? "before"
860
+ : observed.value === affected.afterImage || ownedScopedPostTree(observed, affected.postFiles ?? [], true)
861
+ ? "after"
862
+ : undefined;
863
+ if (!image) continue;
864
+ affected.scopedRemoval = {
865
+ image,
866
+ identity: mayChange ? stableScopedIdentity(observedPath, observed.stat) : scopedIdentity(observed.stat),
867
+ };
868
+ }
869
+ options.validateMutation?.();
870
+ options.signal?.throwIfAborted();
871
+ await persistJournal(directory, journal);
872
+ const targets = [entry.target, ...(entry.afterTarget ? [entry.afterTarget] : [])];
873
+ return await withSessionWriteProtection(targets, async () => {
874
+ const verified = await verifyTarget(directory, entry, journal, options);
875
+ options.validateMutation?.();
876
+ options.signal?.throwIfAborted();
877
+ if (restoringBefore !== undefined && verified.value === restoringBefore) return false;
878
+ if (current.scoped) {
879
+ if (current.value !== verified.value) throw new Error("Transaction removal image changed.");
880
+ await assertUnexcludedTargets(journal.root, [entry.target], options);
881
+ await removeScopedImage(entry.target, current.scoped, options);
882
+ } else {
883
+ options.validateMutation?.();
884
+ options.signal?.throwIfAborted();
885
+ await fs.rm(entry.target, { recursive: true, force: true });
886
+ }
887
+ await syncDirectory(path.dirname(entry.target));
888
+ return true;
889
+ });
890
+ } catch (error) {
891
+ if (!scoped) throw error;
892
+ throw new ScopedRemovalError("Scoped transaction removal stopped; preserve transaction and backup for review.", {
893
+ cause: error,
894
+ });
895
+ }
896
+ }
897
+
898
+ function upgradeRemovalEvidence(journal: TransactionJournal) {
899
+ if (journal.version < JOURNAL_VERSION) {
900
+ for (const entry of journal.entries) entry.removalPending = false;
901
+ journal.version = JOURNAL_VERSION;
902
+ }
903
+ }
904
+
905
+ async function retireRemovalIntent(
906
+ directory: string,
907
+ journal: TransactionJournal,
908
+ entries: TransactionEntry[],
909
+ options: TransactionOptions,
910
+ ) {
911
+ options.validateMutation?.();
912
+ options.signal?.throwIfAborted();
913
+ if (!entries.length) return;
914
+ upgradeRemovalEvidence(journal);
915
+ for (const entry of entries) {
916
+ entry.removalPending = false;
917
+ entry.replacementStarted = true;
918
+ delete entry.scopedRemoval;
919
+ }
920
+ await writeJson(path.join(directory, "journal.json"), journal, { maxBytes: MAX_TRANSACTION_JOURNAL_BYTES });
921
+ options.validateMutation?.();
922
+ options.signal?.throwIfAborted();
923
+ }
924
+
925
+ async function retireRestorationIntent(
926
+ directory: string,
927
+ journal: TransactionJournal,
928
+ entry: TransactionEntry,
929
+ options: TransactionOptions,
930
+ ) {
931
+ for (const child of journal.entries) {
932
+ if (isStrictlyInside(entry.target, child.target)) {
933
+ child.removalPending = false;
934
+ child.replacementStarted = false;
935
+ }
936
+ }
937
+ await retireRemovalIntent(directory, journal, [entry], options);
938
+ }
939
+
940
+ function persistJournal(directory: string, journal: TransactionJournal) {
941
+ return writeJson(path.join(directory, "journal.json"), journal, { maxBytes: MAX_TRANSACTION_JOURNAL_BYTES });
942
+ }
943
+
944
+ async function completeTransaction(directory: string, journal: TransactionJournal) {
945
+ await writeJson(
946
+ path.join(directory, "journal.json"),
947
+ {
948
+ ...journal,
949
+ version: isScopedJournal(journal) ? journal.version : journal.sessionRootTransition ? 7 : 6,
950
+ completed: true,
951
+ },
952
+ {
953
+ maxBytes: MAX_TRANSACTION_JOURNAL_BYTES,
954
+ },
955
+ );
956
+ await removeTransaction(directory);
957
+ }
958
+ async function removeTransaction(directory: string) {
959
+ await fs.rm(directory, { recursive: true, force: true });
960
+ await syncDirectory(transactionRoot()).catch(() => {});
961
+ }