dsh-config-manager 0.10.0 → 0.12.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 (233) hide show
  1. package/lib/adapters/attachment-index-cache.d.ts +33 -0
  2. package/lib/adapters/attachment-index-cache.js +114 -0
  3. package/lib/adapters/attachments.d.ts +97 -0
  4. package/lib/adapters/attachments.js +448 -0
  5. package/lib/adapters/index.d.ts +1 -0
  6. package/lib/adapters/index.js +7 -1
  7. package/lib/adapters/self.d.ts +2 -2
  8. package/lib/adapters/self.js +4 -0
  9. package/lib/adapters/sessions.d.ts +18 -1
  10. package/lib/adapters/sessions.js +193 -0
  11. package/lib/adapters/test-helpers.d.ts +7 -0
  12. package/lib/adapters/test-helpers.js +8 -1
  13. package/lib/adapters/workspaces.d.ts +0 -1
  14. package/lib/adapters/workspaces.js +244 -5
  15. package/lib/client.d.ts +170 -1
  16. package/lib/client.js +2067 -889
  17. package/lib/core/analyzer.d.ts +1 -14
  18. package/lib/core/analyzer.js +25 -67
  19. package/lib/core/backup-plan.js +4 -0
  20. package/lib/core/backup.d.ts +7 -0
  21. package/lib/core/backup.js +3 -0
  22. package/lib/core/external-failure.d.ts +18 -2
  23. package/lib/core/external-failure.js +63 -19
  24. package/lib/core/host-service.d.ts +12 -0
  25. package/lib/core/host-service.js +12 -0
  26. package/lib/core/host-session-shapes.d.ts +209 -0
  27. package/lib/core/host-session-shapes.js +134 -0
  28. package/lib/core/host-workspace-facade.d.ts +57 -0
  29. package/lib/core/host-workspace-facade.js +127 -0
  30. package/lib/core/import-planning.d.ts +15 -0
  31. package/lib/core/import-planning.js +44 -0
  32. package/lib/core/messages.d.ts +20 -0
  33. package/lib/core/messages.js +40 -0
  34. package/lib/core/phase3-host.js +2 -2
  35. package/lib/core/session-analyze.d.ts +52 -0
  36. package/lib/core/session-analyze.js +135 -0
  37. package/lib/core/session-compare.d.ts +54 -0
  38. package/lib/core/session-compare.js +154 -0
  39. package/lib/core/session-meta.d.ts +32 -0
  40. package/lib/core/session-meta.js +49 -0
  41. package/lib/core/session-verdict-plan.d.ts +2 -0
  42. package/lib/core/session-verdict-plan.js +33 -0
  43. package/lib/core/types.d.ts +153 -0
  44. package/lib/core/unit-enrichment.d.ts +19 -0
  45. package/lib/core/unit-enrichment.js +39 -0
  46. package/lib/core/workspace-root.d.ts +46 -0
  47. package/lib/core/workspace-root.js +121 -0
  48. package/lib/index.d.ts +2 -112
  49. package/lib/index.js +17 -222
  50. package/lib/profiles/dsh-profile-runtime.d.ts +54 -2
  51. package/lib/profiles/dsh-profile-runtime.js +124 -9
  52. package/lib/routes/route-support.d.ts +3 -0
  53. package/lib/routes/route-support.js +6 -0
  54. package/lib/schema/section-registry.js +15 -0
  55. package/lib/schema/types.d.ts +17 -1
  56. package/lib/sync/autosync-backoff.d.ts +40 -0
  57. package/lib/sync/autosync-backoff.js +74 -0
  58. package/lib/sync/autosync-config.d.ts +26 -41
  59. package/lib/sync/autosync-config.js +25 -26
  60. package/lib/sync/autosync-scheduler.d.ts +22 -10
  61. package/lib/sync/autosync-scheduler.js +509 -331
  62. package/lib/sync/backup-files.d.ts +3 -1
  63. package/lib/sync/backup-files.js +5 -3
  64. package/lib/sync/baseline-record.d.ts +10 -1
  65. package/lib/sync/baseline-record.js +2 -2
  66. package/lib/sync/credential-plan.d.ts +8 -0
  67. package/lib/sync/credential-plan.js +65 -0
  68. package/lib/sync/local-changes.d.ts +2 -1
  69. package/lib/sync/local-changes.js +4 -2
  70. package/lib/sync/partial-conflict-push.d.ts +39 -0
  71. package/lib/sync/partial-conflict-push.js +70 -0
  72. package/lib/sync/retention-policy.d.ts +23 -6
  73. package/lib/sync/retention-policy.js +51 -10
  74. package/lib/sync/round-export-cache.d.ts +22 -0
  75. package/lib/sync/round-export-cache.js +36 -0
  76. package/lib/sync/snapshot-zip.d.ts +9 -0
  77. package/lib/sync/snapshot-zip.js +62 -0
  78. package/lib/sync/sync-engine.d.ts +24 -30
  79. package/lib/sync/sync-engine.js +145 -157
  80. package/lib/sync/sync-history.d.ts +4 -0
  81. package/lib/sync/sync-watcher.d.ts +27 -0
  82. package/lib/sync/sync-watcher.js +80 -3
  83. package/lib/sync/transport.js +11 -2
  84. package/lib/sync/watch-scope.js +2 -1
  85. package/lib/ui/backup-schedule.d.ts +5 -1
  86. package/lib/ui/backup-schedule.js +5 -3
  87. package/lib/ui/export-flow.js +1 -0
  88. package/lib/ui/i18n.d.ts +3 -0
  89. package/lib/ui/i18n.js +6 -0
  90. package/lib/ui/import-wizard.d.ts +8 -0
  91. package/lib/ui/import-wizard.js +10 -0
  92. package/lib/ui/overview-view.d.ts +20 -7
  93. package/lib/ui/overview-view.js +31 -12
  94. package/lib/ui/selection-model.d.ts +60 -0
  95. package/lib/ui/selection-model.js +258 -2
  96. package/lib/ui/session-compare-facts.d.ts +51 -0
  97. package/lib/ui/session-compare-facts.js +82 -0
  98. package/lib/ui/sync-settings-view.d.ts +25 -0
  99. package/lib/ui/sync-settings-view.js +9 -0
  100. package/lib/ui/test-helpers.js +2 -2
  101. package/lib/utils/attachment-refs.d.ts +49 -0
  102. package/lib/utils/attachment-refs.js +169 -0
  103. package/lib/utils/shared-constants.d.ts +16 -0
  104. package/lib/utils/shared-constants.js +13 -0
  105. package/package.json +1 -1
  106. package/src/adapters/attachment-index-cache.test.ts +122 -0
  107. package/src/adapters/attachment-index-cache.ts +129 -0
  108. package/src/adapters/attachments.test.ts +358 -0
  109. package/src/adapters/attachments.ts +472 -0
  110. package/src/adapters/index.ts +8 -1
  111. package/src/adapters/self.ts +5 -1
  112. package/src/adapters/sessions.test.ts +140 -0
  113. package/src/adapters/sessions.ts +184 -1
  114. package/src/adapters/test-helpers.ts +8 -1
  115. package/src/adapters/workspaces.test.ts +254 -0
  116. package/src/adapters/workspaces.ts +234 -5
  117. package/src/client/common/ContentPicker.tsx +235 -15
  118. package/src/client/common/section-labels.test.ts +1 -1
  119. package/src/client/common/section-labels.ts +1 -0
  120. package/src/client/common/ui.tsx +17 -1
  121. package/src/client/config-manager.module.css +117 -0
  122. package/src/client/export/ExportView.tsx +15 -2
  123. package/src/client/home/BackupScheduleCard.tsx +13 -9
  124. package/src/client/home/HomePanel.tsx +28 -8
  125. package/src/client/import/ImportWizardView.tsx +8 -0
  126. package/src/client/import/PathMappingForm.tsx +49 -10
  127. package/src/client/import/import-wizard-steps.tsx +80 -3
  128. package/src/client/import/use-import-wizard-controller.ts +3 -0
  129. package/src/client/locales/backup.ts +6 -0
  130. package/src/client/locales/common.ts +2 -0
  131. package/src/client/locales/export.ts +5 -0
  132. package/src/client/locales/import.ts +6 -0
  133. package/src/client/locales/overview.ts +14 -0
  134. package/src/client/locales/sessions.ts +26 -0
  135. package/src/client/run-store-redact.ts +122 -0
  136. package/src/client/run-store.ts +13 -115
  137. package/src/client/sync/AutosyncCard.tsx +19 -8
  138. package/src/client/sync/SyncChannelEntryCard.tsx +21 -2
  139. package/src/client/sync/SyncChannelSection.tsx +23 -0
  140. package/src/client/sync/SyncChannelSkeleton.tsx +116 -0
  141. package/src/client/sync/SyncLogList.tsx +22 -2
  142. package/src/client/sync/SyncPanel.tsx +24 -25
  143. package/src/client/sync/SyncSecuritySection.tsx +7 -1
  144. package/src/client/sync/SyncSettingsDialog.tsx +22 -6
  145. package/src/client/sync/SyncSettingsView.test.ts +15 -0
  146. package/src/client/sync/history-model.test.ts +19 -1
  147. package/src/client/sync/history-model.ts +15 -0
  148. package/src/client/sync/sync-api.ts +14 -0
  149. package/src/client/sync/sync-locales.ts +28 -0
  150. package/src/client/sync/sync-view-v2.test.ts +14 -3
  151. package/src/client/sync/sync-view.test.ts +106 -4
  152. package/src/client/sync/sync-view.ts +159 -16
  153. package/src/core/analyzer.ts +38 -79
  154. package/src/core/backup-plan.ts +4 -0
  155. package/src/core/backup.ts +10 -0
  156. package/src/core/external-failure.test.ts +31 -0
  157. package/src/core/external-failure.ts +71 -21
  158. package/src/core/host-service.ts +17 -0
  159. package/src/core/host-session-shapes.ts +270 -0
  160. package/src/core/host-workspace-facade.ts +153 -0
  161. package/src/core/import-planning.ts +48 -0
  162. package/src/core/messages.ts +40 -0
  163. package/src/core/native-path-picker.test.ts +3 -2
  164. package/src/core/phase3-host.ts +2 -2
  165. package/src/core/phase3-production-integration.test.ts +73 -1
  166. package/src/core/session-analyze.ts +159 -0
  167. package/src/core/session-compare-wiring.test.ts +207 -0
  168. package/src/core/session-compare.test.ts +119 -0
  169. package/src/core/session-compare.ts +185 -0
  170. package/src/core/session-meta.test.ts +25 -2
  171. package/src/core/session-meta.ts +54 -0
  172. package/src/core/session-verdict-plan.test.ts +70 -0
  173. package/src/core/session-verdict-plan.ts +32 -0
  174. package/src/core/types.ts +161 -0
  175. package/src/core/unit-enrichment.ts +46 -0
  176. package/src/core/workspace-root.test.ts +152 -0
  177. package/src/core/workspace-root.ts +146 -0
  178. package/src/foreign/trae-sqlcipher.test.ts +13 -4
  179. package/src/index.ts +27 -355
  180. package/src/profiles/dsh-profile-runtime.test.ts +77 -3
  181. package/src/profiles/dsh-profile-runtime.ts +125 -9
  182. package/src/routes/route-support.test.ts +49 -0
  183. package/src/routes/route-support.ts +9 -0
  184. package/src/schema/manifest-schema.test.ts +1 -1
  185. package/src/schema/registry.test.ts +7 -2
  186. package/src/schema/section-registry.ts +15 -0
  187. package/src/schema/types.ts +23 -2
  188. package/src/sync/autosync-backoff.test.ts +64 -0
  189. package/src/sync/autosync-backoff.ts +88 -0
  190. package/src/sync/autosync-config.test.ts +38 -0
  191. package/src/sync/autosync-config.ts +66 -52
  192. package/src/sync/autosync-risk-tier.test.ts +5 -1
  193. package/src/sync/autosync-scheduler.test.ts +537 -4
  194. package/src/sync/autosync-scheduler.ts +252 -68
  195. package/src/sync/backup-files.ts +4 -1
  196. package/src/sync/backup-scheduler.test.ts +5 -4
  197. package/src/sync/baseline-record.ts +12 -3
  198. package/src/sync/credential-plan.ts +70 -0
  199. package/src/sync/local-changes.ts +10 -4
  200. package/src/sync/partial-conflict-push.test.ts +237 -0
  201. package/src/sync/partial-conflict-push.ts +105 -0
  202. package/src/sync/retention-age-tier.test.ts +79 -0
  203. package/src/sync/retention-policy.test.ts +101 -10
  204. package/src/sync/retention-policy.ts +57 -8
  205. package/src/sync/round-export-cache.test.ts +83 -0
  206. package/src/sync/round-export-cache.ts +53 -0
  207. package/src/sync/snapshot-zip.ts +70 -0
  208. package/src/sync/sync-engine.test.ts +35 -1
  209. package/src/sync/sync-engine.ts +145 -155
  210. package/src/sync/sync-history.ts +4 -0
  211. package/src/sync/sync-watcher.test.ts +76 -4
  212. package/src/sync/sync-watcher.ts +82 -3
  213. package/src/sync/transport.test.ts +1 -1
  214. package/src/sync/transport.ts +10 -2
  215. package/src/sync/watch-scope.test.ts +8 -0
  216. package/src/sync/watch-scope.ts +2 -1
  217. package/src/ui/backup-schedule.test.ts +13 -6
  218. package/src/ui/backup-schedule.ts +4 -2
  219. package/src/ui/export-flow.ts +1 -0
  220. package/src/ui/i18n.ts +6 -0
  221. package/src/ui/import-wizard.ts +11 -0
  222. package/src/ui/overview-view.test.ts +48 -0
  223. package/src/ui/overview-view.ts +47 -13
  224. package/src/ui/selection-model.test.ts +265 -3
  225. package/src/ui/selection-model.ts +256 -2
  226. package/src/ui/session-compare-facts.test.ts +67 -0
  227. package/src/ui/session-compare-facts.ts +122 -0
  228. package/src/ui/sync-settings-view.test.ts +60 -1
  229. package/src/ui/sync-settings-view.ts +36 -0
  230. package/src/ui/test-helpers.ts +2 -2
  231. package/src/utils/attachment-refs.test.ts +98 -0
  232. package/src/utils/attachment-refs.ts +177 -0
  233. package/src/utils/shared-constants.ts +22 -0
