dsh-compaction-adaptive 0.1.2 → 0.2.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 +58 -3
- package/client.js +128 -0
- package/cordis.patch.yml +10 -14
- package/insight-host.js +46 -0
- package/lib/index.js +130 -1
- package/lib/insight.js +216 -0
- package/lib/projection.js +187 -0
- package/package.json +22 -3
package/README.md
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Adaptive context compaction for DeepSeek Harness.
|
|
4
4
|
|
|
5
|
+
[](https://www.npmjs.com/package/dsh-compaction-adaptive)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
5
8
|
The plugin is a compaction backend (`ctx.compaction`) built on
|
|
6
9
|
`@deepseek-ai/dsh-compaction-basic`: it keeps that backend's durable
|
|
7
10
|
compaction protocol and replaces only its numeric policy. Before every
|
|
@@ -47,15 +50,26 @@ dsh plugin --profile desktop add dsh-compaction-adaptive
|
|
|
47
50
|
dsh plugin --profile desktop add https://github.com/ArtomYuan/dsh-compaction-adaptive
|
|
48
51
|
|
|
49
52
|
# pinned to a tag, for a reproducible profile
|
|
50
|
-
dsh plugin --profile desktop add https://codeload.github.com/ArtomYuan/dsh-compaction-adaptive/tar.gz/refs/tags/v0.
|
|
53
|
+
dsh plugin --profile desktop add https://codeload.github.com/ArtomYuan/dsh-compaction-adaptive/tar.gz/refs/tags/v0.2.0
|
|
51
54
|
|
|
52
|
-
#
|
|
53
|
-
|
|
55
|
+
# a local checkout, while developing: pack it, then install the tarball
|
|
56
|
+
pnpm pack --pack-destination .dist
|
|
57
|
+
dsh plugin --profile desktop add .dist/dsh-compaction-adaptive-0.2.0.tgz
|
|
54
58
|
```
|
|
55
59
|
|
|
56
60
|
`dsh plugin` forwards to the bundled pnpm, so a git install is pinned to a commit
|
|
57
61
|
in the profile lockfile; `dsh plugin --profile desktop update` moves it forward.
|
|
58
62
|
|
|
63
|
+
> **Install from a package, never as a linked directory.** A `link:` install
|
|
64
|
+
> (adding the checkout path, `link:…`, or a directory that pnpm links) does not
|
|
65
|
+
> install the package's dependencies. The plugin imports
|
|
66
|
+
> `@deepseek-ai/dsh-compaction-basic`, `@deepseek-ai/dsh-llm` and
|
|
67
|
+
> `@deepseek-ai/schemastery` from the Harness, so a linked install leaves those
|
|
68
|
+
> imports unresolvable; the preset row then fails to activate, the realm has no
|
|
69
|
+
> `compaction` service, and **every session fails to initialize** — sessions
|
|
70
|
+
> vanish from the sidebar instead of reporting an error. Pack a tarball or install
|
|
71
|
+
> from the registry.
|
|
72
|
+
|
|
59
73
|
The same URLs work in **Plugins → Add plugin**, where picking a local directory is
|
|
60
74
|
also accepted.
|
|
61
75
|
|
|
@@ -116,6 +130,8 @@ Checks run without a Harness process:
|
|
|
116
130
|
node test/policy.test.mjs # threshold / tail planning
|
|
117
131
|
node test/chunking.test.mjs # packing, projection, prompts, concurrency
|
|
118
132
|
node test/adaptive-chunking.test.mjs # model-derived budgets, shrink/recover
|
|
133
|
+
node test/insight.test.mjs # panel projections: budget card, chunk card, transactions
|
|
134
|
+
node test/projection.test.mjs # session projection: fold, wire payload, schemas
|
|
119
135
|
```
|
|
120
136
|
|
|
121
137
|
At runtime:
|
|
@@ -126,6 +142,21 @@ At runtime:
|
|
|
126
142
|
[compaction-adaptive] chunked summary (20043 tokens > 12822 budget): 2 parts <= 8243 tokens / 700000 B each, concurrency 2, capacity scale 100%
|
|
127
143
|
```
|
|
128
144
|
|
|
145
|
+
## Insight panel
|
|
146
|
+
|
|
147
|
+
The package ships a read-only panel next to the conversation: the right sidebar's
|
|
148
|
+
guide card carries **Compaction insight**, which opens one tab per session and
|
|
149
|
+
lists the compaction transactions — when each ran, whether it was automatic or
|
|
150
|
+
`/compact`, the shadowed token count and node count, the summarizing route, the
|
|
151
|
+
summary length, the duration, and the outcome.
|
|
152
|
+
|
|
153
|
+
It reads a *session projection*, not the session itself: `lib/projection.js` folds
|
|
154
|
+
`compaction/*` events into plain state, and the browser reads the published value
|
|
155
|
+
by key through `useProjection`. The projection is registered with
|
|
156
|
+
`wire.summary: false`, so it never rides along in a session summary; and both the
|
|
157
|
+
fold and the view are wrapped so a failure degrades the panel to an empty list
|
|
158
|
+
rather than failing a session load.
|
|
159
|
+
|
|
129
160
|
## Scope
|
|
130
161
|
|
|
131
162
|
Reuses `compaction-basic`'s protocol, so session replay, summary checkpoints,
|
|
@@ -133,12 +164,36 @@ the busy lock, overflow recovery, and `/compact` are unchanged. It does not
|
|
|
133
164
|
change which surface range is compacted, and it drops reasoning blocks from
|
|
134
165
|
digested parts.
|
|
135
166
|
|
|
167
|
+
## Releasing
|
|
168
|
+
|
|
169
|
+
Pushing a `v*` tag runs [`.github/workflows/publish.yml`](.github/workflows/publish.yml):
|
|
170
|
+
the checks run first, then `npm stage publish` over OIDC. The version is *staged*,
|
|
171
|
+
not live — a maintainer approves it with 2FA on npmjs.com under **Staged Packages**
|
|
172
|
+
(or `npm stage approve <id>`). An OIDC token cannot approve by design, so the human
|
|
173
|
+
gate is on approval rather than on a stored token.
|
|
174
|
+
|
|
175
|
+
The trusted publisher is registered as stage-only, which makes a direct
|
|
176
|
+
`npm publish` from the workflow fail:
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
npm trust github dsh-compaction-adaptive \
|
|
180
|
+
--file publish.yml --repo ArtomYuan/dsh-compaction-adaptive --allow-stage-publish
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The same fields exist in the package's **Settings → Trusted publishing** page
|
|
184
|
+
(owner `ArtomYuan`, repository `dsh-compaction-adaptive`, workflow `publish.yml`,
|
|
185
|
+
environment empty).
|
|
186
|
+
|
|
136
187
|
## Files
|
|
137
188
|
|
|
138
189
|
| File | Role |
|
|
139
190
|
| --- | --- |
|
|
140
191
|
| `lib/policy.js` | Pure planning math (window, growth, threshold, tail); no Cordis dependency |
|
|
141
192
|
| `lib/chunking.js` | Budget derivation, packing, text projection, digest/fold prompts, bounded concurrency |
|
|
193
|
+
| `lib/insight.js` | Read-only projections for the observation panel: budget card, chunk card, compaction transactions |
|
|
194
|
+
| `lib/projection.js` | Session projection behind the panel: fold, wire payload, schemas |
|
|
195
|
+
| `insight-host.js` | Host row that registers the projection (`wire.summary: false`) |
|
|
196
|
+
| `client.js` | Browser half: the `compaction-insight` tab type and its panel |
|
|
142
197
|
| `lib/index.js` | The backend: observes, plans, installs the per-model policy, summarizes, delegates |
|
|
143
198
|
| `cordis.patch.yml` | Bundle layer; explains why the row is mounted by the preset |
|
|
144
199
|
| `docs/DETAIL.md` | Configuration, log lines, limits |
|
package/client.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-compaction-adaptive',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react')
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
7
|
+
/** Projection key published by the host row (`insight-host.js`). */
|
|
8
|
+
const KEY = 'compaction-adaptive/insight'
|
|
9
|
+
const TYPE = 'compaction-insight'
|
|
10
|
+
|
|
11
|
+
const COLORS = {
|
|
12
|
+
ink: 'var(--dsw-alias-text-primary, #e7e8ea)',
|
|
13
|
+
muted: 'var(--dsw-alias-text-secondary, #9aa0a8)',
|
|
14
|
+
line: 'var(--dsw-alias-border, #2b2d31)',
|
|
15
|
+
card: 'var(--dsw-alias-surface, #1c1d20)',
|
|
16
|
+
ok: '#42c58a',
|
|
17
|
+
warn: '#e0a63c',
|
|
18
|
+
bad: '#e5534b',
|
|
19
|
+
accent: '#4c8dff',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One transaction row: time, cause, shadowed span, route, outcome. */
|
|
23
|
+
function Row({ record }) {
|
|
24
|
+
const status = record.status ?? 'running'
|
|
25
|
+
const tone = status === 'ok' ? COLORS.ok : status === 'failed' ? COLORS.bad : COLORS.warn
|
|
26
|
+
const when = record.startedAt === undefined ? '—' : new Date(record.startedAt).toLocaleTimeString()
|
|
27
|
+
return h('div', {
|
|
28
|
+
style: {
|
|
29
|
+
display: 'grid', gridTemplateColumns: '66px 1fr auto', gap: 10,
|
|
30
|
+
alignItems: 'baseline', padding: '7px 0', borderBottom: `1px dashed ${COLORS.line}`,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
h('span', { style: { color: COLORS.muted, font: '11px/1 ui-monospace, monospace' } }, when),
|
|
34
|
+
h('span', { style: { fontSize: 12 } },
|
|
35
|
+
`${record.manual ? '手动 /compact' : '自动'} · 影子 `,
|
|
36
|
+
h('b', null, record.shadowedTokenCount === undefined ? '—' : `${Math.round(record.shadowedTokenCount / 1000)}K`),
|
|
37
|
+
record.shadowedNodes === undefined ? '' : ` · ${record.shadowedNodes} 节点`,
|
|
38
|
+
h('br'),
|
|
39
|
+
h('span', { style: { color: COLORS.muted, fontSize: 11 } },
|
|
40
|
+
`${record.provider ?? '—'} · 摘要 ${record.summaryChars ?? 0} 字符`,
|
|
41
|
+
record.durationMs === null || record.durationMs === undefined ? '' : ` · ${(record.durationMs / 1000).toFixed(1)}s`,
|
|
42
|
+
record.error === undefined ? '' : ` · ${record.error}`),
|
|
43
|
+
),
|
|
44
|
+
h('span', {
|
|
45
|
+
style: {
|
|
46
|
+
color: tone, border: `1px solid ${tone}44`, background: `${tone}18`,
|
|
47
|
+
borderRadius: 6, padding: '2px 6px', font: '600 10px/1 ui-monospace, monospace',
|
|
48
|
+
},
|
|
49
|
+
}, status === 'ok' ? '成功' : status === 'failed' ? '失败' : '进行中'))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The tab body. */
|
|
53
|
+
function Panel(props) {
|
|
54
|
+
const raw = props.useProjection === undefined ? undefined : props.useProjection(KEY)
|
|
55
|
+
const view = raw?.data ?? raw?.value ?? raw ?? null
|
|
56
|
+
const totals = view?.totals
|
|
57
|
+
const records = view?.transactions ?? []
|
|
58
|
+
|
|
59
|
+
return h('div', { style: { padding: '12px 14px', color: COLORS.ink, fontSize: 13 } },
|
|
60
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 10 } },
|
|
61
|
+
h('div', { style: { fontWeight: 650 } }, '压缩洞察'),
|
|
62
|
+
h('span', { style: { marginLeft: 'auto', color: COLORS.muted, fontSize: 11 } },
|
|
63
|
+
totals === undefined ? '读取中…' : `本会话 ${totals.total} 次`)),
|
|
64
|
+
|
|
65
|
+
h('div', {
|
|
66
|
+
style: {
|
|
67
|
+
display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 8, marginBottom: 10,
|
|
68
|
+
},
|
|
69
|
+
}, ...[
|
|
70
|
+
['自动', totals?.automatic ?? 0],
|
|
71
|
+
['手动', totals?.manual ?? 0],
|
|
72
|
+
['失败', totals?.failed ?? 0],
|
|
73
|
+
['影子', totals === undefined ? '—' : `${Math.round(totals.shadowedTokens / 1000)}K`],
|
|
74
|
+
].map(([label, value]) => h('div', {
|
|
75
|
+
key: label,
|
|
76
|
+
style: { background: COLORS.card, border: `1px solid ${COLORS.line}`, borderRadius: 10, padding: '7px 9px' },
|
|
77
|
+
},
|
|
78
|
+
h('div', { style: { color: COLORS.muted, fontSize: 11 } }, label),
|
|
79
|
+
h('div', { style: { font: '16px/1.3 ui-monospace, monospace' } }, String(value))))),
|
|
80
|
+
|
|
81
|
+
records.length === 0
|
|
82
|
+
? h('div', { style: { color: COLORS.muted, fontSize: 12, padding: '10px 0' } },
|
|
83
|
+
view === null ? '等待宿主投影…' : '本会话还没有压缩记录。')
|
|
84
|
+
: h('div', null, ...records.map((record) => h(Row, { key: record.compactionId, record }))),
|
|
85
|
+
|
|
86
|
+
h('div', { style: { color: COLORS.muted, fontSize: 11, marginTop: 10 } },
|
|
87
|
+
'预算与分块卡需要引擎的实时状态,下一版接入;这里只显示日志能证明的事实。'),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Tab title: the type's own label. */
|
|
92
|
+
function Title() {
|
|
93
|
+
return h('span', { style: { fontSize: 12 } }, '压缩洞察')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
inject: ['slots'],
|
|
98
|
+
apply(ctx) {
|
|
99
|
+
const id = TYPE
|
|
100
|
+
|
|
101
|
+
// One type, one tab per session: opening it again reveals it.
|
|
102
|
+
ctx.effect(() => ctx.sidebarRightTabs.register({
|
|
103
|
+
id,
|
|
104
|
+
kind: TYPE,
|
|
105
|
+
multiple: false,
|
|
106
|
+
priority: 'extension',
|
|
107
|
+
title: () => '压缩洞察',
|
|
108
|
+
guide: [{
|
|
109
|
+
id: 'open',
|
|
110
|
+
order: 30,
|
|
111
|
+
title: () => '压缩洞察',
|
|
112
|
+
description: () => '查看压缩事务、影子区间与摘要路由',
|
|
113
|
+
}],
|
|
114
|
+
}), 'dsh-compaction-adaptive: tab type')
|
|
115
|
+
|
|
116
|
+
ctx.effect(() => ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register(
|
|
117
|
+
{ name: 'sidebar.right.pane.tab', key: id },
|
|
118
|
+
Panel,
|
|
119
|
+
)), 'dsh-compaction-adaptive: tab body')
|
|
120
|
+
|
|
121
|
+
ctx.effect(() => ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register(
|
|
122
|
+
{ name: 'sidebar.right.pane.tab.title', key: id },
|
|
123
|
+
Title,
|
|
124
|
+
)), 'dsh-compaction-adaptive: tab title')
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
})
|
package/cordis.patch.yml
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
# Bundle layer for dsh-compaction-adaptive.
|
|
2
2
|
#
|
|
3
|
-
#
|
|
4
|
-
# be mounted per realm, and the active Agent preset owns that realm, so the row
|
|
5
|
-
# is mounted where the preset declares its compaction group — replacing
|
|
6
|
-
# `@deepseek-ai/dsh-compaction-basic` in that group's plugin list:
|
|
3
|
+
# Two halves, two homes:
|
|
7
4
|
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
[]
|
|
5
|
+
# 1. The compaction backend (`ctx.compaction`) lives where the Agent preset
|
|
6
|
+
# declares its compaction group, so the preset row mounts it (see
|
|
7
|
+
# docs/DETAIL.md) — not here.
|
|
8
|
+
# 2. The insight panel's host half registers a *session projection*, a host-level
|
|
9
|
+
# registry the browser reads. That row belongs to the profile, so it is
|
|
10
|
+
# inserted here.
|
|
11
|
+
- insert:
|
|
12
|
+
- id: compaction-insight
|
|
13
|
+
name: 'dsh-compaction-adaptive/insight-host'
|
package/insight-host.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host row for the insight panel.
|
|
3
|
+
*
|
|
4
|
+
* The compaction backend itself is mounted by the Agent preset. This row lives
|
|
5
|
+
* in the profile instead, because a session projection is a host-level registry
|
|
6
|
+
* whose values the browser reads — the browser half (`./client`) can only see
|
|
7
|
+
* what a host half publishes.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-compaction-adaptive/insight-host
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { insightProjectionDefinition } from './lib/projection.js'
|
|
13
|
+
|
|
14
|
+
export const name = 'compaction-insight-host'
|
|
15
|
+
|
|
16
|
+
/** The projection registry must exist before this row activates. */
|
|
17
|
+
export const inject = ['sessionProjections']
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Register the read-only projection behind the panel.
|
|
21
|
+
* @param {any} ctx - plugin context.
|
|
22
|
+
*/
|
|
23
|
+
export function apply(ctx) {
|
|
24
|
+
let disposed = false
|
|
25
|
+
let handle
|
|
26
|
+
ctx.effect(() => () => {
|
|
27
|
+
disposed = true
|
|
28
|
+
handle?.dispose?.()
|
|
29
|
+
}, 'compaction-insight projection lifetime')
|
|
30
|
+
|
|
31
|
+
ctx.sessionProjections.register(insightProjectionDefinition).then(
|
|
32
|
+
(registered) => {
|
|
33
|
+
handle = registered
|
|
34
|
+
if (disposed) {
|
|
35
|
+
handle?.dispose?.()
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
ctx.logger?.info(`[compaction-insight] projection "${insightProjectionDefinition.key}" registered`)
|
|
39
|
+
},
|
|
40
|
+
(error) => {
|
|
41
|
+
ctx.logger?.warn(`[compaction-insight] projection registration failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default { name, inject, apply }
|
package/lib/index.js
CHANGED
|
@@ -32,6 +32,14 @@ import {
|
|
|
32
32
|
streamText,
|
|
33
33
|
utf8Bytes,
|
|
34
34
|
} from './chunking.js'
|
|
35
|
+
import {
|
|
36
|
+
budgetSnapshot,
|
|
37
|
+
chunkSnapshot,
|
|
38
|
+
compactionTotals,
|
|
39
|
+
recordChunkRun,
|
|
40
|
+
recordScaleChange,
|
|
41
|
+
summarizeCompactions,
|
|
42
|
+
} from './insight.js'
|
|
35
43
|
import {
|
|
36
44
|
ADAPTIVE_KEYS,
|
|
37
45
|
describePlan,
|
|
@@ -136,6 +144,10 @@ export default class AdaptiveCompactionEngine extends BasicCompactionEngine {
|
|
|
136
144
|
this.chunk = resolveChunkConfig(config)
|
|
137
145
|
/** @type {Map<string, number>} learned per-summarizer capacity multipliers. */
|
|
138
146
|
this.scales = new Map()
|
|
147
|
+
/** @type {object[]} newest-last in-process chunk runs (not durable). */
|
|
148
|
+
this.chunkRuns = []
|
|
149
|
+
/** @type {object[]} newest-last learned-capacity changes (not durable). */
|
|
150
|
+
this.scaleSeries = []
|
|
139
151
|
/** @type {WeakMap<object, { growth: number, last: number | undefined, cooldown: number }>} */
|
|
140
152
|
this.sessions = new WeakMap()
|
|
141
153
|
/** @type {Map<string, string>} */
|
|
@@ -404,7 +416,18 @@ export default class AdaptiveCompactionEngine extends BasicCompactionEngine {
|
|
|
404
416
|
))
|
|
405
417
|
signal?.throwIfAborted()
|
|
406
418
|
const merged = await this._fold(target, system, digests, capacity, signal, base)
|
|
407
|
-
|
|
419
|
+
const recovered = nextScale(this._scaleFor(target), 'recover', this.chunk)
|
|
420
|
+
this.scales.set(key, recovered)
|
|
421
|
+
recordScaleChange(this.scaleSeries, { at: Date.now(), scale: recovered, reason: 'recover' })
|
|
422
|
+
recordChunkRun(this.chunkRuns, {
|
|
423
|
+
at: Date.now(),
|
|
424
|
+
provider: target.provider,
|
|
425
|
+
model: target.model,
|
|
426
|
+
parts: chunks.length,
|
|
427
|
+
tokens: contentTokens,
|
|
428
|
+
bytes: contentBytes,
|
|
429
|
+
scale: plan.scale,
|
|
430
|
+
})
|
|
408
431
|
this.ctx.logger?.info(
|
|
409
432
|
`[compaction-adaptive] chunked summary done: ${chunks.length} digests folded into ${merged.length} chars`,
|
|
410
433
|
)
|
|
@@ -422,6 +445,7 @@ export default class AdaptiveCompactionEngine extends BasicCompactionEngine {
|
|
|
422
445
|
attempt += 1
|
|
423
446
|
const next = nextScale(this._scaleFor(target), 'shrink', this.chunk)
|
|
424
447
|
this.scales.set(key, next)
|
|
448
|
+
recordScaleChange(this.scaleSeries, { at: Date.now(), scale: next, reason: 'shrink' })
|
|
425
449
|
this.ctx.logger?.warn(
|
|
426
450
|
`[compaction-adaptive] ${key} rejected a chunked request (${error instanceof Error ? error.message : String(error)}); `
|
|
427
451
|
+ `shrinking capacity to ${(next * 100).toFixed(0)}% and retrying`,
|
|
@@ -592,6 +616,111 @@ export default class AdaptiveCompactionEngine extends BasicCompactionEngine {
|
|
|
592
616
|
}
|
|
593
617
|
}
|
|
594
618
|
|
|
619
|
+
/**
|
|
620
|
+
* Build the read-only snapshot the insight panel renders.
|
|
621
|
+
*
|
|
622
|
+
* Pure projection: reads the same measurement the pressure gate uses, the
|
|
623
|
+
* plan the engine would install for the current route, the chunk budgets the
|
|
624
|
+
* summary model implies, and the durable `compaction/*` events. Nothing is
|
|
625
|
+
* written, so a panel may poll it.
|
|
626
|
+
* @param {any} agent - agent whose session is being described.
|
|
627
|
+
* @returns {Promise<object>} panel snapshot.
|
|
628
|
+
*/
|
|
629
|
+
async insight(agent) {
|
|
630
|
+
const session = agent?.session
|
|
631
|
+
const header = session?.requestHeader?.()
|
|
632
|
+
const routed = header?.config
|
|
633
|
+
const measurement = this.ctx.tokenMeter.measure(session, header)
|
|
634
|
+
const state = this._state(session)
|
|
635
|
+
|
|
636
|
+
const conversation = routed?.provider && routed?.model
|
|
637
|
+
? { provider: routed.provider, model: routed.model }
|
|
638
|
+
: agent?.options?.provider && agent?.options?.model
|
|
639
|
+
? { provider: agent.options.provider, model: agent.options.model }
|
|
640
|
+
: undefined
|
|
641
|
+
|
|
642
|
+
const conversationInfo = conversation === undefined
|
|
643
|
+
? undefined
|
|
644
|
+
: await this.ctx.llm.resolveModelInfo(conversation.provider, conversation.model)
|
|
645
|
+
const conversationWindow = conversationInfo?.context?.contextWindow ?? 0
|
|
646
|
+
const reserved = routed?.maxTokens ?? conversationInfo?.defaultMaxTokens ?? 0
|
|
647
|
+
const adaptive = conversation === undefined ? this.adaptive : this._adaptiveFor(conversation)
|
|
648
|
+
const plan = conversationWindow > 0
|
|
649
|
+
? planPolicy({
|
|
650
|
+
contextWindow: conversationWindow,
|
|
651
|
+
reservedTokens: reserved,
|
|
652
|
+
growthTokens: state.growth,
|
|
653
|
+
adaptive,
|
|
654
|
+
profile: adaptive.profile,
|
|
655
|
+
})
|
|
656
|
+
: null
|
|
657
|
+
|
|
658
|
+
const summaryTarget = this._summarizationTarget(agent)
|
|
659
|
+
const summaryInfo = await this.ctx.llm.resolveModelInfo(summaryTarget.provider, summaryTarget.model)
|
|
660
|
+
const capacity = {
|
|
661
|
+
window: summaryInfo?.context?.contextWindow ?? 0,
|
|
662
|
+
reservedTokens: summaryTarget.maxTokens ?? 0,
|
|
663
|
+
overheadTokens: this._overheadTokens(agent, header),
|
|
664
|
+
}
|
|
665
|
+
const scale = this._scaleFor(summaryTarget)
|
|
666
|
+
const records = summarizeCompactions(this._eventsOf(session), 20)
|
|
667
|
+
|
|
668
|
+
return {
|
|
669
|
+
at: Date.now(),
|
|
670
|
+
conversation: conversation ?? null,
|
|
671
|
+
conversationWindow,
|
|
672
|
+
summaryRoute: { provider: summaryTarget.provider, model: summaryTarget.model },
|
|
673
|
+
budget: budgetSnapshot({ plan, measurement, adaptive }),
|
|
674
|
+
chunk: chunkSnapshot({ chunkConfig: this.chunk, capacity, scale }),
|
|
675
|
+
compactions: records,
|
|
676
|
+
compactionTotals: compactionTotals(records),
|
|
677
|
+
chunkRuns: this.chunkRuns.slice().reverse(),
|
|
678
|
+
scaleSeries: this.scaleSeries.slice(),
|
|
679
|
+
growth: { ema: Math.round(state.growth), cooldown: state.cooldown },
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Read a session's events without depending on an events collection.
|
|
685
|
+
* @param {any} session - session to read.
|
|
686
|
+
* @returns {any[]} events in log order.
|
|
687
|
+
*/
|
|
688
|
+
_eventsOf(session) {
|
|
689
|
+
const events = []
|
|
690
|
+
const end = session?.seq ?? 0
|
|
691
|
+
for (let seq = 0; seq < end; seq += 1) {
|
|
692
|
+
const event = session.eventAt?.(seq)
|
|
693
|
+
if (event !== undefined) events.push(event)
|
|
694
|
+
}
|
|
695
|
+
return events
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* Estimate the fixed overhead a summarization request carries: the replayed
|
|
700
|
+
* system head, the instruction, and the tool schemas.
|
|
701
|
+
* @param {any} agent - owning agent.
|
|
702
|
+
* @param {any} header - latest request header.
|
|
703
|
+
* @returns {number} overhead tokens.
|
|
704
|
+
*/
|
|
705
|
+
_overheadTokens(agent, header) {
|
|
706
|
+
let systemTokens = 0
|
|
707
|
+
try {
|
|
708
|
+
const head = agent?.session?.eventAt?.(0)
|
|
709
|
+
const message = head === undefined ? null : agent.session.deriveEventMessage(head)
|
|
710
|
+
const text = message === null
|
|
711
|
+
? ''
|
|
712
|
+
: (message.content ?? [])
|
|
713
|
+
.filter((block) => block?.type === 'text' && typeof block.text === 'string')
|
|
714
|
+
.map((block) => block.text)
|
|
715
|
+
.join('\n')
|
|
716
|
+
if (text.length > 0) systemTokens = conservativeTokens(text, this._estimateText(text))
|
|
717
|
+
} catch {
|
|
718
|
+
// A session whose head does not project (custom seed) contributes no system prompt.
|
|
719
|
+
}
|
|
720
|
+
const toolsBytes = header?.tools === undefined ? 0 : utf8Bytes(JSON.stringify(header.tools))
|
|
721
|
+
return systemTokens + this._estimateText(mergeInstruction(0)) + Math.ceil(toolsBytes / 3)
|
|
722
|
+
}
|
|
723
|
+
|
|
595
724
|
/**
|
|
596
725
|
* Record one routed model's adaptive budget without discarding the explicit
|
|
597
726
|
* routing a user configured for that model (its summarization target, for
|
package/lib/insight.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read-only projections for the compaction insight panel.
|
|
3
|
+
*
|
|
4
|
+
* Everything here is derived from facts the harness already keeps: the same
|
|
5
|
+
* `TokenMeasurement` the pressure gate uses, the plan the engine installed, the
|
|
6
|
+
* chunk budgets derived from the summary model's window, and the durable
|
|
7
|
+
* `compaction/*` events. Nothing here writes to a session, so a panel can call
|
|
8
|
+
* it as often as it likes.
|
|
9
|
+
*
|
|
10
|
+
* Deliberate honesty rules:
|
|
11
|
+
* - `compaction/start` records only `turn`/`sourceCommandId`, so the panel may
|
|
12
|
+
* say "manual" vs "automatic", never "pressure" vs "overflow".
|
|
13
|
+
* - `compaction/summary` records the shadowed span and the summarizing route,
|
|
14
|
+
* never whether the call was chunked. Chunk evidence therefore comes from the
|
|
15
|
+
* engine's own in-process log (`chunkRuns`), which is not durable and does not
|
|
16
|
+
* claim to be.
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-compaction-adaptive/insight
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** One compaction transaction rebuilt from its durable events. */
|
|
22
|
+
const COMPACTION_TYPES = new Set(['compaction/start', 'compaction/summary', 'compaction/end'])
|
|
23
|
+
|
|
24
|
+
/** Fraction of a whole for one bar segment; 0 when the whole is unknown. */
|
|
25
|
+
function share(part, whole) {
|
|
26
|
+
if (!Number.isFinite(part) || !Number.isFinite(whole) || whole <= 0) return 0
|
|
27
|
+
return Math.max(0, Math.min(1, part / whole))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Number of measured turns until the surface reaches the pressure threshold.
|
|
32
|
+
* @param {number} totalTokens - current surface pressure.
|
|
33
|
+
* @param {number} thresholdTokens - installed trigger point.
|
|
34
|
+
* @param {number} growthTokens - per-turn growth estimate.
|
|
35
|
+
* @returns {number | null} turns, or `null` when growth is not positive.
|
|
36
|
+
*/
|
|
37
|
+
export function turnsToThreshold(totalTokens, thresholdTokens, growthTokens) {
|
|
38
|
+
if (!Number.isFinite(growthTokens) || growthTokens <= 0) return null
|
|
39
|
+
const remaining = thresholdTokens - totalTokens
|
|
40
|
+
if (remaining <= 0) return 0
|
|
41
|
+
return Math.ceil(remaining / growthTokens)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Shape the panel's top card: how full the window is, where the trigger sits,
|
|
46
|
+
* and how the bar splits into retained tail, usable middle, and free headroom.
|
|
47
|
+
* @param {object} input - snapshot input.
|
|
48
|
+
* @param {any} input.plan - installed plan from the policy module.
|
|
49
|
+
* @param {{totalTokens: number}} input.measurement - current meeting.
|
|
50
|
+
* @param {Readonly<Record<string, any>>} input.adaptive - effective adaptive settings.
|
|
51
|
+
* @returns {object} panel card data.
|
|
52
|
+
*/
|
|
53
|
+
export function budgetSnapshot(input) {
|
|
54
|
+
const total = Number.isFinite(input.measurement?.totalTokens) ? input.measurement.totalTokens : 0
|
|
55
|
+
const plan = input.plan ?? null
|
|
56
|
+
const window = plan?.contextWindow ?? 0
|
|
57
|
+
const budget = plan?.messageBudget ?? 0
|
|
58
|
+
const threshold = plan?.thresholdTokens ?? 0
|
|
59
|
+
const tail = plan?.retainTokens ?? 0
|
|
60
|
+
const growth = plan?.growthTokens ?? 0
|
|
61
|
+
return {
|
|
62
|
+
totalTokens: total,
|
|
63
|
+
windowTokens: window,
|
|
64
|
+
budgetTokens: budget,
|
|
65
|
+
thresholdTokens: threshold,
|
|
66
|
+
thresholdRatio: plan?.thresholdRatio ?? null,
|
|
67
|
+
tailTokens: tail,
|
|
68
|
+
growthTokens: growth,
|
|
69
|
+
usedRatio: share(total, window),
|
|
70
|
+
thresholdShare: share(threshold, window),
|
|
71
|
+
tailShare: share(tail, window),
|
|
72
|
+
usableShare: share(Math.max(0, Math.min(total, threshold) - tail), window),
|
|
73
|
+
freeShare: share(Math.max(0, window - Math.max(total, threshold)), window),
|
|
74
|
+
turnsToThreshold: turnsToThreshold(total, threshold, growth),
|
|
75
|
+
turnsAhead: input.adaptive?.turnsAhead ?? null,
|
|
76
|
+
profile: input.adaptive?.profile ?? null,
|
|
77
|
+
mode: input.adaptive?.mode ?? null,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Derive the chunk card's budgets for one route.
|
|
83
|
+
* @param {object} input - snapshot input.
|
|
84
|
+
* @param {Readonly<Record<string, any>>} input.chunkConfig - resolved chunking configuration.
|
|
85
|
+
* @param {{window: number, reservedTokens: number, overheadTokens: number}} input.capacity - summary-model capacity.
|
|
86
|
+
* @param {number} input.scale - learned capacity multiplier for the route.
|
|
87
|
+
* @returns {object} chunk card data.
|
|
88
|
+
*/
|
|
89
|
+
export function chunkSnapshot(input) {
|
|
90
|
+
const config = input.chunkConfig
|
|
91
|
+
const capacity = Math.max(
|
|
92
|
+
0,
|
|
93
|
+
(input.capacity.window > 0 ? input.capacity.window : 0) - input.capacity.reservedTokens - input.capacity.overheadTokens,
|
|
94
|
+
)
|
|
95
|
+
const scale = Number.isFinite(input.scale) ? input.scale : 1
|
|
96
|
+
const adaptive = config.chunkSizing !== 'fixed'
|
|
97
|
+
return {
|
|
98
|
+
enabled: config.chunking !== 'off',
|
|
99
|
+
sizing: config.chunkSizing,
|
|
100
|
+
scale,
|
|
101
|
+
capacityTokens: capacity,
|
|
102
|
+
singleShotTokens: Math.floor(capacity * (adaptive ? config.chunkSingleShotRatio : 1) * scale),
|
|
103
|
+
partTokens: Math.max(256, Math.floor(capacity * (adaptive ? config.chunkTokenRatio : 1) * scale)),
|
|
104
|
+
partBytes: Math.max(10_000, Math.floor(config.chunkBytes * scale)),
|
|
105
|
+
triggerBytes: Math.floor(config.chunkTriggerBytes * scale),
|
|
106
|
+
concurrency: config.chunkConcurrency,
|
|
107
|
+
retries: config.chunkRetries,
|
|
108
|
+
shrinkAttempts: config.chunkShrinkAttempts,
|
|
109
|
+
shrinkFactor: config.chunkShrinkFactor,
|
|
110
|
+
recoverFactor: config.chunkRecoverFactor,
|
|
111
|
+
minScale: config.chunkMinScale,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Rebuild compaction transactions from durable events.
|
|
117
|
+
*
|
|
118
|
+
* Pairs `start`/`summary`/`end` by `compactionId`; a transaction keeps its
|
|
119
|
+
* summary even when the close carries an error, because both facts matter to a
|
|
120
|
+
* reader ("it wrote a summary, then failed to commit" is different from "it
|
|
121
|
+
* never summarized").
|
|
122
|
+
* @param {readonly any[]} events - session events in log order.
|
|
123
|
+
* @param {number} [limit] - newest records to keep.
|
|
124
|
+
* @returns {object[]} newest-first transactions.
|
|
125
|
+
*/
|
|
126
|
+
export function summarizeCompactions(events, limit = 20) {
|
|
127
|
+
const open = new Map()
|
|
128
|
+
const done = []
|
|
129
|
+
for (const event of events ?? []) {
|
|
130
|
+
if (!COMPACTION_TYPES.has(event?.type)) continue
|
|
131
|
+
const id = event.data?.compactionId
|
|
132
|
+
if (id === undefined) continue
|
|
133
|
+
let record = open.get(id)
|
|
134
|
+
if (record === undefined) {
|
|
135
|
+
record = { compactionId: id }
|
|
136
|
+
open.set(id, record)
|
|
137
|
+
}
|
|
138
|
+
if (event.type === 'compaction/start') {
|
|
139
|
+
record.startedAt = event.time
|
|
140
|
+
record.seq = event.seq
|
|
141
|
+
record.turn = event.data.turn
|
|
142
|
+
record.manual = event.data.turn === null
|
|
143
|
+
record.commandId = event.data.sourceCommandId ?? null
|
|
144
|
+
} else if (event.type === 'compaction/summary') {
|
|
145
|
+
record.shadowedTokenCount = event.data.shadowedTokenCount ?? 0
|
|
146
|
+
record.shadowedNodes = Array.isArray(event.data.shadowedSeqs) ? event.data.shadowedSeqs.length : 0
|
|
147
|
+
record.provider = event.data.provider ?? null
|
|
148
|
+
record.model = event.data.model ?? null
|
|
149
|
+
record.maxTokens = event.data.maxTokens ?? null
|
|
150
|
+
record.summaryChars = Array.isArray(event.data.summary)
|
|
151
|
+
? event.data.summary.reduce((sum, block) => sum + (typeof block?.text === 'string' ? block.text.length : 0), 0)
|
|
152
|
+
: 0
|
|
153
|
+
} else {
|
|
154
|
+
record.endedAt = event.time
|
|
155
|
+
record.error = event.data.error ?? null
|
|
156
|
+
open.delete(id)
|
|
157
|
+
done.push(record)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// A transaction still open when the panel reads is a live lock, not history.
|
|
161
|
+
for (const record of open.values()) done.push({ ...record, inFlight: true })
|
|
162
|
+
done.sort((left, right) => (right.startedAt ?? 0) - (left.startedAt ?? 0))
|
|
163
|
+
return done.slice(0, limit).map((record) => ({
|
|
164
|
+
...record,
|
|
165
|
+
durationMs: record.startedAt !== undefined && record.endedAt !== undefined
|
|
166
|
+
? Math.max(0, record.endedAt - record.startedAt)
|
|
167
|
+
: null,
|
|
168
|
+
status: record.inFlight === true ? 'running' : record.error ? 'failed' : 'ok',
|
|
169
|
+
}))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Count automatic vs manual and ok vs failed transactions for a summary strip.
|
|
174
|
+
* @param {readonly any[]} records - transactions from {@link summarizeCompactions}.
|
|
175
|
+
* @returns {{total: number, automatic: number, manual: number, failed: number, running: number, shadowedTokens: number}} counts.
|
|
176
|
+
*/
|
|
177
|
+
export function compactionTotals(records) {
|
|
178
|
+
const totals = { total: 0, automatic: 0, manual: 0, failed: 0, running: 0, shadowedTokens: 0 }
|
|
179
|
+
for (const record of records ?? []) {
|
|
180
|
+
totals.total += 1
|
|
181
|
+
if (record.manual === true) totals.manual += 1
|
|
182
|
+
else totals.automatic += 1
|
|
183
|
+
if (record.status === 'failed') totals.failed += 1
|
|
184
|
+
if (record.status === 'running') totals.running += 1
|
|
185
|
+
totals.shadowedTokens += Number.isFinite(record.shadowedTokenCount) ? record.shadowedTokenCount : 0
|
|
186
|
+
}
|
|
187
|
+
return totals
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Append one in-process chunk run, keeping the log bounded.
|
|
192
|
+
* @param {object[]} log - mutable run log, oldest first.
|
|
193
|
+
* @param {object} entry - run facts.
|
|
194
|
+
* @param {number} [limit] - maximum retained entries.
|
|
195
|
+
* @returns {object[]} the same log.
|
|
196
|
+
*/
|
|
197
|
+
export function recordChunkRun(log, entry, limit = 20) {
|
|
198
|
+
log.push({ ...entry })
|
|
199
|
+
while (log.length > limit) log.shift()
|
|
200
|
+
return log
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Append one learned-capacity change, keeping the series bounded.
|
|
205
|
+
* @param {object[]} series - mutable series, oldest first.
|
|
206
|
+
* @param {object} entry - `{ at, scale, reason }`.
|
|
207
|
+
* @param {number} [limit] - maximum retained points.
|
|
208
|
+
* @returns {object[]} the same series.
|
|
209
|
+
*/
|
|
210
|
+
export function recordScaleChange(series, entry, limit = 40) {
|
|
211
|
+
const last = series[series.length - 1]
|
|
212
|
+
if (last !== undefined && last.scale === entry.scale && last.reason === entry.reason) return series
|
|
213
|
+
series.push({ ...entry })
|
|
214
|
+
while (series.length > limit) series.shift()
|
|
215
|
+
return series
|
|
216
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-side session projection behind the insight panel.
|
|
3
|
+
*
|
|
4
|
+
* A projection is the only sanctioned way for browser code to read host state:
|
|
5
|
+
* the host folds committed events into plain state, `wire.view` publishes the
|
|
6
|
+
* read-side shape, and the Client reads it by key through `useProjection`. So
|
|
7
|
+
* this unit folds exactly what the durable log can prove — compaction
|
|
8
|
+
* transactions, their outcome, the shadowed span, and the summarizing route —
|
|
9
|
+
* and nothing else.
|
|
10
|
+
*
|
|
11
|
+
* Live engine state (the plan, the growth estimate, the learned chunk capacity)
|
|
12
|
+
* is intentionally absent: a projection must be replayable from the log, and
|
|
13
|
+
* those values are not logged. The panel labels them as live process state.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-compaction-adaptive/projection
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { z } from 'zod'
|
|
19
|
+
|
|
20
|
+
/** Projection key read by the panel through `useProjection`. */
|
|
21
|
+
export const INSIGHT_PROJECTION_KEY = 'compaction-adaptive/insight'
|
|
22
|
+
|
|
23
|
+
/** Newest transactions kept in the projection state. */
|
|
24
|
+
const MAX_TRANSACTIONS = 40
|
|
25
|
+
|
|
26
|
+
const transactionSchema = z.object({
|
|
27
|
+
compactionId: z.string(),
|
|
28
|
+
startedAt: z.number().int().nonnegative(),
|
|
29
|
+
endedAt: z.number().int().nonnegative().optional(),
|
|
30
|
+
turn: z.number().int().nullable(),
|
|
31
|
+
manual: z.boolean(),
|
|
32
|
+
shadowedTokenCount: z.number().int().nonnegative().optional(),
|
|
33
|
+
shadowedNodes: z.number().int().nonnegative().optional(),
|
|
34
|
+
provider: z.string().optional(),
|
|
35
|
+
model: z.string().optional(),
|
|
36
|
+
summaryChars: z.number().int().nonnegative().optional(),
|
|
37
|
+
error: z.string().optional(),
|
|
38
|
+
inFlight: z.boolean().optional(),
|
|
39
|
+
durationMs: z.number().int().nonnegative().nullable().optional(),
|
|
40
|
+
status: z.enum(['ok', 'failed', 'running']).optional(),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** Fold state: raw transactions, oldest first. */
|
|
44
|
+
const stateSchema = z.object({
|
|
45
|
+
transactions: z.array(transactionSchema).max(MAX_TRANSACTIONS),
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/** Wire payload: what the browser renders, already counted. */
|
|
49
|
+
const viewSchema = z.object({
|
|
50
|
+
transactions: z.array(transactionSchema),
|
|
51
|
+
totals: z.object({
|
|
52
|
+
total: z.number().int().nonnegative(),
|
|
53
|
+
automatic: z.number().int().nonnegative(),
|
|
54
|
+
manual: z.number().int().nonnegative(),
|
|
55
|
+
failed: z.number().int().nonnegative(),
|
|
56
|
+
running: z.number().int().nonnegative(),
|
|
57
|
+
shadowedTokens: z.number().int().nonnegative(),
|
|
58
|
+
}),
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Apply one event to the fold.
|
|
63
|
+
*
|
|
64
|
+
* Only `compaction/*` events are interesting; every other event returns the same
|
|
65
|
+
* state reference so the framework does no downstream work.
|
|
66
|
+
* @param {z.infer<typeof stateSchema>} state - state covering all prior events.
|
|
67
|
+
* @param {any} event - next committed event.
|
|
68
|
+
* @returns {z.infer<typeof stateSchema>} next state.
|
|
69
|
+
*/
|
|
70
|
+
export function applyInsight(state, event) {
|
|
71
|
+
try {
|
|
72
|
+
return foldInsight(state, event)
|
|
73
|
+
} catch {
|
|
74
|
+
// A fold unit runs inside every session's projection pass: it must never be
|
|
75
|
+
// able to fail a session load. Returning the previous state drops only this
|
|
76
|
+
// unit's contribution to that event.
|
|
77
|
+
return state
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The fold proper. Kept separate so {@link applyInsight} can guarantee the
|
|
83
|
+
* no-throw contract a projection unit needs.
|
|
84
|
+
* @param {any} state - state covering all prior events.
|
|
85
|
+
* @param {any} event - next committed event.
|
|
86
|
+
* @returns {any} next state.
|
|
87
|
+
*/
|
|
88
|
+
function foldInsight(state, event) {
|
|
89
|
+
if (typeof event?.type !== 'string' || !event.type.startsWith('compaction/')) return state
|
|
90
|
+
const id = event.data?.compactionId
|
|
91
|
+
if (id === undefined) return state
|
|
92
|
+
|
|
93
|
+
const transactions = (Array.isArray(state?.transactions) ? state.transactions : []).slice()
|
|
94
|
+
let index = transactions.findIndex((entry) => entry.compactionId === id)
|
|
95
|
+
if (index === -1) {
|
|
96
|
+
if (event.type !== 'compaction/start') return state
|
|
97
|
+
transactions.push({ compactionId: id, startedAt: event.time ?? 0, turn: event.data.turn ?? null, manual: event.data.turn === null })
|
|
98
|
+
index = transactions.length - 1
|
|
99
|
+
} else {
|
|
100
|
+
transactions[index] = { ...transactions[index] }
|
|
101
|
+
}
|
|
102
|
+
const record = transactions[index]
|
|
103
|
+
|
|
104
|
+
if (event.type === 'compaction/summary') {
|
|
105
|
+
record.shadowedTokenCount = event.data.shadowedTokenCount ?? 0
|
|
106
|
+
record.shadowedNodes = Array.isArray(event.data.shadowedSeqs) ? event.data.shadowedSeqs.length : 0
|
|
107
|
+
if (typeof event.data.provider === 'string') record.provider = event.data.provider
|
|
108
|
+
if (typeof event.data.model === 'string') record.model = event.data.model
|
|
109
|
+
record.summaryChars = Array.isArray(event.data.summary)
|
|
110
|
+
? event.data.summary.reduce((sum, block) => sum + (typeof block?.text === 'string' ? block.text.length : 0), 0)
|
|
111
|
+
: 0
|
|
112
|
+
} else if (event.type === 'compaction/end') {
|
|
113
|
+
record.endedAt = event.time ?? 0
|
|
114
|
+
if (typeof event.data.error === 'string') record.error = event.data.error
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
while (transactions.length > MAX_TRANSACTIONS) transactions.shift()
|
|
118
|
+
return { transactions }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Derive the browser payload from fold state.
|
|
123
|
+
*
|
|
124
|
+
* Adds duration and status (pure functions of the folded fields) and the counts
|
|
125
|
+
* the summary strip needs. The fold returns the same state reference for
|
|
126
|
+
* irrelevant events, so this runs only after a compaction event lands.
|
|
127
|
+
* @param {any} state - folded state.
|
|
128
|
+
* @returns {object} wire payload.
|
|
129
|
+
*/
|
|
130
|
+
export function viewInsight(state) {
|
|
131
|
+
try {
|
|
132
|
+
return projectView(state)
|
|
133
|
+
} catch {
|
|
134
|
+
// `wire.view` runs while the host builds a session summary. A throw here can
|
|
135
|
+
// fail the whole session, so a degraded empty payload is always better than
|
|
136
|
+
// an exception. The panel shows "no records" instead of the session dying.
|
|
137
|
+
return { transactions: [], totals: { total: 0, automatic: 0, manual: 0, failed: 0, running: 0, shadowedTokens: 0 } }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The view proper, separated for the no-throw wrapper above.
|
|
143
|
+
* @param {any} state - folded state.
|
|
144
|
+
* @returns {object} wire payload.
|
|
145
|
+
*/
|
|
146
|
+
function projectView(state) {
|
|
147
|
+
const transactions = (Array.isArray(state?.transactions) ? state.transactions : [])
|
|
148
|
+
.map((record) => ({
|
|
149
|
+
...record,
|
|
150
|
+
durationMs: record.endedAt === undefined ? null : Math.max(0, record.endedAt - record.startedAt),
|
|
151
|
+
status: record.endedAt === undefined ? 'running' : record.error === undefined ? 'ok' : 'failed',
|
|
152
|
+
}))
|
|
153
|
+
.slice()
|
|
154
|
+
.reverse()
|
|
155
|
+
const totals = { total: 0, automatic: 0, manual: 0, failed: 0, running: 0, shadowedTokens: 0 }
|
|
156
|
+
for (const record of transactions) {
|
|
157
|
+
totals.total += 1
|
|
158
|
+
if (record.manual) totals.manual += 1
|
|
159
|
+
else totals.automatic += 1
|
|
160
|
+
if (record.status === 'failed') totals.failed += 1
|
|
161
|
+
if (record.status === 'running') totals.running += 1
|
|
162
|
+
totals.shadowedTokens += typeof record.shadowedTokenCount === 'number' ? record.shadowedTokenCount : 0
|
|
163
|
+
}
|
|
164
|
+
return { transactions, totals }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The projection definition registered with `ctx.sessionProjections`.
|
|
169
|
+
*
|
|
170
|
+
* `stateVersion` starts at 1: bump it whenever a field or the fold semantics
|
|
171
|
+
* change, so a stale cached checkpoint is discarded rather than replaying with
|
|
172
|
+
* the wrong meaning.
|
|
173
|
+
*/
|
|
174
|
+
export const insightProjectionDefinition = {
|
|
175
|
+
key: INSIGHT_PROJECTION_KEY,
|
|
176
|
+
stateSchema,
|
|
177
|
+
init: () => ({ transactions: [] }),
|
|
178
|
+
apply: applyInsight,
|
|
179
|
+
wire: {
|
|
180
|
+
viewSchema,
|
|
181
|
+
view: (state) => viewInsight(state),
|
|
182
|
+
// Excluded from every Session summary: a summary-carried projection runs during
|
|
183
|
+
// session load, where a throw fails the load and the host disposes the session.
|
|
184
|
+
summary: false,
|
|
185
|
+
},
|
|
186
|
+
stateVersion: 1,
|
|
187
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-compaction-adaptive",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Adaptive context compaction for DeepSeek Harness: derives each model's pressure threshold and retained tail from its context window and the session's measured growth.",
|
|
6
6
|
"icon": "icon.svg",
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
},
|
|
20
20
|
"exports": {
|
|
21
21
|
".": "./lib/index.js",
|
|
22
|
+
"./insight-host": "./insight-host.js",
|
|
23
|
+
"./client": "./client.js",
|
|
22
24
|
"./policy": "./lib/policy.js",
|
|
23
25
|
"./chunking": "./lib/chunking.js",
|
|
24
26
|
"./locale/*.json": "./locale/*.json",
|
|
@@ -31,14 +33,28 @@
|
|
|
31
33
|
"docs",
|
|
32
34
|
"icon.svg",
|
|
33
35
|
"cordis.patch.yml",
|
|
34
|
-
"README.md"
|
|
36
|
+
"README.md",
|
|
37
|
+
"insight-host.js",
|
|
38
|
+
"client.js"
|
|
35
39
|
],
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@deepseek-ai/dsh-compaction-basic": "*",
|
|
42
|
+
"@deepseek-ai/dsh-llm": "*",
|
|
43
|
+
"@deepseek-ai/schemastery": "*"
|
|
44
|
+
},
|
|
36
45
|
"engines": {
|
|
37
46
|
"dsh": ">=0.2.2-rc.1"
|
|
38
47
|
},
|
|
39
48
|
"dsh": {
|
|
40
49
|
"bundle": {
|
|
41
50
|
"patch": "./cordis.patch.yml"
|
|
51
|
+
},
|
|
52
|
+
"client": {
|
|
53
|
+
"platform": "web",
|
|
54
|
+
"inject": [
|
|
55
|
+
"@deepseek-ai/dsh-client-ui-session",
|
|
56
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right"
|
|
57
|
+
]
|
|
42
58
|
}
|
|
43
59
|
},
|
|
44
60
|
"keywords": [
|
|
@@ -49,5 +65,8 @@
|
|
|
49
65
|
"compaction",
|
|
50
66
|
"context"
|
|
51
67
|
],
|
|
52
|
-
"license": "MIT"
|
|
68
|
+
"license": "MIT",
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"zod": "^4.6.7"
|
|
71
|
+
}
|
|
53
72
|
}
|