dsh-config-manager 0.8.1 → 0.9.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/mcp.js +42 -13
- package/lib/adapters/messages.d.ts +1 -0
- package/lib/adapters/messages.js +2 -0
- package/lib/adapters/patch-key-guard.d.ts +23 -0
- package/lib/adapters/patch-key-guard.js +13 -0
- package/lib/adapters/plugin-data.d.ts +19 -5
- package/lib/adapters/plugin-data.js +76 -22
- package/lib/adapters/plugins.d.ts +14 -1
- package/lib/adapters/plugins.js +147 -4
- package/lib/cli/actions.d.ts +7 -105
- package/lib/cli/actions.js +16 -172
- package/lib/cli/control-state.d.ts +54 -0
- package/lib/cli/control-state.js +107 -0
- package/lib/cli/index.js +14 -0
- package/lib/cli/write-gates.d.ts +61 -0
- package/lib/cli/write-gates.js +110 -0
- package/lib/client.d.ts +122 -50
- package/lib/client.js +12676 -12091
- package/lib/core/backup.d.ts +0 -4
- package/lib/core/backup.js +21 -4
- package/lib/core/boot-rescue.d.ts +1 -1
- package/lib/core/boot-rescue.js +12 -1
- package/lib/core/boot-safety.js +37 -2
- package/lib/core/conflict-decisions.js +2 -1
- package/lib/core/exporter.js +22 -1
- package/lib/core/journal.d.ts +54 -4
- package/lib/core/journal.js +158 -35
- package/lib/core/local-plugin-host.d.ts +29 -0
- package/lib/core/local-plugin-host.js +55 -48
- package/lib/core/messages.d.ts +7 -0
- package/lib/core/messages.js +14 -0
- package/lib/core/migration-history.js +16 -9
- package/lib/core/model-tools.d.ts +2 -2
- package/lib/core/model-tools.js +14 -2
- package/lib/core/patch-secret-guard.d.ts +49 -0
- package/lib/core/patch-secret-guard.js +164 -0
- package/lib/core/reconcile.d.ts +8 -0
- package/lib/core/reconcile.js +18 -3
- package/lib/core/reinstall.d.ts +2 -0
- package/lib/core/reinstall.js +6 -2
- package/lib/core/restore.js +31 -6
- package/lib/core/section-sanitize.d.ts +32 -0
- package/lib/core/section-sanitize.js +13 -0
- package/lib/core/snapshot-diff.d.ts +30 -0
- package/lib/core/snapshot-diff.js +56 -20
- package/lib/core/startup-barrier.js +8 -2
- package/lib/core/types.d.ts +22 -0
- package/lib/core/update-check.js +51 -2
- package/lib/foreign/kernel.js +9 -2
- package/lib/foreign/read-claude-plugins.js +29 -11
- package/lib/foreign/read-dsh.js +31 -2
- package/lib/foreign/read-trae.d.ts +11 -0
- package/lib/foreign/read-trae.js +124 -4
- package/lib/foreign/session-ir.js +50 -9
- package/lib/foreign/session-read.d.ts +8 -0
- package/lib/foreign/session-read.js +26 -0
- 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 +1 -1
- package/lib/index.js +1 -1
- package/lib/profiles/dsh-profile-io.d.ts +20 -0
- package/lib/profiles/dsh-profile-io.js +27 -0
- package/lib/profiles/dsh-profile-manager.d.ts +27 -0
- package/lib/profiles/dsh-profile-manager.js +45 -4
- package/lib/profiles/dsh-profile-runtime.d.ts +5 -1
- package/lib/profiles/dsh-profile-runtime.js +16 -3
- package/lib/profiles/process-control.d.ts +18 -1
- package/lib/profiles/process-control.js +25 -1
- package/lib/routes/profiles.js +2 -1
- package/lib/routes/sync.js +13 -1
- package/lib/schema/types.d.ts +11 -0
- package/lib/security/redaction.js +22 -6
- package/lib/sync/autosync-repeat.d.ts +27 -0
- package/lib/sync/autosync-repeat.js +23 -0
- package/lib/sync/autosync-scheduler.d.ts +34 -1
- package/lib/sync/autosync-scheduler.js +151 -43
- package/lib/sync/baseline-heal.js +7 -0
- package/lib/sync/baseline-record.d.ts +67 -0
- package/lib/sync/baseline-record.js +74 -0
- package/lib/sync/blob-store.js +13 -0
- package/lib/sync/content-diff.d.ts +39 -0
- package/lib/sync/content-diff.js +46 -0
- package/lib/sync/git/git-transport.d.ts +32 -1
- package/lib/sync/git/git-transport.js +146 -26
- package/lib/sync/github-auth.d.ts +17 -1
- package/lib/sync/github-auth.js +37 -1
- package/lib/sync/merge-apply-plan.d.ts +38 -0
- package/lib/sync/merge-apply-plan.js +76 -0
- package/lib/sync/messages.d.ts +1 -0
- package/lib/sync/messages.js +2 -0
- package/lib/sync/pending-merge.d.ts +34 -0
- package/lib/sync/pending-merge.js +53 -0
- package/lib/sync/s3/s3-transport.d.ts +14 -2
- package/lib/sync/s3/s3-transport.js +72 -48
- package/lib/sync/sync-engine.d.ts +23 -40
- package/lib/sync/sync-engine.js +115 -132
- package/lib/sync/sync-history.d.ts +7 -0
- package/lib/sync/sync-state.d.ts +28 -0
- package/lib/sync/sync-state.js +20 -0
- package/lib/sync/transport.d.ts +12 -3
- package/lib/sync/transport.js +13 -4
- package/lib/sync/webdav/webdav-transport.d.ts +19 -2
- package/lib/sync/webdav/webdav-transport.js +81 -40
- package/lib/ui/commands.js +5 -11
- package/lib/ui/dsh-profiles-view.d.ts +8 -3
- package/lib/ui/dsh-profiles-view.js +9 -5
- package/lib/ui/foreign-view.d.ts +19 -0
- package/lib/ui/foreign-view.js +24 -0
- package/lib/ui/i18n.d.ts +7 -1
- package/lib/ui/i18n.js +17 -2
- package/lib/ui/menu-placement.js +1 -1
- package/lib/ui/migration-consult-view.d.ts +11 -0
- package/lib/ui/migration-consult-view.js +15 -0
- package/lib/ui/overview-view.d.ts +7 -0
- package/lib/ui/overview-view.js +3 -0
- package/lib/ui/report.d.ts +9 -0
- package/lib/ui/report.js +17 -4
- package/lib/ui/selection-model.d.ts +8 -0
- package/lib/ui/selection-model.js +16 -0
- package/lib/utils/patch-yaml.d.ts +39 -1
- package/lib/utils/patch-yaml.js +82 -2
- package/lib/utils/paths.d.ts +13 -0
- package/lib/utils/paths.js +35 -0
- package/lib/utils/workdir-lock.js +18 -3
- package/package.json +2 -1
- package/src/adapters/mcp.test.ts +29 -0
- package/src/adapters/mcp.ts +43 -12
- package/src/adapters/messages.ts +2 -0
- package/src/adapters/patch-key-guard.ts +29 -0
- package/src/adapters/plugin-data.test.ts +126 -0
- package/src/adapters/plugin-data.ts +97 -26
- package/src/adapters/plugins.test.ts +288 -0
- package/src/adapters/plugins.ts +141 -4
- package/src/cli/actions.test.ts +74 -1
- package/src/cli/actions.ts +27 -246
- package/src/cli/control-state.ts +143 -0
- package/src/cli/index.ts +18 -0
- package/src/cli/web/web.test.ts +27 -0
- package/src/cli/write-gates.ts +149 -0
- package/src/client/about/AboutPanel.tsx +7 -6
- package/src/client/about/release-notes-view.test.ts +17 -0
- package/src/client/about/release-notes-view.ts +49 -24
- package/src/client/chat/ChatEntryView.tsx +2 -2
- package/src/client/chat/rollback-entry-view.ts +6 -1
- package/src/client/common/CommandPalette.tsx +6 -3
- package/src/client/common/ConfirmDialog.tsx +29 -12
- package/src/client/common/ContentPicker.tsx +13 -12
- package/src/client/common/Modal.tsx +12 -5
- package/src/client/common/MoreMenu.tsx +4 -1
- package/src/client/common/RunsCenter.tsx +103 -32
- package/src/client/common/Select.tsx +2 -2
- package/src/client/common/floating-bounds.ts +1 -1
- package/src/client/common/floating-units.test.ts +1 -2
- package/src/client/common/plan-text-redaction.test.ts +188 -3
- package/src/client/common/runs-center-guard.test.ts +60 -0
- package/src/client/common/runs-center-view.test.ts +128 -0
- package/src/client/common/runs-center-view.ts +96 -0
- package/src/client/common/section-title-action.test.ts +66 -0
- package/src/client/common/ui.tsx +93 -30
- package/src/client/config-manager.module.css +1507 -272
- package/src/client/consult/ConsultCard.tsx +7 -6
- package/src/client/environment/EnvironmentPanel.tsx +20 -21
- package/src/client/environment/maintenance/DiskUsageCard.tsx +65 -91
- package/src/client/environment/profiles-incomplete-ui.test.ts +8 -8
- package/src/client/export/ExportView.tsx +7 -16
- package/src/client/history/HistoryPanel.tsx +88 -28
- package/src/client/history/history-locales.ts +3 -3
- package/src/client/home/BackupScheduleCard.tsx +35 -30
- package/src/client/home/HomePanel.tsx +45 -10
- package/src/client/import/ConflictList.tsx +13 -13
- package/src/client/import/ForeignImportView.tsx +32 -13
- package/src/client/import/ImportWizardView.tsx +2 -3
- package/src/client/import/PathMappingForm.tsx +4 -4
- package/src/client/import/PathPickerDialog.tsx +2 -1
- package/src/client/import/foreign-import-view-guard.test.ts +1 -1
- package/src/client/import/import-wizard-steps.tsx +51 -39
- package/src/client/import/use-import-wizard-controller.test.ts +129 -0
- package/src/client/import/use-import-wizard-controller.ts +116 -22
- package/src/client/library/ArtifactRow.tsx +2 -2
- package/src/client/library/BackupInspectView.tsx +19 -15
- package/src/client/library/LibraryActions.tsx +56 -16
- package/src/client/library/LibraryPanel.tsx +7 -7
- package/src/client/library/RestorePlanView.tsx +13 -8
- package/src/client/locales/about.ts +0 -2
- package/src/client/locales/backup.ts +10 -12
- package/src/client/locales/common.ts +2 -0
- package/src/client/locales/export.ts +5 -12
- package/src/client/locales/foreign.ts +11 -2
- package/src/client/locales/import.ts +3 -2
- package/src/client/locales/overview.ts +4 -0
- package/src/client/locales/profiles.ts +32 -4
- package/src/client/locales/restore.ts +0 -2
- package/src/client/locales/sessions.ts +0 -2
- package/src/client/locales/shell.ts +2 -2
- package/src/client/market/MarketPanel.tsx +38 -20
- package/src/client/market/MyConfigsList.tsx +34 -32
- package/src/client/market/MyConfigsLoginCard.tsx +12 -15
- package/src/client/market/MyConfigsView.tsx +5 -5
- package/src/client/market/MyConfigsWizard.tsx +7 -12
- package/src/client/market/market-locales.ts +0 -6
- package/src/client/market/my-configs-split.test.ts +2 -1
- package/src/client/nav-menu-clip-guard.test.ts +52 -0
- package/src/client/recovery/IncompleteCopiesSection.tsx +1 -3
- package/src/client/recovery/RecoveryPanel.tsx +31 -16
- package/src/client/recovery/RollbackCard.tsx +58 -16
- package/src/client/recovery/RollbackFileChanges.tsx +40 -27
- package/src/client/recovery/SessionHealthCard.tsx +2 -2
- package/src/client/recovery/SessionHealthDialog.tsx +5 -7
- package/src/client/recovery/SessionLayoutSection.tsx +3 -5
- package/src/client/recovery/recovery-locales.ts +12 -8
- package/src/client/run-store.test.ts +30 -0
- package/src/client/run-store.ts +21 -1
- package/src/client/sync/AddChannelCard.tsx +65 -0
- package/src/client/sync/AutosyncCard.tsx +182 -108
- package/src/client/sync/ChannelClearConfirmDialog.tsx +5 -1
- package/src/client/sync/ChannelConfigDialog.tsx +27 -48
- package/src/client/sync/MergePreviewView.tsx +3 -5
- package/src/client/sync/PushReportSections.tsx +35 -0
- package/src/client/sync/SyncChannelEntryCard.tsx +33 -24
- package/src/client/sync/SyncConfirmView.tsx +3 -3
- package/src/client/sync/SyncLogList.tsx +16 -5
- package/src/client/sync/SyncPanel.tsx +153 -159
- package/src/client/sync/SyncProbeRow.tsx +5 -7
- package/src/client/sync/SyncRepositoryPicker.tsx +17 -4
- package/src/client/sync/SyncSecuritySection.tsx +157 -0
- package/src/client/sync/SyncSettingsDialog.tsx +304 -0
- package/src/client/sync/SyncSettingsView.test.ts +26 -8
- package/src/client/sync/WorkDirCard.tsx +2 -4
- package/src/client/sync/config-clear-guard.test.ts +94 -0
- package/src/client/sync/config-clear-guard.ts +98 -0
- package/src/client/sync/config-dirty.test.ts +91 -0
- package/src/client/sync/config-dirty.ts +88 -0
- package/src/client/sync/history-model.test.ts +2 -0
- package/src/client/sync/history-model.ts +3 -0
- package/src/client/sync/merge-request.ts +20 -0
- package/src/client/sync/sync-api.ts +12 -0
- package/src/client/sync/sync-locales.ts +87 -47
- package/src/client/sync/sync-panel-clear.test.ts +44 -0
- package/src/client/sync/sync-view.test.ts +20 -0
- package/src/client/sync/sync-view.ts +42 -4
- package/src/core/backup.test.ts +61 -1
- package/src/core/backup.ts +21 -4
- package/src/core/boot-rescue.test.ts +35 -0
- package/src/core/boot-rescue.ts +13 -2
- package/src/core/boot-safety.test.ts +25 -0
- package/src/core/boot-safety.ts +37 -2
- package/src/core/conflict-decisions.ts +2 -1
- package/src/core/exporter.ts +21 -1
- package/src/core/journal.test.ts +183 -0
- package/src/core/journal.ts +163 -25
- package/src/core/local-plugin-host.test.ts +68 -2
- package/src/core/local-plugin-host.ts +31 -4
- package/src/core/messages.ts +14 -0
- package/src/core/migration-history.test.ts +27 -0
- package/src/core/migration-history.ts +12 -4
- package/src/core/model-tools.test.ts +67 -0
- package/src/core/model-tools.ts +16 -4
- package/src/core/patch-secret-guard.test.ts +110 -0
- package/src/core/patch-secret-guard.ts +176 -0
- package/src/core/reconcile.ts +32 -4
- package/src/core/reinstall.ts +8 -2
- package/src/core/restore.ts +32 -7
- package/src/core/section-sanitize.ts +37 -0
- package/src/core/snapshot-diff.test.ts +73 -1
- package/src/core/snapshot-diff.ts +66 -21
- package/src/core/startup-barrier.test.ts +64 -0
- package/src/core/startup-barrier.ts +8 -2
- package/src/core/types.ts +27 -1
- package/src/core/update-check.test.ts +40 -0
- package/src/core/update-check.ts +53 -2
- package/src/foreign/claude-code.test.ts +5 -0
- package/src/foreign/claude-plugins.test.ts +57 -0
- package/src/foreign/claude-sessions-bytes.test.ts +7 -1
- package/src/foreign/dsh.test.ts +23 -0
- package/src/foreign/file-budget.test.ts +7 -1
- package/src/foreign/kernel.ts +9 -2
- package/src/foreign/read-claude-plugins.ts +32 -11
- package/src/foreign/read-dsh.ts +33 -2
- package/src/foreign/read-trae.ts +117 -5
- package/src/foreign/session-ir.test.ts +67 -0
- package/src/foreign/session-ir.ts +54 -14
- package/src/foreign/session-read.ts +23 -0
- 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.ts +1 -1
- package/src/profiles/dsh-profile-copy-rollback.test.ts +58 -4
- package/src/profiles/dsh-profile-io.ts +29 -0
- package/src/profiles/dsh-profile-manager.test.ts +30 -0
- package/src/profiles/dsh-profile-manager.ts +53 -5
- package/src/profiles/dsh-profile-runtime.test.ts +29 -7
- package/src/profiles/dsh-profile-runtime.ts +17 -3
- package/src/profiles/process-control.test.ts +97 -0
- package/src/profiles/process-control.ts +28 -1
- package/src/routes/profiles.ts +2 -1
- package/src/routes/sync.ts +13 -1
- package/src/schema/types.ts +16 -1
- package/src/security/redaction.test.ts +31 -0
- package/src/security/redaction.ts +23 -6
- package/src/sync/autosync-repeat.test.ts +58 -0
- package/src/sync/autosync-repeat.ts +55 -0
- package/src/sync/autosync-scheduler.test.ts +417 -0
- package/src/sync/autosync-scheduler.ts +154 -41
- package/src/sync/baseline-heal.ts +6 -0
- package/src/sync/baseline-record.ts +135 -0
- package/src/sync/blob-store.test.ts +16 -0
- package/src/sync/blob-store.ts +13 -0
- package/src/sync/content-diff.test.ts +95 -0
- package/src/sync/content-diff.ts +79 -0
- package/src/sync/git/git-transport.test.ts +151 -4
- package/src/sync/git/git-transport.ts +143 -27
- package/src/sync/github-auth.test.ts +26 -1
- package/src/sync/github-auth.ts +37 -1
- package/src/sync/merge-apply-plan.ts +117 -0
- package/src/sync/messages.ts +2 -0
- package/src/sync/pending-merge.ts +77 -0
- package/src/sync/s3/s3-delete-and-blob-index.test.ts +249 -0
- package/src/sync/s3/s3-transport.ts +76 -42
- package/src/sync/sync-config.test.ts +39 -12
- package/src/sync/sync-device-identity.test.ts +3 -0
- package/src/sync/sync-engine.test.ts +465 -12
- package/src/sync/sync-engine.ts +130 -135
- package/src/sync/sync-history.ts +7 -0
- package/src/sync/sync-state.ts +48 -0
- package/src/sync/transport.test.ts +13 -1
- package/src/sync/transport.ts +12 -4
- package/src/sync/webdav/webdav-delete-and-blob-index.test.ts +249 -0
- package/src/sync/webdav/webdav-transport.ts +86 -34
- package/src/ui/commands.test.ts +11 -0
- package/src/ui/commands.ts +5 -14
- package/src/ui/dsh-profiles-view.test.ts +16 -0
- package/src/ui/dsh-profiles-view.ts +11 -5
- package/src/ui/foreign-view.test.ts +36 -0
- package/src/ui/foreign-view.ts +46 -0
- package/src/ui/i18n.ts +17 -2
- package/src/ui/menu-placement.ts +1 -1
- package/src/ui/migration-consult-view.test.ts +20 -1
- package/src/ui/migration-consult-view.ts +17 -0
- package/src/ui/overview-view.ts +10 -0
- package/src/ui/report.test.ts +25 -1
- package/src/ui/report.ts +15 -4
- package/src/ui/selection-model.test.ts +15 -1
- package/src/ui/selection-model.ts +18 -0
- package/src/utils/js-yaml4.d.ts +35 -0
- package/src/utils/patch-yaml.test.ts +207 -1
- package/src/utils/patch-yaml.ts +98 -2
- package/src/utils/paths.test.ts +14 -1
- package/src/utils/paths.ts +35 -0
- package/src/utils/workdir-lock.test.ts +69 -0
- package/src/utils/workdir-lock.ts +17 -3
- package/lib/ui/path-mapping.d.ts +0 -38
- package/lib/ui/path-mapping.js +0 -75
- package/src/client/common/InfoHint.tsx +0 -289
- package/src/client/common/info-hint-guard.test.ts +0 -1119
- package/src/client/sync/DecryptPasswordCard.tsx +0 -48
- package/src/client/sync/SecurityOptionsCard.tsx +0 -97
- package/src/client/sync/SyncSectionPickerDialog.tsx +0 -213
- package/src/ui/path-mapping.test.ts +0 -67
- package/src/ui/path-mapping.ts +0 -103
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trae 2.3.x 加密会话库(SQLCipher 4.x 页面布局)的读盘层测试。
|
|
3
|
+
*
|
|
4
|
+
* 本机**没有安装 Trae**(也就没有真机样本)⇒ 夹具一律用 node:crypto **正向造出**
|
|
5
|
+
* SQLCipher 形态(4096 页、每页尾 80 B = IV16 + HMAC512、第 1 页前 16 B 是 salt),
|
|
6
|
+
* 再用 node:sqlite 建出的**真明文库**作为解密目标 —— 这样「解密 → 打开 → 读表」整条链
|
|
7
|
+
* 都是真跑,而不是断言我们自己写死的常量。
|
|
8
|
+
*/
|
|
9
|
+
import { test } from 'node:test';
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { createCipheriv, randomBytes } from 'node:crypto';
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
13
|
+
import os from 'node:os';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
16
|
+
|
|
17
|
+
import { normalizePlatform } from './platform-paths.ts';
|
|
18
|
+
import { sqliteCapability } from './sqlite.ts';
|
|
19
|
+
import {
|
|
20
|
+
TRAE_DB_DEFAULT_KEY,
|
|
21
|
+
TRAE_DB_KEY_ENV,
|
|
22
|
+
TRAE_DB_MAX_BYTES,
|
|
23
|
+
TRAE_DB_PAGE_SIZE,
|
|
24
|
+
TRAE_DB_RESERVE_BYTES,
|
|
25
|
+
decryptTraeDatabase,
|
|
26
|
+
messageItemsOf,
|
|
27
|
+
probeTraeEncryptedDatabase,
|
|
28
|
+
readTraeEncryptedDatabase,
|
|
29
|
+
resolveTraeDbKey,
|
|
30
|
+
traeDbPathFor,
|
|
31
|
+
traeDbSkipOf,
|
|
32
|
+
validateTraePageOne,
|
|
33
|
+
} from './trae-sqlcipher.ts';
|
|
34
|
+
|
|
35
|
+
const HOST = normalizePlatform(process.platform);
|
|
36
|
+
const CWD = HOST === 'win32' ? 'C:\\work\\trae' : '/work/trae';
|
|
37
|
+
const PAGE = TRAE_DB_PAGE_SIZE;
|
|
38
|
+
const RESERVE = TRAE_DB_RESERVE_BYTES;
|
|
39
|
+
|
|
40
|
+
async function tmpRoot(tag: string): Promise<string> {
|
|
41
|
+
return fs.mkdtemp(path.join(os.tmpdir(), 'dcm-' + tag + '-'));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function supportsSqlite(): Promise<boolean> {
|
|
45
|
+
return (await sqliteCapability()).available;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 造一个真实明文 SQLite 库(页面大小默认 4096,会话 2 条) */
|
|
49
|
+
function makePlainDb(file: string, options: { pageSize?: number; sessionIds?: readonly string[] } = {}): void {
|
|
50
|
+
const ids = options.sessionIds ?? ['s-1', 's-2'];
|
|
51
|
+
const db = new DatabaseSync(file);
|
|
52
|
+
try {
|
|
53
|
+
if (options.pageSize !== undefined) db.exec('PRAGMA page_size = ' + String(options.pageSize));
|
|
54
|
+
db.exec('PRAGMA journal_mode = DELETE');
|
|
55
|
+
db.exec('CREATE TABLE chat_session (id TEXT PRIMARY KEY, session_id TEXT, title TEXT, cwd TEXT, created_at INTEGER)');
|
|
56
|
+
db.exec('CREATE TABLE history_v2 (session_id TEXT, messages TEXT)');
|
|
57
|
+
const insertSession = db.prepare('INSERT INTO chat_session (id, session_id, title, cwd, created_at) VALUES (?, ?, ?, ?, ?)');
|
|
58
|
+
const insertHistory = db.prepare('INSERT INTO history_v2 (session_id, messages) VALUES (?, ?)');
|
|
59
|
+
for (const id of ids) {
|
|
60
|
+
insertSession.run(id, id, 'title-' + id, CWD, 1750000000000);
|
|
61
|
+
insertHistory.run(id, JSON.stringify({
|
|
62
|
+
raw_messages: [
|
|
63
|
+
{ role: 'user', content: 'hello ' + id },
|
|
64
|
+
{ role: 'assistant', content: [{ type: 'text', text: 'reply ' + id }] },
|
|
65
|
+
],
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
} finally {
|
|
69
|
+
db.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 把 node:sqlite 建的普通库机械重排成 **reserve 80 的明文库**(真实 SQLCipher 明文就是这个形状):
|
|
75
|
+
* 每页只有前 4016 B 可用,页尾 80 B 是保留区(本应是 IV/HMAC 的位置,明文里恒为 0)。
|
|
76
|
+
*
|
|
77
|
+
* 做法:内容区 [contentStart, pageSize) 整体上移 80 B,同步改 b-tree 头的 contentStart 与指针数组,
|
|
78
|
+
* 头里「每页保留字节数」(offset 20)写 80。只对无 freeblock / 无 overflow 的小库做(夹具满足)。
|
|
79
|
+
*/
|
|
80
|
+
function toReserve80Bytes(input: Buffer, pageSize = PAGE): Buffer {
|
|
81
|
+
const out = Buffer.from(input);
|
|
82
|
+
const pages = Math.floor(out.length / pageSize);
|
|
83
|
+
const usable = pageSize - RESERVE;
|
|
84
|
+
for (let index = 0; index < pages; index += 1) {
|
|
85
|
+
const base = index * pageSize;
|
|
86
|
+
const header = index === 0 ? 100 : 0;
|
|
87
|
+
const cellCount = out.readUInt16BE(base + header + 3);
|
|
88
|
+
const contentStart = out.readUInt16BE(base + header + 5);
|
|
89
|
+
if (contentStart === 0 || contentStart >= pageSize) continue; // 空页(内容区未使用)
|
|
90
|
+
out.copy(out, base + contentStart - RESERVE, base + contentStart, base + pageSize);
|
|
91
|
+
out.fill(0, base + usable, base + pageSize);
|
|
92
|
+
out.writeUInt16BE(contentStart - RESERVE, base + header + 5);
|
|
93
|
+
for (let cell = 0; cell < cellCount; cell += 1) {
|
|
94
|
+
const pointer = base + header + 8 + cell * 2;
|
|
95
|
+
out.writeUInt16BE(out.readUInt16BE(pointer) - RESERVE, pointer);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
out[20] = RESERVE;
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 造 SQLCipher 形态密文库(**正向加密**,与读盘层的解密互逆)。
|
|
104
|
+
*
|
|
105
|
+
* 明文先重排成 reserve 80 的形态(真实 SQLCipher 库的形状),再按页加密:
|
|
106
|
+
* page0:salt(16) + AES(明文页 [16,4016)) + IV(16) + HMAC(64 随机占位)
|
|
107
|
+
* pageN:AES(明文页 [0,4016)) + IV(16) + HMAC(64)
|
|
108
|
+
*/
|
|
109
|
+
async function encryptLikeSqlcipher(plainFile: string, outFile: string, keyHex: string, pageSize = PAGE): Promise<void> {
|
|
110
|
+
const key = Buffer.from(keyHex, 'hex');
|
|
111
|
+
const plain = toReserve80Bytes(await fs.readFile(plainFile), pageSize);
|
|
112
|
+
const pages = Math.ceil(plain.length / pageSize);
|
|
113
|
+
const out: Buffer[] = [];
|
|
114
|
+
const salt = randomBytes(16);
|
|
115
|
+
for (let index = 0; index < pages; index += 1) {
|
|
116
|
+
const start = index * pageSize;
|
|
117
|
+
const page = Buffer.alloc(pageSize);
|
|
118
|
+
plain.copy(page, 0, start, Math.min(start + pageSize, plain.length));
|
|
119
|
+
const usable = pageSize - RESERVE;
|
|
120
|
+
const payload = index === 0 ? page.subarray(16, usable) : page.subarray(0, usable);
|
|
121
|
+
const iv = randomBytes(16);
|
|
122
|
+
const cipher = createCipheriv('aes-256-cbc', key, iv);
|
|
123
|
+
cipher.setAutoPadding(false);
|
|
124
|
+
const encrypted = Buffer.concat([cipher.update(payload), cipher.final()]);
|
|
125
|
+
out.push(index === 0 ? salt : Buffer.alloc(0), encrypted, iv, randomBytes(64));
|
|
126
|
+
}
|
|
127
|
+
await fs.writeFile(outFile, Buffer.concat(out));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function envOf(extra: Record<string, string> = {}): Record<string, string | undefined> {
|
|
131
|
+
return { ...extra };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* ------------------------------------------------------------------ 用例 */
|
|
135
|
+
|
|
136
|
+
test('s1 夹具解出 chat_session / history_v2:会话数与正文条数与明文库一致', async () => {
|
|
137
|
+
if (!await supportsSqlite()) return; // 宿主无 node:sqlite:夹具本身造不出来
|
|
138
|
+
const root = await tmpRoot('trae-db-s1');
|
|
139
|
+
try {
|
|
140
|
+
const plain = path.join(root, 'plain.db');
|
|
141
|
+
makePlainDb(plain);
|
|
142
|
+
const enc = path.join(root, 'database.db');
|
|
143
|
+
await encryptLikeSqlcipher(plain, enc, TRAE_DB_DEFAULT_KEY);
|
|
144
|
+
const outcome = await readTraeEncryptedDatabase(enc, { env: envOf(), platform: HOST });
|
|
145
|
+
assert.equal(outcome.problem, undefined);
|
|
146
|
+
assert.equal(outcome.mode, 'decrypted');
|
|
147
|
+
assert.deepEqual(outcome.sessions.map((s) => s.id), ['s-1', 's-2']);
|
|
148
|
+
const first = outcome.sessions[0];
|
|
149
|
+
assert.ok(first !== undefined);
|
|
150
|
+
assert.equal(first.cwd, CWD);
|
|
151
|
+
assert.equal(first.title, 'title-s-1');
|
|
152
|
+
assert.equal(first.createdAt, 1750000000000);
|
|
153
|
+
// 消息容器 = raw_messages(调研确证的唯一形态)
|
|
154
|
+
const items = first.items;
|
|
155
|
+
assert.equal(items.length, 2);
|
|
156
|
+
assert.deepEqual(messageItemsOf({ raw_messages: items })?.length, 2);
|
|
157
|
+
assert.equal((items[0] as { role?: string }).role, 'user');
|
|
158
|
+
} finally {
|
|
159
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('s2 四类坏输入各给稳定码,且不抛未捕获异常、不留半截明文', async () => {
|
|
164
|
+
if (!await supportsSqlite()) return;
|
|
165
|
+
const root = await tmpRoot('trae-db-s2');
|
|
166
|
+
const before = (await fs.readdir(os.tmpdir())).filter((n) => n.startsWith('dcm-trae-db-'));
|
|
167
|
+
try {
|
|
168
|
+
const plain = path.join(root, 'plain.db');
|
|
169
|
+
makePlainDb(plain);
|
|
170
|
+
const good = path.join(root, 'good.db');
|
|
171
|
+
await encryptLikeSqlcipher(plain, good, TRAE_DB_DEFAULT_KEY);
|
|
172
|
+
|
|
173
|
+
// ① 错误密钥(换版 / 覆盖错)
|
|
174
|
+
const wrongKey = 'ab'.repeat(32);
|
|
175
|
+
const wrong = await readTraeEncryptedDatabase(good, { env: envOf({ [TRAE_DB_KEY_ENV]: wrongKey }), platform: HOST });
|
|
176
|
+
assert.equal(wrong.sessions.length, 0);
|
|
177
|
+
assert.equal(wrong.problem?.code, 'trae-db-key-invalid');
|
|
178
|
+
assert.equal(wrong.problem?.detail, 'page-size-mismatch');
|
|
179
|
+
|
|
180
|
+
// ② 截断(对齐但页数不足 / 不对齐都算可判定失败)
|
|
181
|
+
const truncated = path.join(root, 'truncated.db');
|
|
182
|
+
const bytes = await fs.readFile(good);
|
|
183
|
+
await fs.writeFile(truncated, bytes.subarray(0, PAGE));
|
|
184
|
+
const cut = await readTraeEncryptedDatabase(truncated, { env: envOf(), platform: HOST });
|
|
185
|
+
assert.equal(cut.problem?.code, 'trae-db-key-invalid');
|
|
186
|
+
assert.equal(cut.problem?.detail, 'page-count-mismatch');
|
|
187
|
+
const ragged = path.join(root, 'ragged.db');
|
|
188
|
+
await fs.writeFile(ragged, bytes.subarray(0, PAGE + 100));
|
|
189
|
+
const raggedOut = await readTraeEncryptedDatabase(ragged, { env: envOf(), platform: HOST });
|
|
190
|
+
assert.equal(raggedOut.problem?.code, 'trae-db-decrypt-failed');
|
|
191
|
+
assert.equal(raggedOut.problem?.detail, 'truncated-database');
|
|
192
|
+
|
|
193
|
+
// ③ 非加密库(随机字节,页面网格对齐)
|
|
194
|
+
const junk = path.join(root, 'junk.db');
|
|
195
|
+
await fs.writeFile(junk, randomBytes(PAGE * 2));
|
|
196
|
+
const junkOut = await readTraeEncryptedDatabase(junk, { env: envOf(), platform: HOST });
|
|
197
|
+
assert.equal(junkOut.problem?.code, 'trae-db-key-invalid');
|
|
198
|
+
|
|
199
|
+
// ④ 页大小不符(真实页面大小 8192,却按 4096 解)
|
|
200
|
+
const bigPlain = path.join(root, 'big.db');
|
|
201
|
+
makePlainDb(bigPlain, { pageSize: 8192, sessionIds: ['s-1'] });
|
|
202
|
+
const bigEnc = path.join(root, 'big-enc.db');
|
|
203
|
+
await encryptLikeSqlcipher(bigPlain, bigEnc, TRAE_DB_DEFAULT_KEY);
|
|
204
|
+
const bigOut = await readTraeEncryptedDatabase(bigEnc, { env: envOf(), platform: HOST });
|
|
205
|
+
assert.equal(bigOut.problem?.code, 'trae-db-key-invalid');
|
|
206
|
+
assert.equal(bigOut.problem?.detail, 'page-size-mismatch');
|
|
207
|
+
|
|
208
|
+
// 四次失败都不得留下半截明文:临时目录集合必须回到原样
|
|
209
|
+
const after = (await fs.readdir(os.tmpdir())).filter((n) => n.startsWith('dcm-trae-db-'));
|
|
210
|
+
assert.deepEqual(after.sort(), before.sort());
|
|
211
|
+
} finally {
|
|
212
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('s3 解密产物是 node:sqlite 可只读打开的明文库,且临时目录导入后不存在', async () => {
|
|
217
|
+
if (!await supportsSqlite()) return;
|
|
218
|
+
const root = await tmpRoot('trae-db-s3');
|
|
219
|
+
const before = (await fs.readdir(os.tmpdir())).filter((n) => n.startsWith('dcm-trae-db-'));
|
|
220
|
+
try {
|
|
221
|
+
const plain = path.join(root, 'plain.db');
|
|
222
|
+
makePlainDb(plain);
|
|
223
|
+
const enc = path.join(root, 'database.db');
|
|
224
|
+
await encryptLikeSqlcipher(plain, enc, TRAE_DB_DEFAULT_KEY);
|
|
225
|
+
const dest = path.join(root, 'decrypted.db');
|
|
226
|
+
const result = await decryptTraeDatabase(enc, dest, Buffer.from(TRAE_DB_DEFAULT_KEY, 'hex'));
|
|
227
|
+
assert.equal(result.ok, true);
|
|
228
|
+
const head = (await fs.readFile(dest)).subarray(0, 16).toString('latin1');
|
|
229
|
+
assert.equal(head, 'SQLite format 3\0');
|
|
230
|
+
const db = new DatabaseSync(dest, { readOnly: true });
|
|
231
|
+
try {
|
|
232
|
+
const rows = db.prepare('SELECT id FROM chat_session ORDER BY id').all() as { id: string }[];
|
|
233
|
+
assert.deepEqual(rows.map((r) => r.id), ['s-1', 's-2']);
|
|
234
|
+
} finally {
|
|
235
|
+
db.close();
|
|
236
|
+
}
|
|
237
|
+
// 读盘层自己造的一次性目录必须在结束后消失(明文不留在用户可见位置)
|
|
238
|
+
const outcome = await readTraeEncryptedDatabase(enc, { env: envOf(), platform: HOST });
|
|
239
|
+
assert.equal(outcome.sessions.length, 2);
|
|
240
|
+
const after = (await fs.readdir(os.tmpdir())).filter((n) => n.startsWith('dcm-trae-db-'));
|
|
241
|
+
assert.deepEqual(after.sort(), before.sort());
|
|
242
|
+
} finally {
|
|
243
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('s4 密钥:环境变量覆盖生效;格式非法给 key-invalid/key-format(绝不回落到内置)', async () => {
|
|
248
|
+
if (!await supportsSqlite()) return;
|
|
249
|
+
const root = await tmpRoot('trae-db-s4');
|
|
250
|
+
try {
|
|
251
|
+
const custom = 'cd'.repeat(32);
|
|
252
|
+
const plain = path.join(root, 'plain.db');
|
|
253
|
+
makePlainDb(plain, { sessionIds: ['env-1'] });
|
|
254
|
+
const enc = path.join(root, 'database.db');
|
|
255
|
+
await encryptLikeSqlcipher(plain, enc, custom);
|
|
256
|
+
|
|
257
|
+
// 缺省 key 解不开自定义 key 的库
|
|
258
|
+
const viaDefault = await readTraeEncryptedDatabase(enc, { env: envOf(), platform: HOST });
|
|
259
|
+
assert.equal(viaDefault.problem?.code, 'trae-db-key-invalid');
|
|
260
|
+
// 覆盖后解得开
|
|
261
|
+
const viaEnv = await readTraeEncryptedDatabase(enc, { env: envOf({ [TRAE_DB_KEY_ENV]: custom }), platform: HOST });
|
|
262
|
+
assert.equal(viaEnv.problem, undefined);
|
|
263
|
+
assert.deepEqual(viaEnv.sessions.map((s) => s.id), ['env-1']);
|
|
264
|
+
const resolved = resolveTraeDbKey(envOf({ [TRAE_DB_KEY_ENV]: custom }));
|
|
265
|
+
assert.equal('problem' in resolved ? null : resolved.source, 'env');
|
|
266
|
+
|
|
267
|
+
// 设了但不合法 ⇒ 可判定失败(不悄悄用内置 key 掩盖配置错误)
|
|
268
|
+
const bogus = resolveTraeDbKey(envOf({ [TRAE_DB_KEY_ENV]: 'not-a-hex-key' }));
|
|
269
|
+
assert.equal('problem' in bogus ? bogus.problem.code : null, 'trae-db-key-invalid');
|
|
270
|
+
assert.equal('problem' in bogus ? bogus.problem.detail : null, 'key-format');
|
|
271
|
+
} finally {
|
|
272
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('s5 表结构不符 / 超来源上限:各自稳定码(全局 1 GiB 未参与)', async () => {
|
|
277
|
+
if (!await supportsSqlite()) return;
|
|
278
|
+
const root = await tmpRoot('trae-db-s5');
|
|
279
|
+
try {
|
|
280
|
+
const plain = path.join(root, 'plain.db');
|
|
281
|
+
makePlainDb(plain);
|
|
282
|
+
const enc = path.join(root, 'database.db');
|
|
283
|
+
await encryptLikeSqlcipher(plain, enc, TRAE_DB_DEFAULT_KEY);
|
|
284
|
+
|
|
285
|
+
// 超限:把上限压到 1 页(夹具不必造 2 GiB),走 copy-too-large 语义
|
|
286
|
+
const tooLarge = await readTraeEncryptedDatabase(enc, { env: envOf(), platform: HOST, maxBytes: PAGE });
|
|
287
|
+
assert.equal(tooLarge.problem?.code, 'trae-db-too-large');
|
|
288
|
+
assert.equal(tooLarge.problem?.detail, 'copy-too-large');
|
|
289
|
+
assert.equal(TRAE_DB_MAX_BYTES > 1024 * 1024 * 1024, true);
|
|
290
|
+
|
|
291
|
+
// 表结构不符:解密成功但没有 history_v2
|
|
292
|
+
const partial = path.join(root, 'partial.db');
|
|
293
|
+
const db = new DatabaseSync(partial);
|
|
294
|
+
db.exec('CREATE TABLE chat_session (id TEXT PRIMARY KEY)');
|
|
295
|
+
db.exec("INSERT INTO chat_session (id) VALUES ('x')");
|
|
296
|
+
db.close();
|
|
297
|
+
const partialEnc = path.join(root, 'partial-enc.db');
|
|
298
|
+
await encryptLikeSqlcipher(partial, partialEnc, TRAE_DB_DEFAULT_KEY);
|
|
299
|
+
const schema = await readTraeEncryptedDatabase(partialEnc, { env: envOf(), platform: HOST });
|
|
300
|
+
assert.equal(schema.problem?.code, 'trae-db-schema-mismatch');
|
|
301
|
+
assert.equal(schema.problem?.detail, 'missing-table-history_v2');
|
|
302
|
+
|
|
303
|
+
// skip 形态:码 + origin=trae + detail(界面据此翻字典)
|
|
304
|
+
const skip = traeDbSkipOf({ code: 'trae-db-key-invalid', detail: 'page-size-mismatch', count: 3 });
|
|
305
|
+
assert.deepEqual(skip, { code: 'trae-db-key-invalid', origin: 'trae', detail: 'page-size-mismatch', count: 3 });
|
|
306
|
+
} finally {
|
|
307
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('s6 detect 期只解 1 页就能给出预告;库不存在时不报任何码', async () => {
|
|
312
|
+
if (!await supportsSqlite()) return;
|
|
313
|
+
const root = await tmpRoot('trae-db-s6');
|
|
314
|
+
try {
|
|
315
|
+
const plain = path.join(root, 'plain.db');
|
|
316
|
+
makePlainDb(plain);
|
|
317
|
+
const enc = path.join(root, 'database.db');
|
|
318
|
+
await encryptLikeSqlcipher(plain, enc, TRAE_DB_DEFAULT_KEY);
|
|
319
|
+
|
|
320
|
+
assert.equal(await probeTraeEncryptedDatabase(enc, { env: envOf() }), null);
|
|
321
|
+
const wrong = await probeTraeEncryptedDatabase(enc, { env: envOf({ [TRAE_DB_KEY_ENV]: 'ef'.repeat(32) }) });
|
|
322
|
+
assert.equal(wrong?.code, 'trae-db-key-invalid');
|
|
323
|
+
const huge = await probeTraeEncryptedDatabase(enc, { env: envOf(), maxBytes: 1 });
|
|
324
|
+
assert.equal(huge?.code, 'trae-db-too-large');
|
|
325
|
+
const missing = await probeTraeEncryptedDatabase(path.join(root, 'nope.db'), { env: envOf() });
|
|
326
|
+
assert.equal(missing, null);
|
|
327
|
+
|
|
328
|
+
// 明文库(旧版 / 已被第三方解密)不报码,读盘层走 plaintext 模式
|
|
329
|
+
const plainOut = await readTraeEncryptedDatabase(plain, { env: envOf(), platform: HOST });
|
|
330
|
+
assert.equal(plainOut.problem, undefined);
|
|
331
|
+
assert.equal(plainOut.mode, 'plaintext');
|
|
332
|
+
assert.equal(await probeTraeEncryptedDatabase(plain, { env: envOf() }), null);
|
|
333
|
+
} finally {
|
|
334
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test('s7 路径与页头校验的纯函数口径', () => {
|
|
339
|
+
assert.equal(traeDbPathFor('/home/u/Trae CN/User', 'linux'), '/home/u/Trae CN/User/ModularData/ai-agent/database.db');
|
|
340
|
+
assert.equal(
|
|
341
|
+
traeDbPathFor('C:\\Users\\u\\AppData\\Roaming\\Trae\\User', 'win32'),
|
|
342
|
+
'C:\\Users\\u\\AppData\\Roaming\\Trae\\User\\ModularData\\ai-agent\\database.db',
|
|
343
|
+
);
|
|
344
|
+
// 合法页头:页大小 4096、reserve 80、payload 分数 64/32/32、页数一致
|
|
345
|
+
const page = Buffer.alloc(4000);
|
|
346
|
+
page.writeUInt16BE(4096, 0);
|
|
347
|
+
page[4] = 80;
|
|
348
|
+
page[5] = 64;
|
|
349
|
+
page[6] = 32;
|
|
350
|
+
page[7] = 32;
|
|
351
|
+
page.writeUInt32BE(3, 12);
|
|
352
|
+
assert.equal(validateTraePageOne(page, 3), null);
|
|
353
|
+
assert.equal(validateTraePageOne(page, 4), 'page-count-mismatch');
|
|
354
|
+
page[4] = 0;
|
|
355
|
+
assert.equal(validateTraePageOne(page, 3), null); // 个别库不写 reserve:接受
|
|
356
|
+
page[4] = 8;
|
|
357
|
+
assert.equal(validateTraePageOne(page, 3), 'reserve-mismatch');
|
|
358
|
+
const shortPage = Buffer.alloc(8);
|
|
359
|
+
assert.equal(validateTraePageOne(shortPage, 1), 'short-first-page');
|
|
360
|
+
});
|