@@ -0,0 +1,135 @@
1
+ import { attachmentIdOfRelPath } from '../utils/attachment-refs.js';
2
+ import { overwriteExposure } from './session-compare.js';
3
+ /**
4
+ * 会话格式体检(探针注入:core 不解析会话格式)。
5
+ *
6
+ * 「包里的版本 > 本机支持的版本」告警;本机版本解析不到时**不猜**(宁可不报,也不谎报兼容)。
7
+ * 不可判定(无探针 / 无 sessions / 本机版本解析不到 / 探针抛错)一律返回 null。
8
+ */
9
+ export function resolveSessionFormats(sections, probe, target) {
10
+ if (probe === undefined || !sections.has('sessions'))
11
+ return null;
12
+ if (target === undefined)
13
+ return null;
14
+ const rows = sections.get('sessions')?.files;
15
+ if (!Array.isArray(rows))
16
+ return null;
17
+ const files = [];
18
+ for (const row of rows) {
19
+ const item = row;
20
+ if (typeof item.relativePath === 'string' && item.data instanceof Uint8Array) {
21
+ files.push({ relativePath: item.relativePath, data: item.data });
22
+ }
23
+ }
24
+ try {
25
+ const probed = probe(files);
26
+ // 只有明确「版本更高」才算读不了;units 缺省(旧探针实现)时退化为空数组(不猜单元)。
27
+ return {
28
+ target,
29
+ unreadable: (probed.units ?? []).filter((unit) => unit.version > target),
30
+ examined: probed.sampled + probed.unreadable,
31
+ skipped: probed.skipped,
32
+ };
33
+ }
34
+ catch {
35
+ // 体检失败绝不拖垮分析,也绝不因此宣称兼容(本条静默,另有导出报告可核对)。
36
+ return null;
37
+ }
38
+ }
39
+ export function sessionFormatWarnings(result, msg) {
40
+ if (result === null)
41
+ return [];
42
+ const target = result.target;
43
+ const newer = result.unreadable.map((unit) => unit.version);
44
+ if (newer.length === 0)
45
+ return [];
46
+ const warnings = [msg('import.sessionsFormatUnsupported', {
47
+ count: String(newer.length),
48
+ newer: String(Math.max(...newer)),
49
+ target: String(target),
50
+ })];
51
+ if (result.skipped > 0) {
52
+ warnings.push(msg('import.sessionsFormatSampled', {
53
+ sampled: String(result.examined),
54
+ skipped: String(result.skipped),
55
+ }));
56
+ }
57
+ return warnings;
58
+ }
59
+ /**
60
+ * 会话逐条比对(包内同名会话 vs **本机**)—— 与格式体检一样**每份归档只跑一次**。
61
+ *
62
+ * 为什么由 sessions adapter 执行而不是这里:解会话日志要用 zstd 帧与首帧能力,那是会话字节
63
+ * 工具(core 不 import 它);纯比对算法在 core/session-compare.ts。此处只负责「找到适配器、
64
+ * 调一次、再补上附件缺口」。
65
+ *
66
+ * 附件缺口是**纯包内判定**(包内会话引用到的 id ∩ 包内实际带上的附件对象),因此不读本机、
67
+ * 也不会因为本机文件被用户删了而误报:它回答的是「按这个包恢复,哪些图会缺」。
68
+ */
69
+ export async function resolveSessionCompare(sections, adapters, ctx) {
70
+ if (!sections.has('sessions'))
71
+ return null;
72
+ const data = sections.get('sessions');
73
+ if (data === undefined || !Array.isArray(data.files) || data.files.length === 0)
74
+ return null;
75
+ const adapter = adapters.find((candidate) => candidate.id === 'sessions' && typeof candidate.compareSessions === 'function');
76
+ if (adapter?.compareSessions === undefined)
77
+ return null;
78
+ let summary;
79
+ try {
80
+ summary = await adapter.compareSessions(ctx, data);
81
+ }
82
+ catch {
83
+ // 比对失败绝不拖垮分析,也绝不因此宣称「无差异」(本条静默)
84
+ return null;
85
+ }
86
+ if (summary === null)
87
+ return null;
88
+ const present = new Set();
89
+ const attachments = sections.get('attachments');
90
+ if (attachments !== undefined && Array.isArray(attachments.files)) {
91
+ for (const row of attachments.files) {
92
+ const rel = row.relativePath;
93
+ if (typeof rel !== 'string')
94
+ continue;
95
+ const id = attachmentIdOfRelPath(rel);
96
+ if (id !== null)
97
+ present.add(id);
98
+ }
99
+ }
100
+ const missing = summary.attachmentsMissing.filter((id) => !present.has(id));
101
+ return { ...summary, attachmentsMissing: missing };
102
+ }
103
+ /**
104
+ * 逐条比对的告警(只在**真会丢东西**时说话):
105
+ * - 本机比包内多内容 → 「导入会覆盖掉本机 N 条消息」(这是本功能存在的理由);
106
+ * - 引用了包内没有的附件 → 恢复后图会缺;
107
+ * - 预算截断 → 「另有 N 条未比对」(绝不让用户以为全都比过了)。
108
+ */
109
+ export function sessionCompareWarnings(result, msg) {
110
+ if (result === null)
111
+ return [];
112
+ const warnings = [];
113
+ const exposure = overwriteExposure(result.rows);
114
+ if (exposure.units > 0) {
115
+ warnings.push(msg('import.sessionOverwrite', {
116
+ units: String(exposure.units),
117
+ events: String(exposure.events),
118
+ total: String(result.rows.length),
119
+ }));
120
+ }
121
+ if (result.attachmentsMissing.length > 0) {
122
+ warnings.push(msg('import.sessionAttachmentsMissing', {
123
+ count: String(result.attachmentsMissing.length),
124
+ ids: result.attachmentsMissing.slice(0, 3).map((id) => id.slice(0, 19)).join(', '),
125
+ }));
126
+ }
127
+ if (result.skipped > 0) {
128
+ warnings.push(msg('import.sessionCompareSampled', {
129
+ sampled: String(result.rows.length),
130
+ skipped: String(result.skipped),
131
+ }));
132
+ }
133
+ return warnings;
134
+ }
135
+ //# sourceMappingURL=session-analyze.js.map
@@ -0,0 +1,54 @@
1
+ /**
2
+ * 会话逐条比对(P3)—— **纯函数,零 I/O**。
3
+ *
4
+ * 为什么需要它:导入是把包内会话**覆盖**到本机(备份恢复口径,不是增量追加 —— 见 K1:
5
+ * 双机日常同步走远程同步通道)。于是「本机这条对话在导出之后又聊了 40 轮」这件事,
6
+ * 用户在导入前必须看得见:导入会以包为准,那 40 轮会被覆盖掉。此前插件只能告诉用户
7
+ * 「这个文件 Conflict」(文件级 SHA 不同),无法说清**差在哪、会丢多少**。
8
+ *
9
+ * 判据只有「事件序列前缀」这一条(不猜时间、不猜 tokens):
10
+ * - identical:逐条一致(只差 bookkeeping 事件也算一致);
11
+ * - packAhead:本机是包的前缀 → 包里多出 N 条(导入后多 N 条);
12
+ * - localAhead:包是本机的前缀 → 本机多出 N 条(**导入会覆盖掉这 N 条**);
13
+ * - diverged:前缀之后两边都有内容 → 分叉(导入按包为准)。
14
+ *
15
+ * 反序列化口径:事件行按 JSON 规范化后比对(键排序 + `cwd` 归一为 `<cwd>`)。为什么必须
16
+ * 归一 cwd:包内首帧 cwd 是**导出机**的路径,导入时会按映射改写(只改首帧,C1),
17
+ * 不归一会让任何跨机恢复都被判成 diverged —— 那是误报,不是差异。
18
+ */
19
+ import type { SessionCompareRow, SessionVerdict } from './types.ts';
20
+ /** 一行事件 → 比对用的规范形态;空行返回空串(调用方过滤)。 */
21
+ export declare function canonicalEventLine(line: string): string;
22
+ /** 文本(解码后的日志)→ 规范事件行。空行与非对象行照样保留原文,绝不静默丢条。 */
23
+ export declare function canonicalEventLines(text: string): string[];
24
+ /** 两条事件序列的比对结论(不含 unit 信息;聚合见 {@link aggregateSessionRows})。 */
25
+ export interface EventStreamDiff {
26
+ verdict: 'identical' | 'packAhead' | 'localAhead' | 'diverged';
27
+ /** 包内事件条数 */
28
+ packEvents: number;
29
+ /** 本机事件条数 */
30
+ localEvents: number;
31
+ /** 包比本机多出的事件条数(prefix 之后) */
32
+ packAheadBy: number;
33
+ /** 本机比包多出的事件条数(prefix 之后)—— diverged 时 = 会被覆盖掉的条数 */
34
+ localAheadBy: number;
35
+ /** 首个不一致位置(0 起);无差异时缺省 */
36
+ divergedAt?: number;
37
+ }
38
+ export declare function compareEventStreams(pack: readonly string[], local: readonly string[]): EventStreamDiff;
39
+ /**
40
+ * 同一会话单元可能有多份日志(不同 generation)。按文件相对路径逐一比,再取**最严重**的那条:
41
+ * 用户关心的是「这一条导入会不会丢东西」,任一份分叉就必须说成会丢。
42
+ */
43
+ export declare function aggregateSessionRows(unitId: string, diffs: readonly EventStreamDiff[], title?: string): SessionCompareRow;
44
+ /** 汇总计数(UI/报告只看这几个数字,逐条明细在 rows 里)。 */
45
+ export declare function countSessionVerdicts(rows: readonly SessionCompareRow[]): Record<SessionVerdict, number>;
46
+ /**
47
+ * 「导入会覆盖掉本机会话内容」的条数与事件总数(确认页必须把它说出来的那句数字)。
48
+ *
49
+ * 口径:localAhead + diverged 两类 —— 它们的共同点就是**本机有包内没有的内容**。
50
+ */
51
+ export declare function overwriteExposure(rows: readonly SessionCompareRow[]): {
52
+ units: number;
53
+ events: number;
54
+ };
@@ -0,0 +1,154 @@
1
+ /** bookkeeping 事件:不含对话内容,只记录日志自身形态(发种子结束、日志维护)。 */
2
+ const BOOKKEEPING_TYPES = new Set(['session/end-seed']);
3
+ const BOOKKEEPING_PREFIX = 'session-log-';
4
+ function canonicalize(value) {
5
+ if (Array.isArray(value))
6
+ return value.map(canonicalize);
7
+ if (value !== null && typeof value === 'object') {
8
+ const source = value;
9
+ const out = {};
10
+ for (const key of Object.keys(source).sort()) {
11
+ out[key] = key === 'cwd' ? '<cwd>' : canonicalize(source[key]);
12
+ }
13
+ return out;
14
+ }
15
+ return value;
16
+ }
17
+ /** 一行事件 → 比对用的规范形态;空行返回空串(调用方过滤)。 */
18
+ export function canonicalEventLine(line) {
19
+ const trimmed = line.trim();
20
+ if (trimmed === '')
21
+ return '';
22
+ let parsed;
23
+ try {
24
+ parsed = JSON.parse(trimmed);
25
+ }
26
+ catch {
27
+ // 解析不了就按原文比对:宁可判 diverged(保守),也不假装一致
28
+ return trimmed;
29
+ }
30
+ return JSON.stringify(canonicalize(parsed));
31
+ }
32
+ /** 文本(解码后的日志)→ 规范事件行。空行与非对象行照样保留原文,绝不静默丢条。 */
33
+ export function canonicalEventLines(text) {
34
+ const out = [];
35
+ for (const line of text.split(/\r?\n/)) {
36
+ const canonical = canonicalEventLine(line);
37
+ if (canonical !== '')
38
+ out.push(canonical);
39
+ }
40
+ return out;
41
+ }
42
+ function isBookkeepingLine(canonical) {
43
+ let type;
44
+ try {
45
+ type = JSON.parse(canonical).type;
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ if (typeof type !== 'string')
51
+ return false;
52
+ return BOOKKEEPING_TYPES.has(type) || type.startsWith(BOOKKEEPING_PREFIX);
53
+ }
54
+ export function compareEventStreams(pack, local) {
55
+ let i = 0;
56
+ while (i < pack.length && i < local.length && pack[i] === local[i])
57
+ i += 1;
58
+ const packRest = pack.slice(i);
59
+ const localRest = local.slice(i);
60
+ const base = { packEvents: pack.length, localEvents: local.length };
61
+ if (packRest.length === 0 && localRest.length === 0) {
62
+ return { verdict: 'identical', ...base, packAheadBy: 0, localAheadBy: 0 };
63
+ }
64
+ if (packRest.length === 0) {
65
+ // 本机多出来的全是 bookkeeping → 内容其实一致,别用噪声报「本机更新」
66
+ if (localRest.every(isBookkeepingLine)) {
67
+ return { verdict: 'identical', ...base, packAheadBy: 0, localAheadBy: 0 };
68
+ }
69
+ return { verdict: 'localAhead', ...base, packAheadBy: 0, localAheadBy: localRest.length };
70
+ }
71
+ if (localRest.length === 0) {
72
+ if (packRest.every(isBookkeepingLine)) {
73
+ return { verdict: 'identical', ...base, packAheadBy: 0, localAheadBy: 0 };
74
+ }
75
+ return { verdict: 'packAhead', ...base, packAheadBy: packRest.length, localAheadBy: 0 };
76
+ }
77
+ return {
78
+ verdict: 'diverged',
79
+ ...base,
80
+ packAheadBy: packRest.length,
81
+ localAheadBy: localRest.length,
82
+ divergedAt: i,
83
+ };
84
+ }
85
+ /** 同一单元内多份日志的严重度顺序:分叉 > 本机更新 > 包更新 > 一致。 */
86
+ const VERDICT_RANK = {
87
+ unreadable: 5,
88
+ diverged: 4,
89
+ localAhead: 3,
90
+ packAhead: 2,
91
+ identical: 1,
92
+ new: 0,
93
+ };
94
+ /**
95
+ * 同一会话单元可能有多份日志(不同 generation)。按文件相对路径逐一比,再取**最严重**的那条:
96
+ * 用户关心的是「这一条导入会不会丢东西」,任一份分叉就必须说成会丢。
97
+ */
98
+ export function aggregateSessionRows(unitId, diffs, title) {
99
+ // 没有任何可比数据 ≠「本机没有同名会话」:new 的语义是后者,空输入只能是「读不出/没比过」。
100
+ if (diffs.length === 0)
101
+ return { unitId, verdict: 'unreadable', packEvents: 0, localEvents: 0 };
102
+ let worst = diffs[0];
103
+ let packEvents = 0;
104
+ let localEvents = 0;
105
+ let packAheadBy = 0;
106
+ let localAheadBy = 0;
107
+ let divergedAt;
108
+ for (const diff of diffs) {
109
+ packEvents += diff.packEvents;
110
+ localEvents += diff.localEvents;
111
+ packAheadBy += diff.packAheadBy;
112
+ localAheadBy += diff.localAheadBy;
113
+ if (divergedAt === undefined && diff.divergedAt !== undefined)
114
+ divergedAt = diff.divergedAt;
115
+ if (VERDICT_RANK[diff.verdict] > VERDICT_RANK[worst.verdict])
116
+ worst = diff;
117
+ }
118
+ return {
119
+ unitId,
120
+ verdict: worst.verdict,
121
+ packEvents,
122
+ localEvents,
123
+ ...(packAheadBy > 0 ? { packAheadBy } : {}),
124
+ ...(localAheadBy > 0 ? { localAheadBy } : {}),
125
+ ...(divergedAt !== undefined ? { divergedAt } : {}),
126
+ ...(title !== undefined && title !== '' ? { title } : {}),
127
+ };
128
+ }
129
+ /** 汇总计数(UI/报告只看这几个数字,逐条明细在 rows 里)。 */
130
+ export function countSessionVerdicts(rows) {
131
+ const out = {
132
+ new: 0, identical: 0, packAhead: 0, localAhead: 0, diverged: 0, unreadable: 0,
133
+ };
134
+ for (const row of rows)
135
+ out[row.verdict] += 1;
136
+ return out;
137
+ }
138
+ /**
139
+ * 「导入会覆盖掉本机会话内容」的条数与事件总数(确认页必须把它说出来的那句数字)。
140
+ *
141
+ * 口径:localAhead + diverged 两类 —— 它们的共同点就是**本机有包内没有的内容**。
142
+ */
143
+ export function overwriteExposure(rows) {
144
+ let units = 0;
145
+ let events = 0;
146
+ for (const row of rows) {
147
+ if (row.verdict !== 'localAhead' && row.verdict !== 'diverged')
148
+ continue;
149
+ units += 1;
150
+ events += row.localAheadBy ?? 0;
151
+ }
152
+ return { units, events };
153
+ }
154
+ //# sourceMappingURL=session-compare.js.map
@@ -103,6 +103,21 @@ export declare function applySessionParentLinks<T extends {
103
103
  id: string;
104
104
  parentSessionId?: string;
105
105
  }>(units: readonly T[], parentOf: ReadonlyMap<string, string> | undefined, sectionId?: string): T[];
106
+ /**
107
+ * 导入计划项版的 `applySessionParentLinks`:给 sessions 计划项补「父对话」。
108
+ *
109
+ * 与导出侧只差一个 id 形状问题:计划项 id 是「分区:相对路径」、末段是**文件名**
110
+ * (`session.jsonl` / `session.jsonl.zstd`),直接解析拿不到会话目录 → 必须用 `unitId`
111
+ * (`sessions:<项目键>/<会话目录>`,与附件 `parentUnitIds` 同一口径)。
112
+ * `parentOf` 由宿主 `sessions.parentRelations()` 经 `subagentParentMap()` 归一化;
113
+ * 查不到 / 非 sessions 分区一律原样返回(不猜)。
114
+ */
115
+ export declare function applySessionParentLinksToPlanItems<T extends {
116
+ id: string;
117
+ adapter: string;
118
+ unitId?: string;
119
+ parentSessionId?: string;
120
+ }>(items: readonly T[], parentOf: ReadonlyMap<string, string> | undefined, sectionId?: string): T[];
106
121
  /**
107
122
  * 给**会话体检行**补上「界面标题」(行上的 `sessionId` = 会话目录名,索引键是裸键 → 查表前必须归一化)。
108
123
  *
@@ -135,3 +150,20 @@ export declare function applySessionTitles<T extends {
135
150
  * 用户看到的就是「历史对话没有按最新到最旧排序」。缓存里有精确的 lastPromptAt 时仍以它为第一口径。
136
151
  */
137
152
  export declare function applySessionMeta(units: readonly ExportUnit[], index: SessionMetaIndex, sectionId?: string, activityAt?: ReadonlyMap<string, number>): ExportUnit[];
153
+ /**
154
+ * sessions 计划项的一次性标注:会话标题/工作区分组 + 子代理父子链接(导入选择器的展示与勾选联动)。
155
+ *
156
+ * 两条都是「读不到就原样」:宿主不提供 storages 或 parentRelations、解析失败、形状不符 —— 一律保持
157
+ * 目录名与扁平结构,绝不让计划生成失败,也不凭 id 编造归属(宁可不联动,不猜)。
158
+ * 语义边界(父子链接):只认**本机**已知关系,包内新会话在本机还不存在就保持扁平。
159
+ */
160
+ export declare function annotateSessionsPlanItems<T extends {
161
+ id: string;
162
+ adapter: string;
163
+ unitId?: string;
164
+ label?: string;
165
+ group?: string;
166
+ parentSessionId?: string;
167
+ }>(items: readonly T[], host: HostContext, sectionId?: string): Promise<(T & {
168
+ blank?: boolean;
169
+ })[]>;
@@ -302,6 +302,25 @@ export function applySessionParentLinks(units, parentOf, sectionId = 'sessions')
302
302
  return parent === undefined ? unit : { ...unit, parentSessionId: parent };
303
303
  });
