# ctx.compact `CompactService` (abstract seam) — provided by `@deepseek-ai/dsh-compact`. Abstract compaction service. Implementations own token estimation, retention, and summarization, but a successful run must replace the selected surface span with one summary node and prevent concurrent compaction of the same session. Load one implementation per context as `ctx.compact`. [Source](https://github.com/deepseek-harness/deepseek-harness/blob/master/packages/compact/compact/src/index.ts#L37) ### ctx.compact.compactIfNeeded(agent, fullSystemPrompt, sessionPrefix, signal) ```ts website-api abstract compactIfNeeded( agent: CompactAgentContext, fullSystemPrompt: string, sessionPrefix: readonly Message[], signal: AbortSignal, ): Promise ``` Check token pressure and compact if the conversation is too large. Estimate the next request, including its session prefix, derived history, and system prompt. Above threshold, compact a head-anchored range ending at a balanced tool boundary and reconsolidate any prior automatic checkpoint. Return `null` when no compaction is needed or an open tail leaves no safe cutoff. A single oversized retained unit or prefix cannot be repaired here. - `agent` — agent context owning the session surface and model options. - `fullSystemPrompt` — assembled system prompt, counted toward the estimate. - `sessionPrefix` — the instance's composed session prefix, counted toward the estimate. - `signal` — cancellation signal; model-backed implementations must forward it. **Returns** the compaction result, or `null` if no compaction was needed. [Source](https://github.com/deepseek-harness/deepseek-harness/blob/master/packages/compact/compact/src/index.ts#L57) ### ctx.compact.compactRegion(session, start, end, agent, signal?) ```ts website-api abstract compactRegion( session: Session, start: number, end: number, agent: CompactAgentContext, signal?: AbortSignal, ): Promise ``` Forcibly compact a range of surface nodes into a single summary node. `start` and `end` name an inclusive span by surface position, not numeric seq order; replacements can make visible seqs non-monotonic. Both edges must be balanced so assistant tool calls remain paired with their results. A model- backed implementation forwards cancellation and rejects active, missing, reversed, or unbalanced ranges. Use toolPairingBalancedBefore and toolPairingBalancedAfter for the edge checks. - `session` — session to mutate. - `start` — first surface seq, inclusive. - `end` — last surface seq, inclusive. - `agent` — summarizer context. - `signal` — optional cancellation; model-backed implementations must forward it. **Returns** the replaced range and summary. [Source](https://github.com/deepseek-harness/deepseek-harness/blob/master/packages/compact/compact/src/index.ts#L82)