dsh-config-manager 0.8.2 → 0.10.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 (418) hide show
  1. package/lib/adapters/mcp.js +51 -15
  2. package/lib/adapters/messages.d.ts +1 -0
  3. package/lib/adapters/messages.js +2 -0
  4. package/lib/adapters/patch-key-guard.d.ts +23 -0
  5. package/lib/adapters/patch-key-guard.js +13 -0
  6. package/lib/adapters/plugin-data.d.ts +19 -5
  7. package/lib/adapters/plugin-data.js +76 -22
  8. package/lib/adapters/plugins.d.ts +14 -1
  9. package/lib/adapters/plugins.js +204 -6
  10. package/lib/adapters/prompts.js +15 -4
  11. package/lib/adapters/test-helpers.d.ts +15 -1
  12. package/lib/adapters/test-helpers.js +11 -0
  13. package/lib/cli/actions.d.ts +7 -105
  14. package/lib/cli/actions.js +16 -172
  15. package/lib/cli/control-state.d.ts +54 -0
  16. package/lib/cli/control-state.js +107 -0
  17. package/lib/cli/help.d.ts +1 -1
  18. package/lib/cli/help.js +21 -14
  19. package/lib/cli/index.js +132 -104
  20. package/lib/cli/sessions-repair.js +58 -22
  21. package/lib/cli/web/client-script.d.ts +1 -1
  22. package/lib/cli/web/client-script.js +71 -5
  23. package/lib/cli/web/page.d.ts +9 -19
  24. package/lib/cli/web/page.js +277 -173
  25. package/lib/cli/web/routes.js +85 -25
  26. package/lib/cli/write-gates.d.ts +61 -0
  27. package/lib/cli/write-gates.js +110 -0
  28. package/lib/client.d.ts +196 -75
  29. package/lib/client.js +8139 -7487
  30. package/lib/core/backup.d.ts +0 -4
  31. package/lib/core/backup.js +21 -4
  32. package/lib/core/boot-rescue.d.ts +1 -1
  33. package/lib/core/boot-rescue.js +12 -1
  34. package/lib/core/boot-safety.js +37 -2
  35. package/lib/core/conflict-decisions.js +2 -1
  36. package/lib/core/exporter.js +22 -1
  37. package/lib/core/journal.d.ts +54 -4
  38. package/lib/core/journal.js +158 -35
  39. package/lib/core/local-plugin-host.d.ts +29 -0
  40. package/lib/core/local-plugin-host.js +55 -48
  41. package/lib/core/messages.d.ts +17 -0
  42. package/lib/core/messages.js +34 -0
  43. package/lib/core/migration-history.js +16 -9
  44. package/lib/core/model-tools.d.ts +2 -2
  45. package/lib/core/model-tools.js +20 -2
  46. package/lib/core/patch-chain.d.ts +65 -0
  47. package/lib/core/patch-chain.js +100 -0
  48. package/lib/core/patch-document.d.ts +32 -4
  49. package/lib/core/patch-document.js +48 -17
  50. package/lib/core/patch-secret-guard.d.ts +49 -0
  51. package/lib/core/patch-secret-guard.js +164 -0
  52. package/lib/core/reconcile.d.ts +8 -0
  53. package/lib/core/reconcile.js +18 -3
  54. package/lib/core/reinstall.d.ts +2 -0
  55. package/lib/core/reinstall.js +6 -2
  56. package/lib/core/restore.js +31 -6
  57. package/lib/core/section-sanitize.d.ts +32 -0
  58. package/lib/core/section-sanitize.js +13 -0
  59. package/lib/core/snapshot-diff.d.ts +30 -0
  60. package/lib/core/snapshot-diff.js +56 -20
  61. package/lib/core/startup-barrier.js +8 -2
  62. package/lib/core/types.d.ts +38 -1
  63. package/lib/core/update-check.js +51 -2
  64. package/lib/foreign/kernel.js +9 -2
  65. package/lib/foreign/read-claude-plugins.js +29 -11
  66. package/lib/foreign/read-dsh.js +31 -2
  67. package/lib/foreign/session-ir.js +50 -9
  68. package/lib/foreign/session-read.d.ts +8 -0
  69. package/lib/foreign/session-read.js +26 -0
  70. package/lib/index.d.ts +9 -4
  71. package/lib/index.js +42 -47
  72. package/lib/profiles/dsh-profile-io.d.ts +20 -0
  73. package/lib/profiles/dsh-profile-io.js +27 -0
  74. package/lib/profiles/dsh-profile-manager.d.ts +27 -0
  75. package/lib/profiles/dsh-profile-manager.js +45 -4
  76. package/lib/profiles/dsh-profile-runtime.d.ts +5 -1
  77. package/lib/profiles/dsh-profile-runtime.js +16 -3
  78. package/lib/profiles/process-control.d.ts +18 -1
  79. package/lib/profiles/process-control.js +25 -1
  80. package/lib/routes/profiles.js +2 -1
  81. package/lib/routes/sync.js +19 -1
  82. package/lib/schema/types.d.ts +11 -0
  83. package/lib/security/redaction.js +22 -6
  84. package/lib/sync/autosync-repeat.d.ts +27 -0
  85. package/lib/sync/autosync-repeat.js +23 -0
  86. package/lib/sync/autosync-scheduler.d.ts +47 -2
  87. package/lib/sync/autosync-scheduler.js +217 -53
  88. package/lib/sync/baseline-heal.js +7 -0
  89. package/lib/sync/baseline-record.d.ts +67 -0
  90. package/lib/sync/baseline-record.js +74 -0
  91. package/lib/sync/blob-store.js +13 -0
  92. package/lib/sync/content-diff.d.ts +39 -0
  93. package/lib/sync/content-diff.js +46 -0
  94. package/lib/sync/git/git-transport.d.ts +32 -1
  95. package/lib/sync/git/git-transport.js +146 -26
  96. package/lib/sync/github-auth.d.ts +17 -1
  97. package/lib/sync/github-auth.js +37 -1
  98. package/lib/sync/local-changes.d.ts +15 -0
  99. package/lib/sync/local-changes.js +39 -0
  100. package/lib/sync/merge-apply-plan.d.ts +38 -0
  101. package/lib/sync/merge-apply-plan.js +76 -0
  102. package/lib/sync/merge-keyed.d.ts +68 -0
  103. package/lib/sync/merge-keyed.js +124 -0
  104. package/lib/sync/messages.d.ts +1 -0
  105. package/lib/sync/messages.js +2 -0
  106. package/lib/sync/patch-line-identity.d.ts +19 -0
  107. package/lib/sync/patch-line-identity.js +99 -0
  108. package/lib/sync/pending-merge.d.ts +41 -0
  109. package/lib/sync/pending-merge.js +61 -0
  110. package/lib/sync/plugin-removals.d.ts +15 -15
  111. package/lib/sync/plugin-removals.js +134 -83
  112. package/lib/sync/push-skip.d.ts +55 -0
  113. package/lib/sync/push-skip.js +70 -0
  114. package/lib/sync/s3/s3-transport.d.ts +14 -2
  115. package/lib/sync/s3/s3-transport.js +72 -48
  116. package/lib/sync/sync-engine.d.ts +44 -51
  117. package/lib/sync/sync-engine.js +160 -194
  118. package/lib/sync/sync-history.d.ts +7 -0
  119. package/lib/sync/sync-state.d.ts +28 -0
  120. package/lib/sync/sync-state.js +20 -0
  121. package/lib/sync/transport.d.ts +12 -3
  122. package/lib/sync/transport.js +13 -4
  123. package/lib/sync/webdav/webdav-transport.d.ts +19 -2
  124. package/lib/sync/webdav/webdav-transport.js +81 -40
  125. package/lib/sync/workspace-merge.d.ts +8 -2
  126. package/lib/ui/artifact-view.d.ts +1 -1
  127. package/lib/ui/commands.js +5 -11
  128. package/lib/ui/dsh-profiles-view.d.ts +8 -3
  129. package/lib/ui/dsh-profiles-view.js +9 -5
  130. package/lib/ui/foreign-view.d.ts +14 -0
  131. package/lib/ui/foreign-view.js +34 -0
  132. package/lib/ui/i18n.d.ts +7 -1
  133. package/lib/ui/i18n.js +15 -2
  134. package/lib/ui/menu-placement.js +1 -1
  135. package/lib/ui/migration-consult-view.d.ts +11 -0
  136. package/lib/ui/migration-consult-view.js +15 -0
  137. package/lib/ui/overview-view.d.ts +7 -0
  138. package/lib/ui/overview-view.js +3 -0
  139. package/lib/ui/report.d.ts +9 -0
  140. package/lib/ui/report.js +17 -4
  141. package/lib/ui/selection-model.d.ts +10 -2
  142. package/lib/ui/selection-model.js +23 -3
  143. package/lib/utils/async-once.d.ts +11 -0
  144. package/lib/utils/async-once.js +15 -0
  145. package/lib/utils/patch-chain-read.d.ts +51 -0
  146. package/lib/utils/patch-chain-read.js +240 -0
  147. package/lib/utils/patch-yaml.d.ts +39 -1
  148. package/lib/utils/patch-yaml.js +82 -2
  149. package/lib/utils/paths.d.ts +13 -0
  150. package/lib/utils/paths.js +35 -0
  151. package/lib/utils/workdir-lock.js +18 -3
  152. package/package.json +3 -2
  153. package/src/adapters/mcp.test.ts +29 -0
  154. package/src/adapters/mcp.ts +52 -14
  155. package/src/adapters/messages.ts +2 -0
  156. package/src/adapters/patch-key-guard.ts +29 -0
  157. package/src/adapters/plugin-data.test.ts +126 -0
  158. package/src/adapters/plugin-data.ts +97 -26
  159. package/src/adapters/plugins.test.ts +403 -0
  160. package/src/adapters/plugins.ts +198 -8
  161. package/src/adapters/prompts.ts +15 -4
  162. package/src/adapters/test-helpers.ts +15 -1
  163. package/src/cli/actions.test.ts +74 -1
  164. package/src/cli/actions.ts +27 -246
  165. package/src/cli/control-state.ts +143 -0
  166. package/src/cli/help.ts +21 -14
  167. package/src/cli/index.ts +123 -107
  168. package/src/cli/sessions-repair.ts +55 -22
  169. package/src/cli/web/client-script.ts +71 -5
  170. package/src/cli/web/page.ts +327 -208
  171. package/src/cli/web/routes.ts +85 -25
  172. package/src/cli/web/web.test.ts +65 -0
  173. package/src/cli/write-gates.ts +149 -0
  174. package/src/client/ConfigManagerSection.tsx +8 -4
  175. package/src/client/about/AboutPanel.tsx +9 -12
  176. package/src/client/about/release-notes-view.test.ts +17 -0
  177. package/src/client/about/release-notes-view.ts +49 -24
  178. package/src/client/api.ts +2 -1
  179. package/src/client/chat/ChatEntryView.tsx +2 -2
  180. package/src/client/chat/rollback-entry-view.ts +6 -1
  181. package/src/client/common/CommandPalette.tsx +6 -3
  182. package/src/client/common/ConfirmDialog.tsx +29 -12
  183. package/src/client/common/ContentPicker.tsx +24 -24
  184. package/src/client/common/Modal.tsx +12 -5
  185. package/src/client/common/MoreMenu.tsx +4 -1
  186. package/src/client/common/RunsCenter.tsx +103 -32
  187. package/src/client/common/Select.tsx +2 -2
  188. package/src/client/common/floating-bounds.ts +1 -1
  189. package/src/client/common/floating-units.test.ts +1 -2
  190. package/src/client/common/plan-text-redaction.test.ts +190 -5
  191. package/src/client/common/runs-center-guard.test.ts +60 -0
  192. package/src/client/common/runs-center-view.test.ts +128 -0
  193. package/src/client/common/runs-center-view.ts +96 -0
  194. package/src/client/common/section-title-action.test.ts +66 -0
  195. package/src/client/common/ui.tsx +93 -30
  196. package/src/client/config-manager.module.css +1515 -270
  197. package/src/client/consult/ConsultCard.tsx +7 -6
  198. package/src/client/environment/EnvironmentPanel.tsx +22 -25
  199. package/src/client/environment/maintenance/DiskUsageCard.tsx +69 -96
  200. package/src/client/environment/profiles-incomplete-ui.test.ts +8 -8
  201. package/src/client/export/ExportView.tsx +7 -16
  202. package/src/client/history/HistoryPanel.tsx +88 -28
  203. package/src/client/history/history-locales.ts +3 -3
  204. package/src/client/home/BackupScheduleCard.tsx +77 -69
  205. package/src/client/home/HomePanel.tsx +47 -12
  206. package/src/client/import/ConflictList.tsx +13 -13
  207. package/src/client/import/ForeignImportView.tsx +67 -9
  208. package/src/client/import/ImportWizardView.tsx +4 -4
  209. package/src/client/import/PathMappingForm.tsx +36 -9
  210. package/src/client/import/PathPickerDialog.tsx +2 -1
  211. package/src/client/import/import-wizard-split.test.ts +1 -1
  212. package/src/client/import/import-wizard-steps.tsx +51 -39
  213. package/src/client/import/use-import-wizard-controller.test.ts +129 -0
  214. package/src/client/import/use-import-wizard-controller.ts +116 -22
  215. package/src/client/library/ArtifactRow.tsx +2 -2
  216. package/src/client/library/BackupInspectView.tsx +21 -15
  217. package/src/client/library/LibraryActions.tsx +116 -28
  218. package/src/client/library/LibraryPanel.tsx +14 -15
  219. package/src/client/library/RestorePlanView.tsx +13 -8
  220. package/src/client/locales/about.ts +0 -2
  221. package/src/client/locales/backup.ts +15 -12
  222. package/src/client/locales/common.ts +2 -0
  223. package/src/client/locales/export.ts +5 -12
  224. package/src/client/locales/foreign.ts +4 -2
  225. package/src/client/locales/import.ts +5 -2
  226. package/src/client/locales/overview.ts +4 -0
  227. package/src/client/locales/profiles.ts +32 -4
  228. package/src/client/locales/restore.ts +2 -2
  229. package/src/client/locales/sessions.ts +0 -2
  230. package/src/client/locales/shell.ts +2 -2
  231. package/src/client/market/MarketImportReview.tsx +4 -4
  232. package/src/client/market/MarketPanel.tsx +58 -56
  233. package/src/client/market/MyConfigsList.tsx +34 -32
  234. package/src/client/market/MyConfigsLoginCard.tsx +12 -15
  235. package/src/client/market/MyConfigsView.tsx +8 -15
  236. package/src/client/market/MyConfigsWizard.tsx +7 -12
  237. package/src/client/market/market-locales.ts +0 -6
  238. package/src/client/market/my-configs-split.test.ts +2 -1
  239. package/src/client/nav-menu-clip-guard.test.ts +52 -0
  240. package/src/client/recovery/IncompleteCopiesSection.tsx +1 -3
  241. package/src/client/recovery/RecoveryPanel.tsx +35 -22
  242. package/src/client/recovery/RollbackCard.tsx +132 -75
  243. package/src/client/recovery/RollbackFileChanges.tsx +40 -27
  244. package/src/client/recovery/SessionHealthCard.tsx +2 -2
  245. package/src/client/recovery/SessionHealthDialog.tsx +5 -7
  246. package/src/client/recovery/SessionLayoutSection.tsx +3 -5
  247. package/src/client/recovery/recovery-locales.ts +12 -8
  248. package/src/client/recovery/rollback-enabled.ts +29 -0
  249. package/src/client/recovery/use-recovery-write.ts +5 -0
  250. package/src/client/run-store.test.ts +30 -0
  251. package/src/client/run-store.ts +21 -1
  252. package/src/client/sync/AddChannelCard.tsx +65 -0
  253. package/src/client/sync/AutosyncCard.tsx +184 -108
  254. package/src/client/sync/ChannelClearConfirmDialog.tsx +5 -1
  255. package/src/client/sync/ChannelConfigDialog.tsx +27 -48
  256. package/src/client/sync/MergePreviewView.tsx +5 -7
  257. package/src/client/sync/PushPreviewCard.tsx +2 -1
  258. package/src/client/sync/PushReportSections.tsx +35 -0
  259. package/src/client/sync/SyncChannelEntryCard.tsx +33 -24
  260. package/src/client/sync/SyncConfirmView.tsx +130 -133
  261. package/src/client/sync/SyncLogList.tsx +29 -14
  262. package/src/client/sync/SyncPanel.tsx +188 -185
  263. package/src/client/sync/SyncProbeRow.tsx +5 -7
  264. package/src/client/sync/SyncRepositoryPicker.tsx +17 -4
  265. package/src/client/sync/SyncSecuritySection.tsx +157 -0
  266. package/src/client/sync/SyncSettingsDialog.tsx +293 -0
  267. package/src/client/sync/SyncSettingsView.test.ts +26 -8
  268. package/src/client/sync/WorkDirCard.tsx +2 -4
  269. package/src/client/sync/config-clear-guard.test.ts +94 -0
  270. package/src/client/sync/config-clear-guard.ts +98 -0
  271. package/src/client/sync/config-dirty.test.ts +91 -0
  272. package/src/client/sync/config-dirty.ts +88 -0
  273. package/src/client/sync/history-model.test.ts +2 -0
  274. package/src/client/sync/history-model.ts +5 -0
  275. package/src/client/sync/merge-request.ts +20 -0
  276. package/src/client/sync/sync-api.ts +13 -1
  277. package/src/client/sync/sync-locales.ts +95 -47
  278. package/src/client/sync/sync-panel-clear.test.ts +44 -0
  279. package/src/client/sync/sync-push-preview.test.ts +9 -0
  280. package/src/client/sync/sync-view.test.ts +20 -0
  281. package/src/client/sync/sync-view.ts +76 -4
  282. package/src/core/backup.test.ts +61 -1
  283. package/src/core/backup.ts +21 -4
  284. package/src/core/boot-rescue.test.ts +35 -0
  285. package/src/core/boot-rescue.ts +13 -2
  286. package/src/core/boot-safety.test.ts +25 -0
  287. package/src/core/boot-safety.ts +37 -2
  288. package/src/core/conflict-decisions.ts +2 -1
  289. package/src/core/exporter.ts +21 -1
  290. package/src/core/journal.test.ts +183 -0
  291. package/src/core/journal.ts +163 -25
  292. package/src/core/local-plugin-host.test.ts +68 -2
  293. package/src/core/local-plugin-host.ts +31 -4
  294. package/src/core/messages.ts +34 -0
  295. package/src/core/migration-history.test.ts +27 -0
  296. package/src/core/migration-history.ts +12 -4
  297. package/src/core/model-tools.test.ts +67 -0
  298. package/src/core/model-tools.ts +22 -4
  299. package/src/core/patch-chain.test.ts +83 -0
  300. package/src/core/patch-chain.ts +150 -0
  301. package/src/core/patch-document.test.ts +48 -1
  302. package/src/core/patch-document.ts +66 -15
  303. package/src/core/patch-layers.test.ts +1 -1
  304. package/src/core/patch-secret-guard.test.ts +110 -0
  305. package/src/core/patch-secret-guard.ts +176 -0
  306. package/src/core/reconcile.ts +32 -4
  307. package/src/core/reinstall.ts +8 -2
  308. package/src/core/restore.ts +32 -7
  309. package/src/core/section-sanitize.ts +37 -0
  310. package/src/core/smoke.test.ts +2 -1
  311. package/src/core/snapshot-diff.test.ts +73 -1
  312. package/src/core/snapshot-diff.ts +66 -21
  313. package/src/core/startup-barrier.test.ts +64 -0
  314. package/src/core/startup-barrier.ts +8 -2
  315. package/src/core/types.ts +43 -2
  316. package/src/core/update-check.test.ts +40 -0
  317. package/src/core/update-check.ts +53 -2
  318. package/src/foreign/claude-code.test.ts +5 -0
  319. package/src/foreign/claude-plugins.test.ts +57 -0
  320. package/src/foreign/claude-sessions-bytes.test.ts +7 -1
  321. package/src/foreign/dsh.test.ts +23 -0
  322. package/src/foreign/kernel.ts +9 -2
  323. package/src/foreign/read-claude-plugins.ts +32 -11
  324. package/src/foreign/read-dsh.ts +33 -2
  325. package/src/foreign/session-ir.test.ts +67 -0
  326. package/src/foreign/session-ir.ts +54 -14
  327. package/src/foreign/session-read.ts +23 -0
  328. package/src/index.facade.test.ts +186 -6
  329. package/src/index.ts +53 -47
  330. package/src/profiles/dsh-profile-copy-rollback.test.ts +58 -4
  331. package/src/profiles/dsh-profile-io.ts +29 -0
  332. package/src/profiles/dsh-profile-manager.test.ts +30 -0
  333. package/src/profiles/dsh-profile-manager.ts +53 -5
  334. package/src/profiles/dsh-profile-runtime.test.ts +29 -7
  335. package/src/profiles/dsh-profile-runtime.ts +17 -3
  336. package/src/profiles/process-control.test.ts +97 -0
  337. package/src/profiles/process-control.ts +28 -1
  338. package/src/routes/profiles.ts +2 -1
  339. package/src/routes/sync.ts +19 -1
  340. package/src/schema/types.ts +16 -1
  341. package/src/security/redaction.test.ts +31 -0
  342. package/src/security/redaction.ts +23 -6
  343. package/src/security/security.test.ts +1 -1
  344. package/src/sync/autosync-repeat.test.ts +58 -0
  345. package/src/sync/autosync-repeat.ts +55 -0
  346. package/src/sync/autosync-scheduler.test.ts +515 -0
  347. package/src/sync/autosync-scheduler.ts +225 -51
  348. package/src/sync/baseline-heal.ts +6 -0
  349. package/src/sync/baseline-record.ts +135 -0
  350. package/src/sync/blob-store.test.ts +16 -0
  351. package/src/sync/blob-store.ts +13 -0
  352. package/src/sync/content-diff.test.ts +95 -0
  353. package/src/sync/content-diff.ts +79 -0
  354. package/src/sync/git/git-transport.test.ts +151 -4
  355. package/src/sync/git/git-transport.ts +143 -27
  356. package/src/sync/github-auth.test.ts +26 -1
  357. package/src/sync/github-auth.ts +37 -1
  358. package/src/sync/local-changes.ts +40 -0
  359. package/src/sync/merge-apply-plan.ts +117 -0
  360. package/src/sync/merge-keyed.test.ts +167 -0
  361. package/src/sync/merge-keyed.ts +160 -0
  362. package/src/sync/messages.ts +3 -0
  363. package/src/sync/patch-line-identity.test.ts +73 -0
  364. package/src/sync/patch-line-identity.ts +105 -0
  365. package/src/sync/pending-merge.test.ts +41 -0
  366. package/src/sync/pending-merge.ts +85 -0
  367. package/src/sync/plugin-removals.test.ts +199 -39
  368. package/src/sync/plugin-removals.ts +150 -64
  369. package/src/sync/push-skip.test.ts +326 -0
  370. package/src/sync/push-skip.ts +102 -0
  371. package/src/sync/s3/s3-delete-and-blob-index.test.ts +249 -0
  372. package/src/sync/s3/s3-transport.ts +76 -42
  373. package/src/sync/sync-config.test.ts +39 -12
  374. package/src/sync/sync-device-identity.test.ts +3 -0
  375. package/src/sync/sync-engine.test.ts +465 -12
  376. package/src/sync/sync-engine.ts +189 -193
  377. package/src/sync/sync-history.ts +7 -0
  378. package/src/sync/sync-state.ts +48 -0
  379. package/src/sync/transport.test.ts +13 -1
  380. package/src/sync/transport.ts +12 -4
  381. package/src/sync/webdav/webdav-delete-and-blob-index.test.ts +249 -0
  382. package/src/sync/webdav/webdav-transport.ts +86 -34
  383. package/src/sync/workspace-merge.ts +9 -2
  384. package/src/ui/artifact-view.ts +1 -1
  385. package/src/ui/commands.test.ts +11 -0
  386. package/src/ui/commands.ts +5 -14
  387. package/src/ui/dsh-profiles-view.test.ts +16 -0
  388. package/src/ui/dsh-profiles-view.ts +11 -5
  389. package/src/ui/foreign-view.test.ts +49 -0
  390. package/src/ui/foreign-view.ts +47 -0
  391. package/src/ui/i18n.ts +15 -2
  392. package/src/ui/menu-placement.ts +1 -1
  393. package/src/ui/migration-consult-view.test.ts +20 -1
  394. package/src/ui/migration-consult-view.ts +17 -0
  395. package/src/ui/overview-view.ts +10 -0
  396. package/src/ui/report.test.ts +25 -1
  397. package/src/ui/report.ts +15 -4
  398. package/src/ui/selection-model.test.ts +29 -1
  399. package/src/ui/selection-model.ts +25 -4
  400. package/src/utils/async-once.ts +14 -0
  401. package/src/utils/js-yaml4.d.ts +35 -0
  402. package/src/utils/patch-chain-read.test.ts +171 -0
  403. package/src/utils/patch-chain-read.ts +259 -0
  404. package/src/utils/patch-yaml.test.ts +215 -3
  405. package/src/utils/patch-yaml.ts +98 -2
  406. package/src/utils/paths.test.ts +14 -1
  407. package/src/utils/paths.ts +35 -0
  408. package/src/utils/workdir-lock.test.ts +69 -0
  409. package/src/utils/workdir-lock.ts +17 -3
  410. package/lib/ui/path-mapping.d.ts +0 -38
  411. package/lib/ui/path-mapping.js +0 -75
  412. package/src/client/common/InfoHint.tsx +0 -289
  413. package/src/client/common/info-hint-guard.test.ts +0 -1119
  414. package/src/client/sync/DecryptPasswordCard.tsx +0 -48
  415. package/src/client/sync/SecurityOptionsCard.tsx +0 -97
  416. package/src/client/sync/SyncSectionPickerDialog.tsx +0 -213
  417. package/src/ui/path-mapping.test.ts +0 -67
  418. package/src/ui/path-mapping.ts +0 -103