304
304
  }
305
+ /**
306
+ * 导入计划项版的 `applySessionParentLinks`:给 sessions 计划项补「父对话」。
307
+ *
308
+ * 与导出侧只差一个 id 形状问题:计划项 id 是「分区:相对路径」、末段是**文件名**
309
+ * (`session.jsonl` / `session.jsonl.zstd`),直接解析拿不到会话目录 → 必须用 `unitId`
310
+ * (`sessions:<项目键>/<会话目录>`,与附件 `parentUnitIds` 同一口径)。
311
+ * `parentOf` 由宿主 `sessions.parentRelations()` 经 `subagentParentMap()` 归一化;
312
+ * 查不到 / 非 sessions 分区一律原样返回(不猜)。
313
+ */
314
+ export function applySessionParentLinksToPlanItems(items, parentOf, sectionId = 'sessions') {
315
+ if (parentOf === undefined || parentOf.size === 0)
316
+ return [...items];
317
+ return items.map((item) => {
318
+ if (item.adapter !== sectionId)
319
+ return item;
320
+ const [probe] = applySessionParentLinks([{ id: item.unitId ?? item.id }], parentOf, sectionId);
321
+ return probe?.parentSessionId === undefined ? item : { ...item, parentSessionId: probe.parentSessionId };
322
+ });
323
+ }
305
324
  /**
306
325
  * 给**会话体检行**补上「界面标题」(行上的 `sessionId` = 会话目录名,索引键是裸键 → 查表前必须归一化)。
307
326
  *
@@ -386,4 +405,34 @@ export function applySessionMeta(units, index, sectionId = 'sessions', activityA
386
405
  return diff !== 0 ? diff : (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
387
406
  });
388
407
  }
408
+ /**
409
+ * sessions 计划项的一次性标注:会话标题/工作区分组 + 子代理父子链接(导入选择器的展示与勾选联动)。
410
+ *
411
+ * 两条都是「读不到就原样」:宿主不提供 storages 或 parentRelations、解析失败、形状不符 —— 一律保持
412
+ * 目录名与扁平结构,绝不让计划生成失败,也不凭 id 编造归属(宁可不联动,不猜)。
413
+ * 语义边界(父子链接):只认**本机**已知关系,包内新会话在本机还不存在就保持扁平。
414
+ */
415
+ export async function annotateSessionsPlanItems(items, host, sectionId = 'sessions') {
416
+ if (!items.some((item) => item.adapter === sectionId))
417
+ return [...items];
418
+ let out = [...items];
419
+ try {
420
+ const ids = out.filter((item) => item.adapter === sectionId).map((item) => item.unitId ?? item.id);
421
+ out = applySessionMetaToPlanItems(out, await readSessionMetaWithTitles(host, ids), sectionId);
422
+ }
423
+ catch {
424
+ /* 保持目录名(旧行为) */
425
+ }
426
+ const relations = host.sessions?.parentRelations;
427
+ if (relations !== undefined && host.sessions !== undefined) {
428
+ try {
429
+ // this 绑定必须留在门面对象上(宿主实现内部读 this 状态)
430
+ out = applySessionParentLinksToPlanItems(out, subagentParentMap(await relations.call(host.sessions)), sectionId);
431
+ }
432
+ catch {
433
+ /* 读不到就不标 */
434
+ }
435
+ }
436
+ return out;
437
+ }
389
438
  //# sourceMappingURL=session-meta.js.map
