diff --git a/Cargo.lock b/Cargo.lock index 4942fbd..f61d677 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,6 +106,7 @@ dependencies = [ "tempfile", "tokio", "tokio-stream", + "tokio-tungstenite", "tokio-util", "tracing", ] @@ -276,6 +277,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "bumpalo" version = "3.20.3" @@ -312,6 +322,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core", +] + [[package]] name = "chrono" version = "0.4.45" @@ -373,6 +394,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -388,6 +415,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -398,14 +434,40 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid", + "crypto-common 0.2.2", ] [[package]] @@ -609,6 +671,7 @@ dependencies = [ "cfg-if", "libc", "r-efi", + "rand_core", ] [[package]] @@ -647,6 +710,31 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "iana-time-zone" version = "0.1.65" @@ -868,6 +956,23 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + [[package]] name = "redox_users" version = "0.5.2" @@ -1051,6 +1156,17 @@ dependencies = [ "serde_core", ] +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + [[package]] name = "sha2" version = "0.10.9" @@ -1058,8 +1174,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", ] [[package]] @@ -1099,6 +1215,16 @@ version = "1.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "strsim" version = "0.11.1" @@ -1179,6 +1305,7 @@ dependencies = [ "mio", "pin-project-lite", "signal-hook-registry", + "socket2", "tokio-macros", "windows-sys", ] @@ -1206,6 +1333,18 @@ dependencies = [ "tokio-util", ] +[[package]] +name = "tokio-tungstenite" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17a073bfed563fa236697a068031408a93cd9522e08abf9933ead3e73411bd71" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + [[package]] name = "tokio-util" version = "0.7.18" @@ -1332,6 +1471,22 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "tungstenite" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48ac77174b19c110a50ab2128b24215ac9cb40e0e12e093fb602d175c569d22" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand", + "sha1", + "thiserror", +] + [[package]] name = "typenum" version = "1.20.1" diff --git a/Cargo.toml b/Cargo.toml index 249c5e9..d146c1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,9 +48,10 @@ serde_json = "1.0" sha2 = "0.10" tempfile = "3.20" thiserror = "2.0" -tokio = { version = "1.47", features = ["fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync", "time"] } +tokio = { version = "1.47", features = ["net", "fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync", "time"] } tokio-stream = { version = "0.1", features = ["sync"] } tokio-util = { version = "0.7", features = ["codec"] } +tokio-tungstenite = { version = "0.30", default-features = false, features = ["handshake"] } toml = "0.9" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] } diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index 973c7a5..febcade 100644 --- a/crates/cli/src/app.rs +++ b/crates/cli/src/app.rs @@ -1,3 +1,5 @@ +// ABOUTME: Coordinates native provider launches, handoffs, and canonical history capture. +// ABOUTME: Enforces workspace and session integrity across CLI operations. //! Application composition root. This is the only layer that knows concrete //! storage, provider protocol, native process, and workspace types. @@ -909,7 +911,7 @@ async fn reconcile_codex_launch( mark_codex_launch_uncertain(store, config, launch, &error)?; return Err(error); } - capture_codex_native_history(store, paths, config, session, identity, &native_session) + capture_codex_native_history(store, paths, config, session, &native_session) .await .context("failed to reconcile the unfinished native Codex transcript")?; let switching_provider = @@ -1299,11 +1301,10 @@ async fn run_native_codex( &native_session.native_session_id, )?; - // Reopen the official app-server only after the native CLI releases the thread, - // then capture the native delta into the canonical log. + // Read persisted native turns through the official app-server without + // acquiring the thread's writer, then capture its terminal delta. let capture = - capture_codex_native_history(store, paths, config, session, identity, &native_session) - .await?; + capture_codex_native_history(store, paths, config, session, &native_session).await?; store.update_native_launch( launch_id, NativeLaunchState::Captured, @@ -1508,26 +1509,16 @@ async fn capture_codex_native_history( paths: &AgentctlPaths, config: &Config, session: &UnifiedSession, - identity: &WorkspaceIdentity, native_session: &NativeSession, ) -> Result { let provider = load_native_provider(paths, config, &ProviderKind::Codex).await?; - let context = SessionContext { - unified_session_id: session.id, - workspace_root: identity.execution_root().to_path_buf(), - workspace_fingerprint: identity.fingerprint.clone(), - auth_mode: session.auth_mode, - }; let captured = async { - let restored = provider - .restore_session(&context, native_session.clone()) - .await?; - let transcript = provider.read_native_history(&restored).await?; + let transcript = provider.read_native_history(native_session).await?; operations::persist_native_capture( store, &config.payload_guard()?, session, - &restored, + native_session, &transcript, ) } @@ -1620,7 +1611,7 @@ async fn load_native_provider( kind: &ProviderKind, ) -> Result> { match kind { - ProviderKind::Codex => Ok(Arc::new(CodexAdapter::new( + ProviderKind::Codex => Ok(Arc::new(CodexAdapter::for_interactive_sessions( &config.providers.codex_binary, Some(paths.codex_protocol_root()), ))), @@ -2192,6 +2183,61 @@ mod routing_tests { } } + #[tokio::test] + #[ignore = "requires a locally installed Codex CLI"] + async fn installed_codex_capture_reads_history_without_acquiring_its_writer() { + let directory = tempfile::tempdir().unwrap(); + let paths = AgentctlPaths::resolve(Some(directory.path().join("home"))).unwrap(); + let store = operations::open_store(&paths).unwrap(); + let workspace = tempfile::tempdir().unwrap(); + let config = Config::default(); + let session = create_session( + &store, + &config, + workspace.path(), + Some("capture-live-probe"), + Some(ProviderKind::Codex), + ) + .unwrap(); + let identity = WorkspaceIdentity::discover(workspace.path()).unwrap(); + let context = SessionContext { + unified_session_id: session.id, + workspace_root: identity.execution_root().to_path_buf(), + workspace_fingerprint: identity.fingerprint.clone(), + auth_mode: session.auth_mode, + }; + let owner = CodexAdapter::new("codex", None); + let native = owner.ensure_session(&context).await.unwrap(); + let now = Utc::now(); + store + .upsert_provider_session(&ProviderSessionRecord { + id: native.id, + unified_session_id: session.id, + provider: native.provider.clone(), + native_session_id: native.native_session_id.clone(), + native_version: native.native_version.clone(), + last_synced_seq: 0, + status: ProviderStatus::Ready, + reset_at: None, + capabilities: native.capabilities.clone(), + metadata: serde_json::json!({}), + created_at: now, + updated_at: now, + }) + .unwrap(); + + let captured = + capture_codex_native_history(&store, &paths, &config, &session, &native).await; + owner.shutdown().await.unwrap(); + captured.unwrap(); + assert!( + store + .list_events(session.id, 0, usize::MAX) + .unwrap() + .is_empty() + ); + } + #[test] fn stale_transient_health_does_not_trigger_post_native_failover() { let directory = tempfile::tempdir().unwrap(); diff --git a/crates/provider-codex/Cargo.toml b/crates/provider-codex/Cargo.toml index a1a58ce..3164451 100644 --- a/crates/provider-codex/Cargo.toml +++ b/crates/provider-codex/Cargo.toml @@ -26,6 +26,7 @@ tracing.workspace = true [target.'cfg(unix)'.dependencies] nix.workspace = true +tokio-tungstenite.workspace = true [dev-dependencies] tempfile.workspace = true diff --git a/crates/provider-codex/src/adapter.rs b/crates/provider-codex/src/adapter.rs index fe81068..33d6be6 100644 --- a/crates/provider-codex/src/adapter.rs +++ b/crates/provider-codex/src/adapter.rs @@ -1,3 +1,5 @@ +// ABOUTME: Connects Codex native sessions to canonical events and provider operations. +// ABOUTME: Reads native history and manages the official app-server protocol. use std::{ collections::{BTreeMap, HashMap, HashSet}, path::{Path, PathBuf}, @@ -37,6 +39,8 @@ pub struct CodexConfig { pub schema_cache_root: Option, pub channel_capacity: usize, pub request_timeout: Duration, + /// Shares the native CLI service when it is running. + pub interactive: bool, } impl Default for CodexConfig { @@ -46,6 +50,7 @@ impl Default for CodexConfig { schema_cache_root: None, channel_capacity: 256, request_timeout: Duration::from_secs(30), + interactive: false, } } } @@ -112,6 +117,19 @@ impl CodexAdapter { }) } + /// Connects interactive sessions through the same native service as the Codex CLI. + pub fn for_interactive_sessions( + binary: impl Into, + schema_cache_root: Option, + ) -> Self { + Self::from_config(CodexConfig { + binary: binary.into(), + schema_cache_root, + interactive: true, + ..CodexConfig::default() + }) + } + pub fn from_config(config: CodexConfig) -> Self { Self { config, @@ -269,6 +287,23 @@ impl CodexAdapter { { return Ok(Arc::clone(client)); } + #[cfg(unix)] + let socket = if self.config.interactive { + crate::process::native_socket(&self.config.binary).await? + } else { + None + }; + #[cfg(unix)] + if let Some(socket) = socket { + let client = CodexRpcClient::connect( + &socket, + self.config.channel_capacity, + self.config.request_timeout, + ) + .await?; + *connection = Some(Arc::clone(&client)); + return Ok(client); + } let client = spawn_app_server( &self.config.binary, self.config.channel_capacity, @@ -527,7 +562,6 @@ impl AgentProvider for CodexAdapter { validate_session(session)?; self.require_installed_client_method("thread/read").await?; let client = self.client().await?; - self.ensure_thread_loaded(&client, session, None).await?; let response = client .request( "thread/read", diff --git a/crates/provider-codex/src/jsonrpc.rs b/crates/provider-codex/src/jsonrpc.rs index 72f9d40..39bee18 100644 --- a/crates/provider-codex/src/jsonrpc.rs +++ b/crates/provider-codex/src/jsonrpc.rs @@ -1,3 +1,5 @@ +// ABOUTME: Exchanges bounded Codex protocol messages over owned or shared connections. +// ABOUTME: Correlates requests and closes only resources owned by this client. use std::{ collections::{HashMap, HashSet}, sync::{ @@ -9,15 +11,15 @@ use std::{ use agentctl_core::{ProviderError, ProviderKind}; use agentctl_workspace::{ProcessTree, configure_tokio_process_group}; -use futures::StreamExt; +use futures::{Sink, SinkExt, Stream, StreamExt}; use serde_json::{Value, json}; use tokio::{ - io::{AsyncWriteExt, BufReader}, - process::{Child, ChildStdin, Command}, + io::BufReader, + process::{Child, Command}, sync::{Mutex, broadcast, mpsc, oneshot}, time::timeout, }; -use tokio_util::codec::{FramedRead, LinesCodec}; +use tokio_util::codec::{FramedRead, FramedWrite, LinesCodec}; const MAX_PROTOCOL_LINE_BYTES: usize = 8 * 1024 * 1024; type PendingSender = oneshot::Sender>; @@ -54,14 +56,15 @@ pub(crate) struct RpcResponse { #[derive(Debug)] struct OutboundLine(Value); -/// Bounded, multiplexed JSONL transport for one `codex app-server` process. +/// Bounded, multiplexed protocol connection to a Codex session service. #[derive(Debug)] pub(crate) struct CodexRpcClient { outbound: mpsc::Sender, pending: PendingRequests, inbound: broadcast::Sender, - child: Mutex, - process_tree: ProcessTree, + child: Mutex>, + process_tree: Option, + tasks: Mutex>, next_id: std::sync::atomic::AtomicU64, request_timeout: Duration, closed: Arc, @@ -119,17 +122,36 @@ impl CodexRpcClient { outbound, pending: Arc::clone(&pending), inbound: inbound.clone(), - child: Mutex::new(child), - process_tree, + child: Mutex::new(Some(child)), + process_tree: Some(process_tree), + tasks: Mutex::new(Vec::new()), next_id: std::sync::atomic::AtomicU64::new(1), request_timeout, closed: Arc::clone(&closed), loaded_threads: Mutex::new(HashSet::new()), }); - tokio::spawn(write_loop(stdin, outbound_rx, Arc::clone(&closed))); - tokio::spawn(read_loop(stdout, pending, inbound, closed)); - tokio::spawn(async move { + let writer = FramedWrite::new( + stdin, + LinesCodec::new_with_max_length(MAX_PROTOCOL_LINE_BYTES), + ) + .with(|text: String| { + futures::future::ready(Ok::(text)) + }) + .sink_map_err(|error| { + ProviderError::Process(format!("Codex protocol write failed: {error}")) + }); + let reader = FramedRead::new( + BufReader::new(stdout), + LinesCodec::new_with_max_length(MAX_PROTOCOL_LINE_BYTES), + ) + .map(|line| line.map_err(|error| error.to_string())); + let mut tasks = client.tasks.lock().await; + tasks.push( + tokio::spawn(write_loop(writer, outbound_rx, Arc::clone(&closed))).abort_handle(), + ); + tasks.push(tokio::spawn(read_loop(reader, pending, inbound, closed)).abort_handle()); + tasks.push(tokio::spawn(async move { let mut lines = FramedRead::new( BufReader::new(stderr), LinesCodec::new_with_max_length(MAX_PROTOCOL_LINE_BYTES), @@ -147,12 +169,87 @@ impl CodexRpcClient { } } } - }); + }).abort_handle()); + drop(tasks); client.initialize().await?; Ok(client) } + #[cfg(unix)] + pub(crate) async fn connect( + socket_path: &std::path::Path, + channel_capacity: usize, + request_timeout: Duration, + ) -> Result, ProviderError> { + use tokio::net::UnixStream; + use tokio_tungstenite::{ + client_async_with_config, + tungstenite::{Message, protocol::WebSocketConfig}, + }; + + let stream = timeout(request_timeout, UnixStream::connect(socket_path)) + .await + .map_err(|_| { + ProviderError::Process("native Codex socket connection timed out".to_owned()) + })??; + let configuration = WebSocketConfig::default() + .max_message_size(Some(MAX_PROTOCOL_LINE_BYTES)) + .max_frame_size(Some(MAX_PROTOCOL_LINE_BYTES)); + let (websocket, _) = timeout( + request_timeout, + client_async_with_config("ws://localhost/", stream, Some(configuration)), + ) + .await + .map_err(|_| ProviderError::Process("native Codex handshake timed out".to_owned()))? + .map_err(|error| { + ProviderError::Protocol(format!("native Codex handshake failed: {error}")) + })?; + let (writer, reader) = websocket.split(); + let writer = writer + .with(|text: String| { + futures::future::ready(Ok::( + Message::text(text), + )) + }) + .sink_map_err(|error| { + ProviderError::Process(format!("native Codex write failed: {error}")) + }); + let reader = reader.filter_map(|frame| { + futures::future::ready(match frame { + Ok(Message::Text(text)) => Some(Ok(text.to_string())), + Ok(Message::Ping(_) | Message::Pong(_)) => None, + Ok(Message::Close(_)) => Some(Err("native Codex connection closed".to_owned())), + Ok(_) => Some(Err("native Codex sent a non-text protocol frame".to_owned())), + Err(error) => Some(Err(error.to_string())), + }) + }); + let (outbound, outbound_rx) = mpsc::channel(channel_capacity.max(1)); + let (inbound, _) = broadcast::channel(channel_capacity.max(16)); + let pending = Arc::new(Mutex::new(HashMap::new())); + let closed = Arc::new(AtomicBool::new(false)); + let client = Arc::new(Self { + outbound, + pending: Arc::clone(&pending), + inbound: inbound.clone(), + child: Mutex::new(None), + process_tree: None, + tasks: Mutex::new(Vec::new()), + next_id: std::sync::atomic::AtomicU64::new(1), + request_timeout, + closed: Arc::clone(&closed), + loaded_threads: Mutex::new(HashSet::new()), + }); + let mut tasks = client.tasks.lock().await; + tasks.push( + tokio::spawn(write_loop(writer, outbound_rx, Arc::clone(&closed))).abort_handle(), + ); + tasks.push(tokio::spawn(read_loop(reader, pending, inbound, closed)).abort_handle()); + drop(tasks); + client.initialize().await?; + Ok(client) + } + async fn initialize(&self) -> Result<(), ProviderError> { self.request( "initialize", @@ -249,36 +346,49 @@ impl CodexRpcClient { } pub(crate) async fn shutdown(&self) -> Result<(), ProviderError> { + self.closed.store(true, Ordering::Release); + for task in self.tasks.lock().await.iter() { + task.abort(); + } + fail_pending(&self.pending, "Codex connection shut down".to_owned()).await; let mut child = self.child.lock().await; - let root_running = child.try_wait().map_err(ProviderError::Io)?.is_none(); - let terminated = self - .process_tree - .terminate() - .map_err(|error| ProviderError::Process(error.to_string())); - if root_running { - child.wait().await.map_err(ProviderError::Io)?; + if let (Some(child), Some(process_tree)) = (child.as_mut(), self.process_tree.as_ref()) { + let root_running = child.try_wait().map_err(ProviderError::Io)?.is_none(); + let terminated = process_tree + .terminate() + .map_err(|error| ProviderError::Process(error.to_string())); + if root_running { + child.wait().await.map_err(ProviderError::Io)?; + } + terminated?; + } + Ok(()) + } +} + +impl Drop for CodexRpcClient { + fn drop(&mut self) { + for task in self.tasks.get_mut().iter() { + task.abort(); } - terminated } } async fn write_loop( - mut stdin: ChildStdin, + mut writer: impl Sink + Unpin, mut receiver: mpsc::Receiver, closed: Arc, ) { while let Some(OutboundLine(value)) = receiver.recv().await { - let encoded = match serde_json::to_vec(&value) { + let encoded = match serde_json::to_string(&value) { Ok(encoded) => encoded, Err(error) => { tracing::error!(provider = %ProviderKind::Codex, %error, "failed to encode request"); continue; } }; - if stdin.write_all(&encoded).await.is_err() - || stdin.write_all(b"\n").await.is_err() - || stdin.flush().await.is_err() - { + if let Err(error) = writer.send(encoded).await { + tracing::warn!(provider = %ProviderKind::Codex, %error, "Codex connection write failed"); break; } } @@ -286,15 +396,11 @@ async fn write_loop( } async fn read_loop( - stdout: tokio::process::ChildStdout, + mut lines: impl Stream> + Unpin, pending: PendingRequests, inbound: broadcast::Sender, closed: Arc, ) { - let mut lines = FramedRead::new( - BufReader::new(stdout), - LinesCodec::new_with_max_length(MAX_PROTOCOL_LINE_BYTES), - ); while let Some(line) = lines.next().await { let line = match line { Ok(line) => line, @@ -511,11 +617,14 @@ sleep 1 .trim() .is_empty() ); - let group = client.process_tree.id(); - tokio::time::timeout(Duration::from_secs(2), client.child.lock().await.wait()) - .await - .unwrap() - .unwrap(); + let group = client.process_tree.as_ref().unwrap().id(); + tokio::time::timeout( + Duration::from_secs(2), + client.child.lock().await.as_mut().unwrap().wait(), + ) + .await + .unwrap() + .unwrap(); assert!(process_group_exists(group).unwrap()); client.shutdown().await.unwrap(); diff --git a/crates/provider-codex/src/process.rs b/crates/provider-codex/src/process.rs index 0c91370..8dc1105 100644 --- a/crates/provider-codex/src/process.rs +++ b/crates/provider-codex/src/process.rs @@ -1,3 +1,5 @@ +// ABOUTME: Opens owned Codex helpers and discovers the native session service. +// ABOUTME: Selects connections without interrupting other native sessions. use std::{path::Path, sync::Arc, time::Duration}; use agentctl_core::ProviderError; @@ -11,3 +13,103 @@ pub(crate) async fn spawn_app_server( ) -> Result, ProviderError> { CodexRpcClient::spawn(binary, channel_capacity, request_timeout).await } + +#[cfg(unix)] +pub(crate) async fn native_socket( + binary: &Path, +) -> Result, ProviderError> { + use directories::BaseDirs; + use serde_json::Value; + use std::os::unix::fs::FileTypeExt; + use tokio::process::Command; + + let home = match std::env::var_os("CODEX_HOME") { + Some(home) => std::path::PathBuf::from(home), + None => BaseDirs::new() + .ok_or_else(|| ProviderError::Process("could not determine Codex home".to_owned()))? + .home_dir() + .join(".codex"), + }; + let socket = home.join("app-server-control/app-server-control.sock"); + match tokio::fs::symlink_metadata(&socket).await { + Ok(_) => { + let metadata = tokio::fs::metadata(&socket).await?; + if !metadata.file_type().is_socket() { + return Err(ProviderError::Incompatible( + "native Codex control endpoint is not a Unix socket".to_owned(), + )); + } + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(ProviderError::Io(error)), + } + let mut command = Command::new(binary); + command.args(["app-server", "daemon", "version"]); + let output = + crate::schema::run_output(command, binary, "native Codex daemon discovery").await?; + if !output.status.success() { + return Err(ProviderError::Process(format!( + "native Codex daemon discovery failed: {}", + String::from_utf8_lossy(&output.stderr).trim() + ))); + } + let report: Value = serde_json::from_slice(&output.stdout).map_err(|error| { + ProviderError::Protocol(format!("invalid native daemon report: {error}")) + })?; + validate_native_daemon(&report, &socket)?; + Ok(Some(socket)) +} + +#[cfg(unix)] +fn validate_native_daemon(report: &serde_json::Value, socket: &Path) -> Result<(), ProviderError> { + if report["status"] != "running" || report["socketPath"].as_str() != socket.to_str() { + return Err(ProviderError::Incompatible( + "native Codex daemon report does not match its control socket".to_owned(), + )); + } + if report["cliVersion"].as_str().is_none() || report["cliVersion"] != report["appServerVersion"] + { + return Err(ProviderError::Incompatible( + "native Codex daemon and selected CLI versions differ; use matching versions before switching providers".to_owned(), + )); + } + Ok(()) +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use serde_json::json; + + fn report() -> serde_json::Value { + json!({ + "status": "running", + "socketPath": "/tmp/native-codex.sock", + "cliVersion": "0.159.2", + "appServerVersion": "0.159.2" + }) + } + + #[test] + fn native_daemon_requires_matching_cli_version_and_socket() { + validate_native_daemon(&report(), Path::new("/tmp/native-codex.sock")).unwrap(); + } + + #[test] + fn native_daemon_rejects_missing_or_mismatched_versions() { + let mut response = report(); + response["appServerVersion"] = json!("0.158.0"); + assert!(validate_native_daemon(&response, Path::new("/tmp/native-codex.sock")).is_err()); + response["cliVersion"] = serde_json::Value::Null; + response["appServerVersion"] = serde_json::Value::Null; + assert!(validate_native_daemon(&response, Path::new("/tmp/native-codex.sock")).is_err()); + } + + #[test] + fn native_daemon_rejects_other_sockets_and_unconfirmed_running_state() { + assert!(validate_native_daemon(&report(), Path::new("/tmp/other.sock")).is_err()); + let mut response = report(); + response["status"] = json!("unknown"); + assert!(validate_native_daemon(&response, Path::new("/tmp/native-codex.sock")).is_err()); + } +} diff --git a/crates/provider-codex/src/schema.rs b/crates/provider-codex/src/schema.rs index a2976a1..aa7b17d 100644 --- a/crates/provider-codex/src/schema.rs +++ b/crates/provider-codex/src/schema.rs @@ -1,3 +1,5 @@ +// ABOUTME: Detects Codex installations and caches their official protocol schemas. +// ABOUTME: Runs bounded discovery commands and protects private schema artifacts. use std::{ fs::{File, OpenOptions}, path::{Path, PathBuf}, @@ -234,7 +236,7 @@ async fn valid_client_request_schema(directory: &Path) -> bool { serde_json::from_slice::(&encoded).is_ok() } -async fn run_output( +pub(crate) async fn run_output( mut command: Command, binary: &Path, description: &str, diff --git a/crates/provider-codex/tests/live_probe.rs b/crates/provider-codex/tests/live_probe.rs index e24a0ab..27e1b92 100644 --- a/crates/provider-codex/tests/live_probe.rs +++ b/crates/provider-codex/tests/live_probe.rs @@ -1,3 +1,5 @@ +// ABOUTME: Exercises the installed Codex app-server against disposable native sessions. +// ABOUTME: Verifies protocol behavior without submitting conversational model turns. use agentctl_core::{ AgentProvider, AuthMode, CanonicalEvent, EventId, EventVisibility, ProviderStatus, SessionContext, SyncBatch, UnifiedSessionId, @@ -98,3 +100,135 @@ async fn installed_thread_injection_and_restart_resume() { ); restarted.shutdown().await.unwrap(); } + +#[tokio::test] +#[ignore = "requires a locally installed Codex CLI"] +async fn installed_native_history_can_be_read_while_another_client_owns_the_thread() { + let workspace = tempfile::TempDir::new().unwrap(); + let context = SessionContext { + unified_session_id: UnifiedSessionId::new(), + workspace_root: workspace.path().to_path_buf(), + workspace_fingerprint: "history-reader-live-probe".to_owned(), + auth_mode: AuthMode::NativeLocal, + }; + let owner = CodexAdapter::new("codex", None); + let session = owner.ensure_session(&context).await.unwrap(); + let reader = CodexAdapter::new("codex", None); + let transcript = reader.read_native_history(&session).await; + reader.shutdown().await.unwrap(); + owner.shutdown().await.unwrap(); + + let transcript = transcript.unwrap(); + assert_eq!(transcript.native_session_id, session.native_session_id); + assert_eq!( + transcript.workspace_cwd, + workspace.path().canonicalize().unwrap() + ); + assert!(transcript.turns.is_empty()); +} + +#[cfg(unix)] +#[tokio::test] +#[ignore = "requires a running authenticated native Codex daemon"] +async fn installed_interactive_projection_uses_the_native_thread_owner() { + use futures::{SinkExt, StreamExt}; + use tokio::net::UnixStream; + use tokio_tungstenite::{client_async, tungstenite::Message}; + + let home = std::env::var_os("CODEX_HOME").map_or_else( + || { + directories::BaseDirs::new() + .unwrap() + .home_dir() + .join(".codex") + }, + std::path::PathBuf::from, + ); + let socket = home.join("app-server-control/app-server-control.sock"); + let stream = UnixStream::connect(socket).await.unwrap(); + let (mut owner, _) = client_async("ws://localhost/", stream).await.unwrap(); + let mut request_id = 0; + macro_rules! request { + ($method:expr, $params:expr) => {{ + request_id += 1; + owner.send(Message::text(json!({ + "id": request_id, "method": $method, "params": $params + }).to_string())).await.unwrap(); + loop { + let frame = owner.next().await.unwrap().unwrap(); + if let Message::Text(text) = frame { + let response: serde_json::Value = serde_json::from_str(&text).unwrap(); + if response["id"] == request_id { + assert!(response.get("error").is_none(), "{response}"); + break response["result"].clone(); + } + } + } + }}; + } + request!( + "initialize", + json!({ + "clientInfo": {"name": "agentctl", "version": "0.1.0"}, + "capabilities": {"experimentalApi": true} + }) + ); + owner + .send(Message::text(json!({"method": "initialized"}).to_string())) + .await + .unwrap(); + let workspace = tempfile::TempDir::new().unwrap(); + let started = request!("thread/start", json!({"cwd": workspace.path()})); + let thread_id = started["thread"]["id"].as_str().unwrap().to_owned(); + request!( + "thread/inject_items", + json!({"threadId": thread_id, "items": [{ + "type": "message", "role": "assistant", "content": [{ + "type": "output_text", "text": "Disposable native owner context" + }] + }]}) + ); + let context = SessionContext { + unified_session_id: UnifiedSessionId::new(), + workspace_root: workspace.path().to_path_buf(), + workspace_fingerprint: "native-owner-live-probe".to_owned(), + auth_mode: AuthMode::NativeLocal, + }; + let adapter = CodexAdapter::for_interactive_sessions("codex", None); + let attached = adapter.attach_existing_session(&context, &thread_id).await; + if let Ok(session) = &attached { + let receipt = adapter + .sync_context( + session, + SyncBatch { + from_seq_exclusive: 0, + through_seq_inclusive: 1, + projection_version: 1, + events: vec![CanonicalEvent { + schema_version: 1, + session_id: context.unified_session_id, + seq: 1, + event_id: EventId::new(), + turn_id: None, + origin_provider: None, + kind: "assistant_final".to_owned(), + visibility: EventVisibility::Projection, + payload: json!({"text": "Disposable projection context"}), + content_hash: "sha256:native-owner-live-probe".to_owned(), + raw_event_id: None, + created_at: Utc::now(), + }], + handoff: None, + }, + ) + .await + .unwrap(); + assert!(receipt.native_receipt.is_some()); + } + adapter.shutdown().await.unwrap(); + let resumed = request!("thread/resume", json!({"threadId": thread_id})); + assert_eq!(resumed["thread"]["id"], thread_id); + request!("thread/unsubscribe", json!({"threadId": thread_id})); + owner.close(None).await.unwrap(); + assert_eq!(attached.unwrap().native_session_id, thread_id); +} diff --git a/docs/protocols.md b/docs/protocols.md index 4697804..ab713b4 100644 --- a/docs/protocols.md +++ b/docs/protocols.md @@ -16,9 +16,15 @@ codex resume THREAD_ID --cd WORKTREE `agentctl` inherits stdin, stdout, and stderr so Codex retains its normal terminal UI, approvals, tools, MCP servers, configuration, and agent loop. -Around that foreground process, the Codex bridge launches `codex app-server`, -completes `initialize`/`initialized`, and communicates over bidirectional -JSON-RPC/JSONL. The control connection is used to: +On Unix, the interactive bridge connects to the running native Codex daemon +through its control socket using WebSocket JSON-RPC. It checks the daemon +report and requires its version to match the selected CLI. Closing the bridge +connection leaves the shared daemon and other native sessions running. + +When no native control endpoint exists, the bridge launches its own +`codex app-server` and uses JSON-RPC/JSONL over stdio. A discovered endpoint +that cannot be validated or connected produces an error. Both transports +complete `initialize`/`initialized`. The control connection is used to: - create or resume the mapped thread; - inject a missing canonical handoff with `thread/inject_items`; diff --git a/scripts/native-e2e-smoke.sh b/scripts/native-e2e-smoke.sh index 63bf346..75599e6 100755 --- a/scripts/native-e2e-smoke.sh +++ b/scripts/native-e2e-smoke.sh @@ -1,4 +1,6 @@ #!/usr/bin/env bash +# ABOUTME: Verifies native provider lifecycle and synchronization in a disposable workspace. +# ABOUTME: Checks writer exclusion, transcript capture, mappings, and repeated synchronization. set -Eeuo pipefail ROOT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P) @@ -306,13 +308,17 @@ jq -e ' note "running projection synchronization twice" ( cd -- "$WORKSPACE" - "$AGENTCTL_BIN" --home "$STATE_DIR" --json sync "$SESSION_NAME" >/dev/null + "$AGENTCTL_BIN" --home "$STATE_DIR" --json sync "$SESSION_NAME" >"$LOG_DIR/sync-one.json" ) +jq -e 'all(.providers[]; has("error") | not)' "$LOG_DIR/sync-one.json" >/dev/null \ + || die "provider synchronization failed: $(cat "$LOG_DIR/sync-one.json")" STATUS_AFTER_SYNC_ONE=$(status_json) ( cd -- "$WORKSPACE" - "$AGENTCTL_BIN" --home "$STATE_DIR" --json sync "$SESSION_NAME" >/dev/null + "$AGENTCTL_BIN" --home "$STATE_DIR" --json sync "$SESSION_NAME" >"$LOG_DIR/sync-two.json" ) +jq -e 'all(.providers[]; has("error") | not)' "$LOG_DIR/sync-two.json" >/dev/null \ + || die "provider synchronization failed: $(cat "$LOG_DIR/sync-two.json")" STATUS_AFTER_SYNC_TWO=$(status_json) jq -en \ diff --git a/scripts/native-pty-driver.exp b/scripts/native-pty-driver.exp index f92b397..759decb 100755 --- a/scripts/native-pty-driver.exp +++ b/scripts/native-pty-driver.exp @@ -1,4 +1,6 @@ #!/usr/bin/expect -f +# ABOUTME: Drives startup and exit of disposable provider-native interactive sessions. +# ABOUTME: Holds each native UI for lifecycle assertions without submitting model prompts. # Drives only the lifecycle boundary of a provider-native interactive CLI. # It deliberately never submits a conversational prompt. @@ -105,12 +107,16 @@ while {![native_child_started $child_pid $provider]} { } # Both providers can ask whether a previously unseen worktree is trusted. The -# smoke owns a brand-new disposable repository, so accepting the default choice +# smoke owns a brand-new disposable repository, so accepting the trust choice # is bounded to test data. If no trust screen appears, these are empty-editor # Enter events and do not start a model turn. set trust_at [expr {[clock milliseconds] + 2000}] while {[clock milliseconds] < $trust_at} { expect { + -re {Enter y/n:} { + after 250 + send -- "y\r" + } -re {Do you trust} { after 250 send -- "\r" @@ -133,6 +139,10 @@ set settle_seconds [expr {max(0, $startup_seconds - 2)}] set ready_at [expr {[clock milliseconds] + ($settle_seconds * 1000)}] while {[clock milliseconds] < $ready_at} { expect { + -re {Enter y/n:} { + after 250 + send -- "y\r" + } -re {Do you trust} { after 250 send -- "\r"