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(graph): a node's variant spans no longer depend on worker order

Martin Vogel59ff283e5fcf
diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.c
index 7f44a2d19..fd81f2c40 100644
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@ -105,6 +105,11 @@ struct cbm_gbuf {
bool by_id_off;
int64_t by_id_cap;
+ /* Same-QN collision spans, written on export (cbm_gbuf_settle_variants).
+ * key: the surviving node's own QN string, value: gb_span_vec_t*. NULL
+ * until the first collision. */
+ CBMHashTable *variant_spans;
+
/* Secondary node indexes */
CBMHashTable *nodes_by_label; /* key: label, value: (node_ptr_array_t*) */
CBMHashTable *nodes_by_name; /* key: name, value: (node_ptr_array_t*) */
@@ -672,10 +677,16 @@ cbm_gbuf_t *cbm_gbuf_new_worker(const char *project, const char *root_path,
return gb;
}
+static void gb_free_span_vec(const char *qn, void *value, void *ud);
+
void cbm_gbuf_free(cbm_gbuf_t *gb) {
if (!gb) {
return;
}
+ if (gb->variant_spans) {
+ cbm_ht_foreach(gb->variant_spans, gb_free_span_vec, NULL);
+ cbm_ht_free(gb->variant_spans);
+ }
/* Free each individually-allocated node */
for (int i = 0; i < gb->nodes.count; i++) {
@@ -864,13 +875,31 @@ static void gb_yy_free(void *ctx, void *block) {
static const yyjson_alc gb_yy_alc = {gb_yy_malloc, gb_yy_realloc, gb_yy_free, NULL};
-enum { GB_VARIANTS_MAX = 64 };
+typedef struct {
+ const char *file; /* interned in the buffer's pool */
+ int start;
+ int end;
+} gb_span_t;
+/* One side of a same-QN collision. */
typedef struct {
+ const char *label;
+ const char *props;
const char *file;
int start;
int end;
-} gb_span_t;
+} gb_def_t;
+
+/* Every definition span that collapsed into one node, gathered while the
+ * buffer is built and written into the node's `variants` once, on export
+ * (cbm_gbuf_settle_variants). Rewriting a growing JSON list on every
+ * collision was quadratic in the collisions of one QN: 7,586 junit
+ * `testcase` elements in one file. */
+typedef struct {
+ gb_span_t *items;
+ size_t count;
+ size_t cap;
+} gb_span_vec_t;
static bool gb_variant_label(const char *label) {
static const char *const labels[] = {"Function", "Method", "Class", "Interface", "Enum",
@@ -896,99 +925,219 @@ static int gb_span_cmp(const void *a, const void *b) {
return c;
}
-/* A definition's spans: its `variants` property, else its own span. */
-static int gb_spans_of(yyjson_doc *doc, const char *file, int start, int end, gb_span_t *out,
- int n) {
+/* The non-empty `variants` list of a properties document, or NULL. */
+static yyjson_val *gb_variants_list(yyjson_doc *doc) {
yyjson_val *list = doc ? yyjson_obj_get(yyjson_doc_get_root(doc), "variants") : NULL;
- if (yyjson_is_arr(list) && yyjson_arr_size(list) > 0) {
- size_t i;
- size_t max;
- yyjson_val *v;
- yyjson_arr_foreach(list, i, max, v) {
- const char *f = yyjson_get_str(yyjson_obj_get(v, "file_path"));
- int s = (int)yyjson_get_int(yyjson_obj_get(v, "start_line"));
- int e = (int)yyjson_get_int(yyjson_obj_get(v, "end_line"));
- if (f && s > 0 && n < GB_VARIANTS_MAX) {
- out[n++] = (gb_span_t){f, s, e};
+ return yyjson_is_arr(list) && yyjson_arr_size(list) > 0 ? list : NULL;
+}
+
+static bool gb_props_have_variants(const char *props) {
+ return props && strstr(props, "\"variants\"") != NULL;
+}
+
+/* Does this side carry a span: a variants list, or a definition's own span? */
+static bool gb_def_has_spans(const gb_def_t *d) {
+ return gb_props_have_variants(d->props) || (gb_variant_label(d->label) && d->start > 0);
+}
+
+static void gb_span_push(gb_span_vec_t *vec, gb_span_t span) {
+ if (vec->count == vec->cap) {
+ enum { GB_SPANS_MIN = 4 };
+ size_t cap = vec->cap ? vec->cap * PAIR_LEN : GB_SPANS_MIN;
+ gb_span_t *grown = cbm_realloc(CBM_MEM_CLASS_GBUF_INDEX, vec->items, cap * sizeof(*grown));
+ if (!grown) {
+ return; /* a lost span costs precision, never data */
+ }
+ vec->items = grown;
+ vec->cap = cap;
+ }
+ vec->items[vec->count++] = span;
+}
+
+/* Append a side's spans: its `variants` list, else its own span when it is a
+ * definition. File paths are interned, so the spans outlive the props. */
+static void gb_collect_spans(cbm_gbuf_t *gb, gb_span_vec_t *vec, const gb_def_t *d) {
+ if (gb_props_have_variants(d->props)) {
+ yyjson_doc *doc = yyjson_read_opts((char *)d->props, strlen(d->props), 0, &gb_yy_alc, NULL);
+ yyjson_val *list = gb_variants_list(doc);
+ if (list) {
+ size_t i;
+ size_t max;
+ yyjson_val *v;
+ yyjson_arr_foreach(list, i, max, v) {
+ const char *f = yyjson_get_str(yyjson_obj_get(v, "file_path"));
+ int st = (int)yyjson_get_int(yyjson_obj_get(v, "start_line"));
+ int en = (int)yyjson_get_int(yyjson_obj_get(v, "end_line"));
+ if (f && st > 0) {
+ gb_span_push(vec, (gb_span_t){gb_intern(gb, f), st, en});
+ }
}
+ yyjson_doc_free(doc);
+ return;
}
- return n;
+ yyjson_doc_free(doc);
}
- if (start > 0 && n < GB_VARIANTS_MAX) {
- out[n++] = (gb_span_t){file ? file : "", start, end};
+ if (gb_variant_label(d->label) && d->start > 0) {
+ gb_span_push(vec, (gb_span_t){gb_intern(gb, d->file ? d->file : ""), d->start, d->end});
}
- return n;
}
-/* The survivor's properties with the union of both definitions' variant
- * spans. Returns a GBUF_STRING-owned JSON string, or NULL when there is
- * nothing to record (fewer than two distinct spans) or on any failure — the
- * survivor then keeps its own properties: a lost variants list costs
- * precision, never data. */
-static char *gb_variants_props(const char *survivor_props, const char *a_props, const char *a_file,
- int a_start, int a_end, const char *b_props, const char *b_file,
- int b_start, int b_end) {
- if (a_start <= 0 || b_start <= 0) {
- return NULL;
+/* Append spans gathered in another buffer (a worker's), re-interned here. */
+static void gb_append_spans(cbm_gbuf_t *gb, gb_span_vec_t *vec, const gb_span_vec_t *from) {
+ for (size_t i = 0; i < from->count; i++) {
+ gb_span_t span = from->items[i];
+ span.file = gb_intern(gb, span.file);
+ gb_span_push(vec, span);
+ }
+}
+
+/* A same-QN collision: gather both sides' spans for the surviving node,
+ * whichever label wins and however many there are. Call it before the
+ * winner is decided, while `existing` still holds its own fields.
+ * `arrival_spans` are the arrival's spans gathered in a worker buffer (they
+ * already include its own); NULL when it brings none. */
+static void gb_note_collision(cbm_gbuf_t *gb, const cbm_gbuf_node_t *existing,
+ const gb_def_t *arrival, const gb_span_vec_t *arrival_spans) {
+ gb_def_t prior = {existing->label, existing->properties_json, existing->file_path,
+ existing->start_line, existing->end_line};
+ bool arrival_has = arrival_spans ? arrival_spans->count > 0 : gb_def_has_spans(arrival);
+ if (!existing->qualified_name || (!gb_def_has_spans(&prior) && !arrival_has)) {
+ return;
}
- yyjson_doc *a_doc =
- a_props ? yyjson_read_opts((char *)a_props, strlen(a_props), 0, &gb_yy_alc, NULL) : NULL;
- yyjson_doc *b_doc =
- b_props ? yyjson_read_opts((char *)b_props, strlen(b_props), 0, &gb_yy_alc, NULL) : NULL;
- gb_span_t spans[GB_VARIANTS_MAX * 2];
- int n = gb_spans_of(a_doc, a_file, a_start, a_end, spans, 0);
- n = gb_spans_of(b_doc, b_file, b_start, b_end, spans, n);
- qsort(spans, (size_t)n, sizeof(spans[0]), gb_span_cmp);
- int unique = 0;
- for (int i = 0; i < n; i++) {
- if (unique == 0 || gb_span_cmp(&spans[unique - 1], &spans[i]) != 0) {
- spans[unique++] = spans[i];
+ if (!gb->variant_spans) {
+ gb->variant_spans = cbm_ht_create_in(CBM_MEM_CLASS_GBUF_INDEX, 0);
+ if (!gb->variant_spans) {
+ return;
}
}
- char *out = NULL;
- if (unique >= 2) {
- yyjson_mut_doc *mut = yyjson_mut_doc_new(&gb_yy_alc);
- yyjson_doc *s_doc = survivor_props
- ? yyjson_read_opts((char *)survivor_props, strlen(survivor_props),
- 0, &gb_yy_alc, NULL)
- : NULL;
- yyjson_mut_val *root = s_doc && yyjson_is_obj(yyjson_doc_get_root(s_doc))
- ? yyjson_val_mut_copy(mut, yyjson_doc_get_root(s_doc))
- : yyjson_mut_obj(mut);
- yyjson_mut_val *list = yyjson_mut_arr(mut);
- for (int i = 0; root && list && i < unique; i++) {
- yyjson_mut_val *v = yyjson_mut_arr_add_obj(mut, list);
- yyjson_mut_obj_add_strcpy(mut, v, "file_path", spans[i].file);
- yyjson_mut_obj_add_int(mut, v, "start_line", spans[i].start);
- yyjson_mut_obj_add_int(mut, v, "end_line", spans[i].end);
- }
- if (mut && root && list) {
- yyjson_mut_obj_remove_key(root, "variants");
- yyjson_mut_obj_add_val(mut, root, "variants", list);
- yyjson_mut_doc_set_root(mut, root);
- out = yyjson_mut_write_opts(mut, 0, &gb_yy_alc, NULL, NULL);
+ gb_span_vec_t *vec = cbm_ht_get(gb->variant_spans, existing->qualified_name);
+ if (!vec) {
+ vec = cbm_calloc(CBM_MEM_CLASS_GBUF_INDEX, sizeof(*vec));
+ if (!vec) {
+ return;
}
- yyjson_doc_free(s_doc);
- yyjson_mut_doc_free(mut);
+ gb_collect_spans(gb, vec, &prior);
+ cbm_ht_set(gb->variant_spans, existing->qualified_name, vec);
}
- yyjson_doc_free(a_doc);
- yyjson_doc_free(b_doc);
- return out;
+ if (arrival_spans) {
+ gb_append_spans(gb, vec, arrival_spans);
+ } else {
+ gb_collect_spans(gb, vec, arrival);
+ }
+}
+
+/* A worker node copied into this buffer brings the spans it gathered there. */
+static void gb_adopt_spans(cbm_gbuf_t *gb, const cbm_gbuf_node_t *node, const gb_span_vec_t *from) {
+ if (!node || !node->qualified_name || !from || from->count == 0) {
+ return;
+ }
+ if (!gb->variant_spans) {
+ gb->variant_spans = cbm_ht_create_in(CBM_MEM_CLASS_GBUF_INDEX, 0);
+ if (!gb->variant_spans) {
+ return;
+ }
+ }
+ gb_span_vec_t *vec = cbm_ht_get(gb->variant_spans, node->qualified_name);
+ if (!vec) {
+ vec = cbm_calloc(CBM_MEM_CLASS_GBUF_INDEX, sizeof(*vec));
+ if (!vec) {
+ return;
+ }
+ cbm_ht_set(gb->variant_spans, node->qualified_name, vec);
+ }
+ gb_append_spans(gb, vec, from);
}
-/* Record the variant spans of a same-QN collision on the surviving node. */
-static void gb_record_variants(cbm_gbuf_node_t *survivor, const char *a_label, const char *a_props,
- const char *a_file, int a_start, int a_end, const char *b_label,
- const char *b_props, const char *b_file, int b_start, int b_end) {
- if (!gb_variant_label(a_label) || !b_label || strcmp(a_label, b_label) != 0) {
+static void gb_span_vec_free(gb_span_vec_t *vec) {
+ if (vec) {
+ cbm_free(CBM_MEM_CLASS_GBUF_INDEX, vec->items);
+ cbm_free(CBM_MEM_CLASS_GBUF_INDEX, vec);
+ }
+}
+
+/* A node leaves the QN index: its gathered spans go with it, so a later node
+ * of the same QN starts from its own. */
+static void gb_drop_variant_spans(cbm_gbuf_t *gb, const char *qn) {
+ if (gb->variant_spans && qn) {
+ gb_span_vec_free(cbm_ht_delete(gb->variant_spans, qn));
+ }
+}
+
+/* Write a node's gathered spans into its properties: sorted, distinct, the
+ * list left out only when it would say no more than the node's own span. */
+static void gb_write_variants(cbm_gbuf_node_t *node, gb_span_vec_t *vec) {
+ qsort(vec->items, vec->count, sizeof(vec->items[0]), gb_span_cmp);
+ size_t unique = 0;
+ for (size_t i = 0; i < vec->count; i++) {
+ if (unique == 0 || gb_span_cmp(&vec->items[unique - SKIP_ONE], &vec->items[i]) != 0) {
+ vec->items[unique++] = vec->items[i];
+ }
+ }
+ gb_span_t own = {node->file_path ? node->file_path : "", node->start_line, node->end_line};
+ bool own_is_span = gb_variant_label(node->label) && node->start_line > 0;
+ bool only_own =
+ own_is_span ? unique == SKIP_ONE && gb_span_cmp(&vec->items[0], &own) == 0 : unique == 0;
+ if (only_own && !gb_props_have_variants(node->properties_json)) {
return;
}
- char *merged = gb_variants_props(survivor->properties_json, a_props, a_file, a_start, a_end,
- b_props, b_file, b_start, b_end);
- if (merged) {
- cbm_free(CBM_MEM_CLASS_GBUF_STRING, survivor->properties_json);
- survivor->properties_json = merged;
+ /* Numbers stay as written: a pass printed them (importance 0.000000). */
+ const char *props = node->properties_json;
+ yyjson_doc *doc = props ? yyjson_read_opts((char *)props, strlen(props),
+ YYJSON_READ_NUMBER_AS_RAW, &gb_yy_alc, NULL)
+ : NULL;
+ yyjson_mut_doc *mut = yyjson_mut_doc_new(&gb_yy_alc);
+ yyjson_mut_val *root = NULL;
+ if (mut) {
+ root = doc && yyjson_is_obj(yyjson_doc_get_root(doc))
+ ? yyjson_val_mut_copy(mut, yyjson_doc_get_root(doc))
+ : yyjson_mut_obj(mut);
+ }
+ yyjson_mut_val *list = root && !only_own ? yyjson_mut_arr(mut) : NULL;
+ for (size_t i = 0; list && i < unique; i++) {
+ yyjson_mut_val *v = yyjson_mut_arr_add_obj(mut, list);
+ yyjson_mut_obj_add_strcpy(mut, v, "file_path", vec->items[i].file);
+ yyjson_mut_obj_add_int(mut, v, "start_line", vec->items[i].start);
+ yyjson_mut_obj_add_int(mut, v, "end_line", vec->items[i].end);
+ }
+ if (root && (only_own || list)) {
+ yyjson_mut_obj_remove_key(root, "variants");
+ if (list) {
+ yyjson_mut_obj_add_val(mut, root, "variants", list);
+ }
+ yyjson_mut_doc_set_root(mut, root);
+ char *out = yyjson_mut_write_opts(mut, 0, &gb_yy_alc, NULL, NULL);
+ if (out) {
+ cbm_free(CBM_MEM_CLASS_GBUF_STRING, node->properties_json);
+ node->properties_json = out;
+ }
+ }
+ yyjson_mut_doc_free(mut);
+ yyjson_doc_free(doc);
+}
+
+static void gb_settle_one(const char *qn, void *value, void *ud) {
+ cbm_gbuf_t *gb = ud;
+ gb_span_vec_t *vec = value;
+ cbm_gbuf_node_t *node = gb->node_by_qn ? cbm_ht_get(gb->node_by_qn, qn) : NULL;
+ if (node && vec) {
+ gb_write_variants(node, vec);
+ }
+ gb_span_vec_free(vec);
+}
+
+static void gb_free_span_vec(const char *qn, void *value, void *ud) {
+ (void)qn;
+ (void)ud;
+ gb_span_vec_free(value);
+}
+
+void cbm_gbuf_settle_variants(cbm_gbuf_t *gb) {
+ if (!gb || !gb->variant_spans) {
+ return;
}
+ cbm_ht_foreach(gb->variant_spans, gb_settle_one, gb);
+ cbm_ht_free(gb->variant_spans);
+ gb->variant_spans = NULL;
}
/* ── Node operations ─────────────────────────────────────────────── */
@@ -1066,20 +1215,13 @@ int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name
if (c == 0) {
c = strcmp(existing->label ? existing->label : "", label ? label : "");
}
+ gb_def_t arrival = {label, properties_json, file_path, start_line, end_line};
+ gb_note_collision(gb, existing, &arrival, NULL);
if (c > 0) {
- gb_record_variants(existing, existing->label, existing->properties_json,
- existing->file_path, existing->start_line, existing->end_line, label,
- properties_json, file_path, start_line, end_line);
return existing->id; /* existing entity is the canonical winner */
}
- /* The arrival wins: its properties, plus every variant span both carry. */
- char *variant_props = NULL;
- if (gb_variant_label(label) && existing->label && strcmp(existing->label, label) == 0) {
- variant_props =
- gb_variants_props(properties_json, existing->properties_json, existing->file_path,
- existing->start_line, existing->end_line, properties_json,
- file_path, start_line, end_line);
- }
+ /* The arrival wins: its properties; the variant spans both carry are
+ * written on export (cbm_gbuf_settle_variants). */
/* Update in-place. name/properties are strdup'd BEFORE freeing old ones
* (callers may pass existing->name as an argument). label/file_path are
* interned: gb_intern returns a stable pool pointer (idempotent even when
@@ -1089,9 +1231,7 @@ int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name
* label/name — cbm_gbuf_find_by_label/name then missed or mis-listed it,
* which is how the flickering Function set reached the semantic pass). */
char *new_name = heap_strdup(name);
- char *new_props = variant_props ? variant_props
- : properties_json ? heap_strdup(properties_json)
- : NULL;
+ char *new_props = properties_json ? heap_strdup(properties_json) : NULL;
const char *new_label_interned = gb_intern(gb, label);
bool label_changed = !existing->label || !new_label_interned ||
strcmp(existing->label, new_label_interned) != 0;
@@ -1236,6 +1376,7 @@ int cbm_gbuf_delete_by_label(cbm_gbuf_t *gb, const char *label) {
intptr_to_ptr(SKIP_ONE));
/* Remove from primary indexes */
+ gb_drop_variant_spans(gb, n->qualified_name);
cbm_ht_delete(gb->node_by_qn, n->qualified_name);
if (n->id >= 0 && n->id < gb->by_id_cap) {
gb->by_id[n->id] = NULL;
@@ -1283,6 +1424,7 @@ int cbm_gbuf_delete_by_file(cbm_gbuf_t *gb, const char *file_path) {
remove_node_from_ptr_array(cbm_ht_get(gb->nodes_by_name, n->name), n->id);
/* Remove from primary indexes */
+ gb_drop_variant_spans(gb, n->qualified_name);
cbm_ht_delete(gb->node_by_qn, n->qualified_name);
if (n->id >= 0 && n->id < gb->by_id_cap) {
gb->by_id[n->id] = NULL;
@@ -1646,7 +1788,8 @@ static void free_remap_entry(const char *key, void *val, void *ud) {
/* Handle QN collision: update dst node fields (src wins), record remap if IDs differ.
* label/file_path are re-interned into dst's pool (sn's pointers belong to src). */
static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
- const cbm_gbuf_node_t *sn, CBMHashTable **remap) {
+ const cbm_gbuf_node_t *sn, const gb_span_vec_t *sn_spans,
+ CBMHashTable **remap) {
/* Same guard as cbm_gbuf_upsert_node: a per-file "Module" def coming from a
* worker-local gbuf must not touch the structural directory node ("Project"
* root or "Folder") that shares its QN in a directory-based-module language
@@ -1685,22 +1828,12 @@ static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
c = strcmp(existing->label ? existing->label : "", sn->label ? sn->label : "");
}
bool sn_wins = c <= 0;
- if (!sn_wins) {
- gb_record_variants(existing, existing->label, existing->properties_json,
- existing->file_path, existing->start_line, existing->end_line,
- sn->label, sn->properties_json, sn->file_path, sn->start_line,
- sn->end_line);
- }
+ gb_def_t src_def = {sn->label, sn->properties_json, sn->file_path, sn->start_line,
+ sn->end_line};
+ gb_note_collision(dst, existing, &src_def, sn_spans);
if (sn_wins) {
- /* src's properties, plus every variant span both carry. */
- char *variant_props = NULL;
- if (gb_variant_label(sn->label) && existing->label &&
- strcmp(existing->label, sn->label) == 0) {
- variant_props = gb_variants_props(sn->properties_json, existing->properties_json,
- existing->file_path, existing->start_line,
- existing->end_line, sn->properties_json,
- sn->file_path, sn->start_line, sn->end_line);
- }
+ /* src's properties; the variant spans both carry are written on
+ * export (cbm_gbuf_settle_variants). */
/* Keep the secondary indexes consistent when the surviving
* label/name changes (the old code left the node listed under its
* original label/name, so find_by_label/name mis-listed it). */
@@ -1725,10 +1858,9 @@ static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
existing->file_path = (char *)gb_intern(dst, sn->file_path);
existing->start_line = sn->start_line;
existing->end_line = sn->end_line;
- if (variant_props || sn->properties_json) {
+ if (sn->properties_json) {
cbm_free(CBM_MEM_CLASS_GBUF_STRING, existing->properties_json);
- existing->properties_json =
- variant_props ? variant_props : heap_strdup(sn->properties_json);
+ existing->properties_json = heap_strdup(sn->properties_json);
}
if (label_changed) {
node_ptr_array_t *by_label = get_or_create_node_array(
@@ -1829,11 +1961,16 @@ int cbm_gbuf_merge(cbm_gbuf_t *dst, cbm_gbuf_t *src) {
continue;
}
+ /* The spans src gathered for this node travel with it; they are
+ * written once, when dst is exported. */
+ const gb_span_vec_t *spans =
+ src->variant_spans ? cbm_ht_get(src->variant_spans, sn->qualified_name) : NULL;
cbm_gbuf_node_t *existing = cbm_ht_get(dst->node_by_qn, sn->qualified_name);
if (existing) {
- merge_update_existing(dst, existing, sn, &remap);
+ merge_update_existing(dst, existing, sn, spans, &remap);
} else {
merge_copy_new_node(dst, sn);
+ gb_adopt_spans(dst, cbm_ht_get(dst->node_by_qn, sn->qualified_name), spans);
}
}
@@ -2122,6 +2259,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
if (!gb || !path) {
return CBM_NOT_FOUND;
}
+ cbm_gbuf_settle_variants(gb);
CBM_PROF_START(t_count);
int live_count = count_live_nodes(gb);
@@ -2239,6 +2377,7 @@ int cbm_gbuf_flush_to_store(cbm_gbuf_t *gb, cbm_store_t *store) {
if (!gb || !store) {
return CBM_NOT_FOUND;
}
+ cbm_gbuf_settle_variants(gb);
/* Upsert project */
cbm_store_upsert_project(store, gb->project, gb->root_path);
@@ -2313,6 +2452,7 @@ int cbm_gbuf_merge_into_store(cbm_gbuf_t *gb, cbm_store_t *store) {
if (!gb || !store) {
return CBM_NOT_FOUND;
}
+ cbm_gbuf_settle_variants(gb);
/* Begin bulk mode — no project wipe */
cbm_store_begin(store);
diff --git a/src/graph_buffer/graph_buffer.h b/src/graph_buffer/graph_buffer.h
index ffbff0469..a0783261d 100644
--- a/src/graph_buffer/graph_buffer.h
+++ b/src/graph_buffer/graph_buffer.h
@@ -188,4 +188,10 @@ int cbm_gbuf_merge_into_store(cbm_gbuf_t *gb, cbm_store_t *store);
* (the old value is kept). */
int cbm_gbuf_node_set_properties_json(cbm_gbuf_node_t *node, const char *json);
+/* Write the definition spans gathered from same-QN collisions into each
+ * surviving node's `variants` property. The dump, flush and merge entry
+ * points do it themselves; call it directly only to read a node's
+ * properties_json from the buffer after collisions. */
+void cbm_gbuf_settle_variants(cbm_gbuf_t *gb);
+
#endif /* CBM_GRAPH_BUFFER_H */
diff --git a/src/pipeline/pipeline_delta.c b/src/pipeline/pipeline_delta.c
index e7f5ee40a..e6dd7cc2e 100644
--- a/src/pipeline/pipeline_delta.c
+++ b/src/pipeline/pipeline_delta.c
@@ -596,6 +596,9 @@ int cbm_delta_patch(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf, i
if (cbm_store_begin(store) != CBM_STORE_OK) {
return CBM_NOT_FOUND;
}
+ /* This writes the buffer's nodes itself: settle the collision spans first,
+ * as the buffer's own dump and flush do. */
+ cbm_gbuf_settle_variants(gbuf);
delta_patch_ctx_t ctx = {.db = db, .project = project, .max_db_id = max_db_id};
if (sqlite3_prepare_v2(db,
"SELECT id FROM nodes WHERE project = ?1"
diff --git a/tests/test_graph_buffer.c b/tests/test_graph_buffer.c
index dd257f187..b57378691 100644
--- a/tests/test_graph_buffer.c
+++ b/tests/test_graph_buffer.c
@@ -569,6 +569,7 @@ TEST(gbuf_upsert_same_qn_updates_all_fields) {
ASSERT_EQ(id1, id2);
ASSERT_EQ(cbm_gbuf_node_count(gb), 1);
+ cbm_gbuf_settle_variants(gb);
const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(gb, "pkg.fn");
ASSERT_NOT_NULL(n);
ASSERT_STR_EQ(n->label, "Method");
@@ -576,7 +577,11 @@ TEST(gbuf_upsert_same_qn_updates_all_fields) {
ASSERT_STR_EQ(n->file_path, "new.go");
ASSERT_EQ(n->start_line, 20);
ASSERT_EQ(n->end_line, 30);
- ASSERT_STR_EQ(n->properties_json, "{\"k\":\"v2\"}");
+ /* the winner's own properties, plus both definitions' spans: whichever
+ * label survives, a change in either body lands on this node */
+ ASSERT_STR_EQ(n->properties_json,
+ "{\"k\":\"v2\",\"variants\":[{\"file_path\":\"new.go\",\"start_line\":20,"
+ "\"end_line\":30},{\"file_path\":\"old.go\",\"start_line\":1,\"end_line\":10}]}");
cbm_gbuf_free(gb);
PASS();
@@ -647,6 +652,146 @@ TEST(gbuf_merge_definition_beats_same_qn_macro_c1) {
PASS();
}
+/* deckhouse pkg/log: a package function and three methods share the QN
+ * `pkg.log.Error` (Go method QNs carry no receiver). Every definition that
+ * collapsed into the node is a span a change can land in, so `variants`
+ * holds all four -- in every arrival order. A Function arriving between the
+ * methods used to replace the node's list, and logger.go's span came and went
+ * with worker scheduling. */
+TEST(gbuf_variants_cross_label_order_independent) {
+ static const struct {
+ const char *label;
+ const char *file;
+ int start;
+ int end;
+ } defs[] = {{"Method", "pkg/log/logger.go", 227, 232},
+ {"Function", "pkg/log/global.go", 83, 85},
+ {"Method", "pkg/log/fields_test.go", 76, 78},
+ {"Method", "pkg/log/fields_test.go", 85, 87}};
+ static const int perms[][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 2, 1, 3}, {3, 2, 1, 0},
+ {1, 3, 0, 2}, {2, 0, 3, 1}, {3, 1, 2, 0}, {0, 3, 1, 2}};
+ char first[1024] = "";
+ for (size_t p = 0; p < sizeof(perms) / sizeof(perms[0]); p++) {
+ cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp");
+ for (int i = 0; i < 4; i++) {
+ int d = perms[p][i];
+ cbm_gbuf_upsert_node(gb, defs[d].label, "Error", "p.pkg.log.Error", defs[d].file,
+ defs[d].start, defs[d].end, "{}");
+ }
+ cbm_gbuf_settle_variants(gb);
+ const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(gb, "p.pkg.log.Error");
+ ASSERT_NOT_NULL(n);
+ ASSERT_NOT_NULL(n->properties_json);
+ ASSERT_NOT_NULL(
+ strstr(n->properties_json,
+ "{\"file_path\":\"pkg/log/logger.go\",\"start_line\":227,\"end_line\":232}"));
+ ASSERT_NOT_NULL(
+ strstr(n->properties_json,
+ "{\"file_path\":\"pkg/log/global.go\",\"start_line\":83,\"end_line\":85}"));
+ if (p == 0) {
+ snprintf(first, sizeof(first), "%s", n->properties_json);
+ } else {
+ ASSERT_STR_EQ(n->properties_json, first);
+ }
+ cbm_gbuf_free(gb);
+ }
+ PASS();
+}
+
+/* No count decides which spans a node keeps: 100 same-QN definitions (generated
+ * protobuf `String()` methods) keep all 100, whichever order they arrive in. */
+TEST(gbuf_variants_keep_every_span) {
+ enum { N_DEFS = 100 };
+ char *props[2] = {NULL, NULL};
+ for (int order = 0; order < 2; order++) {
+ cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp");
+ for (int i = 0; i < N_DEFS; i++) {
+ int k = order == 0 ? i : N_DEFS - 1 - i;
+ char file[32];
+ snprintf(file, sizeof(file), "pb/m%03d.pb.go", k);
+ cbm_gbuf_upsert_node(gb, "Method", "String", "p.pb.String", file, 10, 12, "{}");
+ }
+ cbm_gbuf_settle_variants(gb);
+ const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(gb, "p.pb.String");
+ ASSERT_NOT_NULL(n);
+ ASSERT_NOT_NULL(n->properties_json);
+ int spans = 0;
+ for (const char *s = n->properties_json; (s = strstr(s, "\"file_path\"")) != NULL; s++) {
+ spans++;
+ }
+ ASSERT_EQ(spans, N_DEFS);
+ props[order] = strdup(n->properties_json);
+ cbm_gbuf_free(gb);
+ }
+ ASSERT_STR_EQ(props[0], props[1]);
+ free(props[0]);
+ free(props[1]);
+ PASS();
+}
+
+/* Spans gathered in worker buffers travel with their nodes into the main
+ * buffer, whichever worker merges first. */
+TEST(gbuf_variants_travel_through_worker_merge) {
+ char first[512] = "";
+ for (int order = 0; order < 2; order++) {
+ cbm_gbuf_t *main_gb = cbm_gbuf_new("test", "/tmp");
+ cbm_gbuf_t *w1 = cbm_gbuf_new("test", "/tmp");
+ cbm_gbuf_t *w2 = cbm_gbuf_new("test", "/tmp");
+ cbm_gbuf_upsert_node(w1, "Method", "X", "p.X", "a.go", 10, 12, "{}");
+ cbm_gbuf_upsert_node(w1, "Method", "X", "p.X", "b.go", 5, 6, "{}");
+ cbm_gbuf_upsert_node(w2, "Method", "X", "p.X", "c.go", 1, 2, "{}");
+ cbm_gbuf_upsert_node(w2, "Function", "X", "p.X", "d.go", 3, 4, "{}");
+ ASSERT_EQ(cbm_gbuf_merge(main_gb, order == 0 ? w1 : w2), 0);
+ ASSERT_EQ(cbm_gbuf_merge(main_gb, order == 0 ? w2 : w1), 0);
+ cbm_gbuf_settle_variants(main_gb);
+ const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(main_gb, "p.X");
+ ASSERT_NOT_NULL(n);
+ ASSERT_STR_EQ(n->properties_json,
+ "{\"variants\":[{\"file_path\":\"a.go\",\"start_line\":10,\"end_line\":12},"
+ "{\"file_path\":\"b.go\",\"start_line\":5,\"end_line\":6},"
+ "{\"file_path\":\"c.go\",\"start_line\":1,\"end_line\":2},"
+ "{\"file_path\":\"d.go\",\"start_line\":3,\"end_line\":4}]}");
+ if (order == 0) {
+ snprintf(first, sizeof(first), "%s", n->properties_json);
+ } else {
+ ASSERT_STR_EQ(n->properties_json, first);
+ }
+ cbm_gbuf_free(w1);
+ cbm_gbuf_free(w2);
+ cbm_gbuf_free(main_gb);
+ }
+ PASS();
+}
+
+/* The list is written once, on export: the properties come out the same
+ * whether the collision happened in the main buffer or in a worker's, after
+ * a later pass appended a key with its own number text. */
+TEST(gbuf_variants_written_once_at_export) {
+ char props[2][512];
+ for (int via_worker = 0; via_worker < 2; via_worker++) {
+ cbm_gbuf_t *main_gb = cbm_gbuf_new("test", "/tmp");
+ cbm_gbuf_t *w = via_worker ? cbm_gbuf_new("test", "/tmp") : main_gb;
+ cbm_gbuf_upsert_node(w, "Method", "X", "p.X", "a.go", 10, 12, "{\"k\":1}");
+ cbm_gbuf_upsert_node(w, "Method", "X", "p.X", "b.go", 5, 6, "{\"k\":1}");
+ if (via_worker) {
+ ASSERT_EQ(cbm_gbuf_merge(main_gb, w), 0);
+ cbm_gbuf_free(w);
+ }
+ cbm_gbuf_node_t *n = (cbm_gbuf_node_t *)cbm_gbuf_find_by_qn(main_gb, "p.X");
+ ASSERT_NOT_NULL(n);
+ char buf[512];
+ snprintf(buf, sizeof(buf), "%.*s,\"importance\":0.000000}",
+ (int)strlen(n->properties_json) - 1, n->properties_json);
+ ASSERT_EQ(cbm_gbuf_node_set_properties_json(n, buf), 0);
+ cbm_gbuf_settle_variants(main_gb);
+ snprintf(props[via_worker], sizeof(props[0]), "%s", n->properties_json);
+ cbm_gbuf_free(main_gb);
+ }
+ ASSERT_STR_EQ(props[0], props[1]);
+ ASSERT_NOT_NULL(strstr(props[0], "\"importance\":0.000000,\"variants\":["));
+ PASS();
+}
+
TEST(gbuf_upsert_long_qn) {
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp");
@@ -913,13 +1058,17 @@ TEST(gbuf_merge_overlapping_qns) {
ASSERT_EQ(cbm_gbuf_node_count(dst), 3);
/* Verify src fields won for the overlapping QN */
+ cbm_gbuf_settle_variants(dst);
const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(dst, "pkg.fn");
ASSERT_NOT_NULL(n);
ASSERT_STR_EQ(n->label, "Method");
ASSERT_STR_EQ(n->name, "fn_new");
ASSERT_STR_EQ(n->file_path, "new.go");
ASSERT_EQ(n->start_line, 20);
- ASSERT_STR_EQ(n->properties_json, "{\"from\":\"src\"}");
+ /* src's properties, plus both definitions' spans (same rule as upsert) */
+ ASSERT_STR_EQ(n->properties_json,
+ "{\"from\":\"src\",\"variants\":[{\"file_path\":\"new.go\",\"start_line\":20,"
+ "\"end_line\":30},{\"file_path\":\"old.go\",\"start_line\":1,\"end_line\":10}]}");
/* Both unique nodes present */
ASSERT_NOT_NULL(cbm_gbuf_find_by_qn(dst, "pkg.unique_dst"));
@@ -1317,6 +1466,10 @@ SUITE(graph_buffer) {
RUN_TEST(gbuf_upsert_same_qn_updates_all_fields);
RUN_TEST(gbuf_upsert_definition_beats_same_qn_macro_c1);
RUN_TEST(gbuf_merge_definition_beats_same_qn_macro_c1);
+ RUN_TEST(gbuf_variants_cross_label_order_independent);
+ RUN_TEST(gbuf_variants_keep_every_span);
+ RUN_TEST(gbuf_variants_travel_through_worker_merge);
+ RUN_TEST(gbuf_variants_written_once_at_export);
RUN_TEST(gbuf_upsert_long_qn);
RUN_TEST(gbuf_find_by_qn_missing);
RUN_TEST(gbuf_find_by_id_missing);