Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/search): stop re-embedding unchanged multi-category blocks in every process (#15137)

Reinier van der Leerad5c3b8c6814
diff --git a/autogpt_platform/backend/backend/api/features/search/content_handlers.py b/autogpt_platform/backend/backend/api/features/search/content_handlers.py
index 4606c2bf2f..7d9414c853 100644
--- a/autogpt_platform/backend/backend/api/features/search/content_handlers.py
+++ b/autogpt_platform/backend/backend/api/features/search/content_handlers.py
@@ -334,13 +334,15 @@ def _build_block_content_item(block_id: str, block: AnyBlockSchema) -> ContentIt
if not block.name:
logger.warning(f"Block {block_id} has no name — using block_id as fallback")
display_name = split_camelcase(block.name) if block.name else ""
+ # Sorted: a set iterates in hash-seed order, and the stale check compares text.
+ categories = sorted(str(cat.value) for cat in block.categories)
parts = []
if display_name:
parts.append(display_name)
if block.description:
parts.append(block.description)
- if block.categories:
- parts.append(" ".join(str(cat.value) for cat in block.categories))
+ if categories:
+ parts.append(" ".join(categories))
# Add input schema field descriptions
parts += [
@@ -351,10 +353,6 @@ def _build_block_content_item(block_id: str, block: AnyBlockSchema) -> ContentIt
searchable_text = " ".join(parts)
- categories_list = (
- [cat.value for cat in block.categories] if block.categories else []
- )
-
# Extract provider names from credentials fields
credentials_info = block.input_schema.get_credentials_fields_info()
is_integration = len(credentials_info) > 0
@@ -376,7 +374,7 @@ def _build_block_content_item(block_id: str, block: AnyBlockSchema) -> ContentIt
searchable_text=searchable_text,
metadata={
"name": display_name or block.name or block_id,
- "categories": categories_list,
+ "categories": categories,
"providers": provider_names,
"has_llm_model_field": has_llm_model_field,
"is_integration": is_integration,
diff --git a/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py b/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py
index 692054459a..c1d9e0717f 100644
--- a/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py
+++ b/autogpt_platform/backend/backend/api/features/search/content_handlers_test.py
@@ -2,6 +2,9 @@
Tests for content handlers (blocks, store agents, documentation).
"""
+import os
+import subprocess
+import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -272,6 +275,38 @@ async def test_block_handler_reembeds_stale_searchable_text():
assert [item.content_id for item in items] == ["renamed-block"]
+def test_block_searchable_text_is_identical_across_processes():
+ """The stale check compares stored text with freshly built text, so text that
+ varies with the interpreter's hash seed re-embeds the block in every process."""
+ runs = [
+ subprocess.Popen(
+ [sys.executable, "-c", _BUILD_MULTI_CATEGORY_BLOCK_TEXT],
+ env={**os.environ, "PYTHONHASHSEED": seed},
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ )
+ for seed in ("0", "1", "2", "3")
+ ]
+ texts = set()
+ for run in runs:
+ out, err = run.communicate(timeout=120)
+ assert run.returncode == 0, err
+ texts.add(out)
+
+ assert len(texts) == 1, texts
+
+
+# Three categories, which these four seeds iterate in three different orders.
+_BUILD_MULTI_CATEGORY_BLOCK_TEXT = """
+from backend.api.features.search.content_handlers import _build_block_content_item
+from backend.blocks.text_to_speech_block import UnrealTextToSpeechBlock
+block = UnrealTextToSpeechBlock()
+assert len(block.categories) == 3, block.categories
+print(_build_block_content_item("block-id", block).searchable_text)
+"""
+
+
@pytest.mark.asyncio(loop_scope="session")
async def test_block_handler_get_missing_items_batch_size_zero():
"""batch_size=0 returns an empty list; the DB is still queried to find missing IDs."""