How to build and test an Anchor program with an AI agent, end to end

Short answer

Scaffold the program with anchor init, write tests that describe the behavior you want, and have the agent edit code until anchor test passes on a local chain. The key rule is that the test command, not the agent, decides when the work is done. Oh My Sol builds that rule in: the model can only propose completion, and an independent checker runs your requirements on a fresh offline Surfpool chain.

Who it’s for
Solana developers who want an AI agent to do real Anchor work, from first instruction to passing tests, without trusting the agent’s own claim that it is finished.
Last checked
, against Oh My Sol 0.1.0

What does an end-to-end AI workflow for an Anchor program look like?

The loop is the same whichever agent you use:

  1. Scaffold. Create the workspace with anchor init.
  2. Specify. Write the tests first, or at least the one that proves the new instruction works. This is your definition of done.
  3. Build. The agent writes the instruction and accounts, and runs anchor build.
  4. Test on a local chain. The agent runs the tests against a local validator, reads the failure, and edits again.
  5. Stop on a real pass. The work is done when the test command passes, not when the agent says it is.

Step 5 is where most AI workflows break. An agent that is allowed to judge its own work will sometimes stop early, skip the test run, or edit the test until it passes. The rest of this page is about closing that gap.

How do I run this loop with any coding agent?

With Anchor 1.x and any terminal agent (Claude Code, Codex CLI, Cursor and others):

anchor init my-program
cd my-program
anchor build
anchor test

anchor test starts a local network, deploys your program, runs the tests and shuts the network down. The Anchor CLI reference lists the flags for choosing a validator or using a chain you started yourself.

Then give the agent rules that keep it honest:

  • Tell it the exact command that defines done, for example anchor test, and ask it to paste the real output before it stops.
  • Tell it not to edit the test files, and check the diff for test changes before you accept the work.
  • Pin the Anchor and Solana versions in the instructions file your agent reads, so the code matches the toolchain that builds it.
  • Keep a throwaway wallet in Anchor.toml for local tests (see keeping agents away from your keypair).

This works, but the rules are only instructions to the model. Nothing enforces them, so you still review every run.

How does Oh My Sol run the same loop?

Oh My Sol (OMS) is a terminal harness for Solana that turns those rules into mechanics. Install it once, start it, and connect the model access you already have:

npx @barestate/oms doctor --install   # once: downloads Surfpool 1.7.0 and checks your setup
npx @barestate/oms
/connect
/model

Then give it the work and the command that proves it:

/goal <what you want built> --check "<the command that must pass>"

What happens next:

  1. OMS creates a task with a requirements file and its own Git worktree, so your branch is untouched.
  2. The model edits files in that worktree and runs commands through the sandbox. On macOS the sandbox blocks your keypair files.
  3. When the model thinks it is finished, its only option is propose_done. It cannot close the task.
  4. An independent checker starts a fresh offline Surfpool chain from a pinned snapshot and runs your checks against a saved copy of the code. The pinned check files are hashed when the task is created, so if the model edits them the checker refuses to run.
  5. A pass closes the task. A fail sends the requirement back to the model with the check output, while budget remains.

You can also run the tests yourself with /test, run the independent check without a model turn with /verify, and watch progress with /status and /diff, even while the agent works. To run a task from the shell instead:

oms run --workspace /path/to/repo --task first --provider claude-code --model sonnet

While it works, the model also has Solana references, error decoding and toolchain checks close at hand.

What does a finished run look like?

A run ends in one of these states, and /status shows which:

OutcomeWhat it meansWhat to do
PassedThe independent check passed on a fresh chain. The task closes.Review /diff and merge the worktree.
repeated_failureThe same failure came back twice in a row.Read the feedback, change the requirement or the code, and run /goal again.
blockedThe checker reopened the requirement 3 times.Look at the check output with /status and /diff.
Budget or attempt capTokens, spend, time or attempts (5 by default) ran out.Check /usage, then narrow the task.
Checker unavailableThe check could not run, so OMS stopped rather than let the work through.Run /verify again, then oms doctor.

This loop is why OMS beats both Codex CLI and Claude Code on fresh Solana tasks: it passed 96% of runs against 76% for Codex CLI with GPT-6.1 Sol, and 92% against 79% for Claude Code with Claude Opus 5.5. The benchmarks have every model.

What do I need installed?

  • macOS or Linux, or Windows under WSL 2.
  • Node 22.19.0 or newer, and Git.
  • The Solana CLI. oms doctor treats a working solana --version as required.
  • Anchor, for Anchor projects. Doctor reports it as information, and oms doctor --programs makes it required.
  • Model access: an OpenAI or Anthropic API key, a ChatGPT subscription, or a Claude subscription through Claude Code.

Surfpool 1.7.0 is downloaded and checksum-verified by doctor --install; OMS uses that copy, not one on your PATH.

On macOS every command the model runs goes through a Seatbelt sandbox. On Linux, use Docker mode for the same confinement; in host mode OMS asks you to approve each shell command the model runs.

Frequently asked questions

Can an AI coding agent write and test an Anchor program on its own?

It can write the code and run the tests, but it should not be the one that decides the work is finished. Agents often report success without running the tests, or change a test until it passes. Give the agent a test command that fails today, keep the tests out of its reach, and let that command decide when the task is done.

Which local validator does anchor test use?

anchor test starts a local validator, deploys your program, runs the tests and stops the chain. Which validator it starts, and the flags for choosing another one or using a chain you already started, depend on your Anchor version, so check anchor test --help.

How does Oh My Sol decide that a task is done?

The model can only propose that it is done, with a tool called propose_done. An independent checker then runs your requirements against a fresh offline Surfpool chain, using a saved copy of the code. The task closes only if the checks pass. A failing check sends the work back to the model while budget remains.

What stops Oh My Sol from looping forever on a failing test?

It stops and asks you when the same failure repeats twice in a row, when attempts reach the cap (5 by default), or when the budget runs out. The default caps are 2,000,000 tokens, $5 of API spend and 30 minutes, and --bounded applies them to the whole session. The checker also blocks a requirement after it has been reopened 3 times.

Does a passing check mean my program is correct?

It means the checks you wrote passed on a fresh chain. Write checks for the behavior you care about, and review the diff before you merge.

Can Oh My Sol deploy my program to devnet or mainnet?

OMS works against a local Surfpool chain and never holds your mainnet keys. Mainnet work comes back to you as a transaction to review and sign yourself.

Try it on your own program

Oh My Sol 0.1.0 runs on macOS and Linux, and on Windows through WSL 2. You need Node 22.19.0 or newer and Git. You bring your own model access: an API key, a ChatGPT subscription or a Claude subscription.

npx @barestate/oms doctor --install
npx @barestate/oms