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 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-10-04 15:11:43 +02:00
1 parent 060ccf66e6
commit 617356e60c
35 files changed
+1054 -751

No files matched your search

+3 -3
View File
@@ -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")
+14 -21
View File
@@ -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`
+51 -47
View File
@@ -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:<id>`), 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:<id>`), 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 <name>` 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")
+6 -7
View File
@@ -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
+36 -30
View File
@@ -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:<target>`.
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:<target>`. 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:<target>`,
`ats:detailfailed:<target>` and `ats:detailempty:<target>`) 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)
+11 -11
View File
@@ -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 `"<agent_id>:<uid>"`,
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 `"<agent_id>:<uid>"`,
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:
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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.
"""
+132 -67
View File
@@ -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)}
+13 -12
View File
@@ -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
+7 -6
View File
@@ -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
+48 -40
View File
@@ -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] = []
+101 -4
View File
@@ -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
+167 -125
View File
@@ -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
#: `<name>_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"]))
+75 -23
View File
@@ -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)