aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-11 11:06:46 +01:00
parent df99318392
commit 86e7c72e5f
10383 changed files with 749164 additions and 749170 deletions

View File

@@ -0,0 +1,638 @@
use std::fmt;
use std::future::Future;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::process::Stdio;
use std::sync::Arc;
use std::sync::OnceLock;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use akode_code_mode_protocol::CodeModeOutputTruncation;
use akode_code_mode_protocol::CodeModeSessionCellExecutionLimits;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::CodeModeSessionOptions;
use akode_code_mode_protocol::ExecuteRequest;
use akode_code_mode_protocol::StartedCell;
use akode_code_mode_protocol::TerminateRequest;
use akode_code_mode_protocol::WaitOutcome;
use akode_code_mode_protocol::WaitRequest;
use akode_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY;
use akode_code_mode_protocol::host::Capability;
use akode_code_mode_protocol::host::CapabilitySet;
use akode_code_mode_protocol::host::ClientHello;
use akode_code_mode_protocol::host::ClientToHost;
use akode_code_mode_protocol::host::EncodedFrame;
use akode_code_mode_protocol::host::FramedReader;
use akode_code_mode_protocol::host::FramedWriter;
use akode_code_mode_protocol::host::HostToClient;
use akode_code_mode_protocol::host::LARGE_DELEGATE_CAPACITY_CAPABILITY;
use akode_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS;
use akode_code_mode_protocol::host::ProtocolVersion;
use akode_code_mode_protocol::host::RequestId;
use akode_code_mode_protocol::host::SESSION_RESOURCE_LIMITS_CAPABILITY;
use akode_code_mode_protocol::host::SupportedProtocolVersions;
use akode_code_mode_protocol::host::YIELD_OBSERVATION_CAPABILITY;
use akode_protocol::shell_environment::scrub_non_inheritable_env_vars;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Child;
use tokio::process::ChildStdin;
use tokio::process::ChildStdout;
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::debug;
use tracing::warn;
use self::driver::ConnectionDriver;
use self::driver::DriverCommand;
use self::driver::DriverEvent;
use self::driver::DriverLifecycle;
pub(super) use self::driver::RemoteSession;
pub(super) use self::driver::SessionCleanup;
use self::reader::drive_reader;
mod driver;
mod reader;
// Accommodate a completion burst and concurrent control messages from a full host.
const IPC_CHANNEL_CAPACITY: usize = MAX_PENDING_DELEGATE_CALLS * 2;
const LOCAL_HOST_STARTUP_TIMEOUT: Duration = Duration::from_secs(30);
// TODO(anp) make this timeout configurable if 60 seconds is insufficient.
const DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT: Duration = Duration::from_secs(60);
// Host spawn errors become model-visible tool output. Bound configured paths
// while preserving the executable-bearing suffix needed to diagnose failures.
const MAX_DISPLAYED_HOST_PROGRAM_BYTES: usize = 512;
const TRUNCATED_HOST_PROGRAM_PREFIX: &str = "...";
pub(super) enum ConnectionError {
Spawn {
host_program: PathBuf,
error: io::Error,
},
Other(String),
}
impl fmt::Display for ConnectionError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Spawn {
host_program,
error,
} => {
let host_program = host_program.to_string_lossy();
if host_program.len() <= MAX_DISPLAYED_HOST_PROGRAM_BYTES {
return write!(
formatter,
"failed to spawn code-mode host {host_program}: {error}"
);
}
let mut suffix_start = host_program.len()
- (MAX_DISPLAYED_HOST_PROGRAM_BYTES - TRUNCATED_HOST_PROGRAM_PREFIX.len());
while !host_program.is_char_boundary(suffix_start) {
suffix_start += 1;
}
write!(
formatter,
"failed to spawn code-mode host {TRUNCATED_HOST_PROGRAM_PREFIX}{}: {error}",
&host_program[suffix_start..]
)
}
Self::Other(message) => formatter.write_str(message),
}
}
}
pub(super) struct Connection {
command_tx: mpsc::Sender<DriverCommand>,
execute_claim_tx: mpsc::UnboundedSender<RequestId>,
alive: Arc<AtomicBool>,
failure: Arc<std::sync::Mutex<Option<String>>>,
cancellation: CancellationToken,
capabilities: CapabilitySet,
}
struct CallerCancellation {
token: CancellationToken,
armed: bool,
}
struct ConnectionSupervisor {
child: Child,
event_tx: mpsc::Sender<DriverEvent>,
cancellation: CancellationToken,
alive: Arc<AtomicBool>,
failure: Arc<std::sync::Mutex<Option<String>>>,
driver_task: JoinHandle<()>,
reader_task: JoinHandle<Result<(), String>>,
writer_task: JoinHandle<Result<(), String>>,
}
impl CallerCancellation {
fn new() -> Self {
Self {
token: CancellationToken::new(),
armed: true,
}
}
fn token(&self) -> CancellationToken {
self.token.clone()
}
fn disarm(mut self) {
self.armed = false;
}
}
impl Drop for CallerCancellation {
fn drop(&mut self) {
if self.armed {
self.token.cancel();
}
}
}
impl Connection {
pub(super) async fn spawn(host_program: &Path) -> Result<Self, ConnectionError> {
let mut command = Command::new(host_program);
#[cfg(unix)]
command.process_group(0);
#[cfg(windows)]
command.creation_flags(/*flags*/ 0x0800_0000); // CREATE_NO_WINDOW
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
scrub_non_inheritable_env_vars(command.as_std_mut());
let mut child = command.spawn().map_err(|error| ConnectionError::Spawn {
host_program: host_program.to_path_buf(),
error,
})?;
if let Some(stderr) = child.stderr.take() {
tokio::spawn(async move {
let mut lines = BufReader::new(stderr).lines();
loop {
match lines.next_line().await {
Ok(Some(line)) => debug!("code-mode host stderr: {line}"),
Ok(None) => break,
Err(err) => {
warn!("failed to read code-mode host stderr: {err}");
break;
}
}
}
});
}
let stdin = child
.stdin
.take()
.ok_or_else(|| ConnectionError::Other("spawned code-mode host has no stdin".into()))?;
let stdout = child
.stdout
.take()
.ok_or_else(|| ConnectionError::Other("spawned code-mode host has no stdout".into()))?;
Self::establish(FramedReader::new(stdout), FramedWriter::new(stdin), child).await
}
async fn establish(
mut reader: FramedReader<ChildStdout>,
mut writer: FramedWriter<ChildStdin>,
mut child: Child,
) -> Result<Self, ConnectionError> {
let handshake = async {
let session_limits_capability = Capability::new(SESSION_RESOURCE_LIMITS_CAPABILITY)
.map_err(|error| error.to_string())?;
let yield_capability =
Capability::new(YIELD_OBSERVATION_CAPABILITY).map_err(|error| error.to_string())?;
let output_capability =
Capability::new(BOUNDED_OUTPUT_CAPABILITY).map_err(|error| error.to_string())?;
let delegate_capacity = Capability::new(LARGE_DELEGATE_CAPACITY_CAPABILITY)
.map_err(|error| error.to_string())?;
let optional_capabilities = CapabilitySet::try_new([
session_limits_capability,
yield_capability,
output_capability,
delegate_capacity,
])
.map_err(|error| error.to_string())?;
let hello = ClientHello::new(
SupportedProtocolVersions::try_new([ProtocolVersion::V1])
.map_err(|err| err.to_string())?,
CapabilitySet::empty(),
optional_capabilities,
)
.map_err(|err| err.to_string())?;
writer
.write(&ClientToHost::ClientHello(hello))
.await
.map_err(|err| format!("failed to write code-mode host hello: {err}"))?;
match reader
.read()
.await
.map_err(|err| format!("failed to read code-mode host hello: {err}"))?
{
Some(HostToClient::HostHello(hello))
if hello.selected_version() == ProtocolVersion::V1 =>
{
Ok(hello.capabilities().clone())
}
Some(HostToClient::HandshakeRejected { reason }) => {
Err(format!("code-mode host rejected the handshake: {reason:?}"))
}
Some(message) => Err(format!(
"code-mode host returned an invalid handshake response: {message:?}"
)),
None => Err("code-mode host exited during handshake".to_string()),
}
};
let handshake_result =
match tokio::time::timeout(LOCAL_HOST_STARTUP_TIMEOUT, handshake).await {
Ok(result) => result,
Err(_) => {
kill_and_reap(&mut child).await;
return Err(ConnectionError::Other(
"timed out negotiating with the code-mode host".into(),
));
}
};
let capabilities = match handshake_result {
Ok(negotiated) => negotiated,
Err(err) => {
kill_and_reap(&mut child).await;
return Err(ConnectionError::Other(err));
}
};
let (command_tx, command_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY);
let (event_tx, event_rx) = mpsc::channel(IPC_CHANNEL_CAPACITY);
let (outgoing_tx, outgoing_rx) = mpsc::channel::<EncodedFrame>(IPC_CHANNEL_CAPACITY);
let cancellation = CancellationToken::new();
let alive = Arc::new(AtomicBool::new(true));
let failure = Arc::new(std::sync::Mutex::new(None));
let writer_cancellation = cancellation.clone();
let writer_task =
tokio::spawn(
async move { drive_writer(writer, outgoing_rx, writer_cancellation).await },
);
let reader_events = event_tx.clone();
let reader_cancellation = cancellation.clone();
let reader_task =
tokio::spawn(
async move { drive_reader(reader, reader_events, reader_cancellation).await },
);
let (driver, execute_claim_tx) = ConnectionDriver::new(
command_rx,
event_rx,
event_tx.clone(),
outgoing_tx,
DriverLifecycle {
alive: Arc::clone(&alive),
failure: Arc::clone(&failure),
cancellation: cancellation.clone(),
},
);
let driver_task = tokio::spawn(driver.run());
tokio::spawn(
ConnectionSupervisor {
child,
event_tx,
cancellation: cancellation.clone(),
alive: Arc::clone(&alive),
failure: Arc::clone(&failure),
driver_task,
reader_task,
writer_task,
}
.run(),
);
Ok(Self {
command_tx,
execute_claim_tx,
alive,
failure,
cancellation,
capabilities,
})
}
pub(super) fn is_alive(&self) -> bool {
if self.command_tx.is_closed() {
mark_connection_dead(
&self.alive,
&self.failure,
"code-mode connection driver closed".to_string(),
);
}
self.alive.load(Ordering::Acquire)
}
fn supports(&self, capability: &str) -> bool {
self.capabilities
.iter()
.any(|value| value.as_str() == capability)
}
pub(super) async fn open_session(
&self,
session: RemoteSession,
mut options: CodeModeSessionOptions,
pinned: &OnceLock<CodeModeOutputTruncation>,
) -> Result<SessionCleanup, String> {
if options.cell_execution_limits != CodeModeSessionCellExecutionLimits::default()
&& !self.supports(SESSION_RESOURCE_LIMITS_CAPABILITY)
{
return Err(format!(
"code-mode host does not support session resource limits: missing `{SESSION_RESOURCE_LIMITS_CAPABILITY}` capability"
));
}
let pinned_mode = pinned.get().copied();
let output_truncation = if self.supports(BOUNDED_OUTPUT_CAPABILITY) {
pinned_mode.unwrap_or(options.output_truncation)
} else if pinned_mode == Some(CodeModeOutputTruncation::Server) {
return Err(
"code-mode host cannot preserve server output truncation after reconnect".into(),
);
} else {
CodeModeOutputTruncation::Client
};
options.output_truncation = output_truncation;
let cleanup = SessionCleanup::new();
let cancellation = CallerCancellation::new();
let (response_tx, response_rx) = oneshot::channel();
self.send(DriverCommand::OpenSession {
session,
options,
cleanup: cleanup.clone(),
caller_cancellation: cancellation.token(),
response_tx,
})
.await?;
let result = self.receive(response_rx).await;
cancellation.disarm();
result?;
pinned.get_or_init(|| output_truncation);
Ok(cleanup)
}
pub(super) async fn execute(
&self,
session: RemoteSession,
request: ExecuteRequest,
delegate: Arc<dyn CodeModeSessionDelegate>,
yield_signal: Option<CancellationToken>,
) -> Result<StartedCell, String> {
let yield_signal = self.supported_yield_signal(yield_signal);
let cancellation = CallerCancellation::new();
let (response_tx, response_rx) = oneshot::channel();
self.send(DriverCommand::Execute {
session,
request,
delegate,
caller_cancellation: cancellation.token(),
yield_signal,
response_tx,
})
.await?;
let delivered = match self.receive(response_rx).await {
Ok(delivered) => delivered,
Err(err) => {
cancellation.disarm();
return Err(err);
}
};
self.execute_claim_tx
.send(delivered.request_id)
.map_err(|_| self.failure_message())?;
cancellation.disarm();
Ok(delivered.started)
}
pub(super) async fn wait(
&self,
session: RemoteSession,
request: WaitRequest,
yield_signal: Option<CancellationToken>,
) -> Result<WaitOutcome, String> {
let yield_signal = self.supported_yield_signal(yield_signal);
// Account for the runtime's one-second yield grace separately from transport.
let runtime_timeout =
Duration::from_millis(request.yield_time_ms).saturating_add(Duration::from_secs(1));
let cancellation = CallerCancellation::new();
let (response_tx, response_rx) = oneshot::channel();
let result = self
.with_transport_deadline(runtime_timeout, "wait", async {
self.send(DriverCommand::Wait {
session,
request,
caller_cancellation: cancellation.token(),
yield_signal,
response_tx,
})
.await?;
self.receive(response_rx).await
})
.await;
cancellation.disarm();
result
}
fn supported_yield_signal(
&self,
signal: Option<CancellationToken>,
) -> Option<CancellationToken> {
self.supports(YIELD_OBSERVATION_CAPABILITY)
.then_some(signal)
.flatten()
}
pub(super) async fn terminate(
&self,
session: RemoteSession,
request: TerminateRequest,
) -> Result<WaitOutcome, String> {
let (response_tx, response_rx) = oneshot::channel();
self.with_transport_deadline(Duration::ZERO, "terminate", async {
self.send(DriverCommand::Terminate {
session,
request,
response_tx,
})
.await?;
self.receive(response_rx).await
})
.await
}
pub(super) async fn shutdown_session(&self, session: RemoteSession) -> Result<(), String> {
let (response_tx, response_rx) = oneshot::channel();
self.send(DriverCommand::ShutdownSession {
session,
response_tx,
})
.await?;
self.receive(response_rx).await
}
async fn with_transport_deadline<T>(
&self,
runtime_timeout: Duration,
request_type: &str,
request: impl Future<Output = Result<T, String>>,
) -> Result<T, String> {
let deadline = runtime_timeout.saturating_add(DEFAULT_HOST_WAIT_TRANSPORT_TIMEOUT);
match tokio::time::timeout(deadline, request).await {
Ok(result) => result,
Err(_) => {
warn!(request_type, "code-mode host request exceeded its deadline");
let reason =
format!("code-mode host timed out waiting for {request_type} response");
mark_connection_dead(&self.alive, &self.failure, reason.clone());
self.cancellation.cancel();
Err(reason)
}
}
}
async fn send(&self, command: DriverCommand) -> Result<(), String> {
if !self.is_alive() {
return Err(self.failure_message());
}
self.command_tx
.send(command)
.await
.map_err(|_| self.failure_message())
}
async fn receive<T>(
&self,
response_rx: oneshot::Receiver<Result<T, String>>,
) -> Result<T, String> {
response_rx.await.map_err(|_| self.failure_message())?
}
fn failure_message(&self) -> String {
self.failure
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
.unwrap_or_else(|| "code-mode host connection closed".to_string())
}
}
async fn drive_writer(
mut writer: FramedWriter<ChildStdin>,
mut outgoing: mpsc::Receiver<EncodedFrame>,
cancellation: CancellationToken,
) -> Result<(), String> {
loop {
tokio::select! {
_ = cancellation.cancelled() => return Ok(()),
frame = outgoing.recv() => {
let Some(frame) = frame else {
return Err("code-mode host outgoing stream closed".to_string());
};
tokio::select! {
_ = cancellation.cancelled() => return Ok(()),
result = writer.write_frame(&frame) => {
result.map_err(|error| {
format!("failed to write code-mode host message: {error}")
})?;
}
}
}
}
}
}
impl Drop for Connection {
fn drop(&mut self) {
mark_connection_dead(
&self.alive,
&self.failure,
"code-mode host connection closed".to_string(),
);
self.cancellation.cancel();
}
}
impl ConnectionSupervisor {
async fn run(mut self) {
let mut child_exited = false;
let reason = tokio::select! {
biased;
_ = self.cancellation.cancelled() => failure_message(&self.failure),
result = &mut self.driver_task => match result {
Ok(()) => "code-mode connection driver exited unexpectedly".to_string(),
Err(err) => format!("code-mode connection driver task failed: {err}"),
},
result = &mut self.reader_task => task_failure("reader", result),
result = &mut self.writer_task => task_failure("writer", result),
result = self.child.wait() => {
child_exited = true;
match result {
Ok(status) => format!("code-mode host exited with status {status}"),
Err(error) => format!("failed waiting for code-mode host: {error}"),
}
}
};
mark_connection_dead(&self.alive, &self.failure, reason.clone());
let _ = self.event_tx.try_send(DriverEvent::Failed(reason));
self.cancellation.cancel();
if !child_exited {
kill_and_reap(&mut self.child).await;
}
}
}
fn task_failure(
task_name: &str,
result: Result<Result<(), String>, tokio::task::JoinError>,
) -> String {
match result {
Ok(Ok(())) => format!("code-mode connection {task_name} exited unexpectedly"),
Ok(Err(err)) => err,
Err(err) => format!("code-mode connection {task_name} task failed: {err}"),
}
}
fn mark_connection_dead(
alive: &AtomicBool,
failure: &std::sync::Mutex<Option<String>>,
reason: String,
) {
alive.store(false, Ordering::Release);
let mut failure = failure
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if failure.is_none() {
*failure = Some(reason);
}
}
fn failure_message(failure: &std::sync::Mutex<Option<String>>) -> String {
failure
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
.unwrap_or_else(|| "code-mode host connection closed".to_string())
}
async fn kill_and_reap(child: &mut Child) {
let _ = child.start_kill();
let _ = child.wait().await;
}

