Skip to content

The agent that grows with you

fix(tools): reject all-blank and over-limit clarify choices instead of dropping them silently

Brooklyn Nicholsona4c31d592b9d
diff --git a/tests/tools/test_clarify_tool.py b/tests/tools/test_clarify_tool.py
index 850dc74d84e..54e8d180beb 100644
--- a/tests/tools/test_clarify_tool.py
+++ b/tests/tools/test_clarify_tool.py
@@ -245,6 +245,34 @@ class TestClarifyBatchValidation:
result = _ask(lambda questions: _reply({}), [{"question": "Real?"}, {"question": " "}])
assert "error" in result
+ def test_all_blank_choices_are_an_error_not_open_ended(self):
+ """#73152: a choices list whose entries are all blank must not quietly become a free-text card."""
+ asked = []
+ result = _ask(lambda questions: asked.append(questions) or _reply({}),
+ [{"question": "Pick one?", "choices": ["", " "]}])
+ assert "error" in result and "blank" in result["error"]
+ assert asked == []
+
+ def test_empty_choices_list_stays_open_ended(self):
+ seen = {}
+ _ask(lambda questions: seen.setdefault("q", questions) and _reply({"q0": "free text"}),
+ [{"question": "Anything?", "choices": []}])
+ assert seen["q"][0]["choices"] is None
+
+ def test_over_limit_choice_is_rejected_at_the_source(self):
+ """#124127: a choice longer than a surface renders is refused, not silently dropped downstream."""
+ from tools.clarify_tool import MAX_CHOICE_CHARS
+ long_choice = "x" * (MAX_CHOICE_CHARS + 1)
+ result = _ask(lambda questions: _reply({}), [{"question": "Pick?", "choices": ["short", long_choice]}])
+ assert "error" in result and str(MAX_CHOICE_CHARS) in result["error"]
+
+ def test_long_multi_line_choice_within_limit_is_kept(self):
+ seen = {}
+ choice = "Option A\n" + "detail " * 60
+ _ask(lambda questions: seen.setdefault("q", questions) and _reply({"q0": "Option A"}),
+ [{"question": "Pick?", "choices": [choice, "B"]}])
+ assert seen["q"][0]["choices_offered"][0] == choice.strip()
+
def test_batch_rejects_non_list(self):
result = _ask(lambda questions: _reply({}), {"question": "Q?"})
assert "error" in result
diff --git a/tools/clarify_tool.py b/tools/clarify_tool.py
index e09ac84bd13..b47fb048c38 100644
--- a/tools/clarify_tool.py
+++ b/tools/clarify_tool.py
@@ -7,6 +7,9 @@ from typing import Callable, Dict, List, Optional
MAX_CHOICES = 4 # the UI always appends an "Other (type your answer)" row
MAX_QUESTIONS = 5 # independent questions per call
+# Per-choice cap. Surfaces wrap long choice text (newlines kept), so anything longer is rejected here, at the
+# source, instead of a surface silently dropping a choice that ``choices_offered`` still reports as shown.
+MAX_CHOICE_CHARS = 8000
# Applied to the first choice here (not per-surface) so every adapter renders it identically.
RECOMMENDED_LABEL = "(Recommended)"
_UNAVAILABLE = "Clarify tool is not available in this execution context."
@@ -63,7 +66,16 @@ def _normalize_questions(questions) -> tuple:
if choices is not None:
if not isinstance(choices, list) or not all(isinstance(c, str) for c in choices):
return None, f"questions[{index}].choices must be a list of strings."
- choices = [c.strip() for c in choices if c.strip()][:MAX_CHOICES] or None
+ for pos, choice in enumerate(choices):
+ if len(choice.strip()) > MAX_CHOICE_CHARS:
+ return None, (f"questions[{index}].choices[{pos}] is {len(choice.strip())} characters; the limit is "
+ f"{MAX_CHOICE_CHARS}. Move detail into the question text and resend.")
+ cleaned = [c.strip() for c in choices if c.strip()][:MAX_CHOICES]
+ if choices and not cleaned:
+ # A card with no options strands the turn: say so instead of silently asking open-ended.
+ return None, (f"questions[{index}].choices has {len(choices)} entries but all are blank. "
+ "Send real labels, or omit choices to ask open-ended.")
+ choices = cleaned or None
normalized.append({
"qid": f"q{index}", "question": text,
"choices": mark_recommended(list(choices)) if choices else None,
@@ -157,7 +169,7 @@ CLARIFY_SCHEMA = {
"question": {"type": "string"},
"choices": {
"type": "array",
- "items": {"type": "string"},
+ "items": {"type": "string", "maxLength": MAX_CHOICE_CHARS},
"maxItems": MAX_CHOICES,
},
"multi_select": {"type": "boolean"},