Repository navigation
Description
Bug Description
With Codex ACP authenticated through the CODEX_AUTH_JSON secret (ChatGPT login), every Agent Server restart loses the Codex thread. _CodexAuthLifecycle.materialize creates a fresh tempfile.mkdtemp(prefix="openhands-codex-") and sets CODEX_HOME to it. Codex keeps its threads in $CODEX_HOME/sessions (plus session_index.jsonl, state_5.sqlite, …), and cleanup removes that directory. After a restart load_session cannot find the thread, fails with Internal error, and the SDK falls back to a fresh session (#5094). The conversation shows the full history, but the agent has no context.
Steps to Reproduce
- Agent Canvas
main, Agent Server 1.59.0, Codex ACP profile (codex-acp@2.2.2) withCODEX_AUTH_JSONpasted in Settings → Agents. - Run a turn: list files,
cat README.md,echo verify-order. Ask "What did echo print?" →verify-order(context kept within the process). - Restart the Agent Server (
control-openhands restart), reopen the conversation, ask "Which file did you display with cat earlier?".
Actual Behavior
ACP load_session(...6c5aa6cd) failed (Internal error); starting a fresh session, and the reply is I can't determine that from the conversation context available to me. The new openhands-codex-* directory (created at the restart) holds only the fresh session; the earlier directory with the original thread is gone.
Expected Behavior
A restarted Agent Server resumes the Codex thread. The Codex state directory should survive restarts (for example a per-conversation directory under the conversation's persistence directory, with only auth.json materialised from the secret), and a failed load should not silently drop context (#5094).
Additional Context
The same probably applies whenever the ACP subprocess is restarted for a conversation (idle eviction, drain-timeout restart), since each materialize call creates a new temporary CODEX_HOME. Found with the verify-openhands harness while checking ACP behaviors in Canvas.
This issue was created by an AI agent (OpenHands / smolpaws) on behalf of Engel Nyst.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Confirmed in the SDK: _CodexAuthLifecycle.materialize (openhands-sdk/openhands/sdk/agent/acp_file_credentials.py) creates a fresh tempfile.mkdtemp(prefix="openhands-codex-"), writes auth.json into it, sets CODEX_HOME, and _cleanup_runtime removes that directory on close/discard. On any Agent Server restart (or ACP subprocess re-materialization within a conversation) Codex therefore starts with an empty sessions/, so load_session fails and the run silently falls back to a fresh session.
The fix should reuse the repository's one authoritative durable location: the per-conversation Codex directory <persistence_dir>/acp/codex (ACPAgent._acp_file_secret_dir) — the same tree _isolate_acp_data_dir already points CODEX_HOME at, where the non-binding file-secret path seeds auth.json seed-if-absent, and whose spec documents that Codex's refresh-rewritten auth.json "must live on durable, writable storage". The versioned-binding refresh monitoring/sync (rotation write-back to the credential source, re-auth detection) must keep working unchanged.
Bounded scope: make the Codex versioned-binding CODEX_HOME durable across Agent Server restarts and ACP subprocess re-materialization within the same conversation, keeping the existing credential-sync behavior and secret-handling guarantees.
Non-goals: changing the silent fresh-session fallback after a failed load_session (tracked in #5094); cross-process/replica credential refresh locking (tracked in #4268); new user-facing settings; migrating or recovering threads lost in already-deleted temporary directories.
Acceptance Criteria
- With
CODEX_AUTH_JSONbound to a versioned credential source, the directoryCODEX_HOMEpoints to for a persisted conversation is the conversation's durable Codex directory (<persistence_dir>/acp/codex, falling back to the per-conversation workspace path when the conversation is not persisted) and nevertempfile.mkdtemp. - After an Agent Server process restart, the conversation resumes the same Codex thread: the persisted
acp_session_idloads successfully (noACP load_session ... failed; starting a fresh sessionlog line, no changed session id), and the agent answers a question about content produced in the pre-restart turn. - Restarting or re-materializing the ACP subprocess within one live conversation (e.g. the idle/drain-timeout restart) does not create a new empty
CODEX_HOMEthat loses the thread used by the conversation. - A token rotation performed by the CLI inside
$CODEX_HOME/auth.jsonis still detected and written back to the versioned credential source (credential_binding_replaceoutcomesuccess), and the materialized file/dir permissions remain 0600/0700. - When the credential source reports an invalid or re-auth-required credential, the failure still surfaces as
CredentialNeedsReauthenticationand the durable directory is not silently filled with stale credentials. - Conversation close/discard or deletion cleans up the runtime credentials per the existing per-conversation persistence lifecycle: no new orphaned runtime directory outside the conversation's persistence tree and no
openhands-codex-*leftovers under the system temp dir. -
auth.jsonand Codex state under the conversation'sacp/tree remain excluded from trajectory/download archives (the existing_TRAJECTORY_EXCLUDED_TOP_DIRS = {"acp"}carve-out still holds) and are never logged. - Automated validation covers at least: materialize followed by a simulated restart (a fresh agent instance loading the same persisted conversation) resuming the same session id, and the existing Codex credential-binding tests in
tests/sdk/agent/test_acp_file_credentials.pystill pass.
Activity
all-hands-bot commentedon Nov 4, 2026
New issue reports a distinct root cause: _CodexAuthLifecycle.materialize creates an ephemeral tempfile CODEX_HOME (openhands-codex-*) and cleanup deletes it, so Codex's threads/sessions directory is lost on every Agent Server restart and load_session falls back to a fresh session. That silent fallback symptom is exactly the subject of open issue #5094 ('ACP session/load failure silently starts a fresh session without restoring conversation context'), which #6453 explicitly cross-references and partly folds into its requested fix ('a failed load should not silently drop context'). However, #5094's requested fix is a fail-loudly/explicit-gate change for the fallback path (its own root cause was an orphaned process holding a thread writer lock), whereas #6453 proposes a different fix: a persistent per-conversation state directory with only auth.json materialised. The two are not the same report and #5094's fix would not resolve #6453, so this is overlapping-scope (same symptom, adjacent code path, likely discussion fragmentation) rather than a duplicate; do not auto-close. #2867 (closed) is the historical implementation of ACP session_id persistence/load_session resume and provides directly relevant background, and #5653 is an agent-server umbrella that is only loosely related.
Found 3 possible duplicate issues:
These may not be exact duplicates, but the scope appears to overlap enough that keeping discussion in one place may be more useful.
This comment was created by an AI assistant (OpenHands) on behalf of the repository maintainer.