This commit is contained in:
Ahmed Ibrahim
2026-01-19 09:49:08 -08:00
parent bb275ebbfb
commit 8c03179f7d
4 changed files with 15 additions and 24 deletions
+1 -1
View File
@@ -93,7 +93,7 @@ tracing = { workspace = true, features = ["log"] }
tree-sitter = { workspace = true }
tree-sitter-bash = { workspace = true }
url = { workspace = true }
uuid = { workspace = true, features = ["serde", "v4", "v5"] }
uuid = { workspace = true, features = ["serde", "v4", "v5", "v7"] }
which = { workspace = true }
wildmatch = { workspace = true }
+6 -2
View File
@@ -699,7 +699,7 @@ impl Session {
.await
.map(Arc::new);
}
let state = SessionState::new(session_configuration.clone(), conversation_id);
let state = SessionState::new(session_configuration.clone());
let services = SessionServices {
mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::default())),
@@ -1352,7 +1352,7 @@ impl Session {
turn_context: &TurnContext,
rollout_items: &[RolloutItem],
) -> Vec<ResponseItem> {
let mut history = ContextManager::new();
let mut history = ContextManager::new(&self.codex_home().await);
for item in rollout_items {
match item {
RolloutItem::ResponseItem(response_item) => {
@@ -1571,6 +1571,10 @@ impl Session {
self.send_event(turn_context, event).await;
}
pub(crate) async fn codex_home(&self) -> PathBuf {
self.state.lock().await.session_configuration.codex_home().clone()
}
pub(crate) async fn set_total_tokens_full(&self, turn_context: &TurnContext) {
if let Some(context_window) = turn_context.client.get_model_context_window() {
let mut state = self.state.lock().await;
+7 -12
View File
@@ -26,15 +26,15 @@ pub(crate) struct ContextManager {
/// The oldest items are at the beginning of the vector.
items: Vec<ResponseItem>,
token_info: Option<TokenUsageInfo>,
user_message_dir: Option<PathBuf>,
codex_home: PathBuf,
}
impl ContextManager {
pub(crate) fn new() -> Self {
pub(crate) fn new(codex_home: &PathBuf) -> Self {
Self {
items: Vec::new(),
token_info: TokenUsageInfo::new_or_append(&None, &None, None),
user_message_dir: None,
codex_home: codex_home.clone(),
}
}
@@ -46,10 +46,6 @@ impl ContextManager {
self.token_info = info;
}
pub(crate) fn set_user_message_dir(&mut self, dir: Option<PathBuf>) {
self.user_message_dir = dir;
}
pub(crate) fn set_token_usage_full(&mut self, context_window: i64) {
match &mut self.token_info {
Some(info) => info.fill_to_context_window(context_window),
@@ -331,10 +327,9 @@ impl ContextManager {
return item.clone();
}
let Some(user_message_dir) = self.user_message_dir.as_ref() else {
return item.clone();
};
let Some(path) = write_message_to_user_dir(&text, user_message_dir) else {
let id_str = Uuid::now_v7().to_string();
let user_message_dir = self.codex_home.join("context").join("usermsgs").join(&id_str);
let Some(path) = write_message_to_user_dir(&text, &user_message_dir) else {
return item.clone();
};
@@ -390,7 +385,7 @@ fn write_message_to_user_dir(text: &str, user_message_dir: &Path) -> Option<Path
return None;
}
let id = Uuid::new_v4();
let id = Uuid::now_v7();
let path = user_message_dir.join(format!("user-message-{id}.txt"));
if let Err(err) = std::fs::write(&path, text.as_bytes()) {
warn!(error = %err, "failed to write user message to file");
+1 -9
View File
@@ -1,6 +1,5 @@
//! Session-wide mutable state.
use codex_protocol::ThreadId;
use codex_protocol::models::ResponseItem;
use crate::codex::SessionConfiguration;
@@ -21,15 +20,8 @@ impl SessionState {
/// Create a new session state mirroring previous `State::default()` semantics.
pub(crate) fn new(
session_configuration: SessionConfiguration,
conversation_id: ThreadId,
) -> Self {
let mut history = ContextManager::new();
let user_message_dir = session_configuration
.codex_home()
.join("context")
.join("usermsgs")
.join(conversation_id.to_string());
history.set_user_message_dir(Some(user_message_dir));
let history = ContextManager::new(session_configuration.codex_home());
Self {
session_configuration,
history,