@aiwayds/dsh-dcp 0.4.0 → 0.5.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.en.md CHANGED
@@ -115,16 +115,23 @@ Settable: `dedup`, `purgeErrors`, `maxItems`, `maxItemChars`,
115
115
  `maxSummaryTokens`, `language`, `tokenEstimate`, `thresholdRatio`,
116
116
  `roundInterval`, `notice`.
117
117
 
118
+ The `/dcp` status also lists every session that has compacted (subagents
119
+ included): `per-session: session-1 (2 compactions, ~444 tokens), child
120
+ (1 compaction, ~22 tokens)`. Compactions count per session; disposed
121
+ sessions (one-shot subagents included) fall out of the overview
122
+ automatically, and the list is capped at the first 10 sessions (`+N more`
123
+ for the rest) so the status stays one line.
124
+
118
125
  ## Triggers
119
126
 
120
127
  | Trigger | When | Notes |
121
128
  |---|---|---|
122
- | Pressure | before every step | tokens ≥ `thresholdRatio` (default 0.7) × context window, inherited |
129
+ | Pressure | before every step | tokens ≥ `thresholdRatio` (inherited upstream default 0.8; this plugin's bundle mounts 0.7 — see config table) × context window |
123
130
  | Overflow recovery | on a provider context-window error | inherited |
124
- | **Round interval** | every `roundInterval` completed turns | added by this plugin; one round = one successfully completed turn. **Default 50**: first compaction at turn 50, then every 50 more (100, 150, …); any compaction (pressure included) restarts the clock. Fires at the first idle boundary after the count is reached (below the pressure threshold too). `0` disables; requires the default `auto: true` |
131
+ | **Round interval** | every `roundInterval` assistant messages | added by this plugin; one round = one LLM roundtrip (each tool-iteration response counts, so one-shot subagents trigger too). **Default 50**: first compaction after message 50, then every 50 more (100, 150, …); any compaction (pressure included) restarts the clock. Fires at the first idle boundary after the count is reached (below the pressure threshold too). `0` disables; requires the default `auto: true` |
125
132
  | Manual | `/dcp compact`, `/compact` | anytime |
126
133
 
127
- - **Subagents are covered**: in-process subagents (including continuable children) dispatch through the same events, so pressure/overflow/round triggers count and fire per child session independently.
134
+ - **Subagents are covered**: in-process subagents (including continuable and one-shot children) dispatch through the same events, so pressure/overflow/round triggers count and fire per child session independently. The round trigger counts assistant messages, so a one-shot subagent whose whole run is a single turn (many tool iterations) triggers too.
128
135
  - **Visibility**: after every trigger event a one-line notice row (`dcp: compacted N history items (~X tokens, trigger)`) is appended to the session; frontends render it as a collapsed row. Note the row also rides the model request context (~15–25 tokens per compaction), and it is **on by default since 0.4.0** — disable with `notice: false`. `/dcp` stats count every committed region (a pressure retry loop may commit several).
129
136
 
130
137
  ## Configuration
@@ -133,8 +140,8 @@ All optional, defaults work out of the box:
133
140
 
134
141
  | Key | Default | Meaning |
135
142
  |---|---|---|
136
- | `thresholdRatio` | 0.7 | pressure trigger; 0.7 recommended for CJK-heavy sessions |
137
- | `roundInterval` | 50 | compact every N completed turns (0 disables). Default 50: 50, 100, 150… — the clock restarts after every compaction |
143
+ | `thresholdRatio` | 0.8 | pressure trigger (inherited upstream compaction-basic default 0.8; this plugin's bundle patch mounts 0.7, recommended for CJK-heavy sessions) |
144
+ | `roundInterval` | 50 | compact every N assistant messages (one LLM roundtrip) (0 disables). Default 50: 50, 100, 150… — the clock restarts after every compaction |
138
145
  | `notice` | `true` | append the one-line compaction notice to the session |
139
146
  | `language` | `zh` | summary language; `zh` also enables Chinese error/"待办:" detection |
140
147
  | `tokenEstimate` | `cjk` | CJK (zh/ja/ko/full-width) at ~2 chars/token; `ascii` matches the host |
@@ -143,6 +150,8 @@ All optional, defaults work out of the box:
143
150
  | `maxItems` / `maxItemChars` | 10 / 200 | summary density |
144
151
  | `maxSummaryTokens` | 2048 | summary token budget |
145
152
 
153
+ > **Upgrade note (0.5.0)**: the `roundInterval` counter switched from completed turns to assistant messages — the same value now triggers more often (a single turn usually contains several assistant messages).
154
+
146
155
  ## Design reference
147
156
 
148
157
  - [Opencode-DCP/opencode-dynamic-context-pruning](https://github.com/Opencode-DCP/opencode-dynamic-context-pruning)
@@ -151,7 +160,7 @@ All optional, defaults work out of the box:
151
160
  ## Development
152
161
 
153
162
  ```bash
154
- npm install && npm test # 61 tests: extractor/compaction/command/config/triggers/setup
163
+ npm install && npm test # 65 tests: extractor/compaction/command/config/triggers/setup
155
164
  ```
156
165
 
157
166
  ## License
package/README.md CHANGED
@@ -92,16 +92,18 @@ npx dsh-dcp-setup # 安全脚本:带日期备份 → 只追
92
92
 
93
93
  可调键:`dedup`、`purgeErrors`、`maxItems`、`maxItemChars`、`maxSummaryTokens`、`language`、`tokenEstimate`、`thresholdRatio`、`roundInterval`、`notice`。
94
94
 
95
+ `/dcp` 状态还会列出每个发生过压缩的会话(per-session 概览,含子代理),例如 `per-session: session-1 (2 compactions, ~444 tokens), child (1 compaction, ~22 tokens)`。压缩按会话独立计数;已销毁的会话(含 one-shot 子代理)自动从概览消失;列表封顶一行(最多前 10 个会话,超出显示 `+N more`)。
96
+
95
97
  ## 触发条件
96
98
 
97
99
  | 触发 | 时机 | 说明 |
98
100
  |---|---|---|
99
- | 压力触发 | 每步请求前 | token ≥ `thresholdRatio`(默认 0.7)× 上下文窗口,继承官方 |
101
+ | 压力触发 | 每步请求前 | token ≥ `thresholdRatio`(继承上游默认 0.8;本插件 bundle 挂载默认 0.7,见配置表)× 上下文窗口 |
100
102
  | 溢出恢复 | 模型报 context 超限时 | 继承官方 |
101
- | **轮数触发** | 会话每完成 `roundInterval` 轮 | 本插件新增;一轮 = 一次正常完成的 turn。**默认 50**:第 50 轮触发第一次,之后每 50 轮一次(100、150……);任何一次压缩(含压力触发)都会重置轮数时钟。到达轮数后的第一个空闲点触发(阈值之下也压)。`0` 关闭;需保持 `auto: true`(默认开) |
103
+ | **轮数触发** | 会话每收到 `roundInterval` 条 assistant message | 本插件新增;一条 = 一次 LLM 往返(每轮工具迭代各算一条,one-shot 子代理也能触发)。**默认 50**:第 50 条后触发第一次,之后每 50 条一次(100、150……);任何一次压缩(含压力触发)都会重置轮数时钟。到达条数后的第一个空闲点触发(阈值之下也压)。`0` 关闭;需保持 `auto: true`(默认开) |
102
104
  | 手动 | `/dcp compact`、`/compact` | 随时可用 |
103
105
 
104
- - **subagent 同样生效**:进程内 subagent(含 continuable 子代理)走同一套事件分发,压力/溢出/轮数触发对子会话独立计数、独立触发。
106
+ - **subagent 同样生效**:进程内 subagent(含 continuable 与 one-shot 子代理)走同一套事件分发,压力/溢出/轮数触发对子会话独立计数、独立触发。轮数触发按 assistant message 计数,所以全程只有 1 个 turn 的 one-shot 子代理(多次工具迭代)也能触发。
105
107
  - **压缩可见性**:每次压缩成功后,会话里追加一行 `dcp: 已压缩 N 条历史(约 X tokens,触发方式)` 通知行(前端渲染为折叠行)。注意该行也会作为上下文随请求发给模型(每次压缩约 15–25 tokens),且 **0.4.0 起默认开启**;`notice: false` 可关闭。`/dcp` 的 stats 持续累计(压力触发的多次 region 提交各计一次)。
106
108
 
107
109
  ## 配置
@@ -110,8 +112,8 @@ npx dsh-dcp-setup # 安全脚本:带日期备份 → 只追
110
112
 
111
113
  | 键 | 默认 | 说明 |
112
114
  |---|---|---|
113
- | `thresholdRatio` | 0.7 | 压力触发阈值;中文场景建议 0.7 |
114
- | `roundInterval` | 50 | 每 N 轮触发一次压缩(0 关闭)。默认 50:50、100、150……每次压缩后重数 |
115
+ | `thresholdRatio` | 0.8 | 压力触发阈值(继承上游 compaction-basic 默认 0.8;本插件 bundle patch 挂载时默认 0.7,中文场景建议 0.7) |
116
+ | `roundInterval` | 50 | 每 N 条 assistant message(一次 LLM 往返)触发一次压缩(0 关闭)。默认 50:50、100、150……每次压缩后重数 |
115
117
  | `notice` | `true` | 压缩后在会话中追加一行通知 |
116
118
  | `language` | `zh` | 摘要语言;`zh` 额外识别中文报错和"待办:" |
117
119
  | `tokenEstimate` | `cjk` | CJK(中/日/韩/全角)按 ~2 字符/token 计价;`ascii` 与宿主一致 |
@@ -120,6 +122,8 @@ npx dsh-dcp-setup # 安全脚本:带日期备份 → 只追
120
122
  | `maxItems` / `maxItemChars` | 10 / 200 | 摘要密度 |
121
123
  | `maxSummaryTokens` | 2048 | 摘要 token 预算 |
122
124
 
125
+ > **升级提示(0.5.0)**:`roundInterval` 的计数单位由 completed turn 改为 assistant message——同值下触发会更频繁(一个 turn 内往往有多条 assistant message)。
126
+
123
127
  ## 设计参考
124
128
 
125
129
  - [Opencode-DCP/opencode-dynamic-context-pruning](https://github.com/Opencode-DCP/opencode-dynamic-context-pruning)
@@ -128,7 +132,7 @@ npx dsh-dcp-setup # 安全脚本:带日期备份 → 只追
128
132
  ## 开发
129
133
 
130
134
  ```bash
131
- npm install && npm test # 61 个用例:抽取/压缩/命令/配置/触发/安装脚本
135
+ npm install && npm test # 65 个用例:抽取/压缩/命令/配置/触发/安装脚本
132
136
  ```
133
137
 
134
138
  ## License
package/lib/command.js CHANGED
@@ -81,6 +81,22 @@ function statusText(engine, version) {
81
81
  `config: dedup=${engine.dcp.dedup} purgeErrors=${engine.dcp.purgeErrors} maxItems=${engine.dcp.maxItems} maxItemChars=${engine.dcp.maxItemChars} maxSummaryTokens=${engine.dcp.maxSummaryTokens} language=${engine.dcp.language} tokenEstimate=${engine.dcp.tokenEstimate} protectedTools=[${engine.dcp.protectedTools.join(', ')}] thresholdRatio=${engine.config.thresholdRatio} roundInterval=${engine.dcp.roundInterval}${engine.dcp.roundInterval > 0 ? '' : ' (off)'} notice=${engine.dcp.notice}`,
82
82
  `stats: ${stats.compactions} compaction${stats.compactions === 1 ? '' : 's'}, ~${stats.shadowedTokens} tokens shadowed, ${stats.compactions} LLM summary call${stats.compactions === 1 ? '' : 's'} avoided`,
83
83
  ]
84
+ // Per-session dimension: one compacted session per entry, in store
85
+ // (creation) order. Bounded to one line so a long-lived host with many
86
+ // compacted children never floods the status.
87
+ const perSession = engine.sessionStatsOverview()
88
+ if (perSession.length > 0) {
89
+ const MAX_SESSIONS = 10
90
+ const shown = perSession.slice(0, MAX_SESSIONS)
91
+ const rest = perSession.length - shown.length
92
+ const items = shown.map(({ id, compactions, shadowedTokens }) => {
93
+ const unit = compactions === 1 ? 'compaction' : 'compactions'
94
+ const tokens = shadowedTokens > 0 ? `, ~${shadowedTokens} tokens` : ''
95
+ return `${id} (${compactions} ${unit}${tokens})`
96
+ })
97
+ if (rest > 0) items.push(`+${rest} more`)
98
+ lines.push(`per-session: ${items.join(', ')}`)
99
+ }
84
100
  if (stats.lastAt !== null) lines.push(`last compaction: ${new Date(stats.lastAt).toLocaleString()}`)
85
101
  return lines.join('\n')
86
102
  }
package/lib/index.js CHANGED
@@ -19,8 +19,8 @@
19
19
  * - id: compaction-basic
20
20
  * name: /absolute/path/to/dsh-dcp/lib/index.js
21
21
  * config:
22
- * thresholdRatio: 0.7 # optional; every key is optional
23
- * roundInterval: 100 # optional; also compact every N completed turns (default 50)
22
+ * thresholdRatio: 0.7 # optional; 0.7 = this bundle patch's mount value, package default is 0.8; every key is optional
23
+ * roundInterval: 100 # optional; also compact every N assistant messages, one per LLM roundtrip (default 50)
24
24
  * ```
25
25
  *
26
26
  * @module dsh-dcp
@@ -95,9 +95,10 @@ export class DcpEngine extends BasicCompactionEngine {
95
95
  pluginPath
96
96
 
97
97
  /**
98
- * Completed-turn counters since the last dsh-dcp compaction, per session —
99
- * one "round" is one `turn/end` whose reason is `completed`. Weak keys:
100
- * disposed sessions (including one-shot subagents) drop out with the object.
98
+ * Completed-assistant-message counters since the last dsh-dcp compaction,
99
+ * per session — one "round" is one `assistant/message` (one LLM roundtrip).
100
+ * Weak keys: disposed sessions (including one-shot subagents) drop out with
101
+ * the object.
101
102
  */
102
103
  #rounds = new WeakMap()
103
104
 
@@ -112,6 +113,16 @@ export class DcpEngine extends BasicCompactionEngine {
112
113
  /** Sessions with a round-triggered compaction still in flight. */
113
114
  #roundInFlight = new WeakSet()
114
115
 
116
+ /**
117
+ * Per-session compaction records: one entry per session that committed at
118
+ * least one compaction, counting the compactions and the shadowed tokens.
119
+ * Weak keys (mirroring `#rounds`): disposed sessions — including one-shot
120
+ * subagents — drop out with the object. Not enumerated directly, because a
121
+ * WeakMap leaks nothing and lists nothing; `/dcp` walks the sessions
122
+ * service and looks each live session up here.
123
+ */
124
+ #sessionStats = new WeakMap()
125
+
115
126
  constructor(ctx, config = {}) {
116
127
  const { basic, dcp } = splitConfig(config)
117
128
  super(ctx, basic)
@@ -126,11 +137,13 @@ export class DcpEngine extends BasicCompactionEngine {
126
137
  }
127
138
 
128
139
  /**
129
- * Round-interval trigger: count completed turns per session and, once the
130
- * configured `roundInterval` is reached, compact at the agent's next idle
131
- * boundary through the manual-compaction seam (`compactNow`). In-process
132
- * subagents run through the same session/event and agent/status dispatch,
133
- * so continuable children are covered exactly like the top-level session.
140
+ * Round-interval trigger: count assistant messages (one per LLM roundtrip)
141
+ * per session and, once the configured `roundInterval` is reached, compact
142
+ * at the agent's next idle boundary through the manual-compaction seam
143
+ * (`compactNow`). In-process subagents run through the same session/event
144
+ * and agent/status dispatch, so continuable children are covered exactly
145
+ * like the top-level session — and because one-shot subagents emit many
146
+ * assistant messages inside a single turn, they now trigger too.
134
147
  */
135
148
  #registerRoundTrigger() {
136
149
  const { ctx } = this
@@ -138,7 +151,10 @@ export class DcpEngine extends BasicCompactionEngine {
138
151
  // Counting is skipped while disabled, but the listener stays registered
139
152
  // so `/dcp set roundInterval N` can arm it again at runtime.
140
153
  if (!this.dcp.roundInterval) return
141
- if (event.type !== 'turn/end' || event.data.reason?.kind !== 'completed') return
154
+ // Every assembled assistant message is one completed LLM roundtrip;
155
+ // counting it (instead of completed turns) also covers one-shot
156
+ // subagents, whose whole run is a single turn with many model calls.
157
+ if (event.type !== 'assistant/message') return
142
158
  this.#rounds.set(session, (this.#rounds.get(session) ?? 0) + 1)
143
159
  })
144
160
  ctx.on('agent/status', ({ agent, status }) => {
@@ -151,7 +167,7 @@ export class DcpEngine extends BasicCompactionEngine {
151
167
  * single-flight per session. `busy` (queued waking work won the race) and
152
168
  * `cancelled` (the agent was interrupted mid-compaction) both keep the
153
169
  * accumulated rounds for the next idle boundary — cancelling one attempt
154
- * must not cancel the N completed turns behind it. Any other failure warns
170
+ * must not cancel the N assistant messages behind it. Any other failure warns
155
171
  * and releases the boundary: the pressure trigger remains the safety net.
156
172
  */
157
173
  #maybeRoundCompact(agent) {
@@ -244,6 +260,20 @@ export class DcpEngine extends BasicCompactionEngine {
244
260
  this.dcpStats.shadowedTokens += result.shadowedTokenCount
245
261
  this.dcpStats.lastAt = Date.now()
246
262
  this.#rounds.delete(session)
263
+ this.#recordSessionStats(session, result.shadowedTokenCount)
264
+ }
265
+
266
+ /**
267
+ * Accumulate one committed compaction against its session. WeakMap keys
268
+ * must be objects, so a session-less recording (should not happen) is
269
+ * skipped rather than thrown on.
270
+ */
271
+ #recordSessionStats(session, shadowedTokenCount) {
272
+ if (session === undefined || session === null || typeof session !== 'object') return
273
+ const entry = this.#sessionStats.get(session) ?? { compactions: 0, shadowedTokens: 0 }
274
+ entry.compactions += 1
275
+ entry.shadowedTokens += shadowedTokenCount
276
+ this.#sessionStats.set(session, entry)
247
277
  }
248
278
 
249
279
  /**
@@ -278,6 +308,31 @@ export class DcpEngine extends BasicCompactionEngine {
278
308
  this.#recordStats(session, result)
279
309
  this.#appendNotice(session, result, trigger)
280
310
  }
311
+
312
+ /**
313
+ * Per-session compaction overview for `/dcp`: one entry per live session
314
+ * that has recorded at least one compaction. The WeakMap is not enumerable
315
+ * and must never retain sessions, so this walks the sessions service and
316
+ * looks each live session up. Disposed sessions (including one-shot
317
+ * subagents) fall out of the store, and their records with them.
318
+ *
319
+ * @returns {Array<{id: string, compactions: number, shadowedTokens: number}>}
320
+ */
321
+ sessionStatsOverview() {
322
+ const overview = []
323
+ // Services live on ctx for a cordis plugin instance (`inject` only
324
+ // declares them); this.sessions is undefined in production.
325
+ for (const session of this.ctx.sessions?.list?.() ?? []) {
326
+ const entry = this.#sessionStats.get(session)
327
+ if (entry === undefined) continue
328
+ overview.push({
329
+ id: session.header?.id ?? '<unknown>',
330
+ compactions: entry.compactions,
331
+ shadowedTokens: entry.shadowedTokens,
332
+ })
333
+ }
334
+ return overview
335
+ }
281
336
  }
282
337
 
283
338
  export default DcpEngine
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiwayds/dsh-dcp",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Deterministic context-pruning compaction backend for dsh (DeepSeek Harness) — zero-LLM summaries, /dcp command, works out of the box. Design references Opencode-DCP/opencode-dynamic-context-pruning.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",