From 3388b97cf4f89be6dd9c1e4e8c4dad021388c8ce Mon Sep 17 00:00:00 2001 From: Wolf Date: Mon, 5 Oct 2026 09:25:43 +0200 Subject: [PATCH] =?UTF-8?q?The=20location=20gate=20rejects=20on=20evidence?= =?UTF-8?q?=20only:=20a=20place=20gate=20is=20empty=20for=20a=20role=20wit?= =?UTF-8?q?h=20no=20place=20the=20gazetteer=20resolved,=20an=20any=20group?= =?UTF-8?q?=20whose=20country=20and=20place=20members=20are=20all=20empty?= =?UTF-8?q?=20passes=20whatever=20remote=20says,=20and=20a=20role=20place?= =?UTF-8?q?=20that=20holds=20one=20of=20a=20country=20or=20place=20gate's?= =?UTF-8?q?=20places=20(=C3=98resund,=20Sweden,=20Europe)=20meets=20it.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.1 --- app/jobs/score.py | 22 ++++++--- app/storage/store.py | 43 ++++++++++++----- tests/test_gazetteer.py | 3 +- tests/test_score.py | 104 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 150 insertions(+), 22 deletions(-) diff --git a/app/jobs/score.py b/app/jobs/score.py index 90e1249..9e19e14 100644 --- a/app/jobs/score.py +++ b/app/jobs/score.py @@ -9,16 +9,19 @@ good for a given search. Each agent first applies its fetch gate: 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 locations + gated ("none of country DK / remote remote"), unless its + location is unknown; skipped for a company set to All + locations exclude gates a role that matches one of the values keeps its row, with 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 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 gated by it, and a place the gazetteer could not resolve, or one +that holds the agent's places (Øresund for Copenhagen), is no evidence. +`extract` is what fills an empty column, and it reads 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 @@ -181,6 +184,10 @@ def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]: 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 role whose location is unknown, every `country` and `place` member + empty, passes the group whatever its remote says: a hybrid role with no + place may still be in one of the agent's towns, and extract reads it + only in the cut. A group of remote alone still fails a hybrid role. A company set to All locations 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.""" @@ -190,7 +197,10 @@ def failed_gates(candidate: Candidate, gates: Sequence[Gate]) -> list[str]: 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 not candidate.all_countries and group and not any(state == 1 for _, state in group) \ + located = [state for g, state in group if g.field in ("country", "place")] + unknown = bool(located) and all(state == -1 for state in located) + if not candidate.all_countries and group and not unknown \ + 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 diff --git a/app/storage/store.py b/app/storage/store.py index 000931a..5d7f75b 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -289,14 +289,22 @@ def _gate_sql(gate: Gate, gaz: places.Gazetteer) -> tuple[str, str, list[Any]]: `params` belong to `match`; `empty` takes none. `country` and `place` match over the gazetteer, as the feed's Place - filter does (`_places_sql`): a country gate is met by a role whose - `country` is one of its codes or that has a place in one of them, so - `DK` is met by `Copenhagen; London`. Its column is empty only when the - role has neither. A `require` gate on country reads a remote role whose + filter does (`_places_sql`), and also on a role place that holds one of + the gate's places (`wider`): a country gate is met by a role whose + `country` is one of its codes or that has a place in or around one of + them, so `DK` is met by `Copenhagen; London`, and by Øresund or Europe + alone, which are no evidence the role is outside Denmark; a town in + Germany is. Its column is empty only when the role has neither a country + nor a resolved place. A `require` gate on country reads a remote role whose area is stated through the area alone, as the remote chips do (`Gazetteer.overlap`): "Home Based - Worldwide" and "Germany (Remote)" meet Europe, "Remote US" does not, and neither is empty. A place gate's - value is a place token, bare text a `place:`. + value is a place token, bare text a `place:`. Its column is empty when + the role has no place the gazetteer resolved, as "Storkøbenhavn" or + "Headquarters" are not: that is no evidence the role is elsewhere. Such + a role can still match, by its country or by whole words of its line + for a town the gazetteer does not know, so a place gate's `match` is + tested before its `empty` (`Store.score_candidates`). `remote_from` takes place tokens without their `remote:` (`country:DK`, `anywhere`) and matches as the remote chips do. It is empty for a role @@ -314,7 +322,8 @@ def _gate_sql(gate: Gate, gaz: places.Gazetteer) -> tuple[str, str, list[Any]]: return f"(r.remote = '' OR (r.remote = 'remote' AND NOT {_HAS_AREA}))", match, params if gate.field == "country": tokens = [f"country:{v}" for v in values] - match, params = _places_sql(tokens, gaz, table="r", codes=[v.upper() for v in values]) + match, params = _places_sql(tokens, gaz, table="r", codes=[v.upper() for v in values], + wider=True) empty = f"(r.country = '' AND {_unplaced(gaz)})" if gate.mode != "require": return empty, match, params @@ -323,8 +332,8 @@ def _gate_sql(gate: Gate, gaz: places.Gazetteer) -> tuple[str, str, list[Any]]: return (f"({empty} AND NOT {_HAS_AREA})", f"(CASE WHEN {_HAS_AREA} THEN {area} ELSE {match} END)", [*more, *params]) if gate.field == "place": - match, params = _places_sql(values, gaz, table="r") - return "r.place = ''", match, params + match, params = _places_sql(values, gaz, table="r", wider=True) + return _unplaced(gaz), match, params if gate.field == "title": # Whole words of the folded title: " account executive " inside # " enterprise account executive uk ". A fold drops `%` and `_`, so a @@ -344,13 +353,19 @@ def _gate_sql(gate: Gate, gaz: places.Gazetteer) -> tuple[str, str, list[Any]]: def _places_sql(tokens: Sequence[str], gaz: places.Gazetteer, *, table: str = "roles", - codes: Sequence[str] = (), scope: Sequence[Gate] = ()) -> tuple[str, list[Any]]: + codes: Sequence[str] = (), scope: Sequence[Gate] = (), + wider: bool = False) -> tuple[str, list[Any]]: """Any of the place tokens over `table`, as a join over the gazetteer: a role with a place under one of the places the tokens name (`country:DK` every place in Denmark, `region:skane` every town in Skåne, `place:malmo` Malmö by any spelling), or whose `country` is one of their countries (a role whose board gave a country and no place). `codes` are more values of - `country` to accept. A `place:` token the gazetteer does not know is + `country` to accept. `wider` also takes a role place that holds one of + them (`Gazetteer.overlap`): Øresund, Europe or Anywhere meet + `country:DK`, as the fetch gate reads them (`_gate_sql`); the feed's + Place filter leaves it off. The `country` column is not widened, as + Sweden would then meet Malmö for a role in Stockholm. A `place:` token + the gazetteer does not know is matched on whole words of the role's folded line (`places.match_sql`), so a town nobody has entered yet is still found. @@ -381,7 +396,7 @@ def _places_sql(tokens: Sequence[str], gaz: places.Gazetteer, *, table: str = "r params += values continue under = gaz.under(named) - ids |= under + ids |= gaz.overlap(named) if wider else under found |= gaz.codes(under) if ids: clauses.insert(0, f"{table}.uid IN (SELECT uid FROM role_places " @@ -2317,7 +2332,11 @@ class Store: empty, match, values = _gate_sql(part, gaz) whens.append(f"WHEN {match} THEN {k}") params += values - columns.append(f"CASE WHEN {empty} THEN -1 {' '.join(whens)} ELSE 0 END AS g{n}") + # A place gate's match goes first: an unresolved place can still + # meet one of its values (`_gate_sql`). + case = (f"{' '.join(whens)} WHEN {empty} THEN -1" if gate.field == "place" + else f"WHEN {empty} THEN -1 {' '.join(whens)}") + columns.append(f"CASE {case} ELSE 0 END AS g{n}") select = ", ".join(["r.uid", "r.posted_at", "COALESCE(ac.affinity, 0) AS affinity", "COALESCE(c.all_countries, 0) AS all_countries", diff --git a/tests/test_gazetteer.py b/tests/test_gazetteer.py index 66d916a..48e2206 100644 --- a/tests/test_gazetteer.py +++ b/tests/test_gazetteer.py @@ -328,8 +328,9 @@ def test_a_place_gate_matches_over_the_gazetteer(store: Store, several: dict[str, str]) -> None: found = gates(store, Gate(field="place", values=["Malmö", "Lund", "oberkochen"], mode="any")) + # "Remote (SE)" is placed at Sweden, which holds Malmö: no evidence against. assert {uid for uid, g in found.items() if g == 1} == { - several["malmo"], several["lund"], several["zeiss"]} + several["malmo"], several["lund"], several["zeiss"], several["remote"]} # ------------------------------------------------------------ unresolved texts diff --git a/tests/test_score.py b/tests/test_score.py index adc081d..8f69d20 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -346,9 +346,89 @@ def test_any_gates_are_one_group_a_role_must_match_one_of( 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") + # "Hybrid" is a country-less line the remote lookup reads as hybrid: the + # role may still be in Denmark, so it passes until extract reads it. + assert scores[hybrid_nowhere][1] == "" + + +def test_an_unknown_location_passes_the_any_group_whatever_its_remote( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """A place line the gazetteer cannot resolve is no evidence the role is + elsewhere, for the country and for the place gate: the role passes, + hybrid or not. One whose unknown line names a gate value by whole words + matches it, and a role in a resolved place elsewhere is still gated, its + remote unknown or not.""" + jobbank = role(store, "1", place="Storkøbenhavn; Udlandet (øvrige)", remote="hybrid") + office = role(store, "2", place="Fleet Place Office", remote="onsite") + avedore = role(store, "3", place="Avedøre Holme") + berlin = role(store, "4", place="Berlin, Germany") + berlin_hybrid = role(store, "5", place="Berlin, Germany", remote="hybrid") + agent = store.save_agent("Malmö, Avedøre or remote", "", [ + Gate(field="country", values=["SE"], mode="any"), + Gate(field="remote", values=["remote"], mode="any"), + Gate(field="place", values=["Malmö", "Avedøre Holme"], mode="any")]) + + run(config, store, score.run_score) + + states = {c.uid: c.gates for c in store.score_candidates(agent.id, agent.gates)} + assert states[jobbank] == [-1, 0, -1] and states[office] == [-1, 0, -1] + assert states[avedore] == [-1, -1, 1] + assert states[berlin] == [0, -1, 0] + scores = store.scores(agent.id) + assert all(scores[uid][1] == "" for uid in (jobbank, office, avedore)) + reason = "none of country SE, remote remote, place Malmö / Avedøre Holme" + assert scores[berlin] == (0.0, reason) and scores[berlin_hybrid] == (0.0, reason) + + +def test_a_remote_only_group_still_gates_a_hybrid_role( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """With no country or place in the group, remote is the only evidence: + a hybrid role is gated, one whose remote is unknown passes.""" + hybrid = role(store, "1", place="Headquarters", remote="hybrid") + unknown = role(store, "2", place="Headquarters") + agent = store.save_agent("Remote", "", [Gate(field="remote", values=["remote"], mode="any")]) + + run(config, store, score.run_score) + + scores = store.scores(agent.id) + assert scores[hybrid] == (0.0, "none of remote remote") + assert scores[unknown][1] == "" + + +def test_a_region_that_holds_the_agents_places_is_no_evidence_against( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """Øresund holds the Capital Region and Skåne, so a role placed only + there meets a gate on Denmark and one on Copenhagen; Berlin meets + neither. The feed's Place filter still reads only what lies under.""" + oresund = role(store, "1", place="Øresundsregionen", remote="onsite") + berlin = role(store, "2", place="Berlin, Germany", remote="onsite") + by_country = store.save_agent("Denmark", "", [ + Gate(field="country", values=["DK"], mode="any")]) + by_town = store.save_agent("Copenhagen", "", [ + Gate(field="place", values=["copenhagen"], mode="any")]) + world = store.save_agent("Denmark only", "", [Gate(field="country", values=["DK"])]) + + run(config, store, score.run_score) + + for agent in (by_country, by_town, world): + scores = store.scores(agent.id) + assert scores[oresund][1] == "" + assert scores[berlin][1] != "" + assert oresund not in {r.uid for r in store.roles(places=["country:DK"])} + + +def test_a_country_that_holds_the_agents_town_is_no_match_for_a_role_placed_elsewhere( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """Sweden holds Malmö, yet a role in Stockholm is evidence against it.""" + stockholm = role(store, "1", place="Stockholm, Sweden", remote="onsite") + malmo = role(store, "2", place="Malmö, Sweden", remote="onsite") + agent = store.save_agent("Malmö", "", [Gate(field="place", values=["Malmö"], mode="any")]) + + run(config, store, score.run_score) + + scores = store.scores(agent.id) + assert scores[stockholm] == (0.0, "none of place Malmö") + assert scores[malmo][1] == "" def test_failed_gates_reads_evidence_only() -> None: @@ -365,6 +445,24 @@ def test_failed_gates_reads_evidence_only() -> None: "none of remote remote, seniority senior"] +def test_an_any_group_with_every_place_member_empty_does_not_fail() -> None: + """Remote is no evidence against a role whose location is unknown; a + known location elsewhere is, also with remote unknown.""" + gates = [Gate(field="country", values=["DK"], mode="any"), + Gate(field="remote", values=["remote"], mode="any"), + Gate(field="place", values=["Malmö"], mode="any")] + def cand(*states: int) -> Candidate: + return Candidate(uid="u", posted_at=NOW.isoformat(), gates=list(states)) + assert score.failed_gates(cand(-1, 0, -1), gates) == [] + assert score.failed_gates(cand(0, -1, -1), gates) == [ + "none of country DK, remote remote, place Malmö"] + assert score.failed_gates(cand(-1, -1, 0), gates) == [ + "none of country DK, remote remote, place Malmö"] + remote_only = [Gate(field="remote", values=["remote"], mode="any")] + assert score.failed_gates(cand(0), remote_only) == ["none of remote remote"] + assert score.failed_gates(cand(-1), remote_only) == [] + + def test_a_gate_is_require_any_or_exclude_and_has_no_weight() -> None: """Prefer gates left the agent: hybrid is a feed filter now.""" with pytest.raises(ValidationError):