From 9e2631ffe3bee59e8e2a6e75027280d01ca1e87f Mon Sep 17 00:00:00 2001 From: Wolf Date: Wed, 30 Sep 2026 16:27:09 +0200 Subject: [PATCH] =?UTF-8?q?Offer=20only=20the=20remote=20tokens=20within?= =?UTF-8?q?=20the=20asking=20agent's=20scope,=20so=20an=20agent=20kept=20t?= =?UTF-8?q?o=20Europe=20is=20not=20offered=20Remote=20=C2=B7=20United=20St?= =?UTF-8?q?ates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- app/places.py | 20 ++++++++++++++------ app/web/api.py | 12 +++++++++--- tests/test_remote.py | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/places.py b/app/places.py index 6f9fe79..76f5a94 100644 --- a/app/places.py +++ b/app/places.py @@ -738,7 +738,8 @@ def _asks_remote(text: str) -> tuple[bool, str]: def _remote_suggestions(text: str, gaz: Gazetteer, - areas: list[tuple[tuple[int, ...], int]]) -> list[Suggestion]: + areas: list[tuple[tuple[int, ...], int]], + scope: Iterable[int] = ()) -> list[Suggestion]: """`remote:` tokens for typed text, each with the open remote roles it finds (`Gazetteer.overlap`, which holds Anywhere), none that finds nothing, widest first and then by open roles. A place is offered when it @@ -746,7 +747,10 @@ def _remote_suggestions(text: str, gaz: Gazetteer, (`held`). "remote" alone offers every continent and country held; "remote den" or "den" the places offered whose name it starts, a region or a town only when its token names it alone. `remote:anywhere` is a - token and no suggestion: every remote chip holds Anywhere already.""" + token and no suggestion: every remote chip holds Anywhere already. With + a `scope`, the places of the asking agent's `require` gates on country, + only a place in the scope or one the scope lies in is offered, so an + agent kept to Europe is not offered Remote · United States.""" asked, text = _asks_remote(text) held: set[int] = set() for ids, _ in areas: @@ -759,6 +763,8 @@ def _remote_suggestions(text: str, gaz: Gazetteer, if (place.kind in _WIDE or p in held) and _prefixes(text, gaz.names(p))} else: candidates = set() + if scope := set(scope): + candidates &= gaz.under(scope) | {a for p in scope for a in gaz.ancestors(p)} found: dict[str, tuple[int, Suggestion]] = {} for place_id in candidates - {gaz.anywhere}: token = parse(f"remote:{gaz.token(place_id)}") @@ -776,7 +782,8 @@ def _remote_suggestions(text: str, gaz: Gazetteer, def suggest(q: str, places: Iterable[tuple[str, str, int]], limit: int = 12, remote: Iterable[tuple[Iterable[int], int]] = (), gaz: Gazetteer | None = None, - remote_any: Callable[[], int] | None = None) -> list[Suggestion]: + remote_any: Callable[[], int] | None = None, + scope: Iterable[int] = ()) -> list[Suggestion]: """Tokens for the text typed so far. `places` is `(place, country, open roles)`, `remote` is `(area, open roles)` for the open remote roles, the area as place ids of `gaz`, and `remote_any` counts the open roles @@ -791,7 +798,8 @@ def suggest(q: str, places: Iterable[tuple[str, str, int]], limit: int = 12, a line, grouped by fold and by alias (`Copenhagen` and `København` are one town, shown as its commonest spelling). Countries, regions and towns with no open role are left out: a chip that finds nothing helps nobody. Remote - tokens are `_remote_suggestions`, offered only with `gaz`.""" + tokens are `_remote_suggestions`, offered only with `gaz` and kept to + `scope` when one is given.""" text = fold(q) if not text: return [] @@ -837,8 +845,8 @@ def suggest(q: str, places: Iterable[tuple[str, str, int]], limit: int = 12, else _TOWN_LABELS.get(group) or group.title() towns.append(Suggestion(f"place:{fold(shown)}", shown, "place", total)) - remotes = _remote_suggestions(text, gaz, [(tuple(ids), int(n)) for ids, n in remote]) \ - if gaz is not None else [] + remotes = _remote_suggestions(text, gaz, [(tuple(ids), int(n)) for ids, n in remote], + scope) if gaz is not None else [] first = [] if remote_any is not None and _asks_remote(text)[0] and (count := remote_any()): first.append(Suggestion("remote:any", "Remote", "remote", count)) diff --git a/app/web/api.py b/app/web/api.py index 91dce47..c58f0f6 100644 --- a/app/web/api.py +++ b/app/web/api.py @@ -345,15 +345,21 @@ def suggest_places( regions, then towns, each by open roles (`places.suggest`, over every open role's place line and every open remote role's area). The count of `remote:any` is the remote roles in the scope of `agent`, the default - agent when absent, as the feed counts them. An empty `q` suggests - nothing.""" + agent when absent, as the feed counts them, and the other remote tokens + are kept to that agent's scope, its `require` gates on country. An empty + `q` suggests nothing.""" if not places.fold(q): return [] viewer = _agent_id(store, agent) + gaz = store.gazetteer() + found = store.agent(viewer) if viewer is not None else None + scope = {p for g in (found.gates if found is not None else ()) + if g.field == "country" and g.mode == "require" + for v in g.values for p in gaz.lookup(f"country:{v}") or ()} return [PlaceSuggestion(token=s.token, label=s.label, kind=s.kind, count=s.count) for s in places.suggest( q, store.open_places(), limit, remote=store.open_remote_areas(), - gaz=store.gazetteer(), + gaz=gaz, scope=scope, remote_any=lambda: store.count_roles(places=["remote:any"], agent=viewer, by_score=False))] diff --git a/tests/test_remote.py b/tests/test_remote.py index 32ba4a6..5cb719a 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -425,6 +425,25 @@ def test_remote_is_suggested_after_remote_any_and_before_the_regions( assert shown("re") == [t for t in shown("re") if t[1] != "remote:any"] +def test_an_agent_kept_to_europe_is_offered_only_remote_tokens_in_europe( + store: Store, roles: dict[str, str]) -> None: + gaz = store.gazetteer() + europe = {gaz.by_key[("continent", "europe")]} + denmark = {gaz.by_key[("country", "DK")]} + + def tokens(q: str, scope: set[int]) -> list[str]: + return [s.token for s in suggest(q, store.open_places(), 50, + remote=store.open_remote_areas(), gaz=gaz, scope=scope) + if s.kind == "remote"] + + assert "remote:country:US" in tokens("remote", set()) + assert "remote:country:US" not in tokens("remote", europe) + assert "remote:country:EU" in tokens("remote", europe) + assert "remote:country:DK" in tokens("remote den", europe) + # A scope of one country still offers the wider places it lies in. + assert tokens("remote", denmark) == ["remote:country:EU", "remote:country:DK"] + + def test_a_suggested_remote_token_finds_the_roles_it_counted( store: Store, roles: dict[str, str]) -> None: gaz = store.gazetteer() @@ -457,6 +476,8 @@ def test_the_api_suggests_labels_and_filters_the_remote_tokens(tmp_path: Path) - "count": 1} in found other = client.get("/api/suggest/places", params={"q": "remote", "agent": agent.id}) assert other.json()[0]["count"] == 2 + tokens = [s["token"] for s in other.json()] + assert "remote:country:EU" in tokens and "remote:country:US" not in tokens labels = client.get("/api/places/labels", params=[ ("tokens", "remote:country:DK"), ("tokens", "remote:anywhere"), ("tokens", "remote:country:ZZ")]).json()