@narumitw/pi-sync 0.59.0 → 0.60.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +25 -17
  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-NGW4JMBF.ts → chunk-4RNCZZY7.ts} +33 -27
  6. package/dist/chunks/{chunk-NGW4JMBF.ts.map → chunk-4RNCZZY7.ts.map} +3 -3
  7. package/dist/chunks/{chunk-2ZZJXITS.ts → chunk-6LHIRS5R.ts} +30 -47
  8. package/dist/chunks/chunk-6LHIRS5R.ts.map +7 -0
  9. package/dist/chunks/{config-J5VZUY4J.ts → chunk-7AC2RX4M.ts} +6 -13
  10. package/dist/chunks/chunk-7AC2RX4M.ts.map +7 -0
  11. package/dist/chunks/{chunk-76HID27T.ts → chunk-BGGKHHO4.ts} +18 -14
  12. package/dist/chunks/{chunk-76HID27T.ts.map → chunk-BGGKHHO4.ts.map} +1 -1
  13. package/dist/chunks/chunk-BWWTOQOW.ts +737 -0
  14. package/dist/chunks/chunk-BWWTOQOW.ts.map +7 -0
  15. package/dist/chunks/chunk-CEMFCWIU.ts +20 -0
  16. package/dist/chunks/chunk-CEMFCWIU.ts.map +7 -0
  17. package/dist/chunks/chunk-CZNBKIM5.ts +20 -0
  18. package/dist/chunks/chunk-CZNBKIM5.ts.map +7 -0
  19. package/dist/chunks/{chunk-YIPK23JW.ts → chunk-ENFHHB5O.ts} +1 -16
  20. package/dist/chunks/{chunk-YIPK23JW.ts.map → chunk-ENFHHB5O.ts.map} +1 -1
  21. package/dist/chunks/{chunk-KHBPED6W.ts → chunk-GSQGBVKG.ts} +2 -2
  22. package/dist/chunks/chunk-HNKLBM3U.ts +18 -0
  23. package/dist/chunks/chunk-HNKLBM3U.ts.map +7 -0
  24. package/dist/chunks/{chunk-QPC5HT3A.ts → chunk-J5COV5TY.ts} +21 -2
  25. package/dist/chunks/chunk-J5COV5TY.ts.map +7 -0
  26. package/dist/chunks/chunk-JKBUYCPC.ts +52 -0
  27. package/dist/chunks/chunk-JKBUYCPC.ts.map +7 -0
  28. package/dist/chunks/chunk-JZKCOM62.ts +26 -0
  29. package/dist/chunks/chunk-JZKCOM62.ts.map +7 -0
  30. package/dist/chunks/chunk-M7RHV3H6.ts +33 -0
  31. package/dist/chunks/chunk-M7RHV3H6.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-PQ75RPUP.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-DTPELCPW.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-SDGKIQH2.ts → command-handler-FO4XBBHF.ts} +80 -86
  46. package/dist/chunks/command-handler-FO4XBBHF.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-OM64J27R.ts → continuous-record-ZHUXVO4E.ts} +5 -13
  50. package/dist/chunks/continuous-record-ZHUXVO4E.ts.map +7 -0
  51. package/dist/chunks/continuous-storage-SQBAXACF.ts +27 -0
  52. package/dist/chunks/continuous-storage-SQBAXACF.ts.map +7 -0
  53. package/dist/chunks/{file-selection-7HEVPO4T.ts → file-selection-S6CNNNQI.ts} +11 -14
  54. package/dist/chunks/{file-selection-7HEVPO4T.ts.map → file-selection-S6CNNNQI.ts.map} +1 -1
  55. package/dist/chunks/manager-result-dispatcher-I2AMCCE7.ts +18 -0
  56. package/dist/chunks/{manager-ui-65ZETN6F.ts → manager-ui-AY5ZU6B4.ts} +180 -164
  57. package/dist/chunks/manager-ui-AY5ZU6B4.ts.map +7 -0
  58. package/dist/chunks/merge-review-SGDLMVS4.ts +10 -0
  59. package/dist/chunks/remote-selection-ui-GZHPDQ4L.ts +17 -0
  60. package/dist/chunks/{setup-switch-AB27ADDI.ts → setup-switch-M3BAFGJM.ts} +9 -13
  61. package/dist/chunks/{setup-wizard-5FILKFLQ.ts → setup-wizard-NFAWP4KF.ts} +18 -21
  62. package/dist/chunks/{setup-wizard-5FILKFLQ.ts.map → setup-wizard-NFAWP4KF.ts.map} +1 -1
  63. package/dist/chunks/{snapshot-G3W2GOGB.ts → snapshot-D4SFDP5J.ts} +8 -13
  64. package/dist/chunks/snapshot-D4SFDP5J.ts.map +7 -0
  65. package/dist/chunks/snapshot-transaction-directory-TNKBQ3UQ.ts +13 -0
  66. package/dist/chunks/snapshot-transaction-directory-TNKBQ3UQ.ts.map +7 -0
  67. package/dist/chunks/state-directory-JZSZVVPX.ts +17 -0
  68. package/dist/chunks/sync-inspection-BJ4KPPNO.ts +23 -0
  69. package/dist/chunks/sync-inspection-BJ4KPPNO.ts.map +7 -0
  70. package/dist/chunks/sync-operations-TLWER2TT.ts +45 -0
  71. package/dist/chunks/sync-operations-TLWER2TT.ts.map +7 -0
  72. package/dist/chunks/{sync-state-H4TFZZ7S.ts → sync-state-FVCJJ7MY.ts} +5 -11
  73. package/dist/chunks/sync-state-FVCJJ7MY.ts.map +7 -0
  74. package/dist/index.ts +269 -313
  75. package/dist/index.ts.map +4 -4
  76. package/docs/daemon-migration.md +93 -0
  77. package/docs/tui-contract.md +12 -0
  78. package/package.json +3 -6
  79. package/skills/configure-pi-sync/SKILL.md +1 -1
  80. package/skills/configure-pi-sync/references/configuration.md +19 -6
  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 -293
  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 +40 -36
  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 -263
  105. package/src/settings/settings-management.ts +11 -254
  106. package/src/settings/settings-store.ts +10 -48
  107. package/src/settings/settings-types.ts +1 -157
  108. package/src/settings/settings-validation.ts +17 -435
  109. package/src/snapshot/session-paths.ts +11 -111
  110. package/src/snapshot/snapshot-apply.ts +9 -322
  111. package/src/snapshot/snapshot-case-replacement.ts +6 -80
  112. package/src/snapshot/snapshot-codec.ts +2 -59
  113. package/src/snapshot/snapshot-exclude-safety.ts +2 -44
  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 -946
  120. package/src/snapshot/snapshot-types.ts +1 -43
  121. package/src/snapshot/snapshot.ts +19 -447
  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 -208
  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 -25
  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 -111
  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 -61
  156. package/src/sync/exclude-policy.ts +6 -84
  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 -119
  162. package/src/sync/merge-apply.ts +2 -439
  163. package/src/sync/merge-journal.ts +10 -228
  164. package/src/sync/merge-transfer-error.ts +2 -72
  165. package/src/sync/merged-sync.ts +2 -919
  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 -55
  169. package/src/sync/remote-snapshot.ts +7 -79
  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 -111
  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 -59
  180. package/src/sync/sync-local.ts +2 -44
  181. package/src/sync/sync-mutations.ts +10 -788
  182. package/src/sync/sync-operations.ts +32 -11
  183. package/src/sync/sync-policy.ts +34 -474
  184. package/src/sync/sync-queries.ts +2 -247
  185. package/src/sync/sync-state.ts +18 -157
  186. package/src/sync/text-merge.ts +10 -133
  187. package/src/sync-extension.ts +192 -142
  188. package/src/ui/manager-state.ts +35 -23
  189. package/src/ui/manager-ui.ts +80 -36
  190. package/src/ui/sync-attention.ts +7 -49
  191. package/dist/chunks/agent-candidate-ZHN7FTZT.ts +0 -30
  192. package/dist/chunks/agent-plan-ILFEUJ2E.ts +0 -124
  193. package/dist/chunks/agent-plan-ILFEUJ2E.ts.map +0 -7
  194. package/dist/chunks/agent-refusal-retry-HRMHCX2A.ts +0 -73
  195. package/dist/chunks/agent-refusal-retry-HRMHCX2A.ts.map +0 -7
  196. package/dist/chunks/agent-runner-FCRV2Z75.ts +0 -185
  197. package/dist/chunks/agent-runner-FCRV2Z75.ts.map +0 -7
  198. package/dist/chunks/agent-store-CCKN4DPR.ts +0 -175
  199. package/dist/chunks/agent-store-CCKN4DPR.ts.map +0 -7
  200. package/dist/chunks/agent-sync-EGA2I5W7.ts +0 -22
  201. package/dist/chunks/chunk-25QCU3LY.ts +0 -220
  202. package/dist/chunks/chunk-25QCU3LY.ts.map +0 -7
  203. package/dist/chunks/chunk-2JZFHNLM.ts +0 -113
  204. package/dist/chunks/chunk-2JZFHNLM.ts.map +0 -7
  205. package/dist/chunks/chunk-2UKUKIBI.ts +0 -25
  206. package/dist/chunks/chunk-2UKUKIBI.ts.map +0 -7
  207. package/dist/chunks/chunk-2ZZJXITS.ts.map +0 -7
  208. package/dist/chunks/chunk-3JTZF7Z5.ts +0 -77
  209. package/dist/chunks/chunk-3JTZF7Z5.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-4NVHHDN2.ts +0 -61
  213. package/dist/chunks/chunk-4NVHHDN2.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-6OFMB6KK.ts +0 -215
  217. package/dist/chunks/chunk-6OFMB6KK.ts.map +0 -7
  218. package/dist/chunks/chunk-6XU5HZAN.ts +0 -205
  219. package/dist/chunks/chunk-6XU5HZAN.ts.map +0 -7
  220. package/dist/chunks/chunk-6YGRZSCN.ts +0 -128
  221. package/dist/chunks/chunk-6YGRZSCN.ts.map +0 -7
  222. package/dist/chunks/chunk-7D5SF7P3.ts +0 -29
  223. package/dist/chunks/chunk-7D5SF7P3.ts.map +0 -7
  224. package/dist/chunks/chunk-7WYF2KUO.ts +0 -1161
  225. package/dist/chunks/chunk-7WYF2KUO.ts.map +0 -7
  226. package/dist/chunks/chunk-APDNHB2T.ts +0 -2037
  227. package/dist/chunks/chunk-APDNHB2T.ts.map +0 -7
  228. package/dist/chunks/chunk-AR2DGDUI.ts +0 -55
  229. package/dist/chunks/chunk-AR2DGDUI.ts.map +0 -7
  230. package/dist/chunks/chunk-DT7M4VQS.ts +0 -69
  231. package/dist/chunks/chunk-DT7M4VQS.ts.map +0 -7
  232. package/dist/chunks/chunk-DTPELCPW.ts.map +0 -7
  233. package/dist/chunks/chunk-ECYCNZOE.ts.map +0 -7
  234. package/dist/chunks/chunk-FHE2DCA2.ts +0 -61
  235. package/dist/chunks/chunk-FHE2DCA2.ts.map +0 -7
  236. package/dist/chunks/chunk-G4RUXRWZ.ts +0 -48
  237. package/dist/chunks/chunk-G4RUXRWZ.ts.map +0 -7
  238. package/dist/chunks/chunk-ILXMZ2HU.ts +0 -58
  239. package/dist/chunks/chunk-ILXMZ2HU.ts.map +0 -7
  240. package/dist/chunks/chunk-LA3HQMFG.ts +0 -115
  241. package/dist/chunks/chunk-LA3HQMFG.ts.map +0 -7
  242. package/dist/chunks/chunk-M6VQJIOU.ts +0 -54
  243. package/dist/chunks/chunk-M6VQJIOU.ts.map +0 -7
  244. package/dist/chunks/chunk-MKC3PXCN.ts +0 -369
  245. package/dist/chunks/chunk-MKC3PXCN.ts.map +0 -7
  246. package/dist/chunks/chunk-OQBE2OMB.ts +0 -88
  247. package/dist/chunks/chunk-OQBE2OMB.ts.map +0 -7
  248. package/dist/chunks/chunk-P3YD4ONV.ts +0 -123
  249. package/dist/chunks/chunk-P3YD4ONV.ts.map +0 -7
  250. package/dist/chunks/chunk-PRUEIIK7.ts +0 -24
  251. package/dist/chunks/chunk-PRUEIIK7.ts.map +0 -7
  252. package/dist/chunks/chunk-PWMWOJP4.ts +0 -17
  253. package/dist/chunks/chunk-PWMWOJP4.ts.map +0 -7
  254. package/dist/chunks/chunk-QPC5HT3A.ts.map +0 -7
  255. package/dist/chunks/chunk-QQVK2XFC.ts +0 -64
  256. package/dist/chunks/chunk-QQVK2XFC.ts.map +0 -7
  257. package/dist/chunks/chunk-RDOX3VH5.ts +0 -140
  258. package/dist/chunks/chunk-RDOX3VH5.ts.map +0 -7
  259. package/dist/chunks/chunk-S7D6XWHI.ts +0 -258
  260. package/dist/chunks/chunk-S7D6XWHI.ts.map +0 -7
  261. package/dist/chunks/chunk-SHPNS7FM.ts +0 -64
  262. package/dist/chunks/chunk-SHPNS7FM.ts.map +0 -7
  263. package/dist/chunks/chunk-SWRVXVFD.ts +0 -248
  264. package/dist/chunks/chunk-SWRVXVFD.ts.map +0 -7
  265. package/dist/chunks/chunk-T2SJD3N7.ts +0 -99
  266. package/dist/chunks/chunk-T2SJD3N7.ts.map +0 -7
  267. package/dist/chunks/chunk-T2TYBLXK.ts +0 -27
  268. package/dist/chunks/chunk-T2TYBLXK.ts.map +0 -7
  269. package/dist/chunks/chunk-UZSDS4HF.ts +0 -403
  270. package/dist/chunks/chunk-UZSDS4HF.ts.map +0 -7
  271. package/dist/chunks/chunk-VPODHKOX.ts +0 -229
  272. package/dist/chunks/chunk-VPODHKOX.ts.map +0 -7
  273. package/dist/chunks/chunk-WEKGW7HV.ts +0 -162
  274. package/dist/chunks/chunk-WEKGW7HV.ts.map +0 -7
  275. package/dist/chunks/chunk-WUHRVSWI.ts +0 -369
  276. package/dist/chunks/chunk-WUHRVSWI.ts.map +0 -7
  277. package/dist/chunks/chunk-X7R6DCVF.ts +0 -28
  278. package/dist/chunks/chunk-X7R6DCVF.ts.map +0 -7
  279. package/dist/chunks/chunk-XJHQPB3E.ts +0 -107
  280. package/dist/chunks/chunk-XJHQPB3E.ts.map +0 -7
  281. package/dist/chunks/chunk-XZ3ZTHKR.ts +0 -1184
  282. package/dist/chunks/chunk-XZ3ZTHKR.ts.map +0 -7
  283. package/dist/chunks/chunk-YDVQYUHE.ts +0 -34
  284. package/dist/chunks/chunk-YDVQYUHE.ts.map +0 -7
  285. package/dist/chunks/chunk-YEULK7MK.ts +0 -415
  286. package/dist/chunks/chunk-YEULK7MK.ts.map +0 -7
  287. package/dist/chunks/chunk-YHT4YTYG.ts +0 -31
  288. package/dist/chunks/chunk-YHT4YTYG.ts.map +0 -7
  289. package/dist/chunks/chunk-ZW5ZLGBI.ts +0 -154
  290. package/dist/chunks/chunk-ZW5ZLGBI.ts.map +0 -7
  291. package/dist/chunks/command-handler-SDGKIQH2.ts.map +0 -7
  292. package/dist/chunks/conflict-artifacts-MYSH3L2J.ts +0 -33
  293. package/dist/chunks/conflict-review-J6ZSPNWL.ts +0 -190
  294. package/dist/chunks/conflict-review-J6ZSPNWL.ts.map +0 -7
  295. package/dist/chunks/continuous-sync-BH5SWIRV.ts +0 -590
  296. package/dist/chunks/continuous-sync-BH5SWIRV.ts.map +0 -7
  297. package/dist/chunks/git-backend-6CKI6CUG.ts +0 -1074
  298. package/dist/chunks/git-backend-6CKI6CUG.ts.map +0 -7
  299. package/dist/chunks/local-fields-SR7PH66U.ts +0 -27
  300. package/dist/chunks/local-fields-SR7PH66U.ts.map +0 -7
  301. package/dist/chunks/manager-result-dispatcher-CXUP5WDP.ts +0 -33
  302. package/dist/chunks/manager-result-dispatcher-CXUP5WDP.ts.map +0 -7
  303. package/dist/chunks/manager-ui-65ZETN6F.ts.map +0 -7
  304. package/dist/chunks/merge-baseline-store-4Y2X7P2D.ts +0 -26
  305. package/dist/chunks/merge-baseline-store-4Y2X7P2D.ts.map +0 -7
  306. package/dist/chunks/remote-selection-ui-W55BQDJQ.ts +0 -32
  307. package/dist/chunks/remote-selection-ui-W55BQDJQ.ts.map +0 -7
  308. package/dist/chunks/s3-backend-FDK7JSDQ.ts +0 -845
  309. package/dist/chunks/s3-backend-FDK7JSDQ.ts.map +0 -7
  310. package/dist/chunks/setup-switch-AB27ADDI.ts.map +0 -7
  311. package/dist/chunks/snapshot-G3W2GOGB.ts.map +0 -7
  312. package/dist/chunks/snapshot-transaction-SGRUWT73.ts +0 -25
  313. package/dist/chunks/snapshot-transaction-SGRUWT73.ts.map +0 -7
  314. package/dist/chunks/sync-inspection-MDYHZHT7.ts +0 -26
  315. package/dist/chunks/sync-inspection-MDYHZHT7.ts.map +0 -7
  316. package/dist/chunks/sync-operations-ZAUP65FH.ts +0 -1032
  317. package/dist/chunks/sync-operations-ZAUP65FH.ts.map +0 -7
  318. package/dist/chunks/sync-state-H4TFZZ7S.ts.map +0 -7
  319. package/dist/chunks/webdav-backend-UNMIAXM5.ts +0 -852
  320. package/dist/chunks/webdav-backend-UNMIAXM5.ts.map +0 -7
  321. /package/dist/chunks/{chunk-KHBPED6W.ts.map → chunk-GSQGBVKG.ts.map} +0 -0
  322. /package/dist/chunks/{chunk-PQ75RPUP.ts.map → chunk-UZUUUY7C.ts.map} +0 -0
  323. /package/dist/chunks/{agent-candidate-ZHN7FTZT.ts.map → manager-result-dispatcher-I2AMCCE7.ts.map} +0 -0
  324. /package/dist/chunks/{agent-sync-EGA2I5W7.ts.map → merge-review-SGDLMVS4.ts.map} +0 -0
  325. /package/dist/chunks/{config-J5VZUY4J.ts.map → remote-selection-ui-GZHPDQ4L.ts.map} +0 -0
  326. /package/dist/chunks/{conflict-artifacts-MYSH3L2J.ts.map → setup-switch-M3BAFGJM.ts.map} +0 -0
  327. /package/dist/chunks/{continuous-record-OM64J27R.ts.map → state-directory-JZSZVVPX.ts.map} +0 -0
