@narumitw/pi-sync 0.61.0 → 0.63.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 (278) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +46 -25
  3. package/dist/chunks/{chunk-277VHM2P.ts → chunk-27F5W7DX.ts} +7 -3
  4. package/dist/chunks/chunk-27F5W7DX.ts.map +7 -0
  5. package/dist/chunks/{chunk-ZIL6PW5L.ts → chunk-6YKOHBSC.ts} +2 -2
  6. package/dist/chunks/{chunk-OA4NZLSL.ts → chunk-APZVJAEW.ts} +4 -3
  7. package/dist/chunks/chunk-CKUGCBHP.ts +114 -0
  8. package/dist/chunks/chunk-CKUGCBHP.ts.map +7 -0
  9. package/dist/chunks/chunk-EJDDRS6O.ts +17 -0
  10. package/dist/chunks/chunk-EJDDRS6O.ts.map +7 -0
  11. package/dist/chunks/{chunk-JVFCVPPD.ts → chunk-EXDWEFIJ.ts} +87 -147
  12. package/dist/chunks/chunk-EXDWEFIJ.ts.map +7 -0
  13. package/dist/chunks/{chunk-4SR3ZYHA.ts → chunk-GELYN4UC.ts} +2 -2
  14. package/dist/chunks/{chunk-2ZGV5KNC.ts → chunk-HSCAFOT5.ts} +15 -12
  15. package/dist/chunks/chunk-HSCAFOT5.ts.map +7 -0
  16. package/dist/chunks/{chunk-YQSFJLJ3.ts → chunk-IBXEM35O.ts} +4 -3
  17. package/dist/chunks/{chunk-2JHAN3ZO.ts → chunk-JREGWM2B.ts} +3 -3
  18. package/dist/chunks/chunk-MANNB4WZ.ts +36 -0
  19. package/dist/chunks/chunk-MANNB4WZ.ts.map +7 -0
  20. package/dist/chunks/{chunk-EMRFVLZM.ts → chunk-NXWHPVXR.ts} +2 -2
  21. package/dist/chunks/{chunk-SMVKMYLK.ts → chunk-P4GI7LTT.ts} +2 -2
  22. package/dist/chunks/{chunk-ZVGRZD4C.ts → chunk-R3SAMXB6.ts} +2 -2
  23. package/dist/chunks/{chunk-4KTJLJSJ.ts → chunk-S6FUUWV5.ts} +148 -21
  24. package/dist/chunks/chunk-S6FUUWV5.ts.map +7 -0
  25. package/dist/chunks/{chunk-PJMM75W5.ts → chunk-SXCH4Q5O.ts} +41 -23
  26. package/dist/chunks/chunk-SXCH4Q5O.ts.map +7 -0
  27. package/dist/chunks/{chunk-RNIEVDTR.ts → chunk-T3LABNHF.ts} +2 -2
  28. package/dist/chunks/{chunk-E3YLRLJM.ts → chunk-UGS6RYDZ.ts} +14 -12
  29. package/dist/chunks/chunk-UGS6RYDZ.ts.map +7 -0
  30. package/dist/chunks/{chunk-S3R3W3KF.ts → chunk-UMPA5S7X.ts} +2 -2
  31. package/dist/chunks/{chunk-EMVZL4PH.ts → chunk-V76KCWHO.ts} +7 -5
  32. package/dist/chunks/{chunk-EMVZL4PH.ts.map → chunk-V76KCWHO.ts.map} +1 -1
  33. package/dist/chunks/{chunk-S7F4FOAS.ts → chunk-VQ47JBLF.ts} +5 -5
  34. package/dist/chunks/{chunk-WP2M67XZ.ts → chunk-WBIOSK76.ts} +2 -2
  35. package/dist/chunks/chunk-WFBHWYI6.ts +18 -0
  36. package/dist/chunks/chunk-WFBHWYI6.ts.map +7 -0
  37. package/dist/chunks/{chunk-HEB6YT64.ts → chunk-ZO52JM4H.ts} +2 -2
  38. package/dist/chunks/{command-handler-K4XJOGYC.ts → command-handler-VINMQ7ZM.ts} +26 -43
  39. package/dist/chunks/command-handler-VINMQ7ZM.ts.map +7 -0
  40. package/dist/chunks/{conflict-review-IGKY2TOJ.ts → conflict-review-PCDFRIVB.ts} +4 -4
  41. package/dist/chunks/{continuous-record-3XDXDOMV.ts → continuous-record-3SKJ6I3V.ts} +2 -2
  42. package/dist/chunks/{continuous-storage-HLKCHF6Z.ts → continuous-storage-3EU6I7Z3.ts} +2 -2
  43. package/dist/chunks/{file-selection-MGGVOKM7.ts → file-selection-KID23GAD.ts} +21 -14
  44. package/dist/chunks/file-selection-KID23GAD.ts.map +7 -0
  45. package/dist/chunks/manager-result-dispatcher-GDB74IOP.ts +20 -0
  46. package/dist/chunks/{manager-ui-KYQ5AZJS.ts → manager-ui-YHDECYKI.ts} +454 -167
  47. package/dist/chunks/manager-ui-YHDECYKI.ts.map +7 -0
  48. package/dist/chunks/remote-selection-ui-GPITNEBF.ts +19 -0
  49. package/dist/chunks/{setup-switch-D6K6PBPC.ts → setup-switch-L54X5U5I.ts} +7 -7
  50. package/dist/chunks/{setup-wizard-4DYUDHJR.ts → setup-wizard-YO4MCPUF.ts} +15 -11
  51. package/dist/chunks/{setup-wizard-4DYUDHJR.ts.map → setup-wizard-YO4MCPUF.ts.map} +1 -1
  52. package/dist/chunks/{snapshot-5HY3M2NL.ts → snapshot-ZUJJZCFW.ts} +2 -2
  53. package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts → snapshot-transaction-directory-F46JWRAU.ts} +2 -2
  54. package/dist/chunks/{state-directory-XXHU73Z3.ts → state-directory-CMAIF34J.ts} +2 -2
  55. package/dist/chunks/{sync-inspection-J3CRBORL.ts → sync-inspection-H2WX5SUT.ts} +5 -5
  56. package/dist/chunks/{sync-operations-BVWYYKH3.ts → sync-operations-2MZAYHL2.ts} +13 -9
  57. package/dist/chunks/sync-operations-2MZAYHL2.ts.map +7 -0
  58. package/dist/chunks/{sync-state-2LMIXJ2C.ts → sync-state-QIT64TKV.ts} +2 -2
  59. package/dist/daemon/LICENSE +21 -0
  60. package/dist/daemon/dist/build-version.js +8 -0
  61. package/dist/daemon/dist/child-sdk.mjs +17 -0
  62. package/dist/daemon/dist/cli.js +169 -0
  63. package/dist/daemon/dist/client.js +909 -0
  64. package/dist/daemon/dist/consent.js +383 -0
  65. package/dist/daemon/dist/document.js +6 -0
  66. package/dist/daemon/dist/endpoint.js +91 -0
  67. package/dist/daemon/dist/engine/backends/backend-factory.js +16 -0
  68. package/dist/daemon/dist/engine/backends/backend-identity.js +38 -0
  69. package/dist/daemon/dist/engine/backends/backend-types.js +1 -0
  70. package/dist/daemon/dist/engine/backends/git/git-backend.js +746 -0
  71. package/dist/daemon/dist/engine/backends/git/git-config.js +118 -0
  72. package/dist/daemon/dist/engine/backends/git/git-runner.js +232 -0
  73. package/dist/daemon/dist/engine/backends/git/git-storage.js +252 -0
  74. package/dist/daemon/dist/engine/backends/s3/s3-backend.js +444 -0
  75. package/dist/daemon/dist/engine/backends/s3/s3-client.js +352 -0
  76. package/dist/daemon/dist/engine/backends/s3/s3-conditions.js +66 -0
  77. package/dist/daemon/dist/engine/backends/snapshot-pointer.js +22 -0
  78. package/dist/daemon/dist/engine/backends/sync-backend.js +23 -0
  79. package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +542 -0
  80. package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +326 -0
  81. package/dist/daemon/dist/engine/backends/webdav/webdav-config.js +73 -0
  82. package/dist/daemon/dist/engine/commands/command-types.js +1 -0
  83. package/dist/daemon/dist/engine/host.js +16 -0
  84. package/dist/daemon/dist/engine/ignore/effective-ignore-policy.js +33 -0
  85. package/dist/daemon/dist/engine/ignore/ignore-edit.js +102 -0
  86. package/dist/daemon/dist/engine/ignore/ignore-initialization.js +252 -0
  87. package/dist/daemon/dist/engine/ignore/ignore-migration-prompt.js +139 -0
  88. package/dist/daemon/dist/engine/ignore/ignore-settings.js +393 -0
  89. package/dist/daemon/dist/engine/ignore/ignore-source.js +250 -0
  90. package/dist/daemon/dist/engine/paths.js +71 -0
  91. package/dist/daemon/dist/engine/settings/config-errors.js +4 -0
  92. package/dist/daemon/dist/engine/settings/config-file.js +736 -0
  93. package/dist/daemon/dist/engine/settings/config.js +344 -0
  94. package/dist/daemon/dist/engine/settings/settings-management.js +199 -0
  95. package/dist/daemon/dist/engine/settings/settings-store.js +41 -0
  96. package/dist/daemon/dist/engine/settings/settings-types.js +1 -0
  97. package/dist/daemon/dist/engine/settings/settings-validation.js +384 -0
  98. package/dist/daemon/dist/engine/snapshot/session-paths.js +165 -0
  99. package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +348 -0
  100. package/dist/daemon/dist/engine/snapshot/snapshot-bounds.js +20 -0
  101. package/dist/daemon/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
  102. package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +61 -0
  103. package/dist/daemon/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
  104. package/dist/daemon/dist/engine/snapshot/snapshot-exclude-safety.js +262 -0
  105. package/dist/daemon/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
  106. package/dist/daemon/dist/engine/snapshot/snapshot-paths.js +11 -0
  107. package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +10 -0
  108. package/dist/daemon/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
  109. package/dist/daemon/dist/engine/snapshot/snapshot-root-transition.js +138 -0
  110. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
  111. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
  112. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
  113. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-policy.js +296 -0
  114. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
  115. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
  116. package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +855 -0
  117. package/dist/daemon/dist/engine/snapshot/snapshot-types.js +9 -0
  118. package/dist/daemon/dist/engine/snapshot/snapshot.js +678 -0
  119. package/dist/daemon/dist/engine/state/json-file.js +49 -0
  120. package/dist/daemon/dist/engine/state/lock-policy.js +2 -0
  121. package/dist/daemon/dist/engine/state/lock.js +259 -0
  122. package/dist/daemon/dist/engine/state/lockfile-fs.js +13 -0
  123. package/dist/daemon/dist/engine/state/merge-baseline-store.js +198 -0
  124. package/dist/daemon/dist/engine/state/mutation-directory-sync.js +34 -0
  125. package/dist/daemon/dist/engine/state/operation-availability.js +40 -0
  126. package/dist/daemon/dist/engine/state/state-directory.js +227 -0
  127. package/dist/daemon/dist/engine/state/state-types.js +1 -0
  128. package/dist/daemon/dist/engine/state/sync-state-store.js +54 -0
  129. package/dist/daemon/dist/engine/sync/agent-candidate.js +207 -0
  130. package/dist/daemon/dist/engine/sync/agent-plan.js +85 -0
  131. package/dist/daemon/dist/engine/sync/agent-policy.js +10 -0
  132. package/dist/daemon/dist/engine/sync/agent-refusal-retry.js +39 -0
  133. package/dist/daemon/dist/engine/sync/agent-request.js +13 -0
  134. package/dist/daemon/dist/engine/sync/agent-runner.js +183 -0
  135. package/dist/daemon/dist/engine/sync/agent-store.js +177 -0
  136. package/dist/daemon/dist/engine/sync/agent-sync.js +139 -0
  137. package/dist/daemon/dist/engine/sync/apply-review.js +52 -0
  138. package/dist/daemon/dist/engine/sync/conflict-artifact-ownership.js +53 -0
  139. package/dist/daemon/dist/engine/sync/conflict-artifacts.js +158 -0
  140. package/dist/daemon/dist/engine/sync/conflict-groups.js +76 -0
  141. package/dist/daemon/dist/engine/sync/conflict-preview.js +53 -0
  142. package/dist/daemon/dist/engine/sync/conflict-resolution.js +53 -0
  143. package/dist/daemon/dist/engine/sync/conflict-retention.js +61 -0
  144. package/dist/daemon/dist/engine/sync/conflict-review.js +73 -0
  145. package/dist/daemon/dist/engine/sync/content-conflicts.js +70 -0
  146. package/dist/daemon/dist/engine/sync/continuous-errors.js +66 -0
  147. package/dist/daemon/dist/engine/sync/continuous-lease.js +206 -0
  148. package/dist/daemon/dist/engine/sync/continuous-record.js +199 -0
  149. package/dist/daemon/dist/engine/sync/continuous-storage.js +59 -0
  150. package/dist/daemon/dist/engine/sync/continuous-sync.js +538 -0
  151. package/dist/daemon/dist/engine/sync/continuous-watch.js +140 -0
  152. package/dist/daemon/dist/engine/sync/exclude-policy.js +86 -0
  153. package/dist/daemon/dist/engine/sync/field-migration.js +20 -0
  154. package/dist/daemon/dist/engine/sync/file-merge-planner.js +123 -0
  155. package/dist/daemon/dist/engine/sync/initialization.js +80 -0
  156. package/dist/daemon/dist/engine/sync/json-document.js +172 -0
  157. package/dist/daemon/dist/engine/sync/local-field-policy.js +26 -0
  158. package/dist/daemon/dist/engine/sync/local-fields.js +196 -0
  159. package/dist/daemon/dist/engine/sync/merge-acceptance.js +29 -0
  160. package/dist/daemon/dist/engine/sync/merge-apply.js +532 -0
  161. package/dist/daemon/dist/engine/sync/merge-journal.js +314 -0
  162. package/dist/daemon/dist/engine/sync/merge-transfer-error.js +64 -0
  163. package/dist/daemon/dist/engine/sync/merged-sync.js +666 -0
  164. package/dist/daemon/dist/engine/sync/mutation-owner.js +18 -0
  165. package/dist/daemon/dist/engine/sync/package-merge.js +82 -0
  166. package/dist/daemon/dist/engine/sync/partial-progress.js +41 -0
  167. package/dist/daemon/dist/engine/sync/remote-snapshot.js +119 -0
  168. package/dist/daemon/dist/engine/sync/resolver-stream.js +34 -0
  169. package/dist/daemon/dist/engine/sync/session-merge.js +178 -0
  170. package/dist/daemon/dist/engine/sync/session-push.js +47 -0
  171. package/dist/daemon/dist/engine/sync/settings-conflicts.js +56 -0
  172. package/dist/daemon/dist/engine/sync/settings-merge.js +93 -0
  173. package/dist/daemon/dist/engine/sync/signals.js +8 -0
  174. package/dist/daemon/dist/engine/sync/startup-timings.js +44 -0
  175. package/dist/daemon/dist/engine/sync/sync-decision.js +81 -0
  176. package/dist/daemon/dist/engine/sync/sync-error-guidance.js +55 -0
  177. package/dist/daemon/dist/engine/sync/sync-errors.js +18 -0
  178. package/dist/daemon/dist/engine/sync/sync-inspection.js +42 -0
  179. package/dist/daemon/dist/engine/sync/sync-loaders.js +8 -0
  180. package/dist/daemon/dist/engine/sync/sync-local.js +40 -0
  181. package/dist/daemon/dist/engine/sync/sync-mutations.js +754 -0
  182. package/dist/daemon/dist/engine/sync/sync-operations.js +3 -0
  183. package/dist/daemon/dist/engine/sync/sync-policy.js +646 -0
  184. package/dist/daemon/dist/engine/sync/sync-queries.js +212 -0
  185. package/dist/daemon/dist/engine/sync/sync-state.js +124 -0
  186. package/dist/daemon/dist/engine/sync/text-merge.js +124 -0
  187. package/dist/daemon/dist/engine/sync/transfer-freshness.js +82 -0
  188. package/dist/daemon/dist/engine/types.js +2 -0
  189. package/dist/daemon/dist/engine/ui/merge-review.js +7 -0
  190. package/dist/daemon/dist/engine/ui/merged-sync-summary.js +86 -0
  191. package/dist/daemon/dist/engine/ui/sync-format.js +165 -0
  192. package/dist/daemon/dist/engine/ui/sync-status.js +30 -0
  193. package/dist/daemon/dist/engine/ui/terminal-text.js +9 -0
  194. package/dist/daemon/dist/engine/ui/transfer-changes.js +24 -0
  195. package/dist/daemon/dist/engine/ui/transfer-review.js +19 -0
  196. package/dist/daemon/dist/engine-service.js +441 -0
  197. package/dist/daemon/dist/identity.js +241 -0
  198. package/dist/daemon/dist/idle-exit.js +77 -0
  199. package/dist/daemon/dist/index.js +4 -0
  200. package/dist/daemon/dist/lifecycle-log.js +53 -0
  201. package/dist/daemon/dist/lifecycle.js +21 -0
  202. package/dist/daemon/dist/model-signature.js +7 -0
  203. package/dist/daemon/dist/monitor.js +324 -0
  204. package/dist/daemon/dist/participant-admission.js +30 -0
  205. package/dist/daemon/dist/protocol.js +104 -0
  206. package/dist/daemon/dist/resolver-host.js +78 -0
  207. package/dist/daemon/dist/server.js +994 -0
  208. package/dist/daemon/dist/service-host.js +175 -0
  209. package/dist/daemon/dist/upgrade-repair.js +55 -0
  210. package/dist/daemon/package.json +3 -0
  211. package/dist/index.ts +40 -37
  212. package/dist/index.ts.map +4 -4
  213. package/dist/package.json +6 -0
  214. package/docs/daemon-migration.md +27 -16
  215. package/package.json +9 -3
  216. package/skills/configure-pi-sync/SKILL.md +24 -46
  217. package/skills/configure-pi-sync/references/configuration.md +62 -22
  218. package/src/commands/command-execution.ts +13 -9
  219. package/src/commands/command-types.ts +1 -0
  220. package/src/daemon/pi-client.ts +4 -2
  221. package/src/settings/config.ts +2 -0
  222. package/src/sync/automatic-sync.ts +9 -2
  223. package/src/sync/setup-switch.ts +38 -15
  224. package/src/sync/startup-check.ts +7 -4
  225. package/src/sync/sync-operations.ts +18 -10
  226. package/src/ui/excluded-paths-ui.ts +6 -3
  227. package/src/ui/file-selection.ts +7 -2
  228. package/src/ui/ignore-file-guidance.ts +7 -0
  229. package/src/ui/ignore-file-ui.ts +97 -0
  230. package/src/ui/ignore-migration-prompt.ts +65 -0
  231. package/src/ui/manager-state.ts +40 -14
  232. package/src/ui/manager-ui.ts +41 -19
  233. package/src/ui/remote-selection-ui.ts +88 -28
  234. package/src/ui/settings-review.ts +26 -0
  235. package/src/ui/settings-ui.ts +378 -224
  236. package/src/ui/setup/setup-actions.ts +16 -4
  237. package/src/ui/setup/setup-prompts.ts +55 -9
  238. package/src/ui/setup/setup-switcher.ts +24 -11
  239. package/src/ui/storage-connections-ui.ts +50 -20
  240. package/src/ui/sync-setups-ui.ts +65 -30
  241. package/dist/chunks/chunk-277VHM2P.ts.map +0 -7
  242. package/dist/chunks/chunk-2ZGV5KNC.ts.map +0 -7
  243. package/dist/chunks/chunk-4KTJLJSJ.ts.map +0 -7
  244. package/dist/chunks/chunk-E3YLRLJM.ts.map +0 -7
  245. package/dist/chunks/chunk-ECYCNZOE.ts +0 -12
  246. package/dist/chunks/chunk-ECYCNZOE.ts.map +0 -7
  247. package/dist/chunks/chunk-JVFCVPPD.ts.map +0 -7
  248. package/dist/chunks/chunk-PJMM75W5.ts.map +0 -7
  249. package/dist/chunks/command-handler-K4XJOGYC.ts.map +0 -7
  250. package/dist/chunks/file-selection-MGGVOKM7.ts.map +0 -7
  251. package/dist/chunks/manager-result-dispatcher-4VJVDEUD.ts +0 -19
  252. package/dist/chunks/manager-ui-KYQ5AZJS.ts.map +0 -7
  253. package/dist/chunks/remote-selection-ui-W3KOZM3W.ts +0 -18
  254. package/dist/chunks/sync-operations-BVWYYKH3.ts.map +0 -7
  255. /package/dist/chunks/{chunk-ZIL6PW5L.ts.map → chunk-6YKOHBSC.ts.map} +0 -0
  256. /package/dist/chunks/{chunk-OA4NZLSL.ts.map → chunk-APZVJAEW.ts.map} +0 -0
  257. /package/dist/chunks/{chunk-4SR3ZYHA.ts.map → chunk-GELYN4UC.ts.map} +0 -0
  258. /package/dist/chunks/{chunk-YQSFJLJ3.ts.map → chunk-IBXEM35O.ts.map} +0 -0
  259. /package/dist/chunks/{chunk-2JHAN3ZO.ts.map → chunk-JREGWM2B.ts.map} +0 -0
  260. /package/dist/chunks/{chunk-EMRFVLZM.ts.map → chunk-NXWHPVXR.ts.map} +0 -0
  261. /package/dist/chunks/{chunk-SMVKMYLK.ts.map → chunk-P4GI7LTT.ts.map} +0 -0
  262. /package/dist/chunks/{chunk-ZVGRZD4C.ts.map → chunk-R3SAMXB6.ts.map} +0 -0
  263. /package/dist/chunks/{chunk-RNIEVDTR.ts.map → chunk-T3LABNHF.ts.map} +0 -0
  264. /package/dist/chunks/{chunk-S3R3W3KF.ts.map → chunk-UMPA5S7X.ts.map} +0 -0
  265. /package/dist/chunks/{chunk-S7F4FOAS.ts.map → chunk-VQ47JBLF.ts.map} +0 -0
  266. /package/dist/chunks/{chunk-WP2M67XZ.ts.map → chunk-WBIOSK76.ts.map} +0 -0
  267. /package/dist/chunks/{chunk-HEB6YT64.ts.map → chunk-ZO52JM4H.ts.map} +0 -0
  268. /package/dist/chunks/{conflict-review-IGKY2TOJ.ts.map → conflict-review-PCDFRIVB.ts.map} +0 -0
  269. /package/dist/chunks/{continuous-record-3XDXDOMV.ts.map → continuous-record-3SKJ6I3V.ts.map} +0 -0
  270. /package/dist/chunks/{continuous-storage-HLKCHF6Z.ts.map → continuous-storage-3EU6I7Z3.ts.map} +0 -0
  271. /package/dist/chunks/{manager-result-dispatcher-4VJVDEUD.ts.map → manager-result-dispatcher-GDB74IOP.ts.map} +0 -0
  272. /package/dist/chunks/{remote-selection-ui-W3KOZM3W.ts.map → remote-selection-ui-GPITNEBF.ts.map} +0 -0
  273. /package/dist/chunks/{setup-switch-D6K6PBPC.ts.map → setup-switch-L54X5U5I.ts.map} +0 -0
  274. /package/dist/chunks/{snapshot-5HY3M2NL.ts.map → snapshot-ZUJJZCFW.ts.map} +0 -0
  275. /package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts.map → snapshot-transaction-directory-F46JWRAU.ts.map} +0 -0
  276. /package/dist/chunks/{state-directory-XXHU73Z3.ts.map → state-directory-CMAIF34J.ts.map} +0 -0
  277. /package/dist/chunks/{sync-inspection-J3CRBORL.ts.map → sync-inspection-H2WX5SUT.ts.map} +0 -0
  278. /package/dist/chunks/{sync-state-2LMIXJ2C.ts.map → sync-state-QIT64TKV.ts.map} +0 -0
