Skip to content

Commit 69b26df

Browse files
authored
Merge pull request DeusData#2372 from lorenzozanee/fix/search-code-cancellation-observability
fix(mcp): report request timing in mcp.request logs
2 parents 129443a + 25665c6 commit 69b26df

5 files changed

Lines changed: 142 additions & 36 deletions

File tree

‎src/foundation/log.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,29 @@ static void copy_path_without_query(const char *path, char *out, size_t outsz) {
363363
}
364364

365365
void cbm_log_mcp_request(const char *method, const char *tool_name, bool is_error,
366-
int64_t duration_us) {
366+
int64_t duration_us, int64_t handler_duration_us,
367+
int64_t response_serialization_us, bool cancelled, bool timed_out) {
367368
char duration_ms[CBM_SZ_32];
369+
char handler_duration_ms[CBM_SZ_32];
370+
char response_serialization_ms[CBM_SZ_32];
368371
snprintf(duration_ms, sizeof(duration_ms), "%" PRId64, duration_us / 1000);
372+
snprintf(handler_duration_ms, sizeof(handler_duration_ms), "%" PRId64,
373+
handler_duration_us / 1000);
374+
snprintf(response_serialization_ms, sizeof(response_serialization_ms), "%" PRId64,
375+
response_serialization_us / 1000);
376+
const char *status = timed_out ? "timeout" : (is_error ? "error" : "ok");
369377
if (tool_name && tool_name[0] != '\0') {
370378
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "protocol", "jsonrpc",
371-
"method", method ? method : "", "tool", tool_name, "status",
372-
is_error ? "error" : "ok", "duration_ms", duration_ms, NULL);
379+
"method", method ? method : "", "tool", tool_name, "status", status, "duration_ms",
380+
duration_ms, "handler_duration_ms", handler_duration_ms,
381+
"response_serialization_ms", response_serialization_ms, "cancelled",
382+
cancelled ? "true" : "false", "timed_out", timed_out ? "true" : "false", NULL);
373383
} else {
374384
cbm_log(is_error ? CBM_LOG_WARN : CBM_LOG_INFO, "mcp.request", "protocol", "jsonrpc",
375-
"method", method ? method : "", "status", is_error ? "error" : "ok", "duration_ms",
376-
duration_ms, NULL);
385+
"method", method ? method : "", "status", status, "duration_ms", duration_ms,
386+
"handler_duration_ms", handler_duration_ms, "response_serialization_ms",
387+
response_serialization_ms, "cancelled", cancelled ? "true" : "false", "timed_out",
388+
timed_out ? "true" : "false", NULL);
377389
}
378390
}
379391

‎src/foundation/log.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ void cbm_log_int(CBMLogLevel level, const char *msg, const char *key, int64_t va
9595
/* Operational event helpers. They deliberately avoid request bodies, headers,
9696
* arguments, and query strings. */
9797
void cbm_log_mcp_request(const char *method, const char *tool_name, bool is_error,
98-
int64_t duration_us);
98+
int64_t duration_us, int64_t handler_duration_us,
99+
int64_t response_serialization_us, bool cancelled, bool timed_out);
99100
void cbm_log_http_request(const char *component, const char *method, const char *path, int status,
100101
int64_t duration_ms, size_t request_bytes, size_t response_bytes);
101102

‎src/mcp/mcp.c‎

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20936,6 +20936,11 @@ bool cbm_mcp_jsonrpc_response_prepend_notice(char **response_io, const char *not
2093620936
return true;
2093720937
}
2093820938

