From fb24c8e4c9bd38cd6bd12529c316aa697db9cc4a Mon Sep 17 00:00:00 2001 From: Wolf Date: Sun, 4 Oct 2026 08:15:26 +0200 Subject: [PATCH] Lead a role in several cities with the one nearest the viewing agent, in the row, the reader and the posting link Places are ordered in tiers off the agent's gates (the any group, then the require scope, then the rest), and within a tier by the home the any group gives, since the gazetteer holds no coordinates. GET /api/roles and /api/roles/{uid} carry place_texts, lead_place and lead_url for the roles served; the gates and the dupes rule are unchanged. Co-Authored-By: Claude Opus 5.5 --- app/models.py | 18 +++++- app/places.py | 111 ++++++++++++++++++++++++++++++++++-- app/static/roles.js | 22 ++++--- app/storage/store.py | 57 ++++++++++++++---- docs/decisions.md | 4 +- tests/test_dupes_storage.py | 33 +++++++++++ tests/test_gazetteer.py | 59 +++++++++++++++++++ tests/test_web.py | 2 +- 8 files changed, 277 insertions(+), 29 deletions(-) diff --git a/app/models.py b/app/models.py index 23f5909..c4023ca 100644 --- a/app/models.py +++ b/app/models.py @@ -1194,9 +1194,17 @@ class Role(Model): #: The open copies of this role, oldest first, when it is the kept one; #: empty otherwise. Not a column: filled by `Store.role`. copies: list[RoleCopy] = [] - #: Every place of the role, in the host's order. Not a column: filled by - #: `Store.role` from `role_places`. + #: Every place of the role, the nearest to the viewing agent first + #: (`places.Nearness`), else in the host's order. Not a column: filled + #: by `Store.role` from `role_places`. places: list[RolePlace] = [] + #: The text of the first of `places`, what the row and the reader lead + #: with; '' for a role with no place. + lead_place: str = "" + #: The link of the folded copy whose place leads, which the posting + #: opens at; '' when the role's own place leads, and the role's own + #: link stands. + lead_url: str = "" #: Where a remote hire may live, as a label: "anywhere", "Europe", #: "United States, Canada". '' when the role is not remote or its area is #: not stated. Not a column: filled by `Store` from `role_remote`. @@ -1321,6 +1329,12 @@ class RoleRow(Model): excerpt: str = "" #: How many open roles are copies of this one (`Role.copies`), 0 when none. copies: int = 0 + #: The distinct place texts, the nearest to the agent first; `place` is + #: the first of them and "+N" for the others. `lead_place` and + #: `lead_url` as on `Role`. + place_texts: list[str] = [] + lead_place: str = "" + lead_url: str = "" #: As `Role.remote_where`: the area a remote hire may live in, '' when #: the role is not remote or does not say. remote_where: str = "" diff --git a/app/places.py b/app/places.py index 76f5a94..8d564b7 100644 --- a/app/places.py +++ b/app/places.py @@ -52,13 +52,13 @@ from collections.abc import Callable, Iterable, Sequence from dataclasses import dataclass from functools import lru_cache -from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, _place_fold, - country_of, remote_of) +from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, Gate, + _place_fold, country_of, remote_of) -__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer", "Place", - "Region", "Seed", "Suggestion", "Token", "fold", "known", "label", "match_sql", - "matches", "parse", "placeless", "register", "remote_part", "seed", "suggest", - "towns"] +__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer", + "Nearness", "Place", "Region", "Seed", "Suggestion", "Token", "fold", "known", "label", + "match_sql", "matches", "nearest_first", "parse", "placeless", "register", + "remote_part", "seed", "suggest", "towns"] def fold(text: str) -> str: @@ -1255,3 +1255,102 @@ class Gazetteer: else: found = {p for form in _expand(tok.value) for p in self.aliases.get(form, ())} return frozenset(found) or None + + +# ------------------------------------------------------------------ nearness + +class Nearness: + """How near each place of a role is to one agent, read off its gates: a + role posted in several cities is shown, and linked, by the nearest. + + Three tiers. A place comes first when it meets the agent's `any` group: + it lies under one of the group's `country` or `place` values, or it is a + remote part whose area lies under one of them, is open to the agent + through a `remote_from` value as the remote chips match, or meets the + agent's scope (its `require` gates on country) under a plain `remote` + any gate, as `remote:any` does. A place in the scope comes second, the rest third, a remote part with no + stated area ("Remote") among them. The gazetteer holds no coordinates, + so within a tier the home stands in for distance: the first value of + the first `any` gate on country or place (Denmark for "DK, SE"). A + place in one of the group's own towns inside the home comes first + (Copenhagen and Odense), then one elsewhere in the home (Billund, + "Denmark"), then the rest, each in the host's order. The gates + themselves never read this: they read every place.""" + + def __init__(self, gaz: Gazetteer, gates: Iterable[Gate]) -> None: + self.gaz = gaz + near: set[int] = set() + towns: set[int] = set() + home: frozenset[int] | None = None + self._words: list[str] = [] + self._remote_any = False + self._remote_from: set[int] = set() + scopes: list[frozenset[int]] = [] + for gate in gates: + values = [str(v).strip() for v in gate.values if str(v).strip()] + if gate.field == "country" and gate.mode == "require": + scopes.append(frozenset(p for v in values for p in gaz.lookup(f"country:{v}") or ())) + if gate.mode != "any": + continue + if gate.field == "remote": + self._remote_any |= any(fold(v) == "remote" for v in values) + elif gate.field == "remote_from": + for value in values: + tok = parse(f"remote:{value}") + if tok.value == "any": + self._remote_any = True + elif named := gaz.lookup(str(tok)): + self._remote_from |= named if tok.value == "anywhere" else gaz.overlap(named) + elif gate.field in ("country", "place"): + for value in values: + token = f"country:{value}" if gate.field == "country" else value + named = gaz.lookup(token) + if named is None: + self._words.append(token) + continue + near |= named + if gate.field == "place": + towns |= named + if home is None: + home = named + self._near = gaz.under(near) + self._home = gaz.under(home or ()) + self._home_towns = gaz.under(p for p in towns if p in self._home) + self._scope = [gaz.under(s) for s in scopes] + self._scope_areas = [gaz.overlap(s) for s in scopes] + + def _in_scope(self, ids: Sequence[int]) -> bool: + return bool(self._scope) and any(all(p in s for s in self._scope) for p in ids) + + def _area_in_scope(self, area: Sequence[int]) -> bool: + return all(any(p in s for p in area) for s in self._scope_areas) + + def key(self, text: str, ids: Iterable[int | None]) -> tuple[int, int]: + """`(tier, 0 in a home town, 1 in the home, else 2)` of one place + text and the places it resolved to, lower nearer.""" + found = [p for p in ids if p is not None and p in self.gaz.places] + area = self.gaz.remote_places(text) + named = any(p in self._near for p in (*found, *area)) or ( + not found and any(matches(fold(text), "", w) for w in self._words)) + remote = bool(area) and (any(p in self._remote_from for p in area) + or (self._remote_any and self._area_in_scope(area))) + if named or remote: + tier = 1 + elif self._in_scope(found) or (area and self._scope and self._area_in_scope(area)): + tier = 2 + else: + tier = 3 + both = (*found, *area) + return tier, (0 if any(p in self._home_towns for p in both) + else 1 if any(p in self._home for p in both) else 2) + + +def nearest_first(near: Nearness | None, + entries: Sequence[tuple[str, Sequence[int | None]]]) -> list[int]: + """The indices of a role's places `(text, place ids)` nearest first + (`Nearness.key`), the host's order within a key and throughout without + an agent.""" + order = list(range(len(entries))) + if near is None: + return order + return sorted(order, key=lambda i: near.key(*entries[i])) diff --git a/app/static/roles.js b/app/static/roles.js index 33f0be9..2ece0ee 100644 --- a/app/static/roles.js +++ b/app/static/roles.js @@ -979,13 +979,20 @@ return n > 1 ? `×${n}` : ""; } - /* Where a role's posting is: the ad's own page once it is known + /* Where a role's own posting is: the ad's own page once it is known (`posting_url`, found for a Jobindex role when its text is fetched), else the link the board listed. Either way only an http(s) link. */ - function linkOf(x) { + function ownLink(x) { return safeUrl(x?.posting_url) || safeUrl(x?.original_url); } + /* Where the row and the reader open a role: the copy of the place it leads + with, the one nearest the agent (`lead_url`, `places.Nearness`), else + its own posting. */ + function linkOf(x) { + return safeUrl(x?.lead_url) || ownLink(x); + } + /* The other postings of this role, each a link to where it is posted, labelled by host and place, as newsdesk's "Also covered by". */ function alsoPosted(r) { @@ -1004,11 +1011,12 @@ `; } - /* The role's places in the host's order, one entry for each place text a - link opens: the copies' own links, and the role's link for its own. */ + /* The role's places as the server orders them, the nearest to the agent + first, one entry for each place text a link opens: the copies' own + links, and the role's link for its own. */ function placeEntries(r) { const seen = new Set(); - const own = linkOf(r); + const own = ownLink(r); const out = []; for (const p of Array.isArray(r.places) ? r.places : []) { const url = p.url ? safeUrl(p.url) : own; @@ -1020,8 +1028,8 @@ return out; } - /* The place line of the reader: the first place and "+N" for the others, - as the feed's row shows it. */ + /* The place line of the reader: the leading place and "+N" for the + others, as the feed's row shows it. */ function placeLine(r) { const texts = [...new Set(placeEntries(r).map((e) => e.text))]; return texts.length > 1 ? `${texts[0]} +${texts.length - 1}` : r.place || texts[0] || ""; diff --git a/app/storage/store.py b/app/storage/store.py index 9d62f1b..443f99c 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -214,6 +214,27 @@ def _own_texts(conn: sqlite3.Connection, uid: str) -> list[str]: (uid,)))) +def _nearest_first(rows: Sequence[RolePlace], + near: places.Nearness | None) -> list[RolePlace]: + """A role's place rows, those of one place text (a copy's, one position) + kept together, nearest to the agent first (`places.nearest_first`).""" + groups: dict[tuple[str, int, str], list[RolePlace]] = {} + for row in rows: + groups.setdefault((row.url, row.position, row.text), []).append(row) + entries = list(groups.values()) + order = places.nearest_first(near, [(g[0].text, [r.place_id for r in g]) for g in entries]) + return [row for i in order for row in entries[i]] + + +def _leads(rows: Sequence[RolePlace]) -> tuple[list[str], str, str]: + """`(distinct texts, lead text, lead url)` of places already in order: + the first place leads, and its url is a copy's link, '' for the role's + own place.""" + texts = list(dict.fromkeys(t for r in rows if (t := r.text.strip()))) + lead = next((r for r in rows if r.text.strip()), None) + return texts, lead.text.strip() if lead else "", lead.url if lead else "" + + def _like(text: str) -> str: """A `LIKE` pattern for a case-insensitive substring match, with SQLite's own wildcards escaped so a search for "50%" or "a_b" stays literal.""" @@ -918,7 +939,9 @@ class Store: role = ScoredRole.from_row(row) role.tags = self._tags_of(uid) role.copies = self._copies_of(uid) - role.places = self.role_places(uid) + role.places = _nearest_first(self.role_places(uid), self._nearness(agent_id)) + _, role.lead_place, role.lead_url = _leads(role.places) + role.lead_place = role.lead_place or role.place role.remote_where = self._remote_where([uid]).get(uid, "") role.judgement = self.judgement(agent_id, uid) role.cv_score = role.judgement.cv_score if role.judgement else None @@ -967,21 +990,28 @@ class Store: gaz = self.gazetteer() return {uid: gaz.area_label(ids) for uid, ids in found.items()} - def _place_lines(self, uids: Sequence[str]) -> dict[str, str]: - """`uid -> the line the feed shows` for a page of roles that have - more than one place (`dupes.place_line`); a role with one place or - none is absent and keeps `roles.place`.""" + def _nearness(self, agent_id: int | None) -> places.Nearness | None: + """How near a place is to the agent a page is viewed as, from its + gates; None without an agent, which keeps the host's order.""" + found = self.agent(agent_id) if agent_id is not None else None + return places.Nearness(self.gazetteer(), found.gates) if found is not None else None + + def _place_leads(self, uids: Sequence[str], + near: places.Nearness | None) -> dict[str, list[RolePlace]]: + """`uid -> its places nearest first` (`_nearest_first`) for a page + of roles; a role with no place is absent. One query over the page's + uids, so only the roles shown are ordered.""" if not uids: return {} - found: dict[str, list[str]] = {} + found: dict[str, list[RolePlace]] = {} for r in self._rows( - """SELECT uid, text FROM role_places + """SELECT uid, position, text, place_id, url FROM role_places WHERE uid IN (SELECT value FROM json_each(?)) ORDER BY uid, url != '', url, position, rowid""", (json.dumps(list(uids)),)): - found.setdefault(str(r["uid"]), []).append(str(r["text"])) - return {uid: dupes.place_line(texts) for uid, texts in found.items() - if len(set(texts)) > 1} + found.setdefault(str(r["uid"]), []).append(RolePlace( + position=r["position"], text=r["text"], place_id=r["place_id"], url=r["url"])) + return {uid: _nearest_first(rows, near) for uid, rows in found.items()} def rebuild_dupes(self) -> DupeCounts: """Recompute which open roles are copies of one posting, in one @@ -1270,12 +1300,15 @@ class Store: found.append(RoleRow.from_row(row)) by_uid = self._tags_by_uid([f.uid for f in found]) copies = self._copy_counts([f.uid for f in found]) - lines = self._place_lines([f.uid for f in found]) + leads = self._place_leads([f.uid for f in found], self._nearness(agent)) areas = self._remote_where([f.uid for f in found]) for f in found: f.tags = by_uid.get(f.uid, []) f.copies = copies.get(f.uid, 0) - f.place = lines.get(f.uid, f.place) + f.place_texts, f.lead_place, f.lead_url = _leads(leads.get(f.uid, [])) + if len(f.place_texts) > 1: + f.place = dupes.place_line(f.place_texts) + f.lead_place = f.lead_place or f.place f.remote_where = areas.get(f.uid, "") return found diff --git a/docs/decisions.md b/docs/decisions.md index 0bfa085..a08b3bb 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -118,7 +118,9 @@ What was decided for roledesk and the one reason for each. The longer reasoning **The agents set the reach of a pull.** `app/jobs/reach.py` computes it once per pull: the union of the enabled agents' `any` gates on country, town (as its country) and remote, or everything when one agent has none. Fetching has two filters, countries and role types, and a company has an override for each. A company set to **All countries** (`companies.all_countries`, migration 24) is read with the wide reach: the union of the enabled agents' `require` reach, Europe today, and the union of the `any` gates, so the override never reads less than the board would without it. An agent with no `require` gate on place makes the wide reach everywhere. For a company the owner cares about, a role in Freiburg may be open to remote, or worth asking about for Denmark, while Microsoft Germany is not wanted. The overrides are separate from favourite and liked, and global rather than per agent, because they decide what is fetched and fetching is shared. They matter only for a board of 300 roles or more on a host that filters, so a small company is affected once it grows past that. The Companies list states both halves per board, e.g. "follows agents: DK, SE, remote · all roles" or "all countries (Europe) · follows agents' terms: engineer, product, data", or why the board is read whole. Favourite (affinity 2) affects scoring only, where it skips the `any` gates; it used to widen the fetch to its agent's `require` reach, and migration 21 tagged every company that was a favourite of any agent so nothing read before is lost. That tag, first named All roles in `companies.all_roles`, also replaced a board's `{"reach": "all"}` param, which read everywhere: migration 21 tagged those companies and logged each such board, since no everywhere level is left. Migration 24 renamed the tag to All countries and gave its companies (Mistral AI and OpenAI on live) All roles as well, since the owner wanted their every role. A board's `params` override the agents' reach and All countries. Boards under 300 roles, and hosts with a one-country filter over more than 8 countries, are read whole. A board's size (`ats:size:`, with `ats:sizechecked:`) is set once, not by every read: `set_board` sizes a new board with `ats.board_size` (a stated total from one unfiltered page 1, else a whole read), a board with no size is sized in its batch before its reach is chosen, and Recheck on the Companies row, `app companies size ` and `--missing` size it again. Before, a new board had no size and was read whole on its first pull: Infineon read 999 roles worldwide and cut at page 100. A board whose reader cannot filter is sized by its whole read instead of a second request. On 2026-09-30 `--missing` sized 38 boards the old open-role fallback had sized, among them Bosch 4,830, Verisure 3,447, Microsoft 2,345 and NVIDIA at Workday's 2,000. For a Workday or Oracle site, which filters by its own location ids, the Companies line names a country the site does not list and a missing remote option ("follows agents: DK, remote · SE not on this site"), and "nothing asked" when it lists none, where it used to name what was meant to be asked. SmartRecruiters, Eightfold, SuccessFactors, Phenom, Workday and Oracle filter at the source; countries and remote go as separate series, because the hosts intersect two facets. A complete filtered read closes roles outside the filter, except roles with a vote or an application; a filtered read, or one from a host whose unknown slug answers empty, that lists under 10% of the last read with the same filter counts as failed. -**A role has a list of places, resolved against a gazetteer.** Readers kept one place of several, joined them, or the host posted a copy per place. A reader now returns every place and never multiplies a row. `places` (city, region, country, continent, with `place_includes` for Øresund across two countries) and `place_aliases` are seeded from the dictionaries that were in code; `role_places` links each place of a role, and an unknown text is listed by `unresolved_places` until an alias links it. The Place filter and the gates join over the tree, which halved their time and dropped the `EU` fallback. Copies of one posting per place, same host, company and title and texts at least 30% alike, fold into one role that keeps each copy's place and link; the rule applies to every host, since the 5,024 such roles came mostly from hosts that do not declare copies. A place text resolves by its own words (`Gazetteer.resolve`), never by the role's stated `country`, which names one country of a multi-place line: `San Francisco; London, UK` with `US` is in Europe through London. The gates and the Place filter read every place of a kept role, a folded copy's included, so OpenAI's San Francisco role with a Dublin copy meets a Europe gate although its `place` and `country` are San Francisco's; its row reads "San Francisco +1". On 2026-10-03 this kept 45 open roles in agent 2's feed that its own places alone would gate. +**A role has a list of places, resolved against a gazetteer.** Readers kept one place of several, joined them, or the host posted a copy per place. A reader now returns every place and never multiplies a row. `places` (city, region, country, continent, with `place_includes` for Øresund across two countries) and `place_aliases` are seeded from the dictionaries that were in code; `role_places` links each place of a role, and an unknown text is listed by `unresolved_places` until an alias links it. The Place filter and the gates join over the tree, which halved their time and dropped the `EU` fallback. Copies of one posting per place, same host, company and title and texts at least 30% alike, fold into one role that keeps each copy's place and link; the rule applies to every host, since the 5,024 such roles came mostly from hosts that do not declare copies. A place text resolves by its own words (`Gazetteer.resolve`), never by the role's stated `country`, which names one country of a multi-place line: `San Francisco; London, UK` with `US` is in Europe through London. The gates and the Place filter read every place of a kept role, a folded copy's included, so OpenAI's San Francisco role with a Dublin copy meets a Europe gate although its `place` and `country` are San Francisco's; its row read "San Francisco +1" until a row led with the place nearest the agent (below). On 2026-10-03 this kept 45 open roles in agent 2's feed that its own places alone would gate. + +**A role in several cities leads with the one nearest the viewing agent** (`places.Nearness`). Mistral's Forward Deployed role, kept as its Montréal posting with Abu Dhabi, New York and Paris copies, read "Montréal +3" and opened the Montréal page, while for the owner it is a Paris role. A role's places, its copies' included, are ordered in three tiers read off the agent's gates: a place that meets the `any` group (under one of its `country` or `place` values, or a remote part whose area is open to it through `remote_from`, or that meets the scope with a plain `remote` any gate), then a place inside the `require` scope on country, then the rest, a remote part with no stated area among them. The gazetteer holds no coordinates, so within a tier the home stands in for distance: the first value of the first `any` gate on country or place. A place in one of the group's own towns inside the home leads, then one elsewhere in the home, then the host's order; for agent 2 Copenhagen leads "Denmark", Billund and Stockholm. `GET /api/roles` and `GET /api/roles/{uid}` compute it for the agent the feed is viewed as and only for the roles served: the row carries `place_texts`, `lead_place` and `lead_url` (the leading copy's link, '' when the role's own place leads), and `place` is the lead and "+N" as before. The reader's Places list follows the order, and its posting links and the `o` key open `lead_url`. Without an agent the host's order stands. The gates and the dupes rule do not read the order. On the live copy of 2026-10-04 agent 2 sees the Mistral role as "Paris +3" linking the Paris copy, OpenAI's Applied AI Architect as "Dublin, Ireland +1", and 277 of the 279 kept roles with Copenhagen among several places lead with it; the other two lead with a joined line that names København. **A remote role has a remote area: the places a remote hire may live in** (migration 27, `role_remote`, `app/prep/area.py`). On 2026-09-30 the Place field listed "USA (Remote)", "Remote US" and "United States (Remote)" as three towns, and no filter said which of 3,177 remote roles someone in Denmark could take. Those lines came from a few whole-read Greenhouse and Ashby boards (Grafana Labs, Cloudflare, Monzo, Talkdesk), not from an ingest filter. Danish employers barely post remote roles (11 of 1,395 open Danish roles). The remote roles open to Denmark are the roughly 300 scoped to Europe or EMEA and the 210 scoped to anywhere. The area is the first of these that names a place: the model's `remote_where`, the parts of the place texts that say remote ("Germany (Remote)", "Home Based - EMEA"; "Worldwide" or "Global" in such a part is Anywhere), and the role's resolved places lifted to their countries. "Remote" alone names none, so such a role is remote with no stated area. Anywhere is a standalone place with no aliases and nothing under it, so the Place filter and `under` never reach every role through it, and it never counts as a place for a country gate's empty check. Remote lines are no longer suggested as towns. diff --git a/tests/test_dupes_storage.py b/tests/test_dupes_storage.py index 3af74ab..73feb67 100644 --- a/tests/test_dupes_storage.py +++ b/tests/test_dupes_storage.py @@ -301,3 +301,36 @@ def test_switching_a_board_off_shows_its_roles_copies_at_once(store: Store) -> N set_fetch(store, sub.id, False) assert dup_of(store) == {board.uid: None, search.uid: None} + + +def test_a_role_in_several_cities_leads_with_the_one_nearest_the_agent(store: Store) -> None: + """Mistral posts one role per city. For an agent kept to Europe the kept + Montréal role leads with Paris, in the row and the reader, and opens the + Paris copy; without an agent the host's order stands.""" + from app import prep + from app.models import Gate + items = [role(c, company="Mistral AI", place=p, board="ashby") for c, p in ( + ("mtl", "Montréal"), ("nyc", "New York"), ("par", "Paris"))] + for item in items: + store.upsert_role(item) + prep.run(store) + kept = kept_of(store, *items) + agent = store.save_agent("A", gates=[ + Gate(field="country", values=["FR", "DK", "EU"], mode="require"), + Gate(field="country", values=["DK"], mode="any")]) + + got = store.role(kept, agent.id) + assert got is not None + own = next(p.text for p in got.places if not p.url) + assert own != "Paris" + assert (got.lead_place, got.lead_url) == ("Paris", "https://jobs.example/par") + assert got.places[0].text == "Paris" + (row,) = store.roles_page(agent=agent.id, by_score=False) + assert (row.place, row.lead_place, row.lead_url) == ("Paris +2", "Paris", got.lead_url) + assert row.place_texts[0] == "Paris" and len(row.place_texts) == 3 + + plain = store.role(kept) + assert plain is not None + assert plain.lead_place == own and plain.lead_url == "" + (row,) = store.roles_page(by_score=False) + assert row.place == f"{own} +2" and row.lead_url == "" diff --git a/tests/test_gazetteer.py b/tests/test_gazetteer.py index 10b3d5c..961d277 100644 --- a/tests/test_gazetteer.py +++ b/tests/test_gazetteer.py @@ -429,3 +429,62 @@ def test_the_gazetteer_finds_what_the_words_found( by_places = uids(store, token) lost = {lines[uid][0] for uid in by_words - by_places} assert lost <= NOT_FOUND_ANY_MORE.get(token, set()), (token, lost) + + +# ------------------------------------------------------------------ nearness + +#: Agent 2's place gates: Europe as its scope, Denmark, Sweden, remote and +#: the Øresund towns as its `any` group. +EUROPE_DK_SE = [ + Gate(field="country", values=["DK", "FR", "DE", "IE", "SE", "GB", "EU"], mode="require"), + Gate(field="country", values=["DK", "SE"], mode="any"), + Gate(field="remote", values=["remote"], mode="any"), + Gate(field="place", values=["Malmö", "copenhagen"], mode="any"), + Gate(field="remote", values=["hybrid"], mode="prefer", weight=0.05), +] + + +def nearest(gates: list[Gate] | None, *texts: str) -> list[str]: + gaz = gazetteer() + near = places.Nearness(gaz, gates) if gates is not None else None + entries = [(t, list(gaz.resolve(t)) or [None]) for t in texts] + return [texts[i] for i in places.nearest_first(near, entries)] + + +def test_a_place_in_the_scope_leads_the_ones_outside_it() -> None: + """Mistral's role in Montréal, Abu Dhabi, New York and Paris is a Paris + role for an agent kept to Europe; the others keep the host's order.""" + assert nearest(EUROPE_DK_SE, "Montréal", "Abu Dhabi", "New York", "Paris") == [ + "Paris", "Montréal", "Abu Dhabi", "New York"] + assert nearest(EUROPE_DK_SE, "San Francisco", "Dublin, Ireland") == [ + "Dublin, Ireland", "San Francisco"] + + +def test_the_any_group_leads_the_scope_and_the_home_leads_the_group() -> None: + """Copenhagen, Stockholm and Malmö meet the `any` group, London and + Lisbon only the scope; Denmark, the group's first value, is the home, so + a Danish place leads the group.""" + assert nearest(EUROPE_DK_SE, "London", "Stockholm, Sweden", "Lisbon", "Copenhagen") == [ + "Copenhagen", "Stockholm, Sweden", "London", "Lisbon"] + assert nearest(EUROPE_DK_SE, "London", "Malmö", "Copenhagen, Denmark") == [ + "Copenhagen, Denmark", "Malmö", "London"] + + +def test_a_town_of_the_group_in_the_home_leads_the_rest_of_the_home() -> None: + """Copenhagen is one of the group's towns and in Denmark, so it leads + "Denmark" and Billund, which lead Stockholm.""" + assert nearest(EUROPE_DK_SE, "Stockholm", "Denmark", "Billund", "Copenhagen") == [ + "Copenhagen", "Denmark", "Billund", "Stockholm"] + + +def test_a_remote_place_is_near_by_its_area() -> None: + """A remote area within the scope meets the plain remote gate; one outside + it, and "Remote" with no area, come last.""" + assert nearest(EUROPE_DK_SE, "Remote US", "Remote", "Berlin", "Remote - Europe") == [ + "Remote - Europe", "Berlin", "Remote US", "Remote"] + + +def test_without_an_agent_or_its_gates_the_hosts_order_stands() -> None: + texts = ("Montréal", "New York", "Paris") + assert nearest(None, *texts) == list(texts) + assert nearest([], *texts) == list(texts) diff --git a/tests/test_web.py b/tests/test_web.py index c46a85d..aa0a1de 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1231,7 +1231,7 @@ def test_list_roles_serves_the_row_projection_and_not_the_big_columns( "seniority", "salary_min", "salary_max", "currency", "salary_text", "salary_state", "original_url", "posting_url", "posted_at", "score", "gated_reason", "prompt", "vote", "voted_at", "read_at", "tags", "excerpt", "copies", "remote_where", "deadline", - "lang_flag", "lang_note", "cv_score", "cv_fit", + "lang_flag", "lang_note", "cv_score", "cv_fit", "place_texts", "lead_place", "lead_url", } assert row["uid"] == uid assert row["company"] == "Acme" and row["place"] == "Berlin, Germany"