@@ -189,6 +189,17 @@ const SNAPSHOTS_REL = 'snapshots';
189
189
  /** 加密快照的「密文单文件」目录:整个快照 JSON(含密文载荷)以 <id>.json 提交。
190
190
  * 加密快照不写散文件目录(密文无法平铺为明文 JSON 分区;远端已存密文)。 */
191
191
  const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
192
+ /**
193
+ * 明文最新快照的**只读派生视图**(issue #141):与 `snapshots/<id>/` 逐文件同布局,供人和 CI 直接读,
194
+ * 不必先知道快照 id。(契约:docs/contracts/sync-channel-v1.md)
195
+ *
196
+ * 四条硬约束,改这里之前先读:
197
+ * - **只写不读**:list()/download()/合并/保留策略/冲突判定一律不把它当快照目录,也不依赖它的存在;
198
+ * - **必须与快照进同一个 commit**:push 之后再单独提交一次就多出一次远端写(正是报告者担心的并发窗口);
199
+ * - 加密快照不写(密文没有明文镜像)—— 由引擎显式告警,绝不静默;
200
+ * - 保留策略剪旧不动它:它永远指向最新那份明文快照。
201
+ */
202
+ const LATEST_REL = 'latest';
192
203
  /**
193
204
  * 内容寻址 blob 仓在仓库内的相对目录(P1-4)。
194
205
  *
@@ -198,14 +209,20 @@ const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
198
209
  */
