From bc9cdf6f816dbbef30df076094ad22d209acefef Mon Sep 17 00:00:00 2001 From: Yev Meyer Date: Fri, 20 Nov 2026 00:02:08 -0500 Subject: [PATCH 1/2] fix(engine): ask for an answer when a tool call passes the turn's limit A call past tool_max_calls_per_turn got a reply saying only that it was not run, and the reply the assistant was then asked for without tools was often empty: the assistant had planned more calls and had no answer ready. The refusal now also asks the assistant to answer the user with the results it has. Signed-off-by: Yev Meyer --- architecture/probes.md | 2 +- src/usersim/engine/core/probes.py | 8 +++++--- tests/engine/core/test_probes_substrate.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/architecture/probes.md b/architecture/probes.md index c9741c97..21164fd1 100644 --- a/architecture/probes.md +++ b/architecture/probes.md @@ -148,7 +148,7 @@ its label. | `verbatim_field` | `"verbatim_text"` | `BankVerbatimMixin`: the task field turn 1 reads | | `turn1_instruction_template` | `""` | `CustomTurn1InstructionMixin`: the instruction turn 1 is written from | | `tool_loop_mode` | `"single"` | `ToolExecutionMixin`: one round of tool calls per turn, or with `"multi"` rounds until the assistant stops calling tools | -| `tool_max_calls_per_turn` | `8` | `ToolExecutionMixin`: the most tool calls it runs in one turn. A call past it gets a reply saying it was not run; after such a reply, the turn ends with a reply made without tools | +| `tool_max_calls_per_turn` | `8` | `ToolExecutionMixin`: the most tool calls it runs in one turn. A call past it gets a reply saying it was not run and to answer the user with the results the assistant has; after such a reply, the turn ends with a reply made without tools | ## Mixins diff --git a/src/usersim/engine/core/probes.py b/src/usersim/engine/core/probes.py index dad5513f..4866f9b3 100644 --- a/src/usersim/engine/core/probes.py +++ b/src/usersim/engine/core/probes.py @@ -1271,8 +1271,9 @@ class ToolExecutionMixin: across several assistant<->tool round-trips). In either shape, a call beyond the turn's cap is not run: it gets a - ``role:"tool"`` reply saying so, so every call an assistant message makes - has a reply, and the assistant sees why before it answers. + ``role:"tool"`` reply saying so and asking for an answer with the results + in hand, so every call an assistant message makes has a reply, and the + assistant sees why before it answers. Hooks a probe implements: @@ -1379,7 +1380,8 @@ async def after_assistant_turn( { "error": ( "Not run: this turn reached its limit of " - f"{self.tool_max_calls_per_turn} tool calls." + f"{self.tool_max_calls_per_turn} tool calls. " + "Answer the user now with the results you have." ), "status": 429, } diff --git a/tests/engine/core/test_probes_substrate.py b/tests/engine/core/test_probes_substrate.py index 11eeeb8d..2dcee705 100644 --- a/tests/engine/core/test_probes_substrate.py +++ b/tests/engine/core/test_probes_substrate.py @@ -660,6 +660,24 @@ async def test_a_call_past_the_cap_is_answered_but_not_run(self, mode: str) -> N assert replies["c2"]["status"] == 429 assert _unanswered_call_ids(state.messages) == set() + async def test_a_call_past_the_cap_tells_the_assistant_to_answer_with_what_it_has(self) -> None: + probe = _ToolExecProbe(mode="multi", cap=1) + state = _StubState() + with patch( + "usersim.engine.core.llm.acall_llm", + return_value={"content": "capped synth", "tool_calls": None}, + ): + await probe.after_assistant_turn( + {}, + state, + {"content": "", "tool_calls": [_tc(cid="c1"), _tc(cid="c2")]}, + object(), + ) + refused = next(json.loads(m["content"]) for m in state.messages if m.get("tool_call_id") == "c2") + assert refused["error"] == ( + "Not run: this turn reached its limit of 1 tool calls. Answer the user now with the results you have." + ) + async def test_multi_mode_turn_at_the_cap_is_told_before_its_reply_without_tools(self) -> None: probe = _ToolExecProbe(mode="multi", cap=2) state = _StubState() From aca8cf398390e809bff78c02ad78f0c37ea31f24 Mon Sep 17 00:00:00 2001 From: Yev Meyer Date: Fri, 20 Nov 2026 00:02:10 -0500 Subject: [PATCH 2/2] fix(engine): don't end a conversation on a user message that asks for more The early-stop check asked the summary model whether a user message ends the conversation, and a model sometimes said yes to a question, or to a message giving the details the assistant had asked for, so the conversation stopped with that message unanswered. A message holding a question mark now never ends a conversation and needs no model call, and the check's prompt says a message answering the assistant, or giving it the details, a choice or a correction it asked for, does not end it either. Signed-off-by: Yev Meyer --- src/usersim/engine/core/simulation.py | 15 ++++++++++++--- tests/engine/core/test_engine_e2e.py | 18 ++++++++++++++++++ .../engine/core/test_episode_runtime_parity.py | 2 +- tests/probes/general_open_ended/test_probe.py | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/usersim/engine/core/simulation.py b/src/usersim/engine/core/simulation.py index 49b0ccc0..1a470fe6 100644 --- a/src/usersim/engine/core/simulation.py +++ b/src/usersim/engine/core/simulation.py @@ -440,12 +440,17 @@ def _is_assistant_refusal_echo(text: str, allowed: frozenset[str] = frozenset()) "Does this user message end the conversation: the user is finished, satisfied " "or not, and the message asks the assistant to do nothing further? A message " "that also makes a request, gives an instruction or asks a question, such as " - "asking it to book, change or check something, does not end it. Consider the " - "language and cultural context.\n" + "asking it to book, change or check something, does not end it. Nor does one " + "that answers the assistant's question or gives it the details, a choice or a " + "correction it asked for, even when it also thanks the assistant or says that " + "is all it needs. Consider the language and cultural context.\n" "Reply with ONLY 'yes' or 'no'.\n\n" "User message: {follow_up}" ) +# A question mark in Latin, full-width (CJK) or Arabic script. +_QUESTION_MARK = re.compile(r"[??؟]") + # Tool-call syntax that reached the reply as text instead of as a tool call. _TOOL_CALL_MARKUP = re.compile(r"||\[TOOL_CALLS\]") @@ -477,8 +482,12 @@ async def _check_conversation_complete(models: dict, follow_up: str) -> bool: """Ask the summary_model whether the user is signaling conversation completion. Language-agnostic: works for any locale without per-language regex patterns. - Uses the cheapest model (summary_model) with a short yes/no prompt. + Uses the cheapest model (summary_model) with a short yes/no prompt. A message + holding a question mark asks for something, so it never ends the + conversation and needs no model call. """ + if _QUESTION_MARK.search(follow_up or ""): + return False prompt = _EARLY_STOP_PROMPT.format(follow_up=follow_up) msgs = [{"role": "user", "content": prompt}] resp = await acall_llm(models, MODEL_SUMMARY, msgs) diff --git a/tests/engine/core/test_engine_e2e.py b/tests/engine/core/test_engine_e2e.py index 32959e8a..8f479860 100644 --- a/tests/engine/core/test_engine_e2e.py +++ b/tests/engine/core/test_engine_e2e.py @@ -20,6 +20,7 @@ from usersim.engine.core.outcomes import FailureAttribution, FailureClass, OutcomeStatus from usersim.engine.core.probes import BaseProbe from usersim.engine.core.simulation import ( + _EARLY_STOP_PROMPT, ConversationLoop, _check_conversation_complete, _poor_assistant_response_reason, @@ -155,6 +156,23 @@ async def test_llm_based_handles_whitespace(self): models = {"summary_model": StubFacade([" Yes \n"])} assert await _check_conversation_complete(models, "Perfect, thanks!") + @pytest.mark.parametrize( + "follow_up", + [ + "That cutoff's already passed, so can you find the soonest option I can actually book?", + "Merci ! Et pour dimanche, tu regardes aussi ?", + "ありがとう。で、来週の分は?", + ], + ) + async def test_a_message_with_a_question_never_ends_the_conversation(self, follow_up): + summary = StubFacade(["yes"]) + assert not await _check_conversation_complete({"summary_model": summary}, follow_up) + assert summary._idx == 0 # decided without a model call + + def test_the_question_covers_a_message_that_answers_the_assistant(self): + assert "answers the assistant's question or gives it the details" in _EARLY_STOP_PROMPT + assert "even when it also thanks the assistant or says that is all it needs" in _EARLY_STOP_PROMPT + # --------------------------------------------------------------------------- # Tests: assistant quality check diff --git a/tests/engine/core/test_episode_runtime_parity.py b/tests/engine/core/test_episode_runtime_parity.py index 14048478..f8e97f97 100644 --- a/tests/engine/core/test_episode_runtime_parity.py +++ b/tests/engine/core/test_episode_runtime_parity.py @@ -162,7 +162,7 @@ async def acompletion(self, messages: Sequence[Any], **kwargs: Any) -> SimpleNam "assistant": "Scripted assistant response.", "judge": "valid scripted turnsuccess", "summary": "yes", - "user": "Could you explain that a little more?", + "user": "Please explain that a little more.", }[self.role] return SimpleNamespace( message=SimpleNamespace( diff --git a/tests/probes/general_open_ended/test_probe.py b/tests/probes/general_open_ended/test_probe.py index 14581a57..7f2bdf29 100644 --- a/tests/probes/general_open_ended/test_probe.py +++ b/tests/probes/general_open_ended/test_probe.py @@ -31,7 +31,7 @@ class KyotoTrip(ScriptedModel): CONTENT = { **ScriptedModel.CONTENT, - "user": "週末に京都へ行くなら、どのお寺を見ればいいですか?", + "user": "週末に京都へ行くので、見るべきお寺を教えてください。", "assistant": "清水寺と伏見稲荷大社がおすすめです。", "summary": "no", }