Guard each target in a batch, close remote-unasked and narrowed roles by age, settle failing searches, and raise one notice per settle naming what needs a look

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-30 15:53:47 +02:00
1 parent 24a165818c
commit 9c8a610787
4 files changed
+576 -115

No files matched your search

+231 -6
View File
@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import sys
import threading
import types
from concurrent.futures import ThreadPoolExecutor
from datetime import UTC, datetime, timedelta
from pathlib import Path
@@ -21,8 +22,8 @@ if str(ROOT) not in sys.path:
from desk.models import Item # noqa: E402
from pipeline import Cancelled, RunContext, Tally # noqa: E402
from app.models import Application # noqa: E402
from plugins import readers # noqa: E402
from plugins.readers import _runner # noqa: E402
from plugins import ats, readers # noqa: E402
from plugins.readers import Filters, Reads, _runner # noqa: E402
from plugins.ats import ATSSource # noqa: E402
from app import prep # noqa: E402
from app.jobs import cascade, pull # noqa: E402
@@ -142,7 +143,7 @@ def test_close_boards_closes_what_a_completed_board_no_longer_lists(
result = pull.close_boards(FakeConfig(), store, FakeTally()) # type: ignore[arg-type]
assert result == {"boards": 1, "searches": 0, "failed": 1, "incomplete": 0,
assert result == {"boards": 1, "searches": 0, "unanswered": 1, "incomplete": 0,
"roles": 1, "closed": 1}
assert {r.source_id for r in store.roles()} == {"greenhouse:acme:1", "greenhouse:other:9"}
@@ -163,7 +164,7 @@ def test_close_boards_never_closes_a_board_stamped_by_an_earlier_run(
result = pull.close_boards(FakeConfig(), store, FakeTally()) # type: ignore[arg-type]
assert result == {"boards": 0, "searches": 0, "failed": 1, "incomplete": 0,
assert result == {"boards": 0, "searches": 0, "unanswered": 1, "incomplete": 0,
"roles": 0, "closed": 0}
assert {r.source_id for r in store.roles()} == {"greenhouse:acme:1"}
@@ -256,7 +257,7 @@ def test_a_failing_board_keeps_its_roles_before_stale_days(
_answer(monkeypatch, ValueError("gone"))
result = _pull(acme)
assert _open(acme) == {"workable:acme:1"}
assert (result["failed"], result["closed"]) == (1, 0)
assert (result["unanswered"], result["closed"]) == (1, 0)
since, reason = pull.failing_boards(acme)["workable:acme"]
assert since and reason == "ValueError: gone"
@@ -486,7 +487,7 @@ def test_run_a_settle_does_not_close_on_run_b_ids(
b_result = pull.run_batch(FakeConfig(), acme, **b) # type: ignore[arg-type]
result = pull.close_boards(FakeConfig(), acme, Tally(results=(a_result,))) # type: ignore[arg-type]
assert (result["failed"], result["closed"]) == (1, 0)
assert (result["unanswered"], result["closed"]) == (1, 0)
assert _open(acme) == {"workable:acme:1", "workable:acme:2"}
assert pull.running(acme) == b["run"]
@@ -507,3 +508,227 @@ def test_the_settle_reads_its_marker_off_its_batches_payloads(acme: Store) -> No
context = RunContext(waited_for=(failed,))
result = pull.close_boards(FakeConfig(), acme, Tally(), context=context) # type: ignore[arg-type]
assert result["boards"] == 1 and pull.running(acme) == ""
# -------------------------------------------------------- losses, guarded
def _days_ago(days: float) -> str:
return (datetime.now(UTC) - timedelta(days=days)).isoformat(timespec="seconds")
def _set(store: Store, column: str, value: str, *source_ids: str) -> None:
with store.transaction() as conn:
conn.executemany(f"UPDATE roles SET {column} = ? WHERE source_id = ?",
[(value, i) for i in source_ids])
@pytest.fixture
def big(store: Store) -> Store:
store.add_subscription("ats", "greenhouse:big", label="Big", company="Big",
board="greenhouse")
return store
def test_a_whole_read_of_nothing_after_forty_keeps_the_roles_and_fails(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(40)], True))
_pull(big)
_answer(monkeypatch, ([], True))
result = _pull(big)
assert len(_open(big)) == 40
assert (result["unanswered"], result["closed"]) == (1, 0)
assert "under 10% of the 40" in pull.failing_boards(big)["greenhouse:big"][1]
def test_a_whole_read_of_nothing_after_five_closes_the_board(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(5)], True))
_pull(big)
_answer(monkeypatch, ([], True))
assert _pull(big)["closed"] == 5
assert _open(big) == set()
def test_a_suspect_answer_is_accepted_after_stale_days(
big: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(str(n)) for n in range(40)], True))
_pull(big)
_answer(monkeypatch, ([_row("1")], True))
_pull(big)
assert len(_open(big)) == 40
record = json.loads(big.state[f"{pull.FAILING}greenhouse:big"])
big.state[f"{pull.FAILING}greenhouse:big"] = json.dumps(
record | {"since": _days_ago(pull.STALE_DAYS + 1)})
result = _pull(big)
assert _open(big) == {"greenhouse:big:1"}
assert (result["unanswered"], result["closed"]) == (0, 39)
assert pull.failing_boards(big) == {}
assert json.loads(big.state["ats:count:greenhouse:big"])["count"] == 1
def _remote_host(monkeypatch: pytest.MonkeyPatch, remote: str) -> list[str]:
"""A whole-listing host `fake` that filters by country and, per `remote`,
on remote; its feed lists the ids in the returned list."""
listed: list[str] = []
module: object = types.ModuleType("fake")
module.NAME, module.LABEL, module.PATTERNS = "fake", "Fake", () # type: ignore[attr-defined]
module.slug_ok = lambda slug: slug.isalnum() # type: ignore[attr-defined]
module.allowed = lambda slug: lambda h: h == "jobs.fake.example" # type: ignore[attr-defined]
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}" # type: ignore[attr-defined]
module.READS = Reads( # type: ignore[attr-defined]
scope="board", listing="whole", text="listing", complete="whole", places="one",
robots="test", filters=Filters(country="many", remote=remote)) # type: ignore[arg-type]
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}" # type: ignore[attr-defined]
module.parse = lambda body, slug: [_row(i) for i in json.loads(body)] # type: ignore[attr-defined]
monkeypatch.setitem(readers.HOSTS, "fake", module)
monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: json.dumps(
listed).encode())
return listed
def test_a_filtered_read_that_could_not_ask_remote_keeps_remote_roles_until_stale(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
listed = _remote_host(monkeypatch, remote="none")
store.add_subscription("ats", "fake:acme", label="Acme", company="Acme", board="fake")
reach = {"fake:acme": {"countries": ["DK"], "remote": True}}
def pull_once() -> dict[str, object]:
run = pull.claim_run(store)
result = pull.run_batch(FakeConfig(), store, targets=["fake:acme"], run=run, # type: ignore[arg-type]
reach=reach)
return pull.close_boards(FakeConfig(), store, Tally(results=(result,))) # type: ignore[arg-type]
listed[:] = ["1", "2", "3", "4"]
pull_once()
_set(store, "remote", "remote", "fake:acme:3", "fake:acme:4")
listed[:] = ["1"]
assert pull_once()["closed"] == 1
assert _open(store) == {"fake:acme:1", "fake:acme:3", "fake:acme:4"}
assert store.state["ats:remote_unasked:fake:acme"] == "1"
_set(store, "last_seen", _days_ago(pull.STALE_DAYS + 1), "fake:acme:4")
assert pull_once()["closed"] == 1
assert _open(store) == {"fake:acme:1", "fake:acme:3"}
def test_a_narrowed_read_closes_what_it_has_not_listed_for_stale_days(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(i) for i in "123"], True))
_pull(acme)
def fake(*args: object, **kwargs: object) -> _runner.Listing:
return _runner.Listing([_row("1")], True, ["1"], narrowed=True)
monkeypatch.setattr(_runner, "read_listing", fake)
assert _pull(acme)["closed"] == 0
_set(acme, "last_seen", _days_ago(pull.STALE_DAYS + 1), "workable:acme:3")
assert _pull(acme)["closed"] == 1
assert _open(acme) == {"workable:acme:1", "workable:acme:2"}
def test_a_target_that_raises_after_its_read_is_recorded_and_the_batch_goes_on(
store: Store, monkeypatch: pytest.MonkeyPatch) -> None:
targets = [f"greenhouse:{slug}" for slug in ("a", "b", "c")]
for target in targets:
slug = target.partition(":")[2]
store.add_subscription("ats", target, label=slug, company=slug, board="greenhouse")
def fake_fetch(self: ATSSource) -> object:
[(target, _)] = self.targets()
yield _item("greenhouse", target.partition(":")[2], "1")
self.state[f"ats:ok:{target}"] = self.options["run"]
real = Store.upsert_role
def upsert(self: Store, item: Item, sub: object = None) -> bool:
if item.source_id.startswith("greenhouse:b:"):
raise ValueError("a row the store refuses")
return real(self, item, sub) # type: ignore[arg-type]
monkeypatch.setattr(ATSSource, "fetch", fake_fetch)
monkeypatch.setattr(Store, "upsert_role", upsert)
result = pull.run_batch(FakeConfig(), store, targets=targets, run="r") # type: ignore[arg-type]
assert result["roles"] == 2
assert _open(store) == {"greenhouse:a:1", "greenhouse:c:1"}
assert "ats:ok:greenhouse:b" not in store.state
since, reason = pull.failing_boards(store)["greenhouse:b"]
assert since and reason == "ValueError: a row the store refuses"
assert set(pull.failing_boards(store)) == {"greenhouse:b"}
def test_a_board_whose_rows_do_not_store_keeps_its_failing_record(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
since = _days_ago(2)
acme.state[f"{pull.FAILING}workable:acme"] = json.dumps(
{"since": since, "reason": "404 Not Found"})
_answer(monkeypatch, ([_row("1")], True))
def refuse(self: Store, item: Item, sub: object = None) -> bool:
raise ValueError("no")
monkeypatch.setattr(Store, "upsert_role", refuse)
_pull(acme)
assert pull.failing_boards(acme)["workable:acme"] == (since, "ValueError: no")
assert "ats:ok:workable:acme" not in acme.state
# ------------------------------------------------------- the settle speaks
def test_a_settle_names_the_unanswered_board_and_the_one_that_closed_many(
store: Store) -> None:
sub = store.add_subscription("ats", "greenhouse:big", label="Big", company="Big",
board="greenhouse")
store.add_subscription("ats", "greenhouse:gone", label="Gone", company="Gone",
board="greenhouse")
for n in range(80):
store.upsert_role(_item("greenhouse", "big", str(n), company="Big"), sub)
run = pull.claim_run(store)
store.state["ats:ok:greenhouse:big"] = run
store.state["ats:complete:greenhouse:big"] = "1"
store.state["ats:ids:greenhouse:big"] = json.dumps(
[f"greenhouse:big:{n}" for n in range(20)])
store.state[f"{pull.FAILING}greenhouse:gone"] = json.dumps(
{"since": _days_ago(1), "reason": "404 Not Found"})
said: list[object] = []
result = pull.close_boards(FakeConfig(), store, Tally(results=({"roles": 20, "run": run},)), # type: ignore[arg-type]
context=RunContext(on_notice=said.append))
assert (result["unanswered"], result["closed"]) == (1, 60)
[notice] = said
assert notice.code == "pull.settled" # type: ignore[attr-defined]
assert "greenhouse:gone (404 Not Found)" in notice.text # type: ignore[attr-defined]
assert "greenhouse:big 60 of 80" in notice.text # type: ignore[attr-defined]
assert "closed 60 roles" in notice.text # type: ignore[attr-defined]
def test_a_clean_settle_says_nothing(acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
_answer(monkeypatch, ([_row(i) for i in "12"], True))
said: list[object] = []
results = [pull.run_batch(FakeConfig(), acme, **batch) # type: ignore[arg-type]
for batch in pull._boards(FakeConfig(), acme)] # type: ignore[arg-type]
pull.close_boards(FakeConfig(), acme, Tally(results=tuple(results)), # type: ignore[arg-type]
context=RunContext(on_notice=said.append))
assert said == []
def test_a_finder_that_raises_releases_the_pull_claim(
acme: Store, monkeypatch: pytest.MonkeyPatch) -> None:
def broken(store: Store, subs: object) -> dict[str, object]:
raise RuntimeError("reach broke")
monkeypatch.setattr(pull, "reaches", broken)
# The worker runs the chain's finder once the job is done; here at once.
context = RunContext(on_task=lambda task: task.finder(FakeConfig(), acme))
with pytest.raises(RuntimeError, match="reach broke"):
cascade.run_pull(FakeConfig(), acme, context=context)
assert pull.running(acme) == ""
# And the job itself lets go when the chain cannot be built.
def no_chain(run: str) -> object:
raise RuntimeError("no chain")
monkeypatch.setattr(cascade, "pull_chain", no_chain)
with pytest.raises(RuntimeError, match="no chain"):
cascade.run_pull(FakeConfig(), acme, context=RunContext(on_task=lambda task: True))
assert pull.running(acme) == ""