diff --git a/docs/ROLLOUT_GUIDE.md b/docs/ROLLOUT_GUIDE.md index 7db0cbc5..c30b438b 100644 --- a/docs/ROLLOUT_GUIDE.md +++ b/docs/ROLLOUT_GUIDE.md @@ -27,6 +27,11 @@ This guide shows how to run rollouts for the three environments using a single u Data path default: `data/gaia/val.json` +On Python 3.10+, unconfigured GAIA runs use free, keyless Parallel Search MCP. +Configured Google credentials and explicit `--gaia_tools` lists keep their +choices. See [GAIA web search](../openmanus_rl/tools/README.md#gaia-web-search) +for installation, provider selection and page fetching. + - Dry‑run (no model calls): - `python scripts/rollout/openmanus_rollout.py --env gaia --batch_size 2 --total_envs 4 --dry_run` diff --git a/openmanus_rl/environments/env_manager.py b/openmanus_rl/environments/env_manager.py index 9743c243..db181263 100644 --- a/openmanus_rl/environments/env_manager.py +++ b/openmanus_rl/environments/env_manager.py @@ -380,9 +380,13 @@ def make_envs(config): tasks_data = json.load(f) # Get available tools from config - available_tools = getattr(config.env, 'available_tools', [ - 'google_search', 'wikipedia_knowledge_searcher', 'arxiv_paper_searcher' - ]) + from openmanus_rl.tools.search_defaults import default_search_tool + if hasattr(config.env, 'available_tools'): + available_tools = config.env.available_tools + else: + available_tools = [ + default_search_tool(), 'wikipedia_knowledge_searcher', 'arxiv_paper_searcher' + ] # Build environments _envs = build_tool_use_envs( diff --git a/openmanus_rl/environments/env_package/tool_use/envs.py b/openmanus_rl/environments/env_package/tool_use/envs.py index 4c0ea2ac..b6be4a3c 100644 --- a/openmanus_rl/environments/env_package/tool_use/envs.py +++ b/openmanus_rl/environments/env_package/tool_use/envs.py @@ -32,6 +32,7 @@ def reset(self, task_idx: int = None) -> Tuple[str, Dict]: elif self.current_task_idx >= len(self.tasks_data): self.current_task_idx = 0 + self.tool_manager.start_session() task_data = self.tasks_data[self.current_task_idx] self.current_task_idx += 1 @@ -140,6 +141,7 @@ def _load_tool(self, tool_name: str): # Map tool names to their module paths tool_mapping = { 'google_search': 'openmanus_rl.tools.google_search.tool.Google_Search_Tool', + 'parallel_search': 'openmanus_rl.tools.parallel_search.tool.ParallelSearchTool', 'wikipedia_knowledge_searcher': 'openmanus_rl.tools.wikipedia_knowledge_searcher.tool.Wikipedia_Knowledge_Searcher_Tool', 'arxiv_paper_searcher': 'openmanus_rl.tools.arxiv_paper_searcher.tool.ArXiv_Paper_Searcher_Tool', 'pubmed_search': 'openmanus_rl.tools.pubmed_search.tool.Pubmed_Search_Tool', @@ -168,6 +170,12 @@ def _load_tool(self, tool_name: str): self.available_tools[tool_name] = tool_instance + def start_session(self): + """Start a research conversation for this environment's next task.""" + tool = self.available_tools.get('parallel_search') + if tool is not None: + tool.start_session() + def get_tools_metadata(self) -> str: """Generate formatted metadata for all available tools""" if not self.available_tools: diff --git a/openmanus_rl/tools/README.md b/openmanus_rl/tools/README.md index f947afaa..3715a29e 100644 --- a/openmanus_rl/tools/README.md +++ b/openmanus_rl/tools/README.md @@ -1,4 +1,48 @@ +## GAIA web search + +On Python 3.10+, GAIA/tool-use runs use [Parallel Search MCP](https://docs.parallel.ai/integrations/mcp/search-mcp) +when neither `GOOGLE_API_KEY` nor `GOOGLE_CX` is set. Search uses Fast mode and +needs no account or API key. The anonymous service has usage limits; errors are +returned to the agent without switching providers or retrying automatically. +Existing Google credentials, including credentials loaded from `.env`, keep +Google selected. Set both Google variables for a working Google configuration. + +Install the search dependencies in your existing rollout environment from the +repository root (they are also declared in the main dependency manifests): + +```sh +pip install -r requirements-search.txt +``` + +`scripts/rollout/gaia.py` and `scripts/rollout/openmanus_rollout.py --env gaia` +select this default automatically. To select it explicitly, use +`--tools parallel_search wikipedia_knowledge_searcher python_code_generator` +with `gaia.py`, or `--gaia_tools` with `openmanus_rollout.py`. Explicit tool lists +and `config.env.available_tools` are used as supplied. Omit search tools from +those lists to disable web search; an empty configuration list disables all tools. +`google_search` remains available explicitly, with no fallback after an error. +Python versions before 3.10 retain the Google default. + +The agent can search or fetch page excerpts using the same tool: + +```text + +tool: parallel_search +parameters: {"query": "2024 Nobel chemistry winners"} + + + +tool: parallel_search +parameters: {"urls": ["https://www.nobelprize.org/prizes/chemistry/2024/summary/"], "query": "Who won?"} + +``` + +Results include source URLs and excerpts in the next agent observation. Each +environment reset creates a new research session; related searches and fetches +within one task share it. Requests have a 30-second total timeout. This provider +does not configure the separate modular-stage `search` wrapper. + ## Testing the Tools To test the text detection tool, follow these steps: diff --git a/openmanus_rl/tools/parallel_search/__init__.py b/openmanus_rl/tools/parallel_search/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/openmanus_rl/tools/parallel_search/tool.py b/openmanus_rl/tools/parallel_search/tool.py new file mode 100644 index 00000000..e92c7401 --- /dev/null +++ b/openmanus_rl/tools/parallel_search/tool.py @@ -0,0 +1,113 @@ +"""Anonymous Parallel Search MCP tool for one GAIA research task.""" + +import asyncio +import uuid +from urllib.parse import urlsplit + +import anyio +import httpx +from mcp import ClientSession +from mcp.client.streamable_http import streamable_http_client + +from openmanus_rl.tools.base import BaseTool + +MCP_URL = "https://search.parallel.ai/mcp" +USER_AGENT = "OpenManus-RL/parallel-search (+https://github.com/OpenManus/OpenManus-RL)" + + +class ParallelSearchTool(BaseTool): + """Search and fetch using the same conversation ID until the task resets.""" + + def __init__(self, timeout=30): + super().__init__( + tool_name="parallel_search", + tool_description=( + "Search the web without an API key, or fetch relevant excerpts " + "from HTTP/HTTPS URLs. Supply query for search, urls for fetch." + ), + tool_version="1.0.0", + input_types={ + "query": "str - Search query, or optional focus for fetching URLs.", + "urls": "list[str] - Optional HTTP/HTTPS URLs to fetch (up to 20).", + }, + output_type="dict - Source URLs, titles and excerpts, or an error.", + ) + self.timeout = timeout + self.start_session() + + def start_session(self): + """Called by the owning ToolUseEnv when it starts a new task.""" + self.session_id = uuid.uuid4().hex + + async def _call(self, name, arguments): + # Connections are short lived; conversation identity is supplied by the + # task-owned tool, independently of the MCP transport's session ID. + with anyio.fail_after(self.timeout): + async with httpx.AsyncClient( + headers={"User-Agent": USER_AGENT}, + timeout=self.timeout, + ) as client, streamable_http_client( + MCP_URL, + http_client=client, + ) as (read, write, _): + async with ClientSession(read, write) as session: + await session.initialize() + result = await session.call_tool(name, arguments) + if result.isError: + return { + "error": "\n".join( + block.text + for block in result.content + if block.type == "text" + ) + } + if result.structuredContent is not None: + return result.structuredContent + return { + "output": "\n".join( + block.text + for block in result.content + if block.type == "text" + ) + } + + def execute(self, query="", urls=None): + if not isinstance(query, str): + return {"error": "query must be a string"} + if urls is not None: + if not isinstance(urls, list) or not 1 <= len(urls) <= 20: + return {"error": "urls must contain 1 to 20 HTTP/HTTPS URLs"} + for url in urls: + try: + parsed = urlsplit(url) if isinstance(url, str) else None + if ( + parsed is None + or parsed.scheme not in ("http", "https") + or not parsed.hostname + or parsed.username + or parsed.password + ): + return { + "error": "Only HTTP/HTTPS URLs without credentials are accepted" + } + except ValueError: + return {"error": "Invalid URL"} + name = "web_fetch" + arguments = {"urls": urls} + if query.strip(): + arguments["objective"] = query[:200] + else: + if not query.strip(): + return {"error": "A nonempty query is required for search"} + name = "web_search" + arguments = {"objective": query, "search_queries": [query]} + arguments["session_id"] = self.session_id + try: + return asyncio.run(self._call(name, arguments)) + except TimeoutError: + return {"error": "Parallel request timed out"} + except Exception as exc: + # Do not retry another provider or regenerate the conversation ID. + while getattr(exc, "exceptions", None) and len(exc.exceptions) == 1: + exc = exc.exceptions[0] + return {"error": f"Parallel request failed: {exc}"} diff --git a/openmanus_rl/tools/search_defaults.py b/openmanus_rl/tools/search_defaults.py new file mode 100644 index 00000000..2449cb8d --- /dev/null +++ b/openmanus_rl/tools/search_defaults.py @@ -0,0 +1,19 @@ +"""Search selection for unconfigured GAIA/tool-use environments.""" + +import os +import sys + + +def default_search_tool(): + # Match the existing Google tool's .env support, without overriding the + # environment. Explicit tool lists bypass this default entirely. + from dotenv import load_dotenv + + load_dotenv() + if ( + sys.version_info < (3, 10) + or os.getenv("GOOGLE_API_KEY") + or os.getenv("GOOGLE_CX") + ): + return "google_search" + return "parallel_search" diff --git a/pyproject.toml b/pyproject.toml index 84babdbf..a9acff50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,8 @@ authors = [ # Dependencies corresponding to install_requires in setup.py dependencies = [ + "mcp>=1.26,<2; python_version >= '3.10'", + "python-dotenv>=1,<2", "accelerate", "codetiming", "datasets", diff --git a/requirements-search.txt b/requirements-search.txt new file mode 100644 index 00000000..84db00b3 --- /dev/null +++ b/requirements-search.txt @@ -0,0 +1,3 @@ +# Optional GAIA/tool-use search dependencies (Python 3.10+). +mcp>=1.26,<2 +python-dotenv>=1,<2 diff --git a/requirements.txt b/requirements.txt index 3dda39de..b52d899a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ accelerate +mcp>=1.26,<2; python_version >= '3.10' +python-dotenv>=1,<2 codetiming datasets dill @@ -21,4 +23,4 @@ wandb packaging>=20.0 uvicorn fastapi -qwen-vl-utils[decord] \ No newline at end of file +qwen-vl-utils[decord] diff --git a/requirements_docker.txt b/requirements_docker.txt index 415c0b82..4b4618a5 100644 --- a/requirements_docker.txt +++ b/requirements_docker.txt @@ -1,4 +1,5 @@ # Core dependencies for OpenManus-RL in AMD vllm Docker +mcp>=1.26,<2; python_version >= '3.10' gymnasium==0.29.1 stable-baselines3==2.6.0 alfworld diff --git a/scripts/docker_setup.sh b/scripts/docker_setup.sh index 2b371afa..67fd06db 100755 --- a/scripts/docker_setup.sh +++ b/scripts/docker_setup.sh @@ -46,6 +46,7 @@ uv pip install -U openai uv pip install Ray uv pip install together uv pip install wikipedia python-dotenv requests +uv pip install -r requirements-search.txt echo "Environment setup complete!" ' diff --git a/scripts/rollout/gaia.py b/scripts/rollout/gaia.py index a7937e50..528cd5ae 100644 --- a/scripts/rollout/gaia.py +++ b/scripts/rollout/gaia.py @@ -19,6 +19,7 @@ from openmanus_rl.environments.env_manager import * from openai import OpenAI from together import Together +from openmanus_rl.tools.search_defaults import default_search_tool def build_env(env_name, tasks_data, available_tools, env_num=1, seed=1, history_length=2, max_steps=30): @@ -143,7 +144,7 @@ def load_gaia_tasks(data_path: str, max_tasks: int = None) -> List[dict]: parser.add_argument("--base_url", default=None, help="OpenAI-compatible base URL (e.g., vLLM http://127.0.0.1:8000/v1)") parser.add_argument("--chat_root", default=None, help="If set, save per-episode chat histories under this root") parser.add_argument("--data_path", default="data/gaia/val.json", help="Path to GAIA dataset") - parser.add_argument("--tools", nargs='+', default=['google_search', 'wikipedia_knowledge_searcher', 'python_code_generator'], + parser.add_argument("--tools", nargs='+', default=[default_search_tool(), 'wikipedia_knowledge_searcher', 'python_code_generator'], help="List of available tools") parser.add_argument("--dry_run", action="store_true", help="仅打印任务分配,不创建环境、不调用模型") args = parser.parse_args() diff --git a/scripts/rollout/openmanus_rollout.py b/scripts/rollout/openmanus_rollout.py index eacb9f96..cb5679f2 100644 --- a/scripts/rollout/openmanus_rollout.py +++ b/scripts/rollout/openmanus_rollout.py @@ -21,6 +21,7 @@ from openmanus_rl.environments.env_manager import * from openai import OpenAI from together import Together +from openmanus_rl.tools.search_defaults import default_search_tool try: import dotenv @@ -361,7 +362,7 @@ def main(): parser.add_argument("--gaia_data_path", default="data/gaia/val.json", help="Path to GAIA dataset") parser.add_argument("--gaia_tools", nargs='+', - default=['google_search', 'wikipedia_knowledge_searcher', 'python_code_generator'], + default=[default_search_tool(), 'wikipedia_knowledge_searcher', 'python_code_generator'], help="List of available tools for GAIA") parser.add_argument("--webshop_train", action="store_true", help="Use WebShop training set instead of test set") diff --git a/setup.py b/setup.py index 6d0fcf2a..ce497ef1 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,8 @@ __version__ = f.read().strip() install_requires = [ + "mcp>=1.26,<2; python_version >= '3.10'", + "python-dotenv>=1,<2", "accelerate", "codetiming", "datasets", @@ -90,4 +92,4 @@ include_package_data=True, long_description=long_description, long_description_content_type="text/markdown", -) \ No newline at end of file +) diff --git a/test/test_parallel_search.py b/test/test_parallel_search.py new file mode 100644 index 00000000..3cb780e6 --- /dev/null +++ b/test/test_parallel_search.py @@ -0,0 +1,412 @@ +"""GAIA caller tests using the real MCP client against a deterministic HTTP peer.""" + +import argparse +import asyncio +import json +import runpy +from pathlib import Path +from types import SimpleNamespace + +import httpx +import pytest + +import openmanus_rl.environments.env_package.tool_use.manager as tool_use_manager +import openmanus_rl.environments.env_package.tool_use.projection as tool_projection +from openmanus_rl.environments.env_package.tool_use.envs import ToolUseEnvs +from openmanus_rl.tools.parallel_search import tool as parallel +from openmanus_rl.tools.search_defaults import default_search_tool + + +@pytest.fixture(autouse=True) +def isolated_credentials(monkeypatch): + for key in ("GOOGLE_API_KEY", "GOOGLE_CX", "PARALLEL_API_KEY"): + monkeypatch.delenv(key, raising=False) + # A developer's saved .env must not affect the anonymous-path tests. + monkeypatch.setattr("dotenv.load_dotenv", lambda *args, **kwargs: False) + + +@pytest.fixture +def peer(monkeypatch): + requests = [] + behavior = {"mode": "ok"} + + async def handle(request): + data = json.loads(request.content) + requests.append((request, data)) + if "id" not in data: + return httpx.Response(202) + if data["method"] == "initialize": + result = { + "protocolVersion": data["params"]["protocolVersion"], + "capabilities": {"tools": {}}, + "serverInfo": {"name": "test-peer", "version": "1"}, + } + elif data["method"] == "tools/list": + result = { + "tools": [ + {"name": name, "inputSchema": {"type": "object", "properties": {}}} + for name in ("web_search", "web_fetch") + ] + } + else: + assert data["method"] == "tools/call" + mode = behavior["mode"] + if mode == "timeout": + await asyncio.sleep(5) + if mode in ("401", "429"): + return httpx.Response( + int(mode), + text="Permission denied" if mode == "401" else "Quota exceeded", + ) + if mode == "rpc_error": + return httpx.Response( + 200, + json={ + "jsonrpc": "2.0", + "id": data["id"], + "error": {"code": -32602, "message": "Invalid arguments"}, + }, + ) + if mode == "malformed": + return httpx.Response( + 200, text="{broken", headers={"content-type": "application/json"} + ) + output = { + "results": [ + { + "url": "https://example.org/source", + "title": "Source", + "excerpts": ["Useful evidence"], + } + ] + } + result = { + "content": [ + { + "type": "text", + "text": "Quota exceeded" + if mode == "tool_error" + else json.dumps(output), + } + ] + } + if mode == "tool_error": + result["isError"] = True + elif mode != "text": + result["structuredContent"] = output + return httpx.Response( + 200, json={"jsonrpc": "2.0", "id": data["id"], "result": result} + ) + + def factory(headers=None, timeout=None, auth=None): + return httpx.AsyncClient( + headers=headers, + timeout=timeout, + auth=auth, + transport=httpx.MockTransport(handle), + ) + + monkeypatch.setattr(parallel, "httpx", SimpleNamespace(AsyncClient=factory)) + return requests, behavior + + +def make_manager(tools=None, count=1): + if tools is None: + tools = [default_search_tool()] + envs = ToolUseEnvs( + [{"question": "Find evidence", "answer": "done", "pid": "test"}], + tools, + env_num=count, + ) + manager = tool_use_manager.ToolUseEnvironmentManager( + envs, + tool_projection.tool_use_projection, + SimpleNamespace(env=SimpleNamespace(history_length=2, max_steps=20)), + ) + manager.reset() + return manager + + +def action(query="web evidence", urls=None): + params = {"query": query} + if urls is not None: + params["urls"] = urls + return ( + "\ntool: parallel_search\nparameters: " + + json.dumps(params) + + "\n" + ) + + +def calls(requests): + return [data["params"] for _, data in requests if data["method"] == "tools/call"] + + +def test_observation_consumes_search_fetch_and_followup(peer): + requests, _ = peer + manager = make_manager() + for text in ( + action(), + action(urls=["https://example.org/source"]), + action("followup evidence"), + ): + obs, _, _, _ = manager.step([text]) + assert "Useful evidence" in obs["text"][0] + assert "https://example.org/source" in obs["text"][0] + sent = calls(requests) + assert [call["name"] for call in sent] == ["web_search", "web_fetch", "web_search"] + assert len({call["arguments"]["session_id"] for call in sent}) == 1 + for request, _ in requests: + assert str(request.url) == parallel.MCP_URL + assert request.headers["user-agent"] == parallel.USER_AGENT + assert "authorization" not in request.headers + + +def test_interleaved_tasks_reset_and_retry_keep_identity(peer): + requests, behavior = peer + manager = make_manager(count=2) + # Both environment instances use the same production transport factory. + first = [ + env.tool_manager.available_tools["parallel_search"].session_id + for env in manager.envs.envs + ] + assert first[0] != first[1] + manager.step([action(), action()]) + behavior["mode"] = "tool_error" + manager.step([action(), action()]) + behavior["mode"] = "ok" + manager.step([action(urls=["https://example.org/source"]), action()]) + assert [call["arguments"]["session_id"] for call in calls(requests)] == first * 3 + manager.reset() + manager.step([action(), action()]) + next_ids = [call["arguments"]["session_id"] for call in calls(requests)[-2:]] + assert len(set(first + next_ids)) == 4 + + +@pytest.mark.parametrize( + "variables", + [ + {}, + {"GOOGLE_API_KEY": "configured"}, + {"GOOGLE_CX": "configured"}, + {"GOOGLE_API_KEY": "key", "GOOGLE_CX": "cx"}, + ], +) +def test_default_respects_configured_credentials(monkeypatch, variables): + for key, value in variables.items(): + monkeypatch.setenv(key, value) + assert default_search_tool() == ( + "google_search" if variables else "parallel_search" + ) + + +@pytest.mark.parametrize( + "script,option", [("gaia.py", "--tools"), ("openmanus_rollout.py", "--gaia_tools")] +) +@pytest.mark.parametrize("explicit", [False, True]) +def test_actual_cli_parser_preserves_explicit_tools( + monkeypatch, script, option, explicit +): + class Parsed(Exception): + pass + + original = argparse.ArgumentParser.parse_args + captured = {} + + def parse(parser, *args, **kwargs): + captured.update(vars(original(parser, *args, **kwargs))) + raise Parsed + + monkeypatch.setattr(argparse.ArgumentParser, "parse_args", parse) + monkeypatch.setattr( + "sys.argv", + [script] + + (["--env", "gaia"] if script == "openmanus_rollout.py" else []) + + ([option, "google_search"] if explicit else []), + ) + with pytest.raises(Parsed): + runpy.run_path( + str(Path(__file__).parents[1] / "scripts/rollout" / script), + run_name="__main__", + ) + selected = captured[option[2:]] + assert selected[0] == ("google_search" if explicit else "parallel_search") + + +def test_disabled_and_unavailable_tools_do_not_send_requests(peer): + requests, _ = peer + manager = make_manager(tools=[]) + obs, _, _, _ = manager.step([action()]) + assert "not available" in obs["text"][0] + assert requests == [] + + +@pytest.mark.parametrize( + "mode", ["tool_error", "rpc_error", "401", "429", "malformed", "timeout"] +) +def test_errors_reach_observation_without_fallback(peer, mode): + requests, behavior = peer + manager = make_manager() + tool = manager.envs.envs[0].tool_manager.available_tools["parallel_search"] + tool.timeout = 1 + session_id = tool.session_id + behavior["mode"] = mode + obs, _, _, _ = manager.step([action()]) + assert "error" in obs["text"][0].lower() + assert tool.session_id == session_id + assert len(calls(requests)) == 1 + assert all(str(request.url) == parallel.MCP_URL for request, _ in requests) + + +def test_text_content_is_consumed(peer): + _, behavior = peer + behavior["mode"] = "text" + manager = make_manager() + obs, _, _, _ = manager.step([action()]) + assert "Useful evidence" in obs["text"][0] + + +@pytest.mark.parametrize( + "urls", + [ + ["file:///etc/passwd"], + ["https://user:pass@example.org"], + [], + "https://example.org", + ["https://["], + ], +) +def test_invalid_urls_never_reach_transport(peer, urls): + requests, _ = peer + manager = make_manager() + obs, _, _, _ = manager.step([action(urls=urls)]) + assert "error" in obs["text"][0].lower() + assert requests == [] + + +def test_cancellation_propagates_through_dispatcher(peer, monkeypatch): + manager = make_manager() + tool = manager.envs.envs[0].tool_manager.available_tools["parallel_search"] + session_id = tool.session_id + + class CancelledClient: + async def __aenter__(self): + raise asyncio.CancelledError + + async def __aexit__(self, *args): + return False + + monkeypatch.setattr( + parallel, + "httpx", + SimpleNamespace(AsyncClient=lambda **kwargs: CancelledClient()), + ) + with pytest.raises(asyncio.CancelledError): + manager.step([action()]) + assert tool.session_id == session_id + + +def test_concurrent_conversations_keep_distinct_ids(peer): + from concurrent.futures import ThreadPoolExecutor + + requests, _ = peer + managers = [make_manager(), make_manager()] + ids = [ + manager.envs.envs[0].tool_manager.available_tools["parallel_search"].session_id + for manager in managers + ] + with ThreadPoolExecutor(max_workers=2) as pool: + observations = list( + pool.map(lambda manager: manager.step([action()])[0], managers) + ) + assert all("Useful evidence" in obs["text"][0] for obs in observations) + assert {call["arguments"]["session_id"] for call in calls(requests)} == set(ids) + assert len(set(ids)) == 2 + + +def test_configured_google_is_executed_without_parallel_fallback(peer, monkeypatch): + requests, _ = peer + monkeypatch.setenv("GOOGLE_API_KEY", "saved-key") + monkeypatch.setenv("GOOGLE_CX", "saved-engine") + observed = [] + + def get(url, params, timeout): + observed.append((url, params, timeout)) + return SimpleNamespace( + status_code=403, + json=lambda: {"error": {"message": "Google denied the request"}}, + ) + + monkeypatch.setattr("requests.get", get) + manager = make_manager() + obs, _, _, _ = manager.step([action().replace("parallel_search", "google_search")]) + assert "Google denied" in obs["text"][0] + assert observed[0][1]["key"] == "saved-key" + assert observed[0][1]["cx"] == "saved-engine" + assert requests == [] + + +@pytest.mark.parametrize( + "configured", [None, [], ["google_search"], ["parallel_search"]] +) +def test_training_factory_preserves_saved_tool_list( + peer, monkeypatch, tmp_path, configured +): + from openmanus_rl.environments.env_manager import make_envs + + path = tmp_path / "tasks.json" + path.write_text( + json.dumps([{"question": "Find evidence", "answer": "done", "pid": "test"}]) + ) + env = SimpleNamespace( + env_name="tool_use", seed=42, data_path=str(path), rollout=SimpleNamespace(n=1) + ) + if configured is not None: + env.available_tools = configured + # Keep unrelated optional tools out of this focused factory test. + from openmanus_rl.environments.env_package.tool_use.envs import ToolManager + + original = ToolManager._load_tool + + def load(manager, name): + if name in ("google_search", "parallel_search"): + original(manager, name) + + monkeypatch.setattr(ToolManager, "_load_tool", load) + train, val = make_envs( + SimpleNamespace( + env=env, data=SimpleNamespace(train_batch_size=1, val_batch_size=1) + ) + ) + for manager in (train, val): + names = manager.envs.envs[0].available_tools + assert ( + names == configured + if configured is not None + else names[0] == "parallel_search" + ) + + +def test_saved_dotenv_credentials_are_respected(monkeypatch, tmp_path): + from dotenv.main import load_dotenv + + path = tmp_path / ".env" + path.write_text("GOOGLE_API_KEY=saved-key\nGOOGLE_CX=saved-engine\n") + monkeypatch.setattr("dotenv.load_dotenv", lambda: load_dotenv(path, override=False)) + try: + assert default_search_tool() == "google_search" + finally: + # load_dotenv writes directly to os.environ, outside monkeypatch tracking. + import os + + os.environ.pop("GOOGLE_API_KEY", None) + os.environ.pop("GOOGLE_CX", None) + + +@pytest.mark.parametrize("query", ["", " ", None, 42]) +def test_invalid_search_query_is_not_sent(peer, query): + requests, _ = peer + manager = make_manager() + obs, _, _, _ = manager.step([action(query=query)]) + assert "error" in obs["text"][0].lower() + assert requests == []