The Forge · 21 min mission
Loop Engineering in Codex
Use /goal, automations, AGENTS.md, skills, subagents, and review gates as one control system.
On this page
- Who this is for
- Put the stable rules in AGENTS.md
- Configure sandbox profiles before the loop runs
- Write the /goal as a contract
- Use a state file as the loop ledger
- Package repeated procedures as a skill
- Run a read-only discovery pass before editing
- Product-from-scratch loop
- Backlog and production-error automation
- Engineering-manager operating loop
- Use /review as the merge gate
- Verify the loop before scheduling it
- Limitations
- Glossary
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
/goalavailable. - 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.
| Primitive | Loop role | Use it for | Failure to avoid |
|---|---|---|---|
/goal | Active objective and completion condition | One feature slice or repair batch | Vague goal with no verifier |
| Automation | Scheduled or background cadence | Daily PR sweep, weekly docs drift, nightly error triage | Write access before triage is proven safe |
AGENTS.md | Repo policy and review rules | Test commands, protected paths, severity rules | Huge instruction file that hides the real constraints |
| Skill | Reusable procedure and assets | Backlog loop, release loop, incident loop, docs loop | Skill overlaps with AGENTS.md and triggers too broadly |
| Subagent | Parallel worker or fresh reviewer | Security pass, docs pass, test adequacy, multi-package triage | No consolidation rule |
| Sandbox + approval | Permission boundary | Read-only discovery, workspace-write implementation, owner gates | Unattended write access on high-risk paths |
/review | Diff review before merge | P0/P1 check, missing tests, risky behavior changes | Same agent self-approves its own work |
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.
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.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.
## 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.
[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.
/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.
# 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.
---
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 batchRun 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.
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.
| Loop | Input | Codex surface | Verifier | Output |
|---|---|---|---|---|
| Brief loop | user notes, support tickets, repo constraints | manual /goal or read-only task | brief has user, first-value moment, non-goals | product brief |
| Architecture loop | brief, repo map, data model | manual /goal | files in scope/out of scope are explicit | slice plan |
| Implementation loop | slice plan, state file | manual /goal | tests, lint, build, browser check | feature branch |
| Review loop | diff, state file, original goal | /review or subagent | no P0/P1 finding | merge decision |
| Release loop | diff, docs, monitoring, rollback note | automation or manual run | release checklist has owner and post-deploy check | release note |
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.
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.
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 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 diffVerify the loop before scheduling it
Run the loop manually once before creating an automation:
- Run discovery with the read-only profile.
- Confirm the state file updates without edits.
- Run one supervised implementation pass with
loop_workspace. - Run the verifier yourself for high-risk work.
- Run
/reviewagainst the diff. - Confirm the state file records evidence and open risk.
- 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.
| Symptom | Likely cause | Fix |
|---|---|---|
| Automation creates duplicate issues | No idempotency check | Read existing issues and state file before opening new work |
| Codex edits before understanding the incident | Discovery profile has write access | Start with loop_readonly and require approval before patching |
| Goal never completes | Stop condition is subjective | Replace "good enough" with tests, CI, log query, screenshot, or review rubric |
| Diff touches protected paths | Risk gate is only prose | Add owner gate to AGENTS.md and require review before merge |
| Review finds missing tests late | Verifier was not named in the contract | Put the exact verifier in the state file and goal prompt |
| Agent weakens the check to pass | Verifier is editable by the same loop | Require fresh review for test deletion, assertion weakening, log suppression, and catch-all changes |
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.