You have an issue sitting in your GitHub repo, and by the end of this loop you want a merged pull request that closes it, with a human reviewing the diff as the final gate. That is the whole job. Codex is built to run most of it while you stay in control.
This lesson walks that loop concretely: pick up the issue, let Codex branch and implement, run the tests, open the PR, review, and merge. The discipline that makes it work is plain: branch hygiene, small diffs, and treating review as a real checkpoint instead of a rubber stamp.
Where Codex lives in this workflow
Codex is OpenAI's software engineering agent. It reads your repo, edits files, runs commands, and pushes branches. You can reach it from several places:
- Codex in ChatGPT (the cloud version), which runs each task in an isolated sandbox.
- The Codex CLI in your terminal.
- The Codex IDE extension for VS Code and its forks such as Cursor.
- GitHub itself, where you can mention
@codexon a pull request.
It connects to GitHub through the Codex GitHub integration, so it can open and update PRs directly.
The mental model: Codex is a teammate who does the mechanical work of a change fast, but who never merges its own code. You assign scope, it produces a diff, you review and merge. GitHub's branch protection enforces that boundary, so set it up first.
Set the guardrails before you start
On the repo, enable branch protection (or a ruleset) on your default branch:
- Require a pull request before merging.
- Require at least one approving review.
- Require status checks (your CI) to pass.
Now even an agent that pushes code cannot land it without a green build and a human approval. This is the single most important setting for letting an agent work in a real repo.
Step 1: Pick up the issue
Start from a real, well-scoped issue. Codex works best when the issue reads like a spec. Compare:
Weak: "Login is flaky."
Strong: "Users with emails containing a + alias (e.g. sam+test@x.com) get rejected at signup. Expected: RFC-compliant addresses with + are accepted. See validateEmail in src/auth/validate.ts. Add a failing test first."
The strong version names the file and the expected behavior, and it asks for a test. That constraint alone tends to cut the diff in half.
In Codex, point it at the repo and paste the task. If your repo has an AGENTS.md file at the root, Codex reads it as standing instructions: how to run tests, code style, what not to touch. You can also add more AGENTS.md files in subfolders for package-specific rules. Treat this file as the durable version of your custom instructions for this codebase.
# AGENTS.md
## Commands
- Install: `pnpm install`
- Test: `pnpm test`
- Lint: `pnpm lint`
## Conventions
- TypeScript strict mode. No `any`.
- One logical change per PR. Keep diffs under ~150 lines.
- Never edit files under `src/generated/`.Check your reading
What is the core mental model the lesson uses to describe Codex's role in the issue-to-merge workflow?
Step 2: let codex branch
Do not let Codex work on main. Tell it to create a branch with a clear name tied to the issue:
codex "Fix issue #214: accept + aliases in validateEmail.
Branch: fix/214-email-plus-alias.
Write a failing test first, then make it pass."Good branch names are boring and traceable: fix/214-email-plus-alias, feat/187-csv-export. With the issue number in the branch name, anyone can trace code back to the conversation that created it. One branch, one intent, easy to delete after merge.
Codex starts working, and you will see it read files, form a plan, and run commands. Let it run, but read the plan before it writes much. If the plan already looks too big (it wants to refactor three modules for a one-line bug), stop it and narrow the scope now. Narrowing at review time costs more.
Step 3: Implement and test the change
The order matters, and it is worth being explicit with Codex: failing test first, then the fix. This is the agent version of test-driven development. It proves the bug exists, and it proves your fix addresses that bug and not something adjacent.
A clean loop for our example, using Vitest:
// src/auth/validate.test.ts
import { describe, it, expect } from "vitest";
import { validateEmail } from "./validate";
describe("validateEmail", () => {
it.each(["sam+test@example.com", "sam+news+promo@example.com"])(
"accepts plus alias %s",
(email) => {
expect(validateEmail(email)).toBe(true);
}
);
it("still rejects a bare plus", () => {
expect(validateEmail("+@example.com")).toBe(false);
});
});Codex adds the test, runs pnpm test, watches it fail, then edits validateEmail until the suite is green. Because it runs real commands, you get the real output instead of a guess about whether the code works. If a test fails in a way it did not expect, it iterates.
Watch that it does not "fix" the test to match broken behavior. That is a classic agent failure mode, and it is why you asked for the failing test up front: you can read the test and confirm it encodes the behavior you want.
When it finishes, Codex reports what changed and why. Read that summary, but do not trust it in place of the diff.
Video
Codex: From Issue to Pull Request
OpenAI walkthrough of assigning a task and reviewing the resulting PR
Watch on YouTubeStep 4: Open the pull request
Have Codex open the PR from its branch into main. A good agent-authored PR body lets a reviewer approve in minutes:
## What
Accept email addresses with `+` aliases in `validateEmail`.
## Why
Closes #214. Aliased addresses are RFC 5321 valid; we were rejecting them.
## Changes
- `src/auth/validate.ts`: widened local-part regex to allow `+`.
- `src/auth/validate.test.ts`: added alias-accept and bare-plus-reject cases.
## Testing
- `pnpm test` passes (3 new cases).Note Closes #214. That keyword links the PR to the issue and closes the issue when the PR merges. Your board stays honest with zero extra clicks.
The PR triggers your CI. Because you required status checks, the merge button stays disabled until the build passes. Your first automated gate does its job before a human even looks.
Step 5: Review the diff (the real gate)
This is the step you never delegate. Open the Files changed tab and read every line as if a junior engineer wrote it, because functionally one did.
What to look for in agent-generated diffs:
- Scope creep. Did it only touch what the issue needed? Reformatted files, renamed unrelated variables and "while I was here" edits inflate the diff and hide the real change. Ask Codex to revert anything off-topic.
- The regex or logic that "fixes" it. A widened regex can accept
+and also accept garbage. Read the actual pattern, because subtle security and correctness bugs live there. - Test quality. Does the test fail without the fix? Does it assert the right thing? A test that passes even with the bug present is worse than no test.
- Secrets and config. Confirm nothing hardcoded a key or changed an env default.
Small diffs make this possible. A 40-line PR gets a real review. A 600-line PR gets skimmed and waved through, which defeats the point of the human gate. If Codex produced something large, ask it to split the change into two or three sequential PRs, each reviewable on its own.
You can also leave review comments on the PR and tag @codex to address them. It pushes new commits to the same branch, CI re-runs, and you re-review the delta. Most of the real collaboration happens in that comment loop.
Codex can review code too. Comment @codex review on a PR, or turn on automatic reviews in Codex settings, and it will flag likely bugs. Treat that as a second pair of eyes before your own pass. It does not replace your approval, and branch protection still needs a human.
Check your reading
Why does the lesson call branch protection 'the single most important setting' when letting an agent work in a real repo?
Step 6: Merge and clean up
Once CI is green and you have approved, merge. Prefer Squash and merge for agent work. The branch may contain a dozen tiny iteration commits ("fix test", "oops", "lint"), and squashing collapses them into one clean commit on main with your PR title as the message.
After merge, delete the branch. GitHub offers a button right after merging, and you can turn on auto-delete in repo settings so stale agent branches never pile up. The issue closes automatically because of your Closes #214 line. Issue in, reviewed and tested change out, clean history.
A note on choosing a surface
The surfaces suit different tasks:
- Cloud Codex (ChatGPT, or
@codexon GitHub) is best for asynchronous, self-contained tasks. Assign it, walk away, review the PR when it is ready. - The CLI and IDE extension are best when you want to watch and steer in real time, work against uncommitted local changes, or use local-only tooling. The IDE extension can also hand a task off to the cloud and pull the result back.
All of them respect your AGENTS.md. The Codex docs will always be more current than any tutorial, so bookmark them.
Why this loop scales
Codex slots into a process that already exists. You are not inventing a new merge policy for AI. You point an agent at the same branch protection, CI and review culture your humans use, and the agent inherits those guardrails. Failing tests first, small diffs and mandatory human approval make human PRs safe, and they make agent PRs safe for the same reasons.
The failure mode to avoid is speed pressure. Codex produces diffs fast, and it is tempting to approve fast to match. Resist that. Review quality should be your bottleneck, because review is the only step that catches the plausible-but-wrong change.
Key Takeaways
- Set branch protection before Codex touches the repo. Require a PR, a review and passing checks so an agent cannot merge unreviewed code.
- Write issues like specs: name the file, the expected behavior, and ask for a failing test first. Put standing rules in
AGENTS.md. - Keep one branch per intent and squash on merge. Issue numbers in branch names plus
Closes #NNNkeep history and your board clean. - Never delegate the diff review.
@codex reviewcan catch bugs early, but a human reads every line and approves. - Use cloud Codex for fire-and-forget PRs, and the CLI or IDE extension when you want to steer live.
To do
What to do, from this lesson
These actions are compiled in the role's Playbook.
- Set branch protection requiring PR, review, and passing checks before Codex runs
- Run autonomous loops only with a machine-checkable done condition and hard caps
- Review every diff for the deleted-failing-test cheat and scope creep
Quiz
Did the essentials stick?
3 questions, one at a time. Each one right on the first try brings the probe closer to the planet.
Question 1 of 3