Skip to content

feat(memory): refresh the code index at session start - #473

Merged
mrnasil merged 1 commit into
mainfrom
feat/code-index-refresh
Nov 25, 2026
Merged

mrnasil merged 1 commit into
mainfrom
feat/code-index-refresh

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Problem

The session auto-index (maybe_auto_index_background) builds the code index only when the bank has no code chunks, then never touches it again. As the code changes, turn recall keeps injecting chunks of code that no longer exists, at line ranges that have moved, and new files are never searchable. The only way out was running whycodes memory index by hand.

Change

When the index already exists, session start now runs MemoryService::refresh_code_index on a background thread, quietly (no "Indexing…" notice):

  1. chunks of files that are gone (or no longer walked) are dropped;
  2. files whose mtime is newer than their oldest chunk are re-indexed;
  3. new files are added while the total stays under auto_index_max_chunks.

A file that cannot be stat'ed, or a timestamp that cannot be parsed, counts as changed; re-indexing it drops it if it is gone. The first build and memory index are unchanged (they share a new index_file helper).

Storage: code_chunk_files (path, chunk count, oldest updated_at) and delete_code_chunks_for_path; FTS stays in sync through the existing triggers.

Cost

On this repo with nothing changed (debug build): 70 ms of walking and stat'ing in the background, against 13 s for a full build.

Checks

  • cargo test -p whycodes-memory --lib (94). New tests cover: unchanged → no-op; edited / deleted / new / too-short-to-index files; no room under the cap; disabled; changed_since for future, past, unparsable and missing; background Ok / Err.
  • cargo test -p whycodes-storage --lib (45, plus error paths for the two new queries)
  • Local line coverage, CI-equivalent (-skip-expansions, summary): storage 1456/1456; memory misses identical to main (Windows-only lines).
  • clippy, fmt, cargo check --workspace --tests, ratchets

🤖 Generated with Claude Code

The session auto-index was built once, when the bank had no code chunks,
and never touched again. As code changed, turn recall kept offering
chunks of code (and line ranges) that no longer existed, and new files
were never searchable.

When the index already exists, session start now refreshes it on a
background thread, quietly: chunks of files that are gone are dropped,
files modified since they were indexed are indexed again, and new files
are added while there is room under auto_index_max_chunks. With nothing
changed this is a walk and a stat per file: 70 ms on this repo in a
debug build, against 13 s for a full build.

Storage gains code_chunk_files (path, chunk count, oldest updated_at)
and delete_code_chunks_for_path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 817ac42 into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the feat/code-index-refresh branch November 25, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant