dsh-config-manager 0.6.0 → 0.8.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 (112) hide show
  1. package/lib/adapters/test-helpers.d.ts +7 -0
  2. package/lib/adapters/test-helpers.js +11 -0
  3. package/lib/cli/actions.js +1 -1
  4. package/lib/client.d.ts +14 -2
  5. package/lib/client.js +722 -647
  6. package/lib/core/backup.d.ts +9 -1
  7. package/lib/core/backup.js +21 -0
  8. package/lib/core/local-plugin-host.d.ts +9 -0
  9. package/lib/core/local-plugin-host.js +174 -1
  10. package/lib/core/messages-core.d.ts +3 -0
  11. package/lib/core/messages-core.js +6 -0
  12. package/lib/core/messages.d.ts +8 -0
  13. package/lib/core/messages.js +18 -0
  14. package/lib/core/model-tools.js +2 -0
  15. package/lib/core/phase4-crash-child.js +1 -1
  16. package/lib/core/plugin-cli.d.ts +11 -0
  17. package/lib/core/plugin-cli.js +30 -0
  18. package/lib/core/restore-wal.d.ts +2 -0
  19. package/lib/core/restore-wal.js +11 -6
  20. package/lib/core/restore.d.ts +15 -2
  21. package/lib/core/restore.js +65 -3
  22. package/lib/core/run-registry.d.ts +10 -0
  23. package/lib/core/run-registry.js +17 -0
  24. package/lib/core/snapshot-diff.js +2 -1
  25. package/lib/core/types.d.ts +32 -0
  26. package/lib/index.d.ts +8 -2
  27. package/lib/index.js +24 -21
  28. package/lib/routes/import.js +6 -1
  29. package/lib/routes/route-support.d.ts +7 -1
  30. package/lib/routes/route-support.js +36 -5
  31. package/lib/routes/sync.js +8 -2
  32. package/lib/sync/autosync-config.d.ts +9 -1
  33. package/lib/sync/autosync-config.js +10 -2
  34. package/lib/sync/autosync-scheduler.js +18 -6
  35. package/lib/sync/git/git-transport.d.ts +31 -1
  36. package/lib/sync/git/git-transport.js +119 -23
  37. package/lib/sync/merge-preview-view.d.ts +78 -0
  38. package/lib/sync/merge-preview-view.js +46 -0
  39. package/lib/sync/merge.d.ts +2 -1
  40. package/lib/sync/merge.js +9 -4
  41. package/lib/sync/plugin-removal-apply.d.ts +95 -0
  42. package/lib/sync/plugin-removal-apply.js +158 -0
  43. package/lib/sync/plugin-removals.d.ts +90 -0
  44. package/lib/sync/plugin-removals.js +167 -0
  45. package/lib/sync/sync-config.d.ts +34 -3
  46. package/lib/sync/sync-config.js +38 -5
  47. package/lib/sync/sync-engine.d.ts +34 -43
  48. package/lib/sync/sync-engine.js +50 -42
  49. package/lib/sync/sync-selection.d.ts +8 -0
  50. package/lib/sync/sync-selection.js +6 -0
  51. package/lib/sync/workspace-merge.d.ts +8 -1
  52. package/lib/ui/restore-plan-view.js +1 -0
  53. package/lib/ui/sync-settings-view.d.ts +14 -0
  54. package/lib/ui/sync-settings-view.js +5 -0
  55. package/package.json +1 -1
  56. package/src/adapters/test-helpers.ts +10 -0
  57. package/src/cli/actions.ts +1 -1
  58. package/src/client/common/info-hint-guard.test.ts +2 -1
  59. package/src/client/run-store.test.ts +8 -6
  60. package/src/client/run-store.ts +9 -5
  61. package/src/client/sync/ChannelConfigDialog.tsx +15 -1
  62. package/src/client/sync/SyncPanel.tsx +16 -0
  63. package/src/client/sync/SyncSectionPickerDialog.tsx +17 -1
  64. package/src/client/sync/sync-api.ts +2 -0
  65. package/src/client/sync/sync-locales.ts +10 -0
  66. package/src/client/sync/sync-view.ts +6 -0
  67. package/src/core/backup.ts +21 -1
  68. package/src/core/local-plugin-host.test.ts +31 -2
  69. package/src/core/local-plugin-host.ts +175 -2
  70. package/src/core/messages-core.ts +6 -0
  71. package/src/core/messages.ts +18 -0
  72. package/src/core/model-tools.ts +2 -0
  73. package/src/core/phase4-crash-child.ts +1 -1
  74. package/src/core/plugin-cli.ts +37 -0
  75. package/src/core/restore-plugin-install.test.ts +116 -0
  76. package/src/core/restore-wal.ts +12 -6
  77. package/src/core/restore.ts +79 -4
  78. package/src/core/run-registry-runs.test.ts +26 -1
  79. package/src/core/run-registry.ts +18 -0
  80. package/src/core/smoke.test.ts +5 -0
  81. package/src/core/snapshot-diff.test.ts +1 -1
  82. package/src/core/snapshot-diff.ts +2 -1
  83. package/src/core/types.ts +31 -0
  84. package/src/index.sync.test.ts +27 -0
  85. package/src/index.ts +30 -28
  86. package/src/routes/import.ts +6 -1
  87. package/src/routes/route-support.ts +34 -5
  88. package/src/routes/sync.ts +8 -2
  89. package/src/security/security.test.ts +1 -0
  90. package/src/sync/autosync-config.ts +10 -2
  91. package/src/sync/autosync-scheduler.test.ts +61 -1
  92. package/src/sync/autosync-scheduler.ts +17 -5
  93. package/src/sync/git/git-transport.test.ts +117 -0
  94. package/src/sync/git/git-transport.ts +131 -23
  95. package/src/sync/merge-preview-view.ts +97 -0
  96. package/src/sync/merge.test.ts +17 -15
  97. package/src/sync/merge.ts +10 -3
  98. package/src/sync/plugin-removal-apply.ts +198 -0
  99. package/src/sync/plugin-removals.test.ts +221 -0
  100. package/src/sync/plugin-removals.ts +208 -0
  101. package/src/sync/sync-config.test.ts +59 -1
  102. package/src/sync/sync-config.ts +51 -5
  103. package/src/sync/sync-engine.test.ts +251 -1
  104. package/src/sync/sync-engine.ts +84 -83
  105. package/src/sync/sync-selection.test.ts +29 -7
  106. package/src/sync/sync-selection.ts +13 -0
  107. package/src/sync/workspace-merge.ts +9 -0
  108. package/src/ui/restore-plan-view.test.ts +1 -1
  109. package/src/ui/restore-plan-view.ts +1 -0
  110. package/src/ui/snapshots-view.test.ts +1 -1
  111. package/src/ui/sync-settings-view.test.ts +21 -0
  112. package/src/ui/sync-settings-view.ts +19 -0
