Skip to content

fix: use high-water mark for incremental indexing (fixes #84)#85

Closed
anastasiiaanfimova wants to merge 1 commit into
obra:mainfrom
anastasiiaanfimova:fix/incremental-indexing-high-water-mark
Closed

fix: use high-water mark for incremental indexing (fixes #84)#85
anastasiiaanfimova wants to merge 1 commit into
obra:mainfrom
anastasiiaanfimova:fix/incremental-indexing-high-water-mark

Conversation

@anastasiiaanfimova

Copy link
Copy Markdown

Problem

The indexer skips a file entirely if COUNT(*) > 0 for its archive_path:

const alreadyIndexed = db.prepare(
  'SELECT COUNT(*) as count FROM exchanges WHERE archive_path = ?'
).get(archivePath) as { count: number };

if (alreadyIndexed.count > 0) continue;

This means any conversation that was indexed in a previous run will never receive new exchanges — even if the source .jsonl file has grown since then. Fixes #84.

Fix

Replace the count check with a high-water mark using MAX(line_end). After parsing, filter out exchanges already covered by the high-water mark. The archive copy is also updated whenever the file was partially indexed (it may have grown since the last copy).

const maxIndexedResult = db.prepare(
  'SELECT COALESCE(MAX(line_end), 0) as max_line FROM exchanges WHERE archive_path = ?'
).get(archivePath) as { max_line: number };
const maxIndexedLine = maxIndexedResult.max_line;

if (!fs.existsSync(archivePath) || maxIndexedLine > 0) {
  fs.copyFileSync(sourcePath, archivePath);
}

const exchanges = await parseConversation(sourcePath, project, archivePath);
if (exchanges.length === 0) continue;

const newExchanges = maxIndexedLine > 0
  ? exchanges.filter(e => e.lineStart > maxIndexedLine)
  : exchanges;

if (newExchanges.length === 0) continue;

unprocessed.push({ ...conv, exchanges: newExchanges });

No schema migration needed — line_start and line_end are already stored per exchange.

Testing

Verified locally: after rebuild, subsequent index runs correctly pick up new exchanges appended to previously-indexed files.

@obra obra added bug Something isn't working area:indexer Indexer/file discovery labels May 2, 2026
obra added a commit that referenced this pull request May 2, 2026
The indexer skipped any file with COUNT(*) > 0 in exchanges, so once a
transcript had been indexed it never gained new rows. Resumed sessions
and concurrent SessionStart syncs that raced a still-running session
left the tail permanently invisible to search.

Schema already stored line_start/line_end per exchange, so the fix is a
high-water-mark check: SELECT MAX(line_end), then filter parsed
exchanges to those with lineStart past that mark. Transcript JSONLs are
append-only, so monotonicity holds and IDs from prior runs are
preserved.

The archive copy is refreshed whenever maxIndexedLine > 0, since the
source may have grown since the last copy.

Test: indexUnprocessed against a temp source with 2 exchanges, append 3
more, re-index, verify all 5 are present in the DB.

Credit: independent re-derivation of @anastasiiaanfimova's #85 via TDD.

Closes #84
@obra

obra commented May 2, 2026

Copy link
Copy Markdown
Owner

Hi @anastasiiaanfimova — thanks for diagnosing this and proposing the fix. The bug + your high-water-mark approach were correct. Re-derived in fa02569 via TDD: red test that indexes 2 exchanges, appends 3 more, re-indexes, asserts 5 in the DB. Closing in favor of the merged version, but the credit is yours.

Original reporter @jamster — your detection script and the file-distribution stats were what made this actionable. Thank you.

Closes #84.

— Claude Opus 4.7, Claude Code 2.1.119

@obra obra closed this May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:indexer Indexer/file discovery bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Indexer skips entire file when any rows exist, leaving appended turns permanently unindexed

2 participants