dsh-smart-compact
其他 活跃维护

dsh-smart-compact

Liangebra/dsh-smart-compact

提供可配置阈值触发、强制自动压缩安全网、DCP风格内容去重清理建议及自定义摘要提示词能力,零外部状态依赖,原始上下文内容永久留存,是轻量智能的上下文压缩后端方案。

0
Stars 标星
0
Forks 分支
0
Watchers 关注
0
Open Issues
TypeScript
主要语言
MIT
开源协议
55 KB
仓库大小
1 个月前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:Liangebra/dsh-smart-compact
git clone https://github.com/Liangebra/dsh-smart-compact.git
git clone git@github.com:Liangebra/dsh-smart-compact.git
README.md main

English · 简体中文

License: MIT
DSH plugin
Tests: 33/33

dsh-smart-compact

A custom CompactionEngine backend for the
DeepSeek Harness: configurable threshold prompting, a mandatory
auto-compaction
safety net at the limit, DCP-style dedup / purge
suggestions, a fully configurable summary prompt, and a /compact command
that keeps the original engine-driven behavior.

Why the name? The strategy families — deduplication of repeated tool
outputs, purging errored calls, a model-driven compress tool and nudges —
are inspired by DCP (Dynamic Context Pruning), the opencode-dcp
approach to keeping long sessions cheap. The mechanism is different:
opencode-dcp prunes only the outbound LLM payload without touching session
history, while this engine lives on the DSH CompactionEngine seam and
writes standard durable compaction brackets into the session log (zero
external state, originals never deleted). "smart-compact" names the
mechanism; the DCP lineage lives in the strategy module (dcp.ts) and the
dcp_status tool.

Zero external state. No kernel, no ledger files, no database, no
in-memory state that must stay in sync with the log. Every compression is a
standard durable bracket in the append-only session log:

compaction/start  →  compaction/summary  →  user/message (surface replace)  →  compaction/end

Original events are never deleted; block information is rebuilt from the
log on demand (rebuildBlockLedger), so a crash, restart, or resumed session
always sees a consistent picture.

Mount

One compaction backend per realm — disable the stock compaction-basic row
and add this one to your composition (e.g. ~/.dsh/profiles/web/cordis.patch.yml):

- id: compaction-smart
  name: 'dsh-smart-compact'
  config:
    thresholdRatio: 0.5
    mandatoryRatio: 0.9
    summarizationMaxTokens: 8192
    preserveRecent: 5
    dedupEnabled: true
    purgeErroredTurns: 4
    summaryPrompt: |
      Write ONE dense technical summary of the content below.
      Preserve exact paths, commands, error strings, identifiers,
      decisions and pending work; discard spent detail.
      {content}

The package registers itself as ctx.compaction. Restart dsh web after a
composition change (the web profile runs without HMR).

Configuration

Option Default Meaning
thresholdRatio 0.5 Context-usage fraction where the advisory nudge appears (agent/pre-step); the model decides whether to use the compress tool.
mandatoryRatio 0.9 At/above this the engine auto-compacts the largest compressible span once per turn.
summarizationMaxTokens 8192 Generation cap for the direct ctx.llm.stream summary call (purpose: 'compaction').
preserveRecent 5 Number of newest surface nodes never auto-compressed.
dedupEnabled true Offer DCP-style dedup suggestions in dcp_status.
purgeErroredTurns 4 Offer purge suggestions for the N most recent errored turns.
protectedTools [] Tool names whose call/result pairs are never compressed.
protectUserMessages false Keep every user message verbatim (never compress them).
summaryPrompt built-in Full prompt template; {content} is replaced with the span text.
modelContextLimit auto-probed Explicit context window (tokens) that wins over model probing.
autoNudge true Enable the threshold advisory nudge.
autoTools true Register compress and dcp_status model tools.

Behavior

  • Threshold prompting — a short advisory nudge is injected once the
    surface usage crosses thresholdRatio. The system prompt section explains
    the compress tool (range/message modes) and points at dcp_status.
  • Mandatory auto-compaction — when usage reaches mandatoryRatio (or the
    provider confirms context-overflow), the engine auto-compacts the largest
    compressible span with an engine-written summary. This is a hard safety
    net
    , gated once per turn.
  • /compact — engine-driven LLM summary of the largest compressible span,
    serialized against driver turns via runMaintenance; failures surface as
    classified ManualCompactionErrors (busy, cancelled, changed,
    summary, commit).
  • DCP-inspired suggestions — dcp_status reports dedup (repeated tool
    outputs) and purge (errored turns) candidates plus the current pressure and
    compressible ranges.
  • Safety — direct paths refuse to shadow compaction checkpoints or the
    latest user message; configured protected tools and (optionally) every user
    message are never compressed; tool-call/result pairs stay balanced; the
    durable compaction/start lock is opened before summarization and always
    released (success or recorded failure).

Development

npm install
npm run typecheck
npm test        # node --import tsx --test tests/*.test.ts
npm run build

License

MIT