Skip to content

fix(index,agent): stop the daemon's heap growing with every session - #439

Merged
mrnasil merged 3 commits into
mainfrom
fix/index-watcher-ignore-leak
Nov 9, 2026
Merged

mrnasil merged 3 commits into
mainfrom
fix/index-watcher-ignore-leak

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 9, 2026

Copy link
Copy Markdown
Member

Problem

A soak of whycodes serve (thousands of /v1 sessions against a local mock provider, load → idle cycles) never returned to its idle heap. With a counting #[global_allocator] and a live-allocation size histogram, every session that had already left memory still held 8 allocations / ~222 B (more with hash-table growth). Handles and threads stayed flat, so the sessions themselves were freed.

Backtraces of the retained sizes pointed at three causes:

  1. Index watcher ignored .gitignore / .ignore / exclude rules. The full scan honours them; watcher deltas were filtered by the name policy only. Every file created later under an ignored path was indexed (path, nucleo item, content trigrams) for the life of the process. Each bash call writes .whycodes/scratch/bg-N.log; .whycodes is whitelisted and the scratch dir's * .gitignore was never consulted. Ignored build output outside SKIP_DIRS (out/, bin/, obj/, …) did the same in long TUI sessions.
  2. Duplicate picker items on repeat upserts. nucleo cannot replace an item, yet every upsert of a path the index already held pushed another one. Each save of a file, and each new file in a directory (Windows reports the directory as modified), left a duplicate until a removal rebuilt the engine.
  3. Agent::cache_reads kept the last prompt-cache read for every session id the (daemon-lifetime) agent ever ran.

Fix

  • index::ignore_rules::IgnoreRules: checks each watcher upsert against the walker's rule files with the walker's precedence (deeper dir first, .ignore before .gitignore, then .git/info/exclude and the global file; an ignored parent hides re-includes below it). Directory matchers load lazily; the cache is bounded by MAX_RULE_DIRS (added to memory_budget.json).
  • A changed .gitignore / .ignore arrives as the new ChangeKind::Rules (checked before the name policy, since .ignore is hidden) and triggers a rescan, which also resets the rule cache — the index follows edited rules both ways.
  • Upserts reach nucleo only when IndexStore::insert reports a new entry.
  • The cache-read value moved to Session::last_cache_read (#[serde(skip)]), so it dies with the session.
  • Unrelated small fix found while testing: serve writes its lock before binding, so a port held by another program was reported as "A serve lock exists for pid ". connect_hint now skips its own lock (hint_for split out for a deterministic test).

Not changed, on purpose: Agent::session_starts (documented "once per session id per process"; only grows with hooks configured) and the server's session_route / allow_always maps (explicit user choices that are not persisted; dropping them on eviction would lose them).

Measurement (Windows, release build + counting allocator, 3 × (2 min load, 1.5 min idle), session idle-evict shortened to 60 s)

Build Retained per session after eviction Live heap at idle, cycle 1 → 3
main 8.0 allocs, 222 B 1.88 → 2.81 MB
+ ignore rules, cache read on session 1.06 allocs, 77 B 1.68 → 1.89 MB
+ no duplicate nucleo items (this PR) 0 1.61 → 1.61 MB (2401 live allocs, flat)

Tests

  • ignore_rules::tests::*: nested rules, re-include, ignored parent beats re-include, .ignore over .gitignore, exclude file, scratch dir, cache clear, cache bound, bad lines.
  • watcher_upserts_under_ignored_paths_are_dropped, rules_change_rescans_with_fresh_rules, repeated_upserts_do_not_duplicate_picker_items (fails on main: 4 hits for one file), rules_files_map_to_rules_changes.
  • cache_read_notes_compare_with_the_last_step_of_the_same_session, hint_for_names_another_live_server.

Local: fmt, clippy (-D warnings) on touched crates, all five budget scripts, affected tests (only the four known Windows-env failures in whycodes-cli, unchanged from main), and llvm-cov -skip-expansions for whycodes-index / -agent / -session: no missed lines in touched files (the two misses in content.rs / walk.rs are identical on main — Windows-only paths). Knowhow entry added.

🤖 Generated with Claude Code

mrnasil and others added 3 commits November 9, 2026 14:03
A soak of `whycodes serve` showed the live heap ~650 B higher after every
session had left memory: 8 allocations per session, never freed.

- index: watcher deltas skipped `.gitignore` / `.ignore` / exclude rules.
  The full scan honours them, but files created later under an ignored
  path were indexed (path, fuzzy item, content trigrams) for the life of
  the process. Every `bash` call writes `.whycodes/scratch/bg-N.log`, so a
  long session or the daemon kept one entry per shell command; ignored
  build output outside `SKIP_DIRS` did the same. `IgnoreRules` now checks
  each upsert with the walker's precedence, and an edited rules file
  arrives as `ChangeKind::Rules` and rescans. Its directory cache is
  bounded (`MAX_RULE_DIRS`, memory budget).
- agent: the last prompt-cache read moved from a per-agent map keyed by
  session id to `Session::last_cache_read`, so it dies with the session.
- cli: `serve` writes its lock before binding, so a port held by another
  program was reported as "a serve lock exists for pid <own pid>". The
  hint now skips its own lock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nucleo has no item replace, yet every watcher upsert of a path the index
already held pushed another item. Each save of a file, and each new file in
a directory (Windows reports the directory as modified), left a duplicate
until a removal rebuilt the engine: one more allocation per shell command
in the daemon soak after the ignore-rules fix. Push only when
`IndexStore::insert` reports a new entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 1bcd4a2 into main Nov 9, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/index-watcher-ignore-leak branch November 9, 2026 11:43
@mrnasil mrnasil mentioned this pull request Nov 9, 2026
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