Skip to content

The agent that grows with you

fix(nous): only rescue an empty list where emptiness means "filtered out"

Mariano Nicolinida3c2435e2ad
diff --git a/docs/nous-org-model-policy.md b/docs/nous-org-model-policy.md
index 7b4027ba72e..8b1cbc4573a 100644
--- a/docs/nous-org-model-policy.md
+++ b/docs/nous-org-model-policy.md
@@ -143,12 +143,17 @@ then empties the picker — strictly worse than an unfiltered list, because the
models the org may use are the ones dropped. Fall back to the reachable set
itself in exactly that case.
-Only when the intersection is empty, and only when the set is small enough to
-be an allowlist rather than a whole catalog. A jurisdiction or provider policy
-narrows the catalog without emptying the curated overlap; appending its
-remainder pushes non-curated alphabetical ids into a picker that shows a
-curated order on purpose. A size cap alone does not catch this — a region
-filter can leave few enough models to pass it.
+Only when the intersection is empty, only when the set is small enough to be
+an allowlist rather than a whole catalog, and **only for the list a user picks
+from**. Callers opt in per list. Any list whose emptiness carries meaning must
+not get the rescue: a paid-tier user's unavailable list is legitimately empty,
+and rescuing it reads that as "nothing survived" and fills the picker's
+unavailable block with the entire reachable set.
+
+A size cap alone does not make this safe — a jurisdiction filter can leave few
+enough models to pass it — and neither does gating on an empty intersection,
+because an intentionally empty input is indistinguishable from a fully filtered
+one. The opt-in is what separates them.
**Do not** narrow a list on evidence that cannot support it.
`nous_policy_allowed_ids()` returns `None` — meaning "leave the list alone" —
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py
index eebbc5609c9..c6a7330e29a 100644
--- a/hermes_cli/auth.py
+++ b/hermes_cli/auth.py
@@ -9432,7 +9432,9 @@ def _login_nous(args, pconfig: ProviderConfig) -> None:
# unauthenticated, so neither knows what the org may reach.
# Narrow both lists to the policy before they are shown.
_policy_allowed = nous_policy_allowed_ids()
- model_ids = restrict_to_nous_policy(model_ids, _policy_allowed)
+ model_ids = restrict_to_nous_policy(
+ model_ids, _policy_allowed, rescue_empty=True,
+ )
unavailable_models = restrict_to_nous_policy(
unavailable_models, _policy_allowed,
)
diff --git a/hermes_cli/model_setup_flows.py b/hermes_cli/model_setup_flows.py
index 94896921591..c4b9dedfd74 100644
--- a/hermes_cli/model_setup_flows.py
+++ b/hermes_cli/model_setup_flows.py
@@ -565,7 +565,9 @@ def _model_flow_nous(config, current_model="", args=None):
from hermes_cli.models import nous_policy_allowed_ids, restrict_to_nous_policy
_policy_allowed = nous_policy_allowed_ids()
- model_ids = restrict_to_nous_policy(model_ids, _policy_allowed)
+ model_ids = restrict_to_nous_policy(
+ model_ids, _policy_allowed, rescue_empty=True,
+ )
unavailable_models = restrict_to_nous_policy(unavailable_models, _policy_allowed)
if not model_ids and not unavailable_models:
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index 3516cdfe24b..fe18cb93a61 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -3111,7 +3111,9 @@ def list_authenticated_providers(
restrict_to_nous_policy as _nous_restrict,
)
- model_ids = _nous_restrict(model_ids, _nous_policy())
+ model_ids = _nous_restrict(
+ model_ids, _nous_policy(), rescue_empty=True,
+ )
except Exception:
pass
else:
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index e7754fa196c..6662b9d3c80 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -2657,7 +2657,10 @@ _NOUS_POLICY_APPEND_MAX = 64
def restrict_to_nous_policy(
- model_ids: list[str], allowed: Optional[set[str]]
+ model_ids: list[str],
+ allowed: Optional[set[str]],
+ *,
+ rescue_empty: bool = False,
) -> list[str]:
"""*model_ids* narrowed to *allowed*, preserving the caller's order.
@@ -2681,14 +2684,17 @@ def restrict_to_nous_policy(
# An allowlist can admit only models the curated manifest has never heard
# of, leaving nothing to intersect and an empty picker — strictly worse than
# the unfiltered list, because the models the org may actually use are the
- # ones dropped. Fall back to the reachable set itself in exactly that case.
+ # ones dropped. *rescue_empty* falls back to the reachable set in exactly
+ # that case, and callers opt in per list: it is meaningful for the list a
+ # user picks from, and wrong for any list whose emptiness carries meaning.
+ # An unavailable/gated list is legitimately empty, and rescuing it would
+ # read that as "nothing survived" and fill it with the whole reachable set.
#
- # Only when the intersection is empty. A jurisdiction or provider policy
- # narrows the catalog without emptying the curated overlap, and appending
- # its remainder would push non-curated alphabetical ids into a picker that
- # shows a curated order on purpose. Anything omitted is still reachable
- # through the picker's custom-model entry.
- if not kept and len(allowed) <= _NOUS_POLICY_APPEND_MAX:
+ # Bounded, because a jurisdiction or provider policy narrows the catalog
+ # without shrinking it to an allowlist, and a large alphabetical dump buries
+ # the curated order the pickers show on purpose. Anything omitted stays
+ # reachable through the picker's custom-model entry.
+ if rescue_empty and not kept and len(allowed) <= _NOUS_POLICY_APPEND_MAX:
return sorted(allowed)
return kept
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 41577df7d4d..97c5dfe9e2a 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -7520,7 +7520,9 @@ def get_recommended_default_model(provider: str = ""):
# Neither the curated list nor the Portal's recommendations know
# what the org may reach, and this endpoint picks the model a user
# lands on without choosing it.
- model_ids = restrict_to_nous_policy(model_ids, nous_policy_allowed_ids())
+ model_ids = restrict_to_nous_policy(
+ model_ids, nous_policy_allowed_ids(), rescue_empty=True,
+ )
model = pick_silent_default_model(model_ids, provider="nous")
return {"provider": "nous", "model": model, "free_tier": bool(free_tier)}
diff --git a/tests/hermes_cli/test_nous_policy_filter.py b/tests/hermes_cli/test_nous_policy_filter.py
index b91fe101489..b488ca23672 100644
--- a/tests/hermes_cli/test_nous_policy_filter.py
+++ b/tests/hermes_cli/test_nous_policy_filter.py
@@ -63,11 +63,7 @@ class TestRestrictToNousPolicy:
) == ["vendor/model:free"]
def test_drops_a_free_sibling_whose_base_is_blocked(self):
- """The blocked sibling goes; the model the org may actually use takes
- its place rather than leaving the picker empty."""
- assert restrict_to_nous_policy(["vendor/model:free"], {"other/model"}) == [
- "other/model"
- ]
+ assert restrict_to_nous_policy(["vendor/model:free"], {"other/model"}) == []
class TestNousPolicyAllowedIds:
@@ -200,7 +196,7 @@ class TestAllowlistOutsideTheCuratedList:
def test_surfaces_an_allowed_model_the_curated_list_lacks(self):
assert restrict_to_nous_policy(
- ["vendor/a", "vendor/b"], {"amazon/nova-2-lite-v1"}
+ ["vendor/a", "vendor/b"], {"amazon/nova-2-lite-v1"}, rescue_empty=True
) == ["amazon/nova-2-lite-v1"]
def test_does_not_append_when_the_curated_overlap_is_non_empty(self):
@@ -208,7 +204,9 @@ class TestAllowlistOutsideTheCuratedList:
emptying the curated overlap. Appending its remainder would push
non-curated alphabetical ids into a deliberately curated order."""
kept = restrict_to_nous_policy(
- ["z/curated", "a/curated"], {"z/curated", "a/curated", "new/model"}
+ ["z/curated", "a/curated"],
+ {"z/curated", "a/curated", "new/model"},
+ rescue_empty=True,
)
assert kept == ["z/curated", "a/curated"]
@@ -217,19 +215,46 @@ class TestAllowlistOutsideTheCuratedList:
the size cap, so a size-only guard let it append."""
curated = ["vendor/one", "vendor/two", "vendor/three"]
reachable = {"vendor/one", "vendor/two"} | {f"cn/model-{i}" for i in range(20)}
- assert restrict_to_nous_policy(curated, reachable) == [
+ assert restrict_to_nous_policy(curated, reachable, rescue_empty=True) == [
"vendor/one",
"vendor/two",
]
def test_does_not_append_a_free_sibling_already_covered(self):
- assert restrict_to_nous_policy(["vendor/m:free"], {"vendor/m"}) == [
- "vendor/m:free"
- ]
+ assert restrict_to_nous_policy(
+ ["vendor/m:free"], {"vendor/m"}, rescue_empty=True
+ ) == ["vendor/m:free"]
def test_a_provider_only_policy_does_not_bury_the_curated_order(self):
"""Such a policy leaves the whole catalog reachable; appending it would
drop hundreds of alphabetical ids into the picker."""
curated = ["vendor/one", "vendor/two"]
catalog = {f"vendor/model-{i}" for i in range(300)} | set(curated)
- assert restrict_to_nous_policy(curated, catalog) == curated
+ assert restrict_to_nous_policy(curated, catalog, rescue_empty=True) == curated
+
+
+class TestRescueIsOptIn:
+ """The empty-intersection rescue is meaningful only for the list a user
+ picks from. Any list whose emptiness carries meaning must not get it."""
+
+ def test_no_rescue_by_default(self):
+ assert restrict_to_nous_policy([], {"a/one", "b/two"}) == []
+
+ def test_rescue_only_when_asked(self):
+ assert restrict_to_nous_policy(
+ [], {"a/one"}, rescue_empty=True
+ ) == ["a/one"]
+
+ def test_an_already_empty_unavailable_list_is_never_filled(self):
+ """Regression: a paid-tier user has no gated models, so the
+ unavailable list is legitimately empty. Rescuing it read that as
+ "nothing survived" and pushed the whole reachable set into the picker's
+ unavailable block."""
+ reachable = {f"cn/model-{i}" for i in range(42)}
+ assert restrict_to_nous_policy([], reachable) == []
+
+ def test_rescue_does_not_resurrect_a_fully_blocked_list(self):
+ """A list whose every entry was blocked is a real filter result, not a
+ signal to show something else — unless the caller asked for the
+ rescue, which only the selectable list does."""
+ assert restrict_to_nous_policy(["x/blocked"], {"y/allowed"}) == []