Reliability is mostly prepared before the agent starts
The most consequential Codex configuration is often the repository. An agent working in a project with a deterministic setup, focused tests, clear ownership, small interfaces, and documented constraints receives frequent ground truth. The same model in a repository with stale instructions, environment drift, flaky tests, and broad credentials must infer what the organization never encoded.
SWE-agent demonstrated that the agent-computer interface itself changes software-task performance [8]. Anthropic’s production guidance similarly emphasizes simple composable patterns, environmental feedback, clear tool interfaces, and sandboxed testing rather than complexity for its own sake [9]. The lesson is vendor-independent: prepare the control loop.
This guide models acceptance as
where a proposed change
Stage 0: make the repository agent-ready
Agent readiness is ordinary engineering quality made explicit.
Reproducible bootstrap
A clean checkout should reach a working development state through one documented entry point. Pin runtimes and dependencies; separate installation from tests; define required services; provide deterministic fixtures; avoid hidden global tools. Cloud execution makes missing setup visible because each task begins in a configured environment [5]. Local work benefits equally.
Test the bootstrap in CI from an empty cache. A setup that works only on a long-lived laptop encodes unobserved state.
Fast discriminating checks
Provide a ladder from cheap to expensive:
- syntax and formatting;
- static analysis and types;
- focused unit tests;
- component and contract tests;
- integration tests with isolated services;
- end-to-end or visual checks;
- security, performance, migration, and deployment checks.
Fast feedback lets an agent update its plan before a mistake diffuses. Expensive checks remain necessary where their additional information exceeds cost.
Treat flaky checks as noisy sensors
A flaky test does not become ground truth because an agent can rerun it quickly. Let a test have latent pass probability
For a suspected flaky check, isolate environmental variables, seed randomness, capture timing and resource state, and compare baseline with treatment under the same repetition design. Use a binomial or beta-binomial model when a probability estimate is useful, but investigate clustered causes rather than assuming independent trials. A pass rate can hide failures concentrated on one platform or load condition.
Do not let the agent delete or weaken a flaky test inside an unrelated feature task. Quarantine can restore delivery flow only when accompanied by ownership, an issue, retained signal, and a deadline. The correct repair often belongs to the test harness or product race condition, not the assertion line.
Make flakiness visible in the evidence bundle. “Passed after three retries” and “passed first run from clean state” are different observations. A stop hook that merely demands green output can incentivize repeated sampling; a good gate evaluates the run policy as well as the final status.
Protected control files
Identify tests, schemas, generated-code sources, CI policy, migration records, release configuration, and security settings. Decide which an implementation agent may edit, which require a different task, and which are immutable. Filesystem protection should enforce that classification; an instruction alone is not a boundary.
Observable ownership
Map modules, interfaces, code owners, and deployment units. When a change crosses ownership, the task contract should request additional review rather than letting scope expand silently.
NIST’s Secure Software Development Framework treats preparation, protection, production, and vulnerability response as lifecycle practices [10]. Agent readiness belongs inside that lifecycle, not as an exception to it.
Stage 1: write a task contract
“Improve authentication” is a topic. A delegable task states the desired observable transition.
A task contract should contain:
- initial state: branch, commit, environment, known failures, and dirty files;
- objective: user-visible or system-level postcondition;
- scope: permitted modules and explicitly excluded work;
- constraints: compatibility, performance, security, style, and migration rules;
- acceptance: commands and human judgments required;
- authority: filesystem, network, service, and credential capabilities;
- budget: time, tokens/credits, retries, and review deadline;
- stop conditions: completion, blocker, ambiguity, or risk threshold;
- deliverables: diff, tests, evidence, documentation, and unresolved questions.
Use examples to disambiguate behavior, but avoid prescribing an implementation unless architecture is itself a requirement. Preserve the original contract. Follow-up prompts can amend it through a visible change log.
For uncertainty, instruct the agent to ask before choosing an externally visible semantic. For ordinary local decisions, provide principles and let it proceed. Too many checkpoints create approval fatigue; too few turn ambiguity into rework.
Stage 2: layer instructions without pretending they enforce policy
AGENTS.md is an open Markdown convention for build commands, tests, style, and agent-specific repository context [7]. Codex uses layered AGENTS.md files so instructions can become more specific deeper in a repository. Effective files emphasize facts that are difficult to infer:
- canonical setup and test commands;
- generated-file ownership;
- architecture invariants and rationale;
- forbidden compatibility breaks;
- where secrets and production operations are handled;
- expected validation by change type;
- links to longer references.
Do not dump the entire repository map into instructions. Large always-loaded context competes with the task and becomes stale. Put repeatable procedures into focused skills; current Codex skills package instructions, references, and optional scripts and load full content when selected [4].
Use three separate control layers:
- Instructions shape what the model proposes.
- Policy and sandboxing constrain what can execute.
- Evidence and review decide what can be accepted.
An instruction such as “never read .env” is useful defense in depth. A filesystem read prohibition and absence of the secret are the control.
Stage 3: choose a surface and reconstructable environment
Codex CLI fits work where local state and fast steering matter; the IDE reduces context-description cost; cloud tasks support isolated, parallel, background execution [1, 5]. Choose based on state locality and reproducibility, not habit.
Record an environment manifest:
- OS and architecture;
- runtime and package-manager versions;
- dependency lock hashes;
- required services and fixtures;
- environment variable names without secret values;
- setup command and image identifier;
- network policy;
- test data version.
Cloud and local parity need not be bit-identical. They must be equivalent for the properties being tested. If a hardware-specific issue cannot reproduce in cloud, keep execution local and isolate through a disposable machine or container.
Treat caches as performance optimizations whose correctness effect is tested. A cached container can hide a missing dependency; a clean build should remain part of acceptance.
Stage 4: define the minimum permission profile
Current Codex documentation separates sandbox mode—what commands can technically reach—from approval policy—when boundary crossing needs authorization [2]. The local default generally permits workspace edits and routine commands with networking disabled, while cloud agent phases are offline unless enabled.
Start from required capabilities:
| Task | Filesystem | Network | Credentials | External writes |
|---|---|---|---|---|
| Explain code | read-only project | none | none | none |
| Focused fix | workspace write | none | none | none |
| Dependency update | workspace write | allowlisted registry | package read | none |
| PR creation | workspace write | source host | branch-scoped token | draft PR |
| Migration rehearsal | disposable environment | required services | test-only | test data only |
Do not grant production authority because the eventual workflow may need it. Split implementation from release. Require a new task and principal for deployment.
Protect repository metadata and agent configuration from self-modification unless the task explicitly owns them. Log permission escalations with reason, scope, and duration. Revoke temporary capabilities after use.
Stage 5: establish a baseline before editing
Before a substantive change, Codex should inspect applicable instructions, repository status, and the owning code path, then run the narrowest existing check that captures the behavior. Record whether baseline tests already fail.
This prevents three common errors:
- attributing an existing failure to the patch;
- “fixing” the wrong layer because the call path was guessed;
- broad exploration consuming context without producing a falsifiable hypothesis.
The initial plan should state one current hypothesis and one check capable of disproving it. Plans are navigation aids, not commitments. Update them when evidence changes and preserve why.
Stage 6: edit in small falsifiable increments
The ideal first edit is the smallest change that lets a nearby check distinguish the hypothesis. After each behavioral edit:
- run the focused check;
- inspect unexpected output;
- either repair the same slice or revise the hypothesis;
- only then widen scope.
This is slower than generating a complete patch in one turn when the model is right. It is faster in expectation when uncertainty is high because it shortens error propagation.
Use version-control checkpoints before risky transformations. For bulk mechanical work, separate transformation from semantic changes and validate each class. Do not let the agent erase unrelated user edits to simplify its state.
Stage 7: use hooks for deterministic lifecycle gates
Current Codex hooks can run trusted commands at session, prompt, tool, permission, compaction, subagent, and stop events. Non-managed command-hook definitions require review and are trusted by hash; changed hooks need renewed trust [3].
High-value uses include:
- scanning prompts for accidental secret inclusion;
- blocking protected-file edits;
- validating command arguments before execution;
- running formatters or focused tests after edits;
- injecting current branch or environment facts at session start;
- preventing completion while required checks are missing;
- exporting structured audit events.
Hooks are privileged code. Pin interpreters and dependencies, use absolute paths, sanitize JSON input, set timeouts, and decide failure semantics. A hook that fails open cannot enforce policy; one that fails closed can halt work during an outage. Match behavior to consequence.
Keep model-based auto-review separate from deterministic hooks. A reviewer agent can judge contextual risk; a command hook can enforce a hard rule. Use both where their error modes differ.
Stage 8: place verification gates by expected loss
Let gate
with additional value for information and regulatory requirements. The quantities are rarely known precisely; the inequality structures the decision.
Gate examples:
- after requirements: user confirms externally visible behavior;
- after data-model design: owner reviews compatibility and migration;
- after dependency change: provenance, license, and vulnerability checks;
- after implementation slice: focused behavior tests;
- before merging: clean full suite, static checks, security review;
- before deployment: staged rollout, backup, rollback, observability;
- after deployment: canary health and invariant checks.
Do not ask humans to approve low-level commands they cannot evaluate meaningfully. Present decisions with alternatives, evidence, and consequence. Human gates are strongest around intent, tradeoffs, and irreversible authority.
Tier the workflow by blast radius
One universal workflow either over-controls trivial edits or under-controls consequential ones. Classify changes before granting authority:
- Tier 0, observational: explanation, search, and read-only review. No repository mutation or external writes.
- Tier 1, reversible local: documentation, formatting, focused tests, and narrow code changes on a branch. Workspace writes with automated checks and ordinary review.
- Tier 2, cross-boundary: dependencies, public APIs, schemas, authentication, concurrency, or multi-service changes. Clean environments, specialized reviewers, compatibility tests, and explicit rollback.
- Tier 3, consequential: production data, deployment, credentials, billing, security controls, signing, or destructive migrations. Separate principal, staged execution, two-person authorization where appropriate, backups, canaries, and postcondition monitoring.
The agent may recommend a tier but should not lower it unilaterally. Escalate when scope expands or evidence reveals a wider blast radius. A nominal documentation task that asks for live customer data is no longer Tier 0.
Tie budgets and models to tiers without confusing capability with authority. Stronger reasoning can be economical for difficult Tier 2 analysis; Tier 3 still needs stricter controls. Conversely, a cheap model can safely handle high-volume Tier 1 transformations when deterministic validation is strong.
Stage 9: decompose subagents by information boundary
Codex subagents can isolate exploration, tests, logs, and specialized reviews from the main context, and can use different models and sandbox modes [6]. Delegate when a task is separable and the return can be compact.
Good worker roles:
- read-only code-path mapper;
- documentation/API verifier;
- focused test runner;
- security or concurrency reviewer;
- browser reproducer;
- migration-plan critic.
Poor decomposition creates two writers modifying the same interface without coordination. Give each worker an explicit input commit, ownership boundary, output schema, budget, and no more tools than needed. Have the parent verify claims against artifacts rather than accepting summaries as evidence.
Subagents consume additional tokens and can share model-family blind spots. Parallel agreement is not independent validation unless observations and methods differ.
Stage 10: produce an evidence bundle
At completion, require a bundle that another engineer can inspect without replaying the conversation:
- task contract and amendments;
- starting and final commits plus full diff;
- model, Codex version, configuration profile, and date;
- environment manifest;
- permission grants and network/service use;
- exact commands and exit statuses;
- focused and full test results;
- screenshots or traces for UI/runtime behavior;
- dependency and migration changes;
- failed attempts and unresolved risk;
- rollback or recovery procedure;
- recommended reviewers.
Logs are not automatically evidence. Associate each artifact with a claim. “Unit command exited zero” supports a bounded statement; it does not prove production correctness.
Stage 11: review independently and from a clean state
Review intent before style. Confirm that the patch solves the contract, then examine architecture, security, tests, operations, and maintainability. Use an independent reviewer context so the generator’s rationale does not anchor every judgment.
Re-run required checks from a clean checkout or declared environment. Verify generated files derive from sources. Inspect changed tests and configuration more skeptically than ordinary implementation because they define what “passing” means.
Reject false precision. If one platform, load level, or failure path was not tested, record it. Acceptance can be conditional on staged observation.
Stage 12: close the loop after merge
Production outcomes update the workflow. Track defects, reverts, review rounds, missed requirements, expensive prompts, and checks that caught real issues. Convert recurring facts into concise instructions, recurring procedures into skills, and recurring hard constraints into policy or hooks.
DORA’s capabilities framing emphasizes that AI amplifies the surrounding organizational system [11]. A weak process can generate weak changes faster. A learning process uses every agent run to improve repository observability and control.
Audit instructions and allow rules periodically. Models, tools, and command semantics change. Remove exceptions that no longer have owners. Test disaster recovery without the agent service.
A compact production checklist
Before delegation:
- reproducible setup and baseline;
- task contract and scoped authority;
- applicable AGENTS.md verified;
- permission profile and rollback;
- known acceptance checks.
Before acceptance:
- focused and clean full validation;
- protected tests/policy unchanged or separately reviewed;
- evidence bundle complete;
- independent review proportional to consequence;
- residual uncertainty and rollback explicit.
After acceptance:
- monitor outcomes;
- record defects and review cost;
- update controls at the correct layer;
- expire temporary authority and state.
Model capability is leverage; workflow design sets the fulcrum
Reliable Codex operation does not require turning every repository into a formal-methods project. It requires honest boundaries: what the agent knows, what it can change, what was measured, and who accepts the remainder.
Good workflows make the easy path observable and reversible. They let Codex act quickly inside a small trusted region, stop at meaningful uncertainty, and return evidence rather than confidence. As models change, that architecture preserves the organization’s definition of correct work.