From ee74384c79cd64fb714c1b609f2e14d2a49bf85e Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 19 Jan 2026 01:57:55 +0000 Subject: [PATCH] fix: Prevent llm.search from returning huge object lists in list mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When llm.search is called with an empty query (list mode) to retrieve all available questions, include_objects=true was returning full object schemas for all related objects, resulting in massive responses that could fill the LLM's context and cause rejections. Fix: include_objects now only works when query is non-empty (search mode). When query is empty (list mode), only question templates are returned without object details, regardless of include_objects setting. This makes semantic sense: - Empty query = "list all questions" → just titles/bodies (compact) - Non-empty query = "search for specific questions" → full details including object schemas (for answering the question) Changes: - Modified fts_search_llm() to check !query.empty() before fetching objects - Updated tool schema description to clarify this behavior --- lib/Discovery_Schema.cpp | 11 ++++++----- lib/Query_Tool_Handler.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/Discovery_Schema.cpp b/lib/Discovery_Schema.cpp index 360dd11e8..51ebc3fbc 100644 --- a/lib/Discovery_Schema.cpp +++ b/lib/Discovery_Schema.cpp @@ -2003,13 +2003,14 @@ std::string Discovery_Schema::fts_search_llm( } delete resultset; - // If include_objects, fetch object details - if (include_objects) { - proxy_error("FTS search: include_objects=%d, objects_to_fetch size=%zu\n", include_objects ? 1 : 0, objects_to_fetch.size()); + // If include_objects AND query is not empty (search mode), fetch object details + // For list mode (empty query), we don't include objects to avoid huge responses + if (include_objects && !query.empty()) { + proxy_info("FTS search: include_objects=true (search mode), objects_to_fetch size=%zu\n", objects_to_fetch.size()); } - if (include_objects && !objects_to_fetch.empty()) { - proxy_info("FTS search: include_objects=true, objects_to_fetch size=%zu\n", objects_to_fetch.size()); + if (include_objects && !query.empty() && !objects_to_fetch.empty()) { + proxy_info("FTS search: Fetching object details for %zu objects\n", objects_to_fetch.size()); // First, build a map of object_name -> schema_name by querying the objects table std::map object_to_schema; diff --git a/lib/Query_Tool_Handler.cpp b/lib/Query_Tool_Handler.cpp index b4f3f8deb..f6d587f23 100644 --- a/lib/Query_Tool_Handler.cpp +++ b/lib/Query_Tool_Handler.cpp @@ -728,7 +728,7 @@ json Query_Tool_Handler::get_tool_list() { tools.push_back(create_tool_schema( "llm.search", - "Full-text search across LLM artifacts. For question_templates, returns example_sql, related_objects, template_json, and confidence. Use include_objects=true to get full object schema details.", + "Full-text search across LLM artifacts. For question_templates, returns example_sql, related_objects, template_json, and confidence. Use include_objects=true with a non-empty query to get full object schema details (for search mode only). Empty query (list mode) returns only templates without objects to avoid huge responses.", {"run_id"}, {{"query", "string"}, {"limit", "integer"}, {"include_objects", "boolean"}} ));