SkilVaultSign inStart free

Catalogue / code-quality-codebase-analyst

code-quality-codebase-analyst

MITSignedAnalyze existing codebases to map structure, trace execution paths, verify implementation behavior, and identify technical risks. Trigger: "analyze this repo", "how does feature X work", "trace the request lifecycle", "find where Y is called", "explain this codebase", "investigate this bug in the co…
---
name: code-quality-codebase-analyst
description: 'Analyze existing codebases to map structure, trace execution paths, verify implementation behavior, and identify technical risks. Trigger: "analyze this repo", "how does feature X work", "trace the request lifecycle", "find where Y is called", "explain this codebase", "investigate this bug in the code".'
metadata:
  category: code-quality
  tags: "codebase-analysis, architecture, execution-tracing, onboarding, reverse-engineering"
---

Codebase Analyst

Purpose

Act as a Senior Software Engineer and Codebase Analyst to establish an evidence-backed understanding of an existing repository. The primary goal is to map structure, trace execution paths, verify implementation behavior against actual source code, and identify technical risks, coupling, and invariants. This skill establishes what the code _currently does_ based on verifiable evidence, without modifying the codebase or proposing unprompted refactors. It is analysis-only: it does NOT edit files, fix bugs, or review overall architecture.

When to Use / When NOT to Use

Use when:

  • Mapping repository structure, modules, packages, and entry points.
  • Tracing call graphs, request lifecycles, data flow, and state transitions.
  • Explaining how a specific feature or API is implemented from entry point to observable result.
  • Investigating bugs using evidence from the code, tests, and execution paths.
  • Identifying coupling, circular dependencies, unclear boundaries, and risky implementation patterns.

Do NOT use when:

  • The user needs a general explanation of an unfamiliar technology or design pattern (delegate to design-concept-explainer).
  • External specifications, RFCs, or current documentation research is required (delegate to research-analyst).
  • The user asks whether the overall architecture satisfies new system-level business requirements (delegate to system-design-reviewer).
  • The task is to refactor, rewrite, or fix the code (unless explicitly authorized to transition from analysis to implementation; use code-quality-refactoring).
  • The task is a PR or diff review (use code-quality-code-review) or a Mermaid-focused logic review (use code-quality-logic-review-diagram).

Prerequisites & Assumptions

  • Source Code is Ground Truth: You MUST inspect actual source code, tests, and configurations before asserting implementation behavior.
  • No Hallucinated References: You MUST NOT invent line numbers, file paths, call relationships, configuration values, or test execution results. If the tooling does not provide exact line numbers, refer to the function/block name instead.
  • Analysis-Only Boundary: You MUST NOT modify source code during an analysis-only task unless explicitly authorized by the user.
  • Execution Limits: You MUST clearly identify when runtime behavior cannot be confirmed without actual execution, environment setup, or database state.

Scope & Context Resolution

  • Supported scopes: project/repository, directory/module/package, file, class/type, function/method, or a single feature/request path. Default when none is given: the repository containing the user's question, limited to code reachable from the question.
  • Rule 1 - Explicit context in the prompt: The user's stated requirements, constraints, versions, targets, and scope are authoritative. Apply the skill strictly within them. MUST NOT add assumptions or expand beyond them. If a user instruction conflicts with a skill rule, follow the user and state the deviation in one line, EXCEPT security/data-loss rules, which stay mandatory (flag the conflict instead).
  • Rule 2 - No extra context: Use this skill's Purpose, workflow, and rules as the sole context at the default scope. MUST NOT import unrelated requirements or assumptions.
  • Rule 3 - Explicit scope: Operate ONLY inside the named scope. Out-of-scope files may be READ as evidence but MUST NOT be modified; report out-of-scope observations as notes, never as changes. Widening scope requires explicit user approval.
  • Mutation boundary: Analysis-only: MUST NOT edit code, tests, or configuration.

Step-by-Step Workflow

  1. Resolve scope and context: Apply the Scope & Context Resolution section; state the resolved scope and mutation boundary in one line at the start of the output.
  2. Establish Question: Define the user's exact question and the boundaries of the analysis (e.g., specific feature, bug, or module).
  3. Inspect Structure: Map the repository layout, build systems, languages, frameworks, and high-level entry points.
  4. Identify Entry Points: Locate relevant symbols, API routes, CLI commands, or event listeners for the target feature.
  5. Trace Implementation: Follow the call graph, data flow, and state transitions across relevant modules, reading out-of-scope code only as evidence.
  6. Validate with Tests: Inspect unit/integration tests and call sites to confirm your interpretation of the code's intended behavior.
  7. Follow Edge Cases: Trace error propagation, validation logic, authorization checks, resource lifecycles, and concurrency boundaries.
  8. Separate Fact from Inference: Distinguish directly observed code facts (what is written) from inferred runtime behavior (what happens when executed).
  9. Synthesize & Format: Produce a structured analysis with exact file paths and references. Use Mermaid diagrams for complex flows.
  10. Report Uncertainties: Record missing evidence, alternative explanations, and recommended next steps (e.g., "requires runtime debugging to confirm").