package/dist/index.ts CHANGED
@@ -1,182 +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-2ZZJXITS.ts";
4
+ createSyncAttentionController,
5
+ readStateForConfig,
6
+ syncStateFingerprint
7
+ } from "./chunks/chunk-6LHIRS5R.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-T2TYBLXK.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-XZ3ZTHKR.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-S7D6XWHI.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-4NVHHDN2.ts";
52
- import "./chunks/chunk-AR2DGDUI.ts";
53
- import {
54
- stateDirectoryMigrationNotice,
55
- withStateDirectoryAccess
56
- } from "./chunks/chunk-SWRVXVFD.ts";
57
- import "./chunks/chunk-2JZFHNLM.ts";
58
- import "./chunks/chunk-T2SJD3N7.ts";
38
+ } from "./chunks/chunk-PDBTD2K6.ts";
59
39
  import {
60
- syncSessionsSelected
61
- } from "./chunks/chunk-WUHRVSWI.ts";
40
+ errorMessage
41
+ } from "./chunks/chunk-HNKLBM3U.ts";
62
42
  import {
63
- sessionDirFromContext,
64
- snapshotOptionsForContext
65
- } from "./chunks/chunk-LA3HQMFG.ts";
66
- import "./chunks/chunk-3JTZF7Z5.ts";
67
- 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
68
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,
69
55
  startupTransferEnabled
