Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions codex-rs/code-mode/src/cell_actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use self::conversions::output_item;
use self::conversions::runtime_request;
use self::types::CellCommand;
pub(crate) use self::types::CellError;
#[cfg(test)]
pub(crate) use self::types::CellEvent;
pub(crate) use self::types::CellEvent as ActorEvent;
pub(crate) use self::types::CellEventFuture;
pub(crate) use self::types::CellHandle;
pub(crate) use self::types::CellHost;
Expand All @@ -30,15 +33,14 @@ pub(crate) use self::types::CellToolCall;
pub(crate) use self::types::CompletionCommit;
use self::types::CompletionDelivery;
use self::types::ObservationDelivery;
pub(crate) use self::types::ObserveMode;
use crate::runtime::PendingRuntimeMode;
use crate::runtime::RuntimeCommand;
use crate::runtime::RuntimeControlCommand;
use crate::runtime::RuntimeEvent;
use crate::runtime::spawn_runtime;
use crate::session_runtime::CellEvent;
use crate::session_runtime::CellExecutionPolicy;
use crate::session_runtime::CreateCellRequest as CellRequest;
use crate::session_runtime::ObserveMode;
use crate::session_runtime::OutputItem;
use crate::session_runtime::PendingFrontier;
use crate::session_runtime::PendingGeneration;
Expand Down Expand Up @@ -89,7 +91,7 @@ struct CellContext {

struct Observer {
mode: ObserveMode,
response_tx: oneshot::Sender<Result<CellEvent, CellError>>,
response_tx: oneshot::Sender<Result<ActorEvent, CellError>>,
}

async fn run_cell<H: CellHost>(
Expand Down Expand Up @@ -148,7 +150,7 @@ async fn run_cell<H: CellHost>(
finish_termination(
&cell_state,
observer.take().map(|observer| observer.response_tx),
CellEvent::Terminated {
ActorEvent::Terminated {
content_items: take_termination_content(
&mut pending_frontier,
pending_frontier_observed,
Expand Down Expand Up @@ -235,12 +237,12 @@ async fn run_cell<H: CellHost>(
{
let delivered = match send_cell_event(
response_tx,
CellEvent::Yielded {
ActorEvent::Yielded {
content_items: yielded_items,
},
) {
Ok(()) => true,
Err(CellEvent::Yielded { content_items }) => {
Err(ActorEvent::Yielded { content_items }) => {
pending_initial_yield_items = Some(content_items);
has_been_observed = false;
false
Expand All @@ -260,7 +262,7 @@ async fn run_cell<H: CellHost>(
if matches!(mode, ObserveMode::PendingFrontier)
&& let Some(frontier) = pending_frontier.as_ref()
{
if send_cell_event(response_tx, CellEvent::Pending(frontier.clone())).is_ok() {
if send_cell_event(response_tx, ActorEvent::Pending(frontier.clone())).is_ok() {
pending_frontier_observed = true;
}
continue;
Expand All @@ -285,7 +287,7 @@ async fn run_cell<H: CellHost>(
restore_undelivered_yield(
send_observer_event(
observer.take(),
CellEvent::Yielded {
ActorEvent::Yielded {
content_items: std::mem::take(&mut content_items),
},
),
Expand Down Expand Up @@ -317,7 +319,7 @@ async fn run_cell<H: CellHost>(
finish_termination(
&cell_state,
observer.take().map(|observer| observer.response_tx),
CellEvent::Terminated {
ActorEvent::Terminated {
content_items: termination_content_items,
},
);
Expand All @@ -330,7 +332,7 @@ async fn run_cell<H: CellHost>(
CallbackCompletion::DrainNotifications,
)
.await;
let event = CellEvent::Completed {
let event = ActorEvent::Completed {
content_items: take_termination_content(
&mut pending_frontier,
pending_frontier_observed,
Expand Down Expand Up @@ -362,7 +364,7 @@ async fn run_cell<H: CellHost>(
finish_termination(
&cell_state,
response_tx,
CellEvent::Terminated {
ActorEvent::Terminated {
content_items: rejected_completion_content(rejected_event),
},
);
Expand Down Expand Up @@ -408,7 +410,7 @@ async fn run_cell<H: CellHost>(
yield_timer = None;
if send_cell_event(
observer.response_tx,
CellEvent::Pending(frontier.clone()),
ActorEvent::Pending(frontier.clone()),
)
.is_ok()
{
Expand All @@ -433,7 +435,7 @@ async fn run_cell<H: CellHost>(
restore_undelivered_yield(
send_observer_event(
observer.take(),
CellEvent::Yielded {
ActorEvent::Yielded {
content_items: std::mem::take(&mut content_items),
},
),
Expand Down Expand Up @@ -491,7 +493,7 @@ async fn run_cell<H: CellHost>(
finish_termination(
&cell_state,
observer.take().map(|observer| observer.response_tx),
CellEvent::Terminated {
ActorEvent::Terminated {
content_items: termination_content_items,
},
);
Expand All @@ -504,7 +506,7 @@ async fn run_cell<H: CellHost>(
CallbackCompletion::DrainNotifications,
)
.await;
let event = CellEvent::Completed {
let event = ActorEvent::Completed {
content_items: std::mem::take(&mut content_items),
error_text,
};
Expand All @@ -531,7 +533,7 @@ async fn run_cell<H: CellHost>(
finish_termination(
&cell_state,
response_tx,
CellEvent::Terminated {
ActorEvent::Terminated {
content_items: rejected_completion_content(rejected_event),
},
);
Expand Down Expand Up @@ -568,28 +570,31 @@ async fn run_cell<H: CellHost>(
host.closed().await;
}

fn send_observer_event(observer: Option<Observer>, event: CellEvent) -> Result<(), CellEvent> {
fn send_observer_event(observer: Option<Observer>, event: ActorEvent) -> Result<(), ActorEvent> {
let Some(observer) = observer else {
return Err(event);
};
send_cell_event(observer.response_tx, event)
}

fn send_cell_event(
response_tx: oneshot::Sender<Result<CellEvent, CellError>>,
event: CellEvent,
) -> Result<(), CellEvent> {
response_tx: oneshot::Sender<Result<ActorEvent, CellError>>,
event: ActorEvent,
) -> Result<(), ActorEvent> {
match response_tx.send(Ok(event)) {
Ok(()) => Ok(()),
Err(Ok(event)) => Err(event),
Err(Err(error)) => panic!("cell event delivery returned an actor error: {error:?}"),
}
}

fn restore_undelivered_yield(delivery: Result<(), CellEvent>, content_items: &mut Vec<OutputItem>) {
fn restore_undelivered_yield(
delivery: Result<(), ActorEvent>,
content_items: &mut Vec<OutputItem>,
) {
match delivery {
Ok(()) => {}
Err(CellEvent::Yielded {
Err(ActorEvent::Yielded {
content_items: mut undelivered_items,
}) => {
undelivered_items.append(content_items);
Expand All @@ -599,9 +604,9 @@ fn restore_undelivered_yield(delivery: Result<(), CellEvent>, content_items: &mu
}
}

fn rejected_completion_content(event: Option<CellEvent>) -> Vec<OutputItem> {
fn rejected_completion_content(event: Option<ActorEvent>) -> Vec<OutputItem> {
match event {
Some(CellEvent::Completed { content_items, .. }) => content_items,
Some(ActorEvent::Completed { content_items, .. }) => content_items,
None => Vec::new(),
Some(event) => panic!("completion commit rejected an unexpected event: {event:?}"),
}
Expand Down Expand Up @@ -638,8 +643,8 @@ fn take_termination_content(

fn finish_termination(
cell_state: &CellState,
observer_tx: Option<oneshot::Sender<Result<CellEvent, CellError>>>,
event: CellEvent,
observer_tx: Option<oneshot::Sender<Result<ActorEvent, CellError>>>,
event: ActorEvent,
) {
if let Some(event) = cell_state.finish_termination(event)
&& let Some(observer_tx) = observer_tx
Expand Down
25 changes: 23 additions & 2 deletions codex-rs/code-mode/src/cell_actor/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use serde_json::Value as JsonValue;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;

use crate::session_runtime::CellEvent;
use crate::session_runtime::ObserveMode;
use crate::session_runtime::OutputItem;
use crate::session_runtime::PendingFrontier;
use crate::session_runtime::PendingGeneration;
use crate::session_runtime::ResumeOutcome;
use crate::session_runtime::ToolKind;
Expand All @@ -23,6 +23,27 @@ pub(crate) type CellEventFuture =
pub(crate) type ResumeFuture =
Pin<Box<dyn Future<Output = Result<ResumeOutcome, CellError>> + Send + 'static>>;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ObserveMode {
YieldAfter(Duration),
PendingFrontier,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum CellEvent {
Yielded {
content_items: Vec<OutputItem>,
},
Pending(PendingFrontier),
Completed {
content_items: Vec<OutputItem>,
error_text: Option<String>,
},
Terminated {
content_items: Vec<OutputItem>,
},
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CellError {
Busy,
Expand Down
Loading
Loading