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
@@ -0,0 +1,105 @@
1
+ /**
2
+ * patch 行的「内容身份」(issue #145 门②)。
3
+ *
4
+ * 问题:导出侧 SecretScanner 会把命中字段名/值形状的叶子剥成空串并在行上记 `redactedPaths`;
5
+ * 同一行在另一台机器(或祖先副本)上是明文、且没有这个字段。于是「同一行」的 JSON 逐字比较
6
+ * 恒不相等 —— 合并钩子拿它当「两侧都改过」,每次同步都报冲突,patch 行永远合不进去(门②)。
7
+ *
8
+ * 解法:**只用于身份比较**的归一化 —— 取同一行三侧 redactedPaths 的并集,在副本上把这些叶子
9
+ * 置为 null,再按键序稳定序列化。被剥空的值与远端明文因此落在同一身份上。产物本身绝不改写:
10
+ * 合并输出仍取原对象(redactedPaths 原样保留),写回侧的 patch-secret-guard 才据此决定
11
+ * 「这一行哪些叶子不许被空串覆盖」。
12
+ *
13
+ * 纯函数、零 I/O。
14
+ */
15
+ import { parsePath } from '../core/patch-secret-guard.ts';
16
+ import type { PatchLine } from '../schema/types.ts';
17
+
18
+ type Segment = string | number;
19
+
20
+ /** 深拷贝并把路径上的叶子置为 null;路径不存在就原样返回(不猜形状)。 */
21
+ function blankAt(value: unknown, segs: readonly Segment[]): unknown {
22
+ const [head, ...rest] = segs;
23
+ if (head === undefined) return null;
24
+ if (typeof head === 'number') {
25
+ if (!Array.isArray(value)) return value;
26
+ const copy = value.slice();
27
+ copy[head] = blankAt(copy[head], rest);
28
+ return copy;
29
+ }
30
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
31
+ const copy: Record<string, unknown> = { ...(value as Record<string, unknown>) };
32
+ if (!Object.prototype.hasOwnProperty.call(copy, head)) return copy;
33
+ copy[head] = blankAt(copy[head], rest);
34
+ return copy;
35
+ }
36
+
37
+ /** 键序稳定序列化(两侧对象字面量顺序不同不算改动)。 */
38
+ function stableJson(value: unknown): string {
39
+ const sort = (v: unknown): unknown => {
40
+ if (Array.isArray(v)) return v.map(sort);
41
+ if (v === null || typeof v !== 'object') return v;
42
+ const obj = v as Record<string, unknown>;
43
+ const out: Record<string, unknown> = {};
44
+ for (const key of Object.keys(obj).sort()) out[key] = sort(obj[key]);
45
+ return out;
46
+ };
47
+ return JSON.stringify(sort(value)) ?? '';
48
+ }
49
+
50
+ /** 同一行三侧声明的剥空路径并集(行内相对,带 `raw` 前缀)。 */
51
+ export function redactedUnion(lines: readonly (PatchLine | undefined)[]): string[] {
52
+ const set = new Set<string>();
53
+ for (const line of lines) for (const path of line?.redactedPaths ?? []) set.add(path);
54
+ return [...set];
55
+ }
56
+
57
+ /**
58
+ * 路径解析缓存:路径串来自载荷(不可信、可能每行都不同),所以按条数封顶、满即整体清空
59
+ * —— 命中率靠「多数同步轮次里路径集合稳定」,不靠无限增长。纯函数,缓存不改结果。
60
+ */
61
+ const PARSE_CACHE_LIMIT = 512;
62
+ const parsedPaths = new Map<string, readonly Segment[] | null>();
63
+
64
+ function segmentsOf(path: string): readonly Segment[] | null {
65
+ if (parsedPaths.has(path)) return parsedPaths.get(path) ?? null;
66
+ const segs = parsePath(path);
67
+ if (parsedPaths.size >= PARSE_CACHE_LIMIT) parsedPaths.clear();
68
+ parsedPaths.set(path, segs);
69
+ return segs;
70
+ }
71
+
72
+ /** 一次合并里三侧共用的身份上下文:并集路径只解析一次。 */
73
+ export interface PatchIdentityContext {
74
+ /** 已解析的路径段(`raw` 前缀已剥掉,可直接作用在 `line.raw` 上)。 */
75
+ readonly segments: readonly (readonly Segment[])[];
76
+ }
77
+
78
+ /** 为同一键的三侧行准备上下文;由 `mergeKeyedList` 的 `identityContextOf` 每键调一次。 */
79
+ export function patchIdentityContext(peers: readonly (PatchLine | undefined)[]): PatchIdentityContext {
80
+ const segments: (readonly Segment[])[] = [];
81
+ for (const path of redactedUnion(peers)) {
82
+ const segs = segmentsOf(path);
83
+ if (segs === null) continue;
84
+ // 载荷路径相对整行对象(`{ file, lineId, raw }`),行内相对只需 `raw` 之后的部分。
85
+ segments.push(segs[0] === 'raw' ? segs.slice(1) : segs);
86
+ }
87
+ return { segments };
88
+ }
89
+
90
+ /**
91
+ * 一行的内容身份:file + lineId + 归一化后的 raw。
92
+ *
93
+ * 归一化只抹「三侧里有人声明过被剥空」的叶子 → 声明过的空串与明文视为同一内容;
94
+ * 两侧真写了不同的值(都不在声明里)仍然不同 —— 那才是需要人裁决的改动。
95
+ */
96
+ export function patchLineIdentity(
97
+ line: PatchLine,
98
+ peers: readonly PatchLine[],
99
+ context?: PatchIdentityContext,
100
+ ): string {
101
+ const segments = context?.segments ?? patchIdentityContext([line, ...peers]).segments;
102
+ let normalized: unknown = line.raw;
103
+ for (const segs of segments) normalized = blankAt(normalized, segs);
104
+ return [line.file ?? '', line.lineId ?? '', stableJson(normalized)].join('\u0000');
105
+ }
@@ -0,0 +1,41 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+
4
+ import { pendingMergeFor } from './pending-merge.ts';
5
+ import type { SyncState } from './sync-state.ts';
6
+ import type { SectionId } from '../schema/types.ts';
7
+
8
+ const NOW = '2026-11-15T00:00:00.000Z';
9
+ const LATER = '2026-11-15T03:00:00.000Z';
10
+
11
+ function stateWith(sections: SectionId[], remoteSnapshotId: string, since: string): SyncState {
12
+ return { version: 1, sections: {}, pendingMerge: { remoteSnapshotId, sections, since } } as unknown as SyncState;
13
+ }
14
+
15
+ /**
16
+ * issue #146 成因链(follow-up):pendingMerge 同时是「这轮还要扣下哪些分区」的事实源,
17
+ * 而每轮上报的冲突只是「本轮查出来的那一部分」—— 替换语义会让上一轮的分区凭空消失,
18
+ * 下一轮该分区若判无冲突就整分区上行(正是 #146 的插件列表被覆盖回去)。
19
+ */
20
+ test('pendingMergeFor: 同一远端快照上连续上报取并集(不同来源的扣下不得互相抹掉)', () => {
21
+ const first = pendingMergeFor(stateWith(['plugins'], 'sync-r1', NOW), 'sync-r1', ['plugins'], NOW);
22
+ assert.deepEqual(first.sections, ['plugins']);
23
+
24
+ const second = pendingMergeFor(stateWith(first.sections, 'sync-r1', first.since), 'sync-r1', ['mcp'], LATER);
25
+ assert.deepEqual(second.sections, ['plugins', 'mcp'], '上一轮的 plugins 必须留下');
26
+ assert.equal(second.since, NOW, '并集不刷新 since(界面上的「等多久」保持首次)');
27
+
28
+ const third = pendingMergeFor(stateWith(second.sections, 'sync-r1', second.since), 'sync-r1', ['plugins'], LATER);
29
+ assert.deepEqual(third.sections, ['plugins', 'mcp'], '重复上报不产生重复项');
30
+ });
31
+
32
+ test('pendingMergeFor: 远端换了一版 → 重新计时、重新计集合(旧快照的待裁决已作废)', () => {
33
+ const next = pendingMergeFor(stateWith(['plugins'], 'sync-r1', NOW), 'sync-r2', ['mcp'], LATER);
34
+ assert.deepEqual(next.sections, ['mcp'], '换快照后不继承旧集合');
35
+ assert.equal(next.since, LATER, '换快照要重新计时');
36
+ });
37
+
38
+ test('pendingMergeFor: 没有历史待裁决 → 直接用本轮集合', () => {
39
+ const fresh = pendingMergeFor({ version: 1, sections: {} } as unknown as SyncState, 'sync-r1', ['settings'], NOW);
40
+ assert.deepEqual(fresh, { remoteSnapshotId: 'sync-r1', sections: ['settings'], since: NOW });
41
+ });
@@ -18,17 +18,25 @@ export interface PendingMergeDeps {
18
18
  msg: MsgFunc
19
19
  }
