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
@@ -22,7 +22,7 @@
22
22
  * detail = too-large / max-logs-reached / session-byte-budget),绝不静默截断。
23
23
  */
24
24
  import { envValue, joinFor, normalizePlatform } from './platform-paths.ts';
25
- import { listDirNames, listFileNames, readBytesSafe, statOrNull } from './session-read.ts';
25
+ import { listDirNames, listFileNames, readBytesPrefixSafe, readBytesSafe, statOrNull } from './session-read.ts';
26
26
  import { readLogHeaderFromBytes } from '../utils/session-log.ts';
27
27
  import type { SessionLogHeader } from '../utils/session-log.ts';
28
28
  import type { RootProbeOptions, SessionReadOutcome } from './session-source.ts';
@@ -38,6 +38,13 @@ export type DshGeneration = 'v3' | 'v4';
38
38
  const DEFAULT_MAX_LOGS = 2000;
39
39
  const DEFAULT_MAX_FILE_BYTES = 32 * 1024 * 1024;
40
40
  const DEFAULT_MAX_TOTAL_BYTES = 256 * 1024 * 1024;
41
+
42
+ /**
43
+ * 代次嗅探窗口(审计 P7-7):header 是第一行 JSON,64 KiB 覆盖任何真实 cwd/标题组合
44
+ * (首行实测 < 1 KiB)。窗口内解析不出 header 时退回整读,因此这个值只影响「能不能提前
45
+ * 跳过非目标代次」,不会改变任何判定结果。
46
+ */
47
+ const HEADER_SNIFF_BYTES = 64 * 1024;
41
48
  const HEADER_WINDOW_BYTES = 64 * 1024;
42
49
 
43
50
  export interface DshResolvedHome { readonly home: string; readonly overridden: boolean }
@@ -158,6 +165,23 @@ export async function readDshLogs(opts: DshReadOptions): Promise<SessionReadOutc
158
165
  scanned += 1;
159
166
  if (st.size > maxFileBytes) { truncated = truncated ?? 'too-large'; continue; }
160
167
  if (bytesRead + st.size > maxTotalBytes) { truncated = 'byte-budget'; break outer; }
168
+ /**
169
+ * 代次嗅探(审计 P7-7):日志的 header 就在第一行,而 dsh 与 dsh4 两代并存的机器上
170
+ * 一半文件属于「非目标代次」——旧实现为了看那 1 KB header 把 31 MiB 的日志整份读进来
171
+ * 再丢掉。现在先只读头部窗口,判定为别的代次就直接 countedContinue,一次整读都不做。
172
+ *
173
+ * 退化路径保持旧行为:头部窗口解析不出 header(首行比窗口还长 / 读不出)时仍整读一次,
174
+ * 让 log-header-unreadable / log-read-error 这些既有码的触发条件逐字不变。
175
+ */
176
+ const head = await readBytesPrefixSafe(abs, HEADER_SNIFF_BYTES);
177
+ if (head !== null) {
178
+ const sniffed = dshHeaderOfLogBytes(head);
179
+ const sniffedGeneration = sniffed === undefined ? undefined : dshGenerationOfVersion(sniffed.version);
180
+ if (sniffedGeneration !== undefined && sniffedGeneration !== opts.generation) {
181
+ otherGeneration += 1;
182
+ continue;
183
+ }
184
+ }
161
185
  const data = await readBytesSafe(abs, maxFileBytes);
162
186
  if (data === null) {
163
187
  findings.push({ code: 'source-unreadable', origin: name, detail: 'log-read-error' });
@@ -178,7 +202,14 @@ export async function readDshLogs(opts: DshReadOptions): Promise<SessionReadOutc
178
202
  findings.push({ code: 'source-unreadable', origin: name, detail: 'log-header-incomplete' });
179
203
  continue;
180
204
  }
181
- if (seen.has(header.id)) continue;
205
+ if (seen.has(header.id)) {
206
+ /**
207
+ * 同一 id 出现在两个会话目录(复制/迁移残留):只保留第一个,但**必须留码** ——
208
+ * 静默丢会让「导入少了一条会话」在计划里无从解释(本文件顶部第 21-22 行的硬约束)。
209
+ */
210
+ findings.push({ code: 'session-id-conflict', origin: header.id, detail: 'duplicate-session-id' });
211
+ continue;
212
+ }
182
213
  seen.add(header.id);
