@try-works/dsh-minimal-router 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/README.md +69 -2
- package/client/index.js +69905 -0
- package/cordis.patch.yml +147 -0
- package/lib/backend.js +14 -0
- package/lib/build-id.js +4 -0
- package/lib/capability-sync.js +27 -0
- package/lib/engine-adapter.js +451 -0
- package/lib/evidence.js +1016 -0
- package/lib/host.js +551 -0
- package/lib/index.js +1916 -0
- package/lib/route-application.js +766 -0
- package/lib/routing.js +847 -0
- package/lib/settings-route.js +52 -0
- package/lib/settings-store.js +770 -0
- package/lib/summariser.js +157 -0
- package/lib/telemetry.js +993 -0
- package/lib/tool-result-pruner.js +224 -0
- package/package.json +38 -4
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// @dsh-local-router/plugin — bundled tool-result pruner (Run 24, DEC-24-07..09).
|
|
2
|
+
//
|
|
3
|
+
// A pure module: no DSH imports, no filesystem, no network. The host wires it
|
|
4
|
+
// onto the `tools/post-execute` waterfall (see index.js) and supplies the
|
|
5
|
+
// summarise function, the spill writer and the audit-row writer. Every
|
|
6
|
+
// reduction is fail-open: a throw, a missing summariser, a failed spill or a
|
|
7
|
+
// non-subagent result returns the original body untouched, so the pruner can
|
|
8
|
+
// never corrupt what the model sees.
|
|
9
|
+
//
|
|
10
|
+
// Modes (DEC-24-01/04/08): off | reference | summarise | headTail.
|
|
11
|
+
// Subagent-only (DEC-24-02): a non-subagent result is returned with mode
|
|
12
|
+
// "skipped". Audit rows follow DEC-24-09:
|
|
13
|
+
// { at, tool, sessionId, subagent, charsBefore, charsAfter, mode, error?, spillPath? }
|
|
14
|
+
//
|
|
15
|
+
// Retention (F-24-01): the waterfall replaces content BEFORE the tool/result
|
|
16
|
+
// event commits, so the original is NOT in the session log. Reference mode is
|
|
17
|
+
// therefore only honest when the host can spill the body to disk; without a
|
|
18
|
+
// working spill it reports "reference-unretained" and says so in the text
|
|
19
|
+
// rather than claiming the text can be re-read.
|
|
20
|
+
|
|
21
|
+
export const TRIM_MARKER = "\n\n[... tool result middle pruned ...]\n\n"
|
|
22
|
+
|
|
23
|
+
export const MODES = ["off", "reference", "summarise", "headTail"]
|
|
24
|
+
|
|
25
|
+
/** Low-friction defaults per DEC-24-03 (the 20000/10000/4000-char budget). */
|
|
26
|
+
/**
|
|
27
|
+
* Derive the char budget from the model's context window instead of a fixed
|
|
28
|
+
* literal. A 20,000-char trigger is meaningless on an 8,192-token window (the
|
|
29
|
+
* trigger is most of the window) and needlessly aggressive on a 1M-token one.
|
|
30
|
+
*
|
|
31
|
+
* Rule: the trigger is `charRatio` of the window expressed in characters
|
|
32
|
+
* (4 chars ~ 1 token), clamped to [minChars, DEFAULT_BUDGET.maxChars]. The head
|
|
33
|
+
* and tail keep the shipped 5:2 proportions of the trigger.
|
|
34
|
+
*
|
|
35
|
+
* @param {object} options
|
|
36
|
+
* @param {number} [options.contextWindow] model context window in tokens
|
|
37
|
+
* @param {number} [options.maxChars] explicit override (wins)
|
|
38
|
+
* @param {number} [options.headChars]
|
|
39
|
+
* @param {number} [options.tailChars]
|
|
40
|
+
* @param {number} [options.charRatio] fraction of the window, default 0.25
|
|
41
|
+
* @param {number} [options.minChars] floor, default 2000
|
|
42
|
+
*/
|
|
43
|
+
export function resolveBudget(options = {}) {
|
|
44
|
+
const opts = options !== null && typeof options === "object" ? options : {}
|
|
45
|
+
const explicitMax = Number.isInteger(opts.maxChars) && opts.maxChars > 0 ? opts.maxChars : null
|
|
46
|
+
const window = Number.isFinite(opts.contextWindow) && opts.contextWindow > 0 ? Math.floor(opts.contextWindow) : null
|
|
47
|
+
const charRatio = Number.isFinite(opts.charRatio) && opts.charRatio > 0 && opts.charRatio < 1 ? opts.charRatio : 0.25
|
|
48
|
+
const minChars = Number.isInteger(opts.minChars) && opts.minChars > 0 ? opts.minChars : 2000
|
|
49
|
+
let maxChars
|
|
50
|
+
let source
|
|
51
|
+
if (explicitMax !== null) {
|
|
52
|
+
maxChars = explicitMax
|
|
53
|
+
source = "config"
|
|
54
|
+
} else if (window !== null) {
|
|
55
|
+
const derived = Math.round(window * 4 * charRatio)
|
|
56
|
+
maxChars = Math.min(Math.max(derived, minChars), DEFAULT_BUDGET.maxChars)
|
|
57
|
+
source = "context-window"
|
|
58
|
+
} else {
|
|
59
|
+
maxChars = DEFAULT_BUDGET.maxChars
|
|
60
|
+
source = "default"
|
|
61
|
+
}
|
|
62
|
+
const headChars = Number.isInteger(opts.headChars) && opts.headChars >= 0 ? opts.headChars : Math.round(maxChars * 0.5)
|
|
63
|
+
const tailChars = Number.isInteger(opts.tailChars) && opts.tailChars >= 0 ? opts.tailChars : Math.round(maxChars * 0.2)
|
|
64
|
+
return { maxChars, headChars, tailChars, source, contextWindow: window }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const DEFAULT_BUDGET = Object.freeze({
|
|
68
|
+
maxChars: 20000,
|
|
69
|
+
headChars: 10000,
|
|
70
|
+
tailChars: 4000
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
/** Count Unicode code points without splitting a surrogate pair. */
|
|
74
|
+
export function codePointLength(text) {
|
|
75
|
+
return Array.from(String(text)).length
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Pointer text for reference mode. `retained` is the honest retention
|
|
80
|
+
* statement: either a spill path or a warning that nothing was kept.
|
|
81
|
+
*/
|
|
82
|
+
function referenceReplacement(opts) {
|
|
83
|
+
const where = [opts.callId ? "call " + opts.callId : null, opts.sessionId ? "session " + opts.sessionId : null]
|
|
84
|
+
.filter(Boolean)
|
|
85
|
+
.join(", ")
|
|
86
|
+
return [
|
|
87
|
+
"[tool result pruned: reference]",
|
|
88
|
+
(opts.tool || "tool") + " produced a " + opts.chars + "-character result; the body is not copied here.",
|
|
89
|
+
opts.retained,
|
|
90
|
+
"Reference: " + (where || "unavailable") + "."
|
|
91
|
+
].join("\n")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Keep the configured head, the trim marker, then the configured tail. */
|
|
95
|
+
function headTailReplacement(body, headChars, tailChars) {
|
|
96
|
+
const points = Array.from(body)
|
|
97
|
+
const head = points.slice(0, headChars).join("")
|
|
98
|
+
const tail = points.slice(Math.max(headChars, points.length - tailChars)).join("")
|
|
99
|
+
return head + TRIM_MARKER + tail
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Create one pruner with a fixed mode and character budget.
|
|
104
|
+
* @param options.mode off | reference | summarise | headTail (default off)
|
|
105
|
+
* @param options.maxChars trigger threshold (default 20000)
|
|
106
|
+
* @param options.headChars head retained in headTail mode (default 10000)
|
|
107
|
+
* @param options.tailChars tail retained in headTail mode (default 4000)
|
|
108
|
+
* @param options.summarise async (body: string) => string | null
|
|
109
|
+
* @param options.spill async (body: string, meta) => string | null; returns
|
|
110
|
+
* a path or pointer that makes the body retrievable.
|
|
111
|
+
* @param options.now () => string timestamp for audit rows.
|
|
112
|
+
*/
|
|
113
|
+
export function createToolResultPruner(options = {}) {
|
|
114
|
+
const opts = options !== null && typeof options === "object" ? options : {}
|
|
115
|
+
// A typo must not silently disable the pruner (verified live: mode
|
|
116
|
+
// "bogus-mode" booted cleanly and coerced to off). The requested value is
|
|
117
|
+
// reported so the host can warn instead of pretending it was configured.
|
|
118
|
+
const invalidMode = opts.mode !== undefined && !MODES.includes(opts.mode)
|
|
119
|
+
const mode = MODES.includes(opts.mode) ? opts.mode : "off"
|
|
120
|
+
const maxChars = Number.isInteger(opts.maxChars) && opts.maxChars > 0 ? opts.maxChars : DEFAULT_BUDGET.maxChars
|
|
121
|
+
const headChars = Number.isInteger(opts.headChars) && opts.headChars >= 0 ? opts.headChars : DEFAULT_BUDGET.headChars
|
|
122
|
+
const tailChars = Number.isInteger(opts.tailChars) && opts.tailChars >= 0 ? opts.tailChars : DEFAULT_BUDGET.tailChars
|
|
123
|
+
const summarise = typeof opts.summarise === "function" ? opts.summarise : null
|
|
124
|
+
const preserve = typeof opts.preserve === "function" ? opts.preserve : null
|
|
125
|
+
const spill = typeof opts.spill === "function" ? opts.spill : null
|
|
126
|
+
const now = typeof opts.now === "function" ? opts.now : () => new Date().toISOString()
|
|
127
|
+
|
|
128
|
+
const pass = (source, body) => ({ ...source, body })
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Reduce one tool result. Returns { result, audit } where `result` is the
|
|
132
|
+
* (possibly) reduced normalized result and `audit` is the DEC-24-09 row.
|
|
133
|
+
* Never throws: every failure path returns the original body plus an error
|
|
134
|
+
* audit row.
|
|
135
|
+
*/
|
|
136
|
+
const apply = async (result) => {
|
|
137
|
+
const source = result !== null && typeof result === "object" ? result : {}
|
|
138
|
+
const tool = typeof source.tool === "string" ? source.tool : ""
|
|
139
|
+
const sessionId = typeof source.sessionId === "string" ? source.sessionId : ""
|
|
140
|
+
const callId = typeof source.callId === "string" ? source.callId : undefined
|
|
141
|
+
const subagent = source.subagent === true
|
|
142
|
+
const body = typeof source.body === "string" ? source.body : ""
|
|
143
|
+
const charsBefore = codePointLength(body)
|
|
144
|
+
|
|
145
|
+
const audit = (modeValue, charsAfter, error, spillPath) => ({
|
|
146
|
+
at: now(),
|
|
147
|
+
tool,
|
|
148
|
+
sessionId,
|
|
149
|
+
subagent,
|
|
150
|
+
charsBefore,
|
|
151
|
+
charsAfter,
|
|
152
|
+
mode: modeValue,
|
|
153
|
+
// V-24-01: an unknown mode used to vanish into mode:"off". The row itself now
|
|
154
|
+
// says what was asked for, so the durable record shows the coercion.
|
|
155
|
+
...(invalidMode === true ? { invalidMode: true, requestedMode: opts.mode } : {}),
|
|
156
|
+
...(error !== undefined ? { error } : {}),
|
|
157
|
+
...(spillPath !== undefined ? { spillPath } : {})
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const untouched = (modeValue) => ({ result: pass(source, body), audit: audit(modeValue, charsBefore) })
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
if (mode === "off") return untouched("off")
|
|
164
|
+
if (!subagent) return untouched("skipped")
|
|
165
|
+
if (charsBefore <= maxChars) return untouched("below-threshold")
|
|
166
|
+
|
|
167
|
+
if (mode === "reference") {
|
|
168
|
+
// No spill means no honest pointer: report the loss instead of claiming
|
|
169
|
+
// the text survives (F-24-01).
|
|
170
|
+
if (spill === null) {
|
|
171
|
+
const nextBody = referenceReplacement({
|
|
172
|
+
tool, sessionId, callId, chars: charsBefore,
|
|
173
|
+
retained: "The full text was NOT retained: this host has no spill writer, so re-run the tool if you need the body."
|
|
174
|
+
})
|
|
175
|
+
return { result: pass(source, nextBody), audit: audit("reference-unretained", codePointLength(nextBody)) }
|
|
176
|
+
}
|
|
177
|
+
const path = await spill(body, { tool, sessionId, callId })
|
|
178
|
+
if (typeof path !== "string" || path.trim().length === 0) {
|
|
179
|
+
// A spill that cannot give back a retrievable location must not be
|
|
180
|
+
// presented as a reference; fail open to the original body.
|
|
181
|
+
return { result: pass(source, body), audit: audit("reference-spill-failed", charsBefore, "spill returned no path") }
|
|
182
|
+
}
|
|
183
|
+
const nextBody = referenceReplacement({
|
|
184
|
+
tool, sessionId, callId, chars: charsBefore,
|
|
185
|
+
retained: "The full text is written to " + path + " and can be read back on request."
|
|
186
|
+
})
|
|
187
|
+
return { result: pass(source, nextBody), audit: audit("reference", codePointLength(nextBody), undefined, path) }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (mode === "headTail") {
|
|
191
|
+
const nextBody = headTailReplacement(body, headChars, tailChars)
|
|
192
|
+
return { result: pass(source, nextBody), audit: audit("headTail", codePointLength(nextBody)) }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (mode === "summarise") {
|
|
196
|
+
if (summarise === null) return untouched("summarise-unavailable")
|
|
197
|
+
const summary = await summarise(body)
|
|
198
|
+
if (typeof summary !== "string" || summary.trim().length === 0) {
|
|
199
|
+
return untouched("summarise-unavailable")
|
|
200
|
+
}
|
|
201
|
+
// Measured (plugin-arms-v6): a model summary alone dropped every
|
|
202
|
+
// identifier in the body. The index is deterministic, so the summary
|
|
203
|
+
// keeps the facts a later turn needs without trusting the model to.
|
|
204
|
+
let nextBody = summary
|
|
205
|
+
if (preserve !== null) {
|
|
206
|
+
const kept = await preserve(body)
|
|
207
|
+
const list = Array.isArray(kept) ? kept.filter((item) => typeof item === "string" && item.length > 0) : []
|
|
208
|
+
if (list.length > 0) nextBody = summary + "\n\nIdentifiers preserved: " + list.join(" ")
|
|
209
|
+
}
|
|
210
|
+
return { result: pass(source, nextBody), audit: audit("summarise", codePointLength(nextBody)) }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Unknown mode is unreachable (validated at construction); fail open.
|
|
214
|
+
return untouched("off")
|
|
215
|
+
} catch (error) {
|
|
216
|
+
return {
|
|
217
|
+
result: pass(source, body),
|
|
218
|
+
audit: audit("error", charsBefore, error instanceof Error ? error.message : String(error))
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { apply, mode, maxChars, headChars, tailChars, invalidMode, requestedMode: opts.mode }
|
|
224
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@try-works/dsh-minimal-router",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "DSH local router plugin: per-session auto-routing between a local engine and remote models, evidence pages, and a plugin-owned engine provider.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"main": "./lib/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./lib/index.js",
|
|
10
|
+
"./host": "./lib/host.js",
|
|
11
|
+
"./client": "./client/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib",
|
|
15
|
+
"client/index.js",
|
|
16
|
+
"cordis.patch.yml",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"!**/*.test.js",
|
|
20
|
+
"!**/*.test.ts"
|
|
21
|
+
],
|
|
22
|
+
"dsh": {
|
|
23
|
+
"bundle": {
|
|
24
|
+
"patch": "./cordis.patch.yml"
|
|
25
|
+
},
|
|
26
|
+
"client": {
|
|
27
|
+
"platform": "web",
|
|
28
|
+
"inject": [
|
|
29
|
+
"@deepseek-ai/dsh-client-ui-slots"
|
|
30
|
+
],
|
|
31
|
+
"external": []
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^18.3.1"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|