dsh-compaction-adaptive 0.0.0-stage → 0.1.2
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 +21 -0
- package/README.md +147 -2
- package/cordis.patch.yml +17 -0
- package/docs/DETAIL.md +174 -0
- package/icon.svg +18 -0
- package/lib/chunking.js +459 -0
- package/lib/index.js +667 -0
- package/lib/policy.js +315 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ArtomYuan
|
|
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,148 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-compaction-adaptive
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Adaptive context compaction for DeepSeek Harness.
|
|
4
|
+
|
|
5
|
+
The plugin is a compaction backend (`ctx.compaction`) built on
|
|
6
|
+
`@deepseek-ai/dsh-compaction-basic`: it keeps that backend's durable
|
|
7
|
+
compaction protocol and replaces only its numeric policy. Before every
|
|
8
|
+
automatic decision it derives the routed model's pressure threshold and
|
|
9
|
+
retained tail from
|
|
10
|
+
|
|
11
|
+
- the model's real context window (`llm.resolveModelInfo`),
|
|
12
|
+
- the output tokens the current request reserves,
|
|
13
|
+
- an exponential moving average of how many tokens each turn actually adds.
|
|
14
|
+
|
|
15
|
+
So a 1M-token cloud route stops trying to fill 800K before summarizing, and a
|
|
16
|
+
128K local route keeps more of the recent turn verbatim.
|
|
17
|
+
|
|
18
|
+
When a single summary request would exceed what the summarizing model can take,
|
|
19
|
+
the backend derives its own budgets from that model's declared window — and from
|
|
20
|
+
what the route has actually accepted — then digests the shadowed content in
|
|
21
|
+
parts and folds the digests into one checkpoint. A rejected request shrinks the
|
|
22
|
+
learned capacity and retries smaller; a success lets it recover. No separate
|
|
23
|
+
stream-interception plugin is needed for a relay limit.
|
|
24
|
+
|
|
25
|
+
## What it decides
|
|
26
|
+
|
|
27
|
+
| Question | Answer source |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| When to compact | `window − reserved output − turnsAhead × measured growth`, clamped to `floorRatio … ceilRatio` |
|
|
30
|
+
| How much recent history to keep verbatim | at least one turn's worth of measured growth, at least `tailMinTokens`, at most `tailMaxRatio` of the message budget |
|
|
31
|
+
| How large one summary request may be | the summary model's window times `chunkSingleShotRatio`, with a byte cap as the transport guard |
|
|
32
|
+
| How large one digest part may be | the same capacity times `chunkTokenRatio`, shrunk by the route's learned multiplier |
|
|
33
|
+
|
|
34
|
+
Cost ceilings stay expressible: a slow local prefill can cap its route with
|
|
35
|
+
`routes[].ceilRatio` and the adaptation happens *inside* that cap.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
Requires DeepSeek Harness `>= 0.2.2-rc.1`. There is no build step: the published
|
|
40
|
+
package is the repository.
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
# from npm — the shortest form
|
|
44
|
+
dsh plugin --profile desktop add dsh-compaction-adaptive
|
|
45
|
+
|
|
46
|
+
# straight from GitHub
|
|
47
|
+
dsh plugin --profile desktop add https://github.com/ArtomYuan/dsh-compaction-adaptive
|
|
48
|
+
|
|
49
|
+
# 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.1.2
|
|
51
|
+
|
|
52
|
+
# or a local checkout, while developing
|
|
53
|
+
dsh plugin --profile desktop add D:\path\to\dsh-compaction-adaptive
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`dsh plugin` forwards to the bundled pnpm, so a git install is pinned to a commit
|
|
57
|
+
in the profile lockfile; `dsh plugin --profile desktop update` moves it forward.
|
|
58
|
+
|
|
59
|
+
The same URLs work in **Plugins → Add plugin**, where picking a local directory is
|
|
60
|
+
also accepted.
|
|
61
|
+
|
|
62
|
+
Mount it inside the Agent preset's `compaction` group in place of
|
|
63
|
+
`compaction-basic` (one backend per realm):
|
|
64
|
+
|
|
65
|
+
```yaml
|
|
66
|
+
- id: compaction
|
|
67
|
+
name: cordis:group
|
|
68
|
+
group: true
|
|
69
|
+
isolate:
|
|
70
|
+
compaction: true
|
|
71
|
+
toolResultPruner: true
|
|
72
|
+
config:
|
|
73
|
+
- id: compaction-adaptive
|
|
74
|
+
name: 'dsh-compaction-adaptive'
|
|
75
|
+
config:
|
|
76
|
+
mode: adaptive
|
|
77
|
+
profile: balanced
|
|
78
|
+
summarizationProvider: deepseek-account
|
|
79
|
+
summarizationModel: deepseek-flash
|
|
80
|
+
# Optional per-host cost ceiling: adapt within it, never exceed it.
|
|
81
|
+
routes:
|
|
82
|
+
- provider: strata-mi50
|
|
83
|
+
model: deepseek-v4.1-flash-2.25bpw
|
|
84
|
+
ceilRatio: 0.25
|
|
85
|
+
- id: command-compact
|
|
86
|
+
name: '@deepseek-ai/dsh-command-compact'
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Restart the Harness once; the row is composed at boot.
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
`mode: adaptive | fixed` — `fixed` forwards to the base backend's fixed ratios,
|
|
94
|
+
for A/B comparison.
|
|
95
|
+
|
|
96
|
+
`profile: economy | balanced | quality` — multiplies `turnsAhead`, the retained
|
|
97
|
+
tail, and the headroom as a package.
|
|
98
|
+
|
|
99
|
+
`routes[]` — partial adaptive overrides for one `provider`/`model`, typically a
|
|
100
|
+
cost ceiling (`ceilRatio`, `profile`).
|
|
101
|
+
|
|
102
|
+
Chunking — `chunking: auto | off`, `chunkSizing: adaptive | fixed`,
|
|
103
|
+
`chunkSingleShotRatio`, `chunkTokenRatio`, `chunkTriggerBytes`, `chunkBytes`,
|
|
104
|
+
`chunkConcurrency`, `chunkMaxTokens`, `mergeMaxTokens`, `chunkRetries`,
|
|
105
|
+
`chunkShrinkAttempts`, `chunkShrinkFactor`, `chunkRecoverFactor`,
|
|
106
|
+
`chunkMinScale`.
|
|
107
|
+
|
|
108
|
+
Every field, the planning formula, the learn/recover loop, and the log lines are
|
|
109
|
+
documented in [`docs/DETAIL.md`](docs/DETAIL.md).
|
|
110
|
+
|
|
111
|
+
## Verify
|
|
112
|
+
|
|
113
|
+
Checks run without a Harness process:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
node test/policy.test.mjs # threshold / tail planning
|
|
117
|
+
node test/chunking.test.mjs # packing, projection, prompts, concurrency
|
|
118
|
+
node test/adaptive-chunking.test.mjs # model-derived budgets, shrink/recover
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
At runtime:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
[compaction-adaptive] adaptive mode, balanced profile, 3 turn(s) of headroom, tail >= 4096 tokens, chunking auto/adaptive
|
|
125
|
+
[compaction-adaptive] deepseek-account/deepseek-flash window=1048576 budget=1015808 growth=12400 threshold=576716 (55.0%) tail=104038
|
|
126
|
+
[compaction-adaptive] chunked summary (20043 tokens > 12822 budget): 2 parts <= 8243 tokens / 700000 B each, concurrency 2, capacity scale 100%
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Scope
|
|
130
|
+
|
|
131
|
+
Reuses `compaction-basic`'s protocol, so session replay, summary checkpoints,
|
|
132
|
+
the busy lock, overflow recovery, and `/compact` are unchanged. It does not
|
|
133
|
+
change which surface range is compacted, and it drops reasoning blocks from
|
|
134
|
+
digested parts.
|
|
135
|
+
|
|
136
|
+
## Files
|
|
137
|
+
|
|
138
|
+
| File | Role |
|
|
139
|
+
| --- | --- |
|
|
140
|
+
| `lib/policy.js` | Pure planning math (window, growth, threshold, tail); no Cordis dependency |
|
|
141
|
+
| `lib/chunking.js` | Budget derivation, packing, text projection, digest/fold prompts, bounded concurrency |
|
|
142
|
+
| `lib/index.js` | The backend: observes, plans, installs the per-model policy, summarizes, delegates |
|
|
143
|
+
| `cordis.patch.yml` | Bundle layer; explains why the row is mounted by the preset |
|
|
144
|
+
| `docs/DETAIL.md` | Configuration, log lines, limits |
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Bundle layer for dsh-compaction-adaptive.
|
|
2
|
+
#
|
|
3
|
+
# The plugin is a compaction backend (`ctx.compaction`). Exactly one backend may
|
|
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:
|
|
7
|
+
#
|
|
8
|
+
# - id: compaction-adaptive
|
|
9
|
+
# name: 'dsh-compaction-adaptive'
|
|
10
|
+
# config:
|
|
11
|
+
# mode: adaptive
|
|
12
|
+
#
|
|
13
|
+
# This layer therefore inserts nothing by itself: a second Host-level backend
|
|
14
|
+
# would run beside the preset's engine and compact the same session twice.
|
|
15
|
+
# Installing the bundle makes the package resolvable and keeps it in the
|
|
16
|
+
# profile; the preset row above is the mount point.
|
|
17
|
+
[]
|
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# 自适应上下文压缩
|
|
2
|
+
|
|
3
|
+
一个压缩后端(`ctx.compaction`):它把 **何时压缩** 和 **压缩后保留多少近期历史** 从固定比例改成按模型现场推算。
|
|
4
|
+
|
|
5
|
+
它复用了 DSH 自带的 `@deepseek-ai/dsh-compaction-basic` 的全部持久化协议(`compaction/start` 锁、重放、单条摘要替换、失败重试、溢出恢复),只替换数字策略,所以会话日志、回放、fork、`/compact` 的行为完全不变。
|
|
6
|
+
|
|
7
|
+
## 它为什么需要"自适应"
|
|
8
|
+
|
|
9
|
+
原后端按窗口比例算预算:`thresholdTokens = min(窗口 × thresholdRatio, 消息预算 − headroomTokens)`,保留尾窗按 `retainRatio` 或绝对值算。同一组比例放在不同窗口上会得出很不一样的体感:
|
|
10
|
+
|
|
11
|
+
- 1M 窗口 + 0.8 → 800K 才压缩,单次 prefill 极贵;
|
|
12
|
+
- 128K 窗口 + 0.8 → 100K 就压缩,但尾窗只剩 20K,刚聊完的上下文容易被切掉。
|
|
13
|
+
|
|
14
|
+
按模型逐个写 `modelPolicies` 能治,但要靠手调,且模型一换、会话增长方式一变就失准。
|
|
15
|
+
|
|
16
|
+
## 每步实际算的东西
|
|
17
|
+
|
|
18
|
+
每次进入自动压缩判定(`agent/pre-step` 的压力检查、切换模型时的预算复核、上下文溢出恢复)前:
|
|
19
|
+
|
|
20
|
+
1. 读当前路由模型的真实窗口:`llm.resolveModelInfo(provider, model)` → `context.contextWindow`;同时扣掉该请求预留的输出(`header.config.maxTokens`,未声明则用适配器默认值)。
|
|
21
|
+
2. 用 `ctx.tokenMeter.measure()` 现价本会话的表层,和上一次观测相减得到"这一轮新长了多少 token",折进指数滑动平均 `growth`(`growthAlpha`,默认 0.35;负增长视为压过了/裁剪过,只重新定基准,不污染均值)。
|
|
22
|
+
3. 推算触发点:目标是在**按当前速度再聊 `turnsAhead` 轮**之前就压缩,也就是
|
|
23
|
+
`headroom = clamp(growth × turnsAhead, 窗口 × headroomMinRatio, 窗口 × headroomMaxRatio)`,
|
|
24
|
+
`thresholdTokens = 消息预算 − headroom`,再夹在 `floorRatio ~ ceilRatio` 之间。
|
|
25
|
+
4. 推算尾窗:至少保留 `growth × keepTurns`(刚跑完的一轮别被切掉)、至少 `tailMinTokens`,上限 `tailMaxRatio × 消息预算` 且不超过阈值的 60%。
|
|
26
|
+
5. 把这份结果写成该 `provider/model` 的策略条目(保留你为该模型显式配置的摘要路由等字段),再交给原后端照常执行。
|
|
27
|
+
|
|
28
|
+
防抖:刚压缩完的下一步,若表层还没回到 `threshold × thrashMargin`(默认 1.15),这一步不压,避免"压一压、涨一点、再压一压"。
|
|
29
|
+
|
|
30
|
+
## 配置
|
|
31
|
+
|
|
32
|
+
行挂在 Agent 预设的 `compaction` 组里,替换原来的 `compaction-basic`:
|
|
33
|
+
|
|
34
|
+
```yaml
|
|
35
|
+
- id: compaction-adaptive
|
|
36
|
+
name: 'dsh-compaction-adaptive'
|
|
37
|
+
config:
|
|
38
|
+
mode: adaptive # adaptive(默认)| fixed(完全等于原后端行为)
|
|
39
|
+
profile: balanced # economy | balanced | quality
|
|
40
|
+
turnsAhead: 3 # 想给"再聊几轮"留出余量
|
|
41
|
+
growthAlpha: 0.35 # 增长速度的 EMA 权重
|
|
42
|
+
floorRatio: 0.35 # 触发点下限(占窗口)
|
|
43
|
+
ceilRatio: 0.9 # 触发点上限(占窗口)
|
|
44
|
+
headroomMinRatio: 0.06 # 余量下限(占窗口)
|
|
45
|
+
headroomMaxRatio: 0.4 # 余量上限(占窗口)
|
|
46
|
+
tailRatio: 0.1 # 尾窗基准(占消息预算)
|
|
47
|
+
tailMinTokens: 4096 # 尾窗绝对下限
|
|
48
|
+
tailMaxRatio: 0.22 # 尾窗上限
|
|
49
|
+
keepTurns: 1 # 至少保证"一轮"的 token 量留在原文
|
|
50
|
+
cooldownSteps: 1 # 压缩后至少隔几步再评估
|
|
51
|
+
thrashMargin: 1.15 # 冷却期允许压缩的额外余量
|
|
52
|
+
verbose: false # true 时每步都打印推算结果
|
|
53
|
+
# 以下字段与 compaction-basic 同名同义,作为回退值与摘要控制
|
|
54
|
+
summarizationProvider: deepseek-account
|
|
55
|
+
summarizationModel: deepseek-flash
|
|
56
|
+
maxTokens: 12000
|
|
57
|
+
compactionRetries: 2
|
|
58
|
+
maxOverflowRetries: 2
|
|
59
|
+
modelPolicies:
|
|
60
|
+
- provider: strata-local
|
|
61
|
+
model: iq3_xxs
|
|
62
|
+
summarizationProvider: strata-local
|
|
63
|
+
summarizationModel: iq3_xxs
|
|
64
|
+
# 按主机设"成本上限":本机 prefill 慢,窗口再大也别顶满
|
|
65
|
+
routes:
|
|
66
|
+
- provider: strata-mi50
|
|
67
|
+
model: deepseek-v4.1-flash-2.25bpw
|
|
68
|
+
ceilRatio: 0.25 # 1M 窗口 → 262K 就压
|
|
69
|
+
profile: economy
|
|
70
|
+
# 单次摘要请求过大时分块:预算由"摘要模型自己的窗口"推算,不是写死的字节数
|
|
71
|
+
chunking: auto
|
|
72
|
+
chunkSizing: adaptive
|
|
73
|
+
chunkSingleShotRatio: 0.7 # 一次发送最多占摘要模型可用容量的 70%
|
|
74
|
+
chunkTokenRatio: 0.45 # 每块最多占 45%
|
|
75
|
+
chunkTriggerBytes: 2400000 # 传输层兜底(字节),与此同效的还有 token 预算
|
|
76
|
+
chunkBytes: 700000 # 每块字节上限
|
|
77
|
+
chunkConcurrency: 2
|
|
78
|
+
chunkMaxTokens: 4000 # 每块笔记的输出上限
|
|
79
|
+
mergeMaxTokens: 6000 # 折叠检查点的输出上限
|
|
80
|
+
chunkRetries: 1 # 单次调用的重试次数
|
|
81
|
+
chunkShrinkAttempts: 2 # 被判定"请求太大"后整体缩容重排的次数
|
|
82
|
+
chunkShrinkFactor: 0.6 # 每次缩容比例
|
|
83
|
+
chunkRecoverFactor: 1.15 # 成功后逐步恢复比例
|
|
84
|
+
chunkMinScale: 0.25 # 缩容下限
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`modelPolicies` 与 `routes` 分工不同,不要混:
|
|
88
|
+
|
|
89
|
+
| 字段 | 归属 | 作用 |
|
|
90
|
+
| --- | --- | --- |
|
|
91
|
+
| `modelPolicies` | 被复用引擎 | 摘要路由/摘要上限等原样字段,自适应不会覆盖 |
|
|
92
|
+
| `routes` | 本插件 | 只放自适应参数(`ceilRatio`、`profile`、`turnsAhead`…),可只写想改的键 |
|
|
93
|
+
|
|
94
|
+
`routes` 里只写一部分键时,其余键继续用顶层自适应设置;若只把 `ceilRatio` 压得比默认 `floorRatio`(0.35)还低,下限会自动收到上限处,不会因为"下限高于上限"报错。
|
|
95
|
+
|
|
96
|
+
## profile 三档
|
|
97
|
+
|
|
98
|
+
`profile` 是三档预设乘数,不想逐个调字段时用:
|
|
99
|
+
|
|
100
|
+
| profile | turnsAhead 乘数 | 尾窗乘数 | 余量乘数 |
|
|
101
|
+
| --- | --- | --- | --- |
|
|
102
|
+
| economy | 0.6 | 0.6 | 0.9 |
|
|
103
|
+
| balanced | 1 | 1 | 1 |
|
|
104
|
+
| quality | 1.75 | 1.4 | 1.15 |
|
|
105
|
+
|
|
106
|
+
`mode: fixed` 时插件只做转发,行为与 `compaction-basic` 一致,方便逐模型对比。
|
|
107
|
+
|
|
108
|
+
## 怎么确认它在工作
|
|
109
|
+
|
|
110
|
+
启动日志(插件激活时):
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
[compaction-adaptive] adaptive mode, balanced profile, 3 turn(s) of headroom, tail >= 4096 tokens
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
每次自动判定(`verbose: true` 或发生压缩时):
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
[compaction-adaptive] strata-local/iq3_xxs window=524288 budget=491520 growth=12400 threshold=403200 (76.9%) tail=39321
|
|
120
|
+
[compaction-adaptive] shadowed 14 nodes (~268430 tokens)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
压缩本身仍由原后端写下 `compaction/*` 事件,`/compact` 的手动路径不变。
|
|
124
|
+
|
|
125
|
+
## 分块摘要(单次请求太大时)
|
|
126
|
+
|
|
127
|
+
自适应决定的是**何时压**;这条决定的是**压不完怎么办**。
|
|
128
|
+
|
|
129
|
+
### 预算从哪来
|
|
130
|
+
|
|
131
|
+
不写死字节数,而是问"要来做摘要的那个模型一次能吃多少":
|
|
132
|
+
|
|
133
|
+
1. `llm.resolveModelInfo(摘要路由)` 拿到它的真实窗口;
|
|
134
|
+
2. 扣掉它为本请求预留的输出(`maxTokens`)和固定开销(系统提示 + 折叠指令 + 工具 schema 的 token 估计);
|
|
135
|
+
3. 剩下的容量里,一次发送最多用 `chunkSingleShotRatio`(默认 70%),每块最多用 `chunkTokenRatio`(默认 45%);
|
|
136
|
+
4. 字节上限(`chunkTriggerBytes` / `chunkBytes`)作为**传输层**的第二道闸:token 与字节任一越线都算超限。
|
|
137
|
+
|
|
138
|
+
token 估计取「tokenMeter 启发式」与「UTF-8 字节 ÷ 3」的较大者:前者按字符密度算,遇到中文会低估约 3 倍;后者对中文精确、对 ASCII 只悲观约 1/3,是"决定能不能装下"时该偏的方向。
|
|
139
|
+
|
|
140
|
+
### 学习曲线
|
|
141
|
+
|
|
142
|
+
每条摘要路由有一个容量乘数(初始 1.0):
|
|
143
|
+
|
|
144
|
+
- 请求被判定为"太大"(`CONTEXT_WINDOW_EXCEEDED`、`MAX_TOKENS`、`413`、`payload too large` 等)→ 乘数 × `chunkShrinkFactor`(默认 0.6,下限 `chunkMinScale` 0.25),按更小的块**重排重试**,最多 `chunkShrinkAttempts` 次;
|
|
145
|
+
- 成功一次 → 乘数 × `chunkRecoverFactor`(默认 1.15,上限 1.0),逐步恢复到模型声明的容量。
|
|
146
|
+
|
|
147
|
+
所以它不是"猜一次",而是跟着真实反馈收敛:小窗口本地模型第一次就会分得很细,大窗口云端模型继续一次性发送;某个中转悄悄收紧限额时,下一次压缩会自动变小并记住。
|
|
148
|
+
|
|
149
|
+
### 流程
|
|
150
|
+
|
|
151
|
+
1. 把被压缩区间投影成文本(丢掉 reasoning 块),按当前乘数下的 token / 字节双预算装箱;
|
|
152
|
+
2. 每块一次调用产出该段工程笔记(`chunkMaxTokens`,并发 `chunkConcurrency`,失败按 `chunkRetries` 重试);
|
|
153
|
+
3. 笔记本身若超过折叠预算,先分组再摘要一轮(最多 3 轮),最后折叠成一份符合 harness 结构的检查点(`mergeMaxTokens`)。
|
|
154
|
+
|
|
155
|
+
调用次数 > 1 时返回值里**不带** `llmStreamCall` 标记(该标记的语义就是"恰好一次 `ctx.llm.stream()` 调用"),落盘记录只保留 `rawOutput`,所以日志、回放、`/compact` 的语义都不变。
|
|
156
|
+
|
|
157
|
+
日志:
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
[compaction-adaptive] chunked summary (20043 tokens > 12822 budget): 2 parts <= 8243 tokens / 700000 B each, concurrency 2, capacity scale 100%
|
|
161
|
+
[compaction-adaptive] sum/sum-small rejected a chunked request (chunk summarization: the maximum context length is 32768 tokens); shrinking capacity to 60% and retrying
|
|
162
|
+
[compaction-adaptive] chunked summary done: 3 digests folded into 3120 chars
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`chunking: off` 关掉这条路径,永远单次发送;`chunkSizing: fixed` 则退回"把整段容量当预算"的老口径(不做 70%/45% 缩放),便于对比。
|
|
166
|
+
|
|
167
|
+
## 边界
|
|
168
|
+
|
|
169
|
+
- 窗口未知的路由(适配器没有声明 `contextWindow`)不参与 token 推算:只剩字节闸门决定是否分块,不会凭空造出一个 token 上限。
|
|
170
|
+
- 分块只解决"摘要请求太大",不改变区间选择:单个不可分的表层节点(一张巨大的工具结果、一个超大的非工具消息)仍然压不下——那是 `compaction-basic` 的范围规则,工具结果裁剪(`tool-result-pruner`)仍然先行。
|
|
171
|
+
- 分块摘要会丢弃 reasoning 块(不发给摘要模型);需要保留推理过程的场景请用 `chunking: off` 并自行控制上限。
|
|
172
|
+
- 容量乘数是运行期学习状态,不写日志,重启后从 1.0 重新收敛(第一次略保守或略乐观,最多多试一轮)。
|
|
173
|
+
- 本插件占用 `ctx.compaction`,一个 realm 只能挂一个压缩后端:替换该行即等于停用原后端,不要同时挂两个。
|
|
174
|
+
- 会话内观测(增长速度)是派生缓存,不写日志,重启后从零开始重新估计(前几步回落到 `headroomMinRatio` 的保守值)。
|
package/icon.svg
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Adaptive context compaction">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="frame" x1="8" y1="8" x2="56" y2="56" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop offset="0" stop-color="#6AA6FF"/>
|
|
5
|
+
<stop offset="1" stop-color="#2F6BE0"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="bar" x1="17" y1="0" x2="45" y2="0" gradientUnits="userSpaceOnUse">
|
|
8
|
+
<stop offset="0" stop-color="#9CC6FF"/>
|
|
9
|
+
<stop offset="1" stop-color="#4C8DFF"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
</defs>
|
|
12
|
+
<rect x="7.5" y="7.5" width="49" height="49" rx="13" fill="none" stroke="url(#frame)" stroke-width="3.5"/>
|
|
13
|
+
<rect x="17" y="17" width="22" height="4.5" rx="2.25" fill="url(#bar)"/>
|
|
14
|
+
<rect x="17" y="25" width="17" height="4.5" rx="2.25" fill="url(#bar)" opacity="0.78"/>
|
|
15
|
+
<rect x="17" y="33" width="13" height="4.5" rx="2.25" fill="url(#bar)" opacity="0.56"/>
|
|
16
|
+
<rect x="17" y="41" width="9.5" height="4.5" rx="2.25" fill="url(#bar)" opacity="0.36"/>
|
|
17
|
+
<path d="M22 49.5h20" stroke="#2F6BE0" stroke-width="3.2" stroke-linecap="round" opacity="0.55"/>
|
|
18
|
+
</svg>
|