hermes-agent/tests/gateway/test_clarify_thread_followup_not_swallowed.py
Siddharth Balyan 5eea87882a
Clarify: one question shape and one result shape on every surface (skip, cancel, timeout and undelivered are told apart) (#127760)
* refactor(desktop): split clarify-tool.tsx into a clarify/ folder

Pure moves, no behaviour change. Parsing, the question-card core
(shell, choice rows, question block), the delivery watchdog, and each
pending/settled card get their own file so the core can be reused.

* feat(clarify): one questions[] shape with per-question status and one outcome

The clarify tool now takes only questions=[{question, choices?, multi_select?}].
A wrong shape is a tool error that names the right one, so a model that
sends the old top-level question/choices corrects itself on the next call.

Every result has the same shape on every surface: each response carries
status (answered, skipped, unanswered) with user_response null unless
answered, and the result carries one outcome (submitted, cancelled,
timed_out, undelivered) plus an optional surface-written notice. The
timeout and cancel sentences that used to pose as the user's answer are
gone, and the compressor reads status instead of matching their prefixes.

The callback contract is callback(questions) -> {answers, outcome, notice?}
(None = skipped, missing qid = unanswered). tui_gateway settles a batch the
same way for the last lock, a cancel, an interrupt and the deadline, and
clarify.lock keeps a null answer as a skip. A multi-select answer that is
not a JSON array counts as one typed ("Other") answer. The tool-row preview
reads the first question.

* feat(clarify): every surface asks through the one question card

CLI: the batch panel is the only panel; Enter on an empty field skips a
question, Ctrl+C cancels and keeps the locked answers, the deadline returns
timed_out. -q and -z return undelivered with a no-user notice.

Ink TUI: the single-question prompt is gone; the card supports multi-select
(Space or a digit toggles, Enter locks, typed Other text joins the picks),
an empty submit skips, Esc cancels, and "Batch" names are dropped.

Desktop: the single-question card is deleted and its keyboard handling moved
into the one card (arrows, letters and digits, auto-advance, Enter picks then
confirms). Confirm enables at one answer; blank questions lock null; Skip and
a composer message cancel; the settled card shows No answer for unanswered
questions. The bots room card follows the same contract.

Messaging: one card per question with a "Reply skip to skip" line in every
locale; timeouts and delivery failures map to timed_out and undelivered.

* fix(clarify): cancelled for a released messaging card, labels win over the skip word

A prose reply to a messaging choice card, /new and session end released
the wait with "", which read as timed_out. They now resolve with a
CANCELLED marker and the tool gets outcome cancelled.

A typed reply that matches a choice label ("Skip") now resolves to that
choice; the skip word applies only when no choice matches.

The bots room card sends the picked labels as they are; the tool already
strips the recommendation label. Unused OUTCOMES and a new docstring and
comment are removed.

* fix(tui): re-editing a multi-select answer keeps its picks

The Ink card stored a multi-select answer as display text ("A, B"), so
revisiting the question put the whole string into Other and re-locked it
as one item. The answers map now keeps the raw JSON array, the card
restores the picks on revisit, and only the display lines format it.

Comments that earlier edits reworded are restored to their original
words, minus the phrases the change made wrong. The compute-host clarify
lock accepts None for a skipped question.

* test(clarify): align three checks with the one-answer confirm and the regenerated keys

The desktop Cmd+Enter test now expects a send with one answer (the blank
question locks null), the compressor test expects the single-answer summary
the code gives, and locales/_keys.desktop.json is regenerated for the
removed and added clarify strings.

* test(tui): the one-question card header is singular
2026-09-29 18:33:44 +05:30

310 lines
9.8 KiB
Python

"""Regression tests for #62034 — pending multi-choice clarify prompts must not
swallow unrelated thread follow-up messages.
When a NATIVE interactive multi-choice clarify (buttons rendered,
``awaiting_text=False``) is pending, the gateway text-intercept used to
consume ANY non-command message in the session as the clarify answer —
arbitrary prose vanished into clarify resolution and the agent appeared to
ignore the user's thread messages.
After the fix (``tools/clarify_gateway._coerce_text_response`` rejects
arbitrary prose for native multi-choice prompts):
* numeric selections ("2") and exact choice labels still resolve, and
* arbitrary prose falls through the intercept and continues as a normal
message-handling turn.
Open-ended clarifies, explicit "Other" text-capture mode, and the base
adapter's numbered-text fallback (which flips ``awaiting_text`` at send time)
keep accepting free text.
"""
from unittest.mock import patch
import pytest
from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import (
BasePlatformAdapter,
SendResult,
)
from gateway.platforms.event import MessageEvent, MessageType
from gateway.session import SessionSource
SESSION_KEY = "agent:main:slack:dm:D123:1111.2222"
class _StubAdapter(BasePlatformAdapter):
def __init__(self):
super().__init__(PlatformConfig(enabled=True, token="test"), Platform.SLACK)
async def connect(self, *, is_reconnect: bool = False):
return True
async def disconnect(self):
pass
async def send(self, chat_id, content, reply_to=None, metadata=None):
return SendResult(success=True, message_id="m1")
async def get_chat_info(self, chat_id):
return {"id": chat_id, "type": "im"}
class _CardAdapter(_StubAdapter):
"""Adapter with a persistent native card: records the gateway's retire callback."""
def __init__(self):
super().__init__()
self.retired: list[tuple[str, str]] = []
async def retire_clarify_card(self, clarify_id: str, notice: str) -> None:
self.retired.append((clarify_id, notice))
class _FellThroughIntercept(Exception):
"""Sentinel: _handle_message got PAST the clarify text-intercept."""
def _event(text):
return MessageEvent(
text=text,
message_type=MessageType.TEXT,
source=SessionSource(
platform=Platform.SLACK,
chat_id="D123",
chat_type="dm",
user_id="U1",
thread_id="1111.2222",
),
message_id="msg1",
)
def _clear_clarify_state():
from tools import clarify_gateway as cm
with cm._lock:
cm._entries.clear()
cm._session_index.clear()
cm._notify_cbs.clear()
def _make_runner(adapter):
from gateway.run import GatewayRunner
runner = GatewayRunner.__new__(GatewayRunner)
runner._startup_restore_in_progress = False
runner._scale_to_zero_note_real_inbound = lambda: None
runner._is_user_authorized = lambda source: True
runner._session_key_for_source = lambda source: SESSION_KEY
runner._delivery_adapter_for = lambda source: adapter
runner._update_prompt_pending = {}
return runner
async def _dispatch(runner, event):
"""Run _handle_message with a tripwire installed AFTER the clarify
intercept (the slash-confirm pending lookup is the next statement), so a
raised ``_FellThroughIntercept`` proves the message was NOT swallowed."""
import tools.slash_confirm as slash_confirm_mod
def _tripwire(_key):
raise _FellThroughIntercept()
with patch("hermes_cli.plugins.invoke_hook", return_value=[]), \
patch.object(slash_confirm_mod, "get_pending", _tripwire):
return await runner._handle_message(event)
@pytest.mark.asyncio
async def test_thread_prose_not_swallowed_by_native_multi_choice_clarify():
"""Arbitrary prose during a pending button-clarify continues as a normal turn."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
# Native interactive multi-choice prompt: awaiting_text stays False.
entry = cm.register("cl-native", SESSION_KEY, "Pick a UI variant", ["buttons", "dropdown"])
assert entry.awaiting_text is False
with pytest.raises(_FellThroughIntercept):
await _dispatch(runner, _event("just checking the visual UI, no need to pass any data"))
# The prose is not accepted as the answer, but the clarify must be
# released before normal busy routing so redirect-to-steer can drain.
with cm._lock:
entry = cm._entries.get("cl-native")
assert entry is not None
assert entry.event.is_set()
assert entry.response == cm.CANCELLED
_clear_clarify_state()
@pytest.mark.asyncio
async def test_thread_prose_retires_the_native_card_before_falling_through():
"""The card adapter gets one retire call (cancel notice) while the prose still falls through."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _CardAdapter()
runner = _make_runner(adapter)
cm.register("cl-slack-card", SESSION_KEY, "Pick a UI variant", ["buttons", "dropdown"])
with pytest.raises(_FellThroughIntercept):
await _dispatch(runner, _event("just checking the visual UI, no need to pass any data"))
assert [cid for cid, _ in adapter.retired] == ["cl-slack-card"]
assert "cancelled" in adapter.retired[0][1].lower()
_clear_clarify_state()
@pytest.mark.asyncio
async def test_typed_selection_retires_the_native_card_with_the_answer():
"""A numeric pick typed into the thread resolves the clarify AND rewrites the card."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _CardAdapter()
runner = _make_runner(adapter)
entry = cm.register("cl-typed-card", SESSION_KEY, "Pick a UI variant", ["buttons", "dropdown"])
assert await _dispatch(runner, _event("2")) == ""
assert entry.response == "dropdown"
assert adapter.retired == [("cl-typed-card", "✅ answered: dropdown")]
_clear_clarify_state()
@pytest.mark.asyncio
async def test_thread_prose_does_not_overwrite_concurrent_button_choice():
"""A button result that wins the race remains the clarify response."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
entry = cm.register(
"cl-button-race",
SESSION_KEY,
"Pick a UI variant",
["buttons", "dropdown"],
)
assert cm.resolve_gateway_clarify("cl-button-race", "buttons") is True
with pytest.raises(_FellThroughIntercept):
await _dispatch(runner, _event("one more unrelated thought"))
assert entry.event.is_set()
assert entry.response == "buttons"
_clear_clarify_state()
@pytest.mark.asyncio
async def test_native_multi_select_out_of_range_keeps_clarify_pending():
"""Out-of-range multi-select numbers must not cancel the pending prompt."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
entry = cm.register(
"cl-ms-oor",
SESSION_KEY,
"Pick some targets",
["staging", "prod", "canary"],
multi_select=True,
)
assert entry.awaiting_text is False
result = await _dispatch(runner, _event("99"))
assert result == ""
with cm._lock:
still = cm._entries.get("cl-ms-oor")
assert still is not None
assert not still.event.is_set()
assert still.response is None
_clear_clarify_state()
@pytest.mark.asyncio
async def test_native_multi_select_bad_comma_list_keeps_clarify_pending():
"""Unrecognised comma-lists are retryable selection attempts, not prose."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
entry = cm.register(
"cl-ms-bad",
SESSION_KEY,
"Pick some targets",
["staging", "prod", "canary"],
multi_select=True,
)
assert entry.awaiting_text is False
result = await _dispatch(runner, _event("1,99"))
assert result == ""
with cm._lock:
still = cm._entries.get("cl-ms-bad")
assert still is not None
assert not still.event.is_set()
assert still.response is None
_clear_clarify_state()
@pytest.mark.asyncio
async def test_native_multi_select_prose_releases_clarify_before_routing():
"""Free prose on multi-select still breaks the redirect/steer deadlock."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
cm.register(
"cl-ms-prose",
SESSION_KEY,
"Pick some targets",
["staging", "prod"],
multi_select=True,
)
with pytest.raises(_FellThroughIntercept):
await _dispatch(
runner,
_event("just checking the visual UI, no need to pass any data"),
)
with cm._lock:
entry = cm._entries.get("cl-ms-prose")
assert entry is not None
assert entry.event.is_set()
assert entry.response == cm.CANCELLED
_clear_clarify_state()
@pytest.mark.asyncio
async def test_prose_still_accepted_after_other_flips_text_capture():
"""After the user taps 'Other', free text IS the answer — must resolve."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _StubAdapter()
runner = _make_runner(adapter)
cm.register("cl-other", SESSION_KEY, "Pick a UI variant", ["buttons", "dropdown"])
assert cm.mark_awaiting_text("cl-other") is True
result = await _dispatch(runner, _event("a carousel actually"))
assert result == ""
with cm._lock:
entry = cm._entries.get("cl-other")
assert entry is not None
assert entry.event.is_set()
assert entry.response == "a carousel actually"
_clear_clarify_state()