dsh-context-truth: Let the Model See the Real Context Budget

Preface

DeepSeek Harness (DSH) manages context usage via token-meter, but its results are only used for UI projection — the model itself never receives any token numbers. Automatic compaction only triggers at contextWindow × thresholdRatio (default 80%), and before that point the model has no way of knowing how much context remains. As history grows, the model may emit unobservable assertions such as “my context is exhausted, I will not make any more calls this turn,” and once those assertions are written into history, every subsequent turn treats them as established fact and keeps imitating them.

The dsh-context-truth plugin replaces guessing with telling. Before every step, it hands the model the real usage, the remaining budget, and the threshold as context, and requires the model to treat that measurement as authoritative. If the model claims exhaustion below the threshold, the plugin injects an immediate correction to reject the claim. It also provides a /context command to force an on-demand injection at any time.

What This Is

dsh-context-truth is a DSH host plugin with zero runtime dependencies. It does exactly one thing: stop the model from guessing how much context it has left. It fixes the vicious cycle where an agent misjudges its conversation as about to be exhausted due to long context, over-trims its execution, and then repeatedly references the previous turn’s (incorrect) assessment in every subsequent turn.

  • Maintainer: Vladivostok1017
  • Category: Model reasoning
  • License: MIT
  • Node version: >= 20
  • DSH version: >= 0.1.5-rc.2

Core Features

1. Real Context Budget Injection (Automatic)

Before every agent/pre-step, it uses ctx.tokenMeter.measure(session).totalTokens (the exact same number the automatic compaction decision uses) together with session.requestContext().contextWindow to compute the real percentage / remaining tokens / threshold, and injects a host-native context entry with source.kind = 'plugin:context-truth':

Context budget for this request (measured by the harness token meter, the same figure automatic compaction uses): ~412,000 of 1,000,000 tokens used (41.2%), ~588,000 remaining.
Compaction threshold 800,000 (80%): not reached, so the context is not near its limit.
Treat this measurement, not your own estimate, as the truth about remaining context.

Threshold source priority: compaction.thresholdRatio in settings.yaml → plugin config.thresholdRatio → built-in default 0.8. Re-read on every step, so configuration changes take effect immediately.

2. Spurious “Context Exhaustion” Claim Correction

If the most recent assistant message claims context exhaustion while usage is below the threshold, the plugin appends an Auto-correction entry that rejects the claim on the spot, preventing it from self-reinforcing in the history.

Detection is a conservative four-condition conjunction — it would rather miss a case than produce a false positive:

  1. Assertion pattern matched (Chinese and English);
  2. First-person self-reference;
  3. Contains a cessation intent (e.g. “no more / cannot / will not / hand off / wrap up / must stop …”);
  4. Excludes negation and quotation contexts.

Before matching, the body text is sanitized: fenced code blocks, inline code, paired quotation marks, and Markdown blockquotes are stripped. If the entire message is about the detector itself (containing strings like claimsContextExhaustion, assert.equal, test case, false positive, etc.), the message is skipped entirely.

Default: at most 1 correction per turn; can be disabled entirely with corrections: false.

3. /context Manual Injection Command

For the scenario where “the model is already hallucinating, but the automatic injection’s 10% tick mark hasn’t been reached yet”:

/context          Default auto: agent running → inject for the next step; idle → becomes the next turn's context
/context turn     Force-start a turn (even if the agent is already running)
/context queue    Don't deliver proactively; hold and inject on the next request
  • Alias: /ctx;
  • The command return value is for human eyes only and does not enter the model context, e.g.: Context ~412,000 / 1,000,000 (41.2%), ~588,000 remaining; compaction threshold 800,000 (80%). Injected: next step of this turn.
  • Three-level delivery fallback: agent.inbox.prepend('next-step') → agent.followup() → pending flag (unconditional one-time injection on the next pre-step). Stops as soon as any one succeeds; no duplicate delivery.

Installation & Activation

dsh plugin --profile web add link:<absolute-path-to-this-repo>

dsh plugin delegates to pnpm, writes a link: dependency, creates a junction in the profile’s node_modules, and — because this package declares dsh.bundle.patch — automatically appends dsh-context-truth to dsh.profile.bundles. Restart DSH after install to activate.

Option B: Packaged Distribution

npm run pack:tgz      # produces dist/dsh-context-truth-<version>.tgz
dsh plugin --profile web add link:<absolute-path>\dist\dsh-context-truth-1.1.1.tgz

Uninstall / Upgrade

dsh plugin --profile web remove dsh-context-truth   # automatically removed from bundles

Upgrade = change the code and restart (link: installs don’t need a re-add).

Configuration

All fields are optional and go under the mount entry’s config:

- insert:
    - id: context-truth
      name: dsh-context-truth
      config:
        thresholdRatio: 0.8            # Fallback threshold; compaction.thresholdRatio in settings.yaml takes priority
        claimTolerance: 0.02           # Tolerance for "spurious claim" detection
        corrections: true              # false = only inject facts, no corrections
        maxCorrectionsPerTurn: 1       # Max corrections per turn
        commandName: context           # Command name; false = don't register the command
        commandAliases: [ctx]          # Alias list
        commandMode: auto              # auto | turn | queue
        enabled: true                  # false = disable the entire plugin
        allowMultipleMounts: false     # true = allow multiple instances in the same process (rarely needed)

Typical Usage

After installing, verify in your session:

  1. Trace panel: you should see an injection line with source plugin:context-truth;
  2. Command palette: typing / should surface “Immediately inject current context remaining budget to the model”;
  3. Session log: a user/message entry exists with source.kind === 'plugin:context-truth';
  4. Usage verification (optional): node tools/measure-context.mjs "<session>\session.v3.jsonl.zstd" — decompresses DSH’s append-only log frame by frame, prints the real prompt usage per step, window percentage, compaction events, and whether the peak crossed the threshold.

Use Cases & Caveats

This plugin is suited for scenarios requiring precise context control, especially long conversations and complex task chains. It does not depend on any @deepseek-ai/* packages — it only uses the two services the host provides: tokenMeter and commands.

Known Limitations

  • Claim detection is heuristic and deliberately conservative: it would rather miss a case than insert an unwarranted correction in front of the user. If it feels noisy, set corrections: false;
  • Only effective for sessions that have already reported request/context (before the first request there is no denominator; at that point /context returns a clear notice rather than a guessed number);
  • tokenMeter.totalTokens includes the last assistant output, so it is slightly (tens to hundreds of) tokens larger than the pure prompt-side figure. The difference is negligible in magnitude, and this is the exact same number the compaction decision uses;
  • /context when the agent is idle will by default start a turn (so the model sees and responds to it immediately). To avoid auto-starting a turn: set commandMode: queue, or use /context queue for a one-off;
  • This plugin blocks the “fabricating numbers” path. “Making excuses to stop” is a model behavior issue, so the injected text also hard-codes a rule: if you must stop, you must report a specific blocker; citing context capacity as the reason is not permitted.

Closing

dsh-context-truth solves the problem of the model guessing its own remaining context by injecting the real context budget, correcting spurious “context exhaustion” claims, and providing the /context manual injection command. It has zero runtime dependencies, integrates seamlessly with the DSH ecosystem, and is well-suited for agent development scenarios that require precise context control.

  • Catalog page: https://www.skillhub.cn/plugins/Vladivostok1017/dsh-context-truth
  • GitHub: https://github.com/Vladivostok1017/dsh-context-truth
羽毛球分组比赛记分
小程序二维码

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

Xiaoye