199
210
  const BLOBS_REL = 'blobs';
200
211
  /**
201
- * 同步仓库(git 通道)顶层**只可能出现**这三个条目。issue #104 的 `adopt` 模式拿它当安全判据:
212
+ * 同步仓库(git 通道)顶层**只可能出现**这四个条目。issue #104 的 `adopt` 模式拿它当安全判据:
202
213
  * 远端分支的顶层出现别的名字 = 这不是本插件的同步仓库,合并它等于把别人的仓库内容搬进工作副本。
214
+ * `latest/` 只是承认「这是本插件写的派生视图」(issue #141),读取路径一律不碰它。
215
+ *
216
+ * 老版本客户端不知道 `latest/` ⇒ 只有在换仓库/远端历史重写时的 adopt 会 fail-closed 拒收
217
+ * (docs/known-issues/known-limits.md 有记录)。
203
218
  */
204
- const ADOPT_ROOT_ALLOW = new Set<string>([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL]);
219
+ const ADOPT_ROOT_ALLOW = new Set<string>([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL, LATEST_REL]);
205
220
  /**
206
- * 同步仓库只在这三个顶层目录里写内容 ⇒ partial clone 的 sparse-checkout 面与它同源(issue #122)。
221
+ * 同步仓库只在这四个顶层目录里写内容 ⇒ partial clone 的 sparse-checkout 面与它同源(issue #122)。
207
222
  * 远端仓库比同步内容大几个数量级时(报告者现场:1.8 GB 仓库 / 6 MB 内容),全量 clone 与全量 fetch
208
- * 都要拉完整对象图,而工作副本真正需要的只有这三个目录。
223
+ * 都要拉完整对象图,而工作副本真正需要的只有这四个目录。
224
+ * `latest/` 虽是派生物,但它是同一 commit 里的产物:落在 cone 之外 `git add` 会直接拒收入库
225
+ * ("outside of your sparse-checkout definition"),所以必须进 sparse 面。
209
226
  */
210
227
  /* 具体的 sparse 面在 `syncDirs()` 里按子目录前缀派生(issue #131),基名与 ADOPT_ROOT_ALLOW 同源。 */
211
228
  /**
@@ -407,6 +424,8 @@ export class GitTransport implements SyncTransport {
407
424
  * 的唯一线索。列表接口把它如实回传,让状态自洽(绝不静默失败)。
408
425
  */
409
426
  private readErrors: { file: string; reason: string }[] = [];
427
+ /** 存量工作副本的 sparse cone 是否已确认包含 latest/(issue #141;只补一次,不每次同步都重设) */
428
+ private sparseConeCoversLatest = false;
410
429
  private readonly msg: MsgFunc;
411
430
  /** repoUrl 是否为网络地址(本地目录仓库 == false):决定要不要额外禁 file 传输(SEC-12)。 */
412
431
  private readonly networkRepo: boolean;
@@ -544,26 +563,22 @@ export class GitTransport implements SyncTransport {
544
563
  if (await fsx.exists(dir)) await fsx.remove(dir); // 覆盖语义:先清旧目录,避免残留旧文件
545
564
  // P1-4:会话等大分区改为内容寻址外置(写进共享 blobs/,快照只留引用)
546
565
  await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
547
- // git 不跟踪空目录:为空文件类分区目录写占位文件,保证远端保留目录。
548
- // 占位文件不参与 manifest.sectionHashes(基于传入数据计算);读回时按名+内容过滤。
549
- // 否则空分区(如未安装 skills)上传后目录在远端丢失,B 机全新 clone 下载即失败。
550
- const plain = snapshot.sections as Partial<Record<SectionId, SectionData>>;
551
- const blobSections = new Set<string>(BLOB_SECTIONS);
552
- for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
553
- // 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
554
- if (blobSections.has(sid)) continue;
555
- const data = plain[sid as SectionId] as FilesSection | undefined;
556
- if (data === undefined || data.files.length > 0) continue;
557
- // joinFs 只接受两个参数,占位路径拼进 prefix 一起传
558
- await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
559
- }
566
+ // git 不跟踪空目录:为空文件类分区目录写占位文件,保证远端保留目录
567
+ // (否则空分区(如未安装 skills)上传后目录在远端丢失,B 机全新 clone 下载即失败)。
568
+ await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
560
569
  // 同 id 旧密文单文件残留 → 一并清理(形态切换不残留)
561
570
  await fsx.remove(this.encryptedSnapshotFile(snapshot.id));
562
571
  rel = `${this.relDir(SNAPSHOTS_REL)}/${snapshot.id}`;
563
572
  }
573
+ // issue #141:`latest/` 派生视图与快照**进同一个 commit**(绝不在 push 之后补交一次)。
574
+ // 加密快照不写(没有明文镜像,引擎侧显式告警);远端已有更新视图时保持不动(不变量:永远指最新)。
575
+ const latestRel = !encrypted && await this.writeLatestMirror(snapshot, fsx) ? this.relDir(LATEST_REL) : null;
564
576
  // blob 仓与快照同一次提交:引用文件与它引用的字节绝不跨 commit 分离
565
577
  const hasBlobs = await fsx.exists(this.blobsDir());
566
- await this.runGit(hasBlobs ? ['add', '--', rel, this.relDir(BLOBS_REL)] : ['add', '--', rel]);
578
+ const addPaths = [rel];
579
+ if (hasBlobs) addPaths.push(this.relDir(BLOBS_REL));
580
+ if (latestRel !== null) addPaths.push(latestRel);
581
+ await this.runGit(['add', '--', ...addPaths]);
567
582
  const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
568
583
  if (diff.code !== 0) {
569
584
  const verb = existed ? 'update' : 'add';
@@ -573,6 +588,70 @@ export class GitTransport implements SyncTransport {
573
588
  return computeSnapshotMeta(snapshot);
574
589
  }
575
590
 
591
+ /**
592
+ * 空文件类分区的 git 占位文件(git 不跟踪空目录):
593
+ * 占位文件不参与 manifest.sectionHashes(基于传入数据计算);读回时按名+内容过滤。
594
+ */
595
+ private async writeEmptyFileSectionPlaceholders(dir: string, snapshot: SyncSnapshot, fsx: SnapshotFs): Promise<void> {
596
+ const plain = snapshot.sections as Partial<Record<SectionId, SectionData>>;
597
+ const blobSections = new Set<string>(BLOB_SECTIONS);
598
+ for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
599
+ // 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
600
+ if (blobSections.has(sid)) continue;
601
+ const data = plain[sid as SectionId] as FilesSection | undefined;
602
+ if (data === undefined || data.files.length > 0) continue;
603
+ // joinFs 只接受两个参数,占位路径拼进 prefix 一起传
604
+ await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
605
+ }
606
+ }
607
+
608
+ /**
609
+ * 写 `latest/` 派生视图(issue #141):与 `snapshots/<id>/` 逐文件同布局,manifest 里保留**真实快照 id**
610
+ * (反向识别用)。返回本轮是否真的重建 —— false = 远端已有更新的视图,保持不动。
611
+ *
612
+ * 为什么按整份快照重写而不是拷目录:布局只有 layout.ts 一处权威,重写不会漏掉新增/删除的分区与占位文件;
613
+ * 外置分区只写引用文件(字节在共享 blobs/ 里按内容寻址,不重复落盘、不重复上传)。
614
+ */
615
+ private async writeLatestMirror(snapshot: SyncSnapshot, fsx: SnapshotFs): Promise<boolean> {
616
+ const dir = this.latestDir();
617
+ const existing = await this.readDirManifest(dir);
618
+ // 不变量:latest/ 永远指向最新那份 —— out-of-order 上传(例如补齐更旧的快照)绝不把它回退成旧的
619
+ if (existing !== null && existing.createdAt > snapshot.createdAt) return false;
620
+ // 存量工作副本的 sparse 面是旧版本建的(不含 latest/)⇒ 必须在写文件**之前**补齐 cone,否则 add 拒收
621
+ await this.ensureSparseConeCoversLatest();
622
+ await fsx.remove(dir); // 覆盖语义:先清旧视图,避免残留已下线的分区文件
623
+ await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
624
+ await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
625
+ return true;
626
+ }
627
+
628
+ /**
629
+ * 存量工作副本的 sparse-checkout 面是**旧版本**建的(不含 `latest/`):cone 之外的路径 `git add` 会
630
+ * 拒绝入索引("outside of your sparse-checkout definition")⇒ 派生视图永远推不上去。
631
+ * 首次写 latest/ 前把 cone 补齐一次(克隆时 syncDirs() 已含它,新副本不会走到这里)。
632
+ *
633
+ * 非 sparse 副本(老 git 回落全量 clone)无需处理;读不出配置一律按「无需处理」走,绝不因优化失败。
634
+ */
635
+ private async ensureSparseConeCoversLatest(): Promise<void> {
636
+ if (this.sparseConeCoversLatest) return;
637
+ const flag = await this.runGit(['config', '--get', 'core.sparseCheckout'], { allowNonZero: true });
638
+ if (flag.code !== 0 || flag.stdout.trim() !== 'true') {
639
+ this.sparseConeCoversLatest = true;
640
+ return;
641
+ }
642
+ const latestLine = `/${this.relDir(LATEST_REL)}/`;
643
+ const gitPath = await this.runGit(['rev-parse', '--git-path', 'info/sparse-checkout'], { allowNonZero: true });
644
+ const relPath = gitPath.code === 0 ? gitPath.stdout.trim() : '';
645
+ const abs = relPath === '' ? '' : path.isAbsolute(relPath) ? relPath : path.join(this.o.workDir, relPath);
646
+ const content = abs === '' ? '' : await fs.readFile(abs, 'utf8').catch(() => '');
647
+ if (content.split(/\r?\n/).some((line) => line.trim() === latestLine)) {
648
+ this.sparseConeCoversLatest = true;
649
+ return;
650
+ }
651
+ const res = await this.runGit(['sparse-checkout', 'set', '--cone', ...this.syncDirs()], { allowNonZero: true });
652
+ this.sparseConeCoversLatest = res.code === 0;
653
+ }
654
+
576
655
  /** 下载快照完整载荷。明文 → 读回散文件目录;加密 → 读回密文单文件。不存在的 id 必须抛错(契约)。 */
