dsh-token-trim 0.1.1 → 0.1.3

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 CHANGED
@@ -52,11 +52,12 @@ Read those two numbers carefully:
52
52
  measured by replaying 1,518 real tool results), minus rework. It is a replay estimate, not an invoice.
53
53
  - The Savings tab shows the same split: gross, suspected-rework deduction, net — so you can see when the
54
54
  plugin starts to hurt instead of help.
55
- - **Measured replay (button in the tab).** Reads the real session logs and charges each rewrite once at the
56
- miss price and once per later request until the next compaction — a counterfactual bill. On the author's
57
- current session that measured **−7.8%** of that session's bill (34 aligned rewrites); the −41.4% figure is
58
- the config-level model, which also includes compaction tuning. Both are shown, and the model number is
59
- labelled as a model. Rewrites from before session ids existed are counted but not priced.
55
+ - **Measured replay (in the tab).** Reads the real session logs and charges each rewrite once at the miss
56
+ price and once per later request until the next compaction; the threshold side replays the measured
57
+ per-step deltas under the current policy (`0.4 / 0.12`) and the shipped default (`0.8 / 0.16`). On the
58
+ author's session this came out at **−30.6% compaction + −5.3% trimming ≈ −35.8% net**; the −41.3% figure
59
+ is the older corpus-level model kept as a comparison. Rewrites from before session ids existed are counted
60
+ but not priced, and the tab says so.
60
61
 
61
62
  ## Safety model
62
63
 
