dsh-token-trim 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/lib/replay.mjs ADDED
@@ -0,0 +1,353 @@
1
+ // 反事实回放:用真实会话日志逐 step 计算「如果没有 token-trim,账单会是多少」。
2
+ //
3
+ // 模型(每一步都可在 README 里核对):
4
+ // 1. 一条被改写的结果在第 i0 个请求进入上下文(取 ts 之后的第一个 assistant/message);
5
+ // 2. 它一直留在上下文里,直到下一次 compaction(compaction/summary)把它换掉;
6
+ // 3. 于是"多出来的 token"在 k 个请求里被重复支付:
7
+ // - 第一步是未命中(新内容)→ 价格 ×10
8
+ // - 之后 k-1 步是缓存命中 → 价格 ×1
9
+ // 4. 反事实账单 = 当前账单 + Σ(savedTokens × (10 + (k-1)))
10
+ //
11
+ // 只统计能对齐到真实 step 的改写;legacy 行(没有 session)只计数不参与。
12
+ import fs from 'node:fs'
13
+ import path from 'node:path'
14
+ import { homedir } from 'node:os'
15
+ import { decompress } from '../vendor/fzstd/index.mjs'
16
+
17
+ export const PRICES = { hit: 1, miss: 10, out: 15 }
18
+ export const TOK_PER_CHAR = 0.306
19
+ const MAX_LOG_BYTES = 32 * 1024 * 1024
20
+
21
+ /** 扫描 sessions 根目录,建立 sessionId -> 文件 的索引。 */
22
+ export function indexSessionFiles(sessionsRoot) {
23
+ const map = new Map()
24
+ const walk = (dir, depth) => {
25
+ if (depth > 3) return
26
+ let entries
27
+ try {
28
+ entries = fs.readdirSync(dir, { withFileTypes: true })
29
+ } catch {
30
+ return
31
+ }
32
+ for (const entry of entries) {
33
+ const full = path.join(dir, entry.name)
34
+ if (entry.isDirectory()) {
35
+ walk(full, depth + 1)
36
+ } else if (entry.name === 'session.v4.jsonl.zstd') {
37
+ const id = path.basename(dir)
38
+ try {
39
+ const stat = fs.statSync(full)
40
+ if (stat.size <= MAX_LOG_BYTES) map.set(id, { file: full, size: stat.size, mtimeMs: stat.mtimeMs })
41
+ } catch {}
42
+ }
43
+ }
44
+ }
45
+ walk(sessionsRoot, 0)
46
+ return map
47
+ }
48
+
49
+ /** 解析日志文本:保留每一步的时间、用量,以及 compaction 发生在第几步之后。 */
50
+ export function parseLog(text) {
51
+ const steps = []
52
+ const compactions = []
53
+ let current = 0
54
+ for (const line of String(text).split('\n')) {
55
+ if (!line) continue
56
+ let event
57
+ try {
58
+ event = JSON.parse(line)
59
+ } catch {
60
+ continue
61
+ }
62
+ if (!event || typeof event !== 'object') continue
63
+ if (event.type === 'compaction/summary') {
64
+ compactions.push(steps.length)
65
+ continue
66
+ }
67
+ if (event.type !== 'assistant/message') continue
68
+ const usage = event.data?.usage
69
+ if (!usage) continue
70
+ const hit = Number(usage.cacheReadTokens) || 0
71
+ const miss = Number(usage.inputTokens) || 0
72
+ const out = Number(usage.outputTokens) || 0
73
+ const prompt = hit + miss
74
+ steps.push({ time: Number(event.time) || 0, hit, miss, out, prompt })
75
+ current += 1
76
+ }
77
+ return { steps, compactions }
78
+ }
79
+
80
+ export function replaySession({ steps, compactions, rewrites, tokPerChar = TOK_PER_CHAR }) {
81
+ let savedEquiv = 0
82
+ let aligned = 0
83
+ let skipped = 0
84
+ for (const rewrite of rewrites) {
85
+ const savedTokens = (Number(rewrite.originalChars) - Number(rewrite.visibleChars)) * tokPerChar
86
+ if (!(savedTokens > 0)) continue
87
+ const ts = Number(rewrite.ts) || 0
88
+ if (!ts) {
89
+ skipped += 1
90
+ continue
91
+ }
92
+ let i0 = -1
93
+ for (let i = 0; i < steps.length; i += 1) {
94
+ if (steps[i].time > ts) {
95
+ i0 = i
96
+ break
97
+ }
98
+ }
99
+ if (i0 < 0) {
100
+ // 改写发生在最后一次请求之后:它在本次会话里没有被重复发送过
101
+ skipped += 1
102
+ continue
103
+ }
104
+ let end = steps.length
105
+ for (const index of compactions) {
106
+ if (index > i0) {
107
+ end = index
108
+ break
109
+ }
110
+ }
111
+ const k = end - i0
112
+ if (k <= 0) {
113
+ skipped += 1
114
+ continue
115
+ }
116
+ savedEquiv += savedTokens * (PRICES.miss + Math.max(0, k - 1) * PRICES.hit)
117
+ aligned += 1
118
+ }
119
+ return { savedEquiv, aligned, skipped }
120
+ }
121
+
122
+ export function costOf(steps) {
123
+ let cost = 0
124
+ for (const step of steps) {
125
+ cost += step.hit * PRICES.hit + step.miss * PRICES.miss + step.out * PRICES.out
126
+ }
127
+ return cost
128
+ }
129
+
130
+ // ---- 压缩阈值节省(与 dsh-context-kit 同一算法)---------------------------------
131
+ // 用实测的每步增量回放两套策略(当前阈值 vs 出厂 0.8/0.16),比较总成本。
132
+ export const DEFAULT_WINDOW = 1048576
133
+ export const BASE_POLICY = { thresholdRatio: 0.8, retainRatio: 0.16 }
134
+ export const FALLBACK_POLICY = { thresholdRatio: 0.4, retainRatio: 0.12 }
135
+
136
+ const triggerFor = (window, ratio) => Math.min(Math.floor(window * ratio), window - 256000 - 65536)
137
+
138
+ export function estimateCompactionSavings(deltas, options = {}) {
139
+ const window = options.contextWindow ?? DEFAULT_WINDOW
140
+ const current = {
141
+ thresholdRatio: options.thresholdRatio ?? FALLBACK_POLICY.thresholdRatio,
142
+ retainRatio: options.retainRatio ?? FALLBACK_POLICY.retainRatio,
143
+ }
144
+ const base = {
145
+ thresholdRatio: options.baseThresholdRatio ?? BASE_POLICY.thresholdRatio,
146
+ retainRatio: options.baseRetainRatio ?? BASE_POLICY.retainRatio,
147
+ }
148
+ const summaryRatio = options.summaryRatio ?? 0.005
149
+ const steps = deltas.filter((value) => Number.isFinite(value) && value > 0)
150
+ if (steps.length === 0) return null
151
+
152
+ const replay = (policy) => {
153
+ const trigger = triggerFor(window, policy.thresholdRatio)
154
+ const retain = Math.floor(window * policy.retainRatio)
155
+ let prompt = 0
156
+ let cost = 0
157
+ let events = 0
158
+ for (const step of steps) {
159
+ const previous = prompt
160
+ prompt += step
161
+ if (prompt > trigger && prompt - retain > 20000) {
162
+ const shadowed = prompt - retain
163
+ const summary = Math.max(60, Math.round(shadowed * summaryRatio))
164
+ if (summary < shadowed) {
165
+ cost += shadowed * PRICES.miss + summary * PRICES.out
166
+ events += 1
167
+ prompt = retain + summary
168
+ cost += prompt * PRICES.miss
169
+ continue
170
+ }
171
+ }
172
+ cost += previous * PRICES.hit + step * PRICES.miss
173
+ }
174
+ return { cost, events }
175
+ }
176
+
177
+ const currentRun = replay(current)
178
+ const baseRun = replay(base)
179
+ const delta = Math.max(0, baseRun.cost - currentRun.cost)
180
+ return {
181
+ baseCost: baseRun.cost,
182
+ currentCost: currentRun.cost,
183
+ baseEvents: baseRun.events,
184
+ currentEvents: currentRun.events,
185
+ delta,
186
+ savedPct: baseRun.cost > 0 ? ((baseRun.cost - currentRun.cost) / baseRun.cost) * 100 : 0,
187
+ steps: steps.length,
188
+ policy: current,
189
+ basePolicy: base,
190
+ }
191
+ }
192
+
193
+ /** 读当前压缩策略:扫 profile 里的 dsh-context-kit/settings.json,取最新一份。 */
194
+ export function readCompactionPolicy() {
195
+ const base = path.join(homedir(), '.dsh', 'profiles')
196
+ let best = null
197
+ try {
198
+ for (const entry of fs.readdirSync(base, { withFileTypes: true })) {
199
+ if (!entry.isDirectory()) continue
200
+ const file = path.join(base, entry.name, 'dsh-context-kit', 'settings.json')
201
+ try {
202
+ const stat = fs.statSync(file)
203
+ if (!best || stat.mtimeMs > best.mtimeMs) best = { file, mtimeMs: stat.mtimeMs }
204
+ } catch {}
205
+ }
206
+ } catch {
207
+ return { ...FALLBACK_POLICY, source: 'fallback' }
208
+ }
209
+ if (!best) return { ...FALLBACK_POLICY, source: 'fallback' }
210
+ try {
211
+ const parsed = JSON.parse(fs.readFileSync(best.file, 'utf8'))
212
+ const thresholdRatio = Number(parsed.thresholdRatio)
213
+ const retainRatio = Number(parsed.retainRatio)
214
+ return {
215
+ thresholdRatio: Number.isFinite(thresholdRatio) && thresholdRatio > 0 ? thresholdRatio : FALLBACK_POLICY.thresholdRatio,
216
+ retainRatio: Number.isFinite(retainRatio) && retainRatio > 0 ? retainRatio : FALLBACK_POLICY.retainRatio,
217
+ source: best.file,
218
+ }
219
+ } catch {
220
+ return { ...FALLBACK_POLICY, source: 'fallback' }
221
+ }
222
+ }
223
+
224
+ // 解析结果缓存:同一日志文件(mtime+size 未变)不重复解压。
225
+ const parseCache = new Map()
226
+
227
+ function parseSessionCached(entry) {
228
+ const key = entry.file
229
+ const cached = parseCache.get(key)
230
+ if (cached && cached.mtimeMs === entry.mtimeMs && cached.size === entry.size) return cached.parsed
231
+ const raw = decompress(fs.readFileSync(entry.file))
232
+ const parsed = parseLog(Buffer.from(raw).toString('utf8'))
233
+ parseCache.set(key, { mtimeMs: entry.mtimeMs, size: entry.size, parsed })
234
+ if (parseCache.size > 24) parseCache.delete(parseCache.keys().next().value)
235
+ return parsed
236
+ }
237
+
238
+ /** 主入口:把 manifest 里的改写事件按会话对齐到真实日志,算出反事实账单占比。 */
239
+ export function replayAll({ manifest, sessionsRoot, tokPerChar = TOK_PER_CHAR, log = () => {} }) {
240
+ let records = []
241
+ try {
242
+ records = fs
243
+ .readFileSync(manifest, 'utf8')
244
+ .split('\n')
245
+ .filter(Boolean)
246
+ .map((line) => {
247
+ try {
248
+ return JSON.parse(line)
249
+ } catch {
250
+ return null
251
+ }
252
+ })
253
+ .filter(Boolean)
254
+ } catch {
255
+ return { available: false, reason: 'no-manifest', manifest }
256
+ }
257
+
258
+ const files = indexSessionFiles(sessionsRoot)
259
+ const policy = readCompactionPolicy()
260
+ const bySession = new Map()
261
+ let legacy = 0
262
+ for (const record of records) {
263
+ const session = record.session
264
+ if (!session || session === 'legacy') {
265
+ legacy += 1
266
+ continue
267
+ }
268
+ const list = bySession.get(session) ?? []
269
+ list.push(record)
270
+ bySession.set(session, list)
271
+ }
272
+
273
+ const sessions = []
274
+ let savedEquiv = 0
275
+ let compactionDelta = 0
276
+ let costCurrent = 0
277
+ let aligned = 0
278
+ let skipped = 0
279
+ let missing = 0
280
+
281
+ for (const [session, rewrites] of bySession) {
282
+ const entry = files.get(session)
283
+ if (!entry) {
284
+ missing += 1
285
+ continue
286
+ }
287
+ let parsed
288
+ try {
289
+ parsed = parseSessionCached(entry)
290
+ } catch {
291
+ missing += 1
292
+ continue
293
+ }
294
+ const result = replaySession({ steps: parsed.steps, compactions: parsed.compactions, rewrites, tokPerChar })
295
+ const cost = costOf(parsed.steps)
296
+ const deltas = []
297
+ let previous = 0
298
+ for (const step of parsed.steps) {
299
+ const delta = step.prompt - previous
300
+ if (delta > 0) deltas.push(delta)
301
+ previous = step.prompt
302
+ }
303
+ const compaction = estimateCompactionSavings(deltas, {
304
+ thresholdRatio: policy.thresholdRatio,
305
+ retainRatio: policy.retainRatio,
306
+ })
307
+ savedEquiv += result.savedEquiv
308
+ compactionDelta += compaction ? compaction.delta : 0
309
+ costCurrent += cost
310
+ aligned += result.aligned
311
+ skipped += result.skipped
312
+ sessions.push({
313
+ sessionId: session,
314
+ steps: parsed.steps.length,
315
+ compactions: parsed.compactions.length,
316
+ rewrites: rewrites.length,
317
+ aligned: result.aligned,
318
+ skipped: result.skipped,
319
+ savedEquiv: result.savedEquiv,
320
+ compactionDelta: compaction ? compaction.delta : 0,
321
+ costCurrent: cost,
322
+ })
323
+ log(session)
324
+ }
325
+
326
+ sessions.sort((a, b) => b.savedEquiv + b.compactionDelta - (a.savedEquiv + a.compactionDelta))
327
+ const baselineCost = costCurrent + savedEquiv + compactionDelta
328
+ const totalDelta = savedEquiv + compactionDelta
329
+ const share = (value) => (baselineCost > 0 ? (value / baselineCost) * 100 : 0)
330
+ return {
331
+ available: aligned > 0,
332
+ manifest,
333
+ sessionsRoot,
334
+ policy,
335
+ totals: {
336
+ sessions: sessions.length,
337
+ events: records.length,
338
+ aligned,
339
+ skipped,
340
+ missingSessions: missing,
341
+ legacy,
342
+ costCurrent,
343
+ baselineCost,
344
+ ruleSavedEquiv: savedEquiv,
345
+ compactionSavedEquiv: compactionDelta,
346
+ totalSavedEquiv: totalDelta,
347
+ rulePct: share(savedEquiv),
348
+ compactionPct: share(compactionDelta),
349
+ totalPct: share(totalDelta),
350
+ },
351
+ sessions: sessions.slice(0, 20),
352
+ }
353
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-token-trim",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Tool-result trimmer for DeepSeek Harness: structural summaries, head/tail truncation, repeat deltas and artifact spill before results enter the context — plus a context index and a measured savings view.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -33,7 +33,8 @@
33
33
  "cordis.patch.yml",
34
34
  "icon.svg",
35
35
  "README.md",
36
- "LICENSE"
36
+ "LICENSE",
37
+ "vendor/"
37
38
  ],
38
39
  "exports": {
39
40
  ".": "./index.js",
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Arjun Barrett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.