Per-agent ranking: votes, affinity and a taste term; settings are three sub-tabs
Votes on roles and company affinity move to role_votes and agent_companies, one row per search agent, so two agents rank the same roles differently. The score gains taste, the cosine to mean(liked) - mean(passed) of the agent's votes, as newsdesk ranks articles. The API takes agent on roles, votes and companies and falls back to the default agent, the enabled one with the lowest id. Settings become Search agents (a list again, with New and Delete), Sources (job boards, company boards and runs with the message log, one section per plugin) and Account. Old paths answer 301 to their section. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
515d2250fc
commit
a0772519b3
28 files changed
+1461
-668
No files matched your search
+244
-39
@@ -85,20 +85,103 @@ 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:
|
||||
for column in ("permit", "languages", "extract_tries", "vote", "voted_at",
|
||||
"summary", "summarised_at", "article_text", "article_at"):
|
||||
# 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"):
|
||||
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}")
|
||||
conn.execute("DROP TABLE vectors")
|
||||
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
|
||||
for table in ("vectors", "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"):
|
||||
for table in ("roles", "agents", "vectors", "companies", "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."""
|
||||
with sqlite3.connect(path) as conn:
|
||||
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")
|
||||
conn.execute("DROP TABLE agent_companies")
|
||||
conn.execute("DROP TABLE role_votes")
|
||||
conn.execute("PRAGMA user_version = 4")
|
||||
|
||||
|
||||
def test_v5_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"))
|
||||
store.upsert_role(posting("passed"))
|
||||
store.upsert_role(posting("none"))
|
||||
store.add_company("Acme", key="acme")
|
||||
store.add_company("Other", key="other")
|
||||
# The disabled agent has the lower id; the enabled one is the default.
|
||||
second = store.save_agent("Second", enabled=False)
|
||||
first = store.save_agent("First")
|
||||
assert first.id > second.id and store.default_agent().id == first.id
|
||||
_downgrade_to_v4(path)
|
||||
with sqlite3.connect(path) as conn:
|
||||
conn.execute("UPDATE roles SET vote = 1, voted_at = 'then' WHERE uid = ?",
|
||||
(posting("liked").uid,))
|
||||
conn.execute("UPDATE roles SET vote = -1 WHERE uid = ?", (posting("passed").uid,))
|
||||
conn.execute("UPDATE companies SET affinity = -1 WHERE key = 'acme'")
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [5]
|
||||
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) == {}
|
||||
liked = migrated.role(posting("liked").uid, first.id)
|
||||
assert liked is not None and liked.voted_at == "then"
|
||||
passed = migrated.role(posting("passed").uid, first.id)
|
||||
assert passed is not None and passed.voted_at # stamped when v4 had none
|
||||
assert [c.affinity for c in migrated.companies(first.id)] == [-1, 0]
|
||||
assert [c.affinity for c in migrated.companies(second.id)] == [0, 0]
|
||||
with sqlite3.connect(path) as conn:
|
||||
columns = {r[1] for r in conn.execute("PRAGMA table_info(roles)")}
|
||||
assert not {"vote", "voted_at"} & columns
|
||||
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(
|
||||
tmp_path: Path) -> None:
|
||||
path = tmp_path / "v4.db"
|
||||
store = Store(path)
|
||||
store.upsert_role(posting("liked"))
|
||||
store.add_company("Acme", key="acme")
|
||||
_downgrade_to_v4(path)
|
||||
with sqlite3.connect(path) as conn:
|
||||
conn.execute("UPDATE companies SET affinity = 2 WHERE key = 'acme'")
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
[agent] = migrated.agents()
|
||||
assert (agent.name, agent.enabled, agent.gates) == ("My ranking", True, [])
|
||||
assert agent.created_at.endswith("+00:00")
|
||||
assert migrated.default_agent() == agent
|
||||
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:
|
||||
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).agents() == []
|
||||
|
||||
|
||||
def test_state_is_a_mapping(store: Store) -> None:
|
||||
store.state["ats:last"] = "2026-09-23"
|
||||
assert store.state["ats:last"] == "2026-09-23"
|
||||
@@ -190,7 +273,9 @@ def test_roles_filters_q_function_remote_and_days(store: Store, clock: Clock) ->
|
||||
|
||||
|
||||
def test_roles_hide_muted_excludes_before_the_limit(store: Store, clock: Clock) -> None:
|
||||
store.add_company("Acme", key="acme", affinity=-1)
|
||||
agent = store.save_agent("A")
|
||||
store.add_company("Acme", key="acme")
|
||||
store.set_affinity(agent.id, "acme", -1)
|
||||
store.upsert_role(posting("other", company="Other"))
|
||||
clock.tick()
|
||||
for i in range(3):
|
||||
@@ -199,10 +284,15 @@ def test_roles_hide_muted_excludes_before_the_limit(store: Store, clock: Clock)
|
||||
# Acme's roles are the three newest, so a plain limit=1 shows only Acme;
|
||||
# hide_muted has to reach past all three to find "other" rather than
|
||||
# just dropping Acme's rows out of an already-limited page.
|
||||
assert [r.source_id for r in store.roles(limit=1)] == ["a2"]
|
||||
assert [r.source_id for r in store.roles(limit=1, hide_muted=True)] == ["other"]
|
||||
assert {r.source_id for r in store.roles(hide_muted=True)} == {"other"}
|
||||
assert {r.source_id for r in store.roles()} == {"a0", "a1", "a2", "other"}
|
||||
listing = {"agent": agent.id, "by_score": False}
|
||||
assert [r.source_id for r in store.roles(limit=1, **listing)] == ["a2"]
|
||||
assert [r.source_id for r in store.roles(limit=1, hide_muted=True, **listing)] == ["other"]
|
||||
assert {r.source_id for r in store.roles(hide_muted=True, **listing)} == {"other"}
|
||||
assert {r.source_id for r in store.roles(**listing)} == {"a0", "a1", "a2", "other"}
|
||||
# Hidden is per agent: another agent, or no agent, hides nothing.
|
||||
other = store.save_agent("B")
|
||||
assert store.count_roles(hide_muted=True, agent=other.id, by_score=False) == 4
|
||||
assert store.count_roles(hide_muted=True) == 4
|
||||
|
||||
|
||||
def test_roles_page_by_offset_and_count_the_same_filter(
|
||||
@@ -214,34 +304,94 @@ def test_roles_page_by_offset_and_count_the_same_filter(
|
||||
assert [r.source_id for r in store.roles(limit=2, offset=3)] == ["1"]
|
||||
assert store.count_roles() == 4
|
||||
assert store.count_roles(company="acme") == 3
|
||||
store.add_company("Other", key="other", affinity=-1)
|
||||
assert store.count_roles(hide_muted=True) == 3
|
||||
agent = store.save_agent("A")
|
||||
store.add_company("Other", key="other")
|
||||
store.set_affinity(agent.id, "other", -1)
|
||||
assert store.count_roles(hide_muted=True, agent=agent.id, by_score=False) == 3
|
||||
|
||||
|
||||
def test_a_vote_is_stamped_cleared_and_filtered(store: Store, clock: Clock) -> None:
|
||||
for sid in ("up", "down", "fine", "none"):
|
||||
store.upsert_role(posting(sid))
|
||||
uid = {r.source_id: r.uid for r in store.roles()}
|
||||
role = store.set_vote(uid["up"], 1)
|
||||
agent = store.save_agent("A")
|
||||
role = store.set_vote(agent.id, uid["up"], 1)
|
||||
assert (role.vote, role.voted_at) == (1, clock().isoformat(timespec="seconds"))
|
||||
store.set_vote(uid["down"], -1)
|
||||
store.set_vote(uid["fine"], 0)
|
||||
store.set_vote(agent.id, uid["down"], -1)
|
||||
store.set_vote(agent.id, uid["fine"], 0)
|
||||
assert store.votes(agent.id) == {uid["up"]: 1, uid["down"]: -1, uid["fine"]: 0}
|
||||
|
||||
assert [r.source_id for r in store.roles(judged="liked")] == ["up"]
|
||||
assert [r.source_id for r in store.roles(judged="passed")] == ["down"]
|
||||
assert [r.source_id for r in store.roles(judged="unjudged")] == ["none"]
|
||||
assert store.count_roles(judged="unjudged") == 1
|
||||
def listed(judged: str) -> list[str]:
|
||||
return [r.source_id for r in store.roles(agent=agent.id, by_score=False,
|
||||
judged=judged)]
|
||||
|
||||
assert listed("liked") == ["up"]
|
||||
assert listed("passed") == ["down"]
|
||||
assert listed("unjudged") == ["none"]
|
||||
assert store.count_roles(agent=agent.id, by_score=False, judged="unjudged") == 1
|
||||
with pytest.raises(ValueError, match="judged"):
|
||||
store.roles(judged="maybe")
|
||||
|
||||
cleared = store.set_vote(uid["up"], None)
|
||||
clock.tick()
|
||||
changed = store.set_vote(agent.id, uid["up"], -1)
|
||||
assert (changed.vote, changed.voted_at) == (-1, clock().isoformat(timespec="seconds"))
|
||||
cleared = store.set_vote(agent.id, uid["up"], None)
|
||||
assert (cleared.vote, cleared.voted_at) == (None, None)
|
||||
assert store.set_vote(agent.id, uid["up"], None).vote is None # clearing twice is fine
|
||||
with pytest.raises(ValueError):
|
||||
store.set_vote(uid["up"], 2)
|
||||
store.set_vote(agent.id, uid["up"], 2)
|
||||
with pytest.raises(ValueError):
|
||||
store.set_vote(uid["up"], True)
|
||||
store.set_vote(agent.id, uid["up"], True)
|
||||
with pytest.raises(KeyError):
|
||||
store.set_vote("nope", 1)
|
||||
store.set_vote(agent.id, "nope", 1)
|
||||
with pytest.raises(KeyError):
|
||||
store.set_vote(999, uid["up"], 1)
|
||||
|
||||
|
||||
def test_votes_are_per_agent(store: Store) -> None:
|
||||
store.upsert_role(posting("1"))
|
||||
uid = posting("1").uid
|
||||
one = store.save_agent("One")
|
||||
two = store.save_agent("Two")
|
||||
store.set_vote(one.id, uid, 1)
|
||||
|
||||
assert store.role(uid, one.id).vote == 1 # type: ignore[union-attr]
|
||||
assert store.role(uid, two.id).vote is None # type: ignore[union-attr]
|
||||
assert store.role(uid).vote is None # type: ignore[union-attr]
|
||||
assert store.votes(two.id) == {}
|
||||
assert [r.vote for r in store.roles(agent=two.id, by_score=False)] == [None]
|
||||
assert [r.vote for r in store.roles()] == [None]
|
||||
assert [r.source_id for r in store.roles(agent=two.id, by_score=False, judged="liked")] == []
|
||||
assert [r.source_id for r in store.roles(judged="liked")] == []
|
||||
assert [r.source_id for r in store.roles(judged="unjudged")] == ["1"]
|
||||
|
||||
store.set_vote(two.id, uid, -1)
|
||||
assert store.role(uid, one.id).vote == 1 # type: ignore[union-attr]
|
||||
assert store.role(uid, two.id).vote == -1 # type: ignore[union-attr]
|
||||
|
||||
# Deleting the agent takes its votes with it and leaves the other's.
|
||||
store.delete_agent(two.id)
|
||||
assert store.votes(one.id) == {uid: 1}
|
||||
with store.transaction() as conn:
|
||||
assert conn.execute("SELECT COUNT(*) FROM role_votes").fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_roles_with_an_agent_but_not_by_score_are_newest_first_with_votes(
|
||||
store: Store, clock: Clock) -> None:
|
||||
for sid in ("1", "2", "3"):
|
||||
store.upsert_role(posting(sid))
|
||||
clock.tick()
|
||||
uid = {r.source_id: r.uid for r in store.roles()}
|
||||
agent = store.save_agent("A")
|
||||
store.replace_scores(agent.id, [(uid["1"], 0.9, ""), (uid["2"], 0.1, "")]) # 3 unscored
|
||||
store.set_vote(agent.id, uid["1"], 1)
|
||||
|
||||
newest = store.roles(agent=agent.id, by_score=False)
|
||||
assert [r.source_id for r in newest] == ["3", "2", "1"]
|
||||
assert [r.score for r in newest] == [None, None, None]
|
||||
assert [r.vote for r in newest] == [None, None, 1]
|
||||
assert store.count_roles(agent=agent.id, by_score=False) == 3
|
||||
assert [r.source_id for r in store.roles(agent=agent.id)] == ["1", "2"]
|
||||
|
||||
|
||||
def test_a_vote_moves_the_served_score_and_the_order(store: Store) -> None:
|
||||
@@ -249,15 +399,21 @@ def test_a_vote_moves_the_served_score_and_the_order(store: Store) -> None:
|
||||
store.upsert_role(posting(sid))
|
||||
uid = {r.source_id: r.uid for r in store.roles()}
|
||||
agent = store.save_agent("A")
|
||||
other = store.save_agent("B")
|
||||
store.replace_scores(agent.id, [(uid["a"], 0.9, ""), (uid["b"], 0.5, ""),
|
||||
(uid["c"], 0.4, "")])
|
||||
store.set_vote(uid["a"], -1) # 0.45
|
||||
store.set_vote(uid["c"], 1) # 0.48
|
||||
store.replace_scores(other.id, [(uid["a"], 0.9, ""), (uid["b"], 0.5, ""),
|
||||
(uid["c"], 0.4, "")])
|
||||
store.set_vote(agent.id, uid["a"], -1) # 0.45
|
||||
store.set_vote(agent.id, uid["c"], 1) # 0.48
|
||||
ranked = [(r.source_id, round(r.score or 0, 3)) for r in store.roles(agent=agent.id)]
|
||||
assert ranked == [("b", 0.5), ("c", 0.48), ("a", 0.45)]
|
||||
assert store.scores(agent.id)[uid["a"]][0] == 0.9 # the stored score is kept
|
||||
store.set_vote(uid["a"], 1)
|
||||
store.set_vote(agent.id, uid["a"], 1)
|
||||
assert store.roles(agent=agent.id, limit=1)[0].score == 1.0
|
||||
# The other agent's ranking is untouched by these votes.
|
||||
assert [(r.source_id, r.score) for r in store.roles(agent=other.id)] == \
|
||||
[("a", 0.9), ("b", 0.5), ("c", 0.4)]
|
||||
|
||||
|
||||
def test_mark_closed_closes_what_the_board_no_longer_lists(store: Store) -> None:
|
||||
@@ -288,10 +444,10 @@ def test_companies_crud(store: Store) -> None:
|
||||
assert acme.key == "acme" and acme.affinity == 0 and acme.status == "researching"
|
||||
with pytest.raises(ValueError, match="already exists"):
|
||||
store.add_company("Acme")
|
||||
updated = store.update_company("acme", affinity=2, status="contacted")
|
||||
assert (updated.affinity, updated.status) == (2, "contacted")
|
||||
with pytest.raises(ValueError, match="affinity"):
|
||||
store.update_company("acme", affinity=5)
|
||||
updated = store.update_company("acme", status="contacted")
|
||||
assert updated.status == "contacted"
|
||||
with pytest.raises(ValueError, match="not a company field"):
|
||||
store.update_company("acme", affinity=2)
|
||||
with pytest.raises(ValueError, match="not a company field"):
|
||||
store.update_company("acme", created_at="x")
|
||||
assert [c.key for c in store.companies()] == ["acme"]
|
||||
@@ -299,6 +455,56 @@ def test_companies_crud(store: Store) -> None:
|
||||
assert store.company("acme") is None
|
||||
|
||||
|
||||
def test_affinity_is_per_agent(store: Store) -> None:
|
||||
store.add_company("Acme", key="acme")
|
||||
store.add_company("Other", key="other")
|
||||
one = store.save_agent("One")
|
||||
two = store.save_agent("Two")
|
||||
|
||||
assert store.set_affinity(one.id, "acme", 2).affinity == 2
|
||||
assert store.company("acme", one.id).affinity == 2 # type: ignore[union-attr]
|
||||
assert store.company("acme", two.id).affinity == 0 # type: ignore[union-attr]
|
||||
assert store.company("acme").affinity == 0 # type: ignore[union-attr]
|
||||
assert [(c.key, c.affinity) for c in store.companies(one.id)] == [("acme", 2), ("other", 0)]
|
||||
assert [c.affinity for c in store.companies(two.id)] == [0, 0]
|
||||
assert [c.affinity for c in store.companies()] == [0, 0]
|
||||
|
||||
store.set_affinity(two.id, "acme", -1)
|
||||
assert store.company("acme", one.id).affinity == 2 # type: ignore[union-attr]
|
||||
assert store.company("acme", two.id).affinity == -1 # type: ignore[union-attr]
|
||||
|
||||
# 0 deletes the row rather than keeping a neutral one.
|
||||
assert store.set_affinity(one.id, "acme", 0).affinity == 0
|
||||
with store.transaction() as conn:
|
||||
rows = conn.execute("SELECT agent_id, company_key FROM agent_companies").fetchall()
|
||||
assert [tuple(r) for r in rows] == [(two.id, "acme")]
|
||||
|
||||
with pytest.raises(ValueError, match="affinity"):
|
||||
store.set_affinity(one.id, "acme", 5)
|
||||
with pytest.raises(ValueError, match="affinity"):
|
||||
store.set_affinity(one.id, "acme", True) # type: ignore[arg-type]
|
||||
with pytest.raises(KeyError):
|
||||
store.set_affinity(one.id, "nope", 1)
|
||||
with pytest.raises(KeyError):
|
||||
store.set_affinity(999, "acme", 1)
|
||||
|
||||
store.delete_agent(two.id)
|
||||
with store.transaction() as conn:
|
||||
assert conn.execute("SELECT COUNT(*) FROM agent_companies").fetchone()[0] == 0
|
||||
|
||||
|
||||
def test_the_default_agent_is_the_lowest_enabled_id(store: Store) -> None:
|
||||
assert store.default_agent() is None
|
||||
off = store.save_agent("Off", enabled=False)
|
||||
assert store.default_agent() == off
|
||||
on = store.save_agent("On")
|
||||
assert store.default_agent() == on
|
||||
later = store.save_agent("Later")
|
||||
assert store.default_agent() == on
|
||||
store.delete_agent(on.id)
|
||||
assert store.default_agent() == later
|
||||
|
||||
|
||||
def test_open_role_counts_and_company_boards(store: Store) -> None:
|
||||
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
||||
store.add_company("Acme", key="acme")
|
||||
@@ -390,23 +596,22 @@ def test_a_summary_is_written_once(store: Store) -> None:
|
||||
|
||||
|
||||
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, a hidden
|
||||
company's role and a gated score are not in anybody's top."""
|
||||
for sid, company in (("a", "Acme"), ("b", "Acme"), ("c", "Hidden"), ("d", "Acme")):
|
||||
store.upsert_role(posting(sid, company=company))
|
||||
"""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:
|
||||
`score` writes no rows for it under the agent that hid it."""
|
||||
for sid in "abcd":
|
||||
store.upsert_role(posting(sid))
|
||||
a, b, c, d = (posting(s).uid for s in "abcd")
|
||||
store.add_company("Hidden", key="hidden", affinity=-1)
|
||||
with store.transaction() as conn:
|
||||
conn.execute("UPDATE roles SET closed_at = ? WHERE uid = ?", (store.now(), d))
|
||||
one = store.save_agent("One")
|
||||
two = store.save_agent("Two")
|
||||
store.replace_scores(one.id, [(d, 0.99, ""), (a, 0.9, ""), (b, 0.5, "")])
|
||||
store.replace_scores(two.id, [(a, 0.95, "remote"), (c, 0.9, ""), (b, 0.8, "")])
|
||||
store.replace_scores(two.id, [(a, 0.95, "remote"), (c, 0.7, ""), (b, 0.8, "")])
|
||||
|
||||
waiting = {r.uid for r in store.summarise_waiting(10, per_agent=1)}
|
||||
|
||||
# `c` is agent two's best open ungated role but its company is hidden.
|
||||
assert waiting == {a}
|
||||
assert {r.uid for r in store.summarise_waiting(10, per_agent=2)} == {a, b}
|
||||
assert store.summarise_pending(per_agent=2) == 2
|
||||
assert waiting == {a, b}
|
||||
assert {r.uid for r in store.summarise_waiting(10, per_agent=2)} == {a, b, c}
|
||||
assert store.summarise_pending(per_agent=2) == 3
|
||||
assert [r.uid for r in store.summarise_waiting(10, 1, uids=[d])] == [d]
|
||||
Reference in new issue
Block a user