Offer only the remote tokens within the asking agent's scope, so an agent kept to Europe is not offered Remote · United States

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-30 16:27:09 +02:00
1 parent 5c3c187dca
commit 9e2631ffe3
3 files changed
+44 -9

No files matched your search

+14 -6
View File
@@ -738,7 +738,8 @@ def _asks_remote(text: str) -> tuple[bool, str]:
def _remote_suggestions(text: str, gaz: Gazetteer, 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 """`remote:` tokens for typed text, each with the open remote roles it
finds (`Gazetteer.overlap`, which holds Anywhere), none that finds finds (`Gazetteer.overlap`, which holds Anywhere), none that finds
nothing, widest first and then by open roles. A place is offered when it 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; (`held`). "remote" alone offers every continent and country held;
"remote den" or "den" the places offered whose name it starts, a region "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 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) asked, text = _asks_remote(text)
held: set[int] = set() held: set[int] = set()
for ids, _ in areas: 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))} if (place.kind in _WIDE or p in held) and _prefixes(text, gaz.names(p))}
else: else:
candidates = set() 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]] = {} found: dict[str, tuple[int, Suggestion]] = {}
for place_id in candidates - {gaz.anywhere}: for place_id in candidates - {gaz.anywhere}:
token = parse(f"remote:{gaz.token(place_id)}") 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, def suggest(q: str, places: Iterable[tuple[str, str, int]], limit: int = 12,
remote: Iterable[tuple[Iterable[int], int]] = (), remote: Iterable[tuple[Iterable[int], int]] = (),
gaz: Gazetteer | None = None, 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 """Tokens for the text typed so far. `places` is `(place, country, open
roles)`, `remote` is `(area, open roles)` for the open remote roles, the 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 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 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 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 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) text = fold(q)
if not text: if not text:
return [] 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() else _TOWN_LABELS.get(group) or group.title()
towns.append(Suggestion(f"place:{fold(shown)}", shown, "place", total)) towns.append(Suggestion(f"place:{fold(shown)}", shown, "place", total))
remotes = _remote_suggestions(text, gaz, [(tuple(ids), int(n)) for ids, n in remote]) \ remotes = _remote_suggestions(text, gaz, [(tuple(ids), int(n)) for ids, n in remote],
if gaz is not None else [] scope) if gaz is not None else []
first = [] first = []
if remote_any is not None and _asks_remote(text)[0] and (count := remote_any()): if remote_any is not None and _asks_remote(text)[0] and (count := remote_any()):
first.append(Suggestion("remote:any", "Remote", "remote", count)) first.append(Suggestion("remote:any", "Remote", "remote", count))
+9 -3
View File
@@ -345,15 +345,21 @@ def suggest_places(
regions, then towns, each by open roles (`places.suggest`, over every 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 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 `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 agent when absent, as the feed counts them, and the other remote tokens
nothing.""" are kept to that agent's scope, its `require` gates on country. An empty
`q` suggests nothing."""
if not places.fold(q): if not places.fold(q):
return [] return []
viewer = _agent_id(store, agent) 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) return [PlaceSuggestion(token=s.token, label=s.label, kind=s.kind, count=s.count)
for s in places.suggest( for s in places.suggest(
q, store.open_places(), limit, remote=store.open_remote_areas(), 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, remote_any=lambda: store.count_roles(places=["remote:any"], agent=viewer,
by_score=False))] by_score=False))]
+21
View File
@@ -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"] 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( def test_a_suggested_remote_token_finds_the_roles_it_counted(
store: Store, roles: dict[str, str]) -> None: store: Store, roles: dict[str, str]) -> None:
gaz = store.gazetteer() 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 "count": 1} in found
other = client.get("/api/suggest/places", params={"q": "remote", "agent": agent.id}) other = client.get("/api/suggest/places", params={"q": "remote", "agent": agent.id})
assert other.json()[0]["count"] == 2 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=[ labels = client.get("/api/places/labels", params=[
("tokens", "remote:country:DK"), ("tokens", "remote:anywhere"), ("tokens", "remote:country:DK"), ("tokens", "remote:anywhere"),
("tokens", "remote:country:ZZ")]).json() ("tokens", "remote:country:ZZ")]).json()