A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Optional TOML configuration (.agent-context.toml).""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import sys |
| 6 | from dataclasses import dataclass, field |
| 7 | from pathlib import Path |
| 8 | from typing import Any |
| 9 | |
| 10 | if sys.version_info >= (3, 11): |
| 11 | import tomllib |
| 12 | else: # pragma: no cover |
| 13 | import tomli as tomllib # type: ignore[no-redef] |
| 14 | |
| 15 | |
| 16 | DEFAULT_MAX_TOKENS = 12_000 |
| 17 | CONFIG_FILENAME = ".agent-context.toml" |
| 18 | |
| 19 | |
| 20 | @dataclass |
| 21 | class Config: |
| 22 | """Runtime configuration. All fields have safe defaults.""" |
| 23 | |
| 24 | max_tokens: int = DEFAULT_MAX_TOKENS |
| 25 | redact_secrets: bool = True |
| 26 | include: list[str] = field(default_factory=list) |
| 27 | exclude: list[str] = field(default_factory=list) |
| 28 | config_path: Path | None = None |
| 29 | |
| 30 | @classmethod |
| 31 | def defaults(cls) -> Config: |
| 32 | return cls() |
| 33 | |
| 34 | |
| 35 | class ConfigError(ValueError): |
| 36 | """Raised when configuration is present but invalid.""" |
| 37 | |
| 38 | |
| 39 | def find_config(root: Path) -> Path | None: |
| 40 | path = root / CONFIG_FILENAME |
| 41 | return path if path.is_file() else None |
| 42 | |
| 43 | |
| 44 | def load_config(root: Path, *, path: Path | None = None) -> Config: |
| 45 | """Load optional config from ``root`` or an explicit path. |
| 46 | |
| 47 | Missing config is not an error — returns defaults. |
| 48 | Malformed TOML raises ``ConfigError``. |
| 49 | """ |
| 50 | cfg_path = path or find_config(root) |
| 51 | if cfg_path is None: |
| 52 | return Config.defaults() |
| 53 | |
| 54 | try: |
| 55 | raw = cfg_path.read_bytes() |
| 56 | except OSError as exc: |
| 57 | raise ConfigError(f"Cannot read config: {cfg_path}: {exc}") from exc |
| 58 | |
| 59 | try: |
| 60 | data = tomllib.loads(raw.decode("utf-8")) |
| 61 | except Exception as exc: # noqa: BLE001 - tomllib raises various errors |
| 62 | raise ConfigError(f"Invalid TOML in {cfg_path}: {exc}") from exc |
| 63 | |
| 64 | if not isinstance(data, dict): |
| 65 | raise ConfigError(f"Config root must be a table: {cfg_path}") |
| 66 | |
| 67 | return _from_dict(data, cfg_path) |
| 68 | |
| 69 | |
| 70 | def _from_dict(data: dict[str, Any], cfg_path: Path) -> Config: |
| 71 | context = data.get("context", {}) or {} |
| 72 | security = data.get("security", {}) or {} |
| 73 | files = data.get("files", {}) or {} |
| 74 | |
| 75 | if not isinstance(context, dict) or not isinstance(security, dict) or not isinstance(files, dict): |
| 76 | raise ConfigError(f"Invalid section types in {cfg_path}") |
| 77 | |
| 78 | max_tokens = context.get("max_tokens", DEFAULT_MAX_TOKENS) |
| 79 | if not isinstance(max_tokens, int) or max_tokens <= 0: |
| 80 | raise ConfigError(f"context.max_tokens must be a positive integer in {cfg_path}") |
| 81 | |
| 82 | redact = security.get("redact_secrets", True) |
| 83 | if not isinstance(redact, bool): |
| 84 | raise ConfigError(f"security.redact_secrets must be a boolean in {cfg_path}") |
| 85 | |
| 86 | include = files.get("include", []) or [] |
| 87 | exclude = files.get("exclude", []) or [] |
| 88 | if not isinstance(include, list) or not isinstance(exclude, list): |
| 89 | raise ConfigError(f"files.include/exclude must be arrays in {cfg_path}") |
| 90 | if not all(isinstance(x, str) for x in include + exclude): |
| 91 | raise ConfigError(f"files.include/exclude must be string arrays in {cfg_path}") |
| 92 | |
| 93 | return Config( |
| 94 | max_tokens=max_tokens, |
| 95 | redact_secrets=redact, |
| 96 | include=list(include), |
| 97 | exclude=list(exclude), |
| 98 | config_path=cfg_path, |
| 99 | ) |