Give a search agent several prompts and score a role by its best one
Migration 14 moves each non-empty agents.intent into agent_prompts as one prompt named after the agent, with its cached vector, and drops the three intent columns. A role's fit is the maximum over the agent's prompts of the calibrated cosine, and scores.prompt names the prompt that won. The agents API takes prompts and still takes intent; the role endpoint serves the agent's score and prompt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b6afe24eaa
commit
4e3c3f130e
9 files changed
+552
-127
No files matched your search
+112
-4
@@ -11,10 +11,13 @@ import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from desk.models import Item
|
||||
import numpy as np
|
||||
|
||||
from app.models import (
|
||||
Application, Limits, TRUSTED_POSTED_HOSTS, company_key, place_key, posted_at,
|
||||
Application, Limits, Prompt, TRUSTED_POSTED_HOSTS, company_key, place_key, posted_at,
|
||||
posted_host, title_key,
|
||||
)
|
||||
from app.storage.store import pack
|
||||
from app.storage import LATEST, Store
|
||||
|
||||
|
||||
@@ -66,7 +69,7 @@ def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
|
||||
"application_events", "agents", "scores", "state"} <= tables
|
||||
assert "idx_roles_cluster" in indexes
|
||||
assert store.migrations_applied == []
|
||||
assert LATEST == 13
|
||||
assert LATEST == 14
|
||||
# A second open finds it current and applies nothing.
|
||||
assert Store(path).migrations_applied == []
|
||||
|
||||
@@ -85,11 +88,23 @@ def _columns(path: Path, table: str) -> list[tuple[str, str, int, str]]:
|
||||
return [(r[1], r[2], r[3], r[4]) for r in conn.execute(f"PRAGMA table_info({table})")]
|
||||
|
||||
|
||||
def _undo_v14(conn: sqlite3.Connection) -> None:
|
||||
"""A fresh file put back to the v13 shape: an agent's intent and its
|
||||
cached vector on the agent row, no prompts, no prompt on a score. Every
|
||||
downgrade below starts here, since v14 is the last migration."""
|
||||
conn.execute("DROP TABLE agent_prompts")
|
||||
conn.execute("ALTER TABLE scores DROP COLUMN prompt")
|
||||
conn.execute("ALTER TABLE agents ADD COLUMN intent TEXT NOT NULL DEFAULT ''")
|
||||
conn.execute("ALTER TABLE agents ADD COLUMN intent_key TEXT NOT NULL DEFAULT ''")
|
||||
conn.execute("ALTER TABLE agents ADD COLUMN intent_vec BLOB")
|
||||
|
||||
|
||||
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:
|
||||
_undo_v14(conn)
|
||||
conn.execute("DROP INDEX idx_roles_posted") # v9: indexes posted_at
|
||||
for column in ("permit", "languages", "extract_tries", "summary", "summarised_at",
|
||||
"article_text", "article_at", "read_at", "posted_at", "salary_text"):
|
||||
@@ -107,8 +122,8 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||||
|
||||
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", "role_tags",
|
||||
"agent_companies", "role_votes"):
|
||||
for table in ("roles", "agents", "agent_prompts", "scores", "vectors", "companies",
|
||||
"role_tags", "agent_companies", "role_votes"):
|
||||
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
|
||||
|
||||
|
||||
@@ -116,6 +131,7 @@ def test_a_v8_file_gains_ats_slug_and_keeps_its_companies(tmp_path: Path) -> Non
|
||||
path = tmp_path / "v8.db"
|
||||
Store(path).add_company("Acme", ats="teamtailor", country="DK")
|
||||
with sqlite3.connect(path) as conn:
|
||||
_undo_v14(conn)
|
||||
conn.execute("ALTER TABLE companies DROP COLUMN ats_slug")
|
||||
conn.execute("DROP INDEX idx_roles_posted") # v10, which follows
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN posted_at")
|
||||
@@ -134,6 +150,7 @@ 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 read state, no tags, no per-agent tables."""
|
||||
with sqlite3.connect(path) as conn:
|
||||
_undo_v14(conn)
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN read_at")
|
||||
conn.execute("DROP INDEX idx_roles_posted") # v9: indexes posted_at
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN posted_at") # v9
|
||||
@@ -245,6 +262,7 @@ def test_v10_backfills_posted_at_by_the_same_rule_upsert_uses(
|
||||
assert before_trusted is not None and before_untrusted is not None
|
||||
|
||||
with sqlite3.connect(path) as conn:
|
||||
_undo_v14(conn)
|
||||
conn.execute("DROP INDEX idx_roles_posted")
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN posted_at")
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN salary_text") # v12
|
||||
@@ -272,6 +290,7 @@ def test_v13_recomputes_country_from_place_unless_the_board_or_extract_set_it(
|
||||
for item in (wrong, several, given, extracted):
|
||||
store.upsert_role(item)
|
||||
with sqlite3.connect(path) as conn:
|
||||
_undo_v14(conn)
|
||||
conn.execute("UPDATE roles SET country = 'US' WHERE place = 'Bogota, CO'")
|
||||
conn.execute("UPDATE roles SET country = '' WHERE uid = ?", (several.uid,))
|
||||
conn.execute("UPDATE roles SET extracted_at = '2026-09-23' WHERE uid = ?",
|
||||
@@ -287,6 +306,95 @@ def test_v13_recomputes_country_from_place_unless_the_board_or_extract_set_it(
|
||||
extracted.uid: "US"}
|
||||
|
||||
|
||||
def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: Path) -> None:
|
||||
path = tmp_path / "v13.db"
|
||||
store = Store(path)
|
||||
store.upsert_role(posting("a"))
|
||||
uid = posting("a").uid
|
||||
backend = store.save_agent("Backend")
|
||||
blank = store.save_agent("Blank")
|
||||
empty = store.save_agent("Empty")
|
||||
store.replace_scores(backend.id, [(uid, 0.7, "")])
|
||||
vec = np.asarray([0.6, 0.8], dtype=np.float32)
|
||||
with sqlite3.connect(path) as conn:
|
||||
_undo_v14(conn)
|
||||
conn.execute("UPDATE agents SET intent = 'python backend', intent_key = 'k', "
|
||||
"intent_vec = ? WHERE id = ?", (pack(vec), backend.id))
|
||||
conn.execute("UPDATE agents SET intent = ' \n ' WHERE id = ?", (blank.id,))
|
||||
conn.execute("PRAGMA user_version = 13")
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [14]
|
||||
assert migrated.agent(backend.id).prompts == [ # type: ignore[union-attr]
|
||||
Prompt(name="Backend", text="python backend")]
|
||||
assert migrated.agent(blank.id).prompts == [] # type: ignore[union-attr]
|
||||
assert migrated.agent(empty.id).prompts == [] # type: ignore[union-attr]
|
||||
# The key hashes the model and the text, both unchanged: nothing to re-embed.
|
||||
cached = migrated.prompt_vector(backend.id, "Backend", "k")
|
||||
assert cached is not None and cached.tolist() == pytest.approx(vec.tolist())
|
||||
assert migrated.scores(backend.id) == {uid: (0.7, "")}
|
||||
assert migrated.role(uid, backend.id).prompt == "" # type: ignore[union-attr]
|
||||
columns = {c[0] for c in _columns(path, "agents")}
|
||||
assert not {"intent", "intent_key", "intent_vec"} & columns
|
||||
|
||||
|
||||
def test_prompts_are_saved_replaced_reordered_and_deleted(store: Store) -> None:
|
||||
ai = Prompt(name="AI leadership", text="Head of AI")
|
||||
fde = Prompt(name="Forward deployed", text="FDE lead")
|
||||
agent = store.save_agent("Wolf", prompts=[ai, fde])
|
||||
assert store.agent(agent.id).prompts == [ai, fde] # type: ignore[union-attr]
|
||||
store.set_prompt_vector(agent.id, "Forward deployed", "k", np.ones(2))
|
||||
|
||||
# Reordered and renamed, the same text keeps its vector; a new text has none.
|
||||
research = Prompt(name="Research", text="AI research")
|
||||
renamed = Prompt(name="FDE", text="FDE lead")
|
||||
store.save_agent("Wolf", agent_id=agent.id, prompts=[renamed, research])
|
||||
assert [p.name for p in store.agents()[0].prompts] == ["FDE", "Research"]
|
||||
assert store.prompt_vector(agent.id, "FDE", "k") is not None
|
||||
assert store.prompt_vector(agent.id, "Research", "k") is None
|
||||
|
||||
# A prompt may have no text; its name is what must be there, and unique.
|
||||
store.save_agent("Wolf", agent_id=agent.id, prompts=[Prompt(name="Later")])
|
||||
assert store.agents()[0].prompts == [Prompt(name="Later", text="")]
|
||||
with pytest.raises(ValueError, match="two prompts are named 'fde'"):
|
||||
store.save_agent("Wolf", agent_id=agent.id,
|
||||
prompts=[renamed, Prompt(name="fde", text="other")])
|
||||
with pytest.raises(ValidationError, match="a prompt needs a name"):
|
||||
Prompt(name=" ", text="x")
|
||||
assert store.agents()[0].prompts == [Prompt(name="Later")]
|
||||
|
||||
# Without prompts an intent is the one prompt, named after the agent.
|
||||
store.save_agent("Wolf", "python backend", agent_id=agent.id)
|
||||
assert store.agent(agent.id).prompts == [ # type: ignore[union-attr]
|
||||
Prompt(name="Wolf", text="python backend")]
|
||||
store.save_agent("Wolf", agent_id=agent.id)
|
||||
assert store.agent(agent.id).prompts == [] # type: ignore[union-attr]
|
||||
|
||||
store.save_agent("Wolf", agent_id=agent.id, prompts=[ai, fde])
|
||||
store.delete_agent(agent.id)
|
||||
with store.transaction() as conn:
|
||||
assert conn.execute("SELECT COUNT(*) FROM agent_prompts").fetchone()[0] == 0
|
||||
|
||||
|
||||
def test_an_agent_whose_prompts_are_all_empty_feeds_neither_model_stage(
|
||||
store: Store) -> None:
|
||||
store.upsert_role(posting("a"))
|
||||
store.upsert_role(posting("b"))
|
||||
a, b = posting("a").uid, posting("b").uid
|
||||
top = Limits(extract_top=1, extract_min=0, summarise_top=1, summarise_min=0)
|
||||
blank = store.save_agent("Blank", limits=top,
|
||||
prompts=[Prompt(name="One"), Prompt(name="Two", text=" \n")])
|
||||
store.replace_scores(blank.id, [(a, 0.9, "")])
|
||||
assert store.extract_waiting(10) == [] and store.summarise_waiting(10) == []
|
||||
|
||||
# One prompt with text is enough.
|
||||
store.save_agent("Blank", agent_id=blank.id, limits=top,
|
||||
prompts=[Prompt(name="One"), Prompt(name="Two", text="python")])
|
||||
assert [r.uid for r in store.extract_waiting(10)] == [a]
|
||||
assert b not in {r.uid for r in store.summarise_waiting(10)}
|
||||
|
||||
|
||||
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