"""Managed env persistence, validation preview, and rendering.""" from __future__ import annotations import os from collections.abc import Mapping from typing import Any from config.paths import managed_env_path from .manifest import FIELD_BY_KEY, FIELDS, SECTIONS, ConfigFieldSpec from .sources import dotenv_values_from_file, is_locked_source, template_values from .validation import validate_values from .values import MASKED_SECRET, load_value_state, normalize_for_env def target_values_with_updates(updates: Mapping[str, Any]) -> dict[str, str]: """Return managed env values after applying admin updates.""" state = load_value_state() values = template_values() # Preserve existing managed values when present. If no managed config exists, # seed the first write from effective repo values to migrate legacy setups. managed_values = dotenv_values_from_file(managed_env_path()) if managed_values: values.update( {key: val for key, val in managed_values.items() if key in values} ) else: for key, entry in state.items(): if entry["source"] in {"repo_env", "template", "default"}: values[key] = str(entry["value"]) for key, value in updates.items(): field = FIELD_BY_KEY.get(key) if field is None: continue if is_locked_source(state[key]["source"]): continue if field.secret and value == MASKED_SECRET: continue values[key] = normalize_for_env(value) for field in FIELDS: values.setdefault(field.key, field.default) return values def effective_values_for_validation( target_values: Mapping[str, str], ) -> dict[str, str]: """Return values validated after preserving locked external sources.""" values = dict(target_values) for key, entry in load_value_state().items(): if is_locked_source(entry["source"]): values[key] = str(entry["value"]) return values def validate_updates(updates: Mapping[str, Any]) -> dict[str, Any]: """Validate partial admin updates and return a masked generated env preview.""" target_values = target_values_with_updates(updates) effective_values = effective_values_for_validation(target_values) valid, errors = validate_values(effective_values) return { "valid": valid, "errors": errors, "env_preview": render_env_file(target_values, mask_secrets=True), } def changed_pending_fields(updates: Mapping[str, Any]) -> list[str]: """Return changed fields that require manual runtime action.""" state = load_value_state() pending: list[str] = [] for key, value in updates.items(): field = FIELD_BY_KEY.get(key) if field is None or not (field.restart_required or field.session_sensitive): continue if normalize_for_env(value) == str(state[key]["value"]): continue pending.append(key) return pending def write_managed_env(updates: Mapping[str, Any]) -> dict[str, Any]: """Validate and atomically write the admin-managed env file.""" validation = validate_updates(updates) if not validation["valid"]: return validation | {"applied": False, "pending_fields": []} target_values = target_values_with_updates(updates) pending_fields = changed_pending_fields(updates) path = managed_env_path() path.parent.mkdir(parents=True, exist_ok=True) temp_path = path.with_suffix(path.suffix + ".tmp") temp_path.write_text(render_env_file(target_values), encoding="utf-8") os.replace(temp_path, path) return { "applied": True, "valid": True, "errors": [], "env_preview": render_env_file(target_values, mask_secrets=True), "path": str(path), "pending_fields": pending_fields, } def quote_env_value(value: str) -> str: """Quote a value when dotenv syntax requires it.""" if value == "": return "" escaped = value.replace("\\", "\\\\").replace('"', '\\"') if any(char.isspace() for char in value) or any( char in value for char in ('"', "#", "=", "$") ): return f'"{escaped}"' return value def render_env_file(values: Mapping[str, str], *, mask_secrets: bool = False) -> str: """Render a complete grouped env file.""" lines: list[str] = [ "# Managed by Free Claude Code /admin.", "# Edit in the server UI when possible.", "", ] fields_by_section: dict[str, list[ConfigFieldSpec]] = { section.section_id: [] for section in SECTIONS } for field in FIELDS: fields_by_section.setdefault(field.section_id, []).append(field) for section in SECTIONS: lines.append(f"# {section.label}") for field in fields_by_section.get(section.section_id, []): value = values.get(field.key, field.default) if mask_secrets and field.secret and value: value = MASKED_SECRET lines.append(f"{field.key}={quote_env_value(value)}") lines.append("") return "\n".join(lines).rstrip() + "\n"