70
- } from "./chunks/chunk-PRUEIIK7.ts";
56
+ } from "@narumitw/sync-daemon/engine/sync/agent-policy";
71
57
 
72
58
  // src/settings/config-errors.ts
73
- function isMissingConfigError(error) {
74
- return error instanceof Error && (error.message.startsWith("Missing pi-sync settings.") || error.message === "No sync setups are configured.");
75
- }
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";
76
63
 
77
64
  // src/sync/automatic-sync.ts
78
- var AUTO_SYNC_OPTIONS = {
79
- yes: true,
80
- force: false,
81
- stale: false,
82
- silent: true,
83
- reload: false,
84
- auto: true,
85
- args: []
86
- };
87
65
  async function startSession(ctx, signal, publish) {
88
- const timings = createStartupTimings(publish);
89
- let debug = false;
90
- let outcome = "stopped";
91
- try {
92
- throwIfAborted(signal);
93
- const stateNotice = stateDirectoryMigrationNotice();
94
- if (stateNotice && ctx.hasUI) ctx.ui.notify(stateNotice, "warning");
95
- const validateMutation = captureMutationOwner(ctx, signal);
96
- const currentFile = ctx.sessionManager.getSessionFile?.();
97
- await timings.measure("transaction recovery", async () => {
98
- const pending = await pendingTransactionEntries();
99
- validateMutation();
100
- if (!pending.some((entry) => entry.isDirectory())) return;
101
- const { recoverSnapshotTransactionsOnStartup } = await import("./chunks/snapshot-transaction-SGRUWT73.ts");
102
- validateMutation();
103
- const recoverySettings = await readLocalConfigObject();
104
- validateMutation();
105
- const recoverySetup = recoverySettings?.activeSyncSetup;
106
- await recoverSnapshotTransactionsOnStartup({
107
- exclude: recoverySetup ? recoverySettings.syncSetups[recoverySetup]?.sync.exclude : void 0,
108
- signal,
109
- sessionDir: sessionDirFromContext(ctx),
110
- protectedTargets: currentFile ? [currentFile] : [],
111
- validateMutation
112
- });
113
- });
66
+ throwIfAborted(signal);
67
+ const client = piDaemonClient(ctx);
68
+ if (!ctx.hasUI || !["tui", "rpc"].includes(ctx.mode)) {
69
+ await client.attachExisting();
114
70
  throwIfAborted(signal);
115
- try {
116
- const settings = await timings.measure("settings read", () => readLocalConfigObject());
117
- throwIfAborted(signal);
118
- debug = settings?.debug ?? false;
119
- const names = settings ? Object.keys(settings.syncSetups).sort((left, right) => left.localeCompare(right)) : [];
120
- setSyncSetupCompletions(names);
121
- configureSyncStatus(ctx, settings?.showStatus ?? true);
122
- } catch {
123
- if (signal.aborted) return;
124
- setSyncSetupCompletions([]);
125
- }
126
- const migrationNotice = consumeLocalConfigMigrationNotice();
127
- if (migrationNotice && ctx.hasUI) ctx.ui.notify(migrationNotice, "warning");
128
- throwIfAborted(signal);
129
- if (!ctx.hasUI) return { debug };
130
- try {
131
- const config = await timings.measure("config resolution", () => loadConfig());
132
- throwIfAborted(signal);
133
- configureSyncStatus(ctx, config.showStatus);
134
- outcome = "completed";
135
- return { debug, config };
136
- } catch (error) {
137
- throwIfAborted(signal);
138
- if (!isMissingConfigError(error)) {
139
- ctx.ui.notify(`pi-sync startup check skipped: ${safeTerminalText(errorMessage(error))}`, "warning");
140
- }
141
- }
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");
142
88
  return { debug };
143
- } finally {
144
- timings.publish(ctx, debug, signal, "local initialization", outcome);
145
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 };
146
105
  }
147
- async function autoPushSessions(ctx, signal, loaders) {
106
+ async function autoPushSessions(ctx, signal, _loaders) {
148
107
  try {
149
- const partial = await loadPartialConfig();
150
- throwIfAborted(signal);
151
- if (!partial.automatic) return;
152
- if (!syncSessionsSelected(partial)) return;
153
- await ensureStateDir();
108
+ const client = piDaemonClient(ctx);
109
+ if (!await client.configured()) return;
154
110
  throwIfAborted(signal);
155
- const config = await loadConfig();
156
- throwIfAborted(signal);
157
- if (!syncSessionsSelected(config)) return;
158
- const [operations, snapshotModule, syncStateModule] = await Promise.all([
159
- loaders.operations(),
160
- loaders.snapshot(),
161
- loaders.syncState()
162
- ]);
163
- throwIfAborted(signal);
164
- await withLock("auto-session-push", async () => {
165
- throwIfAborted(signal);
166
- const state = await readStateForConfig(config);
167
- throwIfAborted(signal);
168
- const local = await snapshotModule.createSnapshot(config.snapshotIdentity, {
169
- ...snapshotOptionsForContext(ctx, config),
170
- signal
171
- });
172
- throwIfAborted(signal);
173
- if (!syncStateModule.hasLocalChanges(local, state, config)) return;
174
- await operations.push(ctx, { ...AUTO_SYNC_OPTIONS, signal }, { config, state, local });
175
- });
111
+ await client.run(
112
+ "shutdown",
113
+ { args: [], yes: true, force: false, stale: false, silent: true, reload: false },
114
+ signal
115
+ );
176
116
  } catch (error) {
177
117
  if (signal.aborted || isMissingConfigError(error)) return;
178
- setSyncStatus(ctx, void 0);
179
- ctx.ui.notify(`pi-sync session push skipped: ${errorMessage(error)}`, "warning");
118
+ ctx.ui.notify(`pi-sync session push skipped: ${safeTerminalText(errorMessage(error))}`, "warning");
180
119
  }
181
120
  }
182
121
 
@@ -241,13 +180,7 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
241
180
  if (syncCheckConfigFingerprint(currentConfig) !== identity) return;
242
181
  const inspection = await timings.measure(
243
182
  "inspection and guards",
244
- () => withStateDirectoryAccess(() => {
245
- throwIfAborted(signal);
246
- return withLock("startup-check", () => {
247
- throwIfAborted(signal);
248
- return inspectSync(captured, snapshotOptionsForContext(ctx, captured), signal, void 0, timings);
249
- });
250
- })
183
+ () => inspectSync(captured, snapshotOptionsForContext(ctx, captured), signal, void 0, timings)
251
184
  );
252
185
  throwIfAborted(signal);
253
186
  const latest = await loadConfigForCheck();
@@ -264,10 +197,7 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
264
197
  automaticTransfer: startupTransferEnabled(captured)
265
198
  });
