A change made while score runs gets one follow-up score, and the page waits on it
score is solo, so a request during a run was deduped into the running job, which had already read the old state. _score_after_change returns a queued score when one has not started, else enqueues a plain score and, if that dedupes to a running job, one follow-up with depends_on it, after="done" and dedup_key score:after:<id>. POST /api/score answers with that job and agents.js waits on it; the affinity route answers with score_job. The flaky affinity test was pipeline's race, not the app's: Jobs.enqueue re-raises the UNIQUE IntegrityError when the live job it collided with finishes before the SELECT that should hand it back. The app retries. Co-Authored-By: Claude Opus <noreply@anthropic.com>
This commit is contained in:
1 parent
3abb44e10a
commit
91d15fcc7d
4 files changed
+110
-12
No files matched your search
@@ -289,14 +289,16 @@
|
||||
render();
|
||||
}
|
||||
|
||||
// Start the `score` job and wait for it, so "scored" means the Roles tab
|
||||
// already orders by what was saved. `said` is what happened first.
|
||||
// Ask for a `score` and wait for the job the server answers with, so
|
||||
// "scored" means the Roles tab already orders by what was saved. While a
|
||||
// score is running that is a follow-up queued after it, since the running
|
||||
// one read the agent before the save. `said` is what happened first.
|
||||
async function rescore(status, agent, said) {
|
||||
const runs = () => link("/settings/sources#runs", "follow it under Runs");
|
||||
status.replaceChildren(`${said}. Scoring every open role; `, runs(), ".");
|
||||
let job;
|
||||
try {
|
||||
job = await api("/api/jobs", { method: "POST", body: { kind: "score", payload: {} } });
|
||||
job = await api("/api/score", { method: "POST" });
|
||||
} catch (err) {
|
||||
status.textContent = `${said}, but scoring did not start.`;
|
||||
return;
|
||||
|
||||
+58
-7
@@ -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
|
||||
@@ -349,18 +350,68 @@ 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."""
|
||||
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)
|
||||
@@ -368,8 +419,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
|
||||
@@ -589,8 +640,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)
|
||||
|
||||
Reference in new issue
Block a user