dsh-config-manager 0.8.2 → 0.9.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 (345) hide show
  1. package/lib/adapters/mcp.js +42 -13
  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 +147 -4
  10. package/lib/cli/actions.d.ts +7 -105
  11. package/lib/cli/actions.js +16 -172
  12. package/lib/cli/control-state.d.ts +54 -0
  13. package/lib/cli/control-state.js +107 -0
  14. package/lib/cli/index.js +14 -0
  15. package/lib/cli/write-gates.d.ts +61 -0
  16. package/lib/cli/write-gates.js +110 -0
  17. package/lib/client.d.ts +104 -49
  18. package/lib/client.js +12536 -12006
  19. package/lib/core/backup.d.ts +0 -4
  20. package/lib/core/backup.js +21 -4
  21. package/lib/core/boot-rescue.d.ts +1 -1
  22. package/lib/core/boot-rescue.js +12 -1
  23. package/lib/core/boot-safety.js +37 -2
  24. package/lib/core/conflict-decisions.js +2 -1
  25. package/lib/core/exporter.js +22 -1
  26. package/lib/core/journal.d.ts +54 -4
  27. package/lib/core/journal.js +158 -35
  28. package/lib/core/local-plugin-host.d.ts +29 -0
  29. package/lib/core/local-plugin-host.js +55 -48
  30. package/lib/core/messages.d.ts +7 -0
  31. package/lib/core/messages.js +14 -0
  32. package/lib/core/migration-history.js +16 -9
  33. package/lib/core/model-tools.d.ts +2 -2
  34. package/lib/core/model-tools.js +14 -2
  35. package/lib/core/patch-secret-guard.d.ts +49 -0
  36. package/lib/core/patch-secret-guard.js +164 -0
  37. package/lib/core/reconcile.d.ts +8 -0
  38. package/lib/core/reconcile.js +18 -3
  39. package/lib/core/reinstall.d.ts +2 -0
  40. package/lib/core/reinstall.js +6 -2
  41. package/lib/core/restore.js +31 -6
  42. package/lib/core/section-sanitize.d.ts +32 -0
  43. package/lib/core/section-sanitize.js +13 -0
  44. package/lib/core/snapshot-diff.d.ts +30 -0
  45. package/lib/core/snapshot-diff.js +56 -20
  46. package/lib/core/startup-barrier.js +8 -2
  47. package/lib/core/types.d.ts +22 -0
  48. package/lib/core/update-check.js +51 -2
  49. package/lib/foreign/kernel.js +9 -2
  50. package/lib/foreign/read-claude-plugins.js +29 -11
  51. package/lib/foreign/read-dsh.js +31 -2
  52. package/lib/foreign/session-ir.js +50 -9
  53. package/lib/foreign/session-read.d.ts +8 -0
  54. package/lib/foreign/session-read.js +26 -0
  55. package/lib/index.d.ts +1 -1
  56. package/lib/index.js +1 -1
  57. package/lib/profiles/dsh-profile-io.d.ts +20 -0
  58. package/lib/profiles/dsh-profile-io.js +27 -0
  59. package/lib/profiles/dsh-profile-manager.d.ts +27 -0
  60. package/lib/profiles/dsh-profile-manager.js +45 -4
  61. package/lib/profiles/dsh-profile-runtime.d.ts +5 -1
  62. package/lib/profiles/dsh-profile-runtime.js +16 -3
  63. package/lib/profiles/process-control.d.ts +18 -1
  64. package/lib/profiles/process-control.js +25 -1
  65. package/lib/routes/profiles.js +2 -1
  66. package/lib/routes/sync.js +13 -1
  67. package/lib/schema/types.d.ts +11 -0
  68. package/lib/security/redaction.js +22 -6
  69. package/lib/sync/autosync-repeat.d.ts +27 -0
  70. package/lib/sync/autosync-repeat.js +23 -0
  71. package/lib/sync/autosync-scheduler.d.ts +34 -1
  72. package/lib/sync/autosync-scheduler.js +151 -43
  73. package/lib/sync/baseline-heal.js +7 -0
  74. package/lib/sync/baseline-record.d.ts +67 -0
  75. package/lib/sync/baseline-record.js +74 -0
  76. package/lib/sync/blob-store.js +13 -0
  77. package/lib/sync/content-diff.d.ts +39 -0
  78. package/lib/sync/content-diff.js +46 -0
  79. package/lib/sync/git/git-transport.d.ts +32 -1
  80. package/lib/sync/git/git-transport.js +146 -26
  81. package/lib/sync/github-auth.d.ts +17 -1
  82. package/lib/sync/github-auth.js +37 -1
  83. package/lib/sync/merge-apply-plan.d.ts +38 -0
  84. package/lib/sync/merge-apply-plan.js +76 -0
  85. package/lib/sync/messages.d.ts +1 -0
  86. package/lib/sync/messages.js +2 -0
  87. package/lib/sync/pending-merge.d.ts +34 -0
  88. package/lib/sync/pending-merge.js +53 -0
  89. package/lib/sync/s3/s3-transport.d.ts +14 -2
  90. package/lib/sync/s3/s3-transport.js +72 -48
  91. package/lib/sync/sync-engine.d.ts +23 -40
  92. package/lib/sync/sync-engine.js +115 -132
  93. package/lib/sync/sync-history.d.ts +7 -0
  94. package/lib/sync/sync-state.d.ts +28 -0
  95. package/lib/sync/sync-state.js +20 -0
  96. package/lib/sync/transport.d.ts +12 -3
  97. package/lib/sync/transport.js +13 -4
  98. package/lib/sync/webdav/webdav-transport.d.ts +19 -2
  99. package/lib/sync/webdav/webdav-transport.js +81 -40
  100. package/lib/ui/commands.js +5 -11
  101. package/lib/ui/dsh-profiles-view.d.ts +8 -3
  102. package/lib/ui/dsh-profiles-view.js +9 -5
  103. package/lib/ui/i18n.d.ts +2 -1
  104. package/lib/ui/i18n.js +4 -2
  105. package/lib/ui/menu-placement.js +1 -1
  106. package/lib/ui/migration-consult-view.d.ts +11 -0
  107. package/lib/ui/migration-consult-view.js +15 -0
  108. package/lib/ui/overview-view.d.ts +7 -0
  109. package/lib/ui/overview-view.js +3 -0
  110. package/lib/ui/report.d.ts +9 -0
  111. package/lib/ui/report.js +17 -4
  112. package/lib/ui/selection-model.d.ts +8 -0
  113. package/lib/ui/selection-model.js +16 -0
  114. package/lib/utils/patch-yaml.d.ts +39 -1
  115. package/lib/utils/patch-yaml.js +82 -2
  116. package/lib/utils/paths.d.ts +13 -0
  117. package/lib/utils/paths.js +35 -0
  118. package/lib/utils/workdir-lock.js +18 -3
  119. package/package.json +2 -1
  120. package/src/adapters/mcp.test.ts +29 -0
  121. package/src/adapters/mcp.ts +43 -12
  122. package/src/adapters/messages.ts +2 -0
  123. package/src/adapters/patch-key-guard.ts +29 -0
  124. package/src/adapters/plugin-data.test.ts +126 -0
  125. package/src/adapters/plugin-data.ts +97 -26
  126. package/src/adapters/plugins.test.ts +288 -0
  127. package/src/adapters/plugins.ts +141 -4
  128. package/src/cli/actions.test.ts +74 -1
  129. package/src/cli/actions.ts +27 -246
  130. package/src/cli/control-state.ts +143 -0
  131. package/src/cli/index.ts +18 -0
  132. package/src/cli/web/web.test.ts +27 -0
  133. package/src/cli/write-gates.ts +149 -0
  134. package/src/client/about/AboutPanel.tsx +7 -6
  135. package/src/client/about/release-notes-view.test.ts +17 -0
  136. package/src/client/about/release-notes-view.ts +49 -24
  137. package/src/client/chat/ChatEntryView.tsx +2 -2
  138. package/src/client/chat/rollback-entry-view.ts +6 -1
  139. package/src/client/common/CommandPalette.tsx +6 -3
  140. package/src/client/common/ConfirmDialog.tsx +29 -12
  141. package/src/client/common/ContentPicker.tsx +13 -12
  142. package/src/client/common/Modal.tsx +12 -5
  143. package/src/client/common/MoreMenu.tsx +4 -1
  144. package/src/client/common/RunsCenter.tsx +103 -32
  145. package/src/client/common/Select.tsx +2 -2
  146. package/src/client/common/floating-bounds.ts +1 -1
  147. package/src/client/common/floating-units.test.ts +1 -2
  148. package/src/client/common/plan-text-redaction.test.ts +188 -3
  149. package/src/client/common/runs-center-guard.test.ts +60 -0
  150. package/src/client/common/runs-center-view.test.ts +128 -0
  151. package/src/client/common/runs-center-view.ts +96 -0
  152. package/src/client/common/section-title-action.test.ts +66 -0
  153. package/src/client/common/ui.tsx +93 -30
  154. package/src/client/config-manager.module.css +1507 -272
  155. package/src/client/consult/ConsultCard.tsx +7 -6
  156. package/src/client/environment/EnvironmentPanel.tsx +20 -21
  157. package/src/client/environment/maintenance/DiskUsageCard.tsx +65 -91
  158. package/src/client/environment/profiles-incomplete-ui.test.ts +8 -8
  159. package/src/client/export/ExportView.tsx +7 -16
  160. package/src/client/history/HistoryPanel.tsx +88 -28
  161. package/src/client/history/history-locales.ts +3 -3
  162. package/src/client/home/BackupScheduleCard.tsx +35 -30
  163. package/src/client/home/HomePanel.tsx +45 -10
  164. package/src/client/import/ConflictList.tsx +13 -13
  165. package/src/client/import/ForeignImportView.tsx +7 -6
  166. package/src/client/import/ImportWizardView.tsx +2 -3
  167. package/src/client/import/PathMappingForm.tsx +4 -4
  168. package/src/client/import/PathPickerDialog.tsx +2 -1
  169. package/src/client/import/import-wizard-steps.tsx +51 -39
  170. package/src/client/import/use-import-wizard-controller.test.ts +129 -0
  171. package/src/client/import/use-import-wizard-controller.ts +116 -22
  172. package/src/client/library/ArtifactRow.tsx +2 -2
  173. package/src/client/library/BackupInspectView.tsx +19 -15
  174. package/src/client/library/LibraryActions.tsx +56 -16
  175. package/src/client/library/LibraryPanel.tsx +7 -7
  176. package/src/client/library/RestorePlanView.tsx +13 -8
  177. package/src/client/locales/about.ts +0 -2
  178. package/src/client/locales/backup.ts +10 -12
  179. package/src/client/locales/common.ts +2 -0
  180. package/src/client/locales/export.ts +5 -12
  181. package/src/client/locales/foreign.ts +2 -2
  182. package/src/client/locales/import.ts +3 -2
  183. package/src/client/locales/overview.ts +4 -0
  184. package/src/client/locales/profiles.ts +32 -4
  185. package/src/client/locales/restore.ts +0 -2
  186. package/src/client/locales/sessions.ts +0 -2
  187. package/src/client/locales/shell.ts +2 -2
  188. package/src/client/market/MarketPanel.tsx +38 -20
  189. package/src/client/market/MyConfigsList.tsx +34 -32
  190. package/src/client/market/MyConfigsLoginCard.tsx +12 -15
  191. package/src/client/market/MyConfigsView.tsx +5 -5
  192. package/src/client/market/MyConfigsWizard.tsx +7 -12
  193. package/src/client/market/market-locales.ts +0 -6
  194. package/src/client/market/my-configs-split.test.ts +2 -1
  195. package/src/client/nav-menu-clip-guard.test.ts +52 -0
  196. package/src/client/recovery/IncompleteCopiesSection.tsx +1 -3
  197. package/src/client/recovery/RecoveryPanel.tsx +31 -16
  198. package/src/client/recovery/RollbackCard.tsx +58 -16
  199. package/src/client/recovery/RollbackFileChanges.tsx +40 -27
  200. package/src/client/recovery/SessionHealthCard.tsx +2 -2
  201. package/src/client/recovery/SessionHealthDialog.tsx +5 -7
  202. package/src/client/recovery/SessionLayoutSection.tsx +3 -5
  203. package/src/client/recovery/recovery-locales.ts +12 -8
  204. package/src/client/run-store.test.ts +30 -0
  205. package/src/client/run-store.ts +21 -1
  206. package/src/client/sync/AddChannelCard.tsx +65 -0
  207. package/src/client/sync/AutosyncCard.tsx +182 -108
  208. package/src/client/sync/ChannelClearConfirmDialog.tsx +5 -1
  209. package/src/client/sync/ChannelConfigDialog.tsx +27 -48
  210. package/src/client/sync/MergePreviewView.tsx +3 -5
  211. package/src/client/sync/PushReportSections.tsx +35 -0
  212. package/src/client/sync/SyncChannelEntryCard.tsx +33 -24
  213. package/src/client/sync/SyncConfirmView.tsx +3 -3
  214. package/src/client/sync/SyncLogList.tsx +16 -5
  215. package/src/client/sync/SyncPanel.tsx +153 -159
  216. package/src/client/sync/SyncProbeRow.tsx +5 -7
  217. package/src/client/sync/SyncRepositoryPicker.tsx +17 -4
  218. package/src/client/sync/SyncSecuritySection.tsx +157 -0
  219. package/src/client/sync/SyncSettingsDialog.tsx +304 -0
  220. package/src/client/sync/SyncSettingsView.test.ts +26 -8
  221. package/src/client/sync/WorkDirCard.tsx +2 -4
  222. package/src/client/sync/config-clear-guard.test.ts +94 -0
  223. package/src/client/sync/config-clear-guard.ts +98 -0
  224. package/src/client/sync/config-dirty.test.ts +91 -0
  225. package/src/client/sync/config-dirty.ts +88 -0
  226. package/src/client/sync/history-model.test.ts +2 -0
  227. package/src/client/sync/history-model.ts +3 -0
  228. package/src/client/sync/merge-request.ts +20 -0
  229. package/src/client/sync/sync-api.ts +12 -0
  230. package/src/client/sync/sync-locales.ts +87 -47
  231. package/src/client/sync/sync-panel-clear.test.ts +44 -0
  232. package/src/client/sync/sync-view.test.ts +20 -0
  233. package/src/client/sync/sync-view.ts +42 -4
  234. package/src/core/backup.test.ts +61 -1
  235. package/src/core/backup.ts +21 -4
  236. package/src/core/boot-rescue.test.ts +35 -0
  237. package/src/core/boot-rescue.ts +13 -2
  238. package/src/core/boot-safety.test.ts +25 -0
  239. package/src/core/boot-safety.ts +37 -2
  240. package/src/core/conflict-decisions.ts +2 -1
  241. package/src/core/exporter.ts +21 -1
  242. package/src/core/journal.test.ts +183 -0
  243. package/src/core/journal.ts +163 -25
  244. package/src/core/local-plugin-host.test.ts +68 -2
  245. package/src/core/local-plugin-host.ts +31 -4
  246. package/src/core/messages.ts +14 -0
  247. package/src/core/migration-history.test.ts +27 -0
  248. package/src/core/migration-history.ts +12 -4
  249. package/src/core/model-tools.test.ts +67 -0
  250. package/src/core/model-tools.ts +16 -4
  251. package/src/core/patch-secret-guard.test.ts +110 -0
  252. package/src/core/patch-secret-guard.ts +176 -0
  253. package/src/core/reconcile.ts +32 -4
  254. package/src/core/reinstall.ts +8 -2
  255. package/src/core/restore.ts +32 -7
  256. package/src/core/section-sanitize.ts +37 -0
  257. package/src/core/snapshot-diff.test.ts +73 -1
  258. package/src/core/snapshot-diff.ts +66 -21
  259. package/src/core/startup-barrier.test.ts +64 -0
  260. package/src/core/startup-barrier.ts +8 -2
  261. package/src/core/types.ts +27 -1
  262. package/src/core/update-check.test.ts +40 -0
  263. package/src/core/update-check.ts +53 -2
  264. package/src/foreign/claude-code.test.ts +5 -0
  265. package/src/foreign/claude-plugins.test.ts +57 -0
  266. package/src/foreign/claude-sessions-bytes.test.ts +7 -1
  267. package/src/foreign/dsh.test.ts +23 -0
  268. package/src/foreign/kernel.ts +9 -2
  269. package/src/foreign/read-claude-plugins.ts +32 -11
  270. package/src/foreign/read-dsh.ts +33 -2
  271. package/src/foreign/session-ir.test.ts +67 -0
  272. package/src/foreign/session-ir.ts +54 -14
  273. package/src/foreign/session-read.ts +23 -0
  274. package/src/index.ts +1 -1
  275. package/src/profiles/dsh-profile-copy-rollback.test.ts +58 -4
  276. package/src/profiles/dsh-profile-io.ts +29 -0
  277. package/src/profiles/dsh-profile-manager.test.ts +30 -0
  278. package/src/profiles/dsh-profile-manager.ts +53 -5
  279. package/src/profiles/dsh-profile-runtime.test.ts +29 -7
  280. package/src/profiles/dsh-profile-runtime.ts +17 -3
  281. package/src/profiles/process-control.test.ts +97 -0
  282. package/src/profiles/process-control.ts +28 -1
  283. package/src/routes/profiles.ts +2 -1
  284. package/src/routes/sync.ts +13 -1
  285. package/src/schema/types.ts +16 -1
  286. package/src/security/redaction.test.ts +31 -0
  287. package/src/security/redaction.ts +23 -6
  288. package/src/sync/autosync-repeat.test.ts +58 -0
  289. package/src/sync/autosync-repeat.ts +55 -0
  290. package/src/sync/autosync-scheduler.test.ts +417 -0
  291. package/src/sync/autosync-scheduler.ts +154 -41
  292. package/src/sync/baseline-heal.ts +6 -0
  293. package/src/sync/baseline-record.ts +135 -0
  294. package/src/sync/blob-store.test.ts +16 -0
  295. package/src/sync/blob-store.ts +13 -0
  296. package/src/sync/content-diff.test.ts +95 -0
  297. package/src/sync/content-diff.ts +79 -0
  298. package/src/sync/git/git-transport.test.ts +151 -4
  299. package/src/sync/git/git-transport.ts +143 -27
  300. package/src/sync/github-auth.test.ts +26 -1
  301. package/src/sync/github-auth.ts +37 -1
  302. package/src/sync/merge-apply-plan.ts +117 -0
  303. package/src/sync/messages.ts +2 -0
  304. package/src/sync/pending-merge.ts +77 -0
  305. package/src/sync/s3/s3-delete-and-blob-index.test.ts +249 -0
  306. package/src/sync/s3/s3-transport.ts +76 -42
  307. package/src/sync/sync-config.test.ts +39 -12
  308. package/src/sync/sync-device-identity.test.ts +3 -0
  309. package/src/sync/sync-engine.test.ts +465 -12
  310. package/src/sync/sync-engine.ts +130 -135
  311. package/src/sync/sync-history.ts +7 -0
  312. package/src/sync/sync-state.ts +48 -0
  313. package/src/sync/transport.test.ts +13 -1
  314. package/src/sync/transport.ts +12 -4
  315. package/src/sync/webdav/webdav-delete-and-blob-index.test.ts +249 -0
  316. package/src/sync/webdav/webdav-transport.ts +86 -34
  317. package/src/ui/commands.test.ts +11 -0
  318. package/src/ui/commands.ts +5 -14
  319. package/src/ui/dsh-profiles-view.test.ts +16 -0
  320. package/src/ui/dsh-profiles-view.ts +11 -5
  321. package/src/ui/i18n.ts +4 -2
  322. package/src/ui/menu-placement.ts +1 -1
  323. package/src/ui/migration-consult-view.test.ts +20 -1
  324. package/src/ui/migration-consult-view.ts +17 -0
  325. package/src/ui/overview-view.ts +10 -0
  326. package/src/ui/report.test.ts +25 -1
  327. package/src/ui/report.ts +15 -4
  328. package/src/ui/selection-model.test.ts +15 -1
  329. package/src/ui/selection-model.ts +18 -0
  330. package/src/utils/js-yaml4.d.ts +35 -0
  331. package/src/utils/patch-yaml.test.ts +207 -1
  332. package/src/utils/patch-yaml.ts +98 -2
  333. package/src/utils/paths.test.ts +14 -1
  334. package/src/utils/paths.ts +35 -0
  335. package/src/utils/workdir-lock.test.ts +69 -0
  336. package/src/utils/workdir-lock.ts +17 -3
  337. package/lib/ui/path-mapping.d.ts +0 -38
  338. package/lib/ui/path-mapping.js +0 -75
  339. package/src/client/common/InfoHint.tsx +0 -289
  340. package/src/client/common/info-hint-guard.test.ts +0 -1119
  341. package/src/client/sync/DecryptPasswordCard.tsx +0 -48
  342. package/src/client/sync/SecurityOptionsCard.tsx +0 -97
  343. package/src/client/sync/SyncSectionPickerDialog.tsx +0 -213
  344. package/src/ui/path-mapping.test.ts +0 -67
  345. package/src/ui/path-mapping.ts +0 -103
