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

+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: