chatboxai/chatbox · warning · Error

Offset ${startLine} is beyond end of file (${totalLines} lin

Error message

Offset ${startLine} is beyond end of file (${totalLines} lines total)

What it means

Thrown by the sandboxed Node read program emitted by buildSandboxReadScript. After streaming the entire file to count totalLines, if startLine exceeds totalLines (and the file is not empty with startLine===1, the 'read empty file from line 1' special case) the program throws before writing its JSON line. The exit path prints error.message to stderr and sets process.exitCode=1.

Source

Thrown at src/main/sandbox/file-read.ts:53

;(async () => {
  for await (const line of lines) {
    totalLines++
    if (totalLines >= startLine && selected.length < limit && !selectionFull) {
      const candidate = line.length > maxLineLength ? line.slice(0, maxLineLength - 3) + '...' : line
      // JSON.stringify includes the surrounding quotes. Subtract those two bytes, then account
      // for the escaped newline separator that selected.join('\\n') adds between lines.
      const candidateBytes = Buffer.byteLength(JSON.stringify(candidate), 'utf8') - 2
      const separatorBytes = selected.length > 0 ? 2 : 0
      if (selectedBytes + separatorBytes + candidateBytes > maxContentBytes) {
        selectionFull = true
      } else {
        selected.push(candidate)
        selectedBytes += separatorBytes + candidateBytes
      }
    }
  }
  if (startLine > totalLines && !(totalLines === 0 && startLine === 1)) {
    throw new Error('Offset ' + startLine + ' is beyond end of file (' + totalLines + ' lines total)')
  }
  const endLine = selected.length > 0 ? startLine + selected.length - 1 : 0
  process.stdout.write(JSON.stringify({ content: selected.join('\\n'), startLine, endLine, totalLines }))
})().catch((error) => {
  console.error(error instanceof Error ? error.message : String(error))
  process.exitCode = 1
})
`
}

View on GitHub (pinned to 81571269ad)

Solutions

  1. Before invoking the reader, clamp startLine to Math.min(startLine, lastKnownTotalLines) and treat startLine > totalLines as 'no more content' rather than an error.
  2. On this error, treat totalLines as authoritative (re-read with startLine=1 or surface 'file changed, reload').
  3. If reading volatile files, capture a fresh totalLines immediately before paginating instead of trusting a cached value.
  4. Guard the empty-file edge case: if you expect possibly-empty files, pass startLine=1 (the special case is handled).

Example fix

// before
const startLine = requestedOffset // can exceed file length

// after
const startLine = Math.max(1, Math.min(requestedOffset, lastKnownTotalLines || 1))
Defensive patterns

Strategy: validation

Validate before calling

const startLine = requestedOffset
if (lastKnownTotalLines && startLine > lastKnownTotalLines) { return { content: '', startLine, endLine: 0, totalLines: lastKnownTotalLines } }

Type guard

function isOffsetBeyondEof(e: unknown): e is Error {
  return e instanceof Error && /^Offset \d+ is beyond end of file/.test(e.message)
}

Try / catch

try { return await readSandboxFile(path, requestedOffset, limit) }
catch (e) { if (isOffsetBeyondEof(e)) { return { content: '', startLine: requestedOffset, endLine: 0, totalLines: 0 /* re-fetch */ } } throw e }

Prevention

When it happens

Trigger: Caller passes startLine greater than the file's actual line count (e.g. requesting offset 500 from a 50-line file), or the file was truncated/replaced with shorter content between when the caller fetched totalLines and when the read script runs. Also when startLine was computed from a stale totalLines cached elsewhere.

Common situations: Pagination UI held an old totalLines and the file shrank (log rotation, overwrite); a tool computed startLine = lastEndLine + 1 without clamping to totalLines; reading a file that is actively being written and got rotated; off-by-one where startLine=totalLines+1.

Related errors


AI-assisted analysis of chatboxai/chatbox@81571269ad (2026-08-12). Data as JSON: /api/errors/4f70498b9679234f. Report an issue: GitHub.