1
---
2
name: monorepo-doctor
3
description: Use when a user asks for a JS/TS monorepo audit, or Bazel target visibility or testonly issues in a JS or Java workspace. Also covers Nx/Turborepo caching, affected tests, cycles and production test leakage. Read-only; writes Markdown and JSON reports.
4
---
5
6
# Monorepo Doctor
7
8
Audit a JS/TS monorepo for five classes of problems that teams routinely miss,
9
or run the limited Bazel target checks in a standalone Bazel workspace, and
10
produce an evidence-backed report. Bazel-only support does not analyze Java
11
source, cycles, caching, or CI test coverage.
12
13
Prerequisites: Node ≥ 22.5. The work dir is `$W` = `$(node -p 'require("os").tmpdir()')/monorepo-doctor` (see detection.md).
14
15
## Rules
16
17
- **Read-only.** Never install dependencies, build, run tests, or edit project files. Allowed writes: `monorepo-doctor-report.md`, `monorepo-doctor-report.json` (default `${tmpdir}/monorepo-doctor-reports`, or a user-given dir) and `$W`.
18
- Allowed commands: the snippets in the reference files, `npm pack --dry-run --json --ignore-scripts`, read-only `git log`, and the repo's own `./node_modules/.bin/{knip,publint,nx,turbo}` **only if** detection reports them installed (nx/turbo: graph / `--dry=json` / `boundaries` only — never real task runs, with read-only env vars from the references).
19
- **No runtime downloads by default.** Don't fetch packages (`npx`, `npm exec`, `pnpm dlx`, `yarn dlx`, `bunx`). If a tool isn't installed, skip that sub-step and mark the check `partial`. Only if the user explicitly allows downloads in this request may you run the pinned `knip@6.41.0` / `publint@0.3.25` shown in the references — never other versions, other packages, or nx/turbo.
20
- **Never abort a check.** If a tool fails, mark the check `partial` or `skipped` with a reason and continue.
21
- **Every finding needs evidence** (config snippet, file:line, or command output). No evidence → no finding.
22
- **Actionable only.** Do not emit a finding when the honest fix is "keep it as-is" — a deliberate, idiomatic or correct pattern (e.g. `cache: false` on a migration task, lazy ORM references). Mention it in the check's `reason` instead.
23
- **One root cause, one finding.** If two findings share the same fix, merge them.
24
- Use only the finding ids defined in the reference files.
25
26
## Workflow
27
28
1. **Detect** — follow [references/detection.md](references/detection.md). Stop if `NO_WORKSPACE`.
29
2. **Scan** — follow [references/scan.md](references/scan.md). If Bazel is detected, also run the static inspection in [references/bazel.md](references/bazel.md) before the checks.
30
3. **Check** — run all five unless the user asked for a subset (write `{"status":"not-run","reason":"not requested","findings":[]}` to `$W/findings/${check}.json` for the others):
31
- [references/boundaries.md](references/boundaries.md) — package split & public API, including Bazel visibility
32
- [references/circular-deps.md](references/circular-deps.md) — package and file cycles
33
- [references/caching.md](references/caching.md) — Nx / Turborepo cache correctness
34
- [references/ci-coverage.md](references/ci-coverage.md) — tests that silently don't run in CI
35
- [references/prod-leakage.md](references/prod-leakage.md) — test code shipped to production
36
The checks are independent; if you can run sub-agents, run them in parallel.
37
4. **Report** — follow [references/report-format.md](references/report-format.md): validate each `findings/${check}.json`, render, then show the user where the reports were written, the summary line and the top 5 findings.
38
5. **CI mode** (only if asked, or when invoked non-interactively with a threshold) — run the exit-code snippet from report-format.md.
39
40
## Tracking
41
42
Create a todo per phase/check and mark each done as its findings file is written.
43
44