@narumitw/pi-sync 0.58.1 → 0.60.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 (334) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +28 -15
  3. package/dist/chunks/{chunk-AMJRUXFT.ts → chunk-2JNFEZ3V.ts} +2 -6
  4. package/dist/chunks/{chunk-AMJRUXFT.ts.map → chunk-2JNFEZ3V.ts.map} +1 -1
  5. package/dist/chunks/{chunk-MG66XM5U.ts → chunk-4RNCZZY7.ts} +86 -36
  6. package/dist/chunks/chunk-4RNCZZY7.ts.map +7 -0
  7. package/dist/chunks/{config-HYG7G5EM.ts → chunk-7AC2RX4M.ts} +6 -12
  8. package/dist/chunks/chunk-7AC2RX4M.ts.map +7 -0
  9. package/dist/chunks/{chunk-XEODG2PE.ts → chunk-BGGKHHO4.ts} +18 -14
  10. package/dist/chunks/{chunk-XEODG2PE.ts.map → chunk-BGGKHHO4.ts.map} +1 -1
  11. package/dist/chunks/chunk-BWWTOQOW.ts +737 -0
  12. package/dist/chunks/chunk-BWWTOQOW.ts.map +7 -0
  13. package/dist/chunks/chunk-CEMFCWIU.ts +20 -0
  14. package/dist/chunks/chunk-CEMFCWIU.ts.map +7 -0
  15. package/dist/chunks/chunk-CZNBKIM5.ts +20 -0
  16. package/dist/chunks/chunk-CZNBKIM5.ts.map +7 -0
  17. package/dist/chunks/{chunk-YIPK23JW.ts → chunk-ENFHHB5O.ts} +1 -16
  18. package/dist/chunks/{chunk-YIPK23JW.ts.map → chunk-ENFHHB5O.ts.map} +1 -1
  19. package/dist/chunks/{chunk-KHBPED6W.ts → chunk-GSQGBVKG.ts} +2 -2
  20. package/dist/chunks/chunk-HNKLBM3U.ts +18 -0
  21. package/dist/chunks/chunk-HNKLBM3U.ts.map +7 -0
  22. package/dist/chunks/{chunk-QPC5HT3A.ts → chunk-J5COV5TY.ts} +21 -2
  23. package/dist/chunks/chunk-J5COV5TY.ts.map +7 -0
  24. package/dist/chunks/chunk-JKBUYCPC.ts +52 -0
  25. package/dist/chunks/chunk-JKBUYCPC.ts.map +7 -0
  26. package/dist/chunks/chunk-JZKCOM62.ts +26 -0
  27. package/dist/chunks/chunk-JZKCOM62.ts.map +7 -0
  28. package/dist/chunks/chunk-M7RHV3H6.ts +33 -0
  29. package/dist/chunks/chunk-M7RHV3H6.ts.map +7 -0
  30. package/dist/chunks/{chunk-REMGB255.ts → chunk-MIXZVHFR.ts} +35 -10
  31. package/dist/chunks/chunk-MIXZVHFR.ts.map +7 -0
  32. package/dist/chunks/chunk-NPT2G4FU.ts +15 -0
  33. package/dist/chunks/chunk-NPT2G4FU.ts.map +7 -0
  34. package/dist/chunks/chunk-PDBTD2K6.ts +11 -0
  35. package/dist/chunks/chunk-PDBTD2K6.ts.map +7 -0
  36. package/dist/chunks/chunk-UBE6XWOM.ts +29 -0
  37. package/dist/chunks/chunk-UBE6XWOM.ts.map +7 -0
  38. package/dist/chunks/{chunk-JYILGR3K.ts → chunk-UZUUUY7C.ts} +8 -8
  39. package/dist/chunks/chunk-VZCWYXRS.ts +23 -0
  40. package/dist/chunks/chunk-VZCWYXRS.ts.map +7 -0
  41. package/dist/chunks/{chunk-U6GXGY5G.ts → chunk-YWEYRRQI.ts} +45 -37
  42. package/dist/chunks/chunk-YWEYRRQI.ts.map +7 -0
  43. package/dist/chunks/{chunk-ECYCNZOE.ts → chunk-ZMNVNECV.ts} +5 -1
  44. package/dist/chunks/chunk-ZMNVNECV.ts.map +7 -0
  45. package/dist/chunks/{command-handler-MRXE5AKK.ts → command-handler-3O76V7Q5.ts} +83 -87
  46. package/dist/chunks/command-handler-3O76V7Q5.ts.map +7 -0
  47. package/dist/chunks/conflict-review-XBSME2ER.ts +18 -0
  48. package/dist/chunks/conflict-review-XBSME2ER.ts.map +7 -0
  49. package/dist/chunks/{continuous-record-EPHGR2DU.ts → continuous-record-ZHUXVO4E.ts} +5 -13
  50. package/dist/chunks/continuous-record-ZHUXVO4E.ts.map +7 -0
  51. package/dist/chunks/{file-selection-IELTXSGZ.ts → file-selection-S6CNNNQI.ts} +14 -14
  52. package/dist/chunks/file-selection-S6CNNNQI.ts.map +7 -0
  53. package/dist/chunks/manager-result-dispatcher-I2AMCCE7.ts +18 -0
  54. package/dist/chunks/{manager-ui-AY34N4UY.ts → manager-ui-SJOBTJLY.ts} +221 -147
  55. package/dist/chunks/manager-ui-SJOBTJLY.ts.map +7 -0
  56. package/dist/chunks/merge-review-SGDLMVS4.ts +10 -0
  57. package/dist/chunks/remote-selection-ui-GZHPDQ4L.ts +17 -0
  58. package/dist/chunks/{setup-switch-KLGESLQX.ts → setup-switch-M3BAFGJM.ts} +9 -12
  59. package/dist/chunks/{setup-wizard-ZQAK35BP.ts → setup-wizard-NFAWP4KF.ts} +18 -20
  60. package/dist/chunks/{setup-wizard-ZQAK35BP.ts.map → setup-wizard-NFAWP4KF.ts.map} +1 -1
  61. package/dist/chunks/{snapshot-RGNDYT6A.ts → snapshot-D4SFDP5J.ts} +8 -12
  62. package/dist/chunks/snapshot-D4SFDP5J.ts.map +7 -0
  63. package/dist/chunks/snapshot-transaction-directory-TNKBQ3UQ.ts +13 -0
  64. package/dist/chunks/snapshot-transaction-directory-TNKBQ3UQ.ts.map +7 -0
  65. package/dist/chunks/state-directory-JZSZVVPX.ts +17 -0
  66. package/dist/chunks/sync-inspection-BJ4KPPNO.ts +23 -0
  67. package/dist/chunks/sync-inspection-BJ4KPPNO.ts.map +7 -0
  68. package/dist/chunks/sync-operations-TLWER2TT.ts +45 -0
  69. package/dist/chunks/sync-operations-TLWER2TT.ts.map +7 -0
  70. package/dist/chunks/{sync-state-7L4NT7W2.ts → sync-state-FVCJJ7MY.ts} +5 -10
  71. package/dist/chunks/sync-state-FVCJJ7MY.ts.map +7 -0
  72. package/dist/index.ts +269 -306
  73. package/dist/index.ts.map +4 -4
  74. package/docs/daemon-migration.md +93 -0
  75. package/docs/exclude-policy-audit.md +54 -0
  76. package/docs/exclude-review-ledger.md +36 -0
  77. package/docs/tui-contract.md +1 -1
  78. package/package.json +3 -5
  79. package/skills/configure-pi-sync/SKILL.md +5 -4
  80. package/skills/configure-pi-sync/references/configuration.md +91 -19
  81. package/src/backends/backend-factory.ts +2 -23
  82. package/src/backends/backend-identity.ts +5 -40
  83. package/src/backends/backend-types.ts +1 -81
  84. package/src/backends/git/git-backend.ts +6 -784
  85. package/src/backends/git/git-config.ts +8 -130
  86. package/src/backends/git/git-runner.ts +7 -255
  87. package/src/backends/git/git-storage.ts +14 -292
  88. package/src/backends/s3/s3-backend.ts +11 -523
  89. package/src/backends/s3/s3-client.ts +2 -399
  90. package/src/backends/s3/s3-conditions.ts +2 -67
  91. package/src/backends/snapshot-pointer.ts +2 -27
  92. package/src/backends/sync-backend.ts +6 -101
  93. package/src/backends/webdav/webdav-backend.ts +10 -622
  94. package/src/backends/webdav/webdav-client.ts +6 -370
  95. package/src/backends/webdav/webdav-config.ts +8 -82
  96. package/src/commands/command-execution.ts +42 -38
  97. package/src/commands/command.ts +23 -1
  98. package/src/daemon/apply-gate.ts +146 -0
  99. package/src/daemon/pi-client.ts +591 -0
  100. package/src/daemon/recovery.ts +103 -0
  101. package/src/paths.ts +12 -83
  102. package/src/settings/config-errors.ts +2 -6
  103. package/src/settings/config-file.ts +19 -693
  104. package/src/settings/config.ts +16 -257
  105. package/src/settings/settings-management.ts +11 -253
  106. package/src/settings/settings-store.ts +10 -48
  107. package/src/settings/settings-types.ts +1 -154
  108. package/src/settings/settings-validation.ts +17 -431
  109. package/src/snapshot/session-paths.ts +11 -110
  110. package/src/snapshot/snapshot-apply.ts +9 -306
  111. package/src/snapshot/snapshot-case-replacement.ts +6 -80
  112. package/src/snapshot/snapshot-codec.ts +2 -50
  113. package/src/snapshot/snapshot-exclude-safety.ts +2 -0
  114. package/src/snapshot/snapshot-mutation-queues.ts +6 -142
  115. package/src/snapshot/snapshot-paths.ts +2 -11
  116. package/src/snapshot/snapshot-root-transition.ts +6 -154
  117. package/src/snapshot/snapshot-transaction-directory.ts +5 -16
  118. package/src/snapshot/snapshot-transaction-plan.ts +2 -72
  119. package/src/snapshot/snapshot-transaction.ts +6 -934
  120. package/src/snapshot/snapshot-types.ts +1 -43
  121. package/src/snapshot/snapshot.ts +18 -405
  122. package/src/state/json-file.ts +2 -47
  123. package/src/state/lock-policy.ts +2 -2
  124. package/src/state/lock.ts +10 -292
  125. package/src/state/lockfile-fs.ts +2 -25
  126. package/src/state/merge-baseline-store.ts +8 -200
  127. package/src/state/mutation-directory-sync.ts +2 -34
  128. package/src/state/operation-availability.ts +7 -59
  129. package/src/state/state-directory.ts +8 -243
  130. package/src/state/state-types.ts +1 -24
  131. package/src/state/sync-state-store.ts +11 -75
  132. package/src/sync/agent-candidate.ts +7 -256
  133. package/src/sync/agent-plan.ts +2 -107
  134. package/src/sync/agent-policy.ts +10 -13
  135. package/src/sync/agent-refusal-retry.ts +2 -54
  136. package/src/sync/agent-request.ts +2 -19
  137. package/src/sync/agent-runner.ts +2 -191
  138. package/src/sync/agent-store.ts +7 -182
  139. package/src/sync/agent-sync.ts +2 -141
  140. package/src/sync/apply-review.ts +2 -63
  141. package/src/sync/automatic-sync.ts +55 -106
  142. package/src/sync/conflict-artifact-ownership.ts +2 -63
  143. package/src/sync/conflict-artifacts.ts +8 -195
  144. package/src/sync/conflict-groups.ts +2 -68
  145. package/src/sync/conflict-preview.ts +2 -56
  146. package/src/sync/conflict-resolution.ts +2 -93
  147. package/src/sync/conflict-retention.ts +2 -72
  148. package/src/sync/conflict-review.ts +3 -92
  149. package/src/sync/content-conflicts.ts +2 -73
  150. package/src/sync/continuous-errors.ts +7 -85
  151. package/src/sync/continuous-lease.ts +2 -216
  152. package/src/sync/continuous-record.ts +10 -261
  153. package/src/sync/continuous-storage.ts +12 -64
  154. package/src/sync/continuous-sync.ts +2 -563
  155. package/src/sync/continuous-watch.ts +2 -59
  156. package/src/sync/exclude-policy.ts +6 -0
  157. package/src/sync/field-migration.ts +2 -39
  158. package/src/sync/file-merge-planner.ts +2 -143
  159. package/src/sync/json-document.ts +7 -184
  160. package/src/sync/local-field-policy.ts +2 -29
  161. package/src/sync/local-fields.ts +9 -108
  162. package/src/sync/merge-apply.ts +2 -434
  163. package/src/sync/merge-journal.ts +10 -219
  164. package/src/sync/merge-transfer-error.ts +2 -72
  165. package/src/sync/merged-sync.ts +2 -914
  166. package/src/sync/mutation-owner.ts +2 -22
  167. package/src/sync/package-merge.ts +2 -87
  168. package/src/sync/partial-progress.ts +2 -54
  169. package/src/sync/remote-snapshot.ts +7 -80
  170. package/src/sync/session-merge.ts +2 -194
  171. package/src/sync/settings-conflicts.ts +2 -64
  172. package/src/sync/settings-merge.ts +6 -121
  173. package/src/sync/signals.ts +2 -8
  174. package/src/sync/startup-check.ts +15 -31
  175. package/src/sync/startup-timings.ts +2 -94
  176. package/src/sync/sync-decision.ts +3 -101
  177. package/src/sync/sync-error-guidance.ts +2 -46
  178. package/src/sync/sync-errors.ts +7 -23
  179. package/src/sync/sync-inspection.ts +10 -57
  180. package/src/sync/sync-local.ts +2 -44
  181. package/src/sync/sync-mutations.ts +10 -781
  182. package/src/sync/sync-operations.ts +32 -11
  183. package/src/sync/sync-policy.ts +34 -410
  184. package/src/sync/sync-queries.ts +2 -237
  185. package/src/sync/sync-state.ts +18 -151
  186. package/src/sync/text-merge.ts +10 -133
  187. package/src/sync-extension.ts +192 -142
  188. package/src/ui/excluded-paths-ui.ts +106 -0
  189. package/src/ui/file-selection.ts +6 -3
  190. package/src/ui/manager-state.ts +11 -5
  191. package/src/ui/remote-selection-ui.ts +73 -10
  192. package/src/ui/settings-ui.ts +20 -5
  193. package/src/ui/setup/setup-actions.ts +1 -1
  194. package/src/ui/setup/setup-switcher.ts +3 -2
  195. package/src/ui/storage-connections-ui.ts +2 -2
  196. package/src/ui/sync-attention.ts +17 -3
  197. package/src/ui/sync-format.ts +13 -1
  198. package/dist/chunks/agent-candidate-U2NU3GUX.ts +0 -29
  199. package/dist/chunks/agent-plan-3YXDIJXO.ts +0 -123
  200. package/dist/chunks/agent-plan-3YXDIJXO.ts.map +0 -7
  201. package/dist/chunks/agent-refusal-retry-476YGNU5.ts +0 -72
  202. package/dist/chunks/agent-refusal-retry-476YGNU5.ts.map +0 -7
  203. package/dist/chunks/agent-runner-FCRV2Z75.ts +0 -185
  204. package/dist/chunks/agent-runner-FCRV2Z75.ts.map +0 -7
  205. package/dist/chunks/agent-store-7GINKTS7.ts +0 -175
  206. package/dist/chunks/agent-store-7GINKTS7.ts.map +0 -7
  207. package/dist/chunks/agent-sync-2R4GG2MF.ts +0 -22
  208. package/dist/chunks/chunk-2JZFHNLM.ts +0 -113
  209. package/dist/chunks/chunk-2JZFHNLM.ts.map +0 -7
  210. package/dist/chunks/chunk-3ST7RSYN.ts +0 -21
  211. package/dist/chunks/chunk-3ST7RSYN.ts.map +0 -7
  212. package/dist/chunks/chunk-45COPAK7.ts +0 -205
  213. package/dist/chunks/chunk-45COPAK7.ts.map +0 -7
  214. package/dist/chunks/chunk-5SFMBY5P.ts +0 -26
  215. package/dist/chunks/chunk-5SFMBY5P.ts.map +0 -7
  216. package/dist/chunks/chunk-5TSCBBPP.ts +0 -229
  217. package/dist/chunks/chunk-5TSCBBPP.ts.map +0 -7
  218. package/dist/chunks/chunk-6LD5KX55.ts +0 -396
  219. package/dist/chunks/chunk-6LD5KX55.ts.map +0 -7
  220. package/dist/chunks/chunk-6O3EDBRL.ts +0 -258
  221. package/dist/chunks/chunk-6O3EDBRL.ts.map +0 -7
  222. package/dist/chunks/chunk-7A35ZAVO.ts +0 -1109
  223. package/dist/chunks/chunk-7A35ZAVO.ts.map +0 -7
  224. package/dist/chunks/chunk-7D5SF7P3.ts +0 -29
  225. package/dist/chunks/chunk-7D5SF7P3.ts.map +0 -7
  226. package/dist/chunks/chunk-7ZIYU5QS.ts +0 -64
  227. package/dist/chunks/chunk-7ZIYU5QS.ts.map +0 -7
  228. package/dist/chunks/chunk-AKMV47YC.ts +0 -215
  229. package/dist/chunks/chunk-AKMV47YC.ts.map +0 -7
  230. package/dist/chunks/chunk-AR2DGDUI.ts +0 -55
  231. package/dist/chunks/chunk-AR2DGDUI.ts.map +0 -7
  232. package/dist/chunks/chunk-BEST45H2.ts +0 -153
  233. package/dist/chunks/chunk-BEST45H2.ts.map +0 -7
  234. package/dist/chunks/chunk-CBLTWXAU.ts +0 -114
  235. package/dist/chunks/chunk-CBLTWXAU.ts.map +0 -7
  236. package/dist/chunks/chunk-CQD2TJHE.ts +0 -220
  237. package/dist/chunks/chunk-CQD2TJHE.ts.map +0 -7
  238. package/dist/chunks/chunk-DN4IA7MR.ts +0 -61
  239. package/dist/chunks/chunk-DN4IA7MR.ts.map +0 -7
  240. package/dist/chunks/chunk-DT7M4VQS.ts +0 -69
  241. package/dist/chunks/chunk-DT7M4VQS.ts.map +0 -7
  242. package/dist/chunks/chunk-ECYCNZOE.ts.map +0 -7
  243. package/dist/chunks/chunk-ESKOPQVB.ts +0 -348
  244. package/dist/chunks/chunk-ESKOPQVB.ts.map +0 -7
  245. package/dist/chunks/chunk-FHE2DCA2.ts +0 -61
  246. package/dist/chunks/chunk-FHE2DCA2.ts.map +0 -7
  247. package/dist/chunks/chunk-FLHCOJCD.ts +0 -57
  248. package/dist/chunks/chunk-FLHCOJCD.ts.map +0 -7
  249. package/dist/chunks/chunk-G4RUXRWZ.ts +0 -48
  250. package/dist/chunks/chunk-G4RUXRWZ.ts.map +0 -7
  251. package/dist/chunks/chunk-GNYGNHFF.ts +0 -27
  252. package/dist/chunks/chunk-GNYGNHFF.ts.map +0 -7
  253. package/dist/chunks/chunk-KYTT7KV4.ts +0 -319
  254. package/dist/chunks/chunk-KYTT7KV4.ts.map +0 -7
  255. package/dist/chunks/chunk-M6VQJIOU.ts +0 -54
  256. package/dist/chunks/chunk-M6VQJIOU.ts.map +0 -7
  257. package/dist/chunks/chunk-MG66XM5U.ts.map +0 -7
  258. package/dist/chunks/chunk-MKSMJPVA.ts +0 -107
  259. package/dist/chunks/chunk-MKSMJPVA.ts.map +0 -7
  260. package/dist/chunks/chunk-MMQIEDZ6.ts +0 -1994
  261. package/dist/chunks/chunk-MMQIEDZ6.ts.map +0 -7
  262. package/dist/chunks/chunk-OJARL2TG.ts +0 -25
  263. package/dist/chunks/chunk-OJARL2TG.ts.map +0 -7
  264. package/dist/chunks/chunk-OVBAW7KV.ts +0 -1174
  265. package/dist/chunks/chunk-OVBAW7KV.ts.map +0 -7
  266. package/dist/chunks/chunk-PRUEIIK7.ts +0 -24
  267. package/dist/chunks/chunk-PRUEIIK7.ts.map +0 -7
  268. package/dist/chunks/chunk-PWMWOJP4.ts +0 -17
  269. package/dist/chunks/chunk-PWMWOJP4.ts.map +0 -7
  270. package/dist/chunks/chunk-QPC5HT3A.ts.map +0 -7
  271. package/dist/chunks/chunk-REMGB255.ts.map +0 -7
  272. package/dist/chunks/chunk-S5OCYVPS.ts +0 -415
  273. package/dist/chunks/chunk-S5OCYVPS.ts.map +0 -7
  274. package/dist/chunks/chunk-SWRVXVFD.ts +0 -248
  275. package/dist/chunks/chunk-SWRVXVFD.ts.map +0 -7
  276. package/dist/chunks/chunk-T2SJD3N7.ts +0 -99
  277. package/dist/chunks/chunk-T2SJD3N7.ts.map +0 -7
  278. package/dist/chunks/chunk-TIMWN2RH.ts +0 -89
  279. package/dist/chunks/chunk-TIMWN2RH.ts.map +0 -7
  280. package/dist/chunks/chunk-TZC7GDJE.ts +0 -58
  281. package/dist/chunks/chunk-TZC7GDJE.ts.map +0 -7
  282. package/dist/chunks/chunk-U6GXGY5G.ts.map +0 -7
  283. package/dist/chunks/chunk-WQMYV4DW.ts +0 -140
  284. package/dist/chunks/chunk-WQMYV4DW.ts.map +0 -7
  285. package/dist/chunks/chunk-X7R6DCVF.ts +0 -28
  286. package/dist/chunks/chunk-X7R6DCVF.ts.map +0 -7
  287. package/dist/chunks/chunk-XJHQPB3E.ts +0 -107
  288. package/dist/chunks/chunk-XJHQPB3E.ts.map +0 -7
  289. package/dist/chunks/chunk-Y4UVXOLQ.ts +0 -123
  290. package/dist/chunks/chunk-Y4UVXOLQ.ts.map +0 -7
  291. package/dist/chunks/chunk-YDVQYUHE.ts +0 -34
  292. package/dist/chunks/chunk-YDVQYUHE.ts.map +0 -7
  293. package/dist/chunks/chunk-YHT4YTYG.ts +0 -31
  294. package/dist/chunks/chunk-YHT4YTYG.ts.map +0 -7
  295. package/dist/chunks/chunk-ZW5ZLGBI.ts +0 -154
  296. package/dist/chunks/chunk-ZW5ZLGBI.ts.map +0 -7
  297. package/dist/chunks/command-handler-MRXE5AKK.ts.map +0 -7
  298. package/dist/chunks/conflict-artifacts-J6WSDCWP.ts +0 -32
  299. package/dist/chunks/conflict-review-GXA6Y7KY.ts +0 -189
  300. package/dist/chunks/conflict-review-GXA6Y7KY.ts.map +0 -7
  301. package/dist/chunks/continuous-sync-FAFNLEUW.ts +0 -585
  302. package/dist/chunks/continuous-sync-FAFNLEUW.ts.map +0 -7
  303. package/dist/chunks/file-selection-IELTXSGZ.ts.map +0 -7
  304. package/dist/chunks/git-backend-KHD36W27.ts +0 -1077
  305. package/dist/chunks/git-backend-KHD36W27.ts.map +0 -7
  306. package/dist/chunks/local-fields-JEKAPZKD.ts +0 -26
  307. package/dist/chunks/local-fields-JEKAPZKD.ts.map +0 -7
  308. package/dist/chunks/manager-result-dispatcher-XCYPW7RC.ts +0 -32
  309. package/dist/chunks/manager-result-dispatcher-XCYPW7RC.ts.map +0 -7
  310. package/dist/chunks/manager-ui-AY34N4UY.ts.map +0 -7
  311. package/dist/chunks/merge-baseline-store-65GKUMXI.ts +0 -25
  312. package/dist/chunks/merge-baseline-store-65GKUMXI.ts.map +0 -7
  313. package/dist/chunks/remote-selection-ui-MYM2YCPF.ts +0 -31
  314. package/dist/chunks/remote-selection-ui-MYM2YCPF.ts.map +0 -7
  315. package/dist/chunks/s3-backend-L24UZ6IK.ts +0 -844
  316. package/dist/chunks/s3-backend-L24UZ6IK.ts.map +0 -7
  317. package/dist/chunks/setup-switch-KLGESLQX.ts.map +0 -7
  318. package/dist/chunks/snapshot-RGNDYT6A.ts.map +0 -7
  319. package/dist/chunks/snapshot-transaction-26BQO36S.ts +0 -24
  320. package/dist/chunks/snapshot-transaction-26BQO36S.ts.map +0 -7
  321. package/dist/chunks/sync-inspection-MOMLGUTD.ts +0 -25
  322. package/dist/chunks/sync-inspection-MOMLGUTD.ts.map +0 -7
  323. package/dist/chunks/sync-operations-45APGOL3.ts +0 -1021
  324. package/dist/chunks/sync-operations-45APGOL3.ts.map +0 -7
  325. package/dist/chunks/sync-state-7L4NT7W2.ts.map +0 -7
  326. package/dist/chunks/webdav-backend-3ZCACILP.ts +0 -851
  327. package/dist/chunks/webdav-backend-3ZCACILP.ts.map +0 -7
  328. /package/dist/chunks/{chunk-KHBPED6W.ts.map → chunk-GSQGBVKG.ts.map} +0 -0
  329. /package/dist/chunks/{chunk-JYILGR3K.ts.map → chunk-UZUUUY7C.ts.map} +0 -0
  330. /package/dist/chunks/{agent-candidate-U2NU3GUX.ts.map → manager-result-dispatcher-I2AMCCE7.ts.map} +0 -0
  331. /package/dist/chunks/{agent-sync-2R4GG2MF.ts.map → merge-review-SGDLMVS4.ts.map} +0 -0
  332. /package/dist/chunks/{config-HYG7G5EM.ts.map → remote-selection-ui-GZHPDQ4L.ts.map} +0 -0
  333. /package/dist/chunks/{conflict-artifacts-J6WSDCWP.ts.map → setup-switch-M3BAFGJM.ts.map} +0 -0
  334. /package/dist/chunks/{continuous-record-EPHGR2DU.ts.map → state-directory-JZSZVVPX.ts.map} +0 -0
