@@ -229,6 +229,32 @@ def test_final_pattern_basic(self):
229229 assert obs .done
230230 assert obs .metadata ["final_answer" ] == "42"
231231
232+ @pytest .mark .parametrize (
233+ "code, expected" ,
234+ [
235+ # Nested function calls inside FINAL(...).
236+ ("print('FINAL(f(x))')" , "f(x)" ),
237+ # Tuple as the final answer.
238+ ("print('FINAL((1, 2, 3))')" , "(1, 2, 3)" ),
239+ # Math expression with multiple nested parens (e2b_repl_example).
240+ (
241+ "print('FINAL(2^(2^(2^(2))) = 65536)')" ,
242+ "2^(2^(2^(2))) = 65536" ,
243+ ),
244+ # Dict containing a tuple value.
245+ ("print(\" FINAL({'a': (1, 2)})\" )" , "{'a': (1, 2)}" ),
246+ # Output after FINAL must not bleed into the extracted answer.
247+ ("print('FINAL(42)\\ nresult: (ok)')" , "42" ),
248+ ],
249+ )
250+ def test_final_pattern_nested_parentheses (self , code , expected ):
251+ """FINAL(...) extraction must handle nested parentheses (rlm #75)."""
252+ env = REPLEnvironment ()
253+ env .reset ()
254+ obs = env .step (REPLAction (code = code ))
255+ assert obs .done
256+ assert obs .metadata ["final_answer" ] == expected
257+
232258 def test_final_var_pattern (self ):
233259 """Test FINAL_VAR() pattern."""
234260 env = REPLEnvironment ()
0 commit comments