# Global Codex instructions
This file is the user-wide installation template for the named `planner`/`advisor` routing policy. Install it as `$CODEX_HOME/AGENTS.md`; this repository does not duplicate it as a repository-root `AGENTS.md`.
## Native named-agent dispatch
This is Codex's default routing for non-trivial implementation work when no portable skill defines its own orchestration. A portable skill such as `pr-loop` follows its own `SKILL.md` contract instead and may use whichever native independent-subagent mechanism the active runtime provides, without requiring `.codex/agents` or a fixed named agent.
`planner` and `advisor` must be invoked through Codex's native multi-agent tools. Do not invoke them through `codex exec`, nested Codex CLI processes, shell wrappers, copied prompts, generic agents, or simulations.
Invoke both named roles in a fresh child context. In MultiAgentV2, set `fork_turns: "none"`; in MultiAgentV1, use `fork_context: false` or omit `fork_context`. Planner correctness comes from the explicit context packet below rather than inherited parent history; advisor independence comes from a fresh context plus task-specific primary evidence. This keeps one semantic isolation contract across native runtimes without depending on each runtime's parameter names or default history-inheritance behavior.
Treat the installed agent TOML as the source of truth for the named role, model, and requested sandbox default. Reasoning effort is deliberately selected per native dispatch rather than pinned in the agent definition. A successful native dispatch to the requested named role is sufficient; the runtime does not need to echo those configuration values back to the parent. A broader effective sandbox such as `workspace-write` does not by itself invalidate the invocation when the named-agent mutation guard below can be established: `planner` and `advisor` remain behaviorally read-only and must not modify files. Treat the invocation as `unsupported` only when available runtime evidence explicitly shows that native named-role dispatch is unavailable, a generic or different agent was used, the configured model was overridden, an explicitly requested per-dispatch reasoning effort was overridden incompatibly, the requested fresh-context isolation was not honored, or a writable invocation cannot be guarded because the workspace is not a Git worktree. Missing runtime telemetry, an adaptively selected reasoning effort, or a writable effective sandbox alone is not evidence of a mismatch.
Reasoning effort for `planner` and `advisor` is adaptive by dispatch policy. Their TOML files intentionally omit `model_reasoning_effort`. Before each spawn, explicitly select and pass the lowest adequate supported effort for the task instead of relying on `[agents]` defaults or parent-effort inheritance: use `medium` for routine non-trivial planning or review, `high` for complex or cross-cutting work, and `xhigh` only for unusually demanding work.
Implementation is owned by the top-level main agent. Do not delegate implementation to named or generic worker subagents. If native named-role dispatch is explicitly unavailable or incompatible with the configured role, report `unsupported` rather than silently omitting, downgrading, or simulating a phase.
### Planner context handoff
Every planner dispatch must include a context packet that preserves the user's intent without depending on inherited conversation history. Include:
- `USER REQUEST`: the user's actual request with minimal paraphrasing; prefer verbatim wording when practical.
- `PRIOR DECISIONS`: decisions already settled with the user. Do not reopen them without a concrete conflict or new evidence.
- `TASK CONTEXT`: relevant repository state, existing implementation, architecture, and other facts needed to plan.
- `NON-NEGOTIABLE CONSTRAINTS`: user and project constraints, compatibility, security, migration, operational requirements, and explicit exclusions.
- `OPEN QUESTIONS`: only genuinely unresolved material decisions.
Keep settled decisions separate from open questions. The explicit packet is the authoritative planning handoff; the fresh child context ensures planner correctness does not depend on parent-history inheritance.
### Named-agent mutation guard
For every `planner` or `advisor` invocation in a Git worktree, immediately before dispatch record a Git-visible baseline: `HEAD` when it exists (otherwise an explicit unborn-`HEAD` sentinel), index diff, tracked worktree diff, and every non-ignored untracked path with a content digest. Compare the same state immediately after return. Any persistent Git-visible mutation introduced during the invocation invalidates the result; preserve changes that already existed in the baseline. If available runtime output or telemetry explicitly shows a mutating action, including a transient edit that was restored before return, invalidate the result even when the post-dispatch baseline matches. Ignored and generated files are intentionally outside the persistent-state comparison. If the workspace is not a Git worktree, require an effective read-only sandbox; do not accept a writable effective sandbox without this guard. This guard establishes persistent Git-visible state integrity; it does not prove that a writable runtime performed no transient writes. The named agents remain behaviorally read-only and must not intentionally edit files. Do not describe this guard as runtime-enforced read-only or mutation-free execution.
## Model routing
This section applies only to the top-level main agent when no portable skill defines its own orchestration. A portable skill such as `pr-loop` follows its own `SKILL.md` contract instead; its planning, review, and feedback-analysis roles must not be routed through the named `planner` and `advisor` agents below. The named `planner` and `advisor` agents follow their own definitions and must not spawn or delegate to another subagent.
Use the main agent directly for simple questions and narrow, deterministic edits when planning overhead is not justified.
For non-trivial implementation tasks:
1. Apply the named-agent mutation guard, select and pass the planner reasoning effort according to the adaptive policy above, then invoke the configured `planner` in a fresh behaviorally read-only child context: use `fork_turns: "none"` with MultiAgentV2, or `fork_context: false`/omitted with MultiAgentV1. Supply the required planner context packet and obtain a decision-complete contract covering objective, scope, interfaces, constraints, and verification. Prefer the configured read-only sandbox, but accept the contract when native dispatch returns the requested planner result unless runtime evidence explicitly reports a generic/different-agent fallback, incompatible model override, failure to honor the explicitly requested reasoning effort or fresh-context isolation, or a writable invocation outside a Git worktree. A writable effective sandbox alone is not a failure condition when the guard can be established.
2. Route planner decisions before implementation. If the planner returns `STATUS: blocked`, obtain the smallest missing user decision and replan. If it returns `STATUS: ready`, preserve decisions already settled by the user. Require explicit user approval only when the contract introduces or changes a material user-facing or requirement-level decision that is not already settled, including observable product behavior, API or compatibility guarantees, architecture with meaningful trade-offs, destructive or irreversible migration, security posture, significant scope expansion, or a new requirement. The main agent may approve tactical and operational implementation details internally when they stay within the established contract. Do not invent requirements or reopen settled decisions without cause.
3. Pass the approved contract to the top-level main agent and implement it directly. Keep the main agent's reasoning effort at the user-selected or current-session setting; this routing policy must not set, override, or require a particular reasoning effort. Do not delegate implementation to a worker subagent.
4. Inspect the actual changes, preserve unrelated work, and run the relevant verification from the planner contract.
5. Invoke `advisor` only when an independent second opinion materially improves decision quality or confidence. Appropriate triggers include an explicit user request; unresolved architecture, security, API, compatibility, migration, or other cross-cutting trade-offs; multiple plausible approaches with meaningful consequences; verification failures whose diagnosis remains uncertain; or a high-risk/regression-prone implementation where independent review is warranted. Skip advisor for routine, low-risk changes when the main agent can validate the result directly.
6. When `advisor` is invoked, apply the named-agent mutation guard, select and pass its reasoning effort according to the adaptive policy above, and invoke it in a fresh behaviorally read-only child context: use `fork_turns: "none"` with MultiAgentV2, or `fork_context: false`/omitted with MultiAgentV1. Provide only the task-specific contract and primary evidence needed for independent review. For implementation review, provide the planner contract and primary evidence: the actual changed files or diff, relevant source and test configuration, and verification commands and results. Treat implementation decisions, summaries, and reported verification outcomes as orientation or claims rather than authoritative evidence; the advisor must independently inspect available primary evidence. Treat the advisor result as guidance rather than independent approval: apply supported `fix-first` findings in the main agent, return material `rethink` findings to `planner`, and surface any conflict where verified primary evidence contradicts the advice instead of following it mechanically. Rerun relevant verification after changes. Re-invoke `advisor` only when another independent opinion remains materially useful or the user explicitly requests it; do not loop solely to obtain `VERDICT: ship`. The verdict is an advisory classification, not a completion gate. An `unsupported` advisor result blocks completion only when the user explicitly required the consultation or a material risk remains unresolved and cannot be validated independently.
The planner and advisor TOML files intentionally omit `model_reasoning_effort` and configure read-only sandbox defaults. Select reasoning effort explicitly whenever either named agent is dispatched. Runtime sandbox broadening alone must not block execution when the mutation guard can be established. Every named-agent invocation must use the Git-visible mutation guard above before its result is accepted; outside a Git worktree, require an effective read-only sandbox instead.
For architecture, design evaluation, or technical advice without implementation, invoke `advisor` when its independent judgment is useful or when the user explicitly requests it. Apply the same named-agent mutation guard, select advisor effort adaptively, invoke it in a fresh child context using the active native runtime's isolation parameter, and keep the work behaviorally read-only.
Do not invoke a subagent when the main agent can complete a non-implementation task safely and efficiently without delegation.