View File

@@ -0,0 +1,184 @@
use std::collections::VecDeque;
use std::panic::AssertUnwindSafe;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use akode_code_mode_protocol::CellId;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::host::EncodedFrame;
use akode_code_mode_protocol::host::HostToClient;
use akode_code_mode_protocol::host::RequestId;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
pub(in crate::remote_session) use self::cleanup::SessionCleanup;
use self::delegate_runtime::DelegateRuntime;
use self::request_tracker::RequestTracker;
use self::session_registry::SessionRegistry;
pub(super) use self::types::DriverCommand;
pub(super) use self::types::DriverEvent;
pub(in crate::remote_session) use self::types::RemoteSession;
mod cell_ids;
mod cleanup;
mod commands;
mod delegate_runtime;
mod request_tracker;
mod responses;
mod session_registry;
mod types;
pub(super) struct DriverLifecycle {
pub(super) alive: Arc<AtomicBool>,
pub(super) failure: Arc<std::sync::Mutex<Option<String>>>,
pub(super) cancellation: CancellationToken,
}
pub(super) struct ConnectionDriver {
command_rx: mpsc::Receiver<DriverCommand>,
event_rx: mpsc::Receiver<DriverEvent>,
event_tx: mpsc::Sender<DriverEvent>,
execute_claim_rx: mpsc::UnboundedReceiver<RequestId>,
outgoing_tx: mpsc::Sender<EncodedFrame>,
requests: RequestTracker,
deferred_host_messages: VecDeque<HostToClient>,
sessions: SessionRegistry,
delegates: DelegateRuntime,
alive: Arc<AtomicBool>,
failure: Arc<std::sync::Mutex<Option<String>>>,
cancellation: CancellationToken,
failed: bool,
}
impl ConnectionDriver {
pub(super) fn new(
command_rx: mpsc::Receiver<DriverCommand>,
event_rx: mpsc::Receiver<DriverEvent>,
event_tx: mpsc::Sender<DriverEvent>,
outgoing_tx: mpsc::Sender<EncodedFrame>,
lifecycle: DriverLifecycle,
) -> (Self, mpsc::UnboundedSender<RequestId>) {
let (execute_claim_tx, execute_claim_rx) = mpsc::unbounded_channel();
(
Self {
command_rx,
event_rx,
event_tx: event_tx.clone(),
execute_claim_rx,
outgoing_tx,
requests: RequestTracker::new(),
deferred_host_messages: VecDeque::new(),
sessions: SessionRegistry::new(),
delegates: DelegateRuntime::new(event_tx),
alive: lifecycle.alive,
failure: lifecycle.failure,
cancellation: lifecycle.cancellation,
failed: false,
},
execute_claim_tx,
)
}
pub(super) async fn run(mut self) {
loop {
tokio::select! {
biased;
_ = self.cancellation.cancelled() => {
self.fail("code-mode host connection closed".to_string());
return;
}
event = self.event_rx.recv() => {
let Some(event) = event else {
self.fail("code-mode host event stream closed".to_string());
return;
};
if !self.cancel_dropped_callers() || !self.handle_event(event) {
return;
}
}
claim = self.execute_claim_rx.recv() => {
let Some(request_id) = claim else {
self.fail("code-mode execute claim stream closed".to_string());
return;
};
self.requests.claim_execute(request_id);
}
command = self.command_rx.recv() => {
let Some(command) = command else {
self.fail("code-mode host command stream closed".to_string());
return;
};
if !self.cancel_dropped_callers() || !self.handle_command(command) {
return;
}
}
}
}
}
fn handle_event(&mut self, event: DriverEvent) -> bool {
let keep_running = match event {
DriverEvent::HostMessage(message) => self.handle_host_message(message),
DriverEvent::DelegateCompleted { id, result } => self.complete_delegate(id, result),
DriverEvent::RequestCancelled(id) => self.cancel_request(id),
DriverEvent::YieldRequest(id) => self.yield_request(id),
DriverEvent::Failed(reason) => {
self.fail(reason);
false
}
};
if keep_running {
self.flush_deferred_waits()
} else {
false
}
}
fn queue_frame(&mut self, frame: EncodedFrame) -> bool {
match self.outgoing_tx.try_send(frame) {
Ok(()) => true,
Err(mpsc::error::TrySendError::Full(_)) => {
self.fail("code-mode host outgoing queue is full".to_string());
false
}
Err(mpsc::error::TrySendError::Closed(_)) => {
self.fail("code-mode host writer closed".to_string());
false
}
}
}
fn fail(&mut self, reason: String) {
if self.failed {
return;
}
self.failed = true;
self.alive.store(false, Ordering::Release);
let reason = {
let mut failure = self
.failure
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
failure.get_or_insert(reason).clone()
};
self.requests.fail_all(&reason);
let failed_sessions = self.sessions.drain();
self.delegates.fail_all(failed_sessions);
self.cancellation.cancel();
}
}
impl Drop for ConnectionDriver {
fn drop(&mut self) {
self.fail("code-mode connection driver stopped unexpectedly".to_string());
}
}
fn notify_cell_closed(delegate: &Arc<dyn CodeModeSessionDelegate>, cell_id: &CellId) {
let _ = std::panic::catch_unwind(AssertUnwindSafe(|| delegate.cell_closed(cell_id)));
}
#[cfg(test)]
#[path = "driver_tests.rs"]
mod tests;

