dsh-config-manager 0.8.0 → 0.8.2
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.
- package/lib/adapters/mcp.d.ts +6 -1
- package/lib/adapters/mcp.js +22 -4
- package/lib/client.d.ts +18 -1
- package/lib/client.js +628 -573
- package/lib/core/messages.d.ts +2 -0
- package/lib/core/messages.js +4 -0
- package/lib/core/patch-document.d.ts +65 -0
- package/lib/core/patch-document.js +161 -0
- package/lib/foreign/read-trae.d.ts +11 -0
- package/lib/foreign/read-trae.js +124 -4
- package/lib/foreign/session-source.d.ts +12 -4
- package/lib/foreign/session-source.js +5 -3
- package/lib/foreign/trae-sqlcipher.d.ts +127 -0
- package/lib/foreign/trae-sqlcipher.js +519 -0
- package/lib/foreign/trae.js +3 -1
- package/lib/foreign/truth-table.js +3 -2
- package/lib/foreign/types.d.ts +9 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.js +35 -53
- package/lib/ui/foreign-view.d.ts +19 -0
- package/lib/ui/foreign-view.js +24 -0
- package/lib/ui/i18n.d.ts +5 -0
- package/lib/ui/i18n.js +13 -0
- package/package.json +1 -1
- package/src/adapters/mcp.test.ts +23 -3
- package/src/adapters/mcp.ts +24 -4
- package/src/client/import/ForeignImportView.tsx +25 -7
- package/src/client/import/foreign-import-view-guard.test.ts +1 -1
- package/src/client/locales/foreign.ts +9 -0
- package/src/core/messages.ts +4 -0
- package/src/core/patch-document.test.ts +107 -0
- package/src/core/patch-document.ts +194 -0
- package/src/foreign/file-budget.test.ts +7 -1
- package/src/foreign/read-trae.ts +117 -5
- package/src/foreign/session-source.ts +15 -5
- package/src/foreign/trae-sqlcipher.test.ts +360 -0
- package/src/foreign/trae-sqlcipher.ts +564 -0
- package/src/foreign/trae.test.ts +188 -0
- package/src/foreign/trae.ts +3 -1
- package/src/foreign/truth-table.ts +4 -2
- package/src/foreign/types.ts +10 -1
- package/src/index.facade.test.ts +83 -1
- package/src/index.ts +37 -47
- package/src/ui/foreign-view.test.ts +36 -0
- package/src/ui/foreign-view.ts +46 -0
- package/src/ui/i18n.ts +13 -0
- package/src/utils/patch-yaml.test.ts +1 -1
package/lib/core/messages.d.ts
CHANGED
|
@@ -195,6 +195,8 @@ export declare const zh: {
|
|
|
195
195
|
readonly 'host.patchUnsupported': 'dsh-config-manager 仅支持管理 {user} 与 profile 的 {profile}(收到 {file})';
|
|
196
196
|
readonly 'host.patchUnreadable': 'patch 层 {file} 读不出来({reason}):该层的 patch 行不会被导出/同步,也不会被改写';
|
|
197
197
|
readonly 'host.patchRefuseClobber': 'patch 层 {file} 解析失败({reason}):已拒绝覆盖写入,否则会把这层里读不出来的行整体删掉';
|
|
198
|
+
readonly 'host.patchRefusePromote': 'patch 层 {file} 的写入会把 {ids} 从 insert(新增)改成顶层行(覆盖):已拒绝写入,文件未改动(顶层行按 id 覆盖,查不到就整条丢弃)';
|
|
199
|
+
readonly 'host.patchSemanticDrift': 'patch 层 {file} 写回后结构自检不通过({detail}):已拒绝写入,文件未改动';
|
|
198
200
|
readonly 'host.fsPathEscape': '路径越界: {path}';
|
|
199
201
|
readonly 'host.activationRowFailed': '插件 {pkg} 已安装,但激活行写入 profile 补丁失败:{reason}(重启后该插件可能未激活;修复后重试安装即可幂等补行)';
|
|
200
202
|
readonly 'host.syncTokenWriteFailed': 'token 写入 DSH credentials 失败:{reason}(请在 DSH 凭据管理里配置 {ref} 后重试)';
|
package/lib/core/messages.js
CHANGED
|
@@ -184,6 +184,8 @@ export const zh = {
|
|
|
184
184
|
'host.patchUnsupported': 'dsh-config-manager 仅支持管理 {user} 与 profile 的 {profile}(收到 {file})',
|
|
185
185
|
'host.patchUnreadable': 'patch 层 {file} 读不出来({reason}):该层的 patch 行不会被导出/同步,也不会被改写',
|
|
186
186
|
'host.patchRefuseClobber': 'patch 层 {file} 解析失败({reason}):已拒绝覆盖写入,否则会把这层里读不出来的行整体删掉',
|
|
187
|
+
'host.patchRefusePromote': 'patch 层 {file} 的写入会把 {ids} 从 insert(新增)改成顶层行(覆盖):已拒绝写入,文件未改动(顶层行按 id 覆盖,查不到就整条丢弃)',
|
|
188
|
+
'host.patchSemanticDrift': 'patch 层 {file} 写回后结构自检不通过({detail}):已拒绝写入,文件未改动',
|
|
187
189
|
'host.fsPathEscape': '路径越界: {path}',
|
|
188
190
|
'host.activationRowFailed': '插件 {pkg} 已安装,但激活行写入 profile 补丁失败:{reason}(重启后该插件可能未激活;修复后重试安装即可幂等补行)',
|
|
189
191
|
'host.syncTokenWriteFailed': 'token 写入 DSH credentials 失败:{reason}(请在 DSH 凭据管理里配置 {ref} 后重试)',
|
|
@@ -697,6 +699,8 @@ export const en = {
|
|
|
697
699
|
'host.patchUnsupported': 'dsh-config-manager only manages {user} and the profile {profile} (received {file})',
|
|
698
700
|
'host.patchUnreadable': 'Cannot read patch layer {file} ({reason}): its patch rows are not exported or synced, and the file is left untouched',
|
|
699
701
|
'host.patchRefuseClobber': 'Refusing to rewrite patch layer {file}: it cannot be parsed ({reason}), and rewriting would delete every row that could not be read',
|
|
702
|
+
'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)',
|
|
703
|
+
'host.patchSemanticDrift': 'Structure self-check failed after serializing patch layer {file} ({detail}): the write was refused and the file is untouched',
|
|
700
704
|
'host.fsPathEscape': 'Path outside home directory: {path}',
|
|
701
705
|
'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)',
|
|
702
706
|
'host.syncTokenWriteFailed': 'Failed to write the token into DSH credentials: {reason} (configure {ref} in DSH credential management, then retry)',
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* patch 文档的**结构重建**(issue #133)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要独立一层:`cordis.patch.yml` 的两种形态语义**不等价**(宿主 dsh-app-boot 的
|
|
5
|
+
* readProfilePatches 合并循环):
|
|
6
|
+
* - 顶层 `- id: X` → 按 id **覆盖**已存在的条目;id 查不到时打 `patch: entry "X" not found` 丢弃;
|
|
7
|
+
* - `- insert:` 块成员 → **新增**条目(`data.push(...clone)`,唯一的新增路径)。
|
|
8
|
+
* 此前重建时把所有行(含 insert 成员)统一输出成顶层行,于是「只能靠 insert 新增」的条目
|
|
9
|
+
* (典型是 MCP server)在 DSH 侧全部失效 —— 一条 update 就够触发。
|
|
10
|
+
*
|
|
11
|
+
* 本模块只做**结构**:不读文件、不碰 YAML(core 层零依赖),输入是已解析的文档 + 变更清单,
|
|
12
|
+
* 输出是重建后的条目数组与两套 id。序列化与自检在宿主门面(DshPatchFileFacade)里做。
|
|
13
|
+
*
|
|
14
|
+
* 三条不变量:
|
|
15
|
+
* ① 原本是 insert 成员的 id,重建后仍在 insert 块里(update 不改变归属);
|
|
16
|
+
* ② 新增条目(`action:'insert'` 且文档里没有)落进 insert 块,而不是顶层行;
|
|
17
|
+
* ③ 任一 id 不得同时出现在顶层与 insert 两处 —— 计划阶段就抛错(防御性,正常路径不可达)。
|
|
18
|
+
*/
|
|
19
|
+
export interface PatchLineChange {
|
|
20
|
+
lineId: string;
|
|
21
|
+
raw: unknown;
|
|
22
|
+
action: 'insert' | 'update' | 'remove';
|
|
23
|
+
}
|
|
24
|
+
export interface PatchDocumentPlan {
|
|
25
|
+
/** 重建后的文档条目(顺序尽量贴原文档:顶层行原位、insert 块原位、新增项追加在尾部) */
|
|
26
|
+
items: unknown[];
|
|
27
|
+
/** 落在 insert 块里的 id(按输出顺序) */
|
|
28
|
+
insertIds: string[];
|
|
29
|
+
/** 落在顶层行里的 id(按输出顺序) */
|
|
30
|
+
topLevelIds: string[];
|
|
31
|
+
}
|
|
32
|
+
/** 计划会把 insert 成员提升成顶层行:调用方必须放弃这次写入(零写入)。 */
|
|
33
|
+
export declare class PatchPromoteRefusedError extends Error {
|
|
34
|
+
readonly ids: string[];
|
|
35
|
+
constructor(ids: string[]);
|
|
36
|
+
}
|
|
37
|
+
/** 读出文档里的 insert 成员 id 与顶层行 id(自检与测试共用;不判重、保持文档顺序)。 */
|
|
38
|
+
export declare function describePatchDocument(doc: unknown): {
|
|
39
|
+
insertIds: string[];
|
|
40
|
+
topLevelIds: string[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* 重建成 patch 文档条目。
|
|
44
|
+
*
|
|
45
|
+
* `doc` 是已解析的原文档(非数组视为空文档);顺序按原布局保留,新增项追加在尾部。
|
|
46
|
+
*/
|
|
47
|
+
export declare function planPatchDocument(doc: unknown, changes: readonly PatchLineChange[]): PatchDocumentPlan;
|
|
48
|
+
/**
|
|
49
|
+
* 计划布局与观察到的布局是否一致(写后自检用,issue #133)。
|
|
50
|
+
* 一致返回 `null`;不一致返回一句可直接进错误文案的差异说明(只含 id,不含值)。
|
|
51
|
+
*/
|
|
52
|
+
export declare function describePatchLayoutDrift(planned: {
|
|
53
|
+
insertIds: readonly string[];
|
|
54
|
+
topLevelIds: readonly string[];
|
|
55
|
+
}, observed: {
|
|
56
|
+
insertIds: readonly string[];
|
|
57
|
+
topLevelIds: readonly string[];
|
|
58
|
+
}): string | null;
|
|
59
|
+
/**
|
|
60
|
+
* 跨层提升检查(issue #133):`topLevelIds` 里哪些是**另一层文档**的 `- insert:` 成员。
|
|
61
|
+
*
|
|
62
|
+
* 命中就意味着「把新增改写成覆盖」——目标作用域里那条通常不存在,宿主会 `not found` 丢弃。
|
|
63
|
+
* `otherDoc` 读不到或解析不了时调用方传 `undefined`,本函数返回空(守卫只做加强)。
|
|
64
|
+
*/
|
|
65
|
+
export declare function promotedTopLevelIds(otherDoc: unknown, topLevelIds: readonly string[]): string[];
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/** 计划会把 insert 成员提升成顶层行:调用方必须放弃这次写入(零写入)。 */
|
|
2
|
+
export class PatchPromoteRefusedError extends Error {
|
|
3
|
+
ids;
|
|
4
|
+
constructor(ids) {
|
|
5
|
+
super('patch rebuild would promote insert members to top-level rows: ' + ids.join(', '));
|
|
6
|
+
this.name = 'PatchPromoteRefusedError';
|
|
7
|
+
this.ids = ids;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function entryId(value) {
|
|
11
|
+
if (value === null || typeof value !== 'object')
|
|
12
|
+
return null;
|
|
13
|
+
const id = value['id'];
|
|
14
|
+
return typeof id === 'string' && id !== '' ? id : null;
|
|
15
|
+
}
|
|
16
|
+
function blockMembers(value) {
|
|
17
|
+
if (value === null || typeof value !== 'object')
|
|
18
|
+
return null;
|
|
19
|
+
const insert = value['insert'];
|
|
20
|
+
if (!Array.isArray(insert))
|
|
21
|
+
return null;
|
|
22
|
+
return insert.filter((entry) => entry !== null && typeof entry === 'object');
|
|
23
|
+
}
|
|
24
|
+
/** 读出文档里的 insert 成员 id 与顶层行 id(自检与测试共用;不判重、保持文档顺序)。 */
|
|
25
|
+
export function describePatchDocument(doc) {
|
|
26
|
+
const insertIds = [];
|
|
27
|
+
const topLevelIds = [];
|
|
28
|
+
if (!Array.isArray(doc))
|
|
29
|
+
return { insertIds, topLevelIds };
|
|
30
|
+
for (const item of doc) {
|
|
31
|
+
const members = blockMembers(item);
|
|
32
|
+
if (members !== null) {
|
|
33
|
+
for (const member of members) {
|
|
34
|
+
const id = entryId(member);
|
|
35
|
+
if (id !== null)
|
|
36
|
+
insertIds.push(id);
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const id = entryId(item);
|
|
41
|
+
if (id !== null)
|
|
42
|
+
topLevelIds.push(id);
|
|
43
|
+
}
|
|
44
|
+
return { insertIds, topLevelIds };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 重建成 patch 文档条目。
|
|
48
|
+
*
|
|
49
|
+
* `doc` 是已解析的原文档(非数组视为空文档);顺序按原布局保留,新增项追加在尾部。
|
|
50
|
+
*/
|
|
51
|
+
export function planPatchDocument(doc, changes) {
|
|
52
|
+
const state = new Map();
|
|
53
|
+
const layout = [];
|
|
54
|
+
if (Array.isArray(doc)) {
|
|
55
|
+
for (const item of doc) {
|
|
56
|
+
const members = blockMembers(item);
|
|
57
|
+
if (members !== null) {
|
|
58
|
+
const ids = [];
|
|
59
|
+
for (const member of members) {
|
|
60
|
+
const id = entryId(member);
|
|
61
|
+
if (id === null)
|
|
62
|
+
continue;
|
|
63
|
+
ids.push(id);
|
|
64
|
+
if (!state.has(id))
|
|
65
|
+
state.set(id, { raw: member, inInsert: true });
|
|
66
|
+
}
|
|
67
|
+
if (ids.length > 0)
|
|
68
|
+
layout.push({ kind: 'block', ids });
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const id = entryId(item);
|
|
72
|
+
if (id === null)
|
|
73
|
+
continue;
|
|
74
|
+
layout.push({ kind: 'row', id });
|
|
75
|
+
if (!state.has(id))
|
|
76
|
+
state.set(id, { raw: item, inInsert: false });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const change of changes) {
|
|
80
|
+
if (change.action === 'remove') {
|
|
81
|
+
state.delete(change.lineId);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const previous = state.get(change.lineId);
|
|
85
|
+
// 归属只由**原文档**决定:更新一条既有 insert 成员不会把它搬到顶层;新增才按动作分流
|
|
86
|
+
const inInsert = previous !== undefined ? previous.inInsert : change.action === 'insert';
|
|
87
|
+
state.set(change.lineId, { raw: change.raw, inInsert });
|
|
88
|
+
}
|
|
89
|
+
const items = [];
|
|
90
|
+
const insertIds = [];
|
|
91
|
+
const topLevelIds = [];
|
|
92
|
+
const emitted = new Set();
|
|
93
|
+
const emitRow = (id) => {
|
|
94
|
+
const slot = state.get(id);
|
|
95
|
+
if (slot === undefined || emitted.has(id))
|
|
96
|
+
return;
|
|
97
|
+
emitted.add(id);
|
|
98
|
+
items.push(slot.raw);
|
|
99
|
+
topLevelIds.push(id);
|
|
100
|
+
};
|
|
101
|
+
const emitBlock = (ids) => {
|
|
102
|
+
const members = ids.filter((id) => {
|
|
103
|
+
const slot = state.get(id);
|
|
104
|
+
return slot !== undefined && slot.inInsert && !emitted.has(id);
|
|
105
|
+
});
|
|
106
|
+
if (members.length === 0)
|
|
107
|
+
return;
|
|
108
|
+
for (const id of members)
|
|
109
|
+
emitted.add(id);
|
|
110
|
+
items.push({ insert: members.map((id) => state.get(id).raw) });
|
|
111
|
+
insertIds.push(...members);
|
|
112
|
+
};
|
|
113
|
+
for (const slot of layout) {
|
|
114
|
+
if (slot.kind === 'row')
|
|
115
|
+
emitRow(slot.id);
|
|
116
|
+
else
|
|
117
|
+
emitBlock(slot.ids);
|
|
118
|
+
}
|
|
119
|
+
// 新写入、且原文档布局里没有的 id:insert 动作进块,update 动作保持旧的 upsert 语义(顶层行)
|
|
120
|
+
for (const [id, slot] of state) {
|
|
121
|
+
if (emitted.has(id))
|
|
122
|
+
continue;
|
|
123
|
+
if (slot.inInsert)
|
|
124
|
+
continue;
|
|
125
|
+
emitRow(id);
|
|
126
|
+
}
|
|
127
|
+
emitBlock([...state.keys()].filter((id) => !emitted.has(id)));
|
|
128
|
+
// 不变量③:同一 id 不得两种身份并存(正常路径不可达;坏了就拒写,别写出一份语义分裂的文件)
|
|
129
|
+
const promoted = insertIds.filter((id) => topLevelIds.includes(id));
|
|
130
|
+
if (promoted.length > 0)
|
|
131
|
+
throw new PatchPromoteRefusedError(promoted);
|
|
132
|
+
return { items, insertIds, topLevelIds };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 计划布局与观察到的布局是否一致(写后自检用,issue #133)。
|
|
136
|
+
* 一致返回 `null`;不一致返回一句可直接进错误文案的差异说明(只含 id,不含值)。
|
|
137
|
+
*/
|
|
138
|
+
export function describePatchLayoutDrift(planned, observed) {
|
|
139
|
+
const same = planned.insertIds.length === observed.insertIds.length
|
|
140
|
+
&& planned.topLevelIds.length === observed.topLevelIds.length
|
|
141
|
+
&& planned.insertIds.every((id, i) => id === observed.insertIds[i])
|
|
142
|
+
&& planned.topLevelIds.every((id, i) => id === observed.topLevelIds[i]);
|
|
143
|
+
if (same)
|
|
144
|
+
return null;
|
|
145
|
+
const fmt = (label, ids) => label + '=[' + ids.join(', ') + ']';
|
|
146
|
+
return 'planned ' + fmt('insert', planned.insertIds) + ' ' + fmt('top-level', planned.topLevelIds)
|
|
147
|
+
+ '; got ' + fmt('insert', observed.insertIds) + ' ' + fmt('top-level', observed.topLevelIds);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 跨层提升检查(issue #133):`topLevelIds` 里哪些是**另一层文档**的 `- insert:` 成员。
|
|
151
|
+
*
|
|
152
|
+
* 命中就意味着「把新增改写成覆盖」——目标作用域里那条通常不存在,宿主会 `not found` 丢弃。
|
|
153
|
+
* `otherDoc` 读不到或解析不了时调用方传 `undefined`,本函数返回空(守卫只做加强)。
|
|
154
|
+
*/
|
|
155
|
+
export function promotedTopLevelIds(otherDoc, topLevelIds) {
|
|
156
|
+
if (topLevelIds.length === 0)
|
|
157
|
+
return [];
|
|
158
|
+
const insertMembers = new Set(describePatchDocument(otherDoc).insertIds);
|
|
159
|
+
return topLevelIds.filter((id) => insertMembers.has(id));
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=patch-document.js.map
|
|
@@ -63,5 +63,16 @@ export declare function traeVscdbPaths(userDir: string, platform: ForeignPlatfor
|
|
|
63
63
|
*/
|
|
64
64
|
export declare function readTrae(opts: RootProbeOptions & {
|
|
65
65
|
readonly maxFiles?: number;
|
|
66
|
+
readonly maxEncryptedDbBytes?: number;
|
|
66
67
|
}): Promise<SessionReadOutcome<TraeSessionFile>>;
|
|
68
|
+
/**
|
|
69
|
+
* detect 期的**轻量预告**(issue #134 的第二半):让 `/foreign-sources` 在用户点导入前就把
|
|
70
|
+
* 「为什么这里没有会话」摆出来,而不是等 build 的 400 才闪一下。
|
|
71
|
+
*
|
|
72
|
+
* 代价(刻意接受):只读**页头**与 **ItemTable 的 key 列**,绝不读正文、绝不枚举会话 ——
|
|
73
|
+
* 4 个发行版的加密库最多各解密 1 页,旧库只是 `SELECT key`。因此 detect 仍是有界的只读探测。
|
|
74
|
+
*/
|
|
75
|
+
export declare function traeDetectSkips(opts: RootProbeOptions & {
|
|
76
|
+
readonly maxEncryptedDbBytes?: number;
|
|
77
|
+
}): Promise<ForeignSkip[]>;
|
|
67
78
|
export {};
|
package/lib/foreign/read-trae.js
CHANGED
|
@@ -21,6 +21,11 @@
|
|
|
21
21
|
* · 会话容器的字段级 schema **未取证**:本层用**结构自证**的窄口径(必须有「消息数组 + 至少一条
|
|
22
22
|
* 带 role/type 的消息」)识别会话,容器必须自带字符串 id(否则铸稳定兜底 id);cwd 只认明文
|
|
23
23
|
* 绝对路径字段,找不到就交给下游落 session-missing-cwd(**绝不猜 cwd**)。
|
|
24
|
+
*
|
|
25
|
+
* 2.3.x 起多了一条通道(issue #134):会话迁到 `<User>/ModularData/ai-agent/database.db`,
|
|
26
|
+
* **整库 SQLCipher 加密**。解密、临时明文、页头校验都在 trae-sqlcipher.ts(只交出中性行);
|
|
27
|
+
* 本文件只负责「读表结果 → 统一会话形态」的语义映射(role/正文/cwd 口径与旧通道**同一份代码**)。
|
|
28
|
+
* 两条通道按会话 id 去重、**先加密库后旧库**(迁移后的库才是权威),并存时只计一份。
|
|
24
29
|
*/
|
|
25
30
|
import { isAbsoluteFor, joinFor, normalizePlatform, vscodeUserDataDir } from './platform-paths.js';
|
|
26
31
|
import { isFile, listDirNames, statOrNull } from './session-read.js';
|
|
@@ -30,6 +35,7 @@ import { cellBytes, cellText, openSqliteIfShape, sqliteCapability } from './sqli
|
|
|
30
35
|
// SQLite 批的**唯一**时间口径(秒/毫秒自适应 + 安全整数)在本文件的共享模块里,不再抄第二份
|
|
31
36
|
import { msTime } from './read-opencode.js';
|
|
32
37
|
import { isRecord } from '../utils/guards.js';
|
|
38
|
+
import { probeTraeEncryptedDatabase, readTraeEncryptedDatabase, traeDbPathFor, traeDbSkipOf } from './trae-sqlcipher.js';
|
|
33
39
|
/** 四个发行版名(顺序 = chat-import traeUserDataDirs 的判定顺序) */
|
|
34
40
|
export const TRAE_PRODUCTS = ['Trae', 'Trae CN', 'TRAE SOLO CN', 'TRAE SOLO'];
|
|
35
41
|
export const TRAE_VSCDB_NAME = 'state.vscdb';
|
|
@@ -406,12 +412,66 @@ export async function readTrae(opts) {
|
|
|
406
412
|
extraCounts: { 'trae.vscdb': 0 },
|
|
407
413
|
};
|
|
408
414
|
}
|
|
415
|
+
const extraSkips = [];
|
|
416
|
+
let truncated = false;
|
|
417
|
+
// ① 加密库通道(2.3.x):先读 —— 迁移后的库才是权威,同 id 冲突以它为准
|
|
418
|
+
const encryptedDbs = [];
|
|
419
|
+
for (const userDir of traeUserDataDirs(opts)) {
|
|
420
|
+
const dbPath = traeDbPathFor(userDir, platform);
|
|
421
|
+
if (await isFile(dbPath))
|
|
422
|
+
encryptedDbs.push(dbPath);
|
|
423
|
+
}
|
|
424
|
+
let encryptedSessions = 0;
|
|
425
|
+
for (const dbPath of encryptedDbs) {
|
|
426
|
+
const outcome = await readTraeEncryptedDatabase(dbPath, {
|
|
427
|
+
env: opts.env,
|
|
428
|
+
platform,
|
|
429
|
+
...(opts.maxEncryptedDbBytes !== undefined ? { maxBytes: opts.maxEncryptedDbBytes } : {}),
|
|
430
|
+
maxSessions: maxFiles,
|
|
431
|
+
});
|
|
432
|
+
// 密钥错/超限/截断/表结构不符 ⇒ 稳定码(绝不静默 0 会话);旧库有货时继续走旧库
|
|
433
|
+
if (outcome.problem !== undefined) {
|
|
434
|
+
extraSkips.push(traeDbSkipOf(outcome.problem));
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (outcome.dropped !== undefined && outcome.dropped > 0) {
|
|
438
|
+
findings.push({ code: 'source-unreadable', origin: 'trae', detail: 'encrypted-row-json-error', count: outcome.dropped });
|
|
439
|
+
}
|
|
440
|
+
if (outcome.skipped !== undefined && outcome.skipped > 0) {
|
|
441
|
+
findings.push({ code: 'source-unreadable', origin: 'trae', detail: 'encrypted-session-without-id', count: outcome.skipped });
|
|
442
|
+
}
|
|
443
|
+
if (outcome.truncated === true)
|
|
444
|
+
truncated = true;
|
|
445
|
+
for (const session of outcome.sessions) {
|
|
446
|
+
if (files.length >= maxFiles) {
|
|
447
|
+
truncated = true;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
const id = isSafeIrId(session.id) ? session.id : safeFragment('trae-db-' + session.id);
|
|
451
|
+
if (seen.has(id))
|
|
452
|
+
continue;
|
|
453
|
+
const ignored = {};
|
|
454
|
+
const parsed = recordsOf(session.items, ignored);
|
|
455
|
+
seen.add(id);
|
|
456
|
+
files.push({
|
|
457
|
+
id,
|
|
458
|
+
cwd: session.cwd,
|
|
459
|
+
createdAt: session.createdAt,
|
|
460
|
+
title: session.title ?? '',
|
|
461
|
+
records: parsed.records,
|
|
462
|
+
raw: parsed.raw,
|
|
463
|
+
bad: 0,
|
|
464
|
+
ignored,
|
|
465
|
+
});
|
|
466
|
+
encryptedSessions += 1;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
// ② 旧 state.vscdb 通道(保留):同 id 已被加密库计入的不再重复
|
|
409
470
|
const dbs = [];
|
|
410
471
|
for (const userDir of traeUserDataDirs(opts)) {
|
|
411
472
|
dbs.push(...await traeVscdbPaths(userDir, platform));
|
|
412
473
|
}
|
|
413
474
|
let candidateKeys = 0;
|
|
414
|
-
let truncated = false;
|
|
415
475
|
for (const dbPath of dbs) {
|
|
416
476
|
if (files.length >= maxFiles) {
|
|
417
477
|
truncated = true;
|
|
@@ -471,8 +531,8 @@ export async function readTrae(opts) {
|
|
|
471
531
|
if (truncated)
|
|
472
532
|
break;
|
|
473
533
|
}
|
|
474
|
-
|
|
475
|
-
if (files.length === 0 && candidateKeys === 0 && dbs.length > 0) {
|
|
534
|
+
// 加密库存在时,「旧库没有候选键」不是用户需要的解释(真因已由 trae-db-* 说明)
|
|
535
|
+
if (files.length === 0 && candidateKeys === 0 && dbs.length > 0 && encryptedDbs.length === 0) {
|
|
476
536
|
extraSkips.push({ code: 'sessions-not-migrated', origin: 'trae', detail: 'chat-storage-key-not-found' });
|
|
477
537
|
}
|
|
478
538
|
else if (files.length === 0 && candidateKeys > 0) {
|
|
@@ -486,7 +546,67 @@ export async function readTrae(opts) {
|
|
|
486
546
|
files,
|
|
487
547
|
readFindings: findings,
|
|
488
548
|
extraSkips,
|
|
489
|
-
extraCounts: {
|
|
549
|
+
extraCounts: {
|
|
550
|
+
'trae.vscdb': dbs.length,
|
|
551
|
+
'trae.chatStorageKeys': candidateKeys,
|
|
552
|
+
'trae.encryptedDb': encryptedDbs.length,
|
|
553
|
+
'trae.encryptedSessions': encryptedSessions,
|
|
554
|
+
},
|
|
490
555
|
};
|
|
491
556
|
}
|
|
557
|
+
/**
|
|
558
|
+
* detect 期的**轻量预告**(issue #134 的第二半):让 `/foreign-sources` 在用户点导入前就把
|
|
559
|
+
* 「为什么这里没有会话」摆出来,而不是等 build 的 400 才闪一下。
|
|
560
|
+
*
|
|
561
|
+
* 代价(刻意接受):只读**页头**与 **ItemTable 的 key 列**,绝不读正文、绝不枚举会话 ——
|
|
562
|
+
* 4 个发行版的加密库最多各解密 1 页,旧库只是 `SELECT key`。因此 detect 仍是有界的只读探测。
|
|
563
|
+
*/
|
|
564
|
+
export async function traeDetectSkips(opts) {
|
|
565
|
+
const platform = normalizePlatform(opts.platform);
|
|
566
|
+
const skips = [];
|
|
567
|
+
let encryptedFound = 0;
|
|
568
|
+
for (const userDir of traeUserDataDirs(opts)) {
|
|
569
|
+
const dbPath = traeDbPathFor(userDir, platform);
|
|
570
|
+
if (!await isFile(dbPath))
|
|
571
|
+
continue;
|
|
572
|
+
encryptedFound += 1;
|
|
573
|
+
const problem = await probeTraeEncryptedDatabase(dbPath, {
|
|
574
|
+
env: opts.env,
|
|
575
|
+
...(opts.maxEncryptedDbBytes !== undefined ? { maxBytes: opts.maxEncryptedDbBytes } : {}),
|
|
576
|
+
});
|
|
577
|
+
if (problem !== null)
|
|
578
|
+
skips.push(traeDbSkipOf(problem));
|
|
579
|
+
}
|
|
580
|
+
// 加密库在场时不再猜旧库:迁移后旧库多半只是残留,混进「没有候选键」会误导用户
|
|
581
|
+
if (encryptedFound > 0)
|
|
582
|
+
return skips;
|
|
583
|
+
const capability = await sqliteCapability();
|
|
584
|
+
if (!capability.available)
|
|
585
|
+
return skips;
|
|
586
|
+
const dbs = [];
|
|
587
|
+
for (const userDir of traeUserDataDirs(opts)) {
|
|
588
|
+
dbs.push(...await traeVscdbPaths(userDir, platform));
|
|
589
|
+
}
|
|
590
|
+
if (dbs.length === 0)
|
|
591
|
+
return skips;
|
|
592
|
+
let candidateKeys = 0;
|
|
593
|
+
for (const dbPath of dbs) {
|
|
594
|
+
const db = await openSqliteIfShape(dbPath, { ItemTable: ['key'] });
|
|
595
|
+
if (db === null)
|
|
596
|
+
continue;
|
|
597
|
+
const rows = db.all('SELECT key FROM ItemTable');
|
|
598
|
+
db.close();
|
|
599
|
+
if (rows === null)
|
|
600
|
+
continue;
|
|
601
|
+
for (const row of rows) {
|
|
602
|
+
const key = cellText(row, 'key');
|
|
603
|
+
if (key !== undefined && isTraeChatStorageKey(key))
|
|
604
|
+
candidateKeys += 1;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (candidateKeys === 0) {
|
|
608
|
+
skips.push({ code: 'sessions-not-migrated', origin: 'trae', detail: 'chat-storage-key-not-found' });
|
|
609
|
+
}
|
|
610
|
+
return skips;
|
|
611
|
+
}
|
|
492
612
|
//# sourceMappingURL=read-trae.js.map
|
|
@@ -191,8 +191,14 @@ export interface SessionSourceWiring<TFile extends {
|
|
|
191
191
|
readonly probePaths: (opts: RootProbeOptions) => readonly string[];
|
|
192
192
|
/** 命中即报 source-location-overridden 的环境变量(只报键名,绝不报值) */
|
|
193
193
|
readonly probeEnvKeys?: readonly string[] | undefined;
|
|
194
|
-
/**
|
|
195
|
-
|
|
194
|
+
/**
|
|
195
|
+
* 探测期的附加提示(如 chatgpt 的「无自动根」)。
|
|
196
|
+
*
|
|
197
|
+
* 允许返回 Promise:少数来源需要**有界**地读一点内容才能给出可判定的预告(trae 的加密库
|
|
198
|
+
* 只解密第 1 页、旧库只读 key 列)。这类探测必须自证「有界、只读、不枚举会话」,
|
|
199
|
+
* 否则 detect 会被 30 个来源拖慢。
|
|
200
|
+
*/
|
|
201
|
+
readonly probeSkips?: ((opts: RootProbeOptions, probed: readonly string[]) => readonly ForeignSkip[] | Promise<readonly ForeignSkip[]>) | undefined;
|
|
196
202
|
readonly read: (opts: SessionSourceHostOptions) => Promise<SessionReadOutcome<TFile>>;
|
|
197
203
|
readonly draftOf: (file: TFile) => SessionDraft | {
|
|
198
204
|
readonly skip: {
|
|
@@ -205,8 +211,10 @@ export interface SessionSourceWiring<TFile extends {
|
|
|
205
211
|
/**
|
|
206
212
|
* 把一个会话来源装配成 `ForeignSource`。
|
|
207
213
|
*
|
|
208
|
-
* **detect
|
|
209
|
-
* detect,任何一处读全文都会把面板拖慢;真正的枚举发生在 build
|
|
214
|
+
* **detect 默认只 stat 真值表里的位置**(绝不读内容、绝不枚举会话)—— 界面对 30 个来源各调一次
|
|
215
|
+
* detect,任何一处读全文都会把面板拖慢;真正的枚举发生在 build。唯一例外是来源在 `probeSkips`
|
|
216
|
+
* 里显式声明的**有界轻量探测**(trae:加密库只解密第 1 页、旧库只读 key 列),因为它们要回答的
|
|
217
|
+
* 正是「为什么这里没有会话」,而这件事只有看内容才知道。
|
|
210
218
|
*
|
|
211
219
|
* platform 的兜底只在**这一处**:`ctx.platform ?? process.platform`(装配层碰运行平台是允许的,
|
|
212
220
|
* 真值表函数本身一律显式收参)。
|
|
@@ -400,8 +400,10 @@ export function recordIdOf(raw, index) {
|
|
|
400
400
|
/**
|
|
401
401
|
* 把一个会话来源装配成 `ForeignSource`。
|
|
402
402
|
*
|
|
403
|
-
* **detect
|
|
404
|
-
* detect,任何一处读全文都会把面板拖慢;真正的枚举发生在 build
|
|
403
|
+
* **detect 默认只 stat 真值表里的位置**(绝不读内容、绝不枚举会话)—— 界面对 30 个来源各调一次
|
|
404
|
+
* detect,任何一处读全文都会把面板拖慢;真正的枚举发生在 build。唯一例外是来源在 `probeSkips`
|
|
405
|
+
* 里显式声明的**有界轻量探测**(trae:加密库只解密第 1 页、旧库只读 key 列),因为它们要回答的
|
|
406
|
+
* 正是「为什么这里没有会话」,而这件事只有看内容才知道。
|
|
405
407
|
*
|
|
406
408
|
* platform 的兜底只在**这一处**:`ctx.platform ?? process.platform`(装配层碰运行平台是允许的,
|
|
407
409
|
* 真值表函数本身一律显式收参)。
|
|
@@ -425,7 +427,7 @@ export function sessionSourceOf(wiring) {
|
|
|
425
427
|
if (envValue(ctx.env, key) !== undefined)
|
|
426
428
|
skipped.push({ code: 'source-location-overridden', origin: key });
|
|
427
429
|
}
|
|
428
|
-
skipped.push(...(wiring.probeSkips?.(opts, probed.paths) ?? []));
|
|
430
|
+
skipped.push(...((await wiring.probeSkips?.(opts, probed.paths)) ?? []));
|
|
429
431
|
return { found: probed.paths.length > 0, paths: [...probed.paths], skipped };
|
|
430
432
|
},
|
|
431
433
|
async build(ctx) {
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ForeignPlatform } from './platform-paths.ts';
|
|
2
|
+
import type { ForeignSkip } from './types.ts';
|
|
3
|
+
/** 加密库相对 `<User>` 的路径段(4 个发行版共用同一命名) */
|
|
4
|
+
export declare const TRAE_DB_REL_SEGMENTS: readonly string[];
|
|
5
|
+
/** SQLCipher 4.x 的页大小(该库固定 4096) */
|
|
6
|
+
export declare const TRAE_DB_PAGE_SIZE = 4096;
|
|
7
|
+
/** 每页尾保留字节:IV(16) + HMAC-SHA512(64) */
|
|
8
|
+
export declare const TRAE_DB_RESERVE_BYTES = 80;
|
|
9
|
+
/** IV 长度(AES 块大小) */
|
|
10
|
+
export declare const TRAE_DB_IV_BYTES = 16;
|
|
11
|
+
/** 第 1 页开头的 salt 长度(不是密文,也不参与解密) */
|
|
12
|
+
export declare const TRAE_DB_SALT_BYTES = 16;
|
|
13
|
+
/** raw AES-256 密钥长度 */
|
|
14
|
+
export declare const TRAE_DB_KEY_BYTES = 32;
|
|
15
|
+
/**
|
|
16
|
+
* 缺省密钥:参考实现 xitula/trae-session-export 公开的 raw key 常量。
|
|
17
|
+
*
|
|
18
|
+
* **为什么可以内置**:它是产品级的固定 key(不是用户口令),参考实现公开分发;本插件只读、
|
|
19
|
+
* 且不写回任何 Trae 数据。换版后 key 变化 ⇒ 页头校验失败 ⇒ `trae-db-key-invalid`(可判定)。
|
|
20
|
+
*/
|
|
21
|
+
export declare const TRAE_DB_DEFAULT_KEY = "3605f6691095a993f03d5009c918352ef5be31ae31e8f000212b81ff058da773";
|
|
22
|
+
/** 覆盖密钥的环境变量名(只报键名,绝不回显值) */
|
|
23
|
+
export declare const TRAE_DB_KEY_ENV = "DCM_TRAE_DB_KEY";
|
|
24
|
+
/**
|
|
25
|
+
* 加密库的体积上限(**按来源独立**,2 GiB 覆盖 1.4 GB 量级;超限走既有 copy-too-large 语义)。
|
|
26
|
+
* 刻意不碰 sqlite.ts 的 `DEFAULT_MAX_COPY_BYTES`(1 GiB,全局兜底)。
|
|
27
|
+
*/
|
|
28
|
+
export declare const TRAE_DB_MAX_BYTES: number;
|
|
29
|
+
/** 会话表(调研确证) */
|
|
30
|
+
export declare const TRAE_DB_TABLE_SESSION = "chat_session";
|
|
31
|
+
/** 正文表(调研确证:消息在 `messages` 的 `raw_messages[]`) */
|
|
32
|
+
export declare const TRAE_DB_TABLE_HISTORY = "history_v2";
|
|
33
|
+
/** 表结构**最小**签名:只钉确证过的表与列,其余列按候选自证 */
|
|
34
|
+
export declare const TRAE_DB_REQUIRED_SCHEMA: Readonly<Record<string, readonly string[]>>;
|
|
35
|
+
/** 会话上限(与 read-trae 的 MAX_SESSIONS 同量级;由调用方再收窄) */
|
|
36
|
+
export declare const TRAE_DB_MAX_SESSIONS = 5000;
|
|
37
|
+
/** 解密/读表失败的**稳定码**(与 types.ts 的 ForeignSkipCode 一一对应) */
|
|
38
|
+
export type TraeDbProblemCode = 'trae-db-key-invalid' | 'trae-db-too-large' | 'trae-db-decrypt-failed' | 'trae-db-schema-mismatch';
|
|
39
|
+
/** 失败原因(`detail` 只放可枚举的短标签,绝不放密钥/明文片段) */
|
|
40
|
+
export interface TraeDbProblem {
|
|
41
|
+
readonly code: TraeDbProblemCode;
|
|
42
|
+
readonly detail: string;
|
|
43
|
+
readonly count?: number | undefined;
|
|
44
|
+
}
|
|
45
|
+
/** 读表交出的**中性**会话行(ID 校验与角色映射在 read-trae 里做,口径一处不改) */
|
|
46
|
+
export interface TraeDbSession {
|
|
47
|
+
readonly id: string;
|
|
48
|
+
readonly cwd?: string | undefined;
|
|
49
|
+
readonly title?: string | undefined;
|
|
50
|
+
readonly createdAt?: number | undefined;
|
|
51
|
+
readonly items: readonly unknown[];
|
|
52
|
+
}
|
|
53
|
+
/** 一次加密库读取的结果 */
|
|
54
|
+
export interface TraeDbReadOutcome {
|
|
55
|
+
readonly sessions: readonly TraeDbSession[];
|
|
56
|
+
readonly problem?: TraeDbProblem | undefined;
|
|
57
|
+
/** 解密出的页数(明文直读时为 undefined) */
|
|
58
|
+
readonly pages?: number | undefined;
|
|
59
|
+
/** `decrypted` = 走解密;`plaintext` = 库本身已是明文(旧版/已被解密的形态) */
|
|
60
|
+
readonly mode?: 'decrypted' | 'plaintext' | undefined;
|
|
61
|
+
/** 消息单元格解析不出的行数(如实计入,绝不伪造正文) */
|
|
62
|
+
readonly dropped?: number | undefined;
|
|
63
|
+
/** 缺 id 而无法建会话的行数 */
|
|
64
|
+
readonly skipped?: number | undefined;
|
|
65
|
+
/** 会话数触顶(调用方的 maxSessions) */
|
|
66
|
+
readonly truncated?: boolean | undefined;
|
|
67
|
+
}
|
|
68
|
+
/** 读表选项 */
|
|
69
|
+
export interface TraeDbReadOptions {
|
|
70
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
71
|
+
readonly platform: ForeignPlatform;
|
|
72
|
+
/** 体积上限(缺省 `TRAE_DB_MAX_BYTES`;单测用它造超限,不必造 2 GiB 文件) */
|
|
73
|
+
readonly maxBytes?: number | undefined;
|
|
74
|
+
/** 会话数上限(缺省 `TRAE_DB_MAX_SESSIONS`) */
|
|
75
|
+
readonly maxSessions?: number | undefined;
|
|
76
|
+
}
|
|
77
|
+
/** 密钥解析结果:要么给出 32 字节 raw key,要么给出可判定失败 */
|
|
78
|
+
export type TraeDbKeyResolution = {
|
|
79
|
+
readonly key: Buffer;
|
|
80
|
+
readonly source: 'default' | 'env';
|
|
81
|
+
} | {
|
|
82
|
+
readonly problem: TraeDbProblem;
|
|
83
|
+
};
|
|
84
|
+
/** 一个 `<User>` 根下的加密库位置(**绝不硬编码分隔符**) */
|
|
85
|
+
export declare function traeDbPathFor(userDir: string, platform: ForeignPlatform): string;
|
|
86
|
+
/**
|
|
87
|
+
* 解析密钥:环境变量优先,缺省内置常量。
|
|
88
|
+
*
|
|
89
|
+
* 环境变量**设了但不合法**时直接报 `trae-db-key-invalid`(detail=key-format),
|
|
90
|
+
* 绝不悄悄回落到内置 key —— 那会让「我换了密钥但没生效」变成静默 0 会话。
|
|
91
|
+
*/
|
|
92
|
+
export declare function resolveTraeDbKey(env: Readonly<Record<string, string | undefined>>): TraeDbKeyResolution;
|
|
93
|
+
/**
|
|
94
|
+
* 校验解密出的第 1 页明文(`plain` = 明文页的 `[16, 4016)`,即文件 offset 16 起的 4000 B)。
|
|
95
|
+
*
|
|
96
|
+
* 返回 null = 通过;返回字符串 = 可判定的失败 detail(一律映射成 `trae-db-key-invalid`)。
|
|
97
|
+
*/
|
|
98
|
+
export declare function validateTraePageOne(plain: Buffer, pages: number | null): string | null;
|
|
99
|
+
/**
|
|
100
|
+
* 把一个加密库逐页解密成明文库(**覆盖写 `destPath`**,目标不存在才写)。
|
|
101
|
+
*
|
|
102
|
+
* 只读源库;内存占用恒为一页;失败时返回稳定码,**不留半截明文**(由调用方的临时目录统一清理)。
|
|
103
|
+
*/
|
|
104
|
+
export declare function decryptTraeDatabase(sourcePath: string, destPath: string, key: Buffer): Promise<{
|
|
105
|
+
ok: true;
|
|
106
|
+
pages: number;
|
|
107
|
+
} | {
|
|
108
|
+
ok: false;
|
|
109
|
+
problem: TraeDbProblem;
|
|
110
|
+
}>;
|
|
111
|
+
/**
|
|
112
|
+
* 探测期的**单页**校验(detect 用):只解密第 1 页、只读 4 KB,用来抢在 build 之前把
|
|
113
|
+
* 「密钥错 / 截断 / 超限」这三类可判定失败摆到界面上(#134 的第二半)。
|
|
114
|
+
*
|
|
115
|
+
* 返回 null = 没发现问题(或库不存在 / 已是明文)。
|
|
116
|
+
*/
|
|
117
|
+
export declare function probeTraeEncryptedDatabase(sourcePath: string, options: Pick<TraeDbReadOptions, 'env' | 'maxBytes'>): Promise<TraeDbProblem | null>;
|
|
118
|
+
/** 正文容器 → 消息数组(`raw_messages` 第一顺位;数组本身也接受) */
|
|
119
|
+
export declare function messageItemsOf(value: unknown): unknown[] | null;
|
|
120
|
+
/**
|
|
121
|
+
* 读一个加密库:体积上限 → 密钥 → 解密(或明文直读)→ 只读读表 → 删临时明文。
|
|
122
|
+
*
|
|
123
|
+
* **绝不抛出**:所有失败都收成 `problem`(稳定码 + detail),调用方据此推 skip。
|
|
124
|
+
*/
|
|
125
|
+
export declare function readTraeEncryptedDatabase(sourcePath: string, options: TraeDbReadOptions): Promise<TraeDbReadOutcome>;
|
|
126
|
+
/** problem → skip(origin 固定 'trae';count 只在有意义时带上) */
|
|
127
|
+
export declare function traeDbSkipOf(problem: TraeDbProblem): ForeignSkip;
|