codegraph-engine 2.3.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 (118) hide show
  1. codegraph/__init__.py +1 -1
  2. codegraph/__main__.py +7 -0
  3. codegraph/agent_brain.py +253 -2
  4. codegraph/agent_capabilities.py +1348 -67
  5. codegraph/agent_rules.py +2 -1
  6. codegraph/api_drift.py +455 -0
  7. codegraph/architecture_rules.py +123 -0
  8. codegraph/async_queue/__init__.py +50 -0
  9. codegraph/async_queue/detectors/__init__.py +48 -0
  10. codegraph/async_queue/detectors/celery.py +160 -0
  11. codegraph/async_queue/detectors/kafka.py +168 -0
  12. codegraph/async_queue/detectors/rabbitmq.py +116 -0
  13. codegraph/async_queue/detectors/redis.py +130 -0
  14. codegraph/async_queue/detectors/sqs.py +135 -0
  15. codegraph/async_queue/models.py +161 -0
  16. codegraph/async_queue/otel_models.py +74 -0
  17. codegraph/async_queue/otel_parser.py +337 -0
  18. codegraph/async_queue/reconciler.py +427 -0
  19. codegraph/async_queue/scanner.py +291 -0
  20. codegraph/async_queue/traversal.py +447 -0
  21. codegraph/change_impact.py +320 -110
  22. codegraph/changelog.py +170 -0
  23. codegraph/cli.py +1605 -62
  24. codegraph/cli_output.py +17 -2
  25. codegraph/context.py +135 -24
  26. codegraph/context_freshness.py +2 -0
  27. codegraph/database/__init__.py +14 -0
  28. codegraph/database/extractor.py +712 -17
  29. codegraph/database/interrogation.py +388 -2
  30. codegraph/database/schema_drift.py +862 -0
  31. codegraph/dataflow_propagation.py +118 -0
  32. codegraph/dead_code.py +387 -0
  33. codegraph/epistemic.py +15 -0
  34. codegraph/evidence_contract.py +34 -3
  35. codegraph/federation.py +245 -0
  36. codegraph/frameworks.py +234 -12
  37. codegraph/frameworks_nestjs.py +502 -0
  38. codegraph/frameworks_nextjs.py +337 -0
  39. codegraph/frameworks_react.py +457 -0
  40. codegraph/freshness.py +2 -0
  41. codegraph/frontend_html.py +109 -0
  42. codegraph/git.py +2 -0
  43. codegraph/git_state.py +2 -0
  44. codegraph/graph/traversal.py +150 -7
  45. codegraph/graph_csr.py +218 -0
  46. codegraph/incremental.py +150 -0
  47. codegraph/indexing/__init__.py +7 -1
  48. codegraph/indexing/classifier.py +5 -0
  49. codegraph/indexing/indexer.py +292 -38
  50. codegraph/indexing/languages/__init__.py +1 -0
  51. codegraph/indexing/languages/cpp.py +457 -0
  52. codegraph/indexing/languages/go.py +459 -0
  53. codegraph/indexing/languages/jvm.py +583 -0
  54. codegraph/indexing/languages/rust.py +507 -0
  55. codegraph/indexing/models.py +15 -1
  56. codegraph/indexing/parser.py +385 -9
  57. codegraph/indexing/pool.py +136 -0
  58. codegraph/indexing/protocols.py +220 -0
  59. codegraph/indexing/scanner.py +23 -0
  60. codegraph/indexing/templates.py +418 -0
  61. codegraph/interrogation.py +207 -39
  62. codegraph/lsp.py +318 -0
  63. codegraph/lsp_bridge/__init__.py +16 -0
  64. codegraph/lsp_bridge/bridge.py +102 -0
  65. codegraph/lsp_bridge/client.py +207 -0
  66. codegraph/lsp_bridge/models.py +71 -0
  67. codegraph/mcp/server.py +502 -29
  68. codegraph/memory/trajectory.py +282 -0
  69. codegraph/models.py +7 -0
  70. codegraph/monorepo.py +3 -0
  71. codegraph/multi_agent/__init__.py +13 -0
  72. codegraph/multi_agent/coordinator.py +229 -0
  73. codegraph/multi_agent/models.py +76 -0
  74. codegraph/optimizer.py +42 -5
  75. codegraph/polymorphism.py +95 -0
  76. codegraph/pr_review.py +296 -0
  77. codegraph/process_lifecycle.py +50 -12
  78. codegraph/reachability.py +198 -0
  79. codegraph/reachability_index.py +74 -0
  80. codegraph/refactor/__init__.py +48 -0
  81. codegraph/refactor/models.py +114 -0
  82. codegraph/refactor/patcher.py +348 -0
  83. codegraph/refactor/renamer.py +844 -0
  84. codegraph/refactor/transactions.py +414 -0
  85. codegraph/repair.py +233 -0
  86. codegraph/resolver.py +1357 -161
  87. codegraph/retrieval_policy.py +22 -1
  88. codegraph/roblox.py +2174 -0
  89. codegraph/route_composer.py +7 -0
  90. codegraph/route_topology.py +172 -0
  91. codegraph/runtime/__init__.py +25 -0
  92. codegraph/runtime/collector.py +155 -0
  93. codegraph/runtime/db_watcher.py +191 -0
  94. codegraph/runtime/ingestor.py +30 -10
  95. codegraph/runtime/otel_receiver.py +208 -0
  96. codegraph/runtime/port_inspector.py +304 -0
  97. codegraph/runtime/runner.py +155 -6
  98. codegraph/runtime/trace_linker.py +175 -0
  99. codegraph/sandbox.py +215 -0
  100. codegraph/search/hybrid.py +26 -0
  101. codegraph/security/supply_chain.py +188 -0
  102. codegraph/security/taint.py +421 -0
  103. codegraph/semantic_decorators.py +34 -3
  104. codegraph/structural_diff.py +2 -0
  105. codegraph/styles.py +144 -0
  106. codegraph/symbol_history.py +2 -0
  107. codegraph/test_gen.py +437 -0
  108. codegraph/tsconfig.py +236 -0
  109. codegraph/ui/__init__.py +6 -0
  110. codegraph/ui/server.py +876 -0
  111. codegraph/watcher.py +323 -0
  112. {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/METADATA +365 -67
  113. codegraph_engine-3.0.1.dist-info/RECORD +164 -0
  114. {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/WHEEL +1 -1
  115. codegraph_engine-2.3.0.dist-info/RECORD +0 -94
  116. {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/entry_points.txt +0 -0
  117. {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/licenses/LICENSE +0 -0
  118. {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,348 @@
1
+ """Deterministic, syntax-validated, transaction-safe multi-file semantic patch engine.
2
+
3
+ Invariants:
4
+ 1. Zero disk mutation during planning, dry-run preview, or validation.
5
+ 2. Pre-flight AST and syntax validation (Python, Luau, JSON, JS/TS bracket balancing)
6
+ before any disk write. If ANY file fails validation, the entire transaction is aborted.
7
+ 3. Strict path validation: boundary enforcement and sensitive file redaction.
8
+ 4. Precondition hash checking: verifies files on disk match expected baseline.
9
+ 5. Atomic multi-file commit via sibling temporary files and os.replace() under RefactorLock.
10
+ 6. Persistent rollback manifest allowing deterministic, byte-exact restoration.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import ast
15
+ import difflib
16
+ import hashlib
17
+ import json
18
+ import uuid
19
+ from collections.abc import Sequence
20
+ from dataclasses import dataclass, field
21
+ from pathlib import Path
22
+ from typing import Any
23
+
24
+ from codegraph.errors import SecurityError
25
+ from codegraph.refactor.models import (
26
+ RefactorPlan,
27
+ RefactorResult,
28
+ RefactorRisk,
29
+ RefactorStatus,
30
+ )
31
+ from codegraph.refactor.transactions import commit_refactor_transaction
32
+ from codegraph.security.paths import is_sensitive_path, safe_path
33
+
34
+
35
+ @dataclass
36
+ class PatchHunk:
37
+ """A target-replacement hunk within a file."""
38
+
39
+ target: str
40
+ replacement: str
41
+
42
+
43
+ @dataclass
44
+ class FilePatchSpec:
45
+ """Specification of a patch on a single file."""
46
+
47
+ path: str
48
+ hunks: list[PatchHunk] = field(default_factory=list)
49
+ new_content: str | None = None
50
+ expected_hash: str | None = None
51
+
52
+
53
+ def validate_file_syntax(path: str, content: str) -> tuple[bool, str | None]:
54
+ """Validate syntax of proposed content based on file extension.
55
+
56
+ Returns (is_valid, error_message).
57
+ """
58
+ ext = Path(path).suffix.lower()
59
+
60
+ if ext in (".py", ".pyi"):
61
+ try:
62
+ ast.parse(content, filename=path)
63
+ except SyntaxError as e:
64
+ return False, f"Python SyntaxError in '{path}' at line {e.lineno}, col {e.offset}: {e.msg}"
65
+
66
+ elif ext == ".json":
67
+ try:
68
+ json.loads(content)
69
+ except json.JSONDecodeError as e:
70
+ return False, f"JSON DecodeError in '{path}' at line {e.lineno}, col {e.colno}: {e.msg}"
71
+
72
+ elif ext in (".lua", ".luau"):
73
+ # Bounded Luau syntax check: verify deterministic parsing
74
+ try:
75
+ from codegraph.roblox import parse_luau
76
+ _ = parse_luau(content, path)
77
+ except Exception as e:
78
+ return False, f"Luau Syntax/Parse Error in '{path}': {e}"
79
+
80
+ elif ext in (".js", ".jsx", ".ts", ".tsx"):
81
+ # Fast structural delimiter balance check
82
+ stack: list[tuple[str, int]] = []
83
+ pairs = {')': '(', '}': '{', ']': '['}
84
+ lines = content.splitlines()
85
+ in_multiline_comment = False
86
+
87
+ for line_num, line in enumerate(lines, start=1):
88
+ i = 0
89
+ while i < len(line):
90
+ if in_multiline_comment:
91
+ if line[i:i+2] == "*/":
92
+ in_multiline_comment = False
93
+ i += 2
94
+ continue
95
+ i += 1
96
+ continue
97
+
98
+ if line[i:i+2] == "//":
99
+ break
100
+ if line[i:i+2] == "/*":
101
+ in_multiline_comment = True
102
+ i += 2
103
+ continue
104
+
105
+ ch = line[i]
106
+ if ch in ("'", '"', '`'):
107
+ quote = ch
108
+ i += 1
109
+ while i < len(line):
110
+ if line[i] == "\\":
111
+ i += 2
112
+ continue
113
+ if line[i] == quote:
114
+ break
115
+ i += 1
116
+ elif ch in "({[":
117
+ stack.append((ch, line_num))
118
+ elif ch in ")}]":
119
+ if not stack:
120
+ return False, f"JavaScript/TypeScript unbalanced '{ch}' at line {line_num} in '{path}'"
121
+ expected_open, open_line = stack.pop()
122
+ if pairs[ch] != expected_open:
123
+ return False, (
124
+ f"JavaScript/TypeScript mismatched '{ch}' at line {line_num} "
125
+ f"(expected closing for '{expected_open}' opened at line {open_line}) in '{path}'"
126
+ )
127
+ i += 1
128
+
129
+ if stack:
130
+ unclosed, open_line = stack[-1]
131
+ return False, f"JavaScript/TypeScript unclosed '{unclosed}' opened at line {open_line} in '{path}'"
132
+
133
+ return True, None
134
+
135
+
136
+ def apply_hunks_to_content(original_content: str, hunks: list[PatchHunk], path: str) -> str:
137
+ """Deterministically apply target->replacement hunks to content buffer."""
138
+ current = original_content
139
+ for hunk in hunks:
140
+ if not hunk.target:
141
+ raise ValueError(f"Empty target hunk in patch for '{path}'")
142
+ count = current.count(hunk.target)
143
+ if count == 0:
144
+ raise ValueError(
145
+ f"Target hunk not found in '{path}':\n```\n{hunk.target[:100]}\n```"
146
+ )
147
+ if count > 1:
148
+ raise ValueError(
149
+ f"Target hunk is ambiguous (found {count} matches) in '{path}'. Please provide more context."
150
+ )
151
+ current = current.replace(hunk.target, hunk.replacement, 1)
152
+ return current
153
+
154
+
155
+ def plan_semantic_patch(
156
+ repository: Path,
157
+ patch_specs: Sequence[FilePatchSpec | dict[str, Any]],
158
+ ) -> tuple[RefactorPlan | None, dict[str, str], list[str]]:
159
+ """Plan and validate a semantic patch across repository files.
160
+
161
+ Returns:
162
+ (plan, modified_buffers, errors)
163
+ """
164
+ repo_root = repository.resolve()
165
+ errors: list[str] = []
166
+ normalized_specs: list[FilePatchSpec] = []
167
+
168
+ for item in patch_specs:
169
+ if isinstance(item, FilePatchSpec):
170
+ normalized_specs.append(item)
171
+ elif isinstance(item, dict):
172
+ hunks: list[PatchHunk] = []
173
+ for h in item.get("hunks", []):
174
+ hunks.append(PatchHunk(target=h["target"], replacement=h["replacement"]))
175
+ normalized_specs.append(
176
+ FilePatchSpec(
177
+ path=item["path"],
178
+ hunks=hunks,
179
+ new_content=item.get("new_content") or item.get("content"),
180
+ expected_hash=item.get("expected_hash"),
181
+ )
182
+ )
183
+
184
+ if not normalized_specs:
185
+ return None, {}, ["NO_PATCHES_PROVIDED: patch_specs list cannot be empty."]
186
+
187
+ modified_buffers: dict[str, str] = {}
188
+ original_hashes: dict[str, str] = {}
189
+ files: list[str] = []
190
+
191
+ for spec in normalized_specs:
192
+ rel_path = spec.path.replace("\\", "/").strip()
193
+ if not rel_path:
194
+ errors.append("EMPTY_PATH: File path cannot be empty.")
195
+ continue
196
+
197
+ if is_sensitive_path(rel_path):
198
+ errors.append(f"SENSITIVE_FILE: Cannot patch sensitive file '{rel_path}'.")
199
+ continue
200
+
201
+ try:
202
+ abs_path = safe_path(repo_root, rel_path)
203
+ except SecurityError as sec_err:
204
+ errors.append(f"SECURITY_ERROR: {sec_err}")
205
+ continue
206
+
207
+ if not abs_path.exists() or not abs_path.is_file():
208
+ errors.append(f"FILE_NOT_FOUND: Target file '{rel_path}' does not exist.")
209
+ continue
210
+
211
+ try:
212
+ orig_bytes = abs_path.read_bytes()
213
+ orig_content = orig_bytes.decode("utf-8")
214
+ except UnicodeDecodeError:
215
+ errors.append(f"BINARY_FILE: Cannot patch non-UTF8 binary file '{rel_path}'.")
216
+ continue
217
+
218
+ current_hash = hashlib.sha256(orig_bytes).hexdigest()
219
+ if spec.expected_hash and current_hash != spec.expected_hash:
220
+ errors.append(
221
+ f"HASH_MISMATCH: File '{rel_path}' hash ({current_hash[:12]}...) "
222
+ f"does not match expected ({spec.expected_hash[:12]}...)."
223
+ )
224
+ continue
225
+
226
+ # Compute modified content
227
+ try:
228
+ if spec.new_content is not None:
229
+ mod_content = spec.new_content
230
+ elif spec.hunks:
231
+ mod_content = apply_hunks_to_content(orig_content, spec.hunks, rel_path)
232
+ else:
233
+ errors.append(f"NO_CHANGES_SPECIFIED: File '{rel_path}' has neither hunks nor new_content.")
234
+ continue
235
+ except ValueError as err:
236
+ errors.append(str(err))
237
+ continue
238
+
239
+ # Validate syntax
240
+ is_valid, syn_err = validate_file_syntax(rel_path, mod_content)
241
+ if not is_valid and syn_err:
242
+ errors.append(f"SYNTAX_VALIDATION_FAILED: {syn_err}")
243
+ continue
244
+
245
+ files.append(rel_path)
246
+ original_hashes[rel_path] = current_hash
247
+ modified_buffers[rel_path] = mod_content
248
+
249
+ if errors:
250
+ return None, {}, errors
251
+
252
+ tx_id = f"patch-{uuid.uuid4().hex[:12]}"
253
+ plan = RefactorPlan(
254
+ transaction_id=tx_id,
255
+ target_symbol="semantic_patch",
256
+ new_name="applied_patch",
257
+ files=files,
258
+ risk=RefactorRisk.LOW,
259
+ original_file_hashes=original_hashes,
260
+ )
261
+ return plan, modified_buffers, []
262
+
263
+
264
+ def preview_semantic_patch(
265
+ repository: Path,
266
+ patch_specs: Sequence[FilePatchSpec | dict[str, Any]],
267
+ ) -> RefactorResult:
268
+ """Dry-run preview a semantic patch with syntax validation and unified diffs.
269
+
270
+ Zero disk mutation invariant guaranteed.
271
+ """
272
+ repo_root = repository.resolve()
273
+ plan, modified_buffers, errors = plan_semantic_patch(repo_root, patch_specs)
274
+
275
+ if errors or plan is None:
276
+ return RefactorResult(
277
+ status=RefactorStatus.BLOCKED if any("SECURITY" in e or "SENSITIVE" in e for e in errors) else RefactorStatus.ERROR,
278
+ target_symbol="semantic_patch",
279
+ new_name="",
280
+ risk=RefactorRisk.HIGH,
281
+ errors=errors,
282
+ )
283
+
284
+ # Generate unified diffs
285
+ diffs: dict[str, str] = {}
286
+ for rel_path, new_text in modified_buffers.items():
287
+ abs_path = safe_path(repo_root, rel_path)
288
+ orig_text = abs_path.read_text(encoding="utf-8")
289
+ orig_lines = orig_text.splitlines(keepends=True)
290
+ new_lines = new_text.splitlines(keepends=True)
291
+ diff = difflib.unified_diff(
292
+ orig_lines,
293
+ new_lines,
294
+ fromfile=f"a/{rel_path}",
295
+ tofile=f"b/{rel_path}",
296
+ )
297
+ diffs[rel_path] = "".join(diff)
298
+
299
+ return RefactorResult(
300
+ status=RefactorStatus.READY,
301
+ target_symbol="semantic_patch",
302
+ new_name="",
303
+ risk=RefactorRisk.LOW,
304
+ files_changed=len(plan.files),
305
+ diffs=diffs,
306
+ )
307
+
308
+
309
+ def apply_semantic_patch(
310
+ repository: Path,
311
+ patch_specs: Sequence[FilePatchSpec | dict[str, Any]],
312
+ dry_run: bool = False,
313
+ ) -> RefactorResult:
314
+ """Atomically apply a semantic patch across files with pre-flight syntax validation and rollback support."""
315
+ repo_root = repository.resolve()
316
+
317
+ if dry_run:
318
+ return preview_semantic_patch(repo_root, patch_specs)
319
+
320
+ plan, modified_buffers, errors = plan_semantic_patch(repo_root, patch_specs)
321
+ if errors or plan is None:
322
+ return RefactorResult(
323
+ status=RefactorStatus.BLOCKED if any("SECURITY" in e or "SENSITIVE" in e for e in errors) else RefactorStatus.ERROR,
324
+ target_symbol="semantic_patch",
325
+ new_name="",
326
+ risk=RefactorRisk.HIGH,
327
+ errors=errors,
328
+ )
329
+
330
+ # Compute diffs for result
331
+ diffs: dict[str, str] = {}
332
+ for rel_path, new_text in modified_buffers.items():
333
+ abs_path = safe_path(repo_root, rel_path)
334
+ orig_text = abs_path.read_text(encoding="utf-8")
335
+ orig_lines = orig_text.splitlines(keepends=True)
336
+ new_lines = new_text.splitlines(keepends=True)
337
+ diff = difflib.unified_diff(
338
+ orig_lines,
339
+ new_lines,
340
+ fromfile=f"a/{rel_path}",
341
+ tofile=f"b/{rel_path}",
342
+ )
343
+ diffs[rel_path] = "".join(diff)
344
+
345
+ # Execute commit under RefactorLock
346
+ result = commit_refactor_transaction(repo_root, plan, modified_buffers)
347
+ result.diffs = diffs
348
+ return result