-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext_packer.py
More file actions
1238 lines (1139 loc) · 42.3 KB
/
context_packer.py
File metadata and controls
1238 lines (1139 loc) · 42.3 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Context Pack Compiler — Role-specific context compilation with token budgets.
Phase 3 of Intelligence v2:
- Pack types: planner, reviewer, executor, bridge_checker, handoff
- Greedy coverage algorithm: maximize relevance+novelty, minimize redundancy
- Token budget optimization
- Session continuity (resume_context) with handoff packs
"""
from __future__ import annotations
import logging
import math
import re
import sqlite3
from collections import defaultdict
from datetime import datetime, timezone
from typing import Any
from advanced_context import (
_keyword_score,
build_strategy as _build_advanced_strategy,
compute_strategy_match as _compute_advanced_task_match,
select_context_items as _select_advanced_items,
)
from db_utils import (
_sqlite_has_column,
_sqlite_table_exists,
add_provenance_link,
effective_fact_confidence,
now_iso,
upsert_memory_artifact,
)
from intelligence_v2 import (
_new_id,
load_config,
log_enrichment_run,
)
logger = logging.getLogger("sqlite-kb")
# Minimum relevance score — fragments below this threshold are excluded
_MIN_RELEVANCE_THRESHOLD = 0.18
_TASK_TOKEN_RE = re.compile(r"[0-9A-Za-zА-Яа-я_./:-]{4,}")
_RECENCY_HALF_LIFE_DAYS = 21
_EXECUTOR_MIN_CHUNK_RELEVANCE = 0.45
_EXECUTOR_MIN_CHUNK_TRUST = 0.30
_EXECUTOR_PREVIEW_MIN_RELEVANCE = 0.50
_EXECUTOR_PREVIEW_MIN_QUALITY = 0.50
_RETRIEVAL_CONTRACT_VERSION = "memory_contract_v2"
_MAX_ITEMS_BY_TYPE = {
"fact": 6,
"claim": 5,
"question": 4,
"chunk": 6,
}
_MAX_CHUNKS_PER_GROUP = 3
_CHUNK_TRUST_BY_STATE = {
"enrichable": 0.35,
"uncertain": 0.22,
"awaiting_human": 0.16,
}
_RETRIEVAL_POLICIES: dict[str, dict[str, Any]] = {
"planner": {
"min_relevance": {
"fact": 0.18,
"claim": 0.18,
"question": 0.18,
"chunk": 0.20,
},
"min_trust": {"chunk": 0.16},
"suppress_chunks_if_core_signal": False,
"preview_requires_core_signal": False,
"preview_min_relevance": 0.0,
"preview_min_quality": 0.0,
},
"reviewer": {
"min_relevance": {
"fact": 0.18,
"claim": 0.18,
"question": 0.18,
"chunk": 0.22,
},
"min_trust": {"chunk": 0.18},
"suppress_chunks_if_core_signal": False,
"preview_requires_core_signal": False,
"preview_min_relevance": 0.0,
"preview_min_quality": 0.0,
},
"executor": {
"min_relevance": {
"fact": 0.18,
"claim": 0.22,
"question": 0.22,
"chunk": _EXECUTOR_MIN_CHUNK_RELEVANCE,
},
"min_trust": {"chunk": _EXECUTOR_MIN_CHUNK_TRUST},
"suppress_chunks_if_core_signal": True,
"preview_requires_core_signal": True,
"preview_min_relevance": _EXECUTOR_PREVIEW_MIN_RELEVANCE,
"preview_min_quality": _EXECUTOR_PREVIEW_MIN_QUALITY,
},
"bridge_checker": {
"min_relevance": {
"fact": 0.18,
"claim": 0.18,
"question": 0.18,
"chunk": 0.22,
},
"min_trust": {"chunk": 0.18},
"suppress_chunks_if_core_signal": False,
"preview_requires_core_signal": False,
"preview_min_relevance": 0.0,
"preview_min_quality": 0.0,
},
"handoff": {
"min_relevance": {
"fact": 0.18,
"claim": 0.18,
"question": 0.18,
"chunk": 0.20,
},
"min_trust": {"chunk": 0.16},
"suppress_chunks_if_core_signal": False,
"preview_requires_core_signal": False,
"preview_min_relevance": 0.0,
"preview_min_quality": 0.0,
},
}
# ── Task-Relevant Helpers ────────────────────────────────────────────────
def _format_ts(iso_str: str | None) -> str:
"""Format ISO timestamp as [YYYY-MM-DD HH:MM] prefix, or empty string."""
if not iso_str:
return ""
try:
return f"[{iso_str[:16].replace('T', ' ')}] "
except (ValueError, TypeError):
return ""
def _extract_task_keywords(text: str) -> list[str]:
"""Extract robust task keywords, preserving tool-like snake_case identifiers."""
if not text:
return []
keywords: list[str] = []
seen: set[str] = set()
for raw in _TASK_TOKEN_RE.findall(text.lower()):
token = raw.strip("._:/-")
if len(token) >= 4 and token not in seen:
keywords.append(token)
seen.add(token)
for part in re.split(r"[_./:-]+", token):
part = part.strip()
if len(part) >= 4 and part not in seen:
keywords.append(part)
seen.add(part)
return keywords[:40]
def _entity_name_overlap_score(text: str, name_scores: dict[str, float]) -> float:
"""Return strongest relevant-entity name match found inside text."""
if not text or not name_scores:
return 0.0
haystack = text.lower()
best = 0.0
for name, score in name_scores.items():
if name and name.lower() in haystack:
best = max(best, score)
return best
def _compute_recency_score(
iso_str: str | None,
half_life_days: float = _RECENCY_HALF_LIFE_DAYS,
) -> float:
"""Exponential recency score: 1.0 for fresh content, decays with age."""
if not iso_str:
return 0.5
try:
dt = datetime.fromisoformat(iso_str)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
age_days = max(0.0, (datetime.now(timezone.utc) - dt).total_seconds() / 86400.0)
return math.pow(2.0, -age_days / half_life_days)
except (ValueError, TypeError):
return 0.5
def _signal_floor(value: float | int | None, floor: float = 0.2) -> float:
"""Normalize noisy 0..1-ish signals so zero-ish rows can still participate."""
if value is None:
return floor
try:
return max(floor, min(1.0, float(value)))
except (ValueError, TypeError):
return floor
def _selection_score(
base_signal: float,
relevance: float,
trust: float,
freshness: float,
role_weight: float,
) -> float:
"""Composite score used for greedy selection."""
return (
_signal_floor(base_signal)
* role_weight
* max(relevance, 0.01)
* (0.45 + (trust * 0.55))
* (0.75 + (freshness * 0.25))
)
def _chunk_group_key(chunk: sqlite3.Row) -> str:
"""Group related chunk fragments so one source cannot dominate the pack."""
for key in ("entity_id", "source_ref", "title"):
value = chunk[key]
if value:
return str(value).lower()
return chunk["chunk_id"]
def _make_coverage_keys(
*texts: str | None,
extras: list[str] | None = None,
limit: int = 6,
) -> list[str]:
"""Build compact coverage keys for the optional advanced selector."""
keys: list[str] = []
seen: set[str] = set()
for extra in extras or []:
norm = str(extra).strip().lower()
if norm and norm not in seen:
keys.append(norm)
seen.add(norm)
if len(keys) >= limit:
return keys[:limit]
for text in texts:
for keyword in _extract_task_keywords(text or ""):
marker = f"kw:{keyword}"
if marker not in seen:
keys.append(marker)
seen.add(marker)
if len(keys) >= limit:
return keys[:limit]
return keys[:limit]
def _greedy_select_items(
items: list[dict[str, Any]],
budget: int,
) -> tuple[list[dict[str, Any]], int, dict[str, Any]]:
"""Baseline deterministic selector used when advanced mode is disabled."""
items.sort(
key=lambda x: (
-float(x["score"]),
str(x["type"]),
str(x.get("id", "")),
str(x["text"]),
)
)
selected: list[dict[str, Any]] = []
tokens_used = 0
type_counts: dict[str, int] = defaultdict(int)
chunk_group_counts: dict[str, int] = defaultdict(int)
seen_texts: set[str] = set()
for item in items:
if tokens_used + item["tokens"] > budget:
continue
if type_counts[item["type"]] >= _MAX_ITEMS_BY_TYPE[item["type"]]:
continue
if item["type"] == "chunk":
group_key = item.get("group_key")
if group_key and chunk_group_counts[group_key] >= _MAX_CHUNKS_PER_GROUP:
continue
dedupe_key = " ".join(item["text"].lower().split())
if dedupe_key in seen_texts:
continue
selected.append(item)
tokens_used += item["tokens"]
type_counts[item["type"]] += 1
if item["type"] == "chunk" and item.get("group_key"):
chunk_group_counts[item["group_key"]] += 1
seen_texts.add(dedupe_key)
return selected, tokens_used, {"selection_strategy": "greedy"}
def _prune_context_pack_history(
conn: sqlite3.Connection,
pack_type: str,
target_ref: str | None,
keep: int,
) -> None:
"""Keep only the most recent packs for a given pack_type/target_ref tuple."""
if target_ref is None:
rows = conn.execute(
"SELECT pack_id FROM context_packs "
"WHERE pack_type = ? AND target_ref IS NULL "
"ORDER BY created_at DESC",
(pack_type,),
).fetchall()
else:
rows = conn.execute(
"SELECT pack_id FROM context_packs "
"WHERE pack_type = ? AND target_ref = ? "
"ORDER BY created_at DESC",
(pack_type, target_ref),
).fetchall()
stale_ids = [r["pack_id"] for r in rows[keep:]]
if not stale_ids:
return
ph = ",".join("?" * len(stale_ids))
conn.execute(f"DELETE FROM context_packs WHERE pack_id IN ({ph})", stale_ids)
def _build_task_query(conn: sqlite3.Connection, task_id: str) -> tuple[str, list[int]]:
"""Extract search query text and linked entity IDs from a task."""
task = conn.execute(
"SELECT title, project, description, notes FROM tasks WHERE id = ?",
(task_id,),
).fetchone()
if not task:
return "", []
parts = [task["title"] or ""]
if task["project"]:
parts.append(task["project"])
if task["description"]:
parts.append(task["description"][:500])
if task["notes"]:
parts.append(task["notes"][:300])
query = " ".join(parts)
linked = conn.execute(
"SELECT entity_id FROM task_entity_links WHERE task_id = ?", (task_id,)
).fetchall()
return query, [r["entity_id"] for r in linked]
def _find_relevant_entities(
conn: sqlite3.Connection,
query: str,
linked_ids: list[int],
session_id: str | None,
limit: int = 50,
) -> dict[str, dict[Any, float]]:
"""Return entity relevance maps keyed by both entity name and entity id."""
# Tokenize, build FTS5 OR query
words = [w for w in query.strip().split() if len(w) > 2]
if not words:
# Fallback: linked entities only
name_scores: dict[str, float] = {}
id_scores: dict[int, float] = {}
for eid in linked_ids:
row = conn.execute(
"SELECT id, name FROM entities WHERE id = ?", (eid,)
).fetchone()
if row:
name_scores[row["name"]] = 1.0
id_scores[row["id"]] = 1.0
return {"by_name": name_scores, "by_id": id_scores}
escaped = ['"' + w.replace('"', '""') + '"' for w in words[:20]]
fts_q = " OR ".join(escaped)
try:
fts_rows = conn.execute(
"SELECT memory_fts.rowid AS eid, memory_fts.name, "
"memory_fts.entity_type, e.project, memory_fts.rank "
"FROM memory_fts JOIN entities e ON e.id = memory_fts.rowid "
"WHERE memory_fts MATCH ? ORDER BY memory_fts.rank LIMIT ?",
(fts_q, 100),
).fetchall()
except sqlite3.OperationalError:
fts_rows = []
# Optional vector search + RRF merge
try:
from vec_search import VEC_AVAILABLE, rrf_merge, vector_search
if VEC_AVAILABLE:
vec_rows = vector_search(conn, query, 100)
if fts_rows and vec_rows:
fts_rows = rrf_merge(fts_rows, vec_rows)
elif vec_rows:
fts_rows = vec_rows
except ImportError:
pass
if not fts_rows and not linked_ids:
return {}
# 6-signal reranking
reranked: list[dict] = []
if fts_rows:
try:
from smart_retrieval import rerank_entities
reranked = rerank_entities(
conn, fts_rows, None, session_id, linked_ids, limit
)
except (ImportError, sqlite3.OperationalError) as exc:
logger.warning("rerank_entities failed, using raw FTS: %s", exc)
reranked = [
{"eid": r["eid"], "name": r["name"], "_score": 0.1}
for r in fts_rows[:limit]
]
name_scores: dict[str, float] = {}
id_scores: dict[int, float] = {}
for item in reranked:
score = item.get("_score", 0.1)
name_scores[item["name"]] = score
if item.get("eid") is not None:
id_scores[int(item["eid"])] = score
# Add linked entities not already in search results
for eid in linked_ids:
row = conn.execute(
"SELECT id, name FROM entities WHERE id = ?", (eid,)
).fetchone()
if row:
name_scores[row["name"]] = max(name_scores.get(row["name"], 0.0), 0.75)
id_scores[row["id"]] = max(id_scores.get(row["id"], 0.0), 0.75)
return {"by_name": name_scores, "by_id": id_scores}
# ── Pack Type Definitions ────────────────────────────────────────────────
PACK_TYPES = ("planner", "reviewer", "executor", "bridge_checker", "handoff")
# What each pack type prioritizes
_PACK_PRIORITIES = {
"planner": {
"fact_weight": 1.0, # canonical facts are critical for planning
"claim_weight": 0.7, # provisional claims are useful context
"question_weight": 0.9, # open questions shape the plan
"chunk_weight": 0.3, # raw chunks less useful
},
"reviewer": {
"fact_weight": 1.0,
"claim_weight": 0.8, # reviewer needs to validate claims
"question_weight": 0.5,
"chunk_weight": 0.4,
},
"executor": {
"fact_weight": 1.0,
"claim_weight": 0.5, # executor uses confirmed facts
"question_weight": 0.3,
"chunk_weight": 0.1, # executor should prefer direct evidence over raw fragments
},
"bridge_checker": {
"fact_weight": 0.8,
"claim_weight": 0.9, # bridge needs to check claims
"question_weight": 0.7,
"chunk_weight": 0.5,
},
"handoff": {
"fact_weight": 1.0,
"claim_weight": 0.8,
"question_weight": 1.0, # handoff must include open questions
"chunk_weight": 0.2,
},
}
# Approximate tokens per character (conservative estimate)
_CHARS_PER_TOKEN = 4
def _estimate_tokens(text: str) -> int:
"""Estimate token count from text length."""
return max(1, len(text) // _CHARS_PER_TOKEN)
# ── Core: Build Context Pack ─────────────────────────────────────────────
def build_context_pack(
conn: sqlite3.Connection,
pack_type: str = "executor",
target_ref: str | None = None,
session_id: str | None = None,
token_budget: int | None = None,
persist: bool = True,
) -> dict[str, Any]:
"""Compile role-specific context pack with validated facts, provisional warnings.
Uses greedy coverage algorithm:
1. Score all available items by (relevance × novelty)
2. Greedily add highest-scoring items until token budget exhausted
3. Mark provisional items with warnings
Returns dict with pack metadata, body, and relevance/trust/freshness scores.
"""
config = load_config()
started = now_iso()
if not config["enabled"]:
return {"status": "disabled"}
if pack_type not in PACK_TYPES:
return {"error": f"Invalid pack type: {pack_type}. Use one of: {PACK_TYPES}"}
budget = token_budget or config.get("context_pack_token_budget_default", 4000)
priorities = _PACK_PRIORITIES[pack_type]
policy = _RETRIEVAL_POLICIES[pack_type]
# Task-relevant filtering: when target_ref is a task ID, scope to relevant entities
relevant_names: dict[str, float] = {}
relevant_entity_ids: dict[int, float] = {}
task_keywords: list[str] = []
is_task_scoped = bool(target_ref)
advanced_strategy: dict[str, Any] = {
"enabled": False,
"query_expansion_used": False,
"submodular_enabled": False,
"metadata": {
"seed_entities": 0,
"expanded_entities": 0,
"expansion_keywords": 0,
},
}
if target_ref:
try:
task_query, linked_ids = _build_task_query(conn, target_ref)
task_keywords = _extract_task_keywords(task_query)
if task_query or linked_ids:
relevant_entities = _find_relevant_entities(
conn, task_query, linked_ids, session_id
)
relevant_names = relevant_entities["by_name"]
relevant_entity_ids = relevant_entities["by_id"]
if config.get("advanced_context_enabled"):
advanced_strategy = _build_advanced_strategy(
conn,
target_ref=target_ref,
task_query=task_query,
linked_ids=linked_ids,
task_keywords=task_keywords,
relevant_names=relevant_names,
relevant_entity_ids=relevant_entity_ids,
config=config,
)
except (sqlite3.OperationalError, KeyError) as exc:
logger.warning(
"Task-scoped filtering failed, falling back to no-context: %s", exc
)
# Gather available items
items: list[dict[str, Any]] = []
def _task_match(
*texts: str | None,
entity_id: int | str | None = None,
entity_name: str | None = None,
) -> float:
if not is_task_scoped:
return 1.0
best = 0.0
if entity_id is not None:
try:
best = max(best, relevant_entity_ids.get(int(entity_id), 0.0))
except (ValueError, TypeError):
pass
if entity_name:
best = max(best, relevant_names.get(entity_name, 0.0))
joined = " ".join(t for t in texts if t)
if joined:
best = max(best, _keyword_score(joined, task_keywords))
best = max(best, _entity_name_overlap_score(joined, relevant_names))
best = max(
best,
_compute_advanced_task_match(
advanced_strategy,
*texts,
entity_id=entity_id,
entity_name=entity_name,
),
)
return best
# 1. Canonical facts (highest trust)
fact_cols = [
"fact_id",
"subject",
"predicate",
"object_text",
"fact_scope",
"confidence",
"source_claim_id",
"created_at",
"updated_at",
]
if _sqlite_has_column(conn, "canonical_facts", "valid_from"):
fact_cols.append("valid_from")
if _sqlite_has_column(conn, "canonical_facts", "valid_to"):
fact_cols.append("valid_to")
if _sqlite_has_column(conn, "canonical_facts", "contradiction_count"):
fact_cols.append("contradiction_count")
facts = conn.execute(
f"SELECT {', '.join(fact_cols)} FROM canonical_facts "
"ORDER BY confidence DESC, updated_at DESC, fact_id"
).fetchall()
for fact_row in facts:
f = dict(fact_row)
if f.get("valid_to"):
continue
contradiction_count = int(f.get("contradiction_count") or 0)
if contradiction_count > 0:
# Retrieval contract: unresolved contradictions are not canonical.
continue
has_fact_provenance = False
if _sqlite_table_exists(conn, "provenance_links"):
has_fact_provenance = (
conn.execute(
"SELECT 1 FROM provenance_links "
"WHERE subject_kind = 'fact' AND subject_ref = ? LIMIT 1",
(f["fact_id"],),
).fetchone()
is not None
)
if not f.get("source_claim_id") and not has_fact_provenance:
continue
rel = _task_match(
f["subject"],
f["predicate"],
f["object_text"],
entity_name=f["subject"],
)
if is_task_scoped and rel < policy["min_relevance"]["fact"]:
continue
trust = max(
0.55,
effective_fact_confidence(
f["confidence"],
updated_at=f.get("updated_at") or f.get("created_at"),
contradiction_count=contradiction_count,
),
)
freshness = _compute_recency_score(f["created_at"])
ts = _format_ts(f["created_at"])
validity_suffix = ""
if f.get("valid_from"):
validity_suffix = f", valid_from: {f['valid_from'][:10]}"
text = (
f"{ts}[FACT] {f['subject']} {f['predicate']} {f['object_text']} "
f"(scope: {f['fact_scope']}{validity_suffix})"
)
items.append(
{
"type": "fact",
"id": f["fact_id"],
"text": text,
"tokens": _estimate_tokens(text),
"relevance": rel,
"trust": trust,
"freshness": freshness,
"score": _selection_score(
f["confidence"],
rel,
trust,
freshness,
priorities["fact_weight"],
),
"provisional": False,
"coverage_keys": _make_coverage_keys(
f["subject"],
f["predicate"],
f["object_text"],
extras=[
f"subject:{(f['subject'] or '').lower()}",
f"predicate:{(f['predicate'] or '').lower()}",
f"scope:{(f['fact_scope'] or '').lower()}",
],
),
}
)
# 2. Candidate claims (provisional)
claim_sql = (
"SELECT claim_id, subject, predicate, object_text, claim_scope, confidence, "
"created_at FROM candidate_claims WHERE status = 'candidate' "
)
if _sqlite_table_exists(conn, "claim_evidence"):
claim_sql += (
"AND EXISTS (SELECT 1 FROM claim_evidence ce "
"WHERE ce.claim_id = candidate_claims.claim_id) "
)
claim_sql += "ORDER BY confidence DESC, created_at DESC, claim_id LIMIT 50"
claims = conn.execute(claim_sql).fetchall()
for c in claims:
rel = _task_match(
c["subject"],
c["predicate"],
c["object_text"],
entity_name=c["subject"],
)
if is_task_scoped and rel < policy["min_relevance"]["claim"]:
continue
trust = min(0.7, 0.2 + (_signal_floor(c["confidence"], 0.2) * 0.5))
freshness = _compute_recency_score(c["created_at"])
ts = _format_ts(c["created_at"])
text = (
f"{ts}[PROVISIONAL] {c['subject']} {c['predicate']} {c['object_text']} "
f"(scope: {c['claim_scope']}, confidence: {c['confidence']:.2f})"
)
items.append(
{
"type": "claim",
"id": c["claim_id"],
"text": text,
"tokens": _estimate_tokens(text),
"relevance": rel,
"trust": trust,
"freshness": freshness,
"score": _selection_score(
c["confidence"],
rel,
trust,
freshness,
priorities["claim_weight"],
),
"provisional": True,
"coverage_keys": _make_coverage_keys(
c["subject"],
c["predicate"],
c["object_text"],
extras=[
f"subject:{(c['subject'] or '').lower()}",
f"predicate:{(c['predicate'] or '').lower()}",
f"scope:{(c['claim_scope'] or '').lower()}",
],
),
}
)
# 3. Open questions (for planner/handoff)
if priorities["question_weight"] > 0.3:
questions = conn.execute(
"SELECT q.question_id, q.question_text, q.question_type, q.priority_score, "
"q.created_at, c.title AS chunk_title "
"FROM context_questions q "
"LEFT JOIN context_chunks c ON q.chunk_id = c.chunk_id "
"WHERE q.state = 'open' "
"ORDER BY q.priority_score DESC LIMIT 20"
).fetchall()
for q in questions:
rel = _task_match(q["chunk_title"], q["question_text"])
if is_task_scoped and rel < policy["min_relevance"]["question"]:
continue
trust = 0.55
freshness = _compute_recency_score(q["created_at"])
ts = _format_ts(q["created_at"])
ctx = f" (re: {q['chunk_title']})" if q["chunk_title"] else ""
text = f"{ts}[QUESTION] {q['question_text']}{ctx}"
items.append(
{
"type": "question",
"id": q["question_id"],
"text": text,
"tokens": _estimate_tokens(text),
"relevance": rel,
"trust": trust,
"freshness": freshness,
"score": _selection_score(
q["priority_score"],
rel,
trust,
freshness,
priorities["question_weight"],
),
"provisional": False,
"coverage_keys": _make_coverage_keys(
q["question_text"],
q["chunk_title"],
extras=[f"question:{(q['question_type'] or '').lower()}"],
),
}
)
# 4. Enrichable/uncertain chunks (raw context)
if priorities["chunk_weight"] > 0:
chunk_limit = 100 if is_task_scoped else 30
chunks = conn.execute(
"SELECT chunk_id, entity_id, source_ref, title, body, materiality_score, "
"state, created_at "
"FROM context_chunks "
"WHERE state IN ('enrichable', 'uncertain', 'awaiting_human') "
"ORDER BY materiality_score DESC LIMIT ?",
(chunk_limit,),
).fetchall()
for ch in chunks:
min_chunk_relevance = policy["min_relevance"]["chunk"]
rel = _task_match(
ch["source_ref"],
ch["title"],
ch["body"][:500],
entity_id=ch["entity_id"],
entity_name=ch["source_ref"] or ch["title"],
)
if is_task_scoped and rel < min_chunk_relevance:
continue
trust = _CHUNK_TRUST_BY_STATE.get(ch["state"], 0.18)
if trust < float(policy["min_trust"].get("chunk", 0.0)):
continue
freshness = _compute_recency_score(ch["created_at"])
ts = _format_ts(ch["created_at"])
label = f"[CONTEXT:{ch['state'].upper()}]"
title_part = f" {ch['title']} —" if ch["title"] else ""
body_preview = ch["body"][:300] + ("..." if len(ch["body"]) > 300 else "")
text = f"{ts}{label}{title_part} {body_preview}"
items.append(
{
"type": "chunk",
"id": ch["chunk_id"],
"text": text,
"tokens": _estimate_tokens(text),
"group_key": _chunk_group_key(ch),
"relevance": rel,
"trust": trust,
"freshness": freshness,
"score": _selection_score(
ch["materiality_score"],
rel,
trust,
freshness,
priorities["chunk_weight"],
),
"provisional": True,
"coverage_keys": _make_coverage_keys(
ch["title"],
ch["source_ref"],
ch["body"][:240],
extras=[
f"chunk_state:{(ch['state'] or '').lower()}",
f"source:{_chunk_group_key(ch)}",
],
),
}
)
selection_meta = {"selection_strategy": "greedy"}
if advanced_strategy.get("enabled") and advanced_strategy.get("submodular_enabled"):
try:
selection = _select_advanced_items(
items,
budget=budget,
max_items_by_type=_MAX_ITEMS_BY_TYPE,
max_chunks_per_group=_MAX_CHUNKS_PER_GROUP,
strategy=advanced_strategy,
)
selected = selection["selected"]
tokens_used = selection["tokens_used"]
selection_meta = selection["metadata"]
except (sqlite3.OperationalError, ValueError, KeyError) as exc:
logger.warning(
"Advanced context selector failed, using greedy fallback: %s", exc
)
selected, tokens_used, selection_meta = _greedy_select_items(items, budget)
selection_meta["selector_error"] = str(exc)
else:
selected, tokens_used, selection_meta = _greedy_select_items(items, budget)
visible_selected = list(selected)
if policy["suppress_chunks_if_core_signal"] and any(
item["type"] in {"fact", "claim", "question"} for item in selected
):
# Reader/executor views should not mix solid signal with loose fragment dumps.
visible_selected = [item for item in selected if item["type"] != "chunk"]
total_weight = sum(max(item["score"], 0.001) for item in visible_selected) or 1.0
relevance_score = (
sum(item["relevance"] * max(item["score"], 0.001) for item in visible_selected)
/ total_weight
)
quality_score = (
sum(item["trust"] * max(item["score"], 0.001) for item in visible_selected)
/ total_weight
)
freshness_score = (
sum(item["freshness"] * max(item["score"], 0.001) for item in visible_selected)
/ total_weight
)
# Build pack body
sections: dict[str, list[str]] = {
"facts": [],
"claims": [],
"questions": [],
"chunks": [],
}
for item in visible_selected:
sections[item["type"] + "s" if item["type"] != "chunk" else "chunks"].append(
item["text"]
)
body_parts = []
if sections["facts"]:
body_parts.append("## Canonical Facts\n" + "\n".join(sections["facts"]))
if sections["claims"]:
body_parts.append(
"## Provisional Claims (⚠ unverified)\n" + "\n".join(sections["claims"])
)
if sections["questions"]:
body_parts.append("## Open Questions\n" + "\n".join(sections["questions"]))
if sections["chunks"]:
body_parts.append("## Context Fragments\n" + "\n".join(sections["chunks"]))
pack_body = (
"\n\n".join(body_parts)
if body_parts
else (
"(no task-specific context found)"
if is_task_scoped
else "(no context available)"
)
)
previewable = bool(body_parts)
if policy["preview_requires_core_signal"] or policy["preview_min_relevance"] > 0.0:
has_core_signal = bool(
sections["facts"] or sections["claims"] or sections["questions"]
)
previewable = (
previewable
and (has_core_signal if policy["preview_requires_core_signal"] else True)
and relevance_score >= float(policy["preview_min_relevance"])
and quality_score >= float(policy["preview_min_quality"])
)
# Compute input signature for caching
input_sig = f"{pack_type}:{target_ref}:{session_id}:{budget}"
pack_id = None
if persist:
pack_id = _new_id()
now = now_iso()
if _sqlite_has_column(conn, "context_packs", "contract_version"):
conn.execute(
"INSERT INTO context_packs "
"(pack_id, session_id, entity_id, pack_type, target_ref, input_signature, "
"token_budget, body, freshness_score, contract_version, created_at) "
"VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?)",
(
pack_id,
session_id,
pack_type,
target_ref,
input_sig,
budget,
pack_body,
freshness_score,
_RETRIEVAL_CONTRACT_VERSION,
now,
),
)
else:
conn.execute(
"INSERT INTO context_packs "
"(pack_id, session_id, entity_id, pack_type, target_ref, input_signature, "
"token_budget, body, freshness_score, created_at) "
"VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?, ?)",
(
pack_id,
session_id,
pack_type,
target_ref,
input_sig,
budget,
pack_body,
freshness_score,
now,
),
)
for item in visible_selected:
add_provenance_link(
conn,
subject_kind="context_pack",
subject_ref=pack_id,
source_kind=item["type"],
source_ref=str(item.get("id", "")),
excerpt=item["text"][:400],
confidence=item.get("trust", 0.5),
created_at=now,
)
upsert_memory_artifact(
conn,
artifact_kind="summary",
scope_kind="context_pack",
scope_ref=pack_id,
artifact_key=f"summary:context_pack:{pack_id}",