Codebase Audit for AI-Assisted Development

Dan Greer · · 13 min read

Part of the AI Code Quality guide. Start with How to Reduce AI Coding Mistakes: A Practical Guide

Developers reviewing code on screen during codebase audit for AI assisted development

A passing test suite can leave a basic question unanswered: does production ever reach the function you just added?

That's where I'd start a codebase audit for AI-assisted development. The useful result is a finding list tied to a file, a revision, a saved configuration, and enough evidence to decide what needs changing. A general verdict that the repository looks healthy doesn't give you much to work with.

The runbook below covers code that's already been written. For the checks to put around the next change, see reducing AI coding mistakes.

Start with a revision you can name

Pick the commit you're auditing and record its ID beside the reports. Keep the source tree unchanged during the scan, record the tool versions, save the configuration, and write down the directories you excluded so someone else can reproduce the finding.

Use Node.js 24 for the TypeScript examples below, with source under src and pnpm available.

Change that path for your repository. Run the same configuration against your baseline and the revision under review.

Don't fix things yet.

An ignored directory, a missing generated file, or an entry point the scanner hasn't recognized can change what a report means, and I'd rather sort that out before an agent starts removing exports. Keep dependency and build output out of the source scans, but include the application packages you actually ship.

Check copied code with jscpd

jscpd reports matching token sequences in its default clone-detection pass.

Its agent skill documents this AI-readable reporter:

npx jscpd --reporters ai --ignore "**/node_modules/**,**/dist/**" src

A trimmed example from the vendor's agent skill, rather than a scan of a real project.

Two files in the same directory print their shared path prefix once:

Clones:
src/ foo.ts:10-25 ~ bar.ts:42-57

Open the two ranges together. Compare their callers, check whether the differences encode different behavior, look at the tests around those differences, and decide whether both implementations need to exist before extracting a shared helper.

jscpd gives you source ranges to investigate. Different implementations of the same behavior can still need a manual comparison. This command's token matches don't establish behavioral equivalence.

If the report points to copied fixtures or generated clients, document an exclusion and rerun it.

For application logic, the guide to duplicate functions covers the reuse decision.

Check unused files and exports with Knip

Knip finds unused files, dependencies and exports in JavaScript and TypeScript projects.

Its installation instructions require Node.js 20.19.0 or later.

Knip uses typescript and @types/node as peer dependencies, which a TypeScript project usually has already.

pnpm add -D knip adds only Knip so the audit doesn't change the project's TypeScript version:

pnpm add -D knip
pnpm exec knip --reporter json

The JSON reporter documentation includes these fields in an issue. This is a trimmed field excerpt, not a complete JSON document:

"file": "src/math.ts",
"exports": [{ "name": "factorial", "line": 12, "col": 14, "pos": 256 }],

Open the export before deleting it. Check the scanner's entry files, check generated inputs, look for dynamic imports, and confirm the file isn't part of a package's public interface before deciding that the report describes something you can remove.

Knip's issue-handling guide covers unresolved dynamic import specifiers. Knip also excludes unused exports of entry files by default.

For a deliberate review of those exports, its Knip CLI reference documents:

pnpm exec knip --include-entry-exports

Knip reports from the project model you've given it, so keep a note of missing plugins, unresolved imports, generated files and entry patterns alongside the findings. The follow-up on finding unused files and exports goes further into that triage.

Ask whether a production path reaches the function

I build Pharaoh. It maps TypeScript, JavaScript, Python, C and C++ repositories into structural graphs and exposes them through MCP.

The tool overview describes the available queries.

Ask your connected agent to run get_unused_code for the module you're investigating. Here is trimmed captured output from Pharaoh-so/cal.diy, revision 54343aa6, captured on October 26, 2026:

# Unused code in cal.diy / features/auth: 3 dead, 4 possibly unused, 1 used only by tests

## Dead (3): unreached, named in no other file, file not imported
- ensureSession — packages/features/auth/lib/ensureSession.ts (5 LOC)

