-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.py
More file actions
430 lines (357 loc) · 15.5 KB
/
memory.py
File metadata and controls
430 lines (357 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
MemoryManager — Feather DB v0.6.0
===================================
Higher-order memory operations that compose existing Feather DB primitives.
All methods are stateless — pass the DB instance as the first argument.
Features
--------
why_retrieved Score breakdown explaining why a node was returned
health_report Orphan nodes, tier distribution, recall histogram
search_mmr Semantic search with Maximal Marginal Relevance diversity
consolidate Cluster similar nodes and merge into summary nodes
assign_tiers Classify nodes as hot / warm / cold by access patterns
"""
from __future__ import annotations
import math
import time
from typing import Callable, Optional
import numpy as np
# ──────────────────────────────────────────────────────────────────────────────
# Score formula constants (must mirror scoring.h)
# ──────────────────────────────────────────────────────────────────────────────
_DEFAULT_HALF_LIFE = 30.0 # days
_DEFAULT_TIME_WT = 0.3
class MemoryManager:
"""
Stateless helper for living-context memory operations.
Every public method takes `db` (a feather_db.DB instance) as its first
argument — no coupling to any particular DB path or embedder.
"""
# ── Score explanation ────────────────────────────────────────────────────
@staticmethod
def why_retrieved(
db,
node_id: int,
query_vec: np.ndarray,
half_life_days: float = _DEFAULT_HALF_LIFE,
time_weight: float = _DEFAULT_TIME_WT,
) -> dict:
"""
Explain why `node_id` would be returned for `query_vec`.
Returns a dict with keys:
similarity, stickiness, effective_age_days, recency,
importance, confidence, final_score, recall_count
"""
meta = db.get_metadata(node_id)
if meta is None:
return {"error": f"Node {node_id} not found"}
# Retrieve stored vector for similarity calculation
stored = db.get_vector(node_id, "text")
if len(stored) == 0:
similarity = 0.0
else:
stored_arr = np.array(stored, dtype=np.float32)
q = np.array(query_vec, dtype=np.float32)
dist = float(np.linalg.norm(q - stored_arr))
similarity = 1.0 / (1.0 + dist)
now = time.time()
age_days = (now - meta.timestamp) / 86400.0 if meta.timestamp > 0 else 0.0
stickiness = 1.0 + math.log(1.0 + meta.recall_count)
eff_age = age_days / stickiness
recency = 0.5 ** (eff_age / half_life_days)
final_score = ((1.0 - time_weight) * similarity + time_weight * recency) * meta.importance
return {
"node_id": node_id,
"similarity": round(similarity, 4),
"stickiness": round(stickiness, 3),
"effective_age_days":round(eff_age, 2),
"recency": round(recency, 4),
"importance": round(meta.importance, 3),
"confidence": round(meta.confidence, 3),
"final_score": round(final_score, 4),
"recall_count": meta.recall_count,
"formula": (
f"((1-{time_weight}) × {round(similarity,4)} "
f"+ {time_weight} × {round(recency,4)}) "
f"× {round(meta.importance,3)} = {round(final_score,4)}"
),
}
# ── Health report ────────────────────────────────────────────────────────
@staticmethod
def health_report(db, modality: str = "text") -> dict:
"""
Analyse the DB and return a health summary dict.
Keys:
total, hot_count, warm_count, cold_count,
orphan_count, expired_count,
recall_histogram, avg_importance, avg_confidence
"""
ids = db.get_all_ids(modality)
now = int(time.time())
hot_count = warm_count = cold_count = 0
orphan_count = expired_count = 0
total_importance = total_confidence = 0.0
recall_hist = {"0": 0, "1-5": 0, "6-20": 0, "21-100": 0, ">100": 0}
for nid in ids:
meta = db.get_metadata(nid)
if meta is None:
continue
rc = meta.recall_count
if rc == 0:
cold_count += 1
recall_hist["0"] += 1
elif rc <= 5:
warm_count += 1
recall_hist["1-5"] += 1
elif rc <= 20:
hot_count += 1
recall_hist["6-20"] += 1
elif rc <= 100:
hot_count += 1
recall_hist["21-100"] += 1
else:
hot_count += 1
recall_hist[">100"] += 1
out_edges = db.get_edges(nid)
in_edges = db.get_incoming(nid)
if len(out_edges) == 0 and len(in_edges) == 0:
orphan_count += 1
if meta.ttl > 0 and now > meta.timestamp + meta.ttl:
expired_count += 1
total_importance += meta.importance
total_confidence += meta.confidence
n = len(ids) or 1
return {
"total": len(ids),
"hot_count": hot_count,
"warm_count": warm_count,
"cold_count": cold_count,
"orphan_count": orphan_count,
"expired_count": expired_count,
"recall_histogram": recall_hist,
"avg_importance": round(total_importance / n, 3),
"avg_confidence": round(total_confidence / n, 3),
}
# ── MMR search ───────────────────────────────────────────────────────────
@staticmethod
def search_mmr(
db,
query_vec: np.ndarray,
k: int = 10,
diversity: float = 0.5,
fetch_k: Optional[int] = None,
modality: str = "text",
filter=None,
scoring=None,
) -> list:
"""
Semantic search with Maximal Marginal Relevance post-processing.
diversity=0.0 → pure similarity ranking (same as db.search)
diversity=1.0 → maximum diversity (ignores query similarity)
diversity=0.5 → balanced default
Returns list of feather_db.SearchResult (same type as db.search()).
"""
if fetch_k is None:
fetch_k = min(k * 5, 200)
# Over-fetch candidates
candidates = db.search(query_vec, k=fetch_k,
filter=filter, scoring=scoring, modality=modality)
if not candidates:
return []
# Retrieve vectors for all candidates
vecs: dict[int, np.ndarray] = {}
for r in candidates:
v = db.get_vector(r.id, modality)
if len(v) > 0:
vecs[r.id] = np.array(v, dtype=np.float32)
q = np.array(query_vec, dtype=np.float32)
selected: list = []
selected_vecs: list[np.ndarray] = []
remaining = list(candidates)
while remaining and len(selected) < k:
best_idx = -1
best_score = -1e9
for i, r in enumerate(remaining):
sim_q = r.score # already computed by db.search
# Max similarity to already-selected nodes
if selected_vecs and r.id in vecs:
rv = vecs[r.id]
sim_sel = max(
float(np.dot(rv, sv) / (np.linalg.norm(rv) * np.linalg.norm(sv) + 1e-9))
for sv in selected_vecs
)
else:
sim_sel = 0.0
mmr = (1.0 - diversity) * sim_q - diversity * sim_sel
if mmr > best_score:
best_score = mmr
best_idx = i
if best_idx == -1:
break
chosen = remaining.pop(best_idx)
selected.append(chosen)
if chosen.id in vecs:
selected_vecs.append(vecs[chosen.id])
return selected
# ── Tier assignment ──────────────────────────────────────────────────────
@staticmethod
def assign_tiers(
db,
modality: str = "text",
hot_recall_pct: float = 0.10,
warm_recall_pct: float = 0.30,
write_back: bool = True,
) -> dict[int, str]:
"""
Classify all nodes as 'hot', 'warm', or 'cold' and (optionally)
write the tier back as an attribute on each node.
Returns {node_id: tier_str}.
"""
ids = db.get_all_ids(modality)
if not ids:
return {}
# Score per node: 40% recall_count rank + 60% recency rank
now = time.time()
scores: list[tuple[int, float]] = []
for nid in ids:
meta = db.get_metadata(nid)
if meta is None:
continue
recency_score = 1.0 / (1.0 + (now - meta.last_recalled_at) / 86400.0) \
if meta.last_recalled_at > 0 else 0.0
scores.append((nid, 0.4 * meta.recall_count + 0.6 * recency_score * 100))
scores.sort(key=lambda x: x[1], reverse=True)
n = len(scores)
hot_n = max(1, int(n * hot_recall_pct))
warm_n = max(1, int(n * warm_recall_pct))
tiers: dict[int, str] = {}
for rank, (nid, _) in enumerate(scores):
if rank < hot_n:
tier = "hot"
elif rank < hot_n + warm_n:
tier = "warm"
else:
tier = "cold"
tiers[nid] = tier
if write_back:
for nid, tier in tiers.items():
meta = db.get_metadata(nid)
if meta is None:
continue
meta.set_attribute("tier", tier)
db.update_metadata(nid, meta)
return tiers
# ── Memory consolidation ─────────────────────────────────────────────────
@staticmethod
def consolidate(
db,
namespace: str,
since_hours: float = 24.0,
similarity_threshold: float = 0.85,
min_cluster_size: int = 2,
llm_fn: Optional[Callable[[str], str]] = None,
modality: str = "text",
) -> list[int]:
"""
Cluster similar nodes in `namespace` added within `since_hours` and
merge each cluster into a single summary node.
Steps:
1. Fetch matching nodes and their vectors
2. Greedy union-find clustering by cosine similarity
3. For each cluster >= min_cluster_size:
- Generate summary (llm_fn or concat first 200 chars)
- Create a new FACT node in the same namespace
- Link each original to new node with 'consolidated_into'
- Lower original nodes' importance to 0.3
Returns list of new consolidated node IDs.
"""
try:
import feather_db as _fdb
except ImportError:
raise ImportError("feather_db must be installed")
cutoff = int(time.time()) - int(since_hours * 3600)
# Gather candidate nodes
all_ids = db.get_all_ids(modality)
candidates: list[tuple[int, np.ndarray, object]] = [] # (id, vec, meta)
for nid in all_ids:
meta = db.get_metadata(nid)
if meta is None:
continue
if meta.namespace_id != namespace:
continue
if meta.timestamp < cutoff:
continue
if meta.importance <= 0.01: # already forgotten/consolidated
continue
vec = db.get_vector(nid, modality)
if len(vec) == 0:
continue
candidates.append((nid, np.array(vec, dtype=np.float32), meta))
if len(candidates) < min_cluster_size:
return []
# Greedy clustering
n = len(candidates)
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a, b):
pa, pb = find(a), find(b)
if pa != pb:
parent[pa] = pb
for i in range(n):
for j in range(i + 1, n):
vi, vj = candidates[i][1], candidates[j][1]
cos = float(np.dot(vi, vj) / (np.linalg.norm(vi) * np.linalg.norm(vj) + 1e-9))
if cos >= similarity_threshold:
union(i, j)
# Group by cluster root
clusters: dict[int, list[int]] = {}
for idx in range(n):
root = find(idx)
clusters.setdefault(root, []).append(idx)
new_ids: list[int] = []
for root, member_indices in clusters.items():
if len(member_indices) < min_cluster_size:
continue
members = [candidates[i] for i in member_indices]
# Generate summary
texts = [m[2].content for m in members]
if llm_fn is not None:
summary = llm_fn("\n---\n".join(texts))
else:
summary = " | ".join(t[:200] for t in texts)
# Compute centroid vector
centroid = np.mean([m[1] for m in members], axis=0).astype(np.float32)
norm = np.linalg.norm(centroid)
if norm > 0:
centroid /= norm
# Create new consolidated node
import hashlib
id_src = f"consolidation:{namespace}:{root}:{int(time.time())}"
new_id = int.from_bytes(
hashlib.sha256(id_src.encode()).digest()[:7], "little"
) % (2 ** 50)
new_meta = _fdb.Metadata()
new_meta.timestamp = int(time.time())
new_meta.importance = max(m[2].importance for m in members)
new_meta.confidence = sum(m[2].confidence for m in members) / len(members)
new_meta.type = _fdb.ContextType.FACT
new_meta.source = "consolidation"
new_meta.content = summary
new_meta.namespace_id = namespace
new_meta.entity_id = members[0][2].entity_id
new_meta.set_attribute("entity_type", "consolidated_memory")
new_meta.set_attribute("member_count", str(len(members)))
new_meta.set_attribute("consolidation_ts", str(int(time.time())))
db.add(id=new_id, vec=centroid, meta=new_meta, modality=modality)
# Link originals → new node + reduce importance
for nid, _, orig_meta in members:
db.link(from_id=nid, to_id=new_id,
rel_type="consolidated_into", weight=1.0)
orig_meta.importance = 0.3
db.update_metadata(nid, orig_meta)
new_ids.append(new_id)
return new_ids