Import Codex sessions into `clai2` with their harness context, compacted history, encrypted reasoning, and sub-agents as tasks

Merged
DO
1mo ago
+1061-146

Why we make these changes

Douwe wants to move his Codex sessions into clai2 without losing anything. The importer read Codex rollouts as a flat list and threw away a lot of what Codex's model actually saw. I compared it with the open-source Codex source (rollout_reconstruction.rs, ResponseItem, context_manager, agent/control) and with his 305 local rollouts. Before this change, 7,580 of the 17,231 model-visible texts were missing from the import. Now 0 of 15,899 are missing, and 0 of 1,851 for sub-agents.

What the importer now keeps:

  • Harness context. Developer instructions, <environment_context>, AGENTS.md and the other context Codex added are imported as imported_context, as Claude Code's are. Codex's content_item_kinds set the kind; older versions fall back to tag prefixes. Typed prompts keep their images in order.
  • Compaction. The history before each compaction is now kept. An encrypted compaction item becomes CompactionPart(provider_name='openai-codex'), which acts as a wire boundary: openai-codex: models continue from the summary, as Codex did, and other models read the whole history, since they can't read the summary. The items compaction retained (messages, agent messages, tool outputs) follow it as context. A readable local summary is tagged as such.
  • OpenAI's encrypted round-trip data. Reasoning keeps its encrypted_content as the ThinkingPart signature. Text keeps phase, and function calls keep namespace and ids, all mirroring OpenAIResponsesModel.
  • Sub-agents. Every descendant thread becomes a finished task of the session, under ids derived from the session and thread ids. Default sub-agents are imported as resumable tasks named self. FINAL_ANSWER messages become <task_output> reports, and agent messages Codex encrypted are marked. Sub-agent sessions are no longer listed on their own.
  • Codex semantics. thread_rolled_back drops turns the way Codex counts them, keeping the session's opening context. Legacy inter_agent_communication records are read. Delivered-message records are skipped.
  • Framing. The opening and closing notes about the change of harness are shared with Claude Code's importer, through framed and final_answer in imported_history.py.
  • /resume listing. Sessions run by codex exec are no longer listed, but --resume-codex ID still imports them. These were 252 of 272 of Douwe's sessions.

Maintainer decisions:

  • Keep the pre-compaction history rather than match Codex exactly.
  • Hide exec sessions.

New public surface

None. These are clai2 internals: HistoryBuilder.rollback, imported_kind, framed, final_answer, Header.listed and codex_sessions.tasks. Two module constants change value, and both are waived in the API-compatibility allowlist:

  • codex_sessions.PROVIDER goes from 'openai' to 'openai-codex'.
  • codex_sessions.CONTEXT_PREFIXES gains entries.

User-visible behavior

Codex imports now carry the context, compaction history, reasoning and sub-agents listed above. codex exec runs are hidden from /resume.

Verification

  • tests/clai2 passes (4,565). The new tests cover context kinds, encrypted round-trip parts, compaction (encrypted, local and legacy), rollbacks, sub-agents as tasks, and the exec listing. codex_sessions.py and imported_history.py have 100% coverage.
  • Corpus check over all of Douwe's local rollouts: no errors, 0 model-visible texts missing, and all 33 sub-agents mapped to tasks.
  • I ran two live requests via openai-codex: on the account Codex used, one on a compacted session and one on a session with reasoning and namespaced calls. Both continued correctly. A custom tool call's ctc_ id is rejected as a function-call id, so custom calls carry no id.

What changes for existing users

Re-importing a Codex session that hasn't been continued in CLAI yet refreshes it with the fuller history. Copies already continued in CLAI are unchanged.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.