dsh-context-manager 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.7.1
4
+
5
+ - 修复:操作台账(`/ctxmgr batches`)在个别时刻读成空台账。根因是把日志事件序号当成「已消费事件数」传给 `snapshotEvents(n)`,窗口可能落到日志末尾之外;台账现在每次全量扫描会话日志(只由用户命令触发,成本可忽略)。
6
+ - 修复:持久队列(删除/恢复的排队重放)使用同一错误游标,改为按「已消费事件数」推进——与 agent-loop 的 `storedCount` 写法一致,不再有跳过记录的窗口。
7
+ - 修复:`/ctxmgr undo-batch` 可能重复选中「已经撤销过」的批次(撤销是排队到下个请求才落地,连调工具时墓碑仍在表面上)。现在同时看台账信号:某批次已被一条 `op=undo` 记录点名即视为已撤销,再撤销会明确报「还没有可撤销的批次记录」。
3
8
  ## 1.7.0
4
9
 
5
10
  - Token metering: `core.js` gains `estimateMessageTokens`, a heuristic priced like DSH's own `dsh-token-meter` (4 characters ≈ 1 token, +4 per content block, +4 role overhead per message). The message list shows `≈N tok` per message and the current total context occupation; delete, whole-turn delete and clean results report the estimated tokens released. These are estimates only — Chinese text and JSON are markedly underestimated.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  一个 DSH(DeepSeek Harness)插件:在会话标题栏增加「上下文管理」入口,管理会话的**模型上下文**——按条删、按整轮删,之后还能恢复。原始会话日志、聊天记录与审计数据始终保留。
4
4
 
5
5
  - 包名:`dsh-context-manager`
6
- - 版本:`1.7.0`
6
+ - 版本:`1.7.1`
7
7
  - 类型:Cordis bundle(Host 半 + Web Client 半),纯 JavaScript,**无构建、无第三方依赖**
8
8
  - 适用:DSH `0.2.1-rc.2` 及以上(`0.2.2-rc.5` 实测)
9
9
 
@@ -15,14 +15,14 @@
15
15
 
16
16
  | 来源 | target 写法 | 说明 |
17
17
  |---|---|---|