package/dist/index.ts CHANGED
@@ -1,175 +1,121 @@
1
1
  // @generated by scripts/build-runtime.mjs; do not edit.
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
- createSyncAttentionController
5
- } from "./chunks/chunk-REMGB255.ts";
4
+ createSyncAttentionController,
5
+ readStateForConfig,
6
+ syncStateFingerprint
7
+ } from "./chunks/chunk-MIXZVHFR.ts";
8
+ import "./chunks/chunk-NPT2G4FU.ts";
6
9
  import {
7
- createStartupTimings
8
- } from "./chunks/chunk-M6VQJIOU.ts";
10
+ withStateDirectoryAccess
11
+ } from "./chunks/chunk-CEMFCWIU.ts";
9
12
  import {
10
- completeSyncArguments,
11
- setSyncSetupCompletions,
12
- splitArgs
13
- } from "./chunks/chunk-QPC5HT3A.ts";
13
+ snapshotOptionsForContext
14
+ } from "./chunks/chunk-CZNBKIM5.ts";
14
15
  import {
15
- captureMutationOwner
16
- } from "./chunks/chunk-GNYGNHFF.ts";
16
+ loadConfigForCheck,
17
+ syncCheckConfigFingerprint
18
+ } from "./chunks/chunk-7AC2RX4M.ts";
17
19
  import {
18
- safeTerminalText
19
- } from "./chunks/chunk-AMJRUXFT.ts";
20
+ bindPiDaemonClient,
21
+ installPiDaemonHost,
22
+ piDaemonClient,
23
+ withPiDaemonClient
24
+ } from "./chunks/chunk-BWWTOQOW.ts";
20
25
  import {
21
26
  configureSyncStatus,
22
27
  createSyncStatusPublisher,
23
28
  setSyncStatus
24
- } from "./chunks/chunk-YIPK23JW.ts";
25
- import {
26
- errorMessage
27
- } from "./chunks/chunk-7D5SF7P3.ts";
28
- import {
29
- consumeLocalConfigMigrationNotice,
30
- loadConfig,
31
- loadConfigForCheck,
32
- loadPartialConfig,
33
- readLocalConfigObject,
34
- syncCheckConfigFingerprint
35
- } from "./chunks/chunk-OVBAW7KV.ts";
36
- import "./chunks/chunk-X7R6DCVF.ts";
37
- import {
38
- pendingTransactionEntries
39
- } from "./chunks/chunk-5SFMBY5P.ts";
29
+ } from "./chunks/chunk-ENFHHB5O.ts";
40
30
  import {
41
- withLock
42
- } from "./chunks/chunk-6O3EDBRL.ts";
31
+ completeSyncArguments,
32
+ setSyncSetupCompletions,
33
+ splitArgs
34
+ } from "./chunks/chunk-J5COV5TY.ts";
43
35
  import {
44
36
  combineSignals,
45
37
  throwIfAborted
46
- } from "./chunks/chunk-PWMWOJP4.ts";
47
- import {
48
- ensureStateDir,
49
- readStateForConfig,
50
- syncStateFingerprint
51
- } from "./chunks/chunk-DN4IA7MR.ts";
52
- import "./chunks/chunk-AR2DGDUI.ts";
38
+ } from "./chunks/chunk-PDBTD2K6.ts";
53
39
  import {
54
- stateDirectoryMigrationNotice,
55
- withStateDirectoryAccess
56
- } from "./chunks/chunk-SWRVXVFD.ts";
57
- import "./chunks/chunk-2JZFHNLM.ts";
58
- import "./chunks/chunk-T2SJD3N7.ts";
59
- import "./chunks/chunk-KYTT7KV4.ts";
40
+ errorMessage
41
+ } from "./chunks/chunk-HNKLBM3U.ts";
60
42
  import {
61
- sessionDirFromContext,
62
- snapshotOptionsForContext
63
- } from "./chunks/chunk-CBLTWXAU.ts";
64
- import "./chunks/chunk-FHE2DCA2.ts";
43
+ safeTerminalText
44
+ } from "./chunks/chunk-2JNFEZ3V.ts";
45
+ import "./chunks/chunk-JKBUYCPC.ts";
46
+
47
+ // src/sync/agent-policy.ts
65
48
  import {
49
+ AGENT_INPUT_BYTES,
50
+ AGENT_MAX_ATTEMPTS,
51
+ AGENT_MAX_TOKENS,
52
+ AGENT_MAX_UNITS,
53
+ AGENT_OUTPUT_BYTES,
54
+ AGENT_TIMEOUT_MS,
66
55
  startupTransferEnabled
67
- } from "./chunks/chunk-PRUEIIK7.ts";
56
+ } from "@narumitw/sync-daemon/engine/sync/agent-policy";
68
57
 
