Files
deepseek-harness/website/zh-CN/api/harness/compact.md
T
Tianyi Cui fd54b5c848 Merge latest master into post-step recovery
Combine agent initiator scoping and exact Cordis JSDoc inspection with post-step compaction and bounded request recovery.

Regenerate the Cordis and website API catalogs, and classify the recovery and compaction types required by the current catalog link-coverage gate.
2026-07-19 16:46:01 +08:00

4.5 KiB

ctx.compact

CompactService (abstract seam) — provided by @deepseek-ai/dsh-compact.

Abstract compaction service. Implementations own trigger policy, retention, and summarization, and may consume a separate measurement service. A successful run replaces the selected surface span with one summary node and prevents concurrent compaction of the same session. Load one implementation per context as ctx.compact.

Source

ctx.compact.compactIfNeeded(agent, trigger, signal)

/**
 * Consider automatic compaction for one explicit trigger. Pressure policy
 * uses the latest durable routed request, while context-overflow policy may
 * force a useful balanced reduction even below the normal threshold. Return
 * `null` when no safe range can be compacted. A single oversized retained
 * unit or request envelope cannot be repaired through surface compaction.
 *
 * @param agent - agent context owning the session surface and routing options.
 * @param trigger - normal pressure or provider-confirmed context overflow.
 * @param signal - cancellation signal; model-backed implementations must forward it.
 * @returns the compaction result, or `null` if no compaction was needed.
 */
abstract compactIfNeeded( agent: CompactAgentContext, trigger: CompactionTrigger, signal: AbortSignal, ): Promise<CompactionResult | null>

Consider automatic compaction for one explicit trigger. Pressure policy uses the latest durable routed request, while context-overflow policy may force a useful balanced reduction even below the normal threshold. Return null when no safe range can be compacted. A single oversized retained unit or request envelope cannot be repaired through surface compaction.

  • agent — agent context owning the session surface and routing options.
  • trigger — normal pressure or provider-confirmed context overflow.
  • signal — cancellation signal; model-backed implementations must forward it.

Returns the compaction result, or null if no compaction was needed.

Source

ctx.compact.compactRegion(start, end, agent, signal?)

/**
 * 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. The target session is `agent.session`.
 * Use {@link toolPairingBalancedBefore} and {@link toolPairingBalancedAfter}
 * for the edge checks.
 *
 * @param start - first surface seq, inclusive.
 * @param end - last surface seq, inclusive.
 * @param agent - context whose session is mutated and whose routing options guide summarization.
 * @param signal - optional cancellation; model-backed implementations must forward it.
 * @throws when compaction is active or the range is missing, reversed, or unbalanced.
 * @returns the appended event seqs, summary, replaced range, and token accounting.
 */
abstract compactRegion( start: number, end: number, agent: CompactAgentContext, signal?: AbortSignal, ): Promise<CompactionResult>

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. The target session is agent.session. Use toolPairingBalancedBefore and toolPairingBalancedAfter for the edge checks.

  • start — first surface seq, inclusive.
  • end — last surface seq, inclusive.
  • agent — context whose session is mutated and whose routing options guide summarization.
  • signal — optional cancellation; model-backed implementations must forward it.

Returns the appended event seqs, summary, replaced range, and token accounting.

Source