iOS Assistant Runtime Extraction Audit
March 19, 2026 · View on GitHub
Scope
Audit source: openai/codex from GitHub (main, inspected at 903660edba6e1ecfd7c9b1782105be4ebf0e02a7).
Goal: identify the minimum reusable Codex pieces for an iOS-native embedded assistant runtime that keeps:
- ChatGPT authentication/session handling
- thread and turn lifecycle
- streaming event delivery
- approvals
- resumable conversations
- host-defined tool execution
while removing:
- CLI and TUI concerns
- shell and subprocess execution
- repo editing
- git/worktree flows
- patch generation/application
- desktop callback assumptions
Best Reuse Candidates
1. Authentication and Account State
Primary upstream references:
codex-rs/core/src/auth.rscodex-rs/core/src/auth/storage.rscodex-rs/login/src/server.rscodex-rs/login/src/device_code_auth.rscodex-rs/app-server-protocol/src/protocol/v2.rscodex-rs/app-server/src/message_processor.rscodex-rs/app-server/src/codex_message_processor.rs
Reusable ideas:
AuthManageris the real center of gravity, not the CLI login command.- Codex distinguishes three auth shapes:
ApiKeyChatgptChatgptAuthTokens
AuthManageralready models externally managed ChatGPT tokens throughExternalAuthRefresher, which is exactly the right adaptation seam for iOS.- Token refresh recovery is already state-machine based:
- guarded reload
- refresh-token flow
- externally managed token refresh callback
account/chatgptAuthTokens/refreshin app-server is a strong host/runtime boundary for externally managed mobile auth.
What ports cleanly:
- auth state model
- refresh lifecycle
- external-token bridge concept
- account metadata extraction (
email,plan_type,chatgpt_account_id) - logout/invalidation semantics
What does not port directly:
- localhost callback server in
login/src/server.rs - browser launch assumptions
- terminal-oriented device-code UX
- CLI/keyring storage choices as-is
2. Thread and Turn Lifecycle
Primary upstream references:
codex-rs/core/src/thread_manager.rscodex-rs/core/src/codex_thread.rscodex-rs/core/src/rollout/recorder.rscodex-rs/core/src/state/session.rscodex-rs/core/src/state/turn.rscodex-rs/app-server-protocol/src/protocol/v2.rscodex-rs/app-server/src/thread_state.rscodex-rs/app-server/src/thread_status.rscodex-rs/state/src/runtime/threads.rs
Reusable ideas:
- separate thread creation/resume/fork concepts
- per-thread runtime status
- per-turn pending state for approvals, user input, and tool results
- lightweight persisted thread metadata separate from active in-memory execution
- event-sourced rollout/history as the source for resume/read
What ports cleanly:
Thread/Turn/Itemlayering- resumable thread identity
- active turn pending maps for:
- approvals
- request-user-input
- request-permissions
- dynamic tool results
- loaded vs active vs waiting states
What must be simplified for iOS:
- no fork/worktree/repo-oriented thread source semantics
- no cwd/git coupling in thread identity
- no shell snapshot or background terminal cleanup paths
- no reliance on rollout files containing repo/file edit events
3. Streaming Event Model
Primary upstream references:
codex-rs/protocol/src/protocol.rscodex-rs/app-server-protocol/src/protocol/common.rscodex-rs/app-server-protocol/src/protocol/v2.rssdk/typescript/src/events.tssdk/typescript/src/thread.ts
Reusable ideas:
- Codex already exposes an event-driven runtime.
- The strongest portable pattern is:
thread/startedturn/started- item lifecycle events
- assistant text deltas
- final completion / failure events
- The TypeScript SDK shows the minimal consumer-facing shape better than the full CLI/runtime internals.
What ports cleanly:
- streamed assistant deltas
- structured completion and failure events
- turn-scoped usage
- thread status updates
- item-oriented lifecycle hooks for tools and approvals
What should be dropped or flattened:
- shell output delta events
- terminal interaction events
- file change diff events
- collab/sub-agent events
- review mode items
4. Approval Model
Primary upstream references:
codex-rs/protocol/src/approvals.rscodex-rs/protocol/src/request_permissions.rscodex-rs/protocol/src/request_user_input.rscodex-rs/app-server-protocol/src/protocol/v2.rscodex-rs/app-server/src/thread_status.rs
Reusable ideas:
- approvals are modeled as structured requests with stable IDs
- active thread state explicitly tracks “waiting on approval” and “waiting on user input”
- approvals are not tied to one UI; they are runtime pauses resolved by the host
- there is already a generic shape for user elicitation and dynamic tool callbacks
What ports cleanly:
- approval request ID
- structured reason/message payload
- approve/deny result envelope
- suspension/resume of an active turn while waiting for host input
What should not be ported:
- shell-specific review decisions
- execpolicy amendment mechanics
- filesystem/network sandbox amendment details
- guardian sub-agent approval reviewer flow
5. Host Integration Boundary
Primary upstream references:
codex-rs/app-server/README.mdcodex-rs/app-server-protocol/src/protocol/common.rscodex-rs/app-server-protocol/src/protocol/v2.rscodex-rs/app-server/src/message_processor.rscodex-rs/app-server/src/dynamic_tools.rscodex-rs/app-server/src/bespoke_event_handling.rs
Reusable ideas:
- app-server already treats the UI/app as a host that:
- authenticates
- presents approvals
- answers elicitation
- executes dynamic tools
- receives streamed notifications
DynamicToolCallParamsandDynamicToolCallResponseare a direct model for app-defined tools.ChatgptAuthTokensRefreshis a direct model for host-owned auth refresh.
This is the strongest extraction seam in the whole repository.
Modules to Reuse by Concept
Keep as conceptual source material
core/src/auth.rscore/src/auth/storage.rsapp-server-protocol/src/protocol/common.rsapp-server-protocol/src/protocol/v2.rsprotocol/src/protocol.rsprotocol/src/dynamic_tools.rsprotocol/src/approvals.rsthread_manager.rscodex_thread.rsrollout/recorder.rsstate/session.rsstate/turn.rsstate/src/runtime/threads.rs
Keep only as adaptation reference, not direct code
login/src/server.rslogin/src/device_code_auth.rs
Reason:
- the login flow is useful
- the transport assumptions are not
Fence off entirely
cli/tui/exec/exec-server/shell-command/shell-escalation/apply-patch/file-search/linux-sandbox/windows-sandbox-rs/- repo/git helpers and worktree flows
agent/controlsub-agent lifecycle- review-specific flows
iOS-Incompatible Dependencies and Assumptions
Desktop auth assumptions
- localhost callback HTTP server
- implicit browser launching from runtime
- device-code flow presented as terminal copy
- keyring/file fallback policy oriented around CLI home directories
Coding-agent assumptions
- shell approval payloads
- command execution events
- filesystem write diffs
- repo metadata in thread lifecycle
- sandbox escalation semantics designed for shell commands
Host/process assumptions
- JSON-RPC over stdio / websocket
- long-running process that owns all runtime behavior
- desktop filesystem visibility beyond app sandbox
Minimum Extraction Set for an iOS Runtime
This is the smallest viable set worth carrying over.
A. Auth/session core
Port:
- auth state model
- secure persistence abstraction
- refresh/invalidation lifecycle
- external auth refresher callback
Do not port:
- desktop login server implementation
B. Thread runtime core
Port:
- thread identity
- turn lifecycle
- active pending maps for approvals/tools
- lightweight thread persistence
Do not port:
- repo-specific source metadata beyond optional opaque host metadata
C. Streaming event core
Port:
- thread started/status changed
- turn started/completed/failed
- assistant text delta
- message committed
- tool call started/completed
- approval requested/resolved
Drop:
- shell/file-diff/terminal specific items
D. Tool bridge core
Port:
- dynamic tool registration shape
- invocation routing
- result envelope
Drop:
- any built-in tool implementations
E. Approval bridge core
Port:
- structured approval request
- host-mediated decision
- turn pause/resume semantics
Drop:
- execpolicy/network sandbox amendment payloads from the core iOS API
Recommended Extraction Strategy
- Re-implement the runtime in Swift rather than trying to embed Rust wholesale.
- Treat Codex as the protocol and lifecycle reference implementation.
- Preserve the host/runtime boundaries from app-server.
- Replace desktop auth with iOS-native auth UX and Keychain persistence.
- Keep the event vocabulary intentionally smaller than upstream Codex.
Bottom Line
The most reusable pieces of Codex for iOS are not the CLI or the agent executor. They are:
AuthManager’s ChatGPT auth and refresh model- the app-server host/runtime boundary
- the thread/turn/item lifecycle
- the streaming notification vocabulary
- the dynamic tool and approval callback shapes
That is the correct minimum extraction set for an iOS-native assistant runtime.