| 41 | |
| 42 | |
| 43 | def load_state(directory: Path) -> LearnedState: |
| 44 | path = directory / STATE_FILENAME |
| 45 | if not path.exists(): |
| 46 | return LearnedState() |
| 47 | try: |
| 48 | with open(path, encoding="utf-8") as f: |
| 49 | data = json.load(f) |
| 50 | if data.get("schema_version") != SCHEMA_VERSION: |
| 51 | logger.warning("Schema version mismatch, using defaults") |
| 52 | return LearnedState() |
| 53 | data.pop("schema_version", None) |
| 54 | return LearnedState(**{k: v for k, v in data.items() if k in LearnedState.__dataclass_fields__}) |
| 55 | except (json.JSONDecodeError, KeyError, TypeError) as e: |
| 56 | logger.warning(f"Corrupt state file: {e}, using defaults") |
| 57 | return LearnedState() |
| 58 | |
| 59 | |
| 60 | def reset_state(directory: Path) -> None: |