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
Emit DECORATES to synthetic nodes for external decorators + python subscript-dunder CALLS
Martin Vogelde2c54ac1ff8
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 2043b8b84..126245997 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -1953,6 +1953,20 @@ static void py_emit_dunder_call(PyLSPContext* ctx, const CBMType* recv, const chpy_lookup_attribute(ctx, recv->data.named.qualified_name, dunder);if (f && f->qualified_name) {py_emit_resolved_call(ctx, f->qualified_name, "lsp_operator_dunder", 0.85f);+ /* A subscript (`s[k]`) / binary_operator (`a + b`) is not a syntactic+ * `call` node, so the extractor produced no CBMCall for it and the+ * resolved_call above would never be matched into a CALLS edge. Inject+ * a synthetic CBMCall keyed on (enclosing_func_qn, short dunder name)+ * so resolve_file_calls can pair it with the resolved entry. The+ * resolved callee QN ends in the dunder, so its short name == dunder.+ * Mirrors rust_inject_syn_call. */+ if (ctx->syn_calls && ctx->arena && ctx->enclosing_func_qn) {+ CBMCall call;+ memset(&call, 0, sizeof(call));+ call.callee_name = cbm_arena_strdup(ctx->arena, dunder);+ call.enclosing_func_qn = ctx->enclosing_func_qn;+ cbm_calls_push(ctx->syn_calls, ctx->arena, call);+ }}}@@ -3038,6 +3052,9 @@ void cbm_run_py_lsp(CBMArena* arena, CBMFileResult* result,PyLSPContext ctx;py_lsp_init(&ctx, arena, source, source_len, ®, module_qn, &result->resolved_calls);+ /* Let the resolver inject synthetic syntactic calls for operator/subscript+ * dunder desugaring so those recovered calls reach the CALLS-edge pipeline. */+ ctx.syn_calls = &result->calls;for (int i = 0; i < result->imports.count; i++) {CBMImport* imp = &result->imports.items[i];diff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.hindex 670717755..5548d4ef3 100644--- a/internal/cbm/lsp/py_lsp.h+++ b/internal/cbm/lsp/py_lsp.h@@ -47,6 +47,18 @@ typedef struct {// Output: resolved calls accumulate here.CBMResolvedCallArray* resolved_calls;+ // Syntactic-call list (result->calls), borrowed from the per-file+ // extraction result. The downstream pipeline only turns a resolved_call+ // into a CALLS edge when a *syntactic* CBMCall with the same+ // (enclosing_func_qn, callee short-name) exists here. Operator/subscript+ // dunder desugaring (`s[k]` -> T.__getitem__, `a + b` -> T.__add__) never+ // appears in result->calls because a `subscript`/`binary_operator` is not+ // a `call` node, so the syntactic extractor produced no call. When this is+ // non-NULL, py_emit_dunder_call injects a matching synthetic CBMCall so the+ // recovered dunder call becomes a real CALLS edge. NULL in the cross-file+ // path (no result->calls available there). Mirrors RustLSPContext.syn_calls.+ CBMCallArray* syn_calls;+// Lambda registry: simple linear list, looked up by name.CBMLambdaEntry* lambdas;int lambda_count;diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex d7956ced6..fadf20374 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -1914,17 +1914,43 @@ static void resolve_def_decorators(resolve_ctx_t *rc, resolve_worker_state_t *wscontinue;}cbm_resolution_t res = cbm_registry_resolve(rc->registry, fn, mq, ik, iv, ic);- if (!res.qualified_name || res.qualified_name[0] == '\0') {- continue;+ if ((!res.qualified_name || res.qualified_name[0] == '\0') && !strchr(fn, '.')) {+ /* C# attributes are referenced by their short name (`[Log]`) but+ * declared with an `Attribute` suffix (`class LogAttribute`). */+ char with_suffix[CBM_SZ_256];+ int wn = snprintf(with_suffix, sizeof(with_suffix), "%sAttribute", fn);+ if (wn > 0 && (size_t)wn < sizeof(with_suffix)) {+ res = cbm_registry_resolve(rc->registry, with_suffix, mq, ik, iv, ic);+ }}- const cbm_gbuf_node_t *dn = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);- if (dn && node->id != dn->id) {+ const cbm_gbuf_node_t *dn = NULL;+ if (res.qualified_name && res.qualified_name[0] != '\0') {+ dn = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);+ }+ int64_t dn_id = 0;+ if (dn) {+ dn_id = dn->id;+ } else {+ /* External/stdlib decorator (Rust `#[derive(Debug)]`, Swift+ * `@discardableResult`, Scala `@deprecated`, Python `@cache`,+ * Java `@Override`, ...): no local symbol resolves. Materialise a+ * synthetic "Decorator" node so the DECORATES relation is recorded.+ * The node is created in the per-worker local_edge_buf (shared-ID+ * gbuf); the sequential merge dedupes by QN across workers, so all+ * uses of the same decorator name collapse to one node project-wide+ * and the edge target IDs are remapped consistently. */+ char syn_qn[CBM_SZ_512];+ snprintf(syn_qn, sizeof(syn_qn), "<decorator:%s>", fn);+ dn_id = cbm_gbuf_upsert_node(ws->local_edge_buf, "Decorator", fn, syn_qn, "", 0, 0,+ "{}");+ }+ if (dn_id != 0 && node->id != dn_id) {char dp[CBM_SZ_256];snprintf(dp, sizeof(dp), "{\"decorator\":\"%s\"}", def->decorators[dc]);- cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dn->id, "DECORATES", dp);+ cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dn_id, "DECORATES", dp);/* Ensure a reference-style edge exists so the decorator appears in queries* without being misclassified as a real call by downstream passes. */- cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dn->id, "USAGE", "{}");+ cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dn_id, "USAGE", "{}");ws->semantic_resolved++;}}diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.cindex 1d17c0c63..3bf514de4 100644--- a/src/pipeline/pass_semantic.c+++ b/src/pipeline/pass_semantic.c@@ -343,6 +343,16 @@ int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {return edge_count;}+/* Build the QN of the synthetic node that stands in for an external/stdlib+ * decorator whose target is not a local symbol (Rust `#[derive(Debug)]`, Swift+ * `@discardableResult`, Scala `@deprecated`, Python `@cache`, Java `@Override`,+ * ...). Namespaced with `<decorator:` so it can never collide with a real+ * symbol and so all uses of the same decorator name across a project dedupe to+ * one node by QN. */+static void synth_decorator_qn(const char *func_name, char *out, size_t outsz) {+ snprintf(out, outsz, "<decorator:%s>", func_name);+}+/* Process INHERITS + DECORATES edges for one definition. *//* Resolve one decorator and create DECORATES edge. */static void resolve_decorator(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *node,@@ -367,10 +377,26 @@ static void resolve_decorator(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *noimp_count);}}- if (!res.qualified_name || res.qualified_name[0] == '\0') {- return;+ const cbm_gbuf_node_t *dec = NULL;+ if (res.qualified_name && res.qualified_name[0] != '\0') {+ dec = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);+ }+ if (!dec) {+ /* The decorator target is not a local symbol (external attribute /+ * stdlib annotation / proc-macro derive). Materialise a synthetic+ * "Decorator" node so the DECORATES relation is still recorded.+ * Upsert dedupes by QN, so every use of the same decorator name shares+ * one node (one extra node per distinct external decorator, project-+ * wide). Created in the single-threaded semantic pass, so direct+ * ctx->gbuf mutation is safe here (mirrors pass_route_nodes). */+ char syn_qn[CBM_SZ_512];+ synth_decorator_qn(func_name, syn_qn, sizeof(syn_qn));+ int64_t syn_id =+ cbm_gbuf_upsert_node(ctx->gbuf, "Decorator", func_name, syn_qn, "", 0, 0, "{}");+ if (syn_id != 0) {+ dec = cbm_gbuf_find_by_qn(ctx->gbuf, syn_qn);+ }}- const cbm_gbuf_node_t *dec = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);if (dec && node->id != dec->id) {char props[CBM_SZ_256];snprintf(props, sizeof(props), "{\"decorator\":\"%s\"}", decorator);