Guides / Code_structure · Edit on GitHub

Code structure

Guide for navigating the rgctl workspace: how crates are segmented, how they connect, and where to put new functionality so it is not duplicated elsewhere.


1. Crate segmentation (overview)

Rendering diagram…

Reading the diagram: Data generally flows down and left-to-right during discover: registry → extraction → graph → analysis → persisted .rgctl/ artifacts. Query commands (find, blast-radius, inspect) read the graph and analysis layers without re-parsing source unless slicing or CFG is required.


2. Segmented design (details)

Design principles

PrincipleWhat it means in practice
One graph modelAll nodes/edges live in rgctl-graph. Do not invent a parallel graph type in CLI or analysis code.
Plugins extract, pipeline orchestratesLanguage-specific parsing stays in rgctl-lang-* (via LanguagePlugin). File walking and graph assembly stay in rgctl-extraction / rgctl-pipeline.
Analysis is graph-onlyAlgorithms in rgctl-analysis take MemoryBackend, PetGraphView, or snapshots — not raw source files (except CFG/PDG/slice paths that explicitly need source).
CLI is thinsrc/cli/ parses args, resolves paths, calls library crates. Heavy logic belongs in workspace crates, not new src/cli/*.rs helpers. JSON shape lives in *_output.rs; graph/cache enrichment stays in rgctl-analysis.
Errors are centralizedUse rgctl_error::Error / Result from rgctl-error. Do not add ad-hoc error enums in the CLI.
All languages always linkedThe binary always includes every Tier 1 LanguagePlugin in rgctl-languages (see languages.toml).

Layer responsibilities

Entry (rgctl root crate)

  • src/main.rs — process entry, dispatches to CLI.
  • src/cli/ — subcommands: discover, find / callers / relations / …, blast-radius, serve, slice, inspect, metrics, semantic, communities, cpg, check, export.
  • src/cli/http_serve.rs — serve: dashboard + POST /api/query (foreground HTTP for one repo).
  • src/cli/session_status.rs — status: cheap snapshot / digest / node-edge summary.
  • src/cli/*_output.rs — typed JSON serializers (blast_radius_output, discover_output, structured-query envelopes, …). Commands assemble domain results from workspace crates and serialize here; do not embed algorithm logic in output modules.
  • src/languages/ — wires the active language bundle into a LanguageRegistry at runtime.
  • Re-exports rgctl-core for library users (use rgctl::analysis, etc.).

Put new user-facing commands here; implement behavior in the appropriate workspace crate.

Facade (rgctl-core)

Stable “library surface” for embedders: re-exports graph, analysis, pipeline, export, incremental, registry, rules, semantic, security, project-config. Also hosts memory monitoring helpers used during discover.

If you add a new workspace crate that external tools should use, export it through rgctl-core (and optionally the root rgctl crate).

Plugin system

CrateRole
rgctl-plugin-apiTraits and types: LanguagePlugin, Symbol, relations, config format plugins. Contract all languages implement.
rgctl-plugin-helpersShared tree-sitter/complexity utilities for plugin authors.
rgctl-lang-runtimeConfig-driven generic plugins (tree-sitter / regex) for simple languages.
rgctl-config-formatsNon-code config parsers (YAML, JSON, TOML, properties).
rgctl-lang-markdownCustom markup plugin for .md / .mdx (context graph; not Tier 1).
rgctl-registryLanguageRegistry, dynamic plugin loading, full_registry().
rgctl-languagesRegisters all Tier 1 lang crates at link time.
rgctl-lang-*Per-language implementations (see note below).
rgctl-macros#[derive(LanguagePlugin)] and related proc macros.

Language crates (rgctl-lang-*): One crate per language or config dialect (e.g. rgctl-lang-java, rgctl-lang-github-actions). Each registers a plugin with the registry. Do not add parsing logic for an existing language in another language crate — extend the relevant rgctl-lang-* plugin instead. For Tier 1 / full analysis parity, see tier-1-language-support.md.

Ingestion pipeline

CrateRole
rgctl-extractionFileDiscoverer, Extractor, GraphBuilder — turns plugin output into graph mutations.
rgctl-pipelineProcessingPipeline — parallel repo processing, progress, stats; calls extraction + graph.
rgctl-incrementalFileTracker, change detection, incremental graph updates between discovers.

Discover flow: CLI discover → discover_impl → ProcessingPipeline → plugins → CodeGraph → analysis passes → write .rgctl/.

Graph storage (rgctl-graph)

  • CodeGraph — high-level API over the backend.
  • backend/ — MemoryBackend, indexes, batch insert, query.
  • schema/ — Node, Edge, NodeType, EdgeType.
  • snapshot/ — columnar v2 mmap snapshots (graph.snapshot.bin, 64B node / 40B edge rows + string pool); v1 bincode still readable. SnapshotNodeStore, ColumnarGraphMmap.
  • export/ / import_json — JSON serialization (legacy graph.db).
  • query/ — simple string queries over the backend.

All persistent graph topology belongs here. Analysis results that attach to nodes may use rgctl-analysis::results columnar tables, not new graph backends.

Graph analytics (rgctl-analysis)

Single home for graph algorithms and semantic analysis:

Module areaExamples
Impact / structureblast_radius_scc, blast_engine_snapshot, macro_call_index, macro_call_lookup, graph_utils (filter_impact_by_caller_depth, PetGraphView), dependency, callgraph
Metricscentrality, community, complexity
Control / data flowcfg, cfg_builder, pdg, dominance, dataflow, def_use, slicing, interprocedural_*
Security-ish analysistaint, policy
Projectionsgraph_utils (PetGraphView)
Persistenceresults (columnar analysis tables), storage
Handoffblast_slice_handoff (blast → slice seeds)

Do not reimplement SCC blast radius, PageRank, community detection, or CFG building outside this crate.

Query & export

CrateRole
rgctl-exportDashboard HTML, Mermaid, Graphviz/DOT, GraphML; subgraph selection from graph queries.

Cross-cutting

CrateRole
rgctl-semanticFunction signatures, type inference helpers, IDL generation — source-level semantics, not graph storage.
rgctl-rulesDeclarative rulesets for automatic node labeling.
rgctl-securitySecurity analyzer and CWE/CVE pattern matching over graph/content.
rgctl-project-config.rgctl project file, config drift, secret detection in config files.
rgctl-errorShared Error enum used across crates.

Where CLI commands map

CommandPrimary crates
discoverpipeline, extraction, registry, graph, analysis, incremental, export, project-config; stdout JSON via discover_output when -f json
blast-radiusanalysis (engine + macro index + depth filter), graph (columnar snapshot mmap); CLI orchestration in blast_radius.rs
servehttp_serve — foreground HTTP dashboard + /api/query
statusSession graph presence / digest / counts
find / callers / relations / inventorygraph (structured query)
sliceanalysis (CFG, PDG, slicing), reads source from disk
inspectgraph, analysis
metricsanalysis (centrality, community)
checkanalysis (policies, blast radius)
exportexport, graph

On-disk artifacts (.rgctl/)

Understanding files helps avoid duplicating cache layers:

FileProduced byConsumed by
graph.db / graph.jsondiscover (JSON)Legacy load paths
graph.snapshot.bindiscover (columnar v2 default)CodeGraph::open_snapshot, SnapshotNodeStore, ColumnarGraphMmap, serve
blast_engine.snapshot.bindiscovertry_load_engine, lite blast-radius path, serve
macro_call_index.db / .bindiscoverblast-radius T0 fast path only — SQLite/bincode lookup cache, not the graph
cfg_pdg.archive.bindiscover --with-cfgblast-radius --with-slices, slice hand-offs
query.sock(retired)Former blast-radius Unix-socket client (v0.4.7); removed with daemon mode
analysis_results.bindiscoverColumnar metrics (CentralityTable, community, blast); blast columns may stay empty on flat/on-demand graphs (bulk fill skipped — #28 won't-fix; use live blast-radius)
dashboard/ (bundle)discoverBrowser static dashboard (index.html, manifest.json, graph_payload.bin)

3. Crate reference (non-language)

Alphabetical list of workspace crates excluding individual rgctl-lang-* plugins.

CratePathPurpose
rgctl.CLI binary, command dispatch, language bundle wiring, public library root.
rgctl-analysiscrates/rgctl-analysisGraph algorithms: blast radius, centrality, community, CFG/PDG, slicing, taint, policies, caches, PetGraphView.
rgctl-languagescrates/rgctl-languagesRegisters all Tier 1 language plugins (Rust, Python, JS/TS, Go, Java, C#, C, C++).
rgctl-config-formatscrates/rgctl-config-formatsConfig file plugins (YAML, JSON, TOML, properties).
rgctl-lang-markdowncrates/rgctl-lang-markdownMarkdown context graph (headings, links, frontmatter).
rgctl-corecrates/rgctl-coreFacade crate re-exporting the stable library API for embedders.
rgctl-errorcrates/rgctl-errorShared error types (Error, Result) for the whole workspace.
rgctl-exportcrates/rgctl-exportExport graph and analysis to HTML dashboard, Mermaid, GraphML, Graphviz.
rgctl-extractioncrates/rgctl-extractionFile discovery, extraction orchestration, graph building from plugin output.
rgctl-graphcrates/rgctl-graphCode knowledge graph storage, schema, indexes, JSON import/export, mmap snapshots.
rgctl-incrementalcrates/rgctl-incrementalIncremental updates, file tracking, change detection between indexing runs.
rgctl-lang-runtimecrates/rgctl-lang-runtimeGeneric tree-sitter and regex language plugins from static config.
rgctl-macrosrgctl-macrosProcedural macros for language plugin boilerplate.
rgctl-pipelinecrates/rgctl-pipelineParallel repository processing pipeline (discover/index entry point).
rgctl-plugin-apicrates/rgctl-plugin-apiCore plugin traits, symbol/ relation types, config format registrar.
rgctl-plugin-helperscrates/rgctl-plugin-helpersShared extraction helpers (tree-sitter utilities, complexity calculator).
rgctl-project-configcrates/rgctl-project-configProject-level config, secret scanning, config drift analysis.
rgctl-registrycrates/rgctl-registryLanguage plugin registry and optional dynamic plugin loading.
rgctl-rulescrates/rgctl-rulesRule engine for automatic graph labeling from declarative rulesets.
rgctl-securitycrates/rgctl-securitySecurity vulnerability analysis and CWE pattern library.
rgctl-semanticcrates/rgctl-semanticSignature extraction, type inference, IDL generation from source.

Language implementations (rgctl-lang-*)

There are many crates named rgctl-lang-<language> (and a few for CI/config dialects). Each implements LanguagePlugin (or a config plugin) for one language or format. They are registered through rgctl-registry and rgctl-languages — not linked directly from analysis or graph code.

When adding or fixing language support:

  1. Change or add a rgctl-lang-* crate.
  2. Register it in rgctl-languages.
  3. Do not add language-specific parsing to rgctl-analysis or src/cli/.

Quick “where do I put this?” table

I want to…Put it in…
Parse a new language constructRelevant rgctl-lang-* plugin
Add a graph edge type or node propertyrgctl-graph schema + migration
Add a graph algorithm (impact, metrics, flow)rgctl-analysis
Add a CLI flag or subcommandsrc/cli/ + call into library crate
Add --depth or query-tier behaviorgraph_utils filter + blast_radius.rs paths (cache, lite, full)
Add CLI JSON schema / fieldsrc/cli/<command>_output.rs + tests/cli_output/ (Layer 1)
Add subprocess regression for CLIsubprocess_golden_path.rs (narrow) or all_commands_sanity.rs (full audit) + tests/fixtures/ — see cli-io-sanity-qe.md
Add a structured query filter or verbrgctl-graph + src/cli/structured_query.rs
Add HTML/Mermaid/GraphML outputrgctl-export
Add a discover-time cache filediscover_impl writer + relevant analysis/graph module reader
Add a labeling or policy rulergctl-rules or rgctl-analysis::policy
Add shared error variantrgctl-error

Related docs: user-guide.md, json-api.md, dashboard-design.md, cli-io-sanity-qe.md, graph-storage-architecture.md, CLI_STRUCTURE.txt.