The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(usage-window): do not /retry a turn that ran while the limit-offer switch resolved
Tekniumd43d16c98880
diff --git a/gateway/slash_commands_model.py b/gateway/slash_commands_model.pyindex edda8974297..8f9479431e3 100644--- a/gateway/slash_commands_model.py+++ b/gateway/slash_commands_model.py@@ -662,7 +662,8 @@ class GatewayModelCommandsMixin:if generation is not None and self._usage_limit_offer_is_stale(session_key, generation):# A newer turn ran since the refusal: /retry would re-run that one, not the refused one.return t("platform.discord.picker.choice_expired")- return await self._accept_usage_limit_switch(event, value)+ return await self._accept_usage_limit_switch(+ event, value, offer=None if generation is None else (session_key, generation))choices = [{"value": str(s["model"]),"label": t("gateway.usage_limit.switch_choice", model=str(s.get("label") or s["model"]),@@ -685,8 +686,10 @@ class GatewayModelCommandsMixin:except (AttributeError, KeyError, TypeError, ValueError):return False- async def _accept_usage_limit_switch(self, event: MessageEvent, model_id: str) -> str:- """The tap: switch this session to *model_id* on Nous, then re-send the refused message."""+ async def _accept_usage_limit_switch(self, event: MessageEvent, model_id: str, *,+ offer: Optional[tuple[str, int]] = None) -> str:+ """The tap: switch this session to *model_id* on Nous, then re-send the refused message.+ ``offer`` is the refused turn's ``(session_key, run_generation)``."""from gateway.run import _hermes_homesource = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)session_key = self._session_key_for_source(source)@@ -717,13 +720,15 @@ class GatewayModelCommandsMixin:# Only after the switch landed (an error reply leaves the override unchanged). Exactly a typed /retry from the same sender (rewinds the refused turn, runs the message# again, and the adapter delivers the reply). Called directly, the re-run's reply would be# returned to nobody.- self._track_background_task(self._dispatch_usage_limit_retry(event))+ self._track_background_task(self._dispatch_usage_limit_retry(event, offer))return reply- async def _dispatch_usage_limit_retry(self, event: MessageEvent) -> None:+ async def _dispatch_usage_limit_retry(self, event: MessageEvent, offer: Optional[tuple[str, int]] = None) -> None:from gateway.platforms.event import MessageTypeadapter = self._delivery_adapter_for(event.source)- if adapter is None:+ # Re-checked here, with no await before the /retry: the switch awaited model resolution, and a+ # turn that ran meanwhile is what /retry would rewind. The switch itself stands.+ if adapter is None or (offer is not None and self._usage_limit_offer_is_stale(*offer)):returntry:await adapter.handle_message(MessageEvent(diff --git a/tests/gateway/test_usage_limit_offer.py b/tests/gateway/test_usage_limit_offer.pyindex 74a9faffded..4b8b52898ff 100644--- a/tests/gateway/test_usage_limit_offer.py+++ b/tests/gateway/test_usage_limit_offer.py@@ -223,6 +223,44 @@ async def test_a_tap_after_a_newer_turn_changes_nothing():assert await on_choice("c1", "example/model-free:free") == "switched"+async def test_a_turn_that_ran_during_the_switch_is_not_retried(monkeypatch):+ """The tap was current, but a newer turn ran while the switch awaited model resolution: the+ /retry would rewind that turn, so it is not sent."""+ from gateway import slash_commands_model as scm++ adapter = _ButtonAdapter()+ adapter.handle_message = AsyncMock()+ runner = _runner(adapter)+ runner.config = SimpleNamespace(multiplex_profiles=False)+ runner._session_model_overrides = {}+ runner._normalize_source_for_session_key = lambda source: source+ state = runner._session_state("chat:c1")+ state.persistent.run_generation = 7++ async def _perform(ctx, target, provider, source):+ state.persistent.run_generation = 8 # another message's turn ran during this await+ return SimpleNamespace(new_model=target), None++ async def _commit(result, ctx, *, source, picker):+ runner._session_model_overrides[ctx.session_key] = {"model": result.new_model, "provider": "nous"}+ return "✓ Model switched to Model Free"++ tasks = []+ runner._perform_model_switch = _perform+ runner._commit_model_switch = _commit+ runner._track_background_task = lambda coro: tasks.append(coro)+ monkeypatch.setattr(scm._ModelSwitchContext, "read_config", lambda self: None)+ monkeypatch.setattr(scm._ModelSwitchContext, "apply_override", lambda self, override: None)++ await runner._offer_usage_limit_switch(_event(), LIMIT, 7)+ on_choice = runner._try_send_choice_picker.await_args.kwargs["on_choice_selected"]+ assert (await on_choice("c1", "example/model-free:free")).startswith("✓")+ for task in tasks:+ await task+ adapter.handle_message.assert_not_awaited()++def test_a_messaging_refusal_points_at_the_portal_not_topup():from agent.plan_limits import portal_wordingassert portal_wording("You've hit your daily limit · resets 15:56 · turn on Keep going in /topup") == (