@linxin666/dsh-liangshen 0.4.18 → 0.4.20
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.i18n.yaml +2 -2
- package/README.md +11 -7
- package/README.zh.md +11 -7
- package/lib/client.js +118 -12
- package/lib/client.js.map +1 -1
- package/lib/index.js +46 -13
- package/lib/types/client/LiangShenSettingsCard.d.ts +3 -0
- package/lib/types/client/lever-controller.d.ts +26 -2
- package/lib/types/client/locales.d.ts +3 -0
- package/lib/types/client/settings-form.d.ts +2 -0
- package/lib/types/composition.d.ts +6 -0
- package/lib/types/index.d.ts +14 -1
- package/package.json +15 -15
- package/presets/liangshen/agent.cordis.yml +16 -3
- package/presets/liangshen/dispatcher-rules.mjs +87 -62
- package/presets/liangshen/fact-ledger.mjs +41 -25
- package/presets/liangshen/minimal-prompt.mjs +72 -25
- package/presets/liangshen/paging.mjs +269 -26
- package/presets/liangshen/subagent-routes.mjs +11 -22
- package/presets/liangshen/tool-activate.mjs +21 -13
- package/presets/liangshen/tool-catalog.mjs +162 -101
- package/presets/liangshen/wire-presentation.mjs +33 -0
- package/presets/liangshen/working-context.mjs +94 -96
- package/src/announce.test.ts +58 -0
- package/src/client/LiangShenSettingsCard.tsx +17 -1
- package/src/client/lever-controller.ts +57 -15
- package/src/client/locales.ts +6 -0
- package/src/client/settings-form.ts +21 -2
- package/src/composition.test.ts +33 -0
- package/src/composition.ts +22 -3
- package/src/index.test.ts +1 -0
- package/src/index.ts +37 -5
|
@@ -16,6 +16,25 @@
|
|
|
16
16
|
* Least-recently-used eviction keeps at most MAX active namespaces: activating
|
|
17
17
|
* beyond the cap evicts the namespace whose latest activation or use is the
|
|
18
18
|
* oldest, returning its tools to the paged summary.
|
|
19
|
+
*
|
|
20
|
+
* Deferred idle reclaim (optional, `idleReclaimSteps`, default 0 = off): a
|
|
21
|
+
* namespace with no successful activation or use for N steps is expired, but
|
|
22
|
+
* the fold removes it only at a moment the request's prompt prefix changes
|
|
23
|
+
* anyway — the next activation touch, or a `compaction/end` boundary. Until
|
|
24
|
+
* then it stays resident and reachable, and any successful call into it
|
|
25
|
+
* refreshes its recency and cancels the expiry. The deferral is the cache
|
|
26
|
+
* contract (issue #2066): the applied active set, and therefore the withheld
|
|
27
|
+
* names and the `tools:sdk` section rendered from them, changes only where it
|
|
28
|
+
* already changes without reclaim, plus compaction boundaries whose history
|
|
29
|
+
* rewrite already cold-starts the prefix cache — never one extra rebuild.
|
|
30
|
+
*
|
|
31
|
+
* SHARED DURABLE-LOG HELPERS: the same replay reads one session log in every
|
|
32
|
+
* projection row of this preset, so the readers live here once — the tolerant
|
|
33
|
+
* event source ({@link sessionEvents}), the message text and visibility rules
|
|
34
|
+
* ({@link textOf}, {@link latestVisiblePluginMessage}), the step-message
|
|
35
|
+
* reconciliation every durable plugin message goes through
|
|
36
|
+
* ({@link reconcileStepMessage}), and the failed-call index both the activation
|
|
37
|
+
* replay and the working-context fold collect ({@link erroredCallId}).
|
|
19
38
|
*/
|
|
20
39
|
|
|
21
40
|
/**
|
|
@@ -30,6 +49,9 @@ export const DEFAULT_PAGED_TOOL_PATTERNS = ['mcp__*', 'browser_*', 'task_board_*
|
|
|
30
49
|
/** The LRU cap on simultaneously active paged namespaces. */
|
|
31
50
|
export const DEFAULT_MAX_ACTIVE_NAMESPACES = 3
|
|
32
51
|
|
|
52
|
+
/** Idle-step threshold of the deferred reclaim; 0 keeps every activation resident (the shipped behavior). */
|
|
53
|
+
export const DEFAULT_IDLE_RECLAIM_STEPS = 0
|
|
54
|
+
|
|
33
55
|
/** The activation tool this preset registers. */
|
|
34
56
|
export const TOOL_ACTIVATE_NAME = 'tool_activate'
|
|
35
57
|
|
|
@@ -187,33 +209,97 @@ export function resultIsError(data) {
|
|
|
187
209
|
}
|
|
188
210
|
|
|
189
211
|
/**
|
|
190
|
-
*
|
|
212
|
+
* The callId one event reports as failed, or undefined when it reports no
|
|
213
|
+
* failure at all.
|
|
191
214
|
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
215
|
+
* The activation replay skips a failed call because it never took effect, and
|
|
216
|
+
* the working-context projection collects the same index inside the single pass
|
|
217
|
+
* it is already making over the log. One definition of "this call failed" keeps
|
|
218
|
+
* a failure from meaning two different things in the two folds.
|
|
219
|
+
* @param event - one durable session event.
|
|
220
|
+
* @returns the failed call's id as a string, or undefined.
|
|
198
221
|
*/
|
|
199
|
-
export function
|
|
200
|
-
|
|
222
|
+
export function erroredCallId(event) {
|
|
223
|
+
if (event?.type !== 'tool/result' || !resultIsError(event.data)) return undefined
|
|
224
|
+
const callId = event.data?.message?.source?.callId ?? event.data?.callId
|
|
225
|
+
return callId === undefined ? undefined : String(callId)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** The failed callIds of one log, as the activation replay reads them. */
|
|
229
|
+
function failedCallIdsOf(events) {
|
|
201
230
|
const errored = new Set()
|
|
202
|
-
for (const event of
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (callId !== undefined && resultIsError(event.data)) errored.add(String(callId))
|
|
231
|
+
for (const event of events) {
|
|
232
|
+
const callId = erroredCallId(event)
|
|
233
|
+
if (callId !== undefined) errored.add(callId)
|
|
206
234
|
}
|
|
235
|
+
return errored
|
|
236
|
+
}
|
|
207
237
|
|
|
238
|
+
/**
|
|
239
|
+
* The paging fold: one state machine both the replay and the activation
|
|
240
|
+
* projection run, so the page of the next assembly and the report of this
|
|
241
|
+
* activation can never disagree.
|
|
242
|
+
*
|
|
243
|
+
* With `idleReclaimSteps` 0 (the shipped default) the fold is exactly the
|
|
244
|
+
* historical LRU replay: activations and successful uses touch the recency
|
|
245
|
+
* list, and only the capacity evicts. With a positive threshold the fold also
|
|
246
|
+
* records the step of the latest successful activation or use of each
|
|
247
|
+
* namespace (`step/start` events advance the counter) and removes expired
|
|
248
|
+
* namespaces at the two application points — an activation touch and
|
|
249
|
+
* `compaction/end` — never at the idle detection itself, an ordinary use, a
|
|
250
|
+
* turn boundary, or a resume. A log without `step/start` events never
|
|
251
|
+
* reclaims: no step evidence, no expiry.
|
|
252
|
+
*/
|
|
253
|
+
function pagingFold(capacity, patterns, idleReclaimSteps, errored) {
|
|
208
254
|
const active = []
|
|
209
255
|
const evicted = []
|
|
210
|
-
const
|
|
256
|
+
const reclaimed = []
|
|
257
|
+
const lastUse = new Map()
|
|
258
|
+
let step = 0
|
|
259
|
+
|
|
260
|
+
const reclaimExpired = () => {
|
|
261
|
+
if (idleReclaimSteps <= 0) return
|
|
262
|
+
for (let index = 0; index < active.length;) {
|
|
263
|
+
const namespace = active[index]
|
|
264
|
+
const used = lastUse.get(namespace)
|
|
265
|
+
if (used !== undefined && step - used >= idleReclaimSteps) {
|
|
266
|
+
reclaimed.push(namespace)
|
|
267
|
+
active.splice(index, 1)
|
|
268
|
+
} else {
|
|
269
|
+
index += 1
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const reorder = (namespace) => {
|
|
211
274
|
const index = active.indexOf(namespace)
|
|
212
275
|
if (index >= 0) active.splice(index, 1)
|
|
213
276
|
active.push(namespace)
|
|
277
|
+
}
|
|
278
|
+
const activate = (namespace) => {
|
|
279
|
+
// The activation refreshes its own recency first, so reclaiming the OTHER
|
|
280
|
+
// expired namespaces can never remove the one being activated, and the
|
|
281
|
+
// capacity reclaim frees may spare an LRU eviction.
|
|
282
|
+
lastUse.set(namespace, step)
|
|
283
|
+
reclaimExpired()
|
|
284
|
+
reorder(namespace)
|
|
214
285
|
while (active.length > capacity) evicted.push(active.shift())
|
|
215
286
|
}
|
|
216
|
-
|
|
287
|
+
const use = (namespace) => {
|
|
288
|
+
// A use only refreshes recency. Applying reclaim here would move the
|
|
289
|
+
// prompt prefix on an ordinary call — the extra rebuild the deferral
|
|
290
|
+
// exists to prevent.
|
|
291
|
+
lastUse.set(namespace, step)
|
|
292
|
+
reorder(namespace)
|
|
293
|
+
}
|
|
294
|
+
const applyEvent = (event) => {
|
|
295
|
+
if (event?.type === 'step/start') {
|
|
296
|
+
step += 1
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
if (event?.type === 'compaction/end') {
|
|
300
|
+
reclaimExpired()
|
|
301
|
+
return
|
|
302
|
+
}
|
|
217
303
|
// Under the `ptc` presentation the wire carries `run_code` alone, so the model
|
|
218
304
|
// reaches EVERY tool from inside a program and the harness records each call as
|
|
219
305
|
// a `tool/ptc-dispatch` event — never as a top-level `tool/call`. Reading only
|
|
@@ -227,28 +313,185 @@ export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPAC
|
|
|
227
313
|
// takes under the default presentation refreshed nothing.
|
|
228
314
|
if (event?.type === 'tool/ptc-dispatch') {
|
|
229
315
|
const dispatch = event.data
|
|
230
|
-
if (dispatch?.isError === true)
|
|
316
|
+
if (dispatch?.isError === true) return
|
|
231
317
|
if (dispatch?.name === TOOL_ACTIVATE_NAME) {
|
|
232
318
|
const namespace = callArguments(dispatch)?.namespace
|
|
233
|
-
if (typeof namespace === 'string' && namespace !== '')
|
|
234
|
-
|
|
319
|
+
if (typeof namespace === 'string' && namespace !== '') activate(namespace)
|
|
320
|
+
return
|
|
235
321
|
}
|
|
236
322
|
const namespace = namespaceOf(dispatch?.name, patterns)
|
|
237
|
-
if (namespace !== undefined && active.includes(namespace))
|
|
238
|
-
|
|
323
|
+
if (namespace !== undefined && active.includes(namespace)) use(namespace)
|
|
324
|
+
return
|
|
239
325
|
}
|
|
240
|
-
if (event?.type !== 'tool/call')
|
|
326
|
+
if (event?.type !== 'tool/call') return
|
|
241
327
|
const data = event.data
|
|
242
|
-
if (data?.callId !== undefined && errored.has(String(data.callId)))
|
|
328
|
+
if (data?.callId !== undefined && errored.has(String(data.callId))) return
|
|
243
329
|
if (data?.name === TOOL_ACTIVATE_NAME) {
|
|
244
330
|
const namespace = callArguments(data)?.namespace
|
|
245
|
-
if (typeof namespace === 'string' && namespace !== '')
|
|
246
|
-
|
|
331
|
+
if (typeof namespace === 'string' && namespace !== '') activate(namespace)
|
|
332
|
+
return
|
|
247
333
|
}
|
|
248
334
|
const namespace = namespaceOf(data?.name, patterns)
|
|
249
|
-
if (namespace !== undefined && active.includes(namespace))
|
|
335
|
+
if (namespace !== undefined && active.includes(namespace)) use(namespace)
|
|
336
|
+
}
|
|
337
|
+
return { active, evicted, reclaimed, activate, applyEvent }
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Replay the durable session event stream into the active namespace set.
|
|
342
|
+
*
|
|
343
|
+
* Events are the source of truth: a `tool_activate` call whose result did not
|
|
344
|
+
* error activates its namespace (or refreshes its recency), and any other
|
|
345
|
+
* successful call into a paged tool refreshes the namespace it belongs to.
|
|
346
|
+
* Activating beyond the capacity evicts the least recently used namespace.
|
|
347
|
+
* Returns the active namespaces oldest-first and every evicted namespace in
|
|
348
|
+
* eviction order.
|
|
349
|
+
*
|
|
350
|
+
* @param events - the durable log to replay.
|
|
351
|
+
* @param capacity - how many namespaces stay active at once.
|
|
352
|
+
* @param patterns - the paging patterns that name a tool's namespace.
|
|
353
|
+
* @param failedCallIds - the failed callIds when the caller already folded them
|
|
354
|
+
* ({@link erroredCallId}); omitted, this replay collects them itself.
|
|
355
|
+
* @param idleReclaimSteps - idle steps after which a namespace expires; 0 (the
|
|
356
|
+
* default) keeps the historical resident-until-evicted behavior. Expiry is
|
|
357
|
+
* applied only at activation touches and `compaction/end` boundaries.
|
|
358
|
+
*/
|
|
359
|
+
export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPACES, patterns = DEFAULT_PAGED_TOOL_PATTERNS, failedCallIds, idleReclaimSteps = DEFAULT_IDLE_RECLAIM_STEPS) {
|
|
360
|
+
const list = Array.isArray(events) ? events : []
|
|
361
|
+
const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
|
|
362
|
+
const fold = pagingFold(capacity, patterns, idleReclaimSteps, errored)
|
|
363
|
+
for (const event of list) fold.applyEvent(event)
|
|
364
|
+
return { active: fold.active, evicted: fold.evicted }
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Project one activation about to be recorded: fold the log as it stands, then
|
|
369
|
+
* apply the activation transition the next replay applies once the call lands
|
|
370
|
+
* in the stream. The activation tool reports from this projection, so its
|
|
371
|
+
* eviction/reclaim report and the page of the next assembly are one fact.
|
|
372
|
+
*
|
|
373
|
+
* @param events - the durable log, excluding the call being projected.
|
|
374
|
+
* @param capacity - how many namespaces stay active at once.
|
|
375
|
+
* @param patterns - the paging patterns that name a tool's namespace.
|
|
376
|
+
* @param namespace - the namespace this activation targets.
|
|
377
|
+
* @param idleReclaimSteps - the deferred-reclaim threshold, as the replay reads it.
|
|
378
|
+
* @param failedCallIds - the failed callIds when the caller already folded them.
|
|
379
|
+
* @returns wasActive (the target already resident before this call), the
|
|
380
|
+
* post-activation active set oldest-first, and the namespaces THIS activation
|
|
381
|
+
* evicted (LRU) or reclaimed (applied expiry), in removal order.
|
|
382
|
+
*/
|
|
383
|
+
export function projectActivation(events, capacity, patterns, namespace, idleReclaimSteps = DEFAULT_IDLE_RECLAIM_STEPS, failedCallIds) {
|
|
384
|
+
const list = Array.isArray(events) ? events : []
|
|
385
|
+
const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
|
|
386
|
+
const fold = pagingFold(capacity, patterns, idleReclaimSteps, errored)
|
|
387
|
+
for (const event of list) fold.applyEvent(event)
|
|
388
|
+
const wasActive = fold.active.includes(namespace)
|
|
389
|
+
const evictedBefore = fold.evicted.length
|
|
390
|
+
const reclaimedBefore = fold.reclaimed.length
|
|
391
|
+
fold.activate(namespace)
|
|
392
|
+
return {
|
|
393
|
+
wasActive,
|
|
394
|
+
active: fold.active,
|
|
395
|
+
evicted: fold.evicted.slice(evictedBefore),
|
|
396
|
+
reclaimed: fold.reclaimed.slice(reclaimedBefore),
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* The durable events of one session, tolerating both shapes a host exposes and
|
|
402
|
+
* an unreadable log.
|
|
403
|
+
*
|
|
404
|
+
* An event source that throws, or a session that exposes none, contributes an
|
|
405
|
+
* empty log: every projection in this preset is a fold over what is readable,
|
|
406
|
+
* and a step that cannot read the log still has to run — a resume that hits a
|
|
407
|
+
* broken snapshot loses the derived state for one step, not the request.
|
|
408
|
+
* @param session - Session-like record.
|
|
409
|
+
* @returns the session's events, never undefined.
|
|
410
|
+
*/
|
|
411
|
+
export function sessionEvents(session) {
|
|
412
|
+
if (Array.isArray(session?.events)) return session.events
|
|
413
|
+
if (typeof session?.snapshotEvents === 'function') {
|
|
414
|
+
try {
|
|
415
|
+
const snapshot = session.snapshotEvents()
|
|
416
|
+
if (Array.isArray(snapshot)) return snapshot
|
|
417
|
+
} catch {
|
|
418
|
+
// Unreadable log: fall through to the empty one.
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return []
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** The text one message contributes, joined across its text blocks. */
|
|
425
|
+
export function textOf(message) {
|
|
426
|
+
const blocks = Array.isArray(message?.content) ? message.content : []
|
|
427
|
+
return blocks
|
|
428
|
+
.filter(block => block?.type === 'text' && typeof block.text === 'string')
|
|
429
|
+
.map(block => block.text)
|
|
430
|
+
.join('\n')
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* The latest copy of one plugin's durable message that is still on the visible
|
|
435
|
+
* surface, plus whether the log carries one at all.
|
|
436
|
+
*
|
|
437
|
+
* Two facts a republishing decision needs, and they are different facts: a
|
|
438
|
+
* message the log holds is "published" even when a compaction shadowed it, and
|
|
439
|
+
* that is exactly the case where the copy has to travel again. Visibility is a
|
|
440
|
+
* position check against the live surface, so an array of nodes answers it with
|
|
441
|
+
* one scan instead of a Set built on every step.
|
|
442
|
+
* @param agent - agent scope whose session log and surface are read.
|
|
443
|
+
* @param matcher - predicate matching one durable message record.
|
|
444
|
+
* @returns the published flag plus the visible text, absent when none is visible.
|
|
445
|
+
*/
|
|
446
|
+
export function latestVisiblePluginMessage(agent, matcher) {
|
|
447
|
+
const session = agent?.session
|
|
448
|
+
if (typeof matcher !== 'function') return { published: false }
|
|
449
|
+
const events = sessionEvents(session)
|
|
450
|
+
const nodes = session?.surface?.nodes
|
|
451
|
+
let published = false
|
|
452
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
453
|
+
const event = events[index]
|
|
454
|
+
if (event?.type !== 'user/message' || !matcher(event.data)) continue
|
|
455
|
+
published = true
|
|
456
|
+
const visible = !Array.isArray(nodes) || typeof event.seq !== 'number' || nodes.lastIndexOf(event.seq) !== -1
|
|
457
|
+
if (visible) return { published, text: textOf(event.data) }
|
|
250
458
|
}
|
|
251
|
-
return {
|
|
459
|
+
return { published }
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Reconcile one plugin's durable step message against the decision a step just
|
|
464
|
+
* built: publish it when its text changed, keep an already-current copy in the
|
|
465
|
+
* admitted batch, and drop a stale copy when the published one is still on the
|
|
466
|
+
* visible surface.
|
|
467
|
+
*
|
|
468
|
+
* Three states decide the outcome, and the order they are read in is the
|
|
469
|
+
* behavior: the text this step wants, the text the log already shows, and the
|
|
470
|
+
* copy the harness put in the batch. A message is never published twice and
|
|
471
|
+
* never left twice.
|
|
472
|
+
*
|
|
473
|
+
* @param decision - the step decision the hook received.
|
|
474
|
+
* @param history - the visible published state, as the reader above returns it.
|
|
475
|
+
* @param candidate - the text this step wants on the wire, or undefined when the
|
|
476
|
+
* step has nothing to say.
|
|
477
|
+
* @param createMessage - builds the durable message for this step; returning
|
|
478
|
+
* nothing means this step has nothing to publish.
|
|
479
|
+
* @param isMatch - predicate recognizing this plugin's message.
|
|
480
|
+
* @returns the decision to hand back, unchanged when nothing moved.
|
|
481
|
+
*/
|
|
482
|
+
export function reconcileStepMessage(decision, history, candidate, createMessage, isMatch) {
|
|
483
|
+
const messages = Array.isArray(decision?.messages) ? decision.messages : undefined
|
|
484
|
+
if (messages === undefined || typeof isMatch !== 'function') return decision
|
|
485
|
+
const existing = messages.find(isMatch)
|
|
486
|
+
const drop = () => (existing === undefined
|
|
487
|
+
? decision
|
|
488
|
+
: { ...decision, messages: messages.filter(message => message.id !== existing.id) })
|
|
489
|
+
if (candidate === undefined || history?.text === candidate) return drop()
|
|
490
|
+
if (existing !== undefined && textOf(existing) === candidate) return decision
|
|
491
|
+
const message = typeof createMessage === 'function' ? createMessage() : undefined
|
|
492
|
+
if (message === undefined || message === null) return drop()
|
|
493
|
+
if (existing === undefined) return { ...decision, messages: [...messages, message] }
|
|
494
|
+
return { ...decision, messages: messages.map(item => (item.id === existing.id ? message : item)) }
|
|
252
495
|
}
|
|
253
496
|
|
|
254
497
|
/**
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
* adds a denial it can justify from the durable log.
|
|
36
36
|
*/
|
|
37
37
|
|
|
38
|
+
// The session log is read through the preset's shared reader: an unreadable log
|
|
39
|
+
// reads as empty here exactly as it does in every other fold of this preset.
|
|
40
|
+
import { sessionEvents } from './paging.mjs'
|
|
41
|
+
|
|
38
42
|
/** Cordis plugin name used by loader diagnostics. */
|
|
39
43
|
export const name = 'liangshen-subagent-routes'
|
|
40
44
|
|
|
@@ -57,11 +61,11 @@ export const DEFAULT_EXPLICIT_TOOLS = ['subagent']
|
|
|
57
61
|
|
|
58
62
|
/**
|
|
59
63
|
* Delegation tools that start their child on the caller's own route.
|
|
60
|
-
* `subagent_fork` is the
|
|
61
|
-
*
|
|
62
|
-
*
|
|
64
|
+
* `subagent_fork` is the only one: it publishes no route fields at all, so
|
|
65
|
+
* policing it as an explicit tool would demand a pair no call could name and
|
|
66
|
+
* deny every delegation.
|
|
63
67
|
*/
|
|
64
|
-
export const DEFAULT_INHERIT_TOOLS = ['subagent_fork'
|
|
68
|
+
export const DEFAULT_INHERIT_TOOLS = ['subagent_fork']
|
|
65
69
|
|
|
66
70
|
/** How far the policy lookup follows the Session chain before giving up. */
|
|
67
71
|
export const MAX_ANCESTOR_DEPTH = 8
|
|
@@ -100,27 +104,14 @@ export function parseAllowedRoutes(data) {
|
|
|
100
104
|
return routes
|
|
101
105
|
}
|
|
102
106
|
|
|
103
|
-
/** The events of one session, tolerating both snapshot shapes. */
|
|
104
|
-
function sessionEvents(session) {
|
|
105
|
-
if (Array.isArray(session?.events)) return session.events
|
|
106
|
-
if (typeof session?.snapshotEvents === 'function') {
|
|
107
|
-
try {
|
|
108
|
-
const snapshot = session.snapshotEvents()
|
|
109
|
-
if (Array.isArray(snapshot)) return snapshot
|
|
110
|
-
} catch {
|
|
111
|
-
return undefined
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return undefined
|
|
115
|
-
}
|
|
116
|
-
|
|
117
107
|
/**
|
|
118
108
|
* The authorized routes a session recorded, if any.
|
|
119
109
|
*
|
|
120
110
|
* The policy is appended before the Session's first model request, so the scan
|
|
121
111
|
* stays short; the bound keeps a Session without one from walking its whole
|
|
122
112
|
* log. Both the durable `eventAt(seq)` reader and a plain events array are
|
|
123
|
-
* accepted, because the row may run against a Host that exposes only one.
|
|
113
|
+
* accepted, because the row may run against a Host that exposes only one. An
|
|
114
|
+
* unreadable log reads as "no policy recorded", which is the fail-open answer.
|
|
124
115
|
* @param session - Session-like record.
|
|
125
116
|
* @returns the recorded routes, or undefined when this Session recorded none.
|
|
126
117
|
*/
|
|
@@ -141,9 +132,7 @@ export function policyRoutesOf(session) {
|
|
|
141
132
|
}
|
|
142
133
|
return undefined
|
|
143
134
|
}
|
|
144
|
-
const
|
|
145
|
-
if (events === undefined) return undefined
|
|
146
|
-
for (const event of events.slice(0, MAX_POLICY_SCAN)) {
|
|
135
|
+
for (const event of sessionEvents(session).slice(0, MAX_POLICY_SCAN)) {
|
|
147
136
|
if (event?.type !== POLICY_EVENT) continue
|
|
148
137
|
const routes = parseAllowedRoutes(event.data)
|
|
149
138
|
if (routes !== undefined) return routes
|
|
@@ -23,16 +23,22 @@
|
|
|
23
23
|
* eviction is named in this tool's result so the catalog's next publication
|
|
24
24
|
* reflects it.
|
|
25
25
|
*
|
|
26
|
+
* With `idleReclaimSteps` enabled, the same projection also applies expired
|
|
27
|
+
* namespaces at this activation and names them in the result; the report comes
|
|
28
|
+
* from `projectActivation`, the same fold transition the next replay applies.
|
|
29
|
+
*
|
|
26
30
|
* Validation: the target must name a namespace that currently has paged,
|
|
27
31
|
* inactive tools — an unknown namespace fails with the list of available ones,
|
|
28
32
|
* and an already-active namespace is a no-op error instead of a silent success.
|
|
29
33
|
*/
|
|
30
34
|
|
|
31
35
|
import {
|
|
36
|
+
DEFAULT_IDLE_RECLAIM_STEPS,
|
|
32
37
|
DEFAULT_MAX_ACTIVE_NAMESPACES,
|
|
33
38
|
integerAtLeast,
|
|
34
39
|
namespaceOf,
|
|
35
|
-
|
|
40
|
+
projectActivation,
|
|
41
|
+
sessionEvents,
|
|
36
42
|
validatePagedToolPatterns,
|
|
37
43
|
} from './paging.mjs'
|
|
38
44
|
import { pagedNamespacesFor } from './tool-catalog.mjs'
|
|
@@ -43,13 +49,6 @@ export const name = 'liangshen-tool-activate'
|
|
|
43
49
|
/** The tools registry must exist before the activation tool can register. */
|
|
44
50
|
export const inject = ['tools']
|
|
45
51
|
|
|
46
|
-
/** Session events, tolerating both snapshotEvents() and events array. */
|
|
47
|
-
function sessionEvents(session) {
|
|
48
|
-
if (Array.isArray(session?.events)) return session.events
|
|
49
|
-
if (typeof session?.snapshotEvents === 'function') return session.snapshotEvents()
|
|
50
|
-
return []
|
|
51
|
-
}
|
|
52
|
-
|
|
53
52
|
/** Register the model-facing `tool_activate` tool. */
|
|
54
53
|
export function apply(ctx, config) {
|
|
55
54
|
const pagedToolPatterns = validatePagedToolPatterns(name, config?.pagedToolPatterns)
|
|
@@ -60,6 +59,13 @@ export function apply(ctx, config) {
|
|
|
60
59
|
1,
|
|
61
60
|
DEFAULT_MAX_ACTIVE_NAMESPACES,
|
|
62
61
|
)
|
|
62
|
+
const idleReclaimSteps = integerAtLeast(
|
|
63
|
+
name,
|
|
64
|
+
config?.idleReclaimSteps,
|
|
65
|
+
'idleReclaimSteps',
|
|
66
|
+
0,
|
|
67
|
+
DEFAULT_IDLE_RECLAIM_STEPS,
|
|
68
|
+
)
|
|
63
69
|
|
|
64
70
|
/**
|
|
65
71
|
* Read the session's visible tool schemas from the public API, tolerating
|
|
@@ -126,6 +132,7 @@ export function apply(ctx, config) {
|
|
|
126
132
|
description: [
|
|
127
133
|
`Load one paged tool namespace back into this session's reach — onto the wire, and the generated SDK along with it. The session catalog summarizes the paged namespaces; activating one makes its tools callable from the next request on.`,
|
|
128
134
|
`At most ${maxActiveNamespaces} namespaces stay active at once: activating beyond the cap evicts the least recently used namespace back to the catalog summary.`,
|
|
135
|
+
...(idleReclaimSteps > 0 ? [`Idle namespaces (no successful call for ${idleReclaimSteps} steps) are reclaimed at the next activation or compaction, never mid-turn; a reclaimed namespace returns to the catalog summary and can be activated again.`] : []),
|
|
129
136
|
'The target must be a paged, currently inactive namespace exactly as the catalog names it.',
|
|
130
137
|
].join('\n'),
|
|
131
138
|
parameters: {
|
|
@@ -183,21 +190,22 @@ export function apply(ctx, config) {
|
|
|
183
190
|
&& event.data?.name === 'tool_activate'
|
|
184
191
|
&& String(event.data?.callId) === String(selfCallId)
|
|
185
192
|
))
|
|
186
|
-
const
|
|
187
|
-
if (
|
|
193
|
+
const projection = projectActivation(events, maxActiveNamespaces, pagedToolPatterns, namespace, idleReclaimSteps)
|
|
194
|
+
if (projection.wasActive) {
|
|
188
195
|
throw new Error(`namespace "${namespace}" is already active; its tools are on the wire`)
|
|
189
196
|
}
|
|
190
197
|
|
|
191
|
-
const next = [...active, namespace]
|
|
192
|
-
const evicted = next.length > maxActiveNamespaces ? next.shift() : undefined
|
|
193
198
|
const tools = paged.get(namespace)
|
|
194
199
|
const lines = [
|
|
195
200
|
`Activated namespace "${namespace}" (${tools.length} tool${tools.length === 1 ? '' : 's'}): ${tools.join(', ')}.`,
|
|
196
201
|
'Its tools are on the wire from the next request; call them directly by name.',
|
|
197
202
|
]
|
|
198
|
-
|
|
203
|
+
for (const evicted of projection.evicted) {
|
|
199
204
|
lines.push(`Least recently used namespace "${evicted}" was paged out (capacity ${maxActiveNamespaces}); its summary stays in the catalog and tool_activate can reload it.`)
|
|
200
205
|
}
|
|
206
|
+
for (const reclaimed of projection.reclaimed) {
|
|
207
|
+
lines.push(`Idle namespace "${reclaimed}" was reclaimed (no successful call for ${idleReclaimSteps} steps); its summary stays in the catalog and tool_activate can reload it.`)
|
|
208
|
+
}
|
|
201
209
|
return { text: lines.join(' ') }
|
|
202
210
|
},
|
|
203
211
|
})
|