DSH plugin token meter and spill: logRevision and SpillRef
DSH plugin context cost is decided by two mechanisms: token metering, which snapshots pressure per position, and spill, which moves oversized intermediates out of context. Metering's key property is logRevision — every number corresponds to "how many durable events were consumed" — so an older snapshot remains interpretable as replay advances. Node pricing splits in two: route-priced tokens drives trigger and retention decisions, while the fixed-heuristic heuristicTokens keeps shadow pricing consistent. Spill does exactly one thing: save caller-supplied text verbatim and return an opaque locator whose hint you render but never parse.
DSH plugin token metering: an immutable snapshot at a revision
Bottom line: the measurement is immutable and anchored to consumed event count; replay moving on does not reshape it.
- Core fields:
logRevision,baseline,surfaceDeltaTokens(signed),totalTokens,surfaceTokens,nodes. - Two anchor kinds:
baseline.kind === 'usage'means the latest successful call shares the canonical envelope and is no lower than its full route-priced anchor;estimatedmeans no reusable conservative anchor exists and the service prices the full envelope and surface itself. - Anchors are replaced: later successful calls replace earlier anchors, with both sides repriced under the same route.
- Two totals:
totalTokensis pressure,surfaceTokensis the surface price sum. - Reading the numbers: if
totalTokensrises whilesurfaceTokensstays flat, the pressure came from responses, not from more surface content.
DSH plugin surface pricing: nodes, two token values and positional order
Bottom line: surface order is authoritative, and a replacement node's seq may exceed nodes positioned after it — never sort by seq.
| Field | Meaning | Use |
|---|---|---|
seq | Durable sequence of the surface event | Locate, not sort |
tokens | Request pressure for this message under the route | Triggers, retention, ranges |
heuristicTokens | Route-independent fixed heuristic | Shadow-price replacement |
- Images follow the route: when the routed adapter declares image pricing, images charge visual tokens plus model-visible text; other routes keep the heuristic.
- Order is authoritative: a replacement node's durable seq may be higher than nodes after it, so sorting by seq breaks semantics.
- Immutable snapshot: it does not grow as the replay fold advances, which makes it safe to cache and compare.
- O(1) fold:
heuristicTokensexists so projection folds agree with their own appends; do not use it for decisions.
DSH plugin measurement: measure and estimateMessage
Bottom line: measure recomputes the current surface and accepts a replacement envelope; estimateMessage prices one message heuristically.
- Whole-session measurement:
const m = ctx.tokenMeter.measure(session, requestHeader?)
// m.logRevision / m.totalTokens / m.surfaceTokens / m.nodes
- Replacement envelope: passing
requestHeaderreplaces the latest logged envelope, useful for "what will this next request cost." - Complexity: every call clones positional nodes, so it is O(surface) — avoid high-frequency calls on very long sessions.
- Single message:
ctx.tokenMeter.estimateMessage(message)uses the fixed heuristic. - Selection: use
measurefor cost decisions andestimateMessage(or nodeheuristicTokens) for shadow pricing.
DSH plugin spill: saveText and an opaque locator
Bottom line: spill has one operation, saving text verbatim and returning a locator, byte count and retrieval hint.
interface SaveTextSpill {
owner: { sessionId: SessionId }
source: { kind: 'tool'; toolName: string; callId: ToolCallId; label: string }
| { kind: 'session-reference'; sessionId: SessionId; label: string }
suggestedName: string // a naming hint, never a path
content: string
}
// returns SpillRef: { locator, bytes, retrievalHint }
- Owner scopes storage: the backend groups by producing session; forked sessions inherit locators already in the seeded log, artifacts are not copied or re-owned, and new spills use the child session id.
- Source is descriptive only: tool sources carry a callId, session-reference sources carry the captured session id; it is never access control.
- suggestedName is a hint: the backend sanitizes it to a single safe path segment.
- Locators are opaque: a local backend renders a path, remote backends may render URIs or keys; render with
retrievalHint, never parse. - Failures reject: real storage failures (permissions, ENOSPC, backend unavailable) reject, and the caller decides how to degrade.
- Scope: the seam only stores; it owns no retention policy, tool-result replacement or search API.
DSH plugin local spill backend: layout, permissions and symlink defense
Bottom line: the local backend writes into a private session subdirectory with exclusive owner-only writes, preventing planted symlinks from redirecting output.
- Path shape:
<root>/session-<hash>/<random>-<safeName>, with session subdirectories keyed bysha256(sessionId). - Private root: the root is a configured or lazily created private directory (0700).
- Write mode:
open(path, 'wx', 0o600)— exclusive and owner-only, so a pre-planted symlink cannot redirect the write. - Retrieval hint: the local backend tells the model to use
readorgrepon that path. - Single implementation: only one
ctx.spillStoreper context; loading a second fails under Cordis duplicate-service rules.
DSH plugin metering and spill troubleshooting
Bottom line: match revisions for metering, and never treat a spill locator as a path.
- Numbers disagree — compare
logRevision; different revisions should not match. - Surface order looks wrong — you sorted by seq; surface order is authoritative.
- Cost jumped — check whether
totalTokensorsurfaceTokensmoved. - Spill locator will not open — do not build paths; follow
retrievalHint, which may not be a path at all. - Tool errors after a failed save — the consumer turned a reject into
isError; the official policy keeps the inline result. - Repair status, stated honestly: measurement fields, anchor rules and the spill contract follow the official Token metering and Spill storage pages; confirm signatures against their Cordis API blocks and your version.
Sources: Token metering, Spill storage, dsh-token-meter source.
FAQ
In a DSH plugin TokenMeasurement, logRevision is the number of durable events consumed to produce the measurement, equal to the next unread event seq. It anchors the numbers to a log position so an older snapshot stays interpretable.
DSH plugin totalTokens is current request-and-response pressure, while surfaceTokens is the route-priced total of the current surface, equal to the sum of node prices. Read totalTokens for pressure and surfaceTokens for surface content value.
DSH plugin nodes carry route-priced tokens plus a fixed-heuristic heuristicTokens value. The former drives trigger, retention and range selection; the latter keeps shadow-price replacement consistent with the O(1) projection fold.
DSH plugin spill returns a SpillRef containing an opaque locator, exact byte length and a retrieval hint. The model sees the locator and hint, and consumers must not parse the locator — follow retrievalHint instead.
No: DSH plugin spill policy is best-effort, keeping the original inline result when saving fails. The spill seam only stores; how to degrade is the consumer's decision.
Related Terms
- TokenMeasurement
- TokenMeasurement is an immutable request-pressure and surface snapshot at one consumed log revision, carrying the revision, baseline, delta, totals and positional nodes. It does not grow as replay advances.— https://deepseek-harness.github.io/deepseek-harness/reference/subsystems/token-meter
- logRevision
- logRevision is the number of durable events consumed when the measurement was produced, equal to the next unread event seq. It anchors statistics to a log position so results are reproducible and comparable.— https://deepseek-harness.github.io/deepseek-harness/reference/subsystems/token-meter
- SpillRef
- SpillRef is the result of one spill save: an opaque locator, the exact byte length and a backend-provided retrieval hint. It is the only credential consumers and models use to refer to a spilled artifact.— https://deepseek-harness.github.io/deepseek-harness/reference/subsystems/spill
- SpillLocator
- SpillLocator is the backend-returned, branded, model-facing opaque handle for a spilled artifact; a local backend renders a filesystem path while remote backends may render URIs or keys. Consumers render it but never parse it.— https://deepseek-harness.github.io/deepseek-harness/reference/subsystems/spill
Sources
- DeepSeek Harness docs — Token metering (TokenMeasurement, TokenSurfaceNode, ctx.tokenMeter)· deepseek-harness.github.io
- DeepSeek Harness docs — Spill storage (SaveTextSpill, SpillRef, local backend)· deepseek-harness.github.io
- dsh-token-meter source (measure/estimateMessage)· deepseek-ai (GitHub)