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.
feat(backend/copilot): require library similarity check before create_agent (#13080)
An Vy Lea11174b7cfa4
diff --git a/autogpt_platform/backend/backend/api/features/library/db.py b/autogpt_platform/backend/backend/api/features/library/db.pyindex bfcfcbc567..f427f0693f 100644--- a/autogpt_platform/backend/backend/api/features/library/db.py+++ b/autogpt_platform/backend/backend/api/features/library/db.py@@ -38,6 +38,7 @@ from backend.util.models import Paginationfrom backend.util.settings import Configfrom . import model as library_model+from .embeddings import schedule_library_agent_embeddinglogger = logging.getLogger(__name__)config = Config()@@ -597,9 +598,12 @@ async def create_library_agent())- # Generate images for the main graph and sub-graphs+ # Generate images for the main graph and sub-graphs, and refresh the+ # library-agent embedding so the create-time similarity gate can find+ # this agent next time the user describes a goal.for agent, graph in zip(library_agents, graph_entries):asyncio.create_task(add_generated_agent_image(graph, user_id, agent.id))+ schedule_library_agent_embedding(agent.id, user_id, graph)schedule_info = await _fetch_schedule_info(user_id)return [@@ -761,6 +765,9 @@ async def update_library_agent_version_and_settings(user_id=user_id,settings=updated_settings,)+ # Re-embed so name/description/instructions changes are reflected in+ # similarity search results before the next create-time gate runs.+ schedule_library_agent_embedding(library.id, user_id, agent_graph)return librarydiff --git a/autogpt_platform/backend/backend/api/features/library/embeddings.py b/autogpt_platform/backend/backend/api/features/library/embeddings.pynew file mode 100644index 0000000000..f9b45eabab--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/library/embeddings.py@@ -0,0 +1,93 @@+"""Background embedding generation for LibraryAgent rows.++LibraryAgent embeddings power the "similar agents in your library" check+that runs before CoPilot creates a new agent. Generation is fire-and-forget+so user-facing latency on create/update is unaffected; failures are logged+and swallowed because a missing embedding only degrades search quality, it+never breaks correctness.+"""++from __future__ import annotations++import asyncio+import logging++from prisma.enums import ContentType++from backend.api.features.store.embeddings import (+ ensure_content_embedding,+ get_content_embedding,+)+from backend.data import graph as graph_db++logger = logging.getLogger(__name__)++# ``asyncio.create_task`` only registers the task in a *weak* set on the+# event loop, so a fire-and-forget task with no other strong reference can+# be garbage-collected mid-run. The asyncio docs explicitly warn about+# this and recommend keeping the task object alive elsewhere — see the+# "Important" note at+# https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task.+# Our callers in ``library/db.py`` don't await the task (intentional+# fire-and-forget), so we hold it here and let it remove itself via+# ``add_done_callback`` once it finishes.+_background_tasks: set[asyncio.Task[None]] = set()+++def _build_searchable_text(graph: graph_db.GraphModel) -> str:+ parts = [+ graph.name or "",+ graph.description or "",+ graph.instructions or "",+ ]+ return " ".join(part for part in parts if part).strip()+++async def _run_embedding(+ library_agent_id: str, user_id: str, graph: graph_db.GraphModel+) -> None:+ try:+ searchable_text = _build_searchable_text(graph)+ if not searchable_text:+ logger.debug(+ "Skipping library agent embedding for %s: empty searchable text",+ library_agent_id,+ )+ return+ # Skip the OpenAI call when the existing embedding's text is byte-+ # identical — e.g. settings-only updates where the version bumped+ # but name/description/instructions did not change.+ existing = await get_content_embedding(+ ContentType.LIBRARY_AGENT, library_agent_id, user_id+ )+ if existing and existing.get("searchableText") == searchable_text:+ logger.debug(+ "Library agent %s embedding text unchanged; skipping refresh",+ library_agent_id,+ )+ return+ await ensure_content_embedding(+ content_type=ContentType.LIBRARY_AGENT,+ content_id=library_agent_id,+ searchable_text=searchable_text,+ metadata={"name": graph.name or ""},+ user_id=user_id,+ force=True,+ )+ except Exception as e:+ logger.warning(+ "Failed to ensure library agent embedding for %s: %s",+ library_agent_id,+ e,+ )+++def schedule_library_agent_embedding(+ library_agent_id: str, user_id: str, graph: graph_db.GraphModel+) -> asyncio.Task[None]:+ """Schedule a background (re-)embed. No-ops cheaply when the existing+ embedding's searchableText is unchanged. Failures are logged, not raised."""+ task = asyncio.create_task(_run_embedding(library_agent_id, user_id, graph))+ _background_tasks.add(task)+ task.add_done_callback(_background_tasks.discard)+ return taskdiff --git a/autogpt_platform/backend/backend/api/features/library/embeddings_test.py b/autogpt_platform/backend/backend/api/features/library/embeddings_test.pynew file mode 100644index 0000000000..a90517b984--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/library/embeddings_test.py@@ -0,0 +1,138 @@+"""Tests for the library-agent embedding scheduler."""++import asyncio+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.api.features.library.embeddings import (+ _build_searchable_text,+ _run_embedding,+ schedule_library_agent_embedding,+)+++def _mock_graph(name="A", description="B", instructions="C"):+ g = MagicMock()+ g.name = name+ g.description = description+ g.instructions = instructions+ return g+++def test_build_searchable_text_concatenates_present_fields():+ text = _build_searchable_text(_mock_graph("Email Bot", "Sends emails", ""))+ assert "Email Bot" in text+ assert "Sends emails" in text+++def test_build_searchable_text_skips_empty_fields():+ text = _build_searchable_text(_mock_graph("", "", ""))+ assert text == ""+++async def test_run_embedding_skips_when_text_is_empty():+ """No call to ensure_content_embedding when there's nothing to embed."""+ with patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(return_value=True),+ ) as mock_ensure:+ await _run_embedding("la-1", "user-1", _mock_graph("", "", ""))+ mock_ensure.assert_not_called()+++async def test_run_embedding_forwards_user_scope_and_force_true():+ with (+ patch(+ "backend.api.features.library.embeddings.get_content_embedding",+ new=AsyncMock(return_value=None),+ ),+ patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(return_value=True),+ ) as mock_ensure,+ ):+ await _run_embedding("la-1", "user-1", _mock_graph("X", "Y", ""))+ kwargs = mock_ensure.call_args.kwargs+ assert kwargs["content_id"] == "la-1"+ assert kwargs["user_id"] == "user-1"+ assert kwargs["force"] is True+++async def test_run_embedding_skips_when_searchable_text_unchanged():+ """If the existing embedding row already carries the same text, skip+ the OpenAI call — covers settings-only updates that bumped the graph+ version but didn't touch name/description/instructions."""+ graph = _mock_graph("X", "Y", "Z")+ existing = {"searchableText": "X Y Z"}+ with (+ patch(+ "backend.api.features.library.embeddings.get_content_embedding",+ new=AsyncMock(return_value=existing),+ ),+ patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(return_value=True),+ ) as mock_ensure,+ ):+ await _run_embedding("la-1", "user-1", graph)+ mock_ensure.assert_not_called()+++async def test_run_embedding_refreshes_when_text_changed():+ """When the existing embedding's text differs, re-embed."""+ graph = _mock_graph("X", "Y", "Z")+ existing = {"searchableText": "old text"}+ with (+ patch(+ "backend.api.features.library.embeddings.get_content_embedding",+ new=AsyncMock(return_value=existing),+ ),+ patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(return_value=True),+ ) as mock_ensure,+ ):+ await _run_embedding("la-1", "user-1", graph)+ mock_ensure.assert_awaited_once()+++async def test_run_embedding_swallows_failures():+ """Failure to embed must not propagate — it's a best-effort background task."""+ with (+ patch(+ "backend.api.features.library.embeddings.get_content_embedding",+ new=AsyncMock(return_value=None),+ ),+ patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(side_effect=RuntimeError("openai down")),+ ),+ ):+ # Must not raise.+ await _run_embedding("la-1", "user-1", _mock_graph())+++async def test_schedule_returns_task_and_runs_in_background():+ with (+ patch(+ "backend.api.features.library.embeddings.get_content_embedding",+ new=AsyncMock(return_value=None),+ ),+ patch(+ "backend.api.features.library.embeddings.ensure_content_embedding",+ new=AsyncMock(return_value=True),+ ) as mock_ensure,+ ):+ task = schedule_library_agent_embedding(+ "la-1", "user-1", _mock_graph("X", "Y", "")+ )+ assert isinstance(task, asyncio.Task)+ await task+ mock_ensure.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/api/features/library/search.py b/autogpt_platform/backend/backend/api/features/library/search.pynew file mode 100644index 0000000000..17cce284b3--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/library/search.py@@ -0,0 +1,195 @@+"""Hybrid semantic + lexical search over a user's library agents.++Used by the CoPilot ``find_library_agent`` tool's ``for_creation`` mode and+by the ``create_agent`` similarity gate. Library-agent embeddings live in+the ``UnifiedContentEmbedding`` table scoped by ``userId``; we delegate the+actual ranking to ``unified_hybrid_search`` (via the ``db_accessors.search``+shim) so we inherit its graceful degradation (lexical-only fallback when+the embedding API is unavailable) and its BM25 reranking — and so the+call works whether Prisma is connected in-process or only available via+the database-manager RPC service.+"""++from __future__ import annotations++import logging+import re+from typing import Any++from prisma.enums import ContentType++from backend.api.features.store.hybrid_search import UnifiedSearchWeights+from backend.data.db_accessors import search++logger = logging.getLogger(__name__)++# Minimum combined relevance score for a library agent to be considered+# "functionally similar" enough to recommend before creating a new one.+# Calibrated empirically against the 9-near-duplicate "YouTube Video+# Summarizer" corpus: with ``_LIBRARY_SEARCH_WEIGHTS`` a true match scores+# ~0.91, an unrelated query (email / weather) tops out at ~0.29, so 0.55+# sits in the middle of a 0.62-wide separation gap.+LIBRARY_SIMILARITY_THRESHOLD = 0.55++# Library-agent search weights. Differ from ``DEFAULT_UNIFIED_WEIGHTS``+# (40/40/10/10) only in that ``category`` is zero — LibraryAgent metadata+# has no categories, and the 10% would just dilute the other signals.+# Semantic stays slightly dominant over lexical so paraphrased goals+# ("draft an agent that watches YouTube and writes notes") still catch a+# match where the lexical keyword form drops out. See+# ``_extract_lexical_keywords`` for why lexical needs the keyword form.+_LIBRARY_SEARCH_WEIGHTS = UnifiedSearchWeights(+ semantic=0.50,+ lexical=0.40,+ category=0.0,+ recency=0.10,+)++# English stopwords + a few command verbs (``build``, ``create``, ``make``)+# that are common in user goals but are pure noise to a tsvector match.+# Kept short on purpose: PostgreSQL's ``english`` text-search config+# already strips most stopwords during ``to_tsvector`` — this list only+# trims tokens that the user is *likely* to type and that would+# otherwise eat slots in the keyword budget (see ``_MAX_KEYWORDS``).+_STOPWORDS: frozenset[str] = frozenset(+ {+ "a",+ "about",+ "an",+ "and",+ "any",+ "are",+ "as",+ "at",+ "be",+ "build",+ "by",+ "can",+ "create",+ "do",+ "does",+ "for",+ "from",+ "has",+ "have",+ "how",+ "i",+ "in",+ "is",+ "it",+ "its",+ "make",+ "me",+ "my",+ "of",+ "on",+ "or",+ "our",+ "should",+ "some",+ "that",+ "the",+ "their",+ "them",+ "they",+ "this",+ "to",+ "us",+ "want",+ "was",+ "we",+ "were",+ "what",+ "when",+ "where",+ "which",+ "who",+ "why",+ "will",+ "with",+ "would",+ "you",+ "your",+ }+)++_MAX_KEYWORDS = 5+# Strip surrounding punctuation but keep intra-word characters (so+# ``CamelCase``, ``email-summariser`` etc. survive intact).+_TOKEN_SPLIT_RE = re.compile(r"\s+")+++def _extract_lexical_keywords(text: str, max_keywords: int = _MAX_KEYWORDS) -> str:+ """Return up to ``max_keywords`` distinctive tokens from ``text``.++ ``plainto_tsquery`` AND-joins every word, so a long natural-language+ goal ("Summarize a YouTube video with timestamped bullet points and a+ topic summary from a URL input") zeroes the ``@@`` match against any+ agent description that doesn't contain *all* the terms. Feeding it a+ short keyword string ("youtube video summarize timestamped bullet")+ keeps the AND semantics but lets near-duplicate agents match.++ Stopwords are dropped, tokens shorter than 3 chars are skipped (one-+ and two-letter tokens carry little signal and inflate the AND), and+ duplicates are de-duped while preserving original order.+ """+ keywords: list[str] = []+ seen: set[str] = set()+ for raw in _TOKEN_SPLIT_RE.split(text):+ token = raw.lower().strip(".,!?;:'\"()[]{}<>")+ if len(token) < 3 or token in _STOPWORDS or token in seen:+ continue+ seen.add(token)+ keywords.append(token)+ if len(keywords) >= max_keywords:+ break+ return " ".join(keywords)+++async def hybrid_search_library_agents(+ query: str,+ user_id: str,+ limit: int = 5,+ min_score: float = LIBRARY_SIMILARITY_THRESHOLD,+) -> list[dict[str, Any]]:+ """Search the user's library agents by hybrid relevance.++ Args:+ query: The user's goal text (free-form).+ user_id: Owner of the library agents to search.+ limit: Maximum number of matches to return.+ min_score: Minimum combined relevance to keep a match.++ Returns:+ A list of result dicts ordered by relevance. Each row contains at+ least ``content_id`` (the LibraryAgent id) and ``combined_score`` /+ ``relevance``. Returns ``[]`` when the query is empty.+ """+ query = (query or "").strip()+ if not query:+ return []++ # Semantic path keeps the full sentence (richer context for the+ # embedding); lexical path uses a stopword-stripped keyword form so+ # plainto_tsquery's AND-of-terms doesn't zero out every match.+ lexical_query = _extract_lexical_keywords(query) or query++ # Query at min_score=0 so we can log sub-threshold near-misses for+ # retuning, then filter to ``min_score`` before returning.+ raw_results, _total = await search().unified_hybrid_search(+ query=query,+ content_types=[ContentType.LIBRARY_AGENT],+ page=1,+ page_size=max(1, limit),+ min_score=0.0,+ user_id=user_id,+ weights=_LIBRARY_SEARCH_WEIGHTS,+ lexical_query=lexical_query,+ )+ if raw_results:+ logger.info(+ "Library similarity scan (threshold=%.2f): top scores=%s",+ min_score,+ [round(r.get("combined_score") or 0.0, 3) for r in raw_results[:5]],+ )+ return [r for r in raw_results if (r.get("combined_score") or 0.0) >= min_score]diff --git a/autogpt_platform/backend/backend/api/features/library/search_test.py b/autogpt_platform/backend/backend/api/features/library/search_test.pynew file mode 100644index 0000000000..40a7dfa61b--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/library/search_test.py@@ -0,0 +1,147 @@+"""Tests for hybrid_search_library_agents."""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.api.features.library.search import (+ _extract_lexical_keywords,+ hybrid_search_library_agents,+)+++def _patch_search(return_value):+ """Patch the db_accessors.search() shim to return a mock module/client+ whose ``unified_hybrid_search`` returns ``return_value``."""+ mock_shim = MagicMock()+ mock_shim.unified_hybrid_search = AsyncMock(return_value=return_value)+ return (+ patch(+ "backend.api.features.library.search.search",+ return_value=mock_shim,+ ),+ mock_shim,+ )+++async def test_returns_empty_list_for_empty_query():+ """Empty/whitespace query short-circuits without calling the DB."""+ patcher, mock_shim = _patch_search(([], 0))+ with patcher:+ result = await hybrid_search_library_agents(query=" ", user_id="u1")+ assert result == []+ mock_shim.unified_hybrid_search.assert_not_called()+++async def test_delegates_to_unified_hybrid_search_with_user_scope():+ """Delegates to unified_hybrid_search with user_id + an unfiltered+ min_score=0 query (the wrapper does its own threshold filtering so+ sub-threshold scores can be logged for retuning)."""+ rows = [{"content_id": "lib-1", "combined_score": 0.82}]+ patcher, mock_shim = _patch_search((rows, 1))+ with patcher:+ result = await hybrid_search_library_agents(+ query="summarise my email", user_id="user-42", limit=3+ )++ assert result == rows+ mock_shim.unified_hybrid_search.assert_awaited_once()+ kwargs = mock_shim.unified_hybrid_search.call_args.kwargs+ assert kwargs["user_id"] == "user-42"+ assert kwargs["page_size"] == 3+ assert kwargs["min_score"] == 0.0+ assert len(kwargs["content_types"]) == 1+++async def test_filters_sub_threshold_results_after_querying():+ """Underlying query returns all candidates; wrapper filters to+ ``min_score`` (default LIBRARY_SIMILARITY_THRESHOLD) so the caller+ only sees the strong matches."""+ rows = [+ {"content_id": "strong", "combined_score": 0.80},+ {"content_id": "weak", "combined_score": 0.30},+ ]+ patcher, _ = _patch_search((rows, 2))+ with patcher:+ result = await hybrid_search_library_agents(+ query="summarise my email", user_id="user-42"+ )++ assert [r["content_id"] for r in result] == ["strong"]+++async def test_respects_explicit_min_score_override():+ """Caller-supplied ``min_score`` overrides the default threshold."""+ rows = [+ {"content_id": "a", "combined_score": 0.40},+ {"content_id": "b", "combined_score": 0.20},+ ]+ patcher, _ = _patch_search((rows, 2))+ with patcher:+ result = await hybrid_search_library_agents(+ query="x", user_id="u", min_score=0.35+ )+ assert [r["content_id"] for r in result] == ["a"]+++async def test_forwards_keyword_extracted_lexical_query():+ """Semantic uses the full sentence; lexical gets the stopword-stripped+ keyword form so plainto_tsquery's AND-of-terms doesn't zero out every+ match against an agent description."""+ rows: list[dict] = []+ patcher, mock_shim = _patch_search((rows, 0))+ with patcher:+ await hybrid_search_library_agents(+ query=(+ "Summarize a YouTube video with timestamped bullet points "+ "and topic summary from a URL input"+ ),+ user_id="u",+ )++ kwargs = mock_shim.unified_hybrid_search.call_args.kwargs+ # Full sentence preserved for embedding+ assert kwargs["query"].startswith("Summarize a YouTube video")+ # Lexical query: short keyword string, stopwords + short tokens dropped+ lex = kwargs["lexical_query"].split()+ assert len(lex) <= 5+ assert "the" not in lex and "a" not in lex and "from" not in lex+ assert "summarize" in lex+ assert "youtube" in lex+ assert "video" in lex+++def test_extract_lexical_keywords_drops_stopwords_and_short_tokens():+ out = _extract_lexical_keywords("Build me an agent to summarize my emails")+ tokens = out.split()+ # 'Build', 'me', 'an', 'to', 'my' are stopwords/short → dropped+ assert "agent" in tokens+ assert "summarize" in tokens+ assert "emails" in tokens+ assert "build" not in tokens and "me" not in tokens and "to" not in tokens+++def test_extract_lexical_keywords_caps_count():+ out = _extract_lexical_keywords(+ "alpha beta gamma delta epsilon zeta eta theta", max_keywords=5+ )+ assert out.split() == ["alpha", "beta", "gamma", "delta", "epsilon"]+++def test_extract_lexical_keywords_dedupes_preserving_order():+ out = _extract_lexical_keywords("youtube video youtube summarizer video")+ assert out.split() == ["youtube", "video", "summarizer"]+++def test_extract_lexical_keywords_empty_when_only_stopwords():+ assert _extract_lexical_keywords("a the of to and") == ""+++# Note: caller-supplied ``min_score`` is now enforced inside the wrapper+# (so sub-threshold scores can be logged); see+# ``test_respects_explicit_min_score_override`` above.diff --git a/autogpt_platform/backend/backend/api/features/store/content_handlers.py b/autogpt_platform/backend/backend/api/features/store/content_handlers.pyindex d7cf5c6667..76bc2f2333 100644--- a/autogpt_platform/backend/backend/api/features/store/content_handlers.py+++ b/autogpt_platform/backend/backend/api/features/store/content_handlers.py@@ -644,9 +644,96 @@ class DocumentationHandler(ContentHandler):}+class LibraryAgentHandler(ContentHandler):+ """Handler for user-scoped library agents.++ Library agents are private (one row per user) so embeddings carry a+ non-null userId. The unique key on (contentType, contentId, userId)+ keeps the same library_agent_id from colliding across users that may+ have forked the same graph.+ """++ @property+ def content_type(self) -> ContentType:+ return ContentType.LIBRARY_AGENT++ async def get_missing_items(self, batch_size: int) -> list[ContentItem]:+ missing = await query_raw_with_schema(+ """+ SELECT+ la.id,+ la."userId",+ g.name,+ g.description,+ g.instructions+ FROM {schema_prefix}"LibraryAgent" la+ JOIN {schema_prefix}"AgentGraph" g+ ON g.id = la."agentGraphId" AND g.version = la."agentGraphVersion"+ LEFT JOIN {schema_prefix}"UnifiedContentEmbedding" uce+ ON uce."contentId" = la.id+ AND uce."contentType" = 'LIBRARY_AGENT'::{schema_prefix}"ContentType"+ AND uce."userId" = la."userId"+ WHERE la."isDeleted" = false+ AND la."isHidden" = false+ AND uce."contentId" IS NULL+ LIMIT $1+ """,+ batch_size,+ )++ items: list[ContentItem] = []+ for row in missing:+ name = row["name"] or ""+ description = row["description"] or ""+ instructions = row["instructions"] or ""+ searchable_text = " ".join(+ part for part in (name, description, instructions) if part+ )+ items.append(+ ContentItem(+ content_id=row["id"],+ content_type=ContentType.LIBRARY_AGENT,+ searchable_text=searchable_text,+ metadata={"name": name},+ user_id=row["userId"],+ )+ )+ return items++ async def get_stats(self) -> dict[str, int]:+ total_result = await query_raw_with_schema(+ """+ SELECT COUNT(*) as count+ FROM {schema_prefix}"LibraryAgent"+ WHERE "isDeleted" = false AND "isHidden" = false+ """+ )+ total = total_result[0]["count"] if total_result else 0++ with_result = await query_raw_with_schema(+ """+ SELECT COUNT(*) as count+ FROM {schema_prefix}"LibraryAgent" la+ JOIN {schema_prefix}"UnifiedContentEmbedding" uce+ ON uce."contentId" = la.id+ AND uce."contentType" = 'LIBRARY_AGENT'::{schema_prefix}"ContentType"+ AND uce."userId" = la."userId"+ WHERE la."isDeleted" = false AND la."isHidden" = false+ """+ )+ with_embeddings = with_result[0]["count"] if with_result else 0++ return {+ "total": total,+ "with_embeddings": with_embeddings,+ "without_embeddings": total - with_embeddings,+ }++# Content handler registryCONTENT_HANDLERS: dict[ContentType, ContentHandler] = {ContentType.STORE_AGENT: StoreAgentHandler(),ContentType.BLOCK: BlockHandler(),ContentType.DOCUMENTATION: DocumentationHandler(),+ ContentType.LIBRARY_AGENT: LibraryAgentHandler(),}diff --git a/autogpt_platform/backend/backend/api/features/store/content_handlers_test.py b/autogpt_platform/backend/backend/api/features/store/content_handlers_test.pyindex 45be949ae1..0dfd9b5a86 100644--- a/autogpt_platform/backend/backend/api/features/store/content_handlers_test.py+++ b/autogpt_platform/backend/backend/api/features/store/content_handlers_test.py@@ -12,6 +12,7 @@ from backend.api.features.store.content_handlers import (CONTENT_HANDLERS,BlockHandler,DocumentationHandler,+ LibraryAgentHandler,StoreAgentHandler,_get_enabled_blocks,)@@ -554,7 +555,70 @@ async def test_content_handlers_registry():assert ContentType.STORE_AGENT in CONTENT_HANDLERSassert ContentType.BLOCK in CONTENT_HANDLERSassert ContentType.DOCUMENTATION in CONTENT_HANDLERS+ assert ContentType.LIBRARY_AGENT in CONTENT_HANDLERSassert isinstance(CONTENT_HANDLERS[ContentType.STORE_AGENT], StoreAgentHandler)assert isinstance(CONTENT_HANDLERS[ContentType.BLOCK], BlockHandler)assert isinstance(CONTENT_HANDLERS[ContentType.DOCUMENTATION], DocumentationHandler)+ assert isinstance(CONTENT_HANDLERS[ContentType.LIBRARY_AGENT], LibraryAgentHandler)++[email protected](loop_scope="session")+async def test_library_agent_handler_emits_user_scoped_items():+ """get_missing_items must return one ContentItem per (user, library agent),+ with non-null user_id and searchable_text combining name + description ++ instructions."""+ rows = [+ {+ "id": "la-1",+ "userId": "user-a",+ "name": "Email Bot",+ "description": "Summarises emails",+ "instructions": "Run nightly",+ },+ {+ "id": "la-2",+ "userId": "user-b",+ "name": "Inbox Triage",+ "description": "",+ "instructions": None,+ },+ ]+ with patch(+ "backend.api.features.store.content_handlers.query_raw_with_schema",+ return_value=rows,+ ):+ handler = LibraryAgentHandler()+ items = await handler.get_missing_items(batch_size=10)++ assert len(items) == 2+ assert items[0].content_id == "la-1"+ assert items[0].user_id == "user-a"+ assert items[0].content_type == ContentType.LIBRARY_AGENT+ assert "Email Bot" in items[0].searchable_text+ assert "Summarises emails" in items[0].searchable_text+ assert "Run nightly" in items[0].searchable_text+ assert items[1].content_id == "la-2"+ assert items[1].user_id == "user-b"+ assert items[1].searchable_text == "Inbox Triage"++[email protected](loop_scope="session")+async def test_library_agent_handler_stats():+ """get_stats counts total non-deleted/non-hidden library agents vs.+ those with an embedding row."""++ async def fake_query_raw(sql: str, *args, **kwargs):+ if "uce" in sql or "UnifiedContentEmbedding" in sql:+ return [{"count": 4}]+ return [{"count": 10}]++ with patch(+ "backend.api.features.store.content_handlers.query_raw_with_schema",+ side_effect=fake_query_raw,+ ):+ stats = await LibraryAgentHandler().get_stats()++ assert stats["total"] == 10+ assert stats["with_embeddings"] == 4+ assert stats["without_embeddings"] == 6diff --git a/autogpt_platform/backend/backend/api/features/store/embeddings.py b/autogpt_platform/backend/backend/api/features/store/embeddings.pyindex 50e869e62f..b9920a7c49 100644--- a/autogpt_platform/backend/backend/api/features/store/embeddings.py+++ b/autogpt_platform/backend/backend/api/features/store/embeddings.py@@ -462,6 +462,7 @@ async def backfill_all_content_types(batch_size: int = 10) -> dict[str, Any]:ContentType.BLOCK,ContentType.STORE_AGENT,ContentType.DOCUMENTATION,+ ContentType.LIBRARY_AGENT,]for content_type in processing_order:@@ -623,6 +624,8 @@ async def cleanup_orphaned_embeddings() -> dict[str, Any]:- STORE_AGENT: Removes embeddings for rejected/deleted store listings- BLOCK: Removes embeddings for blocks no longer registered- DOCUMENTATION: Removes embeddings for deleted doc files+ - LIBRARY_AGENT: Removes embeddings for soft-deleted/hidden library agents+ (matches the LibraryAgentHandler.get_missing_items filter)Returns:Dict with cleanup statistics per content type@@ -635,6 +638,7 @@ async def cleanup_orphaned_embeddings() -> dict[str, Any]:ContentType.STORE_AGENT,ContentType.BLOCK,ContentType.DOCUMENTATION,+ ContentType.LIBRARY_AGENT,]for content_type in cleanup_types:@@ -664,6 +668,17 @@ async def cleanup_orphaned_embeddings() -> dict[str, Any]:current_ids = {row["id"] for row in valid_agents}elif content_type == ContentType.BLOCK:current_ids = set(get_blocks().keys())+ elif content_type == ContentType.LIBRARY_AGENT:+ # Match LibraryAgentHandler.get_missing_items: only non-deleted,+ # non-hidden LibraryAgent rows are considered valid.+ valid_agents = await query_raw_with_schema(+ """+ SELECT id+ FROM {schema_prefix}"LibraryAgent"+ WHERE "isDeleted" = false AND "isHidden" = false+ """,+ )+ current_ids = {row["id"] for row in valid_agents}elif content_type == ContentType.DOCUMENTATION:# Use DocumentationHandler to get section-based content IDsfrom backend.api.features.store.content_handlers import (diff --git a/autogpt_platform/backend/backend/api/features/store/hybrid_search.py b/autogpt_platform/backend/backend/api/features/store/hybrid_search.pyindex e4a5c4c5e6..593bd5201e 100644--- a/autogpt_platform/backend/backend/api/features/store/hybrid_search.py+++ b/autogpt_platform/backend/backend/api/features/store/hybrid_search.py@@ -140,6 +140,7 @@ async def unified_hybrid_search(weights: UnifiedSearchWeights | None = None,min_score: float | None = None,user_id: str | None = None,+ lexical_query: str | None = None,) -> tuple[list[dict[str, Any]], int]:"""Unified hybrid search across all content types.@@ -147,7 +148,8 @@ async def unified_hybrid_search(Searches UnifiedContentEmbedding using both semantic (vector) and lexical (tsvector) signals.Args:- query: Search query string+ query: Search query string (used for embedding + lexical unless+ ``lexical_query`` is provided).content_types: List of content types to search. Defaults to all public types.category: Filter by category (for content types that support it)page: Page number (1-indexed)@@ -155,6 +157,13 @@ async def unified_hybrid_search(weights: Custom weights for search signalsmin_score: Minimum relevance score threshold (0-1)user_id: User ID for searching private content (library agents)+ lexical_query: Optional override for the tsvector ``@@`` candidate+ selection and ``ts_rank_cd`` scoring. Use when the natural-+ language query is too long for ``plainto_tsquery``'s+ AND-of-terms semantics — typically a keyword-extracted form+ of ``query`` (e.g. ``"youtube summarize video"`` for a goal+ like ``"summarize a YouTube video..."``). Defaults to+ ``query`` when None.Returns:Tuple of (results list, total count)@@ -163,6 +172,11 @@ async def unified_hybrid_search(query = query.strip()if not query:return [], 0+ # Default the lexical query to the semantic query for backwards-+ # compatibility; callers (e.g. library similarity search) override+ # when ``plainto_tsquery``'s AND semantics over a long natural goal+ # would zero out every match.+ lexical_query = (lexical_query or query).strip() or queryif page < 1:page = 1@@ -213,12 +227,15 @@ async def unified_hybrid_search(params: list[Any] = []param_idx = 1- # Query for lexical search- params.append(query)+ # Query for lexical search (may differ from semantic ``query`` —+ # see the ``lexical_query`` parameter doc).+ params.append(lexical_query)query_param = f"${param_idx}"param_idx += 1- # Query lowercase for category matching+ # Query lowercase for category matching — keep tied to the+ # natural-language ``query`` so categories still match the user's+ # intent words, not the stripped lexical form.params.append(query.lower())query_lower_param = f"${param_idx}"param_idx += 1@@ -235,11 +252,18 @@ async def unified_hybrid_search(content_types_param = f"${param_idx}"param_idx += 1- # User ID filter (for private content)+ # User ID filter (for private content).+ # Defense-in-depth: a LIBRARY_AGENT row with NULL userId would be matched+ # by the public ``OR userId IS NULL`` branch and leak across users.+ # LIBRARY_AGENT rows are always per-user; explicitly exclude that combo.user_filter = ""if user_id is not None:params.append(user_id)- user_filter = f'AND (uce."userId" = ${param_idx} OR uce."userId" IS NULL)'+ user_filter = (+ f'AND (uce."userId" = ${param_idx} OR uce."userId" IS NULL) '+ f'AND NOT (uce."contentType" = \'LIBRARY_AGENT\'::{{schema_prefix}}"ContentType" '+ f'AND uce."userId" IS NULL)'+ )param_idx += 1else:user_filter = 'AND uce."userId" IS NULL'diff --git a/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py b/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.pyindex 7027ba994a..b21a602c5d 100644--- a/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py+++ b/autogpt_platform/backend/backend/api/features/store/hybrid_search_test.py@@ -569,6 +569,36 @@ async def test_unified_hybrid_search_with_user_id():assert "user-123" in params[email protected](loop_scope="session")+async def test_unified_hybrid_search_excludes_null_user_library_agents():+ """Defense-in-depth: the SQL filter must NEVER let a LIBRARY_AGENT row+ with NULL userId match an authenticated query — that would leak private+ library data across users. Sentry's HIGH-severity bug-prediction on+ hybrid_search.py:259."""+ with patch(+ "backend.api.features.store.hybrid_search.query_raw_with_schema"+ ) as mock_query:+ with patch(+ "backend.api.features.store.hybrid_search.embed_query"+ ) as mock_embed:+ mock_query.return_value = []+ mock_embed.return_value = [0.1] * embeddings.EMBEDDING_DIM++ await unified_hybrid_search(+ query="email summarizer",+ user_id="user-123",+ page=1,+ page_size=20,+ )++ sql_template = mock_query.call_args[0][0]+ assert (+ "NOT (uce.\"contentType\" = 'LIBRARY_AGENT'::{schema_prefix}"+ '"ContentType" AND uce."userId" IS NULL)' in sql_template+ )++@pytest.mark.asyncio(loop_scope="session")@pytest.mark.integrationasync def test_unified_hybrid_search_custom_weights():diff --git a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.mdindex 7938929812..2afedddc30 100644--- a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md+++ b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md@@ -5,41 +5,71 @@ generate the agent JSON yourself using block schemas, then validate and save.### Workflow for Creating/Editing Agents-1. **If editing**: First narrow to the specific agent by UUID, then fetch its+1. **If creating a new agent from a user goal (REQUIRED before `create_agent`)**:+ First call+ `find_library_agent(for_creation=true, goal_summary="<one-sentence summary of what the user wants>")`+ to check whether the user already has a functionally similar agent. The+ tool runs a hybrid semantic + lexical similarity search over the user's+ library and returns either:+ - `AgentsFoundResponse` with candidates whose descriptions are prefixed+ with `[N% match]`. **Surface these to the user**, preserving the match+ prefix, and ask whether they want to reuse one before you build a new+ agent. If they pick an existing agent, run it with `run_agent` —+ **do not** call `create_agent`.+ - `NoResultsResponse` — no functionally similar agent exists, so you may+ proceed with `create_agent`.++ `create_agent` enforces this check as a hard gate (`require_library_check`)+ and will refuse otherwise. If the user has been shown matches and+ explicitly tells you to build a new one anyway, retry `create_agent`+ with `library_check_ack=true` to bypass the gate for that call.+ **Never set `library_check_ack=true` proactively** — only after the+ user has seen the matches and chosen to build new.++ Builder-bound sessions (when the user is already editing a specific+ agent in the Builder) bypass this gate automatically — no+ pre-flight call is needed there.++2. **If editing**: First narrow to the specific agent by UUID, then fetch itsgraph: `find_library_agent(query="<agent_id>", include_graph=true)`. Thisreturns the full graph structure (nodes + links). **Never edit blindly** —always inspect the current graph first so you know exactly what to change.Avoid using `include_graph=true` with broad keyword searches, as fetchingmultiple graphs at once is expensive and consumes LLM context budget.-2. **Discover blocks**: Call `find_block(query, include_schemas=true, for_agent_generation=true)` to+3. **Discover blocks**: Call `find_block(query, include_schemas=true, for_agent_generation=true)` tosearch for relevant blocks. This returns block IDs, names, descriptions,and full input/output schemas. The `for_agent_generation=true` flag isrequired to surface graph-only blocks such as AgentInputBlock,AgentDropdownInputBlock, AgentOutputBlock, OrchestratorBlock,and WebhookBlock and MCPToolBlock. (When running MCP tools interactivelyin CoPilot outside agent generation, use `run_mcp_tool` instead.)-3. **Find library agents**: Call `find_library_agent` to discover reusable- agents that can be composed as sub-agents via `AgentExecutorBlock`.-4. **Generate/modify JSON**: Build or modify the agent JSON using block schemas:- - Use block IDs from step 2 as `block_id` in nodes+4. **Find library agents for sub-agent composition**: Call `find_library_agent`+ (default mode, no `for_creation` flag) to discover reusable agents that+ can be composed as sub-agents via `AgentExecutorBlock`. This is distinct+ from the create-time similarity check in step 1 — here you're looking+ for building blocks, not asking "does the user already have this?".+5. **Generate/modify JSON**: Build or modify the agent JSON using block schemas:+ - Use block IDs from step 3 as `block_id` in nodes- Wire outputs to inputs using links- Set design-time config in `input_default`- Use `AgentInputBlock` for values the user provides at runtime- When editing, apply targeted changes and preserve unchanged parts-5. **Write to workspace**: Save the JSON to a workspace file so the user+6. **Write to workspace**: Save the JSON to a workspace file so the usercan review it: `write_workspace_file(filename="agent.json", content=...)`-6. **Validate**: Call `validate_agent_graph` with the agent JSON to check+7. **Validate**: Call `validate_agent_graph` with the agent JSON to checkfor errors-7. **Fix if needed**: Call `fix_agent_graph` to auto-fix common issues,+8. **Fix if needed**: Call `fix_agent_graph` to auto-fix common issues,or fix manually based on the error descriptions. Iterate until valid.-8. **Save**: Call `create_agent` (new) or `edit_agent` (existing) with- the final `agent_json`-8. **Dry-run**: ALWAYS call `run_agent` with `dry_run=True` and- `wait_for_result=120` to verify the agent works end-to-end.-9. **Inspect & fix**: Check the dry-run output for errors. If issues are- found, call `edit_agent` to fix and dry-run again. Repeat until the- simulation passes or the problems are clearly unfixable.- See "REQUIRED: Dry-Run Verification Loop" section below for details.+9. **Save**: Call `create_agent` (new) or `edit_agent` (existing) with+ the final `agent_json`. For `create_agent`, the library similarity gate+ from step 1 must have been satisfied (either matches were shown to the+ user, or pass `library_check_ack=true` after explicit user confirmation).+10. **Dry-run**: ALWAYS call `run_agent` with `dry_run=True` and+ `wait_for_result=120` to verify the agent works end-to-end.+11. **Inspect & fix**: Check the dry-run output for errors. If issues are+ found, call `edit_agent` to fix and dry-run again. Repeat until the+ simulation passes or the problems are clearly unfixable.+ See "REQUIRED: Dry-Run Verification Loop" section below for details.### Agent JSON Structurediff --git a/autogpt_platform/backend/backend/copilot/tools/_test_data.py b/autogpt_platform/backend/backend/copilot/tools/_test_data.pyindex f94666bfef..9b39107db6 100644--- a/autogpt_platform/backend/backend/copilot/tools/_test_data.py+++ b/autogpt_platform/backend/backend/copilot/tools/_test_data.py@@ -42,14 +42,19 @@ async def _ensure_db_connected() -> None:await db_module.connect()-def make_session(user_id: str, *, guide_read: bool = True):+def make_session(user_id: str, *, guide_read: bool = True, library_check: bool = True):"""Build a fake ChatSession for tool tests.``guide_read=True`` (default) pre-populates the session with a``get_agent_building_guide`` tool-call history entry so the agent-- generation gate (see ``helpers.require_guide_read``) lets through any- subsequent ``create_agent`` / ``edit_agent`` / ``validate_agent_graph``- / ``fix_agent_graph`` call.+ generation gate lets through any subsequent ``create_agent`` /+ ``edit_agent`` / ``validate_agent_graph`` / ``fix_agent_graph`` call.++ ``library_check=True`` (default) announces an in-flight+ ``find_library_agent(for_creation=true)`` call so the create-time+ library-similarity gate lets through ``create_agent``. The gate is+ turn-scoped (in-flight only), so seeding via the in-flight buffer —+ not the durable messages list — is the correct shape."""messages: list[ChatMessage] = []if guide_read:@@ -60,7 +65,7 @@ def make_session(user_id: str, *, guide_read: bool = True):tool_calls=[{"function": {"name": "get_agent_building_guide"}}],))- return ChatSession(+ session = ChatSession(session_id=str(uuid.uuid4()),user_id=user_id,messages=messages,@@ -70,6 +75,12 @@ def make_session(user_id: str, *, guide_read: bool = True):successful_agent_runs={},successful_agent_schedules={},)+ if library_check:+ session.announce_inflight_tool_call(+ "find_library_agent",+ arguments={"for_creation": True, "goal_summary": "test"},+ )+ return session@pytest_asyncio.fixture(scope="session", loop_scope="session")diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_search.py b/autogpt_platform/backend/backend/copilot/tools/agent_search.pyindex d6f43d391e..ea81badd56 100644--- a/autogpt_platform/backend/backend/copilot/tools/agent_search.py+++ b/autogpt_platform/backend/backend/copilot/tools/agent_search.py@@ -4,12 +4,14 @@ from __future__ import annotationsimport asyncioimport logging-from typing import TYPE_CHECKING, Literal+from typing import TYPE_CHECKING, Any, Literalif TYPE_CHECKING:from backend.api.features.library.model import LibraryAgentfrom backend.api.features.store.model import StoreAgent, StoreAgentDetails+from backend.api.features.library.search import hybrid_search_library_agents+from backend.copilot.tracking import track_library_check_outcomefrom backend.data.db_accessors import graph_db, library_db, store_dbfrom backend.util.exceptions import DatabaseError, NotFoundError@@ -341,6 +343,180 @@ async def _get_marketplace_agent_by_slug(creator: str, slug: str) -> AgentInfo |return None+async def search_library_for_creation(+ goal_summary: str,+ session_id: str | None,+ user_id: str | None,+) -> ToolResponseBase:+ """Hybrid (semantic + lexical) library search used by the create-agent+ similarity gate.++ Unlike ``_search_library`` (substring), this is intended to surface+ *functionally similar* agents the user may want to reuse before+ creating a new one. The response message instructs the LLM to ask the+ user before proceeding to ``create_agent``.+ """+ if not user_id:+ return ErrorResponse(+ message="User authentication required to search library",+ session_id=session_id,+ )++ goal_summary = (goal_summary or "").strip()+ if not goal_summary:+ # Soft-fail instead of an error response so the UI doesn't render+ # "Error finding agents" and the gate still recognises this as a+ # valid call (the tool *was* invoked).+ return NoResultsResponse(+ message=(+ "No `goal_summary` was provided, so no similarity check "+ "ran. If the user is asking for a new agent, retry "+ "find_library_agent with for_creation=true and a "+ "goal_summary describing what they want. If the user has "+ "since clarified they want a new agent regardless, "+ "proceed with create_agent and pass "+ "library_check_ack=true."+ ),+ suggestions=[+ "Retry with for_creation=true and goal_summary=<user's goal>",+ "Proceed with create_agent + library_check_ack=true",+ ],+ session_id=session_id,+ )++ try:+ matches = await hybrid_search_library_agents(+ query=goal_summary, user_id=user_id+ )+ except DatabaseError as e:+ # logger.error → captured by Sentry's LoggingIntegration so a+ # flaky DB doesn't silently disable the gate.+ logger.error(f"Error during hybrid library search: {e}", exc_info=True)+ track_library_check_outcome(+ user_id=user_id, session_id=session_id, outcome="soft_failed"+ )+ return NoResultsResponse(+ message=(+ "Could not run the library similarity check (database "+ "error). Proceeding to create_agent is safe; pass "+ "library_check_ack=true to satisfy the gate."+ ),+ suggestions=["Proceed with create_agent + library_check_ack=true"],+ session_id=session_id,+ )+ except Exception as e:+ # Embedding service down / pgvector edge case — degrade gracefully+ # but log at ERROR so Sentry captures the silent feature-disable.+ logger.error(f"Hybrid library search failed unexpectedly: {e}", exc_info=True)+ track_library_check_outcome(+ user_id=user_id, session_id=session_id, outcome="soft_failed"+ )+ return NoResultsResponse(+ message=(+ "Could not run the library similarity check. Proceeding "+ "to create_agent is safe; pass library_check_ack=true to "+ "satisfy the gate."+ ),+ suggestions=["Proceed with create_agent + library_check_ack=true"],+ session_id=session_id,+ )++ if not matches:+ track_library_check_outcome(+ user_id=user_id, session_id=session_id, outcome="no_matches"+ )+ return NoResultsResponse(+ message=(+ "No functionally similar agents found in the user's library. "+ "You may proceed to create a new agent: call `create_agent` "+ "with `library_check_ack=true` to satisfy the similarity "+ "gate."+ ),+ suggestions=[+ "Proceed with create_agent (no similar library agent to reuse)",+ ],+ session_id=session_id,+ )++ agents = await _load_and_format_matched_agents(matches, user_id)++ if not agents:+ track_library_check_outcome(+ user_id=user_id, session_id=session_id, outcome="no_matches"+ )+ return NoResultsResponse(+ message=(+ "No functionally similar agents found in the user's library. "+ "You may proceed to create a new agent: call `create_agent` "+ "with `library_check_ack=true` to satisfy the similarity "+ "gate."+ ),+ suggestions=[+ "Proceed with create_agent (no similar library agent to reuse)",+ ],+ session_id=session_id,+ )++ top_score = max((a.match_score or 0.0) for a in agents)+ track_library_check_outcome(+ user_id=user_id,+ session_id=session_id,+ outcome="matches_shown",+ matches_count=len(agents),+ top_score=top_score,+ )+ return AgentsFoundResponse(+ message=(+ "Found agents in the user's library that may already match the "+ "user's goal. Present them with their `match_score` (a float in "+ "[0, 1]; format as `[N% match]` for the user) and ask whether "+ "they want to reuse one of these instead of creating a new "+ "agent. Use run_agent to execute a chosen existing agent. ONLY "+ "call `create_agent` with `library_check_ack=true` if the user "+ "explicitly chooses to build a new one anyway."+ ),+ title=(+ f"Found {len(agents)} potentially similar agent"+ f"{'s' if len(agents) != 1 else ''} in your library"+ ),+ agents=agents,+ count=len(agents),+ session_id=session_id,+ )+++async def _load_and_format_matched_agents(+ matches: list[dict[str, Any]], user_id: str+) -> list[AgentInfo]:+ """Resolve hybrid-search matches to ``AgentInfo`` rows with ``match_score``+ set from the search's ``combined_score`` (pre-BM25, always in [0, 1];+ ``relevance`` is post-BM25 and can go negative on near-duplicate corpora).+ Skips matches that can no longer be loaded; propagates ``DatabaseError``."""+ lib_db = library_db()+ agents: list[AgentInfo] = []+ for match in matches:+ content_id = match.get("content_id")+ if not content_id:+ continue+ try:+ library_agent = await lib_db.get_library_agent(content_id, user_id)+ except NotFoundError:+ continue+ except DatabaseError:+ raise+ except Exception as e:+ logger.warning(+ f"Could not fetch matched library agent {content_id}: {e}",+ exc_info=True,+ )+ continue++ info = _library_agent_to_info(library_agent)+ info.match_score = match.get("combined_score") or 0.0+ agents.append(info)+ return agents++async def _get_library_agent_by_id(user_id: str, agent_id: str) -> AgentInfo | None:"""Fetch a library agent by ID (library agent ID or graph_id).diff --git a/autogpt_platform/backend/backend/copilot/tools/create_agent.py b/autogpt_platform/backend/backend/copilot/tools/create_agent.pyindex 815888370a..dbd6c12d3e 100644--- a/autogpt_platform/backend/backend/copilot/tools/create_agent.py+++ b/autogpt_platform/backend/backend/copilot/tools/create_agent.py@@ -5,10 +5,11 @@ import uuidfrom typing import Anyfrom backend.copilot.model import ChatSession+from backend.copilot.tracking import track_library_check_outcomefrom .agent_generator.pipeline import fetch_library_agents, fix_validate_and_savefrom .base import BaseTool-from .helpers import require_guide_read+from .helpers import require_guide_read, require_library_checkfrom .models import ErrorResponse, ToolResponseBaselogger = logging.getLogger(__name__)@@ -25,8 +26,8 @@ class CreateAgentTool(BaseTool):def description(self) -> str:return ("Create a new agent from JSON (nodes + links). Validates, "- "auto-fixes, and saves. "- "Requires get_agent_building_guide first (refuses otherwise)."+ "auto-fixes, and saves. Requires get_agent_building_guide and "+ "find_library_agent(for_creation=true) first.")@property@@ -66,6 +67,11 @@ class CreateAgentTool(BaseTool):),"default": False,},+ "library_check_ack": {+ "type": "boolean",+ "description": "Bypass library-similarity gate after user declined.",+ "default": False,+ },},"required": ["agent_json"],}@@ -79,6 +85,7 @@ class CreateAgentTool(BaseTool):library_agent_ids: list[str] | None = None,folder_id: str | None = None,is_hidden: bool = False,+ library_check_ack: bool = False,**kwargs,) -> ToolResponseBase:session_id = session.session_id if session else None@@ -87,6 +94,17 @@ class CreateAgentTool(BaseTool):if guide_gate is not None:return guide_gate+ if not library_check_ack:+ library_gate = require_library_check(session, "create_agent")+ if library_gate is not None:+ return library_gate+ elif user_id and not (session and session.metadata.builder_graph_id):+ # Track the LLM-driven gate bypass so we can measure how often+ # users were shown matches but chose to build new anyway.+ track_library_check_outcome(+ user_id=user_id, session_id=session_id, outcome="bypassed_ack"+ )+if not agent_json:return ErrorResponse(message=(diff --git a/autogpt_platform/backend/backend/copilot/tools/create_agent_test.py b/autogpt_platform/backend/backend/copilot/tools/create_agent_test.pyindex 245233ee20..210336facb 100644--- a/autogpt_platform/backend/backend/copilot/tools/create_agent_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/create_agent_test.py@@ -173,6 +173,117 @@ async def test_is_hidden_passes_through_to_pipeline(tool, session, is_hidden):assert mock_fix_validate.call_args.kwargs["is_hidden"] is is_hidden+# ── Library similarity gate tests ──────────────────────────────────────+++async def test_create_agent_gated_until_library_check(tool):+ """Without prior find_library_agent, create_agent must refuse."""+ session = make_session(_TEST_USER_ID, library_check=False)++ agent_json = {+ "name": "Test Agent",+ "nodes": [+ {+ "id": "node-1",+ "block_id": "block-1",+ "input_default": {},+ "metadata": {"position": {"x": 0, "y": 0}},+ }+ ],+ "links": [],+ }++ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ agent_json=agent_json,+ )++ assert isinstance(result, ErrorResponse)+ assert "find_library_agent" in result.message+ assert "library_check_ack" in result.message+++async def test_create_agent_skipped_with_ack(tool):+ """library_check_ack=True bypasses the gate even when find_library_agent+ was never called this session."""+ session = make_session(_TEST_USER_ID, library_check=False)++ agent_json = {+ "name": "Test Agent",+ "description": "A test agent",+ "nodes": [+ {+ "id": "node-1",+ "block_id": "block-1",+ "input_default": {},+ "metadata": {"position": {"x": 0, "y": 0}},+ }+ ],+ "links": [],+ }++ with (+ patch(+ "backend.copilot.tools.create_agent.fetch_library_agents",+ new=AsyncMock(return_value=[]),+ ),+ patch(+ "backend.copilot.tools.create_agent.fix_validate_and_save",+ new=AsyncMock(return_value=MagicMock()),+ ) as mock_fix_validate,+ ):+ await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ agent_json=agent_json,+ library_check_ack=True,+ )++ mock_fix_validate.assert_awaited_once()+++async def test_create_agent_skipped_in_builder_context(tool):+ """Builder-bound sessions bypass the library gate (no goal to match)."""+ session = make_session(_TEST_USER_ID, library_check=False)+ session.metadata.builder_graph_id = "some-graph-id"++ agent_json = {+ "name": "Test Agent",+ "description": "A test agent",+ "nodes": [+ {+ "id": "node-1",+ "block_id": "block-1",+ "input_default": {},+ "metadata": {"position": {"x": 0, "y": 0}},+ }+ ],+ "links": [],+ }++ with (+ patch(+ "backend.copilot.tools.create_agent.fetch_library_agents",+ new=AsyncMock(return_value=[]),+ ),+ patch(+ "backend.copilot.tools.create_agent.fix_validate_and_save",+ new=AsyncMock(return_value=MagicMock()),+ ) as mock_fix_validate,+ ):+ await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ agent_json=agent_json,+ )++ mock_fix_validate.assert_awaited_once()++@pytest.mark.asyncioasync def test_local_mode_no_auth_returns_error(tool, session):"""Local mode with save=true and no user returns ErrorResponse."""diff --git a/autogpt_platform/backend/backend/copilot/tools/find_library_agent.py b/autogpt_platform/backend/backend/copilot/tools/find_library_agent.pyindex a5714ff8df..d3865b4538 100644--- a/autogpt_platform/backend/backend/copilot/tools/find_library_agent.py+++ b/autogpt_platform/backend/backend/copilot/tools/find_library_agent.py@@ -4,7 +4,7 @@ from typing import Anyfrom backend.copilot.model import ChatSession-from .agent_search import search_agents+from .agent_search import search_agents, search_library_for_creationfrom .base import BaseToolfrom .models import ToolResponseBase@@ -19,9 +19,9 @@ class FindLibraryAgentTool(BaseTool):@propertydef description(self) -> str:return (- "Search user's library agents. Returns graph_id, schemas for sub-agent composition. "- "Omit query to list all. Set include_graph=true to also fetch the full "- "graph structure (nodes + links) for debugging or editing."+ "Search user's library agents. for_creation=true+goal_summary "+ "runs the similarity check required by create_agent. Omit query "+ "to list all; include_graph=true for nodes+links.")@property@@ -42,7 +42,18 @@ class FindLibraryAgentTool(BaseTool):),"default": False,},+ "for_creation": {+ "type": "boolean",+ "description": "Pre-create similarity check.",+ "default": False,+ },+ "goal_summary": {+ "type": "string",+ "description": "Required when for_creation.",+ },},+ # goal_summary is enforced inside the for_creation branch via+ # a NoResultsResponse soft-fail, not as a JSON-schema required."required": [],}@@ -56,8 +67,18 @@ class FindLibraryAgentTool(BaseTool):session: ChatSession,query: str = "",include_graph: bool = False,+ for_creation: bool = False,+ goal_summary: str = "",**kwargs,) -> ToolResponseBase:+ if for_creation:+ # No ``or query`` fallback: the gate only accepts non-empty+ # goal_summary, so falling back to ``query`` would loop the LLM.+ return await search_library_for_creation(+ goal_summary=goal_summary,+ session_id=session.session_id,+ user_id=user_id,+ )return await search_agents(query=query.strip(),source="library",diff --git a/autogpt_platform/backend/backend/copilot/tools/find_library_agent_test.py b/autogpt_platform/backend/backend/copilot/tools/find_library_agent_test.pynew file mode 100644index 0000000000..86d18e13ed--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/find_library_agent_test.py@@ -0,0 +1,200 @@+"""Tests for FindLibraryAgentTool, especially the create-time similarity mode."""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.copilot.tools.find_library_agent import FindLibraryAgentTool+from backend.copilot.tools.models import AgentsFoundResponse, NoResultsResponse+from backend.util.exceptions import DatabaseError++from ._test_data import make_session++_TEST_USER_ID = "test-user-find-library-agent"+++def tool():+ return FindLibraryAgentTool()+++def session():+ return make_session(_TEST_USER_ID)+++def _mock_library_agent(library_id: str, name: str, description: str = ""):+ """Build a mock LibraryAgent with the fields _library_agent_to_info reads."""+ agent = MagicMock()+ agent.id = library_id+ agent.name = name+ agent.description = description+ agent.creator_name = "test-creator"+ status = MagicMock()+ status.value = "ready"+ agent.status = status+ agent.can_access_graph = True+ agent.has_external_trigger = False+ agent.new_output = False+ agent.graph_id = f"graph-{library_id}"+ agent.graph_version = 1+ agent.input_schema = {}+ agent.output_schema = {}+ return agent+++async def test_for_creation_returns_hybrid_ranked_results(tool, session):+ """for_creation=true delegates to hybrid_search_library_agents and+ sets match_score on each returned AgentInfo (no description mutation)."""+ match_a = {"content_id": "lib-a", "combined_score": 0.91, "relevance": 0.91}+ match_b = {"content_id": "lib-b", "combined_score": 0.72, "relevance": 0.72}++ lib_db = MagicMock()+ lib_db.get_library_agent = AsyncMock(+ side_effect=[+ _mock_library_agent("lib-a", "Email Summariser", "summarises emails"),+ _mock_library_agent("lib-b", "Inbox Triage", "triages inbox"),+ ]+ )++ with (+ patch(+ "backend.copilot.tools.agent_search.hybrid_search_library_agents",+ new=AsyncMock(return_value=[match_a, match_b]),+ ),+ patch(+ "backend.copilot.tools.agent_search.library_db",+ return_value=lib_db,+ ),+ ):+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ goal_summary="summarise my emails",+ )++ assert isinstance(result, AgentsFoundResponse)+ assert result.count == 2+ assert result.agents[0].match_score == 0.91+ assert result.agents[1].match_score == 0.72+ # Description must NOT be mutated with a "[N% match]" prefix anymore.+ assert "match]" not in result.agents[0].description+ assert "match]" not in result.agents[1].description+ # Message should steer the LLM and reference the new field name.+ assert "library_check_ack" in result.message+ assert "match_score" in result.message+++async def test_for_creation_no_matches_returns_no_results(tool, session):+ """When the hybrid search returns nothing, the tool tells the LLM it+ may proceed with create_agent + library_check_ack=true."""+ with patch(+ "backend.copilot.tools.agent_search.hybrid_search_library_agents",+ new=AsyncMock(return_value=[]),+ ):+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ goal_summary="some niche thing",+ )++ assert isinstance(result, NoResultsResponse)+ assert "library_check_ack" in result.message+++async def test_for_creation_without_goal_summary_soft_fails(tool, session):+ """for_creation=true with no goal_summary returns a NoResultsResponse+ (not an ErrorResponse) so the chat UI doesn't render "Error finding+ agents" — the gate is still satisfied because the tool was called."""+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ )+ assert isinstance(result, NoResultsResponse)+ assert "goal_summary" in result.message+ assert "library_check_ack" in result.message+++async def test_for_creation_does_not_fall_back_to_query(tool, session):+ """``query`` must NOT substitute for a missing ``goal_summary`` when+ ``for_creation=true``. Falling back would let the search succeed+ while the create_agent gate validator (which only accepts a non-empty+ ``goal_summary``) rejects, forcing the LLM into a retry loop. Sentry+ flagged this MEDIUM-severity misalignment on find_library_agent.py:83.+ """+ with patch(+ "backend.copilot.tools.agent_search.hybrid_search_library_agents",+ new=AsyncMock(return_value=[]),+ ) as mock_search:+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ query="summarise my emails",+ )++ mock_search.assert_not_awaited()+ assert isinstance(result, NoResultsResponse)+ assert "goal_summary" in result.message+++async def test_for_creation_db_error_soft_fails(tool, session):+ """A DatabaseError from the hybrid search degrades to NoResults so the+ UI stays clean and the LLM can proceed."""+ with patch(+ "backend.copilot.tools.agent_search.hybrid_search_library_agents",+ new=AsyncMock(side_effect=DatabaseError("connection refused")),+ ):+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ goal_summary="email summary agent",+ )+ assert isinstance(result, NoResultsResponse)+ assert "library_check_ack" in result.message+++async def test_for_creation_unexpected_error_soft_fails(tool, session):+ """Any other exception from hybrid search also degrades to NoResults."""+ with patch(+ "backend.copilot.tools.agent_search.hybrid_search_library_agents",+ new=AsyncMock(side_effect=RuntimeError("embedding API down")),+ ):+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ for_creation=True,+ goal_summary="email summary agent",+ )+ assert isinstance(result, NoResultsResponse)+ assert "library_check_ack" in result.message+++async def test_default_mode_still_uses_substring_search(tool, session):+ """Without for_creation, the tool falls back to the existing+ substring-search path via search_agents."""+ with patch(+ "backend.copilot.tools.find_library_agent.search_agents",+ new=AsyncMock(return_value=NoResultsResponse(message="ok")),+ ) as mock_search:+ result = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query="email",+ )++ assert isinstance(result, NoResultsResponse)+ mock_search.assert_awaited_once()+ assert mock_search.call_args.kwargs["source"] == "library"+ assert mock_search.call_args.kwargs["query"] == "email"diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex 70a40fa19c..0e7e733e74 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -986,3 +986,53 @@ def require_guide_read(session: ChatSession, tool_name: str):),session_id=session.session_id,)+++_LIBRARY_CHECK_TOOL_NAME = "find_library_agent"+++def _has_for_creation_args(args: dict) -> bool:+ """``for_creation=True`` + non-empty ``goal_summary``: the inputs the+ hybrid search actually needs. Empty goal_summary soft-fails without+ running the search, so it must not satisfy the gate."""+ if args.get("for_creation") is not True:+ return False+ goal_summary = args.get("goal_summary")+ return isinstance(goal_summary, str) and bool(goal_summary.strip())+++def _was_called_for_creation(session: ChatSession) -> bool:+ """True iff a satisfying ``find_library_agent`` call exists in the+ current turn's in-flight buffer. Turn-scoped on purpose: a stale+ call from an earlier turn's unrelated goal must not satisfy the+ gate for a new create_agent request."""+ for args in session.get_inflight_tool_call_args(_LIBRARY_CHECK_TOOL_NAME):+ if _has_for_creation_args(args):+ return True+ return False+++def require_library_check(session: ChatSession, tool_name: str):+ """Return an ErrorResponse if ``find_library_agent(for_creation=true)``+ hasn't been called in this session. Bypassed in builder-bound sessions+ (already editing a specific agent)."""+ from .models import ErrorResponse # noqa: PLC0415 — avoid circular import++ if session.metadata.builder_graph_id:+ return None+ if _was_called_for_creation(session):+ return None+ return ErrorResponse(+ message=(+ f"Before {tool_name} can run, search the user's library for an "+ "agent that already does what they want. Call "+ "`find_library_agent` with `for_creation=true` and "+ "`goal_summary=<one-sentence description of the user's goal>` "+ "(default-mode substring search does NOT satisfy this gate). "+ "If any agents are returned, present them to the user and ask "+ "whether they want to reuse one. Only retry "+ f"{tool_name} with `library_check_ack=true` if the user "+ "explicitly chooses to build a new agent anyway."+ ),+ session_id=session.session_id,+ )diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py b/autogpt_platform/backend/backend/copilot/tools/helpers_test.pyindex 80506b1013..8cda688611 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers_test.py@@ -13,6 +13,7 @@ from backend.copilot.tools.helpers import (check_hitl_review,execute_block,prepare_block_for_execution,+ require_library_check,)from backend.copilot.tools.models import (BlockOutputResponse,@@ -22,6 +23,8 @@ from backend.copilot.tools.models import (SetupRequirementsResponse,)+from ._test_data import make_session+_USER = "test-user-helpers"_SESSION = "test-session-helpers"@@ -1330,3 +1333,82 @@ class TestExecuteBlockAutoCredentials:assert isinstance(result, ErrorResponse)assert "Insufficient credits" in result.messagemock_lock.release.assert_awaited_once()+++class TestRequireLibraryCheck:+ """Tests for the library-similarity gate. The gate is turn-scoped:+ only the current turn's in-flight find_library_agent calls satisfy+ it, so a stale call from an earlier turn (against an unrelated+ goal_summary) cannot pass create_agent through."""++ def test_inflight_with_args_satisfies(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.announce_inflight_tool_call(+ "find_library_agent",+ arguments={"for_creation": True, "goal_summary": "summarise emails"},+ )+ assert require_library_check(session, "create_agent") is None++ def test_inflight_with_empty_goal_summary_does_not_satisfy(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.announce_inflight_tool_call(+ "find_library_agent",+ arguments={"for_creation": True, "goal_summary": ""},+ )+ result = require_library_check(session, "create_agent")+ assert isinstance(result, ErrorResponse)++ def test_inflight_with_for_creation_false_does_not_satisfy(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.announce_inflight_tool_call(+ "find_library_agent",+ arguments={"for_creation": False, "goal_summary": "x"},+ )+ result = require_library_check(session, "create_agent")+ assert isinstance(result, ErrorResponse)++ def test_inflight_name_only_does_not_satisfy(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.announce_inflight_tool_call("find_library_agent")+ result = require_library_check(session, "create_agent")+ assert isinstance(result, ErrorResponse)++ def test_history_only_call_does_not_satisfy(self):+ """A find_library_agent call recorded in session.messages from a+ prior turn must NOT satisfy the gate — it was almost certainly+ against an unrelated goal_summary."""+ from backend.copilot.model import ChatMessage++ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.messages.append(+ ChatMessage(+ role="assistant",+ content="",+ tool_calls=[+ {+ "function": {+ "name": "find_library_agent",+ "arguments": (+ '{"for_creation": true, '+ '"goal_summary": "summarise emails"}'+ ),+ }+ }+ ],+ )+ )+ result = require_library_check(session, "create_agent")+ assert isinstance(result, ErrorResponse)++ def test_returns_error_when_not_called(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ result = require_library_check(session, "create_agent")+ assert isinstance(result, ErrorResponse)+ assert "find_library_agent" in result.message+ assert "for_creation" in result.message+ assert "library_check_ack" in result.message++ def test_bypassed_in_builder_context(self):+ session = make_session("user-lib-check", guide_read=False, library_check=False)+ session.metadata.builder_graph_id = "some-graph-id"+ assert require_library_check(session, "create_agent") is Nonediff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.pyindex 70f4ad65f5..88d9695102 100644--- a/autogpt_platform/backend/backend/copilot/tools/models.py+++ b/autogpt_platform/backend/backend/copilot/tools/models.py@@ -138,6 +138,14 @@ class AgentInfo(BaseModel):new_output: bool | None = Nonegraph_id: str | None = Nonegraph_version: int | None = None+ match_score: float | None = Field(+ default=None,+ description=(+ "Combined relevance score in [0, 1] when this agent was returned "+ "from a similarity search (e.g. the create-time library check). "+ "Null for non-similarity sources."+ ),+ )input_schema: dict[str, Any] | None = Field(default=None,description="JSON Schema for the agent's inputs (for AgentExecutorBlock)",diff --git a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.pyindex a1a39866ab..97d79d08e6 100644--- a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py@@ -59,7 +59,13 @@ from backend.copilot.tools import TOOL_REGISTRY# sentinel — its description spends ~170 chars explaining the "fire# into a fresh chat" semantics so the model picks the right value# (null vs omit vs target_session_id) for autopilot-style flows.-_CHAR_BUDGET = 39_000+# Bumped 39000 -> 39500 for the create-time library-similarity gate:+# find_library_agent's new ``for_creation`` and ``goal_summary``+# parameters and create_agent's ``library_check_ack`` bypass — the+# extra ~270 chars on CI (env-flagged tool registrations push CI+# higher than local) carry the LLM-decision-critical copy for+# "search the library before building new" + "user-confirmed bypass".+_CHAR_BUDGET = 39_500@pytest.fixture(scope="module")diff --git a/autogpt_platform/backend/backend/copilot/tracking.py b/autogpt_platform/backend/backend/copilot/tracking.pyindex 7ef8659259..d50716fe2e 100644--- a/autogpt_platform/backend/backend/copilot/tracking.py+++ b/autogpt_platform/backend/backend/copilot/tracking.py@@ -284,3 +284,43 @@ def track_trigger_setup()except Exception as e:logger.warning(f"Failed to track trigger setup: {e}")+++def track_library_check_outcome(+ user_id: str,+ session_id: str | None,+ outcome: str,+ matches_count: int = 0,+ top_score: float | None = None,+) -> None:+ """Track the create-time library-similarity gate's outcome so we can+ measure how often the LLM bypasses it vs. reuses an existing agent.++ Args:+ outcome: One of ``"matches_shown"``, ``"no_matches"``,+ ``"bypassed_ack"`` (LLM set ``library_check_ack=true``),+ or ``"soft_failed"`` (search threw, gate silently disabled).+ matches_count: Number of candidates returned above threshold.+ top_score: Highest ``combined_score`` seen (above OR below+ threshold) — included so sub-threshold near-misses can+ inform retuning ``LIBRARY_SIMILARITY_THRESHOLD``.+ """+ client = _get_posthog_client()+ if not client:+ return++ try:+ properties = {+ **_get_base_properties(),+ "session_id": session_id,+ "outcome": outcome,+ "matches_count": matches_count,+ "top_score": top_score,+ }+ client.capture(+ distinct_id=user_id,+ event="copilot_library_check_outcome",+ properties=properties,+ )+ except Exception as e:+ logger.warning(f"Failed to track library check outcome: {e}")diff --git a/autogpt_platform/backend/migrations/20260525120000_library_agent_userid_check/migration.sql b/autogpt_platform/backend/migrations/20260525120000_library_agent_userid_check/migration.sqlnew file mode 100644index 0000000000..c47f2c33c1--- /dev/null+++ b/autogpt_platform/backend/migrations/20260525120000_library_agent_userid_check/migration.sql@@ -0,0 +1,15 @@+-- Defense-in-depth: LIBRARY_AGENT embeddings are always per-user. The+-- runtime filter in hybrid_search excludes NULL-userId LIBRARY_AGENT rows+-- to prevent cross-user leaks, but that's a single line of defense. This+-- CHECK constraint enforces the invariant at the storage layer so a future+-- write-path or migration bug can't introduce a leaking row.++-- Sweep any stray rows first (none expected — LibraryAgentHandler always+-- writes with userId, schedule_library_agent_embedding does too) so the+-- ALTER doesn't fail loudly in environments where someone hand-inserted.+DELETE FROM "UnifiedContentEmbedding"+WHERE "contentType" = 'LIBRARY_AGENT' AND "userId" IS NULL;++ALTER TABLE "UnifiedContentEmbedding"+ADD CONSTRAINT "UnifiedContentEmbedding_library_agent_requires_user"+CHECK ("contentType" != 'LIBRARY_AGENT' OR "userId" IS NOT NULL);diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 3996b45da5..ed7d9462d6 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -1228,7 +1228,10 @@ model UnifiedContentEmbedding {// Content identificationcontentType ContentTypecontentId String // DB ID (storeListingVersionId) or file identifier (block.id, file_path)- userId String? // NULL for public content (store, blocks, docs), userId for private content (library agents)+ // NULL for public content (store, blocks, docs); userId for private content (library agents).+ // A SQL CHECK constraint enforces ``contentType != 'LIBRARY_AGENT' OR userId IS NOT NULL``+ // (see migration ``20260525120000_library_agent_userid_check``).+ userId String?// Search dataembedding Unsupported("vector(1536)") // pgvector embedding (extension in platform schema)diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 6a9b523241..12f44a55ff 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -10048,6 +10048,11 @@"anyOf": [{ "type": "integer" }, { "type": "null" }],"title": "Graph Version"},+ "match_score": {+ "anyOf": [{ "type": "number" }, { "type": "null" }],+ "title": "Match Score",+ "description": "Combined relevance score in [0, 1] when this agent was returned from a similarity search (e.g. the create-time library check). Null for non-similarity sources."+ },"input_schema": {"anyOf": [{ "additionalProperties": true, "type": "object" },