BXUP Track progress & test yourself — free
Software Engineering · Module · ~15 min read
SOFTWARE ENGINEERING · MODULE

Codebase Context Engineering

This module installs the habit that turns a repository into context you can trust: interrogating unfamiliar code with a read-only agent under a citation rule, and shaping the repo itself — instruction files, feedback loops, grounded docs — so every future prompt from every teammate starts from a better position.

  1. THIS WEEK

    Sana has owned orders-api for two years. Now she inherits billing-service — forty thousand lines she has never read, and a change due Friday.

  2. THE INSTINCT

    Her old move is to open the files and start reading. Instead she points a read-only agent at the repo and asks it to map the request path.

  3. +90 SECONDS

    A fluent tour: entry points, the main flow, and the line "proration logic lives in billing/proration.py, in apply_discount, called from the invoice worker." It reads authoritatively.

  4. THE SPOT-CHECK

    She opens billing/proration.py. There is no apply_discount. The discount math is inlined in build_invoice, three files away.

  5. THE DAMAGE

    The agent had narrated a plausible architecture instead of the real one, and every downstream claim resting on that phantom function was now suspect.

  6. THE RESTART

    She runs the pass again with a tighter scope and one hard rule: a file path and a symbol per claim, or it does not count.

  7. THE VERDICT

    She opens two cited files at random; both hold up. The agent had, in fact, mapped the real skeleton in a fraction of the time reading would have taken.

The value was only bankable once she spot-checked it.

The Common Core taught context engineering as a prompting discipline: what you put in the window determines what comes out. This module applies it at the scale where backend engineers actually live — a repository.

Two facts set the agenda. First, "explaining unfamiliar code" is a top-rated AI strength in the 2025 Stack Overflow survey, and understanding code before changing it is the daily reality of every services engineer who inherits a new client codebase every 6–18 months. Second, coding agents now read your repo as part of their prompt: a CLAUDE.md, an AGENTS.md, a copilot-instructions.md, your linter config, and your test suite are all context that every agent run consumes — which means they are all surfaces you can engineer.

The engineers getting compounding returns from AI are not writing better one-off prompts; they are shaping repositories so that every prompt, from every teammate, starts from a better position. That is codebase context engineering: exploration (getting accurate context out of a repo), instruction files (putting durable context in), repo ergonomics (making the environment itself a feedback loop), grounded onboarding docs, and the hazards — context rot and training cutoffs — that silently corrupt all of it.

Key Takeaway:

A coding agent's effective prompt is your instruction files + your repo's structure + whatever it can discover with its tools. You control all three. Engineering them once improves every AI interaction anyone has with that repo afterward — it is the highest-leverage AI work a senior engineer does.

What You'll Learn

  • A read-only exploration protocolbuilding accurate mental models of unfamiliar code with an agent, under a citation rule
  • CLAUDE.md / AGENTS.md / copilot-instructions.mdauthoring instruction files that measurably change agent output
  • What makes a repo agent-friendlyfast deterministic tests, linters and type-checkers as feedback loops
  • Onboarding docs grounded in the actual repohow to generate them, and how to keep them from lying
  • Context rot and training-cutoff hazardsthe two silent corrupters, and the habits that defend against each

Read-Only Agentic Exploration

The single safest, highest-value agentic workflow is also the least practiced: point an agent at an unfamiliar codebase with no write access and interrogate it. Nothing can break. Hallucinations are cheap to catch because the ground truth is sitting in your editor. And the payoff lands exactly where the job hurts: ramping onto a legacy service before a change, inheriting a client codebase, or answering "what would break if we touched this?"

The tooling is interchangeable — the protocol is not. In Claude Code, plan mode (read-only by design) or a plain session where you simply don't approve edits; in Copilot, Chat with the workspace/agent context in ask mode; in Cursor, chat with codebase context enabled. On a $0 path, Copilot Free's chat allowance handles exploration sessions, and Gemini Code Assist or Continue.dev (open-source, bring-your-own-key) are workable alternates.

The exploration protocol

