fix(runtime): pass session messages by reference

Resolves clippy::needless_pass_by_value in session.rs.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim 2026-04-02 14:53:20 +09:00
parent 86bc510722
commit 2959cd1e51
2 changed files with 28 additions and 21 deletions

View file

@ -349,7 +349,7 @@ where
); );
self.session self.session
.push_message(assistant_message.clone()) .push_message(&assistant_message)
.map_err(|error| RuntimeError::new(error.to_string()))?; .map_err(|error| RuntimeError::new(error.to_string()))?;
assistant_messages.push(assistant_message); assistant_messages.push(assistant_message);
@ -440,7 +440,7 @@ where
), ),
}; };
self.session self.session
.push_message(result_message.clone()) .push_message(&result_message)
.map_err(|error| RuntimeError::new(error.to_string()))?; .map_err(|error| RuntimeError::new(error.to_string()))?;
self.record_tool_finished(iterations, &result_message); self.record_tool_finished(iterations, &result_message);
tool_results.push(result_message); tool_results.push(result_message);

View file

@ -173,14 +173,14 @@ impl Session {
Ok(session.with_persistence_path(path.to_path_buf())) Ok(session.with_persistence_path(path.to_path_buf()))
} }
pub fn push_message(&mut self, message: ConversationMessage) -> Result<(), SessionError> { pub fn push_message(&mut self, message: &ConversationMessage) -> Result<(), SessionError> {
self.touch(); self.touch();
self.messages.push(message.clone()); self.messages.push(message.clone());
self.append_persisted_message(&message) self.append_persisted_message(message)
} }
pub fn push_user_text(&mut self, text: impl Into<String>) -> Result<(), SessionError> { pub fn push_user_text(&mut self, text: impl Into<String>) -> Result<(), SessionError> {
self.push_message(ConversationMessage::user_text(text)) self.push_message(&ConversationMessage::user_text(text))
} }
pub fn record_compaction(&mut self, summary: impl Into<String>, removed_message_count: usize) { pub fn record_compaction(&mut self, summary: impl Into<String>, removed_message_count: usize) {
@ -815,6 +815,12 @@ fn current_time_millis() -> u64 {
.unwrap_or_default() .unwrap_or_default()
} }
fn has_jsonl_extension(path: &Path) -> bool {
path.extension()
.and_then(|value| value.to_str())
.is_some_and(|extension| extension.eq_ignore_ascii_case("jsonl"))
}
fn generate_session_id() -> String { fn generate_session_id() -> String {
let millis = current_time_millis(); let millis = current_time_millis();
let counter = SESSION_ID_COUNTER.fetch_add(1, Ordering::Relaxed); let counter = SESSION_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
@ -879,12 +885,8 @@ fn cleanup_rotated_logs(path: &Path) -> Result<(), SessionError> {
entry_path entry_path
.file_name() .file_name()
.and_then(|value| value.to_str()) .and_then(|value| value.to_str())
.is_some_and(|name| { .is_some_and(|name| name.starts_with(&prefix))
name.starts_with(&prefix) && has_jsonl_extension(entry_path)
&& Path::new(name)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("jsonl"))
})
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -910,7 +912,7 @@ mod tests {
use crate::json::JsonValue; use crate::json::JsonValue;
use crate::usage::TokenUsage; use crate::usage::TokenUsage;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
#[test] #[test]
@ -920,7 +922,7 @@ mod tests {
.push_user_text("hello") .push_user_text("hello")
.expect("user message should append"); .expect("user message should append");
session session
.push_message(ConversationMessage::assistant_with_usage( .push_message(&ConversationMessage::assistant_with_usage(
vec![ vec![
ContentBlock::Text { ContentBlock::Text {
text: "thinking".to_string(), text: "thinking".to_string(),
@ -940,7 +942,7 @@ mod tests {
)) ))
.expect("assistant message should append"); .expect("assistant message should append");
session session
.push_message(ConversationMessage::tool_result( .push_message(&ConversationMessage::tool_result(
"tool-1", "bash", "hi", false, "tool-1", "bash", "hi", false,
)) ))
.expect("tool result should append"); .expect("tool result should append");
@ -997,7 +999,7 @@ mod tests {
.push_user_text("hi") .push_user_text("hi")
.expect("user append should succeed"); .expect("user append should succeed");
session session
.push_message(ConversationMessage::assistant(vec![ContentBlock::Text { .push_message(&ConversationMessage::assistant(vec![ContentBlock::Text {
text: "hello".to_string(), text: "hello".to_string(),
}])) }]))
.expect("assistant append should succeed"); .expect("assistant append should succeed");
@ -1060,7 +1062,13 @@ mod tests {
#[test] #[test]
fn rotates_and_cleans_up_large_session_logs() { fn rotates_and_cleans_up_large_session_logs() {
let path = temp_session_path("rotation"); let path = temp_session_path("rotation");
fs::write(&path, "x".repeat((super::ROTATE_AFTER_BYTES + 10) as usize)) fs::write(
&path,
"x".repeat(
usize::try_from(super::ROTATE_AFTER_BYTES + 10)
.expect("rotation threshold should fit usize"),
),
)
.expect("oversized file should write"); .expect("oversized file should write");
rotate_session_file_if_needed(&path).expect("rotation should succeed"); rotate_session_file_if_needed(&path).expect("rotation should succeed");
assert!( assert!(
@ -1089,7 +1097,7 @@ mod tests {
std::env::temp_dir().join(format!("runtime-session-{label}-{nanos}.json")) std::env::temp_dir().join(format!("runtime-session-{label}-{nanos}.json"))
} }
fn rotation_files(path: &PathBuf) -> Vec<PathBuf> { fn rotation_files(path: &Path) -> Vec<PathBuf> {
let stem = path let stem = path
.file_stem() .file_stem()
.and_then(|value| value.to_str()) .and_then(|value| value.to_str())
@ -1103,9 +1111,8 @@ mod tests {
entry_path entry_path
.file_name() .file_name()
.and_then(|value| value.to_str()) .and_then(|value| value.to_str())
.is_some_and(|name| { .is_some_and(|name| name.starts_with(&format!("{stem}.rot-")))
name.starts_with(&format!("{stem}.rot-")) && name.ends_with(".jsonl") && super::has_jsonl_extension(entry_path)
})
}) })
.collect() .collect()
} }