Smart Compaction: Letting the Model Decide When to Compress Context

DSH (DeepSeek Harness) has a built-in compaction mechanism that is purely reactive. It is triggered by a fixed threshold and cannot determine whether the current context truncation point is safe. For example, it might trigger compaction immediately after reading a file and before editing it, causing the model to lose key information it just read.

Smart Compaction delegates the decision to the model, allowing it to proactively assess the situation and trigger compaction at a safe point.

What It Is

This is a plugin for DeepSeek Harness, maintained by JoblessJoe. It provides the model with two tools: compact_now and context_status.

  • compact_now: Allows the model to proactively compact the context at a safe point, such as after completing a todo item.
  • context_status: Provides real token usage data, helping the model understand the current occupancy of the context window.

Core Features

compact_now

When the model calls this tool, it selects the largest currently compactible segment of history and invokes DSH’s compactRegion() method. Before the call, it sends a “Compacting now” notice to the chat interface to prevent users from assuming the model has stopped.

If there is not enough history to compact, the tool performs a harmless no-op and does not send a notice.

context_status

This is a read-only tool that does not modify the session. It reports the current token usage relative to the model’s actual context window size, for example displaying “~42,300 / 77,824 tokens used (54.3%)”. The data source is DSH’s ctx.sessionProjections.stateOf(), which is consistent with the context meter data in the Web UI.

Automatic Safe-Point Compaction

The plugin listens for todo_write events. When a task is marked as completed, it represents a natural truncation point. If context usage at that time exceeds safePointRatio (default: 0.5), the plugin automatically triggers compaction before the next step begins. DSH’s original 80% threshold trigger remains as a fallback mechanism.

Installation and Enablement

Run the following command in DSH’s web configuration file (or another configuration file that supports live reload):

npx @deepseek-ai/dsh plugin --profile web add smart-compaction

After installation, the plugin is automatically added to dsh.profile.bundles. The web configuration file supports hot reload, so the plugin takes effect immediately.

For configuration files based on @deepseek-ai/dsh-web-app, you also need to run the following command to mount the compaction service into the isolated realm:

npx -p smart-compaction smart-compaction-mount-into-presets

Configuration

You can adjust the safe-point compaction trigger threshold in cordis.patch.yml. For example:

- id: tool-compact-now
  config:
    safePointRatio: 0.6   # 或设置为 false 以禁用

Notes

  1. Dependency Service: compact_now requires access to a compaction service (for example, compaction-basic).
  2. Token Metering: context_status requires @deepseek-ai/dsh-token-meter to provide context pressure data.
  3. Non-Invasive: The plugin only uses DSH’s public APIs and makes no configuration modifications or changes to core code.
  4. Live Reload: To observe configuration changes or changes in plugin behavior, it is recommended to run under the web configuration file.
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye