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.

feat(mcp): real impact analysis + cycle detection (multi-source BFS, SCC)

Martin Vogel130a82bd106b
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 6062416aa..a945ee051 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -25,8 +25,10 @@ enum {
MCP_DEFAULT_DEPTH = 3,
MCP_DEFAULT_BFS_DEPTH = 2,
MCP_DEFAULT_LIMIT = 10,
- MCP_BFS_LIMIT = 100, /* default per-direction trace budget (limit param raises) */
- MCP_BFS_LIMIT_MAX = 5000, /* hard ceiling for the limit param (context-bomb guard) */
+ MCP_BFS_LIMIT = 100, /* default per-direction trace budget (limit param raises) */
+ MCP_BFS_LIMIT_MAX = 5000, /* hard ceiling for the limit param (context-bomb guard) */
+ MCP_DEFAULT_IMPACT_LIMIT = 200, /* detect_changes per-symbol rows; rollup stays complete */
+ MCP_SNIPPET_MAX_LINES = 500, /* get_code_snippet line cap (whole-file Module guard) */
MCP_N_DEFAULTS_2 = 2,
MCP_URI_PREFIX = 7, /* strlen("file://") */
MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */
@@ -519,9 +521,12 @@ static const tool_def_t TOOLS[] = {
"apps/hoa)\"},"
"\"aspects\":{\"type\":\"array\",\"items\":{\"type\":\"string\",\"enum\":[\"all\","
"\"overview\",\"structure\",\"dependencies\",\"routes\",\"languages\",\"packages\","
- "\"entry_points\",\"hotspots\",\"boundaries\",\"layers\",\"file_tree\",\"clusters\"]},"
+ "\"entry_points\",\"hotspots\",\"boundaries\",\"layers\",\"file_tree\",\"clusters\","
+ "\"cycles\"]},"
"\"description\":\"Aspects to include. 'all' = everything; 'overview' = compact summary "
- "(all except file_tree); omit = all.\"}},\"required\":[\"project\"]}"},
+ "(all except file_tree); omit = all. 'cycles' is opt-in ONLY (never via all/overview): it "
+ "scans the whole call graph for circular CALLS dependencies (SCCs of size > 1).\"}},"
+ "\"required\":[\"project\"]}"},
{"search_code", "Search code",
"Graph-augmented code search. Finds text patterns via grep, then enriches results with "
@@ -591,11 +596,28 @@ static const tool_def_t TOOLS[] = {
"\"required\":[\"project\"],\"anyOf\":[{\"required\":[\"paths\"]},{\"required\":[\"scopes\"]}]"
"}"},
- {"detect_changes", "Detect changes", "Detect code changes and their impact",
+ {"detect_changes", "Detect changes",
+ "Map a git diff to its BLAST RADIUS. Resolves changed files to the symbols they define, then "
+ "runs ONE multi-source graph traversal to the transitive impact set. RESPONSE: base + "
+ "merge_base SHA, changed_files list, then impacted = prefix-grouped tree rows (name label "
+ "hop; "
+ "full qn = group prefix + dot + name) + an impacted_modules rollup; impacted_total + "
+ "truncated are exact. Seeds (the changed symbols) are excluded from impacted; a changed file "
+ "reached from another changed file is not counted as extra impact. format=\"json\" returns "
+ "the "
+ "same model as structured JSON.",
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"scope\":{\"type\":"
- "\"string\"},\"depth\":{\"type\":\"integer\",\"default\":2},\"base_branch\":{\"type\":"
+ "\"string\",\"enum\":[\"files\",\"impact\"],\"description\":\"files: changed files only "
+ "(no traversal). impact (default): files + the transitive impact set.\"},"
+ "\"direction\":{\"type\":\"string\",\"enum\":[\"inbound\",\"outbound\",\"both\"],\"default\":"
+ "\"inbound\",\"description\":\"inbound (default) = the blast radius: transitive CALLERS of "
+ "the "
+ "changed symbols. outbound = what the changed code depends on. both = union.\"},"
+ "\"depth\":{\"type\":\"integer\",\"default\":2,\"description\":\"Max traversal hops from the "
+ "changed symbols.\"},\"base_branch\":{\"type\":"
"\"string\",\"default\":\"main\"},\"since\":{\"type\":\"string\",\"description\":"
- "\"Git ref or tag to compare from (e.g. HEAD~5, v0.5.0). Diffs <ref>...HEAD.\"}},"
+ "\"Git ref or tag to compare from (e.g. HEAD~5, v0.5.0). Diffs <ref>...HEAD.\"},"
+ "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"],\"default\":\"tree\"}},"
"\"required\":"
"[\"project\"]}"},
@@ -3971,9 +3993,192 @@ static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) {
* of truth for the server-side validation (authoritative); the JSON-Schema
* enum in the TOOLS entry above is the advisory client-side mirror — update
* both together when the aspect set changes. */
-static const char *VALID_ASPECTS[] = {
- "all", "overview", "structure", "dependencies", "routes", "languages", "packages",
- "entry_points", "hotspots", "boundaries", "layers", "file_tree", "clusters", NULL};
+static const char *VALID_ASPECTS[] = {"all", "overview", "structure", "dependencies",
+ "routes", "languages", "packages", "entry_points",
+ "hotspots", "boundaries", "layers", "file_tree",
+ "clusters", "cycles", NULL};
+
+/* ── SCC / cycle condensation (get_architecture "cycles") ─────────
+ * Iterative Tarjan over the CALLS call graph. Recursion would overflow on a
+ * large graph, so the DFS state lives on explicit heap stacks. Reports the
+ * strongly-connected components of size > 1 — the circular call dependencies,
+ * the non-trivial content of the condensation quotient. */
+typedef struct {
+ int64_t *ids; /* sorted unique node ids; index = position */
+ int nverts; /* |V| */
+ int *adj_head; /* CSR row starts, length nverts+1 */
+ int *adj; /* CSR column (target vertex indices), length nedges */
+ int nedges; /* |E| within the vertex set */
+} scc_graph_t;
+
+static int scc_id_index(const int64_t *ids, int n, int64_t id) {
+ int lo = 0;
+ int hi = n - 1;
+ while (lo <= hi) {
+ int mid = (lo + hi) / 2;
+ if (ids[mid] < id) {
+ lo = mid + 1;
+ } else if (ids[mid] > id) {
+ hi = mid - 1;
+ } else {
+ return mid;
+ }
+ }
+ return -1;
+}
+
+static int cmp_int64(const void *a, const void *b) {
+ int64_t x = *(const int64_t *)a;
+ int64_t y = *(const int64_t *)b;
+ return (x > y) - (x < y);
+}
+
+/* Build the CSR call graph from parallel (src,tgt) edge arrays. Returns false
+ * on OOM/empty. */
+static bool scc_build(const int64_t *src, const int64_t *tgt, int ecount, scc_graph_t *g) {
+ memset(g, 0, sizeof(*g));
+ if (ecount <= 0) {
+ return false;
+ }
+ int64_t *all = malloc((size_t)ecount * 2 * sizeof(int64_t));
+ if (!all) {
+ return false;
+ }
+ for (int i = 0; i < ecount; i++) {
+ all[2 * i] = src[i];
+ all[2 * i + 1] = tgt[i];
+ }
+ qsort(all, (size_t)ecount * 2, sizeof(int64_t), cmp_int64);
+ int nv = 0;
+ for (int i = 0; i < ecount * 2; i++) {
+ if (i == 0 || all[i] != all[i - 1]) {
+ all[nv++] = all[i];
+ }
+ }
+ g->ids = all;
+ g->nverts = nv;
+ g->adj_head = calloc((size_t)nv + 1, sizeof(int));
+ g->adj = malloc((size_t)ecount * sizeof(int));
+ if (!g->adj_head || !g->adj) {
+ free(g->ids);
+ free(g->adj_head);
+ free(g->adj);
+ memset(g, 0, sizeof(*g));
+ return false;
+ }
+ /* two-pass CSR fill */
+ for (int i = 0; i < ecount; i++) {
+ int u = scc_id_index(g->ids, nv, src[i]);
+ g->adj_head[u + 1]++;
+ }
+ for (int i = 0; i < nv; i++) {
+ g->adj_head[i + 1] += g->adj_head[i];
+ }
+ int *cursor = malloc((size_t)nv * sizeof(int));
+ if (!cursor) {
+ free(g->ids);
+ free(g->adj_head);
+ free(g->adj);
+ memset(g, 0, sizeof(*g));
+ return false;
+ }
+ for (int i = 0; i < nv; i++) {
+ cursor[i] = g->adj_head[i];
+ }
+ for (int i = 0; i < ecount; i++) {
+ int u = scc_id_index(g->ids, nv, src[i]);
+ int v = scc_id_index(g->ids, nv, tgt[i]);
+ g->adj[cursor[u]++] = v;
+ }
+ free(cursor);
+ g->nedges = ecount;
+ return true;
+}
+
+static void scc_free(scc_graph_t *g) {
+ free(g->ids);
+ free(g->adj_head);
+ free(g->adj);
+ memset(g, 0, sizeof(*g));
+}
+
+/* Iterative Tarjan. Fills comp[v] with a component id; components are numbered
+ * in discovery order. Returns the component count, or -1 on OOM. */
+static int scc_tarjan(const scc_graph_t *g, int *comp) {
+ enum { SCC_UNVISITED = -1 };
+ int nv = g->nverts;
+ int *index = malloc((size_t)nv * sizeof(int));
+ int *low = malloc((size_t)nv * sizeof(int));
+ bool *on_stack = calloc((size_t)nv, sizeof(bool));
+ int *tstack = malloc((size_t)nv * sizeof(int)); /* Tarjan's node stack */
+ int *dfs_v = malloc((size_t)nv * sizeof(int)); /* explicit DFS: vertex */
+ int *dfs_i = malloc((size_t)nv * sizeof(int)); /* explicit DFS: adj cursor */
+ if (!index || !low || !on_stack || !tstack || !dfs_v || !dfs_i) {
+ free(index);
+ free(low);
+ free(on_stack);
+ free(tstack);
+ free(dfs_v);
+ free(dfs_i);
+ return -1;
+ }
+ for (int i = 0; i < nv; i++) {
+ index[i] = SCC_UNVISITED;
+ comp[i] = SCC_UNVISITED;
+ }
+ int counter = 0;
+ int tsp = 0; /* Tarjan stack pointer */
+ int ncomp = 0; /* component id allocator */
+ for (int s = 0; s < nv; s++) {
+ if (index[s] != SCC_UNVISITED) {
+ continue;
+ }
+ int dsp = 0; /* DFS stack pointer */
+ dfs_v[dsp] = s;
+ dfs_i[dsp] = g->adj_head[s];
+ index[s] = low[s] = counter++;
+ tstack[tsp++] = s;
+ on_stack[s] = true;
+ while (dsp >= 0) {
+ int v = dfs_v[dsp];
+ if (dfs_i[dsp] < g->adj_head[v + 1]) {
+ int w = g->adj[dfs_i[dsp]++];
+ if (index[w] == SCC_UNVISITED) {
+ index[w] = low[w] = counter++;
+ tstack[tsp++] = w;
+ on_stack[w] = true;
+ dsp++;
+ dfs_v[dsp] = w;
+ dfs_i[dsp] = g->adj_head[w];
+ } else if (on_stack[w] && index[w] < low[v]) {
+ low[v] = index[w];
+ }
+ } else {
+ /* v fully explored: it is a root iff low==index -> pop an SCC */
+ if (low[v] == index[v]) {
+ int w;
+ do {
+ w = tstack[--tsp];
+ on_stack[w] = false;
+ comp[w] = ncomp;
+ } while (w != v);
+ ncomp++;
+ }
+ dsp--;
+ if (dsp >= 0 && low[v] < low[dfs_v[dsp]]) {
+ low[dfs_v[dsp]] = low[v];
+ }
+ }
+ }
+ }
+ free(index);
+ free(low);
+ free(on_stack);
+ free(tstack);
+ free(dfs_v);
+ free(dfs_i);
+ return ncomp;
+}
static bool aspect_is_valid(const char *name) {
if (!name) {
@@ -3990,6 +4195,25 @@ static bool aspect_is_valid(const char *name) {
/* Check if an aspect is requested. NULL aspects = all. The array can contain
* "all" (everything), "overview" (everything except file_tree — see
* cbm_store_arch_aspect_in_overview in store.c), or the aspect name itself. */
+/* True ONLY when `name` is explicitly present in the aspects array — never via
+ * the no-filter default, "all", or "overview". For expensive opt-in aspects
+ * (cycles scans the whole call graph) that must not run on a bare call. */
+static bool aspect_explicitly_named(yyjson_val *aspects_arr, const char *name) {
+ if (!aspects_arr) {
+ return false;
+ }
+ yyjson_arr_iter iter;
+ yyjson_arr_iter_init(aspects_arr, &iter);
+ yyjson_val *val;
+ while ((val = yyjson_arr_iter_next(&iter)) != NULL) {
+ const char *s = yyjson_get_str(val);
+ if (s && strcmp(s, name) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
static bool aspect_wanted(yyjson_doc *aspects_doc, yyjson_val *aspects_arr, const char *name) {
if (!aspects_arr) {
return true; /* no filter = all */
@@ -4055,6 +4279,136 @@ static void arch_join_list(char *buf, size_t sz, const char **items, int n) {
}
}
+/* Compute the circular-dependency SCCs (size > 1) of the CALLS graph. Returns
+ * a malloc'd array of components, each a malloc'd int64 array of member node
+ * ids, with sizes in *out_sizes and count in *out_ncycles; sets *scanned_edges
+ * and *edges_truncated. Caller frees each component + the arrays. Returns
+ * CBM_STORE_OK, or CBM_STORE_ERR on failure (all outs zeroed). */
+enum { ARCH_SCC_MAX_EDGES = 400000, ARCH_SCC_MAX_CYCLES = 100, ARCH_SCC_MEMBERS_SHOWN = 20 };
+
+static int arch_compute_cycles(cbm_store_t *store, const char *project, int64_t ***out_members,
+ int **out_sizes, int *out_ncycles, int *scanned_edges,
+ bool *edges_truncated) {
+ *out_members = NULL;
+ *out_sizes = NULL;
+ *out_ncycles = 0;
+ *scanned_edges = 0;
+ *edges_truncated = false;
+
+ int64_t *src = NULL;
+ int64_t *tgt = NULL;
+ int ecount = 0;
+ if (cbm_store_fetch_call_edges(store, project, ARCH_SCC_MAX_EDGES, &src, &tgt, &ecount,
+ edges_truncated) != CBM_STORE_OK) {
+ return CBM_STORE_ERR;
+ }
+ *scanned_edges = ecount;
+ scc_graph_t g;
+ if (!scc_build(src, tgt, ecount, &g)) {
+ free(src);
+ free(tgt);
+ return CBM_STORE_OK; /* no edges = no cycles, not an error */
+ }
+ free(src);
+ free(tgt);
+
+ int *comp = malloc((size_t)g.nverts * sizeof(int));
+ if (!comp) {
+ scc_free(&g);
+ return CBM_STORE_ERR;
+ }
+ int ncomp = scc_tarjan(&g, comp);
+ if (ncomp < 0) {
+ free(comp);
+ scc_free(&g);
+ return CBM_STORE_ERR;
+ }
+ /* size per component */
+ int *csize = calloc((size_t)ncomp, sizeof(int));
+ if (!csize) {
+ free(comp);
+ scc_free(&g);
+ return CBM_STORE_ERR;
+ }
+ for (int v = 0; v < g.nverts; v++) {
+ csize[comp[v]]++;
+ }
+ /* collect components with size > 1 (the cycles) */
+ int ncyc = 0;
+ for (int c = 0; c < ncomp; c++) {
+ if (csize[c] > 1) {
+ ncyc++;
+ }
+ }
+ if (ncyc > ARCH_SCC_MAX_CYCLES) {
+ ncyc = ARCH_SCC_MAX_CYCLES;
+ }
+ int64_t **members = ncyc > 0 ? calloc((size_t)ncyc, sizeof(int64_t *)) : NULL;
+ int *sizes = ncyc > 0 ? calloc((size_t)ncyc, sizeof(int)) : NULL;
+ /* map component id -> output slot (only for the first ARCH_SCC_MAX_CYCLES
+ * size>1 comps, in component-id order) */
+ int *slot = malloc((size_t)ncomp * sizeof(int));
+ if ((ncyc > 0 && (!members || !sizes)) || !slot) {
+ free(members);
+ free(sizes);
+ free(slot);
+ free(csize);
+ free(comp);
+ scc_free(&g);
+ return CBM_STORE_ERR;
+ }
+ int next_slot = 0;
+ for (int c = 0; c < ncomp; c++) {
+ if (csize[c] > 1 && next_slot < ncyc) {
+ slot[c] = next_slot;
+ sizes[next_slot] = csize[c];
+ members[next_slot] = malloc((size_t)csize[c] * sizeof(int64_t));
+ next_slot++;
+ } else {
+ slot[c] = -1;
+ }
+ }
+ int *fill = calloc((size_t)ncyc, sizeof(int));
+ if (ncyc > 0 && !fill) {
+ for (int i = 0; i < ncyc; i++) {
+ free(members[i]);
+ }
+ free(members);
+ free(sizes);
+ free(slot);
+ free(csize);
+ free(comp);
+ scc_free(&g);
+ return CBM_STORE_ERR;
+ }
+ for (int v = 0; v < g.nverts; v++) {
+ int sl = slot[comp[v]];
+ if (sl >= 0) {
+ members[sl][fill[sl]++] = g.ids[v];
+ }
+ }
+ free(fill);
+ free(slot);
+ free(csize);
+ free(comp);
+ scc_free(&g);
+ *out_members = members;
+ *out_sizes = sizes;
+ *out_ncycles = ncyc;
+ return CBM_STORE_OK;
+}
+
+/* Fetch the qualified_name for a node id, or a "#<id>" fallback. */
+static void arch_node_qn(cbm_store_t *store, int64_t id, char *out, size_t outsz) {
+ cbm_node_t n = {0};
+ if (cbm_store_find_node_by_id(store, id, &n) == CBM_STORE_OK && n.qualified_name) {
+ snprintf(out, outsz, "%s", n.qualified_name);
+ } else {
+ snprintf(out, outsz, "#%lld", (long long)id);
+ }
+ free_node_contents(&n);
+}
+
static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
char *project = get_project_arg(args);
char *scope_path = cbm_mcp_get_string_arg(args, "path");
@@ -4354,6 +4708,49 @@ static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
}
}
+ /* cycles: circular CALLS dependencies (SCCs of size > 1) — opt-in, it
+ * scans the whole call graph. A quotient/condensation view. */
+ if (aspect_explicitly_named(aspects_arr, "cycles")) {
+ int64_t **members = NULL;
+ int *sizes = NULL;
+ int ncyc = 0;
+ int scanned = 0;
+ bool etrunc = false;
+ if (arch_compute_cycles(store, project, &members, &sizes, &ncyc, &scanned, &etrunc) ==
+ CBM_STORE_OK) {
+ cbm_tree_scalar_int(&sb, "call_edges_scanned", scanned);
+ if (etrunc) {
+ cbm_tree_scalar_bool(&sb, "cycles_partial", true);
+ cbm_tree_scalar_str(&sb, "cycles_hint",
+ "call graph exceeded the scan budget; cycle list may be "
+ "incomplete");
+ }
+ char hdr[CBM_SZ_128];
+ snprintf(hdr, sizeof(hdr),
+ "cycles: %d (rows: size members; circular CALLS dependencies)\n", ncyc);
+ cbm_sb_append(&sb, hdr);
+ for (int c = 0; c < ncyc; c++) {
+ char row[CBM_SZ_2K];
+ int off = snprintf(row, sizeof(row), " %d ", sizes[c]);
+ bool clipped = sizes[c] > ARCH_SCC_MEMBERS_SHOWN;
+ int show = clipped ? ARCH_SCC_MEMBERS_SHOWN : sizes[c];
+ for (int m = 0; m < show && off < (int)sizeof(row) - 2; m++) {
+ char qn[CBM_SZ_512];
+ arch_node_qn(store, members[c][m], qn, sizeof(qn));
+ off += snprintf(row + off, sizeof(row) - off, "%s%s", m ? ";" : "", qn);
+ }
+ if (clipped && off < (int)sizeof(row) - 8) {
+ snprintf(row + off, sizeof(row) - off, ";+%d", sizes[c] - show);
+ }
+ cbm_sb_append(&sb, row);
+ cbm_sb_append(&sb, "\n");
+ free(members[c]);
+ }
+ free(members);
+ free(sizes);
+ }
+ }
+
cbm_store_architecture_free(&arch);
cbm_store_schema_free(&schema);
if (aspects_doc) {
@@ -4596,6 +4993,38 @@ static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
append_cross_repo_summary(doc, root, &schema);
+ /* cycles: SCCs of size > 1 in the CALLS graph (same model as tree). */
+ if (aspect_explicitly_named(aspects_arr, "cycles")) {
+ int64_t **members = NULL;
+ int *sizes = NULL;
+ int ncyc = 0;
+ int scanned = 0;
+ bool etrunc = false;
+ if (arch_compute_cycles(store, project, &members, &sizes, &ncyc, &scanned, &etrunc) ==
+ CBM_STORE_OK) {
+ yyjson_mut_obj_add_int(doc, root, "call_edges_scanned", scanned);
+ yyjson_mut_obj_add_bool(doc, root, "cycles_partial", etrunc);
+ yyjson_mut_val *cyc = yyjson_mut_arr(doc);
+ for (int c = 0; c < ncyc; c++) {
+ yyjson_mut_val *o = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_int(doc, o, "size", sizes[c]);
+ yyjson_mut_val *mem = yyjson_mut_arr(doc);
+ int show = sizes[c] < ARCH_SCC_MEMBERS_SHOWN ? sizes[c] : ARCH_SCC_MEMBERS_SHOWN;
+ for (int m = 0; m < show; m++) {
+ char qn[CBM_SZ_512];
+ arch_node_qn(store, members[c][m], qn, sizeof(qn));
+ yyjson_mut_arr_add_strcpy(doc, mem, qn);
+ }
+ yyjson_mut_obj_add_val(doc, o, "members", mem);
+ yyjson_mut_arr_add_val(cyc, o);
+ free(members[c]);
+ }
+ yyjson_mut_obj_add_val(doc, root, "cycles", cyc);
+ free(members);
+ free(sizes);
+ }
+ }
+
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
cbm_store_architecture_free(&arch);
@@ -6798,6 +7227,16 @@ static char *build_snippet_response(cbm_mcp_server_t *srv, cbm_node_t *node,
int start = node->start_line > 0 ? node->start_line : SKIP_ONE;
int end = node->end_line > start ? node->end_line : start + SNIPPET_DEFAULT_LINES;
+ /* Context-bomb guard: a structural node (Module/File) spans its whole file,
+ * so an unclipped read returned the ENTIRE source — a field-eval agent that
+ * fell back to a Module snippet pulled 400KB in one call. Cap the line span
+ * (far above any real function) and flag it; the exact range is still in
+ * start_line/end_line for a targeted re-read. */
+ bool snippet_clipped = false;
+ if (end - start + 1 > MCP_SNIPPET_MAX_LINES) {
+ end = start + MCP_SNIPPET_MAX_LINES - 1;
+ snippet_clipped = true;
+ }
char *abs_path = NULL;
char *source = resolve_snippet_source(root_path, node->file_path, start, end, &abs_path);
@@ -6819,6 +7258,10 @@ static char *build_snippet_response(cbm_mcp_server_t *srv, cbm_node_t *node,
yyjson_mut_obj_add_str(doc, root_obj, "file_path", display_path);
yyjson_mut_obj_add_int(doc, root_obj, "start_line", start);
yyjson_mut_obj_add_int(doc, root_obj, "end_line", end);
+ if (snippet_clipped) {
+ yyjson_mut_obj_add_bool(doc, root_obj, "source_clipped", true);
+ yyjson_mut_obj_add_int(doc, root_obj, "clipped_at_lines", MCP_SNIPPET_MAX_LINES);
+ }
if (source) {
char *safe_source = sanitize_utf8_lossy(source);
@@ -8062,25 +8505,101 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
/* ── detect_changes ───────────────────────────────────────────── */
-/* Find symbols defined in a file and add them to the impacted array. */
-static void detect_add_impacted_symbols(cbm_store_t *store, const char *project, const char *file,
- yyjson_mut_doc *doc, yyjson_mut_val *impacted) {
+/* Collect BFS seed ids: every symbol DEFINED in a changed file (everything but
+ * the structural container labels — those have no CALLS edges). These anchor
+ * the multi-source impact traversal. */
+static void detect_collect_seeds(cbm_store_t *store, const char *project, const char *file,
+ int64_t **seeds, int *n, int *cap) {
cbm_node_t *nodes = NULL;
int ncount = 0;
cbm_store_find_nodes_by_file(store, project, file, &nodes, &ncount);
for (int i = 0; i < ncount; i++) {
- if (nodes[i].label && strcmp(nodes[i].label, "File") != 0 &&
- strcmp(nodes[i].label, "Folder") != 0 && strcmp(nodes[i].label, "Project") != 0) {
- yyjson_mut_val *item = yyjson_mut_obj(doc);
- yyjson_mut_obj_add_strcpy(doc, item, "name", nodes[i].name ? nodes[i].name : "");
- yyjson_mut_obj_add_strcpy(doc, item, "label", nodes[i].label);
- yyjson_mut_obj_add_strcpy(doc, item, "file", file);
- yyjson_mut_arr_add_val(impacted, item);
+ const char *lb = nodes[i].label;
+ if (lb && strcmp(lb, "File") != 0 && strcmp(lb, "Folder") != 0 &&
+ strcmp(lb, "Project") != 0 && strcmp(lb, "Module") != 0 && strcmp(lb, "Package") != 0 &&
+ strcmp(lb, "Section") != 0) {
+ if (*n >= *cap) {
+ *cap = *cap ? *cap * 2 : 16;
+ *seeds = safe_realloc(*seeds, (size_t)*cap * sizeof(int64_t));
+ }
+ (*seeds)[(*n)++] = nodes[i].id;
}
}
cbm_store_free_nodes(nodes, ncount);
}
+/* Module key for the impacted rollup = the first TWO path segments
+ * ("src/mcp/mcp.c" -> "src/mcp"), a quotient of the blast radius coarse enough
+ * to fit yet specific enough to localize (one segment collapses a whole tree
+ * to "src"). Falls back to one segment, then the whole path. */
+static void detect_module_of(const char *file, char *out, size_t outsz) {
+ if (!file || !file[0]) {
+ snprintf(out, outsz, "(root)");
+ return;
+ }
+ const char *s1 = strchr(file, '/');
+ if (!s1) {
+ snprintf(out, outsz, "%s", file);
+ return;
+ }
+ const char *s2 = strchr(s1 + 1, '/');
+ size_t len = s2 ? (size_t)(s2 - file) : strlen(file);
+ if (len >= outsz) {
+ len = outsz - 1;
+ }
+ memcpy(out, file, len);
+ out[len] = '\0';
+}
+
+/* Emit the impacted set as a grouped tree leg: rows grouped under their shared
+ * (qn-prefix, file), `name label hop` per row. At most `limit` rows are listed
+ * (the visited array is hop-ordered, so the closest — highest-signal — impact
+ * shows first); impacted_total always carries the exact full count, and
+ * `impacted_shown < impacted_total` is the honest truncation signal. The
+ * module rollup (emitted by the caller) stays complete regardless. */
+static void detect_emit_impacted_tree(cbm_sb_t *sb, cbm_traverse_result_t *tr, int limit) {
+ cbm_tree_scalar_int(sb, "impacted_total", tr->visited_count);
+ int shown = tr->visited_count < limit ? tr->visited_count : limit;
+ /* qn order for stable grouping, but keep hop-closeness: sort by (hop) is
+ * lost under qn sort, so group AFTER selecting the nearest `shown` rows —
+ * the visited array is already (hop,id)-ordered from the BFS. */
+ char hdr[CBM_SZ_128];
+ snprintf(hdr, sizeof(hdr),
+ "impacted_shown: %d\nimpacted: %d (rows: name label hop; qn = group prefix + \".\" "
+ "+ name; nearest hops first)\n",
+ shown, shown);
+ cbm_sb_append(sb, hdr);
+ if (shown > 1) {
+ qsort(tr->visited, (size_t)shown, sizeof(cbm_node_hop_t), tree_hop_cmp_qn);
+ }
+ char cur_group[CBM_SZ_1K] = "";
+ for (int i = 0; i < shown; i++) {
+ const char *qn =
+ tr->visited[i].node.qualified_name ? tr->visited[i].node.qualified_name : "";
+ const char *file = tr->visited[i].node.file_path ? tr->visited[i].node.file_path : "";
+ size_t plen = sg_qn_prefix_len(qn);
+ char group[CBM_SZ_1K];
+ snprintf(group, sizeof(group), "%.*s (%s)", (int)plen, qn, file);
+ if (strcmp(group, cur_group) != 0) {
+ snprintf(cur_group, sizeof(cur_group), "%s", group);
+ cbm_sb_append(sb, group);
+ cbm_sb_append(sb, ":\n");
+ }
+ char row[CBM_SZ_512];
+ snprintf(row, sizeof(row), " %s %s %d\n", plen ? qn + plen + 1 : qn,
+ tr->visited[i].node.label ? tr->visited[i].node.label : "", tr->visited[i].hop);
+ cbm_sb_append(sb, row);
+ }
+ if (shown < tr->visited_count) {
+ char more[CBM_SZ_256];
+ snprintf(more, sizeof(more),
+ "impacted_omitted: %d (see impacted_modules for the full rollup; raise 'limit' "
+ "or lower 'depth' to see specifics)\n",
+ tr->visited_count - shown);
+ cbm_sb_append(sb, more);
+ }
+}
+
static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
char *project = get_project_arg(args);
char *base_branch = cbm_mcp_get_string_arg(args, "base_branch");
@@ -8178,19 +8697,45 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
return cbm_mcp_text_result(errmsg, true);
}
- yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
- yyjson_mut_val *root_obj = yyjson_mut_obj(doc);
- yyjson_mut_doc_set_root(doc, root_obj);
-
- yyjson_mut_val *changed = yyjson_mut_arr(doc);
- yyjson_mut_val *impacted = yyjson_mut_arr(doc);
-
/* resolve_store already called via get_project_root above */
cbm_store_t *store = srv->store;
- char line[CBM_SZ_1K];
+ /* Direction of impact. Default inbound = the BLAST RADIUS: the transitive
+ * CALLERS of the changed symbols, which may need review. outbound = what
+ * the changed code depends on; both = union. */
+ char *direction = cbm_mcp_get_string_arg(args, "direction");
+ if (!direction) {
+ direction = heap_strdup("inbound");
+ }
+ if (strcmp(direction, "inbound") != 0 && strcmp(direction, "outbound") != 0 &&
+ strcmp(direction, "both") != 0) {
+ free(direction);
+ direction = heap_strdup("inbound");
+ }
+ char *fmt = cbm_mcp_get_string_arg(args, "format");
+ bool legacy_json = fmt && strcmp(fmt, "json") == 0;
+ free(fmt);
+
+ /* Per-symbol impacted-row display cap (the module rollup stays complete).
+ * impacted_total always reports the true count, so this never hides scale. */
+ int imp_limit = cbm_mcp_get_int_arg(args, "limit", MCP_DEFAULT_IMPACT_LIMIT);
+ if (imp_limit < 1) {
+ imp_limit = 1;
+ }
+ if (imp_limit > MCP_BFS_LIMIT_MAX) {
+ imp_limit = MCP_BFS_LIMIT_MAX;
+ }
+
+ /* Collect changed file paths into a C array (drives seeds, the rollup, and
+ * both output encodings). */
+ char **files = NULL;
int file_count = 0;
+ int file_cap = 0;
+ int64_t *seeds = NULL;
+ int seed_count = 0;
+ int seed_cap = 0;
+ char line[CBM_SZ_1K];
while (fgets(line, sizeof(line), fp)) {
size_t len = strlen(line);
while (len > 0 && (line[len - SKIP_ONE] == '\n' || line[len - SKIP_ONE] == '\r')) {
@@ -8199,59 +8744,213 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
if (len == 0) {
continue;
}
-
- /* `git status --porcelain` prefixes each path with a two-character
- * status code and a space ("?? path", "A path", " M path"). The two
- * `git diff --name-only` sources emit bare paths. Strip the porcelain
- * prefix when present so all three sources yield clean paths; for a
- * rename ("R old -> new") keep the post-arrow destination path. */
+ /* Strip the `git status --porcelain` 2-char code + space; for a rename
+ * ("R old -> new") keep the destination path. */
char *path_line = line;
if (len > PAIR_LEN && line[PAIR_LEN] == ' ' && strchr(" MADRCU?!", line[0]) &&
strchr(" MADRCU?!", line[1])) {
path_line = line + PAIR_LEN + SKIP_ONE;
char *arrow = strstr(path_line, " -> ");
if (arrow) {
- enum { ARROW_LEN = 4 }; /* length of " -> " */
+ enum { ARROW_LEN = 4 };
path_line = arrow + ARROW_LEN;
}
}
if (path_line[0] == '\0') {
continue;
}
-
- yyjson_mut_arr_add_strcpy(doc, changed, path_line);
- file_count++;
-
+ /* Dedup: the three git sources are sorted+unioned on POSIX but not on
+ * Windows (separate commands), and a path can repeat. */
+ bool dup = false;
+ for (int i = 0; i < file_count; i++) {
+ if (strcmp(files[i], path_line) == 0) {
+ dup = true;
+ break;
+ }
+ }
+ if (dup) {
+ continue;
+ }
+ if (file_count >= file_cap) {
+ file_cap = file_cap ? file_cap * 2 : 16;
+ files = safe_realloc(files, (size_t)file_cap * sizeof(char *));
+ }
+ files[file_count++] = heap_strdup(path_line);
if (want_symbols) {
- detect_add_impacted_symbols(store, project, path_line, doc, impacted);
+ detect_collect_seeds(store, project, path_line, &seeds, &seed_count, &seed_cap);
}
}
int git_status = cbm_pclose(fp);
- bool is_error = false;
- if (git_status != 0 && file_count == 0) {
- char hint_buf[CBM_SZ_256];
- snprintf(hint_buf, sizeof(hint_buf),
- "git diff exited with status %d. Check that branch '%s' exists.", git_status,
+ /* merge-base SHA: the exact commit the diff is measured against, so the
+ * result is reproducible even as base_branch advances. Best-effort. */
+ char merge_base[64] = "";
+ {
+ char mbcmd[CBM_SZ_2K];
+#ifdef _WIN32
+ snprintf(mbcmd, sizeof(mbcmd), "git -C \"%s\" merge-base \"%s\" HEAD 2>NUL", root_path,
base_branch);
- yyjson_mut_obj_add_strcpy(doc, root_obj, "hint", hint_buf);
- is_error = true;
+#else
+ snprintf(mbcmd, sizeof(mbcmd), "git -C '%s' merge-base '%s' HEAD 2>/dev/null", root_path,
+ base_branch);
+#endif
+ FILE *mbfp = cbm_popen(mbcmd, "r");
+ if (mbfp) {
+ if (fgets(merge_base, sizeof(merge_base), mbfp)) {
+ size_t l = strlen(merge_base);
+ while (l > 0 && (merge_base[l - 1] == '\n' || merge_base[l - 1] == '\r')) {
+ merge_base[--l] = '\0';
+ }
+ }
+ (void)cbm_pclose(mbfp);
+ }
}
- yyjson_mut_obj_add_val(doc, root_obj, "changed_files", changed);
- yyjson_mut_obj_add_int(doc, root_obj, "changed_count", file_count);
- yyjson_mut_obj_add_val(doc, root_obj, "impacted_symbols", impacted);
- yyjson_mut_obj_add_int(doc, root_obj, "depth", depth);
+ /* The impact traversal: ONE multi-source BFS over all seeds. */
+ cbm_traverse_result_t impact = {0};
+ bool truncated = false;
+ if (want_symbols && seed_count > 0) {
+ (void)cbm_store_bfs_multi(store, seeds, seed_count, direction, NULL, 0, depth,
+ MCP_BFS_LIMIT_MAX, &impact, &truncated);
+ }
- char *json = yy_doc_to_str(doc);
- yyjson_mut_doc_free(doc);
+ bool is_error = (git_status != 0 && file_count == 0);
+ char *out_str = NULL;
+
+ if (!legacy_json) {
+ cbm_sb_t sb;
+ cbm_sb_init(&sb);
+ cbm_tree_scalar_str(&sb, "base", base_branch);
+ if (merge_base[0]) {
+ cbm_tree_scalar_str(&sb, "merge_base", merge_base);
+ }
+ cbm_tree_scalar_str(&sb, "direction", direction);
+ if (is_error) {
+ char hint_buf[CBM_SZ_256];
+ snprintf(hint_buf, sizeof(hint_buf),
+ "git diff exited with status %d. Check that branch '%s' exists.", git_status,
+ base_branch);
+ cbm_tree_scalar_str(&sb, "hint", hint_buf);
+ }
+ /* changed files (the git result) */
+ char cf[CBM_SZ_64];
+ snprintf(cf, sizeof(cf), "changed_files: %d\n", file_count);
+ cbm_sb_append(&sb, cf);
+ for (int i = 0; i < file_count; i++) {
+ cbm_sb_append(&sb, " ");
+ cbm_sb_append(&sb, files[i]);
+ cbm_sb_append(&sb, "\n");
+ }
+ cbm_tree_scalar_int(&sb, "seed_symbols", seed_count);
+ if (want_symbols) {
+ detect_emit_impacted_tree(&sb, &impact, imp_limit);
+ /* module rollup: a quotient view of the blast radius */
+ if (impact.visited_count > 0) {
+ cbm_sb_append(&sb, "impacted_modules: (rows: module count)\n");
+ enum { MODCAP = 256 };
+ char mods[MODCAP][CBM_SZ_128];
+ int mcnt[MODCAP];
+ int nmods = 0;
+ int overflow = 0; /* symbols whose module didn't fit MODCAP */
+ for (int i = 0; i < impact.visited_count; i++) {
+ char m[CBM_SZ_128];
+ detect_module_of(impact.visited[i].node.file_path, m, sizeof(m));
+ int j = 0;
+ for (; j < nmods; j++) {
+ if (strcmp(mods[j], m) == 0) {
+ mcnt[j]++;
+ break;
+ }
+ }
+ if (j == nmods) {
+ if (nmods < MODCAP) {
+ snprintf(mods[nmods], sizeof(mods[nmods]), "%s", m);
+ mcnt[nmods] = 1;
+ nmods++;
+ } else {
+ overflow++; /* never silently dropped — surfaced below */
+ }
+ }
+ }
+ for (int j = 0; j < nmods; j++) {
+ char mrow[CBM_SZ_256];
+ snprintf(mrow, sizeof(mrow), " %s %d\n", mods[j], mcnt[j]);
+ cbm_sb_append(&sb, mrow);
+ }
+ if (overflow > 0) {
+ char orow[CBM_SZ_128];
+ snprintf(orow, sizeof(orow), " (other) %d\n", overflow);
+ cbm_sb_append(&sb, orow);
+ }
+ }
+ if (truncated) {
+ cbm_tree_scalar_bool(&sb, "truncated", true);
+ cbm_tree_scalar_str(&sb, "hint",
+ "impact hit the safety ceiling — narrow with a lower "
+ "'depth' or a smaller diff");
+ }
+ }
+ out_str = cbm_sb_finish(&sb);
+ } else {
+ /* format:"json" = json-stringified tree: same model, structured. */
+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
+ yyjson_mut_val *root_obj = yyjson_mut_obj(doc);
+ yyjson_mut_doc_set_root(doc, root_obj);
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "base", base_branch);
+ if (merge_base[0]) {
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "merge_base", merge_base);
+ }
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "direction", direction);
+ yyjson_mut_val *cf = yyjson_mut_arr(doc);
+ for (int i = 0; i < file_count; i++) {
+ yyjson_mut_arr_add_strcpy(doc, cf, files[i]);
+ }
+ yyjson_mut_obj_add_val(doc, root_obj, "changed_files", cf);
+ yyjson_mut_obj_add_int(doc, root_obj, "seed_symbols", seed_count);
+ yyjson_mut_obj_add_int(doc, root_obj, "impacted_total", impact.visited_count);
+ int imp_shown = impact.visited_count < imp_limit ? impact.visited_count : imp_limit;
+ yyjson_mut_obj_add_int(doc, root_obj, "impacted_shown", imp_shown);
+ yyjson_mut_val *imp = yyjson_mut_arr(doc);
+ for (int i = 0; i < imp_shown; i++) {
+ yyjson_mut_val *o = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_strcpy(
+ doc, o, "qn",
+ impact.visited[i].node.qualified_name ? impact.visited[i].node.qualified_name : "");
+ yyjson_mut_obj_add_strcpy(
+ doc, o, "label", impact.visited[i].node.label ? impact.visited[i].node.label : "");
+ yyjson_mut_obj_add_strcpy(
+ doc, o, "file",
+ impact.visited[i].node.file_path ? impact.visited[i].node.file_path : "");
+ yyjson_mut_obj_add_int(doc, o, "hop", impact.visited[i].hop);
+ yyjson_mut_arr_add_val(imp, o);
+ }
+ yyjson_mut_obj_add_val(doc, root_obj, "impacted", imp);
+ yyjson_mut_obj_add_bool(doc, root_obj, "truncated", truncated);
+ if (is_error) {
+ char hint_buf[CBM_SZ_256];
+ snprintf(hint_buf, sizeof(hint_buf),
+ "git diff exited with status %d. Check that branch '%s' exists.", git_status,
+ base_branch);
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "hint", hint_buf);
+ }
+ out_str = yy_doc_to_str(doc);
+ yyjson_mut_doc_free(doc);
+ }
+
+ cbm_store_traverse_free(&impact);
+ for (int i = 0; i < file_count; i++) {
+ free(files[i]);
+ }
+ free(files);
+ free(seeds);
+ free(direction);
free(root_path);
free(project);
free(base_branch);
free(scope);
- char *result = cbm_mcp_text_result(json, is_error);
- free(json);
+ char *result = cbm_mcp_text_result(out_str, is_error);
+ free(out_str);
return result;
}
diff --git a/src/store/store.c b/src/store/store.c
index a9e86cb34..7b966051a 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -1846,6 +1846,76 @@ static void bind_proj_and_type(sqlite3_stmt *stmt, const void *data) {
bind_text(stmt, ST_COL_2, b->type);
}
+int cbm_store_fetch_call_edges(cbm_store_t *s, const char *project, int max_edges,
+ int64_t **out_src, int64_t **out_tgt, int *count, bool *truncated) {
+ if (out_src) {
+ *out_src = NULL;
+ }
+ if (out_tgt) {
+ *out_tgt = NULL;
+ }
+ if (count) {
+ *count = 0;
+ }
+ if (truncated) {
+ *truncated = false;
+ }
+ if (!s || !s->db || !project || !out_src || !out_tgt || !count) {
+ return CBM_STORE_ERR;
+ }
+ /* CALLS edges whose BOTH endpoints are callable defs — the call graph the
+ * SCC pass condenses. Ordered for determinism. LIMIT max_edges + 1 so a
+ * full result is distinguishable from a truncated one. */
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(s->db,
+ "SELECT e.source_id, e.target_id FROM edges e "
+ "JOIN nodes ns ON ns.id = e.source_id "
+ "JOIN nodes nt ON nt.id = e.target_id "
+ "WHERE e.project = ?1 AND e.type = 'CALLS' "
+ " AND ns.label IN ('Function','Method') "
+ " AND nt.label IN ('Function','Method') "
+ "ORDER BY e.source_id, e.target_id LIMIT ?2;",
+ CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ store_set_error_sqlite(s, "fetch_call_edges prepare");
+ return CBM_STORE_ERR;
+ }
+ bind_text(stmt, SKIP_ONE, project);
+ sqlite3_bind_int(stmt, 2, max_edges > 0 ? max_edges + SKIP_ONE : CBM_NOT_FOUND);
+
+ int cap = ST_INIT_CAP_16;
+ int n = 0;
+ int64_t *src = malloc((size_t)cap * sizeof(int64_t));
+ int64_t *tgt = malloc((size_t)cap * sizeof(int64_t));
+ int scan_rc;
+ while ((scan_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
+ if (max_edges > 0 && n >= max_edges) {
+ if (truncated) {
+ *truncated = true; /* the sentinel row proves there are more */
+ }
+ break;
+ }
+ if (n >= cap) {
+ cap *= ST_GROWTH;
+ src = safe_realloc(src, (size_t)cap * sizeof(int64_t));
+ tgt = safe_realloc(tgt, (size_t)cap * sizeof(int64_t));
+ }
+ src[n] = sqlite3_column_int64(stmt, 0);
+ tgt[n] = sqlite3_column_int64(stmt, 1);
+ n++;
+ }
+ sqlite3_finalize(stmt);
+ if (scan_rc != SQLITE_DONE && scan_rc != SQLITE_ROW) {
+ free(src);
+ free(tgt);
+ store_set_error_sqlite(s, "fetch_call_edges scan");
+ return CBM_STORE_ERR;
+ }
+ *out_src = src;
+ *out_tgt = tgt;
+ *count = n;
+ return CBM_STORE_OK;
+}
+
int cbm_store_find_edges_by_source(cbm_store_t *s, int64_t source_id, cbm_edge_t **out,
int *count) {
bind_id_t b = {source_id};
@@ -3833,6 +3903,132 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
return CBM_STORE_OK;
}
+/* Multi-source BFS: one recursive CTE anchored on ALL seeds (via a temp
+ * table — never a per-seed loop, which re-walks overlapping hub subgraphs
+ * seed_count times). Semantics for impact analysis:
+ * - shortest-path MIN(hop) across the whole seed set;
+ * - SEEDS ARE EXCLUDED from the result: a seed reached from another seed
+ * (changed files call each other constantly) is not "impact";
+ * - uncapped counting up to max_results as a memory-safety ceiling only —
+ * *truncated reports when it was hit (never a silent cap);
+ * - canonical (hop, id) order.
+ * No root node and no edge collection — impact wants the reached set. */
+int cbm_store_bfs_multi(cbm_store_t *s, const int64_t *seed_ids, int seed_count,
+ const char *direction, const char **edge_types, int edge_type_count,
+ int max_depth, int max_results, cbm_traverse_result_t *out,
+ bool *truncated) {
+ memset(out, 0, sizeof(*out));
+ if (truncated) {
+ *truncated = false;
+ }
+ if (!s || !s->db || !seed_ids || seed_count <= 0) {
+ return CBM_STORE_ERR;
+ }
+
+ if (sqlite3_exec(s->db,
+ "CREATE TEMP TABLE IF NOT EXISTS bfs_seeds (id INTEGER PRIMARY KEY);"
+ "DELETE FROM bfs_seeds;",
+ NULL, NULL, NULL) != SQLITE_OK) {
+ store_set_error_sqlite(s, "bfs_multi seeds");
+ return CBM_STORE_ERR;
+ }
+ sqlite3_stmt *ins = NULL;
+ if (sqlite3_prepare_v2(s->db, "INSERT OR IGNORE INTO bfs_seeds(id) VALUES (?1)", CBM_NOT_FOUND,
+ &ins, NULL) != SQLITE_OK) {
+ store_set_error_sqlite(s, "bfs_multi seed insert");
+ return CBM_STORE_ERR;
+ }
+ for (int i = 0; i < seed_count; i++) {
+ sqlite3_reset(ins);
+ sqlite3_bind_int64(ins, SKIP_ONE, seed_ids[i]);
+ (void)sqlite3_step(ins);
+ }
+ sqlite3_finalize(ins);
+
+ char types_clause[CBM_SZ_512];
+ bfs_build_types_clause(edge_type_count, types_clause, (int)sizeof(types_clause));
+
+ const char *join_cond;
+ const char *next_id;
+ bool is_inbound = (direction != NULL) && (strcmp(direction, "inbound") == 0);
+ if (is_inbound) {
+ join_cond = "e.target_id = bfs.node_id";
+ next_id = "e.source_id";
+ } else {
+ join_cond = "e.source_id = bfs.node_id";
+ next_id = "e.target_id";
+ }
+
+ char sql[CBM_SZ_4K];
+ snprintf(sql, sizeof(sql),
+ "WITH RECURSIVE bfs(node_id, hop) AS ("
+ " SELECT id, 0 FROM bfs_seeds"
+ " UNION"
+ " SELECT %s, bfs.hop + 1"
+ " FROM bfs"
+ " JOIN edges e ON %s"
+ " WHERE e.type IN (%s) AND bfs.hop < %d"
+ ")"
+ "SELECT n.id, n.project, n.label, n.name, n.qualified_name, "
+ "n.file_path, n.start_line, n.end_line, n.properties, MIN(bfs.hop) AS hop "
+ "FROM bfs "
+ "JOIN nodes n ON n.id = bfs.node_id "
+ /* hop > 0 excludes the anchors themselves; the NOT IN excludes a
+ * seed REACHED from another seed at hop > 0 (leak-back). */
+ "WHERE bfs.hop > 0 AND bfs.node_id NOT IN (SELECT id FROM bfs_seeds) "
+ "GROUP BY n.id "
+ "ORDER BY hop, n.id "
+ "LIMIT %d;",
+ next_id, join_cond, types_clause, max_depth, max_results + SKIP_ONE);
+
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ store_set_error_sqlite(s, "bfs_multi prepare");
+ return CBM_STORE_ERR;
+ }
+ if (edge_type_count > 0) {
+ for (int i = 0; i < edge_type_count; i++) {
+ bind_text(stmt, i + SKIP_ONE, edge_types[i]);
+ }
+ } else {
+ bind_text(stmt, SKIP_ONE, "CALLS");
+ }
+
+ int cap = ST_INIT_CAP_16;
+ int n = 0;
+ cbm_node_hop_t *visited = malloc(cap * sizeof(cbm_node_hop_t));
+ int scan_rc16;
+ while ((scan_rc16 = sqlite3_step(stmt)) == SQLITE_ROW) {
+ if (n > max_results) {
+ break; /* ceiling + 1 row fetched: report truncation below */
+ }
+ if (n >= cap) {
+ cap *= ST_GROWTH;
+ visited = safe_realloc(visited, cap * sizeof(cbm_node_hop_t));
+ }
+ scan_node(stmt, &visited[n].node);
+ visited[n].hop = sqlite3_column_int(stmt, ST_COL_9);
+ n++;
+ }
+ bool aborted = (scan_rc16 != SQLITE_DONE && scan_rc16 != SQLITE_ROW);
+ if (n > max_results) {
+ /* Free the sentinel row and flag the ceiling. */
+ n = max_results;
+ cbm_node_free_fields(&visited[n].node);
+ if (truncated) {
+ *truncated = true;
+ }
+ }
+ sqlite3_finalize(stmt);
+ out->visited = visited;
+ out->visited_count = n;
+ if (aborted) {
+ store_set_error_sqlite(s, "bfs_multi row scan aborted");
+ return CBM_STORE_ERR;
+ }
+ return CBM_STORE_OK;
+}
+
void cbm_store_traverse_free(cbm_traverse_result_t *out) {
if (!out) {
return;
diff --git a/src/store/store.h b/src/store/store.h
index d088139ea..373440474 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -368,6 +368,13 @@ int64_t cbm_store_insert_edge(cbm_store_t *s, const cbm_edge_t *e);
/* Insert edges in batch. */
int cbm_store_insert_edge_batch(cbm_store_t *s, const cbm_edge_t *edges, int count);
+/* Fetch all CALLS edges among Function/Method nodes for a project as parallel
+ * (source_id, target_id) arrays (caller frees both). For SCC / cycle analysis.
+ * Stops at max_edges and sets *truncated — never a silent cap. Returns
+ * CBM_STORE_OK (or _ERR); *count is the number returned. */
+int cbm_store_fetch_call_edges(cbm_store_t *s, const char *project, int max_edges,
+ int64_t **out_src, int64_t **out_tgt, int *count, bool *truncated);
+
/* Find edges by source node. */
int cbm_store_find_edges_by_source(cbm_store_t *s, int64_t source_id, cbm_edge_t **out, int *count);
@@ -500,6 +507,15 @@ void cbm_store_search_free(cbm_search_output_t *out);
int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const char **edge_types,
int edge_type_count, int max_depth, int max_results, cbm_traverse_result_t *out);
+/* Multi-source BFS from ALL seed ids at once (one CTE, temp-table anchored).
+ * Seeds are EXCLUDED from the result (impact semantics); MIN(hop) across the
+ * seed set; canonical (hop,id) order; *truncated set when the max_results
+ * memory-safety ceiling was hit (counting is otherwise uncapped). */
+int cbm_store_bfs_multi(cbm_store_t *s, const int64_t *seed_ids, int seed_count,
+ const char *direction, const char **edge_types, int edge_type_count,
+ int max_depth, int max_results, cbm_traverse_result_t *out,
+ bool *truncated);
+
/* Free a traverse result's allocated memory. */
void cbm_store_traverse_free(cbm_traverse_result_t *out);
diff --git a/tests/test_incremental.c b/tests/test_incremental.c
index 00c2ea53a..72250707b 100644
--- a/tests/test_incremental.c
+++ b/tests/test_incremental.c
@@ -1860,11 +1860,35 @@ TEST(tool_detect_changes_default) {
double ms;
char *r = call_tool_timed("detect_changes", &ms, "{\"project\":\"%s\"}", g_project);
TOOL_OK(r, ms);
- /* Must have changed_files array and changed_count */
- ASSERT(resp_has_key(r, "changed_files"));
- ASSERT(resp_has_key(r, "changed_count"));
- ASSERT(resp_has_key(r, "impacted_symbols"));
- ASSERT(resp_has_key(r, "depth"));
+ /* New tree contract: base + direction scalars, a changed_files section,
+ * and the seed/impact accounting. (The old changed_count/impacted_symbols/
+ * depth keys are gone — impact is now a real traversal, not bare names.) */
+ ASSERT(strstr(r, "changed_files:") != NULL);
+ ASSERT(strstr(r, "direction:") != NULL);
+ ASSERT(strstr(r, "seed_symbols:") != NULL);
+ free(r);
+ PASS();
+}
+
+/* The blast radius is a REAL traversal now: default inbound gives transitive
+ * callers of the changed symbols with an exact impacted_total and a module
+ * rollup. Fixture diff may be empty (shallow clone at HEAD) — the sections and
+ * their accounting must still be present and internally consistent. */
+TEST(tool_detect_changes_impact_shape) {
+ double ms;
+ char *r = call_tool_timed("detect_changes", &ms,
+ "{\"project\":\"%s\",\"base_branch\":\"HEAD\"}", g_project);
+ TOOL_OK(r, ms);
+ ASSERT(strstr(r, "direction: inbound") != NULL); /* default = blast radius */
+ ASSERT(strstr(r, "seed_symbols:") != NULL);
+ /* format:"json" returns the same model as structured JSON. */
+ free(r);
+ r = call_tool_timed("detect_changes", &ms,
+ "{\"project\":\"%s\",\"base_branch\":\"HEAD\",\"format\":\"json\"}",
+ g_project);
+ TOOL_OK(r, ms);
+ ASSERT(resp_has_key(r, "impacted_total"));
+ ASSERT(resp_has_key(r, "direction"));
free(r);
PASS();
}
@@ -3100,6 +3124,7 @@ SUITE(incremental) {
/* Phase 15: detect_changes */
RUN_TEST(tool_detect_changes_default);
+ RUN_TEST(tool_detect_changes_impact_shape);
RUN_TEST(tool_detect_changes_custom_branch);
RUN_TEST(tool_detect_changes_since);
RUN_TEST(tool_detect_changes_since_precedence);
diff --git a/tests/test_integration.c b/tests/test_integration.c
index a63f9e5d4..aa5ad5187 100644
--- a/tests/test_integration.c
+++ b/tests/test_integration.c
@@ -633,6 +633,125 @@ TEST(store_bfs_edges_survive_large_visited_set) {
PASS();
}
+/* Multi-source BFS is the substrate for detect_changes impact analysis. Its
+ * contract (challenger's flagged traps): (1) ONE traversal over ALL seeds,
+ * not seed_count separate walks; (2) seeds EXCLUDED from the result even when
+ * reachable from another seed (changed files call each other — that is not
+ * "downstream impact"); (3) MIN(hop) across the whole seed set; (4) uncapped
+ * counting up to the memory ceiling, which sets *truncated when hit. Fixture:
+ * two seeds A, B; A->mid->leaf, B->leaf (leaf is hop 1 from B, hop 2 from A),
+ * and A->B directly (B reachable from A). Impact set must be {mid, leaf} with
+ * leaf at hop 1, and must NOT contain A or B. */
+TEST(store_bfs_multi_excludes_seeds_and_takes_min_hop) {
+ cbm_store_t *s = cbm_store_open_memory();
+ ASSERT_NOT_NULL(s);
+ ASSERT_EQ(cbm_store_upsert_project(s, "impact", "/tmp/impact"), CBM_STORE_OK);
+
+ int64_t ids[4];
+ const char *names[4] = {"A", "B", "mid", "leaf"};
+ for (int i = 0; i < 4; i++) {
+ char qn[32];
+ snprintf(qn, sizeof(qn), "impact.%s", names[i]);
+ cbm_node_t n = {.project = "impact",
+ .label = "Function",
+ .name = names[i],
+ .qualified_name = qn,
+ .file_path = "m.c",
+ .start_line = 1,
+ .end_line = 5};
+ ids[i] = cbm_store_upsert_node(s, &n);
+ ASSERT_GT(ids[i], 0);
+ }
+ int64_t A = ids[0];
+ int64_t B = ids[1];
+ int64_t mid = ids[2];
+ int64_t leaf = ids[3];
+ struct {
+ int64_t from;
+ int64_t to;
+ } edges[] = {{A, mid}, {mid, leaf}, {B, leaf}, {A, B}};
+ for (size_t i = 0; i < sizeof(edges) / sizeof(edges[0]); i++) {
+ cbm_edge_t e = {.project = "impact",
+ .source_id = edges[i].from,
+ .target_id = edges[i].to,
+ .type = "CALLS"};
+ ASSERT_GT(cbm_store_insert_edge(s, &e), 0);
+ }
+
+ int64_t seeds[2] = {A, B};
+ cbm_traverse_result_t tr = {0};
+ bool truncated = true;
+ ASSERT_EQ(cbm_store_bfs_multi(s, seeds, 2, "outbound", NULL, 0, 5, 100, &tr, &truncated),
+ CBM_STORE_OK);
+ ASSERT_FALSE(truncated);
+
+ /* Impact set = {mid, leaf}; A and B (seeds) excluded even though B is
+ * reachable from A. */
+ ASSERT_EQ(tr.visited_count, 2);
+ int seen_mid = 0;
+ int seen_leaf = 0;
+ int leaf_hop = -1;
+ for (int i = 0; i < tr.visited_count; i++) {
+ int64_t id = tr.visited[i].node.id;
+ ASSERT_TRUE(id != A && id != B); /* seeds never in the result */
+ if (id == mid) {
+ seen_mid = 1;
+ }
+ if (id == leaf) {
+ seen_leaf = 1;
+ leaf_hop = tr.visited[i].hop;
+ }
+ }
+ ASSERT_TRUE(seen_mid && seen_leaf);
+ ASSERT_EQ(leaf_hop, 1); /* MIN(hop): 1 from B, not 2 from A */
+ cbm_store_traverse_free(&tr);
+ cbm_store_close(s);
+ PASS();
+}
+
+/* The memory-safety ceiling reports truncation instead of silently capping.
+ * A star of N callees from one seed, ceiling = N/2, must return exactly N/2
+ * rows with *truncated = true. */
+TEST(store_bfs_multi_reports_truncation_at_ceiling) {
+ cbm_store_t *s = cbm_store_open_memory();
+ ASSERT_NOT_NULL(s);
+ ASSERT_EQ(cbm_store_upsert_project(s, "cap", "/tmp/cap"), CBM_STORE_OK);
+ cbm_node_t hub = {.project = "cap",
+ .label = "Function",
+ .name = "hub",
+ .qualified_name = "cap.hub",
+ .file_path = "h.c",
+ .start_line = 1,
+ .end_line = 2};
+ int64_t hub_id = cbm_store_upsert_node(s, &hub);
+ ASSERT_GT(hub_id, 0);
+ enum { N = 40, CEIL = 20 };
+ for (int i = 0; i < N; i++) {
+ char qn[32];
+ snprintf(qn, sizeof(qn), "cap.c%02d", i);
+ cbm_node_t n = {.project = "cap",
+ .label = "Function",
+ .name = qn + 4,
+ .qualified_name = qn,
+ .file_path = "c.c",
+ .start_line = 1,
+ .end_line = 2};
+ int64_t nid = cbm_store_upsert_node(s, &n);
+ ASSERT_GT(nid, 0);
+ cbm_edge_t e = {.project = "cap", .source_id = hub_id, .target_id = nid, .type = "CALLS"};
+ ASSERT_GT(cbm_store_insert_edge(s, &e), 0);
+ }
+ cbm_traverse_result_t tr = {0};
+ bool truncated = false;
+ ASSERT_EQ(cbm_store_bfs_multi(s, &hub_id, 1, "outbound", NULL, 0, 5, CEIL, &tr, &truncated),
+ CBM_STORE_OK);
+ ASSERT_EQ(tr.visited_count, CEIL);
+ ASSERT_TRUE(truncated);
+ cbm_store_traverse_free(&tr);
+ cbm_store_close(s);
+ PASS();
+}
+
/* #411: index_repository silently drops entire subtrees with no record.
* Moderate/fast mode applies FAST_SKIP_DIRS (tools/scripts/bin/docs/...) and ALL
* modes apply ALWAYS_SKIP_DIRS (node_modules/...), so files are excluded from the
@@ -721,6 +840,8 @@ SUITE(integration) {
RUN_TEST(integ_store_find_by_file);
RUN_TEST(integ_store_bfs_traversal);
RUN_TEST(store_bfs_edges_survive_large_visited_set);
+ RUN_TEST(store_bfs_multi_excludes_seeds_and_takes_min_hop);
+ RUN_TEST(store_bfs_multi_reports_truncation_at_ceiling);
/* Pipeline API tests (no db needed) */
RUN_TEST(integ_pipeline_fqn_compute);
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index bfccf786b..27d65fe9f 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -532,7 +532,7 @@ TEST(mcp_get_architecture_aspects_schema_enum_pr560) {
static const char *expected[] = {"all", "overview", "structure", "dependencies",
"routes", "languages", "packages", "entry_points",
"hotspots", "boundaries", "layers", "file_tree",
- "clusters"};
+ "clusters", "cycles"};
size_t expected_count = sizeof(expected) / sizeof(expected[0]);
ASSERT_EQ(yyjson_arr_size(enum_arr), expected_count);
for (size_t i = 0; i < expected_count; i++) {
@@ -1090,6 +1090,133 @@ static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz);
static void cleanup_snippet_dir(const char *tmp_dir);
static char *extract_text_content(const char *mcp_result);
+/* SCC condensation (get_architecture aspect "cycles"): a 3-function CALLS
+ * cycle A->B->C->A must be reported as one circular dependency of size 3 with
+ * all three members; a separate acyclic chain (D->E) must NOT appear. The
+ * aspect is opt-in — a default get_architecture call must NOT compute it. */
+TEST(tool_get_architecture_cycles_detects_scc) {
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *st = cbm_mcp_server_store(srv);
+ const char *proj = "cycproj";
+ cbm_mcp_server_set_project(srv, proj);
+ cbm_store_upsert_project(st, proj, "/tmp/cyc");
+
+ const char *names[5] = {"A", "B", "C", "D", "E"};
+ int64_t id[5];
+ for (int i = 0; i < 5; i++) {
+ char qn[32];
+ snprintf(qn, sizeof(qn), "cycproj.m.%s", names[i]);
+ cbm_node_t n = {.project = proj,
+ .label = "Function",
+ .name = names[i],
+ .qualified_name = qn,
+ .file_path = "m.c",
+ .start_line = i + 1,
+ .end_line = i + 2};
+ id[i] = cbm_store_upsert_node(st, &n);
+ ASSERT_GT(id[i], 0);
+ }
+ /* cycle A->B->C->A, plus acyclic D->E */
+ struct {
+ int f;
+ int t;
+ } e[] = {{0, 1}, {1, 2}, {2, 0}, {3, 4}};
+ for (size_t i = 0; i < sizeof(e) / sizeof(e[0]); i++) {
+ cbm_edge_t ed = {
+ .project = proj, .source_id = id[e[i].f], .target_id = id[e[i].t], .type = "CALLS"};
+ ASSERT_GT(cbm_store_insert_edge(st, &ed), 0);
+ }
+
+ /* opt-in cycles aspect */
+ char *resp = cbm_mcp_server_handle(
+ srv, "{\"jsonrpc\":\"2.0\",\"id\":71,\"method\":\"tools/call\",\"params\":{"
+ "\"name\":\"get_architecture\",\"arguments\":{\"project\":\"cycproj\","
+ "\"aspects\":[\"cycles\"]}}}");
+ ASSERT_NOT_NULL(resp);
+ char *inner = extract_text_content(resp);
+ ASSERT_NOT_NULL(inner);
+ ASSERT_NOT_NULL(strstr(inner, "cycles: 1")); /* exactly one SCC of size>1 */
+ ASSERT_NOT_NULL(strstr(inner, "cycproj.m.A"));
+ ASSERT_NOT_NULL(strstr(inner, "cycproj.m.B"));
+ ASSERT_NOT_NULL(strstr(inner, "cycproj.m.C"));
+ ASSERT_NULL(strstr(inner, "cycproj.m.D")); /* acyclic node not in any cycle */
+ free(inner);
+ free(resp);
+
+ /* default call (no aspects) must NOT run the scan. */
+ resp = cbm_mcp_server_handle(
+ srv, "{\"jsonrpc\":\"2.0\",\"id\":72,\"method\":\"tools/call\",\"params\":{"
+ "\"name\":\"get_architecture\",\"arguments\":{\"project\":\"cycproj\"}}}");
+ ASSERT_NOT_NULL(resp);
+ inner = extract_text_content(resp);
+ ASSERT_NOT_NULL(inner);
+ ASSERT_NULL(strstr(inner, "cycles:"));
+ free(inner);
+ free(resp);
+ cbm_mcp_server_free(srv);
+ PASS();
+}
+
+/* Context-bomb guard: get_code_snippet on a whole-file node (a Module/File
+ * span) used to read the ENTIRE file into one response — a field-eval agent
+ * that fell back to a Module snippet pulled ~400KB in a single call. The read
+ * must clip at MCP_SNIPPET_MAX_LINES and flag source_clipped, while the exact
+ * start/end range stays in the response for a targeted re-read. */
+TEST(tool_get_code_snippet_clips_whole_file_node) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_snipcap_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ char proj_dir[512];
+ snprintf(proj_dir, sizeof(proj_dir), "%s/project", tmp);
+ cbm_mkdir(proj_dir);
+ char src_path[600];
+ snprintf(src_path, sizeof(src_path), "%s/big.py", proj_dir);
+ FILE *fp = fopen(src_path, "w");
+ ASSERT_NOT_NULL(fp);
+ enum { BIG_LINES = 2000 };
+ for (int i = 0; i < BIG_LINES; i++) {
+ fprintf(fp, "line_%04d = %d # padding to blow up an unclipped read\n", i, i);
+ }
+ fclose(fp);
+
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *st = cbm_mcp_server_store(srv);
+ const char *proj = "test-project";
+ cbm_mcp_server_set_project(srv, proj);
+ cbm_store_upsert_project(st, proj, proj_dir);
+
+ cbm_node_t mod = {0};
+ mod.project = proj;
+ mod.label = "Module";
+ mod.name = "big";
+ mod.qualified_name = "test-project.big";
+ mod.file_path = "big.py";
+ mod.start_line = 1;
+ mod.end_line = BIG_LINES;
+ ASSERT_GT(cbm_store_upsert_node(st, &mod), 0);
+
+ char *resp = cbm_mcp_server_handle(
+ srv, "{\"jsonrpc\":\"2.0\",\"id\":70,\"method\":\"tools/call\",\"params\":{"
+ "\"name\":\"get_code_snippet\",\"arguments\":{\"project\":\"test-project\","
+ "\"qualified_name\":\"test-project.big\"}}}");
+ ASSERT_NOT_NULL(resp);
+ char *inner = extract_text_content(resp);
+ ASSERT_NOT_NULL(inner);
+ ASSERT_NOT_NULL(strstr(inner, "\"source_clipped\":true"));
+ /* The whole 2000-line file (~100KB) must NOT be in the response. */
+ ASSERT_TRUE(strlen(inner) < 60000);
+ /* The last line must be absent (clipped), the first present. */
+ ASSERT_NOT_NULL(strstr(inner, "line_0000"));
+ ASSERT_NULL(strstr(inner, "line_1999"));
+ free(inner);
+ free(resp);
+ cbm_mcp_server_free(srv);
+ th_rmtree(tmp);
+ PASS();
+}
+
TEST(tool_search_graph_includes_node_properties) {
/* Node properties are OPT-IN columns in the default TOON output: the
* default row is qn/label/file/lines/degrees only, `fields` adds the
@@ -6809,6 +6936,8 @@ SUITE(mcp) {
RUN_TEST(tool_get_graph_schema_empty);
RUN_TEST(tool_unknown_tool);
RUN_TEST(tool_search_graph_basic);
+ RUN_TEST(tool_get_architecture_cycles_detects_scc);
+ RUN_TEST(tool_get_code_snippet_clips_whole_file_node);
RUN_TEST(tool_search_graph_includes_node_properties);
RUN_TEST(tool_search_graph_toon_never_leaks_internal_fields);
RUN_TEST(tool_lean_defaults_schema_and_status);