dsh-context-kit 0.1.1 → 0.3.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/README.md +41 -9
- package/bin/dsh-context-kit.mjs +36 -1
- package/client/panel.js +341 -115
- package/cordis.patch.yml +4 -14
- package/host.js +8 -0
- package/lib/kit-ops.mjs +25 -2
- package/lib/remote.mjs +11 -1
- package/lib/tool-savings.mjs +78 -0
- package/lib/usage.mjs +448 -0
- package/package.json +6 -3
- package/tuner.js +67 -3
- package/vendor/fzstd/LICENSE +21 -0
- package/vendor/fzstd/index.mjs +753 -0
package/lib/remote.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
7
|
+
import { summarizeToolSavings } from './tool-savings.mjs'
|
|
7
8
|
|
|
8
9
|
/** 施加 Remote(name) 标记,兼容无装饰器的写法(与 dsh-memory-local 同款)。 */
|
|
9
10
|
export function markRemoteMethod(serviceClass, method, exportName = method) {
|
|
@@ -48,8 +49,17 @@ export class ContextKitRemoteService extends TypertRemoteService {
|
|
|
48
49
|
clear() {
|
|
49
50
|
return this.kit.clear()
|
|
50
51
|
}
|
|
52
|
+
|
|
53
|
+
async report(days) {
|
|
54
|
+
return this.kit.report({ days: Number(days) || 7 })
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 独立统计:不依赖 token-guard 插件,直接读它的 manifest。 */
|
|
58
|
+
toolSavings() {
|
|
59
|
+
return summarizeToolSavings()
|
|
60
|
+
}
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
for (const method of ['status', 'preview', 'save', 'sync', 'clear']) {
|
|
63
|
+
for (const method of ['status', 'preview', 'save', 'sync', 'clear', 'report', 'toolSavings']) {
|
|
54
64
|
markRemoteMethod(ContextKitRemoteService, method)
|
|
55
65
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具节省的「独立统计」:直接读 token-trim 的 manifest,不依赖 token-trim 插件在场。
|
|
3
|
+
* 与 token-trim 的 Settings 视图共享同一口径(0.306 token/字符,工具结果占账单 40.7%)。
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
|
|
9
|
+
export const TOK_PER_CHAR = 0.306
|
|
10
|
+
export const TOOL_SHARE_PCT = 40.7
|
|
11
|
+
export const TOOL_TOKENS_BASELINE = 751715
|
|
12
|
+
|
|
13
|
+
export function defaultManifestPath() {
|
|
14
|
+
const current = path.join(homedir(), '.dsh', 'token-trim', 'manifest.jsonl')
|
|
15
|
+
const legacy = path.join(homedir(), '.dsh', 'token-guard', 'manifest.jsonl')
|
|
16
|
+
try {
|
|
17
|
+
if (!existsSync(current) && existsSync(legacy)) return legacy
|
|
18
|
+
} catch {}
|
|
19
|
+
return current
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readRecords(file) {
|
|
23
|
+
try {
|
|
24
|
+
const text = readFileSync(file, 'utf8')
|
|
25
|
+
const lines = text.split('\n')
|
|
26
|
+
const out = []
|
|
27
|
+
for (const line of lines) {
|
|
28
|
+
if (!line) continue
|
|
29
|
+
try { out.push(JSON.parse(line)) } catch {}
|
|
30
|
+
}
|
|
31
|
+
return out
|
|
32
|
+
} catch {
|
|
33
|
+
return []
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const billPctOf = (savedChars) => (savedChars * TOK_PER_CHAR / TOOL_TOKENS_BASELINE) * TOOL_SHARE_PCT
|
|
38
|
+
|
|
39
|
+
export function summarizeToolSavings(manifestFile = defaultManifestPath()) {
|
|
40
|
+
const records = readRecords(manifestFile)
|
|
41
|
+
if (!records.length) return { available: false, manifest: manifestFile }
|
|
42
|
+
let orig = 0
|
|
43
|
+
let vis = 0
|
|
44
|
+
const tools = new Map()
|
|
45
|
+
const sessions = new Set()
|
|
46
|
+
let last = 0
|
|
47
|
+
for (const r of records) {
|
|
48
|
+
const o = Number(r.originalChars) || 0
|
|
49
|
+
const v = Number(r.visibleChars) || 0
|
|
50
|
+
orig += o
|
|
51
|
+
vis += v
|
|
52
|
+
if (r.session) sessions.add(String(r.session))
|
|
53
|
+
last = Math.max(last, Number(r.ts) || 0)
|
|
54
|
+
const name = typeof r.tool === 'string' ? r.tool : '(unknown)'
|
|
55
|
+
const t = tools.get(name) || { name, events: 0, saved: 0 }
|
|
56
|
+
t.events += 1
|
|
57
|
+
t.saved += Math.max(0, o - v)
|
|
58
|
+
tools.set(name, t)
|
|
59
|
+
}
|
|
60
|
+
let updatedAt = 0
|
|
61
|
+
try { updatedAt = statSync(manifestFile).mtimeMs } catch {}
|
|
62
|
+
const saved = Math.max(0, orig - vis)
|
|
63
|
+
return {
|
|
64
|
+
available: true,
|
|
65
|
+
manifest: manifestFile,
|
|
66
|
+
events: records.length,
|
|
67
|
+
sessions: sessions.size,
|
|
68
|
+
orig,
|
|
69
|
+
vis,
|
|
70
|
+
saved,
|
|
71
|
+
cutPct: orig ? Number(((1 - vis / orig) * 100).toFixed(1)) : 0,
|
|
72
|
+
tokens: Math.round(saved * TOK_PER_CHAR),
|
|
73
|
+
billPct: Number(billPctOf(saved).toFixed(2)),
|
|
74
|
+
last,
|
|
75
|
+
updatedAt,
|
|
76
|
+
tools: [...tools.values()].sort((a, b) => b.saved - a.saved).slice(0, 6),
|
|
77
|
+
}
|
|
78
|
+
}
|
package/lib/usage.mjs
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
// 效果统计:读本机会话日志,给出「实测台账 + 估算节省 + 返工信号」。
|
|
2
|
+
//
|
|
3
|
+
// 三条原则:
|
|
4
|
+
// 1. 只读本地文件,不联网、不上报任何内容。
|
|
5
|
+
// 2. 实测与估算分开标注:token 用量是实测,节省百分比是模型估算(反事实无法观测)。
|
|
6
|
+
// 3. 解析函数是纯函数(吃 JSONL 文本),便于测试;读文件与解压是薄薄一层。
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import path from 'node:path'
|
|
9
|
+
import { decompress } from '../vendor/fzstd/index.mjs'
|
|
10
|
+
|
|
11
|
+
/** 价格比(DeepSeek 公开价:命中/未命中/输出)。只用于把用量折算成可比较的"命中当量"。 */
|
|
12
|
+
export const PRICES = { hit: 1, miss: 10, out: 15 }
|
|
13
|
+
|
|
14
|
+
/** DSH 默认压缩参数,作为"不装本插件"的基线。 */
|
|
15
|
+
export const BASE_POLICY = { thresholdRatio: 0.8, retainRatio: 0.16 }
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_CONTEXT_WINDOW = 1048576
|
|
18
|
+
|
|
19
|
+
/** 从工具参数里取一个"做什么"的签名,用于识别重复劳动。 */
|
|
20
|
+
export function toolKey(name, args) {
|
|
21
|
+
let parsed = args
|
|
22
|
+
if (typeof args === 'string') {
|
|
23
|
+
try {
|
|
24
|
+
parsed = JSON.parse(args)
|
|
25
|
+
} catch {
|
|
26
|
+
parsed = null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const pick = (value) => (typeof value === 'string' && value.trim() ? value.trim() : '')
|
|
30
|
+
if (parsed && typeof parsed === 'object') {
|
|
31
|
+
const interesting =
|
|
32
|
+
pick(parsed.path) ||
|
|
33
|
+
pick(parsed.file_path) ||
|
|
34
|
+
pick(parsed.command) ||
|
|
35
|
+
pick(parsed.pattern) ||
|
|
36
|
+
pick(parsed.query) ||
|
|
37
|
+
pick(parsed.url)
|
|
38
|
+
if (interesting) return `${name}|${interesting}`
|
|
39
|
+
try {
|
|
40
|
+
return `${name}|${JSON.stringify(parsed).slice(0, 120)}`
|
|
41
|
+
} catch {
|
|
42
|
+
return `${name}|`
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return `${name}|`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const num = (value) => (Number.isFinite(Number(value)) ? Number(value) : 0)
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 解析一个会话的 JSONL 文本。
|
|
52
|
+
* 纯函数:不碰文件系统,测试直接喂字符串。
|
|
53
|
+
*/
|
|
54
|
+
export function parseSessionJsonl(text) {
|
|
55
|
+
const steps = []
|
|
56
|
+
const deltas = []
|
|
57
|
+
const compactionTriggers = []
|
|
58
|
+
const calls = new Map()
|
|
59
|
+
let compactions = 0
|
|
60
|
+
let summaryTokens = 0
|
|
61
|
+
let prompt = 0
|
|
62
|
+
let previousPrompt = 0
|
|
63
|
+
let firstTime = 0
|
|
64
|
+
let lastTime = 0
|
|
65
|
+
|
|
66
|
+
for (const line of String(text).split('\n')) {
|
|
67
|
+
if (!line) continue
|
|
68
|
+
let event
|
|
69
|
+
try {
|
|
70
|
+
event = JSON.parse(line)
|
|
71
|
+
} catch {
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
if (!event || typeof event !== 'object') continue
|
|
75
|
+
if (Number.isFinite(event.time)) {
|
|
76
|
+
if (!firstTime) firstTime = event.time
|
|
77
|
+
lastTime = event.time
|
|
78
|
+
}
|
|
79
|
+
const type = event.type
|
|
80
|
+
|
|
81
|
+
if (type === 'tool/call') {
|
|
82
|
+
const data = event.data || {}
|
|
83
|
+
const key = toolKey(String(data.name ?? 'unknown'), data.arguments)
|
|
84
|
+
calls.set(key, (calls.get(key) ?? 0) + 1)
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (type === 'compaction/start') {
|
|
89
|
+
// 压缩发生时的 prompt 高度 ≈ 当时的阈值:用它给这次会话的运行策略分期
|
|
90
|
+
if (previousPrompt > 0) compactionTriggers.push(previousPrompt)
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (type === 'compaction/summary') {
|
|
95
|
+
compactions += 1
|
|
96
|
+
const blocks = event.data?.summary
|
|
97
|
+
if (Array.isArray(blocks)) {
|
|
98
|
+
for (const block of blocks) if (typeof block?.text === 'string') summaryTokens += Math.ceil(block.text.length / 4)
|
|
99
|
+
}
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (type !== 'assistant/message') continue
|
|
104
|
+
const usage = event.data?.usage
|
|
105
|
+
if (!usage) continue
|
|
106
|
+
const hit = num(usage.cacheReadTokens)
|
|
107
|
+
const miss = num(usage.inputTokens)
|
|
108
|
+
const out = num(usage.outputTokens)
|
|
109
|
+
prompt = hit + miss
|
|
110
|
+
const delta = prompt - previousPrompt
|
|
111
|
+
// 压缩后 prompt 会变短,这一步的负增量不参与回放(否则会把压缩当成"缩水")
|
|
112
|
+
if (delta > 0) deltas.push(delta)
|
|
113
|
+
steps.push({ hit, miss, out, prompt })
|
|
114
|
+
previousPrompt = prompt
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let duplicates = 0
|
|
118
|
+
let reused = 0
|
|
119
|
+
for (const count of calls.values()) {
|
|
120
|
+
if (count > 1) {
|
|
121
|
+
duplicates += count - 1
|
|
122
|
+
reused += 1
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
steps,
|
|
128
|
+
deltas,
|
|
129
|
+
compactionTriggers,
|
|
130
|
+
compactions,
|
|
131
|
+
summaryTokens,
|
|
132
|
+
duplicates,
|
|
133
|
+
distinctCalls: calls.size,
|
|
134
|
+
repeatedGroups: reused,
|
|
135
|
+
firstTime,
|
|
136
|
+
lastTime,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 把解析结果压成紧凑统计(缓存只存这个,别存完整 steps)。 */
|
|
141
|
+
export function compactSession(parsed) {
|
|
142
|
+
const compact = {
|
|
143
|
+
stepCount: parsed.steps.length,
|
|
144
|
+
hit: 0,
|
|
145
|
+
miss: 0,
|
|
146
|
+
out: 0,
|
|
147
|
+
promptMax: 0,
|
|
148
|
+
compactions: parsed.compactions,
|
|
149
|
+
duplicates: parsed.duplicates,
|
|
150
|
+
distinctCalls: parsed.distinctCalls,
|
|
151
|
+
deltas: parsed.deltas,
|
|
152
|
+
compactionTriggers: parsed.compactionTriggers ?? [],
|
|
153
|
+
}
|
|
154
|
+
for (const step of parsed.steps) {
|
|
155
|
+
compact.hit += step.hit
|
|
156
|
+
compact.miss += step.miss
|
|
157
|
+
compact.out += step.out
|
|
158
|
+
if (step.prompt > compact.promptMax) compact.promptMax = step.prompt
|
|
159
|
+
}
|
|
160
|
+
return compact
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 把多个会话的紧凑统计汇总成台账。 */
|
|
164
|
+
export function summarize(parsed) {
|
|
165
|
+
const totals = { sessions: parsed.length, steps: 0, hit: 0, miss: 0, out: 0, compactions: 0, duplicates: 0, distinctCalls: 0, promptMax: 0 }
|
|
166
|
+
for (const session of parsed) {
|
|
167
|
+
totals.steps += session.stepCount
|
|
168
|
+
totals.hit += session.hit
|
|
169
|
+
totals.miss += session.miss
|
|
170
|
+
totals.out += session.out
|
|
171
|
+
totals.compactions += session.compactions
|
|
172
|
+
totals.duplicates += session.duplicates
|
|
173
|
+
totals.distinctCalls += session.distinctCalls
|
|
174
|
+
if (session.promptMax > totals.promptMax) totals.promptMax = session.promptMax
|
|
175
|
+
}
|
|
176
|
+
const cost = {
|
|
177
|
+
hit: totals.hit * PRICES.hit,
|
|
178
|
+
miss: totals.miss * PRICES.miss,
|
|
179
|
+
out: totals.out * PRICES.out,
|
|
180
|
+
}
|
|
181
|
+
cost.total = cost.hit + cost.miss + cost.out
|
|
182
|
+
const promptTotal = totals.hit + totals.miss
|
|
183
|
+
return {
|
|
184
|
+
...totals,
|
|
185
|
+
promptAvg: totals.steps > 0 ? Math.round(promptTotal / totals.steps) : 0,
|
|
186
|
+
cost,
|
|
187
|
+
missSharePct: cost.total > 0 ? (cost.miss / cost.total) * 100 : 0,
|
|
188
|
+
duplicatePct: totals.distinctCalls > 0 ? (totals.duplicates / (totals.distinctCalls + totals.duplicates)) * 100 : 0,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const triggerFor = (window, ratio) => Math.min(Math.floor(window * ratio), window - 256000 - 65536)
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 这次会话当时跑的是哪套压缩策略?
|
|
196
|
+
* 只看得到副作用:压缩发生时的 prompt 高度。没有压缩事件的会话无法判断(记为 no-compaction)。
|
|
197
|
+
*
|
|
198
|
+
* 注意:阈值和触发点是多对一的(ratio 超过预算后触发点被 727040 截断),
|
|
199
|
+
* 所以按「触发点的整数高度」分期,不硬猜 ratio。
|
|
200
|
+
*/
|
|
201
|
+
export function classifySession(session, options = {}) {
|
|
202
|
+
const contextWindow = options.contextWindow ?? DEFAULT_CONTEXT_WINDOW
|
|
203
|
+
const budget = contextWindow - 256000 - 65536
|
|
204
|
+
const triggers = (session.compactionTriggers ?? []).filter((value) => Number.isFinite(value) && value > 0)
|
|
205
|
+
if (triggers.length === 0) return { key: 'no-compaction', triggerTokens: null, thresholdRatio: null }
|
|
206
|
+
const sorted = [...triggers].sort((a, b) => a - b)
|
|
207
|
+
const median = sorted[Math.floor(sorted.length / 2)]
|
|
208
|
+
const bucket = Math.max(8192, Math.round(median / 8192) * 8192)
|
|
209
|
+
return {
|
|
210
|
+
key: `t${bucket}`,
|
|
211
|
+
triggerTokens: bucket,
|
|
212
|
+
// 触发点贴着预算上限时,无法反推 ratio(任何更大的 ratio 都给同一个触发点)
|
|
213
|
+
thresholdRatio: bucket >= budget - 8192 ? null : Math.round((bucket / contextWindow) * 100) / 100,
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 按观测到的策略分期。这是「实测归因」的核心:
|
|
219
|
+
* 历史数据里混着旧阈值跑出来的会话,不能整体当作当前配置的效果。
|
|
220
|
+
*/
|
|
221
|
+
export function groupByPolicy(sessions, options = {}) {
|
|
222
|
+
const contextWindow = options.contextWindow ?? DEFAULT_CONTEXT_WINDOW
|
|
223
|
+
const budget = contextWindow - 256000 - 65536
|
|
224
|
+
const currentRatio = options.thresholdRatio ?? 0.4
|
|
225
|
+
const currentTrigger = Math.min(Math.floor(contextWindow * currentRatio), budget)
|
|
226
|
+
const groups = new Map()
|
|
227
|
+
for (const session of sessions) {
|
|
228
|
+
const policy = classifySession(session, { contextWindow })
|
|
229
|
+
const group = groups.get(policy.key) ?? { ...policy, members: [] }
|
|
230
|
+
group.members.push(session)
|
|
231
|
+
groups.set(policy.key, group)
|
|
232
|
+
}
|
|
233
|
+
return [...groups.values()]
|
|
234
|
+
.map((group) => ({
|
|
235
|
+
key: group.key,
|
|
236
|
+
triggerTokens: group.triggerTokens,
|
|
237
|
+
thresholdRatio: group.thresholdRatio,
|
|
238
|
+
isCurrent:
|
|
239
|
+
group.triggerTokens !== null && Math.abs(group.triggerTokens - currentTrigger) <= 32768,
|
|
240
|
+
totals: summarize(group.members),
|
|
241
|
+
}))
|
|
242
|
+
.sort((a, b) => b.totals.steps - a.totals.steps)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 反事实回放:同一批"每步新增量",分别按当前阈值和基线阈值压缩,比较总成本。
|
|
247
|
+
* 这是估算:假设工作量不变、价格比固定、摘要规模与实测同比例。面板里必须标注"估算"。
|
|
248
|
+
*/
|
|
249
|
+
export function estimateSavings(deltas, options = {}) {
|
|
250
|
+
const window = options.contextWindow ?? DEFAULT_CONTEXT_WINDOW
|
|
251
|
+
const current = { thresholdRatio: options.thresholdRatio ?? 0.4, retainRatio: options.retainRatio ?? 0.12 }
|
|
252
|
+
const base = { thresholdRatio: options.baseThresholdRatio ?? BASE_POLICY.thresholdRatio, retainRatio: options.baseRetainRatio ?? BASE_POLICY.retainRatio }
|
|
253
|
+
const summaryRatio = options.summaryRatio ?? 0.005
|
|
254
|
+
const steps = deltas.filter((value) => Number.isFinite(value) && value > 0)
|
|
255
|
+
if (steps.length === 0) return null
|
|
256
|
+
|
|
257
|
+
const replay = (policy) => {
|
|
258
|
+
const trigger = triggerFor(window, policy.thresholdRatio)
|
|
259
|
+
const retain = Math.floor(window * policy.retainRatio)
|
|
260
|
+
let prompt = 0
|
|
261
|
+
let cost = 0
|
|
262
|
+
let events = 0
|
|
263
|
+
for (const step of steps) {
|
|
264
|
+
const previous = prompt
|
|
265
|
+
prompt += step
|
|
266
|
+
if (prompt > trigger && prompt - retain > 20000) {
|
|
267
|
+
const shadowed = prompt - retain
|
|
268
|
+
const summary = Math.max(60, Math.round(shadowed * summaryRatio))
|
|
269
|
+
if (summary < shadowed) {
|
|
270
|
+
cost += shadowed * PRICES.miss + summary * PRICES.out
|
|
271
|
+
events += 1
|
|
272
|
+
prompt = retain + summary
|
|
273
|
+
cost += prompt * PRICES.miss
|
|
274
|
+
continue
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
cost += previous * PRICES.hit + step * PRICES.miss
|
|
278
|
+
}
|
|
279
|
+
return { cost, events }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const currentRun = replay(current)
|
|
283
|
+
const baseRun = replay(base)
|
|
284
|
+
return {
|
|
285
|
+
estimate: true,
|
|
286
|
+
baseThresholdRatio: base.thresholdRatio,
|
|
287
|
+
baseRetainRatio: base.retainRatio,
|
|
288
|
+
thresholdRatio: current.thresholdRatio,
|
|
289
|
+
retainRatio: current.retainRatio,
|
|
290
|
+
baseCost: baseRun.cost,
|
|
291
|
+
currentCost: currentRun.cost,
|
|
292
|
+
baseEvents: baseRun.events,
|
|
293
|
+
currentEvents: currentRun.events,
|
|
294
|
+
savedPct: baseRun.cost > 0 ? ((baseRun.cost - currentRun.cost) / baseRun.cost) * 100 : 0,
|
|
295
|
+
steps: steps.length,
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const CACHE_VERSION = 2
|
|
300
|
+
|
|
301
|
+
function readCache(cachePath) {
|
|
302
|
+
try {
|
|
303
|
+
const parsed = JSON.parse(fs.readFileSync(cachePath, 'utf8'))
|
|
304
|
+
if (parsed && parsed.version === CACHE_VERSION && parsed.files && typeof parsed.files === 'object') return parsed
|
|
305
|
+
} catch {}
|
|
306
|
+
return { version: CACHE_VERSION, files: {} }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function writeCache(cachePath, cache) {
|
|
310
|
+
try {
|
|
311
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true })
|
|
312
|
+
const tmp = `${cachePath}.tmp`
|
|
313
|
+
fs.writeFileSync(tmp, JSON.stringify(cache), 'utf8')
|
|
314
|
+
fs.renameSync(tmp, cachePath)
|
|
315
|
+
} catch {}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function collectSessionFiles(home, since, limits) {
|
|
319
|
+
const root = path.join(home, 'sessions')
|
|
320
|
+
const files = []
|
|
321
|
+
const walk = (dir, depth) => {
|
|
322
|
+
if (depth > 3) return
|
|
323
|
+
let entries
|
|
324
|
+
try {
|
|
325
|
+
entries = fs.readdirSync(dir, { withFileTypes: true })
|
|
326
|
+
} catch {
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
for (const entry of entries) {
|
|
330
|
+
const full = path.join(dir, entry.name)
|
|
331
|
+
if (entry.isDirectory()) walk(full, depth + 1)
|
|
332
|
+
else if (entry.name === 'session.v4.jsonl.zstd') {
|
|
333
|
+
try {
|
|
334
|
+
const stat = fs.statSync(full)
|
|
335
|
+
if (stat.mtimeMs >= since && stat.size <= limits.maxBytes) {
|
|
336
|
+
files.push({ file: full, mtimeMs: stat.mtimeMs, size: stat.size })
|
|
337
|
+
}
|
|
338
|
+
} catch {}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
walk(root, 0)
|
|
343
|
+
files.sort((a, b) => b.mtimeMs - a.mtimeMs)
|
|
344
|
+
return files.slice(0, limits.maxFiles)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 读取近期会话并汇总。
|
|
349
|
+
* 生成器形式:每个文件之后让出一次,供同步/异步两个入口复用——
|
|
350
|
+
* 同步入口用于 CLI/测试,异步入口用于宿主 RPC(避免解压大日志时卡住事件循环)。
|
|
351
|
+
*/
|
|
352
|
+
function* collectReport(options = {}) {
|
|
353
|
+
const home = options.home
|
|
354
|
+
const cachePath = options.cachePath
|
|
355
|
+
const days = options.days ?? 7
|
|
356
|
+
const limits = { maxFiles: options.maxFiles ?? 40, maxBytes: options.maxBytes ?? 16 * 1024 * 1024 }
|
|
357
|
+
const since = Date.now() - days * 24 * 3600 * 1000
|
|
358
|
+
const cache = readCache(cachePath)
|
|
359
|
+
const sessions = []
|
|
360
|
+
const files = []
|
|
361
|
+
const skipped = []
|
|
362
|
+
let decompressed = 0
|
|
363
|
+
|
|
364
|
+
for (const item of collectSessionFiles(home, since, limits)) {
|
|
365
|
+
const key = item.file
|
|
366
|
+
const cached = cache.files[key]
|
|
367
|
+
if (cached && cached.mtimeMs === item.mtimeMs && cached.size === item.size && cached.compact) {
|
|
368
|
+
sessions.push(cached.compact)
|
|
369
|
+
files.push(cached.summary)
|
|
370
|
+
yield
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
const raw = decompress(fs.readFileSync(item.file))
|
|
375
|
+
decompressed += raw.length
|
|
376
|
+
const parsed = parseSessionJsonl(Buffer.from(raw).toString('utf8'))
|
|
377
|
+
const compact = compactSession(parsed)
|
|
378
|
+
const summary = {
|
|
379
|
+
sessionId: path.basename(path.dirname(item.file)),
|
|
380
|
+
mtimeMs: item.mtimeMs,
|
|
381
|
+
size: item.size,
|
|
382
|
+
steps: compact.stepCount,
|
|
383
|
+
compactions: compact.compactions,
|
|
384
|
+
duplicates: compact.duplicates,
|
|
385
|
+
}
|
|
386
|
+
cache.files[key] = { mtimeMs: item.mtimeMs, size: item.size, compact, summary }
|
|
387
|
+
sessions.push(compact)
|
|
388
|
+
files.push(summary)
|
|
389
|
+
} catch (error) {
|
|
390
|
+
skipped.push({ file: path.basename(path.dirname(item.file)), reason: String(error?.message ?? error) })
|
|
391
|
+
}
|
|
392
|
+
yield
|
|
393
|
+
}
|
|
394
|
+
writeCache(cachePath, cache)
|
|
395
|
+
|
|
396
|
+
const totals = summarize(sessions)
|
|
397
|
+
const deltas = []
|
|
398
|
+
for (const session of sessions) for (const delta of session.deltas) deltas.push(delta)
|
|
399
|
+
|
|
400
|
+
const policies = groupByPolicy(sessions, {
|
|
401
|
+
contextWindow: options.contextWindow,
|
|
402
|
+
thresholdRatio: options.thresholdRatio,
|
|
403
|
+
})
|
|
404
|
+
const currentPolicy = policies.find((policy) => policy.isCurrent) ?? null
|
|
405
|
+
const savings = estimateSavings(deltas, {
|
|
406
|
+
contextWindow: options.contextWindow,
|
|
407
|
+
thresholdRatio: options.thresholdRatio,
|
|
408
|
+
retainRatio: options.retainRatio,
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
generatedAt: new Date().toISOString(),
|
|
413
|
+
window: { days, since: new Date(since).toISOString() },
|
|
414
|
+
home,
|
|
415
|
+
totals,
|
|
416
|
+
policies,
|
|
417
|
+
// 归因:只有「观测到压缩触发点≈当前阈值」的会话,才算当前配置下的实测数据
|
|
418
|
+
coverage: {
|
|
419
|
+
currentThresholdRatio: options.thresholdRatio ?? null,
|
|
420
|
+
sessionsUnderCurrent: currentPolicy?.totals.sessions ?? 0,
|
|
421
|
+
stepsUnderCurrent: currentPolicy?.totals.steps ?? 0,
|
|
422
|
+
sharePct: totals.steps > 0 ? ((currentPolicy?.totals.steps ?? 0) / totals.steps) * 100 : 0,
|
|
423
|
+
isModelOnly: (currentPolicy?.totals.steps ?? 0) < 200,
|
|
424
|
+
},
|
|
425
|
+
savings,
|
|
426
|
+
files,
|
|
427
|
+
skipped,
|
|
428
|
+
decompressedBytes: decompressed,
|
|
429
|
+
privacy: 'local-only: 只读 ~/.dsh/sessions 下的日志并按路径缓存统计结果;不联网、不上报正文。',
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export function loadReport(options = {}) {
|
|
434
|
+
const iterator = collectReport(options)
|
|
435
|
+
for (;;) {
|
|
436
|
+
const result = iterator.next()
|
|
437
|
+
if (result.done) return result.value
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export async function loadReportAsync(options = {}) {
|
|
442
|
+
const iterator = collectReport(options)
|
|
443
|
+
for (;;) {
|
|
444
|
+
const result = iterator.next()
|
|
445
|
+
if (result.done) return result.value
|
|
446
|
+
await new Promise((resolve) => setImmediate(resolve))
|
|
447
|
+
}
|
|
448
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-context-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Compaction-threshold tuner for DeepSeek Harness: edits the agent preset (thresholdRatio / retainRatio) with provenance-safe patches, and shows measured usage. The context index now lives in dsh-token-trim.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"license": "MIT",
|
|
@@ -23,17 +23,20 @@
|
|
|
23
23
|
"node": ">=22"
|
|
24
24
|
},
|
|
25
25
|
"exports": {
|
|
26
|
-
".": "./
|
|
26
|
+
".": "./host.js",
|
|
27
|
+
"./index-host": "./index.js",
|
|
27
28
|
"./tuner": "./tuner.js",
|
|
28
29
|
"./client": "./client/panel.js",
|
|
29
30
|
"./icon": "./icon.svg"
|
|
30
31
|
},
|
|
31
32
|
"files": [
|
|
33
|
+
"host.js",
|
|
32
34
|
"index.js",
|
|
33
35
|
"tuner.js",
|
|
34
36
|
"lib/",
|
|
35
37
|
"bin/",
|
|
36
38
|
"client/",
|
|
39
|
+
"vendor/",
|
|
37
40
|
"cordis.patch.yml",
|
|
38
41
|
"icon.svg",
|
|
39
42
|
"README.md",
|
package/tuner.js
CHANGED
|
@@ -38,6 +38,62 @@ function statusText(kit) {
|
|
|
38
38
|
return lines.join('\n')
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
const fmtTokens = (value) => (value >= 1e9 ? `${(value / 1e9).toFixed(2)}B` : value >= 1e6 ? `${(value / 1e6).toFixed(1)}M` : `${Math.round(value / 1e3)}K`)
|
|
42
|
+
|
|
43
|
+
function reportText(report) {
|
|
44
|
+
const t = report.totals
|
|
45
|
+
const savings = report.savings
|
|
46
|
+
const coverage = report.coverage || {}
|
|
47
|
+
const lines = [
|
|
48
|
+
`窗口: 最近 ${report.window.days} 天(自 ${report.window.since.slice(0, 10)})`,
|
|
49
|
+
`会话 ${t.sessions} 个 / ${t.steps} 步 / 压缩 ${t.compactions} 次`,
|
|
50
|
+
'',
|
|
51
|
+
'按观测到的压缩触发点分期(历史里可能混着旧阈值跑出来的会话):',
|
|
52
|
+
]
|
|
53
|
+
for (const policy of report.policies || []) {
|
|
54
|
+
const label = policy.triggerTokens === null
|
|
55
|
+
? '未发生压缩(无法判断策略)'
|
|
56
|
+
: `触发点 ≈${k(policy.triggerTokens)}${policy.thresholdRatio === null ? '(贴着上限)' : `(${policy.thresholdRatio.toFixed(2)})`}`
|
|
57
|
+
lines.push(
|
|
58
|
+
` ${label}: ${policy.totals.sessions} 会话 / ${policy.totals.steps} 步 / 压缩 ${policy.totals.compactions} 次` +
|
|
59
|
+
(policy.isCurrent ? ' ← 当前阈值' : ''),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
lines.push(
|
|
63
|
+
'',
|
|
64
|
+
'实测用量(全部会话,来自日志里的 usage):',
|
|
65
|
+
` 缓存命中 ${fmtTokens(t.hit)} 未命中 ${fmtTokens(t.miss)} 输出 ${fmtTokens(t.out)}`,
|
|
66
|
+
` 未命中占成本 ${t.missSharePct.toFixed(1)}% 平均上下文 ${k(t.promptAvg)} 峰值 ${k(t.promptMax)}`,
|
|
67
|
+
` 成本(命中当量) ${fmtTokens(t.cost.total)}`,
|
|
68
|
+
)
|
|
69
|
+
if (coverage.currentThresholdRatio !== null && coverage.currentThresholdRatio !== undefined) {
|
|
70
|
+
lines.push(
|
|
71
|
+
` 其中按当前阈值(${coverage.currentThresholdRatio})跑的实测: ${coverage.sessionsUnderCurrent} 会话 / ${coverage.stepsUnderCurrent} 步(${coverage.sharePct.toFixed(1)}%)`,
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
if (savings) {
|
|
75
|
+
lines.push(
|
|
76
|
+
'',
|
|
77
|
+
`节省估算(非实测,模型): ${savings.savedPct.toFixed(1)}%`,
|
|
78
|
+
` 基线 ${savings.baseThresholdRatio}/${savings.baseRetainRatio} → 当前 ${savings.thresholdRatio}/${savings.retainRatio}`,
|
|
79
|
+
` 做法:把历史每步增量按两种阈值各回放一次(压缩 ${savings.baseEvents} → ${savings.currentEvents} 次),假设工作量与价格比不变`,
|
|
80
|
+
)
|
|
81
|
+
if (coverage.isModelOnly) {
|
|
82
|
+
lines.push(' ! 当前阈值下的实测样本还太少,这个百分比只能当方向,不能当结论。')
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
lines.push(
|
|
86
|
+
'',
|
|
87
|
+
`返工信号: 重复调用 ${t.duplicates} 次 / 不同调用 ${t.distinctCalls} 个(${t.duplicatePct.toFixed(1)}%)`,
|
|
88
|
+
' 重复高说明在重读/重跑——那是压缩压过头的第一信号。',
|
|
89
|
+
'',
|
|
90
|
+
`本次解压 ${(report.decompressedBytes / 1e6).toFixed(1)}MB;缓存 ${report.files.length} 个会话文件` +
|
|
91
|
+
(report.skipped.length > 0 ? `;跳过 ${report.skipped.length} 个` : ''),
|
|
92
|
+
report.privacy,
|
|
93
|
+
)
|
|
94
|
+
return lines.join('\n')
|
|
95
|
+
}
|
|
96
|
+
|
|
41
97
|
export function apply(ctx, config = {}) {
|
|
42
98
|
const settings = resolveConfig(config)
|
|
43
99
|
const kit = createKit(config)
|
|
@@ -65,14 +121,19 @@ export function apply(ctx, config = {}) {
|
|
|
65
121
|
'原文逐字节保留、只插入你配置的少量行,生成托管块写进 profile 的 cordis.patch.yml。' +
|
|
66
122
|
'因此不会像手写覆盖那样冻结上游——DSH 升级后会按上游重新生成。' +
|
|
67
123
|
'action=status 报告上游是否漂移;preview 只返回将要写入的文本;sync 真正落盘(需重启 DSH 生效);' +
|
|
68
|
-
'clear
|
|
124
|
+
'clear 移除托管块;report 统计最近几天在本机产生的真实用量与节省估算(只读本地会话日志)。' +
|
|
125
|
+
'若 profile 里已有手写的同 id 覆盖,sync 默认拒绝写入,需 force + mode="replace" 接管。',
|
|
69
126
|
parameters: {
|
|
70
127
|
type: 'object',
|
|
71
128
|
properties: {
|
|
72
129
|
action: {
|
|
73
130
|
type: 'string',
|
|
74
|
-
enum: ['status', 'preview', 'sync', 'clear'],
|
|
75
|
-
description: 'status(只读报告)/ preview(只返回文本)/ sync(写盘)/ clear
|
|
131
|
+
enum: ['status', 'preview', 'sync', 'clear', 'report'],
|
|
132
|
+
description: 'status(只读报告)/ preview(只返回文本)/ sync(写盘)/ clear(移除托管块)/ report(效果台账)',
|
|
133
|
+
},
|
|
134
|
+
days: {
|
|
135
|
+
type: 'number',
|
|
136
|
+
description: 'report 用:统计最近几天,默认 7',
|
|
76
137
|
},
|
|
77
138
|
force: {
|
|
78
139
|
type: 'boolean',
|
|
@@ -120,6 +181,9 @@ export function apply(ctx, config = {}) {
|
|
|
120
181
|
if (!result.changed) return '没有托管块,未改动文件。'
|
|
121
182
|
return `已移除 ${result.removed} 个托管块。备份: ${result.backup ?? '<无>'}\n重启 DSH 后恢复上游默认行为。`
|
|
122
183
|
}
|
|
184
|
+
if (action === 'report') {
|
|
185
|
+
return reportText(await kit.report({ days: Number(args.days) || 7 }))
|
|
186
|
+
}
|
|
123
187
|
throw new Error(`未知 action: ${action}`)
|
|
124
188
|
},
|
|
125
189
|
})
|