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.

chore: merge upstream main into issue 2265 branch

Pcristin6015c8439469
diff --cc src/mcp/mcp.c
index 57c8139d5,28d42230c..efb217e95
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@@ -68,7 -71,7 +71,8 @@@ enum
#include "watcher/watcher.h"
#include "foundation/mem.h"
#include "foundation/mem_core.h"
+#include "foundation/hash_table.h"
+ #include "foundation/arena.h"
#include "foundation/diagnostics.h"
#include "foundation/platform.h"
#include "foundation/compat.h"
diff --cc src/pipeline/pass_calls.c
index 7882a2307,a85ad7dca..3359841b1
--- a/src/pipeline/pass_calls.c
+++ b/src/pipeline/pass_calls.c
@@@ -686,8 -685,16 +690,17 @@@ static int resolve_single_call(cbm_pipe
res.qualified_name = target_node->qualified_name;
res.confidence = lsp->confidence;
res.strategy = lsp->strategy;
- res.candidate_count = 1;
+ res.candidate_count = CBM_ALLOC_ONE;
+ call->coverage_candidate_qn = target_node->qualified_name;
+ /* An LSP answer on a synthetic builtin (`<python-builtins>`) is
+ * not project code: a spawn spelling still spawns. The parallel
+ * pass keeps the LSP answer in `res` and asks the same question.
+ * MUST match pass_parallel.c. */
+ cbm_pipeline_spawn_t spawn;
+ if (cbm_pipeline_spawn_site(ctx->gbuf, lang, call, imports, &res, &spawn)) {
+ cbm_pipeline_emit_spawn(ctx->gbuf, source_node, call, &spawn);
+ return SKIP_ONE;
+ }
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys,
imp_vals, imp_count, false, route_mount);
return SKIP_ONE;
diff --cc src/pipeline/pass_parallel.c
index 0073e2a5d,d7847305a..b34db716c
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@@ -3688,28 -3873,55 +3877,78 @@@ static bool pp_has_pending_lsp_site(con
return false;
}
+static CBMFileResult *resolve_load_result(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ int file_idx) {
+ if (ws->loaded) {
+ cbm_free_result(ws->loaded);
+ ws->loaded = NULL;
+ }
+ CBMFileResult *result = rc->result_cache[file_idx];
+ if (!result && rc->pctx && rc->pctx->spill && cbm_result_spill_has(rc->pctx->spill, file_idx)) {
+ result = cbm_result_spill_load(rc->pctx->spill, file_idx);
+ ws->loaded = result;
++ (void)cbm_pipeline_test_extraction_ok(rc->pctx, rc->files[file_idx].language, result);
+ if (!result) {
+ cbm_pipeline_mark_unresolved_capture_failed(rc->pctx->pipeline);
+ }
+ }
+ return result;
+}
+
+static void resolve_capture_coverage(const resolve_ctx_t *rc, const char *rel,
+ const CBMFileResult *result) {
+ cbm_pipeline_record_unresolved_calls(rc->pctx ? rc->pctx->pipeline : NULL, rel, result);
+}
+
+ /* A C member access binds only through a field-owner row. The per-file walk
+ * publishes owners for the structs its own file declares; a struct from a
+ * header is known only to the cross-file pass. A member access that still has
+ * no owner is therefore a request for that pass, exactly like an unqualified
+ * call site -- without it a file with no pending call would keep none of its
+ * edges onto header-declared fields. */
+ static bool pp_has_unowned_c_member(const CBMFileResult *result, CBMLanguage lang,
+ const char *rel) {
+ if (!cbm_c_member_rule_file(lang, rel)) {
+ return false;
+ }
+ cbm_pipeline_lsp_field_index_t field_index = {0};
+ bool ready = cbm_pipeline_lsp_field_index_build(&result->resolved_calls, &field_index);
+ bool unowned = false;
+ for (int i = 0; i < result->usages.count && !unowned; i++) {
+ const CBMUsage *usage = &result->usages.items[i];
+ if (usage->kind != CBM_USAGE_VALUE || !usage->is_member_access || !usage->ref_name) {
+ continue;
+ }
+ cbm_pipeline_lsp_field_cursor_t owners =
+ cbm_pipeline_lsp_field_cursor(&result->resolved_calls, ready ? &field_index : NULL,
+ usage->enclosing_func_qn, usage->ref_name);
+ unowned = cbm_pipeline_lsp_field_next(&owners) == NULL;
+ }
+ for (int i = 0; i < result->rw.count && !unowned; i++) {
+ const CBMReadWrite *rw = &result->rw.items[i];
+ if (!rw->is_member_access || !rw->var_name) {
+ continue;
+ }
+ cbm_pipeline_lsp_field_cursor_t owners =
+ cbm_pipeline_lsp_field_cursor(&result->resolved_calls, ready ? &field_index : NULL,
+ rw->enclosing_func_qn, rw->var_name);
+ unowned = cbm_pipeline_lsp_field_next(&owners) == NULL;
+ }
+ for (int i = 0; i < result->calls.count && !unowned; i++) {
+ const CBMCall *call = &result->calls.items[i];
+ if (!call->callee_name ||
+ (!strchr(call->callee_name, '.') && !strstr(call->callee_name, "->"))) {
+ continue;
+ }
+ cbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(
+ &result->resolved_calls, ready ? &field_index : NULL, call->enclosing_func_qn,
+ cbm_lsp_bare_segment(call->callee_name));
+ unowned = cbm_pipeline_lsp_field_next(&owners) == NULL;
+ }
+ cbm_pipeline_lsp_field_index_free(&field_index);
+ return unowned;
+ }
+
static void resolve_worker(int worker_id, void *ctx_ptr) {
resolve_ctx_t *rc = ctx_ptr;
resolve_worker_state_t *ws = &rc->workers[worker_id];
@@@ -3948,10 -4192,7 +4210,11 @@@
atomic_fetch_add_explicit(&rc->time_ns_semantic, extract_now_ns() - _ph_t0,
memory_order_relaxed);
+ resolve_file_cleanup:
+ /* The cross-file additions and actual emitted-site evidence live in
+ * this result, including when it was loaded from the spill store. */
+ resolve_capture_coverage(rc, rel, result);
+
cbm_registry_reach_cache_end();
cbm_registry_import_map_cache_end();
cbm_registry_resolve_cache_end();
diff --cc src/pipeline/pipeline.c
index 2173a5aba,e18b41fc6..60f3c799f
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@@ -42,9 -41,7 +42,13 @@@ enum { CBM_DIR_PERMS = 0755, PL_RING =
#include "foundation/mem.h"
#include "foundation/mem_core.h"
#include "result_spill.h"
+#include "cbm.h"
#include "foundation/secure_random.h"
++/* C11 guarantees stdbool.h; keep yyjson's Boolean types aligned with ours. */
++#ifndef YYJSON_HAS_STDBOOL_H
++#define YYJSON_HAS_STDBOOL_H 1
++#endif
+#include "yyjson/yyjson.h"
#include <ctype.h>
#include <errno.h>
@@@ -443,269 -907,19 +923,282 @@@ void cbm_pipeline_set_lsp_surfaces(cbm_
p->surface_row_count = count;
}
+static void *unresolved_json_alloc(void *ctx, size_t size) {
+ (void)ctx;
+ return cbm_alloc(CBM_MEM_CLASS_DUMP, size);
+}
+
+static void *unresolved_json_realloc(void *ctx, void *ptr, size_t old_size, size_t size) {
+ (void)ctx;
+ (void)old_size;
+ return cbm_realloc(CBM_MEM_CLASS_DUMP, ptr, size);
+}
+
+static void unresolved_json_free(void *ctx, void *ptr) {
+ (void)ctx;
+ cbm_free(CBM_MEM_CLASS_DUMP, ptr);
+}
+
+enum { UNRESOLVED_KEY_OVERHEAD = 128, UNRESOLVED_INITIAL_ROWS = 64, UNRESOLVED_ROW_GROWTH = 2 };
+
+static char *unresolved_site_key(const char *caller, const char *leaf, uint32_t start, uint32_t end,
+ CBMSourceOrigin origin) {
+ size_t cn = strlen(caller);
+ size_t ln = strlen(leaf);
+ size_t cap = cn + ln + UNRESOLVED_KEY_OVERHEAD;
+ char *key = cbm_alloc(CBM_MEM_CLASS_DUMP, cap);
+ if (key) {
+ (void)snprintf(key, cap, "%zu:%s|%zu:%s|%u:%u:%u", cn, caller, ln, leaf, start, end,
+ (unsigned)origin);
+ }
+ return key;
+}
+
+typedef struct {
+ const CBMCall *call;
+} unresolved_carrier_t;
+
+typedef struct {
+ yyjson_mut_doc *doc;
+ yyjson_mut_val *sites;
+ CBMHashTable *carriers;
+ CBMHashTable *seen;
+} unresolved_capture_t;
+
+static void unresolved_carrier_free(const char *key, void *value, void *ctx) {
+ (void)ctx;
+ cbm_free(CBM_MEM_CLASS_DUMP, (void *)key);
+ cbm_free(CBM_MEM_CLASS_DUMP, value);
+}
+
+static void unresolved_key_free(const char *key, void *value, void *ctx) {
+ (void)value;
+ (void)ctx;
+ cbm_free(CBM_MEM_CLASS_DUMP, (void *)key);
+}
+
+static void unresolved_capture_free(unresolved_capture_t *capture) {
+ if (capture->carriers) {
+ cbm_ht_foreach(capture->carriers, unresolved_carrier_free, NULL);
+ cbm_ht_free(capture->carriers);
+ }
+ if (capture->seen) {
+ cbm_ht_foreach(capture->seen, unresolved_key_free, NULL);
+ cbm_ht_free(capture->seen);
+ }
+ if (capture->doc) {
+ yyjson_mut_doc_free(capture->doc);
+ }
+}
+
+static bool unresolved_call_is_missing(const CBMResolvedCall *rc) {
+ return rc->kind == CBM_RESOLVED_INVOCATION && rc->strategy &&
+ strcmp(rc->strategy, "lsp_unresolved") == 0 && rc->caller_qn && rc->callee_qn;
+}
+
+static bool unresolved_has_missing_calls(const CBMFileResult *result) {
+ for (int i = 0; i < result->resolved_calls.count; i++) {
+ if (unresolved_call_is_missing(&result->resolved_calls.items[i])) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool unresolved_add_carrier(CBMHashTable *carriers, const CBMCall *call) {
+ if (!call->callee_name || call->site_end_byte <= call->site_start_byte) {
+ return true;
+ }
+ char *key =
+ unresolved_site_key("", cbm_lsp_bare_segment(call->callee_name), call->site_start_byte,
+ call->site_end_byte, call->source_origin);
+ if (!key) {
+ return false;
+ }
+ if (cbm_ht_has(carriers, key)) {
+ /* Ambiguous extractor occurrences must never erase a diagnostic. */
+ unresolved_carrier_t *carrier = cbm_ht_get(carriers, key);
+ carrier->call = NULL;
+ cbm_free(CBM_MEM_CLASS_DUMP, key);
+ return true;
+ }
+ unresolved_carrier_t *carrier = cbm_alloc(CBM_MEM_CLASS_DUMP, sizeof(*carrier));
+ if (!carrier) {
+ cbm_free(CBM_MEM_CLASS_DUMP, key);
+ return false;
+ }
+ carrier->call = call;
+ cbm_ht_set(carriers, key, carrier);
+ if (!cbm_ht_has(carriers, key)) {
+ cbm_free(CBM_MEM_CLASS_DUMP, key);
+ cbm_free(CBM_MEM_CLASS_DUMP, carrier);
+ return false;
+ }
+ return true;
+}
+
+static bool unresolved_build_carriers(CBMHashTable *carriers, const CBMFileResult *result) {
+ /* Caller names in the TS LSP walk can denote an outer factory. The
+ * extractor's exact occurrence is the authoritative enclosing function.
+ * Index by span AND leaf: two receiver calls on one line remain distinct. */
+ for (int i = 0; i < result->calls.count; i++) {
+ if (!unresolved_add_carrier(carriers, &result->calls.items[i])) {
+ return false;
+ }
+ }
+ return true;
+}
+
+static bool unresolved_append_site(unresolved_capture_t *capture, const CBMResolvedCall *rc,
+ const CBMCall *call, const char *caller, const char *leaf) {
+ yyjson_mut_doc *doc = capture->doc;
+ yyjson_mut_val *site = yyjson_mut_obj(doc);
+ bool ok =
+ site && yyjson_mut_obj_add_strcpy(doc, site, "caller", caller) &&
+ yyjson_mut_obj_add_strcpy(doc, site, "leaf", leaf) &&
+ yyjson_mut_obj_add_uint(doc, site, "start_byte", rc->site_start_byte) &&
+ yyjson_mut_obj_add_uint(doc, site, "end_byte", rc->site_end_byte) &&
+ yyjson_mut_obj_add_uint(doc, site, "source_origin", rc->source_origin) &&
+ yyjson_mut_obj_add_strcpy(doc, site, "reason", rc->reason ? rc->reason : "unresolved");
+ if (ok && call && call->start_line > 0) {
+ ok = yyjson_mut_obj_add_int(doc, site, "line", call->start_line);
+ }
+ if (ok && call && call->coverage_candidate_qn) {
+ ok = yyjson_mut_obj_add_strcpy(doc, site, "candidate", call->coverage_candidate_qn);
+ }
+ return ok && yyjson_mut_arr_add_val(capture->sites, site);
+}
+
+static bool unresolved_collect_site(unresolved_capture_t *capture, const CBMResolvedCall *rc) {
+ const char *leaf = cbm_lsp_bare_segment(rc->callee_qn);
+ char *lookup =
+ unresolved_site_key("", leaf, rc->site_start_byte, rc->site_end_byte, rc->source_origin);
+ if (!lookup) {
+ return false;
+ }
+ const unresolved_carrier_t *carrier = cbm_ht_get(capture->carriers, lookup);
+ const CBMCall *call = carrier ? carrier->call : NULL;
+ cbm_free(CBM_MEM_CLASS_DUMP, lookup);
+ /* This exact occurrence emitted a real CALLS edge, possibly via the
+ * registry after LSP failure. Never suppress a different occurrence. */
+ if (call && call->coverage_calls_emitted) {
+ return true;
+ }
+ const char *caller = call && call->enclosing_func_qn ? call->enclosing_func_qn : rc->caller_qn;
+ char *key = unresolved_site_key(caller, leaf, rc->site_start_byte, rc->site_end_byte,
+ rc->source_origin);
+ if (!key) {
+ return false;
+ }
+ if (cbm_ht_has(capture->seen, key)) {
+ cbm_free(CBM_MEM_CLASS_DUMP, key);
+ return true;
+ }
+ cbm_ht_set(capture->seen, key, key);
+ if (!cbm_ht_has(capture->seen, key)) {
+ cbm_free(CBM_MEM_CLASS_DUMP, key);
+ return false;
+ }
+ return unresolved_append_site(capture, rc, call, caller, leaf);
+}
+
+static bool unresolved_collect_sites(unresolved_capture_t *capture, const CBMFileResult *result) {
+ for (int i = 0; i < result->resolved_calls.count; i++) {
+ const CBMResolvedCall *rc = &result->resolved_calls.items[i];
+ if (unresolved_call_is_missing(rc) && !unresolved_collect_site(capture, rc)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/* Caller holds unresolved_mutex. Ownership transfers only on success. */
+static bool unresolved_append_row(cbm_pipeline_t *p, const char *path, const char *detail) {
+ if (!detail || !path || p->unresolved_capture_failed) {
+ return false;
+ }
+ if (p->unresolved_count == p->unresolved_capacity) {
+ int capacity = p->unresolved_capacity ? p->unresolved_capacity * UNRESOLVED_ROW_GROWTH
+ : UNRESOLVED_INITIAL_ROWS;
+ cbm_coverage_row_t *rows =
+ cbm_realloc(CBM_MEM_CLASS_DUMP, p->unresolved_rows, (size_t)capacity * sizeof(*rows));
+ if (!rows) {
+ return false;
+ }
+ p->unresolved_rows = rows;
+ p->unresolved_capacity = capacity;
+ }
+ p->unresolved_rows[p->unresolved_count++] =
+ (cbm_coverage_row_t){.rel_path = path, .kind = "unresolved_calls", .detail = detail};
+ return true;
+}
+
+void cbm_pipeline_record_unresolved_calls(cbm_pipeline_t *p, const char *rel_path,
+ const CBMFileResult *result) {
+ if (!p || !rel_path || !result || p->unresolved_capture_failed) {
+ return;
+ }
+ if (!unresolved_has_missing_calls(result)) {
+ return;
+ }
+ yyjson_alc allocator = {.malloc = unresolved_json_alloc,
+ .realloc = unresolved_json_realloc,
+ .free = unresolved_json_free};
+ unresolved_capture_t capture = {.doc = yyjson_mut_doc_new(&allocator),
+ .carriers = cbm_ht_create(0),
+ .seen = cbm_ht_create(0)};
+ capture.sites = capture.doc ? yyjson_mut_arr(capture.doc) : NULL;
+ bool ok = capture.doc && capture.carriers && capture.seen && capture.sites;
+ if (capture.doc) {
+ yyjson_mut_doc_set_root(capture.doc, capture.sites);
+ }
+ ok = ok && unresolved_build_carriers(capture.carriers, result) &&
+ unresolved_collect_sites(&capture, result);
+ if (ok && yyjson_mut_arr_size(capture.sites) > 0) {
+ char *detail = yyjson_mut_write_opts(capture.doc, 0, &allocator, NULL, NULL);
+ char *path = cbm_mem_strdup(CBM_MEM_CLASS_DUMP, rel_path);
+ cbm_mutex_lock(&p->unresolved_mutex);
+ ok = unresolved_append_row(p, path, detail);
+ cbm_mutex_unlock(&p->unresolved_mutex);
+ if (!ok) {
+ cbm_free(CBM_MEM_CLASS_DUMP, detail);
+ cbm_free(CBM_MEM_CLASS_DUMP, path);
+ }
+ }
+ if (!ok) {
+ p->unresolved_capture_failed = true;
+ }
+ unresolved_capture_free(&capture);
+}
+
+void cbm_pipeline_mark_unresolved_capture_failed(cbm_pipeline_t *p) {
+ if (p)
+ p->unresolved_capture_failed = true;
+}
+
+void cbm_pipeline_get_unresolved_calls(cbm_pipeline_t *p, cbm_coverage_row_t **rows, int *count,
+ bool *complete) {
+ if (rows)
+ *rows = p ? p->unresolved_rows : NULL;
+ if (count)
+ *count = p ? p->unresolved_count : 0;
+ if (complete)
+ *complete = p && !p->unresolved_capture_failed;
+}
+
+ static void pipeline_release_test_config(cbm_pipeline_t *p) {
+ if (!p) {
+ return;
+ }
+ if (p->userconfig && cbm_get_user_lang_config() == p->userconfig) {
+ cbm_set_user_lang_config(NULL);
+ }
+ cbm_test_declarations_free(p->test_declarations);
+ p->test_declarations = NULL;
+ cbm_userconfig_free(p->userconfig);
+ p->userconfig = NULL;
+ }
+
void cbm_pipeline_free(cbm_pipeline_t *p) {
if (!p) {
return;
@@@ -736,20 -950,10 +1229,16 @@@
cbm_store_free_lsp_surfaces(p->surface_rows, p->surface_row_count);
p->surface_rows = NULL;
p->surface_row_count = 0;
+ for (int i = 0; i < p->unresolved_count; i++) {
+ cbm_free(CBM_MEM_CLASS_DUMP, (char *)p->unresolved_rows[i].rel_path);
+ cbm_free(CBM_MEM_CLASS_DUMP, (char *)p->unresolved_rows[i].detail);
+ }
+ cbm_free(CBM_MEM_CLASS_DUMP, p->unresolved_rows);
+ cbm_mutex_destroy(&p->unresolved_mutex);
cbm_git_context_free(&p->git_ctx);
/* gbuf, store, registry freed during/after run */
- /* Defensively free userconfig in case run() was never called or panicked */
- if (p->userconfig) {
- cbm_set_user_lang_config(NULL);
- cbm_userconfig_free(p->userconfig);
- p->userconfig = NULL;
- }
+ /* Defensive owner cleanup; normal runs release after publication. */
+ pipeline_release_test_config(p);
free(p);
}