| 16490 | } |
| 16491 | |
| 16492 | static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) { |
| 16493 | char *pattern = cbm_mcp_get_string_arg(args, "pattern"); |
| 16494 | char *project = get_project_arg(args); |
| 16495 | char *file_pattern = cbm_mcp_get_string_arg(args, "file_pattern"); |
| 16496 | char *path_filter = cbm_mcp_get_string_arg(args, "path_filter"); |
| 16497 | char *mode_str = cbm_mcp_get_string_arg(args, "mode"); |
| 16498 | int legacy_limit = cbm_mcp_get_int_arg(args, "limit", MCP_DEFAULT_LIMIT); |
| 16499 | /* #1511: a negative limit flowed straight into the result cap and came back |
| 16500 | * as the reported count ("results: -5"), which reads to an agent as a real |
| 16501 | * answer rather than a rejected argument. The schema now declares |
| 16502 | * minimum:1, but a schema is a request to the client, never a guarantee to |
| 16503 | * the server — clamp here too. */ |
| 16504 | if (legacy_limit < 1) { |
| 16505 | legacy_limit = MCP_DEFAULT_LIMIT; |
| 16506 | } |
| 16507 | int result_limit = cbm_mcp_get_int_arg(args, "result_limit", legacy_limit); |
| 16508 | int result_offset = cbm_mcp_get_int_arg(args, "result_offset", 0); |
| 16509 | int context_lines = cbm_mcp_get_int_arg(args, "context", 0); |
| 16510 | if (context_lines < 0) { |
| 16511 | context_lines = 0; |
| 16512 | } else if (context_lines > MCP_SEARCH_CONTEXT_MAX_LINES) { |
| 16513 | context_lines = MCP_SEARCH_CONTEXT_MAX_LINES; |
| 16514 | } |
| 16515 | bool use_regex = cbm_mcp_get_bool_arg(args, "regex"); |
| 16516 | uint64_t search_t0 = cbm_now_ms(); |
| 16517 | search_metrics_t metrics = {0}; |
| 16518 | metrics.include_phase_timings = cbm_mcp_get_bool_arg(args, "debug"); |
| 16519 | /* In literal (non-regex) mode a '|' is matched as a byte, not alternation — |
| 16520 | * a common silent 0-match trap; flagged in the result warnings (#282). */ |
| 16521 | bool pat_has_pipe = pattern && strchr(pattern, '|') != NULL; |
| 16522 | |
| 16523 | int mode = parse_search_mode(mode_str); |
| 16524 | free(mode_str); |
| 16525 | |
| 16526 | if (result_limit < 1) { |
| 16527 | result_limit = MCP_DEFAULT_LIMIT; |
| 16528 | } else if (result_limit > 500) { |
| 16529 | result_limit = 500; |
| 16530 | } |
| 16531 | if (result_offset < 0) { |
| 16532 | result_offset = 0; |
| 16533 | } |
| 16534 | int raw_limit = cbm_mcp_get_int_arg(args, "raw_limit", 5); |
| 16535 | if (raw_limit < 0) { |
| 16536 | raw_limit = 0; |
| 16537 | } else if (raw_limit > 100) { |
| 16538 | raw_limit = 100; |
| 16539 | } |
| 16540 | int raw_offset = cbm_mcp_get_int_arg(args, "raw_offset", 0); |
| 16541 | if (raw_offset < 0) { |
| 16542 | raw_offset = 0; |
| 16543 | } |
| 16544 | int raw_content_offset_arg = cbm_mcp_get_int_arg(args, "raw_content_offset", 0); |
| 16545 | if (raw_content_offset_arg < 0) { |
| 16546 | raw_content_offset_arg = 0; |
| 16547 | } |
| 16548 | bool raw_content_offset_set = false; |
| 16549 | yyjson_doc *search_args_doc = args ? yyjson_read(args, strlen(args), 0) : NULL; |
no test coverage detected