Merge branch 'gates-any'
# Conflicts: # README.md # app/storage/store.py
This commit is contained in:
commit
6779dda888
10 files changed
+143
-28
No files matched your search
@@ -43,7 +43,7 @@ The cog opens `/settings`: a rail of pages on the left, the page beside it, stic
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| **Search agents** | a card per agent, newest first: name, an On/Off switch, what it looks for, and its weights. Edit turns the card into the form in place (name, intent, weights, require/prefer gates behind an Advanced disclosure); New agent puts a blank card on top. Saving or switching on rescores; deleting one takes its votes, liked companies and scores with it |
|
||||
| **Search agents** | a card per agent, newest first: name, an On/Off switch, what it looks for, and its weights. Edit turns the card into the form in place (name, intent, weights, require/any/prefer gates behind an Advanced disclosure); New agent puts a blank card on top. Saving or switching on rescores; deleting one takes its votes, liked companies and scores with it |
|
||||
| **Sources** | one page showing one section at a time, picked in the rail or by the URL fragment: job boards (queries on Jobindex or Adzuna: try, save, edit, remove), company boards (every company whose board is read, its open roles and its affinity for the picked agent, following a company) and runs (Run the pull now, what is running, the schedule, and the Problems log) |
|
||||
| **Account** | the password, sessions and passkeys. Open on loopback, locked on anything else |
|
||||
|
||||
|
||||
+27
-8
@@ -4,8 +4,16 @@ A role is scored per agent, `(uid, agent) -> score`, because a role is only
|
||||
good for a given search. For each agent:
|
||||
|
||||
require gates a role that fails one keeps its row, with score 0 and the
|
||||
reason ("place unknown", "remote not remote")
|
||||
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
|
||||
|
||||
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.
|
||||
fit cos(role vector, intent vector), min-max scaled across
|
||||
the roles this agent scores in this run: the closest is 1,
|
||||
the farthest 0, a lone role 1
|
||||
@@ -117,21 +125,32 @@ def scale(cosines: dict[str, float]) -> dict[str, float]:
|
||||
return {uid: (value - low) / (high - low) for uid, value in cosines.items()}
|
||||
|
||||
|
||||
def _reason(gate: Gate, state: int) -> str:
|
||||
if state < 0:
|
||||
return f"{gate.field} unknown"
|
||||
def _wanted(gate: Gate) -> str:
|
||||
if gate.field == "salary_min":
|
||||
return f"salary_min {min(gate.values, key=lambda v: float(v))}"
|
||||
return f"{gate.field} {' / '.join(str(v) for v in gate.values)}"
|
||||
|
||||
|
||||
def _reason(gate: Gate) -> str:
|
||||
if gate.field == "salary_min":
|
||||
return f"salary_min under {min(gate.values, key=lambda v: float(v))}"
|
||||
return f"{gate.field} not {' / '.join(str(v) for v in gate.values)}"
|
||||
|
||||
|
||||
def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]:
|
||||
"""Every require gate this role fails, as reasons. None for a favourite,
|
||||
which skips the gates."""
|
||||
"""Why this role is gated, as reasons: every require gate it fails, 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. None for a favourite, which skips the gates."""
|
||||
if candidate.affinity == 2:
|
||||
return []
|
||||
return [_reason(g, state) for g, state in zip(gates, candidate.gates, strict=True)
|
||||
if g.mode == "require" and state != 1]
|
||||
states = list(zip(gates, candidate.gates, strict=True))
|
||||
reasons = [_reason(g) for g, state in states if g.mode == "require" and state == 0]
|
||||
group = [(g, state) for g, state in states if g.mode == "any"]
|
||||
if 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
|
||||
|
||||
|
||||
def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights,
|
||||
|
||||
+4
-3
@@ -46,9 +46,10 @@ def create_app(config: Config | None = None) -> FastAPI:
|
||||
store = Store(config.database)
|
||||
jobs.seed_agents(config, store)
|
||||
jobs.seed_searches(config, store)
|
||||
# Roles pulled before `country` was read from `place` have none; after the
|
||||
# first start only the places the lookup cannot place are left to read.
|
||||
store.backfill_country()
|
||||
# Roles stored before a word reached the place lookups have no country or
|
||||
# remote from it; after the first start only the lines the lookups cannot
|
||||
# read are left, and those are read again each start, which is cheap.
|
||||
store.backfill_from_place()
|
||||
|
||||
app = FastAPI(title="roledesk", docs_url="/api/docs",
|
||||
summary="ATS boards and job searches in, roles scored per search agent, "
|
||||
|
||||
+30
-3
@@ -188,6 +188,30 @@ def country_of(place: str) -> str:
|
||||
Remote = Literal["", "remote", "hybrid", "onsite"]
|
||||
REMOTE: tuple[str, ...] = get_args(Remote)
|
||||
|
||||
#: Words in a location line that say how the role is worked, for a board that
|
||||
#: gives no remote flag of its own: Greenhouse writes "Remote", "Hybrid" or
|
||||
#: "Distributed" as the location and nothing else.
|
||||
_REMOTE_WORDS: tuple[tuple[str, str], ...] = (
|
||||
("hybrid", "hybrid"),
|
||||
("remote", "remote"), ("distributed", "remote"), ("home based", "remote"),
|
||||
("home-based", "remote"), ("work from home", "remote"), ("anywhere", "remote"),
|
||||
("worldwide", "remote"), ("telecommute", "remote"),
|
||||
("on-site", "onsite"), ("onsite", "onsite"), ("on site", "onsite"),
|
||||
("in-office", "onsite"), ("in office", "onsite"),
|
||||
)
|
||||
|
||||
|
||||
def remote_of(place: str) -> str:
|
||||
"""How a board's location line says the role is worked, as one of
|
||||
`REMOTE`, or '' when it does not say. "Hybrid" wins over "remote" in the
|
||||
same line ("Remote or hybrid, Berlin" is hybrid: some days in an office),
|
||||
and a line that names only a place is ''."""
|
||||
folded = _fold(place or "")
|
||||
for word, mode in _REMOTE_WORDS:
|
||||
if word in folded:
|
||||
return mode
|
||||
return ""
|
||||
|
||||
Seniority = Literal["", "intern", "junior", "mid", "senior", "staff", "lead", "manager",
|
||||
"director", "exec"]
|
||||
SENIORITIES: tuple[str, ...] = get_args(Seniority)
|
||||
@@ -363,13 +387,16 @@ GateField = Literal["place", "country", "remote", "seniority", "function", "sala
|
||||
"currency", "permit", "languages", "company"]
|
||||
GATE_FIELDS: tuple[str, ...] = get_args(GateField)
|
||||
|
||||
GateMode = Literal["require", "prefer"]
|
||||
GateMode = Literal["require", "any", "prefer"]
|
||||
|
||||
|
||||
class Gate(Model):
|
||||
"""One condition of an agent. `require` gates a role that fails it (the
|
||||
row is kept with the reason); `prefer` adds `weight` to its score when it
|
||||
matches."""
|
||||
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. 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."""
|
||||
|
||||
field: GateField
|
||||
values: list[str | int | float] = Field(min_length=1)
|
||||
|
||||
+16
-8
@@ -23,7 +23,7 @@ from desk.models import Item
|
||||
from app.models import (
|
||||
AFFINITY, REMOTE, STATUSES, Agent, Application, ApplicationEvent, Candidate,
|
||||
Company, Gate, Limits, Role, ScoredRole, Subscription, Weights, company_key,
|
||||
country_of, place_key, title_key,
|
||||
country_of, place_key, remote_of, title_key,
|
||||
)
|
||||
from app.storage import schema
|
||||
|
||||
@@ -291,6 +291,7 @@ class Store:
|
||||
}
|
||||
row |= {name: str(raw.get(name) or "") for name in _RAW_TEXT}
|
||||
row["country"] = row["country"] or country_of(place)
|
||||
row["remote"] = row["remote"] or remote_of(place)
|
||||
row |= {name: _int_or_none(raw.get(name)) for name in _RAW_INT}
|
||||
|
||||
keep_filled = ("company", "company_key", "place_key", "remote", "description",
|
||||
@@ -608,14 +609,21 @@ class Store:
|
||||
"UPDATE roles SET closed_at = ? WHERE subscription_id = ? AND closed_at IS NULL",
|
||||
(self.now(), subscription_id))
|
||||
|
||||
def backfill_country(self) -> int:
|
||||
"""Fill `country` from `place` on every role that has none, the way
|
||||
`upsert_role` does for a new one. Returns how many it filled."""
|
||||
rows = self._rows("SELECT uid, place FROM roles WHERE country = '' AND place != ''")
|
||||
filled = [(code, r["uid"]) for r in rows if (code := country_of(r["place"]))]
|
||||
def backfill_from_place(self) -> int:
|
||||
"""Fill `country` and `remote` from `place` on every role that has
|
||||
none, the way `upsert_role` does for a new one, so a word added to the
|
||||
lookups reaches the roles already stored. Returns how many columns it
|
||||
filled."""
|
||||
rows = self._rows("SELECT uid, place FROM roles "
|
||||
"WHERE place != '' AND (country = '' OR remote = '')")
|
||||
countries = [(code, r["uid"]) for r in rows if (code := country_of(r["place"]))]
|
||||
remotes = [(mode, r["uid"]) for r in rows if (mode := remote_of(r["place"]))]
|
||||
with self.transaction() as conn:
|
||||
conn.executemany("UPDATE roles SET country = ? WHERE uid = ?", filled)
|
||||
return len(filled)
|
||||
conn.executemany("UPDATE roles SET country = ? WHERE uid = ? AND country = ''",
|
||||
countries)
|
||||
conn.executemany("UPDATE roles SET remote = ? WHERE uid = ? AND remote = ''",
|
||||
remotes)
|
||||
return len(countries) + len(remotes)
|
||||
|
||||
# -------------------------------------------------------------- extract
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
<summary>Advanced</summary>
|
||||
<label>Gates
|
||||
<textarea id="a-gates" class="code" rows="6" spellcheck="false">[]</textarea>
|
||||
<span class="hint">Rules a role must meet (require) or that lift it (prefer), as JSON: <code>[{"field": "country", "values": ["DK"], "mode": "require"}, {"field": "remote", "values": ["remote"], "mode": "prefer", "weight": 0.05}]</code>. Fields: place, country, remote, seniority, function, salary_min, currency, permit, languages, company. EU is a role open anywhere in Europe.</span></label>
|
||||
<span class="hint">Rules a role must meet (require), must meet one of (any), or that lift it (prefer), as JSON: <code>[{"field": "country", "values": ["DK"], "mode": "any"}, {"field": "remote", "values": ["remote"], "mode": "any"}, {"field": "seniority", "values": ["senior", "staff"], "mode": "prefer", "weight": 0.05}]</code> is "in Denmark, or remote". Fields: place, country, remote, seniority, function, salary_min, currency, permit, languages, company. EU is a role open anywhere in Europe. A rule only rejects a role whose column is filled; an empty one ranks until extract reads the posting.</span></label>
|
||||
</details>
|
||||
<div class="agent-actions">
|
||||
<button type="submit">Save and score</button>
|
||||
|
||||
+4
-1
@@ -81,7 +81,10 @@ sources: {}
|
||||
# adzuna: {app_id: "...", app_key: "..."}
|
||||
|
||||
# Starter search agents, imported once into an empty `agents` table. Each gate
|
||||
# is {field, values, mode: require|prefer, weight}; fields are place, country,
|
||||
# is {field, values, mode: require|any|prefer, weight}; the any gates are one
|
||||
# group a role must match one of ("in Denmark, or remote"), and a gate rejects
|
||||
# only a role whose column is filled, since extract fills the rest for the
|
||||
# roles that rank. Fields are place, country,
|
||||
# remote, seniority, function, salary_min, currency, permit, languages and
|
||||
# company. weights default to {fit: 0.6, recency: 0.2, affinity: 0.2, taste:
|
||||
# 0.2}; taste is how close a role is to the ones the agent liked minus the
|
||||
|
||||
@@ -78,6 +78,8 @@ What was decided for roledesk and the one reason for each. The longer reasoning
|
||||
|
||||
**A score is the weighted sum over the sum of the weights.** The agent's `fit`, `recency` and `affinity` weights and every prefer gate's `weight` form one denominator, so a score stays in 0..1 and the 0.9 floor for a favourite means the same thing whatever the weights are.
|
||||
|
||||
**A gate rejects on evidence, not on absence, and `any` gates are one group.** A require gate used to gate a role whose column was empty ("country unknown"), and extract, which fills that column, is bounded to the roles that rank, so 2,281 of 15,556 open roles were out of reach for good: every board that writes "Remote", "Hybrid" or "Distributed" as its location. Now an empty column neither passes nor fails; the role ranks, extract reads it, and the rescore gates it on what it found. `any` is the third mode: the `any` gates form one group a role must match one of, so "in Denmark, or remote" is two `any` gates, where require alone could only say "and". `remote` is also read off the location line, as `country` is, since Greenhouse puts "Hybrid" there and nowhere else.
|
||||
|
||||
**Gates match loosely where the column is free text.** `place` is a case-insensitive substring, so `Berlin` matches "Berlin, Germany"; `company` compares company keys, `languages` passes when every required language is a value, `salary_min` is at least the smallest value, and the rest are case-insensitive equality. Each gate is one SQL `CASE` column, so a require gate filters while the gated row keeps every failed gate as its reason.
|
||||
|
||||
**A role with no vector is scored without `fit`.** The fit weight leaves the denominator, so a role `embed` has not reached yet ranks on recency, affinity and prefer gates instead of sinking to the bottom. It is gated as "no vector" only when fit is the only weighted term, and as "no intent vector" when the intent could not be embedded.
|
||||
|
||||
+42
-2
@@ -156,12 +156,52 @@ def test_a_require_gate_keeps_the_row_with_a_reason(
|
||||
scores = store.scores(agent.id)
|
||||
assert scores[berlin][1] == "" and scores[berlin][0] > 0
|
||||
assert scores[munich] == (0.0, "place not Berlin")
|
||||
assert scores[nowhere] == (0.0, "place unknown")
|
||||
# An empty column is no evidence: the role ranks, and extract can reach it.
|
||||
assert scores[nowhere][1] == "" and scores[nowhere][0] > 0
|
||||
shown = store.roles(agent=agent.id)
|
||||
assert [r.uid for r in shown] == [berlin]
|
||||
assert {r.uid for r in shown} == {berlin, nowhere}
|
||||
assert {r.uid for r in store.roles(agent=agent.id, gated=True)} == {berlin, munich, nowhere}
|
||||
|
||||
|
||||
def test_any_gates_are_one_group_a_role_must_match_one_of(
|
||||
store: Store, config: Config, fake: FakeEmbedder) -> None:
|
||||
""""In Denmark, or remote": a role passes on either, is gated when it has
|
||||
evidence against both, and ranks when both columns are empty."""
|
||||
copenhagen = role(store, "1", place="Copenhagen, Denmark")
|
||||
remote_de = role(store, "2", place="Berlin, Germany", remote="remote")
|
||||
onsite_de = role(store, "3", place="Berlin, Germany", remote="onsite")
|
||||
hybrid_nowhere = role(store, "4", place="Hybrid") # remote from place, no country
|
||||
blank = role(store, "5")
|
||||
agent = store.save_agent("Copenhagen or remote", "", [
|
||||
Gate(field="country", values=["DK"], mode="any"),
|
||||
Gate(field="remote", values=["remote"], mode="any")])
|
||||
|
||||
run(config, store, score.run_score)
|
||||
|
||||
scores = store.scores(agent.id)
|
||||
assert scores[copenhagen][1] == "" and scores[remote_de][1] == ""
|
||||
assert scores[onsite_de] == (0.0, "none of country DK, remote remote")
|
||||
assert scores[blank][1] == ""
|
||||
# "Hybrid" is a country-less line the remote lookup reads as hybrid, so
|
||||
# the group has evidence against remote and none for country: gated.
|
||||
assert scores[hybrid_nowhere][1].startswith("none of")
|
||||
|
||||
|
||||
def test_failed_gates_reads_evidence_only() -> None:
|
||||
gates = [Gate(field="country", values=["DK"]),
|
||||
Gate(field="remote", values=["remote"], mode="any"),
|
||||
Gate(field="seniority", values=["senior"], mode="any"),
|
||||
Gate(field="function", values=["engineering"], mode="prefer")]
|
||||
def cand(*states: int) -> Candidate:
|
||||
return Candidate(uid="u", first_seen=NOW.isoformat(), affinity=0, gates=list(states))
|
||||
assert score.failed_gates(cand(1, 1, 0, 0), gates) == []
|
||||
assert score.failed_gates(cand(-1, -1, -1, 0), gates) == []
|
||||
assert score.failed_gates(cand(0, 0, -1, 1), gates) == [
|
||||
"country not DK", "none of remote remote, seniority senior"]
|
||||
assert score.failed_gates(cand(1, 0, 0, 1), gates) == [
|
||||
"none of remote remote, seniority senior"]
|
||||
|
||||
|
||||
def test_a_prefer_gate_adds_its_weight_to_the_score(
|
||||
store: Store, config: Config, fake: FakeEmbedder) -> None:
|
||||
remote = role(store, "1", remote="remote")
|
||||
|
||||
+16
-1
@@ -20,7 +20,7 @@ from plugins import search # noqa: E402
|
||||
from plugins.search import SearchSource # noqa: E402
|
||||
from app.config import SearchSeed # noqa: E402
|
||||
from app.jobs import pull # noqa: E402
|
||||
from app.models import country_of # noqa: E402
|
||||
from app.models import country_of, remote_of # noqa: E402
|
||||
from app.storage import Store # noqa: E402
|
||||
|
||||
FIXTURES = ROOT / "tests" / "fixtures" / "search"
|
||||
@@ -53,6 +53,21 @@ def test_country_of(place: str, code: str) -> None:
|
||||
assert country_of(place) == code
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("place", "mode"), [
|
||||
("Remote", "remote"),
|
||||
("Distributed", "remote"),
|
||||
("Home based - Worldwide", "remote"),
|
||||
("Remote - US", "remote"),
|
||||
("Hybrid", "hybrid"),
|
||||
("Remote or hybrid, Berlin", "hybrid"),
|
||||
("In-Office", "onsite"),
|
||||
("Berlin, Germany", ""),
|
||||
("", ""),
|
||||
])
|
||||
def test_remote_of(place: str, mode: str) -> None:
|
||||
assert remote_of(place) == mode
|
||||
|
||||
|
||||
def test_upsert_fills_country_from_place_unless_the_plugin_says(store: Store) -> None:
|
||||
def item(source_id: str, **raw: Any) -> Item:
|
||||
it = Item(source_id=source_id, location="https://x.example/1", title="Engineer",
|
||||
|
||||
Reference in new issue
Block a user