-
Notifications
You must be signed in to change notification settings - Fork 6.7k
perf: graph cache with write-invalidation in build_graph() #661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+84
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ def fake_get(limit=1000, offset=0, include=None): | |
| build_graph, | ||
| find_tunnels, | ||
| graph_stats, | ||
| invalidate_graph_cache, | ||
| traverse, | ||
| ) | ||
|
|
||
|
|
@@ -38,6 +39,9 @@ def fake_get(limit=1000, offset=0, include=None): | |
|
|
||
|
|
||
| class TestBuildGraph: | ||
| def setup_method(self): | ||
| invalidate_graph_cache() | ||
|
|
||
| def test_empty_collection(self): | ||
| col = _make_fake_collection([]) | ||
| nodes, edges = build_graph(col=col) | ||
|
|
@@ -114,11 +118,43 @@ def test_dates_capped_at_five(self): | |
| nodes, _ = build_graph(col=col) | ||
| assert len(nodes["busy"]["dates"]) <= 5 | ||
|
|
||
| def test_cache_returns_same_result(self): | ||
| """Second call within TTL returns cached nodes without re-scanning. | ||
|
|
||
| The cache intentionally ignores col/config args when warm — this is | ||
| correct for the MCP server's single-palace use case. Callers that | ||
| switch collections must call invalidate_graph_cache() first. | ||
| """ | ||
| col = _make_fake_collection( | ||
| [{"room": "auth", "wing": "wing_code", "hall": "security", "date": "2026-01-01"}] | ||
| ) | ||
| nodes1, edges1 = build_graph(col=col) | ||
| # Second call with a *different* collection — should still return cached result | ||
| col2 = _make_fake_collection([]) | ||
| nodes2, edges2 = build_graph(col=col2) | ||
| assert nodes1 == nodes2 | ||
| assert edges1 == edges2 | ||
|
Comment on lines
+132
to
+136
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documented — expanded docstring to explain this verifies intentional single-palace cache behavior. If multi-palace support is needed, cache key should be extended and this test updated. 277e476 |
||
|
|
||
| def test_invalidate_clears_cache(self): | ||
| """invalidate_graph_cache() forces a fresh scan on next call.""" | ||
| col = _make_fake_collection( | ||
| [{"room": "auth", "wing": "wing_code", "hall": "security", "date": "2026-01-01"}] | ||
| ) | ||
| build_graph(col=col) | ||
| invalidate_graph_cache() | ||
| col_empty = _make_fake_collection([]) | ||
| nodes, edges = build_graph(col=col_empty) | ||
| assert nodes == {} | ||
| assert edges == [] | ||
|
|
||
|
|
||
| # --- traverse --- | ||
|
|
||
|
|
||
| class TestTraverse: | ||
| def setup_method(self): | ||
| invalidate_graph_cache() | ||
|
|
||
| def _build_col(self): | ||
| return _make_fake_collection( | ||
| [ | ||
|
|
@@ -156,6 +192,9 @@ def test_traverse_max_hops(self): | |
|
|
||
|
|
||
| class TestFindTunnels: | ||
| def setup_method(self): | ||
| invalidate_graph_cache() | ||
|
|
||
| def _build_tunnel_col(self): | ||
| return _make_fake_collection( | ||
| [ | ||
|
|
@@ -192,6 +231,9 @@ def test_find_tunnels_both_wings(self): | |
|
|
||
|
|
||
| class TestGraphStats: | ||
| def setup_method(self): | ||
| invalidate_graph_cache() | ||
|
|
||
| def test_empty_graph(self): | ||
| col = _make_fake_collection([]) | ||
| stats = graph_stats(col=col) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documented — added comment explaining this is intentional for the MCP server's single-palace use case. Callers switching collections should call
invalidate_graph_cache()first. 277e476