## Possibly unused (4): unreached, but named elsewhere or imported. Check before deleting.

In the Dead category, no entry point reaches the function, no other file names it, and no production file imports its file. Every signal must agree. Possibly unused retains evidence of a reference or an import, so inspect the reported files before making a deletion.

You can also ask about a particular function. The same capture includes this complete result from get_unused_code with functions: ["checkAdminOrOwner"]:

# Reachability in cal.diy

- checkAdminOrOwner (packages/features/auth/lib/checkAdminOrOwner.ts): reachable: getAtomEventTypeApp → getEventTypesAppIntegration → checkAdminOrOwner

That gives the review a specific path to inspect. Follow it through the handler, open its callers, check authorization at the relevant boundary, and exercise the production entry point rather than relying on a unit test that calls the helper directly.

Check dependency cycles with dependency-cruiser

dependency-cruiser validates dependencies against rules. Its README documents installation and initialization.

The Node requirements include Node.js 24.

Keep an existing project configuration. Run --init only when you need to create one, then review its rules before scanning:

pnpm add -D dependency-cruiser
npx dependency-cruiser --init
npx dependency-cruiser src --output-type json

The generated rules include a circular-dependency check. Keep that rule enabled, verify that your source paths resolve, check excluded directories, and include every application package involved in the suspected cycle when you run the report.

Before you trust an empty result, run npx dependency-cruiser --info and confirm it found a supported typescript.

typescript must fall within dependency-cruiser 18.7.0's supported range, >=2.0.0 <7.0.0. Then check that summary.totalCruised counts your modules.

dependency-cruiser skips the .ts files without a supported compiler and still returns an empty violations array.

The dependency-cruiser CLI reference documents the JSON output option.

The vendor's JSON format example uses commander.js.

These trimmed lines from its summary show a scan with no violations. Use your own report to assess your repository:

    "violations": [],
    "error": 0,
    "warn": 0,
    "info": 0,
    "totalCruised": 6,

Inspect summary.violations in your own result. Use each violation's rule and source/target paths to find the imports involved; a clean result applies to the graph scanned under your configuration.

For each cycle, read the imports that complete the loop. Ask which module should own the shared behavior, identify the edge you intend to remove, make that change separately, and rerun the same rule against the resulting dependency graph.

Don't move a function into a shared directory solely to silence the report. Write down the ownership decision so the next generated helper has somewhere sensible to go.

Run Semgrep separately for security findings

Semgrep's scan command runs the rules you select against your source.

pipx installs Semgrep as documented in the Community Edition quickstart.

The Semgrep CLI reference documents saving JSON alongside terminal output:

pipx install semgrep
semgrep scan --config auto --json-output audit-semgrep.json .

The auto configuration fetches rules for your project from the Semgrep Registry, and the CLI reference notes that it logs in to the Registry with your project URL.

Registry rules enable metrics. Review both before you scan a private repository, or point --config at a local ruleset.

A trimmed excerpt from the vendor's Python quickstart example, which uses a command-injection ruleset on its sample app.py, rather than the auto scan above:

app.py
❯❯❱ python.lang.security.audit.dangerous-system-call-audit.dangerous-system-call-audit
5┆ os.system("ls " + user_input)

Take each finding back to the source. Trace the input, inspect the authorization decision, check what the selected rule actually detects, and add a regression test for a confirmed bug before applying an agent's proposed fix.

Keep the ruleset and scanner version with the report, record any files the scan skipped, investigate errors, and leave the remaining uncertainty in the audit record. A quiet scan covers the rules and files that ran; it doesn't certify the application, and a reachability graph doesn't make that security decision for you.

Compare architecture with the baseline

