mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Preserve remote session identity across recovery
This commit is contained in:
@@ -25,6 +25,9 @@ mod connection;
|
||||
const CODE_MODE_HOST_PATH_ENV: &str = "CODEX_CODE_MODE_HOST_PATH";
|
||||
|
||||
/// Creates code-mode sessions backed by one lazily spawned process host.
|
||||
///
|
||||
/// All sessions created by one provider share that host. Callers that want one
|
||||
/// sidecar across multiple Codex threads must share the provider instance.
|
||||
pub struct ProcessOwnedCodeModeSessionProvider {
|
||||
host_program: PathBuf,
|
||||
process_host: StdMutex<Option<Arc<OwnedProcessHost>>>,
|
||||
@@ -139,6 +142,7 @@ enum SessionState {
|
||||
pub struct ProcessOwnedCodeModeSession {
|
||||
process_host: Arc<OwnedProcessHost>,
|
||||
session_id: SessionId,
|
||||
next_cell_id: AtomicU64,
|
||||
delegate: Arc<dyn CodeModeSessionDelegate>,
|
||||
state: StdMutex<SessionState>,
|
||||
transition_permit: Semaphore,
|
||||
@@ -146,12 +150,8 @@ pub struct ProcessOwnedCodeModeSession {
|
||||
|
||||
impl ProcessOwnedCodeModeSession {
|
||||
pub fn new() -> Self {
|
||||
Self::with_delegate(Arc::new(NoopCodeModeSessionDelegate))
|
||||
}
|
||||
|
||||
pub fn with_delegate(delegate: Arc<dyn CodeModeSessionDelegate>) -> Self {
|
||||
Self::with_process_host(
|
||||
delegate,
|
||||
Arc::new(NoopCodeModeSessionDelegate),
|
||||
Arc::new(OwnedProcessHost::new(default_host_program())),
|
||||
)
|
||||
}
|
||||
@@ -164,6 +164,7 @@ impl ProcessOwnedCodeModeSession {
|
||||
Self {
|
||||
process_host,
|
||||
session_id,
|
||||
next_cell_id: AtomicU64::new(1),
|
||||
delegate,
|
||||
state: StdMutex::new(SessionState::New),
|
||||
transition_permit: Semaphore::new(/*permits*/ 1),
|
||||
@@ -214,12 +215,21 @@ impl ProcessOwnedCodeModeSession {
|
||||
}
|
||||
|
||||
pub async fn execute(&self, request: ExecuteRequest) -> Result<StartedCell, String> {
|
||||
let cell_id = self.allocate_cell_id();
|
||||
self.connection()
|
||||
.await?
|
||||
.execute(self.session_id.clone(), request)
|
||||
.execute(self.session_id.clone(), cell_id, request)
|
||||
.await
|
||||
}
|
||||
|
||||
fn allocate_cell_id(&self) -> CellId {
|
||||
CellId::new(
|
||||
self.next_cell_id
|
||||
.fetch_add(1, Ordering::Relaxed)
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn wait(&self, request: WaitRequest) -> Result<WaitOutcome, String> {
|
||||
self.connection()
|
||||
.await?
|
||||
|
||||
@@ -203,6 +203,7 @@ impl Connection {
|
||||
pub(super) async fn execute(
|
||||
&self,
|
||||
session_id: SessionId,
|
||||
cell_id: CellId,
|
||||
request: ExecuteRequest,
|
||||
) -> Result<StartedCell, String> {
|
||||
let request = request
|
||||
@@ -223,6 +224,7 @@ impl Connection {
|
||||
id,
|
||||
request: HostRequest::Execute {
|
||||
session_id,
|
||||
cell_id: (&cell_id).into(),
|
||||
request,
|
||||
},
|
||||
})
|
||||
@@ -244,10 +246,11 @@ impl Connection {
|
||||
}
|
||||
};
|
||||
match response {
|
||||
HostResponse::ExecutionStarted { cell_id } => Ok(StartedCell::from_result_receiver(
|
||||
cell_id.into(),
|
||||
initial_rx,
|
||||
)),
|
||||
HostResponse::ExecutionStarted {
|
||||
cell_id: started_cell_id,
|
||||
} if started_cell_id.as_str() == cell_id.as_str() => {
|
||||
Ok(StartedCell::from_result_receiver(cell_id, initial_rx))
|
||||
}
|
||||
_ => {
|
||||
self.state.initial_responses.lock().await.remove(&id);
|
||||
Err("code-mode host returned an invalid execute response".to_string())
|
||||
|
||||
@@ -97,18 +97,26 @@ impl InProcessCodeModeSession {
|
||||
)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
let cell_id = protocol_cell_id(&started.cell_id);
|
||||
let response_cell_id = cell_id.clone();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
tokio::spawn(async move {
|
||||
let response = started
|
||||
.initial_event()
|
||||
.await
|
||||
.map_err(|error| error.to_string())
|
||||
.and_then(|event| runtime_response(&response_cell_id, event));
|
||||
let _ = response_tx.send(response);
|
||||
});
|
||||
Ok(StartedCell::from_result_receiver(cell_id, response_rx))
|
||||
Ok(protocol_started_cell(started))
|
||||
}
|
||||
|
||||
/// Executes a cell using an ID assigned by a remote-session client.
|
||||
pub async fn execute_with_cell_id(
|
||||
&self,
|
||||
cell_id: CellId,
|
||||
request: ExecuteRequest,
|
||||
) -> Result<StartedCell, String> {
|
||||
let yield_time_ms = request.yield_time_ms.unwrap_or(DEFAULT_EXEC_YIELD_TIME_MS);
|
||||
let started = self
|
||||
.runtime
|
||||
.execute_with_cell_id(
|
||||
runtime_cell_id(&cell_id),
|
||||
runtime_request(request),
|
||||
runtime::ObserveMode::YieldAfter(Duration::from_millis(yield_time_ms)),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(protocol_started_cell(started))
|
||||
}
|
||||
|
||||
pub async fn execute_to_pending(
|
||||
@@ -319,6 +327,21 @@ fn protocol_cell_id(cell_id: &runtime::CellId) -> CellId {
|
||||
CellId::new(cell_id.as_str().to_string())
|
||||
}
|
||||
|
||||
fn protocol_started_cell(started: runtime::StartedCell) -> StartedCell {
|
||||
let cell_id = protocol_cell_id(&started.cell_id);
|
||||
let response_cell_id = cell_id.clone();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
tokio::spawn(async move {
|
||||
let response = started
|
||||
.initial_event()
|
||||
.await
|
||||
.map_err(|error| error.to_string())
|
||||
.and_then(|event| runtime_response(&response_cell_id, event));
|
||||
let _ = response_tx.send(response);
|
||||
});
|
||||
StartedCell::from_result_receiver(cell_id, response_rx)
|
||||
}
|
||||
|
||||
fn pending_outcome(
|
||||
cell_id: &CellId,
|
||||
event: runtime::CellEvent,
|
||||
|
||||
@@ -67,11 +67,20 @@ impl<D: SessionRuntimeDelegate> SessionRuntime<D> {
|
||||
&self,
|
||||
request: CreateCellRequest,
|
||||
initial_observe_mode: ObserveMode,
|
||||
) -> Result<StartedCell, Error> {
|
||||
self.execute_with_cell_id(self.allocate_cell_id(), request, initial_observe_mode)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn execute_with_cell_id(
|
||||
&self,
|
||||
cell_id: CellId,
|
||||
request: CreateCellRequest,
|
||||
initial_observe_mode: ObserveMode,
|
||||
) -> Result<StartedCell, Error> {
|
||||
if self.inner.shutdown_token.is_cancelled() {
|
||||
return Err(Error::ShuttingDown);
|
||||
}
|
||||
let cell_id = self.allocate_cell_id();
|
||||
let initial_event = self
|
||||
.start_cell(cell_id.clone(), request, initial_observe_mode)
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user