# normalize (main binary crate)

The primary CLI binary crate that wires together all normalize sub-crates into the `normalize` command. It depends on roughly 30 domain crates (`normalize-facts`, `normalize-languages`, `normalize-edit`, `normalize-session-analysis`, etc.) and exposes them through a unified service layer built with the server-less `#[cli]` proc macro. Three embedded CLI tools are included as optional features: `rg` (ripgrep), `jq` (jaq), and `ast-grep`/`sg`, each invocable via subcommand or via symlink to the binary. The default feature is `cli-full = ["cli", "jq-cli", "rg-cli", "ast-grep-cli"]`. The binary tagline is "normalize - Structural code intelligence: index symbols and calls, enforce rules, track complexity."

Recent notable additions: `normalize grammars install` (and the first-run auto-install path) now defaults to the binary's own version (`v$CARGO_PKG_VERSION`) instead of GitHub's `releases/latest` — prevents silent ABI mismatches when an old binary fetches grammars from a newer release; rusqlite/libsql co-link conflict resolved by migrating ca_cache + native-rules + syntax-rules caches to libsql; `init_sqlite_serialized` workaround removed; `dead-parameter` native rule (scope-based unused parameter detection via `normalize-scope`); `normalize edit inline-function <file> <line>:<col>` — inlines a single-use function at its call site (recipe in `normalize-refactor`); `normalize sync` command copies a project + session metadata to a destination with `--dry-run`, `--all`, `--active N`, `--repo`/`--exclude` filters, and post-copy index path rewriting via libsql; `normalize rules run` returns typed `DiagnosticsReport` with `--sarif`/`--pretty` output; `normalize-context` crate extracted for frontmatter-filtered context assembly; `normalize grep` groups consecutive same-symbol matches under a single header; `normalize rules run --only`/`--exclude` pre-walk scoping via `PathFilter`; `normalize-refactor` crate for semantic batch edit actions; service doc strings enriched with synonym terms for discoverability; `index::ensure_ready_or_warn()` auto-builds the structural index for enrichment commands; `normalize rules test` command with inline error annotations; LSP publishes native rule diagnostics; daemon diagnostics persisted to SQLite to eliminate memory leak; watcher consolidated to single shared thread; `normalize-ca-cache` inlined into `normalize-facts/src/ca_cache.rs` (crate removed); daemon uses rkyv binary IPC with per-file diagnostic storage and pushes deltas via binary subscribe; persistent index connection per daemon root eliminates SQLite lock races; daemon `DaemonClient` captures socket path at construction; daemon `.scm` hash tracking for Tier 2 surgical re-eval of custom rules on file change; re-export tracing in `normalize-facts` (`pub use` chains followed at index time, schema v12).

The `src/commands/` directory holds domain logic (report structs, `OutputFormatter` impls, computation functions) organized by feature area. The `src/service/` directory is the server-less `#[cli]`-annotated registration layer; most service methods return `Result<T, String>` with `?` propagation, with `AnalyzeService` methods returning `Result<T, AnalyzeError>`. The root `NormalizeService` composes sub-services: `AnalyzeService`, `BudgetService`, `CiService`, `ConfigService`, `DaemonService`, `EditService`, `FactsService`, `GrammarService`, `GuideService`, `GenerateService`, `PackageService`, `RankService`, `RatchetService`, `RenameService`, `RulesService` (from `normalize-rules`), `ServeService`, `SessionsService`, `SyntaxService`, `ToolsService`, and `ViewService`. `main()` is `#[tokio::main] async fn`; service methods are `async fn`. The `CiService` (`normalize ci`) runs all rule engines (syntax, native, ratchet, budget) in a single pass. `ViewService` prepends directory context files by default (configurable via `[view] context_files`; suppressed with `--no-dir-context`; `--dir-context <N>` controls how many ancestor SUMMARY.md levels are included); `ViewReport.summary` carries SUMMARY.md content for dirs or module-level doc comments for files. `RenameService` implements cross-file symbol rename with `--dry-run`. `RulesService` (from `normalize-rules`) includes `compile` for `.dl` validation; `show`/`tags` return structured JSON. `DaemonService` exposes `DaemonRootsReport.running: bool` — exits 0 when daemon is not running. The `sessions` command family supports a unified `--sort <field>[,<field>]` (with optional `-`/`+` prefix) across `messages`, `list`, `stats`, and `patterns`; when `--sort` is explicit, message-level truncation is applied after global sorting so `--sort=-tokens --limit N` returns the top-N messages across all sessions.
