Skip to content

feat(core): port server/tools_viz.py to backend-neutral queries#22

Merged
joy-software merged 1 commit into
developfrom
feature/duckdb-viz
Jun 2, 2026
Merged

feat(core): port server/tools_viz.py to backend-neutral queries#22
joy-software merged 1 commit into
developfrom
feature/duckdb-viz

Conversation

@joy-software
Copy link
Copy Markdown
Contributor

Summary

Sixth PR in the Kuzu → DuckDB migration. The Mermaid / DOT diagram generators in `tools_viz.py` and the `graph_stats` / `live_graph_stats` MCP tools no longer emit Cypher.

What lands

File What changed
`core/protocol.py` 2 additions: `find_neighbors` gains `limit=`, new `count_nodes(label, where=)`
`server/tools_viz.py` Every `conn.execute()` against the graph DB gone. 6 viz helpers + 3 MCP tools ported. `_rows` import gone.

Notable porting decisions:

  • The Cypher `WHERE name = $n OR ...` OR-condition is replaced with two find_neighbors calls unioned in Python (cheap because LIMIT bounds each side).
  • `UNION ALL` for "imports outgoing + incoming" similarly becomes two find_neighbors calls.
  • `GROUP BY` aggregations (files per language, top files by symbol density) done in Python using `collections.Counter` and a small dict tally. Small-N enough that the difference vs SQL GROUP BY is immaterial.

End-to-end verification

3-file Python repo with imports + inheritance:

Stat Kuzu DuckDB
Files 5 5
Functions 2 2
Classes 2 2
IMPORTS edges 2 2
INHERITS edges 1 1

Tests

298 / 298 pass, lint clean. No new tests added in this PR — the existing helper parity sweep (40 tests across both backends) already covers `find_neighbors` with `limit` and `count_nodes` semantics.

Remaining migration work

Sixth step of the Kuzu -> DuckDB migration. The Mermaid / DOT diagram
generators in tools_viz.py and the graph_stats / live_graph_stats
MCP tools no longer emit Cypher. Both backends produce identical
diagram input and identical node counts.

What lands:

- codegraph/core/protocol.py: two helper additions on GraphDB.
  - find_neighbors gets a limit= parameter for the bounded-traversal
    patterns in the viz tools (call graph, class hierarchy, etc.
    all take a max_nodes hint).
  - count_nodes(label, where=...) for the 6 "count of nodes" sites
    in graph_stats and live_graph_stats — single COUNT(*) per call
    on DuckDB, one cheap MATCH count() on Kuzu.

- codegraph/server/tools_viz.py: every conn.execute() against the
  graph DB gone. _rows import gone.
  - _viz_file_imports: two find_neighbors calls (outgoing + incoming)
    replacing the UNION ALL pattern. Filtered or unfiltered via the
    src_key / no-anchor distinction.
  - _viz_call_graph and _viz_class_hierarchy: when symbol_name is
    supplied, the Cypher "WHERE caller.name = $n OR callee.name = $n"
    becomes two filtered find_neighbors calls unioned in Python.
    Cheap because LIMIT bounds each side.
  - _viz_file_symbols: three find_nodes calls (Function / Class /
    MdSection by file_path) with order_by=['start_line'].
  - _viz_doc_structure: one find_nodes call with file_path filter
    + order_by + limit.
  - _viz_full_overview: file aggregation by language done in Python
    (Counter) since GROUP BY isn't a protocol primitive yet. Top
    files by symbol density similarly aggregated from find_neighbors
    over DEFINES_FN edges. Both small-N enough that the Python pass
    is negligible.
  - graph_stats / live_graph_stats: 6 conn.execute count() calls
    each become count_nodes() helper calls.

  The SQLite FTS count in live_graph_stats stays as direct fts_conn
  .execute() — that's a different database and out of scope for the
  graph backend swap.

Test count: 298 (unchanged; no new tests, the existing 298-test
suite already covers find_neighbors with limit + count semantics
through the helper parity sweep).

End-to-end smoke on a 3-file Python repo:

  Kuzu:    Files=5 Functions=2 Classes=2 IMPORTS=2 INHERITS=1
  DuckDB:  Files=5 Functions=2 Classes=2 IMPORTS=2 INHERITS=1

What's left for the migration:

- cli/commands_monitor.py (12 stats queries — task #10)
- federation.py (kuzu-direct child DB opens — task #11)
@joy-software joy-software merged commit b0994f3 into develop Jun 2, 2026
1 check passed
@joy-software joy-software deleted the feature/duckdb-viz branch June 2, 2026 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant