codegraph-engine 2.1.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 (62) hide show
  1. codegraph/__init__.py +37 -0
  2. codegraph/agent.py +26 -0
  3. codegraph/architecture.py +328 -0
  4. codegraph/audit.py +106 -0
  5. codegraph/cache.py +95 -0
  6. codegraph/cli.py +854 -0
  7. codegraph/config.py +43 -0
  8. codegraph/constraints.py +238 -0
  9. codegraph/context.py +1228 -0
  10. codegraph/epistemic.py +90 -0
  11. codegraph/errors.py +275 -0
  12. codegraph/evidence/__init__.py +15 -0
  13. codegraph/evidence/citations.py +397 -0
  14. codegraph/frameworks.py +434 -0
  15. codegraph/freshness.py +295 -0
  16. codegraph/git.py +278 -0
  17. codegraph/graph/__init__.py +46 -0
  18. codegraph/graph/models.py +41 -0
  19. codegraph/graph/traversal.py +1291 -0
  20. codegraph/indexing/__init__.py +4 -0
  21. codegraph/indexing/classifier.py +274 -0
  22. codegraph/indexing/indexer.py +943 -0
  23. codegraph/indexing/models.py +338 -0
  24. codegraph/indexing/parser.py +1240 -0
  25. codegraph/indexing/scanner.py +200 -0
  26. codegraph/indexing/test_framework.py +116 -0
  27. codegraph/interrogation.py +1582 -0
  28. codegraph/llm/__init__.py +3 -0
  29. codegraph/llm/base.py +15 -0
  30. codegraph/llm/context.py +20 -0
  31. codegraph/mcp/__init__.py +3 -0
  32. codegraph/mcp/server.py +736 -0
  33. codegraph/memory/__init__.py +3 -0
  34. codegraph/memory/store.py +46 -0
  35. codegraph/models.py +289 -0
  36. codegraph/observability.py +151 -0
  37. codegraph/optimizer.py +372 -0
  38. codegraph/planner.py +417 -0
  39. codegraph/py.typed +1 -0
  40. codegraph/query_expansion.py +199 -0
  41. codegraph/ranking.py +363 -0
  42. codegraph/resolver.py +843 -0
  43. codegraph/resources/__init__.py +45 -0
  44. codegraph/resources/cache.py +117 -0
  45. codegraph/resources/coalescer.py +83 -0
  46. codegraph/resources/debouncer.py +98 -0
  47. codegraph/resources/governor.py +232 -0
  48. codegraph/resources/policy.py +123 -0
  49. codegraph/retrieval_policy.py +220 -0
  50. codegraph/search/__init__.py +23 -0
  51. codegraph/search/hybrid.py +301 -0
  52. codegraph/search/semantic.py +28 -0
  53. codegraph/security/__init__.py +3 -0
  54. codegraph/security/paths.py +35 -0
  55. codegraph/target_resolver.py +348 -0
  56. codegraph/task.py +637 -0
  57. codegraph_engine-2.1.1.dist-info/METADATA +334 -0
  58. codegraph_engine-2.1.1.dist-info/RECORD +62 -0
  59. codegraph_engine-2.1.1.dist-info/WHEEL +5 -0
  60. codegraph_engine-2.1.1.dist-info/entry_points.txt +2 -0
  61. codegraph_engine-2.1.1.dist-info/licenses/LICENSE +21 -0
  62. codegraph_engine-2.1.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,348 @@
1
+ """First-class Target Resolver for CodeGraph MCP v2.1.
2
+
3
+ Resolves raw user-supplied target strings (symbol names, routes, file paths,
4
+ class.method forms) against the indexed repository with an explicit,
5
+ ordered resolution strategy.
6
+
7
+ Resolution order:
8
+ 1. Exact canonical_id match
9
+ 2. Exact qualified_name match
10
+ 3. Exact endpoint / route match (route_path or endpoint_id)
11
+ 4. Module + symbol form (mod.sym → resolve module, then symbol)
12
+ 5. Class.method form (explicit dot-split + join lookup)
13
+ 6. Exact filename match
14
+ 7. Short name (name = raw) — MEDIUM if unique, AMBIGUOUS if multiple
15
+ 8. No match → UNKNOWN
16
+
17
+ Every resolution is source-backed: it only returns what is actually in the
18
+ index. It never fabricates relationships.
19
+ """
20
+ from __future__ import annotations
21
+
22
+ import sqlite3
23
+ from dataclasses import dataclass
24
+ from enum import StrEnum
25
+
26
+ # ---------------------------------------------------------------------------
27
+ # Enumerations
28
+ # ---------------------------------------------------------------------------
29
+
30
+
31
+ class TargetType(StrEnum):
32
+ FILE = "FILE"
33
+ MODULE = "MODULE"
34
+ CLASS = "CLASS"
35
+ FUNCTION = "FUNCTION"
36
+ METHOD = "METHOD"
37
+ API = "API"
38
+ API_ENDPOINT = "API_ENDPOINT"
39
+ TEST = "TEST"
40
+ TYPE = "TYPE"
41
+ CONFIG = "CONFIG"
42
+ EXTERNAL_SERVICE = "EXTERNAL_SERVICE"
43
+ UNKNOWN = "UNKNOWN"
44
+
45
+
46
+ class ResolutionMethod(StrEnum):
47
+ CANONICAL_ID = "CANONICAL_ID"
48
+ EXACT_QUALIFIED = "EXACT_QUALIFIED"
49
+ ROUTE_MATCH = "ROUTE_MATCH"
50
+ MODULE_SYMBOL = "MODULE_SYMBOL"
51
+ CLASS_METHOD = "CLASS_METHOD"
52
+ FILENAME = "FILENAME"
53
+ LEXICAL = "LEXICAL"
54
+ AMBIGUOUS = "AMBIGUOUS"
55
+ UNKNOWN = "UNKNOWN"
56
+
57
+
58
+ # ---------------------------------------------------------------------------
59
+ # Result types
60
+ # ---------------------------------------------------------------------------
61
+
62
+ _KIND_TO_TARGET_TYPE: dict[str, TargetType] = {
63
+ "class": TargetType.CLASS,
64
+ "function": TargetType.FUNCTION,
65
+ "method": TargetType.METHOD,
66
+ "module": TargetType.MODULE,
67
+ "type": TargetType.TYPE,
68
+ "test": TargetType.TEST,
69
+ "config": TargetType.CONFIG,
70
+ "file": TargetType.FILE,
71
+ }
72
+
73
+
74
+ def _kind_to_type(kind: str | None) -> TargetType:
75
+ if not kind:
76
+ return TargetType.UNKNOWN
77
+ return _KIND_TO_TARGET_TYPE.get(kind.lower().strip(), TargetType.UNKNOWN)
78
+
79
+
80
+ @dataclass(frozen=True)
81
+ class TargetResolution:
82
+ schema_version: str
83
+ raw_target: str
84
+ target_type: TargetType
85
+ canonical_id: str | None
86
+ qualified_name: str | None
87
+ file_path: str | None
88
+ line: int | None
89
+ confidence: str # HIGH | MEDIUM | LOW | UNKNOWN
90
+ resolution_method: ResolutionMethod
91
+ alternatives: tuple[dict[str, object], ...] # other possible resolutions
92
+ ambiguity_state: str # CLEAR | ASSUMED | AMBIGUOUS | CONFLICT | UNKNOWN
93
+ evidence_ids: tuple[str, ...] # canonical_ids of evidence sources
94
+
95
+ def is_resolved(self) -> bool:
96
+ return self.confidence in ("HIGH", "MEDIUM") and self.canonical_id is not None
97
+
98
+ def as_dict(self) -> dict[str, object]:
99
+ return {
100
+ "schema_version": self.schema_version,
101
+ "raw_target": self.raw_target,
102
+ "target_type": self.target_type.value,
103
+ "canonical_id": self.canonical_id,
104
+ "qualified_name": self.qualified_name,
105
+ "file_path": self.file_path,
106
+ "line": self.line,
107
+ "confidence": self.confidence,
108
+ "resolution_method": self.resolution_method.value,
109
+ "alternatives": list(self.alternatives),
110
+ "ambiguity_state": self.ambiguity_state,
111
+ "evidence_ids": list(self.evidence_ids),
112
+ }
113
+
114
+
115
+ _SCHEMA_VERSION = "2.1"
116
+
117
+ # ---------------------------------------------------------------------------
118
+ # Core resolution logic
119
+ # ---------------------------------------------------------------------------
120
+
121
+ _SYMBOL_SELECT = (
122
+ "SELECT canonical_id, qualified_name, name, kind, path, start_line "
123
+ "FROM symbols"
124
+ )
125
+
126
+
127
+ def _row_to_resolution(
128
+ row: sqlite3.Row,
129
+ raw: str,
130
+ method: ResolutionMethod,
131
+ ambiguity_state: str = "CLEAR",
132
+ confidence: str = "HIGH",
133
+ alternatives: tuple[dict[str, object], ...] = (),
134
+ ) -> TargetResolution:
135
+ return TargetResolution(
136
+ schema_version=_SCHEMA_VERSION,
137
+ raw_target=raw,
138
+ target_type=_kind_to_type(row["kind"]),
139
+ canonical_id=row["canonical_id"],
140
+ qualified_name=row["qualified_name"],
141
+ file_path=row["path"],
142
+ line=row["start_line"],
143
+ confidence=confidence,
144
+ resolution_method=method,
145
+ alternatives=alternatives,
146
+ ambiguity_state=ambiguity_state,
147
+ evidence_ids=(row["canonical_id"],),
148
+ )
149
+
150
+
151
+ def resolve_target(raw: str, con: sqlite3.Connection) -> TargetResolution:
152
+ """Resolve a single raw target string against the indexed repository.
153
+
154
+ Resolution order:
155
+ 1. Exact canonical_id
156
+ 2. Exact qualified_name
157
+ 3. Route/endpoint exact match
158
+ 4. module.symbol resolution
159
+ 5. Class.method resolution
160
+ 6. Exact filename
161
+ 7. Short name (name=raw)
162
+ 8. UNKNOWN fallback
163
+ """
164
+ clean = raw.strip()
165
+ if not clean:
166
+ return TargetResolution(
167
+ schema_version=_SCHEMA_VERSION,
168
+ raw_target=raw,
169
+ target_type=TargetType.UNKNOWN,
170
+ canonical_id=None,
171
+ qualified_name=None,
172
+ file_path=None,
173
+ line=None,
174
+ confidence="UNKNOWN",
175
+ resolution_method=ResolutionMethod.UNKNOWN,
176
+ alternatives=(),
177
+ ambiguity_state="UNKNOWN",
178
+ evidence_ids=(),
179
+ )
180
+
181
+ # ── 1. Exact canonical_id ────────────────────────────────────────────────
182
+ row = con.execute(
183
+ _SYMBOL_SELECT + " WHERE canonical_id=? LIMIT 1", (clean,)
184
+ ).fetchone()
185
+ if row:
186
+ return _row_to_resolution(row, clean, ResolutionMethod.CANONICAL_ID)
187
+
188
+ # ── 2. Exact qualified_name ──────────────────────────────────────────────
189
+ row = con.execute(
190
+ _SYMBOL_SELECT + " WHERE qualified_name=? LIMIT 1", (clean,)
191
+ ).fetchone()
192
+ if row:
193
+ return _row_to_resolution(row, clean, ResolutionMethod.EXACT_QUALIFIED)
194
+
195
+ # ── 3. Endpoint / route ──────────────────────────────────────────────────
196
+ try:
197
+ route_row = con.execute(
198
+ "SELECT route_path, http_method, handler_name, file_path, line, endpoint_id "
199
+ "FROM framework_routes "
200
+ "WHERE route_path=? OR endpoint_id=? LIMIT 1",
201
+ (clean, clean),
202
+ ).fetchone()
203
+ if route_row:
204
+ return TargetResolution(
205
+ schema_version=_SCHEMA_VERSION,
206
+ raw_target=raw,
207
+ target_type=TargetType.API_ENDPOINT,
208
+ canonical_id=route_row["endpoint_id"] or f"{route_row['http_method']} {route_row['route_path']}",
209
+ qualified_name=route_row["route_path"],
210
+ file_path=route_row["file_path"],
211
+ line=route_row["line"],
212
+ confidence="HIGH",
213
+ resolution_method=ResolutionMethod.ROUTE_MATCH,
214
+ alternatives=(),
215
+ ambiguity_state="CLEAR",
216
+ evidence_ids=(route_row["endpoint_id"] or clean,),
217
+ )
218
+ except sqlite3.OperationalError:
219
+ pass
220
+
221
+ # ── 4. Module.symbol (e.g. "src.services.auth_service.AuthService") ─────
222
+ if "." in clean:
223
+ parts = clean.rsplit(".", 1)
224
+ module_part, sym_part = parts[0], parts[1]
225
+ row = con.execute(
226
+ _SYMBOL_SELECT
227
+ + " WHERE (module=? OR module LIKE ?) AND (name=? OR qualified_name=?) "
228
+ "ORDER BY path, start_line LIMIT 1",
229
+ (module_part, f"%.{module_part}", sym_part, clean),
230
+ ).fetchone()
231
+ if row:
232
+ return _row_to_resolution(row, clean, ResolutionMethod.MODULE_SYMBOL)
233
+
234
+ # ── 5. Class.method (e.g. "AdminDashboardView.get") ─────────────────────
235
+ if "." in clean:
236
+ class_part, method_part = clean.rsplit(".", 1)
237
+ # Look for parent class, then find method inside it
238
+ class_row = con.execute(
239
+ _SYMBOL_SELECT + " WHERE (name=? OR qualified_name LIKE ?) AND kind='class' LIMIT 1",
240
+ (class_part, f"%.{class_part}"),
241
+ ).fetchone()
242
+ if class_row:
243
+ method_row = con.execute(
244
+ _SYMBOL_SELECT
245
+ + " WHERE name=? AND parent_symbol_id=? LIMIT 1",
246
+ (method_part, class_row["canonical_id"]),
247
+ ).fetchone()
248
+ if method_row:
249
+ return _row_to_resolution(method_row, clean, ResolutionMethod.CLASS_METHOD)
250
+ # fallback: qualified_name contains the class.method pattern
251
+ qrow = con.execute(
252
+ _SYMBOL_SELECT + " WHERE qualified_name LIKE ? LIMIT 1",
253
+ (f"%.{clean}",),
254
+ ).fetchone()
255
+ if qrow:
256
+ return _row_to_resolution(qrow, clean, ResolutionMethod.CLASS_METHOD)
257
+
258
+ # ── 6. Exact filename ────────────────────────────────────────────────────
259
+ try:
260
+ file_row = con.execute(
261
+ "SELECT path FROM files WHERE path=? OR path LIKE ? LIMIT 1",
262
+ (clean, f"%/{clean}"),
263
+ ).fetchone()
264
+ if file_row:
265
+ return TargetResolution(
266
+ schema_version=_SCHEMA_VERSION,
267
+ raw_target=raw,
268
+ target_type=TargetType.FILE,
269
+ canonical_id=file_row["path"],
270
+ qualified_name=file_row["path"],
271
+ file_path=file_row["path"],
272
+ line=None,
273
+ confidence="HIGH",
274
+ resolution_method=ResolutionMethod.FILENAME,
275
+ alternatives=(),
276
+ ambiguity_state="CLEAR",
277
+ evidence_ids=(file_row["path"],),
278
+ )
279
+ except sqlite3.OperationalError:
280
+ pass
281
+
282
+ # ── 7. Short name (name = clean) — ambiguity-aware ──────────────────────
283
+ short_rows = con.execute(
284
+ _SYMBOL_SELECT + " WHERE name=? ORDER BY kind, path, start_line LIMIT 10",
285
+ (clean,),
286
+ ).fetchall()
287
+ if len(short_rows) == 1:
288
+ return _row_to_resolution(
289
+ short_rows[0], clean, ResolutionMethod.LEXICAL,
290
+ ambiguity_state="CLEAR", confidence="MEDIUM",
291
+ )
292
+ if len(short_rows) > 1:
293
+ alternatives = tuple(
294
+ {
295
+ "canonical_id": r["canonical_id"],
296
+ "qualified_name": r["qualified_name"],
297
+ "kind": r["kind"],
298
+ "path": r["path"],
299
+ "line": r["start_line"],
300
+ }
301
+ for r in short_rows
302
+ )
303
+ # Pick the non-test one if unique
304
+ non_test = [r for r in short_rows if "test" not in (r["path"] or "").lower()]
305
+ if len(non_test) == 1:
306
+ return _row_to_resolution(
307
+ non_test[0], clean, ResolutionMethod.LEXICAL,
308
+ ambiguity_state="ASSUMED", confidence="MEDIUM",
309
+ alternatives=alternatives,
310
+ )
311
+ return TargetResolution(
312
+ schema_version=_SCHEMA_VERSION,
313
+ raw_target=raw,
314
+ target_type=_kind_to_type(short_rows[0]["kind"]),
315
+ canonical_id=short_rows[0]["canonical_id"],
316
+ qualified_name=short_rows[0]["qualified_name"],
317
+ file_path=short_rows[0]["path"],
318
+ line=short_rows[0]["start_line"],
319
+ confidence="LOW",
320
+ resolution_method=ResolutionMethod.AMBIGUOUS,
321
+ alternatives=alternatives,
322
+ ambiguity_state="AMBIGUOUS",
323
+ evidence_ids=tuple(r["canonical_id"] for r in short_rows),
324
+ )
325
+
326
+ # ── 8. UNKNOWN ───────────────────────────────────────────────────────────
327
+ return TargetResolution(
328
+ schema_version=_SCHEMA_VERSION,
329
+ raw_target=raw,
330
+ target_type=TargetType.UNKNOWN,
331
+ canonical_id=None,
332
+ qualified_name=None,
333
+ file_path=None,
334
+ line=None,
335
+ confidence="UNKNOWN",
336
+ resolution_method=ResolutionMethod.UNKNOWN,
337
+ alternatives=(),
338
+ ambiguity_state="UNKNOWN",
339
+ evidence_ids=(),
340
+ )
341
+
342
+
343
+ def resolve_targets(
344
+ targets: list[str],
345
+ con: sqlite3.Connection,
346
+ ) -> dict[str, TargetResolution]:
347
+ """Resolve a list of target strings. Returns mapping raw_target -> TargetResolution."""
348
+ return {t: resolve_target(t, con) for t in targets}