@linxin666/dsh-liangshen 0.4.18 → 0.4.19
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 +4 -4
- package/README.zh.md +4 -4
- package/lib/client.js +52 -9
- package/lib/client.js.map +1 -1
- package/lib/index.js +28 -6
- package/lib/types/client/lever-controller.d.ts +26 -2
- package/package.json +15 -15
- package/presets/liangshen/dispatcher-rules.mjs +87 -62
- package/presets/liangshen/fact-ledger.mjs +41 -25
- package/presets/liangshen/minimal-prompt.mjs +35 -16
- package/presets/liangshen/paging.mjs +140 -7
- package/presets/liangshen/subagent-routes.mjs +11 -22
- package/presets/liangshen/tool-activate.mjs +1 -7
- package/presets/liangshen/tool-catalog.mjs +141 -99
- package/presets/liangshen/working-context.mjs +81 -95
- package/src/announce.test.ts +58 -0
- package/src/client/lever-controller.ts +57 -15
- package/src/composition.test.ts +26 -0
- package/src/composition.ts +9 -1
- package/src/index.ts +22 -4
|
@@ -765,20 +765,26 @@ export function apply(ctx, config) {
|
|
|
765
765
|
}
|
|
766
766
|
sessionStateMap.set(session, state)
|
|
767
767
|
|
|
768
|
-
// Reconstruct touched directories from durable history
|
|
769
|
-
|
|
768
|
+
// Reconstruct touched directories from the durable history, but ONLY in
|
|
769
|
+
// system-prompt mode: that is the one mode that discovers subdirectory
|
|
770
|
+
// instructions itself, so the other two never pay for a log read they
|
|
771
|
+
// cannot use (the harness's own row, or the hint, owns the channel there).
|
|
772
|
+
if (instructionSource === 'system-prompt' && typeof session?.snapshotEvents === 'function') {
|
|
770
773
|
try {
|
|
771
774
|
const events = session.snapshotEvents()
|
|
772
775
|
const cwd = session.header?.cwd ?? process.cwd()
|
|
773
776
|
for (const ev of events) {
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
777
|
+
// Both wire shapes count: a top-level call, and the program dispatch
|
|
778
|
+
// a PTC session records for the same tool. A dispatch that reported
|
|
779
|
+
// an error touched nothing.
|
|
780
|
+
if (ev?.type !== 'tool/call' && ev?.type !== 'tool/ptc-dispatch') continue
|
|
781
|
+
if (!ev.data || ev.data.isError === true) continue
|
|
782
|
+
let args = ev.data.arguments
|
|
783
|
+
if (typeof args === 'string') {
|
|
784
|
+
try { args = JSON.parse(args) } catch {}
|
|
781
785
|
}
|
|
786
|
+
const p = filePathFromExecution({ name: ev.data.name, arguments: args })
|
|
787
|
+
if (p) state.touchedDirs.add(dirname(resolve(cwd, p)))
|
|
782
788
|
}
|
|
783
789
|
} catch {
|
|
784
790
|
// Replay reconstruction fallback
|
|
@@ -852,8 +858,15 @@ export function apply(ctx, config) {
|
|
|
852
858
|
}
|
|
853
859
|
}, { prepend: true })
|
|
854
860
|
|
|
855
|
-
// 2. Synchronous tool result tracking (no fake emit, no async work in emit listener)
|
|
861
|
+
// 2. Synchronous tool result tracking (no fake emit, no async work in emit listener).
|
|
862
|
+
//
|
|
863
|
+
// Tracking exists for the dynamic subdirectory discovery of system-prompt mode,
|
|
864
|
+
// so the other two modes return before touching the session at all: host mode
|
|
865
|
+
// hands the channel to the harness's own row, and hint mode never reads the log.
|
|
866
|
+
// The listener stays registered in every mode, because the hook surface is part
|
|
867
|
+
// of this preset's contract whatever a mode does with it.
|
|
856
868
|
ctx.on('tools/result', (exec, result) => {
|
|
869
|
+
if (instructionSource !== 'system-prompt') return
|
|
857
870
|
if (result?.isError || !exec?.agent || exec?.signal?.aborted) return
|
|
858
871
|
const path = filePathFromExecution(exec)
|
|
859
872
|
if (!path) return
|
|
@@ -873,6 +886,14 @@ export function apply(ctx, config) {
|
|
|
873
886
|
|
|
874
887
|
const agent = payload?.agent
|
|
875
888
|
const session = agent?.session
|
|
889
|
+
|
|
890
|
+
// Host mode: the harness's agent-instructions row owns the channel, so every
|
|
891
|
+
// message it queued — baseline and dynamic alike — passes through verbatim:
|
|
892
|
+
// no filterInstructionMessages, no dynamic subdirectory discovery, no
|
|
893
|
+
// plugin-authored message, and no per-session state to build. Handing the
|
|
894
|
+
// decision back unchanged is the point.
|
|
895
|
+
if (instructionSource === 'host') return decision
|
|
896
|
+
|
|
876
897
|
const state = session ? getSessionState(session) : undefined
|
|
877
898
|
|
|
878
899
|
if (instructionSource === 'hint') {
|
|
@@ -880,12 +901,6 @@ export function apply(ctx, config) {
|
|
|
880
901
|
return { ...decision, messages: instructionHintMessages(decision.messages, state) }
|
|
881
902
|
}
|
|
882
903
|
|
|
883
|
-
// Host mode: the harness's agent-instructions row owns the channel, so every
|
|
884
|
-
// message it queued — baseline and dynamic alike — passes through verbatim:
|
|
885
|
-
// no filterInstructionMessages, no dynamic subdirectory discovery, no
|
|
886
|
-
// plugin-authored message. Handing the decision back unchanged is the point.
|
|
887
|
-
if (instructionSource === 'host') return decision
|
|
888
|
-
|
|
889
904
|
// System-prompt mode:
|
|
890
905
|
const cwd = session?.header?.cwd
|
|
891
906
|
const baselineLoaded = state?.baselineLoaded ?? false
|
|
@@ -921,8 +936,12 @@ export function apply(ctx, config) {
|
|
|
921
936
|
}, { prepend: true })
|
|
922
937
|
|
|
923
938
|
// 4. Session lifecycle / compaction recovery
|
|
939
|
+
//
|
|
940
|
+
// Host mode writes no per-session state (the harness's own row owns the
|
|
941
|
+
// channel), so it has nothing to reset and the map is never consulted there.
|
|
924
942
|
ctx.on('session/event', (session, event) => {
|
|
925
943
|
if (event?.type === 'compaction/end') {
|
|
944
|
+
if (instructionSource === 'host') return
|
|
926
945
|
const state = sessionStateMap.get(session)
|
|
927
946
|
if (state) {
|
|
928
947
|
state.hinted = false
|
|
@@ -16,6 +16,14 @@
|
|
|
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
|
+
* SHARED DURABLE-LOG HELPERS: the same replay reads one session log in every
|
|
21
|
+
* projection row of this preset, so the readers live here once — the tolerant
|
|
22
|
+
* event source ({@link sessionEvents}), the message text and visibility rules
|
|
23
|
+
* ({@link textOf}, {@link latestVisiblePluginMessage}), the step-message
|
|
24
|
+
* reconciliation every durable plugin message goes through
|
|
25
|
+
* ({@link reconcileStepMessage}), and the failed-call index both the activation
|
|
26
|
+
* replay and the working-context fold collect ({@link erroredCallId}).
|
|
19
27
|
*/
|
|
20
28
|
|
|
21
29
|
/**
|
|
@@ -186,6 +194,33 @@ export function resultIsError(data) {
|
|
|
186
194
|
return blocks.some(block => block?.isError === true)
|
|
187
195
|
}
|
|
188
196
|
|
|
197
|
+
/**
|
|
198
|
+
* The callId one event reports as failed, or undefined when it reports no
|
|
199
|
+
* failure at all.
|
|
200
|
+
*
|
|
201
|
+
* The activation replay skips a failed call because it never took effect, and
|
|
202
|
+
* the working-context projection collects the same index inside the single pass
|
|
203
|
+
* it is already making over the log. One definition of "this call failed" keeps
|
|
204
|
+
* a failure from meaning two different things in the two folds.
|
|
205
|
+
* @param event - one durable session event.
|
|
206
|
+
* @returns the failed call's id as a string, or undefined.
|
|
207
|
+
*/
|
|
208
|
+
export function erroredCallId(event) {
|
|
209
|
+
if (event?.type !== 'tool/result' || !resultIsError(event.data)) return undefined
|
|
210
|
+
const callId = event.data?.message?.source?.callId ?? event.data?.callId
|
|
211
|
+
return callId === undefined ? undefined : String(callId)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The failed callIds of one log, as the activation replay reads them. */
|
|
215
|
+
function failedCallIdsOf(events) {
|
|
216
|
+
const errored = new Set()
|
|
217
|
+
for (const event of events) {
|
|
218
|
+
const callId = erroredCallId(event)
|
|
219
|
+
if (callId !== undefined) errored.add(callId)
|
|
220
|
+
}
|
|
221
|
+
return errored
|
|
222
|
+
}
|
|
223
|
+
|
|
189
224
|
/**
|
|
190
225
|
* Replay the durable session event stream into the active namespace set.
|
|
191
226
|
*
|
|
@@ -195,15 +230,16 @@ export function resultIsError(data) {
|
|
|
195
230
|
* Activating beyond the capacity evicts the least recently used namespace.
|
|
196
231
|
* Returns the active namespaces oldest-first and every evicted namespace in
|
|
197
232
|
* eviction order.
|
|
233
|
+
*
|
|
234
|
+
* @param events - the durable log to replay.
|
|
235
|
+
* @param capacity - how many namespaces stay active at once.
|
|
236
|
+
* @param patterns - the paging patterns that name a tool's namespace.
|
|
237
|
+
* @param failedCallIds - the failed callIds when the caller already folded them
|
|
238
|
+
* ({@link erroredCallId}); omitted, this replay collects them itself.
|
|
198
239
|
*/
|
|
199
|
-
export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPACES, patterns = DEFAULT_PAGED_TOOL_PATTERNS) {
|
|
240
|
+
export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPACES, patterns = DEFAULT_PAGED_TOOL_PATTERNS, failedCallIds) {
|
|
200
241
|
const list = Array.isArray(events) ? events : []
|
|
201
|
-
const errored =
|
|
202
|
-
for (const event of list) {
|
|
203
|
-
if (event?.type !== 'tool/result') continue
|
|
204
|
-
const callId = event.data?.message?.source?.callId ?? event.data?.callId
|
|
205
|
-
if (callId !== undefined && resultIsError(event.data)) errored.add(String(callId))
|
|
206
|
-
}
|
|
242
|
+
const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
|
|
207
243
|
|
|
208
244
|
const active = []
|
|
209
245
|
const evicted = []
|
|
@@ -251,6 +287,103 @@ export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPAC
|
|
|
251
287
|
return { active, evicted }
|
|
252
288
|
}
|
|
253
289
|
|
|
290
|
+
/**
|
|
291
|
+
* The durable events of one session, tolerating both shapes a host exposes and
|
|
292
|
+
* an unreadable log.
|
|
293
|
+
*
|
|
294
|
+
* An event source that throws, or a session that exposes none, contributes an
|
|
295
|
+
* empty log: every projection in this preset is a fold over what is readable,
|
|
296
|
+
* and a step that cannot read the log still has to run — a resume that hits a
|
|
297
|
+
* broken snapshot loses the derived state for one step, not the request.
|
|
298
|
+
* @param session - Session-like record.
|
|
299
|
+
* @returns the session's events, never undefined.
|
|
300
|
+
*/
|
|
301
|
+
export function sessionEvents(session) {
|
|
302
|
+
if (Array.isArray(session?.events)) return session.events
|
|
303
|
+
if (typeof session?.snapshotEvents === 'function') {
|
|
304
|
+
try {
|
|
305
|
+
const snapshot = session.snapshotEvents()
|
|
306
|
+
if (Array.isArray(snapshot)) return snapshot
|
|
307
|
+
} catch {
|
|
308
|
+
// Unreadable log: fall through to the empty one.
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return []
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The text one message contributes, joined across its text blocks. */
|
|
315
|
+
export function textOf(message) {
|
|
316
|
+
const blocks = Array.isArray(message?.content) ? message.content : []
|
|
317
|
+
return blocks
|
|
318
|
+
.filter(block => block?.type === 'text' && typeof block.text === 'string')
|
|
319
|
+
.map(block => block.text)
|
|
320
|
+
.join('\n')
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The latest copy of one plugin's durable message that is still on the visible
|
|
325
|
+
* surface, plus whether the log carries one at all.
|
|
326
|
+
*
|
|
327
|
+
* Two facts a republishing decision needs, and they are different facts: a
|
|
328
|
+
* message the log holds is "published" even when a compaction shadowed it, and
|
|
329
|
+
* that is exactly the case where the copy has to travel again. Visibility is a
|
|
330
|
+
* position check against the live surface, so an array of nodes answers it with
|
|
331
|
+
* one scan instead of a Set built on every step.
|
|
332
|
+
* @param agent - agent scope whose session log and surface are read.
|
|
333
|
+
* @param matcher - predicate matching one durable message record.
|
|
334
|
+
* @returns the published flag plus the visible text, absent when none is visible.
|
|
335
|
+
*/
|
|
336
|
+
export function latestVisiblePluginMessage(agent, matcher) {
|
|
337
|
+
const session = agent?.session
|
|
338
|
+
if (typeof matcher !== 'function') return { published: false }
|
|
339
|
+
const events = sessionEvents(session)
|
|
340
|
+
const nodes = session?.surface?.nodes
|
|
341
|
+
let published = false
|
|
342
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
343
|
+
const event = events[index]
|
|
344
|
+
if (event?.type !== 'user/message' || !matcher(event.data)) continue
|
|
345
|
+
published = true
|
|
346
|
+
const visible = !Array.isArray(nodes) || typeof event.seq !== 'number' || nodes.lastIndexOf(event.seq) !== -1
|
|
347
|
+
if (visible) return { published, text: textOf(event.data) }
|
|
348
|
+
}
|
|
349
|
+
return { published }
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Reconcile one plugin's durable step message against the decision a step just
|
|
354
|
+
* built: publish it when its text changed, keep an already-current copy in the
|
|
355
|
+
* admitted batch, and drop a stale copy when the published one is still on the
|
|
356
|
+
* visible surface.
|
|
357
|
+
*
|
|
358
|
+
* Three states decide the outcome, and the order they are read in is the
|
|
359
|
+
* behavior: the text this step wants, the text the log already shows, and the
|
|
360
|
+
* copy the harness put in the batch. A message is never published twice and
|
|
361
|
+
* never left twice.
|
|
362
|
+
*
|
|
363
|
+
* @param decision - the step decision the hook received.
|
|
364
|
+
* @param history - the visible published state, as the reader above returns it.
|
|
365
|
+
* @param candidate - the text this step wants on the wire, or undefined when the
|
|
366
|
+
* step has nothing to say.
|
|
367
|
+
* @param createMessage - builds the durable message for this step; returning
|
|
368
|
+
* nothing means this step has nothing to publish.
|
|
369
|
+
* @param isMatch - predicate recognizing this plugin's message.
|
|
370
|
+
* @returns the decision to hand back, unchanged when nothing moved.
|
|
371
|
+
*/
|
|
372
|
+
export function reconcileStepMessage(decision, history, candidate, createMessage, isMatch) {
|
|
373
|
+
const messages = Array.isArray(decision?.messages) ? decision.messages : undefined
|
|
374
|
+
if (messages === undefined || typeof isMatch !== 'function') return decision
|
|
375
|
+
const existing = messages.find(isMatch)
|
|
376
|
+
const drop = () => (existing === undefined
|
|
377
|
+
? decision
|
|
378
|
+
: { ...decision, messages: messages.filter(message => message.id !== existing.id) })
|
|
379
|
+
if (candidate === undefined || history?.text === candidate) return drop()
|
|
380
|
+
if (existing !== undefined && textOf(existing) === candidate) return decision
|
|
381
|
+
const message = typeof createMessage === 'function' ? createMessage() : undefined
|
|
382
|
+
if (message === undefined || message === null) return drop()
|
|
383
|
+
if (existing === undefined) return { ...decision, messages: [...messages, message] }
|
|
384
|
+
return { ...decision, messages: messages.map(item => (item.id === existing.id ? message : item)) }
|
|
385
|
+
}
|
|
386
|
+
|
|
254
387
|
/**
|
|
255
388
|
* One-line catalog summary per inactive namespace: the namespace, its tool
|
|
256
389
|
* count, a sample tool name, and the first tool's one-line description as the
|
|
@@ -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
|
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
integerAtLeast,
|
|
34
34
|
namespaceOf,
|
|
35
35
|
replayActivations,
|
|
36
|
+
sessionEvents,
|
|
36
37
|
validatePagedToolPatterns,
|
|
37
38
|
} from './paging.mjs'
|
|
38
39
|
import { pagedNamespacesFor } from './tool-catalog.mjs'
|
|
@@ -43,13 +44,6 @@ export const name = 'liangshen-tool-activate'
|
|
|
43
44
|
/** The tools registry must exist before the activation tool can register. */
|
|
44
45
|
export const inject = ['tools']
|
|
45
46
|
|
|
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
47
|
/** Register the model-facing `tool_activate` tool. */
|
|
54
48
|
export function apply(ctx, config) {
|
|
55
49
|
const pagedToolPatterns = validatePagedToolPatterns(name, config?.pagedToolPatterns)
|