The Forge · 21 min mission

Loop Engineering in Codex

Use /goal, automations, AGENTS.md, skills, subagents, and review gates as one control system.

loop engineeringgoalautomationsagents.mdsubagentsFact-checked 2026-06-21
On this page

This guide shows you how to build Codex loops that use /goal, automations, AGENTS.md, skills, subagents, sandbox profiles, and review gates as one control system.

Who this is for

Use this guide when a single Codex prompt is not enough: product-from-scratch work, backlog repair, production-error sweeps, CI failure repair, documentation drift, and engineering-manager operating loops. The examples assume a git repo, tests or CI, and permission to add repo instructions.

You need:

  • Codex with /goal available.
  • A repo-level AGENTS.md.
  • A verifier such as tests, build, CI, logs, screenshots, or /review.
  • A place to store loop state, usually docs/agent-runs/.
  • A clear sandbox and approval policy before any scheduled or unattended work.

Expected result: a Codex loop that can run one bounded pass, produce evidence, ask for review, and stop or escalate without silently widening scope.

Start with the short version in Loop Engineering for AI Coding Agents, or compare the companion Claude Code Loop Engineering guide when your workflow uses /goal, /loop, hooks, and subagents.

PrimitiveLoop roleUse it forFailure to avoid
/goalActive objective and completion conditionOne feature slice or repair batchVague goal with no verifier
AutomationScheduled or background cadenceDaily PR sweep, weekly docs drift, nightly error triageWrite access before triage is proven safe
AGENTS.mdRepo policy and review rulesTest commands, protected paths, severity rulesHuge instruction file that hides the real constraints
SkillReusable procedure and assetsBacklog loop, release loop, incident loop, docs loopSkill overlaps with AGENTS.md and triggers too broadly
SubagentParallel worker or fresh reviewerSecurity pass, docs pass, test adequacy, multi-package triageNo consolidation rule
Sandbox + approvalPermission boundaryRead-only discovery, workspace-write implementation, owner gatesUnattended write access on high-risk paths
/reviewDiff review before mergeP0/P1 check, missing tests, risky behavior changesSame agent self-approves its own work
Codex loop primitives. Use them together instead of treating each one as a separate trick.

Build and audit a Codex loop

Loop engineering lab

Build, simulate, and audit an agent loop

Use this when a prompt has to survive more than one pass. The lab emits a reusable contract, animates the execution path, builds nested loop runbooks, and checks whether the loop is safe to run.

Audit score15/17
DecisionHuman-supervised only
Trigger
Verifier
Risk gate

Contract output

/goal ship the smallest working onboarding-to-first-value slice without expanding into adjacent features

Role:
Act as the implementer for one bounded loop pass. Do not grade your own final answer.

Context to read first:
- Nearest repo instructions: AGENTS.md, CLAUDE.md, or package-level rules.
- State file: docs/agent-runs/product-slice-loop.md.
- Current diff, linked issue, relevant tests, and recent failing output.

Scope:
- Work inside: one user path, one data model, one happy path, one failure state.
- Final artifact: feature branch, run log, screenshots, test output, and review notes.
- Invariant: do not add the next feature until this slice is usable and verified.

Pass protocol:
1. Write a pass plan with files in scope, files out of scope, risk, and verifier.
2. Make the smallest useful change.
3. Run verifier: npm test && npm run lint && npm run build
4. Update docs/agent-runs/product-slice-loop.md with commands, evidence, changed files, and next decision.
5. Ask for fresh review before claiming done.

Stop when:
- the selected test, lint, and build commands exit 0 with no ignored failures.
- The run log has changed files, commands, evidence, and remaining risks.
- Fresh review finds no P0/P1 issue.

Escalate when:
- The same failure repeats twice.
- The work leaves scope: one user path, one data model, one happy path, one failure state.
- The agent proposes weakening tests, logs, or monitoring.
- pause before schema changes, external API changes, broad refactors, or dependency upgrades.
Use the lab to generate a Codex /goal contract, simulate one pass, build a nested loop runbook, and score the loop before you make it scheduled or unattended.

Put the stable rules in AGENTS.md

The loop prompt should not repeat every team rule. Put stable policy in AGENTS.md, then keep the prompt focused on the current objective, state file, verifier, and escalation rule.

AGENTS.md loop policy
markdown
## Agent loop policy
 
For any multi-pass task:
 
1. Create or update docs/agent-runs/<task-slug>.md before editing.
2. Record objective, scope, verifier, pass log, risk, and escalation conditions.
3. Make one bounded pass at a time.
4. Run the verifier after each pass.
5. Stop instead of widening scope silently.
 
Required evidence:
 
- changed files
- commands run
- verifier result
- fresh review result
- remaining risk
 
Forbidden shortcuts:
 
- Do not delete, skip, or weaken tests to make a loop pass.
- Do not mute logs or alerts to reduce noise.
- Do not broaden catch blocks without root-cause evidence.
- Do not touch auth, payments, secrets, migrations, data deletion, or deployment scripts without an explicit owner approval note.
 
