-
Notifications
You must be signed in to change notification settings - Fork 7
Implement state persistence and resume functionality for culprit finder #105
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
danielibarrola
merged 3 commits into
google-ml-infra:feature/culprit-finder
from
danielibarrola:feature/culprit-finder-local-state
Jan 6, 2026
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
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
119 changes: 119 additions & 0 deletions
119
culprit_finder/src/culprit_finder/culprit_finder_state.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,119 @@ | ||
| """Manages the state of the culprit finder to persist it across runs.""" | ||
|
|
||
| from __future__ import annotations | ||
| import json | ||
| from pathlib import Path | ||
| from typing import TypedDict, Literal | ||
|
|
||
|
|
||
| _STATE_ROOT_DIRNAME = ".github_culprit_finder" | ||
|
|
||
| COMMIT_STATUS = Literal["PASS", "FAIL"] | ||
|
|
||
|
|
||
| class CulpritFinderState(TypedDict): | ||
| repo: str | ||
| workflow: str | ||
| original_start: str | ||
| original_end: str | ||
| current_good: str | ||
| current_bad: str | ||
| cache: dict[str, COMMIT_STATUS] | ||
|
|
||
|
|
||
| def _sanitize_component(value: str) -> str: | ||
| """ | ||
| Sanitizes a string so it is safe to use as a filesystem path component. | ||
|
|
||
| This is used when turning user-provided values like repository names | ||
| (`owner/repo`) and workflow filenames into directory/file names. | ||
|
|
||
| The sanitization is intentionally conservative and focuses on preventing: | ||
| - path traversal (`..`) | ||
| - accidental directory separators on Windows (`\\`) | ||
| - characters that are problematic in filenames on common platforms (e.g. `:`) | ||
|
|
||
| Args: | ||
| value: Raw component string (e.g. repo owner, repo name, workflow file name). | ||
|
|
||
| Returns: | ||
| A sanitized string suitable for use as a single path component. | ||
| """ | ||
| return ( | ||
| value.strip() | ||
| .replace("..", ".") | ||
| .replace("\\", "_") | ||
| .replace(":", "_") | ||
| .replace("|", "_") | ||
| ) | ||
|
|
||
|
|
||
| class StatePersister: | ||
| """Handles the persistence of the CulpritFinderState.""" | ||
|
|
||
| def __init__(self, repo: str, workflow: str): | ||
| self._repo = repo | ||
| self._workflow = workflow | ||
|
|
||
| def _get_base_dir(self) -> Path: | ||
| """Returns the base directory for the repo state.""" | ||
| home = Path.home() | ||
| root = home / _STATE_ROOT_DIRNAME | ||
|
|
||
| repo_path = Path(*[ | ||
| _sanitize_component(p) for p in self._repo.split("/") if p.strip() | ||
| ]) | ||
|
|
||
| return root / repo_path | ||
|
|
||
| def _get_file_path(self) -> Path: | ||
| """Returns the path to the state file.""" | ||
| safe_workflow = _sanitize_component(self._workflow) or "default" | ||
| return self._get_base_dir() / f"{safe_workflow}.json" | ||
|
|
||
| def _ensure_directory_exists(self) -> None: | ||
| """Creates the necessary directories for storage.""" | ||
| self._get_base_dir().mkdir(parents=True, exist_ok=True) | ||
|
|
||
| def exists(self) -> bool: | ||
| """Checks if the state file exists. | ||
|
|
||
| Returns: | ||
| bool: True if the state file exists, False otherwise. | ||
| """ | ||
| return self._get_file_path().exists() | ||
|
|
||
| def save(self, state: CulpritFinderState) -> None: | ||
| """Saves the state to disk. | ||
|
|
||
| Args: | ||
| state: The CulpritFinderState object to save. | ||
| """ | ||
| self._ensure_directory_exists() | ||
| state_path = self._get_file_path() | ||
| with state_path.open("w", encoding="utf-8") as f: | ||
| json.dump(state, f) | ||
|
|
||
| def load(self) -> CulpritFinderState: | ||
| """Loads the state from disk. | ||
|
|
||
| Returns: | ||
| CulpritFinderState: The loaded CulpritFinderState object. | ||
| """ | ||
| state_path = self._get_file_path() | ||
| with state_path.open("r", encoding="utf-8") as f: | ||
| data = json.load(f) | ||
| return { | ||
| "repo": data["repo"], | ||
| "workflow": data["workflow"], | ||
| "original_start": data["original_start"], | ||
| "original_end": data["original_end"], | ||
| "current_good": data.get("current_good", ""), | ||
| "current_bad": data.get("current_bad", ""), | ||
| "cache": data.get("cache", {}), | ||
| } | ||
|
|
||
| def delete(self) -> None: | ||
| """Deletes the state file.""" | ||
| state_path = self._get_file_path() | ||
| state_path.unlink() |
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.
Uh oh!
There was an error while loading. Please reload this page.