mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
## Why Paginated history previously persisted completed commands' full `aggregated_output`, allowing large command outputs to inflate stored history. ## What changed - Limit persisted command `aggregated_output` to 64 KiB, including a truncation marker. Preserve the beginning and end of the output without splitting UTF-8 characters. - Apply the shared persistence policy to rollout recording, thread-store appends, and legacy-to-paginated history migration. - Measure persisted payload bytes after truncation so persistence metrics reflect the stored representation. ## Testing Add coverage for the byte limit, UTF-8 boundaries, unchanged inputs that fit, and markers that exceed the budget. Verify capped output in paginated thread history and migrated legacy history, and verify persistence metrics use the truncated payload size. GitOrigin-RevId: 01d420d396ad34b42dc5dc208c0bf88d890639ee
173 lines
6.3 KiB
Rust
173 lines
6.3 KiB
Rust
//! Rollout persistence and discovery for Codex session files.
|
|
|
|
use std::sync::LazyLock;
|
|
|
|
use codex_protocol::protocol::SessionSource;
|
|
use serde::de::Error as _;
|
|
use serde_json::Value;
|
|
|
|
pub(crate) mod compression;
|
|
pub(crate) mod config;
|
|
pub(crate) mod list;
|
|
mod maintenance;
|
|
pub(crate) mod metadata;
|
|
mod model_context;
|
|
mod ordinal;
|
|
mod persistence_metrics;
|
|
pub(crate) mod policy;
|
|
pub(crate) mod recorder;
|
|
mod reverse_jsonl_scanner;
|
|
mod rollout_file_name;
|
|
mod rollout_reference_index;
|
|
pub(crate) mod search;
|
|
mod seekable_reader;
|
|
pub(crate) mod session_index;
|
|
mod sqlite_metrics;
|
|
pub mod state_db;
|
|
mod writer_lock;
|
|
|
|
pub use codex_history::CompactedItem;
|
|
pub use codex_history::CompactionResumeMetadata;
|
|
pub use codex_history::InitialHistory;
|
|
pub use codex_history::ResponseItemEnvelope;
|
|
pub use codex_history::ResumedHistory;
|
|
pub use codex_history::RetainedContextEntry;
|
|
pub use codex_history::RetainedContextEvent;
|
|
pub use codex_history::RetainedInputSource;
|
|
pub use codex_history::RolloutItem;
|
|
pub use codex_history::RolloutLine;
|
|
pub use codex_history::resume_multi_agent_version;
|
|
pub(crate) use codex_protocol::protocol;
|
|
|
|
/// Decodes a persisted rollout record without Serde's flattened-envelope buffering.
|
|
///
|
|
/// With `serde_json/arbitrary_precision`, Serde's generic buffer cannot replay
|
|
/// floating-point values nested inside flattened or internally tagged fields:
|
|
/// https://github.com/serde-rs/json/issues/721
|
|
/// https://github.com/serde-rs/serde/issues/1183
|
|
///
|
|
/// Keep this JSON-specific workaround at the persistence boundary so history
|
|
/// remains format-neutral and resume and projection use the same item decoder.
|
|
/// Remove it once Serde supports format-specific buffering.
|
|
pub fn decode_rollout_line(value: Value) -> serde_json::Result<RolloutLine> {
|
|
let Value::Object(mut fields) = value else {
|
|
return Err(serde_json::Error::custom(
|
|
"rollout line must be a JSON object",
|
|
));
|
|
};
|
|
let timestamp = fields
|
|
.remove("timestamp")
|
|
.ok_or_else(|| serde_json::Error::missing_field("timestamp"))
|
|
.and_then(serde_json::from_value)?;
|
|
let ordinal = fields
|
|
.remove("ordinal")
|
|
.map(serde_json::from_value::<Option<u64>>)
|
|
.transpose()?
|
|
.flatten();
|
|
let item = serde_json::from_value(Value::Object(fields))?;
|
|
|
|
Ok(RolloutLine {
|
|
timestamp,
|
|
ordinal,
|
|
item,
|
|
})
|
|
}
|
|
|
|
/// Parses a persisted JSONL rollout record through the canonical JSON decoder.
|
|
pub fn parse_rollout_line(line: &str) -> serde_json::Result<RolloutLine> {
|
|
serde_json::from_str::<Value>(line).and_then(decode_rollout_line)
|
|
}
|
|
|
|
/// Parses persisted JSONL rollout record bytes through the canonical JSON decoder.
|
|
pub fn parse_rollout_line_bytes(bytes: &[u8]) -> serde_json::Result<RolloutLine> {
|
|
serde_json::from_slice::<Value>(bytes).and_then(decode_rollout_line)
|
|
}
|
|
|
|
pub const SESSIONS_SUBDIR: &str = "sessions";
|
|
pub const ARCHIVED_SESSIONS_SUBDIR: &str = "archived_sessions";
|
|
pub static INTERACTIVE_SESSION_SOURCES: LazyLock<Vec<SessionSource>> = LazyLock::new(|| {
|
|
vec![
|
|
SessionSource::Cli,
|
|
SessionSource::VSCode,
|
|
SessionSource::Custom("atlas".to_string()),
|
|
SessionSource::Custom("chatgpt".to_string()),
|
|
]
|
|
});
|
|
|
|
pub use codex_protocol::protocol::SessionMeta;
|
|
pub use compression::RolloutCompressionTrigger;
|
|
pub use compression::RolloutLineReader;
|
|
pub use compression::existing_rollout_path;
|
|
pub use compression::open_rollout_line_reader;
|
|
pub use compression::plain_rollout_path;
|
|
pub use compression::spawn_rollout_compression_worker;
|
|
pub use seekable_reader::open_rollout_seekable_reader;
|
|
pub use seekable_reader::read_rollout_prefix;
|
|
pub use seekable_reader::rollout_contains_prefix;
|
|
|
|
/// Materializes a compressed rollout as plain JSONL before another rollout references it.
|
|
pub async fn materialize_rollout_for_reference(
|
|
path: &std::path::Path,
|
|
) -> std::io::Result<std::path::PathBuf> {
|
|
compression::materialize_rollout_for_append(path, /*writer_lock*/ None).await
|
|
}
|
|
pub use config::Config;
|
|
pub use config::RolloutConfig;
|
|
pub use config::RolloutConfigView;
|
|
pub use list::Cursor;
|
|
pub use list::SortDirection;
|
|
pub use list::ThreadItem;
|
|
pub use list::ThreadListConfig;
|
|
pub use list::ThreadListLayout;
|
|
pub use list::ThreadSortKey;
|
|
pub use list::ThreadsPage;
|
|
pub use list::find_archived_thread_path_by_id_str;
|
|
pub use list::find_rollout_path_by_rollout_id;
|
|
pub use list::find_thread_path_by_id_str;
|
|
#[deprecated(note = "use find_thread_path_by_id_str")]
|
|
pub use list::find_thread_path_by_id_str as find_conversation_path_by_id_str;
|
|
pub use list::get_threads;
|
|
pub use list::get_threads_in_root;
|
|
pub use list::parse_cursor;
|
|
pub use list::read_head_for_summary;
|
|
pub use list::read_session_meta_line;
|
|
pub use list::read_thread_item_from_rollout;
|
|
pub use list::rollout_date_parts;
|
|
pub use maintenance::RolloutMaintenanceGuard;
|
|
pub use maintenance::try_acquire_rollout_maintenance_lock;
|
|
pub use metadata::builder_from_items;
|
|
pub use metadata::forked_from_ordinal_exclusive;
|
|
pub use metadata::rollout_id_from_path;
|
|
pub use model_context::ModelContextScan;
|
|
pub use model_context::ModelContextScanProgress;
|
|
pub use persistence_metrics::RolloutPersistenceBatchMeasurement;
|
|
pub use persistence_metrics::RolloutPersistenceTelemetry;
|
|
pub use persistence_metrics::measure_and_filter_rollout_items;
|
|
pub use policy::into_persisted_rollout_items;
|
|
pub use policy::persisted_rollout_item;
|
|
pub use policy::persisted_rollout_items;
|
|
pub use policy::should_persist_response_item;
|
|
pub use policy::should_persist_response_item_for_memories;
|
|
pub use recorder::RolloutRecorder;
|
|
pub use recorder::RolloutRecorderParams;
|
|
pub use recorder::append_rollout_item_to_path;
|
|
pub use reverse_jsonl_scanner::ReverseJsonlScanner;
|
|
pub use reverse_jsonl_scanner::ScanOutcome;
|
|
pub use rollout_reference_index::RolloutReferenceIndex;
|
|
pub use search::first_rollout_content_match_snippet;
|
|
pub use search::search_rollout_matches;
|
|
pub use search::search_rollout_paths;
|
|
pub use session_index::append_thread_name;
|
|
pub use session_index::find_thread_meta_by_name_str;
|
|
pub use session_index::find_thread_meta_candidates_by_name_str;
|
|
pub use session_index::find_thread_name_by_id;
|
|
pub use session_index::find_thread_names_by_ids;
|
|
pub use session_index::remove_thread_name_entries;
|
|
pub use state_db::StateDbHandle;
|
|
pub use state_db::sqlite_telemetry_recorder;
|
|
pub use writer_lock::WriterLockCoordinator;
|
|
pub use writer_lock::WriterLockGuard;
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|