Extract and summarise bounds are each agent's own limits, set on its form

agents.limits holds extract_top, extract_min, summarise_top and
summarise_min; the scope query reads them per ranked row, and a row saved
before this behaves like a fresh agent. The four global config keys go.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-09-28 16:02:06 +02:00
1 parent e602eb3388
commit cebe62ddc9
20 files changed
+229 -147

No files matched your search

+8 -2
View File
@@ -167,7 +167,9 @@ END;
-- A search agent: a named saved search with its own intent text, gates and
-- weights. gates is a list of {field, values, mode: require|prefer, weight}.
-- intent_vec caches the intent's vector; intent_key is sha1 of the embedding
-- model and the intent text, so editing either makes it stale.
-- model and the intent text, so editing either makes it stale. limits is how
-- deep into this agent's ranking extract and summarise read, as JSON: a key
-- it lacks means the `Limits` default.
CREATE TABLE IF NOT EXISTS agents (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
@@ -177,7 +179,8 @@ CREATE TABLE IF NOT EXISTS agents (
enabled INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL,
intent_key TEXT NOT NULL DEFAULT '',
intent_vec BLOB
intent_vec BLOB,
limits TEXT NOT NULL DEFAULT '{}'
);
-- One agent's stance on a company: -1 hidden, 0 neutral, 1 liked, 2
@@ -331,6 +334,9 @@ ALTER TABLE companies DROP COLUMN affinity;
ALTER TABLE companies ADD COLUMN ats TEXT NOT NULL DEFAULT '';
ALTER TABLE companies ADD COLUMN country TEXT NOT NULL DEFAULT '';
ALTER TABLE companies ADD COLUMN city TEXT NOT NULL DEFAULT '';
"""),
(8, "agents.limits: how deep into its ranking the model stages read is each agent's own", """
ALTER TABLE agents ADD COLUMN limits TEXT NOT NULL DEFAULT '{}';
"""),
)
+46 -43
View File
@@ -14,7 +14,7 @@ from collections.abc import Callable, Iterator, MutableMapping, Sequence
from contextlib import closing, contextmanager
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any
from typing import Any, Literal
from uuid import uuid4
import numpy as np
@@ -22,8 +22,8 @@ import numpy as np
from desk.models import Item
from app.models import (
AFFINITY, REMOTE, STATUSES, Agent, Application, ApplicationEvent, Candidate,
Company, Gate, Role, ScoredRole, Subscription, Weights, company_key, country_of,
place_key, title_key,
Company, Gate, Limits, Role, ScoredRole, Subscription, Weights, company_key,
country_of, place_key, title_key,
)
from app.storage import schema
@@ -619,24 +619,29 @@ class Store:
# -------------------------------------------------------------- extract
def _top_scope(self, per_agent: int, min_score: float) -> tuple[str, list[Any]]:
"""The `WHERE` fragment for a role among the `per_agent` best ungated
scores of an enabled agent, not below `min_score`: what the two model
stages, extract and summarise, are bounded to. The role is `r`. A
company an agent hid needs no clause: `score` writes no rows for it
under that agent."""
return ("""r.uid IN (SELECT uid FROM (
SELECT s.uid, s.score, ROW_NUMBER() OVER (
def _top_scope(self, stage: Literal["extract", "summarise"]) -> tuple[str, list[Any]]:
"""The `WHERE` fragment for a role among the best ungated scores of
an enabled agent, as deep and as low as that agent's own `limits`
say for `stage`: what the two model stages, extract and summarise,
are bounded to. The rank is numbered per agent and the agent's
limits ride out of the window subquery beside it, so each row is
held to the limits of the agent that ranked it; a key the JSON lacks
is the `Limits` default. The role is `r`. A company an agent hid
needs no clause: `score` writes no rows for it under that agent."""
defaults = Limits()
return (f"""r.uid IN (SELECT uid FROM (
SELECT s.uid, s.score, a.limits, ROW_NUMBER() OVER (
PARTITION BY s.agent_id
ORDER BY s.score DESC, top.first_seen DESC, s.uid) AS n
FROM scores s
JOIN agents a ON a.id = s.agent_id AND a.enabled = 1
JOIN roles top ON top.uid = s.uid AND top.closed_at IS NULL
WHERE s.gated_reason = '')
WHERE n <= ? AND score >= ?)""", [per_agent, min_score])
WHERE n <= COALESCE(json_extract(limits, '$.{stage}_top'), ?)
AND score >= COALESCE(json_extract(limits, '$.{stage}_min'), ?))""",
[getattr(defaults, f"{stage}_top"), getattr(defaults, f"{stage}_min")])
def _extract_scope(self, per_agent: int, min_score: float,
uids: Sequence[str] | None) -> tuple[str, list[Any]]:
def _extract_scope(self, uids: Sequence[str] | None) -> tuple[str, list[Any]]:
"""The `WHERE` for a role `extract` may read: open, never answered,
not given up. Given `uids`, those of them and nothing else, since a
batch asks again when it runs and must finish what it was dealt;
@@ -646,21 +651,21 @@ class Store:
if uids is not None:
where.append("r.uid IN (SELECT value FROM json_each(?))")
return " AND ".join(where), [json.dumps(list(uids))]
top, params = self._top_scope(per_agent, min_score)
top, params = self._top_scope("extract")
return " AND ".join([*where, top]), params
def extract_pending(self, per_agent: int, min_score: float = 0.0) -> int:
where, params = self._extract_scope(per_agent, min_score, None)
def extract_pending(self) -> int:
where, params = self._extract_scope(None)
row = self._row(f"SELECT COUNT(*) AS n FROM roles r WHERE {where}", params)
return int(row["n"]) if row else 0
def extract_waiting(self, limit: int, per_agent: int, min_score: float = 0.0,
uids: Sequence[str] | None = None) -> list[Role]:
"""Roles `extract` has yet to read, newest first: the best
`per_agent` of every enabled agent's scores not below `min_score`, so
the backlog follows the rankings rather than the boards. Given
`uids`, only those of them still waiting, whatever their rank."""
where, params = self._extract_scope(per_agent, min_score, uids)
def extract_waiting(self, limit: int, uids: Sequence[str] | None = None) -> list[Role]:
"""Roles `extract` has yet to read, newest first: the best of every
enabled agent's scores, as deep and as low as that agent's own
`limits` say, so the backlog follows the rankings rather than the
boards. Given `uids`, only those of them still waiting, whatever
their rank."""
where, params = self._extract_scope(uids)
return [Role.from_row(r) for r in self._rows(
f"SELECT r.* FROM roles r WHERE {where} "
"ORDER BY r.first_seen DESC, r.uid LIMIT ?", [*params, limit])]
@@ -722,8 +727,7 @@ class Store:
#: roles in some agent's top list.
_SUMMARY_MISSED = "summarise.missed:"
def _summary_scope(self, per_agent: int, min_score: float,
uids: Sequence[str] | None) -> tuple[str, list[Any]]:
def _summary_scope(self, uids: Sequence[str] | None) -> tuple[str, list[Any]]:
"""The `WHERE` for a role `summarise` may be sent: no summary yet and
some text to summarise. Given `uids`, those of them and nothing else,
open or not and given up or not, because somebody asked for them.
@@ -733,7 +737,7 @@ class Store:
if uids is not None:
where.append("r.uid IN (SELECT value FROM json_each(?))")
return " AND ".join(where), [json.dumps(list(uids))]
top, params = self._top_scope(per_agent, min_score)
top, params = self._top_scope("summarise")
where += [
"r.closed_at IS NULL",
f"""NOT EXISTS (SELECT 1 FROM state
@@ -743,20 +747,18 @@ class Store:
]
return " AND ".join(where), params
def summarise_pending(self, per_agent: int, min_score: float = 0.0) -> int:
where, params = self._summary_scope(per_agent, min_score, None)
def summarise_pending(self) -> int:
where, params = self._summary_scope(None)
row = self._row(f"SELECT COUNT(*) AS n FROM roles r WHERE {where}", params)
return int(row["n"]) if row else 0
def summarise_waiting(self, limit: int, per_agent: int,
uids: Sequence[str] | None = None,
min_score: float = 0.0) -> list[Role]:
"""Roles `summarise` has yet to write, newest first. The top
`per_agent` of every enabled agent's scores not below `min_score`, so
the backlog is bounded by the number of agents rather than by the
number of open roles; given `uids`, those still without a summary,
whatever their rank. A batch asks again when it runs."""
where, params = self._summary_scope(per_agent, min_score, uids)
def summarise_waiting(self, limit: int, uids: Sequence[str] | None = None) -> list[Role]:
"""Roles `summarise` has yet to write, newest first: the top of every
enabled agent's scores, as deep and as low as that agent's own
`limits` say, so the backlog is bounded by the agents rather than by
the number of open roles; given `uids`, those still without a
summary, whatever their rank. A batch asks again when it runs."""
where, params = self._summary_scope(uids)
return [Role.from_row(r) for r in self._rows(
f"SELECT r.* FROM roles r WHERE {where} "
"ORDER BY r.first_seen DESC, r.uid LIMIT ?", [*params, limit])]
@@ -866,24 +868,25 @@ class Store:
def save_agent(self, name: str, intent: str = "", gates: Sequence[Gate] = (),
weights: Weights | None = None, enabled: bool = True,
agent_id: int | None = None) -> Agent:
agent_id: int | None = None, limits: Limits | None = None) -> Agent:
"""Create an agent, or with `agent_id` replace that one's fields.
Raises ValueError on an empty or taken name, KeyError on an unknown id."""
name = name.strip()
if not name:
raise ValueError("an agent needs a name")
fields = (name, intent, json.dumps([g.model_dump() for g in gates]),
(weights or Weights()).model_dump_json(), int(enabled))
(weights or Weights()).model_dump_json(),
(limits or Limits()).model_dump_json(), int(enabled))
try:
with self.transaction() as conn:
if agent_id is None:
agent_id = conn.execute(
"""INSERT INTO agents (name, intent, gates, weights, enabled,
created_at) VALUES (?,?,?,?,?,?)""",
"""INSERT INTO agents (name, intent, gates, weights, limits,
enabled, created_at) VALUES (?,?,?,?,?,?,?)""",
(*fields, self.now())).lastrowid
elif not conn.execute(
"""UPDATE agents SET name=?, intent=?, gates=?, weights=?,
enabled=? WHERE id=?""",
limits=?, enabled=? WHERE id=?""",
(*fields, agent_id)).rowcount:
raise KeyError(agent_id)
except sqlite3.IntegrityError as exc: