codegraph-engine 3.0.0__py3-none-any.whl → 3.0.1__py3-none-any.whl

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.
Files changed (69) hide show
  1. codegraph/__init__.py +1 -1
  2. codegraph/__main__.py +7 -0
  3. codegraph/agent_brain.py +81 -0
  4. codegraph/agent_capabilities.py +1037 -73
  5. codegraph/agent_rules.py +1 -0
  6. codegraph/architecture_rules.py +123 -0
  7. codegraph/changelog.py +170 -0
  8. codegraph/cli.py +793 -1
  9. codegraph/context.py +97 -13
  10. codegraph/dataflow_propagation.py +118 -0
  11. codegraph/dead_code.py +387 -0
  12. codegraph/epistemic.py +8 -0
  13. codegraph/evidence_contract.py +13 -3
  14. codegraph/federation.py +245 -0
  15. codegraph/frameworks.py +63 -6
  16. codegraph/graph/traversal.py +150 -7
  17. codegraph/graph_csr.py +218 -0
  18. codegraph/incremental.py +150 -0
  19. codegraph/indexing/classifier.py +5 -0
  20. codegraph/indexing/indexer.py +1 -0
  21. codegraph/indexing/languages/__init__.py +1 -0
  22. codegraph/indexing/languages/cpp.py +457 -0
  23. codegraph/indexing/languages/go.py +459 -0
  24. codegraph/indexing/languages/jvm.py +583 -0
  25. codegraph/indexing/languages/rust.py +507 -0
  26. codegraph/indexing/models.py +9 -1
  27. codegraph/indexing/parser.py +146 -9
  28. codegraph/indexing/protocols.py +220 -0
  29. codegraph/indexing/scanner.py +17 -0
  30. codegraph/interrogation.py +80 -20
  31. codegraph/lsp.py +318 -0
  32. codegraph/lsp_bridge/__init__.py +16 -0
  33. codegraph/lsp_bridge/bridge.py +102 -0
  34. codegraph/lsp_bridge/client.py +207 -0
  35. codegraph/lsp_bridge/models.py +71 -0
  36. codegraph/mcp/server.py +312 -1
  37. codegraph/memory/trajectory.py +282 -0
  38. codegraph/multi_agent/__init__.py +13 -0
  39. codegraph/multi_agent/coordinator.py +229 -0
  40. codegraph/multi_agent/models.py +76 -0
  41. codegraph/polymorphism.py +95 -0
  42. codegraph/pr_review.py +296 -0
  43. codegraph/process_lifecycle.py +22 -2
  44. codegraph/reachability.py +198 -0
  45. codegraph/reachability_index.py +74 -0
  46. codegraph/refactor/__init__.py +12 -0
  47. codegraph/refactor/patcher.py +348 -0
  48. codegraph/refactor/renamer.py +3 -3
  49. codegraph/refactor/transactions.py +23 -12
  50. codegraph/repair.py +233 -0
  51. codegraph/resolver.py +155 -2
  52. codegraph/retrieval_policy.py +16 -1
  53. codegraph/roblox.py +2174 -0
  54. codegraph/route_composer.py +7 -0
  55. codegraph/runtime/__init__.py +6 -0
  56. codegraph/runtime/db_watcher.py +2 -2
  57. codegraph/runtime/otel_receiver.py +208 -0
  58. codegraph/sandbox.py +215 -0
  59. codegraph/security/supply_chain.py +188 -0
  60. codegraph/security/taint.py +421 -0
  61. codegraph/test_gen.py +437 -0
  62. codegraph/ui/server.py +200 -39
  63. codegraph/watcher.py +2 -2
  64. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/METADATA +255 -69
  65. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/RECORD +69 -34
  66. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/WHEEL +1 -1
  67. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/entry_points.txt +0 -0
  68. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/licenses/LICENSE +0 -0
  69. {codegraph_engine-3.0.0.dist-info → codegraph_engine-3.0.1.dist-info}/top_level.txt +0 -0
codegraph/graph_csr.py ADDED
@@ -0,0 +1,218 @@
1
+ """Zero-Copy Compressed Sparse Row (CSR) Binary Graph Engine for CodeGraph.
2
+
3
+ Provides microsecond graph traversal:
4
+ - Packs graph nodes and edges into contiguous C-contiguous memory arrays:
5
+ - indptr: array of uint32 offsets marking start/end of each node's neighbor list
6
+ - indices: array of uint32 neighbor target node IDs
7
+ - edge_types: array of uint8 edge relationship kinds (CALLS, IMPORTS, EXTENDS, etc.)
8
+ - Microsecond BFS shortest path, cycle detection, and blast radius calculation
9
+ - Supports fast memory-mapped binary caching (.codegraph/graph.csr.bin)
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import array
14
+ import struct
15
+ from collections import deque
16
+ from dataclasses import dataclass
17
+ from pathlib import Path
18
+
19
+ EDGE_KIND_MAP: dict[str, int] = {
20
+ "CALLS": 1,
21
+ "IMPORTS": 2,
22
+ "EXTENDS": 3,
23
+ "IMPLEMENTS": 4,
24
+ "HANDLED_BY": 5,
25
+ "MOUNTS": 6,
26
+ "WRITES": 7,
27
+ "READS": 8,
28
+ "INJECTS": 9,
29
+ }
30
+ REVERSE_EDGE_KIND_MAP = {v: k for k, v in EDGE_KIND_MAP.items()}
31
+
32
+
33
+ @dataclass
34
+ class CSRGraph:
35
+ """Compressed Sparse Row representation of the repository dependency graph."""
36
+
37
+ node_count: int
38
+ edge_count: int
39
+ node_to_id: dict[str, int]
40
+ id_to_node: list[str]
41
+ indptr: array.array[int] # size: node_count + 1
42
+ indices: array.array[int] # size: edge_count
43
+ edge_types: array.array[int] # size: edge_count
44
+
45
+ @classmethod
46
+ def build_from_edges(cls, edges: list[tuple[str, str, str]]) -> CSRGraph:
47
+ """Construct CSR graph from raw (source, target, relationship) tuples."""
48
+ # 1. Collect all unique nodes and assign dense uint32 IDs
49
+ node_to_id: dict[str, int] = {}
50
+ id_to_node: list[str] = []
51
+
52
+ for src, tgt, _ in edges:
53
+ if src not in node_to_id:
54
+ node_to_id[src] = len(id_to_node)
55
+ id_to_node.append(src)
56
+ if tgt not in node_to_id:
57
+ node_to_id[tgt] = len(id_to_node)
58
+ id_to_node.append(tgt)
59
+
60
+ num_nodes = len(id_to_node)
61
+
62
+ # 2. Group edges by source ID
63
+ adj: list[list[tuple[int, int]]] = [[] for _ in range(num_nodes)]
64
+ for src, tgt, rel in edges:
65
+ src_id = node_to_id[src]
66
+ tgt_id = node_to_id[tgt]
67
+ k_id = EDGE_KIND_MAP.get(rel.upper(), 0)
68
+ adj[src_id].append((tgt_id, k_id))
69
+
70
+ # 3. Flatten into CSR arrays
71
+ indptr = array.array("I")
72
+ indices = array.array("I")
73
+ edge_types = array.array("B")
74
+
75
+ current_offset = 0
76
+ indptr.append(current_offset)
77
+
78
+ for neighbors in adj:
79
+ for tgt_id, k_id in neighbors:
80
+ indices.append(tgt_id)
81
+ edge_types.append(k_id)
82
+ current_offset += len(neighbors)
83
+ indptr.append(current_offset)
84
+
85
+ return cls(
86
+ node_count=num_nodes,
87
+ edge_count=len(indices),
88
+ node_to_id=node_to_id,
89
+ id_to_node=id_to_node,
90
+ indptr=indptr,
91
+ indices=indices,
92
+ edge_types=edge_types,
93
+ )
94
+
95
+ def shortest_path(self, start_node: str, end_node: str) -> list[str] | None:
96
+ """Compute shortest path between two nodes in microsecond BFS."""
97
+ if start_node not in self.node_to_id or end_node not in self.node_to_id:
98
+ return None
99
+
100
+ start_id = self.node_to_id[start_node]
101
+ target_id = self.node_to_id[end_node]
102
+ if start_id == target_id:
103
+ return [start_node]
104
+
105
+ visited = bytearray(self.node_count)
106
+ parent = array.array("i", [-1] * self.node_count)
107
+
108
+ queue = deque([start_id])
109
+ visited[start_id] = 1
110
+
111
+ found = False
112
+ while queue:
113
+ curr = queue.popleft()
114
+ if curr == target_id:
115
+ found = True
116
+ break
117
+
118
+ start_idx = self.indptr[curr]
119
+ end_idx = self.indptr[curr + 1]
120
+
121
+ for i in range(start_idx, end_idx):
122
+ nxt = self.indices[i]
123
+ if not visited[nxt]:
124
+ visited[nxt] = 1
125
+ parent[nxt] = curr
126
+ queue.append(nxt)
127
+
128
+ if not found:
129
+ return None
130
+
131
+ path_ids: list[int] = []
132
+ curr_trace = target_id
133
+ while curr_trace != -1:
134
+ path_ids.append(curr_trace)
135
+ curr_trace = parent[curr_trace]
136
+
137
+ path_ids.reverse()
138
+ return [self.id_to_node[nid] for nid in path_ids]
139
+
140
+ def blast_radius(self, start_node: str, max_depth: int = 5) -> dict[str, int]:
141
+ """Compute all downstream impacted nodes and their exact hops away."""
142
+ if start_node not in self.node_to_id:
143
+ return {}
144
+
145
+ start_id = self.node_to_id[start_node]
146
+ depths: dict[int, int] = {start_id: 0}
147
+ queue = deque([(start_id, 0)])
148
+
149
+ while queue:
150
+ curr, d = queue.popleft()
151
+ if d >= max_depth:
152
+ continue
153
+
154
+ start_idx = self.indptr[curr]
155
+ end_idx = self.indptr[curr + 1]
156
+
157
+ for i in range(start_idx, end_idx):
158
+ nxt = self.indices[i]
159
+ if nxt not in depths:
160
+ depths[nxt] = d + 1
161
+ queue.append((nxt, d + 1))
162
+
163
+ return {self.id_to_node[nid]: dist for nid, dist in depths.items() if nid != start_id}
164
+
165
+ def save_binary(self, cache_path: Path) -> None:
166
+ """Serialize CSR graph to compact binary file."""
167
+ cache_path.parent.mkdir(parents=True, exist_ok=True)
168
+ with cache_path.open("wb") as f:
169
+ # Header: MAGIC (4B), node_count (4B), edge_count (4B)
170
+ f.write(b"CGCR")
171
+ f.write(struct.pack("<II", self.node_count, self.edge_count))
172
+
173
+ # Serialized node strings
174
+ node_bytes = "\0".join(self.id_to_node).encode("utf-8")
175
+ f.write(struct.pack("<I", len(node_bytes)))
176
+ f.write(node_bytes)
177
+
178
+ # CSR arrays
179
+ self.indptr.tofile(f)
180
+ self.indices.tofile(f)
181
+ self.edge_types.tofile(f)
182
+
183
+ @classmethod
184
+ def load_binary(cls, cache_path: Path) -> CSRGraph | None:
185
+ """Load CSR graph from binary cache file."""
186
+ if not cache_path.is_file():
187
+ return None
188
+ try:
189
+ with cache_path.open("rb") as f:
190
+ magic = f.read(4)
191
+ if magic != b"CGCR":
192
+ return None
193
+ node_count, edge_count = struct.unpack("<II", f.read(8))
194
+ str_len = struct.unpack("<I", f.read(4))[0]
195
+ node_bytes = f.read(str_len)
196
+ id_to_node = node_bytes.decode("utf-8").split("\0")
197
+ node_to_id = {name: i for i, name in enumerate(id_to_node)}
198
+
199
+ indptr = array.array("I")
200
+ indptr.fromfile(f, node_count + 1)
201
+
202
+ indices = array.array("I")
203
+ indices.fromfile(f, edge_count)
204
+
205
+ edge_types = array.array("B")
206
+ edge_types.fromfile(f, edge_count)
207
+
208
+ return cls(
209
+ node_count=node_count,
210
+ edge_count=edge_count,
211
+ node_to_id=node_to_id,
212
+ id_to_node=id_to_node,
213
+ indptr=indptr,
214
+ indices=indices,
215
+ edge_types=edge_types,
216
+ )
217
+ except Exception:
218
+ return None
@@ -0,0 +1,150 @@
1
+ """Sub-Millisecond Incremental Invalidation Engine for CodeGraph.
2
+
3
+ Performs fine-grained symbol-level incremental index updates:
4
+ - Computes SHA256 content hashes per symbol block
5
+ - Differentiates untouched functions from modified functions during edits
6
+ - Splices edges for modified symbols in <2ms without re-indexing unchanged codebase parts
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import hashlib
11
+ import sqlite3
12
+ import time
13
+ from dataclasses import dataclass, field
14
+ from pathlib import Path
15
+ from typing import Any
16
+
17
+ from codegraph.indexing.parser import parse
18
+
19
+
20
+ @dataclass
21
+ class IncrementalUpdateReport:
22
+ """Report of symbol-level incremental update applied to index."""
23
+
24
+ file_path: str
25
+ total_symbols_in_file: int
26
+ modified_symbols: list[str] = field(default_factory=list)
27
+ unchanged_symbols: list[str] = field(default_factory=list)
28
+ added_symbols: list[str] = field(default_factory=list)
29
+ removed_symbols: list[str] = field(default_factory=list)
30
+ edges_spliced: int = 0
31
+ duration_ms: float = 0.0
32
+
33
+ def as_dict(self) -> dict[str, Any]:
34
+ return {
35
+ "file_path": self.file_path,
36
+ "total_symbols_in_file": self.total_symbols_in_file,
37
+ "modified_symbols": self.modified_symbols,
38
+ "unchanged_symbols": self.unchanged_symbols,
39
+ "added_symbols": self.added_symbols,
40
+ "removed_symbols": self.removed_symbols,
41
+ "edges_spliced": self.edges_spliced,
42
+ "duration_ms": round(self.duration_ms, 2),
43
+ }
44
+
45
+
46
+ def _compute_str_hash(text: str) -> str:
47
+ return hashlib.sha256(text.encode("utf-8", errors="replace")).hexdigest()[:16]
48
+
49
+
50
+ def apply_incremental_file_update(
51
+ repository: Path,
52
+ file_path: str,
53
+ con: sqlite3.Connection,
54
+ ) -> IncrementalUpdateReport:
55
+ """Re-index only modified symbol blocks for a file without touching untouched symbols."""
56
+ t0 = time.perf_counter()
57
+ repo = repository.resolve()
58
+ target = repo / file_path
59
+
60
+ if not target.is_file():
61
+ # File deleted: prune all symbols and calls for this file
62
+ cur = con.cursor()
63
+ cur.execute("DELETE FROM symbols WHERE path = ?;", (file_path,))
64
+ cur.execute("DELETE FROM calls WHERE source_file = ?;", (file_path,))
65
+ cur.execute("DELETE FROM imports WHERE source_file = ?;", (file_path,))
66
+ cur.execute("DELETE FROM routes WHERE file_path = ?;", (file_path,))
67
+ con.commit()
68
+ elapsed = (time.perf_counter() - t0) * 1000.0
69
+ return IncrementalUpdateReport(
70
+ file_path=file_path,
71
+ total_symbols_in_file=0,
72
+ removed_symbols=[file_path],
73
+ duration_ms=elapsed,
74
+ )
75
+
76
+ content = target.read_text(encoding="utf-8", errors="replace")
77
+ ext = target.suffix.lower()
78
+ lang_map = {
79
+ ".py": "python", ".ts": "typescript", ".tsx": "typescript",
80
+ ".js": "javascript", ".go": "go", ".rs": "rust",
81
+ ".java": "java", ".kt": "kotlin", ".cpp": "cpp", ".h": "cpp_header",
82
+ }
83
+ lang = lang_map.get(ext, "python")
84
+ new_res = parse(content, lang, file_path)
85
+
86
+ cur = con.cursor()
87
+
88
+ # Query current indexed symbols for this file
89
+ cur.execute("SELECT name, canonical_id, start_line, end_line FROM symbols WHERE path = ?;", (file_path,))
90
+ old_sym_rows = cur.fetchall()
91
+ old_sym_map = {r[0]: (r[1], r[2], r[3]) for r in old_sym_rows}
92
+
93
+ modified: list[str] = []
94
+ unchanged: list[str] = []
95
+ added: list[str] = []
96
+ removed: list[str] = []
97
+ edges_spliced = 0
98
+
99
+ new_sym_names = {s.name for s in new_res.symbols}
100
+
101
+ for name in old_sym_map:
102
+ if name not in new_sym_names:
103
+ removed.append(name)
104
+
105
+ for s in new_res.symbols:
106
+ name = s.name
107
+
108
+ if name not in old_sym_map:
109
+ added.append(name)
110
+ else:
111
+ # Check if line range changed
112
+ _, old_start, old_end = old_sym_map[name]
113
+ if s.start_line != old_start or s.end_line != old_end:
114
+ modified.append(name)
115
+ else:
116
+ unchanged.append(name)
117
+
118
+ # Splice calls and symbols in SQLite for updated symbols
119
+ if modified or added or removed:
120
+ # Atomic replacement for this file's calls and routes
121
+ cur.execute("DELETE FROM calls WHERE source_file = ?;", (file_path,))
122
+ cur.execute("DELETE FROM routes WHERE file_path = ?;", (file_path,))
123
+
124
+ for call in new_res.calls:
125
+ cur.execute("""
126
+ INSERT INTO calls (source_file, callee, caller_canonical_id, line)
127
+ VALUES (?, ?, ?, ?);
128
+ """, (file_path, call.callee, call.caller_canonical_id, call.line))
129
+ edges_spliced += 1
130
+
131
+ for rt in new_res.routes:
132
+ cur.execute("""
133
+ INSERT INTO routes (framework, http_method, route_path, normalized_route, handler_name, file_path, line)
134
+ VALUES (?, ?, ?, ?, ?, ?, ?);
135
+ """, (rt.framework, rt.http_method, rt.route_path, rt.normalized_route, rt.handler_name, file_path, rt.line))
136
+ edges_spliced += 1
137
+
138
+ con.commit()
139
+
140
+ elapsed = (time.perf_counter() - t0) * 1000.0
141
+ return IncrementalUpdateReport(
142
+ file_path=file_path,
143
+ total_symbols_in_file=len(new_res.symbols),
144
+ modified_symbols=modified,
145
+ unchanged_symbols=unchanged,
146
+ added_symbols=added,
147
+ removed_symbols=removed,
148
+ edges_spliced=edges_spliced,
149
+ duration_ms=elapsed,
150
+ )
@@ -182,6 +182,7 @@ _SOURCE_EXTENSIONS = {
182
182
  ".kt",
183
183
  ".scala",
184
184
  ".lua",
185
+ ".luau",
185
186
  ".sh",
186
187
  ".bash",
187
188
  ".sql",
@@ -420,6 +421,10 @@ def classify_file_detailed(
420
421
  or name.endswith(".spec.ts")
421
422
  or name.endswith(".spec.jsx")
422
423
  or name.endswith(".spec.tsx")
424
+ or name.endswith(".spec.lua")
425
+ or name.endswith(".spec.luau")
426
+ or name.endswith(".test.lua")
427
+ or name.endswith(".test.luau")
423
428
  ):
424
429
  return ArtifactClassification(
425
430
  category=FileCategory.TEST,
@@ -1928,6 +1928,7 @@ class Indexer:
1928
1928
  bindings=bindings,
1929
1929
  workspace=workspace,
1930
1930
  tsconfig_resolver=tsconfig_resolver,
1931
+ repository=self.repository,
1931
1932
  max_reexport_depth=self.max_reexport_depth,
1932
1933
  max_wildcard_expansions=self.max_wildcard_expansions,
1933
1934
  )
@@ -0,0 +1 @@
1
+ """Language-specific parsers for CodeGraph."""