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.

Wire up risk_labels on trace_path, add --progress CLI flag

Martin Vogeldbab4d372f9d
diff --git a/Makefile.cbm b/Makefile.cbm
index c553c3e8a..b2e95b461 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -194,7 +194,7 @@ TRACES_SRCS = src/traces/traces.c
WATCHER_SRCS = src/watcher/watcher.c
# CLI module (new)
-CLI_SRCS = src/cli/cli.c
+CLI_SRCS = src/cli/cli.c src/cli/progress_sink.c
# UI module (graph visualization)
UI_SRCS = \
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 44faf1913..ecf198166 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -405,6 +405,7 @@ static const char skill_content[] =
"| Dead code | `search_graph(max_degree=0, exclude_entry_points=true)` |\n"
"| Cross-service edges | `query_graph` with Cypher |\n"
"| Impact of local changes | `detect_changes()` |\n"
+ "| Risk-classified trace | `trace_path(risk_labels=true)` |\n"
"| Text search | `search_code` or Grep |\n"
"\n"
"## Exploration Workflow\n"
diff --git a/src/cli/progress_sink.c b/src/cli/progress_sink.c
new file mode 100644
index 000000000..231277373
--- /dev/null
+++ b/src/cli/progress_sink.c
@@ -0,0 +1,217 @@
+/*
+ * progress_sink.c — Human-readable progress for --progress CLI flag.
+ *
+ * Maps structured log events (msg=pass.timing, msg=pipeline.done, etc.)
+ * to phase labels on stderr. When installed, replaces default log output.
+ *
+ * Thread-safe: fprintf has per-FILE* locking on POSIX.
+ */
+#include "progress_sink.h"
+#include "foundation/constants.h"
+#include "foundation/log.h"
+
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+enum { PERCENT = 100, NOT_SET = -1 };
+
+static FILE *s_out;
+static atomic_int s_needs_newline;
+static int s_gbuf_nodes = NOT_SET;
+static int s_gbuf_edges = NOT_SET;
+
+/* Extract value of "key=VALUE" from a structured log line. */
+static const char *extract_kv(const char *line, const char *key, char *buf, int buf_len) {
+ if (!line || !key || !buf || buf_len <= 0) {
+ return NULL;
+ }
+ size_t klen = strlen(key);
+ const char *p = line;
+ while (*p) {
+ if ((p == line || p[-SKIP_ONE] == ' ') && strncmp(p, key, klen) == 0 && p[klen] == '=') {
+ const char *val = p + klen + SKIP_ONE;
+ int i = 0;
+ while (val[i] && val[i] != ' ' && i < buf_len - SKIP_ONE) {
+ buf[i] = val[i];
+ i++;
+ }
+ buf[i] = '\0';
+ return buf;
+ }
+ p++;
+ }
+ return NULL;
+}
+
+void cbm_progress_sink_init(FILE *out) {
+ s_out = out ? out : stderr;
+ atomic_store(&s_needs_newline, 0);
+ s_gbuf_nodes = NOT_SET;
+ s_gbuf_edges = NOT_SET;
+ cbm_log_set_sink(cbm_progress_sink_fn);
+}
+
+void cbm_progress_sink_fini(void) {
+ if (atomic_load(&s_needs_newline) && s_out) {
+ (void)fprintf(s_out, "\n");
+ (void)fflush(s_out);
+ }
+ cbm_log_set_sink(NULL);
+ s_out = NULL;
+}
+
+/* Phase label table: maps pass names to display labels. */
+typedef struct {
+ const char *pass;
+ const char *label;
+} phase_t;
+
+static const phase_t phases[] = {
+ {"parallel_extract", "[2/9] Extracting definitions"},
+ {"registry_build", "[3/9] Building registry"},
+ {"parallel_resolve", "[4/9] Resolving calls & edges"},
+ {"tests", "[5/9] Detecting tests"},
+ {"httplinks", "[6/9] Scanning HTTP links"},
+ {"githistory_compute", "[7/9] Analyzing git history"},
+ {"configlink", "[8/9] Linking config files"},
+ {"dump", "[9/9] Writing database"},
+};
+
+enum { PHASE_COUNT = sizeof(phases) / sizeof(phases[0]) };
+
+/* Flush pending \r line if needed. */
+static void flush_carriage(void) {
+ if (atomic_load(&s_needs_newline)) {
+ (void)fprintf(s_out, "\n");
+ atomic_store(&s_needs_newline, 0);
+ }
+}
+
+/* Handle pipeline.discover event. */
+static void on_discover(const char *line) {
+ char files[CBM_SZ_32] = {0};
+ if (extract_kv(line, "files", files, (int)sizeof(files))) {
+ (void)fprintf(s_out, " Discovering files (%s found)\n", files);
+ } else {
+ (void)fprintf(s_out, " Discovering files...\n");
+ }
+ (void)fflush(s_out);
+}
+
+/* Handle pipeline.route event. */
+static void on_route(const char *line) {
+ char val[CBM_SZ_32] = {0};
+ const char *path = extract_kv(line, "path", val, (int)sizeof(val));
+ if (path && strcmp(path, "incremental") == 0) {
+ (void)fprintf(s_out, " Starting incremental index\n");
+ } else {
+ (void)fprintf(s_out, " Starting full index\n");
+ }
+ (void)fflush(s_out);
+}
+
+/* Handle pass.start event. */
+static void on_pass_start(const char *line) {
+ char val[CBM_SZ_64] = {0};
+ const char *pass = extract_kv(line, "pass", val, (int)sizeof(val));
+ if (pass && strcmp(pass, "structure") == 0) {
+ (void)fprintf(s_out, "[1/9] Building file structure\n");
+ (void)fflush(s_out);
+ }
+}
+
+/* Handle pass.timing event. */
+static void on_pass_timing(const char *line) {
+ char val[CBM_SZ_64] = {0};
+ const char *pass = extract_kv(line, "pass", val, (int)sizeof(val));
+ if (!pass) {
+ return;
+ }
+ flush_carriage();
+ for (int i = 0; i < PHASE_COUNT; i++) {
+ if (strcmp(pass, phases[i].pass) == 0) {
+ (void)fprintf(s_out, "%s\n", phases[i].label);
+ (void)fflush(s_out);
+ return;
+ }
+ }
+}
+
+/* Handle gbuf.dump event — capture node/edge counts. */
+static void on_gbuf_dump(const char *line) {
+ char n[CBM_SZ_32] = {0};
+ char e[CBM_SZ_32] = {0};
+ if (extract_kv(line, "nodes", n, (int)sizeof(n))) {
+ s_gbuf_nodes = (int)strtol(n, NULL, CBM_DECIMAL_BASE);
+ }
+ if (extract_kv(line, "edges", e, (int)sizeof(e))) {
+ s_gbuf_edges = (int)strtol(e, NULL, CBM_DECIMAL_BASE);
+ }
+}
+
+/* Handle pipeline.done event. */
+static void on_done(const char *line) {
+ flush_carriage();
+ char ms[CBM_SZ_32] = {0};
+ const char *elapsed = extract_kv(line, "elapsed_ms", ms, (int)sizeof(ms));
+ if (s_gbuf_nodes >= 0 && s_gbuf_edges >= 0 && elapsed) {
+ (void)fprintf(s_out, "Done: %d nodes, %d edges (%s ms)\n", s_gbuf_nodes, s_gbuf_edges,
+ elapsed);
+ } else if (s_gbuf_nodes >= 0 && s_gbuf_edges >= 0) {
+ (void)fprintf(s_out, "Done: %d nodes, %d edges\n", s_gbuf_nodes, s_gbuf_edges);
+ } else {
+ (void)fprintf(s_out, "Done.\n");
+ }
+ (void)fflush(s_out);
+}
+
+/* Handle parallel.extract.progress event — in-place counter. */
+static void on_extract_progress(const char *line) {
+ char done[CBM_SZ_32] = {0};
+ char total[CBM_SZ_32] = {0};
+ if (extract_kv(line, "done", done, (int)sizeof(done)) &&
+ extract_kv(line, "total", total, (int)sizeof(total))) {
+ long d = strtol(done, NULL, CBM_DECIMAL_BASE);
+ long t = strtol(total, NULL, CBM_DECIMAL_BASE);
+ int pct = (t > 0) ? (int)((d * PERCENT) / t) : 0;
+ (void)fprintf(s_out, "\r Extracting: %ld/%ld files (%d%%)", d, t, pct);
+ (void)fflush(s_out);
+ atomic_store(&s_needs_newline, SKIP_ONE);
+ }
+}
+
+/* Event dispatch table. */
+typedef struct {
+ const char *msg;
+ void (*handler)(const char *line);
+} event_handler_t;
+
+static const event_handler_t handlers[] = {
+ {"pipeline.discover", on_discover},
+ {"pipeline.route", on_route},
+ {"pass.start", on_pass_start},
+ {"pass.timing", on_pass_timing},
+ {"gbuf.dump", on_gbuf_dump},
+ {"pipeline.done", on_done},
+ {"parallel.extract.progress", on_extract_progress},
+};
+
+enum { HANDLER_COUNT = sizeof(handlers) / sizeof(handlers[0]) };
+
+void cbm_progress_sink_fn(const char *line) {
+ if (!line || !s_out) {
+ return;
+ }
+ char msg[CBM_SZ_64] = {0};
+ if (!extract_kv(line, "msg", msg, (int)sizeof(msg))) {
+ return;
+ }
+ for (int i = 0; i < HANDLER_COUNT; i++) {
+ if (strcmp(msg, handlers[i].msg) == 0) {
+ handlers[i].handler(line);
+ return;
+ }
+ }
+}
diff --git a/src/cli/progress_sink.h b/src/cli/progress_sink.h
new file mode 100644
index 000000000..356b7049a
--- /dev/null
+++ b/src/cli/progress_sink.h
@@ -0,0 +1,19 @@
+/*
+ * progress_sink.h — Human-readable progress output for --progress CLI flag.
+ *
+ * Installs a log sink that maps structured pipeline events to phase labels.
+ * Usage:
+ * cbm_progress_sink_init(stderr);
+ * // ... run pipeline ...
+ * cbm_progress_sink_fini();
+ */
+#ifndef CBM_PROGRESS_SINK_H
+#define CBM_PROGRESS_SINK_H
+
+#include <stdio.h>
+
+void cbm_progress_sink_init(FILE *out);
+void cbm_progress_sink_fini(void);
+void cbm_progress_sink_fn(const char *line);
+
+#endif
diff --git a/src/foundation/log.c b/src/foundation/log.c
index 627934357..b4daa229e 100644
--- a/src/foundation/log.c
+++ b/src/foundation/log.c
@@ -65,12 +65,12 @@ void cbm_log(CBMLogLevel level, const char *msg, ...) {
}
va_end(args);
- /* Write to stderr */
- (void)fprintf(stderr, "%s\n", line_buf);
-
- /* Send to sink if registered */
+ /* When a sink is registered it takes over all output (exclusive).
+ * Otherwise write structured log to stderr. */
if (g_log_sink) {
g_log_sink(line_buf);
+ } else {
+ (void)fprintf(stderr, "%s\n", line_buf);
}
}
@@ -83,9 +83,9 @@ void cbm_log_int(CBMLogLevel level, const char *msg, const char *key, int64_t va
snprintf(line_buf, sizeof(line_buf), "level=%s msg=%s %s=%" PRId64, level_str(level),
msg ? msg : "", key ? key : "?", value);
- (void)fprintf(stderr, "%s\n", line_buf);
-
if (g_log_sink) {
g_log_sink(line_buf);
+ } else {
+ (void)fprintf(stderr, "%s\n", line_buf);
}
}
diff --git a/src/main.c b/src/main.c
index 822aa02bf..a612d3f82 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,7 @@
#include "pipeline/pipeline.h"
#include "store/store.h"
#include "cli/cli.h"
+#include "cli/progress_sink.h"
#include "foundation/constants.h"
enum {
@@ -117,16 +118,43 @@ static int watcher_index_fn(const char *project_name, const char *root_path, voi
static int run_cli(int argc, char **argv) {
if (argc < MAIN_MIN_ARGC) {
- (void)fprintf(stderr, "Usage: codebase-memory-mcp cli <tool_name> [json_args]\n");
+ (void)fprintf(stderr,
+ "Usage: codebase-memory-mcp cli [--progress] <tool_name> [json_args]\n");
+ return SKIP_ONE;
+ }
+
+ /* Strip --progress flag from argv. */
+ bool progress = false;
+ for (int i = 0; i < argc; i++) {
+ if (strcmp(argv[i], "--progress") == 0) {
+ progress = true;
+ for (int j = i; j < argc - SKIP_ONE; j++) {
+ argv[j] = argv[j + SKIP_ONE];
+ }
+ argc--;
+ break;
+ }
+ }
+
+ if (argc < MAIN_MIN_ARGC) {
+ (void)fprintf(stderr,
+ "Usage: codebase-memory-mcp cli [--progress] <tool_name> [json_args]\n");
return SKIP_ONE;
}
const char *tool_name = argv[0];
const char *args_json = argc >= MAIN_CLI_ARGC ? argv[SKIP_ONE] : "{}";
+ if (progress) {
+ cbm_progress_sink_init(stderr);
+ }
+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
if (!srv) {
(void)fprintf(stderr, "Failed to create server\n");
+ if (progress) {
+ cbm_progress_sink_fini();
+ }
return SKIP_ONE;
}
@@ -137,6 +165,9 @@ static int run_cli(int argc, char **argv) {
}
cbm_mcp_server_free(srv);
+ if (progress) {
+ cbm_progress_sink_fini();
+ }
return 0;
}
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index e69a21f29..9d75b4ab3 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -294,7 +294,9 @@ static const tool_def_t TOOLS[] = {
"with arg expressions. cross_service: follow HTTP_CALLS+ASYNC_CALLS+DATA_FLOWS through "
"Routes.\"},\"parameter_name\":{\"type\":\"string\",\"description\":\"For data_flow mode: "
"scope trace to a specific parameter name\"},\"edge_types\":{\"type\":\"array\",\"items\":{"
- "\"type\":\"string\"}}},\"required\":[\"function_name\",\"project\"]}"},
+ "\"type\":\"string\"}},\"risk_labels\":{\"type\":\"boolean\",\"default\":false,"
+ "\"description\":\"Add risk classification (CRITICAL/HIGH/MEDIUM/LOW) based on hop distance"
+ "\"}},\"required\":[\"function_name\",\"project\"]}"},
{"get_code_snippet",
"Read source code for a function/class/symbol. IMPORTANT: First call search_graph to find the "
@@ -1312,7 +1314,8 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
}
/* Convert BFS traversal results into a yyjson_mut array of {name, qualified_name, hop}. */
-static yyjson_mut_val *bfs_to_json_array(yyjson_mut_doc *doc, cbm_traverse_result_t *tr) {
+static yyjson_mut_val *bfs_to_json_array(yyjson_mut_doc *doc, cbm_traverse_result_t *tr,
+ bool risk_labels) {
yyjson_mut_val *arr = yyjson_mut_arr(doc);
for (int i = 0; i < tr->visited_count; i++) {
yyjson_mut_val *item = yyjson_mut_obj(doc);
@@ -1322,6 +1325,10 @@ static yyjson_mut_val *bfs_to_json_array(yyjson_mut_doc *doc, cbm_traverse_resul
doc, item, "qualified_name",
tr->visited[i].node.qualified_name ? tr->visited[i].node.qualified_name : "");
yyjson_mut_obj_add_int(doc, item, "hop", tr->visited[i].hop);
+ if (risk_labels) {
+ yyjson_mut_obj_add_str(doc, item, "risk",
+ cbm_risk_label(cbm_hop_to_risk(tr->visited[i].hop)));
+ }
yyjson_mut_arr_add_val(arr, item);
}
return arr;
@@ -1335,6 +1342,7 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
char *mode = cbm_mcp_get_string_arg(args, "mode");
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");
if (!func_name) {
free(project);
@@ -1411,13 +1419,13 @@ 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));
+ yyjson_mut_obj_add_val(doc, root, "callees", bfs_to_json_array(doc, &tr_out, risk_labels));
}
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));
+ yyjson_mut_obj_add_val(doc, root, "callers", bfs_to_json_array(doc, &tr_in, risk_labels));
}
/* Serialize BEFORE freeing traversal results (yyjson borrows strings) */