183
214
  bytesRead += data.length;
184
215
  files.push({ id: header.id, cwd: header.cwd, version: header.version, name, data });
@@ -252,3 +252,70 @@ test('未闭合工具调用:不伪造 result、callId 非空、后续事件与
252
252
  assert.equal(rows.filter((r) => r.type === 'user/message').length, 2, '中断之后的消息不得被吞掉');
253
253
  assert.equal(rows[rows.length - 1]?.type, 'user/message');
254
254
  });
255
+
256
+ /* ---------------- 工具结果的三种「静默丢失」路径(2026-11-14 审计 P7-1/2/4/5) ---------------- */
257
+
258
+ function synthOf(messages: readonly IrMessage[]): { rows: DshSessionRow[]; stats: SynthesisStats } {
259
+ const s = stats();
260
+ const rows = synthesizeDshRows(
261
+ { id: 's9', cwd: 'D:/proj', createdAt: NOW, records: messages.length, messages },
262
+ { title: '', titleSource: { kind: 'fallback' }, emptyBlocks: [], provider: 'claude-code', reasoningEffort: 'medium', maxTokens: 8192 },
263
+ s,
264
+ NOW,
265
+ );
266
+ return { rows, stats: s };
267
+ }
268
+
269
+ /** P7-1:同一条 user 记录既含 tool_result 又含 text —— 文本必须另发一条 user/message,不能消失且不能无计数。 */
270
+ test('user 记录同时含 tool_result 与 text:文本作为独立 user/message 发出', () => {
271
+ const { rows, stats: s } = synthOf([
272
+ irMessage(irFallbackMessageId(0), 'user', [irTextBlock('开始')], { time: NOW }),
273
+ irMessage(irFallbackMessageId(1), 'assistant', [irToolCallBlock('t1', 'terminal', { command: 'ls' })], { time: NOW + 1 }),
274
+ irMessage(irFallbackMessageId(2), 'user', [irToolResultBlock('t1', 'ok', false), irTextBlock('MARKER-TEXT')], { time: NOW + 2 }),
275
+ ]);
276
+ const humanTexts = rows
277
+ .filter((r) => r.type === 'user/message')
278
+ .map((r) => (r.data as { source: { kind: string }; content: { text?: string }[] }));
279
+ const fromUser = humanTexts.filter((m) => m.source.kind === 'user').flatMap((m) => m.content.map((c) => c.text ?? ''));
280
+ assert.ok(fromUser.join('|').includes('MARKER-TEXT'), '用户文本被静默吞掉了:' + JSON.stringify(rows.map((r) => r.type)));
281
+ assert.equal(s.userMessages, 2, '结果块之后的文本也要计入 userMessages');
282
+ assert.equal(s.toolResults, 1, '同一条记录里的 tool/result 仍必须发出');
283
+ assert.equal(rows.filter((r) => r.type === 'tool/result').length, 1);
284
+ });
285
+
286
+ /** P7-4:非空 id 但不在未决表里 = 悬空引用,必须计数丢弃(不得发指向不存在调用的结果)。 */
287
+ test('tool_result 带未知 id:按 orphan-tool-result 计数,不产出悬空 callId', () => {
288
+ const { rows, stats: s } = synthOf([
289
+ irMessage(irFallbackMessageId(0), 'user', [irTextBlock('开始')], { time: NOW }),
290
+ irMessage(irFallbackMessageId(1), 'assistant', [irToolCallBlock('t1', 'terminal', { command: 'ls' })], { time: NOW + 1 }),
291
+ irMessage(irFallbackMessageId(2), 'user', [irToolResultBlock('t1', 'ok', false), irToolResultBlock('t-unknown', 'x', false)], { time: NOW + 2 }),
292
+ ]);
293
+ const results = rows.filter((r) => r.type === 'tool/result');
294
+ assert.deepEqual(results.map((r) => (r.data as { message: { source: { callId: string } } }).message.source.callId), ['t1']);
295
+ assert.equal(s.ignored['orphan-tool-result'], 1, '悬空 id 必须可见地计数');
296
+ });
297
+
298
+ /** P7-2:缺 id 的结果不得回填到**上一个回合**已闭合的调用上(openCallIds 必须按回合清空)。 */
299
+ test('回合闭合后缺 id 的 tool_result 不回填上一回合的调用', () => {
300
+ const { rows } = synthOf([
301
+ irMessage(irFallbackMessageId(0), 'user', [irTextBlock('turn1')], { time: NOW }),
302
+ irMessage(irFallbackMessageId(1), 'assistant', [irToolCallBlock('turn1-call', 'terminal', { command: 'a' })], { time: NOW + 1 }),
303
+ irMessage(irFallbackMessageId(2), 'user', [irTextBlock('turn2')], { time: NOW + 2 }),
304
+ irMessage(irFallbackMessageId(3), 'assistant', [irToolCallBlock('turn2-call', 'terminal', { command: 'b' })], { time: NOW + 3 }),
305
+ irMessage(irFallbackMessageId(4), 'user', [irToolResultBlock('', 'late', false)], { time: NOW + 4 }),
306
+ ]);
307
+ const result = rows.find((r) => r.type === 'tool/result');
308
+ const callId = (result?.data as { message: { source: { callId: string } } } | undefined)?.message.source.callId;
309
+ assert.equal(callId, 'turn2-call', '缺 id 的结果只能配对当前回合仍未结算的调用,不能跨回合回填');
310
+ });
311
+
312
+ /** P7-5:assistant 记录里携带的 tool_result 无处归位 —— 必须计数(绝不静默丢弃)。 */
313
+ test('assistant 记录携带 tool_result:按 assistant-tool-result 计数', () => {
314
+ const { rows, stats: s } = synthOf([
315
+ irMessage(irFallbackMessageId(0), 'user', [irTextBlock('开始')], { time: NOW }),
316
+ irMessage(irFallbackMessageId(1), 'assistant', [irToolCallBlock('t1', 'terminal', { command: 'ls' }), irToolResultBlock('t1', 'ok', false)], { time: NOW + 1 }),
317
+ ]);
318
+ assert.equal(s.ignored['assistant-tool-result'], 1, '助手记录里的结果块不得无声消失');
319
+ assert.equal(s.toolResults, 0);
320
+ assert.equal(rows.filter((r) => r.type === 'tool/result').length, 0);
321
+ });
@@ -330,6 +330,12 @@ export function synthesizeDshRows(
330
330
  };
