Skip to content

Commit 6ecbc5f

Browse files
authored
Merge pull request DeusData#1605 from ertankucukoglu/perf/search-code-counters
perf(mcp): report search phase timings
2 parents e51f7b5 + 2eb46cf commit 6ecbc5f

2 files changed

Lines changed: 90 additions & 15 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ static const tool_def_t TOOLS[] = {
597597
"signatures+metadata (default). full: with source. files: just file list.\"},"
598598
"\"context\":{\"type\":\"integer\",\"description\":\"Lines of context around each match "
599599
"(like grep -C). Only used in compact mode.\"},"
600-
"\"regex\":{\"type\":\"boolean\",\"default\":false},\"limit\":{\"type\":\"integer\","
600+
"\"regex\":{\"type\":\"boolean\",\"default\":false},\"debug\":{\"type\":\"boolean\","
601+
"\"default\":false,\"description\":\"Include scope_ms, scan_ms, and enrich_ms phase timing "
602+
"diagnostics.\"},\"limit\":{\"type\":\"integer\","
601603
"\"description\":\"Max enriched results per call. Default 10. Response includes "
602604
"'total_grep_matches' and 'total_results' so callers can detect truncation. No "
603605
"offset parameter — raise limit or narrow with file_pattern / path_filter to see more."
@@ -8722,6 +8724,14 @@ typedef struct {
87228724
int match_count;
87238725
} search_result_t;
87248726

8727+
typedef struct {
8728+
uint64_t scope_ms;
8729+
uint64_t scan_ms;
8730+
uint64_t enrich_ms;
8731+
uint64_t elapsed_ms;
8732+
bool include_phase_timings;
8733+
} search_metrics_t;
8734+
87258735
/* Score a result for ranking: project source first, vendored last, tests lowest */
87268736
enum { SCORE_FUNC = 10, SCORE_ROUTE = 15, SCORE_VENDORED = -50, SCORE_TEST = -5 };
87278737
enum { MAX_LINE_SPAN = 999999 };
@@ -9012,7 +9022,7 @@ static yyjson_mut_val *build_dir_distribution(yyjson_mut_doc *doc, search_result
90129022
* distribution table, and the summary scalars. */
90139023
static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep_match_t *raw,
90149024
int raw_count, int gm_count, int limit,
9015-
bool warn_literal_pipe, uint64_t elapsed_ms) {
9025+
bool warn_literal_pipe, const search_metrics_t *metrics) {
90169026
enum { MAX_RAW = 20, SEARCH_SLOW_MS = 5000 };
90179027
cbm_sb_t sb;
90189028
cbm_sb_init(&sb);
@@ -9082,14 +9092,19 @@ static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep
90829092
cbm_tree_scalar_int(&sb, "total_grep_matches", gm_count);
90839093
cbm_tree_scalar_int(&sb, "total_results", sr_count);
90849094
cbm_tree_scalar_int(&sb, "raw_match_count", raw_count);
9085-
cbm_tree_scalar_int(&sb, "elapsed_ms", (long long)elapsed_ms);
9095+
if (metrics->include_phase_timings) {
9096+
cbm_tree_scalar_int(&sb, "scope_ms", (long long)metrics->scope_ms);
9097+
cbm_tree_scalar_int(&sb, "scan_ms", (long long)metrics->scan_ms);
9098+
cbm_tree_scalar_int(&sb, "enrich_ms", (long long)metrics->enrich_ms);
9099+
}
9100+
cbm_tree_scalar_int(&sb, "elapsed_ms", (long long)metrics->elapsed_ms);
90869101
if (warn_literal_pipe) {
90879102
cbm_tree_scalar_str(&sb, "warning",
90889103
"pattern contains '|' but regex=false, so it is matched literally "
90899104
"(not as alternation). Pass regex=true for 'foo|bar' to mean "
90909105
"'foo OR bar'.");
90919106
}
9092-
if (elapsed_ms >= SEARCH_SLOW_MS) {
9107+
if (metrics->elapsed_ms >= SEARCH_SLOW_MS) {
90939108
cbm_tree_scalar_str(&sb, "warning_slow",
90949109
"search was slow; narrow file_pattern/path_filter or use a more "
90959110
"specific pattern");
@@ -9101,7 +9116,7 @@ static char *assemble_search_output_toon(search_result_t *sr, int sr_count, grep
91019116
static char *assemble_search_output(search_result_t *sr, int sr_count, grep_match_t *raw,
91029117
int raw_count, int gm_count, int limit, int mode,
91039118
int context_lines, const char *root_path,
9104-
bool warn_literal_pipe, uint64_t elapsed_ms) {
9119+
bool warn_literal_pipe, const search_metrics_t *metrics) {
91059120
enum { MODE_COMPACT = 0, MODE_FULL = 1, MODE_FILES = 2, SEARCH_SLOW_MS = 5000 };
91069121

91079122
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
@@ -9185,7 +9200,12 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
91859200
yyjson_mut_obj_add_int(doc, root_obj, "total_grep_matches", gm_count);
91869201
yyjson_mut_obj_add_int(doc, root_obj, "total_results", sr_count);
91879202
yyjson_mut_obj_add_int(doc, root_obj, "raw_match_count", raw_count);
9188-
yyjson_mut_obj_add_int(doc, root_obj, "elapsed_ms", (int)elapsed_ms);
9203+
if (metrics->include_phase_timings) {
9204+
yyjson_mut_obj_add_uint(doc, root_obj, "scope_ms", metrics->scope_ms);
9205+
yyjson_mut_obj_add_uint(doc, root_obj, "scan_ms", metrics->scan_ms);
9206+
yyjson_mut_obj_add_uint(doc, root_obj, "enrich_ms", metrics->enrich_ms);
9207+
}
9208+
yyjson_mut_obj_add_uint(doc, root_obj, "elapsed_ms", metrics->elapsed_ms);
91899209
if (sr_count > 0 && gm_count > 0) {
91909210
char ratio[CBM_SZ_32];
91919211
snprintf(ratio, sizeof(ratio), "%.1fx", (double)gm_count / (double)(sr_count + raw_count));
@@ -9200,15 +9220,15 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
92009220
"pattern contains '|' but regex=false, so it is matched literally (not as "
92019221
"alternation). Pass regex=true for 'foo|bar' to mean 'foo OR bar'.");
92029222
}
9203-
if (elapsed_ms >= SEARCH_SLOW_MS) {
9223+
if (metrics->elapsed_ms >= SEARCH_SLOW_MS) {
92049224
char slow[CBM_SZ_128];
92059225
snprintf(slow, sizeof(slow),
92069226
"search took %dms (>%ds); narrow file_pattern/path_filter or use a more "
92079227
"specific pattern",
9208-
(int)elapsed_ms, SEARCH_SLOW_MS / 1000);
9228+
(int)metrics->elapsed_ms, SEARCH_SLOW_MS / 1000);
92099229
yyjson_mut_arr_add_strcpy(doc, warnings, slow);
92109230
char ems[CBM_SZ_32];
9211-
snprintf(ems, sizeof(ems), "%d", (int)elapsed_ms);
9231+
snprintf(ems, sizeof(ems), "%d", (int)metrics->elapsed_ms);
92129232
cbm_log_warn("search.slow", "elapsed_ms", ems); /* visibility in logs */
92139233
}
92149234
if (yyjson_mut_arr_size(warnings) > 0) {
@@ -9684,6 +9704,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
96849704
int context_lines = cbm_mcp_get_int_arg(args, "context", 0);
96859705
bool use_regex = cbm_mcp_get_bool_arg(args, "regex");
96869706
uint64_t search_t0 = cbm_now_ms();
9707+
search_metrics_t metrics = {0};
9708+
metrics.include_phase_timings = cbm_mcp_get_bool_arg(args, "debug");
96879709
/* In literal (non-regex) mode a '|' is matched as a byte, not alternation —
96889710
* a common silent 0-match trap; flagged in the result warnings (#282). */
96899711
bool pat_has_pipe = pattern && strchr(pattern, '|') != NULL;
@@ -9819,17 +9841,22 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98199841
bool scoped = false;
98209842
int scoped_written = 0;
98219843

9844+
uint64_t scope_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
98229845
scoped = write_scoped_filelist(srv, project, root_path, scratch.filelist, has_path_filter,
98239846
has_path_filter ? &path_regex : NULL, &scoped_written);
98249847
/* Close before grep runs: this is what flushes the records the helper wrote
98259848
* through the descriptor. Clearing the field hands ownership to
98269849
* search_scratch_close, which still unlinks the file itself. */
98279850
(void)fclose(scratch.filelist);
98289851
scratch.filelist = NULL;
9852+
if (metrics.include_phase_timings) {
9853+
metrics.scope_ms = cbm_now_ms() - scope_t0;
9854+
}
98299855

98309856
/* Collect grep matches into array */
98319857
int gm_count = 0;
98329858
grep_match_t *gm = NULL;
9859+
uint64_t scan_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
98339860
if (scoped && scoped_written == 0) {
98349861
/* The path_filter excluded every indexed file — nothing to scan.
98359862
* Skip the grep subprocess: xargs on an empty filelist is
@@ -9862,11 +9889,15 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98629889
* code, the file list is removed even when the scan was not scoped. */
98639890
search_scratch_close(&scratch);
98649891
}
9892+
if (metrics.include_phase_timings) {
9893+
metrics.scan_ms = cbm_now_ms() - scan_t0;
9894+
}
98659895

98669896
/* ── Phase 2+3: Block expansion + graph ranking ──────────── */
98679897
/* Sort grep matches by file for contiguous processing.
98689898
* Then: one SQL query per unique file for nodes, one batch query for all degrees. */
98699899

9900+
uint64_t enrich_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
98709901
cbm_store_t *store = resolve_store(srv, project);
98719902

98729903
int sr_cap = CBM_SZ_32;
@@ -9910,6 +9941,10 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
99109941
if (sr_count > SKIP_ONE) {
99119942
qsort(sr, sr_count, sizeof(search_result_t), search_result_cmp);
99129943
}
9944+
if (metrics.include_phase_timings) {
9945+
metrics.enrich_ms = cbm_now_ms() - enrich_t0;
9946+
}
9947+
metrics.elapsed_ms = cbm_now_ms() - search_t0;
99139948

99149949
/* ── Phase 4: Context assembly (extracted helper) ─────────── */
99159950

@@ -9922,15 +9957,14 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
99229957

99239958
char *result = NULL;
99249959
if (mode == 0 && !sc_legacy_json) {
9925-
char *toon_text =
9926-
assemble_search_output_toon(sr, sr_count, raw, raw_count, gm_count, limit,
9927-
pat_has_pipe && !use_regex, cbm_now_ms() - search_t0);
9960+
char *toon_text = assemble_search_output_toon(sr, sr_count, raw, raw_count, gm_count, limit,
9961+
pat_has_pipe && !use_regex, &metrics);
99289962
result = cbm_mcp_text_result(toon_text ? toon_text : "out of memory", toon_text == NULL);
99299963
free(toon_text);
99309964
} else {
9931-
result = assemble_search_output(sr, sr_count, raw, raw_count, gm_count, limit, mode,
9932-
context_lines, root_path, pat_has_pipe && !use_regex,
9933-
cbm_now_ms() - search_t0);
9965+
result =
9966+
assemble_search_output(sr, sr_count, raw, raw_count, gm_count, limit, mode,
9967+
context_lines, root_path, pat_has_pipe && !use_regex, &metrics);
99349968
}
99359969
free(gm);
99369970
free(sr);

‎tests/test_mcp.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,6 +4599,46 @@ TEST(search_code_literal_pipe_warns_issue282) {
45994599
PASS();
46004600
}
46014601

4602+
TEST(search_code_reports_phase_timings_only_in_debug_mode) {
4603+
char tmp[512];
4604+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
4605+
ASSERT_NOT_NULL(srv);
4606+
4607+
char *response = cbm_mcp_handle_tool(
4608+
srv, "search_code", "{\"pattern\":\"HandleRequest\",\"project\":\"test-project\"}");
4609+
ASSERT_NOT_NULL(response);
4610+
ASSERT_NULL(strstr(response, "scope_ms"));
4611+
ASSERT_NULL(strstr(response, "scan_ms"));
4612+
ASSERT_NULL(strstr(response, "enrich_ms"));
4613+
ASSERT_NOT_NULL(strstr(response, "elapsed_ms"));
4614+
free(response);
4615+
4616+
response = cbm_mcp_handle_tool(
4617+
srv, "search_code",
4618+
"{\"pattern\":\"HandleRequest\",\"project\":\"test-project\",\"debug\":true}");
4619+
ASSERT_NOT_NULL(response);
4620+
ASSERT_NOT_NULL(strstr(response, "scope_ms"));
4621+
ASSERT_NOT_NULL(strstr(response, "scan_ms"));
4622+
ASSERT_NOT_NULL(strstr(response, "enrich_ms"));
4623+
ASSERT_NOT_NULL(strstr(response, "elapsed_ms"));
4624+
free(response);
4625+
4626+
response = cbm_mcp_handle_tool(
4627+
srv, "search_code",
4628+
"{\"pattern\":\"HandleRequest\",\"project\":\"test-project\",\"format\":\"json\","
4629+
"\"debug\":true}");
4630+
ASSERT_NOT_NULL(response);
4631+
ASSERT_NOT_NULL(strstr(response, "\\\"scope_ms\\\":"));
4632+
ASSERT_NOT_NULL(strstr(response, "\\\"scan_ms\\\":"));
4633+
ASSERT_NOT_NULL(strstr(response, "\\\"enrich_ms\\\":"));
4634+
ASSERT_NOT_NULL(strstr(response, "\\\"elapsed_ms\\\":"));
4635+
free(response);
4636+
4637+
cleanup_snippet_dir(tmp);
4638+
cbm_mcp_server_free(srv);
4639+
PASS();
4640+
}
4641+
46024642
/* issue #272: '&' in a path / file_pattern is neutralised by the command's
46034643
* quoting and must no longer be rejected as "invalid characters". */
46044644
TEST(search_code_ampersand_accepted_issue272) {
@@ -10768,6 +10808,7 @@ SUITE(mcp) {
1076810808
RUN_TEST(search_code_windows_prefilter_precedes_content_scan);
1076910809
RUN_TEST(search_code_invalid_regex_errors_issue283);
1077010810
RUN_TEST(search_code_literal_pipe_warns_issue282);
10811+
RUN_TEST(search_code_reports_phase_timings_only_in_debug_mode);
1077110812
RUN_TEST(search_code_ampersand_accepted_issue272);
1077210813
RUN_TEST(tool_detect_changes_no_project);
1077310814
RUN_TEST(tool_manage_adr_no_project);

0 commit comments

Comments
 (0)