69
58
  // src/settings/config-errors.ts
70
- function isMissingConfigError(error) {
71
- return error instanceof Error && (error.message.startsWith("Missing pi-sync settings.") || error.message === "No sync setups are configured.");
72
- }
59
+ import { isMissingConfigError } from "@narumitw/sync-daemon/engine/settings/config-errors";
60
+
61
+ // src/sync/startup-timings.ts
62
+ import { createStartupTimings } from "@narumitw/sync-daemon/engine/sync/startup-timings";
73
63
 
74
64
  // src/sync/automatic-sync.ts
75
- var AUTO_SYNC_OPTIONS = {
76
- yes: true,
77
- force: false,
78
- stale: false,
79
- silent: true,
80
- reload: false,
81
- auto: true,
82
- args: []
83
- };
84
65
  async function startSession(ctx, signal, publish) {
85
- const timings = createStartupTimings(publish);
86
- let debug = false;
87
- let outcome = "stopped";
88
- try {
89
- throwIfAborted(signal);
90
- const stateNotice = stateDirectoryMigrationNotice();
91
- if (stateNotice && ctx.hasUI) ctx.ui.notify(stateNotice, "warning");
92
- const validateMutation = captureMutationOwner(ctx, signal);
93
- const currentFile = ctx.sessionManager.getSessionFile?.();
94
- await timings.measure("transaction recovery", async () => {
95
- const pending = await pendingTransactionEntries();
96
- validateMutation();
97
- if (!pending.some((entry) => entry.isDirectory())) return;
98
- const { recoverSnapshotTransactionsOnStartup } = await import("./chunks/snapshot-transaction-26BQO36S.ts");
99
- validateMutation();
100
- await recoverSnapshotTransactionsOnStartup({
101
- signal,
102
- sessionDir: sessionDirFromContext(ctx),
103
- protectedTargets: currentFile ? [currentFile] : [],
104
- validateMutation
105
- });
106
- });
66
+ throwIfAborted(signal);
67
+ const client = piDaemonClient(ctx);
68
+ if (!ctx.hasUI || !["tui", "rpc"].includes(ctx.mode)) {
69
+ await client.attachExisting();
107
70
  throwIfAborted(signal);
108
- try {
109
- const settings = await timings.measure("settings read", () => readLocalConfigObject());
110
- throwIfAborted(signal);
111
- debug = settings?.debug ?? false;
112
- const names = settings ? Object.keys(settings.syncSetups).sort((left, right) => left.localeCompare(right)) : [];
113
- setSyncSetupCompletions(names);
114
- configureSyncStatus(ctx, settings?.showStatus ?? true);
115
- } catch {
116
- if (signal.aborted) return;
117
- setSyncSetupCompletions([]);
118
- }
119
- const migrationNotice = consumeLocalConfigMigrationNotice();
120
- if (migrationNotice && ctx.hasUI) ctx.ui.notify(migrationNotice, "warning");
121
- throwIfAborted(signal);
122
- if (!ctx.hasUI) return { debug };
123
- try {
124
- const config = await timings.measure("config resolution", () => loadConfig());
125
- throwIfAborted(signal);
126
- configureSyncStatus(ctx, config.showStatus);
127
- outcome = "completed";
128
- return { debug, config };
129
- } catch (error) {
130
- throwIfAborted(signal);
131
- if (!isMissingConfigError(error)) {
132
- ctx.ui.notify(`pi-sync startup check skipped: ${safeTerminalText(errorMessage(error))}`, "warning");
133
- }
134
- }
71
+ return { debug: false };
72
+ }
73
+ const timings = createStartupTimings(publish);
74
+ const document = await timings.measure("settings read", () => client.readSettings());
75
+ throwIfAborted(signal);
76
+ configureSyncStatus(ctx, document?.parsed.showStatus ?? true);
77
+ const { stateDirectoryMigrationNotice } = await import("./chunks/state-directory-JZSZVVPX.ts");
78
+ const notice = stateDirectoryMigrationNotice();
79
+ const { pendingTransactionEntries } = await import("./chunks/snapshot-transaction-directory-TNKBQ3UQ.ts");
80
+ const recoveryNeeded = (await pendingTransactionEntries()).some((entry) => entry.isDirectory());
81
+ throwIfAborted(signal);
82
+ if (!await client.configured() && !recoveryNeeded) {
83
+ if (notice) ctx.ui.notify(notice, "warning");
84
+ setSyncSetupCompletions([]);
85
+ const debug = document?.parsed.debug === true;
86
+ await client.markReady(debug);
87
+ timings.publish(ctx, debug, signal, "local initialization", "completed");
135
88
  return { debug };
136
- } finally {
137
- timings.publish(ctx, debug, signal, "local initialization", outcome);
138
89
  }
90
+ throwIfAborted(signal);
91
+ const result = await client.run("initialize", {}, signal);
92
+ throwIfAborted(signal);
93
+ configureSyncStatus(ctx, result.settings?.showStatus ?? true);
94
+ setSyncSetupCompletions(Object.keys(result.settings?.syncSetups ?? {}).sort((a, b) => a.localeCompare(b)));
95
+ await client.markReady(result.debug);
96
+ throwIfAborted(signal);
97
+ if (result.handoverRequired) {
98
+ ctx.ui.notify(
99
+ "pi-sync: close or upgrade older Pi processes, then use /sync to review daemon handover. No automatic transfer is authorized yet.",
100
+ "warning"
101
+ );
102
+ return { debug: result.debug };
103
+ }
104
+ return { debug: result.debug, config: result.config };
139
105
  }
140
- async function autoPushSessions(ctx, signal, loaders) {
106
+ async function autoPushSessions(ctx, signal, _loaders) {
141
107
  try {
142
- const partial = await loadPartialConfig();
143
- throwIfAborted(signal);
144
- if (!partial.automatic) return;
145
- if (!partial.include.includes("sessions")) return;
146
- await ensureStateDir();
108
+ const client = piDaemonClient(ctx);
109
+ if (!await client.configured()) return;
147
110
  throwIfAborted(signal);
148
- const config = await loadConfig();
149
- throwIfAborted(signal);
150
- if (!config.include.includes("sessions")) return;
151
- const [operations, snapshotModule, syncStateModule] = await Promise.all([
152
- loaders.operations(),
153
- loaders.snapshot(),
154
- loaders.syncState()
155
- ]);
156
- throwIfAborted(signal);
157
- await withLock("auto-session-push", async () => {
158
- throwIfAborted(signal);
159
- const state = await readStateForConfig(config);
160
- throwIfAborted(signal);
161
- const local = await snapshotModule.createSnapshot(config.snapshotIdentity, {
162
- ...snapshotOptionsForContext(ctx, config),
163
- signal
164
- });
165
- throwIfAborted(signal);
166
- if (!syncStateModule.hasLocalChanges(local, state, config)) return;
167
- await operations.push(ctx, { ...AUTO_SYNC_OPTIONS, signal }, { config, state, local });
168
- });
111
+ await client.run(
112
+ "shutdown",
113
+ { args: [], yes: true, force: false, stale: false, silent: true, reload: false },
114
+ signal
115
+ );
169
116
  } catch (error) {
170
117
  if (signal.aborted || isMissingConfigError(error)) return;
171
- setSyncStatus(ctx, void 0);
172
- ctx.ui.notify(`pi-sync session push skipped: ${errorMessage(error)}`, "warning");
118
+ ctx.ui.notify(`pi-sync session push skipped: ${safeTerminalText(errorMessage(error))}`, "warning");
173
119
  }
174
120
  }
175
121
 
@@ -234,13 +180,7 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
234
180
  if (syncCheckConfigFingerprint(currentConfig) !== identity) return;
235
181
  const inspection = await timings.measure(
236
182
  "inspection and guards",
237
- () => withStateDirectoryAccess(() => {
238
- throwIfAborted(signal);
239
- return withLock("startup-check", () => {
240
- throwIfAborted(signal);
241
- return inspectSync(captured, snapshotOptionsForContext(ctx, captured), signal, void 0, timings);
242
- });
243
- })
183
+ () => inspectSync(captured, snapshotOptionsForContext(ctx, captured), signal, void 0, timings)
244
184
  );
245
185
  throwIfAborted(signal);
246
186
  const latest = await loadConfigForCheck();
@@ -257,10 +197,7 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
257
197
  automaticTransfer: startupTransferEnabled(captured)
258
198
  });
