Skip to content

ref: full state refactor#4174

Merged
jif-oai merged 15 commits intomainfrom
jif/state11
Sep 25, 2025
Merged

ref: full state refactor#4174
jif-oai merged 15 commits intomainfrom
jif/state11

Conversation

@jif-oai
Copy link
Copy Markdown
Collaborator

@jif-oai jif-oai commented Sep 24, 2025

Current State Observations

  • Session currently holds many unrelated responsibilities (history, approval queues, task handles, rollout recorder, shell discovery, token tracking, etc.), making it hard to reason about ownership and lifetimes.
  • The anonymous State struct inside codex.rs mixes session-long data with turn-scoped queues and approval bookkeeping.
  • Turn execution (run_task) relies on ad-hoc local variables that should conceptually belong to a per-turn state object.
  • External modules (codex::compact, tests) frequently poke the raw Session.state mutex, which couples them to implementation details.
  • Interrupts, approvals, and rollout persistence all have bespoke cleanup paths, contributing to subtle bugs when a turn is aborted mid-flight.

Desired End State

  • Keep a slim Session object that acts as the orchestrator and façade. It should expose a focused API (submit, approvals, interrupts, event emission) without storing unrelated fields directly.
  • Introduce a state module that encapsulates all mutable data structures:
    • SessionState: session-persistent data (history, approved commands, token/rate-limit info, maybe user preferences).
    • ActiveTurn: metadata for the currently running turn (sub-id, task kind, abort handle) and an Arc<TurnState>.
    • TurnState: all turn-scoped pieces (pending inputs, approval waiters, diff tracker, review history, auto-compact flags, last agent message, outstanding tool call bookkeeping).
  • Group long-lived helpers/managers into a dedicated SessionServices struct so Session does not accumulate "random" fields.
  • Provide clear, lock-safe APIs so other modules never touch raw mutexes.
  • Ensure every turn creates/drops a TurnState and that interrupts/finishes delegate cleanup to it.

Comment thread codex-rs/core/src/codex.rs
Comment thread codex-rs/core/src/state/turn.rs
Comment thread codex-rs/core/src/codex.rs
Copy link
Copy Markdown
Collaborator

@pakrym-oai pakrym-oai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer us to keep turn state in task state (or make it the same) but non-blocking for this iteration.

# Conflicts:
#	codex-rs/core/src/codex.rs
#	codex-rs/core/src/lib.rs
@jif-oai jif-oai merged commit 250b244 into main Sep 25, 2025
19 checks passed
@jif-oai jif-oai deleted the jif/state11 branch September 25, 2025 10:16
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants