Repository Intelligence: Teaching AI Agents to Understand Your Codebase Before They Change It

AI coding agents can write syntactically correct code and pass a test suite on the first try. They can also silently wreck your architecture in the process. The difference between an agent that helps you and one that creates expensive cleanup work comes down to a single capability: repository intelligence. Teams that invest in teaching their agents the why behind their codebase, not just the what, are getting dramatically better results from refactoring and debugging work. This article explains why that gap exists and how to close it.

The Shadow Tech Debt Problem

A term gaining traction in 2026 puts a useful name to something developers have been experiencing for months: "shadow tech debt." It describes code generated by AI agents that is architecturally blind. The code compiles, the tests pass, and the pull request looks clean, but it violates structural assumptions embedded in the project, introduces an inconsistency in how a pattern is applied, or discards a deliberate constraint that existed for a reason no agent ever found.

Shadow tech debt is uniquely insidious because it hides in plain sight. A human reviewer scanning a diff for obvious bugs will miss a decision that conflicts with a design principle documented only in a three-year-old commit message. The codebase degrades gradually, and the cost surfaces weeks later as confusion, regressions, and the increasingly difficult question: "Why is this written this way?"

The root cause is not that agents are bad at writing code. It is that they are operating without the context that makes code mean something in a specific project.

The Intent Gap: Why Reading Code Is Not Enough

When an agent is handed a function and asked to refactor it, it sees tokens. It understands what the code does computationally. What it cannot infer from the code alone is the conversation that led to it.

Consider an authentication middleware that calls a permissions check before every route handler, including the public-facing ones. A naive agent might eliminate the redundant call as dead code. But the commit that introduced it three years ago says: "Deliberately routing all requests through the permissions layer, even public routes, so future auth changes have a single insertion point. See security audit findings, doc #47." Without that context, the "optimization" is a time bomb.

This is the intent gap. Code encodes decisions, but it encodes only their outcome. The reasoning, the constraints, the alternatives that were rejected: all of that lives in git history, commit messages, design documents, and code reviews. Modern agents are remarkably capable at the syntax and semantics of code. Repository intelligence is the discipline of bridging the intent gap.

Semantic Search: Giving Agents a Map of Your Codebase

Before an agent can reason about your codebase, it needs to find the relevant code. In large repositories with millions of lines across thousands of files, keyword search fails. A function called get_user and another called fetch_account_record might be logically identical. An agent searching for "user lookup logic" needs to find both.

Semantic code search solves this by converting code into vector embeddings and retrieving results by meaning rather than string matching. Tools like Sourcegraph's Deep Search (which uses SCIP for near-compiler-accurate code understanding), GitHub Copilot's instant semantic indexing, and GitLab's semantic search all provide this layer. The agent can ask "where is the authentication logic?" and get accurate results even when variable names differ across files.

This is the enabling layer for everything else. An agent cannot make repository-aware changes if it cannot rapidly locate distant, related code. Semantic search also reduces hallucination: instead of generating plausible-sounding code from training data, the agent grounds its responses in actual codebase patterns it has retrieved and read.

Here is what a semantic retrieval workflow looks like in practice, when an agent is tasked with adding caching to a user lookup function:

# Pseudocode: semantic retrieval steps before the agent writes a single line of code

results = semantic_search(
    query="user lookup function",
    scope="entire repo",
    top_k=5
)
# Returns: auth/user_service.py:fetch_user_by_id, db/user_repo.py:get_user, ...

related = semantic_search(
    query="user cache layer and invalidation",
    scope="entire repo",
    top_k=5
)
# Returns: cache/user_cache.py:set_user, cache/user_cache.py:invalidate_user, ...

tests = semantic_search(
    query="tests for user fetch and cache",
    scope="tests/",
    top_k=5
)
# Returns: tests/test_user_service.py:test_cached_user_lookup, ...

history = git_log_semantic(
    query="cache user lookup",
    max_commits=10
)
# Returns: commits mentioning cache strategy decisions for user data

Each retrieval step narrows the context and shapes the agent's reasoning. The agent that adds caching after this process understands where the cache layer already lives, how invalidation is handled, and whether any past decisions constrain the approach.

Git History as Structured Intelligence

Recent research on agent-augmented development makes a compelling argument that should change how every team writes commit messages: git commit history is a structured knowledge protocol for AI agents. Every well-written commit message is a retrieval target. Every poorly written one is a dead end.

When commit messages encode decision context, constraints, and trade-offs, they become the mechanism by which agents reconstruct the intent behind existing code. This is not a speculative future capability; it is a practical technique available today.

Compare these two commit messages for the same change:

# Weak: describes what, not why
refactor: move auth logic to middleware
# Strong: encodes intent, constraints, and context
refactor: extract authentication middleware from route handlers

Previously, auth logic was scattered across 12 route files, making it
hard to audit and test. This extracts it to a single middleware layer
that can be composed. Preserves existing behavior exactly: no changes
to token format, expiry, or error codes.

Constraint: future auth changes should go through this layer only.
See: docs/auth-refactor-2026.md, security audit findings #47.

The second message is a retrieval target. An agent asked to modify the authentication system can find this commit, understand the architectural decision it represents, and reason about whether its proposed change is consistent with the intent. The first message tells the agent nothing it could not already infer by reading the diff.

