@@ -49,8 +49,11 @@ static inline void *intptr_to_ptr(intptr_t v) {
4949
5050/* ── Internal types ──────────────────────────────────────────────── */
5151
52- /* Edge key for dedup hash table — composite key as string "srcID:tgtID:type" */
53- #define EDGE_KEY_BUF CBM_SZ_128
52+ /* Edge key for dedup hash table — composite key as string "srcID:tgtID:type",
53+ * plus ":local_name" for IMPORTS edges (#768). 256 bytes fit two int64s, the
54+ * type and a ~200-char local_name verbatim; longer local_names are re-keyed
55+ * with a hash of the full name in make_edge_key (never silently truncated). */
56+ #define EDGE_KEY_BUF CBM_SZ_256
5457
5558/* Per-type or per-key edge list stored in hash tables as values */
5659typedef CBM_DYN_ARRAY (const cbm_gbuf_edge_t * ) edge_ptr_array_t ;
@@ -135,7 +138,52 @@ static void make_id_key(char *buf, size_t bufsz, int64_t id) {
135138 snprintf (buf , bufsz , "%lld" , (long long )id );
136139}
137140
138- static void make_edge_key (char * buf , size_t bufsz , int64_t src , int64_t tgt , const char * type ) {
141+ /* FNV-1a 64-bit over a byte slice — for re-keying oversized local_names. */
142+ static uint64_t fnv1a64 (const char * s , size_t len ) {
143+ uint64_t h = 14695981039346656037ULL ;
144+ for (size_t i = 0 ; i < len ; i ++ ) {
145+ h ^= (uint8_t )s [i ];
146+ h *= 1099511628211ULL ;
147+ }
148+ return h ;
149+ }
150+
151+ /* IMPORTS edges carry exactly one imported symbol's local_name (#768): two
152+ * named imports from the same specifier resolve to the same (source,
153+ * target) pair but are distinct symbols. Key on local_name too so the
154+ * second import doesn't dedup-collide with and overwrite the first —
155+ * every pass that walks IMPORTS edges (pass_calls.c, pass_usages.c,
156+ * pass_semantic.c, pass_lsp_cross.c) expects one local_name per edge, so
157+ * losing an edge here silently breaks cross-file call resolution for
158+ * whichever symbol got dropped, not just "who imports X" queries. Other
159+ * edge types keep the plain (source,target,type) key: collapsing repeat
160+ * edges of the same type between the same two nodes (e.g. multiple call
161+ * sites) into one is the existing, intended dedup behavior there.
162+ *
163+ * A local_name too long for the key buffer is re-keyed with an FNV-1a hash
164+ * of the FULL name instead of being truncated — a truncated key would
165+ * collide two long names sharing a prefix and silently drop an edge again.
166+ * The hash key is prefixed with byte 0x01, which cannot appear in the raw
167+ * JSON slice (control characters must be \u-escaped in JSON), so hash keys
168+ * can never collide with verbatim keys. */
169+ static void make_edge_key (char * buf , size_t bufsz , int64_t src , int64_t tgt , const char * type ,
170+ const char * properties_json ) {
171+ if (properties_json && strcmp (type , "IMPORTS" ) == 0 ) {
172+ static const char local_name_key [] = "\"local_name\":\"" ;
173+ const char * ln = strstr (properties_json , local_name_key );
174+ if (ln ) {
175+ ln += sizeof (local_name_key ) - 1 ;
176+ const char * end = strchr (ln , '"' );
177+ size_t ln_len = end ? (size_t )(end - ln ) : strlen (ln );
178+ int n = snprintf (buf , bufsz , "%lld:%lld:%s:%.*s" , (long long )src , (long long )tgt , type ,
179+ (int )ln_len , ln );
180+ if (n < 0 || (size_t )n >= bufsz ) {
181+ snprintf (buf , bufsz , "%lld:%lld:%s:\x01%016llx" , (long long )src , (long long )tgt ,
182+ type , (unsigned long long )fnv1a64 (ln , ln_len ));
183+ }
184+ return ;
185+ }
186+ }
139187 snprintf (buf , bufsz , "%lld:%lld:%s" , (long long )src , (long long )tgt , type );
140188}
141189
@@ -244,7 +292,7 @@ static void remove_node_from_ptr_array(node_ptr_array_t *arr, int64_t node_id) {
244292static void unindex_edge (cbm_gbuf_t * gb , const cbm_gbuf_edge_t * e ) {
245293 char key [EDGE_KEY_BUF ];
246294
247- make_edge_key (key , sizeof (key ), e -> source_id , e -> target_id , e -> type );
295+ make_edge_key (key , sizeof (key ), e -> source_id , e -> target_id , e -> type , e -> properties_json );
248296 const char * ekey = cbm_ht_get_key (gb -> edge_by_key , key );
249297 cbm_ht_delete (gb -> edge_by_key , key );
250298 free ((void * )ekey );
@@ -919,7 +967,7 @@ int64_t cbm_gbuf_insert_edge(cbm_gbuf_t *gb, int64_t source_id, int64_t target_i
919967
920968 /* Check for dedup */
921969 char key [EDGE_KEY_BUF ];
922- make_edge_key (key , sizeof (key ), source_id , target_id , type );
970+ make_edge_key (key , sizeof (key ), source_id , target_id , type , properties_json );
923971
924972 cbm_gbuf_edge_t * existing = cbm_ht_get (gb -> edge_by_key , key );
925973 if (existing ) {
@@ -1032,7 +1080,8 @@ int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type) {
10321080 cbm_gbuf_edge_t * e = gb -> edges .items [i ];
10331081 if (strcmp (e -> type , type ) == 0 ) {
10341082 char key [EDGE_KEY_BUF ];
1035- make_edge_key (key , sizeof (key ), e -> source_id , e -> target_id , e -> type );
1083+ make_edge_key (key , sizeof (key ), e -> source_id , e -> target_id , e -> type ,
1084+ e -> properties_json );
10361085 const char * ekey = cbm_ht_get_key (gb -> edge_by_key , key );
10371086 cbm_ht_delete (gb -> edge_by_key , key );
10381087 free ((void * )ekey );
@@ -1182,23 +1231,24 @@ int cbm_gbuf_merge(cbm_gbuf_t *dst, cbm_gbuf_t *src) {
11821231
11831232/* ── Dump / Flush ────────────────────────────────────────────────── */
11841233
1185- /* Extract url_path value from a properties JSON string.
1234+ /* Extract a string property from a properties JSON string.
11861235 * Returns heap-allocated string or NULL. Caller must free.
1187- * Parses real JSON: the dump writer feeds this value into idx_edges_url_path,
1188- * whose backing column is GENERATED AS json_extract(properties,'$.url_path ').
1236+ * Parses real JSON: the dump writer feeds these values into indexes whose
1237+ * backing columns are GENERATED AS json_extract(properties,'$.<key> ').
11891238 * Naive byte slicing returned the ESCAPED text (and cut at embedded \\")
11901239 * while json_extract yields the unescaped value — the mismatch left rows
1191- * "missing from index idx_edges_url_path" under PRAGMA integrity_check. */
1192- static char * extract_url_path (const char * props ) {
1193- if (!props || !strstr (props , "\"url_path\"" )) {
1240+ * "missing from index idx_edges_url_path" under PRAGMA integrity_check.
1241+ * key_quoted ("\"key\"") is a fast pre-filter to skip the JSON parse. */
1242+ static char * extract_prop_string (const char * props , const char * key_quoted , const char * key ) {
1243+ if (!props || !strstr (props , key_quoted )) {
11941244 return NULL ;
11951245 }
11961246 yyjson_doc * doc = yyjson_read (props , strlen (props ), 0 );
11971247 if (!doc ) {
11981248 return NULL ;
11991249 }
12001250 char * out = NULL ;
1201- yyjson_val * v = yyjson_obj_get (yyjson_doc_get_root (doc ), "url_path" );
1251+ yyjson_val * v = yyjson_obj_get (yyjson_doc_get_root (doc ), key );
12021252 if (v && yyjson_is_str (v )) {
12031253 const char * sv = yyjson_get_str (v );
12041254 out = cbm_strndup (sv , strlen (sv ));
@@ -1207,6 +1257,16 @@ static char *extract_url_path(const char *props) {
12071257 return out ;
12081258}
12091259
1260+ static char * extract_url_path (const char * props ) {
1261+ return extract_prop_string (props , "\"url_path\"" , "url_path" );
1262+ }
1263+
1264+ /* local_name feeds the hand-built sqlite_autoindex_edges_1 — its backing
1265+ * column local_name_gen is GENERATED only for IMPORTS edges (#768). */
1266+ static char * extract_local_name (const char * props ) {
1267+ return extract_prop_string (props , "\"local_name\"" , "local_name" );
1268+ }
1269+
12101270/* Remap a temp edge ID to its final sequential ID, or 0 if out of range. */
12111271static int64_t remap_id (const int64_t * temp_to_final , int64_t max_temp_id , int64_t temp_id ) {
12121272 return (temp_id < max_temp_id ) ? temp_to_final [temp_id ] : 0 ;
@@ -1265,9 +1325,11 @@ static CBMDumpNode *build_dump_nodes(cbm_gbuf_t *gb, int live_count, int64_t *te
12651325 return dump_nodes ;
12661326}
12671327
1268- /* Build dump-ready edge array with remapped IDs. Returns url_paths via out param. */
1328+ /* Build dump-ready edge array with remapped IDs. Returns url_paths and
1329+ * local_names (heap string arrays owned by the caller) via out params. */
12691330static CBMDumpEdge * build_dump_edges (cbm_gbuf_t * gb , const int64_t * temp_to_final ,
1270- int64_t max_temp_id , int * out_count , char * * * out_url_paths ) {
1331+ int64_t max_temp_id , int * out_count , char * * * out_url_paths ,
1332+ char * * * out_local_names ) {
12711333 /* Count valid edges (both endpoints resolved) */
12721334 int valid_edges = 0 ;
12731335 for (int i = 0 ; i < gb -> edges .count ; i ++ ) {
@@ -1281,6 +1343,7 @@ static CBMDumpEdge *build_dump_edges(cbm_gbuf_t *gb, const int64_t *temp_to_fina
12811343 CBMDumpEdge * dump_edges =
12821344 malloc ((size_t )(valid_edges > 0 ? valid_edges : SKIP_ONE ) * sizeof (CBMDumpEdge ));
12831345 char * * url_paths = calloc ((size_t )(valid_edges > 0 ? valid_edges : SKIP_ONE ), sizeof (char * ));
1346+ char * * local_names = calloc ((size_t )(valid_edges > 0 ? valid_edges : SKIP_ONE ), sizeof (char * ));
12841347 int idx = 0 ;
12851348
12861349 for (int i = 0 ; i < gb -> edges .count ; i ++ ) {
@@ -1294,6 +1357,12 @@ static CBMDumpEdge *build_dump_edges(cbm_gbuf_t *gb, const int64_t *temp_to_fina
12941357 char * url_path = extract_url_path (e -> properties_json );
12951358 url_paths [idx ] = url_path ;
12961359
1360+ /* IMPORTS only — mirrors the local_name_gen CASE in the edges DDL. */
1361+ char * local_name = (e -> type && strcmp (e -> type , "IMPORTS" ) == 0 )
1362+ ? extract_local_name (e -> properties_json )
1363+ : NULL ;
1364+ local_names [idx ] = local_name ;
1365+
12971366 const char * props = e -> properties_json ? e -> properties_json : "{}" ;
12981367 dump_edges [idx ] = (CBMDumpEdge ){
12991368 .id = idx + SKIP_ONE ,
@@ -1303,12 +1372,14 @@ static CBMDumpEdge *build_dump_edges(cbm_gbuf_t *gb, const int64_t *temp_to_fina
13031372 .type = e -> type ,
13041373 .properties = props ,
13051374 .url_path = url_path ? url_path : "" ,
1375+ .local_name = local_name ? local_name : "" ,
13061376 };
13071377 idx ++ ;
13081378 }
13091379
13101380 * out_count = idx ;
13111381 * out_url_paths = url_paths ;
1382+ * out_local_names = local_names ;
13121383 return dump_edges ;
13131384}
13141385
@@ -1360,12 +1431,15 @@ static void log_dump_summary(int node_count, int edge_count) {
13601431 cbm_log_info ("gbuf.dump" , "nodes" , b1 , "edges" , b2 );
13611432}
13621433
1363- static void free_dump_resources (char * * url_paths , int edge_count , CBMDumpEdge * dump_edges ,
1364- CBMDumpNode * dump_nodes , int64_t * temp_to_final ) {
1434+ static void free_dump_resources (char * * url_paths , char * * local_names , int edge_count ,
1435+ CBMDumpEdge * dump_edges , CBMDumpNode * dump_nodes ,
1436+ int64_t * temp_to_final ) {
13651437 for (int i = 0 ; i < edge_count ; i ++ ) {
13661438 free (url_paths [i ]);
1439+ free (local_names [i ]);
13671440 }
13681441 free (url_paths );
1442+ free (local_names );
13691443 free (dump_edges );
13701444 free (dump_nodes );
13711445 free (temp_to_final );
@@ -1469,10 +1543,12 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
14691543
14701544 int edge_idx = 0 ;
14711545 char * * url_paths = NULL ;
1546+ char * * local_names = NULL ;
14721547 CBMDumpEdge * dump_edges = NULL ;
14731548 if (rc == 0 ) {
14741549 CBM_PROF_START (t_build_edges );
1475- dump_edges = build_dump_edges (gb , temp_to_final , max_temp_id , & edge_idx , & url_paths );
1550+ dump_edges =
1551+ build_dump_edges (gb , temp_to_final , max_temp_id , & edge_idx , & url_paths , & local_names );
14761552 CBM_PROF_END_N ("dump" , "3_build_dump_edges" , t_build_edges , edge_idx );
14771553 release_and_remap_vectors (gb , temp_to_final , max_temp_id );
14781554 }
@@ -1489,7 +1565,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
14891565 }
14901566
14911567 log_dump_summary (node_idx , edge_idx );
1492- free_dump_resources (url_paths , edge_idx , dump_edges , dump_nodes , temp_to_final );
1568+ free_dump_resources (url_paths , local_names , edge_idx , dump_edges , dump_nodes , temp_to_final );
14931569 free (src_nodes );
14941570 return rc ;
14951571}
0 commit comments