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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
wip: capture remaining semantic reference regressions
Martin Vogel300225c09d89
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex fbfd22e0f..f479b7c7b 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -151,6 +151,7 @@ static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) {static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node);static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node);static void py_process_statement(PyLSPContext *ctx, TSNode node);+static void py_invalidate_possible_bindings(PyLSPContext *ctx, TSNode node, int depth);static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn,const char *member_name);static void py_emit_resolved_call(PyLSPContext *ctx, const char *callee_qn, const char *strategy,@@ -183,15 +184,37 @@ static const char *py_lookup_dict_dispatch(PyLSPContext *ctx, const char *var, c* node is legitimately re-evaluated under different bindings — e.g. a* lambda body re-walked per call site with per-call argument types, or* isinstance-narrowed branches. */+static void py_disable_callable_value_proof(PyLSPContext *ctx) {+ if (ctx)+ ctx->callable_value_proof_disabled = true;+}+static void py_scope_bind(PyLSPContext *ctx, const char *name, const CBMType *type) {ctx->type_cache_gen++;cbm_scope_bind(ctx->current_scope, name, type);+ if (name && !cbm_scope_contains(ctx->current_scope, name))+ py_disable_callable_value_proof(ctx);}static void py_scope_bind_callable(PyLSPContext *ctx, const char *name, const CBMType *type,const char *callable_qn) {ctx->type_cache_gen++;cbm_scope_bind_callable(ctx->current_scope, name, type, callable_qn);+ const char *bound = name ? cbm_scope_lookup_callable(ctx->current_scope, name) : NULL;+ if (name && (!cbm_scope_contains(ctx->current_scope, name) ||+ (callable_qn && (!bound || strcmp(bound, callable_qn) != 0)))) {+ py_disable_callable_value_proof(ctx);+ }+}++static CBMScope *py_scope_push_checked(PyLSPContext *ctx) {+ if (!ctx)+ return NULL;+ CBMScope *parent = ctx->current_scope;+ CBMScope *child = cbm_scope_push(ctx->arena, parent);+ if (!child || child == parent)+ py_disable_callable_value_proof(ctx);+ return child;}static void py_scope_clear_callable(PyLSPContext *ctx, const char *name) {@@ -217,7 +240,7 @@ void py_lsp_init(PyLSPContext *ctx, CBMArena *arena, const char *source, int souctx->registry = registry;ctx->module_qn = module_qn;ctx->resolved_calls = out;- ctx->current_scope = cbm_scope_push(arena, NULL);+ ctx->current_scope = py_scope_push_checked(ctx);const char *dbg = getenv("CBM_LSP_DEBUG");ctx->debug = dbg && dbg[0] && dbg[0] != '0';}@@ -404,74 +427,104 @@ static const char *py_canonical_from_import_qn(PyLSPContext *ctx, TSNode stateme* binding for the local name. Python permits later imports to rebind the same* local; selecting the first would turn conflicting runtime identities into* false semantic proof. */-static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, TSNode root,- const char *local, const char **qn_io) {- const char *qn = qn_io ? *qn_io : NULL;- if (!ctx || !local || !qn || !qn_io || ts_node_is_null(root))- return PY_DIRECT_IMPORT_UNKNOWN;-- PyImportSyntaxMatch match = {0};- uint32_t root_count = ts_node_named_child_count(root);- for (uint32_t i = 0; i < root_count; i++) {- TSNode stmt = ts_node_named_child(root, i);- const char *stmt_kind = ts_node_type(stmt);- if (strcmp(stmt_kind, "import_from_statement") == 0) {- TSNode module = py_from_import_module_node(stmt);- uint32_t import_count = ts_node_named_child_count(stmt);- for (uint32_t j = 0; j < import_count; j++) {- TSNode item = ts_node_named_child(stmt, j);- if (!ts_node_is_null(module) && ts_node_eq(item, module))- continue;- const char *kind = ts_node_type(item);- if (strcmp(kind, "aliased_import") == 0) {- TSNode name = ts_node_child_by_field_name(item, "name", 4);- TSNode alias = ts_node_child_by_field_name(item, "alias", 5);- if (py_import_node_text_equals(ctx, alias, local)) {- const char *canonical =- py_canonical_from_import_qn(ctx, stmt, name, qn);- py_import_syntax_match_add(- &match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,- canonical ? canonical : qn);- }- } else if ((strcmp(kind, "identifier") == 0 ||- strcmp(kind, "dotted_name") == 0) &&- py_import_node_text_equals(ctx, item, local)) {- const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);- py_import_syntax_match_add(- &match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,- canonical ? canonical : qn);- }- }- continue;- }- if (strcmp(stmt_kind, "import_statement") != 0)- continue;+static void py_import_match_statement(PyLSPContext *ctx, TSNode stmt, const char *local,+ const char *qn, PyImportSyntaxMatch *match) {+ if (!ctx || ts_node_is_null(stmt) || !local || !qn || !match)+ return;+ const char *stmt_kind = ts_node_type(stmt);+ if (strcmp(stmt_kind, "import_from_statement") == 0) {+ TSNode module = py_from_import_module_node(stmt);uint32_t import_count = ts_node_named_child_count(stmt);for (uint32_t j = 0; j < import_count; j++) {TSNode item = ts_node_named_child(stmt, j);+ if (!ts_node_is_null(module) && ts_node_eq(item, module))+ continue;const char *kind = ts_node_type(item);if (strcmp(kind, "aliased_import") == 0) {+ TSNode name = ts_node_child_by_field_name(item, "name", 4);TSNode alias = ts_node_child_by_field_name(item, "alias", 5);- if (py_import_node_text_equals(ctx, alias, local))- py_import_syntax_match_add(&match, PY_DIRECT_IMPORT_ALIASED, qn);- } else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&- py_import_node_root_equals(ctx, item, local)) {- char *imported_path = py_import_node_text_dup(ctx, item);+ if (py_import_node_text_equals(ctx, alias, local)) {+ const char *canonical = py_canonical_from_import_qn(ctx, stmt, name, qn);+ py_import_syntax_match_add(+ match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,+ canonical ? canonical : qn);+ }+ } else if ((strcmp(kind, "identifier") == 0 ||+ strcmp(kind, "dotted_name") == 0) &&+ py_import_node_text_equals(ctx, item, local)) {+ const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);+ py_import_syntax_match_add(+ match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,+ canonical ? canonical : qn);+ }+ }+ return;+ }+ if (strcmp(stmt_kind, "import_statement") != 0)+ return;+ uint32_t import_count = ts_node_named_child_count(stmt);+ for (uint32_t j = 0; j < import_count; j++) {+ TSNode item = ts_node_named_child(stmt, j);+ const char *kind = ts_node_type(item);+ if (strcmp(kind, "aliased_import") == 0) {+ TSNode name = ts_node_child_by_field_name(item, "name", 4);+ TSNode alias = ts_node_child_by_field_name(item, "alias", 5);+ if (py_import_node_text_equals(ctx, alias, local)) {+ char *imported_path = py_import_node_text_dup(ctx, name);PyDirectImportKind matched_kind =imported_path && py_qn_has_boundary_suffix(qn, imported_path)- ? PY_DIRECT_IMPORT_UNALIASED+ ? PY_DIRECT_IMPORT_ALIASED: PY_IMPORT_UNCLASSIFIED;- py_import_syntax_match_add(&match, matched_kind, qn);+ py_import_syntax_match_add(match, matched_kind, qn);}+ } else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&+ py_import_node_root_equals(ctx, item, local)) {+ char *imported_path = py_import_node_text_dup(ctx, item);+ PyDirectImportKind matched_kind =+ imported_path && py_qn_has_boundary_suffix(qn, imported_path)+ ? PY_DIRECT_IMPORT_UNALIASED+ : PY_IMPORT_UNCLASSIFIED;+ py_import_syntax_match_add(match, matched_kind, qn);}}- if (match.count == 0)+}++static PyDirectImportKind py_import_match_result(PyImportSyntaxMatch *match,+ const char **qn_io) {+ if (!match || !qn_io)+ return PY_DIRECT_IMPORT_UNKNOWN;+ if (match->count == 0)return PY_IMPORT_UNCLASSIFIED;- if (match.count > 1)+ if (match->count > 1)return PY_IMPORT_AMBIGUOUS;- if (match.kind == PY_FROM_IMPORT && match.canonical_qn)- *qn_io = match.canonical_qn;- return match.kind;+ if (match->kind == PY_FROM_IMPORT && match->canonical_qn)+ *qn_io = match->canonical_qn;+ return match->kind;+}++static PyDirectImportKind py_import_kind_from_statement(PyLSPContext *ctx, TSNode stmt,+ const char *local,+ const char **qn_io) {+ const char *qn = qn_io ? *qn_io : NULL;+ if (!ctx || !local || !qn || !qn_io || ts_node_is_null(stmt))+ return PY_DIRECT_IMPORT_UNKNOWN;+ PyImportSyntaxMatch match = {0};+ py_import_match_statement(ctx, stmt, local, qn, &match);+ return py_import_match_result(&match, qn_io);+}++static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, TSNode root,+ const char *local, const char **qn_io) {+ const char *qn = qn_io ? *qn_io : NULL;+ if (!ctx || !local || !qn || !qn_io || ts_node_is_null(root))+ return PY_DIRECT_IMPORT_UNKNOWN;++ PyImportSyntaxMatch match = {0};+ uint32_t root_count = ts_node_named_child_count(root);+ for (uint32_t i = 0; i < root_count; i++) {+ py_import_match_statement(ctx, ts_node_named_child(root, i), local, qn, &match);+ }+ return py_import_match_result(&match, qn_io);}static bool py_import_index_is_from_binding(const PyLSPContext *ctx, int index) {@@ -513,40 +566,47 @@ static void py_bind_dotted_prefixes(PyLSPContext *ctx, const char *qn) {}}-static void py_lsp_bind_imports_for_root(PyLSPContext *ctx, TSNode root) {- if (!ctx || !ctx->current_scope)+static void py_classify_imports_for_root(PyLSPContext *ctx, TSNode root) {+ if (!ctx)return;for (int i = 0; i < ctx->import_count; i++) {const char *local = ctx->import_local_names[i];const char *qn = ctx->import_module_qns[i];if (!local || !qn)continue;-- // Wildcard imports are recorded for traceability but cannot bind a- // concrete name — skip the scope insertion. Phase 9 cross-file- // logic will use the import map directly to find re-exports.- if (strcmp(local, "*") == 0)- continue;-PyDirectImportKind direct_kind = py_import_kind_from_ast(ctx, root, local, &qn);ctx->import_module_qns[i] = qn;if (ctx->import_kinds)ctx->import_kinds[i] = (unsigned char)direct_kind;- bool from_style = direct_kind == PY_FROM_IMPORT ||- (direct_kind == PY_DIRECT_IMPORT_UNKNOWN &&- import_is_from_style(local, qn));- const CBMType *t;- if (direct_kind == PY_DIRECT_IMPORT_UNALIASED) {+ }+}++static void py_bind_import_index(PyLSPContext *ctx, int index, bool synthetic_fallback) {+ if (!ctx || !ctx->current_scope || index < 0 || index >= ctx->import_count)+ return;+ const char *local = ctx->import_local_names[index];+ const char *qn = ctx->import_module_qns[index];+ if (!local || !qn || strcmp(local, "*") == 0)+ return;++ PyDirectImportKind direct_kind = ctx->import_kinds+ ? (PyDirectImportKind)ctx->import_kinds[index]+ : PY_DIRECT_IMPORT_UNKNOWN;+ bool from_style = direct_kind == PY_FROM_IMPORT ||+ (direct_kind == PY_DIRECT_IMPORT_UNKNOWN &&+ import_is_from_style(local, qn));+ const CBMType *t;+ if (direct_kind == PY_DIRECT_IMPORT_UNALIASED) {// Python binds only the root of an unaliased dotted import.- t = cbm_type_module(ctx->arena, local);- } else if (direct_kind == PY_DIRECT_IMPORT_ALIASED) {- t = cbm_type_module(ctx->arena, qn);- } else if (from_style) {+ t = cbm_type_module(ctx->arena, local);+ } else if (direct_kind == PY_DIRECT_IMPORT_ALIASED) {+ t = cbm_type_module(ctx->arena, qn);+ } else if (from_style) {// `from X import Y` — bind Y to NAMED(X.Y). Phase 6 attribute// resolution checks the registry to upgrade to MODULE / class// / function as appropriate.- t = cbm_type_named(ctx->arena, qn);- } else if (strchr(qn, '.') != NULL) {+ t = cbm_type_named(ctx->arena, qn);+ } else if (strchr(qn, '.') != NULL) {// Dotted path whose tail does NOT match the local name: an// ALIASED binding — `from X import Y as Z` (Z names function/// class X.Y) or `import a.b as z` (z names module a.b). The@@ -556,23 +616,28 @@ static void py_lsp_bind_imports_for_root(PyLSPContext *ctx, TSNode root) {// Binding MODULE here made `g()` calls on `from m import f as g`// resolve as calls on a module — lsp=MISS, and the whole CALLS// edge was lost (#988).- t = cbm_type_named(ctx->arena, qn);- } else {+ t = cbm_type_named(ctx->arena, qn);+ } else {// `import X` / `import X as Y` (single segment) — MODULE(X).- t = cbm_type_module(ctx->arena, qn);- }- const CBMRegisteredFunc *imported =- from_style && ctx->registry ? cbm_registry_lookup_func(ctx->registry, qn) : NULL;- if (py_func_is_exact_callable_value(imported)) {- py_scope_bind_callable(ctx, local, t, imported->qualified_name);- } else {- py_scope_bind(ctx, local, t);- }- // With a real AST, only an unaliased direct import introduces the- // dotted root. Keep the historical prefix fallback for synthetic- // unit-test contexts that call py_lsp_bind_imports without a tree.- if (direct_kind == PY_DIRECT_IMPORT_UNALIASED || ts_node_is_null(root))- py_bind_dotted_prefixes(ctx, qn);+ t = cbm_type_module(ctx->arena, qn);+ }+ const CBMRegisteredFunc *imported =+ from_style && ctx->registry ? cbm_registry_lookup_func(ctx->registry, qn) : NULL;+ if (py_func_is_exact_callable_value(imported)) {+ py_scope_bind_callable(ctx, local, t, imported->qualified_name);+ } else {+ py_scope_bind(ctx, local, t);+ }+ if (direct_kind == PY_DIRECT_IMPORT_UNALIASED || synthetic_fallback)+ py_bind_dotted_prefixes(ctx, qn);+}++static void py_lsp_bind_imports_for_root(PyLSPContext *ctx, TSNode root) {+ if (!ctx || !ctx->current_scope)+ return;+ py_classify_imports_for_root(ctx, root);+ for (int i = 0; i < ctx->import_count; i++) {+ py_bind_import_index(ctx, i, ts_node_is_null(root));}}@@ -851,7 +916,7 @@ static const char *py_exact_imported_reference_candidate(PyLSPContext *ctx,* statically exact. Complex containers/conditionals deliberately return NULL* so their constituent function names stay ordinary USAGE. */static const char *py_exact_callable_target(PyLSPContext *ctx, TSNode node) {- if (!ctx || ts_node_is_null(node))+ if (!ctx || ctx->callable_value_proof_disabled || ts_node_is_null(node))return NULL;/* Parentheses do not change callable identity. Unwrap them iteratively:@@ -904,6 +969,8 @@ static const char *py_exact_callable_target(PyLSPContext *ctx, TSNode node) {}static void py_resolve_value_references_at(PyLSPContext *ctx, TSNode call) {+ if (!ctx || ctx->callable_value_proof_disabled)+ return;TSNode args = ts_node_child_by_field_name(call, "arguments", 9);if (ts_node_is_null(args))return;@@ -928,8 +995,15 @@ static void py_resolve_value_references_at(PyLSPContext *ctx, TSNode call) {const char *candidate = strcmp(kind, "identifier") == 0? py_exact_imported_reference_candidate(ctx, source_name): NULL;- if (candidate)- py_emit_unresolved_reference(ctx, candidate, arg);+ if (candidate && cbm_scope_contains(ctx->current_scope, source_name)) {+ const CBMType *binding =+ cbm_type_resolve_alias(cbm_scope_lookup(ctx->current_scope, source_name));+ if (binding && binding->kind == CBM_TYPE_NAMED &&+ binding->data.named.qualified_name &&+ strcmp(binding->data.named.qualified_name, candidate) == 0) {+ py_emit_unresolved_reference(ctx, candidate, arg);+ }+ }}}@@ -998,17 +1072,24 @@ static void py_overlay_register_field(PyLSPContext *ctx, const char *class_qn,if (ctx->field_overlay_count >= ctx->field_overlay_cap) {int new_cap = ctx->field_overlay_cap == 0 ? 16 : ctx->field_overlay_cap * 2;void *na = cbm_arena_alloc(ctx->arena, (size_t)new_cap * sizeof(*ctx->field_overlay));- if (!na)- return; /* OOM: drop this field (resolution degrades, never corrupts) */+ if (!na) {+ py_disable_callable_value_proof(ctx);+ return;+ }if (ctx->field_overlay && ctx->field_overlay_count > 0)memcpy(na, ctx->field_overlay,(size_t)ctx->field_overlay_count * sizeof(*ctx->field_overlay));ctx->field_overlay = na;ctx->field_overlay_cap = new_cap;}- ctx->field_overlay[ctx->field_overlay_count].class_qn = cbm_arena_strdup(ctx->arena, class_qn);- ctx->field_overlay[ctx->field_overlay_count].field_name =- cbm_arena_strdup(ctx->arena, field_name);+ const char *stored_class = cbm_arena_strdup(ctx->arena, class_qn);+ const char *stored_field = cbm_arena_strdup(ctx->arena, field_name);+ if (!stored_class || !stored_field) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ ctx->field_overlay[ctx->field_overlay_count].class_qn = stored_class;+ ctx->field_overlay[ctx->field_overlay_count].field_name = stored_field;ctx->field_overlay[ctx->field_overlay_count].field_type = field_type;ctx->field_overlay_count++;}@@ -2390,6 +2471,8 @@ static void py_process_statement(PyLSPContext *ctx, TSNode node) {/* ── Recursive walker: process statements + emit resolved_calls ── */static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {+ if (!ctx || ctx->callable_value_proof_disabled)+ return;TSNode fn = ts_node_child_by_field_name(call_node, "function", 8);if (ts_node_is_null(fn))return;@@ -2409,7 +2492,7 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4);TSNode args = ts_node_child_by_field_name(call_node, "arguments", 9);CBMScope *saved = ctx->current_scope;- ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);+ ctx->current_scope = py_scope_push_checked(ctx);// Bind each lambda param to the call-site arg's type.if (!ts_node_is_null(params) && !ts_node_is_null(args)) {uint32_t pn = ts_node_named_child_count(params);@@ -2902,34 +2985,6 @@ static bool py_block_terminates(TSNode block) {strcmp(k, "break_statement") == 0 || strcmp(k, "continue_statement") == 0;}-/* A write guarded by an if has more than one possible post-if value. Clear an- * outer exact callable identity before walking the temporary branch scopes so- * the initializer cannot leak through the control-flow join. */-static void py_invalidate_conditional_aliases(PyLSPContext *ctx, TSNode node, int depth) {- if (!ctx || ts_node_is_null(node) || depth > 64)- return;- const char *kind = ts_node_type(node);- if (strcmp(kind, "function_definition") == 0 || strcmp(kind, "class_definition") == 0 ||- strcmp(kind, "lambda") == 0 || strcmp(kind, "list_comprehension") == 0 ||- strcmp(kind, "set_comprehension") == 0 || strcmp(kind, "dictionary_comprehension") == 0 ||- strcmp(kind, "generator_expression") == 0) {- return;- }- if (strcmp(kind, "assignment") == 0) {- TSNode left = ts_node_child_by_field_name(node, "left", 4);- if (!ts_node_is_null(left) && strcmp(ts_node_type(left), "identifier") == 0) {- char *name = py_node_text(ctx, left);- if (name && cbm_scope_lookup_callable(ctx->current_scope, name))- py_scope_clear_callable(ctx, name);- }- return;- }- uint32_t count = ts_node_named_child_count(node);- for (uint32_t i = 0; i < count; i++) {- py_invalidate_conditional_aliases(ctx, ts_node_named_child(node, i), depth + 1);- }-}-/* Walk if_statement: push scope for consequence body when narrowing* applies, then recurse. If the consequence terminates (return/raise),* apply the negated narrow to the *enclosing* scope so subsequent@@ -2939,8 +2994,8 @@ static void py_walk_if_statement(PyLSPContext *ctx, TSNode if_node) {TSNode body = ts_node_child_by_field_name(if_node, "consequence", 11);TSNode alt = ts_node_child_by_field_name(if_node, "alternative", 11);- py_invalidate_conditional_aliases(ctx, body, 0);- py_invalidate_conditional_aliases(ctx, alt, 0);+ py_invalidate_possible_bindings(ctx, body, 0);+ py_invalidate_possible_bindings(ctx, alt, 0);// Walrus bindings in the condition leak into the enclosing scope.py_bind_walrus_in(ctx, cond);@@ -2983,7 +3038,7 @@ static void py_walk_if_statement(PyLSPContext *ctx, TSNode if_node) {// Consequence: maybe narrow.if (!ts_node_is_null(body)) {CBMScope *saved = ctx->current_scope;- ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);+ ctx->current_scope = py_scope_push_checked(ctx);// Try isinstance narrowing.if (!ts_node_is_null(cond) && strcmp(ts_node_type(cond), "call") == 0) {@@ -3181,7 +3236,7 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {if (strcmp(ts_node_type(case_clause), "case_clause") != 0)continue;CBMScope *saved = ctx->current_scope;- ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);+ ctx->current_scope = py_scope_push_checked(ctx);// Pattern is the first non-block named child; consequence// is the block.@@ -3326,7 +3381,7 @@ static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {if (strcmp(k, "list_comprehension") == 0 || strcmp(k, "dictionary_comprehension") == 0 ||strcmp(k, "set_comprehension") == 0 || strcmp(k, "generator_expression") == 0) {CBMScope *saved = ctx->current_scope;- ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);+ ctx->current_scope = py_scope_push_checked(ctx);uint32_t cnc = ts_node_named_child_count(node);// First pass: bind for-clause vars (process in source order so// chained comprehensions like `for x in xs for y in x.ys` see@@ -3939,7 +3994,7 @@ static void py_process_function(PyLSPContext *ctx, TSNode func_node, const charctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", base_qn, fname);CBMScope *saved = ctx->current_scope;- ctx->current_scope = cbm_scope_push(ctx->arena, ctx->current_scope);+ ctx->current_scope = py_scope_push_checked(ctx);TSNode params = ts_node_child_by_field_name(func_node, "parameters", 10);py_bind_parameters(ctx, params);@@ -4068,43 +4123,513 @@ static void py_process_class(PyLSPContext *ctx, TSNode class_node) {ctx->enclosing_class_qn = prev_class;}-/* Bind every Class / Type definition from the registry into the root scope- * as NAMED(qn). Lets bare references like `Foo()` and `c: Foo` resolve to- * the registered class type. */-static void py_bind_module_classes(PyLSPContext *ctx) {+/* Module bindings are replayed in source order before deferred function bodies+ * are analyzed. Class definitions always replace an earlier callable identity;+ * the registry decides whether the resulting class type is known precisely. */+static bool py_root_defines_class_named(PyLSPContext *ctx, TSNode root, const char *name) {+ uint32_t count = ts_node_named_child_count(root);+ for (uint32_t i = 0; i < count; i++) {+ TSNode node = ts_node_named_child(root, i);+ const char *kind = ts_node_type(node);+ if (strcmp(kind, "decorated_definition") == 0) {+ node = ts_node_child_by_field_name(node, "definition", 10);+ kind = ts_node_is_null(node) ? "" : ts_node_type(node);+ }+ if (strcmp(kind, "class_definition") != 0)+ continue;+ TSNode name_node = ts_node_child_by_field_name(node, "name", 4);+ if (py_import_node_text_equals(ctx, name_node, name))+ return true;+ }+ return false;+}++/* A project registry can contain another file's class in the same logical+ * Python module. Keep those cross-file globals available, but do not prebind+ * classes declared by this source: their binding epoch belongs in the ordered+ * replay below. */+static void py_bind_external_module_classes(PyLSPContext *ctx, TSNode root) {if (!ctx || !ctx->registry || !ctx->module_qn)return;- const CBMRegisteredType *types = ctx->registry->types;- int n = ctx->registry->type_count;size_t prefix_len = strlen(ctx->module_qn);- for (int i = 0; i < n; i++) {- const char *qn = types[i].qualified_name;- const char *sname = types[i].short_name;- if (!qn || !sname)+ for (int i = 0; i < ctx->registry->type_count; i++) {+ const CBMRegisteredType *type = &ctx->registry->types[i];+ const char *qn = type->qualified_name;+ const char *name = type->short_name;+ if (!qn || !name || strncmp(qn, ctx->module_qn, prefix_len) != 0 ||+ qn[prefix_len] != '.' || py_root_defines_class_named(ctx, root, name)) {+ continue;+ }+ py_scope_bind(ctx, name, cbm_type_named(ctx->arena, qn));+ }+}++static void py_bind_module_class(PyLSPContext *ctx, TSNode class_node) {+ if (!ctx || ts_node_is_null(class_node) || !ctx->module_qn)+ return;+ TSNode name_node = ts_node_child_by_field_name(class_node, "name", 4);+ char *name = py_node_text(ctx, name_node);+ if (!name || !name[0]) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, name);+ if (!qn) {+ py_disable_callable_value_proof(ctx);+ py_scope_bind(ctx, name, cbm_type_unknown());+ return;+ }+ const CBMRegisteredType *type = cbm_registry_lookup_type(ctx->registry, qn);+ py_scope_bind(ctx, name,+ type ? cbm_type_named(ctx->arena, type->qualified_name) : cbm_type_unknown());+}++/* Preserve the historical registry fallback for an unshadowed function: it+ * keeps ordinary direct-call resolution stable. When a definition actually+ * replaces a prior binding (import, wildcard uncertainty, class, assignment),+ * record the new exact callable value or clear the stale identity. */+static void py_bind_module_function(PyLSPContext *ctx, TSNode func_node) {+ if (!ctx || ts_node_is_null(func_node) || !ctx->module_qn)+ return;+ TSNode name_node = ts_node_child_by_field_name(func_node, "name", 4);+ char *name = py_node_text(ctx, name_node);+ if (!name || !name[0]) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ if (!cbm_scope_contains(ctx->current_scope, name))+ return;++ const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, name);+ if (!qn) {+ py_disable_callable_value_proof(ctx);+ py_scope_bind(ctx, name, cbm_type_unknown());+ return;+ }+ const CBMRegisteredFunc *func = cbm_registry_lookup_func(ctx->registry, qn);+ if (py_func_is_exact_callable_value(func)) {+ py_scope_bind_callable(ctx, name, cbm_type_unknown(), func->qualified_name);+ } else {+ py_scope_bind(ctx, name, cbm_type_unknown());+ }+}++static bool py_import_statement_is_wildcard(TSNode stmt) {+ uint32_t count = ts_node_named_child_count(stmt);+ for (uint32_t i = 0; i < count; i++) {+ if (strcmp(ts_node_type(ts_node_named_child(stmt, i)), "wildcard_import") == 0)+ return true;+ }+ return false;+}++static void py_invalidate_registry_module_functions(PyLSPContext *ctx,+ const CBMTypeRegistry *registry) {+ if (!ctx || !registry || !ctx->module_qn)+ return;+ size_t prefix_len = strlen(ctx->module_qn);+ for (int i = 0; i < registry->func_count; i++) {+ const CBMRegisteredFunc *func = ®istry->funcs[i];+ const char *qn = func->qualified_name;+ if (!qn || func->receiver_type || strncmp(qn, ctx->module_qn, prefix_len) != 0 ||+ qn[prefix_len] != '.') {continue;- if (strncmp(qn, ctx->module_qn, prefix_len) != 0)+ }+ const char *suffix = qn + prefix_len + 1;+ if (!suffix[0] || strchr(suffix, '.'))continue;- if (qn[prefix_len] != '.')+ py_scope_bind(ctx, func->short_name ? func->short_name : suffix, cbm_type_unknown());+ }+ py_invalidate_registry_module_functions(ctx, registry->fallback);+}++/* `from module import *` may overwrite any module global through `__all__`.+ * Without the imported module's export set, retaining any callable identity is+ * unsound. Clear current bindings and install UNKNOWN shadows for registry-only+ * local functions so exact lookup cannot fall through around the wildcard. */+static void py_invalidate_module_bindings_for_wildcard(PyLSPContext *ctx) {+ if (!ctx || !ctx->current_scope)+ return;+ for (CBMScopeChunk *chunk = ctx->current_scope->chunks; chunk; chunk = chunk->next) {+ for (int i = 0; i < chunk->used; i++) {+ const char *name = chunk->bindings[i].name;+ if (name)+ py_scope_bind(ctx, name, cbm_type_unknown());+ }+ }+ py_invalidate_registry_module_functions(ctx, ctx->registry);+}++static char *py_import_item_local_name(PyLSPContext *ctx, TSNode item, bool from_import) {+ const char *kind = ts_node_type(item);+ if (strcmp(kind, "aliased_import") == 0) {+ TSNode alias = ts_node_child_by_field_name(item, "alias", 5);+ return py_import_node_text_dup(ctx, alias);+ }+ if (strcmp(kind, "identifier") != 0 && strcmp(kind, "dotted_name") != 0)+ return NULL;+ char *name = py_import_node_text_dup(ctx, item);+ if (!name || from_import)+ return name;+ char *dot = strchr(name, '.');+ if (dot)+ *dot = '\0';+ return name;+}++/* Invalidate one syntactic binding target without treating attribute or+ * subscript writes as rebinding the same-spelled module global. Pattern+ * containers are walked recursively; a one-component dotted_name is the+ * tree-sitter shape used by Python capture patterns. */+static void py_invalidate_binding_target(PyLSPContext *ctx, TSNode target, int depth) {+ if (!ctx || ts_node_is_null(target))+ return;+ if (depth > 64) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ const char *kind = ts_node_type(target);+ if (strcmp(kind, "identifier") == 0) {+ char *name = py_node_text(ctx, target);+ if (!name) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ py_scope_bind(ctx, name, cbm_type_unknown());+ return;+ }+ if (strcmp(kind, "attribute") == 0 || strcmp(kind, "subscript") == 0)+ return;+ uint32_t count = ts_node_named_child_count(target);+ if (strcmp(kind, "dotted_name") == 0 && count != 1)+ return;+ for (uint32_t i = 0; i < count; i++)+ py_invalidate_binding_target(ctx, ts_node_named_child(target, i), depth + 1);+}++/* Match patterns mix value expressions and capture targets. Skip the class+ * expression in `case Type(...)` and keyword labels, while invalidating every+ * nested capture/as/star target that may replace a module binding. */+static void py_invalidate_match_pattern(PyLSPContext *ctx, TSNode pattern, int depth) {+ if (!ctx || ts_node_is_null(pattern))+ return;+ if (depth > 64) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ const char *kind = ts_node_type(pattern);+ uint32_t count = ts_node_named_child_count(pattern);+ if (strcmp(kind, "class_pattern") == 0) {+ for (uint32_t i = 1; i < count; i++)+ py_invalidate_match_pattern(ctx, ts_node_named_child(pattern, i), depth + 1);+ return;+ }+ if (strcmp(kind, "keyword_pattern") == 0) {+ if (count > 0)+ py_invalidate_match_pattern(ctx, ts_node_named_child(pattern, count - 1), depth + 1);+ return;+ }+ if (strcmp(kind, "attribute") == 0)+ return;+ if (strcmp(kind, "dotted_name") == 0) {+ if (count == 1)+ py_invalidate_binding_target(ctx, ts_node_named_child(pattern, 0), depth + 1);+ return;+ }+ if (strcmp(kind, "identifier") == 0) {+ py_invalidate_binding_target(ctx, pattern, depth + 1);+ return;+ }+ for (uint32_t i = 0; i < count; i++)+ py_invalidate_match_pattern(ctx, ts_node_named_child(pattern, i), depth + 1);+}++static void py_invalidate_import_bindings(PyLSPContext *ctx, TSNode stmt) {+ if (py_import_statement_is_wildcard(stmt)) {+ py_invalidate_module_bindings_for_wildcard(ctx);+ return;+ }+ bool from_import = strcmp(ts_node_type(stmt), "import_from_statement") == 0;+ TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};+ uint32_t count = ts_node_named_child_count(stmt);+ for (uint32_t i = 0; i < count; i++) {+ TSNode item = ts_node_named_child(stmt, i);+ if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))continue;- py_scope_bind(ctx, sname, cbm_type_named(ctx->arena, qn));+ const char *kind = ts_node_type(item);+ char *local = py_import_item_local_name(ctx, item, from_import);+ if (local) {+ py_scope_bind(ctx, local, cbm_type_unknown());+ } else if (strcmp(kind, "aliased_import") == 0 || strcmp(kind, "identifier") == 0 ||+ strcmp(kind, "dotted_name") == 0) {+ py_disable_callable_value_proof(ctx);+ }}}+/* Conservatively join possible binding effects from module/function compound+ * statements. Only names that occur in a binding position lose exact callable+ * identity. Nested lexical bodies are not walked after their definition name,+ * and annotation-only assignments do not replace a runtime value. */+static void py_invalidate_possible_bindings(PyLSPContext *ctx, TSNode node, int depth) {+ if (!ctx || ts_node_is_null(node))+ return;+ if (depth > 64) {+ py_disable_callable_value_proof(ctx);+ return;+ }+ const char *kind = ts_node_type(node);+ if (strcmp(kind, "lambda") == 0 || strcmp(kind, "list_comprehension") == 0 ||+ strcmp(kind, "set_comprehension") == 0 ||+ strcmp(kind, "dictionary_comprehension") == 0 ||+ strcmp(kind, "generator_expression") == 0) {+ return;+ }+ if (strcmp(kind, "function_definition") == 0 || strcmp(kind, "class_definition") == 0) {+ TSNode name = ts_node_child_by_field_name(node, "name", 4);+ if (ts_node_is_null(name))+ py_disable_callable_value_proof(ctx);+ else+ py_invalidate_binding_target(ctx, name, depth + 1);+ return;+ }+ if (strcmp(kind, "decorated_definition") == 0) {+ TSNode definition = ts_node_child_by_field_name(node, "definition", 10);+ if (ts_node_is_null(definition))+ py_disable_callable_value_proof(ctx);+ else+ py_invalidate_possible_bindings(ctx, definition, depth + 1);+ return;+ }+ if (strcmp(kind, "import_statement") == 0 ||+ strcmp(kind, "import_from_statement") == 0) {+ py_invalidate_import_bindings(ctx, node);+ return;+ }+ if (strcmp(kind, "assignment") == 0) {+ TSNode right = ts_node_child_by_field_name(node, "right", 5);+ if (ts_node_is_null(right))+ return;+ py_invalidate_binding_target(ctx, ts_node_child_by_field_name(node, "left", 4),+ depth + 1);+ py_invalidate_possible_bindings(ctx, right, depth + 1);+ return;+ }+ if (strcmp(kind, "augmented_assignment") == 0) {+ TSNode left = ts_node_child_by_field_name(node, "left", 4);+ TSNode right = ts_node_child_by_field_name(node, "right", 5);+ py_invalidate_binding_target(ctx, left, depth + 1);+ py_invalidate_possible_bindings(ctx, right, depth + 1);+ return;+ }+ if (strcmp(kind, "named_expression") == 0 ||+ strcmp(kind, "assignment_expression") == 0) {+ TSNode name = ts_node_child_by_field_name(node, "name", 4);+ if (ts_node_is_null(name))+ name = ts_node_child_by_field_name(node, "left", 4);+ TSNode value = ts_node_child_by_field_name(node, "value", 5);+ if (ts_node_is_null(value))+ value = ts_node_child_by_field_name(node, "right", 5);+ py_invalidate_binding_target(ctx, name, depth + 1);+ py_invalidate_possible_bindings(ctx, value, depth + 1);+ return;+ }+ if (strcmp(kind, "delete_statement") == 0) {+ uint32_t count = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < count; i++)+ py_invalidate_binding_target(ctx, ts_node_named_child(node, i), depth + 1);+ return;+ }+ if (strcmp(kind, "for_statement") == 0) {+ TSNode left = ts_node_child_by_field_name(node, "left", 4);+ py_invalidate_binding_target(ctx, left, depth + 1);+ uint32_t count = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < count; i++) {+ TSNode child = ts_node_named_child(node, i);+ if (!ts_node_eq(child, left))+ py_invalidate_possible_bindings(ctx, child, depth + 1);+ }+ return;+ }+ if (strcmp(kind, "as_pattern") == 0) {+ TSNode alias = ts_node_child_by_field_name(node, "alias", 5);+ uint32_t count = ts_node_named_child_count(node);+ if (ts_node_is_null(alias) && count > 1)+ alias = ts_node_named_child(node, count - 1);+ py_invalidate_binding_target(ctx, alias, depth + 1);+ for (uint32_t i = 0; i < count; i++) {+ TSNode child = ts_node_named_child(node, i);+ if (!ts_node_eq(child, alias))+ py_invalidate_possible_bindings(ctx, child, depth + 1);+ }+ return;+ }+ if (strcmp(kind, "match_statement") == 0) {+ TSNode subject = ts_node_child_by_field_name(node, "subject", 7);+ TSNode body = ts_node_child_by_field_name(node, "body", 4);+ py_invalidate_possible_bindings(ctx, subject, depth + 1);+ uint32_t cases = ts_node_named_child_count(body);+ for (uint32_t i = 0; i < cases; i++) {+ TSNode clause = ts_node_named_child(body, i);+ if (strcmp(ts_node_type(clause), "case_clause") != 0)+ continue;+ TSNode pattern = {0};+ uint32_t count = ts_node_named_child_count(clause);+ for (uint32_t j = 0; j < count; j++) {+ TSNode child = ts_node_named_child(clause, j);+ if (ts_node_is_null(pattern) && strcmp(ts_node_type(child), "block") != 0) {+ pattern = child;+ py_invalidate_match_pattern(ctx, pattern, depth + 1);+ } else if (!ts_node_eq(child, pattern)) {+ py_invalidate_possible_bindings(ctx, child, depth + 1);+ }+ }+ }+ return;+ }+ uint32_t count = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < count; i++)+ py_invalidate_possible_bindings(ctx, ts_node_named_child(node, i), depth + 1);+}++static bool py_replayable_import_kind(PyDirectImportKind kind) {+ return kind == PY_DIRECT_IMPORT_UNALIASED || kind == PY_DIRECT_IMPORT_ALIASED ||+ kind == PY_FROM_IMPORT;+}++/* Replay one syntactic local-binding occurrence. UNKNOWN is installed first,+ * so missing, conflicting, or project-prefix-ambiguous metadata fails closed.+ * Exactly one canonical target may then upgrade that occurrence. */+static void py_replay_import_local(PyLSPContext *ctx, TSNode stmt, const char *local,+ unsigned char *consumed) {+ if (!ctx || !local || !local[0])+ return;+ py_scope_bind(ctx, local, cbm_type_unknown());+ if (ctx->import_count > 0 && !consumed)+ return;++ int chosen = -1;+ PyDirectImportKind chosen_kind = PY_IMPORT_UNCLASSIFIED;+ const char *chosen_qn = NULL;+ bool conflicting_target = false;+ for (int i = 0; i < ctx->import_count; i++) {+ if ((consumed && consumed[i]) || !ctx->import_local_names[i] ||+ !ctx->import_module_qns[i] || strcmp(ctx->import_local_names[i], local) != 0) {+ continue;+ }+ const char *candidate_qn = ctx->import_module_qns[i];+ PyDirectImportKind kind =+ py_import_kind_from_statement(ctx, stmt, local, &candidate_qn);+ if (!py_replayable_import_kind(kind))+ continue;+ if (!chosen_qn) {+ chosen = i;+ chosen_kind = kind;+ chosen_qn = candidate_qn;+ } else if (strcmp(chosen_qn, candidate_qn) != 0 || chosen_kind != kind) {+ conflicting_target = true;+ }+ }+ if (chosen < 0 || conflicting_target)+ return;++ ctx->import_module_qns[chosen] = chosen_qn;+ if (ctx->import_kinds)+ ctx->import_kinds[chosen] = (unsigned char)chosen_kind;+ if (consumed)+ consumed[chosen] = 1;+ py_bind_import_index(ctx, chosen, false);+}++static void py_replay_import_statement(PyLSPContext *ctx, TSNode stmt,+ unsigned char *consumed) {+ if (py_import_statement_is_wildcard(stmt)) {+ py_invalidate_module_bindings_for_wildcard(ctx);+ return;+ }+ bool from_import = strcmp(ts_node_type(stmt), "import_from_statement") == 0;+ TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};+ uint32_t count = ts_node_named_child_count(stmt);+ for (uint32_t i = 0; i < count; i++) {+ TSNode item = ts_node_named_child(stmt, i);+ if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))+ continue;+ char *local = py_import_item_local_name(ctx, item, from_import);+ if (local) {+ py_replay_import_local(ctx, stmt, local, consumed);+ } else {+ const char *kind = ts_node_type(item);+ if (strcmp(kind, "aliased_import") == 0 || strcmp(kind, "identifier") == 0 ||+ strcmp(kind, "dotted_name") == 0) {+ py_disable_callable_value_proof(ctx);+ }+ }+ }+}++static bool py_expression_is_annotation_only_assignment(TSNode statement) {+ if (ts_node_named_child_count(statement) != 1)+ return false;+ TSNode expression = ts_node_named_child(statement, 0);+ if (strcmp(ts_node_type(expression), "assignment") != 0)+ return false;+ TSNode annotation = ts_node_child_by_field_name(expression, "type", 4);+ TSNode value = ts_node_child_by_field_name(expression, "right", 5);+ return !ts_node_is_null(annotation) && ts_node_is_null(value);+}+void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {if (!ctx || ts_node_is_null(root))return;- py_lsp_bind_imports_for_root(ctx, root);- py_bind_module_classes(ctx);+ py_classify_imports_for_root(ctx, root);+ py_bind_external_module_classes(ctx, root);+ unsigned char *consumed_imports = NULL;+ if (ctx->import_count > 0) {+ consumed_imports =+ (unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)ctx->import_count);+ if (consumed_imports)+ memset(consumed_imports, 0, (size_t)ctx->import_count);+ }uint32_t nc = ts_node_named_child_count(root);- // Pass 1: top-level assignments bind into module scope.+ const char *prev_func = ctx->enclosing_func_qn;+ ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.__module__", ctx->module_qn);+ // Pass 1: execute top-level binding effects in source order. Function+ // bodies remain deferred until the final module scope has been assembled.for (uint32_t i = 0; i < nc; i++) {TSNode c = ts_node_named_child(root, i);- py_process_statement(ctx, c);+ const char *ck = ts_node_type(c);+ if (strcmp(ck, "import_statement") == 0 ||+ strcmp(ck, "import_from_statement") == 0) {+ py_replay_import_statement(ctx, c, consumed_imports);+ } else if (strcmp(ck, "function_definition") == 0) {+ py_bind_module_function(ctx, c);+ } else if (strcmp(ck, "class_definition") == 0) {+ py_bind_module_class(ctx, c);+ } else if (strcmp(ck, "decorated_definition") == 0) {+ TSNode def = ts_node_child_by_field_name(c, "definition", 10);+ if (!ts_node_is_null(def)) {+ const char *dk = ts_node_type(def);+ if (strcmp(dk, "function_definition") == 0)+ py_bind_module_function(ctx, def);+ else if (strcmp(dk, "class_definition") == 0)+ py_bind_module_class(ctx, def);+ }+ } else if (strcmp(ck, "expression_statement") == 0) {+ /* The recursive walker reaches a wrapped assignment and applies+ * its binding exactly once here, at module execution time. It also+ * preserves top-level call edges without replaying the assignment+ * later against the final module scope. */+ if (!py_expression_is_annotation_only_assignment(c))+ py_resolve_calls_in(ctx, c);+ } else {+ /* A compound statement can leave several possible module values.+ * Invalidate only syntactic binding targets at the control-flow+ * join; a later unconditional statement may restore exact proof. */+ py_invalidate_possible_bindings(ctx, c, 0);+ }}// Pass 2: top-level calls (rare) and nested definitions.- const char *prev_func = ctx->enclosing_func_qn;- ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.__module__", ctx->module_qn);for (uint32_t i = 0; i < nc; i++) {TSNode c = ts_node_named_child(root, i);const char *ck = ts_node_type(c);@@ -4122,9 +4647,6 @@ void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {} else if (strcmp(dk, "class_definition") == 0) {py_process_class(ctx, def);}- } else if (strcmp(ck, "expression_statement") == 0) {- // Top-level call statements- py_resolve_calls_in(ctx, c);}}ctx->enclosing_func_qn = prev_func;diff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.hindex 8f6401a1e..96a85dc5d 100644--- a/internal/cbm/lsp/py_lsp.h+++ b/internal/cbm/lsp/py_lsp.h@@ -100,6 +100,12 @@ typedef struct {int eval_steps; // per-file work budget used (PY_EVAL_MAX_STEPS_PER_FILE)uint32_t eval_truncations; // depth/budget cutoff count — gates memo inserts+ // Sticky fail-closed guard for exact callable-value proof. If a required+ // scope/name/overlay allocation fails, later semantic passes must leave+ // the syntactic occurrence as ordinary USAGE instead of consulting stale+ // bindings and fabricating CALL_REFERENCE/CALLS edges.+ bool callable_value_proof_disabled;+// Per-file instance-field OVERLAY. When the Tier-2 registry is shared + sealed// (registry->read_only), `self.x = ...` / PEP-526 field discoveries made during// resolve are recorded HERE instead of mutating the shared registry (which woulddiff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex 083a7b3a8..b77df854e 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -374,9 +374,9 @@ static const tool_def_t TOOLS[] = {"The three modes are independent and can be combined in a single call. ""RESPONSE: prefix-grouped tree rows by default — a shared (qn-prefix, file) group ""header printed once, then `name label lines in out` per row (full qn = group prefix "- "+ dot + name). in/out = TOTAL degree across ALL edge types (DEFINES, "- "USAGE, CALLS, CALL_REFERENCE, ...), NOT caller/callee counts — use trace_path for callers. "- "Add per-node "+ "+ dot + name). in/out = selected degree across CALLS, USAGE, CALL_REFERENCE, "+ "INHERITS, and IMPLEMENTS; other edge types are excluded. These are NOT caller/callee "+ "counts — use trace_path for callers. Add per-node ""property columns via ""fields (e.g. [\"complexity\",\"signature\",\"docstring\"]); format=\"json\" returns ""the SAME tree model as structured JSON. "diff --git a/tests/repro/repro_reference_precision.c b/tests/repro/repro_reference_precision.cindex 63558e3e9..f0589a4b2 100644--- a/tests/repro/repro_reference_precision.c+++ b/tests/repro/repro_reference_precision.c@@ -1357,6 +1357,617 @@ TEST(repro_python_alias_equal_module_leaf_targets_imported_member) {PASS();}+static int rp_assert_python_callback_occurrence_stays_usage(const char *source,+ const char *caller) {+ CBMFileResult *raw = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "repro",+ "use.py", 0, NULL, NULL);+ ASSERT_NOT_NULL(raw);+ ASSERT_FALSE(raw->has_error || raw->parse_incomplete);+ const char *argument_call = NULL;+ const char *scan = source;+ while ((scan = strstr(scan, "accept(callback)")) != NULL) {+ argument_call = scan;+ scan++;+ }+ ASSERT_NOT_NULL(argument_call);+ uint32_t site_start = (uint32_t)(argument_call - source) + 7U;+ uint32_t site_end = site_start + (uint32_t)strlen("callback");+ int ordinary_usage = 0;+ int promoted_usage = 0;+ int precise_reference = 0;+ for (int i = 0; i < raw->usages.count; i++) {+ const CBMUsage *usage = &raw->usages.items[i];+ if (!usage->ref_name || strcmp(usage->ref_name, "callback") != 0 ||+ !usage->enclosing_func_qn ||+ !rp_qn_ends_with(usage->enclosing_func_qn, caller) ||+ usage->site_start_byte != site_start || usage->site_end_byte != site_end)+ continue;+ if (usage->kind == CBM_USAGE_VALUE)+ ordinary_usage++;+ else if (usage->kind == CBM_USAGE_CALL_REFERENCE)+ promoted_usage++;+ }+ for (int i = 0; i < raw->resolved_calls.count; i++) {+ const CBMResolvedCall *resolved = &raw->resolved_calls.items[i];+ if (resolved->kind == CBM_RESOLVED_CALL_REFERENCE && resolved->caller_qn &&+ rp_qn_ends_with(resolved->caller_qn, caller) &&+ resolved->site_start_byte == site_start && resolved->site_end_byte == site_end) {+ precise_reference++;+ }+ }+ cbm_free_result(raw);++ ASSERT_GTE(ordinary_usage, 1);+ ASSERT_EQ(promoted_usage, 0);+ ASSERT_EQ(precise_reference, 0);+ return 0;+}++static int rp_assert_python_imported_callback_fails_closed(const char *source) {+ int raw_rc = rp_assert_python_callback_occurrence_stays_usage(source, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;++ const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", source},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python module-binding fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "accept", ".use.accept");+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", NULL, NULL, NULL);+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ int fabricated_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "handler", ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 0);+ ASSERT_EQ(imported_reference, 0);+ ASSERT_EQ(fabricated_call, 0);+ return 0;+}++static int rp_assert_python_exact_reference_occurrence(const char *filename, const char *source,+ const char *marker,+ const char *site_text,+ const char *ref_name) {+ CBMFileResult *raw = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "repro",+ filename, 0, NULL, NULL);+ ASSERT_NOT_NULL(raw);+ ASSERT_FALSE(raw->has_error || raw->parse_incomplete);++ const char *marker_start = strstr(source, marker);+ ASSERT_NOT_NULL(marker_start);+ const char *site = strstr(marker_start, site_text);+ ASSERT_TRUE(site && site + strlen(site_text) <= marker_start + strlen(marker));+ uint32_t site_start = (uint32_t)(site - source);+ uint32_t site_end = site_start + (uint32_t)strlen(site_text);++ const CBMUsage *carrier = NULL;+ int carrier_count = 0;+ int target_reference_rows = 0;+ int target_invocation_rows = 0;+ for (int i = 0; i < raw->usages.count; i++) {+ const CBMUsage *usage = &raw->usages.items[i];+ if (usage->kind == CBM_USAGE_VALUE && usage->ref_name &&+ strcmp(usage->ref_name, ref_name) == 0 && usage->enclosing_func_qn &&+ rp_qn_ends_with(usage->enclosing_func_qn, "argument") &&+ usage->site_start_byte == site_start && usage->site_end_byte == site_end) {+ carrier = usage;+ carrier_count++;+ }+ }+ for (int i = 0; i < raw->resolved_calls.count; i++) {+ const CBMResolvedCall *resolved = &raw->resolved_calls.items[i];+ if (!resolved->caller_qn || !rp_qn_ends_with(resolved->caller_qn, "argument") ||+ !resolved->callee_qn || !rp_qn_ends_with(resolved->callee_qn, ".handler") ||+ resolved->site_start_byte != site_start || resolved->site_end_byte != site_end) {+ continue;+ }+ target_reference_rows += resolved->kind == CBM_RESOLVED_CALL_REFERENCE;+ target_invocation_rows += resolved->kind == CBM_RESOLVED_INVOCATION;+ }+ const CBMResolvedCall *joined =+ carrier ? cbm_pipeline_find_lsp_reference(&raw->resolved_calls, carrier, false) : NULL;+ bool carrier_is_candidate = carrier && carrier->may_be_call_reference;+ bool exact_join = joined && joined->kind == CBM_RESOLVED_CALL_REFERENCE &&+ joined->callee_qn && rp_qn_ends_with(joined->callee_qn, ".handler");+ cbm_free_result(raw);++ ASSERT_EQ(carrier_count, 1);+ ASSERT_TRUE(carrier_is_candidate);+ ASSERT_EQ(target_reference_rows, 1);+ ASSERT_EQ(target_invocation_rows, 0);+ ASSERT_TRUE(exact_join);+ return rp_assert_exact_function_value_reference(filename, source, CBM_LANG_PYTHON,+ "Function");+}++TEST(repro_python_module_walrus_shadow_fails_closed) {+ static const char source[] = "from target import handler as callback\n"+ "(callback := 0)\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n";+ int rc = rp_assert_python_imported_callback_fails_closed(source);+ if (rc != 0)+ return rc;+ PASS();+}++/* A comprehension assignment expression binds in the containing scope, not+ * the comprehension's implicit scope. */+TEST(repro_python_module_comprehension_walrus_shadow_fails_closed) {+ static const char source[] = "from target import handler as callback\n"+ "[(callback := 0) for _ in (0,)]\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n";+ int rc = rp_assert_python_imported_callback_fails_closed(source);+ if (rc != 0)+ return rc;+ PASS();+}++TEST(repro_python_module_type_alias_shadow_fails_closed) {+ static const char source[] = "from target import handler as callback\n"+ "type callback = int\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n";+ int rc = rp_assert_python_imported_callback_fails_closed(source);+ if (rc != 0)+ return rc;+ PASS();+}++TEST(repro_python_parenthesized_function_value_is_exact_reference) {+ static const char source[] = "def handler():\n"+ " pass\n"+ "def accept(fn):\n"+ " pass\n"+ "def argument():\n"+ " accept((handler))\n";+ int rc = rp_assert_python_exact_reference_occurrence(+ "parenthesized_reference.py", source, "accept((handler))", "((handler))", "handler");+ if (rc != 0)+ return rc;+ PASS();+}++TEST(repro_python_local_callable_alias_value_is_exact_reference) {+ static const char source[] = "def handler():\n"+ " pass\n"+ "def accept(fn):\n"+ " pass\n"+ "def argument():\n"+ " callback = handler\n"+ " accept(callback)\n";+ int rc = rp_assert_python_exact_reference_occurrence(+ "local_alias_reference.py", source, "accept(callback)", "callback", "callback");+ if (rc != 0)+ return rc;+ PASS();+}++/* Raw lexical coverage for this occurrence lives in+ * repro_python_assignment_blocks_earlier_use_for_whole_function. This graph+ * control proves that a semantic row cannot bypass that local-shadow guard. */+TEST(repro_python_read_before_local_assignment_has_no_graph_reference) {+ static const char source[] = "def handler():\n"+ " pass\n"+ "def accept(fn):\n"+ " pass\n"+ "def argument():\n"+ " accept(handler)\n"+ " handler = 0\n";+ ASSERT_TRUE(rp_extracts_clean(source, CBM_LANG_PYTHON, "read_before_write.py"));++ RProj project;+ cbm_store_t *store = rh_index(&project, "read_before_write.py", source);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python read-before-write fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "argument", "Function",+ "accept", NULL);+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE", "argument",+ NULL, NULL, NULL);+ int fabricated_call = rp_edge_count(store, project.project, "CALLS", "argument", "Function",+ "handler", NULL);+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 0);+ ASSERT_EQ(fabricated_call, 0);+ PASS();+}++TEST(repro_python_local_function_shadows_imported_callable) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "def callback():\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python local-function shadow fixture did not produce a graph store");+ }+ int correct_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int wrong_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(correct_reference, 1);+ ASSERT_EQ(wrong_reference, 0);+ PASS();+}++TEST(repro_python_alias_capture_precedes_later_import_rebinding) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "def callback():\n pass\n"+ "alias = callback\n"+ "from target import handler as callback\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(alias)\n"},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python ordered alias-capture fixture did not produce a graph store");+ }+ int correct_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int wrong_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(correct_reference, 1);+ ASSERT_EQ(wrong_reference, 0);+ PASS();+}++TEST(repro_python_later_class_clears_function_value_identity) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "def callback():\n pass\n"+ "class callback:\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ int raw_rc =+ rp_assert_python_callback_occurrence_stays_usage(files[1].content, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python class-rebinding fixture did not produce a graph store");+ }+ int local_function_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(local_function_reference, 0);+ ASSERT_EQ(imported_reference, 0);+ PASS();+}++TEST(repro_python_later_wildcard_import_clears_local_function_proof) {+ static const RFile files[] = {+ {"target.py", "def callback():\n pass\n"},+ {"use.py", "def callback():\n pass\n"+ "from target import *\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ int raw_rc =+ rp_assert_python_callback_occurrence_stays_usage(files[1].content, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python wildcard-rebinding fixture did not produce a graph store");+ }+ int local_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".target.callback");+ rh_cleanup(&project, store);++ ASSERT_EQ(local_reference, 0);+ ASSERT_EQ(imported_reference, 0);+ PASS();+}++TEST(repro_python_decorated_local_shadow_fails_closed) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "def replace(fn):\n return object()\n"+ "@replace\n"+ "def callback():\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ int raw_rc =+ rp_assert_python_callback_occurrence_stays_usage(files[1].content, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python decorated-shadow fixture did not produce a graph store");+ }+ int local_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(local_reference, 0);+ ASSERT_EQ(imported_reference, 0);+ PASS();+}++TEST(repro_python_module_if_assignment_shadow_fails_closed) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "if True:\n callback = 0\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ int raw_rc =+ rp_assert_python_callback_occurrence_stays_usage(files[1].content, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python conditional-assignment shadow fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "accept", ".use.accept");+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", NULL, NULL, NULL);+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ int fabricated_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "handler", ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 0);+ ASSERT_EQ(imported_reference, 0);+ ASSERT_EQ(fabricated_call, 0);+ PASS();+}++TEST(repro_python_module_if_definition_shadow_fails_closed) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "if True:\n"+ " def callback():\n"+ " pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ int raw_rc =+ rp_assert_python_callback_occurrence_stays_usage(files[1].content, "crossArgument");+ if (raw_rc != 0)+ return raw_rc;+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python conditional-definition shadow fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "accept", ".use.accept");+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", NULL, NULL, NULL);+ int local_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ int local_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "callback", ".use.callback");+ int imported_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "handler", ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 0);+ ASSERT_EQ(local_reference, 0);+ ASSERT_EQ(imported_reference, 0);+ ASSERT_EQ(local_call, 0);+ ASSERT_EQ(imported_call, 0);+ PASS();+}++TEST(repro_python_later_import_restores_imported_callable_identity) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "def callback():\n pass\n"+ "from target import handler as callback\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python later-import fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "accept", ".use.accept");+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", NULL, NULL, NULL);+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ int local_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_usage = rp_edge_count(store, project.project, "USAGE", "crossArgument",+ "Function", "handler", ".target.handler");+ int fabricated_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "handler", ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 1);+ ASSERT_EQ(imported_reference, 1);+ ASSERT_EQ(local_reference, 0);+ ASSERT_EQ(imported_usage, 0);+ ASSERT_EQ(fabricated_call, 0);+ PASS();+}++TEST(repro_python_wildcard_then_local_function_restores_exactness) {+ static const RFile files[] = {+ {"target.py", "def callback():\n pass\n"},+ {"use.py", "from target import *\n"+ "def callback():\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python wildcard-then-definition fixture did not produce a graph store");+ }+ int registrar_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "accept", ".use.accept");+ int total_references = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", NULL, NULL, NULL);+ int local_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".use.callback");+ int imported_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "callback",+ ".target.callback");+ int local_usage = rp_edge_count(store, project.project, "USAGE", "crossArgument",+ "Function", "callback", ".use.callback");+ int fabricated_call = rp_edge_count(store, project.project, "CALLS", "crossArgument",+ "Function", "callback", ".use.callback");+ rh_cleanup(&project, store);++ ASSERT_EQ(registrar_call, 1);+ ASSERT_EQ(total_references, 1);+ ASSERT_EQ(local_reference, 1);+ ASSERT_EQ(imported_reference, 0);+ ASSERT_EQ(local_usage, 0);+ ASSERT_EQ(fabricated_call, 0);+ PASS();+}++TEST(repro_python_module_compound_binding_matrix_fails_closed) {+ static const char *const cases[] = {+ "from target import handler as callback\n"+ "while flag:\n callback = 0\n break\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "for callback in items:\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "with manager as callback:\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "try:\n callback = 0\nexcept Exception:\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "match value:\n case callback:\n pass\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "if flag:\n del callback\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "if flag:\n from other import alternate as callback\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ "from target import handler as callback\n"+ "if flag:\n from other import *\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n",+ };+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {+ int rc = rp_assert_python_callback_occurrence_stays_usage(cases[i], "crossArgument");+ if (rc != 0)+ return rc;+ }+ PASS();+}++TEST(repro_python_annotation_only_preserves_imported_callable_identity) {+ static const RFile files[] = {+ {"target.py", "def handler():\n pass\n"},+ {"use.py", "from target import handler as callback\n"+ "callback: object\n"+ "def accept(fn):\n pass\n"+ "def crossArgument():\n accept(callback)\n"},+ };+ RProj project;+ cbm_store_t *store = rh_index_files(&project, files, 2);+ if (!store) {+ rh_cleanup(&project, store);+ FAIL("Python annotation-only fixture did not produce a graph store");+ }+ int precise_reference = rp_edge_count(store, project.project, "CALL_REFERENCE",+ "crossArgument", "Function", "handler",+ ".target.handler");+ int ordinary_usage = rp_edge_count(store, project.project, "USAGE", "crossArgument",+ "Function", "handler", ".target.handler");+ rh_cleanup(&project, store);++ ASSERT_EQ(precise_reference, 1);+ ASSERT_EQ(ordinary_usage, 0);+ PASS();+}+TEST(repro_rust_cross_file_exact_function_value_is_call_reference) {static const RFile files[] = {{"target.rs", "pub fn handler() {}\n"},@@ -2164,6 +2775,23 @@ SUITE(repro_reference_precision) {RUN_TEST(repro_python_cross_file_exact_function_value_is_call_reference);RUN_TEST(repro_python_aliased_import_function_value_is_call_reference);RUN_TEST(repro_python_alias_equal_module_leaf_targets_imported_member);+ RUN_TEST(repro_python_module_walrus_shadow_fails_closed);+ RUN_TEST(repro_python_module_comprehension_walrus_shadow_fails_closed);+ RUN_TEST(repro_python_module_type_alias_shadow_fails_closed);+ RUN_TEST(repro_python_parenthesized_function_value_is_exact_reference);+ RUN_TEST(repro_python_local_callable_alias_value_is_exact_reference);+ RUN_TEST(repro_python_read_before_local_assignment_has_no_graph_reference);+ RUN_TEST(repro_python_local_function_shadows_imported_callable);+ RUN_TEST(repro_python_alias_capture_precedes_later_import_rebinding);+ RUN_TEST(repro_python_later_class_clears_function_value_identity);+ RUN_TEST(repro_python_later_wildcard_import_clears_local_function_proof);+ RUN_TEST(repro_python_decorated_local_shadow_fails_closed);+ RUN_TEST(repro_python_module_if_assignment_shadow_fails_closed);+ RUN_TEST(repro_python_module_if_definition_shadow_fails_closed);+ RUN_TEST(repro_python_later_import_restores_imported_callable_identity);+ RUN_TEST(repro_python_wildcard_then_local_function_restores_exactness);+ RUN_TEST(repro_python_module_compound_binding_matrix_fails_closed);+ RUN_TEST(repro_python_annotation_only_preserves_imported_callable_identity);RUN_TEST(repro_rust_cross_file_exact_function_value_is_call_reference);RUN_TEST(repro_csharp_cross_file_exact_function_value_is_call_reference);RUN_TEST(repro_csharp_cross_file_callable_alias_invocation_targets_exact_value);diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex 34178540e..f220f5487 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -968,6 +968,70 @@ typedef struct {const char *target_name;} PreciseReferencePair;+typedef struct {+ bool database_opened;+ bool query_ready;+ bool query_completed;+ int row_count;+ int valid_json_count;+ int exact_callee_count;+} NamedEdgePropertyObservation;++/* Read one named edge's persisted properties through SQLite's JSON functions.+ * The guarded json_extract is intentional: on the RED revision the parallel+ * usage emitter truncates the final `}` from a boundary-sized callee property.+ * json_valid must report that row without json_extract aborting the statement,+ * while the fixed revision must round-trip the complete callee value. */+static NamedEdgePropertyObservation observe_named_edge_callee_property(+ const char *db_path, const char *project, const char *edge_type, const char *source_name,+ const char *target_name, const char *expected_callee) {+ NamedEdgePropertyObservation observation = {0};+ sqlite3 *db = NULL;+ if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {+ if (db) {+ sqlite3_close(db);+ }+ return observation;+ }+ observation.database_opened = true;++ static const char sql[] =+ "SELECT e.properties, json_valid(e.properties), "+ "CASE WHEN json_valid(e.properties) "+ "THEN json_extract(e.properties, '$.callee') END "+ "FROM edges e "+ "JOIN nodes src ON src.id=e.source_id AND src.project=e.project "+ "JOIN nodes tgt ON tgt.id=e.target_id AND tgt.project=e.project "+ "WHERE e.project=?1 AND e.type=?2 AND src.name=?3 AND tgt.name=?4;";+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK ||+ sqlite3_bind_text(stmt, 1, project, -1, SQLITE_TRANSIENT) != SQLITE_OK ||+ sqlite3_bind_text(stmt, 2, edge_type, -1, SQLITE_TRANSIENT) != SQLITE_OK ||+ sqlite3_bind_text(stmt, 3, source_name, -1, SQLITE_TRANSIENT) != SQLITE_OK ||+ sqlite3_bind_text(stmt, 4, target_name, -1, SQLITE_TRANSIENT) != SQLITE_OK) {+ sqlite3_finalize(stmt);+ sqlite3_close(db);+ return observation;+ }+ observation.query_ready = true;++ int step_rc = SQLITE_OK;+ while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {+ observation.row_count++;+ if (sqlite3_column_int(stmt, 1) != 0) {+ observation.valid_json_count++;+ }+ const unsigned char *callee = sqlite3_column_text(stmt, 2);+ if (callee && strcmp((const char *)callee, expected_callee) == 0) {+ observation.exact_callee_count++;+ }+ }+ observation.query_completed = step_rc == SQLITE_DONE;+ sqlite3_finalize(stmt);+ sqlite3_close(db);+ return observation;+}+/* Count only edges of `edge_type` between the exact named source/target nodes.* The parity and incremental guards deliberately use unique symbol names, so* this canonicalizes across independent stores without depending on node IDs@@ -1615,6 +1679,30 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {"func goReferenceTarget() {}\n""func goReferenceAccept(callback func()) {}\n""func goReferenceSite() { goReferenceAccept(goReferenceTarget) }\n");+ /* `{"callee":""}` contributes 13 bytes. A 243-byte identifier therefore+ * produces 256 bytes of JSON content: it fits the usage emitter's escaped+ * value buffer but needs 257 bytes including NUL for the wrapper. The+ * sequential 512-byte wrapper is valid; the parallel 256-byte wrapper is+ * RED because snprintf drops the closing brace. */+ enum { LONG_REFERENCE_NAME_LEN = 243 };+ char long_reference_name[LONG_REFERENCE_NAME_LEN + 1];+ memset(long_reference_name, 'x', LONG_REFERENCE_NAME_LEN);+ long_reference_name[0] = 'l';+ long_reference_name[LONG_REFERENCE_NAME_LEN] = '\0';+ char long_reference_source[1024];+ int long_reference_source_len = snprintf(+ long_reference_source, sizeof(long_reference_source),+ "package parity\n"+ "func %s() {}\n"+ "func longPropertiesReferenceAccept(callback func()) {}\n"+ "func longPropertiesReferenceSite() { longPropertiesReferenceAccept(%s) }\n",+ long_reference_name, long_reference_name);+ if (long_reference_source_len <= 0 ||+ (size_t)long_reference_source_len >= sizeof(long_reference_source)) {+ th_rmtree(tmp);+ FAIL("long reference source buffer");+ }+ write_temp_file(tmp, "long_properties.go", long_reference_source);write_temp_file(tmp, "python_target.py","def pythonReferenceTarget():\n"" pass\n");@@ -1719,7 +1807,7 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {" const typescriptShadowTarget = (): void => {};\n""}\n");- /* Fourteen positive and three shadow fixtures plus 34 source fillers exceed+ /* Fifteen positive and three shadow fixtures plus 34 source fillers exceed* the 50-file fused threshold without making the test larger than needed. */for (int i = 0; i < 34; i++) {char name[64];@@ -1750,6 +1838,14 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {}int sequential_total = -1;int parallel_total = -1;+ int sequential_long_reference = -1;+ int sequential_long_usage = -1;+ int sequential_long_calls = -1;+ int parallel_long_reference = -1;+ int parallel_long_usage = -1;+ int parallel_long_calls = -1;+ NamedEdgePropertyObservation sequential_long_property = {0};+ NamedEdgePropertyObservation parallel_long_property = {0};char *old_workers = getenv("CBM_WORKERS");char *saved_workers = old_workers ? strdup(old_workers) : NULL;@@ -1788,6 +1884,18 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {named_edge_count(sequential_store, sequential_project, "CALLS",shadow_controls[i].source_name, shadow_controls[i].target_name);}+ sequential_long_reference = named_edge_count(+ sequential_store, sequential_project, "CALL_REFERENCE", "longPropertiesReferenceSite",+ long_reference_name);+ sequential_long_usage = named_edge_count(sequential_store, sequential_project, "USAGE",+ "longPropertiesReferenceSite",+ long_reference_name);+ sequential_long_calls = named_edge_count(sequential_store, sequential_project, "CALLS",+ "longPropertiesReferenceSite",+ long_reference_name);+ sequential_long_property = observe_named_edge_callee_property(+ sequential_db_path, sequential_project, "CALL_REFERENCE",+ "longPropertiesReferenceSite", long_reference_name, long_reference_name);cbm_store_close(sequential_store);}cbm_pipeline_free(sequential);@@ -1825,6 +1933,16 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {named_edge_count(parallel_store, parallel_project, "CALLS",shadow_controls[i].source_name, shadow_controls[i].target_name);}+ parallel_long_reference = named_edge_count(+ parallel_store, parallel_project, "CALL_REFERENCE", "longPropertiesReferenceSite",+ long_reference_name);+ parallel_long_usage = named_edge_count(parallel_store, parallel_project, "USAGE",+ "longPropertiesReferenceSite", long_reference_name);+ parallel_long_calls = named_edge_count(parallel_store, parallel_project, "CALLS",+ "longPropertiesReferenceSite", long_reference_name);+ parallel_long_property = observe_named_edge_callee_property(+ parallel_db_path, parallel_project, "CALL_REFERENCE", "longPropertiesReferenceSite",+ long_reference_name, long_reference_name);cbm_store_close(parallel_store);}cbm_pipeline_free(parallel);@@ -1847,6 +1965,24 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {ASSERT_TRUE(sequential_store_opened);ASSERT_EQ(parallel_run_rc, 0);ASSERT_TRUE(parallel_store_opened);+ ASSERT_EQ(sequential_long_reference, 1);+ ASSERT_EQ(sequential_long_usage, 0);+ ASSERT_EQ(sequential_long_calls, 0);+ ASSERT_TRUE(sequential_long_property.database_opened);+ ASSERT_TRUE(sequential_long_property.query_ready);+ ASSERT_TRUE(sequential_long_property.query_completed);+ ASSERT_EQ(sequential_long_property.row_count, 1);+ ASSERT_EQ(sequential_long_property.valid_json_count, 1);+ ASSERT_EQ(sequential_long_property.exact_callee_count, 1);+ ASSERT_EQ(parallel_long_reference, 1);+ ASSERT_EQ(parallel_long_usage, 0);+ ASSERT_EQ(parallel_long_calls, 0);+ ASSERT_TRUE(parallel_long_property.database_opened);+ ASSERT_TRUE(parallel_long_property.query_ready);+ ASSERT_TRUE(parallel_long_property.query_completed);+ ASSERT_EQ(parallel_long_property.row_count, 1);+ ASSERT_EQ(parallel_long_property.valid_json_count, 1);+ ASSERT_EQ(parallel_long_property.exact_callee_count, 1);ASSERT_EQ(parallel_total, sequential_total);int expected_total = 0;for (int i = 0; i < pair_count; i++) {@@ -1858,6 +1994,7 @@ TEST(pipeline_call_reference_sequential_parallel_edge_set_parity) {ASSERT_EQ(parallel_calls[i], 0);expected_total += sequential_reference[i];}+ expected_total += sequential_long_reference;for (int i = 0; i < shadow_count; i++) {ASSERT_EQ(sequential_shadow_controls[i], 1);ASSERT_EQ(parallel_shadow_controls[i], 1);diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.cindex c803a91bd..3aaadee51 100644--- a/tests/test_py_lsp.c+++ b/tests/test_py_lsp.c@@ -922,6 +922,51 @@ TEST(pylsp_later_plain_import_rebinding_same_local_fails_closed) {PASS();}+TEST(pylsp_top_level_function_shadows_imported_callable) {+ static const char source[] = "from target import handler as callback\n"+ "def callback():\n"+ " pass\n"+ "def accept(fn):\n"+ " pass\n"+ "def crossArgument():\n"+ " accept(callback)\n";+ PyImportReferenceCounts counts = pylsp_import_reference_counts(+ source, "callback", "project.target.handler", "project.target.handler", NULL);+ ASSERT_EQ(counts.first_target, 0);+ PASS();+}++TEST(pylsp_decorated_top_level_function_shadows_imported_callable) {+ static const char source[] = "from target import handler as callback\n"+ "def replace(fn):\n"+ " return object()\n"+ "@replace\n"+ "def callback():\n"+ " pass\n"+ "def accept(fn):\n"+ " pass\n"+ "def crossArgument():\n"+ " accept(callback)\n";+ PyImportReferenceCounts counts = pylsp_import_reference_counts(+ source, "callback", "project.target.handler", "project.target.handler", NULL);+ ASSERT_EQ(counts.first_target, 0);+ PASS();+}++TEST(pylsp_later_import_rebinds_top_level_function_name) {+ static const char source[] = "def callback():\n"+ " pass\n"+ "from target import handler as callback\n"+ "def accept(fn):\n"+ " pass\n"+ "def crossArgument():\n"+ " accept(callback)\n";+ PyImportReferenceCounts counts = pylsp_import_reference_counts(+ source, "callback", "project.target.handler", "project.target.handler", NULL);+ ASSERT_EQ(counts.first_target, 1);+ PASS();+}+TEST(pylsp_batch_cross_file_callable_value_preserves_exact_reference_site) {static const char source[] = "from target import handler\n""def accept(callback):\n"@@ -2156,6 +2201,9 @@ SUITE(py_lsp) {RUN_TEST(pylsp_project_prefixed_direct_alias_same_tail_is_not_from_import_reference);RUN_TEST(pylsp_competing_import_forms_for_same_local_fail_closed);RUN_TEST(pylsp_later_plain_import_rebinding_same_local_fails_closed);+ RUN_TEST(pylsp_top_level_function_shadows_imported_callable);+ RUN_TEST(pylsp_decorated_top_level_function_shadows_imported_callable);+ RUN_TEST(pylsp_later_import_rebinds_top_level_function_name);RUN_TEST(pylsp_batch_cross_file_callable_value_preserves_exact_reference_site);RUN_TEST(pylsp_same_target_calls_have_distinct_exact_semantic_sites);RUN_TEST(pylsp_batch_preserves_two_same_target_semantic_sites);diff --git a/tests/test_scope.c b/tests/test_scope.cindex 728a77327..9effb7ca9 100644--- a/tests/test_scope.c+++ b/tests/test_scope.c@@ -149,6 +149,34 @@ TEST(scope_assignment_updates_or_clears_nearest_callable_only) {PASS();}+TEST(scope_checked_bind_reports_child_oom_despite_parent_name) {+ CBMArena a;+ cbm_arena_init(&a);+ CBMScope *root = cbm_scope_push(&a, NULL);+ CBMScope *ordinary_child = cbm_scope_push(&a, root);+ CBMScope *callable_child = cbm_scope_push(&a, root);+ const CBMType *callback_type = named_t(&a, "Callback");+ const CBMType *shadow_type = named_t(&a, "Shadow");+ cbm_scope_bind_callable(root, "callback", callback_type, "pkg.parent");++ int saved_nblocks = a.nblocks;+ size_t saved_used = a.used;+ a.nblocks = CBM_ARENA_MAX_BLOCKS;+ a.used = a.block_size;+ bool ordinary_bound = cbm_scope_bind_checked(ordinary_child, "callback", shadow_type);+ bool callable_bound = cbm_scope_bind_callable_checked(+ callable_child, "callback", shadow_type, "pkg.child");+ a.nblocks = saved_nblocks;+ a.used = saved_used;++ ASSERT_FALSE(ordinary_bound);+ ASSERT_FALSE(callable_bound);+ ASSERT_STR_EQ(cbm_scope_lookup_callable(ordinary_child, "callback"), "pkg.parent");+ ASSERT_STR_EQ(cbm_scope_lookup_callable(callable_child, "callback"), "pkg.parent");+ cbm_arena_destroy(&a);+ PASS();+}+/* ── Dynamic growth — the Phase 0 win ───────────────────────────── */TEST(scope_dynamic_growth_300_bindings) {@@ -233,6 +261,7 @@ SUITE(scope) {RUN_TEST(scope_child_shadows_parent);RUN_TEST(scope_callable_identity_follows_nearest_binding);RUN_TEST(scope_assignment_updates_or_clears_nearest_callable_only);+ RUN_TEST(scope_checked_bind_reports_child_oom_despite_parent_name);RUN_TEST(scope_dynamic_growth_300_bindings);RUN_TEST(scope_growth_chunk_boundary);RUN_TEST(scope_rebind_in_old_chunk);