Score each role's closeness to the agent's CV, let the judge also read the 50 closest the score leaves out, and order CV match's unjudged roles by it
The CV is cut into chunks inside e5's window, embedded like a prompt and cached per agent under a hash of the model, chunking and text. A role's cv_fit (scores.cv_fit, migration 31) is its best chunk cosine, scaled with its own calibration band: fit's clipped 88 of agent 2's ungated roles to 1. It is no term of the Agent match score. judge_cv_top (default 50) sits beside judge_top and judge_min on the agents page and card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b481cb5692
commit
c23594507a
23 files changed
+603
-85
No files matched your search
@@ -387,6 +387,8 @@ CREATE TABLE IF NOT EXISTS role_votes (
|
||||
|
||||
-- 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.
|
||||
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,
|
||||
@@ -394,9 +396,21 @@ CREATE TABLE IF NOT EXISTS scores (
|
||||
gated_reason TEXT NOT NULL DEFAULT '',
|
||||
scored_at TEXT NOT NULL,
|
||||
prompt TEXT NOT NULL DEFAULT '',
|
||||
cv_fit REAL,
|
||||
PRIMARY KEY (uid, agent_id)
|
||||
);
|
||||
|
||||
-- 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.
|
||||
CREATE TABLE IF NOT EXISTS agent_cv_vectors (
|
||||
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL,
|
||||
vec_key TEXT NOT NULL,
|
||||
vec BLOB NOT NULL,
|
||||
PRIMARY KEY (agent_id, position)
|
||||
);
|
||||
|
||||
-- One agent's judge reading of a role against its CV. Its own table, since
|
||||
-- replace_scores rewrites scores every run and a judgement outlives that
|
||||
-- and the role closing. skills, experience and career are 0 to 100,
|
||||
@@ -713,6 +727,10 @@ UPDATE roles SET seniority = COALESCE(NULLIF(seniority_model, ''),
|
||||
# reads every role again (judge.FORMAT).
|
||||
(30, "agents.like_text: what the person likes, beside what they do not like",
|
||||
lambda conn: _add_like_text(conn)),
|
||||
# A Python step: it adds the column and the table only where they are
|
||||
# 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)),
|
||||
)
|
||||
|
||||
#: What a file created from `SCHEMA` today is already at.
|
||||
@@ -1061,6 +1079,21 @@ def _add_like_text(conn: sqlite3.Connection) -> None:
|
||||
conn.execute("ALTER TABLE agents ADD COLUMN like_text TEXT NOT NULL DEFAULT ''")
|
||||
|
||||
|
||||
def _add_cv_fit(conn: sqlite3.Connection) -> None:
|
||||
"""Migration 31's step: `scores.cv_fit`, NULL on every row until the
|
||||
next score, and `agent_cv_vectors`, empty until then. Runs twice without
|
||||
harm."""
|
||||
columns = {r[1] for r in conn.execute("PRAGMA table_info(scores)")}
|
||||
if "cv_fit" not in columns:
|
||||
conn.execute("ALTER TABLE scores ADD COLUMN cv_fit REAL")
|
||||
conn.execute("""CREATE TABLE IF NOT EXISTS agent_cv_vectors (
|
||||
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
|
||||
position INTEGER NOT NULL,
|
||||
vec_key TEXT NOT NULL,
|
||||
vec BLOB NOT NULL,
|
||||
PRIMARY KEY (agent_id, position))""")
|
||||
|
||||
|
||||
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
|
||||
|
||||
+67
-27
@@ -146,8 +146,10 @@ def _cv_sql(weights: Weights | None) -> str:
|
||||
|
||||
def _by_cv(expression: str) -> str:
|
||||
"""The order of CV match: the judged roles by `cv_score` (its SQL
|
||||
`expression`), then the rest by score, as `roles` orders them."""
|
||||
return f"{expression} IS NULL, {expression} DESC, "
|
||||
`expression`), then the rest by how close they are to the CV
|
||||
(`scores.cv_fit`), then by score, as `roles` orders them."""
|
||||
return (f"{expression} IS NULL, {expression} DESC, "
|
||||
"s.cv_fit IS NULL, s.cv_fit DESC, ")
|
||||
|
||||
#: How many days after today the feed's Closing soon filter reaches.
|
||||
CLOSING_DAYS = 4
|
||||
@@ -900,15 +902,15 @@ class Store:
|
||||
|
||||
def role(self, uid: str, agent_id: int | None = None) -> ScoredRole | None:
|
||||
"""One role with its tags, and with `vote`, `voted_at`, `score`,
|
||||
`gated_reason` and `prompt` as `agent_id` judged and scored it, and
|
||||
its `judgement`, `cv_score` (under the agent's weights as they are
|
||||
now) and language flag for that agent; None
|
||||
`gated_reason`, `prompt` and `cv_fit` as `agent_id` judged and scored
|
||||
it, and its `judgement`, `cv_score` (under the agent's weights as they
|
||||
are now) and language flag for that agent; None
|
||||
(unjudged, unscored) without an agent. The score is moved by the vote
|
||||
as `roles` serves it."""
|
||||
row = self._row(
|
||||
f"SELECT roles.*, v.vote, v.voted_at, {_VOTED_SCORE} AS score, "
|
||||
"COALESCE(s.gated_reason, '') AS gated_reason, COALESCE(s.prompt, '') AS prompt "
|
||||
f"FROM roles {_WITH_VOTES} "
|
||||
"COALESCE(s.gated_reason, '') AS gated_reason, COALESCE(s.prompt, '') AS prompt, "
|
||||
f"s.cv_fit FROM roles {_WITH_VOTES} "
|
||||
"LEFT JOIN scores s ON s.uid = roles.uid AND s.agent_id = ? WHERE roles.uid=?",
|
||||
(agent_id, agent_id, uid))
|
||||
if row is None:
|
||||
@@ -1143,7 +1145,8 @@ class Store:
|
||||
no score.
|
||||
|
||||
With `by_cv` as well, the roles the agent's `judge` read come first
|
||||
by `cv_score`, then the others by score (CV match). Every row
|
||||
by `cv_score`, then the others by how close they are to the CV
|
||||
(`cv_fit`), then by score (CV match). Every row
|
||||
carries the agent's `cv_score` (None when not judged), computed
|
||||
from the three numbers under the agent's weights at read time, and the
|
||||
language flag of its languages against the agent's
|
||||
@@ -1167,7 +1170,7 @@ class Store:
|
||||
order = "roles.posted_at DESC, roles.uid"
|
||||
scored = agent is not None and by_score
|
||||
if scored:
|
||||
select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt"
|
||||
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:
|
||||
# Newest first, open only: idx_roles_posted covers this exactly
|
||||
@@ -1221,7 +1224,8 @@ class Store:
|
||||
by uid, see `role`.
|
||||
|
||||
`lang_flag` and `lang_note` are the role's languages against the
|
||||
agent's (`language_flag`), and `cv_score` its `judge` total.
|
||||
agent's (`language_flag`), `cv_score` its `judge` total and `cv_fit`
|
||||
how close it is to the agent's CV.
|
||||
|
||||
`excerpt` is `app.models.excerpt` over a `SUBSTR` of `_EXCERPT_SOURCE`
|
||||
(`_EXCERPT_RAW` characters, trimmed further in Python), so the source
|
||||
@@ -1244,7 +1248,7 @@ class Store:
|
||||
order = "roles.posted_at DESC, roles.uid"
|
||||
scored = agent is not None and by_score
|
||||
if scored:
|
||||
select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt"
|
||||
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:
|
||||
# Same reasoning as `roles`: the planner otherwise sorts every
|
||||
@@ -1259,6 +1263,8 @@ class Store:
|
||||
row["excerpt"] = excerpt(row["excerpt"])
|
||||
if row["cv_score"] is not None:
|
||||
row["cv_score"] = round(row["cv_score"], 1)
|
||||
if row.get("cv_fit") is not None:
|
||||
row["cv_fit"] = round(row["cv_fit"], 3)
|
||||
row["lang_flag"], row["lang_note"] = language_flag(
|
||||
role_languages(row.pop("languages_required")), mine)
|
||||
found.append(RoleRow.from_row(row))
|
||||
@@ -1871,8 +1877,10 @@ class Store:
|
||||
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, each with some text
|
||||
(`article_text` or `description`). Given `uids`, those of them with
|
||||
(`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
|
||||
@@ -1887,16 +1895,23 @@ 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"""{select} (SELECT s.uid, s.score 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 = ''
|
||||
ORDER BY s.score DESC, top.posted_at DESC, s.uid LIMIT ?) best
|
||||
JOIN roles r ON r.uid = best.uid {joined}
|
||||
WHERE best.score >= ? AND {text}
|
||||
ORDER BY best.score DESC, r.posted_at DESC, r.uid""",
|
||||
(agent.id, agent.limits.judge_top, agent.id, agent.limits.judge_min))
|
||||
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))
|
||||
|
||||
def set_judgement(self, agent_id: int, uid: str, input_key: str, skills: int,
|
||||
experience: int, career: int, strengths: Sequence[str],
|
||||
@@ -2189,6 +2204,27 @@ class Store:
|
||||
self._write("UPDATE agent_prompts SET vec_key=?, vec=? WHERE agent_id=? AND name=?",
|
||||
(key, pack(vec), agent_id, name))
|
||||
|
||||
def cv_vectors(self, agent_id: int, key: str) -> list[np.ndarray] | None:
|
||||
"""The cached vectors of the agent's CV, one per chunk in order, if
|
||||
they were made under `key`; None otherwise."""
|
||||
rows = self._rows("SELECT vec_key, vec FROM agent_cv_vectors WHERE agent_id=? "
|
||||
"ORDER BY position", (agent_id,))
|
||||
if not rows or any(r["vec_key"] != key for r in rows):
|
||||
return None
|
||||
return [unpack(r["vec"]) for r in rows]
|
||||
|
||||
def set_cv_vectors(self, agent_id: int, key: str, vectors: Sequence[np.ndarray]) -> None:
|
||||
"""Replace the agent's CV vectors with these, made under `key`. An
|
||||
agent deleted meanwhile gets nothing written, as in `replace_scores`."""
|
||||
with self.transaction() as conn:
|
||||
if conn.execute("SELECT 1 FROM agents WHERE id=?", (agent_id,)).fetchone() is None:
|
||||
return
|
||||
conn.execute("DELETE FROM agent_cv_vectors WHERE agent_id=?", (agent_id,))
|
||||
conn.executemany(
|
||||
"INSERT INTO agent_cv_vectors (agent_id, position, vec_key, vec) "
|
||||
"VALUES (?,?,?,?)",
|
||||
[(agent_id, n, key, pack(vec)) for n, vec in enumerate(vectors)])
|
||||
|
||||
# --------------------------------------------------------------- scores
|
||||
|
||||
def score_candidates(self, agent_id: int, gates: Sequence[Gate]) -> list[Candidate]:
|
||||
@@ -2226,11 +2262,13 @@ class Store:
|
||||
for r in rows]
|
||||
|
||||
def replace_scores(self, agent_id: int,
|
||||
rows: Sequence[tuple[str, float, str] | tuple[str, float, str, str]]
|
||||
rows: Sequence[tuple[str, float, str] | tuple[str, float, str, str]
|
||||
| tuple[str, float, str, str, float | None]]
|
||||
) -> 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), replacing whatever it had: a role closed or muted since the last run
|
||||
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
|
||||
@@ -2241,9 +2279,11 @@ class Store:
|
||||
return 0
|
||||
conn.execute("DELETE FROM scores WHERE agent_id=?", (agent_id,))
|
||||
conn.executemany(
|
||||
"""INSERT INTO scores (uid, agent_id, score, gated_reason, prompt, scored_at)
|
||||
VALUES (?,?,?,?,?,?)""",
|
||||
[(row[0], agent_id, row[1], row[2], row[3] if len(row) > 3 else "", stamp)
|
||||
"""INSERT INTO scores (uid, agent_id, score, gated_reason, prompt, cv_fit,
|
||||
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)
|
||||
for row in rows])
|
||||
return len(rows)
|
||||
|
||||
|
||||
Reference in new issue
Block a user