View File

@@ -0,0 +1,119 @@
use akode_code_mode_protocol::CellId;
use akode_code_mode_protocol::RuntimeResponse;
use akode_code_mode_protocol::WaitOutcome;
use akode_code_mode_protocol::WaitRequest;
use akode_code_mode_protocol::host::WireCellId;
use akode_code_mode_protocol::host::WireRuntimeResponse;
use akode_code_mode_protocol::host::WireWaitOutcome;
use akode_code_mode_protocol::host::WireWaitRequest;
use super::RemoteSession;
pub(super) fn public_cell_id(generation: u64, cell_id: &WireCellId) -> CellId {
if generation == 1 {
CellId::new(cell_id.as_str().to_string())
} else {
CellId::new(format!("g{generation}:{}", cell_id.as_str()))
}
}
pub(super) fn public_cell_id_from_protocol(generation: u64, cell_id: &CellId) -> CellId {
public_cell_id(generation, &WireCellId::new(cell_id.as_str()))
}
pub(super) fn remote_cell_id(
session: &RemoteSession,
cell_id: &CellId,
) -> Result<WireCellId, String> {
if session.generation == 1 {
if cell_id.as_str().starts_with('g') && cell_id.as_str().contains(':') {
return Err(format!(
"cell {cell_id} belongs to a stale code-mode host generation"
));
}
return Ok(WireCellId::new(cell_id.as_str()));
}
let prefix = format!("g{}:", session.generation);
let Some(remote_id) = cell_id.as_str().strip_prefix(&prefix) else {
return Err(format!(
"cell {cell_id} belongs to a stale code-mode host generation"
));
};
Ok(WireCellId::new(remote_id))
}
pub(super) fn remote_wait_request(
session: &RemoteSession,
request: WaitRequest,
) -> Result<WireWaitRequest, String> {
Ok(WireWaitRequest {
cell_id: remote_cell_id(session, &request.cell_id)?,
yield_time_ms: request.yield_time_ms,
})
}
pub(super) fn public_runtime_response(
generation: u64,
response: RuntimeResponse,
) -> RuntimeResponse {
match response {
RuntimeResponse::Yielded {
cell_id,
content_items,
code_mode_host_duration,
} => RuntimeResponse::Yielded {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
code_mode_host_duration,
},
RuntimeResponse::Terminated {
cell_id,
content_items,
code_mode_host_duration,
} => RuntimeResponse::Terminated {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
code_mode_host_duration,
},
RuntimeResponse::Result {
cell_id,
content_items,
result_buffered_for,
error_text,
code_mode_host_duration,
} => RuntimeResponse::Result {
cell_id: public_cell_id_from_protocol(generation, &cell_id),
content_items,
result_buffered_for,
error_text,
code_mode_host_duration,
},
}
}
pub(super) fn public_wait_outcome(generation: u64, outcome: WaitOutcome) -> WaitOutcome {
match outcome {
WaitOutcome::LiveCell(response) => {
WaitOutcome::LiveCell(public_runtime_response(generation, response))
}
WaitOutcome::MissingCell(response) => {
WaitOutcome::MissingCell(public_runtime_response(generation, response))
}
}
}
pub(super) fn runtime_response_cell_id(response: &WireRuntimeResponse) -> &WireCellId {
match response {
WireRuntimeResponse::Yielded { cell_id, .. }
| WireRuntimeResponse::Terminated { cell_id, .. }
| WireRuntimeResponse::Result { cell_id, .. } => cell_id,
}
}
pub(super) fn wait_outcome_cell_id(outcome: &WireWaitOutcome) -> &WireCellId {
match outcome {
WireWaitOutcome::LiveCell(response) | WireWaitOutcome::MissingCell(response) => {
runtime_response_cell_id(response)
}
}
}

View File

@@ -0,0 +1,40 @@
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
use super::notify_cell_closed;
use super::session_registry::CellOwner;
struct CleanupInner {
complete: CancellationToken,
}
#[derive(Clone)]
pub(in crate::remote_session) struct SessionCleanup {
inner: Arc<CleanupInner>,
}
impl SessionCleanup {
pub(in crate::remote_session) fn new() -> Self {
Self {
inner: Arc::new(CleanupInner {
complete: CancellationToken::new(),
}),
}
}
pub(super) fn fail(&self, cells: Vec<CellOwner>) {
for owner in cells {
notify_cell_closed(&owner.delegate, &owner.cell_id);
}
self.inner.complete.cancel();
}
pub(in crate::remote_session) async fn wait(&self) {
self.inner.complete.cancelled().await;
}
pub(in crate::remote_session) fn is_complete(&self) -> bool {
self.inner.complete.is_cancelled()
}
}

View File

