@@ -770,6 +770,127 @@ TEST(parallel_post_extract_projection_spills_before_resolve) {
770770 PASS ();
771771}
772772
773+ /* ── Sequential cache: held like the parallel one ─────────────────── */
774+
775+ /* The parallel extract caches each result without its parse tree and
776+ * compacted (pass_parallel.c). The sequential definitions pass cached the raw
777+ * result, tree and working arena included, and that cache lives through every
778+ * later pass: about 5x the memory per file, for every file of the repo. PHP
779+ * keeps its tree: the vendor-import check reads its sites' syntax from it. */
780+ TEST (sequential_definitions_cache_drops_trees_and_compacts ) {
781+ if (ensure_parity_setup () != 0 )
782+ FAIL ("setup failed" );
783+ cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL };
784+ cbm_file_info_t * files = NULL ;
785+ int file_count = 0 ;
786+ ASSERT_EQ (cbm_discover (g_par_tmpdir , & opts , & files , & file_count ), 0 );
787+ ASSERT_GT (file_count , 0 );
788+
789+ cbm_gbuf_t * gbuf = cbm_gbuf_new ("par-test" , g_par_tmpdir );
790+ cbm_registry_t * reg = cbm_registry_new ();
791+ CBMFileResult * * cache = (CBMFileResult * * )calloc ((size_t )file_count , sizeof (CBMFileResult * ));
792+ ASSERT_NOT_NULL (cache );
793+ atomic_int cancelled ;
794+ atomic_init (& cancelled , 0 );
795+ cbm_pipeline_ctx_t ctx = {
796+ .project_name = "par-test" ,
797+ .repo_path = g_par_tmpdir ,
798+ .gbuf = gbuf ,
799+ .registry = reg ,
800+ .cancelled = & cancelled ,
801+ .result_cache = cache ,
802+ };
803+
804+ cbm_init ();
805+ cbm_work_arena_release (); /* start from a thread that keeps nothing */
806+ ASSERT_EQ (cbm_pipeline_pass_definitions (& ctx , files , file_count ), 0 );
807+ ASSERT_FALSE (cbm_work_arena_keeping ()); /* ...and end the pass that way */
808+
809+ int cached = 0 ;
810+ int php_trees = 0 ;
811+ for (int i = 0 ; i < file_count ; i ++ ) {
812+ const CBMFileResult * r = cache [i ];
813+ if (!r ) {
814+ continue ;
815+ }
816+ cached ++ ;
817+ if (files [i ].language == CBM_LANG_PHP ) {
818+ ASSERT_NOT_NULL (r -> cached_tree );
819+ php_trees ++ ;
820+ } else {
821+ ASSERT_NULL (r -> cached_tree );
822+ }
823+ /* One exact block: capacity == bytes used, no dead headroom. */
824+ ASSERT_EQ (r -> arena .nblocks , 1 );
825+ ASSERT_EQ (cbm_arena_capacity (& r -> arena ), cbm_arena_total (& r -> arena ));
826+ }
827+ ASSERT_EQ (cached , file_count );
828+ ASSERT_GT (php_trees , 0 ); /* app/Models.php and app/Services.php */
829+
830+ for (int i = 0 ; i < file_count ; i ++ ) {
831+ cbm_free_result (cache [i ]);
832+ }
833+ free (cache );
834+ cbm_registry_free (reg );
835+ cbm_gbuf_free (gbuf );
836+ cbm_discover_free (files , file_count );
837+ PASS ();
838+ }
839+
840+ /* A configured C test convention whose file cannot be extracted aborts the
841+ * sequential pass (CBM_PIPELINE_ABORT_PRESERVE_DB). The Python file compacted
842+ * before it handed its working arena back to this thread, so that exit has to
843+ * release it like the others. */
844+ TEST (sequential_definitions_configured_failure_releases_work_arena ) {
845+ char dir [256 ];
846+ snprintf (dir , sizeof (dir ), "/tmp/cbm_seqcf_XXXXXX" );
847+ ASSERT_NOT_NULL (cbm_mkdtemp (dir ));
848+ char py_path [320 ];
849+ char c_path [320 ];
850+ snprintf (py_path , sizeof (py_path ), "%s/a.py" , dir );
851+ snprintf (c_path , sizeof (c_path ), "%s/z.c" , dir );
852+ ASSERT_EQ (th_write_file (py_path , "def first():\n return 1\n" ), 0 );
853+ ASSERT_EQ (th_write_file (c_path , "void second(void) {}\n" ), 0 );
854+ cbm_file_info_t files [] = {
855+ {.path = py_path , .rel_path = "a.py" , .language = CBM_LANG_PYTHON },
856+ {.path = c_path , .rel_path = "z.c" , .language = CBM_LANG_C },
857+ };
858+ static const char config [] =
859+ "{\"test_impact\":{\"version\":1,\"tests\":{\"conventions\":[{\"language\":\"c\","
860+ "\"role\":\"case\",\"define_macro\":\"CHECK\",\"name_args\":[0]}]}}}" ;
861+ cbm_test_declarations_t * declarations =
862+ cbm_test_declarations_parse (config , sizeof (config ) - 1 , false);
863+ ASSERT_NOT_NULL (declarations );
864+
865+ cbm_gbuf_t * gbuf = cbm_gbuf_new ("seq-cf" , dir );
866+ cbm_registry_t * reg = cbm_registry_new ();
867+ atomic_int cancelled ;
868+ atomic_init (& cancelled , 0 );
869+ cbm_pipeline_ctx_t ctx = {
870+ .project_name = "seq-cf" ,
871+ .repo_path = dir ,
872+ .gbuf = gbuf ,
873+ .registry = reg ,
874+ .cancelled = & cancelled ,
875+ .test_declarations = declarations ,
876+ .test_fault_mask = CBM_PIPELINE_TEST_FAULT_EXTRACT_NULL , /* z.c extracts to NULL */
877+ };
878+
879+ cbm_init ();
880+ cbm_work_arena_release (); /* start from a thread that keeps nothing */
881+ int rc = cbm_pipeline_pass_definitions (& ctx , files , 2 );
882+ bool keeping = cbm_work_arena_keeping ();
883+ cbm_work_arena_release ();
884+
885+ cbm_registry_free (reg );
886+ cbm_gbuf_free (gbuf );
887+ cbm_test_declarations_free (declarations );
888+ rm_rf (dir );
889+ ASSERT_EQ (rc , CBM_PIPELINE_ABORT_PRESERVE_DB );
890+ ASSERT_FALSE (keeping );
891+ PASS ();
892+ }
893+
773894/* ── Empty file list ──────────────────────────────────────────────── */
774895
775896TEST (parallel_empty_files ) {
@@ -5599,6 +5720,8 @@ SUITE(parallel) {
55995720 RUN_TEST (parallel_total_edges );
56005721 RUN_TEST (parallel_spill_mode_builds_the_same_graph );
56015722 RUN_TEST (parallel_post_extract_projection_spills_before_resolve );
5723+ RUN_TEST (sequential_definitions_cache_drops_trees_and_compacts );
5724+ RUN_TEST (sequential_definitions_configured_failure_releases_work_arena );
56025725 RUN_TEST (parallel_empty_files );
56035726 RUN_TEST (parallel_args_json_no_overflow );
56045727 RUN_TEST (parallel_wide_definition_keeps_fp_sp_bt_f14 );
0 commit comments