dsh-config-manager 0.11.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.
- package/lib/adapters/attachment-index-cache.d.ts +33 -0
- package/lib/adapters/attachment-index-cache.js +114 -0
- package/lib/adapters/attachments.d.ts +97 -0
- package/lib/adapters/attachments.js +448 -0
- package/lib/adapters/index.d.ts +1 -0
- package/lib/adapters/index.js +7 -1
- package/lib/adapters/sessions.d.ts +18 -1
- package/lib/adapters/sessions.js +193 -0
- package/lib/adapters/test-helpers.d.ts +7 -0
- package/lib/adapters/test-helpers.js +8 -1
- package/lib/adapters/workspaces.d.ts +0 -1
- package/lib/adapters/workspaces.js +244 -5
- package/lib/client.d.ts +119 -1
- package/lib/client.js +1537 -828
- package/lib/core/analyzer.d.ts +1 -14
- package/lib/core/analyzer.js +25 -67
- package/lib/core/backup-plan.js +4 -0
- package/lib/core/backup.js +3 -0
- package/lib/core/host-service.d.ts +12 -0
- package/lib/core/host-service.js +12 -0
- package/lib/core/host-session-shapes.d.ts +209 -0
- package/lib/core/host-session-shapes.js +134 -0
- package/lib/core/host-workspace-facade.d.ts +57 -0
- package/lib/core/host-workspace-facade.js +127 -0
- package/lib/core/import-planning.d.ts +15 -0
- package/lib/core/import-planning.js +44 -0
- package/lib/core/messages.d.ts +20 -0
- package/lib/core/messages.js +40 -0
- package/lib/core/session-analyze.d.ts +52 -0
- package/lib/core/session-analyze.js +135 -0
- package/lib/core/session-compare.d.ts +54 -0
- package/lib/core/session-compare.js +154 -0
- package/lib/core/session-meta.d.ts +32 -0
- package/lib/core/session-meta.js +49 -0
- package/lib/core/session-verdict-plan.d.ts +2 -0
- package/lib/core/session-verdict-plan.js +33 -0
- package/lib/core/types.d.ts +153 -0
- package/lib/core/unit-enrichment.d.ts +19 -0
- package/lib/core/unit-enrichment.js +39 -0
- package/lib/core/workspace-root.d.ts +46 -0
- package/lib/core/workspace-root.js +121 -0
- package/lib/index.d.ts +2 -112
- package/lib/index.js +15 -222
- package/lib/schema/section-registry.js +15 -0
- package/lib/schema/types.d.ts +17 -1
- package/lib/ui/export-flow.js +1 -0
- package/lib/ui/i18n.d.ts +1 -0
- package/lib/ui/i18n.js +2 -0
- package/lib/ui/import-wizard.d.ts +8 -0
- package/lib/ui/import-wizard.js +10 -0
- package/lib/ui/selection-model.d.ts +60 -0
- package/lib/ui/selection-model.js +258 -2
- package/lib/ui/session-compare-facts.d.ts +51 -0
- package/lib/ui/session-compare-facts.js +82 -0
- package/lib/ui/test-helpers.js +2 -2
- package/lib/utils/attachment-refs.d.ts +49 -0
- package/lib/utils/attachment-refs.js +169 -0
- package/package.json +1 -1
- package/src/adapters/attachment-index-cache.test.ts +122 -0
- package/src/adapters/attachment-index-cache.ts +129 -0
- package/src/adapters/attachments.test.ts +358 -0
- package/src/adapters/attachments.ts +472 -0
- package/src/adapters/index.ts +8 -1
- package/src/adapters/sessions.test.ts +140 -0
- package/src/adapters/sessions.ts +184 -1
- package/src/adapters/test-helpers.ts +8 -1
- package/src/adapters/workspaces.test.ts +254 -0
- package/src/adapters/workspaces.ts +234 -5
- package/src/client/common/ContentPicker.tsx +235 -15
- package/src/client/common/section-labels.test.ts +1 -1
- package/src/client/common/section-labels.ts +1 -0
- package/src/client/common/ui.tsx +17 -1
- package/src/client/config-manager.module.css +29 -0
- package/src/client/export/ExportView.tsx +15 -2
- package/src/client/import/ImportWizardView.tsx +8 -0
- package/src/client/import/PathMappingForm.tsx +49 -10
- package/src/client/import/import-wizard-steps.tsx +80 -3
- package/src/client/import/use-import-wizard-controller.ts +3 -0
- package/src/client/locales/common.ts +2 -0
- package/src/client/locales/export.ts +5 -0
- package/src/client/locales/import.ts +6 -0
- package/src/client/locales/sessions.ts +26 -0
- package/src/client/run-store-redact.ts +122 -0
- package/src/client/run-store.ts +13 -115
- package/src/core/analyzer.ts +38 -79
- package/src/core/backup-plan.ts +4 -0
- package/src/core/backup.ts +3 -0
- package/src/core/host-service.ts +17 -0
- package/src/core/host-session-shapes.ts +270 -0
- package/src/core/host-workspace-facade.ts +153 -0
- package/src/core/import-planning.ts +48 -0
- package/src/core/messages.ts +40 -0
- package/src/core/native-path-picker.test.ts +3 -2
- package/src/core/session-analyze.ts +159 -0
- package/src/core/session-compare-wiring.test.ts +207 -0
- package/src/core/session-compare.test.ts +119 -0
- package/src/core/session-compare.ts +185 -0
- package/src/core/session-meta.test.ts +25 -2
- package/src/core/session-meta.ts +54 -0
- package/src/core/session-verdict-plan.test.ts +70 -0
- package/src/core/session-verdict-plan.ts +32 -0
- package/src/core/types.ts +161 -0
- package/src/core/unit-enrichment.ts +46 -0
- package/src/core/workspace-root.test.ts +152 -0
- package/src/core/workspace-root.ts +146 -0
- package/src/foreign/trae-sqlcipher.test.ts +13 -4
- package/src/index.ts +25 -355
- package/src/schema/manifest-schema.test.ts +1 -1
- package/src/schema/registry.test.ts +7 -2
- package/src/schema/section-registry.ts +15 -0
- package/src/schema/types.ts +23 -2
- package/src/sync/transport.test.ts +1 -1
- package/src/ui/export-flow.ts +1 -0
- package/src/ui/i18n.ts +2 -0
- package/src/ui/import-wizard.ts +11 -0
- package/src/ui/selection-model.test.ts +265 -3
- package/src/ui/selection-model.ts +256 -2
- package/src/ui/session-compare-facts.test.ts +67 -0
- package/src/ui/session-compare-facts.ts +122 -0
- package/src/ui/test-helpers.ts +2 -2
- package/src/utils/attachment-refs.test.ts +98 -0
- package/src/utils/attachment-refs.ts +177 -0
package/lib/adapters/sessions.js
CHANGED
|
@@ -18,6 +18,9 @@ import { applyPathMapping } from '../core/path-mapping.js';
|
|
|
18
18
|
import { toNativePath } from '../utils/paths.js';
|
|
19
19
|
import { sha256Hex } from '../utils/hashing.js';
|
|
20
20
|
import { readLogHeaderFromBytes } from '../utils/session-log.js';
|
|
21
|
+
import { ZSTD_MAGIC, decodeZstdFrame, scanZstdFrames, zstdAvailable } from '../utils/zstd-frame.js';
|
|
22
|
+
import { extractAttachmentIds } from '../utils/attachment-refs.js';
|
|
23
|
+
import { aggregateSessionRows, canonicalEventLines, compareEventStreams } from '../core/session-compare.js';
|
|
21
24
|
import { toPosixRel } from './units.js';
|
|
22
25
|
/** 逐批并发 stat 的批大小:会话树可能有上千个日志文件,一次性开满 Promise 会打爆 libuv 线程池 */
|
|
23
26
|
const MTIME_BATCH = 16;
|
|
@@ -40,6 +43,42 @@ const writtenSessionFiles = new WeakMap();
|
|
|
40
43
|
* → `anyFailed` → 在 `rollbackOnError=true`(向导默认、同步通道恒 true)下**整笔导入回滚**。
|
|
41
44
|
* 与 `writtenSessionFiles` 同样以 ImportContext 为键:宿主每个进程只建一次 adapters。 */
|
|
42
45
|
const writtenSessionUnits = new WeakMap();
|
|
46
|
+
/**
|
|
47
|
+
* 会话日志字节 → 解码文本;解不出(缺 zstd 能力 / 容器损坏 / 帧撕裂)→ null(不猜)。
|
|
48
|
+
*
|
|
49
|
+
* 非 zstd 的明文日志(老代际)按 utf8 直接读:不认魔数就当明文,与附件扫描同一口径。
|
|
50
|
+
*/
|
|
51
|
+
function decodeLogText(bytes) {
|
|
52
|
+
const looksZstd = bytes.length >= 4
|
|
53
|
+
&& bytes[0] === 0x28 && bytes[1] === 0xb5 && bytes[2] === 0x2f && bytes[3] === 0xfd
|
|
54
|
+
&& ZSTD_MAGIC === 0xfd2fb528;
|
|
55
|
+
if (!looksZstd)
|
|
56
|
+
return Buffer.from(bytes).toString('utf8');
|
|
57
|
+
if (!zstdAvailable())
|
|
58
|
+
return null;
|
|
59
|
+
let frames;
|
|
60
|
+
try {
|
|
61
|
+
frames = scanZstdFrames(bytes).frames;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (frames.length === 0)
|
|
67
|
+
return null;
|
|
68
|
+
const parts = [];
|
|
69
|
+
for (const frame of frames) {
|
|
70
|
+
try {
|
|
71
|
+
parts.push(decodeZstdFrame(bytes.subarray(frame.start, frame.end)).toString('utf8'));
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return parts.join('\n');
|
|
78
|
+
}
|
|
79
|
+
/** 比对预算:条数 + 总字节(体检是分析阶段的附加只读动作,不许把导入前分析拖成分钟级)。 */
|
|
80
|
+
const COMPARE_MAX_UNITS = 200;
|
|
81
|
+
const COMPARE_MAX_BYTES = 64 * 1024 * 1024;
|
|
43
82
|
export class SessionsAdapter extends FileCollectionAdapter {
|
|
44
83
|
id = 'sessions';
|
|
45
84
|
displayName = 'Sessions';
|
|
@@ -445,6 +484,160 @@ export class SessionsAdapter extends FileCollectionAdapter {
|
|
|
445
484
|
}),
|
|
446
485
|
}];
|
|
447
486
|
}
|
|
487
|
+
/** 本机同名会话目录(两种命名形态都试);都没有 → null(= 包内是新增会话)。 */
|
|
488
|
+
async localSessionDir(ctx, unit) {
|
|
489
|
+
const parts = toPosixRel(unit).split('/');
|
|
490
|
+
const key = parts[0] ?? '';
|
|
491
|
+
const dir = parts[1] ?? '';
|
|
492
|
+
if (key === '' || dir === '')
|
|
493
|
+
return null;
|
|
494
|
+
const bare = sessionIdKey(dir);
|
|
495
|
+
const names = dir.startsWith('session-') ? [dir, bare] : ['session-' + bare, bare];
|
|
496
|
+
for (const name of names) {
|
|
497
|
+
if (name === '')
|
|
498
|
+
continue;
|
|
499
|
+
const rel = this.baseDir + '/' + key + '/' + name;
|
|
500
|
+
try {
|
|
501
|
+
if (await ctx.fs.exists(rel))
|
|
502
|
+
return rel;
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
// 探测失败 → 继续试下一个候选形态(不因一次 stat 失败就断言「本机没有」)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* P3「逐条会话比对」:包内会话 vs **本机同名会话**(只读,零写入)。
|
|
512
|
+
*
|
|
513
|
+
* 判据(详见 core/session-compare.ts 文件头):同名 = 同一 projectKey + 同一会话目录
|
|
514
|
+
* (两种目录命名形态都试);按**日志文件名**配对比较事件流,同一单元多份日志取最严重结论。
|
|
515
|
+
*
|
|
516
|
+
* 三条不猜的边界:
|
|
517
|
+
* - 包内日志解不出(容器损坏 / 缺 zstd 能力)→ 该单元不比对,绝不当作「新增」;
|
|
518
|
+
* - 本机目录在、但没有任何可配对的日志 → verdict=`unreadable`(不是 new);
|
|
519
|
+
* - 本机读失败 → `unreadable`;只有「目录形态都探不到」才是 `new`。
|
|
520
|
+
*
|
|
521
|
+
* 预算:条数与字节双上限(超限的单元计入 skipped,报告必须写明「另有 N 条未比对」)。
|
|
522
|
+
* 附件引用统计覆盖**全部**可解码单元(缺口数不能因预算被截断而少报)。
|
|
523
|
+
*/
|
|
524
|
+
async compareSessions(ctx, data) {
|
|
525
|
+
const files = Array.isArray(data.files) ? data.files : [];
|
|
526
|
+
if (files.length === 0)
|
|
527
|
+
return null;
|
|
528
|
+
const byUnit = new Map();
|
|
529
|
+
for (const file of files) {
|
|
530
|
+
const rel = toPosixRel(file.relativePath);
|
|
531
|
+
const name = rel.split('/').pop() ?? '';
|
|
532
|
+
if (!isSessionLogFileName(name))
|
|
533
|
+
continue;
|
|
534
|
+
const unit = toPosixRel(this.unitIdOf(rel));
|
|
535
|
+
const list = byUnit.get(unit) ?? [];
|
|
536
|
+
list.push({ relativePath: rel, data: file.data });
|
|
537
|
+
byUnit.set(unit, list);
|
|
538
|
+
}
|
|
539
|
+
if (byUnit.size === 0)
|
|
540
|
+
return null;
|
|
541
|
+
const units = [...byUnit.keys()].sort();
|
|
542
|
+
const referenced = new Set();
|
|
543
|
+
const rows = [];
|
|
544
|
+
let skipped = 0;
|
|
545
|
+
let bytes = 0;
|
|
546
|
+
for (const unit of units) {
|
|
547
|
+
const logs = (byUnit.get(unit) ?? []).slice()
|
|
548
|
+
.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
|
|
549
|
+
const packStreams = [];
|
|
550
|
+
let packEvents = 0;
|
|
551
|
+
// 引用统计与比对共用同一次解码:先解包内日志(预算判断在解码之后,避免「预算刚好用完」漏统计)
|
|
552
|
+
for (const log of logs) {
|
|
553
|
+
bytes += log.data.byteLength;
|
|
554
|
+
const text = decodeLogText(log.data);
|
|
555
|
+
if (text === null)
|
|
556
|
+
continue;
|
|
557
|
+
for (const id of extractAttachmentIds(text))
|
|
558
|
+
referenced.add(id);
|
|
559
|
+
const lines = canonicalEventLines(text);
|
|
560
|
+
packEvents += lines.length;
|
|
561
|
+
packStreams.push({ name: log.relativePath.split('/').pop() ?? '', lines });
|
|
562
|
+
}
|
|
563
|
+
if (packStreams.length === 0) {
|
|
564
|
+
// 解不出(容器损坏 / 缺 zstd 能力):不比对,绝不谎报 new;但必须计入未比对,
|
|
565
|
+
// 否则报告会静默少一条会话(「没结论」与「比对过且一致」必须分开)。
|
|
566
|
+
skipped += 1;
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (rows.length >= COMPARE_MAX_UNITS || bytes > COMPARE_MAX_BYTES) {
|
|
570
|
+
skipped += 1;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const localDir = await this.localSessionDir(ctx, unit);
|
|
574
|
+
if (localDir === null) {
|
|
575
|
+
rows.push({ unitId: unit, verdict: 'new', packEvents, localEvents: 0 });
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
let localNames;
|
|
579
|
+
try {
|
|
580
|
+
localNames = (await listFilesDetailed(ctx.fs, localDir)).paths
|
|
581
|
+
.map((entry) => entry.split(/[\\/]/).pop() ?? '')
|
|
582
|
+
.filter((name) => isSessionLogFileName(name))
|
|
583
|
+
.sort();
|
|
584
|
+
}
|
|
585
|
+
catch {
|
|
586
|
+
rows.push({ unitId: unit, verdict: 'unreadable', packEvents, localEvents: 0 });
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
if (localNames.length === 0) {
|
|
590
|
+
// 目录在、却没有会话日志:不是「新增」,也不是「一致」——如实说读不出可比对内容
|
|
591
|
+
rows.push({ unitId: unit, verdict: 'unreadable', packEvents, localEvents: 0 });
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
let pairs = packStreams.filter((stream) => localNames.includes(stream.name));
|
|
595
|
+
// 未配对的本地日志(本机多出一份代际日志)**必须参与判定**:只比同名文件会把
|
|
596
|
+
// 「本机整份日志都是新增」误判成 identical / packAhead,从而少报覆盖风险。
|
|
597
|
+
if (pairs.length < packStreams.length || pairs.length < localNames.length) {
|
|
598
|
+
if (packStreams.length === localNames.length) {
|
|
599
|
+
// 份数相同、名字不同(代际命名差异)⇒ 排序后一一对应,两侧内容都进比对
|
|
600
|
+
const sortedPack = [...packStreams].sort((a, b) => a.name.localeCompare(b.name));
|
|
601
|
+
pairs = sortedPack.map((stream, index) => ({ name: localNames[index] ?? '', lines: stream.lines }));
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
// 份数都对不上(本机多/少一份日志):无从对应,绝不猜成「一致」
|
|
605
|
+
rows.push({ unitId: unit, verdict: 'unreadable', packEvents, localEvents: 0 });
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const diffs = [];
|
|
610
|
+
let unreadable = false;
|
|
611
|
+
for (const pair of pairs) {
|
|
612
|
+
let localBytes;
|
|
613
|
+
try {
|
|
614
|
+
localBytes = await ctx.fs.readFile(localDir + '/' + pair.name);
|
|
615
|
+
}
|
|
616
|
+
catch {
|
|
617
|
+
unreadable = true;
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
bytes += localBytes.byteLength;
|
|
621
|
+
const localText = decodeLogText(localBytes);
|
|
622
|
+
if (localText === null) {
|
|
623
|
+
unreadable = true;
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
diffs.push(compareEventStreams(pair.lines, canonicalEventLines(localText)));
|
|
627
|
+
}
|
|
628
|
+
if (unreadable) {
|
|
629
|
+
rows.push({ unitId: unit, verdict: 'unreadable', packEvents, localEvents: 0 });
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
rows.push(aggregateSessionRows(unit, diffs));
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
rows,
|
|
636
|
+
skipped,
|
|
637
|
+
attachmentsReferenced: referenced.size,
|
|
638
|
+
attachmentsMissing: [...referenced].sort(),
|
|
639
|
+
};
|
|
640
|
+
}
|
|
448
641
|
async applyItem(item, ctx) {
|
|
449
642
|
const result = await super.applyItem(item, ctx);
|
|
450
643
|
// 只累计「本次导入(同一 ImportContext)」写成功的数量 —— 见 writtenSessionFiles 的说明。
|
|
@@ -111,6 +111,13 @@ export declare class MemWorkspace implements WorkspaceFacade {
|
|
|
111
111
|
records: Map<string, WorkspaceRecord>;
|
|
112
112
|
/** 可选(issue #45):测试按需注入的会话登记钩子;缺省维持「宿主不支持该能力」的旧行为。 */
|
|
113
113
|
attachSession?: WorkspaceFacade['attachSession'];
|
|
114
|
+
/** 可选(P2):归档集读取 / 归档动作 / 显示顺序;缺省 = 宿主不支持(老宿主行为)。 */
|
|
115
|
+
listArchivedSessionIds?: WorkspaceFacade['listArchivedSessionIds'];
|
|
116
|
+
archiveSession?: WorkspaceFacade['archiveSession'];
|
|
117
|
+
reorderWorkspaces?: WorkspaceFacade['reorderWorkspaces'];
|
|
118
|
+
/** 可选(P2-pin):置顶集读取 / 置顶动作;缺省 = 宿主不支持(旧宿主行为)。 */
|
|
119
|
+
listPinnedSessionIds?: WorkspaceFacade['listPinnedSessionIds'];
|
|
120
|
+
pinSession?: WorkspaceFacade['pinSession'];
|
|
114
121
|
listRecords(): Promise<WorkspaceRecord[]>;
|
|
115
122
|
writeRecord(r: WorkspaceRecord): Promise<void>;
|
|
116
123
|
removeRecord(id: string): Promise<void>;
|
|
@@ -182,6 +182,13 @@ export class MemWorkspace {
|
|
|
182
182
|
records = new Map();
|
|
183
183
|
/** 可选(issue #45):测试按需注入的会话登记钩子;缺省维持「宿主不支持该能力」的旧行为。 */
|
|
184
184
|
attachSession;
|
|
185
|
+
/** 可选(P2):归档集读取 / 归档动作 / 显示顺序;缺省 = 宿主不支持(老宿主行为)。 */
|
|
186
|
+
listArchivedSessionIds;
|
|
187
|
+
archiveSession;
|
|
188
|
+
reorderWorkspaces;
|
|
189
|
+
/** 可选(P2-pin):置顶集读取 / 置顶动作;缺省 = 宿主不支持(旧宿主行为)。 */
|
|
190
|
+
listPinnedSessionIds;
|
|
191
|
+
pinSession;
|
|
185
192
|
async listRecords() { return [...this.records.values()]; }
|
|
186
193
|
async writeRecord(r) { this.records.set(r.id, r); }
|
|
187
194
|
async removeRecord(id) { this.records.delete(id); }
|
|
@@ -318,7 +325,7 @@ export function makeManifest(platform = 'win32') {
|
|
|
318
325
|
settings: false, ui: false, providers: false, plugins: false, mcp: false, prompts: false,
|
|
319
326
|
skills: false, agentPresets: false, agentInstructions: false, workspaces: false, pluginFiles: false,
|
|
320
327
|
credentialsStatus: false, secrets: false, sessions: false, self: false,
|
|
321
|
-
pluginData: false, pluginAssets: false,
|
|
328
|
+
pluginData: false, pluginAssets: false, attachments: false,
|
|
322
329
|
},
|
|
323
330
|
security: { containsSecrets: false, encrypted: false, encryption: null },
|
|
324
331
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { MsgFunc } from '../core/messages.ts';
|
|
2
|
-
import './messages.ts';
|
|
3
2
|
import type { WorkspacesSection } from '../schema/types.ts';
|
|
4
3
|
import type { ApplyResult, ConfigAdapter, ExportOptions, ExportSection, ExportUnit, HostContext, ImportContext, PlanItem, ValidationResult } from '../core/types.ts';
|
|
5
4
|
/**
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
import { isDeepStrictEqual } from 'node:util';
|
|
8
8
|
import { normalizePath } from '../utils/paths.js';
|
|
9
9
|
import { zhMsg } from '../core/messages.js';
|
|
10
|
-
// t6:注册 adapters
|
|
11
|
-
import './messages.js';
|
|
10
|
+
// t6:注册 adapters 域追加目录;导出侧没有注入 msg(HostContext.msg 可选),故用本域 engineMsg。
|
|
11
|
+
import { engineMsg } from './messages.js';
|
|
12
12
|
import { validateJsonSection } from './json-section.js';
|
|
13
13
|
// issue #45 勾选联动第二判据:工作区 path 的 cwd 目录键(DSH 就是按它把会话显示在工作区下的)
|
|
14
14
|
import { projectKeyOf } from '../core/session-meta.js';
|
|
@@ -84,12 +84,20 @@ export class WorkspacesAdapter {
|
|
|
84
84
|
const records = await ctx.workspace.listRecords();
|
|
85
85
|
// Phase 1 条目级选择:单元 = 一条工作区记录(id 与导入侧 `workspace:<id>` 一致)
|
|
86
86
|
const allow = options.includeItems?.['workspaces'];
|
|
87
|
-
const kept = allow === undefined ? records : records.filter((r) => allow.includes(
|
|
87
|
+
const kept = allow === undefined ? records : records.filter((r) => allow.includes('workspace:' + r.id));
|
|
88
|
+
const archived = await readSessionIdSet(ctx, kept, 'archive');
|
|
89
|
+
const pinned = await readSessionIdSet(ctx, kept, 'pin');
|
|
88
90
|
return {
|
|
89
91
|
sectionId: 'workspaces',
|
|
90
|
-
data: {
|
|
92
|
+
data: {
|
|
93
|
+
version: 1,
|
|
94
|
+
workspaces: kept,
|
|
95
|
+
// 宿主没有归档/置顶能力 ⇒ 不带字段(「读不到」与「确实为空」必须分开)
|
|
96
|
+
...(archived.ids === undefined ? {} : { archivedSessionIds: archived.ids }),
|
|
97
|
+
...(pinned.ids === undefined ? {} : { pinnedSessionIds: pinned.ids }),
|
|
98
|
+
},
|
|
91
99
|
counts: { workspaces: kept.length },
|
|
92
|
-
warnings: [],
|
|
100
|
+
warnings: [...archived.warnings, ...pinned.warnings],
|
|
93
101
|
};
|
|
94
102
|
}
|
|
95
103
|
/** 单元清单(零 I/O):一条工作区记录 = 一个单元;标题缺失时退回 id。 */
|
|
@@ -259,6 +267,13 @@ export class WorkspacesAdapter {
|
|
|
259
267
|
message: note.message,
|
|
260
268
|
});
|
|
261
269
|
}
|
|
270
|
+
// P2:显示顺序与归档状态都在「工作区与会话都已落地」之后恢复
|
|
271
|
+
const order = await restoreWorkspaceOrder(ctx, data);
|
|
272
|
+
if (order !== undefined)
|
|
273
|
+
results.push(order);
|
|
274
|
+
// 置顶必须排在归档之前:宿主「归档即掉置顶」,所以两边都点了同一个会话时以归档为准
|
|
275
|
+
results.push(...(await restorePinSet(ctx, data)));
|
|
276
|
+
results.push(...(await restoreArchiveSet(ctx, data)));
|
|
262
277
|
return results;
|
|
263
278
|
}
|
|
264
279
|
async validate(data, msg = zhMsg) {
|
|
@@ -295,6 +310,25 @@ export class WorkspacesAdapter {
|
|
|
295
310
|
}
|
|
296
311
|
}
|
|
297
312
|
}
|
|
313
|
+
// P2 / P2-pin:两个**注册表级**会话集合同样来自不可信 ZIP。导入侧会对元素调
|
|
314
|
+
// sessionIdKey() 再逐个 archiveSession / pinSession,非字符串元素会在执行期抛内部
|
|
315
|
+
// TypeError(整包被拦、文案不可行动),所以在这里就按「元素必须是字符串」拦下。
|
|
316
|
+
for (const [field, value] of [
|
|
317
|
+
['archivedSessionIds', section.archivedSessionIds],
|
|
318
|
+
['pinnedSessionIds', section.pinnedSessionIds],
|
|
319
|
+
]) {
|
|
320
|
+
if (value === undefined)
|
|
321
|
+
continue;
|
|
322
|
+
if (!Array.isArray(value)) {
|
|
323
|
+
issues.push({ path: field, message: msg('adapter.validate.array', { subject: field }), severity: 'error' });
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
value.forEach((id, i) => {
|
|
327
|
+
if (typeof id !== 'string') {
|
|
328
|
+
issues.push({ path: `${field}[${i}]`, message: msg('adapter.validate.string', { subject: `${field}[${i}]` }), severity: 'error' });
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
298
332
|
});
|
|
299
333
|
}
|
|
300
334
|
}
|
|
@@ -433,4 +467,209 @@ async function registerImportedSessions(ctx, rec, units) {
|
|
|
433
467
|
const head = attached > 0 ? ctx.msg('adapter.workspaceSessionsAttached', { count: String(attached) }) : '';
|
|
434
468
|
return { message: compose(head), failed: 0 };
|
|
435
469
|
}
|
|
470
|
+
/**
|
|
471
|
+
* P2:读注册表级归档集,并收窄到「本次真的带着记录的工作区所声明的会话」。
|
|
472
|
+
*
|
|
473
|
+
* 收窄的理由:归档集是全局的,用户可能只勾了其中几个工作区。若把这些工作区之外的
|
|
474
|
+
* 归档 id 也写进包,导入侧就会去归档本机根本不该被这次导入碰到的会话 —— 失败告警还
|
|
475
|
+
* 会把人引向无关的排查方向。
|
|
476
|
+
*
|
|
477
|
+
* 宿主没有该能力 ⇒ ids 为 undefined(调用方据此**不写**字段),与「确实为空」区分开:
|
|
478
|
+
* 后者要写成 [],让缺字段 = 老包(导入侧不做任何归档动作)这个语义可辨。
|
|
479
|
+
*/
|
|
480
|
+
/**
|
|
481
|
+
* P2:读一个**注册表级**会话集合(归档集 / 置顶集),并收窄到本次真正导出的工作区。
|
|
482
|
+
*
|
|
483
|
+
* 为什么要收窄:两个集合都是全局的,若原样写进包,导入侧会去动「本机这条备份根本
|
|
484
|
+
* 没包含的工作区里的会话」——那是用户没授权的写入。只保留 kept 记录声明过的 sessionIds,
|
|
485
|
+
* 孤儿会话与未勾选工作区的会话一律不写进包。
|
|
486
|
+
*
|
|
487
|
+
* 收窄的另一面:记录的 sessionIds 覆盖率本来就低(真机 570 条会话只有 23 条在记录里),
|
|
488
|
+
* 所以包内可能比本机少 —— 这是「如实少带」,不是读失败。
|
|
489
|
+
*/
|
|
490
|
+
async function readSessionIdSet(ctx, kept, kind) {
|
|
491
|
+
const read = kind === 'archive' ? ctx.workspace.listArchivedSessionIds : ctx.workspace.listPinnedSessionIds;
|
|
492
|
+
if (read === undefined)
|
|
493
|
+
return { ids: undefined, warnings: [] };
|
|
494
|
+
try {
|
|
495
|
+
const raw = await read.call(ctx.workspace);
|
|
496
|
+
// undefined = 宿主有这个端口但读不到集合(getter 不存在 / 形状不认识)
|
|
497
|
+
if (raw === undefined)
|
|
498
|
+
return { ids: undefined, warnings: [] };
|
|
499
|
+
const declared = new Map();
|
|
500
|
+
for (const rec of kept) {
|
|
501
|
+
for (const sid of rec.sessionIds) {
|
|
502
|
+
const key = sessionIdKey(sid);
|
|
503
|
+
if (!declared.has(key))
|
|
504
|
+
declared.set(key, sid);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const ids = [];
|
|
508
|
+
const seen = new Set();
|
|
509
|
+
for (const id of raw) {
|
|
510
|
+
const key = sessionIdKey(id);
|
|
511
|
+
const local = declared.get(key);
|
|
512
|
+
if (local === undefined || seen.has(key))
|
|
513
|
+
continue;
|
|
514
|
+
seen.add(key);
|
|
515
|
+
ids.push(local);
|
|
516
|
+
}
|
|
517
|
+
return { ids, warnings: [] };
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
const key = kind === 'archive' ? 'adapter.workspaceArchiveUnreadable' : 'adapter.workspacePinUnreadable';
|
|
521
|
+
return { ids: undefined, warnings: [engineMsg(key, { msg: reasonOf(error) })] };
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* P2:把工作区显示顺序恢复成包里的顺序。
|
|
526
|
+
*
|
|
527
|
+
* 顺序不是数据:能力缺失或只有一条时什么都不做,失败也只告警(不该因为排序让整轮导入
|
|
528
|
+
* 变成「部分失败」)。未知 id 由门面自己过滤。
|
|
529
|
+
*/
|
|
530
|
+
async function restoreWorkspaceOrder(ctx, data) {
|
|
531
|
+
const reorder = ctx.target.workspace.reorderWorkspaces;
|
|
532
|
+
if (reorder === undefined)
|
|
533
|
+
return undefined;
|
|
534
|
+
const want = (data.workspaces ?? []).filter((rec) => rec !== null && typeof rec === 'object' && typeof rec.path === 'string');
|
|
535
|
+
if (want.length < 2)
|
|
536
|
+
return undefined;
|
|
537
|
+
// 包内 id 在新机上**不存在**:宿主 workspaceRegistry.create(path, title) 不接受 id,
|
|
538
|
+
// 新建记录必然是新 id。所以先把包内记录对齐到真实记录再排序 —— path(已过映射,与
|
|
539
|
+
// applyItem 落盘同源)优先,title 兜底;一条都对不上就如实告警,绝不静默当成功。
|
|
540
|
+
let actual;
|
|
541
|
+
try {
|
|
542
|
+
actual = await ctx.target.workspace.listRecords();
|
|
543
|
+
}
|
|
544
|
+
catch (error) {
|
|
545
|
+
return { ok: false, warning: true, message: ctx.msg('adapter.workspaceReorderFailed', { msg: reasonOf(error) }) };
|
|
546
|
+
}
|
|
547
|
+
const byPath = new Map();
|
|
548
|
+
const byTitle = new Map();
|
|
549
|
+
for (const rec of actual) {
|
|
550
|
+
byPath.set(normalizePath(rec.path), rec.id);
|
|
551
|
+
const title = rec.title ?? '';
|
|
552
|
+
if (title !== '' && !byTitle.has(title))
|
|
553
|
+
byTitle.set(title, rec.id);
|
|
554
|
+
}
|
|
555
|
+
const ids = [];
|
|
556
|
+
for (const rec of want) {
|
|
557
|
+
const id = byPath.get(normalizePath(rec.path)) ?? byTitle.get(rec.title ?? '');
|
|
558
|
+
if (id === undefined || ids.includes(id))
|
|
559
|
+
continue;
|
|
560
|
+
ids.push(id);
|
|
561
|
+
}
|
|
562
|
+
if (ids.length < 2) {
|
|
563
|
+
return { ok: false, warning: true, message: ctx.msg('adapter.workspaceReorderSkipped', { count: String(ids.length) }) };
|
|
564
|
+
}
|
|
565
|
+
try {
|
|
566
|
+
await reorder.call(ctx.target.workspace, ids);
|
|
567
|
+
return undefined;
|
|
568
|
+
}
|
|
569
|
+
catch (error) {
|
|
570
|
+
return { ok: false, warning: true, message: ctx.msg('adapter.workspaceReorderFailed', { msg: reasonOf(error) }) };
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* P2-pin:把置顶集恢复到目标机。
|
|
575
|
+
*
|
|
576
|
+
* 三个硬口径:
|
|
577
|
+
* 1. **倒序逐条置顶**:DSH 的 `pinSession()` 是前插(最近置顶排最前),包内顺序也是
|
|
578
|
+
* 「最近置顶在前」,所以必须从末尾往前置顶,落库后顺序才与包内一致。
|
|
579
|
+
* 2. **归档优先**:同一个会话同时出现在两个集合里(只可能来自手改或旧包),跳过置顶 ——
|
|
580
|
+
* 宿主「归档会掉置顶」,按归档处理才是宿主认可的终态。
|
|
581
|
+
* 3. 缺字段(老包)/ 空数组 ⇒ 一条动作都不做;宿主没有置顶能力 ⇒ 一条 warning,绝不静默。
|
|
582
|
+
*/
|
|
583
|
+
async function restorePinSet(ctx, data) {
|
|
584
|
+
const ids = data.pinnedSessionIds;
|
|
585
|
+
if (!Array.isArray(ids) || ids.length === 0)
|
|
586
|
+
return [];
|
|
587
|
+
const pin = ctx.target.workspace.pinSession;
|
|
588
|
+
if (pin === undefined) {
|
|
589
|
+
return [{ ok: false, warning: true, message: ctx.msg('host.workspacePinUnavailable') }];
|
|
590
|
+
}
|
|
591
|
+
const archived = new Set();
|
|
592
|
+
for (const id of data.archivedSessionIds ?? []) {
|
|
593
|
+
if (typeof id === 'string' && id !== '')
|
|
594
|
+
archived.add(sessionIdKey(id));
|
|
595
|
+
}
|
|
596
|
+
const want = ids.filter((id) => typeof id === 'string' && id !== '' && !archived.has(sessionIdKey(id)));
|
|
597
|
+
let pinned = 0;
|
|
598
|
+
const failed = [];
|
|
599
|
+
let firstReason = '';
|
|
600
|
+
for (const id of [...want].reverse()) {
|
|
601
|
+
try {
|
|
602
|
+
await pin.call(ctx.target.workspace, id);
|
|
603
|
+
pinned += 1;
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
failed.push(id);
|
|
607
|
+
if (firstReason === '')
|
|
608
|
+
firstReason = reasonOf(error);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const results = [];
|
|
612
|
+
if (pinned > 0) {
|
|
613
|
+
results.push({ ok: true, message: ctx.msg('adapter.workspacePinRestored', { count: String(pinned) }) });
|
|
614
|
+
}
|
|
615
|
+
if (failed.length > 0) {
|
|
616
|
+
results.push({
|
|
617
|
+
ok: false,
|
|
618
|
+
warning: true,
|
|
619
|
+
message: ctx.msg('adapter.workspacePinFailed', {
|
|
620
|
+
ids: failed.slice(0, 5).join(', '),
|
|
621
|
+
count: String(failed.length),
|
|
622
|
+
msg: firstReason,
|
|
623
|
+
}),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
return results;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* P2:把归档集恢复到目标机。
|
|
630
|
+
*
|
|
631
|
+
* 缺字段(老包)或空数组 ⇒ 一条动作都不做:绝不「对齐成空集」去反归档。
|
|
632
|
+
* 宿主没有归档能力 ⇒ 一条 warning(不是静默跳过),因为用户勾了这个能力就该知道没生效。
|
|
633
|
+
*/
|
|
634
|
+
async function restoreArchiveSet(ctx, data) {
|
|
635
|
+
const ids = data.archivedSessionIds;
|
|
636
|
+
if (!Array.isArray(ids) || ids.length === 0)
|
|
637
|
+
return [];
|
|
638
|
+
const archive = ctx.target.workspace.archiveSession;
|
|
639
|
+
if (archive === undefined) {
|
|
640
|
+
return [{ ok: false, warning: true, message: ctx.msg('host.workspaceArchiveUnavailable') }];
|
|
641
|
+
}
|
|
642
|
+
let archived = 0;
|
|
643
|
+
const failed = [];
|
|
644
|
+
let firstReason = '';
|
|
645
|
+
for (const id of ids) {
|
|
646
|
+
if (typeof id !== 'string' || id === '')
|
|
647
|
+
continue;
|
|
648
|
+
try {
|
|
649
|
+
await archive.call(ctx.target.workspace, id);
|
|
650
|
+
archived += 1;
|
|
651
|
+
}
|
|
652
|
+
catch (error) {
|
|
653
|
+
failed.push(id);
|
|
654
|
+
if (firstReason === '')
|
|
655
|
+
firstReason = reasonOf(error);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
const results = [];
|
|
659
|
+
if (archived > 0) {
|
|
660
|
+
results.push({ ok: true, message: ctx.msg('adapter.workspaceArchiveRestored', { count: String(archived) }) });
|
|
661
|
+
}
|
|
662
|
+
if (failed.length > 0) {
|
|
663
|
+
results.push({
|
|
664
|
+
ok: false,
|
|
665
|
+
warning: true,
|
|
666
|
+
message: ctx.msg('adapter.workspaceArchiveFailed', {
|
|
667
|
+
ids: failed.slice(0, 5).join(', '),
|
|
668
|
+
count: String(failed.length),
|
|
669
|
+
msg: firstReason,
|
|
670
|
+
}),
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
return results;
|
|
674
|
+
}
|
|
436
675
|
//# sourceMappingURL=workspaces.js.map
|