@@ -0,0 +1,95 @@
1
+ import type { MsgFunc } from '../core/messages.ts';
2
+ import type { SnapshotPluginRemoval } from '../core/types.ts';
3
+ import type { SectionData, SectionId } from '../schema/types.ts';
4
+ import type { PluginRemovalEntry, PluginRemovalPlan } from './plugin-removals.ts';
5
+ /** 一条插件卸载结果(reinstallTarget 供 undo 与人工补装使用)。 */
6
+ export interface PluginRemovalReportItem {
7
+ name: string;
8
+ version: string;
9
+ /** 重装目标:`name@version`(版本未知时退回裸包名) */
10
+ reinstallTarget: string;
11
+ ok: boolean;
12
+ /** 失败原因(已由 CLI 分类器给出可操作文案);ok=true 时缺省 */
13
+ message?: string;
14
+ }
15
+ /** 预览侧的删除视图(确认页据此单列「将删除:X@1.2.3」,用户可整轮取消)。 */
16
+ export interface PluginRemovalPreviewView {
17
+ pluginRemovals: {
18
+ name: string;
19
+ version: string;
20
+ }[];
21
+ pluginRemovalConflicts: {
22
+ name: string;
23
+ ancestorVersion: string;
24
+ localVersion: string;
25
+ }[];
26
+ pluginRemovalSkipped: {
27
+ name: string;
28
+ reason: string;
29
+ }[];
30
+ }
31
+ /** 判据输入侧的「已装插件」最小形状(PluginInfo 与 PluginEntry 都能直接传)。 */
32
+ type InstalledPlugin = {
33
+ name: string;
34
+ version: string;
35
+ enabled?: boolean;
36
+ spec?: string;
37
+ bundlesOnly?: boolean;
38
+ };
39
+ /**
40
+ * 把某个分区载荷里的插件清单抽成判据要的形状(不认识 = undefined)。
41
+ *
42
+ * 为什么直接从「本轮合并的三方输入」读而不是再 export 一次:那三个载荷就是本轮合并的真实输入,
43
+ * 再 export 会拿到「已经写了一半」的状态,判据与合并结论就会打架。
44
+ */
45
+ export declare function pluginEntriesOf(data: SectionData | undefined): PluginRemovalEntry[] | undefined;
46
+ /** 三方输入 + 本机已装 → 删除计划(预览与执行共用同一份判据)。 */
47
+ export declare function pluginRemovalPlanOf(args: {
48
+ ancestor?: SectionData;
49
+ remote?: SectionData;
50
+ installed: readonly InstalledPlugin[];
51
+ enabled: boolean;
52
+ selfName?: string;
53
+ }): PluginRemovalPlan;
54
+ /**
55
+ * 执行删除(逐项失败不阻断)。
56
+ *
57
+ * 为什么只在 plugins 分区真的被应用时才动:分区冲突/跳过 = 本轮还没确定「远端删了它」,
58
+ * 此时删本机插件等于拿未裁决的远端差异动用户机器。
59
+ */
60
+ export declare function applyPluginRemovalStep(args: {
61
+ appliedIds: readonly SectionId[];
62
+ plan: PluginRemovalPlan | undefined;
63
+ enabled: boolean;
64
+ uninstall: (name: string) => Promise<unknown>;
65
+ msg: MsgFunc;
66
+ }): Promise<{
67
+ removals: PluginRemovalReportItem[];
68
+ warnings: string[];
69
+ }>;
70
+ /**
71
+ * 把「本轮删掉哪些插件」写进本次回滚快照(undo 线索)。
72
+ *
73
+ * 写不进去不中止同步(分区已落地且已重导出),但必须留一条可见告警 ——
74
+ * 报告里的 reinstallTarget 仍可人工执行,静默丢失 undo 线索才是真问题。
75
+ */
76
+ export declare function recordPluginRemovalUndo(args: {
77
+ removals: readonly PluginRemovalReportItem[];
78
+ snapshotId: string;
79
+ record?: (id: string, entries: SnapshotPluginRemoval[]) => Promise<void>;
80
+ msg: MsgFunc;
81
+ }): Promise<string[]>;
82
+ /**
83
+ * 预览侧的删除视图:确认页必须能单列「将删除:X@1.2.3」让用户整轮取消,
84
+ * 也必须如实说明「开关关着,所以没删」与「远端删了但本机改过」两种不删的原因。
85
+ *
86
+ * plugins 分区本轮不可应用(conflict / skip)时不列删除项:那时「远端删了它」还没被用户确认。
87
+ */
88
+ export declare function pluginRemovalPreviewOf(args: {
89
+ plan: PluginRemovalPlan | undefined;
90
+ sections: readonly {
91
+ id: SectionId;
92
+ decision: string;
93
+ }[];
94
+ }): PluginRemovalPreviewView | undefined;
95
+ export {};
@@ -0,0 +1,158 @@
1
+ /**
2
+ * issue #132:跟随远端删除插件的**编排层**(判据 → 卸载 → 告警 → undo 线索)。
3
+ *
4
+ * 为什么从 sync-engine 下沉:引擎已顶到文件体量棘轮(tests/file-size-ratchet.test.ts),
5
+ * 而这部分只依赖「三方载荷 + 本机已装清单 + 卸载函数 + msg」,不碰引擎内部状态;
6
+ * 独立成模块后可单独测,也不会把引擎继续堆高。判据本体仍留在 ./plugin-removals.ts(纯函数、零 IO)。
7
+ *
8
+ * 三条语义在此定型,改之前先读 issue #132 的裁决:
9
+ * 1. 开关关闭 = **不删但也绝不装作没有候选**:逐项发 warning 说明「开着会删谁」;
10
+ * 2. 卸载失败(含 desktop 保留档案被 CLI 拒)逐项进报告 + warning,不阻断其余项、不回滚整轮;
11
+ * 3. undo 线索只写「真的删成功」的那些,写不进去只告警(报告里的 reinstallTarget 仍是人工兜底)。
12
+ */
13
+ import { registerMessageCatalog } from '../core/engine-locale.js';
14
+ import { pluginInstallTarget } from '../core/plugin-cli.js';
15
+ import { planPluginRemovals } from './plugin-removals.js';
16
+ const zh = {
17
+ 'sync.pluginRemoveDone': '已跟随远端删除插件 {name}@{version}(可随时用 "{name}@{version}" 重装)',
18
+ 'sync.pluginRemoveFailed': '删除插件 {name} 失败:{reason}(其余项继续,失败项下一轮会重新判定)',
19
+ 'sync.pluginRemoveConflict': '插件 {name} 远端已删除,但本机改过(本机 {local},共同祖先 {ancestor}):未删除,请人工确认',
20
+ 'sync.pluginRemoveSkipped': '插件 {name} 在远端已删除,但"跟随删除插件"未开启:已保留(开启后会删除)',
21
+ 'sync.pluginRemoveUndoUnrecorded': '插件已经删除,但没能写进本次回滚快照的 undo 记录({names}):恢复这份快照不会自动装回它们,需要时请手动执行 dsh plugin add',
22
+ };
23
+ const en = {
24
+ 'sync.pluginRemoveDone': 'Removed plugin {name}@{version} to follow the remote (reinstall anytime with "{name}@{version}")',
25
+ 'sync.pluginRemoveFailed': 'Could not remove plugin {name}: {reason} (the remaining items continue; this one is re-evaluated next round)',
26
+ 'sync.pluginRemoveConflict': 'Plugin {name} was deleted remotely but changed locally (local {local}, common ancestor {ancestor}): not removed, needs a human decision',
27
+ 'sync.pluginRemoveSkipped': 'Plugin {name} was deleted remotely, but "delete plugins to follow the remote" is off: kept (it will be removed once enabled)',
28
+ 'sync.pluginRemoveUndoUnrecorded': 'The plugin(s) were removed, but the undo record could not be written into this rollback snapshot ({names}): restoring that snapshot will not reinstall them — run dsh plugin add manually if needed',
29
+ };
30
+ registerMessageCatalog({ id: 'sync-plugin-removal', zh, en });
31
+ /**
32
+ * 把某个分区载荷里的插件清单抽成判据要的形状(不认识 = undefined)。
33
+ *
34
+ * 为什么直接从「本轮合并的三方输入」读而不是再 export 一次:那三个载荷就是本轮合并的真实输入,
35
+ * 再 export 会拿到「已经写了一半」的状态,判据与合并结论就会打架。
36
+ */
37
+ export function pluginEntriesOf(data) {
38
+ if (data === null || typeof data !== 'object')
39
+ return undefined;
40
+ const obj = data;
41
+ if (obj.version !== 1 || !Array.isArray(obj.plugins))
42
+ return undefined;
43
+ const out = [];
44
+ for (const raw of obj.plugins) {
45
+ if (raw === null || typeof raw !== 'object')
46
+ continue;
47
+ const p = raw;
48
+ if (typeof p.name !== 'string' || p.name === '')
49
+ continue;
50
+ out.push({
51
+ name: p.name,
52
+ version: typeof p.version === 'string' ? p.version : '',
53
+ ...(typeof p.spec === 'string' ? { spec: p.spec } : {}),
54
+ ...(p.bundlesOnly === true ? { bundlesOnly: true } : {}),
55
+ });
56
+ }
57
+ return out;
58
+ }
59
+ /** 三方输入 + 本机已装 → 删除计划(预览与执行共用同一份判据)。 */
60
+ export function pluginRemovalPlanOf(args) {
61
+ return planPluginRemovals({
62
+ enabled: args.enabled,
63
+ ancestor: pluginEntriesOf(args.ancestor),
64
+ remote: pluginEntriesOf(args.remote),
65
+ local: args.installed,
66
+ selfName: args.selfName ?? '',
67
+ });
68
+ }
69
+ /**
70
+ * 执行删除(逐项失败不阻断)。
71
+ *
72
+ * 为什么只在 plugins 分区真的被应用时才动:分区冲突/跳过 = 本轮还没确定「远端删了它」,
73
+ * 此时删本机插件等于拿未裁决的远端差异动用户机器。
74
+ */
75
+ export async function applyPluginRemovalStep(args) {
76
+ const removals = [];
77
+ const warnings = [];
78
+ if (!args.appliedIds.includes('plugins'))
79
+ return { removals, warnings };
80
+ const plan = args.plan;
81
+ if (plan === undefined)
82
+ return { removals, warnings };
83
+ for (const c of plan.conflicts) {
84
+ warnings.push(args.msg('sync.pluginRemoveConflict', {
85
+ name: c.name, local: c.localVersion, ancestor: c.ancestorVersion,
86
+ }));
87
+ }
88
+ if (!args.enabled) {
89
+ // 开关关:把「打开后会删什么」如实说出来(不删,但也绝不装作没有候选)
90
+ for (const k of plan.kept) {
91
+ if (k.reason === 'switch-off')
92
+ warnings.push(args.msg('sync.pluginRemoveSkipped', { name: k.name }));
93
+ }
94
+ return { removals, warnings };
95
+ }
96
+ for (const r of plan.removals) {
97
+ // 重装目标与导入侧同一决策(core/plugin-cli.ts 的 pluginInstallTarget):github:/git+/file:/link:
98
+ // 这类来源只靠 name@version 装不回同一份代码,必须带祖先记录的 spec。
99
+ const reinstallTarget = pluginInstallTarget(r.name, {
100
+ ...(r.version === '' ? {} : { version: r.version }),
101
+ ...(r.spec === undefined ? {} : { spec: r.spec }),
102
+ });
103
+ try {
104
+ await args.uninstall(r.name);
105
+ removals.push({ name: r.name, version: r.version, reinstallTarget, ok: true });
106
+ warnings.push(args.msg('sync.pluginRemoveDone', { name: r.name, version: r.version }));
107
+ }
108
+ catch (error) {
109
+ const reason = error instanceof Error ? error.message : String(error);
110
+ removals.push({ name: r.name, version: r.version, reinstallTarget, ok: false, message: reason });
111
+ warnings.push(args.msg('sync.pluginRemoveFailed', { name: r.name, reason }));
112
+ }
113
+ }
114
+ return { removals, warnings };
115
+ }
116
+ /**
117
+ * 把「本轮删掉哪些插件」写进本次回滚快照(undo 线索)。
118
+ *
119
+ * 写不进去不中止同步(分区已落地且已重导出),但必须留一条可见告警 ——
120
+ * 报告里的 reinstallTarget 仍可人工执行,静默丢失 undo 线索才是真问题。
121
+ */
122
+ export async function recordPluginRemovalUndo(args) {
123
+ const removed = args.removals.filter((r) => r.ok);
124
+ if (removed.length === 0)
125
+ return [];
126
+ const names = removed.map((r) => r.reinstallTarget).join(', ');
127
+ const entries = removed.map((r) => ({ name: r.name, version: r.version, target: r.reinstallTarget }));
128
+ if (args.record === undefined)
129
+ return [args.msg('sync.pluginRemoveUndoUnrecorded', { names })];
130
+ try {
131
+ await args.record(args.snapshotId, entries);
132
+ return [];
133
+ }
134
+ catch (error) {
135
+ const reason = error instanceof Error ? error.message : String(error);
136
+ return [args.msg('sync.pluginRemoveUndoUnrecorded', { names }), reason];
137
+ }
138
+ }
139
+ /**
140
+ * 预览侧的删除视图:确认页必须能单列「将删除:X@1.2.3」让用户整轮取消,
141
+ * 也必须如实说明「开关关着,所以没删」与「远端删了但本机改过」两种不删的原因。
142
+ *
143
+ * plugins 分区本轮不可应用(conflict / skip)时不列删除项:那时「远端删了它」还没被用户确认。
144
+ */
145
+ export function pluginRemovalPreviewOf(args) {
146
+ if (args.plan === undefined)
147
+ return undefined;
148
+ const section = args.sections.find((s) => s.id === 'plugins');
149
+ const applied = section !== undefined && section.decision !== 'conflict' && section.decision !== 'skip';
150
+ return {
151
+ pluginRemovals: applied ? args.plan.removals.map((r) => ({ name: r.name, version: r.version })) : [],
152
+ pluginRemovalConflicts: args.plan.conflicts.map((c) => ({
153
+ name: c.name, ancestorVersion: c.ancestorVersion, localVersion: c.localVersion,
154
+ })),
155
+ pluginRemovalSkipped: args.plan.kept.map((k) => ({ name: k.name, reason: k.reason })),
156
+ };
157
+ }
158
+ //# sourceMappingURL=plugin-removal-apply.js.map
@@ -0,0 +1,90 @@
1
+ /**
2
+ * #132:同步「跟随远端删除插件」的**唯一判据**与 plugins 分区的按名三方合并。
3
+ *
4
+ * 为什么必须单独一个模块:
5
+ * 1. 删除判定是纯逻辑(开关 × 祖先 × 远端 × 本机),散进引擎里既测不了也说不清;
6
+ * 2. plugins 分区不能走通用 JSON 合并 —— `merge.ts` 的 `mergeJsonValues` 对数组一律「两侧都改
7
+ * 且不逐字相等 → 冲突」,而本分区的日常形态恰恰是「A 机卸载了 X、B 机新装了 Y」,通用路径
8
+ * 会把它报成整分区冲突,于是删除永远进不了自动应用(issue #132 的行为表 A→B 走不通)。
9
+ *
10
+ * 删除判据(缺一不可,全部满足才删):
11
+ * - 开关打开(`SyncSelection.deletePlugins`,缺省关);
12
+ * - **祖先**里有它(A 机那轮同步见过的状态)——没有祖先就没有「谁删了它」的证据;
13
+ * - **远端**没有它(= 有人在另一台机器上卸载了);
14
+ * - **本机装着**(卸载才有意义);
15
+ * - 本机版本**仍等于祖先版本**(本地改过 = 用户在自己这台机器上动过它,绝不替用户做决定,
16
+ * 报 `version-changed` 冲突交人裁决);
17
+ * - 不是本插件自己、不是 bundlesOnly 条目(后者由 DSH 自身安装位置解析,`dsh plugin remove`
18
+ * 对它无意义,见 `core/plugin-cli.ts` 的 `isBundlesOnlyEntry`)。
19
+ *
20
+ * 纯函数、零 I/O:引擎调用它拿计划,UI 调用它拿「将删除」清单。
21
+ */
22
+ import type { SectionData } from '../schema/types.ts';
23
+ import type { GranularMergeOutcome } from './workspace-merge.ts';
24
+ /** 一条「该卸载」的结论(version = 祖先/本机一致的版本,用于报告与回滚重装目标)。 */
25
+ export interface PluginRemoval {
26
+ name: string;
27
+ version: string;
28
+ /** 祖先条目里的安装源(github:/git+/file:/link: 等);重装目标要用它,否则装不回同一来源 */
29
+ spec?: string;
30
+ }
31
+ /** 未删的原因:每一条都要能对用户解释(报告里逐项列出,绝不静默跳过)。 */
32
+ export type PluginRemovalKeptReason = 'still-in-remote' | 'self' | 'bundles-only' | 'not-installed' | 'switch-off';
33
+ export interface PluginRemovalConflict {
34
+ name: string;
35
+ reason: 'version-changed';
36
+ ancestorVersion: string;
37
+ localVersion: string;
38
+ }
39
+ export interface PluginRemovalPlan {
40
+ /** 该执行 `dsh plugin remove` 的插件(顺序 = 祖先顺序,便于报告比对) */
41
+ removals: PluginRemoval[];
42
+ /** 本机改过、远端删了 → 交人裁决(不删也不静默保留) */
43
+ conflicts: PluginRemovalConflict[];
44
+ /** 考察过但不删的条目及原因 */
45
+ kept: {
46
+ name: string;
47
+ reason: PluginRemovalKeptReason;
48
+ }[];
49
+ }
50
+ /**
51
+ * 判据只需要这几个字段:`PluginEntry` 与宿主的 `PluginInfo`(listInstalled 的返回值)都能直接传入,
52
+ * 不必为了调用它先拼一个完整的分区条目。
53
+ */
54
+ export interface PluginRemovalEntry {
55
+ name: string;
56
+ version: string;
57
+ spec?: string;
58
+ bundlesOnly?: boolean;
59
+ }
60
+ export interface PluginRemovalInput {
61
+ /** 同步选择里的开关(缺省关);关 = 一个都不删 */
62
+ enabled: boolean;
63
+ /** 共同祖先快照里的插件清单(undefined = 没有祖先副本) */
64
+ ancestor: readonly PluginRemovalEntry[] | undefined;
65
+ /** 远端快照里的插件清单(undefined = 远端没有该分区) */
66
+ remote: readonly PluginRemovalEntry[] | undefined;
67
+ /** 本机当前已装(plugins 分区导出的口径) */
68
+ local: readonly PluginRemovalEntry[];
69
+ /** 本插件自己的包名(永不删) */
70
+ selfName: string | undefined;
71
+ }
72
+ /** 判定「该不该删」。调用方只在 `applied` 含 plugins 时才真正执行。 */
73
+ export declare function planPluginRemovals(input: PluginRemovalInput): PluginRemovalPlan;
74
+ /**
75
+ * plugins 分区的按名三方合并(`merge.ts` 的 `GRANULAR_MERGES.plugins`)。
76
+ *
77
+ * 逐名规则与 git 同口径,祖先只用于判断「谁动了它」,绝不猜:
78
+ * - 两侧都有:身份相同 → 保留;某一侧与祖先相同 → 取另一侧;两侧都变(或没有祖先)→ both-modified 冲突;
79
+ * - 只有本机:有祖先且与祖先相同 → 跟随远端删除(真删除);无祖先 → 本机独有,保留;有祖先且不同 →
80
+ * modify-delete 冲突(用户在 A 机卸载、B 机却升级过它,不能替用户选);
81
+ * - 只有远端:有祖先且与祖先相同 → 本机删除,跟随(不复活);无祖先 → 远端独有,采纳;有祖先且不同 →
82
+ * delete-modify 冲突;
83
+ * - 两侧都没有:消失。
84
+ *
85
+ * 插件列表以外的字段不一致时返回 `undefined`:patch 行/pnpm-workspace 有各自的既有口径,
86
+ * 在这里再造一套合并只会和通用路径打架(通用路径会整分区处理,其中包含 plugins 数组的旧行为)。
87
+ */
88
+ export declare function mergePluginsSection(local: SectionData, remote: SectionData, ancestor: SectionData | undefined, hooks?: {
89
+ deletePlugins?: boolean;
90
+ }): GranularMergeOutcome | undefined;
@@ -0,0 +1,167 @@
1
+ import { isBundlesOnlyEntry } from '../core/plugin-cli.js';
2
+ function byName(list) {
3
+ const map = new Map();
4
+ for (const p of list ?? []) {
5
+ if (p !== null && typeof p === 'object' && typeof p.name === 'string' && p.name !== '')
6
+ map.set(p.name, p);
7
+ }
8
+ return map;
9
+ }
10
+ /** 判定「该不该删」。调用方只在 `applied` 含 plugins 时才真正执行。 */
11
+ export function planPluginRemovals(input) {
12
+ const plan = { removals: [], conflicts: [], kept: [] };
13
+ if (input.ancestor === undefined)
14
+ return plan; // 没有祖先:无删除语义(无法区分「对方删了」与「本机独有」)
15
+ const remote = byName(input.remote);
16
+ const local = byName(input.local);
17
+ for (const a of input.ancestor) {
18
+ const name = a.name;
19
+ if (typeof name !== 'string' || name === '')
20
+ continue;
21
+ if (remote.has(name)) {
22
+ plan.kept.push({ name, reason: 'still-in-remote' });
23
+ continue;
24
+ }
25
+ if (input.selfName !== undefined && name === input.selfName) {
26
+ plan.kept.push({ name, reason: 'self' });
27
+ continue;
28
+ }
29
+ const l = local.get(name);
30
+ if (isBundlesOnlyEntry(a) || (l !== undefined && isBundlesOnlyEntry(l))) {
31
+ plan.kept.push({ name, reason: 'bundles-only' });
32
+ continue;
33
+ }
34
+ if (l === undefined) {
35
+ plan.kept.push({ name, reason: 'not-installed' });
36
+ continue;
37
+ }
38
+ if (!input.enabled) {
39
+ plan.kept.push({ name, reason: 'switch-off' });
40
+ continue;
41
+ }
42
+ if (l.version !== a.version) {
43
+ plan.conflicts.push({ name, reason: 'version-changed', ancestorVersion: a.version, localVersion: l.version });
44
+ continue;
45
+ }
46
+ plan.removals.push({ name, version: a.version, ...(a.spec === undefined ? {} : { spec: a.spec }) });
47
+ }
48
+ return plan;
49
+ }
50
+ /* --------------------------------------------------------------- 分区级三方合并 */
51
+ /** 形状闸门:不认识就交回通用路径(畸形数据由通用路径拒绝,这里不另造一套判定)。 */
52
+ function asPlugins(data) {
53
+ if (data === null || typeof data !== 'object')
54
+ return undefined;
55
+ const obj = data;
56
+ if (obj.version !== 1 || !Array.isArray(obj.plugins))
57
+ return undefined;
58
+ return data;
59
+ }
60
+ /** 决定「同一个插件是否还是同一个东西」的身份字段(version + 来源 spec + bundlesOnly 形态)。 */
61
+ function entryKey(e) {
62
+ return [e.version ?? '', e.spec ?? '', e.bundlesOnly === true ? '1' : '0'].join('\u0000');
63
+ }
64
+ /** 插件列表以外的字段(patch 行 / pnpm-workspace 原文 / 本地 tarball / patch 文件)必须两侧相等才介入。 */
65
+ function sameShell(l, r) {
66
+ const pick = (s) => JSON.stringify({
67
+ patch: s.patch ?? [],
68
+ pnpmWorkspace: s.pnpmWorkspace ?? null,
69
+ localTarballs: s.localTarballs ?? [],
70
+ patchFiles: s.patchFiles ?? [],
71
+ });
72
+ return pick(l) === pick(r);
73
+ }
74
+ /**
75
+ * plugins 分区的按名三方合并(`merge.ts` 的 `GRANULAR_MERGES.plugins`)。
76
+ *
77
+ * 逐名规则与 git 同口径,祖先只用于判断「谁动了它」,绝不猜:
78
+ * - 两侧都有:身份相同 → 保留;某一侧与祖先相同 → 取另一侧;两侧都变(或没有祖先)→ both-modified 冲突;
79
+ * - 只有本机:有祖先且与祖先相同 → 跟随远端删除(真删除);无祖先 → 本机独有,保留;有祖先且不同 →
80
+ * modify-delete 冲突(用户在 A 机卸载、B 机却升级过它,不能替用户选);
81
+ * - 只有远端:有祖先且与祖先相同 → 本机删除,跟随(不复活);无祖先 → 远端独有,采纳;有祖先且不同 →
82
+ * delete-modify 冲突;
83
+ * - 两侧都没有:消失。
84
+ *
85
+ * 插件列表以外的字段不一致时返回 `undefined`:patch 行/pnpm-workspace 有各自的既有口径,
86
+ * 在这里再造一套合并只会和通用路径打架(通用路径会整分区处理,其中包含 plugins 数组的旧行为)。
87
+ */
88
+ export function mergePluginsSection(local, remote, ancestor, hooks = {}) {
89
+ // 开关关着 → 钩子不参与(交回通用合并 = 与 #132 之前逐字一致)。若照常「跟随远端删除」,
90
+ // 合并结果会丢掉本机条目:插件包还在盘上,下一轮导出会把它当「本机独有」推回远端 = 复活。
91
+ if (hooks.deletePlugins !== true)
92
+ return undefined;
93
+ const l = asPlugins(local);
94
+ const r = asPlugins(remote);
95
+ if (l === undefined || r === undefined)
96
+ return undefined;
97
+ const a = ancestor === undefined ? undefined : asPlugins(ancestor);
98
+ if (ancestor !== undefined && a === undefined)
99
+ return undefined;
100
+ if (!sameShell(l, r))
101
+ return undefined;
102
+ const aMap = byName(a?.plugins);
103
+ const rMap = byName(r.plugins);
104
+ const merged = [];
105
+ const conflicts = [];
106
+ let mergePoints = 0;
107
+ const conflict = (path, reason, lp, rp, ap) => {
108
+ conflicts.push({ path, kind: 'key', reason, autoMergeAttempted: false, local: lp, remote: rp, ancestor: ap });
109
+ };
110
+ for (const lp of l.plugins) {
111
+ const name = lp.name;
112
+ const rp = rMap.get(name);
113
+ const ap = aMap.get(name);
114
+ if (rp !== undefined) {
115
+ if (entryKey(lp) === entryKey(rp)) {
116
+ merged.push(lp);
117
+ continue;
118
+ }
119
+ const lChanged = ap === undefined || entryKey(lp) !== entryKey(ap);
120
+ const rChanged = ap === undefined || entryKey(rp) !== entryKey(ap);
121
+ if (lChanged && rChanged) {
122
+ conflict(`plugins.${name}`, 'both-modified', lp, rp, ap);
123
+ continue;
124
+ }
125
+ if (lChanged) {
126
+ merged.push(lp);
127
+ mergePoints += 1;
128
+ continue;
129
+ }
130
+ merged.push(rp);
131
+ mergePoints += 1;
132
+ continue;
133
+ }
134
+ // 只有本机有
135
+ if (ap === undefined) {
136
+ merged.push(lp);
137
+ mergePoints += 1;
138
+ continue;
139
+ } // 本机独有
140
+ if (entryKey(lp) === entryKey(ap)) {
141
+ mergePoints += 1;
142
+ continue;
143
+ } // 本机未改、远端删 → 跟随删除
144
+ conflict(`plugins.${name}`, 'modify-delete', lp, undefined, ap);
145
+ }
146
+ // 只有远端有的
147
+ for (const rp of r.plugins) {
148
+ const name = rp.name;
149
+ if (l.plugins.some((p) => p.name === name))
150
+ continue;
151
+ const ap = aMap.get(name);
152
+ if (ap === undefined) {
153
+ merged.push(rp);
154
+ mergePoints += 1;
155
+ continue;
156
+ } // 远端独有
157
+ if (entryKey(rp) === entryKey(ap)) {
158
+ mergePoints += 1;
159
+ continue;
160
+ } // 远端未改、本机删 → 跟随删除
161
+ conflict(`plugins.${name}`, 'delete-modify', undefined, rp, ap);
162
+ }
163
+ if (conflicts.length > 0)
164
+ return { merged: l, conflicts, mergePoints };
165
+ return { merged: { ...l, plugins: merged }, conflicts: [], mergePoints };
166
+ }
167
+ //# sourceMappingURL=plugin-removals.js.map
@@ -32,13 +32,25 @@ export declare function channelOf(cfg: SyncConfig): SyncTransportType;
32
32
  * 在每处穷举字面量(漏写的表现曾是「该通道的配置永远读不到 / 写不回」,静默且难查)。
