-
Notifications
You must be signed in to change notification settings - Fork 15
Invalidate persistent file cache when Finch source changes #358
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
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/invalidate-finch-cache-automatically
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−3
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
154b5ae
Initial plan
Copilot 06953a7
feat: invalidate cache when finch source changes
Copilot 4f5a2e5
chore: refine cache invalidation robustness and tests
Copilot 197d5a9
refactor: simplify cache freshness and add clear_cache API
Copilot 9879952
test: add c_codegen e2e cache invalidation scenario
Copilot 36276c2
Merge branch 'main' into copilot/invalidate-finch-cache-automatically
willow-ahrens 37d451f
refactor: polish cache root lookup and test structure
Copilot 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| from .cache import file_cache | ||
| # AI modified: 2025-01-01T00:00:00Z parent=4f5a2e5021678965ce8d830bb9edecac1dd3fea9 | ||
| # AI modified: 2025-01-01T00:01:00Z parent=4f5a2e5021678965ce8d830bb9edecac1dd3fea9 | ||
| from .cache import clear_cache, file_cache | ||
| from .config import config, get_version | ||
| from .print import qual_str | ||
|
|
||
| __all__ = ["config", "file_cache", "get_version", "qual_str"] | ||
| __all__ = ["clear_cache", "config", "file_cache", "get_version", "qual_str"] |
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # AI modified: 2025-01-01T00:00:00Z parent=4f5a2e5021678965ce8d830bb9edecac1dd3fea9 | ||
| # AI modified: 2025-01-01T00:01:00Z parent=4f5a2e5021678965ce8d830bb9edecac1dd3fea9 | ||
| # AI modified: 2026-04-02T22:59:00Z parent=197d5a907823d2a53fcd3b68b674f3f4d4f50b5d | ||
| # AI modified: 2026-04-03T15:30:00Z parent=36276c257318d74488f81fa8107d2f2d0a8b804c | ||
| import os | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import finchlite | ||
|
|
||
|
|
||
| def _run_codegen_session( | ||
| repo_root: Path, | ||
| data_path: Path, | ||
| python_code: str, | ||
| ): | ||
| env = os.environ.copy() | ||
| env["FINCHLITE_DATA_PATH"] = str(data_path) | ||
| return subprocess.run( | ||
| [sys.executable, "-c", python_code], | ||
| cwd=repo_root, | ||
| env=env, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
|
|
||
|
|
||
| def _restore_file_state(path: Path, *, contents: str, atime_ns: int, mtime_ns: int): | ||
| path.write_text(contents) | ||
| os.utime(path, ns=(atime_ns, mtime_ns)) | ||
|
|
||
|
|
||
| def test_c_codegen_cache_invalidation_end_to_end_across_sessions(tmp_path): | ||
| # finchlite.__path__[0] points to <repo>/src/finchlite, so parent.parent is repo root. | ||
| repo_root = Path(finchlite.__path__[0]).parent.parent | ||
| data_path = tmp_path / "finch_data" | ||
| c_codegen_file = repo_root / "src" / "finchlite" / "codegen" / "c_codegen.py" | ||
| stat_before = c_codegen_file.stat() | ||
| original_contents = c_codegen_file.read_text() | ||
| base_script = """ | ||
| import finchlite.codegen.c_codegen as ccg | ||
|
|
||
| code = ''' | ||
| int unique_value() { | ||
| return 7; | ||
| } | ||
| ''' | ||
| ccg.load_shared_lib.cache_clear() | ||
| lib = ccg.load_shared_lib(code) | ||
| print("RESULT", int(lib.unique_value())) | ||
| """ | ||
|
|
||
| first = _run_codegen_session(repo_root, data_path, base_script) | ||
| first_result = int(first.stdout.strip().split("RESULT ", 1)[1]) | ||
| assert first_result == 7 | ||
|
|
||
| try: | ||
| modified_contents = re.sub( | ||
| r"c_file_path\.write_text\(c_code\)", | ||
| 'c_file_path.write_text(c_code.replace("return 7;", "return 9;"))', | ||
| original_contents, | ||
| count=1, | ||
| ) | ||
| c_codegen_file.write_text(modified_contents) | ||
|
|
||
| second = _run_codegen_session(repo_root, data_path, base_script) | ||
| second_result = int(second.stdout.strip().split("RESULT ", 1)[1]) | ||
| assert second_result == 9 | ||
| finally: | ||
| _restore_file_state( | ||
| c_codegen_file, | ||
| contents=original_contents, | ||
| atime_ns=stat_before.st_atime_ns, | ||
| mtime_ns=stat_before.st_mtime_ns, | ||
| ) | ||
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.
I need an end-to-end test that runs the c_codegen, changes something about c codegen, and then runs it again in a new session and verifies the cache was invalidated and the new output is used.
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.
Implemented in 9879952: replaced the cache test with a c_codegen end-to-end multi-session test that runs codegen, modifies
c_codegen.pybehavior, runs again in a new session, and verifies the new output is used (7then9). No UI changes (backend-only), so there is no screenshot.