-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsmart_retrieval.py
More file actions
221 lines (185 loc) · 7.33 KB
/
smart_retrieval.py
File metadata and controls
221 lines (185 loc) · 7.33 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
"""Smart Retrieval (Layer 1) — BM25 + multi-signal re-ranking.
Always-on, query-time enrichment: FTS5 top-N → Python re-rank with 6 signals → top-K.
Falls back gracefully to pure BM25 on any error.
"""
from __future__ import annotations
import math
import sqlite3
from datetime import datetime, timezone
from typing import Any
# ── Tunable constants ──────────────────────────────────────────────────────
RECENCY_HALF_LIFE_DAYS = 14
PROJECT_BOOST = 1.5
GRAPH_BOOST_1HOP = 1.8
GRAPH_BOOST_2HOP = 1.3
RICHNESS_DIVISOR = 3.0
FACT_BOOST = 1.4
SESSION_BOOST = 2.0
RERANKING_POOL_SIZE = 100
# ── Scoring helpers ────────────────────────────────────────────────────────
def compute_recency_decay(updated_at: str | None, half_life_days: float = RECENCY_HALF_LIFE_DAYS) -> float:
"""Exponential decay: 2^(-days / half_life). Returns 1.0 if unparseable."""
if not updated_at:
return 0.5
try:
dt = datetime.fromisoformat(updated_at)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
days = (datetime.now(timezone.utc) - dt).total_seconds() / 86400
return max(math.pow(2, -days / half_life_days), 0.1)
except (ValueError, TypeError):
return 0.5
def compute_composite_score(
bm25_rank: float,
updated_at: str | None,
project: str | None,
current_project: str | None,
obs_count: int,
relation_hops: int,
has_canonical_facts: bool,
in_active_session: bool,
) -> float:
"""Multiplicative composite score from 6 signals.
bm25_rank is negative (lower = better match in FTS5), so we invert it.
"""
# Base: invert BM25 rank (FTS5 rank is negative, closer to 0 = worse match)
base = 1.0 / (1.0 + abs(bm25_rank))
# Signal 1: recency
recency = compute_recency_decay(updated_at)
# Signal 2: project affinity
proj = PROJECT_BOOST if (current_project and project == current_project) else 1.0
# Signal 3: graph proximity
if relation_hops == 1:
graph = GRAPH_BOOST_1HOP
elif relation_hops == 2:
graph = GRAPH_BOOST_2HOP
else:
graph = 1.0
# Signal 4: observation richness (log scale, capped)
richness = 1.0 + math.log1p(obs_count) / RICHNESS_DIVISOR
# Signal 5: canonical facts existence
facts = FACT_BOOST if has_canonical_facts else 1.0
# Signal 6: active session
session = SESSION_BOOST if in_active_session else 1.0
return base * recency * proj * graph * richness * facts * session
# ── Main re-ranking entry point ────────────────────────────────────────────
def rerank_entities(
conn: sqlite3.Connection,
fts_rows: list[Any],
current_project: str | None,
session_id: str | None,
query_entity_ids: list[int] | None,
limit: int = 50,
) -> list[dict]:
"""Re-rank FTS5 results using composite scoring.
Args:
conn: Active SQLite connection (within transaction).
fts_rows: Rows from FTS5 query with eid, name, entity_type, project, rank.
current_project: Current project for affinity boost.
session_id: Current session ID for active-file boost.
query_entity_ids: Entity IDs from the query itself (for graph proximity).
limit: Max results to return.
Returns:
List of dicts with entity info + _meta scoring details.
"""
if not fts_rows:
return []
eids = [r["eid"] for r in fts_rows]
ph = ",".join("?" * len(eids))
# Batch-fetch observation counts
obs_counts: dict[int, int] = {}
for row in conn.execute(
f"SELECT entity_id, COUNT(*) AS cnt FROM observations "
f"WHERE entity_id IN ({ph}) GROUP BY entity_id",
eids,
):
obs_counts[row["entity_id"]] = row["cnt"]
# Batch-fetch updated_at timestamps
updated_map: dict[int, str] = {}
for row in conn.execute(
f"SELECT id, updated_at FROM entities WHERE id IN ({ph})", eids
):
updated_map[row["id"]] = row["updated_at"]
# Batch-fetch 1-hop relations (entities connected to query entities)
one_hop: set[int] = set()
two_hop: set[int] = set()
if query_entity_ids:
q_ph = ",".join("?" * len(query_entity_ids))
for row in conn.execute(
f"SELECT DISTINCT to_id FROM relations WHERE from_id IN ({q_ph}) "
f"UNION SELECT DISTINCT from_id FROM relations WHERE to_id IN ({q_ph})",
query_entity_ids + query_entity_ids,
):
one_hop.add(row[0])
# Expand to 2-hop (only if manageable)
if one_hop and len(one_hop) < 500:
hop_ph = ",".join("?" * len(one_hop))
hop_list = list(one_hop)
for row in conn.execute(
f"SELECT DISTINCT to_id FROM relations WHERE from_id IN ({hop_ph}) "
f"UNION SELECT DISTINCT from_id FROM relations WHERE to_id IN ({hop_ph})",
hop_list + hop_list,
):
if row[0] not in one_hop:
two_hop.add(row[0])
# Batch-check canonical_facts subjects
facts_subjects: set[str] = set()
eid_names = {r["eid"]: r["name"] for r in fts_rows}
name_list = list(eid_names.values())
if name_list:
n_ph = ",".join("?" * len(name_list))
try:
for row in conn.execute(
f"SELECT DISTINCT subject FROM canonical_facts WHERE subject IN ({n_ph})",
name_list,
):
facts_subjects.add(row["subject"])
except Exception:
pass # canonical_facts may not exist yet
# Session active files
active_file_entities: set[str] = set()
if session_id:
try:
srow = conn.execute(
"SELECT active_files FROM sessions WHERE session_id = ?",
(session_id,),
).fetchone()
if srow and srow["active_files"]:
import json
files = json.loads(srow["active_files"])
if isinstance(files, list):
active_file_entities = set(files)
except Exception:
pass
# Compute scores and build results
scored: list[tuple[float, dict]] = []
for r in fts_rows:
eid = r["eid"]
name = r["name"]
# Determine graph proximity
if eid in one_hop:
hops = 1
elif eid in two_hop:
hops = 2
else:
hops = 0
score = compute_composite_score(
bm25_rank=r["rank"],
updated_at=updated_map.get(eid),
project=r["project"],
current_project=current_project,
obs_count=obs_counts.get(eid, 0),
relation_hops=hops,
has_canonical_facts=name in facts_subjects,
in_active_session=name in active_file_entities,
)
scored.append((score, {
"eid": eid,
"name": name,
"entity_type": r["entity_type"],
"project": r["project"],
"_score": round(score, 6),
}))
# Sort descending by score, truncate
scored.sort(key=lambda x: x[0], reverse=True)
return [item for _, item in scored[:limit]]