dsh-config-manager 0.4.2 → 0.5.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/README.md +24 -0
- package/README.zh-CN.md +10 -0
- package/lib/adapters/mcp.d.ts +9 -0
- package/lib/adapters/mcp.js +37 -0
- package/lib/adapters/plugins.js +10 -2
- package/lib/adapters/pnpm-workspace.js +1 -1
- package/lib/adapters/self.d.ts +2 -0
- package/lib/adapters/self.js +18 -2
- package/lib/adapters/workspaces.js +1 -1
- package/lib/cli/restore-resume.js +1 -1
- package/lib/cli/web/page.js +2 -2
- package/lib/client.d.ts +19 -41
- package/lib/client.js +863 -722
- package/lib/core/access-mode.d.ts +25 -0
- package/lib/core/access-mode.js +28 -0
- package/lib/core/backup.d.ts +11 -0
- package/lib/core/backup.js +27 -0
- package/lib/core/env-lock-lifecycle.d.ts +17 -3
- package/lib/core/env-lock-lifecycle.js +38 -0
- package/lib/core/external-failure.d.ts +9 -0
- package/lib/core/external-failure.js +50 -0
- package/lib/core/local-plugin-host.d.ts +9 -7
- package/lib/core/local-plugin-host.js +109 -15
- package/lib/core/local-plugin-pack.d.ts +20 -0
- package/lib/core/local-plugin-pack.js +18 -3
- package/lib/core/mcp-connector-store.d.ts +13 -0
- package/lib/core/mcp-connector-store.js +16 -1
- package/lib/core/messages-core.d.ts +45 -0
- package/lib/core/messages-core.js +92 -0
- package/lib/core/messages.d.ts +1 -0
- package/lib/core/messages.js +6 -0
- package/lib/core/model-tool-plans.d.ts +66 -0
- package/lib/core/model-tool-plans.js +128 -0
- package/lib/core/model-tools.d.ts +96 -8
- package/lib/core/model-tools.js +568 -31
- package/lib/core/phase3-host.js +10 -2
- package/lib/core/plugin-data-registry.d.ts +1 -1
- package/lib/core/plugin-data-registry.js +1 -1
- package/lib/core/restore-plan-fingerprint.d.ts +2 -0
- package/lib/core/restore-plan-fingerprint.js +18 -0
- package/lib/core/restore-resume.d.ts +1 -1
- package/lib/core/restore-resume.js +1 -1
- package/lib/core/restore.d.ts +5 -0
- package/lib/core/restore.js +4 -1
- package/lib/core/types.d.ts +12 -0
- package/lib/foreign/claude-sessions.d.ts +1 -1
- package/lib/foreign/claude-sessions.js +1 -1
- package/lib/foreign/registry.js +1 -1
- package/lib/foreign/types.d.ts +1 -1
- package/lib/index.d.ts +15 -15
- package/lib/index.js +65 -72
- package/lib/market/git-file-writer.js +1 -1
- package/lib/market/github-repos.js +1 -1
- package/lib/market/market-config.js +1 -1
- package/lib/market/messages-market.js +1 -1
- package/lib/market/my-repo.js +1 -1
- package/lib/market/prepare.js +1 -1
- package/lib/market/reader.js +2 -2
- package/lib/market/repo-url.d.ts +1 -1
- package/lib/market/security.js +2 -2
- package/lib/market/star-cache.d.ts +1 -1
- package/lib/market/types.d.ts +1 -1
- package/lib/market/types.js +1 -1
- package/lib/market/url.js +1 -1
- package/lib/routes/backup.js +17 -9
- package/lib/routes/index.d.ts +1 -1
- package/lib/routes/kit.d.ts +42 -3
- package/lib/routes/kit.js +43 -3
- package/lib/routes/market.d.ts +1 -1
- package/lib/routes/market.js +3 -60
- package/lib/routes/model-tools-deps.d.ts +41 -0
- package/lib/routes/model-tools-deps.js +18 -0
- package/lib/routes/recovery.js +5 -3
- package/lib/routes/request-trust.d.ts +28 -0
- package/lib/routes/request-trust.js +65 -0
- package/lib/routes/route-support.d.ts +18 -26
- package/lib/routes/route-support.js +19 -32
- package/lib/routes/snapshots.d.ts +1 -1
- package/lib/routes/snapshots.js +4 -40
- package/lib/routes/sync.js +40 -28
- package/lib/schema/index.d.ts +2 -2
- package/lib/schema/index.js +2 -2
- package/lib/schema/section-registry.d.ts +5 -3
- package/lib/schema/section-registry.js +7 -3
- package/lib/security/secret-scanner.js +1 -1
- package/lib/sync/apply-bundle.d.ts +50 -0
- package/lib/sync/apply-bundle.js +60 -0
- package/lib/sync/autosync-scheduler.d.ts +9 -0
- package/lib/sync/autosync-scheduler.js +118 -89
- package/lib/sync/backup-scheduler.js +5 -4
- package/lib/sync/blob-store.d.ts +2 -2
- package/lib/sync/git/git-transport.d.ts +12 -0
- package/lib/sync/git/git-transport.js +50 -2
- package/lib/sync/merge.d.ts +2 -0
- package/lib/sync/merge.js +28 -0
- package/lib/sync/messages.d.ts +4 -0
- package/lib/sync/messages.js +9 -2
- package/lib/sync/s3/s3-transport.d.ts +1 -1
- package/lib/sync/s3/s3-transport.js +1 -1
- package/lib/sync/sync-engine.d.ts +4 -4
- package/lib/sync/sync-engine.js +25 -26
- package/lib/sync/sync-scope-boot.d.ts +34 -0
- package/lib/sync/sync-scope-boot.js +71 -0
- package/lib/sync/sync-scope.d.ts +103 -0
- package/lib/sync/sync-scope.js +229 -0
- package/lib/sync/sync-selection.d.ts +17 -4
- package/lib/sync/sync-selection.js +40 -14
- package/lib/sync/transport.d.ts +4 -1
- package/lib/sync/transport.js +6 -0
- package/lib/sync/webdav/webdav-transport.js +1 -1
- package/lib/sync/workspace-merge.d.ts +36 -0
- package/lib/sync/workspace-merge.js +152 -0
- package/lib/ui/backup-schedule.d.ts +13 -2
- package/lib/ui/backup-schedule.js +3 -0
- package/lib/ui/snapshots-view.d.ts +16 -0
- package/lib/ui/snapshots-view.js +21 -0
- package/lib/utils/env-lock.d.ts +96 -0
- package/lib/utils/env-lock.js +332 -111
- package/lib/utils/messages-utils.d.ts +6 -0
- package/lib/utils/messages-utils.js +17 -1
- package/lib/utils/patch-yaml.js +1 -1
- package/lib/utils/zip.js +1 -1
- package/package.json +3 -2
- package/src/adapters/mcp.test.ts +28 -0
- package/src/adapters/mcp.ts +33 -0
- package/src/adapters/plugins.test.ts +49 -0
- package/src/adapters/plugins.ts +9 -1
- package/src/adapters/pnpm-workspace.ts +1 -1
- package/src/adapters/self.test.ts +39 -1
- package/src/adapters/self.ts +20 -2
- package/src/adapters/workspaces.ts +1 -1
- package/src/cli/restore-resume.ts +1 -1
- package/src/cli/web/page.ts +2 -2
- package/src/client/about/AboutPanel.tsx +8 -1
- package/src/client/about/about-view.test.ts +1 -1
- package/src/client/about/about-view.ts +1 -1
- package/src/client/api.ts +1 -6
- package/src/client/chat/chat-entry-view.test.ts +1 -1
- package/src/client/chat/chat-slots.ts +1 -1
- package/src/client/chat/rollback-diff-tab.ts +1 -1
- package/src/client/client-types.ts +1 -1
- package/src/client/common/access-mode.ts +21 -0
- package/src/client/common/routes.ts +1 -5
- package/src/client/css-scale-guard.test.ts +1 -1
- package/src/client/home/HomePanel.tsx +34 -6
- package/src/client/library/ArtifactRow.tsx +14 -2
- package/src/client/library/LibraryPanel.tsx +44 -0
- package/src/client/locales/about.ts +6 -0
- package/src/client/locales/overview.ts +18 -2
- package/src/client/locales/sessions.ts +3 -0
- package/src/client/market/MyConfigsView.tsx +1 -1
- package/src/client/market/market-api.ts +2 -14
- package/src/client/market/market-locales.ts +1 -1
- package/src/client/market/market-view.ts +3 -3
- package/src/client/market/my-configs-api.ts +1 -1
- package/src/client/market/my-configs-view.ts +1 -1
- package/src/client/recovery/recovery-locales.test.ts +1 -1
- package/src/client/run-store.ts +1 -1
- package/src/client/sync/SyncSectionPickerDialog.tsx +0 -5
- package/src/client/sync/history-model.test.ts +28 -7
- package/src/client/sync/history-model.ts +6 -3
- package/src/client/sync/sync-locales.ts +10 -4
- package/src/client/sync/sync-view.ts +1 -1
- package/src/core/access-mode.ts +37 -0
- package/src/core/analyzer-cache.test.ts +1 -1
- package/src/core/backup.test.ts +45 -1
- package/src/core/backup.ts +25 -0
- package/src/core/disk-usage.test.ts +2 -0
- package/src/core/env-lock-lifecycle.ts +33 -3
- package/src/core/external-failure.test.ts +60 -0
- package/src/core/external-failure.ts +54 -0
- package/src/core/integrity-extra-entries.test.ts +1 -1
- package/src/core/local-plugin-host.test.ts +134 -0
- package/src/core/local-plugin-host.ts +122 -17
- package/src/core/local-plugin-pack.ts +39 -3
- package/src/core/mcp-connector-store.ts +26 -1
- package/src/core/messages-core.ts +94 -0
- package/src/core/messages.ts +6 -0
- package/src/core/model-tool-plans.test.ts +111 -0
- package/src/core/model-tool-plans.ts +170 -0
- package/src/core/model-tools.test.ts +212 -12
- package/src/core/model-tools.ts +710 -38
- package/src/core/phase3-host.ts +10 -2
- package/src/core/phase3-production-integration.test.ts +43 -0
- package/src/core/plugin-data-registry.ts +1 -1
- package/src/core/restore-plan-fingerprint.ts +19 -0
- package/src/core/restore-resume.ts +1 -1
- package/src/core/restore.ts +9 -1
- package/src/core/types.ts +12 -0
- package/src/foreign/claude-sessions.ts +3 -3
- package/src/foreign/registry.ts +1 -1
- package/src/foreign/types.ts +1 -1
- package/src/index.ts +96 -103
- package/src/market/git-file-writer.ts +1 -1
- package/src/market/github-repos.test.ts +1 -1
- package/src/market/github-repos.ts +1 -1
- package/src/market/market-config.ts +1 -1
- package/src/market/messages-market.ts +1 -1
- package/src/market/my-repo.test.ts +1 -1
- package/src/market/my-repo.ts +1 -1
- package/src/market/prepare.test.ts +1 -1
- package/src/market/prepare.ts +1 -1
- package/src/market/reader.ts +2 -2
- package/src/market/repo-url.ts +1 -1
- package/src/market/security.ts +2 -2
- package/src/market/star-cache.ts +1 -1
- package/src/market/types.ts +1 -1
- package/src/market/url.test.ts +1 -1
- package/src/market/url.ts +1 -1
- package/src/profiles/dsh-profile-copy-rollback.test.ts +1 -1
- package/src/routes/backup.ts +16 -9
- package/src/routes/index.ts +1 -1
- package/src/routes/kit.ts +71 -5
- package/src/routes/market.ts +2 -59
- package/src/routes/model-tools-deps.ts +56 -0
- package/src/routes/recovery.ts +5 -3
- package/src/routes/request-trust.ts +97 -0
- package/src/routes/route-support.ts +49 -36
- package/src/routes/snapshots.ts +4 -40
- package/src/routes/sync.ts +39 -27
- package/src/schema/index.ts +2 -2
- package/src/schema/manifest-schema.test.ts +2 -2
- package/src/schema/registry.test.ts +1 -1
- package/src/schema/section-registry.ts +9 -4
- package/src/security/redaction.test.ts +1 -1
- package/src/security/secret-scanner.test.ts +2 -2
- package/src/security/secret-scanner.ts +1 -1
- package/src/sync/apply-bundle.ts +104 -0
- package/src/sync/autosync-scheduler.test.ts +180 -6
- package/src/sync/autosync-scheduler.ts +132 -92
- package/src/sync/backup-scheduler.test.ts +11 -4
- package/src/sync/backup-scheduler.ts +5 -4
- package/src/sync/blob-store.ts +2 -2
- package/src/sync/git/git-transport.test.ts +62 -0
- package/src/sync/git/git-transport.ts +54 -3
- package/src/sync/merge.ts +29 -0
- package/src/sync/messages.ts +9 -2
- package/src/sync/s3/s3-transport.ts +1 -1
- package/src/sync/sync-engine.test.ts +238 -2
- package/src/sync/sync-engine.ts +25 -26
- package/src/sync/sync-preview-push.test.ts +2 -1
- package/src/sync/sync-scope-boot.ts +87 -0
- package/src/sync/sync-scope.test.ts +154 -0
- package/src/sync/sync-scope.ts +256 -0
- package/src/sync/sync-selection.test.ts +76 -2
- package/src/sync/sync-selection.ts +40 -13
- package/src/sync/transport.test.ts +21 -1
- package/src/sync/transport.ts +12 -0
- package/src/sync/webdav/webdav-delete-index-unreadable.test.ts +2 -2
- package/src/sync/webdav/webdav-transport.ts +1 -1
- package/src/sync/workspace-merge.test.ts +110 -0
- package/src/sync/workspace-merge.ts +180 -0
- package/src/ui/backup-schedule.test.ts +5 -0
- package/src/ui/backup-schedule.ts +21 -2
- package/src/ui/session-issue-labels.ts +1 -1
- package/src/ui/snapshots-view.test.ts +32 -0
- package/src/ui/snapshots-view.ts +34 -0
- package/src/utils/env-lock.test.ts +242 -9
- package/src/utils/env-lock.ts +247 -13
- package/src/utils/messages-utils.ts +17 -1
- package/src/utils/patch-yaml.ts +1 -1
- package/src/utils/zip.ts +1 -1
- package/src/client/CLIENT_DEPENDENCIES.md +0 -94
package/lib/core/model-tools.js
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-config-manager — Agent 可调用的模型工具(P0-1)。
|
|
2
|
+
* dsh-config-manager — Agent 可调用的模型工具(P0-1 / D2–D6)。
|
|
3
3
|
*
|
|
4
|
-
* 在 host 半注册
|
|
5
|
-
* config_backup 全量备份到 exports
|
|
4
|
+
* 在 host 半注册 9 个 Cordis 模型工具,让 Agent 能自主驱动配置运维:
|
|
5
|
+
* config_backup 全量备份到 exports 目录(不含 secret;可选加密)
|
|
6
6
|
* config_list_snapshots 列出本地回滚快照
|
|
7
|
-
* config_restore 预览 /
|
|
7
|
+
* config_restore 预览 / 执行恢复到某快照(预览给 planId;执行必须带回来)
|
|
8
8
|
* config_sync_push 手动推送同步(写远端)
|
|
9
9
|
* config_sync_pull 拉取差异预览(零写入)
|
|
10
|
+
* config_sync_preview 三方合并预览(只读:出 planId + 分区裁决清单)
|
|
11
|
+
* config_sync_apply 按分区裁决落地合并(必须带预览给的 planId)
|
|
12
|
+
* config_import_plan 分析备份归档并生成导入计划(只读:零写入)
|
|
13
|
+
* config_import_apply 执行导入计划(必须带 planId + confirm:true)
|
|
10
14
|
*
|
|
11
15
|
* 设计遵循 AGENTS.md 铁律:
|
|
12
16
|
* - 所有业务逻辑为可独立测试的纯编排函数(createModelTools),
|
|
13
|
-
* 复用 src/core 已解耦引擎(Exporter / restore / SyncEngine),不重复实现;
|
|
17
|
+
* 复用 src/core 已解耦引擎(Exporter / restore / SyncEngine / Importer),不重复实现;
|
|
14
18
|
* - React / HTTP 壳不参与;本文件是「引擎侧编排」,不放浏览器 src/ui/;
|
|
15
19
|
* - ctx.tools 服务用可选读取(ctx.get)守卫:未组合 tools 的部署不注册、不崩溃。
|
|
16
20
|
*
|
|
17
21
|
* 安全不变量(硬约束,勿破坏):
|
|
18
|
-
* - secret
|
|
19
|
-
*
|
|
20
|
-
* -
|
|
21
|
-
* -
|
|
22
|
+
* - secret 值与密码永不进入工具入参/出参/render/日志;agent 拿不到任何凭据值
|
|
23
|
+
* (config_sync_push 的 includeSecrets 恒 false 且传 true 直接拒绝,导入不接受 secretInputs);
|
|
24
|
+
* - 同步密码优先从 DSH credentials 槽位读(入参 password 保留但 deprecated:模型不该把明文写进会话);
|
|
25
|
+
* - 预览与执行必须同一份计划:预览返回 planId(= 计划指纹),执行时按当前磁盘/远端重算比对,
|
|
26
|
+
* 不一致 → 拒绝且零写入(等价 GUI 恢复路由的 409 plan-changed);
|
|
27
|
+
* - 恢复 / 合并落地 / 导入执行一律过 GLOBAL mutation lock(与 GUI 同一条锁);
|
|
28
|
+
* - config_sync_pull / config_sync_preview / config_import_plan 零写入;
|
|
29
|
+
* - 同步侧永不触碰 sessions 分区(agent 构造引擎时永久剔除:会话内容不因一次工具调用被上下行)。
|
|
22
30
|
*/
|
|
23
31
|
import { engineMsg } from './messages-core.js';
|
|
24
32
|
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
25
33
|
import { randomBytes } from 'node:crypto';
|
|
26
|
-
import { join } from 'node:path';
|
|
34
|
+
import { join, resolve } from 'node:path';
|
|
27
35
|
import { Exporter } from './exporter.js';
|
|
28
36
|
import { listSnapshots, planRestore, restore as executeRestore } from './restore.js';
|
|
29
37
|
import { probeRestoreResume, runRestoreWithResume } from './restore-resume.js';
|
|
@@ -32,6 +40,9 @@ import { SECTION_IDS } from '../schema/config.js';
|
|
|
32
40
|
import { parseSyncChannel, readFullSyncConfig, readSyncConfigFor, SYNC_CHANNELS } from '../sync/sync-config.js';
|
|
33
41
|
import { runWithMutationLock } from '../utils/env-lock.js';
|
|
34
42
|
import { formatDateStamp } from '../utils/time.js';
|
|
43
|
+
import { isSameOrChild } from '../utils/paths.js';
|
|
44
|
+
import { restorePlanFingerprint } from './restore-plan-fingerprint.js';
|
|
45
|
+
import { importPlanFingerprint, ModelPlanStore, syncMergeFingerprint } from './model-tool-plans.js';
|
|
35
46
|
/** 快照 id 校验:拒绝路径分隔符与 `.`/`..`(防 join(snapshotsDir, id) 越界)。 */
|
|
36
47
|
function assertValidSnapshotId(snapshotId) {
|
|
37
48
|
if (snapshotId === '' || snapshotId.includes('/') || snapshotId.includes('\\')) {
|
|
@@ -57,8 +68,8 @@ function describeConfigIssues(issues, channel) {
|
|
|
57
68
|
return '';
|
|
58
69
|
return own.map((issue) => `config issue ${issue.code} (${issue.channel}): ${issue.message}`).join('; ');
|
|
59
70
|
}
|
|
60
|
-
/**
|
|
61
|
-
async function
|
|
71
|
+
/** 解析同步目标(通道 + 配置):channel 缺省取已配置的活动通道(git/webdav),未配置则抛可操作错误。 */
|
|
72
|
+
async function resolveSyncTarget(deps, channel) {
|
|
62
73
|
// SEC-03:两处读取都收集 issues —— 否则「地址被安全校验拒掉」会被报成
|
|
63
74
|
// 「sync-config.json 缺失或损坏」,让模型/用户反复重填同一个被拒地址。
|
|
64
75
|
const issues = [];
|
|
@@ -72,11 +83,171 @@ async function resolveEngine(deps, channel) {
|
|
|
72
83
|
// 无 issue = 真的没配(保持原文案);有 issue = 说清成因,绝不吞掉
|
|
73
84
|
throw new Error(detail === '' ? base : `${base} — ${detail}`);
|
|
74
85
|
}
|
|
75
|
-
return {
|
|
86
|
+
return { cfg, channel: ch };
|
|
87
|
+
}
|
|
88
|
+
/** 解析同步引擎(推送 / 拉取:缺省只同步 portable 分区,与改动前行为逐字节一致)。 */
|
|
89
|
+
async function resolveEngine(deps, channel) {
|
|
90
|
+
const target = await resolveSyncTarget(deps, channel);
|
|
91
|
+
return { engine: deps.makeSyncEngine(target.cfg), channel: target.channel };
|
|
92
|
+
}
|
|
93
|
+
/* ------------------------------------------------------------ 结构化结论与摘要 */
|
|
94
|
+
/** agent 侧永久排除的同步分区(sessions = 会话内容:路径/cwd/对话,绝不因一次工具调用被上下行)。 */
|
|
95
|
+
const AGENT_EXCLUDED_SECTIONS = ['sessions'];
|
|
96
|
+
function failure(code, messageKey, nextActionKey, retryable, params) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
code,
|
|
100
|
+
message: engineMsg(messageKey, params),
|
|
101
|
+
retryable,
|
|
102
|
+
nextAction: engineMsg(nextActionKey),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** agent 侧可用可选分区:用户放行的 'auto' 档 ∩ 非永久排除项(顺序不变,供引擎判档)。 */
|
|
106
|
+
function agentOptInSections(deps, channel) {
|
|
107
|
+
const allowed = deps.optInSections?.(channel) ?? [];
|
|
108
|
+
return allowed.filter((id) => !AGENT_EXCLUDED_SECTIONS.includes(id));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 导入 ZIP 必须落在受控根内:与 src/routes/route-support.ts 的 isControlledPath 同一判据
|
|
112
|
+
* (resolve + isSameOrChild)。roots 缺失 = 一律拒绝(fail-closed)。
|
|
113
|
+
*/
|
|
114
|
+
function isControlledZipPath(zipPath, roots) {
|
|
115
|
+
if (roots === undefined || roots.length === 0)
|
|
116
|
+
return false;
|
|
117
|
+
const target = resolve(zipPath);
|
|
118
|
+
return roots.some((root) => isSameOrChild(target, resolve(root)));
|
|
119
|
+
}
|
|
120
|
+
/** 导入计划摘要里返回的条数上限:整份 items 可能上百条,全量下发会淹掉模型上下文。 */
|
|
121
|
+
const MAX_PLAN_ITEMS_RETURNED = 50;
|
|
122
|
+
/** 分析摘要:只出判定 + 计数 + 文案(凭据只出 ref 名,值从不出现)。 */
|
|
123
|
+
function summarizeAnalysis(analysis) {
|
|
124
|
+
return {
|
|
125
|
+
valid: analysis.valid,
|
|
126
|
+
compatibility: analysis.compatibility,
|
|
127
|
+
encrypted: analysis.encrypted,
|
|
128
|
+
secretCount: analysis.secretCount,
|
|
129
|
+
sectionsInZip: analysis.sectionsInZip,
|
|
130
|
+
errorCount: analysis.errors.length,
|
|
131
|
+
errors: analysis.errors,
|
|
132
|
+
warnings: analysis.warnings,
|
|
133
|
+
pathIssueCount: analysis.pathIssues.length,
|
|
134
|
+
plugins: { installed: analysis.pluginSummary.installed, toInstall: analysis.pluginSummary.toInstall },
|
|
135
|
+
...(analysis.sessionFormats === undefined
|
|
136
|
+
? {}
|
|
137
|
+
: {
|
|
138
|
+
sessionFormats: {
|
|
139
|
+
target: analysis.sessionFormats.target,
|
|
140
|
+
unreadable: analysis.sessionFormats.unreadable.length,
|
|
141
|
+
sampled: analysis.sessionFormats.sampled,
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** 计划摘要:按 kind 计数 + 前 MAX_PLAN_ITEMS_RETURNED 条 + 缺口计数(itemsTruncated)。 */
|
|
147
|
+
function summarizePlan(plan) {
|
|
148
|
+
const counts = {};
|
|
149
|
+
for (const item of plan.items)
|
|
150
|
+
counts[item.kind] = (counts[item.kind] ?? 0) + 1;
|
|
151
|
+
const items = plan.items.slice(0, MAX_PLAN_ITEMS_RETURNED).map((item) => ({
|
|
152
|
+
id: item.id,
|
|
153
|
+
kind: item.kind,
|
|
154
|
+
adapter: item.adapter,
|
|
155
|
+
description: item.description,
|
|
156
|
+
severity: item.severity,
|
|
157
|
+
...(item.target === undefined ? {} : { target: item.target.ref }),
|
|
158
|
+
}));
|
|
159
|
+
return {
|
|
160
|
+
needsRestart: plan.needsRestart,
|
|
161
|
+
estimatedActions: plan.estimatedActions,
|
|
162
|
+
globalStrategy: plan.globalStrategy,
|
|
163
|
+
counts,
|
|
164
|
+
items,
|
|
165
|
+
itemsTruncated: plan.items.length - items.length,
|
|
166
|
+
missingSecrets: plan.missingSecrets.map((secret) => secret.ref),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** 执行结果摘要:ok/回滚/重启 + 计数(逐项明细留在 journal 与 GUI 运行中心)。 */
|
|
170
|
+
function summarizeImportResult(result) {
|
|
171
|
+
return {
|
|
172
|
+
ok: result.ok,
|
|
173
|
+
snapshotId: result.snapshotId ?? '',
|
|
174
|
+
needsRestart: result.needsRestart,
|
|
175
|
+
executedCount: result.executed.length,
|
|
176
|
+
missingSecrets: result.missingSecrets.length,
|
|
177
|
+
warnings: result.warnings,
|
|
178
|
+
rolledBack: result.rollback !== null,
|
|
179
|
+
...(result.rollback === null ? {} : { rollbackFull: result.rollback.full }),
|
|
180
|
+
...(result.ineffective === undefined ? {} : { ineffectiveCount: result.ineffective.length }),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 合并预览摘要:**不下发** localPreview / remotePreview / ancestorPreview 三列 ——
|
|
185
|
+
* 那是给 GUI 做逐键对比用的分段文本(每段 ≤2KiB),对模型只是噪声且可能带真实配置片段。
|
|
186
|
+
* 只给路径 + 冲突种类 + 原因,模型据此向用户转述「哪些分区要人裁决」。
|
|
187
|
+
*/
|
|
188
|
+
function summarizeMergePreview(preview) {
|
|
189
|
+
return {
|
|
190
|
+
snapshotId: preview.snapshotId,
|
|
191
|
+
hasAncestor: preview.hasAncestor,
|
|
192
|
+
summary: preview.summary,
|
|
193
|
+
sections: preview.sections.map((section) => ({
|
|
194
|
+
id: section.id,
|
|
195
|
+
decision: section.decision,
|
|
196
|
+
conflictCount: section.conflictCount,
|
|
197
|
+
...(section.autoMerged === undefined ? {} : { autoMerged: section.autoMerged }),
|
|
198
|
+
conflicts: section.conflicts.map((conflict) => ({
|
|
199
|
+
path: conflict.path,
|
|
200
|
+
kind: conflict.kind,
|
|
201
|
+
...(conflict.reason === undefined ? {} : { reason: conflict.reason }),
|
|
202
|
+
})),
|
|
203
|
+
})),
|
|
204
|
+
...(preview.message === undefined ? {} : { message: preview.message }),
|
|
205
|
+
...(preview.reason === undefined ? {} : { reason: preview.reason }),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/* ------------------------------------------------------------ 裁决值归一(D5/D6) */
|
|
209
|
+
const MERGE_ACTIONS = ['keepLocal', 'useRemote', 'useMerged', 'skip'];
|
|
210
|
+
function isMergeAction(value) {
|
|
211
|
+
return MERGE_ACTIONS.includes(value);
|
|
212
|
+
}
|
|
213
|
+
const ITEM_RESOLUTIONS = ['keepCurrent', 'useImported', 'review'];
|
|
214
|
+
/** 只接受已知裁决值:乱填的键值一律丢弃(宁缺不乱,createImportPlan 按缺省语义处理)。 */
|
|
215
|
+
function normalizeResolutions(input) {
|
|
216
|
+
const out = {};
|
|
217
|
+
if (input === undefined)
|
|
218
|
+
return out;
|
|
219
|
+
for (const [key, value] of Object.entries(input)) {
|
|
220
|
+
if (typeof value !== 'string')
|
|
221
|
+
continue;
|
|
222
|
+
if (!ITEM_RESOLUTIONS.includes(value))
|
|
223
|
+
continue;
|
|
224
|
+
out[key] = value;
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
const MAPPING_TARGETS = ['workspaces', 'mcp', 'pluginConfig', 'skills'];
|
|
229
|
+
function isMappingTarget(value) {
|
|
230
|
+
return MAPPING_TARGETS.includes(value);
|
|
231
|
+
}
|
|
232
|
+
/** 只留非空映射,并丢弃非法的 appliesTo 取值(缺省 / 空 appliesTo 合法 = 不作用到任何分区)。 */
|
|
233
|
+
function normalizePathMappings(input) {
|
|
234
|
+
if (input === undefined)
|
|
235
|
+
return [];
|
|
236
|
+
return input
|
|
237
|
+
.filter((mapping) => mapping.oldPrefix !== '' && mapping.newPrefix !== '')
|
|
238
|
+
.map((mapping) => ({
|
|
239
|
+
oldPrefix: mapping.oldPrefix,
|
|
240
|
+
newPrefix: mapping.newPrefix,
|
|
241
|
+
appliesTo: (mapping.appliesTo ?? []).filter(isMappingTarget),
|
|
242
|
+
}));
|
|
76
243
|
}
|
|
77
244
|
/* ------------------------------------------------------------ 纯编排函数 */
|
|
78
|
-
/**
|
|
245
|
+
/** 9 个工具的纯编排实现(可独立测试,不依赖 Cordis ctx)。所有返回均为 JsonValue(可序列化、无 undefined)。 */
|
|
79
246
|
export function createModelTools(deps) {
|
|
247
|
+
// 计划句柄表(D2/D5/D6):进程内、30 分钟 TTL、上限 8。只存「重放材料」——
|
|
248
|
+
// 恢复 → 快照 id;合并 → 通道 + 快照 id;导入 → ZIP 路径 + 决策 JSON。
|
|
249
|
+
// 绝不放密码或凭据值:凭据只在宿主内单次调用栈上出现,不落表、不落盘。
|
|
250
|
+
const plans = deps.planStore ?? new ModelPlanStore();
|
|
80
251
|
return {
|
|
81
252
|
/** 全量备份到 exports 目录(默认不含 secret;password 可选加密,仅内存)。 */
|
|
82
253
|
async backup(input) {
|
|
@@ -127,11 +298,11 @@ export function createModelTools(deps) {
|
|
|
127
298
|
}));
|
|
128
299
|
},
|
|
129
300
|
/**
|
|
130
|
-
* 恢复到指定快照。缺省只 planRestore
|
|
131
|
-
* confirm:true
|
|
301
|
+
* 恢复到指定快照。缺省只 planRestore(零写入)预览并**返回 planId**;
|
|
302
|
+
* 真实执行必须 confirm:true 且带回预览给的 planId —— 计划在预览后变了就拒绝(零写入)。
|
|
132
303
|
*/
|
|
133
304
|
async restore(input) {
|
|
134
|
-
const { snapshotId, confirm } = input;
|
|
305
|
+
const { snapshotId, planId, confirm } = input;
|
|
135
306
|
assertValidSnapshotId(snapshotId);
|
|
136
307
|
if (!(await snapshotDirExists(deps, snapshotId))) {
|
|
137
308
|
throw new Error(engineMsg('core.modelTools.snapshotNotFound', { id: snapshotId }));
|
|
@@ -152,10 +323,14 @@ export function createModelTools(deps) {
|
|
|
152
323
|
currentDshVersion: deps.host.dshVersion,
|
|
153
324
|
};
|
|
154
325
|
const plan = await planRestore(restoreOpts);
|
|
326
|
+
const fingerprint = restorePlanFingerprint(plan);
|
|
155
327
|
if (confirm !== true) {
|
|
328
|
+
// 预览即登记计划(供执行侧比对);同一份盘面重复预览 → 同一 planId(覆盖写入,幂等)。
|
|
329
|
+
plans.set('restore', fingerprint, { snapshotId });
|
|
156
330
|
return {
|
|
157
331
|
dryRun: true,
|
|
158
332
|
snapshotId: plan.snapshotId,
|
|
333
|
+
planId: fingerprint,
|
|
159
334
|
createdAt: plan.createdAt,
|
|
160
335
|
summary: {
|
|
161
336
|
hostFileRestores: plan.summary.hostFileRestores,
|
|
@@ -174,6 +349,14 @@ export function createModelTools(deps) {
|
|
|
174
349
|
})),
|
|
175
350
|
};
|
|
176
351
|
}
|
|
352
|
+
// D2 预览-执行一致性:执行必须带预览给的 planId(缺 = plan-required,不符 = plan-changed)。
|
|
353
|
+
// 两处都在**写盘之前**判定;锁内还会按当前盘面重算一次(GUI 恢复路由 409 的同款语义)。
|
|
354
|
+
if (planId === undefined || planId === '') {
|
|
355
|
+
return failure('plan-required', 'core.modelTools.errPlanRequired', 'core.modelTools.nextRepreview', true);
|
|
356
|
+
}
|
|
357
|
+
if (planId !== fingerprint) {
|
|
358
|
+
return failure('plan-changed', 'core.modelTools.errPlanChanged', 'core.modelTools.nextRepreview', true);
|
|
359
|
+
}
|
|
177
360
|
// Step 3 P0-A:真实 model-restore 在已持锁下创建 journal(不 double-acquire)
|
|
178
361
|
// ctx = journal 上下文(可选):交给 restore() 逐动作 WAL,崩溃后 reconcile 可判进度
|
|
179
362
|
const doRestore = async (ctx) => executeRestore(ctx === undefined ? restoreOpts : { ...restoreOpts, journal: ctx });
|
|
@@ -184,16 +367,25 @@ export function createModelTools(deps) {
|
|
|
184
367
|
? null
|
|
185
368
|
: await probeRestoreResume({ store: phase3.store, restoreOpts, allowedOperationTypes: ['model-restore'] });
|
|
186
369
|
const resumeAllowed = resumeProbe !== null && resumeProbe.ok;
|
|
187
|
-
const
|
|
370
|
+
const outcome = await runWithMutationLock(deps.host.mutationLock, { op: 'model-restore', target: snapshotId, isBlocked: () => (deps.host.safeModeIsBlocked?.() ?? false) && !resumeAllowed }, async (lockCtx) => {
|
|
371
|
+
const fresh = await planRestore(restoreOpts);
|
|
372
|
+
if (restorePlanFingerprint(fresh) !== planId)
|
|
373
|
+
return { kind: 'changed' };
|
|
188
374
|
if (phase3 !== undefined && lockCtx !== null) {
|
|
189
375
|
const out = await runRestoreWithResume({
|
|
190
376
|
store: phase3.store, restoreOpts, operationType: 'model-restore',
|
|
191
377
|
allowedOperationTypes: ['model-restore'], lockCtx, phase3,
|
|
192
378
|
});
|
|
193
|
-
return out.report;
|
|
379
|
+
return { kind: 'ok', report: out.report };
|
|
194
380
|
}
|
|
195
|
-
return doRestore();
|
|
381
|
+
return { kind: 'ok', report: await doRestore() };
|
|
196
382
|
});
|
|
383
|
+
if (outcome.kind === 'changed') {
|
|
384
|
+
return failure('plan-changed', 'core.modelTools.errPlanChanged', 'core.modelTools.nextRepreview', true);
|
|
385
|
+
}
|
|
386
|
+
// 计划已消费:同一 planId 不能重放(否则「执行两次」与「执行一次」无法区分)。
|
|
387
|
+
plans.delete(planId);
|
|
388
|
+
const report = outcome.report;
|
|
197
389
|
return {
|
|
198
390
|
dryRun: false,
|
|
199
391
|
snapshotId: report.snapshotId,
|
|
@@ -204,19 +396,32 @@ export function createModelTools(deps) {
|
|
|
204
396
|
skipped: report.skipped,
|
|
205
397
|
};
|
|
206
398
|
},
|
|
207
|
-
/**
|
|
399
|
+
/**
|
|
400
|
+
* 手动推送同步(写远端)。密钥绝不明文进通道:
|
|
401
|
+
* D4 起 includeSecrets 恒 false —— agent 传 true 直接拒绝(模型无权让凭据值上行)。
|
|
402
|
+
*/
|
|
208
403
|
async syncPush(input) {
|
|
209
|
-
|
|
404
|
+
// D4:agent 永不触发凭据上行。参数保留在 schema 只为给出**可读的拒绝**,
|
|
405
|
+
// 而不是让模型猜「为什么 includeSecrets 没生效」。
|
|
406
|
+
if (input.includeSecrets === true) {
|
|
407
|
+
return failure('include-secrets-forbidden', 'core.modelTools.errIncludeSecrets', 'core.modelTools.nextRetryWithoutSecrets', false);
|
|
408
|
+
}
|
|
409
|
+
const { engine, channel } = await resolveEngine(deps, input.channel);
|
|
210
410
|
const sections = input.sections === undefined ? undefined : filterSectionIds(input.sections);
|
|
211
411
|
const encrypt = input.encrypt === true;
|
|
212
|
-
|
|
412
|
+
// D3:密码来源优先级 = 已保存的 DSH credentials 槽位 → 入参 password(deprecated)→ 缺失即拒绝。
|
|
413
|
+
// 顺序刻意:模型不该把明文密码写进工具入参(那会进会话历史);已存槽位才是常态来源。
|
|
414
|
+
const stored = encrypt ? await deps.syncPassword?.('ENCRYPT', channel) : undefined;
|
|
415
|
+
const password = stored ?? (input.password === undefined || input.password === '' ? undefined : input.password);
|
|
416
|
+
if (encrypt && password === undefined) {
|
|
417
|
+
return failure('sync-password-missing', 'core.modelTools.errSyncPasswordMissing', 'core.modelTools.nextSaveSyncPassword', true, { channel });
|
|
418
|
+
}
|
|
213
419
|
// Phase 2 锁:push 写远端 + 本地散文件 + sync-state,属 GLOBAL mutation。
|
|
214
420
|
// Step 3 P0-A:外部 push 记 intent journal(crash 后不可证明 → NEEDS_ATTENTION,不自动重推)。
|
|
215
421
|
const doPush = () => engine.push({
|
|
216
422
|
...(sections === undefined ? {} : { sections }),
|
|
217
|
-
...(encrypt
|
|
218
|
-
|
|
219
|
-
: {}),
|
|
423
|
+
...(encrypt ? { encrypt: true, includeSecrets: false, password: password ?? '' } : {}),
|
|
424
|
+
...(input.force === true ? { force: true } : {}),
|
|
220
425
|
});
|
|
221
426
|
const report = await runWithMutationLock(deps.host.mutationLock, { op: 'model-sync-push', isBlocked: () => deps.host.safeModeIsBlocked?.() ?? false }, async (lockCtx) => {
|
|
222
427
|
if (deps.host.phase3Recovery !== undefined && lockCtx !== null) {
|
|
@@ -235,17 +440,24 @@ export function createModelTools(deps) {
|
|
|
235
440
|
snapshotId: report.snapshotId,
|
|
236
441
|
sections: report.sections,
|
|
237
442
|
warnings: report.warnings,
|
|
443
|
+
// 前置检测拒绝(远端已被别的设备推进)不是通道故障:必须显式透传,
|
|
444
|
+
// 否则模型会把它当成「推送成功但内容为空」继续往下走。
|
|
445
|
+
...(report.conflict === true ? { conflict: true } : {}),
|
|
446
|
+
...(report.adoptedBase === undefined ? {} : { adoptedBase: report.adoptedBase }),
|
|
238
447
|
...(report.message === undefined ? {} : { message: report.message }),
|
|
448
|
+
...(encrypt ? { passwordSource: stored === undefined ? 'argument' : 'stored' } : {}),
|
|
239
449
|
};
|
|
240
450
|
},
|
|
241
|
-
/** 拉取差异预览(零写入:只下载 + analyze + plan
|
|
451
|
+
/** 拉取差异预览(零写入:只下载 + analyze + plan 出差异报告)。解密密码同样优先取已存槽位(D3)。 */
|
|
242
452
|
async syncPull(input) {
|
|
243
|
-
const { engine } = await resolveEngine(deps, input.channel);
|
|
453
|
+
const { engine, channel } = await resolveEngine(deps, input.channel);
|
|
244
454
|
const strategy = input.strategy === 'replace' || input.strategy === 'skipExisting' ? input.strategy : 'merge';
|
|
455
|
+
const stored = await deps.syncPassword?.('DECRYPT', channel);
|
|
456
|
+
const password = stored ?? (input.password === undefined || input.password === '' ? undefined : input.password);
|
|
245
457
|
const report = await engine.pull({
|
|
246
458
|
strategy,
|
|
247
459
|
...(input.snapshotId === undefined || input.snapshotId === '' ? {} : { snapshotId: input.snapshotId }),
|
|
248
|
-
...(
|
|
460
|
+
...(password === undefined ? {} : { password }),
|
|
249
461
|
});
|
|
250
462
|
return {
|
|
251
463
|
ok: report.ok,
|
|
@@ -261,9 +473,171 @@ export function createModelTools(deps) {
|
|
|
261
473
|
...(report.message === undefined ? {} : { message: report.message }),
|
|
262
474
|
};
|
|
263
475
|
},
|
|
476
|
+
/* ---------------- D5:三方合并(落地开放,sessions 永久排除) ---------------- */
|
|
477
|
+
/**
|
|
478
|
+
* 三方合并预览(只读):下载远端快照 + 分类 + 出裁决清单,零写入。
|
|
479
|
+
* 返回的 planId 是这份预览的指纹:config_sync_apply 必须带回来,落地前在锁内重跑预览比对。
|
|
480
|
+
* 刻意不下发 localPreview / remotePreview / ancestorPreview —— 那是 GUI 逐键对比用的分段文本。
|
|
481
|
+
*/
|
|
482
|
+
async syncPreview(input) {
|
|
483
|
+
const { cfg, channel } = await resolveSyncTarget(deps, input.channel);
|
|
484
|
+
// D5:只按用户放行的 'auto' 档分区建引擎;sessions 已在 agentOptInSections 里永久剔除。
|
|
485
|
+
const engine = deps.makeSyncEngine(cfg, { optInSections: agentOptInSections(deps, channel) });
|
|
486
|
+
const stored = await deps.syncPassword?.('DECRYPT', channel);
|
|
487
|
+
const password = stored ?? (input.password === undefined || input.password === '' ? undefined : input.password);
|
|
488
|
+
const opts = {
|
|
489
|
+
...(input.snapshotId === undefined || input.snapshotId === '' ? {} : { snapshotId: input.snapshotId }),
|
|
490
|
+
...(password === undefined ? {} : { password }),
|
|
491
|
+
};
|
|
492
|
+
const preview = await engine.previewMerge(opts);
|
|
493
|
+
const planId = syncMergeFingerprint(preview);
|
|
494
|
+
plans.set('sync-merge', planId, {
|
|
495
|
+
channel,
|
|
496
|
+
...(input.snapshotId === undefined ? {} : { snapshotId: input.snapshotId }),
|
|
497
|
+
});
|
|
498
|
+
return { ok: preview.ok, planId, preview: summarizeMergePreview(preview) };
|
|
499
|
+
},
|
|
500
|
+
/**
|
|
501
|
+
* 落地三方合并:必须带 config_sync_preview 返回的 planId(缺失/过期 → plan-*,零写入)。
|
|
502
|
+
* 锁内重跑预览比对指纹 —— 预览后远端或本机变了就拒绝,不把旧裁决硬套到新盘面。
|
|
503
|
+
*/
|
|
504
|
+
async syncApply(input) {
|
|
505
|
+
const record = plans.get(input.planId);
|
|
506
|
+
if (record === undefined || record.kind !== 'sync-merge') {
|
|
507
|
+
return failure('plan-expired', 'core.modelTools.errPlanExpired', 'core.modelTools.nextRepreview', true);
|
|
508
|
+
}
|
|
509
|
+
const channel = parseSyncChannel(record.payload.channel ?? '');
|
|
510
|
+
if (channel === null || channel === undefined) {
|
|
511
|
+
return failure('plan-expired', 'core.modelTools.errPlanExpired', 'core.modelTools.nextRepreview', true);
|
|
512
|
+
}
|
|
513
|
+
const resolutions = [];
|
|
514
|
+
for (const entry of input.resolutions) {
|
|
515
|
+
if (!isMergeAction(entry.action)) {
|
|
516
|
+
return failure('invalid-resolution', 'core.modelTools.errInvalidResolution', 'core.modelTools.nextFixResolution', true, { action: entry.action });
|
|
517
|
+
}
|
|
518
|
+
resolutions.push({ id: entry.id, action: entry.action });
|
|
519
|
+
}
|
|
520
|
+
const { cfg } = await resolveSyncTarget(deps, channel);
|
|
521
|
+
const engine = deps.makeSyncEngine(cfg, { optInSections: agentOptInSections(deps, channel) });
|
|
522
|
+
const stored = await deps.syncPassword?.('DECRYPT', channel);
|
|
523
|
+
const password = stored ?? (input.password === undefined || input.password === '' ? undefined : input.password);
|
|
524
|
+
const snapshotId = record.payload.snapshotId;
|
|
525
|
+
const opts = {
|
|
526
|
+
...(snapshotId === undefined || snapshotId === '' ? {} : { snapshotId }),
|
|
527
|
+
...(password === undefined ? {} : { password }),
|
|
528
|
+
};
|
|
529
|
+
const outcome = await runWithMutationLock(deps.host.mutationLock, { op: 'model-sync-apply', target: channel, isBlocked: () => deps.host.safeModeIsBlocked?.() ?? false }, async () => {
|
|
530
|
+
const fresh = await engine.previewMerge(opts);
|
|
531
|
+
if (syncMergeFingerprint(fresh) !== input.planId)
|
|
532
|
+
return { kind: 'changed' };
|
|
533
|
+
const known = new Set(fresh.sections.map((section) => section.id));
|
|
534
|
+
const decisions = resolutions.filter((resolution) => known.has(resolution.id));
|
|
535
|
+
const ignored = resolutions
|
|
536
|
+
.filter((resolution) => !known.has(resolution.id))
|
|
537
|
+
.map((resolution) => resolution.id);
|
|
538
|
+
const report = await engine.applyMergeResolutions(decisions, opts);
|
|
539
|
+
return { kind: 'ok', report, ignored };
|
|
540
|
+
});
|
|
541
|
+
if (outcome.kind === 'changed') {
|
|
542
|
+
return failure('plan-changed', 'core.modelTools.errPlanChanged', 'core.modelTools.nextRepreview', true);
|
|
543
|
+
}
|
|
544
|
+
plans.delete(input.planId);
|
|
545
|
+
const report = outcome.report;
|
|
546
|
+
return {
|
|
547
|
+
ok: report.ok,
|
|
548
|
+
applied: report.applied,
|
|
549
|
+
restoreId: report.restoreId,
|
|
550
|
+
rolledBack: report.rolledBack,
|
|
551
|
+
...(report.rollbackFull === undefined ? {} : { rollbackFull: report.rollbackFull }),
|
|
552
|
+
warnings: report.warnings,
|
|
553
|
+
pendingConflicts: report.pendingConflicts,
|
|
554
|
+
...(outcome.ignored.length === 0 ? {} : { ignoredResolutions: outcome.ignored }),
|
|
555
|
+
};
|
|
556
|
+
},
|
|
557
|
+
/* ---------------- D6:导入计划 + 执行(句柄 + 结构化决策,不接受密码/凭据值) ---------------- */
|
|
558
|
+
/**
|
|
559
|
+
* 生成导入计划(只读):分析归档 + 合并冲突决策 / 路径映射,零写入。
|
|
560
|
+
* 加密归档直接拒绝 —— 解密需要密码,而密码不进 agent 面(该场景走 GUI 导入)。
|
|
561
|
+
*/
|
|
562
|
+
async importPlan(input) {
|
|
563
|
+
const importer = deps.makeImporter?.();
|
|
564
|
+
if (importer === undefined) {
|
|
565
|
+
return failure('import-unavailable', 'core.modelTools.errImportUnavailable', 'core.modelTools.nextUseGuiImport', false);
|
|
566
|
+
}
|
|
567
|
+
if (!isControlledZipPath(input.zipPath, deps.roots)) {
|
|
568
|
+
return failure('path-not-controlled', 'core.modelTools.errPathNotControlled', 'core.modelTools.nextPickControlledZip', false, { path: input.zipPath });
|
|
569
|
+
}
|
|
570
|
+
const decisions = {
|
|
571
|
+
strategy: input.strategy === 'replace' || input.strategy === 'skipExisting' ? input.strategy : 'merge',
|
|
572
|
+
resolutions: normalizeResolutions(input.resolutions),
|
|
573
|
+
pathMappings: normalizePathMappings(input.pathMappings),
|
|
574
|
+
};
|
|
575
|
+
const analysis = await importer.analyzeImport(input.zipPath);
|
|
576
|
+
if (analysis.encrypted) {
|
|
577
|
+
return failure('encrypted-archive', 'core.modelTools.errEncryptedArchive', 'core.modelTools.nextUseGuiImport', false);
|
|
578
|
+
}
|
|
579
|
+
if (!analysis.valid) {
|
|
580
|
+
return {
|
|
581
|
+
ok: false,
|
|
582
|
+
code: 'archive-invalid',
|
|
583
|
+
message: analysis.errors.join('; '),
|
|
584
|
+
retryable: false,
|
|
585
|
+
nextAction: engineMsg('core.modelTools.nextUseGuiImport'),
|
|
586
|
+
analysis: summarizeAnalysis(analysis),
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const plan = await importer.createImportPlan(input.zipPath, decisions);
|
|
590
|
+
const planId = importPlanFingerprint(plan, decisions);
|
|
591
|
+
plans.set('import', planId, { zipPath: input.zipPath, decisionsJson: JSON.stringify(decisions) });
|
|
592
|
+
return {
|
|
593
|
+
ok: true,
|
|
594
|
+
planId,
|
|
595
|
+
zipPath: input.zipPath,
|
|
596
|
+
analysis: summarizeAnalysis(analysis),
|
|
597
|
+
plan: summarizePlan(plan),
|
|
598
|
+
};
|
|
599
|
+
},
|
|
600
|
+
/**
|
|
601
|
+
* 执行导入计划:必须带 config_import_plan 返回的 planId + confirm:true。
|
|
602
|
+
* 执行前按存档决策重算计划比对指纹(归档被换 / 本机状态变了 → 拒绝,零写入)。
|
|
603
|
+
*/
|
|
604
|
+
async importApply(input) {
|
|
605
|
+
if (input.confirm !== true) {
|
|
606
|
+
return failure('confirm-required', 'core.modelTools.errConfirmRequired', 'core.modelTools.nextPassConfirm', true);
|
|
607
|
+
}
|
|
608
|
+
const record = plans.get(input.planId);
|
|
609
|
+
if (record === undefined || record.kind !== 'import') {
|
|
610
|
+
return failure('plan-expired', 'core.modelTools.errPlanExpired', 'core.modelTools.nextRepreview', true);
|
|
611
|
+
}
|
|
612
|
+
const importer = deps.makeImporter?.();
|
|
613
|
+
if (importer === undefined) {
|
|
614
|
+
return failure('import-unavailable', 'core.modelTools.errImportUnavailable', 'core.modelTools.nextUseGuiImport', false);
|
|
615
|
+
}
|
|
616
|
+
const zipPath = record.payload.zipPath ?? '';
|
|
617
|
+
if (!isControlledZipPath(zipPath, deps.roots)) {
|
|
618
|
+
return failure('path-not-controlled', 'core.modelTools.errPathNotControlled', 'core.modelTools.nextPickControlledZip', false, { path: zipPath });
|
|
619
|
+
}
|
|
620
|
+
let decisions;
|
|
621
|
+
try {
|
|
622
|
+
decisions = JSON.parse(record.payload.decisionsJson ?? '');
|
|
623
|
+
}
|
|
624
|
+
catch {
|
|
625
|
+
return failure('plan-expired', 'core.modelTools.errPlanExpired', 'core.modelTools.nextRepreview', true);
|
|
626
|
+
}
|
|
627
|
+
const fresh = await importer.createImportPlan(zipPath, decisions);
|
|
628
|
+
if (importPlanFingerprint(fresh, decisions) !== input.planId) {
|
|
629
|
+
return failure('plan-changed', 'core.modelTools.errPlanChanged', 'core.modelTools.nextRepreview', true);
|
|
630
|
+
}
|
|
631
|
+
const result = await runWithMutationLock(deps.host.mutationLock, { op: 'model-import-apply', target: zipPath, isBlocked: () => deps.host.safeModeIsBlocked?.() ?? false }, async () => importer.executeImportPlan(zipPath, fresh, {
|
|
632
|
+
confirm: true,
|
|
633
|
+
...(input.rollbackOnError === undefined ? {} : { rollbackOnError: input.rollbackOnError }),
|
|
634
|
+
}));
|
|
635
|
+
plans.delete(input.planId);
|
|
636
|
+
return summarizeImportResult(result);
|
|
637
|
+
},
|
|
264
638
|
};
|
|
265
639
|
}
|
|
266
|
-
/** 把
|
|
640
|
+
/** 把 9 个模型工具注册进 ctx.tools;tools 服务未组合时静默跳过(不崩溃)。 */
|
|
267
641
|
export function registerModelTools(ctx, deps) {
|
|
268
642
|
const toolsSvc = ctx.get('tools');
|
|
269
643
|
if (toolsSvc === null || toolsSvc === undefined || typeof toolsSvc !== 'object') {
|
|
@@ -307,6 +681,8 @@ export function registerModelTools(ctx, deps) {
|
|
|
307
681
|
name: 'config_list_snapshots',
|
|
308
682
|
description: engineMsg('core.modelTools.listSnapshotsToolDesc'),
|
|
309
683
|
parameters: {},
|
|
684
|
+
// 只读工具:并发调用安全(宿主可并行调度)。
|
|
685
|
+
isConcurrencySafe: () => true,
|
|
310
686
|
output: {
|
|
311
687
|
schema: {
|
|
312
688
|
type: 'json',
|
|
@@ -327,6 +703,10 @@ export function registerModelTools(ctx, deps) {
|
|
|
327
703
|
required: true,
|
|
328
704
|
description: engineMsg('core.modelTools.restoreSnapshotIdParam'),
|
|
329
705
|
},
|
|
706
|
+
planId: {
|
|
707
|
+
type: 'string',
|
|
708
|
+
description: engineMsg('core.modelTools.restorePlanIdParam'),
|
|
709
|
+
},
|
|
330
710
|
confirm: {
|
|
331
711
|
type: 'boolean',
|
|
332
712
|
description: engineMsg('core.modelTools.restoreConfirmParam'),
|
|
@@ -364,7 +744,11 @@ export function registerModelTools(ctx, deps) {
|
|
|
364
744
|
},
|
|
365
745
|
password: {
|
|
366
746
|
type: 'string',
|
|
367
|
-
description: engineMsg('core.modelTools.
|
|
747
|
+
description: engineMsg('core.modelTools.syncPushPasswordDeprecatedParam'),
|
|
748
|
+
},
|
|
749
|
+
force: {
|
|
750
|
+
type: 'boolean',
|
|
751
|
+
description: engineMsg('core.modelTools.syncPushForceParam'),
|
|
368
752
|
},
|
|
369
753
|
},
|
|
370
754
|
output: {
|
|
@@ -398,6 +782,8 @@ export function registerModelTools(ctx, deps) {
|
|
|
398
782
|
description: engineMsg('core.modelTools.syncPullPasswordParam'),
|
|
399
783
|
},
|
|
400
784
|
},
|
|
785
|
+
// 只读工具:pull 只下载 + 分析,零写入(并发调用安全)。
|
|
786
|
+
isConcurrencySafe: () => true,
|
|
401
787
|
output: {
|
|
402
788
|
schema: { type: 'json', description: engineMsg('core.modelTools.syncPullOutputDesc') },
|
|
403
789
|
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
@@ -406,6 +792,157 @@ export function registerModelTools(ctx, deps) {
|
|
|
406
792
|
return tools.syncPull(args);
|
|
407
793
|
},
|
|
408
794
|
}));
|
|
795
|
+
register(defineTool({
|
|
796
|
+
name: 'config_sync_preview',
|
|
797
|
+
description: engineMsg('core.modelTools.syncPreviewToolDesc'),
|
|
798
|
+
parameters: {
|
|
799
|
+
channel: {
|
|
800
|
+
type: 'string',
|
|
801
|
+
enum: [...SYNC_CHANNELS],
|
|
802
|
+
description: engineMsg('core.modelTools.channelParam'),
|
|
803
|
+
},
|
|
804
|
+
snapshotId: {
|
|
805
|
+
type: 'string',
|
|
806
|
+
description: engineMsg('core.modelTools.syncPreviewSnapshotIdParam'),
|
|
807
|
+
},
|
|
808
|
+
password: {
|
|
809
|
+
type: 'string',
|
|
810
|
+
description: engineMsg('core.modelTools.syncPreviewPasswordParam'),
|
|
811
|
+
},
|
|
812
|
+
},
|
|
813
|
+
isConcurrencySafe: () => true,
|
|
814
|
+
output: {
|
|
815
|
+
schema: { type: 'json', description: engineMsg('core.modelTools.syncPreviewOutputDesc') },
|
|
816
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
817
|
+
},
|
|
818
|
+
async execute(args) {
|
|
819
|
+
return tools.syncPreview(args);
|
|
820
|
+
},
|
|
821
|
+
}));
|
|
822
|
+
register(defineTool({
|
|
823
|
+
name: 'config_sync_apply',
|
|
824
|
+
description: engineMsg('core.modelTools.syncApplyToolDesc'),
|
|
825
|
+
parameters: {
|
|
826
|
+
planId: {
|
|
827
|
+
type: 'string',
|
|
828
|
+
required: true,
|
|
829
|
+
description: engineMsg('core.modelTools.syncApplyPlanIdParam'),
|
|
830
|
+
},
|
|
831
|
+
resolutions: {
|
|
832
|
+
type: 'array',
|
|
833
|
+
required: true,
|
|
834
|
+
items: {
|
|
835
|
+
type: 'object',
|
|
836
|
+
properties: {
|
|
837
|
+
id: {
|
|
838
|
+
type: 'string',
|
|
839
|
+
required: true,
|
|
840
|
+
description: engineMsg('core.modelTools.syncApplyResolutionIdParam'),
|
|
841
|
+
},
|
|
842
|
+
action: {
|
|
843
|
+
type: 'string',
|
|
844
|
+
required: true,
|
|
845
|
+
enum: [...MERGE_ACTIONS],
|
|
846
|
+
description: engineMsg('core.modelTools.syncApplyResolutionActionParam'),
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
additionalProperties: false,
|
|
850
|
+
},
|
|
851
|
+
description: engineMsg('core.modelTools.syncApplyResolutionsParam'),
|
|
852
|
+
},
|
|
853
|
+
password: {
|
|
854
|
+
type: 'string',
|
|
855
|
+
description: engineMsg('core.modelTools.syncApplyPasswordParam'),
|
|
856
|
+
},
|
|
857
|
+
},
|
|
858
|
+
output: {
|
|
859
|
+
schema: { type: 'json', description: engineMsg('core.modelTools.syncApplyOutputDesc') },
|
|
860
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
861
|
+
},
|
|
862
|
+
async execute(args) {
|
|
863
|
+
return tools.syncApply(args);
|
|
864
|
+
},
|
|
865
|
+
}));
|
|
866
|
+
register(defineTool({
|
|
867
|
+
name: 'config_import_plan',
|
|
868
|
+
description: engineMsg('core.modelTools.importPlanToolDesc'),
|
|
869
|
+
parameters: {
|
|
870
|
+
zipPath: {
|
|
871
|
+
type: 'string',
|
|
872
|
+
required: true,
|
|
873
|
+
description: engineMsg('core.modelTools.importPlanZipPathParam'),
|
|
874
|
+
},
|
|
875
|
+
strategy: {
|
|
876
|
+
type: 'string',
|
|
877
|
+
enum: ['merge', 'replace', 'skipExisting'],
|
|
878
|
+
description: engineMsg('core.modelTools.importPlanStrategyParam'),
|
|
879
|
+
},
|
|
880
|
+
resolutions: {
|
|
881
|
+
type: 'object',
|
|
882
|
+
additionalProperties: true,
|
|
883
|
+
description: engineMsg('core.modelTools.importPlanResolutionsParam'),
|
|
884
|
+
},
|
|
885
|
+
pathMappings: {
|
|
886
|
+
type: 'array',
|
|
887
|
+
items: {
|
|
888
|
+
type: 'object',
|
|
889
|
+
properties: {
|
|
890
|
+
oldPrefix: {
|
|
891
|
+
type: 'string',
|
|
892
|
+
required: true,
|
|
893
|
+
description: engineMsg('core.modelTools.importPlanMappingOldParam'),
|
|
894
|
+
},
|
|
895
|
+
newPrefix: {
|
|
896
|
+
type: 'string',
|
|
897
|
+
required: true,
|
|
898
|
+
description: engineMsg('core.modelTools.importPlanMappingNewParam'),
|
|
899
|
+
},
|
|
900
|
+
appliesTo: {
|
|
901
|
+
type: 'array',
|
|
902
|
+
items: { type: 'string', enum: [...MAPPING_TARGETS] },
|
|
903
|
+
description: engineMsg('core.modelTools.importPlanMappingAppliesToParam'),
|
|
904
|
+
},
|
|
905
|
+
},
|
|
906
|
+
additionalProperties: false,
|
|
907
|
+
},
|
|
908
|
+
description: engineMsg('core.modelTools.importPlanPathMappingsParam'),
|
|
909
|
+
},
|
|
910
|
+
},
|
|
911
|
+
isConcurrencySafe: () => true,
|
|
912
|
+
output: {
|
|
913
|
+
schema: { type: 'json', description: engineMsg('core.modelTools.importPlanOutputDesc') },
|
|
914
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
915
|
+
},
|
|
916
|
+
async execute(args) {
|
|
917
|
+
return tools.importPlan(args);
|
|
918
|
+
},
|
|
919
|
+
}));
|
|
920
|
+
register(defineTool({
|
|
921
|
+
name: 'config_import_apply',
|
|
922
|
+
description: engineMsg('core.modelTools.importApplyToolDesc'),
|
|
923
|
+
parameters: {
|
|
924
|
+
planId: {
|
|
925
|
+
type: 'string',
|
|
926
|
+
required: true,
|
|
927
|
+
description: engineMsg('core.modelTools.importApplyPlanIdParam'),
|
|
928
|
+
},
|
|
929
|
+
confirm: {
|
|
930
|
+
type: 'boolean',
|
|
931
|
+
description: engineMsg('core.modelTools.importApplyConfirmParam'),
|
|
932
|
+
},
|
|
933
|
+
rollbackOnError: {
|
|
934
|
+
type: 'boolean',
|
|
935
|
+
description: engineMsg('core.modelTools.importApplyRollbackOnErrorParam'),
|
|
936
|
+
},
|
|
937
|
+
},
|
|
938
|
+
output: {
|
|
939
|
+
schema: { type: 'json', description: engineMsg('core.modelTools.importApplyOutputDesc') },
|
|
940
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
941
|
+
},
|
|
942
|
+
async execute(args) {
|
|
943
|
+
return tools.importApply(args);
|
|
944
|
+
},
|
|
945
|
+
}));
|
|
409
946
|
ctx.effect(() => () => { for (const d of disposers)
|
|
410
947
|
d(); }, 'config-manager: model tools');
|
|
411
948
|
}
|