dsh-config-manager 0.9.0 → 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 (150) hide show
  1. package/lib/adapters/mcp.js +9 -2
  2. package/lib/adapters/plugins.js +65 -10
  3. package/lib/adapters/prompts.js +15 -4
  4. package/lib/adapters/test-helpers.d.ts +15 -1
  5. package/lib/adapters/test-helpers.js +11 -0
  6. package/lib/cli/help.d.ts +1 -1
  7. package/lib/cli/help.js +21 -14
  8. package/lib/cli/index.js +118 -104
  9. package/lib/cli/sessions-repair.js +58 -22
  10. package/lib/cli/web/client-script.d.ts +1 -1
  11. package/lib/cli/web/client-script.js +71 -5
  12. package/lib/cli/web/page.d.ts +9 -19
  13. package/lib/cli/web/page.js +277 -173
  14. package/lib/cli/web/routes.js +85 -25
  15. package/lib/client.d.ts +93 -27
  16. package/lib/client.js +11461 -11339
  17. package/lib/core/messages.d.ts +10 -0
  18. package/lib/core/messages.js +20 -0
  19. package/lib/core/model-tools.js +7 -1
  20. package/lib/core/patch-chain.d.ts +65 -0
  21. package/lib/core/patch-chain.js +100 -0
  22. package/lib/core/patch-document.d.ts +32 -4
  23. package/lib/core/patch-document.js +48 -17
  24. package/lib/core/patch-secret-guard.d.ts +1 -1
  25. package/lib/core/patch-secret-guard.js +1 -1
  26. package/lib/core/types.d.ts +16 -1
  27. package/lib/index.d.ts +9 -4
  28. package/lib/index.js +42 -47
  29. package/lib/routes/sync.js +6 -0
  30. package/lib/sync/autosync-scheduler.d.ts +13 -1
  31. package/lib/sync/autosync-scheduler.js +73 -17
  32. package/lib/sync/local-changes.d.ts +15 -0
  33. package/lib/sync/local-changes.js +39 -0
  34. package/lib/sync/merge-keyed.d.ts +68 -0
  35. package/lib/sync/merge-keyed.js +124 -0
  36. package/lib/sync/patch-line-identity.d.ts +19 -0
  37. package/lib/sync/patch-line-identity.js +99 -0
  38. package/lib/sync/pending-merge.d.ts +8 -1
  39. package/lib/sync/pending-merge.js +13 -5
  40. package/lib/sync/plugin-removals.d.ts +15 -15
  41. package/lib/sync/plugin-removals.js +134 -83
  42. package/lib/sync/push-skip.d.ts +55 -0
  43. package/lib/sync/push-skip.js +70 -0
  44. package/lib/sync/sync-engine.d.ts +22 -12
  45. package/lib/sync/sync-engine.js +53 -70
  46. package/lib/sync/workspace-merge.d.ts +8 -2
  47. package/lib/ui/artifact-view.d.ts +1 -1
  48. package/lib/ui/foreign-view.d.ts +14 -0
  49. package/lib/ui/foreign-view.js +34 -0
  50. package/lib/ui/i18n.d.ts +5 -0
  51. package/lib/ui/i18n.js +11 -0
  52. package/lib/ui/selection-model.d.ts +2 -2
  53. package/lib/ui/selection-model.js +7 -3
  54. package/lib/utils/async-once.d.ts +11 -0
  55. package/lib/utils/async-once.js +15 -0
  56. package/lib/utils/patch-chain-read.d.ts +51 -0
  57. package/lib/utils/patch-chain-read.js +240 -0
  58. package/package.json +2 -2
  59. package/src/adapters/mcp.ts +9 -2
  60. package/src/adapters/plugins.test.ts +115 -0
  61. package/src/adapters/plugins.ts +64 -11
  62. package/src/adapters/prompts.ts +15 -4
  63. package/src/adapters/test-helpers.ts +15 -1
  64. package/src/cli/help.ts +21 -14
  65. package/src/cli/index.ts +105 -107
  66. package/src/cli/sessions-repair.ts +55 -22
  67. package/src/cli/web/client-script.ts +71 -5
  68. package/src/cli/web/page.ts +327 -208
  69. package/src/cli/web/routes.ts +85 -25
  70. package/src/cli/web/web.test.ts +38 -0
  71. package/src/client/ConfigManagerSection.tsx +8 -4
  72. package/src/client/about/AboutPanel.tsx +2 -6
  73. package/src/client/api.ts +2 -1
  74. package/src/client/common/ContentPicker.tsx +11 -12
  75. package/src/client/common/plan-text-redaction.test.ts +2 -2
  76. package/src/client/config-manager.module.css +10 -0
  77. package/src/client/environment/EnvironmentPanel.tsx +2 -4
  78. package/src/client/environment/maintenance/DiskUsageCard.tsx +4 -5
  79. package/src/client/home/BackupScheduleCard.tsx +52 -49
  80. package/src/client/home/HomePanel.tsx +2 -2
  81. package/src/client/import/ForeignImportView.tsx +60 -3
  82. package/src/client/import/ImportWizardView.tsx +2 -1
  83. package/src/client/import/PathMappingForm.tsx +32 -5
  84. package/src/client/import/import-wizard-split.test.ts +1 -1
  85. package/src/client/library/BackupInspectView.tsx +4 -2
  86. package/src/client/library/LibraryActions.tsx +60 -12
  87. package/src/client/library/LibraryPanel.tsx +9 -10
  88. package/src/client/locales/backup.ts +5 -0
  89. package/src/client/locales/foreign.ts +2 -0
  90. package/src/client/locales/import.ts +2 -0
  91. package/src/client/locales/restore.ts +2 -0
  92. package/src/client/market/MarketImportReview.tsx +4 -4
  93. package/src/client/market/MarketPanel.tsx +20 -36
  94. package/src/client/market/MyConfigsView.tsx +3 -10
  95. package/src/client/recovery/RecoveryPanel.tsx +4 -6
  96. package/src/client/recovery/RollbackCard.tsx +74 -59
  97. package/src/client/recovery/rollback-enabled.ts +29 -0
  98. package/src/client/recovery/use-recovery-write.ts +5 -0
  99. package/src/client/sync/AutosyncCard.tsx +3 -1
  100. package/src/client/sync/MergePreviewView.tsx +2 -2
  101. package/src/client/sync/PushPreviewCard.tsx +2 -1
  102. package/src/client/sync/SyncConfirmView.tsx +127 -130
  103. package/src/client/sync/SyncLogList.tsx +14 -10
  104. package/src/client/sync/SyncPanel.tsx +37 -28
  105. package/src/client/sync/SyncSettingsDialog.tsx +10 -21
  106. package/src/client/sync/history-model.ts +2 -0
  107. package/src/client/sync/sync-api.ts +1 -1
  108. package/src/client/sync/sync-locales.ts +8 -0
  109. package/src/client/sync/sync-push-preview.test.ts +9 -0
  110. package/src/client/sync/sync-view.ts +34 -0
  111. package/src/core/messages.ts +20 -0
  112. package/src/core/model-tools.ts +7 -1
  113. package/src/core/patch-chain.test.ts +83 -0
  114. package/src/core/patch-chain.ts +150 -0
  115. package/src/core/patch-document.test.ts +48 -1
  116. package/src/core/patch-document.ts +66 -15
  117. package/src/core/patch-layers.test.ts +1 -1
  118. package/src/core/patch-secret-guard.ts +1 -1
  119. package/src/core/smoke.test.ts +2 -1
  120. package/src/core/types.ts +16 -1
  121. package/src/index.facade.test.ts +186 -6
  122. package/src/index.ts +53 -47
  123. package/src/routes/sync.ts +6 -0
  124. package/src/security/security.test.ts +1 -1
  125. package/src/sync/autosync-scheduler.test.ts +98 -0
  126. package/src/sync/autosync-scheduler.ts +78 -17
  127. package/src/sync/local-changes.ts +40 -0
  128. package/src/sync/merge-keyed.test.ts +167 -0
  129. package/src/sync/merge-keyed.ts +160 -0
  130. package/src/sync/messages.ts +1 -0
  131. package/src/sync/patch-line-identity.test.ts +73 -0
  132. package/src/sync/patch-line-identity.ts +105 -0
  133. package/src/sync/pending-merge.test.ts +41 -0
  134. package/src/sync/pending-merge.ts +13 -5
  135. package/src/sync/plugin-removals.test.ts +199 -39
  136. package/src/sync/plugin-removals.ts +150 -64
  137. package/src/sync/push-skip.test.ts +326 -0
  138. package/src/sync/push-skip.ts +102 -0
  139. package/src/sync/sync-engine.ts +68 -67
  140. package/src/sync/workspace-merge.ts +9 -2
  141. package/src/ui/artifact-view.ts +1 -1
  142. package/src/ui/foreign-view.test.ts +49 -0
  143. package/src/ui/foreign-view.ts +47 -0
  144. package/src/ui/i18n.ts +11 -0
  145. package/src/ui/selection-model.test.ts +14 -0
  146. package/src/ui/selection-model.ts +7 -4
  147. package/src/utils/async-once.ts +14 -0
  148. package/src/utils/patch-chain-read.test.ts +171 -0
  149. package/src/utils/patch-chain-read.ts +259 -0
  150. package/src/utils/patch-yaml.test.ts +8 -2
