diff --git a/docs/advanced/configuration.mdx b/docs/advanced/configuration.mdx index fac557e38..38e79e500 100644 --- a/docs/advanced/configuration.mdx +++ b/docs/advanced/configuration.mdx @@ -148,7 +148,9 @@ affecting the agents that do the actual testing. API key for [Parallel Search](https://docs.parallel.ai/search/search-quickstart). Set `STRIX_WEB_SEARCH_PROVIDER=parallel` to use it for the built-in `web_search` tool. Requests use `/v1/search` in `fast` mode, returning up to five source URLs with - relevant excerpts. Output is capped at 12,000 characters plus a truncation notice. + relevant excerpts. Excerpts are shortened to keep output within 12,000 characters + plus a truncation notice. Source titles and URLs are always preserved, even if + those headers alone exceed the budget. Queries may contain up to 5,000 characters; the full query is the search objective, with its first 200 characters also supplied as a search query. This uses the authenticated API, not the free Search MCP. `web_get_contents` diff --git a/strix/tools/web_search/tool.py b/strix/tools/web_search/tool.py index 656701f35..6717dd4a3 100644 --- a/strix/tools/web_search/tool.py +++ b/strix/tools/web_search/tool.py @@ -136,6 +136,24 @@ def _exa_content(api_key: str, query: str, search_type: str, num_results: int) - _PARALLEL_MAX_CHARS = 12000 +def _parallel_result_content(results: list[tuple[str, str]]) -> str: + # Reserve every source header before spending the remaining budget on excerpts. + header_chars = sum(len(header) for header, _ in results) + 2 * (len(results) - 1) + remaining = max(0, _PARALLEL_MAX_CHARS - header_chars) + blocks: list[str] = [] + truncated = False + for header, excerpts in results: + excerpt = excerpts[: max(0, remaining - 2)] + blocks.append(f"{header}\n\n{excerpt}" if excerpt else header) + if excerpt: + remaining -= len(excerpt) + 2 + truncated |= len(excerpt) < len(excerpts) + content = "\n\n".join(blocks) + if truncated: + content += "\n[excerpts truncated to preserve source links]" + return content + + def _parallel_content(api_key: str, query: str) -> str: with requests.post( "https://api.parallel.ai/v1/search", @@ -159,7 +177,7 @@ def _parallel_content(api_key: str, query: str) -> str: if not results: return "No web search results found." - blocks: list[str] = [] + blocks: list[tuple[str, str]] = [] for item in results[:5]: if not isinstance(item, dict): continue @@ -167,19 +185,17 @@ def _parallel_content(api_key: str, query: str) -> str: if not result.get("url"): continue url = str(result["url"]) - parts = [f"### {result.get('title') or url}\n{url}"] + header = f"### {result.get('title') or url}\n{url}" + parts: list[str] = [] excerpts: list[Any] | None = result.get("excerpts") if isinstance(excerpts, list): parts.extend( text.strip() for text in excerpts if isinstance(text, str) and text.strip() ) - blocks.append("\n\n".join(parts)) + blocks.append((header, "\n\n".join(parts))) if not blocks: raise ValueError("Parallel response has no usable results") - content = "\n\n".join(blocks) - if len(content) > _PARALLEL_MAX_CHARS: - return f"{content[:_PARALLEL_MAX_CHARS]}\n[truncated at {_PARALLEL_MAX_CHARS} characters]" - return content + return _parallel_result_content(blocks) def _normalize_url(url: str) -> str: diff --git a/tests/test_web_search.py b/tests/test_web_search.py index 96ff9653f..20e2b2134 100644 --- a/tests/test_web_search.py +++ b/tests/test_web_search.py @@ -551,8 +551,50 @@ def test_parallel_caps_output(monkeypatch: pytest.MonkeyPatch) -> None: ) result = tool._parallel_content("key", "vendor advisory") assert result.startswith("### https://vendor.example\nhttps://vendor.example") - assert result.endswith("\n[truncated at 12000 characters]") - assert len(result) == 12000 + len("\n[truncated at 12000 characters]") + notice = "\n[excerpts truncated to preserve source links]" + assert result.endswith(notice) + assert len(result) == 12000 + len(notice) + + +@pytest.mark.parametrize("first_excerpt_size", [11920, 20000]) +def test_parallel_truncation_keeps_all_source_headers( + monkeypatch: pytest.MonkeyPatch, + first_excerpt_size: int, +) -> None: + results = [ + { + "title": f"Vendor advisory {index}", + "url": f"https://vendor.example/advisory/{index}/" + "a" * 100, + "excerpts": ["x" * (first_excerpt_size if index == 0 else 2000)], + } + for index in range(5) + ] + monkeypatch.setattr( + requests, + "post", + lambda *_a, **_kw: _FakeResponse({"results": results}), + ) + content = tool._parallel_content("key", "vendor advisories") + for result in results: + assert f"### {result['title']}\n{result['url']}" in content + notice = "\n[excerpts truncated to preserve source links]" + assert content.endswith(notice) + assert len(content) <= 12000 + len(notice) + + +@pytest.mark.parametrize("spare_chars", [-1, 0, 1, 2, 3]) +def test_parallel_keeps_headers_when_no_excerpt_fits( + monkeypatch: pytest.MonkeyPatch, + spare_chars: int, +) -> None: + headers = ["### First\nhttps://first.example", "### Second\nhttps://second.example"] + header_text = "\n\n".join(headers) + monkeypatch.setattr(tool, "_PARALLEL_MAX_CHARS", len(header_text) + spare_chars) + content = tool._parallel_result_content([(header, "excerpt") for header in headers]) + assert all(header in content for header in headers) + notice = "\n[excerpts truncated to preserve source links]" + assert content.endswith(notice) + assert len(content) <= max(len(header_text), tool._PARALLEL_MAX_CHARS) + len(notice) @pytest.mark.usefixtures("_parallel_settings")