Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/contributing/runtime-implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ The sandbox has two key directories that map to Claude Code's config levels (plu
├── codex-config/ ← CODEX_HOME (codex runtime)
│ Created by the sandbox image (codex will not start without it);
│ populated by CodexRuntime.Bootstrap (#6920, follow-up PR)
│ └── agents/<role>.toml Native sub-agent roles (skill personas plus default and explore), written only
│ when the ADR 0126 gate holds; runner-held digests, checked by the spawn guard (#6970)
│
├── claude-config/ ← CLAUDE_CONFIG_DIR (personal level)
│ ├── agents/
Expand Down Expand Up @@ -1099,7 +1101,9 @@ flowchart TB
files through the shell, and Codex 0.157.0 truncates a single exec at 10,000 tokens (head and
tail), so `codexNoSubagentNote` tells it to count lines with `awk 'END { print NR }'` (`wc -l`
misses a last line with no trailing newline) then `sed -n` a long `SKILL.md` in at most
200-line ranges, one per tool call (#7831).
200-line ranges, one per tool call (#7831). A native child runs with its role's
`developer_instructions` in place of the parent's, so every role file opens with
`codexChildNote`: the child contract pi gives every child, and the same rule.
- **AGENTS.md** — codex skips a project's own `AGENTS.md` while the project is untrusted
(`codex-rs/core/src/agents_md.rs`), but always loads `$CODEX_HOME/AGENTS.md` as user
instructions. The runner copies the repo's root `AGENTS.md` (or the injected org-level one) there
Expand Down Expand Up @@ -1258,7 +1262,7 @@ as the place its expected value comes from. There are two trustworthy places —
| hook adapter, auth script | **compile-time** — `go:embed`ed, so the digest is a literal in the run command | SHA-256, exit 97 |
| the shared hook scripts | **compile-time** for the bytes, **runner-held** for which names — Bootstrap records the name → digest map for the set the harness enabled | SHA-256 per filename, plus an exhaustive directory check, exit 97 |
| `config.toml`, `hooks.json` | **runner-held** — rendered per run, so Bootstrap records what it uploaded outside the sandbox and Run injects it into the launch command (`codex_integrity.go`) | whole-file SHA-256, exit 98 |
| `agents/<role>.toml` (role files; none written yet) | **runner-held** — Bootstrap records name → digest in `RoleFiles`; Run exports it with `hooks.json`'s digest as `FULLSEND_CODEX_SPAWN_DIGESTS` | SHA-256 per filename plus an exhaustive `agents/` check, by the spawn guard before every spawn; exit 2 |
| `agents/<role>.toml` | **runner-held** — rendered per run from the skills' `sub-agents/*.md` plus the built-in `default` and `explore`; Bootstrap records one digest per role name (`RoleFiles`) and the run command exports them as `FULLSEND_CODEX_SPAWN_DIGESTS` | SHA-256 per file name plus an exhaustive directory check by the spawn guard before every spawn: a changed, missing or extra file denies the spawn (exit 2 with a reason) |
| `fullsend-manifest.json` | none — it is a file in the same agent-writable directory | information only; never authority |

The hook-script check makes codex **stricter than Claude Code and pi**, where the scripts stay
Expand Down Expand Up @@ -1422,8 +1426,11 @@ Two artefacts of the run are worth knowing about:
and local smoke runs.
- The rollout session files are archived as transcripts but not error-classified: only the tee'd
`exec --json` capture yields a verdict, which is what the runner's exit-code override reads.
- Sub-agent rosters are not wired, so `review`/`retro` are unsupported; `Bootstrap` appends a runtime
note telling the agent to execute sub-agent definitions itself, in order, as it does for pi.
- Sub-agent roles are generated (`agents/<role>.toml`, digest-guarded) for an agent with the `Agent`
tool under harness security whose parent model Codex resolves to collaboration V1 (ADR 0126), but
codex's multi-agent tools stay off (`[agents] enabled = false`), so `review`/`retro` are unsupported;
`Bootstrap` appends a runtime note telling the agent to execute sub-agent definitions itself, in
order, as it does for pi.

### Re-check on a `CODEX_VERSION` bump

Expand All @@ -1445,3 +1452,4 @@ Two artefacts of the run are worth knowing about:
| Project trust and `AGENTS.md` | the pinned untrusted entry must still stop codex recording its own trust level, the repo's `.codex/` layer must stay unloaded, and `$CODEX_HOME/AGENTS.md` must still load while the project is untrusted, or the bridge stops reaching the agent | `codex-rs/app-server/src/request_processors/thread_processor.rs` (trust write), `codex-rs/config/src/loader/mod.rs`, `codex-rs/core/src/agents_md.rs`, `codex-rs/codex-home/src/instructions/mod.rs` |
| JSONL event structs, rollout line types and rollout file naming | the stream parser and transcript extraction; a rollout line type missing from `codexRolloutEnvelopes` discards the whole transcript | `codex-rs/exec/src/exec_events.rs`, `codex-rs/history/src/rollout_payload.rs` (`RolloutItemWire`), `codex-rs/thread-store/src/local/helpers.rs` |
| The multi-agent tool names (`multi_agent_v1*`, `collaboration*`, the bare V1 `spawn_agent`), the block contract (exit 2 with non-empty stderr blocks; a timeout kills the handler and lets the call through), `agent_id` and `agent_type` in a child's payload, the spawn arguments, and what a child start rereads (`hooks.json` and the role file) | the spawn guard's matcher, pass-through set, policy, digest set and deadline rest on them; a renamed tool is denied (loud), but a payload without `agent_id`, a new model or effort argument, a changed block contract or a child that rereads another file turns a deny into a pass | `codex-rs/core/src/tools/handlers/multi_agents_spec.rs`, `codex-rs/core/src/tools/registry.rs` (`function_hook_tool_name`), `codex-rs/hooks/src/engine/command_runner.rs` (the timeout branch), `codex-rs/core/src/agent/role.rs` (the role file, read at spawn) |
| The bundled catalog's `multi_agent_version` entries, the version codex supplies to an entry that carries none, and the floor model (`codex debug models --bundled`; at 0.159.3 `gpt-5.6-luna` is the listed model that selects `v1`, and `gpt-5.5` carries no version, which the `multi_agent` feature, on by default, resolves to V1) | part two of the delegation gate and the child-model floor rest on them: a bump that moves the floor to V2, or drops it, turns native sub-agents off for every agent; a bump that turns `multi_agent` off by default, or gives the null entry a version, changes which parents delegate; and the fixture `internal/runtime/testdata/codex/models-bundled-0.159.3.json` must be regenerated, never edited | `codex-rs/models-manager/models.json` in the bumped tag; regenerate the fixture with the one-liner in `codex_personas_test.go`'s `TestCodexCatalog` comment and run `go test ./internal/runtime/ -run 'TestCodexCatalog\|TestCodexParentResolvesV1'` |
77 changes: 69 additions & 8 deletions internal/runtime/codex_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var claudeToolForCodex = map[string]string{

func (r CodexRuntime) codexHooksDir() string { return r.ConfigDir() + "/hooks" }

// codexRolesDir is where codex discovers sub-agent roles at launch (#6970).
func (r CodexRuntime) codexRolesDir() string { return r.ConfigDir() + "/agents" }

func (r CodexRuntime) codexManifestPath() string { return r.ConfigDir() + "/" + codexManifestFile }

func (r CodexRuntime) codexSessionsDir() string { return r.ConfigDir() + "/sessions" }
Expand Down Expand Up @@ -151,6 +154,25 @@ func (r CodexRuntime) Bootstrap(input BootstrapInput) error {
if !ok || repoInput.RepoDir() == "" {
return fmt.Errorf("codex bootstrap: the target repository's sandbox path is required")
}
// ADR 0126 gate. Part one: tools include Agent and harness security is
// on. Part two needs the pinned CLI's catalog: codex must resolve the
// model Run will launch to collaboration V1, the only version whose spawn
// tool the guard admits (its entry selects V1, or carries no version and
// codex's multi_agent feature, on by default, supplies V1). A V2 or
// unlisted parent keeps the multi-agent tools off and runs in one context,
// as every agent does today.
_, securityOn := input.(SandboxHooksBootstrap)
var catalog codexCatalog
Comment thread
dhshah13 marked this conversation as resolved.
Resolved
delegating := false
if codexDelegates(def, securityOn) {
if catalog, err = codexPreflightCatalog(sandboxName); err != nil {
return err
}
var reason string
if delegating, reason = codexParentResolvesV1(catalog, input.ParentModel(), def.Model); !delegating {
fmt.Fprintf(os.Stderr, "sub-agents: %s; native sub-agents need a parent Codex resolves to collaboration V1, running in one context\n", reason)
}
}
// The gateway provider is rendered only when the runner registered an
// inference.gateway block for this sandbox (PrepareGatewayRun).
gatewayBaseURL := codexGatewayRunFor(sandboxName)
Expand Down Expand Up @@ -273,6 +295,23 @@ func (r CodexRuntime) Bootstrap(input BootstrapInput) error {
return fmt.Errorf("writing %s: %w", codexHooksFile, err)
}
digests.HooksJSON = codexAssetSHA256(hooksJSON)
// The role files are agent-writable, so their digests join the
// runner-held set the spawn guard re-checks before every spawn.
// Codex's multi-agent tools stay off in config.toml until the
// dispatch note lands (#6970).
if delegating {
if err := codexExecOK(sandboxName, "mkdir -p "+shellQuote(r.codexRolesDir()), "creating codex roles dir"); err != nil {
return err
}
digests.RoleFiles = map[string]string{}
for _, role := range codexRoles(input.SkillDirs(), agentName) {
data := role.toml()
if err := uploadBytes(sandboxName, r.codexRolesDir()+"/"+role.Name+".toml", data); err != nil {
return fmt.Errorf("writing role %q: %w", role.Name, err)
}
digests.RoleFiles[role.Name] = codexAssetSHA256(data)
}
}
manifest.Hooks = codexHooksManifestFor(r.codexHooksDir(), hooks)
}

Expand Down Expand Up @@ -334,19 +373,21 @@ func codexDeveloperInstructions(agentName string, def *piAgentDef) string {
// single-context path deliberately instead of recording a failed dispatch.
// codex does have a spawn_agent tool, but fullsend wires no sub-agent roster
// for it in v1, the same position pi was in (#6527).
//
// The second paragraph is the chunked-read rule for Codex's exec truncation
// (10,000 tokens, head and tail, at 0.157.0). A single cat of a long SKILL.md
// drops the middle with no retry (#7831). Lines are counted with awk's NR, not
// `wc -l`: `wc -l` counts newline characters, so a file whose last line has no
// trailing newline reports one line too few and the last range would stop
// short of it.
const codexNoSubagentNote = "\n## Runtime note\n\n" +
"This agent runs on the codex runtime (FULLSEND_RUNTIME=codex). No fullsend sub-agent " +
"roster is available. When a skill says to dispatch sub-agents, execute each sub-agent " +
"definition yourself, in the listed order, with the same context package, and treat each " +
"output as that sub-agent's result.\n\n" +
"When reading a SKILL.md or other instruction file, do not cat it in one call: Codex " +
codexChunkedReadNote

// codexChunkedReadNote is the chunked-read rule for Codex's exec truncation
// (10,000 tokens, head and tail, at 0.157.0). A single cat of a long SKILL.md
// drops the middle with no retry (#7831). Lines are counted with awk's NR, not
// `wc -l`: `wc -l` counts newline characters, so a file whose last line has no
// trailing newline reports one line too few and the last range would stop
// short of it. codexNoSubagentNote and every role file (codexChildNote) end
// their runtime note with it.
const codexChunkedReadNote = "When reading a SKILL.md or other instruction file, do not cat it in one call: Codex " +
"truncates exec output (head and tail, dropping the middle). For each file, count its " +
"lines with `awk 'END { print NR }' <file>` (`wc -l` misses a last line with no newline), " +
"then `sed -n '<start>,<end>p'` in contiguous ranges of at most 200 lines, one range per " +
Expand Down Expand Up @@ -433,6 +474,26 @@ func codexPreflightVersion(sandboxName string) (string, error) {
return sanitizeOutput(number), nil
}

// codexPreflightCatalog dumps the catalog compiled into the pinned binary
// (offline, no CODEX_HOME needed) and parses it. Failure here is a broken
// image, reported before any iteration.
func codexPreflightCatalog(sandboxName string) (codexCatalog, error) {
const cmd = "codex debug models --bundled"
stdout, stderr, exitCode, err := sandbox.Exec(sandboxName, cmd, 30*time.Second)
if err != nil {
return nil, fmt.Errorf("codex preflight: %w", err)
}
if exitCode != 0 {
return nil, fmt.Errorf("codex preflight: `%s` exited %d: %s",
cmd, exitCode, strings.TrimSpace(sanitizeOutput(stderr)))
}
catalog, err := parseCodexCatalog([]byte(stdout))
if err != nil {
return nil, fmt.Errorf("codex preflight: `%s` printed no model catalog: %w", cmd, err)
}
return catalog, nil
}

// codexVersionPrefix is what `codex --version` puts before the number.
// Asserted at image build time too (images/sandbox/Containerfile compares the
// whole "codex-cli <pin>" string), so a change here shows up as a build
Expand Down
Loading
Loading