dsh-skill-pool 0.3.7 → 0.4.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/CONTRACTS-v3.md +129 -12
- package/README.md +173 -14
- package/client.js +341 -73
- package/index.js +58 -16
- package/package.json +8 -2
- package/src/activation.js +584 -115
- package/src/inventory.js +6 -1
- package/src/mount.js +9 -4
- package/src/rules.js +29 -0
- package/src/store.js +494 -28
- package/src/web-api.js +413 -69
package/src/activation.js
CHANGED
|
@@ -27,9 +27,10 @@
|
|
|
27
27
|
|
|
28
28
|
import { librarySkillsRoot } from './library.js'
|
|
29
29
|
import { createLibraryProvider } from './provider.js'
|
|
30
|
-
import { mcpServerOf, MCP_TOOL_PREFIX, resolvePool } from './rules.js'
|
|
30
|
+
import { isAmbiguousMcpToolName, mcpServerOf, MCP_TOOL_PREFIX, resolvePool } from './rules.js'
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
/** Labels of effects that restrict the MCP tool surface (normal or fallback). */
|
|
33
|
+
const RESTRICT_LABELS = new Set(['mcp-restrict', 'mcp-restrict-fallback'])
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Read the Agent mode of one live Agent.
|
|
@@ -94,109 +95,384 @@ export function workspaceOf(agent) {
|
|
|
94
95
|
return process.cwd()
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
/** Chinese prompt summary. */
|
|
98
|
-
function summaryTextZh({ mode, cwd, skillNames, serverNames, skillGated, mcpGated }) {
|
|
99
|
-
const lines = ['## 技能池已激活', '', `Agent 模式:\`${mode}\`,工作区:\`${cwd}\`。`]
|
|
100
|
-
if (skillGated) {
|
|
101
|
-
lines.push(
|
|
102
|
-
skillNames.length > 0
|
|
103
|
-
? `技能池在本会话提供了这些技能:${skillNames.join('、')}。`
|
|
104
|
-
: '技能池在本会话没有提供任何技能。',
|
|
105
|
-
)
|
|
106
|
-
}
|
|
107
|
-
if (mcpGated) {
|
|
108
|
-
lines.push(
|
|
109
|
-
serverNames.length > 0
|
|
110
|
-
? `本会话可用的 MCP 连接只有:${serverNames.join('、')};其它 MCP 工具已被隐藏。`
|
|
111
|
-
: '本会话没有任何可用 MCP 连接;所有 MCP 工具已被隐藏。',
|
|
112
|
-
)
|
|
113
|
-
}
|
|
114
|
-
lines.push(
|
|
115
|
-
'未列出的技能池资源在本模式/工作区下确实不可用:不要尝试调用,也不要凭记忆复述它们的内容。',
|
|
116
|
-
'如果任务确实需要某个不可用的资源,请让用户在「设置 → 技能池」里为它加上当前的 Agent 模式或工作区条件。',
|
|
117
|
-
)
|
|
118
|
-
return lines.join('\n')
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** English prompt summary. */
|
|
122
|
-
function summaryTextEn({ mode, cwd, skillNames, serverNames, skillGated, mcpGated }) {
|
|
123
|
-
const lines = ['## Resource pool active', '', `Agent mode \`${mode}\`, workspace \`${cwd}\`.`]
|
|
124
|
-
if (skillGated) {
|
|
125
|
-
lines.push(
|
|
126
|
-
skillNames.length > 0
|
|
127
|
-
? `The resource pool supplies these skills in this session: ${skillNames.join(', ')}.`
|
|
128
|
-
: 'The resource pool supplies no skills in this session.',
|
|
129
|
-
)
|
|
130
|
-
}
|
|
131
|
-
if (mcpGated) {
|
|
132
|
-
lines.push(
|
|
133
|
-
serverNames.length > 0
|
|
134
|
-
? `The only MCP connections available in this session are: ${serverNames.join(', ')}; every other MCP tool is hidden.`
|
|
135
|
-
: 'No MCP connections are available in this session; all MCP tools are hidden.',
|
|
136
|
-
)
|
|
137
|
-
}
|
|
138
|
-
lines.push(
|
|
139
|
-
'Resource-pool resources that are not listed are genuinely unavailable in this mode/workspace: do not try to call them and do not recall their content from memory.',
|
|
140
|
-
'If a task really needs an unavailable resource, ask the user to add the current Agent mode or workspace to its condition in Settings → Resource pool.',
|
|
141
|
-
)
|
|
142
|
-
return lines.join('\n')
|
|
143
|
-
}
|
|
144
|
-
|
|
145
98
|
/**
|
|
146
99
|
* Create the activation manager.
|
|
147
100
|
* @param {object} options - manager options.
|
|
148
101
|
* @param {object} options.ctx - plugin context.
|
|
149
102
|
* @param {(fresh?: boolean) => Promise<object>} options.getState - read the current document.
|
|
150
103
|
* @param {{ debug?: Function, warn?: Function }} [options.logger] - logger.
|
|
151
|
-
* @param {() => string} [options.promptLanguage] - 'zh' | 'en'.
|
|
152
104
|
* @param {() => string} [options.libraryRoot] - library skills root override.
|
|
153
105
|
* @returns {object} manager with applyToAgent/reapplyAll/disposeAgent/disposeAll.
|
|
154
106
|
*/
|
|
155
|
-
export function createActivation({ ctx, getState, logger,
|
|
156
|
-
/** @type {WeakMap<object, {disposers: Function[], signature: string, chain: Promise<void>}>} */
|
|
107
|
+
export function createActivation({ ctx, getState, logger, libraryRoot = () => librarySkillsRoot() }) {
|
|
108
|
+
/** @type {WeakMap<object, {disposers: {dispose: Function, label: string}[], signature: string, activated: boolean, chain: Promise<void>}>} */
|
|
157
109
|
const agents = new WeakMap()
|
|
158
110
|
|
|
111
|
+
/** Dispose one recorded registration, tolerating an already-disposed effect. */
|
|
112
|
+
const disposeEntry = (entry) => {
|
|
113
|
+
try {
|
|
114
|
+
entry.dispose()
|
|
115
|
+
} catch {
|
|
116
|
+
/* already disposed */
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
159
120
|
/** Release every registration owned for one Agent. */
|
|
160
121
|
const release = (agent) => {
|
|
161
122
|
const record = agents.get(agent)
|
|
162
123
|
if (record === undefined) return
|
|
163
|
-
for (const
|
|
164
|
-
try {
|
|
165
|
-
dispose()
|
|
166
|
-
} catch {
|
|
167
|
-
/* already disposed */
|
|
168
|
-
}
|
|
169
|
-
}
|
|
124
|
+
for (const entry of record.disposers.reverse()) disposeEntry(entry)
|
|
170
125
|
record.disposers = []
|
|
171
126
|
record.signature = ''
|
|
127
|
+
record.activated = false
|
|
128
|
+
record.conservative = false
|
|
129
|
+
record.degraded = false
|
|
130
|
+
record.underRestricted = false
|
|
131
|
+
record.mcpGateActive = false
|
|
172
132
|
}
|
|
173
133
|
|
|
174
134
|
/** Signature of a decision, used to skip no-op re-applications. */
|
|
175
135
|
const signatureOf = (pool, allowed, libraryNames) =>
|
|
176
|
-
JSON.stringify({
|
|
136
|
+
JSON.stringify({
|
|
137
|
+
gated: pool.gated,
|
|
138
|
+
skills: allowed,
|
|
139
|
+
mcp: pool.mcpServers,
|
|
140
|
+
gs: pool.gateSkills,
|
|
141
|
+
gm: pool.gateMcp,
|
|
142
|
+
lib: libraryNames,
|
|
143
|
+
})
|
|
177
144
|
|
|
178
|
-
/**
|
|
179
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Serialize applications per Agent.
|
|
147
|
+
*
|
|
148
|
+
* A failure for an Agent that already has a live activation is contained: the
|
|
149
|
+
* previous registrations stay installed and are never released before the new
|
|
150
|
+
* ones are known to be safe. A failure for an Agent with no activation is
|
|
151
|
+
* propagated to the caller, so `agent/created` rejects and no ungated Agent is
|
|
152
|
+
* published.
|
|
153
|
+
*/
|
|
154
|
+
const applyToAgent = async (agent, { force = false, conservative = false } = {}) => {
|
|
180
155
|
if (agent?.ctx === undefined) return
|
|
181
156
|
let record = agents.get(agent)
|
|
182
157
|
if (record === undefined) {
|
|
183
|
-
record = {
|
|
158
|
+
record = {
|
|
159
|
+
disposers: [],
|
|
160
|
+
signature: '',
|
|
161
|
+
activated: false,
|
|
162
|
+
conservative: false,
|
|
163
|
+
degraded: false,
|
|
164
|
+
underRestricted: false,
|
|
165
|
+
mcpGateActive: false,
|
|
166
|
+
chain: Promise.resolve(),
|
|
167
|
+
}
|
|
184
168
|
agents.set(agent, record)
|
|
185
169
|
}
|
|
186
|
-
const
|
|
170
|
+
const run = record.chain.then(() => applyNow(agent, record, { force }))
|
|
171
|
+
// Keep the per-Agent chain fulfilled even when this application failed, so a
|
|
172
|
+
// later retry (after the file is repaired) is not wedged behind it.
|
|
173
|
+
record.chain = run.then(
|
|
174
|
+
() => undefined,
|
|
175
|
+
() => undefined,
|
|
176
|
+
)
|
|
177
|
+
try {
|
|
178
|
+
await run
|
|
179
|
+
if (record.underRestricted) {
|
|
180
|
+
return {
|
|
181
|
+
ok: false,
|
|
182
|
+
code: 'resource-pool/activation-incomplete',
|
|
183
|
+
message: 'MCP deny is incomplete; a tool whose restrict layer failed may be callable until the next replay',
|
|
184
|
+
underRestricted: true,
|
|
185
|
+
overRestricted: record.degraded === true,
|
|
186
|
+
conservative: false,
|
|
187
|
+
noVisibleTools: false,
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return record.degraded
|
|
191
|
+
? {
|
|
192
|
+
ok: true,
|
|
193
|
+
degraded: true,
|
|
194
|
+
code: 'resource-pool/activation-over-restricted',
|
|
195
|
+
message: 'some previously denied tools are not currently visible; old deny layers were retained and may over-restrict',
|
|
196
|
+
underRestricted: false,
|
|
197
|
+
overRestricted: true,
|
|
198
|
+
conservative: false,
|
|
199
|
+
noVisibleTools: false,
|
|
200
|
+
}
|
|
201
|
+
: { ok: true, underRestricted: false, overRestricted: false, conservative: false, noVisibleTools: false }
|
|
202
|
+
} catch (error) {
|
|
203
|
+
const hadActivation = record.activated
|
|
187
204
|
logger?.warn?.(`[resource-pool] activation failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
205
|
+
if (hadActivation) {
|
|
206
|
+
// A conservative fallback is refreshable: every explicit re-apply must
|
|
207
|
+
// see newly registered MCP tools even while the document is unreadable.
|
|
208
|
+
if (record.conservative && conservative) {
|
|
209
|
+
try {
|
|
210
|
+
return conservativeResult(agent)
|
|
211
|
+
} catch (gateError) {
|
|
212
|
+
logger?.warn?.(`[resource-pool] conservative MCP gate refresh failed: ${gateError instanceof Error ? gateError.message : String(gateError)}`)
|
|
213
|
+
const retained = record.disposers.some((entry) => RESTRICT_LABELS.has(entry.label))
|
|
214
|
+
return {
|
|
215
|
+
ok: false,
|
|
216
|
+
code: 'resource-pool/activation-degraded',
|
|
217
|
+
message: 'state unavailable; conservative MCP deny refresh failed and previous layers were retained',
|
|
218
|
+
fallbackFailed: true,
|
|
219
|
+
retainedOldGate: retained,
|
|
220
|
+
underRestricted: false,
|
|
221
|
+
overRestricted: false,
|
|
222
|
+
conservative: false,
|
|
223
|
+
noVisibleTools: false,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// A healthy old layer is deliberately still installed. Reporting success
|
|
228
|
+
// would hide a degraded state; rejecting would make the Agent unusable
|
|
229
|
+
// even though its previous, authorized gate still protects it.
|
|
230
|
+
const hasRestrictLayer = record.disposers.some((entry) => RESTRICT_LABELS.has(entry.label))
|
|
231
|
+
if (conservative && record.mcpGateActive !== false && !hasRestrictLayer) {
|
|
232
|
+
// The previous successful activation intentionally allowed every
|
|
233
|
+
// visible tool (so it had zero restrict layers). Treating that as a
|
|
234
|
+
// "retained gate" would leave a later failure ungated. Install a
|
|
235
|
+
// conservative deny for all currently known MCP tools instead.
|
|
236
|
+
try {
|
|
237
|
+
return conservativeResult(agent)
|
|
238
|
+
} catch (gateError) {
|
|
239
|
+
logger?.warn?.(
|
|
240
|
+
`[resource-pool] conservative MCP gate failed without a prior layer: ${gateError instanceof Error ? gateError.message : String(gateError)}`,
|
|
241
|
+
)
|
|
242
|
+
return {
|
|
243
|
+
ok: false,
|
|
244
|
+
code: 'resource-pool/activation-degraded',
|
|
245
|
+
message: 'state unavailable and no prior MCP restriction could be installed',
|
|
246
|
+
fallbackFailed: true,
|
|
247
|
+
retainedOldGate: false,
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (hasRestrictLayer) {
|
|
252
|
+
return {
|
|
253
|
+
ok: false,
|
|
254
|
+
code: 'resource-pool/activation-degraded',
|
|
255
|
+
message: 'activation failed; previous MCP restriction layer retained',
|
|
256
|
+
retainedOldGate: true,
|
|
257
|
+
underRestricted: false,
|
|
258
|
+
overRestricted: false,
|
|
259
|
+
conservative: false,
|
|
260
|
+
noVisibleTools: false,
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
ok: false,
|
|
265
|
+
code: 'resource-pool/activation-degraded',
|
|
266
|
+
message: 'activation failed; no prior MCP restriction layer to retain',
|
|
267
|
+
retainedOldGate: false,
|
|
268
|
+
underRestricted: false,
|
|
269
|
+
overRestricted: false,
|
|
270
|
+
conservative: false,
|
|
271
|
+
noVisibleTools: false,
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (conservative) {
|
|
275
|
+
// A live Agent with no layer of ours (for example one that existed before
|
|
276
|
+
// a plugin reload) cannot be left ungated while the document is
|
|
277
|
+
// unreadable. Deny every currently visible MCP tool and keep the Agent
|
|
278
|
+
// alive; dynamic additions remain a documented gap.
|
|
279
|
+
try {
|
|
280
|
+
return conservativeResult(agent)
|
|
281
|
+
} catch (gateError) {
|
|
282
|
+
logger?.warn?.(
|
|
283
|
+
`[resource-pool] conservative MCP gate failed: ${gateError instanceof Error ? gateError.message : String(gateError)}`,
|
|
284
|
+
)
|
|
285
|
+
return {
|
|
286
|
+
ok: false,
|
|
287
|
+
code: 'resource-pool/activation-degraded',
|
|
288
|
+
message: 'state unavailable; conservative MCP deny could not be installed',
|
|
289
|
+
fallbackFailed: true,
|
|
290
|
+
retainedOldGate: false,
|
|
291
|
+
underRestricted: false,
|
|
292
|
+
overRestricted: false,
|
|
293
|
+
conservative: false,
|
|
294
|
+
noVisibleTools: false,
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// This is also the only safe response for an Agent with no previous gate:
|
|
299
|
+
// no caller may be told "activated" while nothing protects the Agent.
|
|
300
|
+
throw error
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Apply a deny-all-visible-MCP layer to one live Agent when the state cannot
|
|
306
|
+
* be read and there is no previous layer to keep. This is deliberately a
|
|
307
|
+
* static list: newly registered MCP tools can still appear, as documented.
|
|
308
|
+
*/
|
|
309
|
+
const applyConservativeGate = (agent) => {
|
|
310
|
+
const tools = ctx.get('tools')
|
|
311
|
+
if (tools === undefined || typeof tools.schemas !== 'function') {
|
|
312
|
+
throw new Error('global tools service is unavailable')
|
|
313
|
+
}
|
|
314
|
+
const schemas = tools.schemas()
|
|
315
|
+
if (!Array.isArray(schemas)) throw new Error('global tool list is not an array')
|
|
316
|
+
const visible = schemas
|
|
317
|
+
.map((schema) => schema?.name)
|
|
318
|
+
.filter((name) => typeof name === 'string' && name.startsWith(MCP_TOOL_PREFIX))
|
|
319
|
+
const visibleSet = new Set(visible)
|
|
320
|
+
const record = agents.get(agent)
|
|
321
|
+
if (record === undefined) throw new Error('agent activation record is missing')
|
|
322
|
+
|
|
323
|
+
if (visible.length === 0) {
|
|
324
|
+
// Keep every previous per-tool layer: an absent tool may simply be
|
|
325
|
+
// between generations, and we cannot register its name again now. There
|
|
326
|
+
// is nothing we can truthfully claim as a newly installed deny. Any old
|
|
327
|
+
// denied name is currently invisible, so retaining it is over-restriction
|
|
328
|
+
// risk (we cannot know whether it is still meant to be denied).
|
|
329
|
+
const retainedNames = record.disposers
|
|
330
|
+
.filter((entry) => RESTRICT_LABELS.has(entry.label))
|
|
331
|
+
.flatMap((entry) => (Array.isArray(entry.deny) ? entry.deny : []))
|
|
332
|
+
const over = retainedNames.length > 0
|
|
333
|
+
record.signature = ''
|
|
334
|
+
record.activated = true
|
|
335
|
+
record.conservative = true
|
|
336
|
+
record.degraded = over
|
|
337
|
+
record.underRestricted = false
|
|
338
|
+
logger?.warn?.('[resource-pool] state unavailable; no visible MCP tools, kept previous conservative deny')
|
|
339
|
+
return { visible: 0, failed: 0, under: false, over, kept: record.disposers.length > 0 }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const previous = record.disposers
|
|
343
|
+
const oldRestrictions = []
|
|
344
|
+
for (const entry of previous) {
|
|
345
|
+
if (RESTRICT_LABELS.has(entry.label)) oldRestrictions.push(entry)
|
|
346
|
+
else disposeEntry(entry)
|
|
347
|
+
}
|
|
348
|
+
const pending = []
|
|
349
|
+
const failedNames = []
|
|
350
|
+
const newEntries = []
|
|
351
|
+
try {
|
|
352
|
+
for (const name of visible) {
|
|
353
|
+
try {
|
|
354
|
+
const dispose = agent.ctx.effect(
|
|
355
|
+
() => agent.ctx.tools.restrict({ deny: [name] }),
|
|
356
|
+
'resource-pool.mcp-restrict-fallback',
|
|
357
|
+
)
|
|
358
|
+
const entry = {
|
|
359
|
+
dispose: typeof dispose === 'function' ? dispose : () => {},
|
|
360
|
+
label: 'mcp-restrict-fallback',
|
|
361
|
+
deny: [name],
|
|
362
|
+
}
|
|
363
|
+
pending.push(entry)
|
|
364
|
+
newEntries.push(entry)
|
|
365
|
+
} catch (error) {
|
|
366
|
+
failedNames.push(name)
|
|
367
|
+
logger?.warn?.(
|
|
368
|
+
`[resource-pool] one conservative MCP deny could not be registered (retaining old layers): ${error instanceof Error ? error.message : String(error)}`,
|
|
369
|
+
)
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const oldCoverage = new Set(oldRestrictions.flatMap((entry) => (Array.isArray(entry.deny) ? entry.deny : [])))
|
|
374
|
+
let retained = []
|
|
375
|
+
let retainedInvisible = false
|
|
376
|
+
if (failedNames.length > 0) {
|
|
377
|
+
retained = oldRestrictions
|
|
378
|
+
retainedInvisible = oldRestrictions.length > 0
|
|
379
|
+
} else {
|
|
380
|
+
for (const entry of oldRestrictions) {
|
|
381
|
+
const names = Array.isArray(entry.deny) ? entry.deny : []
|
|
382
|
+
if (names.some((name) => !visibleSet.has(name))) {
|
|
383
|
+
retained.push(entry)
|
|
384
|
+
retainedInvisible = true
|
|
385
|
+
} else {
|
|
386
|
+
disposeEntry(entry)
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
record.disposers = [...pending, ...retained]
|
|
391
|
+
|
|
392
|
+
const retainedCoverage = new Set(
|
|
393
|
+
retained.flatMap((entry) => (Array.isArray(entry.deny) ? entry.deny : [])),
|
|
394
|
+
)
|
|
395
|
+
for (const entry of newEntries) {
|
|
396
|
+
const name = entry.deny?.[0]
|
|
397
|
+
if (!retainedCoverage.has(name)) continue
|
|
398
|
+
disposeEntry(entry)
|
|
399
|
+
const index = pending.indexOf(entry)
|
|
400
|
+
if (index >= 0) pending.splice(index, 1)
|
|
401
|
+
record.disposers = record.disposers.filter((candidate) => candidate !== entry)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const over = retained.some((entry) =>
|
|
405
|
+
(Array.isArray(entry.deny) ? entry.deny : []).some((name) => !visibleSet.has(name)),
|
|
406
|
+
)
|
|
407
|
+
record.signature = ''
|
|
408
|
+
record.activated = true
|
|
409
|
+
record.conservative = true
|
|
410
|
+
record.degraded = over
|
|
411
|
+
const under = failedNames.some((name) => !oldCoverage.has(name))
|
|
412
|
+
record.underRestricted = under
|
|
413
|
+
logger?.warn?.(
|
|
414
|
+
failedNames.length > 0
|
|
415
|
+
? `[resource-pool] state unavailable; conservative MCP deny is incomplete (${failedNames.length} failed)`
|
|
416
|
+
: `[resource-pool] state unavailable; conservative MCP deny covers ${visible.length} visible tool${visible.length === 1 ? '' : 's'}`,
|
|
417
|
+
)
|
|
418
|
+
return { visible: visible.length, failed: failedNames.length, under, over, kept: retainedInvisible }
|
|
419
|
+
} catch (error) {
|
|
420
|
+
for (const entry of pending.reverse()) disposeEntry(entry)
|
|
421
|
+
throw error
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Convert one conservative-gate outcome into the exact external result.
|
|
427
|
+
* A partial failure is under-restriction; empty visibility never claims a
|
|
428
|
+
* deny was installed; success is the conservative mode result.
|
|
429
|
+
*/
|
|
430
|
+
const conservativeResult = (agent) => {
|
|
431
|
+
const info = applyConservativeGate(agent)
|
|
432
|
+
const common = {
|
|
433
|
+
underRestricted: info.under === true,
|
|
434
|
+
overRestricted: info.over === true,
|
|
435
|
+
conservative: info.visible > 0,
|
|
436
|
+
noVisibleTools: info.visible === 0,
|
|
437
|
+
}
|
|
438
|
+
if (info.under) {
|
|
439
|
+
return {
|
|
440
|
+
ok: false,
|
|
441
|
+
code: 'resource-pool/activation-incomplete',
|
|
442
|
+
message: 'state unavailable; conservative MCP deny is incomplete and a failed tool may be callable',
|
|
443
|
+
...common,
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (info.visible === 0) {
|
|
447
|
+
return {
|
|
448
|
+
ok: false,
|
|
449
|
+
code: 'resource-pool/activation-degraded',
|
|
450
|
+
message: info.kept
|
|
451
|
+
? 'state unavailable; no MCP tools visible, previous layers kept, no new deny installed'
|
|
452
|
+
: 'state unavailable; no MCP tools visible, no deny could be installed',
|
|
453
|
+
...common,
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
ok: false,
|
|
458
|
+
code: 'resource-pool/activation-conservative',
|
|
459
|
+
message: 'state unavailable; conservative MCP deny applied to visible tools',
|
|
460
|
+
...common,
|
|
461
|
+
}
|
|
191
462
|
}
|
|
192
463
|
|
|
193
464
|
/** Build one Agent's registrations. Runs serialized per Agent. */
|
|
194
465
|
const applyNow = async (agent, record, { force }) => {
|
|
466
|
+
// This read is fail-closed: a corrupt/unreadable/unsupported document throws
|
|
467
|
+
// instead of resolving to an empty pool. It happens before any live
|
|
468
|
+
// registration is touched, so an already-gated Agent keeps its old layers.
|
|
195
469
|
const state = await getState()
|
|
196
470
|
const settings = state.settings ?? {}
|
|
197
471
|
const mode = modeOf(ctx, agent)
|
|
198
472
|
const cwd = workspaceOf(agent)
|
|
199
473
|
const pool = resolvePool(state, { mode, cwd })
|
|
474
|
+
// Only servers this Agent may use enter the summary.
|
|
475
|
+
const servers = pool.gateMcp ? [...pool.mcpServers] : []
|
|
200
476
|
|
|
201
477
|
// The pool owns every library skill, so an Agent sees a skill only when the
|
|
202
478
|
// pool admits it here. A disabled master switch means "do not gate at all",
|
|
@@ -211,69 +487,219 @@ export function createActivation({ ctx, getState, logger, promptLanguage = () =>
|
|
|
211
487
|
|
|
212
488
|
// ---- Compute the whole next activation before touching the live one.
|
|
213
489
|
const allowedSkills = new Set(allowed)
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
490
|
+
let denyMcp = []
|
|
491
|
+
let observedMcp = 0
|
|
492
|
+
const visibleMcpNames = []
|
|
493
|
+
if (pool.gateMcp) {
|
|
494
|
+
// A missing/unreadable global tool surface is not "nothing to hide": it
|
|
495
|
+
// means the MCP gate cannot be established. Treat it as an activation
|
|
496
|
+
// failure instead of publishing an Agent without its deny layer.
|
|
497
|
+
if (tools === undefined || typeof tools.schemas !== 'function') {
|
|
498
|
+
throw new Error('MCP gate is required but the global tools service is unavailable')
|
|
499
|
+
}
|
|
500
|
+
let schemas
|
|
217
501
|
try {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
502
|
+
schemas = tools.schemas()
|
|
503
|
+
} catch (error) {
|
|
504
|
+
throw new Error('MCP gate is required but the global tool list could not be read', { cause: error })
|
|
505
|
+
}
|
|
506
|
+
if (!Array.isArray(schemas)) throw new Error('MCP gate is required but the global tool list is not an array')
|
|
507
|
+
const allowedServers = new Set(pool.mcpServers)
|
|
508
|
+
let ambiguous = 0
|
|
509
|
+
for (const schema of schemas) {
|
|
510
|
+
const name = schema?.name
|
|
511
|
+
if (typeof name !== 'string' || !name.startsWith(MCP_TOOL_PREFIX)) continue
|
|
512
|
+
observedMcp += 1
|
|
513
|
+
visibleMcpNames.push(name)
|
|
514
|
+
if (isAmbiguousMcpToolName(name)) {
|
|
515
|
+
// The public name cannot be attributed to one server; deny rather than
|
|
516
|
+
// allow by a prefix that an external server could share.
|
|
517
|
+
ambiguous += 1
|
|
518
|
+
denyMcp.push(name)
|
|
519
|
+
continue
|
|
222
520
|
}
|
|
223
|
-
|
|
224
|
-
|
|
521
|
+
if (!allowedServers.has(mcpServerOf(name))) denyMcp.push(name)
|
|
522
|
+
}
|
|
523
|
+
if (ambiguous > 0) {
|
|
524
|
+
logger?.warn?.(`[resource-pool] denied ${ambiguous} ambiguous MCP tool name${ambiguous === 1 ? '' : 's'} to avoid cross-server prefix allow`)
|
|
225
525
|
}
|
|
226
526
|
}
|
|
227
527
|
|
|
228
|
-
// ---- Apply
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
528
|
+
// ---- Apply transactionally: install the new MCP deny while the old one is
|
|
529
|
+
// still active (restrictions intersect, so this can only tighten), release
|
|
530
|
+
// only the non-critical old effects, and release the old deny last. A
|
|
531
|
+
// failed critical registration leaves the previous gate untouched.
|
|
532
|
+
const previous = record.disposers
|
|
533
|
+
const pending = []
|
|
534
|
+
let incomplete = false
|
|
535
|
+
const addEffect = (register, label, { critical = false, deny } = {}) => {
|
|
232
536
|
try {
|
|
233
|
-
|
|
537
|
+
const dispose = agent.ctx.effect(register, `resource-pool.${label}`)
|
|
538
|
+
pending.push({
|
|
539
|
+
dispose: typeof dispose === 'function' ? dispose : () => {},
|
|
540
|
+
label,
|
|
541
|
+
...(Array.isArray(deny) ? { deny: [...deny] } : {}),
|
|
542
|
+
})
|
|
543
|
+
return true
|
|
234
544
|
} catch (error) {
|
|
545
|
+
if (critical) throw error
|
|
546
|
+
incomplete = true
|
|
235
547
|
logger?.debug?.(`[resource-pool] ${label} registration failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
548
|
+
return false
|
|
236
549
|
}
|
|
237
550
|
}
|
|
238
551
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
552
|
+
let retainedInvisible = false
|
|
553
|
+
let underRestricted = false
|
|
554
|
+
let overRestricted = false
|
|
555
|
+
try {
|
|
556
|
+
// Separate old restriction layers before registering anything.
|
|
557
|
+
const oldRestrictions = []
|
|
558
|
+
for (const entry of previous) {
|
|
559
|
+
if (RESTRICT_LABELS.has(entry.label)) oldRestrictions.push(entry)
|
|
560
|
+
else disposeEntry(entry)
|
|
561
|
+
}
|
|
562
|
+
record.disposers = oldRestrictions
|
|
563
|
+
|
|
564
|
+
const visibleSet = new Set(visibleMcpNames)
|
|
565
|
+
// Old layers that cover an invisible name will be retained; their visible
|
|
566
|
+
// denied names are already covered, so do not stack duplicates.
|
|
567
|
+
const retainedCoverage = new Set(
|
|
568
|
+
oldRestrictions
|
|
569
|
+
.filter((entry) => {
|
|
570
|
+
const names = Array.isArray(entry.deny) ? entry.deny : []
|
|
571
|
+
return names.some((name) => !visibleSet.has(name))
|
|
572
|
+
})
|
|
573
|
+
.flatMap((entry) => (Array.isArray(entry.deny) ? entry.deny : [])),
|
|
574
|
+
)
|
|
575
|
+
const failedNames = []
|
|
576
|
+
const newEntries = []
|
|
577
|
+
let restrictFailures = 0
|
|
578
|
+
if (pool.gateMcp && observedMcp > 0) {
|
|
579
|
+
for (const name of denyMcp) {
|
|
580
|
+
if (retainedCoverage.has(name)) continue
|
|
581
|
+
try {
|
|
582
|
+
const dispose = agent.ctx.effect(
|
|
583
|
+
() => agent.ctx.tools.restrict({ deny: [name] }),
|
|
584
|
+
'resource-pool.mcp-restrict',
|
|
585
|
+
)
|
|
586
|
+
const entry = {
|
|
587
|
+
dispose: typeof dispose === 'function' ? dispose : () => {},
|
|
588
|
+
label: 'mcp-restrict',
|
|
589
|
+
deny: [name],
|
|
590
|
+
}
|
|
591
|
+
pending.push(entry)
|
|
592
|
+
newEntries.push(entry)
|
|
593
|
+
} catch (error) {
|
|
594
|
+
restrictFailures += 1
|
|
595
|
+
failedNames.push(name)
|
|
596
|
+
incomplete = true
|
|
597
|
+
logger?.warn?.(
|
|
598
|
+
`[resource-pool] one MCP restrict layer could not be registered (retaining old layers): ${error instanceof Error ? error.message : String(error)}`,
|
|
599
|
+
)
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Skill providers and prompt sections are keyed by name, so the old
|
|
605
|
+
// registration of that kind must be gone before the new one is inserted.
|
|
606
|
+
// They are not a rights-widening boundary: if the new one fails, the Agent
|
|
607
|
+
// has no pool skills / summary, which fails closed for this Agent.
|
|
608
|
+
if (allowedSkills.size > 0) {
|
|
609
|
+
if (skills === undefined || typeof skills.registerProvider !== 'function') {
|
|
610
|
+
logger?.warn?.('[resource-pool] skills registry unavailable; library skills cannot be served')
|
|
611
|
+
} else {
|
|
612
|
+
const root = libraryRoot()
|
|
613
|
+
addEffect(
|
|
614
|
+
() => skills.registerProvider((control) => createLibraryProvider({ root, allowed: allowedSkills, logger, control })),
|
|
615
|
+
'skill-provider',
|
|
616
|
+
)
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if (!pool.gateMcp) {
|
|
621
|
+
// Explicit no-gate intent: release every layer, including names that
|
|
622
|
+
// are not currently visible.
|
|
623
|
+
for (const entry of oldRestrictions) disposeEntry(entry)
|
|
624
|
+
record.disposers = pending
|
|
625
|
+
} else if (observedMcp === 0) {
|
|
626
|
+
// No MCP tool is visible: we cannot name anything new, so keep every old
|
|
627
|
+
// per-tool layer (transient host generation change).
|
|
628
|
+
record.disposers = [...pending, ...oldRestrictions]
|
|
629
|
+
retainedInvisible = oldRestrictions.length > 0
|
|
630
|
+
overRestricted = oldRestrictions.some((entry) =>
|
|
631
|
+
(Array.isArray(entry.deny) ? entry.deny : []).some((name) => !visibleSet.has(name)),
|
|
632
|
+
)
|
|
633
|
+
} else if (restrictFailures > 0) {
|
|
634
|
+
// A brand-new Agent has no prior authorization to fall back on: partial
|
|
635
|
+
// MCP coverage must fail creation.
|
|
636
|
+
if (!record.activated) throw new Error('MCP restrict registration failed for a new Agent')
|
|
637
|
+
const oldCoverage = new Set(
|
|
638
|
+
oldRestrictions.flatMap((entry) => (Array.isArray(entry.deny) ? entry.deny : [])),
|
|
639
|
+
)
|
|
640
|
+
// Drop a successful new layer if an old retained layer already covers
|
|
641
|
+
// the same name, so repeated failures cannot grow the active set.
|
|
642
|
+
for (const entry of newEntries) {
|
|
643
|
+
const name = entry.deny?.[0]
|
|
644
|
+
if (!oldCoverage.has(name)) continue
|
|
645
|
+
disposeEntry(entry)
|
|
646
|
+
const index = pending.indexOf(entry)
|
|
647
|
+
if (index >= 0) pending.splice(index, 1)
|
|
648
|
+
}
|
|
649
|
+
retainedInvisible = oldRestrictions.length > 0
|
|
650
|
+
underRestricted = failedNames.some((name) => !oldCoverage.has(name))
|
|
651
|
+
const deniedSet = new Set(denyMcp)
|
|
652
|
+
overRestricted = oldRestrictions.some((entry) =>
|
|
653
|
+
(Array.isArray(entry.deny) ? entry.deny : []).some(
|
|
654
|
+
(name) => !visibleSet.has(name) || !deniedSet.has(name),
|
|
655
|
+
),
|
|
656
|
+
)
|
|
657
|
+
record.disposers = [...pending, ...oldRestrictions]
|
|
242
658
|
} else {
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
659
|
+
const retained = []
|
|
660
|
+
for (const entry of oldRestrictions) {
|
|
661
|
+
const names = Array.isArray(entry.deny) ? entry.deny : []
|
|
662
|
+
// A name not visible now cannot be re-registered; keep the old layer
|
|
663
|
+
// that covers it. If it also covered a currently visible allowed name,
|
|
664
|
+
// the old layer must stay too (it cannot be split), so the result is
|
|
665
|
+
// reported as degraded rather than claimed fully applied.
|
|
666
|
+
if (names.some((name) => !visibleSet.has(name))) {
|
|
667
|
+
retained.push(entry)
|
|
668
|
+
retainedInvisible = true
|
|
669
|
+
} else {
|
|
670
|
+
disposeEntry(entry)
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
record.disposers = [...pending, ...retained]
|
|
674
|
+
const deniedSet = new Set(denyMcp)
|
|
675
|
+
overRestricted = retained.some((entry) =>
|
|
676
|
+
(Array.isArray(entry.deny) ? entry.deny : []).some(
|
|
677
|
+
(name) => !visibleSet.has(name) || !deniedSet.has(name),
|
|
678
|
+
),
|
|
248
679
|
)
|
|
249
680
|
}
|
|
250
|
-
}
|
|
251
681
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}),
|
|
272
|
-
'summary',
|
|
273
|
-
)
|
|
682
|
+
record.activated = true
|
|
683
|
+
record.mcpGateActive = pool.gateMcp
|
|
684
|
+
// Any retained layer covering a currently invisible name means the last
|
|
685
|
+
// safe activation could not be reconstructed exactly. Keep conservative
|
|
686
|
+
// semantics so a later state failure refreshes coverage for new tools.
|
|
687
|
+
// An under-restriction activation has no complete prior gate, so a later
|
|
688
|
+
// state failure must attempt a conservative refresh even if some new
|
|
689
|
+
// per-tool layers succeeded and there were no old layers.
|
|
690
|
+
record.conservative = retainedInvisible || underRestricted
|
|
691
|
+
record.degraded = overRestricted
|
|
692
|
+
record.underRestricted = underRestricted
|
|
693
|
+
record.signature = incomplete || retainedInvisible ? '' : signature
|
|
694
|
+
} catch (error) {
|
|
695
|
+
// No successful commit: remove anything this attempt registered and leave
|
|
696
|
+
// the previous gate (still in `record.disposers`) in force. Clear the
|
|
697
|
+
// signature so a later ordinary re-apply retries the failed layer set.
|
|
698
|
+
for (const entry of pending.reverse()) disposeEntry(entry)
|
|
699
|
+
record.signature = ''
|
|
700
|
+
throw error
|
|
274
701
|
}
|
|
275
702
|
|
|
276
|
-
record.signature = signature
|
|
277
703
|
logger?.debug?.(
|
|
278
704
|
`[resource-pool] activated mode=${mode} cwd=${cwd} skills=${allowedSkills.size}/${libraryNames.length} mcpHidden=${denyMcp.length}`,
|
|
279
705
|
)
|
|
@@ -283,7 +709,50 @@ export function createActivation({ ctx, getState, logger, promptLanguage = () =>
|
|
|
283
709
|
const reapplyAll = async () => {
|
|
284
710
|
const registry = ctx.get('agents')
|
|
285
711
|
const live = typeof registry?.list === 'function' ? registry.list() : []
|
|
286
|
-
|
|
712
|
+
// A failure for one Agent must not stop the others, and it must not be
|
|
713
|
+
// reported as a successful application for that Agent. Messages are fixed
|
|
714
|
+
// so callers can show a warning without echoing tool names or state bytes.
|
|
715
|
+
const results = await Promise.all(
|
|
716
|
+
live.map(async (agent) => {
|
|
717
|
+
try {
|
|
718
|
+
return await applyToAgent(agent, { force: true, conservative: true })
|
|
719
|
+
} catch (error) {
|
|
720
|
+
logger?.warn?.(`[resource-pool] re-apply failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
721
|
+
return { ok: false, code: 'resource-pool/activation-failed', message: 'activation failed' }
|
|
722
|
+
}
|
|
723
|
+
}),
|
|
724
|
+
)
|
|
725
|
+
const failed = results.filter((result) => result !== undefined && result.ok === false)
|
|
726
|
+
const count = (key) => results.filter((result) => result?.[key] === true).length
|
|
727
|
+
const under = count('underRestricted')
|
|
728
|
+
const over = count('overRestricted')
|
|
729
|
+
const conservative = count('conservative')
|
|
730
|
+
const noVisible = count('noVisibleTools')
|
|
731
|
+
const retainedOldGate = count('retainedOldGate')
|
|
732
|
+
const fallbackFailed = count('fallbackFailed')
|
|
733
|
+
const degraded = over
|
|
734
|
+
const seen = new Set()
|
|
735
|
+
const warnings = []
|
|
736
|
+
for (const result of results) {
|
|
737
|
+
if (result === undefined || typeof result.code !== 'string' || typeof result.message !== 'string') continue
|
|
738
|
+
if (result.ok === true && result.degraded !== true) continue
|
|
739
|
+
const key = `${result.code}|${result.message}`
|
|
740
|
+
if (seen.has(key)) continue
|
|
741
|
+
seen.add(key)
|
|
742
|
+
warnings.push({ code: result.code, message: result.message })
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
total: live.length,
|
|
746
|
+
failed: failed.length,
|
|
747
|
+
degraded,
|
|
748
|
+
under,
|
|
749
|
+
over,
|
|
750
|
+
conservative,
|
|
751
|
+
noVisible,
|
|
752
|
+
retainedOldGate,
|
|
753
|
+
fallbackFailed,
|
|
754
|
+
warnings,
|
|
755
|
+
}
|
|
287
756
|
}
|
|
288
757
|
|
|
289
758
|
/** Release one Agent's registrations. */
|