Repository navigation
perf(staleness): ask pages that share a filter once, not once per page (#6325) - #6407
Merged
Merged
Conversation
#6325) Pages with the same tags and fact types were each re-reading the same matching memories to decide if they are stale. Now each distinct filter is asked for its newest matching write once, and every page compares its own cutoff against it. The aggregate keeps a FILTER so Postgres stays on the tag index instead of walking the whole bank for a filter that matches nothing. 100k memories, 53 pages on one shared tag: 904 ms -> 18 ms. Same answers as the single-scope check in every case measured. Reported with a patch by @harshitgupta31415.
…ted_at directly - _ScopeFilterKey replaces a bare tuple key, matching _ScopeGroupKey next to it - comment on the test's direct read of memory_units.updated_at
This was referenced Nov 6, 2026
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.
Fixes #6325. Thanks @harshitgupta31415 for the report, the benchmark and the patch idea.
What was wrong
The knowledge tree and the mental-model list check every page to see if it's out of date. Pages that watch the same tags and fact types each re-read the same matching memories, so 53 pages on one shared tag meant 53 reads of the same rows. Only each page's cutoff differs.
What changes
max(...) FILTER (WHERE updated_at IS NOT NULL). A baremaxlets Postgres walk the whole bank from newest to oldest, which made a filter that matches nothing go from 0.5 ms to 15 ms in testing. The FILTER keeps it on the tag index.tag_groupskeep their per-scope path.Not taken from the issue's patch: the "check the bank's newest 64 writes first" step. When the newest writes belong to another scope, the check misses and both steps run, so it was slower in testing (34 ms against 18 ms on the shared-tag case below).
Numbers
100k memories, Postgres 18, JIT off, median of 5 runs. Every case gave the same answers as the single-page check.
anymodeTests
test_pages_sharing_a_filter_each_keep_their_own_cutoffcovers both query shapes, with cutoffs before, at and after the write, tags listed in a different order, and a fact-type filter that must stay separate. It fails if the walk starts from the latest cutoff instead of the earliest.test_mental_models.pyandtest_knowledge_base.py: 241 passed locally. 8 errored only because no LLM key was set locally, and they error the same way on main.