266
199
  if (startupTransferEnabled(captured) && !inspection.firstSync && inspection.head && inspection.selectionState?.kind === "same") {
267
- const [operations, { withAgentSyncLock }] = await timings.measure(
268
- "operations import",
269
- () => Promise.all([loaders.operations(), import("./chunks/agent-sync-EGA2I5W7.ts")])
270
- );
200
+ const operations = await timings.measure("operations import", () => loaders.operations());
271
201
  throwIfAborted(signal);
272
202
  if (!isCurrent()) return;
273
203
  if (!ctx.isIdle()) {
@@ -276,24 +206,18 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
276
206
  }
277
207
  await timings.measure(
278
208
  "automatic transfer",
279
- () => withStateDirectoryAccess(
280
- () => withAgentSyncLock(
281
- ctx,
282
- {
283
- args: [],
284
- yes: true,
285
- force: false,
286
- stale: false,
287
- silent: true,
288
- reload: false,
289
- auto: true,
290
- signal,
291
- startupTimings: timings,
292
- onCommit: () => attention.clearObservation()
293
- },
294
- (options) => operations.syncBoth(ctx, options)
295
- )
296
- )
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
+ })
297
221
  );
298
222
  throwIfAborted(signal);
299
223
  if (!isCurrent()) return;
@@ -365,12 +289,12 @@ function createStartupCheck(loaders, attention, timeoutMs = 3e4, publish) {
365
289
  // src/sync/sync-loaders.ts
366
290
  function createSyncLoaders(dependencies) {
367
291
  const loaders = {
368
- commands: cachedModuleLoader(dependencies.loadCommandHandler ?? (() => import("./chunks/command-handler-SDGKIQH2.ts"))),
369
- inspection: cachedModuleLoader(dependencies.loadSyncInspection ?? (() => import("./chunks/sync-inspection-MDYHZHT7.ts"))),
370
- setupSwitch: cachedModuleLoader(dependencies.loadSetupSwitch ?? (() => import("./chunks/setup-switch-AB27ADDI.ts"))),
371
- snapshot: cachedModuleLoader(dependencies.loadSnapshot ?? (() => import("./chunks/snapshot-G3W2GOGB.ts"))),
372
- syncState: cachedModuleLoader(dependencies.loadSyncState ?? (() => import("./chunks/sync-state-H4TFZZ7S.ts"))),
373
- operations: cachedModuleLoader(dependencies.loadSyncOperations ?? (() => import("./chunks/sync-operations-ZAUP65FH.ts")))
292
+ commands: cachedModuleLoader(dependencies.loadCommandHandler ?? (() => import("./chunks/command-handler-FO4XBBHF.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")))
374
298
  };
375
299
  return loaders;
376
300
  }
@@ -412,6 +336,7 @@ function registerStartupTimings(pi) {
412
336
 
413
337
  // src/sync-extension.ts
414
338
  function sync(pi, dependencies = {}) {
339
+ installPiDaemonHost();
415
340
  const loaders = createSyncLoaders(dependencies);
416
341
  const attention = createSyncAttentionController();
417
342
  const status = createSyncStatusPublisher(pi.events);
@@ -420,173 +345,204 @@ function sync(pi, dependencies = {}) {
420
345
  let sessionAbort = new AbortController();
421
346
  let shutdownAbort;
422
347
  let initialization = Promise.resolve(void 0);
423
- let continuous;
424
348
  let owner;
425
- let continuousPossible = false;
426
- let startupDebug = false;
427
- const foreground = /* @__PURE__ */ new Map();
428
- async function resumeContinuous(ctx, signal, retry) {
429
- const isCurrent = () => !signal.aborted && !foreground.has(ctx.sessionManager) && owner === ctx.sessionManager;
430
- if (!isCurrent() || !ctx.hasUI) return;
431
- try {
432
- const { loadConfigForCheck: loadConfigForCheck2 } = await import("./chunks/config-J5VZUY4J.ts");
433
- if (!isCurrent()) return;
434
- const config = await loadConfigForCheck2();
435
- if (!isCurrent()) return;
436
- continuousPossible = Boolean(config.continuous && config.agentMode);
437
- if (!continuousPossible) {
438
- await continuous?.stop(ctx.sessionManager);
439
- return;
440
- }
441
- const { createContinuousSync } = await import("./chunks/continuous-sync-BH5SWIRV.ts");
442
- if (!isCurrent()) return;
443
- continuous ??= createContinuousSync(loaders, void 0, publishTimings);
444
- continuous.start(
445
- ctx,
446
- signal,
447
- Boolean(retry && (retry.setup === void 0 || retry.setup === config.setupName)),
448
- startupDebug
449
- );
450
- } catch {
451
- }
452
- }
349
+ let ownerManager;
453
350
  pi.registerCommand("sync", {
454
351
  description: "Sync Pi settings through Git, WebDAV, R2, or S3-compatible storage",
455
352
  getArgumentCompletions: completeSyncArguments,
456
353
  handler: async (args, ctx) => {
457
- if (!ctx.hasUI) {
458
- throw new Error("/sync requires TUI or RPC mode so results and safety prompts are observable.");
459
- }
354
+ if (!ctx.hasUI) throw new Error("/sync requires TUI or RPC mode so results and safety prompts are observable.");
460
355
  status.bind(ctx);
461
356
  const signal = sessionAbort.signal;
462
- const manager = ctx.sessionManager;
463
- const command = splitArgs(args)[0];
464
- let retry;
465
- let mayResume = false;
466
- foreground.set(manager, (foreground.get(manager) ?? 0) + 1);
467
- 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];
468
369
  const ready = await initialization;
469
- mayResume = ready !== false;
470
- if (signal.aborted) return;
471
- await continuous?.stop(manager);
472
- if (signal.aborted) return;
473
- await check.stop();
474
- if (signal.aborted) return;
475
- if (ready === false && command !== "help" && command !== "unlock" && command !== "migrate-state") {
476
- ctx.ui.notify(
477
- "pi-sync recovery required. Repair the startup error, then /reload before syncing. Use /sync help for recovery guidance.",
478
- "error"
479
- );
480
- return;
481
- }
482
- 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) {
483
391
  if (!signal.aborted) {
484
392
  ctx.ui.notify(`pi-sync command unavailable: ${safeTerminalText(errorMessage(error))}`, "error");
393
+ if (command === "unlock" || command === "migrate-state") throw error;
485
394
  }
486
- });
487
- if (signal.aborted || !commands) return;
488
- const run = () => commands.handleCommand(args, ctx, signal, loaders, attention, (setup) => {
489
- retry = { setup };
490
- });
491
- if (command === "migrate-state" || command === "help") await run();
492
- else await withStateDirectoryAccess(run);
493
- } finally {
494
- const count = (foreground.get(manager) ?? 1) - 1;
495
- if (count) foreground.set(manager, count);
496
- else foreground.delete(manager);
497
- if (mayResume) await resumeContinuous(ctx, signal, retry);
498
- }
395
+ } finally {
396
+ if (!signal.aborted && owner === client) await client.setBusy("command", false);
397
+ }
398
+ });
499
399
  }
