deepseek-ai/deepseek-harness · error · Error

compactRegion: start seq ${start} (position ${startIdx}) is

Error message

compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface

What it means

The surface position of `start` is after the position of `end`. Compaction spans are positional, and because a replacement can land a fresh high-seq summary node at an older position, visible seqs can be non-monotonic — the pair must be ordered by index in session.surface.nodes, never by numeric seq comparison.

Source

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

 */
export function assertNoActiveCompaction(session: Session, stage: string): void {
  const entryState = inspectCompactionEntryState(session.events)
  assertCompactionInactive(
    entryState.unmatchedCompactionStart,
    entryState.latestEndSeedSeq,
    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,

View on GitHub (pinned to b150a551b8)

Solutions

  1. Order the pair by nodes.indexOf(start) < nodes.indexOf(end) before calling
  2. Treat shadowedSeqs and CompactionResult as the authoritative ordering, not numeric comparison
  3. Pick both boundaries fresh from the current surface after any replacement

Example fix

// before: numeric ordering assumed
await ctx.compaction.compactRegion(Math.min(a, b), Math.max(a, b), agent)

// after: positional ordering from the surface
const nodes = agent.session.surface.nodes
const [start, end] = nodes.indexOf(a) <= nodes.indexOf(b) ? [a, b] : [b, a]
await ctx.compaction.compactRegion(start, end, agent)
Defensive patterns

Strategy: validation

Validate before calling

const nodes = agent.session.surface.nodes
const s = nodes.indexOf(start)
const e = nodes.indexOf(end)
if (s === -1 || e === -1 || s > e) {
  throw new RangeError('span is missing or reversed on the current surface')
}

Prevention

When it happens

Trigger: Calling compactRegion(high, low) after a prior compaction replacement reshuffled seq ordering, assuming numeric order defines the span; building the pair from a reversed list; sorting boundary seqs numerically before the call.

Common situations: A checkpoint landed mid-history and numeric ordering no longer matches surface order; reusing old (start, end) tuples on a surface whose positions changed; sorting shadowedSeqs with a default numeric sort.

Related errors


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