@@ -0,0 +1,9 @@
1
+ export const IGNORE_SNAPSHOT_VERSION = 6;
2
+ export function isSupportedSnapshotVersion(version) {
3
+ return (version === 1 ||
4
+ version === 2 ||
5
+ version === 3 ||
6
+ version === 4 ||
7
+ version === 5 ||
8
+ version === IGNORE_SNAPSHOT_VERSION);
9
+ }
@@ -0,0 +1,678 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { constants } from "node:fs";
3
+ import fs from "node:fs/promises";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { isDeniedPath, posixJoin, safeJoin, toPosix } from "../paths.js";
7
+ import { agentDir, configuredSessionDir, expandSessionDir } from "./session-paths.js";
8
+ import { assertAgentDirectorySettingsSize, MAX_AGENT_DIRECTORY_BYTES } from "./snapshot-bounds.js";
9
+ import { agentDirectoryAnchor, agentDirectoryPhysicalSessionPath, agentDirectorySessionRoot, reservedIgnoreTargetGuard, } from "./snapshot-exclude-safety.js";
10
+ import { sessionStorageRoot } from "./snapshot-paths.js";
11
+ export { isDeniedPath } from "../paths.js";
12
+ export { sessionStorageRoot } from "./snapshot-paths.js";
13
+ import { compileSyncExclude } from "../sync/exclude-policy.js";
14
+ import { assertAgentDirectoryPathLayout, canonicalSnapshotPathForConfig, compileSyncPolicyExclude, compileSyncPolicyTraversal, configuredSnapshotPathMatcher, customIncludePathsByLower, DEFAULT_SYNC_INCLUDE, includeFromSelectionConfig, isIgnoreControlPath, isPreservableUnmanagedSnapshotPath, MAX_AGENT_DIRECTORY_ENTRIES, MAX_AGENT_DIRECTORY_FILES, normalizeExtraFiles, normalizeSyncFiles, requireLegacyTransferPolicy, sameSnapshotSelection, selectionForSnapshot, snapshotSelectionExclude, snapshotSelectionInclude, syncIncludeSelection, syncSessionsSelected, } from "../sync/sync-policy.js";
15
+ import { IGNORE_SNAPSHOT_VERSION, } from "./snapshot-types.js";
16
+ export { canonicalSnapshotPathForConfig, isConfiguredSnapshotPath } from "../sync/sync-policy.js";
17
+ const VERSION = 1;
18
+ const scanInventories = new WeakMap();
19
+ export function agentDirectoryInventory(snapshot) {
20
+ return scanInventories.get(snapshot);
21
+ }
22
+ const TOP_LEVEL_FILES = DEFAULT_SYNC_INCLUDE.filter((name) => name.includes("."));
23
+ const TOP_LEVEL_DIRS = new Set(DEFAULT_SYNC_INCLUDE.filter((name) => !name.includes(".")));
24
+ const SECRET_PATTERNS = [
25
+ /AWS_SECRET_ACCESS_KEY\s*[=:]\s*['"]?[A-Za-z0-9/+]{35,}/i,
26
+ /(ANTHROPIC|OPENAI|GEMINI|GOOGLE|FIRECRAWL|GITHUB|CLOUDFLARE|R2|S3)_[A-Z0-9_]*(KEY|TOKEN|SECRET)\s*[=:]\s*['"]?[^\s'"]{12,}/i,
27
+ /sk-ant-[A-Za-z0-9_-]{20,}/,
28
+ /sk-[A-Za-z0-9]{20,}/,
29
+ /gh[pousr]_[A-Za-z0-9_]{20,}/,
30
+ ];
31
+ function selectTopLevelFileEntry(entries, fileName) {
32
+ const exact = entries.find((entry) => entry.isFile() && entry.name === fileName);
33
+ if (exact)
34
+ return exact;
35
+ const lower = fileName.toLowerCase();
36
+ return entries
37
+ .filter((entry) => entry.isFile() && entry.name.toLowerCase() === lower)
38
+ .sort((left, right) => left.name.localeCompare(right.name))[0];
39
+ }
40
+ function sha256(value) {
41
+ return createHash("sha256").update(value).digest("hex");
42
+ }
43
+ function isSafeSnapshotPath(relativePath) {
44
+ if (relativePath.includes("\\"))
45
+ return false;
46
+ const normalized = toPosix(relativePath);
47
+ return (Boolean(normalized) &&
48
+ normalized !== "." &&
49
+ normalized !== ".." &&
50
+ !normalized.startsWith("../") &&
51
+ !path.posix.isAbsolute(normalized) &&
52
+ path.posix.normalize(normalized) === normalized &&
53
+ !isDeniedPath(normalized));
54
+ }
55
+ function snapshotsMatch(left, right) {
56
+ const leftHashes = new Map(left.files.map((file) => [file.path, file.sha256]));
57
+ const rightHashes = new Map(right.files.map((file) => [file.path, file.sha256]));
58
+ return (left.syncSessions === right.syncSessions &&
59
+ sameSnapshotSelection(left.selection, right.selection) &&
60
+ leftHashes.size === rightHashes.size &&
61
+ [...leftHashes].every(([filePath, hash]) => rightHashes.get(filePath) === hash));
62
+ }
63
+ function snapshotId() {
64
+ return `${new Date().toISOString().replace(/[:.]/g, "-")}-${randomUUID().slice(0, 8)}`;
65
+ }
66
+ export function regenerateSnapshotIdentity(snapshot) {
67
+ return {
68
+ ...snapshot,
69
+ id: snapshotId(),
70
+ createdAt: new Date().toISOString(),
71
+ machine: os.hostname(),
72
+ };
73
+ }
74
+ export async function createSnapshot(profile, options = {}) {
75
+ options.signal?.throwIfAborted();
76
+ const include = effectiveInclude(options);
77
+ const policy = { ...options, include };
78
+ const excluded = compileSyncPolicyExclude(policy);
79
+ const syncSessions = syncSessionsSelected(policy);
80
+ const needsSessionConfig = syncSessions || !excluded("settings.json");
81
+ const sessionDir = options.scope === "agent-directory"
82
+ ? options.sessionDir
83
+ : (options.sessionDir ?? (needsSessionConfig ? await configuredSessionDir(options) : undefined));
84
+ options.signal?.throwIfAborted();
85
+ const collectionOptions = { ...policy, sessionDir };
86
+ const collected = options.scope === "agent-directory" ? await collectAgentDirectory(agentDir(), collectionOptions) : undefined;
87
+ const files = collected?.files ?? (await collectFiles(agentDir(), collectionOptions, excluded));
88
+ options.signal?.throwIfAborted();
89
+ const selection = selectionForSnapshot(include, options.exclude, options);
90
+ const snapshot = {
91
+ version: selection.version === 4
92
+ ? IGNORE_SNAPSHOT_VERSION
93
+ : options.scope === "agent-directory"
94
+ ? 5
95
+ : options.exclude === undefined
96
+ ? VERSION
97
+ : 4,
98
+ id: snapshotId(),
99
+ createdAt: new Date().toISOString(),
100
+ machine: os.hostname(),
101
+ profile,
102
+ syncSessions: selection.version === 4 ? files.some((file) => isSessionPath(file.path)) : syncSessions,
103
+ selection,
104
+ files,
105
+ };
106
+ if (collected)
107
+ scanInventories.set(snapshot, collected.inventory);
108
+ return snapshot;
109
+ }
110
+ function effectiveInclude(options) {
111
+ if (options.include)
112
+ return options.include;
113
+ if (options.scope === "agent-directory")
114
+ return [];
115
+ return [
116
+ ...normalizeSyncFiles(options.syncFiles),
117
+ ...normalizeExtraFiles(options.extraFiles),
118
+ ...(options.syncSessions ? ["sessions"] : []),
119
+ ];
120
+ }
121
+ export async function collectFiles(root, options = {}, excluded = compileSyncPolicyExclude(options)) {
122
+ if (options.scope === "agent-directory")
123
+ return (await collectAgentDirectory(root, options)).files;
124
+ const signal = options.signal;
125
+ const guardReserved = reservedIgnoreTargetGuard(root, options);
126
+ const collection = { signal, excluded, guardReserved };
127
+ signal?.throwIfAborted();
128
+ const results = [];
129
+ const entries = await fs.readdir(root, { withFileTypes: true });
130
+ signal?.throwIfAborted();
131
+ const selection = syncIncludeSelection(effectiveInclude(options));
132
+ const selectedFiles = new Set(selection.builtIns);
133
+ for (const entry of entries) {
134
+ if (entry.isDirectory() &&
135
+ TOP_LEVEL_DIRS.has(entry.name) &&
136
+ selectedFiles.has(entry.name) &&
137
+ !excluded(entry.name)) {
138
+ await collectDirectory(results, root, entry.name, collection);
139
+ }
140
+ }
141
+ for (const fileName of TOP_LEVEL_FILES) {
142
+ if (!selectedFiles.has(fileName) || excluded(fileName))
143
+ continue;
144
+ const entry = selectTopLevelFileEntry(entries, fileName);
145
+ if (entry)
146
+ await addFile(results, root, entry.name, fileName, collection);
147
+ }
148
+ for (const relativePath of selection.custom) {
149
+ if (!excluded(relativePath))
150
+ await collectIncludedPath(results, root, relativePath, collection);
151
+ }
152
+ if (selection.sessions && !excluded("sessions")) {
153
+ try {
154
+ await collectDirectory(results, sessionStorageRoot(root, options.sessionDir), "", {
155
+ ...collection,
156
+ sessionsOnly: true,
157
+ virtualPrefix: "sessions",
158
+ });
159
+ }
160
+ catch (error) {
161
+ if (error.code !== "ENOENT")
162
+ throw error;
163
+ }
164
+ }
165
+ signal?.throwIfAborted();
166
+ return results.sort((left, right) => left.path.localeCompare(right.path));
167
+ }
168
+ function sameScannedPath(left, right) {
169
+ return (left.dev === right.dev &&
170
+ left.ino === right.ino &&
171
+ left.mode === right.mode &&
172
+ left.nlink === right.nlink &&
173
+ left.size === right.size &&
174
+ left.mtimeMs === right.mtimeMs &&
175
+ left.ctimeMs === right.ctimeMs);
176
+ }
177
+ function changedScan() {
178
+ throw new Error("Agent directory changed or has an unsafe filesystem layout; no snapshot was collected.");
179
+ }
180
+ async function identify(absolute, canonical, signal) {
181
+ signal?.throwIfAborted();
182
+ const stat = await fs.lstat(absolute);
183
+ if (stat.isSymbolicLink() || (await fs.realpath(absolute)) !== canonical)
184
+ changedScan();
185
+ signal?.throwIfAborted();
186
+ return { absolute, canonical, stat };
187
+ }
188
+ async function recheck({ absolute, canonical, stat }, signal) {
189
+ if (!sameScannedPath((await identify(absolute, canonical, signal)).stat, stat))
190
+ changedScan();
191
+ }
192
+ async function readScannedFile(candidate, signal) {
193
+ await recheck(candidate, signal);
194
+ const handle = await fs.open(candidate.absolute, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
195
+ try {
196
+ if (!sameScannedPath(await handle.stat(), candidate.stat))
197
+ changedScan();
198
+ await recheck(candidate, signal);
199
+ const content = Buffer.alloc(candidate.stat.size);
200
+ for (let offset = 0; offset < content.length;) {
201
+ signal?.throwIfAborted();
202
+ const { bytesRead } = await handle.read(content, offset, content.length - offset, offset);
203
+ if (bytesRead === 0)
204
+ changedScan();
205
+ offset += bytesRead;
206
+ }
207
+ if (!sameScannedPath(await handle.stat(), candidate.stat))
208
+ changedScan();
209
+ return content;
210
+ }
211
+ finally {
212
+ await handle.close();
213
+ }
214
+ }
215
+ export async function agentDirectoryConfiguredSessionDir(signal, root = agentDir(), options = {}) {
216
+ const content = await readAgentDirectorySettings(signal, root, options);
217
+ return content === undefined ? undefined : agentDirectorySessionDirFromSettings(content);
218
+ }
219
+ export async function readAgentDirectorySettings(signal, root = agentDir(), options = {}) {
220
+ const guardReserved = reservedIgnoreTargetGuard(root, options);
221
+ if (guardReserved)
222
+ await guardReserved(path.join(root, "settings.json"));
223
+ const absolute = path.resolve(root);
224
+ const canonical = path.join(await fs.realpath(path.dirname(absolute)), path.basename(absolute));
225
+ await identify(absolute, canonical, signal);
226
+ let settings;
227
+ try {
228
+ settings = await identify(path.join(absolute, "settings.json"), path.join(canonical, "settings.json"), signal);
229
+ }
230
+ catch (error) {
231
+ if (error.code === "ENOENT")
232
+ return undefined;
233
+ throw error;
234
+ }
235
+ if (!settings.stat.isFile() || settings.stat.nlink !== 1)
236
+ changedScan();
237
+ assertAgentDirectorySettingsSize(settings.stat.size);
238
+ const content = await readScannedFile(settings, signal);
239
+ await recheck(settings, signal);
240
+ return content;
241
+ }
242
+ export function agentDirectorySessionDirFromSettings(content) {
243
+ assertAgentDirectorySettingsSize(content.length);
244
+ let value;
245
+ try {
246
+ value = JSON.parse(content.toString("utf8").replace(/^\uFEFF/u, ""))
247
+ ?.sessionDir;
248
+ }
249
+ catch {
250
+ throw new Error("Pi settings.json cannot be parsed; repair the file before syncing.");
251
+ }
252
+ if (value !== undefined && typeof value !== "string")
253
+ throw new Error("Pi sessionDir must be a string.");
254
+ return value ? expandSessionDir(value) : undefined;
255
+ }
256
+ export async function scanAgentDirectoryInventory(root, options, pendingStateEntry) {
257
+ return (await collectAgentDirectory(root, options, false, pendingStateEntry)).inventory;
258
+ }
259
+ async function collectAgentDirectory(root, options, readContents = true, pendingStateEntry) {
260
+ const signal = options.signal;
261
+ const excluded = compileSyncPolicyExclude(options);
262
+ const canTraverse = compileSyncPolicyTraversal(options);
263
+ const sessionDir = options.sessionDir ?? (await agentDirectoryConfiguredSessionDir(signal, root, options));
264
+ const isPhysicalSessionPath = agentDirectoryPhysicalSessionPath(root, sessionDir, options.ignorePolicy, options.reservedIgnoreNames);
265
+ const sessionRoot = agentDirectorySessionRoot(root, sessionDir, options.ignorePolicy, options.reservedIgnoreNames);
266
+ const directories = [];
267
+ const candidates = [];
268
+ const entries = new Map();
269
+ const routingDirectories = new Set();
270
+ let bytes = 0;
271
+ async function walk(directory, prefix, sessions) {
272
+ if (!directory.stat.isDirectory())
273
+ changedScan();
274
+ directories.push(directory);
275
+ const children = [];
276
+ for await (const entry of await fs.opendir(directory.absolute)) {
277
+ signal?.throwIfAborted();
278
+ const absolute = path.join(directory.absolute, entry.name);
279
+ entries.set(absolute, "unreviewed");
280
+ const allowance = pendingStateEntry !== undefined && entries.has(pendingStateEntry) ? 1 : 0;
281
+ if (entries.size > MAX_AGENT_DIRECTORY_ENTRIES + allowance)
282
+ throw new Error("Agent directory exceeds the scan entry bound.");
283
+ children.push(entry);
284
+ }
285
+ await recheck(directory, signal);
286
+ for (const entry of children) {
287
+ signal?.throwIfAborted();
288
+ const snapshotPath = posixJoin(prefix, entry.name);
289
+ if (!sessions && (/^sessions(?:\/|$)/iu.test(snapshotPath) || isPhysicalSessionPath(snapshotPath)))
290
+ continue;
291
+ if (entry.isDirectory() ? !canTraverse(snapshotPath) : !entry.isFile() || excluded(snapshotPath))
292
+ continue;
293
+ if (!entry.isDirectory() && sessions && !isSessionFilePath(snapshotPath))
294
+ continue;
295
+ const candidate = await identify(path.join(directory.absolute, entry.name), path.join(directory.canonical, entry.name), signal);
296
+ if (entry.isDirectory()) {
297
+ if (!excluded(snapshotPath, true))
298
+ entries.set(candidate.absolute, "directory");
299
+ await walk(candidate, snapshotPath, sessions);
300
+ }
301
+ else {
302
+ if (!candidate.stat.isFile() || candidate.stat.nlink !== 1)
303
+ changedScan();
304
+ if (snapshotPath.toLowerCase() === "settings.json")
305
+ assertAgentDirectorySettingsSize(candidate.stat.size);
306
+ bytes += candidate.stat.size;
307
+ if (bytes > MAX_AGENT_DIRECTORY_BYTES)
308
+ throw new Error("Agent directory exceeds the 64 MiB scan byte bound.");
309
+ if (candidates.length >= MAX_AGENT_DIRECTORY_FILES)
310
+ throw new Error("Agent directory exceeds the scan file bound.");
311
+ entries.set(candidate.absolute, "file");
312
+ candidates.push({ ...candidate, snapshotPath });
313
+ }
314
+ }
315
+ await recheck(directory, signal);
316
+ }
317
+ const absoluteRoot = path.resolve(root);
318
+ const canonicalRoot = path.join(await fs.realpath(path.dirname(absoluteRoot)), path.basename(absoluteRoot));
319
+ const selectedSessions = syncSessionsSelected(options);
320
+ async function identifySessionRoot(routing) {
321
+ if (!sessionDir && !selectedSessions)
322
+ return undefined;
323
+ let absolute = agentDirectoryAnchor(absoluteRoot, sessionRoot);
324
+ const parts = path.relative(absolute, sessionRoot).split(path.sep);
325
+ let canonical = await fs.realpath(absolute);
326
+ let directory;
327
+ try {
328
+ for (const part of parts) {
329
+ absolute = path.join(absolute, part);
330
+ canonical = path.join(canonical, part);
331
+ directory = await identify(absolute, canonical, signal);
332
+ if (!directory.stat.isDirectory())
333
+ changedScan();
334
+ routing?.add(absolute);
335
+ }
336
+ }
337
+ catch (error) {
338
+ if (error.code === "ENOENT")
339
+ return undefined;
340
+ throw error;
341
+ }
342
+ return directory;
343
+ }
344
+ const sessionDirectory = await identifySessionRoot(routingDirectories);
345
+ await walk(await identify(absoluteRoot, canonicalRoot, signal), "", false);
346
+ if (selectedSessions && sessionDirectory)
347
+ await walk(sessionDirectory, "sessions", true);
348
+ assertAgentDirectoryPathLayout(candidates.map((candidate) => candidate.snapshotPath));
349
+ const results = [];
350
+ if (readContents) {
351
+ for (const candidate of candidates) {
352
+ const content = await readScannedFile(candidate, signal);
353
+ results.push({
354
+ path: candidate.snapshotPath,
355
+ contentBase64: content.toString("base64"),
356
+ sha256: sha256(content),
357
+ });
358
+ }
359
+ }
360
+ for (const directory of directories)
361
+ await recheck(directory, signal);
362
+ if (options.sessionDir === undefined &&
363
+ (await agentDirectoryConfiguredSessionDir(signal, root, options)) !== sessionDir)
364
+ changedScan();
365
+ const currentSessionDirectory = await identifySessionRoot();
366
+ if (Boolean(sessionDirectory) !== Boolean(currentSessionDirectory) ||
367
+ (sessionDirectory &&
368
+ currentSessionDirectory &&
369
+ (sessionDirectory.stat.dev !== currentSessionDirectory.stat.dev ||
370
+ sessionDirectory.stat.ino !== currentSessionDirectory.stat.ino)))
371
+ changedScan();
372
+ for (const candidate of candidates)
373
+ await recheck(candidate, signal);
374
+ signal?.throwIfAborted();
375
+ return {
376
+ files: results.sort((left, right) => left.path.localeCompare(right.path)),
377
+ inventory: {
378
+ root: absoluteRoot,
379
+ sessionRoot,
380
+ scanSessions: selectedSessions,
381
+ entries,
382
+ fileSizes: new Map(candidates.map(({ absolute, stat }) => [absolute, stat.size])),
383
+ walkedDirectories: new Set(directories.map(({ absolute }) => absolute)),
384
+ routingDirectories,
385
+ },
386
+ };
387
+ }
388
+ async function collectIncludedPath(results, root, relativePath, options) {
389
+ const { signal } = options;
390
+ signal?.throwIfAborted();
391
+ const absolutePath = safeJoin(root, relativePath);
392
+ try {
393
+ const stat = await fs.lstat(absolutePath);
394
+ signal?.throwIfAborted();
395
+ if (stat.isFile())
396
+ await addFile(results, root, relativePath, relativePath, options);
397
+ else if (stat.isDirectory())
398
+ await collectDirectory(results, root, relativePath, options);
399
+ }
400
+ catch (error) {
401
+ if (error.code !== "ENOENT")
402
+ throw error;
403
+ if (!relativePath.includes("/")) {
404
+ const entries = await fs.readdir(root, { withFileTypes: true });
405
+ signal?.throwIfAborted();
406
+ const entry = selectTopLevelFileEntry(entries, relativePath);
407
+ if (entry)
408
+ await addFile(results, root, entry.name, relativePath, options);
409
+ }
410
+ }
411
+ }
412
+ async function collectDirectory(results, root, relativeDirectory, options) {
413
+ options.signal?.throwIfAborted();
414
+ const absoluteDirectory = path.join(root, relativeDirectory);
415
+ if (options.guardReserved)
416
+ await options.guardReserved(absoluteDirectory);
417
+ options.signal?.throwIfAborted();
418
+ for (const entry of await fs.readdir(absoluteDirectory, { withFileTypes: true })) {
419
+ options.signal?.throwIfAborted();
420
+ const relativePath = relativeDirectory ? posixJoin(relativeDirectory, entry.name) : entry.name;
421
+ const snapshotPath = options.virtualPrefix ? posixJoin(options.virtualPrefix, relativePath) : relativePath;
422
+ if (isDeniedPath(snapshotPath) || options.excluded(snapshotPath))
423
+ continue;
424
+ if (entry.isDirectory()) {
425
+ await collectDirectory(results, root, relativePath, options);
426
+ }
427
+ else if (entry.isFile() && (!options.sessionsOnly || isSessionFilePath(snapshotPath))) {
428
+ await addFile(results, root, relativePath, snapshotPath, options);
429
+ }
430
+ }
431
+ }
432
+ export async function readLegacySnapshotSource(absolutePath, guardReserved, signal) {
433
+ if (!guardReserved)
434
+ return fs.readFile(absolutePath, { signal });
435
+ signal?.throwIfAborted();
436
+ const canonical = await guardReserved(absolutePath);
437
+ const candidate = await identify(absolutePath, canonical, signal);
438
+ if (!candidate.stat.isFile() || candidate.stat.nlink !== 1)
439
+ changedScan();
440
+ return readScannedFile(candidate, signal);
441
+ }
442
+ async function addFile(results, root, relativePath, snapshotPath, { signal, guardReserved }) {
443
+ signal?.throwIfAborted();
444
+ if (!isSafeSnapshotPath(snapshotPath))
445
+ return;
446
+ const absolutePath = safeJoin(root, relativePath);
447
+ const content = await readLegacySnapshotSource(absolutePath, guardReserved, signal);
448
+ signal?.throwIfAborted();
449
+ results.push({
450
+ path: snapshotPath,
451
+ contentBase64: content.toString("base64"),
452
+ sha256: sha256(content),
453
+ });
454
+ }
455
+ export function isSessionPath(relativePath) {
456
+ return toPosix(relativePath).startsWith("sessions/");
457
+ }
458
+ export function isSessionFilePath(relativePath) {
459
+ const normalized = toPosix(relativePath);
460
+ return isSessionPath(normalized) && normalized.endsWith(".jsonl");
461
+ }
462
+ export function sessionSnapshotPathFromAbsolute(sessionFile, configuredSessionDir) {
463
+ const relativePath = toPosix(path.relative(sessionStorageRoot(agentDir(), configuredSessionDir), sessionFile));
464
+ if (!relativePath || relativePath.startsWith("../") || path.posix.isAbsolute(relativePath)) {
465
+ return undefined;
466
+ }
467
+ const snapshotPath = posixJoin("sessions", relativePath);
468
+ return isSessionFilePath(snapshotPath) ? snapshotPath : undefined;
469
+ }
470
+ export function snapshotTarget(root, relativePath, configuredSessionDir) {
471
+ if (isSessionPath(relativePath)) {
472
+ return safeJoin(sessionStorageRoot(root, configuredSessionDir), relativePath.slice("sessions/".length));
473
+ }
474
+ return safeJoin(root, relativePath);
475
+ }
476
+ export function snapshotIncludesSessions(snapshot) {
477
+ if (snapshot.selection?.version === 3)
478
+ return syncSessionsSelected(snapshot.selection);
479
+ if (snapshot.selection?.version === 4)
480
+ return snapshot.files.some((file) => isSessionPath(file.path));
481
+ return (snapshot.syncSessions === true ||
482
+ (snapshotSelectionInclude(snapshot)?.includes("sessions") === true &&
483
+ !compileSyncExclude(snapshotSelectionExclude(snapshot))("sessions")) ||
484
+ snapshot.files.some((file) => isSessionPath(file.path)));
485
+ }
486
+ export function filterSnapshotForConfigPolicy(snapshot, config, options = {}) {
487
+ const include = includeFromSelectionConfig(config);
488
+ const wholeDirectory = config.scope === "agent-directory";
489
+ if (!wholeDirectory || !config.ignorePolicy)
490
+ requireLegacyTransferPolicy(snapshot);
491
+ const exclude = config.exclude ?? (snapshot.version >= 4 ? [] : undefined);
492
+ const selection = selectionForSnapshot(include, exclude, config);
493
+ const files = wholeDirectory
494
+ ? filterAgentDirectoryFiles(snapshot.files, config, options.sessionDir)
495
+ : canonicalizeSnapshotFilesForConfig(snapshot.files, config, customIncludePathsByLower(include));
496
+ const filtered = {
497
+ ...snapshot,
498
+ version: selection.version === 4
499
+ ? IGNORE_SNAPSHOT_VERSION
500
+ : wholeDirectory
501
+ ? 5
502
+ : exclude === undefined
503
+ ? snapshot.version
504
+ : 4,
505
+ syncSessions: selection.version === 4
506
+ ? files.some((file) => isSessionPath(file.path))
507
+ : syncSessionsSelected(config)
508
+ ? snapshot.syncSessions
509
+ : false,
510
+ selection,
511
+ files,
512
+ };
513
+ if (!options.regenerateId || snapshotsMatch(snapshot, filtered))
514
+ return filtered;
515
+ return {
516
+ ...filtered,
517
+ id: snapshotId(),
518
+ createdAt: new Date().toISOString(),
519
+ machine: os.hostname(),
520
+ };
521
+ }
522
+ function filterAgentDirectoryFiles(files, config, sessionDir) {
523
+ const configured = configuredSnapshotPathMatcher(config);
524
+ const isPhysicalSessionPath = sessionDir
525
+ ? agentDirectoryPhysicalSessionPath(agentDir(), sessionDir, config.ignorePolicy, config.reservedIgnoreNames)
526
+ : undefined;
527
+ const projected = files.filter((file) => {
528
+ if (!isSafeSnapshotPath(file.path) || !configured(file.path))
529
+ return false;
530
+ if (/^sessions(?:\/|$)/iu.test(file.path))
531
+ return isSessionFilePath(file.path);
532
+ return !isPhysicalSessionPath?.(file.path);
533
+ });
534
+ assertAgentDirectoryPathLayout(projected.map((file) => file.path));
535
+ return projected.sort((left, right) => left.path.localeCompare(right.path));
536
+ }
537
+ function canonicalizeSnapshotFilesForConfig(files, config, includePaths) {
538
+ const configuredFiles = [];
539
+ const extraCandidates = new Map();
540
+ const configured = configuredSnapshotPathMatcher(config);
541
+ for (const file of files) {
542
+ const normalized = toPosix(file.path);
543
+ if (!isSafeSnapshotPath(file.path) || !configured(normalized)) {
544
+ continue;
545
+ }
546
+ if (normalized.includes("/")) {
547
+ configuredFiles.push(normalized === file.path ? file : { ...file, path: normalized });
548
+ continue;
549
+ }
550
+ const topLevelPath = canonicalSnapshotPathForConfig(normalized, includePaths);
551
+ const candidate = {
552
+ exact: normalized === topLevelPath,
553
+ file: { ...file, path: topLevelPath },
554
+ originalPath: normalized,
555
+ };
556
+ const current = extraCandidates.get(topLevelPath.toLowerCase());
557
+ if (!current || isPreferredExtraCandidate(candidate, current)) {
558
+ extraCandidates.set(topLevelPath.toLowerCase(), candidate);
559
+ }
560
+ }
561
+ return [...configuredFiles, ...[...extraCandidates.values()].map((candidate) => candidate.file)].sort((left, right) => left.path.localeCompare(right.path));
562
+ }
563
+ function isPreferredExtraCandidate(left, right) {
564
+ if (left.exact !== right.exact)
565
+ return left.exact;
566
+ return left.originalPath.localeCompare(right.originalPath) < 0;
567
+ }
568
+ export function snapshotWithoutSessions(snapshot) {
569
+ requireLegacyTransferPolicy(snapshot);
570
+ const files = snapshot.files.filter((file) => !isSessionPath(file.path));
571
+ const include = snapshotSelectionInclude(snapshot)?.filter((item) => item !== "sessions");
572
+ if (files.length === snapshot.files.length &&
573
+ snapshot.syncSessions !== true &&
574
+ include?.length === snapshot.selection?.include?.length) {
575
+ return snapshot;
576
+ }
577
+ return {
578
+ ...snapshot,
579
+ id: snapshotId(),
580
+ createdAt: new Date().toISOString(),
581
+ machine: os.hostname(),
582
+ syncSessions: false,
583
+ ...(include
584
+ ? {
585
+ selection: selectionForSnapshot(include, snapshotSelectionExclude(snapshot), snapshot.selection?.version === 3 ? snapshot.selection : undefined),
586
+ }
587
+ : {}),
588
+ files,
589
+ };
590
+ }
591
+ export function scanSnapshot(snapshot) {
592
+ const findings = [];
593
+ for (const file of snapshot.files) {
594
+ const content = Buffer.from(file.contentBase64, "base64");
595
+ if (content.includes(0))
596
+ continue;
597
+ const text = content.toString("utf8");
598
+ for (const pattern of SECRET_PATTERNS) {
599
+ if (pattern.test(text)) {
600
+ findings.push(file.path);
601
+ break;
602
+ }
603
+ }
604
+ }
605
+ return findings;
606
+ }
607
+ export function mergeRemotePreservedFiles(local, remote, config, options = {}) {
608
+ if (config.scope !== "agent-directory" || !config.ignorePolicy) {
609
+ requireLegacyTransferPolicy(local);
610
+ requireLegacyTransferPolicy(remote);
611
+ }
612
+ if (config.scope === "agent-directory")
613
+ return filterSnapshotForConfigPolicy(local, config, { ...options, regenerateId: true });
614
+ if (config.reservedIgnoreNames !== undefined) {
615
+ const names = new Set(config.reservedIgnoreNames);
616
+ const files = local.files.filter((file) => !isIgnoreControlPath(toPosix(file.path), names));
617
+ if (files.length !== local.files.length)
618
+ local = regenerateSnapshotIdentity({ ...local, files });
619
+ }
620
+ const localPathNames = new Set(local.files.map((file) => file.path.toLowerCase()));
621
+ const preservedPathNames = new Set();
622
+ const configured = configuredSnapshotPathMatcher(config);
623
+ const excluded = compileSyncPolicyExclude(config);
624
+ const preserved = remote.files.filter((file) => {
625
+ const normalized = toPosix(file.path);
626
+ const lower = normalized.toLowerCase();
627
+ if (localPathNames.has(lower) ||
628
+ preservedPathNames.has(lower) ||
629
+ !isSafeSnapshotPath(file.path) ||
630
+ configured(normalized) ||
631
+ excluded(normalized) ||
632
+ !isPreservableUnmanagedSnapshotPath(normalized)) {
633
+ return false;
634
+ }
635
+ preservedPathNames.add(lower);
636
+ return true;
637
+ });
638
+ if (preserved.length === 0)
639
+ return local;
640
+ return {
641
+ ...local,
642
+ id: snapshotId(),
643
+ createdAt: new Date().toISOString(),
644
+ machine: os.hostname(),
645
+ syncSessions: !excluded("sessions") && (snapshotIncludesSessions(local) || snapshotIncludesSessions(remote)),
646
+ files: [...local.files, ...preserved].sort((left, right) => left.path.localeCompare(right.path)),
647
+ };
648
+ }
649
+ export function mergeRemoteSessionFiles(local, remote) {
650
+ requireLegacyTransferPolicy(local);
651
+ requireLegacyTransferPolicy(remote);
652
+ const directory = local.selection?.version === 3 ? local.selection : undefined;
653
+ const excluded = directory
654
+ ? compileSyncPolicyExclude(directory)
655
+ : compileSyncExclude(snapshotSelectionExclude(local));
656
+ if (directory ? !syncSessionsSelected(directory) : excluded("sessions"))
657
+ return local;
658
+ const remoteSessions = remote.files.filter((file) => {
659
+ const normalized = toPosix(file.path);
660
+ return isSessionFilePath(normalized) && isSafeSnapshotPath(file.path) && !excluded(normalized);
661
+ });
662
+ if (remoteSessions.length === 0 && !snapshotIncludesSessions(remote))
663
+ return local;
664
+ const localInclude = snapshotSelectionInclude(local);
665
+ return {
666
+ ...local,
667
+ id: snapshotId(),
668
+ createdAt: new Date().toISOString(),
669
+ machine: os.hostname(),
670
+ syncSessions: true,
671
+ ...(localInclude
672
+ ? {
673
+ selection: selectionForSnapshot(localInclude.includes("sessions") ? localInclude : [...localInclude, "sessions"], snapshotSelectionExclude(local), directory),
674
+ }
675
+ : {}),
676
+ files: [...local.files.filter((file) => !isSessionPath(file.path)), ...remoteSessions].sort((left, right) => left.path.localeCompare(right.path)),
677
+ };
678
+ }