@@ -958,6 +958,157 @@ TEST(gbuf_next_id_accessors) {
958958 PASS ();
959959}
960960
961+ TEST (gbuf_raise_next_id_never_lowers ) {
962+ cbm_gbuf_t * gb = cbm_gbuf_new ("proj" , "/" );
963+ cbm_gbuf_raise_next_id (gb , 100 );
964+ ASSERT_EQ (cbm_gbuf_next_id (gb ), 100 );
965+ cbm_gbuf_raise_next_id (gb , 40 );
966+ ASSERT_EQ (cbm_gbuf_next_id (gb ), 100 );
967+ int64_t id = cbm_gbuf_upsert_node (gb , "Function" , "foo" , "proj.foo" , "f.go" , 1 , 5 , "{}" );
968+ ASSERT_EQ (id , 100 );
969+ cbm_gbuf_free (gb );
970+ PASS ();
971+ }
972+
973+ /* Edge ids in the main buffer stay unique through the parallel phases.
974+ * Merged worker nodes keep their shared ids, and merged edges take fresh ids
975+ * from the main counter after the highest of them, so the main counter can
976+ * end above the shared one (12 of 17 infra corpora, deckhouse by 229,233).
977+ * The sync after each parallel phase used to set the main counter to the
978+ * shared value anyway, and later passes reused the merged edges' ids. Where
979+ * the counter ends depends on worker timing, so the seam pins it: right before
980+ * each sync the hook takes ID_SYNC_PUSH edge ids from the main buffer. */
981+ enum { ID_FILES = 64 , ID_SYNC_PUSH = 3 };
982+
983+ typedef struct {
984+ bool push ;
985+ int syncs ;
986+ } id_sync_probe_t ;
987+
988+ static void id_sync_hook (cbm_gbuf_t * gb , void * ctx ) {
989+ id_sync_probe_t * probe = ctx ;
990+ probe -> syncs ++ ;
991+ const cbm_gbuf_node_t * * fns = NULL ;
992+ int n = 0 ;
993+ if (!probe -> push || cbm_gbuf_find_by_label (gb , "Function" , & fns , & n ) != 0 || n < 2 ) {
994+ return ;
995+ }
996+ char props [CBM_SZ_64 ];
997+ for (int i = 0 ; i < ID_SYNC_PUSH ; i ++ ) {
998+ snprintf (props , sizeof (props ), "{\"id_sync_probe\":%d}" , probe -> syncs * ID_SYNC_PUSH + i );
999+ cbm_gbuf_insert_edge (gb , fns [0 ]-> id , fns [1 ]-> id , "CALLS" , props );
1000+ }
1001+ }
1002+
1003+ typedef struct {
1004+ int edges ;
1005+ int duplicates ;
1006+ } edge_id_probe_t ;
1007+
1008+ static void probe_edge_ids (const cbm_gbuf_t * gb , void * ctx ) {
1009+ edge_id_probe_t * probe = ctx ;
1010+ probe -> edges = cbm_gbuf_edge_count (gb );
1011+ probe -> duplicates = cbm_gbuf_duplicate_edge_ids (gb );
1012+ }
1013+
1014+ /* `count` Go files; each function calls the next file's. */
1015+ static bool write_id_fixture (const char * repo , int count ) {
1016+ char path [512 ];
1017+ char source [256 ];
1018+ for (int i = 0 ; i < count ; i ++ ) {
1019+ snprintf (path , sizeof (path ), "%s/f%02d.go" , repo , i );
1020+ snprintf (source , sizeof (source ),
1021+ "package main\n\nfunc F%02d() int {\n\treturn F%02d() + 1\n}\n" , i ,
1022+ (i + 1 ) % count );
1023+ if (th_write_file (path , source ) != 0 ) {
1024+ return false;
1025+ }
1026+ }
1027+ return true;
1028+ }
1029+
1030+ TEST (parallel_full_index_keeps_edge_ids_unique ) {
1031+ char repo [256 ];
1032+ snprintf (repo , sizeof (repo ), "/tmp/cbm_par_ids_XXXXXX" );
1033+ ASSERT_NOT_NULL (cbm_mkdtemp (repo ));
1034+ /* More files than MIN_FILES_FOR_PARALLEL (50): the parallel route. */
1035+ if (!write_id_fixture (repo , ID_FILES )) {
1036+ th_rmtree (repo );
1037+ FAIL ("failed to write the edge-id fixture" );
1038+ }
1039+ char db_path [512 ];
1040+ snprintf (db_path , sizeof (db_path ), "%s/ids.db" , repo );
1041+
1042+ char * old_workers = getenv ("CBM_WORKERS" );
1043+ char * saved_workers = old_workers ? strdup (old_workers ) : NULL ;
1044+ cbm_setenv ("CBM_WORKERS" , "4" , 1 );
1045+ id_sync_probe_t syncs = {.push = true};
1046+ edge_id_probe_t probe = {0 };
1047+ cbm_pipeline_set_id_sync_hook_for_tests (id_sync_hook , & syncs );
1048+ cbm_pipeline_t * p = cbm_pipeline_new (repo , db_path , CBM_MODE_FULL );
1049+ int rc = CBM_NOT_FOUND ;
1050+ if (p ) {
1051+ cbm_pipeline_set_before_dump_hook_for_tests (p , probe_edge_ids , & probe );
1052+ rc = cbm_pipeline_run (p );
1053+ cbm_pipeline_free (p );
1054+ }
1055+ cbm_pipeline_set_id_sync_hook_for_tests (NULL , NULL );
1056+ if (saved_workers ) {
1057+ cbm_setenv ("CBM_WORKERS" , saved_workers , 1 );
1058+ free (saved_workers );
1059+ } else {
1060+ cbm_unsetenv ("CBM_WORKERS" );
1061+ }
1062+ th_rmtree (repo );
1063+
1064+ ASSERT_EQ (rc , 0 );
1065+ /* Both full-route syncs (after extract, after resolve) go through the helper. */
1066+ ASSERT_EQ (syncs .syncs , 2 );
1067+ ASSERT_GT (probe .edges , ID_FILES );
1068+ ASSERT_EQ (probe .duplicates , 0 );
1069+ PASS ();
1070+ }
1071+
1072+ /* The closure-repair route always runs the parallel phases; both of its
1073+ * syncs go through the same helper. */
1074+ TEST (parallel_closure_repair_syncs_ids_through_the_helper ) {
1075+ char repo [256 ];
1076+ snprintf (repo , sizeof (repo ), "/tmp/cbm_par_ids_XXXXXX" );
1077+ ASSERT_NOT_NULL (cbm_mkdtemp (repo ));
1078+ if (!write_id_fixture (repo , 3 )) {
1079+ th_rmtree (repo );
1080+ FAIL ("failed to write the edge-id fixture" );
1081+ }
1082+ char db_path [512 ];
1083+ snprintf (db_path , sizeof (db_path ), "%s/ids.db" , repo );
1084+ cbm_pipeline_incremental_test_reset_faults ();
1085+ cbm_pipeline_t * first = cbm_pipeline_new (repo , db_path , CBM_MODE_FULL );
1086+ int first_rc = first ? cbm_pipeline_run (first ) : CBM_NOT_FOUND ;
1087+ cbm_pipeline_free (first );
1088+
1089+ /* A body-only edit (no new names) routes to closure repair. */
1090+ char path [512 ];
1091+ snprintf (path , sizeof (path ), "%s/f01.go" , repo );
1092+ int write_rc =
1093+ th_write_file (path , "package main\n\nfunc F01() int {\n\treturn F02() + 40 + 2\n}\n" );
1094+ id_sync_probe_t syncs = {.push = false};
1095+ cbm_pipeline_set_id_sync_hook_for_tests (id_sync_hook , & syncs );
1096+ cbm_pipeline_incremental_test_reset_faults ();
1097+ cbm_pipeline_t * second = cbm_pipeline_new (repo , db_path , CBM_MODE_FULL );
1098+ int second_rc = second ? cbm_pipeline_run (second ) : CBM_NOT_FOUND ;
1099+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route ();
1100+ cbm_pipeline_free (second );
1101+ cbm_pipeline_set_id_sync_hook_for_tests (NULL , NULL );
1102+ th_rmtree (repo );
1103+
1104+ ASSERT_EQ (first_rc , 0 );
1105+ ASSERT_EQ (write_rc , 0 );
1106+ ASSERT_EQ (second_rc , 0 );
1107+ ASSERT_EQ (route , CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR );
1108+ ASSERT_EQ (syncs .syncs , 2 );
1109+ PASS ();
1110+ }
1111+
9611112/* ── Parallel-pipeline LSP-override regression ────────────────────── */
9621113/* Pin the wiring fix that unified pass_calls.c (sequential) and
9631114 * pass_parallel.c (parallel) on cbm_pipeline_find_lsp_resolution +
@@ -4768,6 +4919,9 @@ SUITE(parallel) {
47684919 RUN_TEST (gbuf_merge_empty_src );
47694920 RUN_TEST (gbuf_merge_src_free_safe );
47704921 RUN_TEST (gbuf_next_id_accessors );
4922+ RUN_TEST (gbuf_raise_next_id_never_lowers );
4923+ RUN_TEST (parallel_full_index_keeps_edge_ids_unique );
4924+ RUN_TEST (parallel_closure_repair_syncs_ids_through_the_helper );
47714925
47724926 /* Parallel pipeline parity tests */
47734927 RUN_TEST (parallel_node_count );
0 commit comments