Skip to content

The agent that grows with you

fix(compression): preserve batch clarify answers in summarize pass

gaoanze8888af248042c1b
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index ac32a6fc18b..15d3543e07b 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1457,7 +1457,25 @@ def _sum_clarify(name, args, content, content_len, line_count):
# min_prune_chars guard and skips the >=200-char dedup.
max_summary_chars = _PRUNE_MIN_CHARS - 1
truncation_marker = "...[truncated]"
- response = _json_dict(content).get("user_response")
+ parsed = _json_dict(content)
+ response = parsed.get("user_response")
+ # Batch clarify (``questions=[...]``) nests each answer inside ``responses[].user_response``
+ # rather than the top level; without this every batch answer was lost and the summarizer only
+ # saw "asked user a question" (#106077).
+ if response is None:
+ batch_responses = parsed.get("responses")
+ if isinstance(batch_responses, list) and batch_responses:
+ collected = []
+ for entry in batch_responses:
+ if not isinstance(entry, dict):
+ continue
+ single = entry.get("user_response")
+ # multi_select emits a list of strings; flatten it so the summary keeps every choice.
+ if isinstance(single, str) and single:
+ collected.append(single)
+ elif isinstance(single, list) and all(isinstance(s, str) and s for s in single):
+ collected.extend(single)
+ response = collected if collected else None
is_answer_shaped = (isinstance(response, str) and bool(response)) or (
isinstance(response, list) and bool(response) and all(isinstance(s, str) and s for s in response)
)
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index e023335403e..0e83f80b720 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -233,6 +233,51 @@ class TestSummarizeToolResultClarify:
assert summary == "[clarify] asked user a question", sentinel
+ def test_preserves_batch_user_response_from_responses_list(self):
+ """Batch clarify (``questions=[...]``) nests answers inside ``responses[].user_response``;
+ the summarizer must surface them, not just 'asked user a question' (#106077)."""
+ content = json.dumps({
+ "responses": [
+ {
+ "question": "May the fields be removed?",
+ "choices_offered": None,
+ "user_response": "Keep the fields until ratification.",
+ }
+ ]
+ })
+
+ summary = _summarize_tool_result("clarify", "{}", content)
+
+ assert summary == '[clarify] user responded: ["Keep the fields until ratification."]'
+
+ def test_preserves_batch_multi_select_and_skips_empties(self):
+ """A partially-answered batch (multi_select + a skipped question) still surfaces every real decision."""
+ content = json.dumps({
+ "responses": [
+ {"question": "Q1?", "user_response": "Answer one"},
+ {"question": "Q2?", "user_response": ["Choice A", "Choice B"]},
+ {"question": "Q3?", "user_response": ""},
+ ]
+ })
+
+ summary = _summarize_tool_result("clarify", "{}", content)
+
+ assert "Answer one" in summary
+ assert "Choice A" in summary
+ assert "Choice B" in summary
+
+ def test_batch_with_only_sentinel_falls_back_to_generic(self):
+ """A batch where every response is a timeout sentinel must not be quoted as a user answer."""
+ content = json.dumps({
+ "responses": [
+ {"question": "Q?", "user_response": "[user did not respond within 15m]"},
+ ]
+ })
+
+ summary = _summarize_tool_result("clarify", "{}", content)
+
+ assert summary == "[clarify] asked user a question"
+
class TestShouldCompress:
def test_below_threshold(self, compressor):