@@ -4195,18 +4195,19 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
41954195}
41964196
41974197/* Extract keyword strings from a yyjson array into `keywords`. Returns the
4198- * number of strings copied (capped at `max_out`). */
4198+ * number of strings copied (capped at `max_out`), or -1 when any element is
4199+ * not a string: a mixed-type array is a caller error and is never silently
4200+ * narrowed to its string members. */
41994201static int extract_semantic_keywords(yyjson_val *sq_val, const char **keywords, int max_out) {
4200- int kw_count = (int)yyjson_arr_size(sq_val);
4201- if (kw_count > max_out) {
4202- kw_count = max_out;
4203- }
42044202 size_t kw_idx = 0;
42054203 size_t kw_max = 0;
42064204 yyjson_val *kw_val;
42074205 int ki = 0;
42084206 yyjson_arr_foreach(sq_val, kw_idx, kw_max, kw_val) {
4209- if (ki < kw_count && yyjson_is_str(kw_val)) {
4207+ if (!yyjson_is_str(kw_val)) {
4208+ return -1;
4209+ }
4210+ if (ki < max_out) {
42104211 keywords[ki++] = yyjson_get_str(kw_val);
42114212 }
42124213 }
@@ -4237,13 +4238,20 @@ static void emit_semantic_results(yyjson_mut_doc *doc, yyjson_mut_val *root,
42374238 yyjson_mut_obj_add_val(doc, root, "semantic", sem);
42384239}
42394240
4241+ typedef enum {
4242+ SQ_RUN_OK = 0,
4243+ SQ_RUN_TYPE_ERROR, /* semantic_query is not an array of strings */
4244+ SQ_RUN_STORE_ERROR, /* the vector scan itself failed */
4245+ } sq_run_status_t;
4246+
42404247/* Run the semantic_query vector search from raw args. Sets *out_vresults /
42414248 * *out_vcount (caller frees via cbm_store_free_vector_results when vcount>0).
4242- * Returns true if semantic_query was provided as a non-array (type error —
4243- * caller should surface to the user). */
4244- static bool run_semantic_query_core(const char *args, cbm_store_t *store, const char *project,
4245- int materialize_limit, cbm_vector_result_t **out_vresults,
4246- int *out_vcount, bool *out_present) {
4249+ * A store without a vector table (lean index) is an empty result; a failed
4250+ * scan is SQ_RUN_STORE_ERROR and must never be rendered as zero matches. */
4251+ static sq_run_status_t run_semantic_query_core(const char *args, cbm_store_t *store,
4252+ const char *project, int materialize_limit,
4253+ cbm_vector_result_t **out_vresults, int *out_vcount,
4254+ bool *out_present) {
42474255 enum { MAX_KW_SEARCH = 32 };
42484256 *out_vresults = NULL;
42494257 *out_vcount = 0;
@@ -4256,25 +4264,31 @@ static bool run_semantic_query_core(const char *args, cbm_store_t *store, const
42564264 if (out_present && sq_val) {
42574265 *out_present = true;
42584266 }
4259- bool type_error = false ;
4267+ sq_run_status_t status = SQ_RUN_OK ;
42604268 if (sq_val && !yyjson_is_arr(sq_val)) {
4261- type_error = true ;
4269+ status = SQ_RUN_TYPE_ERROR ;
42624270 } else if (sq_val && yyjson_arr_size(sq_val) > 0) {
42634271 const char *keywords[MAX_KW_SEARCH];
42644272 int ki = extract_semantic_keywords(sq_val, keywords, MAX_KW_SEARCH);
4265- cbm_vector_result_t *vresults = NULL;
4266- int vcount = 0;
4267- if (cbm_store_vector_search(store, project, keywords, ki, materialize_limit, &vresults,
4268- &vcount) == CBM_STORE_OK &&
4269- vcount > 0) {
4270- *out_vresults = vresults;
4271- *out_vcount = vcount;
4273+ if (ki < 0) {
4274+ status = SQ_RUN_TYPE_ERROR;
4275+ } else {
4276+ cbm_vector_result_t *vresults = NULL;
4277+ int vcount = 0;
4278+ int rc = cbm_store_vector_search(store, project, keywords, ki, materialize_limit,
4279+ &vresults, &vcount);
4280+ if (rc == CBM_STORE_ERR) {
4281+ status = SQ_RUN_STORE_ERROR;
4282+ } else if (rc == CBM_STORE_OK && vcount > 0) {
4283+ *out_vresults = vresults;
4284+ *out_vcount = vcount;
4285+ }
42724286 }
42734287 }
42744288 if (args_doc) {
42754289 yyjson_doc_free(args_doc);
42764290 }
4277- return type_error ;
4291+ return status ;
42784292}
42794293
42804294static bool search_graph_arg_present(const char *args, const char *name) {
@@ -5134,9 +5148,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
51345148 cbm_vector_result_t *vresults = NULL;
51355149 int vcount = 0;
51365150 bool sq_present = false;
5137- bool sq_type_error = run_semantic_query_core(args, store, project, semantic_materialize_limit,
5138- &vresults, &vcount, &sq_present);
5139- if (sq_type_error ) {
5151+ sq_run_status_t sq_status = run_semantic_query_core(
5152+ args, store, project, semantic_materialize_limit, &vresults, &vcount, &sq_present);
5153+ if (sq_status != SQ_RUN_OK ) {
51405154 if (fields_owner) {
51415155 yyjson_doc_free(fields_owner);
51425156 }
@@ -5146,11 +5160,17 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
51465160 free(qn_pattern);
51475161 free(file_pattern);
51485162 free(relationship);
5163+ if (sq_status == SQ_RUN_STORE_ERROR) {
5164+ return cbm_mcp_text_result(
5165+ "semantic search failed: the vector index could not be scanned — see the "
5166+ "server log for the SQLite error; reindex the project if it persists.",
5167+ true);
5168+ }
51495169 return cbm_mcp_text_result(
51505170 "semantic_query must be an array of keyword strings, e.g. "
5151- "[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
5152- "into individual keywords; each is scored independently via per-keyword "
5153- "min-cosine.",
5171+ "[\"send\",\"pubsub\",\"publish\"] — not a single string, and every element "
5172+ "must be a string. Split your query into individual keywords; each is scored "
5173+ "independently via per-keyword min-cosine.",
51545174 true);
51555175 }
51565176
0 commit comments