Navegador

navegador / navegador / context / loader.py
Source Blame History 376 lines
5e4b8e4… anonymous 1 """
4be3595… lmata 2 ContextLoader — builds structured context bundles from the navegador graph.
4be3595… lmata 3
4be3595… lmata 4 Operates across both layers:
4be3595… lmata 5 CODE — files, functions, classes, call graphs, decorators, references
4be3595… lmata 6 KNOWLEDGE — concepts, rules, decisions, wiki pages, domains
4be3595… lmata 7
4be3595… lmata 8 Output can be dict, JSON string, or markdown for direct paste into AI chat.
5e4b8e4… anonymous 9 """
5e4b8e4… anonymous 10
5e4b8e4… anonymous 11 import json
5e4b8e4… anonymous 12 import logging
5e4b8e4… anonymous 13 from dataclasses import dataclass, field
5e4b8e4… anonymous 14 from pathlib import Path
5e4b8e4… anonymous 15 from typing import Any
5e4b8e4… anonymous 16
3c74e66… lmata 17 from navegador.graph import GraphStore, queries
5e4b8e4… anonymous 18
5e4b8e4… anonymous 19 logger = logging.getLogger(__name__)
5e4b8e4… anonymous 20
5e4b8e4… anonymous 21
5e4b8e4… anonymous 22 @dataclass
5e4b8e4… anonymous 23 class ContextNode:
5e4b8e4… anonymous 24 type: str
5e4b8e4… anonymous 25 name: str
4be3595… lmata 26 file_path: str = ""
5e4b8e4… anonymous 27 line_start: int | None = None
5e4b8e4… anonymous 28 docstring: str | None = None
5e4b8e4… anonymous 29 signature: str | None = None
5e4b8e4… anonymous 30 source: str | None = None
4be3595… lmata 31 description: str | None = None
4be3595… lmata 32 domain: str | None = None
4be3595… lmata 33 status: str | None = None
ece88ca… lmata 34 rationale: str | None = None
ece88ca… lmata 35 alternatives: str | None = None
ece88ca… lmata 36 date: str | None = None
5e4b8e4… anonymous 37
5e4b8e4… anonymous 38
5e4b8e4… anonymous 39 @dataclass
5e4b8e4… anonymous 40 class ContextBundle:
5e4b8e4… anonymous 41 target: ContextNode
5e4b8e4… anonymous 42 nodes: list[ContextNode] = field(default_factory=list)
5e4b8e4… anonymous 43 edges: list[dict[str, str]] = field(default_factory=list)
5e4b8e4… anonymous 44 metadata: dict[str, Any] = field(default_factory=dict)
5e4b8e4… anonymous 45
5e4b8e4… anonymous 46 def to_dict(self) -> dict[str, Any]:
5e4b8e4… anonymous 47 return {
5e4b8e4… anonymous 48 "target": vars(self.target),
5e4b8e4… anonymous 49 "nodes": [vars(n) for n in self.nodes],
5e4b8e4… anonymous 50 "edges": self.edges,
5e4b8e4… anonymous 51 "metadata": self.metadata,
5e4b8e4… anonymous 52 }
5e4b8e4… anonymous 53
5e4b8e4… anonymous 54 def to_json(self, indent: int = 2) -> str:
5e4b8e4… anonymous 55 return json.dumps(self.to_dict(), indent=indent)
5e4b8e4… anonymous 56
5e4b8e4… anonymous 57 def to_markdown(self) -> str:
5e4b8e4… anonymous 58 lines = [
5e4b8e4… anonymous 59 f"# Context: `{self.target.name}`",
5e4b8e4… anonymous 60 f"**Type:** {self.target.type}",
5e4b8e4… anonymous 61 ]
4be3595… lmata 62 if self.target.file_path:
4be3595… lmata 63 lines.append(f"**File:** `{self.target.file_path}`")
4be3595… lmata 64 if self.target.domain:
4be3595… lmata 65 lines.append(f"**Domain:** {self.target.domain}")
4be3595… lmata 66 if self.target.status:
4be3595… lmata 67 lines.append(f"**Status:** {self.target.status}")
4be3595… lmata 68 if self.target.docstring or self.target.description:
4be3595… lmata 69 lines += ["", f"> {self.target.docstring or self.target.description}"]
5e4b8e4… anonymous 70 if self.target.signature:
5e4b8e4… anonymous 71 lines += ["", f"```python\n{self.target.signature}\n```"]
5e4b8e4… anonymous 72
5e4b8e4… anonymous 73 if self.nodes:
5e4b8e4… anonymous 74 lines += ["", "## Related nodes", ""]
5e4b8e4… anonymous 75 for node in self.nodes:
4be3595… lmata 76 loc = f"`{node.file_path}`" if node.file_path else ""
4be3595… lmata 77 lines.append(f"- **{node.type}** `{node.name}` {loc}".strip())
4be3595… lmata 78 summary = node.docstring or node.description
4be3595… lmata 79 if summary:
4be3595… lmata 80 lines.append(f" > {summary}")
5e4b8e4… anonymous 81
5e4b8e4… anonymous 82 if self.edges:
5e4b8e4… anonymous 83 lines += ["", "## Relationships", ""]
5e4b8e4… anonymous 84 for edge in self.edges:
5e4b8e4… anonymous 85 lines.append(f"- `{edge['from']}` **{edge['type']}** `{edge['to']}`")
5e4b8e4… anonymous 86
5e4b8e4… anonymous 87 return "\n".join(lines)
5e4b8e4… anonymous 88
5e4b8e4… anonymous 89
5e4b8e4… anonymous 90 class ContextLoader:
5e4b8e4… anonymous 91 """
4be3595… lmata 92 Loads context bundles from the navegador graph — code and knowledge layers.
5e4b8e4… anonymous 93
5e4b8e4… anonymous 94 Usage:
5e4b8e4… anonymous 95 store = GraphStore.sqlite()
5e4b8e4… anonymous 96 loader = ContextLoader(store)
5e4b8e4… anonymous 97
5e4b8e4… anonymous 98 bundle = loader.load_file("src/auth.py")
4be3595… lmata 99 bundle = loader.load_function("validate_token")
4be3595… lmata 100 bundle = loader.load_class("AuthService")
4be3595… lmata 101 bundle = loader.explain("validate_token") # full picture
4be3595… lmata 102 bundle = loader.load_concept("JWT")
4be3595… lmata 103 bundle = loader.load_domain("auth")
5e4b8e4… anonymous 104 """
5e4b8e4… anonymous 105
5e4b8e4… anonymous 106 def __init__(self, store: GraphStore) -> None:
5e4b8e4… anonymous 107 self.store = store
5e4b8e4… anonymous 108
4be3595… lmata 109 # ── Code: file ────────────────────────────────────────────────────────────
4be3595… lmata 110
4be3595… lmata 111 def load_file(self, file_path: str) -> ContextBundle:
4be3595… lmata 112 """All symbols in a file and their relationships."""
5e4b8e4… anonymous 113 result = self.store.query(queries.FILE_CONTENTS, {"path": file_path})
5e4b8e4… anonymous 114 target = ContextNode(type="File", name=Path(file_path).name, file_path=file_path)
5e4b8e4… anonymous 115 nodes = []
758c6b7… lmata 116 for row in result.result_set or []:
758c6b7… lmata 117 nodes.append(
758c6b7… lmata 118 ContextNode(
758c6b7… lmata 119 type=row[0] or "Unknown",
758c6b7… lmata 120 name=row[1] or "",
758c6b7… lmata 121 file_path=file_path,
758c6b7… lmata 122 line_start=row[2],
758c6b7… lmata 123 docstring=row[3],
758c6b7… lmata 124 signature=row[4],
758c6b7… lmata 125 )
758c6b7… lmata 126 )
758c6b7… lmata 127 return ContextBundle(target=target, nodes=nodes, metadata={"query": "file_contents"})
4be3595… lmata 128
4be3595… lmata 129 # ── Code: function ────────────────────────────────────────────────────────
e72710e… lmata 130
e72710e… lmata 131 def load_function(self, name: str, file_path: str = "", depth: int = 2) -> ContextBundle:
4be3595… lmata 132 """Callers, callees, decorators — everything touching this function."""
5e4b8e4… anonymous 133 target = ContextNode(type="Function", name=name, file_path=file_path)
5e4b8e4… anonymous 134 nodes: list[ContextNode] = []
5e4b8e4… anonymous 135 edges: list[dict[str, str]] = []
5e4b8e4… anonymous 136
4be3595… lmata 137 params = {"name": name, "file_path": file_path, "depth": depth}
4be3595… lmata 138
4be3595… lmata 139 callees = self.store.query(queries.CALLEES, params)
758c6b7… lmata 140 for row in callees.result_set or []:
5e4b8e4… anonymous 141 nodes.append(ContextNode(type=row[0], name=row[1], file_path=row[2], line_start=row[3]))
5e4b8e4… anonymous 142 edges.append({"from": name, "type": "CALLS", "to": row[1]})
5e4b8e4… anonymous 143
4be3595… lmata 144 callers = self.store.query(queries.CALLERS, params)
758c6b7… lmata 145 for row in callers.result_set or []:
5e4b8e4… anonymous 146 nodes.append(ContextNode(type=row[0], name=row[1], file_path=row[2], line_start=row[3]))
5e4b8e4… anonymous 147 edges.append({"from": row[1], "type": "CALLS", "to": name})
5e4b8e4… anonymous 148
4be3595… lmata 149 decorators = self.store.query(
4be3595… lmata 150 queries.DECORATORS_FOR, {"name": name, "file_path": file_path}
4be3595… lmata 151 )
758c6b7… lmata 152 for row in decorators.result_set or []:
4be3595… lmata 153 nodes.append(ContextNode(type="Decorator", name=row[0], file_path=row[1]))
4be3595… lmata 154 edges.append({"from": row[0], "type": "DECORATES", "to": name})
4be3595… lmata 155
758c6b7… lmata 156 return ContextBundle(
758c6b7… lmata 157 target=target,
758c6b7… lmata 158 nodes=nodes,
758c6b7… lmata 159 edges=edges,
758c6b7… lmata 160 metadata={"depth": depth, "query": "function_context"},
758c6b7… lmata 161 )
4be3595… lmata 162
4be3595… lmata 163 # ── Code: class ───────────────────────────────────────────────────────────
5e4b8e4… anonymous 164
e72710e… lmata 165 def load_class(self, name: str, file_path: str = "") -> ContextBundle:
4be3595… lmata 166 """Methods, parent classes, subclasses, references."""
5e4b8e4… anonymous 167 target = ContextNode(type="Class", name=name, file_path=file_path)
5e4b8e4… anonymous 168 nodes: list[ContextNode] = []
5e4b8e4… anonymous 169 edges: list[dict[str, str]] = []
5e4b8e4… anonymous 170
5e4b8e4… anonymous 171 parents = self.store.query(queries.CLASS_HIERARCHY, {"name": name})
758c6b7… lmata 172 for row in parents.result_set or []:
5e4b8e4… anonymous 173 nodes.append(ContextNode(type="Class", name=row[0], file_path=row[1]))
5e4b8e4… anonymous 174 edges.append({"from": name, "type": "INHERITS", "to": row[0]})
5e4b8e4… anonymous 175
5e4b8e4… anonymous 176 subs = self.store.query(queries.SUBCLASSES, {"name": name})
758c6b7… lmata 177 for row in subs.result_set or []:
5e4b8e4… anonymous 178 nodes.append(ContextNode(type="Class", name=row[0], file_path=row[1]))
5e4b8e4… anonymous 179 edges.append({"from": row[0], "type": "INHERITS", "to": name})
5e4b8e4… anonymous 180
4be3595… lmata 181 refs = self.store.query(queries.REFERENCES_TO, {"name": name, "file_path": ""})
758c6b7… lmata 182 for row in refs.result_set or []:
4be3595… lmata 183 nodes.append(ContextNode(type=row[0], name=row[1], file_path=row[2], line_start=row[3]))
4be3595… lmata 184 edges.append({"from": row[1], "type": "REFERENCES", "to": name})
4be3595… lmata 185
758c6b7… lmata 186 return ContextBundle(
758c6b7… lmata 187 target=target, nodes=nodes, edges=edges, metadata={"query": "class_context"}
758c6b7… lmata 188 )
4be3595… lmata 189
4be3595… lmata 190 # ── Universal: explain ────────────────────────────────────────────────────
4be3595… lmata 191
4be3595… lmata 192 def explain(self, name: str, file_path: str = "") -> ContextBundle:
4be3595… lmata 193 """
4be3595… lmata 194 Full picture: all inbound and outbound relationships for any node,
4be3595… lmata 195 across both code and knowledge layers.
4be3595… lmata 196 """
4be3595… lmata 197 params = {"name": name, "file_path": file_path}
4be3595… lmata 198 target = ContextNode(type="Node", name=name, file_path=file_path)
4be3595… lmata 199 nodes: list[ContextNode] = []
4be3595… lmata 200 edges: list[dict[str, str]] = []
4be3595… lmata 201
4be3595… lmata 202 outbound = self.store.query(queries.OUTBOUND, params)
758c6b7… lmata 203 for row in outbound.result_set or []:
4be3595… lmata 204 rel, ntype, nname, npath = row[0], row[1], row[2], row[3]
4be3595… lmata 205 nodes.append(ContextNode(type=ntype, name=nname, file_path=npath))
4be3595… lmata 206 edges.append({"from": name, "type": rel, "to": nname})
4be3595… lmata 207
4be3595… lmata 208 inbound = self.store.query(queries.INBOUND, params)
758c6b7… lmata 209 for row in inbound.result_set or []:
4be3595… lmata 210 rel, ntype, nname, npath = row[0], row[1], row[2], row[3]
4be3595… lmata 211 nodes.append(ContextNode(type=ntype, name=nname, file_path=npath))
4be3595… lmata 212 edges.append({"from": nname, "type": rel, "to": name})
4be3595… lmata 213
758c6b7… lmata 214 return ContextBundle(target=target, nodes=nodes, edges=edges, metadata={"query": "explain"})
4be3595… lmata 215
4be3595… lmata 216 # ── Knowledge: concept ────────────────────────────────────────────────────
4be3595… lmata 217
4be3595… lmata 218 def load_concept(self, name: str) -> ContextBundle:
4be3595… lmata 219 """Concept + governing rules + related concepts + implementing code + wiki pages."""
4be3595… lmata 220 result = self.store.query(queries.CONCEPT_CONTEXT, {"name": name})
4be3595… lmata 221 rows = result.result_set or []
4be3595… lmata 222
4be3595… lmata 223 if not rows:
758c6b7… lmata 224 return ContextBundle(
758c6b7… lmata 225 target=ContextNode(type="Concept", name=name),
758c6b7… lmata 226 metadata={"query": "concept_context", "found": False},
758c6b7… lmata 227 )
4be3595… lmata 228
4be3595… lmata 229 row = rows[0]
4be3595… lmata 230 target = ContextNode(
758c6b7… lmata 231 type="Concept",
758c6b7… lmata 232 name=row[0],
758c6b7… lmata 233 description=row[1],
758c6b7… lmata 234 status=row[2],
758c6b7… lmata 235 domain=row[3],
4be3595… lmata 236 )
4be3595… lmata 237 nodes: list[ContextNode] = []
4be3595… lmata 238 edges: list[dict[str, str]] = []
4be3595… lmata 239
758c6b7… lmata 240 for cname in row[4] or []:
4be3595… lmata 241 nodes.append(ContextNode(type="Concept", name=cname))
4be3595… lmata 242 edges.append({"from": name, "type": "RELATED_TO", "to": cname})
758c6b7… lmata 243 for rname in row[5] or []:
4be3595… lmata 244 nodes.append(ContextNode(type="Rule", name=rname))
4be3595… lmata 245 edges.append({"from": rname, "type": "GOVERNS", "to": name})
758c6b7… lmata 246 for wname in row[6] or []:
4be3595… lmata 247 nodes.append(ContextNode(type="WikiPage", name=wname))
4be3595… lmata 248 edges.append({"from": wname, "type": "DOCUMENTS", "to": name})
758c6b7… lmata 249 for iname in row[7] or []:
4be3595… lmata 250 nodes.append(ContextNode(type="Code", name=iname))
4be3595… lmata 251 edges.append({"from": iname, "type": "IMPLEMENTS", "to": name})
4be3595… lmata 252
758c6b7… lmata 253 return ContextBundle(
758c6b7… lmata 254 target=target, nodes=nodes, edges=edges, metadata={"query": "concept_context"}
758c6b7… lmata 255 )
4be3595… lmata 256
4be3595… lmata 257 # ── Knowledge: domain ─────────────────────────────────────────────────────
4be3595… lmata 258
4be3595… lmata 259 def load_domain(self, domain: str) -> ContextBundle:
4be3595… lmata 260 """Everything belonging to a domain — code and knowledge."""
4be3595… lmata 261 result = self.store.query(queries.DOMAIN_CONTENTS, {"domain": domain})
4be3595… lmata 262 target = ContextNode(type="Domain", name=domain)
4be3595… lmata 263 nodes = [
758c6b7… lmata 264 ContextNode(type=row[0], name=row[1], file_path=row[2], description=row[3] or None)
4be3595… lmata 265 for row in (result.result_set or [])
4be3595… lmata 266 ]
758c6b7… lmata 267 return ContextBundle(target=target, nodes=nodes, metadata={"query": "domain_contents"})
4be3595… lmata 268
4be3595… lmata 269 # ── Search ────────────────────────────────────────────────────────────────
5e4b8e4… anonymous 270
5e4b8e4… anonymous 271 def search(self, query: str, limit: int = 20) -> list[ContextNode]:
4be3595… lmata 272 """Search code symbols by name."""
5e4b8e4… anonymous 273 result = self.store.query(queries.SYMBOL_SEARCH, {"query": query, "limit": limit})
5e4b8e4… anonymous 274 return [
758c6b7… lmata 275 ContextNode(
758c6b7… lmata 276 type=row[0], name=row[1], file_path=row[2], line_start=row[3], docstring=row[4]
758c6b7… lmata 277 )
4be3595… lmata 278 for row in (result.result_set or [])
4be3595… lmata 279 ]
4be3595… lmata 280
4be3595… lmata 281 def search_all(self, query: str, limit: int = 20) -> list[ContextNode]:
4be3595… lmata 282 """Search everything — code symbols, concepts, rules, decisions, wiki."""
4be3595… lmata 283 result = self.store.query(queries.GLOBAL_SEARCH, {"query": query, "limit": limit})
4be3595… lmata 284 return [
758c6b7… lmata 285 ContextNode(
758c6b7… lmata 286 type=row[0], name=row[1], file_path=row[2], docstring=row[3], line_start=row[4]
758c6b7… lmata 287 )
4be3595… lmata 288 for row in (result.result_set or [])
4be3595… lmata 289 ]
4be3595… lmata 290
4be3595… lmata 291 def search_by_docstring(self, query: str, limit: int = 20) -> list[ContextNode]:
4be3595… lmata 292 """Search functions/classes whose docstring contains the query."""
4be3595… lmata 293 result = self.store.query(queries.DOCSTRING_SEARCH, {"query": query, "limit": limit})
4be3595… lmata 294 return [
758c6b7… lmata 295 ContextNode(
758c6b7… lmata 296 type=row[0], name=row[1], file_path=row[2], line_start=row[3], docstring=row[4]
ece88ca… lmata 297 )
ece88ca… lmata 298 for row in (result.result_set or [])
ece88ca… lmata 299 ]
ece88ca… lmata 300
ece88ca… lmata 301 # ── Knowledge: decision rationale ────────────────────────────────────────
ece88ca… lmata 302
ece88ca… lmata 303 def load_decision(self, name: str) -> ContextBundle:
ece88ca… lmata 304 """Decision rationale, alternatives, status, and related nodes."""
ece88ca… lmata 305 result = self.store.query(queries.DECISION_RATIONALE, {"name": name})
ece88ca… lmata 306 rows = result.result_set or []
ece88ca… lmata 307
ece88ca… lmata 308 if not rows:
ece88ca… lmata 309 return ContextBundle(
ece88ca… lmata 310 target=ContextNode(type="Decision", name=name),
ece88ca… lmata 311 metadata={"query": "decision_rationale", "found": False},
ece88ca… lmata 312 )
ece88ca… lmata 313
ece88ca… lmata 314 row = rows[0]
ece88ca… lmata 315 target = ContextNode(
ece88ca… lmata 316 type="Decision",
ece88ca… lmata 317 name=row[0],
ece88ca… lmata 318 description=row[1],
ece88ca… lmata 319 status=row[4],
ece88ca… lmata 320 domain=row[6],
ece88ca… lmata 321 )
ece88ca… lmata 322 target.rationale = row[2]
ece88ca… lmata 323 target.alternatives = row[3]
ece88ca… lmata 324 target.date = row[5]
ece88ca… lmata 325
ece88ca… lmata 326 nodes: list[ContextNode] = []
ece88ca… lmata 327 edges: list[dict[str, str]] = []
ece88ca… lmata 328
ece88ca… lmata 329 for tname in row[7] or []:
ece88ca… lmata 330 nodes.append(ContextNode(type="Node", name=tname))
ece88ca… lmata 331 edges.append({"from": name, "type": "DOCUMENTS", "to": tname})
ece88ca… lmata 332 for pname in row[8] or []:
ece88ca… lmata 333 nodes.append(ContextNode(type="Person", name=pname))
ece88ca… lmata 334 edges.append({"from": name, "type": "DECIDED_BY", "to": pname})
ece88ca… lmata 335
ece88ca… lmata 336 return ContextBundle(
ece88ca… lmata 337 target=target, nodes=nodes, edges=edges, metadata={"query": "decision_rationale"}
ece88ca… lmata 338 )
ece88ca… lmata 339
ece88ca… lmata 340 # ── Knowledge: find owners ────────────────────────────────────────────────
ece88ca… lmata 341
ece88ca… lmata 342 def find_owners(self, name: str, file_path: str = "") -> list[ContextNode]:
ece88ca… lmata 343 """Find people assigned to a named node."""
e5e1a3b… lmata 344 result = self.store.query(queries.FIND_OWNERS, {"name": name, "file_path": file_path})
ece88ca… lmata 345 return [
ece88ca… lmata 346 ContextNode(
ece88ca… lmata 347 type="Person",
ece88ca… lmata 348 name=row[2],
ece88ca… lmata 349 description=f"role={row[4]}, team={row[5]}",
ece88ca… lmata 350 )
ece88ca… lmata 351 for row in (result.result_set or [])
ece88ca… lmata 352 ]
ece88ca… lmata 353
ece88ca… lmata 354 # ── Knowledge: search ────────────────────────────────────────────────────
ece88ca… lmata 355
ece88ca… lmata 356 def search_knowledge(self, query: str, limit: int = 20) -> list[ContextNode]:
ece88ca… lmata 357 """Search concepts, rules, decisions, and wiki pages."""
ece88ca… lmata 358 result = self.store.query(queries.KNOWLEDGE_SEARCH, {"query": query, "limit": limit})
ece88ca… lmata 359 return [
ece88ca… lmata 360 ContextNode(
ece88ca… lmata 361 type=row[0],
ece88ca… lmata 362 name=row[1],
ece88ca… lmata 363 description=row[2],
ece88ca… lmata 364 domain=row[3],
ece88ca… lmata 365 status=row[4],
758c6b7… lmata 366 )
4be3595… lmata 367 for row in (result.result_set or [])
4be3595… lmata 368 ]
4be3595… lmata 369
4be3595… lmata 370 def decorated_by(self, decorator_name: str) -> list[ContextNode]:
4be3595… lmata 371 """All functions/methods carrying a given decorator."""
758c6b7… lmata 372 result = self.store.query(queries.DECORATED_BY, {"decorator_name": decorator_name})
4be3595… lmata 373 return [
4be3595… lmata 374 ContextNode(type=row[0], name=row[1], file_path=row[2], line_start=row[3])
5e4b8e4… anonymous 375 for row in (result.result_set or [])
5e4b8e4… anonymous 376 ]

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button