500
400
  });
501
401
  pi.on("session_start", async (_event, ctx) => {
502
402
  const releaseStatus = status.start(ctx);
503
- try {
504
- shutdownAbort?.abort(new DOMException("Session replaced", "AbortError"));
505
- shutdownAbort = void 0;
506
- sessionAbort.abort(new DOMException("Session replaced", "AbortError"));
507
- sessionAbort = new AbortController();
508
- const signal = sessionAbort.signal;
509
- owner = ctx.sessionManager;
510
- continuousPossible = false;
511
- startupDebug = false;
512
- attention.reset(ctx);
513
- const previous = initialization;
514
- initialization = (async () => {
515
- await previous;
516
- if (signal.aborted) return false;
517
- await continuous?.stop();
518
- 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 {
519
420
  await check.stop();
520
- if (signal.aborted) return false;
521
- try {
522
- const startup = await withStateDirectoryAccess(() => startSession(ctx, signal, publishTimings));
523
- if (signal.aborted) return false;
524
- startupDebug = startup?.debug ?? false;
525
- return startup?.config;
526
- } catch (error) {
527
- if (!signal.aborted && ctx.hasUI) {
528
- 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 {
529
429
  }
530
- return false;
430
+ await client.finishReattachment();
531
431
  }
532
- })();
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 {
533
447
  const ready = await initialization;
534
448
  if (ready === false) releaseStatus();
535
- if (ready && !signal.aborted) {
536
- if (ready.continuous && ready.agentMode) await resumeContinuous(ctx, signal);
537
- else if (ready.automatic || startupTransferEnabled(ready)) check.start(ctx, signal, ready);
538
- }
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
+ });
539
453
  } catch (error) {
540
454
  releaseStatus();
541
455
  throw error;
542
456
  }
543
457
  });
544
458
  pi.on("agent_start", async (_event, ctx) => {
545
- await continuous?.stop(ctx.sessionManager);
546
- 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);
547
464
  });