Unstructured "explain this codebase" prompts produce plausible tour-guide prose. Structured interrogation produces a mental model. Run these passes in order against a service you need to understand:

Pass 1 — Skeleton (breadth):
"Map this repo: entry points (servers, CLIs, cron/queue consumers),
the request path from entry to persistence for the main flow,
external dependencies (DBs, queues, third-party APIs), and where
configuration comes from. Cite file paths for every claim."

Pass 2 — Load-bearing walls (depth):
"Trace what happens when <core operation, e.g. 'an order is placed'>
end to end. List every side effect: writes, events emitted, caches
touched, emails/webhooks fired. Cite the exact functions."

Pass 3 — Danger list (judgment):
"What in this codebase would you least want to modify without tests?
Look for: shared mutable state, implicit ordering dependencies,
duplicated business logic, catch-and-ignore error handling,
concurrency. Rank by risk and cite evidence."

Two rules make this trustworthy:

  • Demand citations for every claima file path and symbol name per assertion — because an exploration answer without citations is unverifiable prose, and the Common Core taught you what to do with unverifiable prose.
  • Spot-check one claim per pass by opening the cited file. If the citation doesn't support the claim, you've caught the model narrating from pattern-matching rather than reading — restart the pass with a narrower scope.

Verifying the mental model

Comprehension you haven't tested is comprehension you don't have. Close the loop by inverting the roles: after your passes, have the agent quiz you ("ask me five questions about this codebase that I can only answer correctly if I understood the request path and the danger list; then grade my answers against the code"). Wrong answers send you back into the repo — which is the point. The output artifact of an exploration session is a short architecture brief: skeleton, core-flow trace, danger list, open questions. Written down, it becomes reviewable by a teammate who knows the system — the cheapest possible verification — and it becomes the seed for the onboarding docs later in this module.

Pro Tip

Exploration sessions are where long context earns its keep, but don't dump the repo into the window and hope. Agentic exploration — the agent greps, opens files, follows imports — consistently beats bulk-pasting source, because the agent retrieves what the question needs instead of drowning in what it doesn't.

Warning

The failure mode of AI-assisted comprehension is fluent wrongness about code the model skimmed. Symptoms: architecture claims without file citations, descriptions of "typical" patterns your repo doesn't actually use, and confident summaries of files the agent never opened — Sana's phantom apply_discount was all three at once. The citation rule exists because these failures are invisible without it.

Pro Tip

Your first 15 minutes in any unfamiliar repo: run one exploration pass that demands a file path and a symbol per claim, then open two cited files at random — if either citation doesn't support its claim, you've caught the agent narrating instead of reading, and every later answer stays suspect until you tighten the scope.

Instruction Files That Actually Change Behavior

