Skip to content

exploration: create Session as part of Codex::spawn() - #2291

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

bolinfest merged 1 commit into
mainfrom
pr2291

Conversation

@bolinfest

@bolinfest bolinfest commented Aug 14, 2025 •

Copy link
Copy Markdown
Collaborator

Historically, Codex::spawn() would create the instance of Codex and enforce, by construction, that Op::ConfigureSession was the first Op submitted via submit(). Then over in submission_loop(), it would handle the case for taking the parameters of Op::ConfigureSession and turning it into a Session.

This approach has two challenges from a state management perspective:

let mut sess: Option<Arc<Session>> = None;

  • The local sess variable in submission_loop() has to be mut and Option<Arc<Session>> because it is not invariant that a Session is present for the lifetime of the loop, so there is a lot of logic to deal with the case where sess is None (e.g., the send_no_session_event function and all of its callsites).
  • submission_loop() is written in such a way that Op::ConfigureSession could be observed multiple times, but in practice, it is only observed exactly once at the start of the loop.

In this PR, we try to simplify the state management by removing the Op::ConfigureSession enum variant and constructing the Session as part of Codex::spawn() so that it can be passed to submission_loop() as Arc<Session>. The original logic from the Op::ConfigureSession has largely been moved to the new Session::new() constructor.


Incidentally, I also noticed that the handling of Op::ConfigureSession can result in events being dispatched in addition to EventMsg::SessionConfigured, as an EventMsg::Error is created for every MCP initialization error, so it is important to preserve that behavior:

let events = std::iter::once(Event {
id: sub.id.clone(),
msg: EventMsg::SessionConfigured(SessionConfiguredEvent {
session_id,
model,
history_log_id,
history_entry_count,
}),
})
.chain(mcp_connection_errors.into_iter());
for event in events {
if let Err(e) = tx_event.send(event).await {
error!("failed to send event: {e:?}");
}
}
}

Though admittedly, I believe this does not play nice with #2264, as these error messages will likely be dispatched before the client has a chance to call addConversationListener, so we likely need to make it so newConversation automatically creates the subscription, but we must also guarantee that the "ack" from newConversation is returned before any other conversation-related notifications are sent so the client knows what conversation_id to match on.

@bolinfest
bolinfest marked this pull request as ready for review August 14, 2025 16:39
@bolinfest
bolinfest merged commit cf7a7e6 into main Aug 14, 2025
@bolinfest
bolinfest deleted the pr2291 branch August 14, 2025 16:55
@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.

2 participants