@@ -0,0 +1,351 @@
use std::sync::Arc;
use akode_code_mode_protocol::CodeModeOutputTruncation;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::CodeModeSessionOptions;
use akode_code_mode_protocol::ExecuteRequest;
use akode_code_mode_protocol::TerminateRequest;
use akode_code_mode_protocol::WaitOutcome;
use akode_code_mode_protocol::WaitRequest;
use akode_code_mode_protocol::host::ClientToHost;
use akode_code_mode_protocol::host::EncodedFrame;
use akode_code_mode_protocol::host::HostRequest;
use akode_code_mode_protocol::host::WireOutputTruncation;
use akode_code_mode_protocol::host::WireSessionCellExecutionLimits;
use akode_code_mode_protocol::host::WireWaitRequest;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;
use super::ConnectionDriver;
use super::cell_ids::remote_cell_id;
use super::cell_ids::remote_wait_request;
use super::types::CancellableRequest;
use super::types::DeferredWait;
use super::types::DeliveredExecute;
use super::types::DriverCommand;
use super::types::ObservationYield;
use super::types::PendingRequest;
use super::types::RemoteSession;
impl ConnectionDriver {
pub(super) fn handle_command(&mut self, command: DriverCommand) -> bool {
match command {
DriverCommand::OpenSession {
session,
options,
cleanup,
caller_cancellation,
response_tx,
} => self.open_session(session, options, cleanup, caller_cancellation, response_tx),
DriverCommand::Execute {
session,
request,
delegate,
caller_cancellation,
yield_signal,
response_tx,
} => self.execute(
session,
request,
delegate,
caller_cancellation,
yield_signal,
response_tx,
),
DriverCommand::Wait {
session,
request,
caller_cancellation,
yield_signal,
response_tx,
} => self.wait(
session,
request,
caller_cancellation,
yield_signal,
response_tx,
),
DriverCommand::Terminate {
session,
request,
response_tx,
} => self.terminate(session, request, response_tx),
DriverCommand::ShutdownSession {
session,
response_tx,
} => self.shutdown_session(session, response_tx),
}
}
fn open_session(
&mut self,
session: RemoteSession,
options: CodeModeSessionOptions,
cleanup: super::cleanup::SessionCleanup,
caller_cancellation: CancellationToken,
response_tx: oneshot::Sender<Result<(), String>>,
) -> bool {
if self.sessions.contains(&session.id) || self.requests.contains_pending_open(&session) {
let _ = response_tx.send(Err(format!(
"code-mode session {} is already open",
session.id
)));
return true;
}
let limits = match WireSessionCellExecutionLimits::try_from(options.cell_execution_limits) {
Ok(limits) => limits,
Err(error) => {
let _ = response_tx.send(Err(format!(
"failed to encode code-mode session execution limits: {error}"
)));
return true;
}
};
let request_id = match self.requests.allocate_id() {
Ok(id) => id,
Err(err) => {
let _ = response_tx.send(Err(err));
return false;
}
};
let message = ClientToHost::Request {
id: request_id,
request: HostRequest::OpenSession {
session_id: session.id.clone(),
cell_execution_limits: (limits != WireSessionCellExecutionLimits::default())
.then_some(limits),
output_truncation: (options.output_truncation == CodeModeOutputTruncation::Server)
.then_some(WireOutputTruncation::Server),
},
};
let frame = match EncodedFrame::encode(&message) {
Ok(frame) => frame,
Err(err) => {
let _ = response_tx.send(Err(format!(
"failed to encode code-mode open-session request: {err}"
)));
return true;
}
};
let cancellation = CancellableRequest::new(caller_cancellation);
self.requests.insert_pending(
request_id,
PendingRequest::OpenSession {
session,
cleanup,
cancellation,
response_tx,
},
&self.event_tx,
);
self.queue_frame(frame)
}
fn execute(
&mut self,
session: RemoteSession,
request: ExecuteRequest,
delegate: Arc<dyn CodeModeSessionDelegate>,
caller_cancellation: CancellationToken,
yield_signal: Option<CancellationToken>,
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
) -> bool {
if let Err(err) = self.sessions.require_ready(&session) {
let _ = response_tx.send(Err(err));
return true;
}
let request = match request.try_into() {
Ok(request) => request,
Err(err) => {
let _ = response_tx.send(Err(format!(
"failed to encode code-mode execute request: {err}"
)));
return true;
}
};
let request_id = match self.requests.allocate_id() {
Ok(id) => id,
Err(err) => {
let _ = response_tx.send(Err(err));
return false;
}
};
let message = ClientToHost::Request {
id: request_id,
request: HostRequest::Execute {
session_id: session.id.clone(),
request,
},
};
let frame = match EncodedFrame::encode(&message) {
Ok(frame) => frame,
Err(err) => {
let _ = response_tx.send(Err(format!(
"code-mode execute request exceeds the IPC frame limit: {err}"
)));
return true;
}
};
let (initial_response_tx, initial_response_rx) = oneshot::channel();
let cancellation = CancellableRequest::new(caller_cancellation);
self.requests.insert_pending(
request_id,
PendingRequest::Execute {
session,
delegate,
response_tx,
initial_response_tx,
initial_response_rx,
cancellation,
yield_observation: ObservationYield::new(yield_signal),
},
&self.event_tx,
);
self.queue_frame(frame)
}
fn wait(
&mut self,
session: RemoteSession,
request: WaitRequest,
caller_cancellation: CancellationToken,
yield_signal: Option<CancellationToken>,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
) -> bool {
if let Err(err) = self.sessions.require_ready(&session) {
let _ = response_tx.send(Err(err));
return true;
}
let max_output_tokens = request.max_output_tokens;
let request = match remote_wait_request(&session, request) {
Ok(request) => request,
Err(err) => {
let _ = response_tx.send(Err(err));
return true;
}
};
if self.requests.has_cancelled_wait(&session, &request.cell_id) {
self.requests.push_deferred_wait(DeferredWait {
session,
request,
max_output_tokens,
caller_cancellation,
yield_signal,
response_tx,
});
return true;
}
self.start_wait(
session,
request,
max_output_tokens,
caller_cancellation,
yield_signal,
response_tx,
)
}
pub(super) fn start_wait(
&mut self,
session: RemoteSession,
request: WireWaitRequest,
max_output_tokens: Option<usize>,
caller_cancellation: CancellationToken,
yield_signal: Option<CancellationToken>,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
) -> bool {
let cell_id = request.cell_id.clone();
self.send_request(
HostRequest::Wait {
session_id: session.id.clone(),
request,
max_output_tokens,
},
PendingRequest::Wait {
session,
cell_id,
cancellation: CancellableRequest::new(caller_cancellation),
yield_observation: ObservationYield::new(yield_signal),
response_tx,
},
)
}
fn terminate(
&mut self,
session: RemoteSession,
request: TerminateRequest,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
) -> bool {
if let Err(err) = self.sessions.require_ready(&session) {
let _ = response_tx.send(Err(err));
return true;
}
let cell_id = match remote_cell_id(&session, &request.cell_id) {
Ok(cell_id) => cell_id,
Err(err) => {
let _ = response_tx.send(Err(err));
return true;
}
};
let pending_cell_id = cell_id.clone();
self.send_request(
HostRequest::Terminate {
session_id: session.id.clone(),
cell_id,
max_output_tokens: request.max_output_tokens,
},
PendingRequest::Terminate {
session,
cell_id: pending_cell_id,
response_tx,
},
)
}
fn shutdown_session(
&mut self,
session: RemoteSession,
response_tx: oneshot::Sender<Result<(), String>>,
) -> bool {
if let Err(err) = self.sessions.begin_shutdown(&session) {
let _ = response_tx.send(Err(err));
return true;
}
self.send_request(
HostRequest::ShutdownSession {
session_id: session.id.clone(),
},
PendingRequest::ShutdownSession {
session,
response_tx,
},
)
}
pub(super) fn send_request(&mut self, request: HostRequest, pending: PendingRequest) -> bool {
let request_id = match self.requests.allocate_id() {
Ok(id) => id,
Err(err) => {
pending.fail(err);
return false;
}
};
let message = ClientToHost::Request {
id: request_id,
request,
};
let frame = match EncodedFrame::encode(&message) {
Ok(frame) => frame,
Err(err) => {
pending.fail(format!(
"code-mode request exceeds the IPC frame limit: {err}"
));
return true;
}
};
self.requests
.insert_pending(request_id, pending, &self.event_tx);
self.queue_frame(frame)
}
}

View File

