Skip to content

Commit 13bfcde

Browse files
committed
chore: cargo fmt --all on fix-160 branch
1 parent 5f27218 commit 13bfcde

2 files changed

Lines changed: 115 additions & 55 deletions

File tree

rust/crates/runtime/src/session_control.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,22 +1242,31 @@ mod tests {
12421242
let session = persist_session_via_store(&store, "160 regression test");
12431243

12441244
// when/then — session exists and is listed before deletion
1245-
assert!(!store.list_sessions().expect("list").is_empty(),
1246-
"store should have at least one session");
1247-
assert!(store.session_exists(&session.session_id),
1248-
"session should exist before deletion");
1245+
assert!(
1246+
!store.list_sessions().expect("list").is_empty(),
1247+
"store should have at least one session"
1248+
);
1249+
assert!(
1250+
store.session_exists(&session.session_id),
1251+
"session should exist before deletion"
1252+
);
12491253

12501254
// when — delete the session
1251-
let deleted = store.delete_session(&session.session_id)
1255+
let deleted = store
1256+
.delete_session(&session.session_id)
12521257
.expect("delete should succeed");
12531258

12541259
// then — session is gone
12551260
assert_eq!(deleted.id, session.session_id);
12561261
assert!(!deleted.path.exists(), "session file should be removed");
1257-
assert!(!store.session_exists(&session.session_id),
1258-
"session should not exist after deletion");
1259-
assert!(store.list_sessions().expect("list").is_empty(),
1260-
"store should have no sessions after deletion");
1262+
assert!(
1263+
!store.session_exists(&session.session_id),
1264+
"session should not exist after deletion"
1265+
);
1266+
assert!(
1267+
store.list_sessions().expect("list").is_empty(),
1268+
"store should have no sessions after deletion"
1269+
);
12611270

12621271
fs::remove_dir_all(base).expect("temp dir should clean up");
12631272
}

rust/crates/runtime/src/trident.rs

