Merge main: read state and tags meet per-agent votes and affinity

The per-agent migration becomes v6 behind main's v5. A vote still stamps
the role's read_at, tag counts hide the agent's hidden companies, and
/api/filters takes the agent like /api/roles.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-28 12:59:41 +02:00
commit c69fb0b6c4
23 files changed
+2270 -717

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
"""The Roles page's shell: the feed and a role's reader are one page, so
`/roles/{uid}` renders what `/` renders with that role open. The script
itself is exercised in a browser, not here; this holds what the server
decides."""
from __future__ import annotations
from collections.abc import Iterator
from datetime import UTC, datetime
from pathlib import Path
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.config import Config, ServerConfig
from app.main import create_app
from app.storage import Store
from desk.models import Item
def posting(source_id: str = "1", **raw: object) -> Item:
item = Item(source_id=source_id, location=f"https://boards.example/acme/{source_id}",
title="Senior Backend Engineer", body="Build things.",
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw=dict(raw))
item.source = "ats"
return item
@pytest.fixture
def app(tmp_path: Path) -> FastAPI:
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
@pytest.fixture
def store(app: FastAPI) -> Store:
return app.state.store
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
yield c
def test_the_roles_page_is_the_rail_the_feed_and_a_shut_reader(client: TestClient) -> None:
page = client.get("/").text
for part in ('id="rail"', 'id="list"', 'id="roles-list"', 'id="reader"',
'id="rail-toggle"', 'id="search"', 'id="sort"', 'id="period"', 'id="tags"'):
assert part in page, part
assert '<article id="reader" aria-label="Role" hidden>' in page
assert "data-reader" not in page.split("<header", 1)[0]
assert "<title>Roles — roledesk</title>" in page
assert "/static/roles.js" in page and "/static/judge.js" in page
def test_a_role_address_is_the_same_page_with_its_reader_open(
client: TestClient, store: Store) -> None:
store.upsert_role(posting("1", company="Acme"))
uid = posting("1").uid
page = client.get(f"/roles/{uid}")
assert page.status_code == 200
assert 'id="roles-list"' in page.text and "/static/roles.js" in page.text
assert '<body data-reader="open">' in page.text
assert '<article id="reader" aria-label="Role">' in page.text
assert "<title>Senior Backend Engineer — roledesk</title>" in page.text
# Roles stays the lit tab, though the path is not its own.
assert 'href="/"\n class="active" aria-current="page">Roles' in page.text
missing = client.get("/roles/nope")
assert missing.status_code == 404 and "text/html" in missing.headers["content-type"]
def test_the_read_page_of_its_own_is_gone(client: TestClient) -> None:
assert client.get("/static/role.js").status_code == 404
other = client.get("/applications").text
assert 'id="rail-toggle"' not in other and 'id="search"' not in other
assert "<main>" in other
+137 -11
View File
@@ -85,28 +85,31 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
Store(fresh)
Store(old)
with sqlite3.connect(old) as conn:
# v1 had no v2-v4 columns; the v4 vote columns that v5 drops again are
# not in a fresh file, so there is nothing to drop for them.
for column in ("permit", "languages", "extract_tries", "summary", "summarised_at",
"article_text", "article_at"):
"article_text", "article_at", "read_at"):
conn.execute(f"ALTER TABLE roles DROP COLUMN {column}")
for column in ("intent_key", "intent_vec"):
conn.execute(f"ALTER TABLE agents DROP COLUMN {column}")
# v1 had affinity on the company; the vote columns v4 added and v6
# drops again are not in a fresh file, so there is nothing to put back.
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
for table in ("vectors", "agent_companies", "role_votes"):
for table in ("vectors", "role_tags", "agent_companies", "role_votes"):
conn.execute(f"DROP TABLE {table}")
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.
for table in ("roles", "agents", "vectors", "companies", "agent_companies", "role_votes"):
for table in ("roles", "agents", "vectors", "companies", "role_tags",
"agent_companies", "role_votes"):
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
def _downgrade_to_v4(path: Path) -> None:
"""A fresh file put back to the v4 shape: votes on the role, affinity on
the company, no per-agent tables."""
the company, no read state, no tags, no per-agent tables."""
with sqlite3.connect(path) as conn:
conn.execute("ALTER TABLE roles DROP COLUMN read_at")
conn.execute("DROP TABLE role_tags")
conn.execute("ALTER TABLE roles ADD COLUMN vote INTEGER CHECK (vote IN (-1, 0, 1))")
conn.execute("ALTER TABLE roles ADD COLUMN voted_at TEXT")
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
@@ -115,7 +118,7 @@ def _downgrade_to_v4(path: Path) -> None:
conn.execute("PRAGMA user_version = 4")
def test_v5_moves_the_votes_and_affinities_to_the_default_agent(tmp_path: Path) -> None:
def test_v6_moves_the_votes_and_affinities_to_the_default_agent(tmp_path: Path) -> None:
path = tmp_path / "v4.db"
store = Store(path)
store.upsert_role(posting("liked"))
@@ -136,7 +139,7 @@ def test_v5_moves_the_votes_and_affinities_to_the_default_agent(tmp_path: Path)
migrated = Store(path)
assert migrated.migrations_applied == [5]
assert migrated.migrations_applied == [5, 6]
assert [a.name for a in migrated.agents()] == ["First", "Second"] # none added
assert migrated.votes(first.id) == {posting("liked").uid: 1, posting("passed").uid: -1}
assert migrated.votes(second.id) == {}
@@ -152,7 +155,7 @@ def test_v5_moves_the_votes_and_affinities_to_the_default_agent(tmp_path: Path)
assert "affinity" not in {r[1] for r in conn.execute("PRAGMA table_info(companies)")}
def test_v5_makes_an_agent_when_there_is_something_to_move_and_none_to_take_it(
def test_v6_makes_an_agent_when_there_is_something_to_move_and_none_to_take_it(
tmp_path: Path) -> None:
path = tmp_path / "v4.db"
store = Store(path)
@@ -171,17 +174,31 @@ def test_v5_makes_an_agent_when_there_is_something_to_move_and_none_to_take_it(
assert migrated.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
def test_v5_makes_no_agent_when_there_is_nothing_to_move(tmp_path: Path) -> None:
def test_v6_makes_no_agent_when_there_is_nothing_to_move(tmp_path: Path) -> None:
path = tmp_path / "v4.db"
store = Store(path)
store.upsert_role(posting("plain"))
store.add_company("Acme", key="acme")
_downgrade_to_v4(path)
assert Store(path).migrations_applied == [5]
assert Store(path).migrations_applied == [5, 6]
assert Store(path).agents() == []
def test_a_v4_file_migrates_read_state_and_tags(tmp_path: Path) -> None:
fresh, old = tmp_path / "fresh.db", tmp_path / "old.db"
Store(fresh)
Store(old)
_downgrade_to_v4(old)
assert Store(old).migrations_applied == [5, 6]
assert sorted(_columns(old, "roles")) == sorted(_columns(fresh, "roles"))
with sqlite3.connect(old) as conn:
tables = {r[0] for r in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table'")}
assert "role_tags" in tables
def test_state_is_a_mapping(store: Store) -> None:
store.state["ats:last"] = "2026-09-23"
assert store.state["ats:last"] == "2026-09-23"
@@ -394,6 +411,101 @@ def test_roles_with_an_agent_but_not_by_score_are_newest_first_with_votes(
assert [r.source_id for r in store.roles(agent=agent.id)] == ["1", "2"]
def test_read_state_is_set_cleared_and_a_vote_marks_read(
store: Store, clock: Clock) -> None:
store.upsert_role(posting("1"))
uid = posting("1").uid
role = store.role(uid)
assert role is not None and role.read_at is None
read = store.set_read(uid)
assert read.read_at == clock().isoformat(timespec="seconds")
clock.tick()
again = store.set_read(uid)
assert again.read_at == read.read_at, "reading twice keeps the first stamp"
cleared = store.set_read(uid, False)
assert cleared.read_at is None
agent = store.save_agent("A")
voted = store.set_vote(agent.id, uid, 1)
assert voted.read_at is not None, "voting is reading"
stamp = voted.read_at
unvoted = store.set_vote(agent.id, uid, None)
assert unvoted.read_at == stamp, "clearing a vote is not un-reading"
with pytest.raises(KeyError):
store.set_read("nope")
def test_roles_include_read_excludes_a_read_role_before_the_limit(store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
uid = {r.source_id: r.uid for r in store.roles()}
store.set_read(uid["1"])
assert {r.source_id for r in store.roles()} == {"1", "2"}
assert {r.source_id for r in store.roles(include_read=False)} == {"2"}
assert store.count_roles(include_read=False) == 1
def test_set_tags_normalises_dedupes_and_caps(store: Store) -> None:
store.upsert_role(posting("1"))
uid = posting("1").uid
long_tag = "x" * 50
tags = store.set_tags(uid, [" Backend Engineer ", "python", "PYTHON", "", " ",
long_tag, *[f"t{i}" for i in range(15)]])
assert tags == sorted(tags)
assert len(tags) == 12 # capped at _MAX_TAGS
assert len(set(tags)) == 12 # no repeats
assert all(len(t) <= 40 for t in tags) # cut to _MAX_TAG_LEN
assert "backend engineer" in tags and "python" in tags
role = store.role(uid)
assert role is not None and role.tags == tags # loaded sorted, with the role
with pytest.raises(KeyError):
store.set_tags("nope", ["x"])
def test_roles_filters_by_tags_any_and_all(store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
store.upsert_role(posting("3"))
uid = {r.source_id: r.uid for r in store.roles()}
store.set_tags(uid["1"], ["python", "backend"])
store.set_tags(uid["2"], ["python", "frontend"])
store.set_tags(uid["3"], ["go"])
assert {r.source_id for r in store.roles(tags=["python"])} == {"1", "2"}
assert {r.source_id for r in store.roles(tags=["python", "go"])} == {"1", "2", "3"}
assert {r.source_id for r in
store.roles(tags=["python", "backend"], tag_mode="all")} == {"1"}
assert store.count_roles(tags=["python"]) == 2
assert store.count_roles(tags=["python", "backend"], tag_mode="all") == 1
with pytest.raises(ValueError, match="tag_mode"):
store.roles(tags=["python"], tag_mode="maybe")
def test_tag_counts_orders_by_count_then_tag_and_respects_muted(store: Store) -> None:
store.add_company("Hidden", key="hidden")
agent = store.save_agent("A")
store.set_affinity(agent.id, "hidden", -1)
store.upsert_role(posting("1", company="Acme"))
store.upsert_role(posting("2", company="Acme"))
store.upsert_role(posting("3", company="Hidden"))
uid = {r.source_id: r.uid for r in store.roles(hide_muted=False)}
store.set_tags(uid["1"], ["python", "backend"])
store.set_tags(uid["2"], ["python"])
store.set_tags(uid["3"], ["python", "backend"])
store.set_read(uid["1"])
assert store.tag_counts(agent.id) == [("python", 2, 1), ("backend", 1, 0)]
assert store.tag_counts(agent.id, show_muted=True) == [("python", 3, 2), ("backend", 2, 1)]
assert store.tag_counts() == [("python", 3, 2), ("backend", 2, 1)] # no agent, nothing hidden
def test_a_vote_moves_the_served_score_and_the_order(store: Store) -> None:
for sid in ("a", "b", "c"):
store.upsert_role(posting(sid))
@@ -595,6 +707,20 @@ def test_a_summary_is_written_once(store: Store) -> None:
assert store.set_summary("nope", "x") is False
def test_a_summarys_tags_land_with_it_and_not_on_a_rejected_late_answer(
store: Store) -> None:
store.upsert_role(posting())
uid = posting().uid
assert store.set_summary(uid, "First.", ["Python", "backend"]) is True
role = store.role(uid)
assert role is not None and role.tags == ["backend", "python"]
# A second write is rejected (already summarised), and does not touch tags.
assert store.set_summary(uid, "Second.", ["go"]) is False
role = store.role(uid)
assert role is not None and role.tags == ["backend", "python"]
def test_summarise_waits_on_the_top_of_each_enabled_agent(store: Store) -> None:
"""Two agents each contribute their own top one; a closed role and a
gated score are not in anybody's top. A hidden company needs no case:
+19 -2
View File
@@ -43,8 +43,9 @@ class Model:
return agent.Reply(data={"summaries": self.reply(items)}, backend="api")
def written(items: list[dict[str, Any]]) -> list[dict[str, str]]:
return [{"uid": i["uid"], "summary": f"A backend role at Acme, {i['uid']}."}
def written(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
return [{"uid": i["uid"], "summary": f"A backend role at Acme, {i['uid']}.",
"tags": ["Backend Engineer", "python", "fintech"]}
for i in items]
@@ -89,12 +90,28 @@ def test_a_batch_writes_the_summary_and_stamps_summarised_at(
assert role is not None
assert role.summary == f"A backend role at Acme, {uid}."
assert role.summarised_at is not None
assert role.tags == ["backend engineer", "fintech", "python"]
assert result["summarised"] == 1 and result["pending"] == 0
[[item]] = model.asked
assert item == {"uid": uid, "title": "Backend Engineer 1", "company": "Acme",
"place": "Copenhagen", "text": "Python and Go on Kubernetes. Pays 700k DKK."}
def test_the_replys_tags_are_normalised_and_stored(
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
[uid] = ranked(store, "1")
stub(monkeypatch, lambda items: [
{"uid": items[0]["uid"], "summary": "A backend role, long enough to store.",
"tags": [" Backend Engineer ", "PYTHON", "python", "", " "]}])
modeltask.run(summarise.TASK, config, store)
role = store.role(uid)
assert role is not None
# Collapsed whitespace, lowercased, deduplicated, empties dropped.
assert role.tags == ["backend engineer", "python"]
def test_the_fetched_page_is_read_before_the_description_and_capped(
store: Store, config: Config, monkeypatch: pytest.MonkeyPatch) -> None:
[uid] = ranked(store, "1")
+67 -2
View File
@@ -284,6 +284,72 @@ def test_a_vote_moves_a_role_in_the_ranking_at_once(
assert ranked() == [("a", 1.0), ("c", 0.66), ("b", 0.6)] # capped at 1
def test_read_state_is_set_and_cleared_over_the_api(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
uid = posting("1").uid
res = client.post(f"/api/roles/{uid}/read")
assert res.status_code == 200
assert res.json()["read_at"] is not None
res = client.delete(f"/api/roles/{uid}/read")
assert res.status_code == 200
assert res.json()["read_at"] is None
assert client.post("/api/roles/nope/read").status_code == 404
assert client.delete("/api/roles/nope/read").status_code == 404
def test_list_roles_filters_by_include_read(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
uid = posting("1").uid
client.post(f"/api/roles/{uid}/read")
all_ = client.get("/api/roles").json()
assert {r["source_id"] for r in all_["items"]} == {"1", "2"}
unread = client.get("/api/roles", params={"include_read": "false"}).json()
assert {r["source_id"] for r in unread["items"]} == {"2"}
assert unread["total"] == 1
def test_list_roles_filters_by_tags_any_and_all(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1"))
store.upsert_role(posting("2"))
uid = {r.source_id: r.uid for r in store.roles()}
store.set_tags(uid["1"], ["python", "backend"])
store.set_tags(uid["2"], ["python", "frontend"])
res = client.get("/api/roles", params={"tags": ["python"]})
assert {r["source_id"] for r in res.json()["items"]} == {"1", "2"}
both = client.get("/api/roles", params={"tags": ["python", "backend"],
"tag_mode": "all"})
assert {r["source_id"] for r in both.json()["items"]} == {"1"}
[item] = both.json()["items"]
assert item["tags"] == ["backend", "python"]
def test_filters_lists_tag_counts_and_respects_show_muted(
client: TestClient, store: Store) -> None:
store.add_company("Hidden", key="hidden")
agent = store.save_agent("A") # the default agent, whose hidden companies count
store.set_affinity(agent.id, "hidden", -1)
store.upsert_role(posting("1", company="Acme"))
store.upsert_role(posting("2", company="Hidden"))
uid = {r.source_id: r.uid for r in store.roles(hide_muted=False)}
store.set_tags(uid["1"], ["python"])
store.set_tags(uid["2"], ["python"])
res = client.get("/api/filters")
assert res.status_code == 200
assert res.json() == {"tags": [{"tag": "python", "count": 1, "unread": 1}]}
muted = client.get("/api/filters", params={"show_muted": "true"})
assert muted.json() == {"tags": [{"tag": "python", "count": 2, "unread": 2}]}
def test_one_role_and_its_read_page(client: TestClient, store: Store) -> None:
store.upsert_role(posting("1", company="Acme", place="Berlin"))
uid = posting("1").uid
@@ -298,8 +364,7 @@ def test_one_role_and_its_read_page(client: TestClient, store: Store) -> None:
page = client.get(f"/roles/{uid}")
assert page.status_code == 200
assert 'id="role"' in page.text and "/static/role.js" in page.text
assert "/static/roles.js" not in page.text
assert '<body data-reader="open">' in page.text and "/static/roles.js" in page.text
missing = client.get("/roles/nope")
assert missing.status_code == 404 and "text/html" in missing.headers["content-type"]