548
465
  pi.on("agent_settled", async (_event, ctx) => {
549
- const signal = sessionAbort.signal;
550
- if (!signal.aborted) {
551
- check.settled(ctx);
552
- if (continuousPossible) await resumeContinuous(ctx, signal);
553
- }
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
+ });
554
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);
555
492
  pi.on("session_shutdown", async (event, ctx) => {
556
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;
557
507
  try {
558
- if (continuous && owner && owner !== ctx.sessionManager) return;
559
- sessionAbort.abort(new DOMException("Session shut down", "AbortError"));
560
- attention.reset(ctx);
561
- shutdownAbort?.abort(new DOMException("Session shut down again", "AbortError"));
562
- const controller = new AbortController();
563
- shutdownAbort = controller;
564
- const signal = combineSignals(controller.signal, AbortSignal.timeout(3e4));
565
- const reason = typeof event === "object" && event ? event.reason : void 0;
566
- try {
567
- const ready = await initialization;
568
- if (signal.aborted) return;
569
- await continuous?.stop(ctx.sessionManager);
570
- if (signal.aborted) return;
571
- await check.stop();
572
- if (signal.aborted) return;
573
- if (ready !== false && reason !== "reload") {
574
- await withStateDirectoryAccess(async () => {
575
- if (signal.aborted) return;
576
- await autoPushSessions(ctx, signal, loaders);
577
- });
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 {
578
524
  }
579
- } catch (error) {
580
- if (!signal.aborted) {
581
- 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 {
582
533
  }
534
+ }
535
+ try {
536
+ await client.disposeForShutdown(reason, target);
583
537
  } finally {
584
- if (shutdownAbort === controller) shutdownAbort = void 0;
538
+ try {
539
+ await shutdownClient?.disposeForShutdown(reason, target);
540
+ } finally {
541
+ controller.abort();
542
+ releaseStatus();
543
+ }
585
544
  }
586
- if (signal.aborted) return;
587
- setSyncStatus(ctx, void 0);
588
- } finally {
589
- releaseStatus();
545
+ if (shutdownAbort === controller) shutdownAbort = void 0;
590
546
  }
591
547
  });
592
548
  }