dsh-token-trim 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 LISHUANGXI1412
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.
package/README.md CHANGED
@@ -1,3 +1,124 @@
1
- # Temporary Holding Version
1
+ # dsh-token-trim
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Trim tool results **before they enter the model context** — structural summaries, head/tail caps, repeat
4
+ deltas, and an artifact spill you can search back. Ships with a context index and a **measured savings view**
5
+ inside the conversation.
6
+
7
+ > Not a proxy, not a middleware you have to trust: every rewrite is reproducible, the original is always on
8
+ > disk, and the model can read it back with `guard_query`.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ dsh plugin add dsh-token-trim
14
+ ```
15
+
16
+ Restart DSH. You get:
17
+
18
+ - `token-trim` (host): rewrites tool results on `tools/post-execute`;
19
+ - `token-trim-index` (host): a small system-prompt section listing recent artifacts and touched paths;
20
+ - **Savings** view tab next to Chat / Trajectory / Stats, plus the `guard_query` tool.
21
+
22
+ ## What it does
23
+
24
+ | rule | applies to | behaviour |
25
+ | --- | --- | --- |
26
+ | **structural summary** | JSON-shaped results from `cordis_inspect_*`, `plugin_manager`, `grep`, `skill`, `list_agents`, CUA window dumps | keeps the shape (keys, counts, ids), replaces verbose bodies with pointers |
27
+ | **head/tail cap** | long text from `pwsh`, `job_output`, … | keeps the first 72% and last 28% of the cap, inserts an artifact pointer + line count |
28
+ | **repeat delta** | any tool in `deltaTools` | if the same tool+args runs again and the diff is < 60% of the full text, send only the diff |
29
+ | **unchanged pointer** | same as above | sends `[unchanged · same as #N · artifact <id>]` |
30
+
31
+ Everything else is left alone: `edit`, `write`, `todo`, sub-agent messages, images, multi-block results,
32
+ reasoning, and any result under `minChars` (default 2000).
33
+
34
+ ## Measured on the author's machine
35
+
36
+ 19 days of real sessions, 279 rewrites, not a benchmark script:
37
+
38
+ | metric | value |
39
+ | --- | --- |
40
+ | characters rewritten | 3,493,915 → 876,875 (**−74.9%**) |
41
+ | tokens not re-sent | ≈ 800K |
42
+ | tools affected | `pwsh` 163, `cordis_inspect_query` 58, `plugin_manager` 20, `grep` 9, `read` 14, … |
43
+ | bill estimate (compaction tuning × this layer) | **−41.4%** (offline replay) |
44
+ | suspected rework deducted | 16 reruns, **−0.13%** |
45
+ | **net bill estimate** | **−41.3%** |
46
+
47
+ Read those two numbers carefully:
48
+
49
+ - **−74.9%** is the character cut **only over the results this plugin actually rewrote**. It is not the bill:
50
+ small results, the system prompt, tool definitions and reasoning are outside the hook.
51
+ - **−41.4%** is a bill-level model: earlier compaction (−31%) composed with this rule layer (−15% of the bill,
52
+ measured by replaying 1,518 real tool results), minus rework. It is a replay estimate, not an invoice.
53
+ - The Savings tab shows the same split: gross, suspected-rework deduction, net — so you can see when the
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.
60
+
61
+ ## Safety model
62
+
63
+ 1. **Fail open.** Any exception, any unexpected shape, any non-text result → the original result passes
64
+ through untouched. A bug cannot block a tool.
65
+ 2. **Single text block only.** Images, structured values and multi-block results are never rewritten.
66
+ 3. **Originals are always written first**, to `~/.dsh/token-trim/artifacts/<sha16>.txt`, and the replacement
67
+ carries that id.
68
+ 4. **`guard_query(id, pattern)`** searches inside the original (substring or `/regex/`, with context lines).
69
+ An artifact can be fetched **twice**; after that the tool returns the full text capped at 12K chars —
70
+ because a third fetch costs more context than it saves.
71
+ 5. **Audit trail.** Every transformation appends one line to `~/.dsh/token-trim/manifest.jsonl`
72
+ (tool, mode, chars in/out, artifact id, session id).
73
+ 6. **State is per agent.** Repeat-delta and unchanged detection are keyed by `session + tool + args`, so a
74
+ sub-agent never receives "unchanged" for a result it never saw.
75
+
76
+ ## What we tried and rejected (kept, so you don't repeat it)
77
+
78
+ - **Truncating `read` results.** Two real sessions, both truncated `read`s triggered a follow-up
79
+ `offset/limit` read: saved 2.3K tokens, cost ~50K in extra steps. `read` is now off by default
80
+ (`caps.read: 0`).
81
+ - **LLM summarization of logs.** Gold-recall 77.2% against the required 93.8%. Summaries lose the one line
82
+ you need. Logs are capped, never summarized.
83
+ - **Sending "unchanged" across agents** — a real bug, fixed by the agent key above.
84
+ - **Model-based trimming (a local 4B).** Projected 3–6 points of bill for a resident 4B service; shelved.
85
+
86
+ ## Configuration
87
+
88
+ Patch row `token-trim` in the DSH profile:
89
+
90
+ ```yaml
91
+ - id: token-trim
92
+ name: dsh-token-trim
93
+ config:
94
+ enabled: true
95
+ dryRun: false # record what would happen, rewrite nothing
96
+ minChars: 2000
97
+ maxFetches: 2
98
+ caps:
99
+ pwsh: 4000
100
+ cordis_inspect_query: 12000
101
+ read: 0 # 0 disables a cap
102
+ ```
103
+
104
+ `dryRun: true` is the honest way to try it: the Savings tab fills up, the context does not change.
105
+
106
+ ## Savings view
107
+
108
+ The **Savings** tab (next to Chat / Trajectory / Stats) shows, per window and per session:
109
+
110
+ - net bill estimate with the rework deduction spelled out;
111
+ - measured usage read from `dsh-context-kit`'s session cache when that plugin is installed
112
+ (hit ×1 / miss ×10 / output ×15);
113
+ - this plugin's own char cut, per tool and per session;
114
+ - the last transformations with artifact ids.
115
+
116
+ ## Companion package
117
+
118
+ `dsh-token-trim` owns the runtime savings: tool trimming, the context index, the view.
119
+ **`dsh-context-kit`** separately owns compaction-threshold tuning (Settings → Context). Install both for the
120
+ full effect; either works alone.
121
+
122
+ ## License
123
+
124
+ MIT
@@ -0,0 +1,503 @@
1
+ // dsh-token-trim 的会话视图(Web 客户端插件)。
2
+ // 位置:对话上方的 View 标签行(conversation.view),与「对话 / 轨迹 / 统计」并列。
3
+ // 数据:tokenTrim/status + /report + /bill + /replay(均无参数,筛选在客户端)。
4
+ window.__ModuleLoader__.load({
5
+ id: 'dsh-token-trim',
6
+ factory(require) {
7
+ const React = require('react')
8
+ const h = React.createElement
9
+
10
+ const EN = {
11
+ label: 'Savings',
12
+ title: 'Savings',
13
+ subtitle: 'Measured from real session logs: compaction tuning + tool-result trimming, minus rework.',
14
+ liveTitle: 'Bill saved (measured, net)',
15
+ eq: 'net =',
16
+ compaction: 'compaction tuning',
17
+ rules: 'tool trimming',
18
+ rework: 'suspected rework',
19
+ sample: 'samples',
20
+ aligned: 'aligned',
21
+ legacy: 'legacy rows (no session id)',
22
+ policy: 'policy',
23
+ modelTitle: 'fixed model (historical)',
24
+ modelNote: 'Config-level replay from 2026-11-15: compaction −31% × rules −15% ≈ −41%. Kept for comparison; trust the measured number above.',
25
+ charsTitle: 'Trimming detail',
26
+ charsScope: 'this window',
27
+ events: 'results rewritten',
28
+ chars: 'characters',
29
+ savedChars: 'saved',
30
+ tokens: 'est. tokens',
31
+ denom: 'The percentage is measured only over the results this plugin actually rewrote.',
32
+ usageTitle: 'Bill usage (measured)',
33
+ usageNote: 'From the dsh-context-kit session cache: hit ×1, miss ×10, output ×15.',
34
+ usageHit: 'cache hit',
35
+ usageMiss: 'miss',
36
+ usageOut: 'output',
37
+ usageCost: 'cost',
38
+ sessionsTitle: 'By session',
39
+ recentTitle: 'Recent rewrites',
40
+ refresh: 'Refresh',
41
+ busy: 'Loading…',
42
+ auto: 'Live',
43
+ paused: 'Paused',
44
+ updated: 'updated',
45
+ replayBtn: 'Run replay',
46
+ replayBusy: 'Replaying…',
47
+ runAll: 'All sessions',
48
+ runThis: 'This session',
49
+ empty: 'Nothing rewritten yet. token-guard only records results it actually changed.',
50
+ none: 'No manifest yet — the plugin is idle, or dryRun never wrote anything.',
51
+ dry: 'dryRun is ON: nothing is being rewritten.',
52
+ error: 'Error',
53
+ tool: 'tool',
54
+ count: 'n',
55
+ last: 'last',
56
+ focus: 'Focus',
57
+ clear: 'clear',
58
+ }
59
+
60
+ const ZH = {
61
+ label: '节省',
62
+ title: '账单节省',
63
+ subtitle: '来自真实会话日志:压缩阈值调优 + 工具结果治理,再扣掉返工。',
64
+ liveTitle: '账单净节省(实测)',
65
+ eq: '即',
66
+ compaction: '压缩阈值调优',
67
+ rules: '工具治理',
68
+ rework: '疑似返工',
69
+ sample: '样本',
70
+ aligned: '已对齐',
71
+ legacy: '条无会话 id(不计价)',
72
+ policy: '策略',
73
+ modelTitle: '固定模型(历史口径)',
74
+ modelNote: '2026-11-15 的语料级回放:压缩 −31% × 规则 −15% ≈ −41%。保留作对照,以上面的实测为准。',
75
+ charsTitle: '工具治理明细',
76
+ charsScope: '本窗口',
77
+ events: '条结果被改写',
78
+ chars: '字符',
79
+ savedChars: '省下',
80
+ tokens: '约合 token',
81
+ denom: '百分比的分母只是本插件实际改写过的工具结果。',
82
+ usageTitle: '账单用量(实测)',
83
+ usageNote: '来自 dsh-context-kit 的会话日志缓存:命中 ×1、未命中 ×10、输出 ×15。',
84
+ usageHit: '缓存命中',
85
+ usageMiss: '未命中',
86
+ usageOut: '输出',
87
+ usageCost: '成本',
88
+ sessionsTitle: '按会话',
89
+ recentTitle: '最近改写',
90
+ refresh: '刷新',
91
+ busy: '读取中…',
92
+ auto: '实时',
93
+ paused: '已暂停',
94
+ updated: '更新于',
95
+ replayBtn: '实测回放',
96
+ replayBusy: '回放中…',
97
+ runAll: '全部会话',
98
+ runThis: '本会话',
99
+ empty: '还没有被改写的工具结果。token-trim 只记录真正改过的结果。',
100
+ none: '还没有 manifest —— 插件空闲,或 dryRun 从未写过盘。',
101
+ dry: 'dryRun 已开启:内容没有被改写。',
102
+ error: '出错',
103
+ tool: '工具',
104
+ count: '次数',
105
+ last: '最近',
106
+ focus: '聚焦',
107
+ clear: '取消',
108
+ }
109
+
110
+ const CSS = [
111
+ '.tg_view { display: flex; flex-direction: column; gap: 12px; padding: 18px 22px 40px; max-width: 920px; box-sizing: border-box; }',
112
+ '.tg_head h2 { margin: 0 0 4px; font-size: 16px; }',
113
+ '.tg_head p { margin: 0; opacity: .7; font-size: 12px; }',
114
+ '.tg_row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }',
115
+ '.tg_card { border: 1px solid rgba(127,127,127,.25); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }',
116
+ '.tg_card.muted { opacity: .72; }',
117
+ '.tg_cardhead { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }',
118
+ '.tg_cardhead strong { font-size: 13px; }',
119
+ '.tg_hero { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }',
120
+ '.tg_hero .tg_big { font-size: 30px; }',
121
+ '.tg_seg { display: inline-flex; border: 1px solid rgba(127,127,127,.35); border-radius: 8px; overflow: hidden; }',
122
+ '.tg_seg button { border: 0; background: transparent; color: inherit; padding: 4px 10px; font-size: 12px; cursor: pointer; }',
123
+ '.tg_seg button.on { background: rgba(37,99,235,.18); font-weight: 600; }',
124
+ '.tg_btn { border: 1px solid rgba(127,127,127,.35); background: transparent; color: inherit; border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 12px; }',
125
+ '.tg_btn:disabled { opacity: .5; cursor: default; }',
126
+ '.tg_big { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }',
127
+ '.tg_meta { font-size: 11px; opacity: .65; }',
128
+ '.tg_note { font-size: 11px; opacity: .55; line-height: 1.5; }',
129
+ '.tg_warn { font-size: 12px; color: #d97706; }',
130
+ '.tg_err { font-size: 12px; color: #dc2626; }',
131
+ '.tg_table { width: 100%; border-collapse: collapse; font-size: 12px; }',
132
+ '.tg_table th, .tg_table td { text-align: left; padding: 3px 6px; border-bottom: 1px solid rgba(127,127,127,.16); font-variant-numeric: tabular-nums; }',
133
+ '.tg_table th { font-weight: 600; opacity: .7; }',
134
+ '.tg_table td.num, .tg_table th.num { text-align: right; }',
135
+ '.tg_table tr.clickable { cursor: pointer; }',
136
+ '.tg_table tr.clickable:hover td { background: rgba(127,127,127,.08); }',
137
+ '.tg_table tr.on td { background: rgba(37,99,235,.12); }',
138
+ '.tg_ev { display: flex; gap: 8px; align-items: baseline; font-size: 12px; padding: 2px 0; border-bottom: 1px solid rgba(127,127,127,.1); }',
139
+ '.tg_ev .t { min-width: 72px; opacity: .6; font-variant-numeric: tabular-nums; }',
140
+ '.tg_ev .tg_target { opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1 1 120px; }',
141
+ '.tg_chip { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: rgba(127,127,127,.18); }',
142
+ '.tg_chip.warn { background: rgba(217,119,6,.18); }',
143
+ ].join('\n')
144
+
145
+ const fmtBig = (value) => {
146
+ if (!Number.isFinite(value)) return '—'
147
+ if (Math.abs(value) >= 1e9) return (value / 1e9).toFixed(2) + 'B'
148
+ if (Math.abs(value) >= 1e6) return (value / 1e6).toFixed(1) + 'M'
149
+ if (Math.abs(value) >= 1e3) return Math.round(value / 1e3) + 'K'
150
+ return String(Math.round(value))
151
+ }
152
+ const pct = (value) => (Number.isFinite(value) ? value.toFixed(1) + '%' : '—')
153
+ const timeOf = (ts) => {
154
+ if (!ts) return '—'
155
+ const d = new Date(ts)
156
+ const pad = (n) => String(n).padStart(2, '0')
157
+ return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes())
158
+ }
159
+ const shortId = (id) => {
160
+ const s = String(id || '')
161
+ return s.length > 10 ? s.slice(0, 8) : s
162
+ }
163
+ const meta = (value) => h('div', { className: 'tg_meta' }, value)
164
+ const TOK_PER_CHAR = 0.306
165
+
166
+ const cache = { at: 0, data: null, loading: null }
167
+ function loadReport(api, force) {
168
+ if (!force && cache.data && Date.now() - cache.at < 15000) return Promise.resolve(cache.data)
169
+ if (cache.loading) return cache.loading
170
+ cache.loading = api.report()
171
+ .then(function (data) { cache.at = Date.now(); cache.data = data; cache.loading = null; return data })
172
+ .catch(function (cause) { cache.loading = null; throw cause })
173
+ return cache.loading
174
+ }
175
+
176
+ function makeT(ctx) {
177
+ try {
178
+ ctx.locale.register('token-trim', { en: EN, zh: ZH })
179
+ } catch {}
180
+ let bound
181
+ try {
182
+ bound = ctx.locale.bind('token-trim')
183
+ } catch {}
184
+ return function t(key, fallback) {
185
+ try {
186
+ if (typeof bound === 'function') {
187
+ const value = bound(key)
188
+ if (typeof value === 'string' && value) return value
189
+ } else if (bound && typeof bound[key] === 'string') {
190
+ return bound[key]
191
+ }
192
+ } catch {}
193
+ let id = 'en'
194
+ try {
195
+ id = ctx.locale.getLocale().id
196
+ } catch {}
197
+ const dict = id === 'zh' ? ZH : EN
198
+ return dict[key] || fallback || key
199
+ }
200
+ }
201
+
202
+ function makeApi(ctx) {
203
+ function call(method, args) {
204
+ return ctx.connection.rpc.call('/api', 'tokenTrim/' + method, { args: args || {} }, undefined).then(function (response) {
205
+ if (response && response.ok) return response.value
206
+ const message = response && response.error ? response.error.message : 'token trim remote call failed'
207
+ const error = new Error(message)
208
+ error.code = response && response.error ? response.error.code : 'REMOTE_FAILED'
209
+ throw error
210
+ })
211
+ }
212
+ return {
213
+ report: () => call('report', {}),
214
+ status: () => call('status', {}),
215
+ bill: () => call('bill', {}),
216
+ replay: () => call('replay', {}),
217
+ }
218
+ }
219
+
220
+ function viewModel(data, focus, days) {
221
+ const cutoff = days > 0 ? Date.now() - days * 86400000 : 0
222
+ const sessions = (data.sessions || []).filter(function (s) { return !cutoff || s.last >= cutoff })
223
+ const selected = focus ? sessions.find(function (s) { return s.id === focus }) || null : null
224
+ const pool = selected ? [selected] : sessions
225
+ const totals = { events: 0, orig: 0, vis: 0, saved: 0 }
226
+ const toolMap = new Map()
227
+ for (const s of pool) {
228
+ totals.events += s.events
229
+ totals.orig += s.orig
230
+ totals.vis += s.vis
231
+ totals.saved += s.saved
232
+ for (const tool of s.tools || []) {
233
+ const cur = toolMap.get(tool.name) || { name: tool.name, events: 0, saved: 0 }
234
+ cur.events += tool.events
235
+ cur.saved += tool.saved
236
+ toolMap.set(tool.name, cur)
237
+ }
238
+ }
239
+ totals.cutPct = totals.orig ? Number(((1 - totals.vis / totals.orig) * 100).toFixed(1)) : 0
240
+ totals.tokens = Math.round(totals.saved * TOK_PER_CHAR)
241
+ const events = (data.events || []).filter(function (e) {
242
+ if (cutoff && e.ts < cutoff) return false
243
+ return selected ? e.session === selected.id : true
244
+ })
245
+ return {
246
+ sessions,
247
+ selected,
248
+ totals,
249
+ tools: [...toolMap.values()].sort(function (a, b) { return b.saved - a.saved }),
250
+ events,
251
+ }
252
+ }
253
+
254
+ function Seg(props) {
255
+ const options = [
256
+ { value: 0, label: props.t('allTime') },
257
+ { value: 7, label: props.t('days7') },
258
+ { value: 30, label: props.t('days30') },
259
+ ]
260
+ return h('div', { className: 'tg_seg' }, options.map((opt) =>
261
+ h('button', {
262
+ key: opt.value,
263
+ type: 'button',
264
+ className: props.value === opt.value ? 'on' : '',
265
+ onClick: function () { props.onChange(opt.value) },
266
+ }, opt.label),
267
+ ))
268
+ }
269
+
270
+ function Panel(props) {
271
+ const t = props.t
272
+ const api = props.api
273
+ const sessionId = props.sessionId || ''
274
+ const [data, setData] = React.useState(null)
275
+ const [bill, setBill] = React.useState(null)
276
+ const [replay, setReplay] = React.useState(null)
277
+ const [days, setDays] = React.useState(0)
278
+ const [focus, setFocus] = React.useState('')
279
+ const [busy, setBusy] = React.useState(false)
280
+ const [replayBusy, setReplayBusy] = React.useState(false)
281
+ const [error, setError] = React.useState('')
282
+ const [status, setStatus] = React.useState(null)
283
+ const [auto, setAuto] = React.useState(true)
284
+ const [updatedAt, setUpdatedAt] = React.useState(0)
285
+ const lastRecords = React.useRef(-1)
286
+
287
+ const load = React.useCallback(function (force, silent) {
288
+ if (!silent) setBusy(true)
289
+ return Promise.all([
290
+ loadReport(api, force),
291
+ api.status().catch(function () { return null }),
292
+ api.bill().catch(function () { return null }),
293
+ ])
294
+ .then(function (results) {
295
+ setData(results[0])
296
+ if (results[1]) setStatus(results[1])
297
+ if (results[2]) setBill(results[2])
298
+ setUpdatedAt(Date.now())
299
+ })
300
+ .catch(function (cause) { setError(String(cause && cause.message ? cause.message : cause)) })
301
+ .then(function () { if (!silent) setBusy(false) })
302
+ }, [api])
303
+
304
+ const runReplay = React.useCallback(function (silent) {
305
+ if (!silent) setReplayBusy(true)
306
+ return api.replay()
307
+ .then(function (value) { setReplay(value) })
308
+ .catch(function (cause) { setError(String(cause && cause.message ? cause.message : cause)) })
309
+ .then(function () { setReplayBusy(false) })
310
+ }, [api])
311
+
312
+ React.useEffect(function () { load(false, false) }, [load])
313
+
314
+ // 实时:视图可见时每 5 秒静默重读;切回页面立即补一次。
315
+ React.useEffect(function () {
316
+ if (!auto) return undefined
317
+ const tick = function () {
318
+ if (typeof document !== 'undefined' && document.hidden) return
319
+ load(true, true).catch(function () {})
320
+ }
321
+ const timer = setInterval(tick, 5000)
322
+ const onVisible = function () {
323
+ if (typeof document !== 'undefined' && !document.hidden) load(true, true).catch(function () {})
324
+ }
325
+ if (typeof document !== 'undefined') document.addEventListener('visibilitychange', onVisible)
326
+ return function () {
327
+ clearInterval(timer)
328
+ if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', onVisible)
329
+ }
330
+ }, [auto, load])
331
+
332
+ // 每次出现新的改写(manifest 行数变化)就自动跑一次实测回放;解析结果有缓存。
333
+ React.useEffect(function () {
334
+ if (!data) return
335
+ 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])
340
+
341
+ const model = data ? viewModel(data, focus, days) : null
342
+ const rows = []
343
+
344
+ if (error) rows.push(h('div', { className: 'tg_err' }, t('error') + ': ' + error))
345
+ if (status && status.dryRun) rows.push(h('div', { className: 'tg_warn' }, t('dry')))
346
+ if (data && !data.available) rows.push(meta(t('none')))
347
+
348
+ if (model) {
349
+ const rt = replay && replay.totals ? replay.totals : null
350
+ const netModel = bill && Number.isFinite(bill.netPct) ? bill.netPct : 41.3
351
+ const reworkPct = bill && bill.rework && Number.isFinite(bill.rework.pctOfBaseline) ? bill.rework.pctOfBaseline : 0
352
+ const netMeasured = rt ? rt.totalPct - reworkPct : 0
353
+
354
+ // 1) 实测净节省(压缩阈值 + 工具治理 − 疑似返工),放首位
355
+ rows.push(h('div', { className: 'tg_card' },
356
+ h('div', { className: 'tg_hero' },
357
+ h('span', { className: 'tg_big' }, rt ? '−' + pct(netMeasured) : '—'),
358
+ h('span', { className: 'tg_meta' }, t('liveTitle'))),
359
+ h('div', { className: 'tg_row' },
360
+ meta(rt ? t('compaction') + ' +' + pct(rt.compactionPct) : t('compaction') + ' …'),
361
+ meta(rt ? t('rules') + ' +' + pct(rt.rulePct) : t('rules') + ' …'),
362
+ meta(t('rework') + ' −' + reworkPct.toFixed(2) + '%')),
363
+ h('div', { className: 'tg_row' },
364
+ meta(rt ? t('eq') + ' +' + pct(rt.totalPct) + ' − ' + reworkPct.toFixed(2) + '%' : ''),
365
+ rt ? meta(t('sample') + ' ' + rt.sessions + ' · ' + t('aligned') + ' ' + rt.aligned + '/' + rt.events) : null,
366
+ rt && rt.legacy ? meta(rt.legacy + ' ' + t('legacy')) : null,
367
+ replay.policy ? meta(t('policy') + ' ' + replay.policy.thresholdRatio + ' / ' + replay.policy.retainRatio) : null),
368
+ h('div', { className: 'tg_note' },
369
+ rt
370
+ ? '反事实回放:压缩按当前策略 vs 出厂 0.8/0.16;工具治理按每条改写结果的实际存活步数计费。'
371
+ : '点「实测回放」按真实会话日志计算。'),
372
+ replayBusy ? h('div', { className: 'tg_note' }, t('replayBusy')) : null))
373
+
374
+ // 2) 固定模型(次要,明确标注)
375
+ rows.push(h('div', { className: 'tg_card muted' },
376
+ h('div', { className: 'tg_cardhead' },
377
+ h('strong', null, t('modelTitle')),
378
+ meta('−' + netModel.toFixed(1) + '%')),
379
+ h('div', { className: 'tg_note' }, t('modelNote'))))
380
+
381
+ // 3) 工具治理明细(字符口径)
382
+ rows.push(h('div', { className: 'tg_card' },
383
+ h('div', { className: 'tg_cardhead' },
384
+ h('strong', null, t('charsTitle')),
385
+ meta(t('charsScope') + ' · ' + model.totals.events + ' ' + t('events'))),
386
+ h('div', { className: 'tg_row' },
387
+ h('span', { className: 'tg_big' }, '−' + pct(model.totals.cutPct)),
388
+ meta(fmtBig(model.totals.orig) + ' → ' + fmtBig(model.totals.vis) + ' ' + t('chars')),
389
+ meta(t('savedChars') + ' ' + fmtBig(model.totals.saved) + ' ≈ ' + fmtBig(model.totals.tokens) + ' tok')),
390
+ h('table', { className: 'tg_table' },
391
+ h('thead', null, h('tr', null,
392
+ h('th', null, t('tool')),
393
+ h('th', { className: 'num' }, t('count')),
394
+ h('th', { className: 'num' }, t('savedChars')))),
395
+ h('tbody', null, model.tools.slice(0, 6).map((tool) =>
396
+ h('tr', { key: tool.name },
397
+ h('td', null, tool.name),
398
+ h('td', { className: 'num' }, tool.events),
399
+ h('td', { className: 'num' }, fmtBig(tool.saved)))))),
400
+ h('div', { className: 'tg_note' }, t('denom'))))
401
+
402
+ // 4) 账单用量
403
+ if (bill && bill.available && bill.totals) {
404
+ const bt = bill.totals
405
+ rows.push(h('div', { className: 'tg_card' },
406
+ h('div', { className: 'tg_cardhead' },
407
+ h('strong', null, t('usageTitle')),
408
+ meta(bt.sessions + ' ' + t('sample') + ' · ' + fmtBig(bt.steps) + ' steps')),
409
+ h('div', { className: 'tg_row' },
410
+ meta(t('usageHit') + ' ' + fmtBig(bt.hit)),
411
+ meta(t('usageMiss') + ' ' + fmtBig(bt.miss)),
412
+ meta(t('usageOut') + ' ' + fmtBig(bt.out)),
413
+ meta(t('usageCost') + ' ' + fmtBig(bt.cost))),
414
+ h('div', { className: 'tg_note' }, t('usageNote')),
415
+ bill.updatedAt ? meta(t('updated') + ' ' + timeOf(bill.updatedAt)) : null))
416
+ }
417
+
418
+ // 5) 按会话
419
+ rows.push(h('div', { className: 'tg_card' },
420
+ h('div', { className: 'tg_cardhead' },
421
+ h('strong', null, t('sessionsTitle')),
422
+ focus ? h('button', { className: 'tg_btn', type: 'button', onClick: function () { setFocus('') } }, t('clear')) : null,
423
+ !focus && sessionId ? h('button', { className: 'tg_btn', type: 'button', onClick: function () { setFocus(sessionId) } }, t('runThis')) : null),
424
+ h('table', { className: 'tg_table' },
425
+ h('thead', null, h('tr', null,
426
+ h('th', null, ''),
427
+ h('th', { className: 'num' }, t('count')),
428
+ h('th', { className: 'num' }, t('savedChars')),
429
+ h('th', null, t('last')))),
430
+ h('tbody', null, model.sessions.slice(0, 10).map((row) =>
431
+ h('tr', {
432
+ key: row.id,
433
+ className: 'clickable' + (model.selected && model.selected.id === row.id ? ' on' : ''),
434
+ onClick: function () { setFocus(model.selected && model.selected.id === row.id ? '' : row.id) },
435
+ },
436
+ h('td', null, row.id === 'legacy' ? 'legacy' : shortId(row.id)),
437
+ h('td', { className: 'num' }, row.events),
438
+ h('td', { className: 'num' }, fmtBig(row.saved)),
439
+ h('td', { className: 'tg_meta' }, timeOf(row.last))))))))
440
+
441
+ // 6) 最近改写
442
+ rows.push(h('div', { className: 'tg_card' },
443
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('recentTitle'))),
444
+ ...model.events.slice(0, 10).map(function (row, index) {
445
+ return h('div', { className: 'tg_ev', key: index },
446
+ h('span', { className: 't' }, timeOf(row.ts).slice(5)),
447
+ h('span', { className: 'tg_chip' }, row.tool + ' · ' + row.mode),
448
+ h('span', { className: 'tg_meta' }, fmtBig(row.orig) + '→' + fmtBig(row.vis)),
449
+ h('span', { className: 'tg_target' }, row.target || ''),
450
+ row.dryRun ? h('span', { className: 'tg_chip warn' }, 'dry') : null)
451
+ })))
452
+
453
+ if (status) {
454
+ const where = []
455
+ if (status.artifactDir) where.push(meta(status.artifactDir))
456
+ if (status.manifest) where.push(meta(status.manifest))
457
+ if (where.length) rows.push(h('div', { className: 'tg_card muted' }, ...where))
458
+ }
459
+ }
460
+
461
+ return h('div', { className: 'tg_view' },
462
+ h('div', { className: 'tg_head' },
463
+ h('h2', null, t('title')),
464
+ h('p', null, t('subtitle'))),
465
+ h('div', { className: 'tg_row' },
466
+ h(Seg, { t: t, value: days, onChange: setDays }),
467
+ h('button', { className: 'tg_btn', type: 'button', disabled: busy, onClick: function () { return load(true, false) } },
468
+ busy ? t('busy') : t('refresh')),
469
+ h('button', { className: 'tg_btn', type: 'button', disabled: replayBusy, onClick: function () { return runReplay(false) } },
470
+ replayBusy ? t('replayBusy') : t('replayBtn')),
471
+ h('button', { className: 'tg_btn', type: 'button', onClick: function () { setAuto(function (value) { return !value }) } },
472
+ auto ? t('auto') : t('paused')),
473
+ updatedAt ? h('span', { className: 'tg_meta' }, t('updated') + ' ' + new Date(updatedAt).toLocaleTimeString()) : null),
474
+ ...rows)
475
+ }
476
+
477
+ return {
478
+ inject: ['slots', 'connection', 'locale'],
479
+ apply(ctx) {
480
+ const style = document.createElement('style')
481
+ style.dataset.plugin = 'dsh-token-trim'
482
+ style.textContent = CSS
483
+ document.head.appendChild(style)
484
+ ctx.effect(function () {
485
+ return function () { style.remove() }
486
+ })
487
+
488
+ const t = makeT(ctx)
489
+ const api = makeApi(ctx)
490
+
491
+ ctx.slots.inject('conversation.view', function () {
492
+ return ctx.slots.register({
493
+ name: 'conversation.view',
494
+ id: 'token-trim',
495
+ order: 50,
496
+ label: function () { return t('label', 'Savings') },
497
+ inject: function () { return { api: api, t: t } },
498
+ }, Panel)
499
+ })
500
+ },
501
+ }
502
+ },
503
+ })
@@ -0,0 +1,23 @@
1
+ # dsh-token-trim: 一个包两个 Host 入口 + 一个会话视图(视图由 package.json 的 dsh.client 声明)。
2
+ # - token-trim:工具结果进上下文前的确定性治理(摘要 / 截断 / 重跑差异 / 原文落盘 / guard_query 取回)
3
+ # - token-trim-index:上下文索引(在 system prompt 里给一段「有什么、在哪」的指针)
4
+ - insert:
5
+ - id: token-trim
6
+ name: dsh-token-trim
7
+ config:
8
+ enabled: true
9
+ dryRun: false
10
+ minChars: 2000
11
+ maxFetches: 2
12
+ - id: token-trim-index
13
+ name: dsh-token-trim/index-host
14
+ config:
15
+ enabled: true
16
+ injectMode: section
17
+ sectionName: index:index-page
18
+ sectionOrder: 10500
19
+ maxTokens: 1000
20
+ pathLimit: 40
21
+ artifactLimit: 20
22
+ targetChars: 48
23
+ manifestLines: 500