From 9881cf58b0a6d1c4a8b7394d3dc9438b9481a9b5 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Wed, 18 Nov 2026 21:55:50 +0800 Subject: [PATCH 1/7] Preserve explicit tracing setup selections --- Cargo.lock | 2 +- src/args.rs | 9 +++++++-- src/experiments/mod.rs | 1 + src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ src/setup/mod.rs | 1 + src/trace_host.rs | 28 +++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cae6b197..14163c65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "bt-daemon" version = "0.1.0" -source = "git+https://github.com/braintrustdata/braintrust-coding-agent-plugins?rev=ff09caa4725747702d3335c5485b81e283b8849f#ff09caa4725747702d3335c5485b81e283b8849f" +source = "git+https://github.com/braintrustdata/braintrust-coding-agent-plugins?rev=df963568a743509cbdeb63febe40ad88ed2e757a#df963568a743509cbdeb63febe40ad88ed2e757a" dependencies = [ "anyhow", "async-trait", diff --git a/src/args.rs b/src/args.rs index 718d95bf..0d4219dc 100644 --- a/src/args.rs +++ b/src/args.rs @@ -120,6 +120,9 @@ pub struct BaseArgs { #[arg(short = 'o', long = "org", env = "BRAINTRUST_ORG_NAME", global = true)] pub org_name: Option, + #[arg(skip)] + pub trace_org_explicit: bool, + /// Override active project #[arg( short = 'p', @@ -129,6 +132,9 @@ pub struct BaseArgs { global = true )] pub project: Option, + + #[arg(skip)] + pub trace_project_explicit: bool, } #[derive(Debug, Clone, Args)] @@ -189,8 +195,7 @@ impl From for BaseArgs { fn from(login: LoginBaseArgs) -> Self { Self { login, - org_name: None, - project: None, + ..Default::default() } } } diff --git a/src/experiments/mod.rs b/src/experiments/mod.rs index e45f17e8..2aec3224 100644 --- a/src/experiments/mod.rs +++ b/src/experiments/mod.rs @@ -416,6 +416,7 @@ mod tests { }, org_name: Some("explicit-org".to_string()), project: Some("explicit-project".to_string()), + ..Default::default() }; let parsed = parse_experiment_compare_url( "https://www.example.test/app/url-org/p/url-project/experiments/baseline?c=challenger", diff --git a/src/main.rs b/src/main.rs index f0b4259a..3faa8151 100644 --- a/src/main.rs +++ b/src/main.rs @@ -379,6 +379,9 @@ fn try_main() -> Result<()> { cmd.args.cli_project = cmd.base.project.clone(); } } + if let Commands::Trace(cmd) = &mut cli.command { + apply_trace_arg_sources(&matches, &mut cmd.base); + } apply_base_arg_sources(&matches, cli.command.base_mut()); cli.command.base_mut().profile_explicit = has_explicit_profile_arg(&argv); apply_base_output_defaults(&mut cli.command); @@ -441,6 +444,13 @@ fn try_main() -> Result<()> { command_result } +fn apply_trace_arg_sources(matches: &ArgMatches, base: &mut crate::args::BaseArgs) { + base.trace_org_explicit = + find_value_source(matches, "org_name") == Some(ValueSource::CommandLine); + base.trace_project_explicit = + find_value_source(matches, "project") == Some(ValueSource::CommandLine); +} + fn apply_base_arg_sources(matches: &ArgMatches, base: &mut LoginBaseArgs) { base.verbose_source = find_value_source(matches, "verbose").and_then(map_value_source); base.quiet_source = find_value_source(matches, "quiet").and_then(map_value_source); @@ -701,6 +711,39 @@ mod tests { assert!(cli.command.base().verbose_explicit()); } + #[test] + fn trace_route_sources_distinguish_flags_from_environment() { + let _guard = env_test_lock().lock().expect("env test lock"); + let old_project = env::var_os("BRAINTRUST_DEFAULT_PROJECT"); + let old_org = env::var_os("BRAINTRUST_ORG_NAME"); + env::set_var("BRAINTRUST_DEFAULT_PROJECT", "default-project"); + env::set_var("BRAINTRUST_ORG_NAME", "default-org"); + for (argv, expected_project, expected_org) in [ + (vec!["bt", "trace", "enable", "pi"], false, false), + ( + vec!["bt", "trace", "enable", "pi", "--project", "chosen"], + true, + false, + ), + ( + vec!["bt", "trace", "enable", "pi", "--org", "chosen"], + false, + true, + ), + ] { + let matches = Cli::command().try_get_matches_from(argv).unwrap(); + let mut cli = Cli::from_arg_matches(&matches).unwrap(); + let Commands::Trace(cmd) = &mut cli.command else { + panic!("expected trace") + }; + apply_trace_arg_sources(&matches, &mut cmd.base); + assert_eq!(cmd.base.trace_project_explicit, expected_project); + assert_eq!(cmd.base.trace_org_explicit, expected_org); + } + restore_env_var("BRAINTRUST_DEFAULT_PROJECT", old_project); + restore_env_var("BRAINTRUST_ORG_NAME", old_org); + } + #[test] fn login_rejects_context_selection_flags() { for args in [ diff --git a/src/setup/mod.rs b/src/setup/mod.rs index 00cc5ff8..74b76b01 100644 --- a/src/setup/mod.rs +++ b/src/setup/mod.rs @@ -5289,6 +5289,7 @@ mod tests { }, org_name: Some("test org".to_string()), project: Some("test project".to_string()), + ..Default::default() }; assert_eq!( diff --git a/src/trace_host.rs b/src/trace_host.rs index 90e838d3..501851d3 100644 --- a/src/trace_host.rs +++ b/src/trace_host.rs @@ -309,6 +309,12 @@ async fn resolve_trace_project(mut base: BaseArgs) -> anyhow::Result { #[async_trait] impl TraceHostServices for BtTraceHost { + fn has_explicit_route_selection(&self) -> bool { + self.base.profile_explicit + || self.base.trace_org_explicit + || self.base.trace_project_explicit + } + async fn resolve_route( &self, requirements: RouteRequirements, @@ -580,6 +586,28 @@ mod tests { use std::{env, ffi::OsString, fs}; use tempfile::TempDir; + #[test] + fn explicit_trace_route_requires_command_line_selection() { + let mut base = BaseArgs { + login: LoginBaseArgs { + profile: Some("environment-profile".into()), + ..Default::default() + }, + project: Some("environment-default".into()), + org_name: Some("environment-org".into()), + ..Default::default() + }; + assert!(!BtTraceHost::new(base.clone()).has_explicit_route_selection()); + base.profile_explicit = true; + assert!(BtTraceHost::new(base.clone()).has_explicit_route_selection()); + base.profile_explicit = false; + base.trace_org_explicit = true; + assert!(BtTraceHost::new(base.clone()).has_explicit_route_selection()); + base.trace_org_explicit = false; + base.trace_project_explicit = true; + assert!(BtTraceHost::new(base).has_explicit_route_selection()); + } + struct TestOAuthProfile { name: &'static str, id: &'static str, From dff173a6a54280d8d8f009bd54151153479c104b Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 19 Nov 2026 02:57:16 +0800 Subject: [PATCH 2/7] Pass explicit project tracing selections to daemon --- src/trace_host.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/trace_host.rs b/src/trace_host.rs index 501851d3..13275635 100644 --- a/src/trace_host.rs +++ b/src/trace_host.rs @@ -315,6 +315,30 @@ impl TraceHostServices for BtTraceHost { || self.base.trace_project_explicit } + fn explicit_route_selection(&self) -> HostRouteSelection { + HostRouteSelection { + auth: AuthSelection { + source: AuthSource::Auto, + profile_id: None, + profile: self + .base + .profile_explicit + .then(|| self.base.profile.clone()) + .flatten(), + org_name: self + .base + .trace_org_explicit + .then(|| self.base.org_name.clone()) + .flatten(), + }, + project_name: self + .base + .trace_project_explicit + .then(|| self.base.project.clone()) + .flatten(), + } + } + async fn resolve_route( &self, requirements: RouteRequirements, @@ -597,9 +621,19 @@ mod tests { org_name: Some("environment-org".into()), ..Default::default() }; - assert!(!BtTraceHost::new(base.clone()).has_explicit_route_selection()); + let implicit = BtTraceHost::new(base.clone()); + assert!(!implicit.has_explicit_route_selection()); + let selection = implicit.explicit_route_selection(); + assert!(selection.auth.profile.is_none()); + assert!(selection.auth.org_name.is_none()); + assert!(selection.project_name.is_none()); base.profile_explicit = true; - assert!(BtTraceHost::new(base.clone()).has_explicit_route_selection()); + let explicit = BtTraceHost::new(base.clone()); + assert!(explicit.has_explicit_route_selection()); + assert_eq!( + explicit.explicit_route_selection().auth.profile.as_deref(), + Some("environment-profile") + ); base.profile_explicit = false; base.trace_org_explicit = true; assert!(BtTraceHost::new(base.clone()).has_explicit_route_selection()); From 81ccdffc91a1bc833d081b15fb0453c86d85009c Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 19 Nov 2026 03:05:37 +0800 Subject: [PATCH 3/7] Update setup command output expectation --- tests/cli.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cli.rs b/tests/cli.rs index e6fc866e..05446594 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1754,7 +1754,8 @@ fn trace_setup_honors_global_json() { .clone(); let output: serde_json::Value = serde_json::from_slice(&stdout).expect("trace setup emits JSON"); - assert_eq!(output["command"], "enable"); + assert_eq!(output["command"], "setup"); + assert_eq!(output["scope"], "user"); assert_eq!(output["source"], "opencode"); assert_eq!(output["display_name"], "OpenCode"); assert_eq!(output["restart_required"], true); From e70977e65886f45038e2e4268ee51fb2cad5daa6 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 19 Nov 2026 20:46:16 +0800 Subject: [PATCH 4/7] Delegate trace argument interpretation to daemon --- src/args.rs | 9 ++---- src/experiments/mod.rs | 1 - src/main.rs | 50 ++++------------------------------ src/setup/mod.rs | 1 - src/trace_host.rs | 62 ------------------------------------------ tests/cli.rs | 43 +++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 115 deletions(-) diff --git a/src/args.rs b/src/args.rs index 0d4219dc..718d95bf 100644 --- a/src/args.rs +++ b/src/args.rs @@ -120,9 +120,6 @@ pub struct BaseArgs { #[arg(short = 'o', long = "org", env = "BRAINTRUST_ORG_NAME", global = true)] pub org_name: Option, - #[arg(skip)] - pub trace_org_explicit: bool, - /// Override active project #[arg( short = 'p', @@ -132,9 +129,6 @@ pub struct BaseArgs { global = true )] pub project: Option, - - #[arg(skip)] - pub trace_project_explicit: bool, } #[derive(Debug, Clone, Args)] @@ -195,7 +189,8 @@ impl From for BaseArgs { fn from(login: LoginBaseArgs) -> Self { Self { login, - ..Default::default() + org_name: None, + project: None, } } } diff --git a/src/experiments/mod.rs b/src/experiments/mod.rs index 2aec3224..e45f17e8 100644 --- a/src/experiments/mod.rs +++ b/src/experiments/mod.rs @@ -416,7 +416,6 @@ mod tests { }, org_name: Some("explicit-org".to_string()), project: Some("explicit-project".to_string()), - ..Default::default() }; let parsed = parse_experiment_compare_url( "https://www.example.test/app/url-org/p/url-project/experiments/baseline?c=challenger", diff --git a/src/main.rs b/src/main.rs index 3faa8151..78241cfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -379,9 +379,6 @@ fn try_main() -> Result<()> { cmd.args.cli_project = cmd.base.project.clone(); } } - if let Commands::Trace(cmd) = &mut cli.command { - apply_trace_arg_sources(&matches, &mut cmd.base); - } apply_base_arg_sources(&matches, cli.command.base_mut()); cli.command.base_mut().profile_explicit = has_explicit_profile_arg(&argv); apply_base_output_defaults(&mut cli.command); @@ -435,7 +432,12 @@ fn try_main() -> Result<()> { Commands::Switch(cmd) => switch::run(cmd.base, cmd.args).await?, Commands::Status(cmd) => status::run(cmd.base, cmd.args).await?, Commands::Trace(cmd) => { - bt_daemon::run_trace(cmd.args, trace_host::context(cmd.base)).await? + bt_daemon::run_trace_with_matches( + cmd.args, + trace_host::context(cmd.base), + Some(&matches), + ) + .await? } } Ok(()) @@ -444,13 +446,6 @@ fn try_main() -> Result<()> { command_result } -fn apply_trace_arg_sources(matches: &ArgMatches, base: &mut crate::args::BaseArgs) { - base.trace_org_explicit = - find_value_source(matches, "org_name") == Some(ValueSource::CommandLine); - base.trace_project_explicit = - find_value_source(matches, "project") == Some(ValueSource::CommandLine); -} - fn apply_base_arg_sources(matches: &ArgMatches, base: &mut LoginBaseArgs) { base.verbose_source = find_value_source(matches, "verbose").and_then(map_value_source); base.quiet_source = find_value_source(matches, "quiet").and_then(map_value_source); @@ -711,39 +706,6 @@ mod tests { assert!(cli.command.base().verbose_explicit()); } - #[test] - fn trace_route_sources_distinguish_flags_from_environment() { - let _guard = env_test_lock().lock().expect("env test lock"); - let old_project = env::var_os("BRAINTRUST_DEFAULT_PROJECT"); - let old_org = env::var_os("BRAINTRUST_ORG_NAME"); - env::set_var("BRAINTRUST_DEFAULT_PROJECT", "default-project"); - env::set_var("BRAINTRUST_ORG_NAME", "default-org"); - for (argv, expected_project, expected_org) in [ - (vec!["bt", "trace", "enable", "pi"], false, false), - ( - vec!["bt", "trace", "enable", "pi", "--project", "chosen"], - true, - false, - ), - ( - vec!["bt", "trace", "enable", "pi", "--org", "chosen"], - false, - true, - ), - ] { - let matches = Cli::command().try_get_matches_from(argv).unwrap(); - let mut cli = Cli::from_arg_matches(&matches).unwrap(); - let Commands::Trace(cmd) = &mut cli.command else { - panic!("expected trace") - }; - apply_trace_arg_sources(&matches, &mut cmd.base); - assert_eq!(cmd.base.trace_project_explicit, expected_project); - assert_eq!(cmd.base.trace_org_explicit, expected_org); - } - restore_env_var("BRAINTRUST_DEFAULT_PROJECT", old_project); - restore_env_var("BRAINTRUST_ORG_NAME", old_org); - } - #[test] fn login_rejects_context_selection_flags() { for args in [ diff --git a/src/setup/mod.rs b/src/setup/mod.rs index 74b76b01..00cc5ff8 100644 --- a/src/setup/mod.rs +++ b/src/setup/mod.rs @@ -5289,7 +5289,6 @@ mod tests { }, org_name: Some("test org".to_string()), project: Some("test project".to_string()), - ..Default::default() }; assert_eq!( diff --git a/src/trace_host.rs b/src/trace_host.rs index 13275635..90e838d3 100644 --- a/src/trace_host.rs +++ b/src/trace_host.rs @@ -309,36 +309,6 @@ async fn resolve_trace_project(mut base: BaseArgs) -> anyhow::Result { #[async_trait] impl TraceHostServices for BtTraceHost { - fn has_explicit_route_selection(&self) -> bool { - self.base.profile_explicit - || self.base.trace_org_explicit - || self.base.trace_project_explicit - } - - fn explicit_route_selection(&self) -> HostRouteSelection { - HostRouteSelection { - auth: AuthSelection { - source: AuthSource::Auto, - profile_id: None, - profile: self - .base - .profile_explicit - .then(|| self.base.profile.clone()) - .flatten(), - org_name: self - .base - .trace_org_explicit - .then(|| self.base.org_name.clone()) - .flatten(), - }, - project_name: self - .base - .trace_project_explicit - .then(|| self.base.project.clone()) - .flatten(), - } - } - async fn resolve_route( &self, requirements: RouteRequirements, @@ -610,38 +580,6 @@ mod tests { use std::{env, ffi::OsString, fs}; use tempfile::TempDir; - #[test] - fn explicit_trace_route_requires_command_line_selection() { - let mut base = BaseArgs { - login: LoginBaseArgs { - profile: Some("environment-profile".into()), - ..Default::default() - }, - project: Some("environment-default".into()), - org_name: Some("environment-org".into()), - ..Default::default() - }; - let implicit = BtTraceHost::new(base.clone()); - assert!(!implicit.has_explicit_route_selection()); - let selection = implicit.explicit_route_selection(); - assert!(selection.auth.profile.is_none()); - assert!(selection.auth.org_name.is_none()); - assert!(selection.project_name.is_none()); - base.profile_explicit = true; - let explicit = BtTraceHost::new(base.clone()); - assert!(explicit.has_explicit_route_selection()); - assert_eq!( - explicit.explicit_route_selection().auth.profile.as_deref(), - Some("environment-profile") - ); - base.profile_explicit = false; - base.trace_org_explicit = true; - assert!(BtTraceHost::new(base.clone()).has_explicit_route_selection()); - base.trace_org_explicit = false; - base.trace_project_explicit = true; - assert!(BtTraceHost::new(base).has_explicit_route_selection()); - } - struct TestOAuthProfile { name: &'static str, id: &'static str, diff --git a/tests/cli.rs b/tests/cli.rs index 05446594..5c2ca884 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1769,6 +1769,49 @@ fn trace_setup_honors_global_json() { ); } +#[test] +fn project_setup_uses_only_explicit_global_selections() { + let repo = tempfile::tempdir().unwrap(); + let home = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + fs::create_dir(repo.path().join(".git")).unwrap(); + let path = repo.path().join(".claude/braintrust.json"); + let run = |args: &[&str]| { + bt_trace_command(config.path(), "environment-profile", "environment-org") + .env("HOME", home.path()) + .env("BRAINTRUST_DEFAULT_PROJECT", "environment-project") + .current_dir(repo.path()) + .args(args) + .assert() + .success(); + serde_json::from_slice::(&fs::read(&path).unwrap()).unwrap() + }; + assert_eq!( + run(&["trace", "setup", "claude", "--scope", "project"]), + serde_json::json!({"trace_to_braintrust":true}) + ); + assert_eq!( + run(&[ + "trace", + "enable", + "claude", + "--scope", + "project", + "--profile", + "chosen-profile", + "--org", + "chosen-org", + "--project", + "chosen-project" + ]), + serde_json::json!({"trace_to_braintrust":true,"profile":"chosen-profile","org_name":"chosen-org","project":"chosen-project"}) + ); + assert_eq!( + run(&["trace", "disable", "claude", "--scope", "project"])["project"], + "chosen-project" + ); +} + #[cfg(unix)] #[test] fn trace_doctor_reports_saved_profile_provenance_without_credentials() { From 439a63f39f6330716a659fbd60a1ea898daebbd4 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 19 Nov 2026 21:54:41 +0800 Subject: [PATCH 5/7] Exercise scoped tracing installations --- tests/cli.rs | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 1 deletion(-) diff --git a/tests/cli.rs b/tests/cli.rs index 5c2ca884..87166224 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1277,6 +1277,85 @@ fn trace_run_uses_the_invocation_project_without_changing_setup() { ); } +#[cfg(unix)] +#[test] +fn trace_run_inherits_user_and_project_settings_without_mutating_them() { + let home = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + let bin = tempfile::tempdir().unwrap(); + let repo = tempfile::tempdir().unwrap(); + let state = tempfile::tempdir().unwrap(); + fs::create_dir(repo.path().join(".git")).unwrap(); + fs::create_dir(repo.path().join(".codex")).unwrap(); + let user = state.path().join("user.json"); + let project = repo.path().join(".codex/braintrust.json"); + let run = state.path().join("run.json"); + let user_text = r#"{"trace_to_braintrust":false,"route":{"auth":{},"destination":{"type":"project_logs","project_name":"user"},"tags":["user"],"additional_metadata":{"nested":{"a":1,"b":2},"list":[1,2]}}}"#; + let project_text = r#"{"trace_to_braintrust":false,"project":"project","additional_metadata":{"nested":{"b":3},"list":[4]}}"#; + fs::write(&user, user_text).unwrap(); + fs::write(&project, project_text).unwrap(); + write_run_agent(&bin.path().join("codex")); + bt_trace_environment_command(config.path()) + .current_dir(repo.path()) + .env("HOME", home.path()) + .env("PATH", bin.path()) + .env("AGENT_RUN_LOG", state.path().join("run.log")) + .env("AGENT_RUN_SETTINGS", &run) + .env("AGENT_RUN_DAEMON_ENV", state.path().join("daemon.txt")) + .env("BRAINTRUST_DAEMON_CONFIG", &user) + .args([ + "trace", + "run", + "codex", + "--inherit", + "--additional-metadata", + r#"{"nested":{"c":5}}"#, + "--", + "--version", + ]) + .assert() + .success(); + let result: serde_json::Value = serde_json::from_slice(&fs::read(&run).unwrap()).unwrap(); + assert_eq!(result["trace_to_braintrust"], true); + assert_eq!(result["route"]["auth"]["source"], "environment"); + assert_eq!(result["route"]["destination"]["project_name"], "project"); + assert_eq!(result["route"]["tags"], serde_json::json!(["user"])); + assert_eq!( + result["route"]["additional_metadata"], + serde_json::json!({"nested":{"a":1,"b":3,"c":5},"list":[4]}) + ); + bt_trace_environment_command(config.path()) + .current_dir(repo.path()) + .env("HOME", home.path()) + .env("PATH", bin.path()) + .env("AGENT_RUN_LOG", state.path().join("run.log")) + .env("AGENT_RUN_SETTINGS", &run) + .env("AGENT_RUN_DAEMON_ENV", state.path().join("daemon.txt")) + .env("BRAINTRUST_DAEMON_CONFIG", &user) + .args([ + "trace", + "run", + "codex", + "--inherit", + "--project", + "explicit", + "--tag", + "run", + "--", + "--version", + ]) + .assert() + .success(); + let overridden: serde_json::Value = serde_json::from_slice(&fs::read(&run).unwrap()).unwrap(); + assert_eq!( + overridden["route"]["destination"]["project_name"], + "explicit" + ); + assert_eq!(overridden["route"]["tags"], serde_json::json!(["run"])); + assert_eq!(fs::read_to_string(user).unwrap(), user_text); + assert_eq!(fs::read_to_string(project).unwrap(), project_text); +} + #[cfg(unix)] #[test] fn trace_enable_persists_environment_auth_and_completes_setup() { @@ -1769,6 +1848,72 @@ fn trace_setup_honors_global_json() { ); } +#[test] +fn trace_install_can_be_disabled_by_default() { + let home = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + let settings = config.path().join("tracing.json"); + bt_trace_command(config.path(), "test-profile", "test-org") + .env("HOME", home.path()) + .env("BRAINTRUST_DAEMON_CONFIG", &settings) + .args([ + "trace", + "install", + "opencode", + "--disable", + "--project", + "agent-traces", + "--no-input", + ]) + .assert() + .success() + .stdout(predicate::str::contains("Tracing is disabled by default")); + let settings: serde_json::Value = serde_json::from_slice(&fs::read(settings).unwrap()).unwrap(); + assert_eq!(settings["trace_to_braintrust"], false); + assert_eq!( + settings["route"]["destination"]["project_name"], + "agent-traces" + ); +} + +#[test] +fn trace_doctor_reports_effective_project_settings() { + let home = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + let repo = tempfile::tempdir().unwrap(); + let settings = config.path().join("tracing.json"); + fs::create_dir(repo.path().join(".git")).unwrap(); + fs::create_dir(repo.path().join(".claude")).unwrap(); + let project = repo.path().join(".claude/braintrust.json"); + fs::write(&settings, r#"{"trace_to_braintrust":false,"route":{"auth":{"source":"environment"},"destination":{"type":"project_logs","project_name":"global"}}}"#).unwrap(); + fs::write( + &project, + r#"{"trace_to_braintrust":true,"project":"local"}"#, + ) + .unwrap(); + let result = bt_trace_environment_command(config.path()) + .env("HOME", home.path()) + .env("BRAINTRUST_DAEMON_CONFIG", &settings) + .current_dir(repo.path()) + .args(["trace", "doctor", "claude", "--json"]) + .assert() + .success() + .get_output() + .stdout + .clone(); + let output: serde_json::Value = serde_json::from_slice(&result).unwrap(); + assert_eq!(output["enabled"], true); + assert_eq!( + output["project_settings_path"], + project.canonicalize().unwrap().to_string_lossy().as_ref() + ); + assert_eq!(output["route"]["destination"]["project_name"], "local"); + assert!(output["project_settings_fields"] + .as_array() + .unwrap() + .contains(&serde_json::json!("trace_to_braintrust"))); +} + #[test] fn project_setup_uses_only_explicit_global_selections() { let repo = tempfile::tempdir().unwrap(); @@ -1787,9 +1932,23 @@ fn project_setup_uses_only_explicit_global_selections() { serde_json::from_slice::(&fs::read(&path).unwrap()).unwrap() }; assert_eq!( - run(&["trace", "setup", "claude", "--scope", "project"]), + run(&["trace", "install", "claude", "--scope", "project"]), serde_json::json!({"trace_to_braintrust":true}) ); + let stdout = bt_trace_command(config.path(), "environment-profile", "environment-org") + .env("HOME", home.path()) + .current_dir(repo.path()) + .args(["trace", "install", "claude", "--scope", "project", "--json"]) + .assert() + .success() + .get_output() + .stdout + .clone(); + let output: serde_json::Value = serde_json::from_slice(&stdout).unwrap(); + assert!(output["warnings"][0] + .as_str() + .unwrap() + .contains("Each user")); assert_eq!( run(&[ "trace", From 6ad9876cf3c06fbd7ba1367bcf1d7a95ac1137e7 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Thu, 19 Nov 2026 22:40:37 +0800 Subject: [PATCH 6/7] Test shared repository tracing settings --- tests/cli.rs | 151 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 139 insertions(+), 12 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index 87166224..4b3ed67b 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1286,19 +1286,19 @@ fn trace_run_inherits_user_and_project_settings_without_mutating_them() { let repo = tempfile::tempdir().unwrap(); let state = tempfile::tempdir().unwrap(); fs::create_dir(repo.path().join(".git")).unwrap(); - fs::create_dir(repo.path().join(".codex")).unwrap(); let user = state.path().join("user.json"); - let project = repo.path().join(".codex/braintrust.json"); + let project = repo.path().join(".braintrust.json"); let run = state.path().join("run.json"); let user_text = r#"{"trace_to_braintrust":false,"route":{"auth":{},"destination":{"type":"project_logs","project_name":"user"},"tags":["user"],"additional_metadata":{"nested":{"a":1,"b":2},"list":[1,2]}}}"#; let project_text = r#"{"trace_to_braintrust":false,"project":"project","additional_metadata":{"nested":{"b":3},"list":[4]}}"#; fs::write(&user, user_text).unwrap(); fs::write(&project, project_text).unwrap(); - write_run_agent(&bin.path().join("codex")); + write_run_agent(&bin.path().join("pi")); bt_trace_environment_command(config.path()) .current_dir(repo.path()) .env("HOME", home.path()) .env("PATH", bin.path()) + .env("PI_BIN", bin.path().join("pi")) .env("AGENT_RUN_LOG", state.path().join("run.log")) .env("AGENT_RUN_SETTINGS", &run) .env("AGENT_RUN_DAEMON_ENV", state.path().join("daemon.txt")) @@ -1306,10 +1306,10 @@ fn trace_run_inherits_user_and_project_settings_without_mutating_them() { .args([ "trace", "run", - "codex", + "pi", "--inherit", "--additional-metadata", - r#"{"nested":{"c":5}}"#, + r#"{"nested":{"c":5},"list":[5]}"#, "--", "--version", ]) @@ -1322,12 +1322,13 @@ fn trace_run_inherits_user_and_project_settings_without_mutating_them() { assert_eq!(result["route"]["tags"], serde_json::json!(["user"])); assert_eq!( result["route"]["additional_metadata"], - serde_json::json!({"nested":{"a":1,"b":3,"c":5},"list":[4]}) + serde_json::json!({"nested":{"a":1,"b":3,"c":5},"list":[1,2,4,5]}) ); bt_trace_environment_command(config.path()) .current_dir(repo.path()) .env("HOME", home.path()) .env("PATH", bin.path()) + .env("PI_BIN", bin.path().join("pi")) .env("AGENT_RUN_LOG", state.path().join("run.log")) .env("AGENT_RUN_SETTINGS", &run) .env("AGENT_RUN_DAEMON_ENV", state.path().join("daemon.txt")) @@ -1335,7 +1336,7 @@ fn trace_run_inherits_user_and_project_settings_without_mutating_them() { .args([ "trace", "run", - "codex", + "pi", "--inherit", "--project", "explicit", @@ -1351,11 +1352,78 @@ fn trace_run_inherits_user_and_project_settings_without_mutating_them() { overridden["route"]["destination"]["project_name"], "explicit" ); - assert_eq!(overridden["route"]["tags"], serde_json::json!(["run"])); + assert_eq!( + overridden["route"]["tags"], + serde_json::json!(["run", "user"]) + ); assert_eq!(fs::read_to_string(user).unwrap(), user_text); assert_eq!(fs::read_to_string(project).unwrap(), project_text); } +#[cfg(unix)] +#[test] +fn trace_run_inherits_project_plugins_or_replaces_them() { + let home = tempfile::tempdir().unwrap(); + let config = tempfile::tempdir().unwrap(); + let bin = tempfile::tempdir().unwrap(); + let repo = tempfile::tempdir().unwrap(); + let state = tempfile::tempdir().unwrap(); + fs::create_dir(repo.path().join(".git")).unwrap(); + fs::create_dir(repo.path().join("plugins")).unwrap(); + let user_plugin = state.path().join("user.mjs"); + let project_plugin = repo.path().join("plugins/project.mjs"); + let invocation_plugin = state.path().join("run.mjs"); + for plugin in [&user_plugin, &project_plugin, &invocation_plugin] { + fs::write(plugin, "export default span => span").unwrap(); + } + let user = state.path().join("user.json"); + let project = repo.path().join(".braintrust.json"); + fs::write(&user, serde_json::json!({"trace_to_braintrust":false,"route":{"auth":{"source":"environment"},"destination":{"type":"project_logs","project_name":"user"},"span_plugins":[user_plugin.canonicalize().unwrap()]}}).to_string()).unwrap(); + fs::write( + &project, + r#"{"trace_to_braintrust":true,"route":{"span_plugins":["plugins/project.mjs"]}}"#, + ) + .unwrap(); + write_run_agent(&bin.path().join("pi")); + let run = state.path().join("run.json"); + let execute = |replace: bool| { + let mut command = bt_trace_environment_command(config.path()); + command + .current_dir(repo.path()) + .env("HOME", home.path()) + .env("PATH", bin.path()) + .env("PI_BIN", bin.path().join("pi")) + .env("AGENT_RUN_LOG", state.path().join("run.log")) + .env("AGENT_RUN_SETTINGS", &run) + .env("AGENT_RUN_DAEMON_ENV", state.path().join("daemon.txt")) + .env("BRAINTRUST_DAEMON_CONFIG", &user) + .args(["trace", "run", "pi", "--inherit"]); + if replace { + command.arg("--no-inherit-plugins"); + } + command + .arg("--plugin") + .arg(&invocation_plugin) + .args(["--", "--version"]) + .assert() + .success(); + let result: serde_json::Value = serde_json::from_slice(&fs::read(&run).unwrap()).unwrap(); + result["route"]["span_plugins"].clone() + }; + assert_eq!( + execute(false), + serde_json::json!([ + user_plugin.canonicalize().unwrap(), + project_plugin.canonicalize().unwrap(), + invocation_plugin.canonicalize().unwrap() + ]) + ); + assert_eq!( + execute(true), + serde_json::json!([invocation_plugin.canonicalize().unwrap()]) + ); +} + #[cfg(unix)] #[test] fn trace_enable_persists_environment_auth_and_completes_setup() { @@ -1833,7 +1901,7 @@ fn trace_setup_honors_global_json() { .clone(); let output: serde_json::Value = serde_json::from_slice(&stdout).expect("trace setup emits JSON"); - assert_eq!(output["command"], "setup"); + assert_eq!(output["command"], "install"); assert_eq!(output["scope"], "user"); assert_eq!(output["source"], "opencode"); assert_eq!(output["display_name"], "OpenCode"); @@ -1884,7 +1952,7 @@ fn trace_doctor_reports_effective_project_settings() { let settings = config.path().join("tracing.json"); fs::create_dir(repo.path().join(".git")).unwrap(); fs::create_dir(repo.path().join(".claude")).unwrap(); - let project = repo.path().join(".claude/braintrust.json"); + let project = repo.path().join(".braintrust.json"); fs::write(&settings, r#"{"trace_to_braintrust":false,"route":{"auth":{"source":"environment"},"destination":{"type":"project_logs","project_name":"global"}}}"#).unwrap(); fs::write( &project, @@ -1920,12 +1988,13 @@ fn project_setup_uses_only_explicit_global_selections() { let home = tempfile::tempdir().unwrap(); let config = tempfile::tempdir().unwrap(); fs::create_dir(repo.path().join(".git")).unwrap(); - let path = repo.path().join(".claude/braintrust.json"); + fs::create_dir(repo.path().join("nested")).unwrap(); + let path = repo.path().join(".braintrust.json"); let run = |args: &[&str]| { bt_trace_command(config.path(), "environment-profile", "environment-org") .env("HOME", home.path()) .env("BRAINTRUST_DEFAULT_PROJECT", "environment-project") - .current_dir(repo.path()) + .current_dir(repo.path().join("nested")) .args(args) .assert() .success(); @@ -1935,6 +2004,7 @@ fn project_setup_uses_only_explicit_global_selections() { run(&["trace", "install", "claude", "--scope", "project"]), serde_json::json!({"trace_to_braintrust":true}) ); + assert!(!repo.path().join(".claude/braintrust.json").exists()); let stdout = bt_trace_command(config.path(), "environment-profile", "environment-org") .env("HOME", home.path()) .current_dir(repo.path()) @@ -1949,6 +2019,7 @@ fn project_setup_uses_only_explicit_global_selections() { .as_str() .unwrap() .contains("Each user")); + assert_eq!(output["applies_to_all_agents"], true); assert_eq!( run(&[ "trace", @@ -1969,6 +2040,62 @@ fn project_setup_uses_only_explicit_global_selections() { run(&["trace", "disable", "claude", "--scope", "project"])["project"], "chosen-project" ); + fs::create_dir(repo.path().join("plugins")).unwrap(); + fs::write( + repo.path().join("plugins/redact.mjs"), + "export default span => span", + ) + .unwrap(); + let configured = run(&[ + "trace", + "install", + "claude", + "--scope", + "project", + "--plugin", + "plugins/redact.mjs", + "--no-inherit-plugins", + ]); + assert_eq!(configured["inherit_plugins"], false); + assert_eq!( + configured["route"]["span_plugins"], + serde_json::json!(["plugins/redact.mjs"]) + ); + let restored = run(&[ + "trace", + "enable", + "claude", + "--scope", + "project", + "--inherit-plugins", + ]); + assert_eq!(restored["inherit_plugins"], true); + assert_eq!( + restored["route"]["span_plugins"], + serde_json::json!(["plugins/redact.mjs"]) + ); + let user = config.path().join("user-settings.json"); + fs::write(&user, r#"{"trace_to_braintrust":true}"#).unwrap(); + let mut project: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + project["unrelated"] = serde_json::json!({"preserve": false}); + fs::write(&path, project.to_string()).unwrap(); + bt_trace_command(config.path(), "environment-profile", "environment-org") + .env("HOME", home.path()) + .env("BRAINTRUST_DAEMON_CONFIG", &user) + .current_dir(repo.path()) + .args(["trace", "uninstall", "claude", "--scope", "project"]) + .assert() + .success() + .stdout(predicate::str::contains("all agents")); + assert!(!path.exists()); + assert!(!repo.path().join(".braintrust.jsonlock").exists()); + assert!(user.exists()); + bt_trace_command(config.path(), "environment-profile", "environment-org") + .env("HOME", home.path()) + .current_dir(repo.path()) + .args(["trace", "uninstall", "cursor", "--scope", "project"]) + .assert() + .success(); } #[cfg(unix)] From 819e2167d14b8db924f98c995c8fd216fd4ad517 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Sat, 21 Nov 2026 02:08:34 +0800 Subject: [PATCH 7/7] Update tracing daemon to current main --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14163c65..5e73a6e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "bt-daemon" version = "0.1.0" -source = "git+https://github.com/braintrustdata/braintrust-coding-agent-plugins?rev=df963568a743509cbdeb63febe40ad88ed2e757a#df963568a743509cbdeb63febe40ad88ed2e757a" +source = "git+https://github.com/braintrustdata/braintrust-coding-agent-plugins?rev=d1a863ad05099d1a45b39c187050c1c7ab8e0c75#d1a863ad05099d1a45b39c187050c1c7ab8e0c75" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index b32008a3..6c2fedbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ eula = false actix-web = "4.11.0" anyhow = "1.0.89" braintrust-sdk-rust = { git = "https://github.com/braintrustdata/braintrust-sdk-rust", rev = "3f34503e2be50270f92e96aa2d4b872ccd789936" } -bt-daemon = { git = "https://github.com/braintrustdata/braintrust-coding-agent-plugins", rev = "ff09caa4725747702d3335c5485b81e283b8849f" } +bt-daemon = { git = "https://github.com/braintrustdata/braintrust-coding-agent-plugins", rev = "d1a863ad05099d1a45b39c187050c1c7ab8e0c75" } async-trait = "0.1" clap = { version = "4.5.20", features = ["derive", "env", "string"] } crossterm = "0.28.1"