diff --git a/README.md b/README.md index 977568f..61e0aac 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,7 @@ The most useful configuration options are: | Variable | Purpose | | --- | --- | | `BL_HOME` | Override the BuilderLab state directory. | +| `BL_DEFAULT_ORG` | Distribution-provided organization fallback when no user `org` preference exists; ignored by `--local-dev`. Does not write preferences or change the `agent-tools` command. | | `BL_SKILLS_HOME` | Override the installed skills directory. | | `BL_SKILLS_CONFIG` | Select an explicit skills configuration file. | | `BL_SKILLS_PROFILE` | Select a skills configuration profile. | diff --git a/src/bl/skills_config.rs b/src/bl/skills_config.rs index 963737d..38d8348 100644 --- a/src/bl/skills_config.rs +++ b/src/bl/skills_config.rs @@ -22,10 +22,13 @@ use serde::{Deserialize, Serialize}; use crate::kgoose::DEFAULT_KGOOSE_BASE_URL; use super::display::Style; -use super::org_routing::resolve_org_kgoose_base_url; +use super::org_routing::{normalize_org, resolve_org_kgoose_base_url}; use super::skills_models::SkillsPreferences; pub const KGOOSE_BASE_URL_ENV_VAR: &str = "KGOOSE_BASE_URL"; +/// Optional organization fallback supplied by a managed CLI distribution. +/// Stored user preferences always win; this never writes configuration. +pub const BL_DEFAULT_ORG_ENV_VAR: &str = "BL_DEFAULT_ORG"; pub const LOCAL_DEV_CONFIG_FILE_NAME: &str = "bl-local-dev-config.yaml"; pub const BL_SKILLS_HOME_ENV_VAR: &str = "BL_SKILLS_HOME"; pub const BL_SKILLS_PACKAGES_DIR_ENV_VAR: &str = "BL_SKILLS_PACKAGES_DIR"; @@ -152,7 +155,12 @@ impl SkillsConfig { ); let preferences_path = default_preferences_path(&bl_home); let preferences = read_preferences_file(&preferences_path)?; - let org = preferences.org.clone(); + // Config commands must remain available to repair a bad distribution default. + let org = if org_routing { + resolve_default_org(preferences.org.clone(), local_dev)? + } else { + preferences.org.clone() + }; let kgoose_base_url = if org_routing { resolve_org_kgoose_base_url( &raw_kgoose_base_url, @@ -219,6 +227,24 @@ impl SkillsConfig { } } +/// Resolve an optional distribution fallback without changing user preferences. +pub fn resolve_default_org(stored: Option, local_dev: bool) -> Result> { + if stored.is_some() || local_dev { + return Ok(stored); + } + read_optional_env(BL_DEFAULT_ORG_ENV_VAR)? + .map(|value| value.trim().to_string()) + .filter(|value| !value.is_empty()) + .map(|value| { + normalize_org(&value).with_context(|| { + format!( + "invalid {BL_DEFAULT_ORG_ENV_VAR}; unset it or run `bl config set org `" + ) + }) + }) + .transpose() +} + pub fn resolve_skills_profile_context( options: SkillsProfileResolveOptions, ) -> Result { diff --git a/src/lib.rs b/src/lib.rs index 167c831..2ee3756 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,8 +30,8 @@ use crate::bl::auth_storage::stored_session_credential_header_value_for_kgoose_b use crate::bl::org_routing::resolve_org_kgoose_base_url; use crate::bl::skills_config::{ default_bl_home, default_preferences_path, normalize_kgoose_service_path, read_optional_env, - read_preferences_file, resolve_skills_profile_context, SkillsProfileResolveOptions, - BL_HOME_ENV_VAR, DEFAULT_KGOOSE_SERVICE_PATH, + read_preferences_file, resolve_default_org, resolve_skills_profile_context, + SkillsProfileResolveOptions, BL_HOME_ENV_VAR, DEFAULT_KGOOSE_SERVICE_PATH, }; use crate::catalog::{load_extensions_catalog, write_extensions_catalog}; use crate::kgoose::{CallToolResponse, HttpKgooseClient, KgooseClient, KgooseConfig}; @@ -366,10 +366,8 @@ fn apply_builderlab_mode_if_needed(config: &mut KgooseConfig, builderlab_mode: b .map(std::path::PathBuf::from) .unwrap_or_else(default_bl_home); let preferences = read_preferences_file(&default_preferences_path(&bl_home))?; - let org = preferences - .org - .as_deref() - .ok_or_else(bl_org_required_error)?; + let org = resolve_default_org(preferences.org, false)?; + let org = org.as_deref().ok_or_else(bl_org_required_error)?; config.base_url = resolve_org_kgoose_base_url(&config.base_url, Some(org), false, &config.service_path)?; config.session_credential = diff --git a/tests/bl_distribution_defaults.rs b/tests/bl_distribution_defaults.rs new file mode 100644 index 0000000..d0ea0c6 --- /dev/null +++ b/tests/bl_distribution_defaults.rs @@ -0,0 +1,159 @@ +//! Managed defaults are a fallback, not a rewrite of user or development state. + +use std::process::{Command, Output}; + +use serde_json::Value; +use tempfile::TempDir; + +fn command(home: &TempDir) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_bl")); + command + .env_clear() + .env("HOME", home.path()) + .env("BL_HOME", home.path().join(".bl")) + .env("BL_AUTH_STORAGE", "file") + .env("KGOOSE_BASE_URL", "https://builderlab.xyz") + .args(["auth", "status", "--json"]); + command +} + +fn json(output: &Output) -> Value { + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + serde_json::from_slice(&output.stdout).expect("JSON status") +} + +#[test] +fn default_org_routes_without_creating_user_preferences() { + let home = TempDir::new().unwrap(); + let output = command(&home) + .env("BL_DEFAULT_ORG", " Demo ") + .output() + .unwrap(); + let status = json(&output); + assert_eq!(status["kgoose_base_url"], "https://demo.builderlab.xyz"); + assert_eq!(status["kgoose_service_path"], "/api/goose"); + assert_eq!(status["authenticated"], false); + assert!(!home.path().join(".bl/config.yaml").exists()); +} + +#[test] +fn stored_org_wins_even_over_an_invalid_distribution_default() { + let home = TempDir::new().unwrap(); + std::fs::create_dir(home.path().join(".bl")).unwrap(); + let preferences = home.path().join(".bl/config.yaml"); + let original = "org: chosen\nno_auto_updates: true\n"; + std::fs::write(&preferences, original).unwrap(); + let output = command(&home) + .env("BL_DEFAULT_ORG", "invalid org!") + .output() + .unwrap(); + assert_eq!( + json(&output)["kgoose_base_url"], + "https://chosen.builderlab.xyz" + ); + assert_eq!(std::fs::read_to_string(preferences).unwrap(), original); +} + +#[test] +fn no_distribution_default_preserves_missing_org_behavior() { + let home = TempDir::new().unwrap(); + let output = command(&home).output().unwrap(); + assert_eq!(output.status.code(), Some(3)); + assert!(String::from_utf8_lossy(&output.stderr).contains("org_required")); + assert!(!home.path().join(".bl/config.yaml").exists()); +} + +#[test] +fn invalid_distribution_default_fails_without_writing_preferences() { + let home = TempDir::new().unwrap(); + let output = command(&home) + .env("BL_DEFAULT_ORG", "invalid org!") + .output() + .unwrap(); + assert!(!output.status.success()); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("org must contain")); + assert!(stderr.contains("BL_DEFAULT_ORG")); + assert!(stderr.contains("bl config set org")); + assert!(!home.path().join(".bl/config.yaml").exists()); +} + +#[test] +fn blank_distribution_default_behaves_like_unset() { + for value in ["", " \t "] { + let home = TempDir::new().unwrap(); + let output = command(&home) + .env("BL_DEFAULT_ORG", value) + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(3)); + assert!(String::from_utf8_lossy(&output.stderr).contains("org_required")); + assert!(!home.path().join(".bl/config.yaml").exists()); + } +} + +#[test] +fn config_commands_can_recover_from_invalid_distribution_default() { + let home = TempDir::new().unwrap(); + let mut config = Command::new(env!("CARGO_BIN_EXE_bl")); + config + .env_clear() + .env("HOME", home.path()) + .env("BL_HOME", home.path().join(".bl")) + .env("BL_DEFAULT_ORG", "invalid org!"); + assert!(config + .args(["config", "path"]) + .output() + .unwrap() + .status + .success()); + + let mut set = Command::new(env!("CARGO_BIN_EXE_bl")); + let output = set + .env_clear() + .env("HOME", home.path()) + .env("BL_HOME", home.path().join(".bl")) + .env("BL_DEFAULT_ORG", "invalid org!") + .args(["config", "set", "org", "chosen"]) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!( + json( + &command(&home) + .env("BL_DEFAULT_ORG", "invalid org!") + .output() + .unwrap() + )["kgoose_base_url"], + "https://chosen.builderlab.xyz" + ); + assert!(std::fs::read_to_string(home.path().join(".bl/config.yaml")) + .unwrap() + .contains("org: chosen")); +} + +#[test] +fn local_development_ignores_the_distribution_default() { + let home = TempDir::new().unwrap(); + std::fs::write( + home.path().join("bl-local-dev-config.yaml"), + "current_profile: local\nprofiles:\n local: {}\n", + ) + .unwrap(); + let output = command(&home) + .current_dir(home.path()) + .env("KGOOSE_BASE_URL", "http://127.0.0.1:5173") + .env("BL_DEFAULT_ORG", "invalid org!") + .arg("--local-dev") + .output() + .unwrap(); + assert_eq!(json(&output)["kgoose_base_url"], "http://127.0.0.1:5173"); +}