@magic6/dsh-compaction-policy-engine 0.0.0-stage → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/LICENSE +21 -0
- package/README.md +353 -2
- package/README.zh-CN.md +308 -0
- package/cordis.patch.yml +61 -0
- package/docs/DESIGN.md +161 -0
- package/examples/standard-with-policy/cordis.patch.yml +139 -0
- package/examples/standard-with-policy/package.json +12 -0
- package/lib/client-policy.js +192 -0
- package/lib/client.js +1791 -0
- package/lib/companion.js +13 -0
- package/lib/engine.js +19 -0
- package/lib/index.js +1384 -0
- package/lib/policy.js +608 -0
- package/lib/settings-file.js +396 -0
- package/lib/settings.js +138 -0
- package/package.json +70 -4
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# dsh-compaction-policy-engine
|
|
2
|
+
|
|
3
|
+
[English](https://github.com/T-splines/dsh-compaction-policy-engine/blob/main/README.md) | **简体中文**
|
|
4
|
+
|
|
5
|
+
**DeepSeek Harness 的按模型上下文压缩策略——三层阈值、热加载与摘要模型选择,构建在官方压缩引擎之上。**
|
|
6
|
+
|
|
7
|
+
官方 `@deepseek-ai/dsh-compaction-basic` 已经支持阈值、摘要模型,以及通过 `modelPolicies`
|
|
8
|
+
给精确 `(provider, model)` 覆盖。本插件是它的子类,在它之上再加一层:
|
|
9
|
+
|
|
10
|
+
- **供应商层**——一条规则覆盖该 provider 后面的所有模型,不必逐个枚举模型 id;
|
|
11
|
+
- **逐字段继承**——模型层只覆盖 `threshold`,`retain`、`maxTokens`、摘要路由仍从供应商层或
|
|
12
|
+
全局层继承;
|
|
13
|
+
- **比例与绝对 token 双单位**,按路由声明的窗口换算;
|
|
14
|
+
- **热加载 + 设置面板 + 会话徽标**;
|
|
15
|
+
- **摘要回退链**与上下文策略提示词。
|
|
16
|
+
|
|
17
|
+
选区、工具配对平衡、摘要框架、落盘事务全部沿用官方实现。如果你只需要少量精确
|
|
18
|
+
`(provider, model)` 覆盖,官方 `modelPolicies` 已经够用;本插件面向的是跨很多路由的分层策略。
|
|
19
|
+
|
|
20
|
+
- [为什么需要它](#为什么需要它)
|
|
21
|
+
- [差异点](#差异点)
|
|
22
|
+
- [安装与挂载](#安装与挂载)
|
|
23
|
+
- [配置](#配置)
|
|
24
|
+
- [策略如何解析](#策略如何解析)
|
|
25
|
+
- [设置面板](#设置面板)
|
|
26
|
+
- [行为保证](#行为保证)
|
|
27
|
+
- [测试](#测试)
|
|
28
|
+
- [兼容与迁移](#兼容与迁移)
|
|
29
|
+
|
|
30
|
+
## 为什么需要它
|
|
31
|
+
|
|
32
|
+
官方引擎的 `modelPolicies` 是精确 `(provider, model)` 对。当一个网关暴露很多模型 id,或者
|
|
33
|
+
多条路由共享同一套策略时,逐个枚举既啰嗦又脆弱:新加的模型会静默落回全局阈值,直到有人补上
|
|
34
|
+
一行。本插件允许一条供应商层规则覆盖该 provider 的所有模型,并按
|
|
35
|
+
`models[] → providers{} → default` 逐字段解析。
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
models[] → providers{} → default
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
模型层可以只覆盖 `threshold`,`retain`、`maxTokens`、`headroomTokens` 和摘要路由继续从供应商
|
|
42
|
+
层或全局层继承。
|
|
43
|
+
|
|
44
|
+
## 差异点
|
|
45
|
+
|
|
46
|
+
- **三层策略、逐字段。** `models[]` → `providers{}` → `default`;改一个字段不需要抄整层,
|
|
47
|
+
一条供应商行覆盖该 provider 后面的所有模型。
|
|
48
|
+
- **比例或绝对 token 两种单位。** `threshold` / `retain` 按路由声明的窗口换算;
|
|
49
|
+
`thresholdTokens` / `retainTokens` 是绝对预算。换算写回父类时使用
|
|
50
|
+
`(tokens + 0.5) / window`,保证精确回到声明的数;路由没有声明窗口时**拒绝猜**,
|
|
51
|
+
在真正需要策略的那一步报错并点名路由。
|
|
52
|
+
- **热加载。** 引擎每秒轮询 `settings.yaml` 与 `compaction-control-settings` 行;策略修改在
|
|
53
|
+
下一次压缩生效,不需要重启。面板写的是同一份文档。引擎挂载行自己的 `config:` 在加载时读取
|
|
54
|
+
(见[安装与挂载](#安装与挂载))。
|
|
55
|
+
- **部分文档合并。** `settings.yaml` 的 `compaction-control:` 段与挂载行 `config:` **逐字段**
|
|
56
|
+
合并(映射递归、数组整体替换),面板保存不会悄悄抹掉 profile 里的 `models[]` 层。
|
|
57
|
+
- **设置面板。** 编辑全局层或某个供应商层,切换单位时不留下两个键,空隙过小时给出红字提示;
|
|
58
|
+
会话头徽标显示当前触发点和「上一次摘要实际由哪条路由写」。
|
|
59
|
+
- **摘要回退链。** `summarizationFallback` 在主摘要路由失败时按序尝试其它路由,避免一条坏
|
|
60
|
+
路由让整个压缩停摆。
|
|
61
|
+
- **上下文策略提示词。** companion 注册一段系统提示:上下文管理是自动的,模型无法测量自己的
|
|
62
|
+
用量,不得因为自认为「上下文快满了」而停止、缩短或移交工作。
|
|
63
|
+
- **手动 `/compact` 是持久工作。** 客户端断线/重连不会杀死一次 20–40 秒的压缩事务;无法开始
|
|
64
|
+
时如实报告原因,不把空锁说成「已有压缩在跑」。
|
|
65
|
+
- **有测试。** 198 个 `node --test` 用例,分布在 13 个文件,覆盖真实 bundle 渲染、三层解析、
|
|
66
|
+
单位换算、补丁文件解析和手动压缩保留量。
|
|
67
|
+
|
|
68
|
+
## 安装与挂载
|
|
69
|
+
|
|
70
|
+
要求:DSH `0.2.2-rc.x`、Node.js 20+。
|
|
71
|
+
|
|
72
|
+
### 从 npm 安装
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
dsh plugin --profile web add @magic6/dsh-compaction-policy-engine
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
npm 包里包含 `examples/standard-with-policy` 供参考;下面的 preset 覆盖规则仍然适用。
|
|
79
|
+
web 小节里的 `$PWD` 命令假设使用源码检出。
|
|
80
|
+
|
|
81
|
+
### 从源码安装
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
git clone https://github.com/T-splines/dsh-compaction-policy-engine.git
|
|
85
|
+
cd dsh-compaction-policy-engine
|
|
86
|
+
dsh plugin --profile web add "$PWD"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
包本身是 bundle,会挂两行:
|
|
90
|
+
|
|
91
|
+
- **引擎行**(`compaction-control`),**默认 disabled**;
|
|
92
|
+
- **companion 行**(`compaction-control-settings`),始终启用,负责设置卡片与上下文提示词;
|
|
93
|
+
它**不**认领 `ctx.compaction`。
|
|
94
|
+
|
|
95
|
+
### 先确定挂载平面
|
|
96
|
+
|
|
97
|
+
`ctx.compaction` 是**按 realm 解析的单槽接缝**。必须挂在 agent 实际解析到的作用域;挂在别处
|
|
98
|
+
不是「覆盖」而是「并存」——两个引擎都订阅冒泡的 `agent/pre-step`,触发点变成两者中较低的那个,
|
|
99
|
+
表现为「装完插件后压缩异常地早」。
|
|
100
|
+
|
|
101
|
+
| profile | agent 的压缩引擎属于谁 | 怎么挂 |
|
|
102
|
+
|:--|:--|:--|
|
|
103
|
+
| `headless` | 宿主平面(没有 preset) | profile 的 `cordis.patch.yml`,覆盖 bundle 已插入的引擎行 |
|
|
104
|
+
| `web` | **agent preset**(`dsh-web-app` 停用了宿主平面的 `compaction-basic`) | 覆盖 preset 声明;本仓库带一个示例 bundle |
|
|
105
|
+
|
|
106
|
+
#### headless(宿主平面)
|
|
107
|
+
|
|
108
|
+
bundle 已经插入 `compaction-control`(disabled)。**覆盖这一行**,不要再 `insert` 一份:
|
|
109
|
+
|
|
110
|
+
```yaml
|
|
111
|
+
# $DSH_HOME/profiles/<profile>/cordis.patch.yml
|
|
112
|
+
- id: compaction-basic
|
|
113
|
+
disabled: true
|
|
114
|
+
|
|
115
|
+
- id: compaction-control
|
|
116
|
+
disabled: false
|
|
117
|
+
config:
|
|
118
|
+
default:
|
|
119
|
+
threshold: 0.8
|
|
120
|
+
retain: 0.16
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
loader 认的键是 **`disabled`**,不是 `disable`;写错不会报错,会被静态配置静默丢掉
|
|
124
|
+
(看起来停用了,其实还启用)。
|
|
125
|
+
|
|
126
|
+
#### web(agent preset)
|
|
127
|
+
|
|
128
|
+
DSH 0.2.2 的 preset 是 bundle patch 里的 `@deepseek-ai/dsh-agent-preset` 声明。旧目录
|
|
129
|
+
`$DSH_HOME/.agent-presets/<id>/agent.cordis.yml` 已经不再被读取。web 的 agent realm 隔离了
|
|
130
|
+
`ctx.compaction`,宿主平面的引擎它看不到。
|
|
131
|
+
|
|
132
|
+
本仓库带一个示例 bundle:覆盖 `preset-standard`,把 `compaction` 组里的 `compaction-basic`
|
|
133
|
+
换成 `compaction-control`,并保留 `command-compact` 与 `tool-result-pruner`:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
dsh plugin --profile web add "$PWD"
|
|
137
|
+
dsh plugin --profile web add "$PWD/examples/standard-with-policy"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
重启 web app,新开会话验证。示例是 DSH 0.2.2 标准 preset 的快照;如果你改过标准 preset,请把
|
|
141
|
+
`compaction` 组的手动合并进你自己的 preset 声明,而不是盲目安装示例(Creator 模式可以代写
|
|
142
|
+
这个覆盖)。文件在
|
|
143
|
+
[`examples/standard-with-policy/cordis.patch.yml`](examples/standard-with-policy/cordis.patch.yml),
|
|
144
|
+
头部也写了同样的提醒。
|
|
145
|
+
|
|
146
|
+
两种静默坏组合:
|
|
147
|
+
|
|
148
|
+
- 宿主平面挂了引擎,preset 里原版 `compaction-basic` 还在 → 两个引擎,阈值低的赢;
|
|
149
|
+
- 原版停用了却没有挂任何替代引擎 → 接缝无人认领,会话静默停止压缩,一路涨到被上游拒绝。
|
|
150
|
+
|
|
151
|
+
「停用旧的」和「启用新的」必须写在同一个组合里,一起 review。
|
|
152
|
+
|
|
153
|
+
### 热加载范围
|
|
154
|
+
|
|
155
|
+
设置面板写 `$DSH_HOME/settings.yaml`。引擎每秒轮询该文件**以及** `compaction-control-settings`
|
|
156
|
+
行,所以面板修改和这一行的修改会在下一次压缩生效,不需要重启。**引擎挂载行**
|
|
157
|
+
(`compaction-control`)的 `config:` 在插件加载时读取,改它需要重启。
|
|
158
|
+
|
|
159
|
+
### 阈值语义
|
|
160
|
+
|
|
161
|
+
官方引擎的触发点是
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
floor(min(W × threshold, W − O − headroomTokens))
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
其中 `W` 是路由声明的窗口,`O` 是输出预留,`headroomTokens` 默认 65,536。`measured`
|
|
168
|
+
**包含固定的系统提示词和工具清单**。在工具很多的部署里,`threshold: 0.5` 可能意味着
|
|
169
|
+
「系统提示词之外再攒约 23K 就压」,不是「对话到一半才压」。
|
|
170
|
+
|
|
171
|
+
本插件的 `threshold` / `retain` 映射到官方的 `thresholdRatio` / `retainRatio`;
|
|
172
|
+
`headroomTokens` 与 `maxTokens` 是逐层字段。默认 0.8 是官方引擎默认值,除非 headroom 上限
|
|
173
|
+
先到,否则不会比原版更早触发。会话头徽标显示的是 `窗口 × threshold` 的快速预览;因为
|
|
174
|
+
`W − O − headroomTokens` 可能更低,把它当作触发点的**上界**。
|
|
175
|
+
|
|
176
|
+
## 配置
|
|
177
|
+
|
|
178
|
+
策略可以写在挂载行的 `config:`,也可以写在 `$DSH_HOME/settings.yaml` 的
|
|
179
|
+
`compaction-control:` 段(面板编辑后者)。
|
|
180
|
+
|
|
181
|
+
```yaml
|
|
182
|
+
compaction-control:
|
|
183
|
+
default:
|
|
184
|
+
threshold: 0.8 # 到达窗口的这个比例就压
|
|
185
|
+
retain: 0.16 # 原样保留的尾部比例,必须小于 threshold
|
|
186
|
+
summarizationModel: auto # auto = 用会话当前路由的模型,或 "provider/model"
|
|
187
|
+
summarizationFallback: [] # 主摘要失败后按序尝试的 "provider/model" 路由
|
|
188
|
+
headroomTokens: 65536 # 阈值下方预留的空间
|
|
189
|
+
maxTokens: 8192 # 摘要长度上限
|
|
190
|
+
compactionRetries: 1
|
|
191
|
+
maxOverflowRetries: 1
|
|
192
|
+
|
|
193
|
+
providers:
|
|
194
|
+
some-provider:
|
|
195
|
+
threshold: 0.5
|
|
196
|
+
|
|
197
|
+
models:
|
|
198
|
+
- provider: local
|
|
199
|
+
model: model-262k
|
|
200
|
+
threshold: 0.9
|
|
201
|
+
|
|
202
|
+
auto: true
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### 两种单位
|
|
206
|
+
|
|
207
|
+
每个预算字段都有比例与绝对 token 两种写法,**同一层只能写一种**:
|
|
208
|
+
|
|
209
|
+
| 比例 | 绝对 token | 语义 |
|
|
210
|
+
|:--|:--|:--|
|
|
211
|
+
| `threshold` | `thresholdTokens` | 到达这个量就压 |
|
|
212
|
+
| `retain` | `retainTokens` | 保留最近这么多内容 |
|
|
213
|
+
|
|
214
|
+
同一层两种都写时 token 形式生效,并打一行 `[compaction] WARNING` 点名被遮蔽的键。整段拒绝
|
|
215
|
+
会让面板刚保存的值看起来被静默忽略,所以选择「生效 + 告警」。
|
|
216
|
+
|
|
217
|
+
### 其它策略字段
|
|
218
|
+
|
|
219
|
+
以下字段在 `default`、`providers{}` 层或 `models[]` 层都有效:
|
|
220
|
+
|
|
221
|
+
| 字段 | 默认值 | 含义 |
|
|
222
|
+
|:--|:--|:--|
|
|
223
|
+
| `summarizationModel` | `auto` | `auto` 复用语会话当前路由;也可写明确的 `provider/model` |
|
|
224
|
+
| `summarizationFallback` | `[]` | 主摘要失败后按序尝试的 `provider/model` 路由 |
|
|
225
|
+
| `headroomTokens` | `65536`(官方默认) | 阈值下方预留的 token,给摘要留出比被压区间更小的空间 |
|
|
226
|
+
| `maxTokens` | 官方默认 | 生成摘要的长度上限 |
|
|
227
|
+
| `compactionRetries` | `1` | 压力仍高于阈值时的额外摘要尝试 |
|
|
228
|
+
| `maxOverflowRetries` | `1` | 确认上下文溢出后的重试次数;`0` 关闭恢复 |
|
|
229
|
+
|
|
230
|
+
`auto` 是根级开关,不是层字段:设为 `false` 只关闭自动压缩,手动 `/compact` 仍然可用。
|
|
231
|
+
|
|
232
|
+
**空隙要够大。** 摘要是结构化总结,实测下限约 3.2K token;`threshold − retain < 8192` 时摘要
|
|
233
|
+
不可能比被压缩的区间更小,父类会拒绝。面板把「可压缩空间」标红提示;小窗口下小空隙可能无法
|
|
234
|
+
避免,所以是告警而不是加载失败。
|
|
235
|
+
|
|
236
|
+
## 策略如何解析
|
|
237
|
+
|
|
238
|
+
- `models[]` → `providers{}` → `default`,**逐字段**解析,按 `(provider, model, window)` 缓存。
|
|
239
|
+
会话中途换模型,下一次门限判断立刻使用新策略。
|
|
240
|
+
- 引擎在门限检查时读取**当时**的路由,而不是会话启动时的路由。
|
|
241
|
+
- 路由没声明窗口时,绝对 token 预算无法兑现:字段进入 `deferred`,真正需要策略的那一步报错
|
|
242
|
+
并点名路由。
|
|
243
|
+
- 配置错误(比例越界、`retain >= threshold`、未知字段、`models[]` 重复目标、
|
|
244
|
+
`summarizationModel` 残缺)在插件加载期失败,消息定位到字段。
|
|
245
|
+
|
|
246
|
+
## 设置面板
|
|
247
|
+
|
|
248
|
+
Models 页脚 → **Compaction policy** 卡片:
|
|
249
|
+
|
|
250
|
+
- 选择层:全局默认或某个供应商路由;
|
|
251
|
+
- 编辑每个预算字段;**数值形状即单位**(`0.2` 比例,`200000` token),非法值标红并禁用保存;
|
|
252
|
+
- 切换单位时在同一批操作里 `unset` 被替换的键,文档不会同时留下两种单位;
|
|
253
|
+
- 新增/移除供应商覆盖;移除会把用户层里的所有字段 unset,包括卡片不渲染的字段;
|
|
254
|
+
- 会话头徽标显示当前触发点(`比例 · token`)、来源层(`models[]` / `providers{}` / `default` /
|
|
255
|
+
引擎默认)、可压缩空间,以及上一次摘要实际使用的路由。
|
|
256
|
+
|
|
257
|
+
`models[]` 仍然只能手写 `settings.yaml`:settings 写入器对数组是整体替换,列表编辑器会在每次
|
|
258
|
+
输入时重写整张表。徽标仍会报告会话命中了 `models[]`。
|
|
259
|
+
|
|
260
|
+
非环回浏览器拿到的是 DSH 的内存态视图(`status: 'unavailable'`、`writable: false`),卡片会
|
|
261
|
+
明说,而不是把插件默认值当成部署策略。
|
|
262
|
+
|
|
263
|
+
## 行为保证
|
|
264
|
+
|
|
265
|
+
- **配置错误在启动时炸,不在第一次压缩时炸。** 例外是需要路由窗口的绝对预算:在真正需要策略
|
|
266
|
+
的那一步报错并点名路由。
|
|
267
|
+
- **覆盖导致 `retain >= threshold` 时报错**,不悄悄缩小 retain。
|
|
268
|
+
- **不劫持压缩语义。** 选区、工具配对、事务落盘一行没改。
|
|
269
|
+
- **手动压缩是持久工作。** `/compact` 不因客户端断线而取消;只有 agent 自己的维护信号能取消。
|
|
270
|
+
Stop 仍然有效。
|
|
271
|
+
- **失败原因不谎报。** 闸门被占时先有界重试一次;始终不释放时明说「上一次活动还在收尾,
|
|
272
|
+
锁是空的」。
|
|
273
|
+
- **上下文策略。** companion 声明:上下文由 harness 自动管理,模型无法测量自身用量,不得因为
|
|
274
|
+
自认为快满而停止或缩短工作。profile 没有 `systemPrompt` 服务时不注册。
|
|
275
|
+
|
|
276
|
+
## 测试
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
npm test
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
198 个用例,分布在 13 个文件,包括:
|
|
283
|
+
|
|
284
|
+
- `manual-compaction.test.js` — 闸门重试、UI 信号中断、真实失败、诚实消息,以及对已安装官方包
|
|
285
|
+
的契约断言;
|
|
286
|
+
- `context-policy.test.js` — 提示词段落注册与无服务时的降级;
|
|
287
|
+
- `policy.test.js` — 三层解析、逐字段合并、两种单位、换算、告警、overlay 文档规则;
|
|
288
|
+
- `engine.test.js` — 接缝注册、`inject` 继承、`Config` 必须自有、按目标切换、窗口换算、
|
|
289
|
+
无窗口拒绝、`settings.yaml` 段存在时 profile 的 `models[]` 仍生效;
|
|
290
|
+
- `settings-file.test.js` — 补丁/settings 解析与即时编辑;
|
|
291
|
+
- `fallback.test.js`、`patch-file.test.js`、`settings.test.js`、`summary-prefix.test.js`、
|
|
292
|
+
`unstated-threshold.test.js` — 摘要回退、补丁 entry 形状、settings 合并、摘要框架与
|
|
293
|
+
未声明阈值告警;
|
|
294
|
+
- `client-policy.test.js` / `client.test.js` — 面板计算、单位切换、供应商层、会话徽标、
|
|
295
|
+
非法值拦截、内联 bundle 一致性。
|
|
296
|
+
|
|
297
|
+
## 兼容与迁移
|
|
298
|
+
|
|
299
|
+
- DSH `0.2.2-rc.x`(`engines.dsh`:`>=0.2.2-0 <0.3.0-0`)、Node.js 20+。
|
|
300
|
+
- **0.5.0 从 `dsh-compaction-control` 改名。** entry id 保持不变:
|
|
301
|
+
`compaction-control`(引擎)、`compaction-control-settings`(companion),以及
|
|
302
|
+
`compaction-control:` 设置命名空间。把 profile/preset 里的 `name:` 改成
|
|
303
|
+
`dsh-compaction-policy-engine`(引擎行加 `/engine`)即可。
|
|
304
|
+
- 仍然是官方 `BasicCompactionEngine` 的子类,不需要改 `node_modules`。
|
|
305
|
+
|
|
306
|
+
## 许可
|
|
307
|
+
|
|
308
|
+
MIT,见 [LICENSE](LICENSE)。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Plugin bundle for dsh-compaction-policy-engine: mounts the policy engine (inert by
|
|
2
|
+
# default) and the always-enabled host-plane companion.
|
|
3
|
+
#
|
|
4
|
+
# The engine subclasses `BasicCompactionEngine`, so region selection, tool-pairing
|
|
5
|
+
# balance, summary framing, and the durable compaction transaction stay the
|
|
6
|
+
# official implementation — only the policy (three-layer threshold + summarization
|
|
7
|
+
# model) becomes configurable here.
|
|
8
|
+
#
|
|
9
|
+
# `ctx.compaction` is a single-slot seam: enabling this row means the stock
|
|
10
|
+
# `compaction-basic` row must be disabled in the same profile. This bundle
|
|
11
|
+
# deliberately does not do that for you, and ships the engine `disabled`:
|
|
12
|
+
# superseding the stock engine is a deployment decision, and a bundle that turned
|
|
13
|
+
# itself on without also re-enabling `compaction-basic` would leave the profile
|
|
14
|
+
# with no compaction at all. Enable both in the same patch:
|
|
15
|
+
#
|
|
16
|
+
# - id: compaction-basic
|
|
17
|
+
# disabled: true
|
|
18
|
+
#
|
|
19
|
+
# - id: compaction-control
|
|
20
|
+
# disabled: false
|
|
21
|
+
# config:
|
|
22
|
+
# default:
|
|
23
|
+
# threshold: 0.8 # ratio of the adapter-declared window
|
|
24
|
+
# summarizationModel: auto # `auto` = the conversation's own model
|
|
25
|
+
# providers:
|
|
26
|
+
# gateway-b:
|
|
27
|
+
# threshold: 0.5
|
|
28
|
+
# models:
|
|
29
|
+
# - provider: local
|
|
30
|
+
# model: model-g
|
|
31
|
+
# threshold: 0.9
|
|
32
|
+
#
|
|
33
|
+
# Thresholds are ratios, never absolute token counts: a model's advertised window
|
|
34
|
+
# is not stable, and a model that declares no window cannot honor a token budget.
|
|
35
|
+
#
|
|
36
|
+
# The companion row below is a **separate, enabled, host-plane** row because the
|
|
37
|
+
# browser bundle is discovered from enabled loader entries (`dsh-client-modules`
|
|
38
|
+
# walks `ctx.loader.entries()` and skips disabled or fiberless ones) — the engine
|
|
39
|
+
# row is disabled by default and normally mounted inside an agent preset, where
|
|
40
|
+
# that scan cannot see it. It deliberately does NOT claim `ctx.compaction`: the
|
|
41
|
+
# seam resolves per realm, so a host-plane engine would run *beside* the preset
|
|
42
|
+
# one on the same bubbling `agent/pre-step` and the lower threshold would win.
|
|
43
|
+
#
|
|
44
|
+
# `name` is the **bare package name**, which resolves to `lib/companion.js`. That
|
|
45
|
+
# split is load-bearing: the loader mounts a row's `exports.default` and reads no
|
|
46
|
+
# entry option, so this row can never mount the engine. (It once carried an
|
|
47
|
+
# `export:` key, which was silently dropped — putting a second engine on the host
|
|
48
|
+
# plane at the shipped default and capping every raised threshold. Two
|
|
49
|
+
# `[compact-debug]` lines per step with the same `measured` were the signature.)
|
|
50
|
+
- insert:
|
|
51
|
+
- id: compaction-control
|
|
52
|
+
name: 'dsh-compaction-policy-engine/engine'
|
|
53
|
+
disabled: true
|
|
54
|
+
|
|
55
|
+
- insert:
|
|
56
|
+
- id: compaction-control-settings
|
|
57
|
+
name: 'dsh-compaction-policy-engine'
|
|
58
|
+
config:
|
|
59
|
+
default:
|
|
60
|
+
threshold: 0.8
|
|
61
|
+
retain: 0.16
|
package/docs/DESIGN.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Design
|
|
2
|
+
|
|
3
|
+
## 1. Scope
|
|
4
|
+
|
|
5
|
+
The plugin adds a policy layer on top of `@deepseek-ai/dsh-compaction-basic`. It does not
|
|
6
|
+
reimplement compaction: `CompactionControlEngine` extends `BasicCompactionEngine`, so region
|
|
7
|
+
selection, tool-pairing balance, summary framing and the durable transaction stay official.
|
|
8
|
+
Only two decisions move into this plugin:
|
|
9
|
+
|
|
10
|
+
1. **when** to compact (threshold, per route);
|
|
11
|
+
2. **which model** writes the summary (including a fallback chain).
|
|
12
|
+
|
|
13
|
+
## 2. Policy model
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
models[] exact (provider, model) overrides; highest priority
|
|
17
|
+
providers{} per-provider overrides
|
|
18
|
+
default global fallback
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Resolution is **per field**. For each budget field, the first layer that defines it wins.
|
|
22
|
+
A model override of `threshold` leaves `retain`, `maxTokens` and `summarizationModel`
|
|
23
|
+
inheriting from the provider or global layer.
|
|
24
|
+
|
|
25
|
+
The result is cached by `(provider, model, window)`. The engine reads the session's **current**
|
|
26
|
+
route at each threshold check, so switching models mid-session switches policy immediately.
|
|
27
|
+
|
|
28
|
+
### Units
|
|
29
|
+
|
|
30
|
+
| ratio form | absolute form | semantics |
|
|
31
|
+
|:--|:--|:--|
|
|
32
|
+
| `threshold` | `thresholdTokens` | compact at this point |
|
|
33
|
+
| `retain` | `retainTokens` | keep this much recent content |
|
|
34
|
+
|
|
35
|
+
The parent engine consumes ratios (`thresholdRatio`) and computes
|
|
36
|
+
`floor(window × ratio)`. To honor an absolute budget exactly, the plugin writes
|
|
37
|
+
`(tokens + 0.5) / window`, so the parent's floor returns the declared token count. Absolute
|
|
38
|
+
`retainTokens` is passed through (the parent uses it as a boundary).
|
|
39
|
+
|
|
40
|
+
If a route declares no context window, an absolute budget cannot be honored. The plugin does
|
|
41
|
+
not guess: the field is deferred and the error names the route when the policy is needed.
|
|
42
|
+
|
|
43
|
+
If both forms appear in one layer, the token form wins and one `[compaction] WARNING` names
|
|
44
|
+
the shadowed key. Rejecting the whole layer would make a just-saved panel value look ignored.
|
|
45
|
+
|
|
46
|
+
**Gap warning.** A structured summary has a practical lower bound of about 3.2K tokens. When
|
|
47
|
+
`threshold − retain < 8192`, the summary cannot be smaller than the content it replaces and
|
|
48
|
+
the parent engine rejects it. This is a warning, not a load error: small windows may make a
|
|
49
|
+
small gap unavoidable.
|
|
50
|
+
|
|
51
|
+
## 3. Configuration sources
|
|
52
|
+
|
|
53
|
+
The engine reads, in order:
|
|
54
|
+
|
|
55
|
+
1. the **mount row's `config:`** (the composition baseline);
|
|
56
|
+
2. `$DSH_HOME/settings.yaml` under `compaction-control:` (the panel's document);
|
|
57
|
+
3. the **profile patch layer** (`cordis.patch.yml` / preset file), which the loader applies
|
|
58
|
+
last and therefore wins.
|
|
59
|
+
|
|
60
|
+
`settings.yaml` is a **partial document**: it is merged per field with the mount config
|
|
61
|
+
(maps recurse, arrays replace wholesale, explicit `undefined` keeps the baseline). Writing a
|
|
62
|
+
section in the file does not discard the profile's `models[]`; that was a real regression and
|
|
63
|
+
now has a dedicated test.
|
|
64
|
+
|
|
65
|
+
The engine polls the sources every second, so policy edits apply without a restart. When the
|
|
66
|
+
file segment exists, it is the effective document for the fields it defines; fields it omits
|
|
67
|
+
still come from the mount row.
|
|
68
|
+
|
|
69
|
+
## 4. Realm and seam rules
|
|
70
|
+
|
|
71
|
+
`ctx.compaction` is a per-realm single slot. The stock web app intentionally disables the
|
|
72
|
+
host-plane `compaction-basic` / `command-compact` / `tool-result-pruner` and moves the
|
|
73
|
+
compaction backend into the agent preset. Therefore:
|
|
74
|
+
|
|
75
|
+
- in `web`, mount the engine **inside the preset**, replacing the stock row in the same
|
|
76
|
+
realm;
|
|
77
|
+
- in a profile without a preset (`headless`), mount it on the host plane and disable
|
|
78
|
+
`compaction-basic` in the same patch.
|
|
79
|
+
|
|
80
|
+
If two engines exist, both subscribe to the bubbling `agent/pre-step`; the lower threshold
|
|
81
|
+
triggers first. If no engine exists, the seam is unclaimed and the session silently stops
|
|
82
|
+
compacting. Both mistakes are silent, which is why the documentation insists on reviewing
|
|
83
|
+
the disable and the mount together.
|
|
84
|
+
|
|
85
|
+
The loader key is `disabled`, not `disable`. The typo is dropped by the schema and the row
|
|
86
|
+
stays enabled.
|
|
87
|
+
|
|
88
|
+
The bundle deliberately ships the engine row `disabled`: turning it on without also disabling
|
|
89
|
+
the stock engine would create a second engine, and disabling the stock engine without a
|
|
90
|
+
replacement would leave no engine. Superseding the stock engine is a deployment decision.
|
|
91
|
+
|
|
92
|
+
The companion row is host-plane and always enabled because the browser bundle is discovered
|
|
93
|
+
from enabled loader entries; the engine row lives in a preset and is invisible to that scan.
|
|
94
|
+
The companion never claims `ctx.compaction`.
|
|
95
|
+
|
|
96
|
+
## 5. Manual compaction
|
|
97
|
+
|
|
98
|
+
The stock `compactNow` hardcodes a zero retention in its region selector. Manual `/compact`
|
|
99
|
+
therefore would reduce the session to the last surface node. The plugin fixes this without
|
|
100
|
+
patching `node_modules`:
|
|
101
|
+
|
|
102
|
+
- the retention index is computed from the policy cache (including the window, which is part
|
|
103
|
+
of the cache key);
|
|
104
|
+
- a shadowed `tokenMeter` reframes the measurement so the parent's `accumulated >=
|
|
105
|
+
retainTokens` boundary lands on the intended index;
|
|
106
|
+
- the real meter is kept for pricing via `regionDependencies()`;
|
|
107
|
+
- when retention cannot be satisfied, the plugin does not fabricate a region: all nodes are
|
|
108
|
+
discounted and the parent returns `null`, matching the real selector.
|
|
109
|
+
|
|
110
|
+
The transaction is wrapped in a signal that is never aborted by the command caller; only the
|
|
111
|
+
agent's maintenance signal can cancel it. A client reconnect cannot kill a 20–40 second
|
|
112
|
+
compaction. Busy-gate failures are retried once with a bounded wait and reported honestly.
|
|
113
|
+
|
|
114
|
+
## 6. Summarization fallback
|
|
115
|
+
|
|
116
|
+
`summarizationFallback` is a chain of `provider/model` routes tried in order. The parent
|
|
117
|
+
consumes `ctx.llm.stream()` incrementally, so each attempt is fully buffered and released
|
|
118
|
+
only when it completes with text. This prevents a half-streamed attempt from being committed
|
|
119
|
+
as a plausible-looking summary. Terminal `error`/`aborted` finish chunks count as failures;
|
|
120
|
+
on success the plugin rewrites `options.provider`/`options.model` in place so the durable
|
|
121
|
+
`compaction/summary` record names the route that actually wrote the summary.
|
|
122
|
+
|
|
123
|
+
Aborts do not consume the next route. A route equal to the primary (including an `auto`
|
|
124
|
+
resolution) is rejected at load time. Exhaustion yields a terminal finish chunk rather than
|
|
125
|
+
throwing, matching the parent's chunk contract.
|
|
126
|
+
|
|
127
|
+
## 7. Context policy prompt
|
|
128
|
+
|
|
129
|
+
The companion registers `compaction:context-policy` at order 100. It says context management
|
|
130
|
+
is automatic, the model cannot measure its own usage, and it must not stop, shorten, defer or
|
|
131
|
+
hand off work because it believes context is nearly full.
|
|
132
|
+
|
|
133
|
+
This is not cosmetic. In long sessions models claimed "context is nearly full" well before
|
|
134
|
+
the configured threshold had ever fired, then repeatedly cited that claim. No system prompt
|
|
135
|
+
or injected message had told them their usage; they were self-diagnosing incorrectly. Since
|
|
136
|
+
compaction is automatic, declaring the rule in the compaction plugin is the right scope.
|
|
137
|
+
|
|
138
|
+
## 8. Tests
|
|
139
|
+
|
|
140
|
+
The unit suite (198 cases) covers the policy, the engine integration, settings parsing, the
|
|
141
|
+
manual-compaction wrapper, the context prompt and both client modules. Two kinds of tests are
|
|
142
|
+
worth calling out:
|
|
143
|
+
|
|
144
|
+
- **Contract tests against the official package** pin assumptions the plugin relies on (for
|
|
145
|
+
example, that the parent still wraps the agent-loop gate as a recognizable `busy`), so an
|
|
146
|
+
upstream change fails loudly instead of silently disabling a retry.
|
|
147
|
+
- **Real bundle evaluation**: `client.test.js` evaluates `lib/client.js` with a JSX-runtime
|
|
148
|
+
stub, fake slots and a fake settings scope. Static checks missed several "card renders but
|
|
149
|
+
all fields are empty" regressions; the bundle test catches them.
|
|
150
|
+
|
|
151
|
+
A smoke script (`scripts/smoke.sh`) checks the real start-up log for `engine up:` and compares
|
|
152
|
+
the reported threshold with the deployment's declared policy.
|
|
153
|
+
|
|
154
|
+
## 9. Known limitations
|
|
155
|
+
|
|
156
|
+
- The settings writer cannot edit arrays; `models[]` stays hand-edited in `settings.yaml`.
|
|
157
|
+
- Non-loopback browsers receive DSH's memory-state settings view; the card is read-only and
|
|
158
|
+
says so.
|
|
159
|
+
- The context-policy paragraph is only registered when the profile has a `systemPrompt`
|
|
160
|
+
service.
|
|
161
|
+
- Absolute token budgets require a route that declares a context window.
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Example preset override: run the DSH web agent preset with
|
|
2
|
+
# dsh-compaction-policy-engine instead of the stock compaction-basic engine.
|
|
3
|
+
#
|
|
4
|
+
# Derived from @deepseek-ai/dsh-web-app 0.2.2 presets/standard.patch.yml (MIT).
|
|
5
|
+
# It replaces the complete preset config, as an id-targeted override does.
|
|
6
|
+
# If your DSH version ships a different standard preset, merge the compaction
|
|
7
|
+
# group into your own preset declaration instead of copying this file blindly.
|
|
8
|
+
|
|
9
|
+
- id: preset-standard
|
|
10
|
+
name: '@deepseek-ai/dsh-agent-preset'
|
|
11
|
+
config:
|
|
12
|
+
id: standard
|
|
13
|
+
order: 1
|
|
14
|
+
plugins:
|
|
15
|
+
- id: persona
|
|
16
|
+
name: '@deepseek-ai/dsh-persona'
|
|
17
|
+
config:
|
|
18
|
+
prefix: You are a coding agent powered by the {{model}} model.
|
|
19
|
+
- id: agent-instructions
|
|
20
|
+
name: '@deepseek-ai/dsh-agent-instructions'
|
|
21
|
+
config:
|
|
22
|
+
maxBytes: 65536
|
|
23
|
+
- id: time-context
|
|
24
|
+
name: '@deepseek-ai/dsh-time-context'
|
|
25
|
+
- id: tool-bash
|
|
26
|
+
name: '@deepseek-ai/dsh-tool-bash'
|
|
27
|
+
disabled: !!js process.platform === 'win32'
|
|
28
|
+
- id: tool-pwsh
|
|
29
|
+
name: '@deepseek-ai/dsh-tool-pwsh'
|
|
30
|
+
disabled: !!js process.platform !== 'win32'
|
|
31
|
+
- id: tool-fs
|
|
32
|
+
name: '@deepseek-ai/dsh-tool-fs'
|
|
33
|
+
config:
|
|
34
|
+
# Set false to drop the apply_patch tool beside edit.
|
|
35
|
+
applyPatch: false
|
|
36
|
+
- id: tool-fs-search
|
|
37
|
+
name: '@deepseek-ai/dsh-tool-fs-search'
|
|
38
|
+
config:
|
|
39
|
+
sampleOverCapGlobResults: false
|
|
40
|
+
- id: tool-jobs
|
|
41
|
+
name: '@deepseek-ai/dsh-tool-jobs'
|
|
42
|
+
- id: tool-schedule
|
|
43
|
+
name: '@deepseek-ai/dsh-tool-schedule'
|
|
44
|
+
- id: skill-filesystem
|
|
45
|
+
name: '@deepseek-ai/dsh-skill-filesystem'
|
|
46
|
+
- id: tool-skill
|
|
47
|
+
name: '@deepseek-ai/dsh-tool-skill'
|
|
48
|
+
- id: command-goal
|
|
49
|
+
name: '@deepseek-ai/dsh-command-goal'
|
|
50
|
+
- id: tool-goal
|
|
51
|
+
name: '@deepseek-ai/dsh-tool-goal'
|
|
52
|
+
- id: planning
|
|
53
|
+
name: cordis:group
|
|
54
|
+
group: true
|
|
55
|
+
isolate:
|
|
56
|
+
planMode: true
|
|
57
|
+
config:
|
|
58
|
+
- id: plan-mode
|
|
59
|
+
name: '@deepseek-ai/dsh-plan-mode'
|
|
60
|
+
config:
|
|
61
|
+
section: |
|
|
62
|
+
You are in plan mode. Stay in plan mode until the user approves your plan through exit_plan_mode or switches the session mode. Imperative language to implement changes means plan the implementation, not execute it. A user's conversational agreement — including an answer confirming something you asked — approves nothing and does not end plan mode; fold the confirmed decision into the plan and submit it through exit_plan_mode.
|
|
63
|
+
|
|
64
|
+
Explore first. Use non-mutating reads, searches, static analysis, and checks to ground the plan in the actual repository. Do not edit or write files, change configuration, run formatters or code generation that rewrites tracked files, commit, or otherwise carry out the plan. Prefer existing functions and patterns over new machinery.
|
|
65
|
+
|
|
66
|
+
The tool catalog stays the same across modes for request-cache stability. These plan-mode rules override any later tool description or guidance that suggests using mutation tools; those tools remain listed to keep the tool catalog unchanged. Do not use todo_write to track this planning phase: it tracks implementation after an approved plan, while the plan itself belongs in exit_plan_mode.
|
|
67
|
+
|
|
68
|
+
Resolve discoverable facts by inspection. Use ask_user_question only for user-owned choices or material ambiguity that inspection cannot answer. Do not ask the user where code lives or how current behavior works when you can find out.
|
|
69
|
+
|
|
70
|
+
Make the plan decision-complete: state the goal and success criteria; group implementation changes by subsystem; identify public API, schema, and data-flow changes; cover edge cases, failure modes, tests, acceptance criteria, and explicit assumptions. Keep it concise enough to review but detailed enough that another engineer can implement it without making design decisions.
|
|
71
|
+
|
|
72
|
+
When ready, call exit_plan_mode with the complete plan markdown, starting with a # title. Make exit_plan_mode the only and final tool call in that assistant response: it presents the plan for approval, and implementation begins only in a later step after approval. Do not paste the final plan as a plain reply or ask "should I proceed?" through prose or ask_user_question. If review rejects it, incorporate the feedback and present again. If the review channel is unavailable or aborted, stay in plan mode and ask the user to switch modes manually; do not proceed with implementation.
|
|
73
|
+
- id: compaction
|
|
74
|
+
name: cordis:group
|
|
75
|
+
group: true
|
|
76
|
+
isolate:
|
|
77
|
+
compaction: true
|
|
78
|
+
toolResultPruner: true
|
|
79
|
+
config:
|
|
80
|
+
# The policy engine replaces the stock compaction-basic in this realm.
|
|
81
|
+
- id: compaction-control
|
|
82
|
+
name: 'dsh-compaction-policy-engine/engine'
|
|
83
|
+
config:
|
|
84
|
+
default:
|
|
85
|
+
threshold: 0.8
|
|
86
|
+
retain: 0.16
|
|
87
|
+
- id: command-compact
|
|
88
|
+
name: '@deepseek-ai/dsh-command-compact'
|
|
89
|
+
- id: tool-result-pruner
|
|
90
|
+
name: '@deepseek-ai/dsh-compaction-tool-result-pruner'
|
|
91
|
+
config:
|
|
92
|
+
thresholdChars: 8192
|
|
93
|
+
headChars: 4096
|
|
94
|
+
tailChars: 1024
|
|
95
|
+
- id: delegation
|
|
96
|
+
name: cordis:group
|
|
97
|
+
group: true
|
|
98
|
+
isolate:
|
|
99
|
+
workflowEngine: true
|
|
100
|
+
config:
|
|
101
|
+
- id: tool-subagent-control
|
|
102
|
+
name: '@deepseek-ai/dsh-tool-subagent-control'
|
|
103
|
+
- id: tool-subagent
|
|
104
|
+
name: '@deepseek-ai/dsh-tool-subagent'
|
|
105
|
+
config:
|
|
106
|
+
provider: spawn
|
|
107
|
+
toolName: subagent
|
|
108
|
+
modelSelection: true
|
|
109
|
+
modelDiscovery: true
|
|
110
|
+
- id: tool-subagent-fork
|
|
111
|
+
name: '@deepseek-ai/dsh-tool-subagent'
|
|
112
|
+
config:
|
|
113
|
+
provider: fork
|
|
114
|
+
toolName: subagent_fork
|
|
115
|
+
- id: workflow-ptc
|
|
116
|
+
name: '@deepseek-ai/dsh-workflow-ptc'
|
|
117
|
+
config:
|
|
118
|
+
provider: spawn
|
|
119
|
+
- id: tool-workflow
|
|
120
|
+
name: '@deepseek-ai/dsh-tool-workflow'
|
|
121
|
+
- id: tool-ask-user
|
|
122
|
+
name: '@deepseek-ai/dsh-tool-ask-user'
|
|
123
|
+
- id: tool-todo
|
|
124
|
+
name: '@deepseek-ai/dsh-tool-todo'
|
|
125
|
+
config:
|
|
126
|
+
allowParallelInProgress: true
|
|
127
|
+
- id: tool-web
|
|
128
|
+
name: '@deepseek-ai/dsh-tool-web'
|
|
129
|
+
config:
|
|
130
|
+
fetch: true
|
|
131
|
+
searchTimeoutMs: 60000
|
|
132
|
+
- id: present
|
|
133
|
+
name: '@deepseek-ai/dsh-tool-present'
|
|
134
|
+
- id: tool-plugin-manager
|
|
135
|
+
name: '@deepseek-ai/dsh-plugin-manager/tools'
|
|
136
|
+
disabled: !!js "!ctx.get('profileContext')"
|
|
137
|
+
- id: tool-plugin-market
|
|
138
|
+
name: '@deepseek-ai/dsh-plugin-market/tools'
|
|
139
|
+
disabled: true
|