Investigate Codebase
Objective
Build the minimum sufficient, falsifiable model needed to answer the user's
question. Prefer a thin vertical slice over an exhaustive repository summary.
Optimize for human understanding and verification, not the amount of code read.
Operating contract
- Read all applicable repository and agent instruction files before inspecting
implementation, including nested per-directory files under whichever convention
the repository uses (for example
AGENTS.md, CLAUDE.md, or CONTRIBUTING.md).
- Default to read-only investigation. Do not edit files, install dependencies,
switch branches, reset state, or alter durable resources unless the user
requested that action.
- Preserve unrelated and dirty worktree changes. Separate committed changes from
local modifications before drawing conclusions.
- Record the repository revision. For change analysis, resolve and record the
exact base and head revisions rather than silently guessing.
- Treat README files, architecture documents, comments, and PR descriptions as
intent evidence that must be reconciled with implementation and behavior.
- Start narrow. Expand scope only when a dependency, contract, contradiction, or
unresolved hypothesis requires it.
- Prefer version-control history, manifests, exact symbol references, fast
repository-wide text search (
rg, grep, or the equivalent available in the
environment), language-native tooling, tests, and runtime observation. Use
semantic or embedding search only to locate candidate code, never as behavioral
proof.
- Use whatever file-reading, search, and command-execution capabilities the host
environment provides. If a capability required by a check is unavailable, record
the check as
UNKNOWN with the reason rather than substituting a weaker claim.
- Ask before checks that may use production credentials, access external systems,
mutate durable state, incur meaningful cost, or take substantial time.
- Never equate passing tests, high coverage, or clean static analysis with proof
of complete correctness.
Select the investigation
Choose one primary mode and read its guide completely:
ORIENT: Map a large or unfamiliar repository. Read
orient-large-codebase.md.
TRACE: Explain a concrete runtime, control, or data flow. Read
trace-runtime-flow.md.
IMPACT: Analyze a PR, branch, commit, or diff. Read
analyze-pr-impact.md.
VERIFY: Assess generated or changed code. Read
verify-generated-code.md.
Combine modes only when necessary, such as IMPACT + VERIFY. Read only the
guides required for the selected modes.
Choose a depth:
scan: Find landmarks and a likely path; keep uncertainty explicit.
working: Produce an evidence-backed explanation and focused checks. Use by default.
audit: Broaden risk and verification coverage when explicitly requested or
warranted by the change's consequences.
State the selected mode, depth, scope, revision, assumptions, and important
environment limits in the result.
Investigation loop
1. Anchor the target
- Identify the repository root, applicable instructions, working-tree state,
revision, build system, and relevant runtime configuration.
- For a PR or branch, resolve the actual comparison base from PR metadata or the
merge base. Keep unrelated local changes out of the comparison.
- Exclude generated, vendored, fixture, snapshot, lockfile, and formatting noise
from semantic analysis unless one of them changes behavior or contracts.
2. Frame falsifiable questions
- Rewrite the request as observable questions: given which input and preconditions,
what output, state change, side effect, or failure should occur?
- Identify available correctness oracles: explicit requirements, acceptance
criteria, public contracts, schemas, stable prior behavior, domain invariants,
reference implementations, or production observations.
- If no independent oracle exists, state which consistency claims can be assessed
and which intent claims cannot be established.
3. Build the minimum map
- Locate only the relevant manifests, boundaries, entry points, public contracts,
configuration, persistence, external effects, and tests.
- Describe components by runtime or domain responsibility rather than by directory.
- Mark dynamic dispatch, dependency injection, reflection, generated code, plugins,
queues, and callbacks as unresolved edges until supported by evidence.
4. Trace a thin vertical slice
Follow the smallest path that can answer the question:
input boundary -> parsing/auth -> orchestration -> domain decisions -> state or external effect -> output/error/telemetry
Track control flow, data-shape changes, state ownership, feature flags, async
boundaries, retries, serialization, errors, and observable side effects when relevant.
5. Maintain an evidence ledger
For each material claim, record:
| Claim |
Evidence status |
Revision + path/symbol or command |
Result or limitation |
Use these labels:
SOURCE: Directly supported by implementation, configuration, schema, or diff.
RUNTIME: Observed through an executed command, test, log, trace, or reproducer.
INFERRED: Reasoned from evidence but not directly observed.
UNKNOWN: Missing code, environment, specification, oracle, or access.
CONTRADICTED: A counterexample or conflicting source disproves the claim.
Prefer revision + file + symbol; use line numbers as secondary anchors because
they drift. Include exact commands and outcomes for runtime evidence. Never label a
statically inferred call path as an observed runtime trace.
6. Challenge the model
- Turn the model into predictions that could be false.
- Search for contradictory call sites, tests, schemas, configuration, history,
and runtime behavior.
- Consider empty and boundary inputs, permissions, ordering, retries, idempotency,
concurrency, partial failure, compatibility, migration, rollback, and external
boundary behavior according to risk.
- Ask which unchanged behavior must remain invariant, not only what changed.
7. Verify economically
Choose the cheapest check capable of falsifying each important claim:
- Source, type, schema, and configuration inspection
- Typecheck, lint, compile, or static analysis
- Focused unit or component test
- Integration or end-to-end path
- Base/head differential, property, metamorphic, adversarial, or mutation check
- Runtime trace or production-like observation
Prefer focused checks tied to a hypothesis over an untargeted full suite. Distinguish
pre-existing failures, regressions, flaky outcomes, skipped checks, timeouts, and
environment mismatches. Do not hide checks that could not run.
8. Synthesize for human use
Answer the question before presenting supporting detail. Use progressive disclosure:
30 seconds: Direct answer, primary path, and largest uncertainty.
5 minutes: Minimal model, contracts or invariants, behavior delta, and risks.
Audit: Evidence ledger, commands, results, contradictions, and unknowns.
Limit the first map to roughly five to seven meaningful components and one to three
representative flows. Provide a five-to-twelve-file reading route in causal or
dependency order, with one reason per file. If the user is learning, finish with
two or three prediction questions that test whether the model transfers.
Report contract
Return the sections relevant to the selected mode:
- Direct answer
- Scope, mode, depth, revision, and base/head when applicable
- Minimal architecture or execution-flow model
- Contracts and invariants
- Evidence ledger
- Behavioral impact or correctness matrix
- Risks, contradictions, assumptions, and unknowns
- Recommended reading route
- Smallest next verification steps
- Prediction questions when learning is part of the goal
Use a diagram only when it materially clarifies at least three boundaries, branches,
or state transitions. Do not produce a full repository tree as a substitute for a model.
Phrase correctness conclusions conditionally, for example:
At revision X in environment Y, evidence supports behavior Z for scenarios A
and B. Scenario C and external dependency D remain unverified.
Do not output an uncalibrated correctness percentage.
Completion criteria
Stop when:
- the target question has a supported answer;
- the relevant entry, decisions, state ownership, side effects, and failure path
are accounted for to the requested depth;
- every material claim has evidence or is explicitly marked unknown;
- verification results and limitations are reproducible; and
- further exploration is unlikely to change the answer materially.
Do not continue exploring merely to appear exhaustive.
1---2name: investigate-codebase3description: Build an evidence-backed working model of an existing codebase. Use when you must orient in a large or unfamiliar repository; explain architecture or a subsystem; trace a request, runtime, control, or data flow for a concrete question or bug; analyze a branch, commit, diff, or pull request for behavioral impact and blast radius; or assess AI-generated or changed code against requirements, contracts, invariants, tests, and runtime evidence. Default to read-only investigation. Do not trigger only because a coding task touches an existing repository; use when understanding or verification is the requested outcome or a substantial prerequisite.4---56# Investigate Codebase78## Objective910Build the minimum sufficient, falsifiable model needed to answer the user's11question. Prefer a thin vertical slice over an exhaustive repository summary.12Optimize for human understanding and verification, not the amount of code read.1314## Operating contract1516- Read all applicable repository and agent instruction files before inspecting17 implementation, including nested per-directory files under whichever convention18 the repository uses (for example `AGENTS.md`, `CLAUDE.md`, or `CONTRIBUTING.md`).19- Default to read-only investigation. Do not edit files, install dependencies,20 switch branches, reset state, or alter durable resources unless the user21 requested that action.22- Preserve unrelated and dirty worktree changes. Separate committed changes from23 local modifications before drawing conclusions.24- Record the repository revision. For change analysis, resolve and record the25 exact base and head revisions rather than silently guessing.26- Treat README files, architecture documents, comments, and PR descriptions as27 intent evidence that must be reconciled with implementation and behavior.28- Start narrow. Expand scope only when a dependency, contract, contradiction, or29 unresolved hypothesis requires it.30- Prefer version-control history, manifests, exact symbol references, fast31 repository-wide text search (`rg`, `grep`, or the equivalent available in the32 environment), language-native tooling, tests, and runtime observation. Use33 semantic or embedding search only to locate candidate code, never as behavioral34 proof.35- Use whatever file-reading, search, and command-execution capabilities the host36 environment provides. If a capability required by a check is unavailable, record37 the check as `UNKNOWN` with the reason rather than substituting a weaker claim.38- Ask before checks that may use production credentials, access external systems,39 mutate durable state, incur meaningful cost, or take substantial time.40- Never equate passing tests, high coverage, or clean static analysis with proof41 of complete correctness.4243## Select the investigation4445Choose one primary mode and read its guide completely:4647- `ORIENT`: Map a large or unfamiliar repository. Read48 [orient-large-codebase.md](references/orient-large-codebase.md).49- `TRACE`: Explain a concrete runtime, control, or data flow. Read50 [trace-runtime-flow.md](references/trace-runtime-flow.md).51- `IMPACT`: Analyze a PR, branch, commit, or diff. Read52 [analyze-pr-impact.md](references/analyze-pr-impact.md).53- `VERIFY`: Assess generated or changed code. Read54 [verify-generated-code.md](references/verify-generated-code.md).5556Combine modes only when necessary, such as `IMPACT + VERIFY`. Read only the57guides required for the selected modes.5859Choose a depth:6061- `scan`: Find landmarks and a likely path; keep uncertainty explicit.62- `working`: Produce an evidence-backed explanation and focused checks. Use by default.63- `audit`: Broaden risk and verification coverage when explicitly requested or64 warranted by the change's consequences.6566State the selected mode, depth, scope, revision, assumptions, and important67environment limits in the result.6869## Investigation loop7071### 1. Anchor the target7273- Identify the repository root, applicable instructions, working-tree state,74 revision, build system, and relevant runtime configuration.75- For a PR or branch, resolve the actual comparison base from PR metadata or the76 merge base. Keep unrelated local changes out of the comparison.77- Exclude generated, vendored, fixture, snapshot, lockfile, and formatting noise78 from semantic analysis unless one of them changes behavior or contracts.7980### 2. Frame falsifiable questions8182- Rewrite the request as observable questions: given which input and preconditions,83 what output, state change, side effect, or failure should occur?84- Identify available correctness oracles: explicit requirements, acceptance85 criteria, public contracts, schemas, stable prior behavior, domain invariants,86 reference implementations, or production observations.87- If no independent oracle exists, state which consistency claims can be assessed88 and which intent claims cannot be established.8990### 3. Build the minimum map9192- Locate only the relevant manifests, boundaries, entry points, public contracts,93 configuration, persistence, external effects, and tests.94- Describe components by runtime or domain responsibility rather than by directory.95- Mark dynamic dispatch, dependency injection, reflection, generated code, plugins,96 queues, and callbacks as unresolved edges until supported by evidence.9798### 4. Trace a thin vertical slice99100Follow the smallest path that can answer the question:101102`input boundary -> parsing/auth -> orchestration -> domain decisions -> state or external effect -> output/error/telemetry`103104Track control flow, data-shape changes, state ownership, feature flags, async105boundaries, retries, serialization, errors, and observable side effects when relevant.106107### 5. Maintain an evidence ledger108109For each material claim, record:110111| Claim | Evidence status | Revision + path/symbol or command | Result or limitation |112|---|---|---|---|113114Use these labels:115116- `SOURCE`: Directly supported by implementation, configuration, schema, or diff.117- `RUNTIME`: Observed through an executed command, test, log, trace, or reproducer.118- `INFERRED`: Reasoned from evidence but not directly observed.119- `UNKNOWN`: Missing code, environment, specification, oracle, or access.120- `CONTRADICTED`: A counterexample or conflicting source disproves the claim.121122Prefer `revision + file + symbol`; use line numbers as secondary anchors because123they drift. Include exact commands and outcomes for runtime evidence. Never label a124statically inferred call path as an observed runtime trace.125126### 6. Challenge the model127128- Turn the model into predictions that could be false.129- Search for contradictory call sites, tests, schemas, configuration, history,130 and runtime behavior.131- Consider empty and boundary inputs, permissions, ordering, retries, idempotency,132 concurrency, partial failure, compatibility, migration, rollback, and external133 boundary behavior according to risk.134- Ask which unchanged behavior must remain invariant, not only what changed.135136### 7. Verify economically137138Choose the cheapest check capable of falsifying each important claim:1391401. Source, type, schema, and configuration inspection1412. Typecheck, lint, compile, or static analysis1423. Focused unit or component test1434. Integration or end-to-end path1445. Base/head differential, property, metamorphic, adversarial, or mutation check1456. Runtime trace or production-like observation146147Prefer focused checks tied to a hypothesis over an untargeted full suite. Distinguish148pre-existing failures, regressions, flaky outcomes, skipped checks, timeouts, and149environment mismatches. Do not hide checks that could not run.150151### 8. Synthesize for human use152153Answer the question before presenting supporting detail. Use progressive disclosure:154155- `30 seconds`: Direct answer, primary path, and largest uncertainty.156- `5 minutes`: Minimal model, contracts or invariants, behavior delta, and risks.157- `Audit`: Evidence ledger, commands, results, contradictions, and unknowns.158159Limit the first map to roughly five to seven meaningful components and one to three160representative flows. Provide a five-to-twelve-file reading route in causal or161dependency order, with one reason per file. If the user is learning, finish with162two or three prediction questions that test whether the model transfers.163164## Report contract165166Return the sections relevant to the selected mode:1671681. Direct answer1692. Scope, mode, depth, revision, and base/head when applicable1703. Minimal architecture or execution-flow model1714. Contracts and invariants1725. Evidence ledger1736. Behavioral impact or correctness matrix1747. Risks, contradictions, assumptions, and unknowns1758. Recommended reading route1769. Smallest next verification steps17710. Prediction questions when learning is part of the goal178179Use a diagram only when it materially clarifies at least three boundaries, branches,180or state transitions. Do not produce a full repository tree as a substitute for a model.181182Phrase correctness conclusions conditionally, for example:183184> At revision X in environment Y, evidence supports behavior Z for scenarios A185> and B. Scenario C and external dependency D remain unverified.186187Do not output an uncalibrated correctness percentage.188189## Completion criteria190191Stop when:192193- the target question has a supported answer;194- the relevant entry, decisions, state ownership, side effects, and failure path195 are accounted for to the requested depth;196- every material claim has evidence or is explicitly marked unknown;197- verification results and limitations are reproducible; and198- further exploration is unlikely to change the answer materially.199200Do not continue exploring merely to appear exhaustive.