20939+
static long long mcp_elapsed_us(struct timespec start, struct timespec end) {
20940+
return ((long long)(end.tv_sec - start.tv_sec) * MCP_S_TO_US) +
20941+
((long long)(end.tv_nsec - start.tv_nsec) / MCP_MS_TO_US);
20942+
}
20943+
2093920944
char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2094020945
cbm_jsonrpc_request_t req = {0};
2094120946
if (cbm_jsonrpc_parse(line, &req) < 0) {
@@ -20966,7 +20971,11 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2096620971
cbm_clock_gettime(CLOCK_MONOTONIC, &req_t0);
2096720972
char *result_json = NULL;
2096820973
char *request_error_json = NULL;
20969-
bool request_logged = false;
20974+
char *request_tool_name = NULL;
20975+
long long handler_duration_us = 0;
20976+
bool request_is_error = false;
20977+
bool request_cancelled = false;
20978+
bool request_timed_out = false;
2097020979

2097120980
if (strcmp(req.method, "initialize") == 0) {
2097220981
result_json = cbm_mcp_initialize_response_for_profile(req.params_raw, srv->tool_profile);
@@ -20993,6 +21002,7 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2099321002
char *tool_name = req.params_raw ? cbm_mcp_get_tool_name(req.params_raw) : NULL;
2099421003
char *tool_args =
2099521004
req.params_raw ? cbm_mcp_get_arguments(req.params_raw) : heap_strdup("{}");
21005+
request_tool_name = tool_name;
2099621006
srv->active_request_id = req.id;
2099721007
free(srv->active_request_id_str);
2099821008
srv->active_request_id_str = req.id_str ? heap_strdup(req.id_str) : NULL;
@@ -21005,16 +21015,15 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2100521015
srv->active_request_id_str = NULL;
2100621016
struct timespec t1;
2100721017
cbm_clock_gettime(CLOCK_MONOTONIC, &t1);
21008-
long long dur_us = ((long long)(t1.tv_sec - t0.tv_sec) * MCP_S_TO_US) +
21009-
((long long)(t1.tv_nsec - t0.tv_nsec) / MCP_MS_TO_US);
21018+
long long dur_us = mcp_elapsed_us(t0, t1);
2101021019
bool is_err = (result_json != NULL) && (strstr(result_json, "\"isError\":true") != NULL);
2101121020
cbm_diag_record_query(dur_us, is_err);
21012-
long long request_dur_us = ((long long)(t1.tv_sec - req_t0.tv_sec) * MCP_S_TO_US) +
21013-
((long long)(t1.tv_nsec - req_t0.tv_nsec) / MCP_MS_TO_US);
21014-
cbm_log_mcp_request(req.method, tool_name, is_err, request_dur_us);
21015-
request_logged = true;
21021+
handler_duration_us = mcp_elapsed_us(req_t0, t1);
21022+
request_is_error = is_err;
21023+
request_cancelled = mcp_request_cancelled(srv);
21024+
request_timed_out =
21025+
result_json && strstr(result_json, "\"code\":\"request_timeout\"") != NULL;
2101621026

21017-
free(tool_name);
2101821027
free(tool_args);
2101921028
} else {
2102021029
/* Echo the original id (string or numeric, issue #253) on the error. */
@@ -21026,14 +21035,20 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2102621035
.id_str = req.id_str,
2102721036
.error_json = err_obj,
2102821037
};
21029-
char *err = cbm_jsonrpc_format_response(&err_resp);
2103021038
struct timespec t1;
2103121039
cbm_clock_gettime(CLOCK_MONOTONIC, &t1);
21032-
long long dur_us = ((long long)(t1.tv_sec - req_t0.tv_sec) * MCP_S_TO_US) +
21033-
((long long)(t1.tv_nsec - req_t0.tv_nsec) / MCP_MS_TO_US);
21040+
long long handler_us = mcp_elapsed_us(req_t0, t1);
21041+
struct timespec serialize_t0;
21042+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t0);
21043+
char *err = cbm_jsonrpc_format_response(&err_resp);
21044+
struct timespec serialize_t1;
21045+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t1);
21046+
long long serialization_us = mcp_elapsed_us(serialize_t0, serialize_t1);
21047+
long long duration_us = mcp_elapsed_us(req_t0, serialize_t1);
2103421048
/* server/discover is the modern connect probe. -32601 stays so the
2103521049
* client falls back to initialize; that miss is not an error (#2433). */
21036-
cbm_log_mcp_request(req.method, NULL, strcmp(req.method, "server/discover") != 0, dur_us);
21050+
cbm_log_mcp_request(req.method, NULL, strcmp(req.method, "server/discover") != 0,
21051+
duration_us, handler_us, serialization_us, false, false);
2103721052
cbm_mcp_server_request_scope_end(srv);
2103821053
cbm_jsonrpc_request_free(&req);
2103921054
return err;
@@ -21045,32 +21060,42 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
2104521060
.id_str = req.id_str,
2104621061
.error_json = request_error_json,
2104721062
};
21063+
struct timespec serialize_t0;
21064+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t0);
2104821065
char *err = cbm_jsonrpc_format_response(&err_resp);
21049-
struct timespec t1;
21050-
cbm_clock_gettime(CLOCK_MONOTONIC, &t1);
21051-
long long dur_us = ((long long)(t1.tv_sec - req_t0.tv_sec) * MCP_S_TO_US) +
21052-
((long long)(t1.tv_nsec - req_t0.tv_nsec) / MCP_MS_TO_US);
21053-
cbm_log_mcp_request(req.method, NULL, true, dur_us);
21066+
struct timespec serialize_t1;
21067+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t1);
21068+
long long handler_us = mcp_elapsed_us(req_t0, serialize_t0);
21069+
long long serialization_us = mcp_elapsed_us(serialize_t0, serialize_t1);
21070+
long long duration_us = mcp_elapsed_us(req_t0, serialize_t1);
21071+
cbm_log_mcp_request(req.method, NULL, true, duration_us, handler_us, serialization_us,
21072+
false, false);
2105421073
free(request_error_json);
2105521074
cbm_mcp_server_request_scope_end(srv);
2105621075
cbm_jsonrpc_request_free(&req);
2105721076
return err;
2105821077
}
2105921078

