dsh-project-mcp 0.3.1 → 0.4.1
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 +38 -23
- package/lib/client.js +15 -15
- package/lib/client.js.map +1 -1
- package/lib/index.d.ts +181 -27
- package/lib/index.js +273 -61
- package/lib/index.js.map +1 -1
- package/package.json +18 -17
- package/src/activation.ts +164 -43
- package/src/bridge.ts +114 -7
- package/src/client/locales/ui.ts +6 -6
- package/src/client/plugin-config.ts +9 -9
- package/src/runtime.ts +77 -19
package/lib/index.js
CHANGED
|
@@ -21,17 +21,20 @@ const DEFAULT_TOOL_POLICY = {
|
|
|
21
21
|
* every tool the project owns. This module keeps the *presentation* list small
|
|
22
22
|
* without touching the registry:
|
|
23
23
|
*
|
|
24
|
-
* - the usage counters seed a **baseline** of tools the project
|
|
25
|
-
* ({@link seedFromUsage}) — they are offered directly from
|
|
24
|
+
* - the usage counters seed a **baseline** of tools the project has called — one
|
|
25
|
+
* call is enough ({@link seedFromUsage}) — and they are offered directly from
|
|
26
|
+
* the first step on;
|
|
26
27
|
* - the newest task text ranks the project's mounted tools with BM25, and the
|
|
27
28
|
* best matches are offered through a **sticky window** that holds one offered
|
|
28
|
-
* set across
|
|
29
|
+
* set across the session's steps ({@link advanceAutoOffer});
|
|
29
30
|
* - the rest are discovered through the `mcp_search_tools` tool this module
|
|
30
31
|
* registers in the agent scope, which **activates** the matches for the next
|
|
31
32
|
* step ({@link activate});
|
|
32
|
-
* - a
|
|
33
|
-
*
|
|
34
|
-
* ({@link onCompaction});
|
|
33
|
+
* - a compaction ends the session's activations that no call backs, while the
|
|
34
|
+
* ones the model really called and the counter baseline stay
|
|
35
|
+
* ({@link onCompaction}); idle pruning ({@link pruneIdle}) stays available
|
|
36
|
+
* through a positive `toolIdleMs`, and the shipped `0` leaves it off, so the
|
|
37
|
+
* compaction boundary is the one place that shrinks the offered set;
|
|
35
38
|
* - the agent's own `system-prompt/assemble` listener puts the active schemas
|
|
36
39
|
* back into the assembled request ({@link withActiveTools}).
|
|
37
40
|
*
|
|
@@ -50,12 +53,28 @@ const DEFAULT_TOOL_POLICY = {
|
|
|
50
53
|
*/
|
|
51
54
|
/** `true` while the session may offer MCP tools through search + activation. */
|
|
52
55
|
const DEFAULT_ACTIVATION_ENABLED = true;
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
56
|
+
/**
|
|
57
|
+
* Tools seeded into the baseline from the durable counters. A project with a
|
|
58
|
+
* few dozen mounted tools calls more than a handful of them, so the cap sits
|
|
59
|
+
* above the narrow window that used to evict a tool the project really uses.
|
|
60
|
+
*/
|
|
61
|
+
const DEFAULT_ACTIVATION_SEEDED = 16;
|
|
62
|
+
/**
|
|
63
|
+
* Calls one tool needs in the counters before it is seeded. One call is enough:
|
|
64
|
+
* a tool the project actually uses enters the durable baseline the first time it
|
|
65
|
+
* is called, so the baseline grows from real usage instead of waiting for the
|
|
66
|
+
* counters to reach an arbitrary threshold.
|
|
67
|
+
*/
|
|
68
|
+
const DEFAULT_ACTIVATION_MIN_CALLS = 1;
|
|
69
|
+
/**
|
|
70
|
+
* How long a session-activated tool survives without a call (`0` disables).
|
|
71
|
+
*
|
|
72
|
+
* The shipped `0` keeps a searched tool offered for the rest of the session:
|
|
73
|
+
* dropping it mid-session shrinks the request and rewrites the prompt prefix,
|
|
74
|
+
* which invalidates the provider's prefix cache, so time-based removal stays an
|
|
75
|
+
* explicit opt-in.
|
|
76
|
+
*/
|
|
77
|
+
const DEFAULT_TOOL_IDLE_MS = 0;
|
|
59
78
|
/** Matches one `mcp_search_tools` call activates when it omits `limit`. */
|
|
60
79
|
const DEFAULT_SEARCH_LIMIT = 8;
|
|
61
80
|
/** Hard cap on matches one `mcp_search_tools` call may activate. */
|
|
@@ -70,6 +89,8 @@ const MAX_SEARCH_TOKENS = 8;
|
|
|
70
89
|
const SEARCH_TOOL_NAME = "mcp_search_tools";
|
|
71
90
|
/** Empty foreign set: what the containment reads when nothing is wired or foreign. */
|
|
72
91
|
const NO_FOREIGN = /* @__PURE__ */ new Set();
|
|
92
|
+
/** Hard cap on the sticky window, however the config asks for more. */
|
|
93
|
+
const MAX_AUTO_ACTIVATION_STICKY_STEPS = 1e3;
|
|
73
94
|
/**
|
|
74
95
|
* Serialized size of the MCP surface a session may offer directly before this
|
|
75
96
|
* module defers anything, in characters. Roughly four characters per token, so
|
|
@@ -95,12 +116,13 @@ const MAX_AUTO_QUERY_CHARS = 2e3;
|
|
|
95
116
|
/**
|
|
96
117
|
* Build one session's state.
|
|
97
118
|
* @param baseline - counter-seeded public names; defaults to none.
|
|
98
|
-
* @returns an empty active set over that baseline.
|
|
119
|
+
* @returns an empty active set and an empty used record over that baseline.
|
|
99
120
|
*/
|
|
100
121
|
function createActivationState(baseline = []) {
|
|
101
122
|
return {
|
|
102
123
|
baseline: new Set(baseline),
|
|
103
|
-
active: /* @__PURE__ */ new Map()
|
|
124
|
+
active: /* @__PURE__ */ new Map(),
|
|
125
|
+
used: /* @__PURE__ */ new Set()
|
|
104
126
|
};
|
|
105
127
|
}
|
|
106
128
|
/**
|
|
@@ -153,13 +175,21 @@ function activate(state, names, at) {
|
|
|
153
175
|
}
|
|
154
176
|
return active === void 0 ? state : {
|
|
155
177
|
baseline: state.baseline,
|
|
156
|
-
active
|
|
178
|
+
active,
|
|
179
|
+
used: state.used
|
|
157
180
|
};
|
|
158
181
|
}
|
|
159
182
|
/**
|
|
160
|
-
*
|
|
161
|
-
* keeps
|
|
162
|
-
*
|
|
183
|
+
* Record a call — the freshness of a session-activated tool and the evidence a
|
|
184
|
+
* compaction keeps it by.
|
|
185
|
+
*
|
|
186
|
+
* `active` is refreshed exactly as before: a call is what keeps a
|
|
187
|
+
* session-activated tool offered. `used` records the call itself, but only for
|
|
188
|
+
* this plugin's own namespace: the core tools (`read`, `bash`, …) ride the same
|
|
189
|
+
* `tools/result` hook, and they are no part of a session's MCP activation state.
|
|
190
|
+
* A baseline name is not recorded either — the durable baseline already keeps it
|
|
191
|
+
* offered, so recording the call would only make a compaction look like it
|
|
192
|
+
* changed something.
|
|
163
193
|
*
|
|
164
194
|
* @param state - activation state before the call.
|
|
165
195
|
* @param name - public name of the called tool.
|
|
@@ -168,12 +198,18 @@ function activate(state, names, at) {
|
|
|
168
198
|
*/
|
|
169
199
|
function noteUse(state, name, at) {
|
|
170
200
|
const previous = state.active.get(name);
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
201
|
+
const refresh = previous !== void 0 && previous !== at;
|
|
202
|
+
const already = state.used?.has(name) === true;
|
|
203
|
+
const record = name.startsWith("mcp__") && !state.baseline.has(name) && !already;
|
|
204
|
+
if (!refresh && !record) return state;
|
|
205
|
+
const active = refresh ? new Map(state.active) : void 0;
|
|
206
|
+
active?.set(name, at);
|
|
207
|
+
const used = record ? new Set(state.used ?? []) : void 0;
|
|
208
|
+
used?.add(name);
|
|
174
209
|
return {
|
|
175
210
|
baseline: state.baseline,
|
|
176
|
-
active
|
|
211
|
+
active: active ?? state.active,
|
|
212
|
+
used: used ?? state.used ?? /* @__PURE__ */ new Set()
|
|
177
213
|
};
|
|
178
214
|
}
|
|
179
215
|
/**
|
|
@@ -181,6 +217,13 @@ function noteUse(state, name, at) {
|
|
|
181
217
|
* The baseline is never pruned: it comes from the durable counters, not from the
|
|
182
218
|
* session. `idleMs <= 0` disables pruning.
|
|
183
219
|
*
|
|
220
|
+
* A positive `idleMs` is an explicit opt-in to *forgetting*: an evicted name
|
|
221
|
+
* leaves the used record with the offer it was about, so the sweep is final —
|
|
222
|
+
* a later compaction cannot read the call back and resurrect what time already
|
|
223
|
+
* dropped. A name the model needs again is activated again through search. The
|
|
224
|
+
* names still inside the window keep their record, and a sweep that evicts
|
|
225
|
+
* nothing returns its input.
|
|
226
|
+
*
|
|
184
227
|
* @param state - activation state at `now`.
|
|
185
228
|
* @param now - epoch milliseconds of the sweep.
|
|
186
229
|
* @param idleMs - age at which a tool falls out.
|
|
@@ -189,27 +232,63 @@ function noteUse(state, name, at) {
|
|
|
189
232
|
function pruneIdle(state, now, idleMs) {
|
|
190
233
|
if (idleMs <= 0 || state.active.size === 0) return state;
|
|
191
234
|
let active;
|
|
235
|
+
let used;
|
|
192
236
|
for (const [name, at] of state.active) {
|
|
193
237
|
if (now - at < idleMs) continue;
|
|
194
238
|
active ??= new Map(state.active);
|
|
195
239
|
active.delete(name);
|
|
240
|
+
if (state.used?.has(name) !== true) continue;
|
|
241
|
+
used ??= new Set(state.used ?? []);
|
|
242
|
+
used.delete(name);
|
|
196
243
|
}
|
|
197
244
|
return active === void 0 ? state : {
|
|
198
245
|
baseline: state.baseline,
|
|
199
|
-
active
|
|
246
|
+
active,
|
|
247
|
+
used: used ?? state.used ?? /* @__PURE__ */ new Set()
|
|
200
248
|
};
|
|
201
249
|
}
|
|
202
250
|
/**
|
|
203
|
-
* End the session's activations
|
|
204
|
-
*
|
|
251
|
+
* End the session's activations that no call backs, at the compaction boundary.
|
|
252
|
+
*
|
|
253
|
+
* A compaction rewrites the conversation, so the provider's prompt prefix is
|
|
254
|
+
* recomputed anyway: this is the one boundary where dropping a tool costs no
|
|
255
|
+
* cache, which is why idle time is off by default and why this is the only place
|
|
256
|
+
* the offered set shrinks. Only the part the session never used goes — an offer
|
|
257
|
+
* from a context match or a search that the model did not act on. A tool this
|
|
258
|
+
* session did call is one it really uses, so it stays active, stamped with its
|
|
259
|
+
* last touch; evicting it would charge a re-discovery step for a tool the
|
|
260
|
+
* session demonstrably needs. A used name that was never activated falls back to
|
|
261
|
+
* the compaction's own clock. The counter baseline is never pruned.
|
|
262
|
+
*
|
|
263
|
+
* The used record is cumulative for the session and deliberately never bounded:
|
|
264
|
+
* with the shipped `DEFAULT_TOOL_IDLE_MS` of `0` nothing else removes a name, so
|
|
265
|
+
* a tool the session used stays offered for the rest of the session, across every
|
|
266
|
+
* further compaction. This boundary shrinks the offers no call backs — it never
|
|
267
|
+
* shrinks one the session used again.
|
|
268
|
+
*
|
|
205
269
|
* @param state - activation state before the compaction.
|
|
206
|
-
* @
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
270
|
+
* @param at - epoch milliseconds of the compaction.
|
|
271
|
+
* @returns the updated state, or the input when nothing was active or used, or
|
|
272
|
+
* when the rebuild is equivalent to what is already there — a caller compares
|
|
273
|
+
* by identity to tell a real change from a boundary that changed nothing.
|
|
274
|
+
*/
|
|
275
|
+
function onCompaction(state, at) {
|
|
276
|
+
const used = state.used ?? /* @__PURE__ */ new Set();
|
|
277
|
+
if (state.active.size === 0 && used.size === 0) return state;
|
|
278
|
+
const active = /* @__PURE__ */ new Map();
|
|
279
|
+
for (const name of used) active.set(name, state.active.get(name) ?? at);
|
|
280
|
+
if (active.size === state.active.size) {
|
|
281
|
+
let equivalent = true;
|
|
282
|
+
for (const [name, stamp] of active) if (state.active.get(name) !== stamp) {
|
|
283
|
+
equivalent = false;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
if (equivalent) return state;
|
|
287
|
+
}
|
|
210
288
|
return {
|
|
211
289
|
baseline: state.baseline,
|
|
212
|
-
active
|
|
290
|
+
active,
|
|
291
|
+
used: state.used ?? /* @__PURE__ */ new Set()
|
|
213
292
|
};
|
|
214
293
|
}
|
|
215
294
|
/**
|
|
@@ -567,7 +646,18 @@ function autoOffers(state) {
|
|
|
567
646
|
entries.sort((left, right) => left.advance - right.advance || compareNames$1(left.name, right.name));
|
|
568
647
|
return entries.map((entry) => entry.name);
|
|
569
648
|
}
|
|
570
|
-
/**
|
|
649
|
+
/**
|
|
650
|
+
* Whether two context-driven states offer the same names.
|
|
651
|
+
*
|
|
652
|
+
* Only the presented names are compared, never the advance stamp and never the
|
|
653
|
+
* per-name window stamps, so an advance that changes nothing hands back the
|
|
654
|
+
* previous state without ticking the advance counter: the sticky window ages by
|
|
655
|
+
* changes of the offered set, not by input count. This was reviewed as a
|
|
656
|
+
* possible stickiness off-by-one and cannot produce a wrong offered set — a
|
|
657
|
+
* stall can only hold names the ranking has just chosen again, and it ends at
|
|
658
|
+
* the first advance whose set differs — so do not re-chase an age here before
|
|
659
|
+
* reading `advanceAutoOffer`.
|
|
660
|
+
*/
|
|
571
661
|
function sameAutoOffer(left, right) {
|
|
572
662
|
const leftNames = autoPresentedNames(left);
|
|
573
663
|
const rightNames = autoPresentedNames(right);
|
|
@@ -589,7 +679,7 @@ function autoOfferLimit(value) {
|
|
|
589
679
|
* @returns the effective extra advances; `0` keeps only the current advance.
|
|
590
680
|
*/
|
|
591
681
|
function autoOfferStickySteps(value) {
|
|
592
|
-
return Math.min(Math.max(value ??
|
|
682
|
+
return Math.min(Math.max(value ?? 100, 0), MAX_AUTO_ACTIVATION_STICKY_STEPS);
|
|
593
683
|
}
|
|
594
684
|
/**
|
|
595
685
|
* Derive what one session offers the model right now from its live state.
|
|
@@ -903,7 +993,7 @@ function installActivation(options) {
|
|
|
903
993
|
const observe = (_session, event) => {
|
|
904
994
|
try {
|
|
905
995
|
if (event?.type === "compaction/end") {
|
|
906
|
-
options.setState(onCompaction(options.state()));
|
|
996
|
+
options.setState(onCompaction(options.state(), Date.now()));
|
|
907
997
|
options.onCompaction?.();
|
|
908
998
|
tracker.state = resetAutoOffer(tracker.state);
|
|
909
999
|
tracker.calls = [];
|
|
@@ -2459,7 +2549,8 @@ function forwardProjectTools(options) {
|
|
|
2459
2549
|
* which the live first copy refuses as a duplicate. A bridge that steps aside
|
|
2460
2550
|
* for a duplicate (it must, for the session's own tools) then drops those names
|
|
2461
2551
|
* silently, and a full pass records `namesIn` as the whole set, so the loss is
|
|
2462
|
-
* frozen for every later pass. That is the defect this class exists to
|
|
2552
|
+
* frozen for every later pass. That is the first defect this class exists to
|
|
2553
|
+
* close.
|
|
2463
2554
|
*
|
|
2464
2555
|
* So: a pass requested from inside a pass only marks the batch dirty, and the
|
|
2465
2556
|
* frame that owns the batch runs one more pass when it is complete — which is
|
|
@@ -2467,6 +2558,23 @@ function forwardProjectTools(options) {
|
|
|
2467
2558
|
* the old batch is released **before** the new one is registered, because one
|
|
2468
2559
|
* layer holds one entry per name: registering over a live batch would refuse
|
|
2469
2560
|
* every name it already had.
|
|
2561
|
+
*
|
|
2562
|
+
* Owning the re-entry is not enough on its own, and the shape of the event is
|
|
2563
|
+
* why. Replacing a batch of N entries announces 2·N changes — one per disposal
|
|
2564
|
+
* and one per registration — and the event carries no origin, so every session
|
|
2565
|
+
* watching the stream runs a pass for every one of them, and every pass begins
|
|
2566
|
+
* by projecting the project's whole catalog, which the registry answers by deep
|
|
2567
|
+
* cloning each visible definition's JSON schema. The registry per pass is what
|
|
2568
|
+
* makes that cost real: one MCP generation swap was measured holding the event
|
|
2569
|
+
* loop for the full 54 seconds of a CPU profile (47% of its samples inside this
|
|
2570
|
+
* sync, 52% in the collector the projections feed), while the plugin's own
|
|
2571
|
+
* frames were 0.46% of it. So {@link note} is the seat a `tools/change`
|
|
2572
|
+
* subscription takes, not {@link sync}: a change that arrives during a pass is
|
|
2573
|
+
* absorbed by it, and a burst that arrives between passes costs **one** queued
|
|
2574
|
+
* pass however many events it carries. A stale forwarder is safe in the window
|
|
2575
|
+
* that opens — it resolves the project entry at call time and fails closed when
|
|
2576
|
+
* there is none (`forwarder`) — and every read path brings the batch level
|
|
2577
|
+
* before it reads, so a reader never waits for the queue.
|
|
2470
2578
|
*/
|
|
2471
2579
|
var ForwardBatch = class {
|
|
2472
2580
|
options;
|
|
@@ -2478,6 +2586,8 @@ var ForwardBatch = class {
|
|
|
2478
2586
|
#building = false;
|
|
2479
2587
|
/** Whether a pass asked for while {@link #building} still needs a run. */
|
|
2480
2588
|
#again = false;
|
|
2589
|
+
/** Whether a scheduled pass is already waiting for a burst of changes. */
|
|
2590
|
+
#queued = false;
|
|
2481
2591
|
constructor(options) {
|
|
2482
2592
|
this.options = options;
|
|
2483
2593
|
}
|
|
@@ -2492,11 +2602,12 @@ var ForwardBatch = class {
|
|
|
2492
2602
|
return this.#namesIn;
|
|
2493
2603
|
}
|
|
2494
2604
|
/**
|
|
2495
|
-
* Bring the session's layer level with the project's live tool set.
|
|
2605
|
+
* Bring the session's layer level with the project's live tool set, now.
|
|
2496
2606
|
*
|
|
2497
2607
|
* Cheap when it already is: an unchanged set short-circuits and leaves the
|
|
2498
2608
|
* live batch — and its generation of definitions — untouched. Re-entrant
|
|
2499
|
-
* while registering
|
|
2609
|
+
* while registering, and bounded to {@link MAX_SYNC_PASSES} synchronous
|
|
2610
|
+
* rounds so no registry state can hold the caller's frame: see the class note.
|
|
2500
2611
|
* @returns nothing; a failure from the probe or the registry propagates.
|
|
2501
2612
|
*/
|
|
2502
2613
|
sync() {
|
|
@@ -2504,17 +2615,42 @@ var ForwardBatch = class {
|
|
|
2504
2615
|
this.#again = true;
|
|
2505
2616
|
return;
|
|
2506
2617
|
}
|
|
2618
|
+
this.#queued = false;
|
|
2507
2619
|
this.#building = true;
|
|
2508
2620
|
try {
|
|
2509
|
-
|
|
2621
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
2510
2622
|
this.#again = false;
|
|
2511
2623
|
this.#syncOnce();
|
|
2512
|
-
|
|
2624
|
+
if (!this.#again) return;
|
|
2625
|
+
}
|
|
2626
|
+
this.#schedule(true);
|
|
2513
2627
|
} finally {
|
|
2514
2628
|
this.#building = false;
|
|
2515
2629
|
}
|
|
2516
2630
|
}
|
|
2517
2631
|
/**
|
|
2632
|
+
* Note that the registry changed, and bring the batch level — off this stack.
|
|
2633
|
+
*
|
|
2634
|
+
* This is the seat a `tools/change` subscription takes, and the reason it is
|
|
2635
|
+
* not {@link sync}: the event is emitted once per entry registered and once
|
|
2636
|
+
* per entry disposed, from inside the registry's own insertion and removal,
|
|
2637
|
+
* so one MCP generation swap is a burst of thousands. A change that arrives
|
|
2638
|
+
* while a pass is running is absorbed by that pass — it re-reads the set it is
|
|
2639
|
+
* replacing before it returns — and a burst that arrives between passes costs
|
|
2640
|
+
* one pass: the first queues, every later one folds into it.
|
|
2641
|
+
*
|
|
2642
|
+
* Nothing reads a stale layer for it. Every read path brings the batch level
|
|
2643
|
+
* first (`src/runtime.ts`, `mountedSchemas`), and the queued pass has run
|
|
2644
|
+
* before any turn that could read the layer at all.
|
|
2645
|
+
*/
|
|
2646
|
+
note() {
|
|
2647
|
+
if (this.#building) {
|
|
2648
|
+
this.#again = true;
|
|
2649
|
+
return;
|
|
2650
|
+
}
|
|
2651
|
+
this.#schedule(false);
|
|
2652
|
+
}
|
|
2653
|
+
/**
|
|
2518
2654
|
* Drop the live batch: the session keeps no forwarder, and the next
|
|
2519
2655
|
* {@link sync} starts from nothing rather than amending a stale set.
|
|
2520
2656
|
* Idempotent.
|
|
@@ -2523,16 +2659,45 @@ var ForwardBatch = class {
|
|
|
2523
2659
|
* session is subscribed to that event; a pass re-entered from there must not
|
|
2524
2660
|
* resurrect the batch it is watching being dropped. So the guard is held for
|
|
2525
2661
|
* the whole release, and the pass it queues is discarded rather than run.
|
|
2662
|
+
*
|
|
2663
|
+
* The guard is *held*, not taken: a drop that runs inside a pass — a detach
|
|
2664
|
+
* driven from a listener, a scope torn down under a registration — restores
|
|
2665
|
+
* the pass's own `building` rather than clearing it. Clearing it there would
|
|
2666
|
+
* let the next `tools/change` start a nested pass inside a frame that still
|
|
2667
|
+
* owns one, which is exactly the re-entry this class exists to own.
|
|
2526
2668
|
*/
|
|
2527
2669
|
drop() {
|
|
2670
|
+
const building = this.#building;
|
|
2528
2671
|
this.#building = true;
|
|
2529
2672
|
try {
|
|
2530
2673
|
this.#releaseBatch();
|
|
2531
2674
|
} finally {
|
|
2532
|
-
this.#building =
|
|
2675
|
+
this.#building = building;
|
|
2533
2676
|
this.#again = false;
|
|
2677
|
+
this.#queued = false;
|
|
2534
2678
|
}
|
|
2535
2679
|
}
|
|
2680
|
+
/**
|
|
2681
|
+
* Queue the one deferred pass a burst of changes needs.
|
|
2682
|
+
* @param yielding - run it on the timer queue rather than the microtask queue.
|
|
2683
|
+
* A pass that overran {@link MAX_SYNC_PASSES} asks for this: a chain of
|
|
2684
|
+
* microtasks still starves timers and I/O, a round of timers does not.
|
|
2685
|
+
*/
|
|
2686
|
+
#schedule(yielding) {
|
|
2687
|
+
if (this.#queued) return;
|
|
2688
|
+
this.#queued = true;
|
|
2689
|
+
const run = () => {
|
|
2690
|
+
if (!this.#queued) return;
|
|
2691
|
+
this.#queued = false;
|
|
2692
|
+
try {
|
|
2693
|
+
this.sync();
|
|
2694
|
+
} catch (error) {
|
|
2695
|
+
this.options.onError?.(error);
|
|
2696
|
+
}
|
|
2697
|
+
};
|
|
2698
|
+
if (yielding) setTimeout(run, 0);
|
|
2699
|
+
else queueMicrotask(run);
|
|
2700
|
+
}
|
|
2536
2701
|
/** One pass: read the project's set, then replace the batch when it moved. */
|
|
2537
2702
|
#syncOnce() {
|
|
2538
2703
|
const names = this.options.names();
|
|
@@ -5334,23 +5499,30 @@ var ProjectMcpRuntime = class {
|
|
|
5334
5499
|
const bridge = {
|
|
5335
5500
|
batch: new ForwardBatch({
|
|
5336
5501
|
names: () => {
|
|
5502
|
+
const project = state.project;
|
|
5503
|
+
if (project === void 0) return void 0;
|
|
5337
5504
|
try {
|
|
5338
|
-
return projectToolNames(
|
|
5505
|
+
return projectToolNames(project.key, (scope) => this.bridgeSchemas(scope), servers());
|
|
5339
5506
|
} catch (error) {
|
|
5340
5507
|
this.ctx.logger.warn(`project-mcp: reading the tools to forward to session ${state.agent.id} failed: ${errorText(error)}`);
|
|
5341
5508
|
return;
|
|
5342
5509
|
}
|
|
5343
5510
|
},
|
|
5344
|
-
register: (names) =>
|
|
5345
|
-
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5511
|
+
register: (names) => {
|
|
5512
|
+
const project = state.project;
|
|
5513
|
+
if (project === void 0) return () => void 0;
|
|
5514
|
+
return forwardProjectTools({
|
|
5515
|
+
ctx: context,
|
|
5516
|
+
project: project.key,
|
|
5517
|
+
names,
|
|
5518
|
+
registry
|
|
5519
|
+
});
|
|
5520
|
+
},
|
|
5521
|
+
onError: (error) => this.reportBridgeFailure(state, error)
|
|
5350
5522
|
}),
|
|
5351
5523
|
detach: () => void 0
|
|
5352
5524
|
};
|
|
5353
|
-
const detach = state.scope.ctx.on("tools/change", () => this.
|
|
5525
|
+
const detach = state.scope.ctx.on("tools/change", () => this.noteBridge(state));
|
|
5354
5526
|
bridge.detach = typeof detach === "function" ? detach : () => void 0;
|
|
5355
5527
|
state.bridge = bridge;
|
|
5356
5528
|
this.syncBridge(state);
|
|
@@ -5371,9 +5543,37 @@ var ProjectMcpRuntime = class {
|
|
|
5371
5543
|
try {
|
|
5372
5544
|
bridge.batch.sync();
|
|
5373
5545
|
} catch (error) {
|
|
5374
|
-
this.
|
|
5546
|
+
this.reportBridgeFailure(state, error);
|
|
5375
5547
|
}
|
|
5376
5548
|
}
|
|
5549
|
+
/**
|
|
5550
|
+
* Note one registry change for one session's bridge, off the emitter's stack.
|
|
5551
|
+
*
|
|
5552
|
+
* The event this answers is emitted synchronously from inside the registry's
|
|
5553
|
+
* own insertion and removal — once per entry in, once per entry out — so one
|
|
5554
|
+
* MCP generation swap is a burst of thousands. Running the pass here would
|
|
5555
|
+
* make whoever registered a tool pay for every session watching the project,
|
|
5556
|
+
* and each pass projects the project's whole catalog. The batch folds the
|
|
5557
|
+
* burst into one queued pass instead, which is also why this is not
|
|
5558
|
+
* {@link syncBridge}: the read paths, the mount settle and the scope wiring
|
|
5559
|
+
* all bring the batch level synchronously, before anything reads the layer.
|
|
5560
|
+
* @param state - the session whose bridge noticed the change.
|
|
5561
|
+
*/
|
|
5562
|
+
noteBridge(state) {
|
|
5563
|
+
state.bridge?.batch.note();
|
|
5564
|
+
}
|
|
5565
|
+
/**
|
|
5566
|
+
* Write the one warning line a failed bridge pass is allowed.
|
|
5567
|
+
*
|
|
5568
|
+
* Shared by the synchronous pass and by the one a batch runs on its own: a
|
|
5569
|
+
* scheduled pass has no caller left to propagate its failure to, so the batch
|
|
5570
|
+
* hands it here through `onError` rather than swallowing it.
|
|
5571
|
+
* @param state - the session whose bridge failed.
|
|
5572
|
+
* @param error - what the pass threw.
|
|
5573
|
+
*/
|
|
5574
|
+
reportBridgeFailure(state, error) {
|
|
5575
|
+
this.ctx.logger.warn(`project-mcp: forwarding the tools of ${state.project?.projectRoot ?? "the project"} into session ${state.agent.id} failed: ${errorText(error)}`);
|
|
5576
|
+
}
|
|
5377
5577
|
/** The project scope's visible schemas, as {@link projectToolNames} reads them. */
|
|
5378
5578
|
bridgeSchemas(scope) {
|
|
5379
5579
|
return this.bridgeRegistry().schemas(scope);
|
|
@@ -5505,7 +5705,7 @@ var ProjectMcpRuntime = class {
|
|
|
5505
5705
|
for (const name of names) state.steps.set(name, step);
|
|
5506
5706
|
},
|
|
5507
5707
|
onCompaction: () => {
|
|
5508
|
-
|
|
5708
|
+
this.pruneStepRecords(state);
|
|
5509
5709
|
},
|
|
5510
5710
|
autoState: state.auto ?? createAutoOfferState(),
|
|
5511
5711
|
setAutoState: (next) => {
|
|
@@ -5532,8 +5732,8 @@ var ProjectMcpRuntime = class {
|
|
|
5532
5732
|
if (existing !== void 0) return existing;
|
|
5533
5733
|
const projectRoot = state.projectRoot;
|
|
5534
5734
|
const seeded = createActivationState(seedFromUsage(projectRoot === void 0 ? void 0 : this.options.usage?.forProject(projectRoot), {
|
|
5535
|
-
count: this.config.activationSeeded ??
|
|
5536
|
-
minCalls: this.config.activationMinCalls ??
|
|
5735
|
+
count: this.config.activationSeeded ?? 16,
|
|
5736
|
+
minCalls: this.config.activationMinCalls ?? 1
|
|
5537
5737
|
}));
|
|
5538
5738
|
state.activation = seeded;
|
|
5539
5739
|
return seeded;
|
|
@@ -5698,8 +5898,10 @@ var ProjectMcpRuntime = class {
|
|
|
5698
5898
|
}
|
|
5699
5899
|
}
|
|
5700
5900
|
/**
|
|
5701
|
-
*
|
|
5702
|
-
*
|
|
5901
|
+
* Record a call against the session's activation state: it refreshes the
|
|
5902
|
+
* freshness of a session-activated tool, and it is the evidence a later
|
|
5903
|
+
* compaction keeps that activation by. It rides the same `tools/result`
|
|
5904
|
+
* subscription the counters use, so no second observer exists.
|
|
5703
5905
|
* @param agentId - calling agent, when the call was agent-scoped.
|
|
5704
5906
|
* @param toolName - public registry name of the call.
|
|
5705
5907
|
*/
|
|
@@ -5713,10 +5915,20 @@ var ProjectMcpRuntime = class {
|
|
|
5713
5915
|
pruneActivation(state) {
|
|
5714
5916
|
const current = state.activation;
|
|
5715
5917
|
if (current === void 0) return;
|
|
5716
|
-
state.activation = pruneIdle(current, Date.now(), this.config.toolIdleMs ??
|
|
5717
|
-
if (state.activation !== current)
|
|
5718
|
-
|
|
5719
|
-
|
|
5918
|
+
state.activation = pruneIdle(current, Date.now(), this.config.toolIdleMs ?? 0);
|
|
5919
|
+
if (state.activation !== current) this.pruneStepRecords(state);
|
|
5920
|
+
}
|
|
5921
|
+
/**
|
|
5922
|
+
* Drop the step records whose activation the session no longer holds.
|
|
5923
|
+
*
|
|
5924
|
+
* Called wherever activations end — the idle sweep and a compaction — so a
|
|
5925
|
+
* record never outlives the offer it was made for, and a name the session
|
|
5926
|
+
* still offers keeps the step its offer happened on.
|
|
5927
|
+
* @param state - the session whose records are reconciled.
|
|
5928
|
+
*/
|
|
5929
|
+
pruneStepRecords(state) {
|
|
5930
|
+
const active = state.activation?.active;
|
|
5931
|
+
for (const name of [...state.steps.keys()]) if (active === void 0 || !active.has(name)) state.steps.delete(name);
|
|
5720
5932
|
}
|
|
5721
5933
|
async unmount(home, name, reason) {
|
|
5722
5934
|
const mount = home.mounts.get(name);
|
|
@@ -7230,12 +7442,12 @@ const DEFAULTS = {
|
|
|
7230
7442
|
debounceMs: 300,
|
|
7231
7443
|
rescanIntervalMs: 1e4,
|
|
7232
7444
|
activationEnabled: true,
|
|
7233
|
-
activationSeeded:
|
|
7234
|
-
activationMinCalls:
|
|
7235
|
-
toolIdleMs:
|
|
7445
|
+
activationSeeded: 16,
|
|
7446
|
+
activationMinCalls: 1,
|
|
7447
|
+
toolIdleMs: 0,
|
|
7236
7448
|
guidanceEnabled: true,
|
|
7237
7449
|
activationAutoLimit: 12,
|
|
7238
|
-
activationAutoStickySteps:
|
|
7450
|
+
activationAutoStickySteps: 100,
|
|
7239
7451
|
activationToolBudgetChars: DEFAULT_TOOL_BUDGET_CHARS,
|
|
7240
7452
|
allowGlobalWrite: false
|
|
7241
7453
|
};
|