{"record":{"id":"6968bd62504459ab","repo":"deepseek-ai/deepseek-harness","slug":"compactregion-start-seq-start-position-star","errorCode":null,"errorMessage":"compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface","messagePattern":"compactRegion: start seq (.+?) \\(position (.+?)\\) is after end seq (.+?) \\(position (.+?)\\) on the surface","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/compaction/compaction-basic/src/region.ts","lineNumber":322,"sourceCode":" */\nexport function assertNoActiveCompaction(session: Session, stage: string): void {\n  const entryState = inspectCompactionEntryState(session.events)\n  assertCompactionInactive(\n    entryState.unmatchedCompactionStart,\n    entryState.latestEndSeedSeq,\n    stage,\n  )\n}\n\n/** Validate one requested surface-position span before asynchronous work begins. */\nfunction validateSurfaceRegion(session: Session, start: number, end: number): SurfaceSelection {\n  const nodes = session.surface.nodes\n  const startIdx = nodes.indexOf(start)\n  const endIdx = nodes.indexOf(end)\n  if (startIdx === -1) throw new Error(`compactRegion: start seq ${start} not found in surface`)\n  if (endIdx === -1) throw new Error(`compactRegion: end seq ${end} not found in surface`)\n  if (startIdx > endIdx) {\n    throw new Error(\n      `compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface`,\n    )\n  }\n  // oxlint-disable-next-line typescript/no-non-null-assertion\n  if (!toolPairingBalancedBefore(session, nodes[startIdx]!)) {\n    throw new Error(`compactRegion: start seq ${start} is not a balanced boundary (would split a step's tool-call/result pair)`)\n  }\n  // oxlint-disable-next-line typescript/no-non-null-assertion\n  if (!toolPairingBalancedAfter(session, nodes[endIdx]!)) {\n    throw new Error(`compactRegion: end seq ${end} is not a balanced boundary (would split a step, or the step is still open)`)\n  }\n\n  return { start, end, startIdx, endIdx, shadowedSeqs: nodes.slice(startIdx, endIdx + 1) }\n}\n\n/** Snapshot pricing and replay input for a validated surface range. */\nfunction prepareCompaction(\n  dependencies: RegionDependencies,","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/deepseek-ai/deepseek-harness/blob/b150a551b8d465e31e418e1b2eaf5e79bbb7d28e/packages/compaction/compaction-basic/src/region.ts#L304-L340","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Order the pair by nodes.indexOf(start) < nodes.indexOf(end) before calling","Treat shadowedSeqs and CompactionResult as the authoritative ordering, not numeric comparison","Pick both boundaries fresh from the current surface after any replacement"],"exampleFix":"// before: numeric ordering assumed\nawait ctx.compaction.compactRegion(Math.min(a, b), Math.max(a, b), agent)\n\n// after: positional ordering from the surface\nconst nodes = agent.session.surface.nodes\nconst [start, end] = nodes.indexOf(a) <= nodes.indexOf(b) ? [a, b] : [b, a]\nawait ctx.compaction.compactRegion(start, end, agent)","handlingStrategy":"validation","validationCode":"const nodes = agent.session.surface.nodes\nconst s = nodes.indexOf(start)\nconst e = nodes.indexOf(end)\nif (s === -1 || e === -1 || s > e) {\n  throw new RangeError('span is missing or reversed on the current surface')\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Order span edges by surface index, never by numeric seq comparison — replacements make visible seqs non-monotonic","Treat shadowedSeqs (surface order) as the authoritative ordering","Validate both indexOf results before calling compactRegion"],"tags":["compaction","surface","span-ordering","validation"],"backgroundTag":"reversed-range-bounds","analyzedSha":"b150a551b8d465e31e418e1b2eaf5e79bbb7d28e","analyzedAt":"2026-08-24T18:12:29.105Z","schemaVersion":2},"datasetVersion":"2026-08-24T22:17:12.610Z"}