diff --git a/app/__init__.py b/app/__init__.py index 53f08ae..e5cfd08 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,14 +1,19 @@ """roledesk: a job-search engine on the `desk` plugin contract and the `pipeline` queue. -Layers, bottom to top, each importing only from those below it (see + uv run app serve + +The package is `app` because the boilerplate's deploy kit assumes it. Layers, +bottom to top, each importing only from those below it (see `tests/test_architecture.py`): models the shapes handed round, and the key functions for clustering - config `roles.yaml`, validated + config `config.yaml`, validated storage `Store`: every SQL statement against `roledesk.db` - jobs the work this app queues - web the FastAPI app - cli the entry point + jobs the work this app queues, and its default schedule + web the tabs and the JSON routes behind them + main `create_app()`: the door, the queue, the pages, in that order + cli `app serve`, `app job`, `app worker`, `app companies`, `app db` -`desk` and `pipeline` come from the news repo and are imported, never edited. +`pipeline/`, `auth/` and `desk/` are downstream copies (of the boilerplate and +of news) and import nothing from here; `tests/test_coupling.py` holds that. """ diff --git a/app/__main__.py b/app/__main__.py new file mode 100644 index 0000000..38026fe --- /dev/null +++ b/app/__main__.py @@ -0,0 +1,12 @@ +"""`python -m app` is `app`. + +For a checkout whose console script is not on the PATH — a worktree the release +scripts made, a container shell, a colleague who has not run `uv sync`. +""" + +from __future__ import annotations + +from app.cli import main + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/app/cli.py b/app/cli.py index eef6024..7d785a0 100644 --- a/app/cli.py +++ b/app/cli.py @@ -1,13 +1,28 @@ -"""`app serve | job pull|extract|embed|score | companies seed|add|check | db init`.""" +"""The command line. + + app serve [--host H] [--port P] [--reload] + app job pull|extract|embed|score [--payload JSON] + app worker [--workers http=8,cpu=1] [--until-idle] + app companies seed|add|check + app db init + +`app job` queues the work on a queue of its own and drains it here, so a +cascade such as `pull` runs every stage it starts before the command returns, +and prints one line per stage. `app worker` is the boilerplate's: the same +queue with no web app, and the schedules fired from it. + +`python -m app` is the same thing, for a checkout with no console script. +""" from __future__ import annotations import argparse import json import logging +import os import sys from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from app import config as config_mod @@ -26,18 +41,27 @@ if str(_ROOT) not in sys.path: def build_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser(prog="app", - description="A job-search engine on desk.") - parser.add_argument("-c", "--config", default=None, help="path to roles.yaml") + parser = argparse.ArgumentParser( + prog="app", description="roledesk: a job-search engine on desk and the pipeline queue.") + parser.add_argument("-c", "--config", default=None, + help="the YAML file to read (default: $APP_CONFIG, then config.yaml)") parser.add_argument("-v", "--verbose", action="store_true") sub = parser.add_subparsers(dest="command", required=True) serve = sub.add_parser("serve", help="run the web app") - serve.add_argument("--host", default="127.0.0.1") - serve.add_argument("--port", type=int, default=8790) + serve.add_argument("--host", default="", help="override config server.host") + serve.add_argument("--port", type=int, default=0, help="override config server.port") + serve.add_argument("--reload", action="store_true", help="restart on a source change") - job = sub.add_parser("job", help="queue a job or task and drain the queue once") - job.add_argument("kind", choices=["pull", "extract", "embed", "score"]) + job = sub.add_parser("job", help="queue a job, task or cascade and drain the queue") + job.add_argument("kind", help="pull, extract, embed, score, or any registered job") + job.add_argument("--payload", default="{}", + help="JSON spread over a job handler's keyword arguments") + + worker = sub.add_parser("worker", help="drain the job table with no web app") + worker.add_argument("--workers", default="", help="resource=count,... e.g. cpu=2") + worker.add_argument("--until-idle", action="store_true", + help="run what is queued, then exit") companies = sub.add_parser("companies", help="the target company list") companies_sub = companies.add_subparsers(dest="action", required=True) @@ -60,52 +84,89 @@ def main(argv: list[str] | None = None) -> int: format="%(asctime)s %(levelname)s %(name)s: %(message)s") try: if args.command == "serve": - from app.web import serve - config_mod.load(args.config) # a config error here, not in uvicorn - return serve(args.host, args.port, args.config) + return serve(args) if args.command == "job": return run_job(args) + if args.command == "worker": + return run_worker(args) if args.command == "companies": return run_companies(args) return init_db(args) - except FileNotFoundError as exc: + except (FileNotFoundError, config_mod.ConfigError) as exc: print(exc, file=sys.stderr) return 1 +def serve(args: argparse.Namespace) -> int: + import uvicorn + + config = config_mod.load(args.config) # a config error here, not in uvicorn + host = args.host or config.server.host + port = args.port or config.server.port + level = str(config.log_level).lower() + if args.reload: + # The reloader builds the app in a fresh process from an import + # string, and that process reads the config from $APP_CONFIG. + if args.config: + os.environ["APP_CONFIG"] = str(args.config) + uvicorn.run("app.main:app", host=host, port=port, reload=True, log_level=level) + return 0 + from app.main import create_app + + uvicorn.run(create_app(config), host=host, port=port, log_level=level) + return 0 + + def run_job(args: argparse.Namespace) -> int: - """Queue `kind` — a job, or a task from `pipeline.TASKS` such as `pull` — - then run everything queued until the queue is idle, and print how it - ended: one line per task, from each task's final job. `pull` is a - cascade, so it prints a line for every stage it went through.""" - from pipeline import FINAL, JobQueue, Jobs, TASKS + """Queue `kind` (a cascade such as `pull`, a task from `pipeline.TASKS`, + or a job), run everything queued until the queue is idle, and print how + it ended: one line per task, from each task's final job. `pull` is a + cascade, so it prints a line for every stage it went through. + + The queue fires no schedules: the command asked for one piece of work.""" from app import jobs from app.storage import Store + from pipeline import FINAL, JOBS, TASKS, JobQueue, Jobs + cascade = jobs.cascade + + try: + payload: dict[str, Any] = json.loads(args.payload or "{}") + except json.JSONDecodeError as exc: + print(f"--payload is not JSON: {exc}", file=sys.stderr) + return 2 + chained = args.kind in cascade.CASCADES + if not chained and args.kind not in TASKS and args.kind not in JOBS: + known = sorted(set(JOBS) | set(TASKS)) + print(f"no job kind {args.kind!r}. Have: {', '.join(known)}", file=sys.stderr) + return 2 config = config_mod.load(args.config) store = Store(config.database) - queue = JobQueue(Jobs(config.database), config, store, + queue = JobQueue(Jobs(str(config.database)), config, store, workers=config.workers.model_dump(), - bootstrap=jobs.BOOTSTRAP, bootstrap_arg=config.path) - if args.kind in TASKS: - task_id = queue.start_task(TASKS[args.kind]()) + bootstrap=jobs.BOOTSTRAP, bootstrap_arg=str(config.path or ""), + tz=config.timezone, schedules=False) + if chained or args.kind not in JOBS: + task_id = queue.start_task(cascade.chain(args.kind) if chained + else TASKS[args.kind]()) queue.drain() started = queue.jobs.of_task(task_id) if not started: + print(json.dumps({"task": args.kind, "result": "nothing to do"})) return 0 - cascade = queue.jobs.of_cascade(started[0].cascade_id) - finals = [j for j in cascade if j.kind == FINAL] + whole = queue.jobs.of_cascade(started[0].cascade_id) + finals = [j for j in whole if j.kind == FINAL] for final in finals: # A task's own jobs say what the work did; the final job only # tallies how they ended. Up to five, so a pull of twenty board # batches stays one readable line. - work = [j.result for j in cascade if j.task_id == final.task_id + work = [j.result for j in whole if j.task_id == final.task_id and j.kind != FINAL][:5] print(json.dumps({"id": final.id, "state": final.state, "result": final.result, "work": work, "error": final.error})) return 0 if all(f.state == "done" for f in finals) else 1 - queued = queue.enqueue(args.kind) + queued = queue.enqueue(args.kind, payload) queue.drain() if queued is not None: done = queue.jobs.get(queued.id) @@ -116,6 +177,24 @@ def run_job(args: argparse.Namespace) -> int: return 0 +def run_worker(args: argparse.Namespace) -> int: + """`python -m pipeline.worker` with this app's settings filled in: the + bootstrap, the pools from `workers:`, and the timezone, without which a + `daily at 06:00` schedule would run at 06:00 on the machine's clock.""" + from app.jobs import BOOTSTRAP + from pipeline.worker import main as worker_main + + config = config_mod.load(args.config) + argv = ["--db", str(config.database), "--bootstrap", BOOTSTRAP, + "--bootstrap-arg", str(args.config or config.path or ""), + "--timezone", str(config.timezone)] + argv += ["--workers", args.workers or + ",".join(f"{name}={n}" for name, n in config.workers.model_dump().items())] + if args.until_idle: + argv.append("--until-idle") + return worker_main(argv) + + def run_companies(args: argparse.Namespace) -> int: from app.storage import Store diff --git a/app/config.py b/app/config.py index 73b6b83..657f6f6 100644 --- a/app/config.py +++ b/app/config.py @@ -1,7 +1,16 @@ -"""Config loading: one YAML file, `roles.yaml`, validated into a model. +"""Config loading: one YAML file, validated into a model. -A typo in a key is an error naming the key, because `Section` forbids extras. -`sources` stays a dict: one block per plugin, whose keys only the plugin knows. +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 @@ -11,12 +20,23 @@ from pathlib import Path from typing import Any import yaml -from pydantic import Field, ValidationError +from pydantic import Field, ValidationError, model_validator -from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig from app.models import Gate, Weights +from auth import AuthConfig +from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig -DEFAULT_PATH = Path(os.environ.get("ROLEDESK_CONFIG", "roles.yaml")) +#: 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): @@ -42,7 +62,7 @@ class EmbedConfig(Section): class AgentSeed(Section): - """A search agent as `roles.yaml` lists it. Imported into an empty + """A search agent as `config.yaml` lists it. Imported into an empty `agents` table once; after that the table is the answer.""" name: str @@ -55,9 +75,27 @@ class AgentSeed(Section): class Config(Section): """The whole file.""" - database: str = "roledesk.db" + 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) @@ -65,28 +103,41 @@ class Config(Section): 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 was loaded from. Never written back into the file. - path: str = Field("", exclude=True) + + #: 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(FileNotFoundError): - """A config that exists but does not validate. A FileNotFoundError so the - CLI reports both kinds of unusable config the same way: as a message.""" +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 | Path | None = None) -> Config: - p = Path(path or DEFAULT_PATH).expanduser() - if not p.exists(): - raise FileNotFoundError( - f"No config at {p}. Copy config.example.yaml to {p} and edit it.") - with p.open(encoding="utf-8") as fh: - raw = yaml.safe_load(fh) or {} +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: - config = Config(**raw, path=str(p)) + return Config(**raw, path=where) except ValidationError as exc: - raise ConfigError(f"{p}:\n{_explain(exc)}") from None - config.agent.credentials_database = config.database - return config + raise ConfigError(f"{where}:\n{_explain(exc)}") from None def _explain(exc: ValidationError) -> str: diff --git a/app/jobs/__init__.py b/app/jobs/__init__.py index a2593f9..21cbabe 100644 --- a/app/jobs/__init__.py +++ b/app/jobs/__init__.py @@ -1,15 +1,18 @@ -"""The work roledesk queues. Importing this package registers every job and -every task in `pipeline.TASKS`. +"""The work roledesk queues. Importing this package registers every job, every +task in `pipeline.TASKS` and the default schedule in `pipeline.SCHEDULES`. pull.py read every configured plugin and store the roles it yields extract.py title and description into the gate columns, on Haiku embed.py one vector per open role, for fit score.py every open role scored for every enabled search agent - cascade.py pull → extract → embed → score, and what `TASKS` offers + cascade.py pull → extract → embed → score, what `TASKS` offers, the + `pull` job a schedule fires, and that schedule """ from __future__ import annotations +from typing import Any + from app.jobs import cascade, embed, extract, pull, score from app.jobs.score import seed_agents @@ -17,9 +20,10 @@ from app.jobs.score import seed_agents BOOTSTRAP = "app.jobs:bootstrap" -def bootstrap(config_path: str = "") -> tuple[object, object]: - """roledesk's `(config, store)` from the config file alone, for a job run - in its own process or by `python -m pipeline.worker`.""" +def bootstrap(config_path: str = "") -> tuple[Any, Any]: + """`(config, store)` from the config file alone, for a job run in its own + process or by `python -m pipeline.worker`. Importing this module is what + fills `JOBS`, which is why the worker is handed a name rather than a queue.""" from app import config as config_mod from app.storage import Store diff --git a/app/jobs/cascade.py b/app/jobs/cascade.py index f2cf15d..bb96406 100644 --- a/app/jobs/cascade.py +++ b/app/jobs/cascade.py @@ -15,6 +15,10 @@ too, since embedding does not read its columns and the order costs nothing. A batch that failed, or a stage somebody killed, does not stop the chain: the work comes round on the next pull, and a rescore of what did arrive is worth having either way. + +`pull` is a job as well as a cascade, because a schedule enqueues a job kind +and a cascade is started by one: the `pull` job's only work is to ask for the +chain. Its default schedule, `every 6h`, is declared here beside it. """ from __future__ import annotations @@ -24,9 +28,9 @@ from dataclasses import replace from functools import partial from typing import Any -from desk.tools import agent -from pipeline import TASKS, Tally, Task from app.jobs import embed, extract, pull, score # noqa: F401 -- registers the jobs +from desk.tools import agent +from pipeline import NULL, TASKS, RunContext, Tally, Task, declare, job STEP = "app.jobs.cascade:step" @@ -98,8 +102,35 @@ def step(config: Any, store: Any, tally: Tally, payload: dict[str, Any], *, return out -#: What `app job ` and the Jobs view offer. `pull` is the whole -#: chain; the others are one stage each, on their own. -TASKS["pull"] = partial(task, "pull", cascade="pull") +def chain(name: str) -> Task: + """The first stage of cascade `name`, carrying `step`, so the rest follow.""" + return task(CASCADES[name][0], cascade=name) + + +@job("pull", "Pulling every board", resource="http") +def run_pull(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str, Any]: + """Ask for the pull cascade once this job is done. Its own work is nil. + + Under `NULL` there is no queue to take the chain, so this raises rather + than report a pull that never happened; `app job pull` starts the chain + on a queue of its own instead of calling this. + """ + if not context.task(chain("pull")): + raise RuntimeError("no queue to start the pull cascade on") + return {"started": " > ".join(CASCADES["pull"])} + + +#: What `app job ` and the Jobs tab offer besides `pull`: one stage +#: each, on its own. `pull` is not a task here, since it is a job: a name that +#: is both would be two buttons on the Jobs tab. for _name in ("extract", "embed", "score"): TASKS[_name] = partial(task, _name) + + +# ---------------------------------------------------------------- schedules + +# The default, next to the job it fires. A starting point rather than a +# setting: a cadence edited or switched off on the Schedules tab survives +# every restart (see pipeline/schedule.py). +declare("pull", "pull", "every 6h", + label="Pull every board, then extract, embed and score") diff --git a/app/jobs/pull.py b/app/jobs/pull.py index 479afb9..6863ba5 100644 --- a/app/jobs/pull.py +++ b/app/jobs/pull.py @@ -28,7 +28,7 @@ from app.storage import Store log = logging.getLogger(__name__) #: As many boards in the air per job as there are `http` workers by default -#: (see `roles.yaml`), so one pull keeps the pool busy without one job +#: (see `config.yaml`), so one pull keeps the pool busy without one job #: holding all 190-odd boards' worth of retries against a single attempt. BATCH_SIZE = 10 @@ -52,10 +52,14 @@ def _boards(config: Config, store: Store) -> list[dict[str, Any]]: @job("pull_batch", "Reading boards", resource="http") -def run_batch(config: Config, store: Store, targets: list[str], *, - context: RunContext = NULL) -> dict[str, Any]: +def run_batch(config: Config, store: Store, *, targets: list[str], + context: RunContext = NULL) -> dict[str, Any]: """Fetch this batch's boards and `upsert_role` every item they list. + `targets` is keyword-only without a default, which is what makes it a + required payload key to the queue: the Jobs tab offers no button for a + batch that could only fail without one. + Builds the `ats` plugin scoped to just these targets — `Source.targets()` reads `options[subscription_field]`, so handing it only this batch's entries is what keeps one job from re-fetching every other job's boards. @@ -105,6 +109,6 @@ def close_boards(config: Config, store: Store, tally: Tally, *, def pull_task() -> Task: """The pull on its own. `app.jobs.cascade` makes it the first stage - of the chain that `TASKS["pull"]` starts.""" + of the chain the `pull` job starts.""" return Task(kind="pull_batch", finder=_boards, name="pull", label="Reading every board", settle=SETTLE) diff --git a/app/jobs/score.py b/app/jobs/score.py index fdba1ee..bdbf781 100644 --- a/app/jobs/score.py +++ b/app/jobs/score.py @@ -197,7 +197,7 @@ def run_score(config: Config, store: Store, *, # ------------------------------------------------------------------ seeding def seed_agents(config: Config, store: Store) -> list[str]: - """Import `roles.yaml`'s `agents:` into an empty table, once. The state + """Import `config.yaml`'s `agents:` into an empty table, once. The state marker is what makes it once: deleting every agent afterwards is a choice, and a restart must not undo it.""" if store.state.get(SEEDED) or store.agents(): @@ -213,5 +213,5 @@ def seed_agents(config: Config, store: Store) -> list[str]: added.append(seed.name) if added: store.state[SEEDED] = store.now() - log.info("imported %d agents from %s", len(added), config.path or "roles.yaml") + log.info("imported %d agents from %s", len(added), config.path or "the defaults") return added diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..727326e --- /dev/null +++ b/app/main.py @@ -0,0 +1,118 @@ +"""The app, assembled. + + uv run app serve + uv run uvicorn app.main:app --reload + +`create_app()` is the whole of the wiring, and the order in it is the point: +config and store, the door, the queue, the pages. Everything before the queue +is something the queue needs; everything after it is something that enqueues. +""" + +from __future__ import annotations + +import logging +from typing import Any + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from fastapi.staticfiles import StaticFiles +from starlette.exceptions import HTTPException as StarletteHTTPException + +import auth +from app import jobs +from app.config import Config, load +from app.storage import Store +from app.web import STATIC_DIR, api_router, pages_router, templates +from pipeline import Jobs +from pipeline.web import connect + +log = logging.getLogger(__name__) + +#: Prefixes whose errors are JSON rather than a page. A caller that asked for +#: data gets `{"detail": ...}`; a browser that asked for a page gets one. +API_PREFIXES = ("/api", "/ws", "/_jobs", "/_auth", "/static", "/health") + + +def create_app(config: Config | None = None) -> FastAPI: + """Build the application. A test passes its own config; nobody else does.""" + config = config or load() + logging.basicConfig( + level=getattr(logging, str(config.log_level).upper(), logging.INFO), + format="%(asctime)s %(levelname)s %(name)s: %(message)s") + + # `Store` first, so the file is at the current schema before the queue and + # auth add their own tables to it. + store = Store(config.database) + jobs.seed_agents(config, store) + + app = FastAPI(title="roledesk", docs_url="/api/docs", + summary="ATS boards in, roles scored per search agent, " + "applications tracked.") + app.state.config = config + app.state.store = store + + # The door, first, because it is the door. The middleware covers every + # route whenever it was added, `/ws/jobs` included; written first, nobody + # has to work that out. `auto` locks unless the bind address is loopback. + # Auth's tables are the `auth_*` ones in the same file. + auth.connect(app, auth.AuthStore(config.database), config.auth, + host=config.server.host, open=("/static/",), + stylesheet="/static/app.css") + + app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static") + + # The queue: routes, socket, ``, ``, and a + # worker in the lifespan that also fires the schedules. `tz` is what a + # `daily at 06:00` cadence is read in. + connect(app, Jobs(str(config.database)), config, store, + workers=config.workers.model_dump(), + bootstrap=jobs.BOOTSTRAP, bootstrap_arg=str(config.path or ""), + tz=config.timezone) + + app.include_router(api_router) + app.include_router(pages_router) + + @app.get("/health", include_in_schema=False) + def health() -> dict[str, Any]: + """Open, session-free, and it reads the store: `docker compose up + --wait` waits on this, so it has to prove the database answers.""" + return {"ok": True, "open_roles": sum(store.open_role_counts().values())} + + @app.exception_handler(StarletteHTTPException) + async def errors(request: Request, exc: StarletteHTTPException) -> Any: + """JSON for a caller, a page for a browser. One shape for every API + error, `{"detail": ""}`, so `api()` in `static/app.js` + turns any failure into one toast carrying the server's own wording.""" + headers = getattr(exc, "headers", None) + wants_page = (request.method == "GET" + and not request.url.path.startswith(API_PREFIXES)) + if not wants_page: + return JSONResponse({"detail": exc.detail}, status_code=exc.status_code, + headers=headers) + return templates.TemplateResponse( + request, "error.html", + {"status": exc.status_code, "detail": exc.detail}, + status_code=exc.status_code, headers=headers) + + @app.exception_handler(RequestValidationError) + async def invalid(request: Request, exc: RequestValidationError) -> JSONResponse: + """FastAPI's 422 body is a list of objects, which a toast shows as + `[object Object]`. The same errors as one sentence instead.""" + said = "; ".join( + f"{'.'.join(str(p) for p in err['loc'] if p != 'body')}: {err['msg']}" + for err in exc.errors()) + return JSONResponse({"detail": said or "The request was not valid."}, + status_code=422) + + return app + + +def __getattr__(name: str) -> Any: + """`app.main:app` for uvicorn, built on first use rather than on import, so + importing this module opens no database.""" + if name == "app": + built = create_app() + globals()["app"] = built + return built + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/app/static/app.css b/app/static/app.css new file mode 100644 index 0000000..bab1250 --- /dev/null +++ b/app/static/app.css @@ -0,0 +1,381 @@ +/* The whole stylesheet: the boilerplate's, with roledesk's palette in the + * custom properties (a quiet UI face and newsdesk's teal signal colour) and + * roledesk's own tabs in the section at the bottom. No build step. + * + * The `--desk-jobs-*` block at the bottom is how `` is themed. The + * element keeps to a shadow root and reads those properties, so an app skins + * it without forking it -- which is the deal that lets the queue ship its own + * UI and still look like it belongs here. + */ + +:root { + color-scheme: light dark; + + --bg: #eceff1; + --panel: #ffffff; + --fg: #1a2027; + --dim: #505c67; + --line: #d6dbdf; + --accent: #14655b; + --accent-fg: #ffffff; + --good: #15803d; + --warn: #b45309; + --error: #b91c1c; + + --radius: 10px; + --gap: 1rem; + --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, Roboto, + "Helvetica Neue", Arial, sans-serif; + --mono: ui-monospace, "Cascadia Mono", Consolas, monospace; +} + +@media (prefers-color-scheme: dark) { + :root { + --bg: #14181c; + --panel: #1b2126; + --fg: #e3e7ea; + --dim: #a8b2ba; + --line: #2a3238; + --accent: #4fb3a3; + --accent-fg: #14181c; + --good: #4ade80; + --warn: #fbbf24; + --error: #f87171; + } +} + +* { box-sizing: border-box; } + +body { + margin: 0; + background: var(--bg); + color: var(--fg); + font: 16px/1.55 var(--font); +} + +code, pre { font-family: var(--mono); font-size: 0.9em; } +pre { overflow-x: auto; white-space: pre-wrap; word-break: break-word; } +a { color: var(--accent); } + +/* -- the frame ------------------------------------------------------------ */ + +.top { + display: flex; + align-items: center; + gap: var(--gap); + flex-wrap: wrap; + padding: 0.75rem 1.25rem; + background: var(--panel); + border-bottom: 1px solid var(--line); + position: sticky; + top: 0; + z-index: 5; +} + +.brand span { color: var(--accent); } +.brand { + font-weight: 700; + letter-spacing: 0.02em; + text-decoration: none; + color: var(--fg); +} + +.top nav { display: flex; gap: 0.25rem; flex-wrap: wrap; } + +.top nav a { + padding: 0.35rem 0.7rem; + border-radius: var(--radius); + text-decoration: none; + color: var(--dim); +} +.top nav a:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); } +.top nav a.active { background: var(--accent); color: var(--accent-fg); } + +main { + max-width: 62rem; + margin: 0 auto; + padding: 1.5rem 1.25rem 4rem; +} + +h1 { font-size: 1.6rem; margin: 0 0 0.5rem; } +h2 { font-size: 1.1rem; margin: 0 0 0.75rem; } + +.lede { color: var(--dim); max-width: 46rem; } +.muted { color: var(--dim); } +.small { font-size: 0.85rem; } + +.note { + background: color-mix(in srgb, var(--accent) 8%, var(--panel)); + border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); + border-radius: var(--radius); + padding: 0.9rem 1rem; + max-width: 46rem; +} + +.card { + background: var(--panel); + border: 1px solid var(--line); + border-radius: var(--radius); + padding: 1rem 1.15rem; + margin: 1.25rem 0; +} + +.row-between { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: var(--gap); +} + +.summary { display: grid; grid-template-columns: max-content 1fr; gap: 0.4rem 1rem; margin: 0; } +.summary dt { color: var(--dim); } +.summary dd { margin: 0; } + +.buttons { display: flex; flex-wrap: wrap; gap: 0.5rem; } + +button { + font: inherit; + padding: 0.4rem 0.9rem; + border-radius: var(--radius); + border: 1px solid transparent; + background: var(--accent); + color: var(--accent-fg); + cursor: pointer; +} +button:disabled { opacity: 0.55; cursor: progress; } +button.secondary { + background: transparent; + color: var(--fg); + border-color: var(--line); +} + +.badge { + display: inline-block; + padding: 0 0.4rem; + border-radius: 999px; + background: var(--line); + color: var(--dim); + font-size: 0.8rem; +} + +/* -- the notice log ------------------------------------------------------- */ + +.notice { + border: 1px solid var(--line); + border-left: 3px solid var(--dim); + border-radius: var(--radius); + padding: 0.6rem 0.8rem; + margin: 0.5rem 0; +} +.notice.error { border-left-color: var(--error); } +.notice.warning { border-left-color: var(--warn); } +.notice.good { border-left-color: var(--good); } +.notice-head { display: flex; justify-content: space-between; gap: var(--gap); align-items: start; } +.notice-text { font-weight: 600; } + +/* -- toasts --------------------------------------------------------------- */ + +.toasts { + position: fixed; + right: 1rem; + bottom: 1rem; + width: min(26rem, calc(100vw - 2rem)); + display: flex; + flex-direction: column; + gap: 0.5rem; + /* A popover brings its own box; this is a container, not a card. */ + border: 0; + background: transparent; + padding: 0; + margin: 0; + inset: auto 1rem 1rem auto; +} + +.toast { + display: flex; + gap: 0.5rem; + align-items: start; + background: var(--panel); + color: var(--fg); + border: 1px solid var(--line); + border-left: 3px solid var(--dim); + border-radius: var(--radius); + padding: 0.6rem 0.75rem; + box-shadow: 0 8px 24px rgb(0 0 0 / 18%); + animation: toast-in 160ms ease-out; +} +.toast.error { border-left-color: var(--error); } +.toast.warning { border-left-color: var(--warn); } +.toast.good { border-left-color: var(--good); } +.toast.out { opacity: 0; transition: opacity 180ms ease-in; } +.toast-body { flex: 1; min-width: 0; } +.toast-count { color: var(--dim); } +.toast-fix { color: var(--dim); } +.toast-actions { display: flex; gap: 0.5rem; align-items: center; } +.toast-actions:empty { display: none; } +.toast-close { + background: transparent; + color: var(--dim); + border: 0; + padding: 0 0.25rem; + line-height: 1; +} + +@keyframes toast-in { + from { transform: translateY(6px); opacity: 0; } + to { transform: none; opacity: 1; } +} + +/* -- the modules' elements ------------------------------------------------ */ +/* `` and `` from the queue, `` from + * auth: all three read these, so they look like one application and none of + * them is forked to get it. An element the app has not got yet simply matches + * nothing. */ + +desk-jobs, desk-schedules, auth-account { + display: block; + --desk-jobs-bg: var(--panel); + --desk-jobs-fg: var(--fg); + --desk-jobs-dim: var(--dim); + --desk-jobs-accent: var(--accent); + --desk-jobs-error: var(--error); + --desk-jobs-font: var(--font); + --desk-jobs-gap: var(--gap); + --desk-jobs-radius: var(--radius); +} + +/* -- roledesk's tabs ------------------------------------------------------ */ +/* Roles, Applications, Agents and Companies, drawn by static/roles.js. */ + +input, select, textarea { font: inherit; color: inherit; } + +.filters { + display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; + padding: .75rem; margin-bottom: .5rem; + background: var(--panel); + border: 1px solid var(--line); border-radius: var(--radius); +} +.filters input[type="search"], .filters input[type="text"], .filters select { + padding: .4rem .55rem; + border: 1px solid var(--line); border-radius: .3rem; + background: var(--bg); min-width: 0; +} +.filters input[type="search"] { flex: 1 1 12rem; } +.filters input[type="text"] { flex: 1 1 8rem; } +.check { + display: flex; align-items: center; gap: .35rem; + color: var(--dim); font-size: .9rem; white-space: nowrap; +} +.check[hidden] { display: none; } + +button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, +textarea:focus-visible { + outline: 2px solid var(--accent); outline-offset: 1px; +} + +.count { color: var(--dim); font-size: .85rem; margin: .75rem 0 .5rem; } + +/* roles */ + +.roles { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; } +.role { + background: var(--panel); border: 1px solid var(--line); border-radius: .5rem; + padding: .6rem .8rem; + display: flex; flex-direction: column; gap: .35rem; +} +.role-main { display: flex; flex-wrap: wrap; align-items: baseline; gap: .5rem; } +.role-title { font-weight: 600; text-decoration: none; } +.role-title:hover { text-decoration: underline; } +.role-company { color: var(--dim); } +.role-meta { + display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; + color: var(--dim); font-size: .85rem; +} +.role-meta .badge { + background: transparent; color: var(--dim); + border: 1px solid var(--line); border-radius: 1rem; + padding: .05rem .55rem; font-size: .78rem; +} +.role-meta .badge:empty { display: none; } +.role-meta .role-score { + color: var(--accent); border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); + font-variant-numeric: tabular-nums; +} +.role-gated { color: var(--error); font-size: .85rem; } +.role-age { margin-left: auto; } +.role.muted, tr.muted { opacity: .55; } +.role.gated { opacity: .7; } + +.affinity { display: inline-flex; gap: .15rem; } +.role-main .affinity { margin-left: auto; } +.affinity button { + background: none; border: 1px solid var(--line); border-radius: .3rem; + padding: .1rem .4rem; color: var(--dim); font-size: .85rem; line-height: 1.4; +} +.affinity button[aria-pressed="true"] { + color: var(--accent-fg); background: var(--accent); border-color: var(--accent); +} +.affinity button[data-value="-1"][aria-pressed="true"] { + background: var(--error); border-color: var(--error); +} + +.save { align-self: flex-start; padding: .3rem .8rem; font-weight: 600; } +.save[disabled] { + background: var(--bg); color: var(--dim); border-color: var(--line); cursor: default; +} + +/* agents */ + +.agents { list-style: none; margin: 0 0 1rem; padding: 0; display: flex; flex-direction: column; gap: .4rem; } +.agent { + display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; + background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); + padding: .5rem .75rem; +} +.agent a { font-weight: 600; text-decoration: none; } +.agent-meta { color: var(--dim); font-size: .85rem; flex: 1 1 12rem; } +.agent-form { + display: flex; flex-direction: column; gap: .6rem; max-width: 44rem; + background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); + padding: .75rem 1rem; +} +.agent-form h2 { font-size: 1rem; margin: 0; } +.agent-form label { display: flex; flex-direction: column; gap: .2rem; font-size: .9rem; } +.agent-form label.inline { flex-direction: row; align-items: center; gap: .4rem; } +.agent-form input[type="text"], .agent-form textarea, .agent-form input[type="number"] { + background: var(--bg); border: 1px solid var(--line); + border-radius: .3rem; padding: .35rem .5rem; width: 100%; +} +.agent-form textarea { font-family: var(--mono); font-size: .85rem; } +.agent-form .hint { color: var(--dim); font-size: .8rem; overflow-wrap: anywhere; } +.weights { display: flex; gap: .75rem; flex-wrap: wrap; border: 1px solid var(--line); border-radius: .3rem; } +.weights label { flex: 1 1 6rem; } +.agent-actions { display: flex; gap: .5rem; flex-wrap: wrap; } + +/* applications and companies */ + +table.list { width: 100%; border-collapse: collapse; background: var(--panel); } +table.list th, table.list td { + text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--line); + font-size: .9rem; +} +table.list .num { text-align: right; font-variant-numeric: tabular-nums; } +table.list select { padding: .25rem .4rem; } + +/* Below tablet width a table stops being a table: each row becomes a card, + its cells stacked, so nothing scrolls sideways to be read. */ +@media (max-width: 640px) { + table.list thead { display: none; } + table.list, table.list tbody, table.list tr, table.list td { display: block; width: 100%; } + table.list tr { margin-bottom: .6rem; border: 1px solid var(--line); border-radius: .4rem; } + table.list td { border-bottom: none; } + table.list .num { text-align: left; } + table.list td::before { + content: attr(data-label); + display: block; font-size: .72rem; color: var(--dim); margin-bottom: .1rem; + } +} + +@media (max-width: 480px) { + .role-age, .role-main .affinity { margin-left: 0; } +} diff --git a/app/static/app.js b/app/static/app.js new file mode 100644 index 0000000..c4fe9d5 --- /dev/null +++ b/app/static/app.js @@ -0,0 +1,73 @@ +/* The one way this app talks to its own API. + * + * const kinds = await api("/api/jobs/kinds"); + * await api("/api/jobs", { method: "POST", body: { kind: "tick" } }); + * + * Every button on every tab goes through `api`, so a failure is always one + * toast carrying the server's own sentence. That is the other half of the + * bargain the exception handler in `app/main.py` makes: it promises every + * error body is `{"detail": ""}`, and this is what + * cashes the promise. Wording a failure in the browser as well would be two + * descriptions of one thing, and the client's would be the one that was wrong. + * + * `body` may be a plain object; it is JSON-encoded with the header set, since + * forgetting the header is the failure that looks like a server bug. + */ + +(function () { + "use strict"; + + async function api(path, options) { + const o = Object.assign({}, options); + const headers = Object.assign({ Accept: "application/json" }, o.headers); + if (o.body !== undefined && typeof o.body !== "string") { + headers["Content-Type"] = "application/json"; + o.body = JSON.stringify(o.body); + } + o.headers = headers; + + let response; + try { + response = await fetch(path, o); + } catch (e) { + // The network, not the server: there is no `detail` to read. + window.toast.error("Could not reach the server. Is it still running?"); + throw e; + } + const text = await response.text(); + let data = null; + if (text) { + try { data = JSON.parse(text); } catch (e) { data = null; } + } + if (!response.ok) { + const detail = (data && data.detail) + || `The server answered ${response.status} and said nothing about why.`; + window.toast.error(String(detail)); + const error = new Error(detail); + error.status = response.status; + error.detail = detail; + throw error; + } + return data; + } + + /* `window.esc` is toasts.js's — it loads first and had to have one anyway, + and two copies of an escaper is one of them being fixed. See base.html + for the load order that makes this safe. */ + + /* The queue's own elements do their own fetching, so their failures never + pass through `api()`. They raise an event carrying the server's `detail` + instead, and this is the same bargain kept one level out: one toast, the + server's sentence, nothing reworded here. + + `desk-jobs:notice` is a notice arriving on the element's socket, which + live.js already delivers as `app:notice` — so it is deliberately not + listened for. Two toasts for one failure is the bug that listening would + buy. */ + document.addEventListener("desk-schedules:error", (e) => { + const detail = (e.detail && e.detail.detail) || "That change was refused."; + window.toast.error(String(detail)); + }); + + window.api = api; +})(); diff --git a/app/static/jobs.js b/app/static/jobs.js new file mode 100644 index 0000000..79adc3c --- /dev/null +++ b/app/static/jobs.js @@ -0,0 +1,66 @@ +/* The Jobs tab: a button per kind. + * + * The list comes from `/api/jobs/kinds`, which is the registry, so a new + * `@job` in `app/jobs.py` is a new button and this file is not edited. What + * the queue is doing is ``'s business, and it draws itself. + * + * Failures are the server's sentence through `api()`, which toasts it -- so + * there is no error handling here beyond keeping the button usable. + */ + +(function () { + "use strict"; + + const host = document.getElementById("kinds"); + if (!host) return; + + async function start(kind, button) { + button.disabled = true; + try { + const job = await window.api("/api/jobs", { method: "POST", body: { kind } }); + window.toast.good(`${job.label || kind}: queued.`); + } catch (e) { + /* already toasted */ + } finally { + button.disabled = false; + } + } + + async function draw() { + let kinds; + try { + kinds = await window.api("/api/jobs/kinds"); + } catch (e) { + host.innerHTML = 'could not read the job kinds'; + return; + } + if (!kinds.length) { + host.innerHTML = 'nothing is registered'; + return; + } + host.innerHTML = ""; + kinds.forEach((k) => { + const button = document.createElement("button"); + button.type = "button"; + button.textContent = k.label || k.kind; + button.title = k.solo ? `${k.kind} -- one at a time` : k.kind; + button.dataset.kind = k.kind; + button.addEventListener("click", () => start(k.kind, button)); + host.appendChild(button); + }); + } + + /* `` takes up no space while the queue is idle, so the line that + says so is the app's to show and hide. Off the same socket the element + reads, through live.js, rather than by asking the element: it keeps to a + shadow root and owes nobody an answer about its own height. */ + document.addEventListener("app:jobs", (e) => { + const idle = document.getElementById("idle"); + if (!idle) return; + const state = e.detail || {}; + const busy = (state.running || []).length + (state.queued || []).length; + idle.hidden = busy > 0; + }); + + document.addEventListener("DOMContentLoaded", draw); +})(); diff --git a/app/static/live.js b/app/static/live.js new file mode 100644 index 0000000..2a3aae4 --- /dev/null +++ b/app/static/live.js @@ -0,0 +1,84 @@ +/* The page's one connection to the server: the queue's socket. + * + * `/ws/jobs` sends two kinds of message, and this turns each into a document + * event so that no other script opens a socket of its own — five tabs each + * holding their own would be five reconnect loops to get subtly wrong: + * + * app:jobs the whole queue: {running, queued, recent, ...}. Sent on + * connect and after every change. `window.live.jobs` holds the + * latest, for a script that loads after a message. + * app:notice one notice from the log (app/notices.py), its count + * included. toasts.js shows it. + * app:schedules the schedule list, whenever one fires, is skipped or is + * edited. `` holds its own socket and does not + * need this; it is here for a page that wants to say when the + * next run is without drawing the whole table. + * + * Nothing here dispatches by hand: the name is taken from the message, so a + * fourth type is a listener somewhere and no edit to this file. + * + * Reconnects with backoff, because the server restarting is the normal case + * and a page that needs reloading after every deploy is a page people reload + * by habit and then trust less. + */ + +(function () { + "use strict"; + + const live = { jobs: null, connected: false }; + window.live = live; + + let backoff = 1000; + + function connect() { + const proto = location.protocol === "https:" ? "wss:" : "ws:"; + const socket = new WebSocket(`${proto}//${location.host}/ws/jobs`); + socket.onopen = () => { + backoff = 1000; + live.connected = true; + }; + socket.onmessage = (ev) => { + let msg; + try { msg = JSON.parse(ev.data); } catch (e) { return; } + const { type, ...body } = msg; + if (!type) return; + if (type === "jobs") live.jobs = body; + document.dispatchEvent(new CustomEvent(`app:${type}`, { detail: body })); + }; + socket.onclose = () => { + live.connected = false; + setTimeout(async () => { + if (await signedOut()) return; + connect(); + }, backoff); + backoff = Math.min(backoff * 2, 30000); + }; + } + + /* A refused handshake and a dead server look identical from here: the + middleware in `auth/` closes the socket before accepting it, which the + server turns into an HTTP 403 on the upgrade, and the browser reports the + same abnormal close it reports for "the server is gone". So one cheap + request tells them apart, and an expired session sends the page to the + door instead of leaving it reconnecting quietly against a login form for + the rest of the afternoon. `` has the same trick as its + `probe` attribute; it is done here so that it is done on every tab. */ + async function signedOut() { + let answer; + try { + answer = await fetch("/api/jobs", { headers: { Accept: "application/json" } }); + } catch (e) { + return false; // offline: keep the backoff going + } + if (answer.status !== 401 && answer.status !== 403) return false; + const next = location.pathname + location.search; + location.assign(`/login?next=${encodeURIComponent(next)}`); + return true; + } + + document.addEventListener("DOMContentLoaded", () => { + // The login page has no session for the socket to use, and asking would + // only be a 401 every second while somebody types a password. + if (location.pathname !== "/login") connect(); + }); +})(); diff --git a/app/static/messages.js b/app/static/messages.js new file mode 100644 index 0000000..f60d424 --- /dev/null +++ b/app/static/messages.js @@ -0,0 +1,85 @@ +/* The Messages tab: the notice log as a table. + * + * `/api/notices` is the queue's own route (`pipeline/web/jobs.py`) -- the log + * belongs to the queue, the wording belongs to the app, and this page is only + * the reading of it. It redraws on `app:notice` so a failure that happens + * while you are looking at this tab appears without a reload. + */ + +(function () { + "use strict"; + + const host = document.getElementById("notices"); + const clearAll = document.getElementById("clear-all"); + if (!host) return; + + const esc = window.esc; + + function when(iso) { + if (!iso) return ""; + const d = new Date(iso); + return isNaN(d) ? iso : d.toLocaleString(); + } + + function row(n) { + const count = n.count > 1 ? `x${n.count}` : ""; + return ` +
+
+ ${esc(n.text)} ${count} + +
+
${esc(n.code)}${n.key ? " / " + esc(n.key) : ""} + -- last said ${esc(when(n.last_at))} + ${n.kind === "bug" ? "-- looks like a bug rather than a bad day" : ""}
+ ${n.fix ? `
${esc(n.fix)}
` : ""} + ${n.detail ? `
details +
${esc(n.detail)}
` : ""} +
`; + } + + async function draw() { + let list; + try { + list = await window.api("/api/notices"); + } catch (e) { + host.innerHTML = 'could not read the log'; + return; + } + if (!list.length) { + host.innerHTML = 'Nothing to report.'; + return; + } + host.innerHTML = list.map(row).join(""); + } + + host.addEventListener("click", async (e) => { + const id = e.target.dataset && e.target.dataset.clear; + if (!id) return; + e.target.disabled = true; + try { + await window.api(`/api/notices/${id}`, { method: "DELETE" }); + await draw(); + } catch (err) { + e.target.disabled = false; + } + }); + + if (clearAll) { + clearAll.addEventListener("click", async () => { + clearAll.disabled = true; + try { + const out = await window.api("/api/notices", { method: "DELETE" }); + window.toast.good(`Cleared ${out.cleared}.`); + await draw(); + } catch (e) { + /* already toasted */ + } finally { + clearAll.disabled = false; + } + }); + } + + document.addEventListener("app:notice", draw); + document.addEventListener("DOMContentLoaded", draw); +})(); diff --git a/app/static/roles.js b/app/static/roles.js new file mode 100644 index 0000000..99523ec --- /dev/null +++ b/app/static/roles.js @@ -0,0 +1,524 @@ +/* roledesk's four tabs: Roles, Applications, Agents and Companies. + * + * Each template renders an empty shell; everything on screen comes from the + * JSON routes in app/web/api.py, and every action (Save, an affinity click, a + * status change, an agent saved) is a fetch from here, not a form post. One + * script for the four, because they share the company list and the affinity + * control; `init` looks at which containers the page has. + * + * Every request goes through `api()` from app.js, so a failure is one toast + * carrying the server's own sentence. The catch blocks here only put the + * control back the way it was. + */ +(function () { + "use strict"; + + const api = window.api; + + /** @type {Map} company_key -> row from /api/companies */ + let companies = new Map(); + /** @type {Map} role_uid -> application, for roles already tracked */ + let trackedByRole = new Map(); + /** @type {object[]} the last /api/roles response, already muted-filtered by the server */ + let roles = []; + /** @type {object[]} every search agent, from /api/agents */ + let agents = []; + + const $ = (sel, root = document) => root.querySelector(sel); + + function debounce(fn, ms) { + let timer; + return (...args) => { + clearTimeout(timer); + timer = setTimeout(() => fn(...args), ms); + }; + } + + function count(n, one, many) { + return `${n} ${n === 1 ? one : many || `${one}s`}`; + } + + async function loadCompanies() { + const rows = await api("/api/companies"); + companies = new Map(rows.map((c) => [c.key, c])); + } + + async function loadApplications() { + const applications = await api("/api/applications"); + trackedByRole = new Map(applications.filter((a) => a.role_uid).map((a) => [a.role_uid, a])); + return applications; + } + + async function loadAgents() { + agents = await api("/api/agents"); + } + + function cell(tr, label, content) { + const td = document.createElement("td"); + td.dataset.label = label; + if (typeof content === "string") td.textContent = content; + else td.appendChild(content); + tr.appendChild(td); + return td; + } + + // ---------------------------------------------------------------- affinity + + const AFFINITY_LEVELS = [ + { value: -1, label: "Mute" }, + { value: 0, label: "Neutral" }, + { value: 1, label: "Like" }, + { value: 2, label: "Favourite" }, + ]; + + /* The four buttons. `onChange` runs after the server accepted the change, + so each tab decides what a change means for it: the role list refetches, + since muting changes which roles come back, and the company table redraws. */ + function affinityControl(companyKey, current, onChange) { + const span = document.createElement("span"); + span.className = "affinity"; + span.setAttribute("role", "group"); + span.setAttribute("aria-label", "Company affinity"); + for (const level of AFFINITY_LEVELS) { + const button = document.createElement("button"); + button.type = "button"; + button.dataset.value = String(level.value); + button.textContent = level.label[0]; + button.title = level.label; + button.setAttribute("aria-label", level.label); + button.setAttribute("aria-pressed", String(level.value === current)); + button.addEventListener("click", () => setAffinity(companyKey, level.value, onChange)); + span.appendChild(button); + } + return span; + } + + async function setAffinity(companyKey, affinity, onChange) { + if (!companyKey) return; + let updated; + try { + updated = await api(`/api/companies/${encodeURIComponent(companyKey)}/affinity`, { + method: "POST", body: { affinity }, + }); + } catch (err) { + return; + } + const company = companies.get(companyKey); + if (company) company.affinity = updated.affinity; + await onChange(); + } + + // ------------------------------------------------------------------- roles + + function filterParams() { + const params = new URLSearchParams(); + for (const key of ["q", "company", "place"]) { + const value = $(`#f-${key}`).value.trim(); + if (value) params.set(key, value); + } + for (const key of ["function", "remote", "days"]) { + const value = $(`#f-${key}`).value; + if (value) params.set(key, value); + } + if ($("#f-show-muted").checked) params.set("show_muted", "true"); + const agent = $("#f-agent").value; + if (agent) { + params.set("agent", agent); + if ($("#f-gated").checked) params.set("gated", "1"); + } + return params; + } + + async function loadRoles() { + try { + roles = await api(`/api/roles?${filterParams().toString()}`); + } catch (err) { + return; + } + renderRoles(); + } + + function relativeAge(iso) { + const then = new Date(iso); + const minutes = Math.max(0, Math.floor((Date.now() - then.getTime()) / 60000)); + if (minutes < 60) return `${minutes} min ago`; + const hours = Math.floor(minutes / 60); + if (hours < 24) return `${hours} h ago`; + return `${Math.floor(hours / 24)} d ago`; + } + + function renderRoles() { + const list = $("#roles-list"); + const template = $("#role-row"); + list.replaceChildren(); + + for (const role of roles) { + const node = template.content.cloneNode(true); + const li = node.querySelector(".role"); + const company = companies.get(role.company_key); + + if (company && company.affinity === -1) li.classList.add("muted"); + + const titleLink = node.querySelector(".role-title"); + titleLink.textContent = role.title || "(untitled)"; + titleLink.href = role.original_url || "#"; + + node.querySelector(".role-company").textContent = role.company || ""; + node.querySelector(".role-place").textContent = role.place || ""; + node.querySelector(".role-remote").textContent = role.remote || ""; + node.querySelector(".role-function").textContent = role.function || ""; + node.querySelector(".role-age").textContent = relativeAge(role.first_seen); + if (role.score !== null && role.score !== undefined) { + const badge = node.querySelector(".role-score"); + badge.textContent = role.gated_reason ? "gated" : role.score.toFixed(2); + badge.title = "Score for the selected agent"; + node.querySelector(".role-gated").textContent = role.gated_reason || ""; + if (role.gated_reason) li.classList.add("gated"); + } + + node.querySelector(".affinity").replaceWith( + affinityControl(role.company_key, company ? company.affinity : 0, loadRoles)); + + const saveButton = node.querySelector(".save"); + setSaveButton(saveButton, role.uid); + saveButton.addEventListener("click", () => saveRole(role.uid, saveButton)); + + list.appendChild(node); + } + + $("#roles-count").textContent = count(roles.length, "role"); + } + + function setSaveButton(button, uid) { + const saved = trackedByRole.has(uid); + button.textContent = saved ? "Saved" : "Save"; + button.disabled = saved; + } + + async function saveRole(uid, button) { + button.disabled = true; + button.textContent = "Saving…"; + try { + const application = await api("/api/applications", { + method: "POST", body: { role_uid: uid, status: "saved" }, + }); + trackedByRole.set(uid, application); + setSaveButton(button, uid); + } catch (err) { + button.disabled = false; + button.textContent = "Save"; + } + } + + // The filter bar's order: "Newest", or one agent's score. Keeps the current + // choice when the list is refreshed, unless that agent is gone. + function renderAgentSelect() { + const select = $("#f-agent"); + const current = select.value; + select.replaceChildren(new Option("Newest", "")); + for (const agent of agents) { + const label = agent.enabled ? agent.name : `${agent.name} (off)`; + select.appendChild(new Option(label, String(agent.id))); + } + select.value = agents.some((a) => String(a.id) === current) ? current : ""; + $("#show-gated").hidden = !select.value; + } + + /* `?q=`, `?company=`, `?place=` and `?agent=` prefill the filter bar, which + is how the Companies and Agents tabs link into this one. */ + function prefill() { + const query = new URLSearchParams(location.search); + for (const key of ["q", "company", "place"]) { + if (query.get(key)) $(`#f-${key}`).value = query.get(key); + } + const agent = query.get("agent"); + if (agent && agents.some((a) => String(a.id) === agent)) { + $("#f-agent").value = agent; + $("#show-gated").hidden = false; + } + } + + async function initRoles() { + try { + await Promise.all([loadCompanies(), loadApplications(), loadAgents()]); + } catch (err) { + return; + } + renderAgentSelect(); + prefill(); + await loadRoles(); + + const refetch = debounce(loadRoles, 300); + for (const key of ["q", "company", "place"]) { + $(`#f-${key}`).addEventListener("input", refetch); + } + for (const sel of ["#f-function", "#f-remote", "#f-days", "#f-show-muted", "#f-gated"]) { + $(sel).addEventListener("change", loadRoles); + } + $("#f-agent").addEventListener("change", () => { + $("#show-gated").hidden = !$("#f-agent").value; + loadRoles(); + }); + $("#filters").addEventListener("submit", (e) => e.preventDefault()); + } + + // ------------------------------------------------------------ applications + + // `ROLEDESK_STATUSES` is set by applications.html from the same `STATUSES` + // tuple the store validates against, so this list can never drift from + // what a `POST .../status` will accept. + function statusSelect(current) { + const select = document.createElement("select"); + for (const status of window.ROLEDESK_STATUSES) { + const option = document.createElement("option"); + option.value = status; + option.textContent = status[0].toUpperCase() + status.slice(1); + option.selected = status === current; + select.appendChild(option); + } + return select; + } + + function renderApplications(applications) { + const body = $("#applications-list"); + body.replaceChildren(); + for (const application of applications) { + const tr = document.createElement("tr"); + if (application.url) { + const link = document.createElement("a"); + link.href = application.url; + link.target = "_blank"; + link.rel = "noopener"; + link.textContent = application.role_title || "(untitled)"; + cell(tr, "Role", link); + } else { + cell(tr, "Role", application.role_title || "(untitled)"); + } + const company = companies.get(application.company_key); + cell(tr, "Company", company ? company.name : application.company_key); + const select = statusSelect(application.status); + select.addEventListener("change", () => setStatus(application.id, select)); + cell(tr, "Status", select); + cell(tr, "Next", [application.next_action, application.next_date] + .filter(Boolean).join(" · ")); + body.appendChild(tr); + } + $("#applications-count").textContent = + count(applications.length, "tracked application"); + } + + async function setStatus(id, select) { + select.disabled = true; + try { + await api(`/api/applications/${encodeURIComponent(id)}/status`, { + method: "POST", body: { status: select.value }, + }); + renderApplications(await loadApplications()); + } catch (err) { + select.disabled = false; + } + } + + async function initApplications() { + try { + const [, applications] = await Promise.all([loadCompanies(), loadApplications()]); + renderApplications(applications); + } catch (err) { + /* already toasted */ + } + } + + // ------------------------------------------------------------------ agents + + function renderAgents() { + const list = $("#agents-list"); + list.replaceChildren(); + for (const agent of agents) { + const li = document.createElement("li"); + li.className = "agent"; + const name = document.createElement("a"); + name.href = `/?agent=${agent.id}`; + name.title = "Roles, ordered by this agent's score"; + name.textContent = agent.name; + const meta = document.createElement("span"); + meta.className = "agent-meta"; + const gates = agent.gates.map((g) => `${g.mode} ${g.field}`).join(", ") || "no gates"; + meta.textContent = `${agent.enabled ? "" : "off · "}${gates}`; + const edit = document.createElement("button"); + edit.type = "button"; + edit.className = "secondary"; + edit.textContent = "Edit"; + edit.addEventListener("click", () => fillAgentForm(agent)); + li.append(name, meta, edit); + list.appendChild(li); + } + $("#agents-count").textContent = count(agents.length, "agent"); + } + + function fillAgentForm(agent) { + $("#agent-form-title").textContent = agent ? `Edit ${agent.name}` : "New agent"; + $("#a-id").value = agent ? String(agent.id) : ""; + $("#a-name").value = agent ? agent.name : ""; + $("#a-intent").value = agent ? agent.intent : ""; + $("#a-gates").value = JSON.stringify(agent ? agent.gates : [], null, 2); + const weights = agent ? agent.weights : { fit: 0.6, recency: 0.2, affinity: 0.2 }; + $("#a-fit").value = weights.fit; + $("#a-recency").value = weights.recency; + $("#a-affinity").value = weights.affinity; + $("#a-enabled").checked = agent ? agent.enabled : true; + $("#a-delete").hidden = !agent; + $("#agent-status").textContent = ""; + } + + async function saveAgent(event) { + event.preventDefault(); + const status = $("#agent-status"); + let gates; + try { + gates = JSON.parse($("#a-gates").value || "[]"); + } catch (err) { + window.toast.error(`Gates are not valid JSON: ${err.message}`); + return; + } + const id = $("#a-id").value; + const body = { + name: $("#a-name").value.trim(), + intent: $("#a-intent").value, + gates, + weights: { + fit: Number($("#a-fit").value), + recency: Number($("#a-recency").value), + affinity: Number($("#a-affinity").value), + }, + enabled: $("#a-enabled").checked, + }; + if (id) body.id = Number(id); + let saved; + try { + saved = await api("/api/agents", { method: "POST", body }); + } catch (err) { + status.textContent = ""; + return; + } + try { + await loadAgents(); + renderAgents(); + } catch (err) { + /* already toasted; the save itself landed */ + } + fillAgentForm(saved); + await rescore(status, saved); + } + + async function deleteAgent() { + const id = $("#a-id").value; + if (!id || !confirm("Delete this agent and its scores?")) return; + try { + await api(`/api/agents/${encodeURIComponent(id)}`, { method: "DELETE" }); + fillAgentForm(null); + await loadAgents(); + renderAgents(); + } catch (err) { + /* already toasted */ + } + } + + // Start the `score` job and wait for it, so "Saved and scored" means the + // Roles tab already orders by what was saved. + async function rescore(status, agent) { + status.textContent = "Saved. Scoring…"; + let job; + try { + job = await api("/api/jobs", { method: "POST", body: { kind: "score", payload: {} } }); + } catch (err) { + status.textContent = "Saved, but scoring did not start."; + return; + } + for (let i = 0; i < 300; i += 1) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + let snapshot; + try { + snapshot = await api("/api/jobs"); + } catch (err) { + return; + } + const busy = [...snapshot.running, ...snapshot.queued].some((j) => j.id === job.id); + if (busy) continue; + const done = snapshot.recent.find((j) => j.id === job.id); + if (done && done.state !== "done") { + status.textContent = `Saved. Scoring ${done.state}.`; + if (done.error) window.toast.error(`Scoring ${done.state}: ${done.error}`); + return; + } + status.replaceChildren("Saved and scored. "); + const link = document.createElement("a"); + link.href = `/?agent=${agent.id}`; + link.textContent = "See the roles"; + status.appendChild(link); + return; + } + status.textContent = "Saved. Scoring is still running; see the Jobs tab."; + } + + async function initAgents() { + try { + await loadAgents(); + } catch (err) { + return; + } + renderAgents(); + $("#agent-form").addEventListener("submit", saveAgent); + $("#a-new").addEventListener("click", () => fillAgentForm(null)); + $("#a-delete").addEventListener("click", deleteAgent); + } + + // --------------------------------------------------------------- companies + + function renderCompanies() { + const q = $("#c-q").value.trim().toLowerCase(); + const affinity = $("#c-affinity").value; + const rows = [...companies.values()] + .filter((c) => !q || c.name.toLowerCase().includes(q) || c.board.includes(q)) + .filter((c) => affinity === "" || String(c.affinity) === affinity) + .sort((a, b) => b.open_roles - a.open_roles || a.name.localeCompare(b.name)); + const body = $("#companies-list"); + body.replaceChildren(); + for (const company of rows) { + const tr = document.createElement("tr"); + if (company.affinity === -1) tr.classList.add("muted"); + const link = document.createElement("a"); + link.href = `/?company=${encodeURIComponent(company.name)}`; + link.title = "This company's open roles"; + link.textContent = company.name; + cell(tr, "Company", link); + cell(tr, "Board", company.board || ""); + cell(tr, "Open roles", String(company.open_roles)).className = "num"; + cell(tr, "Affinity", + affinityControl(company.key, company.affinity, async () => renderCompanies())); + body.appendChild(tr); + } + const open = rows.reduce((sum, c) => sum + c.open_roles, 0); + $("#companies-count").textContent = + `${count(rows.length, "company", "companies")}, ${count(open, "open role")}`; + } + + async function initCompanies() { + try { + await loadCompanies(); + } catch (err) { + return; + } + renderCompanies(); + $("#c-q").addEventListener("input", debounce(renderCompanies, 150)); + $("#c-affinity").addEventListener("change", renderCompanies); + } + + // -------------------------------------------------------------------- init + + document.addEventListener("DOMContentLoaded", () => { + if ($("#roles-list")) initRoles(); + if ($("#applications-list")) initApplications(); + if ($("#agents-list")) initAgents(); + if ($("#companies-list")) initCompanies(); + }); +})(); diff --git a/app/static/toasts.js b/app/static/toasts.js new file mode 100644 index 0000000..ccacb8a --- /dev/null +++ b/app/static/toasts.js @@ -0,0 +1,226 @@ +/* Toasts: the one place on screen for messages to the user. + * + * Adapted from health's `webapp/static/toasts.js`, which is where the + * behaviour was worked out. Two sources, one look: + * + * server notices from the log (app/notices.py), delivered by live.js as + * `app:notice`. A problem (warning, error) interrupts the first + * time; a repeat within five minutes only bumps the count of a + * toast already on screen, since it is already in the log. An + * outcome (good, info) is news every time. On page load, notices + * raised since the last visit are shown too, so one raised while + * no tab was open is not lost. + * pages `toast.error(text)`, `toast.good(text)`, ... for the outcome of + * something the user just did. These are not logged: the user is + * looking at the result. + * + * Errors stay until dismissed; everything else fades. Progress of work in + * flight is not a toast -- that is what the job's progress bar is for. + */ + +(function () { + "use strict"; + + const FADE_MS = { info: 5000, good: 5000, warning: 10000, error: 0 }; + const SEEN_KEY = "app.notices.seen"; + const MAX_ON_LOAD = 3; + + let stack = null; + const byCode = new Map(); // "code|key" -> toast element, for server notices + + /* Escaping, once for the page. This file needs one and loads first, so it + is the one that exports it: every other script here builds a little HTML + out of server strings, and a notice's detail is raw error text that has + already been through somebody else's parser once. Two copies of an + escaper is one of them being the one that gets fixed. */ + function esc(s) { + return String(s === null || s === undefined ? "" : s).replace(/[&<>"']/g, (c) => ({ + "&": "&", "<": "<", ">": ">", '"': """, "'": "'", + })[c]); + } + window.esc = esc; + + /* The stack is a manual popover, so it lives in the browser's top layer and + shows above a modal . The top layer is ordered by opening, so it + is reopened on every new toast to stay above whatever opened since. */ + const POPOVER = typeof HTMLElement.prototype.showPopover === "function"; + + function container() { + if (!stack) { + stack = document.createElement("div"); + stack.className = "toasts"; + stack.setAttribute("aria-live", "polite"); + if (POPOVER) stack.setAttribute("popover", "manual"); + document.body.appendChild(stack); + } + return stack; + } + + /* A modal dialog makes the rest of the document inert, a popover opened + after it included: the toast would be visible but its buttons dead. So + while a modal is open the stack lives inside it, and goes back to the body + when the modal closes. */ + function raise() { + const modal = document.querySelector("dialog:modal"); + const home = modal || document.body; + if (stack.parentElement !== home) { + home.appendChild(stack); + if (modal) { + modal.addEventListener("close", () => { + if (stack.parentElement !== modal) return; + document.body.appendChild(stack); + if (stack.children.length) reopen(); + }, { once: true }); + } + } + reopen(); + } + + function reopen() { + if (!POPOVER) return; + if (stack.matches(":popover-open")) stack.hidePopover(); + stack.showPopover(); + } + + /* opts: level (info|good|warning|error), text, detail, fix, count, sticky, + action {label, run}, link {label, href}, id (dedupe key) */ + function show(opts) { + const o = typeof opts === "string" ? { text: opts } : opts; + const level = o.level || "info"; + if (o.id && byCode.has(o.id)) { + const existing = byCode.get(o.id); + if (existing.isConnected) { + update(existing, o); + return existing; + } + byCode.delete(o.id); + } + + const t = document.createElement("div"); + t.className = `toast ${level}`; + t.setAttribute("role", level === "error" ? "alert" : "status"); + t.innerHTML = ` +
+
+ ${o.fix ? `
${esc(o.fix)}
` : ""} + ${o.detail ? `
details +
${esc(o.detail)}
` : ""} +
+ ${o.action ? `` : ""} + ${o.link ? `${esc(o.link.label)}` : ""} +
+
+ `; + update(t, o); + t.addEventListener("click", (e) => { + const act = e.target.dataset && e.target.dataset.act; + if (act === "close") dismiss(t); + else if (act === "action" && o.action) { + o.action.run(); + dismiss(t); + } + }); + container().appendChild(t); + raise(); + if (o.id) byCode.set(o.id, t); + + const fade = o.sticky ? 0 : FADE_MS[level]; + if (fade) { + let timer = setTimeout(() => dismiss(t), fade); + // Reading it keeps it: a toast does not fade from under the cursor. + t.addEventListener("mouseenter", () => clearTimeout(timer)); + t.addEventListener("mouseleave", () => { timer = setTimeout(() => dismiss(t), fade); }); + } + return t; + } + + function update(t, o) { + const count = o.count > 1 ? ` x${o.count}` : ""; + t.querySelector(".toast-text").innerHTML = esc(o.text) + count; + } + + function dismiss(t) { + t.classList.add("out"); + setTimeout(() => { + t.remove(); + if (POPOVER && stack && !stack.children.length && stack.matches(":popover-open")) { + stack.hidePopover(); + } + }, 180); + } + + const toast = { + show, + info: (text, o) => show({ ...o, level: "info", text }), + good: (text, o) => show({ ...o, level: "good", text }), + warning: (text, o) => show({ ...o, level: "warning", text }), + error: (text, o) => show({ ...o, level: "error", text }), + }; + window.toast = toast; + + /* -- server notices ---------------------------------------------------- */ + + function seen() { + try { return localStorage.getItem(SEEN_KEY) || ""; } catch (e) { return ""; } + } + function markSeen(at) { + try { if (at > seen()) localStorage.setItem(SEEN_KEY, at); } catch (e) { /* convenience */ } + } + + const PROBLEM = new Set(["warning", "error"]); + + function fromNotice(n) { + const t = { + id: `${n.code}|${n.key || ""}`, + level: n.level, + text: n.text, + fix: n.fix, + detail: n.detail, + link: { label: "all messages", href: "/messages" }, + }; + if (PROBLEM.has(n.level)) t.count = n.count; + if (n.level === "good") t.action = { label: "reload", run: () => location.reload() }; + return t; + } + + /* When this page last showed each problem. A repeat within QUIET_MS only + bumps a count, so a burst -- a job retried in a loop -- is one toast; a + repeat after that, such as a retry the user just pressed, is shown again. */ + const QUIET_MS = 5 * 60 * 1000; + const shownAt = new Map(); + + function onNotice(n) { + const id = `${n.code}|${n.key || ""}`; + const onScreen = byCode.get(id); + markSeen(n.last_at || ""); + if (PROBLEM.has(n.level) && n.count > 1 && !(onScreen && onScreen.isConnected) + && Date.now() - (shownAt.get(id) || 0) < QUIET_MS) return; + shownAt.set(id, Date.now()); + show(fromNotice(n)); + } + + async function missed() { + // A browser that has never been here shows the last day's messages, not + // the whole fortnight the log keeps. + const since = seen() || new Date(Date.now() - 86400000).toISOString(); + let list; + try { + const r = await fetch("/api/notices", { headers: { Accept: "application/json" } }); + if (!r.ok) return; + list = await r.json(); + } catch (e) { return; } + const fresh = list.filter((n) => n.last_at > since); + fresh.slice(0, MAX_ON_LOAD).forEach((n) => show(fromNotice(n))); + if (fresh.length > MAX_ON_LOAD) { + toast.info(`${fresh.length - MAX_ON_LOAD} more messages since your last visit.`, + { link: { label: "all messages", href: "/messages" } }); + } + if (fresh.length) markSeen(fresh[0].last_at); + } + + document.addEventListener("app:notice", (e) => onNotice(e.detail)); + document.addEventListener("DOMContentLoaded", () => { + // Not on Messages: the table is right there, in full, already. + if (location.pathname !== "/login" && location.pathname !== "/messages") missed(); + }); +})(); diff --git a/app/templates/account.html b/app/templates/account.html new file mode 100644 index 0000000..b09d305 --- /dev/null +++ b/app/templates/account.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block title %}Account — roledesk{% endblock %} + +{% block content %} +

Account

+ +

One password, the sessions it has opened, and the passkeys registered beside it. There is no user to create and none to delete, because there is one. docs/password-protection.md is the reasoning behind every choice on this page; auth/README.md is how to take the folder into another project.

+ +
+ {# As on the Jobs and Schedules tabs: the element and its script belong to + the module, so this page is two lines and `auth/` owns what they draw. + + It says its own failures in place, beside the button that caused them, + and raises `auth-account:error` with the server's `detail` as well. No + toast is wired to that here on purpose: one failure said twice is worse + than one failure said once, and next to the form is where it belongs. #} + + +
+{% endblock %} diff --git a/app/templates/agents.html b/app/templates/agents.html new file mode 100644 index 0000000..9780dd2 --- /dev/null +++ b/app/templates/agents.html @@ -0,0 +1,37 @@ +{% extends "base.html" %} +{% block title %}Agents — roledesk{% endblock %} + +{% block content %} +

Search agents

+

A search agent is a named search: an intent that fit is measured against, gates that require or prefer a column value, and the weights of each term. Saving one rescores it; the Roles tab orders by it when it is picked there.

+ +

+
    + +
    +

    New agent

    + + + + +
    + Weights + + + +
    + +
    + + + +
    +

    +
    +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/app/templates/applications.html b/app/templates/applications.html new file mode 100644 index 0000000..9cad05a --- /dev/null +++ b/app/templates/applications.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% block title %}Applications — roledesk{% endblock %} + +{% block content %} +

    Applications

    +

    Every role saved from the Roles tab, and where it stands. A status change is recorded as an event, which is what days to first screen and conversion are computed from.

    + +

    + + + + + +
    RoleCompanyStatusNext
    +{% endblock %} + +{% block scripts %} + + +{% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html new file mode 100644 index 0000000..161af38 --- /dev/null +++ b/app/templates/base.html @@ -0,0 +1,35 @@ +{# The frame every page is in. The nav is `TABS` from app/web/pages.py and + nothing else: adding a tab is one entry in that list, never a link here. #} + + + + + + {% block title %}roledesk{% endblock %} + + + + +
    + roledesk + +
    + +
    + {% block content %}{% endblock %} +
    + +{# toasts first, and the order is load-bearing: it owns `window.toast`, which + app.js calls on a failed fetch and live.js feeds server notices into, and + `window.esc`, which every page script below builds its HTML with. #} + + + +{% block scripts %}{% endblock %} + + diff --git a/app/templates/companies.html b/app/templates/companies.html new file mode 100644 index 0000000..cd9a6ed --- /dev/null +++ b/app/templates/companies.html @@ -0,0 +1,30 @@ +{% extends "base.html" %} +{% block title %}Companies — roledesk{% endblock %} + +{% block content %} +

    Companies

    +

    Every company whose board is read, with how many of its roles are open. Affinity: Mute hides its roles and leaves them unscored, Neutral is the default, Like raises the affinity term, Favourite skips the gates and floors the score at 0.9.

    + +
    + + +
    + +

    + + + + + +
    CompanyBoardOpen rolesAffinity
    +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/app/templates/error.html b/app/templates/error.html new file mode 100644 index 0000000..a8cd81c --- /dev/null +++ b/app/templates/error.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% block title %}{{ status }} — roledesk{% endblock %} + +{% block content %} +

    {{ status }}

    + +{# The server's own sentence, the same one `/api` callers get as `detail`. + Two wordings for one failure is how a page ends up telling you something + the API does not. #} +

    {{ detail }}

    + +

    Back to the roles, or see what the server has been saying.

    +{% endblock %} diff --git a/app/templates/jobs.html b/app/templates/jobs.html new file mode 100644 index 0000000..5298c7a --- /dev/null +++ b/app/templates/jobs.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} +{% block title %}Jobs — roledesk{% endblock %} + +{% block content %} +

    Jobs

    + +

    pull runs the whole chain: every board, then extract, embed and score. extract, embed and score run one stage on their own. The buttons come from /api/jobs/kinds, so a new @job under app/jobs/ appears here without an edit.

    + +
    +

    Start something

    +
    + loading the job kinds… +
    +
    + +
    + {# The queue's own element, served by its own router. Two lines, and the + module owns what they draw — see pipeline/web/static/jobs.js. + + The heading is the app's, because the element takes up no space while + nothing is running — that is its `idle="hide"` default, and a strip that + is always there is furniture — and a card with nothing in it and nothing + above it reads as something that failed to load. #} +

    What is running

    +

    Nothing at the moment. Press one of the buttons above.

    + + +
    +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/app/templates/messages.html b/app/templates/messages.html new file mode 100644 index 0000000..4ec08dc --- /dev/null +++ b/app/templates/messages.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block title %}Messages — roledesk{% endblock %} + +{% block content %} +

    Messages

    + +

    Everything the server has said that nobody has cleared, newest first. A toast fades and is raised as often as not while no tab is open; this is where it is found again, with its fix, its raw error, and how many times it has been said. A row goes when you clear it, or a fortnight after it was last said.

    + +
    +
    +

    The log

    + +
    +
    loading…
    +
    +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/app/templates/roles.html b/app/templates/roles.html new file mode 100644 index 0000000..61e5373 --- /dev/null +++ b/app/templates/roles.html @@ -0,0 +1,67 @@ +{% extends "base.html" %} +{% block title %}Roles — roledesk{% endblock %} + +{# An empty shell: `static/roles.js` fills it and drives every action. The + server renders only the select options, from `app.models` and + `plugins.ats.FUNCTIONS`, so they cannot drift from what the store accepts. #} +{% block content %} +
    + + + + + + + + + +
    + +

    +
      + + +{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/app/templates/schedules.html b/app/templates/schedules.html new file mode 100644 index 0000000..2d20dd9 --- /dev/null +++ b/app/templates/schedules.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% block title %}Schedules — roledesk{% endblock %} + +{% block content %} +

      Schedules

      + +

      A schedule enqueues a job kind on a cadence: every 15m, every 6h, every 2d, daily at 06:00 or weekly mon 07:00, the clock ones read in timezone from config.yaml. The default, pull every 6h, is declared next to the pull job in app/jobs/cascade.py. It is a starting point: a cadence edited here, or a schedule switched off, survives restarts and deploys.

      + +
      + {# The element and its script are the scheduler's own, served by its own + router — see pipeline/web/static/schedules.js. Two lines, as with jobs. #} + + +
      +{% endblock %} diff --git a/app/web/__init__.py b/app/web/__init__.py index 5efb954..4b68540 100644 --- a/app/web/__init__.py +++ b/app/web/__init__.py @@ -1,7 +1,11 @@ -"""The web face. One door: `create_app` and `serve`.""" +"""The web face: the tabs, the JSON routes behind them, and the Jinja +environment. The queue brings its own routes and elements (`pipeline.web`), +auth the login page and `` (`auth`); `app.main` puts them +together. One door for all of it.""" from __future__ import annotations -from app.web.app import create_app, serve +from app.web.api import router as api_router +from app.web.pages import STATIC_DIR, TABS, router as pages_router, templates -__all__ = ["create_app", "serve"] +__all__ = ["STATIC_DIR", "TABS", "api_router", "pages_router", "templates"] diff --git a/app/web/api.py b/app/web/api.py index ede70f6..b73fb6a 100644 --- a/app/web/api.py +++ b/app/web/api.py @@ -1,7 +1,9 @@ -"""The JSON routes the page uses: roles, companies, applications, agents. +"""The JSON routes the tabs use: roles, companies, applications, agents. -`/api/jobs*`, `/_jobs/jobs.js` and `/ws/jobs` are `pipeline.web`'s, mounted by -`connect` in `app.py`. This is roledesk's own API, over the same store. +`/api/jobs*`, `/api/schedules*`, `/api/notices` and `/ws/jobs` are +`pipeline.web`'s and `/api/account*` is `auth`'s, both wired in `app/main.py`. +This is roledesk's own API, over the same store. Every refusal is +`{"detail": ""}`, which `api()` in `static/app.js` turns into a toast. """ from __future__ import annotations diff --git a/app/web/app.py b/app/web/app.py deleted file mode 100644 index 1f3df50..0000000 --- a/app/web/app.py +++ /dev/null @@ -1,44 +0,0 @@ -"""The application factory: the queue's routes and socket from -`pipeline.web.connect`, roledesk's own JSON API, and the one page.""" - -from __future__ import annotations - -from pathlib import Path - -from fastapi import FastAPI -from fastapi.staticfiles import StaticFiles - -from pipeline import Jobs -from pipeline.web import connect -from app import config as config_mod -from app import jobs -from app.storage import Store -from app.web.api import router as api_router -from app.web.pages import router as pages_router - -WEB = Path(__file__).resolve().parent - - -def create_app(config_path: str | None = None) -> FastAPI: - config = config_mod.load(config_path) - # `Store` first, so the file is at the current schema before the queue - # adds its own table to it. - store = Store(config.database) - jobs.seed_agents(config, store) - app = FastAPI(title="roledesk", docs_url="/api/docs") - connect(app, Jobs(config.database), config, store, - workers=config.workers.model_dump(), - bootstrap=jobs.BOOTSTRAP, bootstrap_arg=config.path or "") - app.state.config = config - app.state.store = store - app.include_router(api_router) - app.include_router(pages_router) - app.mount("/static", StaticFiles(directory=WEB / "static"), name="static") - return app - - -def serve(host: str, port: int, config_path: str | None) -> int: - import uvicorn - - uvicorn.run(create_app(config_path), host=host, port=port) - return 0 diff --git a/app/web/pages.py b/app/web/pages.py index a3e9bd7..6cce5e6 100644 --- a/app/web/pages.py +++ b/app/web/pages.py @@ -1,17 +1,24 @@ -"""The one page. Everything under `/api` and `/_jobs` is JSON; this route -hands the browser an empty shell — the filter bar, the section headers, the -` - - - - - - - - - - - - -

      -
        - - - - - - -
        -

        Jobs

        - - -
        - - - - - - - diff --git a/config.example.yaml b/config.example.yaml index a6a1914..549f463 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,10 +1,36 @@ -# roledesk configuration. `roles.yaml` is the one the app reads by default -# (or $ROLEDESK_CONFIG, or `app -c path`). Every key here is optional. +# roledesk configuration. The app reads --config, else $APP_CONFIG, else +# config.yaml beside the checkout (committed: these defaults plus the starter +# agents). Every value below is the default, so a copy with nothing changed +# behaves exactly like no file at all. An unknown key is an error, not a +# shrug; see app/config.py. -# The SQLite file: roles, companies, subscriptions, applications, and the -# job queue's own table. Not shared with newsdesk. +server: + # 0.0.0.0 in a container -- the proxy is what is in front of it. + host: 127.0.0.1 + port: 8790 + +# The SQLite file: roles, companies, subscriptions, applications, agents and +# scores, the job queue and its schedules, the notice log, and auth's +# sessions. Not shared with newsdesk. database: roledesk.db +# What a clock on screen means, and what `daily at 06:00` is 06:00 in. +timezone: Europe/Berlin + +log_level: INFO + +# One password on a public hostname; see auth/README.md and +# docs/password-protection.md. The password is never in this file. +auth: + enabled: auto # auto: open on loopback, locked on anything else + session_days: 30 + secure_cookie: auto # always, behind a TLS-terminating proxy + rp_id: "" # roledesk.example.com -- passkeys are bound to it + rp_name: app # what the authenticator shows you + origin: "" # https://roledesk.example.com + password_env: APP_PASSWORD + cookie: app_session # unique per app sharing a hostname + # Any .py file in these directories defining a desk `Source` subclass is a # plugin. A plugin imports `desk` only. plugin_paths: @@ -16,9 +42,10 @@ workers: http: 8 cpu: 1 -# Who answers when a job needs a model. See desk/tools/agent.py in ../news. +# Who answers when a job needs a model. See desk/tools/agent.py; needs the +# `agent` extra. agent: - backend: auto # auto | api | claude_code | off + backend: auto # auto | api | claude_code | off # Title and description in, gate columns out (app/jobs/extract.py). Off by # default: on the API backend every batch of 20 roles is a paid call. @@ -27,22 +54,18 @@ extract: model: claude-haiku-4-5-20251001 # A role and each agent's intent as vectors, for the fit term of a score -# (app/jobs/embed.py). Needs sentence-transformers; without it `embed` -# posts a notice and scores leave fit out. Changing the model re-embeds. +# (app/jobs/embed.py). Needs the `embeddings` extra; without it `embed` posts +# a notice and scores leave fit out. Changing the model re-embeds. embed: model: sentence-transformers/all-MiniLM-L6-v2 per_run: 50000 +# One block per plugin, keyed by the plugin's name. The ats plugin reads its +# boards from the subscriptions table, so it needs none. +sources: {} + # Starter search agents, imported once into an empty `agents` table. Each gate # is {field, values, mode: require|prefer, weight}; fields are place, country, # remote, seniority, function, salary_min, currency, permit, languages and # company. weights default to {fit: 0.6, recency: 0.2, affinity: 0.2}. -agents: - - name: Berlin backend - intent: Senior backend engineer, Python services and PostgreSQL. - gates: - - {field: place, values: [Berlin], mode: require} - - {field: remote, values: [hybrid, remote], mode: prefer, weight: 0.2} - -# One block per plugin. Empty until the ats plugin lands. -sources: {} +agents: [] diff --git a/roles.yaml b/config.yaml similarity index 77% rename from roles.yaml rename to config.yaml index 3505af9..48aebd9 100644 --- a/roles.yaml +++ b/config.yaml @@ -1,5 +1,13 @@ -# The default config. See config.example.yaml for what each key means. +# roledesk's default config, committed. config.example.yaml documents every +# key; anything left out here takes the default written there. +server: + host: 127.0.0.1 + port: 8790 database: roledesk.db +timezone: Europe/Berlin +auth: + rp_name: roledesk + cookie: roledesk_session plugin_paths: - plugins workers: @@ -9,11 +17,11 @@ workers: extract: enabled: false model: claude-haiku-4-5-20251001 -sources: {} embed: model: sentence-transformers/all-MiniLM-L6-v2 +sources: {} # Starter search agents, imported once into an empty table. Edit them on the -# page afterwards; this list is not read again. +# Agents tab afterwards; this list is not read again. agents: - name: Berlin backend intent: >- diff --git a/plugins/__init__.py b/plugins/__init__.py index db23568..5433535 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -1,4 +1,4 @@ -"""Drop-in plugins, discovered by `plugin_paths` in `roles.yaml`. +"""Drop-in plugins, discovered by `plugin_paths` in `config.yaml`. A plugin file imports `desk` only — see `tests/test_architecture.py`. This `__init__.py` makes the directory importable as `plugins.` for the rest diff --git a/pyproject.toml b/pyproject.toml index 1d982e5..ee3a65d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,8 @@ ignore = [ "app/models.py" = ["RUF012"] "desk/models.py" = ["RUF012"] "desk/sources/spec.py" = ["RUF012"] +# The boilerplate's own test, adapted here but kept in its style. +"tests/test_coupling.py" = ["SIM102"] # ------------------------------------------------------------ type checking diff --git a/tests/test_architecture.py b/tests/test_architecture.py index 249a5bd..24bbdc0 100644 --- a/tests/test_architecture.py +++ b/tests/test_architecture.py @@ -1,10 +1,12 @@ """The package boundaries, held as a test rather than as a convention. Adapted from news/tests/test_architecture.py. One tree of our own, `app`, -and two external ones, `desk` and `pipeline`, imported from ../news. +and three copied ones: `pipeline` and `auth` from the boilerplate, `desk` from +news. Which of those may import which is `tests/test_coupling.py`'s rule; this +file holds `app`'s own layers. 1. **Direction.** Within `app`, a package imports only from packages below - it in `LAYERS`. `desk` and `pipeline` may be imported by any of them. + it in `LAYERS`. `desk`, `pipeline` and `auth` may be imported by any of them. 2. **Doors.** From outside a package, only `.` is imported, and only names it lists in `__all__`. `desk.tools` has no door of its own, so there the door is the tool: `desk.tools.`, one level, no deeper. @@ -25,10 +27,11 @@ import pytest ROOT = Path(__file__).resolve().parents[1] -#: Bottom to top. `cli` is the entry point and may reach anything. -LAYERS = ["models", "config", "storage", "jobs", "web", "cli"] +#: Bottom to top. `main` assembles the app from everything below it; `cli` +#: and `__main__` are the entry points and may reach anything. +LAYERS = ["models", "config", "storage", "jobs", "web", "main", "cli", "__main__"] APP = "app" -EXTERNAL = {"desk", "pipeline"} +EXTERNAL = {"desk", "pipeline", "auth"} TREES = {APP} | EXTERNAL @@ -142,7 +145,9 @@ def test_the_rules_catch_what_they_are_for() -> None: assert plugin_violations("from desk.sources.base import Source") assert not plugin_violations("from desk.sources import Source, Item") - assert direction_violations("storage", "from app.web import create_app") + assert direction_violations("storage", "from app.web import TABS") + assert direction_violations("web", "from app.main import create_app") + assert not direction_violations("main", "from app.web import pages_router") assert direction_violations("storage", "from app import jobs") assert not direction_violations("jobs", "from app.storage import Store") assert not direction_violations("models", "from pipeline import job") diff --git a/tests/test_coupling.py b/tests/test_coupling.py index d6cc527..87b1c5c 100644 --- a/tests/test_coupling.py +++ b/tests/test_coupling.py @@ -1,10 +1,13 @@ """The coupling rule, held by reading the imports. -`pipeline/` and `auth/` import nothing from `app/` and nothing from each -other. `app/` imports both. That is what makes each of them copyable on its -own into a project that has never heard of this one -- and it is exactly the -rule that breaks quietly, one convenient import at a time, until the folder is -a port rather than a copy. +The boilerplate's test, with roledesk's two extra folders. `pipeline/`, +`auth/` and `desk/` import nothing from `app/` and nothing from each other, +except that `desk/` is built on `pipeline/`, as it is in news. `plugins/` +imports `desk/` and nothing else, since a plugin is app-agnostic. `app/` +imports all of them. That is what keeps each copied folder a copy that can be +refreshed from its source, and it is the rule that breaks quietly, one +convenient import at a time, until the folder is a port rather than a copy. +`tests/test_architecture.py` holds `app/`'s own layers. Read with `ast` rather than grepped: a string containing the word `app`, or a line in a docstring showing what an app would write, is not an import, and a @@ -21,10 +24,12 @@ import pytest ROOT = Path(__file__).resolve().parent.parent #: The module folders and what each may not reach for. Listed rather than -#: derived so that adding a fourth folder is a decision, not an accident. +#: derived so that adding a folder is a decision, not an accident. FORBIDDEN = { - "pipeline": ("app", "auth"), - "auth": ("app", "pipeline"), + "pipeline": ("app", "auth", "desk", "plugins"), + "auth": ("app", "pipeline", "desk", "plugins"), + "desk": ("app", "auth", "plugins"), + "plugins": ("app", "pipeline", "auth"), } diff --git a/tests/test_packaging.py b/tests/test_packaging.py index 7412e0d..2ab7b3c 100644 --- a/tests/test_packaging.py +++ b/tests/test_packaging.py @@ -41,7 +41,7 @@ def test_there_are_scripts_to_check(): def test_pyproject_packages_the_module_folders(): data = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8")) include = data["tool"]["setuptools"]["packages"]["find"]["include"] - for pattern in ("app*", "pipeline*", "auth*"): + for pattern in ("app*", "pipeline*", "auth*", "desk*"): assert pattern in include, f"{pattern} is missing: an install would leave it out" diff --git a/tests/test_score.py b/tests/test_score.py index c7da8ce..d75552c 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -17,7 +17,7 @@ if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from desk.models import Item # noqa: E402 -from pipeline import FINAL, TASKS, JobQueue, Jobs, RunContext # noqa: E402 +from pipeline import FINAL, SCHEDULES, TASKS, JobQueue, Jobs, RunContext # noqa: E402 from plugins.ats import ATSSource # noqa: E402 from app.config import AgentSeed, Config # noqa: E402 from app.jobs import cascade, embed, score # noqa: E402 @@ -266,13 +266,13 @@ def test_starter_agents_are_imported_once(store: Store, tmp_path: Path) -> None: def test_the_shipped_starter_agents_validate() -> None: from app import config as config_mod - names = [a.name for a in config_mod.load(ROOT / "roles.yaml").agents] + names = [a.name for a in config_mod.load(ROOT / "config.yaml").agents] assert names == ["Berlin backend", "Remote EU stretch"] # ---------------------------------------------------------------- cascade -def test_the_pull_cascade_runs_extract_embed_then_score( +def test_the_pull_job_runs_pull_extract_embed_then_score( store: Store, config: Config, fake: FakeEmbedder, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme", @@ -292,19 +292,34 @@ def test_the_pull_cascade_runs_extract_embed_then_score( queue = JobQueue(Jobs(tmp_path / "roledesk.db"), config, store, workers={"cpu": 1, "http": 1, "llm": 1}) - task_id = queue.start_task(TASKS["pull"]()) + # The `pull` job, which is what the schedule fires: its own work is to ask + # for the chain, which then runs to its end in the same drain. + started = queue.enqueue("pull") queue.drain(timeout=60) - cascade_id = queue.jobs.of_task(task_id)[0].cascade_id - jobs = sorted(queue.jobs.of_cascade(cascade_id), key=lambda j: j.id) + assert started is not None and queue.jobs.get(started.id).state == "done" # type: ignore[union-attr] + batch = queue.jobs.last("pull_batch") + assert batch is not None + jobs = sorted(queue.jobs.of_cascade(batch.cascade_id), key=lambda j: j.id) stages = [j.result.get("task") for j in jobs if j.kind == FINAL] assert stages == list(cascade.CASCADES["pull"]) work = [j.kind for j in jobs if j.kind != FINAL] - assert work == ["pull_batch", "embed", "score"], "extract is off, so it finds nothing" + # The `pull` job and the chain it asked for are one cascade, so the Jobs + # tab shows a scheduled pull as one run. Extract is off and finds nothing. + assert work == ["pull", "pull_batch", "embed", "score"] assert all(j.state == "done" for j in jobs) assert len(store.scores(agent.id)) == 1 +def test_pull_is_a_job_on_a_six_hour_schedule_and_the_stages_are_tasks() -> None: + """A schedule enqueues a job kind, so `pull` is a job; were it a task too, + the Jobs tab would draw two `pull` buttons.""" + assert "pull" not in TASKS + assert {"extract", "embed", "score"} <= set(TASKS) + assert SCHEDULES["pull"].kind == "pull" + assert SCHEDULES["pull"].cadence == "every 6h" + + def test_follows_walks_the_cascade_and_stops_at_its_end() -> None: assert cascade.follows("pull", "pull") == "extract" assert cascade.follows("pull", "extract") == "embed" diff --git a/tests/test_web.py b/tests/test_web.py index 5c7c7f3..bbffbd0 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1,4 +1,4 @@ -"""The JSON API the page calls, against a temp database and FastAPI's +"""The tabs and the JSON API they call, against a temp database and FastAPI's TestClient. No network — nothing here touches an ATS host or the queue's own worker beyond starting and stopping it with the app.""" @@ -12,9 +12,12 @@ import pytest from fastapi import FastAPI from fastapi.testclient import TestClient -from desk.models import Item +from app.config import Config, ServerConfig +from app.main import create_app from app.storage import Store -from app.web.app import create_app +from app.web import TABS +from auth import AuthConfig +from desk.models import Item class Clock: @@ -41,10 +44,10 @@ def posting(source_id: str = "1", **raw: object) -> Item: @pytest.fixture def app(tmp_path: Path) -> FastAPI: - db = tmp_path / "roledesk.db" - config_path = tmp_path / "roles.yaml" - config_path.write_text(f"database: {db.as_posix()}\n", encoding="utf-8") - return create_app(str(config_path)) + """The database in `tmp_path` and the config passed in, so a test never + touches `roledesk.db` and two tests never share a jobs table.""" + return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0), + database=tmp_path / "roledesk.db")) @pytest.fixture @@ -195,14 +198,82 @@ def test_set_status_validates_and_writes_an_event( json={"status": "applied"}).status_code == 404 -# --------------------------------------------------------------------- page +# -------------------------------------------------------------------- tabs -def test_index_renders_the_shell(client: TestClient) -> None: - res = client.get("/") - assert res.status_code == 200 - assert "roledesk" in res.text - assert 'id="roles-list"' in res.text - assert "" in res.text +def test_every_tab_renders_with_the_whole_nav(client: TestClient) -> None: + assert [t.label for t in TABS] == ["Roles", "Applications", "Agents", "Companies", + "Jobs", "Schedules", "Messages", "Account"] + for tab in TABS: + res = client.get(tab.path) + assert res.status_code == 200, f"{tab.path} answered {res.status_code}" + assert "roledesk" in res.text + for other in TABS: + assert f">{other.label}" in res.text + + +def test_each_roledesk_tab_is_a_shell_for_roles_js(client: TestClient) -> None: + for path, container in [("/", 'id="roles-list"'), + ("/applications", 'id="applications-list"'), + ("/agents", 'id="agents-list"'), + ("/companies", 'id="companies-list"')]: + page = client.get(path).text + assert container in page and "/static/roles.js" in page + # toasts.js before app.js before the page's own script: `api()` needs + # `window.toast`, and roles.js needs `api()`. + assert page.index("toasts.js") < page.index("app.js") < page.index("roles.js") + assert "ROLEDESK_STATUSES" in client.get("/applications").text + assert client.get("/static/roles.js").status_code == 200 + + +def test_the_modules_bring_their_own_elements(client: TestClient) -> None: + assert "" in client.get("/jobs").text + assert "" in client.get("/schedules").text + assert "" in client.get("/account").text + + +def test_pull_is_declared_every_six_hours(client: TestClient) -> None: + rows = {r["name"]: r for r in client.get("/api/schedules").json()} + assert rows["pull"]["kind"] == "pull" + assert rows["pull"]["cadence"] == "every 6h" + assert rows["pull"]["enabled"] is True and rows["pull"]["unknown"] is False + + +def test_the_jobs_tab_offers_pull_once_and_no_batch(client: TestClient) -> None: + """A subset rather than the list: `pipeline/tests` registers kinds of its + own into the same global `JOBS` when the whole suite runs.""" + kinds = [k["kind"] for k in client.get("/api/jobs/kinds").json()] + assert {"embed", "extract", "pull", "score"} <= set(kinds) + assert kinds.count("pull") == 1 + assert not {"pull_batch", "extract_batch", "advance"} & set(kinds) + + +def test_an_error_is_one_sentence(client: TestClient) -> None: + """`api()` toasts `detail` as it is, so a 422 is a sentence, not a list.""" + res = client.post("/api/agents", json={"name": "Bad", + "gates": [{"field": "salary", "values": [1]}]}) + assert res.status_code == 422 + assert "gates.0.field" in res.json()["detail"] + missing = client.get("/nothing-here") + assert missing.status_code == 404 and "text/html" in missing.headers["content-type"] + + +def test_health_is_open_and_reads_the_store(client: TestClient) -> None: + assert client.get("/health").json() == {"ok": True, "open_roles": 0} + + +def test_a_locked_app_shuts_every_tab_and_the_api( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("APP_PASSWORD", "open-sesame-please") + config = Config(server=ServerConfig(host="127.0.0.1", port=0), + database=tmp_path / "locked.db", auth=AuthConfig(enabled="always")) + with TestClient(create_app(config), follow_redirects=False) as locked: + for tab in TABS: + assert locked.get(tab.path).status_code == 303 + assert locked.get("/api/roles").status_code == 401 + assert locked.get("/health").status_code == 200 + signed_in = locked.post("/login", json={"password": "open-sesame-please"}) + assert signed_in.status_code == 200 + assert locked.get("/api/roles").status_code == 200 # ------------------------------------------------------------------ agents