@linxin666/dsh-liangshen 0.4.5 → 0.4.6
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 -6
- package/README.zh.md +11 -6
- package/lib/client.js +77 -13
- package/lib/client.js.map +1 -1
- package/lib/index.js +33 -19
- package/lib/types/client/LiangShenMark.d.ts +13 -0
- package/lib/types/client/LiangShenSettingsCard.d.ts +11 -7
- package/lib/types/client/locales.d.ts +3 -0
- package/lib/types/client/plugin-card-seat.d.ts +5 -0
- package/lib/types/composition.d.ts +15 -11
- package/lib/types/index.d.ts +44 -22
- package/package.json +2 -2
- package/presets/liangshen/agent.cordis.yml +41 -6
- package/presets/liangshen/dispatcher-rules.mjs +121 -0
- package/presets/liangshen/guard.mjs +134 -34
- package/presets/liangshen/minimal-prompt.mjs +65 -9
- package/presets/liangshen/tool-catalog.mjs +45 -12
- package/src/announce.test.ts +3 -2
- package/src/client/LiangShenMark.tsx +25 -0
- package/src/client/LiangShenSettingsCard.tsx +29 -10
- package/src/client/locales.ts +16 -10
- package/src/client/plugin-card-seat.ts +7 -0
- package/src/composition.test.ts +56 -0
- package/src/composition.ts +16 -11
- package/src/index.test.ts +56 -0
- package/src/index.ts +51 -23
|
@@ -18,24 +18,32 @@
|
|
|
18
18
|
* tool call nor any visible reply text — the #5976 shape. Reading the
|
|
19
19
|
* reasoning TEXT is unnecessary (and impossible on routes that redact it);
|
|
20
20
|
* the block's character count is the objective "long thinking" measure.
|
|
21
|
+
* On a route that persists signed reasoning blocks with NO text, every
|
|
22
|
+
* character count reads zero and the character ladders can never fire, so a
|
|
23
|
+
* session in which no reasoning text was ever observed falls back to a
|
|
24
|
+
* step-count measure (see DEFAULT_BLIND_STALL_CAP) and says so once.
|
|
21
25
|
* - ECHO: the same tool called with the same arguments failing M times in a
|
|
22
26
|
* row with no success in between — the closed loop of repeating one broken
|
|
23
27
|
* call.
|
|
24
28
|
*
|
|
25
|
-
* On either signal the breaker FIRES once per episode
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
29
|
+
* On either signal the breaker FIRES once per episode with a circuit-breaker
|
|
30
|
+
* user message injected at the next pre-step (the one channel guaranteed to
|
|
31
|
+
* reach the model, as durable as the working-context projection), telling the
|
|
32
|
+
* model the loop was interrupted and to close out or pick a materially
|
|
33
|
+
* different action.
|
|
34
|
+
*
|
|
35
|
+
* A STALL additionally arms a temporary reasoning-effort step-down that rides
|
|
36
|
+
* the `agent/request` waterfall for the next FEW requests: one notch down
|
|
37
|
+
* `max -> high`, and `high` (like every other level) is left alone. Lowering
|
|
38
|
+
* the budget is the community-observed recovery move (r/DeepSeek 1whgo3e) and
|
|
39
|
+
* the official effort-cost curve puts the sweet spot below max — neither piece
|
|
40
|
+
* of evidence supports going BELOW the 60-80 sweet spot, so the ladder stops
|
|
41
|
+
* at `high`. ECHO does NOT step down: its cause is a broken call, not an
|
|
42
|
+
* over-large budget, and lowering the effort would weaken a model that is
|
|
43
|
+
* already stuck. This is the ONLY moment this plugin rewrites a request: with
|
|
44
|
+
* no signal it never touches one, so prefix-cache stability and the user's
|
|
45
|
+
* explicit model-selector effort stay untouched — the constraint recorded in
|
|
46
|
+
* the Agent Note that removed the old phase-based switching.
|
|
39
47
|
*
|
|
40
48
|
* Recovery: a step with a tool call or a visible reply re-arms the breaker
|
|
41
49
|
* and, once the step-down window has run its requests, the route's own effort
|
|
@@ -95,6 +103,14 @@ export const GLOBAL_MIN_REASONING_CHARS = 200
|
|
|
95
103
|
/** How many consecutive output-free reasoning steps trip the slow-burn ladder. */
|
|
96
104
|
export const DEFAULT_GLOBAL_STALL_CAP = 4
|
|
97
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Consecutive output-free steps that trip STALL on a route that never produced
|
|
108
|
+
* any reasoning text. Both character ladders read zero there, so the step count
|
|
109
|
+
* is the only objective measure left; the cap matches the slow-burn ladder's,
|
|
110
|
+
* which keeps the fallback as conservative as the ladder it stands in for.
|
|
111
|
+
*/
|
|
112
|
+
export const DEFAULT_BLIND_STALL_CAP = 4
|
|
113
|
+
|
|
98
114
|
/**
|
|
99
115
|
* Sensitivity presets: a whole-scale multiplier over the adaptive thresholds,
|
|
100
116
|
* for operators who know they fear false interruptions more than missed
|
|
@@ -114,8 +130,13 @@ export const DEFAULT_STEP_DOWN_REQUESTS = 3
|
|
|
114
130
|
/** Steps the breaker stays quiet after firing once (no message spam). */
|
|
115
131
|
export const DEFAULT_REFIRE_COOLDOWN_STEPS = 5
|
|
116
132
|
|
|
117
|
-
/**
|
|
118
|
-
|
|
133
|
+
/**
|
|
134
|
+
* The effort ladder a step-down walks along: one notch down from `max` only.
|
|
135
|
+
* `high` is where the ladder stops, because the official effort-cost curve
|
|
136
|
+
* puts the model's sweet spot at 60-80 and nothing supports stepping below it;
|
|
137
|
+
* every other level (including `high` itself) is left alone.
|
|
138
|
+
*/
|
|
139
|
+
const EFFORT_LADDER = ['max', 'high']
|
|
119
140
|
|
|
120
141
|
/** Validate a positive-integer config value, or fall back when absent. */
|
|
121
142
|
function integerAtLeast(value, field, minimum, fallback) {
|
|
@@ -142,6 +163,15 @@ function reasoningCharsOf(event) {
|
|
|
142
163
|
return chars
|
|
143
164
|
}
|
|
144
165
|
|
|
166
|
+
/** Reasoning blocks one assistant/message event carries, text or not. */
|
|
167
|
+
function reasoningBlocksOf(event) {
|
|
168
|
+
let blocks = 0
|
|
169
|
+
for (const block of event?.data?.message?.content ?? []) {
|
|
170
|
+
if (block?.type === 'reasoning') blocks += 1
|
|
171
|
+
}
|
|
172
|
+
return blocks
|
|
173
|
+
}
|
|
174
|
+
|
|
145
175
|
/** Visible reply blocks (non-empty text) one assistant/message event carries. */
|
|
146
176
|
function visibleRepliesOf(event) {
|
|
147
177
|
let count = 0
|
|
@@ -185,14 +215,18 @@ function isErrorResult(event) {
|
|
|
185
215
|
* - the latest tool call's signature and its consecutive-failure count,
|
|
186
216
|
* reset by any success or any differently-shaped call.
|
|
187
217
|
*
|
|
188
|
-
* Returns `{ signal: 'stall' | 'echo' | undefined, detail }` for the TAIL
|
|
189
|
-
* the stream: the breaker only cares whether the session is degenerate NOW.
|
|
218
|
+
* Returns `{ signal: 'stall' | 'echo' | undefined, detail, blind }` for the TAIL
|
|
219
|
+
* of the stream: the breaker only cares whether the session is degenerate NOW.
|
|
220
|
+
* `blind` reports the session-level fact that reasoning blocks were observed
|
|
221
|
+
* without a single character of reasoning text, so the caller can say once that
|
|
222
|
+
* this session's stall detection is running on step counts.
|
|
190
223
|
*/
|
|
191
224
|
export function foldGuardSignal(events, options) {
|
|
192
225
|
const stallSteps = options?.stallSteps ?? DEFAULT_STALL_STEPS
|
|
193
226
|
const stallChars = options?.stallReasoningChars ?? DEFAULT_STALL_REASONING_CHARS
|
|
194
227
|
const echoFailures = options?.echoFailures ?? DEFAULT_ECHO_FAILURES
|
|
195
228
|
const globalCap = options?.globalStallCap ?? DEFAULT_GLOBAL_STALL_CAP
|
|
229
|
+
const blindCap = options?.blindStallCap ?? DEFAULT_BLIND_STALL_CAP
|
|
196
230
|
|
|
197
231
|
// Stall, two independent ladders walked in parallel:
|
|
198
232
|
// 1. PER-STEP: one step whose reasoning alone blows past the character floor
|
|
@@ -202,11 +236,25 @@ export function foldGuardSignal(events, options) {
|
|
|
202
236
|
// hard global cap so a legitimately long investigation (many small
|
|
203
237
|
// read-only steps) cannot trip it by accumulation alone: the streak only
|
|
204
238
|
// counts while the steps are also individually reasoning-heavy.
|
|
239
|
+
// 3. BLIND (fallback): on a route that persists signed reasoning blocks
|
|
240
|
+
// with no text, every character measure reads zero and ladders 1 and 2
|
|
241
|
+
// are structurally dead. When the whole stream carried reasoning blocks
|
|
242
|
+
// but not one character of reasoning text, the step count is the only
|
|
243
|
+
// measure left, so consecutive output-free reasoning-block steps trip
|
|
244
|
+
// STALL at the same cap the slow-burn ladder uses.
|
|
205
245
|
let stallStreak = 0
|
|
206
246
|
let globalStreak = 0
|
|
247
|
+
let blindStreak = 0
|
|
248
|
+
// Session-level facts the blind fallback keys on: whether ANY reasoning text
|
|
249
|
+
// was ever observed, and whether any reasoning block was observed at all.
|
|
250
|
+
// A stream with no reasoning block yet (a fresh session) is NOT blind — the
|
|
251
|
+
// character ladders have simply not had their chance.
|
|
252
|
+
let reasoningTextSeen = false
|
|
253
|
+
let reasoningBlocksSeen = 0
|
|
207
254
|
// pending holds, for the current step, whether we saw reasoning and whether
|
|
208
255
|
// we saw any output (tool call or visible text).
|
|
209
256
|
let pendingReasoning = 0
|
|
257
|
+
let pendingReasoningBlocks = 0
|
|
210
258
|
let pendingOutput = false
|
|
211
259
|
|
|
212
260
|
const closeStep = () => {
|
|
@@ -224,7 +272,16 @@ export function foldGuardSignal(events, options) {
|
|
|
224
272
|
} else if (pendingReasoning >= GLOBAL_MIN_REASONING_CHARS) {
|
|
225
273
|
globalStreak = Math.min(globalStreak + 1, globalCap)
|
|
226
274
|
}
|
|
275
|
+
// Blind ladder: an output-free step that still carried a reasoning block —
|
|
276
|
+
// the exact shape a redacted route produces. Character count is not part of
|
|
277
|
+
// the test, because on that route it is always zero.
|
|
278
|
+
if (pendingOutput) {
|
|
279
|
+
blindStreak = 0
|
|
280
|
+
} else if (pendingReasoningBlocks > 0) {
|
|
281
|
+
blindStreak = Math.min(blindStreak + 1, blindCap)
|
|
282
|
+
}
|
|
227
283
|
pendingReasoning = 0
|
|
284
|
+
pendingReasoningBlocks = 0
|
|
228
285
|
pendingOutput = false
|
|
229
286
|
}
|
|
230
287
|
|
|
@@ -241,7 +298,15 @@ export function foldGuardSignal(events, options) {
|
|
|
241
298
|
break
|
|
242
299
|
case 'assistant/message': {
|
|
243
300
|
const reasoning = reasoningCharsOf(event)
|
|
244
|
-
if (reasoning > 0)
|
|
301
|
+
if (reasoning > 0) {
|
|
302
|
+
pendingReasoning += reasoning
|
|
303
|
+
reasoningTextSeen = true
|
|
304
|
+
}
|
|
305
|
+
const blocks = reasoningBlocksOf(event)
|
|
306
|
+
if (blocks > 0) {
|
|
307
|
+
pendingReasoningBlocks += blocks
|
|
308
|
+
reasoningBlocksSeen += blocks
|
|
309
|
+
}
|
|
245
310
|
if (visibleRepliesOf(event) > 0) pendingOutput = true
|
|
246
311
|
break
|
|
247
312
|
}
|
|
@@ -273,23 +338,35 @@ export function foldGuardSignal(events, options) {
|
|
|
273
338
|
}
|
|
274
339
|
closeStep()
|
|
275
340
|
|
|
341
|
+
// The blind fallback applies only when the session really is on a redacted
|
|
342
|
+
// route: reasoning blocks were observed, none of them carried any text.
|
|
343
|
+
const blind = !reasoningTextSeen && reasoningBlocksSeen > 0
|
|
344
|
+
if (blind && blindStreak >= blindCap) {
|
|
345
|
+
return {
|
|
346
|
+
signal: 'stall',
|
|
347
|
+
detail: `${blindStreak} consecutive output-free reasoning steps (no reasoning text in this session)`,
|
|
348
|
+
blind,
|
|
349
|
+
}
|
|
350
|
+
}
|
|
276
351
|
if (stallStreak >= stallSteps) {
|
|
277
|
-
return { signal: 'stall', detail: `${stallStreak} consecutive runaway-reasoning steps (${stallChars}+ chars each)
|
|
352
|
+
return { signal: 'stall', detail: `${stallStreak} consecutive runaway-reasoning steps (${stallChars}+ chars each)`, blind }
|
|
278
353
|
}
|
|
279
354
|
if (globalStreak >= globalCap) {
|
|
280
|
-
return { signal: 'stall', detail: `${globalStreak} consecutive output-free reasoning steps (slow burn)
|
|
355
|
+
return { signal: 'stall', detail: `${globalStreak} consecutive output-free reasoning steps (slow burn)`, blind }
|
|
281
356
|
}
|
|
282
357
|
if (failStreak >= echoFailures) {
|
|
283
|
-
return { signal: 'echo', detail: `${failStreak} consecutive identical-argument tool failures
|
|
358
|
+
return { signal: 'echo', detail: `${failStreak} consecutive identical-argument tool failures`, blind }
|
|
284
359
|
}
|
|
285
|
-
return { signal: undefined, detail: '' }
|
|
360
|
+
return { signal: undefined, detail: '', blind }
|
|
286
361
|
}
|
|
287
362
|
|
|
288
363
|
/**
|
|
289
364
|
* Resolve the concrete thresholds for one request: the adaptive floor for the
|
|
290
365
|
* current effort, scaled by the sensitivity preset, with any explicit fine-
|
|
291
366
|
* tuning override winning over the table. All inputs are optional; the result
|
|
292
|
-
* is always a complete, valid set.
|
|
367
|
+
* is always a complete, valid set. Every guard fine-tuning field is absent by
|
|
368
|
+
* default, which is what lets the effort table and the sensitivity preset be
|
|
369
|
+
* the shipped behavior rather than dead branches behind a factory value.
|
|
293
370
|
*/
|
|
294
371
|
export function resolveThresholds(options) {
|
|
295
372
|
const effort = options?.effort
|
|
@@ -305,10 +382,20 @@ export function resolveThresholds(options) {
|
|
|
305
382
|
const echoFailures = options?.echoFailures !== undefined
|
|
306
383
|
? options.echoFailures
|
|
307
384
|
: Math.max(2, Math.round(DEFAULT_ECHO_FAILURES * scale))
|
|
308
|
-
|
|
385
|
+
// The blind fallback carries no fine-tuning field of its own: it is a
|
|
386
|
+
// degraded stand-in for the slow-burn ladder, so it scales with the same
|
|
387
|
+
// sensitivity preset unless the caller pins the slow-burn cap explicitly.
|
|
388
|
+
const blindStallCap = options?.blindStallCap !== undefined
|
|
389
|
+
? options.blindStallCap
|
|
390
|
+
: globalStallCap
|
|
391
|
+
return { stallReasoningChars, globalStallCap, echoFailures, blindStallCap }
|
|
309
392
|
}
|
|
310
393
|
|
|
311
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* One notch down the effort ladder, or undefined when the level is left alone.
|
|
396
|
+
* Only `max` moves, and it moves to `high`: the ladder stops at the sweet
|
|
397
|
+
* spot, so a session the user explicitly put on `high` is never weakened.
|
|
398
|
+
*/
|
|
312
399
|
export function stepDownEffort(effort) {
|
|
313
400
|
const index = EFFORT_LADDER.indexOf(effort)
|
|
314
401
|
if (index < 0 || index === EFFORT_LADDER.length - 1) return undefined
|
|
@@ -329,7 +416,7 @@ export function renderGuardMessage(verdict) {
|
|
|
329
416
|
].join(' ')
|
|
330
417
|
}
|
|
331
418
|
|
|
332
|
-
/** Register the signal fold, the pre-step injection, and the effort step-down. */
|
|
419
|
+
/** Register the signal fold, the pre-step injection, and the stall-triggered effort step-down. */
|
|
333
420
|
export function apply(ctx, config) {
|
|
334
421
|
const enabled = config?.enabled !== false
|
|
335
422
|
if (!enabled) return
|
|
@@ -349,16 +436,17 @@ export function apply(ctx, config) {
|
|
|
349
436
|
const stepDownRequests = integerAtLeast(config?.stepDownRequests, 'stepDownRequests', 1, DEFAULT_STEP_DOWN_REQUESTS)
|
|
350
437
|
const refireCooldown = integerAtLeast(config?.refireCooldownSteps, 'refireCooldownSteps', 1, DEFAULT_REFIRE_COOLDOWN_STEPS)
|
|
351
438
|
|
|
352
|
-
// Per-agent runtime state: the breaker cooldown / step-down window
|
|
353
|
-
// current reasoning effort the request waterfall observed
|
|
354
|
-
//
|
|
439
|
+
// Per-agent runtime state: the breaker cooldown / step-down window, the
|
|
440
|
+
// current reasoning effort the request waterfall observed, and whether this
|
|
441
|
+
// session's degraded stall detection has already been reported. The signal
|
|
442
|
+
// itself always re-derives from the durable event stream, so a resume never
|
|
355
443
|
// inherits a stale verdict; the effort is a read-only observation, never a
|
|
356
444
|
// rewrite outside a fired episode.
|
|
357
445
|
const stateByAgent = new WeakMap()
|
|
358
446
|
const stateOf = (agent) => {
|
|
359
447
|
let state = stateByAgent.get(agent)
|
|
360
448
|
if (state === undefined) {
|
|
361
|
-
state = { cooldown: 0, firedVerdict: undefined, stepDownLeft: 0, currentEffort: undefined }
|
|
449
|
+
state = { cooldown: 0, firedVerdict: undefined, stepDownLeft: 0, currentEffort: undefined, blindWarned: false }
|
|
362
450
|
stateByAgent.set(agent, state)
|
|
363
451
|
}
|
|
364
452
|
return state
|
|
@@ -405,10 +493,22 @@ export function apply(ctx, config) {
|
|
|
405
493
|
})
|
|
406
494
|
const verdict = foldGuardSignal(sessionEvents(agent.session), { stallSteps, ...thresholds })
|
|
407
495
|
|
|
496
|
+
// A redacted route makes both character ladders structurally dead; the fold
|
|
497
|
+
// has fallen back to step counts. Say so once per agent, because a session
|
|
498
|
+
// whose stall detection is degraded is a fact an operator debugging a
|
|
499
|
+
// missed episode needs, and the breaker's own fire log never carries it.
|
|
500
|
+
if (verdict.blind && !state.blindWarned) {
|
|
501
|
+
state.blindWarned = true
|
|
502
|
+
try { ctx.logger?.warn?.(`${name}: this session persisted reasoning blocks without any reasoning text, so stall detection is degraded to step counts (${thresholds.blindStallCap} consecutive output-free steps)`) } catch {}
|
|
503
|
+
}
|
|
504
|
+
|
|
408
505
|
if (verdict.signal !== undefined && state.cooldown === 0) {
|
|
409
|
-
// Fire: inject the breaker message and arm the effort step-down
|
|
506
|
+
// Fire: inject the breaker message, and arm the effort step-down only for
|
|
507
|
+
// a STALL. A stall is an over-large reasoning budget, which is what
|
|
508
|
+
// lowering the effort addresses; an echo is a broken call, where
|
|
509
|
+
// weakening the model would only make the loop harder to escape.
|
|
410
510
|
state.cooldown = refireCooldown
|
|
411
|
-
state.stepDownLeft = stepDownRequests
|
|
511
|
+
if (verdict.signal === 'stall') state.stepDownLeft = stepDownRequests
|
|
412
512
|
state.firedVerdict = verdict
|
|
413
513
|
const message = {
|
|
414
514
|
id: globalThis.crypto.randomUUID(),
|
|
@@ -416,7 +516,7 @@ export function apply(ctx, config) {
|
|
|
416
516
|
content: [{ type: 'text', text: renderGuardMessage(verdict) }],
|
|
417
517
|
source: { kind: name },
|
|
418
518
|
}
|
|
419
|
-
try { ctx.logger?.warn?.(`${name}: circuit breaker fired (${verdict.detail}) [${thresholds.stallReasoningChars}ch/${thresholds.globalStallCap}steps/${thresholds.echoFailures}fails, ${sensitivity}, effort ${state.currentEffort ?? 'unknown'}]`) } catch {}
|
|
519
|
+
try { ctx.logger?.warn?.(`${name}: circuit breaker fired (${verdict.detail}) [${thresholds.stallReasoningChars}ch/${thresholds.globalStallCap}steps/${thresholds.echoFailures}fails, ${sensitivity}, effort ${state.currentEffort ?? 'unknown'}, ${verdict.signal === 'stall' ? `step-down ${String(stepDownRequests)} requests` : 'message only'}]`) } catch {}
|
|
420
520
|
return { ...decision, messages: [...decision.messages, message] }
|
|
421
521
|
}
|
|
422
522
|
|
|
@@ -83,11 +83,21 @@
|
|
|
83
83
|
* PLAN MODE is kept by default. `dsh-plan-mode` enforces its rules through
|
|
84
84
|
* the `plan:policy` prompt section alone. `keepPlanPolicy: false` restores the
|
|
85
85
|
* strict one-line surface.
|
|
86
|
+
*
|
|
87
|
+
* DISPATCHER RULES (`dispatcher`, default `true`): the switch appends
|
|
88
|
+
* `./dispatcher-rules.mjs` to the persona text, after the workspace line, under
|
|
89
|
+
* a two-line heading that names the block and states that it takes effect
|
|
90
|
+
* alongside the discipline above it. The persona and its discipline stay
|
|
91
|
+
* exactly where they are — the block adds complexity triage, delegation
|
|
92
|
+
* discipline, and verification on top of them. Switched off, the assembly
|
|
93
|
+
* returns to the bare persona byte for byte: no import side effect, no extra
|
|
94
|
+
* section, no extra separator.
|
|
86
95
|
*/
|
|
87
96
|
|
|
88
97
|
import { readFile, stat } from 'node:fs/promises'
|
|
89
98
|
import { homedir } from 'node:os'
|
|
90
99
|
import { dirname, join, relative, resolve } from 'node:path'
|
|
100
|
+
import { DISPATCHER_RULES } from './dispatcher-rules.mjs'
|
|
91
101
|
|
|
92
102
|
/** Cordis plugin name used by loader diagnostics. */
|
|
93
103
|
export const name = 'liangshen-minimal-prompt'
|
|
@@ -176,6 +186,40 @@ function optionalSource(value, field, fallback) {
|
|
|
176
186
|
return value
|
|
177
187
|
}
|
|
178
188
|
|
|
189
|
+
/**
|
|
190
|
+
* The two-line heading that separates the appended dispatcher block from the
|
|
191
|
+
* persona discipline. The block is long and starts at its own top-level
|
|
192
|
+
* headings, so it is announced rather than being read as one more persona
|
|
193
|
+
* bullet, and the heading states the discipline is still in force.
|
|
194
|
+
*/
|
|
195
|
+
export const DISPATCHER_RULES_HEADING = [
|
|
196
|
+
'',
|
|
197
|
+
'',
|
|
198
|
+
'---',
|
|
199
|
+
'',
|
|
200
|
+
'## Dispatcher rules (a mode-level append)',
|
|
201
|
+
'',
|
|
202
|
+
'You are this session\'s dispatcher: your deliverable is the plan, the task',
|
|
203
|
+
'book, the review verdict, and the hand-off — not the code written in your',
|
|
204
|
+
'place. The working discipline above stays in force; the rules below add',
|
|
205
|
+
'complexity triage, delegation discipline, and verification on top of it.',
|
|
206
|
+
'',
|
|
207
|
+
'---',
|
|
208
|
+
'',
|
|
209
|
+
].join('\n')
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Append the dispatcher block to the persona section text: the persona with
|
|
213
|
+
* its workspace line, the heading, then the rule block. The block is prefixed
|
|
214
|
+
* by the "additional material after the stable prompt prefix" clause so
|
|
215
|
+
* DeepSeek's long-range attention keeps treating the rules as operative, which
|
|
216
|
+
* is how the reference deployment renders them.
|
|
217
|
+
*/
|
|
218
|
+
export function withDispatcherRules(text) {
|
|
219
|
+
if (text.includes(DISPATCHER_RULES)) return text
|
|
220
|
+
return `${text}${DISPATCHER_RULES_HEADING}${DISPATCHER_RULES}`
|
|
221
|
+
}
|
|
222
|
+
|
|
179
223
|
function optionalByteSize(value, field, fallback) {
|
|
180
224
|
if (value === undefined) return fallback
|
|
181
225
|
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
@@ -664,27 +708,39 @@ export function dropInstructionMessages(messages, baselineFiles = [], baselineLo
|
|
|
664
708
|
* to the persona section at assembly time. The literal cwd comes from the
|
|
665
709
|
* session header, so the line stays correct after a workspace switch, and a
|
|
666
710
|
* session without a readable cwd keeps the bare persona rather than failing.
|
|
711
|
+
*
|
|
712
|
+
* `dispatcher` appends the dispatcher block to the SAME persona section, after
|
|
713
|
+
* the workspace line, so the order is fixed: orientation, then the rules. With
|
|
714
|
+
* the switch off this function is the one it always was — same array identity
|
|
715
|
+
* when there is nothing to append, byte-identical text when there is.
|
|
716
|
+
*
|
|
717
|
+
* Nothing is escaped on the way in: a personality section is prompt text the
|
|
718
|
+
* assembler emits, not section text the renderer interpolates, so a literal
|
|
719
|
+
* `{{name}}` inside the appended rules reaches the model as written instead of
|
|
720
|
+
* throwing on an unknown variable. The append is idempotent, like the workspace
|
|
721
|
+
* line: a persona that already carries the block is left alone.
|
|
667
722
|
*/
|
|
668
723
|
const WORKSPACE_LINE_PREFIX = '\n\nYour working directory is '
|
|
669
724
|
|
|
670
|
-
/** Append the workspace line to the persona section
|
|
671
|
-
export function withWorkspaceLine(sections, agent) {
|
|
725
|
+
/** Append the workspace line, and the dispatcher block when the switch is on, to the persona section. */
|
|
726
|
+
export function withWorkspaceLine(sections, agent, dispatcher = false) {
|
|
672
727
|
const cwd = agent?.session?.header?.cwd
|
|
673
|
-
|
|
674
|
-
|
|
728
|
+
const line = (typeof cwd === 'string' && cwd.length > 0) ? `${WORKSPACE_LINE_PREFIX}${cwd}.` : ''
|
|
729
|
+
if (line === '' && !dispatcher) return sections
|
|
675
730
|
const persona = sections.find(section =>
|
|
676
|
-
PERSONA_SECTION_NAMES.includes(section?.name)
|
|
677
|
-
&& typeof section?.text === 'string'
|
|
678
|
-
&& !section.text.includes(line))
|
|
731
|
+
PERSONA_SECTION_NAMES.includes(section?.name) && typeof section?.text === 'string')
|
|
679
732
|
if (persona === undefined) return sections
|
|
733
|
+
const text = line !== '' && !persona.text.includes(line) ? `${persona.text}${line}` : persona.text
|
|
734
|
+
if (text === persona.text && !dispatcher) return sections
|
|
680
735
|
return sections.map(section => section === persona
|
|
681
|
-
? { ...section, text:
|
|
736
|
+
? { ...section, text: dispatcher ? withDispatcherRules(text) : text }
|
|
682
737
|
: section)
|
|
683
738
|
}
|
|
684
739
|
|
|
685
740
|
/** Register the section filter, workspace-instruction source, and dynamic discovery hooks. */
|
|
686
741
|
export function apply(ctx, config) {
|
|
687
742
|
const keepPlanPolicy = optionalBoolean(config?.keepPlanPolicy, 'keepPlanPolicy', true)
|
|
743
|
+
const dispatcher = optionalBoolean(config?.dispatcher, 'dispatcher', true)
|
|
688
744
|
const instructionSource = optionalSource(config?.instructionSource, 'instructionSource', 'host')
|
|
689
745
|
const instructionMaxBytes = optionalByteSize(config?.instructionMaxBytes, 'instructionMaxBytes', DEFAULT_INSTRUCTION_MAX_BYTES)
|
|
690
746
|
|
|
@@ -757,7 +813,7 @@ export function apply(ctx, config) {
|
|
|
757
813
|
+ 'keeping the assembled prompt instead of sending an empty one')
|
|
758
814
|
return assembled
|
|
759
815
|
}
|
|
760
|
-
const narrowed = withWorkspaceLine(sections, context?.agent)
|
|
816
|
+
const narrowed = withWorkspaceLine(sections, context?.agent, dispatcher)
|
|
761
817
|
// 'host' and 'hint' append no workspace-instructions section: the harness's
|
|
762
818
|
// own agent-instructions row carries the content (or the hint replaces it).
|
|
763
819
|
if (instructionSource !== 'system-prompt') return { ...assembled, sections: narrowed }
|
|
@@ -43,6 +43,18 @@
|
|
|
43
43
|
* (`withheldToolNames`) and is lifted before a new one is applied, so repeated
|
|
44
44
|
* collections do not accumulate filters.
|
|
45
45
|
*
|
|
46
|
+
* WHICH PRESENTATION PAGES WHAT: the registry restriction is installed for
|
|
47
|
+
* `ptc` alone, because a collapsed wire is the one presentation where the
|
|
48
|
+
* scope's registry IS the whole reachable surface. `both` deliberately leaves
|
|
49
|
+
* the registry unrestricted: its wire carries the full native roster beside
|
|
50
|
+
* `run_code`, so a paged namespace is off the direct-call surface while the
|
|
51
|
+
* SDK inside a program still reaches it. That reachability is the native wire's
|
|
52
|
+
* own — the wire carries every registered tool and nothing removes the paged
|
|
53
|
+
* ones from the scope's registry — rather than an exemption paging grants, and
|
|
54
|
+
* it is why the paged-out summary states each presentation's reachability
|
|
55
|
+
* instead of describing both with one sentence. `native` carries no program at
|
|
56
|
+
* all, so the wire partition is the whole of paging there.
|
|
57
|
+
*
|
|
46
58
|
* TIMING: that restriction is installed from OUTSIDE the assembly waterfall.
|
|
47
59
|
* `SystemPrompt.assemble()` collects the tool providers and calls every
|
|
48
60
|
* section's `text(context)` BEFORE it runs the `system-prompt/assemble`
|
|
@@ -62,10 +74,12 @@
|
|
|
62
74
|
* The catalog republishes only when its content changes or the published copy
|
|
63
75
|
* left the visible surface (compaction, resume).
|
|
64
76
|
*
|
|
65
|
-
* RESIDENT BUDGET: `maxResidentTokens`
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
77
|
+
* RESIDENT BUDGET: `maxResidentTokens` estimates the always-on-wire surface
|
|
78
|
+
* and warns once when it is crossed; the shipped ceiling is the
|
|
79
|
+
* `DEFAULT_MAX_RESIDENT_TOKENS` constant below, and a deployment may raise or
|
|
80
|
+
* lower it through the config. The guard is a diagnostic, never a truncation:
|
|
81
|
+
* silently dropping a tool the session needs would trade a measurable context
|
|
82
|
+
* cost for an unmeasurable capability loss.
|
|
69
83
|
*/
|
|
70
84
|
|
|
71
85
|
import {
|
|
@@ -322,21 +336,29 @@ const PTC_PROGRAM_LINES = [
|
|
|
322
336
|
/**
|
|
323
337
|
* The short note the catalog carries when `run_code` sits on the wire beside
|
|
324
338
|
* the native roster ('both'): native calls are the primary surface, and the
|
|
325
|
-
* transport covers programmatic batch work.
|
|
326
|
-
*
|
|
339
|
+
* transport covers programmatic batch work. The trigger is countable, because a
|
|
340
|
+
* shape rule alone leaves the boundary to the model's judgement. Paged-out
|
|
341
|
+
* namespaces stay reachable through the SDK inside a program even before
|
|
342
|
+
* activation — the native wire carries every registered tool and 'both' leaves
|
|
343
|
+
* the scope's registry unrestricted, so nothing removed them from the program's
|
|
344
|
+
* own surface.
|
|
327
345
|
*/
|
|
328
346
|
const BOTH_PROGRAM_LINES = [
|
|
329
|
-
'Prefer calling the tools above directly by name: ordinary single-step work (read, edit, bash, one search) goes through the native call, never through `run_code`. Reserve `run_code` for what a direct call cannot do — an async TypeScript body (`code`, with a short `description`) that reaches tools as `await tools.<name>({ ... })`, overlaps independent read-only calls under `Promise.all`, and catches `ToolCallError` to continue: programmatic batch computation, wide fan-out, or multi-step data shaping.
|
|
330
|
-
'
|
|
347
|
+
'Prefer calling the tools above directly by name: ordinary single-step work (read, edit, bash, one search) goes through the native call, never through `run_code`. Reserve `run_code` for what a direct call cannot do — an async TypeScript body (`code`, with a short `description`) that reaches tools as `await tools.<name>({ ... })`, overlaps independent read-only calls under `Promise.all`, and catches `ToolCallError` to continue: programmatic batch computation, wide fan-out, or multi-step data shaping.',
|
|
348
|
+
'',
|
|
349
|
+
'The trigger is countable: one intent with three or more independent targets, or intermediate results that do not belong in the conversation. Below that, call the tools directly. Every call inside a program must handle its own failure with `try/catch`, so the error chain a program produces is longer than a direct call — wrapping one-step work in a program to save a call pays that cost for nothing.',
|
|
350
|
+
'',
|
|
351
|
+
'Paged-out namespaces stay off the NATIVE direct-call surface but stay reachable through the SDK inside a program even before activation — the native wire carries every registered tool and nothing removes the paged ones from the scope registry, so this is the reachability the wire itself carries rather than an exemption paging grants — and activating one also puts its tools back on the direct surface.',
|
|
331
352
|
]
|
|
332
353
|
|
|
333
354
|
/**
|
|
334
355
|
* Model-facing catalog text.
|
|
335
356
|
* - Native presentation: the on-wire tool list plus the paged-out namespace summary.
|
|
336
|
-
* - 'both': the on-wire list INCLUDING `run_code`, the namespace summary
|
|
337
|
-
* the short programmatic-escape note.
|
|
357
|
+
* - 'both': the on-wire list INCLUDING `run_code`, the namespace summary with
|
|
358
|
+
* its direct-call-only reachability, and the short programmatic-escape note.
|
|
338
359
|
* - 'ptc' (wire collapsed to `run_code`): the SDK-reachable list plus the full
|
|
339
|
-
* program contract naming `run_code` the one direct transport
|
|
360
|
+
* program contract naming `run_code` the one direct transport, and a
|
|
361
|
+
* namespace summary whose page covers the whole surface.
|
|
340
362
|
*/
|
|
341
363
|
export function renderCatalogText(entries, presentation = 'native', inactive = []) {
|
|
342
364
|
// Activated paged families are listed under their namespace heading; every
|
|
@@ -364,11 +386,22 @@ export function renderCatalogText(entries, presentation = 'native', inactive = [
|
|
|
364
386
|
const footer = inactive.length === 0
|
|
365
387
|
? 'This is the complete current list and replaces any earlier available-tools list in this session.'
|
|
366
388
|
: 'This is the current on-wire list and replaces any earlier available-tools list in this session.'
|
|
389
|
+
// The paged-out summary states its own reachability per presentation, because
|
|
390
|
+
// the presentations differ on exactly the fact a reader would otherwise have
|
|
391
|
+
// to infer from the program contract further down: 'ptc' pages the whole
|
|
392
|
+
// surface, 'both' pages the direct-call surface only while a program still
|
|
393
|
+
// reaches what it withholds, and 'native' carries no program at all.
|
|
394
|
+
const inactiveReachability = presentation === 'ptc'
|
|
395
|
+
? 'The page covers the whole surface here, not the wire alone: these namespaces are unreachable from inside a program too until they are activated.'
|
|
396
|
+
: presentation === 'both'
|
|
397
|
+
? 'Only the direct-call surface is paged here: a program still reaches these namespaces before activation, and activating one returns its tools to the direct-call surface as well.'
|
|
398
|
+
: 'This presentation carries no program transport, so the wire is the only surface these namespaces can be reached through.'
|
|
367
399
|
const inactiveBlock = inactive.length === 0 ? [] : [
|
|
368
400
|
'',
|
|
369
401
|
'These tool namespaces are registered but currently paged out of the wire:',
|
|
402
|
+
inactiveReachability,
|
|
370
403
|
'<inactive_namespaces>',
|
|
371
|
-
...inactive.map(ns => `- \`${ns.namespace}\` (${ns.count} tool${ns.count === 1 ? '' : 's'}${ns.sample === '' ? '' : `, e.g. \`${ns.sample}\``}): ${ns.description} Call \`tool_activate({ namespace: "${ns.namespace}" })\` to
|
|
404
|
+
...inactive.map(ns => `- \`${ns.namespace}\` (${ns.count} tool${ns.count === 1 ? '' : 's'}${ns.sample === '' ? '' : `, e.g. \`${ns.sample}\``}): ${ns.description} Call \`tool_activate({ namespace: "${ns.namespace}" })\` to activate it.`),
|
|
372
405
|
'</inactive_namespaces>',
|
|
373
406
|
]
|
|
374
407
|
const presentationLines = presentation === 'ptc'
|
package/src/announce.test.ts
CHANGED
|
@@ -200,11 +200,12 @@ describe('dsh-liangshen preset declaration', () => {
|
|
|
200
200
|
const { host } = await mount({ presentation: 'ptc', guardEnabled: false, guardEchoFailures: 5 })
|
|
201
201
|
const rows = host.declarations[0]!.definition.plugins as readonly { id?: string, config?: Record<string, unknown> }[]
|
|
202
202
|
expect(rows.find(row => row.id === 'tool-catalog')?.config?.['presentation']).toBe('ptc')
|
|
203
|
+
// The echo override the operator committed is written; the two overrides
|
|
204
|
+
// nobody set stay out of the row entirely, so the guard resolves them from
|
|
205
|
+
// the session's reasoning effort and the sensitivity preset.
|
|
203
206
|
expect(rows.find(row => row.id === 'guard')?.config).toEqual({
|
|
204
207
|
enabled: false,
|
|
205
208
|
sensitivity: 'balanced',
|
|
206
|
-
stallReasoningChars: 8000,
|
|
207
|
-
globalStallCap: 4,
|
|
208
209
|
echoFailures: 5,
|
|
209
210
|
})
|
|
210
211
|
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Card mark for the LiangShen settings entry: the mode's machine is a
|
|
3
|
+
* slot-machine lever, so the mark is that lever reduced to one knob, one arm
|
|
4
|
+
* and one pivot foot. Three rounded shapes with no interior detail stay
|
|
5
|
+
* legible at 18px, and every shape is painted with `currentColor` so the mark
|
|
6
|
+
* follows the surrounding theme like the lever itself does.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ReactElement } from 'react'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Render the LiangShen card mark.
|
|
13
|
+
* @returns the 18x18 lever mark, in the current text color.
|
|
14
|
+
*/
|
|
15
|
+
export function LiangShenMark(): ReactElement {
|
|
16
|
+
return (
|
|
17
|
+
<svg width="18" height="18" viewBox="0 0 18 18" fill="none" aria-hidden="true">
|
|
18
|
+
<rect x="3.6" y="13.2" width="8.8" height="2.8" rx="1.4" fill="currentColor" />
|
|
19
|
+
<g transform="rotate(20 8 14.4)">
|
|
20
|
+
<rect x="6.5" y="4.6" width="3" height="9.8" rx="1.5" fill="currentColor" />
|
|
21
|
+
<circle cx="8" cy="4.4" r="2.9" fill="currentColor" />
|
|
22
|
+
</g>
|
|
23
|
+
</svg>
|
|
24
|
+
)
|
|
25
|
+
}
|