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.

fix: sanitize graph comparison metadata

Martin Vogel7ba26f5216b4
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index e7077c12f..ada9e646d 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -3022,8 +3022,8 @@ static yyjson_mut_val *compare_project_json(yyjson_mut_doc *doc, const char *pro
const cbm_graph_compare_project_t *metadata) {
yyjson_mut_val *object = yyjson_mut_obj(doc);
if (!object || !yyjson_mut_obj_add_strcpy(doc, object, "project", project) ||
- !yyjson_mut_obj_add_strcpy(doc, object, "generation", metadata->generation) ||
- !yyjson_mut_obj_add_strcpy(doc, object, "index_mode", metadata->index_mode) ||
+ !compare_add_identity_string(doc, object, "generation", metadata->generation) ||
+ !compare_add_identity_string(doc, object, "index_mode", metadata->index_mode) ||
!yyjson_mut_obj_add_sint(doc, object, "node_count", metadata->node_count) ||
!yyjson_mut_obj_add_sint(doc, object, "edge_count", metadata->edge_count)) {
return NULL;
diff --git a/src/store/store.c b/src/store/store.c
index b991bb58e..ffb0047d1 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -1213,8 +1213,11 @@ int cbm_store_rollback(cbm_store_t *s) {
typedef struct {
cbm_graph_compare_cancel_fn cancel;
void *context;
+ bool progress_cancelled;
} graph_compare_progress_t;
+enum { GRAPH_COMPARE_PROGRESS_INTERVAL = 1000 };
+
typedef struct {
sqlite3_stmt *stmt;
cbm_graph_node_identity_t identity;
@@ -1230,6 +1233,7 @@ typedef struct {
#ifdef CBM_ENABLE_TEST_SEAMS
static atomic_int graph_compare_test_bind_countdown = ATOMIC_VAR_INIT(CBM_NOT_FOUND);
static atomic_int graph_compare_test_cancel_countdown = ATOMIC_VAR_INIT(CBM_NOT_FOUND);
+static atomic_bool graph_compare_test_progress_cancel = ATOMIC_VAR_INIT(false);
void cbm_store_compare_test_fail_bind_after(int successful_binds) {
atomic_store(&graph_compare_test_bind_countdown, successful_binds);
@@ -1239,6 +1243,10 @@ void cbm_store_compare_test_cancel_after(int successful_checks) {
atomic_store(&graph_compare_test_cancel_countdown, successful_checks);
}
+void cbm_store_compare_test_cancel_from_progress(bool enabled) {
+ atomic_store(&graph_compare_test_progress_cancel, enabled);
+}
+
static bool graph_compare_test_countdown_fires(atomic_int *countdown) {
int remaining = atomic_load(countdown);
while (remaining >= 0) {
@@ -1261,7 +1269,25 @@ static bool graph_compare_is_cancelled(const graph_compare_progress_t *progress)
}
static int graph_compare_progress(void *context) {
- return graph_compare_is_cancelled((const graph_compare_progress_t *)context) ? 1 : 0;
+ graph_compare_progress_t *progress = (graph_compare_progress_t *)context;
+#ifdef CBM_ENABLE_TEST_SEAMS
+ if (atomic_load(&graph_compare_test_progress_cancel)) {
+ progress->progress_cancelled = true;
+ return true;
+ }
+#endif
+ bool cancelled = graph_compare_is_cancelled(progress);
+ progress->progress_cancelled = cancelled;
+ return cancelled;
+}
+
+static int graph_compare_progress_interval(void) {
+#ifdef CBM_ENABLE_TEST_SEAMS
+ if (atomic_load(&graph_compare_test_progress_cancel)) {
+ return 1;
+ }
+#endif
+ return GRAPH_COMPARE_PROGRESS_INTERVAL;
}
static int graph_compare_bind_text(sqlite3_stmt *stmt, int column, const char *value) {
@@ -1594,8 +1620,10 @@ int cbm_store_compare_graphs(cbm_store_t *base_store, const char *base_project,
int rc = CBM_STORE_OK;
bool base_transaction = false;
bool target_transaction = false;
- sqlite3_progress_handler(base_store->db, 1000, graph_compare_progress, &progress);
- sqlite3_progress_handler(target_store->db, 1000, graph_compare_progress, &progress);
+ int progress_interval = graph_compare_progress_interval();
+ sqlite3_progress_handler(base_store->db, progress_interval, graph_compare_progress, &progress);
+ sqlite3_progress_handler(target_store->db, progress_interval, graph_compare_progress,
+ &progress);
rc = exec_sql(base_store, "BEGIN;");
if (rc == CBM_STORE_OK) {
@@ -1622,10 +1650,12 @@ int cbm_store_compare_graphs(cbm_store_t *base_store, const char *base_project,
rc = graph_compare_edges(base_store, base_project, target_store, target_project, &progress,
on_edge, out);
}
- if (rc == CBM_STORE_OK && graph_compare_is_cancelled(&progress)) {
+ if (rc == CBM_STORE_OK &&
+ (progress.progress_cancelled || graph_compare_is_cancelled(&progress))) {
rc = CBM_STORE_CANCELLED;
}
- if (rc != CBM_STORE_OK && graph_compare_is_cancelled(&progress)) {
+ if (rc != CBM_STORE_OK &&
+ (progress.progress_cancelled || graph_compare_is_cancelled(&progress))) {
rc = CBM_STORE_CANCELLED;
}
diff --git a/src/store/store.h b/src/store/store.h
index d9047f28f..0409071f1 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -317,6 +317,7 @@ int cbm_store_compare_graphs(cbm_store_t *base_store, const char *base_project,
* the seam. */
void cbm_store_compare_test_fail_bind_after(int successful_binds);
void cbm_store_compare_test_cancel_after(int successful_checks);
+void cbm_store_compare_test_cancel_from_progress(bool enabled);
#endif
/* ── Lifecycle ──────────────────────────────────────────────────── */
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index e867ac0e5..6099c1db2 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -7956,11 +7956,11 @@ typedef struct {
const char *file_path;
} compare_node_spec_t;
-static bool compare_write_coverage(cbm_store_t *store, const char *project,
- const char *generation) {
+static bool compare_write_coverage_mode(cbm_store_t *store, const char *project,
+ const char *generation, const char *index_mode) {
cbm_coverage_meta_t meta = {
.generation = generation,
- .index_mode = "full",
+ .index_mode = index_mode,
.recorded_at = "2026-08-28T00:00:00Z",
.recording_status = "complete",
.ignored_files_stored = 0,
@@ -7971,6 +7971,11 @@ static bool compare_write_coverage(cbm_store_t *store, const char *project,
return cbm_store_coverage_replace_ex(store, project, NULL, 0, &meta) == CBM_STORE_OK;
}
+static bool compare_write_coverage(cbm_store_t *store, const char *project,
+ const char *generation) {
+ return compare_write_coverage_mode(store, project, generation, "full");
+}
+
static bool compare_write_fixture_store(const char *path, const char *project, bool target) {
static const compare_node_spec_t base_nodes[] = {
{"pkg.common", "Function", "src/common.c"}, {"pkg.base", "Function", "src/base.c"},
@@ -8248,10 +8253,11 @@ static bool compare_write_single_node_store(const char *path, const char *projec
}
static bool compare_write_identity_store(const char *path, const char *project,
- const char *generation, const char *source_qn,
- const char *source_label, const char *source_file,
- const char *target_qn, const char *target_label,
- const char *target_file, const char *edge_type) {
+ const char *generation, const char *index_mode,
+ const char *source_qn, const char *source_label,
+ const char *source_file, const char *target_qn,
+ const char *target_label, const char *target_file,
+ const char *edge_type) {
cbm_store_t *store = cbm_store_open_path(path);
if (!store ||
cbm_store_upsert_project(store, project, "/tmp/compare-identity-525") != CBM_STORE_OK) {
@@ -8296,7 +8302,7 @@ static bool compare_write_identity_store(const char *path, const char *project,
};
ok = cbm_store_insert_edge(store, &edge) > 0;
}
- ok = ok && compare_write_coverage(store, project, generation) &&
+ ok = ok && compare_write_coverage_mode(store, project, generation, index_mode) &&
cbm_store_prepare_for_publish(store) == CBM_STORE_OK;
cbm_store_close(store);
return ok;
@@ -8309,10 +8315,10 @@ TEST(tool_compare_graphs_normalizes_legacy_path_separators_issue525) {
char target_path[768];
snprintf(base_path, sizeof(base_path), "%s/sepbase525.db", fixture.cache);
snprintf(target_path, sizeof(target_path), "%s/septarget525.db", fixture.cache);
- ASSERT_TRUE(compare_write_identity_store(base_path, "sepbase525", "sep-base", "pkg.source",
- "Function", "src\\same.c", "pkg.target", "Function",
- "src\\target.c", "CALLS"));
- ASSERT_TRUE(compare_write_identity_store(target_path, "septarget525", "sep-target",
+ ASSERT_TRUE(compare_write_identity_store(base_path, "sepbase525", "sep-base", "full",
+ "pkg.source", "Function", "src\\same.c", "pkg.target",
+ "Function", "src\\target.c", "CALLS"));
+ ASSERT_TRUE(compare_write_identity_store(target_path, "septarget525", "sep-target", "full",
"pkg.source", "Function", "src/same.c", "pkg.target",
"Function", "src/target.c", "CALLS"));
@@ -8358,6 +8364,14 @@ TEST(tool_compare_graphs_sanitizes_legacy_invalid_utf8_issue525) {
"bad.c";
static const char replacement_type[] = "\xEF\xBF\xBD"
"EDGE";
+ static const char invalid_generation[] = "utf-\xFF"
+ "generation";
+ static const char invalid_index_mode[] = "\xFE"
+ "full";
+ static const char replacement_generation[] = "utf-\xEF\xBF\xBD"
+ "generation";
+ static const char replacement_index_mode[] = "\xEF\xBF\xBD"
+ "full";
compare_graphs_fixture_t fixture;
ASSERT_TRUE(compare_graphs_fixture_open(&fixture));
@@ -8365,11 +8379,11 @@ TEST(tool_compare_graphs_sanitizes_legacy_invalid_utf8_issue525) {
char target_path[768];
snprintf(base_path, sizeof(base_path), "%s/utfbase525.db", fixture.cache);
snprintf(target_path, sizeof(target_path), "%s/utftarget525.db", fixture.cache);
- ASSERT_TRUE(compare_write_identity_store(base_path, "utfbase525", "utf-base", NULL, NULL, NULL,
- NULL, NULL, NULL, NULL));
- ASSERT_TRUE(compare_write_identity_store(target_path, "utftarget525", "utf-target", invalid_qn,
- invalid_label, invalid_file, safe_qn, "Function",
- "src/sink.c", invalid_type));
+ ASSERT_TRUE(compare_write_identity_store(base_path, "utfbase525", "utf-base", "full", NULL,
+ NULL, NULL, NULL, NULL, NULL, NULL));
+ ASSERT_TRUE(compare_write_identity_store(
+ target_path, "utftarget525", invalid_generation, invalid_index_mode, invalid_qn,
+ invalid_label, invalid_file, safe_qn, "Function", "src/sink.c", invalid_type));
cbm_mcp_server_t *server = cbm_mcp_server_new(NULL);
ASSERT_NOT_NULL(server);
@@ -8384,6 +8398,9 @@ TEST(tool_compare_graphs_sanitizes_legacy_invalid_utf8_issue525) {
yyjson_doc *doc = inner ? yyjson_read(inner, strlen(inner), 0) : NULL;
ASSERT_NOT_NULL(doc);
yyjson_val *root = yyjson_doc_get_root(doc);
+ yyjson_val *target = yyjson_obj_get(root, "target");
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(target, "generation")), replacement_generation);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(target, "index_mode")), replacement_index_mode);
yyjson_val *items =
yyjson_obj_get(yyjson_obj_get(yyjson_obj_get(root, "nodes"), "added"), "items");
bool found_node = false;
@@ -8486,6 +8503,52 @@ TEST(tool_compare_graphs_midscan_cancel_restores_store_state_issue525) {
PASS();
}
+TEST(tool_compare_graphs_progress_cancel_clears_handler_issue525) {
+ compare_graphs_fixture_t fixture;
+ ASSERT_TRUE(compare_graphs_fixture_open(&fixture));
+ cbm_mcp_server_t *server = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(server);
+
+ cbm_store_compare_test_cancel_from_progress(true);
+ char *cancelled = compare_graphs_call(server, NULL);
+ ASSERT_NOT_NULL(cancelled);
+ ASSERT_NOT_NULL(strstr(cancelled, "\"isError\":true"));
+ ASSERT_NOT_NULL(strstr(cancelled, "cancelled"));
+ char *inner = extract_text_content(cancelled);
+ ASSERT_NOT_NULL(inner);
+ ASSERT_NULL(strstr(inner, "\"nodes\""));
+ free(inner);
+ free(cancelled);
+
+ cbm_store_t *base_store = cbm_store_open_path_query(fixture.base_path);
+ cbm_store_t *target_store = cbm_store_open_path_query(fixture.target_path);
+ ASSERT_NOT_NULL(base_store);
+ ASSERT_NOT_NULL(target_store);
+ cbm_graph_compare_result_t result = {0};
+ ASSERT_EQ(cbm_store_compare_graphs(base_store, "base525", target_store, "target525", 100, NULL,
+ NULL, NULL, NULL, &result),
+ CBM_STORE_CANCELLED);
+ ASSERT_EQ(result.nodes_added_total, 0);
+ ASSERT_EQ(result.nodes_removed_total, 0);
+ ASSERT_EQ(result.edges_added_total, 0);
+ ASSERT_EQ(result.edges_removed_total, 0);
+
+ static const char expensive_query[] =
+ "WITH RECURSIVE sequence(value) AS (VALUES(0) UNION ALL "
+ "SELECT value+1 FROM sequence WHERE value<5000) SELECT sum(value) FROM sequence;";
+ int base_query_rc = cbm_store_exec(base_store, expensive_query);
+ int target_query_rc = cbm_store_exec(target_store, expensive_query);
+ cbm_store_compare_test_cancel_from_progress(false);
+ cbm_store_close(target_store);
+ cbm_store_close(base_store);
+ ASSERT_EQ(base_query_rc, CBM_STORE_OK);
+ ASSERT_EQ(target_query_rc, CBM_STORE_OK);
+
+ cbm_mcp_server_free(server);
+ ASSERT_TRUE(compare_graphs_fixture_close(&fixture));
+ PASS();
+}
+
TEST(tool_compare_graphs_enforces_encoded_budget_issue525) {
compare_graphs_fixture_t fixture;
ASSERT_TRUE(compare_graphs_fixture_open(&fixture));
@@ -12295,6 +12358,7 @@ SUITE(mcp) {
RUN_TEST(tool_compare_graphs_sanitizes_legacy_invalid_utf8_issue525);
RUN_TEST(tool_compare_graphs_bind_failures_are_atomic_issue525);
RUN_TEST(tool_compare_graphs_midscan_cancel_restores_store_state_issue525);
+ RUN_TEST(tool_compare_graphs_progress_cancel_clears_handler_issue525);
RUN_TEST(tool_compare_graphs_enforces_encoded_budget_issue525);
RUN_TEST(tool_compare_graphs_validation_and_scan_cap_are_atomic_issue525);
RUN_TEST(tool_compare_graphs_cancel_and_readonly_handles_release_issue525);