-
Notifications
You must be signed in to change notification settings - Fork 0
feat(test-linker): add optional disk cache for test edges (TAP-4080) #193
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
Merged
Changes from all commits
Commits
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
87 changes: 87 additions & 0 deletions
87
packages/tapps-mcp/src/tapps_mcp/project/test_linker_cache.py
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Disk cache for test edges (TAP-4080).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import asdict | ||
| from pathlib import Path | ||
|
|
||
| import structlog | ||
|
|
||
| from tapps_core.cache import AtomicJsonCache, register_cache_stats | ||
| from tapps_mcp.project.test_linker import TestEdge | ||
|
|
||
| logger = structlog.get_logger(__name__) | ||
|
|
||
| TEST_EDGES_CACHE_REL = ".tapps-mcp/test-edges.json" | ||
|
|
||
| # ADR-0029 / TAP-4561: unified cache-stats counters (test edge load hits/misses). | ||
| _stats: dict[str, int] = {"hits": 0, "misses": 0} | ||
| register_cache_stats("test_edges", lambda: dict(_stats)) | ||
|
|
||
|
|
||
| def load_test_edges_cache(project_root: Path) -> list[TestEdge] | None: | ||
| """Load cached test edges from disk, or None if cache is missing/unreadable. | ||
|
|
||
| Returns None on cache miss or read failure; a derived cache treats this | ||
| as a cache miss and rebuilds. | ||
| """ | ||
| path = project_root / TEST_EDGES_CACHE_REL | ||
| if not path.is_file(): | ||
| _stats["misses"] += 1 | ||
| return None | ||
| raw = AtomicJsonCache.read_json(path) | ||
| if raw is None: | ||
| logger.warning("test_edges_cache_read_failed", path=str(path)) | ||
| _stats["misses"] += 1 | ||
| return None | ||
| if not isinstance(raw, list): | ||
| _stats["misses"] += 1 | ||
| return None | ||
| edges = _test_edges_from_list(raw) | ||
| if edges is None: | ||
| _stats["misses"] += 1 | ||
| return None | ||
| _stats["hits"] += 1 | ||
| return edges | ||
|
|
||
|
|
||
| def save_test_edges_cache(project_root: Path, edges: list[TestEdge]) -> None: | ||
| """Save test edges to disk atomically. | ||
|
|
||
| On write failure, logs a warning and leaves the cache untouched. | ||
| """ | ||
| path = project_root / TEST_EDGES_CACHE_REL | ||
| try: | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| # sort_keys/indent preserve the exact prior byte layout (ADR-0029 pilot). | ||
| AtomicJsonCache.write_json(path, _test_edges_to_list(edges), indent=2, sort_keys=True) | ||
| except OSError as exc: | ||
| logger.warning("test_edges_cache_write_failed", path=str(path), error=str(exc)) | ||
|
|
||
|
|
||
| def _test_edges_to_list(edges: list[TestEdge]) -> list[dict[str, object]]: | ||
| """Serialize TestEdge list to JSON-compatible list of dicts.""" | ||
| return [asdict(e) for e in edges] | ||
|
|
||
|
|
||
| def _test_edges_from_list(raw: list[object]) -> list[TestEdge] | None: | ||
| """Deserialize JSON list of dicts back to TestEdge list. | ||
|
|
||
| Returns None if deserialization fails (malformed or wrong structure). | ||
| """ | ||
| try: | ||
| edges: list[TestEdge] = [] | ||
| for item in raw: | ||
| if not isinstance(item, dict): | ||
| return None | ||
| edge = TestEdge( | ||
| test_symbol=str(item.get("test_symbol", "")), | ||
| test_file=str(item.get("test_file", "")), | ||
| code_symbol=str(item.get("code_symbol", "")), | ||
| code_file=str(item.get("code_file", "")), | ||
| line=int(item.get("line", 0)), | ||
| ) | ||
| edges.append(edge) | ||
| return edges | ||
| except (TypeError, ValueError, KeyError): | ||
| return None |
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
Oops, something went wrong.
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.
When
load_or_build_test_edges()is called after a source or test file changes, this branch returns the old.tapps-mcp/test-edges.jsonwithout comparing it to the call-graph fingerprint or any source state.build_call_graph_index()already detects stale graph caches, but this early return skips that path entirely, so affected-test results can stay stale until callers passforce_rebuild=Trueor delete the cache.Useful? React with 👍 / 👎.