Skip to content
14 changes: 11 additions & 3 deletions cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct StartupContext {
struct AppRuntime {
logger: services::observability::Logger,
telemetry: RuntimeTelemetry,
telemetry_enabled: bool,
fs: services::capabilities::StdFsOps,
git: services::capabilities::ProcessGitOps,
registry: services::command_registry::CommandRegistry,
Expand Down Expand Up @@ -297,7 +298,8 @@ where
);
}
};
runtime.telemetry = RuntimeTelemetry::resolve(span_kind, &runtime.logger).await;
runtime.telemetry =
RuntimeTelemetry::resolve(span_kind, runtime.telemetry_enabled, &runtime.logger).await;
let rendered = run_command_lifecycle(args, &runtime, stdout, stderr).await;
runtime.telemetry.shutdown().await;
app_support::exit_code_for(&rendered)
Expand Down Expand Up @@ -363,6 +365,10 @@ fn initialize_runtime(startup: StartupContext) -> Result<AppRuntime, CliError> {
Ok(AppRuntime {
logger,
telemetry: RuntimeTelemetry::Noop(NoopTelemetry),
telemetry_enabled: startup
.observability_config
.access_policy
.telemetry_enabled(),
fs: services::capabilities::StdFsOps,
git: services::capabilities::ProcessGitOps,
registry: services::command_registry::build_default_registry(),
Expand Down Expand Up @@ -980,8 +986,9 @@ pub(crate) mod tests {
let runtime = build_runtime().unwrap();
let result = runtime.block_on(services::auth_command::telemetry_headers_with(
Some(repo.path().to_path_buf()),
false,
true,
false,
|| true,
|_: &std::path::Path| Some("repository-a".to_string()),
|_: String| stalled_keychain_lookup(),
));
Expand Down Expand Up @@ -1142,7 +1149,8 @@ pub(crate) mod tests {
#[tokio::test]
async fn disallowed_managed_telemetry_resolves_to_noop_without_credential_lookup() {
let telemetry =
RuntimeTelemetry::resolve(None, &services::observability::traits::NoopLogger).await;
RuntimeTelemetry::resolve(None, true, &services::observability::traits::NoopLogger)
.await;
assert!(matches!(telemetry, RuntimeTelemetry::Noop(_)));
}
}
117 changes: 115 additions & 2 deletions cli/src/app/doctor_reconciliation_cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const AUTH_DB_TEST_SECRET: &str = "doctor-cli-test-secret";
const UNREADABLE_AUTH_DB: &[u8] = b"not an encrypted auth database";
const FAR_FUTURE_READINESS: &[u8] = br#"{"expires_at_unix_seconds":4102444800}"#;

const TELEMETRY_ENABLED_CONFIG: &str = r#"{"telemetry":{"enabled":true}}"#;
const TELEMETRY_OPT_IN_NOTE: &str = "Managed telemetry export is off because telemetry.enabled is not true; set telemetry.enabled: true in the global SCE config to resume it.";
const EXIT_SUCCESS: &str = "success";
const EXIT_FAILURE: &str = "failure";

Expand Down Expand Up @@ -142,6 +144,12 @@ impl Sandbox {
.expect("state JSON")
}

fn telemetry_enabled_config(&self) -> String {
let path = self.dir.path().join("telemetry-enabled.json");
std::fs::write(&path, TELEMETRY_ENABLED_CONFIG).expect("telemetry config");
path.to_string_lossy().into_owned()
}

fn run(&self, args: &[&str]) -> CliRun {
self.run_with_env(args, &[])
}
Expand Down Expand Up @@ -620,11 +628,15 @@ fn global_state_warnings(report: &Value) -> Vec<Value> {
async fn doctor_on_fresh_state_reports_unconfigured_telemetry_without_a_warning() {
let sandbox = Sandbox::new("https://example.invalid/org/repo-a.git");
let auth_dir = sandbox.state_root().join("sce");
let config = sandbox.telemetry_enabled_config();

let report = sandbox
.run_with_env(
&["doctor", "--format", "json"],
&[(AUTH_DB_ENCRYPTION_KEY_ENV, "")],
&[
(AUTH_DB_ENCRYPTION_KEY_ENV, ""),
("SCE_CONFIG_FILE", config.as_str()),
],
)
.json();

Expand All @@ -644,7 +656,11 @@ async fn doctor_fix_reports_unreadable_telemetry_credentials_without_auth_side_e
let readiness = auth_dir.join("telemetry-ready.json");
std::fs::write(&auth_db, UNREADABLE_AUTH_DB).expect("auth DB");
std::fs::write(&readiness, FAR_FUTURE_READINESS).expect("readiness");
let env = [(AUTH_DB_ENCRYPTION_KEY_ENV, AUTH_DB_TEST_SECRET)];
let config = sandbox.telemetry_enabled_config();
let env = [
(AUTH_DB_ENCRYPTION_KEY_ENV, AUTH_DB_TEST_SECRET),
("SCE_CONFIG_FILE", config.as_str()),
];

let fix = sandbox.run_with_env(&["doctor", "--fix", "--format", "json"], &env);
let report = fix.json();
Expand Down Expand Up @@ -690,3 +706,100 @@ async fn doctor_fix_reports_unreadable_telemetry_credentials_without_auth_side_e
assert!(!fix.stdout.contains(AUTH_DB_TEST_SECRET));
assert!(!fix.stderr.contains(AUTH_DB_TEST_SECRET));
}

#[tokio::test(flavor = "multi_thread")]
async fn doctor_reports_post_commit_auto_sync_disabled_when_auth_is_disabled() {
let sandbox = Sandbox::new("https://example.invalid/org/repo-a.git");
let config_dir = sandbox.repo.join(".sce");
std::fs::create_dir_all(&config_dir).expect("config dir");
let config = config_dir.join("config.json");

std::fs::write(&config, r#"{"agent_trace":{"auto_sync":true}}"#).expect("config");
let allowed = sandbox.run(&["doctor", "--format", "json"]).json();
assert_eq!(allowed["post_commit_auto_sync"]["enabled"], true);
assert_ne!(allowed["post_commit_auto_sync"]["state"], "disabled");

std::fs::write(
&config,
r#"{"agent_trace":{"auto_sync":true},"auth":{"mode":"disabled"}}"#,
)
.expect("config");
let report = sandbox.run(&["doctor", "--format", "json"]).json();
assert_eq!(
report["post_commit_auto_sync"],
serde_json::json!({
"state": "disabled",
"enabled": false,
"source": "config_file",
"config_source": "default_discovered_local",
})
);
let text = sandbox.run(&["doctor"]);
assert!(
text.stdout
.contains("[PASS] Post-commit Agent Trace auto-sync (disabled by config)\n"),
"{}",
text.stdout
);
}

#[tokio::test(flavor = "multi_thread")]
async fn doctor_with_telemetry_disabled_by_config_never_reads_the_auth_db() {
let sandbox = Sandbox::new("https://example.invalid/org/repo-a.git");
let auth_dir = sandbox.state_root().join("sce");
let env = [(AUTH_DB_ENCRYPTION_KEY_ENV, "")];

let fresh = sandbox.run_with_env(&["doctor"], &env);
assert!(
fresh
.stdout
.contains("[PASS] Managed telemetry (disabled by config)\n"),
"{}",
fresh.stdout
);
assert!(
!fresh.stdout.contains(TELEMETRY_OPT_IN_NOTE),
"{}",
fresh.stdout
);
assert!(!auth_dir.join("auth.db").exists());

std::fs::create_dir_all(&auth_dir).expect("auth dir");
let auth_db = auth_dir.join("auth.db");
let readiness = auth_dir.join("telemetry-ready.json");
std::fs::write(&auth_db, UNREADABLE_AUTH_DB).expect("auth DB");
std::fs::write(&readiness, FAR_FUTURE_READINESS).expect("readiness");

let report = sandbox
.run_with_env(&["doctor", "--format", "json"], &env)
.json();
assert_eq!(
report["managed_telemetry"],
serde_json::json!({
"state": "disabled_by_config",
"reason": "disabled_by_config",
"kind": null,
"severity": null,
"next_action": null,
})
);
let warnings = global_state_warnings(&report);
assert!(warnings.is_empty(), "{warnings:#?}");

let text = sandbox.run_with_env(&["doctor"], &env);
assert!(
text.stdout.contains(&format!(
"[PASS] Managed telemetry (disabled by config)\n {TELEMETRY_OPT_IN_NOTE}\n"
)),
"{}",
text.stdout
);
assert_eq!(
std::fs::read(&auth_db).expect("auth DB kept"),
UNREADABLE_AUTH_DB
);
assert_eq!(
std::fs::read(&readiness).expect("readiness kept"),
FAR_FUTURE_READINESS
);
}
Loading
Loading