AI in Real Codebases: A Context Method with Repository Maps, Rules, and Workflows

    In real business repositories, AI assistants often fail not because they cannot write code, but because they do not understand the repository’s boundaries. This article presents a repeatable context method built on three layers: a project map that defines module ownership and prohibitions, domain rules that constrain changes on each surface, and task workflows that guide common changes end to end. It also covers instruction patterns, reusable skill cards, maintenance practices, and practical acceptance checks so AI-assisted work stays aligned with repository architecture across sessions.

    In real business repositories, AI rarely fails to produce usable results because the model “cannot write code.” More often, it fails because it does not know the repository’s boundaries. Reader-facing pages, the admin console, the gateway, authentication, and business services often share the same language and dependencies, and on the surface everything looks editable. Without entry instructions, an assistant may put logic that belongs in a blog business module into a shared library, or apply admin-side interaction habits to the reader-facing experience. API fields then change back and forth, context is lost between sessions, and picking up the work two weeks later feels like taking over a new project.

    The key is not to pile on another, longer requirement. The key is to give the assistant a repeatable way to enter the repository: first read the overall map, then read constraints by task type, and finally change code according to an established workflow. The sections below follow this approach.

    1. Failures Usually Happen at Entry, Not in Syntax

    There are three typical failure modes.

    First, incorrect module ownership. Shared capabilities, cross-service data objects, the gateway, the auth center, business services, the reader-facing side, and the admin side each have different responsibilities. Without a map, an assistant tends to modify whatever is nearest: shared modules are easiest to change because “everyone can use them”; the gateway looks like a universal entry point, so business validation gets pushed into it. The code may compile in the short term, but over time dependency directions become inverted and business rules scatter across layers where they do not belong.

    Second, UI and API style drift. The reader-facing side emphasizes content display and reading paths, while the admin side emphasizes search, review, and batch operations. Even when the two use similar technology stacks, component density, permission prompts, error handling, and empty states should not be mixed. If the assistant sees only “both are frontend,” it will use one template to generate two different products.

    Third, session memory is unreliable. Field names, pagination conventions, auth headers, error codes, and directory naming will not automatically carry over to a new session two weeks later if they exist only in one conversation. People remember “last time we said not to touch the shared module”; the model sees only the current repository snapshot and the current prompt. Writing constraints into repository documents is more reliable than leaving them in chat history.

    Therefore, the entry point should not be a one-sentence requirement. It should be a set of files versioned alongside the code. People read these files before changing code, and the assistant should also be required to read them before making changes.

    2. Three Entry Layers: Map, Rules, and Workflow

    In practice, “letting the assistant enter” can be split into three layers. The names can vary, but their responsibilities should not be mixed.

    Project map. Place it in the repository root and use a short document to explain what each directory means and what is prohibited. It answers: What modules exist in this repository, what does each one own, and where should changes generally not go? The map should be short enough to be read in full at the start of every task. Do not turn it into a complete architecture book.

    Domain rules. Split them by change surface: backend, reader-facing frontend, admin frontend, and gateway/auth can each have their own document. Rules answer: Which boundaries, naming conventions, error handling, and testing habits must this type of change follow? When a task touches only one surface, there is no need to put every rule into context, which avoids diluting relevant constraints with unrelated ones.

    Task workflow. Write recurring work as steps: when adding a read endpoint, changing an admin list, or adjusting a cross-service data object, which layer changes first, which layer changes next, and how to self-check. Workflows answer “how this type of task should proceed,” rather than explaining again “what the system is.”

    With all three layers in place, the assistant can know at the same time: which modules exist in the repository, which boundaries cannot be crossed, and how the current task should proceed. Without a map, changes land in the wrong place. Without rules, style drifts. Without workflows, callers or tests are missed.

    3. Use a Module Map to Hold Boundaries

    Take a common content site backend as an example. A root-level map can be explicit at this level of granularity (illustrative, not tied to one fixed framework):

    • API and cross-service data objects: only external contracts and transport structures; no business workflows.
    • Shared capabilities: utilities, unified responses, and base exceptions; no business-specific rules.
    • Gateway: routing, rate limiting, and auth forwarding; no article publishing or comment moderation.
    • Auth center: login, tokens, and permission principals; no content model.
    • Business services: split by domain; for example, the content domain handles only articles, categories, comments, and similar concerns.
    • Reader-facing side: visitor-facing display and light interaction.
    • Admin side: operations configuration, review, and search.

    Every entry in the map should be written as “what goes here / what does not.” Listing directory names alone is not enough; the assistant will still fill in the gaps on its own. For example: “The content publishing state machine may appear only in the content business service; shared modules must not reference specific business tables; the reader-facing side must not call write APIs available only to the admin side.” Sentences like these are far more useful than “please pay attention to layering.”

    Dependency directions should also be fixed in writing: business services may depend on shared capabilities, but shared capabilities must not depend in reverse on a specific business package. The reader-facing and admin sides may share design tokens and basic components, but pages, routes, and API wrappers should remain separate. If the assistant finds before making changes that a change would break a dependency direction, it should stop and explain rather than “implement first and discuss later.”

    4. How to Instruct the Assistant to Enter First

    Do not start with “build me a feature.” A project-level prompt should write the reading order into the task, for example:

    “You are working in this repository. First read the project description in the root directory, then read the rules index and the relevant domain rules for the task. Backend tasks should read backend rules; reader-facing tasks should read reader-facing rules; admin tasks should read admin rules. After confirming which directories this change is allowed to touch, list the modification plan and the modules you will not modify, then begin implementation. If the requirement conflicts with the rules, point out the conflict first; do not loosen boundaries on your own.”

    This prompt does four things: it limits the scope of work, defines the reading order, requires the assistant to restate boundaries first, and turns conflict handling from “secretly change the rules” into “ask first.” The restatement itself is a check: if the assistant says it will put review logic in a shared module, a human can reject that before any code is written.

    The requirement itself should still be specific, but it should come after entry: which endpoint to change, inputs and outputs, permissions, whether old fields must remain compatible, and how acceptance will be checked. Context is responsible for “can this change, and where”; the requirement is responsible for “what should it become.” Without either one, the result is either the wrong location or the right location with the wrong behavior.

    5. What Rules Must Contain to Be Executable

    If rules are all adjectives (“code should be elegant,” “pay attention to security”), the assistant cannot check against them. Each rule should be written so that a single code review can decide whether it was followed or violated.

    For the backend, rules can state: API inputs and outputs belong in the contract module; business decisions stay in domain services; transaction boundaries stay inside application services; error codes follow a unified structure, and controllers must not assemble ad hoc messages; pagination field names and time-zone/serialization conventions are consistent across the repository; new write endpoints must specify auth and audit fields. Once a cross-service object is used by multiple callers, changes are treated as compatibility changes by default: additive only, no renaming, no semantic changes, deprecated fields retained for one version cycle and recorded in the rules.

    For the reader-facing side, rules can state: routes and layouts follow the existing information architecture; do not introduce admin-side table density or batch operations; empty, loading, and failure states must be distinguishable; API wrappers may call only read APIs already open to visitors and write APIs explicitly allowed for that surface.

    For the admin side, rules can state: lists must include filters, pagination, and a clear prompt when permissions are insufficient; dangerous operations require secondary confirmation; do not copy admin form field validation into reader-facing components.

    The rules index should only dispatch: it explains which type of path maps to which document, so the assistant does not need to read every document each time and waste context on irrelevant sections.

    6. Workflows Turn “Change One Place” into “Complete a Chain”

    Much rework comes from changing only the server or only the page. Workflow checklists should write out the whole chain. For example, “add a filterable status to articles”:

    1. Confirm in the project map that the status belongs to the content domain and does not go into shared modules.
    2. Read the backend rules and existing enums in the content domain, and check whether a similar status already exists to avoid creating a second naming scheme.
    3. Change contracts and data objects first while maintaining backward compatibility; then change queries and writes in the business service; finally change the surface or surfaces that need to display the status.
    4. If both the reader-facing and admin sides need to display it, change them separately according to their own rules. Do not force one shared page component with hard-coded branches.
    5. Self-check: dependency direction, default behavior for old clients, permissions, error messages, and whether there is a minimal test or manual verification step.

    When continuing the same requirement two weeks later, the new session still enters through the map and rules, and uses the workflow checklist to align on “where we stopped last time.” Progress should be recorded in task notes or an issue-style record, not in chat history. This way, “re-entering the project” means rereading the documents, not reinventing the directory structure.

    7. Skill Layer: Turn Mature Practices into Reusable Steps

    Once the map and rules are stable, high-frequency tasks can be collected into shorter operation cards: add a read-only endpoint, add an admin form, adjust a gateway route, add a permission. Each card should reference the rules rather than copy large sections of principles, so the two do not become inconsistent later. Operation cards are for “how to finish”; rules are for “what not to do.”

    Operation cards should say when to stop: the corresponding module cannot be found, the rules conflict with the requirement, the change would make a shared library depend on a business package, or an API already has callers but the requirement asks to change field semantics. Stopping to report is more valuable than generating a large amount of plausible-looking code.

    8. Checks and Boundaries During Rollout

    After this entry system is in place, validate it with several real changes rather than with vague scores:

    • Can the assistant correctly identify the allowed directories before changing code?
    • Does it still write business rules into shared modules or the gateway?
    • Are reader-facing and admin components and APIs kept separate?
    • Do field changes go through contracts first and consider old callers?
    • Can a new session continue a two-week-old task using only the repository documents?

    The documents themselves must also be maintained. If module boundaries change but the map is not updated, the assistant will follow stale boundaries and do the wrong thing, which is more misleading than having no documents at all. Rule reviews can be combined with code reviews: if a dependency direction changes, the corresponding “what does not go here” sentence in the map should change too.

    This method does not depend on a particular model or editor. It solves an engineering problem: turning repository conventions that exist only in senior engineers’ heads into constraints that both assistants and new team members can read first. For AI to land in real scenarios, the entry point must change from a single requirement into a map, rules, and workflows. Otherwise, it is a new colleague every time—one that will not proactively ask who owns the room at the end of the hall.