Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(mcp): make semantic search contracts explicit

Martin Vogeldaa5dc824784
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 899a5e0a4..45f8f2fdb 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -482,7 +482,7 @@ static const tool_def_t TOOLS[] = {
{"search_graph", "Search graph",
"Regex rows keep qn/file/lines and in/out over CALLS/USAGE/CALL_REFERENCE/INHERITS/"
- "IMPLEMENTS. Pages: offset/limit structural; semantic_offset/semantic_limit semantic.",
+ "IMPLEMENTS. Pages: offset/limit; semantic_offset/semantic_limit.",
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},"
"\"query\":{\"type\":\"string\"},"
"\"label\":{\"type\":\"string\"},\"name_pattern\":{\"type\":\"string\"},\"qn_pattern\":{"
@@ -490,7 +490,7 @@ static const tool_def_t TOOLS[] = {
"\"relationship\":{\"type\":\"string\"},\"min_degree\":{\"type\":\"integer\"},"
"\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{\"type\":\"boolean\"},"
"\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{"
- "\"type\":\"array\",\"items\":{\"type\":\"string\"}},"
+ "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Not with query.\"},"
"\"semantic_limit\":{\"type\":\"integer\",\"default\":50,\"minimum\":0,"
"\"maximum\":500},"
"\"semantic_offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0,"
@@ -4306,6 +4306,16 @@ static bool run_semantic_query_core(const char *args, cbm_store_t *store, const
return type_error;
}
+static bool search_graph_arg_present(const char *args, const char *name) {
+ yyjson_doc *doc = yyjson_read(args, strlen(args), 0);
+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
+ bool present = root && yyjson_is_obj(root) && yyjson_obj_get(root, name) != NULL;
+ if (doc) {
+ yyjson_doc_free(doc);
+ }
+ return present;
+}
+
/* ── Tree output for search_graph ───────────────────────────────────
* Default response encoding chooses the smaller complete direct/grouped tree
* (compact_out.h). format:"json" keeps a stable grouped model;
@@ -4450,7 +4460,9 @@ static void sg_emit_paging_tree(cbm_sb_t *sb, int total, int offset, const sg_re
int requested_fields, bool connected_requested,
size_t max_output_bytes, bool continuation_supported,
bool continuation_requires_positive_limit) {
- bool has_more = continuation_supported && sg_page_has_more(total, offset, plan->returned);
+ bool rows_remain = sg_page_has_more(total, offset, plan->returned);
+ bool has_more = continuation_supported && rows_remain;
+ bool engine_saturated = !continuation_supported && rows_remain;
cbm_tree_scalar_int(sb, "total", total);
cbm_tree_scalar_int(sb, "returned", plan->returned);
cbm_tree_scalar_bool(sb, "has_more", has_more);
@@ -4463,11 +4475,13 @@ static void sg_emit_paging_tree(cbm_sb_t *sb, int total, int offset, const sg_re
: "continuation_requires_higher_budget",
true);
}
- bool truncated = has_more || plan->budget_hit;
+ bool truncated = rows_remain || plan->budget_hit;
cbm_tree_scalar_bool(sb, "truncated", truncated);
if (truncated) {
cbm_tree_scalar_str(sb, "truncation_reason",
- plan->budget_hit ? "output_budget" : "page_limit");
+ plan->budget_hit ? "output_budget"
+ : engine_saturated ? "engine_limit"
+ : "page_limit");
}
if (plan->budget_hit) {
cbm_tree_scalar_int(sb, "max_output_bytes", (long long)max_output_bytes);
@@ -4491,7 +4505,9 @@ static void sg_emit_paging_json(yyjson_mut_doc *doc, yyjson_mut_val *root, int t
bool connected_requested, size_t max_output_bytes,
bool continuation_supported,
bool continuation_requires_positive_limit) {
- bool has_more = continuation_supported && sg_page_has_more(total, offset, plan->returned);
+ bool rows_remain = sg_page_has_more(total, offset, plan->returned);
+ bool has_more = continuation_supported && rows_remain;
+ bool engine_saturated = !continuation_supported && rows_remain;
yyjson_mut_obj_add_int(doc, root, "total", total);
yyjson_mut_obj_add_int(doc, root, "returned", plan->returned);
yyjson_mut_obj_add_int(doc, root, "count", plan->returned);
@@ -4505,11 +4521,13 @@ static void sg_emit_paging_json(yyjson_mut_doc *doc, yyjson_mut_val *root, int t
: "continuation_requires_higher_budget",
true);
}
- bool truncated = has_more || plan->budget_hit;
+ bool truncated = rows_remain || plan->budget_hit;
yyjson_mut_obj_add_bool(doc, root, "truncated", truncated);
if (truncated) {
yyjson_mut_obj_add_str(doc, root, "truncation_reason",
- plan->budget_hit ? "output_budget" : "page_limit");
+ plan->budget_hit ? "output_budget"
+ : engine_saturated ? "engine_limit"
+ : "page_limit");
}
if (plan->budget_hit) {
yyjson_mut_obj_add_uint(doc, root, "max_output_bytes", max_output_bytes);
@@ -4529,15 +4547,31 @@ static void sg_emit_paging_json(yyjson_mut_doc *doc, yyjson_mut_val *root, int t
}
}
-static bool sg_semantic_has_more(const sg_semantic_page_t *semantic, int returned) {
+static bool sg_semantic_has_remaining_rows(const sg_semantic_page_t *semantic, int returned) {
return semantic && sg_page_has_more(semantic->total, semantic->offset, returned);
}
+/* semantic_offset is deliberately bounded: vector ranking certifies an
+ * ordered prefix, so an unbounded offset would turn one tiny MCP request into
+ * unbounded materialization. A continuation is usable only when the next
+ * offset is accepted by the public schema/runtime. */
+static bool sg_semantic_engine_saturated(const sg_semantic_page_t *semantic, int returned) {
+ return sg_semantic_has_remaining_rows(semantic, returned) && returned > 0 &&
+ (long long)semantic->offset + returned > MCP_QUERY_MAX_VISIBLE_ROWS;
+}
+
+static bool sg_semantic_has_more(const sg_semantic_page_t *semantic, int returned) {
+ return sg_semantic_has_remaining_rows(semantic, returned) &&
+ !sg_semantic_engine_saturated(semantic, returned);
+}
+
static void sg_emit_semantic_paging_tree(cbm_sb_t *sb, const sg_semantic_page_t *semantic,
int returned) {
+ bool saturated = sg_semantic_engine_saturated(semantic, returned);
bool has_more = sg_semantic_has_more(semantic, returned);
cbm_tree_scalar_int(sb, "semantic_total", semantic->total);
- cbm_tree_scalar_str(sb, "semantic_total_relation", semantic->total_exact ? "eq" : "gte");
+ cbm_tree_scalar_str(sb, "semantic_total_relation",
+ semantic->total_exact && !saturated ? "eq" : "gte");
cbm_tree_scalar_int(sb, "semantic_returned", returned);
cbm_tree_scalar_bool(sb, "semantic_has_more", has_more);
if (has_more && returned > 0) {
@@ -4548,14 +4582,19 @@ static void sg_emit_semantic_paging_tree(cbm_sb_t *sb, const sg_semantic_page_t
: "semantic_continuation_requires_higher_budget",
true);
}
+ if (saturated) {
+ cbm_tree_scalar_bool(sb, "semantic_engine_saturated", true);
+ cbm_tree_scalar_bool(sb, "semantic_continuation_unavailable", true);
+ }
}
static void sg_emit_semantic_paging_json(yyjson_mut_doc *doc, yyjson_mut_val *root,
const sg_semantic_page_t *semantic, int returned) {
+ bool saturated = sg_semantic_engine_saturated(semantic, returned);
bool has_more = sg_semantic_has_more(semantic, returned);
yyjson_mut_obj_add_int(doc, root, "semantic_total", semantic->total);
yyjson_mut_obj_add_str(doc, root, "semantic_total_relation",
- semantic->total_exact ? "eq" : "gte");
+ semantic->total_exact && !saturated ? "eq" : "gte");
yyjson_mut_obj_add_int(doc, root, "semantic_returned", returned);
yyjson_mut_obj_add_bool(doc, root, "semantic_has_more", has_more);
if (has_more && returned > 0) {
@@ -4567,8 +4606,45 @@ static void sg_emit_semantic_paging_json(yyjson_mut_doc *doc, yyjson_mut_val *ro
: "semantic_continuation_requires_higher_budget",
true);
}
+ if (saturated) {
+ yyjson_mut_obj_add_bool(doc, root, "semantic_engine_saturated", true);
+ yyjson_mut_obj_add_bool(doc, root, "semantic_continuation_unavailable", true);
+ }
}
+#ifdef CBM_ENABLE_TEST_SEAMS
+char *cbm_mcp_render_semantic_paging_for_testing(int total, int offset, int returned, int limit,
+ bool total_exact, bool json_format) {
+ sg_semantic_page_t semantic = {
+ .total = total,
+ .offset = offset,
+ .limit = limit,
+ .total_exact = total_exact,
+ .present = true,
+ };
+ if (!json_format) {
+ cbm_sb_t sb;
+ cbm_sb_init(&sb);
+ sg_emit_semantic_paging_tree(&sb, &semantic, returned);
+ sg_render_plan_t plan = {.returned = returned, .semantic_returned = returned};
+ sg_emit_paging_tree(&sb, total, offset, &plan, 0, false, SIZE_MAX,
+ !sg_semantic_engine_saturated(&semantic, returned), false);
+ return cbm_sb_finish(&sb);
+ }
+
+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
+ yyjson_mut_val *root = yyjson_mut_obj(doc);
+ yyjson_mut_doc_set_root(doc, root);
+ sg_emit_semantic_paging_json(doc, root, &semantic, returned);
+ sg_render_plan_t plan = {.returned = returned, .semantic_returned = returned};
+ sg_emit_paging_json(doc, root, total, offset, &plan, 0, false, SIZE_MAX,
+ !sg_semantic_engine_saturated(&semantic, returned), false);
+ char *json = yy_doc_to_str(doc);
+ yyjson_mut_doc_free(doc);
+ return json;
+}
+#endif
+
static void emit_search_results_ids_tree(cbm_sb_t *sb, const cbm_search_output_t *out,
int returned) {
static const char *const id_cols[] = {"qn"};
@@ -4858,6 +4934,8 @@ static char *sg_render_payload(bool json_format, const cbm_search_output_t *out,
if (semantic_only) {
meta_plan.returned = plan->semantic_returned;
}
+ bool continuation_supported =
+ !semantic_only || !sg_semantic_engine_saturated(semantic, plan->semantic_returned);
if (!json_format) {
cbm_sb_t sb;
@@ -4878,7 +4956,7 @@ static char *sg_render_payload(bool json_format, const cbm_search_output_t *out,
cbm_tree_scalar_str(&sb, "hint", diagnostic_hint);
}
sg_emit_paging_tree(&sb, page_total, page_offset, &meta_plan, requested_fields,
- connected_requested, max_output_bytes, true,
+ connected_requested, max_output_bytes, continuation_supported,
semantic_only && semantic->limit == 0);
return cbm_sb_finish(&sb);
}
@@ -4902,7 +4980,7 @@ static char *sg_render_payload(bool json_format, const cbm_search_output_t *out,
yyjson_mut_obj_add_strcpy(doc, root, "hint", diagnostic_hint);
}
sg_emit_paging_json(doc, root, page_total, page_offset, &meta_plan, requested_fields,
- connected_requested, max_output_bytes, true,
+ connected_requested, max_output_bytes, continuation_supported,
semantic_only && semantic->limit == 0);
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
@@ -4989,7 +5067,11 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
if (semantic_offset < 0) {
semantic_offset = 0;
} else if (semantic_offset > MCP_QUERY_MAX_VISIBLE_ROWS) {
- semantic_offset = MCP_QUERY_MAX_VISIBLE_ROWS;
+ free(project);
+ return cbm_mcp_text_result(
+ "semantic_offset maximum is 99998 — semantic ranking is resource-bounded; "
+ "continue only with a semantic_next_offset emitted by search_graph.",
+ true);
}
/* One ranked hit beyond the requested semantic page makes has_more
* deterministic. Keeping the prefix through that lookahead also lets a
@@ -5008,6 +5090,15 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
* and return early. If FTS5 is unavailable or the query is empty after
* tokenization, fall through to the regex path. */
char *query = cbm_mcp_get_string_arg(args, "query");
+ if (query && search_graph_arg_present(args, "semantic_query")) {
+ free(query);
+ free(project);
+ return cbm_mcp_text_result(
+ "query and semantic_query are mutually exclusive — use query for BM25 full-text "
+ "ranking or semantic_query for vector ranking, then issue a separate request for "
+ "the other mode.",
+ true);
+ }
if (query && query[0]) {
char *q_file_pattern = cbm_mcp_get_string_arg(args, "file_pattern");
char *bm25_json = bm25_search(store, project, query, q_file_pattern, limit, offset,
diff --git a/src/mcp/mcp_internal.h b/src/mcp/mcp_internal.h
index 95b733645..5e5c771a3 100644
--- a/src/mcp/mcp_internal.h
+++ b/src/mcp/mcp_internal.h
@@ -96,6 +96,12 @@ char *cbm_mcp_render_search_rows_for_testing(const char *const *qualified_names,
* deterministic on every test platform. */
char *cbm_mcp_render_raw_preview_for_testing(const char *content, bool content_offset_set,
size_t content_offset, bool json_format);
+
+/* Exercise search_graph's semantic continuation metadata without allocating
+ * a 100k-row vector fixture. The production emitters remain the code under
+ * test; this seam only supplies their already-ranked page descriptor. */
+char *cbm_mcp_render_semantic_paging_for_testing(int total, int offset, int returned, int limit,
+ bool total_exact, bool json_format);
#endif
#endif
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index e946b96b9..dd655bae4 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -3696,6 +3696,140 @@ TEST(tool_search_graph_bm25_reports_candidate_saturation) {
PASS();
}
+/* `query` selects the BM25 fast path, which returns before semantic_query is
+ * evaluated. Accepting both therefore used to report success while silently
+ * discarding one of the caller's two requested result sets. The contract is
+ * deliberately fail-closed: callers choose one ranking model per request. */
+TEST(tool_search_graph_rejects_bm25_and_semantic_query_together) {
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *store = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(store);
+ const char *project = "mixed-search-modes";
+ cbm_mcp_server_set_project(srv, project);
+ ASSERT_EQ(cbm_store_upsert_project(store, project, "/tmp/mixed-search-modes"), CBM_STORE_OK);
+
+ cbm_node_t node = {.project = project,
+ .label = "Function",
+ .name = "mixedneedle",
+ .qualified_name = "mixed-search-modes.mixedneedle",
+ .file_path = "mixed.c",
+ .start_line = 1,
+ .end_line = 1};
+ ASSERT_GT(cbm_store_upsert_node(store, &node), 0);
+ ASSERT_EQ(cbm_store_exec(store, "INSERT INTO nodes_fts(nodes_fts) VALUES('delete-all');"),
+ CBM_STORE_OK);
+ ASSERT_EQ(cbm_store_exec(store, "INSERT INTO nodes_fts(rowid, name, qualified_name, label, "
+ "file_path) SELECT id, cbm_camel_split(name), qualified_name, "
+ "label, file_path FROM nodes;"),
+ CBM_STORE_OK);
+
+ char *response = cbm_mcp_handle_tool(
+ srv, "search_graph",
+ "{\"project\":\"mixed-search-modes\",\"query\":\"mixedneedle\","
+ "\"semantic_query\":[\"meaning\"],\"format\":\"json\"}");
+ ASSERT_NOT_NULL(response);
+ ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
+ ASSERT_NOT_NULL(strstr(response, "query and semantic_query are mutually exclusive"));
+ ASSERT_NULL(strstr(response, "\"search_mode\":\"bm25\""));
+ free(response);
+
+ char *tools = cbm_mcp_tools_list();
+ ASSERT_NOT_NULL(tools);
+ ASSERT_NOT_NULL(strstr(tools, "Not with query"));
+ free(tools);
+
+ cbm_mcp_server_free(srv);
+ PASS();
+}
+
+/* The public schema accepts semantic_offset only through 99,998. A page at
+ * that boundary used to emit semantic_next_offset:99,999; the handler then
+ * clamped replay back to 99,998, repeating the same row forever. At the engine
+ * boundary we must report an explicit lower-bound saturation and no cursor. */
+TEST(tool_search_graph_semantic_ceiling_never_emits_unusable_continuation) {
+ char *json = cbm_mcp_render_semantic_paging_for_testing(100000, 99998, 1, 1, false, true);
+ ASSERT_NOT_NULL(json);
+ yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc);
+ yyjson_val *root = yyjson_doc_get_root(doc);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "semantic_total_relation")), "gte");
+ ASSERT_FALSE(yyjson_get_bool(yyjson_obj_get(root, "semantic_has_more")));
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "semantic_engine_saturated")));
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "semantic_continuation_unavailable")));
+ ASSERT_NULL(yyjson_obj_get(root, "semantic_next_offset"));
+ ASSERT_FALSE(yyjson_get_bool(yyjson_obj_get(root, "has_more")));
+ ASSERT_NULL(yyjson_obj_get(root, "next_offset"));
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "truncated")));
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "truncation_reason")), "engine_limit");
+ yyjson_doc_free(doc);
+ free(json);
+
+ /* Even a fully materialized total becomes a lower-bound report when the
+ * byte budget returns too few rows to reach it with a legal next offset. */
+ json = cbm_mcp_render_semantic_paging_for_testing(100000, 99998, 1, 2, true, true);
+ ASSERT_NOT_NULL(json);
+ doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc);
+ root = yyjson_doc_get_root(doc);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "semantic_total_relation")), "gte");
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "semantic_engine_saturated")));
+ ASSERT_NULL(yyjson_obj_get(root, "semantic_next_offset"));
+ ASSERT_NULL(yyjson_obj_get(root, "next_offset"));
+ yyjson_doc_free(doc);
+ free(json);
+
+ char *tree = cbm_mcp_render_semantic_paging_for_testing(100000, 99998, 1, 1, false, false);
+ ASSERT_NOT_NULL(tree);
+ ASSERT_NOT_NULL(strstr(tree, "semantic_total_relation: gte"));
+ ASSERT_NOT_NULL(strstr(tree, "semantic_has_more: false"));
+ ASSERT_NOT_NULL(strstr(tree, "semantic_engine_saturated: true"));
+ ASSERT_NOT_NULL(strstr(tree, "semantic_continuation_unavailable: true"));
+ ASSERT_NULL(strstr(tree, "semantic_next_offset:"));
+ ASSERT_NOT_NULL(strstr(tree, "has_more: false"));
+ ASSERT_NULL(strstr(tree, "next_offset:"));
+ ASSERT_NOT_NULL(strstr(tree, "truncated: true"));
+ ASSERT_NOT_NULL(strstr(tree, "truncation_reason: engine_limit"));
+ free(tree);
+
+ /* One row earlier, next=99,998 is still valid and must remain available. */
+ json = cbm_mcp_render_semantic_paging_for_testing(100000, 99997, 1, 1, false, true);
+ ASSERT_NOT_NULL(json);
+ doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc);
+ root = yyjson_doc_get_root(doc);
+ ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "semantic_has_more")));
+ ASSERT_EQ(yyjson_get_int(yyjson_obj_get(root, "semantic_next_offset")), 99998);
+ ASSERT_NULL(yyjson_obj_get(root, "semantic_engine_saturated"));
+ ASSERT_NULL(yyjson_obj_get(root, "semantic_continuation_unavailable"));
+ yyjson_doc_free(doc);
+ free(json);
+
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *store = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(store);
+ cbm_mcp_server_set_project(srv, "semantic-ceiling");
+ ASSERT_EQ(cbm_store_upsert_project(store, "semantic-ceiling", "/tmp/semantic-ceiling"),
+ CBM_STORE_OK);
+ char *response = cbm_mcp_handle_tool(
+ srv, "search_graph",
+ "{\"project\":\"semantic-ceiling\",\"semantic_query\":[\"x\"],"
+ "\"semantic_offset\":99999,\"format\":\"json\"}");
+ ASSERT_NOT_NULL(response);
+ ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
+ ASSERT_NOT_NULL(strstr(response, "semantic_offset maximum is 99998"));
+ free(response);
+ cbm_mcp_server_free(srv);
+
+ char *tools = cbm_mcp_tools_list();
+ ASSERT_NOT_NULL(tools);
+ ASSERT_NOT_NULL(strstr(tools, "\"semantic_offset\":{\"type\":\"integer\",\"default\":0,"
+ "\"minimum\":0,\"maximum\":99998}"));
+ free(tools);
+ PASS();
+}
+
enum { MCP_TEST_SEMANTIC_VECTOR_DIM = 768 };
static int mcp_test_insert_semantic_vector(cbm_store_t *store, const char *table,
@@ -18915,6 +19049,8 @@ SUITE(mcp) {
RUN_TEST(tool_output_byte_budgets);
RUN_TEST(tool_search_graph_query_honors_file_pattern_issue552);
RUN_TEST(tool_search_graph_bm25_reports_candidate_saturation);
+ RUN_TEST(tool_search_graph_rejects_bm25_and_semantic_query_together);
+ RUN_TEST(tool_search_graph_semantic_ceiling_never_emits_unusable_continuation);
RUN_TEST(tool_search_graph_semantic_pagination_is_lossless_and_independent);
RUN_TEST(tool_search_graph_budget_preserves_long_values_and_continuation);
RUN_TEST(mcp_resource_discovery_methods_return_empty_lists);