20
20
 
21
- /** 组装待裁决记录:同一远端快照上的连续待裁决**保留首次时间**(界面据此讲「挂了多久」)。 */
21
+ /**
22
+ * 组装待裁决记录:同一远端快照上的连续待裁决**保留首次时间**(界面据此讲「挂了多久」)。
23
+ *
24
+ * 分区取**并集**(issue #146 follow-up):记录同时是「这轮还要扣下哪些分区」的事实源,
25
+ * 而每轮上报的冲突只是「本轮查出来的那一部分」—— 替换语义会让上一轮的分区凭空消失
26
+ * (连续两轮 [plugins] → [mcp] 后剩 [mcp],plugins 下一轮就被整分区推上去)。
27
+ * 剔除只发生在「真推上去了」(pendingMergeAfterPush / recordBaseline 的 null)。
28
+ */
22
29
  export function pendingMergeFor(
23
30
  state: SyncState,
24
31
  remoteSnapshotId: string,
25
32
  sections: SectionId[],
26
33
  nowIso: string,
27
34
  ): PendingMerge {
28
- const since = state.pendingMerge !== undefined && state.pendingMerge.remoteSnapshotId === remoteSnapshotId
29
- ? state.pendingMerge.since
30
- : nowIso;
31
- return { remoteSnapshotId, sections, since };
35
+ const prev = state.pendingMerge;
36
+ const same = prev !== undefined && prev.remoteSnapshotId === remoteSnapshotId;
37
+ const since = same ? prev.since : nowIso;
38
+ const merged = same ? [...new Set([...prev.sections, ...sections])] : sections;
39
+ return { remoteSnapshotId, sections: merged, since };
32
40
  }
