-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
916 lines (795 loc) · 34.1 KB
/
schema.py
File metadata and controls
916 lines (795 loc) · 34.1 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
"""
Knowledge Harness - Database Schema and Models
SQLite-backed storage with Python dataclasses for type safety.
"""
import sqlite3
import uuid
from dataclasses import dataclass, field, asdict
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Optional
import json
# ============================================================================
# Enums
# ============================================================================
class ContentType(Enum):
ARTICLE = "article"
PAPER = "paper"
NOTE = "note"
CODE = "code"
CONVERSATION = "conversation"
REFERENCE = "reference"
OTHER = "other"
class DocumentStatus(Enum):
ACTIVE = "active"
ARCHIVED = "archived"
SUPERSEDED = "superseded"
class ChunkType(Enum):
NARRATIVE = "narrative"
ARGUMENT = "argument"
DATA = "data"
CODE = "code"
DEFINITION = "definition"
EXAMPLE = "example"
OTHER = "other"
class ConceptType(Enum):
TOPIC = "topic"
ENTITY = "entity"
METHOD = "method"
CLAIM = "claim"
QUESTION = "question"
class UsageOutcome(Enum):
WIN = "win"
PARTIAL = "partial"
MISS = "miss"
MISLEADING = "misleading"
class TaskType(Enum):
FACTUAL_LOOKUP = "factual_lookup"
IMPLEMENTATION_HOWTO = "implementation_howto"
CONCEPTUAL_UNDERSTANDING = "conceptual_understanding"
OPINION_GATHERING = "opinion_gathering"
DECISION_SUPPORT = "decision_support"
DEBUGGING = "debugging"
EXPLORATORY_RESEARCH = "exploratory_research"
CREATIVE_INSPIRATION = "creative_inspiration"
OTHER = "other"
class SourceType(Enum):
FORUM = "forum"
ACADEMIC = "academic"
NEWS = "news"
DOCS = "docs"
SOCIAL = "social"
CONVERSATION = "conversation"
PERSONAL_NOTES = "personal_notes"
OTHER = "other"
class RhetoricalMode(Enum):
EXPLANATORY = "explanatory"
ARGUMENTATIVE = "argumentative"
ANECDOTAL = "anecdotal"
SARCASTIC = "sarcastic"
INSTRUCTIONAL = "instructional"
NEUTRAL = "neutral"
OTHER = "other"
class EntityType(Enum):
DOCUMENT = "document"
CHUNK = "chunk"
CONCEPT = "concept"
class LinkCreator(Enum):
AUTO = "auto"
MANUAL = "manual"
# ============================================================================
# Data Models
# ============================================================================
@dataclass
class SourceProfile:
"""
Captures learned understanding of a source's functional strengths and weaknesses.
Not a reliability score—a capability profile.
"""
id: str
domain: str # e.g., "reddit.com", "arxiv.org", "internal-wiki"
source_type: SourceType
functional_profile: Optional[str] = None # LLM-generated prose assessment
strengths: list[str] = field(default_factory=list) # e.g., ["practical_howto", "community_sentiment"]
weaknesses: list[str] = field(default_factory=list) # e.g., ["factual_accuracy", "theoretical_depth"]
trace_counts_by_task: dict = field(default_factory=dict) # {"debugging": {"win": 5, "miss": 1}, ...}
created_at: datetime = field(default_factory=datetime.utcnow)
updated_at: datetime = field(default_factory=datetime.utcnow)
@classmethod
def create(cls, domain: str, source_type: SourceType = SourceType.OTHER, **kwargs):
return cls(
id=str(uuid.uuid4()),
domain=domain.lower().strip(),
source_type=source_type,
**kwargs
)
@dataclass
class Document:
id: str
source: str
content_type: ContentType
title: str
raw_content: str
source_profile_id: Optional[str] = None # FK to SourceProfile
top_summary: Optional[str] = None
key_claims: list[str] = field(default_factory=list)
ingested_at: datetime = field(default_factory=datetime.utcnow)
last_accessed: datetime = field(default_factory=datetime.utcnow)
access_count: int = 0
status: DocumentStatus = DocumentStatus.ACTIVE
@classmethod
def create(cls, source: str, content_type: ContentType, title: str, raw_content: str, **kwargs):
return cls(
id=str(uuid.uuid4()),
source=source,
content_type=content_type,
title=title,
raw_content=raw_content,
**kwargs
)
@dataclass
class Chunk:
id: str
document_id: str
content: str
position: int
summary: Optional[str] = None
chunk_type: ChunkType = ChunkType.OTHER
token_count: int = 0
# LLM-generated prose assessment of what this chunk is good for
functional_profile: Optional[str] = None
# Flags for retrieval-time reasoning
needs_context: bool = False # True if chunk doesn't stand alone well
rhetorical_mode: RhetoricalMode = RhetoricalMode.NEUTRAL
@classmethod
def create(cls, document_id: str, content: str, position: int, **kwargs):
return cls(
id=str(uuid.uuid4()),
document_id=document_id,
content=content,
position=position,
token_count=len(content.split()), # rough estimate
**kwargs
)
@dataclass
class Concept:
id: str
name: str
description: Optional[str] = None
aliases: list[str] = field(default_factory=list)
concept_type: ConceptType = ConceptType.TOPIC
# LLM-generated assessment of our knowledge about this concept
functional_profile: Optional[str] = None
gap_notes: Optional[str] = None # What's missing in our knowledge
created_at: datetime = field(default_factory=datetime.utcnow)
updated_at: datetime = field(default_factory=datetime.utcnow)
@classmethod
def create(cls, name: str, **kwargs):
return cls(
id=str(uuid.uuid4()),
name=name.lower().strip(),
**kwargs
)
@dataclass
class ChunkConcept:
"""Junction table linking chunks to concepts with weight."""
chunk_id: str
concept_id: str
weight: float = 1.0
@dataclass
class UsageTrace:
id: str
chunk_ids: list[str]
session_id: str
context_summary: str
task_type: TaskType = TaskType.OTHER # What kind of task was this
query: Optional[str] = None
outcome: UsageOutcome = UsageOutcome.PARTIAL
notes: Optional[str] = None # Why did it work or fail - the richest signal
timestamp: datetime = field(default_factory=datetime.utcnow)
@classmethod
def create(cls, chunk_ids: list[str], session_id: str, context_summary: str, **kwargs):
return cls(
id=str(uuid.uuid4()),
chunk_ids=chunk_ids,
session_id=session_id,
context_summary=context_summary,
**kwargs
)
@dataclass
class Link:
id: str
source_type: EntityType
source_id: str
target_type: EntityType
target_id: str
relation: str
weight: float = 1.0
created_at: datetime = field(default_factory=datetime.utcnow)
created_by: LinkCreator = LinkCreator.AUTO
@classmethod
def create(cls, source_type: EntityType, source_id: str,
target_type: EntityType, target_id: str, relation: str, **kwargs):
return cls(
id=str(uuid.uuid4()),
source_type=source_type,
source_id=source_id,
target_type=target_type,
target_id=target_id,
relation=relation,
**kwargs
)
# ============================================================================
# Database Manager
# ============================================================================
class KnowledgeDB:
"""SQLite database manager for the knowledge harness."""
SCHEMA = """
-- Source Profiles (learned understanding of source reliability by context)
CREATE TABLE IF NOT EXISTS source_profiles (
id TEXT PRIMARY KEY,
domain TEXT NOT NULL UNIQUE,
source_type TEXT DEFAULT 'other',
functional_profile TEXT,
strengths TEXT, -- JSON array
weaknesses TEXT, -- JSON array
trace_counts_by_task TEXT, -- JSON object
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
-- Documents
CREATE TABLE IF NOT EXISTS documents (
id TEXT PRIMARY KEY,
source TEXT NOT NULL,
source_profile_id TEXT,
content_type TEXT NOT NULL,
title TEXT NOT NULL,
raw_content TEXT NOT NULL,
top_summary TEXT,
key_claims TEXT, -- JSON array
ingested_at TEXT NOT NULL,
last_accessed TEXT NOT NULL,
access_count INTEGER DEFAULT 0,
status TEXT DEFAULT 'active',
FOREIGN KEY (source_profile_id) REFERENCES source_profiles(id)
);
-- Chunks
CREATE TABLE IF NOT EXISTS chunks (
id TEXT PRIMARY KEY,
document_id TEXT NOT NULL,
content TEXT NOT NULL,
position INTEGER NOT NULL,
summary TEXT,
chunk_type TEXT DEFAULT 'other',
token_count INTEGER DEFAULT 0,
functional_profile TEXT,
needs_context INTEGER DEFAULT 0,
rhetorical_mode TEXT DEFAULT 'neutral',
FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
);
-- Concepts
CREATE TABLE IF NOT EXISTS concepts (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
description TEXT,
aliases TEXT, -- JSON array
concept_type TEXT DEFAULT 'topic',
functional_profile TEXT,
gap_notes TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
-- Chunk-Concept junction
CREATE TABLE IF NOT EXISTS chunk_concepts (
chunk_id TEXT NOT NULL,
concept_id TEXT NOT NULL,
weight REAL DEFAULT 1.0,
PRIMARY KEY (chunk_id, concept_id),
FOREIGN KEY (chunk_id) REFERENCES chunks(id) ON DELETE CASCADE,
FOREIGN KEY (concept_id) REFERENCES concepts(id) ON DELETE CASCADE
);
-- Usage traces
CREATE TABLE IF NOT EXISTS usage_traces (
id TEXT PRIMARY KEY,
chunk_ids TEXT NOT NULL, -- JSON array
session_id TEXT NOT NULL,
task_type TEXT DEFAULT 'other',
context_summary TEXT NOT NULL,
query TEXT,
outcome TEXT DEFAULT 'partial',
notes TEXT,
timestamp TEXT NOT NULL
);
-- Links (graph edges)
CREATE TABLE IF NOT EXISTS links (
id TEXT PRIMARY KEY,
source_type TEXT NOT NULL,
source_id TEXT NOT NULL,
target_type TEXT NOT NULL,
target_id TEXT NOT NULL,
relation TEXT NOT NULL,
weight REAL DEFAULT 1.0,
created_at TEXT NOT NULL,
created_by TEXT DEFAULT 'auto'
);
-- Embeddings (stored as JSON arrays for simplicity)
CREATE TABLE IF NOT EXISTS embeddings (
chunk_id TEXT PRIMARY KEY,
model_name TEXT NOT NULL,
embedding TEXT NOT NULL, -- JSON array of floats
created_at TEXT NOT NULL,
FOREIGN KEY (chunk_id) REFERENCES chunks(id) ON DELETE CASCADE
);
-- Indexes for common queries
CREATE INDEX IF NOT EXISTS idx_chunks_document ON chunks(document_id);
CREATE INDEX IF NOT EXISTS idx_chunk_concepts_chunk ON chunk_concepts(chunk_id);
CREATE INDEX IF NOT EXISTS idx_chunk_concepts_concept ON chunk_concepts(concept_id);
CREATE INDEX IF NOT EXISTS idx_links_source ON links(source_type, source_id);
CREATE INDEX IF NOT EXISTS idx_links_target ON links(target_type, target_id);
CREATE INDEX IF NOT EXISTS idx_usage_traces_session ON usage_traces(session_id);
CREATE INDEX IF NOT EXISTS idx_usage_traces_task_type ON usage_traces(task_type);
CREATE INDEX IF NOT EXISTS idx_concepts_name ON concepts(name);
CREATE INDEX IF NOT EXISTS idx_source_profiles_domain ON source_profiles(domain);
CREATE INDEX IF NOT EXISTS idx_documents_source_profile ON documents(source_profile_id);
CREATE INDEX IF NOT EXISTS idx_embeddings_model ON embeddings(model_name);
"""
def __init__(self, db_path: str | Path = "knowledge.db"):
self.db_path = Path(db_path)
# check_same_thread=False allows connection to be used across threads
# This is safe when we ensure serialized access (which asyncio.to_thread does)
self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
self.conn.row_factory = sqlite3.Row
self.conn.execute("PRAGMA foreign_keys = ON")
# WAL mode for better concurrent access from multiple processes
self.conn.execute("PRAGMA journal_mode = WAL")
self._init_schema()
def _init_schema(self):
"""Create tables if they don't exist."""
self.conn.executescript(self.SCHEMA)
self.conn.commit()
def close(self):
self.conn.close()
# ------------------------------------------------------------------------
# Document operations
# ------------------------------------------------------------------------
def insert_document(self, doc: Document) -> str:
self.conn.execute("""
INSERT INTO documents (id, source, content_type, title, raw_content,
top_summary, key_claims, ingested_at, last_accessed,
access_count, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
doc.id, doc.source, doc.content_type.value, doc.title, doc.raw_content,
doc.top_summary, json.dumps(doc.key_claims),
doc.ingested_at.isoformat(), doc.last_accessed.isoformat(),
doc.access_count, doc.status.value
))
self.conn.commit()
return doc.id
def get_document(self, doc_id: str) -> Optional[Document]:
row = self.conn.execute(
"SELECT * FROM documents WHERE id = ?", (doc_id,)
).fetchone()
if not row:
return None
return self._row_to_document(row)
def _row_to_document(self, row: sqlite3.Row) -> Document:
return Document(
id=row["id"],
source=row["source"],
content_type=ContentType(row["content_type"]),
title=row["title"],
raw_content=row["raw_content"],
top_summary=row["top_summary"],
key_claims=json.loads(row["key_claims"]) if row["key_claims"] else [],
ingested_at=datetime.fromisoformat(row["ingested_at"]),
last_accessed=datetime.fromisoformat(row["last_accessed"]),
access_count=row["access_count"],
status=DocumentStatus(row["status"])
)
def update_document_access(self, doc_id: str):
"""Update last_accessed and increment access_count."""
self.conn.execute("""
UPDATE documents
SET last_accessed = ?, access_count = access_count + 1
WHERE id = ?
""", (datetime.now(timezone.utc).isoformat(), doc_id))
self.conn.commit()
def delete_document(self, doc_id: str) -> bool:
"""Delete a document and all its chunks (cascades to embeddings, concepts)."""
cursor = self.conn.execute("DELETE FROM documents WHERE id = ?", (doc_id,))
self.conn.commit()
return cursor.rowcount > 0
# ------------------------------------------------------------------------
# Chunk operations
# ------------------------------------------------------------------------
def insert_chunk(self, chunk: Chunk) -> str:
self.conn.execute("""
INSERT INTO chunks (id, document_id, content, position, summary,
chunk_type, token_count)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
chunk.id, chunk.document_id, chunk.content, chunk.position,
chunk.summary, chunk.chunk_type.value, chunk.token_count
))
self.conn.commit()
return chunk.id
def get_chunks_for_document(self, doc_id: str) -> list[Chunk]:
rows = self.conn.execute(
"SELECT * FROM chunks WHERE document_id = ? ORDER BY position",
(doc_id,)
).fetchall()
return [self._row_to_chunk(row) for row in rows]
def get_chunk(self, chunk_id: str) -> Optional[Chunk]:
row = self.conn.execute(
"SELECT * FROM chunks WHERE id = ?", (chunk_id,)
).fetchone()
if not row:
return None
return self._row_to_chunk(row)
def _row_to_chunk(self, row: sqlite3.Row) -> Chunk:
return Chunk(
id=row["id"],
document_id=row["document_id"],
content=row["content"],
position=row["position"],
summary=row["summary"],
chunk_type=ChunkType(row["chunk_type"]),
token_count=row["token_count"],
functional_profile=row["functional_profile"] if "functional_profile" in row.keys() else None,
needs_context=bool(row["needs_context"]) if "needs_context" in row.keys() else False,
rhetorical_mode=RhetoricalMode(row["rhetorical_mode"]) if "rhetorical_mode" in row.keys() and row["rhetorical_mode"] else RhetoricalMode.NEUTRAL
)
# ------------------------------------------------------------------------
# Concept operations
# ------------------------------------------------------------------------
def insert_concept(self, concept: Concept) -> str:
self.conn.execute("""
INSERT INTO concepts (id, name, description, aliases, concept_type,
created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
concept.id, concept.name, concept.description,
json.dumps(concept.aliases), concept.concept_type.value,
concept.created_at.isoformat(), concept.updated_at.isoformat()
))
self.conn.commit()
return concept.id
def get_concept_by_name(self, name: str) -> Optional[Concept]:
row = self.conn.execute(
"SELECT * FROM concepts WHERE name = ?", (name.lower().strip(),)
).fetchone()
if not row:
return None
return self._row_to_concept(row)
def get_or_create_concept(self, name: str, **kwargs) -> Concept:
"""Get existing concept or create new one."""
existing = self.get_concept_by_name(name)
if existing:
return existing
concept = Concept.create(name, **kwargs)
self.insert_concept(concept)
return concept
def _row_to_concept(self, row: sqlite3.Row) -> Concept:
return Concept(
id=row["id"],
name=row["name"],
description=row["description"],
aliases=json.loads(row["aliases"]) if row["aliases"] else [],
concept_type=ConceptType(row["concept_type"]),
functional_profile=row["functional_profile"] if "functional_profile" in row.keys() else None,
gap_notes=row["gap_notes"] if "gap_notes" in row.keys() else None,
created_at=datetime.fromisoformat(row["created_at"]),
updated_at=datetime.fromisoformat(row["updated_at"])
)
def link_chunk_to_concept(self, chunk_id: str, concept_id: str, weight: float = 1.0):
self.conn.execute("""
INSERT OR REPLACE INTO chunk_concepts (chunk_id, concept_id, weight)
VALUES (?, ?, ?)
""", (chunk_id, concept_id, weight))
self.conn.commit()
def get_concepts_for_chunk(self, chunk_id: str) -> list[tuple[Concept, float]]:
rows = self.conn.execute("""
SELECT c.*, cc.weight FROM concepts c
JOIN chunk_concepts cc ON c.id = cc.concept_id
WHERE cc.chunk_id = ?
""", (chunk_id,)).fetchall()
return [(self._row_to_concept(row), row["weight"]) for row in rows]
def get_chunks_for_concept(self, concept_id: str) -> list[tuple[Chunk, float]]:
rows = self.conn.execute("""
SELECT ch.*, cc.weight FROM chunks ch
JOIN chunk_concepts cc ON ch.id = cc.chunk_id
WHERE cc.concept_id = ?
""", (concept_id,)).fetchall()
return [(self._row_to_chunk(row), row["weight"]) for row in rows]
# ------------------------------------------------------------------------
# Usage trace operations
# ------------------------------------------------------------------------
def insert_usage_trace(self, trace: UsageTrace) -> str:
self.conn.execute("""
INSERT INTO usage_traces (id, chunk_ids, session_id, task_type, context_summary,
query, outcome, notes, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
trace.id, json.dumps(trace.chunk_ids), trace.session_id,
trace.task_type.value, trace.context_summary, trace.query,
trace.outcome.value, trace.notes, trace.timestamp.isoformat()
))
self.conn.commit()
return trace.id
def get_usage_traces_for_chunk(self, chunk_id: str) -> list[UsageTrace]:
"""Get all usage traces that include this chunk."""
rows = self.conn.execute(
"SELECT * FROM usage_traces WHERE chunk_ids LIKE ?",
(f'%{chunk_id}%',)
).fetchall()
return [self._row_to_usage_trace(row) for row in rows]
def _row_to_usage_trace(self, row: sqlite3.Row) -> UsageTrace:
return UsageTrace(
id=row["id"],
chunk_ids=json.loads(row["chunk_ids"]),
session_id=row["session_id"],
context_summary=row["context_summary"],
task_type=TaskType(row["task_type"]) if "task_type" in row.keys() and row["task_type"] else TaskType.OTHER,
query=row["query"],
outcome=UsageOutcome(row["outcome"]),
notes=row["notes"],
timestamp=datetime.fromisoformat(row["timestamp"])
)
def get_chunk_success_rate(self, chunk_id: str) -> dict:
"""Calculate success metrics for a chunk."""
traces = self.get_usage_traces_for_chunk(chunk_id)
if not traces:
return {"total": 0, "success_rate": None}
counts = {"win": 0, "partial": 0, "miss": 0, "misleading": 0}
for t in traces:
counts[t.outcome.value] += 1
# Weighted success rate (misleading penalized 2x)
total = sum(counts.values())
success = counts["win"] + 0.5 * counts["partial"]
penalty = counts["miss"] + 2 * counts["misleading"]
rate = success / (success + penalty) if (success + penalty) > 0 else 0.5
return {
"total": total,
"counts": counts,
"success_rate": rate
}
def get_chunk_success_rate_by_task(self, chunk_id: str) -> dict:
"""Calculate success metrics for a chunk, broken down by task type."""
traces = self.get_usage_traces_for_chunk(chunk_id)
if not traces:
return {"total": 0, "by_task": {}}
by_task = {}
for t in traces:
task = t.task_type.value
if task not in by_task:
by_task[task] = {"win": 0, "partial": 0, "miss": 0, "misleading": 0}
by_task[task][t.outcome.value] += 1
# Calculate success rate per task type
for task, counts in by_task.items():
total = sum(counts.values())
success = counts["win"] + 0.5 * counts["partial"]
penalty = counts["miss"] + 2 * counts["misleading"]
by_task[task]["success_rate"] = success / (success + penalty) if (success + penalty) > 0 else 0.5
by_task[task]["total"] = total
return {
"total": len(traces),
"by_task": by_task
}
def get_chunk_usage_history(self, chunk_id: str, limit: int = 10) -> list[dict]:
"""
Get formatted usage history for a chunk, suitable for LLM reasoning.
Returns most recent traces with task type and notes.
"""
traces = self.get_usage_traces_for_chunk(chunk_id)
traces.sort(key=lambda t: t.timestamp, reverse=True)
return [
{
"timestamp": t.timestamp.strftime("%Y-%m-%d"),
"task_type": t.task_type.value,
"outcome": t.outcome.value,
"context": t.context_summary,
"notes": t.notes
}
for t in traces[:limit]
]
# ------------------------------------------------------------------------
# Source Profile operations
# ------------------------------------------------------------------------
def insert_source_profile(self, profile: SourceProfile) -> str:
self.conn.execute("""
INSERT INTO source_profiles (id, domain, source_type, functional_profile,
strengths, weaknesses, trace_counts_by_task,
created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
profile.id, profile.domain, profile.source_type.value,
profile.functional_profile, json.dumps(profile.strengths),
json.dumps(profile.weaknesses), json.dumps(profile.trace_counts_by_task),
profile.created_at.isoformat(), profile.updated_at.isoformat()
))
self.conn.commit()
return profile.id
def get_source_profile_by_domain(self, domain: str) -> Optional[SourceProfile]:
row = self.conn.execute(
"SELECT * FROM source_profiles WHERE domain = ?", (domain.lower().strip(),)
).fetchone()
if not row:
return None
return self._row_to_source_profile(row)
def get_or_create_source_profile(self, domain: str, source_type: SourceType = SourceType.OTHER) -> SourceProfile:
"""Get existing source profile or create new one."""
existing = self.get_source_profile_by_domain(domain)
if existing:
return existing
profile = SourceProfile.create(domain, source_type)
self.insert_source_profile(profile)
return profile
def update_source_profile(self, profile: SourceProfile):
"""Update a source profile (e.g., after consolidation)."""
self.conn.execute("""
UPDATE source_profiles
SET functional_profile = ?, strengths = ?, weaknesses = ?,
trace_counts_by_task = ?, updated_at = ?
WHERE id = ?
""", (
profile.functional_profile, json.dumps(profile.strengths),
json.dumps(profile.weaknesses), json.dumps(profile.trace_counts_by_task),
datetime.now(timezone.utc).isoformat(), profile.id
))
self.conn.commit()
def _row_to_source_profile(self, row: sqlite3.Row) -> SourceProfile:
return SourceProfile(
id=row["id"],
domain=row["domain"],
source_type=SourceType(row["source_type"]),
functional_profile=row["functional_profile"],
strengths=json.loads(row["strengths"]) if row["strengths"] else [],
weaknesses=json.loads(row["weaknesses"]) if row["weaknesses"] else [],
trace_counts_by_task=json.loads(row["trace_counts_by_task"]) if row["trace_counts_by_task"] else {},
created_at=datetime.fromisoformat(row["created_at"]),
updated_at=datetime.fromisoformat(row["updated_at"])
)
def update_chunk_functional_profile(self, chunk_id: str, profile: str):
"""Update a chunk's functional profile after consolidation."""
self.conn.execute(
"UPDATE chunks SET functional_profile = ? WHERE id = ?",
(profile, chunk_id)
)
self.conn.commit()
def update_concept_functional_profile(self, concept_id: str, profile: str, gap_notes: str = None):
"""Update a concept's functional profile and gap notes."""
self.conn.execute(
"UPDATE concepts SET functional_profile = ?, gap_notes = ?, updated_at = ? WHERE id = ?",
(profile, gap_notes, datetime.now(timezone.utc).isoformat(), concept_id)
)
self.conn.commit()
# ------------------------------------------------------------------------
# Link operations
# ------------------------------------------------------------------------
def insert_link(self, link: Link) -> str:
self.conn.execute("""
INSERT INTO links (id, source_type, source_id, target_type, target_id,
relation, weight, created_at, created_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
link.id, link.source_type.value, link.source_id,
link.target_type.value, link.target_id, link.relation,
link.weight, link.created_at.isoformat(), link.created_by.value
))
self.conn.commit()
return link.id
def get_links_from(self, entity_type: EntityType, entity_id: str) -> list[Link]:
rows = self.conn.execute("""
SELECT * FROM links WHERE source_type = ? AND source_id = ?
""", (entity_type.value, entity_id)).fetchall()
return [self._row_to_link(row) for row in rows]
def get_links_to(self, entity_type: EntityType, entity_id: str) -> list[Link]:
rows = self.conn.execute("""
SELECT * FROM links WHERE target_type = ? AND target_id = ?
""", (entity_type.value, entity_id)).fetchall()
return [self._row_to_link(row) for row in rows]
def _row_to_link(self, row: sqlite3.Row) -> Link:
return Link(
id=row["id"],
source_type=EntityType(row["source_type"]),
source_id=row["source_id"],
target_type=EntityType(row["target_type"]),
target_id=row["target_id"],
relation=row["relation"],
weight=row["weight"],
created_at=datetime.fromisoformat(row["created_at"]),
created_by=LinkCreator(row["created_by"])
)
# ------------------------------------------------------------------------
# Query helpers
# ------------------------------------------------------------------------
def search_documents(self, query: str, limit: int = 10) -> list[Document]:
"""Simple text search across documents."""
rows = self.conn.execute("""
SELECT * FROM documents
WHERE title LIKE ? OR raw_content LIKE ? OR top_summary LIKE ?
ORDER BY last_accessed DESC
LIMIT ?
""", (f'%{query}%', f'%{query}%', f'%{query}%', limit)).fetchall()
return [self._row_to_document(row) for row in rows]
def search_chunks(self, query: str, limit: int = 20) -> list[Chunk]:
"""Simple text search across chunks."""
rows = self.conn.execute("""
SELECT * FROM chunks
WHERE content LIKE ? OR summary LIKE ?
LIMIT ?
""", (f'%{query}%', f'%{query}%', limit)).fetchall()
return [self._row_to_chunk(row) for row in rows]
def get_all_concepts(self) -> list[Concept]:
rows = self.conn.execute("SELECT * FROM concepts ORDER BY name").fetchall()
return [self._row_to_concept(row) for row in rows]
def stats(self) -> dict:
"""Get basic statistics about the knowledge base."""
return {
"documents": self.conn.execute("SELECT COUNT(*) FROM documents").fetchone()[0],
"chunks": self.conn.execute("SELECT COUNT(*) FROM chunks").fetchone()[0],
"concepts": self.conn.execute("SELECT COUNT(*) FROM concepts").fetchone()[0],
"source_profiles": self.conn.execute("SELECT COUNT(*) FROM source_profiles").fetchone()[0],
"usage_traces": self.conn.execute("SELECT COUNT(*) FROM usage_traces").fetchone()[0],
"links": self.conn.execute("SELECT COUNT(*) FROM links").fetchone()[0],
"embeddings": self.conn.execute("SELECT COUNT(*) FROM embeddings").fetchone()[0],
}
# ------------------------------------------------------------------------
# Embedding operations
# ------------------------------------------------------------------------
def store_embedding(self, chunk_id: str, embedding: list[float], model_name: str):
"""Store an embedding for a chunk."""
self.conn.execute("""
INSERT OR REPLACE INTO embeddings (chunk_id, model_name, embedding, created_at)
VALUES (?, ?, ?, ?)
""", (chunk_id, model_name, json.dumps(embedding), datetime.now(timezone.utc).isoformat()))
self.conn.commit()
def get_embedding(self, chunk_id: str) -> Optional[list[float]]:
"""Get the embedding for a chunk."""
row = self.conn.execute(
"SELECT embedding FROM embeddings WHERE chunk_id = ?", (chunk_id,)
).fetchone()
if not row:
return None
return json.loads(row["embedding"])
def get_all_embeddings(self, model_name: str = None) -> list[tuple[str, list[float]]]:
"""Get all embeddings, optionally filtered by model."""
if model_name:
rows = self.conn.execute(
"SELECT chunk_id, embedding FROM embeddings WHERE model_name = ?",
(model_name,)
).fetchall()
else:
rows = self.conn.execute(
"SELECT chunk_id, embedding FROM embeddings"
).fetchall()
return [(row["chunk_id"], json.loads(row["embedding"])) for row in rows]
def get_chunks_without_embeddings(self, model_name: str) -> list[Chunk]:
"""Find chunks that don't have embeddings for the given model."""
rows = self.conn.execute("""
SELECT c.* FROM chunks c
LEFT JOIN embeddings e ON c.id = e.chunk_id AND e.model_name = ?
WHERE e.chunk_id IS NULL
""", (model_name,)).fetchall()
return [self._row_to_chunk(row) for row in rows]
def delete_embeddings_for_model(self, model_name: str) -> int:
"""Delete all embeddings for a specific model (useful when switching models)."""
cursor = self.conn.execute(
"DELETE FROM embeddings WHERE model_name = ?", (model_name,)
)
self.conn.commit()
return cursor.rowcount
# ============================================================================
# Convenience function
# ============================================================================
def init_db(path: str | Path = "knowledge.db") -> KnowledgeDB:
"""Initialize and return a database connection."""
return KnowledgeDB(path)
if __name__ == "__main__":
# Quick sanity check
db = init_db(":memory:")
print("Schema initialized successfully")
print(f"Stats: {db.stats()}")
db.close()