deepseek-ai/deepseek-harness · error · Error

compactRegion: end seq ${end} is not a balanced boundary (wo

Error message

compactRegion: end seq ${end} is not a balanced boundary (would split a step, or the step is still open)

What it means

The cut immediately after `end` is crossed by an in-progress tool call — the replacement would split a step, or the step at the surface tail is still open (a tool-call whose tool/result has not landed yet). Checked with toolPairingBalancedAfter; both edges must be balanced so assistant tool calls stay paired with their results.

Source

Thrown at packages/compaction/compaction-basic/src/region.ts:332

/** Validate one requested surface-position span before asynchronous work begins. */
function validateSurfaceRegion(session: Session, start: number, end: number): SurfaceSelection {
  const nodes = session.surface.nodes
  const startIdx = nodes.indexOf(start)
  const endIdx = nodes.indexOf(end)
  if (startIdx === -1) throw new Error(`compactRegion: start seq ${start} not found in surface`)
  if (endIdx === -1) throw new Error(`compactRegion: end seq ${end} not found in surface`)
  if (startIdx > endIdx) {
    throw new Error(
      `compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface`,
    )
  }
  // oxlint-disable-next-line typescript/no-non-null-assertion
  if (!toolPairingBalancedBefore(session, nodes[startIdx]!)) {
    throw new Error(`compactRegion: start seq ${start} is not a balanced boundary (would split a step's tool-call/result pair)`)
  }
  // oxlint-disable-next-line typescript/no-non-null-assertion
  if (!toolPairingBalancedAfter(session, nodes[endIdx]!)) {
    throw new Error(`compactRegion: end seq ${end} is not a balanced boundary (would split a step, or the step is still open)`)
  }

  return { start, end, startIdx, endIdx, shadowedSeqs: nodes.slice(startIdx, endIdx + 1) }
}

/** Snapshot pricing and replay input for a validated surface range. */
function prepareCompaction(
  dependencies: RegionDependencies,
  session: Session,
  selection: SurfaceSelection,
): PreparedCompaction {
  const measurement = dependencies.meter.measure(session)
  const selectedNodes = measurement.nodes.slice(selection.startIdx, selection.endIdx + 1)
  if (selectedNodes.length !== selection.shadowedSeqs.length
    || selectedNodes.some((node, index) => node.seq !== selection.shadowedSeqs[index])) {
    throw new SurfaceChangedError('compaction: selected surface changed before summarization began')
  }
  return {

View on GitHub (pinned to b150a551b8)

Solutions

  1. Move end earlier to a seq where toolPairingBalancedAfter(session, seq) returns true
  2. If the tail step is still open, wait for its tool results to land before compacting
  3. End on a completed tool/result or on a user-message boundary

Example fix

// before: end on the surface tail while a tool call is open
await ctx.compaction.compactRegion(start, nodes[nodes.length - 1]!, agent)

// after: back off to a balanced trailing cut
import { toolPairingBalancedAfter } from '@deepseek-ai/dsh-compaction'
const nodes = agent.session.surface.nodes
let j = nodes.length - 1
while (!toolPairingBalancedAfter(agent.session, nodes[j]!)) j -= 1
await ctx.compaction.compactRegion(start, nodes[j]!, agent)
Defensive patterns

Strategy: validation

Validate before calling

import { toolPairingBalancedAfter } from '@deepseek-ai/dsh-compaction'
if (!toolPairingBalancedAfter(agent.session, end)) {
  // back the end off to the previous balanced cut, or wait for open steps to close
  const nodes = agent.session.surface.nodes
  let j = nodes.indexOf(end)
  while (!toolPairingBalancedAfter(agent.session, nodes[j]!)) j -= 1
  end = nodes[j]!
}

Prevention

When it happens

Trigger: `end` lands on an assistant message whose tool-calls are answered by later tool/result nodes; `end` is the surface tail while tools are still executing in the open turn; ending on a node whose following cut has a nonzero in-progress tool-call count.

Common situations: Calling compactRegion mid-turn while subprocess or shell tools are still running; cutting at the last assistant message of a parallel-tool step.

Related errors


AI-assisted analysis of deepseek-ai/deepseek-harness@b150a551b8 (2026-08-24). Data as JSON: /api/errors/84e040dd11fb70c4. Report an issue: GitHub.