Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Fix agent tool graph tests (#4948)
Matthew Zhou155fa9c734d7
diff --git a/tests/helpers/endpoints_helper.py b/tests/helpers/endpoints_helper.pyindex 41ea70f21..431ac2c70 100644--- a/tests/helpers/endpoints_helper.py+++ b/tests/helpers/endpoints_helper.py@@ -39,7 +39,7 @@ LLM_CONFIG_PATH = "tests/configs/llm_model_configs/letta-hosted.json"# ======================================================================================================================-def setup_agent(+async def setup_agent(server: SyncServer,filename: str,memory_human_str: str = get_human_text(DEFAULT_HUMAN),@@ -81,8 +81,8 @@ def setup_agent(include_base_tools=include_base_tools,include_base_tool_rules=include_base_tool_rules,)- actor = server.user_manager.get_user_or_default()- agent_state = server.create_agent(request=request, actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ agent_state = await server.create_agent_async(request=request, actor=actor)return agent_statediff --git a/tests/helpers/utils.py b/tests/helpers/utils.pyindex cd43061ea..eaf989f97 100644--- a/tests/helpers/utils.py+++ b/tests/helpers/utils.py@@ -81,12 +81,12 @@ def retry_until_success(max_attempts=10, sleep_time_seconds=4):return decorator_retry-def cleanup(server: SyncServer, agent_uuid: str, actor: User):+async def cleanup_async(server: SyncServer, agent_uuid: str, actor: User):# Clear all agents- agent_states = server.agent_manager.list_agents(name=agent_uuid, actor=actor)+ agent_states = await server.agent_manager.list_agents_async(name=agent_uuid, actor=actor)for agent_state in agent_states:- server.agent_manager.delete_agent(agent_id=agent_state.id, actor=actor)+ await server.agent_manager.delete_agent_async(agent_id=agent_state.id, actor=actor)# Utility functionsdiff --git a/tests/integration_test_agent_tool_graph.py b/tests/integration_test_agent_tool_graph.pyindex 94858a82f..b9adb1762 100644--- a/tests/integration_test_agent_tool_graph.py+++ b/tests/integration_test_agent_tool_graph.py@@ -17,7 +17,7 @@ from tests.helpers.endpoints_helper import (assert_sanity_checks,setup_agent,)-from tests.helpers.utils import cleanup+from tests.helpers.utils import cleanup_asyncfrom tests.utils import create_tool_from_func# Generate uuid for agent name for this example@@ -27,16 +27,17 @@ config_file = "tests/configs/llm_model_configs/openai-gpt-4o.json"@pytest.fixture()-def server():+async def server():config = LettaConfig.load()config.save()server = SyncServer()+ await server.init_async()return server@pytest.fixture(scope="function")-def first_secret_tool(server):+async def first_secret_tool(server):def first_secret_word():"""Retrieves the initial secret word in a multi-step sequence.@@ -46,13 +47,13 @@ def first_secret_tool(server):"""return "v0iq020i0g"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=first_secret_word), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=first_secret_word), actor=actor)yield tool@pytest.fixture(scope="function")-def second_secret_tool(server):+async def second_secret_tool(server):def second_secret_word(prev_secret_word: str):"""Retrieves the second secret word.@@ -67,13 +68,13 @@ def second_secret_tool(server):raise RuntimeError(f"Expected secret {'v0iq020i0g'}, got {prev_secret_word}")return "4rwp2b4gxq"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=second_secret_word), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=second_secret_word), actor=actor)yield tool@pytest.fixture(scope="function")-def third_secret_tool(server):+async def third_secret_tool(server):def third_secret_word(prev_secret_word: str):"""Retrieves the third secret word.@@ -88,13 +89,13 @@ def third_secret_tool(server):raise RuntimeError(f'Expected secret "4rwp2b4gxq", got {prev_secret_word}')return "hj2hwibbqm"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=third_secret_word), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=third_secret_word), actor=actor)yield tool@pytest.fixture(scope="function")-def fourth_secret_tool(server):+async def fourth_secret_tool(server):def fourth_secret_word(prev_secret_word: str):"""Retrieves the final secret word.@@ -109,13 +110,13 @@ def fourth_secret_tool(server):raise RuntimeError(f"Expected secret {'hj2hwibbqm'}, got {prev_secret_word}")return "banana"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=fourth_secret_word), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=fourth_secret_word), actor=actor)yield tool@pytest.fixture(scope="function")-def flip_coin_tool(server):+async def flip_coin_tool(server):def flip_coin():"""Simulates a coin flip with a chance to return a secret word.@@ -127,13 +128,13 @@ def flip_coin_tool(server):return "" if random.random() < 0.5 else "hj2hwibbqm"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=flip_coin), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=flip_coin), actor=actor)yield tool@pytest.fixture(scope="function")-def can_play_game_tool(server):+async def can_play_game_tool(server):def can_play_game():"""Determines whether a game can be played.@@ -145,13 +146,13 @@ def can_play_game_tool(server):return random.random() < 0.5- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=can_play_game), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=can_play_game), actor=actor)yield tool@pytest.fixture(scope="function")-def return_none_tool(server):+async def return_none_tool(server):def return_none():"""Always returns None.@@ -161,13 +162,13 @@ def return_none_tool(server):"""return None- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=return_none), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=return_none), actor=actor)yield tool@pytest.fixture(scope="function")-def auto_error_tool(server):+async def auto_error_tool(server):def auto_error():"""Always raises an error when called.@@ -177,13 +178,13 @@ def auto_error_tool(server):"""raise RuntimeError("This should never be called.")- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=auto_error), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=auto_error), actor=actor)yield tool@pytest.fixture(scope="function")-def save_data_tool(server):+async def save_data_tool(server):def save_data():"""Saves important data before exiting.@@ -193,13 +194,13 @@ def save_data_tool(server):"""return "Data saved successfully"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=save_data), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=save_data), actor=actor)yield tool@pytest.fixture(scope="function")-def cleanup_temp_files_tool(server):+async def cleanup_temp_files_tool(server):def cleanup_temp_files():"""Cleans up temporary files before exiting.@@ -209,13 +210,13 @@ def cleanup_temp_files_tool(server):"""return "Temporary files cleaned up"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=cleanup_temp_files), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=cleanup_temp_files), actor=actor)yield tool@pytest.fixture(scope="function")-def validate_work_tool(server):+async def validate_work_tool(server):def validate_work():"""Validates that work is complete before exiting.@@ -225,14 +226,15 @@ def validate_work_tool(server):"""return "Work validation passed"- actor = server.user_manager.get_user_or_default()- tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=validate_work), actor=actor)+ actor = await server.user_manager.get_actor_or_default_async()+ tool = await server.tool_manager.create_or_update_tool_async(create_tool_from_func(func=validate_work), actor=actor)yield tool@pytest.fixture-def default_user(server):- yield server.user_manager.get_user_or_default()+async def default_user(server):+ actor = await server.user_manager.get_actor_or_default_async()+ yield actorasync def run_agent_step(agent_state, input_messages, actor):@@ -254,7 +256,7 @@ async def run_agent_step(agent_state, input_messages, actor):async def test_single_path_agent_tool_call_graph(server, disable_e2b_api_key, first_secret_tool, second_secret_tool, third_secret_tool, fourth_secret_tool, auto_error_tool, default_user):- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)# Add toolstools = [first_secret_tool, second_secret_tool, third_secret_tool, fourth_secret_tool, auto_error_tool]@@ -270,7 +272,7 @@ async def test_single_path_agent_tool_call_graph(]# Make agent state- agent_state = setup_agent(server, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)response = await run_agent_step(agent_state=agent_state,input_messages=[MessageCreate(role="user", content="What is the fourth secret word?")],@@ -301,7 +303,7 @@ async def test_single_path_agent_tool_call_graph(assert_invoked_send_message_with_keyword(response.messages, "banana")print(f"Got successful response from client: \n\n{response}")- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)@pytest.mark.timeout(60)@@ -314,7 +316,7 @@ async def test_single_path_agent_tool_call_graph(],)@pytest.mark.parametrize("init_tools_case", ["single", "multiple"])-def test_check_tool_rules_with_different_models_parametrized(+async def test_check_tool_rules_with_different_models_parametrized(server, disable_e2b_api_key, first_secret_tool, second_secret_tool, third_secret_tool, default_user, config_file, init_tools_case):"""Test that tool rules are properly validated across model configurations and init tool scenarios."""@@ -332,7 +334,7 @@ def test_check_tool_rules_with_different_models_parametrized(if "gpt-4o" in config_file or init_tools_case == "single":# Should succeed- agent_state = setup_agent(+ agent_state = await setup_agent(server,config_file,agent_uuid=agent_uuid,@@ -343,7 +345,7 @@ def test_check_tool_rules_with_different_models_parametrized(else:# Non-structured model with multiple init tools should failwith pytest.raises(ValueError, match="Multiple initial tools are not supported for non-structured models"):- setup_agent(+ await setup_agent(server,config_file,agent_uuid=agent_uuid,@@ -351,7 +353,7 @@ def test_check_tool_rules_with_different_models_parametrized(tool_rules=tool_rules,)- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)@pytest.mark.timeout(180)@@ -374,7 +376,7 @@ async def test_claude_initial_tool_rule_enforced(for i in range(3):agent_uuid = str(uuid.uuid4())- agent_state = setup_agent(+ agent_state = await setup_agent(server,anthropic_config_file,agent_uuid=agent_uuid,@@ -401,7 +403,7 @@ async def test_claude_initial_tool_rule_enforced(tool_names = tool_names[1:]print(f"Passed iteration {i}")- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)# Exponential backoffif i < 2:@@ -425,9 +427,9 @@ async def test_agent_no_structured_output_with_one_child_tool_parametrized(config_file,):"""Test that agent correctly calls tool chains with unstructured output under various model configs."""- send_message = server.tool_manager.get_tool_by_name(tool_name="send_message", actor=default_user)- archival_memory_search = server.tool_manager.get_tool_by_name(tool_name="archival_memory_search", actor=default_user)- archival_memory_insert = server.tool_manager.get_tool_by_name(tool_name="archival_memory_insert", actor=default_user)+ send_message = await server.tool_manager.get_tool_by_name_async(tool_name="send_message", actor=default_user)+ archival_memory_search = await server.tool_manager.get_tool_by_name_async(tool_name="archival_memory_search", actor=default_user)+ archival_memory_insert = await server.tool_manager.get_tool_by_name_async(tool_name="archival_memory_insert", actor=default_user)tools = [send_message, archival_memory_search, archival_memory_insert]@@ -444,7 +446,7 @@ async def test_agent_no_structured_output_with_one_child_tool_parametrized(for attempt in range(max_retries):try:- agent_state = setup_agent(+ agent_state = await setup_agent(server,config_file,agent_uuid=agent_uuid,@@ -476,12 +478,12 @@ async def test_agent_no_structured_output_with_one_child_tool_parametrized(except AssertionError as e:last_error = eprint(f"[{config_file}] Attempt {attempt + 1} failed")- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)if last_error:raise last_error- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)@pytest.mark.timeout(30)@@ -499,7 +501,7 @@ async def test_init_tool_rule_always_fails(agent_uuid = str(uuid.uuid4())tool_rule = InitToolRule(tool_name=auto_error_tool.name)- agent_state = setup_agent(+ agent_state = await setup_agent(server,config_file,agent_uuid=agent_uuid,@@ -516,7 +518,7 @@ async def test_init_tool_rule_always_fails(assert_invoked_function_call(response.messages, auto_error_tool.name)- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)@pytest.mark.asyncio@@ -525,17 +527,18 @@ async def test_continue_tool_rule(server, default_user):config_file = "tests/configs/llm_model_configs/claude-3-5-sonnet.json"agent_uuid = str(uuid.uuid4())- tool_ids = [- server.tool_manager.get_tool_by_name("send_message", actor=default_user).id,- server.tool_manager.get_tool_by_name("core_memory_append", actor=default_user).id,+ tools = [+ await server.tool_manager.get_tool_by_name_async("send_message", actor=default_user),+ await server.tool_manager.get_tool_by_name_async("core_memory_append", actor=default_user),]+ tool_ids = [t.id for t in tools]tool_rules = [ContinueToolRule(tool_name="send_message"),TerminalToolRule(tool_name="core_memory_append"),]- agent_state = setup_agent(+ agent_state = await setup_agent(server,config_file,agent_uuid,@@ -563,7 +566,7 @@ async def test_continue_tool_rule(server, default_user):)assert send_idx < append_idx, "send_message should occur before core_memory_append"- cleanup(server=server, agent_uuid=agent_uuid, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_uuid, actor=default_user)# @pytest.mark.timeout(60) # Sets a 60-second timeout for the test since this could loop infinitely@@ -605,7 +608,7 @@ async def test_continue_tool_rule(server, default_user):# tools = [flip_coin_tool, reveal_secret]## config_file = "tests/configs/llm_model_configs/claude-3-5-sonnet.json"-# agent_state = setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+# agent_state = await setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)# response = client.user_message(agent_id=agent_state.id, message="flip a coin until you get the secret word")## # Make checks@@ -667,7 +670,7 @@ async def test_continue_tool_rule(server, default_user):# tools = [tool, secret_word]## # Setup agent with all tools-# agent_state = setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+# agent_state = await setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)## # Ask agent to try different tools based on the game output# response = client.user_message(agent_id=agent_state.id, message="call a function, any function. then call send_message")@@ -729,7 +732,7 @@ async def test_continue_tool_rule(server, default_user):# tools = [flip_coin_tool, secret_word_tool]## # Setup initial agent-# agent_state = setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+# agent_state = await setup_agent(client, config_file, agent_uuid=agent_uuid, tool_ids=[t.id for t in tools], tool_rules=tool_rules)## # Call flip_coin first# response = client.user_message(agent_id=agent_state.id, message="flip a coin")@@ -782,7 +785,7 @@ async def test_continue_tool_rule(server, default_user):# )## # Set up agent with the tool rule-# agent_state = setup_agent(+# agent_state = await setup_agent(# client, config_file, agent_uuid, tool_rules=[tool_rule], tool_ids=[t.id for t in tools], include_base_tools=False# )#@@ -821,7 +824,7 @@ async def test_single_required_before_exit_tool(server, disable_e2b_api_key, sav]# Create agent- agent_state = setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)# Send message that would normally cause exitresponse = await run_agent_step(@@ -864,7 +867,7 @@ async def test_multiple_required_before_exit_tools(server, disable_e2b_api_key,]# Create agent- agent_state = setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)# Send message that would normally cause exitresponse = await run_agent_step(@@ -909,7 +912,7 @@ async def test_required_before_exit_with_other_rules(server, disable_e2b_api_key]# Create agent- agent_state = setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)# Send message that would trigger tool flowresponse = await run_agent_step(@@ -953,7 +956,7 @@ async def test_required_tools_called_during_normal_flow(server, disable_e2b_api_]# Create agent- agent_state = setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[t.id for t in tools], tool_rules=tool_rules)# Send message that explicitly mentions calling the required toolresponse = await run_agent_step(@@ -991,7 +994,7 @@ async def test_terminal_tool_rule_send_message_request_heartbeat_false(server, d]# Create agent- agent_state = setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[], tool_rules=tool_rules)+ agent_state = await setup_agent(server, config_file, agent_uuid=agent_name, tool_ids=[], tool_rules=tool_rules)# Send message that should trigger send_message tool callresponse = await run_agent_step(@@ -1023,4 +1026,4 @@ async def test_terminal_tool_rule_send_message_request_heartbeat_false(server, dexcept json.JSONDecodeError:pytest.fail("Failed to parse tool call arguments as JSON")finally:- cleanup(server=server, agent_uuid=agent_name, actor=default_user)+ await cleanup_async(server=server, agent_uuid=agent_name, actor=default_user)