@@ -0,0 +1,2 @@
1
+ import type { PlanItem, SessionCompareRow } from './types.ts';
2
+ export declare function markSessionVerdicts(planItems: readonly PlanItem[], rows: readonly SessionCompareRow[]): void;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * P3 的落点:把逐条比对结论标到**具体计划项**上(键空间对齐见 import-planning 的 sessionUnitLookup)。
3
+ *
4
+ * 为什么独立于 session-analyze:那是「分析期」(读包、解码、算差异),这里是「计划期」
5
+ * (只读 planItems、只写展示字段),职责不同、测试面也不同。
6
+ *
7
+ * 标记是纯附加字段:不改 kind、不拦执行 —— 它只回答「这条导入会覆盖掉本机多少新内容」,
8
+ * 决策仍归用户(本插件的导入是「以包为准」,没有只追加模式)。
9
+ */
10
+ import { sessionUnitLookup } from './import-planning.js';
11
+ export function markSessionVerdicts(planItems, rows) {
12
+ if (rows.length === 0)
13
+ return;
14
+ const byUnit = new Map(rows.map((row) => [row.unitId, row]));
15
+ for (const item of planItems) {
16
+ const row = sessionUnitLookup(byUnit, item.unitId ?? item.id);
17
+ if (row === undefined)
18
+ continue;
19
+ item.sessionVerdict = {
20
+ verdict: row.verdict,
21
+ packEvents: row.packEvents,
22
+ localEvents: row.localEvents,
23
+ ...(row.localAheadBy === undefined ? {} : { localAheadBy: row.localAheadBy }),
24
+ ...(row.packAheadBy === undefined ? {} : { packAheadBy: row.packAheadBy }),
25
+ };
26
+ // 只降显示级别,绝不复原已被阻断的项:SEC-05 在 analyzeBundle 期已把表外条目设成
27
+ // severity:'error' + kind:'Error',无条件覆盖会把「为什么不导」的红色警示降成普通警告。
28
+ if ((row.verdict === 'localAhead' || row.verdict === 'diverged') && item.kind !== 'Error') {
29
+ item.severity = 'warning';
30
+ }
31
+ }
32
+ }
33
+ //# sourceMappingURL=session-verdict-plan.js.map