-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclaim_graph.py
More file actions
665 lines (593 loc) · 20.7 KB
/
claim_graph.py
File metadata and controls
665 lines (593 loc) · 20.7 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
"""Claim Graph — Candidate knowledge extraction, evidence, and promotion.
Phase 2 of Intelligence v2:
- Typed (Subject, Predicate, Object, Scope) claim extraction from text
- Evidence attachment with provenance tracking
- Governance gates for promotion to canonical facts
- Human-required vs auto-promotable scope policies
"""
from __future__ import annotations
import logging
import re
import sqlite3
from typing import Any
from db_utils import (
add_knowledge_link,
add_provenance_link,
now_iso,
record_memory_event,
)
from intelligence_v2 import (
_new_id,
load_config,
log_enrichment_run,
)
from enrichment_constants import _PREDICATE_BASE_CONFIDENCE
_log = logging.getLogger("claim_graph")
# ── Claim Extraction Heuristics ──────────────────────────────────────────
# Patterns for typed claim extraction (simple heuristic, no NLP dependency)
# Format: subject → predicate → object
# "X uses Y", "X depends on Y", "X is Y"
_RELATION_PATTERNS = [
# English patterns
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:uses?|използва)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"uses",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:depends?\s+on|зависи\s+от)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"depends_on",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:is|е|са)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)", re.I
),
"is",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:requires?|изисква)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"requires",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:produces?|генерира|създава)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"produces",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:validates?|валидира)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"validates",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:contains?|съдържа)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"contains",
),
(
re.compile(
r"(\b\w[\w\s]{1,40}?)\s+(?:replaces?|замества|заменя)\s+(\b\w[\w\s]{1,40}?)(?:\.|,|$)",
re.I,
),
"replaces",
),
]
# Scope detection keywords
_SCOPE_KEYWORDS = {
"memory": ("memory", "entity", "observation", "relation", "knowledge", "памет"),
"bridge": ("bridge", "sync", "push", "pull", "мост", "синхронизация"),
"mapping": ("mapping", "map", "column", "template", "шаблон", "маппинг"),
"validation": ("validation", "validate", "check", "audit", "валидация"),
"export": ("export", "excel", "pdf", "report", "експорт", "отчет"),
}
def _detect_scope(text: str) -> str:
"""Detect the most likely scope from text content."""
text_lower = text.lower()
scores: dict[str, int] = {}
for scope, keywords in _SCOPE_KEYWORDS.items():
scores[scope] = sum(1 for kw in keywords if kw in text_lower)
if not any(scores.values()):
return "memory" # default scope
return max(scores, key=scores.get)
def _extract_spo_tuples(text: str) -> list[dict[str, Any]]:
"""Extract (Subject, Predicate, Object) tuples from text using patterns."""
tuples: list[dict[str, Any]] = []
seen = set()
for pattern, predicate in _RELATION_PATTERNS:
for match in pattern.finditer(text):
subject = match.group(1).strip()
obj = match.group(2).strip()
# Skip trivial matches
if len(subject) < 3 or len(obj) < 3:
continue
if subject.lower() == obj.lower():
continue
key = (subject.lower(), predicate, obj.lower())
if key not in seen:
seen.add(key)
tuples.append(
{
"subject": subject,
"predicate": predicate,
"object": obj,
"start": match.start(0),
"end": match.end(0),
"excerpt": match.group(0).strip(),
}
)
return tuples
# ── Core: Extract Candidate Claims ───────────────────────────────────────
def extract_candidate_claims(
conn: sqlite3.Connection,
chunk_ref: str,
scope_hint: str | None = None,
) -> dict[str, Any]:
"""Extract typed (S, P, O, scope) claims with evidence from a context chunk.
Returns dict with: chunk_id, claims_extracted, claims.
"""
config = load_config()
started = now_iso()
if not config["enabled"]:
return {"status": "disabled"}
row = conn.execute(
"SELECT * FROM context_chunks WHERE chunk_id = ?", (chunk_ref,)
).fetchone()
if row is None:
return {"error": f"Chunk '{chunk_ref}' not found"}
chunk_id = row["chunk_id"]
state = row["state"]
body = row["body"]
# Only extract from enrichable or uncertain chunks
if state not in ("enrichable", "uncertain"):
log_enrichment_run(
conn,
"extract_candidate_claims",
"blocked",
chunk_id,
chunk_id=chunk_id,
reason_code=f"invalid_state:{state}",
started_at=started,
)
return {
"error": f"Chunk state '{state}' does not allow claim extraction. "
"State must be 'enrichable' or 'uncertain'.",
}
# Extract SPO tuples
tuples = _extract_spo_tuples(body)
scope = scope_hint or _detect_scope(body)
# Determine if human confirmation required
requires_human = scope in config.get("human_required_scopes", [])
claims_created: list[dict[str, Any]] = []
now = now_iso()
for t in tuples:
claim_id = _new_id()
# Adaptive confidence from predicate type (Layer 2 parity)
confidence = _PREDICATE_BASE_CONFIDENCE.get(t["predicate"], 0.5)
conn.execute(
"INSERT INTO candidate_claims "
"(claim_id, chunk_id, subject, predicate, object_text, object_type, "
"claim_scope, confidence, status, requires_human, created_at, updated_at) "
"VALUES (?, ?, ?, ?, ?, 'text', ?, ?, 'candidate', ?, ?, ?)",
(
claim_id,
chunk_id,
t["subject"],
t["predicate"],
t["object"],
scope,
confidence,
1 if requires_human else 0,
now,
now,
),
)
# Create evidence record linking claim to source chunk
evidence_id = _new_id()
conn.execute(
"INSERT INTO claim_evidence "
"(evidence_id, claim_id, evidence_type, evidence_ref, weight, excerpt, "
"source_start, source_end, created_at) "
"VALUES (?, ?, 'source_chunk', ?, 1.0, ?, ?, ?, ?)",
(
evidence_id,
claim_id,
chunk_id,
t.get("excerpt") or (body[:200] if len(body) > 200 else body),
t.get("start"),
t.get("end"),
now,
),
)
add_provenance_link(
conn,
subject_kind="claim",
subject_ref=claim_id,
source_kind="chunk",
source_ref=chunk_id,
span_start=t.get("start"),
span_end=t.get("end"),
excerpt=t.get("excerpt"),
confidence=confidence,
created_at=now,
)
record_memory_event(
conn,
event_type="claim_extract",
aggregate_kind="claim",
aggregate_id=claim_id,
tool_name="sqlite-intel.extract_candidate_claims",
event_ts=now,
new_value={
"subject": t["subject"],
"predicate": t["predicate"],
"object": t["object"],
"scope": scope,
"confidence": confidence,
},
source_kind="chunk",
source_ref=chunk_id,
source_excerpt=t.get("excerpt"),
source_start=t.get("start"),
source_end=t.get("end"),
)
claims_created.append(
{
"claim_id": claim_id,
"subject": t["subject"],
"predicate": t["predicate"],
"object": t["object"],
"scope": scope,
"confidence": confidence,
"requires_human": requires_human,
}
)
log_enrichment_run(
conn,
"extract_candidate_claims",
"success",
chunk_id,
chunk_id=chunk_id,
started_at=started,
)
return {
"chunk_id": chunk_id,
"claims_extracted": len(claims_created),
"scope": scope,
"claims": claims_created,
}
# ── Core: Promote Candidate ──────────────────────────────────────────────
# Promotion modes
PROMOTION_MODES = ("human_confirmed", "multi_evidence", "imported", "auto_layer1")
# Minimum evidence count for auto-promotion
_MIN_EVIDENCE_FOR_AUTO = 3
_MIN_CONFIDENCE_FOR_AUTO = 0.7
def promote_candidate(
conn: sqlite3.Connection,
claim_id: str,
mode: str = "human_confirmed",
) -> dict[str, Any]:
"""Governance gate: candidate → canonical_fact.
Modes:
- human_confirmed: explicit human approval (always allowed)
- multi_evidence: multiple independent evidence points (policy-gated)
- imported: bulk import from trusted source
Returns dict with: claim_id, promoted, fact_id, reason.
"""
config = load_config()
started = now_iso()
if not config["enabled"]:
return {"status": "disabled"}
if mode not in PROMOTION_MODES:
return {
"error": f"Invalid promotion mode: {mode}. Use one of: {PROMOTION_MODES}"
}
row = conn.execute(
"SELECT * FROM candidate_claims WHERE claim_id = ?", (claim_id,)
).fetchone()
if row is None:
return {"error": f"Claim '{claim_id}' not found"}
claim_scope = row["claim_scope"]
status = row["status"]
requires_human = bool(row["requires_human"])
if status != "candidate":
return {"error": f"Claim status is '{status}', expected 'candidate'"}
# Governance gate: sensitive scopes require human confirmation
human_required_scopes = config.get("human_required_scopes", [])
auto_promote_scopes = config.get("auto_promote_scopes", [])
if claim_scope in human_required_scopes and mode != "human_confirmed":
log_enrichment_run(
conn,
"promote_candidate",
"blocked",
claim_id,
reason_code=f"human_required_for_{claim_scope}",
started_at=started,
)
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Scope '{claim_scope}' requires human confirmation. "
f"Use mode='human_confirmed' to promote.",
}
# Auto Layer 1: confidence-only check, no evidence count requirement
if mode == "auto_layer1":
if claim_scope not in auto_promote_scopes:
log_enrichment_run(
conn,
"promote_candidate",
"blocked",
claim_id,
reason_code=f"scope_{claim_scope}_not_auto",
started_at=started,
)
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Scope '{claim_scope}' not in auto_promote_scopes.",
}
if row["confidence"] < _MIN_CONFIDENCE_FOR_AUTO:
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Confidence {row['confidence']:.2f} < {_MIN_CONFIDENCE_FOR_AUTO}",
}
if requires_human:
return {
"claim_id": claim_id,
"promoted": False,
"reason": "Claim requires human confirmation",
}
# Multi-evidence validation
if mode == "multi_evidence":
if claim_scope not in auto_promote_scopes:
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Scope '{claim_scope}' not in auto_promote_scopes. "
f"Only {auto_promote_scopes} allow multi_evidence promotion.",
}
evidence_count = conn.execute(
"SELECT COUNT(*) FROM claim_evidence WHERE claim_id = ?", (claim_id,)
).fetchone()[0]
if evidence_count < _MIN_EVIDENCE_FOR_AUTO:
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Insufficient evidence ({evidence_count}/{_MIN_EVIDENCE_FOR_AUTO}) "
f"for auto-promotion.",
}
if row["confidence"] < _MIN_CONFIDENCE_FOR_AUTO:
return {
"claim_id": claim_id,
"promoted": False,
"reason": f"Confidence {row['confidence']:.2f} below threshold "
f"{_MIN_CONFIDENCE_FOR_AUTO}.",
}
# Promote: create canonical fact
now = now_iso()
existing_same = conn.execute(
"SELECT fact_id FROM canonical_facts "
"WHERE subject = ? AND predicate = ? AND object_text = ? "
"AND COALESCE(valid_to, '') = '' "
"ORDER BY updated_at DESC LIMIT 1",
(row["subject"], row["predicate"], row["object_text"]),
).fetchone()
if existing_same:
fact_id = existing_same["fact_id"]
conn.execute(
"UPDATE candidate_claims SET status = 'promoted', promoted_to_fact_id = ?, "
"updated_at = ? WHERE claim_id = ?",
(fact_id, now, claim_id),
)
add_provenance_link(
conn,
subject_kind="fact",
subject_ref=fact_id,
source_kind="claim",
source_ref=claim_id,
excerpt=f"Existing fact reinforced by claim {claim_id}",
created_at=now,
)
record_memory_event(
conn,
event_type="fact_reinforce",
aggregate_kind="fact",
aggregate_id=fact_id,
tool_name="sqlite-intel.promote_candidate",
event_ts=now,
new_value={"claim_id": claim_id, "mode": mode},
source_kind="claim",
source_ref=claim_id,
)
return {
"claim_id": claim_id,
"promoted": True,
"fact_id": fact_id,
"subject": row["subject"],
"predicate": row["predicate"],
"object": row["object_text"],
"scope": claim_scope,
"validation_mode": mode,
"reused_existing_fact": True,
}
fact_id = _new_id()
# Build provenance summary
evidence_rows = conn.execute(
"SELECT evidence_type, evidence_ref, excerpt, source_start, source_end "
"FROM claim_evidence WHERE claim_id = ?",
(claim_id,),
).fetchall()
provenance = f"Promoted via {mode} from claim {claim_id}. "
provenance += f"Evidence: {len(evidence_rows)} sources "
provenance += (
"("
+ ", ".join(
f"{r['evidence_type']}:{r['evidence_ref'][:16]}" for r in evidence_rows[:5]
)
+ ")"
)
conn.execute(
"INSERT INTO canonical_facts "
"(fact_id, subject, predicate, object_text, object_type, fact_scope, "
"provenance_summary, confidence, validation_mode, source_claim_id, "
"valid_from, valid_to, superseded_by_fact_id, contradiction_count, "
"created_at, updated_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, 0, ?, ?)",
(
fact_id,
row["subject"],
row["predicate"],
row["object_text"],
row["object_type"],
claim_scope,
provenance,
row["confidence"],
mode,
claim_id,
now,
now,
now,
),
)
# Update claim status
conn.execute(
"UPDATE candidate_claims SET status = 'promoted', promoted_to_fact_id = ?, "
"updated_at = ? WHERE claim_id = ?",
(fact_id, now, claim_id),
)
add_provenance_link(
conn,
subject_kind="fact",
subject_ref=fact_id,
source_kind="claim",
source_ref=claim_id,
excerpt=f"Promoted from claim {claim_id}",
confidence=row["confidence"],
created_at=now,
)
for ev in evidence_rows:
add_provenance_link(
conn,
subject_kind="fact",
subject_ref=fact_id,
source_kind=ev["evidence_type"],
source_ref=ev["evidence_ref"],
span_start=ev["source_start"],
span_end=ev["source_end"],
excerpt=ev["excerpt"],
confidence=row["confidence"],
created_at=now,
)
# Create impact edge: source_chunk → promoted fact
try:
from impact_graph import add_impact_edge
add_impact_edge(
conn,
source_kind="chunk",
source_ref=row["chunk_id"],
target_kind="fact",
target_ref=fact_id,
impact_type="informs",
impact_score=row["confidence"],
rationale=f"Claim {claim_id} promoted via {mode}",
)
except Exception as e:
_log.debug("impact_graph link failed: %s", e)
competing_rows = conn.execute(
"SELECT fact_id FROM canonical_facts "
"WHERE fact_id != ? AND subject = ? AND predicate = ? "
"AND object_text != ? AND COALESCE(valid_to, '') = ''",
(fact_id, row["subject"], row["predicate"], row["object_text"]),
).fetchall()
for comp in competing_rows:
add_knowledge_link(
conn,
subject_kind="fact",
subject_ref=fact_id,
relation_type="contradicts",
object_kind="fact",
object_ref=comp["fact_id"],
rationale=f"Same subject/predicate, different object after claim {claim_id}",
created_at=now,
)
add_knowledge_link(
conn,
subject_kind="fact",
subject_ref=comp["fact_id"],
relation_type="contradicts",
object_kind="fact",
object_ref=fact_id,
rationale=f"Same subject/predicate, different object after claim {claim_id}",
created_at=now,
)
conn.execute(
"UPDATE canonical_facts SET contradiction_count = contradiction_count + 1 "
"WHERE fact_id IN (?, ?)",
(fact_id, comp["fact_id"]),
)
record_memory_event(
conn,
event_type="fact_promote",
aggregate_kind="fact",
aggregate_id=fact_id,
tool_name="sqlite-intel.promote_candidate",
event_ts=now,
new_value={
"subject": row["subject"],
"predicate": row["predicate"],
"object": row["object_text"],
"claim_id": claim_id,
"mode": mode,
},
source_kind="claim",
source_ref=claim_id,
)
log_enrichment_run(
conn, "promote_candidate", "success", claim_id, started_at=started
)
return {
"claim_id": claim_id,
"promoted": True,
"fact_id": fact_id,
"subject": row["subject"],
"predicate": row["predicate"],
"object": row["object_text"],
"scope": claim_scope,
"validation_mode": mode,
}
def auto_promote_layer1(
conn: sqlite3.Connection,
claims: list[dict[str, Any]],
threshold: float = 0.7,
) -> list[dict[str, Any]]:
"""Auto-promote high-confidence Layer 1 claims. Returns list of promoted results."""
promoted = []
for c in claims:
if c.get("requires_human"):
continue
if c.get("confidence", 0) < threshold:
continue
# Dedup check against existing canonical facts
exists = conn.execute(
"SELECT 1 FROM canonical_facts WHERE subject = ? AND predicate = ? AND object_text = ?",
(c["subject"], c["predicate"], c["object"]),
).fetchone()
if exists:
continue
result = promote_candidate(conn, c["claim_id"], mode="auto_layer1")
if result.get("promoted"):
promoted.append(result)
return promoted