@@ -26,7 +26,9 @@
26
26
  * - **环境变量覆盖即跳过**:区域声明的 env 存在且解析结果与默认位置不同 → 报码跳过,绝不猜新位置。
27
27
  * - **不产生用户可见散文**:告警与计划项文案一律走消息目录(i18n 铁律)。
28
28
  */
29
- import { normalizePath } from '../utils/paths.ts';
29
+ import { AsyncLocalStorage } from 'node:async_hooks';
30
+
31
+ import { isSensitiveHomeRel, normalizePath } from '../utils/paths.ts';
30
32
  import { engineMsg } from './messages.ts';
31
33
  import { listFilesDetailed } from './link-report.ts';
32
34
  import type { RecursiveListing } from '../utils/recursive-walk.ts';
@@ -40,8 +42,8 @@ import { osHomeDirOf } from '../core/backup.ts';
40
42
  import { FileCollectionAdapter } from './file-collection.ts';
41
43
  import type { FilesSection } from '../schema/types.ts';
42
44
  import type {
43
- ApplyResult, ExportOptions, ExportSection, FileSystemFacade, HostContext, ImportContext, PlanItem,
44
- SectionPreview,
45
+ ApplyResult, ExportOptions, ExportSection, ExportUnit, FileSystemFacade, HostContext, ImportContext,
46
+ PlanItem, SectionPreview,
45
47
  } from '../core/types.ts';
