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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
test: cover UTF-8 search output paths
anfedoroefdbdd11789e
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex cf5222e0d..02ecddae3 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -8683,16 +8683,17 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matcbuild_dedup_files_array(doc, sr, output_count, raw, raw_count));} else {/* json-stringified tree: cols + column-ordered row arrays. FULL mode- * appends a per-row object cell with the (guarded, windowed) source —- * attach_result_source semantics unchanged. */+ * appends a per-row object cell with the (guarded, windowed) source;+ * context requests append the corresponding context object. */+ bool attach_context = context_lines > 0 && mode != MODE_FULL;yyjson_mut_val *jcols = yyjson_mut_arr(doc);static const char *const sc_cols[] = {"qn", "label", "file", "lines","matches", "in", "out"};for (size_t ci = 0; ci < sizeof(sc_cols) / sizeof(sc_cols[0]); ci++) {yyjson_mut_arr_add_str(doc, jcols, sc_cols[ci]);}- if (mode == MODE_FULL) {- yyjson_mut_arr_add_str(doc, jcols, "source");+ if (mode == MODE_FULL || attach_context) {+ yyjson_mut_arr_add_str(doc, jcols, mode == MODE_FULL ? "source" : "context");}yyjson_mut_obj_add_val(doc, root_obj, "cols", jcols);@@ -8718,7 +8719,7 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matcyyjson_mut_arr_add_val(row, ml);yyjson_mut_arr_add_int(doc, row, r->in_degree);yyjson_mut_arr_add_int(doc, row, r->out_degree);- if (mode == MODE_FULL) {+ if (mode == MODE_FULL || attach_context) {yyjson_mut_val *src = yyjson_mut_obj(doc);attach_result_source(doc, src, r, mode, context_lines, root_path);yyjson_mut_arr_add_val(row, src);diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex 9703dd37e..7a13a7aa7 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -3655,6 +3655,8 @@ TEST(search_code_multi_word) {}/* Regression guard: search_code full results must preserve valid UTF-8 source. */+static bool is_valid_json_response(const char *json);+TEST(search_code_full_preserves_utf8_source) {char tmp[512];snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_utf8_XXXXXX");@@ -3724,6 +3726,178 @@ TEST(search_code_full_preserves_utf8_source) {PASS();}+TEST(search_code_raw_match_preserves_utf8_content) {+ char tmp[256];+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));+ ASSERT_NOT_NULL(srv);++ char raw_path[512];+ snprintf(raw_path, sizeof(raw_path), "%s/project/raw.md", tmp);+ const char raw_source[] = "header\nraw-Русский content\n";+ FILE *fp = cbm_fopen(raw_path, "wb");+ ASSERT_NOT_NULL(fp);+ ASSERT_EQ(fwrite(raw_source, 1, sizeof(raw_source) - SKIP_ONE, fp),+ sizeof(raw_source) - SKIP_ONE);+ ASSERT_EQ(fclose(fp), 0);++ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ cbm_node_t node = {.project = "test-project",+ .label = "Section",+ .name = "raw",+ .qualified_name = "test-project.raw",+ .file_path = "raw.md",+ .start_line = 1,+ .end_line = 1};+ ASSERT_GT(cbm_store_upsert_node(st, &node), 0);++ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":98,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\",\"arguments\":{"+ "\"project\":\"test-project\",\"pattern\":\"raw-\","+ "\"file_pattern\":\"*.md\",\"format\":\"json\",\"limit\":5}}}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);+ ASSERT_NOT_NULL(doc);+ yyjson_val *raw_obj = yyjson_obj_get(yyjson_doc_get_root(doc), "raw_matches");+ ASSERT_NOT_NULL(raw_obj);+ yyjson_val *raw_rows = yyjson_obj_get(raw_obj, "rows");+ ASSERT_NOT_NULL(raw_rows);+ ASSERT_TRUE(yyjson_arr_size(raw_rows) > 0);+ yyjson_val *raw_row = yyjson_arr_get(raw_rows, 0);+ ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(raw_row, 2)), "raw-Русский content");+ yyjson_doc_free(doc);++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_snippet_dir(tmp);+ PASS();+}++TEST(search_code_context_preserves_utf8_context) {+ char tmp[256];+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));+ ASSERT_NOT_NULL(srv);++ char context_path[512];+ snprintf(context_path, sizeof(context_path), "%s/project/context.md", tmp);+ const char context_source[] = "before-до\ncontext-needle\nпосле-после\n";+ FILE *fp = cbm_fopen(context_path, "wb");+ ASSERT_NOT_NULL(fp);+ ASSERT_EQ(fwrite(context_source, 1, sizeof(context_source) - SKIP_ONE, fp),+ sizeof(context_source) - SKIP_ONE);+ ASSERT_EQ(fclose(fp), 0);++ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ cbm_node_t node = {.project = "test-project",+ .label = "Section",+ .name = "context",+ .qualified_name = "test-project.context",+ .file_path = "context.md",+ .start_line = 1,+ .end_line = 3};+ ASSERT_GT(cbm_store_upsert_node(st, &node), 0);++ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":99,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\",\"arguments\":{"+ "\"project\":\"test-project\",\"pattern\":\"context-needle\","+ "\"format\":\"json\",\"context\":1,\"limit\":5}}}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);+ ASSERT_NOT_NULL(doc);+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *cols = yyjson_obj_get(root, "cols");+ yyjson_val *rows = yyjson_obj_get(root, "rows");+ ASSERT_NOT_NULL(cols);+ ASSERT_NOT_NULL(rows);+ ASSERT_TRUE(yyjson_arr_size(rows) > 0);++ size_t context_index = SIZE_MAX;+ size_t col_count = yyjson_arr_size(cols);+ for (size_t i = 0; i < col_count; i++) {+ const char *col = yyjson_get_str(yyjson_arr_get(cols, i));+ if (col && strcmp(col, "context") == 0) {+ context_index = i;+ break;+ }+ }+ ASSERT_TRUE(context_index != SIZE_MAX);+ yyjson_val *context_obj = yyjson_arr_get(yyjson_arr_get(rows, 0), context_index);+ ASSERT_NOT_NULL(context_obj);+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(context_obj, "context")), context_source);+ ASSERT_EQ(yyjson_get_int(yyjson_obj_get(context_obj, "context_start")), 1);+ yyjson_doc_free(doc);++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_snippet_dir(tmp);+ PASS();+}++TEST(search_code_invalid_utf8_still_returns_valid_json) {+ char tmp[256];+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));+ ASSERT_NOT_NULL(srv);++ char invalid_path[512];+ snprintf(invalid_path, sizeof(invalid_path), "%s/project/invalid.md", tmp);+ static const unsigned char invalid_source[] = {+ 'i', 'n', 'v', 'a', 'l', 'i', 'd', '-', 'n', 'e', 'e', 'd', 'l', 'e', ' ',+ 0xFF, '\n',+ };+ FILE *fp = cbm_fopen(invalid_path, "wb");+ ASSERT_NOT_NULL(fp);+ ASSERT_EQ(fwrite(invalid_source, 1, sizeof(invalid_source), fp), sizeof(invalid_source));+ ASSERT_EQ(fclose(fp), 0);++ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ cbm_node_t node = {.project = "test-project",+ .label = "Section",+ .name = "invalid",+ .qualified_name = "test-project.invalid",+ .file_path = "invalid.md",+ .start_line = 1,+ .end_line = 1};+ ASSERT_GT(cbm_store_upsert_node(st, &node), 0);++ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":100,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\",\"arguments\":{"+ "\"project\":\"test-project\",\"pattern\":\"invalid-needle\","+ "\"mode\":\"full\",\"format\":\"json\",\"limit\":5}}}");+ ASSERT_NOT_NULL(resp);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_TRUE(is_valid_json_response(inner));+ yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);+ ASSERT_NOT_NULL(doc);+ yyjson_val *rows = yyjson_obj_get(yyjson_doc_get_root(doc), "rows");+ ASSERT_NOT_NULL(rows);+ ASSERT_TRUE(yyjson_arr_size(rows) > 0);+ yyjson_val *source_obj = yyjson_arr_get(yyjson_arr_get(rows, 0), 7);+ ASSERT_NOT_NULL(source_obj);+ const char *safe_source = yyjson_get_str(yyjson_obj_get(source_obj, "source"));+ ASSERT_NOT_NULL(safe_source);+ ASSERT_NOT_NULL(strstr(safe_source, "\xEF\xBF\xBD"));+ ASSERT_NULL(memchr(safe_source, 0xFF, strlen(safe_source)));+ yyjson_doc_free(doc);++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_snippet_dir(tmp);+ PASS();+}+/* Reproduce-first (#687): scoped content search over a repo whose ROOT PATH* contains a space. write_scoped_filelist emits "<root>/<file>" records that the* Unix pipeline pipes to grep via xargs. With plain `xargs` (newline-split) the@@ -9693,6 +9867,9 @@ SUITE(mcp) {RUN_TEST(tool_search_code_no_project);RUN_TEST(search_code_multi_word);RUN_TEST(search_code_full_preserves_utf8_source);+ RUN_TEST(search_code_raw_match_preserves_utf8_content);+ RUN_TEST(search_code_context_preserves_utf8_context);+ RUN_TEST(search_code_invalid_utf8_still_returns_valid_json);RUN_TEST(search_code_scoped_path_with_spaces_issue687);#ifdef _WIN32RUN_TEST(search_code_scoped_path_with_cjk_root_issue903);