Per-agent ranking: votes, affinity and a taste term; settings are three sub-tabs

Votes on roles and company affinity move to role_votes and agent_companies,
one row per search agent, so two agents rank the same roles differently. The
score gains taste, the cosine to mean(liked) - mean(passed) of the agent's
votes, as newsdesk ranks articles. The API takes agent on roles, votes and
companies and falls back to the default agent, the enabled one with the
lowest id.

Settings become Search agents (a list again, with New and Delete), Sources
(job boards, company boards and runs with the message log, one section per
plugin) and Account. Old paths answer 301 to their section.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-28 12:53:41 +02:00
1 parent 515d2250fc
commit a0772519b3
28 files changed
+1461 -668

No files matched your search

+68 -24
View File
@@ -28,16 +28,42 @@ from pipeline.web import QueueDep
router = APIRouter(prefix="/api", tags=["roledesk"])
# ------------------------------------------------------------------ agent
def _agent_id(store: Store, agent: int | None) -> int | None:
"""The agent a request means: the one named (404 when unknown), else the
default (`Store.default_agent`), else None when there is no agent at all."""
if agent is not None:
if store.agent(agent) is None:
raise HTTPException(404, f"no such agent: {agent}")
return agent
found = store.default_agent()
return found.id if found else None
def _agent_or_default(store: Store, agent: int | None) -> Agent:
"""`_agent_id` for a write, which has to land somewhere: 422 when no
agent exists yet."""
agent_id = _agent_id(store, agent)
found = store.agent(agent_id) if agent_id is not None else None
if found is None:
raise HTTPException(422, "No search agent exists yet. Add one under Settings.")
return found
# ------------------------------------------------------------------- roles
class VoteBody(BaseModel):
value: Literal[-1, 0, 1]
#: Whose vote; the default agent when absent.
agent: int | None = None
@router.get("/roles/{uid}")
def get_role(uid: str, store: StoreDep) -> Role:
"""One role, open or closed, for the read page."""
role = store.role(uid)
def get_role(uid: str, store: StoreDep, agent: int | None = None) -> Role:
"""One role, open or closed, for the read page, with `agent`'s vote (the
default agent's when absent; None when there is no agent)."""
role = store.role(uid, _agent_id(store, agent))
if role is None:
raise HTTPException(404, f"no such role: {uid}")
return role
@@ -45,19 +71,23 @@ def get_role(uid: str, store: StoreDep) -> Role:
@router.post("/roles/{uid}/vote")
def vote_role(uid: str, body: VoteBody, store: StoreDep) -> Role:
"""1 like, 0 neither way, -1 pass. The role stays listed either way; the
vote moves it in a ranking, see `Store.roles`."""
"""1 like, 0 neither way, -1 pass, for `body.agent` or the default agent.
The role stays listed either way; the vote moves it in that agent's
ranking, see `Store.roles`, and shapes its taste at the next `score`."""
owner = _agent_or_default(store, body.agent)
try:
return store.set_vote(uid, body.value)
return store.set_vote(owner.id, uid, body.value)
except KeyError:
raise HTTPException(404, f"no such role: {uid}") from None
@router.delete("/roles/{uid}/vote")
def unvote_role(uid: str, store: StoreDep) -> Role:
"""Unjudged again: what pressing the stance a role already has does."""
def unvote_role(uid: str, store: StoreDep, agent: int | None = None) -> Role:
"""Unjudged again for `agent` or the default agent: what pressing the
stance a role already has does."""
owner = _agent_or_default(store, agent)
try:
return store.set_vote(uid, None)
return store.set_vote(owner.id, uid, None)
except KeyError:
raise HTTPException(404, f"no such role: {uid}") from None
@@ -80,25 +110,31 @@ def list_roles(
days: int | None = None,
show_muted: bool = False,
agent: int | None = None,
order: Literal["score", "newest"] = "score",
gated: bool = False,
judged: Literal["", "liked", "unjudged", "passed"] = "",
offset: Annotated[int, Query(ge=0)] = 0,
limit: Annotated[int, Query(ge=1, le=1000)] = 60,
) -> RolesPage:
"""Open roles, newest first, `limit` of them from `offset`, with the
total. See `Store.roles` for what each filter does.
"""Open roles, `limit` of them from `offset`, with the total. See
`Store.roles` for what each filter does.
A muted company's roles are left out unless `show_muted` is set. With
`agent`, ordered by that agent's score moved by the role's vote, and
carrying `score` and `gated_reason`; a gated role only with `gated=1`.
`judged` keeps the liked, the unjudged or the passed."""
if agent is not None and store.agent(agent) is None:
raise HTTPException(404, f"no such agent: {agent}")
`agent` says whose votes, hidden companies and scores are meant. Each
role carries that agent's `vote`; a company it hid is left out unless
`show_muted` is set; `judged` keeps the liked, the unjudged or the
passed. With `order=score` (the default) the roles are the ones that
agent scored, best first, carrying `score` and `gated_reason`, a gated
one only with `gated=1`; with `order=newest` every open role, newest
first, with no score. Without `agent` the default agent's votes and
hidden companies apply, the order is newest and there is no score; with
no agent at all, every vote is None and nothing is hidden."""
agent_id = _agent_id(store, agent)
by_score = agent is not None and order == "score"
filters: dict[str, Any] = {
"q": q or None, "company": company or None, "place": place or None,
"function": function or None, "remote": remote or None, "days": days,
"hide_muted": not show_muted, "agent": agent, "gated": gated,
"judged": judged or None}
"hide_muted": not show_muted, "agent": agent_id, "by_score": by_score,
"gated": gated, "judged": judged or None}
return RolesPage(items=store.roles(limit=limit, offset=offset, **filters),
total=store.count_roles(**filters))
@@ -200,11 +236,13 @@ def _company_boards(store: Store) -> dict[str, tuple[int, str, bool]]:
@router.get("/companies")
def list_companies(store: StoreDep) -> list[CompanyOut]:
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)
counts = store.open_role_counts()
out = []
for c in store.companies():
for c in store.companies(_agent_id(store, agent)):
sub_id, host, on = boards.get(c.key, (None, "", None))
out.append(CompanyOut(key=c.key, name=c.name, board=host,
subscription_id=sub_id, on=on, affinity=c.affinity,
@@ -214,16 +252,21 @@ def list_companies(store: StoreDep) -> list[CompanyOut]:
class AffinityBody(BaseModel):
affinity: int
#: Whose affinity; the default agent when absent.
agent: int | None = None
@router.post("/companies/{key}/affinity")
def set_affinity(key: str, body: AffinityBody, store: StoreDep, queue: QueueDep) -> Company:
"""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."""
"""-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."""
if body.affinity not in AFFINITY:
raise HTTPException(422, "affinity is -1, 0, 1 or 2")
owner = _agent_or_default(store, body.agent)
try:
company = store.update_company(key, affinity=body.affinity)
company = store.set_affinity(owner.id, key, body.affinity)
except KeyError:
raise HTTPException(404, f"no such company: {key}") from None
queue.enqueue("score")
@@ -482,6 +525,7 @@ def save_agent(body: AgentBody, store: StoreDep) -> Agent:
@router.delete("/agents/{agent_id}", status_code=204)
def delete_agent(agent_id: int, store: StoreDep) -> Response:
"""Its scores, votes and affinities go with it."""
if not store.delete_agent(agent_id):
raise HTTPException(404, f"no such agent: {agent_id}")
return Response(status_code=204)