@@ -197,6 +197,7 @@ export declare const zh: {
197
197
  readonly 'host.patchUnreadable': 'patch 层 {file} 读不出来({reason}):该层的 patch 行不会被导出/同步,也不会被改写';
198
198
  readonly 'host.patchRefuseClobber': 'patch 层 {file} 解析失败({reason}):已拒绝覆盖写入,否则会把这层里读不出来的行整体删掉';
199
199
  readonly 'host.patchRefusePromote': 'patch 层 {file} 的写入会把 {ids} 从 insert(新增)改成顶层行(覆盖):已拒绝写入,文件未改动(顶层行按 id 覆盖,查不到就整条丢弃)';
200
+ readonly 'host.patchRefuseUnknownId': 'patch 层 {file} 的写入含顶层行 {ids},但生效 patch 链(bundle 层 / profile 层 / home 层)没有任何一层提供这些 id:宿主会按 not found 整行丢弃,已拒绝写入,文件未改动';
200
201
  readonly 'host.patchSemanticDrift': 'patch 层 {file} 写回后结构自检不通过({detail}):已拒绝写入,文件未改动';
201
202
  readonly 'host.fsPathEscape': '路径越界: {path}';
202
203
  readonly 'host.activationRowFailed': '插件 {pkg} 已安装,但激活行写入 profile 补丁失败:{reason}(重启后该插件可能未激活;修复后重试安装即可幂等补行)';
@@ -288,6 +289,13 @@ export declare const zh: {
288
289
  readonly 'adapter.pluginInstallFailed': '插件 {name} 安装失败:{msg}。可手动安装:dsh plugin --profile {profile} add {name}(其余配置已导入;可修复依赖后在设置页重试或手动安装)';
289
290
  readonly 'adapter.pluginUpdateFailed': '插件 {name} 更新失败:{msg}。可手动安装:dsh plugin --profile {profile} add {name}(其余配置已导入;可修复依赖后在设置页重试或手动安装)';
290
291
  readonly 'adapter.patchLineCreate': '写入 patch 行 {lineId}';
292
+ readonly 'adapter.patchLineOverride': '覆盖已存在条目(写入 patch 行 {lineId})';
293
+ readonly 'adapter.patchLineOverrideDetail': '该 id 已由 {layers} 提供:只能写顶层覆盖行;写成 insert 会产生第二条同 id 条目(DSH 里保存该插件配置时会报 overridden)';
294
+ readonly 'adapter.patchLineDowngraded': '{ids} 已由其它 patch 层提供,本次改写为覆盖行(不再新增第二条同 id 条目)';
295
+ readonly 'adapter.patchChainPartial': '生效 patch 链有 {count} 层读不出来({files}):本次「这个 id 是否已存在」的判定只覆盖读到的那几层';
296
+ readonly 'adapter.patchLineDuplicateInsert': 'patch 行 {lineId} 在本机已有多条「新增」(insert)来源:同 id 两条新增会让 DSH 保存该插件配置时一直报 overridden';
297
+ readonly 'adapter.patchLineDuplicateInsertDetail': '另一条 insert 来自 {layers};需手工删除其中一条(本插件不会自动删行)';
298
+ readonly 'adapter.patchNotesJoin': ';';
291
299
  readonly 'adapter.patchLineSame': 'patch 行 {lineId} 已一致';
292
300
  readonly 'adapter.patchLineDiff': 'patch 行 {lineId} 与目标不同';
293
301
  readonly 'adapter.patchKeyParseFail': 'patch 行 {ref} 在 {path} 处含会让 DSH 整份文件解析失败的键名:先给该键改成合法名字,再导入';
@@ -382,6 +390,8 @@ export declare const zh: {
382
390
  readonly 'sync.credentialsNeedPassword': '快照 {id} 含加密凭据载荷,需要解密密码才能读取';
383
391
  readonly 'sync.encryptedNeedsPassword': '快照 {id} 已加密,需要解密密码才能读取(自动同步无法解密,请手动同步)';
384
392
  readonly 'sync.noPortableSections': '没有可同步的 portable 分区(全部导出失败)';
393
+ readonly 'sync.pushSkipConflicted': '已跳过本轮推送:{sections} 仍待冲突裁决(未上传、未改动基线)';
394
+ readonly 'sync.pushAllConflicted': '没有可推送的分区:{sections} 全部仍待冲突裁决(先裁决,或改用强制推送)';
385
395
  readonly 'sync.remoteEmpty': '远端无快照';
386
396
  readonly 'sync.sessionDeletionsRecorded': '已记录 {count} 个本机删除的历史对话(墓碑随快照传播,旧快照不会把它们带回来)';
387
397
  readonly 'sync.sessionsTombstoned': '已按远端墓碑跳过 {count} 个已删除的历史对话(不复活)';
@@ -186,6 +186,7 @@ export const zh = {
186
186
  'host.patchUnreadable': 'patch 层 {file} 读不出来({reason}):该层的 patch 行不会被导出/同步,也不会被改写',
187
187
  'host.patchRefuseClobber': 'patch 层 {file} 解析失败({reason}):已拒绝覆盖写入,否则会把这层里读不出来的行整体删掉',
188
188
  'host.patchRefusePromote': 'patch 层 {file} 的写入会把 {ids} 从 insert(新增)改成顶层行(覆盖):已拒绝写入,文件未改动(顶层行按 id 覆盖,查不到就整条丢弃)',
189
+ 'host.patchRefuseUnknownId': 'patch 层 {file} 的写入含顶层行 {ids},但生效 patch 链(bundle 层 / profile 层 / home 层)没有任何一层提供这些 id:宿主会按 not found 整行丢弃,已拒绝写入,文件未改动',
189
190
  'host.patchSemanticDrift': 'patch 层 {file} 写回后结构自检不通过({detail}):已拒绝写入,文件未改动',
190
191
  'host.fsPathEscape': '路径越界: {path}',
191
192
  'host.activationRowFailed': '插件 {pkg} 已安装,但激活行写入 profile 补丁失败:{reason}(重启后该插件可能未激活;修复后重试安装即可幂等补行)',
@@ -279,6 +280,13 @@ export const zh = {
279
280
  'adapter.pluginInstallFailed': '插件 {name} 安装失败:{msg}。可手动安装:dsh plugin --profile {profile} add {name}(其余配置已导入;可修复依赖后在设置页重试或手动安装)',
280
281
  'adapter.pluginUpdateFailed': '插件 {name} 更新失败:{msg}。可手动安装:dsh plugin --profile {profile} add {name}(其余配置已导入;可修复依赖后在设置页重试或手动安装)',
281
282
  'adapter.patchLineCreate': '写入 patch 行 {lineId}',
283
+ 'adapter.patchLineOverride': '覆盖已存在条目(写入 patch 行 {lineId})',
284
+ 'adapter.patchLineOverrideDetail': '该 id 已由 {layers} 提供:只能写顶层覆盖行;写成 insert 会产生第二条同 id 条目(DSH 里保存该插件配置时会报 overridden)',
285
+ 'adapter.patchLineDowngraded': '{ids} 已由其它 patch 层提供,本次改写为覆盖行(不再新增第二条同 id 条目)',
286
+ 'adapter.patchChainPartial': '生效 patch 链有 {count} 层读不出来({files}):本次「这个 id 是否已存在」的判定只覆盖读到的那几层',
287
+ 'adapter.patchLineDuplicateInsert': 'patch 行 {lineId} 在本机已有多条「新增」(insert)来源:同 id 两条新增会让 DSH 保存该插件配置时一直报 overridden',
288
+ 'adapter.patchLineDuplicateInsertDetail': '另一条 insert 来自 {layers};需手工删除其中一条(本插件不会自动删行)',
289
+ 'adapter.patchNotesJoin': ';',
282
290
  'adapter.patchLineSame': 'patch 行 {lineId} 已一致',
283
291
  'adapter.patchLineDiff': 'patch 行 {lineId} 与目标不同',
284
292
  'adapter.patchKeyParseFail': 'patch 行 {ref} 在 {path} 处含会让 DSH 整份文件解析失败的键名:先给该键改成合法名字,再导入',
@@ -376,6 +384,8 @@ export const zh = {
376
384
  'sync.credentialsNeedPassword': '快照 {id} 含加密凭据载荷,需要解密密码才能读取',
377
385
  'sync.encryptedNeedsPassword': '快照 {id} 已加密,需要解密密码才能读取(自动同步无法解密,请手动同步)',
378
386
  'sync.noPortableSections': '没有可同步的 portable 分区(全部导出失败)',
387
+ 'sync.pushSkipConflicted': '已跳过本轮推送:{sections} 仍待冲突裁决(未上传、未改动基线)',
388
+ 'sync.pushAllConflicted': '没有可推送的分区:{sections} 全部仍待冲突裁决(先裁决,或改用强制推送)',
379
389
  'sync.remoteEmpty': '远端无快照',
380
390
  'sync.sessionDeletionsRecorded': '已记录 {count} 个本机删除的历史对话(墓碑随快照传播,旧快照不会把它们带回来)',
381
391
  'sync.sessionsTombstoned': '已按远端墓碑跳过 {count} 个已删除的历史对话(不复活)',
@@ -708,6 +718,7 @@ export const en = {
708
718
  'host.patchUnreadable': 'Cannot read patch layer {file} ({reason}): its patch rows are not exported or synced, and the file is left untouched',
709
719
  'host.patchRefuseClobber': 'Refusing to rewrite patch layer {file}: it cannot be parsed ({reason}), and rewriting would delete every row that could not be read',
710
720
  'host.patchRefusePromote': 'Refusing to write patch layer {file}: it would turn {ids} from insert (add) into top-level rows (override); the file is untouched (top-level rows override by id and are dropped entirely when not found)',
721
+ 'host.patchRefuseUnknownId': 'Refusing to write patch layer {file}: it contains top-level rows {ids} that no layer of the effective patch chain (bundle / profile / home) provides; the host would drop them as not found, so the file is untouched',
711
722
  'host.patchSemanticDrift': 'Structure self-check failed after serializing patch layer {file} ({detail}): the write was refused and the file is untouched',
712
723
  'host.fsPathEscape': 'Path outside home directory: {path}',
713
724
  'host.activationRowFailed': 'Plugin {pkg} is installed, but writing the activation row into the profile patch failed: {reason} (the plugin may be inactive until restart; retrying the install after fixing is idempotent)',
@@ -801,6 +812,13 @@ export const en = {
801
812
  'adapter.pluginInstallFailed': 'Plugin {name} install failed: {msg}. You can install it manually: dsh plugin --profile {profile} add {name} (the rest of the config was imported; fix dependencies and retry from the settings page, or install by hand)',
802
813
  'adapter.pluginUpdateFailed': 'Plugin {name} update failed: {msg}. You can install it manually: dsh plugin --profile {profile} add {name} (the rest of the config was imported; fix dependencies and retry from the settings page, or install by hand)',
803
814
  'adapter.patchLineCreate': 'Write patch row {lineId}',
815
+ 'adapter.patchLineOverride': 'Override the existing entry (write patch row {lineId})',
816
+ 'adapter.patchLineOverrideDetail': 'This id is already provided by {layers}: only a top-level override row is valid; an insert would add a second entry with the same id (saving that plugin config in DSH then reports it as overridden)',
817
+ 'adapter.patchLineDowngraded': '{ids} already provided by another patch layer; written as an override row instead of a second insert',
818
+ 'adapter.patchChainPartial': '{count} layer(s) of the effective patch chain could not be read ({files}); the "does this id already exist" check covers only the readable layers',
819
+ 'adapter.patchLineDuplicateInsert': 'Patch line {lineId} has more than one insert source on this machine: two inserts for the same id make DSH report that plugin config as overridden on every save',
820
+ 'adapter.patchLineDuplicateInsertDetail': 'Another insert comes from {layers}; delete one of them by hand (this plugin never deletes rows on its own)',
821
+ 'adapter.patchNotesJoin': '; ',
804
822
  'adapter.patchLineSame': 'Patch row {lineId} is already identical',
805
823
  'adapter.patchLineDiff': 'Patch row {lineId} differs from the target',
806
824
  'adapter.patchKeyParseFail': 'Patch row {ref} has a key at {path} that makes DSH fail to parse the whole file; rename that key before importing',
@@ -898,6 +916,8 @@ export const en = {
898
916
  'sync.credentialsNeedPassword': 'Snapshot {id} carries an encrypted credentials payload; a decryption password is required',
899
917
  'sync.encryptedNeedsPassword': 'Snapshot {id} is encrypted; a decryption password is required (auto sync cannot decrypt — sync manually)',
900
918
  'sync.noPortableSections': 'No portable sections to sync (all exports failed)',
919
+ 'sync.pushSkipConflicted': 'Push skipped: {sections} still await conflict resolution (nothing uploaded, baseline untouched)',
920
+ 'sync.pushAllConflicted': 'Nothing to push: every target section ({sections}) still awaits conflict resolution (resolve them first, or force push)',
901
921
  'sync.remoteEmpty': 'No snapshots on the remote',
902
922
  'sync.sessionDeletionsRecorded': 'Recorded {count} locally deleted conversation(s) as tombstones (they travel with snapshots, so older snapshots will not bring them back)',
903
923
  'sync.sessionsTombstoned': 'Skipped {count} conversation(s) deleted on the remote side according to tombstones (no resurrection)',
@@ -431,6 +431,9 @@ export function createModelTools(deps) {
431
431
  ...(sections === undefined ? {} : { sections }),
432
432
  ...(encrypt ? { encrypt: true, includeSecrets: false, password: password ?? '' } : {}),
433
433
  ...(input.force === true ? { force: true } : {}),
434
+ // 空数组 = 连磁盘待裁决一起扣(issue #146):agent 的手动推送与 GUI 同口径,
435
+ // 否则「跑一次 push 工具」就能把用户挂着未裁决的分区整块覆盖掉。
436
+ skipSections: [],
434
437
  });
435
438
  const report = await runWithMutationLock(deps.host.mutationLock, { op: 'model-sync-push', isBlocked: () => deps.host.safeModeIsBlocked?.() ?? false }, async (lockCtx) => {
436
439
  if (deps.host.phase3Recovery !== undefined && lockCtx !== null) {
@@ -457,7 +460,10 @@ export function createModelTools(deps) {
457
460
  // 不透传的话模型会把「ok:true + snapshotId」读成「又推了一份新快照」。
458
461
  ...(report.skipped === 'unchanged'
459
462
  ? { skipped: 'unchanged', adoptedSnapshotId: report.adoptedSnapshotId ?? report.snapshotId }
460
- : {}),
463
+ // issue #146:目标分区全被未裁决冲突扣下 ⇒ 一个分区都没推,绝不读成「推成功」
464
+ : report.skipped === 'all-conflicted'
465
+ ? { skipped: 'all-conflicted' }
466
+ : {}),
461
467
  ...(report.message === undefined ? {} : { message: report.message }),
462
468
  ...(encrypt ? { passwordSource: stored === undefined ? 'argument' : 'stored' } : {}),
463
469
  };
@@ -0,0 +1,65 @@
1
+ /** 生效链上的一层:层身份 + 已解析的文档。 */
2
+ export interface PatchChainSource {
3
+ /**
4
+ * 层身份。前两种与 `patch-layers.ts` 同口径(相对 homeDir 的 POSIX 路径);bundle 层用
5
+ * `bundles:<包名>/<patch 文件>` —— 它不是本机可写的层,只用于判重与取 name。
6
+ */
7
+ file: string;
8
+ doc: unknown;
9
+ }
10
+ /** 读不出来的层(fail-open:判定只做加强,但缺口必须能上报,不能静默变成「确知没有」)。 */
11
+ export interface PatchChainFailure {
12
+ file: string;
13
+ reason: string;
14
+ }
15
+ /** 门面读到的整条链(宿主合并序)与读失败的层。 */
16
+ export interface PatchChainRead {
17
+ /** 宿主合并序:bundle 层在前,然后 profile 层、home 层 */
18
+ sources: PatchChainSource[];
19
+ /** 读不出来 / 解析不了的层 */
20
+ failures: PatchChainFailure[];
21
+ }
22
+ /** 链上某个 id 的合流结果。 */
23
+ export interface PatchChainEntry {
24
+ id: string;
25
+ /** 条目声明的 name(写顶层覆盖行时必须与之一致,否则宿主整行跳过;`alignOverrideNames` 用它校正) */
26
+ name?: string;
27
+ /** 提供该 id 的层(按合并序去重) */
28
+ layers: string[];
29
+ /** 其中**以 `- insert:` 成员形式**提供该 id 的层(>=2 就是「两条新增抢同一 id」的坏状态) */
30
+ insertLayers: string[];
31
+ }
32
+ /** 写回结果(`applyPatchChanges` 的返回值):被降级的 insert id。 */
33
+ export interface PatchWriteOutcome {
34
+ /**
35
+ * 本次被**降级为顶层覆盖行**的 id:调用方要求 insert,但链上已有其他层提供该 id
36
+ * (issue #144 的存量修复;正常路径在计划阶段就已判定,这里只是最后一道兜底)。
37
+ */
38
+ downgraded: string[];
39
+ }
40
+ /** 把整条链折成 id → 层/形态索引(顺序 = 宿主合并序,先出现者优先记 name)。 */
41
+ export declare function patchChainIndex(read: PatchChainRead): Map<string, PatchChainEntry>;
42
+ /** 链上「**本文件之外**的层」提供的 id(写这一层时的判据)。 */
43
+ export declare function providedByOtherLayers(index: Map<string, PatchChainEntry>, file: string): Map<string, PatchChainEntry>;
44
+ /**
45
+ * 把待写的**顶层行**里的 `name` 校正成链上既有条目的 name(issue #144 的验收标准之一)。
46
+ *
47
+ * 为什么必须校正:宿主对覆盖行做 `name === target.name` 校验,不一致就
48
+ * `patch: name mismatch … skipping` 把整行丢掉 —— 快照来自另一台机器(裸名)而目标机由
49
+ * bundle 提供(scoped 名)时,导入会报「成功」但一个字节都没生效,原报错照旧。
50
+ * `insert` 块成员不动(那是新增,name 由来源决定)。
51
+ */
52
+ export declare function alignOverrideNames(items: readonly unknown[], index: Map<string, PatchChainEntry>): unknown[];
53
+ /** 读整条链的结果:索引 + 读不出来的层(后者供报告面提示「判据不完整」)。 */
54
+ export interface PatchChainIndexRead {
55
+ /** 门面没实现该能力(老宿主 / 测试替身)→ `null`(判定退化为两层) */
56
+ index: Map<string, PatchChainEntry> | null;
57
+ failures: PatchChainFailure[];
58
+ }
59
+ /**
60
+ * 读整条链并折成索引。门面没实现 `readPatchChain`、或读链本身抛错 → `index: null`
61
+ * (判定退化为两层 patch 文件,绝不因为链读失败阻断写入)。
62
+ */
63
+ export declare function readPatchChainIndex(patchFile: {
64
+ readPatchChain?: () => Promise<PatchChainRead>;
65
+ }): Promise<PatchChainIndexRead>;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * 生效 patch 链(issue #144)。
3
+ *
4
+ * DSH 启动时把**多层** patch 合并到同一份 plugin 树上(dsh-app-boot 的 readProfilePatches):
5
+ * bundle 层(各 bundle 的 `dsh.bundle.patch`,按 `dsh.profile.bundles` 顺序)
6
+ * → profile 层(`profiles/<profile>/cordis.patch.yml`)
7
+ * → home 层(`cordis.patch.yml`)→ overlays。
8
+ *
9
+ * 同一个 id 只能有**一份**「新增」来源:两处 `- insert:` 成员会把同一条目插两次,宿主
10
+ * composeEntries 之后拿到的 config 变成数组 / 取到没有 config 的那份,于是插件自己的
11
+ * ConfigEditor 落盘时报
12
+ * `Configuration for "X" is overridden by a home patch or command-line overlay` —— 报错把原因
13
+ * 说反了(真正的原因是「我们插了第二份」,issue #144)。
14
+ *
15
+ * 于是「这个 id 存在吗」必须按**整条链**判定,而不是只看两个 patch 文件(issue #144 之前的
16
+ * 判据只覆盖 home/profile 两层,见 `patchLayerRels`):
17
+ * - 链上已存在 ⇒ 只能写**顶层覆盖行**(`- id: X`,带 config;写 name 时必须与既有条目一致,
18
+ * 否则宿主 `patch: name mismatch … skipping` 整行跳过);
19
+ * - 链上不存在 ⇒ 只能写 `- insert:` 成员(顶层行会被 `patch: entry not found` 丢弃)。
20
+ *
21
+ * 本模块只做**纯计算**(core 层零依赖、不碰 YAML / IO):各层文档由宿主门面读好交进来。
22
+ * 三个消费点共用同一份索引 —— 导入判定(计划项 Create / Update)、写前降级(insert → 覆盖行)、
23
+ * 覆盖行孤儿守卫(整链没人提供该 id ⇒ 拒写)。
24
+ */
25
+ import { patchDocumentEntries } from './patch-document.js';
26
+ /** 把整条链折成 id → 层/形态索引(顺序 = 宿主合并序,先出现者优先记 name)。 */
27
+ export function patchChainIndex(read) {
28
+ const index = new Map();
29
+ for (const source of read.sources) {
30
+ for (const entry of patchDocumentEntries(source.doc)) {
31
+ const known = index.get(entry.id);
32
+ if (known === undefined) {
33
+ const fresh = { id: entry.id, layers: [source.file], insertLayers: [] };
34
+ if (entry.name !== undefined)
35
+ fresh.name = entry.name;
36
+ if (entry.inInsert)
37
+ fresh.insertLayers.push(source.file);
38
+ index.set(entry.id, fresh);
39
+ continue;
40
+ }
41
+ if (!known.layers.includes(source.file))
42
+ known.layers.push(source.file);
43
+ if (entry.inInsert && !known.insertLayers.includes(source.file))
44
+ known.insertLayers.push(source.file);
45
+ // name 取**第一个**声明它的层:宿主按既有条目的 name 校验覆盖行,低优先级层的同名条目最终也会被覆盖
46
+ if (known.name === undefined && entry.name !== undefined)
47
+ known.name = entry.name;
48
+ }
49
+ }
50
+ return index;
51
+ }
52
+ /** 链上「**本文件之外**的层」提供的 id(写这一层时的判据)。 */
53
+ export function providedByOtherLayers(index, file) {
54
+ const out = new Map();
55
+ for (const [id, entry] of index) {
56
+ if (entry.layers.some((layer) => layer !== file))
57
+ out.set(id, entry);
58
+ }
59
+ return out;
60
+ }
61
+ /**
62
+ * 把待写的**顶层行**里的 `name` 校正成链上既有条目的 name(issue #144 的验收标准之一)。
63
+ *
64
+ * 为什么必须校正:宿主对覆盖行做 `name === target.name` 校验,不一致就
65
+ * `patch: name mismatch … skipping` 把整行丢掉 —— 快照来自另一台机器(裸名)而目标机由
66
+ * bundle 提供(scoped 名)时,导入会报「成功」但一个字节都没生效,原报错照旧。
67
+ * `insert` 块成员不动(那是新增,name 由来源决定)。
68
+ */
69
+ export function alignOverrideNames(items, index) {
70
+ return items.map((item) => {
71
+ if (item === null || typeof item !== 'object' || Array.isArray(item))
72
+ return item;
73
+ const obj = item;
74
+ if ('insert' in obj)
75
+ return item;
76
+ const id = obj['id'];
77
+ if (typeof id !== 'string' || id === '')
78
+ return item;
79
+ const wanted = index.get(id)?.name;
80
+ if (wanted === undefined || obj['name'] === wanted)
81
+ return item;
82
+ return { ...obj, name: wanted };
83
+ });
84
+ }
85
+ /**
86
+ * 读整条链并折成索引。门面没实现 `readPatchChain`、或读链本身抛错 → `index: null`
87
+ * (判定退化为两层 patch 文件,绝不因为链读失败阻断写入)。
88
+ */
89
+ export async function readPatchChainIndex(patchFile) {
90
+ if (patchFile.readPatchChain === undefined)
91
+ return { index: null, failures: [] };
92
+ try {
93
+ const read = await patchFile.readPatchChain();
94
+ return { index: patchChainIndex(read), failures: read.failures };
95
+ }
96
+ catch (err) {
97
+ return { index: null, failures: [{ file: 'patch-chain', reason: err instanceof Error ? err.message : String(err) }] };
98
+ }
99
+ }
100
+ //# sourceMappingURL=patch-chain.js.map
@@ -20,6 +20,14 @@ export interface PatchLineChange {
20
20
  lineId: string;
21
21
  raw: unknown;
22
22
  action: 'insert' | 'update' | 'remove';
23
+ /**
24
+ * 强制这一行落在**顶层**(覆盖行),即使本层还留着同名 insert 成员(issue #144 的存量修复)。
25
+ *
26
+ * 不变①的例外,只在调用方**知道该 id 已被别的层提供**时置位:此时本层的旧 insert 成员与
27
+ * 别处的 insert 是两条新增抢同一个 id(宿主 compose 后取到没有 config 的那份,插件保存配置
28
+ * 必然报 overridden)。光把 action 改成 update 不够 —— 归属按原文档决定,insert 成员会原地留下。
29
+ */
30
+ forceTopLevel?: boolean;
23
31
  }
24
32
  export interface PatchDocumentPlan {
25
33
  /** 重建后的文档条目(顺序尽量贴原文档:顶层行原位、insert 块原位、新增项追加在尾部) */
@@ -34,6 +42,21 @@ export declare class PatchPromoteRefusedError extends Error {
34
42
  readonly ids: string[];
35
43
  constructor(ids: string[]);
36
44
  }
45
+ /**
46
+ * 文档里的一条条目:id、它声明的 name、以及出现在 insert 块里还是顶层。
47
+ *
48
+ * 为什么把 name 一起读出来:写**顶层覆盖行**时宿主会校验 `name === target.name`,不一致就
49
+ * `patch: name mismatch … skipping` 整行跳过(issue #144 的条目都来自 bundle 层,name 在那边)。
50
+ */
51
+ export interface PatchDocumentEntry {
52
+ id: string;
53
+ /** 条目里声明的 name(没写就没有;覆盖行不写 name 也合法) */
54
+ name?: string;
55
+ /** true = `- insert:` 块成员(新增);false = 顶层行(覆盖) */
56
+ inInsert: boolean;
57
+ }
58
+ /** 读出文档里的全部条目(不判重、保持文档顺序)。 */
59
+ export declare function patchDocumentEntries(doc: unknown): PatchDocumentEntry[];
37
60
  /** 读出文档里的 insert 成员 id 与顶层行 id(自检与测试共用;不判重、保持文档顺序)。 */
38
61
  export declare function describePatchDocument(doc: unknown): {
39
62
  insertIds: string[];
@@ -57,9 +80,14 @@ export declare function describePatchLayoutDrift(planned: {
57
80
  topLevelIds: readonly string[];
58
81
  }): string | null;
59
82
  /**
60
- * 跨层提升检查(issue #133):`topLevelIds` 里哪些是**另一层文档**的 `- insert:` 成员。
83
+ * 覆盖行孤儿检查(issue #133 起、issue #144 改判到「整条生效链」口径)。
84
+ *
85
+ * 宿主对顶层行只做**覆盖**:`entryMap` 查不到该 id ⇒ `patch: entry %C not found` 整行丢弃。
86
+ * 所以真正值得拒绝的只有一种:**任何一层都没有提供这个 id**。旧判据反过来拒绝「别的层用
87
+ * `- insert:` 提供过它」—— 那恰恰是条目已经存在、覆盖**合法**的情形,#144 里 bundle 层提供的
88
+ * 6 个 id 正是被这条旧判据误拒的(写进去就是重复条目)。
61
89
  *
62
- * 命中就意味着「把新增改写成覆盖」——目标作用域里那条通常不存在,宿主会 `not found` 丢弃。
63
- * `otherDoc` 读不到或解析不了时调用方传 `undefined`,本函数返回空(守卫只做加强)。
90
+ * `docs` = 判定依据的层文档(生效链 + 本文件写入前的文档;读不出来的层由调用方自行丢弃 ——
91
+ * 守卫只做加强,绝不因为兄弟层损坏而永久阻塞写入)。
64
92
  */
65
- export declare function promotedTopLevelIds(otherDoc: unknown, topLevelIds: readonly string[]): string[];
93
+ export declare function unprovidedTopLevelIds(docs: readonly unknown[], topLevelIds: readonly string[]): string[];
@@ -21,25 +21,44 @@ function blockMembers(value) {
21
21
  return null;
22
22
  return insert.filter((entry) => entry !== null && typeof entry === 'object');
23
23
  }
24
- /** 读出文档里的 insert 成员 id 与顶层行 id(自检与测试共用;不判重、保持文档顺序)。 */
25
- export function describePatchDocument(doc) {
26
- const insertIds = [];
27
- const topLevelIds = [];
24
+ /** 读出文档里的全部条目(不判重、保持文档顺序)。 */
25
+ export function patchDocumentEntries(doc) {
26
+ const entries = [];
28
27
  if (!Array.isArray(doc))
29
- return { insertIds, topLevelIds };
28
+ return entries;
30
29
  for (const item of doc) {
31
30
  const members = blockMembers(item);
32
31
  if (members !== null) {
33
32
  for (const member of members) {
34
33
  const id = entryId(member);
35
- if (id !== null)
36
- insertIds.push(id);
34
+ if (id === null)
35
+ continue;
36
+ const name = member['name'];
37
+ entries.push(typeof name === 'string'
38
+ ? { id, name, inInsert: true }
39
+ : { id, inInsert: true });
37
40
  }
38
41
  continue;
39
42
  }
40
43
  const id = entryId(item);
41
- if (id !== null)
42
- topLevelIds.push(id);
44
+ if (id === null)
45
+ continue;
46
+ const name = item['name'];
47
+ entries.push(typeof name === 'string'
48
+ ? { id, name, inInsert: false }
49
+ : { id, inInsert: false });
50
+ }
51
+ return entries;
52
+ }
53
+ /** 读出文档里的 insert 成员 id 与顶层行 id(自检与测试共用;不判重、保持文档顺序)。 */
54
+ export function describePatchDocument(doc) {
55
+ const insertIds = [];
56
+ const topLevelIds = [];
57
+ for (const entry of patchDocumentEntries(doc)) {
58
+ if (entry.inInsert)
59
+ insertIds.push(entry.id);
60
+ else
61
+ topLevelIds.push(entry.id);
43
62
  }
44
63
  return { insertIds, topLevelIds };
45
64
  }
@@ -82,8 +101,11 @@ export function planPatchDocument(doc, changes) {
82
101
  continue;
83
102
  }
84
103
  const previous = state.get(change.lineId);
85
- // 归属只由**原文档**决定:更新一条既有 insert 成员不会把它搬到顶层;新增才按动作分流
86
- const inInsert = previous !== undefined ? previous.inInsert : change.action === 'insert';
104
+ // 归属默认由**原文档**决定:更新一条既有 insert 成员不会把它搬到顶层;新增才按动作分流。
105
+ // forceTopLevel 是唯一例外(#144 存量:本层旧 insert 成员 + 别层也 insert 同一 id)。
106
+ const inInsert = change.forceTopLevel === true
107
+ ? false
108
+ : previous !== undefined ? previous.inInsert : change.action === 'insert';
87
109
  state.set(change.lineId, { raw: change.raw, inInsert });
88
110
  }
89
111
  const items = [];
@@ -147,15 +169,24 @@ export function describePatchLayoutDrift(planned, observed) {
147
169
  + '; got ' + fmt('insert', observed.insertIds) + ' ' + fmt('top-level', observed.topLevelIds);
148
170
  }
149
171
  /**
150
- * 跨层提升检查(issue #133):`topLevelIds` 里哪些是**另一层文档**的 `- insert:` 成员。
172
+ * 覆盖行孤儿检查(issue #133 起、issue #144 改判到「整条生效链」口径)。
173
+ *
174
+ * 宿主对顶层行只做**覆盖**:`entryMap` 查不到该 id ⇒ `patch: entry %C not found` 整行丢弃。
175
+ * 所以真正值得拒绝的只有一种:**任何一层都没有提供这个 id**。旧判据反过来拒绝「别的层用
176
+ * `- insert:` 提供过它」—— 那恰恰是条目已经存在、覆盖**合法**的情形,#144 里 bundle 层提供的
177
+ * 6 个 id 正是被这条旧判据误拒的(写进去就是重复条目)。
151
178
  *
152
- * 命中就意味着「把新增改写成覆盖」——目标作用域里那条通常不存在,宿主会 `not found` 丢弃。
153
- * `otherDoc` 读不到或解析不了时调用方传 `undefined`,本函数返回空(守卫只做加强)。
179
+ * `docs` = 判定依据的层文档(生效链 + 本文件写入前的文档;读不出来的层由调用方自行丢弃 ——
180
+ * 守卫只做加强,绝不因为兄弟层损坏而永久阻塞写入)。
154
181
  */
155
- export function promotedTopLevelIds(otherDoc, topLevelIds) {
182
+ export function unprovidedTopLevelIds(docs, topLevelIds) {
156
183
  if (topLevelIds.length === 0)
157
184
  return [];
158
- const insertMembers = new Set(describePatchDocument(otherDoc).insertIds);
159
- return topLevelIds.filter((id) => insertMembers.has(id));
185
+ const provided = new Set();
186
+ for (const doc of docs) {
187
+ for (const entry of patchDocumentEntries(doc))
188
+ provided.add(entry.id);
189
+ }
190
+ return topLevelIds.filter((id) => !provided.has(id));
160
191
  }
161
192
  //# sourceMappingURL=patch-document.js.map
@@ -38,7 +38,7 @@ export interface PatchSecretGuardResult {
38
38
  conservative: boolean;
39
39
  }
40
40
  /** 解析扫描器路径语法(`a.b[0].c`)→ 路径段;语法不认识返回 null(宁可放过,不误判)。 */
41
- declare function parsePath(path: string): Segment[] | null;
41
+ export declare function parsePath(path: string): Segment[] | null;
42
42
  /** 路径段 → 展示用相对路径(与扫描器语法同形,如 `raw.mcp.args.token`)。 */
43
43
  declare function formatPath(segs: readonly Segment[]): string;
44
44
  export declare function applyPatchSecretGuard(input: PatchSecretGuardInput): PatchSecretGuardResult;
@@ -16,7 +16,7 @@
16
16
  * 纯函数:不读盘、不打日志、不抛错;返回值不含任何值本身(只有路径),可直接进报告。
17
17
  */
18
18
  /** 解析扫描器路径语法(`a.b[0].c`)→ 路径段;语法不认识返回 null(宁可放过,不误判)。 */
19
- function parsePath(path) {
19
+ export function parsePath(path) {
20
20
  const segs = [];
21
21
  let rest = path;
22
22
  while (rest !== '') {
@@ -9,6 +9,7 @@ import type { EncryptionInfo, Manifest, SectionId, WorkspaceRecord } from '../sc
9
9
  import type { TombstoneKind } from '../schema/tombstones.ts';
10
10
  import type { MutationLockPort } from '../utils/env-lock.ts';
11
11
  import type { RecursiveListing } from '../utils/recursive-walk.ts';
12
+ import type { PatchChainRead, PatchWriteOutcome } from './patch-chain.ts';
12
13
  import type { Logger } from '../utils/logger.ts';
13
14
  import type { MsgFunc } from './messages.ts';
14
15
  export interface ExportOptions {
@@ -405,7 +406,21 @@ export interface PatchFileFacade {
405
406
  lineId: string;
406
407
  raw: unknown;
407
408
  }[]>;
408
- applyPatchChanges(file: string, changes: PatchChange[]): Promise<void>;
409
+ /**
410
+ * 写回一层 patch 并返回本次的降级记录(issue #144)。
411
+ *
412
+ * 返回 `PatchWriteOutcome` 而不是 `void`:请求 insert、但**生效链上已有别的层**提供该 id 时,
413
+ * 门面会把它降级成顶层覆盖行(否则就是两条 insert 抢同一个 id,插件保存配置时必然报
414
+ * `overridden by a home patch or command-line overlay`)。调用方据此给用户一条 warning。
415
+ */
416
+ applyPatchChanges(file: string, changes: PatchChange[]): Promise<PatchWriteOutcome>;
417
+ /**
418
+ * 读**整条生效 patch 链**(bundle 层 + profile 层 + home 层)的文档,issue #144。
419
+ *
420
+ * 可选能力:老宿主 / 测试替身不实现 ⇒ 调用方的链判据退化为「两层 patch 文件」的旧口径
421
+ * (fail-open,绝不因为读不到 bundle 层而拒绝写入)。
422
+ */
423
+ readPatchChain?(): Promise<PatchChainRead>;
409
424
  }
410
425
  /**
411
426
  * 技能摘要(`ctx.skills.list()` 的返回项;DSH `SkillSummary` 的结构子集)。
package/lib/index.d.ts CHANGED
@@ -46,6 +46,7 @@ import { type MsgFunc } from './core/messages.ts';
46
46
  import { runDshPlugin } from './core/plugin-cli.ts';
47
47
  import { type ConfigAdapter, type CredentialsFacade, type FileSystemFacade, type HostContext, type PatchFileFacade, type PluginInfo, type PluginsFacade, type Portability, type SessionMoveResult, type SessionParentRelation, type SessionRewriteResult, type SessionStoreFacade, type SettingsFacade, type SkillStoreFacade, type WorkspaceFacade } from './core/types.ts';
48
48
  import { type SelfUpdateOutcome } from './core/self-update.ts';
49
+ import { type PatchChainRead, type PatchWriteOutcome } from './core/patch-chain.ts';
49
50
  import { type SessionMetaIndex } from './core/session-meta.ts';
50
51
  import { type MutationLockContext, type MutationLockPort } from './utils/env-lock.ts';
51
52
  import { type RecursiveListing } from './utils/recursive-walk.ts';
@@ -75,7 +76,7 @@ export declare const name = "config-manager";
75
76
  /** Services required before the engine can mount (present in every profile). */
76
77
  export declare const inject: string[];
77
78
  /** Plugin version, kept in sync with package.json ("version"). */
78
- export declare const PLUGIN_VERSION = "0.9.0";
79
+ export declare const PLUGIN_VERSION = "0.10.0";
79
80
  /**
80
81
  * Star 引导弹窗指向的 GitHub 仓库(用户引导点 Star 的目标)。
81
82
  * 与 package.json 的 repository 字段保持一致;界面不可改(硬编码,参照
@@ -573,9 +574,13 @@ export declare class DshPatchFileFacade implements PatchFileFacade {
573
574
  lineId: string;
574
575
  raw: unknown;
575
576
  action: 'insert' | 'update' | 'remove';
576
- }[]): Promise<void>;
577
- /** 读另一层并解析;读不到/解析不了 → undefined(跨层守卫 fail-open,见 promotedTopLevelIds)。 */
578
- private readPatchDocQuietly;
577
+ }[]): Promise<PatchWriteOutcome>;
578
+ /** 生效链缓存(见 `utils/patch-chain-read.ts`):一次导入逐行写 patch,不必逐行重读 30+ 个 bundle。 */
579
+ private readonly chainCache;
580
+ /** 读整条生效 patch 链(issue #144):bundle 层 → profile 层 → home 层,宿主合并序。 */
581
+ readPatchChain(): Promise<PatchChainRead>;
582
+ /** 读链失败(profile 清单整体不可读等)→ `null`:判定退化为两层,绝不阻断写入。 */
583
+ private readPatchChainQuietly;
579
584
  }
580
585
  /** File facade over $DSH_HOME, confined to the home root. */
581
586
  export declare class DshFileSystemFacade implements FileSystemFacade {