knowledge-master 0.3.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/PKG-INFO +1 -1
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/cli.py +70 -0
- knowledge_master-0.4.0/knowledge_master/migrations.py +89 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/parsers/git_repo.py +10 -1
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/store.py +42 -4
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/PKG-INFO +1 -1
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/SOURCES.txt +1 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/pyproject.toml +1 -1
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/LICENSE +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/README.md +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/__init__.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/__main__.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/api.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/chunking.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/connectors.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/embeddings.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/intelligence.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/parsers/__init__.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/parsers/markdown.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/rerank.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/server.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/static_analysis.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/ts_parsers.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/watcher.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master/web.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/dependency_links.txt +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/entry_points.txt +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/requires.txt +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/top_level.txt +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/setup.cfg +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/tests/test_api.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/tests/test_chunking.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/tests/test_cli.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/tests/test_intelligence.py +0 -0
- {knowledge_master-0.3.0 → knowledge_master-0.4.0}/tests/test_static_analysis.py +0 -0
|
@@ -460,5 +460,75 @@ def who_owns(file: str = typer.Argument(..., help="File path to check ownership"
|
|
|
460
460
|
console.print("[dim]Run 'km index <repo>' first to extract ownership.[/]")
|
|
461
461
|
|
|
462
462
|
|
|
463
|
+
@app.command()
|
|
464
|
+
def upgrade():
|
|
465
|
+
"""Upgrade graph schema to the latest version."""
|
|
466
|
+
from .migrations import check_and_migrate, get_schema_version, CURRENT_SCHEMA_VERSION
|
|
467
|
+
|
|
468
|
+
graph = store.get_graph()
|
|
469
|
+
current = get_schema_version(graph)
|
|
470
|
+
console.print(f"[bold]Current schema:[/] v{current}")
|
|
471
|
+
console.print(f"[bold]Target schema:[/] v{CURRENT_SCHEMA_VERSION}")
|
|
472
|
+
|
|
473
|
+
if current == CURRENT_SCHEMA_VERSION:
|
|
474
|
+
console.print("[green]✓ Already up to date[/]")
|
|
475
|
+
return
|
|
476
|
+
|
|
477
|
+
result = check_and_migrate(graph, auto_migrate=True)
|
|
478
|
+
for step in result["steps"]:
|
|
479
|
+
console.print(f" [green]✓[/] {step}")
|
|
480
|
+
console.print(f"\n[green]✓ Upgraded to v{CURRENT_SCHEMA_VERSION}[/]")
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
@app.command()
|
|
484
|
+
def prune(
|
|
485
|
+
older_than: int = typer.Option(30, help="Remove chunks not re-indexed in this many days"),
|
|
486
|
+
dry_run: bool = typer.Option(False, "--dry-run", help="Show what would be removed"),
|
|
487
|
+
):
|
|
488
|
+
"""Remove stale/orphaned data from the knowledge graph."""
|
|
489
|
+
graph = store.get_graph()
|
|
490
|
+
|
|
491
|
+
# Find orphaned chunks (no PART_OF edge)
|
|
492
|
+
orphaned = graph.query("MATCH (c:Chunk) WHERE NOT (c)-[:PART_OF]->() RETURN count(c)")
|
|
493
|
+
orphan_count = orphaned.result_set[0][0] if orphaned.result_set else 0
|
|
494
|
+
|
|
495
|
+
# Find documents with no chunks
|
|
496
|
+
empty_docs = graph.query(
|
|
497
|
+
"MATCH (d:Document) WHERE NOT ()-[:PART_OF]->(d) AND NOT (d)-[:IN_REPO]->() RETURN count(d)"
|
|
498
|
+
)
|
|
499
|
+
empty_count = empty_docs.result_set[0][0] if empty_docs.result_set else 0
|
|
500
|
+
|
|
501
|
+
# Find stale chunks (indexed_at older than threshold)
|
|
502
|
+
# FalkorDB timestamp() returns ms since epoch
|
|
503
|
+
threshold_ms = older_than * 86400 * 1000
|
|
504
|
+
stale = graph.query(
|
|
505
|
+
"""MATCH (c:Chunk)
|
|
506
|
+
WHERE c.indexed_at IS NOT NULL AND (timestamp() - c.indexed_at) > $threshold
|
|
507
|
+
RETURN count(c)""",
|
|
508
|
+
params={"threshold": threshold_ms},
|
|
509
|
+
)
|
|
510
|
+
stale_count = stale.result_set[0][0] if stale.result_set else 0
|
|
511
|
+
|
|
512
|
+
console.print("[bold]Prune analysis:[/]")
|
|
513
|
+
console.print(f" Orphaned chunks (no document link): {orphan_count}")
|
|
514
|
+
console.print(f" Empty documents (no chunks): {empty_count}")
|
|
515
|
+
console.print(f" Stale chunks (>{older_than} days): {stale_count}")
|
|
516
|
+
|
|
517
|
+
if dry_run:
|
|
518
|
+
console.print("\n[yellow]Dry run — nothing removed.[/]")
|
|
519
|
+
return
|
|
520
|
+
|
|
521
|
+
total = 0
|
|
522
|
+
if orphan_count > 0:
|
|
523
|
+
graph.query("MATCH (c:Chunk) WHERE NOT (c)-[:PART_OF]->() DELETE c")
|
|
524
|
+
total += orphan_count
|
|
525
|
+
|
|
526
|
+
if empty_count > 0:
|
|
527
|
+
graph.query("MATCH (d:Document) WHERE NOT ()-[:PART_OF]->(d) AND NOT (d)-[:IN_REPO]->() DELETE d")
|
|
528
|
+
total += empty_count
|
|
529
|
+
|
|
530
|
+
console.print(f"\n[green]✓ Removed {total} stale nodes[/]")
|
|
531
|
+
|
|
532
|
+
|
|
463
533
|
if __name__ == "__main__":
|
|
464
534
|
app()
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Schema versioning and migrations for the knowledge graph."""
|
|
2
|
+
|
|
3
|
+
CURRENT_SCHEMA_VERSION = 4 # v0.4.0
|
|
4
|
+
|
|
5
|
+
# Migration definitions: version -> function that upgrades from previous version
|
|
6
|
+
MIGRATIONS = {}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_schema_version(graph) -> int:
|
|
10
|
+
"""Get current schema version from graph metadata."""
|
|
11
|
+
try:
|
|
12
|
+
result = graph.query("MATCH (m:_Meta {key: 'schema_version'}) RETURN m.value")
|
|
13
|
+
if result.result_set:
|
|
14
|
+
return int(result.result_set[0][0])
|
|
15
|
+
except Exception:
|
|
16
|
+
pass
|
|
17
|
+
return 0 # no version = legacy graph
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def set_schema_version(graph, version: int):
|
|
21
|
+
"""Store schema version in graph metadata."""
|
|
22
|
+
graph.query(
|
|
23
|
+
"MERGE (m:_Meta {key: 'schema_version'}) SET m.value = $version",
|
|
24
|
+
params={"version": version},
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def check_and_migrate(graph, auto_migrate: bool = True) -> dict:
|
|
29
|
+
"""Check schema version and migrate if needed.
|
|
30
|
+
|
|
31
|
+
Returns: {"current": int, "target": int, "migrated": bool, "steps": list}
|
|
32
|
+
"""
|
|
33
|
+
current = get_schema_version(graph)
|
|
34
|
+
target = CURRENT_SCHEMA_VERSION
|
|
35
|
+
|
|
36
|
+
if current == target:
|
|
37
|
+
return {"current": current, "target": target, "migrated": False, "steps": []}
|
|
38
|
+
|
|
39
|
+
if current > target:
|
|
40
|
+
raise RuntimeError(
|
|
41
|
+
f"Graph schema v{current} is newer than this version supports (v{target}). "
|
|
42
|
+
"Please upgrade knowledge-master."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if not auto_migrate:
|
|
46
|
+
raise RuntimeError(
|
|
47
|
+
f"Graph schema v{current} needs migration to v{target}. Run: km upgrade"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
steps = []
|
|
51
|
+
for v in range(current + 1, target + 1):
|
|
52
|
+
migration_fn = MIGRATIONS.get(v)
|
|
53
|
+
if migration_fn:
|
|
54
|
+
migration_fn(graph)
|
|
55
|
+
steps.append(f"v{v-1} → v{v}: {migration_fn.__doc__ or 'applied'}")
|
|
56
|
+
else:
|
|
57
|
+
steps.append(f"v{v-1} → v{v}: no-op (compatible)")
|
|
58
|
+
|
|
59
|
+
set_schema_version(graph, target)
|
|
60
|
+
return {"current": current, "target": target, "migrated": True, "steps": steps}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# --- Migrations ---
|
|
64
|
+
|
|
65
|
+
def _migrate_to_v1(graph):
|
|
66
|
+
"""Add indexed_at timestamp to existing chunks missing it."""
|
|
67
|
+
graph.query("MATCH (c:Chunk) WHERE c.indexed_at IS NULL SET c.indexed_at = timestamp()")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _migrate_to_v2(graph):
|
|
71
|
+
"""Add OWNS relationships from ownership extraction."""
|
|
72
|
+
pass # OWNS edges are created by extract_ownership, no schema change needed
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _migrate_to_v3(graph):
|
|
76
|
+
"""Add lang property to IMPORTS edges and Function nodes."""
|
|
77
|
+
graph.query("MATCH (f:Function) WHERE f.lang IS NULL SET f.lang = 'python'")
|
|
78
|
+
graph.query("MATCH ()-[e:IMPORTS]->() WHERE e.lang IS NULL SET e.lang = 'python'")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _migrate_to_v4(graph):
|
|
82
|
+
"""Add content_hash to Chunk nodes for deduplication."""
|
|
83
|
+
pass # New chunks will have hash, old ones are fine without
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
MIGRATIONS[1] = _migrate_to_v1
|
|
87
|
+
MIGRATIONS[2] = _migrate_to_v2
|
|
88
|
+
MIGRATIONS[3] = _migrate_to_v3
|
|
89
|
+
MIGRATIONS[4] = _migrate_to_v4
|
|
@@ -46,6 +46,8 @@ def index_repo(repo_path: str, graph=None, branch: str = "HEAD", on_progress=Non
|
|
|
46
46
|
tracked = repo.git.ls_files().splitlines()
|
|
47
47
|
indexable = [f for f in tracked if _should_index(f)]
|
|
48
48
|
total = len(indexable)
|
|
49
|
+
indexed_files = []
|
|
50
|
+
failed_files = []
|
|
49
51
|
|
|
50
52
|
with Progress(disable=not sys.stdout.isatty()) as progress:
|
|
51
53
|
task = progress.add_task(f"Indexing {repo_name}", total=total)
|
|
@@ -53,19 +55,26 @@ def index_repo(repo_path: str, graph=None, branch: str = "HEAD", on_progress=Non
|
|
|
53
55
|
full_path = os.path.join(repo_path, filepath)
|
|
54
56
|
try:
|
|
55
57
|
_index_file(graph, full_path, filepath, repo_name, repo)
|
|
58
|
+
indexed_files.append(filepath)
|
|
56
59
|
except Exception as e:
|
|
60
|
+
failed_files.append((filepath, str(e)))
|
|
57
61
|
progress.console.print(f" [yellow]skip {filepath}: {e}[/]")
|
|
58
62
|
progress.advance(task)
|
|
59
63
|
if on_progress:
|
|
60
64
|
on_progress(i + 1, total, filepath)
|
|
61
65
|
|
|
66
|
+
# If more than 50% failed, warn (possible systemic issue)
|
|
67
|
+
if total > 0 and len(failed_files) > total * 0.5:
|
|
68
|
+
import sys as _sys
|
|
69
|
+
print(f"WARNING: {len(failed_files)}/{total} files failed. Possible systemic issue.", file=_sys.stderr)
|
|
70
|
+
|
|
62
71
|
# Run intelligence extraction
|
|
63
72
|
intel = extract_all(repo_path, graph)
|
|
64
73
|
|
|
65
74
|
# Run static analysis (import graph, symbols) — all languages
|
|
66
75
|
static = build_import_graph_all(repo_path, graph)
|
|
67
76
|
|
|
68
|
-
return {"repo": repo_name, "files_indexed":
|
|
77
|
+
return {"repo": repo_name, "files_indexed": len(indexed_files), "files_failed": len(failed_files), "intelligence": intel, "static_analysis": static}
|
|
69
78
|
|
|
70
79
|
|
|
71
80
|
def _should_index(filepath: str) -> bool:
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""FalkorDB graph store - nodes, edges, vector search, and graph traversal."""
|
|
2
2
|
|
|
3
|
+
import hashlib
|
|
4
|
+
|
|
3
5
|
from falkordb import FalkorDB
|
|
4
6
|
|
|
5
7
|
GRAPH_NAME = "knowledge"
|
|
@@ -7,11 +9,35 @@ GRAPH_NAME = "knowledge"
|
|
|
7
9
|
# Vector dimension for nomic-embed-text
|
|
8
10
|
VECTOR_DIM = 768
|
|
9
11
|
|
|
12
|
+
_graph_instance = None
|
|
13
|
+
|
|
10
14
|
|
|
11
15
|
def get_graph(host: str = "localhost", port: int = 6379):
|
|
12
|
-
"""Get FalkorDB graph instance."""
|
|
16
|
+
"""Get FalkorDB graph instance with schema version check."""
|
|
17
|
+
global _graph_instance
|
|
18
|
+
if _graph_instance is not None:
|
|
19
|
+
return _graph_instance
|
|
20
|
+
|
|
13
21
|
db = FalkorDB(host=host, port=port)
|
|
14
|
-
|
|
22
|
+
graph = db.select_graph(GRAPH_NAME)
|
|
23
|
+
|
|
24
|
+
# Check and auto-migrate schema
|
|
25
|
+
from .migrations import check_and_migrate
|
|
26
|
+
check_and_migrate(graph, auto_migrate=True)
|
|
27
|
+
|
|
28
|
+
_graph_instance = graph
|
|
29
|
+
return graph
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def reset_graph_instance():
|
|
33
|
+
"""Reset cached graph instance (for testing)."""
|
|
34
|
+
global _graph_instance
|
|
35
|
+
_graph_instance = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def content_hash(text: str) -> str:
|
|
39
|
+
"""Compute content hash for deduplication."""
|
|
40
|
+
return hashlib.sha256(text.encode()).hexdigest()[:16]
|
|
15
41
|
|
|
16
42
|
|
|
17
43
|
def init_schema(graph):
|
|
@@ -35,20 +61,32 @@ def init_schema(graph):
|
|
|
35
61
|
|
|
36
62
|
|
|
37
63
|
def upsert_chunk(graph, chunk_id: str, text: str, embedding: list[float], metadata: dict):
|
|
38
|
-
"""Insert or update a chunk node with embedding."""
|
|
64
|
+
"""Insert or update a chunk node with embedding. Skips if content unchanged (dedup)."""
|
|
65
|
+
chash = content_hash(text)
|
|
66
|
+
|
|
67
|
+
# Check if chunk exists with same content hash — skip if unchanged
|
|
68
|
+
existing = graph.query(
|
|
69
|
+
"MATCH (c:Chunk {id: $id}) RETURN c.content_hash",
|
|
70
|
+
params={"id": chunk_id},
|
|
71
|
+
)
|
|
72
|
+
if existing.result_set and existing.result_set[0][0] == chash:
|
|
73
|
+
return False # skip — content unchanged
|
|
74
|
+
|
|
39
75
|
graph.query(
|
|
40
76
|
"""MERGE (c:Chunk {id: $id})
|
|
41
77
|
SET c.text = $text, c.embedding = vecf32($embedding),
|
|
42
78
|
c.source = $source, c.source_type = $source_type,
|
|
43
|
-
c.indexed_at = timestamp()""",
|
|
79
|
+
c.content_hash = $hash, c.indexed_at = timestamp()""",
|
|
44
80
|
params={
|
|
45
81
|
"id": chunk_id,
|
|
46
82
|
"text": text,
|
|
47
83
|
"embedding": embedding,
|
|
48
84
|
"source": metadata.get("source", ""),
|
|
49
85
|
"source_type": metadata.get("source_type", ""),
|
|
86
|
+
"hash": chash,
|
|
50
87
|
},
|
|
51
88
|
)
|
|
89
|
+
return True # inserted/updated
|
|
52
90
|
|
|
53
91
|
|
|
54
92
|
def upsert_document(graph, path: str, doc_type: str, metadata: dict):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{knowledge_master-0.3.0 → knowledge_master-0.4.0}/knowledge_master.egg-info/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|