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
@@ -0,0 +1,171 @@
1
+ /**
2
+ * 生效 patch 链的读取与写前判据(#144):缓存失效语义、越界 bundle 名、pnpm hoisted 候选位置。
3
+ *
4
+ * 这三条都是「静默失败会变成错误事实」的点:缓存不失效 ⇒ 刚写的行对自己不可见;
5
+ * 名字不校验 ⇒ 链读取变成 $DSH_HOME 外的路径探针;候选漏一个 ⇒ 已安装的 bundle 被判成未安装。
6
+ */
7
+ import test from 'node:test'
8
+ import assert from 'node:assert/strict'
9
+ import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'node:fs'
10
+ import { tmpdir } from 'node:os'
11
+ import { join, resolve } from 'node:path'
12
+
13
+ import {
14
+ PatchChainCache, downgradeProvidedInserts, readExistingPatchDocument, readPatchChainFromProfile,
15
+ type PatchChangeLike,
16
+ } from './patch-chain-read.ts'
17
+ import { USER_PATCH_FILE, profilePatchRel } from '../core/patch-layers.ts'
18
+
19
+ function tempHome(): { home: string; cleanup: () => void } {
20
+ const home = mkdtempSync(join(tmpdir(), 'dsh-cm-chain-'))
21
+ return { home, cleanup: () => rmSync(home, { recursive: true, force: true }) }
22
+ }
23
+
24
+ function writeJson(path: string, value: unknown): void {
25
+ mkdirSync(resolve(path, '..'), { recursive: true })
26
+ writeFileSync(path, JSON.stringify(value), 'utf8')
27
+ }
28
+
29
+ /* ------------------------------- 缓存 ------------------------------- */
30
+
31
+ test('#144 PatchChainCache:TTL 内只读一次,过期后重读', async () => {
32
+ const cache = new PatchChainCache(2000)
33
+ let calls = 0
34
+ const load = async (): Promise<{ sources: never[]; failures: never[] }> => {
35
+ calls += 1
36
+ return { sources: [], failures: [] }
37
+ }
38
+ await cache.get(load, 1000)
39
+ await cache.get(load, 2999)
40
+ assert.equal(calls, 1, 'TTL 内必须命中缓存')
41
+ await cache.get(load, 3000)
42
+ assert.equal(calls, 2, '到期必须重读')
43
+ })
44
+
45
+ test('#144 PatchChainCache:invalidate 立即失效,并发首读只读一遍', async () => {
46
+ const cache = new PatchChainCache(60_000)
47
+ let calls = 0
48
+ const gate: { open: (() => void) | null } = { open: null }
49
+ const load = async (): Promise<{ sources: never[]; failures: never[] }> => {
50
+ calls += 1
51
+ await new Promise<void>((r) => { gate.open = r })
52
+ return { sources: [], failures: [] }
53
+ }
54
+ const a = cache.get(load, 0)
55
+ const b = cache.get(load, 0)
56
+ await new Promise((r) => setImmediate(r))
57
+ gate.open?.()
58
+ await Promise.all([a, b])
59
+ assert.equal(calls, 1, '同批次并发只读一遍(一次导入逐行写 patch)')
60
+ cache.invalidate()
61
+ const afterInvalidate = await cache.get(async () => ({ sources: [{ file: 'x', doc: [] }], failures: [] }), 0)
62
+ assert.equal(afterInvalidate.sources.length, 1, 'invalidate 之后必须重新加载,不能拿写前快照')
63
+ assert.equal(calls, 1, '重新加载走新 loader(老的不会被再调)')
64
+ const cachedAgain = await cache.get(async () => ({ sources: [], failures: [] }), 10)
65
+ assert.equal(cachedAgain.sources.length, 1, '随后同一 TTL 内仍命中缓存')
66
+ })
67
+
68
+ test('#144 PatchChainCache:load 抛错不缓存(下次还会试)', async () => {
69
+ const cache = new PatchChainCache(60_000)
70
+ let calls = 0
71
+ await assert.rejects(cache.get(async () => { calls += 1; throw new Error('boom') }, 0))
72
+ const read = await cache.get(async () => { calls += 1; return { sources: [], failures: [] } }, 1)
73
+ assert.deepEqual(read, { sources: [], failures: [] })
74
+ assert.equal(calls, 2)
75
+ })
76
+
77
+ /* --------------------------- 写前降级判据 --------------------------- */
78
+
79
+ const CHAIN = {
80
+ sources: [
81
+ { file: 'bundles:demo/cordis.patch.yml', doc: [{ insert: [{ id: 'remote-ssh', name: '@zhangfengshun/dsh-remote-ssh' }] }] },
82
+ { file: 'profiles/web/cordis.patch.yml', doc: [{ id: 'local-only' }] },
83
+ ],
84
+ failures: [],
85
+ }
86
+
87
+ test('#144 downgradeProvidedInserts:别的层 insert 提供 ⇒ 降级成顶层覆盖行并强制搬位', () => {
88
+ const changes: PatchChangeLike[] = [
89
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh' }, action: 'insert' },
90
+ { lineId: 'local-only', raw: { id: 'local-only' }, action: 'update' },
91
+ { lineId: 'fresh', raw: { id: 'fresh' }, action: 'insert' },
92
+ ]
93
+ const { effective, downgraded } = downgradeProvidedInserts(CHAIN, 'profiles/web/cordis.patch.yml', changes)
94
+ assert.deepEqual(downgraded, ['remote-ssh'], '只有 insert→update 才算降级(要告诉用户)')
95
+ assert.equal(effective[0]?.action, 'update')
96
+ assert.equal(effective[0]?.forceTopLevel, true, '必须强制落到顶层(存量 insert 成员会原地留着)')
97
+ assert.equal(effective[1]?.forceTopLevel, undefined, '本层自己的覆盖行不动')
98
+ assert.equal(effective[2]?.action, 'insert', '没人提供的 id 仍走新增')
99
+ })
100
+
101
+ test('#144 downgradeProvidedInserts:remove 不改写,链为 null 时原样返回', () => {
102
+ const { effective, downgraded } = downgradeProvidedInserts(CHAIN, 'profiles/web/cordis.patch.yml', [
103
+ { lineId: 'remote-ssh', raw: { id: 'remote-ssh' }, action: 'remove' },
104
+ ])
105
+ assert.deepEqual(downgraded, [])
106
+ assert.equal(effective[0]?.action, 'remove')
107
+ const none = downgradeProvidedInserts(null, 'profiles/web/cordis.patch.yml', [{ lineId: 'a', raw: {}, action: 'insert' }] as PatchChangeLike[])
108
+ assert.equal(none.effective[0]?.action, 'insert')
109
+ assert.deepEqual(none.downgraded, [])
110
+ })
111
+
112
+ /* ------------------------------ 链读取 ------------------------------ */
113
+
114
+ test('#144 readPatchChainFromProfile:hoisted 的 profiles/node_modules 也能命中', async () => {
115
+ const { home, cleanup } = tempHome()
116
+ try {
117
+ writeJson(join(home, 'profiles', 'web', 'package.json'), { name: 'web', dsh: { profile: { bundles: ['demo-bundle'] } } })
118
+ // 真实布局:pnpm hoisted 把包装在 profiles/node_modules 下,profile 自己的 node_modules 是空的
119
+ writeJson(join(home, 'profiles', 'node_modules', 'demo-bundle', 'package.json'), { name: 'demo-bundle', dsh: { bundle: { patch: 'cordis.patch.yml' } } })
120
+ writeFileSync(join(home, 'profiles', 'node_modules', 'demo-bundle', 'cordis.patch.yml'), '- insert:\n - id: remote-ssh\n', 'utf8')
121
+ const chain = await readPatchChainFromProfile(home, 'web')
122
+ assert.deepEqual(chain.sources.map((s) => s.file), ['bundles:demo-bundle/cordis.patch.yml', profilePatchRel('web'), USER_PATCH_FILE])
123
+ assert.deepEqual(chain.failures, [])
124
+ } finally {
125
+ cleanup()
126
+ }
127
+ })
128
+
129
+ test('#144 readPatchChainFromProfile:越界 bundle 名登记失败、绝不读 $DSH_HOME 外的文件', async () => {
130
+ const { home, cleanup } = tempHome()
131
+ try {
132
+ const outside = resolve(home, '..', 'outside-bundle')
133
+ writeJson(join(outside, 'package.json'), { name: 'outside', dsh: { bundle: { patch: 'cordis.patch.yml' } } })
134
+ writeFileSync(join(outside, 'cordis.patch.yml'), '- insert:\n - id: stolen\n', 'utf8')
135
+ writeJson(join(home, 'profiles', 'web', 'package.json'), { name: 'web', dsh: { profile: { bundles: ['../../outside-bundle'] } } })
136
+ const chain = await readPatchChainFromProfile(home, 'web')
137
+ assert.equal(chain.sources.some((s) => JSON.stringify(s.doc).includes('stolen')), false, '不得把越界包读进链')
138
+ assert.ok(chain.failures.some((f) => f.reason.includes('illegal bundle name')), '必须登记:' + JSON.stringify(chain.failures))
139
+ rmSync(outside, { recursive: true, force: true })
140
+ } finally {
141
+ cleanup()
142
+ }
143
+ })
144
+
145
+ test('#144 readPatchChainFromProfile:补丁路径越界只登记该文件', async () => {
146
+ const { home, cleanup } = tempHome()
147
+ try {
148
+ writeJson(join(home, 'profiles', 'web', 'package.json'), { name: 'web', dsh: { profile: { bundles: ['demo-bundle'] } } })
149
+ const pkg = join(home, 'profiles', 'web', 'node_modules', 'demo-bundle')
150
+ writeJson(join(pkg, 'package.json'), { name: 'demo-bundle', dsh: { bundle: { patch: ['ok.yml', '../../../escape.yml'] } } })
151
+ writeFileSync(join(pkg, 'ok.yml'), '- insert:\n - id: good\n', 'utf8')
152
+ const chain = await readPatchChainFromProfile(home, 'web')
153
+ assert.deepEqual(chain.sources.filter((s) => s.file.startsWith('bundles:')).map((s) => s.file), ['bundles:demo-bundle/ok.yml'])
154
+ assert.ok(chain.failures.some((f) => f.reason.includes('escapes the bundle directory')))
155
+ } finally {
156
+ cleanup()
157
+ }
158
+ })
159
+
160
+ test('#144 readExistingPatchDocument:ENOENT → undefined,解析不了必须抛', async () => {
161
+ const { home, cleanup } = tempHome()
162
+ try {
163
+ assert.equal(await readExistingPatchDocument(join(home, 'nope.yml')), undefined)
164
+ const broken = join(home, 'broken.yml')
165
+ writeFileSync(broken, '- insert: [\n', 'utf8')
166
+ await assert.rejects(readExistingPatchDocument(broken), '坏文件绝不能被当成空文档重建(issue #75)')
167
+ assert.equal(readFileSync(broken, 'utf8'), '- insert: [\n', '不得改动原文件')
168
+ } finally {
169
+ cleanup()
170
+ }
171
+ })
@@ -0,0 +1,259 @@
1
+ /**
2
+ * 生效 patch 链的读取与写前判据(issue #144)—— 宿主合并序:bundle 层 → profile 层 → home 层。
3
+ *
4
+ * 为什么判据要看 bundle 层:DSH 启动时把每层 patch 依次 overlay 到同一份 plugin 树上,
5
+ * 「这个 id 已存在吗」是全链事实 —— 只看两个 patch 文件会把 bundle 已提供的 id 当成
6
+ * 「本机没有」,于是导入写出第二条 insert(issue #144 报错的成因)。
7
+ *
8
+ * 本模块是宿主侧 IO 壳:纯判据在 `core/patch-chain.ts`,这里只负责把三层读成文档,
9
+ * 并给写入路径提供「已存在则降级成覆盖行」的兜底。读不出来的层一律 fail-open(进 `failures`),
10
+ * 绝不因为一个装坏的 bundle 拒绝写入 —— 但**必须登记**,否则判据缺口会静默变成「确知没有」。
11
+ */
12
+ import fs from 'node:fs/promises'
13
+ import { resolve } from 'node:path'
14
+ import {
15
+ patchChainIndex,
16
+ providedByOtherLayers,
17
+ type PatchChainRead,
18
+ type PatchChainSource,
19
+ } from '../core/patch-chain.ts'
20
+ import { USER_PATCH_FILE, profilePatchRel } from '../core/patch-layers.ts'
21
+ import { isENOENT } from './guards.ts'
22
+ import { isSameOrChild } from './paths.ts'
23
+ import { describePatchYamlError, loadPatchDocument } from './patch-yaml.ts'
24
+
25
+ /**
26
+ * patch 行改动的最小形状(`PatchFileFacade.applyPatchChanges` 的入参元素)。
27
+ *
28
+ * `forceTopLevel` = 「这一行必须落在**顶层**」:本层若还留着同名 insert 成员(0.9.0 写坏的存量),
29
+ * 光把 action 改成 update 不够 —— planPatchDocument 按**原文档归属**决定身份,insert 成员会
30
+ * 原地留着,于是同 id 仍是两条新增。置位后强制搬到顶层覆盖行。
31
+ */
32
+ export type PatchChangeLike = {
33
+ lineId: string
34
+ raw: unknown
35
+ action: 'insert' | 'update' | 'remove'
36
+ forceTopLevel?: boolean
37
+ }
38
+
39
+ /**
40
+ * 生效链读取结果的短时缓存:一次导入会逐行写 patch,逐行重读 30+ 个 bundle patch 没有意义。
41
+ *
42
+ * 只按 TTL 命中还不够 —— 写盘之后必须 `invalidate()`,否则同一批次里「我刚写下的行」对后续
43
+ * 判定不可见(本层最终会同时出现在 insert 与顶层两种身份里)。
44
+ */
45
+ export class PatchChainCache {
46
+ private at = 0
47
+ private read: PatchChainRead | null = null
48
+ private pending: Promise<PatchChainRead> | null = null
49
+ private readonly ttlMs: number
50
+
51
+ constructor(ttlMs: number) {
52
+ this.ttlMs = ttlMs
53
+ }
54
+
55
+ /** 丢弃缓存(写盘成功后调用)。 */
56
+ invalidate(): void {
57
+ this.at = 0
58
+ this.read = null
59
+ }
60
+
61
+ async get(load: () => Promise<PatchChainRead>, now: number = Date.now()): Promise<PatchChainRead> {
62
+ if (this.read !== null && now - this.at < this.ttlMs) return this.read
63
+ // 并发首读只读一遍:同一次导入里多个分区同时问「这个 id 在吗」很常见
64
+ if (this.pending === null) {
65
+ this.pending = load().then((read) => {
66
+ this.at = now
67
+ this.read = read
68
+ this.pending = null
69
+ return read
70
+ }, (err: unknown) => {
71
+ this.pending = null
72
+ throw err
73
+ })
74
+ }
75
+ return this.pending
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 把「已被**别的层**提供」的 insert 改动降级成覆盖行。
81
+ *
82
+ * 顶层 `- id: X` 行只做覆盖:宿主 `entryMap` 查得到 X 就覆盖它的 config,查不到就整行丢弃;
83
+ * 只有 `- insert:` 块成员才新增条目。所以「X 已在链上」时必须写覆盖行,再写一份 insert 就
84
+ * 变成两条「新增」抢同一个 id —— 宿主 compose 后该条目取到没有 config 的那份,插件保存配置
85
+ * 时必然报 `Configuration for "X" is overridden by a home patch or command-line overlay`。
86
+ */
87
+ export function downgradeProvidedInserts<T extends PatchChangeLike>(
88
+ chain: PatchChainRead | null,
89
+ file: string,
90
+ changes: readonly T[],
91
+ ): { effective: T[]; downgraded: string[] } {
92
+ const effective = [...changes]
93
+ const downgraded: string[] = []
94
+ if (chain === null) return { effective, downgraded }
95
+ const byOther = providedByOtherLayers(patchChainIndex(chain), file)
96
+ if (byOther.size === 0) return { effective, downgraded }
97
+ const out = effective.map((change) => {
98
+ if (change.action === 'remove') return change
99
+ const other = byOther.get(change.lineId)
100
+ // 只有「别的层以 insert(新增)提供」才会撞车:别的层若只是覆盖行,新增来源仍只有我们这一份
101
+ if (other === undefined || other.insertLayers.length === 0) return change
102
+ // 本层可能已经是 insert 成员(0.9.0 写坏的存量):只改 action 留不住位置,必须强制搬到顶层
103
+ if (change.action === 'insert') downgraded.push(change.lineId)
104
+ return { ...change, action: 'update' as const, forceTopLevel: true } as T
105
+ })
106
+ return { effective: out, downgraded }
107
+ }
108
+
109
+ /** 读整条生效链:bundle 层按声明顺序在前,其后 profile 层、home 层(与宿主合并序一致)。 */
110
+ export async function readPatchChainFromProfile(homeDir: string, profile: string): Promise<PatchChainRead> {
111
+ const sources: PatchChainSource[] = []
112
+ const failures: { file: string; reason: string }[] = []
113
+ sources.push(...await readBundleLayerSources(homeDir, profile, failures))
114
+ for (const file of [profilePatchRel(profile), USER_PATCH_FILE]) {
115
+ try {
116
+ sources.push({ file, doc: loadPatchDocument(await fs.readFile(resolve(homeDir, file), 'utf8')) })
117
+ } catch (err) {
118
+ // 真不存在 = 该层本来就没有(空文档);其余一律登记失败,绝不静默当空层
119
+ if (isENOENT(err)) {
120
+ sources.push({ file, doc: [] })
121
+ continue
122
+ }
123
+ failures.push({ file, reason: describePatchYamlError(err) })
124
+ }
125
+ }
126
+ return { sources, failures }
127
+ }
128
+
129
+ /**
130
+ * 读 bundle 层的 patch 文档:`profiles/<p>/package.json` 的 `dsh.profile.bundles` →
131
+ * 每个包的 `dsh.bundle.patch`(字符串或有序字符串数组)→ 已解析文档。
132
+ *
133
+ * 声明顺序即合并顺序(宿主 bundlePatchFiles / bundlePatchPaths 同口径)。
134
+ */
135
+ async function readBundleLayerSources(
136
+ homeDir: string,
137
+ profile: string,
138
+ failures: { file: string; reason: string }[],
139
+ ): Promise<PatchChainSource[]> {
140
+ const sources: PatchChainSource[] = []
141
+ const manifestRel = `profiles/${profile}/package.json`
142
+ let manifest: unknown
143
+ try {
144
+ manifest = JSON.parse(await fs.readFile(resolve(homeDir, manifestRel), 'utf8')) as unknown
145
+ } catch (err) {
146
+ if (!isENOENT(err)) failures.push({ file: manifestRel, reason: describePatchYamlError(err) })
147
+ return sources
148
+ }
149
+ const declared = (manifest as { dsh?: { profile?: { bundles?: unknown } } } | null)?.dsh?.profile?.bundles
150
+ if (!Array.isArray(declared)) return sources
151
+ for (const name of declared) {
152
+ if (typeof name !== 'string' || name === '') continue
153
+ // 清单来自远端快照:名字必须是合法的 npm 包名,否则 `resolve(node_modules, '../../..')` 会
154
+ // 把读取面带出 $DSH_HOME(链判据读的是文件内容,不该成为任意路径的探针)。
155
+ if (!isBundleName(name)) {
156
+ failures.push({ file: `bundles:${name}`, reason: 'illegal bundle name in profile manifest' })
157
+ continue
158
+ }
159
+ const patches = await readBundlePatches(homeDir, profile, name, failures)
160
+ if (patches === null) {
161
+ failures.push({ file: `bundles:${name}`, reason: 'package not installed in this profile' })
162
+ continue
163
+ }
164
+ sources.push(...patches)
165
+ }
166
+ return sources
167
+ }
168
+
169
+ /** 合法 bundle 名:`name` 或 `@scope/name`,分段不得为空、`.`、`..`,不得含分隔符/盘符/NUL。 */
170
+ function isBundleName(name: string): boolean {
171
+ if (name.includes('\0') || name.includes('\\') || name.startsWith('/') || /^[a-zA-Z]:/.test(name)) return false
172
+ const parts = name.split('/')
173
+ if (parts.length === 0 || parts.length > 2) return false
174
+ if (parts.length === 2 && parts[0]?.startsWith('@') !== true) return false
175
+ return parts.every((part) => part !== '' && part !== '.' && part !== '..' && /^@?[A-Za-z0-9._-]+$/.test(part))
176
+ }
177
+
178
+ /**
179
+ * 一个 bundle 声明的全部 patch 文档;包不在本机任何已知 node_modules → `null`。
180
+ *
181
+ * 三个候选位置都是**真实布局**(漏掉就不会命中,而漏掉在 fail-open 下等于「这个 bundle 不存在」):
182
+ * 1. `profiles/<p>/node_modules` —— profile 私有安装;
183
+ * 2. `profiles/node_modules` —— pnpm hoisted 树(`nodeLinker: hoisted`,`@deepseek-ai/*` 在这);
184
+ * 3. `$DSH_HOME/node_modules` —— 早期 / 手工安装。
185
+ *
186
+ * 桌面端的 inbox bundle 只有 `app.asar` 内一份(磁盘上不存在),解析不到时如实进 `failures`。
187
+ */
188
+ async function readBundlePatches(
189
+ homeDir: string,
190
+ profile: string,
191
+ name: string,
192
+ failures: { file: string; reason: string }[],
193
+ ): Promise<PatchChainSource[] | null> {
194
+ const candidates = [
195
+ resolve(homeDir, 'profiles', profile, 'node_modules', name),
196
+ resolve(homeDir, 'profiles', 'node_modules', name),
197
+ resolve(homeDir, 'node_modules', name),
198
+ ]
199
+ let pkgDir: string | null = null
200
+ let manifest: unknown = null
201
+ for (const dir of candidates) {
202
+ if (!isSameOrChild(dir, homeDir)) continue
203
+ try {
204
+ manifest = JSON.parse(await fs.readFile(resolve(dir, 'package.json'), 'utf8')) as unknown
205
+ pkgDir = dir
206
+ break
207
+ } catch (err) {
208
+ // 不存在 → 试下一个候选;存在但读不出/解析不了 → 登记后**不再 fallback**
209
+ // (换用另一个副本会拿错版本的 patch 清单,判据悄悄变味)
210
+ if (isENOENT(err)) continue
211
+ failures.push({ file: `bundles:${name}/package.json`, reason: describePatchYamlError(err) })
212
+ return null
213
+ }
214
+ }
215
+ if (pkgDir === null) return null
216
+ const patch = (manifest as { dsh?: { bundle?: { patch?: unknown } } } | null)?.dsh?.bundle?.patch
217
+ const files = typeof patch === 'string' ? [patch] : patch
218
+ if (patch !== undefined && (typeof patch !== 'string' && !Array.isArray(patch) || Array.isArray(patch) && !patch.every((file) => typeof file === 'string'))) {
219
+ failures.push({ file: `bundles:${name}`, reason: 'dsh.bundle.patch must be a string or string[]' })
220
+ return []
221
+ }
222
+ if (!Array.isArray(files)) return []
223
+ const sources: PatchChainSource[] = []
224
+ for (const rel of files as string[]) {
225
+ const file = 'bundles:' + name + '/' + rel.replace(/\\/g, '/')
226
+ const abs = resolve(pkgDir, rel)
227
+ // 声明里的路径越界(../..)→ 直接忽略该文件:链判据不该读包外的东西
228
+ if (!isSameOrChild(abs, pkgDir)) {
229
+ failures.push({ file, reason: 'patch path escapes the bundle directory' })
230
+ continue
231
+ }
232
+ try {
233
+ sources.push({ file, doc: loadPatchDocument(await fs.readFile(abs, 'utf8')) })
234
+ } catch (err) {
235
+ // 单个 patch 文件读不出来 = 该 bundle 在这条链上不提供 id,但判据缺口必须可见
236
+ failures.push({ file, reason: describePatchYamlError(err) })
237
+ }
238
+ }
239
+ return sources
240
+ }
241
+
242
+ /**
243
+ * 读既有 patch 文件并解析:真不存在(ENOENT)或内容为空 → `undefined`。
244
+ *
245
+ * 读不出来 / 解析不了**必须抛错**(issue #75 的数据丢失点):调用方把失败当成空文档重建,
246
+ * 就等于删掉用户手写的其余行。
247
+ */
248
+ export async function readExistingPatchDocument(path: string): Promise<unknown> {
249
+ let text: string
250
+ try {
251
+ text = await fs.readFile(path, 'utf8')
252
+ } catch (err) {
253
+ if (isENOENT(err)) return undefined
254
+ throw err
255
+ }
256
+ if (text.trim() === '') return undefined
257
+ return loadPatchDocument(text)
258
+ }
259
+