33
33
  */
34
34
  export declare function channelMap<T>(make: (channel: SyncTransportType) => T): Record<SyncTransportType, T>;
35
- /** 当前 sync-config.json schema 版本号(v3:双命名空间共存,切换通道不丢失另一通道配置)。 */
36
- export declare const SYNC_CONFIG_SCHEMA_VERSION = 3;
37
- /** 历史可读取版本:v1、v2、v3。 */
35
+ /**
36
+ * 当前 sync-config.json schema 版本号(v4:git 通道新增可选 `subdir` 子目录前缀,issue #131)。
37
+ *
38
+ * 为什么必须升版而不是只加一个可选字段:老版本插件读到带 `subdir` 的配置会**丢掉这个字段**
39
+ * 然后照旧往仓库根写快照 —— 在「多工具共用一个备份仓库」的现场,那等于把快照撒进别人的目录布局。
40
+ * 升版后老版本读到 v4 直接判为不支持(显示未配置),宁可让用户看见「要升级」,也不静默写错位置。
41
+ */
42
+ export declare const SYNC_CONFIG_SCHEMA_VERSION = 4;
43
+ /** 历史可读取版本:v1、v2、v3、v4。 */
38
44
  export declare const SYNC_CONFIG_SUPPORTED_VERSIONS: readonly number[];
