Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612"""Configuration management for crow-cli.
Default config files are Python string constants imported from defaults.On first access to ~/.agents/crow, defaults are written to disk if nothing exists.The user's config.yaml is read from disk for actual runtime config."""
import osimport refrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any, Literal
import yamlfrom crow_cli.memory import normalize_db_urifrom dotenv import load_dotenvfrom pydantic import BaseModel, ValidationError
from crow_cli.config.default import ( COMPOSE_YAML, CONFIG_YAML, SEARXNG_SETTINGS_YML, SYSTEM_PROMPT,)from crow_cli.agent.logger import setup_logger
ENV_PATTERN = re.compile(r"\$\{([^}]+)\}")
AGENTS_DIR = Path.home() / ".agents"# Config lives under ~/.agents/crow; the shared workspaces (skills, notes,# global AGENTS.md) are SIBLINGS of the config dir, directly under ~/.agents.DEFAULT_CONFIG_DIR = AGENTS_DIR / "crow"SKILLS_DIR = AGENTS_DIR / "skills"NOTES_DIR = AGENTS_DIR / "notes"GLOBAL_AGENTS_MD = AGENTS_DIR / "AGENTS.md"
def get_default_config_dir(config_dir: Path | str | None = None) -> Path: """Return the config directory. If config_dir is given, use it. Otherwise ~/.agents/crow.""" if config_dir is None: return DEFAULT_CONFIG_DIR return Path(config_dir).resolve()
# All default files as (destination_relative_path, content_string)_DEFAULT_FILES: dict[str, str] = { "config.yaml": CONFIG_YAML, "compose.yaml": COMPOSE_YAML, "prompts/system_prompt.jinja2": SYSTEM_PROMPT, "searxng/settings.yml": SEARXNG_SETTINGS_YML,}
def _write_defaults_if_missing(config_dir: Path) -> None: """Write default config files to disk only if they don't already exist.""" config_dir.mkdir(parents=True, exist_ok=True) (config_dir / "logs").mkdir(exist_ok=True)
for rel_path, content in _DEFAULT_FILES.items(): target = config_dir / rel_path if not target.exists(): target.parent.mkdir(parents=True, exist_ok=True) target.write_text(content)
def resolve_env_vars(value: Any, missing: set[str] | None = None) -> Any: """Recursively replace ${VAR} with environment variable values.
Unset variables expand to "" (parity with the Rust CLI), but their names are collected in `missing` so the caller can warn instead of failing later with an opaque provider error. """ if isinstance(value, str):
def _sub(m: re.Match) -> str: name = m.group(1) val = os.getenv(name) if val is None: if missing is not None: missing.add(name) return "" return val
return ENV_PATTERN.sub(_sub, value) elif isinstance(value, dict): return {k: resolve_env_vars(v, missing) for k, v in value.items()} elif isinstance(value, list): return [resolve_env_vars(v, missing) for v in value] return value
# OpenAI reasoning models (gpt-5, o3, ...) accept `reasoning_effort` instead# of `temperature`. These are the enumerable values from the OpenAI API# reference (developers.openai.com): none, minimal, low, medium, high, xhigh,# max. We validate config.yaml against this set so a typo fails fast at load# time instead of as an opaque provider 400 mid-turn.ReasoningEffort = Literal["none", "minimal", "low", "medium", "high", "xhigh", "max"]REASONING_EFFORT_VALUES = ("none", "minimal", "low", "medium", "high", "xhigh", "max")
# Per-model modality LIST — what the model can take as input. Default# ["text", "image"] = assume vision-capable until proven otherwise; a# text-only model gets ["text"]. audio/video are filled in by hand (or by a# probe) for models that natively take them (qwen3.x-max takes video).Modality = Literal["text", "image", "audio", "video"]MODALITY_VALUES = ("text", "image", "audio", "video")
class ReasoningEffortModel(BaseModel): """Validate a reasoning_effort value against OpenAI's enumerable set."""
reasoning_effort: ReasoningEffort
def parse_reasoning_effort(raw: Any) -> str: """Validate and normalize a config.yaml reasoning_effort value.
Raises ValueError naming the valid set when the value is not enumerable. """ value = str(raw).strip().lower() try: return ReasoningEffortModel(reasoning_effort=value).reasoning_effort except ValidationError: raise ValueError( f"reasoning_effort must be one of {', '.join(REASONING_EFFORT_VALUES)}, " f"got {raw!r}" ) from None
# Optional per-model sampling pass-through keys and their types. Absent key# = None = omitted from the request; explicit 0 / 0.0 values ARE sent.OPTIONAL_SAMPLING_PARAMS = ( ("top_p", float), ("top_k", int), ("min_p", float), ("presence_penalty", float), ("repetition_penalty", float),)
def parse_model_number( model_name: str, key: str, raw: Any, cast: type) -> float | int: """Coerce a per-model config.yaml number, failing fast with a message naming the model and key instead of a bare cast error.""" if isinstance(raw, bool): # YAML true/false would sneak through int()/float() raise ValueError(f"model {model_name!r}: {key} must be a number, got {raw!r}") try: return cast(raw) except (TypeError, ValueError): raise ValueError( f"model {model_name!r}: {key} must be a number, got {raw!r}" ) from None
def build_sampling_params( reasoning_effort: str | None, temperature: float, top_p: float | None = None, top_k: int | None = None, min_p: float | None = None, presence_penalty: float | None = None, repetition_penalty: float | None = None, parallel_tool_calls: bool | None = False,) -> dict[str, Any]: """The one sampling rule for every LLM call (react loop AND compaction): reasoning_effort when set — ALL other sampling params omitted, reasoning models reject them — else temperature plus any optional params the model config set (None = omit, so explicit 0 / 0.0 values ARE sent). top_k, min_p and repetition_penalty are non-standard OpenAI fields and are packed into extra_body for pass-through to compatible servers.
parallel_tool_calls is the exception: it is not a sampling param, so it is sent on BOTH branches and always as a literal bool. Omitting it is not the same as false — the provider default is true.""" params: dict[str, Any] = {"parallel_tool_calls": bool(parallel_tool_calls)} if reasoning_effort: params["reasoning_effort"] = reasoning_effort return params params["temperature"] = temperature if top_p is not None: params["top_p"] = top_p if presence_penalty is not None: params["presence_penalty"] = presence_penalty extra_body = { name: value for name, value in ( ("top_k", top_k), ("min_p", min_p), ("repetition_penalty", repetition_penalty), ) if value is not None } if extra_body: params["extra_body"] = extra_body return params
def sampling_params_for(config: "Config", model_id: str) -> dict[str, Any]: """Per-model sampling rule for every LLM call (react loop AND compaction): the configured model's reasoning_effort XOR temperature (+ optional top_p / top_k / min_p / presence_penalty / repetition_penalty). Models not in config get the defaults (temperature 0.6, no effort).""" model = next( (m for m in config.llm.models.values() if m.model_id == model_id), None ) if model is None: return {"temperature": 0.6} return build_sampling_params( model.reasoning_effort, model.temperature, top_p=model.top_p, top_k=model.top_k, min_p=model.min_p, presence_penalty=model.presence_penalty, repetition_penalty=model.repetition_penalty, parallel_tool_calls=config.parallel_tool_calls, )
def max_compact_tokens_for(config: "Config", model_id: str) -> int: """Per-model compaction threshold: the model's own max_compact_tokens when set, else the global MAX_COMPACT_TOKENS. Callers must resolve this LIVE from the session's current model on every check — the model can be switched mid-session (set_config_option), so never cache it at session init.""" model = next( (m for m in config.llm.models.values() if m.model_id == model_id), None ) if model is not None and model.max_compact_tokens is not None: return model.max_compact_tokens return config.MAX_COMPACT_TOKENS
@dataclassclass LLMProvider: name: str base_url: str | None = None api_key: str | None = field(default=None, repr=False)
@dataclassclass LLModel: name: str provider_name: str model_id: str # Per-model sampling, XOR: reasoning_effort wins and ALL other sampling # params are omitted entirely (reasoning models reject them); else # temperature plus any of the optional knobs below that are set. temperature: float = 0.6 reasoning_effort: ReasoningEffort | None = None # Optional sampling pass-through (None = omit). top_p / presence_penalty # are standard OpenAI fields; top_k / min_p / repetition_penalty are not # and are sent via extra_body to OpenAI-compatible servers. top_p: float | None = None top_k: int | None = None min_p: float | None = None presence_penalty: float | None = None repetition_penalty: float | None = None # Per-model compaction threshold override (None = the global # MAX_COMPACT_TOKENS). Local models typically get a lower ceiling than # subscription API models, which keep the global rate. max_compact_tokens: int | None = None # Input modalities. Assume vision-capable until proven otherwise; # ["text"] models get image/audio/video blocks stripped / routed around # (see model_routing). modality: list[Modality] = field(default_factory=lambda: ["text", "image"]) # Ordered fallback chain (model NAMES from this config) used when this # model cannot handle the modalities present in the conversation. fallbacks: list[str] = field(default_factory=list) parallel_tool_calls: bool = False
@property def option_value(self) -> str: """This model as a ``model`` config-option VALUE: ``provider:model_id``.
The wire separates the human-facing ``name`` from the value a client sends back, and both crow agents publish this shape, so it is the one translation a client needs to pick a model. The provider prefix is what routes the request — ``model_id`` alone loses it. """ return f"{self.provider_name}:{self.model_id}"
@dataclassclass LLMConfig: providers: dict[str, LLMProvider] = field(default_factory=dict) models: dict[str, LLModel] = field(default_factory=dict)
def option_value(self, name: str) -> str: """``-m NAME`` as the wire's ``model`` option wants it.
Model choice is the CLIENT's job — it travels over ``session/set_config_option``, never in an agent's argv — so this is where a client turns the name a human typed into the value an agent publishes.
Raises: ValueError: NAME is not a configured model. Raised rather than defaulted: a typo must fail before anything is spawned, not quietly run on the first model in config.yaml. """ try: return self.models[name].option_value except KeyError: known = ", ".join(self.models) or "none configured" raise ValueError(f"Unknown model {name!r}. Available: {known}.") from None
#: Keys accepted inside the ``goal:`` block. Rejected rather than ignored:#: ``goal: {max_turn: 25}`` silently doing nothing is a ceiling the user#: believes they set, which is worse than no ceiling at all.GOAL_CONFIG_KEYS = ("max_turns", "max_tokens")
@dataclassclass GoalConfig: """The limits on a ``/goal``, and the defaults for one set without them.
Kept apart from the react loop's own ceilings because they bound different axes. ``MAX_TOKENS`` is one model call's completion budget and ``Deps.max_turns`` is how many model round trips fit inside ONE turn; these bound how many turns a goal may start BY ITSELF, which is a much smaller number and the only one whose default is a judgement call about how much unattended spending is acceptable. """
#: Continuation turns a goal may start before it goes ``budget_limited``. #: The backstop for a goal that keeps finding real work forever: the #: no-progress rule in :mod:`crow_cli.agent2.goal` catches one that is #: spinning, this catches one that is genuinely grinding, and without it the #: only stop is the user noticing. max_turns: int = 25 #: Token ceiling applied to a goal set without one of its own. None means no #: ceiling, and that is the default because the honest number depends #: entirely on the objective — a guess that is too low stops real work, and #: one that is too high is not a limit. max_tokens: int | None = None
def parse_goal_config(raw: Any) -> GoalConfig: """The ``goal:`` block, from config.yaml or an override file.""" if raw is None: return GoalConfig() if not isinstance(raw, dict): raise ValueError(f"config.yaml: 'goal' must be a mapping, got {raw!r}") unknown = [k for k in raw if k not in GOAL_CONFIG_KEYS] if unknown: raise ValueError( f"config.yaml: unknown key(s) under 'goal': {', '.join(sorted(map(str, unknown)))}. " f"Known: {', '.join(GOAL_CONFIG_KEYS)}." ) goal = GoalConfig() if raw.get("max_turns") is not None: goal.max_turns = int(raw["max_turns"]) if raw.get("max_tokens") is not None: goal.max_tokens = int(raw["max_tokens"]) return goal
@dataclassclass Config: config_dir: Path llm: LLMConfig = field(default_factory=LLMConfig) db_uri: str = "" skills_dir: str = str(SKILLS_DIR) mcp_servers: dict[str, Any] = field(default_factory=dict) # Named agent servers a client can launch by name (Zed-style `agent_servers`): # every entry is a command, honored exactly as written; with no -a the # top entry is the default. See crow_cli.agents. agent_servers: dict[str, Any] = field(default_factory=dict) # image_store.s3 block (endpoint/bucket/access_key/secret_key) — when # present and reachable, images go to S3; else filesystem. Empty = FS. image_store: dict[str, Any] = field(default_factory=dict) #: The wake bus — redis pub/sub, db 0. Whoever finishes a task publishes #: a poke here; the agent process that owns the session is subscribed and #: routes it to that session's driver inbox. db 1 and 2 on the same server #: are celery's broker and result backend. Empty string disables the bus #: entirely, which degrades wake latency to the driver's backstop poll — #: the mailbox rows in sqlite are the truth, never the poke. redis_url: str = "" #: /goal's ceilings. See :class:`GoalConfig`. goal: GoalConfig = field(default_factory=GoalConfig) max_retries_per_step: int = 3 MAX_COMPACT_TOKENS: int = 190000 MAX_TOKENS: int = 38192 chunk_log: bool = False # Write every raw chunk to JSONL for debugging system_prompt: str = SYSTEM_PROMPT system_prompt_path: Path | None = None parallel_tool_calls: bool = False #: Do not merge this config's ``mcpServers`` into a session, so the #: session's tools are exactly the ones its client sent in ``session/new``. #: ``crow-cli acp2 --no-default-tool``: a generic agent by construction #: rather than by prompt — a conductor whose only arm is the MCP server its #: client handed it cannot reach for ``execute`` and go off script. no_default_tool: bool = False
@property def log_path(self) -> str: return str(self.config_dir / "logs" / "crow-cli.log")
@property def is_configured(self) -> bool: """Check if the config has at least one LLM provider and model.""" return bool(self.llm.providers and self.llm.models)
@classmethod def load(cls, config_dir: Path | None = None) -> "Config": target_dir = get_default_config_dir(config_dir) _logger = setup_logger(target_dir / "logs" / "crow-cli.log")
# Write defaults if nothing exists yet if not (target_dir / "config.yaml").exists(): _write_defaults_if_missing(target_dir)
# Load .env env_file = target_dir / ".env" if env_file.exists(): load_dotenv(env_file)
# If no config.yaml, return a bare Config config_file = target_dir / "config.yaml" if not config_file.exists(): _logger.info("No config.yaml found, returning bare Config") return cls( config_dir=target_dir, db_uri=f"sqlite:///{DEFAULT_CONFIG_DIR / 'crow.db'}", )
with open(config_file) as f: raw = yaml.safe_load(f) or {}
# Sampling is per-model now (see LLModel) — a leftover global would be # silently ignored, so reject it with a migration hint instead. for stale in ("TEMPERATURE", "reasoning_effort"): if stale in raw: raise ValueError( f"config.yaml: global {stale!r} was removed; set it per " f"model under models: (temperature / reasoning_effort)." )
_logger.info("RAW config.yaml mcpServers: %s", raw.get("mcpServers", {})) missing_vars: set[str] = set() parsed = resolve_env_vars(raw, missing_vars) _logger.info("PARSED config.yaml mcpServers after resolve_env_vars: %s", parsed.get("mcpServers", {})) if missing_vars: _logger.warning( "config.yaml references unset environment variables (expanded to empty): %s", ", ".join(f"${{{v}}}" for v in sorted(missing_vars)), ) # Parse parallel_tool_calls if "parallel_tool_calls" in raw: parallel_tool_calls: bool = bool(raw["parallel_tool_calls"]) else: parallel_tool_calls: bool = False # Parse providers llm = LLMConfig() for name, data in parsed.get("providers", {}).items(): llm.providers[name] = LLMProvider( name=name, api_key=data.get("api_key"), base_url=data.get("base_url"), )
# Parse models for name, data in parsed.get("models", {}).items(): raw_effort = data.get("reasoning_effort") raw_modality = data.get("modality", ["text", "image"]) if isinstance(raw_modality, str): raw_modality = [raw_modality] if not isinstance(raw_modality, list): raise ValueError( f"model {name!r}: modality must be a list, got {raw_modality!r}" ) modality = [str(m).strip().lower() for m in raw_modality] bad = [m for m in modality if m not in MODALITY_VALUES] if bad: raise ValueError( f"model {name!r}: modality entries must be one of " f"{', '.join(MODALITY_VALUES)}, got {bad}" ) llm.models[name] = LLModel( name=name, provider_name=data.get("provider", ""), model_id=data.get("model", ""), temperature=float(data.get("temperature", 0.6)), reasoning_effort=( parse_reasoning_effort(raw_effort) if raw_effort is not None else None ), # Optional sampling pass-through; absent key = None = omit # (0 / 0.0 are valid values and must survive). **{ key: parse_model_number(name, key, data[key], cast) for key, cast in OPTIONAL_SAMPLING_PARAMS if data.get(key) is not None }, max_compact_tokens=( parse_model_number( name, "max_compact_tokens", data["max_compact_tokens"], int ) if data.get("max_compact_tokens") is not None else None ), modality=modality, fallbacks=list(data.get("fallbacks") or []), parallel_tool_calls=parallel_tool_calls, )
# Parse overrides. db_uri is the canonical key; legacy memory_path # (a plain path) still works and becomes a sqlite URI. Default is # config_dir-relative so custom config dirs stay self-contained. db_uri = normalize_db_uri( parsed.get("db_uri") or parsed.get("memory_path") or str(target_dir / "crow.db") ) skills_dir = os.path.expanduser(parsed.get("skills_dir") or str(SKILLS_DIR)) overrides = {} for key, typ in ( ("max_retries_per_step", int), ("MAX_COMPACT_TOKENS", int), ("MAX_TOKENS", int), ): if key in parsed: overrides[key] = typ(parsed[key]) if "chunk_log" in parsed: overrides["chunk_log"] = bool(parsed["chunk_log"]) if "no_default_tool" in parsed: overrides["no_default_tool"] = bool(parsed["no_default_tool"])
mcp_servers = parsed.get("mcpServers", {}) _logger.info("FINAL mcp_servers stored in Config: %s", mcp_servers) agent_servers = parsed.get("agent_servers", {}) image_store = parsed.get("image_store") or {} # REDIS_PORT is the knob compose.yaml publishes the bus on, and .env is # loaded above, so one value moves the container and the client. # # A presence check, not an or-chain. ``redis_url: ""`` means "no wake # bus" — that is the documented way to turn it off, and an empty url is # what makes WakeWatcher.start a no-op and publish_wake return False. An # or-chain cannot express it: the empty string falls through to the # localhost default, so the one setting that disables the bus is the one # setting the chain ignores. Same reason apply_config_overrides below # tests ``"redis_url" in overrides`` rather than truthiness. if "redis_url" in parsed: redis_url = parsed["redis_url"] or "" else: redis_url = os.getenv("CROW_REDIS_URL") or ( f"redis://localhost:{os.getenv('REDIS_PORT', '6379')}/0" ) goal = parse_goal_config(resolve_env_vars(parsed.get("goal"), missing_vars)) system_prompt_path = None if "system_prompt_path" in parsed: system_prompt_path = Path(os.path.expanduser(parsed["system_prompt_path"]))
return cls( config_dir=target_dir, llm=llm, db_uri=db_uri, skills_dir=skills_dir, mcp_servers=mcp_servers, agent_servers=agent_servers, image_store=image_store, redis_url=redis_url, goal=goal, system_prompt_path=system_prompt_path, parallel_tool_calls=parallel_tool_calls, **overrides, )
def apply_config_overrides(config: "Config", config_file: Path | None) -> "Config": """Apply a YAML override file onto a loaded Config (the --config-file mechanism). Missing keys leave the loaded config untouched.""" if not config_file or not config_file.exists(): return config with open(config_file) as f: overrides = yaml.safe_load(f) or {} if "system_prompt_path" in overrides: config.system_prompt_path = Path(os.path.expanduser(overrides["system_prompt_path"])) if "skills_dir" in overrides: config.skills_dir = os.path.expanduser(overrides["skills_dir"]) if "db_uri" in overrides or "memory_path" in overrides: config.db_uri = normalize_db_uri( resolve_env_vars(overrides.get("db_uri") or overrides["memory_path"]) ) if "max_retries_per_step" in overrides: config.max_retries_per_step = int(overrides["max_retries_per_step"]) if "MAX_COMPACT_TOKENS" in overrides: config.MAX_COMPACT_TOKENS = int(overrides["MAX_COMPACT_TOKENS"]) if "MAX_TOKENS" in overrides: config.MAX_TOKENS = int(overrides["MAX_TOKENS"]) if "chunk_log" in overrides: config.chunk_log = bool(overrides["chunk_log"]) if "no_default_tool" in overrides: config.no_default_tool = bool(overrides["no_default_tool"]) if "mcpServers" in overrides: config.mcp_servers = overrides["mcpServers"] if "agent_servers" in overrides: config.agent_servers = resolve_env_vars(overrides["agent_servers"]) or {} if "image_store" in overrides: config.image_store = resolve_env_vars(overrides["image_store"]) or {} if "redis_url" in overrides: config.redis_url = resolve_env_vars(overrides["redis_url"]) or "" if "goal" in overrides: config.goal = parse_goal_config(resolve_env_vars(overrides["goal"])) return config