Ask the connected agent for get_codebase_map and save the result with the audit's reports. Pharaoh answers for the default branch as it was last mapped, not for an older commit, so the baseline to compare against is the map you saved at your previous audit. If the commit you're auditing isn't the default branch's latest push, note that the Pharaoh results describe a different revision. Pharaoh returns module sizes and dependencies, flags two-way module dependencies, lists the files changed most in 90 days, and includes endpoints with their handler files.

Keep the revision distinction straight. Pharaoh's graph covers the default branch; local edits and other branches need local checks.

A live repository re-maps after a push to its default branch, and pharaoh_account can request a refresh.

For a closer look at a module, ask for get_module_context. Captured on October 26, 2026, this trimmed result is from revision 54343aa6 of Pharaoh-so/cal.diy:

# Module: features/auth

## Structure
43 files, 5,342 LOC, 99 functions

## Dependencies

### Imports from:
- lib (weight: 34)
- lib/auth (weight: 2)
- lib/server (weight: 6)

Compare the module's responsibilities with your intended design, inspect new dependencies, open the endpoint handlers, and investigate growth that doesn't correspond to an intended feature. One snapshot gives you a starting point; drift needs a comparison and a decision about the architecture you want.

For how other servers index and refresh this kind of map, see code graph MCP servers.

If your agent's answers don't match what the map shows, see checking Claude Code's repository context.

Turn the reports into a repair list

For each confirmed finding, record the file and line, the evidence, the intended change, and the check that will show it worked. Assign an owner. Keep uncertain findings open with the missing evidence written down.

Review the blast radius of the proposed change before a deletion or signature change, then make a small repair, rerun the affected checks, inspect the resulting diff, and exercise the feature from its production entry point. Keep the original reports so you can explain what changed.

Pharaoh's Pro PR Guard is advisory by default, with checks for new dead code, breaking signature changes, exact function duplicates and blast radius. Its PR Guard documentation covers the workflow; by default, findings produce a neutral check and don't block merging.

Connect Pharaoh.

Codebase audit for AI-assisted development FAQ

What should you record before starting a codebase audit?

Record the commit ID, tool versions, configuration, and excluded directories. Keep the source tree unchanged during the scan and run the same configuration against the baseline and the revision under review.

Does a scanner finding prove that code can be deleted?

No. Open the reported code, check entry files, generated inputs, dynamic imports, callers, and public interfaces. A report describes the project model and scan configuration, so confirm the production path before deleting anything.

What tools can help audit a TypeScript codebase?

jscpd checks matching token sequences, Knip reports unused files, dependencies, and exports, dependency-cruiser checks dependency rules such as cycles, and Semgrep runs selected security rules against source.

How should security findings be handled?

Trace each finding back to the source, inspect the input and authorization decision, verify what the selected rule detects, and add a regression test for a confirmed bug before applying a proposed fix. Record skipped files, scanner versions, rulesets, and remaining uncertainty.

What should the final repair list contain?

For each confirmed finding, record the file and line, evidence, intended change, and verification check. Assign an owner, keep uncertain findings open with missing evidence documented, make small repairs, rerun affected checks, inspect the diff, and exercise the production entry point.

Article Record

ReferencesPrimary sources used for this article
  1. Does AI-Assisted Coding Deliver? A Difference-in-Differences Study of Cursor’s Impact on Software Projects
  2. Introduction to Semgrep
  3. CLI reference
  4. Get started with Semgrep Community Edition
  5. Getting Started
  6. Resolve reported issues
  7. Reporters & Preprocessors
  8. Dependency cruiser
  9. dependency-cruiser/doc/output-format.md at main · sverweij/dependency-cruiser
  10. jscpd/skills/jscpd/SKILL.md at master · kucherenko/jscpd
  11. Use of hard-coded password
  12. Coding on Copilot: 2023 Data Suggests Downward Pressure on Code Quality
Update HistoryMeaningful revisions to this article
Added an FAQ to Support AI-Assisted Development Audits
  • Added a five-question FAQ section to help readers find answers about codebase audits for AI-assisted development more easily.
Initial publication.

More in AI Code Quality

← Back to blog