Teams that adopt disciplined commit messaging are not just writing better history for humans. They are building a living specification that agents can query.

The Context Window Economy

Even with context windows approaching 200,000 tokens for frontier models, real enterprise codebases routinely exceed what any window can hold. A monorepo with a decade of history and hundreds of contributors is not going into anyone's context window in full.

The practical answer is strategic summarization, and this is where retrieval-augmented generation (RAG) becomes critical for repository-level work. Rather than feeding raw code to the agent, well-designed pipelines extract function signatures, docstrings, dependency graphs, and critical logic paths. Research suggests that this kind of summarization can compress a large codebase representation to roughly a third of its original token count while preserving the structural and relationship information the agent needs to reason accurately.

RAG combines this retrieval step with generation: the agent retrieves the most relevant code, history, and constraints, then reasons and writes within that focused context. The key finding from empirical work is that agent quality scales directly with retrieval quality. A better retrieval mechanism yields more coherent, architecturally sound changes. A weak one yields shadow tech debt.

The core design decision for any team operationalizing repository intelligence is the trade-off between breadth and depth: how much context to provide versus how precise to be about what gets included. For debugging, depth usually wins. For broad refactoring work, breadth matters more. Getting this balance right is an engineering problem worth investing in.

What Agent Refactoring Is Actually Good At

Understanding where agents succeed and fail at refactoring helps set realistic expectations and direct effort appropriately.

Agents deliver the highest return on investment on low-level, high-frequency maintenance: variable renaming, type consistency enforcement, dead code removal, and similar mechanical transformations. They also perform well on medium-level refactorings such as extracting methods and simplifying conditional chains. These tasks are well-scoped, have clear success criteria, and do not require deep architectural judgment.

The picture changes for high-level operations. Research on agent refactoring behavior finds that agents handle architectural changes and multi-file reorganizations at a meaningfully lower rate than human developers. Agents are structurally constrained on the most complex refactoring work, not because they cannot write the code, but because they lack the full project context to reason about systemic consequences.

A second pattern worth noting: a large share of agent refactorings occur in "tangled commits," where refactoring is mixed with feature work. This makes code review significantly harder and is one of the patterns that lets shadow tech debt slip through. Teams that enforce clear separation between refactoring commits and feature commits when working with agents see substantially cleaner review processes.

Encoding Architectural Constraints

The most direct way to prevent shadow tech debt is to make the rules explicit in the context the agent receives. Architectural constraints, design patterns, and invariants the agent should respect need to be surfaced as part of its working knowledge, not left to be inferred from code alone.

This can take several forms. The lightest-weight option is a codebase-level context file (often named AGENTS.md or similar) that states the project's core patterns:

# Architectural Constraints for AI Agents

## Code Style
- Prefer named functions over lambdas; avoid chained lambda expressions
- All database queries go through the repository layer (db/repositories/)
- No direct ORM access from service layer or above

## Error Handling
- All service methods return Result types, never raise exceptions to callers
- Errors are logged at the service boundary, not at the point of occurrence

## Authentication
- Auth middleware in middleware/auth.py handles all authentication
- Do not add per-route auth checks; they will be removed in review
- See: docs/auth-refactor-2026.md for the architectural rationale

## Testing
- Unit tests mock at the repository boundary
- Integration tests use the test database, never mocks

A more robust approach integrates these constraints into the retrieval pipeline, so the agent receives not just abstract rules but concrete examples from the codebase that demonstrate each pattern in action. When the agent can see three examples of how the project handles a particular pattern, it will replicate that pattern rather than invent a new one.

A Practical Path Forward

Implementing repository intelligence does not require rebuilding your toolchain from scratch. Three steps cover most of the ground for a team already using AI coding agents.

First, set up semantic search. If you are on GitHub, Copilot's instant semantic indexing is the lowest-friction starting point. Sourcegraph's Deep Search is the stronger choice for large codebases or cross-repository work. GitLab's semantic search integrates directly with Duo. Pick the tool that fits your environment and make sure your agents are actually using it, not just running keyword searches.

Second, establish a commit message discipline. Start encoding intent into commit messages today. You do not need to retrofit old history. Going forward, include the motivation for the change, any constraints it introduces or preserves, and references to related design decisions. After six months, your git log becomes a retrievable knowledge base. After two years, it is an invaluable asset.

Third, write an architectural constraints document that agents receive as context. Start with the patterns that have caused problems: the ones where a well-meaning agent made a locally sensible change that broke something globally. Make those patterns explicit. Keep the document short enough that agents actually consume it in full, and update it when new patterns emerge.

None of these steps are technically exotic. All three compound over time.

Conclusion

The gap between AI agents that help and agents that create expensive cleanup work is not about model capability. It is about context. Agents operating without repository intelligence optimize for local coherence while ignoring global structure, a combination that produces technically correct but architecturally wrong changes at scale.

Repository intelligence is the discipline of solving this problem deliberately: semantic search so agents can find relevant code, git history structured as a knowledge protocol so agents can understand why decisions were made, RAG pipelines that compress and prioritize what enters the context window, and explicit architectural constraints that give agents the rules of the road. Teams that invest in this layer are not just getting better AI output today. They are building a codebase that gets progressively easier for agents to understand over time.

Subscribe to Marvin G6 | Engineering, AI & Automation

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe