deepseek-ai/deepseek-harness · error · Error

compactRegion: start seq ${start} is not a balanced boundary

Error message

compactRegion: start seq ${start} is not a balanced boundary (would split a step's tool-call/result pair)

What it means

The cut immediately before `start` is crossed by an in-progress tool call: an assistant message containing tool-calls would be separated from its matching tool/result events by the replacement. Both span edges must be tool-pairing balanced (checked with toolPairingBalancedBefore/After from @deepseek-ai/dsh-compaction) so no step's call/result pair is split.

Source

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

    stage,
  )
}

/** 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

View on GitHub (pinned to b150a551b8)

Solutions

  1. Advance start forward to the next seq where toolPairingBalancedBefore(session, seq) returns true
  2. Prefer step boundaries — the node right before an assistant or user message — as cut points
  3. Let the engine select the range (compactNow / selectCompactableRange-style logic already backs off to balanced cuts)

Example fix

// before: arbitrary cut inside a step
await ctx.compaction.compactRegion(nodes[3]!, end, agent)

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

Strategy: validation

Validate before calling

import { toolPairingBalancedBefore } from '@deepseek-ai/dsh-compaction'
if (!toolPairingBalancedBefore(agent.session, start)) {
  // advance start to the next balanced cut before compacting
  const nodes = agent.session.surface.nodes
  let i = nodes.indexOf(start)
  while (!toolPairingBalancedBefore(agent.session, nodes[i]!)) i += 1
  start = nodes[i]!
}

Prevention

When it happens

Trigger: Choosing `start` inside a multi-tool step — for example on a tool/result that follows an assistant message whose second tool-call is answered by a later node; deriving boundaries by token arithmetic without balance checks; cutting between an assistant tool-call message and its results.

Common situations: Agentic turns that issue parallel tool calls; hand-rolled range selection that ignores tool pairing; picking the 'oldest N tokens' boundary mid-step.

Related errors


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