Raven-1/tests/test_a2a_asking.py
KT e5f5558347
feat(*): step through an ask_user batch as one form, with back, multi-select and one submit (#672)
## Summary

The web page renders an `ask_user` batch as one form the reader steps
through, instead of one sheet per question with no way back.

Today the question broker emits one `clarify.request` per question and
the tool loop awaits them one at a time, so the answer to question 1 is
settled on the server before question 2 is even shown: the page could
not offer "back" no matter how it drew the sheet. The page also ignored
`header`, `recommended`, `index`, `total` and `batch`, so a
three-question call arrived as three unrelated sheets.

What changes, mirroring Claude Code's `AskUserQuestion`:

- A lone single-select question still answers on the pick. A
multi-select question (new per-question `multi_select` flag on the tool)
shows check boxes and needs Submit. A batch is a form: step chips (the
question headers) at the top, Back / Skip / Next per step, a
single-select pick advances to the next step, the last step holds its
pick and reports the whole form with Submit. The recommended option is
marked. The form survives a conversation switch.
- Wire contract, all backward compatible: `clarify.request` carries
`multi_select`, and the `batch` entries the `ask_user` tool builds carry
`choices` / `recommended` / `multi_select` so a surface can draw every
step up front; `clarify.respond` accepts an optional `answers` list
aligned with `batch`. The broker resolves the pending question from
`answer` as before and stashes the later answers; the tool loop's later
`await_question` calls for the same batch are answered from the stash
with no further frame. A producer whose batch entries carry no `choices`
(an ACP elicitation form) is still asked one question at a time, and the
TUI and the chat channels keep answering one question at a time. Every
in-repo `QuestionResponder` (the RPC broker, the routing shim and the
A2A broker) takes the widened keyword set, and a test walks the tree so
the next implementation is held to it.
- `ask_user` gains `multi_select` on each question; the answer reaches
the model as the chosen labels joined with `, `.

Not in this PR: check boxes in the TUI, a multi-select hint in chat
channels, and richer batch entries from the ACP elicitor.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Python (worktree venv):

```
uv run pytest tests/test_ask_user_tool.py tests/test_question_broker.py tests/test_a2a_asking.py tests/test_rpc_schema_match.py tests/test_rpc_contract_shapes.py tests/test_acp_ask_user.py tests/test_acp_elicitation.py tests/test_cli_gateway_commands.py tests/test_agents_research_flow_ask_user_surface.py -q
make lint-python && make lint-imports && make lint-types
make test-python
```

- targeted files: 649 passed (plus 9 in
test_contracts_two_tier_ledger.py); lint, import and type gates exit 0;
`make test-python`: 25110 passed, 109 skipped.

Web and TUI:

```
cd ui-web && npx vitest run && npm run type-check && npm run lint && npm run gen:check
cd ui-tui && npm run lint && npm run lint:rpc && npm run lint:i18n && npm run type-check && npm test
```

- ui-web: 193 files / 2678 tests passed including the gates under
scripts/gates (class namespace, css ownership, i18n keys, notification
table); type-check clean; lint 0 errors (4 pre-existing warnings in
files this PR does not touch); gen:check in sync.
- ui-tui: lint, lint:rpc, lint:i18n, type-check and the test suite all
exit 0; the regenerated i18n table and rpc types are committed.

Real gateway, driven with playwright against an isolated `RAVEN_HOME` on
a `deepseek-v4-flash` session that really calls `ask_user`:

- before: a three-question call docked three sheets one after another,
no chips, no way back;
- after: one sheet for the whole batch (a MutationObserver counted one
dock), pick on step 1 advances, Back returns with the pick still held,
the multi-select step joins `cheese, onion`, Submit on the last step,
and the model echoed `ANSWERS: red | cheese, onion | Zed`; a lone
single-select answers on the pick; a lone multi-select needs Submit and
echoed `monday, wednesday`.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

User-visible: a multi-question `ask_user` on the page becomes one
stepped form; digit shortcuts on the sheet no longer fire while any text
field has the caret (they used to fire from the composer). Old frames
and old clients keep working: every new field is optional or additive,
`clarify.respond` without `answers` behaves exactly as before, and the
TUI reads the same frames unchanged. A batch whose entries do not carry
`choices` falls back to one question at a time. Rollback is a revert of
the squash commit; nothing is persisted in a new shape.

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-fable-5-1) <noreply@anthropic.com>
2026-09-23 10:32:44 +08:00

58 lines
2.0 KiB
Python

"""A turn that asks parks its task; a later message answers the waiting turn."""
import asyncio
from a2a.types import TaskState
from raven.a2a.asking import A2aQuestionBroker
from raven.agent.tools.ask_user import AskUserTool
async def test_await_question_parks_and_then_returns_the_answer():
parked = []
broker = A2aQuestionBroker(on_park=parked.append)
waiting = asyncio.create_task(broker.await_question("task-1", prompt="which one?", timeout_s=5.0))
await asyncio.sleep(0)
assert parked == ["task-1"]
assert not waiting.done()
assert broker.answer("task-1", "the second one") is True
assert await waiting == "the second one"
async def test_answering_an_unknown_task_reports_that_it_did_nothing():
assert A2aQuestionBroker(on_park=lambda _: None).answer("nope", "hi") is False
async def test_a_timed_out_question_returns_the_default_and_unparks():
broker = A2aQuestionBroker(on_park=lambda _: None)
out = await broker.await_question("task-2", prompt="?", default="fallback", timeout_s=0.01)
assert out == "fallback"
assert broker.answer("task-2", "too late") is False
def test_parked_is_the_input_required_state():
from raven.a2a.lifecycle import task_state_for
assert task_state_for("question") == TaskState.TASK_STATE_INPUT_REQUIRED
async def test_the_ask_user_tool_asks_through_this_broker_with_every_flag_it_sends():
"""The tool hands the broker every field of the protocol, a multi-select flag
included, so a responder that stopped short of the protocol failed the call
before anything was parked."""
parked = []
broker = A2aQuestionBroker(on_park=parked.append)
tool = AskUserTool(broker=broker, conversation_id="task-3", timeout_s=5.0)
asking = asyncio.create_task(
tool.execute(questions=[{"question": "Which days?", "options": ["mon", "tue"], "multi_select": True}])
)
await asyncio.sleep(0)
assert parked == ["task-3"]
assert broker.answer("task-3", "mon, tue") is True
result = await asking
assert result.display_text == "answered: mon, tue"