39
45
  /** git 通道配置(不含任何凭据;git 可执行文件固定使用系统 PATH 中的 git) */
40
46
  export interface GitConfig {
41
47
  repoUrl: string;
48
+ /**
49
+ * 仓库内子目录前缀(issue #131,可缺省):快照布局落在 `<subdir>/snapshots`、
50
+ * `<subdir>/snapshots-encrypted`、`<subdir>/blobs`。缺省 = 仓库根(历史行为逐字不变)。
51
+ * 多工具共用同一个私有备份仓库时,每个工具各占一个子目录。
52
+ */
53
+ subdir?: string;
42
54
  }
43
55
  /** webdav 通道配置(不含 password;password 走 DSH credentials) */
44
56
  export interface WebDavConfig {
@@ -372,6 +384,25 @@ export declare function validateCloudEndpoint(endpoint: string): CloudConfigIssu
372
384
  export declare function validateCloudRegion(region: string): CloudConfigIssueCode | null;
373
385
  /** 桶名校验。 */
374
386
  export declare function validateCloudBucket(bucket: string): CloudConfigIssueCode | null;
387
+ /**
388
+ * git 通道子目录前缀的归一化与校验(issue #131)。
389
+ *
390
+ * 为什么单独一个函数而不是复用 validateCloudPrefix:对象键前缀与仓库内目录名不是同一套约束 ——
391
+ * 目录名要允许 UTF-8(报告者现场就是 `DSH备份/` 这种中文目录)与多级嵌套,而对象键前缀按
392
+ * 存储桶语义只关心「不是绝对路径、没有 ..」。
393
+ *
394
+ * 接受:空串(= 仓库根,历史行为)、UTF-8 段、多级嵌套(`a/b`)。
395
+ * 拒绝:绝对路径(首部 /)、尾部 /、反斜杠、空段(含 `a//b`)、`.` / `..` 段、控制字符、
396
+ * `.git` 段(写进去会撞 git 自己的元数据目录名,任何大小写都算)。
397
+ */
398
+ export type GitSubdirCheck = {
399
+ ok: true;
400
+ value: string;
401
+ } | {
402
+ ok: false;
403
+ reason: 'gitSubdirInvalid';
404
+ };
405
+ export declare function normalizeGitSubdir(raw: unknown): GitSubdirCheck;
375
406
  /** 对象键前缀校验(可缺省 = 根前缀;不得以 / 开头、不得含 .. / 反斜杠 / 空段)。 */
376
407
  export declare function validateCloudPrefix(prefix: string): CloudConfigIssueCode | null;
377
408
  /** gist id 校验。 */
@@ -69,10 +69,16 @@ export function channelMap(make) {
69
69
  out[channel] = make(channel);
70
70
  return out;
71
71
  }
72
- /** 当前 sync-config.json schema 版本号(v3:双命名空间共存,切换通道不丢失另一通道配置)。 */
73
- export const SYNC_CONFIG_SCHEMA_VERSION = 3;
74
- /** 历史可读取版本:v1、v2、v3。 */
75
- export const SYNC_CONFIG_SUPPORTED_VERSIONS = [1, 2, 3];
72
+ /**
73
+ * 当前 sync-config.json schema 版本号(v4:git 通道新增可选 `subdir` 子目录前缀,issue #131)。
74
+ *
75
+ * 为什么必须升版而不是只加一个可选字段:老版本插件读到带 `subdir` 的配置会**丢掉这个字段**
76
+ * 然后照旧往仓库根写快照 —— 在「多工具共用一个备份仓库」的现场,那等于把快照撒进别人的目录布局。
77
+ * 升版后老版本读到 v4 直接判为不支持(显示未配置),宁可让用户看见「要升级」,也不静默写错位置。
78
+ */
79
+ export const SYNC_CONFIG_SCHEMA_VERSION = 4;
80
+ /** 历史可读取版本:v1、v2、v3、v4。 */
81
+ export const SYNC_CONFIG_SUPPORTED_VERSIONS = [1, 2, 3, 4];
76
82
  /** git 通道守卫 */
77
83
  export function isGitConfig(cfg) {
78
84
  return cfg.transport === 'git';
@@ -93,7 +99,12 @@ export function isGistConfig(cfg) {
93
99
  function parseV1Git(obj) {
94
100
  if (typeof obj['repoUrl'] !== 'string' || obj['repoUrl'] === '')
95
101
  return null;
96
- return { repoUrl: obj['repoUrl'] };
102
+ // issue #131:子目录前缀必须在这里**显式搬运** —— 本函数只构造已知字段,漏搬就是静默丢掉用户配置。
103
+ // 读侧同口径校验:非法值当作「未设置」(落在仓库根),绝不把可疑路径继续交给 git 子进程。
104
+ const subdir = normalizeGitSubdir(obj['subdir']);
105
+ return subdir.ok && subdir.value !== ''
106
+ ? { repoUrl: obj['repoUrl'], subdir: subdir.value }
107
+ : { repoUrl: obj['repoUrl'] };
97
108
  }
98
109
  /** 从 v2 git 命名空间解析;缺有效 repoUrl → null */
99
110
  function parseV2GitNamespace(ns) {
@@ -705,6 +716,28 @@ export function validateCloudBucket(bucket) {
705
716
  return 'cloud.bucketInvalid';
706
717
  return null;
707
718
  }
719
+ export function normalizeGitSubdir(raw) {
720
+ // 缺省(字段不存在)与空串同义:仓库根。只有「显式写了但不是字符串」才算非法。
721
+ if (raw === undefined || raw === null)
722
+ return { ok: true, value: '' };
723
+ if (typeof raw !== 'string')
724
+ return { ok: false, reason: 'gitSubdirInvalid' };
725
+ const p = raw.trim();
726
+ if (p === '')
727
+ return { ok: true, value: '' };
728
+ if (p.startsWith('/') || p.endsWith('/'))
729
+ return { ok: false, reason: 'gitSubdirInvalid' };
730
+ if (p.includes('\\') || /[\u0000-\u001f\u007f]/.test(p))
731
+ return { ok: false, reason: 'gitSubdirInvalid' };
732
+ const segments = p.split('/');
733
+ for (const seg of segments) {
734
+ if (seg === '' || seg === '.' || seg === '..')
735
+ return { ok: false, reason: 'gitSubdirInvalid' };
736
+ if (seg.toLowerCase() === '.git')
737
+ return { ok: false, reason: 'gitSubdirInvalid' };
738
+ }
739
+ return { ok: true, value: segments.join('/') };
740
+ }
708
741
  /** 对象键前缀校验(可缺省 = 根前缀;不得以 / 开头、不得含 .. / 反斜杠 / 空段)。 */
709
742
  export function validateCloudPrefix(prefix) {
710
743
  if (typeof prefix !== 'string')