Skip to content

The agent that grows with you

fix(hosted): a hosted clarify refusal releases the member at once

Teknium3134cfe1d2e3
diff --git a/gateway/session_local.py b/gateway/session_local.py
index f96a885e19b..06f768d816b 100644
--- a/gateway/session_local.py
+++ b/gateway/session_local.py
@@ -69,10 +69,14 @@ class LocalSessionAdapter(BasePlatformAdapter):
async def send_clarify(self, chat_id, question, choices, clarify_id, session_key, metadata=None):
policy = self.policies.get(chat_id)
if policy is not None and policy.source == 'bot_room':
- # Hosted controls expose approvals only; acknowledging this question
- # would park the member on an answer nobody can submit.
- return SendResult(success=False, error='Clarification is unavailable in hosted rooms',
- error_kind='forbidden')
+ # Hosted controls expose approvals only; acknowledging this question would park the
+ # member on an answer nobody can submit. A structured decline releases the wait at
+ # once and skips the plain-text re-ask (an ordinary failure would fall back to a
+ # send() that ACKs, and the member would wait the whole clarify timeout).
+ from gateway.relay.egress import EGRESS_DECLINE_CODE
+ error = 'Clarification is unavailable in hosted rooms'
+ return SendResult(success=False, error=error, error_kind='forbidden',
+ raw_response={'success': False, 'code': EGRESS_DECLINE_CODE, 'error': error})
return SendResult(success=True, message_id=clarify_id)
diff --git a/tests/gateway/test_hosted_clarification_refusal.py b/tests/gateway/test_hosted_clarification_refusal.py
index f9f708374fe..819f27ec4f5 100644
--- a/tests/gateway/test_hosted_clarification_refusal.py
+++ b/tests/gateway/test_hosted_clarification_refusal.py
@@ -1,16 +1,53 @@
-"""Persisted hosted policies cannot acknowledge a question without a reply channel."""
+"""Persisted hosted policies decline a question without a reply channel."""
+import asyncio
+import threading
+import time
from dataclasses import replace
import pytest
+from tools import clarify_gateway as clarify_mod
-async def test_hosted_clarify_is_declined_before_waiting(owner, tmp_path):
+
+def ask(owner, tmp_path, monkeypatch):
+ """Drive the real delivery chain (native send, text fallback, wait) for one choice question."""
+ from gateway.run_turn_runner_clarify_delivery import _clarify_send_then_wait, text_fallback_coro
from gateway.session_local import LocalSessionAdapter
from gateway.session_policy import build_policy
- adapter = LocalSessionAdapter(owner)
- adapter.policies['room'] = replace(build_policy({'source': 'cli', 'cwd': str(tmp_path), 'model': 'm'}, {}),
- source='bot_room', platform='bot_room')
- result = await adapter.send_clarify(chat_id='room', question='Which?', choices=['one', 'two'],
- clarify_id='question', session_key='route')
- assert result.success is False and result.error_kind == 'forbidden'
+
+ loop = asyncio.new_event_loop()
+ thread = threading.Thread(target=loop.run_forever, daemon=True)
+ thread.start()
+ monkeypatch.setattr(clarify_mod, 'get_clarify_timeout', lambda: 10)
+ schedule = lambda coro: asyncio.run_coroutine_threadsafe(coro, loop)
+
+ def run(source, on_sent=None):
+ adapter = LocalSessionAdapter(owner)
+ policy = build_policy({'source': 'cli', 'cwd': str(tmp_path), 'model': 'm'}, {})
+ adapter.policies['chat'] = policy if source == 'cli' else replace(policy, source=source, platform=source)
+ kwargs = dict(chat_id='chat', question='Which?', choices=['one', 'two'], clarify_id='q-' + source,
+ session_key='route-' + source)
+ clarify_mod.register(clarify_id=kwargs['clarify_id'], session_key=kwargs['session_key'],
+ question='Which?', choices=['one', 'two'])
+ sent = schedule(adapter.send_clarify(**kwargs))
+ if on_sent is not None:
+ sent.add_done_callback(lambda _: on_sent(kwargs['session_key']))
+ fallback = lambda: (lambda coro: None if coro is None else schedule(coro))(
+ text_fallback_coro(adapter, **kwargs))
+ started = time.monotonic()
+ result = _clarify_send_then_wait(sent, clarify_id=kwargs['clarify_id'], session_key=kwargs['session_key'],
+ clarify_mod=clarify_mod, fallback=fallback)
+ return result, time.monotonic() - started
+
+ yield run
+ loop.call_soon_threadsafe(loop.stop)
+ thread.join(timeout=5)
+
+
+def test_hosted_clarify_is_declined_without_waiting(ask):
+ from gateway.run_turn_runner_clarify_delivery import UNDELIVERED_DECLINED
+ (response, answered), elapsed = ask('bot_room')
+ assert (response, answered) == (UNDELIVERED_DECLINED, False)
+ assert elapsed < 5, 'the member waited for an answer nobody can submit'
+