331
331
  const closeTurn = (): void => {
332
332
  closeStep();
333
+ /**
334
+ * 回合闭合后再到的结果**不许**回填到本回合的调用上:`openCallIds` 是跨 step 共享的 FIFO,
335
+ * 若不在回合边界清空,下一回合缺 id 的 tool_result 会消费到上一回合已结束的 callId
336
+ * (结果落在 turn N+1、却引用 turn N 的调用,DSH 侧配对不上)。
337
+ */
338
+ openCallIds.length = 0;
333
339
  if (turnOpen) {
334
340
  push('turn/end', now, { turn, reason: { kind: 'completed' } });
335
341
  turnOpen = false;
@@ -344,6 +350,21 @@ export function synthesizeDshRows(
344
350
  });
345
351
  turnOpen = true;
346
352
  };
353
+ /** 发一条**人类** user/message(唯一形状出口:两条路径共用,避免字段漂移) */
354
+ const emitUserMessage = (text: string, at: number, id: string): void => {
355
+ push(
356
+ 'user/message',
357
+ at,
358
+ {
359
+ content: [irTextBlock(text)],
360
+ source: { kind: 'user' },
361
+ role: 'user',
362
+ id,
363
+ },
364
+ 'append',
365
+ );
366
+ stats.userMessages++;
367
+ };
347
368
 