package/client/panel.js CHANGED
@@ -19,6 +19,7 @@ window.__ModuleLoader__.load({
19
19
  sample: 'samples',
20
20
  aligned: 'aligned',
21
21
  legacy: 'legacy rows (no session id)',
22
+ stale: 'new rewrites pending · auto-refresh within 60s',
22
23
  policy: 'policy',
23
24
  modelTitle: 'fixed model (historical)',
24
25
  modelNote: 'Config-level replay from 2026-11-15: compaction −31% × rules −15% ≈ −41%. Kept for comparison; trust the measured number above.',
@@ -69,6 +70,7 @@ window.__ModuleLoader__.load({
69
70
  sample: '样本',
70
71
  aligned: '已对齐',
71
72
  legacy: '条无会话 id(不计价)',
73
+ stale: '有新改写待计入 · 60 秒内自动更新',
72
74
  policy: '策略',
73
75
  modelTitle: '固定模型(历史口径)',
74
76
  modelNote: '2026-11-15 的语料级回放:压缩 −31% × 规则 −15% ≈ −41%。保留作对照,以上面的实测为准。',
@@ -282,7 +284,6 @@ window.__ModuleLoader__.load({
282
284
  const [status, setStatus] = React.useState(null)
283
285
  const [auto, setAuto] = React.useState(true)
284
286
  const [updatedAt, setUpdatedAt] = React.useState(0)
285
- const lastRecords = React.useRef(-1)
286
287
 
287
288
  const load = React.useCallback(function (force, silent) {
288
289
  if (!silent) setBusy(true)
@@ -329,14 +330,19 @@ window.__ModuleLoader__.load({
329
330
  }
330
331
  }, [auto, load])
331
332
 
332
- // 每次出现新的改写(manifest 行数变化)就自动跑一次实测回放;解析结果有缓存。
333
+ // 回放很贵(解压日志在 worker 里也要 2–5 秒):首次进入自动跑一次,之后最多 60 秒一次,
334
+ // 且同一条改写批次不重复跑。忙碌时跳过,避免排队。
335
+ const replayMeta = React.useRef({ at: 0, records: -1 })
333
336
  React.useEffect(function () {
334
- if (!data) return
337
+ if (!data || !auto || replayBusy) return
335
338
  const records = data.records || 0
336
- if (records === lastRecords.current) return
337
- lastRecords.current = records
338
- if (auto || !replay) runReplay(true)
339
- }, [data, auto, replay, runReplay])
339
+ const first = replay === null
340
+ const changed = records !== replayMeta.current.records
341
+ const elapsed = Date.now() - replayMeta.current.at
342
+ if (!first && !(changed && elapsed > 60000)) return
343
+ replayMeta.current = { at: Date.now(), records }
344
+ runReplay(true)
345
+ }, [data, auto, replay, replayBusy, runReplay])
340
346
 
341
347
  const model = data ? viewModel(data, focus, days) : null
342
348
  const rows = []
@@ -364,7 +370,8 @@ window.__ModuleLoader__.load({
364
370
  meta(rt ? t('eq') + ' +' + pct(rt.totalPct) + ' − ' + reworkPct.toFixed(2) + '%' : ''),
365
371
  rt ? meta(t('sample') + ' ' + rt.sessions + ' · ' + t('aligned') + ' ' + rt.aligned + '/' + rt.events) : null,
366
372
  rt && rt.legacy ? meta(rt.legacy + ' ' + t('legacy')) : null,
367
- replay.policy ? meta(t('policy') + ' ' + replay.policy.thresholdRatio + ' / ' + replay.policy.retainRatio) : null),
373
+ replay.policy ? meta(t('policy') + ' ' + replay.policy.thresholdRatio + ' / ' + replay.policy.retainRatio) : null,
374
+ rt && data && data.records !== replayMeta.current.records && !replayBusy ? meta(t('stale')) : null),
368
375
  h('div', { className: 'tg_note' },
369
376
  rt
370
377
  ? '反事实回放:压缩按当前策略 vs 出厂 0.8/0.16;工具治理按每条改写结果的实际存活步数计费。'
package/lib/remote.mjs CHANGED
@@ -9,11 +9,50 @@ import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
9
9
  import { createRequire } from 'node:module'
10
10
  import { homedir } from 'node:os'
11
11
  import path from 'node:path'
12
- import { replayAll } from './replay.mjs'
12
+ import { Worker } from 'node:worker_threads'
13
13
 
14
14
  const require = createRequire(import.meta.url)
15
15
  const { buildReport, buildBill } = require('./report.cjs')
16
16
 
17
+ // 回放常驻 worker:解压 10MB 日志要 4–5 秒,放在主线程会卡住整个 UI 的 RPC。
18
+ let replayWorker = null
19
+ let replaySeq = 0
20
+ const replayWaiters = new Map()
21
+
22
+ function getReplayWorker() {
23
+ if (replayWorker) return replayWorker
24
+ replayWorker = new Worker(new URL('./replay-worker.mjs', import.meta.url))
25
+ replayWorker.on('message', (message) => {
26
+ const waiter = replayWaiters.get(message && message.id)
27
+ if (!waiter) return
28
+ replayWaiters.delete(message.id)
29
+ if (message.error) waiter.reject(new Error(message.error))
30
+ else waiter.resolve(message.result)
31
+ })
32
+ replayWorker.on('error', (error) => {
33
+ for (const waiter of replayWaiters.values()) waiter.reject(error)
34
+ replayWaiters.clear()
35
+ replayWorker = null
36
+ })
37
+ // 不要让 worker 拖住进程退出
38
+ if (typeof replayWorker.unref === 'function') replayWorker.unref()
39
+ return replayWorker
40
+ }
41
+
42
+ function replayInWorker(manifest, sessionsRoot) {
43
+ const id = ++replaySeq
44
+ return new Promise((resolve, reject) => {
45
+ replayWaiters.set(id, { resolve, reject })
46
+ try {
47
+ getReplayWorker().postMessage({ id, manifest, sessionsRoot })
48
+ } catch (error) {
49
+ replayWaiters.delete(id)
50
+ replayWorker = null
51
+ reject(error)
52
+ }
53
+ })
54
+ }
55
+
17
56
  export function markRemoteMethod(serviceClass, method, exportName = method) {
18
57
  const instance = Object.create(serviceClass.prototype)
19
58
  const decorate = Remote(exportName)
@@ -70,14 +109,11 @@ export class TokenTrimRemoteService extends TypertRemoteService {
70
109
  }
71
110
 
72
111
  /**
73
- * 反事实回放(A 方案,按需触发):读真实会话日志,逐 step 计算"没有本次改写时"的账单。
74
- * 只对齐有 session id 的改写;legacy 行只计数。耗时与日志量相关(本机约 2s/400 步)。
112
+ * 反事实回放(按需触发):读真实会话日志,逐 step 计算"没有本次改写时"的账单。
113
+ * 只对齐有 session id 的改写;legacy 行只计数。在 worker 里跑,不阻塞 UI。
75
114
  */
76
115
  async replay() {
77
- return replayAll({
78
- manifest: this.manifest,
79
- sessionsRoot: path.join(homedir(), '.dsh', 'sessions'),
80
- })
116
+ return replayInWorker(this.manifest, path.join(homedir(), '.dsh', 'sessions'))
81
117
  }
82
118
  }
83
119
 
@@ -0,0 +1,15 @@
1
+ // 回放 worker:把「解压会话日志 + 逐 step 计费」移出宿主主线程。
2
+ // 主线程每 2–5 秒会处理 UI/RPC,同步解压 10MB 日志会把它卡住,所以这里单独一个 worker,
3
+ // 并保持常驻,让解析缓存(parseCache)在多次回放之间复用。
4
+ import { parentPort } from 'node:worker_threads'
5
+ import { replayAll } from './replay.mjs'
6
+
7
+ parentPort.on('message', (message) => {
8
+ const { id, manifest, sessionsRoot } = message || {}
9
+ try {
10
+ const result = replayAll({ manifest, sessionsRoot })
11
+ parentPort.postMessage({ id, result })
12
+ } catch (error) {
13
+ parentPort.postMessage({ id, error: String(error && error.message ? error.message : error) })
14
+ }
15
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-token-trim",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
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",