diff --git a/Cargo.lock b/Cargo.lock index 4942fbd..4ee0bdd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,7 @@ dependencies = [ "sha2", "tempfile", "tokio", + "tokio-tungstenite", "toml", "tracing", "uuid", @@ -106,6 +107,7 @@ dependencies = [ "tempfile", "tokio", "tokio-stream", + "tokio-tungstenite", "tokio-util", "tracing", ] @@ -276,6 +278,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 +323,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 +395,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 +416,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 +435,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 +672,7 @@ dependencies = [ "cfg-if", "libc", "r-efi", + "rand_core", ] [[package]] @@ -647,6 +711,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 +957,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 +1157,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 +1175,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 +1216,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 +1306,7 @@ dependencies = [ "mio", "pin-project-lite", "signal-hook-registry", + "socket2", "tokio-macros", "windows-sys", ] @@ -1206,6 +1334,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 +1472,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/Cargo.toml b/crates/cli/Cargo.toml index 495db14..f5dce6f 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -39,6 +39,7 @@ zstd.workspace = true [target.'cfg(unix)'.dependencies] nix.workspace = true +tokio-tungstenite.workspace = true [dev-dependencies] async-trait.workspace = true diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index 973c7a5..e4ed23b 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. @@ -903,13 +905,16 @@ async fn reconcile_codex_launch( ) .context("unfinished Codex launch has an invalid thread/list baseline")?; let current = snapshot_codex_threads(paths, config, identity).await?; - if let Err(error) = - validate_codex_thread_continuity(&baseline, ¤t, &launch.native_session_id) - { + if let Err(error) = validate_codex_launch_continuity( + &baseline, + ¤t, + &launch.native_session_id, + &launch.metadata, + ) { 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 = @@ -1293,17 +1298,20 @@ async fn run_native_codex( .await?; let finalized = async { let thread_after = snapshot_codex_threads(paths, config, identity).await?; - validate_codex_thread_continuity( + let launch = store + .native_launch(launch_id)? + .context("native Codex launch disappeared")?; + validate_codex_launch_continuity( &thread_baseline, &thread_after, &native_session.native_session_id, + &launch.metadata, )?; - // 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, @@ -1362,11 +1370,30 @@ async fn launch_journaled_codex( started_at, updated_at: started_at, })?; + #[cfg(unix)] + let relay = match crate::codex_launch::Relay::prepare( + Path::new(&config.providers.codex_binary), + store.clone(), + launch_id, + ) + .await + { + Ok(relay) => relay, + Err(error) => { + mark_native_launch_after_error(store, config, launch_id, &error, false, None)?; + return Err(error); + } + }; + #[cfg(unix)] + let endpoint = relay.as_ref().map(crate::codex_launch::Relay::endpoint); + #[cfg(not(unix))] + let endpoint: Option = None; let launched = native::launch_codex_with_spawn( &config.providers.codex_binary, identity.execution_root(), &native_session.native_session_id, native_args, + endpoint.as_deref(), |spawn| { store .record_native_launch_pid(launch_id, spawn.pid) @@ -1374,6 +1401,23 @@ async fn launch_journaled_codex( }, ) .await; + #[cfg(unix)] + let launched = match ( + launched, + async { + match relay { + Some(relay) => relay.finish().await, + None => Ok(()), + } + } + .await, + ) { + (Ok(exit), Ok(())) => Ok(exit), + (Err(error), Ok(())) | (Ok(_), Err(error)) => Err(error), + (Err(error), Err(teardown)) => { + Err(error.context(format!("native Codex relay also failed: {teardown}"))) + } + }; let native_exit = match launched { Ok(native_exit) => native_exit, Err(error) => { @@ -1470,6 +1514,24 @@ async fn snapshot_codex_threads( } } +fn validate_codex_launch_continuity( + before: &InteractiveThreadSnapshot, + after: &InteractiveThreadSnapshot, + mapped_thread_id: &str, + metadata: &serde_json::Value, +) -> Result<()> { + #[cfg(unix)] + if let Some(evidence) = metadata.get("codex_evidence") { + ensure!(before.cwd == after.cwd, "Codex launch workspace changed"); + return serde_json::from_value::(evidence.clone()) + .context("invalid native Codex thread-selection evidence")? + .validate(mapped_thread_id); + } + #[cfg(not(unix))] + let _ = metadata; + validate_codex_thread_continuity(before, after, mapped_thread_id) +} + fn validate_codex_thread_continuity( before: &InteractiveThreadSnapshot, after: &InteractiveThreadSnapshot, @@ -1508,26 +1570,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 +1672,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 +2244,157 @@ 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() + ); + } + + #[cfg(unix)] + #[tokio::test] + #[ignore = "requires a running native Codex daemon"] + async fn installed_codex_relay_persists_only_its_client_selections() { + use futures::{SinkExt, StreamExt}; + use tokio_tungstenite::{client_async, tungstenite::Message}; + let directory = tempfile::tempdir().unwrap(); + let paths = AgentctlPaths::resolve(Some(directory.path().join("home"))).unwrap(); + let store = operations::open_store(&paths).unwrap(); + let config = Config::default(); + let session = create_session( + &store, + &config, + directory.path(), + Some("relay-live-probe"), + Some(ProviderKind::Codex), + ) + .unwrap(); + let owner = CodexAdapter::for_interactive_sessions("codex", None); + let native = owner + .ensure_session(&SessionContext { + unified_session_id: session.id, + workspace_root: session.workspace_path.clone(), + workspace_fingerprint: session.workspace_fingerprint.clone(), + auth_mode: session.auth_mode, + }) + .await + .unwrap(); + let id = uuid::Uuid::now_v7(); + store + .start_native_launch(&NativeLaunchRecord { + id, + session_id: session.id, + provider: ProviderKind::Codex, + native_session_id: native.native_session_id.clone(), + workspace_lease_key: "relay-live-probe".to_owned(), + child_pid: None, + state: NativeLaunchState::Started, + exit_code: None, + error: None, + metadata: serde_json::json!({"preserved":true}), + started_at: Utc::now(), + updated_at: Utc::now(), + }) + .unwrap(); + let relay = crate::codex_launch::Relay::prepare(Path::new("codex"), store.clone(), id) + .await + .unwrap() + .unwrap(); + let (mut client, _) = client_async( + "ws://localhost/", + tokio::net::UnixStream::connect(relay.endpoint()) + .await + .unwrap(), + ) + .await + .unwrap(); + for (index, (method, params)) in [ + ("initialize", serde_json::json!({"clientInfo":{"name":"agentctl-relay-live-probe","version":"0.1.0"},"capabilities":{"experimentalApi":true}})), + ("thread/resume", serde_json::json!({"threadId":native.native_session_id})), + ("thread/fork", serde_json::json!({"threadId":native.native_session_id})), + ].into_iter().enumerate() { + client.send(Message::text(serde_json::json!({"id":index,"method":method,"params":params}).to_string())).await.unwrap(); + loop { + let frame = tokio::time::timeout(std::time::Duration::from_secs(30),client.next()).await.unwrap().unwrap().unwrap(); + if let Message::Text(text) = frame { + let response: serde_json::Value = serde_json::from_str(&text).unwrap(); + if response["id"] == index { + assert!(response.get("error").is_none(), "{response}"); + break; + } + } + } + if index == 0 { + client.send(Message::text(serde_json::json!({"method":"initialized"}).to_string())).await.unwrap(); + } + } + let pending: crate::codex_launch::Evidence = serde_json::from_value( + store.native_launch(id).unwrap().unwrap().metadata["codex_evidence"].clone(), + ) + .unwrap(); + assert!(!pending.complete); + assert_eq!(pending.selected_threads.len(), 2); + client.close(None).await.unwrap(); + relay.finish().await.unwrap(); + owner.shutdown().await.unwrap(); + let reopened = operations::open_store(&paths).unwrap(); + let launch = reopened.native_launch(id).unwrap().unwrap(); + assert_eq!(launch.metadata["preserved"], true); + let evidence: crate::codex_launch::Evidence = + serde_json::from_value(launch.metadata["codex_evidence"].clone()).unwrap(); + assert!(evidence.complete); + assert_eq!(evidence.pending_requests, 0); + assert!(evidence.validate(&native.native_session_id).is_err()); + } + #[test] fn stale_transient_health_does_not_trigger_post_native_failover() { let directory = tempfile::tempdir().unwrap(); @@ -3211,6 +3414,23 @@ mod tests { validate_codex_thread_continuity(&before, &after, "mapped").unwrap(); } + #[cfg(unix)] + #[test] + fn codex_thread_continuity_allows_timestamp_updates_in_existing_siblings() { + let cwd = PathBuf::from("/tmp/agentctl-worktree"); + let before = InteractiveThreadSnapshot { + cwd: cwd.clone(), + threads: vec![thread("mapped", "tree-a", 1), thread("other", "tree-b", 1)], + }; + let after = InteractiveThreadSnapshot { + cwd, + threads: vec![thread("mapped", "tree-a", 2), thread("other", "tree-b", 2)], + }; + validate_codex_launch_continuity(&before, &after, "mapped", &serde_json::json!({ + "codex_evidence": {"selected_threads": ["mapped"], "pending_requests": 0, "complete": true, "error": null} + })).unwrap(); + } + #[test] fn codex_thread_continuity_rejects_new_or_switched_existing_threads() { let cwd = PathBuf::from("/tmp/agentctl-worktree"); diff --git a/crates/cli/src/codex_launch.rs b/crates/cli/src/codex_launch.rs new file mode 100644 index 0000000..c3ad412 --- /dev/null +++ b/crates/cli/src/codex_launch.rs @@ -0,0 +1,356 @@ +// ABOUTME: Attributes native Codex thread selections to one CLI connection. +// ABOUTME: Persists bounded selection evidence before forwarding native protocol traffic. +use anyhow::{Result, ensure}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub(crate) struct Evidence { + pub selected_threads: Vec, + pub pending_requests: usize, + pub complete: bool, + pub error: Option, +} + +impl Evidence { + pub(crate) fn validate(&self, mapped: &str) -> Result<()> { + ensure!( + self.complete && self.pending_requests == 0 && self.error.is_none(), + "native Codex thread-selection evidence is incomplete or failed; automatic capture is blocked" + ); + ensure!( + !self.selected_threads.is_empty(), + "native Codex launch has no observed thread selection" + ); + ensure!( + self.selected_threads.iter().all(|thread| thread == mapped), + "Codex selected additional native threads during the mapped launch; resume the original mapped thread before switching providers" + ); + Ok(()) + } +} + +#[derive(Default)] +struct Selections { + evidence: Evidence, + pending: std::collections::HashSet, +} + +impl Selections { + fn request(&mut self, message: &serde_json::Value) -> Result { + if !matches!( + message["method"].as_str(), + Some("thread/start" | "thread/resume" | "thread/fork") + ) { + return Ok(false); + } + // Codex generates titles in a temporary thread without changing the conversation. + if message["method"] == "thread/start" + && message["params"]["ephemeral"] == true + && message["params"]["threadSource"] == "thread_title" + { + return Ok(false); + } + let id = message + .get("id") + .filter(|id| id.is_string() || id.is_number()) + .ok_or_else(|| anyhow::anyhow!("native Codex selection has no request identity"))?; + ensure!( + self.pending.len() < 4096, + "too many pending native Codex selections" + ); + ensure!( + self.pending.insert(id.to_string()), + "duplicate native Codex selection identity" + ); + self.evidence.pending_requests = self.pending.len(); + Ok(true) + } + + fn response(&mut self, message: &serde_json::Value) -> Result { + if message.get("method").is_some() { + return Ok(false); + } + let Some(id) = message.get("id") else { + return Ok(false); + }; + if !self.pending.contains(&id.to_string()) { + return Ok(false); + } + if message.get("error").is_none() { + let thread = message["result"]["thread"]["id"] + .as_str() + .filter(|id| !id.is_empty() && id.len() <= 256) + .ok_or_else(|| { + anyhow::anyhow!("native Codex selection response has no thread identity") + })?; + if !self + .evidence + .selected_threads + .iter() + .any(|known| known == thread) + { + ensure!( + self.evidence.selected_threads.len() < 4096, + "too many native Codex selected threads" + ); + self.evidence.selected_threads.push(thread.to_owned()); + } + } + self.pending.remove(&id.to_string()); + self.evidence.pending_requests = self.pending.len(); + Ok(true) + } +} + +pub(crate) struct Relay { + directory: tempfile::TempDir, + stop: Option>, + task: Option>>, +} + +impl Relay { + pub(crate) async fn prepare( + binary: &std::path::Path, + store: agentctl_storage::AgentctlStore, + launch_id: uuid::Uuid, + ) -> Result> { + agentctl_provider_codex::interactive_endpoint(binary) + .await? + .map(|endpoint| Self::start(&endpoint, store, launch_id)) + .transpose() + } + + fn start( + endpoint: &std::path::Path, + store: agentctl_storage::AgentctlStore, + launch_id: uuid::Uuid, + ) -> Result { + use std::os::unix::fs::PermissionsExt; + let directory = tempfile::Builder::new().prefix("acl-").tempdir()?; + let socket = directory.path().join("native.sock"); + let listener = tokio::net::UnixListener::bind(&socket)?; + std::fs::set_permissions(&socket, std::fs::Permissions::from_mode(0o600))?; + store + .update_codex_launch_evidence(launch_id, &serde_json::to_value(Evidence::default())?)?; + let (stop, mut stopped) = tokio::sync::oneshot::channel(); + let endpoint = endpoint.to_path_buf(); + let task = tokio::spawn(async move { + let mut selections = Selections::default(); + let outcome = async { + loop { + tokio::select! { + _ = &mut stopped => return Ok::<_, anyhow::Error>(()), + accepted = listener.accept() => { + let (stream, _) = accepted?; + relay_connection(stream, &endpoint, &mut selections, &store, launch_id).await?; + } + } + } + }.await; + selections.evidence.complete = outcome.is_ok(); + if outcome.is_err() { + selections.evidence.error = + Some("native Codex relay failed; automatic capture is blocked".to_owned()); + } + store.update_codex_launch_evidence( + launch_id, + &serde_json::to_value(&selections.evidence)?, + )?; + outcome + }); + Ok(Self { + directory, + stop: Some(stop), + task: Some(task), + }) + } + + pub(crate) fn endpoint(&self) -> std::path::PathBuf { + self.directory.path().join("native.sock") + } + + pub(crate) async fn finish(mut self) -> Result<()> { + if let Some(stop) = self.stop.take() { + ensure!( + stop.send(()).is_ok() + || self + .task + .as_ref() + .is_some_and(tokio::task::JoinHandle::is_finished), + "native Codex relay stopped unexpectedly" + ); + } + let task = self + .task + .as_mut() + .expect("relay task exists until completion"); + tokio::time::timeout(std::time::Duration::from_secs(5), task) + .await + .map_err(|_| anyhow::anyhow!("native Codex relay did not close after CLI exit"))???; + Ok(()) + } +} + +impl Drop for Relay { + fn drop(&mut self) { + if let Some(task) = &self.task { + task.abort(); + } + } +} + +async fn relay_connection( + stream: tokio::net::UnixStream, + endpoint: &std::path::Path, + selections: &mut Selections, + store: &agentctl_storage::AgentctlStore, + launch_id: uuid::Uuid, +) -> Result<()> { + use futures::{SinkExt, StreamExt}; + use tokio_tungstenite::{ + accept_async_with_config, client_async_with_config, + tungstenite::{Message, protocol::WebSocketConfig}, + }; + let configuration = WebSocketConfig::default() + .max_message_size(Some(64 * 1024 * 1024)) + .max_frame_size(Some(64 * 1024 * 1024)); + let mut native = tokio::time::timeout( + std::time::Duration::from_secs(30), + accept_async_with_config(stream, Some(configuration)), + ) + .await??; + let upstream = tokio::net::UnixStream::connect(endpoint).await?; + let (mut server, _) = tokio::time::timeout( + std::time::Duration::from_secs(30), + client_async_with_config("ws://localhost/", upstream, Some(configuration)), + ) + .await??; + loop { + tokio::select! { + frame = native.next() => { + let Some(frame) = frame else { return Ok(()); }; + let frame = frame?; + if matches!(frame, Message::Close(_)) { return Ok(()); } + if let Message::Text(text) = &frame + && selections.request(&serde_json::from_str(text)?)? { + store.update_codex_launch_evidence(launch_id, &serde_json::to_value(&selections.evidence)?)?; + } + server.send(frame).await?; + } + frame = server.next() => { + let frame = frame.ok_or_else(|| anyhow::anyhow!("native Codex service closed during the launch"))??; + if matches!(frame, Message::Close(_)) { anyhow::bail!("native Codex service closed during the launch"); } + if let Message::Text(text) = &frame + && selections.response(&serde_json::from_str(text)?)? { + store.update_codex_launch_evidence(launch_id, &serde_json::to_value(&selections.evidence)?)?; + } + native.send(frame).await?; + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn accepts_only_complete_evidence_for_the_mapped_thread() { + let evidence = Evidence { + selected_threads: vec!["mapped".to_owned()], + complete: true, + ..Evidence::default() + }; + evidence.validate("mapped").unwrap(); + assert!(evidence.validate("other").is_err()); + assert!(Evidence::default().validate("mapped").is_err()); + let pending = Evidence { + pending_requests: 1, + ..evidence.clone() + }; + assert!(pending.validate("mapped").is_err()); + let failed = Evidence { + error: Some("connection failed".to_owned()), + ..evidence.clone() + }; + assert!(failed.validate("mapped").is_err()); + } + + #[test] + fn observes_only_successful_selections_and_retains_pending_requests() { + use serde_json::json; + let mut selections = Selections::default(); + assert!( + !selections + .request(&json!({"id":0,"method":"thread/read","params":{"threadId":"sibling"}})) + .unwrap() + ); + for method in ["thread/start", "thread/fork", "thread/resume"] { + assert!( + selections + .request(&json!({"id":1,"method":method})) + .unwrap() + ); + assert_eq!(selections.evidence.pending_requests, 1); + assert!( + selections + .response(&json!({"id":1,"result":{"thread":{"id":"mapped"}}})) + .unwrap() + ); + assert_eq!(selections.evidence.pending_requests, 0); + } + assert_eq!(selections.evidence.selected_threads, vec!["mapped"]); + selections + .request(&json!({"id":"failed","method":"thread/resume"})) + .unwrap(); + selections + .response(&json!({"id":"failed","error":{"message":"not found"}})) + .unwrap(); + assert_eq!(selections.evidence.selected_threads, vec!["mapped"]); + selections + .request(&json!({"id":2,"method":"thread/start"})) + .unwrap(); + assert!(selections.response(&json!({"id":2,"result":{}})).is_err()); + assert_eq!(selections.evidence.pending_requests, 1); + } + + #[test] + fn background_title_generation_is_not_a_conversation_selection() { + let mut selections = Selections::default(); + assert!( + !selections + .request(&serde_json::json!({ + "id":"temporary-structured-title", "method":"thread/start", + "params":{"ephemeral":true,"threadSource":"thread_title"} + })) + .unwrap() + ); + assert_eq!(selections.evidence.pending_requests, 0); + assert!(selections.request(&serde_json::json!({ + "id":1,"method":"thread/start", "params":{"ephemeral":false,"threadSource":"thread_title"} + })).unwrap()); + assert!(selections.request(&serde_json::json!({ + "id":2,"method":"thread/start", "params":{"ephemeral":true,"threadSource":"unknown"} + })).unwrap()); + } + + #[test] + fn server_requests_cannot_resolve_a_native_selection() { + let mut selections = Selections::default(); + selections + .request(&serde_json::json!({"id":1,"method":"thread/resume"})) + .unwrap(); + assert!(!selections.response(&serde_json::json!({"id":1,"method":"item/commandExecution/requestApproval","params":{}})).unwrap()); + assert_eq!(selections.evidence.pending_requests, 1); + } + + #[test] + fn detects_switches_even_when_the_cli_returns_to_the_mapped_thread() { + let evidence = Evidence { + selected_threads: vec!["mapped".to_owned(), "other".to_owned()], + complete: true, + ..Evidence::default() + }; + assert!(evidence.validate("mapped").is_err()); + } +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index af6fae0..f1f7b5a 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,5 +1,9 @@ +// ABOUTME: Dispatches agentctl commands and reports bounded CLI failures. +// ABOUTME: Preserves native exit codes and blocking hook failure semantics. mod app; mod args; +#[cfg(unix)] +mod codex_launch; mod config; mod doctor; mod native; diff --git a/crates/cli/src/native.rs b/crates/cli/src/native.rs index c33471b..7842686 100644 --- a/crates/cli/src/native.rs +++ b/crates/cli/src/native.rs @@ -1,3 +1,5 @@ +// ABOUTME: Launches native provider CLIs with inherited terminal IO. +// ABOUTME: Journals child creation and bounds process-tree cleanup. use std::{ ffi::OsString, path::Path, @@ -104,6 +106,7 @@ pub(crate) async fn launch_codex_with_spawn( workspace: &Path, native_session_id: &str, native_args: &[OsString], + endpoint: Option<&Path>, on_spawn: F, ) -> Result where @@ -123,6 +126,11 @@ where .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .kill_on_drop(true); + if let Some(endpoint) = endpoint { + command + .arg("--remote") + .arg(format!("unix://{}", endpoint.display())); + } configure_tokio_process_group(&mut command); let child = command .spawn() @@ -639,6 +647,7 @@ mod tests { directory.path(), "native-session", &[], + None, move |spawn| { callback_observed.store(spawn.pid, Ordering::SeqCst); Ok(()) @@ -692,6 +701,7 @@ mod tests { directory.path(), "native-session", &[], + None, |_| bail!("simulated journal failure"), ) .await @@ -708,6 +718,7 @@ mod tests { directory.path(), "native-session", &[], + None, |_| Ok(()), ) .await @@ -723,6 +734,7 @@ mod tests { directory.path(), "native-session", &[], + None, |_| Ok(()), ) .await 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..9492abc 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::interactive_endpoint(&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/lib.rs b/crates/provider-codex/src/lib.rs index 016424c..7759b69 100644 --- a/crates/provider-codex/src/lib.rs +++ b/crates/provider-codex/src/lib.rs @@ -1,3 +1,5 @@ +// ABOUTME: Exposes Codex native session and protocol discovery operations. +// ABOUTME: Keeps transport internals behind the provider adapter boundary. //! Native Codex app-server adapter. mod adapter; @@ -6,6 +8,9 @@ mod mapping; mod process; mod schema; +#[cfg(unix)] +pub use process::interactive_endpoint; + pub use adapter::{ CodexAdapter, CodexConfig, InteractiveThreadIdentity, InteractiveThreadSnapshot, MAX_INTERACTIVE_THREAD_SNAPSHOT, diff --git a/crates/provider-codex/src/process.rs b/crates/provider-codex/src/process.rs index 0c91370..c75e2d9 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 async fn interactive_endpoint( + 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/crates/storage/src/sqlite.rs b/crates/storage/src/sqlite.rs index f23f1ba..7dcd553 100644 --- a/crates/storage/src/sqlite.rs +++ b/crates/storage/src/sqlite.rs @@ -1,3 +1,5 @@ +// ABOUTME: Persists canonical sessions and native lifecycle evidence in SQLite. +// ABOUTME: Validates bounded records and enforces durable state transitions. use std::{ collections::BTreeMap, fs::{self, OpenOptions}, @@ -595,6 +597,52 @@ impl SqliteStore { Ok(()) } + /// Persists launch-specific Codex observations without replacing other lifecycle metadata. + pub fn update_codex_launch_evidence( + &self, + id: uuid::Uuid, + evidence: &serde_json::Value, + ) -> Result<()> { + let mut connection = self.connection()?; + let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?; + let (provider, state, mut metadata): (ProviderKind, NativeLaunchState, serde_json::Value) = + transaction + .query_row( + "SELECT provider_json, state, metadata_json FROM native_launches WHERE id = ?1", + [id.to_string()], + |row| { + Ok(( + decode_row(row.get::<_, String>(0)?, 0)?, + NativeLaunchState::parse(&row.get::<_, String>(1)?)?, + decode_row(row.get::<_, String>(2)?, 2)?, + )) + }, + ) + .optional()? + .ok_or_else(|| StorageError::NotFound(format!("native launch {id}")))?; + if provider != ProviderKind::Codex + || !matches!( + state, + NativeLaunchState::Started | NativeLaunchState::Exited + ) + { + return Err(StorageError::InvalidData( + "Codex evidence requires an active Codex launch".to_owned(), + )); + } + metadata["codex_evidence"] = evidence.clone(); + validate_json(&metadata, self.limits)?; + expect_one( + transaction.execute( + "UPDATE native_launches SET metadata_json = ?2, updated_at = ?3 WHERE id = ?1", + params![id.to_string(), encode(&metadata)?, timestamp(Utc::now())], + )?, + format!("native launch {id}"), + )?; + transaction.commit()?; + Ok(()) + } + pub fn update_native_launch( &self, id: uuid::Uuid, @@ -2498,14 +2546,24 @@ mod tests { state: NativeLaunchState::Started, exit_code: None, error: None, - metadata: serde_json::json!({}), + metadata: serde_json::json!({"preserved": true}), started_at: now, updated_at: now, }) .unwrap(); + let evidence = serde_json::json!({"selected_threads": ["thread-native"], "complete": true}); + store.update_codex_launch_evidence(id, &evidence).unwrap(); drop(store); let reopened = SqliteStore::open(&database).unwrap(); + assert_eq!( + reopened.native_launch(id).unwrap().unwrap().metadata["codex_evidence"], + evidence + ); + assert_eq!( + reopened.native_launch(id).unwrap().unwrap().metadata["preserved"], + true + ); assert_eq!(reopened.open_native_launches(session.id).unwrap().len(), 1); reopened .update_native_launch(id, NativeLaunchState::Exited, Some(130), None, Utc::now()) @@ -2521,6 +2579,11 @@ mod tests { reopened .update_native_launch(id, NativeLaunchState::Captured, Some(130), None, Utc::now()) .unwrap(); + assert!( + reopened + .update_codex_launch_evidence(id, &evidence) + .is_err() + ); assert!( reopened .open_native_launches(session.id) diff --git a/docs/protocols.md b/docs/protocols.md index 4697804..3b4861e 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`; @@ -26,6 +32,23 @@ JSON-RPC/JSONL. The control connection is used to: - inspect account, capability, usage, and rate-limit signals; - generate and cache the installed version's JSON Schema. +With a running native daemon on Unix, each Codex CLI launch connects through a +private local relay using the CLI's `--remote` option. Relay endpoints last for +one launch; reopen with `agentctl open codex --session NAME` after exit. +The relay forwards native +protocol traffic and journals successful thread selections from that connection. +It stores thread IDs, pending selection count, and completion state; prompts and +tool payloads are not stored in this evidence. Ephemeral `thread_title` requests +are background title generation and do not select a conversation. Other thread +selections remain subject to continuity validation. Unrelated clients can update or +create sibling threads without being attributed to the mapped launch. + +Capture and recovery require complete evidence selecting only the mapped thread. +Native `/new`, `/fork`, or `/resume` to another thread remain detectable even if +the CLI returns to the mapped thread before exiting. Missing responses or a failed +relay block automatic capture. Launches without a relay retain the conservative +workspace snapshot check, including launches already journaled with that format. + External Claude messages can be injected as real user and assistant transcript items without starting a Codex model turn. Synthetic tool calls are never fabricated; only bounded summaries of their observable effects are projected. 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"