Buckets:

download
raw
14.1 kB
"""
Configuration Bootstrap for Graph-Based Config System.
Loads essential configuration from multiple sources in priority order,
then migrates to graph-based configuration for runtime management.
"""
import logging
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import aiofiles
import yaml
from pydantic import BaseModel, ValidationError
from ciris_engine.schemas.config.essential import EssentialConfig
from ciris_engine.schemas.types import ConfigMapping, JSONDict
from .env_utils import get_env_var
logger = logging.getLogger(__name__)
class ConfigBootstrap:
"""Load essential config from multiple sources in priority order."""
@staticmethod
def _deep_merge(base: ConfigMapping, update: ConfigMapping) -> ConfigMapping:
"""Recursively merge two configuration dictionaries."""
for key, value in update.items():
if key in base and isinstance(base[key], dict) and isinstance(value, dict):
base[key] = ConfigBootstrap._deep_merge(base[key], value) # type: ignore[arg-type]
else:
base[key] = value
return base
@staticmethod
def _apply_env_overrides(config_data: ConfigMapping) -> ConfigMapping:
"""Apply environment variable overrides to config data."""
from typing import cast
# Database URL (overrides main_db path if set)
db_url = get_env_var("CIRIS_DB_URL")
if db_url:
db_section = cast(JSONDict, config_data.setdefault("database", {}))
db_section["database_url"] = db_url
# Database paths
db_path = get_env_var("CIRIS_DB_PATH")
if db_path:
db_section = cast(JSONDict, config_data.setdefault("database", {}))
db_section["main_db"] = db_path
secrets_db = get_env_var("CIRIS_SECRETS_DB_PATH")
if secrets_db:
db_section = cast(JSONDict, config_data.setdefault("database", {}))
db_section["secrets_db"] = secrets_db
audit_db = get_env_var("CIRIS_AUDIT_DB_PATH")
if audit_db:
db_section = cast(JSONDict, config_data.setdefault("database", {}))
db_section["audit_db"] = audit_db
# Service endpoints
llm_endpoint = get_env_var("OPENAI_API_BASE") or get_env_var("LLM_ENDPOINT")
if llm_endpoint:
services_section = cast(JSONDict, config_data.setdefault("services", {}))
services_section["llm_endpoint"] = llm_endpoint
llm_model = get_env_var("OPENAI_MODEL_NAME") or get_env_var("OPENAI_MODEL") or get_env_var("LLM_MODEL")
if llm_model:
services_section = cast(JSONDict, config_data.setdefault("services", {}))
services_section["llm_model"] = llm_model
# Security settings
retention_days = get_env_var("AUDIT_RETENTION_DAYS")
if retention_days:
try:
security_section = cast(JSONDict, config_data.setdefault("security", {}))
security_section["audit_retention_days"] = int(retention_days)
except ValueError:
logger.warning(f"Invalid AUDIT_RETENTION_DAYS value: {retention_days}")
# Operational limits
max_tasks = get_env_var("MAX_ACTIVE_TASKS")
if max_tasks:
try:
limits_section = cast(JSONDict, config_data.setdefault("limits", {}))
limits_section["max_active_tasks"] = int(max_tasks)
except ValueError:
logger.warning(f"Invalid MAX_ACTIVE_TASKS value: {max_tasks}")
max_depth = get_env_var("MAX_THOUGHT_DEPTH")
if max_depth:
try:
security_section = cast(JSONDict, config_data.setdefault("security", {}))
security_section["max_thought_depth"] = int(max_depth)
except ValueError:
logger.warning(f"Invalid MAX_THOUGHT_DEPTH value: {max_depth}")
# Runtime settings
log_level = get_env_var("LOG_LEVEL")
if log_level:
config_data["log_level"] = log_level.upper()
debug_mode = get_env_var("DEBUG_MODE")
if debug_mode:
config_data["debug_mode"] = debug_mode.lower() in ("true", "1", "yes", "on")
# Agent template (only used for first-time identity creation)
template = get_env_var("CIRIS_TEMPLATE")
if template:
config_data["default_template"] = template
return config_data
@staticmethod
def _resolve_database_paths(config_data: ConfigMapping) -> ConfigMapping:
"""Resolve relative database paths to use proper data directory.
If database paths are relative (not absolute), resolve them against
get_data_dir() to ensure they use the correct location based on
runtime environment (development/installed/managed mode).
"""
from typing import cast
from ciris_engine.logic.utils.path_resolution import get_data_dir
db_section = cast(JSONDict, config_data.get("database", {}))
data_dir = get_data_dir()
# Set or resolve main_db
if "main_db" not in db_section:
# Use schema default but resolved to proper data directory
db_section["main_db"] = str(data_dir / "ciris_engine.db")
else:
main_db = Path(str(db_section["main_db"]))
if not main_db.is_absolute():
# For relative paths, use just the filename (strip any data/ prefix)
db_section["main_db"] = str(data_dir / main_db.name)
# Set or resolve secrets_db
if "secrets_db" not in db_section:
db_section["secrets_db"] = str(data_dir / "secrets.db")
else:
secrets_db = Path(str(db_section["secrets_db"]))
if not secrets_db.is_absolute():
db_section["secrets_db"] = str(data_dir / secrets_db.name)
# Set or resolve audit_db
if "audit_db" not in db_section:
db_section["audit_db"] = str(data_dir / "ciris_audit.db")
else:
audit_db = Path(str(db_section["audit_db"]))
if not audit_db.is_absolute():
db_section["audit_db"] = str(data_dir / audit_db.name)
config_data["database"] = db_section
return config_data
@staticmethod
async def load_essential_config(
config_path: Optional[Path] = None, cli_overrides: Optional[ConfigMapping] = None
) -> EssentialConfig:
"""
Load essential configuration from multiple sources.
Priority order (highest to lowest):
1. CLI arguments (if provided)
2. Environment variables
3. Configuration file (YAML)
4. Schema defaults
Args:
config_path: Optional path to YAML config file
cli_overrides: Optional CLI argument overrides
Returns:
Validated EssentialConfig instance
"""
# Start with empty config data
config_data: ConfigMapping = {}
# Load from YAML file if exists
yaml_path = config_path or Path("config/essential.yaml")
if yaml_path.exists():
try:
async with aiofiles.open(yaml_path, "r", encoding="utf-8") as f:
yaml_content = await f.read()
yaml_data = yaml.safe_load(yaml_content) or {}
config_data = ConfigBootstrap._deep_merge(config_data, yaml_data)
logger.info(f"Loaded configuration from {yaml_path}")
except Exception as e:
logger.warning(f"Failed to load YAML config from {yaml_path}: {e}")
# Apply environment variable overrides
config_data = ConfigBootstrap._apply_env_overrides(config_data)
# Apply CLI overrides (highest priority)
if cli_overrides:
config_data = ConfigBootstrap._deep_merge(config_data, cli_overrides)
# Ensure database section exists with schema defaults if not present
if "database" not in config_data:
config_data["database"] = {}
# Resolve relative database paths to use proper data directory
config_data = ConfigBootstrap._resolve_database_paths(config_data)
# Create and validate config. Bootstrap YAML lives in persistent
# user volumes that outlive any single agent image, so schema
# evolution (renamed/removed fields) must not crash-loop the agent.
# Strip unknown keys, warn loudly, and retry. extra="forbid" stays on
# the inner models for defense-in-depth — only the outer bootstrap
# loader is forgiving.
try:
essential_config = EssentialConfig(**config_data)
logger.info("Essential configuration loaded and validated")
return essential_config
except ValidationError as e:
if not ConfigBootstrap._has_only_extra_forbidden_errors(e):
logger.error(f"Configuration validation failed: {e}")
raise ValueError(f"Invalid configuration: {e}") from e
stripped, dropped = ConfigBootstrap._strip_unknown_keys(EssentialConfig, config_data)
if not dropped:
logger.error(f"Configuration validation failed: {e}")
raise ValueError(f"Invalid configuration: {e}") from e
logger.warning(
"Essential config contained %d unknown field(s) from older schema "
"(persistent volume outlived the schema) — dropping and continuing: %s",
len(dropped),
", ".join(sorted(dropped)),
)
try:
essential_config = EssentialConfig(**stripped)
logger.info("Essential configuration loaded after stripping legacy keys")
return essential_config
except Exception as retry_e:
logger.error(f"Configuration validation failed even after stripping legacy keys: {retry_e}")
raise ValueError(f"Invalid configuration: {retry_e}") from retry_e
except Exception as e:
logger.error(f"Configuration validation failed: {e}")
raise ValueError(f"Invalid configuration: {e}") from e
@staticmethod
def _has_only_extra_forbidden_errors(err: ValidationError) -> bool:
"""Check whether every ValidationError entry is an extra_forbidden.
If any other error type is present (wrong type, missing required,
enum mismatch, etc.), we must NOT swallow them — they signal real
misconfiguration rather than a simple stale-volume/rename case.
"""
errors = err.errors()
if not errors:
return False
return all(e.get("type") == "extra_forbidden" for e in errors)
@staticmethod
def _strip_unknown_keys(
model_cls: type, data: Dict[str, Any]
) -> Tuple[Dict[str, Any], List[str]]:
"""Recursively drop keys not declared on the target Pydantic model.
Only strips keys that the model doesn't know about. Preserves all
known fields and recursively descends into nested BaseModel fields.
Returns (cleaned_dict, list_of_dropped_dotted_paths).
"""
dropped: List[str] = []
def _clean(submodel: type, subdata: Any, path: str) -> Any:
if not isinstance(subdata, dict):
return subdata
if not (isinstance(submodel, type) and issubclass(submodel, BaseModel)):
return subdata
known = set(submodel.model_fields.keys())
cleaned: Dict[str, Any] = {}
for key, value in subdata.items():
if key not in known:
dropped.append(f"{path}.{key}" if path else key)
continue
field_info = submodel.model_fields[key]
annotation = field_info.annotation
# Descend if the field is itself a BaseModel subclass.
if (
isinstance(annotation, type)
and issubclass(annotation, BaseModel)
and isinstance(value, dict)
):
cleaned[key] = _clean(annotation, value, f"{path}.{key}" if path else key)
else:
cleaned[key] = value
return cleaned
cleaned_data = _clean(model_cls, data, "")
return cleaned_data, dropped
@staticmethod
def get_config_metadata(config: EssentialConfig, yaml_path: Optional[Path] = None) -> JSONDict:
"""
Generate metadata about config sources for migration to graph.
Returns dict mapping config keys to their source information
(source, env_var/file, bootstrap_phase flag).
"""
metadata: JSONDict = {}
# Check which values came from environment
env_sources = {
"database.main_db": "CIRIS_DB_PATH",
"database.secrets_db": "CIRIS_SECRETS_DB_PATH",
"database.audit_db": "CIRIS_AUDIT_DB_PATH",
"services.llm_endpoint": "OPENAI_API_BASE",
"services.llm_model": "OPENAI_MODEL",
"security.audit_retention_days": "AUDIT_RETENTION_DAYS",
"limits.max_active_tasks": "MAX_ACTIVE_TASKS",
"security.max_thought_depth": "MAX_THOUGHT_DEPTH",
"log_level": "LOG_LEVEL",
"debug_mode": "DEBUG_MODE",
}
for key, env_var in env_sources.items():
if get_env_var(env_var):
metadata[key] = {"source": "env_var", "env_var": env_var, "bootstrap_phase": True}
# Mark file-sourced configs
if yaml_path and yaml_path.exists():
# Would need to track which specific values came from file
# For now, mark all non-env values as file-sourced
for key in ["database", "services", "security", "limits"]:
if key not in metadata:
metadata[key] = {"source": "config_file", "file": str(yaml_path), "bootstrap_phase": True}
# Everything else is from defaults
return metadata

Xet Storage Details

Size:
14.1 kB
·
Xet hash:
cf51dc20eccdb0a1d03bcbe918e3bf1ebdc5893c2c9b2d60d2f0ecef657798e5

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.