Skip to content

Invalidate persistent file cache when Finch source changes#358

Draft
Copilot wants to merge 7 commits into
mainfrom
copilot/invalidate-finch-cache-automatically
Draft

Invalidate persistent file cache when Finch source changes#358
Copilot wants to merge 7 commits into
mainfrom
copilot/invalidate-finch-cache-automatically

Conversation

Copilot AI commented Apr 2, 2026

Copy link
Copy Markdown
Contributor
  • Review new actionable comments from Mateusz
  • Update cache source root detection to use finchlite.__path__[0]
  • Refactor cache e2e test to avoid wrapping the whole test body in try/finally
  • Update repo-root resolution in test to avoid parents[1]
  • Run targeted tests for cache and codegen behavior
  • Reply to actionable new comments with commit hash
  • Run final validation

Copilot AI and others added 2 commits April 2, 2026 20:49
Copilot AI changed the title [WIP] Add automatic cache invalidation for Finch on code changes Invalidate persistent file cache when Finch source changes Apr 2, 2026
Copilot AI requested a review from willow-ahrens April 2, 2026 20:56
Comment thread src/finchlite/util/cache.py Outdated
_finch_source_root = Path(__file__).resolve().parent.parent


@functools.cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set this on load, don't use functool cache

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 197d5a9: removed functools caching and now compute Finch source mtime once at module load (_session_finch_code_mtime_ns).

Comment thread src/finchlite/util/cache.py Outdated
return latest_mtime


def _clear_cache_root(cache_root: Path) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a user-accessible version of this and add a nice docstring, since users may want to use it. make it so the user can just say "clear cache" and it clears the current cache root for the current finch version

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 197d5a9: added user-facing clear_cache() with a docstring, which clears the current Finch version cache root.

path.unlink()


def _ensure_cache_fresh(cache_root: Path) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will only ever be one cache root for a given session. Please simplify accordingly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 197d5a9: simplified freshness tracking to a single session-level _cache_checked flag, assuming one cache root per session.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to put the if _cache_checked in the cache decorator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 9879952: moved the _cache_checked guard into the file_cache decorator and removed that guard from _ensure_cache_fresh. No UI changes (backend-only), so there is no screenshot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 9879952: _cache_checked is now checked in the file_cache decorator before calling _ensure_cache_fresh. No UI changes (backend-only), so there is no screenshot.

Comment thread tests/test_cache.py Outdated
Comment on lines +5 to +6

def test_ensure_cache_fresh_clears_cache_when_code_changes(tmp_path, monkeypatch):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, do an end-to-end test that invokes multiple python sessions and modifies a finch file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 197d5a9: replaced helper-level tests with an end-to-end test that runs separate Python sessions and modifies a Finch source file mtime to verify invalidation.

path.unlink()


def _ensure_cache_fresh(cache_root: Path) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to put the if _cache_checked in the cache decorator

Comment thread tests/test_cache.py
@@ -0,0 +1,57 @@
# AI modified: 2025-01-01T00:00:00Z parent=4f5a2e5021678965ce8d830bb9edecac1dd3fea9

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.py behavior, runs again in a new session, and verifies the new output is used (7 then 9). No UI changes (backend-only), so there is no screenshot.

Comment thread src/finchlite/util/cache.py Outdated
Comment on lines +18 to +19
# util/cache.py lives in src/finchlite/util/, so parent.parent is src/finchlite.
_finch_source_root = Path(__file__).resolve().parent.parent

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that might change in the future (placement of this file), which will surprise the person working on it. I believe that:

import finchlite

_finch_source_root = finchlite.__path__[0]

Is more bulletproof and we don't hardcode this awful .parent.parent thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 37d451f: switched Finch source root detection to Path(finchlite.__path__[0]) so it no longer depends on Path(__file__).resolve().parent.parent. No UI changes (backend-only), so screenshot is not applicable.

Comment thread tests/test_cache.py Outdated
print("RESULT", int(lib.unique_value()))
"""

try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have a whole test in a try block - let's use try for places where we actually expect it to happen and have a recovery in except there (or propagate if that's the case) - the finally contents can be a function that we can reuse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 37d451f: narrowed try/finally to just the part that mutates c_codegen.py and extracted cleanup into _restore_file_state(...) for reuse. No UI changes (backend-only), so screenshot is not applicable.

Comment thread tests/test_cache.py Outdated


def test_c_codegen_cache_invalidation_end_to_end_across_sessions(tmp_path):
repo_root = Path(__file__).resolve().parents[1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .parents[1] instead of .parent? We can access the repo root same way as I described before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in 37d451f: replaced Path(__file__).resolve().parents[1] with Path(finchlite.__path__[0]).parent.parent and added an inline note explaining the path derivation. No UI changes (backend-only), so screenshot is not applicable.

@willow-ahrens

Copy link
Copy Markdown
Member

@mtsokol thanks for your help with this! I'm not sure if it's easier to use AI or to just write it myself. Do you think we should actually try to merge this code?

@mtsokol

mtsokol commented Apr 3, 2026

Copy link
Copy Markdown
Member

@mtsokol thanks for your help with this! I'm not sure if it's easier to use AI or to just write it myself. Do you think we should actually try to merge this code?

I think searching for outdated cache files won't get much prettier than this - still, let's make it more thoughtful then it comes to edge cases, so we won't get surprised in a few months. I can iron it out after Monday next week - if you don't need it earlier.

Have you considered setting up Claude Code for a reviewer assistant instead of Copilot? Drawing on my experience Claude was way more "intelligent" with finch lite code: https://code.claude.com/docs/en/github-actions but that requires Claude Pro subscription.

@willow-ahrens willow-ahrens left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address mateusz's comments

Copilot AI commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Address mateusz's comments

Addressed in 37d451f: applied Mateusz’s requested updates in cache root discovery and the cache e2e test structure/path handling. No UI changes (backend-only), so screenshot is not applicable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalidate finch cache automatically when finch code changes

3 participants