From 1453ef360d0d643c6a76dff193fa090a0f71e355 Mon Sep 17 00:00:00 2001 From: Albert Lie <12984659+albertlieyingadrian@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:04:45 -0500 Subject: [PATCH 1/3] ReAct: run the first of two Actions with its own arguments (#306) _ACTION_RE captured from the first Action up to the next Observation, so a turn with a second Action ran the first tool with both calls' arguments, the later value winning. The capture now also ends at the next Action line, as it ends at a made-up Observation. --- .../async-grpo/environments/protocols/react.md | 2 +- halo/environments/protocols/react.py | 5 ++++- tests/cpu/environments/test_environments.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) 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..4fb820a75 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 `Action:` or `Observation` line, so the rest of a turn that writes two actions or makes up its own observation is ignored. ## Configuration diff --git a/halo/environments/protocols/react.py b/halo/environments/protocols/react.py index 54edc72f3..a852ef05a 100644 --- a/halo/environments/protocols/react.py +++ b/halo/environments/protocols/react.py @@ -40,8 +40,11 @@ _FINAL_ANSWER_RE = re.compile( r"(?:^|\n)\s*(?:Final Answer|FINAL ANSWER|Answer|ANSWER)\s*:\s*(.+?)$", re.IGNORECASE | re.DOTALL ) +# The first Action runs, ending where a made-up Observation or a second Action begins; read on, the first tool would run +# with the later call's arguments. _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*(.+?)(?=\n\s*(?:Observation|OBSERVATION|(?:Action|ACTION)\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..aacf3b78b 100644 --- a/tests/cpu/environments/test_environments.py +++ b/tests/cpu/environments/test_environments.py @@ -472,6 +472,23 @@ 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"}'], + ids=["same-tool", "other-tool", "json"], +) +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 ran 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"}) + + +def test_parse_react_output_keeps_a_code_line_that_starts_with_action(): + step = parse_react_output("Thought: t\nAction: python(code='''x = 1\naction = x + 1\nprint(action)''')") + assert step.action_args == {"code": "x = 1\naction = x + 1\nprint(action)"} + + def test_parse_react_output_numeric_args(): text = """Thought: Calculate Action: multiply(a=5, b=3.14)""" From 0ff908ddf289dac89b5ae3e9ddaebc92c0516438 Mon Sep 17 00:00:00 2001 From: Albert Lie <12984659+albertlieyingadrian@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:30:33 -0500 Subject: [PATCH 2/3] ReAct: match the second Action case-sensitively so a code line like action: int is kept CodeRabbit found that under IGNORECASE an annotated code line such as action: int = 3 ended the action capture. The stop now matches only Action and ACTION, the spellings the grammar lists; a test covers the annotation. --- halo/environments/protocols/react.py | 5 +++-- tests/cpu/environments/test_environments.py | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/halo/environments/protocols/react.py b/halo/environments/protocols/react.py index a852ef05a..940980969 100644 --- a/halo/environments/protocols/react.py +++ b/halo/environments/protocols/react.py @@ -41,9 +41,10 @@ r"(?:^|\n)\s*(?:Final Answer|FINAL ANSWER|Answer|ANSWER)\s*:\s*(.+?)$", re.IGNORECASE | re.DOTALL ) # The first Action runs, ending where a made-up Observation or a second Action begins; read on, the first tool would run -# with the later call's arguments. +# with the later call's arguments. The second Action is matched 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|(?:Action|ACTION)\s*:)|$)", + r"(?:^|\n)\s*(?:Action|ACTION)\s*:\s*(.+?)(?=\n\s*(?:Observation|OBSERVATION|(?-i:Action|ACTION)\s*:)|$)", re.IGNORECASE | re.DOTALL, ) # An action's spellings: ``tool_name(arg=...)``, ``tool_name: arg=...``, a bare ``tool_name``. diff --git a/tests/cpu/environments/test_environments.py b/tests/cpu/environments/test_environments.py index aacf3b78b..6399b6ce7 100644 --- a/tests/cpu/environments/test_environments.py +++ b/tests/cpu/environments/test_environments.py @@ -484,9 +484,11 @@ def test_parse_react_output_runs_the_first_of_two_actions_with_its_own_arguments assert (step.action, step.action_args) == ("calculate", {"expression": "1+1"}) -def test_parse_react_output_keeps_a_code_line_that_starts_with_action(): - step = parse_react_output("Thought: t\nAction: python(code='''x = 1\naction = x + 1\nprint(action)''')") - assert step.action_args == {"code": "x = 1\naction = x + 1\nprint(action)"} +@pytest.mark.parametrize("line", ["action = x + 1", "action: int = x + 1"], ids=["assignment", "annotation"]) +def test_parse_react_output_keeps_a_code_line_that_starts_with_action(line): + code = f"x = 1\n{line}\nprint(action)" + step = parse_react_output(f"Thought: t\nAction: python(code='''{code}''')") + assert step.action_args == {"code": code} def test_parse_react_output_numeric_args(): From 7568d77b3aa52f3b8a4bf152598bbc8fc1ad45bb Mon Sep 17 00:00:00 2001 From: hivaze Date: Wed, 2 Dec 2026 21:45:48 +0000 Subject: [PATCH 3/3] ReAct: end the first Action at a Thought line too With a Thought line between two actions, the first action's text took in the Thought and the second call and did not parse. The turn got the format hint and no tool ran, where main ran the first tool with the second call's arguments. The action now also ends at a line that starts with a Thought label. Thought and Action labels both match case-sensitively, so a lowercase `thought: int = 3` line in a code argument stays in it. A code line that starts with `Thought:` now ends the action; one that starts with `Action:` already did. A single action followed by a Thought line now runs where it got the format hint before. `_THOUGHT_RE` and `_ACTION_RE` share one string of Thought labels, and `_THOUGHT_RE` compiles to the same pattern as before. The two-action test takes the Thought-between turn as a fourth case and its docstring says the first tool would run. A new test checks that each of the six Thought labels ends a single action, and the code-line test keeps `thought: int = x + 1` and `Action = x + 1`. react.md names the Thought line. I ran this in the 1.3.0 Blackwell image on CPU. The 26 parse_react_output tests pass. With the react.py of 0ff908ddf, 7 of them fail, and 10 fail with main's. Each of five mutations of the new stop fails at least one test. Merged with main 8405f24ef, tests/cpu/environments (1882 passed, 80 skipped), tests/cpu/conventions (767 passed), the other CPU tests that build a ReAct environment (294 passed), ruff check and ruff format --check on the changed files, and scripts/docs/check_links.sh all pass. Co-Authored-By: Claude Fable 5.2 --- .../environments/protocols/react.md | 2 +- halo/environments/protocols/react.py | 12 ++++---- tests/cpu/environments/test_environments.py | 28 +++++++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) 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 4fb820a75..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. One action runs per turn: the first, read up to the next `Action:` or `Observation` line, so the rest of a turn that writes two actions or makes up its own observation is ignored. +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 940980969..8a2c98283 100644 --- a/halo/environments/protocols/react.py +++ b/halo/environments/protocols/react.py @@ -33,18 +33,20 @@ 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, ending where a made-up Observation or a second Action begins; read on, the first tool would run -# with the later call's arguments. The second Action is matched case-sensitively, so a code line such as -# ``action: int = 3`` inside an argument does not end it. +# 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|(?-i:Action|ACTION)\s*:)|$)", + 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``. diff --git a/tests/cpu/environments/test_environments.py b/tests/cpu/environments/test_environments.py index 6399b6ce7..6ac57e026 100644 --- a/tests/cpu/environments/test_environments.py +++ b/tests/cpu/environments/test_environments.py @@ -474,19 +474,35 @@ def test_parse_react_output_reads_an_escape_python_rejects_without_a_syntax_warn @pytest.mark.parametrize( "second", - ['Action: calculate(expression="10*10")', 'Action: python(code="print(5)")', 'ACTION: {"name": "python"}'], - ids=["same-tool", "other-tool", "json"], + [ + '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 ran with the later call's arguments.""" + 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("line", ["action = x + 1", "action: int = x + 1"], ids=["assignment", "annotation"]) -def test_parse_react_output_keeps_a_code_line_that_starts_with_action(line): - code = f"x = 1\n{line}\nprint(action)" +@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}