21060-
if (!request_logged) {
21061-
struct timespec t1;
21062-
cbm_clock_gettime(CLOCK_MONOTONIC, &t1);
21063-
long long dur_us = ((long long)(t1.tv_sec - req_t0.tv_sec) * MCP_S_TO_US) +
21064-
((long long)(t1.tv_nsec - req_t0.tv_nsec) / MCP_MS_TO_US);
21065-
cbm_log_mcp_request(req.method, NULL, false, dur_us);
21066-
}
21067-
2106821079
cbm_jsonrpc_response_t resp = {
2106921080
.id = req.id,
2107021081
.id_str = req.id_str,
2107121082
.result_json = result_json,
2107221083
};
21084+
struct timespec serialize_t0;
21085+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t0);
2107321086
char *out = cbm_jsonrpc_format_response(&resp);
21087+
struct timespec serialize_t1;
21088+
cbm_clock_gettime(CLOCK_MONOTONIC, &serialize_t1);
21089+
long long serialization_us = mcp_elapsed_us(serialize_t0, serialize_t1);
21090+
long long duration_us = mcp_elapsed_us(req_t0, serialize_t1);
21091+
request_is_error = request_is_error || out == NULL;
21092+
if (!request_tool_name) {
21093+
handler_duration_us = duration_us - serialization_us;
21094+
}
21095+
cbm_log_mcp_request(req.method, request_tool_name, request_is_error, duration_us,
21096+
handler_duration_us, serialization_us, request_cancelled,
21097+
request_timed_out);
21098+
free(request_tool_name);
2107421099
free(result_json);
2107521100
cbm_mcp_server_request_scope_end(srv);
2107621101
cbm_jsonrpc_request_free(&req);

‎tests/test_log.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ TEST(log_operational_helpers) {
207207
cbm_log_set_level(CBM_LOG_DEBUG);
208208
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
209209
capture_start();
210-
cbm_log_mcp_request("tools/call", "search_graph", false, 1250);
210+
cbm_log_mcp_request("tools/call", "search_graph", false, 1250000, 1000000, 250000, false,
211+
false);
211212
cbm_log_http_request("graph_ui", "GET", "/api/layout", 200, 7, 0, 42);
212213
const char *output = capture_end();
213214
cbm_log_set_level(CBM_LOG_INFO);
@@ -216,10 +217,26 @@ TEST(log_operational_helpers) {
216217
ASSERT(cbm_str_contains_raw(output, "protocol=jsonrpc"));
217218
ASSERT(cbm_str_contains_raw(output, "method=tools/call"));
218219
ASSERT(cbm_str_contains_raw(output, "tool=search_graph"));
220+
ASSERT(cbm_str_contains_raw(output, "handler_duration_ms=1000"));
221+
ASSERT(cbm_str_contains_raw(output, "response_serialization_ms=250"));
222+
ASSERT(cbm_str_contains_raw(output, "cancelled=false"));
223+
ASSERT(cbm_str_contains_raw(output, "timed_out=false"));
219224
ASSERT(cbm_str_contains_raw(output, "msg=http.request"));
220225
ASSERT(cbm_str_contains_raw(output, "method=GET"));
221226
ASSERT(cbm_str_contains_raw(output, "path=/api/layout"));
222227
ASSERT(cbm_str_contains_raw(output, "status=200"));
228+
229+
/* A request that hit its deadline reports status=timeout, even though the
230+
* tool result itself is an error. */
231+
cbm_log_set_level(CBM_LOG_DEBUG);
232+
capture_start();
233+
cbm_log_mcp_request("tools/call", "search_code", true, 2000000, 2000000, 0, false, true);
234+
output = capture_end();
235+
cbm_log_set_level(CBM_LOG_INFO);
236+
237+
ASSERT(cbm_str_contains_raw(output, "msg=mcp.request"));
238+
ASSERT(cbm_str_contains_raw(output, "status=timeout"));
239+
ASSERT(cbm_str_contains_raw(output, "timed_out=true"));
223240
PASS();
224241
}
225242

‎tests/test_mcp.c‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,10 @@ TEST(server_handle_logs_request_without_params) {
24442444
ASSERT_NOT_NULL(strstr(mcp_log_buf, "protocol=jsonrpc"));
24452445
ASSERT_NOT_NULL(strstr(mcp_log_buf, "method=tools/list"));
24462446
ASSERT_NOT_NULL(strstr(mcp_log_buf, "status=ok"));
2447+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "handler_duration_ms="));
2448+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "response_serialization_ms="));
2449+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "cancelled=false"));
2450+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "timed_out=false"));
24472451
ASSERT_NULL(strstr(mcp_log_buf, "token"));
24482452
ASSERT_NULL(strstr(mcp_log_buf, "secret"));
24492453
PASS();
@@ -12462,14 +12466,27 @@ TEST(search_code_cancel_cleans_supervised_scan) {
1246212466
};
1246312467
cbm_mcp_server_set_command_test_hook(srv, mcp_search_command_hook_probe, &probe);
1246412468

