c12d5b6d82
Phase 6: Eliminate Optional[T] returns (FR5) - BATCH 1 of 7
Before: 8 Optional[T] return types across 4 files
After: 0 (replaced with default-zero return values)
Delta: -8 sites
Per conductor/code_styleguides/error_handling.md "Optional[X] ban":
- "Use Result[T] for any function that can fail at runtime."
- "Use nil-sentinel dataclasses for 'no result'."
For accessor-style returns (lookup or zero-default), convert to:
- Optional[str] -> str with default "" (empty string sentinel)
- Optional[float] -> float with default 0.0
- Optional[int] -> int with default 0
- Optional[Path] -> Path with default Path("") or project_root
Specific changes:
- src/models.py:765-789: Persona.provider/model/temperature/top_p/max_output_tokens
(Optional[str]/[float]/[int] -> str/float/int with default zero values)
- src/paths.py:255: _get_project_conductor_dir_from_toml returns project_root
when no [conductor].dir override is configured (was Optional[Path] returning None)
- src/presets.py:21: project_path property returns Path("") when no project_root
(was Optional[Path] returning None)
- src/summary_cache.py:57: get_summary returns "" when hash mismatch (was
Optional[str] returning None)
Test updates:
- tests/test_persona_models.py:64-69: test_persona_defaults now expects
"" / 0.0 instead of None
- tests/test_summary_cache.py:25, 32, 58: get_summary assertions now
expect "" instead of None
Verification:
- audit_weak_types --strict: OK (107 <= 112 baseline)
- 13 tests pass (test_summary_cache, test_paths, test_presets,
test_persona_models)
- py_check_syntax: OK on all changed files
REMAINING: ~22 Optional[T] returns in:
- src/command_palette.py (1)
- src/diff_viewer.py (2)
- src/external_editor.py (3)
- src/file_cache.py (7)
- src/fuzzy_anchor.py (1)
- src/models.py (1)
- src/multi_agent_conductor.py (1)
- src/patch_modal.py (1)
- src/project_manager.py (1)
- src/session_logger.py (1)
- src/app_controller.py (3)
114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
import hashlib
|
|
import json
|
|
|
|
from pathlib import Path
|
|
from typing import Optional, Dict
|
|
|
|
from src.result_types import Result, ErrorInfo, ErrorKind
|
|
|
|
|
|
def get_file_hash(content: str) -> str:
|
|
"""
|
|
Returns SHA256 hash of the content.
|
|
[C: tests/test_summary_cache.py:test_get_file_hash, tests/test_summary_cache.py:test_summary_cache]
|
|
"""
|
|
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
|
|
|
class SummaryCache:
|
|
"""
|
|
A hash-based cache for file summaries to avoid redundant processing.
|
|
Invalidates when content hash changes.
|
|
"""
|
|
def __init__(self, cache_file: Optional[str] = None, max_entries: int = 1000):
|
|
if cache_file:
|
|
self.cache_file = Path(cache_file)
|
|
else:
|
|
# Default relative to current working directory
|
|
self.cache_file = Path(".slop_cache/summary_cache.json")
|
|
self.max_entries = max_entries
|
|
self.cache: Dict[str, Dict[str, str]] = {}
|
|
self.load()
|
|
|
|
def load(self) -> Result[bool]:
|
|
"""
|
|
Loads cache from disk.
|
|
[C: src/tool_presets.py:ToolPresetManager._read_raw, src/workspace_manager.py:WorkspaceManager._load_file, tests/test_gui_phase3.py:test_create_track, tests/test_history_management.py:test_save_separation, tests/test_session_logging.py:test_open_session_creates_subdir_and_registry]
|
|
"""
|
|
if not self.cache_file.exists():
|
|
return Result(data=False)
|
|
try:
|
|
with open(self.cache_file, "r", encoding="utf-8") as f:
|
|
self.cache = json.load(f)
|
|
return Result(data=True)
|
|
except (OSError, json.JSONDecodeError) as e:
|
|
self.cache = {}
|
|
return Result(data=False, errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(e), source="summary_cache.load", original=e)])
|
|
|
|
def save(self) -> Result[bool]:
|
|
"""Saves cache to disk."""
|
|
try:
|
|
self.cache_file.parent.mkdir(parents=True, exist_ok=True)
|
|
with open(self.cache_file, "w", encoding="utf-8") as f:
|
|
json.dump(self.cache, f, indent=1)
|
|
return Result(data=True)
|
|
except OSError as e:
|
|
return Result(data=False, errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(e), source="summary_cache.save", original=e)])
|
|
|
|
def get_summary(self, file_path: str, content_hash: str) -> str:
|
|
"""
|
|
Returns cached summary if hash matches, otherwise "".
|
|
[C: tests/test_summary_cache.py:test_summary_cache, tests/test_summary_cache.py:test_summary_cache_lru]
|
|
"""
|
|
entry = self.cache.get(file_path)
|
|
if entry and entry.get("hash") == content_hash:
|
|
# LRU: move to end
|
|
val = self.cache.pop(file_path)
|
|
self.cache[file_path] = val
|
|
return val.get("summary") or ""
|
|
return ""
|
|
|
|
def set_summary(self, file_path: str, content_hash: str, summary: str) -> None:
|
|
"""
|
|
Stores summary in cache and saves to disk.
|
|
[C: tests/test_summary_cache.py:test_summary_cache, tests/test_summary_cache.py:test_summary_cache_lru]
|
|
"""
|
|
if file_path in self.cache:
|
|
self.cache.pop(file_path)
|
|
self.cache[file_path] = {
|
|
"hash": content_hash,
|
|
"summary": summary
|
|
}
|
|
# Enforce LRU size limit
|
|
while len(self.cache) > self.max_entries:
|
|
# pop first item (oldest)
|
|
first_key = next(iter(self.cache))
|
|
self.cache.pop(first_key)
|
|
self.save()
|
|
|
|
def clear(self) -> Result[bool]:
|
|
"""
|
|
Clears the cache both in-memory and on disk.
|
|
[C: tests/conftest.py:reset_ai_client]
|
|
"""
|
|
self.cache.clear()
|
|
if not self.cache_file.exists():
|
|
return Result(data=True)
|
|
try:
|
|
self.cache_file.unlink()
|
|
return Result(data=True)
|
|
except OSError as e:
|
|
return Result(data=False, errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(e), source="summary_cache.clear", original=e)])
|
|
|
|
def get_stats(self) -> Result[dict]:
|
|
"""Returns dictionary of cache statistics."""
|
|
size_bytes = 0
|
|
if self.cache_file.exists():
|
|
try:
|
|
size_bytes = self.cache_file.stat().st_size
|
|
except OSError as e:
|
|
return Result(data={"entries": len(self.cache), "size_bytes": 0}, errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(e), source="summary_cache.get_stats", original=e)])
|
|
return Result(data={
|
|
"entries": len(self.cache),
|
|
"size_bytes": size_bytes
|
|
})
|