259
199
  if (startupTransferEnabled(captured) && !inspection.firstSync && inspection.head && inspection.selectionState?.kind === "same") {
260
- const [operations, { withAgentSyncLock }] = await timings.measure(
261
- "operations import",
262
- () => Promise.all([loaders.operations(), import("./chunks/agent-sync-2R4GG2MF.ts")])
263
- );
200
+ const operations = await timings.measure("operations import", () => loaders.operations());
264
201
  throwIfAborted(signal);
265
202
  if (!isCurrent()) return;
266
203
  if (!ctx.isIdle()) {
@@ -269,24 +206,18 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
269
206
  }
270
207
  await timings.measure(
271
208
  "automatic transfer",
272
- () => withStateDirectoryAccess(
273
- () => withAgentSyncLock(
274
- ctx,
275
- {
276
- args: [],
277
- yes: true,
278
- force: false,
279
- stale: false,
280
- silent: true,
281
- reload: false,
282
- auto: true,
283
- signal,
284
- startupTimings: timings,
285
- onCommit: () => attention.clearObservation()
286
- },
287
- (options) => operations.syncBoth(ctx, options)
288
- )
289
- )
209
+ () => operations.syncBoth(ctx, {
210
+ args: [],
211
+ yes: true,
212
+ force: false,
213
+ stale: false,
214
+ silent: true,
215
+ reload: false,
216
+ auto: true,
217
+ signal,
218
+ startupTimings: timings,
219
+ onCommit: () => attention.clearObservation()
220
+ })
290
221
  );
291
222
  throwIfAborted(signal);
292
223
  if (!isCurrent()) return;
@@ -358,12 +289,12 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
358
289
  // src/sync/sync-loaders.ts
359
290
  function createSyncLoaders(dependencies) {
360
291
  const loaders = {
361
- commands: cachedModuleLoader(dependencies.loadCommandHandler ?? (() => import("./chunks/command-handler-MRXE5AKK.ts"))),
362
- inspection: cachedModuleLoader(dependencies.loadSyncInspection ?? (() => import("./chunks/sync-inspection-MOMLGUTD.ts"))),
363
- setupSwitch: cachedModuleLoader(dependencies.loadSetupSwitch ?? (() => import("./chunks/setup-switch-KLGESLQX.ts"))),
364
- snapshot: cachedModuleLoader(dependencies.loadSnapshot ?? (() => import("./chunks/snapshot-RGNDYT6A.ts"))),
365
- syncState: cachedModuleLoader(dependencies.loadSyncState ?? (() => import("./chunks/sync-state-7L4NT7W2.ts"))),
366
- operations: cachedModuleLoader(dependencies.loadSyncOperations ?? (() => import("./chunks/sync-operations-45APGOL3.ts")))
292
+ commands: cachedModuleLoader(dependencies.loadCommandHandler ?? (() => import("./chunks/command-handler-3O76V7Q5.ts"))),
293
+ inspection: cachedModuleLoader(dependencies.loadSyncInspection ?? (() => import("./chunks/sync-inspection-BJ4KPPNO.ts"))),
294
+ setupSwitch: cachedModuleLoader(dependencies.loadSetupSwitch ?? (() => import("./chunks/setup-switch-M3BAFGJM.ts"))),
295
+ snapshot: cachedModuleLoader(dependencies.loadSnapshot ?? (() => import("./chunks/snapshot-D4SFDP5J.ts"))),
296
+ syncState: cachedModuleLoader(dependencies.loadSyncState ?? (() => import("./chunks/sync-state-FVCJJ7MY.ts"))),
297
+ operations: cachedModuleLoader(dependencies.loadSyncOperations ?? (() => import("./chunks/sync-operations-TLWER2TT.ts")))
367
298
  };
368
299
  return loaders;
369
300
  }
@@ -405,6 +336,7 @@ function registerStartupTimings(pi) {
405
336
 
406
337
  // src/sync-extension.ts
407
338
  function sync(pi, dependencies = {}) {
339
+ installPiDaemonHost();
408
340
  const loaders = createSyncLoaders(dependencies);
409
341
  const attention = createSyncAttentionController();
410
342
  const status = createSyncStatusPublisher(pi.events);
@@ -413,173 +345,204 @@ function sync(pi, dependencies = {}) {
413
345
  let sessionAbort = new AbortController();
414
346
  let shutdownAbort;
415
347
  let initialization = Promise.resolve(void 0);
416
- let continuous;
417
348
  let owner;
418
- let continuousPossible = false;
419
- let startupDebug = false;
420
- const foreground = /* @__PURE__ */ new Map();
421
- async function resumeContinuous(ctx, signal, retry) {
422
- const isCurrent = () => !signal.aborted && !foreground.has(ctx.sessionManager) && owner === ctx.sessionManager;
423
- if (!isCurrent() || !ctx.hasUI) return;
424
- try {
425
- const { loadConfigForCheck: loadConfigForCheck2 } = await import("./chunks/config-HYG7G5EM.ts");
426
- if (!isCurrent()) return;
427
- const config = await loadConfigForCheck2();
428
- if (!isCurrent()) return;
429
- continuousPossible = Boolean(config.continuous && config.agentMode);
430
- if (!continuousPossible) {
431
- await continuous?.stop(ctx.sessionManager);
432
- return;
433
- }
434
- const { createContinuousSync } = await import("./chunks/continuous-sync-FAFNLEUW.ts");
435
- if (!isCurrent()) return;
436
- continuous ??= createContinuousSync(loaders, void 0, publishTimings);
437
- continuous.start(
438
- ctx,
439
- signal,
440
- Boolean(retry && (retry.setup === void 0 || retry.setup === config.setupName)),
441
- startupDebug
442
- );
443
- } catch {
444
- }
445
- }
349
+ let ownerManager;
446
350
  pi.registerCommand("sync", {
447
351
  description: "Sync Pi settings through Git, WebDAV, R2, or S3-compatible storage",
448
352
  getArgumentCompletions: completeSyncArguments,
449
353
  handler: async (args, ctx) => {
450
- if (!ctx.hasUI) {
451
- throw new Error("/sync requires TUI or RPC mode so results and safety prompts are observable.");
452
- }
354
+ if (!ctx.hasUI) throw new Error("/sync requires TUI or RPC mode so results and safety prompts are observable.");
453
355
  status.bind(ctx);
454
356
  const signal = sessionAbort.signal;
455
- const manager = ctx.sessionManager;
456
- const command = splitArgs(args)[0];
457
- let retry;
458
- let mayResume = false;
459
- foreground.set(manager, (foreground.get(manager) ?? 0) + 1);
460
- try {
357
+ const client = owner;
358
+ if (!client || ownerManager !== ctx.sessionManager) {
359
+ if (splitArgs(args)[0] !== "help") throw new Error("pi-sync session is not ready.");
360
+ const commands = await loaders.commands();
361
+ if (signal.aborted || owner) return;
362
+ ctx.isIdle();
363
+ await commands.handleCommand(args, ctx, signal, loaders, attention);
364
+ return;
365
+ }
366
+ await withPiDaemonClient(ctx, async (ownedContext) => {
367
+ ctx = ownedContext;
368
+ const command = splitArgs(args)[0];
461
369
  const ready = await initialization;
462
- mayResume = ready !== false;
463
- if (signal.aborted) return;
464
- await continuous?.stop(manager);
465
- if (signal.aborted) return;
466
- await check.stop();
467
- if (signal.aborted) return;
468
- if (ready === false && command !== "help" && command !== "unlock" && command !== "migrate-state") {
469
- ctx.ui.notify(
470
- "pi-sync recovery required. Repair the startup error, then /reload before syncing. Use /sync help for recovery guidance.",
471
- "error"
472
- );
473
- return;
474
- }
475
- const commands = await loaders.commands().catch((error) => {
370
+ if (signal.aborted || owner !== client) return;
371
+ await client.setBusy("command", true);
372
+ try {
373
+ await check.stop();
374
+ if (signal.aborted || owner !== client) return;
375
+ if (ready === false && command !== "help" && command !== "unlock" && command !== "migrate-state" && command !== "daemon") {
376
+ ctx.ui.notify(
377
+ "pi-sync recovery required. Repair the startup error, then /reload before syncing. Use /sync help for recovery guidance.",
378
+ "error"
379
+ );
380
+ return;
381
+ }
382
+ if (command === "unlock" || command === "migrate-state") {
383
+ const { stateDirectoryMigrationNotice } = await import("./chunks/state-directory-JZSZVVPX.ts");
384
+ if (signal.aborted || owner !== client) return;
385
+ stateDirectoryMigrationNotice();
386
+ }
387
+ const commands = await loaders.commands();
388
+ if (signal.aborted || owner !== client) return;
389
+ await commands.handleCommand(args, ctx, signal, loaders, attention);
390
+ } catch (error) {
476
391
  if (!signal.aborted) {
477
392
  ctx.ui.notify(`pi-sync command unavailable: ${safeTerminalText(errorMessage(error))}`, "error");
393
+ if (command === "unlock" || command === "migrate-state") throw error;
478
394
  }
479
- });
480
- if (signal.aborted || !commands) return;
481
- const run = () => commands.handleCommand(args, ctx, signal, loaders, attention, (setup) => {
482
- retry = { setup };
483
- });
484
- if (command === "migrate-state" || command === "help") await run();
485
- else await withStateDirectoryAccess(run);
486
- } finally {
487
- const count = (foreground.get(manager) ?? 1) - 1;
488
- if (count) foreground.set(manager, count);
489
- else foreground.delete(manager);
490
- if (mayResume) await resumeContinuous(ctx, signal, retry);
491
- }
395
+ } finally {
396
+ if (!signal.aborted && owner === client) await client.setBusy("command", false);
397
+ }
398
+ });
492
399
  }
493
400
  });
494
401
  pi.on("session_start", async (_event, ctx) => {
495
402
  const releaseStatus = status.start(ctx);
496
- try {
497
- shutdownAbort?.abort(new DOMException("Session replaced", "AbortError"));
498
- shutdownAbort = void 0;
499
- sessionAbort.abort(new DOMException("Session replaced", "AbortError"));
500
- sessionAbort = new AbortController();
501
- const signal = sessionAbort.signal;
502
- owner = ctx.sessionManager;
503
- continuousPossible = false;
504
- startupDebug = false;
505
- attention.reset(ctx);
506
- const previous = initialization;
507
- initialization = (async () => {
508
- await previous;
509
- if (signal.aborted) return false;
510
- await continuous?.stop();
511
- if (signal.aborted) return false;
403
+ const previousClient = owner;
404
+ shutdownAbort?.abort(new DOMException("Session replaced", "AbortError"));
405
+ shutdownAbort = void 0;
406
+ sessionAbort.abort(new DOMException("Session replaced", "AbortError"));
407
+ sessionAbort = new AbortController();
408
+ const signal = sessionAbort.signal;
409
+ const client = bindPiDaemonClient(ctx, signal, publishTimings);
410
+ owner = client;
411
+ ownerManager = ctx.sessionManager;
412
+ const replacementFile = ctx.sessionManager.getSessionFile?.();
413
+ attention.reset(ctx);
414
+ const previous = initialization;
415
+ initialization = withPiDaemonClient(ctx, async () => {
416
+ await previous.catch(() => {
417
+ });
418
+ if (signal.aborted || owner !== client) return false;
419
+ try {
512
420
  await check.stop();
513
- if (signal.aborted) return false;
514
- try {
515
- const startup = await withStateDirectoryAccess(() => startSession(ctx, signal, publishTimings));
516
- if (signal.aborted) return false;
517
- startupDebug = startup?.debug ?? false;
518
- return startup?.config;
519
- } catch (error) {
520
- if (!signal.aborted && ctx.hasUI) {
521
- ctx.ui.notify(`pi-sync recovery required: ${safeTerminalText(errorMessage(error))}`, "error");
421
+ if (previousClient) {
422
+ try {
423
+ await client.attachExisting();
424
+ } catch {
425
+ }
426
+ try {
427
+ await previousClient.disposeForShutdown("resume", replacementFile);
428
+ } catch {
522
429
  }
523
- return false;
430
+ await client.finishReattachment();
524
431
  }
525
- })();
432
+ if (signal.aborted || owner !== client) return false;
433
+ return (await startSession(ctx, signal, publishTimings))?.config;
434
+ } catch (error) {
435
+ if (!signal.aborted && owner === client) {
436
+ try {
437
+ client.validate();
438
+ if (ctx.hasUI)
439
+ ctx.ui.notify(`pi-sync recovery required: ${safeTerminalText(errorMessage(error))}`, "error");
440
+ } catch {
441
+ }
442
+ }
443
+ return false;
444
+ }
445
+ });
446
+ try {
526
447
  const ready = await initialization;
527
448
  if (ready === false) releaseStatus();
528
- if (ready && !signal.aborted) {
529
- if (ready.continuous && ready.agentMode) await resumeContinuous(ctx, signal);
530
- else if (ready.automatic || startupTransferEnabled(ready)) check.start(ctx, signal, ready);
531
- }
449
+ if (ready && !signal.aborted && owner === client && !(ready.continuous && ready.agentMode) && (ready.automatic || startupTransferEnabled(ready)))
450
+ await withPiDaemonClient(ctx, async () => {
451
+ check.start(ctx, signal, ready);
452
+ });
532
453
  } catch (error) {
533
454
  releaseStatus();
534
455
  throw error;
535
456
  }
536
457
  });
537
458
  pi.on("agent_start", async (_event, ctx) => {
538
- await continuous?.stop(ctx.sessionManager);
539
- await check.interrupt(ctx);
459
+ const client = owner;
460
+ if (!client || ownerManager !== ctx.sessionManager || sessionAbort.signal.aborted) return;
461
+ if (!ctx.hasUI || !["tui", "rpc"].includes(ctx.mode)) await client.attachExisting();
462
+ await client.setBusy("agent", true);
463
+ if (owner === client && !sessionAbort.signal.aborted) await check.interrupt(ctx);
540
464
  });
541
465
  pi.on("agent_settled", async (_event, ctx) => {
542
- const signal = sessionAbort.signal;
543
- if (!signal.aborted) {
544
- check.settled(ctx);
545
- if (continuousPossible) await resumeContinuous(ctx, signal);
546
- }
466
+ const client = owner;
467
+ if (!client || ownerManager !== ctx.sessionManager || sessionAbort.signal.aborted) return;
468
+ await client.setBusy("agent", false);
469
+ if (owner === client && !sessionAbort.signal.aborted)
470
+ await withPiDaemonClient(ctx, async () => {
471
+ check.settled(ctx);
472
+ });
547
473
  });
474
+ pi.on("model_select", async (_event, ctx) => {
475
+ if (owner && ownerManager === ctx.sessionManager && !sessionAbort.signal.aborted) await owner.update();
476
+ });
477
+ const pauseForReplacement = async (_event, ctx) => {
478
+ const client = owner;
479
+ if (!client || ownerManager !== ctx.sessionManager || sessionAbort.signal.aborted) return;
480
+ const results = await Promise.allSettled([client.pauseForSessionChange(), check.stop()]);
481
+ if (results.some((result) => result.status === "rejected")) {
482
+ if (owner === client && !sessionAbort.signal.aborted)
483
+ ctx.ui.notify(
484
+ "pi-sync could not drain its writer. Repair or stop the existing daemon before changing sessions.",
485
+ "error"
486
+ );
487
+ return { cancel: true };
488
+ }
489
+ };
490
+ pi.on("session_before_switch", pauseForReplacement);
491
+ pi.on("session_before_fork", pauseForReplacement);
548
492
  pi.on("session_shutdown", async (event, ctx) => {
549
493
  const releaseStatus = status.cleanupFor(ctx);
494
+ const client = owner;
495
+ if (!client || ownerManager !== ctx.sessionManager) {
496
+ releaseStatus();
497
+ return;
498
+ }
499
+ const controller = new AbortController();
500
+ shutdownAbort?.abort(new DOMException("Session shut down again", "AbortError"));
501
+ shutdownAbort = controller;
502
+ const signal = combineSignals(controller.signal, AbortSignal.timeout(3e4));
503
+ const reason = typeof event === "object" && event ? event.reason : void 0;
504
+ const target = typeof event === "object" && event ? event.targetSessionFile : void 0;
505
+ sessionAbort.abort(new DOMException("Session shutting down", "AbortError"));
506
+ let shutdownClient;
550
507
  try {
551
- if (continuous && owner && owner !== ctx.sessionManager) return;
552
- sessionAbort.abort(new DOMException("Session shut down", "AbortError"));
553
- attention.reset(ctx);
554
- shutdownAbort?.abort(new DOMException("Session shut down again", "AbortError"));
555
- const controller = new AbortController();
556
- shutdownAbort = controller;
557
- const signal = combineSignals(controller.signal, AbortSignal.timeout(3e4));
558
- const reason = typeof event === "object" && event ? event.reason : void 0;
559
- try {
560
- const ready = await initialization;
561
- if (signal.aborted) return;
562
- await continuous?.stop(ctx.sessionManager);
563
- if (signal.aborted) return;
564
- await check.stop();
565
- if (signal.aborted) return;
566
- if (ready !== false && reason !== "reload") {
567
- await withStateDirectoryAccess(async () => {
568
- if (signal.aborted) return;
569
- await autoPushSessions(ctx, signal, loaders);
570
- });
508
+ await initialization.catch(() => {
509
+ });
510
+ await check.stop();
511
+ if (signal.aborted || owner !== client) return;
512
+ if (reason !== "reload") {
513
+ shutdownClient = bindPiDaemonClient(ctx, signal, publishTimings);
514
+ await shutdownClient.setBusy("shutdown", true);
515
+ const attached = await shutdownClient.attachExisting();
516
+ if (!signal.aborted && owner === client && attached)
517
+ await withPiDaemonClient(ctx, () => autoPushSessions(ctx, signal, loaders));
518
+ }
519
+ } catch (error) {
520
+ if (!signal.aborted && owner === client) {
521
+ try {
522
+ ctx.ui.notify(`pi-sync shutdown skipped: ${safeTerminalText(errorMessage(error))}`, "warning");
523
+ } catch {
571
524
  }
572
- } catch (error) {
573
- if (!signal.aborted) {
574
- ctx.ui.notify(`pi-sync session push skipped: ${errorMessage(error)}`, "warning");
525
+ }
526
+ } finally {
527
+ if (owner === client) {
528
+ owner = void 0;
529
+ ownerManager = void 0;
530
+ try {
531
+ attention.reset(ctx);
532
+ } catch {
575
533
  }
534
+ }
535
+ try {
536
+ await client.disposeForShutdown(reason, target);
576
537
  } finally {
577
- if (shutdownAbort === controller) shutdownAbort = void 0;
538
+ try {
539
+ await shutdownClient?.disposeForShutdown(reason, target);
540
+ } finally {
541
+ controller.abort();
542
+ releaseStatus();
543
+ }
578
544
  }
579
- if (signal.aborted) return;
580
- setSyncStatus(ctx, void 0);
581
- } finally {
582
- releaseStatus();
545
+ if (shutdownAbort === controller) shutdownAbort = void 0;
583
546
  }
584
547
  });
585
548
  }