|
5e4b8e4…
|
anonymous
|
1 |
""" |
|
4be3595…
|
lmata
|
2 |
Navegador CLI — the single interface to your project's knowledge graph. |
|
e72710e…
|
lmata
|
3 |
|
|
4be3595…
|
lmata
|
4 |
CODE: ingest, context, function, class, search, query |
|
4be3595…
|
lmata
|
5 |
KNOWLEDGE: add (concept/rule/decision/person/domain), wiki, annotate, domain |
|
4be3595…
|
lmata
|
6 |
UNIVERSAL: explain, search (spans both layers), stats |
|
5e4b8e4…
|
anonymous
|
7 |
""" |
|
5e4b8e4…
|
anonymous
|
8 |
|
|
5e4b8e4…
|
anonymous
|
9 |
import asyncio |
|
4be3595…
|
lmata
|
10 |
import json |
|
5e4b8e4…
|
anonymous
|
11 |
import logging |
|
5e4b8e4…
|
anonymous
|
12 |
|
|
5e4b8e4…
|
anonymous
|
13 |
import click |
|
5e4b8e4…
|
anonymous
|
14 |
from rich.console import Console |
|
5e4b8e4…
|
anonymous
|
15 |
from rich.table import Table |
|
5e4b8e4…
|
anonymous
|
16 |
|
|
5e4b8e4…
|
anonymous
|
17 |
console = Console() |
|
5e4b8e4…
|
anonymous
|
18 |
|
|
e72710e…
|
lmata
|
19 |
DB_OPTION = click.option( |
|
e72710e…
|
lmata
|
20 |
"--db", default=".navegador/graph.db", show_default=True, help="Graph DB path." |
|
e72710e…
|
lmata
|
21 |
) |
|
e72710e…
|
lmata
|
22 |
FMT_OPTION = click.option( |
|
758c6b7…
|
lmata
|
23 |
"--format", |
|
758c6b7…
|
lmata
|
24 |
"fmt", |
|
e72710e…
|
lmata
|
25 |
type=click.Choice(["markdown", "json"]), |
|
758c6b7…
|
lmata
|
26 |
default="markdown", |
|
758c6b7…
|
lmata
|
27 |
show_default=True, |
|
e72710e…
|
lmata
|
28 |
help="Output format. Use json for agent/pipe consumption.", |
|
e72710e…
|
lmata
|
29 |
) |
|
e72710e…
|
lmata
|
30 |
|
|
5e4b8e4…
|
anonymous
|
31 |
|
|
5e4b8e4…
|
anonymous
|
32 |
def _get_store(db: str): |
|
b663b12…
|
lmata
|
33 |
from navegador.config import DEFAULT_DB_PATH, get_store |
|
758c6b7…
|
lmata
|
34 |
|
|
b663b12…
|
lmata
|
35 |
return get_store(db if db != DEFAULT_DB_PATH else None) |
|
e72710e…
|
lmata
|
36 |
|
|
e72710e…
|
lmata
|
37 |
|
|
e72710e…
|
lmata
|
38 |
def _emit(text: str, fmt: str) -> None: |
|
e72710e…
|
lmata
|
39 |
if fmt == "json": |
|
e72710e…
|
lmata
|
40 |
click.echo(text) |
|
e72710e…
|
lmata
|
41 |
else: |
|
e72710e…
|
lmata
|
42 |
console.print(text) |
|
e72710e…
|
lmata
|
43 |
|
|
e72710e…
|
lmata
|
44 |
|
|
4be3595…
|
lmata
|
45 |
# ── Root group ──────────────────────────────────────────────────────────────── |
|
758c6b7…
|
lmata
|
46 |
|
|
4be3595…
|
lmata
|
47 |
|
|
5e4b8e4…
|
anonymous
|
48 |
@click.group() |
|
5e4b8e4…
|
anonymous
|
49 |
@click.version_option(package_name="navegador") |
|
5e4b8e4…
|
anonymous
|
50 |
def main(): |
|
4be3595…
|
lmata
|
51 |
"""Navegador — project knowledge graph for AI coding agents. |
|
4be3595…
|
lmata
|
52 |
|
|
4be3595…
|
lmata
|
53 |
Combines code structure (AST, call graphs) with business knowledge |
|
4be3595…
|
lmata
|
54 |
(concepts, rules, decisions, wiki) into a single queryable graph. |
|
4be3595…
|
lmata
|
55 |
""" |
|
5e4b8e4…
|
anonymous
|
56 |
logging.basicConfig(level=logging.WARNING) |
|
4be3595…
|
lmata
|
57 |
|
|
4be3595…
|
lmata
|
58 |
|
|
b663b12…
|
lmata
|
59 |
# ── Init ────────────────────────────────────────────────────────────────────── |
|
b663b12…
|
lmata
|
60 |
|
|
758c6b7…
|
lmata
|
61 |
|
|
b663b12…
|
lmata
|
62 |
@main.command() |
|
b663b12…
|
lmata
|
63 |
@click.argument("path", default=".", type=click.Path()) |
|
758c6b7…
|
lmata
|
64 |
@click.option( |
|
758c6b7…
|
lmata
|
65 |
"--redis", |
|
758c6b7…
|
lmata
|
66 |
"redis_url", |
|
758c6b7…
|
lmata
|
67 |
default="", |
|
758c6b7…
|
lmata
|
68 |
help="Redis URL for centralized/production mode (e.g. redis://host:6379).", |
|
758c6b7…
|
lmata
|
69 |
) |
|
c4536b2…
|
lmata
|
70 |
@click.option( |
|
c4536b2…
|
lmata
|
71 |
"--llm-provider", |
|
c4536b2…
|
lmata
|
72 |
default="", |
|
c4536b2…
|
lmata
|
73 |
help="LLM provider (e.g. anthropic, openai, ollama).", |
|
c4536b2…
|
lmata
|
74 |
) |
|
c4536b2…
|
lmata
|
75 |
@click.option("--llm-model", default="", help="LLM model name.") |
|
c4536b2…
|
lmata
|
76 |
@click.option("--cluster", is_flag=True, help="Enable cluster/swarm mode.") |
|
c4536b2…
|
lmata
|
77 |
def init(path: str, redis_url: str, llm_provider: str, llm_model: str, cluster: bool): |
|
b663b12…
|
lmata
|
78 |
"""Initialise navegador in a project directory. |
|
b663b12…
|
lmata
|
79 |
|
|
c4536b2…
|
lmata
|
80 |
Creates .navegador/ (gitignored), writes config.toml with storage, |
|
c4536b2…
|
lmata
|
81 |
LLM, and cluster settings. |
|
b663b12…
|
lmata
|
82 |
|
|
b663b12…
|
lmata
|
83 |
\b |
|
b663b12…
|
lmata
|
84 |
Local SQLite (default — zero infra): |
|
b663b12…
|
lmata
|
85 |
navegador init |
|
b663b12…
|
lmata
|
86 |
|
|
b663b12…
|
lmata
|
87 |
Centralized Redis (production / multi-agent): |
|
b663b12…
|
lmata
|
88 |
navegador init --redis redis://host:6379 |
|
c4536b2…
|
lmata
|
89 |
|
|
c4536b2…
|
lmata
|
90 |
With LLM: |
|
c4536b2…
|
lmata
|
91 |
navegador init --llm-provider anthropic --llm-model claude-sonnet-4-6 |
|
b663b12…
|
lmata
|
92 |
""" |
|
b663b12…
|
lmata
|
93 |
from navegador.config import init_project |
|
b663b12…
|
lmata
|
94 |
|
|
c4536b2…
|
lmata
|
95 |
storage = "redis" if redis_url else "sqlite" |
|
c4536b2…
|
lmata
|
96 |
nav_dir = init_project( |
|
c4536b2…
|
lmata
|
97 |
path, |
|
c4536b2…
|
lmata
|
98 |
storage=storage, |
|
c4536b2…
|
lmata
|
99 |
redis_url=redis_url, |
|
c4536b2…
|
lmata
|
100 |
llm_provider=llm_provider, |
|
c4536b2…
|
lmata
|
101 |
llm_model=llm_model, |
|
c4536b2…
|
lmata
|
102 |
cluster=cluster, |
|
c4536b2…
|
lmata
|
103 |
) |
|
b663b12…
|
lmata
|
104 |
console.print(f"[green]Initialised navegador[/green] → {nav_dir}") |
|
b663b12…
|
lmata
|
105 |
|
|
b663b12…
|
lmata
|
106 |
if redis_url: |
|
b663b12…
|
lmata
|
107 |
console.print( |
|
b663b12…
|
lmata
|
108 |
f"\n[bold]Redis mode:[/bold] set [cyan]NAVEGADOR_REDIS_URL={redis_url}[/cyan] " |
|
b663b12…
|
lmata
|
109 |
"in your environment or CI secrets." |
|
b663b12…
|
lmata
|
110 |
) |
|
b663b12…
|
lmata
|
111 |
else: |
|
b663b12…
|
lmata
|
112 |
console.print( |
|
b663b12…
|
lmata
|
113 |
"\n[bold]Local SQLite mode[/bold] (default). " |
|
b663b12…
|
lmata
|
114 |
"To use a shared Redis graph set [cyan]NAVEGADOR_REDIS_URL[/cyan]." |
|
b663b12…
|
lmata
|
115 |
) |
|
c4536b2…
|
lmata
|
116 |
|
|
c4536b2…
|
lmata
|
117 |
if llm_provider: |
|
c4536b2…
|
lmata
|
118 |
console.print(f"\n[bold]LLM:[/bold] {llm_provider} / {llm_model or '(default)'}") |
|
c4536b2…
|
lmata
|
119 |
|
|
c4536b2…
|
lmata
|
120 |
if cluster: |
|
c4536b2…
|
lmata
|
121 |
console.print("\n[bold]Cluster mode:[/bold] enabled") |
|
df3d6fa…
|
lmata
|
122 |
|
|
b663b12…
|
lmata
|
123 |
console.print("\nNext: [bold]navegador ingest .[/bold]") |
|
b663b12…
|
lmata
|
124 |
|
|
b663b12…
|
lmata
|
125 |
|
|
4be3595…
|
lmata
|
126 |
# ── CODE: ingest ────────────────────────────────────────────────────────────── |
|
758c6b7…
|
lmata
|
127 |
|
|
5e4b8e4…
|
anonymous
|
128 |
|
|
5e4b8e4…
|
anonymous
|
129 |
@main.command() |
|
5e4b8e4…
|
anonymous
|
130 |
@click.argument("repo_path", type=click.Path(exists=True)) |
|
e72710e…
|
lmata
|
131 |
@DB_OPTION |
|
5e4b8e4…
|
anonymous
|
132 |
@click.option("--clear", is_flag=True, help="Clear existing graph before ingesting.") |
|
df3d6fa…
|
lmata
|
133 |
@click.option("--incremental", is_flag=True, help="Only re-parse changed files.") |
|
df3d6fa…
|
lmata
|
134 |
@click.option("--watch", is_flag=True, help="Watch for changes and re-ingest incrementally.") |
|
df3d6fa…
|
lmata
|
135 |
@click.option("--interval", default=2.0, show_default=True, help="Watch poll interval (seconds).") |
|
e72710e…
|
lmata
|
136 |
@click.option("--json", "as_json", is_flag=True, help="Output stats as JSON.") |
|
b3b8664…
|
lmata
|
137 |
@click.option( |
|
b3b8664…
|
lmata
|
138 |
"--redact", |
|
b3b8664…
|
lmata
|
139 |
is_flag=True, |
|
b3b8664…
|
lmata
|
140 |
help="Scan each file for sensitive content and redact before storing in graph nodes.", |
|
b3b8664…
|
lmata
|
141 |
) |
|
33fd0c0…
|
lmata
|
142 |
@click.option( |
|
33fd0c0…
|
lmata
|
143 |
"--monorepo", |
|
33fd0c0…
|
lmata
|
144 |
is_flag=True, |
|
33fd0c0…
|
lmata
|
145 |
help="Detect and ingest as a monorepo workspace (Turborepo, Nx, Yarn, pnpm, Cargo, Go).", |
|
33fd0c0…
|
lmata
|
146 |
) |
|
e5e1a3b…
|
lmata
|
147 |
def ingest( |
|
e5e1a3b…
|
lmata
|
148 |
repo_path: str, |
|
e5e1a3b…
|
lmata
|
149 |
db: str, |
|
e5e1a3b…
|
lmata
|
150 |
clear: bool, |
|
e5e1a3b…
|
lmata
|
151 |
incremental: bool, |
|
e5e1a3b…
|
lmata
|
152 |
watch: bool, |
|
e5e1a3b…
|
lmata
|
153 |
interval: float, |
|
e5e1a3b…
|
lmata
|
154 |
as_json: bool, |
|
e5e1a3b…
|
lmata
|
155 |
redact: bool, |
|
e5e1a3b…
|
lmata
|
156 |
monorepo: bool, |
|
e5e1a3b…
|
lmata
|
157 |
): |
|
4be3595…
|
lmata
|
158 |
"""Ingest a repository's code into the graph (AST + call graph).""" |
|
33fd0c0…
|
lmata
|
159 |
if monorepo: |
|
33fd0c0…
|
lmata
|
160 |
from navegador.monorepo import MonorepoIngester |
|
33fd0c0…
|
lmata
|
161 |
|
|
33fd0c0…
|
lmata
|
162 |
store = _get_store(db) |
|
33fd0c0…
|
lmata
|
163 |
mono_ingester = MonorepoIngester(store) |
|
33fd0c0…
|
lmata
|
164 |
|
|
33fd0c0…
|
lmata
|
165 |
if as_json: |
|
33fd0c0…
|
lmata
|
166 |
stats = mono_ingester.ingest(repo_path, clear=clear) |
|
33fd0c0…
|
lmata
|
167 |
click.echo(json.dumps(stats, indent=2)) |
|
33fd0c0…
|
lmata
|
168 |
else: |
|
33fd0c0…
|
lmata
|
169 |
with console.status(f"[bold]Ingesting monorepo[/bold] {repo_path}..."): |
|
33fd0c0…
|
lmata
|
170 |
stats = mono_ingester.ingest(repo_path, clear=clear) |
|
33fd0c0…
|
lmata
|
171 |
table = Table(title="Monorepo ingestion complete") |
|
33fd0c0…
|
lmata
|
172 |
table.add_column("Metric", style="cyan") |
|
33fd0c0…
|
lmata
|
173 |
table.add_column("Count", justify="right", style="green") |
|
33fd0c0…
|
lmata
|
174 |
for k, v in stats.items(): |
|
33fd0c0…
|
lmata
|
175 |
table.add_row(str(k).capitalize(), str(v)) |
|
33fd0c0…
|
lmata
|
176 |
console.print(table) |
|
33fd0c0…
|
lmata
|
177 |
return |
|
33fd0c0…
|
lmata
|
178 |
|
|
5e4b8e4…
|
anonymous
|
179 |
from navegador.ingestion import RepoIngester |
|
5e4b8e4…
|
anonymous
|
180 |
|
|
5e4b8e4…
|
anonymous
|
181 |
store = _get_store(db) |
|
b3b8664…
|
lmata
|
182 |
ingester = RepoIngester(store, redact=redact) |
|
df3d6fa…
|
lmata
|
183 |
|
|
df3d6fa…
|
lmata
|
184 |
if watch: |
|
df3d6fa…
|
lmata
|
185 |
console.print(f"[bold]Watching[/bold] {repo_path} (interval={interval}s, Ctrl-C to stop)") |
|
df3d6fa…
|
lmata
|
186 |
|
|
df3d6fa…
|
lmata
|
187 |
def _on_cycle(stats): |
|
df3d6fa…
|
lmata
|
188 |
changed = stats["files"] |
|
df3d6fa…
|
lmata
|
189 |
skipped = stats["skipped"] |
|
df3d6fa…
|
lmata
|
190 |
if changed: |
|
e5e1a3b…
|
lmata
|
191 |
console.print(f" [green]{changed} changed[/green], {skipped} unchanged") |
|
df3d6fa…
|
lmata
|
192 |
return True # keep watching |
|
df3d6fa…
|
lmata
|
193 |
|
|
df3d6fa…
|
lmata
|
194 |
try: |
|
df3d6fa…
|
lmata
|
195 |
ingester.watch(repo_path, interval=interval, callback=_on_cycle) |
|
df3d6fa…
|
lmata
|
196 |
except KeyboardInterrupt: |
|
df3d6fa…
|
lmata
|
197 |
console.print("\n[yellow]Watch stopped.[/yellow]") |
|
df3d6fa…
|
lmata
|
198 |
return |
|
e72710e…
|
lmata
|
199 |
|
|
e72710e…
|
lmata
|
200 |
if as_json: |
|
df3d6fa…
|
lmata
|
201 |
stats = ingester.ingest(repo_path, clear=clear, incremental=incremental) |
|
e72710e…
|
lmata
|
202 |
click.echo(json.dumps(stats, indent=2)) |
|
e72710e…
|
lmata
|
203 |
else: |
|
e72710e…
|
lmata
|
204 |
with console.status(f"[bold]Ingesting[/bold] {repo_path}..."): |
|
df3d6fa…
|
lmata
|
205 |
stats = ingester.ingest(repo_path, clear=clear, incremental=incremental) |
|
4be3595…
|
lmata
|
206 |
table = Table(title="Ingestion complete") |
|
e72710e…
|
lmata
|
207 |
table.add_column("Metric", style="cyan") |
|
e72710e…
|
lmata
|
208 |
table.add_column("Count", justify="right", style="green") |
|
e72710e…
|
lmata
|
209 |
for k, v in stats.items(): |
|
e72710e…
|
lmata
|
210 |
table.add_row(k.capitalize(), str(v)) |
|
e72710e…
|
lmata
|
211 |
console.print(table) |
|
4be3595…
|
lmata
|
212 |
|
|
4be3595…
|
lmata
|
213 |
|
|
4be3595…
|
lmata
|
214 |
# ── CODE: context / function / class ───────────────────────────────────────── |
|
5e4b8e4…
|
anonymous
|
215 |
|
|
758c6b7…
|
lmata
|
216 |
|
|
5e4b8e4…
|
anonymous
|
217 |
@main.command() |
|
5e4b8e4…
|
anonymous
|
218 |
@click.argument("file_path") |
|
e72710e…
|
lmata
|
219 |
@DB_OPTION |
|
e72710e…
|
lmata
|
220 |
@FMT_OPTION |
|
5e4b8e4…
|
anonymous
|
221 |
def context(file_path: str, db: str, fmt: str): |
|
e72710e…
|
lmata
|
222 |
"""Load context for a file — all symbols and their relationships.""" |
|
e72710e…
|
lmata
|
223 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
224 |
|
|
4be3595…
|
lmata
|
225 |
bundle = ContextLoader(_get_store(db)).load_file(file_path) |
|
e72710e…
|
lmata
|
226 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
e72710e…
|
lmata
|
227 |
|
|
e72710e…
|
lmata
|
228 |
|
|
e72710e…
|
lmata
|
229 |
@main.command() |
|
e72710e…
|
lmata
|
230 |
@click.argument("name") |
|
4be3595…
|
lmata
|
231 |
@click.option("--file", "file_path", default="", help="Narrow to a specific file.") |
|
4be3595…
|
lmata
|
232 |
@click.option("--depth", default=2, show_default=True) |
|
e72710e…
|
lmata
|
233 |
@DB_OPTION |
|
e72710e…
|
lmata
|
234 |
@FMT_OPTION |
|
e72710e…
|
lmata
|
235 |
def function(name: str, file_path: str, db: str, depth: int, fmt: str): |
|
4be3595…
|
lmata
|
236 |
"""Load context for a function — callers, callees, decorators.""" |
|
e72710e…
|
lmata
|
237 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
238 |
|
|
4be3595…
|
lmata
|
239 |
bundle = ContextLoader(_get_store(db)).load_function(name, file_path=file_path, depth=depth) |
|
e72710e…
|
lmata
|
240 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
e72710e…
|
lmata
|
241 |
|
|
e72710e…
|
lmata
|
242 |
|
|
e72710e…
|
lmata
|
243 |
@main.command("class") |
|
e72710e…
|
lmata
|
244 |
@click.argument("name") |
|
4be3595…
|
lmata
|
245 |
@click.option("--file", "file_path", default="", help="Narrow to a specific file.") |
|
e72710e…
|
lmata
|
246 |
@DB_OPTION |
|
e72710e…
|
lmata
|
247 |
@FMT_OPTION |
|
e72710e…
|
lmata
|
248 |
def class_(name: str, file_path: str, db: str, fmt: str): |
|
4be3595…
|
lmata
|
249 |
"""Load context for a class — methods, inheritance, references.""" |
|
4be3595…
|
lmata
|
250 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
251 |
|
|
4be3595…
|
lmata
|
252 |
bundle = ContextLoader(_get_store(db)).load_class(name, file_path=file_path) |
|
4be3595…
|
lmata
|
253 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
4be3595…
|
lmata
|
254 |
|
|
4be3595…
|
lmata
|
255 |
|
|
4be3595…
|
lmata
|
256 |
# ── UNIVERSAL: explain ──────────────────────────────────────────────────────── |
|
758c6b7…
|
lmata
|
257 |
|
|
4be3595…
|
lmata
|
258 |
|
|
4be3595…
|
lmata
|
259 |
@main.command() |
|
4be3595…
|
lmata
|
260 |
@click.argument("name") |
|
4be3595…
|
lmata
|
261 |
@click.option("--file", "file_path", default="") |
|
4be3595…
|
lmata
|
262 |
@DB_OPTION |
|
4be3595…
|
lmata
|
263 |
@FMT_OPTION |
|
4be3595…
|
lmata
|
264 |
def explain(name: str, file_path: str, db: str, fmt: str): |
|
4be3595…
|
lmata
|
265 |
"""Full picture: all relationships in and out, code and knowledge layers.""" |
|
5e4b8e4…
|
anonymous
|
266 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
267 |
|
|
4be3595…
|
lmata
|
268 |
bundle = ContextLoader(_get_store(db)).explain(name, file_path=file_path) |
|
e72710e…
|
lmata
|
269 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
4be3595…
|
lmata
|
270 |
|
|
4be3595…
|
lmata
|
271 |
|
|
4be3595…
|
lmata
|
272 |
# ── UNIVERSAL: search ───────────────────────────────────────────────────────── |
|
5e4b8e4…
|
anonymous
|
273 |
|
|
758c6b7…
|
lmata
|
274 |
|
|
5e4b8e4…
|
anonymous
|
275 |
@main.command() |
|
5e4b8e4…
|
anonymous
|
276 |
@click.argument("query") |
|
e72710e…
|
lmata
|
277 |
@DB_OPTION |
|
5e4b8e4…
|
anonymous
|
278 |
@click.option("--limit", default=20, show_default=True) |
|
758c6b7…
|
lmata
|
279 |
@click.option( |
|
758c6b7…
|
lmata
|
280 |
"--all", "search_all", is_flag=True, help="Include knowledge layer (concepts, rules, wiki)." |
|
758c6b7…
|
lmata
|
281 |
) |
|
4be3595…
|
lmata
|
282 |
@click.option("--docs", "by_doc", is_flag=True, help="Search docstrings instead of names.") |
|
e72710e…
|
lmata
|
283 |
@FMT_OPTION |
|
4be3595…
|
lmata
|
284 |
def search(query: str, db: str, limit: int, search_all: bool, by_doc: bool, fmt: str): |
|
4be3595…
|
lmata
|
285 |
"""Search symbols, concepts, rules, and wiki pages.""" |
|
5e4b8e4…
|
anonymous
|
286 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
287 |
|
|
e72710e…
|
lmata
|
288 |
loader = ContextLoader(_get_store(db)) |
|
4be3595…
|
lmata
|
289 |
|
|
4be3595…
|
lmata
|
290 |
if by_doc: |
|
4be3595…
|
lmata
|
291 |
results = loader.search_by_docstring(query, limit=limit) |
|
4be3595…
|
lmata
|
292 |
elif search_all: |
|
4be3595…
|
lmata
|
293 |
results = loader.search_all(query, limit=limit) |
|
4be3595…
|
lmata
|
294 |
else: |
|
4be3595…
|
lmata
|
295 |
results = loader.search(query, limit=limit) |
|
e72710e…
|
lmata
|
296 |
|
|
e72710e…
|
lmata
|
297 |
if fmt == "json": |
|
758c6b7…
|
lmata
|
298 |
click.echo( |
|
758c6b7…
|
lmata
|
299 |
json.dumps( |
|
758c6b7…
|
lmata
|
300 |
[ |
|
758c6b7…
|
lmata
|
301 |
{ |
|
758c6b7…
|
lmata
|
302 |
"type": r.type, |
|
758c6b7…
|
lmata
|
303 |
"name": r.name, |
|
758c6b7…
|
lmata
|
304 |
"file_path": r.file_path, |
|
758c6b7…
|
lmata
|
305 |
"line_start": r.line_start, |
|
758c6b7…
|
lmata
|
306 |
"docstring": r.docstring, |
|
758c6b7…
|
lmata
|
307 |
"description": r.description, |
|
758c6b7…
|
lmata
|
308 |
} |
|
758c6b7…
|
lmata
|
309 |
for r in results |
|
758c6b7…
|
lmata
|
310 |
], |
|
758c6b7…
|
lmata
|
311 |
indent=2, |
|
758c6b7…
|
lmata
|
312 |
) |
|
758c6b7…
|
lmata
|
313 |
) |
|
e72710e…
|
lmata
|
314 |
return |
|
5e4b8e4…
|
anonymous
|
315 |
|
|
5e4b8e4…
|
anonymous
|
316 |
if not results: |
|
5e4b8e4…
|
anonymous
|
317 |
console.print("[yellow]No results.[/yellow]") |
|
5e4b8e4…
|
anonymous
|
318 |
return |
|
5e4b8e4…
|
anonymous
|
319 |
|
|
4be3595…
|
lmata
|
320 |
table = Table(title=f"Search: {query!r}") |
|
4be3595…
|
lmata
|
321 |
table.add_column("Type", style="cyan") |
|
4be3595…
|
lmata
|
322 |
table.add_column("Name", style="bold") |
|
4be3595…
|
lmata
|
323 |
table.add_column("File / Domain") |
|
4be3595…
|
lmata
|
324 |
table.add_column("Line", justify="right") |
|
4be3595…
|
lmata
|
325 |
for r in results: |
|
4be3595…
|
lmata
|
326 |
loc = r.file_path or r.domain or "" |
|
4be3595…
|
lmata
|
327 |
table.add_row(r.type, r.name, loc, str(r.line_start or "")) |
|
4be3595…
|
lmata
|
328 |
console.print(table) |
|
4be3595…
|
lmata
|
329 |
|
|
4be3595…
|
lmata
|
330 |
|
|
4be3595…
|
lmata
|
331 |
# ── CODE: decorator / query ─────────────────────────────────────────────────── |
|
4be3595…
|
lmata
|
332 |
|
|
758c6b7…
|
lmata
|
333 |
|
|
4be3595…
|
lmata
|
334 |
@main.command() |
|
4be3595…
|
lmata
|
335 |
@click.argument("decorator_name") |
|
4be3595…
|
lmata
|
336 |
@DB_OPTION |
|
4be3595…
|
lmata
|
337 |
@FMT_OPTION |
|
4be3595…
|
lmata
|
338 |
def decorated(decorator_name: str, db: str, fmt: str): |
|
4be3595…
|
lmata
|
339 |
"""Find all functions/methods carrying a decorator.""" |
|
4be3595…
|
lmata
|
340 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
341 |
|
|
4be3595…
|
lmata
|
342 |
results = ContextLoader(_get_store(db)).decorated_by(decorator_name) |
|
4be3595…
|
lmata
|
343 |
|
|
4be3595…
|
lmata
|
344 |
if fmt == "json": |
|
758c6b7…
|
lmata
|
345 |
click.echo( |
|
758c6b7…
|
lmata
|
346 |
json.dumps( |
|
758c6b7…
|
lmata
|
347 |
[ |
|
758c6b7…
|
lmata
|
348 |
{"type": r.type, "name": r.name, "file_path": r.file_path, "line": r.line_start} |
|
758c6b7…
|
lmata
|
349 |
for r in results |
|
758c6b7…
|
lmata
|
350 |
], |
|
758c6b7…
|
lmata
|
351 |
indent=2, |
|
758c6b7…
|
lmata
|
352 |
) |
|
758c6b7…
|
lmata
|
353 |
) |
|
4be3595…
|
lmata
|
354 |
return |
|
4be3595…
|
lmata
|
355 |
|
|
4be3595…
|
lmata
|
356 |
if not results: |
|
4be3595…
|
lmata
|
357 |
console.print(f"[yellow]No functions decorated with @{decorator_name}[/yellow]") |
|
4be3595…
|
lmata
|
358 |
return |
|
4be3595…
|
lmata
|
359 |
|
|
4be3595…
|
lmata
|
360 |
table = Table(title=f"@{decorator_name}") |
|
5e4b8e4…
|
anonymous
|
361 |
table.add_column("Type", style="cyan") |
|
5e4b8e4…
|
anonymous
|
362 |
table.add_column("Name", style="bold") |
|
5e4b8e4…
|
anonymous
|
363 |
table.add_column("File") |
|
5e4b8e4…
|
anonymous
|
364 |
table.add_column("Line", justify="right") |
|
5e4b8e4…
|
anonymous
|
365 |
for r in results: |
|
5e4b8e4…
|
anonymous
|
366 |
table.add_row(r.type, r.name, r.file_path, str(r.line_start or "")) |
|
5e4b8e4…
|
anonymous
|
367 |
console.print(table) |
|
5e4b8e4…
|
anonymous
|
368 |
|
|
5e4b8e4…
|
anonymous
|
369 |
|
|
5e4b8e4…
|
anonymous
|
370 |
@main.command() |
|
e72710e…
|
lmata
|
371 |
@click.argument("cypher") |
|
e72710e…
|
lmata
|
372 |
@DB_OPTION |
|
e72710e…
|
lmata
|
373 |
def query(cypher: str, db: str): |
|
4be3595…
|
lmata
|
374 |
"""Run a raw Cypher query — output is always JSON.""" |
|
4be3595…
|
lmata
|
375 |
result = _get_store(db).query(cypher) |
|
4be3595…
|
lmata
|
376 |
click.echo(json.dumps(result.result_set or [], default=str, indent=2)) |
|
e72710e…
|
lmata
|
377 |
|
|
4be3595…
|
lmata
|
378 |
|
|
4be3595…
|
lmata
|
379 |
# ── KNOWLEDGE: add group ────────────────────────────────────────────────────── |
|
4be3595…
|
lmata
|
380 |
|
|
758c6b7…
|
lmata
|
381 |
|
|
4be3595…
|
lmata
|
382 |
@main.group() |
|
4be3595…
|
lmata
|
383 |
def add(): |
|
4be3595…
|
lmata
|
384 |
"""Add knowledge nodes — concepts, rules, decisions, people, domains.""" |
|
4be3595…
|
lmata
|
385 |
|
|
4be3595…
|
lmata
|
386 |
|
|
4be3595…
|
lmata
|
387 |
@add.command("concept") |
|
4be3595…
|
lmata
|
388 |
@click.argument("name") |
|
4be3595…
|
lmata
|
389 |
@click.option("--desc", default="", help="Description / definition.") |
|
4be3595…
|
lmata
|
390 |
@click.option("--domain", default="") |
|
4be3595…
|
lmata
|
391 |
@click.option("--status", default="", help="e.g. stable, proposed, deprecated") |
|
4be3595…
|
lmata
|
392 |
@click.option("--rules", default="", help="Comma-separated rule names.") |
|
4be3595…
|
lmata
|
393 |
@click.option("--wiki", default="", help="Wiki URL or reference.") |
|
4be3595…
|
lmata
|
394 |
@DB_OPTION |
|
4be3595…
|
lmata
|
395 |
def add_concept(name: str, desc: str, domain: str, status: str, rules: str, wiki: str, db: str): |
|
4be3595…
|
lmata
|
396 |
"""Add a business concept to the knowledge graph.""" |
|
4be3595…
|
lmata
|
397 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
398 |
|
|
4be3595…
|
lmata
|
399 |
k = KnowledgeIngester(_get_store(db)) |
|
758c6b7…
|
lmata
|
400 |
k.add_concept(name, description=desc, domain=domain, status=status, rules=rules, wiki_refs=wiki) |
|
4be3595…
|
lmata
|
401 |
console.print(f"[green]Concept added:[/green] {name}") |
|
4be3595…
|
lmata
|
402 |
|
|
4be3595…
|
lmata
|
403 |
|
|
4be3595…
|
lmata
|
404 |
@add.command("rule") |
|
4be3595…
|
lmata
|
405 |
@click.argument("name") |
|
4be3595…
|
lmata
|
406 |
@click.option("--desc", default="") |
|
4be3595…
|
lmata
|
407 |
@click.option("--domain", default="") |
|
4be3595…
|
lmata
|
408 |
@click.option("--severity", default="info", type=click.Choice(["info", "warning", "critical"])) |
|
4be3595…
|
lmata
|
409 |
@click.option("--rationale", default="") |
|
4be3595…
|
lmata
|
410 |
@DB_OPTION |
|
4be3595…
|
lmata
|
411 |
def add_rule(name: str, desc: str, domain: str, severity: str, rationale: str, db: str): |
|
4be3595…
|
lmata
|
412 |
"""Add a business rule or constraint.""" |
|
4be3595…
|
lmata
|
413 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
414 |
|
|
4be3595…
|
lmata
|
415 |
k = KnowledgeIngester(_get_store(db)) |
|
4be3595…
|
lmata
|
416 |
k.add_rule(name, description=desc, domain=domain, severity=severity, rationale=rationale) |
|
4be3595…
|
lmata
|
417 |
console.print(f"[green]Rule added:[/green] {name}") |
|
4be3595…
|
lmata
|
418 |
|
|
4be3595…
|
lmata
|
419 |
|
|
4be3595…
|
lmata
|
420 |
@add.command("decision") |
|
4be3595…
|
lmata
|
421 |
@click.argument("name") |
|
4be3595…
|
lmata
|
422 |
@click.option("--desc", default="") |
|
4be3595…
|
lmata
|
423 |
@click.option("--domain", default="") |
|
4be3595…
|
lmata
|
424 |
@click.option("--rationale", default="") |
|
4be3595…
|
lmata
|
425 |
@click.option("--alternatives", default="") |
|
4be3595…
|
lmata
|
426 |
@click.option("--date", default="") |
|
758c6b7…
|
lmata
|
427 |
@click.option( |
|
758c6b7…
|
lmata
|
428 |
"--status", default="accepted", type=click.Choice(["proposed", "accepted", "deprecated"]) |
|
758c6b7…
|
lmata
|
429 |
) |
|
4be3595…
|
lmata
|
430 |
@DB_OPTION |
|
4be3595…
|
lmata
|
431 |
def add_decision(name, desc, domain, rationale, alternatives, date, status, db): |
|
4be3595…
|
lmata
|
432 |
"""Add an architectural or product decision.""" |
|
4be3595…
|
lmata
|
433 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
434 |
|
|
4be3595…
|
lmata
|
435 |
k = KnowledgeIngester(_get_store(db)) |
|
758c6b7…
|
lmata
|
436 |
k.add_decision( |
|
758c6b7…
|
lmata
|
437 |
name, |
|
758c6b7…
|
lmata
|
438 |
description=desc, |
|
758c6b7…
|
lmata
|
439 |
domain=domain, |
|
758c6b7…
|
lmata
|
440 |
status=status, |
|
758c6b7…
|
lmata
|
441 |
rationale=rationale, |
|
758c6b7…
|
lmata
|
442 |
alternatives=alternatives, |
|
758c6b7…
|
lmata
|
443 |
date=date, |
|
758c6b7…
|
lmata
|
444 |
) |
|
4be3595…
|
lmata
|
445 |
console.print(f"[green]Decision added:[/green] {name}") |
|
4be3595…
|
lmata
|
446 |
|
|
4be3595…
|
lmata
|
447 |
|
|
4be3595…
|
lmata
|
448 |
@add.command("person") |
|
4be3595…
|
lmata
|
449 |
@click.argument("name") |
|
4be3595…
|
lmata
|
450 |
@click.option("--email", default="") |
|
4be3595…
|
lmata
|
451 |
@click.option("--role", default="") |
|
4be3595…
|
lmata
|
452 |
@click.option("--team", default="") |
|
4be3595…
|
lmata
|
453 |
@DB_OPTION |
|
4be3595…
|
lmata
|
454 |
def add_person(name: str, email: str, role: str, team: str, db: str): |
|
4be3595…
|
lmata
|
455 |
"""Add a person (contributor, owner, stakeholder).""" |
|
4be3595…
|
lmata
|
456 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
457 |
|
|
4be3595…
|
lmata
|
458 |
k = KnowledgeIngester(_get_store(db)) |
|
4be3595…
|
lmata
|
459 |
k.add_person(name, email=email, role=role, team=team) |
|
4be3595…
|
lmata
|
460 |
console.print(f"[green]Person added:[/green] {name}") |
|
4be3595…
|
lmata
|
461 |
|
|
4be3595…
|
lmata
|
462 |
|
|
4be3595…
|
lmata
|
463 |
@add.command("domain") |
|
4be3595…
|
lmata
|
464 |
@click.argument("name") |
|
4be3595…
|
lmata
|
465 |
@click.option("--desc", default="") |
|
4be3595…
|
lmata
|
466 |
@DB_OPTION |
|
4be3595…
|
lmata
|
467 |
def add_domain(name: str, desc: str, db: str): |
|
4be3595…
|
lmata
|
468 |
"""Add a business domain (auth, billing, notifications…).""" |
|
4be3595…
|
lmata
|
469 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
470 |
|
|
4be3595…
|
lmata
|
471 |
k = KnowledgeIngester(_get_store(db)) |
|
4be3595…
|
lmata
|
472 |
k.add_domain(name, description=desc) |
|
4be3595…
|
lmata
|
473 |
console.print(f"[green]Domain added:[/green] {name}") |
|
4be3595…
|
lmata
|
474 |
|
|
4be3595…
|
lmata
|
475 |
|
|
4be3595…
|
lmata
|
476 |
# ── KNOWLEDGE: annotate ─────────────────────────────────────────────────────── |
|
4be3595…
|
lmata
|
477 |
|
|
758c6b7…
|
lmata
|
478 |
|
|
4be3595…
|
lmata
|
479 |
@main.command() |
|
4be3595…
|
lmata
|
480 |
@click.argument("code_name") |
|
758c6b7…
|
lmata
|
481 |
@click.option( |
|
758c6b7…
|
lmata
|
482 |
"--type", |
|
758c6b7…
|
lmata
|
483 |
"code_label", |
|
758c6b7…
|
lmata
|
484 |
default="Function", |
|
758c6b7…
|
lmata
|
485 |
type=click.Choice(["Function", "Class", "Method", "File", "Module"]), |
|
758c6b7…
|
lmata
|
486 |
) |
|
4be3595…
|
lmata
|
487 |
@click.option("--concept", default="", help="Link to this concept.") |
|
4be3595…
|
lmata
|
488 |
@click.option("--rule", default="", help="Link to this rule.") |
|
4be3595…
|
lmata
|
489 |
@DB_OPTION |
|
4be3595…
|
lmata
|
490 |
def annotate(code_name: str, code_label: str, concept: str, rule: str, db: str): |
|
4be3595…
|
lmata
|
491 |
"""Link a code node to a concept or rule in the knowledge graph.""" |
|
4be3595…
|
lmata
|
492 |
from navegador.ingestion import KnowledgeIngester |
|
758c6b7…
|
lmata
|
493 |
|
|
4be3595…
|
lmata
|
494 |
k = KnowledgeIngester(_get_store(db)) |
|
758c6b7…
|
lmata
|
495 |
k.annotate_code(code_name, code_label, concept=concept or None, rule=rule or None) |
|
4be3595…
|
lmata
|
496 |
console.print(f"[green]Annotated:[/green] {code_name}") |
|
4be3595…
|
lmata
|
497 |
|
|
4be3595…
|
lmata
|
498 |
|
|
4be3595…
|
lmata
|
499 |
# ── KNOWLEDGE: domain view ──────────────────────────────────────────────────── |
|
758c6b7…
|
lmata
|
500 |
|
|
4be3595…
|
lmata
|
501 |
|
|
4be3595…
|
lmata
|
502 |
@main.command() |
|
4be3595…
|
lmata
|
503 |
@click.argument("name") |
|
4be3595…
|
lmata
|
504 |
@DB_OPTION |
|
4be3595…
|
lmata
|
505 |
@FMT_OPTION |
|
4be3595…
|
lmata
|
506 |
def domain(name: str, db: str, fmt: str): |
|
4be3595…
|
lmata
|
507 |
"""Show everything belonging to a domain — code and knowledge.""" |
|
4be3595…
|
lmata
|
508 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
509 |
|
|
4be3595…
|
lmata
|
510 |
bundle = ContextLoader(_get_store(db)).load_domain(name) |
|
4be3595…
|
lmata
|
511 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
4be3595…
|
lmata
|
512 |
|
|
4be3595…
|
lmata
|
513 |
|
|
4be3595…
|
lmata
|
514 |
@main.command() |
|
4be3595…
|
lmata
|
515 |
@click.argument("name") |
|
4be3595…
|
lmata
|
516 |
@DB_OPTION |
|
4be3595…
|
lmata
|
517 |
@FMT_OPTION |
|
4be3595…
|
lmata
|
518 |
def concept(name: str, db: str, fmt: str): |
|
4be3595…
|
lmata
|
519 |
"""Load a business concept — rules, related concepts, implementing code, wiki.""" |
|
4be3595…
|
lmata
|
520 |
from navegador.context import ContextLoader |
|
758c6b7…
|
lmata
|
521 |
|
|
4be3595…
|
lmata
|
522 |
bundle = ContextLoader(_get_store(db)).load_concept(name) |
|
4be3595…
|
lmata
|
523 |
_emit(bundle.to_json() if fmt == "json" else bundle.to_markdown(), fmt) |
|
4be3595…
|
lmata
|
524 |
|
|
4be3595…
|
lmata
|
525 |
|
|
4be3595…
|
lmata
|
526 |
# ── KNOWLEDGE: wiki ─────────────────────────────────────────────────────────── |
|
758c6b7…
|
lmata
|
527 |
|
|
4be3595…
|
lmata
|
528 |
|
|
4be3595…
|
lmata
|
529 |
@main.group() |
|
4be3595…
|
lmata
|
530 |
def wiki(): |
|
4be3595…
|
lmata
|
531 |
"""Ingest and manage wiki pages in the knowledge graph.""" |
|
4be3595…
|
lmata
|
532 |
|
|
4be3595…
|
lmata
|
533 |
|
|
4be3595…
|
lmata
|
534 |
@wiki.command("ingest") |
|
4be3595…
|
lmata
|
535 |
@click.option("--repo", default="", help="GitHub repo (owner/repo) — clones the wiki.") |
|
4be3595…
|
lmata
|
536 |
@click.option("--dir", "wiki_dir", default="", help="Local directory of markdown files.") |
|
4be3595…
|
lmata
|
537 |
@click.option("--token", default="", envvar="GITHUB_TOKEN", help="GitHub token.") |
|
4be3595…
|
lmata
|
538 |
@click.option("--api", is_flag=True, help="Use GitHub API instead of git clone.") |
|
4be3595…
|
lmata
|
539 |
@DB_OPTION |
|
4be3595…
|
lmata
|
540 |
def wiki_ingest(repo: str, wiki_dir: str, token: str, api: bool, db: str): |
|
4be3595…
|
lmata
|
541 |
"""Pull wiki pages into the knowledge graph.""" |
|
4be3595…
|
lmata
|
542 |
from navegador.ingestion import WikiIngester |
|
758c6b7…
|
lmata
|
543 |
|
|
4be3595…
|
lmata
|
544 |
w = WikiIngester(_get_store(db)) |
|
4be3595…
|
lmata
|
545 |
|
|
4be3595…
|
lmata
|
546 |
if wiki_dir: |
|
4be3595…
|
lmata
|
547 |
stats = w.ingest_local(wiki_dir) |
|
4be3595…
|
lmata
|
548 |
elif repo: |
|
4be3595…
|
lmata
|
549 |
if api: |
|
4be3595…
|
lmata
|
550 |
stats = w.ingest_github_api(repo, token=token) |
|
4be3595…
|
lmata
|
551 |
else: |
|
4be3595…
|
lmata
|
552 |
stats = w.ingest_github(repo, token=token) |
|
4be3595…
|
lmata
|
553 |
else: |
|
4be3595…
|
lmata
|
554 |
raise click.UsageError("Provide --repo or --dir") |
|
4be3595…
|
lmata
|
555 |
|
|
4be3595…
|
lmata
|
556 |
console.print(f"[green]Wiki ingested:[/green] {stats['pages']} pages, {stats['links']} links") |
|
4be3595…
|
lmata
|
557 |
|
|
4be3595…
|
lmata
|
558 |
|
|
4be3595…
|
lmata
|
559 |
# ── Stats ───────────────────────────────────────────────────────────────────── |
|
e72710e…
|
lmata
|
560 |
|
|
758c6b7…
|
lmata
|
561 |
|
|
e72710e…
|
lmata
|
562 |
@main.command() |
|
e72710e…
|
lmata
|
563 |
@DB_OPTION |
|
4be3595…
|
lmata
|
564 |
@click.option("--json", "as_json", is_flag=True) |
|
e72710e…
|
lmata
|
565 |
def stats(db: str, as_json: bool): |
|
4be3595…
|
lmata
|
566 |
"""Graph statistics broken down by node and edge type.""" |
|
4be3595…
|
lmata
|
567 |
from navegador.graph import queries as q |
|
758c6b7…
|
lmata
|
568 |
|
|
e72710e…
|
lmata
|
569 |
store = _get_store(db) |
|
4be3595…
|
lmata
|
570 |
|
|
758c6b7…
|
lmata
|
571 |
node_rows = store.query(q.NODE_TYPE_COUNTS).result_set or [] |
|
758c6b7…
|
lmata
|
572 |
edge_rows = store.query(q.EDGE_TYPE_COUNTS).result_set or [] |
|
4be3595…
|
lmata
|
573 |
|
|
4be3595…
|
lmata
|
574 |
total_nodes = sum(r[1] for r in node_rows) |
|
4be3595…
|
lmata
|
575 |
total_edges = sum(r[1] for r in edge_rows) |
|
4be3595…
|
lmata
|
576 |
|
|
4be3595…
|
lmata
|
577 |
if as_json: |
|
758c6b7…
|
lmata
|
578 |
click.echo( |
|
758c6b7…
|
lmata
|
579 |
json.dumps( |
|
758c6b7…
|
lmata
|
580 |
{ |
|
758c6b7…
|
lmata
|
581 |
"total_nodes": total_nodes, |
|
758c6b7…
|
lmata
|
582 |
"total_edges": total_edges, |
|
758c6b7…
|
lmata
|
583 |
"nodes": {r[0]: r[1] for r in node_rows}, |
|
758c6b7…
|
lmata
|
584 |
"edges": {r[0]: r[1] for r in edge_rows}, |
|
758c6b7…
|
lmata
|
585 |
}, |
|
758c6b7…
|
lmata
|
586 |
indent=2, |
|
758c6b7…
|
lmata
|
587 |
) |
|
758c6b7…
|
lmata
|
588 |
) |
|
4be3595…
|
lmata
|
589 |
return |
|
4be3595…
|
lmata
|
590 |
|
|
4be3595…
|
lmata
|
591 |
node_table = Table(title=f"Nodes ({total_nodes:,})") |
|
4be3595…
|
lmata
|
592 |
node_table.add_column("Type", style="cyan") |
|
4be3595…
|
lmata
|
593 |
node_table.add_column("Count", justify="right", style="green") |
|
4be3595…
|
lmata
|
594 |
for row in node_rows: |
|
4be3595…
|
lmata
|
595 |
node_table.add_row(row[0], f"{row[1]:,}") |
|
4be3595…
|
lmata
|
596 |
|
|
4be3595…
|
lmata
|
597 |
edge_table = Table(title=f"Edges ({total_edges:,})") |
|
4be3595…
|
lmata
|
598 |
edge_table.add_column("Type", style="cyan") |
|
4be3595…
|
lmata
|
599 |
edge_table.add_column("Count", justify="right", style="green") |
|
4be3595…
|
lmata
|
600 |
for row in edge_rows: |
|
4be3595…
|
lmata
|
601 |
edge_table.add_row(row[0], f"{row[1]:,}") |
|
4be3595…
|
lmata
|
602 |
|
|
4be3595…
|
lmata
|
603 |
console.print(node_table) |
|
4be3595…
|
lmata
|
604 |
console.print(edge_table) |
|
4be3595…
|
lmata
|
605 |
|
|
4be3595…
|
lmata
|
606 |
|
|
9c8fd06…
|
lmata
|
607 |
# ── PLANOPTICON ingestion ────────────────────────────────────────────────────── |
|
9c8fd06…
|
lmata
|
608 |
|
|
758c6b7…
|
lmata
|
609 |
|
|
9c8fd06…
|
lmata
|
610 |
@main.group() |
|
9c8fd06…
|
lmata
|
611 |
def planopticon(): |
|
9c8fd06…
|
lmata
|
612 |
"""Ingest planopticon output (meetings, videos, docs) into the knowledge graph.""" |
|
9c8fd06…
|
lmata
|
613 |
|
|
9c8fd06…
|
lmata
|
614 |
|
|
9c8fd06…
|
lmata
|
615 |
@planopticon.command("ingest") |
|
9c8fd06…
|
lmata
|
616 |
@click.argument("path", type=click.Path(exists=True)) |
|
758c6b7…
|
lmata
|
617 |
@click.option( |
|
758c6b7…
|
lmata
|
618 |
"--type", |
|
758c6b7…
|
lmata
|
619 |
"input_type", |
|
758c6b7…
|
lmata
|
620 |
type=click.Choice(["auto", "manifest", "kg", "interchange", "batch"]), |
|
758c6b7…
|
lmata
|
621 |
default="auto", |
|
758c6b7…
|
lmata
|
622 |
show_default=True, |
|
758c6b7…
|
lmata
|
623 |
help="Input format. auto detects from filename.", |
|
758c6b7…
|
lmata
|
624 |
) |
|
9c8fd06…
|
lmata
|
625 |
@click.option("--source", default="", help="Source label for provenance (e.g. 'Q4 planning').") |
|
9c8fd06…
|
lmata
|
626 |
@click.option("--json", "as_json", is_flag=True) |
|
9c8fd06…
|
lmata
|
627 |
@DB_OPTION |
|
9c8fd06…
|
lmata
|
628 |
def planopticon_ingest(path: str, input_type: str, source: str, as_json: bool, db: str): |
|
9c8fd06…
|
lmata
|
629 |
"""Load a planopticon output directory or file into the knowledge graph. |
|
9c8fd06…
|
lmata
|
630 |
|
|
9c8fd06…
|
lmata
|
631 |
PATH can be: |
|
9c8fd06…
|
lmata
|
632 |
- A manifest.json file |
|
9c8fd06…
|
lmata
|
633 |
- A knowledge_graph.json file |
|
9c8fd06…
|
lmata
|
634 |
- An interchange.json file |
|
9c8fd06…
|
lmata
|
635 |
- A batch manifest JSON |
|
9c8fd06…
|
lmata
|
636 |
- A planopticon output directory (auto-detects manifest.json inside) |
|
9c8fd06…
|
lmata
|
637 |
""" |
|
9c8fd06…
|
lmata
|
638 |
from pathlib import Path as P |
|
9c8fd06…
|
lmata
|
639 |
|
|
9c8fd06…
|
lmata
|
640 |
from navegador.ingestion import PlanopticonIngester |
|
9c8fd06…
|
lmata
|
641 |
|
|
9c8fd06…
|
lmata
|
642 |
p = P(path) |
|
9c8fd06…
|
lmata
|
643 |
# Resolve directory → manifest.json |
|
9c8fd06…
|
lmata
|
644 |
if p.is_dir(): |
|
9c8fd06…
|
lmata
|
645 |
candidates = ["manifest.json", "results/knowledge_graph.json", "interchange.json"] |
|
9c8fd06…
|
lmata
|
646 |
for c in candidates: |
|
9c8fd06…
|
lmata
|
647 |
if (p / c).exists(): |
|
9c8fd06…
|
lmata
|
648 |
p = p / c |
|
9c8fd06…
|
lmata
|
649 |
break |
|
9c8fd06…
|
lmata
|
650 |
else: |
|
9c8fd06…
|
lmata
|
651 |
raise click.UsageError(f"No recognised planopticon file found in {path}") |
|
9c8fd06…
|
lmata
|
652 |
|
|
9c8fd06…
|
lmata
|
653 |
# Auto-detect type from filename |
|
9c8fd06…
|
lmata
|
654 |
if input_type == "auto": |
|
9c8fd06…
|
lmata
|
655 |
name = p.name.lower() |
|
9c8fd06…
|
lmata
|
656 |
if "manifest" in name: |
|
9c8fd06…
|
lmata
|
657 |
input_type = "manifest" |
|
9c8fd06…
|
lmata
|
658 |
elif "interchange" in name: |
|
9c8fd06…
|
lmata
|
659 |
input_type = "interchange" |
|
9c8fd06…
|
lmata
|
660 |
elif "batch" in name: |
|
9c8fd06…
|
lmata
|
661 |
input_type = "batch" |
|
9c8fd06…
|
lmata
|
662 |
else: |
|
9c8fd06…
|
lmata
|
663 |
input_type = "kg" |
|
9c8fd06…
|
lmata
|
664 |
|
|
9c8fd06…
|
lmata
|
665 |
ing = PlanopticonIngester(_get_store(db), source_tag=source) |
|
9c8fd06…
|
lmata
|
666 |
|
|
9c8fd06…
|
lmata
|
667 |
dispatch = { |
|
758c6b7…
|
lmata
|
668 |
"manifest": ing.ingest_manifest, |
|
758c6b7…
|
lmata
|
669 |
"kg": ing.ingest_kg, |
|
9c8fd06…
|
lmata
|
670 |
"interchange": ing.ingest_interchange, |
|
758c6b7…
|
lmata
|
671 |
"batch": ing.ingest_batch, |
|
9c8fd06…
|
lmata
|
672 |
} |
|
9c8fd06…
|
lmata
|
673 |
stats = dispatch[input_type](p) |
|
9c8fd06…
|
lmata
|
674 |
|
|
9c8fd06…
|
lmata
|
675 |
if as_json: |
|
9c8fd06…
|
lmata
|
676 |
click.echo(json.dumps(stats, indent=2)) |
|
9c8fd06…
|
lmata
|
677 |
else: |
|
9c8fd06…
|
lmata
|
678 |
table = Table(title=f"Planopticon import ({input_type})") |
|
9c8fd06…
|
lmata
|
679 |
table.add_column("Metric", style="cyan") |
|
9c8fd06…
|
lmata
|
680 |
table.add_column("Count", justify="right", style="green") |
|
9c8fd06…
|
lmata
|
681 |
for k, v in stats.items(): |
|
9c8fd06…
|
lmata
|
682 |
table.add_row(k.capitalize(), str(v)) |
|
9c8fd06…
|
lmata
|
683 |
console.print(table) |
|
9c8fd06…
|
lmata
|
684 |
|
|
9c8fd06…
|
lmata
|
685 |
|
|
57ba118…
|
lmata
|
686 |
# ── Export / Import ────────────────────────────────────────────────────────── |
|
57ba118…
|
lmata
|
687 |
|
|
57ba118…
|
lmata
|
688 |
|
|
57ba118…
|
lmata
|
689 |
@main.command("export") |
|
57ba118…
|
lmata
|
690 |
@click.argument("output", type=click.Path()) |
|
57ba118…
|
lmata
|
691 |
@DB_OPTION |
|
57ba118…
|
lmata
|
692 |
@click.option("--json", "as_json", is_flag=True, help="Output stats as JSON.") |
|
57ba118…
|
lmata
|
693 |
def export_cmd(output: str, db: str, as_json: bool): |
|
57ba118…
|
lmata
|
694 |
"""Export the graph to a text-based JSONL file (git-friendly).""" |
|
57ba118…
|
lmata
|
695 |
from navegador.graph.export import export_graph |
|
57ba118…
|
lmata
|
696 |
|
|
57ba118…
|
lmata
|
697 |
store = _get_store(db) |
|
57ba118…
|
lmata
|
698 |
stats = export_graph(store, output) |
|
57ba118…
|
lmata
|
699 |
|
|
57ba118…
|
lmata
|
700 |
if as_json: |
|
57ba118…
|
lmata
|
701 |
click.echo(json.dumps(stats, indent=2)) |
|
57ba118…
|
lmata
|
702 |
else: |
|
57ba118…
|
lmata
|
703 |
console.print( |
|
57ba118…
|
lmata
|
704 |
f"[green]Exported[/green] {stats['nodes']} nodes, {stats['edges']} edges → {output}" |
|
57ba118…
|
lmata
|
705 |
) |
|
57ba118…
|
lmata
|
706 |
|
|
57ba118…
|
lmata
|
707 |
|
|
57ba118…
|
lmata
|
708 |
@main.command("import") |
|
57ba118…
|
lmata
|
709 |
@click.argument("input_path", type=click.Path(exists=True)) |
|
57ba118…
|
lmata
|
710 |
@DB_OPTION |
|
57ba118…
|
lmata
|
711 |
@click.option("--no-clear", is_flag=True, help="Don't wipe graph before importing.") |
|
57ba118…
|
lmata
|
712 |
@click.option("--json", "as_json", is_flag=True, help="Output stats as JSON.") |
|
57ba118…
|
lmata
|
713 |
def import_cmd(input_path: str, db: str, no_clear: bool, as_json: bool): |
|
57ba118…
|
lmata
|
714 |
"""Import a graph from a JSONL export file.""" |
|
57ba118…
|
lmata
|
715 |
from navegador.graph.export import import_graph |
|
57ba118…
|
lmata
|
716 |
|
|
57ba118…
|
lmata
|
717 |
store = _get_store(db) |
|
57ba118…
|
lmata
|
718 |
stats = import_graph(store, input_path, clear=not no_clear) |
|
57ba118…
|
lmata
|
719 |
|
|
57ba118…
|
lmata
|
720 |
if as_json: |
|
57ba118…
|
lmata
|
721 |
click.echo(json.dumps(stats, indent=2)) |
|
57ba118…
|
lmata
|
722 |
else: |
|
57ba118…
|
lmata
|
723 |
console.print( |
|
57ba118…
|
lmata
|
724 |
f"[green]Imported[/green] {stats['nodes']} nodes, {stats['edges']} edges ← {input_path}" |
|
57ba118…
|
lmata
|
725 |
) |
|
57ba118…
|
lmata
|
726 |
|
|
57ba118…
|
lmata
|
727 |
|
|
0838495…
|
lmata
|
728 |
# ── Schema migrations ──────────────────────────────────────────────────────── |
|
0838495…
|
lmata
|
729 |
|
|
0838495…
|
lmata
|
730 |
|
|
0838495…
|
lmata
|
731 |
@main.command() |
|
0838495…
|
lmata
|
732 |
@DB_OPTION |
|
0838495…
|
lmata
|
733 |
@click.option("--check", is_flag=True, help="Check if migration is needed without applying.") |
|
0838495…
|
lmata
|
734 |
def migrate(db: str, check: bool): |
|
0838495…
|
lmata
|
735 |
"""Apply pending schema migrations to the graph.""" |
|
0838495…
|
lmata
|
736 |
from navegador.graph.migrations import ( |
|
0838495…
|
lmata
|
737 |
CURRENT_SCHEMA_VERSION, |
|
0838495…
|
lmata
|
738 |
get_schema_version, |
|
0838495…
|
lmata
|
739 |
needs_migration, |
|
e5e1a3b…
|
lmata
|
740 |
) |
|
e5e1a3b…
|
lmata
|
741 |
from navegador.graph.migrations import ( |
|
e5e1a3b…
|
lmata
|
742 |
migrate as do_migrate, |
|
0838495…
|
lmata
|
743 |
) |
|
0838495…
|
lmata
|
744 |
|
|
0838495…
|
lmata
|
745 |
store = _get_store(db) |
|
0838495…
|
lmata
|
746 |
|
|
0838495…
|
lmata
|
747 |
if check: |
|
0838495…
|
lmata
|
748 |
current = get_schema_version(store) |
|
0838495…
|
lmata
|
749 |
if needs_migration(store): |
|
0838495…
|
lmata
|
750 |
console.print( |
|
0838495…
|
lmata
|
751 |
f"[yellow]Migration needed:[/yellow] v{current} → v{CURRENT_SCHEMA_VERSION}" |
|
0838495…
|
lmata
|
752 |
) |
|
0838495…
|
lmata
|
753 |
else: |
|
0838495…
|
lmata
|
754 |
console.print(f"[green]Schema is up to date[/green] (v{current})") |
|
0838495…
|
lmata
|
755 |
return |
|
0838495…
|
lmata
|
756 |
|
|
0838495…
|
lmata
|
757 |
current = get_schema_version(store) |
|
0838495…
|
lmata
|
758 |
applied = do_migrate(store) |
|
0838495…
|
lmata
|
759 |
if applied: |
|
0838495…
|
lmata
|
760 |
console.print( |
|
0838495…
|
lmata
|
761 |
f"[green]Migrated[/green] v{current} → v{CURRENT_SCHEMA_VERSION} " |
|
0838495…
|
lmata
|
762 |
f"({len(applied)} migration{'s' if len(applied) != 1 else ''})" |
|
0838495…
|
lmata
|
763 |
) |
|
0838495…
|
lmata
|
764 |
else: |
|
0838495…
|
lmata
|
765 |
console.print(f"[green]Schema is up to date[/green] (v{current})") |
|
2602093…
|
lmata
|
766 |
|
|
2602093…
|
lmata
|
767 |
|
|
2602093…
|
lmata
|
768 |
# ── Graph explorer ──────────────────────────────────────────────────────────── |
|
2602093…
|
lmata
|
769 |
|
|
2602093…
|
lmata
|
770 |
|
|
2602093…
|
lmata
|
771 |
@main.command() |
|
2602093…
|
lmata
|
772 |
@DB_OPTION |
|
2602093…
|
lmata
|
773 |
@click.option("--host", default="127.0.0.1", show_default=True, help="Bind address.") |
|
2602093…
|
lmata
|
774 |
@click.option("--port", default=8080, show_default=True, help="TCP port.") |
|
2602093…
|
lmata
|
775 |
@click.option( |
|
2602093…
|
lmata
|
776 |
"--no-browser", |
|
2602093…
|
lmata
|
777 |
is_flag=True, |
|
2602093…
|
lmata
|
778 |
default=False, |
|
2602093…
|
lmata
|
779 |
help="Don't open a browser tab automatically.", |
|
2602093…
|
lmata
|
780 |
) |
|
2602093…
|
lmata
|
781 |
def explore(db: str, host: str, port: int, no_browser: bool): |
|
2602093…
|
lmata
|
782 |
"""Launch the browser-based graph explorer. |
|
2602093…
|
lmata
|
783 |
|
|
2602093…
|
lmata
|
784 |
Starts an HTTP server and opens the interactive force-directed |
|
2602093…
|
lmata
|
785 |
visualisation in your default browser. |
|
2602093…
|
lmata
|
786 |
|
|
2602093…
|
lmata
|
787 |
\b |
|
2602093…
|
lmata
|
788 |
Examples: |
|
2602093…
|
lmata
|
789 |
navegador explore |
|
2602093…
|
lmata
|
790 |
navegador explore --port 9000 |
|
2602093…
|
lmata
|
791 |
navegador explore --no-browser |
|
2602093…
|
lmata
|
792 |
""" |
|
2602093…
|
lmata
|
793 |
import time |
|
2602093…
|
lmata
|
794 |
import webbrowser |
|
2602093…
|
lmata
|
795 |
|
|
2602093…
|
lmata
|
796 |
from navegador.explorer import ExplorerServer |
|
2602093…
|
lmata
|
797 |
|
|
2602093…
|
lmata
|
798 |
store = _get_store(db) |
|
2602093…
|
lmata
|
799 |
server = ExplorerServer(store, host=host, port=port) |
|
2602093…
|
lmata
|
800 |
server.start() |
|
2602093…
|
lmata
|
801 |
url = server.url |
|
2602093…
|
lmata
|
802 |
|
|
2602093…
|
lmata
|
803 |
console.print(f"[green]Graph explorer running[/green] → {url}") |
|
2602093…
|
lmata
|
804 |
console.print("Press [bold]Ctrl-C[/bold] to stop.") |
|
2602093…
|
lmata
|
805 |
|
|
2602093…
|
lmata
|
806 |
if not no_browser: |
|
2602093…
|
lmata
|
807 |
# Small delay so the server is accepting connections before the browser hits it |
|
2602093…
|
lmata
|
808 |
time.sleep(0.3) |
|
2602093…
|
lmata
|
809 |
webbrowser.open(url) |
|
2602093…
|
lmata
|
810 |
|
|
2602093…
|
lmata
|
811 |
try: |
|
2602093…
|
lmata
|
812 |
while True: |
|
2602093…
|
lmata
|
813 |
time.sleep(1) |
|
2602093…
|
lmata
|
814 |
except KeyboardInterrupt: |
|
2602093…
|
lmata
|
815 |
console.print("\n[yellow]Stopping explorer…[/yellow]") |
|
2602093…
|
lmata
|
816 |
finally: |
|
2602093…
|
lmata
|
817 |
server.stop() |
|
33fd0c0…
|
lmata
|
818 |
|
|
33fd0c0…
|
lmata
|
819 |
|
|
33fd0c0…
|
lmata
|
820 |
# ── Enrichment ─────────────────────────────────────────────────────────────── |
|
33fd0c0…
|
lmata
|
821 |
|
|
33fd0c0…
|
lmata
|
822 |
|
|
33fd0c0…
|
lmata
|
823 |
@main.command() |
|
33fd0c0…
|
lmata
|
824 |
@DB_OPTION |
|
33fd0c0…
|
lmata
|
825 |
@click.option( |
|
33fd0c0…
|
lmata
|
826 |
"--framework", |
|
33fd0c0…
|
lmata
|
827 |
"framework_name", |
|
33fd0c0…
|
lmata
|
828 |
default="", |
|
33fd0c0…
|
lmata
|
829 |
help="Framework to enrich (e.g. django, fastapi). Auto-detects if omitted.", |
|
33fd0c0…
|
lmata
|
830 |
) |
|
33fd0c0…
|
lmata
|
831 |
@click.option("--json", "as_json", is_flag=True, help="Output results as JSON.") |
|
33fd0c0…
|
lmata
|
832 |
def enrich(db: str, framework_name: str, as_json: bool): |
|
33fd0c0…
|
lmata
|
833 |
"""Run framework enrichment on the graph. |
|
33fd0c0…
|
lmata
|
834 |
|
|
33fd0c0…
|
lmata
|
835 |
Promotes generic Function/Class nodes to semantic framework types |
|
33fd0c0…
|
lmata
|
836 |
by detecting framework patterns and adding labels/properties. |
|
33fd0c0…
|
lmata
|
837 |
|
|
33fd0c0…
|
lmata
|
838 |
\b |
|
33fd0c0…
|
lmata
|
839 |
Auto-detect all frameworks: |
|
33fd0c0…
|
lmata
|
840 |
navegador enrich |
|
33fd0c0…
|
lmata
|
841 |
|
|
33fd0c0…
|
lmata
|
842 |
\b |
|
33fd0c0…
|
lmata
|
843 |
Target a specific framework: |
|
33fd0c0…
|
lmata
|
844 |
navegador enrich --framework django |
|
33fd0c0…
|
lmata
|
845 |
""" |
|
33fd0c0…
|
lmata
|
846 |
import importlib |
|
33fd0c0…
|
lmata
|
847 |
import pkgutil |
|
33fd0c0…
|
lmata
|
848 |
|
|
33fd0c0…
|
lmata
|
849 |
import navegador.enrichment as _enrichment_pkg |
|
33fd0c0…
|
lmata
|
850 |
from navegador.enrichment.base import FrameworkEnricher |
|
33fd0c0…
|
lmata
|
851 |
|
|
33fd0c0…
|
lmata
|
852 |
store = _get_store(db) |
|
33fd0c0…
|
lmata
|
853 |
|
|
33fd0c0…
|
lmata
|
854 |
# Discover all FrameworkEnricher subclasses in the enrichment package. |
|
33fd0c0…
|
lmata
|
855 |
def _load_enrichers() -> dict[str, type[FrameworkEnricher]]: |
|
33fd0c0…
|
lmata
|
856 |
enrichers: dict[str, type[FrameworkEnricher]] = {} |
|
33fd0c0…
|
lmata
|
857 |
pkg_path = _enrichment_pkg.__path__ |
|
33fd0c0…
|
lmata
|
858 |
pkg_name = _enrichment_pkg.__name__ |
|
33fd0c0…
|
lmata
|
859 |
for _finder, mod_name, _ispkg in pkgutil.iter_modules(pkg_path): |
|
33fd0c0…
|
lmata
|
860 |
if mod_name == "base": |
|
33fd0c0…
|
lmata
|
861 |
continue |
|
33fd0c0…
|
lmata
|
862 |
mod = importlib.import_module(f"{pkg_name}.{mod_name}") |
|
33fd0c0…
|
lmata
|
863 |
for attr in vars(mod).values(): |
|
33fd0c0…
|
lmata
|
864 |
if ( |
|
33fd0c0…
|
lmata
|
865 |
isinstance(attr, type) |
|
33fd0c0…
|
lmata
|
866 |
and issubclass(attr, FrameworkEnricher) |
|
33fd0c0…
|
lmata
|
867 |
and attr is not FrameworkEnricher |
|
33fd0c0…
|
lmata
|
868 |
): |
|
33fd0c0…
|
lmata
|
869 |
try: |
|
33fd0c0…
|
lmata
|
870 |
instance = attr.__new__(attr) |
|
33fd0c0…
|
lmata
|
871 |
instance.store = store |
|
33fd0c0…
|
lmata
|
872 |
enrichers[attr(store).framework_name] = attr |
|
33fd0c0…
|
lmata
|
873 |
except Exception: # noqa: BLE001 |
|
33fd0c0…
|
lmata
|
874 |
pass |
|
33fd0c0…
|
lmata
|
875 |
return enrichers |
|
33fd0c0…
|
lmata
|
876 |
|
|
33fd0c0…
|
lmata
|
877 |
available = _load_enrichers() |
|
33fd0c0…
|
lmata
|
878 |
|
|
33fd0c0…
|
lmata
|
879 |
if framework_name: |
|
33fd0c0…
|
lmata
|
880 |
if framework_name not in available: |
|
33fd0c0…
|
lmata
|
881 |
raise click.BadParameter( |
|
33fd0c0…
|
lmata
|
882 |
f"Unknown framework {framework_name!r}. " |
|
33fd0c0…
|
lmata
|
883 |
f"Available: {', '.join(sorted(available)) or '(none registered)'}", |
|
33fd0c0…
|
lmata
|
884 |
param_hint="--framework", |
|
33fd0c0…
|
lmata
|
885 |
) |
|
33fd0c0…
|
lmata
|
886 |
targets = {framework_name: available[framework_name]} |
|
33fd0c0…
|
lmata
|
887 |
else: |
|
33fd0c0…
|
lmata
|
888 |
# Auto-detect: only run enrichers whose detect() returns True. |
|
e5e1a3b…
|
lmata
|
889 |
targets = {name: cls for name, cls in available.items() if cls(store).detect()} |
|
33fd0c0…
|
lmata
|
890 |
if not targets and not as_json: |
|
33fd0c0…
|
lmata
|
891 |
console.print("[yellow]No frameworks detected in the graph.[/yellow]") |
|
33fd0c0…
|
lmata
|
892 |
return |
|
33fd0c0…
|
lmata
|
893 |
|
|
33fd0c0…
|
lmata
|
894 |
all_results: dict[str, dict] = {} |
|
33fd0c0…
|
lmata
|
895 |
for name, cls in targets.items(): |
|
33fd0c0…
|
lmata
|
896 |
enricher = cls(store) |
|
33fd0c0…
|
lmata
|
897 |
result = enricher.enrich() |
|
33fd0c0…
|
lmata
|
898 |
all_results[name] = { |
|
33fd0c0…
|
lmata
|
899 |
"promoted": result.promoted, |
|
33fd0c0…
|
lmata
|
900 |
"edges_added": result.edges_added, |
|
33fd0c0…
|
lmata
|
901 |
"patterns_found": result.patterns_found, |
|
33fd0c0…
|
lmata
|
902 |
} |
|
33fd0c0…
|
lmata
|
903 |
|
|
33fd0c0…
|
lmata
|
904 |
if as_json: |
|
33fd0c0…
|
lmata
|
905 |
click.echo(json.dumps(all_results, indent=2)) |
|
33fd0c0…
|
lmata
|
906 |
return |
|
33fd0c0…
|
lmata
|
907 |
|
|
33fd0c0…
|
lmata
|
908 |
for name, data in all_results.items(): |
|
33fd0c0…
|
lmata
|
909 |
table = Table(title=f"Enrichment: {name}") |
|
33fd0c0…
|
lmata
|
910 |
table.add_column("Metric", style="cyan") |
|
33fd0c0…
|
lmata
|
911 |
table.add_column("Value", justify="right", style="green") |
|
33fd0c0…
|
lmata
|
912 |
table.add_row("Nodes promoted", str(data["promoted"])) |
|
33fd0c0…
|
lmata
|
913 |
table.add_row("Edges added", str(data["edges_added"])) |
|
33fd0c0…
|
lmata
|
914 |
for pattern, count in data["patterns_found"].items(): |
|
33fd0c0…
|
lmata
|
915 |
table.add_row(f" {pattern}", str(count)) |
|
33fd0c0…
|
lmata
|
916 |
console.print(table) |
|
33fd0c0…
|
lmata
|
917 |
|
|
33fd0c0…
|
lmata
|
918 |
|
|
33fd0c0…
|
lmata
|
919 |
# ── Diff: map uncommitted changes to affected graph nodes ───────────────────── |
|
33fd0c0…
|
lmata
|
920 |
|
|
33fd0c0…
|
lmata
|
921 |
|
|
33fd0c0…
|
lmata
|
922 |
@main.command("diff") |
|
33fd0c0…
|
lmata
|
923 |
@DB_OPTION |
|
33fd0c0…
|
lmata
|
924 |
@FMT_OPTION |
|
33fd0c0…
|
lmata
|
925 |
@click.option( |
|
33fd0c0…
|
lmata
|
926 |
"--repo", |
|
33fd0c0…
|
lmata
|
927 |
"repo_path", |
|
33fd0c0…
|
lmata
|
928 |
default=".", |
|
33fd0c0…
|
lmata
|
929 |
show_default=True, |
|
33fd0c0…
|
lmata
|
930 |
type=click.Path(exists=True), |
|
33fd0c0…
|
lmata
|
931 |
help="Repository root to inspect (default: current directory).", |
|
33fd0c0…
|
lmata
|
932 |
) |
|
33fd0c0…
|
lmata
|
933 |
def diff_cmd(db: str, fmt: str, repo_path: str): |
|
33fd0c0…
|
lmata
|
934 |
"""Show which graph nodes are affected by uncommitted changes. |
|
33fd0c0…
|
lmata
|
935 |
|
|
33fd0c0…
|
lmata
|
936 |
Reads the current git diff, finds every function/class/method whose |
|
33fd0c0…
|
lmata
|
937 |
line range overlaps a changed hunk, then follows knowledge edges to |
|
33fd0c0…
|
lmata
|
938 |
surface impacted concepts, rules, and decisions. |
|
33fd0c0…
|
lmata
|
939 |
|
|
33fd0c0…
|
lmata
|
940 |
\b |
|
33fd0c0…
|
lmata
|
941 |
Examples: |
|
33fd0c0…
|
lmata
|
942 |
navegador diff |
|
33fd0c0…
|
lmata
|
943 |
navegador diff --format json |
|
33fd0c0…
|
lmata
|
944 |
navegador diff --repo /path/to/project |
|
33fd0c0…
|
lmata
|
945 |
""" |
|
33fd0c0…
|
lmata
|
946 |
from pathlib import Path as P |
|
33fd0c0…
|
lmata
|
947 |
|
|
33fd0c0…
|
lmata
|
948 |
from navegador.diff import DiffAnalyzer |
|
33fd0c0…
|
lmata
|
949 |
|
|
33fd0c0…
|
lmata
|
950 |
analyzer = DiffAnalyzer(_get_store(db), P(repo_path)) |
|
33fd0c0…
|
lmata
|
951 |
|
|
33fd0c0…
|
lmata
|
952 |
if fmt == "json": |
|
33fd0c0…
|
lmata
|
953 |
click.echo(analyzer.to_json()) |
|
33fd0c0…
|
lmata
|
954 |
return |
|
33fd0c0…
|
lmata
|
955 |
|
|
33fd0c0…
|
lmata
|
956 |
# Rich markdown output |
|
33fd0c0…
|
lmata
|
957 |
md = analyzer.to_markdown() |
|
33fd0c0…
|
lmata
|
958 |
console.print(md) |
|
aa55498…
|
lmata
|
959 |
|
|
aa55498…
|
lmata
|
960 |
|
|
aa55498…
|
lmata
|
961 |
# ── Editor integrations ─────────────────────────────────────────────────────── |
|
aa55498…
|
lmata
|
962 |
|
|
aa55498…
|
lmata
|
963 |
|
|
aa55498…
|
lmata
|
964 |
@main.group() |
|
aa55498…
|
lmata
|
965 |
def editor(): |
|
aa55498…
|
lmata
|
966 |
"""Generate MCP config snippets for AI coding editors.""" |
|
aa55498…
|
lmata
|
967 |
|
|
aa55498…
|
lmata
|
968 |
|
|
aa55498…
|
lmata
|
969 |
@editor.command("setup") |
|
aa55498…
|
lmata
|
970 |
@click.argument("editor_name", metavar="EDITOR") |
|
aa55498…
|
lmata
|
971 |
@DB_OPTION |
|
aa55498…
|
lmata
|
972 |
@click.option( |
|
aa55498…
|
lmata
|
973 |
"--write", |
|
aa55498…
|
lmata
|
974 |
"do_write", |
|
aa55498…
|
lmata
|
975 |
is_flag=True, |
|
aa55498…
|
lmata
|
976 |
help="Write the config file to the expected path in the current directory.", |
|
aa55498…
|
lmata
|
977 |
) |
|
aa55498…
|
lmata
|
978 |
def editor_setup(editor_name: str, db: str, do_write: bool): |
|
aa55498…
|
lmata
|
979 |
"""Generate the MCP config snippet for an editor. |
|
aa55498…
|
lmata
|
980 |
|
|
aa55498…
|
lmata
|
981 |
\b |
|
aa55498…
|
lmata
|
982 |
EDITOR is one of: claude-code, cursor, codex, windsurf, all |
|
aa55498…
|
lmata
|
983 |
|
|
aa55498…
|
lmata
|
984 |
\b |
|
aa55498…
|
lmata
|
985 |
Examples: |
|
aa55498…
|
lmata
|
986 |
navegador editor setup claude-code |
|
aa55498…
|
lmata
|
987 |
navegador editor setup cursor --db .navegador/graph.db |
|
aa55498…
|
lmata
|
988 |
navegador editor setup all --write |
|
aa55498…
|
lmata
|
989 |
""" |
|
aa55498…
|
lmata
|
990 |
from navegador.editor import SUPPORTED_EDITORS, EditorIntegration |
|
aa55498…
|
lmata
|
991 |
|
|
aa55498…
|
lmata
|
992 |
if editor_name not in SUPPORTED_EDITORS and editor_name != "all": |
|
aa55498…
|
lmata
|
993 |
raise click.BadParameter( |
|
aa55498…
|
lmata
|
994 |
f"Unknown editor {editor_name!r}. " |
|
aa55498…
|
lmata
|
995 |
f"Choose from: {', '.join(SUPPORTED_EDITORS + ['all'])}", |
|
aa55498…
|
lmata
|
996 |
param_hint="EDITOR", |
|
aa55498…
|
lmata
|
997 |
) |
|
aa55498…
|
lmata
|
998 |
|
|
aa55498…
|
lmata
|
999 |
integration = EditorIntegration(db=db) |
|
aa55498…
|
lmata
|
1000 |
targets = SUPPORTED_EDITORS if editor_name == "all" else [editor_name] |
|
aa55498…
|
lmata
|
1001 |
|
|
aa55498…
|
lmata
|
1002 |
for target in targets: |
|
aa55498…
|
lmata
|
1003 |
config_json = integration.config_json(target) |
|
aa55498…
|
lmata
|
1004 |
config_path = integration.config_path(target) |
|
aa55498…
|
lmata
|
1005 |
|
|
aa55498…
|
lmata
|
1006 |
if len(targets) > 1: |
|
aa55498…
|
lmata
|
1007 |
console.print(f"\n[bold cyan]{target}[/bold cyan] ({config_path})") |
|
aa55498…
|
lmata
|
1008 |
|
|
aa55498…
|
lmata
|
1009 |
click.echo(config_json) |
|
aa55498…
|
lmata
|
1010 |
|
|
aa55498…
|
lmata
|
1011 |
if do_write: |
|
aa55498…
|
lmata
|
1012 |
written = integration.write_config(target) |
|
aa55498…
|
lmata
|
1013 |
console.print(f"[green]Written:[/green] {written}") |
|
aa55498…
|
lmata
|
1014 |
|
|
aa55498…
|
lmata
|
1015 |
|
|
aa55498…
|
lmata
|
1016 |
# ── CI/CD ───────────────────────────────────────────────────────────────────── |
|
aa55498…
|
lmata
|
1017 |
|
|
aa55498…
|
lmata
|
1018 |
|
|
aa55498…
|
lmata
|
1019 |
@main.group() |
|
aa55498…
|
lmata
|
1020 |
def ci(): |
|
aa55498…
|
lmata
|
1021 |
"""CI/CD mode — machine-readable output and structured exit codes. |
|
aa55498…
|
lmata
|
1022 |
|
|
aa55498…
|
lmata
|
1023 |
All subcommands emit JSON to stdout and exit with: |
|
aa55498…
|
lmata
|
1024 |
0 success |
|
aa55498…
|
lmata
|
1025 |
1 error |
|
aa55498…
|
lmata
|
1026 |
2 warnings only |
|
aa55498…
|
lmata
|
1027 |
""" |
|
aa55498…
|
lmata
|
1028 |
|
|
aa55498…
|
lmata
|
1029 |
|
|
aa55498…
|
lmata
|
1030 |
@ci.command("ingest") |
|
aa55498…
|
lmata
|
1031 |
@click.argument("repo_path", type=click.Path(exists=True)) |
|
aa55498…
|
lmata
|
1032 |
@DB_OPTION |
|
aa55498…
|
lmata
|
1033 |
@click.option("--clear", is_flag=True, help="Clear existing graph before ingesting.") |
|
aa55498…
|
lmata
|
1034 |
@click.option("--incremental", is_flag=True, help="Only re-parse changed files.") |
|
aa55498…
|
lmata
|
1035 |
def ci_ingest(repo_path: str, db: str, clear: bool, incremental: bool): |
|
aa55498…
|
lmata
|
1036 |
"""Ingest a repository and exit non-zero on errors or empty results.""" |
|
aa55498…
|
lmata
|
1037 |
import sys |
|
aa55498…
|
lmata
|
1038 |
|
|
aa55498…
|
lmata
|
1039 |
from navegador.cicd import CICDReporter |
|
aa55498…
|
lmata
|
1040 |
from navegador.ingestion import RepoIngester |
|
aa55498…
|
lmata
|
1041 |
|
|
aa55498…
|
lmata
|
1042 |
reporter = CICDReporter() |
|
aa55498…
|
lmata
|
1043 |
data: dict = {} |
|
aa55498…
|
lmata
|
1044 |
|
|
aa55498…
|
lmata
|
1045 |
try: |
|
aa55498…
|
lmata
|
1046 |
store = _get_store(db) |
|
aa55498…
|
lmata
|
1047 |
ingester = RepoIngester(store) |
|
aa55498…
|
lmata
|
1048 |
stats = ingester.ingest(repo_path, clear=clear, incremental=incremental) |
|
aa55498…
|
lmata
|
1049 |
data = stats |
|
aa55498…
|
lmata
|
1050 |
if stats.get("files", 0) == 0: |
|
aa55498…
|
lmata
|
1051 |
reporter.add_warning("No source files were ingested.") |
|
aa55498…
|
lmata
|
1052 |
except Exception as exc: # noqa: BLE001 |
|
aa55498…
|
lmata
|
1053 |
reporter.add_error(str(exc)) |
|
aa55498…
|
lmata
|
1054 |
|
|
aa55498…
|
lmata
|
1055 |
reporter.emit(data=data or None) |
|
aa55498…
|
lmata
|
1056 |
sys.exit(reporter.exit_code()) |
|
aa55498…
|
lmata
|
1057 |
|
|
aa55498…
|
lmata
|
1058 |
|
|
aa55498…
|
lmata
|
1059 |
@ci.command("stats") |
|
aa55498…
|
lmata
|
1060 |
@DB_OPTION |
|
aa55498…
|
lmata
|
1061 |
def ci_stats(db: str): |
|
aa55498…
|
lmata
|
1062 |
"""Emit graph statistics as JSON (for CI consumption).""" |
|
aa55498…
|
lmata
|
1063 |
import sys |
|
aa55498…
|
lmata
|
1064 |
|
|
aa55498…
|
lmata
|
1065 |
from navegador.cicd import CICDReporter |
|
aa55498…
|
lmata
|
1066 |
from navegador.graph import queries as q |
|
aa55498…
|
lmata
|
1067 |
|
|
aa55498…
|
lmata
|
1068 |
reporter = CICDReporter() |
|
aa55498…
|
lmata
|
1069 |
data: dict = {} |
|
aa55498…
|
lmata
|
1070 |
|
|
aa55498…
|
lmata
|
1071 |
try: |
|
aa55498…
|
lmata
|
1072 |
store = _get_store(db) |
|
aa55498…
|
lmata
|
1073 |
node_rows = store.query(q.NODE_TYPE_COUNTS).result_set or [] |
|
aa55498…
|
lmata
|
1074 |
edge_rows = store.query(q.EDGE_TYPE_COUNTS).result_set or [] |
|
aa55498…
|
lmata
|
1075 |
data = { |
|
aa55498…
|
lmata
|
1076 |
"total_nodes": sum(r[1] for r in node_rows), |
|
aa55498…
|
lmata
|
1077 |
"total_edges": sum(r[1] for r in edge_rows), |
|
aa55498…
|
lmata
|
1078 |
"nodes": {r[0]: r[1] for r in node_rows}, |
|
aa55498…
|
lmata
|
1079 |
"edges": {r[0]: r[1] for r in edge_rows}, |
|
aa55498…
|
lmata
|
1080 |
} |
|
aa55498…
|
lmata
|
1081 |
except Exception as exc: # noqa: BLE001 |
|
aa55498…
|
lmata
|
1082 |
reporter.add_error(str(exc)) |
|
aa55498…
|
lmata
|
1083 |
|
|
aa55498…
|
lmata
|
1084 |
reporter.emit(data=data or None) |
|
aa55498…
|
lmata
|
1085 |
sys.exit(reporter.exit_code()) |
|
aa55498…
|
lmata
|
1086 |
|
|
aa55498…
|
lmata
|
1087 |
|
|
aa55498…
|
lmata
|
1088 |
@ci.command("check") |
|
aa55498…
|
lmata
|
1089 |
@DB_OPTION |
|
aa55498…
|
lmata
|
1090 |
def ci_check(db: str): |
|
aa55498…
|
lmata
|
1091 |
"""Check schema version — exits 2 if migration is needed, 1 on hard error.""" |
|
aa55498…
|
lmata
|
1092 |
import sys |
|
aa55498…
|
lmata
|
1093 |
|
|
aa55498…
|
lmata
|
1094 |
from navegador.cicd import CICDReporter |
|
aa55498…
|
lmata
|
1095 |
from navegador.graph.migrations import ( |
|
aa55498…
|
lmata
|
1096 |
CURRENT_SCHEMA_VERSION, |
|
aa55498…
|
lmata
|
1097 |
get_schema_version, |
|
aa55498…
|
lmata
|
1098 |
needs_migration, |
|
aa55498…
|
lmata
|
1099 |
) |
|
aa55498…
|
lmata
|
1100 |
|
|
aa55498…
|
lmata
|
1101 |
reporter = CICDReporter() |
|
aa55498…
|
lmata
|
1102 |
data: dict = {} |
|
aa55498…
|
lmata
|
1103 |
|
|
aa55498…
|
lmata
|
1104 |
try: |
|
aa55498…
|
lmata
|
1105 |
store = _get_store(db) |
|
aa55498…
|
lmata
|
1106 |
current = get_schema_version(store) |
|
aa55498…
|
lmata
|
1107 |
data = {"schema_version": current, "current_schema_version": CURRENT_SCHEMA_VERSION} |
|
aa55498…
|
lmata
|
1108 |
if needs_migration(store): |
|
e5e1a3b…
|
lmata
|
1109 |
reporter.add_warning(f"Schema migration needed: v{current} → v{CURRENT_SCHEMA_VERSION}") |
|
aa55498…
|
lmata
|
1110 |
except Exception as exc: # noqa: BLE001 |
|
aa55498…
|
lmata
|
1111 |
reporter.add_error(str(exc)) |
|
aa55498…
|
lmata
|
1112 |
|
|
aa55498…
|
lmata
|
1113 |
reporter.emit(data=data or None) |
|
aa55498…
|
lmata
|
1114 |
sys.exit(reporter.exit_code()) |
|
aa55498…
|
lmata
|
1115 |
|
|
aa55498…
|
lmata
|
1116 |
|
|
b3b8664…
|
lmata
|
1117 |
# ── Shell completions ───────────────────────────────────────────────────────── |
|
b3b8664…
|
lmata
|
1118 |
|
|
b3b8664…
|
lmata
|
1119 |
|
|
b3b8664…
|
lmata
|
1120 |
@main.command() |
|
b3b8664…
|
lmata
|
1121 |
@click.argument("shell", type=click.Choice(["bash", "zsh", "fish"])) |
|
b3b8664…
|
lmata
|
1122 |
@click.option( |
|
b3b8664…
|
lmata
|
1123 |
"--install", |
|
b3b8664…
|
lmata
|
1124 |
"do_install", |
|
b3b8664…
|
lmata
|
1125 |
is_flag=True, |
|
b3b8664…
|
lmata
|
1126 |
help="Append the completion line to the default shell rc file.", |
|
b3b8664…
|
lmata
|
1127 |
) |
|
b3b8664…
|
lmata
|
1128 |
@click.option( |
|
b3b8664…
|
lmata
|
1129 |
"--rc-path", |
|
b3b8664…
|
lmata
|
1130 |
default="", |
|
b3b8664…
|
lmata
|
1131 |
help="Override the rc file path used by --install.", |
|
b3b8664…
|
lmata
|
1132 |
) |
|
b3b8664…
|
lmata
|
1133 |
def completions(shell: str, do_install: bool, rc_path: str): |
|
b3b8664…
|
lmata
|
1134 |
"""Print (or install) tab-completion for bash, zsh, or fish. |
|
b3b8664…
|
lmata
|
1135 |
|
|
b3b8664…
|
lmata
|
1136 |
\b |
|
b3b8664…
|
lmata
|
1137 |
Print the line to add manually: |
|
b3b8664…
|
lmata
|
1138 |
navegador completions bash |
|
b3b8664…
|
lmata
|
1139 |
navegador completions zsh |
|
b3b8664…
|
lmata
|
1140 |
navegador completions fish |
|
b3b8664…
|
lmata
|
1141 |
|
|
b3b8664…
|
lmata
|
1142 |
\b |
|
b3b8664…
|
lmata
|
1143 |
Auto-append to your rc file: |
|
b3b8664…
|
lmata
|
1144 |
navegador completions bash --install |
|
b3b8664…
|
lmata
|
1145 |
navegador completions zsh --install |
|
b3b8664…
|
lmata
|
1146 |
navegador completions fish --install |
|
b3b8664…
|
lmata
|
1147 |
""" |
|
b3b8664…
|
lmata
|
1148 |
from navegador.completions import get_eval_line, get_rc_path, install_completion |
|
b3b8664…
|
lmata
|
1149 |
|
|
b3b8664…
|
lmata
|
1150 |
if do_install: |
|
b3b8664…
|
lmata
|
1151 |
target = install_completion(shell, rc_path=rc_path or None) |
|
b3b8664…
|
lmata
|
1152 |
console.print(f"[green]Completion installed[/green] → {target}") |
|
b3b8664…
|
lmata
|
1153 |
console.print(f"Restart your shell or run: [bold]source {target}[/bold]") |
|
b3b8664…
|
lmata
|
1154 |
else: |
|
b3b8664…
|
lmata
|
1155 |
line = get_eval_line(shell) |
|
b3b8664…
|
lmata
|
1156 |
rc = rc_path or get_rc_path(shell) |
|
b3b8664…
|
lmata
|
1157 |
console.print(f"Add the following line to [bold]{rc}[/bold]:\n") |
|
b3b8664…
|
lmata
|
1158 |
click.echo(f" {line}") |
|
e5e1a3b…
|
lmata
|
1159 |
console.print(f"\nOr run: [bold]navegador completions {shell} --install[/bold]") |
|
33fd0c0…
|
lmata
|
1160 |
|
|
33fd0c0…
|
lmata
|
1161 |
|
|
33fd0c0…
|
lmata
|
1162 |
# ── Churn / behavioural coupling ───────────────────────────────────────────── |
|
33fd0c0…
|
lmata
|
1163 |
|
|
33fd0c0…
|
lmata
|
1164 |
|
|
33fd0c0…
|
lmata
|
1165 |
@main.command() |
|
33fd0c0…
|
lmata
|
1166 |
@click.argument("repo_path", default=".", type=click.Path(exists=True)) |
|
33fd0c0…
|
lmata
|
1167 |
@DB_OPTION |
|
33fd0c0…
|
lmata
|
1168 |
@click.option("--limit", default=500, show_default=True, help="Max commits to inspect.") |
|
33fd0c0…
|
lmata
|
1169 |
@click.option( |
|
33fd0c0…
|
lmata
|
1170 |
"--min-confidence", |
|
33fd0c0…
|
lmata
|
1171 |
default=0.5, |
|
33fd0c0…
|
lmata
|
1172 |
show_default=True, |
|
33fd0c0…
|
lmata
|
1173 |
type=float, |
|
33fd0c0…
|
lmata
|
1174 |
help="Minimum coupling confidence (0–1).", |
|
33fd0c0…
|
lmata
|
1175 |
) |
|
33fd0c0…
|
lmata
|
1176 |
@click.option( |
|
33fd0c0…
|
lmata
|
1177 |
"--min-co-changes", |
|
33fd0c0…
|
lmata
|
1178 |
default=3, |
|
33fd0c0…
|
lmata
|
1179 |
show_default=True, |
|
33fd0c0…
|
lmata
|
1180 |
type=int, |
|
33fd0c0…
|
lmata
|
1181 |
help="Minimum co-change count for a coupling pair.", |
|
33fd0c0…
|
lmata
|
1182 |
) |
|
33fd0c0…
|
lmata
|
1183 |
@click.option("--store", "do_store", is_flag=True, help="Write results to the graph.") |
|
33fd0c0…
|
lmata
|
1184 |
@click.option("--json", "as_json", is_flag=True, help="Output results as JSON.") |
|
33fd0c0…
|
lmata
|
1185 |
def churn( |
|
33fd0c0…
|
lmata
|
1186 |
repo_path: str, |
|
33fd0c0…
|
lmata
|
1187 |
db: str, |
|
33fd0c0…
|
lmata
|
1188 |
limit: int, |
|
33fd0c0…
|
lmata
|
1189 |
min_confidence: float, |
|
33fd0c0…
|
lmata
|
1190 |
min_co_changes: int, |
|
33fd0c0…
|
lmata
|
1191 |
do_store: bool, |
|
33fd0c0…
|
lmata
|
1192 |
as_json: bool, |
|
33fd0c0…
|
lmata
|
1193 |
): |
|
33fd0c0…
|
lmata
|
1194 |
"""Analyze git history for file churn and behavioural coupling. |
|
33fd0c0…
|
lmata
|
1195 |
|
|
33fd0c0…
|
lmata
|
1196 |
Shows files that change most often and pairs of files that |
|
33fd0c0…
|
lmata
|
1197 |
frequently change together (co-evolution / logical coupling). |
|
33fd0c0…
|
lmata
|
1198 |
|
|
33fd0c0…
|
lmata
|
1199 |
\b |
|
33fd0c0…
|
lmata
|
1200 |
Examples: |
|
33fd0c0…
|
lmata
|
1201 |
navegador churn . |
|
33fd0c0…
|
lmata
|
1202 |
navegador churn . --limit 200 --min-confidence 0.7 |
|
33fd0c0…
|
lmata
|
1203 |
navegador churn . --store # persist to graph |
|
33fd0c0…
|
lmata
|
1204 |
navegador churn . --json # machine-readable output |
|
33fd0c0…
|
lmata
|
1205 |
""" |
|
33fd0c0…
|
lmata
|
1206 |
from pathlib import Path as P |
|
33fd0c0…
|
lmata
|
1207 |
|
|
33fd0c0…
|
lmata
|
1208 |
from navegador.churn import ChurnAnalyzer |
|
33fd0c0…
|
lmata
|
1209 |
|
|
33fd0c0…
|
lmata
|
1210 |
analyzer = ChurnAnalyzer(P(repo_path), limit=limit) |
|
33fd0c0…
|
lmata
|
1211 |
|
|
33fd0c0…
|
lmata
|
1212 |
with console.status("[bold]Analysing git history…[/bold]"): |
|
33fd0c0…
|
lmata
|
1213 |
churn_entries = analyzer.file_churn() |
|
33fd0c0…
|
lmata
|
1214 |
pairs = analyzer.coupling_pairs( |
|
33fd0c0…
|
lmata
|
1215 |
min_co_changes=min_co_changes, min_confidence=min_confidence |
|
33fd0c0…
|
lmata
|
1216 |
) |
|
33fd0c0…
|
lmata
|
1217 |
|
|
33fd0c0…
|
lmata
|
1218 |
if do_store: |
|
33fd0c0…
|
lmata
|
1219 |
store = _get_store(db) |
|
33fd0c0…
|
lmata
|
1220 |
stats = analyzer.store_churn(store) |
|
33fd0c0…
|
lmata
|
1221 |
if as_json: |
|
33fd0c0…
|
lmata
|
1222 |
click.echo(json.dumps(stats, indent=2)) |
|
33fd0c0…
|
lmata
|
1223 |
else: |
|
33fd0c0…
|
lmata
|
1224 |
console.print( |
|
33fd0c0…
|
lmata
|
1225 |
f"[green]Churn stored:[/green] " |
|
33fd0c0…
|
lmata
|
1226 |
f"{stats['churn_updated']} files updated, " |
|
33fd0c0…
|
lmata
|
1227 |
f"{stats['couplings_written']} coupling edges written" |
|
33fd0c0…
|
lmata
|
1228 |
) |
|
33fd0c0…
|
lmata
|
1229 |
return |
|
33fd0c0…
|
lmata
|
1230 |
|
|
33fd0c0…
|
lmata
|
1231 |
if as_json: |
|
33fd0c0…
|
lmata
|
1232 |
click.echo( |
|
33fd0c0…
|
lmata
|
1233 |
json.dumps( |
|
33fd0c0…
|
lmata
|
1234 |
{ |
|
33fd0c0…
|
lmata
|
1235 |
"churn": [ |
|
33fd0c0…
|
lmata
|
1236 |
{ |
|
33fd0c0…
|
lmata
|
1237 |
"file_path": e.file_path, |
|
33fd0c0…
|
lmata
|
1238 |
"commit_count": e.commit_count, |
|
33fd0c0…
|
lmata
|
1239 |
"lines_changed": e.lines_changed, |
|
33fd0c0…
|
lmata
|
1240 |
} |
|
33fd0c0…
|
lmata
|
1241 |
for e in churn_entries |
|
33fd0c0…
|
lmata
|
1242 |
], |
|
33fd0c0…
|
lmata
|
1243 |
"coupling_pairs": [ |
|
33fd0c0…
|
lmata
|
1244 |
{ |
|
33fd0c0…
|
lmata
|
1245 |
"file_a": p.file_a, |
|
33fd0c0…
|
lmata
|
1246 |
"file_b": p.file_b, |
|
33fd0c0…
|
lmata
|
1247 |
"co_change_count": p.co_change_count, |
|
33fd0c0…
|
lmata
|
1248 |
"confidence": p.confidence, |
|
33fd0c0…
|
lmata
|
1249 |
} |
|
33fd0c0…
|
lmata
|
1250 |
for p in pairs |
|
33fd0c0…
|
lmata
|
1251 |
], |
|
33fd0c0…
|
lmata
|
1252 |
}, |
|
33fd0c0…
|
lmata
|
1253 |
indent=2, |
|
33fd0c0…
|
lmata
|
1254 |
) |
|
33fd0c0…
|
lmata
|
1255 |
) |
|
33fd0c0…
|
lmata
|
1256 |
return |
|
33fd0c0…
|
lmata
|
1257 |
|
|
33fd0c0…
|
lmata
|
1258 |
# ── Rich tables ─────────────────────────────────────────────────────────── |
|
33fd0c0…
|
lmata
|
1259 |
churn_table = Table(title=f"File churn (top {min(20, len(churn_entries))})") |
|
33fd0c0…
|
lmata
|
1260 |
churn_table.add_column("File", style="cyan") |
|
33fd0c0…
|
lmata
|
1261 |
churn_table.add_column("Commits", justify="right", style="green") |
|
33fd0c0…
|
lmata
|
1262 |
churn_table.add_column("Lines changed", justify="right") |
|
33fd0c0…
|
lmata
|
1263 |
for entry in churn_entries[:20]: |
|
33fd0c0…
|
lmata
|
1264 |
churn_table.add_row(entry.file_path, str(entry.commit_count), str(entry.lines_changed)) |
|
33fd0c0…
|
lmata
|
1265 |
console.print(churn_table) |
|
33fd0c0…
|
lmata
|
1266 |
|
|
33fd0c0…
|
lmata
|
1267 |
if pairs: |
|
33fd0c0…
|
lmata
|
1268 |
pair_table = Table(title=f"Behavioural coupling ({len(pairs)} pairs)") |
|
33fd0c0…
|
lmata
|
1269 |
pair_table.add_column("File A", style="cyan") |
|
33fd0c0…
|
lmata
|
1270 |
pair_table.add_column("File B", style="cyan") |
|
33fd0c0…
|
lmata
|
1271 |
pair_table.add_column("Co-changes", justify="right", style="green") |
|
33fd0c0…
|
lmata
|
1272 |
pair_table.add_column("Confidence", justify="right") |
|
33fd0c0…
|
lmata
|
1273 |
for pair in pairs[:20]: |
|
33fd0c0…
|
lmata
|
1274 |
pair_table.add_row( |
|
33fd0c0…
|
lmata
|
1275 |
pair.file_a, |
|
33fd0c0…
|
lmata
|
1276 |
pair.file_b, |
|
33fd0c0…
|
lmata
|
1277 |
str(pair.co_change_count), |
|
33fd0c0…
|
lmata
|
1278 |
f"{pair.confidence:.2f}", |
|
33fd0c0…
|
lmata
|
1279 |
) |
|
33fd0c0…
|
lmata
|
1280 |
console.print(pair_table) |
|
33fd0c0…
|
lmata
|
1281 |
else: |
|
b3b8664…
|
lmata
|
1282 |
console.print( |
|
33fd0c0…
|
lmata
|
1283 |
f"[yellow]No coupling pairs found[/yellow] " |
|
33fd0c0…
|
lmata
|
1284 |
f"(min_co_changes={min_co_changes}, min_confidence={min_confidence})" |
|
b3b8664…
|
lmata
|
1285 |
) |
|
b3b8664…
|
lmata
|
1286 |
|
|
b3b8664…
|
lmata
|
1287 |
|
|
4be3595…
|
lmata
|
1288 |
# ── MCP ─────────────────────────────────────────────────────────────────────── |
|
4be3595…
|
lmata
|
1289 |
|
|
758c6b7…
|
lmata
|
1290 |
|
|
5e4b8e4…
|
anonymous
|
1291 |
@main.command() |
|
e72710e…
|
lmata
|
1292 |
@DB_OPTION |
|
b3b8664…
|
lmata
|
1293 |
@click.option( |
|
b3b8664…
|
lmata
|
1294 |
"--read-only", |
|
b3b8664…
|
lmata
|
1295 |
"read_only", |
|
b3b8664…
|
lmata
|
1296 |
is_flag=True, |
|
b3b8664…
|
lmata
|
1297 |
default=False, |
|
b3b8664…
|
lmata
|
1298 |
help=( |
|
e5e1a3b…
|
lmata
|
1299 |
"Start in read-only mode: disables ingest_repo and blocks write operations in query_graph." |
|
b3b8664…
|
lmata
|
1300 |
), |
|
b3b8664…
|
lmata
|
1301 |
) |
|
b3b8664…
|
lmata
|
1302 |
def mcp(db: str, read_only: bool): |
|
e72710e…
|
lmata
|
1303 |
"""Start the MCP server for AI agent integration (stdio).""" |
|
5e4b8e4…
|
anonymous
|
1304 |
from mcp.server.stdio import stdio_server # type: ignore[import] |
|
5e4b8e4…
|
anonymous
|
1305 |
|
|
3c74e66…
|
lmata
|
1306 |
from navegador.mcp import create_mcp_server |
|
5e4b8e4…
|
anonymous
|
1307 |
|
|
b3b8664…
|
lmata
|
1308 |
server = create_mcp_server(lambda: _get_store(db), read_only=read_only) |
|
b3b8664…
|
lmata
|
1309 |
mode = "read-only" if read_only else "read-write" |
|
b3b8664…
|
lmata
|
1310 |
console.print(f"[green]Navegador MCP server running[/green] (stdio, {mode})") |
|
5e4b8e4…
|
anonymous
|
1311 |
|
|
5e4b8e4…
|
anonymous
|
1312 |
async def _run(): |
|
5e4b8e4…
|
anonymous
|
1313 |
async with stdio_server() as (read_stream, write_stream): |
|
5e4b8e4…
|
anonymous
|
1314 |
await server.run(read_stream, write_stream, server.create_initialization_options()) |
|
5e4b8e4…
|
anonymous
|
1315 |
|
|
5e4b8e4…
|
anonymous
|
1316 |
asyncio.run(_run()) |
|
1d4baaf…
|
lmata
|
1317 |
|
|
1d4baaf…
|
lmata
|
1318 |
|
|
1d4baaf…
|
lmata
|
1319 |
# ── ANALYSIS: impact ────────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1320 |
|
|
1d4baaf…
|
lmata
|
1321 |
|
|
1d4baaf…
|
lmata
|
1322 |
@main.command() |
|
1d4baaf…
|
lmata
|
1323 |
@click.argument("name") |
|
1d4baaf…
|
lmata
|
1324 |
@click.option("--file", "file_path", default="", help="Narrow to a specific file.") |
|
1d4baaf…
|
lmata
|
1325 |
@click.option("--depth", default=3, show_default=True, help="Traversal depth.") |
|
1d4baaf…
|
lmata
|
1326 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1327 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
1d4baaf…
|
lmata
|
1328 |
def impact(name: str, file_path: str, depth: int, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1329 |
"""Blast-radius analysis — what does changing NAME affect? |
|
1d4baaf…
|
lmata
|
1330 |
|
|
1d4baaf…
|
lmata
|
1331 |
Traverses CALLS, REFERENCES, INHERITS, IMPLEMENTS, ANNOTATES edges |
|
1d4baaf…
|
lmata
|
1332 |
outward to find all downstream symbols and files affected by a change. |
|
1d4baaf…
|
lmata
|
1333 |
""" |
|
1d4baaf…
|
lmata
|
1334 |
from navegador.analysis.impact import ImpactAnalyzer |
|
1d4baaf…
|
lmata
|
1335 |
|
|
1d4baaf…
|
lmata
|
1336 |
result = ImpactAnalyzer(_get_store(db)).blast_radius(name, file_path=file_path, depth=depth) |
|
1d4baaf…
|
lmata
|
1337 |
|
|
1d4baaf…
|
lmata
|
1338 |
if as_json: |
|
1d4baaf…
|
lmata
|
1339 |
click.echo(json.dumps(result.to_dict(), indent=2)) |
|
1d4baaf…
|
lmata
|
1340 |
return |
|
1d4baaf…
|
lmata
|
1341 |
|
|
e5e1a3b…
|
lmata
|
1342 |
console.print(f"[bold]Blast radius:[/bold] [cyan]{name}[/cyan] (depth={depth})") |
|
1d4baaf…
|
lmata
|
1343 |
if not result.affected_nodes: |
|
1d4baaf…
|
lmata
|
1344 |
console.print("[yellow]No affected nodes found.[/yellow]") |
|
1d4baaf…
|
lmata
|
1345 |
return |
|
1d4baaf…
|
lmata
|
1346 |
|
|
1d4baaf…
|
lmata
|
1347 |
table = Table(title=f"Affected nodes ({len(result.affected_nodes)})") |
|
1d4baaf…
|
lmata
|
1348 |
table.add_column("Type", style="cyan") |
|
1d4baaf…
|
lmata
|
1349 |
table.add_column("Name", style="bold") |
|
1d4baaf…
|
lmata
|
1350 |
table.add_column("File") |
|
1d4baaf…
|
lmata
|
1351 |
table.add_column("Line", justify="right") |
|
1d4baaf…
|
lmata
|
1352 |
for node in result.affected_nodes: |
|
e5e1a3b…
|
lmata
|
1353 |
table.add_row(node["type"], node["name"], node["file_path"], str(node["line_start"] or "")) |
|
1d4baaf…
|
lmata
|
1354 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1355 |
|
|
1d4baaf…
|
lmata
|
1356 |
if result.affected_files: |
|
1d4baaf…
|
lmata
|
1357 |
console.print(f"\n[bold]Affected files ({len(result.affected_files)}):[/bold]") |
|
1d4baaf…
|
lmata
|
1358 |
for fp in result.affected_files: |
|
1d4baaf…
|
lmata
|
1359 |
console.print(f" {fp}") |
|
1d4baaf…
|
lmata
|
1360 |
|
|
1d4baaf…
|
lmata
|
1361 |
if result.affected_knowledge: |
|
1d4baaf…
|
lmata
|
1362 |
console.print(f"\n[bold]Affected knowledge ({len(result.affected_knowledge)}):[/bold]") |
|
1d4baaf…
|
lmata
|
1363 |
for kn in result.affected_knowledge: |
|
1d4baaf…
|
lmata
|
1364 |
console.print(f" [{kn['type']}] {kn['name']}") |
|
1d4baaf…
|
lmata
|
1365 |
|
|
1d4baaf…
|
lmata
|
1366 |
|
|
1d4baaf…
|
lmata
|
1367 |
# ── ANALYSIS: flow trace ────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1368 |
|
|
1d4baaf…
|
lmata
|
1369 |
|
|
1d4baaf…
|
lmata
|
1370 |
@main.command() |
|
1d4baaf…
|
lmata
|
1371 |
@click.argument("name") |
|
1d4baaf…
|
lmata
|
1372 |
@click.option("--file", "file_path", default="", help="Narrow to a specific file.") |
|
1d4baaf…
|
lmata
|
1373 |
@click.option("--depth", default=10, show_default=True, help="Maximum call depth.") |
|
1d4baaf…
|
lmata
|
1374 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1375 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
1d4baaf…
|
lmata
|
1376 |
def trace(name: str, file_path: str, depth: int, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1377 |
"""Execution flow trace — follow call chains from an entry point. |
|
1d4baaf…
|
lmata
|
1378 |
|
|
1d4baaf…
|
lmata
|
1379 |
Traverses CALLS edges forward from NAME, returning all execution paths |
|
1d4baaf…
|
lmata
|
1380 |
up to the given depth. |
|
1d4baaf…
|
lmata
|
1381 |
""" |
|
1d4baaf…
|
lmata
|
1382 |
from navegador.analysis.flow import FlowTracer |
|
1d4baaf…
|
lmata
|
1383 |
|
|
1d4baaf…
|
lmata
|
1384 |
chains = FlowTracer(_get_store(db)).trace(name, file_path=file_path, max_depth=depth) |
|
1d4baaf…
|
lmata
|
1385 |
|
|
1d4baaf…
|
lmata
|
1386 |
if as_json: |
|
1d4baaf…
|
lmata
|
1387 |
click.echo(json.dumps([c.to_list() for c in chains], indent=2)) |
|
1d4baaf…
|
lmata
|
1388 |
return |
|
1d4baaf…
|
lmata
|
1389 |
|
|
1d4baaf…
|
lmata
|
1390 |
if not chains: |
|
1d4baaf…
|
lmata
|
1391 |
console.print(f"[yellow]No call chains found from[/yellow] [cyan]{name}[/cyan].") |
|
1d4baaf…
|
lmata
|
1392 |
return |
|
1d4baaf…
|
lmata
|
1393 |
|
|
e5e1a3b…
|
lmata
|
1394 |
console.print(f"[bold]Call chains from[/bold] [cyan]{name}[/cyan] — {len(chains)} path(s)") |
|
1d4baaf…
|
lmata
|
1395 |
for i, chain in enumerate(chains, 1): |
|
1d4baaf…
|
lmata
|
1396 |
steps = chain.to_list() |
|
e5e1a3b…
|
lmata
|
1397 |
path_str = ( |
|
e5e1a3b…
|
lmata
|
1398 |
" → ".join([steps[0]["caller"]] + [s["callee"] for s in steps]) if steps else name |
|
e5e1a3b…
|
lmata
|
1399 |
) |
|
1d4baaf…
|
lmata
|
1400 |
console.print(f" {i}. {path_str}") |
|
1d4baaf…
|
lmata
|
1401 |
|
|
1d4baaf…
|
lmata
|
1402 |
|
|
1d4baaf…
|
lmata
|
1403 |
# ── ANALYSIS: dead code ─────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1404 |
|
|
1d4baaf…
|
lmata
|
1405 |
|
|
1d4baaf…
|
lmata
|
1406 |
@main.command() |
|
1d4baaf…
|
lmata
|
1407 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1408 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
1d4baaf…
|
lmata
|
1409 |
def deadcode(db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1410 |
"""Detect dead code — unreachable functions, classes, and orphan files. |
|
1d4baaf…
|
lmata
|
1411 |
|
|
1d4baaf…
|
lmata
|
1412 |
A function/class is dead if nothing calls, references, or imports it. |
|
1d4baaf…
|
lmata
|
1413 |
An orphan file is one that no other file imports. |
|
1d4baaf…
|
lmata
|
1414 |
""" |
|
1d4baaf…
|
lmata
|
1415 |
from navegador.analysis.deadcode import DeadCodeDetector |
|
1d4baaf…
|
lmata
|
1416 |
|
|
1d4baaf…
|
lmata
|
1417 |
report = DeadCodeDetector(_get_store(db)).detect() |
|
1d4baaf…
|
lmata
|
1418 |
|
|
1d4baaf…
|
lmata
|
1419 |
if as_json: |
|
1d4baaf…
|
lmata
|
1420 |
click.echo(json.dumps(report.to_dict(), indent=2)) |
|
1d4baaf…
|
lmata
|
1421 |
return |
|
1d4baaf…
|
lmata
|
1422 |
|
|
1d4baaf…
|
lmata
|
1423 |
summary = report.to_dict()["summary"] |
|
1d4baaf…
|
lmata
|
1424 |
console.print( |
|
1d4baaf…
|
lmata
|
1425 |
f"[bold]Dead code report:[/bold] " |
|
1d4baaf…
|
lmata
|
1426 |
f"{summary['unreachable_functions']} dead functions, " |
|
1d4baaf…
|
lmata
|
1427 |
f"{summary['unreachable_classes']} dead classes, " |
|
1d4baaf…
|
lmata
|
1428 |
f"{summary['orphan_files']} orphan files" |
|
1d4baaf…
|
lmata
|
1429 |
) |
|
1d4baaf…
|
lmata
|
1430 |
|
|
1d4baaf…
|
lmata
|
1431 |
if report.unreachable_functions: |
|
e5e1a3b…
|
lmata
|
1432 |
fn_table = Table( |
|
e5e1a3b…
|
lmata
|
1433 |
title=f"Unreachable functions/methods ({len(report.unreachable_functions)})" |
|
e5e1a3b…
|
lmata
|
1434 |
) |
|
1d4baaf…
|
lmata
|
1435 |
fn_table.add_column("Type", style="cyan") |
|
1d4baaf…
|
lmata
|
1436 |
fn_table.add_column("Name", style="bold") |
|
1d4baaf…
|
lmata
|
1437 |
fn_table.add_column("File") |
|
1d4baaf…
|
lmata
|
1438 |
fn_table.add_column("Line", justify="right") |
|
1d4baaf…
|
lmata
|
1439 |
for fn in report.unreachable_functions: |
|
1d4baaf…
|
lmata
|
1440 |
fn_table.add_row(fn["type"], fn["name"], fn["file_path"], str(fn["line_start"] or "")) |
|
1d4baaf…
|
lmata
|
1441 |
console.print(fn_table) |
|
1d4baaf…
|
lmata
|
1442 |
|
|
1d4baaf…
|
lmata
|
1443 |
if report.unreachable_classes: |
|
1d4baaf…
|
lmata
|
1444 |
cls_table = Table(title=f"Unreachable classes ({len(report.unreachable_classes)})") |
|
1d4baaf…
|
lmata
|
1445 |
cls_table.add_column("Name", style="bold") |
|
1d4baaf…
|
lmata
|
1446 |
cls_table.add_column("File") |
|
1d4baaf…
|
lmata
|
1447 |
cls_table.add_column("Line", justify="right") |
|
1d4baaf…
|
lmata
|
1448 |
for cls in report.unreachable_classes: |
|
1d4baaf…
|
lmata
|
1449 |
cls_table.add_row(cls["name"], cls["file_path"], str(cls["line_start"] or "")) |
|
1d4baaf…
|
lmata
|
1450 |
console.print(cls_table) |
|
1d4baaf…
|
lmata
|
1451 |
|
|
1d4baaf…
|
lmata
|
1452 |
if report.orphan_files: |
|
1d4baaf…
|
lmata
|
1453 |
console.print(f"\n[bold]Orphan files ({len(report.orphan_files)}):[/bold]") |
|
1d4baaf…
|
lmata
|
1454 |
for fp in report.orphan_files: |
|
1d4baaf…
|
lmata
|
1455 |
console.print(f" {fp}") |
|
1d4baaf…
|
lmata
|
1456 |
|
|
1d4baaf…
|
lmata
|
1457 |
if not any([report.unreachable_functions, report.unreachable_classes, report.orphan_files]): |
|
1d4baaf…
|
lmata
|
1458 |
console.print("[green]No dead code found.[/green]") |
|
1d4baaf…
|
lmata
|
1459 |
|
|
1d4baaf…
|
lmata
|
1460 |
|
|
1d4baaf…
|
lmata
|
1461 |
# ── ANALYSIS: test mapping ──────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1462 |
|
|
1d4baaf…
|
lmata
|
1463 |
|
|
1d4baaf…
|
lmata
|
1464 |
@main.command() |
|
1d4baaf…
|
lmata
|
1465 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1466 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
1d4baaf…
|
lmata
|
1467 |
def testmap(db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1468 |
"""Map test functions to production code via TESTS edges. |
|
1d4baaf…
|
lmata
|
1469 |
|
|
1d4baaf…
|
lmata
|
1470 |
Finds functions starting with test_, resolves the production symbol |
|
1d4baaf…
|
lmata
|
1471 |
via CALLS edges and name heuristics, then writes TESTS edges to the graph. |
|
1d4baaf…
|
lmata
|
1472 |
""" |
|
1d4baaf…
|
lmata
|
1473 |
from navegador.analysis.testmap import TestMapper |
|
1d4baaf…
|
lmata
|
1474 |
|
|
1d4baaf…
|
lmata
|
1475 |
result = TestMapper(_get_store(db)).map_tests() |
|
1d4baaf…
|
lmata
|
1476 |
|
|
1d4baaf…
|
lmata
|
1477 |
if as_json: |
|
1d4baaf…
|
lmata
|
1478 |
click.echo(json.dumps(result.to_dict(), indent=2)) |
|
1d4baaf…
|
lmata
|
1479 |
return |
|
1d4baaf…
|
lmata
|
1480 |
|
|
1d4baaf…
|
lmata
|
1481 |
console.print( |
|
1d4baaf…
|
lmata
|
1482 |
f"[bold]Test map:[/bold] {len(result.links)} linked, " |
|
1d4baaf…
|
lmata
|
1483 |
f"{len(result.unmatched_tests)} unmatched, " |
|
1d4baaf…
|
lmata
|
1484 |
f"{result.edges_created} TESTS edges created" |
|
1d4baaf…
|
lmata
|
1485 |
) |
|
1d4baaf…
|
lmata
|
1486 |
|
|
1d4baaf…
|
lmata
|
1487 |
if result.links: |
|
1d4baaf…
|
lmata
|
1488 |
table = Table(title=f"Test -> production links ({len(result.links)})") |
|
1d4baaf…
|
lmata
|
1489 |
table.add_column("Test", style="cyan") |
|
1d4baaf…
|
lmata
|
1490 |
table.add_column("Production symbol", style="bold") |
|
1d4baaf…
|
lmata
|
1491 |
table.add_column("File") |
|
1d4baaf…
|
lmata
|
1492 |
table.add_column("Source") |
|
1d4baaf…
|
lmata
|
1493 |
for lnk in result.links: |
|
1d4baaf…
|
lmata
|
1494 |
table.add_row(lnk.test_name, lnk.prod_name, lnk.prod_file, lnk.source) |
|
1d4baaf…
|
lmata
|
1495 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1496 |
|
|
1d4baaf…
|
lmata
|
1497 |
if result.unmatched_tests: |
|
1d4baaf…
|
lmata
|
1498 |
console.print(f"\n[yellow]Unmatched tests ({len(result.unmatched_tests)}):[/yellow]") |
|
1d4baaf…
|
lmata
|
1499 |
for t in result.unmatched_tests: |
|
1d4baaf…
|
lmata
|
1500 |
console.print(f" {t['name']} ({t['file_path']})") |
|
1d4baaf…
|
lmata
|
1501 |
|
|
1d4baaf…
|
lmata
|
1502 |
|
|
1d4baaf…
|
lmata
|
1503 |
# ── ANALYSIS: cycles ────────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1504 |
|
|
1d4baaf…
|
lmata
|
1505 |
|
|
1d4baaf…
|
lmata
|
1506 |
@main.command() |
|
1d4baaf…
|
lmata
|
1507 |
@DB_OPTION |
|
e5e1a3b…
|
lmata
|
1508 |
@click.option( |
|
e5e1a3b…
|
lmata
|
1509 |
"--imports", "check_imports", is_flag=True, default=False, help="Check import cycles only." |
|
e5e1a3b…
|
lmata
|
1510 |
) |
|
e5e1a3b…
|
lmata
|
1511 |
@click.option("--calls", "check_calls", is_flag=True, default=False, help="Check call cycles only.") |
|
1d4baaf…
|
lmata
|
1512 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
1d4baaf…
|
lmata
|
1513 |
def cycles(db: str, check_imports: bool, check_calls: bool, as_json: bool): |
|
1d4baaf…
|
lmata
|
1514 |
"""Detect circular dependencies in import and call graphs. |
|
1d4baaf…
|
lmata
|
1515 |
|
|
1d4baaf…
|
lmata
|
1516 |
By default checks both import cycles and call cycles. |
|
1d4baaf…
|
lmata
|
1517 |
Use --imports or --calls to restrict to one graph. |
|
1d4baaf…
|
lmata
|
1518 |
""" |
|
1d4baaf…
|
lmata
|
1519 |
from navegador.analysis.cycles import CycleDetector |
|
1d4baaf…
|
lmata
|
1520 |
|
|
1d4baaf…
|
lmata
|
1521 |
detector = CycleDetector(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1522 |
run_imports = check_imports or (not check_imports and not check_calls) |
|
1d4baaf…
|
lmata
|
1523 |
run_calls = check_calls or (not check_imports and not check_calls) |
|
1d4baaf…
|
lmata
|
1524 |
|
|
1d4baaf…
|
lmata
|
1525 |
import_cycles = detector.detect_import_cycles() if run_imports else [] |
|
1d4baaf…
|
lmata
|
1526 |
call_cycles = detector.detect_call_cycles() if run_calls else [] |
|
1d4baaf…
|
lmata
|
1527 |
|
|
1d4baaf…
|
lmata
|
1528 |
if as_json: |
|
1d4baaf…
|
lmata
|
1529 |
click.echo( |
|
e5e1a3b…
|
lmata
|
1530 |
json.dumps({"import_cycles": import_cycles, "call_cycles": call_cycles}, indent=2) |
|
1d4baaf…
|
lmata
|
1531 |
) |
|
1d4baaf…
|
lmata
|
1532 |
return |
|
1d4baaf…
|
lmata
|
1533 |
|
|
1d4baaf…
|
lmata
|
1534 |
if not import_cycles and not call_cycles: |
|
1d4baaf…
|
lmata
|
1535 |
console.print("[green]No circular dependencies found.[/green]") |
|
1d4baaf…
|
lmata
|
1536 |
return |
|
1d4baaf…
|
lmata
|
1537 |
|
|
1d4baaf…
|
lmata
|
1538 |
if import_cycles: |
|
1d4baaf…
|
lmata
|
1539 |
table = Table(title=f"Import cycles ({len(import_cycles)})") |
|
1d4baaf…
|
lmata
|
1540 |
table.add_column("#", justify="right") |
|
1d4baaf…
|
lmata
|
1541 |
table.add_column("Cycle") |
|
1d4baaf…
|
lmata
|
1542 |
for i, cycle in enumerate(import_cycles, 1): |
|
1d4baaf…
|
lmata
|
1543 |
table.add_row(str(i), " -> ".join(cycle) + f" -> {cycle[0]}") |
|
1d4baaf…
|
lmata
|
1544 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1545 |
|
|
1d4baaf…
|
lmata
|
1546 |
if call_cycles: |
|
1d4baaf…
|
lmata
|
1547 |
table = Table(title=f"Call cycles ({len(call_cycles)})") |
|
1d4baaf…
|
lmata
|
1548 |
table.add_column("#", justify="right") |
|
1d4baaf…
|
lmata
|
1549 |
table.add_column("Cycle") |
|
1d4baaf…
|
lmata
|
1550 |
for i, cycle in enumerate(call_cycles, 1): |
|
1d4baaf…
|
lmata
|
1551 |
table.add_row(str(i), " -> ".join(cycle) + f" -> {cycle[0]}") |
|
1d4baaf…
|
lmata
|
1552 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1553 |
|
|
1d4baaf…
|
lmata
|
1554 |
|
|
1d4baaf…
|
lmata
|
1555 |
# ── Multi-repo (#16) ───────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1556 |
|
|
1d4baaf…
|
lmata
|
1557 |
|
|
1d4baaf…
|
lmata
|
1558 |
@main.group() |
|
1d4baaf…
|
lmata
|
1559 |
def repo(): |
|
1d4baaf…
|
lmata
|
1560 |
"""Manage and query across multiple repositories.""" |
|
1d4baaf…
|
lmata
|
1561 |
|
|
1d4baaf…
|
lmata
|
1562 |
|
|
1d4baaf…
|
lmata
|
1563 |
@repo.command("add") |
|
1d4baaf…
|
lmata
|
1564 |
@click.argument("name") |
|
1d4baaf…
|
lmata
|
1565 |
@click.argument("path", type=click.Path()) |
|
1d4baaf…
|
lmata
|
1566 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1567 |
def repo_add(name: str, path: str, db: str): |
|
1d4baaf…
|
lmata
|
1568 |
"""Register a repository by NAME and PATH.""" |
|
1d4baaf…
|
lmata
|
1569 |
from navegador.multirepo import MultiRepoManager |
|
1d4baaf…
|
lmata
|
1570 |
|
|
1d4baaf…
|
lmata
|
1571 |
mgr = MultiRepoManager(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1572 |
mgr.add_repo(name, path) |
|
1d4baaf…
|
lmata
|
1573 |
console.print(f"[green]Repo registered:[/green] {name} → {path}") |
|
1d4baaf…
|
lmata
|
1574 |
|
|
1d4baaf…
|
lmata
|
1575 |
|
|
1d4baaf…
|
lmata
|
1576 |
@repo.command("list") |
|
1d4baaf…
|
lmata
|
1577 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1578 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1579 |
def repo_list(db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1580 |
"""List all registered repositories.""" |
|
1d4baaf…
|
lmata
|
1581 |
from navegador.multirepo import MultiRepoManager |
|
1d4baaf…
|
lmata
|
1582 |
|
|
1d4baaf…
|
lmata
|
1583 |
repos = MultiRepoManager(_get_store(db)).list_repos() |
|
1d4baaf…
|
lmata
|
1584 |
if as_json: |
|
1d4baaf…
|
lmata
|
1585 |
click.echo(json.dumps(repos, indent=2)) |
|
1d4baaf…
|
lmata
|
1586 |
return |
|
1d4baaf…
|
lmata
|
1587 |
if not repos: |
|
1d4baaf…
|
lmata
|
1588 |
console.print("[yellow]No repositories registered.[/yellow]") |
|
1d4baaf…
|
lmata
|
1589 |
return |
|
1d4baaf…
|
lmata
|
1590 |
table = Table(title="Registered repositories") |
|
1d4baaf…
|
lmata
|
1591 |
table.add_column("Name", style="cyan") |
|
1d4baaf…
|
lmata
|
1592 |
table.add_column("Path") |
|
1d4baaf…
|
lmata
|
1593 |
for r in repos: |
|
1d4baaf…
|
lmata
|
1594 |
table.add_row(r["name"], r["path"]) |
|
1d4baaf…
|
lmata
|
1595 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1596 |
|
|
1d4baaf…
|
lmata
|
1597 |
|
|
1d4baaf…
|
lmata
|
1598 |
@repo.command("ingest-all") |
|
1d4baaf…
|
lmata
|
1599 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1600 |
@click.option("--clear", is_flag=True, help="Clear graph before ingesting.") |
|
1d4baaf…
|
lmata
|
1601 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1602 |
def repo_ingest_all(db: str, clear: bool, as_json: bool): |
|
1d4baaf…
|
lmata
|
1603 |
"""Ingest all registered repositories.""" |
|
1d4baaf…
|
lmata
|
1604 |
from navegador.multirepo import MultiRepoManager |
|
1d4baaf…
|
lmata
|
1605 |
|
|
1d4baaf…
|
lmata
|
1606 |
mgr = MultiRepoManager(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1607 |
with console.status("[bold]Ingesting all repos…[/bold]"): |
|
1d4baaf…
|
lmata
|
1608 |
summary = mgr.ingest_all(clear=clear) |
|
1d4baaf…
|
lmata
|
1609 |
if as_json: |
|
1d4baaf…
|
lmata
|
1610 |
click.echo(json.dumps(summary, indent=2)) |
|
1d4baaf…
|
lmata
|
1611 |
return |
|
1d4baaf…
|
lmata
|
1612 |
for name, stats in summary.items(): |
|
1d4baaf…
|
lmata
|
1613 |
table = Table(title=f"Repo: {name}") |
|
1d4baaf…
|
lmata
|
1614 |
table.add_column("Metric", style="cyan") |
|
1d4baaf…
|
lmata
|
1615 |
table.add_column("Count", justify="right", style="green") |
|
1d4baaf…
|
lmata
|
1616 |
for k, v in stats.items(): |
|
1d4baaf…
|
lmata
|
1617 |
table.add_row(str(k).capitalize(), str(v)) |
|
1d4baaf…
|
lmata
|
1618 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1619 |
|
|
1d4baaf…
|
lmata
|
1620 |
|
|
1d4baaf…
|
lmata
|
1621 |
@repo.command("search") |
|
1d4baaf…
|
lmata
|
1622 |
@click.argument("query") |
|
1d4baaf…
|
lmata
|
1623 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1624 |
@click.option("--limit", default=20, show_default=True) |
|
1d4baaf…
|
lmata
|
1625 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1626 |
def repo_search(query: str, db: str, limit: int, as_json: bool): |
|
1d4baaf…
|
lmata
|
1627 |
"""Search across all registered repositories.""" |
|
1d4baaf…
|
lmata
|
1628 |
from navegador.multirepo import MultiRepoManager |
|
1d4baaf…
|
lmata
|
1629 |
|
|
1d4baaf…
|
lmata
|
1630 |
results = MultiRepoManager(_get_store(db)).cross_repo_search(query, limit=limit) |
|
1d4baaf…
|
lmata
|
1631 |
if as_json: |
|
1d4baaf…
|
lmata
|
1632 |
click.echo(json.dumps(results, indent=2)) |
|
1d4baaf…
|
lmata
|
1633 |
return |
|
1d4baaf…
|
lmata
|
1634 |
if not results: |
|
1d4baaf…
|
lmata
|
1635 |
console.print("[yellow]No results.[/yellow]") |
|
1d4baaf…
|
lmata
|
1636 |
return |
|
1d4baaf…
|
lmata
|
1637 |
table = Table(title=f"Cross-repo search: {query!r}") |
|
1d4baaf…
|
lmata
|
1638 |
table.add_column("Label", style="cyan") |
|
1d4baaf…
|
lmata
|
1639 |
table.add_column("Name", style="bold") |
|
1d4baaf…
|
lmata
|
1640 |
table.add_column("File/Path") |
|
1d4baaf…
|
lmata
|
1641 |
for r in results: |
|
1d4baaf…
|
lmata
|
1642 |
table.add_row(r["label"], r["name"], r["file_path"]) |
|
1d4baaf…
|
lmata
|
1643 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1644 |
|
|
1d4baaf…
|
lmata
|
1645 |
|
|
1d4baaf…
|
lmata
|
1646 |
# ── Rename (#26) ────────────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1647 |
|
|
1d4baaf…
|
lmata
|
1648 |
|
|
1d4baaf…
|
lmata
|
1649 |
@main.command() |
|
1d4baaf…
|
lmata
|
1650 |
@click.argument("old_name") |
|
1d4baaf…
|
lmata
|
1651 |
@click.argument("new_name") |
|
1d4baaf…
|
lmata
|
1652 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1653 |
@click.option("--preview", is_flag=True, help="Show what would change without applying.") |
|
1d4baaf…
|
lmata
|
1654 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1655 |
def rename(old_name: str, new_name: str, db: str, preview: bool, as_json: bool): |
|
1d4baaf…
|
lmata
|
1656 |
"""Rename a symbol across the graph (coordinated rename). |
|
1d4baaf…
|
lmata
|
1657 |
|
|
1d4baaf…
|
lmata
|
1658 |
\b |
|
1d4baaf…
|
lmata
|
1659 |
Examples: |
|
1d4baaf…
|
lmata
|
1660 |
navegador rename old_func new_func --preview |
|
1d4baaf…
|
lmata
|
1661 |
navegador rename MyClass RenamedClass |
|
1d4baaf…
|
lmata
|
1662 |
""" |
|
1d4baaf…
|
lmata
|
1663 |
from navegador.refactor import SymbolRenamer |
|
1d4baaf…
|
lmata
|
1664 |
|
|
1d4baaf…
|
lmata
|
1665 |
renamer = SymbolRenamer(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1666 |
if preview: |
|
1d4baaf…
|
lmata
|
1667 |
result = renamer.preview_rename(old_name, new_name) |
|
1d4baaf…
|
lmata
|
1668 |
data = { |
|
1d4baaf…
|
lmata
|
1669 |
"old_name": result.old_name, |
|
1d4baaf…
|
lmata
|
1670 |
"new_name": result.new_name, |
|
1d4baaf…
|
lmata
|
1671 |
"affected_files": result.affected_files, |
|
1d4baaf…
|
lmata
|
1672 |
"affected_nodes": len(result.affected_nodes), |
|
1d4baaf…
|
lmata
|
1673 |
"edges_updated": result.edges_updated, |
|
1d4baaf…
|
lmata
|
1674 |
} |
|
1d4baaf…
|
lmata
|
1675 |
else: |
|
1d4baaf…
|
lmata
|
1676 |
result = renamer.apply_rename(old_name, new_name) |
|
1d4baaf…
|
lmata
|
1677 |
data = { |
|
1d4baaf…
|
lmata
|
1678 |
"old_name": result.old_name, |
|
1d4baaf…
|
lmata
|
1679 |
"new_name": result.new_name, |
|
1d4baaf…
|
lmata
|
1680 |
"affected_files": result.affected_files, |
|
1d4baaf…
|
lmata
|
1681 |
"affected_nodes": len(result.affected_nodes), |
|
1d4baaf…
|
lmata
|
1682 |
"edges_updated": result.edges_updated, |
|
1d4baaf…
|
lmata
|
1683 |
} |
|
1d4baaf…
|
lmata
|
1684 |
|
|
1d4baaf…
|
lmata
|
1685 |
if as_json: |
|
1d4baaf…
|
lmata
|
1686 |
click.echo(json.dumps(data, indent=2)) |
|
1d4baaf…
|
lmata
|
1687 |
return |
|
1d4baaf…
|
lmata
|
1688 |
|
|
1d4baaf…
|
lmata
|
1689 |
action = "Preview" if preview else "Renamed" |
|
1d4baaf…
|
lmata
|
1690 |
console.print(f"[green]{action}:[/green] {old_name!r} → {new_name!r}") |
|
1d4baaf…
|
lmata
|
1691 |
console.print(f" Nodes affected : {data['affected_nodes']}") |
|
1d4baaf…
|
lmata
|
1692 |
console.print(f" Edges updated : {data['edges_updated']}") |
|
1d4baaf…
|
lmata
|
1693 |
if data["affected_files"]: |
|
1d4baaf…
|
lmata
|
1694 |
console.print(" Files:") |
|
1d4baaf…
|
lmata
|
1695 |
for f in data["affected_files"]: |
|
1d4baaf…
|
lmata
|
1696 |
console.print(f" {f}") |
|
1d4baaf…
|
lmata
|
1697 |
|
|
1d4baaf…
|
lmata
|
1698 |
|
|
1d4baaf…
|
lmata
|
1699 |
# ── CODEOWNERS (#39) ────────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1700 |
|
|
1d4baaf…
|
lmata
|
1701 |
|
|
1d4baaf…
|
lmata
|
1702 |
@main.command() |
|
1d4baaf…
|
lmata
|
1703 |
@click.argument("repo_path", type=click.Path(exists=True)) |
|
1d4baaf…
|
lmata
|
1704 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1705 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1706 |
def codeowners(repo_path: str, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1707 |
"""Parse CODEOWNERS and map ownership to Person nodes.""" |
|
1d4baaf…
|
lmata
|
1708 |
from navegador.codeowners import CodeownersIngester |
|
1d4baaf…
|
lmata
|
1709 |
|
|
1d4baaf…
|
lmata
|
1710 |
stats = CodeownersIngester(_get_store(db)).ingest(repo_path) |
|
1d4baaf…
|
lmata
|
1711 |
if as_json: |
|
1d4baaf…
|
lmata
|
1712 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1713 |
return |
|
1d4baaf…
|
lmata
|
1714 |
console.print( |
|
1d4baaf…
|
lmata
|
1715 |
f"[green]CODEOWNERS ingested:[/green] " |
|
1d4baaf…
|
lmata
|
1716 |
f"{stats['owners']} owners, {stats['patterns']} patterns, {stats['edges']} edges" |
|
1d4baaf…
|
lmata
|
1717 |
) |
|
1d4baaf…
|
lmata
|
1718 |
|
|
1d4baaf…
|
lmata
|
1719 |
|
|
1d4baaf…
|
lmata
|
1720 |
# ── ADR (#40) ───────────────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1721 |
|
|
1d4baaf…
|
lmata
|
1722 |
|
|
1d4baaf…
|
lmata
|
1723 |
@main.group() |
|
1d4baaf…
|
lmata
|
1724 |
def adr(): |
|
1d4baaf…
|
lmata
|
1725 |
"""Ingest Architecture Decision Records (ADRs) into the knowledge graph.""" |
|
1d4baaf…
|
lmata
|
1726 |
|
|
1d4baaf…
|
lmata
|
1727 |
|
|
1d4baaf…
|
lmata
|
1728 |
@adr.command("ingest") |
|
1d4baaf…
|
lmata
|
1729 |
@click.argument("adr_dir", type=click.Path(exists=True)) |
|
1d4baaf…
|
lmata
|
1730 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1731 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1732 |
def adr_ingest(adr_dir: str, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1733 |
"""Parse ADR markdown files and create Decision nodes.""" |
|
1d4baaf…
|
lmata
|
1734 |
from navegador.adr import ADRIngester |
|
1d4baaf…
|
lmata
|
1735 |
|
|
1d4baaf…
|
lmata
|
1736 |
stats = ADRIngester(_get_store(db)).ingest(adr_dir) |
|
1d4baaf…
|
lmata
|
1737 |
if as_json: |
|
1d4baaf…
|
lmata
|
1738 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1739 |
return |
|
1d4baaf…
|
lmata
|
1740 |
console.print( |
|
1d4baaf…
|
lmata
|
1741 |
f"[green]ADRs ingested:[/green] {stats['decisions']} decisions, {stats['skipped']} skipped" |
|
1d4baaf…
|
lmata
|
1742 |
) |
|
1d4baaf…
|
lmata
|
1743 |
|
|
1d4baaf…
|
lmata
|
1744 |
|
|
1d4baaf…
|
lmata
|
1745 |
# ── API schema (#41) ───────────────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1746 |
|
|
1d4baaf…
|
lmata
|
1747 |
|
|
1d4baaf…
|
lmata
|
1748 |
@main.group() |
|
1d4baaf…
|
lmata
|
1749 |
def api(): |
|
1d4baaf…
|
lmata
|
1750 |
"""Ingest API schema files (OpenAPI, GraphQL) into the graph.""" |
|
1d4baaf…
|
lmata
|
1751 |
|
|
1d4baaf…
|
lmata
|
1752 |
|
|
1d4baaf…
|
lmata
|
1753 |
@api.command("ingest") |
|
1d4baaf…
|
lmata
|
1754 |
@click.argument("path", type=click.Path(exists=True)) |
|
1d4baaf…
|
lmata
|
1755 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1756 |
@click.option( |
|
1d4baaf…
|
lmata
|
1757 |
"--type", |
|
1d4baaf…
|
lmata
|
1758 |
"schema_type", |
|
1d4baaf…
|
lmata
|
1759 |
type=click.Choice(["openapi", "graphql", "auto"]), |
|
1d4baaf…
|
lmata
|
1760 |
default="auto", |
|
1d4baaf…
|
lmata
|
1761 |
show_default=True, |
|
1d4baaf…
|
lmata
|
1762 |
help="Schema type. auto detects from file extension.", |
|
1d4baaf…
|
lmata
|
1763 |
) |
|
1d4baaf…
|
lmata
|
1764 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1765 |
def api_ingest(path: str, db: str, schema_type: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1766 |
"""Parse an OpenAPI or GraphQL schema and create API endpoint nodes. |
|
1d4baaf…
|
lmata
|
1767 |
|
|
1d4baaf…
|
lmata
|
1768 |
\b |
|
1d4baaf…
|
lmata
|
1769 |
Examples: |
|
1d4baaf…
|
lmata
|
1770 |
navegador api ingest openapi.yaml |
|
1d4baaf…
|
lmata
|
1771 |
navegador api ingest schema.graphql --type graphql |
|
1d4baaf…
|
lmata
|
1772 |
navegador api ingest swagger.json --type openapi |
|
1d4baaf…
|
lmata
|
1773 |
""" |
|
1d4baaf…
|
lmata
|
1774 |
from pathlib import Path as P |
|
1d4baaf…
|
lmata
|
1775 |
|
|
1d4baaf…
|
lmata
|
1776 |
from navegador.api_schema import APISchemaIngester |
|
1d4baaf…
|
lmata
|
1777 |
|
|
1d4baaf…
|
lmata
|
1778 |
ingester = APISchemaIngester(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1779 |
p = P(path) |
|
1d4baaf…
|
lmata
|
1780 |
|
|
1d4baaf…
|
lmata
|
1781 |
if schema_type == "auto": |
|
1d4baaf…
|
lmata
|
1782 |
if p.suffix.lower() in (".graphql", ".gql"): |
|
1d4baaf…
|
lmata
|
1783 |
schema_type = "graphql" |
|
1d4baaf…
|
lmata
|
1784 |
else: |
|
1d4baaf…
|
lmata
|
1785 |
schema_type = "openapi" |
|
1d4baaf…
|
lmata
|
1786 |
|
|
1d4baaf…
|
lmata
|
1787 |
if schema_type == "graphql": |
|
1d4baaf…
|
lmata
|
1788 |
stats = ingester.ingest_graphql(path) |
|
1d4baaf…
|
lmata
|
1789 |
label = "GraphQL" |
|
1d4baaf…
|
lmata
|
1790 |
else: |
|
1d4baaf…
|
lmata
|
1791 |
stats = ingester.ingest_openapi(path) |
|
1d4baaf…
|
lmata
|
1792 |
label = "OpenAPI" |
|
1d4baaf…
|
lmata
|
1793 |
|
|
1d4baaf…
|
lmata
|
1794 |
if as_json: |
|
1d4baaf…
|
lmata
|
1795 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1796 |
return |
|
1d4baaf…
|
lmata
|
1797 |
|
|
1d4baaf…
|
lmata
|
1798 |
table = Table(title=f"{label} schema ingested") |
|
1d4baaf…
|
lmata
|
1799 |
table.add_column("Metric", style="cyan") |
|
1d4baaf…
|
lmata
|
1800 |
table.add_column("Count", justify="right", style="green") |
|
1d4baaf…
|
lmata
|
1801 |
for k, v in stats.items(): |
|
1d4baaf…
|
lmata
|
1802 |
table.add_row(k.replace("_", " ").capitalize(), str(v)) |
|
1d4baaf…
|
lmata
|
1803 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1804 |
|
|
1d4baaf…
|
lmata
|
1805 |
|
|
1d4baaf…
|
lmata
|
1806 |
# ── PM: project management ticket ingestion (#53) ───────────────────────────── |
|
1d4baaf…
|
lmata
|
1807 |
|
|
1d4baaf…
|
lmata
|
1808 |
|
|
1d4baaf…
|
lmata
|
1809 |
@main.group() |
|
1d4baaf…
|
lmata
|
1810 |
def pm(): |
|
1d4baaf…
|
lmata
|
1811 |
"""Ingest project management tickets (GitHub Issues, Linear, Jira).""" |
|
1d4baaf…
|
lmata
|
1812 |
|
|
1d4baaf…
|
lmata
|
1813 |
|
|
1d4baaf…
|
lmata
|
1814 |
@pm.command("ingest") |
|
e5e1a3b…
|
lmata
|
1815 |
@click.option( |
|
e5e1a3b…
|
lmata
|
1816 |
"--github", |
|
e5e1a3b…
|
lmata
|
1817 |
"github_repo", |
|
e5e1a3b…
|
lmata
|
1818 |
default="", |
|
e5e1a3b…
|
lmata
|
1819 |
metavar="OWNER/REPO", |
|
e5e1a3b…
|
lmata
|
1820 |
help="GitHub repository in owner/repo format.", |
|
e5e1a3b…
|
lmata
|
1821 |
) |
|
e5e1a3b…
|
lmata
|
1822 |
@click.option("--token", default="", envvar="GITHUB_TOKEN", help="GitHub personal access token.") |
|
e5e1a3b…
|
lmata
|
1823 |
@click.option( |
|
e5e1a3b…
|
lmata
|
1824 |
"--state", |
|
e5e1a3b…
|
lmata
|
1825 |
default="open", |
|
e5e1a3b…
|
lmata
|
1826 |
type=click.Choice(["open", "closed", "all"]), |
|
e5e1a3b…
|
lmata
|
1827 |
show_default=True, |
|
e5e1a3b…
|
lmata
|
1828 |
help="GitHub issue state filter.", |
|
e5e1a3b…
|
lmata
|
1829 |
) |
|
e5e1a3b…
|
lmata
|
1830 |
@click.option("--limit", default=100, show_default=True, help="Maximum number of issues to fetch.") |
|
1d4baaf…
|
lmata
|
1831 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1832 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1833 |
def pm_ingest(github_repo: str, token: str, state: str, limit: int, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1834 |
"""Ingest tickets from a PM tool into the knowledge graph. |
|
1d4baaf…
|
lmata
|
1835 |
|
|
1d4baaf…
|
lmata
|
1836 |
\b |
|
1d4baaf…
|
lmata
|
1837 |
Examples: |
|
1d4baaf…
|
lmata
|
1838 |
navegador pm ingest --github owner/repo |
|
1d4baaf…
|
lmata
|
1839 |
navegador pm ingest --github owner/repo --token ghp_... |
|
1d4baaf…
|
lmata
|
1840 |
navegador pm ingest --github owner/repo --state all --limit 200 |
|
1d4baaf…
|
lmata
|
1841 |
""" |
|
1d4baaf…
|
lmata
|
1842 |
if not github_repo: |
|
e5e1a3b…
|
lmata
|
1843 |
raise click.UsageError( |
|
e5e1a3b…
|
lmata
|
1844 |
"Provide --github <owner/repo> (more backends coming in a future release)." |
|
e5e1a3b…
|
lmata
|
1845 |
) |
|
1d4baaf…
|
lmata
|
1846 |
|
|
1d4baaf…
|
lmata
|
1847 |
from navegador.pm import TicketIngester |
|
1d4baaf…
|
lmata
|
1848 |
|
|
1d4baaf…
|
lmata
|
1849 |
ing = TicketIngester(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1850 |
stats = ing.ingest_github_issues(github_repo, token=token, state=state, limit=limit) |
|
1d4baaf…
|
lmata
|
1851 |
|
|
1d4baaf…
|
lmata
|
1852 |
if as_json: |
|
1d4baaf…
|
lmata
|
1853 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1854 |
else: |
|
1d4baaf…
|
lmata
|
1855 |
table = Table(title=f"PM import: {github_repo}") |
|
1d4baaf…
|
lmata
|
1856 |
table.add_column("Metric", style="cyan") |
|
1d4baaf…
|
lmata
|
1857 |
table.add_column("Count", justify="right", style="green") |
|
1d4baaf…
|
lmata
|
1858 |
for k, v in stats.items(): |
|
1d4baaf…
|
lmata
|
1859 |
table.add_row(k.capitalize(), str(v)) |
|
1d4baaf…
|
lmata
|
1860 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1861 |
|
|
1d4baaf…
|
lmata
|
1862 |
|
|
1d4baaf…
|
lmata
|
1863 |
# ── Dependencies: external package ingestion (#58) ──────────────────────────── |
|
1d4baaf…
|
lmata
|
1864 |
|
|
1d4baaf…
|
lmata
|
1865 |
|
|
1d4baaf…
|
lmata
|
1866 |
@main.group() |
|
1d4baaf…
|
lmata
|
1867 |
def deps(): |
|
1d4baaf…
|
lmata
|
1868 |
"""Ingest external package dependencies (npm, pip, cargo).""" |
|
1d4baaf…
|
lmata
|
1869 |
|
|
1d4baaf…
|
lmata
|
1870 |
|
|
1d4baaf…
|
lmata
|
1871 |
@deps.command("ingest") |
|
1d4baaf…
|
lmata
|
1872 |
@click.argument("path", type=click.Path(exists=True)) |
|
1d4baaf…
|
lmata
|
1873 |
@click.option( |
|
e5e1a3b…
|
lmata
|
1874 |
"--type", |
|
e5e1a3b…
|
lmata
|
1875 |
"dep_type", |
|
1d4baaf…
|
lmata
|
1876 |
type=click.Choice(["auto", "npm", "pip", "cargo"]), |
|
1d4baaf…
|
lmata
|
1877 |
default="auto", |
|
1d4baaf…
|
lmata
|
1878 |
show_default=True, |
|
1d4baaf…
|
lmata
|
1879 |
help="Manifest type. auto detects from filename.", |
|
1d4baaf…
|
lmata
|
1880 |
) |
|
1d4baaf…
|
lmata
|
1881 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1882 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1883 |
def deps_ingest(path: str, dep_type: str, db: str, as_json: bool): |
|
1d4baaf…
|
lmata
|
1884 |
"""Ingest external dependencies from a package manifest. |
|
1d4baaf…
|
lmata
|
1885 |
|
|
1d4baaf…
|
lmata
|
1886 |
\b |
|
1d4baaf…
|
lmata
|
1887 |
PATH can be: |
|
1d4baaf…
|
lmata
|
1888 |
package.json (npm) |
|
1d4baaf…
|
lmata
|
1889 |
requirements.txt (pip) |
|
1d4baaf…
|
lmata
|
1890 |
pyproject.toml (pip) |
|
1d4baaf…
|
lmata
|
1891 |
Cargo.toml (cargo) |
|
1d4baaf…
|
lmata
|
1892 |
|
|
1d4baaf…
|
lmata
|
1893 |
\b |
|
1d4baaf…
|
lmata
|
1894 |
Examples: |
|
1d4baaf…
|
lmata
|
1895 |
navegador deps ingest package.json |
|
1d4baaf…
|
lmata
|
1896 |
navegador deps ingest requirements.txt |
|
1d4baaf…
|
lmata
|
1897 |
navegador deps ingest Cargo.toml --type cargo |
|
1d4baaf…
|
lmata
|
1898 |
""" |
|
1d4baaf…
|
lmata
|
1899 |
from pathlib import Path as P |
|
1d4baaf…
|
lmata
|
1900 |
|
|
1d4baaf…
|
lmata
|
1901 |
from navegador.dependencies import DependencyIngester |
|
1d4baaf…
|
lmata
|
1902 |
|
|
1d4baaf…
|
lmata
|
1903 |
ing = DependencyIngester(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1904 |
p = P(path) |
|
1d4baaf…
|
lmata
|
1905 |
|
|
1d4baaf…
|
lmata
|
1906 |
if dep_type == "auto": |
|
1d4baaf…
|
lmata
|
1907 |
name = p.name.lower() |
|
1d4baaf…
|
lmata
|
1908 |
if name == "package.json": |
|
1d4baaf…
|
lmata
|
1909 |
dep_type = "npm" |
|
1d4baaf…
|
lmata
|
1910 |
elif name in ("requirements.txt", "pyproject.toml"): |
|
1d4baaf…
|
lmata
|
1911 |
dep_type = "pip" |
|
1d4baaf…
|
lmata
|
1912 |
elif name == "cargo.toml": |
|
1d4baaf…
|
lmata
|
1913 |
dep_type = "cargo" |
|
1d4baaf…
|
lmata
|
1914 |
else: |
|
1d4baaf…
|
lmata
|
1915 |
raise click.UsageError( |
|
1d4baaf…
|
lmata
|
1916 |
f"Cannot auto-detect type for {p.name!r}. Use --type npm|pip|cargo." |
|
1d4baaf…
|
lmata
|
1917 |
) |
|
1d4baaf…
|
lmata
|
1918 |
|
|
1d4baaf…
|
lmata
|
1919 |
dispatch = { |
|
1d4baaf…
|
lmata
|
1920 |
"npm": ing.ingest_npm, |
|
1d4baaf…
|
lmata
|
1921 |
"pip": ing.ingest_pip, |
|
1d4baaf…
|
lmata
|
1922 |
"cargo": ing.ingest_cargo, |
|
1d4baaf…
|
lmata
|
1923 |
} |
|
1d4baaf…
|
lmata
|
1924 |
stats = dispatch[dep_type](path) |
|
1d4baaf…
|
lmata
|
1925 |
|
|
1d4baaf…
|
lmata
|
1926 |
if as_json: |
|
1d4baaf…
|
lmata
|
1927 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1928 |
else: |
|
1d4baaf…
|
lmata
|
1929 |
console.print( |
|
e5e1a3b…
|
lmata
|
1930 |
f"[green]Dependencies ingested[/green] ({dep_type}): {stats['packages']} packages" |
|
1d4baaf…
|
lmata
|
1931 |
) |
|
1d4baaf…
|
lmata
|
1932 |
|
|
1d4baaf…
|
lmata
|
1933 |
|
|
1d4baaf…
|
lmata
|
1934 |
# ── Submodules: ingest parent + submodules (#61) ────────────────────────────── |
|
1d4baaf…
|
lmata
|
1935 |
|
|
1d4baaf…
|
lmata
|
1936 |
|
|
1d4baaf…
|
lmata
|
1937 |
@main.group() |
|
1d4baaf…
|
lmata
|
1938 |
def submodules(): |
|
1d4baaf…
|
lmata
|
1939 |
"""Ingest a parent repository and all its git submodules.""" |
|
1d4baaf…
|
lmata
|
1940 |
|
|
1d4baaf…
|
lmata
|
1941 |
|
|
1d4baaf…
|
lmata
|
1942 |
@submodules.command("ingest") |
|
1d4baaf…
|
lmata
|
1943 |
@click.argument("repo_path", type=click.Path(exists=True)) |
|
1d4baaf…
|
lmata
|
1944 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
1945 |
@click.option("--clear", is_flag=True, help="Clear existing graph before ingesting.") |
|
1d4baaf…
|
lmata
|
1946 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
1947 |
def submodules_ingest(repo_path: str, db: str, clear: bool, as_json: bool): |
|
1d4baaf…
|
lmata
|
1948 |
"""Ingest a repository and all its git submodules as linked nodes. |
|
1d4baaf…
|
lmata
|
1949 |
|
|
1d4baaf…
|
lmata
|
1950 |
\b |
|
1d4baaf…
|
lmata
|
1951 |
Examples: |
|
1d4baaf…
|
lmata
|
1952 |
navegador submodules ingest . |
|
1d4baaf…
|
lmata
|
1953 |
navegador submodules ingest /path/to/repo --clear |
|
1d4baaf…
|
lmata
|
1954 |
""" |
|
1d4baaf…
|
lmata
|
1955 |
from navegador.submodules import SubmoduleIngester |
|
1d4baaf…
|
lmata
|
1956 |
|
|
1d4baaf…
|
lmata
|
1957 |
ing = SubmoduleIngester(_get_store(db)) |
|
1d4baaf…
|
lmata
|
1958 |
stats = ing.ingest_with_submodules(repo_path, clear=clear) |
|
1d4baaf…
|
lmata
|
1959 |
|
|
1d4baaf…
|
lmata
|
1960 |
if as_json: |
|
1d4baaf…
|
lmata
|
1961 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
1962 |
else: |
|
1d4baaf…
|
lmata
|
1963 |
sub_names = list(stats.get("submodules", {}).keys()) |
|
1d4baaf…
|
lmata
|
1964 |
console.print( |
|
1d4baaf…
|
lmata
|
1965 |
f"[green]Submodule ingestion complete[/green]: " |
|
1d4baaf…
|
lmata
|
1966 |
f"{stats.get('total_files', 0)} total files, " |
|
1d4baaf…
|
lmata
|
1967 |
f"{len(sub_names)} submodule(s)" |
|
1d4baaf…
|
lmata
|
1968 |
) |
|
1d4baaf…
|
lmata
|
1969 |
if sub_names: |
|
1d4baaf…
|
lmata
|
1970 |
console.print(" Submodules: " + ", ".join(sub_names)) |
|
1d4baaf…
|
lmata
|
1971 |
|
|
1d4baaf…
|
lmata
|
1972 |
|
|
1d4baaf…
|
lmata
|
1973 |
@submodules.command("list") |
|
1d4baaf…
|
lmata
|
1974 |
@click.argument("repo_path", type=click.Path(exists=True), default=".") |
|
1d4baaf…
|
lmata
|
1975 |
def submodules_list(repo_path: str): |
|
1d4baaf…
|
lmata
|
1976 |
"""List git submodules found in REPO_PATH.""" |
|
1d4baaf…
|
lmata
|
1977 |
from navegador.submodules import SubmoduleIngester |
|
1d4baaf…
|
lmata
|
1978 |
|
|
1d4baaf…
|
lmata
|
1979 |
subs = SubmoduleIngester.__new__(SubmoduleIngester) |
|
1d4baaf…
|
lmata
|
1980 |
subs.store = None # type: ignore[assignment] |
|
1d4baaf…
|
lmata
|
1981 |
items = subs.detect_submodules(repo_path) |
|
1d4baaf…
|
lmata
|
1982 |
|
|
1d4baaf…
|
lmata
|
1983 |
if not items: |
|
1d4baaf…
|
lmata
|
1984 |
console.print("[yellow]No submodules found (no .gitmodules).[/yellow]") |
|
1d4baaf…
|
lmata
|
1985 |
return |
|
1d4baaf…
|
lmata
|
1986 |
|
|
1d4baaf…
|
lmata
|
1987 |
table = Table(title=f"Submodules in {repo_path}") |
|
1d4baaf…
|
lmata
|
1988 |
table.add_column("Name", style="cyan") |
|
1d4baaf…
|
lmata
|
1989 |
table.add_column("Path") |
|
1d4baaf…
|
lmata
|
1990 |
table.add_column("URL") |
|
1d4baaf…
|
lmata
|
1991 |
for item in items: |
|
1d4baaf…
|
lmata
|
1992 |
table.add_row(item["name"], item["path"], item.get("url", "")) |
|
1d4baaf…
|
lmata
|
1993 |
console.print(table) |
|
1d4baaf…
|
lmata
|
1994 |
|
|
1d4baaf…
|
lmata
|
1995 |
|
|
1d4baaf…
|
lmata
|
1996 |
# ── Workspace: multi-repo (#62) ──────────────────────────────────────────────── |
|
1d4baaf…
|
lmata
|
1997 |
|
|
1d4baaf…
|
lmata
|
1998 |
|
|
1d4baaf…
|
lmata
|
1999 |
@main.group() |
|
1d4baaf…
|
lmata
|
2000 |
def workspace(): |
|
1d4baaf…
|
lmata
|
2001 |
"""Manage a multi-repo workspace (unified or federated graph).""" |
|
1d4baaf…
|
lmata
|
2002 |
|
|
1d4baaf…
|
lmata
|
2003 |
|
|
1d4baaf…
|
lmata
|
2004 |
@workspace.command("ingest") |
|
1d4baaf…
|
lmata
|
2005 |
@click.argument("repos", nargs=-1, metavar="NAME=PATH ...") |
|
1d4baaf…
|
lmata
|
2006 |
@click.option( |
|
1d4baaf…
|
lmata
|
2007 |
"--mode", |
|
1d4baaf…
|
lmata
|
2008 |
type=click.Choice(["unified", "federated"]), |
|
1d4baaf…
|
lmata
|
2009 |
default="unified", |
|
1d4baaf…
|
lmata
|
2010 |
show_default=True, |
|
1d4baaf…
|
lmata
|
2011 |
help="Graph mode: unified (shared graph) or federated (per-repo graphs).", |
|
1d4baaf…
|
lmata
|
2012 |
) |
|
1d4baaf…
|
lmata
|
2013 |
@DB_OPTION |
|
1d4baaf…
|
lmata
|
2014 |
@click.option("--clear", is_flag=True) |
|
1d4baaf…
|
lmata
|
2015 |
@click.option("--json", "as_json", is_flag=True) |
|
1d4baaf…
|
lmata
|
2016 |
def workspace_ingest(repos: tuple, mode: str, db: str, clear: bool, as_json: bool): |
|
1d4baaf…
|
lmata
|
2017 |
"""Ingest multiple repositories as a workspace. |
|
1d4baaf…
|
lmata
|
2018 |
|
|
1d4baaf…
|
lmata
|
2019 |
\b |
|
1d4baaf…
|
lmata
|
2020 |
REPOS is a list of NAME=PATH pairs, e.g.: |
|
1d4baaf…
|
lmata
|
2021 |
navegador workspace ingest backend=/path/to/backend frontend=/path/to/frontend |
|
1d4baaf…
|
lmata
|
2022 |
|
|
1d4baaf…
|
lmata
|
2023 |
\b |
|
1d4baaf…
|
lmata
|
2024 |
Examples: |
|
1d4baaf…
|
lmata
|
2025 |
navegador workspace ingest backend=. frontend=../frontend --mode unified |
|
1d4baaf…
|
lmata
|
2026 |
navegador workspace ingest api=./api worker=./worker --mode federated |
|
1d4baaf…
|
lmata
|
2027 |
""" |
|
1d4baaf…
|
lmata
|
2028 |
from navegador.multirepo import WorkspaceManager, WorkspaceMode |
|
1d4baaf…
|
lmata
|
2029 |
|
|
1d4baaf…
|
lmata
|
2030 |
if not repos: |
|
1d4baaf…
|
lmata
|
2031 |
raise click.UsageError("Provide at least one NAME=PATH repo.") |
|
1d4baaf…
|
lmata
|
2032 |
|
|
1d4baaf…
|
lmata
|
2033 |
wm = WorkspaceManager(_get_store(db), mode=WorkspaceMode(mode)) |
|
1d4baaf…
|
lmata
|
2034 |
for repo_spec in repos: |
|
1d4baaf…
|
lmata
|
2035 |
if "=" not in repo_spec: |
|
e5e1a3b…
|
lmata
|
2036 |
raise click.UsageError(f"Invalid repo spec {repo_spec!r}. Expected NAME=PATH format.") |
|
1d4baaf…
|
lmata
|
2037 |
name, path = repo_spec.split("=", 1) |
|
1d4baaf…
|
lmata
|
2038 |
wm.add_repo(name.strip(), path.strip()) |
|
1d4baaf…
|
lmata
|
2039 |
|
|
1d4baaf…
|
lmata
|
2040 |
stats = wm.ingest_all(clear=clear) |
|
1d4baaf…
|
lmata
|
2041 |
|
|
1d4baaf…
|
lmata
|
2042 |
if as_json: |
|
1d4baaf…
|
lmata
|
2043 |
click.echo(json.dumps(stats, indent=2)) |
|
1d4baaf…
|
lmata
|
2044 |
else: |
|
1d4baaf…
|
lmata
|
2045 |
for repo_name, repo_stats in stats.items(): |
|
1d4baaf…
|
lmata
|
2046 |
if "error" in repo_stats: |
|
1d4baaf…
|
lmata
|
2047 |
console.print(f"[red]Error ingesting {repo_name}:[/red] {repo_stats['error']}") |
|
1d4baaf…
|
lmata
|
2048 |
else: |
|
1d4baaf…
|
lmata
|
2049 |
console.print( |
|
1d4baaf…
|
lmata
|
2050 |
f"[green]{repo_name}[/green]: " |
|
1d4baaf…
|
lmata
|
2051 |
f"{repo_stats.get('files', 0)} files, " |
|
1d4baaf…
|
lmata
|
2052 |
f"{repo_stats.get('nodes', 0)} nodes" |
|
1d4baaf…
|
lmata
|
2053 |
) |
|
5d70f82…
|
lmata
|
2054 |
|
|
5d70f82…
|
lmata
|
2055 |
|
|
5d70f82…
|
lmata
|
2056 |
# ── Intelligence: semantic search ───────────────────────────────────────────── |
|
5d70f82…
|
lmata
|
2057 |
|
|
5d70f82…
|
lmata
|
2058 |
|
|
5d70f82…
|
lmata
|
2059 |
@main.command("semantic-search") |
|
5d70f82…
|
lmata
|
2060 |
@click.argument("query") |
|
5d70f82…
|
lmata
|
2061 |
@DB_OPTION |
|
5d70f82…
|
lmata
|
2062 |
@click.option("--limit", default=10, show_default=True, help="Maximum results to return.") |
|
5d70f82…
|
lmata
|
2063 |
@click.option( |
|
5d70f82…
|
lmata
|
2064 |
"--index", |
|
5d70f82…
|
lmata
|
2065 |
"do_index", |
|
5d70f82…
|
lmata
|
2066 |
is_flag=True, |
|
5d70f82…
|
lmata
|
2067 |
help="(Re-)build the embedding index before searching.", |
|
5d70f82…
|
lmata
|
2068 |
) |
|
5d70f82…
|
lmata
|
2069 |
@click.option( |
|
5d70f82…
|
lmata
|
2070 |
"--provider", |
|
5d70f82…
|
lmata
|
2071 |
"llm_provider", |
|
5d70f82…
|
lmata
|
2072 |
default="", |
|
5d70f82…
|
lmata
|
2073 |
help="LLM provider to use (anthropic, openai, ollama). Auto-detected if omitted.", |
|
5d70f82…
|
lmata
|
2074 |
) |
|
5d70f82…
|
lmata
|
2075 |
@click.option("--model", "llm_model", default="", help="LLM model name.") |
|
5d70f82…
|
lmata
|
2076 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
5d70f82…
|
lmata
|
2077 |
def semantic_search( |
|
5d70f82…
|
lmata
|
2078 |
query: str, |
|
5d70f82…
|
lmata
|
2079 |
db: str, |
|
5d70f82…
|
lmata
|
2080 |
limit: int, |
|
5d70f82…
|
lmata
|
2081 |
do_index: bool, |
|
5d70f82…
|
lmata
|
2082 |
llm_provider: str, |
|
5d70f82…
|
lmata
|
2083 |
llm_model: str, |
|
5d70f82…
|
lmata
|
2084 |
as_json: bool, |
|
5d70f82…
|
lmata
|
2085 |
): |
|
5d70f82…
|
lmata
|
2086 |
"""Semantic similarity search using embeddings. |
|
5d70f82…
|
lmata
|
2087 |
|
|
5d70f82…
|
lmata
|
2088 |
Embeds QUERY and returns the most similar symbols from the graph. |
|
5d70f82…
|
lmata
|
2089 |
Use --index to (re-)build the embedding index before searching. |
|
5d70f82…
|
lmata
|
2090 |
|
|
5d70f82…
|
lmata
|
2091 |
\b |
|
5d70f82…
|
lmata
|
2092 |
Examples: |
|
5d70f82…
|
lmata
|
2093 |
navegador semantic-search "validates JWT tokens" |
|
5d70f82…
|
lmata
|
2094 |
navegador semantic-search "database connection" --index --provider openai |
|
5d70f82…
|
lmata
|
2095 |
""" |
|
5d70f82…
|
lmata
|
2096 |
from navegador.intelligence.search import SemanticSearch |
|
5d70f82…
|
lmata
|
2097 |
from navegador.llm import auto_provider, get_provider |
|
5d70f82…
|
lmata
|
2098 |
|
|
5d70f82…
|
lmata
|
2099 |
store = _get_store(db) |
|
5d70f82…
|
lmata
|
2100 |
provider = ( |
|
e5e1a3b…
|
lmata
|
2101 |
get_provider(llm_provider, model=llm_model) |
|
e5e1a3b…
|
lmata
|
2102 |
if llm_provider |
|
e5e1a3b…
|
lmata
|
2103 |
else auto_provider(model=llm_model) |
|
5d70f82…
|
lmata
|
2104 |
) |
|
5d70f82…
|
lmata
|
2105 |
ss = SemanticSearch(store, provider) |
|
5d70f82…
|
lmata
|
2106 |
|
|
5d70f82…
|
lmata
|
2107 |
if do_index: |
|
5d70f82…
|
lmata
|
2108 |
n = ss.index() |
|
5d70f82…
|
lmata
|
2109 |
if not as_json: |
|
5d70f82…
|
lmata
|
2110 |
console.print(f"[green]Indexed[/green] {n} nodes.") |
|
5d70f82…
|
lmata
|
2111 |
|
|
5d70f82…
|
lmata
|
2112 |
results = ss.search(query, limit=limit) |
|
5d70f82…
|
lmata
|
2113 |
|
|
5d70f82…
|
lmata
|
2114 |
if as_json: |
|
5d70f82…
|
lmata
|
2115 |
click.echo(json.dumps(results, indent=2)) |
|
5d70f82…
|
lmata
|
2116 |
return |
|
5d70f82…
|
lmata
|
2117 |
|
|
5d70f82…
|
lmata
|
2118 |
if not results: |
|
5d70f82…
|
lmata
|
2119 |
console.print("[yellow]No results found. Try --index to build the index first.[/yellow]") |
|
5d70f82…
|
lmata
|
2120 |
return |
|
5d70f82…
|
lmata
|
2121 |
|
|
5d70f82…
|
lmata
|
2122 |
table = Table(title=f"Semantic search: {query!r}") |
|
5d70f82…
|
lmata
|
2123 |
table.add_column("Score", style="cyan", justify="right") |
|
5d70f82…
|
lmata
|
2124 |
table.add_column("Type", style="yellow") |
|
5d70f82…
|
lmata
|
2125 |
table.add_column("Name", style="bold") |
|
5d70f82…
|
lmata
|
2126 |
table.add_column("File", style="dim") |
|
5d70f82…
|
lmata
|
2127 |
for r in results: |
|
5d70f82…
|
lmata
|
2128 |
table.add_row( |
|
5d70f82…
|
lmata
|
2129 |
f"{r['score']:.3f}", |
|
5d70f82…
|
lmata
|
2130 |
r.get("type", ""), |
|
5d70f82…
|
lmata
|
2131 |
r.get("name", ""), |
|
5d70f82…
|
lmata
|
2132 |
r.get("file_path", ""), |
|
5d70f82…
|
lmata
|
2133 |
) |
|
5d70f82…
|
lmata
|
2134 |
console.print(table) |
|
5d70f82…
|
lmata
|
2135 |
|
|
5d70f82…
|
lmata
|
2136 |
|
|
5d70f82…
|
lmata
|
2137 |
# ── Intelligence: community detection ───────────────────────────────────────── |
|
5d70f82…
|
lmata
|
2138 |
|
|
5d70f82…
|
lmata
|
2139 |
|
|
5d70f82…
|
lmata
|
2140 |
@main.command("communities") |
|
5d70f82…
|
lmata
|
2141 |
@DB_OPTION |
|
5d70f82…
|
lmata
|
2142 |
@click.option("--min-size", default=2, show_default=True, help="Minimum community size.") |
|
5d70f82…
|
lmata
|
2143 |
@click.option("--store-labels", is_flag=True, help="Write community labels back onto nodes.") |
|
5d70f82…
|
lmata
|
2144 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
|
5d70f82…
|
lmata
|
2145 |
def communities(db: str, min_size: int, store_labels: bool, as_json: bool): |
|
5d70f82…
|
lmata
|
2146 |
"""Detect communities in the graph via label propagation. |
|
5d70f82…
|
lmata
|
2147 |
|
|
5d70f82…
|
lmata
|
2148 |
\b |
|
5d70f82…
|
lmata
|
2149 |
Examples: |
|
5d70f82…
|
lmata
|
2150 |
navegador communities |
|
5d70f82…
|
lmata
|
2151 |
navegador communities --min-size 3 --store-labels |
|
5d70f82…
|
lmata
|
2152 |
""" |
|
5d70f82…
|
lmata
|
2153 |
from navegador.intelligence.community import CommunityDetector |
|
5d70f82…
|
lmata
|
2154 |
|
|
5d70f82…
|
lmata
|
2155 |
store = _get_store(db) |
|
5d70f82…
|
lmata
|
2156 |
detector = CommunityDetector(store) |
|
5d70f82…
|
lmata
|
2157 |
detected = detector.detect(min_size=min_size) |
|
5d70f82…
|
lmata
|
2158 |
|
|
5d70f82…
|
lmata
|
2159 |
if store_labels: |
|
5d70f82…
|
lmata
|
2160 |
n = detector.store_communities() |
|
5d70f82…
|
lmata
|
2161 |
if not as_json: |
|
5d70f82…
|
lmata
|
2162 |
console.print(f"[green]Community labels written to[/green] {n} nodes.") |
|
5d70f82…
|
lmata
|
2163 |
|
|
5d70f82…
|
lmata
|
2164 |
if as_json: |
|
5d70f82…
|
lmata
|
2165 |
click.echo( |
|
5d70f82…
|
lmata
|
2166 |
json.dumps( |
|
5d70f82…
|
lmata
|
2167 |
[ |
|
5d70f82…
|
lmata
|
2168 |
{ |
|
5d70f82…
|
lmata
|
2169 |
"name": c.name, |
|
5d70f82…
|
lmata
|
2170 |
"members": c.members, |
|
5d70f82…
|
lmata
|
2171 |
"size": c.size, |
|
5d70f82…
|
lmata
|
2172 |
"density": c.density, |
|
5d70f82…
|
lmata
|
2173 |
} |
|
5d70f82…
|
lmata
|
2174 |
for c in detected |
|
5d70f82…
|
lmata
|
2175 |
], |
|
5d70f82…
|
lmata
|
2176 |
indent=2, |
|
5d70f82…
|
lmata
|
2177 |
) |
|
5d70f82…
|
lmata
|
2178 |
) |
|
5d70f82…
|
lmata
|
2179 |
return |
|
5d70f82…
|
lmata
|
2180 |
|
|
5d70f82…
|
lmata
|
2181 |
if not detected: |
|
5d70f82…
|
lmata
|
2182 |
console.print("[yellow]No communities found (graph may be empty).[/yellow]") |
|
5d70f82…
|
lmata
|
2183 |
return |
|
5d70f82…
|
lmata
|
2184 |
|
|
5d70f82…
|
lmata
|
2185 |
table = Table(title=f"Communities (min_size={min_size})") |
|
5d70f82…
|
lmata
|
2186 |
table.add_column("Community", style="cyan") |
|
5d70f82…
|
lmata
|
2187 |
table.add_column("Size", justify="right", style="green") |
|
5d70f82…
|
lmata
|
2188 |
table.add_column("Density", justify="right", style="yellow") |
|
5d70f82…
|
lmata
|
2189 |
table.add_column("Members (preview)", style="dim") |
|
5d70f82…
|
lmata
|
2190 |
for c in detected: |
|
5d70f82…
|
lmata
|
2191 |
preview = ", ".join(c.members[:5]) |
|
5d70f82…
|
lmata
|
2192 |
if c.size > 5: |
|
5d70f82…
|
lmata
|
2193 |
preview += f" …+{c.size - 5}" |
|
5d70f82…
|
lmata
|
2194 |
table.add_row(c.name, str(c.size), f"{c.density:.3f}", preview) |
|
5d70f82…
|
lmata
|
2195 |
console.print(table) |
|
5d70f82…
|
lmata
|
2196 |
|
|
5d70f82…
|
lmata
|
2197 |
|
|
5d70f82…
|
lmata
|
2198 |
# ── Intelligence: natural language query ────────────────────────────────────── |
|
5d70f82…
|
lmata
|
2199 |
|
|
5d70f82…
|
lmata
|
2200 |
|
|
5d70f82…
|
lmata
|
2201 |
@main.command("ask") |
|
5d70f82…
|
lmata
|
2202 |
@click.argument("question") |
|
5d70f82…
|
lmata
|
2203 |
@DB_OPTION |
|
5d70f82…
|
lmata
|
2204 |
@click.option( |
|
5d70f82…
|
lmata
|
2205 |
"--provider", |
|
5d70f82…
|
lmata
|
2206 |
"llm_provider", |
|
5d70f82…
|
lmata
|
2207 |
default="", |
|
5d70f82…
|
lmata
|
2208 |
help="LLM provider (anthropic, openai, ollama). Auto-detected if omitted.", |
|
5d70f82…
|
lmata
|
2209 |
) |
|
5d70f82…
|
lmata
|
2210 |
@click.option("--model", "llm_model", default="", help="LLM model name.") |
|
5d70f82…
|
lmata
|
2211 |
def ask(question: str, db: str, llm_provider: str, llm_model: str): |
|
5d70f82…
|
lmata
|
2212 |
"""Ask a natural-language question about the codebase. |
|
5d70f82…
|
lmata
|
2213 |
|
|
5d70f82…
|
lmata
|
2214 |
Converts the question to Cypher, executes it, and returns a |
|
5d70f82…
|
lmata
|
2215 |
human-readable answer. |
|
5d70f82…
|
lmata
|
2216 |
|
|
5d70f82…
|
lmata
|
2217 |
\b |
|
5d70f82…
|
lmata
|
2218 |
Examples: |
|
5d70f82…
|
lmata
|
2219 |
navegador ask "Which functions call authenticate_user?" |
|
5d70f82…
|
lmata
|
2220 |
navegador ask "What concepts are in the auth domain?" |
|
5d70f82…
|
lmata
|
2221 |
""" |
|
5d70f82…
|
lmata
|
2222 |
from navegador.intelligence.nlp import NLPEngine |
|
5d70f82…
|
lmata
|
2223 |
from navegador.llm import auto_provider, get_provider |
|
5d70f82…
|
lmata
|
2224 |
|
|
5d70f82…
|
lmata
|
2225 |
store = _get_store(db) |
|
5d70f82…
|
lmata
|
2226 |
provider = ( |
|
e5e1a3b…
|
lmata
|
2227 |
get_provider(llm_provider, model=llm_model) |
|
e5e1a3b…
|
lmata
|
2228 |
if llm_provider |
|
e5e1a3b…
|
lmata
|
2229 |
else auto_provider(model=llm_model) |
|
5d70f82…
|
lmata
|
2230 |
) |
|
5d70f82…
|
lmata
|
2231 |
engine = NLPEngine(store, provider) |
|
5d70f82…
|
lmata
|
2232 |
|
|
5d70f82…
|
lmata
|
2233 |
with console.status("[bold]Thinking...[/bold]"): |
|
5d70f82…
|
lmata
|
2234 |
answer = engine.natural_query(question) |
|
5d70f82…
|
lmata
|
2235 |
|
|
5d70f82…
|
lmata
|
2236 |
console.print(answer) |
|
5d70f82…
|
lmata
|
2237 |
|
|
5d70f82…
|
lmata
|
2238 |
|
|
5d70f82…
|
lmata
|
2239 |
# ── Intelligence: generate docs ─────────────────────────────────────────────── |
|
5d70f82…
|
lmata
|
2240 |
|
|
5d70f82…
|
lmata
|
2241 |
|
|
5d70f82…
|
lmata
|
2242 |
@main.command("generate-docs") |
|
5d70f82…
|
lmata
|
2243 |
@click.argument("name") |
|
5d70f82…
|
lmata
|
2244 |
@DB_OPTION |
|
5d70f82…
|
lmata
|
2245 |
@click.option( |
|
5d70f82…
|
lmata
|
2246 |
"--provider", |
|
5d70f82…
|
lmata
|
2247 |
"llm_provider", |
|
5d70f82…
|
lmata
|
2248 |
default="", |
|
5d70f82…
|
lmata
|
2249 |
help="LLM provider (anthropic, openai, ollama). Auto-detected if omitted.", |
|
5d70f82…
|
lmata
|
2250 |
) |
|
5d70f82…
|
lmata
|
2251 |
@click.option("--model", "llm_model", default="", help="LLM model name.") |
|
5d70f82…
|
lmata
|
2252 |
@click.option("--file", "file_path", default="", help="Narrow to a specific file.") |
|
e5e1a3b…
|
lmata
|
2253 |
def generate_docs_cmd(name: str, db: str, llm_provider: str, llm_model: str, file_path: str): |
|
5d70f82…
|
lmata
|
2254 |
"""Generate LLM-powered documentation for a named symbol. |
|
5d70f82…
|
lmata
|
2255 |
|
|
5d70f82…
|
lmata
|
2256 |
\b |
|
5d70f82…
|
lmata
|
2257 |
Examples: |
|
5d70f82…
|
lmata
|
2258 |
navegador generate-docs authenticate_user |
|
5d70f82…
|
lmata
|
2259 |
navegador generate-docs GraphStore --file navegador/graph/store.py |
|
5d70f82…
|
lmata
|
2260 |
""" |
|
5d70f82…
|
lmata
|
2261 |
from navegador.intelligence.nlp import NLPEngine |
|
5d70f82…
|
lmata
|
2262 |
from navegador.llm import auto_provider, get_provider |
|
5d70f82…
|
lmata
|
2263 |
|
|
5d70f82…
|
lmata
|
2264 |
store = _get_store(db) |
|
5d70f82…
|
lmata
|
2265 |
provider = ( |
|
e5e1a3b…
|
lmata
|
2266 |
get_provider(llm_provider, model=llm_model) |
|
e5e1a3b…
|
lmata
|
2267 |
if llm_provider |
|
e5e1a3b…
|
lmata
|
2268 |
else auto_provider(model=llm_model) |
|
5d70f82…
|
lmata
|
2269 |
) |
|
5d70f82…
|
lmata
|
2270 |
engine = NLPEngine(store, provider) |
|
5d70f82…
|
lmata
|
2271 |
|
|
5d70f82…
|
lmata
|
2272 |
with console.status("[bold]Generating docs...[/bold]"): |
|
5d70f82…
|
lmata
|
2273 |
docs = engine.generate_docs(name, file_path=file_path) |
|
5d70f82…
|
lmata
|
2274 |
|
|
5d70f82…
|
lmata
|
2275 |
console.print(docs) |
|
5d70f82…
|
lmata
|
2276 |
|
|
5d70f82…
|
lmata
|
2277 |
|
|
5d70f82…
|
lmata
|
2278 |
# ── Intelligence: docs (template + LLM) ────────────────────────────────────── |
|
5d70f82…
|
lmata
|
2279 |
|
|
5d70f82…
|
lmata
|
2280 |
|
|
5d70f82…
|
lmata
|
2281 |
@main.command("docs") |
|
5d70f82…
|
lmata
|
2282 |
@click.argument("target") |
|
5d70f82…
|
lmata
|
2283 |
@DB_OPTION |
|
5d70f82…
|
lmata
|
2284 |
@click.option("--project", is_flag=True, help="Generate full project documentation.") |
|
5d70f82…
|
lmata
|
2285 |
@click.option( |
|
5d70f82…
|
lmata
|
2286 |
"--provider", |
|
5d70f82…
|
lmata
|
2287 |
"llm_provider", |
|
5d70f82…
|
lmata
|
2288 |
default="", |
|
5d70f82…
|
lmata
|
2289 |
help="LLM provider (anthropic, openai, ollama). Template mode if omitted.", |
|
5d70f82…
|
lmata
|
2290 |
) |
|
5d70f82…
|
lmata
|
2291 |
@click.option("--model", "llm_model", default="", help="LLM model name.") |
|
5d70f82…
|
lmata
|
2292 |
@click.option("--json", "as_json", is_flag=True, help="Output as JSON (wraps markdown in a dict).") |
|
e5e1a3b…
|
lmata
|
2293 |
def docs(target: str, db: str, project: bool, llm_provider: str, llm_model: str, as_json: bool): |
|
5d70f82…
|
lmata
|
2294 |
"""Generate markdown documentation from the graph. |
|
5d70f82…
|
lmata
|
2295 |
|
|
5d70f82…
|
lmata
|
2296 |
TARGET can be a file path or a module name (dotted or partial). |
|
5d70f82…
|
lmata
|
2297 |
Use --project to generate full project docs instead. |
|
5d70f82…
|
lmata
|
2298 |
|
|
5d70f82…
|
lmata
|
2299 |
\b |
|
5d70f82…
|
lmata
|
2300 |
Examples: |
|
5d70f82…
|
lmata
|
2301 |
navegador docs navegador/graph/store.py |
|
5d70f82…
|
lmata
|
2302 |
navegador docs navegador.graph |
|
5d70f82…
|
lmata
|
2303 |
navegador docs . --project |
|
5d70f82…
|
lmata
|
2304 |
navegador docs . --project --provider openai |
|
5d70f82…
|
lmata
|
2305 |
""" |
|
5d70f82…
|
lmata
|
2306 |
from navegador.intelligence.docgen import DocGenerator |
|
5d70f82…
|
lmata
|
2307 |
|
|
5d70f82…
|
lmata
|
2308 |
store = _get_store(db) |
|
5d70f82…
|
lmata
|
2309 |
|
|
5d70f82…
|
lmata
|
2310 |
provider = None |
|
5d70f82…
|
lmata
|
2311 |
if llm_provider: |
|
5d70f82…
|
lmata
|
2312 |
from navegador.llm import get_provider |
|
5d70f82…
|
lmata
|
2313 |
|
|
5d70f82…
|
lmata
|
2314 |
provider = get_provider(llm_provider, model=llm_model) |
|
5d70f82…
|
lmata
|
2315 |
|
|
5d70f82…
|
lmata
|
2316 |
gen = DocGenerator(store, provider=provider) |
|
5d70f82…
|
lmata
|
2317 |
|
|
5d70f82…
|
lmata
|
2318 |
if project: |
|
5d70f82…
|
lmata
|
2319 |
with console.status("[bold]Generating project docs...[/bold]"): |
|
5d70f82…
|
lmata
|
2320 |
output = gen.generate_project_docs() |
|
5d70f82…
|
lmata
|
2321 |
elif "/" in target or target.endswith(".py"): |
|
5d70f82…
|
lmata
|
2322 |
with console.status(f"[bold]Generating docs for file[/bold] {target}..."): |
|
5d70f82…
|
lmata
|
2323 |
output = gen.generate_file_docs(target) |
|
5d70f82…
|
lmata
|
2324 |
else: |
|
5d70f82…
|
lmata
|
2325 |
with console.status(f"[bold]Generating docs for module[/bold] {target}..."): |
|
5d70f82…
|
lmata
|
2326 |
output = gen.generate_module_docs(target) |
|
5d70f82…
|
lmata
|
2327 |
|
|
5d70f82…
|
lmata
|
2328 |
if as_json: |
|
5d70f82…
|
lmata
|
2329 |
click.echo(json.dumps({"docs": output}, indent=2)) |
|
5d70f82…
|
lmata
|
2330 |
else: |
|
5d70f82…
|
lmata
|
2331 |
console.print(output) |