Extract gate columns from posting text with a Haiku ModelTask
roledesk job extract reads open, unanswered roles 20 to a call and fills place, country, remote, seniority, salary, currency, function, permit and languages, only into columns the board left empty. Schema v2 adds roles.permit, roles.languages and roles.extract_tries. Off by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b44f78ca7e
commit
4dfb8831aa
12 files changed
+578
-14
No files matched your search
@@ -0,0 +1,167 @@
|
||||
"""The `extract` task, with the model call stubbed: what is offered, what an
|
||||
answer writes, and what it may not touch. Nothing here reaches a model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from desk import modeltask
|
||||
from desk.models import Item
|
||||
from desk.tools import agent
|
||||
from roledesk.config import Config
|
||||
from roledesk.jobs import extract
|
||||
from roledesk.storage import Store
|
||||
|
||||
|
||||
def posting(source_id: str, title: str = "Backend Engineer", **raw: object) -> Item:
|
||||
item = Item(source_id=source_id, location=f"https://boards.example/{source_id}",
|
||||
title=title, body="We pay 80.000-95.000 EUR. Hybrid in Berlin.",
|
||||
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw=dict(raw))
|
||||
item.source = "ats"
|
||||
return item
|
||||
|
||||
|
||||
def answer(uid: str, **fields: Any) -> dict[str, Any]:
|
||||
empty: dict[str, Any] = {
|
||||
"place": "", "country": "", "remote": "", "seniority": "", "salary_min": None,
|
||||
"salary_max": None, "currency": "", "function": "", "permit": "",
|
||||
"languages_required": []}
|
||||
return {"uid": uid, **empty, **fields}
|
||||
|
||||
|
||||
class Model:
|
||||
"""Stands in for `agent.ask_json`: records what it was asked and hands
|
||||
back whatever `reply` builds from the batch."""
|
||||
|
||||
def __init__(self, reply: Any) -> None:
|
||||
self.reply = reply
|
||||
self.asked: list[list[dict[str, Any]]] = []
|
||||
|
||||
def __call__(self, settings: Any, **kwargs: Any) -> agent.Reply:
|
||||
roles = kwargs["question"]["roles"]
|
||||
self.asked.append(roles)
|
||||
return agent.Reply(data={"roles": self.reply(roles)}, backend="api")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path: Path) -> Store:
|
||||
return Store(tmp_path / "roledesk.db")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config(tmp_path: Path) -> Config:
|
||||
return Config(database=str(tmp_path / "roledesk.db"), extract={"enabled": True})
|
||||
|
||||
|
||||
def stub(monkeypatch: pytest.MonkeyPatch, reply: Any) -> Model:
|
||||
model = Model(reply)
|
||||
monkeypatch.setattr(agent, "backend", lambda settings: "api")
|
||||
monkeypatch.setattr(agent, "ask_json", model)
|
||||
return model
|
||||
|
||||
|
||||
def test_a_batch_writes_the_columns_and_stamps_extracted_at(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
uid = posting("1").uid
|
||||
store.upsert_role(posting("1"))
|
||||
stub(monkeypatch, lambda roles: [answer(
|
||||
r["uid"], place="Berlin", country="de", remote="hybrid", seniority="senior",
|
||||
salary_min=80000, salary_max=95000, currency="eur", function="engineering",
|
||||
permit="sponsors", languages_required=["EN", "de", "xx-long"]) for r in roles])
|
||||
|
||||
result = modeltask.run(extract.TASK, config, store)
|
||||
|
||||
role = store.role(uid)
|
||||
assert role is not None
|
||||
assert (role.place, role.place_key, role.country, role.remote) == \
|
||||
("Berlin", "berlin", "DE", "hybrid")
|
||||
assert (role.seniority, role.function, role.permit) == ("senior", "engineering", "sponsors")
|
||||
assert (role.salary_min, role.salary_max, role.currency) == (80000, 95000, "EUR")
|
||||
assert role.languages == ["en", "de"]
|
||||
assert role.extracted_at is not None
|
||||
assert result["extracted"] == 1 and result["pending"] == 0
|
||||
|
||||
|
||||
def test_an_empty_answer_never_blanks_and_a_board_field_is_never_replaced(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
item = posting("1", place="Berlin", remote="hybrid", salary_min=70000)
|
||||
store.upsert_role(item)
|
||||
stub(monkeypatch, lambda roles: [answer(
|
||||
r["uid"], place="Munich", remote="", salary_min=None, salary_max=90000,
|
||||
country="DE") for r in roles])
|
||||
|
||||
modeltask.run(extract.TASK, config, store)
|
||||
|
||||
role = store.role(item.uid)
|
||||
assert role is not None
|
||||
assert (role.place, role.remote, role.salary_min) == ("Berlin", "hybrid", 70000)
|
||||
assert (role.country, role.salary_max) == ("DE", 90000)
|
||||
|
||||
|
||||
def test_the_next_upsert_keeps_what_extract_filled(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
stub(monkeypatch, lambda roles: [answer(r["uid"], country="DE", seniority="mid")
|
||||
for r in roles])
|
||||
modeltask.run(extract.TASK, config, store)
|
||||
|
||||
store.upsert_role(posting("1"))
|
||||
|
||||
role = store.role(posting("1").uid)
|
||||
assert role is not None and (role.country, role.seniority) == ("DE", "mid")
|
||||
|
||||
|
||||
def test_closed_and_answered_roles_are_not_offered(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
for sid in ("open", "closed", "done"):
|
||||
store.upsert_role(posting(sid))
|
||||
with store.transaction() as conn:
|
||||
conn.execute("UPDATE roles SET closed_at = '2026-09-22T00:00:00+00:00' "
|
||||
"WHERE source_id = 'closed'")
|
||||
conn.execute("UPDATE roles SET extracted_at = '2026-09-22T00:00:00+00:00' "
|
||||
"WHERE source_id = 'done'")
|
||||
model = stub(monkeypatch, lambda roles: [answer(r["uid"]) for r in roles])
|
||||
|
||||
modeltask.run(extract.TASK, config, store)
|
||||
|
||||
assert [[r["uid"] for r in batch] for batch in model.asked] == [[posting("open").uid]]
|
||||
|
||||
|
||||
def test_an_unknown_uid_is_ignored_and_the_role_left_out_is_charged(
|
||||
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
stub(monkeypatch, lambda roles: [answer("not-in-the-batch", country="DE")])
|
||||
|
||||
result = modeltask.run(extract.TASK, config, store)
|
||||
|
||||
role = store.role(posting("1").uid)
|
||||
assert role is not None and role.country == "" and role.extracted_at is None
|
||||
assert result["rejected"] == ["not-in-the-batch"]
|
||||
assert store.extract_pending() == 1
|
||||
|
||||
modeltask.run(extract.TASK, config, store)
|
||||
modeltask.run(extract.TASK, config, store)
|
||||
assert store.extract_pending() == 0, "given up after three unanswered replies"
|
||||
|
||||
|
||||
def test_a_value_outside_the_vocabulary_is_dropped() -> None:
|
||||
got = extract.fields(answer("u", remote="sometimes", seniority="Senior",
|
||||
country="Germany", currency="€", function="ops",
|
||||
permit="maybe", salary_min=-1, salary_max=True))
|
||||
assert got["remote"] == "" and got["seniority"] == "senior"
|
||||
assert got["country"] == "" and got["currency"] == "" and got["function"] == ""
|
||||
assert got["permit"] == "" and got["salary_min"] is None and got["salary_max"] is None
|
||||
|
||||
|
||||
def test_disabled_finds_nothing_and_asks_nobody(
|
||||
store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
model = stub(monkeypatch, lambda roles: [])
|
||||
|
||||
result = modeltask.run(extract.TASK, Config(database=str(tmp_path / "x.db")), store)
|
||||
|
||||
assert model.asked == [] and result["extracted"] == 0
|
||||
@@ -75,6 +75,25 @@ def test_a_newer_file_is_refused(tmp_path: Path) -> None:
|
||||
Store(path)
|
||||
|
||||
|
||||
def _columns(path: Path, table: str) -> list[tuple[str, str, int, str]]:
|
||||
with sqlite3.connect(path) as conn:
|
||||
return [(r[1], r[2], r[3], r[4]) for r in conn.execute(f"PRAGMA table_info({table})")]
|
||||
|
||||
|
||||
def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||||
fresh, old = tmp_path / "fresh.db", tmp_path / "old.db"
|
||||
Store(fresh)
|
||||
Store(old)
|
||||
with sqlite3.connect(old) as conn:
|
||||
for column in ("permit", "languages", "extract_tries"):
|
||||
conn.execute(f"ALTER TABLE roles DROP COLUMN {column}")
|
||||
conn.execute("PRAGMA user_version = 1")
|
||||
|
||||
assert Store(old).migrations_applied == list(range(2, LATEST + 1))
|
||||
# ALTER appends, so the order differs; every read is by column name.
|
||||
assert sorted(_columns(old, "roles")) == sorted(_columns(fresh, "roles"))
|
||||
|
||||
|
||||
def test_state_is_a_mapping(store: Store) -> None:
|
||||
store.state["ats:last"] = "2026-09-23"
|
||||
assert store.state["ats:last"] == "2026-09-23"
|
||||
|
||||
Reference in new issue
Block a user