@@ -0,0 +1,353 @@
//! Client-side delegate task and closure lifecycle.
//!
//! Cancellation revokes the task's completion path before removing its active-call state. The
//! delegate future may finish later, but it can no longer send a response or affect cell closure.
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use akode_code_mode_protocol::CodeModeNestedToolCall;
use akode_code_mode_protocol::host::ClientToHost;
use akode_code_mode_protocol::host::DelegateRequest;
use akode_code_mode_protocol::host::DelegateRequestId;
use akode_code_mode_protocol::host::DelegateResponse;
use akode_code_mode_protocol::host::EncodedFrame;
use akode_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS;
use akode_code_mode_protocol::host::SessionId;
use akode_code_mode_protocol::host::WireCellId;
use akode_code_mode_protocol::host::WireResult;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use super::ConnectionDriver;
use super::notify_cell_closed;
use super::session_registry::CellOwner;
use super::session_registry::DelegateTarget;
use super::session_registry::FailedSession;
use super::types::DriverEvent;
const MAX_RECENT_DELEGATE_REQUEST_IDS: usize = 4096;
#[derive(Clone, Eq, Hash, PartialEq)]
struct CellKey {
session_id: akode_code_mode_protocol::host::SessionId,
cell_id: akode_code_mode_protocol::CellId,
}
impl CellKey {
fn for_owner(owner: &CellOwner) -> Self {
Self {
session_id: owner.session_id.clone(),
cell_id: owner.cell_id.clone(),
}
}
}
struct DelegateCall {
cell: CellKey,
cancellation: CancellationToken,
completion_stop: CancellationToken,
}
impl DelegateCall {
fn revoke(&self) {
self.cancellation.cancel();
self.completion_stop.cancel();
}
}
enum DelegateTask {
InvokeTool(CodeModeNestedToolCall),
Notify {
call_id: String,
cell_id: akode_code_mode_protocol::CellId,
text: String,
},
}
enum DelegateStartError {
Duplicate(DelegateRequestId),
CapacityExceeded,
}
pub(super) struct DelegateEffects {
pub(super) response: Option<(DelegateRequestId, Result<DelegateResponse, String>)>,
pub(super) closed_cells: Vec<CellOwner>,
}
impl DelegateEffects {
fn empty() -> Self {
Self {
response: None,
closed_cells: Vec::new(),
}
}
fn append(&mut self, mut other: Self) {
debug_assert!(self.response.is_none());
self.response = other.response.take();
self.closed_cells.append(&mut other.closed_cells);
}
}
pub(super) struct DelegateRuntime {
calls: HashMap<DelegateRequestId, DelegateCall>,
seen_requests: HashSet<DelegateRequestId>,
request_order: VecDeque<DelegateRequestId>,
event_tx: mpsc::Sender<DriverEvent>,
}
impl DelegateRuntime {
pub(super) fn new(event_tx: mpsc::Sender<DriverEvent>) -> Self {
Self {
calls: HashMap::new(),
seen_requests: HashSet::new(),
request_order: VecDeque::new(),
event_tx,
}
}
fn start(
&mut self,
id: DelegateRequestId,
target: DelegateTarget,
request: DelegateRequest,
) -> Result<(), DelegateStartError> {
if self.calls.contains_key(&id) || self.seen_requests.contains(&id) {
return Err(DelegateStartError::Duplicate(id));
}
self.remember_request(id);
if self.calls.len() >= MAX_PENDING_DELEGATE_CALLS {
return Err(DelegateStartError::CapacityExceeded);
}
let cancellation = CancellationToken::new();
let task_request = match request {
DelegateRequest::InvokeTool { invocation } => {
let mut invocation: CodeModeNestedToolCall = invocation.into();
invocation.cell_id = target.cell_id.clone();
DelegateTask::InvokeTool(invocation)
}
DelegateRequest::Notify {
call_id,
cell_id: _,
text,
} => DelegateTask::Notify {
call_id,
cell_id: target.cell_id.clone(),
text,
},
};
let delegate = target.delegate;
let task_cancellation = cancellation.clone();
let delegate_task = tokio::spawn(async move {
match task_request {
DelegateTask::InvokeTool(invocation) => delegate
.invoke_tool(invocation, task_cancellation)
.await
.map(|result| DelegateResponse::ToolResult { result }),
DelegateTask::Notify {
call_id,
cell_id,
text,
} => delegate
.notify(call_id, cell_id, text, task_cancellation)
.await
.map(|()| DelegateResponse::NotificationDelivered),
}
});
let completion_stop = CancellationToken::new();
self.calls.insert(
id,
DelegateCall {
cell: CellKey {
session_id: target.session_id,
cell_id: target.cell_id,
},
cancellation,
completion_stop: completion_stop.clone(),
},
);
let event_tx = self.event_tx.clone();
tokio::spawn(async move {
let result = tokio::select! {
biased;
_ = completion_stop.cancelled() => return,
result = delegate_task => match result {
Ok(result) => result,
Err(err) => Err(format!("code-mode delegate task failed: {err}")),
},
};
tokio::select! {
biased;
_ = completion_stop.cancelled() => {}
_ = event_tx.send(DriverEvent::DelegateCompleted { id, result }) => {}
}
});
Ok(())
}
pub(super) fn cancel(&mut self, id: DelegateRequestId) {
if let Some(call) = self.calls.remove(&id) {
call.revoke();
}
}
pub(super) fn complete(
&mut self,
id: DelegateRequestId,
result: Result<DelegateResponse, String>,
) -> DelegateEffects {
if self.calls.remove(&id).is_none() {
return DelegateEffects::empty();
}
let mut effects = DelegateEffects::empty();
effects.response = Some((id, result));
effects
}
pub(super) fn close_cell(&mut self, owner: CellOwner) -> DelegateEffects {
let key = CellKey::for_owner(&owner);
self.calls.retain(|_, call| {
if call.cell != key {
return true;
}
call.revoke();
false
});
let mut effects = DelegateEffects::empty();
effects.closed_cells.push(owner);
effects
}
pub(super) fn close_cells(&mut self, owners: Vec<CellOwner>) -> DelegateEffects {
let mut effects = DelegateEffects::empty();
for owner in owners {
effects.append(self.close_cell(owner));
}
effects
}
pub(super) fn fail_all(&mut self, failed_sessions: Vec<FailedSession>) {
for (_, call) in self.calls.drain() {
call.revoke();
}
for session in failed_sessions {
session.cleanup.fail(session.cells);
}
}
fn remember_request(&mut self, id: DelegateRequestId) {
self.seen_requests.insert(id);
self.request_order.push_back(id);
while self.request_order.len() > MAX_RECENT_DELEGATE_REQUEST_IDS {
if let Some(expired) = self.request_order.pop_front() {
self.seen_requests.remove(&expired);
}
}
}
}
impl ConnectionDriver {
pub(super) fn start_delegate(
&mut self,
id: DelegateRequestId,
session_id: SessionId,
request: DelegateRequest,
) -> bool {
let wire_cell_id = match &request {
DelegateRequest::InvokeTool { invocation } => &invocation.cell_id,
DelegateRequest::Notify { cell_id, .. } => cell_id,
};
let target = match self.sessions.delegate_target(&session_id, wire_cell_id) {
Ok(target) => target,
Err(err) => return self.send_delegate_response(id, Err(err)),
};
match self.delegates.start(id, target, request) {
Ok(()) => true,
Err(DelegateStartError::Duplicate(id)) => {
self.fail(format!("duplicate code-mode delegate request ID {id:?}"));
false
}
Err(DelegateStartError::CapacityExceeded) => self.send_delegate_response(
id,
Err(format!(
"code-mode host exceeded the limit of {MAX_PENDING_DELEGATE_CALLS} pending delegate calls"
)),
),
}
}
pub(super) fn complete_delegate(
&mut self,
id: DelegateRequestId,
result: Result<DelegateResponse, String>,
) -> bool {
let effects = self.delegates.complete(id, result);
self.apply_delegate_effects(effects)
}
fn send_delegate_response(
&mut self,
id: DelegateRequestId,
result: Result<DelegateResponse, String>,
) -> bool {
let message = ClientToHost::DelegateResponse {
id,
result: WireResult::from_result(result),
};
let frame = match EncodedFrame::encode(&message) {
Ok(frame) => frame,
Err(err) => {
let fallback = ClientToHost::DelegateResponse {
id,
result: WireResult::Err {
message: format!(
"code-mode delegate response exceeds the IPC frame limit: {err}"
),
},
};
match EncodedFrame::encode(&fallback) {
Ok(frame) => frame,
Err(fallback_err) => {
self.fail(format!(
"failed to encode code-mode delegate error response: {fallback_err}"
));
return false;
}
}
}
};
self.queue_frame(frame)
}
pub(super) fn close_cell(&mut self, session_id: SessionId, cell_id: WireCellId) -> bool {
let owner = match self.sessions.remove_cell(&session_id, &cell_id) {
Ok(owner) => owner,
Err(err) => {
self.fail(err);
return false;
}
};
let effects = self.delegates.close_cell(owner);
self.apply_delegate_effects(effects)
}
pub(super) fn close_session_locally(&mut self, session_id: &SessionId) -> DelegateEffects {
self.requests.remove_unclaimed_for_session(session_id);
let owners = self.sessions.remove_session(session_id);
self.delegates.close_cells(owners)
}
pub(super) fn apply_delegate_effects(&mut self, effects: DelegateEffects) -> bool {
if let Some((id, result)) = effects.response
&& !self.send_delegate_response(id, result)
{
return false;
}
for closed in effects.closed_cells {
notify_cell_closed(&closed.delegate, &closed.cell_id);
}
true
}
}

View File

@@ -0,0 +1,198 @@
use std::collections::HashMap;
use std::collections::VecDeque;
use akode_code_mode_protocol::host::RequestId;
use akode_code_mode_protocol::host::SessionId;
use akode_code_mode_protocol::host::WireCellId;
use tokio::sync::mpsc;
use super::types::DeferredWait;
use super::types::DriverEvent;
use super::types::InitialResponse;
use super::types::PendingRequest;
use super::types::RemoteSession;
use super::types::UnclaimedExecute;
pub(super) enum CancellationAction {
Send(RequestId),
Terminate {
request_id: RequestId,
execute: UnclaimedExecute,
},
}
pub(super) struct RequestTracker {
pending: HashMap<RequestId, PendingRequest>,
unclaimed_executes: HashMap<RequestId, UnclaimedExecute>,
initial_responses: HashMap<RequestId, InitialResponse>,
deferred_waits: VecDeque<DeferredWait>,
next_request_id: i64,
}
impl RequestTracker {
pub(super) fn new() -> Self {
Self {
pending: HashMap::new(),
unclaimed_executes: HashMap::new(),
initial_responses: HashMap::new(),
deferred_waits: VecDeque::new(),
next_request_id: 1,
}
}
pub(super) fn contains_pending_open(&self, session: &RemoteSession) -> bool {
self.pending.values().any(|pending| {
matches!(
pending,
PendingRequest::OpenSession {
session: pending_session,
..
} if pending_session.id == session.id
)
})
}
pub(super) fn has_pending_execute_for_session(&self, session_id: &SessionId) -> bool {
self.pending.values().any(|request| {
matches!(
request,
PendingRequest::Execute { session, .. } if session.id == *session_id
)
})
}
pub(super) fn allocate_id(&mut self) -> Result<RequestId, String> {
let id = self.next_request_id;
self.next_request_id = self
.next_request_id
.checked_add(1)
.ok_or_else(|| "code-mode host request ID space exhausted".to_string())?;
Ok(RequestId::new(id))
}
pub(super) fn insert_pending(
&mut self,
id: RequestId,
mut pending: PendingRequest,
event_tx: &mpsc::Sender<DriverEvent>,
) {
if let Some(observation) = pending.yield_observation() {
observation.spawn_watcher(id, event_tx.clone());
}
self.pending.insert(id, pending);
if let Some(cancellation) = self
.pending
.get_mut(&id)
.and_then(PendingRequest::cancellation_mut)
{
cancellation.spawn_watcher(id, event_tx.clone());
}
}
pub(super) fn remove_pending(&mut self, id: RequestId) -> Option<PendingRequest> {
self.pending.remove(&id)
}
pub(super) fn insert_initial_response(&mut self, id: RequestId, response: InitialResponse) {
self.initial_responses.insert(id, response);
}
pub(super) fn remove_initial_response(&mut self, id: RequestId) -> Option<InitialResponse> {
self.initial_responses.remove(&id)
}
pub(super) fn insert_unclaimed_execute(&mut self, id: RequestId, execute: UnclaimedExecute) {
self.unclaimed_executes.insert(id, execute);
}
pub(super) fn claim_execute(&mut self, id: RequestId) {
self.unclaimed_executes.remove(&id);
}
pub(super) fn collect_cancellations(&mut self) -> Vec<CancellationAction> {
let mut actions = self
.pending
.iter_mut()
.filter_map(|(id, pending)| {
let cancellation = pending.cancellation_mut()?;
(cancellation.is_cancelled() && cancellation.mark_reported())
.then_some(CancellationAction::Send(*id))
})
.collect::<Vec<_>>();
actions.extend(
self.unclaimed_executes
.extract_if(|_, execute| {
execute.cancellation.is_cancelled() && execute.cancellation.mark_reported()
})
.map(|(request_id, execute)| CancellationAction::Terminate {
request_id,
execute,
}),
);
actions
}
pub(super) fn mark_cancelled(&mut self, id: RequestId) -> Option<CancellationAction> {
if let Some(cancellation) = self
.pending
.get_mut(&id)
.and_then(PendingRequest::cancellation_mut)
{
return cancellation
.mark_reported()
.then_some(CancellationAction::Send(id));
}
let execute = self.unclaimed_executes.get_mut(&id)?;
if !execute.cancellation.mark_reported() {
return None;
}
self.unclaimed_executes
.remove(&id)
.map(|execute| CancellationAction::Terminate {
request_id: id,
execute,
})
}
pub(super) fn has_cancelled_wait(&self, session: &RemoteSession, cell_id: &WireCellId) -> bool {
self.pending.values().any(|pending| {
matches!(
pending,
PendingRequest::Wait {
session: pending_session,
cell_id: pending_cell_id,
cancellation,
..
} if pending_session == session
&& pending_cell_id == cell_id
&& cancellation.is_cancelled()
)
})
}
pub(super) fn push_deferred_wait(&mut self, wait: DeferredWait) {
self.deferred_waits.push_back(wait);
}
pub(super) fn take_deferred_waits(&mut self) -> VecDeque<DeferredWait> {
std::mem::take(&mut self.deferred_waits)
}
pub(super) fn remove_unclaimed_for_session(&mut self, session_id: &SessionId) {
self.unclaimed_executes
.retain(|_, execute| &execute.session.id != session_id);
}
pub(super) fn fail_all(&mut self, reason: &str) {
for (_, pending) in self.pending.drain() {
pending.fail(reason.to_string());
}
self.unclaimed_executes.clear();
for (_, initial) in self.initial_responses.drain() {
let _ = initial.response_tx.send(Err(reason.to_string()));
}
for wait in self.deferred_waits.drain(..) {
let _ = wait.response_tx.send(Err(reason.to_string()));
}
}
}