348
369
  for (const message of session.messages) {
349
370
  // time 缺省(源没给可解析时间)→ 用调用方的时间兜底;安全整数校验在 IR 层已完成
@@ -354,6 +375,15 @@ export function synthesizeDshRows(
354
375
  if (message.role === 'assistant') {
355
376
  const text = irTextOfBlocks(blocks);
356
377
  const calls = blocks.filter((b): b is IrToolCallBlock => b.type === 'tool_call');
378
+ /**
379
+ * 助手记录里携带的 tool_result:本分支只消费 text/tool_call,这类结果**无处归位**
380
+ * (opencode 等来源把 call 与 result 写进同一条记录,由 `splitToolResults` 先拆开;
381
+ * 没拆到这里的不能静默消失)。计数后丢弃 —— 绝不产出悬空引用,也绝不无声丢内容。
382
+ */
383
+ const assistantResults = blocks.filter((b): b is IrToolResultBlock => b.type === 'tool_result');
384
+ if (assistantResults.length > 0) {
385
+ irBump(stats.ignored, 'assistant-tool-result', assistantResults.length);
386
+ }
357
387
  const source = message.passthrough?.['source'];
358
388
  const m = irStr((source as Record<string, unknown> | undefined)?.['model']);
359
389
  if (m !== undefined) model = m;
@@ -361,7 +391,8 @@ export function synthesizeDshRows(
361
391
  const providerName = hints.provider === '' ? 'foreign' : hints.provider;
362
392
  const modelName = model === '' ? 'unknown' : model;
363
393
  if (text.trim() === '' && calls.length === 0) {
364
- irBump(stats.ignored, 'assistant-empty');
394
+ // 只有 tool_result 的助手记录已按 assistant-tool-result 计数,不再重复记 assistant-empty
395
+ if (assistantResults.length === 0) irBump(stats.ignored, 'assistant-empty');
365
396
  continue;
366
397
  }
367
398
  if (!turnOpen) openTurn(at);
@@ -468,7 +499,17 @@ export function synthesizeDshRows(
468
499
  callId = next === undefined ? '' : next;
469
500
  } else {
470
501
  const idx = openCallIds.indexOf(callId);
471
- if (idx >= 0) openCallIds.splice(idx, 1);
502
+ if (idx >= 0) {
503
+ openCallIds.splice(idx, 1);
504
+ } else {
505
+ /**
506
+ * 非空 id 但不在未决表里 = 悬空引用(调用已在上一回合闭合,或源侧 id 与真正发出的
507
+ * callId 不同)。与「缺 id 且无未决调用」同口径:计数后丢弃,绝不发指向不存在
508
+ * tool/call 的结果(DSH 侧会按损坏流拒读整条会话)。
509
+ */
510
+ irBump(stats.ignored, 'orphan-tool-result');
511
+ continue;
512
+ }
472
513
  }
473
514
  if (callId === '') {
474
515
  irBump(stats.ignored, 'orphan-tool-result');
@@ -498,6 +539,16 @@ export function synthesizeDshRows(
498
539
  'append',
499
540
  );
500
541
  }
542
+ /**
543
+ * 同一条 user 记录里既有 tool_result 又有 text(Anthropic 形状里很常见:结果块后跟人类补充):
544
+ * 结果先收进当前 step,文本必须**再发一条真正的 user/message** —— 否则这段用户输入
545
+ * 既到不了目标机、也不进 ignored,属于静默丢失。
546
+ */
547
+ const trailing = irTextOfBlocks(blocks);
548
+ if (trailing.trim() !== '') {
549
+ closeTurn();
550
+ emitUserMessage(trailing, at, message.id);
551
+ }
501
552
  messageIndex++;
502
553
  continue;
503
554
  }
@@ -509,18 +560,7 @@ export function synthesizeDshRows(
509
560
  continue;
510
561
  }
511
562
  closeTurn();
512
- push(
513
- 'user/message',
514
- at,
515
- {
516
- content: [irTextBlock(text)],
517
- source: { kind: 'user' },
518
- role: 'user',
519
- id: message.id,
520
- },
521
- 'append',
522
- );
523
- stats.userMessages++;
563
+ emitUserMessage(text, at, message.id);
524
564
  messageIndex++;
525
565
  }
526
566
  closeTurn();
@@ -65,6 +65,29 @@ export async function readBytesSafe(p: string, maxBytes: number = DEFAULT_MAX_FI
65
65
  }
66
66
  }
67
67
 
68
+ /**
69
+ * 只读文件**头部窗口**(审计 P7-7):给「先看 header 再决定要不要整读」的场景用。
70
+ *
71
+ * 与 readBytesSafe 的区别只有一点:不超过 maxBytes 时只取前 maxBytes 字节,而不是整份读。
72
+ * 语义仍守四条硬边界(读不到返回 null、绝不抛、超上限不读)。调用方必须自己处理
73
+ * 「头部窗口不足以判定」的退化路径 —— 不要把它当成整份内容。
74
+ */
75
+ export async function readBytesPrefixSafe(p: string, maxBytes: number): Promise<Uint8Array | null> {
76
+ const st = await statOrNull(p);
77
+ if (st === null || !st.isFile() || maxBytes <= 0) return null;
78
+ let handle: fs.FileHandle | null = null;
79
+ try {
80
+ handle = await fs.open(p, 'r');
81
+ const buf = new Uint8Array(Math.min(maxBytes, st.size));
82
+ const { bytesRead } = await handle.read(buf, 0, buf.byteLength, 0);
83
+ return bytesRead === buf.byteLength ? buf : buf.subarray(0, bytesRead);
84
+ } catch {
85
+ return null;
86
+ } finally {
87
+ if (handle !== null) await handle.close().catch(() => undefined);
88
+ }
89
+ }
90
+
68
91
  export async function readTextSafe(p: string, maxBytes: number = DEFAULT_MAX_FILE_BYTES): Promise<string | null> {
69
92
  const st = await statOrNull(p);
70
93
  if (st === null || !st.isFile() || st.size > maxBytes) return null;
@@ -53,12 +53,13 @@ class MemPatchFile implements PatchFileFacade {
53
53
  const bucket = this.byFile.get(file);
54
54
  return bucket === undefined ? [] : [...bucket.values()];
55
55
  }
56
- async applyPatchChanges(file: string, changes: PatchChange[]): Promise<void> {
56
+ async applyPatchChanges(file: string, changes: PatchChange[]): Promise<{ downgraded: string[] }> {
57
57
  const bucket = this.bucket(file);
58
58
  for (const c of changes) {
59
59
  if (c.action === 'remove') bucket.delete(c.lineId);
60
60
  else bucket.set(c.lineId, { lineId: c.lineId, raw: c.raw });
61
61
  }
62
+ return { downgraded: [] };
62
63
  }
63
64
  }
64
65
 
@@ -1062,7 +1063,7 @@ test('#133 applyPatchChanges:新 id 走 insert 动作时落进 insert 块(
1062
1063
  }
1063
1064
  })
