mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
## Why SQLite state was still being opened from consumer paths, including lazy `OnceCell`-backed thread-store call sites. That let one process construct multiple state DB connections for the same Codex home, which makes SQLite lock contention and `database is locked` failures much easier to hit. State DB lifetime should be chosen by main-like entrypoints and tests, then passed through explicitly. Consumers should use the supplied `Option<StateDbHandle>` or `StateDbHandle` and keep their existing filesystem fallback or error behavior when no handle is available. The startup path also needs to keep the rollout crate in charge of SQLite state initialization. Opening `codex_state::StateRuntime` directly bypasses rollout metadata backfill, so entrypoints should initialize through `codex_rollout::state_db` and receive a handle only after required rollout backfills have completed. ## What Changed - Initialize the state DB in main-like entrypoints for CLI, TUI, app-server, exec, MCP server, and the thread-manager sample. - Pass `Option<StateDbHandle>` through `ThreadManager`, `LocalThreadStore`, app-server processors, TUI app wiring, rollout listing/recording, personality migration, shell snapshot cleanup, session-name lookup, and memory/device-key consumers. - Remove the lazy local state DB wrapper from the thread store so non-test consumers use only the supplied handle or their existing fallback path. - Make `codex_rollout::state_db::init` the local state startup path: it opens/migrates SQLite, runs rollout metadata backfill when needed, waits for concurrent backfill workers up to a bounded timeout, verifies completion, and then returns the initialized handle. - Keep optional/non-owning SQLite helpers, such as remote TUI local reads, as open-only paths that do not run startup backfill. - Switch app-server startup from direct `codex_state::StateRuntime::init` to the rollout state initializer so app-server cannot skip rollout backfill. - Collapse split rollout lookup/list APIs so callers use the normal methods with an optional state handle instead of `_with_state_db` variants. - Restore `getConversationSummary(ThreadId)` to delegate through `ThreadStore::read_thread` instead of a LocalThreadStore-specific rollout path special case. - Keep DB-backed rollout path lookup keyed on the DB row and file existence, without imposing the filesystem filename convention on existing DB rows. - Verify readable DB-backed rollout paths against `session_meta.id` before returning them, so a stale SQLite row that points at another thread's JSONL falls back to filesystem search and read-repairs the DB row. - Keep `debug prompt-input` filesystem-only so a one-off debug command does not initialize or backfill SQLite state just to print prompt input. - Keep goal-session test Codex homes alive only in the goal-specific helper, rather than leaking tempdirs from the shared session test helper. - Update tests and call sites to pass explicit state handles where DB behavior is expected and explicit `None` where filesystem-only behavior is intended. ## Validation - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo check -p codex-rollout -p codex-thread-store -p codex-app-server -p codex-core -p codex-tui -p codex-exec -p codex-cli --tests` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-rollout state_db_` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-rollout find_thread_path` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-rollout find_thread_path -- --nocapture` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-rollout try_init_ -- --nocapture` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-rollout` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo clippy -p codex-rollout --lib -- -D warnings` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-thread-store read_thread_falls_back_when_sqlite_path_points_to_another_thread -- --nocapture` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-thread-store` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core shell_snapshot` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core --test all personality_migration` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core --test all rollout_list_find` - `RUST_MIN_STACK=8388608 CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core --test all rollout_list_find::find_prefers_sqlite_path_by_id -- --nocapture` - `RUST_MIN_STACK=8388608 CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core --test all rollout_list_find -- --nocapture` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-core interrupt_accounts_active_goal_before_pausing` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-app-server get_auth_status -- --test-threads=1` - `CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo test -p codex-app-server --lib` - `CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db cargo check -p codex-rollout -p codex-app-server --tests` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db just fix -p codex-rollout -p codex-thread-store -p codex-core -p codex-app-server -p codex-tui -p codex-exec -p codex-cli` - `CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db just fix -p codex-rollout -p codex-app-server` - `CARGO_TARGET_DIR=/tmp/codex-target-state-db just fix -p codex-rollout` - `CODEX_SKIP_VENDORED_BWRAP=1 CARGO_TARGET_DIR=/tmp/codex-target-state-db just fix -p codex-core` - `just argument-comment-lint -p codex-core` - `just argument-comment-lint -p codex-rollout` Focused coverage added in `codex-rollout`: - `recorder::tests::state_db_init_backfills_before_returning` verifies the rollout metadata row exists before startup init returns. - `state_db::tests::try_init_waits_for_concurrent_startup_backfill` verifies startup waits for another worker to finish backfill instead of disabling the handle for the process. - `state_db::tests::try_init_times_out_waiting_for_stuck_startup_backfill` verifies startup does not hang indefinitely on a stuck backfill lease. - `tests::find_thread_path_accepts_existing_state_db_path_without_canonical_filename` verifies DB-backed lookup accepts valid existing rollout paths even when the filename does not include the thread UUID. - `tests::find_thread_path_falls_back_when_db_path_points_to_another_thread` verifies DB-backed lookup ignores a stale row whose existing path belongs to another thread and read-repairs the row after filesystem fallback. Focused coverage updated in `codex-core`: - `rollout_list_find::find_prefers_sqlite_path_by_id` now uses a DB-preferred rollout file with matching `session_meta.id`, so it still verifies that valid SQLite paths win without depending on stale/empty rollout contents. `cargo test -p codex-app-server thread_list_respects_search_term_filter -- --test-threads=1 --nocapture` was attempted locally but timed out waiting for the app-server test harness `initialize` response before reaching the changed thread-list code path. `bazel test //codex-rs/thread-store:thread-store-unit-tests --test_output=errors` was attempted locally after the thread-store fix, but this container failed before target analysis while fetching `v8+` through BuildBuddy/direct GitHub. The equivalent local crate coverage, including `cargo test -p codex-thread-store`, passes. A plain local `cargo check -p codex-rollout -p codex-app-server --tests` also requires system `libcap.pc` for `codex-linux-sandbox`; the follow-up app-server check above used `CODEX_SKIP_VENDORED_BWRAP=1` in this container.
269 lines
8.0 KiB
Rust
269 lines
8.0 KiB
Rust
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::io::Seek;
|
|
use std::io::SeekFrom;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::protocol::SessionMetaLine;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use tokio::io::AsyncBufReadExt;
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
const SESSION_INDEX_FILE: &str = "session_index.jsonl";
|
|
const READ_CHUNK_SIZE: usize = 8192;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub struct SessionIndexEntry {
|
|
pub id: ThreadId,
|
|
pub thread_name: String,
|
|
pub updated_at: String,
|
|
}
|
|
|
|
/// Append a thread name update to the session index.
|
|
/// The index is append-only; the most recent entry wins when resolving names or ids.
|
|
pub async fn append_thread_name(
|
|
codex_home: &Path,
|
|
thread_id: ThreadId,
|
|
name: &str,
|
|
) -> std::io::Result<()> {
|
|
use time::OffsetDateTime;
|
|
use time::format_description::well_known::Rfc3339;
|
|
|
|
let updated_at = OffsetDateTime::now_utc()
|
|
.format(&Rfc3339)
|
|
.unwrap_or_else(|_| "unknown".to_string());
|
|
let entry = SessionIndexEntry {
|
|
id: thread_id,
|
|
thread_name: name.to_string(),
|
|
updated_at,
|
|
};
|
|
append_session_index_entry(codex_home, &entry).await
|
|
}
|
|
|
|
/// Append a raw session index entry to `session_index.jsonl`.
|
|
/// The file is append-only; consumers scan from the end to find the newest match.
|
|
pub async fn append_session_index_entry(
|
|
codex_home: &Path,
|
|
entry: &SessionIndexEntry,
|
|
) -> std::io::Result<()> {
|
|
let path = session_index_path(codex_home);
|
|
let mut file = tokio::fs::OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open(&path)
|
|
.await?;
|
|
let mut line = serde_json::to_string(entry).map_err(std::io::Error::other)?;
|
|
line.push('\n');
|
|
file.write_all(line.as_bytes()).await?;
|
|
file.flush().await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Find the latest thread name for a thread id, if any.
|
|
pub async fn find_thread_name_by_id(
|
|
codex_home: &Path,
|
|
thread_id: &ThreadId,
|
|
) -> std::io::Result<Option<String>> {
|
|
let path = session_index_path(codex_home);
|
|
if !path.exists() {
|
|
return Ok(None);
|
|
}
|
|
let id = *thread_id;
|
|
let entry = tokio::task::spawn_blocking(move || scan_index_from_end_by_id(&path, &id))
|
|
.await
|
|
.map_err(std::io::Error::other)??;
|
|
Ok(entry.map(|entry| entry.thread_name))
|
|
}
|
|
|
|
/// Find the latest thread names for a batch of thread ids.
|
|
pub async fn find_thread_names_by_ids(
|
|
codex_home: &Path,
|
|
thread_ids: &HashSet<ThreadId>,
|
|
) -> std::io::Result<HashMap<ThreadId, String>> {
|
|
let path = session_index_path(codex_home);
|
|
if thread_ids.is_empty() || !path.exists() {
|
|
return Ok(HashMap::new());
|
|
}
|
|
|
|
let file = tokio::fs::File::open(&path).await?;
|
|
let reader = tokio::io::BufReader::new(file);
|
|
let mut lines = reader.lines();
|
|
let mut names = HashMap::with_capacity(thread_ids.len());
|
|
|
|
while let Some(line) = lines.next_line().await? {
|
|
let trimmed = line.trim();
|
|
if trimmed.is_empty() {
|
|
continue;
|
|
}
|
|
let Ok(entry) = serde_json::from_str::<SessionIndexEntry>(trimmed) else {
|
|
continue;
|
|
};
|
|
let name = entry.thread_name.trim();
|
|
if !name.is_empty() && thread_ids.contains(&entry.id) {
|
|
names.insert(entry.id, name.to_string());
|
|
}
|
|
}
|
|
|
|
Ok(names)
|
|
}
|
|
|
|
/// Locate a recorded thread rollout and read its session metadata by thread name.
|
|
/// Returns the newest indexed name that still has a readable rollout header.
|
|
pub async fn find_thread_meta_by_name_str(
|
|
codex_home: &Path,
|
|
name: &str,
|
|
state_db_ctx: Option<&codex_state::StateRuntime>,
|
|
) -> std::io::Result<Option<(PathBuf, SessionMetaLine)>> {
|
|
if name.trim().is_empty() {
|
|
return Ok(None);
|
|
}
|
|
let path = session_index_path(codex_home);
|
|
if !path.exists() {
|
|
return Ok(None);
|
|
}
|
|
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
|
|
let name = name.to_string();
|
|
// Stream matching ids newest-first instead of stopping at the first name hit: the newest entry
|
|
// may point at a thread whose rollout was never materialized.
|
|
let scan =
|
|
tokio::task::spawn_blocking(move || stream_thread_ids_from_end_by_name(&path, &name, tx));
|
|
|
|
while let Some(thread_id) = rx.recv().await {
|
|
// Keep walking until a matching id resolves to a loadable rollout so an unsaved or partial
|
|
// rename cannot shadow an older persisted session with the same name.
|
|
if let Some(path) = super::list::find_thread_path_by_id_str(
|
|
codex_home,
|
|
&thread_id.to_string(),
|
|
state_db_ctx,
|
|
)
|
|
.await?
|
|
&& let Ok(session_meta) = super::list::read_session_meta_line(&path).await
|
|
{
|
|
drop(rx);
|
|
scan.await.map_err(std::io::Error::other)??;
|
|
return Ok(Some((path, session_meta)));
|
|
}
|
|
}
|
|
scan.await.map_err(std::io::Error::other)??;
|
|
|
|
Ok(None)
|
|
}
|
|
|
|
fn session_index_path(codex_home: &Path) -> PathBuf {
|
|
codex_home.join(SESSION_INDEX_FILE)
|
|
}
|
|
|
|
fn scan_index_from_end_by_id(
|
|
path: &Path,
|
|
thread_id: &ThreadId,
|
|
) -> std::io::Result<Option<SessionIndexEntry>> {
|
|
scan_index_from_end(path, |entry| entry.id == *thread_id)
|
|
}
|
|
|
|
fn stream_thread_ids_from_end_by_name(
|
|
path: &Path,
|
|
name: &str,
|
|
tx: tokio::sync::mpsc::Sender<ThreadId>,
|
|
) -> std::io::Result<()> {
|
|
let mut seen = HashSet::new();
|
|
scan_index_from_end_for_each(path, |entry| {
|
|
// The first row seen for an id is its latest name. Ignore older rows for that id so a
|
|
// historical name cannot be treated as the current one after the thread is renamed.
|
|
if seen.insert(entry.id) && entry.thread_name == name && tx.blocking_send(entry.id).is_err()
|
|
{
|
|
return Ok(Some(entry.clone()));
|
|
}
|
|
Ok(None)
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
fn scan_index_from_end<F>(
|
|
path: &Path,
|
|
mut predicate: F,
|
|
) -> std::io::Result<Option<SessionIndexEntry>>
|
|
where
|
|
F: FnMut(&SessionIndexEntry) -> bool,
|
|
{
|
|
scan_index_from_end_for_each(path, |entry| {
|
|
if predicate(entry) {
|
|
return Ok(Some(entry.clone()));
|
|
}
|
|
Ok(None)
|
|
})
|
|
}
|
|
|
|
fn scan_index_from_end_for_each<F>(
|
|
path: &Path,
|
|
mut visit_entry: F,
|
|
) -> std::io::Result<Option<SessionIndexEntry>>
|
|
where
|
|
F: FnMut(&SessionIndexEntry) -> std::io::Result<Option<SessionIndexEntry>>,
|
|
{
|
|
let mut file = File::open(path)?;
|
|
let mut remaining = file.metadata()?.len();
|
|
let mut line_rev: Vec<u8> = Vec::new();
|
|
let mut buf = vec![0u8; READ_CHUNK_SIZE];
|
|
|
|
while remaining > 0 {
|
|
let read_size = usize::try_from(remaining.min(READ_CHUNK_SIZE as u64))
|
|
.map_err(std::io::Error::other)?;
|
|
remaining -= read_size as u64;
|
|
file.seek(SeekFrom::Start(remaining))?;
|
|
file.read_exact(&mut buf[..read_size])?;
|
|
|
|
for &byte in buf[..read_size].iter().rev() {
|
|
if byte == b'\n' {
|
|
if let Some(entry) = parse_line_from_rev(&mut line_rev, &mut visit_entry)? {
|
|
return Ok(Some(entry));
|
|
}
|
|
continue;
|
|
}
|
|
line_rev.push(byte);
|
|
}
|
|
}
|
|
|
|
if let Some(entry) = parse_line_from_rev(&mut line_rev, &mut visit_entry)? {
|
|
return Ok(Some(entry));
|
|
}
|
|
|
|
Ok(None)
|
|
}
|
|
|
|
fn parse_line_from_rev<F>(
|
|
line_rev: &mut Vec<u8>,
|
|
visit_entry: &mut F,
|
|
) -> std::io::Result<Option<SessionIndexEntry>>
|
|
where
|
|
F: FnMut(&SessionIndexEntry) -> std::io::Result<Option<SessionIndexEntry>>,
|
|
{
|
|
if line_rev.is_empty() {
|
|
return Ok(None);
|
|
}
|
|
line_rev.reverse();
|
|
let line = std::mem::take(line_rev);
|
|
let Ok(mut line) = String::from_utf8(line) else {
|
|
return Ok(None);
|
|
};
|
|
if line.ends_with('\r') {
|
|
line.pop();
|
|
}
|
|
let trimmed = line.trim();
|
|
if trimmed.is_empty() {
|
|
return Ok(None);
|
|
}
|
|
let Ok(entry) = serde_json::from_str::<SessionIndexEntry>(trimmed) else {
|
|
return Ok(None);
|
|
};
|
|
visit_entry(&entry)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "session_index_tests.rs"]
|
|
mod tests;
|