fabro/lib/foundation/fabro-config/src/parse.rs
Bryan Helmkamp 2a2fc41807
Read catalog policy from lithos core fields and metadata.agent
Fabro's policy layer restated the lithos built-ins under `metadata.fabro`:
enabled flags, credentials, display facts, probe and small-default roles,
and agent profiles. lithos-llm now carries every one of those as a core
field or under the shared `metadata.agent` namespace, so the layer and its
typed view go:

- Delete `fabro-policy.toml` and `FABRO_POLICY_TOML`. The catalog is the
  lithos built-ins plus the operator's `[llm]` overlay, nothing between.
- Delete `fabro_types::catalog_policy`. `enabled`, `stands_in_for`,
  `api_key_url`, `family`, the cutoffs, `estimated_output_tps`,
  `small_default`, and `probe` are read from lithos accessors; the agent
  profile and `reasoning_by_default` come from `metadata.agent`, which
  Pebble reads too.
- `catalog::provider`, `enabled_providers`, and `listed_providers` return
  the lithos `CatalogProvider` directly; `ModelEntry` loses its policy
  field and gains `agent_profile()`.
- Test fixtures move `[providers.x.metadata.fabro] enabled = true` onto
  the provider table, drop `credentials` lists in favor of the secret name
  lithos derives from the provider id, and spell `agent_profile` as
  `metadata.agent.profile`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-09 23:20:12 -06:00

338 lines
11 KiB
Rust