33
41
 
34
42
  /**
@@ -1,18 +1,19 @@
1
1
  /**
2
- * #132 跟随远端删除插件:两个纯函数的行为规格。
2
+ * #132 / #145:跟随远端删除插件的判据 + plugins 分区的按键三方合并。
3
3
  *
4
4
  * 接缝(seam):
5
5
  * 1. planPluginRemovals —— 「哪些已装插件该被删」的唯一判据(开关、祖先、远端、本机三方);
6
- * 2. mergePluginsSection —— plugins 分区的按名三方合并(否则通用 JSON 合并对数组两侧各改即冲突,
6
+ * 2. mergePluginsSection —— plugins 分区的按键三方合并(否则通用 JSON 合并对数组两侧各改即冲突,
7
7
  * 会把「远端删 X + 本机新装 Y」误报成整分区冲突,让删除永远进不了自动应用)。
8
8
  *
9
+ * issue #145 起两者解耦:合并**恒参与**(开关不再决定钩子是否介入),开关只管「远端删了要不要真删」。
9
10
  * 两个函数都是纯函数、零 I/O:引擎与 UI 都只读它们的返回值。
10
11
  */
11
12
  import { test } from 'node:test';
12
13
  import assert from 'node:assert/strict';
13
14
 
14
15
  import { mergePluginsSection, planPluginRemovals } from './plugin-removals.ts';
