From 4fa9e402912b63468998e5461f9d4c4a7d4aabf6 Mon Sep 17 00:00:00 2001 From: Osamaali313 <86572800+Osamaali313@users.noreply.github.com> Date: Wed, 24 Jun 2026 23:46:43 +0300 Subject: [PATCH] fix: extract multi-digit state indices in extract_state_value_from_output The regex matched a single digit and the matches were passed through set(), so a state index >= 10 (e.g. "10") was split into separate digits and an arbitrary one was returned, with the result order depending on set hashing (nondeterministic across runs). Role._think then read the wrong state and silently transitioned incorrectly, or looped, for any role with 10+ states. Match the first (optionally negative, multi-digit) integer and drop the set() dedup so the whole number is returned deterministically. Single-digit and "-1" outputs are unchanged. Adds a regression test. --- metagpt/utils/repair_llm_raw_output.py | 3 +-- tests/metagpt/utils/test_repair_llm_raw_output.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 5c57693f74..e9e18a6f6b 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -349,10 +349,9 @@ def extract_state_value_from_output(content: str) -> str: """ content = content.strip() # deal the output cases like " 0", "0\n" and so on. pattern = ( - r"(? 0 else "-1" return state diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 7a29ea3ee2..683b20508e 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -386,3 +386,15 @@ class Game{\n +int score\n +list tiles\n +function move_ assert output.startswith('{\n"Implementation approach"') and output.endswith( '"Anything UNCLEAR": "The requirement is clear to me."\n}' ) + + +def test_extract_state_value_from_output(): + from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output + + # single-digit states and the "-1" no-op state are returned as-is + assert extract_state_value_from_output("0") == "0" + assert extract_state_value_from_output(" 3\n") == "3" + assert extract_state_value_from_output("-1") == "-1" + # multi-digit state indices (>= 10) must be kept whole, not split into digits + assert extract_state_value_from_output("10") == "10" + assert extract_state_value_from_output("The next state is 12.") == "12"