Liangebra
dsh-smart-compact
DeepSeek Harness 智能上下文压缩引擎(CompactionEngine 后端):可配置阈值提示、强制自动压缩安全网、DCP 风格去重/清理建议、自定义摘要提示词。零外部状态,原文永不删除。Smart context compaction engine for DeepSeek Harness
- Stars
- 0
- Language
- TypeScript
- Created
- Aug 15, 2026
- Updated
- Aug 15, 2026
Introduction
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
compresstool 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 DSHCompactionEngineseam 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 thedcp_statustool.
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 thecompresstool (range/messagemodes) and points atdcp_status. - Mandatory auto-compaction — when usage reaches
mandatoryRatio(or the provider confirmscontext-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 viarunMaintenance; failures surface as classifiedManualCompactionErrors (busy,cancelled,changed,summary,commit).- DCP-inspired suggestions —
dcp_statusreports 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/startlock 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