@bushizhanghan/dsh-context-budget 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/lib/engine.js +17 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -83,16 +83,19 @@ skill catalog: 59 skills, 5022 est tok per turn
|
|
|
83
83
|
- **`estimateTokens` 是估计值,报告里一律标 `est`。** 这个插件要的是**比例**——同一除数在比值里抵消。需要精确数字时用 `request/context` 的 `contextWindow` 做分母。
|
|
84
84
|
- **懒加载规则故意保守**:只标记"本轮从未调用且描述 ≥ 60 token"的技能。用过一次的技能永不标记;没有使用信号的技能不猜测它已死(它可能在别的会话里被用)。
|
|
85
85
|
- **未识别的 `source.kind` 归入 `other` 而不是丢弃**,否则未来 DSH 新增注入面会让审计数字悄悄变小而没人发现。
|
|
86
|
+
- **轮次靠 `turn/start` 累加,不能只读 `data.turn`。** `user/message` 和 `request/header` 根本不带 `turn` 字段(只有 `system/message`、`tool/*`、`turn/start` 带),只读它会把全部注入块和全部请求塞进第 0 轮——173 轮的会话会报成 37 轮。
|
|
87
|
+
- **工具 schema 按请求累加,不按轮覆盖。** 同一轮里每个请求都会重发整套 schema,只留最后一份会把成本算成一份。
|
|
86
88
|
|
|
87
89
|
## 测试
|
|
88
90
|
|
|
89
91
|
```sh
|
|
90
|
-
node --test
|
|
92
|
+
node --test # 19 个单测 + 集成测试
|
|
91
93
|
```
|
|
92
94
|
|
|
93
95
|
`test/engine.test.mjs` 一条规则一个测试(token 估计、CJK、分类映射、签名、重复判定、技能治理、工具分组、空会话不除零)。
|
|
94
96
|
`test/integration.test.mjs` 用假 `ctx = { tools: { register } }` 启动插件,驱动全部 4 个工具打真实会话日志,
|
|
95
|
-
并断言反向情况:不存在的会话/根目录必须返回 `ok: false
|
|
97
|
+
并断言反向情况:不存在的会话/根目录必须返回 `ok: false`。集成测试会挑一个**多于一轮**的会话,
|
|
98
|
+
因为重复注入是跨轮比较出来的,单轮会话没有重复可测。
|
|
96
99
|
|
|
97
100
|
## License
|
|
98
101
|
|
package/lib/engine.js
CHANGED
|
@@ -192,9 +192,21 @@ export function buildTurns(events) {
|
|
|
192
192
|
// system/message carries the assembled system prompt, but only inside its
|
|
193
193
|
// first turn is it new; the same text is resent every turn after. We record
|
|
194
194
|
// it per turn with a signature so the repetition analysis can prove it.
|
|
195
|
+
// `user/message` and `request/header` never carry data.turn — only
|
|
196
|
+
// system/message, tool/* and turn/start do. Reading data.turn alone dropped
|
|
197
|
+
// every injected block and every request into synthetic turn 0, so a 173-turn
|
|
198
|
+
// session reported 37 turns and all 72 requests landed in one bucket.
|
|
199
|
+
let current = 0
|
|
195
200
|
for (const ev of events) {
|
|
196
201
|
const t = String(ev.type || '')
|
|
197
|
-
const
|
|
202
|
+
const explicit = turnOf(ev)
|
|
203
|
+
if (t === 'turn/start') {
|
|
204
|
+
if (explicit != null) current = explicit
|
|
205
|
+
ensure(current)
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
if (explicit != null) current = explicit
|
|
209
|
+
const turn = explicit ?? current
|
|
198
210
|
if (t === 'system/message') {
|
|
199
211
|
const text = contentText(ev.data?.message?.content)
|
|
200
212
|
push(turn ?? 0, {
|
|
@@ -232,7 +244,10 @@ export function buildTurns(events) {
|
|
|
232
244
|
// the model was actually shown.
|
|
233
245
|
t.toolNames = tools.map((x) => x.name)
|
|
234
246
|
t.toolChars = JSON.stringify(tools).length
|
|
235
|
-
|
|
247
|
+
// Every request in the turn re-sends the whole schema set, so the cost
|
|
248
|
+
// is per request, not per turn; overwriting counted one copy and hid
|
|
249
|
+
// the rest (72 requests, one block).
|
|
250
|
+
t.toolTokens = (t.toolTokens || 0) + estimateTokens(JSON.stringify(tools))
|
|
236
251
|
t.toolSignature = signature(tools.map((x) => `${x.name}:${JSON.stringify(x.parameters ?? {})}`).join('|'))
|
|
237
252
|
t.reason = ev.data.reason ?? null
|
|
238
253
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bushizhanghan/dsh-context-budget",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Audit where each turn's context budget goes: per-turn composition (AGENTS.md / skill catalog / tool schemas / runtime context / history), repeated injection of byte-identical content (pure waste), skill-catalog governance, and tool-schema inflation. Deterministic, no LLM. 上下文预算审计:每轮注入构成、重复注入浪费、技能目录治理、工具 schema 膨胀。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
}
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
|
-
"test": "node --test
|
|
49
|
+
"test": "node --test"
|
|
50
50
|
}
|
|
51
51
|
}
|