Every serious coding tool now reads a persistent instruction file from the repo: Claude Code reads CLAUDE.md, the cross-tool AGENTS.md convention is honored by a growing set of agents, GitHub Copilot reads .github/copilot-instructions.md (plus scoped *.instructions.md files, which as of GitHub's July 2026 changelog also condition Copilot code review), and Cursor has its rules directory. Same idea everywhere: a prompt fragment prepended to every session, versioned in git, shared by the whole team. This is the highest-leverage file-to-effort ratio in your repo — and most teams write it badly.

What goes in, what stays out

The file is paid for out of the context window on every run, so every line must earn rent. Three categories earn it:

  1. Things the agent gets wrong without it. Your non-obvious conventions: "we use our internal Result type, never raise across service boundaries," "all timestamps UTC, stored as timestamptz," "never edit src/generated/ — regenerate via make codegen."
  2. Commands and feedback loops. How to run tests, lint, type-check, and build — exactly, with flags: agents that know pytest -x -q tests/unit will run it; agents that don't will guess or skip verification.
  3. Boundaries. What not to touch (migrations without asking, vendored code, secrets files), and what "done" means (tests pass, linter clean, no new type errors).

What stays out: anything the agent can discover faster than you can maintain it (full directory listings — they rot), anything generic ("write clean code" — burns tokens, changes nothing), and long style essays that a formatter already enforces mechanically.

A real example

A production-shaped CLAUDE.md / AGENTS.md for a Python service — note that nearly every line is a correction of observed agent behavior, not aspiration:

# payments-service — agent instructions

## Commands
- Test: `pytest -x -q` (unit only; integration needs Docker: `make test-int`)
- Lint + types: `ruff check . && mypy src/` — must be clean before done
- Never run `make deploy` or anything under `scripts/prod/`

## Conventions
- Errors: return `Result[T, PaymentError]` from service functions;
  raising across module boundaries is a review-blocker
- Money is `Decimal` end to end. Introducing `float` for currency
  is always wrong here.
- DB access only through `src/payments/repo/`. No ORM calls in handlers.
- New endpoints: copy the pattern in `src/payments/api/refunds.py`

## Boundaries
- Do not modify files in `migrations/` — propose migration SQL in the
  plan instead and wait for approval
- `src/generated/` is codegen output; change the proto, run `make codegen`
- Tests that hit the network are forbidden; use fixtures in `tests/fakes/`

The same content works almost verbatim as .github/copilot-instructions.md; for Copilot's scoped variant, split path-specific rules into e.g. .github/instructions/api.instructions.md with an applyTo glob so the API conventions only load when API files are in play — scoped loading is the built-in defense against the context-rot problem covered below.

Test that it works — measurably

An instruction file is a claim ("this changes agent behavior") and claims get tested. The method is a before/after trial: pick three tasks the agent historically fumbles in your repo (e.g., "add an endpoint" — does it copy your conventions or invent its own?). Run each task in a fresh session without the file, save the diffs. Add the file, fresh sessions, same tasks, save the diffs. Count convention violations in each. If a rule is still being violated, the rule is too vague — "follow our error-handling style" fails where "return Result, never raise across boundaries, see refunds.py" succeeds. If removing a rule changes nothing, delete it: it was paying rent for nothing. Treat the file like code — reviewed in PRs, updated when a convention changes, pruned quarterly.

Remember:

Instruction files are corrections, not documentation. The authoring loop is: watch an agent make a mistake in your repo → write the one line that would have prevented it → verify the line prevents it → commit. Files written that way stay short, true, and effective; files written as aspirational essays are ignored by agents and humans alike.

Making the Repo Agent-Friendly

Instruction files tell agents what to do; the repo's ergonomics determine whether they can verify they did it. An agent's edit-run-fix loop is only as good as the feedback signals the repo emits — which is why the practitioner guidance (Simon Willison's coding-agent setup notes, October 2025) and DORA 2025's finding that quality internal platforms amplify AI gains converge on the same checklist. The properties that matter:

  • Fast, deterministic tests. An agent that can run a relevant test slice in seconds will run it after every change; a 20-minute flaky suite means the agent (like your teammates) skips verification. Split unit from integration, kill flakes, and document the fast path in the instruction file.
  • Linters and type-checkers as guardrails.ruff/ESLint and mypy/tsc convert whole classes of AI mistakes — hallucinated attributes, wrong argument types, unused imports — into immediate, machine-readable errors the agent fixes itself before you ever see the diff. Typed interfaces are the single cheapest upgrade: every type annotation is a constraint the agent must satisfy rather than a convention it must remember.
  • One-command everything.make test, make lint, make run — or the package.json/justfile equivalent. Every step that requires tribal knowledge ("oh, you need to source that env file first") is a step where the agent stalls or guesses.
  • Hermetic setup. If a fresh clone plus one documented command yields a working dev environment (devcontainer, Docker Compose, lockfiles), agents — including cloud agents like Copilot coding agent working from an issue — can verify their own work. If setup is a wiki page of manual steps, they can't.
  • Seed docs. A README that states what the service is, the architecture brief from your exploration sessions, ADRs for the decisions that explain the weird parts. Agents read these first and ground their plans in them.

The compounding observation: every one of these makes human onboarding better too. Agent-friendliness is not a new discipline — it is engineering hygiene with a second constituency that, unlike your teammates, will actually run the linter every single time. Budget note for restricted or $0 environments: everything in this list is free and open source; agent-friendliness requires no AI spend at all.

Note

When an agent repeatedly fails in your repo, the instinct is to blame the model; the professional move is to ask what signal the repo failed to emit. No type on the function it misused? No test covering the behavior it broke? No instruction line naming the convention it violated? Fix the repo and the failure class disappears for every future run — and usually for every future human, too.

Onboarding Docs Grounded in the Repo

For services companies especially — new client codebase every engagement, ramp-up time directly billable — the exploration protocol has a productizable output: a codebase tour for newcomers. The prompt shape, run by an agent with repo access:

Produce an onboarding tour for a senior engineer new to this repo:
1. Read this first: the 5 files that explain the system, in order,
   with one sentence on why each matters
2. The life of a request: our main flow, file-by-file
3. How to run it: exact setup and verification commands (verify these
   yourself by checking they exist in the repo's config/scripts)
4. Gotchas: non-obvious conventions, historical wrinkles, danger areas
   (use evidence: TODO/HACK comments, suspicious complexity, files with
   heavy churn)
Cite file paths for every claim. If unsure, say UNSURE — do not guess.

Two disciplines separate a useful tour from a liability:

  • Human curation before publicationthe reviewer is someone who knows the system, and they're auditing the claims, not the prose; this is a T2/T3 document by the Common Core's tiers, because newcomers will trust it blindly.
  • Drift protectionan onboarding doc that lies is worse than none, so make regeneration cheap (keep the prompt in the repo next to the doc) and add a staleness tripwire: regenerate on a cadence or when key directories change, then diff the fresh output against the published version; material drift means the doc gets re-reviewed.

The same generate-verify-diff pattern extends to architecture diagrams (Mermaid from actual code) and runbooks later in this track.

Context Rot and Training-Cutoff Hazards

Two failure modes corrupt everything above, and both are invisible until you know to look.

Context rot

Long agent sessions degrade. As the window fills — with earlier exploration, failed attempts, tool output, and dead ends — models attend less reliably to any given fact in it; your instruction file's rules and the actual task competes with pages of stale history. Symptoms: the agent re-reads files it already read, contradicts its own earlier findings, "forgets" a constraint from the instruction file it honored an hour ago, or drifts back to an approach you explicitly rejected. Defenses:

  • Fresh sessions per task. Sessions are disposable; durable context belongs in files (instruction file, architecture brief, the plan). End of task = end of session.
  • Compact deliberately. When a long session is genuinely necessary, have the agent write its current findings/plan to a file, then start clean and load that file — an explicit checkpoint beats a bloated window.
  • Keep instruction files lean and scoped. Every stale rule and rotted directory listing in CLAUDE.md/copilot-instructions.md is context rot you committed to git — it degrades every session, forever, until pruned. Scoped instruction files (*.instructions.md with path globs, per-directory AGENTS.md/CLAUDE.md in larger monorepos) load rules only where they apply.

Training cutoffs and fast-moving libraries

Every model's knowledge of your dependencies froze at its training cutoff — and it will not volunteer this. Ask for code against a fast-moving library (LLM SDKs are themselves the worst offenders, alongside frontend frameworks and anything pre-1.0) and you'll get fluent code for a version that may be two majors stale: renamed parameters, deprecated auth patterns, removed methods — all rendered with total confidence. This is the Common Core's stale knowledge failure mode with a compiler-shaped blast radius. Defenses:

  • Pin and disclose versions in context. Your lockfile is ground truth; your instruction file should name load-bearing versions ("FastAPI 0.115, Pydantic v2 — do not use Pydantic v1 validator patterns").
  • Feed current docs instead of trusting recall. Paste the relevant doc page, point tools with retrieval at the official docs, or use docs-oriented MCP servers where sanctioned. Grounding beats memory for anything that ships versions faster than models ship cutoffs.
  • Let the toolchain be the arbiter. This is the agent-friendly repo paying off again: mypy/tsc and a fast test run catch stale-API code at generation time, converting a silent hazard into a red squiggle the agent fixes itself.
Exam Tip

Context rot and training cutoff produce the same surface symptom — an agent confidently doing the wrong thing — with opposite fixes. Rot: the context window is degraded → fresh session, externalized state. Cutoff: the model's knowledge is stale → supply current docs and versions. Diagnose before treating: restarting a session cannot fix a model that has never seen Pydantic v2.

Check yourself: An agent hands you a confident architecture summary of a service you don't know, with no file paths cited. Why is that summary a liability rather than a head start, and what single rule converts it into something you can trust?

▸Show answeroptional deep dive — click to expand

Without citations the summary is unverifiable prose, and AI-assisted comprehension's signature failure is fluent wrongness: architecture claims about code the model only skimmed, "typical" patterns your repo doesn't use, confident summaries of files never opened. You cannot tell a correct summary from a fabricated one by reading it, because both read authoritatively — that is the trap. The rule that converts it: demand a file path and symbol per claim, then spot-check a sample by opening the cited file. A citation that doesn't support its claim exposes the narration and tells you to restart with a narrower scope; a sample that holds earns the summary a head start's worth of trust. This is exactly Sana's apply_discount catch — the phantom function that sent her back to tighten the pass.

Exercises

  1. Exploration sprint. Pick a repo you genuinely don't know (an OSS service or an unfamiliar internal one). Run the three-pass protocol with citation demands; spot-check two citations per pass. Produce the one-page architecture brief, then have the agent quiz you with five questions and grade your answers. Deliverable: the brief plus your quiz score and one claim you caught the agent getting wrong.
  2. Instruction file, test-driven. Author a CLAUDE.md/AGENTS.md or copilot-instructions.md for a repo you own, using only the corrections-not-documentation method: at least five rules, each traceable to a real agent mistake. Run the before/after trial on three tasks and record convention violations in each condition. Delete any rule the trial shows to be inert.
  3. Agent-friendliness audit. Score your main repo against the five ergonomic properties (fast deterministic tests, lint/type guardrails, one-command tasks, hermetic setup, seed docs) as green/yellow/red with evidence. Implement the single cheapest red-to-green fix and demonstrate an agent using the new feedback signal (e.g., agent runs the fast test slice and self-corrects).
  4. Grounded tour with drift check. Generate the onboarding tour for a repo you know well; audit every claim and record what the model got wrong or marked UNSURE. Commit the prompt beside the doc. Then simulate drift: make a small architectural change, regenerate, and diff — confirm the tripwire catches it.
  5. Cutoff hunt. Take your most rapidly-moving dependency. In a fresh session with no repo context, ask the model to write a non-trivial snippet against it, then diff the result against the current official docs. Catalogue every stale API it used, then write the instruction-file lines and doc-grounding step that would have prevented each.

Your playbooks & labs

This module's companion playbooks cover legacy-codebase comprehension and architecture-brief production, instruction-file authoring and maintenance for the major agent tools, the agent-friendly repo checklist, and onboarding-tour generation with drift protection. Do this at work this week → pb-01 Map an Unfamiliar Codebase Before You Change It (Sana's read-only exploration protocol, productized), pb-04 Instruction Files That Make Generated Code Match Your Conventions (the corrections-not-documentation CLAUDE.md/AGENTS.md method), and pb-09 Make Your Codebase Agent-Friendly (the five ergonomic properties as a scored audit). Its labs include the legacy comprehension sprint on a provided unfamiliar service (brief + danger list, quiz-graded), the instruction-file authoring lab with measurable before/after behavior change, and the repo agent-readiness lab.

Further Reading

This is the free reading view. Open BXUP to track your progress, run the hands-on labs, and prove it with original, exam-pattern assessments.
© Anvi Innovate · BXUP — Be Exponentially Upskilled · BXUP is an independent learning platform, not affiliated with or endorsed by AWS, Microsoft, Google, NVIDIA, IBM, Salesforce, Databricks, Oracle or Anthropic. All trademarks belong to their respective owners. · bxup.in