Skip to content

Guardrails for AI-Generated Code

How CodeCharter hooks into the build-fix loop of Cursor, Claude Code, Copilot, and Aider and enforces your conventions on AI-generated code.

AI assistants generate code that looks plausible but doesn't reliably know your conventions. Cursor, Claude Code, Copilot, and Aider have no memory of your architecture decisions beyond the current session. They ignore long convention documents as soon as the context window gets tight, and they produce layering violations every single hour.

CodeCharter hooks into the AI workflow at three points and keeps the AI within your rules.

The Build-Fix Loop

Agentic AI tools today have a built-in cycle of code generation, test run, and self-correction. CodeCharter becomes the convention enforcer within that cycle:

AI generates code
  -> codecharter analyze MySolution.sln --fail-on error
    -> Finding: "Controller calls EF directly. Use repository pattern."
      -> AI reads @recommendation
        -> AI fixes it itself
          -> green build

The @recommendation header in every rule becomes the fix prompt for the AI. Your devs write "Inject TimeProvider via constructor" once, and from that point on the AI fixes it itself, every time, for as long as you use it.

Your DSL Rules as an Executable Team Specification

What used to live in the README or in tribal knowledge becomes an executable file in the repo. When the AI composes the next change, it sees not only the existing code but also the .ccr files that express the rules. That is significantly more reliable than a long convention document that runs out of context window.

Example: your team convention "We use IClock instead of TimeProvider because we're not on .NET 8 yet" is something the AI would otherwise have to infer from existing code. With CodeCharter you write a rule:

@name "Use IClock instead of TimeProvider"
@severity error
@category "Team-Conventions"
@recommendation "Inject IClock via constructor and call _clock.UtcNow instead of TimeProvider.System.GetUtcNow()"

from m in Methods
where m.CalledMethods.Any(c => c.FullName.Contains("TimeProvider"))
select m

On the next attempt the AI gets a clear instruction from the recommendation and can produce the fix in one step.

PR Reviews Become Substantive

When AI generates eighty percent of your code, your PR reviews shouldn't be debating naming conventions. CodeCharter clears away the mechanical findings before the PR is even open. Your human attention stays focused on logic, architecture substance, and edge cases.

Writing Recommendation Text for AI Loops

A @recommendation is not just a hint for your devs — it's also the fix prompt for the AI. Three rules of thumb for good recommendations.

Specific, not vague

@recommendation "Fix this"

That helps neither humans nor AI. Better:

@recommendation "Add 'CancellationToken cancellationToken = default' as the last parameter"

Code snippets are welcome

@recommendation "Inject TimeProvider via constructor and call _time.GetUtcNow() instead of DateTime.UtcNow"

The AI uses the recommendation as a literal instruction for the fix and generates exactly what you described.

References to established patterns

@recommendation "Move EF access into a repository class under Acme.Infrastructure.Repositories and inject it via the constructor"

Concrete namespace guidance helps the AI place the code in the right location.

Setup for Common Tools

Cursor

In .cursorrules:

After every change, run: codecharter analyze MySolution.sln --fail-on error
Fix any findings before considering the change complete. Read the
@recommendation field of each finding for guidance.

Claude Code

In CLAUDE.md:

## Lint-Loop
After making changes, always run `codecharter analyze MySolution.sln --fail-on error`
and fix any errors before reporting completion. The @recommendation field
of each finding tells you how to fix it.

GitHub Copilot with Agent Mode

In the agent system prompt:

Run `codecharter analyze MySolution.sln --fail-on error` after each
change. Findings under 'error' severity block completion. Read the
@recommendation field for fix guidance.

Aider

In .aider.conf.yml:

lint-cmd:
  cs: codecharter analyze MySolution.sln --fail-on error

Aider calls lint-cmd after every code change and feeds the result back to the AI for the next iteration.

Performance in AI Loops

A CodeCharter analysis takes a few seconds to a minute depending on solution size. In an AI loop where the AI already runs the build after every code change, that overhead is negligible.

Further Reading