From 617356e60c032c3f2feaac1b8919258cdb2317d6 Mon Sep 17 00:00:00 2001 From: Wolf Date: Sun, 4 Oct 2026 15:11:43 +0200 Subject: [PATCH 1/2] Rank by the cut: fetch gate overrides in scoring, the cut per agent, stages read the cut, fetch text renamed from enrich, the feed kept to the fetch gate, and migration 32 Co-Authored-By: Claude Opus 5.5 --- app/cli.py | 6 +- app/config.py | 35 +-- app/jobs/cascade.py | 98 ++++--- app/jobs/extract.py | 13 +- app/jobs/{enrich.py => fetch_text.py} | 66 +++-- app/jobs/judge.py | 22 +- app/jobs/posting.py | 2 +- app/jobs/prep.py | 2 +- app/jobs/score.py | 199 ++++++++----- app/jobs/stages.py | 25 +- app/jobs/summarise.py | 13 +- app/models.py | 88 +++--- app/storage/schema.py | 105 ++++++- app/storage/store.py | 292 +++++++++++-------- app/web/api.py | 98 +++++-- config.example.yaml | 88 +++--- config.yaml | 5 +- plugins/ats.py | 6 +- plugins/readers/__init__.py | 12 +- tests/test_api_reach.py | 4 +- tests/test_companies.py | 4 +- tests/test_cv_feed.py | 9 +- tests/test_cv_fields.py | 22 +- tests/test_cv_fit.py | 59 ++-- tests/test_dupes_storage.py | 5 +- tests/test_extract.py | 52 ++-- tests/{test_enrich.py => test_fetch_text.py} | 134 +++++---- tests/test_gazetteer.py | 1 - tests/test_judge.py | 36 ++- tests/test_prep.py | 2 +- tests/test_remote.py | 6 +- tests/test_score.py | 158 +++++----- tests/test_stages.py | 21 +- tests/test_storage.py | 81 ++--- tests/test_web.py | 36 ++- 35 files changed, 1054 insertions(+), 751 deletions(-) rename app/jobs/{enrich.py => fetch_text.py} (75%) rename tests/{test_enrich.py => test_fetch_text.py} (78%) diff --git a/app/cli.py b/app/cli.py index 19ca597..1d227cf 100644 --- a/app/cli.py +++ b/app/cli.py @@ -6,7 +6,7 @@ app companies seed|add|edit|checked|remove|by-hand|set-board|detach|discover|check|size app searches add|list|remove app agents list|prompt - app stages [extract|summarise|judge|enrich on|off] + app stages [extract|summarise|judge|fetch_text on|off] app dupes app prep [unmapped|unlinked] [--remap] app db init @@ -177,8 +177,8 @@ def build_parser() -> argparse.ArgumentParser: prompt.add_argument("--remove", action="store_true", help="remove the prompt instead") stages = sub.add_parser( - "stages", help="show whether extract, summarise, judge and enrich run, or switch one") - stages.add_argument("name", nargs="?", choices=["extract", "summarise", "judge", "enrich"]) + "stages", help="show whether extract, summarise, judge and fetch_text run, or switch one") + stages.add_argument("name", nargs="?", choices=["extract", "summarise", "judge", "fetch_text"]) stages.add_argument("state", nargs="?", choices=["on", "off"]) sub.add_parser("seniority", help="per host, how often the stated level equals the model's") diff --git a/app/config.py b/app/config.py index a8f7f4d..85867f9 100644 --- a/app/config.py +++ b/app/config.py @@ -44,9 +44,8 @@ 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 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.""" + roles in the cut of an enabled agent (`app.jobs.score`), so the cost + follows the agents rather than the boards.""" enabled: bool = False model: str = "claude-haiku-4-5-20251001" @@ -56,9 +55,9 @@ class ExtractConfig(ModelTaskConfig): 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 - 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, and every - role that is saved or liked, whatever its rank. The read page's Summarise button asks for one + the best `summarise_top` final scores of each enabled agent (its + `limits`), so the cost is bounded whatever the backlog, and every role + that is saved or liked, whatever its rank. The read page's Summarise button asks for one role whether this is on or not.""" enabled: bool = False @@ -69,12 +68,10 @@ class SummariseConfig(ModelTaskConfig): class JudgeConfig(ModelTaskConfig): - """Each search agent's best roles read against its CV, as three numbers - and a total. See app/jobs/judge.py. Off by default like extract; on, a - pull judges the best of each enabled agent with a CV, as deep and as low - as that agent's own `limits` say (`judge_top`, `judge_min`), and the - `judge_cv_top` ungated ones closest to its CV that those leave out, and - only the roles whose judgement is missing or was read from another CV or + """Each search agent's cut read against its CV, as three numbers and a + total. See app/jobs/judge.py. Off by default like extract; on, a pull + judges the cut of each enabled agent with a CV and a prompt, and only + the roles whose judgement is missing or was read from another CV or text.""" enabled: bool = False @@ -84,17 +81,13 @@ class JudgeConfig(ModelTaskConfig): batch_size: int = Field(5, ge=1) -class EnrichConfig(Section): +class FetchTextConfig(Section): """Reading roles on their own pages after scoring, for the hosts whose - list carries no text. See app/jobs/enrich.py. A role is read when it has - no text and an enabled agent with a prompt ranks it ungated among its - best `top` and at `min_score` or above: the extract bar (`Limits`, - 500 and 0.55) with the score a notch lower, since a role ranked on its - title alone is the one whose score its text moves most.""" + list carries no text. See app/jobs/fetch_text.py. A role is read when it + has no text and is in the cut of an enabled agent (`app.jobs.score`), so + every role the model reads has its text first.""" enabled: bool = True - top: int = Field(500, ge=0) - min_score: float = Field(0.5, ge=0, le=1) #: Reads per board and per run. Per board is the cap the pull had; the #: boards are read in parallel on the `http` pool. per_board: int = Field(200, ge=0) @@ -253,7 +246,7 @@ class Config(Section): summarise: SummariseConfig = Field(default_factory=SummariseConfig) judge: JudgeConfig = Field(default_factory=JudgeConfig) embed: EmbedConfig = Field(default_factory=EmbedConfig) - enrich: EnrichConfig = Field(default_factory=EnrichConfig) + fetch_text: FetchTextConfig = Field(default_factory=FetchTextConfig) #: Starter agents, imported once into an empty table. agents: list[AgentSeed] = Field(default_factory=list) #: Starter searches, imported once into an empty set of `search` diff --git a/app/jobs/cascade.py b/app/jobs/cascade.py index fa4e244..c6b3ba6 100644 --- a/app/jobs/cascade.py +++ b/app/jobs/cascade.py @@ -1,32 +1,29 @@ """The tasks roledesk offers, and the order a pull runs them in. - pull → prep → embed → score → enrich → prep → embed → score → extract → score - → summarise → judge + pull → prep → embed → score → fetch_text → prep → embed → score → extract + → score → judge → summarise newsdesk's cascade convention: a cascade is a tuple of task names, and the final job of one task starts the next (`step` is the callback it carries). Nothing is alive between two tasks. `prep` (`app/jobs/prep.py`) maps what the pull stored and folds the copies before anything reads the roles; after -`enrich` it maps the roles their own pages filled. A stage is addressed by its position in -the tuple, not its name, since `score` runs twice. +`fetch_text` it maps the roles their own pages filled. A stage is addressed +by its position in the tuple, not its name, since `score` runs three times. Every stage is always in the chain, and a stage that is switched off finds -nothing: a stage switched off (`stages.stage_on`), 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 rest. Enrich reads the best roles without -text on their own pages (`app/jobs/enrich.py`), and prep, embed and score -follow for the roles it filled; when it filled none, `step` passes over all -three. Extract -reads only the best -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 follows, on the best roles by the same agent's summarise limits, -and judge runs last, reading each agent's best roles against its CV by its -judge limits. The `enrich` cascade, which follows the rescore after an agent -is saved, ends with judge too, so a changed CV, prompt or like text is read -again at once rather than at the next pull; a pair whose `input_key` did not -change costs nothing. +nothing: a stage switched off (`stages.stage_on`), or an `on_ingest` this +pull does not satisfy, is a stage with no batches whose final job moves +straight on. The free stages go first: embed and score cost no tokens, and +score writes each agent's cut (`app.jobs.score`), which bounds the rest. +Fetch text reads the role's own page for the roles in the cut that came +without text (`app/jobs/fetch_text.py`), and prep, embed and score follow +for the roles it filled, so their levels and the cut are recomputed; when +it filled none, `step` passes over all three. Extract reads the cut; the +score after it applies the fetch gate and the cut again with the columns +extract filled, so a role can drop out or enter (one that enters is +extracted on the next pull). Judge reads each agent's cut as recomputed, +against its CV. Summarise runs last, on the top of each agent's feed by +final score and the roles liked or saved: it is no part of any score. 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 @@ -36,10 +33,13 @@ having either way. and a cascade is started by one: the `pull` job's only work is to ask for the chain. Its default schedule, `every 6h`, is declared here beside it. -`enrich` is a job and a cascade the same way, `enrich → prep → embed → score`, queued -behind the `score` that follows a change to the ranking other than a pull's -(`enrich_after`): an agent saved, a company's affinity changed. A vote moves -one agent's taste a little and queues neither. +`fetch_text` is a job and a cascade the same way, the pull's chain from +fetch text on, queued behind the `score` that follows a change to the +ranking other than a pull's (`fetch_text_after`): an agent saved, a +company's affinity or overrides changed. The cut may have changed, so its +new roles get their text, extract and judge at once rather than at the next +pull; a judgement whose `input_key` did not change costs nothing. A vote +moves one agent's taste a little and queues neither. """ from __future__ import annotations @@ -49,19 +49,22 @@ from dataclasses import replace from functools import partial from typing import Any -from app.jobs import embed, enrich, extract, judge, prep, pull, score, summarise # noqa: F401 -- registers the jobs +from app.jobs import ( # noqa: F401 -- registers the jobs + embed, extract, fetch_text, judge, prep, pull, score, summarise, +) from desk.tools import agent from pipeline import NULL, TASKS, Notice, RunContext, Tally, Task, declare, job STEP = "app.jobs.cascade:step" CASCADES: dict[str, tuple[str, ...]] = { - "pull": ("pull", "prep", "embed", "score", "enrich", "prep", "embed", "score", "extract", - "score", "summarise", "judge"), - "enrich": ("enrich", "prep", "embed", "score", "judge"), + "pull": ("pull", "prep", "embed", "score", "fetch_text", "prep", "embed", "score", + "extract", "score", "judge", "summarise"), + "fetch_text": ("fetch_text", "prep", "embed", "score", "extract", "score", "judge", + "summarise"), } -#: What follows `enrich` for the roles it filled, passed over when it filled none. +#: What follows `fetch_text` for the roles it filled, passed over when it filled none. REFILL = ("prep", "embed", "score") @@ -131,7 +134,7 @@ STAGES: dict[str, Callable[[str], Task]] = { label="Mapping the roles' host words"), "summarise": _summarise, "judge": _judge, - "enrich": lambda cascade: enrich.enrich_task(), + "fetch_text": lambda cascade: fetch_text.fetch_text_task(), } @@ -157,7 +160,7 @@ def step(config: Any, store: Any, tally: Tally, payload: dict[str, Any], *, name, done = payload.get("cascade", ""), payload.get("task", "") at = int(payload.get("at", -1)) tasks = CASCADES.get(name, ()) - if done == "enrich" and not enrich.filled(tally) \ + if done == "fetch_text" and not fetch_text.filled(tally) \ and tasks[at + 1:at + 1 + len(REFILL)] == REFILL: at += len(REFILL) following = follows(name, at) @@ -222,24 +225,25 @@ def run_pull(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str return {"started": " > ".join(CASCADES["pull"]), "run": run} -@job("enrich", "Reading the best roles on their own pages", solo=True, resource="http") -def run_enrich(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str, Any]: - """Ask for the enrich cascade, `enrich → prep → embed → score`. Its own work is - nil, as the `pull` job's. Under `NULL` there is no queue to take it.""" - if not context.task(chain("enrich")): - raise RuntimeError("no queue to start the enrich cascade on") - return {"started": " > ".join(CASCADES["enrich"])} +@job("fetch_text", fetch_text.LABEL, solo=True, resource="http") +def run_fetch_text(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str, Any]: + """Ask for the fetch text cascade, the pull's chain from fetch text on. + Its own work is nil, as the `pull` job's. Under `NULL` there is no queue + to take it.""" + if not context.task(chain("fetch_text")): + raise RuntimeError("no queue to start the fetch text cascade on") + return {"started": " > ".join(CASCADES["fetch_text"])} -def enrich_after(queue: Any, score_job: Any) -> Any: - """Queue the `enrich` job behind `score_job`, the rescore that follows a - change to the ranking, however that score ends. One per score job - (`enrich:after:`), so ten changes answered by one score are one - enrich. None without a score job, or when the queue refuses.""" +def fetch_text_after(queue: Any, score_job: Any) -> Any: + """Queue the `fetch_text` job behind `score_job`, the rescore that + follows a change to the ranking, however that score ends. One per score + job (`fetch_text:after:`), so ten changes answered by one score are + one cascade. None without a score job, or when the queue refuses.""" if score_job is None: return None - return queue.enqueue("enrich", depends_on=[score_job.id], after="done", - dedup_key=f"enrich:after:{score_job.id}") + return queue.enqueue("fetch_text", depends_on=[score_job.id], after="done", + dedup_key=f"fetch_text:after:{score_job.id}") #: What `app job ` and `/api/jobs` start besides `pull`: one stage @@ -255,5 +259,5 @@ for _name in ("extract", "embed", "score", "summarise", "judge", "prep"): # setting: a cadence edited or switched off under Runs survives # every restart (see pipeline/schedule.py). declare("pull", "pull", "every 6h", - label="Pull every board, then map, embed, score, read the best roles' pages, " - "extract, rescore, summarise and judge against the CVs") + label="Pull every board, then map, embed, score, fetch the text of the roles in the " + "cut, extract, rescore, judge against the CVs and summarise") diff --git a/app/jobs/extract.py b/app/jobs/extract.py index c05344e..b298ab7 100644 --- a/app/jobs/extract.py +++ b/app/jobs/extract.py @@ -12,13 +12,12 @@ is waiting while it is open, has not been given up on, has never been answered (`extracted_at IS NULL`), has no model level (`seniority_model` empty), was never asked where a remote hire may live (`remote_where_model` NULL) or for its languages and deadline (`languages_required` NULL), 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 +in the cut of an enabled agent (`scores.cut`, see `app.jobs.score`). That +bound is what keeps the cost proportional to the agents rather than to the +boards: the pull scores every role for free first, then reads the cut, and +scores again so the fetch gate and the cut see what was filled; a role that +enters the cut then is read on the next pull. A batch built for named uids +finishes what it was dealt whatever their rank. The answer only fills columns that are still empty: a board's structured field wins over what a model read out of the prose. The exception is the level, which the model always gives (the board's word and the title's go in as hints) and which diff --git a/app/jobs/enrich.py b/app/jobs/fetch_text.py similarity index 75% rename from app/jobs/enrich.py rename to app/jobs/fetch_text.py index 67ea99e..c084203 100644 --- a/app/jobs/enrich.py +++ b/app/jobs/fetch_text.py @@ -1,32 +1,34 @@ -"""`enrich`: the best roles without text, read on their own pages. +"""`fetch_text`: the roles in the cut without text, read on their own pages. Some hosts list a role without its text and state it on the role's own page only (`text="detail"`: Workday, SuccessFactors, Oracle, Phenom and the rest with a `detail`, and a search provider such as Jobbank). The pull reads -listings only; this stage reads those pages after scoring, so the calls go -where the ranking is. A saved search whose reader has `detail` is treated as -a board under its target `provider:name`: the same bar, budgets and state -keys, and its failing record is `search:failing:`. +listings only; this stage reads those pages after scoring, for the roles the +model pass will read, so each has its text first. A saved search whose +reader has `detail` is treated as a board under its target `provider:name`: +the same budgets and state keys, and its failing record is +`search:failing:`. No model is asked. -1. `enrich_waiting` hands over the open roles without text that an enabled - agent with a prompt ranks ungated among its best `enrich.top` and at - `enrich.min_score` or above, best first by the highest such score. A role - below that bar for every agent gets no call. +1. `Store.fetch_text_waiting` hands over the open roles without text in the + cut of an enabled agent (`scores.cut`, see `app.jobs.score`), best first + by the highest final score. A role in no cut gets no call. 2. They are taken in that order, those whose read failed last, on boards and - searches whose reader has `detail`, at most `enrich.per_board` from one - board and `enrich.per_run` in all, leaving out the roles already read, - unless their read failed or gave no text `enrich.retry_days` ago - (`plugins.ats.detail_due`). + searches whose reader has `detail`, at most `fetch_text.per_board` from + one board and `fetch_text.per_run` in all, leaving out the roles already + read, unless their read failed or gave no text `fetch_text.retry_days` + ago (`plugins.ats.detail_due`). 3. One batch per board or search, on the `http` pool, reads them (`plugins.ats.read_board_details`, which keeps `ats:detailed:`, `ats:detailfailed:` and `ats:detailempty:`) and stores what each page filled. `Store.upsert_role` drops the vector of a role whose text improved, so the next `embed` encodes those roles only. -In the pull cascade `embed` and `score` follow; `cascade.step` passes over -both when no role was filled. The same stage follows a rescore after an agent -is saved or a company's affinity changes (`cascade.enrich_after`), never after -a vote. Whether it runs is `stages.stage_on(config, store, "enrich")`. +In the pull cascade `prep`, `embed` and `score` follow, so the filled roles +are embedded again and their levels and the cut recomputed; `cascade.step` +passes over the three when no role was filled. The same stage starts the +cascade that follows a rescore after an agent is saved or a company's +affinity or overrides change (`cascade.fetch_text_after`), never after a +vote. Whether it runs is `stages.stage_on(config, store, "fetch_text")`. """ from __future__ import annotations @@ -47,8 +49,8 @@ from pipeline import NULL, Batch, RunContext, Tally, Task, job log = logging.getLogger(__name__) -KIND = "enrich_batch" -SETTLE = "app.jobs.enrich:settle" +KIND = "fetch_text_batch" +SETTLE = "app.jobs.fetch_text:settle" def _ats() -> Any: @@ -93,13 +95,14 @@ def _readable(store: Store) -> dict[int, Subscription]: def plan(config: Config, store: Store) -> list[tuple[str, list[str]]]: """`(target, uids)` per board or search, best first, within the budgets: - the roles `enrich_waiting` ranks, on fetchable boards and searches whose - reader has `detail` and that are not failing, and due for a read.""" - settings = config.enrich + the roles `fetch_text_waiting` hands over, on fetchable boards and + searches whose reader has `detail` and that are not failing, and due for + a read.""" + settings = config.fetch_text ats = _ats() subs = _readable(store) by_board: dict[str, list[dict[str, Any]]] = defaultdict(list) - for found in store.enrich_waiting(settings.top, settings.min_score): + for found in store.fetch_text_waiting(): sub = subs.get(found["subscription_id"]) if sub is None or f"{sub.source}:failing:{sub.target}" in store.state: continue @@ -135,7 +138,7 @@ def plan(config: Config, store: Store) -> list[tuple[str, list[str]]]: def batches(config: Config, store: Store) -> list[Batch]: """The finder: one batch per board or search, nothing while the stage is off.""" - if not stages.stage_on(config, store, "enrich"): + if not stages.stage_on(config, store, "fetch_text"): return [] return [Batch({"target": target, "uids": uids}, label=f"Reading {len(uids)} roles on {target}") @@ -153,12 +156,12 @@ def run_batch(config: Config, store: Store, *, target: str, uids: list[str], if not subs or not uids: return {"target": target, "filled": 0, "texts": 0} sub = subs[0] - found = {f["uid"]: f for f in store.enrich_waiting(0, 0.0, uids)} + found = {f["uid"]: f for f in store.fetch_text_waiting(uids)} rows = [_row(found[uid], sub) for uid in uids if uid in found] context.check() with httpx.Client() as client: filled = ats.read_board_details(store.state, target, rows, client=client, - cap=len(rows), retry_days=config.enrich.retry_days) + cap=len(rows), retry_days=config.fetch_text.retry_days) label = sub.company or sub.label texts = 0 for row in filled: @@ -166,7 +169,7 @@ def run_batch(config: Config, store: Store, *, target: str, uids: list[str], item.source = sub.source store.upsert_role(item, sub) texts += bool(row.get("description")) - log.info("enrich: %s: %d read, %d filled, %d with text", target, len(rows), len(filled), + log.info("fetch_text: %s: %d read, %d filled, %d with text", target, len(rows), len(filled), texts) return {"target": target, "read": len(rows), "filled": len(filled), "texts": texts} @@ -183,6 +186,9 @@ def settle(config: Config, store: Store, tally: Tally, *, "texts": sum(int(result.get("texts") or 0) for result in tally.results)} -def enrich_task() -> Task: - return Task(kind=KIND, finder=batches, name="enrich", settle=SETTLE, - label="Reading the best roles on their own pages") +#: What the task and the cascade's job are called on the Runs page. +LABEL = "Fetching the text of the roles in the cut" + + +def fetch_text_task() -> Task: + return Task(kind=KIND, finder=batches, name="fetch_text", settle=SETTLE, label=LABEL) diff --git a/app/jobs/judge.py b/app/jobs/judge.py index ce18aa7..1d0796d 100644 --- a/app/jobs/judge.py +++ b/app/jobs/judge.py @@ -1,4 +1,4 @@ -"""`judge`: each of an agent's best roles read against its CV, as three numbers. +"""`judge`: each role in an agent's cut read against its CV, as three numbers. A `ModelTask` beside `summarise`, after the `/rank` of github.com/MadsLorentzen/ai-job-search: the model reads a posting against a @@ -10,14 +10,15 @@ CV match sorts by. The model also reports a verdict on each of the agent's must-haves (one per line of its must text, numbered M1..Mn in the prompt); it does not apply them. The rule is code's: a "broken" one makes career count as at most `app.models.BROKEN_CAP`, when the role is served. Culture and location are left out: the first -is guessed from ad text, the second is what the gates do. +is guessed from ad text, the second is what the fetch gate does. -The work is pairs, `(agent, role)`: an enabled agent with a CV, and a role -among its best ungated scores as deep and as low as its own `limits` say -(`judge_top`, `judge_min`), or among the `judge_cv_top` ungated roles closest -to its CV (`scores.cv_fit`) that those leave out, with some text, that has -no judgement yet, or one read from another CV, prompts, like, avoid or must -text or role text than today's (`input_key`). A pair is named by the composite key `":"`, +The work is pairs, `(agent, role)`: an enabled agent with a CV and a prompt, +and an open role in its cut (`scores.cut`, see `app.jobs.score`: the best by +fit, the best by CV closeness that those leave out, and the liked and saved) +with some text, that has no judgement yet, or one read from another CV, +prompts, like, avoid or must text or role text than today's (`input_key`). +The cut is the only selection: the pull judges it after extract and the +rescore that follows, so it reads the cut as recomputed. A pair is named by the composite key `":"`, which is what `desk.modeltask` hands round as a uid, so `desk/` is untouched. A batch holds one agent's pairs, since the profile is in the system prompt; the finder cuts per agent (`_finder`) and the job runs one call per agent it @@ -186,8 +187,7 @@ def input_key(agent: Agent, text: str) -> str: def _judged(agent: Agent) -> bool: """Whether `judge` reads for this agent at all: enabled, with a CV and a - prompt with text (without one its scores have no fit, so its best roles - are its newest).""" + prompt with text (without one nothing of its cut is in by fit).""" return agent.enabled and bool(agent.cv_text.strip()) \ and any(p.text.strip() for p in agent.prompts) @@ -195,7 +195,7 @@ def _judged(agent: Agent) -> bool: def waiting(store: Store, keys: Sequence[str] | None = None, limit: int | None = None) -> list[tuple[Agent, Any, str, str]]: """The pairs `judge` has yet to read, as `(agent, row, text, input_key)`, - each agent's best first (`Store.judge_rows`). Given `keys`, only those + each agent's cut in its order (`Store.judge_rows`). Given `keys`, only those of them still waiting, whatever their rank.""" wanted: dict[int, list[str]] | None = None if keys is not None: diff --git a/app/jobs/posting.py b/app/jobs/posting.py index f74ca1c..b3d3265 100644 --- a/app/jobs/posting.py +++ b/app/jobs/posting.py @@ -135,7 +135,7 @@ def _detail_target(role: Role) -> str: def _detail_text(role: Role, target: str) -> str: """The text of a search role, read by its reader's `detail` on the ad's - own page as the pull's enrich stage reads it. Raises `Unreadable` when + own page as the pull's fetch text stage reads it. Raises `Unreadable` when the read fails or the page states no text.""" from plugins import ats diff --git a/app/jobs/prep.py b/app/jobs/prep.py index 013ba4b..bd56d00 100644 --- a/app/jobs/prep.py +++ b/app/jobs/prep.py @@ -3,7 +3,7 @@ In the pull cascade it follows the pull, whose settle has closed what is gone, and maps every role the pull changed, then folds the copies across the whole database (`Store.rebuild_dupes`, which the pull's settle used to -run). After `enrich` it maps the roles their own pages filled, since only +run). After `fetch_text` it maps the roles their own pages filled, since only those are due. `solo`, so two cascades never write the same rows at once. """ diff --git a/app/jobs/score.py b/app/jobs/score.py index f9185a7..eb0eaa8 100644 --- a/app/jobs/score.py +++ b/app/jobs/score.py @@ -1,21 +1,29 @@ -"""`score`: every open role, scored for every enabled search agent. +"""`score`: every open role, scored for every enabled search agent, and each +agent's cut. A role is scored per agent, `(uid, agent) -> score`, because a role is only -good for a given search. For each agent: +good for a given search. Each agent first applies its fetch gate: require gates a role that fails one keeps its row, with score 0 and the - reason ("remote not remote") - any gates one group: a role that matches none of them, and has the - column filled for at least one, is gated ("none of - country DK / remote remote") - prefer gates `weight` when the role matches, 0 when it does not + reason ("remote not remote"); the region, which nothing + lifts + any gates one group, the place part: a role that matches none of + them, and has the column filled for at least one, is + gated ("none of country DK / remote remote"); skipped for + a company set to All countries exclude gates a role that matches one of the values keeps its row, with - score 0 and the value ("title account executive") + score 0 and the value ("title account executive"); those + on `title` are skipped for a company set to All roles A gate rejects on evidence, never on absence: a role whose column is empty is not gated by it. `extract` is what fills an empty column, and it reads -only the roles that rank, so gating on an empty column would keep those -roles out of extract's reach for good. +only the roles in the cut, so gating on an empty column would keep those +roles out of extract's reach for good. A hidden company (`affinity = -1`) +is not scored at all; a favourite passes the gate like any other company. + +The final score, the feed's order, is the weighted sum of four terms over +the sum of the weights, so it stays 0..1: + fit cos(role vector, prompt vector) as a level: the embedding model's calibrated `lo` is 0 and `hi` 1, clipped outside (see `scale`), so a role's fit does not depend on what @@ -29,25 +37,29 @@ roles out of extract's reach for good. over this agent's votes; with only likes it is the liked mean, with only passes the negated passed mean; no votes, no term -Beside the score, for an agent with a CV, `cv_fit`: how close the role is to -the CV, the best cosine between the role and the CV's chunks -(`embed.cv_chunks`, embedded as the prompts are), scaled like fit. It is no -term of the score; the judge reads the closest roles the score leaves out -(`Store.judge_rows`) and CV match orders the roles not judged yet by it. +A role with no vector is scored without `fit` and `taste`, unless `fit` is +all there is, when it is gated as "no vector". An agent with no votes scores +every role without `taste`; the direction is newsdesk's +`centroid(liked) - centroid(disliked)`. An agent with no prompt that has +text has no `fit` either, so nothing of it enters the cut by fit and its +scores do not decide what summarise reads (`Store._summarise_top`); a run +names such agents in one notice. -Every term is 0..1. The score is the weighted sum over the sum of the weights -(the agent's four and its prefer gates'), so it stays 0..1. A favourite -company (`affinity = 2`) skips the `any` group but not the `require` or -`exclude` gates, and -is scored like any other company with its affinity term at 1: nothing puts -its roles above a better fit. A hidden one (`-1`) is not scored at all. A -role with no vector is scored without `fit` and `taste`, unless `fit` is all -there is, when it is gated as "no vector". -An agent with no votes scores every role without `taste`; the direction is -newsdesk's `centroid(liked) - centroid(disliked)`. An agent with no prompt -that has text has no `fit` either: its scores are not a match, so it is left -out of what extract and summarise read (`Store._top_scope`), and a run names -such agents in one notice. +Beside the score, `fit` is stored as it is, and for an agent with a CV, +`cv_fit`: how close the role is to the CV, the best cosine between the role +and the CV's chunks (`embed.cv_chunks`, embedded as the prompts are), scaled +like fit. Neither carries the affinity term. + +The cut is the roles that get fetch text, extract and judge (`cut_of`). It +is computed from two levels, each with the company's affinity as a bonus of +`w.affinity * affinity / 3` over the sum of the weights, the same size as in +the final score (a liked company about 0.05 above neutral, a favourite about +0.1): the fit level `fit + bonus` and the CV level `cv_fit + bonus`. Over +the roles that pass the fetch gate, the best `cut_fit_top` by fit level at +or above `cut_fit_min` are `fit`; the best `cut_cv_top` by CV level at or +above `cut_cv_min` that are not in yet are `cv`; every role this agent +liked or anybody saved that is not in yet is `kept`. Newness and taste are +no part of it, and the two lists are not merged into one number. Recomputed in full on every run, and each agent's rows are replaced in one transaction: nothing is incremental, so nothing can drift. @@ -67,7 +79,7 @@ import numpy as np from pipeline import NULL, Notice, RunContext, job from app.config import Calibration, Config from app.jobs import embed -from app.models import Agent, Candidate, Gate, Weights +from app.models import Agent, Candidate, Gate, Limits, Weights from app.storage import Store log = logging.getLogger(__name__) @@ -130,10 +142,10 @@ def scale(cosines: dict[str, float], embedding model's calibrated `band` (`app.config.CALIBRATION`). Raw cosines on multilingual-e5-small sit between about 0.75 and 0.87 for - real postings against a real intent, so unscaled a prefer gate weighted - 0.2 moved a score more than the whole spread of fit. The band stretches - that spread to 0..1 with fixed constants, so a role's level is the same - in every run and a `limits` bar is an absolute threshold. + real postings against a real intent, so unscaled the whole spread of fit + was smaller than a recency step. The band stretches that spread to 0..1 + with fixed constants, so a role's level is the same in every run and a + minimum of the cut is an absolute threshold. Without a band, for a model nobody calibrated, it falls back to min-max across the run: the closest role is 1 and the farthest 0, which ranks @@ -165,19 +177,20 @@ def _reason(gate: Gate) -> str: def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]: - """Why this role is gated, as reasons: every require gate it fails, every - exclude gate it matches, and the `any` group when it matches none of them. - A gate with an empty column (state -1) neither passes nor fails, so the - group fails only when some member had evidence. A favourite company's - roles skip the `any` group but not the require and exclude gates, so a - star widens a search to a company's roles within e.g. Europe, not to the - whole world, and not to the titles the agent leaves out.""" + """Why the fetch gate rejects this role, as reasons: every require gate + it fails, every exclude gate it matches, and the `any` group when it + matches none of them. A gate with an empty column (state -1) neither + passes nor fails, so the group fails only when some member had evidence. + A company set to All countries skips the `any` group, the place part, + and one set to All roles the exclude gates on `title`, the titles to + leave out; the require gates, the region, apply to every company.""" states = list(zip(gates, candidate.gates, strict=True)) reasons = [_reason(g) for g, state in states if g.mode == "require" and state == 0] reasons += [f"{g.field} {g.values[state - 1]}" for g, state in states - if g.mode == "exclude" and state > 0] + if g.mode == "exclude" and state > 0 + and not (candidate.all_roles and g.field == "title")] group = [(g, state) for g, state in states if g.mode == "any"] - if candidate.affinity != 2 and group and not any(state == 1 for _, state in group) \ + if not candidate.all_countries and group and not any(state == 1 for _, state in group) \ and any(state == 0 for _, state in group): reasons.append("none of " + ", ".join(_wanted(g) for g, _ in group)) return reasons @@ -186,17 +199,15 @@ def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]: def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights, fit: float | None, now: datetime, *, taste: float | None = None, missing: str = "no vector") -> tuple[float, str]: - """One role for one agent: `(score, gated_reason)`. `fit` and `taste` are - already scaled to 0..1 (see `scale`), or None when there is nothing to - measure: no role vector, no prompt vector, no votes. A None term leaves - the denominator; `missing` says why fit is out.""" + """One role for one agent: `(final score, gated_reason)`. `fit` and + `taste` are already scaled to 0..1 (see `scale`), or None when there is + nothing to measure: no role vector, no prompt vector, no votes. A None + term leaves the denominator; `missing` says why fit is out.""" if failed := failed_gates(candidate, gates): return 0.0, ", ".join(failed) terms = [(weights.recency, recency(candidate.posted_at, now)), (weights.affinity, affinity_term(candidate.affinity))] - terms += [(g.weight, 1.0 if state == 1 else 0.0) - for g, state in zip(gates, candidate.gates, strict=True) if g.mode == "prefer"] if fit is not None: terms.append((weights.fit, fit)) if taste is not None: @@ -208,6 +219,44 @@ def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights, return sum(w * t for w, t in terms) / total, "" +def bonus(affinity: int, weights: Weights) -> float: + """What a company's affinity adds to both levels of the cut: the share + the affinity term gives it in the final score above a company at -1, so + a liked company sits about 0.05 above a neutral one and a favourite + about 0.1 on the default weights. 0 when every weight is 0.""" + total = weights.fit + weights.recency + weights.affinity + weights.taste + return weights.affinity * affinity / 3 / total if total else 0.0 + + +def cut_of(candidates: Sequence[Candidate], passed: set[str], fits: dict[str, float], + near: dict[str, float], kept: set[str], weights: Weights, + limits: Limits) -> dict[str, str]: + """`uid -> why` for the roles in the cut, among `candidates` whose uid + is in `passed` (the fetch gate let them through): `fit` for the best + `limits.cut_fit_top` by fit level at or above `cut_fit_min`, `cv` for + the best `cut_cv_top` by CV level at or above `cut_cv_min` not in yet, + and `kept` for those in `kept` (liked or saved) not in yet. A level is + `fits` or `near` plus the company's `bonus`; a role without one is in + no list by it. Ties go to the newer role, then the lower uid.""" + by_uid = {c.uid: c for c in candidates if c.uid in passed} + # Newest first, then uid, as the sort below keeps equal levels in order. + ordered = sorted(sorted(by_uid.values(), key=lambda c: c.uid), + key=lambda c: c.posted_at, reverse=True) + cut: dict[str, str] = {} + for why, levels, top, floor in (("fit", fits, limits.cut_fit_top, limits.cut_fit_min), + ("cv", near, limits.cut_cv_top, limits.cut_cv_min)): + scored = [(levels[c.uid] + bonus(c.affinity, weights), c.uid) for c in ordered + if c.uid in levels and c.uid not in cut] + scored = [(level, uid) for level, uid in scored if level >= floor] + scored.sort(key=lambda pair: pair[0], reverse=True) + for _, uid in scored[:top]: + cut[uid] = why + for c in ordered: + if c.uid in kept and c.uid not in cut: + cut[c.uid] = "kept" + return cut + + # ------------------------------------------------------------------ vectors def intent_key(model: str, intent: str) -> str: @@ -301,12 +350,13 @@ def _cv(store: Store, agent: Agent, model: str, encoder: _Encoder) -> np.ndarray def no_intent(names: Sequence[str]) -> Notice: """The notice for enabled agents without a prompt that has text: their - scores have no fit, so `Store._top_scope` keeps them from feeding extract - and summarise. The code keeps its name from when an agent had one intent.""" + scores have no fit, so nothing of theirs enters the cut by fit and + their final scores do not choose what summarise reads. The code keeps + its name from when an agent had one intent.""" return Notice( code="score.no_intent", level="warning", kind="expected", key="score", - text=f"No prompt with text, so ranked without fit and not read by extract " - f"or summarise: {', '.join(names)}.", + text=f"No prompt with text, so ranked without fit and nothing in the cut by fit: " + f"{', '.join(names)}.", fix="Give the agent a prompt under Settings, Search agents.", where="app.jobs.score") @@ -314,7 +364,8 @@ def no_intent(names: Sequence[str]) -> Notice: def score_agent(store: Store, agent: Agent, vectors: dict[str, np.ndarray], prompts: Sequence[tuple[str, np.ndarray]], now: datetime, calibration: Calibration | None = None, - cv: np.ndarray | None = None) -> dict[str, int]: + cv: np.ndarray | None = None, + saved: set[str] | None = None) -> dict[str, int]: """Two passes: the cosines of the roles this agent scores, to each of its `prompts` and to its taste direction, then every score with both scaled by the model's `calibration`, or across the run without one. A role's fit @@ -327,9 +378,12 @@ def score_agent(store: Store, agent: Agent, vectors: dict[str, np.ndarray], share. The voted roles are scored like any other; a liked one sits near the direction it helped make, which is what a like should do. With `cv` (`_cv`), each of those roles also gets its `cv_fit`, its `closeness` to - the CV scaled like fit, beside the score and not in it.""" + the CV scaled like fit. Then the cut (`cut_of`), with this agent's likes + and the `saved` roles (`Store.saved_uids`, read here when not given) + kept.""" candidates = store.score_candidates(agent.id, agent.gates) - direction = taste_direction(vectors, store.votes(agent.id)) + votes = store.votes(agent.id) + direction = taste_direction(vectors, votes) cosines: dict[str, dict[str, float]] = {name: {} for name, _ in prompts} tastes: dict[str, float] = {} near: dict[str, float] = {} @@ -351,22 +405,29 @@ def score_agent(store: Store, agent: Agent, vectors: dict[str, np.ndarray], fits[uid], best[uid] = value, name tastes = scale(tastes, calibration.taste if calibration else None) near = scale(near, calibration.cv_band if calibration else None) - rows: list[tuple[str, float, str, str, float | None]] = [] - gated = 0 + scored: list[tuple[str, float, str, float | None]] = [] + passed: set[str] = set() for candidate in candidates: fit = fits.get(candidate.uid) value, reason = score_one( candidate, agent.gates, agent.weights, fit, now, taste=tastes.get(candidate.uid), missing="no vector" if prompts else "no prompt vector") - gated += bool(reason) + if not reason: + passed.add(candidate.uid) + scored.append((candidate.uid, value, reason, fit)) + kept = {uid for uid, vote in votes.items() if vote == 1} + kept |= store.saved_uids() if saved is None else saved + cut = cut_of(candidates, passed, fits, near, kept, agent.weights, agent.limits) + rows = [] + for uid, value, reason, fit in scored: counted = fit is not None and agent.weights.fit and not reason - cv_fit = near.get(candidate.uid) - rows.append((candidate.uid, round(value, 6), reason, - best[candidate.uid] if counted else "", - None if cv_fit is None else round(cv_fit, 6))) + cv_fit = near.get(uid) + rows.append((uid, round(value, 6), reason, best[uid] if counted else "", + None if cv_fit is None else round(cv_fit, 6), + None if fit is None else round(fit, 6), cut.get(uid, ""))) store.replace_scores(agent.id, rows) - return {"scored": len(rows), "gated": gated} + return {"scored": len(rows), "gated": len(rows) - len(passed), "cut": len(cut)} @job("score", "Scoring roles for every agent", solo=True, resource="cpu") @@ -386,17 +447,21 @@ def run_score(config: Config, store: Store, *, if blind := [agent.name for agent in agents if not any(p.text.strip() for p in agent.prompts)]: context.notice(no_intent(blind)) - scored = gated = 0 + scored = gated = cut = 0 + saved = store.saved_uids() for n, agent in enumerate(agents): context.check() context.progress(n, len(agents), f"scoring for {agent.name}, {n + 1} of {len(agents)}") counts = score_agent(store, agent, vectors, _prompts(store, agent, model, encoder), - now, calibration, cv=_cv(store, agent, model, encoder)) + now, calibration, cv=_cv(store, agent, model, encoder), + saved=saved) scored += counts["scored"] gated += counts["gated"] - log.info("%-20s %6d scored, %6d gated", agent.name, counts["scored"], counts["gated"]) + cut += counts["cut"] + log.info("%-20s %6d scored, %6d gated, %5d in the cut", agent.name, counts["scored"], + counts["gated"], counts["cut"]) context.progress(len(agents), len(agents), "") - return {"agents": len(agents), "scored": scored, "gated": gated, + return {"agents": len(agents), "scored": scored, "gated": gated, "cut": cut, "vectors": len(vectors)} diff --git a/app/jobs/stages.py b/app/jobs/stages.py index 4d0895c..5caf27b 100644 --- a/app/jobs/stages.py +++ b/app/jobs/stages.py @@ -1,5 +1,5 @@ """Whether the three model stages of a pull, `extract`, `summarise` and -`judge`, and the `enrich` stage run. +`judge`, and the `fetch_text` stage run. `extract.enabled`, `summarise.enabled` and `judge.enabled` in config.yaml are read at startup and say nothing on the page, so the switch lives in the @@ -10,16 +10,17 @@ deployment that never uses the switch behaves as it did. `stage_on` is the one place that decides, and the tasks' finders ask it (`extract.extract_task`, `summarise.summarise_task`, `judge.judge_task`). -`enrich` (reading the best roles on their own pages, `app/jobs/enrich.py`) -asks no model and is on by default (`enrich.enabled`); its switch is -`stage:enrich`, set the same way as the others: by the Runs page -(`PUT /api/stages/enrich`) and by `app stages enrich on|off`. +`fetch_text` (reading the role's own page for the roles in the cut, +`app/jobs/fetch_text.py`) asks no model and is on by default +(`fetch_text.enabled`); its switch is `stage:fetch_text`, set the same way +as the others: by the Runs page (`PUT /api/stages/fetch_text`) and by +`app stages fetch_text on|off`. `describe` is what `GET /api/stages` and `app stages` show: for each stage -with a switch, the model stages and `enrich`, whether it is on, where that comes from, the model it uses, and who -would answer. The model is the one a call is made with: `agent.model` when -set, since it overrides the per-task model everywhere (`desk/modeltask.py`), -else the stage's own. +with a switch, the model stages and `fetch_text`, whether it is on, where +that comes from, the model it uses, and who would answer. The model is the +one a call is made with: `agent.model` when set, since it overrides the +per-task model everywhere (`desk/modeltask.py`), else the stage's own. """ from __future__ import annotations @@ -34,8 +35,8 @@ from desk.tools import agent NAMES = ("extract", "summarise", "judge") -#: Every stage with a switch: the model stages and `enrich`. -SWITCHES = (*NAMES, "enrich") +#: Every stage with a switch: the model stages and `fetch_text`. +SWITCHES = (*NAMES, "fetch_text") #: What `describe_one` says of a stage that asks no model. NO_ASK = "reads role pages, asks no model" @@ -53,7 +54,7 @@ class Stage(BaseModel): `backend` is `agent.backend`'s answer, "" for nobody, and `answer` says it in words.""" - name: Literal["extract", "summarise", "judge", "enrich"] + name: Literal["extract", "summarise", "judge", "fetch_text"] on: bool source: Literal["stored", "config"] model: str diff --git a/app/jobs/summarise.py b/app/jobs/summarise.py index 7da4b44..1f43e83 100644 --- a/app/jobs/summarise.py +++ b/app/jobs/summarise.py @@ -6,12 +6,13 @@ 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, is open and no copy, -and is either in the top of an enabled agent's ungated scores as deep and as -low as that agent's own `limits` say (`summarise_top`, `summarise_min`), or -one the owner cares about whatever its rank: saved (it has an application, the -star) or voted up by an enabled agent. The limits keep the cost flat: a pull -summarises the roles somebody will look at, not the 30k that are open, and the -saved and liked roles are a handful the owner picked. Each is summarised +and is either in the top of an enabled agent's feed, the best `summarise_top` +final scores among the roles its fetch gate passes (its `limits`), or one the +owner cares about whatever its rank: saved (it has an application, the star) +or voted up by an enabled agent. Summarise is no part of any score and runs +last in the pull, once the final scores are set. The limit keeps the cost +flat: a pull summarises the roles somebody will look at, not the 30k that are +open, and the saved and liked roles are a handful the owner picked. Each is summarised once, with the same give-up rule (`GIVE_UP_AFTER` replies that left it without). Saving or liking a role queues a batch for that one uid at once, while the stage is on (`web.api`), rather than waiting for the next pull. A diff --git a/app/models.py b/app/models.py index c4023ca..f205a53 100644 --- a/app/models.py +++ b/app/models.py @@ -1450,28 +1450,29 @@ GateField = Literal["place", "country", "remote", "remote_from", "seniority", "f "salary_min", "currency", "permit", "languages", "company", "title"] GATE_FIELDS: tuple[str, ...] = get_args(GateField) -GateMode = Literal["require", "any", "prefer", "exclude"] +GateMode = Literal["require", "any", "exclude"] class Gate(Model): - """One condition of an agent. `require` gates a role that fails it (the - row is kept with the reason); the `any` gates together are one group a - role must match at least one of ("in Denmark, or remote"); `prefer` adds - `weight` to its score when it matches; `exclude` gates a role that matches - any of its values ("title account executive"), a favourite's too. A gate - rejects on evidence only: a - role whose column is empty is not rejected, since `extract` fills the - column later for the roles that rank, and only for those.""" + """One condition of an agent's fetch gate. `require` gates a role that + fails it (the row is kept with the reason); the `any` gates together are + one group a role must match at least one of ("in Denmark, or remote"); + `exclude` gates a role that matches any of its values ("title account + executive"). A company set to All countries skips the `any` group and one + set to All roles skips the `exclude` gates on `title` (`score.failed_gates`). + A gate rejects on evidence only: a role whose column is empty is not + rejected, since `extract` fills the column later for the roles in the + cut, and only for those.""" field: GateField values: list[str | int | float] = Field(min_length=1) mode: GateMode = "require" - weight: float = Field(1.0, ge=0) class Weights(Model): - """How much each score term counts. Summed with the prefer gates' weights - into the denominator, so a score stays in 0..1 whatever the numbers.""" + """How much each term of the final score counts. The score is the + weighted sum over the sum of the weights, so it stays in 0..1 whatever + the numbers.""" fit: float = Field(0.6, ge=0) recency: float = Field(0.2, ge=0) @@ -1488,31 +1489,21 @@ class Weights(Model): 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. Fit is a - calibrated level (`score.scale`), so a bar means the same in every run. - A fresh role with the worst fit scores about 0.27 on the default - weights, a perfect one about 0.87. Measured on 16,841 open roles for the - "Copenhagen engineering" starter agent (4,156 ungated, no votes), 0.55 - admits about 300 roles to extract and 0.65 about 35 to summarise, the - long answer; the top N catches an agent whose bar admits far more. An - agent keeps the limits stored with it; only a key its JSON lacks - follows these defaults.""" + """The agent's cut and the depth of summarise. The cut is the roles that + get fetch text, extract and judge (`app.jobs.score`): the best + `cut_fit_top` by fit level at or above `cut_fit_min`, then the best + `cut_cv_top` by CV level at or above `cut_cv_min` that the first list + leaves out, then every role liked or saved. Both levels are calibrated + (`score.scale`) with the company's affinity as a small bonus, so a + minimum means the same in every run. For a broad agent the count stops a + list, for a narrow one the minimum. Summarise reads the best + `summarise_top` by final score, and every role liked or saved.""" - extract_top: int = Field(500, ge=0) - extract_min: float = Field(0.55, ge=0, le=1) - summarise_top: int = Field(30, ge=0) - summarise_min: float = Field(0.65, ge=0, le=1) - #: How deep `judge` reads an agent with a CV: a long answer per role, - #: so between summarise's 30 and extract's 500. - judge_top: int = Field(150, ge=0) - judge_min: float = Field(0.55, ge=0, le=1) - #: And the agent's ungated roles closest to its CV (`scores.cv_fit`, see - #: `app.jobs.score`) that the pair above leaves out, as many as this: - #: a role the prompts rank low can still be one the CV fits. - judge_cv_top: int = Field(50, ge=0) + cut_fit_top: int = Field(500, ge=0) + cut_fit_min: float = Field(0.6, ge=0, le=1) + cut_cv_top: int = Field(150, ge=0) + cut_cv_min: float = Field(0.6, ge=0, le=1) + summarise_top: int = Field(50, ge=0) class Prompt(Model): @@ -1601,16 +1592,33 @@ class Agent(Model): created_at: str +class SavedFilter(Model): + """A row of `saved_filters`: the feed's filter settings saved under a + name for one agent. `query` is the feed's query string as `roles.js` + writes it, without the agent. There is no editing: a change is a new + one.""" + + id: int + name: str + query: str + created_at: str + + class Candidate(Model): """What scoring reads about one open role for one agent: its posted - date (for `recency`), its company's affinity for that agent, and per - gate `1` match, `0` no match, `-1` the column is empty. An `exclude` - gate's match is the position of the first value it matched, from 1, so - the reason can name it.""" + date (for `recency`), its company's affinity for that agent and the + company's two overrides of the fetch gate, and per gate `1` match, `0` + no match, `-1` the column is empty. An `exclude` gate's match is the + position of the first value it matched, from 1, so the reason can name + it.""" uid: str posted_at: str affinity: int = 0 + #: `companies.all_countries`: the `any` group (the place part) is skipped. + all_countries: bool = False + #: `companies.all_roles`: the `exclude` gates on `title` are skipped. + all_roles: bool = False gates: list[int] = [] diff --git a/app/storage/schema.py b/app/storage/schema.py index f361f4e..1f77be4 100644 --- a/app/storage/schema.py +++ b/app/storage/schema.py @@ -385,10 +385,14 @@ CREATE TABLE IF NOT EXISTS role_votes ( PRIMARY KEY (agent_id, uid) ); --- A role is scored per agent. A gated role keeps its row, with the reason. --- prompt names the agent's prompt that gave the fit, '' when fit did not count. --- cv_fit is how close the role is to the agent's CV, 0..1, NULL without a CV --- or a vector and for a gated role; it is no term of score. +-- A role is scored per agent. A role the fetch gate rejects keeps its row, +-- with the reason. score is the final score, the feed's order. prompt names +-- the agent's prompt that gave the fit, '' when fit did not count. fit is the +-- prompt fit level and cv_fit how close the role is to the agent's CV, both +-- 0..1 before the affinity bonus, NULL without a prompt, a CV or a vector and +-- for a gated role. cut says why the role is in the agent's cut, the roles +-- that get fetch text, extract and judge: 'fit', 'cv', 'kept' (liked or +-- saved), or '' when it is not. CREATE TABLE IF NOT EXISTS scores ( uid TEXT NOT NULL REFERENCES roles(uid) ON DELETE CASCADE, agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE, @@ -397,9 +401,23 @@ CREATE TABLE IF NOT EXISTS scores ( scored_at TEXT NOT NULL, prompt TEXT NOT NULL DEFAULT '', cv_fit REAL, + fit REAL, + cut TEXT NOT NULL DEFAULT '', PRIMARY KEY (uid, agent_id) ); +-- The feed's filter settings saved under a name, per agent. query is the +-- feed's query string without the agent. Names are unique per agent +-- regardless of case; a change is a new row. +CREATE TABLE IF NOT EXISTS saved_filters ( + id INTEGER PRIMARY KEY, + agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE, + name TEXT NOT NULL, + query TEXT NOT NULL, + created_at TEXT NOT NULL, + UNIQUE (agent_id, name COLLATE NOCASE) +); + -- The vectors of an agent's CV, one per chunk in order, for scores.cv_fit. -- vec_key is sha1 of the embedding model, the chunking and the CV text, the -- same on every row of an agent, so an unchanged CV is not embedded again. @@ -487,6 +505,9 @@ CREATE INDEX IF NOT EXISTS idx_roles_board ON roles(source, subscription_id CREATE INDEX IF NOT EXISTS idx_application_events ON application_events(application_id, at); CREATE INDEX IF NOT EXISTS idx_scores_agent ON scores(agent_id, score DESC); +-- The roles in an agent's cut, which fetch text, extract and judge read. +-- Partial: most roles are in no cut. +CREATE INDEX IF NOT EXISTS idx_scores_cut ON scores(agent_id, uid) WHERE cut <> ''; CREATE INDEX IF NOT EXISTS idx_role_votes_agent ON role_votes(agent_id, vote); CREATE INDEX IF NOT EXISTS idx_vectors_model ON vectors(model); CREATE INDEX IF NOT EXISTS idx_role_tags_tag ON role_tags(tag); @@ -731,6 +752,12 @@ UPDATE roles SET seniority = COALESCE(NULLIF(seniority_model, ''), # missing. The next score fills them. (31, "scores.cv_fit and agent_cv_vectors: how close each role is to the agent's CV", lambda conn: _add_cv_fit(conn)), + # A Python step: gates and limits are JSON. The old limits were bars on + # the blended score and mean nothing on plain fit, so every agent takes + # the new defaults. + (32, "the cut: scores.fit and scores.cut, agents' limits reset to the cut's defaults, " + "prefer gates gone, saved_filters, and enrich renamed fetch text", + lambda conn: _add_cut(conn)), ) #: What a file created from `SCHEMA` today is already at. @@ -1094,6 +1121,76 @@ def _add_cv_fit(conn: sqlite3.Connection) -> None: PRIMARY KEY (agent_id, position))""") +#: What migration 32 writes into every agent's `limits`: `models.Limits` as +#: it stood then, spelled out so a later change of the defaults does not +#: change what this migration did. +_V32_LIMITS = {"cut_fit_top": 500, "cut_fit_min": 0.6, "cut_cv_top": 150, "cut_cv_min": 0.6, + "summarise_top": 50} + +_V32 = """ +CREATE TABLE IF NOT EXISTS saved_filters ( + id INTEGER PRIMARY KEY, + agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE, + name TEXT NOT NULL, + query TEXT NOT NULL, + created_at TEXT NOT NULL, + UNIQUE (agent_id, name COLLATE NOCASE) +); +INSERT OR IGNORE INTO state (key, value) +SELECT 'stage:fetch_text', value FROM state WHERE key = 'stage:enrich'; +DELETE FROM state WHERE key = 'stage:enrich'; +""" + + +def _add_cut(conn: sqlite3.Connection) -> None: + """Migration 32's step. `scores.fit` and `scores.cut`, NULL and empty + until the next score; every agent's `limits` reset to `_V32_LIMITS`; + the `prefer` gates dropped from every agent's `gates` and `weight` from + the others; `saved_filters`; the switch `stage:enrich` renamed + `stage:fetch_text`; and the queued `enrich` and `enrich_batch` jobs + deleted, with a schedule of `enrich` firing `fetch_text`. Runs twice + without harm: limits are reset when the columns are added, and after + that only where they still hold a key the cut does not know.""" + columns = {r[1] for r in conn.execute("PRAGMA table_info(scores)")} + fresh = "cut" not in columns + if "fit" not in columns: + conn.execute("ALTER TABLE scores ADD COLUMN fit REAL") + if fresh: + conn.execute("ALTER TABLE scores ADD COLUMN cut TEXT NOT NULL DEFAULT ''") + reset = dropped = 0 + for agent_id, raw_limits, raw_gates in conn.execute( + "SELECT id, limits, gates FROM agents").fetchall(): + try: + limits = json.loads(raw_limits or "{}") + except ValueError: + limits = None + if fresh or not isinstance(limits, dict) or set(limits) - set(_V32_LIMITS): + conn.execute("UPDATE agents SET limits=? WHERE id=?", + (json.dumps(_V32_LIMITS), agent_id)) + reset += 1 + try: + gates = json.loads(raw_gates or "[]") + except ValueError: + continue + if not isinstance(gates, list): + continue + kept = [{k: v for k, v in g.items() if k != "weight"} for g in gates + if isinstance(g, dict) and g.get("mode") != "prefer"] + if kept != gates: + dropped += sum(1 for g in gates if isinstance(g, dict) and g.get("mode") == "prefer") + conn.execute("UPDATE agents SET gates=? WHERE id=?", (json.dumps(kept), agent_id)) + for statement in _statements(_V32): + conn.execute(statement) + tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")} + if "jobs" in tables: + conn.execute("DELETE FROM jobs WHERE state = 'queued' " + "AND kind IN ('enrich', 'enrich_batch')") + if "schedules" in tables: + conn.execute("UPDATE schedules SET kind = 'fetch_text' WHERE kind = 'enrich'") + log.info("migration 32: %d agents' limits reset to the cut's defaults, %d prefer gates " + "dropped", reset, dropped) + + def _add_all_roles(conn: sqlite3.Connection) -> None: """Migration 21's step. The column; the tag for every company that is a favourite of any agent, since favourite no longer widens what a pull diff --git a/app/storage/store.py b/app/storage/store.py index 443f99c..5bffc74 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -25,7 +25,7 @@ from app import dupes, places from app.models import ( AFFINITY, HAND_REASONS, LEVELS, STATUSES, Agent, Application, ApplicationEvent, Candidate, Company, CvLanguage, Gate, Judgement, Limits, Prompt, Role, - RoleCopy, RolePlace, RoleRow, ScoredRole, cv_languages as normal_languages, cv_score, + RoleCopy, RolePlace, RoleRow, SavedFilter, ScoredRole, cv_languages as normal_languages, cv_score, BROKEN_CAP, capped_career, language_flag, role_languages, Subscription, Weights, check_prompts, company_fold, company_key, country_of, excerpt, place_key, fetch_terms as normal_terms, host_word, iso_day, posted_at, posted_host, remote_of, title_key, @@ -42,7 +42,7 @@ _RAW_TEXT = ("place", "currency") #: The host's own words a reader hands on in `Item.raw`, stored as #: `_host` (`host_word`) for `app.prep` to map. `seniority_host` is #: rewritten every pull, as the host's level always was; the others are kept -#: filled, since a role's own page (`enrich`) may state what its listing +#: filled, since a role's own page (`fetch_text`) may state what its listing #: does not. _HOST_WORDS = ("seniority", "employment", "remote", "country") _KEPT_HOST_WORDS = ("employment_host", "remote_host", "country_host") @@ -78,6 +78,16 @@ _EXTRACT_WAITING = ("(extracted_at IS NULL OR seniority_model = '' OR remote_whe "OR languages_required IS NULL) " f"AND closed_at IS NULL AND dup_of IS NULL AND extract_tries < {GIVE_UP_AFTER}") +#: A role in the cut of an enabled agent (`scores.cut`, see `app.jobs.score`), +#: as a `WHERE` on `r`: what fetch text and extract read. +_IN_CUT = ("r.uid IN (SELECT s.uid FROM scores s JOIN agents a ON a.id = s.agent_id " + "AND a.enabled = 1 WHERE s.cut <> '')") + +#: The order within one agent's cut, over its scores as `s`: in by fit, best +#: fit first, then in by CV closeness, closest first, then kept. +_CUT_ORDER = ("CASE s.cut WHEN 'fit' THEN 0 WHEN 'cv' THEN 1 ELSE 2 END, " + "CASE s.cut WHEN 'fit' THEN s.fit WHEN 'cv' THEN s.cv_fit END DESC") + #: What a vote may be: 1 like, 0 neither way, -1 pass, None unjudged. VOTES = (-1, 0, 1, None) @@ -1127,7 +1137,7 @@ class Store: hide_muted: bool = False, include_read: bool = True, tags: Sequence[str] = (), tag_mode: str = "any", agent: int | None = None, by_score: bool = True, gated: bool = False, - judged: str | None = None, pay: str | None = None, + fetch_gate: bool = False, judged: str | None = None, pay: str | None = None, company_keys: Sequence[str] = (), places: Sequence[str] = (), seniority: Sequence[str] = (), seniority_unknown: bool = True, pay_unknown: bool = False, by_cv: bool = False, closing: bool = False, @@ -1172,7 +1182,9 @@ class Store: set. The score is the stored one moved by the agent's vote (`_VOTED_SCORE`), so a vote shows in the order at once and needs no rescore. With `by_score` off the order is newest, with the votes and - no score. + no score; with `fetch_gate` as well, the newest order keeps to the + agent's fetch gate the same way, carrying `gated_reason`, so no order + shows a role the agent's gate rejects unless `gated`. With `by_cv` as well, the roles the agent's `judge` read come first by `cv_score`, then the others by how close they are to the CV @@ -1191,6 +1203,7 @@ class Store: company=company, place=place, open_only=open_only, q=q, function=function, remote=remote, days=days, hide_muted=hide_muted, include_read=include_read, tags=tags, tag_mode=tag_mode, agent=agent, by_score=by_score, gated=gated, + fetch_gate=fetch_gate, judged=judged, pay=pay, company_keys=company_keys, places=places, seniority=seniority, seniority_unknown=seniority_unknown, pay_unknown=pay_unknown, closing=closing) @@ -1202,7 +1215,9 @@ class Store: if scored: select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt, s.cv_fit" order = (_by_cv(cv) if by_cv else "") + "score DESC, " + order - elif open_only: + elif agent is not None and fetch_gate: + select += ", s.gated_reason" + if not scored and open_only: # Newest first, open only: idx_roles_posted covers this exactly # (posted_at DESC, uid, WHERE closed_at IS NULL), so it earns the # LIMIT rather than sorting every open role first. Left to the @@ -1241,7 +1256,7 @@ class Store: hide_muted: bool = False, include_read: bool = True, tags: Sequence[str] = (), tag_mode: str = "any", agent: int | None = None, by_score: bool = True, gated: bool = False, - judged: str | None = None, pay: str | None = None, + fetch_gate: bool = False, judged: str | None = None, pay: str | None = None, company_keys: Sequence[str] = (), places: Sequence[str] = (), seniority: Sequence[str] = (), seniority_unknown: bool = True, pay_unknown: bool = False, by_cv: bool = False, closing: bool = False, @@ -1264,6 +1279,7 @@ class Store: company=company, place=place, open_only=open_only, q=q, function=function, remote=remote, days=days, hide_muted=hide_muted, include_read=include_read, tags=tags, tag_mode=tag_mode, agent=agent, by_score=by_score, gated=gated, + fetch_gate=fetch_gate, judged=judged, pay=pay, company_keys=company_keys, places=places, seniority=seniority, seniority_unknown=seniority_unknown, pay_unknown=pay_unknown, closing=closing) @@ -1280,7 +1296,9 @@ class Store: if scored: select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt, s.cv_fit" order = (_by_cv(cv) if by_cv else "") + "score DESC, " + order - elif open_only: + elif agent is not None and fetch_gate: + select += ", s.gated_reason" + if not scored and open_only: # Same reasoning as `roles`: the planner otherwise sorts every # open role before applying LIMIT. sql = sql.replace("FROM roles ", "FROM roles INDEXED BY idx_roles_posted ", 1) @@ -1318,7 +1336,8 @@ class Store: days: int | None = None, hide_muted: bool = False, include_read: bool = True, tags: Sequence[str] = (), tag_mode: str = "any", agent: int | None = None, by_score: bool = True, - gated: bool = False, judged: str | None = None, + gated: bool = False, fetch_gate: bool = False, + judged: str | None = None, pay: str | None = None, company_keys: Sequence[str] = (), places: Sequence[str] = (), seniority: Sequence[str] = (), seniority_unknown: bool = True, pay_unknown: bool = False, @@ -1328,6 +1347,7 @@ class Store: company=company, place=place, open_only=open_only, q=q, function=function, remote=remote, days=days, hide_muted=hide_muted, include_read=include_read, tags=tags, tag_mode=tag_mode, agent=agent, by_score=by_score, gated=gated, + fetch_gate=fetch_gate, judged=judged, pay=pay, company_keys=company_keys, places=places, seniority=seniority, seniority_unknown=seniority_unknown, pay_unknown=pay_unknown, closing=closing) @@ -1340,7 +1360,8 @@ class Store: days: int | None = None, hide_muted: bool = False, include_read: bool = True, tags: Sequence[str] = (), tag_mode: str = "any", agent: int | None = None, by_score: bool = True, - gated: bool = False, judged: str | None = None, + gated: bool = False, fetch_gate: bool = False, + judged: str | None = None, pay: str | None = None, company_keys: Sequence[str] = (), places: Sequence[str] = (), seniority: Sequence[str] = (), seniority_unknown: bool = True, @@ -1356,7 +1377,7 @@ class Store: "company": company, "place": place, "open_only": open_only, "q": q, "function": function, "remote": remote, "days": days, "hide_muted": hide_muted, "include_read": include_read, "tags": tags, "tag_mode": tag_mode, "agent": agent, - "by_score": by_score, "gated": gated, "judged": judged, + "by_score": by_score, "gated": gated, "fetch_gate": fetch_gate, "judged": judged, "company_keys": company_keys, "places": places, "closing": closing} by_level, level_params = self._roles_from( **shared, pay=pay, pay_unknown=pay_unknown) @@ -1376,6 +1397,7 @@ class Store: days: int | None, hide_muted: bool, include_read: bool, tags: Sequence[str], tag_mode: str, agent: int | None, by_score: bool, gated: bool, judged: str | None, + fetch_gate: bool = False, pay: str | None = None, company_keys: Sequence[str] = (), places: Sequence[str] = (), seniority: Sequence[str] = (), seniority_unknown: bool = True, @@ -1384,7 +1406,9 @@ class Store: """The `FROM ... WHERE ...` that `roles` and `count_roles` share, with its parameters in order. The agent's votes are always joined as `v` (see `_WITH_VOTES`) and its judgements as `j` (`_WITH_JUDGEMENTS`); - its scores as `s` only when ordering by them. A copy of another role + its scores as `s` when ordering by them or with `fetch_gate`, and + then a role the agent's fetch gate rejects is left out unless + `gated`. A copy of another role (`rebuild_dupes`) is never listed: its kept role stands for it and counts its copies. `closing` keeps the roles whose deadline is today or within `CLOSING_DAYS` after it.""" @@ -1458,7 +1482,7 @@ class Store: params += values sql = f"FROM roles {_WITH_VOTES} {_WITH_JUDGEMENTS}" joins: list[Any] = [agent, agent] - if agent is not None and by_score: + if agent is not None and (by_score or fetch_gate): sql += " JOIN scores s ON s.uid = roles.uid AND s.agent_id = ?" joins.append(agent) if not gated: @@ -1639,21 +1663,18 @@ class Store: # -------------------------------------------------------------- extract - 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. An - agent without a prompt with text counts for nothing here: its scores - have no fit, so they say how fresh a role is, not whether it is - wanted.""" - defaults = Limits() - return (f"""r.uid IN (SELECT uid FROM ( - SELECT s.uid, s.score, a.limits, ROW_NUMBER() OVER ( + def _summarise_top(self) -> tuple[str, list[Any]]: + """The `WHERE` fragment for a role among the best `summarise_top` + final scores of an enabled agent with a prompt, open, no copy and + passing that agent's fetch gate: the top of its feed. 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 limit of the agent + that ranked it; a key the JSON lacks is the `Limits` default. The + role is `r`. An agent without a prompt with text counts for nothing + here: its scores have no fit, so they say how fresh a role is, not + whether it is wanted.""" + return ("""r.uid IN (SELECT uid FROM ( + SELECT s.uid, a.limits, ROW_NUMBER() OVER ( PARTITION BY s.agent_id ORDER BY s.score DESC, top.posted_at DESC, s.uid) AS n FROM scores s @@ -1664,15 +1685,14 @@ class Store: JOIN roles top ON top.uid = s.uid AND top.closed_at IS NULL AND top.dup_of IS NULL WHERE s.gated_reason = '') - 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")]) + WHERE n <= COALESCE(json_extract(limits, '$.summarise_top'), ?))""", + [Limits().summarise_top]) 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; - otherwise only the roles in `_top_scope`.""" + otherwise only the roles in the cut of an enabled agent (`_IN_CUT`).""" where = [_EXTRACT_WAITING.replace("extracted_at", "r.extracted_at") .replace("closed_at", "r.closed_at").replace("extract_tries", "r.extract_tries") .replace("dup_of", "r.dup_of") @@ -1682,8 +1702,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("extract") - return " AND ".join([*where, top]), params + return " AND ".join([*where, _IN_CUT]), [] def extract_pending(self) -> int: where, params = self._extract_scope(None) @@ -1691,14 +1710,13 @@ class Store: return int(row["n"]) if row else 0 def extract_waiting(self, limit: int, uids: Sequence[str] | None = None) -> list[Role]: - """Roles `extract` has yet to read: 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. Ordered by the - best rank any agent gives the role (each agent's first, then each - agent's second ...), then by its score, so every agent's top comes - before anyone's tail whatever limits cut the batch. Given `uids`, - only those of them still waiting, whatever their rank; a role no agent - ranks comes last.""" + """Roles `extract` has yet to read: those in the cut of an enabled + agent, so the backlog follows the cut rather than the boards. + Ordered by the best rank any agent gives the role by final score + (each agent's first, then each agent's second ...), then by that + score, so every agent's top comes before anyone's tail whatever + limit cuts the batch. Given `uids`, only those of them still waiting, + whatever their rank; a role no agent ranks comes last.""" where, params = self._extract_scope(uids) return [Role.from_row(r) for r in self._rows( "SELECT r.* FROM roles r LEFT JOIN (" @@ -1826,15 +1844,16 @@ class Store: """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. - Otherwise open, not given up, and in `_top_scope` or one the owner - cares about whatever its rank: saved (it has an application) or voted - up by an enabled agent.""" + Otherwise open, not given up, and in the top of an enabled agent's + feed (`_summarise_top`) or one the owner cares about whatever its + rank: saved (it has an application) or voted up by an enabled + agent.""" where = ["r.summary = ''", "(TRIM(r.article_text) <> '' OR TRIM(r.description) <> '')"] 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("summarise") + top, params = self._summarise_top() where += [ "r.closed_at IS NULL", "r.dup_of IS NULL", @@ -1856,12 +1875,12 @@ class Store: return int(row["n"]) if row else 0 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, and every open role that is saved or voted - up by an enabled agent, whatever its rank; given `uids`, those still without a - summary, whatever their rank. A batch asks again when it runs.""" + """Roles `summarise` has yet to write, newest first: the best + `summarise_top` final scores of every enabled agent, so the backlog + is bounded by the agents rather than by the number of open roles, + and every open role that is saved or voted up by an enabled agent, + whatever its rank; 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} " @@ -1908,17 +1927,15 @@ class Store: # ---------------------------------------------------------------- judge def judge_rows(self, agent: Agent, uids: Sequence[str] | None = None) -> list[sqlite3.Row]: - """What `judge` may read for one agent: its best ungated scores of - open roles that are no copy, as deep and as low as its `limits` say - (`judge_top`, `judge_min`), best first, then the `judge_cv_top` - ungated ones closest to its CV (`scores.cv_fit`) that the first set - leaves out, closest first, each with some text (`article_text` or - `description`). Given `uids`, those of them with - text, whatever their rank or state. Each row carries `uid`, - `title`, `company`, `place`, `article_text`, `description`, and the - stored judgement's `input_key`, `judged_at` and `tries` (NULL, NULL - and 0 without one), and `given_up` once `tries` reached - `GIVE_UP_AFTER`.""" + """What `judge` may read for one agent: the open roles in its cut + that are no copy and have some text (`article_text` or + `description`), those in by fit first, best fit first, then those in + by CV closeness, closest first, then those kept because they are + liked or saved. Given `uids`, those of them with text, whatever + their rank or state. Each row carries `uid`, `title`, `company`, + `place`, `article_text`, `description`, and the stored judgement's + `input_key`, `judged_at` and `tries` (NULL, NULL and 0 without one), + and `given_up` once `tries` reached `GIVE_UP_AFTER`.""" text = "(TRIM(r.article_text) <> '' OR TRIM(r.description) <> '')" select = ("SELECT r.uid, r.title, r.company, r.place, r.article_text, r.description, " "j.input_key, j.judged_at, COALESCE(j.tries, 0) AS tries, " @@ -1928,23 +1945,13 @@ class Store: return self._rows( f"{select} roles r {joined} WHERE r.uid IN (SELECT value FROM json_each(?)) " f"AND {text} ORDER BY r.uid", (agent.id, json.dumps(list(uids)))) - ranked = ("FROM scores s JOIN roles top ON top.uid = s.uid AND top.closed_at IS NULL " - "AND top.dup_of IS NULL WHERE s.agent_id = ? AND s.gated_reason = ''") return self._rows( - f"""WITH best AS (SELECT s.uid, s.score {ranked} - ORDER BY s.score DESC, top.posted_at DESC, s.uid LIMIT ?), - by_score AS (SELECT uid, 0 AS part, score AS rank FROM best - WHERE score >= ?), - by_cv AS (SELECT s.uid, 1 AS part, s.cv_fit AS rank {ranked} - AND s.cv_fit IS NOT NULL - AND s.uid NOT IN (SELECT uid FROM by_score) - ORDER BY s.cv_fit DESC, top.posted_at DESC, s.uid LIMIT ?) - {select} (SELECT * FROM by_score UNION ALL SELECT * FROM by_cv) w - JOIN roles r ON r.uid = w.uid {joined} - WHERE {text} - ORDER BY w.part, w.rank DESC, r.posted_at DESC, r.uid""", - (agent.id, agent.limits.judge_top, agent.limits.judge_min, agent.id, - agent.limits.judge_cv_top, agent.id)) + f"""{select} scores s JOIN roles r ON r.uid = s.uid AND r.closed_at IS NULL + AND r.dup_of IS NULL + {joined} + WHERE s.agent_id = ? AND s.cut <> '' AND {text} + ORDER BY {_CUT_ORDER}, r.posted_at DESC, r.uid""", + (agent.id, agent.id)) def set_judgement(self, agent_id: int, uid: str, input_key: str, skills: int, experience: int, career: int, strengths: Sequence[str], @@ -2031,18 +2038,15 @@ class Store: keeps it.""" return bool(self._write("UPDATE roles SET posting_url = ? WHERE uid = ?", (url, uid))) - # --------------------------------------------------------------- enrich + # ----------------------------------------------------------- fetch text - def enrich_waiting(self, top: int, floor: float, - uids: Sequence[str] | None = None) -> list[dict[str, Any]]: - """Open board and search roles without text that an enabled agent with a prompt - ranks ungated among its best `top` and at `floor` or above, best - first by the highest such score, as `{uid, source_id, - subscription_id, title, url, published_at, raw, score}`. The ranking - is `_top_scope`'s, with the bar given rather than the agent's own. - Given `uids`, those of them still without text, whatever their rank, - in no order (`score` is None), since a batch finishes what it was - dealt.""" + def fetch_text_waiting(self, uids: Sequence[str] | None = None) -> list[dict[str, Any]]: + """Open board and search roles without text in the cut of an enabled + agent, best first by the highest final score any such agent gives + them, as `{uid, source_id, subscription_id, title, url, published_at, + raw, score}`. Given `uids`, those of them still without text, cut or + not, in no order (`score` is None), since a batch finishes what it + was dealt.""" fields = ("r.uid, r.source_id, r.subscription_id, r.title, r.original_url AS url, " "r.published_at, r.raw") waiting = ("r.closed_at IS NULL AND r.dup_of IS NULL AND r.source IN ('ats', 'search') " @@ -2054,21 +2058,11 @@ class Store: "AND r.uid IN (SELECT value FROM json_each(?))", (json.dumps(list(uids)),)) else: rows = self._rows( - f"""SELECT {fields}, MAX(ranked.score) AS score FROM ( - SELECT s.uid, s.score, ROW_NUMBER() OVER ( - PARTITION BY s.agent_id - ORDER BY s.score DESC, top.posted_at DESC, s.uid) AS n - FROM scores s - JOIN agents a ON a.id = s.agent_id AND a.enabled = 1 - AND EXISTS (SELECT 1 FROM agent_prompts p - WHERE p.agent_id = a.id - AND TRIM(p.text, ' ' || char(9, 10, 13)) <> '') - JOIN roles top ON top.uid = s.uid AND top.closed_at IS NULL - AND top.dup_of IS NULL - WHERE s.gated_reason = '') ranked - JOIN roles r ON r.uid = ranked.uid - WHERE ranked.n <= ? AND ranked.score >= ? AND {waiting} - GROUP BY r.uid ORDER BY score DESC, r.uid""", (top, floor)) + f"""SELECT {fields}, MAX(s.score) AS score FROM scores s + JOIN agents a ON a.id = s.agent_id AND a.enabled = 1 + JOIN roles r ON r.uid = s.uid + WHERE s.cut <> '' AND {waiting} + GROUP BY r.uid ORDER BY score DESC, r.uid""") return [dict(row) for row in rows] # -------------------------------------------------------------- vectors @@ -2220,10 +2214,41 @@ class Store: return found def delete_agent(self, agent_id: int) -> bool: - """Its prompts, scores, votes and affinities go with it, by the - foreign keys.""" + """Its prompts, scores, votes, affinities and saved filters go with + it, by the foreign keys.""" return self._write("DELETE FROM agents WHERE id=?", (agent_id,)) > 0 + # -------------------------------------------------------- saved filters + + def saved_filters(self, agent_id: int) -> list[SavedFilter]: + """The agent's saved filters, by name regardless of case.""" + return [SavedFilter.from_row(r) for r in self._rows( + "SELECT * FROM saved_filters WHERE agent_id=? ORDER BY name COLLATE NOCASE, id", + (agent_id,))] + + def save_filter(self, agent_id: int, name: str, query: str) -> SavedFilter: + """Save the feed's filters under `name` for the agent. ValueError on + an empty name or one the agent already has regardless of case, + KeyError on an unknown agent.""" + name = " ".join(name.split()) + if not name: + raise ValueError("a saved filter needs a name") + try: + with self.transaction() as conn: + if conn.execute("SELECT 1 FROM agents WHERE id=?", (agent_id,)).fetchone() is None: + raise KeyError(agent_id) + new_id = conn.execute( + "INSERT INTO saved_filters (agent_id, name, query, created_at) " + "VALUES (?,?,?,?)", (agent_id, name, query, self.now())).lastrowid + except sqlite3.IntegrityError as exc: + raise ValueError(f"a filter named {name!r} is already saved") from exc + row = self._row("SELECT * FROM saved_filters WHERE id=?", (new_id,)) + assert row is not None + return SavedFilter.from_row(row) + + def delete_filter(self, filter_id: int) -> bool: + return self._write("DELETE FROM saved_filters WHERE id=?", (filter_id,)) > 0 + def prompt_vector(self, agent_id: int, name: str, key: str) -> np.ndarray | None: """The cached vector of the agent's prompt `name`, if it was made under `key`.""" @@ -2262,11 +2287,11 @@ class Store: def score_candidates(self, agent_id: int, gates: Sequence[Gate]) -> list[Candidate]: """Every open role of a company this agent has not hidden, copies - (`rebuild_dupes`) aside, with the agent's affinity for the company - and how the role meets each gate: 1 match, 0 no match, -1 the column - is empty. The gates are evaluated here, in SQL, so a `require` gate is - the `WHERE` it reads as, written - as a column so a gated role keeps its row and reason.""" + (`rebuild_dupes`) aside, with the agent's affinity for the company, + the company's All countries and All roles, and how the role meets + each gate: 1 match, 0 no match, -1 the column is empty. The gates are + evaluated here, in SQL, so a `require` gate is the `WHERE` it reads + as, written as a column so a gated role keeps its row and reason.""" columns: list[str] = [] params: list[Any] = [] gaz = self.gazetteer() @@ -2281,31 +2306,41 @@ class Store: params += values columns.append(f"CASE WHEN {empty} THEN -1 {' '.join(whens)} ELSE 0 END AS g{n}") select = ", ".join(["r.uid", "r.posted_at", - "COALESCE(ac.affinity, 0) AS affinity", *columns]) + "COALESCE(ac.affinity, 0) AS affinity", + "COALESCE(c.all_countries, 0) AS all_countries", + "COALESCE(c.all_roles, 0) AS all_roles", *columns]) rows = self._rows( f"""SELECT {select} FROM roles r LEFT JOIN agent_companies ac ON ac.company_key = r.company_key AND ac.agent_id = ? + LEFT JOIN companies c ON c.key = r.company_key WHERE r.closed_at IS NULL AND r.dup_of IS NULL AND COALESCE(ac.affinity, 0) <> -1""", [*params, agent_id]) return [Candidate(uid=r["uid"], posted_at=r["posted_at"], affinity=int(r["affinity"]), + all_countries=bool(r["all_countries"]), all_roles=bool(r["all_roles"]), gates=[int(r[f"g{n}"]) for n in range(len(gates))]) for r in rows] + def saved_uids(self) -> set[str]: + """The roles saved (an application names them), which make every + agent's cut whatever their levels.""" + return {r["role_uid"] for r in self._rows( + "SELECT DISTINCT role_uid FROM applications WHERE role_uid IS NOT NULL")} + def replace_scores(self, agent_id: int, - rows: Sequence[tuple[str, float, str] | tuple[str, float, str, str] - | tuple[str, float, str, str, float | None]] - ) -> int: + rows: Sequence[tuple[Any, ...]]) -> int: """`(uid, score, gated_reason)` for every role this agent scored, - with the name of the prompt that gave the fit as a fourth item ('' when - left out) and the role's closeness to the agent's CV as a fifth (None - when left out), replacing whatever it had: a role closed or muted since the last run - drops out rather than keeping a stale score. One transaction, so a - reader sees the old list or the new one. An agent deleted while its - run was scoring gets nothing written and 0 back: its rows are gone - with it, and an insert would only fail the foreign key.""" + and optionally, in this order, the name of the prompt that gave the + fit ('' when left out), the role's closeness to the agent's CV, its + prompt fit level (both None when left out) and why it is in the cut + ('' when left out: not in it). Replaces whatever the agent had: a + role closed or muted since the last run drops out rather than keeping + a stale score. One transaction, so a reader sees the old list or the + new one. An agent deleted while its run was scoring gets nothing + written and 0 back: its rows are gone with it, and an insert would + only fail the foreign key.""" stamp = self.now() with self.transaction() as conn: if conn.execute("SELECT 1 FROM agents WHERE id=?", (agent_id,)).fetchone() is None: @@ -2313,13 +2348,20 @@ class Store: conn.execute("DELETE FROM scores WHERE agent_id=?", (agent_id,)) conn.executemany( """INSERT INTO scores (uid, agent_id, score, gated_reason, prompt, cv_fit, - scored_at) - VALUES (?,?,?,?,?,?,?)""", + fit, cut, scored_at) + VALUES (?,?,?,?,?,?,?,?,?)""", [(row[0], agent_id, row[1], row[2], row[3] if len(row) > 3 else "", - row[4] if len(row) > 4 else None, stamp) + row[4] if len(row) > 4 else None, row[5] if len(row) > 5 else None, + row[6] if len(row) > 6 else "", stamp) for row in rows]) return len(rows) + def cut(self, agent_id: int) -> dict[str, str]: + """`uid -> why` for the roles in one agent's cut: 'fit', 'cv' or + 'kept'.""" + return {r["uid"]: str(r["cut"]) for r in self._rows( + "SELECT uid, cut FROM scores WHERE agent_id=? AND cut <> ''", (agent_id,))} + def scores(self, agent_id: int) -> dict[str, tuple[float, str]]: """`uid -> (score, gated_reason)` for one agent.""" return {r["uid"]: (float(r["score"]), str(r["gated_reason"])) diff --git a/app/web/api.py b/app/web/api.py index f5c0624..e5aeea4 100644 --- a/app/web/api.py +++ b/app/web/api.py @@ -18,7 +18,7 @@ from app import jobs, places from app.config import Config from app.models import ( AFFINITY, RUNGS, TRACKS, Agent, Application, Company, CvLanguage, CvLanguages, FetchTerms, - Gate, Limits, Prompts, Role, RoleRow, + Gate, Limits, Prompts, Role, RoleRow, SavedFilter, HandReason, ScoredRole, Status, Subscription, Weights, company_key, levels_between, ) from app.storage import Store @@ -95,7 +95,8 @@ def _feed_filters( empty feed. The three seniority parameters become the levels between them (`levels_between`), which is what the store filters on. `order=cv` is the score order with the agent's judged roles first by `cv_score` - (`by_cv`).""" + (`by_cv`). A request that names an agent keeps to its fetch gate in + every order, newest included (`fetch_gate`), unless `gated`.""" for token in tokens: if not places.known(token): raise HTTPException(422, f"No place is known as {token!r}.") @@ -118,6 +119,7 @@ def _feed_filters( "tag_mode": tag_mode, "agent": _agent_id(store, agent), "by_score": agent is not None and order in ("score", "cv"), "by_cv": agent is not None and order == "cv", "gated": gated, + "fetch_gate": agent is not None, "judged": judged or None, "pay": pay or None, "company_keys": company_keys, "places": tokens, "seniority": list(levels), "seniority_unknown": seniority_unknown, "pay_unknown": pay_unknown, "closing": closing} @@ -235,14 +237,15 @@ def list_roles( `agent` says whose votes, hidden companies and scores are meant. Each role carries that agent's `vote`; a company it hid is left out unless `show_muted` is set; `judged` keeps the liked, the unjudged or the - passed. With `order=score` (the default) the roles are the ones that - agent scored, best first, carrying `score` and `gated_reason`, a gated - one only with `gated=1`; with `order=newest` every open role, newest - first, with no score; with `order=cv` (CV match) the scored roles - the agent's `judge` read come first by `cv_score`, the rest after them - by score. Without `agent` the default agent's votes and - hidden companies apply, the order is newest and there is no score; with - no agent at all, every vote is None and nothing is hidden. + passed. Every order keeps to the roles that agent's fetch gate passes, + a gated one only with `gated=1`: with `order=score` (the default) best + first by final score, carrying `score` and `gated_reason`; with + `order=newest` newest first, carrying `gated_reason` and no score; with + `order=cv` (CV match) the roles the agent's `judge` read come first by + `cv_score`, the rest after them by CV closeness, then score. Without + `agent` the default agent's votes and hidden companies apply, the order + is newest over every open role and there is no score; with no agent at + all, every vote is None and nothing is hidden. `include_read` set false hides a role already marked read. `tags` is `?tags=a&tags=b`, kept to a role with any of them or, with @@ -467,7 +470,7 @@ class StageBody(BaseModel): @router.get("/stages") def list_stages(store: StoreDep, config: ConfigDep) -> list[jobs.stages.Stage]: """The stages of a pull with a switch, extract, summarise, judge and - enrich: whether each is on, whether the stored switch or config.yaml says + fetch_text: whether each is on, whether the stored switch or config.yaml says so, its model, and who would answer.""" return jobs.stages.describe(config, store) @@ -778,7 +781,7 @@ def set_affinity(key: str, body: AffinityBody, store: StoreDep, except KeyError: raise HTTPException(404, f"no such company: {key}") from None job = _score_after_change(queue) - jobs.cascade.enrich_after(queue, job) + jobs.cascade.fetch_text_after(queue, job) return AffinityOut(**company.model_dump(), score_job=job.id if job else None) @@ -804,14 +807,17 @@ class OverrideBody(BaseModel): def _override(store: Store, queue: Any, key: str, on: bool, setter: str) -> Company: - """Switch one of a company's two fetch overrides. On queues a pull right - after, as the other reopening switches do; off queues nothing and asks - nothing first, since no role closes until the next pull reads the board - with its filters again. 404 for an unknown key.""" + """Switch one of a company's two overrides of the fetch gate. Either + way the agents' fetch gates change for the roles already stored, so a + rescore is queued (`_score_after_change`) with the fetch text cascade + behind it. On also queues a pull, as the other reopening switches do; + off queues no pull, since no role closes until the next pull reads the + board with its filters again. 404 for an unknown key.""" try: company = getattr(store, setter)(key, on) except KeyError: raise HTTPException(404, f"no such company: {key}") from None + jobs.cascade.fetch_text_after(queue, _score_after_change(queue)) if on: queue.enqueue("pull") return company @@ -821,16 +827,19 @@ def _override(store: Store, queue: Any, key: str, on: bool, setter: str) -> Comp def set_all_countries(key: str, body: OverrideBody, store: StoreDep, queue: QueueDep) -> Company: """All countries on or off: global, apart from affinity. On, the - company's big boards are read with the agents' wide reach, Europe today, - rather than their places (`app/jobs/reach.py`). See `_override`.""" + company's roles skip the place part of every agent's fetch gate (the + `any` gates, `jobs.score.failed_gates`), and its big boards are read + with the agents' wide reach, Europe today, rather than their places + (`app/jobs/reach.py`). See `_override`.""" return _override(store, queue, key, body.on, "set_all_countries") @router.post("/companies/{key}/all-roles") def set_all_roles(key: str, body: OverrideBody, store: StoreDep, queue: QueueDep) -> Company: """All roles on or off: global, apart from affinity. On, the company's - boards are read with no role-type narrowing, no fetch terms today - (`app/jobs/reach.py`). See `_override`.""" + roles skip the role part of every agent's fetch gate, the titles to + leave out (`jobs.score.failed_gates`), and its boards are read with no + fetch terms (`app/jobs/reach.py`). See `_override`.""" return _override(store, queue, key, body.on, "set_all_roles") @@ -1282,7 +1291,7 @@ def cv_languages(body: CvText, config: ConfigDep) -> CvLanguagesOut: @router.post("/agents") def save_agent(body: AgentBody, store: StoreDep, queue: QueueDep) -> Agent: """Queues the rescore, which the page's `POST /api/score` after a save - joins, and the enrich run behind it (`cascade.enrich_after`).""" + joins, and the fetch text cascade behind it (`cascade.fetch_text_after`).""" try: saved = store.save_agent(body.name, body.intent, body.gates, body.weights, body.enabled, agent_id=body.id, limits=body.limits, @@ -1294,13 +1303,56 @@ def save_agent(body: AgentBody, store: StoreDep, queue: QueueDep) -> Agent: raise HTTPException(409, str(exc)) from exc except KeyError: raise HTTPException(404, f"no such agent: {body.id}") from None - jobs.cascade.enrich_after(queue, _score_after_change(queue)) + jobs.cascade.fetch_text_after(queue, _score_after_change(queue)) return saved @router.delete("/agents/{agent_id}", status_code=204) def delete_agent(agent_id: int, store: StoreDep) -> Response: - """Its prompts, scores, votes and affinities go with it.""" + """Its prompts, scores, votes, affinities and saved filters go with it.""" if not store.delete_agent(agent_id): raise HTTPException(404, f"no such agent: {agent_id}") return Response(status_code=204) + + +# ---------------------------------------------------------- saved filters + +class FilterBody(BaseModel): + #: Checked by `Store.save_filter`, so a blank name is a 422 sentence. + name: str + #: The feed's query string as `roles.js` `addressFor` writes it, without + #: the agent. + query: str = "" + + +@router.get("/agents/{agent_id}/filters") +def list_saved_filters(agent_id: int, store: StoreDep) -> list[SavedFilter]: + """The agent's saved filters, by name. 404 for an unknown agent.""" + if store.agent(agent_id) is None: + raise HTTPException(404, f"no such agent: {agent_id}") + return store.saved_filters(agent_id) + + +@router.post("/agents/{agent_id}/filters", status_code=201) +def save_filter(agent_id: int, body: FilterBody, store: StoreDep) -> SavedFilter: + """Save the feed's filters under a name for the agent. 404 for an + unknown agent, 422 for an empty name, 409 for a name the agent already + has regardless of case. There is no editing: a change is a new one.""" + if store.agent(agent_id) is None: + raise HTTPException(404, f"no such agent: {agent_id}") + if not body.name.strip(): + raise HTTPException(422, "Give the filter a name.") + try: + return store.save_filter(agent_id, body.name, body.query) + except KeyError: + raise HTTPException(404, f"no such agent: {agent_id}") from None + except ValueError as exc: + raise HTTPException(409, f"A filter named {body.name.strip()!r} is already saved.") \ + from exc + + +@router.delete("/filters/{filter_id}", status_code=204) +def delete_saved_filter(filter_id: int, store: StoreDep) -> Response: + if not store.delete_filter(filter_id): + raise HTTPException(404, f"no such saved filter: {filter_id}") + return Response(status_code=204) diff --git a/config.example.yaml b/config.example.yaml index 29c79c7..60b26ba 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -66,9 +66,10 @@ extract: thinking: off # off | adaptive # About 60 words on a role: what it is, the stack, where, the pay if stated -# (app/jobs/summarise.py). Off by default. On, each pull summarises the best -# roles of every enabled agent that have none yet, by that agent's limits, so -# the cost stays bounded however many roles are open. The Summarise button on +# (app/jobs/summarise.py). Off by default. On, each pull summarises the top +# of every enabled agent's feed that has none yet, the best summarise_top by +# final score (its limits), and every role liked or saved, so the cost stays +# bounded however many roles are open. The Summarise button on # a role's page asks for that one role either way. `enabled` is only the # default for a fresh database: the switch under Settings, Sources, Runs (or # `app stages summarise on|off`) overrides it and needs no restart. @@ -81,12 +82,12 @@ summarise: # runs summarise when it is on and a model is reachable (app/jobs/cascade.py). # Only extract reads it. -# Each search agent's best roles read against its CV: skills, experience and -# career 0 to 100, a total, and 1 to 3 strengths and gaps (app/jobs/judge.py), -# for the CV match order on Roles. Off by default. On, each pull judges -# the best roles of every enabled agent with a CV, by that agent's judge -# limits, and only those not judged yet or judged from another CV, prompt, -# avoid or must text, or role text. The CV rides in every call, so batches are +# Each search agent's cut read against its CV: skills, experience and career 0 +# to 100, a total, and 1 to 3 strengths and gaps (app/jobs/judge.py), for the +# CV match order on Roles. Off by default. On, each pull judges the cut of +# every enabled agent with a CV and a prompt, and only the roles not judged +# yet or judged from another CV, prompt, like, avoid or must text, or role +# text. The CV rides in every call, so batches are # small. `enabled` is only the default for a fresh database: the switch under # Settings, Sources, Runs (or `app stages judge on|off`) overrides it and needs # no restart. on_ingest is accepted and ignored, as for summarise. @@ -96,16 +97,14 @@ judge: batch_size: 5 # roles per model call thinking: off # off | adaptive -# Reading the best roles on their own pages, for the hosts whose list carries -# no text (app/jobs/enrich.py); asks no model. A role is read when it has no -# text and an enabled agent with a prompt ranks it ungated among its best -# `top` and at `min_score` or above. `enabled` is only the default for a fresh -# database: the switch under Settings, Sources, Runs (or `app stages enrich -# on|off`) overrides it and needs no restart. -enrich: +# Fetch text: reading a role's own page, for the hosts whose list carries no +# text (app/jobs/fetch_text.py); asks no model. A role is read when it has no +# text and is in the cut of an enabled agent (see limits below), so every role +# the model reads has its text first. `enabled` is only the default for a +# fresh database: the switch under Settings, Sources, Runs (or `app stages +# fetch_text on|off`) overrides it and needs no restart. +fetch_text: enabled: true - top: 500 # rank depth per agent - min_score: 0.5 # 0..1, the score bar per_board: 200 # reads per board and run per_run: 1000 # reads per run retry_days: 7 # days before a page that gave no text is read again @@ -141,30 +140,31 @@ sources: {} # search: # adzuna: {app_id: "...", app_key: "..."} -# Starter search agents, imported once into an empty `agents` table. Each gate -# is {field, values, mode: require|any|prefer, weight}; the any gates are one -# group a role must match one of ("in Denmark, or remote"), and a gate rejects -# only a role whose column is filled, since extract fills the rest for the -# roles that rank. Fields are place, country, -# remote, seniority, function, salary_min, currency, permit, languages and -# company. weights default to {fit: 0.6, recency: 0.2, affinity: 0.2, taste: -# 0.2}; taste is how close a role is to the ones the agent liked minus the -# ones it passed. A country is an ISO code, filled from the board's location -# line; EU is a role open anywhere in Europe. -# limits is how deep into this agent's ranking the model stages read, each a -# top N (the backstop) and a minimum score (the real stop): extract reads the -# best extract_top roles at extract_min or more, summarise the best -# summarise_top at summarise_min or more, and judge the best judge_top at -# judge_min or more for an agent with a CV, and the judge_cv_top ungated roles -# closest to the CV that those leave out. Fit is a calibrated level, so a bar -# means the same in every run: a fresh role with the worst fit scores about -# 0.27 on the default weights, a perfect one about 0.87. The defaults are -# {extract_top: 500, extract_min: 0.55, summarise_top: 30, summarise_min: -# 0.65, judge_top: 150, judge_min: 0.55, judge_cv_top: 50}; measured for "Copenhagen engineering" on 16,841 open roles, 0.55 -# admits about 300 roles to extract and 0.65 about 35 to summarise. A wide -# monitoring search lowers the bars and raises the counts, a narrow hunt the -# reverse. An agent without a prompt that has text has no fit and feeds -# neither stage. +# Starter search agents, imported once into an empty `agents` table. The gates +# are the agent's fetch gate, each {field, values, mode: require|any|exclude}: +# a require gate is the region, the any gates are one group a role must match +# one of ("in Denmark, or remote"), the place part, and an exclude gate leaves +# out the roles that match one of its values (title: the titles to leave +# out). A gate rejects only a role whose column is filled, since extract fills +# the rest for the roles in the cut. A company set to All countries skips the +# any group, one set to All roles the exclude gates on title. Fields are +# place, country, remote, remote_from, seniority, function, salary_min, +# currency, permit, languages, company and title. weights are the final +# score's, the feed's order, default {fit: 0.6, recency: 0.2, affinity: 0.2, +# taste: 0.2}; taste is how close a role is to the ones the agent liked minus +# the ones it passed. A country is an ISO code, filled from the board's +# location line; EU is a role open anywhere in Europe. +# limits is the agent's cut, the roles that get fetch text, extract and +# judge: the best cut_fit_top by fit level at cut_fit_min or more, then the +# best cut_cv_top by CV closeness at cut_cv_min or more that the first list +# leaves out, then every role liked or saved. Both levels are calibrated, so +# a minimum means the same in every run, and a liked company's roles sit +# about 0.05 higher, a favourite's about 0.1. summarise_top is how many of +# the best final scores summarise reads. The defaults are {cut_fit_top: 500, +# cut_fit_min: 0.6, cut_cv_top: 150, cut_cv_min: 0.6, summarise_top: 50}. For +# a broad agent the count stops a list, for a narrow one the minimum. An +# agent without a prompt that has text has no fit, so nothing of it is in +# the cut by fit. # prompts is a list of {name, text}, each what the agent looks for; a role's # fit is its best prompt's, so one agent can hunt several kinds of role # without blending them into one paragraph. Votes, gates and limits stay the @@ -190,7 +190,8 @@ agents: [] # - {field: country, values: [AT, BE, BG, HR, CY, CZ, DK, EE, FI, FR, DE, GR, HU, IE, IT, # LV, LT, LU, MT, NL, PL, PT, RO, SK, SI, ES, SE, IS, LI, NO, # GB, CH, EU], mode: require} -# limits: {extract_top: 1000, extract_min: 0.45, summarise_top: 50, summarise_min: 0.6} +# limits: {cut_fit_top: 1000, cut_fit_min: 0.5, cut_cv_top: 150, cut_cv_min: 0.6, +# summarise_top: 50} # - name: Europe hybrid # intent: >- # Senior backend or platform engineer in a European tech hub, hybrid or @@ -199,7 +200,6 @@ agents: [] # - {field: country, values: [AT, BE, BG, HR, CY, CZ, DK, EE, FI, FR, DE, GR, HU, IE, IT, # LV, LT, LU, MT, NL, PL, PT, RO, SK, SI, ES, SE, IS, LI, NO, # GB, CH, EU], mode: require} -# - {field: remote, values: [hybrid, remote], mode: prefer, weight: 0.05} # Starter searches, imported once while there are no `search` subscriptions. # provider is a search reader: jobindex (Denmark, no key: q, area, pages), diff --git a/config.yaml b/config.yaml index 77b5539..b2bb3c5 100644 --- a/config.yaml +++ b/config.yaml @@ -28,8 +28,8 @@ embed: sources: {} # The starter ranking, imported once into an empty table. Edit it under # Settings, Ranking afterwards; this list is not read again. EU in a country -# gate is a role open anywhere in Europe ("Remote - Europe", "EMEA"). How deep -# into the ranking extract, summarise and judge read is each agent's `limits`, see +# gate is a role open anywhere in Europe ("Remote - Europe", "EMEA"). Which +# roles get fetch text, extract and judge is each agent's cut, its `limits`, see # config.example.yaml, which has two more agents. agents: - name: Copenhagen engineering @@ -39,7 +39,6 @@ agents: Wants ownership of production systems in a product company. gates: - {field: country, values: [DK], mode: require} - - {field: remote, values: [hybrid, remote], mode: prefer, weight: 0.05} # Starter searches, imported once while there are none. Each is a query on a # job board that spans companies (plugins/search.py); `app searches add` adds # more. area 1 is Jobindex's Region Hovedstaden. An adzuna search needs a key: diff --git a/plugins/ats.py b/plugins/ats.py index c40d3a5..2a445ed 100644 --- a/plugins/ats.py +++ b/plugins/ats.py @@ -37,8 +37,8 @@ whose reader cannot narrow that reach is read whole anyway, and sized by that read once it is complete, rather than by a second request. A pull reads listings only. A host that states a role's text on the role's -own page (`text="detail"`) is read there by `app/jobs/enrich.py` after -scoring, best roles first, through `read_board_details`. A search provider +own page (`text="detail"`) is read there by `app/jobs/fetch_text.py` after +scoring, for the roles in the agents' cut, through `read_board_details`. A search provider whose reader has `detail` (Jobbank) is read the same way, under its search target `provider:name`; `read_detail` is the one read the Fetch button makes. @@ -435,7 +435,7 @@ class ATSSource(Source): return f"{host}:{slug}:{source_id}" # A host with `detail` states a role's text on the role's own page, - # which the `enrich` stage reads after scoring (`read_board_details`), + # which the `fetch_text` stage reads after scoring (`read_board_details`), # never the pull. Its rows go out without a description, so the text # a role's page gave stays, and a role read on its own page goes out # without the listing's place and remote as well, so what its page diff --git a/plugins/readers/__init__.py b/plugins/readers/__init__.py index 17e5410..b6d8cdd 100644 --- a/plugins/readers/__init__.py +++ b/plugins/readers/__init__.py @@ -215,11 +215,11 @@ is shown and embedded until `description` arrives. A reader that sees one keeps it. **Detail.** `detail(get, slug, row)` reads one role's own page and returns a -dict of the fields it found. The pull reads listings only; the `enrich` -stage (`app/jobs/enrich.py`) calls `detail` after scoring, so the requests -go where the ranking is: the open roles without text that an agent ranks -among its best `enrich.top` at `enrich.min_score` or above, best first, at -most `enrich.per_board` from one board and `enrich.per_run` in all, one +dict of the fields it found. The pull reads listings only; the `fetch_text` +stage (`app/jobs/fetch_text.py`) calls `detail` after scoring, so the +requests go where the model pass will read: the open roles without text in +the cut of an enabled agent, best first, at most `fetch_text.per_board` +from one board and `fetch_text.per_run` in all, one batch per board through `ats.read_board_details` and `_runner.read_details`, `_runner.PAGE_DELAY` apart, the roles never tried before those whose read failed. Only `_runner.DETAIL_FIELDS` are taken: @@ -230,7 +230,7 @@ any other key is left out with a debug log line, `seniority` among them. `title`, `url`, `department` and `salary_text` cannot be filled from `detail`: the listing rewrites them every pull. A 404 or a 410 counts as read; any other failure is logged and retried on a later run; a read that gave no text -is retried after `enrich.retry_days`; a 429 ends the reads of that board for +is retried after `fetch_text.retry_days`; a 429 ends the reads of that board for the run. A role read in detail is sent by later pulls without the listing's place and remote, so what its own page said about them stays. Completeness is decided by the listing alone. diff --git a/tests/test_api_reach.py b/tests/test_api_reach.py index 0361893..ce3cae1 100644 --- a/tests/test_api_reach.py +++ b/tests/test_api_reach.py @@ -131,7 +131,7 @@ def test_a_stage_shows_the_model_that_will_answer(tmp_path: Path) -> None: config.agent.model = "claude-opus-4-1" by_name = {s["name"]: s for s in client.get("/api/stages").json()} assert by_name["extract"]["model"] == by_name["summarise"]["model"] == "claude-opus-4-1" - assert by_name["enrich"]["model"] == "" + assert by_name["fetch_text"]["model"] == "" # ------------------------------------------------------------------- config @@ -162,7 +162,7 @@ def test_the_example_config_names_every_key_of_the_sections_it_documents() -> No import yaml raw = yaml.safe_load((ROOT / "config.example.yaml").read_text("utf-8")) - for name, allowed_missing in (("agent", set()), ("extract", set()), ("enrich", set()), + for name, allowed_missing in (("agent", set()), ("extract", set()), ("fetch_text", set()), ("embed", set()), ("workers", set()), ("summarise", {"on_ingest"})): # accepted, ignored fields = set(type(getattr(Config(), name)).model_fields) diff --git a/tests/test_companies.py b/tests/test_companies.py index 8b21bbd..eb42a52 100644 --- a/tests/test_companies.py +++ b/tests/test_companies.py @@ -148,7 +148,7 @@ def test_migration_20_seeds_an_old_file_and_leaves_collisions(tmp_path: Path) -> "VALUES (?, ?, '', '')", (key, name)) conn.execute("PRAGMA user_version = 19") reopened = Store(path) - assert reopened.migrations_applied == [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert reopened.migrations_applied == [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] assert reopened.duplicate_companies() == [("orsted", ["orsted", "ørsted"])] assert len(reopened.companies()) == 3 @@ -186,7 +186,7 @@ def test_migration_21_tags_the_favourites_and_the_boards_that_read_all_roles(tmp conn.execute("ALTER TABLE companies DROP COLUMN all_countries") conn.execute("PRAGMA user_version = 20") reopened = Store(path) - assert reopened.migrations_applied == [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert reopened.migrations_applied == [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] # The tag of 21 is All countries and All roles since 24. tagged = {"mistral-ai": 1, "acme": 0, "black-forest-labs": 1, "liked": 0} assert {c.key: c.all_roles for c in reopened.companies()} == tagged diff --git a/tests/test_cv_feed.py b/tests/test_cv_feed.py index a9e37b9..090f03f 100644 --- a/tests/test_cv_feed.py +++ b/tests/test_cv_feed.py @@ -183,10 +183,15 @@ def test_closing_soon_keeps_today_to_four_days_on(client: TestClient, store: Sto owner, uid = setup(store) params = {"agent": owner.id, "order": "newest", "closing": 1} page = client.get("/api/roles", params=params).json() + # d is gated, so newest leaves it out as every order does. + assert sorted(r["uid"] for r in page["items"]) == sorted([uid["a"], uid["b"]]) + assert page["total"] == 2 + facets = client.get("/api/roles/facets", params=params).json() + assert sum(f["count"] for f in facets["pay"]) == 2 + params["gated"] = 1 + page = client.get("/api/roles", params=params).json() assert sorted(r["uid"] for r in page["items"]) == sorted([uid["a"], uid["b"], uid["d"]]) assert page["total"] == 3 - facets = client.get("/api/roles/facets", params=params).json() - assert sum(f["count"] for f in facets["pay"]) == 3 def test_the_role_carries_its_judgement_and_languages( diff --git a/tests/test_cv_fields.py b/tests/test_cv_fields.py index ac6a08f..8916cfb 100644 --- a/tests/test_cv_fields.py +++ b/tests/test_cv_fields.py @@ -71,9 +71,11 @@ def test_a_cv_language_is_a_code_and_a_level_of_the_ladder() -> None: CvLanguage(language="da", level="good") -def test_new_agents_have_judge_limits_by_default(store: Store) -> None: +def test_new_agents_have_the_cut_by_default(store: Store) -> None: made = store.save_agent("Data") - assert (made.limits.judge_top, made.limits.judge_min) == (150, 0.55) + assert made.limits.model_dump() == {"cut_fit_top": 500, "cut_fit_min": 0.6, + "cut_cv_top": 150, "cut_cv_min": 0.6, + "summarise_top": 50} # --------------------------------------------------------------- migration @@ -96,7 +98,7 @@ def test_v28_adds_the_cv_columns_and_the_host_deadline_and_runs_twice(tmp_path: migrated = Store(path) - assert migrated.migrations_applied == [28, 29, 30, 31] + assert migrated.migrations_applied == [28, 29, 30, 31, 32] rows = {r["source_id"]: r for r in migrated._rows( "SELECT source_id, languages_required, deadline_host, deadline FROM roles")} assert (rows["a"]["deadline_host"], rows["a"]["deadline"]) == ("2026-10-18", None) @@ -108,7 +110,7 @@ def test_v28_adds_the_cv_columns_and_the_host_deadline_and_runs_twice(tmp_path: assert migrated.role(posting("a").uid).deadline == "2026-10-18" with sqlite3.connect(path) as conn: conn.execute("PRAGMA user_version = 27") - assert Store(path).migrations_applied == [28, 29, 30, 31] + assert Store(path).migrations_applied == [28, 29, 30, 31, 32] # ---------------------------------------------------------------- deadline @@ -159,7 +161,7 @@ def test_extract_reads_levels_kinds_and_the_deadline( store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: item = posting("1") upsert(store, item) - store.replace_scores(store.save_agent("Data", "data").id, [(item.uid, 0.9, "")]) + store.replace_scores(store.save_agent("Data", "data").id, [(item.uid, 0.9, "", "", None, 0.9, "fit")]) model = Model(lambda roles: [answer(r["uid"], deadline="2026-10-30", languages_required=[ {"language": "DA", "level": "fluent", "kind": "required"}, {"language": "en", "level": "", "kind": "required"}, @@ -186,7 +188,7 @@ def test_extract_reads_levels_kinds_and_the_deadline( def test_a_role_extracted_before_is_read_once_more_for_the_new_fields(store: Store) -> None: item = posting("1") upsert(store, item) - store.replace_scores(store.save_agent("Data", "data").id, [(item.uid, 0.9, "")]) + store.replace_scores(store.save_agent("Data", "data").id, [(item.uid, 0.9, "", "", None, 0.9, "fit")]) store.set_extracted(item.uid, {"seniority": "director"}) assert store.extract_pending() == 0 with store.transaction() as conn: @@ -249,12 +251,12 @@ def test_v29_adds_the_musts_and_broken_columns_and_runs_twice(tmp_path: Path) -> conn.execute(f"ALTER TABLE role_judgements DROP COLUMN {column}") conn.execute("PRAGMA user_version = 28") - assert Store(path).migrations_applied == [29, 30, 31] + assert Store(path).migrations_applied == [29, 30, 31, 32] with sqlite3.connect(path) as conn: columns = {r[1] for r in conn.execute("PRAGMA table_info(role_judgements)")} assert {"musts", "broken"} <= columns conn.execute("PRAGMA user_version = 28") - assert Store(path).migrations_applied == [29, 30, 31] + assert Store(path).migrations_applied == [29, 30, 31, 32] def test_v30_adds_like_text_and_runs_twice(tmp_path: Path) -> None: @@ -266,12 +268,12 @@ def test_v30_adds_like_text_and_runs_twice(tmp_path: Path) -> None: migrated = Store(path) - assert migrated.migrations_applied == [30, 31] + assert migrated.migrations_applied == [30, 31, 32] [data] = migrated.agents() assert (data.like_text, data.avoid_text) == ("", "Reporting.") with sqlite3.connect(path) as conn: conn.execute("PRAGMA user_version = 29") - assert Store(path).migrations_applied == [30, 31] + assert Store(path).migrations_applied == [30, 31, 32] def test_the_musts_text_is_one_must_have_per_line() -> None: diff --git a/tests/test_cv_fit.py b/tests/test_cv_fit.py index 42f9528..200fc86 100644 --- a/tests/test_cv_fit.py +++ b/tests/test_cv_fit.py @@ -218,34 +218,39 @@ def window(store: Store) -> list[str]: return [row["uid"] for _, row, _, _ in judge.waiting(store)] -def test_the_judge_also_reads_the_closest_to_the_cv_the_score_leaves_out( - store: Store) -> None: - owner = store.save_agent("Data", prompts=[Prompt(name="AI", text="Head of AI")], - cv_text="VP Data.", - limits=Limits(judge_top=2, judge_min=0.5, judge_cv_top=2)) - top, second, low, near, nearer, nearest_gated, far = ranked(store, owner, [ - ("top", 0.9, "", 0.1), ("second", 0.8, "", 0.99), ("low", 0.4, "", 0.2), - ("near", 0.3, "", 0.6), ("nearer", 0.2, "", 0.7), ("gated", 0.0, "country SE", None), - ("far", 0.1, "", 0.05)]) - assert window(store) == [top, second, nearer, near] +def test_the_cut_takes_the_closest_to_the_cv_the_fit_list_leaves_out_and_judge_reads_it( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """One role in by fit to the prompt, the one closest to the CV that the + fit list leaves out by CV closeness, a liked one kept; a gated role is in + no list however close, liked or not.""" + backend = role(store, "1", "Backend Python", "Backend python python.", country="DK") + frontend = role(store, "2", "Frontend React", "Frontend react react.", country="DK") + data = role(store, "3", "Data", "Data.", country="DK") + other = role(store, "4", "Data again", "Data data.", country="DK") + gated = role(store, "5", "Frontend React", "Frontend react react.", country="SE") + owner = store.save_agent("Data", prompts=[Prompt(name="Backend", text="python backend")], + gates=[Gate(field="country", values=["DK"])], + cv_text="Frontend react developer.", + limits=Limits(cut_fit_top=1, cut_fit_min=0, cut_cv_top=1, + cut_cv_min=0)) + store.set_vote(owner.id, data, 1) + store.set_vote(owner.id, gated, 1) + run(config, store, embed.run_embed) + result = run(config, store, score.run_score) + assert store.cut(owner.id) == {backend: "fit", frontend: "cv", data: "kept"} + assert result["cut"] == 3 + with store.transaction() as conn: + fits = dict(conn.execute("SELECT uid, fit FROM scores WHERE agent_id = ?", + (owner.id,)).fetchall()) + assert fits[backend] > fits[other] and fits[gated] is None + assert window(store) == [backend, frontend, data] + + # No count, no list: what is liked is kept all the same. store.save_agent("Data", prompts=owner.prompts, agent_id=owner.id, - limits=Limits(judge_top=2, judge_min=0.85, judge_cv_top=2)) - assert window(store) == [top, second, nearer], \ - "a role under the bar is no part of the first set, so it may come in by the CV" - - store.save_agent("Data", prompts=owner.prompts, agent_id=owner.id, - limits=Limits(judge_top=2, judge_min=0.5, judge_cv_top=0)) - assert window(store) == [top, second] - assert low and far and nearest_gated - - -def test_a_gated_role_stays_out_of_the_window_however_close(store: Store) -> None: - owner = store.save_agent("Data", prompts=[Prompt(name="AI", text="Head of AI")], - cv_text="VP Data.", - limits=Limits(judge_top=1, judge_min=0.5, judge_cv_top=5)) - top, _gated = ranked(store, owner, [("top", 0.9, "", 0.1), ("gated", 0.0, "remote", 1.0)]) - assert window(store) == [top] + gates=owner.gates, limits=Limits(cut_fit_top=0, cut_cv_top=0)) + run(config, store, score.run_score) + assert store.cut(owner.id) == {data: "kept"} # ------------------------------------------------------------------- order @@ -277,7 +282,7 @@ def test_migration_31_adds_the_column_and_the_table_and_runs_twice(tmp_path: Pat conn.execute("DROP TABLE agent_cv_vectors") conn.execute("ALTER TABLE scores DROP COLUMN cv_fit") conn.execute("PRAGMA user_version = 30") - assert Store(path).migrations_applied == [31] + assert Store(path).migrations_applied == [31, 32] step = {v: s for v, _, s in MIGRATIONS}[31] with sqlite3.connect(path) as conn: assert callable(step) diff --git a/tests/test_dupes_storage.py b/tests/test_dupes_storage.py index 73feb67..f8435fc 100644 --- a/tests/test_dupes_storage.py +++ b/tests/test_dupes_storage.py @@ -246,7 +246,8 @@ def test_a_copy_is_left_out_of_the_feed_counts_and_model_stages(store: Store) -> assert {r.uid for r in store.roles_to_embed("m", 10)} == {kept.uid, other.uid} assert store.open_role_counts() == {"sap": 2} assert [count for _, _, count, _ in store.open_companies()] == [2] - store.replace_scores(agent.id, [(u, 0.9, "") for u in (kept.uid, copy.uid, other.uid)]) + store.replace_scores(agent.id, [(u, 0.9, "", "", None, 0.9, "fit") + for u in (kept.uid, copy.uid, other.uid)]) assert {r.uid for r in store.extract_waiting(10)} == {kept.uid, other.uid} assert {r.uid for r in store.summarise_waiting(10)} == {kept.uid, other.uid} @@ -278,7 +279,7 @@ def test_a_v17_file_gains_dup_of_with_its_first_rebuild(tmp_path: Path) -> None: migrated = Store(path) - assert migrated.migrations_applied == [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] assert dup_of(migrated) == {first.uid: None, second.uid: first.uid} with sqlite3.connect(path) as conn: assert conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'idx_roles_dup_of'" diff --git a/tests/test_extract.py b/tests/test_extract.py index 47e87a7..951213a 100644 --- a/tests/test_extract.py +++ b/tests/test_extract.py @@ -15,7 +15,7 @@ from desk.models import Item from desk.tools import agent from app.config import Config from app.jobs import extract -from app.models import LEVELS, Limits +from app.models import LEVELS from app.storage import Store from prepped import upsert @@ -61,11 +61,11 @@ def config(tmp_path: Path) -> Config: def ranked(store: Store, *items: Item) -> None: - """Score the roles for one enabled agent, best first, so they are in the - top extract reads. Every role given must already be stored.""" + """Score the roles for one enabled agent, best first, each in its cut, so + extract reads them. Every role given must already be stored.""" agents = store.agents() agent_id = agents[0].id if agents else store.save_agent("Backend", "python backend").id - store.replace_scores(agent_id, [(item.uid, 1.0 - n / 10, "") + store.replace_scores(agent_id, [(item.uid, 1.0 - n / 10, "", "", None, None, "fit") for n, item in enumerate(items)]) @@ -167,37 +167,28 @@ def test_an_unknown_uid_is_ignored_and_the_role_left_out_is_charged( assert store.extract_pending() == 0, "given up after three unanswered replies" -def test_only_the_best_of_each_agent_above_the_floor_is_read( +def test_only_the_cut_of_an_enabled_agent_is_read( store: Store, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - """The bound that keeps extract cheap: the top of an enabled agent's - scores, as deep and as low as that agent's own `limits` say, whatever - the boards produced. A role nobody ranks is never sent; a batch dealt a - uid finishes it.""" - for sid in ("best", "second", "low", "unscored", "gated"): + """The bound that keeps extract cheap: the cut of an enabled agent, + whatever the boards produced. A role in no cut is never sent; a batch + dealt a uid finishes it.""" + for sid in ("best", "kept", "low", "unscored", "gated"): upsert(store, posting(sid)) - one = store.save_agent("One", "python backend", - limits=Limits(extract_top=1, extract_min=0.5)) + one = store.save_agent("One", "python backend") two = store.save_agent("Two", "python backend", enabled=False) - store.replace_scores(one.id, [(posting("best").uid, 0.9, ""), - (posting("second").uid, 0.8, ""), - (posting("low").uid, 0.2, ""), - (posting("gated").uid, 0.95, "country unknown")]) - store.replace_scores(two.id, [(posting("unscored").uid, 0.99, "")]) + store.replace_scores(one.id, [(posting("best").uid, 0.9, "", "", None, 0.9, "fit"), + (posting("kept").uid, 0.3, "", "", None, 0.1, "kept"), + (posting("low").uid, 0.2, "", "", None, 0.2, ""), + (posting("gated").uid, 0.0, "country unknown")]) + store.replace_scores(two.id, [(posting("unscored").uid, 0.99, "", "", None, 0.99, "fit")]) config = Config(database=str(tmp_path / "x.db"), extract={"enabled": True}) model = stub(monkeypatch, lambda roles: [answer(r["uid"]) for r in roles]) - assert store.extract_pending() == 1 - store.save_agent("One", "python backend", agent_id=one.id, - limits=Limits(extract_top=5, extract_min=0.5)) - assert store.extract_pending() == 2, "the floor holds the low one back" - store.save_agent("One", "python backend", agent_id=one.id, - limits=Limits(extract_top=5, extract_min=0)) - assert store.extract_pending() == 3, "the disabled agent's pick never counts" - store.save_agent("One", "python backend", agent_id=one.id, - limits=Limits(extract_top=1, extract_min=0.5)) + assert store.extract_pending() == 2, "the disabled agent's cut never counts" modeltask.run(extract.TASK, config, store) - assert [[r["uid"] for r in batch] for batch in model.asked] == [[posting("best").uid]] + assert [[r["uid"] for r in batch] for batch in model.asked] == [ + [posting("best").uid, posting("kept").uid]] assert [r.uid for r in store.extract_waiting(10, uids=[posting("low").uid])] == \ [posting("low").uid], "a dealt uid is read whatever its rank" @@ -293,9 +284,10 @@ def test_each_agents_best_come_first_then_the_rest(store: Store) -> None: upsert(store, item) one = store.save_agent("One", "python backend") two = store.save_agent("Two", "python backend") - store.replace_scores(one.id, [(items[0].uid, 0.9, ""), (items[1].uid, 0.8, ""), - (items[2].uid, 0.7, "")]) - store.replace_scores(two.id, [(items[3].uid, 0.6, "")]) + store.replace_scores(one.id, [(items[0].uid, 0.9, "", "", None, None, "fit"), + (items[1].uid, 0.8, "", "", None, None, "fit"), + (items[2].uid, 0.7, "", "", None, None, "cv")]) + store.replace_scores(two.id, [(items[3].uid, 0.6, "", "", None, None, "kept")]) got = [r.uid for r in store.extract_waiting(10)] diff --git a/tests/test_enrich.py b/tests/test_fetch_text.py similarity index 78% rename from tests/test_enrich.py rename to tests/test_fetch_text.py index 8667d84..aeba2e6 100644 --- a/tests/test_enrich.py +++ b/tests/test_fetch_text.py @@ -1,7 +1,7 @@ -"""The `enrich` stage: the best roles without text, read on their own pages -after scoring. Which roles and in what order, the budgets, what is stored, -the cascade around it, and what triggers it. No network: a fake host stands -in, answering from a dict.""" +"""The `fetch_text` stage: the roles in the cut without text, read on their +own pages after scoring. Which roles and in what order, the budgets, what is +stored, the cascade around it, and what triggers it. No network: a fake host +stands in, answering from a dict.""" from __future__ import annotations @@ -24,7 +24,7 @@ if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from app.config import Config, ServerConfig # noqa: E402 -from app.jobs import cascade, embed, enrich, stages # noqa: E402 +from app.jobs import cascade, embed, fetch_text, stages # noqa: E402 from app.main import create_app # noqa: E402 from app.storage import Store # noqa: E402 from desk.models import Item # noqa: E402 @@ -78,9 +78,9 @@ def store(tmp_path: Path) -> Store: return Store(tmp_path / "roledesk.db") -def config(tmp_path: Path, **enrich_settings: Any) -> Config: +def config(tmp_path: Path, **settings: Any) -> Config: return Config(database=str(tmp_path / "roledesk.db"), embed={"model": "fake"}, - enrich=enrich_settings) + fetch_text=settings) def board(store: Store, target: str) -> Any: @@ -103,16 +103,20 @@ def role(store: Store, target: str, native: str, *, body: str = "", def scored(store: Store, name: str, scores: dict[str, float], *, - gated: tuple[str, ...] = ()) -> Any: + gated: tuple[str, ...] = (), out: tuple[str, ...] = ()) -> Any: + """An agent whose scores are these, every ungated role in its cut by fit + but those in `out`.""" agent = store.save_agent(name, f"{name} python backend") - store.replace_scores(agent.id, [(uid, value, "gated" if uid in gated else "") - for uid, value in scores.items()]) + store.replace_scores(agent.id, [ + (uid, value, "gated" if uid in gated else "", "", None, value, + "" if uid in gated or uid in out else "fit") + for uid, value in scores.items()]) return agent # -------------------------------------------------------------- which roles -def test_roles_go_best_first_by_their_best_agent_score( +def test_roles_in_the_cut_go_best_first_by_their_best_agent_score( store: Store, tmp_path: Path, web: dict[str, Any]) -> None: board(store, "fake:acme") board(store, "fake:beta") @@ -121,25 +125,28 @@ def test_roles_go_best_first_by_their_best_agent_score( scored(store, "One", {a1: 0.6, a2: 0.9, b1: 0.55}) scored(store, "Two", {a1: 0.95, b1: 0.7}) - assert [(r["uid"], r["score"]) for r in store.enrich_waiting(500, 0.5)] == [ + assert [(r["uid"], r["score"]) for r in store.fetch_text_waiting()] == [ (a1, 0.95), (a2, 0.9), (b1, 0.7)] - assert enrich.plan(config(tmp_path), store) == [("fake:acme", [a1, a2]), + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [a1, a2]), ("fake:beta", [b1])] -def test_a_role_no_agent_ranks_high_enough_gets_no_call( +def test_a_role_in_no_cut_gets_no_call( store: Store, tmp_path: Path, web: dict[str, Any]) -> None: board(store, "fake:acme") low, gated, deep, kept = (role(store, "fake:acme", n) for n in ("1", "2", "3", "4")) - scored(store, "One", {low: 0.4, gated: 0.0, deep: 0.8, kept: 0.9}, gated=(gated,)) - # `top` 1 per agent: only the best ungated role of each agent is in reach. - assert enrich.plan(config(tmp_path, top=1), store) == [("fake:acme", [kept])] - assert enrich.plan(config(tmp_path), store) == [("fake:acme", [kept, deep])] + scored(store, "One", {low: 0.4, gated: 0.0, deep: 0.8, kept: 0.9}, gated=(gated,), + out=(low,)) + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [kept, deep])] + # In another agent's cut, it is read. + scored(store, "Two", {low: 0.4}) + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [kept, deep, low])] + store.delete_agent(store.agents()[1].id) # A disabled agent ranks nothing. agent = store.agents()[0] store.save_agent(agent.name, gates=agent.gates, weights=agent.weights, enabled=False, agent_id=agent.id, limits=agent.limits, prompts=agent.prompts) - assert enrich.plan(config(tmp_path), store) == [] + assert fetch_text.plan(config(tmp_path), store) == [] def test_roles_with_text_hosts_without_pages_and_failing_boards_are_left_out( @@ -152,7 +159,7 @@ def test_roles_with_text_hosts_without_pages_and_failing_boards_are_left_out( down = role(store, "fake:down", "1") store.state["ats:failing:fake:down"] = json.dumps({"since": "2026-09-29", "reason": "500"}) scored(store, "One", {wanted: 0.9, has_text: 0.95, plain: 0.9, down: 0.9}) - assert enrich.plan(config(tmp_path), store) == [("fake:acme", [wanted])] + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [wanted])] def test_a_role_read_already_waits_for_the_retry_and_a_failed_one_goes_last( @@ -164,7 +171,7 @@ def test_a_role_read_already_waits_for_the_retry_and_a_failed_one_goes_last( store.state["ats:detailempty:fake:acme"] = json.dumps({"fake:acme:1": now}) store.state["ats:detailfailed:fake:acme"] = json.dumps(["fake:acme:2"]) scored(store, "One", {read: 0.9, failed: 0.8, fresh: 0.6}) - assert enrich.plan(config(tmp_path), store) == [("fake:acme", [fresh, failed])] + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [fresh, failed])] # ------------------------------------------------------------------ budgets @@ -180,11 +187,11 @@ def test_the_budgets_per_board_and_per_run( for n, uid in enumerate(u for pair in zip(acme, beta, strict=True) for u in pair)}) - assert enrich.plan(config(tmp_path, per_board=2), store) == [ + assert fetch_text.plan(config(tmp_path, per_board=2), store) == [ ("fake:acme", acme[:2]), ("fake:beta", beta[:2])] - assert enrich.plan(config(tmp_path, per_run=3), store) == [ + assert fetch_text.plan(config(tmp_path, per_run=3), store) == [ ("fake:acme", acme[:2]), ("fake:beta", beta[:1])] - assert enrich.plan(config(tmp_path, per_board=1, per_run=5), store) == [ + assert fetch_text.plan(config(tmp_path, per_board=1, per_run=5), store) == [ ("fake:acme", acme[:1]), ("fake:beta", beta[:1])] @@ -192,12 +199,12 @@ def test_the_stage_off_finds_nothing( store: Store, tmp_path: Path, web: dict[str, Any]) -> None: board(store, "fake:acme") scored(store, "One", {role(store, "fake:acme", "1"): 0.9}) - assert len(enrich.batches(config(tmp_path), store)) == 1 - assert enrich.batches(config(tmp_path, enabled=False), store) == [] - stages.set_stage(store, "enrich", True) - assert len(enrich.batches(config(tmp_path, enabled=False), store)) == 1 - stages.set_stage(store, "enrich", False) - assert enrich.batches(config(tmp_path), store) == [] + assert len(fetch_text.batches(config(tmp_path), store)) == 1 + assert fetch_text.batches(config(tmp_path, enabled=False), store) == [] + stages.set_stage(store, "fetch_text", True) + assert len(fetch_text.batches(config(tmp_path, enabled=False), store)) == 1 + stages.set_stage(store, "fetch_text", False) + assert fetch_text.batches(config(tmp_path), store) == [] # ------------------------------------------------------------ what is stored @@ -209,7 +216,7 @@ def test_a_batch_stores_what_the_pages_filled_and_drops_those_vectors( store.put_vectors("fake", [(uid, np.ones(3, dtype=np.float32)) for uid in (one, two, gone)]) web["answers"].update({f"{BASE}/acme/1": b"Python in full", f"{BASE}/acme/2": b"Go too"}) - out = enrich.run_batch(config(tmp_path), store, target="fake:acme", uids=[two, gone, one]) + out = fetch_text.run_batch(config(tmp_path), store, target="fake:acme", uids=[two, gone, one]) assert out == {"target": "fake:acme", "read": 3, "filled": 2, "texts": 2} assert web["asked"] == [f"{BASE}/acme/2", f"{BASE}/acme/3", f"{BASE}/acme/1"] @@ -235,7 +242,7 @@ def test_a_board_switched_off_since_the_plan_reads_nothing( sub = board(store, "fake:acme") uid = role(store, "fake:acme", "1") store.update_subscription(sub.id, fetch=0) - assert enrich.run_batch(config(tmp_path), store, target="fake:acme", uids=[uid])["filled"] == 0 + assert fetch_text.run_batch(config(tmp_path), store, target="fake:acme", uids=[uid])["filled"] == 0 assert web["asked"] == [] @@ -273,15 +280,15 @@ def test_a_search_whose_reader_has_detail_is_planned_like_a_board( f = role(store, "fake:acme", "1") scored(store, "One", {a: 0.9, b: 0.6, j: 0.95, f: 0.7}) - assert enrich.plan(config(tmp_path), store) == [("jobbank:cph", [a, b]), ("fake:acme", [f])] + assert fetch_text.plan(config(tmp_path), store) == [("jobbank:cph", [a, b]), ("fake:acme", [f])] # The budgets are a board's: `per_board` and `per_run` count the search as one. - assert enrich.plan(config(tmp_path, per_board=1), store) == [("jobbank:cph", [a]), + assert fetch_text.plan(config(tmp_path, per_board=1), store) == [("jobbank:cph", [a]), ("fake:acme", [f])] - assert enrich.plan(config(tmp_path, per_run=2), store) == [("jobbank:cph", [a]), + assert fetch_text.plan(config(tmp_path, per_run=2), store) == [("jobbank:cph", [a]), ("fake:acme", [f])] # A failing search is left out, as a failing board is. store.state["search:failing:jobbank:cph"] = "{}" - assert enrich.plan(config(tmp_path), store) == [("fake:acme", [f])] + assert fetch_text.plan(config(tmp_path), store) == [("fake:acme", [f])] def test_a_search_role_read_waits_for_the_retry_and_a_failed_one_goes_last( @@ -296,7 +303,7 @@ def test_a_search_role_read_waits_for_the_retry_and_a_failed_one_goes_last( "jobbank:cph:2": "2020-01-01T00:00:00+00:00"}) # 1 gave no text a moment ago and waits; 2 failed and is due, after 3. - assert enrich.plan(config(tmp_path), store) == [("jobbank:cph", [three, two])] + assert fetch_text.plan(config(tmp_path), store) == [("jobbank:cph", [three, two])] def test_a_batch_reads_a_search_roles_page_and_stores_its_text( @@ -306,7 +313,7 @@ def test_a_batch_reads_a_search_roles_page_and_stores_its_text( store.put_vectors("fake", [(uid, np.ones(3, dtype=np.float32))]) web["answers"][AD_URL] = AD - out = enrich.run_batch(config(tmp_path), store, target="jobbank:cph", uids=[uid]) + out = fetch_text.run_batch(config(tmp_path), store, target="jobbank:cph", uids=[uid]) assert out == {"target": "jobbank:cph", "read": 1, "filled": 1, "texts": 1} assert web["asked"] == [AD_URL] @@ -318,14 +325,14 @@ def test_a_batch_reads_a_search_roles_page_and_stores_its_text( assert json.loads(store.state["ats:detailed:jobbank:cph"]) == ["jobbank:cph:1234"] # Read, it is not planned again, and has no text left to wait for. scored(store, "One", {uid: 0.9}) - assert enrich.plan(config(tmp_path), store) == [] + assert fetch_text.plan(config(tmp_path), store) == [] def test_a_search_batch_counts_a_404_as_read_as_a_board_does( store: Store, tmp_path: Path, web: dict[str, Any]) -> None: search(store) one, two = search_role(store, "jobbank:cph", "1"), search_role(store, "jobbank:cph", "2") - out = enrich.run_batch(config(tmp_path), store, target="jobbank:cph", uids=[one, two]) + out = fetch_text.run_batch(config(tmp_path), store, target="jobbank:cph", uids=[one, two]) assert out["filled"] == 0 and out["read"] == 2 assert json.loads(store.state["ats:detailed:jobbank:cph"]) == [ "jobbank:cph:1", "jobbank:cph:2"] @@ -356,20 +363,21 @@ class _Context: return t -def test_nothing_filled_passes_over_the_prep_embed_and_score_behind_enrich() -> None: +def test_nothing_filled_passes_over_the_prep_embed_and_score_behind_fetch_text() -> None: context = _Context() empty = Tally(results=({"filled": 0},)) - out = cascade.step(None, None, empty, {"cascade": "pull", "task": "enrich", "at": 4}, + out = cascade.step(None, None, empty, {"cascade": "pull", "task": "fetch_text", "at": 4}, context=context) assert out["next"] == "extract" and context.made[0].carries["at"] == 8 out = cascade.step(None, None, Tally(results=({"filled": 2}, {"filled": 0})), - {"cascade": "pull", "task": "enrich", "at": 4}, context=context) + {"cascade": "pull", "task": "fetch_text", "at": 4}, context=context) assert out["next"] == "prep" and context.made[1].carries["at"] == 5 - out = cascade.step(None, None, empty, {"cascade": "enrich", "task": "enrich", "at": 0}, + out = cascade.step(None, None, empty, + {"cascade": "fetch_text", "task": "fetch_text", "at": 0}, context=context) - assert out["next"] == "judge" and context.made[2].carries["at"] == 4 + assert out["next"] == "extract" and context.made[2].carries["at"] == 4 class _Embedder: @@ -378,28 +386,28 @@ class _Embedder: for t in texts]) -def test_the_enrich_job_reads_the_pages_then_embeds_and_scores_those_roles( +def test_the_fetch_text_job_reads_the_pages_then_embeds_and_scores_those_roles( store: Store, tmp_path: Path, web: dict[str, Any], monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(embed, "load_embedder", lambda model: _Embedder()) board(store, "fake:acme") one, two = role(store, "fake:acme", "1"), role(store, "fake:acme", "2") store.put_vectors("fake", [(uid, np.ones(2, dtype=np.float32)) for uid in (one, two)]) - agent = scored(store, "One", {one: 0.9, two: 0.1}) + agent = scored(store, "One", {one: 0.9, two: 0.1}, out=(two,)) web["answers"][f"{BASE}/acme/1"] = b"Python in full" queue = JobQueue(Jobs(tmp_path / "roledesk.db"), config(tmp_path), store, workers={"cpu": 1, "http": 1, "llm": 1}) - started = queue.enqueue("enrich") + started = queue.enqueue("fetch_text") queue.drain(timeout=60) assert started is not None - jobs = sorted(queue.jobs.of_cascade(queue.jobs.last(enrich.KIND).cascade_id), # type: ignore[union-attr] + jobs = sorted(queue.jobs.of_cascade(queue.jobs.last(fetch_text.KIND).cascade_id), # type: ignore[union-attr] key=lambda j: j.id) assert [j.result.get("task") for j in jobs if j.kind == FINAL] == \ - ["enrich", "prep", "embed", "score", "judge"] + ["fetch_text", "prep", "embed", "score", "extract", "score", "judge", "summarise"] assert [j.kind for j in jobs if j.kind != FINAL] == \ - ["enrich", enrich.KIND, "prep", "embed", "score"] + ["fetch_text", fetch_text.KIND, "prep", "embed", "score", "score"] assert web["asked"] == [f"{BASE}/acme/1"] embedded = next(j for j in jobs if j.kind == "embed") assert embedded.result["embedded"] == 1 @@ -424,43 +432,43 @@ def client(app: FastAPI) -> Iterator[TestClient]: ANY_STATE = ("queued", "running", "done", "failed", "cancelled") -def _enrich_behind_a_score(queue: Any) -> Any: - found = queue.jobs.last("enrich", states=ANY_STATE) +def _fetch_text_behind_a_score(queue: Any) -> Any: + found = queue.jobs.last("fetch_text", states=ANY_STATE) assert found is not None assert [queue.jobs.get(i).kind for i in found.depends_on] == ["score"] assert found.after == "done" return found -def test_saving_an_agent_queues_a_score_then_enrich(client: TestClient, app: FastAPI) -> None: +def test_saving_an_agent_queues_a_score_then_fetch_text(client: TestClient, app: FastAPI) -> None: res = client.post("/api/agents", json={"name": "Backend", "intent": "python backend"}) assert res.status_code == 200 - _enrich_behind_a_score(app.state.queue) + _fetch_text_behind_a_score(app.state.queue) res = client.post("/api/agents", json={"id": res.json()["id"], "name": "Backend", "intent": "go backend"}) assert res.status_code == 200 -def test_a_company_affinity_queues_a_score_then_enrich( +def test_a_company_affinity_queues_a_score_then_fetch_text( client: TestClient, app: FastAPI) -> None: store: Store = app.state.store store.save_agent("Backend", "python backend") store.add_company("Acme") res = client.post("/api/companies/acme/affinity", json={"affinity": 2}) assert res.status_code == 200 - found = _enrich_behind_a_score(app.state.queue) + found = _fetch_text_behind_a_score(app.state.queue) assert found.depends_on == [res.json()["score_job"]] -def test_one_score_is_one_enrich(app: FastAPI) -> None: +def test_one_score_is_one_fetch_text(app: FastAPI) -> None: queue = app.state.queue score = queue.enqueue("score") - first = cascade.enrich_after(queue, score) - assert cascade.enrich_after(queue, score).id == first.id - assert cascade.enrich_after(queue, None) is None + first = cascade.fetch_text_after(queue, score) + assert cascade.fetch_text_after(queue, score).id == first.id + assert cascade.fetch_text_after(queue, None) is None -def test_a_vote_queues_neither_score_nor_enrich(client: TestClient, app: FastAPI) -> None: +def test_a_vote_queues_neither_score_nor_fetch_text(client: TestClient, app: FastAPI) -> None: store: Store = app.state.store store.save_agent("Backend", "python backend") item = Item(source_id="fake:acme:1", location=f"{BASE}/acme/1", title="Backend Engineer", @@ -472,5 +480,5 @@ def test_a_vote_queues_neither_score_nor_enrich(client: TestClient, app: FastAPI assert client.delete(f"/api/roles/{item.uid}/vote").status_code == 200 queue = app.state.queue - assert queue.jobs.last("enrich", states=ANY_STATE) is None + assert queue.jobs.last("fetch_text", states=ANY_STATE) is None assert queue.jobs.last("score", states=ANY_STATE) is None diff --git a/tests/test_gazetteer.py b/tests/test_gazetteer.py index 961d277..66d916a 100644 --- a/tests/test_gazetteer.py +++ b/tests/test_gazetteer.py @@ -440,7 +440,6 @@ EUROPE_DK_SE = [ Gate(field="country", values=["DK", "SE"], mode="any"), Gate(field="remote", values=["remote"], mode="any"), Gate(field="place", values=["Malmö", "copenhagen"], mode="any"), - Gate(field="remote", values=["hybrid"], mode="prefer", weight=0.05), ] diff --git a/tests/test_judge.py b/tests/test_judge.py index 2602b7c..b62fd89 100644 --- a/tests/test_judge.py +++ b/tests/test_judge.py @@ -14,7 +14,7 @@ import pytest from app.config import Config from app.jobs import judge, stages -from app.models import CvLanguage, Limits, Prompt +from app.models import CvLanguage, Prompt from app.storage import Store from desk import modeltask from desk.models import Item @@ -85,7 +85,8 @@ def ranked(store: Store, owner: Any, *ids: str) -> list[str]: for sid in ids: store.upsert_role(posting(sid)) uids.append(posting(sid).uid) - store.replace_scores(owner.id, [(uid, 0.9 - n / 100, "") for n, uid in enumerate(uids)]) + store.replace_scores(owner.id, [(uid, 0.9 - n / 100, "", "", None, 0.9 - n / 100, "fit") + for n, uid in enumerate(uids)]) return uids @@ -192,20 +193,27 @@ def test_only_enabled_agents_with_a_cv_and_a_prompt_are_judged(store: Store) -> assert judge.waiting(store) == [] -def test_the_agents_judge_limits_bound_what_is_read(store: Store) -> None: - owner = hunter(store, limits=Limits(judge_top=2, judge_min=0.885)) - uids = ranked(store, owner, "a", "b", "c") - assert [row["uid"] for _, row, _, _ in judge.waiting(store)] == uids[:2] - store.save_agent("Data", prompts=owner.prompts, agent_id=owner.id, - limits=Limits(judge_top=2, judge_min=0.895)) - assert [row["uid"] for _, row, _, _ in judge.waiting(store)] == uids[:1] +def test_the_agents_cut_is_what_is_read_by_fit_then_cv_then_kept(store: Store) -> None: + owner = hunter(store) + for sid in "abcdef": + store.upsert_role(posting(sid)) + uid = {sid: posting(sid).uid for sid in "abcdef"} + store.replace_scores(owner.id, [ + (uid["a"], 0.5, "", "", 0.9, 0.5, "kept"), + (uid["b"], 0.6, "", "", 0.7, 0.4, "cv"), + (uid["c"], 0.7, "", "", 0.1, 0.7, "fit"), + (uid["d"], 0.8, "", "", 0.1, 0.8, "fit"), + (uid["e"], 0.9, "", "", 0.9, 0.9, ""), + (uid["f"], 0.9, "", "", 0.8, 0.3, "cv")]) + assert [row["uid"] for _, row, _, _ in judge.waiting(store)] == [ + uid["d"], uid["c"], uid["f"], uid["b"], uid["a"]] def test_a_role_without_text_is_not_judged(store: Store) -> None: owner = hunter(store) item = posting("empty", body="") store.upsert_role(item) - store.replace_scores(owner.id, [(item.uid, 0.9, "")]) + store.replace_scores(owner.id, [(item.uid, 0.9, "", "", None, 0.9, "fit")]) assert judge.waiting(store) == [] @@ -218,7 +226,7 @@ def test_a_judgement_is_read_again_only_when_what_it_read_changed( assert judge.pending(store) == 0 # A rescore and the role closing leave it be. - store.replace_scores(owner.id, [(uid, 0.8, "")]) + store.replace_scores(owner.id, [(uid, 0.8, "", "", None, 0.8, "fit")]) assert judge.pending(store) == 0 with store.transaction() as conn: conn.execute("UPDATE roles SET closed_at = '2026-10-01' WHERE uid = ?", (uid,)) @@ -274,7 +282,7 @@ def test_a_batch_holds_one_agents_pairs_with_that_agents_profile( first = hunter(store, "First", cv="First CV.") second = hunter(store, "Second", cv="Second CV.") shared = ranked(store, first, "a", "b") - store.replace_scores(second.id, [(uid, 0.9, "") for uid in shared]) + store.replace_scores(second.id, [(uid, 0.9, "", "", None, 0.9, "fit") for uid in shared]) model = stub(monkeypatch) modeltask.run(judge.TASK, config, store) @@ -295,7 +303,7 @@ def test_the_finder_cuts_per_agent_and_finds_nothing_while_the_switch_is_off( first = hunter(store, "First") second = hunter(store, "Second") uids = ranked(store, first, *"abcdefg") - store.replace_scores(second.id, [(uids[0], 0.9, "")]) + store.replace_scores(second.id, [(uids[0], 0.9, "", "", None, 0.9, "fit")]) found = judge.judge_task().finder(config, store) assert [len(b.payload["uids"]) for b in found] == [5, 2, 1] assert found[0].label == "Judging 5 roles for First, 1 of 3" @@ -309,7 +317,7 @@ def test_a_job_given_two_agents_keys_asks_once_for_each( first = hunter(store, "First", cv="First CV.") second = hunter(store, "Second", cv="Second CV.") [uid] = ranked(store, first, "a") - store.replace_scores(second.id, [(uid, 0.9, "")]) + store.replace_scores(second.id, [(uid, 0.9, "", "", None, 0.9, "fit")]) model = stub(monkeypatch) result = judge.run_batch(config, store, uids=[f"{first.id}:{uid}", f"{second.id}:{uid}"]) diff --git a/tests/test_prep.py b/tests/test_prep.py index a38f521..16ba25c 100644 --- a/tests/test_prep.py +++ b/tests/test_prep.py @@ -316,7 +316,7 @@ def test_migration_22_fills_the_host_words_from_raw(tmp_path: Path) -> None: migrated = Store(path) - assert migrated.migrations_applied == [22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] one = row(migrated, "1") assert (one["seniority_host"], one["employment_host"], one["remote_host"], one["country_host"]) == ("senior", "full_time", "true", "DK") diff --git a/tests/test_remote.py b/tests/test_remote.py index 9ff4dce..3fb2274 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -366,7 +366,7 @@ def test_extract_reads_the_area_once_more_and_maps_the_role_before_the_rescore( uid = add(store, "1", "Freiburg, Germany", board="ashby", remote="OnSite") prep.run(store, dupes=False) agent = store.save_agent("Backend", "python backend") - store.replace_scores(agent.id, [(uid, 1.0, "")]) + store.replace_scores(agent.id, [(uid, 1.0, "", "", None, 1.0, "fit")]) with store.transaction() as conn: conn.execute("UPDATE roles SET extracted_at = '2026-09-22T00:00:00+00:00', " "seniority_model = 'senior' WHERE uid = ?", (uid,)) @@ -530,7 +530,7 @@ def test_migration_27_keeps_an_older_readers_remote_as_the_hosts_word(tmp_path: migrated = Store(path) - assert migrated.migrations_applied == [27, 28, 29, 30, 31] + assert migrated.migrations_applied == [27, 28, 29, 30, 31, 32] assert migrated.gazetteer().anywhere is not None row = migrated._row("SELECT remote_host, remote_model, remote_where_model FROM roles " "WHERE uid = ?", (board,)) @@ -540,7 +540,7 @@ def test_migration_27_keeps_an_older_readers_remote_as_the_hosts_word(tmp_path: assert area(migrated, search.uid) == ("", "") with sqlite3.connect(path) as conn: conn.execute("PRAGMA user_version = 26") - assert Store(path).migrations_applied == [27, 28, 29, 30, 31] + assert Store(path).migrations_applied == [27, 28, 29, 30, 31, 32] def test_a_role_waits_for_extract_until_it_was_asked_where(store: Store) -> None: diff --git a/tests/test_score.py b/tests/test_score.py index 2c1903a..a06a686 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -12,6 +12,7 @@ from typing import Any import numpy as np import pytest +from pydantic import ValidationError ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: @@ -353,18 +354,25 @@ def test_any_gates_are_one_group_a_role_must_match_one_of( def test_failed_gates_reads_evidence_only() -> None: gates = [Gate(field="country", values=["DK"]), Gate(field="remote", values=["remote"], mode="any"), - Gate(field="seniority", values=["senior"], mode="any"), - Gate(field="function", values=["engineering"], mode="prefer")] + Gate(field="seniority", values=["senior"], mode="any")] def cand(*states: int) -> Candidate: return Candidate(uid="u", posted_at=NOW.isoformat(), affinity=0, gates=list(states)) - assert score.failed_gates(cand(1, 1, 0, 0), gates) == [] - assert score.failed_gates(cand(-1, -1, -1, 0), gates) == [] - assert score.failed_gates(cand(0, 0, -1, 1), gates) == [ + assert score.failed_gates(cand(1, 1, 0), gates) == [] + assert score.failed_gates(cand(-1, -1, -1), gates) == [] + assert score.failed_gates(cand(0, 0, -1), gates) == [ "country not DK", "none of remote remote, seniority senior"] - assert score.failed_gates(cand(1, 0, 0, 1), gates) == [ + assert score.failed_gates(cand(1, 0, 0), gates) == [ "none of remote remote, seniority senior"] +def test_a_gate_is_require_any_or_exclude_and_has_no_weight() -> None: + """Prefer gates left the agent: hybrid is a feed filter now.""" + with pytest.raises(ValidationError): + Gate(field="remote", values=["hybrid"], mode="prefer") # type: ignore[arg-type] + with pytest.raises(ValidationError): + Gate.model_validate({"field": "remote", "values": ["remote"], "weight": 1.0}) + + def test_an_exclude_gate_on_title_gates_whole_words_and_names_the_value( store: Store, config: Config, fake: FakeEmbedder) -> None: """The sales and data engineering titles the owner does not want: a @@ -389,7 +397,7 @@ def test_an_exclude_gate_on_title_gates_whole_words_and_names_the_value( def test_a_favourite_is_gated_by_an_exclude_gate_it_matches( store: Store, config: Config, fake: FakeEmbedder) -> None: - """A star skips the `any` group, not the titles the agent leaves out.""" + """A star raises the level only: it skips no part of the fetch gate.""" store.add_company("Fave") director = role(store, "1", "Account Director, Nordics", company="Fave") engineer = role(store, "2", "Forward Deployed Engineer", company="Fave") @@ -415,21 +423,6 @@ def test_failed_gates_names_the_value_an_exclude_gate_matched() -> None: assert score.failed_gates(cand(0, 1), gates) == ["country not DK", "title account executive"] -def test_a_prefer_gate_adds_its_weight_to_the_score( - store: Store, config: Config, fake: FakeEmbedder) -> None: - remote = role(store, "1", remote="remote") - office = role(store, "2", remote="onsite") - agent = store.save_agent("Remote", "", [Gate(field="remote", values=["remote"], - mode="prefer", weight=0.5)]) - - run(config, store, score.run_score) - - scores = store.scores(agent.id) - # No intent, so fit is out: recency 0.2 + affinity 0.2 + the gate's 0.5. - assert scores[remote][0] - scores[office][0] == pytest.approx(0.5 / 0.9, abs=1e-5) - assert scores[office][1] == "" - - def test_a_favourite_is_gated_by_a_require_gate_it_fails( store: Store, config: Config, fake: FakeEmbedder) -> None: """A star widens the search to the company, not past a require gate: the @@ -447,40 +440,70 @@ def test_a_favourite_is_gated_by_a_require_gate_it_fails( assert scores[berlin][1] == "" and scores[berlin][0] > 0 -def test_a_favourite_passes_the_any_group_others_fail( +def test_all_countries_passes_the_any_group_a_favourite_does_not( store: Store, config: Config, fake: FakeEmbedder) -> None: """"Denmark or remote" gates the onsite German role of a neutral company - and lets the same role of a starred one through.""" - store.add_company("Fave") + and of a favourite, and lets the same role of an All countries company + through.""" + for name in ("Fave", "Wide", "Other"): + store.add_company(name) fave = role(store, "1", company="Fave", place="Berlin, Germany", remote="onsite") - other = role(store, "2", company="Other", place="Berlin, Germany", remote="onsite") + wide = role(store, "2", company="Wide", place="Berlin, Germany", remote="onsite") + other = role(store, "3", company="Other", place="Berlin, Germany", remote="onsite") agent = store.save_agent("Copenhagen or remote", "", [ Gate(field="country", values=["DK"], mode="any"), Gate(field="remote", values=["remote"], mode="any")]) store.set_affinity(agent.id, "fave", 2) + store.set_all_countries("wide", True) run(config, store, score.run_score) scores = store.scores(agent.id) - assert scores[fave][1] == "" and scores[fave][0] > 0 + assert scores[wide][1] == "" and scores[wide][0] > 0 + assert scores[fave] == (0.0, "none of country DK, remote remote") assert scores[other] == (0.0, "none of country DK, remote remote") -def test_a_favourite_takes_both_halves_of_the_rule_in_one_agent() -> None: - gates = [Gate(field="country", values=["DK"]), +def test_all_countries_skips_the_place_part_and_all_roles_the_titles() -> None: + """The region applies to every company; All countries lifts the `any` + group, All roles the exclude gates on title and no other exclude gate.""" + gates = [Gate(field="country", values=["DK", "SE", "EU"]), + Gate(field="country", values=["DK"], mode="any"), Gate(field="remote", values=["remote"], mode="any"), - Gate(field="seniority", values=["senior"], mode="any")] - def cand(affinity: int, *states: int) -> Candidate: - return Candidate(uid="u", posted_at=NOW.isoformat(), affinity=affinity, - gates=list(states)) - # The any group fails for both; only the neutral company is gated by it. - assert score.failed_gates(cand(2, 1, 0, 0), gates) == [] - assert score.failed_gates(cand(0, 1, 0, 0), gates) == [ - "none of remote remote, seniority senior"] - # The require gate fails for both. - assert score.failed_gates(cand(2, 0, 0, 0), gates) == ["country not DK"] - assert score.failed_gates(cand(0, 0, 0, 0), gates) == [ - "country not DK", "none of remote remote, seniority senior"] + Gate(field="title", values=["account executive"], mode="exclude"), + Gate(field="company", values=["acme"], mode="exclude")] + def cand(*states: int, wide: bool = False, every: bool = False) -> Candidate: + return Candidate(uid="u", posted_at=NOW.isoformat(), all_countries=wide, + all_roles=every, gates=list(states)) + assert score.failed_gates(cand(1, 0, 0, 0, 0), gates) == [ + "none of country DK, remote remote"] + assert score.failed_gates(cand(1, 0, 0, 0, 0, wide=True), gates) == [] + assert score.failed_gates(cand(0, 0, 0, 0, 0, wide=True, every=True), gates) == [ + "country not DK / SE / EU"] + assert score.failed_gates(cand(1, 1, 0, 1, 0), gates) == ["title account executive"] + assert score.failed_gates(cand(1, 1, 0, 1, 0, wide=True), gates) == [ + "title account executive"] + assert score.failed_gates(cand(1, 1, 0, 1, 0, every=True), gates) == [] + assert score.failed_gates(cand(1, 1, 0, 1, 1, every=True), gates) == ["company acme"] + assert score.failed_gates(cand(1, 0, 0, 1, 0, every=True), gates) == [ + "none of country DK, remote remote"] + + +def test_all_roles_keeps_a_title_the_agent_leaves_out( + store: Store, config: Config, fake: FakeEmbedder) -> None: + for name in ("Small", "Other"): + store.add_company(name) + small = role(store, "1", "Account Executive", company="Small") + other = role(store, "2", "Account Executive", company="Other") + agent = store.save_agent("No sales", "", [ + Gate(field="title", values=["account executive"], mode="exclude")]) + store.set_all_roles("small", True) + + run(config, store, score.run_score) + + scores = store.scores(agent.id) + assert scores[small][1] == "" + assert scores[other] == (0.0, "title account executive") def test_a_favourite_with_an_empty_column_is_not_gated() -> None: @@ -808,12 +831,13 @@ def test_a_bad_calibration_is_a_config_error( def test_an_agent_without_an_intent_feeds_neither_model_stage_and_is_named( store: Store, config: Config, clock: Clock, fake: FakeEmbedder) -> None: """Its scores have no fit, so they rank fresh roles, not wanted ones: the - newer role tops it, and extract and summarise must not read it on that.""" + newer role tops it, and nothing of it enters the cut by fit, nor does its + top choose what summarise reads.""" clock.at = NOW - timedelta(days=1) wanted = role(store, "1", "Backend Engineer", body="Python backend") clock.at = NOW fresh = role(store, "2", "Frontend Engineer", body="React apps") - top_one = Limits(extract_top=1, extract_min=0, summarise_top=1, summarise_min=0) + top_one = Limits(cut_fit_top=1, cut_fit_min=0, summarise_top=1) store.save_agent("Backend", "python backend", limits=top_one) blind = store.save_agent("Fresh", "", limits=top_one) store.save_agent("Also fresh", " \n", limits=top_one) @@ -854,26 +878,6 @@ def test_a_gated_role_does_not_stretch_the_fit_range_under_the_fallback( assert scores[close][0] == 1.0 and scores[far][0] == 0.0 -def test_a_small_prefer_gate_no_longer_outweighs_fit( - store: Store, config: Config, fake: FakeEmbedder) -> None: - """The flaw the last real run showed: real cosines sit close together, so - under `(cos + 1) / 2` a prefer gate decided the order over fit. Here the - cosines are about 0.95 and 0.87: the old fit terms differ by 0.04, which - at weight 0.6 is less than the gate's 0.05, so the remote role won. - Calibrated, they are 1 and about 0.89, and the closer role wins.""" - close = role(store, "1", "Backend Engineer", body="Python backend", remote="onsite") - far = role(store, "2", "Backend Engineer", body="Python backend data", remote="remote") - agent = store.save_agent("Backend", "python backend", - [Gate(field="remote", values=["remote"], mode="prefer", - weight=0.05)]) - - run(config, store, embed.run_embed) - run(config, store, score.run_score) - - scores = store.scores(agent.id) - assert scores[close][0] > scores[far][0] - - def test_the_intent_vector_is_cached_until_the_intent_changes( store: Store, config: Config, fake: FakeEmbedder) -> None: role(store, "1") @@ -1018,9 +1022,7 @@ def test_the_shipped_starter_agents_validate() -> None: block = [line[2:] for line in lines[start:] if line.startswith("# ")] examples = [AgentSeed.model_validate(a) for a in yaml.safe_load("\n".join(block))] assert [a.name for a in examples] == ["Remote from Europe", "Europe hybrid"] - # Small enough that a prefer gate breaks ties rather than overrides fit. - assert {g.weight for a in [*agents, *examples] - for g in a.gates if g.mode == "prefer"} == {0.05} + assert examples[0].limits.cut_fit_top == 1000 def test_a_starter_agent_may_list_prompts_or_give_an_intent( @@ -1074,7 +1076,7 @@ def test_the_shipped_starter_searches_validate() -> None: # ---------------------------------------------------------------- cascade -def test_the_pull_job_runs_pull_prep_embed_score_extract_score_then_summarise( +def test_the_pull_job_runs_pull_prep_embed_score_extract_score_judge_then_summarise( store: Store, config: Config, fake: FakeEmbedder, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: store.add_subscription("ats", "greenhouse:acme", label="Acme", company="Acme", @@ -1104,10 +1106,10 @@ def test_the_pull_job_runs_pull_prep_embed_score_extract_score_then_summarise( assert batch is not None jobs = sorted(queue.jobs.of_cascade(batch.cascade_id), key=lambda j: j.id) stages = [j.result.get("task") for j in jobs if j.kind == FINAL] - # Enrich found no board with role pages, so the prep, embed and score + # Fetch text found no board with role pages, so the prep, embed and score # behind it are passed over. - assert stages == ["pull", "prep", "embed", "score", "enrich", "extract", "score", - "summarise", "judge"] + assert stages == ["pull", "prep", "embed", "score", "fetch_text", "extract", "score", + "judge", "summarise"] work = [j.kind for j in jobs if j.kind != FINAL] # The `pull` job and the chain it asked for are one cascade, so the Jobs # tab shows a scheduled pull as one run. Extract and summarise are off and @@ -1159,14 +1161,14 @@ def test_the_model_batches_are_isolated_with_a_timeout() -> None: def test_follows_walks_the_cascade_by_position_and_stops_at_its_end() -> None: - """By position, not by name: `score` is in the chain twice, once before - extract and once after, and the second must lead on to summarise.""" - assert cascade.CASCADES["pull"] == ("pull", "prep", "embed", "score", "enrich", "prep", - "embed", "score", "extract", "score", "summarise", - "judge") + """By position, not by name: `score` is in the chain three times, and the + one after extract must lead on to judge.""" + assert cascade.CASCADES["pull"] == ("pull", "prep", "embed", "score", "fetch_text", "prep", + "embed", "score", "extract", "score", "judge", + "summarise") assert [cascade.follows("pull", at) for at in range(12)] == \ - ["prep", "embed", "score", "enrich", "prep", "embed", "score", "extract", "score", - "summarise", "judge", ""] + ["prep", "embed", "score", "fetch_text", "prep", "embed", "score", "extract", "score", + "judge", "summarise", ""] assert cascade.follows("pull", -1) == "" and cascade.follows("pull", 11) == "" assert cascade.follows("nope", 0) == "" assert cascade.chain("pull").carries == {"cascade": "pull", "task": "pull", "at": 0} @@ -1186,4 +1188,4 @@ def test_step_hands_the_position_on(monkeypatch: pytest.MonkeyPatch) -> None: assert made[0].carries == {"cascade": "pull", "task": "extract", "at": 8} out = cascade.step(None, None, Tally(), {"cascade": "pull", "task": "score", "at": 9}, context=Context()) - assert out["next"] == "summarise" and made[1].carries["at"] == 10 + assert out["next"] == "judge" and made[1].carries["at"] == 10 diff --git a/tests/test_stages.py b/tests/test_stages.py index d49cabf..66ad19a 100644 --- a/tests/test_stages.py +++ b/tests/test_stages.py @@ -45,11 +45,12 @@ def store(tmp_path: Path) -> Store: def ranked(store: Store) -> None: - """Two roles scored well for one enabled agent: inside both stages' bounds.""" + """Two roles scored well for one enabled agent, both in its cut.""" for sid in ("1", "2"): store.upsert_role(posting(sid)) one = store.save_agent("Backend", "python backend") - store.replace_scores(one.id, [(posting("1").uid, 1.0, ""), (posting("2").uid, 0.9, "")]) + store.replace_scores(one.id, [(posting("1").uid, 1.0, "", "", None, 1.0, "fit"), + (posting("2").uid, 0.9, "", "", None, 0.9, "fit")]) def answered_by(monkeypatch: pytest.MonkeyPatch, backend: str) -> None: @@ -159,7 +160,7 @@ def test_get_stages_lists_all_four_with_their_source_model_and_who_answers( {"name": "judge", "on": False, "source": "config", "model": "claude-haiku-4-5-20251001", "backend": "api", "answer": "answered by the Claude API"}, - {"name": "enrich", "on": True, "source": "config", "model": "", "backend": "", + {"name": "fetch_text", "on": True, "source": "config", "model": "", "backend": "", "answer": "reads role pages, asks no model"}, ] @@ -169,7 +170,7 @@ def test_who_answers_is_said_for_the_api_claude_code_and_nobody( answers = {} for backend in ("api", "claude_code", ""): answered_by(monkeypatch, backend) - [first, second, third, _enrich] = client.get("/api/stages").json() + [first, second, third, _fetch_text] = client.get("/api/stages").json() assert first["backend"] == second["backend"] == third["backend"] == backend answers[backend] = first["answer"] @@ -196,16 +197,16 @@ def test_put_stores_the_setting_returns_the_same_shape_and_starts_nothing( assert res.json()["on"] is False and store.state["stage:summarise"] == "off" -def test_the_enrich_switch_is_listed_and_stored_like_the_others( +def test_the_fetch_text_switch_is_listed_and_stored_like_the_others( client: TestClient, store: Store) -> None: - res = client.put("/api/stages/enrich", json={"on": False}) + res = client.put("/api/stages/fetch_text", json={"on": False}) assert res.status_code == 200 - assert res.json()["name"] == "enrich" and res.json()["on"] is False - assert store.state["stage:enrich"] == "off" + assert res.json()["name"] == "fetch_text" and res.json()["on"] is False + assert store.state["stage:fetch_text"] == "off" listed = {stage["name"]: stage for stage in client.get("/api/stages").json()} - assert list(listed) == ["extract", "summarise", "judge", "enrich"] - assert listed["enrich"]["on"] is False and listed["enrich"]["source"] == "stored" + assert list(listed) == ["extract", "summarise", "judge", "fetch_text"] + assert listed["fetch_text"]["on"] is False and listed["fetch_text"]["source"] == "stored" def test_an_unknown_stage_is_404_and_a_bad_body_is_422(client: TestClient) -> None: diff --git a/tests/test_storage.py b/tests/test_storage.py index 8019a05..29037f4 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -70,7 +70,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 == 31 + assert LATEST == 32 # A second open finds it current and applies nothing. assert Store(path).migrations_applied == [] @@ -190,7 +190,7 @@ def test_a_v14_file_gains_the_watchlist_columns_and_leaves_companies_unrated( migrated = Store(path) - assert migrated.migrations_applied == [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] acme = migrated.company("acme") assert acme is not None assert (acme.ats, acme.careers_url) == ("teamtailor", "https://a.example") @@ -395,7 +395,7 @@ def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: P migrated = Store(path) - assert migrated.migrations_applied == [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] 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] @@ -419,7 +419,7 @@ def test_v16_gives_a_company_no_reason_and_keeps_its_row(tmp_path: Path) -> None migrated = Store(path) - assert migrated.migrations_applied == [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] acme = migrated.company("acme") assert acme is not None assert (acme.careers_url, acme.notes, acme.hand_reason) == ( @@ -448,7 +448,7 @@ def test_v17_folds_every_place_and_levels_the_titles_extract_has_not_read( migrated = Store(path) - assert migrated.migrations_applied == [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] with sqlite3.connect(path) as conn: rows = {uid: (fold, level) for uid, fold, level in conn.execute( "SELECT uid, place_fold, seniority FROM roles")} @@ -494,22 +494,21 @@ def test_prompts_are_saved_replaced_reordered_and_deleted(store: Store) -> None: assert conn.execute("SELECT COUNT(*) FROM agent_prompts").fetchone()[0] == 0 -def test_an_agent_whose_prompts_are_all_empty_feeds_neither_model_stage( +def test_an_agent_whose_prompts_are_all_empty_does_not_choose_what_summarise_reads( store: Store) -> None: upsert(store, posting("a")) upsert(store, posting("b")) a, b = posting("a").uid, posting("b").uid - top = Limits(extract_top=1, extract_min=0, summarise_top=1, summarise_min=0) + top = Limits(summarise_top=1) 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) == [] + store.replace_scores(blank.id, [(a, 0.9, ""), (b, 0.8, "")]) + assert 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)} + assert [r.uid for r in store.summarise_waiting(10)] == [a] def test_state_is_a_mapping(store: Store) -> None: @@ -691,7 +690,7 @@ def test_v19_gives_every_role_its_places_and_splits_its_level(tmp_path: Path) -> migrated = Store(path) - assert migrated.migrations_applied == [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] assert [p.name for p in migrated.role_places(joined.uid)] == ["Copenhagen", "Malmö"] assert [p.name for p in migrated.role_places(detailed.uid)] == ["Aarhus"] assert migrated.role_places(stated.uid) == [] @@ -733,7 +732,7 @@ def test_v25_clears_the_model_levels_the_ladder_splits_and_sets_the_level( migrated = Store(path) - assert migrated.migrations_applied == [25, 26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [25, 26, 27, 28, 29, 30, 31, 32] with sqlite3.connect(path) as conn: found = {sid: rest for sid, *rest in conn.execute( "SELECT source_id, seniority_model, seniority_stated, seniority_title, seniority " @@ -751,12 +750,12 @@ def test_v26_adds_posting_url_empty_and_runs_twice_without_harm(tmp_path: Path) migrated = Store(path) - assert migrated.migrations_applied == [26, 27, 28, 29, 30, 31] + assert migrated.migrations_applied == [26, 27, 28, 29, 30, 31, 32] role = migrated.role(posting("a").uid) assert role is not None and role.posting_url == "" with sqlite3.connect(path) as conn: conn.execute("PRAGMA user_version = 25") # the column is there already - assert Store(path).migrations_applied == [26, 27, 28, 29, 30, 31] + assert Store(path).migrations_applied == [26, 27, 28, 29, 30, 31, 32] def test_a_pull_keeps_the_posting_url(store: Store) -> None: @@ -1277,21 +1276,34 @@ def test_votes_are_per_agent(store: Store) -> None: 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"): + for sid in ("1", "2", "3", "4"): upsert(store, 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.replace_scores(agent.id, [(uid["1"], 0.9, ""), (uid["2"], 0.1, ""), + (uid["4"], 0.0, "country not DK")]) # 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 newest] == ["4", "3", "2", "1"] + assert [r.score for r in newest] == [None, None, None, None] + assert [r.vote for r in newest] == [None, None, None, 1] + assert store.count_roles(agent=agent.id, by_score=False) == 4 assert [r.source_id for r in store.roles(agent=agent.id)] == ["1", "2"] + # Kept to the agent's fetch gate, newest lists only what it scored and + # passed, as the score order does; `gated` shows the rest with the reason. + fenced = store.roles(agent=agent.id, by_score=False, fetch_gate=True) + assert [r.source_id for r in fenced] == ["2", "1"] + assert [r.score for r in fenced] == [None, None] + assert store.count_roles(agent=agent.id, by_score=False, fetch_gate=True) == 2 + page = store.roles_page(agent=agent.id, by_score=False, fetch_gate=True, gated=True) + assert [(r.uid, r.gated_reason) for r in page] == [ + (uid["4"], "country not DK"), (uid["2"], ""), (uid["1"], "")] + assert store.count_roles(agent=agent.id, by_score=False, fetch_gate=True, + gated=True) == 3 + def test_read_state_is_set_cleared_and_a_vote_marks_read( store: Store, clock: Clock) -> None: @@ -1738,39 +1750,30 @@ def test_a_summarys_tags_land_with_it_and_not_on_a_rejected_late_answer( def test_summarise_waits_on_the_top_of_each_enabled_agent_by_its_own_limits( store: Store) -> None: - """Each agent contributes its own top, as deep and as low as its own - `limits` say; a closed role and a gated score are in nobody's top. A + """Each agent contributes the top of its feed, its best `summarise_top` + final scores; a closed role and a gated score are in nobody's top. A hidden company needs no case: `score` writes no rows for it under the - agent that hid it. An agent saved before v8 has `limits` of '{}', which - reads as the defaults.""" + agent that hid it. `limits` of '{}' reads as the defaults.""" for sid in "abcd": upsert(store, posting(sid)) a, b, c, d = (posting(s).uid for s in "abcd") with store.transaction() as conn: conn.execute("UPDATE roles SET closed_at = ? WHERE uid = ?", (store.now(), d)) - one = store.save_agent("One", "python backend", - limits=Limits(summarise_top=1, summarise_min=0)) - two = store.save_agent("Two", "python backend", - limits=Limits(summarise_top=1, summarise_min=0)) + one = store.save_agent("One", "python backend", limits=Limits(summarise_top=1)) + two = store.save_agent("Two", "python backend", limits=Limits(summarise_top=1)) 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.7, ""), (b, 0.8, "")]) assert {r.uid for r in store.summarise_waiting(10)} == {a, b} # Two goes deeper: its second, c, joins; One's second, b, was in already. - store.save_agent("Two", "python backend", agent_id=two.id, - limits=Limits(summarise_top=2, summarise_min=0)) + store.save_agent("Two", "python backend", agent_id=two.id, limits=Limits(summarise_top=2)) assert {r.uid for r in store.summarise_waiting(10)} == {a, b, c} assert store.summarise_pending() == 3 assert [r.uid for r in store.summarise_waiting(10, uids=[d])] == [d] - # The floor is Two's own: c at 0.7 drops, One's b at 0.5 stays. - store.save_agent("Two", "python backend", agent_id=two.id, - limits=Limits(summarise_top=2, summarise_min=0.75)) - store.save_agent("One", "python backend", agent_id=one.id, - limits=Limits(summarise_top=2, summarise_min=0)) - assert {r.uid for r in store.summarise_waiting(10)} == {a, b} - # '{}' is the defaults, 30 at 0.65: One's b at 0.5 drops, but Two's b at - # 0.8 and c at 0.7 clear it. + store.save_agent("Two", "python backend", agent_id=two.id, limits=Limits(summarise_top=0)) + assert {r.uid for r in store.summarise_waiting(10)} == {a} + # '{}' is the defaults, 50 deep. with store.transaction() as conn: conn.execute("UPDATE agents SET limits = '{}'") assert {r.uid for r in store.summarise_waiting(10)} == {a, b, c} diff --git a/tests/test_web.py b/tests/test_web.py index aa0a1de..8cbdd6c 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -351,6 +351,7 @@ def test_a_vote_is_set_cleared_and_filtered_on(client: TestClient, store: Store) upsert(store, posting(sid)) uid = {r.source_id: r.uid for r in store.roles()} agent = store.save_agent("A") + store.replace_scores(agent.id, [(u, 0.5, "") for u in uid.values()]) # No `agent` in the body: the default agent's vote. res = client.post(f"/api/roles/{uid['liked']}/vote", json={"value": 1}) @@ -394,6 +395,8 @@ def test_votes_are_per_agent_over_the_api(client: TestClient, store: Store) -> N uid = posting("1").uid one = store.save_agent("One") two = store.save_agent("Two") + for agent in (one, two): + store.replace_scores(agent.id, [(uid, 0.5, "")]) assert client.post(f"/api/roles/{uid}/vote", json={"value": 1, "agent": one.id}).json()["vote"] == 1 assert client.post(f"/api/roles/{uid}/vote", json={"value": -1, "agent": two.id}).json()["vote"] == -1 @@ -439,11 +442,15 @@ def test_roles_with_an_agent_can_be_ordered_newest_and_keep_the_votes( store.replace_scores(agent.id, [(uid["1"], 0.9, ""), (uid["2"], 0.1, "")]) # 3 unscored store.set_vote(agent.id, uid["1"], 1) + # Newest keeps to the agent's fetch gate: only what it scored and passed. page = client.get("/api/roles", params={"agent": agent.id, "order": "newest"}).json() + assert [r["uid"] for r in page["items"]] == [uid["2"], uid["1"]] + assert [r["score"] for r in page["items"]] == [None, None] + assert [r["vote"] for r in page["items"]] == [None, 1] + assert page["total"] == 2 + # Without an agent named, newest is every open role. + page = client.get("/api/roles", params={"order": "newest"}).json() assert [r["uid"] for r in page["items"]] == [uid["3"], uid["2"], uid["1"]] - assert [r["score"] for r in page["items"]] == [None, None, None] - assert [r["vote"] for r in page["items"]] == [None, None, 1] - assert page["total"] == 3 scored = client.get("/api/roles", params={"agent": agent.id, "order": "score"}).json() assert [r["uid"] for r in scored["items"]] == [uid["1"], uid["2"]] assert client.get("/api/roles", params={"agent": agent.id, @@ -1086,7 +1093,9 @@ def test_agents_are_created_updated_listed_and_deleted(client: TestClient) -> No agent = created.json() assert agent["weights"] == {"fit": 0.6, "recency": 0.2, "affinity": 0.2, "taste": 0.2, "cv_skills": 30, "cv_experience": 25, "cv_career": 30} - assert agent["gates"][0]["weight"] == 1.0 + assert agent["gates"] == body["gates"] + prefer = {**body, "gates": [{"field": "remote", "values": ["hybrid"], "mode": "prefer"}]} + assert client.post("/api/agents", json=prefer).status_code == 422 updated = client.post("/api/agents", json={**body, "id": agent["id"], "enabled": False, "weights": {"fit": 1, "recency": 0, @@ -1179,19 +1188,20 @@ def test_a_gate_on_an_unknown_field_is_refused(client: TestClient) -> None: def test_an_agents_limits_are_saved_listed_and_checked(client: TestClient) -> None: - """How deep the model stages read is the agent's own; a score outside - 0..1 is refused.""" + """The cut and the depth of summarise are the agent's own; a minimum + outside 0..1 and a key from before the cut are refused.""" default = client.post("/api/agents", json={"name": "Wide"}).json() - assert default["limits"] == {"extract_top": 500, "extract_min": 0.55, - "summarise_top": 30, "summarise_min": 0.65, - "judge_top": 150, "judge_min": 0.55, "judge_cv_top": 50} - narrow = {"extract_top": 50, "extract_min": 0.6, "summarise_top": 5, "summarise_min": 0.7, - "judge_top": 20, "judge_min": 0.6, "judge_cv_top": 10} + assert default["limits"] == {"cut_fit_top": 500, "cut_fit_min": 0.6, + "cut_cv_top": 150, "cut_cv_min": 0.6, "summarise_top": 50} + narrow = {"cut_fit_top": 50, "cut_fit_min": 0.7, "cut_cv_top": 10, "cut_cv_min": 0.75, + "summarise_top": 5} saved = client.post("/api/agents", json={"name": "Narrow", "limits": narrow}) assert saved.status_code == 200 and saved.json()["limits"] == narrow - assert [a["limits"]["extract_top"] for a in client.get("/api/agents").json()] == [50, 500] - bad = client.post("/api/agents", json={"name": "Bad", "limits": {"extract_min": 2}}) + assert [a["limits"]["cut_fit_top"] for a in client.get("/api/agents").json()] == [50, 500] + bad = client.post("/api/agents", json={"name": "Bad", "limits": {"cut_fit_min": 2}}) assert bad.status_code == 422 + old = client.post("/api/agents", json={"name": "Old", "limits": {"extract_top": 5}}) + assert old.status_code == 422 def test_roles_by_agent_are_ordered_by_score_and_hide_the_gated( From 8b6c6d5ac3daa14e13674a15d10bbb2a6f1c0178 Mon Sep 17 00:00:00 2001 From: Wolf Date: Sun, 4 Oct 2026 15:16:38 +0200 Subject: [PATCH 2/2] Test migration 32, the cut, saved filters and the override rescore, and bring the READMEs and config comments to fetch text and the cut Co-Authored-By: Claude Opus 5.5 --- README.md | 10 ++-- app/jobs/judge.py | 2 +- app/models.py | 13 ++--- config.example.yaml | 6 +-- plugins/README.md | 40 +++++++-------- tests/test_cv_feed.py | 2 +- tests/test_score.py | 71 +++++++++++++++++++++++++- tests/test_storage.py | 114 ++++++++++++++++++++++++++++++++++++++++++ tests/test_web.py | 39 +++++++++++++++ 9 files changed, 260 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index cdbb9cc..0609118 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Built on the [boilerplate](../boilerplate): its job queue and scheduler (`pipeli ```bash uv sync --all-extras uv run app companies seed # 362 European employers from seed/companies.json, 286 with a board; a re-run adds new rows and boards -uv run app job pull # read every board and search, map and fold, embed and score, read the best roles' pages, extract, rescore, summarise +uv run app job pull # read every board and search, map and fold, embed and score, fetch the text of the roles in the cut, extract, rescore, judge, summarise uv run app serve # http://127.0.0.1:8790 ``` @@ -42,7 +42,7 @@ uv run app prep unmapped # the host words no table The seed is a hand-picked list of European tech employers, not a list of boards: a company whose careers site runs on something the app cannot read (its own pages, a host with no module, a site whose robots.txt disallows reading) is still a row, with its careers page and the reason, shown on the Watchlist tab for checking by hand and under Companies as "not readable yet", so the gap is visible and one module at a time closes it. A row where nothing was found reads "board not found"; the "Not readable yet" filter shows both. Each row has a Set board control (Change once it has one) to attach, replace or detach its board by hand. Replacing or detaching closes the old board's open roles, and asks first when there are open roles to lose; a board is read once before it is kept, and one that lists no roles yet is kept too. Setting a board queues a pull right after, so its roles are read by the one already running or the very next one, rather than waiting for the schedule. `companies discover` reads a company's site for a link into one of the boards it knows, probes the hosts with a cheap per-slug endpoint only when no page names any host, and with `--add` sets the board of a new or existing company, or records the host it found when nothing readable was. A probe hit is a guess by name: check the roles it lists before trusting it for a common name. -`app stages` shows or switches extract, summarise and enrich, `app seniority` shows per host how often the stated level equals the model's, `app dupes` recomputes which open roles are copies of one posting, and `app prep` maps the host words and folds the copies without a pull. +`app stages` shows or switches extract, summarise, judge and fetch text, `app seniority` shows per host how often the stated level equals the model's, `app dupes` recomputes which open roles are copies of one posting, and `app prep` maps the host words and folds the copies without a pull. A Jobindex query is its own syntax: words are OR, `"a b"` is a phrase, `+word` is required; `--area 1` is Region Hovedstaden. A Platsbanken query is free text and `--region` the id of a län (`CaRE_1nn_cSU` is Skåne). An Adzuna search needs a free key from developer.adzuna.com in `ADZUNA_APP_ID` and `ADZUNA_APP_KEY`. A freehire query is free text over about 50 hosts whose words are OR over title, company, text and place, so it is noisy: `--country` takes comma-separated ISO codes (`dk,se`) and `--remote` a work mode. A Jobnet query is a loose match without phrases and `--region` one of its five regions (`HovedstadenOgBornholm`, `Midtjylland`, `Syddanmark`, `OevrigeSjaelland`, `Nordjylland`). An Akademikernes Jobbank query is OR words, `--region` its `amt` id (2 is Storkøbenhavn) and `--remote` `helt` or `delvist`. A freehire role whose employer is a board roledesk reads is a copy of that board's role and is hidden behind it. `searches add` runs the search once and saves it only if it finds something. Switching a board or search off closes its open roles, and asks first when there are open roles to lose; switching it back on, or saving a search, queues a pull right after, so the ones it still lists reopen without waiting for the schedule. The `embeddings` extra is what `fit` needs, `agent` is for `extract` on a model, `passkeys` for the Account tab's passkeys. @@ -60,13 +60,13 @@ The cog opens `/settings`: a rail of pages on the left, the page beside it, stic | | | |---|---| -| **Search agents** | a card per agent, newest first: name, an On/Off switch, its prompts, its rules in words, and its weights. Edit turns the card into the form in place (name, prompts as rows, a Where block for the location rules, the other require/any/prefer rules as rows, fetch terms, weights and limits); New agent puts a blank card on top. Saving or switching on rescores; deleting one takes its votes, liked companies and scores with it | -| **Sources** | one page showing one section at a time, picked in the rail or by the URL fragment: job boards (queries on Jobindex, Adzuna, Platsbanken, freehire, Jobnet or Akademikernes Jobbank: new, try, save, edit, remove), company boards (every company, its board or the host it cannot be read from, its open roles and its affinity for the picked agent, what the pull asks its board for, as a reach line such as "follows agents: DK, SE, remote · all roles", the board's size with a Recheck, the All countries and All roles overrides, following a company, setting or changing a company's board), runs (Run the pull now, the switches of extract, summarise and enrich, what is running, the schedule, and the Problems log) and unmapped (the place texts no place resolves and the host words no table maps, to copy) | +| **Search agents** | a card per agent, newest first: name, an On/Off switch, its prompts, its rules in words, and its weights. Edit turns the card into the form in place (name, prompts as rows, a Where block for the location rules, the other require/any/exclude rules as rows, fetch terms, weights and limits); New agent puts a blank card on top. Saving or switching on rescores; deleting one takes its votes, liked companies and scores with it | +| **Sources** | one page showing one section at a time, picked in the rail or by the URL fragment: job boards (queries on Jobindex, Adzuna, Platsbanken, freehire, Jobnet or Akademikernes Jobbank: new, try, save, edit, remove), company boards (every company, its board or the host it cannot be read from, its open roles and its affinity for the picked agent, what the pull asks its board for, as a reach line such as "follows agents: DK, SE, remote · all roles", the board's size with a Recheck, the All countries and All roles overrides, following a company, setting or changing a company's board), runs (Run the pull now, the switches of extract, summarise, judge and fetch text, what is running, the schedule, and the Problems log) and unmapped (the place texts no place resolves and the host words no table maps, to copy) | | **Account** | the password, sessions and passkeys. Open on loopback, locked on anything else | 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 role whose text is a teaser or missing (a search role, or a board role that `enrich` has not read yet) gets a Fetch button that reads the posting page once and stores the text; for a Jobindex role it follows the "Se jobbet" link to the posting itself. A Summarise button asks for a short summary of that one role; the switch under Settings, Sources, Runs (default from `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. +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 role whose text is a teaser or missing (a search role, or a board role whose text fetch text has not read yet) gets a Fetch button that reads the posting page once and stores the text; for a Jobindex role it follows the "Se jobbet" link to the posting itself. A Summarise button asks for a short summary of that one role; the switch under Settings, Sources, Runs (default from `config.yaml`'s `summarise:` block) turns summarising on for the pull itself, bounded to the top of each agent's feed (its `summarise_top`) and the roles liked or saved, so the cost stays flat. ## Docs diff --git a/app/jobs/judge.py b/app/jobs/judge.py index 1d0796d..a0ded90 100644 --- a/app/jobs/judge.py +++ b/app/jobs/judge.py @@ -272,7 +272,7 @@ def build_batch(config: Config, store: Store, limit: int = 5, def _mark(value: Any) -> int | None: if isinstance(value, bool) or not isinstance(value, int | float): return None - return max(0, min(100, int(round(value)))) + return max(0, min(100, round(value))) def _lines(value: Any) -> list[str]: diff --git a/app/models.py b/app/models.py index f205a53..1d86635 100644 --- a/app/models.py +++ b/app/models.py @@ -770,7 +770,8 @@ Status = Literal["saved", "applied", "screen", "technical", "onsite", "offer", "rejected", "withdrawn"] STATUSES: tuple[str, ...] = get_args(Status) -#: -1 mute, 0 neutral, 1 interested, 2 favourite (skips the `any` gates). +#: -1 mute, 0 neutral, 1 interested, 2 favourite. Affinity raises a role's +#: score and its levels for the cut; it skips no part of the fetch gate. AFFINITY = range(-1, 3) #: Why a company is on the list checked by hand. `unread` is a host the app @@ -1038,7 +1039,7 @@ def capped_career(career: int, broken: int = 0) -> int: def cv_score(skills: int, experience: int, career: int, - weights: "Weights | None" = None, broken: int = 0) -> float | None: + weights: Weights | None = None, broken: int = 0) -> float | None: """The one number of a CV match, 0 to 100: the three numbers of the judge averaged by the agent's `cv_skills`, `cv_experience` and `cv_career` weights (30, 25 and 30 by default), career as @@ -1562,10 +1563,10 @@ class Agent(Model): and limits. The prompts are rows of `agent_prompts`, read alongside; their cached vectors are columns this does not declare. - `fetch_terms` are words a role must match for a big board read with the - wide reach to send it (`app/jobs/reach.py`): the pull's terms are the - union over the enabled agents, and an agent with none means no board is - narrowed by words. They decide what is fetched, not what ranks. + `fetch_terms` are words a role must match for a big board to send it, + where its host can filter on them (`app/jobs/reach.py`): the pull's terms + are the union over the enabled agents, and an agent with none means no + board is narrowed by words. They decide what is fetched, not what ranks. `cv_text`, `cv_languages`, `like_text`, `avoid_text` and `must_text` are what `judge` reads a role against: the CV, the languages it states with diff --git a/config.example.yaml b/config.example.yaml index 60b26ba..2c81a65 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -53,9 +53,9 @@ agent: # Title and description in, gate columns out (app/jobs/extract.py). Off by # default: on the API backend every batch of 20 roles is a paid call. On, a -# pull reads only the best roles of every enabled agent, as deep and as low -# as that agent's own limits say (see agents below), after a first score; a -# second score then applies the gates to what was filled. `enabled` is only the +# pull reads only the cut of every enabled agent (its limits, see agents +# below), after a first score; a second score then applies the fetch gate and +# the cut to what was filled. `enabled` is only the # default for a fresh database: the switch under Settings, Sources, Runs (or # `app stages extract on|off`) overrides it and needs no restart. extract: diff --git a/plugins/README.md b/plugins/README.md index 390dbcf..c50beea 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -4,7 +4,7 @@ This file lists every source roledesk reads roles from, how each is read and wha The code has two plugins: `ats` (`plugins/ats.py`) reads the board of a company someone listed, with one module per host in `plugins/readers/`; `search` (`plugins/search.py`) runs saved queries on sites that list roles across companies. Below, a module of `ats` is a "host" and a module of `search` is a "provider". A company's list of roles on a host is its "board"; a board is attached as a target `host:slug`, e.g. `workday:lego.wd103/LEGO_External`. -What a reader does is declared in its `READS` (`plugins/readers/_contract.py`) and listed per reader on the Sources page. The per-host sections below keep what a declaration cannot say: endpoints, quirks, examples and limits. The shared limits are constants in `plugins/readers/_runner.py` (`MAX_BYTES`, `TIMEOUTS`, `DEADLINE`, `MAX_PAGES`, `PAGE_DELAY`, `DETAIL_CAP`, `SUSPECT_SHARE`, `MAX_SERIES`), `app/jobs/pull.py` (`BATCH_SIZE`, `STALE_DAYS`), `app/jobs/reach.py` (`SMALL`) and the `enrich:` block of the config; the table "Rules every source shares" states their effect and is not a second place to change them. +What a reader does is declared in its `READS` (`plugins/readers/_contract.py`) and listed per reader on the Sources page. The per-host sections below keep what a declaration cannot say: endpoints, quirks, examples and limits. The shared limits are constants in `plugins/readers/_runner.py` (`MAX_BYTES`, `TIMEOUTS`, `DEADLINE`, `MAX_PAGES`, `PAGE_DELAY`, `DETAIL_CAP`, `SUSPECT_SHARE`, `MAX_SERIES`), `app/jobs/pull.py` (`BATCH_SIZE`, `STALE_DAYS`), `app/jobs/reach.py` (`SMALL`) and the `fetch_text:` block of the config; the table "Rules every source shares" states their effect and is not a second place to change them. ## Flow @@ -26,13 +26,13 @@ The order of the stages is `CASCADES` in `app/jobs/cascade.py`; this section say | `sweep` | `stamp`, the cursor of a read cut at `MAX_PAGES` | `_runner.read` on the next pull | | `count` | `stamp`, the roles the last read listed; reset by Recheck | `_runner.read`, the 10% guard | | `missing` | `close_boards` (`_close_swept`), the ids the last finished sweep missed | the next finished sweep | -| `failing` | `ats._board` and `run_batch` through `record_failure`, `{since, reason}`; removed after the next stamp. `search:failing:` is the same for a search | `close_boards` (21 days), the Companies list and the search row, `enrich`, the guard's escape after 21 days | +| `failing` | `ats._board` and `run_batch` through `record_failure`, `{since, reason}`; removed after the next stamp. `search:failing:` is the same for a search | `close_boards` (21 days), the Companies list and the search row, `fetch_text`, the guard's escape after 21 days | | `facets` | `_runner.facets_for`, kept one day | `reach.site_facets`, the Companies line | | `size`, `sizechecked` | `ats.keep_size`: `set_board`, Recheck, `app companies size`, or the first read of an unsized board | `reach.sizes`, which decides whether the board is filtered | -| `detailed`, `detailfailed`, `detailempty` | `ats.read_board_details` (`enrich`): ids read, ids whose read failed, `{id: date}` of reads that gave no text; the pull drops the ids a complete listing no longer lists | `enrich.plan` through `detail_due` | +| `detailed`, `detailfailed`, `detailempty` | `ats.read_board_details` (`fetch_text`): ids read, ids whose read failed, `{id: date}` of reads that gave no text; the pull drops the ids a complete listing no longer lists | `fetch_text.plan` through `detail_due` | 4. `close_boards`, the settle of the pull, closes what a complete read did not list, closes a board or search that failed for `STALE_DAYS`, releases the run, and raises one `pull.settled` notice in the Problems log when a target did not answer or was read incomplete, or a board closed more than 50 roles or more than half of its open ones. -5. Then, in order: `prep` (maps the hosts' words, resolves places and company aliases, folds copies with `app/dupes.py`), `embed`, `score`, `enrich` (reads the role pages of the best-ranked roles without text), `prep`, `embed` and `score` again for the roles it filled (skipped when it filled none), `extract`, `score`, `summarise`. Extract and summarise are switches under Runs; enrich is on by default. +5. Then, in order: `prep` (maps the hosts' words, resolves places and company aliases, folds copies with `app/dupes.py`), `embed`, `score` (which writes each agent's cut), `fetch_text` (reads the role pages of the roles in the cut without text), `prep`, `embed` and `score` again for the roles it filled (skipped when it filled none), `extract`, `score`, `judge`, `summarise`. Extract, judge and summarise are switches under Runs; fetch text is on by default. ## The three ways a source is read @@ -40,23 +40,23 @@ The order of the stages is `CASCADES` in `app/jobs/cascade.py`; this section say **Paged.** The list arrives in pages of a fixed size (`READS.page_size`), read one after another `PAGE_DELAY` (0.5 s) apart, at most `MAX_PAGES` (100) pages a pull. The read is complete when the number of distinct roles read equals the total the host states (`complete="stated"`), or, for a host that states none, when the read reaches an empty page (`complete="end"`); a Jobindex search is never complete (`complete="never"`). A board of more than 100 pages is swept: each pull stores what it read and the next continues from the saved cursor, and the read is complete when a sweep reaches the end. Roles closing between pulls shift an offset-paged host's later roles back past the cursor, so a role of a swept board closes only when two finished sweeps in a row missed it. A host with a `ceiling` (Workday 2,000) answers nothing past it: a read that reaches it is incomplete, is not swept and closes nothing, so a filter must bring the board under it. -**List, then each role.** The list, in one request or in pages, carries title, place and mostly a date, but no text (Oracle, Phenom and Jobylon add a teaser). The text is on each role's own page (`READS.text="detail"`), which the pull does not read. The `enrich` stage reads those pages after scoring (`app/jobs/enrich.py`): +**List, then each role.** The list, in one request or in pages, carries title, place and mostly a date, but no text (Oracle, Phenom and Jobylon add a teaser). The text is on each role's own page (`READS.text="detail"`), which the pull does not read. The fetch text stage reads those pages after scoring (`app/jobs/fetch_text.py`): -- It takes open roles without text that an enabled agent with a prompt ranks ungated within its best `enrich.top` (500) at `enrich.min_score` (0.5) or above, best score first. -- It reads at most `enrich.per_board` (200) roles per board and `enrich.per_run` (1,000) per run, roles never tried before roles whose read failed, `PAGE_DELAY` apart, one batch per board on the `http` pool. -- A read that gave no text is tried again after `enrich.retry_days` (7). A 404 or 410 counts as read; another failure is retried on a later run; a 429 ends the reads of that board for the run. -- It runs after every pull, after a saved agent and after a star change, not after a vote, and it can be switched off under Runs. +- It takes open roles without text in the cut of an enabled agent (the best by fit and by CV closeness, and the liked and saved, see `app/jobs/score.py`), best final score first. +- It reads at most `fetch_text.per_board` (200) roles per board and `fetch_text.per_run` (1,000) per run, roles never tried before roles whose read failed, `PAGE_DELAY` apart, one batch per board on the `http` pool. +- A read that gave no text is tried again after `fetch_text.retry_days` (7). A 404 or 410 counts as read; another failure is retried on a later run; a 429 ends the reads of that board for the run. +- It runs after every pull, after a saved agent, an affinity change or an override switched, not after a vote, and it can be switched off under Runs. -A role below the bar for every agent is never read, so it stays ranked on its teaser or its title. A board of 800 roles does not get all its text; the roles that rank do. Four consequences follow from the code: +A role in no agent's cut is never read, so it stays ranked on its teaser or its title. A board of 800 roles does not get all its text; the roles in the cut do. Four consequences follow from the code: -- `embed` encodes a role once, from its description, else its teaser, else its title. When better text arrives its vector is dropped and the `prep`, `embed` and `score` that follow `enrich` encode it again. +- `embed` encodes a role once, from its description, else its teaser, else its title. When better text arrives its vector is dropped and the `prep`, `embed` and `score` that follow fetch text encode it again. - A role page may fill only `_runner.DETAIL_FIELDS`: description, location and places, teaser, remote, country, employment, pay figures and date. Title, link, department and the pay line come from the list, which states them on every pull. A role whose page was read is sent without the list's place and remote on later pulls, so what its page said stays; a place the host changes after that is not picked up. - Completeness, and so closing, is decided by the list alone. A board of 800 roles closes its roles from the first pull. - SmartRecruiters and Workable list their roles in pages and belong here too, since their list has no text. ## Overview -Numbers are from the live database on 2026-09-30, after the pull of 12:52. "Open roles" counts every open role of the host's boards, copies included; the feed's 20,578 leaves copies out, the 24,003 below do not. Since the agents' reach filters big boards at the source (`app/jobs/reach.py`), a host's count is what is in reach, not what its boards hold: SmartRecruiters read 7,238 roles before the filter and 343 after. "Description" is where the text comes from; for the "list, then each role" hosts it is filled only for roles `enrich` has read. +Numbers are from the live database on 2026-09-30, after the pull of 12:52. "Open roles" counts every open role of the host's boards, copies included; the feed's 20,578 leaves copies out, the 24,003 below do not. Since the agents' reach filters big boards at the source (`app/jobs/reach.py`), a host's count is what is in reach, not what its boards hold: SmartRecruiters read 7,238 roles before the filter and 343 after. "Description" is where the text comes from; for the "list, then each role" hosts it is filled only for roles fetch text has read. | Source | Boards | Open roles | How it is read | Roles per request | Description | Pay | Limit that matters most | |---|---|---|---|---|---|---|---| @@ -95,20 +95,20 @@ Numbers are from the live database on 2026-09-30, after the pull of 12:52. "Open | Limit | Value | What it means | |---|---|---| -| Schedule | `pull` every 6 h by default, editable under Runs | Each pull starts the cascade in "Flow". Extract and summarise are off in the config and switched under Runs (both are on in the live database); enrich is on by default. | +| Schedule | `pull` every 6 h by default, editable under Runs | Each pull starts the cascade in "Flow". Extract and summarise are off in the config and switched under Runs (both are on in the live database); fetch text is on by default. | | Batches | 10 boards or searches per batch, 8 batches at once (`workers.http: 8`) | Within a batch boards are read one after another. Enrich reads one batch per board on the same pool. One pull runs at a time. | | Where requests go | https on port 443, only to the host names the host module allows; no IP address, no local name; at most 3 redirects, each checked | A slug cannot point a request anywhere else. | | Size cap | 96 MiB per response | A larger answer fails the board. Greenhouse retries once without text and pay; no other host has a fallback. | | Time limits | 10 s to connect, 30 s between bytes, 60 s for the whole request | The same for boards, role pages and searches. A slow board fails for this pull and is asked again next pull. | | Pages per board | 100 a pull | A board above it is swept across pulls: each pull stores what it read and the next continues from the cursor. A role of a swept board closes only when two finished sweeps in a row missed it, so a new role on a later page waits up to one sweep. A read that reaches the host's `ceiling` (Workday 2,000) is not swept and closes nothing; a filter must bring the board under it. | | Wait | 0.5 s before every page after the first, for boards and searches alike, and before every role page | The list of a Workday board of 1,000 roles spends about 25 s waiting. | -| Per-role reads | `enrich`, after scoring: 200 per board and 1,000 per run, ranked roles first | See "List, then each role". Roles the agents do not rank are not read. | +| Per-role reads | `fetch_text`, after scoring: 200 per board and 1,000 per run, the roles in the agents' cut | See "List, then each role". Roles in no cut are not read. | | Closing a role | after a complete read, every open role of the board it did not list | An incomplete read closes nothing, and so does a read that asked nothing. A role listed again reopens. A board switched off closes its roles at once. A filtered read keeps roles with a vote or an application, and remote roles when the host could not be asked for remote; a read by fetch terms closes only roles not listed for 21 days. | | Search roles | a complete search closes what it did not list; otherwise a role closes when no search has listed it for 21 days | A search result is never a company's whole list. A Jobindex search is never complete, so its roles close by the 21 days only. A search failing for 21 days closes its roles. | | A board that fails | first failure: logged, roles stay open, the Companies list shows "not answering since" | Any error counts: refused host, timeout, HTTP error, parse error, oversize, a row that would not store, and a read that lists under 10% of what the same read listed last time (`SUSPECT_SHARE`) once that was at least `SUSPECT_MIN`, 10. A search that fails is recorded the same way and its row says so. | | A board that fails for 21 days | all its open roles close at the next pull, and a suspect answer that has stood that long is accepted | The next answer reopens what it lists. `STALE_DAYS` is the one 21. | | robots.txt | not read at run time | Decided per reader when it is built and recorded in `READS.robots` with the date (`docs/decisions.md`). `Crawl-delay` is not honoured. | -| 429 | first page: the board fails; a later page: the read stops, keeps what it read and is incomplete; a role page: the reads of that board stop until the next enrich run | Nothing is closed on a list cut short by a 429. | +| 429 | first page: the board fails; a later page: the read stops, keeps what it read and is incomplete; a role page: the reads of that board stop until the next fetch text run | Nothing is closed on a list cut short by a 429. | How far a board is read: a pull asks each board of 300 roles or more for the reach of the search agents that are switched on (`app/jobs/reach.py`): the union of their `any` gates on country, town (as its country) and remote, or every role when one agent can take a role from anywhere. A company set to All countries is asked for the agents' wide reach, the union of their `require` reach, Europe today; a host that asks one country a request reads such a board whole instead of once per country (33 codes today), unless fetch terms narrow it. A board's own reach, set on the Companies list, wins. A complete filtered read closes the roles outside the reach, except those with a vote or an application. A board's size is set once, when it is added, on Recheck or by `app companies size`, and a pull does not rewrite it; a board with no size yet is sized first in its batch and then read as its size says, and a board whose reader cannot narrow the reach is read whole and sized by that read. A host that cannot filter a dimension ignores it: SmartRecruiters, Phenom, SuccessFactors and Eightfold have no remote filter, so their reach asks for countries only. @@ -313,7 +313,7 @@ What no source does: ## Hosts read as list, then each role -For every host below the pull reads the list only; the description arrives when `enrich` reads the role's page, so "Requests per pull" counts list requests. "Places" are a list of the places the host states; only the `location` line joins them. +For every host below the pull reads the list only; the description arrives when fetch text reads the role's page, so "Requests per pull" counts list requests. "Places" are a list of the places the host states; only the `location` line joins them. ### SmartRecruiters @@ -323,7 +323,7 @@ For every host below the pull reads the list only; the description arrives when | Companies | 11 boards: Bosch (about 4,800 roles on the site, 13 open in reach), Delivery Hero, Wise | | List | `api.smartrecruiters.com/v1/companies//postings`, 100 per request by offset; states `totalFound` | | Requests per pull | one series per country of the reach (two for Bosch: DK and SE), each roles / 100, rounded up; a board read whole is roles / 100 | -| Description | per role, `GET /v1/companies//postings/`: the `jobAd.sections` `jobDescription`, `qualifications` and `additionalInformation`, joined; `companyDescription` is the same blurb on every role and is left out. Read by `enrich` for ranked roles only: 1 of 343 open roles has text | +| Description | per role, `GET /v1/companies//postings/`: the `jobAd.sections` `jobDescription`, `qualifications` and `additionalInformation`, joined; `companyDescription` is the same blurb on every role and is left out. Read by fetch text for the roles in the cut only: 1 of 343 open roles has text | | Date | `releasedDate`; trusted | | Place and country | `fullLocation`, else city and country, with empty parts removed; the host's `country` is handed on | | Remote | `location.remote`, true or false, as the host's word | @@ -333,7 +333,7 @@ For every host below the pull reads the list only; the description arrives when | Filter at the source | `?country=dk`, one country a request; remote is ignored by the API, so a remote role outside the reach is not read. Bosch DK: 2 of 4,830 (2026-09-30). Which countries is the agents' reach, see above. Fetch terms as `?q=engineer`, one a request, AND with the country; it matches title and text (Bosch: 1,713 of 4,830) | | Limitations | 100 pages a pull; a larger board is swept across pulls. Whether the identifier is case-sensitive is unsettled: the module docstring says it is, `docs/decisions.md` says it is not (`docs/open.md`). An unknown identifier answers an empty list, not an error: `READS.empty="suspect"` keeps a known board from closing its roles on it, but a mistyped slug attached as a new board reads as an empty, complete board. | -The reader's "Fetch the posting" button shows for a role with no `article_text` and a description under 500 characters, so for these roles until `enrich` has read their page. It reads one posting page on demand into `article_text`, which `summarise` reads and `embed` and `extract` do not. Whether it reads a SmartRecruiters page cleanly was not checked. +The reader's "Fetch the posting" button shows for a role with no `article_text` and a description under 500 characters, so for these roles until fetch text has read their page. It reads one posting page on demand into `article_text`, which `summarise` reads and `embed` and `extract` do not. Whether it reads a SmartRecruiters page cleanly was not checked. ### Workable @@ -343,7 +343,7 @@ The reader's "Fetch the posting" button shows for a role with no `article_text` | Companies | 8 boards: Hugging Face, Kahoot!, Starling Bank | | List | a POST to `apply.workable.com/api/v3/accounts//jobs`, 10 per request with a `nextPage` token; states `total` | | Requests per pull | roles / 10, rounded up, 0.5 s apart; the largest board has 50 roles, so 5 | -| Description | per role, `GET /api/v2/accounts//jobs/`: `description`, `requirements` and `benefits`, joined. The row keeps the `shortcode`, since `source_id` is the numeric id. Read by `enrich` for ranked roles only: 1 of 157 open roles has text | +| Description | per role, `GET /api/v2/accounts//jobs/`: `description`, `requirements` and `benefits`, joined. The row keeps the `shortcode`, since `source_id` is the numeric id. Read by fetch text for the roles in the cut only: 1 of 157 open roles has text | | Date | `published`; trusted | | Place and country | every `locations` entry as "city, region, country", each remote when the role is; `location` when there are none | | Remote | `workplace` (remote, hybrid, on_site) as the host's word, else the `remote` flag | @@ -592,7 +592,7 @@ A saved search is a query with its parameters, stored as a `search` subscription | Saved search | `q` and optionally `region` (an `amt` id from the site's own search form: `REGIONS` in the module, 2 is Storkøbenhavn) and `remote` (`helt` fully remote, `delvist` partly) | | List | the RSS feed `jobbank.dk/job/rss/`, 100 items a page; an empty page ends it. It states no total and is not sorted by date | | Pages read | at most the module's `ceiling`, which keeps a run under about 20 pages | -| Text | one request a role: the JSON-LD `JobPosting` on `/job//...`, read by `enrich` for the roles an agent ranks high. The listing carries none | +| Text | one request a role: the JSON-LD `JobPosting` on `/job//...`, read by fetch text for the roles in the cut. The listing carries none | | Date | `datePosted` from the role's page; trusted | | Place | the region in the item's description line, and `jobLocation.addressLocality` from the page; country Denmark | | Remote | not stated by a row; the search's `remote` filter narrows the list | diff --git a/tests/test_cv_feed.py b/tests/test_cv_feed.py index 090f03f..a2dcb24 100644 --- a/tests/test_cv_feed.py +++ b/tests/test_cv_feed.py @@ -210,7 +210,7 @@ def test_the_role_carries_its_judgement_and_languages( def test_an_agent_without_languages_flags_nothing(client: TestClient, store: Store) -> None: - owner, uid = setup(store) + owner, _uid = setup(store) store.save_agent("Data", prompts=owner.prompts, agent_id=owner.id, cv_languages=[]) rows = client.get("/api/roles", params={"agent": owner.id}).json()["items"] assert {r["lang_flag"] for r in rows} == {""} diff --git a/tests/test_score.py b/tests/test_score.py index a06a686..b57778f 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -23,7 +23,7 @@ from pipeline import FINAL, JOBS, SCHEDULES, TASKS, JobQueue, Jobs, RunContext, from plugins.ats import ATSSource # noqa: E402 from app.config import CALIBRATION, AgentSeed, Config, ConfigError # noqa: E402 from app.jobs import cascade, embed, score # noqa: E402 -from app.models import Candidate, Gate, Limits, Prompt, Weights # noqa: E402 +from app.models import Application, Candidate, Gate, Limits, Prompt, Weights # noqa: E402 from app.storage import Store # noqa: E402 from prepped import upsert # noqa: E402 @@ -991,6 +991,75 @@ def test_without_an_embedder_the_prompts_are_skipped_and_fit_is_out( assert store.scores(agent.id) == {uid: (0.0, "no prompt vector")} +# --------------------------------------------------------------------- cut + +def test_the_affinity_bonus_is_the_share_it_has_in_the_final_score() -> None: + w = Weights() + assert score.bonus(0, w) == 0 + assert score.bonus(1, w) == pytest.approx(0.2 / 3 / 1.2) # about 0.056 + assert score.bonus(2, w) == pytest.approx(0.4 / 3 / 1.2) # about 0.111 + assert score.bonus(2, Weights(fit=0, recency=0, affinity=0, taste=0)) == 0 + + +def _cands(*rows: tuple[str, str, int]) -> list[Candidate]: + """`(uid, posted_at day, affinity)` as candidates.""" + return [Candidate(uid=uid, posted_at=f"2026-09-{day}T00:00:00+00:00", affinity=affinity) + for uid, day, affinity in rows] + + +def test_the_cut_is_the_best_by_fit_then_by_cv_then_the_kept() -> None: + cands = _cands(("a", "10", 0), ("b", "11", 0), ("c", "12", 0), ("d", "13", 0), + ("e", "14", 0), ("f", "15", 0), ("g", "16", 0)) + fits = {"a": 0.9, "b": 0.8, "c": 0.7, "d": 0.65, "e": 0.3, "f": 0.2} + near = {"a": 0.99, "b": 0.1, "c": 0.1, "d": 0.75, "e": 0.8, "f": 0.5} + passed = {"a", "b", "c", "d", "e", "f"} + limits = Limits(cut_fit_top=2, cut_fit_min=0.6, cut_cv_top=2, cut_cv_min=0.6) + cut = score.cut_of(cands, passed, fits, near, {"f", "g", "b"}, Weights(), limits) + # a and b by fit; e and d the closest to the CV that those leave out (a, + # the closest of all, is in already); f kept; g kept but gated, so out. + assert cut == {"a": "fit", "b": "fit", "e": "cv", "d": "cv", "f": "kept"} + + +def test_the_minimum_stops_a_list_before_its_count() -> None: + cands = _cands(("a", "10", 0), ("b", "11", 0)) + limits = Limits(cut_fit_top=500, cut_fit_min=0.6, cut_cv_top=150, cut_cv_min=0.6) + cut = score.cut_of(cands, {"a", "b"}, {"a": 0.61, "b": 0.59}, {"b": 0.59}, set(), + Weights(), limits) + assert cut == {"a": "fit"} + + +def test_affinity_lifts_a_level_over_the_minimum_but_is_no_pass() -> None: + cands = _cands(("liked", "10", 1), ("fave", "10", 2), ("plain", "10", 0), + ("weak", "10", 2)) + fits = {"liked": 0.56, "fave": 0.5, "plain": 0.59, "weak": 0.1} + cut = score.cut_of(cands, {c.uid for c in cands}, fits, {}, set(), Weights(), + Limits(cut_fit_min=0.6)) + assert cut == {"liked": "fit", "fave": "fit"} + + +def test_a_tie_goes_to_the_newer_role_then_the_lower_uid() -> None: + cands = _cands(("b", "10", 0), ("a", "10", 0), ("old", "09", 0), ("new", "11", 0)) + fits = dict.fromkeys(("a", "b", "old", "new"), 0.8) + cut = score.cut_of(cands, {c.uid for c in cands}, fits, {}, set(), Weights(), + Limits(cut_fit_top=3, cut_fit_min=0)) + assert set(cut) == {"new", "a", "b"} + + +def test_a_like_and_a_save_put_a_role_in_the_cut_on_the_next_score( + store: Store, config: Config, fake: FakeEmbedder) -> None: + liked = role(store, "1", "Sales Manager", body="sales") + saved = role(store, "2", "Office Manager", body="office") + other = role(store, "3", "Barista", body="coffee") + agent = store.save_agent("Backend", "python backend", + limits=Limits(cut_fit_min=1, cut_cv_min=1)) + store.set_vote(agent.id, liked, 1) + store.save_application(Application(role_uid=saved, role_title="Office Manager")) + run(config, store, embed.run_embed) + run(config, store, score.run_score) + assert store.cut(agent.id) == {liked: "kept", saved: "kept"} + assert other not in store.cut(agent.id) + + def test_a_disabled_agent_is_not_scored( store: Store, config: Config, fake: FakeEmbedder) -> None: role(store, "1") diff --git a/tests/test_storage.py b/tests/test_storage.py index 29037f4..48d6310 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -2,6 +2,7 @@ from __future__ import annotations +import json import sqlite3 from collections.abc import Iterator from datetime import UTC, datetime, timedelta @@ -1778,3 +1779,116 @@ def test_summarise_waits_on_the_top_of_each_enabled_agent_by_its_own_limits( conn.execute("UPDATE agents SET limits = '{}'") assert {r.uid for r in store.summarise_waiting(10)} == {a, b, c} assert store.agents()[0].limits == Limits() + + +# ------------------------------------------------------------- migration 32 + +def _v31(path: Path) -> None: + """Take a file `Store` made back to v31: no cut columns, no saved + filters.""" + with sqlite3.connect(path) as conn: + conn.execute("DROP INDEX idx_scores_cut") + conn.execute("ALTER TABLE scores DROP COLUMN cut") + conn.execute("ALTER TABLE scores DROP COLUMN fit") + conn.execute("DROP TABLE saved_filters") + conn.execute("PRAGMA user_version = 31") + + +def test_v32_resets_the_limits_drops_prefer_gates_and_renames_enrich(tmp_path: Path) -> None: + """An agent stored before the cut, with the old limits keys, a prefer + gate and a `weight` on every gate, loads after migrating; the switch and + the queued jobs of `enrich` follow the rename.""" + from pipeline import Jobs + + path = tmp_path / "v31.db" + store = Store(path) + upsert(store, posting("a")) + Jobs(path) + old_gates = [{"field": "country", "values": ["DK"], "mode": "require", "weight": 1.0}, + {"field": "remote", "values": ["hybrid", "remote"], "mode": "prefer", + "weight": 0.05}, + {"field": "title", "values": ["account executive"], "mode": "exclude", + "weight": 1.0}] + old_limits = {"extract_top": 500, "extract_min": 0.55, "summarise_top": 30, + "summarise_min": 0.65, "judge_top": 150, "judge_min": 0.55, + "judge_cv_top": 50} + _v31(path) + with sqlite3.connect(path) as conn: + conn.execute("INSERT INTO agents (name, gates, limits, created_at) VALUES (?,?,?,?)", + ("Wolf", json.dumps(old_gates), json.dumps(old_limits), "2026-10-01")) + conn.execute("INSERT INTO agents (name, created_at) VALUES ('Bare', '2026-10-01')") + conn.execute("INSERT INTO state (key, value) VALUES ('stage:enrich', 'off')") + for kind, state in (("enrich", "queued"), ("enrich_batch", "queued"), + ("enrich", "done"), ("score", "queued")): + conn.execute("INSERT INTO jobs (kind, state, created_at) VALUES (?,?,?)", + (kind, state, "2026-10-01")) + conn.execute("INSERT INTO schedules (name, kind, cadence, created_at) " + "VALUES ('texts', 'enrich', 'every 1d', '2026-10-01')") + + migrated = Store(path) + + assert migrated.migrations_applied == [32] + by_name = {a.name: a for a in migrated.agents()} + assert by_name["Wolf"].limits == Limits() == by_name["Bare"].limits + assert [(g.field, g.mode) for g in by_name["Wolf"].gates] == [ + ("country", "require"), ("title", "exclude")] + assert migrated.state["stage:fetch_text"] == "off" and "stage:enrich" not in migrated.state + with sqlite3.connect(path) as conn: + assert conn.execute("SELECT kind, state FROM jobs ORDER BY id").fetchall() == [ + ("enrich", "done"), ("score", "queued")] + assert conn.execute("SELECT kind FROM schedules WHERE name = 'texts'").fetchone() == ( + "fetch_text",) + stored = json.loads(conn.execute( + "SELECT gates FROM agents WHERE name = 'Wolf'").fetchone()[0]) + assert all("weight" not in g for g in stored) + agent = by_name["Wolf"] + migrated.replace_scores(agent.id, [(posting("a").uid, 0.5, "", "", 0.7, 0.6, "fit")]) + assert migrated.cut(agent.id) == {posting("a").uid: "fit"} + assert migrated.save_filter(agent.id, "Remote", "remote=remote").name == "Remote" + + +def test_v32_runs_twice_and_keeps_limits_set_since(tmp_path: Path) -> None: + path = tmp_path / "v31.db" + store = Store(path) + agent = store.save_agent("Wolf", limits=Limits(cut_fit_top=900)) + _v31(path) + assert Store(path).migrations_applied == [32] + assert Store(path).agent(agent.id).limits == Limits() # type: ignore[union-attr] + narrow = Limits(cut_fit_top=900, summarise_top=10) + store.save_agent("Wolf", agent_id=agent.id, limits=narrow) + with sqlite3.connect(path) as conn: + conn.execute("PRAGMA user_version = 31") # the columns are there already + again = Store(path) + assert again.migrations_applied == [32] + assert again.agent(agent.id).limits == narrow # type: ignore[union-attr] + + +def test_a_fresh_file_has_the_cut_columns_and_saved_filters(store: Store) -> None: + with store.transaction() as conn: + columns = {r[1] for r in conn.execute("PRAGMA table_info(scores)")} + tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master")} + assert {"fit", "cut", "cv_fit"} <= columns and "saved_filters" in tables + + +# ------------------------------------------------------------ saved filters + +def test_saved_filters_are_per_agent_named_once_regardless_of_case(store: Store) -> None: + one, two = store.save_agent("One"), store.save_agent("Two") + remote = store.save_filter(one.id, " Remote only ", "remote=remote&order=newest") + store.save_filter(one.id, "berlin", "places=place:berlin") + store.save_filter(two.id, "Remote only", "remote=remote") + + assert (remote.name, remote.query) == ("Remote only", "remote=remote&order=newest") + assert [f.name for f in store.saved_filters(one.id)] == ["berlin", "Remote only"] + with pytest.raises(ValueError, match="already saved"): + store.save_filter(one.id, "REMOTE ONLY", "x=1") + with pytest.raises(ValueError, match="needs a name"): + store.save_filter(one.id, " ", "x=1") + with pytest.raises(KeyError): + store.save_filter(999, "Nobody", "x=1") + + assert store.delete_filter(remote.id) and not store.delete_filter(remote.id) + assert [f.name for f in store.saved_filters(one.id)] == ["berlin"] + store.delete_agent(one.id) + assert store.saved_filters(one.id) == [] + assert [f.name for f in store.saved_filters(two.id)] == ["Remote only"] diff --git a/tests/test_web.py b/tests/test_web.py index 8cbdd6c..467978a 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1874,3 +1874,42 @@ def test_saving_queues_no_summary_when_the_stage_is_off_or_it_has_one( store.upsert_role(posting("3")) client.post("/api/applications", json={"role_uid": posting("3").uid}) assert _summary_jobs(queue) == [] + + +# ------------------------------------------------------------ saved filters + +def test_saved_filters_are_listed_saved_and_deleted_per_agent( + client: TestClient, store: Store) -> None: + one, two = store.save_agent("One"), store.save_agent("Two") + url = f"/api/agents/{one.id}/filters" + + made = client.post(url, json={"name": "Remote", "query": "remote=remote&order=newest"}) + assert made.status_code == 201 + row = made.json() + assert set(row) == {"id", "name", "query", "created_at"} + assert (row["name"], row["query"]) == ("Remote", "remote=remote&order=newest") + client.post(url, json={"name": "berlin", "query": "places=place:berlin"}) + assert [f["name"] for f in client.get(url).json()] == ["berlin", "Remote"] + assert client.get(f"/api/agents/{two.id}/filters").json() == [] + + assert client.post(url, json={"name": "remote", "query": "x=1"}).status_code == 409 + assert client.post(url, json={"name": " ", "query": "x=1"}).status_code == 422 + assert client.post("/api/agents/999/filters", json={"name": "A"}).status_code == 404 + assert client.get("/api/agents/999/filters").status_code == 404 + + assert client.delete(f"/api/filters/{row['id']}").status_code == 204 + assert client.delete(f"/api/filters/{row['id']}").status_code == 404 + assert [f["name"] for f in client.get(url).json()] == ["berlin"] + + +def test_an_override_queues_a_rescore_and_fetch_text_either_way( + client: TestClient, store: Store, queue: Any) -> None: + """All countries and All roles change the agents' fetch gates for the + roles already stored, so either switch, on or off, rescores.""" + states = ("queued", "running", "done", "failed", "cancelled") + store.add_company("Acme") + assert client.post("/api/companies/acme/all-roles", json={"on": False}).status_code == 200 + found = queue.jobs.last("fetch_text", states=states) + assert found is not None + assert [queue.jobs.get(i).kind for i in found.depends_on] == ["score"] + assert queue.jobs.last("pull", states=states) is None