18
- | npm | `dsh-context-manager@1.7.0` | 已发布到 [npm](https://www.npmjs.com/package/dsh-context-manager) |
18
+ | npm | `dsh-context-manager@1.7.1` | 已发布到 [npm](https://www.npmjs.com/package/dsh-context-manager) |
19
19
  | GitHub | `github:xian-yu-zhao/dsh-context-manager` | 免 npm,直接从仓库取 |
20
- | 本地 tgz | `file:C:\path\to\dsh-context-manager-1.7.0.tgz` | Release 资产或 `npm pack` 产物 |
20
+ | 本地 tgz | `file:C:\path\to\dsh-context-manager-1.7.1.tgz` | Release 资产或 `npm pack` 产物 |
21
21
  | 本地目录 | `link:C:\path\to\dsh-context-manager` | 开发调试用,改源码即生效 |
22
22
 
23
23
  安装后刷新 Web 页面(F5)或重启应用,会话标题栏右侧会出现列表图标。
24
24
 
25
- > **旧包已弃用**:`dsh-message-manager`(1.5.0)已在 npm 上标记 deprecated 并指向本包,GitHub 旧仓库 `xian-yu-zhao/dsh-message-manager` 自动重定向到 [`xian-yu-zhao/dsh-context-manager`](https://github.com/xian-yu-zhao/dsh-context-manager)。迁移只需两步:`plugin_manager remove_bundle target=dsh-message-manager`,再 `plugin_manager install_bundle target=dsh-context-manager@1.7.0`——新版本仍能识别旧版写入会话日志的删除记录与排队记录。
25
+ > **旧包已弃用**:`dsh-message-manager`(1.5.0)已在 npm 上标记 deprecated 并指向本包,GitHub 旧仓库 `xian-yu-zhao/dsh-message-manager` 自动重定向到 [`xian-yu-zhao/dsh-context-manager`](https://github.com/xian-yu-zhao/dsh-context-manager)。迁移只需两步:`plugin_manager remove_bundle target=dsh-message-manager`,再 `plugin_manager install_bundle target=dsh-context-manager@1.7.1`——新版本仍能识别旧版写入会话日志的删除记录与排队记录。
26
26
 
27
27
  > 无 npm CLI 也能装:GitHub / tgz / 本地目录三种来源都不经过 npm 发布。
28
28
  >
package/index.js CHANGED
@@ -35,7 +35,6 @@ import {
35
35
  deletedFromSurface,
36
36
  formatBatches,
37
37
  matchUndoBatches,
38
- pickUndoBatch,
39
38
  foldSurface,
40
39
  formatList,
41
40
  formatTurns,
@@ -248,8 +247,11 @@ function durableQueue(ctx, session) {
248
247
  cache = new Map()
249
248
  queueCaches.set(ctx, cache)
250
249
  }
251
- const state = cache.get(session.id) ?? { checkedTo: -1, pending: new Map() }
252
- const events = session.snapshotEvents(Math.max(0, state.checkedTo + 1))
250
+ // `snapshotEvents(n)` consumes n already-read EVENTS, not seq N: keep a consumed
251
+ // count (the same projection the agent loop uses) so the window can never land
252
+ // past the end of the log and silently skip records.
253
+ const state = cache.get(session.id) ?? { checkedCount: 0, pending: new Map() }
254
+ const events = session.snapshotEvents(state.checkedCount)
253
255
  for (const event of events) {
254
256
  const record = pluginRecordOf(event)
255
257
  if (record === undefined) continue
@@ -270,7 +272,7 @@ function durableQueue(ctx, session) {
270
272
  }
271
273
  }
272
274
  }
273
- if (events.length > 0) state.checkedTo = events[events.length - 1].seq
275
+ if (events.length > 0) state.checkedCount += events.length
274
276
  cache.set(session.id, state)
275
277
  return [...state.pending.values()]
276
278
  }
@@ -307,31 +309,29 @@ function appendBatchLedger(session, entry) {
307
309
  })
308
310
  }
309
311
 
310
- /** Per-plugin-instance batch ledger scan state, keyed by ctx then session id. */
311
- const batchCaches = new WeakMap()
312
-
313
312
  /**
314
- * Batch ledger entries in log order, incrementally.
313
+ * Batch ledger entries in log order.
315
314
  *
316
315
  * The ledger is a plugin record inside the append-only session log, so the
317
316
  * audit trail travels with the session, survives restarts, and never rewrites
318
317
  * a past record. Each record carries the batch id, the target seqs, the
319
318
  * tombstones it wrote, the estimated relief, and the outcome.
319
+ *
320
+ * The whole log is rescanned on every call: `snapshotEvents(n)` takes a log
321
+ * OFFSET, not a seq, so an incremental "scan from seq N" cursor silently
322
+ * reads an empty window when N and the remaining count disagree. The ledger
323
+ * is only read by a user-invoked command, and one pass over the in-memory log
324
+ * is cheap, so correctness wins over a cursor here.
320
325
  */
321
326
  function batchesFromLog(ctx, session) {
322
- let cache = batchCaches.get(ctx)
323
- if (cache === undefined) {
324
- cache = new Map()
325
- batchCaches.set(ctx, cache)
326
- }
327
- const state = cache.get(session.id) ?? { checkedTo: -1, entries: [] }
328
- const events = session.snapshotEvents(Math.max(0, state.checkedTo + 1))
327
+ const entries = []
328
+ const events = session.snapshotEvents()
329
329
  for (const event of events) {
330
330
  const record = pluginRecordOf(event)
331
331
  if (record === undefined || record.type !== BATCH_TYPE) continue
332
332
  const data = record.data ?? {}
333
333
  if (typeof data.batchId !== 'string') continue
334
- state.entries.push({
334
+ entries.push({
335
335
  recordSeq: record.seq,
336
336
  time: event.time,
337
337
  batchId: data.batchId,
@@ -344,9 +344,7 @@ function batchesFromLog(ctx, session) {
344
344
  reason: typeof data.reason === 'string' ? data.reason : undefined,
345
345
  })
346
346
  }
347
- if (events.length > 0) state.checkedTo = events[events.length - 1].seq
348
- cache.set(session.id, state)
349
- return state.entries
347
+ return entries
350
348
  }
351
349
 
352
350
  /** List the audit ledger for one live session. */
@@ -455,11 +453,25 @@ async function undoBatch(ctx, agent, batchId) {
455
453
  if (isNamed && matches.length > 1 && !matches.some((entry) => entry.batchId === batchId)) {
456
454
  throw new Error(`批次前缀有歧义(匹配 ${matches.length} 条),请给更长的前缀或完整 batchId(/ctxmgr batches 查看台账)。`)
457
455
  }
458
- const target = pickUndoBatch(batches, batchId)
456
+ // A batch counts as already undone when an undo ledger entry names it, or when
457
+ // none of its tombstones is on the surface any more. The ledger signal matters:
458
+ // a queued restore only lands on the next request, so back-to-back tool calls
459
+ // can still see the tombstone while the undo is already recorded.
460
+ const undoneIds = new Set(batches
461
+ .filter((entry) => entry.op === 'undo' && typeof entry.reason === 'string')
462
+ .map((entry) => entry.reason))
463
+ const stillOnSurface = matches.filter((entry) => !undoneIds.has(entry.batchId)
464
+ && entry.tombstoneSeqs.some((seq) => session.surface.nodes.includes(seq)))
465
+ const target = stillOnSurface.length === 0
466
+ ? undefined
467
+ : isNamed
468
+ ? stillOnSurface.find((entry) => entry.batchId === batchId)
469
+ ?? stillOnSurface.find((entry) => entry.batchId.startsWith(batchId))
470
+ : stillOnSurface[stillOnSurface.length - 1]
459
471
  if (target === undefined) {
460
472
  throw new Error(typeof batchId === 'string' && batchId.length > 0
461
473
  ? '找不到该批次记录(/ctxmgr batches 查看台账)。'
462
- : '还没有可撤销的批次记录(台账为空,或只有恢复类操作)。')
474
+ : '还没有可撤销的批次记录(台账为空、这些批次都已撤销,或只有恢复类操作)。')
463
475
  }
464
476
  const restoreSeqs = target.tombstoneSeqs.filter((seq) => session.surface.nodes.includes(seq))
465
477
  if (restoreSeqs.length === 0) throw new Error('该批次已没有仍在表面的删除记录(可能已恢复过)。')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-context-manager",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "type": "module",
5
5
  "description": "Manage a DSH session's model context: delete single messages or whole conversation turns (roll back to the start of any turn, drop noise), and restore them later, without touching the raw session log.",
6
6
  "license": "MIT",