From 0fa6e0226fdb6c53fe05bba959a3f1be0d920a9e Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Sun, 9 Aug 2026 02:33:11 -0600 Subject: [PATCH 1/2] docs: add .codegraph/basics.md as the org-standard agent entry point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every Optave repo is getting a committed .codegraph/basics.md that agents read to orient before touching unfamiliar code. This adds ours. The .gitignore rule must ignore the directory *contents* (.codegraph/*), not the directory (.codegraph/) — git refuses to descend into an ignored directory, which silently voids the !.codegraph/basics.md negation. Verified empirically. basics.md records verified numbers from a full self-build on 3.16.0 (1037 files, 21770 nodes, 44877 edges, 2 file-level cycles, quality 87/100) and flags that the -T dead-code figure is inflated ~3x by test fixtures (#2256). --- .codegraph/basics.md | 88 ++++++++++++++++++++++++++++++++++++++++++++ .gitignore | 6 ++- CLAUDE.md | 2 + 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 .codegraph/basics.md diff --git a/.codegraph/basics.md b/.codegraph/basics.md new file mode 100644 index 000000000..045a5ca43 --- /dev/null +++ b/.codegraph/basics.md @@ -0,0 +1,88 @@ +# Codegraph — ops-codegraph-tool + +Codegraph builds a function-level dependency graph of this repo (tree-sitter → SQLite) and answers +structural questions offline. It is set up here. **Prefer it over blind `grep` when orienting in, +modifying, or committing unfamiliar code** — grep finds strings; codegraph finds callers, blast radius, +and cycles. + +This repo *is* codegraph, so it dogfoods itself: if a command errors or returns a wrong answer here, +that is a real bug — fix it or file it, never work around it (see `CLAUDE.md`). + +## Setup + +```bash +npm i -g @optave/codegraph # once per machine +codegraph build # from repo root; re-run after pulling +``` + +Working on the source instead of the published CLI? `npm install && npm run build`, then use +`node dist/cli.js ` so you exercise your changes rather than the globally-installed release. + +The graph lives at `.codegraph/graph.db` — local build output, gitignored. Only this file is committed. +`.codegraphrc.json` excludes `crates/**` (the Rust engine is analysed separately from the TS tree). + +## When to use what + +| Situation | Command | +|---|---| +| "Where is this symbol?" | `codegraph where ` | +| "What does this function touch, and who calls it?" | `codegraph context -T` | +| "What breaks if I change this?" | `codegraph fn-impact -T` | +| "What depends on this file?" | `codegraph impact -T` | +| "What does this file import / export to whom?" | `codegraph deps ` · `codegraph exports -T` | +| Before committing | `codegraph diff-impact --staged -T` | +| Getting oriented in a new area | `codegraph brief ` · `codegraph structure --depth 2` | +| Understanding a whole subsystem | `codegraph audit --quick ` | +| Finding dead code | `codegraph roles --role dead -T` (read the caveat below) | +| Health / hotspots | `codegraph stats` · `codegraph map` · `codegraph triage -T` | + +`-T` excludes test files. `-j` emits JSON. Full list: `codegraph --help`. + +## This repo + +- **Languages:** TypeScript (343 files under `src/`) plus a Rust engine in `crates/`. The graph reports + 34 languages across 1037 files — that spread comes from the per-language parser fixtures under + `tests/benchmarks/resolution/fixtures/`, not from production source. +- **Size:** 1037 files · 21,770 nodes · 44,877 edges. Full build ≈ 2.5s on the native engine. +- **Entrypoints:** `src/cli.ts` (the `codegraph` bin) and `src/index.ts` (programmatic API). + Note that `codegraph roles --role entry` also surfaces ~200 hook and script callbacks under + `docs/examples/` and `scripts/` — those are not real entrypoints. +- **Layout:** `shared/` and `infrastructure/` (cross-cutting) → `db/` → `domain/` (parse, resolve, + query) → `features/` (composable analyses) → `presentation/` (formatting + CLI wrappers), with + `graph/` holding the unified model and algorithms. `crates/codegraph-core/` mirrors this tree in + Rust. See `CLAUDE.md` for the full module table. +- **Most-connected modules** — change these carefully: + + | File | fan-in | fan-out | + |---|---|---| + | `src/types.ts` | 230 | 1322 | + | `src/db/index.ts` | 139 | 95 | + | `src/domain/parser.ts` | 88 | 156 | + | `src/extractors/javascript.ts` | 7 | 236 | + | `src/domain/graph/builder/stages/build-edges.ts` | 3 | 169 | + +## Health baseline (as of 2026-08-09, codegraph 3.16.0) + +- **Graph quality:** 87/100 · caller coverage 79.1% (4039/5107 functions have ≥1 caller) +- **Cycles:** 2 file-level, 6 function-level. Both file-level cycles are 2-file pairs: + - `db/repository/native-repository.ts` ↔ `db/connection.ts` + - `domain/graph/builder/stages/resolve-imports.ts` ↔ `domain/graph/builder/context.ts` +- **Dead symbols:** 809 raw · 647 with `-T`. **Neither number is the real one** — see the caveat below. + +Treat these as a baseline, not a target: don't let them grow. `.claude/hooks/pre-commit.sh` already +blocks commits that add cycles or dead exports. + +## Gotchas + +- **`-T` under-filters this repo — the dead-code count is inflated ~3x.** `-T` matches `.test.`, + `.spec.`, `__test__`, `__tests__`, and `.stories.`, but *not* a plain `tests/` directory. 431 of the + 647 `-T` symbols are hand-authored parser fixtures under `tests/benchmarks/resolution/fixtures/`, + which are dead by design. The real figure is closer to **~216**. Tracked in + [#2256](https://github.com/optave/ops-codegraph-tool/issues/2256) — the predicate is defined in three + places (`src/infrastructure/test-filter.ts`, `src/db/query-builder.ts`, and the Rust + `graph/classifiers/roles.rs`) and all three must move together. Until it lands, filter `tests/` out + yourself before trusting a dead-code number here. +- **The 34-language file count is fixtures, not product code.** Don't read it as scope. +- **Two engines must agree.** `--engine native|wasm|auto` (default `auto`). A divergence between them is + a bug in the less-accurate engine, never an acceptable gap — see `CLAUDE.md` and `/parity`. +- **Embeddings are not built** by default; `codegraph search` needs `codegraph embed` first. diff --git a/.gitignore b/.gitignore index d8e9f3c1a..169b6681b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ node_modules/ -.codegraph/ +# codegraph — graph DB is local build output; basics.md is committed guidance for agents. +# Must be `.codegraph/*`, not `.codegraph/`: ignoring the directory stops git descending +# into it, which silently voids the negation below. +.codegraph/* +!.codegraph/basics.md dist/ .tsbuildinfo *.db diff --git a/CLAUDE.md b/CLAUDE.md index 841a7269b..5bf253a2f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -16,6 +16,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Codegraph Workflow +> **New to this repo?** Read [`.codegraph/basics.md`](.codegraph/basics.md) — it carries this repo's structure, entrypoints, coupling hotspots, and health baseline, plus the caveats that make raw numbers here misleading (notably that `-T` under-filters `tests/`, inflating the dead-code count ~3x — [#2256](https://github.com/optave/ops-codegraph-tool/issues/2256)). It is the org-standard entry point: every Optave repo has one. + Hooks handle: file-level deps on reads, graph rebuild after edits, commit-time checks (cycles, dead exports, diff-impact, lint). **Use these for function-level understanding when modifying source code:** ### Before modifying code: From eef3ac73b7d52734dbf010f93369e4d198797f6e Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Sun, 9 Aug 2026 02:47:20 -0600 Subject: [PATCH 2/2] docs: use map-derived coupling numbers, not stats hotspots The stats 'coupling hotspots' fan-out conflates intra-file containment with cross-file coupling: src/types.ts reports fan-out 1322 against 1323 symbols declared in that file. codegraph map reports 0 outbound for the same file. Swap the table to map-derived numbers and warn against substituting the stats figure, since the two commands use similar labels for different metrics. --- .codegraph/basics.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.codegraph/basics.md b/.codegraph/basics.md index 045a5ca43..d643687d2 100644 --- a/.codegraph/basics.md +++ b/.codegraph/basics.md @@ -53,13 +53,18 @@ The graph lives at `.codegraph/graph.db` — local build output, gitignored. Onl Rust. See `CLAUDE.md` for the full module table. - **Most-connected modules** — change these carefully: - | File | fan-in | fan-out | - |---|---|---| - | `src/types.ts` | 230 | 1322 | - | `src/db/index.ts` | 139 | 95 | - | `src/domain/parser.ts` | 88 | 156 | - | `src/extractors/javascript.ts` | 7 | 236 | - | `src/domain/graph/builder/stages/build-edges.ts` | 3 | 169 | + Numbers below are from `codegraph map` (cross-file coupling). Do **not** substitute the fan-out + column from `codegraph stats`' "coupling hotspots" — the two commands report different metrics under + similar labels, and the `stats` figure is inflated by intra-file containment. `src/types.ts` shows + fan-out 1322 there against 1323 symbols declared in the file, which is not cross-file coupling. + + | File | fan-in | fan-out | total | + |---|---|---|---| + | `src/db/index.ts` | 138 | 95 | 233 | + | `src/types.ts` | 229 | 0 | 229 | + | `src/domain/parser.ts` | 87 | 82 | 169 | + | `src/domain/graph/builder/pipeline.ts` | 97 | 30 | 127 | + | `src/domain/graph/builder.ts` | 96 | 9 | 105 | ## Health baseline (as of 2026-08-09, codegraph 3.16.0)