From 8be96fb592bb039d03e46cd636b007712dbc0d58 Mon Sep 17 00:00:00 2001 From: Dylan Hurd Date: Wed, 6 Aug 2025 19:56:13 -0700 Subject: [PATCH] [onboarding] Add simple codex-state interface --- codex-rs/core/src/codex.rs | 58 +++++++-- codex-rs/core/src/codex_state.rs | 178 +++++++++++++++++++++++++++ codex-rs/core/src/config.rs | 7 ++ codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/message_history.rs | 72 +---------- codex-rs/core/src/protocol.rs | 5 + codex-rs/core/src/util.rs | 71 +++++++++++ 7 files changed, 313 insertions(+), 79 deletions(-) create mode 100644 codex-rs/core/src/codex_state.rs diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 98d13b4cd6..97352cb874 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -4,6 +4,7 @@ use std::borrow::Cow; use std::collections::HashMap; use std::collections::HashSet; +use std::ops::Deref; use std::path::PathBuf; use std::sync::Arc; use std::sync::Mutex; @@ -145,6 +146,7 @@ impl Codex { disable_response_storage: config.disable_response_storage, notify: config.notify.clone(), cwd: config.cwd.clone(), + trust_cwd: config.trust_cwd, resume_path: resume_path.clone(), }; @@ -707,6 +709,7 @@ async fn submission_loop( disable_response_storage, notify, cwd, + trust_cwd, resume_path, } => { debug!( @@ -724,6 +727,37 @@ async fn submission_loop( } return; } + + // clone the config and update trust_cwd to match Op::ConfigureSession + let mut updated_config = config.deref().clone(); + updated_config.trust_cwd = trust_cwd; + let updated_config = Arc::new(updated_config); + + // sync trust_cwd to codex_state, and update config accordingly + if let Some(trust_cwd) = trust_cwd { + if trust_cwd { + match crate::codex_state::lookup_project(&updated_config).await { + Ok(mut project) => { + project.trusted = trust_cwd; + trace!( + "set cwd {} trust to {}", + updated_config.cwd.to_string_lossy(), + trust_cwd + ); + if let Err(e) = + crate::codex_state::update_project(&updated_config, &project) + .await + { + warn!("failed to persist project state: {e}"); + } + } + Err(e) => { + warn!("failed to persist project state: {e}"); + } + } + } + } + // Optionally resume an existing rollout. let mut restored_items: Option> = None; let rollout_recorder: Option = @@ -748,8 +782,12 @@ async fn submission_loop( let rollout_recorder = match rollout_recorder { Some(rec) => Some(rec), None => { - match RolloutRecorder::new(&config, session_id, user_instructions.clone()) - .await + match RolloutRecorder::new( + &updated_config, + session_id, + user_instructions.clone(), + ) + .await { Ok(r) => Some(r), Err(e) => { @@ -761,7 +799,7 @@ async fn submission_loop( }; let client = ModelClient::new( - config.clone(), + updated_config.clone(), auth.clone(), provider.clone(), model_reasoning_effort, @@ -786,7 +824,7 @@ async fn submission_loop( // Error messages to dispatch after SessionConfigured is sent. let mut mcp_connection_errors = Vec::::new(); let (mcp_connection_manager, failed_clients) = - match McpConnectionManager::new(config.mcp_servers.clone()).await { + match McpConnectionManager::new(updated_config.mcp_servers.clone()).await { Ok((mgr, failures)) => (mgr, failures), Err(e) => { let message = format!("Failed to create MCP connection manager: {e:#}"); @@ -815,10 +853,10 @@ async fn submission_loop( sess = Some(Arc::new(Session { client, tools_config: ToolsConfig::new( - &config.model_family, + &updated_config.model_family, approval_policy, sandbox_policy.clone(), - config.include_plan_tool, + updated_config.include_plan_tool, ), tx_event: tx_event.clone(), ctrl_c: Arc::clone(&ctrl_c), @@ -826,17 +864,17 @@ async fn submission_loop( base_instructions, approval_policy, sandbox_policy, - shell_environment_policy: config.shell_environment_policy.clone(), + shell_environment_policy: updated_config.shell_environment_policy.clone(), cwd, writable_roots, mcp_connection_manager, notify, state: Mutex::new(state), rollout: Mutex::new(rollout_recorder), - codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), + codex_linux_sandbox_exe: updated_config.codex_linux_sandbox_exe.clone(), disable_response_storage, user_shell: default_shell, - show_raw_agent_reasoning: config.show_raw_agent_reasoning, + show_raw_agent_reasoning: updated_config.show_raw_agent_reasoning, })); // Patch restored state into the newly created session. @@ -849,7 +887,7 @@ async fn submission_loop( // Gather history metadata for SessionConfiguredEvent. let (history_log_id, history_entry_count) = - crate::message_history::history_metadata(&config).await; + crate::message_history::history_metadata(&updated_config).await; // ack let events = std::iter::once(Event { diff --git a/codex-rs/core/src/codex_state.rs b/codex-rs/core/src/codex_state.rs new file mode 100644 index 0000000000..f6f1472c8b --- /dev/null +++ b/codex-rs/core/src/codex_state.rs @@ -0,0 +1,178 @@ +//! Persistence layer for the global, mutable project state store +//! +//! The project state is stored at `~/.codex/project_state.json` as a JSON +//! object. The object has the following schema: +//! +//! ```json +//! { +//! "projects": { +//! "": { +//! "trusted": , +//! // more to come... +//! } +//! }, +//! } +//! ``` +//! +//! To avoid race conditions, we leverage advisory file locking to ensure that +//! only one process can read or write the file at a time. + +use std::collections::HashMap; +use std::fs::File; +use std::fs::OpenOptions; +use std::io::Read; +use std::io::Result; +use std::io::Seek; +use std::io::SeekFrom; +use std::io::Write; +use std::path::PathBuf; + +use serde::Deserialize; +use serde::Serialize; + +use crate::config::Config; +use crate::util::acquire_exclusive_lock_with_retry; +#[cfg(unix)] +use crate::util::acquire_shared_lock_with_retry; +use crate::util::ensure_owner_only_permissions; + +/// Filename that stores the project state inside `~/.codex`. +const CODEX_STATE_FILENAME: &str = "codex-state.json"; + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Project { + pub trusted: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +#[derive(Default)] +pub struct CodexState { + pub projects: HashMap, +} + +fn codex_state_filepath(config: &Config) -> PathBuf { + let mut path = config.codex_home.clone(); + path.push(CODEX_STATE_FILENAME); + path +} + +async fn open_or_create_codex_state_file(config: &Config) -> Result { + // Resolve `~/.codex/codex-state.json` and ensure the parent directory exists. + let path = codex_state_filepath(config); + if let Some(parent) = path.parent() { + tokio::fs::create_dir_all(parent).await?; + } + + let file: File = match OpenOptions::new() + .read(true) + .create(true) + .append(true) + .open(&path) + { + Ok(f) => f, + Err(e) => { + tracing::warn!(error = %e, "failed to open project state file"); + return Err(e); + } + }; + + // Ensure restrictive permissions (0600) on Unix. + #[cfg(unix)] + ensure_owner_only_permissions(&file).await?; + + Ok(file) +} + +/// Lookup current project's state, creating it if needed. +#[cfg(unix)] +pub async fn lookup_project(config: &Config) -> Result { + use std::io::BufReader; + + let file = open_or_create_codex_state_file(config).await?; + + // Acquire a shared lock for reading. + if let Err(e) = acquire_shared_lock_with_retry(&file) { + tracing::warn!(error = %e, "failed to acquire shared lock on project state file"); + return Err(e); + } + + // Try to parse JSON; if empty or invalid, fall back to default state. + let mut reader = BufReader::new(&file); + let mut buf = String::new(); + match reader.read_to_string(&mut buf) { + Ok(_) => { + let codex_state: CodexState = if buf.trim().is_empty() { + CodexState::default() + } else { + match serde_json::from_str(&buf) { + Ok(v) => v, + Err(e) => { + tracing::warn!(error = %e, "failed to parse project state file; using defaults"); + CodexState::default() + } + } + }; + let project = codex_state + .projects + .get(&config.cwd) + .cloned() + .unwrap_or(Project { trusted: false }); + Ok(project) + } + Err(e) => { + tracing::warn!(error = %e, "failed to read project state file"); + Err(e) + } + } +} + +/// Not yet supported on non‑Unix platforms. +#[cfg(not(unix))] +pub async fn lookup_project(_config: &Config) -> Result { + return Ok(Project { trusted: false }); +} + +/// Update the project state for the given project. This function will +/// (currently) read and write the entire file, which does not scale well. +/// Use this function sparingly until we implement a more efficient solution. +pub async fn update_project(config: &Config, project: &Project) -> Result<()> { + let mut file = open_or_create_codex_state_file(config).await?; + + // Open & lock file for writing. + if let Err(e) = acquire_exclusive_lock_with_retry(&file).await { + tracing::warn!(error = %e, "failed to acquire exclusive lock on project state file"); + return Err(e); + } + + // Ensure file permissions. + ensure_owner_only_permissions(&file).await?; + + // Read existing state (if any). + let mut contents = String::new(); + // Safety: reading from start; ensure cursor at 0. + file.seek(SeekFrom::Start(0))?; + let _ = file.read_to_string(&mut contents); + let mut codex_state: CodexState = if contents.trim().is_empty() { + CodexState::default() + } else { + serde_json::from_str(&contents).unwrap_or_default() + }; + let cwd = config.cwd.clone(); + codex_state.projects.insert(cwd, project.clone()); + + // Overwrite the file from the beginning + file.set_len(0)?; + file.seek(SeekFrom::Start(0))?; + serde_json::to_writer_pretty(&mut file, &codex_state)?; + file.flush()?; + + Ok(()) +} + +/// Not yet supported on non‑Unix platforms. +#[cfg(not(unix))] +pub async fn update_project(_config: &Config, _project: &Project) -> Result<()> { + return Ok(()); +} diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs index 63a2e5949f..68f69d63dc 100644 --- a/codex-rs/core/src/config.rs +++ b/codex-rs/core/src/config.rs @@ -103,6 +103,9 @@ pub struct Config { /// resolved against this path. pub cwd: PathBuf, + /// Whether the current repo is trusted. + pub trust_cwd: Option, + /// Definition for MCP servers that Codex can reach out to for tool calls. pub mcp_servers: HashMap, @@ -520,6 +523,7 @@ impl Config { model_provider_id, model_provider, cwd: resolved_cwd, + trust_cwd: None, approval_policy: approval_policy .or(config_profile.approval_policy) .or(cfg.approval_policy) @@ -909,6 +913,7 @@ disable_response_storage = true user_instructions: None, notify: None, cwd: fixture.cwd(), + trust_cwd: None, mcp_servers: HashMap::new(), model_providers: fixture.model_provider_map.clone(), project_doc_max_bytes: PROJECT_DOC_MAX_BYTES, @@ -960,6 +965,7 @@ disable_response_storage = true user_instructions: None, notify: None, cwd: fixture.cwd(), + trust_cwd: None, mcp_servers: HashMap::new(), model_providers: fixture.model_provider_map.clone(), project_doc_max_bytes: PROJECT_DOC_MAX_BYTES, @@ -1026,6 +1032,7 @@ disable_response_storage = true user_instructions: None, notify: None, cwd: fixture.cwd(), + trust_cwd: None, mcp_servers: HashMap::new(), model_providers: fixture.model_provider_map.clone(), project_doc_max_bytes: PROJECT_DOC_MAX_BYTES, diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index c728bd3125..d10e8e6c0d 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -11,6 +11,7 @@ mod chat_completions; mod client; mod client_common; pub mod codex; +pub mod codex_state; pub use codex::Codex; pub use codex::CodexSpawnOk; pub mod codex_wrapper; diff --git a/codex-rs/core/src/message_history.rs b/codex-rs/core/src/message_history.rs index 29970c2a74..ef02377a64 100644 --- a/codex-rs/core/src/message_history.rs +++ b/codex-rs/core/src/message_history.rs @@ -22,25 +22,21 @@ use std::path::PathBuf; use serde::Deserialize; use serde::Serialize; -use std::time::Duration; use tokio::fs; use tokio::io::AsyncReadExt; use uuid::Uuid; use crate::config::Config; use crate::config_types::HistoryPersistence; +use crate::util::acquire_exclusive_lock_with_retry; +use crate::util::ensure_owner_only_permissions; #[cfg(unix)] use std::os::unix::fs::OpenOptionsExt; -#[cfg(unix)] -use std::os::unix::fs::PermissionsExt; /// Filename that stores the message history inside `~/.codex`. const HISTORY_FILENAME: &str = "history.jsonl"; -const MAX_RETRIES: usize = 10; -const RETRY_SLEEP: Duration = Duration::from_millis(100); - #[derive(Serialize, Deserialize, Debug, Clone)] pub struct HistoryEntry { pub session_id: String, @@ -121,29 +117,6 @@ pub(crate) async fn append_entry(text: &str, session_id: &Uuid, config: &Config) Ok(()) } -/// Attempt to acquire an exclusive advisory lock on `file`, retrying up to 10 -/// times if the lock is currently held by another process. This prevents a -/// potential indefinite wait while still giving other writers some time to -/// finish their operation. -async fn acquire_exclusive_lock_with_retry(file: &std::fs::File) -> Result<()> { - use tokio::time::sleep; - - for _ in 0..MAX_RETRIES { - match fs2::FileExt::try_lock_exclusive(file) { - Ok(()) => return Ok(()), - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { - sleep(RETRY_SLEEP).await; - } - Err(e) => return Err(e), - } - } - - Err(std::io::Error::new( - std::io::ErrorKind::WouldBlock, - "could not acquire exclusive lock on history file after multiple attempts", - )) -} - /// Asynchronously fetch the history file's *identifier* (inode on Unix) and /// the current number of entries by counting newline characters. pub(crate) async fn history_metadata(config: &Config) -> (u64, usize) { @@ -194,6 +167,7 @@ pub(crate) async fn history_metadata(config: &Config) -> (u64, usize) { /// locking API. #[cfg(unix)] pub(crate) fn lookup(log_id: u64, offset: usize, config: &Config) -> Option { + use crate::util::acquire_shared_lock_with_retry; use std::io::BufRead; use std::io::BufReader; use std::os::unix::fs::MetadataExt; @@ -255,43 +229,3 @@ pub(crate) fn lookup(log_id: u64, offset: usize, config: &Config) -> Option Result<()> { - for _ in 0..MAX_RETRIES { - match fs2::FileExt::try_lock_shared(file) { - Ok(()) => return Ok(()), - Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { - std::thread::sleep(RETRY_SLEEP); - } - Err(e) => return Err(e), - } - } - - Err(std::io::Error::new( - std::io::ErrorKind::WouldBlock, - "could not acquire shared lock on history file after multiple attempts", - )) -} - -/// On Unix systems ensure the file permissions are `0o600` (rw-------). If the -/// permissions cannot be changed the error is propagated to the caller. -#[cfg(unix)] -async fn ensure_owner_only_permissions(file: &File) -> Result<()> { - let metadata = file.metadata()?; - let current_mode = metadata.permissions().mode() & 0o777; - if current_mode != 0o600 { - let mut perms = metadata.permissions(); - perms.set_mode(0o600); - let perms_clone = perms.clone(); - let file_clone = file.try_clone()?; - tokio::task::spawn_blocking(move || file_clone.set_permissions(perms_clone)).await??; - } - Ok(()) -} - -#[cfg(not(unix))] -async fn ensure_owner_only_permissions(_file: &File) -> Result<()> { - // For now, on non-Unix, simply succeed. - Ok(()) -} diff --git a/codex-rs/core/src/protocol.rs b/codex-rs/core/src/protocol.rs index 052806dd97..f81a4e2961 100644 --- a/codex-rs/core/src/protocol.rs +++ b/codex-rs/core/src/protocol.rs @@ -79,6 +79,11 @@ pub enum Op { /// operate deterministically. cwd: std::path::PathBuf, + // Whether the user has indicated the current repo is trusted. + // If true, we persist this state to CodexState, so they don't get + // asked again. + trust_cwd: Option, + /// Path to a rollout file to resume from. #[serde(skip_serializing_if = "Option::is_none")] resume_path: Option, diff --git a/codex-rs/core/src/util.rs b/codex-rs/core/src/util.rs index 5ba1e25666..333fbe4f69 100644 --- a/codex-rs/core/src/util.rs +++ b/codex-rs/core/src/util.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::io::Result; use std::path::Path; use std::sync::Arc; use std::time::Duration; @@ -6,9 +8,15 @@ use rand::Rng; use tokio::sync::Notify; use tracing::debug; +#[cfg(unix)] +use std::os::unix::fs::PermissionsExt; + const INITIAL_DELAY_MS: u64 = 200; const BACKOFF_FACTOR: f64 = 1.3; +const MAX_RETRIES: usize = 10; +const RETRY_SLEEP: Duration = Duration::from_millis(100); + /// Make a CancellationToken that is fulfilled when SIGINT occurs. pub fn notify_on_sigint() -> Arc { let notify = Arc::new(Notify::new()); @@ -63,3 +71,66 @@ pub fn is_inside_git_repo(base_dir: &Path) -> bool { false } + +/// Attempt to acquire an exclusive advisory lock on `file`, retrying up to 10 +/// times if the lock is currently held by another process. This prevents a +/// potential indefinite wait while still giving other writers some time to +/// finish their operation. +pub(crate) async fn acquire_exclusive_lock_with_retry(file: &std::fs::File) -> Result<()> { + use tokio::time::sleep; + + for _ in 0..MAX_RETRIES { + match fs2::FileExt::try_lock_exclusive(file) { + Ok(()) => return Ok(()), + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + sleep(RETRY_SLEEP).await; + } + Err(e) => return Err(e), + } + } + + Err(std::io::Error::new( + std::io::ErrorKind::WouldBlock, + "could not acquire exclusive lock on history file after multiple attempts", + )) +} + +#[cfg(unix)] +pub(crate) fn acquire_shared_lock_with_retry(file: &File) -> Result<()> { + for _ in 0..MAX_RETRIES { + match fs2::FileExt::try_lock_shared(file) { + Ok(()) => return Ok(()), + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + std::thread::sleep(RETRY_SLEEP); + } + Err(e) => return Err(e), + } + } + + Err(std::io::Error::new( + std::io::ErrorKind::WouldBlock, + "could not acquire shared lock on history file after multiple attempts", + )) +} + +/// On Unix systems ensure the file permissions are `0o600` (rw-------). If the +/// permissions cannot be changed the error is propagated to the caller. +#[cfg(unix)] +pub(crate) async fn ensure_owner_only_permissions(file: &File) -> Result<()> { + let metadata = file.metadata()?; + let current_mode = metadata.permissions().mode() & 0o777; + if current_mode != 0o600 { + let mut perms = metadata.permissions(); + perms.set_mode(0o600); + let perms_clone = perms.clone(); + let file_clone = file.try_clone()?; + tokio::task::spawn_blocking(move || file_clone.set_permissions(perms_clone)).await??; + } + Ok(()) +} + +#[cfg(not(unix))] +pub(crate) async fn ensure_owner_only_permissions(_file: &File) -> Result<()> { + // For now, on non-Unix, simply succeed. + Ok(()) +}