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:
WolfandClaude Opus 5.5 committed 2026-09-29 16:19:57 +02:00
1 parent b6afe24eaa
commit 4e3c3f130e
9 files changed
+552 -127

No files matched your search

+42 -4
View File
@@ -15,7 +15,7 @@ import re
import unicodedata
from typing import Annotated, Any, Literal, get_args
from pydantic import BeforeValidator, Field, computed_field
from pydantic import AfterValidator, BeforeValidator, Field, computed_field, field_validator
from desk.models import JsonDict, Model, ModelTaskResult
@@ -828,6 +828,9 @@ class ScoredRole(Role):
score: float | None = None
gated_reason: str = ""
#: The agent's prompt that gave the role its fit; "" when fit did not
#: count (gated, no vector, no prompt with text).
prompt: str = ""
#: How many characters `RoleRow.excerpt` keeps at most: the feed row's own
@@ -881,6 +884,7 @@ class RoleRow(Model):
posted_at: str = ""
score: float | None = None
gated_reason: str = ""
prompt: str = ""
vote: Literal[-1, 0, 1] | None = None
voted_at: str | None = None
read_at: str | None = None
@@ -1016,13 +1020,47 @@ class Limits(Model):
summarise_min: float = Field(0.65, ge=0, le=1)
class Prompt(Model):
"""One of an agent's descriptions of what it looks for. Each is embedded
on its own and a role's fit is its best prompt's (see `app.jobs.score`),
so one agent can hunt several kinds of role without blending them. A
prompt with no text has no fit to give."""
name: str
text: str = ""
@field_validator("name")
@classmethod
def _named(cls, name: str) -> str:
name = name.strip()
if not name:
raise ValueError("a prompt needs a name")
return name
def check_prompts(prompts: list[Prompt]) -> list[Prompt]:
"""The prompts as they are, or ValueError when two share a name
regardless of case."""
seen: set[str] = set()
for prompt in prompts:
if prompt.name.casefold() in seen:
raise ValueError(f"two prompts are named {prompt.name!r}")
seen.add(prompt.name.casefold())
return prompts
#: An agent's prompts, in order, their names unique regardless of case.
Prompts = Annotated[list[Prompt], AfterValidator(check_prompts)]
class Agent(Model):
"""A row of `agents`: a named search with its own intent, gates, weights
and limits. The cached intent vector is a column this does not declare."""
"""A row of `agents`: a named search with its own prompts, gates, weights
and limits. The prompts are rows of `agent_prompts`, read alongside; their
cached vectors are columns this does not declare."""
id: int
name: str
intent: str = ""
prompts: Prompts = []
gates: Annotated[list[Gate], BeforeValidator(lambda v: _json(v, "[]"))] = []
weights: Annotated[Weights, BeforeValidator(lambda v: _json(v, "{}"))] = Weights()
limits: Annotated[Limits, BeforeValidator(lambda v: _json(v, "{}"))] = Limits()