Skip to content

fix: parallelize logic in Session::new() - #2305

Merged
bolinfest merged 1 commit into
mainfrom
pr2305
Aug 14, 2025
Merged

bolinfest merged 1 commit into
mainfrom
pr2305

Conversation

@bolinfest

@bolinfest bolinfest commented Aug 14, 2025 •

Copy link
Copy Markdown
Collaborator

#2291 made it so that Session::new() is on the critical path to Codex::spawn(), which means it is on the hot path to CLI startup. This refactors Session::new() to run a number of async tasks in parallel that were previously run serially to try to reduce latency.

@github-actions

Copy link
Copy Markdown
Contributor

Summary
Parallelizes session initialization by joining independent async tasks (rollout recorder, MCP manager, shell discovery, history metadata) and refactors session_id handling. Also removes session_id from ConfigureSession and moves ModelClient construction to after final session_id is known.

  • Changed: codex-rs/core/src/codex.rs (multiple hunks; primary changes in Session::new).

Review
Nice win on startup latency and clearer sequencing. A few behavior and polish points to consider:

  • codex-rs/core/src/codex.rs @@ -327,14 +323,48: Good use of tokio::join!. If you intend to fail fast when any task errors, consider tokio::try_join! instead.
  • codex-rs/core/src/codex.rs @@ -347,15 +379,14: Behavior change risk—original comment said “fail if resume_path is specified and resume fails.” New code appears to proceed with a fresh Uuid::new_v4() on error. If resume is required, return an error here rather than silently starting a new session.
  • codex-rs/core/src/codex.rs @@ -256,2 +254,0: Removing session_id from ConfigureSession is a breaking change; confirm no downstream consumers rely on it, or call this out in the PR body.
  • codex-rs/core/src/codex.rs @@ -412 +440,10 (and related): Constructing ModelClient after finalizing session_id makes sense. Double‑check the ModelClient::new signature alignment across crates.
  • codex-rs/core/src/codex.rs @@ -342 +372,3: Ensure the if let Some(path) = resume_path.as_ref() branch handles resume‑specific errors (e.g., by aborting) if that’s still required.
  • codex-rs/core/src/codex.rs @@ -347,15 +379,14: Logging: prefer warn!("{message}") over warn!(message) for consistency and to avoid format‑string surprises.
  • codex-rs/core/src/codex.rs @@ -333–370 region: You clone saved_session.items; if ownership allows, avoid the clone (e.g., then(|| saved_session.items)).

Follow-up (PR body)

  • Please add motivation: “Parallelize session initialization to reduce startup latency by joining independent I/O-bound tasks; streamline session_id derivation; remove session_id from ConfigureSession.”

View workflow run

@dylan-hurd-oai dylan-hurd-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 comment about our error state, otherwise +++ for parallelization


RolloutResult {
session_id: Uuid::new_v4(),
rollout_recorder: None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Won't this just eventually panic when we call record_state_snapshot later on, which uses unwrap?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In record_state_snapshot(), assuming you are referring to:

        let recorder = {
            let guard = self.rollout.lock().unwrap();
            guard.as_ref().cloned()
        };

the unwrap() is not on the Option<RolloutRecorder>, but on Mutex<Option<RolloutRecorder>> (which is understandably confusing).

In that case, the unwrap() is with respect to getting access to the Mutex, not pulling the value from the Option.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ah, yep - makes sense!

.await;
sess.sandbox_policy.clone(),
)));
sess.record_conversation_items(&conversation_items).await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - thanks for consolidating

@bolinfest
bolinfest merged commit 8f11652 into main Aug 14, 2025
@bolinfest
bolinfest deleted the pr2305 branch August 14, 2025 20:30
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 14, 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.

3 participants