Review guidelines:
 
- P0: data loss, auth bypass, secret exposure, payment risk, destructive migration.
- P1: production regression, missing regression test, broken critical path.
- P2: maintainability risk that should be fixed before merge.
- P3: style, clarity, or small cleanup issue.

Configure sandbox profiles before the loop runs

Do not decide permissions mid-loop. Define profiles for read-only discovery, supervised implementation, and tightly scoped unattended work.

~/.codex/config.toml profiles for loop work
toml
[profiles.loop_readonly]
sandbox_mode = "read-only"
approval_policy = "never"
 
[profiles.loop_workspace]
sandbox_mode = "workspace-write"
approval_policy = "on-request"
 
[profiles.loop_unattended]
sandbox_mode = "workspace-write"
approval_policy = "never"
 
# Use loop_unattended only for reversible work with deterministic checks,
# no production secrets in scope, and an explicit state file.

Write the /goal as a contract

/goal should own one objective. It should not own the whole project. Long instructions belong in AGENTS.md, a skill, or the state file.

Codex /goal contract for one product slice
markdown
/goal Ship the private-beta invite request slice.
 
Goal:
A user can request a private-beta invite, see the saved pending state, and see a duplicate-request error for an existing email.
 
Read first:
- AGENTS.md
- docs/product/private-beta.md
- app/invite/**
- lib/db/**
- current diff
 
State:
- Maintain docs/agent-runs/private-beta-invite-loop.md.
 
Scope:
- In scope: invite UI, API handler, persistence, duplicate-request state, tests.
- Out of scope: billing, team roles, email provider, admin dashboard.
 
Verifier:
- npm test
- npm run lint
- npm run build
- browser check for happy path and duplicate-request error
 
Done when:
- The user path works end to end.
- The verifier passes.
- /review or a fresh reviewer reports no P0/P1 issue.
- The state file lists changed files, commands, screenshots, and remaining risk.
 
Escalate when:
- Auth/session behavior changes.
- A migration, external provider, or product decision is needed.
- The same failure repeats twice.

Use a state file as the loop ledger

The state file is the artifact that lets Codex resume, an automation continue, or a reviewer audit the run without trusting a final summary.

docs/agent-runs/private-beta-invite-loop.md
markdown
# Private-beta invite loop
 
## Objective
A user can request a private-beta invite, see the saved pending state, and see duplicate-request feedback.
 
## Scope
- In scope: invite UI, API handler, persistence, duplicate-request state, tests.
- Out of scope: billing, roles, email provider, admin dashboard.
 
## Verifier
`npm test && npm run lint && npm run build`
 
Browser checks:
- /invite accepts a new request.
- /invite shows duplicate-request feedback for an existing email.
 
## Pass log
| pass | hypothesis | files changed | verifier result | evidence | decision |
| --- | --- | --- | --- | --- | --- |
| 1 | | | | | |
 
## Review log
| reviewer | scope | result | blocking findings |
| --- | --- | --- | --- |
 
## Risk register
| risk | owner | trigger | next check |
| --- | --- | --- | --- |
 
## Final evidence
- Changed files:
- Commands:
- Screenshots:
- Remaining risks:

Package repeated procedures as a skill

Use a Codex skill when the procedure is reusable across repositories or teams. Keep repo-specific policy in AGENTS.md; keep the reusable method in the skill.

skills/backlog-loop/SKILL.md
markdown
---
name: backlog-loop
description: Use when triaging and repairing a scoped batch of backlog or production-error issues with a state file, verifier, and review gate.
---
 
# Backlog loop skill
 
Use this procedure only for a scoped issue batch.
 
## Required inputs
 
- issue query or issue list
- repo instructions
- state file path under docs/agent-runs/
- verifier command
- review gate
 
## Procedure
 
1. Read the state file and nearest repo instructions.
2. Sort issues by impact, age, and reproducibility.
3. Pick one issue.
4. Reproduce it with a failing test, script, log query, or explicit non-repro note.
5. Patch root cause only.
6. Add or update a regression check.
7. Run the verifier.
8. Update the issue with evidence.
9. Ask for fresh review before closing.
 
## Stop conditions
 
- verifier passes and fresh review has no P0/P1 finding
- same failure repeats twice
- owner decision is needed
- scope expands beyond the batch

Run a read-only discovery pass before editing

For backlog, incident, and manager loops, start with a read-only run. The first output should be a plan and state update, not a diff.

Headless read-only discovery pass
… scroll to run this session
Use this shape when the loop should inspect before it edits. Adjust the profile and prompt path for your setup.

Product-from-scratch loop

For product work, use Codex as a slice runner. The outer product loop chooses the next slice. The inner implementation loop proves it.

LoopInputCodex surfaceVerifierOutput
Brief loopuser notes, support tickets, repo constraintsmanual /goal or read-only taskbrief has user, first-value moment, non-goalsproduct brief
Architecture loopbrief, repo map, data modelmanual /goalfiles in scope/out of scope are explicitslice plan
Implementation loopslice plan, state filemanual /goaltests, lint, build, browser checkfeature branch
Review loopdiff, state file, original goal/review or subagentno P0/P1 findingmerge decision
Release loopdiff, docs, monitoring, rollback noteautomation or manual runrelease checklist has owner and post-deploy checkrelease note
Nested product loop stack for Codex.

Backlog and production-error automation

Automations work best when the job is repetitive and idempotent. A safe automation can run every day and either update a state file, open a draft branch, or escalate to an owner without creating duplicate work.

Codex automation prompt: daily production-error sweep
markdown
Name: Daily production-error sweep
Cadence: Weekdays at 09:00
Worktree: Fresh automation worktree
Default profile: loop_readonly
 
Prompt:
1. Read AGENTS.md and docs/agent-runs/production-error-loop.md.
2. Pull the top recurring error family from the configured issue or log source.
3. Identify count, impacted path, owner, suspected root cause, and recent related commits.
4. If the fix is low-risk and scoped, create a plan and wait for approval before editing.
5. If the fix is not clear, update the run log and open or update the issue with evidence.
 
Stop when:
- One prioritized error family has evidence and owner.
- Duplicate issues are linked.
- No code was changed without approval.
 
Escalate when:
- Customer data, auth, payments, secrets, deletion, migration, or deployment risk is in scope.
- The issue source cannot be queried.
- The same error was triaged twice without an owner decision.

Engineering-manager operating loop

A manager loop should output owner actions, not a general status digest. It should compare systems of record to evidence and identify missing proof.

.github/codex/prompts/manager-sweep.md
markdown
Run the manager operating loop.
 
Read:
- AGENTS.md
- docs/agent-runs/manager-operating-loop.md
- open PRs older than two business days
- failed CI checks
- unresolved incident follow-up items
- changed code paths without docs updates
 
Classify each item:
- healthy
- blocked
- owner missing
- verifier missing
- stale decision
 
Output:
| item | owner | status | latest evidence | next observable check | escalation date |
| --- | --- | --- | --- | --- | --- |
 
Rules:
- Do not edit code.
- Create docs-only draft changes only when the missing update is obvious.
- Every escalation must name the owner or the owner gap.

Use /review as the merge gate

The implementing loop should not be the only reviewer. Run /review or a fresh reviewer against the diff before merge. Make the review scope specific.

Review prompt for loop output
markdown
/review Focus on:
 
- P0/P1 regressions
- missing regression tests
- auth, payments, secrets, migration, deletion, and deployment risk
- verifier weakening
- scope expansion beyond docs/agent-runs/private-beta-invite-loop.md
- mismatch between the final summary and the actual diff

Verify the loop before scheduling it

Run the loop manually once before creating an automation:

  1. Run discovery with the read-only profile.
  2. Confirm the state file updates without edits.
  3. Run one supervised implementation pass with loop_workspace.
  4. Run the verifier yourself for high-risk work.
  5. Run /review against the diff.
  6. Confirm the state file records evidence and open risk.
  7. Only then convert the prompt into an automation.

If the manual loop cannot stop cleanly, the scheduled loop will make the same mistake on a clock.

SymptomLikely causeFix
Automation creates duplicate issuesNo idempotency checkRead existing issues and state file before opening new work
Codex edits before understanding the incidentDiscovery profile has write accessStart with loop_readonly and require approval before patching
Goal never completesStop condition is subjectiveReplace "good enough" with tests, CI, log query, screenshot, or review rubric
Diff touches protected pathsRisk gate is only proseAdd owner gate to AGENTS.md and require review before merge
Review finds missing tests lateVerifier was not named in the contractPut the exact verifier in the state file and goal prompt
Agent weakens the check to passVerifier is editable by the same loopRequire fresh review for test deletion, assertion weakening, log suppression, and catch-all changes
Common Codex loop failures and fixes.

Limitations

Codex loops still need human ownership for product scope changes, customer data, auth, payments, secrets, migrations, destructive operations, deployment decisions, and incident communication. Automations provide cadence; they do not decide whether a risky change should ship. Use read-only discovery for unclear work and require a named owner before high-risk edits.

Glossary

  • Loop: a repeated agent pass with state, verifier, and stop rule.
  • State file: a durable record of objective, scope, pass log, evidence, and risk.
  • Verifier: command, CI check, log query, screenshot check, or review rubric used to prove progress.
  • Automation: scheduled or background Codex task that runs from a prompt.
  • Idempotency: the loop can run again without duplicating issues, branches, comments, or tasks.
  • Owner gate: a required human approval for high-risk paths.
  • Fresh review: review performed outside the implementing loop's working context.

Reach the end and this star joins your charted sky.