-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintelligence_v2.py
More file actions
934 lines (800 loc) · 29.6 KB
/
intelligence_v2.py
File metadata and controls
934 lines (800 loc) · 29.6 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
"""Intelligence v2 — Context state machine, signal detection, and enrichment policy.
Core module implementing human-sovereign knowledge management:
- Signal phrase detection (ENRICH_OK, NO_ENRICH, WAIT_HUMAN, FREEZE_CONTEXT)
- Context chunk lifecycle state machine
- Uncertainty scoring and AWAITING_HUMAN block generation
- Enrichment run audit logging
- Delta-aware reprocessing (skip on same source_hash)
"""
from __future__ import annotations
import hashlib
import logging
import re
import sqlite3
import uuid
from pathlib import Path
from typing import Any
from db_utils import (
add_provenance_link,
json_loads,
now_iso,
record_memory_event,
)
_log = logging.getLogger("intelligence-v2")
# ── Configuration ────────────────────────────────────────────────────────
_CONFIG_PATH = Path(__file__).parent / "intelligence_config.json"
_DEFAULT_CONFIG: dict[str, Any] = {
"enabled": True,
"default_enrich_policy": "manual",
"respect_signal_phrases": True,
"skip_same_unresolved_source": True,
"max_questions_per_chunk": 5,
"auto_promote_scopes": ["memory"],
"human_required_scopes": ["mapping", "validation", "bridge", "export"],
"context_pack_token_budget_default": 4000,
"conflict_detection_enabled": False,
"query_expansion_enabled": False,
"advanced_context_enabled": False,
"advanced_context_submodular_enabled": True,
"advanced_context_max_seed_entities": 8,
"advanced_context_max_related_entities": 12,
"advanced_context_max_expansion_keywords": 24,
}
def load_config() -> dict[str, Any]:
"""Load intelligence config with graceful fallback to safe defaults."""
try:
return {
**_DEFAULT_CONFIG,
**json_loads(_CONFIG_PATH.read_text(encoding="utf-8")),
}
except (FileNotFoundError, ValueError, OSError):
_log.warning("intelligence_config.json missing or invalid — using defaults")
return dict(_DEFAULT_CONFIG)
# ── Constants ────────────────────────────────────────────────────────────
# Valid context chunk states (lifecycle)
CHUNK_STATES = (
"no_enrich", # default — no enrichment allowed
"enrichable", # opt-in signal received, ready for AI processing
"uncertain", # AI parsed but couldn't extract confident claims
"awaiting_human", # questions generated, waiting for human answer
"frozen", # FREEZE_CONTEXT — no mutation allowed
"stale", # newer evidence available, needs re-assessment
"archived", # retired, kept for audit trail
)
# Valid transitions: (from_state, to_state)
_VALID_TRANSITIONS = frozenset(
{
("no_enrich", "enrichable"), # assess_context can unlock no_enrich chunks
("no_enrich", "frozen"),
("no_enrich", "stale"),
("no_enrich", "archived"),
("enrichable", "uncertain"),
("enrichable", "awaiting_human"),
("enrichable", "frozen"),
("enrichable", "stale"),
("enrichable", "archived"),
("uncertain", "awaiting_human"),
("uncertain", "enrichable"), # after human answers questions
("uncertain", "frozen"),
("uncertain", "stale"),
("awaiting_human", "enrichable"), # human answered
("awaiting_human", "frozen"),
("awaiting_human", "stale"),
("frozen", "enrichable"), # explicit unfreeze
("frozen", "archived"),
("stale", "enrichable"),
("stale", "archived"),
("enrichable", "no_enrich"), # NO_ENRICH signal revokes enrichment
("uncertain", "no_enrich"), # NO_ENRICH signal revokes enrichment
("awaiting_human", "no_enrich"), # NO_ENRICH signal revokes enrichment
}
)
# Signal phrase patterns (case-insensitive)
_SIGNAL_PATTERNS = {
"ENRICH_OK": re.compile(r"\bENRICH_OK\b", re.IGNORECASE),
"NO_ENRICH": re.compile(r"\bNO_ENRICH\b", re.IGNORECASE),
"WAIT_HUMAN": re.compile(r"\bWAIT_HUMAN\b", re.IGNORECASE),
"FREEZE_CONTEXT": re.compile(r"\bFREEZE_CONTEXT\b", re.IGNORECASE),
}
# Source types for context chunks
SOURCE_TYPES = (
"note",
"task_desc",
"session_log",
"bridge_event",
"entity_obs",
"manual",
)
# Annotation types
ANNOTATION_TYPES = ("signal", "awaiting_human", "note", "resolution", "freeze")
# Author types
AUTHOR_TYPES = ("human", "ai", "system")
# Question types for structured clarification
QUESTION_TYPES = ("scope", "semantics", "owner", "time", "action", "downstream_use")
# Enrichment run statuses
RUN_STATUSES = ("success", "skipped", "blocked", "failed")
# ── Helpers ──────────────────────────────────────────────────────────────
def compute_source_hash(text: str) -> str:
"""SHA-256 hash of text content for delta detection."""
return "sha256:" + hashlib.sha256(text.encode("utf-8")).hexdigest()
def detect_signals(text: str) -> list[str]:
"""Scan text for signal phrases. Returns list of detected signal names."""
return [name for name, pattern in _SIGNAL_PATTERNS.items() if pattern.search(text)]
def is_valid_transition(from_state: str, to_state: str) -> bool:
"""Check if state transition is allowed."""
return (from_state, to_state) in _VALID_TRANSITIONS
def _new_id() -> str:
"""Generate a UUID for new records."""
return uuid.uuid4().hex
# ── Uncertainty Scoring ──────────────────────────────────────────────────
# Markers that indicate ambiguity in text
_AMBIGUITY_MARKERS = re.compile(
r"\b(може би|вероятно|не е ясно|perhaps|maybe|unclear|TODO|TBD|FIXME|"
r"not sure|possibly|likely|depends on|to be determined)\b",
re.IGNORECASE,
)
# Completeness indicators (presence lowers uncertainty)
_COMPLETENESS_INDICATORS = re.compile(
r"\b(confirmed|verified|agreed|approved|решено|потвърдено|одобрено|"
r"final|done|complete|accepted)\b",
re.IGNORECASE,
)
def compute_uncertainty(text: str) -> float:
"""Score uncertainty of a text chunk (0.0 = certain, 1.0 = fully uncertain).
Heuristic based on:
- Ambiguity marker density
- Completeness indicator density
- Text length (very short text = less informative = higher uncertainty)
"""
words = text.split()
word_count = max(len(words), 1)
ambiguity_count = len(_AMBIGUITY_MARKERS.findall(text))
completeness_count = len(_COMPLETENESS_INDICATORS.findall(text))
# Base uncertainty from ambiguity density
ambiguity_score = min(ambiguity_count / word_count * 10.0, 1.0)
# Completeness reduces uncertainty
completeness_bonus = min(completeness_count / word_count * 10.0, 0.5)
# Very short text (<20 words) adds uncertainty
brevity_penalty = max(0.0, (20 - word_count) / 20.0) * 0.3
score = ambiguity_score - completeness_bonus + brevity_penalty
return max(0.0, min(1.0, score))
# ── Materiality Scoring ──────────────────────────────────────────────────
# Keywords indicating high-materiality content
_MATERIALITY_KEYWORDS = {
"critical": 0.9,
"mapping": 0.8,
"validation": 0.8,
"bridge": 0.7,
"export": 0.7,
"accounting": 0.9,
"GAAP": 0.9,
"IFRS": 0.9,
"balance": 0.8,
"financial": 0.8,
"audit": 0.8,
"compliance": 0.8,
"error": 0.6,
"bug": 0.5,
"fix": 0.4,
"config": 0.4,
"критично": 0.9,
"грешка": 0.6,
"счетоводство": 0.9,
"баланс": 0.8,
}
def compute_materiality(text: str, scope_hint: str | None = None) -> float:
"""Score materiality of a text chunk (0.0 = trivial, 1.0 = critical).
High materiality = mapping correctness, accounting invariants, bridge workflow.
"""
text_lower = text.lower()
scores = [
weight
for keyword, weight in _MATERIALITY_KEYWORDS.items()
if keyword.lower() in text_lower
]
# Scope hint boosts materiality
scope_boost = 0.0
if scope_hint in ("mapping", "validation", "bridge", "export"):
scope_boost = 0.3
keyword_score = max(scores) if scores else 0.1
return min(1.0, keyword_score + scope_boost)
# ── Audit Trail ──────────────────────────────────────────────────────────
def log_enrichment_run(
conn: sqlite3.Connection,
tool_name: str,
result_status: str,
input_signature: str,
chunk_id: str | None = None,
session_id: str | None = None,
reason_code: str | None = None,
started_at: str | None = None,
) -> str:
"""Record an enrichment run in the audit trail. Returns run_id."""
run_id = _new_id()
finished = now_iso()
conn.execute(
"INSERT INTO enrichment_runs "
"(run_id, tool_name, chunk_id, session_id, result_status, reason_code, "
"input_signature, started_at, finished_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
run_id,
tool_name,
chunk_id,
session_id,
result_status,
reason_code,
input_signature,
started_at or finished,
finished,
),
)
return run_id
# ── Core: Assess Context ─────────────────────────────────────────────────
def assess_context(
conn: sqlite3.Connection,
chunk_ref: str,
session_id: str | None = None,
force: bool = False,
) -> dict[str, Any]:
"""Classify context chunk, detect signals, determine state transition.
Returns dict with: chunk_id, state, policy, materiality, uncertainty,
should_skip, skip_reason, signals_detected, annotations_created.
"""
config = load_config()
started = now_iso()
if not config["enabled"]:
return {"status": "disabled", "message": "Intelligence v2 is disabled"}
# Find or create chunk
row = conn.execute(
"SELECT * FROM context_chunks WHERE chunk_id = ?", (chunk_ref,)
).fetchone()
if row is None:
# chunk_ref might be an entity_id or task reference — create new chunk
return {
"error": f"Chunk '{chunk_ref}' not found. Use ingest to create chunks first."
}
chunk_id = row["chunk_id"]
current_state = row["state"]
body = row["body"]
source_hash = row["source_hash"]
# Frozen → blocked
if current_state == "frozen" and not force:
log_enrichment_run(
conn,
"assess_context",
"blocked",
input_signature=source_hash,
chunk_id=chunk_id,
session_id=session_id,
reason_code="frozen",
started_at=started,
)
return {
"chunk_id": chunk_id,
"state": "frozen",
"should_skip": True,
"skip_reason": "chunk is frozen — no mutation allowed",
}
# Detect signal phrases in body
signals = detect_signals(body) if config["respect_signal_phrases"] else []
# Determine target state based on signals
new_state = current_state
annotations_created = []
if "FREEZE_CONTEXT" in signals:
if is_valid_transition(current_state, "frozen"):
new_state = "frozen"
annotations_created.append(
_create_annotation(
conn,
chunk_id,
"system",
"freeze",
"FREEZE_CONTEXT signal detected",
source_hash,
)
)
elif "NO_ENRICH" in signals:
if is_valid_transition(current_state, "no_enrich"):
new_state = "no_enrich"
annotations_created.append(
_create_annotation(
conn,
chunk_id,
"system",
"signal",
"NO_ENRICH signal detected — enrichment revoked",
source_hash,
)
)
elif "WAIT_HUMAN" in signals:
if is_valid_transition(current_state, "awaiting_human"):
new_state = "awaiting_human"
annotations_created.append(
_create_annotation(
conn,
chunk_id,
"system",
"signal",
"WAIT_HUMAN signal detected — waiting for human input",
source_hash,
)
)
elif "ENRICH_OK" in signals:
if is_valid_transition(current_state, "enrichable"):
new_state = "enrichable"
annotations_created.append(
_create_annotation(
conn,
chunk_id,
"system",
"signal",
"ENRICH_OK signal detected — chunk is now enrichable",
source_hash,
)
)
else:
# No explicit signals — unlock no_enrich chunks by default
if current_state == "no_enrich" and is_valid_transition(
"no_enrich", "enrichable"
):
new_state = "enrichable"
# Skip logic: awaiting_human + same source_hash → skip
# But bypass skip if signals would change the entity's state
if (
current_state == "awaiting_human"
and new_state == current_state
and config["skip_same_unresolved_source"]
and not force
):
# Check if source_hash is unchanged since last AI attempt
last_hash = conn.execute(
"SELECT source_hash_seen FROM context_annotations "
"WHERE chunk_id = ? AND annotation_type = 'awaiting_human' "
"ORDER BY created_at DESC LIMIT 1",
(chunk_id,),
).fetchone()
if last_hash and last_hash["source_hash_seen"] == source_hash:
log_enrichment_run(
conn,
"assess_context",
"skipped",
source_hash,
chunk_id=chunk_id,
session_id=session_id,
reason_code="same_unresolved_source",
started_at=started,
)
return {
"chunk_id": chunk_id,
"state": current_state,
"should_skip": True,
"skip_reason": "awaiting_human with unchanged source — skipping",
}
# Compute scores
uncertainty = compute_uncertainty(body)
materiality = compute_materiality(body)
# If enrichable and high uncertainty → uncertain state
if new_state == "enrichable" and uncertainty > 0.6:
if is_valid_transition(new_state, "uncertain"):
new_state = "uncertain"
# Update chunk
now = now_iso()
conn.execute(
"UPDATE context_chunks SET state = ?, materiality_score = ?, "
"last_ai_attempt_at = ?, updated_at = ? WHERE chunk_id = ?",
(new_state, materiality, now, now, chunk_id),
)
if new_state != current_state:
record_memory_event(
conn,
event_type="chunk_state_change",
aggregate_kind="chunk",
aggregate_id=chunk_id,
tool_name="sqlite-intel.assess_context",
event_ts=now,
old_value={"state": current_state},
new_value={
"state": new_state,
"materiality": round(materiality, 3),
"uncertainty": round(uncertainty, 3),
},
source_kind="chunk",
source_ref=chunk_id,
source_excerpt=body[:300],
)
enrich_policy = config["default_enrich_policy"]
log_enrichment_run(
conn,
"assess_context",
"success",
source_hash,
chunk_id=chunk_id,
session_id=session_id,
started_at=started,
)
return {
"chunk_id": chunk_id,
"state": new_state,
"previous_state": current_state,
"policy": enrich_policy,
"materiality": round(materiality, 3),
"uncertainty": round(uncertainty, 3),
"should_skip": False,
"skip_reason": None,
"signals_detected": signals,
"annotations_created": len(annotations_created),
}
# ── Core: Queue Clarification ────────────────────────────────────────────
def queue_clarification(
conn: sqlite3.Connection,
chunk_ref: str,
max_questions: int = 5,
) -> dict[str, Any]:
"""Generate AWAITING_HUMAN block with focused questions. Locks chunk.
Returns dict with: chunk_id, questions, awaiting_human_block.
"""
config = load_config()
started = now_iso()
if not config["enabled"]:
return {"status": "disabled"}
max_q = min(max_questions, config["max_questions_per_chunk"])
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"]
current_state = row["state"]
body = row["body"]
source_hash = row["source_hash"]
if current_state == "frozen":
log_enrichment_run(
conn,
"queue_clarification",
"blocked",
source_hash,
chunk_id=chunk_id,
reason_code="frozen",
started_at=started,
)
return {"error": "Chunk is frozen — cannot queue clarification"}
# Generate questions based on content analysis
questions = _generate_questions(body, max_q)
# Create question records
question_ids = []
for q in questions:
qid = _new_id()
q_created_at = now_iso()
conn.execute(
"INSERT INTO context_questions "
"(question_id, chunk_id, question_text, question_type, priority_score, "
"state, created_at) VALUES (?, ?, ?, ?, ?, 'open', ?)",
(qid, chunk_id, q["text"], q["type"], q["priority"], q_created_at),
)
record_memory_event(
conn,
event_type="question_create",
aggregate_kind="question",
aggregate_id=qid,
tool_name="sqlite-intel.queue_clarification",
event_ts=q_created_at,
new_value=q,
source_kind="chunk",
source_ref=chunk_id,
source_excerpt=q["text"],
)
question_ids.append(qid)
# Build AWAITING_HUMAN block
now = now_iso()
block_lines = [
f"--- AWAITING_HUMAN | {now} ---",
f"source_hash: {source_hash}",
"reason: insufficient_clarity",
"questions:",
]
for i, q in enumerate(questions, 1):
block_lines.append(f"{i}. {q['text']}")
block_lines.append("status: blocked_until_human_update")
block_text = "\n".join(block_lines)
# Create annotation
_create_annotation(conn, chunk_id, "ai", "awaiting_human", block_text, source_hash)
# Transition to awaiting_human
if is_valid_transition(current_state, "awaiting_human"):
conn.execute(
"UPDATE context_chunks SET state = 'awaiting_human', "
"last_ai_attempt_at = ?, updated_at = ? WHERE chunk_id = ?",
(now, now, chunk_id),
)
record_memory_event(
conn,
event_type="chunk_state_change",
aggregate_kind="chunk",
aggregate_id=chunk_id,
tool_name="sqlite-intel.queue_clarification",
event_ts=now,
old_value={"state": current_state},
new_value={"state": "awaiting_human"},
source_kind="chunk",
source_ref=chunk_id,
source_excerpt=block_text[:300],
)
log_enrichment_run(
conn,
"queue_clarification",
"success",
source_hash,
chunk_id=chunk_id,
started_at=started,
)
return {
"chunk_id": chunk_id,
"state": "awaiting_human",
"questions": [{"id": qid, **q} for qid, q in zip(question_ids, questions)],
"awaiting_human_block": block_text,
}
# ── Core: Record Human Answer ────────────────────────────────────────────
def record_human_answer(
conn: sqlite3.Connection,
chunk_ref: str,
answer_text: str,
question_id: str | None = None,
) -> dict[str, Any]:
"""Ingest human answer, update state, resolve questions.
Returns dict with: chunk_id, new_state, questions_resolved.
"""
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"]
current_state = row["state"]
if current_state == "frozen":
return {"error": "Chunk is frozen — cannot record answer"}
now = now_iso()
questions_resolved = 0
if question_id:
# Resolve specific question
conn.execute(
"UPDATE context_questions SET state = 'answered', answered_by = 'human', "
"answered_at = ?, answer_text = ? WHERE question_id = ? AND chunk_id = ?",
(now, answer_text, question_id, chunk_id),
)
questions_resolved = 1
else:
# Resolve all open questions for this chunk
result = conn.execute(
"UPDATE context_questions SET state = 'answered', answered_by = 'human', "
"answered_at = ?, answer_text = ? WHERE chunk_id = ? AND state = 'open'",
(now, answer_text, chunk_id),
)
questions_resolved = result.rowcount
# Create resolution annotation
_create_annotation(
conn,
chunk_id,
"human",
"resolution",
f"Human answer: {answer_text}",
row["source_hash"],
)
# Recompute source hash (content is now enriched with human context)
new_hash = compute_source_hash(row["body"] + "\n" + answer_text)
# Transition back to enrichable
new_state = current_state
if current_state in ("awaiting_human", "uncertain"):
if is_valid_transition(current_state, "enrichable"):
new_state = "enrichable"
conn.execute(
"UPDATE context_chunks SET state = ?, source_hash = ?, "
"last_human_update_at = ?, updated_at = ? WHERE chunk_id = ?",
(new_state, new_hash, now, now, chunk_id),
)
add_provenance_link(
conn,
subject_kind="chunk",
subject_ref=chunk_id,
source_kind="human_answer",
source_ref=question_id or chunk_id,
excerpt=answer_text[:300],
confidence=1.0,
created_at=now,
)
record_memory_event(
conn,
event_type="human_answer",
aggregate_kind="chunk",
aggregate_id=chunk_id,
tool_name="sqlite-intel.record_human_answer",
event_ts=now,
old_value={"state": current_state, "source_hash": row["source_hash"]},
new_value={"state": new_state, "source_hash": new_hash},
source_kind="human_answer",
source_ref=question_id or chunk_id,
source_excerpt=answer_text[:300],
)
log_enrichment_run(
conn,
"record_human_answer",
"success",
new_hash,
chunk_id=chunk_id,
started_at=started,
)
return {
"chunk_id": chunk_id,
"new_state": new_state,
"previous_state": current_state,
"questions_resolved": questions_resolved,
"source_hash_updated": new_hash != row["source_hash"],
}
# ── Chunk Ingestion (internal helper for MCP tools) ─────────────────────
def ingest_chunk(
conn: sqlite3.Connection,
body: str,
source_type: str,
source_ref: str,
title: str | None = None,
session_id: str | None = None,
entity_id: str | None = None,
language: str = "bg",
) -> dict[str, Any]:
"""Create a new context chunk with default no_enrich state.
Returns dict with: chunk_id, state, source_hash, signals_detected.
"""
config = load_config()
if not config["enabled"]:
return {"status": "disabled"}
chunk_id = _new_id()
source_hash = compute_source_hash(body)
signals = detect_signals(body) if config["respect_signal_phrases"] else []
# Determine initial state from signals
initial_state = "no_enrich"
if "FREEZE_CONTEXT" in signals:
initial_state = "frozen"
elif "ENRICH_OK" in signals:
initial_state = "enrichable"
now = now_iso()
conn.execute(
"INSERT INTO context_chunks "
"(chunk_id, session_id, entity_id, source_type, source_ref, source_hash, "
"title, body, language, state, enrich_policy, materiality_score, created_at, updated_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
chunk_id,
session_id,
entity_id,
source_type,
source_ref,
source_hash,
title,
body,
language,
initial_state,
config["default_enrich_policy"],
0.0,
now,
now,
),
)
return {
"chunk_id": chunk_id,
"state": initial_state,
"source_hash": source_hash,
"signals_detected": signals,
}
# ── Internal Helpers ─────────────────────────────────────────────────────
def _create_annotation(
conn: sqlite3.Connection,
chunk_id: str,
author_type: str,
annotation_type: str,
body: str,
source_hash_seen: str | None = None,
) -> str:
"""Insert a context annotation. Returns annotation_id."""
ann_id = _new_id()
conn.execute(
"INSERT INTO context_annotations "
"(annotation_id, chunk_id, author_type, annotation_type, body, "
"source_hash_seen, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
(
ann_id,
chunk_id,
author_type,
annotation_type,
body,
source_hash_seen,
now_iso(),
),
)
return ann_id
def _generate_questions(text: str, max_questions: int) -> list[dict[str, Any]]:
"""Generate clarification questions based on text content analysis.
Heuristic: looks for ambiguity signals and generates typed questions.
"""
questions: list[dict[str, Any]] = []
text_lower = text.lower()
# Scope question — always useful if scope is unclear
scope_keywords = (
"mapping",
"bridge",
"validation",
"export",
"memory",
"task",
"note",
"config",
"workflow",
)
has_scope = any(kw in text_lower for kw in scope_keywords)
if not has_scope and len(questions) < max_questions:
questions.append(
{
"text": "Към кой workflow се отнася: memory, bridge, mapping, export или validation?",
"type": "scope",
"priority": 0.9,
}
)
# Temporal question — if no dates/times mentioned
time_pattern = re.compile(
r"\b(\d{4}-\d{2}|\d{2}\.\d{2}\.\d{4}|днес|утре|вчера|"
r"today|tomorrow|yesterday)\b",
re.IGNORECASE,
)
if not time_pattern.search(text) and len(questions) < max_questions:
questions.append(
{
"text": "Това описва текущо правило, историческа бележка или идея за бъдеща промяна?",
"type": "time",
"priority": 0.7,
}
)
# Semantics question — if text is very short or ambiguous
if len(text.split()) < 30 and len(questions) < max_questions:
questions.append(
{
"text": "Може ли да разшириш контекста — какъв е конкретният проблем или решение?",
"type": "semantics",
"priority": 0.8,
}
)
# Action question — if no clear actionable items
action_keywords = (
"трябва",
"направи",
"fix",
"add",
"remove",
"update",
"create",
"should",
"must",
"need",
)
has_action = any(kw in text_lower for kw in action_keywords)
if not has_action and len(questions) < max_questions:
questions.append(
{
"text": "Има ли конкретно действие, което трябва да се предприеме?",
"type": "action",
"priority": 0.6,
}
)
# Downstream use question
if len(questions) < max_questions:
questions.append(
{
"text": "Това трябва ли да влезе в canonical knowledge или да остане като note?",
"type": "downstream_use",
"priority": 0.5,
}
)
# Sort by priority descending, limit
questions.sort(key=lambda q: q["priority"], reverse=True)
return questions[:max_questions]