Repository navigation
fix(index,agent): stop the daemon's heap growing with every session - #439
Merged
Merged
Conversation
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]>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A soak of
whycodes serve(thousands of/v1sessions 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:
.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. Eachbashcall writes.whycodes/scratch/bg-N.log;.whycodesis whitelisted and the scratch dir's*.gitignorewas never consulted. Ignored build output outsideSKIP_DIRS(out/,bin/,obj/, …) did the same in long TUI sessions.Agent::cache_readskept 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,.ignorebefore.gitignore, then.git/info/excludeand the global file; an ignored parent hides re-includes below it). Directory matchers load lazily; the cache is bounded byMAX_RULE_DIRS(added tomemory_budget.json)..gitignore/.ignorearrives as the newChangeKind::Rules(checked before the name policy, since.ignoreis hidden) and triggers a rescan, which also resets the rule cache — the index follows edited rules both ways.IndexStore::insertreports a new entry.Session::last_cache_read(#[serde(skip)]), so it dies with the session.servewrites its lock before binding, so a port held by another program was reported as "A serve lock exists for pid ".connect_hintnow skips its own lock (hint_forsplit 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'ssession_route/allow_alwaysmaps (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)
Tests
ignore_rules::tests::*: nested rules, re-include, ignored parent beats re-include,.ignoreover.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 inwhycodes-cli, unchanged from main), and llvm-cov-skip-expansionsforwhycodes-index/-agent/-session: no missed lines in touched files (the two misses incontent.rs/walk.rsare identical on main — Windows-only paths). Knowhow entry added.🤖 Generated with Claude Code