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
fix: preserve UTF-8 in search_code source
anfedoro05574fffec6b
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex fb5a750ec..10b047f98 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -8287,16 +8287,6 @@ static char *handle_get_code_snippet(cbm_mcp_server_t *srv, const char *args) {/* ── search_code v2: graph-augmented code search ─────────────── */-/* Strip non-ASCII bytes to guarantee valid UTF-8 JSON output */-enum { ASCII_MAX = 127 };-static void sanitize_ascii(char *s) {- for (unsigned char *p = (unsigned char *)s; *p; p++) {- if (*p > ASCII_MAX) {- *p = '?';- }- }-}-/* Intermediate grep match */typedef struct {char file[CBM_SZ_512];@@ -8497,8 +8487,11 @@ static void attach_result_source(yyjson_mut_doc *doc, yyjson_mut_val *item, sear}char *source = read_file_lines(abs_path, s, e);if (source) {- sanitize_ascii(source);- yyjson_mut_obj_add_strcpy(doc, item, "source", source);+ char *safe_source = sanitize_utf8_lossy(source);+ if (safe_source) {+ yyjson_mut_obj_add_strcpy(doc, item, "source", safe_source);+ free(safe_source);+ }free(source);if (truncated) {yyjson_mut_obj_add_int(doc, item, "source_start", s);@@ -8513,8 +8506,11 @@ static void attach_result_source(yyjson_mut_doc *doc, yyjson_mut_val *item, sear}char *ctx = read_file_lines(abs_path, ctx_start, ctx_end);if (ctx) {- sanitize_ascii(ctx);- yyjson_mut_obj_add_strcpy(doc, item, "context", ctx);+ char *safe_context = sanitize_utf8_lossy(ctx);+ if (safe_context) {+ yyjson_mut_obj_add_strcpy(doc, item, "context", safe_context);+ free(safe_context);+ }yyjson_mut_obj_add_int(doc, item, "context_start", ctx_start);free(ctx);}@@ -8783,7 +8779,11 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matcchar *json = yy_doc_to_str(doc);if (json) {- sanitize_ascii(json);+ char *safe_json = sanitize_utf8_lossy(json);+ if (safe_json) {+ free(json);+ json = safe_json;+ }}yyjson_mut_doc_free(doc);@@ -8854,8 +8854,10 @@ static grep_match_t *collect_grep_matches(FILE *fp, const char *root_path, size_safe_grow(gm, gm_count, gm_cap, PAIR_LEN);snprintf(gm[gm_count].file, sizeof(gm[0].file), "%s", file);gm[gm_count].line = (int)strtol(sep1 + SKIP_ONE, NULL, CBM_DECIMAL_BASE);- snprintf(gm[gm_count].content, sizeof(gm[0].content), "%s", sep2 + SKIP_ONE);- sanitize_ascii(gm[gm_count].content);+ char *safe_content = sanitize_utf8_lossy(sep2 + SKIP_ONE);+ snprintf(gm[gm_count].content, sizeof(gm[0].content), "%s",+ safe_content ? safe_content : sep2 + SKIP_ONE);+ free(safe_content);gm_count++;}diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex f1809dccd..53720a8e6 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -3530,6 +3530,76 @@ TEST(search_code_multi_word) {PASS();}+/* Regression guard: search_code full results must preserve valid UTF-8 source. */+TEST(search_code_full_preserves_utf8_source) {+ char tmp[512];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_utf8_XXXXXX");+ ASSERT_TRUE(cbm_mkdtemp(tmp) != NULL);++ char project_dir[640];+ snprintf(project_dir, sizeof(project_dir), "%s/project", tmp);+ ASSERT_EQ(cbm_mkdir(project_dir), 0);+ char design_dir[768];+ snprintf(design_dir, sizeof(design_dir), "%s/design", project_dir);+ ASSERT_EQ(cbm_mkdir(design_dir), 0);++ char source_path[768];+ snprintf(source_path, sizeof(source_path), "%s/design.md", design_dir);+ FILE *fp = cbm_fopen(source_path, "wb");+ ASSERT_NOT_NULL(fp);+ const char source[] = "# accounting-design\nРусский текст: бухгалтерский учет.\n";+ ASSERT_EQ(fwrite(source, 1, sizeof(source) - SKIP_ONE, fp), sizeof(source) - SKIP_ONE);+ ASSERT_EQ(fclose(fp), 0);++ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ ASSERT_NOT_NULL(srv);+ cbm_store_t *st = cbm_mcp_server_store(srv);+ ASSERT_NOT_NULL(st);+ const char *project = "utf8-search";+ cbm_mcp_server_set_project(srv, project);+ cbm_store_upsert_project(st, project, project_dir);++ cbm_node_t section = {.project = project,+ .label = "Section",+ .name = "accounting-design",+ .qualified_name = "utf8-search.design.accounting-design",+ .file_path = "design/design.md",+ .start_line = 1,+ .end_line = 2};+ ASSERT_GT(cbm_store_upsert_node(st, §ion), 0);++ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":97,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\",\"arguments\":{"+ "\"project\":\"utf8-search\",\"pattern\":\"accounting-design\","+ "\"file_pattern\":\"*.md\",\"path_filter\":\"^design/\","+ "\"mode\":\"full\",\"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 *rows = yyjson_obj_get(yyjson_doc_get_root(doc), "rows");+ ASSERT_NOT_NULL(rows);+ ASSERT_TRUE(yyjson_arr_size(rows) > 0);+ yyjson_val *row = yyjson_arr_get(rows, 0);+ yyjson_val *source_obj = yyjson_arr_get(row, 7);+ yyjson_val *source_val = yyjson_obj_get(source_obj, "source");+ ASSERT_NOT_NULL(source_val);+ ASSERT_STR_EQ(yyjson_get_str(source_val), source);+ yyjson_doc_free(doc);++ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cbm_unlink(source_path);+ cbm_rmdir(design_dir);+ cbm_rmdir(project_dir);+ cbm_rmdir(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@@ -9410,6 +9480,7 @@ SUITE(mcp) {RUN_TEST(tool_search_code_missing_pattern);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_scoped_path_with_spaces_issue687);#ifdef _WIN32RUN_TEST(search_code_scoped_path_with_cjk_root_issue903);