Skip to content

Autofix

Pavel Erokhin edited this page Nov 30, 2026 · 1 revision

Русская версия

Autofix

ctxray fix fixes what has one right fix. ctxray fix --ai hands the rest to an AI harness: it shortens a file over its limit, writes a skill description the model can pick by, points a broken link at the file it meant. ctxray checks every change the harness makes and keeps only the ones that fix a finding and bring nothing new.

Start

export ANTHROPIC_API_KEY=…
ctxray fix --ai --dry-run   # the prompt and a rough cost, nothing runs
ctxray fix --ai

A model runs only with --ai: the autofix section of the config alone never starts one. Without that section, Claude Code fixes with its default model, through the provider's own API, and stops at 1,000,000 tokens. Like ctxray fix, the run edits your working tree, and --dry-run writes nothing.

What it fixes

The harness gets the findings of these checks: too-large, size, codex-budget, short-description, long-description, no-description, similar-descriptions, skill-listing, import-missing, broken-link, dead-rule, absolute-path, unread, duplicate-skill, duplicate-text, frontmatter, bad-json, and bad-toml. Lint Rules marks each of them.

Some findings never go to a model:

  • secret and personal-data: the key needs revoking, and a model can't do that.
  • broken-symlink, empty-file, and shadowed-file: the fix deletes a file, and a model never deletes one.
  • skill-name and skill-folder-name: renaming a skill breaks whatever calls it by name.

The model may change what the text says: rewrite and shorten an instruction, rephrase a skill's description. It may not lose a requirement on the way, and the prompt says so in so many words.

How a run goes

  1. ctxray fixes what has one right fix, without a model, as ctxray fix does.
  2. It lists the findings that are left for the harness, errors first, at most maxFindings of them.
  3. The harness runs on a copy of the project, in the same sandbox as L6 and L7: a HOME of its own, the network only to ctxray's proxy, no keychain, and a dummy key. The project's own MCP servers and hooks stay off.
  4. ctxray takes the harness's changes into its own copy. A change outside the allowed paths, a deleted file, and a change to a file with a secret never make it.
  5. L1 and L2 run again. The file whose change brought a new finding, even a warning, goes back. At the end, a change that fixed nothing goes back too.
  6. The harness gets what's left and why its changes went back, for another round, up to maxRounds. The budget stops the run whenever it's spent.
  7. Only then do the changes reach your files.

The report is the one ctxray fix prints: Fixed, Left with the reason, and Not fixable automatically. A finding the model left says the model's own reason. Under the report, ctxray names the harness and the model, the rounds, the tokens, and the cost when it knows the model's price.

The prompt

The built-in prompt has a system part and the findings. The system part tells the harness:

  • what the files are and that every instruction in them is a requirement,
  • how ctxray checks its work, so a fix that trades one warning for another is worth nothing,
  • what it may do: rewrite, shorten, move detail into a file that loads when needed, fix paths, links, and syntax,
  • what it must not do: delete an instruction or a file, add ctxray-ignore, raise a limit, touch a file outside the allowed paths, rename a skill, change code blocks and commands, run anything,
  • to keep each file's language, encoding, and line endings,
  • to write a report, one line per finding: FIXED F1: … or LEFT F2: ….

Each finding comes with its file, line, check, message, a link to its section of Lint Rules, and a hint for its check. Findings come grouped by file. ctxray fix --ai --dry-run prints the whole prompt of the first round. Where the harness takes extra system instructions, the system part goes there: --append-system-prompt in Claude Code, developer_instructions in Codex. The others get it at the top of the prompt. The prompt is always in English, the model's language: the model keeps the language of your files itself.

Configuration

export default defineConfig({
  autofix: {
    harness: "claude-code", // the version comes from ctxray.lock
    model: "claude-sonnet-5-5",
    upstream: "https://llm-gateway.corp.example",
    apiKeyEnv: "CORP_LLM_TOKEN", // the variable's name, never the key
    headers: { "X-Team": "${TEAM_ID}" },
    ca: ["certs/corp.pem"],
    rules: ["size", "short-description", "broken-link"],
    maxFindings: 20,
    maxRounds: 3,
    budget: { usd: 2 },
    paths: ["CLAUDE.md", "AGENTS.md", ".claude/", ".agents/"],
    prompt: { file: "ctxray/autofix.md", mode: "append" },
    skills: ["ctxray/skills/style-guide"],
    mcp: { docs: { url: "https://mcp.corp.example/docs" } },
  },
});
Key Default What it does
harness claude-code The harness that fixes: claude-code, codex, gemini-cli, opencode, or cursor-cli (with experimental: true under harnesses). Its build comes from ctxray.lock
model, provider the harness's own The model it runs with, and the provider for a harness with several
upstream the config's upstream The gateway the requests go to
apiKeyEnv the provider's variables The name of the variable with the key. Without it, ctxray reads the variables of Enterprise
headers none Headers the gateway needs. ${NAME} takes a variable's value
ca none Extra CA certificates, on top of network.ca
rules every check above The checks to fix
maxFindings 20 Findings in one run. The next run takes the rest
maxRounds 3 Fix, check, and fix again, at most this many times
budget 1,000,000 tokens usd or tokens. The dollars need the model's price: see prices
paths the agent's files What the harness may change, as .gitignore patterns
allowIgnore false Let the harness add ctxray-ignore where a finding is a deliberate choice
projectConfig false Let the project's own MCP servers and hooks into the run
prompt the built-in one Your prompt: below
skills none Skill folders for the harness that fixes
mcp none MCP servers for the harness that fixes