577
656
  async download(id: string): Promise<SyncSnapshot> {
578
657
  return this.withWorkdir(() => this.downloadLocked(id));
@@ -633,7 +712,12 @@ export class GitTransport implements SyncTransport {
633
712
  await fsx.remove(encFile);
634
713
  rel = `${this.relDir(SNAPSHOTS_ENCRYPTED_REL)}/${id}.json`;
635
714
  }
636
- await this.runGit(['add', '-A', '--', rel]);
715
+ // 不变量:latest/ 绝不指向不存在的快照 —— 删掉的正是它镜像的那份时,同一 commit 里一并移除视图
716
+ // (保留策略永远保住刚 push 的那份,正常裁剪走不到这里;这是用户显式删最新快照的兜底)。
717
+ const mirror = await this.readDirManifest(this.latestDir());
718
+ const dropLatest = mirror !== null && mirror.id === id;
719
+ if (dropLatest) await fsx.remove(this.latestDir());
720
+ await this.runGit(dropLatest ? ['add', '-A', '--', rel, this.relDir(LATEST_REL)] : ['add', '-A', '--', rel]);
637
721
  const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
638
722
  if (diff.code !== 0) {
639
723
  await this.runGit(['commit', '-m', `sync: delete snapshot ${id}`]);
@@ -1195,14 +1279,20 @@ export class GitTransport implements SyncTransport {
1195
1279
  { kind: 'protocol', retryable: false },
1196
1280
  );
1197
1281
  }
1198
- // ③ 合并(不改写任何本地提交);冲突则 abort 回起点再报错
1282
+ // ③ 合并(不改写任何本地提交);冲突则 abort 回起点再报错。
1283
+ // latest/ 是派生视图(issue #141):两条历史各有一份内容不同的 latest/ 时是 add/add 冲突,
1284
+ // 但它不该挡住换仓库 —— 派生层以远端为准继续合并,其它任何路径冲突仍整笔 abort。
1199
1285
  const mergeRes = await this.runGit(['merge', '--allow-unrelated-histories', '--no-edit', `origin/${branch}`], { allowNonZero: true });
1200
1286
  if (mergeRes.code !== 0) {
1201
- await this.runGit(['merge', '--abort'], { allowNonZero: true });
1202
- throw new GitTransportError(
1203
- this.msg('sync.git.adoptMergeConflict', { branch }),
1204
- { kind: 'conflict', retryable: false },
1205
- );
1287
+ const resolved = await this.resolveLatestOnlyMergeConflict();
1288
+ const committed = resolved ? await this.runGit(['commit', '--no-edit'], { allowNonZero: true }) : null;
1289
+ if (committed === null || committed.code !== 0) {
1290
+ await this.runGit(['merge', '--abort'], { allowNonZero: true });
1291
+ throw new GitTransportError(
1292
+ this.msg('sync.git.adoptMergeConflict', { branch }),
1293
+ { kind: 'conflict', retryable: false },
1294
+ );
1295
+ }
1206
1296
  }
1207
1297
  // 让后续 pull --ff-only 有 upstream 可用(忽略失败:分支可能已被删/重命名)
1208
1298
  await this.runGit(['branch', '--set-upstream-to', `origin/${branch}`, branch], { allowNonZero: true });
@@ -1210,6 +1300,27 @@ export class GitTransport implements SyncTransport {
1210
1300
  this.repoReady = true;
1211
1301
  }
1212
1302
 
1303
+ /**
1304
+ * adopt 合并冲突里只放行 latest/(issue #141):派生视图两侧内容不同不算真冲突 ——
1305
+ * 以远端的版本收敛(合并方向 origin/<branch> 即 theirs),其它路径一律不放行。
1306
+ * 返回 false 表示冲突面不止 latest/,调用方必须 abort。
1307
+ */
1308
+ private async resolveLatestOnlyMergeConflict(): Promise<boolean> {
1309
+ const unmerged = await this.runGit(['diff', '--name-only', '--diff-filter=U'], { allowNonZero: true });
1310
+ if (unmerged.code !== 0) return false;
1311
+ const paths = unmerged.stdout.split('\n').map((line) => line.trim()).filter((line) => line !== '');
1312
+ const latestRel = this.relDir(LATEST_REL);
1313
+ const inLatest = (rel: string): boolean => rel === latestRel || rel.startsWith(`${latestRel}/`);
1314
+ if (paths.length === 0 || !paths.every(inLatest)) return false;
1315
+ for (const rel of paths) {
1316
+ const takeTheirs = await this.runGit(['checkout', '--theirs', '--', rel], { allowNonZero: true });
1317
+ // modify/delete 冲突:theirs 里没有这个文件 = 远端已不再有视图,按删除收敛
1318
+ if (takeTheirs.code !== 0) await this.runGit(['rm', '-f', '--', rel], { allowNonZero: true });
1319
+ else await this.runGit(['add', '--', rel], { allowNonZero: true });
1320
+ }
1321
+ return true;
1322
+ }
1323
+
1213
1324
  /**
1214
1325
  * pull --ff-only 同步远端;无本地提交(全新仓库)或无 upstream 时静默跳过。
1215
1326
  * opts.retryable=true(仅 list / download 等**幂等读**路径传):瞬时网络故障走有限指数退避重试。
@@ -1496,11 +1607,11 @@ export class GitTransport implements SyncTransport {
1496
1607
  }
1497
1608
 
1498
1609
  /**
1499
- * 三个同步根目录(带前缀):sparse 面、add 路径、adopt 判据都从这里取 ——
1610
+ * 四个同步根目录(带前缀,含只读派生视图 latest/):sparse 面、add 路径、adopt 判据都从这里取 ——
1500
1611
  * 任何一处漏加前缀都会让「写入 A、读取 B」(issue #131 的失败面)。
1501
1612
  */
1502
1613
  private syncDirs(): string[] {
1503
- return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL)];
1614
+ return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL), this.relDir(LATEST_REL)];
1504
1615
  }
1505
1616
 
1506
1617
  private blobsDir(): string {
@@ -1593,6 +1704,11 @@ export class GitTransport implements SyncTransport {
1593
1704
  return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_REL));
1594
1705
  }
1595
1706
 
1707
+ /** 明文最新快照的只读派生视图目录(<workDir>/latest/;配了前缀则在 <subdir>/latest/) */
1708
+ private latestDir(): string {
1709
+ return joinFs(this.o.workDir, this.relDir(LATEST_REL));
1710
+ }
1711
+
1596
1712
  /** 加密快照密文单文件目录(<workDir>/snapshots-encrypted/;配了前缀则在 <subdir>/ 下) */
1597
1713
  private encryptedSnapshotsDir(): string {
1598
1714
  return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_ENCRYPTED_REL));
@@ -14,7 +14,7 @@ import assert from 'node:assert/strict';
14
14
 
15
15
  import {
16
16
  GITHUB_ACCESS_TOKEN_URL, GITHUB_DEVICE_CODE_URL, GITHUB_DEVICE_FLOW_SCOPE,
17
- DeviceFlowStore, GitHubAuthClient, GitHubAuthError,
17
+ DEVICE_FLOW_LIMIT, DeviceFlowStore, GitHubAuthClient, GitHubAuthError,
18
18
  } from './github-auth.ts';
19
19
  import type { DeviceFlowEntry } from './github-auth.ts';
20
20
 
@@ -278,3 +278,28 @@ test('github-auth: DeviceFlowStore delete 移除条目;未过期条目可正
278
278
  store.set('flow-2', entry({ expiresAt: 1000 }));
279
279
  assert.equal(store.get('flow-2')?.clientId, 'client-123');
280
280
  });
281
+
282
+ test('github-auth: DeviceFlowStore 只 set 不 get 的过期条目 → 下次 set 时被清扫(不永久驻留 device_code)', () => {
283
+ let now = 0;
284
+ const store = new DeviceFlowStore(() => now);
285
+ store.set('flow-1', entry({ expiresAt: 100 }));
286
+ // 用户开了授权页直接关掉:这条流程从此没人轮询 ⇒ 只有 set 路径能清理它
287
+ now = 500;
288
+ store.set('flow-2', entry({ expiresAt: 10_000 }));
289
+ assert.equal(store.size, 1, '过期条目必须在写入路径被淘汰,而不是永远留在表里');
290
+ assert.equal(store.get('flow-1'), undefined);
291
+ assert.equal(store.get('flow-2')?.deviceCode, 'device-abc-123');
292
+ });
293
+
294
+ test('github-auth: DeviceFlowStore 有效条目超上限 → 淘汰最早过期者并保持 size <= DEVICE_FLOW_LIMIT', () => {
295
+ const store = new DeviceFlowStore(() => 0);
296
+ for (let i = 0; i < DEVICE_FLOW_LIMIT; i += 1) {
297
+ store.set('flow-' + i, entry({ expiresAt: 1000 + i }));
298
+ }
299
+ assert.equal(store.size, DEVICE_FLOW_LIMIT);
300
+ store.set('flow-new', entry({ expiresAt: 999_999 }));
301
+ assert.equal(store.size, DEVICE_FLOW_LIMIT, '上限是硬上界:反复开流程不能无限涨');
302
+ assert.equal(store.get('flow-0'), undefined, '先被淘汰的是最早过期的那条(最接近失效)');
303
+ assert.equal(store.get('flow-1')?.deviceCode, 'device-abc-123', '仍在有效期内的其他流程不受影响');
304
+ assert.equal(store.get('flow-new')?.deviceCode, 'device-abc-123');
305
+ });
@@ -268,10 +268,22 @@ export interface DeviceFlowEntry {
268
268
  expiresAt: number;
269
269
  }
270
270
 
271
+ /**
272
+ * 同一时刻允许驻留的设备码流上限。正常用户一次只开一个授权流程,这个数只在「反复重开」
273
+ * 或有人刷 start 端点时才会被顶到;有它才有内存上界(过期时间之外的第二重保证)。
274
+ */
275
+ export const DEVICE_FLOW_LIMIT = 32;
276
+
271
277
  /**
272
278
  * 宿主内存设备码登记表(进程生命周期,跨请求共享 —— 与 RunRegistry 同模式)。
273
279
  * device_code 的持有者是宿主:UI 只拿 flowId(随机 id),轮询时宿主凭 flowId 取回
274
- * device_code 去 GitHub 换 token。过期条目惰性清理,防止无限增长。
280
+ * device_code 去 GitHub 换 token。
281
+ *
282
+ * audit-sync sync-P6-6(P3):清理不能只挂在 get() 上 —— 用户开了授权页又直接关掉时,流程
283
+ * 从此不会有人轮询,那条含 device_code 的过期条目会永久驻留。所以**写入路径也清扫**:
284
+ * set() 先淘汰全部过期条目(now > expiresAt),再把总数压到 DEVICE_FLOW_LIMIT 以内
285
+ * (超出时淘汰最早过期的那条 = 最接近失效、最没价值的一条)。get() 的惰性清理保留,
286
+ * 任何时刻表的大小都不超过该上限。
275
287
  */
