A question that offers real choices, and says how many you may take
Three things about `ask_user`, all of them about the card being answerable rather than about the tool being callable. Options are required now, and they are objects: a label, and a line of description where the label alone does not say what choosing it would mean. "Rewrite it" and "Patch it" are two words that do not tell you which one loses your uncommitted work. They stack one per line, because a row of chips has nowhere to put the second line and no room to read the first. The model says whether they are exclusive. Only it knows whether its options are alternatives or a set, and the card has to show which -- a radio group offered where checkboxes were meant loses every answer but one. Exclusive is the default, being the cheaper mistake. A `multiple` question posts the same field name once per ticked box, so the endpoint gathers choices into a list; the `setdefault` it did before kept the first and dropped the rest, which is an answer that says something the reader did not. And "Something else" is added here, on every question, with the box behind it revealed by `:has()` and no JavaScript at all. The model is told never to write an "other" option of its own, because its version would be a choice with no box behind it -- a word submitted that means nothing. It carries a sentinel rather than an answer, and the endpoint swaps in what was typed beside it, or drops it when the box was left empty rather than telling the model the answer is "__other__". Typing no longer beats picking. That rule belonged to a box that was always visible next to the options; this one only exists once its own option is chosen, so picking is the answer and the box is one of the things you can pick. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+233
-10
@@ -137,7 +137,7 @@ async def test_the_reply_pauses_and_the_card_describes_the_question(scripted):
|
||||
pending = await _until_paused(generation)
|
||||
assert pending.kind == interaction.KIND_QUESTION
|
||||
assert pending.items[0].title == "Tea or coffee?"
|
||||
assert pending.items[0].options == ("Tea", "Coffee")
|
||||
assert [o.label for o in pending.items[0].options] == ["Tea", "Coffee"]
|
||||
assert "Waiting for your answer" in generation.status
|
||||
|
||||
pending.resolve(interaction.ANSWER, answers={"q0": "Tea"})
|
||||
@@ -261,6 +261,13 @@ def test_an_interruption_with_no_future_cannot_be_resolved():
|
||||
assert pause.resolve(interaction.ANSWER) is False
|
||||
|
||||
|
||||
def _opts(*labels: str) -> tuple[interaction.Option, ...]:
|
||||
"""Options as the parser produces them: a label and an optional line under
|
||||
it. Bare strings are what a model sends and what `_options_in` normalises;
|
||||
`Item` itself only ever holds the normalised form."""
|
||||
return tuple(interaction.Option(label=label) for label in labels)
|
||||
|
||||
|
||||
def _item(key: str = "q0", *, title: str = "Tea or coffee?", **kwargs) -> interaction.Item:
|
||||
return interaction.Item(
|
||||
index=0,
|
||||
@@ -374,12 +381,14 @@ def test_the_card_shows_the_question_and_its_options():
|
||||
pause = interaction.Interruption(
|
||||
id="p1",
|
||||
items=(
|
||||
_item(title="Tea or coffee?", options=("Tea", "Coffee")),
|
||||
_item(title="Tea or coffee?", options=_opts("Tea", "Coffee")),
|
||||
),
|
||||
)
|
||||
html = _render(pause)
|
||||
assert "Tea or coffee?" in html
|
||||
assert 'value="Tea"' in html and 'value="Coffee"' in html
|
||||
# Stacked, never in a row: an option carries a description now.
|
||||
assert "interaction__option" in html
|
||||
assert 'hx-post="/api/chats/chat-1/interaction/p1"' in html
|
||||
assert "The model is asking you" in html, "attributed to the model, not to LLeMbas"
|
||||
|
||||
@@ -454,7 +463,7 @@ def test_everything_on_the_card_is_escaped():
|
||||
_item(
|
||||
title="<img src=x onerror=alert(1)>",
|
||||
detail="rm -rf / <script>",
|
||||
options=('"><script>bad</script>',),
|
||||
options=_opts('"><script>bad</script>'),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -675,7 +684,7 @@ def test_the_card_renders_every_question_with_its_own_fields():
|
||||
pause = interaction.Interruption(
|
||||
id="p1",
|
||||
items=(
|
||||
_item("q0", title="Which database?", options=("SQLite", "Postgres")),
|
||||
_item("q0", title="Which database?", options=_opts("SQLite", "Postgres")),
|
||||
_item("q1", title="Which port?"),
|
||||
),
|
||||
)
|
||||
@@ -684,13 +693,68 @@ def test_the_card_renders_every_question_with_its_own_fields():
|
||||
assert "Which database?" in html and "Which port?" in html
|
||||
# Radios rather than submit buttons: picking one must not send the form
|
||||
# while two other questions are still blank.
|
||||
assert 'type="radio" name="choice.q0" value="SQLite"' in html
|
||||
assert 'name="choice.q0" value="SQLite"' in html
|
||||
assert 'name="text.q0"' in html and 'name="text.q1"' in html
|
||||
# A question with no options still gets somewhere to write.
|
||||
assert 'name="choice.q1"' not in html
|
||||
# Every question gets "Something else" and the box behind it, including one
|
||||
# the model gave no options for -- that row is added here, never by the
|
||||
# model, which is why the tool tells it not to write an "other" of its own.
|
||||
assert html.count(f'value="{interaction.OTHER}"') == 2
|
||||
assert html.count("Something else") == 2
|
||||
assert html.count("Send answers") == 1, "one submit for the whole card"
|
||||
|
||||
|
||||
def test_options_are_radios_when_exclusive_and_checkboxes_when_not():
|
||||
"""The model says which, and the card has to show it: a radio group offered
|
||||
where a set was meant loses every answer but one, and checkboxes offered for
|
||||
alternatives invite an answer that contradicts itself."""
|
||||
one = _render(
|
||||
interaction.Interruption(
|
||||
id="p1", items=(_item("q0", options=_opts("Tea", "Coffee")),)
|
||||
)
|
||||
)
|
||||
many = _render(
|
||||
interaction.Interruption(
|
||||
id="p1", items=(_item("q0", options=_opts("Tea", "Coffee"), multiple=True),)
|
||||
)
|
||||
)
|
||||
|
||||
assert 'type="radio"' in one and 'type="checkbox"' not in one
|
||||
assert 'type="checkbox"' in many and 'type="radio"' not in many
|
||||
# Including the "Something else" row, which has to match the rest or the
|
||||
# card offers a set and then refuses to take one.
|
||||
assert one.count('type="radio"') == many.count('type="checkbox"') == 3
|
||||
|
||||
|
||||
def test_an_option_can_carry_a_line_explaining_it():
|
||||
""""Rewrite it" and "Patch it" say nothing about which loses your
|
||||
uncommitted work."""
|
||||
pause = interaction.Interruption(
|
||||
id="p1",
|
||||
items=(
|
||||
_item(
|
||||
"q0",
|
||||
options=(
|
||||
interaction.Option("Rewrite it", "Replaces the file whole."),
|
||||
interaction.Option("Patch it"),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
html = _render(pause)
|
||||
|
||||
assert "Replaces the file whole." in html
|
||||
assert html.count("interaction__option-note") == 1, "and only where there is one"
|
||||
|
||||
|
||||
def test_an_options_description_is_escaped_like_everything_else():
|
||||
pause = interaction.Interruption(
|
||||
id="p1",
|
||||
items=(_item("q0", options=(interaction.Option("ok", '<script>bad</script>'),)),),
|
||||
)
|
||||
|
||||
assert "<script>bad" not in _render(pause)
|
||||
|
||||
|
||||
def test_a_single_question_says_answer_rather_than_send_answers():
|
||||
html = _render(interaction.Interruption(id="p1", items=(_item(),)))
|
||||
assert "Send answers" not in html
|
||||
@@ -719,8 +783,11 @@ def test_the_endpoint_gathers_every_answer_at_once(client, db, registered, user_
|
||||
data={
|
||||
"choice.q0": "Postgres",
|
||||
"text.q0": "",
|
||||
"choice.q1": "Yes",
|
||||
# "Something else" ticked, and the box beside it filled in.
|
||||
"choice.q1": interaction.OTHER,
|
||||
"text.q1": "actually, later",
|
||||
# A box with no choice beside it: the approval card's corrected
|
||||
# command, which is the one place `text.` stands on its own.
|
||||
"text.q2": "5433",
|
||||
},
|
||||
)
|
||||
@@ -729,8 +796,8 @@ def test_the_endpoint_gathers_every_answer_at_once(client, db, registered, user_
|
||||
chats_api.generation_service.answer = original
|
||||
|
||||
assert seen["answers"] == {
|
||||
"q0": "Postgres", # picked, nothing written
|
||||
"q1": "actually, later", # written wins over picked
|
||||
"q0": "Postgres", # picked
|
||||
"q1": "actually, later", # "Something else", and what was written
|
||||
"q2": "5433", # written, nothing to pick
|
||||
}
|
||||
assert seen["verdict"] == ""
|
||||
@@ -754,3 +821,159 @@ def test_the_endpoint_passes_a_verdict_through_untouched(client, db, registered,
|
||||
|
||||
assert seen["verdict"] == "allow"
|
||||
assert seen["answers"] == {}
|
||||
|
||||
|
||||
# --- Options as a model actually sends them -----------------------------------
|
||||
def test_options_are_read_as_objects_or_as_bare_strings():
|
||||
"""The schema asks for objects with a label. A small model sends a list of
|
||||
strings -- which is what the schema asked for until recently, and what most
|
||||
examples of this pattern look like -- so that is read rather than refused."""
|
||||
objects = generation_service._options_in(
|
||||
{"options": [{"label": "Rewrite", "description": "Replaces it whole."}, {"label": "Patch"}]}
|
||||
)
|
||||
strings = generation_service._options_in({"options": ["Rewrite", "Patch"]})
|
||||
|
||||
assert [(o.label, o.description) for o in objects] == [
|
||||
("Rewrite", "Replaces it whole."),
|
||||
("Patch", ""),
|
||||
]
|
||||
assert [(o.label, o.description) for o in strings] == [("Rewrite", ""), ("Patch", "")]
|
||||
|
||||
|
||||
def test_an_option_that_is_neither_is_dropped_rather_than_stringified():
|
||||
"""`{'a': 1}` rendered as a choice is worse than one choice fewer."""
|
||||
options = generation_service._options_in({"options": ["ok", 7, None, {"nope": 1}, ""]})
|
||||
|
||||
assert [o.label for o in options] == ["ok"]
|
||||
|
||||
|
||||
def test_too_many_options_are_cut_off():
|
||||
options = generation_service._options_in({"options": [f"o{i}" for i in range(20)]})
|
||||
|
||||
assert len(options) == interaction.MAX_OPTIONS
|
||||
|
||||
|
||||
def test_the_parser_never_adds_something_else_itself():
|
||||
"""It belongs to the template, which adds it to every question and owns the
|
||||
box behind it. In the data it would be indistinguishable from one the model
|
||||
wrote -- and the model is told not to write one precisely because this row
|
||||
already exists."""
|
||||
options = generation_service._options_in({"options": ["Tea", "Coffee"]})
|
||||
|
||||
assert all(o.label != "Something else" for o in options)
|
||||
assert all(o.label != interaction.OTHER for o in options)
|
||||
|
||||
|
||||
async def test_whether_several_may_be_chosen_survives_into_the_card(db, user_id, monkeypatch):
|
||||
"""Through the real loop, because the flag has to reach `Item` -- reading it
|
||||
off the arguments and never passing it on would look identical here."""
|
||||
import json as _json
|
||||
|
||||
chat_id, message_id = _chat_that_can_ask(db, user_id)
|
||||
arguments = _json.dumps(
|
||||
{
|
||||
"questions": [
|
||||
{"question": "Which of these?", "options": ["a", "b"], "multiple": True},
|
||||
{"question": "And which one?", "options": ["c", "d"]},
|
||||
]
|
||||
}
|
||||
)
|
||||
chunk = {
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"tool_calls": [
|
||||
{
|
||||
"index": 0,
|
||||
"id": "c1",
|
||||
"function": {"name": "ask_user", "arguments": arguments},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
generation_service, "stream_chat", _stub_stream([[chunk], [_text_chunk("ok")]], [])
|
||||
)
|
||||
|
||||
generation = generation_service.Generation(chat_id=chat_id, message_id=message_id)
|
||||
task = asyncio.create_task(generation_service._run(generation))
|
||||
pending = await _until_paused(generation)
|
||||
|
||||
assert pending.items[0].multiple is True
|
||||
# Exclusive is the default: a radio group offered where checkboxes were
|
||||
# meant costs one clarifying round, while checkboxes offered for
|
||||
# alternatives invite an answer that contradicts itself.
|
||||
assert pending.items[1].multiple is False
|
||||
|
||||
pending.resolve(interaction.ANSWER, answers={"q0": "a", "q1": "c"})
|
||||
await task
|
||||
|
||||
|
||||
def test_several_ticked_answers_all_come_back(client, db, registered, user_id):
|
||||
"""A `multiple` question posts the same field name once per ticked box. The
|
||||
old reader kept the first and lost the rest, which is an answer that says
|
||||
something the reader did not."""
|
||||
chat_id, _message_id = _chat_that_can_ask(db, user_id)
|
||||
seen: dict = {}
|
||||
|
||||
from lembas.api import chats as chats_api
|
||||
|
||||
original = chats_api.generation_service.answer
|
||||
chats_api.generation_service.answer = lambda chat, iid, *, verdict="", answers=None: (
|
||||
seen.update(answers=answers) or True
|
||||
)
|
||||
try:
|
||||
client.post(
|
||||
f"/api/chats/{chat_id}/interaction/p1",
|
||||
data={"choice.q0": ["Tea", "Coffee"], "text.q0": ""},
|
||||
)
|
||||
finally:
|
||||
chats_api.generation_service.answer = original
|
||||
|
||||
assert seen["answers"] == {"q0": "Tea, Coffee"}
|
||||
|
||||
|
||||
def test_something_else_ticked_with_an_empty_box_says_nothing(client, db, registered, user_id):
|
||||
"""Rather than telling the model the answer is "__other__", which is exactly
|
||||
the shape of thing it would try to act on."""
|
||||
chat_id, _message_id = _chat_that_can_ask(db, user_id)
|
||||
seen: dict = {}
|
||||
|
||||
from lembas.api import chats as chats_api
|
||||
|
||||
original = chats_api.generation_service.answer
|
||||
chats_api.generation_service.answer = lambda chat, iid, *, verdict="", answers=None: (
|
||||
seen.update(answers=answers) or True
|
||||
)
|
||||
try:
|
||||
client.post(
|
||||
f"/api/chats/{chat_id}/interaction/p1",
|
||||
data={"choice.q0": interaction.OTHER, "text.q0": " "},
|
||||
)
|
||||
finally:
|
||||
chats_api.generation_service.answer = original
|
||||
|
||||
assert seen["answers"] == {}
|
||||
|
||||
|
||||
def test_a_ticked_option_and_something_else_come_back_together(client, db, registered, user_id):
|
||||
chat_id, _message_id = _chat_that_can_ask(db, user_id)
|
||||
seen: dict = {}
|
||||
|
||||
from lembas.api import chats as chats_api
|
||||
|
||||
original = chats_api.generation_service.answer
|
||||
chats_api.generation_service.answer = lambda chat, iid, *, verdict="", answers=None: (
|
||||
seen.update(answers=answers) or True
|
||||
)
|
||||
try:
|
||||
client.post(
|
||||
f"/api/chats/{chat_id}/interaction/p1",
|
||||
data={"choice.q0": ["Tea", interaction.OTHER], "text.q0": "or juice"},
|
||||
)
|
||||
finally:
|
||||
chats_api.generation_service.answer = original
|
||||
|
||||
assert seen["answers"] == {"q0": "Tea, or juice"}
|
||||
|
||||
Reference in New Issue
Block a user