From cebe62ddc9f818d22cbae13d4418cce5480e175d Mon Sep 17 00:00:00 2001 From: Wolf Date: Mon, 28 Sep 2026 16:02:06 +0200 Subject: [PATCH] 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 --- README.md | 2 +- app/config.py | 25 +++------ app/jobs/cascade.py | 7 +-- app/jobs/extract.py | 17 +++--- app/jobs/score.py | 2 +- app/jobs/summarise.py | 13 ++--- app/models.py | 20 ++++++- app/static/agents.js | 21 +++++++ app/static/app.css | 5 +- app/storage/schema.py | 10 +++- app/storage/store.py | 89 +++++++++++++++--------------- app/templates/settings/agents.html | 8 +++ app/web/api.py | 7 ++- config.example.yaml | 28 +++++----- config.yaml | 9 +-- docs/decisions.md | 4 +- tests/test_extract.py | 28 ++++++---- tests/test_storage.py | 45 +++++++++------ tests/test_summarise.py | 16 +++--- tests/test_web.py | 20 +++++++ 20 files changed, 229 insertions(+), 147 deletions(-) diff --git a/README.md b/README.md index a90d694..c46eb74 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ The cog opens `/settings`: a rail of pages on the left, the page beside it, stic Likes, hidden companies and votes belong to a search agent, not to the account: two agents rank the same roles differently, and one may hide a company the other favours. An agent also learns from votes: its `taste` weight scores a role by how close it is to the roles the agent liked minus the ones it passed. -The reader, `/roles/{uid}`, shows the full posting: the same vote and Save controls, the company's own affinity, the tags and the summary when there is one. Opening a role marks it read, and so does a vote. A search role's posting is a teaser; the reader offers a Fetch button that scrapes the original listing once and stores the text. A Summarise button asks for a short summary of that one role; `config.yaml`'s `summarise:` block turns summarising on for the pull itself, bounded to the top `per_agent` roles of each agent so the cost stays flat. +The reader, `/roles/{uid}`, shows the full posting: the same vote and Save controls, the company's own affinity, the tags and the summary when there is one. Opening a role marks it read, and so does a vote. A search role's posting is a teaser; the reader offers a Fetch button that scrapes the original listing once and stores the text. A Summarise button asks for a short summary of that one role; `config.yaml`'s `summarise:` block turns summarising on for the pull itself, bounded to the top roles of each agent by that agent's own `limits` so the cost stays flat. ## Docs diff --git a/app/config.py b/app/config.py index dda9af2..aaf2268 100644 --- a/app/config.py +++ b/app/config.py @@ -22,7 +22,7 @@ from typing import Any, Literal import yaml from pydantic import Field, ValidationError, model_validator -from app.models import Gate, Weights +from app.models import Gate, Limits, Weights from auth import AuthConfig from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig @@ -43,37 +43,27 @@ class ExtractConfig(ModelTaskConfig): """Turning a posting's title and text into the gate columns. See app/jobs/extract.py. Off by default, so nobody is billed by surprise: on the API backend every batch is a paid call. On, it reads only the - best `per_agent` roles of each enabled agent that score at least - `min_score`, so the cost follows the ranking rather than the boards.""" + best roles of each enabled agent, as deep and as low as that agent's + own `limits` say, so the cost follows the ranking rather than the + boards.""" enabled: bool = False model: str = "claude-haiku-4-5-20251001" batch_size: int = Field(20, ge=1) - #: How deep into each enabled agent's ranking extract reads: the backstop. - per_agent: int = Field(500, ge=0) - #: Below this score a role is not read, however few are above it. Loose, - #: since a fresh role with the worst fit scores about 0.27 on the default - #: weights and a good one 0.6 or more. - min_score: float = Field(0.3, ge=0, le=1) class SummariseConfig(ModelTaskConfig): """About 60 words on a role, for the list and the read page. See app/jobs/summarise.py. Off by default like extract; on, a pull summarises - only the best `per_agent` of each enabled agent, so the cost is bounded - whatever the backlog. The read page's Summarise button asks for one role - whether this is on or not.""" + only the best of each enabled agent, as deep and as low as that agent's + own `limits` say, so the cost is bounded whatever the backlog. The read + page's Summarise button asks for one role whether this is on or not.""" enabled: bool = False model: str = "claude-haiku-4-5-20251001" #: A description is capped at 8000 characters per role, so ten is about #: 20k tokens of input per call. batch_size: int = Field(10, ge=1) - #: How deep into each enabled agent's ranking a pull summarises. - per_agent: int = Field(30, ge=0) - #: Below this score a role is not summarised. Stricter than extract's, - #: since a summary is the expensive answer. - min_score: float = Field(0.5, ge=0, le=1) class EmbedConfig(Section): @@ -96,6 +86,7 @@ class AgentSeed(Section): intent: str = "" gates: list[Gate] = Field(default_factory=list) weights: Weights = Field(default_factory=Weights) + limits: Limits = Field(default_factory=Limits) enabled: bool = True diff --git a/app/jobs/cascade.py b/app/jobs/cascade.py index a4e0f15..f74beea 100644 --- a/app/jobs/cascade.py +++ b/app/jobs/cascade.py @@ -12,10 +12,9 @@ nothing: `extract.enabled: false`, or an `on_ingest` this pull does not satisfy, is an extract stage with no batches whose final job moves straight on. The free stages go first: embed and score cost no tokens, and the score they produce is what bounds the two that do. Extract reads only the best -`extract.per_agent` roles of each enabled agent, not below -`extract.min_score`; the second score then applies the gates to the columns -it filled. Summarise runs last, on the best `summarise.per_agent` roles not -below `summarise.min_score`. +roles of each enabled agent, as deep and as low as that agent's own `limits` +say; the second score then applies the gates to the columns it filled. +Summarise runs last, on the best roles by the same agent's summarise limits. A batch that failed, or a stage somebody killed, does not stop the chain: the work comes round on the next pull, and a rescore of what did arrive is worth diff --git a/app/jobs/extract.py b/app/jobs/extract.py index 19f5c13..91e16e5 100644 --- a/app/jobs/extract.py +++ b/app/jobs/extract.py @@ -8,9 +8,10 @@ refused batch, and failing with a notice when no model is reachable are `desk.modeltask`'s. A role is waiting while it is open, has never been answered (`extracted_at IS -NULL`), has not been given up on, and is in the best `extract.per_agent` of an -enabled agent's ungated scores at `extract.min_score` or more. That bound is -what keeps the cost proportional to the rankings rather than to the boards: +NULL`), has not been given up on, and is in the best of an enabled agent's +ungated scores as deep and as low as that agent's own `limits` say +(`extract_top`, `extract_min`). That bound is what keeps the cost +proportional to the rankings rather than to the boards: the pull scores every role for free first, then reads the few hundred that rank, and scores again so the gates see what was filled. A batch built for named uids finishes what it was dealt whatever their rank. The answer only @@ -133,8 +134,7 @@ def _item(role: Role) -> dict[str, Any]: def build_batch(config: Config, store: Store, limit: int = 20, uids: Sequence[str] | None = None) -> dict[str, Any]: return {"instructions": SYSTEM, - "roles": [_item(r) for r in store.extract_waiting( - limit, config.extract.per_agent, config.extract.min_score, uids)]} + "roles": [_item(r) for r in store.extract_waiting(limit, uids)]} # -------------------------------------------------------------------- answer @@ -205,11 +205,8 @@ TASK: ModelTask[ExtractResult] = ModelTask( kind="extract", label="Reading postings for gate fields", verb="extracting", noun="fields", idle="nothing to extract", items="roles", settings=lambda config: config.extract, - pending=lambda config, store: store.extract_pending( - config.extract.per_agent, config.extract.min_score), - waiting=lambda config, store, limit: [ - r.uid for r in store.extract_waiting( - limit, config.extract.per_agent, config.extract.min_score)], + pending=lambda config, store: store.extract_pending(), + waiting=lambda config, store, limit: [r.uid for r in store.extract_waiting(limit)], build_batch=build_batch, system=lambda store: SYSTEM, schema=SCHEMA, diff --git a/app/jobs/score.py b/app/jobs/score.py index 02410c5..335bea4 100644 --- a/app/jobs/score.py +++ b/app/jobs/score.py @@ -276,7 +276,7 @@ def seed_agents(config: Config, store: Store) -> list[str]: for seed in config.agents: try: store.save_agent(seed.name, seed.intent, seed.gates, seed.weights, - seed.enabled) + seed.enabled, limits=seed.limits) except ValueError as exc: log.warning("agents: %s", exc) continue diff --git a/app/jobs/summarise.py b/app/jobs/summarise.py index c72d9d7..f0f30db 100644 --- a/app/jobs/summarise.py +++ b/app/jobs/summarise.py @@ -6,9 +6,10 @@ what the model is told, the schema it answers in, what to do with the answer), and running it is `desk.modeltask`'s. A role is waiting while it has no summary, has some text, and is in the top -`summarise.per_agent` of an enabled agent's ungated scores at -`summarise.min_score` or more. That bound is what keeps the cost flat: a pull -summarises the roles somebody will look at, not the 30k that are open. A batch built for named uids (the read page's Summarise +of an enabled agent's ungated scores as deep and as low as that agent's own +`limits` say (`summarise_top`, `summarise_min`). That bound is what keeps the +cost flat: a pull summarises the roles somebody will look at, not the 30k +that are open. A batch built for named uids (the read page's Summarise button) takes those roles whatever their rank. See `Store.summarise_waiting`. The text is `article_text` when the posting page was fetched, else @@ -121,8 +122,7 @@ def _item(role: Role) -> dict[str, Any]: def _waiting(config: Config, store: Store, limit: int, uids: Sequence[str] | None = None) -> list[Role]: - return store.summarise_waiting(limit, config.summarise.per_agent, uids, - min_score=config.summarise.min_score) + return store.summarise_waiting(limit, uids) def build_batch(config: Config, store: Store, limit: int = 10, @@ -166,8 +166,7 @@ TASK: ModelTask[SummariseResult] = ModelTask( verb="summarising", noun="summaries", idle="nothing to summarise", items="summaries", settings=lambda config: config.summarise, - pending=lambda config, store: store.summarise_pending( - config.summarise.per_agent, config.summarise.min_score), + pending=lambda config, store: store.summarise_pending(), waiting=lambda config, store, limit: [r.uid for r in _waiting(config, store, limit)], build_batch=build_batch, system=lambda store: SYSTEM, diff --git a/app/models.py b/app/models.py index daf276f..5336b67 100644 --- a/app/models.py +++ b/app/models.py @@ -388,15 +388,31 @@ class Weights(Model): taste: float = Field(0.2, ge=0) +class Limits(Model): + """How far down an agent's ranking the two model stages read. Each pair + is a top N, the backstop, and a minimum score, the real stop: a role + is read when it is among the agent's best `extract_top` ungated scores + and not below `extract_min`, and summarised likewise. A fresh role with + the worst fit scores about 0.27 on the default weights, a good one 0.6 + or more, so extract's floor is loose and summarise's, the long answer, + stricter.""" + + extract_top: int = Field(500, ge=0) + extract_min: float = Field(0.3, ge=0, le=1) + summarise_top: int = Field(30, ge=0) + summarise_min: float = Field(0.5, ge=0, le=1) + + class Agent(Model): - """A row of `agents`: a named search with its own intent, gates and - weights. The cached intent vector is a column this does not declare.""" + """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.""" id: int name: str intent: str = "" 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() enabled: bool = True created_at: str diff --git a/app/static/agents.js b/app/static/agents.js index f095ffd..9274b49 100644 --- a/app/static/agents.js +++ b/app/static/agents.js @@ -20,6 +20,10 @@ const $ = (sel, root = document) => root.querySelector(sel); const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2, taste: 0.2 }; + // What the model reads after a pull, per agent: the best `extract_top` + // roles scoring at least `extract_min` get their gate columns filled, the + // best `summarise_top` at `summarise_min` or more a summary. + const DEFAULT_LIMITS = { extract_top: 500, extract_min: 0.3, summarise_top: 30, summarise_min: 0.5 }; const NEW = "new"; /** @type {object[]} every agent, as the server lists them */ @@ -108,6 +112,11 @@ return `fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}${gates}`; } + function limitsLine(row) { + const l = Object.assign({}, DEFAULT_LIMITS, row.limits); + return `reads ${l.extract_top} at ${l.extract_min} · summarises ${l.summarise_top} at ${l.summarise_min}`; + } + function card(row, fallback) { const div = el("article", "agent-card"); if (!row.enabled) div.dataset.off = "true"; @@ -127,6 +136,7 @@ ? el("p", "agent-intent", intent) : el("p", "agent-intent empty", "Nothing described yet, so fit has nothing to measure against. Edit says what this agent looks for.")); div.appendChild(el("p", "agent-weights", weightsLine(row))); + div.appendChild(el("p", "agent-limits", limitsLine(row))); const actions = el("div", "agent-card-actions"); const edit = el("button", "secondary", "Edit"); @@ -151,6 +161,7 @@ const div = el("article", "agent-card editing"); const f = $("#agent-form-template").content.querySelector("form").cloneNode(true); const weights = Object.assign({}, DEFAULT_WEIGHTS, row ? row.weights : {}); + const limits = Object.assign({}, DEFAULT_LIMITS, row ? row.limits : {}); $("#agent-form-title", f).textContent = row ? `Edit ${row.name}` : "New agent"; $("#a-name", f).value = row ? row.name : ""; $("#a-intent", f).value = row ? row.intent : ""; @@ -159,6 +170,10 @@ $("#a-recency", f).value = weights.recency; $("#a-affinity", f).value = weights.affinity; $("#a-taste", f).value = weights.taste; + $("#a-extract-top", f).value = limits.extract_top; + $("#a-extract-min", f).value = limits.extract_min; + $("#a-summarise-top", f).value = limits.summarise_top; + $("#a-summarise-min", f).value = limits.summarise_min; $("#a-delete", f).hidden = !row; f.addEventListener("input", () => { f.dataset.dirty = "true"; }); f.addEventListener("submit", (event) => save(event, row)); @@ -227,6 +242,12 @@ affinity: Number($("#a-affinity", f).value), taste: Number($("#a-taste", f).value), }, + limits: { + extract_top: Number($("#a-extract-top", f).value), + extract_min: Number($("#a-extract-min", f).value), + summarise_top: Number($("#a-summarise-top", f).value), + summarise_min: Number($("#a-summarise-min", f).value), + }, // Saving an agent means it is wanted, so a new one starts on; the // card's switch is where an existing one is switched. enabled: row ? row.enabled : true, diff --git a/app/static/app.css b/app/static/app.css index 98d22de..05d02f2 100644 --- a/app/static/app.css +++ b/app/static/app.css @@ -1083,7 +1083,7 @@ table.applications td[data-label="Saved"] { white-space: nowrap; color: var(--di white-space: pre-line; overflow-wrap: anywhere; } .agent-intent.empty { font-family: inherit; font-size: .9rem; color: var(--dim); font-style: italic; } -.agent-weights { margin: 0; font-size: .8rem; color: var(--faint); font-variant-numeric: tabular-nums; } +.agent-weights, .agent-limits { margin: 0; font-size: .8rem; color: var(--faint); font-variant-numeric: tabular-nums; } .agent-card-actions { display: flex; align-items: center; flex-wrap: wrap; gap: .5rem 1rem; margin-top: .8rem; } .agent-card-actions a { font-size: .9rem; font-weight: 600; text-decoration: none; } @media (hover: hover) { @@ -1147,6 +1147,9 @@ table.applications td[data-label="Saved"] { white-space: nowrap; color: var(--di .weights { display: flex; gap: .75rem; flex-wrap: wrap; border: 1px solid var(--line); border-radius: .3rem; } /* A label that wraps to two lines pushes its box down; the boxes stay in line. */ .weights label { flex: 1 1 6rem; justify-content: space-between; } +/* The limits are a pair per stage, count then score, so two to a row. */ +.weights.limits label { flex: 1 1 40%; } +.agent-form fieldset + .hint { margin-top: -.3rem; } .agent-actions { display: flex; gap: .5rem; flex-wrap: wrap; } .agent-form details summary { cursor: pointer; color: var(--dim); font-size: .9rem; } .agent-form details[open] summary { margin-bottom: .5rem; } diff --git a/app/storage/schema.py b/app/storage/schema.py index f23a8cf..75d7711 100644 --- a/app/storage/schema.py +++ b/app/storage/schema.py @@ -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 '{}'; """), ) diff --git a/app/storage/store.py b/app/storage/store.py index 59ac571..6c36787 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -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: diff --git a/app/templates/settings/agents.html b/app/templates/settings/agents.html index b6e981f..08e0dae 100644 --- a/app/templates/settings/agents.html +++ b/app/templates/settings/agents.html @@ -30,6 +30,14 @@ +
+ What the model reads + + + + +
+ Every pull scores every open role for free; these bound what the model then reads, per agent. The count is the backstop, the score the real stop. A new role with the worst fit scores about 0.27, a good one 0.6 or more.
Advanced