diff --git a/agent-docs/training-methods/async-grpo/environments/protocols/react.md b/agent-docs/training-methods/async-grpo/environments/protocols/react.md index ef082d00f..ac642b32b 100644 --- a/agent-docs/training-methods/async-grpo/environments/protocols/react.md +++ b/agent-docs/training-methods/async-grpo/environments/protocols/react.md @@ -13,7 +13,7 @@ Observation: 100 Final Answer: 110 ``` -An action's arguments are read as a Python call of literals (a triple-quoted `code` argument arrives whole), else by a `name=value` pattern. Arguments neither reads bind as none, which a tool with a required argument refuses as a tool error. An `Action:` that spells no call gets the format hint. Neither is an episode error. +An action's arguments are read as a Python call of literals (a triple-quoted `code` argument arrives whole), else by a `name=value` pattern. Arguments neither reads bind as none, which a tool with a required argument refuses as a tool error. An `Action:` that spells no call gets the format hint. Neither is an episode error. One action runs per turn: the first, read up to the next `Thought:`, `Action:` or `Observation` line, so a second action or a made-up observation after it is ignored. ## Configuration diff --git a/halo/environments/protocols/react.py b/halo/environments/protocols/react.py index 54edc72f3..8a2c98283 100644 --- a/halo/environments/protocols/react.py +++ b/halo/environments/protocols/react.py @@ -33,15 +33,21 @@ logger = logging.getLogger(__name__) # The ReAct turn grammar: a Thought, then an Action or a Final Answer (the answer wins when both appear). +_THOUGHT_LABELS = "Thought|THOUGHT|Think|THINK|Reasoning|REASONING" _THOUGHT_RE = re.compile( - r"(?:^|\n)\s*(?:Thought|THOUGHT|Think|THINK|Reasoning|REASONING)\s*:\s*(.+?)(?=\n\s*(?:Action|ACTION|Final|FINAL)|$)", + rf"(?:^|\n)\s*(?:{_THOUGHT_LABELS})\s*:\s*(.+?)(?=\n\s*(?:Action|ACTION|Final|FINAL)|$)", re.IGNORECASE | re.DOTALL, ) _FINAL_ANSWER_RE = re.compile( r"(?:^|\n)\s*(?:Final Answer|FINAL ANSWER|Answer|ANSWER)\s*:\s*(.+?)$", re.IGNORECASE | re.DOTALL ) +# The first Action runs, up to a made-up Observation or the next Thought or Action: read on, the later call's arguments +# would bind to the first tool, or the Thought line would leave the call unparseable. The Thought and Action labels +# match case-sensitively, so a code line such as ``action: int = 3`` inside an argument does not end it. _ACTION_RE = re.compile( - r"(?:^|\n)\s*(?:Action|ACTION)\s*:\s*(.+?)(?=\n\s*(?:Observation|OBSERVATION)|$)", re.IGNORECASE | re.DOTALL + r"(?:^|\n)\s*(?:Action|ACTION)\s*:\s*(.+?)" + rf"(?=\n\s*(?:Observation|OBSERVATION|(?-i:Action|ACTION|{_THOUGHT_LABELS})\s*:)|$)", + re.IGNORECASE | re.DOTALL, ) # An action's spellings: ``tool_name(arg=...)``, ``tool_name: arg=...``, a bare ``tool_name``. _CALL_ACTION_RE = re.compile(r"^(\w+)\s*\((.*)\)$", re.DOTALL) diff --git a/tests/cpu/environments/test_environments.py b/tests/cpu/environments/test_environments.py index a625d6b2a..6ac57e026 100644 --- a/tests/cpu/environments/test_environments.py +++ b/tests/cpu/environments/test_environments.py @@ -472,6 +472,41 @@ def test_parse_react_output_reads_an_escape_python_rejects_without_a_syntax_warn assert not [w for w in caught if issubclass(w.category, SyntaxWarning)] +@pytest.mark.parametrize( + "second", + [ + 'Action: calculate(expression="10*10")', + 'Action: python(code="print(5)")', + 'ACTION: {"name": "python"}', + 'Thought: next\nAction: calculate(expression="10*10")', + ], + ids=["same-tool", "other-tool", "json", "thought-between"], +) +def test_parse_react_output_runs_the_first_of_two_actions_with_its_own_arguments(second): + """A turn holding a second ``Action:`` runs its first, as one that runs on into a made-up ``Observation`` does; + read together, the first tool would run with the later call's arguments.""" + step = parse_react_output(f'Thought: t\nAction: calculate(expression="1+1")\n{second}') + assert (step.action, step.action_args) == ("calculate", {"expression": "1+1"}) + + +@pytest.mark.parametrize("label", ["Thought", "THOUGHT", "Think", "THINK", "Reasoning", "REASONING"]) +def test_parse_react_output_ends_the_action_at_a_thought_line(label): + """Read into the call, the Thought line leaves it unparseable and the turn gets the format hint.""" + step = parse_react_output(f'Thought: t\nAction: calculate(expression="1+1")\n{label}: that should do it') + assert (step.action, step.action_args) == ("calculate", {"expression": "1+1"}) + + +@pytest.mark.parametrize( + "line", + ["action = x + 1", "action: int = x + 1", "thought: int = x + 1", "Action = x + 1"], + ids=["action-assignment", "action-annotation", "thought-annotation", "Action-assignment"], +) +def test_parse_react_output_keeps_a_code_line_that_starts_with_action_or_thought(line): + code = f"x = 1\n{line}\nprint(x)" + step = parse_react_output(f"Thought: t\nAction: python(code='''{code}''')") + assert step.action_args == {"code": code} + + def test_parse_react_output_numeric_args(): text = """Thought: Calculate Action: multiply(a=5, b=3.14)"""