feat(aggregation): Implement hash-based summary cache

This commit is contained in:
2026-05-04 04:44:11 -04:00
parent 04c710d60c
commit 321810438b
3 changed files with 112 additions and 1 deletions
+55
View File
@@ -0,0 +1,55 @@
import hashlib
import json
from pathlib import Path
from typing import Optional, Dict
def get_file_hash(content: str) -> str:
"""Returns SHA256 hash of the content."""
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):
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.cache: Dict[str, Dict[str, str]] = {}
self.load()
def load(self) -> None:
"""Loads cache from disk."""
if self.cache_file.exists():
try:
with open(self.cache_file, "r", encoding="utf-8") as f:
self.cache = json.load(f)
except Exception:
self.cache = {}
def save(self) -> None:
"""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)
except Exception:
pass
def get_summary(self, file_path: str, content_hash: str) -> Optional[str]:
"""Returns cached summary if hash matches, otherwise None."""
entry = self.cache.get(file_path)
if entry and entry.get("hash") == content_hash:
return entry.get("summary")
return None
def set_summary(self, file_path: str, content_hash: str, summary: str) -> None:
"""Stores summary in cache and saves to disk."""
self.cache[file_path] = {
"hash": content_hash,
"summary": summary
}
self.save()