Give an agent a CV, languages, avoid and must texts, and read a posting's languages with a level and its deadline (migration 28)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-10-03 21:54:16 +02:00
1 parent ed6d641373
commit 4fc474c437
15 files changed
+716 -79

No files matched your search

+66 -26
View File
@@ -24,10 +24,11 @@ from desk.models import Item
from app import dupes, places
from app.models import (
AFFINITY, HAND_REASONS, LEVELS, STATUSES, Agent, Application,
ApplicationEvent, Candidate, Company, Gate, Limits, Prompt, Role, RoleCopy, RolePlace,
RoleRow, ScoredRole,
ApplicationEvent, Candidate, Company, CvLanguage, Gate, Judgement, Limits, Prompt, Role,
RoleCopy, RolePlace, RoleRow, ScoredRole, cv_languages as normal_languages,
language_flag,
Subscription, Weights, check_prompts, company_fold, company_key, country_of, excerpt, place_key,
fetch_terms as normal_terms, host_word, posted_at, posted_host, remote_of, title_key,
fetch_terms as normal_terms, host_word, iso_day, posted_at, posted_host, remote_of, title_key,
)
from app.storage import gazetteer, schema
@@ -46,7 +47,9 @@ _RAW_TEXT = ("place", "currency")
_HOST_WORDS = ("seniority", "employment", "remote", "country")
_KEPT_HOST_WORDS = ("employment_host", "remote_host", "country_host")
#: What prep maps from that a pull rewrites whatever it brings.
_PREP_OVERWRITTEN = ("title", "seniority_host")
#: `deadline_host` is the host's last day to apply (`applicationDeadline` in
#: `raw`, Jobnet and Jobbank), as `YYYY-MM-DD`.
_PREP_OVERWRITTEN = ("title", "seniority_host", "deadline_host")
_RAW_INT = ("salary_min", "salary_max")
#: What `extract` may fill. Only an empty column is written: a board's own
@@ -63,13 +66,16 @@ GIVE_UP_AFTER = 3
#: A role `extract` has yet to read: still open, no copy of another
#: (`rebuild_dupes`), not given up, and either never answered, without a
#: model level, or never asked where a remote hire may live
#: (`remote_where_model` NULL). The last two make `extract` the afterburner
#: for the level and the area: a role read before they were asked for is
#: read again, and only fills what is still empty (`set_extracted`). A reply
#: model level, never asked where a remote hire may live
#: (`remote_where_model` NULL), or never asked for its languages and deadline
#: (`languages_required` NULL). The last three make `extract` the
#: afterburner for the level, the area, the languages and the deadline: a
#: role read before they were asked for is read again, and only fills what
#: is still empty (`set_extracted`). A reply
#: that names no level leaves the role waiting and is charged a try
#: (`extract.apply_batch`), so it ends.
_EXTRACT_WAITING = ("(extracted_at IS NULL OR seniority_model = '' OR remote_where_model IS NULL) "
_EXTRACT_WAITING = ("(extracted_at IS NULL OR seniority_model = '' OR remote_where_model IS NULL "
"OR languages_required IS NULL) "
f"AND closed_at IS NULL AND dup_of IS NULL AND extract_tries < {GIVE_UP_AFTER}")
#: What a vote may be: 1 like, 0 neither way, -1 pass, None unjudged.
@@ -730,8 +736,10 @@ class Store:
(`seniority_stated`, `seniority_title`, `seniority`, `employment`,
`remote`, `country`) are left as prep last wrote them. `prep_due`
is 1 on a new role and grows by one when the title, the company, a
host word or the place texts changed or the role reopens, which
is what the next prep reads.
host word, the host's deadline or the place texts changed or the
role reopens, which is what the next prep reads. `deadline_host` is
`raw["applicationDeadline"]` as `YYYY-MM-DD` (`iso_day`), '' when
the pull states none, and prep composes `deadline` from it.
`posted_at` (see `app.models.posted_at`) is set from this pull's own
`published_at` and `stamp`; on an update the `ON CONFLICT` clause below
@@ -801,6 +809,7 @@ class Store:
# not kept-filled like _RAW_TEXT: a board that stops stating pay
# must be able to clear it, not leave a stale line behind.
row["salary_text"] = str(raw.get("salary_text") or "")
row["deadline_host"] = iso_day(raw.get("applicationDeadline"))
row["prep_due"] = 1
keep_filled = ("company", "place_key", "description", "feed_text", "original_url",
@@ -810,7 +819,7 @@ class Store:
for c in (*_RAW_INT, "subscription_id")]
updates += [f"{c}=excluded.{c}"
for c in ("title", "title_key", "published_at", "raw", "last_seen",
"salary_text", "seniority_host")]
"salary_text", "seniority_host", "deadline_host")]
updates.append("posted_at=MIN(excluded.posted_at, roles.posted_at)")
# Not `keep_filled`'s COALESCE(NULLIF(...)): that would let a fresh
# pull's classifier guess overwrite what extract decided, which is
@@ -1421,6 +1430,7 @@ class Store:
seniority_host, employment_host, remote_host, country_host,
seniority_model, seniority_stated, seniority_title, seniority,
employment, remote, country, remote_model, remote_where_model,
deadline_host, deadline_model, deadline,
json_extract(raw, '$.places') AS raw_places,
(SELECT json_group_array(place_id) FROM (
SELECT place_id FROM role_remote rr WHERE rr.uid = roles.uid
@@ -1446,8 +1456,8 @@ class Store:
def write_prep(self, mapped: Sequence[dict[str, Any]]) -> int:
"""Write what prep mapped, one dict per role with `uid`, `due` (the
`prep_due` it read), `company_key`, `seniority_stated`,
`seniority_title`, `employment`, `remote`, `remote_area` and
`country`. `seniority` is the model's level, else the stated, else
`seniority_title`, `employment`, `remote`, `remote_area`, `country`
and `deadline` (`YYYY-MM-DD`, None for none, written as it is). `seniority` is the model's level, else the stated, else
the title's. `remote` is written as prep composed it (the model's,
else the host's word, else the place line) and `remote_area`, place
ids, replaces the role's `role_remote`. An empty `country` leaves the
@@ -1464,6 +1474,7 @@ class Store:
employment = :employment,
remote = :remote,
country = COALESCE(NULLIF(:country, ''), country),
deadline = :deadline,
prep_due = CASE WHEN prep_due = :due THEN 0 ELSE prep_due END
WHERE uid = :uid""", mapped).rowcount
conn.executemany("DELETE FROM role_remote WHERE uid = ?",
@@ -1541,7 +1552,8 @@ class Store:
.replace("closed_at", "r.closed_at").replace("extract_tries", "r.extract_tries")
.replace("dup_of", "r.dup_of")
.replace("seniority_model", "r.seniority_model")
.replace("remote_where_model", "r.remote_where_model")]
.replace("remote_where_model", "r.remote_where_model")
.replace("languages_required", "r.languages_required")]
if uids is not None:
where.append("r.uid IN (SELECT value FROM json_each(?))")
return " AND ".join(where), [json.dumps(list(uids))]
@@ -1595,7 +1607,10 @@ class Store:
`remote_model` and `remote_where_model` (a JSON list, `[]` for none),
even when empty, since an answer is what the model read this time;
`remote` and the area are composed from them by prep, so the role is
marked due (`prep.run_roles`, which `extract` calls)."""
marked due (`prep.run_roles`, which `extract` calls).
`languages_required` (a JSON list, `[]` for none) and the deadline,
as `deadline_model` (`YYYY-MM-DD` or ''), are written outright too;
prep composes `deadline` from the host's and this one."""
with self.transaction() as conn:
row = conn.execute("SELECT * FROM roles WHERE uid=?", (uid,)).fetchone()
if row is None:
@@ -1620,9 +1635,14 @@ class Store:
sets["languages"] = json.dumps(languages)
remote = str(fields.get("remote") or "")
where = [str(name) for name in fields.get("remote_where") or []]
filled = len(sets) - ("seniority" in sets) + bool(remote) + bool(where)
named = list(fields.get("languages_required") or [])
deadline = str(fields.get("deadline") or "")
filled = len(sets) - ("seniority" in sets) + bool(remote) + bool(where) \
+ bool(named) + bool(deadline)
sets["remote_model"] = remote
sets["remote_where_model"] = json.dumps(where)
sets["languages_required"] = json.dumps(named)
sets["deadline_model"] = deadline
if "place" in sets:
sets["place_key"] = place_key(sets["place"])
sets["place_fold"] = places.fold(sets["place"])
@@ -1880,15 +1900,22 @@ class Store:
weights: Weights | None = None, enabled: bool = True,
agent_id: int | None = None, limits: Limits | None = None,
prompts: Sequence[Prompt] | None = None,
fetch_terms: Sequence[str] | None = None) -> Agent:
fetch_terms: Sequence[str] | None = None,
cv_text: str | None = None,
cv_languages: Sequence[CvLanguage] | None = None,
avoid_text: str | None = None,
must_text: str | None = None) -> Agent:
"""Create an agent, or with `agent_id` replace that one's fields and
prompts. Without `prompts`, a non-empty `intent` is its one prompt,
named after the agent. Without `fetch_terms` an update keeps the
stored ones and a new agent has none, so a caller that does not know
them (the CLI's prompt edit) loses none. A prompt whose text is unchanged keeps its
cached vector, whatever its name or place, so an edit re-embeds only
the prompts it changed. Raises ValueError on an empty or taken name,
or two prompts of one name, KeyError on an unknown id."""
them (the CLI's prompt edit) loses none; `cv_text`, `cv_languages`,
`avoid_text` and `must_text` are kept the same way. A prompt whose text is
unchanged keeps its cached vector, whatever its name or place, so an
edit re-embeds only the prompts it changed. A save gives every role
`judge` gave up on for the agent its tries back, since what it is
judged against may have changed. Raises ValueError on an empty or
taken name, or two prompts of one name, KeyError on an unknown id."""
name = name.strip()
if not name:
raise ValueError("an agent needs a name")
@@ -1897,23 +1924,36 @@ class Store:
prompts = check_prompts(list(prompts))
terms = None if fetch_terms is None else json.dumps(
normal_terms(list(fetch_terms)), ensure_ascii=False)
spoken = None if cv_languages is None else json.dumps(
[lang.model_dump() for lang in normal_languages(list(cv_languages))])
fields = (name, json.dumps([g.model_dump() for g in gates]),
(weights or Weights()).model_dump_json(),
(limits or Limits()).model_dump_json(), int(enabled), terms)
(limits or Limits()).model_dump_json(), int(enabled), terms,
None if cv_text is None else cv_text.strip(), spoken,
None if avoid_text is None else avoid_text.strip(),
None if must_text is None else must_text.strip())
try:
with self.transaction() as conn:
if agent_id is None:
agent_id = conn.execute(
"""INSERT INTO agents (name, gates, weights, limits, enabled,
fetch_terms, created_at)
VALUES (?,?,?,?,?,COALESCE(?, '[]'),?)""",
fetch_terms, cv_text, cv_languages, avoid_text,
must_text, created_at)
VALUES (?,?,?,?,?,COALESCE(?, '[]'),COALESCE(?, ''),
COALESCE(?, '[]'),COALESCE(?, ''),COALESCE(?, ''),?)""",
(*fields, self.now())).lastrowid
elif not conn.execute(
"""UPDATE agents SET name=?, gates=?, weights=?, limits=?, enabled=?,
fetch_terms=COALESCE(?, fetch_terms)
fetch_terms=COALESCE(?, fetch_terms),
cv_text=COALESCE(?, cv_text),
cv_languages=COALESCE(?, cv_languages),
avoid_text=COALESCE(?, avoid_text),
must_text=COALESCE(?, must_text)
WHERE id=?""",
(*fields, agent_id)).rowcount:
raise KeyError(agent_id)
conn.execute("UPDATE role_judgements SET tries = 0 "
"WHERE agent_id = ? AND tries > 0", (agent_id,))
cached = {r["text"]: (r["vec_key"], r["vec"]) for r in conn.execute(
"SELECT text, vec_key, vec FROM agent_prompts WHERE agent_id=?",
(agent_id,))}