1064
1065
 
1065
- test('#133 跨层提升:把另一层的 insert 成员写成顶层行必须拒写(文件不动)', async () => {
1066
+ test('#133/#144 跨层覆盖:链上已提供(profile 层 insert)的 id 写成顶层行是合法覆盖', async () => {
1066
1067
  const { home, cleanup } = patchHome()
1067
1068
  try {
1068
1069
  mkdirSync(join(home, 'profiles', 'web'), { recursive: true })
@@ -1071,12 +1072,36 @@ test('#133 跨层提升:把另一层的 insert 成员写成顶层行必须拒
1071
1072
  const before = readFileSync(profileLayer, 'utf8')
1072
1073
  const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1073
1074
 
1074
- // 用户层此时不存在:写一条与 profile 层 insert 成员同 id 的顶层行 = 跨层语义翻转
1075
+ // 条目已由 profile 层的 insert 提供 ⇒ 顶层行是**覆盖**(宿主认可的形状),不是新增;
1076
+ // issue #144 之前这条被旧判据误拒(判据只问「是不是别的层 insert 过它」,没说清 legal 覆盖)。
1077
+ const outcome = await facade.applyPatchChanges(USER_PATCH_FILE, [
1078
+ { lineId: 'mcp-a', raw: { id: 'mcp-a', name: 'dsh-mcp-client', disabled: true }, action: 'update' },
1079
+ ])
1080
+ assert.deepEqual(outcome.downgraded, [])
1081
+ const text = readFileSync(join(home, USER_PATCH_FILE), 'utf8')
1082
+ assert.match(text, /^- id: '?mcp-a'?$/m, '已存在的 id 只能写顶层覆盖行: ' + text)
1083
+ assert.equal(text.includes('- insert:'), false, '覆盖行不是新增,本层不得插第二份')
1084
+ assert.equal(readFileSync(profileLayer, 'utf8'), before, '另一层当然也不许动')
1085
+ } finally {
1086
+ cleanup()
1087
+ }
1088
+ })
1089
+
1090
+ test('#144 孤儿守卫:整条链都没人提供的 id 写成顶层行必须拒写(文件不动)', async () => {
1091
+ const { home, cleanup } = patchHome()
1092
+ try {
1093
+ mkdirSync(join(home, 'profiles', 'web'), { recursive: true })
1094
+ writeFileSync(join(home, 'profiles', 'web', 'cordis.patch.yml'), INSERT_BLOCK_YAML.replace('- id: llm-pi-ai\n name: "@deepseek-ai/dsh-llm-pi-ai"\n', ''), 'utf8')
1095
+ const profileLayer = join(home, 'profiles', 'web', 'cordis.patch.yml')
1096
+ const before = readFileSync(profileLayer, 'utf8')
1097
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1098
+
1099
+ // 宿主对顶层行只做覆盖:entryMap 查不到 ⇒ patch: entry "ghost" not found 整行丢弃
1075
1100
  const err = await facade
1076
- .applyPatchChanges(USER_PATCH_FILE, [{ lineId: 'mcp-a', raw: { id: 'mcp-a', name: 'dsh-mcp-client' }, action: 'update' }])
1101
+ .applyPatchChanges(USER_PATCH_FILE, [{ lineId: 'ghost', raw: { id: 'ghost', config: { x: 1 } }, action: 'update' }])
1077
1102
  .then(() => null, (e: unknown) => e as Error)
1078
- assert.ok(err !== null, '跨层提升必须拒写')
1079
- assert.match(String(err.message), /mcp-a/)
1103
+ assert.ok(err !== null, '没人提供的顶层行必须拒写')
1104
+ assert.match(String(err.message), /ghost/)
1080
1105
  assert.equal(existsSync(join(home, USER_PATCH_FILE)), false, '拒写 = 用户层文件一个字节都没写')
1081
1106
  assert.equal(readFileSync(profileLayer, 'utf8'), before, '另一层当然也不许动')
1082
1107
  } finally {
@@ -1084,3 +1109,158 @@ test('#133 跨层提升:把另一层的 insert 成员写成顶层行必须拒
1084
1109
  }
1085
1110
  })
1086
1111
 
1112
+ /* --------------------- issue #144:bundle 层已提供的 id 只能写覆盖行 --------------------- */
1113
+
1114
+ /** 真机形态的 bundle 层:`profiles/<p>/package.json` 声明 bundle,包内 `dsh.bundle.patch` 指向 patch 文件。 */
1115
+ function bundlePatchHome(): { home: string; cleanup: () => void } {
1116
+ const { home, cleanup } = patchHome()
1117
+ const profileDir = join(home, 'profiles', 'web')
1118
+ const pkgDir = join(profileDir, 'node_modules', 'demo-bundle')
1119
+ mkdirSync(pkgDir, { recursive: true })
1120
+ writeFileSync(join(profileDir, 'package.json'), JSON.stringify({ name: 'web-profile', dsh: { profile: { bundles: ['demo-bundle'] } } }), 'utf8')
1121
+ writeFileSync(join(pkgDir, 'package.json'), JSON.stringify({ name: 'demo-bundle', dsh: { bundle: { patch: 'cordis.patch.yml' } } }), 'utf8')
1122
+ writeFileSync(join(pkgDir, 'cordis.patch.yml'), [
1123
+ '- insert:',
1124
+ ' - id: remote-ssh',
1125
+ ' name: "@zhangfengshun/dsh-remote-ssh"',
1126
+ '',
1127
+ ].join('\n'), 'utf8')
1128
+ return { home, cleanup }
1129
+ }
1130
+
1131
+ const PROFILE_LAYER = 'profiles/web/cordis.patch.yml'
1132
+
1133
+ test('#144 applyPatchChanges:bundle 层已提供的 id 要求 insert 时降级成顶层覆盖行', async () => {
1134
+ const { home, cleanup } = bundlePatchHome()
1135
+ try {
1136
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1137
+ const outcome = await facade.applyPatchChanges(PROFILE_LAYER, [
1138
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh', name: '@zhangfengshun/dsh-remote-ssh', config: { port: 2222 } }, action: 'insert' },
1139
+ ])
1140
+ assert.deepEqual(outcome.downgraded, ['remote-ssh'], '已由 bundle 层提供 → 必须降级,绝不写第二条 insert')
1141
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1142
+ assert.equal(text.includes('- insert:'), false, '本层不得再新增一份同 id: ' + text)
1143
+ assert.match(text, /^- id: '?remote-ssh'?$/m, '必须是顶层覆盖行: ' + text)
1144
+ const lines = await facade.readPatchLines(PROFILE_LAYER)
1145
+ assert.equal(lines.length, 1)
1146
+ assert.deepEqual((lines[0]?.raw as { config?: unknown } | undefined)?.config, { port: 2222 })
1147
+ } finally {
1148
+ cleanup()
1149
+ }
1150
+ })
1151
+
1152
+ test('#144 applyPatchChanges:同一个 id 重复写仍只有一行(幂等,不会越写越多)', async () => {
1153
+ const { home, cleanup } = bundlePatchHome()
1154
+ try {
1155
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1156
+ const raw = { id: 'remote-ssh', name: '@zhangfengshun/dsh-remote-ssh', config: { port: 2222 } }
1157
+ await facade.applyPatchChanges(PROFILE_LAYER, [{ lineId: 'remote-ssh', raw, action: 'insert' }])
1158
+ await facade.applyPatchChanges(PROFILE_LAYER, [{ lineId: 'remote-ssh', raw, action: 'insert' }])
1159
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1160
+ assert.equal(text.match(/^- id:/gm)?.length, 1, '同 id 只能留一行(重复 = 宿主 compose 后 config 变数组): ' + text)
1161
+ } finally {
1162
+ cleanup()
1163
+ }
1164
+ })
1165
+
1166
+ test('#144 applyPatchChanges:链上没人提供的 id 仍走 insert(新增语义不变)', async () => {
1167
+ const { home, cleanup } = bundlePatchHome()
1168
+ try {
1169
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1170
+ const outcome = await facade.applyPatchChanges(PROFILE_LAYER, [
1171
+ { lineId: 'brand-new', raw: { id: 'brand-new', name: 'pkg-new' }, action: 'insert' },
1172
+ ])
1173
+ assert.deepEqual(outcome.downgraded, [])
1174
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1175
+ assert.ok(text.includes('- insert:'), '新 id 只能靠 insert 新增(顶层行会被 not found 丢弃): ' + text)
1176
+ assert.equal(/^- id: '?brand-new'?$/m.test(text), false)
1177
+ } finally {
1178
+ cleanup()
1179
+ }
1180
+ })
1181
+
1182
+ test('#144 readPatchChain:bundle 层按声明读到,缺失的 bundle 只登记 failures', async () => {
1183
+ const { home, cleanup } = bundlePatchHome()
1184
+ try {
1185
+ writeFileSync(join(home, 'profiles', 'web', 'package.json'), JSON.stringify({ name: 'web-profile', dsh: { profile: { bundles: ['demo-bundle', 'missing-bundle'] } } }), 'utf8')
1186
+ const chain = await new DshPatchFileFacade(home, 'web', zhMsg).readPatchChain()
1187
+ assert.deepEqual(chain.sources.map((s) => s.file), ['bundles:demo-bundle/cordis.patch.yml', PROFILE_LAYER, USER_PATCH_FILE])
1188
+ assert.deepEqual(chain.failures.map((f) => f.file), ['bundles:missing-bundle'])
1189
+ } finally {
1190
+ cleanup()
1191
+ }
1192
+ })
1193
+
1194
+ test('#144 顶层覆盖行的 name 与 bundle 成员对齐(不一致宿主会整行 name mismatch 跳过)', async () => {
1195
+ const { home, cleanup } = bundlePatchHome()
1196
+ try {
1197
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1198
+ // 快照来自另一台机器:裸名;bundle 成员是 scoped 名
1199
+ await facade.applyPatchChanges(PROFILE_LAYER, [
1200
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh', name: 'dsh-remote-ssh', config: { port: 2222 } }, action: 'insert' },
1201
+ ])
1202
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1203
+ assert.ok(text.includes("@zhangfengshun/dsh-remote-ssh"), 'name 必须校正成链上既有条目的 name: ' + text)
1204
+ assert.equal(text.includes("name: dsh-remote-ssh"), false, '不得保留来源机的裸名: ' + text)
1205
+ } finally {
1206
+ cleanup()
1207
+ }
1208
+ })
1209
+
1210
+ test('#144 存量修复:本层残留的 insert 成员 + bundle 也提供 ⇒ 写 update 时搬到顶层(不再两条新增)', async () => {
1211
+ const { home, cleanup } = bundlePatchHome()
1212
+ try {
1213
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1214
+ // 0.9.0 写坏的样子:本层已经是 insert 成员(bundle 也 insert 同一 id)
1215
+ mkdirSync(join(home, 'profiles', 'web'), { recursive: true })
1216
+ writeFileSync(join(home, PROFILE_LAYER), [
1217
+ '- insert:',
1218
+ ' - id: remote-ssh',
1219
+ ' name: "@zhangfengshun/dsh-remote-ssh"',
1220
+ '',
1221
+ ].join('\n'), 'utf8')
1222
+ const outcome = await facade.applyPatchChanges(PROFILE_LAYER, [
1223
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh', name: '@zhangfengshun/dsh-remote-ssh', config: { port: 2222 } }, action: 'update' },
1224
+ ])
1225
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1226
+ assert.equal(text.includes('- insert:'), false, '同 id 仍有两条新增 ⇒ 必须搬到顶层: ' + text)
1227
+ assert.match(text, /^- id: '?remote-ssh'?$/m, '顶层覆盖行才是宿主认的形状: ' + text)
1228
+ assert.deepEqual(outcome.downgraded, [], 'action 本来就是 update,不算降级')
1229
+ } finally {
1230
+ cleanup()
1231
+ }
1232
+ })
1233
+
1234
+ test('#144 链有读不出来的层 ⇒ 孤儿守卫不作终审(fail-open,避免永久拒写)', async () => {
1235
+ const { home, cleanup } = bundlePatchHome()
1236
+ try {
1237
+ // missing-bundle 读不到:某个看不见的层可能就是 ghost 的提供者
1238
+ writeFileSync(join(home, 'profiles', 'web', 'package.json'),
1239
+ JSON.stringify({ name: 'web-profile', dsh: { profile: { bundles: ['demo-bundle', 'missing-bundle'] } } }), 'utf8')
1240
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1241
+ await facade.applyPatchChanges(PROFILE_LAYER, [
1242
+ { lineId: 'ghost', raw: { id: 'ghost', config: { a: 1 } }, action: 'update' },
1243
+ ])
1244
+ const text = readFileSync(join(home, PROFILE_LAYER), 'utf8')
1245
+ assert.match(text, /^- id: '?ghost'?$/m, '链不完整时按 fail-open 写入: ' + text)
1246
+ } finally {
1247
+ cleanup()
1248
+ }
1249
+ })
1250
+
1251
+ test('#144 写盘后链缓存失效:立刻读到的链包含刚写的行', async () => {
1252
+ const { home, cleanup } = bundlePatchHome()
1253
+ try {
1254
+ const facade = new DshPatchFileFacade(home, 'web', zhMsg)
1255
+ await facade.readPatchChain() // 预热缓存(此时 profile 层为空)
1256
+ await facade.applyPatchChanges(PROFILE_LAYER, [
1257
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh', name: '@zhangfengshun/dsh-remote-ssh', config: { port: 2222 } }, action: 'insert' },
1258
+ ])
1259
+ const chain = await facade.readPatchChain()
1260
+ const profileLayer = chain.sources.find((s) => s.file === PROFILE_LAYER)
1261
+ assert.ok(JSON.stringify(profileLayer?.doc ?? null).includes('remote-ssh'), '写后必须重读,不能拿写前快照: ' + JSON.stringify(chain.sources))
1262
+ } finally {
1263
+ cleanup()
1264
+ }
1265
+ })
1266
+