mirror of
https://github.com/openai/codex.git
synced 2026-11-28 23:34:06 +00:00
Use app-server metadata for TUI session restoration (#43360)
## What changed - Carry the working directory from `thread/list` and `thread/read` in session selections, and use app-server metadata to resolve resume and fork directories. - Use the model reported by thread reads and thread-started notifications when restoring session state. - Remove TUI helpers that read session IDs, working directories, and models directly from rollout files or the state database. Picker selection now requires a thread ID in the selected row. ## Testing Extend picker coverage to verify that selections preserve the working directory, and add a snapshot asserting that switching to a read thread displays the server-reported model. GitOrigin-RevId: c9d13e1e56ea2139d3e96344d72ca9ee2de73396
This commit is contained in:
1 parent
21bd5d3cdc
commit
694b6319d3
15 files changed
+117
-462
No files matched your search
@@ -1,52 +1,20 @@
|
||||
//! Resolve saved-session state needed before resuming or forking a thread.
|
||||
//!
|
||||
//! The app-server API owns normal thread lifecycle data. This module coordinates
|
||||
//! the TUI-specific cwd prompt and falls back to local rollout metadata only
|
||||
//! before the app server has resumed the selected thread.
|
||||
//! The app-server API owns thread metadata. This module coordinates the TUI-specific
|
||||
//! cwd prompt using the cwd reported by the selected thread.
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::app_server_session::AppServerSession;
|
||||
use crate::cwd_prompt;
|
||||
use crate::cwd_prompt::CwdPromptAction;
|
||||
use crate::cwd_prompt::CwdPromptOutcome;
|
||||
use crate::legacy_core::config::Config;
|
||||
use crate::resume_picker::SessionTarget;
|
||||
use crate::tui::Tui;
|
||||
use codex_config::types::ResumeCwdMode;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_rollout::open_rollout_line_reader;
|
||||
use codex_state::StateRuntime;
|
||||
use codex_utils_path as path_utils;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Default)]
|
||||
struct RolloutResumeState {
|
||||
thread_id: Option<ThreadId>,
|
||||
cwd: Option<PathBuf>,
|
||||
model: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SessionMetadata {
|
||||
id: ThreadId,
|
||||
cwd: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TurnContextResumeState {
|
||||
cwd: PathBuf,
|
||||
model: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RawRecord {
|
||||
#[serde(rename = "type")]
|
||||
item_type: String,
|
||||
payload: Option<Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(crate) enum ResolveCwdOutcome {
|
||||
@@ -74,43 +42,26 @@ pub(crate) fn effective_resume_cwd_mode(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn resolve_session_thread_id(
|
||||
path: &Path,
|
||||
id_str_if_uuid: Option<&str>,
|
||||
) -> Option<ThreadId> {
|
||||
match id_str_if_uuid {
|
||||
Some(id_str) => ThreadId::from_string(id_str).ok(),
|
||||
None => read_rollout_resume_state(path)
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|state| state.thread_id),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn read_session_model(
|
||||
state_db_ctx: Option<&StateRuntime>,
|
||||
pub(crate) async fn read_session_cwd(
|
||||
app_server: &mut AppServerSession,
|
||||
thread_id: ThreadId,
|
||||
path: Option<&Path>,
|
||||
) -> Option<String> {
|
||||
if let Some(state_db_ctx) = state_db_ctx
|
||||
&& let Ok(Some(metadata)) = state_db_ctx.get_thread(thread_id).await
|
||||
&& let Some(model) = metadata.model
|
||||
{
|
||||
return Some(model);
|
||||
}
|
||||
|
||||
let path = path?;
|
||||
read_rollout_resume_state(path)
|
||||
) -> Option<PathBuf> {
|
||||
match app_server
|
||||
.thread_read(thread_id, /*include_turns*/ false)
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|state| state.model)
|
||||
{
|
||||
Ok(thread) => Some(thread.cwd.to_path_buf()),
|
||||
Err(err) => {
|
||||
tracing::warn!(%thread_id, %err, "Failed to read session cwd from app server");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn resolve_cwd_for_resume_or_fork(
|
||||
tui: &mut Tui,
|
||||
config: &Config,
|
||||
state_db_ctx: Option<&StateRuntime>,
|
||||
target_session: &SessionTarget,
|
||||
history_cwd: Option<PathBuf>,
|
||||
action: CwdPromptAction,
|
||||
cwd_context: ResumeCwdContext<'_>,
|
||||
) -> color_eyre::Result<ResolveCwdOutcome> {
|
||||
@@ -119,13 +70,7 @@ pub(crate) async fn resolve_cwd_for_resume_or_fork(
|
||||
cwd_context.remembered_current_cwd.to_path_buf(),
|
||||
)));
|
||||
}
|
||||
let Some(history_cwd) = read_session_cwd(
|
||||
state_db_ctx,
|
||||
target_session.thread_id,
|
||||
target_session.path.as_deref(),
|
||||
)
|
||||
.await
|
||||
else {
|
||||
let Some(history_cwd) = history_cwd else {
|
||||
if matches!(cwd_context.mode, Some(ResumeCwdMode::Session)) {
|
||||
color_eyre::eyre::bail!(
|
||||
"failed to determine the working directory recorded for the selected session"
|
||||
@@ -166,346 +111,35 @@ pub(crate) async fn resolve_cwd_for_resume_or_fork(
|
||||
Ok(ResolveCwdOutcome::Continue(Some(history_cwd)))
|
||||
}
|
||||
|
||||
async fn read_session_cwd(
|
||||
state_db_ctx: Option<&StateRuntime>,
|
||||
thread_id: ThreadId,
|
||||
path: Option<&Path>,
|
||||
) -> Option<PathBuf> {
|
||||
if let Some(state_db_ctx) = state_db_ctx
|
||||
&& let Ok(Some(metadata)) = state_db_ctx.get_thread(thread_id).await
|
||||
{
|
||||
return Some(metadata.cwd);
|
||||
}
|
||||
|
||||
let path = path?;
|
||||
match read_rollout_resume_state(path).await {
|
||||
Ok(state) => state.cwd,
|
||||
Err(err) => {
|
||||
let rollout_path = path.display().to_string();
|
||||
tracing::warn!(
|
||||
%rollout_path,
|
||||
%err,
|
||||
"Failed to read session metadata from rollout"
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn cwds_differ(current_cwd: &Path, session_cwd: &Path) -> bool {
|
||||
!path_utils::paths_match_after_normalization(current_cwd, session_cwd)
|
||||
}
|
||||
|
||||
async fn read_rollout_resume_state(path: &Path) -> io::Result<RolloutResumeState> {
|
||||
let mut reader = open_rollout_line_reader(path).await?;
|
||||
let mut state = RolloutResumeState::default();
|
||||
let mut saw_record = false;
|
||||
|
||||
while let Some(line) = reader.next_line().await? {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Ok(record) = serde_json::from_str::<RawRecord>(trimmed) else {
|
||||
continue;
|
||||
};
|
||||
saw_record = true;
|
||||
let Some(payload) = record.payload else {
|
||||
continue;
|
||||
};
|
||||
|
||||
match record.item_type.as_str() {
|
||||
"session_meta" if state.thread_id.is_none() => {
|
||||
if let Ok(metadata) = serde_json::from_value::<SessionMetadata>(payload) {
|
||||
state.thread_id = Some(metadata.id);
|
||||
state.cwd.get_or_insert(metadata.cwd);
|
||||
}
|
||||
}
|
||||
"turn_context" => {
|
||||
if let Ok(turn_context) = serde_json::from_value::<TurnContextResumeState>(payload)
|
||||
{
|
||||
state.cwd = Some(turn_context.cwd);
|
||||
state.model = Some(turn_context.model);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if saw_record {
|
||||
Ok(state)
|
||||
} else {
|
||||
Err(io::Error::other(format!(
|
||||
"rollout at {} is empty",
|
||||
path.display()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codex_utils_absolute_path::test_support::PathExt;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn rollout_line(
|
||||
timestamp: &str,
|
||||
item_type: &str,
|
||||
payload: serde_json::Value,
|
||||
) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"timestamp": timestamp,
|
||||
"type": item_type,
|
||||
"payload": payload,
|
||||
})
|
||||
}
|
||||
|
||||
fn write_rollout_lines(path: &Path, lines: &[serde_json::Value]) -> std::io::Result<()> {
|
||||
let mut text = String::new();
|
||||
for line in lines {
|
||||
text.push_str(&serde_json::to_string(line).expect("serialize rollout"));
|
||||
text.push('\n');
|
||||
}
|
||||
std::fs::write(path, text)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_resume_state_prefers_latest_turn_context() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let original = temp_dir.path().join("original");
|
||||
let latest = temp_dir.path().join("latest");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[
|
||||
rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": original,
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
),
|
||||
rollout_line(
|
||||
"t1",
|
||||
"turn_context",
|
||||
serde_json::json!({ "cwd": temp_dir.path().join("middle"), "model": "middle" }),
|
||||
),
|
||||
rollout_line(
|
||||
"t2",
|
||||
"turn_context",
|
||||
serde_json::json!({ "cwd": latest.clone(), "model": "latest" }),
|
||||
),
|
||||
],
|
||||
)?;
|
||||
|
||||
let state = read_rollout_resume_state(&rollout_path).await?;
|
||||
|
||||
assert_eq!(state.thread_id, Some(thread_id));
|
||||
assert_eq!(state.cwd, Some(latest));
|
||||
assert_eq!(state.model, Some("latest".to_string()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_resume_state_falls_back_to_session_meta() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let cwd = temp_dir.path().join("session");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": cwd.clone(),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
)],
|
||||
)?;
|
||||
|
||||
let state = read_rollout_resume_state(&rollout_path).await?;
|
||||
|
||||
assert_eq!(state.thread_id, Some(thread_id));
|
||||
assert_eq!(state.cwd, Some(cwd));
|
||||
assert_eq!(state.model, None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_resume_state_skips_malformed_lines() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let cwd = temp_dir.path().join("session");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
let valid_line = serde_json::to_string(&rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": cwd.clone(),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
))
|
||||
.expect("serialize rollout line");
|
||||
std::fs::write(&rollout_path, format!("{valid_line}\n{{"))?;
|
||||
|
||||
let state = read_rollout_resume_state(&rollout_path).await?;
|
||||
|
||||
assert_eq!(state.thread_id, Some(thread_id));
|
||||
assert_eq!(state.cwd, Some(cwd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_resume_state_preserves_legacy_fork_child_context() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::from_string("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa")
|
||||
.expect("legacy thread id");
|
||||
let parent_thread_id = ThreadId::from_string("bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb")
|
||||
.expect("legacy parent id");
|
||||
let child_cwd = temp_dir.path().join("child");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[
|
||||
rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"forked_from_id": parent_thread_id,
|
||||
"cwd": temp_dir.path().join("initial"),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
),
|
||||
rollout_line(
|
||||
"t1",
|
||||
"event_msg",
|
||||
serde_json::json!({ "type": "task_started", "turn_id": "legacy-child-turn" }),
|
||||
),
|
||||
rollout_line(
|
||||
"t2",
|
||||
"turn_context",
|
||||
serde_json::json!({ "cwd": child_cwd.clone(), "model": "child-model" }),
|
||||
),
|
||||
],
|
||||
)?;
|
||||
|
||||
let state = read_rollout_resume_state(&rollout_path).await?;
|
||||
|
||||
assert_eq!(state.thread_id, Some(thread_id));
|
||||
assert_eq!(state.cwd, Some(child_cwd));
|
||||
assert_eq!(state.model.as_deref(), Some("child-model"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn session_cwd_prefers_state_metadata_over_rollout_context() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let session_cwd = temp_dir.path().join("child");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[
|
||||
rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": session_cwd,
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
),
|
||||
rollout_line(
|
||||
"t1",
|
||||
"turn_context",
|
||||
serde_json::json!({
|
||||
"cwd": temp_dir.path().join("copied-parent"),
|
||||
"model": "parent-model",
|
||||
}),
|
||||
),
|
||||
],
|
||||
)?;
|
||||
let state_runtime = StateRuntime::init(
|
||||
codex_state::SqliteConfig::new_for_testing(temp_dir.path().abs()),
|
||||
"test-provider".to_string(),
|
||||
)
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
let created_at = chrono::DateTime::parse_from_rfc3339("2025-01-05T12:00:00Z")
|
||||
.expect("timestamp should parse")
|
||||
.with_timezone(&chrono::Utc);
|
||||
let mut builder = codex_state::ThreadMetadataBuilder::new(
|
||||
thread_id,
|
||||
rollout_path.clone(),
|
||||
created_at,
|
||||
serde_json::from_value(serde_json::json!("cli"))
|
||||
.expect("cli session source should deserialize"),
|
||||
);
|
||||
builder.cwd = session_cwd.clone();
|
||||
state_runtime
|
||||
.upsert_thread(&builder.build("test-provider"))
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
|
||||
let cwd =
|
||||
read_session_cwd(Some(state_runtime.as_ref()), thread_id, Some(&rollout_path)).await;
|
||||
|
||||
assert_eq!(cwd, Some(session_cwd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn configured_resume_cwd_skips_prompt() -> color_eyre::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let session_cwd = temp_dir.path().join("session");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
let config = crate::legacy_core::config::ConfigBuilder::default()
|
||||
.codex_home(temp_dir.path().to_path_buf())
|
||||
.build()
|
||||
.await?;
|
||||
let current_cwd = config.cwd.to_path_buf();
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": session_cwd.clone(),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
)],
|
||||
)?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
|
||||
for (cwd_mode, expected_cwd) in [
|
||||
(ResumeCwdMode::Current, current_cwd.clone()),
|
||||
(ResumeCwdMode::Session, session_cwd),
|
||||
(ResumeCwdMode::Session, session_cwd.clone()),
|
||||
] {
|
||||
let outcome = resolve_cwd_for_resume_or_fork(
|
||||
&mut tui,
|
||||
&config,
|
||||
/*state_db_ctx*/ None,
|
||||
&SessionTarget {
|
||||
path: Some(rollout_path.clone()),
|
||||
thread_id,
|
||||
history_mode: None,
|
||||
},
|
||||
Some(session_cwd.clone()),
|
||||
CwdPromptAction::Fork,
|
||||
ResumeCwdContext {
|
||||
current_cwd: ¤t_cwd,
|
||||
@@ -524,37 +158,17 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn matching_resume_cwd_skips_prompt_without_configured_mode() -> color_eyre::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
let config = crate::legacy_core::config::ConfigBuilder::default()
|
||||
.codex_home(temp_dir.path().to_path_buf())
|
||||
.build()
|
||||
.await?;
|
||||
let current_cwd = config.cwd.to_path_buf();
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": current_cwd,
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
)],
|
||||
)?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
|
||||
let outcome = resolve_cwd_for_resume_or_fork(
|
||||
&mut tui,
|
||||
&config,
|
||||
/*state_db_ctx*/ None,
|
||||
&SessionTarget {
|
||||
path: Some(rollout_path),
|
||||
thread_id,
|
||||
history_mode: None,
|
||||
},
|
||||
Some(current_cwd.clone()),
|
||||
CwdPromptAction::Resume,
|
||||
ResumeCwdContext {
|
||||
current_cwd: ¤t_cwd,
|
||||
@@ -582,12 +196,7 @@ mod tests {
|
||||
let error = resolve_cwd_for_resume_or_fork(
|
||||
&mut tui,
|
||||
&config,
|
||||
/*state_db_ctx*/ None,
|
||||
&SessionTarget {
|
||||
path: None,
|
||||
thread_id: ThreadId::new(),
|
||||
history_mode: None,
|
||||
},
|
||||
/*history_cwd*/ None,
|
||||
CwdPromptAction::Resume,
|
||||
ResumeCwdContext {
|
||||
current_cwd: ¤t_cwd,
|
||||
|
||||
Reference in new issue
Block a user