fix(desktop): reconcile child log filter with main

Signed-off-by: Jimmy <1fe240cd1a8cf775f6f3060f115e5a303181f3abf28ad4cb0c2515f4a02b36a8@meshllm.communities.buzz.xyz>
This commit is contained in:
Jimmy
2026-09-02 10:44:42 +10:00
parent 1333a7e2a2
commit c2564888ca
3 changed files with 18 additions and 100 deletions

View File

@@ -990,9 +990,6 @@ pub fn start_managed_agent_process(
Ok(())
}
mod child_log_filter;
use child_log_filter::child_rust_log_filter;
#[cfg(test)]
mod test_fixtures;

View File

@@ -1,91 +0,0 @@
//! Log filter for spawned agent processes.
//!
//! Extracted from `runtime.rs` to keep that file under the desktop file-size
//! ratchet; it is a self-contained decision with its own tests.
/// Log filter for the spawned harness and, through it, the agent.
///
/// `buzz-acp` inherits its stderr to the agent subprocess (`acp.rs:463`), so
/// whatever this filter admits is what lands in the per-agent log file. That
/// makes `buzz_agent` as load-bearing as `buzz_acp`: without it the agent's own
/// turn diagnostics — compaction, the `_Stop` veto, max-rounds — are filtered
/// out before they reach disk, and an agent misbehaving in the desktop leaves
/// no trace to debug it with.
pub(super) fn child_rust_log_filter() -> String {
const DEFAULTS: &str = "buzz_acp=info,buzz_agent=info";
match std::env::var("RUST_LOG") {
// An operator filter that already names both crates is taken verbatim.
Ok(existing) if existing.contains("buzz_acp") && existing.contains("buzz_agent") => {
existing
}
// Otherwise append only the directive that is missing, so an explicit
// `buzz_agent=debug` is not overridden by our default.
Ok(existing) if !existing.trim().is_empty() => {
let mut filter = existing;
if !filter.contains("buzz_acp") {
filter.push_str(",buzz_acp=info");
}
if !filter.contains("buzz_agent") {
filter.push_str(",buzz_agent=info");
}
filter
}
_ => DEFAULTS.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::child_rust_log_filter;
/// Serialize: `RUST_LOG` is process-global and these tests mutate it.
fn with_rust_log<T>(value: Option<&str>, body: impl FnOnce() -> T) -> T {
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
let previous = std::env::var("RUST_LOG").ok();
match value {
Some(v) => std::env::set_var("RUST_LOG", v),
None => std::env::remove_var("RUST_LOG"),
}
let result = body();
match previous {
Some(v) => std::env::set_var("RUST_LOG", v),
None => std::env::remove_var("RUST_LOG"),
}
result
}
#[test]
fn default_admits_both_the_harness_and_the_agent() {
// buzz_agent was missing here, which silently discarded every agent
// turn diagnostic from the desktop's per-agent log files.
let filter = with_rust_log(None, child_rust_log_filter);
assert!(filter.contains("buzz_acp=info"));
assert!(filter.contains("buzz_agent=info"));
}
#[test]
fn an_unrelated_operator_filter_is_extended_not_replaced() {
let filter = with_rust_log(Some("warn"), child_rust_log_filter);
assert!(filter.starts_with("warn"), "operator filter must survive");
assert!(filter.contains("buzz_acp=info"));
assert!(filter.contains("buzz_agent=info"));
}
#[test]
fn an_explicit_agent_directive_is_not_downgraded() {
let filter = with_rust_log(Some("buzz_agent=debug"), child_rust_log_filter);
assert!(filter.contains("buzz_agent=debug"));
assert!(!filter.contains("buzz_agent=info"), "must not override");
assert!(filter.contains("buzz_acp=info"), "still needs the harness");
}
#[test]
fn a_fully_specified_filter_is_taken_verbatim() {
let filter = with_rust_log(
Some("buzz_acp=warn,buzz_agent=trace"),
child_rust_log_filter,
);
assert_eq!(filter, "buzz_acp=warn,buzz_agent=trace");
}
}

View File

@@ -74,14 +74,26 @@ pub(crate) fn resolve_session_title(display_name: Option<&str>, name: &str) -> O
.find(|value| !value.is_empty())
}
/// Build the `RUST_LOG` value forwarded to the agent child: keep an existing
/// filter that already mentions `buzz_acp`, append `buzz_acp=info` to any other
/// non-empty filter, and default to `buzz_acp=info` when unset.
/// Build the `RUST_LOG` value forwarded to the agent child, retaining an
/// operator filter while ensuring both harness and agent diagnostics reach the
/// per-agent log.
pub(crate) fn child_rust_log_filter() -> String {
const DEFAULTS: &str = "buzz_acp=info,buzz_agent=info";
match std::env::var("RUST_LOG") {
Ok(existing) if existing.contains("buzz_acp") => existing,
Ok(existing) if !existing.trim().is_empty() => format!("{existing},buzz_acp=info"),
_ => "buzz_acp=info".to_string(),
Ok(existing) if existing.contains("buzz_acp") && existing.contains("buzz_agent") => {
existing
}
Ok(existing) if !existing.trim().is_empty() => {
let mut filter = existing;
if !filter.contains("buzz_acp") {
filter.push_str(",buzz_acp=info");
}
if !filter.contains("buzz_agent") {
filter.push_str(",buzz_agent=info");
}
filter
}
_ => DEFAULTS.to_string(),
}
}