15
- import type { PluginEntry, PluginsSection } from '../schema/types.ts';
16
+ import type { PatchLine, PluginEntry, PluginsSection } from '../schema/types.ts';
16
17
 
17
18
  function entry(name: string, version: string, extra: Partial<PluginEntry> = {}): PluginEntry {
18
19
  return { name, version, isBundle: false, inBundles: [], enabled: true, ...extra };
@@ -130,15 +131,24 @@ test('planPluginRemovals: 没有祖先(首次同步)→ 一律不删(无
130
131
  assert.deepEqual(plan.kept, []);
131
132
  });
132
133
 
133
- /* ------------------------------------------------------------------ 合并:按名三方 */
134
+ /* ------------------------------------------------------------------ 合并:按键三方 */
134
135
 
135
- /** 钩子只在开关打开时参与;单测里显式开启(关闭路径另有用例)。 */
136
+ /** 开关打开:远端删了、本机未改的条目会被真的删掉(删除判据用同一份开关)。 */
136
137
  const ON = { deletePlugins: true };
137
138
 
138
139
  function section(plugins: PluginEntry[], extra: Partial<PluginsSection> = {}): PluginsSection {
139
140
  return { version: 1, plugins, patch: [], ...extra };
140
141
  }
141
142
 
143
+ function line(file: string, lineId: string, raw: unknown, redactedPaths?: string[]): PatchLine {
144
+ return { file, lineId, raw, ...(redactedPaths === undefined ? {} : { redactedPaths }) };
145
+ }
146
+
147
+ function names(res: { merged: unknown } | undefined): string[] {
148
+ assert.ok(res !== undefined);
149
+ return ((res.merged as PluginsSection).plugins ?? []).map((p) => p.name);
150
+ }
151
+
142
152
  test('mergePluginsSection: 远端删 X、本机新装 Y → 合并结果只留 Y,不报冲突', () => {
143
153
  const res = mergePluginsSection(
144
154
  section([entry('alpha', '1.2.0'), entry('y', '2.0.0')]),
@@ -146,9 +156,8 @@ test('mergePluginsSection: 远端删 X、本机新装 Y → 合并结果只留 Y
146
156
  section([entry('alpha', '1.2.0')]),
147
157
  ON,
148
158
  );
149
- assert.ok(res !== undefined);
150
- assert.deepEqual(res.conflicts, []);
151
- assert.deepEqual((res.merged as PluginsSection).plugins.map((p) => p.name), ['y']);
159
+ assert.deepEqual(res?.conflicts, []);
160
+ assert.deepEqual(names(res), ['y']);
152
161
  });
153
162
 
154
163
  test('mergePluginsSection: 本机升级 X、远端删 X → modify-delete 冲突(不静默删也不静默留)', () => {
@@ -158,28 +167,22 @@ test('mergePluginsSection: 本机升级 X、远端删 X → modify-delete 冲突
158
167
  section([entry('alpha', '1.2.0')]),
159
168
  ON,
160
169
  );
161
- assert.ok(res !== undefined);
162
- assert.equal(res.conflicts.length, 1);
163
- assert.equal(res.conflicts[0]!.path, 'plugins.alpha');
164
- assert.equal(res.conflicts[0]!.reason, 'modify-delete');
170
+ assert.equal(res?.conflicts.length, 1);
171
+ assert.equal(res?.conflicts[0]!.path, 'plugins.alpha');
172
+ assert.equal(res?.conflicts[0]!.reason, 'modify-delete');
165
173
  });
166
174
 
167
175
  test('mergePluginsSection: 两侧同版本 → 保留,两侧同删 → 消失', () => {
168
- const same = mergePluginsSection(
169
- section([entry('alpha', '1.2.0')]),
170
- section([entry('alpha', '1.2.0')]),
171
- undefined,
172
- ON,
173
- );
174
- assert.deepEqual((same!.merged as PluginsSection).plugins.map((p) => p.name), ['alpha']);
176
+ const same = mergePluginsSection(section([entry('alpha', '1.2.0')]), section([entry('alpha', '1.2.0')]), undefined, ON);
177
+ assert.deepEqual(names(same), ['alpha']);
175
178
  const both = mergePluginsSection(section([]), section([]), section([entry('alpha', '1.2.0')]), ON);
176
- assert.deepEqual((both!.merged as PluginsSection).plugins, []);
179
+ assert.deepEqual(names(both), []);
177
180
  });
178
181
 
179
182
  test('mergePluginsSection: 无祖先时本机独有/远端独有都保留(不按删除解释)', () => {
180
183
  const res = mergePluginsSection(section([entry('mine', '1.0.0')]), section([entry('theirs', '1.0.0')]), undefined, ON);
181
- assert.deepEqual(res!.conflicts, []);
182
- assert.deepEqual((res!.merged as PluginsSection).plugins.map((p) => p.name).sort(), ['mine', 'theirs']);
184
+ assert.deepEqual(res?.conflicts, []);
185
+ assert.deepEqual(names(res).sort(), ['mine', 'theirs']);
183
186
  });
184
187
 
185
188
  test('mergePluginsSection: 两侧把同一插件改到不同版本 → both-modified 冲突', () => {
@@ -189,33 +192,190 @@ test('mergePluginsSection: 两侧把同一插件改到不同版本 → both-modi
189
192
  section([entry('alpha', '1.2.0')]),
190
193
  ON,
191
194
  );
192
- assert.equal(res!.conflicts.length, 1);
193
- assert.equal(res!.conflicts[0]!.reason, 'both-modified');
195
+ assert.equal(res?.conflicts.length, 1);
196
+ assert.equal(res?.conflicts[0]!.reason, 'both-modified');
194
197
  });
195
198
 
196
- test('mergePluginsSection: 插件列表以外的字段(patch 行等)两侧不必相同 —— 不同即交回通用合并', () => {
199
+ test('mergePluginsSection: 开关关着 + 远端删了本机未改的插件 → 保留(合并仍参与,绝不交回通用路径)', () => {
197
200
  const res = mergePluginsSection(
198
- section([entry('alpha', '1.2.0')], { pnpmWorkspace: 'onlyBuiltDependencies: [a]\n' }),
199
- section([entry('alpha', '1.2.0')], { pnpmWorkspace: 'onlyBuiltDependencies: [b]\n' }),
200
201
  section([entry('alpha', '1.2.0')]),
202
+ section([]),
203
+ section([entry('alpha', '1.2.0')]),
204
+ { deletePlugins: false },
205
+ );
206
+ assert.ok(res !== undefined, '关着开关也必须参与:否则 patch 行等与卸载无关的改动会整分区冲突');
207
+ assert.deepEqual(res.conflicts, []);
208
+ assert.deepEqual(names(res), ['alpha'], '包还在盘上,删了只会下一轮复活 —— 保留才是诚实结果');
209
+ });
210
+
211
+ test('mergePluginsSection: 不传钩子(缺省开关关)也能按名/按行合并', () => {
212
+ const res = mergePluginsSection(
213
+ section([entry('alpha', '1.2.0')], { patch: [line('cfg.yml', 'l1', { a: 1 })] }),
214
+ section([], { patch: [line('cfg.yml', 'l2', { b: 2 })] }),
215
+ section([entry('alpha', '1.2.0')], { patch: [line('cfg.yml', 'l1', { a: 1 })] }),
216
+ );
217
+ assert.ok(res !== undefined);
218
+ assert.deepEqual(res.conflicts, []);
219
+ assert.deepEqual(names(res), ['alpha']);
220
+ assert.deepEqual((res.merged as PluginsSection).patch.map((p) => p.lineId), ['l1', 'l2']);
221
+ });
222
+
223
+ test('mergePluginsSection: 自身插件远端删了也保留、两侧改不同版本时本机胜,且都不报冲突', () => {
224
+ const deleted = mergePluginsSection(
225
+ section([entry(SELF, '0.9.1')]),
226
+ section([]),
227
+ section([entry(SELF, '0.9.1')]),
228
+ { ...ON, selfPluginName: SELF },
229
+ );
230
+ assert.deepEqual(deleted?.conflicts, []);
231
+ assert.deepEqual(names(deleted), [SELF]);
232
+ const changed = mergePluginsSection(
233
+ section([entry(SELF, '0.9.2')]),
234
+ section([entry(SELF, '0.9.3')]),
235
+ section([entry(SELF, '0.9.1')]),
236
+ { ...ON, selfPluginName: SELF },
237
+ );
238
+ assert.deepEqual(changed?.conflicts, []);
239
+ assert.deepEqual(names(changed), [SELF]);
240
+ assert.equal(((changed!.merged as PluginsSection).plugins[0]!).version, '0.9.2');
241
+ });
242
+
243
+ test('mergePluginsSection: patch 行按 file+lineId 配对 —— 同一行两侧都改 → both-modified(路径带文件名+行号)', () => {
244
+ const res = mergePluginsSection(
245
+ section([], { patch: [line('cfg.yml', 'l1', { timeout: 10 })] }),
246
+ section([], { patch: [line('cfg.yml', 'l1', { timeout: 20 })] }),
247
+ section([], { patch: [line('cfg.yml', 'l1', { timeout: 1 })] }),
201
248
  ON,
202
249
  );
203
- assert.equal(res, undefined, 'patch/pnpmWorkspace 的口径由通用路径负责,这里不自造第二套');
250
+ assert.equal(res?.conflicts.length, 1);
251
+ assert.equal(res?.conflicts[0]!.reason, 'both-modified');
252
+ assert.equal(res?.conflicts[0]!.path, 'patch.cfg.yml#l1', '同文件多行要能分辨');
204
253
  });
205
254
 
206
- test('mergePluginsSection: 形状不认识 → undefined(畸形数据交通用路径的形状闸门)', () => {
207
- assert.equal(mergePluginsSection({ version: 2 } as unknown as PluginsSection, section([]), undefined, ON), undefined);
255
+ test('mergePluginsSection: 同 lineId 不同文件不撞键(两行都保留)', () => {
256
+ const res = mergePluginsSection(
257
+ section([], { patch: [line('a.yml', 'l1', { x: 1 })] }),
258
+ section([], { patch: [line('b.yml', 'l1', { y: 1 })] }),
259
+ undefined,
260
+ ON,
261
+ );
262
+ assert.deepEqual(res?.conflicts, []);
263
+ assert.deepEqual((res!.merged as PluginsSection).patch.map((p) => p.file).sort(), ['a.yml', 'b.yml']);
264
+ });
265
+
266
+ test('mergePluginsSection: 导出侧剥空 + redactedPaths 与远端明文是同一行(门②,不报冲突)', () => {
267
+ // 本机(导出侧):secret 被剥成空串并记进 redactedPaths;远端/祖先:明文、无该字段。
268
+ const redacted = line('cfg.yml', 'l1', { token: '', keep: 1 }, ['raw.token']);
269
+ const plain = line('cfg.yml', 'l1', { token: 'sk-live-123', keep: 1 });
270
+ const res = mergePluginsSection(section([], { patch: [redacted] }), section([], { patch: [plain] }), section([], { patch: [plain] }), ON);
271
+ assert.deepEqual(res?.conflicts, [], '被剥空的叶子按三侧 redactedPaths 并集归一化后是同一身份');
272
+ // 身份相同 → 保留本机那一行(导出侧形态):redactedPaths 原样留下,写回守卫据此换回本机明文。
273
+ const merged = (res!.merged as PluginsSection).patch[0]!;
274
+ assert.deepEqual(merged.raw, { token: '', keep: 1 });
275
+ assert.deepEqual(merged.redactedPaths, ['raw.token']);
276
+ // 归一化只抹「被声明剥空」的叶子,绝不挡真实改动:远端把 keep 改了 → 采用远端那一行。
277
+ const changed = line('cfg.yml', 'l1', { token: 'sk-live-123', keep: 2 });
278
+ const res2 = mergePluginsSection(section([], { patch: [redacted] }), section([], { patch: [changed] }), section([], { patch: [plain] }), ON);
279
+ assert.deepEqual(res2?.conflicts, []);
280
+ assert.deepEqual((res2!.merged as PluginsSection).patch[0]!.raw, { token: 'sk-live-123', keep: 2 });
281
+ });
282
+
283
+ test('mergePluginsSection: 不在 redactedPaths 里的字段真改了 → 仍然 both-modified', () => {
284
+ const redacted = line('cfg.yml', 'l1', { token: '', keep: 2 }, ['raw.token']);
285
+ const remote = line('cfg.yml', 'l1', { token: 'sk-other', keep: 3 });
286
+ const res = mergePluginsSection(
287
+ section([], { patch: [redacted] }),
288
+ section([], { patch: [remote] }),
289
+ section([], { patch: [line('cfg.yml', 'l1', { token: 'sk-base', keep: 1 })] }),
290
+ ON,
291
+ );
292
+ assert.equal(res?.conflicts.length, 1);
293
+ assert.equal(res?.conflicts[0]!.reason, 'both-modified');
208
294
  });
209
295
 
210
- test('mergePluginsSection: 开关关着 → 钩子不参与(undefined = 通用合并,行为与 #132 之前逐字一致)', () => {
211
- assert.equal(
212
- mergePluginsSection(
213
- section([entry('alpha', '1.2.0')]),
214
- section([]),
215
- section([entry('alpha', '1.2.0')]),
216
- { deletePlugins: false },
217
- ),
296
+ test('mergePluginsSection: 合并输出保留本机行的 redactedPaths(写回守卫要靠它)', () => {
297
+ const redacted = line('cfg.yml', 'l1', { token: '', keep: 1 }, ['raw.token']);
298
+ const res = mergePluginsSection(
299
+ section([], { patch: [redacted] }),
300
+ section([], { patch: [line('cfg.yml', 'l1', { token: '', keep: 1 }, ['raw.token']), line('cfg.yml', 'l2', { b: 2 })] }),
218
301
  undefined,
219
- '开关关闭时绝不能丢掉本机条目:包还在盘上,下一轮导出会把它当本机独有推回远端 = 复活',
302
+ ON,
303
+ );
304
+ assert.deepEqual(res?.conflicts, []);
305
+ const kept = (res!.merged as PluginsSection).patch.find((p) => p.lineId === 'l1')!;
306
+ assert.deepEqual(kept.redactedPaths, ['raw.token']);
307
+ assert.equal((res!.merged as PluginsSection).patch.length, 2);
308
+ });
309
+
310
+ test('mergePluginsSection: pnpmWorkspace 单侧改动取该侧,两侧都改 → overlap 冲突(不猜用哪一份)', () => {
311
+ const oneSide = mergePluginsSection(
312
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [a]\n' }),
313
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [b]\n' }),
314
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [a]\n' }),
315
+ ON,
316
+ );
317
+ assert.deepEqual(oneSide?.conflicts, []);
318
+ assert.equal((oneSide!.merged as PluginsSection).pnpmWorkspace, 'onlyBuiltDependencies: [b]\n');
319
+ const both = mergePluginsSection(
320
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [b]\n' }),
321
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [c]\n' }),
322
+ section([], { pnpmWorkspace: 'onlyBuiltDependencies: [a]\n' }),
323
+ ON,
324
+ );
325
+ assert.equal(both?.conflicts.length, 1);
326
+ assert.equal(both?.conflicts[0]!.path, 'pnpmWorkspace');
327
+ assert.equal(both?.conflicts[0]!.reason, 'overlap');
328
+ });
329
+
330
+ test('mergePluginsSection: 三侧 pnpmWorkspace 都是 null → 原样保留(不抹成缺键,复审 P0-1)', () => {
331
+ const res = mergePluginsSection(section([], { pnpmWorkspace: null }), section([], { pnpmWorkspace: null }), section([], { pnpmWorkspace: null }), ON);
332
+ assert.deepEqual(res?.conflicts, []);
333
+ assert.ok('pnpmWorkspace' in (res!.merged as PluginsSection), 'null 是合法值,删掉会让合并结果与两侧都不等 ⇒ 每轮假合并');
334
+ assert.equal((res!.merged as PluginsSection).pnpmWorkspace, null);
335
+ assert.equal(res?.mergePoints, 0);
336
+ });
337
+
338
+ test('mergePluginsSection: 本机关掉插件、远端升版本 → 版本听远端,启停留本机(复审 P0-2)', () => {
339
+ const res = mergePluginsSection(
340
+ section([entry('alpha', '1.2.0', { enabled: false })]),
341
+ section([entry('alpha', '1.3.0')]),
342
+ section([entry('alpha', '1.2.0')]),
343
+ ON,
220
344
  );
345
+ assert.deepEqual(res?.conflicts, []);
346
+ const merged = (res!.merged as PluginsSection).plugins[0]!;
347
+ assert.equal(merged.version, '1.3.0');
348
+ assert.equal(merged.enabled, false, '本机停用是用户意图,不能被远端版本覆盖');
349
+ });
350
+
351
+ test('mergePluginsSection: 开关打开时远端删 patch 行也不跟删(开关只管插件,复审 P1-1)', () => {
352
+ const kept = line('cfg.yml', 'l1', { a: 1 });
353
+ const res = mergePluginsSection(section([], { patch: [kept] }), section([]), section([], { patch: [kept] }), ON);
354
+ assert.deepEqual(res?.conflicts, []);
355
+ assert.deepEqual((res!.merged as PluginsSection).patch.map((p) => p.lineId), ['l1']);
356
+ });
357
+
358
+ test('mergePluginsSection: patch 行重复 file+lineId → 整轮交回通用路径(不静默吞条目,复审 P1-2)', () => {
359
+ const dup = [line('cfg.yml', 'l1', { a: 1 }), line('cfg.yml', 'l1', { a: 2 })];
360
+ assert.equal(mergePluginsSection(section([], { patch: dup }), section([], { patch: dup }), undefined, ON), undefined);
361
+ });
362
+
363
+ test('mergePluginsSection: patch 冲突路径带 lineId(同文件两行能分辨,复审 P2)', () => {
364
+ const res = mergePluginsSection(
365
+ section([], { patch: [line('cfg.yml', 'l7', { a: 2 })] }),
366
+ section([], { patch: [line('cfg.yml', 'l7', { a: 3 })] }),
367
+ section([], { patch: [line('cfg.yml', 'l7', { a: 1 })] }),
368
+ ON,
369
+ );
370
+ assert.equal(res?.conflicts[0]!.path, 'patch.cfg.yml#l7');
371
+ });
372
+
373
+ test('mergePluginsSection: 远端独有的未知字段被采纳(不被本机形状吞掉,复审 P2)', () => {
374
+ const remote = { ...section([]), pluginsMeta: { scanned: true } } as unknown as PluginsSection;
375
+ const res = mergePluginsSection(section([]), remote, undefined, ON);
376
+ assert.deepEqual((res!.merged as unknown as { pluginsMeta: unknown }).pluginsMeta, { scanned: true });
377
+ });
378
+
379
+ test('mergePluginsSection: 形状不认识 → undefined(畸形数据交通用路径的形状闸门)', () => {
380
+ assert.equal(mergePluginsSection({ version: 2 } as unknown as PluginsSection, section([]), undefined, ON), undefined);
221
381
  });