use std::fmt;
use crate::SettingsLayer;
const CURRENT_VERSION: u32 = 1;
const ALLOWED_TOP_LEVEL_KEYS: &[&str] = &[
"_version",
"project",
"workflow",
"environments",
"run",
"cli",
"server",
"llm",
];
/// Legacy `[llm]` keys that pre-date the settings-driven catalog plan and
/// should still produce the migration hint for `[run.model]` rather than be
/// silently accepted by the new `[llm]` schema.
const LEGACY_LLM_KEYS: &[&str] = &[
"provider",
"model",
"temperature",
"max_tokens",
"fallbacks",
"fallback",
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseError {
Toml(String),
Version(VersionError),
UnknownTopLevelKey {
key: String,
hint: Option<String>,
},
ServerManagedEnvironmentCwd {
path: String,
source: SettingsSource,
},
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Toml(msg) => write!(f, "settings file is not valid TOML: {msg}"),
Self::Version(err) => fmt::Display::fmt(err, f),
Self::UnknownTopLevelKey { key, hint } => {
if let Some(hint) = hint {
write!(f, "unknown top-level settings key `{key}`: {hint}")
} else {
write!(
f,
"unknown top-level settings key `{key}`: expected one of `_version`, `project`, `workflow`, `environments`, `run`, `cli`, `server`, `llm`"
)
}
}
Self::ServerManagedEnvironmentCwd { path, source } => write!(
f,
"`{path}` is server-managed and cannot be set in {source} settings; configure cwd on a server-managed environment instead."
),
}
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VersionError {
LegacyVersionKey,
UnsupportedHigherVersion { found: u32 },
}
impl fmt::Display for VersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LegacyVersionKey => f.write_str(
"settings files must use `_version` instead of `version`. Rename the key and try again.",
),
Self::UnsupportedHigherVersion { found } => write!(
f,
"settings schema version {found} is newer than this build supports (current: {CURRENT_VERSION}). Upgrade Fabro to read this file."
),
}
}
}
impl std::error::Error for VersionError {}
pub(crate) fn parse_settings(input: &str) -> Result<SettingsLayer, ParseError> {
let raw: toml::Value = toml::from_str(input).map_err(|e| ParseError::Toml(e.to_string()))?;
validate_version(&raw).map_err(ParseError::Version)?;
if let Some(table) = raw.as_table() {
for key in table.keys() {
if !ALLOWED_TOP_LEVEL_KEYS.contains(&key.as_str()) {
return Err(ParseError::UnknownTopLevelKey {
key: key.clone(),
hint: rename_hint(key),
});
}
}
// The settings-driven catalog plan re-introduced `[llm]` for provider
// and model rows. Old top-level `[llm]` keys (`provider`, `model`,
// ...) are forbidden and must continue to surface the migration hint
// for `[run.model]` rather than be silently dropped or generic-error.
if let Some(llm_table) = table.get("llm").and_then(toml::Value::as_table) {
for legacy_key in LEGACY_LLM_KEYS {
if llm_table.contains_key(*legacy_key) {
return Err(ParseError::UnknownTopLevelKey {
key: format!("llm.{legacy_key}"),
hint: rename_hint("llm"),
});
}
}
}
}
raw.try_into::<SettingsLayer>()
.map_err(|e| ParseError::Toml(e.to_string()))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsSource {
ActiveSettings,
Project,
Workflow,
DirectRun,
User,
}
impl SettingsSource {
/// `ActiveSettings` is the aggregated server-side settings file. Other
/// sources are client-provided leaf configs and must not be rewritten while
/// building or preparing a run manifest.
#[must_use]
pub(crate) fn runs_settings_migrations(self) -> bool {
matches!(self, Self::ActiveSettings)
}
#[must_use]
fn allows_environment_cwd(self) -> bool {
matches!(self, Self::ActiveSettings)
}
}
impl fmt::Display for SettingsSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = match self {
Self::ActiveSettings => "active server",
Self::Project => "project",
Self::Workflow => "workflow",
Self::DirectRun => "direct-run",
Self::User => "user",
};
f.write_str(label)
}
}
pub fn validate_settings_source(
layer: &SettingsLayer,
source: SettingsSource,
) -> Result<(), ParseError> {
if !source.allows_environment_cwd() {
for (id, environment) in layer.environments.iter() {
if environment.cwd.is_some() {
return Err(ParseError::ServerManagedEnvironmentCwd {
path: format!("environments.{id}.cwd"),
source,
});
}
}
}
Ok(())
}
fn validate_version(raw: &toml::Value) -> Result<(), VersionError> {
if let Some(table) = raw.as_table() {
if table.contains_key("version") {
return Err(VersionError::LegacyVersionKey);
}
if let Some(value) = table.get("_version").and_then(toml::Value::as_integer) {
let found = u32::try_from(value).unwrap_or(u32::MAX);
if found > CURRENT_VERSION {
return Err(VersionError::UnsupportedHigherVersion { found });
}
}
}
Ok(())
}
fn rename_hint(key: &str) -> Option<String> {
let target = match key {
"version" => "rename to `_version`",
"goal" | "goal_file" | "work_dir" | "directory" => "move to `[run]`",
"graph" => "move to `[workflow]`",
"labels" => "move to `[run.metadata]`",
"llm" => "rename to `[run.model]`",
"vars" => "rename to `[run.inputs]`",
"setup" => "rename to `[run.prepare]`",
"sandbox" => "rename to `[run.environment]` and `[environments.<slug>]`",
"checkpoint" => "move under `[run.checkpoint]`",
"pull_request" => "move under `[run.pull_request]`",
"artifacts" => "move under `[run.artifacts]`",
"hooks" => "move under `[[run.hooks]]`",
"mcp_servers" => "move under `[run.agent.mcps.<name>]` or `[cli.exec.agent.mcps.<name>]`",
"exec" => "rename to `[cli.exec]`",
"api" => "rename to `[server.api]`",
"web" => "rename to `[server.web]`",
"artifact_storage" => "rename to `[server.artifacts]`",
"storage_dir" | "data_dir" => "rename to `[server.storage] root`",
"max_concurrent_runs" => "rename to `[server.scheduler]` field",
"fabro" => "rename to `[project]`; project workflows now live under `.fabro/workflows`",
"git" => "split into `[run.git]` (local git behavior) and `[server.integrations.github]`",
"github" => {
"split into `[server.integrations.github]` (App identity/auth) and \
`[run.integrations.github.permissions]` (sandbox token scopes)"
}
"slack" => "move under `[server.integrations.slack]`",
"log" => "rename to `[server.logging]` or `[cli.logging]` depending on owner",
"prevent_idle_sleep" => "rename to `[cli.exec] prevent_idle_sleep`",
"verbose" => "rename to `[cli.output] verbosity`",
"upgrade_check" => "rename to `[cli.updates] check`",
"dry_run" => "rename to `[run.execution] mode = \"dry_run\"`",
"auto_approve" => "rename to `[run.execution] approval = \"auto\"`",
_ => return None,
};
Some(target.to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_empty_file() {
let file = "".parse::<SettingsLayer>().unwrap();
assert_eq!(file, SettingsLayer::default());
}
#[test]
fn parses_minimal_valid_file() {
let file = "_version = 1\n".parse::<SettingsLayer>().unwrap();
assert_eq!(file.version, Some(1));
}
#[test]
fn rejects_legacy_version_key_with_rename_hint() {
let err = "version = 1".parse::<SettingsLayer>().unwrap_err();
assert!(matches!(
err,
ParseError::Version(VersionError::LegacyVersionKey)
));
assert!(err.to_string().contains("_version"));
}
#[test]
fn rejects_unknown_top_level_key() {
let err = "unknown_key = 1".parse::<SettingsLayer>().unwrap_err();
assert!(matches!(err, ParseError::UnknownTopLevelKey { .. }));
}
#[test]
fn rejects_legacy_features_section() {
let namespace = "features";
let err = format!("[{namespace}]\nremoved_flag = true\n")
.parse::<SettingsLayer>()
.unwrap_err();
match err {
ParseError::UnknownTopLevelKey { key, .. } => assert_eq!(key, "features"),
other => panic!("expected UnknownTopLevelKey, got: {other:?}"),
}
}
#[test]
fn higher_version_rejected_with_upgrade_hint() {
let err = "_version = 99".parse::<SettingsLayer>().unwrap_err();
assert!(err.to_string().contains("Upgrade"));
}
#[test]
fn accepts_llm_overlay_subtree() {
let parsed = "[llm.providers.moonshot]\npriority = 60\n"
.parse::<SettingsLayer>()
.unwrap();
let llm = parsed.llm.unwrap();
assert_eq!(
llm.0["providers"]["moonshot"]["priority"].as_integer(),
Some(60)
);
}
#[test]
fn llm_overlay_merges_across_layers() {
let higher = r#"
[llm.providers.openai.models."gpt-5.4"]
small_default = true
"#
.parse::<SettingsLayer>()
.unwrap();
let lower = r#"
[llm.providers.openai.models."gpt-5.4"]
probe = true
"#
.parse::<SettingsLayer>()
.unwrap();
let merged = crate::Combine::combine(higher, lower);
let model = &merged.llm.unwrap().0["providers"]["openai"]["models"]["gpt-5.4"];
assert_eq!(model["small_default"].as_bool(), Some(true));
assert_eq!(model["probe"].as_bool(), Some(true));
}
#[test]
fn rejects_legacy_llm_provider_key_with_run_model_hint() {
let err = "[llm]\nprovider = \"openai\"\n"
.parse::<SettingsLayer>()
.unwrap_err();
let text = err.to_string();
assert!(
text.contains("run.model") || text.contains("llm"),
"got: {text}"
);
}
#[test]
fn rejects_legacy_llm_model_key_with_run_model_hint() {
let err = "[llm]\nmodel = \"opus\"\n"
.parse::<SettingsLayer>()
.unwrap_err();
let text = err.to_string();
assert!(
text.contains("run.model") || text.contains("llm"),
"got: {text}"
);
}
}