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:
WolfandClaude Opus committed 2026-09-29 15:16:23 +02:00
1 parent 3abb44e10a
commit 91d15fcc7d
4 files changed
+110 -12

No files matched your search

+46 -1
View File
@@ -5,6 +5,7 @@ worker beyond starting and stopping it with the app."""
from __future__ import annotations
import json
import sqlite3
from collections.abc import Iterator
from datetime import UTC, datetime, timedelta
from pathlib import Path
@@ -18,6 +19,7 @@ from app.config import Config, ServerConfig
from app.main import create_app
from app.storage import Store
from app.web import MOVED, SETTINGS, TABS
from app.web.api import _score_after_change
from app.web.pages import SECTIONS
from auth import AuthConfig
from desk.models import Item
@@ -444,7 +446,7 @@ def test_set_affinity_validates_updates_and_enqueues_a_score(
assert res.json()["affinity"] == 2
assert store.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
assert store.company("acme", other.id).affinity == 0 # type: ignore[union-attr]
assert queue.jobs.last("score", states=("queued", "running", "done")) is not None
assert queue.jobs.get(res.json()["score_job"]).kind == "score"
res = client.post("/api/companies/acme/affinity", json={"affinity": -1, "agent": other.id})
assert res.status_code == 200 and res.json()["affinity"] == -1
@@ -458,6 +460,49 @@ def test_set_affinity_validates_updates_and_enqueues_a_score(
assert client.get("/api/companies", params={"agent": 999}).status_code == 404
def test_a_score_asked_for_during_a_run_is_one_follow_up_after_it(queue: Any) -> None:
"""No worker runs here (no client), so the test moves the jobs itself."""
first = _score_after_change(queue)
assert first is not None and first.dedup_key == "score"
assert _score_after_change(queue).id == first.id # queued: it will read the change
assert queue.jobs.claim({"cpu": 1}).id == first.id
during = {_score_after_change(queue).id for _ in range(10)}
assert len(during) == 1 and first.id not in during
follow = queue.jobs.get(during.pop())
assert (follow.depends_on, follow.after, follow.dedup_key) == (
[first.id], "done", f"score:after:{first.id}")
queue.jobs.finish(first.id, "done")
assert _score_after_change(queue).id == follow.id # queued, not started: still the one
assert queue.jobs.claim({"cpu": 1}).id == follow.id
after = _score_after_change(queue)
assert after.id not in (first.id, follow.id) and after.dedup_key == "score"
def test_score_after_change_retries_the_queues_dedup_race(
queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
"""`Jobs.enqueue` re-raises the UNIQUE failure when the live job it
collided with finishes before it reads it back."""
real, calls = queue.enqueue, []
def racy(*args: Any, **kwargs: Any) -> Any:
calls.append(args)
if len(calls) == 1:
raise sqlite3.IntegrityError("UNIQUE constraint failed: jobs.dedup_key")
return real(*args, **kwargs)
monkeypatch.setattr(queue, "enqueue", racy)
assert _score_after_change(queue) is not None and len(calls) == 2
def test_post_score_answers_with_the_job_to_wait_for(client: TestClient, queue: Any) -> None:
res = client.post("/api/score")
assert res.status_code == 200
assert queue.jobs.get(res.json()["id"]).kind == "score"
# ----------------------------------------------------------- applications
def test_save_creates_an_application_with_a_saved_event(