empathy-framework 3.7.1__py3-none-any.whl → 3.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/METADATA +130 -11
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/RECORD +21 -15
- empathy_os/cache/__init__.py +117 -0
- empathy_os/cache/base.py +166 -0
- empathy_os/cache/dependency_manager.py +253 -0
- empathy_os/cache/hash_only.py +248 -0
- empathy_os/cache/hybrid.py +390 -0
- empathy_os/cache/storage.py +282 -0
- empathy_os/config.py +2 -1
- empathy_os/memory/long_term.py +8 -7
- empathy_os/workflows/base.py +131 -1
- empathy_os/workflows/new_sample_workflow1_README.md +1 -1
- empathy_os/workflows/refactor_plan.py +4 -2
- empathy_os/workflows/security_audit.py +2 -6
- empathy_os/workflows/test5_README.md +1 -1
- hot_reload/__init__.py +3 -3
- workflow_patterns/structural.py +8 -8
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/WHEEL +0 -0
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/entry_points.txt +0 -0
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/licenses/LICENSE +0 -0
- {empathy_framework-3.7.1.dist-info → empathy_framework-3.8.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""Hybrid cache with hash + semantic similarity matching.
|
|
2
|
+
|
|
3
|
+
Combines fast hash-based exact matching with intelligent semantic similarity
|
|
4
|
+
for maximum cache hit rate (~70%).
|
|
5
|
+
|
|
6
|
+
Requires optional dependencies:
|
|
7
|
+
- sentence-transformers
|
|
8
|
+
- torch
|
|
9
|
+
- numpy
|
|
10
|
+
|
|
11
|
+
Copyright 2025 Smart-AI-Memory
|
|
12
|
+
Licensed under Fair Source License 0.9
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import hashlib
|
|
16
|
+
import logging
|
|
17
|
+
import time
|
|
18
|
+
from typing import TYPE_CHECKING, Any
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from .base import BaseCache, CacheEntry, CacheStats
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from sentence_transformers import SentenceTransformer
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:
|
|
31
|
+
"""Calculate cosine similarity between two vectors.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
a: First vector.
|
|
35
|
+
b: Second vector.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Similarity score (0.0 to 1.0).
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class HybridCache(BaseCache):
|
|
45
|
+
"""Hybrid hash + semantic similarity cache for maximum hit rate.
|
|
46
|
+
|
|
47
|
+
Provides two-tier caching:
|
|
48
|
+
1. Fast path: Hash-based exact matching (~1-5μs lookup)
|
|
49
|
+
2. Smart path: Semantic similarity matching (~50ms lookup)
|
|
50
|
+
|
|
51
|
+
Achieves ~70% cache hit rate vs ~30% for hash-only.
|
|
52
|
+
|
|
53
|
+
Example:
|
|
54
|
+
cache = HybridCache(similarity_threshold=0.95)
|
|
55
|
+
|
|
56
|
+
# First call (miss)
|
|
57
|
+
result = cache.get("code-review", "scan", "Add auth middleware", "sonnet")
|
|
58
|
+
# → None (cache miss)
|
|
59
|
+
|
|
60
|
+
cache.put("code-review", "scan", "Add auth middleware", "sonnet", response1)
|
|
61
|
+
|
|
62
|
+
# Exact match (hash hit, <5μs)
|
|
63
|
+
result = cache.get("code-review", "scan", "Add auth middleware", "sonnet")
|
|
64
|
+
# → response1 (hash cache hit)
|
|
65
|
+
|
|
66
|
+
# Similar prompt (semantic hit, ~50ms)
|
|
67
|
+
result = cache.get("code-review", "scan", "Add logging middleware", "sonnet")
|
|
68
|
+
# → response1 (92% similar, semantic cache hit)
|
|
69
|
+
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
max_size_mb: int = 500,
|
|
75
|
+
default_ttl: int = 86400,
|
|
76
|
+
max_memory_mb: int = 100,
|
|
77
|
+
similarity_threshold: float = 0.95,
|
|
78
|
+
model_name: str = "all-MiniLM-L6-v2",
|
|
79
|
+
device: str = "cpu",
|
|
80
|
+
):
|
|
81
|
+
"""Initialize hybrid cache.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
max_size_mb: Maximum disk cache size in MB.
|
|
85
|
+
default_ttl: Default TTL in seconds (24 hours).
|
|
86
|
+
max_memory_mb: Maximum in-memory cache size in MB.
|
|
87
|
+
similarity_threshold: Semantic similarity threshold (0.0-1.0, default: 0.95).
|
|
88
|
+
model_name: Sentence transformer model (default: all-MiniLM-L6-v2).
|
|
89
|
+
device: Device for embeddings ("cpu" or "cuda").
|
|
90
|
+
|
|
91
|
+
"""
|
|
92
|
+
super().__init__(max_size_mb, default_ttl)
|
|
93
|
+
self.max_memory_mb = max_memory_mb
|
|
94
|
+
self.similarity_threshold = similarity_threshold
|
|
95
|
+
self.model_name = model_name
|
|
96
|
+
self.device = device
|
|
97
|
+
|
|
98
|
+
# Hash cache (fast path)
|
|
99
|
+
self._hash_cache: dict[str, CacheEntry] = {}
|
|
100
|
+
self._access_times: dict[str, float] = {}
|
|
101
|
+
|
|
102
|
+
# Semantic cache (smart path)
|
|
103
|
+
self._semantic_cache: list[tuple[np.ndarray, CacheEntry]] = []
|
|
104
|
+
|
|
105
|
+
# Load sentence transformer model
|
|
106
|
+
self._model: SentenceTransformer | None = None
|
|
107
|
+
self._load_model()
|
|
108
|
+
|
|
109
|
+
logger.info(
|
|
110
|
+
f"HybridCache initialized (model: {model_name}, threshold: {similarity_threshold}, "
|
|
111
|
+
f"device: {device}, max_memory: {max_memory_mb}MB)"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def _load_model(self) -> None:
|
|
115
|
+
"""Load sentence transformer model for embeddings."""
|
|
116
|
+
try:
|
|
117
|
+
from sentence_transformers import SentenceTransformer
|
|
118
|
+
|
|
119
|
+
logger.debug(f"Loading sentence transformer model: {self.model_name}")
|
|
120
|
+
self._model = SentenceTransformer(self.model_name, device=self.device)
|
|
121
|
+
logger.info(f"Sentence transformer loaded successfully on {self.device}")
|
|
122
|
+
|
|
123
|
+
except ImportError as e:
|
|
124
|
+
logger.error(
|
|
125
|
+
f"Failed to load sentence-transformers: {e}. "
|
|
126
|
+
"Install with: pip install empathy-framework[cache]"
|
|
127
|
+
)
|
|
128
|
+
raise
|
|
129
|
+
except Exception as e:
|
|
130
|
+
logger.warning(f"Failed to load model {self.model_name}: {e}")
|
|
131
|
+
logger.warning("Falling back to hash-only mode")
|
|
132
|
+
self._model = None
|
|
133
|
+
|
|
134
|
+
def get(
|
|
135
|
+
self,
|
|
136
|
+
workflow: str,
|
|
137
|
+
stage: str,
|
|
138
|
+
prompt: str,
|
|
139
|
+
model: str,
|
|
140
|
+
) -> Any | None:
|
|
141
|
+
"""Get cached response using hybrid hash + semantic matching.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
workflow: Workflow name.
|
|
145
|
+
stage: Stage name.
|
|
146
|
+
prompt: Prompt text.
|
|
147
|
+
model: Model identifier.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
Cached response if found (hash or semantic match), None otherwise.
|
|
151
|
+
|
|
152
|
+
"""
|
|
153
|
+
cache_key = self._create_cache_key(workflow, stage, prompt, model)
|
|
154
|
+
current_time = time.time()
|
|
155
|
+
|
|
156
|
+
# Step 1: Try hash cache (fast path, <5μs)
|
|
157
|
+
if cache_key in self._hash_cache:
|
|
158
|
+
entry = self._hash_cache[cache_key]
|
|
159
|
+
|
|
160
|
+
if entry.is_expired(current_time):
|
|
161
|
+
self._evict_entry(cache_key)
|
|
162
|
+
self.stats.misses += 1
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
# Hash hit!
|
|
166
|
+
self._access_times[cache_key] = current_time
|
|
167
|
+
self.stats.hits += 1
|
|
168
|
+
logger.debug(
|
|
169
|
+
f"Cache HIT (hash): {workflow}/{stage} " f"(hit_rate: {self.stats.hit_rate:.1f}%)"
|
|
170
|
+
)
|
|
171
|
+
return entry.response
|
|
172
|
+
|
|
173
|
+
# Step 2: Try semantic cache (smart path, ~50ms)
|
|
174
|
+
if self._model is not None:
|
|
175
|
+
semantic_result = self._semantic_lookup(prompt, workflow, stage, model)
|
|
176
|
+
if semantic_result is not None:
|
|
177
|
+
# Semantic hit! Add to hash cache for future fast lookups
|
|
178
|
+
entry, similarity = semantic_result
|
|
179
|
+
self._hash_cache[cache_key] = entry
|
|
180
|
+
self._access_times[cache_key] = current_time
|
|
181
|
+
|
|
182
|
+
self.stats.hits += 1
|
|
183
|
+
logger.debug(
|
|
184
|
+
f"Cache HIT (semantic): {workflow}/{stage} "
|
|
185
|
+
f"(similarity: {similarity:.3f}, hit_rate: {self.stats.hit_rate:.1f}%)"
|
|
186
|
+
)
|
|
187
|
+
return entry.response
|
|
188
|
+
|
|
189
|
+
# Step 3: Cache miss
|
|
190
|
+
self.stats.misses += 1
|
|
191
|
+
logger.debug(
|
|
192
|
+
f"Cache MISS (hybrid): {workflow}/{stage} " f"(hit_rate: {self.stats.hit_rate:.1f}%)"
|
|
193
|
+
)
|
|
194
|
+
return None
|
|
195
|
+
|
|
196
|
+
def _semantic_lookup(
|
|
197
|
+
self,
|
|
198
|
+
prompt: str,
|
|
199
|
+
workflow: str,
|
|
200
|
+
stage: str,
|
|
201
|
+
model: str,
|
|
202
|
+
) -> tuple[CacheEntry, float] | None:
|
|
203
|
+
"""Perform semantic similarity lookup.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
prompt: Prompt text.
|
|
207
|
+
workflow: Workflow name.
|
|
208
|
+
stage: Stage name.
|
|
209
|
+
model: Model identifier.
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
Tuple of (CacheEntry, similarity_score) if match found, None otherwise.
|
|
213
|
+
|
|
214
|
+
"""
|
|
215
|
+
if not self._semantic_cache:
|
|
216
|
+
return None
|
|
217
|
+
|
|
218
|
+
if self._model is None:
|
|
219
|
+
raise RuntimeError("Sentence transformer model not loaded")
|
|
220
|
+
|
|
221
|
+
# Encode prompt
|
|
222
|
+
prompt_embedding = self._model.encode(prompt, convert_to_numpy=True)
|
|
223
|
+
|
|
224
|
+
# Find best match
|
|
225
|
+
best_similarity = 0.0
|
|
226
|
+
best_entry = None
|
|
227
|
+
current_time = time.time()
|
|
228
|
+
|
|
229
|
+
for cached_embedding, entry in self._semantic_cache:
|
|
230
|
+
# Only match same workflow, stage, and model
|
|
231
|
+
if entry.workflow != workflow or entry.stage != stage or entry.model != model:
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
# Skip expired
|
|
235
|
+
if entry.is_expired(current_time):
|
|
236
|
+
continue
|
|
237
|
+
|
|
238
|
+
# Calculate similarity
|
|
239
|
+
similarity = cosine_similarity(prompt_embedding, cached_embedding)
|
|
240
|
+
|
|
241
|
+
if similarity > best_similarity:
|
|
242
|
+
best_similarity = similarity
|
|
243
|
+
best_entry = entry
|
|
244
|
+
|
|
245
|
+
# Check if best match exceeds threshold
|
|
246
|
+
if best_similarity >= self.similarity_threshold and best_entry is not None:
|
|
247
|
+
return (best_entry, best_similarity)
|
|
248
|
+
|
|
249
|
+
return None
|
|
250
|
+
|
|
251
|
+
def put(
|
|
252
|
+
self,
|
|
253
|
+
workflow: str,
|
|
254
|
+
stage: str,
|
|
255
|
+
prompt: str,
|
|
256
|
+
model: str,
|
|
257
|
+
response: Any,
|
|
258
|
+
ttl: int | None = None,
|
|
259
|
+
) -> None:
|
|
260
|
+
"""Store response in both hash and semantic caches.
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
workflow: Workflow name.
|
|
264
|
+
stage: Stage name.
|
|
265
|
+
prompt: Prompt text.
|
|
266
|
+
model: Model identifier.
|
|
267
|
+
response: LLM response to cache.
|
|
268
|
+
ttl: Optional custom TTL.
|
|
269
|
+
|
|
270
|
+
"""
|
|
271
|
+
cache_key = self._create_cache_key(workflow, stage, prompt, model)
|
|
272
|
+
prompt_hash = hashlib.sha256(prompt.encode()).hexdigest()
|
|
273
|
+
|
|
274
|
+
# Create cache entry
|
|
275
|
+
entry = CacheEntry(
|
|
276
|
+
key=cache_key,
|
|
277
|
+
response=response,
|
|
278
|
+
workflow=workflow,
|
|
279
|
+
stage=stage,
|
|
280
|
+
model=model,
|
|
281
|
+
prompt_hash=prompt_hash,
|
|
282
|
+
timestamp=time.time(),
|
|
283
|
+
ttl=ttl or self.default_ttl,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
# Maybe evict before adding
|
|
287
|
+
self._maybe_evict_lru()
|
|
288
|
+
|
|
289
|
+
# Store in hash cache
|
|
290
|
+
self._hash_cache[cache_key] = entry
|
|
291
|
+
self._access_times[cache_key] = entry.timestamp
|
|
292
|
+
|
|
293
|
+
# Store in semantic cache (if model available)
|
|
294
|
+
if self._model is not None:
|
|
295
|
+
prompt_embedding = self._model.encode(prompt, convert_to_numpy=True)
|
|
296
|
+
self._semantic_cache.append((prompt_embedding, entry))
|
|
297
|
+
|
|
298
|
+
logger.debug(
|
|
299
|
+
f"Cache PUT (hybrid): {workflow}/{stage} "
|
|
300
|
+
f"(hash_entries: {len(self._hash_cache)}, "
|
|
301
|
+
f"semantic_entries: {len(self._semantic_cache)})"
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def clear(self) -> None:
|
|
305
|
+
"""Clear all cached entries."""
|
|
306
|
+
hash_count = len(self._hash_cache)
|
|
307
|
+
semantic_count = len(self._semantic_cache)
|
|
308
|
+
|
|
309
|
+
self._hash_cache.clear()
|
|
310
|
+
self._access_times.clear()
|
|
311
|
+
self._semantic_cache.clear()
|
|
312
|
+
|
|
313
|
+
logger.info(f"Cache cleared (hash: {hash_count}, semantic: {semantic_count} entries)")
|
|
314
|
+
|
|
315
|
+
def get_stats(self) -> CacheStats:
|
|
316
|
+
"""Get cache statistics."""
|
|
317
|
+
return self.stats
|
|
318
|
+
|
|
319
|
+
def _evict_entry(self, cache_key: str) -> None:
|
|
320
|
+
"""Remove entry from both caches.
|
|
321
|
+
|
|
322
|
+
Args:
|
|
323
|
+
cache_key: Key to evict.
|
|
324
|
+
|
|
325
|
+
"""
|
|
326
|
+
# Remove from hash cache
|
|
327
|
+
if cache_key in self._hash_cache:
|
|
328
|
+
entry = self._hash_cache[cache_key]
|
|
329
|
+
del self._hash_cache[cache_key]
|
|
330
|
+
|
|
331
|
+
# Remove from semantic cache
|
|
332
|
+
self._semantic_cache = [
|
|
333
|
+
(emb, e) for emb, e in self._semantic_cache if e.key != entry.key
|
|
334
|
+
]
|
|
335
|
+
|
|
336
|
+
if cache_key in self._access_times:
|
|
337
|
+
del self._access_times[cache_key]
|
|
338
|
+
|
|
339
|
+
self.stats.evictions += 1
|
|
340
|
+
|
|
341
|
+
def _maybe_evict_lru(self) -> None:
|
|
342
|
+
"""Evict least recently used entries if cache too large."""
|
|
343
|
+
# Estimate memory (rough)
|
|
344
|
+
estimated_mb = (len(self._hash_cache) * 0.01) + (len(self._semantic_cache) * 0.1)
|
|
345
|
+
|
|
346
|
+
if estimated_mb > self.max_memory_mb:
|
|
347
|
+
# Evict 10% of entries
|
|
348
|
+
num_to_evict = max(1, len(self._hash_cache) // 10)
|
|
349
|
+
|
|
350
|
+
# Sort by access time
|
|
351
|
+
sorted_keys = sorted(self._access_times.items(), key=lambda x: x[1])
|
|
352
|
+
|
|
353
|
+
for cache_key, _ in sorted_keys[:num_to_evict]:
|
|
354
|
+
self._evict_entry(cache_key)
|
|
355
|
+
|
|
356
|
+
logger.info(
|
|
357
|
+
f"LRU eviction: removed {num_to_evict} entries "
|
|
358
|
+
f"(hash: {len(self._hash_cache)}, semantic: {len(self._semantic_cache)})"
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
def evict_expired(self) -> int:
|
|
362
|
+
"""Remove all expired entries."""
|
|
363
|
+
current_time = time.time()
|
|
364
|
+
expired_keys = [
|
|
365
|
+
key for key, entry in self._hash_cache.items() if entry.is_expired(current_time)
|
|
366
|
+
]
|
|
367
|
+
|
|
368
|
+
for key in expired_keys:
|
|
369
|
+
self._evict_entry(key)
|
|
370
|
+
|
|
371
|
+
if expired_keys:
|
|
372
|
+
logger.info(f"Expired eviction: removed {len(expired_keys)} entries")
|
|
373
|
+
|
|
374
|
+
return len(expired_keys)
|
|
375
|
+
|
|
376
|
+
def size_info(self) -> dict[str, Any]:
|
|
377
|
+
"""Get cache size information."""
|
|
378
|
+
hash_mb = len(self._hash_cache) * 0.01
|
|
379
|
+
semantic_mb = len(self._semantic_cache) * 0.1
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
"hash_entries": len(self._hash_cache),
|
|
383
|
+
"semantic_entries": len(self._semantic_cache),
|
|
384
|
+
"hash_size_mb": round(hash_mb, 2),
|
|
385
|
+
"semantic_size_mb": round(semantic_mb, 2),
|
|
386
|
+
"total_size_mb": round(hash_mb + semantic_mb, 2),
|
|
387
|
+
"max_memory_mb": self.max_memory_mb,
|
|
388
|
+
"model": self.model_name,
|
|
389
|
+
"threshold": self.similarity_threshold,
|
|
390
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"""Persistent disk storage for cache with TTL support.
|
|
2
|
+
|
|
3
|
+
Provides hybrid in-memory + disk persistence for cache entries.
|
|
4
|
+
|
|
5
|
+
Copyright 2025 Smart-AI-Memory
|
|
6
|
+
Licensed under Fair Source License 0.9
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
11
|
+
import time
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from .base import CacheEntry
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class CacheStorage:
|
|
21
|
+
"""Hybrid in-memory + disk cache storage with TTL.
|
|
22
|
+
|
|
23
|
+
Provides:
|
|
24
|
+
- In-memory LRU cache for fast access
|
|
25
|
+
- Persistent disk storage for cache survival across restarts
|
|
26
|
+
- TTL-based expiration
|
|
27
|
+
- Automatic cleanup of expired entries
|
|
28
|
+
|
|
29
|
+
Example:
|
|
30
|
+
storage = CacheStorage()
|
|
31
|
+
storage.put(entry)
|
|
32
|
+
entry = storage.get(cache_key)
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
cache_dir: Path | None = None,
|
|
39
|
+
max_disk_mb: int = 500,
|
|
40
|
+
auto_save: bool = True,
|
|
41
|
+
):
|
|
42
|
+
"""Initialize cache storage.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
cache_dir: Directory for cache files (default: ~/.empathy/cache/).
|
|
46
|
+
max_disk_mb: Maximum disk cache size in MB.
|
|
47
|
+
auto_save: Automatically save to disk on put (default: True).
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
self.cache_dir = cache_dir or Path.home() / ".empathy" / "cache"
|
|
51
|
+
self.cache_file = self.cache_dir / "responses.json"
|
|
52
|
+
self.max_disk_mb = max_disk_mb
|
|
53
|
+
self.auto_save = auto_save
|
|
54
|
+
|
|
55
|
+
# Ensure cache directory exists
|
|
56
|
+
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
|
57
|
+
|
|
58
|
+
# Load cache from disk
|
|
59
|
+
self._entries: dict[str, CacheEntry] = {}
|
|
60
|
+
self.load()
|
|
61
|
+
|
|
62
|
+
logger.debug(
|
|
63
|
+
f"CacheStorage initialized (dir: {self.cache_dir}, "
|
|
64
|
+
f"max: {max_disk_mb}MB, entries: {len(self._entries)})"
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def load(self) -> int:
|
|
68
|
+
"""Load cache from disk into memory.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Number of entries loaded.
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
if not self.cache_file.exists():
|
|
75
|
+
logger.debug("No cache file found, starting fresh")
|
|
76
|
+
return 0
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
with open(self.cache_file) as f:
|
|
80
|
+
data = json.load(f)
|
|
81
|
+
|
|
82
|
+
version = data.get("version", "unknown")
|
|
83
|
+
entries_data = data.get("entries", [])
|
|
84
|
+
|
|
85
|
+
# Load entries
|
|
86
|
+
loaded = 0
|
|
87
|
+
current_time = time.time()
|
|
88
|
+
|
|
89
|
+
for entry_data in entries_data:
|
|
90
|
+
entry = CacheEntry(
|
|
91
|
+
key=entry_data["key"],
|
|
92
|
+
response=entry_data["response"],
|
|
93
|
+
workflow=entry_data["workflow"],
|
|
94
|
+
stage=entry_data["stage"],
|
|
95
|
+
model=entry_data["model"],
|
|
96
|
+
prompt_hash=entry_data["prompt_hash"],
|
|
97
|
+
timestamp=entry_data["timestamp"],
|
|
98
|
+
ttl=entry_data.get("ttl"),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# Skip expired entries
|
|
102
|
+
if entry.is_expired(current_time):
|
|
103
|
+
continue
|
|
104
|
+
|
|
105
|
+
self._entries[entry.key] = entry
|
|
106
|
+
loaded += 1
|
|
107
|
+
|
|
108
|
+
logger.info(
|
|
109
|
+
f"Loaded {loaded} cache entries from disk (version: {version}, "
|
|
110
|
+
f"skipped {len(entries_data) - loaded} expired)"
|
|
111
|
+
)
|
|
112
|
+
return loaded
|
|
113
|
+
|
|
114
|
+
except (json.JSONDecodeError, KeyError) as e:
|
|
115
|
+
logger.warning(f"Failed to load cache from disk: {e}")
|
|
116
|
+
return 0
|
|
117
|
+
|
|
118
|
+
def save(self) -> bool:
|
|
119
|
+
"""Save cache to disk.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
True if saved successfully, False otherwise.
|
|
123
|
+
|
|
124
|
+
"""
|
|
125
|
+
try:
|
|
126
|
+
# Prepare data
|
|
127
|
+
data = {
|
|
128
|
+
"version": "3.8.0",
|
|
129
|
+
"timestamp": time.time(),
|
|
130
|
+
"entries": [
|
|
131
|
+
{
|
|
132
|
+
"key": entry.key,
|
|
133
|
+
"response": entry.response,
|
|
134
|
+
"workflow": entry.workflow,
|
|
135
|
+
"stage": entry.stage,
|
|
136
|
+
"model": entry.model,
|
|
137
|
+
"prompt_hash": entry.prompt_hash,
|
|
138
|
+
"timestamp": entry.timestamp,
|
|
139
|
+
"ttl": entry.ttl,
|
|
140
|
+
}
|
|
141
|
+
for entry in self._entries.values()
|
|
142
|
+
],
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
# Write to disk
|
|
146
|
+
with open(self.cache_file, "w") as f:
|
|
147
|
+
json.dump(data, f, indent=2)
|
|
148
|
+
|
|
149
|
+
logger.debug(f"Saved {len(self._entries)} cache entries to disk")
|
|
150
|
+
return True
|
|
151
|
+
|
|
152
|
+
except (OSError, TypeError) as e:
|
|
153
|
+
logger.error(f"Failed to save cache to disk: {e}")
|
|
154
|
+
return False
|
|
155
|
+
|
|
156
|
+
def get(self, cache_key: str) -> CacheEntry | None:
|
|
157
|
+
"""Get entry from storage.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
cache_key: Cache key to lookup.
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
CacheEntry if found and not expired, None otherwise.
|
|
164
|
+
|
|
165
|
+
"""
|
|
166
|
+
if cache_key not in self._entries:
|
|
167
|
+
return None
|
|
168
|
+
|
|
169
|
+
entry = self._entries[cache_key]
|
|
170
|
+
|
|
171
|
+
# Check expiration
|
|
172
|
+
if entry.is_expired(time.time()):
|
|
173
|
+
del self._entries[cache_key]
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
return entry
|
|
177
|
+
|
|
178
|
+
def put(self, entry: CacheEntry) -> None:
|
|
179
|
+
"""Store entry in storage.
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
entry: CacheEntry to store.
|
|
183
|
+
|
|
184
|
+
"""
|
|
185
|
+
self._entries[entry.key] = entry
|
|
186
|
+
|
|
187
|
+
# Auto-save to disk if enabled
|
|
188
|
+
if self.auto_save:
|
|
189
|
+
self.save()
|
|
190
|
+
|
|
191
|
+
def delete(self, cache_key: str) -> bool:
|
|
192
|
+
"""Delete entry from storage.
|
|
193
|
+
|
|
194
|
+
Args:
|
|
195
|
+
cache_key: Key to delete.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
True if entry was deleted, False if not found.
|
|
199
|
+
|
|
200
|
+
"""
|
|
201
|
+
if cache_key in self._entries:
|
|
202
|
+
del self._entries[cache_key]
|
|
203
|
+
if self.auto_save:
|
|
204
|
+
self.save()
|
|
205
|
+
return True
|
|
206
|
+
return False
|
|
207
|
+
|
|
208
|
+
def clear(self) -> int:
|
|
209
|
+
"""Clear all entries.
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
Number of entries cleared.
|
|
213
|
+
|
|
214
|
+
"""
|
|
215
|
+
count = len(self._entries)
|
|
216
|
+
self._entries.clear()
|
|
217
|
+
|
|
218
|
+
if self.auto_save:
|
|
219
|
+
self.save()
|
|
220
|
+
|
|
221
|
+
return count
|
|
222
|
+
|
|
223
|
+
def evict_expired(self) -> int:
|
|
224
|
+
"""Remove all expired entries.
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
Number of entries evicted.
|
|
228
|
+
|
|
229
|
+
"""
|
|
230
|
+
current_time = time.time()
|
|
231
|
+
expired_keys = [
|
|
232
|
+
key for key, entry in self._entries.items() if entry.is_expired(current_time)
|
|
233
|
+
]
|
|
234
|
+
|
|
235
|
+
for key in expired_keys:
|
|
236
|
+
del self._entries[key]
|
|
237
|
+
|
|
238
|
+
if expired_keys and self.auto_save:
|
|
239
|
+
self.save()
|
|
240
|
+
|
|
241
|
+
return len(expired_keys)
|
|
242
|
+
|
|
243
|
+
def get_all(self) -> list[CacheEntry]:
|
|
244
|
+
"""Get all non-expired entries.
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
List of CacheEntry objects.
|
|
248
|
+
|
|
249
|
+
"""
|
|
250
|
+
current_time = time.time()
|
|
251
|
+
return [entry for entry in self._entries.values() if not entry.is_expired(current_time)]
|
|
252
|
+
|
|
253
|
+
def size_mb(self) -> float:
|
|
254
|
+
"""Estimate cache size in MB.
|
|
255
|
+
|
|
256
|
+
Returns:
|
|
257
|
+
Estimated size in megabytes.
|
|
258
|
+
|
|
259
|
+
"""
|
|
260
|
+
if not self.cache_file.exists():
|
|
261
|
+
return 0.0
|
|
262
|
+
|
|
263
|
+
return self.cache_file.stat().st_size / (1024 * 1024)
|
|
264
|
+
|
|
265
|
+
def stats(self) -> dict[str, Any]:
|
|
266
|
+
"""Get storage statistics.
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
Dictionary with storage metrics.
|
|
270
|
+
|
|
271
|
+
"""
|
|
272
|
+
current_time = time.time()
|
|
273
|
+
expired = sum(1 for entry in self._entries.values() if entry.is_expired(current_time))
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
"total_entries": len(self._entries),
|
|
277
|
+
"expired_entries": expired,
|
|
278
|
+
"active_entries": len(self._entries) - expired,
|
|
279
|
+
"disk_size_mb": round(self.size_mb(), 2),
|
|
280
|
+
"max_disk_mb": self.max_disk_mb,
|
|
281
|
+
"cache_dir": str(self.cache_dir),
|
|
282
|
+
}
|
empathy_os/config.py
CHANGED
|
@@ -475,7 +475,8 @@ def load_config(
|
|
|
475
475
|
try:
|
|
476
476
|
env_config = EmpathyConfig.from_env()
|
|
477
477
|
config = config.merge(env_config)
|
|
478
|
-
except
|
|
478
|
+
except (ValueError, TypeError):
|
|
479
|
+
# Graceful fallback: invalid env var type conversion
|
|
479
480
|
pass # Use current config if environment parsing fails
|
|
480
481
|
|
|
481
482
|
# Validate final configuration
|