Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
30 changes: 28 additions & 2 deletions src/bl/skills_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -219,6 +227,24 @@ impl SkillsConfig {
}
}

/// Resolve an optional distribution fallback without changing user preferences.
pub fn resolve_default_org(stored: Option<String>, local_dev: bool) -> Result<Option<String>> {
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 <org>`"
)
})
})
.transpose()
}

pub fn resolve_skills_profile_context(
options: SkillsProfileResolveOptions,
) -> Result<SkillsProfileContext> {
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 =
Expand Down
159 changes: 159 additions & 0 deletions tests/bl_distribution_defaults.rs
Original file line number Diff line number Diff line change
@@ -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");
}