Skip to content

Resume the real Codex session instead of starting a new one - #927

Merged
selfcontained merged 1 commit into
mainfrom
agt_ceb691d66594/build-codex-resume-starts-new-session
Aug 10, 2026
Merged

Resume the real Codex session instead of starting a new one#927
selfcontained merged 1 commit into
mainfrom
agt_ceb691d66594/build-codex-resume-starts-new-session

Conversation

@selfcontained

Copy link
Copy Markdown
Owner

Problem

Restarting a Codex agent silently started a brand-new conversation. startAgent only ever minted a cli_session_id for Claude agents, so Codex agents kept a null id, shouldResume stayed false, and every restart fell through to the new-session branch — dropping the entire prior conversation.

Fix

Codex mints its own session id at launch, so Dispatch can't pre-assign one the way it does for Claude. Recover it from the rollout logs instead:

  • Rollouts already carry the [dispatch:<agentId>] tag Dispatch stamps into the launch guidance, so the newest tagged rollout under the Codex sessions dir identifies the session to resume.
  • codex resume <id> appends to that same rollout and keeps the same id (verified against codex-cli 0.147.0), so once discovered the id stays valid across any number of restarts.
  • The discovered id is persisted to cli_session_id through the same conditional-UPDATE race guard Claude uses, now extracted as claimCliSessionId.

Two related bugs in the resume command itself, both previously unreachable:

  • Passthrough args were dropped, so a full-access agent came back sandboxed after a restart (losing --dangerously-bypass-approvals-and-sandbox).
  • The session id now trails the flags, so codex binds it to SESSION_ID rather than sliding into the optional trailing [PROMPT] positional.

Rollout discovery moves into a shared codex-sessions module — the token harvester walked the same tree — and now honours CODEX_HOME like the CLI does, which also keeps the harvester test out of the real ~/.codex.

Validation

Live on an isolated dev stack with a real Codex agent: launched it, stopped it, restarted it.

  • Restart resolved cliSessionId to 019fec2f-… and reported Session resumed.
  • Running process: codex resume -c mcp_servers.dispatch.url=… -c mcp_servers.dispatch.bearer_token_env_var=… 019fec2f-… — MCP wiring intact, session id trailing.
  • Still exactly one rollout file for the agent afterwards, confirming it appended to the existing session rather than forking a new one.

Checks: pnpm run check clean, 2561 server unit tests pass, 178 E2E pass. No apps/web changes.

🤖 Generated with Claude Code

Restarting a Codex agent silently began a fresh conversation. `startAgent`
only ever minted a `cli_session_id` for Claude agents, so Codex agents kept
a null id, `shouldResume` stayed false, and the launch fell through to the
new-session branch on every restart.
Codex mints its own session id at launch, so it can't be pre-assigned the
way Claude's is. Recover it from the rollout logs instead: rollouts carry
the `[dispatch:<agentId>]` tag Dispatch stamps into the launch guidance, so
the newest tagged rollout under the Codex sessions dir identifies the
session to resume. `codex resume <id>` appends to that same rollout and
keeps the id, so it stays valid across any number of restarts.
Also fixes the resume command itself: passthrough args were dropped, which
meant a full-access agent came back sandboxed after a restart, and the
session id now trails the flags so codex binds it to SESSION_ID rather than
the optional PROMPT positional.
Rollout discovery moves to a shared `codex-sessions` module (the token
harvester walked the same tree) and now honours `CODEX_HOME` like the CLI
does, which also keeps the harvester test out of the real `~/.codex`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@selfcontained
selfcontained merged commit 16c3a98 into mainAug 10, 2026
1 check passed
@selfcontained
selfcontained deleted the agt_ceb691d66594/build-codex-resume-starts-new-session branch August 10, 2026 15:23
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@selfcontained