Skip to content

Commit d404275

Browse files
committed
style: format runtime memory logging follow-ups
1 parent 1f6bb76 commit d404275

7 files changed

Lines changed: 109 additions & 56 deletions

File tree

src/agent/compaction_support.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ impl Agent {
7272
"context_limit_auto_compaction",
7373
)
7474
.with_session_id(self.session.id.clone())
75-
.with_detail(format!("dropped_messages={dropped},usage_pct={usage_pct:.1}"))
75+
.with_detail(format!(
76+
"dropped_messages={dropped},usage_pct={usage_pct:.1}"
77+
))
7678
.force_attribution(),
7779
);
7880

src/agent/status_support.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::*;
22

33
impl Agent {
4-
pub fn session_memory_profile_snapshot(&mut self) -> crate::session::SessionMemoryProfileSnapshot {
4+
pub fn session_memory_profile_snapshot(
5+
&mut self,
6+
) -> crate::session::SessionMemoryProfileSnapshot {
57
self.session.memory_profile_snapshot()
68
}
79

src/runtime_memory_log.rs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,17 @@ impl RuntimeMemoryLogController {
214214
&self.config
215215
}
216216

217-
pub fn should_write_process_for_event(&self, now: Instant, event: &RuntimeMemoryLogEvent) -> bool {
217+
pub fn should_write_process_for_event(
218+
&self,
219+
now: Instant,
220+
event: &RuntimeMemoryLogEvent,
221+
) -> bool {
218222
event.force_attribution
219223
|| self
220224
.last_process_sample_at
221-
.map(|last| now.saturating_duration_since(last) >= self.config.event_process_min_spacing)
225+
.map(|last| {
226+
now.saturating_duration_since(last) >= self.config.event_process_min_spacing
227+
})
222228
.unwrap_or(true)
223229
}
224230

@@ -249,7 +255,11 @@ impl RuntimeMemoryLogController {
249255
event: Option<&RuntimeMemoryLogEvent>,
250256
) -> RuntimeMemoryLogSampling {
251257
let mut pending_categories = pending_categories(&self.pending_events);
252-
if let Some(event) = event && !pending_categories.iter().any(|value| value == &event.category) {
258+
if let Some(event) = event
259+
&& !pending_categories
260+
.iter()
261+
.any(|value| value == &event.category)
262+
{
253263
if pending_categories.len() < MAX_PENDING_CATEGORIES {
254264
pending_categories.push(event.category.clone());
255265
}
@@ -275,13 +285,19 @@ impl RuntimeMemoryLogController {
275285

276286
let mut threshold_reasons = Vec::new();
277287
let mut forced = false;
278-
if let Some(event) = event && event.force_attribution {
288+
if let Some(event) = event
289+
&& event.force_attribution
290+
{
279291
forced = true;
280292
threshold_reasons.push(format!("event:{}", event.category));
281293
}
282294
if !self.pending_events.is_empty() {
283295
threshold_reasons.push("pending_events".to_string());
284-
if self.pending_events.iter().any(|value| value.force_attribution) {
296+
if self
297+
.pending_events
298+
.iter()
299+
.any(|value| value.force_attribution)
300+
{
285301
forced = true;
286302
}
287303
}
@@ -370,12 +386,14 @@ pub fn server_logging_config() -> RuntimeMemoryLogConfig {
370386
.or_else(|| legacy_interval_secs.map(|value| value.saturating_mul(3)))
371387
.filter(|value| *value >= MIN_ATTRIBUTION_INTERVAL_SECS)
372388
.unwrap_or(DEFAULT_ATTRIBUTION_INTERVAL_SECS);
373-
let attribution_min_spacing_secs = env_u64("JCODE_RUNTIME_MEMORY_LOG_ATTRIBUTION_MIN_SPACING_SECS")
374-
.filter(|value| *value >= MIN_ATTRIBUTION_MIN_SPACING_SECS)
375-
.unwrap_or(DEFAULT_ATTRIBUTION_MIN_SPACING_SECS);
376-
let event_process_min_spacing_secs = env_u64("JCODE_RUNTIME_MEMORY_LOG_EVENT_PROCESS_MIN_SPACING_SECS")
377-
.filter(|value| *value >= MIN_EVENT_PROCESS_MIN_SPACING_SECS)
378-
.unwrap_or(DEFAULT_EVENT_PROCESS_MIN_SPACING_SECS);
389+
let attribution_min_spacing_secs =
390+
env_u64("JCODE_RUNTIME_MEMORY_LOG_ATTRIBUTION_MIN_SPACING_SECS")
391+
.filter(|value| *value >= MIN_ATTRIBUTION_MIN_SPACING_SECS)
392+
.unwrap_or(DEFAULT_ATTRIBUTION_MIN_SPACING_SECS);
393+
let event_process_min_spacing_secs =
394+
env_u64("JCODE_RUNTIME_MEMORY_LOG_EVENT_PROCESS_MIN_SPACING_SECS")
395+
.filter(|value| *value >= MIN_EVENT_PROCESS_MIN_SPACING_SECS)
396+
.unwrap_or(DEFAULT_EVENT_PROCESS_MIN_SPACING_SECS);
379397
let pss_delta_threshold_bytes = env_u64("JCODE_RUNTIME_MEMORY_LOG_PSS_DELTA_THRESHOLD_MB")
380398
.unwrap_or(DEFAULT_PSS_DELTA_THRESHOLD_MB)
381399
.saturating_mul(1024 * 1024);
@@ -401,7 +419,9 @@ pub fn install_event_sink(sender: mpsc::UnboundedSender<RuntimeMemoryLogEvent>)
401419
}
402420

403421
pub fn emit_event(event: RuntimeMemoryLogEvent) {
404-
if let Ok(guard) = event_sink().lock() && let Some(sender) = guard.as_ref() {
422+
if let Ok(guard) = event_sink().lock()
423+
&& let Some(sender) = guard.as_ref()
424+
{
405425
let _ = sender.send(event);
406426
}
407427
}
@@ -503,7 +523,9 @@ fn bytes_to_mb_string(bytes: u64) -> String {
503523
fn server_log_path_for(now: chrono::DateTime<Utc>) -> Result<PathBuf> {
504524
let dir = server_logs_dir()?;
505525
let date = now.format("%Y-%m-%d");
506-
Ok(dir.join(format!("{SERVER_LOG_FILE_PREFIX}{date}{SERVER_LOG_FILE_SUFFIX}")))
526+
Ok(dir.join(format!(
527+
"{SERVER_LOG_FILE_PREFIX}{date}{SERVER_LOG_FILE_SUFFIX}"
528+
)))
507529
}
508530

509531
fn is_server_log_file(path: &Path) -> bool {
@@ -646,21 +668,25 @@ mod tests {
646668
},
647669
);
648670
let process = crate::process_memory::ProcessMemorySnapshot::default();
649-
assert!(controller
650-
.build_sampling_for_attribution(
651-
now + Duration::from_secs(10),
652-
&process,
653-
Some(&RuntimeMemoryLogEvent::new("turn_completed", "turn").force_attribution()),
654-
None,
655-
)
656-
.is_none());
657-
assert!(controller
658-
.build_sampling_for_attribution(
659-
now + Duration::from_secs(31),
660-
&process,
661-
Some(&RuntimeMemoryLogEvent::new("turn_completed", "turn").force_attribution()),
662-
None,
663-
)
664-
.is_some());
671+
assert!(
672+
controller
673+
.build_sampling_for_attribution(
674+
now + Duration::from_secs(10),
675+
&process,
676+
Some(&RuntimeMemoryLogEvent::new("turn_completed", "turn").force_attribution()),
677+
None,
678+
)
679+
.is_none()
680+
);
681+
assert!(
682+
controller
683+
.build_sampling_for_attribution(
684+
now + Duration::from_secs(31),
685+
&process,
686+
Some(&RuntimeMemoryLogEvent::new("turn_completed", "turn").force_attribution()),
687+
None,
688+
)
689+
.is_some()
690+
);
665691
}
666692
}

src/server/client_actions.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,15 +1137,15 @@ pub(super) fn handle_compact(
11371137
.force_attribution(),
11381138
);
11391139
ServerEvent::CompactResult {
1140-
id,
1141-
message: format!(
1142-
"{}\n\n📦 **Compacting context** (manual) — summarizing older messages in the background to stay within the context window.\n\
1140+
id,
1141+
message: format!(
1142+
"{}\n\n📦 **Compacting context** (manual) — summarizing older messages in the background to stay within the context window.\n\
11431143
The summary will be applied automatically when ready.",
1144-
status_msg
1145-
),
1146-
success: true,
1144+
status_msg
1145+
),
1146+
success: true,
1147+
}
11471148
}
1148-
},
11491149
Err(reason) => ServerEvent::CompactResult {
11501150
id,
11511151
message: format!("{status_msg}\n\n⚠ **Cannot compact:** {reason}"),

src/server/client_disconnect_cleanup.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,24 @@ pub(super) async fn cleanup_client_connection(
138138
let working_dir = agent.working_dir().map(|dir| dir.to_string());
139139
drop(agent);
140140
let event = match disposition {
141-
DisconnectDisposition::Closed => crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
142-
"session_closed",
143-
"client_disconnected",
144-
),
145-
DisconnectDisposition::Crashed => crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
146-
"session_crashed",
147-
"client_disconnected_while_processing",
148-
),
149-
DisconnectDisposition::Reloading => crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
150-
"session_reloading",
151-
"server_reload_disconnect",
152-
),
141+
DisconnectDisposition::Closed => {
142+
crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
143+
"session_closed",
144+
"client_disconnected",
145+
)
146+
}
147+
DisconnectDisposition::Crashed => {
148+
crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
149+
"session_crashed",
150+
"client_disconnected_while_processing",
151+
)
152+
}
153+
DisconnectDisposition::Reloading => {
154+
crate::runtime_memory_log::RuntimeMemoryLogEvent::new(
155+
"session_reloading",
156+
"server_reload_disconnect",
157+
)
158+
}
153159
}
154160
.with_session_id(sid.clone())
155161
.force_attribution();

src/session.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ impl Session {
916916
}
917917

918918
fn rebuild_memory_profile_cache(&mut self) {
919-
let message_stats = summarize_message_content(self.messages.iter().map(|message| &message.content));
919+
let message_stats =
920+
summarize_message_content(self.messages.iter().map(|message| &message.content));
920921
let provider_cache_stats = summarize_message_content(
921922
self.provider_messages_cache
922923
.iter()
@@ -930,11 +931,19 @@ impl Session {
930931
env_snapshots_count: self.env_snapshots.len(),
931932
env_snapshots_json_bytes: self.env_snapshots.iter().map(estimate_json_bytes).sum(),
932933
memory_injections_count: self.memory_injections.len(),
933-
memory_injections_json_bytes: self.memory_injections.iter().map(estimate_json_bytes).sum(),
934+
memory_injections_json_bytes: self
935+
.memory_injections
936+
.iter()
937+
.map(estimate_json_bytes)
938+
.sum(),
934939
replay_events_count: self.replay_events.len(),
935940
replay_events_json_bytes: self.replay_events.iter().map(estimate_json_bytes).sum(),
936941
provider_cache_count: self.provider_messages_cache.len(),
937-
provider_cache_json_bytes: self.provider_messages_cache.iter().map(estimate_json_bytes).sum(),
942+
provider_cache_json_bytes: self
943+
.provider_messages_cache
944+
.iter()
945+
.map(estimate_json_bytes)
946+
.sum(),
938947
provider_cache_stats,
939948
};
940949
self.memory_profile_dirty = false;
@@ -969,9 +978,15 @@ impl Session {
969978
+ compaction_json_bytes,
970979
provider_cache_json_bytes: self.memory_profile_cache.provider_cache_json_bytes,
971980
canonical_tool_result_bytes: self.memory_profile_cache.message_stats.tool_result_bytes,
972-
provider_cache_tool_result_bytes: self.memory_profile_cache.provider_cache_stats.tool_result_bytes,
981+
provider_cache_tool_result_bytes: self
982+
.memory_profile_cache
983+
.provider_cache_stats
984+
.tool_result_bytes,
973985
canonical_large_blob_bytes: self.memory_profile_cache.message_stats.large_block_bytes,
974-
provider_cache_large_blob_bytes: self.memory_profile_cache.provider_cache_stats.large_block_bytes,
986+
provider_cache_large_blob_bytes: self
987+
.memory_profile_cache
988+
.provider_cache_stats
989+
.large_block_bytes,
975990
}
976991
}
977992

src/tui/test_harness.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ pub struct SimulatedInstant {
8989

9090
impl SimulatedInstant {
9191
pub fn elapsed(&self) -> Duration {
92-
let now = get_test_clock().map(|c| read_unpoisoned(c).now_ms()).unwrap_or(0);
92+
let now = get_test_clock()
93+
.map(|c| read_unpoisoned(c).now_ms())
94+
.unwrap_or(0);
9395
Duration::from_millis(now.saturating_sub(self.offset_ms))
9496
}
9597

0 commit comments

Comments
 (0)