fix(memory): prevent stuck singleton leaves from starving TiMem consolidation#6402
Merged
Conversation
bug-ops
enabled auto-merge (squash)
July 17, 2026 17:42
bug-ops
force-pushed
the
fix/6393-timem-leaf-starvation
branch
from
July 17, 2026 17:42
7d75818 to
e5c79c8
Compare
…lidation load_tree_leaves_unconsolidated loaded the oldest batch_size unconsolidated leaves every sweep; leaves that never cluster (singletons) keep parent_id NULL, so once more than batch_size singletons accumulate, every sweep re-loads the same stuck set and newer leaves never get a chance. Add a consolidation_attempts/last_attempted_at pair on memory_tree that the sweep bumps for any loaded node that doesn't end up consolidated. Both load queries now order by true LRU (COALESCE(last_attempted_at, created_at) ASC) instead of strict age, giving a bounded-wait guarantee: a leaf's touch time freezes in the past while competing leaves' touch times keep refreshing, so a stuck leaf's relative priority strictly increases until it wins. Selection priority and presentation order are decoupled via a subquery, since cluster_by_similarity requires created_at ASC for deterministic clustering regardless of which rows were selected.
bug-ops
force-pushed
the
fix/6393-timem-leaf-starvation
branch
from
July 17, 2026 18:06
e5c79c8 to
0bab92a
Compare
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.
Summary
load_tree_leaves_unconsolidatedloaded the oldestbatch_sizeunconsolidated leaves every consolidation sweep. Leaves that never cluster (singletons) keepparent_idNULL forever, so once more thanbatch_sizesingletons accumulate, every sweep re-loads and re-embeds the same stuck oldest set — newer leaves with real memorable content never get a chance to be considered.insert_tree_leafa production caller; flagged out of scope during that PR's implementation critique.consolidation_attempts/last_attempted_atpair onmemory_tree(migration 111, sqlite + postgres) thatrun_tree_consolidation_sweepbumps for every loaded node that does not end up consolidated. Bothload_tree_leaves_unconsolidatedandload_tree_levelnow order by true LRU (COALESCE(last_attempted_at, created_at) ASC, withconsolidation_attempts/idas deterministic tiebreakers) instead of strict age — a bounded-wait guarantee, not just a bias, since a stuck leaf's touch time freezes in the past while competing leaves' touch times keep refreshing to "now".cluster_by_similarity's greedy leader algorithm requirescreated_at ASCfor deterministic clustering regardless of which rows were selected.Closes #6393
Test plan
cargo +nightly fmt --checkcargo clippy --profile ci --workspace --all-targets --features "desktop,ide,server,chat,pdf,scheduler,testing" -- -D warningscargo nextest run --config-file .github/nextest.toml --workspace --features "desktop,ide,server,chat,pdf,scheduler" --lib --bins(14119 passed, 0 failed, 35 skipped)RUSTFLAGS="-D warnings" RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps --workspace --features "desktop,ide,server,chat,pdf,scheduler"gitleaks protect --staged --no-banner --redactzeph-dbmigration parity tests (sqlite/postgres dialect symmetry for migration 111)run_tree_consolidation_sweepis reached from the supervisedmem-tree-consolidationbackground task, not just from tests