LAYER 04 · Understand safety and durability

Compaction, token metering, and spill

Not blunt truncation: safely replace the model surface while preserving large results and auditable accounting

21 min3 source anchorsupstream@47f9438
measure → prune → summarize → replace surface
The question this chapter resolves

How do you shorten context without breaking tool call/result pairing or deleting original facts?

Build intuition

An archive does not destroy case files. It adds a summary index for daily use; oversized attachments move to storage while the catalog keeps a locator, size, and retrieval instructions.

MECHANISM

Mechanism

TokenMeter prices the current surface node by node. Compaction logs a start lock, optionally prunes tool results, generates a summary plus replacement user/message, then logs end.

Spill policy stores oversized text in a session-private backend during tools/post-execute and gives the model a preview plus opaque locator. If saving fails, it preserves the original result.

Step 1 of 6

Measure pressure

A usage anchor or conservative heuristic produces totalTokens.

INVARIANTS

What extensions must preserve

  • Compaction boundaries preserve tool call/result balance
  • start/end lock encloses the whole operation
  • Spill locators are opaque handles, not assumed local paths
FAILURE MODES

The tempting wrong turns

  • ×Selecting surface nodes by numeric seq interval
  • ×Writing a successful end after summary failure
  • ×Turning a successful tool call into an error when spill storage fails
VERIFY IN SOURCE

Do not trust the conclusion—verify it

These anchors are pinned to official deepseek-harness@47f9438; account for later breaking changes when reading current upstream.

KNOWLEDGE CHECK

Pause for ten seconds before revealing

Why are compaction/start and end log-only events?

Why the next chapter follows

Compaction still rests on the full log. Next we persist it across processes and read efficiently through projections and queries.

Learn DeepSeek Harness

An independent learning project. Explanations derive from source; upstream remains authoritative.