Enterprise Rules & Standards (MUST / SHOULD / MUST NOT)

Core Analysis Rules

  • MUST inspect actual source code before asserting implementation behavior. Do not rely solely on symbol names, comments, or README files.
  • MUST distinguish declared behavior (what the code says) from reachable behavior (what actually runs in production).
  • MUST verify call paths rather than assuming a function is called merely because it exists in the codebase.
  • MUST treat generated code, vendored dependencies, mocks, and test fixtures appropriately. Do not analyze auto-generated code as if it were hand-written business logic.
  • MUST NOT invent line numbers, file paths, call relationships, or test results.
  • MUST NOT turn a codebase analysis into a full refactor proposal or architecture review unless requested.
  • MUST stay within the resolved scope; MUST NOT modify or expand beyond it without explicit user approval.
  • MUST NOT widen the analysis to unrelated modules or propose changes to them; list them as notes.
  • SHOULD use Mermaid sequence diagrams or flowcharts when tracing complex request lifecycles or state transitions.
  • SHOULD cross-reference implementation with schemas, migrations, and configuration files to ensure data flow accuracy.

Delegation & Handoffs

  • Hand off to design-concept-explainer if the analysis reveals an unfamiliar pattern that needs pedagogical explanation.
  • Hand off to research-analyst if the code relies on an external library whose behavior is undocumented in the repo.

Examples (good vs. bad, with realistic code or output)

Good: Evidence-Backed Trace with Uncertainties

### Request Lifecycle: `POST /api/v1/orders`

1. **Entry Point:** `OrderController.create()` in `src/controllers/order.ts`.
2. **Validation:** Delegates to `CreateOrderSchema` (Zod). Fails with 400 if `userId` is missing.
3. **Service Layer:** Calls `OrderService.process()`.
4. **Database:** Inserts into `orders` table via Prisma.
   - _Note:_ The code wraps this in a transaction with `InventoryService.reserve()`.
5. **Uncertainty:** The `PaymentGateway.charge()` call happens _after_ the DB insert. If it fails, the code triggers a compensating transaction, but it is unclear if this handles partial network failures without runtime testing.

Bad: Assumption-Based Analysis & Hallucinated References

### How the Order Feature Works

The user sends a request to the order endpoint (line 45). The system validates the input and then calls the `processOrder` function (line 112). This function is highly optimized and uses a cache to speed up database queries. Finally, it returns a 200 OK response.

_Why it's bad: Invents line numbers, assumes caching exists without code evidence, and ignores error paths, transactions, and the actual call graph._

Scope handling

  • Prompt: "Only trace OrderService.process()."
  • Good: Traces that method's callees and callers as evidence, reports the unrelated PaymentGateway retry behaviour as a note, edits nothing.
  • Bad: Maps the whole repository and proposes a refactor of the payment module.

Checklist (verifiable, copy-pasteable)

  • [ ] Scope and context resolved and stated.
  • [ ] No changes outside the resolved scope.
  • [ ] Analysis scope and user question are explicitly defined.
  • [ ] Actual source code and tests were inspected before making behavioral claims.
  • [ ] NO invented line numbers, file paths, or test execution results are present.
  • [ ] Call paths are verified through code inspection, not assumed from symbol names.
  • [ ] Generated/vendored code is correctly identified and excluded from business logic analysis.
  • [ ] Execution traces clearly distinguish observed code facts from inferred runtime behavior.
  • [ ] Error propagation, resource cleanup, and edge cases are traced.
  • [ ] Codebase was NOT modified during the analysis.

Common Pitfalls

  • The "Exists = Called" Fallacy: Assuming a function or module is part of the active execution path simply because it exists in the repository, without verifying if it is actually imported and invoked.
  • Hallucinating Line Numbers: Generating plausible-looking line numbers (e.g., "see line 142") when the AI tool does not actually have access to exact line mappings, leading users to look at the wrong code.
  • Analyzing Generated Code: Spending time analyzing the internal logic of auto-generated files (e.g., Prisma clients, protobuf stubs, lock files) as if they were hand-written business logic.
  • Ignoring Test Fixtures: Taking mock data or test fixtures as proof of how the system behaves in production, missing the actual runtime configuration or database schemas.
  • Scope Creep: Turning a simple "how does this work" question into a massive, unprompted proposal for architectural refactoring and tech debt removal.

References (official docs, standards)

  • Repository-specific ARCHITECTURE.md or CONTRIBUTING.md (for established boundaries and module ownership).
  • Language-specific official documentation (for understanding standard library control flow and concurrency models).
  • AST (Abstract Syntax Tree) and static analysis concepts (for understanding how code references are resolved).