The agent's files by default: CLAUDE.md, AGENTS.md, AGENTS.override.md, GEMINI.md, .cursorrules, .mcp.json, opencode.json, and the folders .claude/, .agents/, .codex/, .cursor/, .gemini/, .opencode/.

Your prompt, model, skills, and MCP servers

A company ships one setup to every team with extends: the prompt, the model, the gateway, the skills, and the MCP servers. Paths in an extended config resolve against that config, so the package carries its own files.

// @acme/ctxray-config/index.ts
export default {
  autofix: {
    model: "claude-opus-5-5",
    upstream: "https://llm-gateway.acme.example",
    apiKeyEnv: "ACME_LLM_TOKEN",
    prompt: { file: "autofix.md", rules: { size: "Keep CLAUDE.md under 200 lines." } },
    skills: ["skills/acme-style"],
    mcp: { handbook: { command: "acme-handbook-mcp", args: ["--stdio"] } },
  },
};
// The team's ctxray.config.ts
export default defineConfig({
  extends: "@acme/ctxray-config",
  autofix: { prompt: { text: "Our files are in Russian: keep them so." } },
});
  • prompt.file and prompt.text go after the built-in prompt, under “Team instructions”, the file first. A team adds its words to the company's this way.
  • prompt.mode: "replace" makes them the whole prompt. It must say where the findings go with {{findings}}. Other placeholders: {{paths}}, {{harnesses}}, {{maxRounds}}, {{reportFile}}, {{ignoreRule}}, {{locked}}.
  • prompt.rules puts your hint under a check's findings in place of the built-in one.
  • skills and mcp go to the harness that fixes, in its HOME of its own, as if a user had installed them. They never land in your project, never show in the diff, and never meet the checks. An MCP server takes command, args, and env, or url and headers, and ctxray writes it the way each harness reads it.

Keys and secrets

  • The key comes from an environment variable only. apiKeyEnv names it, and an error names the variable, never its value.
  • The harness holds a dummy key. ctxray's proxy puts the real one in on the way to the gateway, with your headers.
  • A file with a secret or personal data goes to the harness redacted, and the harness may not change it. The prompt, the model's report, and the harness's output pass the same redaction as every other output of ctxray.
  • The project's MCP servers and hooks run the project's code, so they stay off unless projectConfig says otherwise. Cursor CLI runs the project's hooks whatever ctxray tells it, so with .cursor/hooks.json in the project it fixes only with projectConfig: true.
  • Codex keeps .agents and .codex read-only in its own sandbox, and those are the files it has to fix. In ctxray fix --ai, ctxray's sandbox takes the place of Codex's: on macOS it confines the writes to the run's folder, in CI the container does.

In CI with a corporate gateway

GitHub Actions, with the key in a repository secret:

permissions:
  contents: write
  pull-requests: write
steps:
  - uses: actions/checkout@v7
  - uses: MairwunNx/ctxray-action@v0
    env:
      CORP_LLM_TOKEN: ${{ secrets.CORP_LLM_TOKEN }}
    with:
      args: test --layer L1-L2
      fix: true
      fix-ai: true

GitLab CI, with the key in a masked variable CORP_LLM_TOKEN and the project access token in CTXRAY_FIX_TOKEN:

include:
  - remote: https://github.com/MairwunNx/ctxray/releases/latest/download/ctxray.gitlab-ci.yml

ctxray-fix:
  extends: .ctxray-fix
  variables:
    CTXRAY_FIX_AI: "true"

Both take the gateway, the model, and the budget from the autofix section above. The result goes out as for ctxray fix: suggestions on a pull or merge request, a pull or merge request from ctxray/fix on the default branch, a patch artifact when the token can't write, and a commit to the request's own branch only with fix-commit or CTXRAY_FIX_COMMIT. The model runs once per pipeline, however the result goes out. CI Integration has the details.

A run without a model: cassettes

--ai-cassette <file> runs the harness on a mock model that writes the files of a cassette, round by round. Everything else happens as in a real run: the sandbox, the checks, the rollback, the report. It costs nothing, so it tests your setup, your paths, and your CI job:

{
  "rounds": [
    {
      "writes": { "AGENTS.md": "# Payments\n\nRead the [release guide](docs/releasing.md).\n" },
      "report": "FIXED F1: pointed the link at docs/releasing.md\n"
    }
  ]
}

JSON

ctxray fix --ai --json prints the fix schema with one more field, ai: the harness, the model, the backend (real or mock), the rounds, the tokens, the cost, and why it stopped (done, rounds, budget, or failed). With --dry-run, ai holds the prompt and the estimate instead.

Clone this wiki locally