Merge branch 'r2-backend'

# Conflicts:
#	app/storage/schema.py
This commit is contained in:
Wolf committed 2026-09-29 15:22:03 +02:00
commit 562fa3ab09
15 files changed
+924 -157

No files matched your search

+70 -10
View File
@@ -9,6 +9,7 @@ This is roledesk's own API, over the same store. Every refusal is
from __future__ import annotations
import sqlite3
from typing import Annotated, Any, Literal
from fastapi import APIRouter, HTTPException, Query, Response
@@ -270,7 +271,10 @@ class CompanyOut(BaseModel):
no `board` and an `ats` is one the app cannot read yet; `careers_url` is
where a person can. `failing_since` and `failing_reason` say since when
the board has failed on every fetch and why it last did, both empty
while it answers."""
while it answers. `from_search` marks a row that exists only because a
search listed its roles and somebody judged it (`Store.set_affinity`):
no board, not even a detached one, and no `ats`, `careers_url` or
`domain`, which every way of following a company writes one of."""
key: str
name: str
@@ -285,6 +289,7 @@ class CompanyOut(BaseModel):
country: str = ""
failing_since: str = ""
failing_reason: str = ""
from_search: bool = False
def _company_boards(store: Store) -> dict[str, tuple[int, str, bool, str]]:
@@ -307,6 +312,7 @@ def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut
"""Every company, with `affinity` as `agent` (the default agent when
absent) has it; 0 throughout when there is no agent."""
boards = _company_boards(store)
ever_boarded = {company_key(s.company) for s in store.subscriptions(source="ats")}
counts = store.open_role_counts()
failing = jobs.pull.failing_boards(store)
out = []
@@ -318,7 +324,9 @@ def list_companies(store: StoreDep, agent: int | None = None) -> list[CompanyOut
open_roles=counts.get(c.key, 0), ats=c.ats,
ats_slug=c.ats_slug, careers_url=c.careers_url,
country=c.country,
failing_since=since, failing_reason=reason))
failing_since=since, failing_reason=reason,
from_search=not (c.key in ever_boarded or c.ats
or c.careers_url or c.domain)))
return out
@@ -352,18 +360,70 @@ def detach_company_board(key: str, store: StoreDep) -> dict[str, Any]:
return {"closed": closed}
def _score_after_change(queue: Any) -> Job | None:
"""The `score` job whose result will include a change just written.
A `score` that is queued and has not started will read the change, so
it is the answer: the plain one, keyed `score`, or a follow-up. Else a
plain request, which the queue dedupes to a live job keyed `score`
because the kind is `solo`. When that job is already running it read
the old state, so it gets one follow-up that waits for it (`depends_on`,
`after="done"`), deduped on `score:after:<id>`: ten requests during one
run are one extra pass. A pull's `score` stage is a plain `score` job
too, queued when its stage starts, so a request during a pull joins it.
The retry is for a race in `pipeline.table.Jobs.enqueue`: when the
INSERT collides with a live job that finishes before the SELECT after
it, the SELECT finds nothing and the `IntegrityError` is re-raised. That
job has finished by then, so a second try succeeds."""
for attempt in range(3):
try:
queued = queue.jobs.last("score", states=("queued",))
if queued is not None and (queued.dedup_key == "score"
or queued.dedup_key.startswith("score:after:")):
return queued
job = queue.enqueue("score")
if job is not None and job.state == "running":
job = queue.enqueue("score", depends_on=[job.id], after="done",
dedup_key=f"score:after:{job.id}")
return job
except sqlite3.IntegrityError:
if attempt == 2:
raise
return None
@router.post("/score")
def request_score(queue: QueueDep) -> Job:
"""Score every agent's roles after a change: now, or right after the
`score` that is already running (see `_score_after_change`). Returns the
job to wait for; the Search agents page waits on it after a save."""
job = _score_after_change(queue)
if job is None:
raise HTTPException(409, "Scoring would not start.")
return job
class AffinityBody(BaseModel):
affinity: int
#: Whose affinity; the default agent when absent.
agent: int | None = None
class AffinityOut(Company):
#: The `score` job whose result will include this change; None when none
#: could be queued.
score_job: int | None = None
@router.post("/companies/{key}/affinity")
def set_affinity(key: str, body: AffinityBody, store: StoreDep, queue: QueueDep) -> Company:
def set_affinity(key: str, body: AffinityBody, store: StoreDep,
queue: QueueDep) -> AffinityOut:
"""-1 hidden, 0 neutral, 1 liked, 2 favourite, for `body.agent` or the
default agent. A change here is not seen in Best match until `score` has
run: the queue dedupes to a live job of that kind, so several clicks are
one run."""
default agent. A key with roles and no company row gets one first (see
`Store.set_affinity`); 404 for a key with neither. A change here is not
seen in Best match until `score` has run, so it queues one
(`_score_after_change`) and answers with its id."""
if body.affinity not in AFFINITY:
raise HTTPException(422, "affinity is -1, 0, 1 or 2")
owner = _agent_or_default(store, body.agent)
@@ -371,8 +431,8 @@ def set_affinity(key: str, body: AffinityBody, store: StoreDep, queue: QueueDep)
company = store.set_affinity(owner.id, key, body.affinity)
except KeyError:
raise HTTPException(404, f"no such company: {key}") from None
queue.enqueue("score")
return company
job = _score_after_change(queue)
return AffinityOut(**company.model_dump(), score_job=job.id if job else None)
# ----------------------------------------------------------------- sources
@@ -592,8 +652,8 @@ class AgentBody(BaseModel):
@router.post("/agents")
def save_agent(body: AgentBody, store: StoreDep) -> Agent:
"""Scores are not recomputed here: that is the `score` task, which the
page starts after a save."""
"""Scores are not recomputed here: the page asks `POST /api/score` after
a save and waits on the job it answers with."""
try:
return store.save_agent(body.name, body.intent, body.gates, body.weights,
body.enabled, agent_id=body.id, limits=body.limits)