12465-
char *response =
12466-
cbm_mcp_handle_tool(srv, "search_code",
12467-
"{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","
12468-
"\"file_pattern\":\"*.go\"}");
12469+
mcp_log_buf[0] = '\0';
12470+
CBMLogLevel previous_level = cbm_log_get_level();
12471+
cbm_log_set_level(CBM_LOG_INFO);
12472+
cbm_log_set_sink_ex(mcp_capture_log, CBM_LOG_SINK_REPLACE);
12473+
char *response = cbm_mcp_server_handle(
12474+
srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","
12475+
"\"params\":{\"name\":\"search_code\",\"arguments\":{\"pattern\":"
12476+
"\"HandleRequest\",\"project\":\"prefilter-search\",\"file_pattern\":"
12477+
"\"*.go\"}}}");
12478+
cbm_log_set_sink(NULL);
12479+
cbm_log_set_level(previous_level);
1246912480
ASSERT_NOT_NULL(response);
1247012481
ASSERT_TRUE(probe.cancel_accepted);
1247112482
ASSERT_NOT_NULL(strstr(response, "cancelled"));
1247212483
ASSERT_NOT_NULL(strstr(response, "\"isError\":true"));
12484+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "msg=mcp.request"));
12485+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "tool=search_code"));
12486+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "cancelled=true"));
12487+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "timed_out=false"));
12488+
ASSERT_NULL(strstr(mcp_log_buf, "HandleRequest"));
12489+
ASSERT_NULL(strstr(mcp_log_buf, "prefilter-search"));
1247312490

1247412491
char logs[640];
1247512492
snprintf(logs, sizeof(logs), "%s/logs", cache.path);
@@ -12482,6 +12499,39 @@ TEST(search_code_cancel_cleans_supervised_scan) {
1248212499
PASS();
1248312500
}
1248412501

12502+
TEST(server_handle_logs_timeout_without_query) {
12503+
char tmp[512], src_path[768], vendor_path[768];
12504+
cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),
12505+
vendor_path, sizeof(vendor_path));
12506+
ASSERT_NOT_NULL(srv);
12507+
cbm_mcp_server_set_search_scan_timeout_for_test(srv, 0, true);
12508+
12509+
mcp_log_buf[0] = '\0';
12510+
CBMLogLevel previous_level = cbm_log_get_level();
12511+
cbm_log_set_level(CBM_LOG_INFO);
12512+
cbm_log_set_sink_ex(mcp_capture_log, CBM_LOG_SINK_REPLACE);
12513+
char *response = cbm_mcp_server_handle(
12514+
srv, "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
12515+
"\"params\":{\"name\":\"search_code\",\"arguments\":{\"pattern\":"
12516+
"\"HandleRequest\",\"project\":\"prefilter-search\"}}}");
12517+
cbm_log_set_sink(NULL);
12518+
cbm_log_set_level(previous_level);
12519+
12520+
ASSERT_NOT_NULL(response);
12521+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "status=timeout"));
12522+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "timed_out=true"));
12523+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "cancelled=false"));
12524+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "handler_duration_ms="));
12525+
ASSERT_NOT_NULL(strstr(mcp_log_buf, "response_serialization_ms="));
12526+
ASSERT_NULL(strstr(mcp_log_buf, "HandleRequest"));
12527+
ASSERT_NULL(strstr(mcp_log_buf, "prefilter-search"));
12528+
12529+
free(response);
12530+
cbm_mcp_server_free(srv);
12531+
cleanup_prefilter_dir(tmp, src_path, vendor_path);
12532+
PASS();
12533+
}
12534+
1248512535
TEST(search_code_output_limit_fails_closed_and_cleans_scan) {
1248612536
mcp_search_cache_t cache;
1248712537
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-limit"));
@@ -25323,6 +25373,7 @@ SUITE(mcp) {
2532325373
RUN_TEST(search_code_file_pattern_prefilter_boundaries);
2532425374
RUN_TEST(search_code_windows_scope_prefilter_removes_pipeline_filter);
2532525375
RUN_TEST(search_code_cancel_cleans_supervised_scan);
25376+
RUN_TEST(server_handle_logs_timeout_without_query);
2532625377
RUN_TEST(search_code_output_limit_fails_closed_and_cleans_scan);
2532725378
RUN_TEST(search_code_scan_deadline_fails_closed_and_resets);
2532825379
RUN_TEST(search_code_scan_deadline_override_is_per_server);

0 commit comments

Comments
 (0)