276
288
  export class DeviceFlowStore {
277
289
  private readonly flows = new Map<string, DeviceFlowEntry>();
@@ -287,9 +299,33 @@ export class DeviceFlowStore {
287
299
 
288
300
  /** 登记一个设备码流;flowId 由调用方生成(随机,不可猜) */
289
301
  set(flowId: string, entry: DeviceFlowEntry): void {
302
+ this.sweep();
290
303
  this.flows.set(flowId, entry);
291
304
  }
292
305
 
306
+ /**
307
+ * 清扫:先删全部过期条目,再在容量满时淘汰最早过期者(保证 set 之后 size <= DEVICE_FLOW_LIMIT)。
308
+ * 先扫过期是关键 —— 否则上限会去淘汰仍在有效期内的流程,把用户的正常授权踢掉。
309
+ */
310
+ private sweep(): void {
311
+ const now = this.now();
312
+ for (const [flowId, entry] of this.flows) {
313
+ if (now > entry.expiresAt) this.flows.delete(flowId);
314
+ }
315
+ while (this.flows.size >= DEVICE_FLOW_LIMIT) {
316
+ let oldestId: string | undefined;
317
+ let oldestAt = Number.POSITIVE_INFINITY;
318
+ for (const [flowId, entry] of this.flows) {
319
+ if (entry.expiresAt < oldestAt) {
320
+ oldestAt = entry.expiresAt;
321
+ oldestId = flowId;
322
+ }
323
+ }
324
+ if (oldestId === undefined) return; // 理论不可达:size >= 1 时必能挑出一条
325
+ this.flows.delete(oldestId);
326
+ }
327
+ }
328
+
293
329
  /** 取回设备码流;过期条目视为不存在并立即清理 */
294
330
  get(flowId: string): DeviceFlowEntry | undefined {
295
331
  const entry = this.flows.get(flowId);
@@ -0,0 +1,40 @@
1
+ /**
2
+ * 「本地有没有会进载荷的改动」的判定(§3.1 上传「看变化」;从 sync-engine 拆出,体量棘轮只许下调)。
3
+ *
4
+ * 为什么单独一层:判据必须与推送载荷**同集合、同字节口径** —— 比对里剔掉的易变字段必须同时从
5
+ * 载荷剔除,否则每轮轮询都真推一次(字节口径的唯一实现是 adapters/file-collection.ts 的
6
+ * comparableContent(),实测见 docs/evidence/sync-channel.md)。散在引擎里既没法单测,也没人敢改。
7
+ */
8
+ import { sanitizeSectionData } from '../core/section-sanitize.ts';
9
+ import type { ConfigAdapter, HostContext, SecretScanner } from '../core/types.ts';
10
+ import { isFileSection } from '../schema/config.ts';
11
+ import type { SectionData } from '../schema/types.ts';
12
+ import { hashSection } from './sync-state.ts';
13
+ import type { SyncState } from './sync-state.ts';
14
+
15
+ /**
16
+ * 任一候选分区(= 会进载荷的那一批)的导出 hash ≠ 基线 ⇒ true。
17
+ * - 从未同步(state.sections 为空)→ true;
18
+ * - 基线缺该分区 → true(比不出来的不能当成「没变」);
19
+ * - 单项导出失败 → 跳过该项(与 push 单项跳过语义一致),绝不当成「有变化」去触发空推。
20
+ * 只读本地导出 + sync-state,不碰远端。
21
+ */
22
+ export async function hasLocalSectionChanges(args: {
23
+ state: SyncState; adapters: readonly ConfigAdapter[]; ctx: HostContext; scanner: SecretScanner;
24
+ }): Promise<boolean> {
25
+ const { state, adapters, ctx, scanner } = args;
26
+ if (Object.keys(state.sections).length === 0) return true;
27
+ for (const adapter of adapters) {
28
+ let section;
29
+ try {
30
+ section = await adapter.export(ctx, { includeSecrets: false });
31
+ } catch {
32
+ continue;
33
+ }
34
+ const data = isFileSection(adapter.id) ? section.data : sanitizeSectionData(adapter, scanner, section.data).sanitized;
35
+ const recorded = state.sections[adapter.id];
36
+ if (recorded === undefined) return true;
37
+ if (recorded.hash !== hashSection(data as SectionData)) return true;
38
+ }
39
+ return false;
40
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * #136 B:合并落地的「两趟计划 + 待裁决面」,以及执行前的兜底快照。
3
+ *
4
+ * **旧实现的坑**:`strategy: 'replace'` + 空 `resolutions` 规划。冲突只在外部分流到
5
+ * 分区级,而被放行分区里的叶级差异项在计划里是 `Conflict`;`replace` 把它们**不分来源**
6
+ * 统一翻成 `Update` → 落盘,等于「未裁决也按远端覆盖」,且报告 review 恒空 ——
7
+ * 用户既没同意,事后也无从知道。
8
+ *
9
+ * **现口径**:
10
+ * - **分区级裁决 → 逐项 resolutions**:本次要应用的分区(`appliedIds`:autoApply + 会话
11
+ * 伴随的 workspaces)其叶级差异已由分区裁决(useRemote/useMerged/keepLocal)决定,于是
12
+ * 显式给这些项 `useImported`(合并路径的 ZIP 载荷本身就是裁决结果 —— merge 分区的
13
+ * merged 已含双方改动,keepLocal 分区的 merged 就是本机值)。
14
+ * - **没有分区裁决覆盖的项保持 `Conflict`**:不写盘(`planItemWritesTarget()`,
15
+ * `src/core/backup.ts` 拦下),并列进报告的待裁决面 —— 绝不由全局策略暗中落盘。
16
+ * - **两趟计划的理由**:resolutions 要按 **item id** 给,而 id 只有计划才知道(各适配器
17
+ * 自己算)。第一趟用 `merge` + 空 resolutions 只做「探测」,零写入;只有探测出
18
+ * 「可裁决的 Conflict」时才补一遍 resolutions 重算 —— 无差异的常规合并仍然只有一趟。
19
+ */
20
+ import { createSnapshot, FileSnapshotStore } from '../core/backup.ts';
21
+ import type {
22
+ ConfigAdapter,
23
+ HostContext,
24
+ ImportDecisions,
25
+ ImportPlan,
26
+ PathMapping,
27
+ Snapshot,
28
+ SnapshotStore,
29
+ } from '../core/types.ts';
30
+ import type { ReviewQueueItem } from './review-queue.ts';
31
+
32
+ export interface PlanMergeApplyInput {
33
+ /** 本次合成的临时 ZIP(合并结果)。 */
34
+ zipPath: string;
35
+ /** 合并路径的映射(merge() 记下的远端 sourceHome + 用户映射,issue #45)。 */
36
+ pathMappings: PathMapping[];
37
+ /** 本次要应用的分区(autoApply + 伴随分区)。 */
38
+ appliedIds: readonly string[];
39
+ /** 复用宿主 Importer 的规划入口(纯计算,零写入)。 */
40
+ createImportPlan: (zipPath: string, decisions: ImportDecisions) => Promise<ImportPlan>;
41
+ /** 待裁决项的告警文案(i18n 留在调用方,键 `sync.mergeHeldConflicts`)。 */
42
+ heldConflictMessage: (count: number, sections: string[]) => string;
43
+ }
44
+
45
+ export interface MergeApplyPlan {
46
+ plan: ImportPlan;
47
+ /** 叶级未裁决项的报告面:只给 id/分区/描述,**不带值**(值可能含敏感叶子)。 */
48
+ heldReview: ReviewQueueItem[];
49
+ /** 人读告警(空数组 = 没有待裁决项)。 */
50
+ heldWarnings: string[];
51
+ }
52
+
53
+ export async function planMergeApply(input: PlanMergeApplyInput): Promise<MergeApplyPlan> {
54
+ const probePlan = await input.createImportPlan(input.zipPath, {
55
+ strategy: 'merge',
56
+ resolutions: {},
57
+ pathMappings: input.pathMappings,
58
+ });
59
+ const decidedSections = new Set<string>(input.appliedIds);
60
+ const decidable = probePlan.items.filter((i) => i.kind === 'Conflict' && decidedSections.has(i.adapter));
61
+ let plan = probePlan;
62
+ if (decidable.length > 0) {
63
+ const injected = new Set(decidable.map((i) => i.id));
64
+ const decidedPlan = await input.createImportPlan(input.zipPath, {
65
+ strategy: 'merge',
66
+ resolutions: Object.fromEntries([...injected].map((id) => [id, 'useImported' as const])),
67
+ pathMappings: input.pathMappings,
68
+ });
69
+ // #136 A:这些 `useImported` 是**分区级**裁决换来的,不是人对这一行的确认 ——
70
+ // 标成 `decidedBy: 'section'`,写回守卫据此只保留被剥空叶子(其余照常落地)。
71
+ // 漏标就会让自动同步(零 UI)把脱敏剥离的空值当用户主动清空写下去。
72
+ plan = {
73
+ ...decidedPlan,
74
+ items: decidedPlan.items.map((i) => (i.conflict && injected.has(i.id)
75
+ ? { ...i, conflict: { ...i.conflict, decidedBy: 'section' as const } }
76
+ : i)),
77
+ };
78
+ }
79
+ const held = plan.items.filter((i) => i.kind === 'Conflict');
80
+ const heldReview: ReviewQueueItem[] = held.map((i) => ({
81
+ id: i.id,
82
+ sectionId: i.adapter,
83
+ kind: 'key',
84
+ description: i.description,
85
+ }));
86
+ const heldWarnings = held.length === 0
87
+ ? []
88
+ : [input.heldConflictMessage(held.length, [...new Set(held.map((i) => i.adapter))])];
89
+ return { plan, heldReview, heldWarnings };
90
+ }
91
+
92
+ export type MergeApplySnapshotResult =
93
+ | { ok: true; store: SnapshotStore; snapshot: Snapshot }
94
+ | { ok: false; error: unknown };
95
+
96
+ /** 执行前兜底快照(拿到 restoreId 给 UI 一键回滚);失败时把原始错误交回调用方决定文案。 */
97
+ export async function createMergeApplySnapshot(input: {
98
+ ctx: HostContext;
99
+ plan: ImportPlan;
100
+ zipPath: string;
101
+ storeDir: string;
102
+ adapters: ConfigAdapter[];
103
+ }): Promise<MergeApplySnapshotResult> {
104
+ const store: SnapshotStore = new FileSnapshotStore({ dir: input.storeDir });
105
+ try {
106
+ const snapshot = await createSnapshot({
107
+ ctx: input.ctx,
108
+ plan: input.plan,
109
+ sourceZip: input.zipPath,
110
+ store,
111
+ adapters: input.adapters,
112
+ });
113
+ return { ok: true, store, snapshot };
114
+ } catch (error) {
115
+ return { ok: false, error };
116
+ }
117
+ }