46
48
 
47
49
  /** 区域条目(来源 + 区域),编译期与运行期唯一的区域表示 */
@@ -50,6 +52,22 @@ export type PluginDataEntry = { source: PluginDataSource; area: PluginDataArea }
50
52
  /** 声明来源缓存的 TTL(毫秒):一次导出/导入/执行里会多次解析,逐次读 15+ 个 package.json 是净开销 */
51
53
  const DECLARED_TTL_MS = 5_000;
52
54
 
55
+ /**
56
+ * 单次收集 / 分析调用的作用域:`listRelPaths()` 往里写区域表与告警,`unitIdOf()` 从里面读。
57
+ *
58
+ * 为什么必须是**按调用持有**的对象(审计 P2-7):这两个钩子一个 async 一个 sync,本来靠
59
+ * 「实例字段 this.active / this.pendingWarnings」在中间传话 —— 同一 adapter 上并发两路
60
+ * (两个 export / preview / collect)时,后起的那路会把前一路的字段覆盖掉,于是前一路的
61
+ * 文件被归到后一路的区域、告警也串到别人头上(unitId 直接由后者决定)。
62
+ * 作用域用 AsyncLocalStorage 绑定到**调用链**上,`unitIdOf` 这个同步钩子因此仍能读到本次调用的那份。
63
+ */
64
+ interface CollectScope {
65
+ /** 本轮区域表;undefined = 尚未解析(或本次调用没走到 listRelPaths) */
66
+ entries?: readonly PluginDataEntry[];
67
+ /** 本轮要并入返回值的告警(环境变量覆盖 / 凭据剔除 / 缺 home 门面) */
68
+ warnings: string[];
69
+ }
70
+
53
71
  export interface PluginDataAdapterOptions {
54
72
  /** 内置来源(缺省 = BUILTIN_PLUGIN_DATA_SOURCES;测试注入假来源) */
55
73
  readonly sources?: readonly PluginDataSource[];
@@ -82,10 +100,16 @@ export class PluginDataAdapter extends FileCollectionAdapter {
82
100
  * 白名单就变成了「谁先导入谁说了算」。
83
101
  */
84
102
  private declaredMemo: { key: string; at: number; sources: PluginDataSource[] } | undefined;
85
- /** 本轮清单(listRelPaths 写入;unitIdOf 是同步纯函数,只能从这里拿区域表) */
86
- private active: readonly PluginDataEntry[] | undefined;
87
- /** 本轮告警(环境变量覆盖跳过等;export/preview 收尾合并进返回的 warnings) */
88
- private pendingWarnings: string[] = [];
103
+ /** 每次收集调用一份作用域(listRelPaths 写、unitIdOf 读):绝不放实例字段,否则并发两路互相覆盖 */
104
+ private readonly scope = new AsyncLocalStorage<CollectScope>();
105
+ /**
106
+ * 产物 section → 本轮区域表。
107
+ *
108
+ * `listUnits()` 在收集**结束之后**才被调用(作用域已退出),但它必须用「当初归组这批文件的那张区域表」
109
+ * —— 重新解析会与清单不一致(声明来源在这期间装了/卸了插件就会出现「单元 id 不在清单里」)。
110
+ * 键是产物对象本身,因此两路并发各挂各的,不存在覆盖。
111
+ */
112
+ private readonly unitsOf = new WeakMap<object, readonly PluginDataEntry[]>();
89
113
 
90
114
  constructor(section: PluginDataSection, options: PluginDataAdapterOptions = {}) {
91
115
  super();
@@ -188,13 +212,16 @@ export class PluginDataAdapter extends FileCollectionAdapter {
188
212
  // 本分区的根是**系统 home**(~/.dsh-wallpaper-engine、~/.mnemon …),不是 $DSH_HOME ——
189
213
  // 内容 I/O 一律走 homeFs;宿主没提供就什么都不收集,并如实报码(绝不退回 $DSH_HOME 去读错位置)。
190
214
  const homeFs = ctx.homeFs;
215
+ // 作用域按调用持有(审计 P2-7):没人包住本次调用时(直接调 listRelPaths 的测试)退化成丢弃用的空壳,
216
+ // 绝不写回实例字段 —— 那正是并发串台的根因。
217
+ const scope: CollectScope = this.scope.getStore() ?? { warnings: [] };
191
218
  if (homeFs === undefined) {
192
- this.active = [];
193
- this.pendingWarnings = [engineMsg('adapter.pluginDataNoOsHome')];
219
+ scope.entries = [];
220
+ scope.warnings = [engineMsg('adapter.pluginDataNoOsHome')];
194
221
  return PluginDataAdapter.emptyListing();
195
222
  }
196
223
  const entries = await this.allowedEntries(ctx);
197
- this.active = entries;
224
+ scope.entries = entries;
198
225
  const warnings: string[] = [];
199
226
  const paths: string[] = [];
200
227
  const listing = PluginDataAdapter.emptyListing();
@@ -225,8 +252,26 @@ export class PluginDataAdapter extends FileCollectionAdapter {
225
252
  paths.push(p);
226
253
  }
227
254
  }
228
- listing.paths = paths;
229
- this.pendingWarnings = warnings;
255
+ // 凭据文件永不进第三方数据分区(审计 P2-1 P0):分区根是系统主目录,来源含插件自述声明,
256
+ // 声明 `.dsh` 就会把 ~/.dsh/.credentials.yaml 当配置带走 —— 收集侧先剔一次(打包侧还有同判据的兜底)。
257
+ const kept: string[] = [];
258
+ let sensitiveSkipped = 0;
259
+ let sensitiveSample = '';
260
+ for (const p of paths) {
261
+ if (!isSensitiveHomeRel(p)) {
262
+ kept.push(p);
263
+ continue;
264
+ }
265
+ sensitiveSkipped += 1;
266
+ if (sensitiveSample === '') sensitiveSample = p;
267
+ }
268
+ if (sensitiveSkipped > 0) {
269
+ warnings.push(engineMsg('adapter.pluginDataSensitiveSkipped', {
270
+ count: String(sensitiveSkipped), sample: sensitiveSample,
271
+ }));
272
+ }
273
+ listing.paths = kept;
274
+ scope.warnings = warnings;
230
275
  return listing;
231
276
  }
232
277
 
@@ -238,7 +283,9 @@ export class PluginDataAdapter extends FileCollectionAdapter {
238
283
  * \`active\` 可能是 undefined(例如直接调 listUnits),此时退回**内置来源**的最长前缀 —— 与清单口径同源。
239
284
  */
240
285
  protected override unitIdOf(relativePath: string): string {
241
- const entries = this.active ?? areasForSection(this.builtin, this.id);
286
+ // 区域表取自**本次调用**的作用域(AsyncLocalStorage)⇒ 并发两路各读各的(审计 P2-7);
287
+ // 收集结束后才调用的 listUnits 由覆写先铺好作用域,仍走这一处判定。
288
+ const entries = this.scope.getStore()?.entries ?? areasForSection(this.builtin, this.id);
242
289
  const hit = areaOfPath(entries, normalizePath(relativePath));
243
290
  return hit === null ? normalizePath(relativePath) : hit.area.relPath;
244
291
  }
@@ -258,18 +305,36 @@ export class PluginDataAdapter extends FileCollectionAdapter {
258
305
  }
259
306
 
260
307
  override async export(ctx: HostContext, options: ExportOptions): Promise<ExportSection<FilesSection>> {
261
- this.pendingWarnings = [];
262
- const out = await super.export(ctx, options);
263
- return { ...out, warnings: [...out.warnings, ...this.pendingWarnings] };
308
+ // 每次导出各持一份作用域(审计 P2-7):告警与区域表都属于**这一次**调用,收尾时从它合并/登记
309
+ const scope: CollectScope = { warnings: [] };
310
+ const out = await this.scope.run(scope, () => super.export(ctx, options));
311
+ const merged: ExportSection<FilesSection> = { ...out, warnings: [...out.warnings, ...scope.warnings] };
312
+ if (scope.entries !== undefined) this.unitsOf.set(merged, scope.entries);
313
+ return merged;
264
314
  }
265
315
 
266
316
  override async preview(ctx: HostContext, options: ExportOptions): Promise<SectionPreview<FilesSection>> {
267
- this.pendingWarnings = [];
268
- const out = await super.preview(ctx, options);
269
- return {
270
- ...out,
271
- section: { ...out.section, warnings: [...out.section.warnings, ...this.pendingWarnings] },
317
+ const scope: CollectScope = { warnings: [] };
318
+ const out = await this.scope.run(scope, () => super.preview(ctx, options));
319
+ const section: ExportSection<FilesSection> = {
320
+ ...out.section,
321
+ warnings: [...out.section.warnings, ...scope.warnings],
272
322
  };
323
+ if (scope.entries !== undefined) this.unitsOf.set(section, scope.entries);
324
+ return { ...out, section };
325
+ }
326
+
327
+ /**
328
+ * 收集结束后按**那一次收集**的区域表归组(审计 P2-7)。
329
+ *
330
+ * listUnits 通常在 export/preview 返回之后才被调用(作用域已退出),因此 export/preview 把区域表
331
+ * 挂在产物对象上;这里重新铺作用域再交给基类,unitIdOf 规则只有一处实现。
332
+ * 拿不到登记(例如消费者自己构造的 section、来自快照反序列化)→ 交给基类,走内置来源的兜底口径。
333
+ */
334
+ override listUnits(section: ExportSection<FilesSection>): ExportUnit[] {
335
+ const entries = this.unitsOf.get(section);
336
+ if (entries === undefined) return super.listUnits(section);
337
+ return this.scope.run({ entries, warnings: [] }, () => super.listUnits(section));
273
338
  }
274
339
 
275
340
  /**
@@ -283,15 +348,21 @@ export class PluginDataAdapter extends FileCollectionAdapter {
283
348
  // 绝不退回 $DSH_HOME 的 ctx.fs 去比对/写入(那会落到错误位置)。
284
349
  const homeFs = ctx.target.homeFs;
285
350
  const entries = homeFs === undefined ? [] : await this.allowedEntries(ctx.target);
286
- const items = await super.analyzeImport(
287
- data,
288
- homeFs === undefined ? ctx : { ...ctx, target: { ...ctx.target, fs: homeFs } },
351
+ // 用**本机**白名单表铺作用域:基类逐文件调 unitIdOf 时就有确定口径。
352
+ // 旧代码读的是实例上「上次导出的残留区域表」,并发或首次导入时直接错位(审计 P2-7);
353
+ // 而且被放行的路径必然落在某个本机区域内,区域 id 与导入后 listUnits 的口径一致。
354
+ const items = await this.scope.run(
355
+ { entries, warnings: [] },
356
+ () => super.analyzeImport(
357
+ data,
358
+ homeFs === undefined ? ctx : { ...ctx, target: { ...ctx.target, fs: homeFs } },
359
+ ),
289
360
  );
290
361
  return items.map((item) => {
291
362
  const ref = item.target?.ref;
292
363
  if (ref === undefined) return item;
293
364
  const rel = normalizeHomeRel(ref);
294
- if (rel !== null && areaOfPath(entries, rel) !== null) return item;
365
+ if (rel !== null && areaOfPath(entries, rel) !== null && !isSensitiveHomeRel(rel)) return item;
295
366
  return {
296
367
  id: item.id,
297
368
  ...(item.unitId !== undefined ? { unitId: item.unitId } : {}),
@@ -316,7 +387,7 @@ export class PluginDataAdapter extends FileCollectionAdapter {
316
387
  if (ref !== undefined) {
317
388
  const rel = normalizeHomeRel(ref);
318
389
  const entries = await this.allowedEntries(ctx.target);
319
- if (rel === null || areaOfPath(entries, rel) === null) {
390
+ if (rel === null || areaOfPath(entries, rel) === null || isSensitiveHomeRel(rel)) {
320
391
  return { ok: false, message: engineMsg('adapter.pluginDataPathNotAllowed', { path: ref }) };
321
392
  }
322
393
  }
@@ -952,3 +952,291 @@ test('④ 可移植性:本机 pnpm-workspace.yaml 存在但读不出来 →
952
952
  '读不到就不写:本机文件逐字不动',
953
953
  );
954
954
  });
955
+
956
+ test('#137 plugins: patch 行键名会被 DSH 读端改判 → 计划项降级为 Error(不写、不整笔回滚)', async () => {
957
+ const adapter = new PluginsAdapter();
958
+ const data = {
959
+ version: 1,
960
+ plugins: [],
961
+ patch: [
962
+ { lineId: 'nums', file: 'cordis.patch.yml', raw: { id: 'nums', name: 'pkg-a', config: { '007': 1 } } },
963
+ { lineId: 'empty', file: 'cordis.patch.yml', raw: { id: 'empty', name: 'pkg-a', config: { '': 1 } } },
964
+ { lineId: 'ok', file: 'cordis.patch.yml', raw: { id: 'ok', name: 'pkg-a', config: { mode: 'x' } } },
965
+ ],
966
+ } as unknown as PluginsSection;
967
+ const dst = makeContext('linux', '/home/bob');
968
+ const sections = new Map([['plugins', data]]);
969
+ const items = await adapter.analyzeImport(data, makeImportContext(dst, sections));
970
+ const byId = new Map(items.map((i) => [i.id, i]));
971
+
972
+ assert.equal(byId.get('patch:nums')?.kind, 'Error');
973
+ assert.match(byId.get('patch:nums')!.description, /007/);
974
+ assert.equal(byId.get('patch:empty')?.kind, 'Error');
975
+ assert.match(byId.get('patch:empty')!.description, /解析失败/);
976
+ assert.equal(byId.get('patch:ok')?.kind, 'Create', '正常键名照旧 Create');
977
+ assert.equal(byId.get('patch:nums')?.target, undefined, '被拦的项没有写入面,不该认领 target');
978
+ });
979
+
980
+ /* ---------------- issue #136:patch 行剥空守卫(A)与载荷归属路径(C) ---------------- */
981
+
982
+ /** 造一份「脱敏后」的 plugins 载荷:行内被剥空的叶子 + 该行的剥空路径声明。 */
983
+ function redactedSection(lineId: string, raw: Record<string, unknown>, redactedPaths?: string[]): PluginsSection {
984
+ const line: PluginsSection['patch'][number] = { file: 'cordis.patch.yml', lineId, raw };
985
+ if (redactedPaths !== undefined) line.redactedPaths = redactedPaths;
986
+ return { version: 1, plugins: [], patch: [line] };
987
+ }
988
+
989
+ function patchItem(lineId: string, extra: Partial<PlanItem> = {}): PlanItem {
990
+ return {
991
+ id: `patch:${lineId}`,
992
+ kind: 'Conflict',
993
+ adapter: 'plugins',
994
+ description: 'x',
995
+ severity: 'warning',
996
+ target: { adapter: 'plugins', ref: lineId },
997
+ ...extra,
998
+ };
999
+ }
1000
+
1001
+ test('#136/C annotateRedactions:命中路径按行归属,未命中行不带字段、不改行原文', () => {
1002
+ const section: PluginsSection = {
1003
+ version: 1,
1004
+ plugins: [],
1005
+ patch: [
1006
+ { file: 'cordis.patch.yml', lineId: 'a', raw: { id: 'a', config: { apiKey: '' } } },
1007
+ { file: 'cordis.patch.yml', lineId: 'b', raw: { id: 'b', config: { baseURL: 'https://x' } } },
1008
+ ],
1009
+ };
1010
+ const annotated = new PluginsAdapter().annotateRedactions(section, [
1011
+ { path: 'patch[0].raw.config.apiKey', field: 'apiKey' },
1012
+ // 同一行的第二条命中:去重后仍是一条(Set)
1013
+ { path: 'patch[0].raw.config.apiKey', field: 'apiKey' },
1014
+ { path: 'patch[0].raw.config.headers', field: 'headers' },
1015
+ // 分区级/行外命中(不是行内路径)必须被忽略,不能瞎归属
1016
+ { path: 'plugins[0].spec', field: 'spec' },
1017
+ ]) as PluginsSection;
1018
+
1019
+ assert.deepEqual(annotated.patch[0]?.redactedPaths, ['raw.config.apiKey', 'raw.config.headers']);
1020
+ assert.deepEqual(annotated.patch[0]?.raw, { id: 'a', config: { apiKey: '' } }, '归属只加字段,不改行原文');
1021
+ assert.equal('redactedPaths' in (annotated.patch[1] ?? {}), false, '未命中的行不得带 redactedPaths');
1022
+ // 原对象不被就地改写(纯函数)
1023
+ assert.equal('redactedPaths' in (section.patch[0] ?? {}), false);
1024
+ });
1025
+
1026
+ test('#136/A 守卫:远端被剥空 + 本机非空 → 保留本机值、其余字段照常写入、warning 只报行 id 与路径', async () => {
1027
+ const localSecret = 'sk-LOCAL-PLAINTEXT-136';
1028
+ const dst = makeContext('linux', '/home/bob', 'web');
1029
+ dst.patchFile.lines.set('line-a', {
1030
+ lineId: 'line-a',
1031
+ raw: { id: 'line-a', config: { apiKey: localSecret, baseURL: 'https://old.example' } },
1032
+ });
1033
+ const data = redactedSection('line-a', { id: 'line-a', config: { apiKey: '', baseURL: 'https://new.example' } }, ['raw.config.apiKey']);
1034
+ const adapter = new PluginsAdapter();
1035
+ const applied = await adapter.applyItem(patchItem('line-a'), makeImportContext(dst, new Map([['plugins', data]])));
1036
+
1037
+ assert.equal(applied.ok, true);
1038
+ assert.equal(applied.warning, true, '静默保留不允许:必须有可见 warning');
1039
+ assert.ok(applied.message?.includes('patch:line-a'), `告警应带 patch 行 id: ${applied.message}`);
1040
+ assert.ok(applied.message?.includes('raw.config.apiKey'), `告警应带字段路径: ${applied.message}`);
1041
+ assert.ok(!applied.message?.includes(localSecret), '告警绝不能带本机值(值本身是密钥)');
1042
+
1043
+ const written = dst.patchFile.lines.get('line-a')?.raw as { config: { apiKey: string; baseURL: string } };
1044
+ assert.equal(written.config.apiKey, localSecret, '被剥空的叶子保留本机明文');
1045
+ assert.equal(written.config.baseURL, 'https://new.example', '同一行的其余(非剥空)字段照常写入');
1046
+ });
1047
+
1048
+ test('#136/A 守卫:远端真实非空新值照常写入(不回归)', async () => {
1049
+ const remoteSecret = 'sk-REMOTE-REAL-136';
1050
+ const dst = makeContext('linux', '/home/bob', 'web');
1051
+ dst.patchFile.lines.set('line-a', {
1052
+ lineId: 'line-a',
1053
+ raw: { id: 'line-a', config: { apiKey: 'sk-LOCAL-OLD-136' } },
1054
+ });
1055
+ const data = redactedSection('line-a', { id: 'line-a', config: { apiKey: remoteSecret } }, ['raw.config.apiKey']);
1056
+ const applied = await new PluginsAdapter().applyItem(patchItem('line-a'), makeImportContext(dst, new Map([['plugins', data]])));
1057
+
1058
+ assert.equal(applied.ok, true);
1059
+ assert.notEqual(applied.warning, true, '真实新值不该触发守卫告警');
1060
+ assert.equal(
1061
+ (dst.patchFile.lines.get('line-a')?.raw as { config: { apiKey: string } }).config.apiKey,
1062
+ remoteSecret,
1063
+ '远端真实非空新值必须照常写入',
1064
+ );
1065
+ });
1066
+
1067
+ test('#136/A 守卫:分区级裁决(decidedBy: section)不算逐行确认 —— 剥空叶子保留本机、其余改动照常落地', async () => {
1068
+ const localSecret = 'sk-LOCAL-SECTION-136';
1069
+ const dst = makeContext('linux', '/home/bob', 'web');
1070
+ dst.patchFile.lines.set('imagegen', {
1071
+ lineId: 'imagegen',
1072
+ raw: {
1073
+ id: 'imagegen',
1074
+ config: { channels: [{ model: 'gpt-image-1', promptApiKey: localSecret }] },
1075
+ disabled: false,
1076
+ },
1077
+ });
1078
+ const data = redactedSection('imagegen', {
1079
+ id: 'imagegen',
1080
+ config: { channels: [{ model: 'gpt-image-2', promptApiKey: '' }] },
1081
+ disabled: true,
1082
+ }, ['raw.config.channels[0].promptApiKey']);
1083
+ // 合并落地路径:planMergeApply 第二趟把分区级裁决注入成 useImported,来源标 section
1084
+ const sectionDecided = patchItem('imagegen', {
1085
+ kind: 'Update',
1086
+ conflict: { itemId: 'patch:imagegen', resolution: 'useImported', decidedBy: 'section' },
1087
+ });
1088
+
1089
+ const applied = await new PluginsAdapter().applyItem(
1090
+ sectionDecided,
1091
+ makeImportContext(dst, new Map([['plugins', data]])),
1092
+ );
1093
+
1094
+ assert.equal(applied.ok, true, '分区级裁决仍要整行写下去,只是剥空叶子除外');
1095
+ assert.equal(applied.warning, true, '分区级裁决保留本机值时必须有可见 warning');
1096
+ assert.ok(applied.message?.includes('patch:imagegen'), `告警应带 patch 行 id: ${applied.message}`);
1097
+ assert.ok(
1098
+ applied.message?.includes('raw.config.channels[0].promptApiKey'),
1099
+ `告警应带字段路径: ${applied.message}`,
1100
+ );
1101
+ assert.ok(!applied.message?.includes(localSecret), '告警绝不能带本机值');
1102
+
1103
+ const written = dst.patchFile.lines.get('imagegen')?.raw as {
1104
+ config: { channels: { model: string; promptApiKey: string }[] };
1105
+ disabled: boolean;
1106
+ };
1107
+ assert.equal(written.config.channels[0]?.promptApiKey, localSecret, '分区级裁决不得清空被剥空叶子');
1108
+ assert.equal(written.config.channels[0]?.model, 'gpt-image-2', '远端其余改动照常落地');
1109
+ assert.equal(written.disabled, true, '同行的非剥空字段照常写入');
1110
+ });
1111
+
1112
+ test('#136/A 守卫:显式逐行确认(decidedBy: item)才允许真的写空,且确认事实进报告', async () => {
1113
+ const dst = makeContext('linux', '/home/bob', 'web');
1114
+ dst.patchFile.lines.set('line-a', {
1115
+ lineId: 'line-a',
1116
+ raw: { id: 'line-a', config: { apiKey: 'sk-LOCAL-PLAINTEXT-136' } },
1117
+ });
1118
+ const data = redactedSection('line-a', { id: 'line-a', config: { apiKey: '' } }, ['raw.config.apiKey']);
1119
+ const confirmed = patchItem('line-a', {
1120
+ kind: 'Update',
1121
+ conflict: { itemId: 'patch:line-a', resolution: 'useImported', decidedBy: 'item' },
1122
+ });
1123
+ const applied = await new PluginsAdapter().applyItem(confirmed, makeImportContext(dst, new Map([['plugins', data]])));
1124
+
1125
+ assert.equal(applied.ok, true);
1126
+ assert.equal(applied.warning, true, '写空是显式行为,报告仍要可见');
1127
+ assert.ok(applied.message?.includes('raw.config.apiKey'), `告警应带字段路径: ${applied.message}`);
1128
+ assert.equal(
1129
+ (dst.patchFile.lines.get('line-a')?.raw as { config: { apiKey: string } }).config.apiKey,
1130
+ '',
1131
+ '显式逐行确认后才允许清空',
1132
+ );
1133
+ });
1134
+
1135
+ test('#136/A 守卫:旧载荷(无 redactedPaths)走保守分支 —— 导入空串 + 本机非空即保留并告警', async () => {
1136
+ const localSecret = 'sk-LOCAL-LEGACY-136';
1137
+ const dst = makeContext('linux', '/home/bob', 'web');
1138
+ dst.patchFile.lines.set('line-legacy', {
1139
+ lineId: 'line-legacy',
1140
+ raw: { id: 'line-legacy', config: { apiKey: localSecret }, disabled: false },
1141
+ });
1142
+ const data = redactedSection('line-legacy', { id: 'line-legacy', config: { apiKey: '' }, disabled: true });
1143
+ assert.equal('redactedPaths' in (data.patch[0] ?? {}), false, '夹具必须是不带该字段的旧载荷');
1144
+
1145
+ const applied = await new PluginsAdapter().applyItem(patchItem('line-legacy'), makeImportContext(dst, new Map([['plugins', data]])));
1146
+
1147
+ assert.equal(applied.ok, true);
1148
+ assert.equal(applied.warning, true, '旧载荷更要告警:它无法区分剥空与真空');
1149
+ const written = dst.patchFile.lines.get('line-legacy')?.raw as { config: { apiKey: string }; disabled: boolean };
1150
+ assert.equal(written.config.apiKey, localSecret, '旧载荷绝不假定空值等于用户主动清空');
1151
+ assert.equal(written.disabled, true, '非空叶子仍按远端写入');
1152
+ });
1153
+
1154
+ test('#136/A 守卫:Create(本机没有该行)不拦 —— 新行原样写入,不产生告警', async () => {
1155
+ const dst = makeContext('linux', '/home/bob', 'web');
1156
+ const data = redactedSection('line-new', { id: 'line-new', config: { apiKey: '' } }, ['raw.config.apiKey']);
1157
+ const applied = await new PluginsAdapter().applyItem(
1158
+ patchItem('line-new', { kind: 'Create' }),
1159
+ makeImportContext(dst, new Map([['plugins', data]])),
1160
+ );
1161
+
1162
+ assert.equal(applied.ok, true);
1163
+ assert.notEqual(applied.warning, true);
1164
+ assert.deepEqual(dst.patchFile.lines.get('line-new')?.raw, { id: 'line-new', config: { apiKey: '' } });
1165
+ });
1166
+
1167
+ test('#136/A 守卫:来源缺省(未标注 decidedBy)按未确认处理 —— 不得清空本机明文', async () => {
1168
+ // 翻转成白名单后的关键性质:未来新增的注入路径如果忘了标来源,只会「少清空」,不会误清空。
1169
+ const localSecret = 'sk-LOCAL-UNLABELED-136';
1170
+ const dst = makeContext('linux', '/home/bob', 'web');
1171
+ dst.patchFile.lines.set('line-a', {
1172
+ lineId: 'line-a',
1173
+ raw: { id: 'line-a', config: { apiKey: localSecret } },
1174
+ });
1175
+ const data = redactedSection('line-a', { id: 'line-a', config: { apiKey: '' } }, ['raw.config.apiKey']);
1176
+ const unlabeled = patchItem('line-a', {
1177
+ kind: 'Update',
1178
+ conflict: { itemId: 'patch:line-a', resolution: 'useImported' },
1179
+ });
1180
+ const applied = await new PluginsAdapter().applyItem(unlabeled, makeImportContext(dst, new Map([['plugins', data]])));
1181
+
1182
+ assert.equal(applied.ok, true);
1183
+ assert.equal(applied.warning, true, '保留本机值必须可见(否则用户以为清空成功)');
1184
+ assert.equal(
1185
+ (dst.patchFile.lines.get('line-a')?.raw as { config: { apiKey: string } }).config.apiKey,
1186
+ localSecret,
1187
+ '没标明裁决来源的 useImported 不算人的逐行确认',
1188
+ );
1189
+ });
1190
+
1191
+ test('#136/A 守卫:本机 patch 层读不出来 → 该行拒绝写(不按「本机没有这一行」放行)', async () => {
1192
+ const dst = makeContext('linux', '/home/bob', 'web');
1193
+ const localSecret = 'sk-LOCAL-UNREADABLE-136';
1194
+ dst.patchFile.lines.set('line-a', {
1195
+ lineId: 'line-a',
1196
+ raw: { id: 'line-a', config: { apiKey: localSecret } },
1197
+ });
1198
+ const data = redactedSection('line-a', { id: 'line-a', config: { apiKey: '' } }, ['raw.config.apiKey']);
1199
+ const ctx = makeImportContext(dst, new Map([['plugins', data]]));
1200
+ // 层读失败在 readEffectivePatchLines 里被收进 failures(不抛错)—— 覆盖读取端口模拟真实失败
1201
+ ctx.target.patchFile.readPatchLines = async () => {
1202
+ throw new Error('EACCES: permission denied');
1203
+ };
1204
+
1205
+ const applied = await new PluginsAdapter().applyItem(patchItem('line-a'), ctx);
1206
+
1207
+ assert.equal(applied.ok, false, '读不出本机现值 = 无法证明确实安全,不能写');
1208
+ assert.equal(applied.warning, true, '非致命:一行读不出来不得拖垮其余分区');
1209
+ assert.ok(applied.message?.includes('patch:line-a'), `提示要指向具体行: ${applied.message}`);
1210
+ assert.ok(applied.message?.includes('EACCES'), `提示要带失败原因: ${applied.message}`);
1211
+ assert.equal(
1212
+ (dst.patchFile.lines.get('line-a')?.raw as { config: { apiKey: string } }).config.apiKey,
1213
+ localSecret,
1214
+ '零写入:本机明文逐字不动',
1215
+ );
1216
+ });
1217
+
1218
+ test('#136/C validate:redactedPaths 空数组合法(=无剥空位置,守卫走保守分支),非数组/空串元素被拦', async () => {
1219
+ const adapter = new PluginsAdapter();
1220
+ // 空数组不得拦下整包导入:它只是「本行没有剥空位置」,等价于字段缺省。
1221
+ const empty = await adapter.validate(
1222
+ { version: 1, plugins: [], patch: [{ file: 'x.yml', lineId: 'a', raw: {}, redactedPaths: [] }] } as PluginsSection,
1223
+ );
1224
+ assert.equal(empty.valid, true, `空数组应放行: ${JSON.stringify(empty.issues)}`);
1225
+
1226
+ const notArray = await adapter.validate(
1227
+ { version: 1, plugins: [], patch: [{ file: 'x.yml', lineId: 'a', raw: {}, redactedPaths: 'raw.config.apiKey' }] } as unknown as PluginsSection,
1228
+ );
1229
+ assert.equal(notArray.valid, false);
1230
+ assert.ok(notArray.issues.some((i) => i.path === 'patch[0].redactedPaths' && i.severity === 'error'));
1231
+
1232
+ const blank = await adapter.validate(
1233
+ { version: 1, plugins: [], patch: [{ file: 'x.yml', lineId: 'a', raw: {}, redactedPaths: [''] }] } as PluginsSection,
1234
+ );
1235
+ assert.equal(blank.valid, false);
1236
+ assert.ok(blank.issues.some((i) => i.path === 'patch[0].redactedPaths[0]' && i.severity === 'error'));
1237
+
1238
+ const okSection = await adapter.validate(
1239
+ { version: 1, plugins: [], patch: [{ file: 'x.yml', lineId: 'a', raw: {}, redactedPaths: ['raw.config.apiKey'] }] } as PluginsSection,
1240
+ );
1241
+ assert.equal(okSection.valid, true, `合法形状应通过: ${JSON.stringify(okSection.issues)}`);
1242
+ });