MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / merge_update_existing

Function merge_update_existing

src/graph_buffer/graph_buffer.c:2036–2134  ·  view source on GitHub ↗

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). */

Source from the content-addressed store, hash-verified

2034/* Handle QN collision: update dst node fields (src wins), record remap if IDs differ.
2035 * label/file_path are re-interned into dst's pool (sn's pointers belong to src). */
2036static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
2037 const cbm_gbuf_node_t *sn, const gb_span_vec_t *sn_spans,
2038 CBMHashTable **remap) {
2039 /* Same guard as cbm_gbuf_upsert_node: a per-file "Module" def coming from a
2040 * worker-local gbuf must not touch the structural directory node ("Project"
2041 * root or "Folder") that shares its QN in a directory-based-module language
2042 * (Java/Go). pass_structure seeds Folder/Project nodes on the MAIN gbuf
2043 * before the parallel extract, so every worker's always-emitted Module def
2044 * for that package collides here; unconditional "src wins" relabelled the
2045 * directory node to Module and set its file_path to whichever worker merged
2046 * LAST — the nondeterministic USAGE-source misattribution of #787. Keep the
2047 * structural node intact; the ID remap below still redirects the worker's
2048 * edges onto the canonical node. */
2049 bool module_on_container =
2050 existing->label && sn->label && strcmp(sn->label, "Module") == 0 &&
2051 (strcmp(existing->label, "Project") == 0 || strcmp(existing->label, "Folder") == 0);
2052 if (!module_on_container) {
2053 /* Canonical collision winner (determinism) — mirrors
2054 * cbm_gbuf_upsert_node exactly. Distinct source entities can share a
2055 * QN (C: struct/function/macro with one name); unconditional "src
2056 * wins" made the survivor depend on worker merge order, flickering
2057 * the node set (and every downstream consumer) run to run. Winner =
2058 * smallest file_path, then LARGEST start_line, then largest
2059 * name/label — one total order, commutative, scheduling-free; a full
2060 * tie is the same entity → refresh from src. A definition beats a
2061 * macro ahead of all of these (gb_qn_claim_rank). */
2062 int c = gb_qn_claim_rank(existing->label) - gb_qn_claim_rank(sn->label);
2063 if (c == 0) {
2064 c = strcmp(sn->file_path ? sn->file_path : "",
2065 existing->file_path ? existing->file_path : "");
2066 }
2067 if (c == 0) {
2068 c = existing->start_line - sn->start_line;
2069 }
2070 if (c == 0) {
2071 c = strcmp(existing->name ? existing->name : "", sn->name ? sn->name : "");
2072 }
2073 if (c == 0) {
2074 c = strcmp(existing->label ? existing->label : "", sn->label ? sn->label : "");
2075 }
2076 bool sn_wins = c <= 0;
2077 gb_def_t src_def = {sn->label, sn->properties_json, sn->file_path, sn->start_line,
2078 sn->end_line};
2079 gb_note_collision(dst, existing, &src_def, sn_spans);
2080 if (sn_wins) {
2081 /* src's properties; the variant spans both carry are written on
2082 * export (cbm_gbuf_settle_variants). */
2083 /* Keep the secondary indexes consistent when the surviving
2084 * label/name changes (the old code left the node listed under its
2085 * original label/name, so find_by_label/name mis-listed it). */
2086 const char *new_label = gb_intern(dst, sn->label);
2087 bool label_changed =
2088 !existing->label || !new_label || strcmp(existing->label, new_label) != 0;
2089 bool name_changed =
2090 !existing->name || !sn->name || strcmp(existing->name, sn->name) != 0;
2091 if (label_changed) {
2092 remove_node_from_ptr_array(
2093 cbm_ht_get(dst->nodes_by_label, existing->label ? existing->label : ""),

Callers 1

cbm_gbuf_mergeFunction · 0.85

Calls 14

gb_qn_claim_rankFunction · 0.85
gb_note_collisionFunction · 0.85
gb_internFunction · 0.85
cbm_ht_getFunction · 0.85
cbm_freeFunction · 0.85
get_or_create_node_arrayFunction · 0.85
node_array_pushFunction · 0.85
cbm_ht_createFunction · 0.85
make_id_keyFunction · 0.85
cbm_allocFunction · 0.85
cbm_ht_setFunction · 0.85

Tested by

no test coverage detected