fix: tighten up some logic around session timestamps and ids (#922)

* update `SessionConfigured` event to include the UUID for the session
* show the UUID in the Rust TUI
* use local timestamps in log files instead of UTC
* include timestamps in log file names for easier discovery
This commit is contained in:
Michael Bolin
2025-05-13 19:22:16 -07:00
committed by GitHub
parent 3c03c25e56
commit e6c206d19d
9 changed files with 101 additions and 77 deletions

View File

@@ -31,7 +31,7 @@ reqwest = { version = "0.12", features = ["json", "stream"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2.0.12"
time = { version = "0.3", features = ["formatting", "macros"] }
time = { version = "0.3", features = ["formatting", "local-offset", "macros"] }
tokio = { version = "1", features = [
"io-std",
"macros",
@@ -44,7 +44,7 @@ toml = "0.8.20"
tracing = { version = "0.1.41", features = ["log"] }
tree-sitter = "0.25.3"
tree-sitter-bash = "0.23.3"
uuid = { version = "1", features = ["v4"] }
uuid = { version = "1", features = ["serde", "v4"] }
[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2.172"

View File

@@ -30,6 +30,7 @@ use tracing::error;
use tracing::info;
use tracing::trace;
use tracing::warn;
use uuid::Uuid;
use crate::WireApi;
use crate::client::ModelClient;
@@ -62,6 +63,7 @@ use crate::protocol::InputItem;
use crate::protocol::Op;
use crate::protocol::ReviewDecision;
use crate::protocol::SandboxPolicy;
use crate::protocol::SessionConfiguredEvent;
use crate::protocol::Submission;
use crate::rollout::RolloutRecorder;
use crate::safety::SafetyCheck;
@@ -596,13 +598,15 @@ async fn submission_loop(
// Attempt to create a RolloutRecorder *before* moving the
// `instructions` value into the Session struct.
let rollout_recorder = match RolloutRecorder::new(instructions.clone()).await {
Ok(r) => Some(r),
Err(e) => {
tracing::warn!("failed to initialise rollout recorder: {e}");
None
}
};
let session_id = Uuid::new_v4();
let rollout_recorder =
match RolloutRecorder::new(session_id, instructions.clone()).await {
Ok(r) => Some(r),
Err(e) => {
tracing::warn!("failed to initialise rollout recorder: {e}");
None
}
};
sess = Some(Arc::new(Session {
client,
@@ -622,7 +626,7 @@ async fn submission_loop(
// ack
let events = std::iter::once(Event {
id: sub.id.clone(),
msg: EventMsg::SessionConfigured { model },
msg: EventMsg::SessionConfigured(SessionConfiguredEvent { session_id, model }),
})
.chain(mcp_connection_errors.into_iter());
for event in events {

View File

@@ -10,6 +10,7 @@ use std::path::PathBuf;
use mcp_types::CallToolResult;
use serde::Deserialize;
use serde::Serialize;
use uuid::Uuid;
use crate::model_provider_info::ModelProviderInfo;
@@ -323,10 +324,7 @@ pub enum EventMsg {
},
/// Ack the client's configure message.
SessionConfigured {
/// Tell the client what model is being queried.
model: String,
},
SessionConfigured(SessionConfiguredEvent),
McpToolCallBegin {
/// Identifier so this can be paired with the McpToolCallEnd event.
@@ -429,6 +427,15 @@ pub enum EventMsg {
},
}
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct SessionConfiguredEvent {
/// Unique id for this session.
pub session_id: Uuid,
/// Tell the client what model is being queried.
pub model: String,
}
/// User's decision in response to an ExecApprovalRequest.
#[derive(Debug, Default, Clone, Copy, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]

View File

@@ -37,8 +37,8 @@ struct SessionMeta {
/// Rollouts are recorded as JSONL and can be inspected with tools such as:
///
/// ```ignore
/// $ jq -C . ~/.codex/sessions/rollout-2025-05-07-5973b6c0-94b8-487b-a530-2aeb6098ae0e.jsonl
/// $ fx ~/.codex/sessions/rollout-2025-05-07-5973b6c0-94b8-487b-a530-2aeb6098ae0e.jsonl
/// $ jq -C . ~/.codex/sessions/rollout-2025-05-07T17-24-21-5973b6c0-94b8-487b-a530-2aeb6098ae0e.jsonl
/// $ fx ~/.codex/sessions/rollout-2025-05-07T17-24-21-5973b6c0-94b8-487b-a530-2aeb6098ae0e.jsonl
/// ```
#[derive(Clone)]
pub(crate) struct RolloutRecorder {
@@ -49,12 +49,12 @@ impl RolloutRecorder {
/// Attempt to create a new [`RolloutRecorder`]. If the sessions directory
/// cannot be created or the rollout file cannot be opened we return the
/// error so the caller can decide whether to disable persistence.
pub async fn new(instructions: Option<String>) -> std::io::Result<Self> {
pub async fn new(uuid: Uuid, instructions: Option<String>) -> std::io::Result<Self> {
let LogFileInfo {
file,
session_id,
timestamp,
} = create_log_file()?;
} = create_log_file(uuid)?;
// Build the static session metadata JSON first.
let timestamp_format: &[FormatItem] = format_description!(
@@ -154,18 +154,19 @@ struct LogFileInfo {
timestamp: OffsetDateTime,
}
fn create_log_file() -> std::io::Result<LogFileInfo> {
fn create_log_file(session_id: Uuid) -> std::io::Result<LogFileInfo> {
// Resolve ~/.codex/sessions and create it if missing.
let mut dir = codex_dir()?;
dir.push(SESSIONS_SUBDIR);
fs::create_dir_all(&dir)?;
// Generate a v4 UUID matches the JS CLI implementation.
let session_id = Uuid::new_v4();
let timestamp = OffsetDateTime::now_utc();
let timestamp = OffsetDateTime::now_local()
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to get local time: {e}")))?;
// Custom format for YYYY-MM-DD.
let format: &[FormatItem] = format_description!("[year]-[month]-[day]");
// Custom format for YYYY-MM-DDThh-mm-ss. Use `-` instead of `:` for
// compatibility with filesystems that do not allow colons in filenames.
let format: &[FormatItem] =
format_description!("[year]-[month]-[day]T[hour]-[minute]-[second]");
let date_str = timestamp
.format(format)
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to format timestamp: {e}")))?;