dsh-plugin-tool-management 0.22.1 → 0.22.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +8 -7
- package/README_EN.md +8 -7
- package/docs/UPDATE.md +1 -0
- package/lib/client.js +647 -206
- package/lib/compat/capability-specs.js +400 -1
- package/lib/compat/host-contracts.js +334 -3
- package/lib/compat/host-probe.js +40 -0
- package/lib/compat/host-roots.js +23 -8
- package/lib/compat/manual-review.js +55 -0
- package/lib/compat/probe.js +448 -25
- package/lib/desktop-cookie-prune.js +19 -3
- package/lib/index.js +100 -19
- package/lib/mcp/manager.js +2 -1
- package/lib/mcp/prompt-shadow.js +128 -0
- package/lib/mcp/tool-visibility.js +18 -6
- package/lib/memories/service.js +18 -5
- package/lib/op-registry.js +1 -1
- package/lib/ops/compat.js +31 -0
- package/lib/ops/scope.js +9 -9
- package/lib/scene-linkage.js +16 -1
- package/lib/scope/intent.js +1 -1
- package/lib/tool-table-switch.js +2 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { symbols } from '@deepseek-ai/cordis';
|
|
11
11
|
import { defineTool as hostDefineTool } from '@deepseek-ai/dsh-tools';
|
|
12
12
|
import { createRequire } from 'node:module';
|
|
13
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
13
14
|
import { randomUUID } from 'node:crypto';
|
|
14
15
|
import { createHistoryDomain } from './sessions/history.js';
|
|
15
16
|
import { createSkillsService, pluginLog } from './skills/service.js';
|
|
@@ -45,6 +46,7 @@ import { clearRuntimeNote, noteRuntime } from './compat/runtime-notes.js';
|
|
|
45
46
|
import { reachNoticeForAgent } from './compat/preset-reach.js';
|
|
46
47
|
import { installHttpApi } from './http-api.js';
|
|
47
48
|
import { createMcpManager } from './mcp/manager.js';
|
|
49
|
+
import { createMcpPromptShadow } from './mcp/prompt-shadow.js';
|
|
48
50
|
// 确认卡回显"新 URL"时要打码(查询串里的凭据不该明文进卡片),口径与列表视图同一份实现。
|
|
49
51
|
import { maskUrlQuery } from './mcp/secret-guard.js';
|
|
50
52
|
import { createCandidates } from './scenes/candidates.js';
|
|
@@ -236,8 +238,25 @@ export default {
|
|
|
236
238
|
// (停用表丢掉一条 = 已停用的 MCP 工具重新可调用)。
|
|
237
239
|
const hubLock = createHubFileLock(hubPath());
|
|
238
240
|
let writeChain = Promise.resolve();
|
|
241
|
+
/**
|
|
242
|
+
* 「本条异步调用链已经持有写锁」的标记。
|
|
243
|
+
*
|
|
244
|
+
* 为什么必须可重入:锁内确实会回调别的域 —— MCP 改名 / 删台在锁内调
|
|
245
|
+
* `memoriesService.patchIndex` 同步场景档案,而记忆域自己也取锁。没有这个标记,
|
|
246
|
+
* 那条 op 会排在**自己**后面:不是报错,是永远不返回。
|
|
247
|
+
*
|
|
248
|
+
* 用 `AsyncLocalStorage` 而不是一个布尔:多个请求并发共用同一个进程,布尔会让后到的
|
|
249
|
+
* 请求误以为自己也持锁,直接跳过排队。
|
|
250
|
+
*
|
|
251
|
+
* ⚠️ 由此派生的**游离任务**(在临界区里 `void` 起、活过外层临界区)也会被算作"已持锁",
|
|
252
|
+
* 这类任务不得写 hub;现有调用点没有这种形状。
|
|
253
|
+
*/
|
|
254
|
+
const writeLockScope = new AsyncLocalStorage();
|
|
239
255
|
function withWriteLock(fn) {
|
|
240
|
-
|
|
256
|
+
if (writeLockScope.getStore() === true)
|
|
257
|
+
return fn();
|
|
258
|
+
const enter = () => writeLockScope.run(true, fn);
|
|
259
|
+
const run = writeChain.then(() => hubLock.run(enter), () => hubLock.run(enter));
|
|
241
260
|
writeChain = run.then(() => undefined, () => undefined);
|
|
242
261
|
return run;
|
|
243
262
|
}
|
|
@@ -867,6 +886,52 @@ export default {
|
|
|
867
886
|
return undefined;
|
|
868
887
|
return mcpVerdictOf(ctx, parts);
|
|
869
888
|
}
|
|
889
|
+
/**
|
|
890
|
+
* agent 作用域上的 `systemPrompt` 服务(取不到返回 undefined)。
|
|
891
|
+
*
|
|
892
|
+
* 必须从 **agent.ctx.get** 取:Cordis 的 traceable 代理会把方法调用时的 `this.ctx`
|
|
893
|
+
* 换成取服务的那个作用域,`systemPrompt.section()` 才会注册到该 agent 的 scoped 层。
|
|
894
|
+
* 从插件根 ctx 取会把遮蔽段注册到全局层 —— 那会遮蔽所有 agent,而不是只遮蔽本会话。
|
|
895
|
+
*/
|
|
896
|
+
function systemPromptServiceOf(agent) {
|
|
897
|
+
try {
|
|
898
|
+
const agentCtx = agent?.ctx;
|
|
899
|
+
const service = agentCtx && typeof agentCtx.get === 'function' ? agentCtx.get('systemPrompt') : undefined;
|
|
900
|
+
const candidate = service;
|
|
901
|
+
return candidate !== undefined && typeof candidate.section === 'function' && typeof candidate.getSectionOrder === 'function'
|
|
902
|
+
? candidate
|
|
903
|
+
: undefined;
|
|
904
|
+
}
|
|
905
|
+
catch {
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
/** 跨会话 prompt 隔离:按 agent 作用域同名遮蔽官方 MCP 段(见 mcp/prompt-shadow.ts)。 */
|
|
910
|
+
const mcpPromptShadow = createMcpPromptShadow(systemPromptServiceOf);
|
|
911
|
+
/** 把名册同步到某个 agent 的 prompt 遮蔽段;失败只记在本地(不拖垮工具可见性)。 */
|
|
912
|
+
function syncMcpPromptShadows(agent, roster, resourceAvailable) {
|
|
913
|
+
try {
|
|
914
|
+
mcpPromptShadow.sync(agent, [...roster.names], roster.visible, { resourceAvailable });
|
|
915
|
+
}
|
|
916
|
+
catch { /* best effort */ }
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* 预热某个 agent 的 prompt 遮蔽段。
|
|
920
|
+
*
|
|
921
|
+
* 工具可见性重排是异步的;agent 刚创建时先主动算一趟,避免首轮系统提示词在重排完成前
|
|
922
|
+
* 就带上别的对话的服务器名 / instructions。失败与重排那条路一样是 best effort。
|
|
923
|
+
*/
|
|
924
|
+
function warmMcpPromptShadows(agent) {
|
|
925
|
+
void (async () => {
|
|
926
|
+
try {
|
|
927
|
+
const registered = MCP_RESOURCE_TOOL_NAMES.filter((name) => tools.get(name) !== undefined);
|
|
928
|
+
const roster = await visibleMcpServerNames(agent);
|
|
929
|
+
if (roster !== null)
|
|
930
|
+
syncMcpPromptShadows(agent, roster, registered.length > 0);
|
|
931
|
+
}
|
|
932
|
+
catch { /* best effort */ }
|
|
933
|
+
})();
|
|
934
|
+
}
|
|
870
935
|
/**
|
|
871
936
|
* MCP 服务器名册的短 TTL 快照(**与 agent 无关** —— 只有「可见性过滤」才逐 agent)。
|
|
872
937
|
*
|
|
@@ -875,8 +940,9 @@ export default {
|
|
|
875
940
|
*
|
|
876
941
|
* TTL 取 3 秒,与侧车 TTL 同量级(服务器增删是罕见事件)。**陈旧的后果要说明白**:
|
|
877
942
|
* 刚加的服务器晚 3 秒才进名册,这段时间名册缺它 —— 若名册里别的服务器在本会话都看不见,
|
|
878
|
-
*
|
|
879
|
-
*
|
|
943
|
+
* 就会走到「一台都看不见」那一支;`rosterMissesRunningServer` 会在新服务器**有工具**时
|
|
944
|
+
* 拦下(全局工具表里已经有它的 `mcp__*`),只有无工具的服务器才会真被摘掉。恢复要等
|
|
945
|
+
* 「TTL 到期后的下一次重排」
|
|
880
946
|
* —— 重排本身会触发,但 TTL 没到期时它重算的还是陈旧名册。
|
|
881
947
|
*/
|
|
882
948
|
let configuredServerStatesCache = null;
|
|
@@ -893,7 +959,8 @@ export default {
|
|
|
893
959
|
/**
|
|
894
960
|
* 本作用域**看得见的** MCP 服务器名,名册里的全部名字,以及「名册读全了没有」。
|
|
895
961
|
*
|
|
896
|
-
* `visible` =
|
|
962
|
+
* `visible` = 补丁 / loader 里启用、且本作用域看得见的那些(与状态段 / `mcp_manager_list`
|
|
963
|
+
* 同一份判据)。停用的服务器没有运行中的 provider,不算「看得见」。
|
|
897
964
|
* `names` = 名册里的全部名字(看得见 + 看不见)。
|
|
898
965
|
* `complete` = 名册读全了没有:任一层读不动它就只少几条,返回值上与「名册本来就短」
|
|
899
966
|
* 同形(读错误在 `configuredServerStates` 内部被吞成「跳过这一层」)。
|
|
@@ -917,6 +984,8 @@ export default {
|
|
|
917
984
|
if (!name)
|
|
918
985
|
continue;
|
|
919
986
|
names.add(name);
|
|
987
|
+
if (row.disabled === true)
|
|
988
|
+
continue;
|
|
920
989
|
if (sessionServerHiddenOf(agent, name))
|
|
921
990
|
continue;
|
|
922
991
|
visible.push(name);
|
|
@@ -978,11 +1047,16 @@ export default {
|
|
|
978
1047
|
async function resourceToolDenyFor(agent) {
|
|
979
1048
|
try {
|
|
980
1049
|
const registered = MCP_RESOURCE_TOOL_NAMES.filter((name) => tools.get(name) !== undefined);
|
|
981
|
-
if (registered.length === 0)
|
|
982
|
-
return [];
|
|
983
1050
|
const roster = await visibleMcpServerNames(agent);
|
|
984
1051
|
if (roster === null)
|
|
985
1052
|
return [];
|
|
1053
|
+
// 同一趟名册顺带同步 prompt 遮蔽:工具表与系统提示词必须一起收窄,否则本会话会
|
|
1054
|
+
// 「看不到工具、却被提示词教着去调用」。名册不完整时只遮蔽已知的隐藏项,方向选
|
|
1055
|
+
// 不漏掉别的对话。资源工具没注册时仍要遮蔽 `mcp:<server>` 的 instructions ——
|
|
1056
|
+
// 那是另一个官方包注册的段,只是不合成资源段文本。
|
|
1057
|
+
syncMcpPromptShadows(agent, roster, registered.length > 0);
|
|
1058
|
+
if (registered.length === 0)
|
|
1059
|
+
return [];
|
|
986
1060
|
if (!roster.complete)
|
|
987
1061
|
return [];
|
|
988
1062
|
if (roster.visible.length > 0)
|
|
@@ -1175,6 +1249,9 @@ export default {
|
|
|
1175
1249
|
// 会话层覆盖:记忆域按会话投影场景与逐条开关。侧车归 scopeStore 管
|
|
1176
1250
|
// (它有自己的 TTL 缓存),这里只转手 —— 记忆域不引第二份读取点。
|
|
1177
1251
|
scopeOverrideOf: memoryScopeOfSession,
|
|
1252
|
+
// 跨进程写锁:索引是整份读-改-写,web 与 desktop 两个宿主共用同一个 hub 时
|
|
1253
|
+
// 没有这把锁就会互相覆盖(丢场景档案与记忆启停,且无报错)。
|
|
1254
|
+
withWriteLock,
|
|
1178
1255
|
});
|
|
1179
1256
|
// 场景记忆段的注册/生命周期由下面的注入通道统一管(memoriesService.memoryText / promptText)。
|
|
1180
1257
|
// ---------- 场景档案引擎----------
|
|
@@ -1396,9 +1473,9 @@ export default {
|
|
|
1396
1473
|
}
|
|
1397
1474
|
}
|
|
1398
1475
|
/**
|
|
1399
|
-
* 场景应用写全局意图:`blocked
|
|
1476
|
+
* 场景应用写全局意图:`blocked`(全部关闭)是比场景更强的用户要求,场景不得覆盖它
|
|
1400
1477
|
* —— 只有全局意图开关(`scope-intent-set`)能解除。不挡的话,进一次包含这台服务器 /
|
|
1401
|
-
*
|
|
1478
|
+
* 这条技能的场景,就会把「全部关闭」悄悄降级成 off,之后任何会话的租约都能把它拉起来。
|
|
1402
1479
|
*/
|
|
1403
1480
|
function withSceneIntent(current, domain, key, state) {
|
|
1404
1481
|
if (intentOf(current, domain, key) === 'blocked')
|
|
@@ -1485,7 +1562,7 @@ export default {
|
|
|
1485
1562
|
// 场景模式进出一次就会改写 state.json。意图那一格同口径。
|
|
1486
1563
|
const { states, shadowed, unloadable, entryNames } = await skillScan();
|
|
1487
1564
|
const intentSnap = intentStore.snapshot();
|
|
1488
|
-
/**
|
|
1565
|
+
/** 全局「全部关闭」的技能:场景不得覆盖(见 `withSceneIntent`),物理启停也一并跳过。 */
|
|
1489
1566
|
const blocked = new Set();
|
|
1490
1567
|
const planned = [];
|
|
1491
1568
|
const skipped = [];
|
|
@@ -1505,13 +1582,13 @@ export default {
|
|
|
1505
1582
|
skipped.push(`${key}(已不存在)`);
|
|
1506
1583
|
continue;
|
|
1507
1584
|
}
|
|
1508
|
-
//
|
|
1509
|
-
//
|
|
1585
|
+
// 全局全部关闭压过场景(与 `scope-set` 拒写「开」同一条):既不改意图也不动策略表。
|
|
1586
|
+
// 技能还要看所在来源:来源被全部关闭时,技能级开关开着也加载不到(来源闸恒生效)。
|
|
1510
1587
|
if (intentOf(intentSnap, 'skills', key) === 'blocked' ||
|
|
1511
1588
|
intentOf(intentSnap, 'skillSources', key.slice(0, i)) === 'blocked') {
|
|
1512
1589
|
blocked.add(key);
|
|
1513
1590
|
if (on)
|
|
1514
|
-
skipped.push(`${key}
|
|
1591
|
+
skipped.push(`${key}(已在全局「全部关闭」)`);
|
|
1515
1592
|
continue;
|
|
1516
1593
|
}
|
|
1517
1594
|
const wanted = on ? 'on' : 'off';
|
|
@@ -1600,7 +1677,7 @@ export default {
|
|
|
1600
1677
|
const prev = wanted.get(name);
|
|
1601
1678
|
wanted.set(name, prev === undefined ? s.enabled : prev || s.enabled);
|
|
1602
1679
|
}
|
|
1603
|
-
//
|
|
1680
|
+
// 全局「全部关闭」压过场景:不改意图、不动补丁(见 `withSceneIntent`)。
|
|
1604
1681
|
const blocked = new Set();
|
|
1605
1682
|
for (const [name, enabled] of [...wanted]) {
|
|
1606
1683
|
if (intentOf(intentSnap, 'mcp', name) !== 'blocked')
|
|
@@ -1608,7 +1685,7 @@ export default {
|
|
|
1608
1685
|
wanted.delete(name);
|
|
1609
1686
|
blocked.add(name);
|
|
1610
1687
|
if (enabled)
|
|
1611
|
-
ctx.logger?.warn?.(`场景要启用 MCP 服务器「${name}
|
|
1688
|
+
ctx.logger?.warn?.(`场景要启用 MCP 服务器「${name}」,但它已在全局「全部关闭」:已跳过`);
|
|
1612
1689
|
}
|
|
1613
1690
|
if (wanted.size > 0) {
|
|
1614
1691
|
await intentStore.mutate((current) => {
|
|
@@ -1691,14 +1768,14 @@ export default {
|
|
|
1691
1768
|
// 与 MCP 同一条:场景引擎改的是全局定义 → 先写意图,再改来源状态(见
|
|
1692
1769
|
// applyMcpServerSwitches 的注释;运行态 = 意图 ∪ 租约)。
|
|
1693
1770
|
const intentSnap = intentStore.snapshot();
|
|
1694
|
-
//
|
|
1771
|
+
// 全局「全部关闭」压过场景:不改意图、不动来源状态(见 `withSceneIntent`)。
|
|
1695
1772
|
const blocked = new Set();
|
|
1696
1773
|
for (const s of switches) {
|
|
1697
1774
|
if (intentOf(intentSnap, 'skillSources', s.root) !== 'blocked')
|
|
1698
1775
|
continue;
|
|
1699
1776
|
blocked.add(s.root);
|
|
1700
1777
|
if (s.enabled)
|
|
1701
|
-
ctx.logger?.warn?.(`场景要启用技能来源「${s.root}
|
|
1778
|
+
ctx.logger?.warn?.(`场景要启用技能来源「${s.root}」,但它已在全局「全部关闭」:已跳过`);
|
|
1702
1779
|
}
|
|
1703
1780
|
if (switches.length > 0) {
|
|
1704
1781
|
await intentStore.mutate((current) => {
|
|
@@ -3138,7 +3215,7 @@ export default {
|
|
|
3138
3215
|
//
|
|
3139
3216
|
// 只问**全局关着**的那些格:开着的那些本来就该显示成开着,数它没有意义,而记忆这类域
|
|
3140
3217
|
// 可能有几百条键 —— 每次读面板都全扫一遍是不必要的成本。
|
|
3141
|
-
// 键集合的口径:**意图域**只数「意图不是 on
|
|
3218
|
+
// 键集合的口径:**意图域**只数「意图不是 on」的键(全局开着不是租约,全部关闭没有
|
|
3142
3219
|
// 租约这回事);**纯视图域**只数「全局关着」的键。两句话都只回答"这一格该不该挂徽章"。
|
|
3143
3220
|
const leaseKeys = {};
|
|
3144
3221
|
for (const domain of SCOPE_FLAG_DOMAINS) {
|
|
@@ -3222,7 +3299,7 @@ export default {
|
|
|
3222
3299
|
flags,
|
|
3223
3300
|
unavailable,
|
|
3224
3301
|
// 三态意图与租约计数(界面用):`flags` 把 blocked 投影成 false,分不出「关着」与
|
|
3225
|
-
//
|
|
3302
|
+
// 「全部关闭」;租约计数则是全局 off 却仍在跑的唯一解释。
|
|
3226
3303
|
intent: { mcp: intentSnapshot.mcp, skillSources: intentSnapshot.skillSources, skills: intentSnapshot.skills },
|
|
3227
3304
|
leases: {
|
|
3228
3305
|
mcp: leaseCounts('mcp'),
|
|
@@ -3654,7 +3731,7 @@ export default {
|
|
|
3654
3731
|
const rows = rowsIn.map((row) => {
|
|
3655
3732
|
const name = String((row && (row.serverName || row.id)) || '');
|
|
3656
3733
|
const state = intentOf(intent, 'mcp', name);
|
|
3657
|
-
// 全局开着(`on
|
|
3734
|
+
// 全局开着(`on`)不是租约;全部关闭(`blocked`)压过一切会话层 —— 两种都不挂徽章。
|
|
3658
3735
|
const leaseCount = state === 'on' || state === 'blocked' || scan === null ? 0 : (scan.claims.mcp.get(name) || []).length;
|
|
3659
3736
|
return {
|
|
3660
3737
|
...row,
|
|
@@ -5470,6 +5547,7 @@ export default {
|
|
|
5470
5547
|
ctx.effect(() => {
|
|
5471
5548
|
const stop = ctx.on('agent/created', (payload) => {
|
|
5472
5549
|
mcp.attachAgent(payload && payload.agent);
|
|
5550
|
+
warmMcpPromptShadows(payload && payload.agent);
|
|
5473
5551
|
// 技能目录按会话 cwd 取数:先热一份,否则该会话首轮注入会少掉整段目录。
|
|
5474
5552
|
// 子代理热的是**所属用户会话**那一份:它的目录 / 覆盖都继承父会话(见 `sessionScopeIdOf`)。
|
|
5475
5553
|
const scopeSid = sessionScopeIdOf(payload && payload.agent);
|
|
@@ -5486,13 +5564,16 @@ export default {
|
|
|
5486
5564
|
ctx.effect(() => {
|
|
5487
5565
|
const stop = ctx.on('agent/disposed', (payload) => {
|
|
5488
5566
|
mcp.detachAgent(payload && payload.agent);
|
|
5567
|
+
mcpPromptShadow.dispose(payload && payload.agent);
|
|
5489
5568
|
});
|
|
5490
5569
|
return typeof stop === 'function' ? stop : () => { };
|
|
5491
5570
|
}, 'dsh-plugin-tool-management: mcp visibility (agent disposed)');
|
|
5571
|
+
ctx.effect(() => () => { mcpPromptShadow.disposeAll(); }, 'dsh-plugin-tool-management: mcp prompt shadow cleanup');
|
|
5492
5572
|
const agents = typeof ctx.get === 'function' ? ctx.get('agents') : undefined;
|
|
5493
5573
|
if (agents && typeof agents.list === 'function') {
|
|
5494
5574
|
for (const agent of agents.list()) {
|
|
5495
5575
|
mcp.attachAgent(agent);
|
|
5576
|
+
warmMcpPromptShadows(agent);
|
|
5496
5577
|
const scopeSid = sessionScopeIdOf(agent);
|
|
5497
5578
|
if (scopeSid !== null)
|
|
5498
5579
|
void skillCatalog.warmSession(scopeSid);
|
package/lib/mcp/manager.js
CHANGED
|
@@ -1994,7 +1994,8 @@ export function createMcpManager(deps) {
|
|
|
1994
1994
|
// ① 重扫 = 重新读取并解析两个补丁文件,N 条就是 N 次全量解析(行数多时平方级);
|
|
1995
1995
|
// ② 顺带把「整批导入」变成对其它写者的原子操作,不再允许别的写插到条目之间。
|
|
1996
1996
|
// 存在性判定仍留在锁内(TOCTOU 的初衷不变),改成从一次性快照增量维护。
|
|
1997
|
-
// 注意 withWriteLock
|
|
1997
|
+
// 注意 withWriteLock 在同一异步调用链上可重入(重入并入外层临界区、不再排队),
|
|
1998
|
+
// 循环体里仍不必再包一层:外层已经持锁。
|
|
1998
1999
|
await withWriteLock(async () => {
|
|
1999
2000
|
const existing = await collectAll();
|
|
2000
2001
|
// 按 id / serverName 两张表维护同一份快照:覆盖模式会摘掉旧行,两张表必须同步。
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// MCP 跨会话 prompt 隔离:用官方 `systemPrompt` 在 **agent 作用域**注册同名段,
|
|
2
|
+
// 遮蔽根作用域注册的 `mcp-resource-servers` 与 `mcp:<server>`。
|
|
3
|
+
//
|
|
4
|
+
// 为什么必须走同名段:这两类段由 `dsh-mcp-resources` / `dsh-mcp-client` 注册在根作用域,
|
|
5
|
+
// 官方只按「全局层 + 作用域链」解析 provider,不看插件的会话可见性。不能 patch 官方包,
|
|
6
|
+
// 而 `systemPrompt.section()` 的官方语义是「scoped section shadows a global section with
|
|
7
|
+
// the same name」—— 这是唯一能把别的对话的服务器名与 instructions 从本对话提示词里拿掉的
|
|
8
|
+
// 接缝。空文本段会在组装阶段被过滤(`dsh-system-prompt` 的 `renderSections` 只保留非空
|
|
9
|
+
// 文本),所以隐藏服务器的 instructions 直接消失;资源段则替换成只列本会话可见服务器的
|
|
10
|
+
// 等价文本,避免模型被提示去调用已经从工具表里摘掉的三个工具。
|
|
11
|
+
/** 官方共享资源段的名称。 */
|
|
12
|
+
export const MCP_RESOURCE_SECTION_NAME = 'mcp-resource-servers';
|
|
13
|
+
/** 官方 MCP 段使用的排序名(`systemPrompt.getSectionOrder` 的入参)。 */
|
|
14
|
+
export const MCP_SERVERS_SECTION_ORDER = 'MCP_SERVERS';
|
|
15
|
+
/** 官方 `mcp:<server>` 段的名称。 */
|
|
16
|
+
export function mcpServerSectionName(server) {
|
|
17
|
+
return 'mcp:' + server;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 按本会话可见的服务器名,算出要遮蔽哪些 prompt 段。
|
|
21
|
+
*
|
|
22
|
+
* 只有**存在隐藏服务器**时才替换资源段:全部可见时官方段的内容正好等于可见集合,
|
|
23
|
+
* 替换没有收益,反而会丢失官方后续的文案调整。
|
|
24
|
+
*/
|
|
25
|
+
export function mcpPromptShadowPlan(names, visible) {
|
|
26
|
+
const shown = new Set(visible);
|
|
27
|
+
const hidden = names.filter((name) => !shown.has(name));
|
|
28
|
+
return { resource: names.length > 0 && hidden.length > 0, hidden };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 资源服务器段的等价文本。
|
|
32
|
+
*
|
|
33
|
+
* 文案与官方保持一致(标题 + 三个工具名 + server 名单),只把名单收窄到本会话可见的那些;
|
|
34
|
+
* 名单为空时返回空串,让组装阶段把整段过滤掉。
|
|
35
|
+
*/
|
|
36
|
+
export function renderMcpResourceServersSection(visible) {
|
|
37
|
+
if (visible.length === 0)
|
|
38
|
+
return '';
|
|
39
|
+
const names = [...visible].sort();
|
|
40
|
+
return '## MCP resource servers\n\nUse list_mcp_resources, list_mcp_resource_templates, or read_mcp_resource with one of these names as the server argument: ' + JSON.stringify(names) + '.';
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 创建提示词遮蔽器。
|
|
44
|
+
*
|
|
45
|
+
* @param systemPromptOf 从 agent 取 `systemPrompt` 服务;取不到时返回 undefined(该 agent 不遮蔽)。
|
|
46
|
+
* @returns 按 agent 差分注册 / 撤销段的句柄。
|
|
47
|
+
*/
|
|
48
|
+
export function createMcpPromptShadow(systemPromptOf) {
|
|
49
|
+
const states = new Map();
|
|
50
|
+
function release(dispose) {
|
|
51
|
+
if (dispose === undefined)
|
|
52
|
+
return;
|
|
53
|
+
try {
|
|
54
|
+
dispose();
|
|
55
|
+
}
|
|
56
|
+
catch { /* agent 作用域已经释放 */ }
|
|
57
|
+
}
|
|
58
|
+
function sync(agent, names, visible, options) {
|
|
59
|
+
const systemPrompt = systemPromptOf(agent);
|
|
60
|
+
if (systemPrompt === undefined)
|
|
61
|
+
return;
|
|
62
|
+
const order = Number(systemPrompt.getSectionOrder(MCP_SERVERS_SECTION_ORDER));
|
|
63
|
+
if (!Number.isFinite(order))
|
|
64
|
+
return;
|
|
65
|
+
let state = states.get(agent);
|
|
66
|
+
if (state === undefined) {
|
|
67
|
+
state = { visible: [], servers: new Map() };
|
|
68
|
+
states.set(agent, state);
|
|
69
|
+
}
|
|
70
|
+
const current = state;
|
|
71
|
+
current.visible = [...visible];
|
|
72
|
+
const plan = mcpPromptShadowPlan(names, visible);
|
|
73
|
+
const wantResource = plan.resource && options?.resourceAvailable !== false;
|
|
74
|
+
if (wantResource) {
|
|
75
|
+
if (current.resource === undefined) {
|
|
76
|
+
try {
|
|
77
|
+
const dispose = systemPrompt.section({
|
|
78
|
+
name: MCP_RESOURCE_SECTION_NAME,
|
|
79
|
+
order,
|
|
80
|
+
interpolate: false,
|
|
81
|
+
text: () => renderMcpResourceServersSection(current.visible),
|
|
82
|
+
});
|
|
83
|
+
current.resource = typeof dispose === 'function' ? dispose : () => { };
|
|
84
|
+
}
|
|
85
|
+
catch { /* 同名段已被别的 scoped 层占用:保留官方段,不把功能弄坏 */ }
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (current.resource !== undefined) {
|
|
89
|
+
release(current.resource);
|
|
90
|
+
current.resource = undefined;
|
|
91
|
+
}
|
|
92
|
+
const hidden = new Set(plan.hidden);
|
|
93
|
+
for (const [server, dispose] of [...current.servers]) {
|
|
94
|
+
if (hidden.has(server))
|
|
95
|
+
continue;
|
|
96
|
+
release(dispose);
|
|
97
|
+
current.servers.delete(server);
|
|
98
|
+
}
|
|
99
|
+
for (const server of plan.hidden) {
|
|
100
|
+
if (current.servers.has(server))
|
|
101
|
+
continue;
|
|
102
|
+
try {
|
|
103
|
+
const dispose = systemPrompt.section({
|
|
104
|
+
name: mcpServerSectionName(server),
|
|
105
|
+
order,
|
|
106
|
+
interpolate: false,
|
|
107
|
+
text: () => '',
|
|
108
|
+
});
|
|
109
|
+
current.servers.set(server, typeof dispose === 'function' ? dispose : () => { });
|
|
110
|
+
}
|
|
111
|
+
catch { /* 同上 */ }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function dispose(agent) {
|
|
115
|
+
const state = states.get(agent);
|
|
116
|
+
if (state === undefined)
|
|
117
|
+
return;
|
|
118
|
+
release(state.resource);
|
|
119
|
+
for (const disposeSection of state.servers.values())
|
|
120
|
+
release(disposeSection);
|
|
121
|
+
states.delete(agent);
|
|
122
|
+
}
|
|
123
|
+
function disposeAll() {
|
|
124
|
+
for (const agent of [...states.keys()])
|
|
125
|
+
dispose(agent);
|
|
126
|
+
}
|
|
127
|
+
return { sync, dispose, disposeAll };
|
|
128
|
+
}
|
|
@@ -295,7 +295,8 @@ export function createToolVisibility(deps) {
|
|
|
295
295
|
*
|
|
296
296
|
* `mutate` 收到的是**锁内刚读出来**的表(`force` 绕 TTL,否则拿到的还是锁外那份缓存);
|
|
297
297
|
* 返回 `true` 表示有变化、需要落盘。
|
|
298
|
-
* 注意:`withWriteLock`
|
|
298
|
+
* 注意:`withWriteLock` 在同一异步调用链上可重入(重入直接并入外层临界区、不再排队),
|
|
299
|
+
* 但重入不会重新读盘 —— 需要"锁内刚写下的值"时必须显式 `force`。
|
|
299
300
|
*/
|
|
300
301
|
async function mutateKnownMcpTools(mutate) {
|
|
301
302
|
return deps.withWriteLock(async () => {
|
|
@@ -497,18 +498,29 @@ export function createToolVisibility(deps) {
|
|
|
497
498
|
});
|
|
498
499
|
return;
|
|
499
500
|
}
|
|
500
|
-
|
|
501
|
-
|
|
501
|
+
// 从枚举到装层之间可能过去一拍:服务器恰好停了 / 注册被摘掉,而官方 `restrict` 对
|
|
502
|
+
// 认不出的名字是**整份名单抛错**(不是跳过那一个)—— 一个过期名字就会让所有工具重新
|
|
503
|
+
// 出现在模型工具表里。装之前再问一次全局视图,把已经不存在的名字剔掉。
|
|
504
|
+
const denyNames = typeof deps.tools.get === 'function'
|
|
505
|
+
? names.filter((name) => { try {
|
|
506
|
+
return deps.tools.get(name) !== undefined;
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
return false;
|
|
510
|
+
} })
|
|
511
|
+
: names;
|
|
512
|
+
const denyKey = denyNames.slice().sort().join('\u0000');
|
|
513
|
+
if (appliedByAgent.get(agent) === denyKey)
|
|
502
514
|
return;
|
|
503
515
|
liftRestriction(agent);
|
|
504
|
-
if (
|
|
516
|
+
if (denyNames.length === 0) {
|
|
505
517
|
clearRuntimeNote('mcp-tool-visibility');
|
|
506
518
|
return;
|
|
507
519
|
}
|
|
508
520
|
try {
|
|
509
|
-
const dispose = scoped.restrict({ deny:
|
|
521
|
+
const dispose = scoped.restrict({ deny: denyNames });
|
|
510
522
|
agentRestrictions.set(agent, typeof dispose === 'function' ? dispose : () => { });
|
|
511
|
-
appliedByAgent.set(agent,
|
|
523
|
+
appliedByAgent.set(agent, denyKey);
|
|
512
524
|
clearRuntimeNote('mcp-tool-visibility');
|
|
513
525
|
}
|
|
514
526
|
catch (e) {
|
package/lib/memories/service.js
CHANGED
|
@@ -454,6 +454,18 @@ export function createMemoriesService(ctx, deps) {
|
|
|
454
454
|
mutationQueue = queued.catch(() => undefined);
|
|
455
455
|
return queued;
|
|
456
456
|
};
|
|
457
|
+
/**
|
|
458
|
+
* 在跨进程写锁里跑一段读-改-写(锁由宿主注入;缺席时原地执行)。
|
|
459
|
+
*
|
|
460
|
+
* 为什么锁要套在**队列内层**:`mutationQueue` 只串行本进程,web 与 desktop 共用同一个
|
|
461
|
+
* `$DSH_HOME` 时,两边各自 readIndex → 改 → writeIndex 会整份覆盖对方。队列在外、锁在内
|
|
462
|
+
* 保证同一进程内先到先得,锁只覆盖真正的临界区(不把等待时间叠进队列)。
|
|
463
|
+
*
|
|
464
|
+
* 锁在同一异步调用链上可重入:MCP 改名 / 删台会在锁内回调本域的 `patchIndex`,那一条
|
|
465
|
+
* 直接并入外层临界区,不会排在**自己**后面。代价是重入方共享外层的持锁时长 ——
|
|
466
|
+
* 所以临界区里仍然只做索引与记忆文件的读写,不夹带目录扫描之类的大动作。
|
|
467
|
+
*/
|
|
468
|
+
const withProcessLock = (task) => deps.withWriteLock === undefined ? task() : deps.withWriteLock(task);
|
|
457
469
|
// ── 定位 / 解析 id ──────────────────────────────────────────────────────
|
|
458
470
|
/** id = "<group>/<name>";group 可含 '/'(多层),根下规则允许 group 为空。 */
|
|
459
471
|
function parseId(id) {
|
|
@@ -572,7 +584,7 @@ export function createMemoriesService(ctx, deps) {
|
|
|
572
584
|
* 与"落 mode + active"是两件事),而"我没打算改的字段"必须留给**队列内刚读出来**的值,
|
|
573
585
|
* 否则一次陈旧的整片回写会把窗口期里别人改的东西抹掉。
|
|
574
586
|
*/
|
|
575
|
-
const patchIndex = (patch, fields) => enqueueMutation(async () => {
|
|
587
|
+
const patchIndex = (patch, fields) => enqueueMutation(() => withProcessLock(async () => {
|
|
576
588
|
const index = await readIndex(stateDir);
|
|
577
589
|
const want = (f) => (fields ? fields.indexOf(f) >= 0 : patch[f] !== undefined);
|
|
578
590
|
if (want('mode') && patch.mode !== undefined)
|
|
@@ -584,7 +596,7 @@ export function createMemoriesService(ctx, deps) {
|
|
|
584
596
|
index.active = patch.active;
|
|
585
597
|
await writeIndex(stateDir, index);
|
|
586
598
|
await refresh();
|
|
587
|
-
});
|
|
599
|
+
}));
|
|
588
600
|
/**
|
|
589
601
|
* 场景档案引擎专用:只写**一个场景**的档案(队列内按**逐场景增量**落盘)。
|
|
590
602
|
*
|
|
@@ -593,7 +605,7 @@ export function createMemoriesService(ctx, deps) {
|
|
|
593
605
|
* 模型工具 / 另一条引擎路径)保存了场景 B 的档案,前者落盘时会把 B 的整份覆盖掉。
|
|
594
606
|
* 逐场景增量只动自己那一条,其余键保留队列内读到的值。
|
|
595
607
|
*/
|
|
596
|
-
const saveArchive = (scene, archive) => enqueueMutation(async () => {
|
|
608
|
+
const saveArchive = (scene, archive) => enqueueMutation(() => withProcessLock(async () => {
|
|
597
609
|
const index = await readIndex(stateDir);
|
|
598
610
|
const archives = index.archives ?? {};
|
|
599
611
|
if (archive === null)
|
|
@@ -603,7 +615,7 @@ export function createMemoriesService(ctx, deps) {
|
|
|
603
615
|
index.archives = archives;
|
|
604
616
|
await writeIndex(stateDir, index);
|
|
605
617
|
await refresh();
|
|
606
|
-
});
|
|
618
|
+
}));
|
|
607
619
|
/** 场景档案引擎专用:读 mode/archives/active 切片(容忍缺失,缺省 = 无档案 + 自由模式 + 全部启用)。 */
|
|
608
620
|
const readArchiveSlice = async () => {
|
|
609
621
|
const index = await readIndex(stateDir);
|
|
@@ -930,7 +942,8 @@ export function createMemoriesService(ctx, deps) {
|
|
|
930
942
|
};
|
|
931
943
|
/** 写操作:串行队列内执行,成功后触发 refresh(失效缓存,下一请求即生效)。 */
|
|
932
944
|
const runWrite = (task) => enqueueMutation(async () => {
|
|
933
|
-
|
|
945
|
+
// 临界区只包写本身,refresh 放锁外:它只读盘,多挂一会儿只会让另一个宿主空等。
|
|
946
|
+
const result = await withProcessLock(task);
|
|
934
947
|
if (result && result.ok !== false)
|
|
935
948
|
await refresh();
|
|
936
949
|
return result;
|
package/lib/op-registry.js
CHANGED
|
@@ -282,7 +282,7 @@ export const OP_REGISTRY = Object.freeze({
|
|
|
282
282
|
// 与 `mcpm-set-enabled` / `skill-source-enable` 同档:写全局定义、场景锁要冻结它。
|
|
283
283
|
// 会话层的 scope-set 仍不冻结(它只写租约,不改全局)。
|
|
284
284
|
'scope-intent-set': { write: true, frozen: true, syncsArchive: true },
|
|
285
|
-
//
|
|
285
|
+
// 「全部关闭」/「全部停止」:一次性动作(关会话 / 工作区的开关 + 写 off),不是持久意图。
|
|
286
286
|
// 写全局意图之外还改会话侧车,所以照全局写入口同一档冻结;不同步场景档案 ——
|
|
287
287
|
// 它是一次性的,不该把场景里的勾选永久改掉。
|
|
288
288
|
'scope-stop-now': { write: true, frozen: true },
|
package/lib/ops/compat.js
CHANGED
|
@@ -93,6 +93,17 @@ export function buildCompatOps(deps) {
|
|
|
93
93
|
// 宿主非 peer 契约的实况:插件按裸模块名 require 的那几个官方包。与 findings 不同源
|
|
94
94
|
// (模块层 vs 服务层),所以单独一个字段,界面据此列出"裸模块解析"那一块。
|
|
95
95
|
...(assessment.contracts === undefined ? {} : { contracts: assessment.contracts }),
|
|
96
|
+
// 行为契约(事件名 / 数据布局 / 配置键 / 补丁方言):与上一份同一套判定,
|
|
97
|
+
// 但检查的是插件写死的宿主行为字符串 —— 官方改掉一个同样不抛错。
|
|
98
|
+
...(assessment.behaviors === undefined ? {} : { behaviors: assessment.behaviors }),
|
|
99
|
+
// 客户端 UI 契约(模块加载器 / 插槽名 / 客户端服务):服务端看不见浏览器,
|
|
100
|
+
// 只能核对宿主客户端 bundle 里的字符串标记。
|
|
101
|
+
...(assessment.clientUi === undefined ? {} : { clientUi: assessment.clientUi }),
|
|
102
|
+
// 只能人工核对的项:能力表探针与契约 canary 的盲区,随诊断 JSON 返回,页面不展示。
|
|
103
|
+
manualChecks: assessment.manualChecks,
|
|
104
|
+
// 逐包版本:每个 peer 在宿主 / 插件两侧的版本、适配基准与 peer 范围判定。
|
|
105
|
+
// 升级后"哪个包先变了、还在不在范围内"由这一份回答。
|
|
106
|
+
packages: assessment.identity.packages,
|
|
96
107
|
findings,
|
|
97
108
|
degraded,
|
|
98
109
|
blockers: assessment.identity.blockers,
|
|
@@ -209,6 +220,26 @@ export function buildCompatOps(deps) {
|
|
|
209
220
|
...noteFields(note),
|
|
210
221
|
});
|
|
211
222
|
}
|
|
223
|
+
// 宿主契约层:模块还在、入口字符串没了(或整个包不在了)时,对应功能可能已经按旧契约
|
|
224
|
+
// 在跑。单独一行汇总,让"哪些功能要复核"出现在功能总览里 —— 不能只在下方契约表里
|
|
225
|
+
// 列一串包名,页头/总览却全是绿的。
|
|
226
|
+
const contractIssues = assessment === undefined ? [] : [
|
|
227
|
+
...(assessment.contracts?.packages ?? []),
|
|
228
|
+
...(assessment.behaviors?.packages ?? []),
|
|
229
|
+
...(assessment.clientUi?.packages ?? []),
|
|
230
|
+
].filter((item) => item.resolved === null ? item.optional !== true : item.missingMarkers.length > 0);
|
|
231
|
+
push({
|
|
232
|
+
key: 'host-contracts',
|
|
233
|
+
label: '宿主契约(事件 / 配置键 / 布局 / 界面)',
|
|
234
|
+
tab: 'compat',
|
|
235
|
+
state: contractIssues.length === 0 ? 'ok' : 'degraded',
|
|
236
|
+
detail: contractIssues.length === 0
|
|
237
|
+
? 'canary 齐全:事件名、配置键、磁盘布局与客户端界面标记仍与适配基准一致'
|
|
238
|
+
: `需复核 ${contractIssues.length} 个包:` + contractIssues.map((item) => `${item.package}(${item.feature})`).join('、'),
|
|
239
|
+
...(contractIssues.length === 0
|
|
240
|
+
? {}
|
|
241
|
+
: { contracts: contractIssues.map((item) => ({ package: item.package, feature: item.feature })) }),
|
|
242
|
+
});
|
|
212
243
|
// 宿主能力层:删除 / 归档 / 恢复 / 批量各自由路由判定回答(与服务端执行同源)。
|
|
213
244
|
const deleteRoute = routeOf('delete');
|
|
214
245
|
push({
|