View File

@@ -0,0 +1,476 @@
use akode_code_mode_protocol::StartedCell;
use akode_code_mode_protocol::host::ClientToHost;
use akode_code_mode_protocol::host::EncodedFrame;
use akode_code_mode_protocol::host::HostRequest;
use akode_code_mode_protocol::host::HostResponse;
use akode_code_mode_protocol::host::HostToClient;
use akode_code_mode_protocol::host::RequestId;
use akode_code_mode_protocol::host::WireCellId;
use tokio::sync::oneshot;
use super::ConnectionDriver;
use super::cell_ids::public_runtime_response;
use super::cell_ids::public_wait_outcome;
use super::cell_ids::runtime_response_cell_id;
use super::cell_ids::wait_outcome_cell_id;
use super::request_tracker::CancellationAction;
use super::session_registry::CellAdmissionError;
use super::types::DeliveredExecute;
use super::types::InitialResponse;
use super::types::PendingRequest;
use super::types::RemoteSession;
use super::types::UnclaimedExecute;
impl ConnectionDriver {
pub(super) fn flush_deferred_waits(&mut self) -> bool {
let mut deferred = self.requests.take_deferred_waits();
while let Some(wait) = deferred.pop_front() {
if wait.caller_cancellation.is_cancelled() {
let _ = wait
.response_tx
.send(Err("code-mode request cancelled".to_string()));
continue;
}
if self
.requests
.has_cancelled_wait(&wait.session, &wait.request.cell_id)
{
self.requests.push_deferred_wait(wait);
continue;
}
if !self.start_wait(
wait.session,
wait.request,
wait.max_output_tokens,
wait.caller_cancellation,
wait.yield_signal,
wait.response_tx,
) {
for wait in deferred {
let _ = wait
.response_tx
.send(Err("code-mode host connection closed".to_string()));
}
return false;
}
}
true
}
pub(super) fn handle_host_message(&mut self, message: HostToClient) -> bool {
if self.should_defer_host_message(&message) {
if self.deferred_host_messages.len()
>= akode_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS
{
self.fail(
"code-mode host exceeded deferred cross-socket message limit".to_string(),
);
return false;
}
self.deferred_host_messages.push_back(message);
return true;
}
if !self.dispatch_host_message(message) {
return false;
}
for _ in 0..self.deferred_host_messages.len() {
let Some(message) = self.deferred_host_messages.pop_front() else {
break;
};
if self.should_defer_host_message(&message) {
self.deferred_host_messages.push_back(message);
} else if !self.dispatch_host_message(message) {
return false;
}
}
true
}
fn should_defer_host_message(&self, message: &HostToClient) -> bool {
match message {
HostToClient::DelegateRequest {
session_id,
request,
..
} => {
let cell_id = match request {
akode_code_mode_protocol::host::DelegateRequest::InvokeTool { invocation } => {
&invocation.cell_id
}
akode_code_mode_protocol::host::DelegateRequest::Notify { cell_id, .. } => {
cell_id
}
};
!self.sessions.contains_cell(session_id, cell_id)
&& self.requests.has_pending_execute_for_session(session_id)
}
HostToClient::Response { .. }
| HostToClient::InitialResponse { .. }
| HostToClient::CellClosed { .. }
| HostToClient::CancelDelegateRequest { .. }
| HostToClient::HostHello(_)
| HostToClient::HandshakeRejected { .. } => false,
}
}
fn dispatch_host_message(&mut self, message: HostToClient) -> bool {
match message {
HostToClient::Response { id, result } => {
self.complete_request(id, result.into_result())
}
HostToClient::InitialResponse { id, result } => {
self.complete_initial_response(id, result.into_result())
}
HostToClient::DelegateRequest {
id,
session_id,
request,
} => self.start_delegate(id, session_id, request),
HostToClient::CancelDelegateRequest { id } => {
self.deferred_host_messages.retain(|message| {
!matches!(
message,
HostToClient::DelegateRequest { id: deferred_id, .. } if *deferred_id == id
)
});
self.delegates.cancel(id);
true
}
HostToClient::CellClosed {
session_id,
cell_id,
} => self.close_cell(session_id, cell_id),
HostToClient::HostHello(_) | HostToClient::HandshakeRejected { .. } => {
self.fail("code-mode host sent a second handshake response".to_string());
false
}
}
}
fn complete_request(&mut self, id: RequestId, result: Result<HostResponse, String>) -> bool {
let Some(pending) = self.requests.remove_pending(id) else {
self.fail(format!("code-mode host returned unknown request ID {id:?}"));
return false;
};
match pending {
PendingRequest::OpenSession {
session,
cleanup,
cancellation,
response_tx,
} => match result {
Ok(HostResponse::SessionReady { session_id }) if session_id == session.id => {
let abandoned = cancellation.is_cancelled() || response_tx.is_closed();
self.sessions.insert_ready(session.clone(), cleanup);
if abandoned || response_tx.send(Ok(())).is_err() {
return self.shutdown_abandoned_session(session);
}
}
Ok(_) => {
let reason =
"code-mode host returned an invalid open-session response".to_string();
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Err(err) => {
let _ = response_tx.send(Err(err));
}
},
PendingRequest::Execute {
session,
delegate,
response_tx,
initial_response_tx,
initial_response_rx,
cancellation,
yield_observation,
} => match result {
Ok(HostResponse::ExecutionStarted { cell_id }) => {
// The host owns a checked, never-reused ID sequence. Retain only live
// IDs so client memory scales with concurrency, not session lifetime.
let remote_cell_id = cell_id.clone();
let public_id = match self.sessions.admit_cell(&session, cell_id, delegate) {
Ok(public_id) => public_id,
Err(CellAdmissionError::MissingSession) => {
let _ = response_tx
.send(Err("code-mode session closed during execute".to_string()));
return true;
}
Err(CellAdmissionError::DuplicateCell) => {
let reason = format!(
"code-mode host reused live cell {} in session {}",
remote_cell_id.as_str(),
session.id
);
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
};
self.requests.insert_initial_response(
id,
InitialResponse {
generation: session.generation,
cell_id: remote_cell_id.clone(),
response_tx: initial_response_tx,
_yield_observation: yield_observation,
},
);
let started = StartedCell::from_result_receiver(public_id, initial_response_rx);
if cancellation.is_cancelled() || response_tx.is_closed() {
return self.terminate_abandoned_cell(session, remote_cell_id);
}
let delivered = DeliveredExecute {
request_id: id,
started,
};
if response_tx.send(Ok(delivered)).is_err() {
return self.terminate_abandoned_cell(session, remote_cell_id);
}
self.requests.insert_unclaimed_execute(
id,
UnclaimedExecute {
session,
cell_id: remote_cell_id,
cancellation,
},
);
}
Ok(_) => {
let reason = "code-mode host returned an invalid execute response".to_string();
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Err(err) => {
let _ = response_tx.send(Err(err));
}
},
PendingRequest::Wait {
session,
cell_id,
cancellation: _,
yield_observation: _,
response_tx,
} => {
let result = match result {
Ok(HostResponse::WaitCompleted { outcome }) => {
if wait_outcome_cell_id(&outcome) != &cell_id {
let reason = format!(
"code-mode host returned cell {} for request targeting {}",
wait_outcome_cell_id(&outcome).as_str(),
cell_id.as_str()
);
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Ok(public_wait_outcome(session.generation, outcome.into()))
}
Ok(_) => {
let reason = "code-mode host returned an invalid cell response".to_string();
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Err(err) => Err(err),
};
let _ = response_tx.send(result);
}
PendingRequest::Terminate {
session,
cell_id,
response_tx,
} => {
let result = match result {
Ok(HostResponse::WaitCompleted { outcome }) => {
if wait_outcome_cell_id(&outcome) != &cell_id {
let reason = format!(
"code-mode host returned cell {} for request targeting {}",
wait_outcome_cell_id(&outcome).as_str(),
cell_id.as_str()
);
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
public_wait_outcome(session.generation, outcome.into())
}
Ok(_) => {
let reason = "code-mode host returned an invalid cell response".to_string();
let _ = response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Err(err) => {
let _ = response_tx.send(Err(err));
return true;
}
};
let _ = response_tx.send(Ok(result));
}
PendingRequest::ShutdownSession {
session,
response_tx,
} => match result {
Ok(HostResponse::SessionClosed { session_id }) if session_id == session.id => {
let effects = self.close_session_locally(&session.id);
if !self.apply_delegate_effects(effects) {
return false;
}
let _ = response_tx.send(Ok(()));
}
Ok(_) => {
let err = "code-mode host returned an invalid shutdown response".to_string();
let _ = response_tx.send(Err(err.clone()));
self.fail(err);
return false;
}
Err(err) => {
let _ = response_tx.send(Err(err.clone()));
self.fail(err);
return false;
}
},
}
true
}
pub(super) fn cancel_dropped_callers(&mut self) -> bool {
for action in self.requests.collect_cancellations() {
if !self.apply_cancellation(action) {
return false;
}
}
true
}
pub(super) fn cancel_request(&mut self, id: RequestId) -> bool {
self.requests
.mark_cancelled(id)
.is_none_or(|action| self.apply_cancellation(action))
}
fn apply_cancellation(&mut self, action: CancellationAction) -> bool {
match action {
CancellationAction::Send(id) => self.send_cancel_request(id),
CancellationAction::Terminate {
request_id,
execute,
} => {
if !self.send_cancel_request(request_id) {
return false;
}
self.terminate_abandoned_cell(execute.session, execute.cell_id)
}
}
}
pub(super) fn yield_request(&mut self, id: RequestId) -> bool {
let frame = match EncodedFrame::encode(&ClientToHost::YieldRequest { id }) {
Ok(frame) => frame,
Err(err) => {
self.fail(format!("failed to encode code-mode yield request: {err}"));
return false;
}
};
self.queue_frame(frame)
}
fn send_cancel_request(&mut self, id: RequestId) -> bool {
let message = ClientToHost::CancelRequest { id };
let frame = match EncodedFrame::encode(&message) {
Ok(frame) => frame,
Err(err) => {
self.fail(format!(
"failed to encode code-mode cancellation request: {err}"
));
return false;
}
};
self.queue_frame(frame)
}
fn shutdown_abandoned_session(&mut self, session: RemoteSession) -> bool {
let Some(should_shutdown) = self.sessions.begin_abandoned_shutdown(&session.id) else {
self.fail(format!(
"code-mode host committed abandoned session {} without local state",
session.id
));
return false;
};
if !should_shutdown {
return true;
}
let (response_tx, response_rx) = oneshot::channel();
drop(response_rx);
self.send_request(
HostRequest::ShutdownSession {
session_id: session.id.clone(),
},
PendingRequest::ShutdownSession {
session,
response_tx,
},
)
}
fn terminate_abandoned_cell(&mut self, session: RemoteSession, cell_id: WireCellId) -> bool {
let Some(is_closing) = self.sessions.is_closing(&session.id) else {
self.fail(format!(
"code-mode host admitted an abandoned cell in unknown session {}",
session.id
));
return false;
};
if is_closing {
return true;
}
let (response_tx, response_rx) = oneshot::channel();
drop(response_rx);
self.send_request(
HostRequest::Terminate {
session_id: session.id.clone(),
cell_id: cell_id.clone(),
max_output_tokens: None,
},
PendingRequest::Terminate {
session,
cell_id,
response_tx,
},
)
}
fn complete_initial_response(
&mut self,
id: RequestId,
result: Result<akode_code_mode_protocol::host::WireRuntimeResponse, String>,
) -> bool {
let Some(initial) = self.requests.remove_initial_response(id) else {
self.fail(format!(
"code-mode host returned initial response for unknown request ID {id:?}"
));
return false;
};
let response = match result {
Ok(response) if runtime_response_cell_id(&response) == &initial.cell_id => {
Ok(public_runtime_response(initial.generation, response.into()))
}
Ok(response) => {
let reason = format!(
"code-mode host returned initial response for cell {} instead of {}",
runtime_response_cell_id(&response).as_str(),
initial.cell_id.as_str()
);
let _ = initial.response_tx.send(Err(reason.clone()));
self.fail(reason);
return false;
}
Err(err) => Err(err),
};
let _ = initial.response_tx.send(response);
true
}
}

View File

@@ -0,0 +1,208 @@
use std::collections::HashMap;
use std::sync::Arc;
use akode_code_mode_protocol::CellId;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::host::SessionId;
use akode_code_mode_protocol::host::WireCellId;
use super::cell_ids::public_cell_id;
use super::cleanup::SessionCleanup;
use super::types::RemoteSession;
pub(super) struct CellOwner {
pub(super) session_id: SessionId,
pub(super) cell_id: CellId,
pub(super) delegate: Arc<dyn CodeModeSessionDelegate>,
}
pub(super) struct DelegateTarget {
pub(super) session_id: SessionId,
pub(super) cell_id: CellId,
pub(super) delegate: Arc<dyn CodeModeSessionDelegate>,
}
pub(super) struct FailedSession {
pub(super) cleanup: SessionCleanup,
pub(super) cells: Vec<CellOwner>,
}
pub(super) enum CellAdmissionError {
MissingSession,
DuplicateCell,
}
struct SessionRecord {
remote: RemoteSession,
cleanup: SessionCleanup,
phase: SessionPhase,
cells: HashMap<WireCellId, CellOwner>,
}
#[derive(Clone, Copy, Eq, PartialEq)]
enum SessionPhase {
Ready,
Closing,
}
pub(super) struct SessionRegistry {
records: HashMap<SessionId, SessionRecord>,
}
impl SessionRegistry {
pub(super) fn new() -> Self {
Self {
records: HashMap::new(),
}
}
pub(super) fn contains(&self, session_id: &SessionId) -> bool {
self.records.contains_key(session_id)
}
pub(super) fn contains_cell(&self, session_id: &SessionId, cell_id: &WireCellId) -> bool {
self.records
.get(session_id)
.is_some_and(|session| session.cells.contains_key(cell_id))
}
pub(super) fn insert_ready(&mut self, session: RemoteSession, cleanup: SessionCleanup) {
self.records.insert(
session.id.clone(),
SessionRecord {
remote: session,
cleanup,
phase: SessionPhase::Ready,
cells: HashMap::new(),
},
);
}
pub(super) fn require_ready(&self, session: &RemoteSession) -> Result<(), String> {
let record = self
.records
.get(&session.id)
.ok_or_else(|| format!("unknown code-mode session {}", session.id))?;
if record.remote != *session {
return Err("stale code-mode session generation".to_string());
}
if record.phase != SessionPhase::Ready {
return Err("code-mode session is shutting down".to_string());
}
Ok(())
}
pub(super) fn begin_shutdown(&mut self, session: &RemoteSession) -> Result<(), String> {
let record = self
.records
.get_mut(&session.id)
.ok_or_else(|| format!("unknown code-mode session {}", session.id))?;
if record.remote != *session {
return Err("stale code-mode session generation".to_string());
}
if record.phase == SessionPhase::Closing {
return Err("code-mode session is already closing".to_string());
}
record.phase = SessionPhase::Closing;
Ok(())
}
pub(super) fn begin_abandoned_shutdown(&mut self, session_id: &SessionId) -> Option<bool> {
let record = self.records.get_mut(session_id)?;
if record.phase == SessionPhase::Closing {
return Some(false);
}
record.phase = SessionPhase::Closing;
Some(true)
}
pub(super) fn is_closing(&self, session_id: &SessionId) -> Option<bool> {
self.records
.get(session_id)
.map(|record| record.phase == SessionPhase::Closing)
}
pub(super) fn admit_cell(
&mut self,
session: &RemoteSession,
cell_id: WireCellId,
delegate: Arc<dyn CodeModeSessionDelegate>,
) -> Result<CellId, CellAdmissionError> {
let Some(record) = self.records.get_mut(&session.id) else {
return Err(CellAdmissionError::MissingSession);
};
if record.cells.contains_key(&cell_id) {
return Err(CellAdmissionError::DuplicateCell);
}
let public_id = public_cell_id(session.generation, &cell_id);
record.cells.insert(
cell_id,
CellOwner {
session_id: session.id.clone(),
cell_id: public_id.clone(),
delegate,
},
);
Ok(public_id)
}
pub(super) fn delegate_target(
&self,
session_id: &SessionId,
cell_id: &WireCellId,
) -> Result<DelegateTarget, String> {
let session = self
.records
.get(session_id)
.ok_or_else(|| format!("code-mode host delegated for unknown session {session_id}"))?;
let owner = session.cells.get(cell_id).ok_or_else(|| {
format!(
"code-mode host delegated for unknown cell {} in session {session_id}",
cell_id.as_str()
)
})?;
Ok(DelegateTarget {
session_id: session_id.clone(),
cell_id: owner.cell_id.clone(),
delegate: Arc::clone(&owner.delegate),
})
}
pub(super) fn remove_cell(
&mut self,
session_id: &SessionId,
cell_id: &WireCellId,
) -> Result<CellOwner, String> {
let session = self.records.get_mut(session_id).ok_or_else(|| {
format!(
"code-mode host closed cell {} in unknown session {session_id}",
cell_id.as_str()
)
})?;
session
.cells
.remove(cell_id)
.ok_or_else(|| format!("code-mode host closed unknown cell in session {session_id}"))
}
pub(super) fn remove_session(&mut self, session_id: &SessionId) -> Vec<CellOwner> {
let Some(session) = self.records.remove(session_id) else {
return Vec::new();
};
session.cells.into_values().collect()
}
pub(super) fn drain(&mut self) -> Vec<FailedSession> {
let sessions = std::mem::take(&mut self.records);
sessions
.into_values()
.map(|session| {
let cells = session.cells.into_values().collect();
FailedSession {
cleanup: session.cleanup,
cells,
}
})
.collect()
}
}

View File

@@ -0,0 +1,244 @@
use std::sync::Arc;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::CodeModeSessionOptions;
use akode_code_mode_protocol::ExecuteRequest;
use akode_code_mode_protocol::RuntimeResponse;
use akode_code_mode_protocol::StartedCell;
use akode_code_mode_protocol::TerminateRequest;
use akode_code_mode_protocol::WaitOutcome;
use akode_code_mode_protocol::WaitRequest;
use akode_code_mode_protocol::host::DelegateRequestId;
use akode_code_mode_protocol::host::DelegateResponse;
use akode_code_mode_protocol::host::HostToClient;
use akode_code_mode_protocol::host::RequestId;
use akode_code_mode_protocol::host::SessionId;
use akode_code_mode_protocol::host::WireCellId;
use akode_code_mode_protocol::host::WireWaitRequest;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;
use tokio_util::task::AbortOnDropHandle;
use super::cleanup::SessionCleanup;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::remote_session) struct RemoteSession {
pub(in crate::remote_session) id: SessionId,
pub(in crate::remote_session) generation: u64,
}
pub(in crate::remote_session::connection) enum DriverCommand {
OpenSession {
session: RemoteSession,
options: CodeModeSessionOptions,
cleanup: SessionCleanup,
caller_cancellation: CancellationToken,
response_tx: oneshot::Sender<Result<(), String>>,
},
Execute {
session: RemoteSession,
delegate: Arc<dyn CodeModeSessionDelegate>,
request: ExecuteRequest,
caller_cancellation: CancellationToken,
yield_signal: Option<CancellationToken>,
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
},
Wait {
session: RemoteSession,
request: WaitRequest,
caller_cancellation: CancellationToken,
yield_signal: Option<CancellationToken>,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
},
Terminate {
session: RemoteSession,
request: TerminateRequest,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
},
ShutdownSession {
session: RemoteSession,
response_tx: oneshot::Sender<Result<(), String>>,
},
}
pub(in crate::remote_session::connection) enum DriverEvent {
HostMessage(HostToClient),
DelegateCompleted {
id: DelegateRequestId,
result: Result<DelegateResponse, String>,
},
RequestCancelled(RequestId),
YieldRequest(RequestId),
Failed(String),
}
pub(super) struct CancellableRequest {
caller_cancellation: CancellationToken,
watcher_stop: CancellationToken,
reported: bool,
}
impl CancellableRequest {
pub(super) fn new(caller_cancellation: CancellationToken) -> Self {
Self {
caller_cancellation,
watcher_stop: CancellationToken::new(),
reported: false,
}
}
pub(super) fn is_cancelled(&self) -> bool {
self.caller_cancellation.is_cancelled()
}
pub(super) fn mark_reported(&mut self) -> bool {
if self.reported {
return false;
}
self.reported = true;
true
}
pub(super) fn spawn_watcher(&self, id: RequestId, event_tx: mpsc::Sender<DriverEvent>) {
let caller_cancellation = self.caller_cancellation.clone();
let watcher_stop = self.watcher_stop.clone();
tokio::spawn(async move {
tokio::select! {
_ = caller_cancellation.cancelled() => {
let _ = event_tx.send(DriverEvent::RequestCancelled(id)).await;
}
_ = watcher_stop.cancelled() => {}
}
});
}
}
impl Drop for CancellableRequest {
fn drop(&mut self) {
self.watcher_stop.cancel();
}
}
/// Owns the watcher until an observation's final response is received.
pub(super) struct ObservationYield {
signal: Option<CancellationToken>,
watcher: Option<AbortOnDropHandle<()>>,
}
impl ObservationYield {
pub(super) fn new(signal: Option<CancellationToken>) -> Self {
Self {
signal,
watcher: None,
}
}
pub(super) fn spawn_watcher(&mut self, id: RequestId, event_tx: mpsc::Sender<DriverEvent>) {
let Some(signal) = self.signal.take() else {
return;
};
self.watcher = Some(AbortOnDropHandle::new(tokio::spawn(async move {
signal.cancelled().await;
let _ = event_tx.send(DriverEvent::YieldRequest(id)).await;
})));
}
}
pub(super) struct InitialResponse {
pub(super) generation: u64,
pub(super) cell_id: WireCellId,
pub(super) response_tx: oneshot::Sender<Result<RuntimeResponse, String>>,
pub(super) _yield_observation: ObservationYield,
}
pub(in crate::remote_session::connection) struct DeliveredExecute {
pub(in crate::remote_session::connection) request_id: RequestId,
pub(in crate::remote_session::connection) started: StartedCell,
}
pub(super) struct UnclaimedExecute {
pub(super) session: RemoteSession,
pub(super) cell_id: WireCellId,
pub(super) cancellation: CancellableRequest,
}
pub(super) enum PendingRequest {
OpenSession {
session: RemoteSession,
cleanup: SessionCleanup,
cancellation: CancellableRequest,
response_tx: oneshot::Sender<Result<(), String>>,
},
Execute {
session: RemoteSession,
delegate: Arc<dyn CodeModeSessionDelegate>,
response_tx: oneshot::Sender<Result<DeliveredExecute, String>>,
initial_response_tx: oneshot::Sender<Result<RuntimeResponse, String>>,
initial_response_rx: oneshot::Receiver<Result<RuntimeResponse, String>>,
cancellation: CancellableRequest,
yield_observation: ObservationYield,
},
Wait {
session: RemoteSession,
cell_id: WireCellId,
cancellation: CancellableRequest,
yield_observation: ObservationYield,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
},
Terminate {
session: RemoteSession,
cell_id: WireCellId,
response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
},
ShutdownSession {
session: RemoteSession,
response_tx: oneshot::Sender<Result<(), String>>,
},
}
pub(super) struct DeferredWait {
pub(super) session: RemoteSession,
pub(super) request: WireWaitRequest,
pub(super) max_output_tokens: Option<usize>,
pub(super) caller_cancellation: CancellationToken,
pub(super) yield_signal: Option<CancellationToken>,
pub(super) response_tx: oneshot::Sender<Result<WaitOutcome, String>>,
}
impl PendingRequest {
pub(super) fn cancellation_mut(&mut self) -> Option<&mut CancellableRequest> {
match self {
Self::OpenSession { cancellation, .. }
| Self::Execute { cancellation, .. }
| Self::Wait { cancellation, .. } => Some(cancellation),
Self::Terminate { .. } | Self::ShutdownSession { .. } => None,
}
}
pub(super) fn yield_observation(&mut self) -> Option<&mut ObservationYield> {
match self {
Self::Execute {
yield_observation, ..
}
| Self::Wait {
yield_observation, ..
} => Some(yield_observation),
_ => None,
}
}
pub(super) fn fail(self, reason: String) {
match self {
Self::OpenSession { response_tx, .. } | Self::ShutdownSession { response_tx, .. } => {
let _ = response_tx.send(Err(reason));
}
Self::Execute { response_tx, .. } => {
let _ = response_tx.send(Err(reason));
}
Self::Wait { response_tx, .. } | Self::Terminate { response_tx, .. } => {
let _ = response_tx.send(Err(reason));
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
use akode_code_mode_protocol::host::FramedReader;
use tokio::process::ChildStdout;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use super::driver::DriverEvent;
pub(super) async fn drive_reader(
mut reader: FramedReader<ChildStdout>,
events: mpsc::Sender<DriverEvent>,
cancellation: CancellationToken,
) -> Result<(), String> {
loop {
let message = tokio::select! {
_ = cancellation.cancelled() => return Ok(()),
result = reader.read() => result,
};
let message = match message {
Ok(Some(message)) => message,
Ok(None) => return Err("code-mode host closed its stdout".to_string()),
Err(err) => return Err(format!("failed to read code-mode host message: {err}")),
};
events
.send(DriverEvent::HostMessage(message))
.await
.map_err(|_| "code-mode connection driver closed".to_string())?;
}
}

View File

@@ -0,0 +1,73 @@
//! Restores per-execution trace context for callbacks from the shared stdio host.
//!
//! Keep only a W3C snapshot: retaining the caller's tracing span would extend a yielded
//! exec's duration until its background cell closes. The gRPC transport has its own
//! callback propagation and does not use this wrapper.
//! Instrument manually because `#[instrument]` enters the span before its body runs,
//! which is too late to assign the remote OpenTelemetry parent.
use std::sync::Arc;
use akode_code_mode_protocol::CellId;
use akode_code_mode_protocol::CodeModeNestedToolCall;
use akode_code_mode_protocol::CodeModeSessionDelegate;
use akode_code_mode_protocol::NotificationFuture;
use akode_code_mode_protocol::ToolInvocationFuture;
use akode_protocol::protocol::W3cTraceContext;
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
pub(super) struct TracedCellDelegate {
pub(super) delegate: Arc<dyn CodeModeSessionDelegate>,
pub(super) trace: Option<W3cTraceContext>,
}
impl TracedCellDelegate {
fn with_parent(&self, span: tracing::Span) -> tracing::Span {
// Set the remote parent before entering the span: OTEL starts it on entry.
if let Some(trace) = &self.trace {
akode_otel::set_parent_from_w3c_trace_context(&span, trace);
}
span
}
}
impl CodeModeSessionDelegate for TracedCellDelegate {
fn invoke_tool<'a>(
&'a self,
invocation: CodeModeNestedToolCall,
cancellation_token: CancellationToken,
) -> ToolInvocationFuture<'a> {
let span = self.with_parent(tracing::info_span!(parent: None, "code_mode.stdio.callback"));
Box::pin(
async move {
self.delegate
.invoke_tool(invocation, cancellation_token)
.await
}
.instrument(span),
)
}
fn notify<'a>(
&'a self,
call_id: String,
cell_id: CellId,
text: String,
cancellation_token: CancellationToken,
) -> NotificationFuture<'a> {
let span = self.with_parent(tracing::info_span!(parent: None, "code_mode.stdio.notify"));
Box::pin(
async move {
self.delegate
.notify(call_id, cell_id, text, cancellation_token)
.await
}
.instrument(span),
)
}
fn cell_closed(&self, cell_id: &CellId) {
self.delegate.cell_closed(cell_id);
}
}