Skip to content

feat(devkit): add dependency topology scanner script#361

Open
jensenojs wants to merge 2 commits intosudo-tee:mainfrom
jensenojs:feat/dependency-topology-tool
Open

feat(devkit): add dependency topology scanner script#361
jensenojs wants to merge 2 commits intosudo-tee:mainfrom
jensenojs:feat/dependency-topology-tool

Conversation

@jensenojs
Copy link
Copy Markdown
Contributor

@jensenojs jensenojs commented Apr 20, 2026

During the process of splitting core.lua, the code does not seem to have become clearer. #360

After running some static checks, it was found that after deleting core.lua, the cycle dependency graph did not decrease but instead increased—the largest strongly connected component expanded from 34 modules to 36 modules. Modules such as services.session_runtime took over the central position originally held by core and merged into the main cycle.

#360 is heading in the right direction, but what it does is more like exposing the issues that were originally hidden in core.lua rather than resolving them.


We’ve been refactoring without a global view of the dependency graph, any cleanup we did is just hitting a local optimum—we might be fixing one spot while making the overall structure worse.

This PR provides a tool, and I hope it serves as a good start

  • Statically scans require dependencies in lua/opencode/**/*.lua to build a graph.
  • Detects Strongly Connected Components (SCC, i.e., circular dependencies) and policy violations (cross-layer violations).
  • Generate interactive HTML visualizations and agent-friendly JSON output.

Current baseline (upstream/main): 4 SCCs, with the largest cycle containing 34 modules.

Also worth mentioning is that I vibed a topology.jsonc, which roughly aligns with my previous architectural design diagram, targeting a layered architecture (5 layers: entry / dispatch / capabilities / infrastructure / foundation) and specifying which directional dependencies are prohibited (this is a very rough, very rough direction). #322 (comment)

After parsing the topology of the call chain, this part of the rules will be further used to evaluate the rationality of the topology itself.

I'm much less familiar with the project than you are, so I need you to evaluate it @sudo-tee . Once you adjust this jsonc, you'll have a tool to check whether the project's current shape matches your expectations and where it diverges. I can then make further project changes guided by that assessment.

Copilot AI review requested due to automatic review settings April 20, 2026 08:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Python-based static dependency topology scanner for the lua/opencode/** Lua modules, with configurable layer/group policy rules and optional HTML visualization to support architectural cleanup discussions.

Changes:

  • Introduces a topology strategy file (topology.jsonc) defining layers/groups and forbidden dependency directions.
  • Adds scanner CLI (scan / diff) plus core analysis + graph utilities for SCC/cycle and policy-violation detection.
  • Adds an interactive HTML renderer for clustered dependency visualization, and documents the tool for agents/contributors.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
scripts/dependency-topology/topology.jsonc Defines layer groups and policy rules for dependency analysis.
scripts/dependency-topology/scan_topology.py CLI entrypoint for scan/diff, emits JSON or HTML output.
scripts/dependency-topology/scan_analysis.py Implements grouping, policy evaluation, and scan/diff payload construction.
scripts/dependency-topology/graph_utils.py Builds module graph from require() usage and provides SCC/cycle helpers.
scripts/dependency-topology/html_renderer.py Renders interactive dagre-d3 HTML with clustering and violation styling.
scripts/dependency-topology/AGENTS.md Local documentation for using the topology scanner.
AGENTS.md Repo-level guidance referencing the topology scanner for architecture discussions.
.gitignore Ignores topology tool artifacts (pycache + HTML output).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread scripts/dependency-topology/graph_utils.py
Comment thread scripts/dependency-topology/scan_analysis.py
Comment thread scripts/dependency-topology/scan_analysis.py Outdated
Comment thread scripts/dependency-topology/html_renderer.py Outdated
Comment thread scripts/dependency-topology/html_renderer.py
Comment thread scripts/dependency-topology/scan_topology.py
Static analysis tool for Lua codebase architectural layering.

- scan_topology.py: CLI entry (scan / diff subcommands)
- scan_analysis.py: core analysis, group matching, policy violation detection
- graph_utils.py: pure graph algorithms (Tarjan SCC, back-edges, degree)
- html_renderer.py: interactive dagre-d3 HTML visualization with
  cluster expand/collapse, violation highlighting, SCC marking
- topology.jsonc: 5-layer group definitions with English comments
  explaining each module placement and REVIEW notes for debatable calls

scan --json produces agent-friendly output:
  health summary (cycles/violations/ungrouped with verdicts)
  cycles with severity, members_by_layer, example_cycle path, back_edges
  violations grouped by rule with full edge lists
  group_coverage confirming 0 ungrouped modules
- graph_utils: clear error message on invalid git ref
- scan_analysis: remove unused imports (degree, largest_scc_size)
- html_renderer: defensive isinstance guard in match_group;
  fix expanded cluster edges — children now show actual connections
  via rawEdges projection instead of appearing isolated
- scan_topology: eliminate duplicate graph/SCC computation in cmd_scan;
  smart diff default (dirty worktree → HEAD vs worktree, clean → HEAD~1 vs HEAD);
  expand diff human output with SCC changes, fixed/new violations per edge
- add requirements.txt declaring json5 dependency
- AGENTS.md: document diff default behavior and ref syntax
@jensenojs jensenojs force-pushed the feat/dependency-topology-tool branch from f94a0f8 to 0d1bc13 Compare April 20, 2026 09:41
@jensenojs jensenojs changed the title feat(devkit): add dependency topology scanner scripts feat(devkit): add dependency topology scanner script Apr 20, 2026
@jensenojs
Copy link
Copy Markdown
Contributor Author

jensenojs commented Apr 20, 2026

Here is an example of usage,

iShot_2026-04-20_19 19 17_under10mb_safe

@sudo-tee
Copy link
Copy Markdown
Owner

@jensenojs

Thanks for the script I will have a look at it soon. I have a pretty busy week ahead, so it might take a couple of days before I can test it properly

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.

3 participants