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 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-10-04 08:15:26 +02:00
1 parent ac4cdb8a48
commit fb24c8e4c9
8 files changed
+277 -29

No files matched your search

+59
View File
@@ -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)