Lines changed: 97 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ impl TridentStats {
7878
self.messages_clustered, self.clusters_found
7979
),
8080
format!(" Original: {} messages", self.original_message_count),
81-
format!(" Final: {} messages ({:.1}x compression)", self.final_message_count, compression),
81+
format!(
82+
" Final: {} messages ({:.1}x compression)",
83+
self.final_message_count, compression
84+
),
8285
];
8386
if self.tokens_saved_estimate > 0 {
8487
lines.push(format!(
@@ -121,7 +124,8 @@ pub fn trident_compact_session(
121124
}
122125

123126
if trident_config.collapse_enabled {
124-
let (collapsed, chains, collapsed_count) = stage2_collapse(&messages, trident_config.collapse_threshold);
127+
let (collapsed, chains, collapsed_count) =
128+
stage2_collapse(&messages, trident_config.collapse_threshold);
125129
stats.collapsed_chains = chains;
126130
stats.messages_collapsed = collapsed_count;
127131
messages = collapsed;
@@ -178,10 +182,10 @@ fn stage1_supersede(messages: &[ConversationMessage]) -> (Vec<ConversationMessag
178182
for (i, msg) in messages.iter().enumerate() {
179183
for block in &msg.blocks {
180184
if let Some((path, op_type)) = extract_file_operation(block) {
181-
file_ops.entry(path).or_default().push(FileOperation {
182-
index: i,
183-
op_type,
184-
});
185+
file_ops
186+
.entry(path)
187+
.or_default()
188+
.push(FileOperation { index: i, op_type });
185189
}
186190
}
187191
}
@@ -235,7 +239,9 @@ fn extract_file_operation(block: &ContentBlock) -> Option<(String, FileOp)> {
235239
};
236240
Some((path, op_type))
237241
}
238-
ContentBlock::ToolResult { tool_name, output, .. } => {
242+
ContentBlock::ToolResult {
243+
tool_name, output, ..
244+
} => {
239245
let path = extract_path_from_tool_output(tool_name, output)?;
240246
let op_type = match tool_name.as_str() {
241247
"read_file" | "Read" => FileOp::Read,
@@ -251,8 +257,10 @@ fn extract_file_operation(block: &ContentBlock) -> Option<(String, FileOp)> {
251257
}
252258

253259
fn extract_path_from_tool_input(tool_name: &str, input: &str) -> Option<String> {
254-
if !matches!(tool_name, "read_file" | "write_file" | "edit_file" | "Read" | "Write" | "Edit")
255-
{
260+
if !matches!(
261+
tool_name,
262+
"read_file" | "write_file" | "edit_file" | "Read" | "Write" | "Edit"
263+
) {
256264
return None;
257265
}
258266
serde_json::from_str::<serde_json::Value>(input)
@@ -266,8 +274,10 @@ fn extract_path_from_tool_input(tool_name: &str, input: &str) -> Option<String>
266274
}
267275

268276
fn extract_path_from_tool_output(tool_name: &str, output: &str) -> Option<String> {
269-
if !matches!(tool_name, "read_file" | "write_file" | "edit_file" | "Read" | "Write" | "Edit")
270-
{
277+
if !matches!(
278+
tool_name,
279+
"read_file" | "write_file" | "edit_file" | "Read" | "Write" | "Edit"
280+
) {
271281
return None;
272282
}
273283
serde_json::from_str::<serde_json::Value>(output)
@@ -341,15 +351,25 @@ fn stage2_collapse(
341351
}
342352

343353
fn is_chatty_message(msg: &ConversationMessage) -> bool {
344-
let total_chars: usize = msg.blocks.iter().map(|b| match b {
345-
ContentBlock::Text { text } => text.len(),
346-
ContentBlock::ToolUse { input, .. } => input.len(),
347-
ContentBlock::ToolResult { output, .. } => output.len(),
348-
ContentBlock::Thinking { thinking, .. } => thinking.len(),
349-
}).sum();
354+
let total_chars: usize = msg
355+
.blocks
356+
.iter()
357+
.map(|b| match b {
358+
ContentBlock::Text { text } => text.len(),
359+
ContentBlock::ToolUse { input, .. } => input.len(),
360+
ContentBlock::ToolResult { output, .. } => output.len(),
361+
ContentBlock::Thinking { thinking, .. } => thinking.len(),
362+
})
363+
.sum();
350364

351-
let has_tool_use = msg.blocks.iter().any(|b| matches!(b, ContentBlock::ToolUse { .. }));
352-
let has_tool_result = msg.blocks.iter().any(|b| matches!(b, ContentBlock::ToolResult { .. }));
365+
let has_tool_use = msg
366+
.blocks
367+
.iter()
368+
.any(|b| matches!(b, ContentBlock::ToolUse { .. }));
369+
let has_tool_result = msg
370+
.blocks
371+
.iter()
372+
.any(|b| matches!(b, ContentBlock::ToolResult { .. }));
353373

354374
if has_tool_use || has_tool_result {
355375
return false;
@@ -465,16 +485,12 @@ fn stage3_cluster(
465485
cluster_buffers.entry(cid).or_default().push(*msg_idx);
466486
}
467487

468-
469-
470488
for (i, msg) in messages.iter().enumerate() {
471489
if let Some(&cid) = cluster_assignments.get(&i) {
472490
if let Some(buffer) = cluster_buffers.get_mut(&cid) {
473491
if buffer[0] == i {
474-
let cluster_messages: Vec<&ConversationMessage> = buffer
475-
.iter()
476-
.filter_map(|&idx| messages.get(idx))
477-
.collect();
492+
let cluster_messages: Vec<&ConversationMessage> =
493+
buffer.iter().filter_map(|&idx| messages.get(idx)).collect();
478494
let summary = generate_cluster_summary(&cluster_messages);
479495
result.push(ConversationMessage {
480496
role: MessageRole::System,
@@ -520,7 +536,9 @@ fn fingerprint_message(index: usize, msg: &ConversationMessage) -> Option<Messag
520536
}
521537
text_length += input.len();
522538
}
523-
ContentBlock::ToolResult { tool_name, output, .. } => {
539+
ContentBlock::ToolResult {
540+
tool_name, output, ..
541+
} => {
524542
tool_names.insert(tool_name.clone());
525543
if let Some(path) = extract_path_from_tool_output(tool_name, output) {
526544
file_paths.insert(path);
@@ -596,7 +614,9 @@ fn generate_cluster_summary(messages: &[&ConversationMessage]) -> String {
596614
file_paths.insert(path);
597615
}
598616
}
599-
ContentBlock::ToolResult { tool_name, output, .. } => {
617+
ContentBlock::ToolResult {
618+
tool_name, output, ..
619+
} => {
600620
tool_names.insert(tool_name.clone());
601621
if let Some(path) = extract_path_from_tool_output(tool_name, output) {
602622
file_paths.insert(path);
@@ -641,7 +661,7 @@ fn estimate_message_tokens(message: &ConversationMessage) -> usize {
641661
} => (tool_name.len() + output.len()) / 4 + 1,
642662
ContentBlock::Thinking { thinking, .. } => thinking.len() / 4 + 1,
643663
})
644-
.sum()
664+
.sum()
645665
}
646666

647667
fn truncate_text(text: &str, max_chars: usize) -> String {
@@ -667,13 +687,23 @@ mod tests {
667687
name: "read_file".to_string(),
668688
input: r#"{"path":"src/main.rs"}"#.to_string(),
669689
}]),
670-
ConversationMessage::tool_result("1", "read_file", r#"{"path":"src/main.rs","content":"old"}"#, false),
690+
ConversationMessage::tool_result(
691+
"1",
692+
"read_file",
693+
r#"{"path":"src/main.rs","content":"old"}"#,
694+
false,
695+
),
671696
ConversationMessage::assistant(vec![ContentBlock::ToolUse {
672697
id: "2".to_string(),
673698
name: "edit_file".to_string(),
674699
input: r#"{"path":"src/main.rs","old":"old","new":"new"}"#.to_string(),
675700
}]),
676-
ConversationMessage::tool_result("2", "edit_file", r#"{"path":"src/main.rs","ok":true}"#, false),
701+
ConversationMessage::tool_result(
702+
"2",
703+
"edit_file",
704+
r#"{"path":"src/main.rs","ok":true}"#,
705+
false,
706+
),
677707
];
678708

679709
let (kept, superseded) = stage1_supersede(&messages);
@@ -689,7 +719,12 @@ mod tests {
689719
name: "read_file".to_string(),
690720
input: r#"{"path":"src/main.rs"}"#.to_string(),
691721
}]),
692-
ConversationMessage::tool_result("1", "read_file", r#"{"path":"src/main.rs","content":"data"}"#, false),
722+
ConversationMessage::tool_result(
723+
"1",
724+
"read_file",
725+
r#"{"path":"src/main.rs","content":"data"}"#,
726+
false,
727+
),
693728
];
694729

695730
let (kept, superseded) = stage1_supersede(&messages);
@@ -706,11 +741,13 @@ mod tests {
706741
text: format!("got {i}"),
707742
}]));
708743
}
709-
messages.push(ConversationMessage::assistant(vec![ContentBlock::ToolUse {
710-
id: "t".to_string(),
711-
name: "bash".to_string(),
712-
input: r#"{"command":"ls"}"#.to_string(),
713-
}]));
744+
messages.push(ConversationMessage::assistant(vec![
745+
ContentBlock::ToolUse {
746+
id: "t".to_string(),
747+
name: "bash".to_string(),
748+
input: r#"{"command":"ls"}"#.to_string(),
749+
},
750+
]));
714751

715752
let (result, chains, collapsed) = stage2_collapse(&messages, 4);
716753
assert!(chains > 0, "should collapse at least one chain");
@@ -722,11 +759,13 @@ mod tests {
722759
fn stage3_clusters_similar_messages() {
723760
let mut messages = vec![];
724761
for i in 0..5 {
725-
messages.push(ConversationMessage::assistant(vec![ContentBlock::ToolUse {
726-
id: format!("read_{i}"),
727-
name: "read_file".to_string(),
728-
input: format!(r#"{{"path":"src/{i}.rs"}}"#),
729-
}]));
762+
messages.push(ConversationMessage::assistant(vec![
763+
ContentBlock::ToolUse {
764+
id: format!("read_{i}"),
765+
name: "read_file".to_string(),
766+
input: format!(r#"{{"path":"src/{i}.rs"}}"#),
767+
},
768+
]));
730769
messages.push(ConversationMessage::tool_result(
731770
&format!("read_{i}"),
732771
"read_file",
@@ -735,8 +774,7 @@ mod tests {
735774
));
736775
}
737776

738-
let (result, clusters, clustered) =
739-
stage3_cluster(&messages, 3, 0.4);
777+
let (result, clusters, clustered) = stage3_cluster(&messages, 3, 0.4);
740778
assert!(clusters > 0, "should find at least one cluster");
741779
assert!(clustered > 0);
742780
assert!(result.len() < messages.len());
@@ -752,13 +790,23 @@ mod tests {
752790
name: "read_file".to_string(),
753791
input: r#"{"path":"src/main.rs"}"#.to_string(),
754792
}]),
755-
ConversationMessage::tool_result("1", "read_file", r#"{"path":"src/main.rs","content":"fn main() { buggy }"}"#, false),
793+
ConversationMessage::tool_result(
794+
"1",
795+
"read_file",
796+
r#"{"path":"src/main.rs","content":"fn main() { buggy }"}"#,
797+
false,
798+
),
756799
ConversationMessage::assistant(vec![ContentBlock::ToolUse {
757800
id: "2".to_string(),
758801
name: "edit_file".to_string(),
759802
input: r#"{"path":"src/main.rs","old":"buggy","new":"fixed"}"#.to_string(),
760803
}]),
761-
ConversationMessage::tool_result("2", "edit_file", r#"{"path":"src/main.rs","ok":true}"#, false),
804+
ConversationMessage::tool_result(
805+
"2",
806+
"edit_file",
807+
r#"{"path":"src/main.rs","ok":true}"#,
808+
false,
809+
),
762810
ConversationMessage::assistant(vec![ContentBlock::Text {
763811
text: "Fixed the bug in main.rs".to_string(),
764812
}]),
@@ -774,7 +822,10 @@ mod tests {
774822
&trident_config,
775823
);
776824

777-
assert!(result.removed_message_count > 0 || result.compacted_session.messages.len() < session.messages.len());
825+
assert!(
826+
result.removed_message_count > 0
827+
|| result.compacted_session.messages.len() < session.messages.len()
828+
);
778829
}
779830

780831
#[test]

0 commit comments

Comments
 (0)