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(pipeline): keep PHP trees in the sequential cache, release on abort

Martin Vogel5c9099f015bc
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index 4d544fc7f..a3ab79635 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -1136,8 +1136,13 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
if (local_cache) {
/* The cache lives through every later pass: hold the result as the
* parallel extract does (pass_parallel.c), without its tree (the
- * cross-file LSP re-parses) and compacted. */
- cbm_free_tree(result);
+ * cross-file LSP re-parses) and compacted. A PHP result keeps its
+ * tree: the vendor-import check reads each call and usage site's
+ * syntax from it (php_import_kind_at_site, pass_pkgmap.c), and this
+ * route keeps no source to fall back on. */
+ if (lang != CBM_LANG_PHP) {
+ cbm_free_tree(result);
+ }
cbm_result_compact(result);
local_cache[i] = result;
} else {
@@ -1214,5 +1219,6 @@ configured_failure:
}
cbm_free(CBM_MEM_CLASS_EXTRACT, local_cache);
}
+ cbm_work_arena_release(); /* compaction keeps it between files: release on every exit */
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c
index b5f1e7fbf..b38c8ae28 100644
--- a/tests/test_edge_imports.c
+++ b/tests/test_edge_imports.c
@@ -2009,6 +2009,45 @@ TEST(ei_php_vendor_types_never_bind_by_name_issue1186) {
PASS();
}
+/* A vendor `use function` import shares its local name with a project class the
+ * file also imports. `new Thing()`, a `Thing $t` parameter and `extends Thing`
+ * name the class, so the function import must not veto their edges. The check
+ * reads each site's syntax from the parse tree, which the sequential
+ * definitions pass (a repo of 50 files or fewer) keeps for PHP results. */
+TEST(ei_php_vendor_function_import_keeps_same_named_class_sites) {
+ static const EILangFile f[] = {
+ {"composer.json", "{\"autoload\":{\"psr-4\":{\"App\\\\\":\"app/\"}}}\n"},
+ {"app/Models/Thing.php", "<?php\nnamespace App\\Models;\n\nclass Thing {\n"
+ " public function __construct() {}\n"
+ " public function go() { return 1; }\n}\n"},
+ {"app/Http/UsesThing.php", "<?php\nnamespace App\\Http;\n\n"
+ "use App\\Models\\Thing;\nuse function Vendor\\Lib\\Thing;\n\n"
+ "class UsesThing extends Thing {\n"
+ " public function run(Thing $t) {\n"
+ " $made = new Thing();\n"
+ " return $made->go();\n"
+ " }\n}\n"}};
+ static const char *const uses = "app/Http/UsesThing.php";
+ static const char *const thing = "app/Models/Thing.php";
+ EILangProj lp;
+ cbm_store_t *store = ei_index_files(&lp, f, 3);
+ int calls = store ? ei_php_checked_file_edges(store, lp.project, uses, "CALLS", thing) : -1;
+ int usages = store ? ei_php_checked_file_edges(store, lp.project, uses, "USAGE", thing) : -1;
+ int inherits =
+ store ? ei_php_checked_file_edges(store, lp.project, uses, "INHERITS", thing) : -1;
+ ei_cleanup(&lp, store);
+ if (calls != 2 || usages != 2 || inherits != 1) {
+ fprintf(stderr,
+ " UsesThing.php -> Thing.php: CALLS %d (want 2), USAGE %d (want 2), "
+ "INHERITS %d (want 1)\n",
+ calls, usages, inherits);
+ }
+ ASSERT_EQ(calls, 2); /* new Thing() and $made->go() */
+ ASSERT_EQ(usages, 2); /* extends Thing and the Thing $t parameter */
+ ASSERT_EQ(inherits, 1); /* never vetoed: control */
+ PASS();
+}
+
/* Build the metadata through the production writer, then challenge its
* readback. A File node or an empty edge query alone is never completeness. */
static bool ei_php_seed_result(cbm_gbuf_t *gb, const char *project, const char *rel,
@@ -2393,6 +2432,7 @@ SUITE(edge_imports) {
RUN_TEST(ei_php_null_namespace_map_keeps_php_candidate_issue1186);
RUN_TEST(ei_php_require_path_keeps_generic_resolution_issue1186);
RUN_TEST(ei_php_vendor_types_never_bind_by_name_issue1186);
+ RUN_TEST(ei_php_vendor_function_import_keeps_same_named_class_sites);
RUN_TEST(ei_php_binding_snapshot_requires_current_complete_evidence);
RUN_TEST(ei_php_binding_snapshot_groups_kinds_and_invalidates_failed_rebuild);
RUN_TEST(ei_php_psr4_missing_class_leaves_type_evidence_unknown);
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index 9dad1591c..9183f33b5 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -775,7 +775,8 @@ TEST(parallel_post_extract_projection_spills_before_resolve) {
/* The parallel extract caches each result without its parse tree and
* compacted (pass_parallel.c). The sequential definitions pass cached the raw
* result, tree and working arena included, and that cache lives through every
- * later pass: about 5x the memory per file, for every file of the repo. */
+ * later pass: about 5x the memory per file, for every file of the repo. PHP
+ * keeps its tree: the vendor-import check reads its sites' syntax from it. */
TEST(sequential_definitions_cache_drops_trees_and_compacts) {
if (ensure_parity_setup() != 0)
FAIL("setup failed");
@@ -806,18 +807,25 @@ TEST(sequential_definitions_cache_drops_trees_and_compacts) {
ASSERT_FALSE(cbm_work_arena_keeping()); /* ...and end the pass that way */
int cached = 0;
+ int php_trees = 0;
for (int i = 0; i < file_count; i++) {
const CBMFileResult *r = cache[i];
if (!r) {
continue;
}
cached++;
- ASSERT_NULL(r->cached_tree);
+ if (files[i].language == CBM_LANG_PHP) {
+ ASSERT_NOT_NULL(r->cached_tree);
+ php_trees++;
+ } else {
+ ASSERT_NULL(r->cached_tree);
+ }
/* One exact block: capacity == bytes used, no dead headroom. */
ASSERT_EQ(r->arena.nblocks, 1);
ASSERT_EQ(cbm_arena_capacity(&r->arena), cbm_arena_total(&r->arena));
}
ASSERT_EQ(cached, file_count);
+ ASSERT_GT(php_trees, 0); /* app/Models.php and app/Services.php */
for (int i = 0; i < file_count; i++) {
cbm_free_result(cache[i]);
@@ -829,6 +837,60 @@ TEST(sequential_definitions_cache_drops_trees_and_compacts) {
PASS();
}
+/* A configured C test convention whose file cannot be extracted aborts the
+ * sequential pass (CBM_PIPELINE_ABORT_PRESERVE_DB). The Python file compacted
+ * before it handed its working arena back to this thread, so that exit has to
+ * release it like the others. */
+TEST(sequential_definitions_configured_failure_releases_work_arena) {
+ char dir[256];
+ snprintf(dir, sizeof(dir), "/tmp/cbm_seqcf_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(dir));
+ char py_path[320];
+ char c_path[320];
+ snprintf(py_path, sizeof(py_path), "%s/a.py", dir);
+ snprintf(c_path, sizeof(c_path), "%s/z.c", dir);
+ ASSERT_EQ(th_write_file(py_path, "def first():\n return 1\n"), 0);
+ ASSERT_EQ(th_write_file(c_path, "void second(void) {}\n"), 0);
+ cbm_file_info_t files[] = {
+ {.path = py_path, .rel_path = "a.py", .language = CBM_LANG_PYTHON},
+ {.path = c_path, .rel_path = "z.c", .language = CBM_LANG_C},
+ };
+ static const char config[] =
+ "{\"test_impact\":{\"version\":1,\"tests\":{\"conventions\":[{\"language\":\"c\","
+ "\"role\":\"case\",\"define_macro\":\"CHECK\",\"name_args\":[0]}]}}}";
+ cbm_test_declarations_t *declarations =
+ cbm_test_declarations_parse(config, sizeof(config) - 1, false);
+ ASSERT_NOT_NULL(declarations);
+
+ cbm_gbuf_t *gbuf = cbm_gbuf_new("seq-cf", dir);
+ cbm_registry_t *reg = cbm_registry_new();
+ atomic_int cancelled;
+ atomic_init(&cancelled, 0);
+ cbm_pipeline_ctx_t ctx = {
+ .project_name = "seq-cf",
+ .repo_path = dir,
+ .gbuf = gbuf,
+ .registry = reg,
+ .cancelled = &cancelled,
+ .test_declarations = declarations,
+ .test_fault_mask = CBM_PIPELINE_TEST_FAULT_EXTRACT_NULL, /* z.c extracts to NULL */
+ };
+
+ cbm_init();
+ cbm_work_arena_release(); /* start from a thread that keeps nothing */
+ int rc = cbm_pipeline_pass_definitions(&ctx, files, 2);
+ bool keeping = cbm_work_arena_keeping();
+ cbm_work_arena_release();
+
+ cbm_registry_free(reg);
+ cbm_gbuf_free(gbuf);
+ cbm_test_declarations_free(declarations);
+ rm_rf(dir);
+ ASSERT_EQ(rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
+ ASSERT_FALSE(keeping);
+ PASS();
+}
+
/* ── Empty file list ──────────────────────────────────────────────── */
TEST(parallel_empty_files) {
@@ -5659,6 +5721,7 @@ SUITE(parallel) {
RUN_TEST(parallel_spill_mode_builds_the_same_graph);
RUN_TEST(parallel_post_extract_projection_spills_before_resolve);
RUN_TEST(sequential_definitions_cache_drops_trees_and_compacts);
+ RUN_TEST(sequential_definitions_configured_failure_releases_work_arena);
RUN_TEST(parallel_empty_files);
RUN_TEST(parallel_args_json_no_overflow);
RUN_TEST(parallel_wide_definition_keeps_fp_sp_bt_f14);