Skip to content

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.

Add include_tests param to trace_path, mark test files in BFS results

Martin Vogelf25dafadff56
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 9d75b4ab3..35e4153b1 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -296,6 +296,9 @@ static const tool_def_t TOOLS[] = {
"scope trace to a specific parameter name\"},\"edge_types\":{\"type\":\"array\",\"items\":{"
"\"type\":\"string\"}},\"risk_labels\":{\"type\":\"boolean\",\"default\":false,"
"\"description\":\"Add risk classification (CRITICAL/HIGH/MEDIUM/LOW) based on hop distance"
+ "\"},\"include_tests\":{\"type\":\"boolean\",\"default\":false,"
+ "\"description\":\"Include test files in results. When false (default), test files are "
+ "filtered out. When true, test nodes are included with is_test=true marker."
"\"}},\"required\":[\"function_name\",\"project\"]}"},
{"get_code_snippet",
@@ -1313,11 +1316,26 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
return NULL;
}
-/* Convert BFS traversal results into a yyjson_mut array of {name, qualified_name, hop}. */
+/* Check if a file path looks like a test file. */
+static bool is_test_file(const char *path) {
+ if (!path) {
+ return false;
+ }
+ return strstr(path, "/test") != NULL || strstr(path, "test_") != NULL ||
+ strstr(path, "_test.") != NULL || strstr(path, "/tests/") != NULL ||
+ strstr(path, "/spec/") != NULL || strstr(path, ".test.") != NULL;
+}
+
+/* Convert BFS traversal results into a yyjson_mut array. */
static yyjson_mut_val *bfs_to_json_array(yyjson_mut_doc *doc, cbm_traverse_result_t *tr,
- bool risk_labels) {
+ bool risk_labels, bool include_tests) {
yyjson_mut_val *arr = yyjson_mut_arr(doc);
for (int i = 0; i < tr->visited_count; i++) {
+ const char *fp = tr->visited[i].node.file_path;
+ bool test = is_test_file(fp);
+ if (!include_tests && test) {
+ continue;
+ }
yyjson_mut_val *item = yyjson_mut_obj(doc);
yyjson_mut_obj_add_str(doc, item, "name",
tr->visited[i].node.name ? tr->visited[i].node.name : "");
@@ -1329,6 +1347,9 @@ static yyjson_mut_val *bfs_to_json_array(yyjson_mut_doc *doc, cbm_traverse_resul
yyjson_mut_obj_add_str(doc, item, "risk",
cbm_risk_label(cbm_hop_to_risk(tr->visited[i].hop)));
}
+ if (test) {
+ yyjson_mut_obj_add_bool(doc, item, "is_test", true);
+ }
yyjson_mut_arr_add_val(arr, item);
}
return arr;
@@ -1343,6 +1364,7 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
char *param_name = cbm_mcp_get_string_arg(args, "parameter_name");
int depth = cbm_mcp_get_int_arg(args, "depth", MCP_DEFAULT_DEPTH);
bool risk_labels = cbm_mcp_get_bool_arg(args, "risk_labels");
+ bool include_tests = cbm_mcp_get_bool_arg(args, "include_tests");
if (!func_name) {
free(project);
@@ -1419,13 +1441,15 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
if (do_outbound) {
cbm_store_bfs(store, nodes[0].id, "outbound", edge_types, edge_type_count, depth,
MCP_BFS_LIMIT, &tr_out);
- yyjson_mut_obj_add_val(doc, root, "callees", bfs_to_json_array(doc, &tr_out, risk_labels));
+ yyjson_mut_obj_add_val(doc, root, "callees",
+ bfs_to_json_array(doc, &tr_out, risk_labels, include_tests));
}
if (do_inbound) {
cbm_store_bfs(store, nodes[0].id, "inbound", edge_types, edge_type_count, depth,
MCP_BFS_LIMIT, &tr_in);
- yyjson_mut_obj_add_val(doc, root, "callers", bfs_to_json_array(doc, &tr_in, risk_labels));
+ yyjson_mut_obj_add_val(doc, root, "callers",
+ bfs_to_json_array(doc, &tr_in, risk_labels, include_tests));
}
/* Serialize BEFORE freeing traversal results (yyjson borrows strings) */
diff --git a/tests/test_incremental.c b/tests/test_incremental.c
index b039136f2..9e229bba2 100644
--- a/tests/test_incremental.c
+++ b/tests/test_incremental.c
@@ -2583,6 +2583,57 @@ TEST(tool_trace_defines_only) {
PASS();
}
+/* ── trace_path: include_tests flag ────────────────────────────── */
+
+TEST(tool_trace_include_tests) {
+ double ms;
+ /* Default (include_tests=false): test files should be filtered out */
+ char *r = call_tool_timed("trace_path", &ms,
+ "{\"project\":\"%s\","
+ "\"function_name\":\"incr_test_injected\","
+ "\"direction\":\"inbound\","
+ "\"include_tests\":false}",
+ g_project);
+ TOOL_OK(r, ms);
+ NOT_ERROR(r);
+ /* Result should not contain is_test markers when tests are excluded */
+ free(r);
+
+ /* With include_tests=true: test files should appear with is_test marker */
+ r = call_tool_timed("trace_path", &ms,
+ "{\"project\":\"%s\","
+ "\"function_name\":\"incr_test_injected\","
+ "\"direction\":\"inbound\","
+ "\"include_tests\":true}",
+ g_project);
+ TOOL_OK(r, ms);
+ NOT_ERROR(r);
+ free(r);
+ PASS();
+}
+
+/* ── trace_path: risk_labels flag ─────────────────────────────── */
+
+TEST(tool_trace_risk_labels) {
+ double ms;
+ /* Use outbound direction — incr_test_injected may call stdlib functions */
+ char *r = call_tool_timed("trace_path", &ms,
+ "{\"project\":\"%s\","
+ "\"function_name\":\"incr_test_injected\","
+ "\"direction\":\"outbound\","
+ "\"risk_labels\":true}",
+ g_project);
+ TOOL_OK(r, ms);
+ NOT_ERROR(r);
+ /* If there are callees, they should have risk labels.
+ * If no callees, the response is still valid — just empty. */
+ if (strstr(r, "\"hop\"") != NULL) {
+ ASSERT(strstr(r, "\"risk\"") != NULL);
+ }
+ free(r);
+ PASS();
+}
+
/* ── detect_changes: nonexistent branch ────────────────────────── */
TEST(tool_detect_nonexistent_branch) {
@@ -3014,6 +3065,8 @@ SUITE(incremental) {
/* Phase 35: trace_path remaining */
RUN_TEST(tool_trace_depth_0);
RUN_TEST(tool_trace_defines_only);
+ RUN_TEST(tool_trace_include_tests);
+ RUN_TEST(tool_trace_risk_labels);
/* Phase 36: detect_changes remaining */
RUN_TEST(tool_detect_nonexistent_branch);