Files
jobboard/app/config.py
T
WolfandClaude Opus 5.5 59a301c351 Wire the app like the boilerplate: door, queue, schedules, eight tabs
app/main.py assembles it in the boilerplate's order, auth.connect first,
then the queue with the scheduler and the pages. TABS is Roles,
Applications, Agents, Companies, Jobs, Schedules, Messages and Account:
the alpha page's three views become templates on the boilerplate's
base.html, driven by static/roles.js (the old app.js) through api() and
toasts.js, over the unchanged JSON routes. Companies lists every board with
its open roles and the affinity control. A 422 is one sentence, so a toast
never reads [object Object].

Config takes the boilerplate's shape: APP_CONFIG or config.yaml, a missing
file is the defaults, an unknown key is an error. roles.yaml is gone;
config.yaml is the committed default and carries the starter agents.

pull is now a job that asks for the pull cascade, because a schedule
enqueues a job kind; its default schedule, every 6h, is declared beside it
in app/jobs/cascade.py. pull_batch's targets are keyword-only so the Jobs
tab offers no batch button. `app worker` is the boilerplate's; `app job`
still drains a queue of its own and fires no schedules.

test_coupling holds pipeline, auth and desk apart (desk may import
pipeline) and plugins to desk; test_architecture keeps app's layers, now
with main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:48:12 +02:00

152 lines
5.7 KiB
Python

"""Config loading: one YAML file, validated into a model.
The boilerplate's shape with roledesk's keys. A typo in a key is an error
naming the key, because every section forbids extras: a setting you believe
you have changed and have not is the expensive kind of typo. `sources` stays a
dict: one block per plugin, whose keys only the plugin knows.
The path comes from `app --config`, else `APP_CONFIG`, else `config.yaml`
beside the checkout. The Dockerfile sets `APP_CONFIG=/data/config.yaml`.
**A missing file is not an error.** Every field has a default that runs, so
`uv run app serve` works on a fresh clone. The committed `config.yaml` adds
the starter agents; `config.example.yaml` documents every key.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
import yaml
from pydantic import Field, ValidationError, model_validator
from app.models import Gate, Weights
from auth import AuthConfig
from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig
#: Where `load()` looks when nobody says. Read at import so a test can set the
#: variable first, and `app --config` overrides it after.
DEFAULT_PATH = Path(os.environ.get("APP_CONFIG", "config.yaml"))
class ServerConfig(Section):
"""Where uvicorn binds. Anything but a loopback address makes
`auth.enabled: auto` lock the app."""
host: str = "127.0.0.1"
port: int = 8790
class ExtractConfig(ModelTaskConfig):
"""Turning a posting's title and text into the gate columns. See
app/jobs/extract.py. Off by default, so nobody is billed by surprise:
on the API backend every batch is a paid call."""
enabled: bool = False
model: str = "claude-haiku-4-5-20251001"
batch_size: int = Field(20, ge=1)
class EmbedConfig(Section):
"""Turning a role into a vector for `fit`. Needs sentence-transformers;
without it `embed` says so and does nothing, and `score` leaves fit out.
See app/jobs/embed.py."""
#: Recorded on every vector. Changing it drops the old vectors, which are
#: a different geometry and never comparable.
model: str = "sentence-transformers/all-MiniLM-L6-v2"
#: Roles embedded per run at most, newest first.
per_run: int = Field(50_000, ge=1)
class AgentSeed(Section):
"""A search agent as `config.yaml` lists it. Imported into an empty
`agents` table once; after that the table is the answer."""
name: str
intent: str = ""
gates: list[Gate] = Field(default_factory=list)
weights: Weights = Field(default_factory=Weights)
enabled: bool = True
class Config(Section):
"""The whole file."""
server: ServerConfig = Field(default_factory=ServerConfig)
#: One SQLite file for the lot: roles and companies, the jobs table, the
#: notice log, the schedules, and auth's `auth_*` tables. One file is one
#: thing to back up.
database: Path = Path("roledesk.db")
#: What a clock on screen means, and what a `daily at 06:00` schedule is
#: 06:00 in. `every 6h` is six elapsed hours in any zone.
timezone: str = "Europe/Berlin"
log_level: str = "INFO"
#: The door, auth's own model. See auth/README.md.
auth: AuthConfig = Field(default_factory=AuthConfig)
#: Directories searched for `Source` plugins. A plugin imports `desk` only.
plugin_paths: list[str] = Field(default_factory=lambda: ["plugins"])
#: Workers per resource class. `http` is wide because a fetch is mostly waiting.
workers: WorkersConfig = Field(default_factory=WorkersConfig)
#: Who answers when a job needs a model. See desk/tools/agent.py.
agent: AgentConfig = Field(default_factory=AgentConfig)
extract: ExtractConfig = Field(default_factory=ExtractConfig)
embed: EmbedConfig = Field(default_factory=EmbedConfig)
#: Starter agents, imported once into an empty table.
agents: list[AgentSeed] = Field(default_factory=list)
#: One block per plugin: `enabled`, and whatever that plugin reads.
sources: dict[str, Any] = Field(default_factory=dict)
#: Where this came from, or None for the defaults. Never written back.
path: Path | None = Field(default=None, exclude=True)
@model_validator(mode="after")
def _credentials_live_in_the_database(self) -> Config:
"""`desk.tools.agent` reads a stored token from the app's database."""
self.agent.credentials_database = str(self.database)
return self
class ConfigError(ValueError):
"""A config file that exists but does not validate. The message names
every bad key, for the CLI to print as it is."""
def load(path: str | os.PathLike[str] | None = None) -> Config:
"""Read the YAML file into a `Config`, or return the defaults.
An empty file is the same as no file. A file that is not a mapping, or
that does not validate, is a `ConfigError`.
"""
where = Path(path).expanduser() if path else DEFAULT_PATH
if not where.exists():
return Config()
raw: Any = yaml.safe_load(where.read_text(encoding="utf-8"))
if raw is None:
return Config(path=where)
if not isinstance(raw, dict):
raise ConfigError(f"{where}: expected a mapping of settings, "
f"got {type(raw).__name__}")
try:
return Config(**raw, path=where)
except ValidationError as exc:
raise ConfigError(f"{where}:\n{_explain(exc)}") from None
def _explain(exc: ValidationError) -> str:
lines = []
for err in exc.errors():
where = ".".join(str(p) for p in err["loc"]) or "(top level)"
detail = err["msg"]
if err["type"] == "extra_forbidden":
detail = "unknown setting, check the spelling against config.example.yaml"
lines.append(f" {where}: {detail}")
return "\n".join(lines)