Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(grpc): anchor Route emission on stub suffix; preserve service name (#294)

Martin Vogel5f3847c0e5b5
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index ce8fa505b..6aec915e8 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1267,8 +1267,8 @@ static void detect_url_in_args(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
/* Extract gRPC service and method from a callee name.
* Handles patterns like: pb.NewFooServiceClient(conn).GetBar → Foo/GetBar
* Also: FooServiceGrpc.newBlockingStub(ch).getBar → FooService/getBar */
-static bool extract_grpc_service_method(const char *callee, char *service, size_t srv_sz,
- char *method, size_t meth_sz) {
+bool extract_grpc_service_method(const char *callee, char *service, size_t srv_sz, char *method,
+ size_t meth_sz) {
service[0] = '\0';
method[0] = '\0';
if (!callee) {
@@ -1298,20 +1298,35 @@ static bool extract_grpc_service_method(const char *callee, char *service, size_
s += CBM_SZ_3;
}
- /* Strip common suffixes: Client, ServiceClient, ServiceGrpc, Stub */
+ /* Strip the generated-stub/client suffix, preserving the canonical
+ * proto-declared service name. The proto service is `<X>Service`; the
+ * generated client is `<X>ServiceClient` / `<X>ServiceGrpc`, so we strip
+ * only the trailing stub/client token (Client/Stub/Grpc/…), NOT "Service"
+ * itself — stripping "ServiceClient" yielded `<X>` and broke cross-repo
+ * matching against the `<X>Service` declared name (#294). Longest tokens
+ * first so e.g. BlockingStub wins over Stub.
+ *
+ * A match also serves as the gRPC stub-type signal: we ONLY emit a Route
+ * when a recognized suffix is actually present. Without this gate the
+ * fallback turned ordinary receiver vars (`_provider.GetGroup`,
+ * `_builder.AddSomeService`) into phantom `__grpc__provider/...` Routes
+ * that correspond to no .proto anywhere (#294). */
snprintf(service, srv_sz, "%s", s);
size_t slen = strlen(service);
- static const char *suffixes[] = {"ServiceClient", "Client", "ServiceGrpc", "BlockingStub",
- "FutureStub", "Stub", "Servicer", NULL};
- for (const char **sfx = suffixes; *sfx; sfx++) {
+ static const char *const suffixes[] = {"BlockingStub", "FutureStub", "AsyncStub",
+ "AsyncClient", "Servicer", "Client",
+ "Stub", "Grpc", NULL};
+ bool stripped = false;
+ for (const char *const *sfx = suffixes; *sfx; sfx++) {
size_t flen = strlen(*sfx);
if (slen > flen && strcmp(service + slen - flen, *sfx) == 0) {
service[slen - flen] = '\0';
+ stripped = true;
break;
}
}
- return service[0] && method[0];
+ return stripped && service[0] && method[0];
}
/* Emit GRPC_CALLS edge via gRPC Route node. */
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 36a314514..6259892fc 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -486,4 +486,11 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
const char *cbm_pipeline_repo_path(const cbm_pipeline_t *p);
atomic_int *cbm_pipeline_cancelled_ptr(cbm_pipeline_t *p);
+/* Parse a gRPC stub call "<service-stub>.<method>" into the canonical proto
+ * service name + method. Returns true ONLY when a recognized gRPC stub/client
+ * suffix is present (the stub-type signal that gates Route emission, #294).
+ * Exposed for testing. */
+bool extract_grpc_service_method(const char *callee, char *service, size_t srv_sz, char *method,
+ size_t meth_sz);
+
#endif /* CBM_PIPELINE_INTERNAL_H */
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index 147adca3a..987435479 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -145,15 +145,14 @@ static cbm_gbuf_t *run_parallel(const char *project, const char *repo_path, cbm_
char **def_modules = (char **)calloc((size_t)file_count, sizeof(char *));
int def_count = 0;
CBMLSPDef *all_defs = def_modules
- ? cbm_pxc_collect_all_defs(result_cache, files, file_count, ctx.project_name,
- def_modules, &def_count)
- : NULL;
+ ? cbm_pxc_collect_all_defs(result_cache, files, file_count,
+ ctx.project_name, def_modules, &def_count)
+ : NULL;
CBMModuleDefIndex *module_def_index =
all_defs ? cbm_pxc_build_module_def_index(all_defs, def_count) : NULL;
- cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids,
- worker_count, all_defs, def_count, def_modules,
- module_def_index,
+ cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids, worker_count, all_defs,
+ def_count, def_modules, module_def_index,
NULL /* cross_registries — tests use per-file path */);
cbm_gbuf_set_next_id(gbuf, atomic_load(&shared_ids));
@@ -513,7 +512,7 @@ TEST(parallel_python_lsp_override_emits_lsp_strategy_edges) {
fclose(f);
cbm_file_info_t files[1] = {0};
- files[0].path = fpath0;
+ files[0].path = fpath0;
files[0].rel_path = (char *)"app.py";
files[0].language = CBM_LANG_PYTHON;
@@ -586,10 +585,10 @@ TEST(parallel_python_lsp_override_cross_file_emits_lsp_strategy_edges) {
fclose(af);
cbm_file_info_t files[2] = {0};
- files[0].path = gpath;
+ files[0].path = gpath;
files[0].rel_path = (char *)"greeter.py";
files[0].language = CBM_LANG_PYTHON;
- files[1].path = apath;
+ files[1].path = apath;
files[1].rel_path = (char *)"app.py";
files[1].language = CBM_LANG_PYTHON;
@@ -615,9 +614,51 @@ TEST(parallel_python_lsp_override_cross_file_emits_lsp_strategy_edges) {
PASS();
}
+/* issue #294: gRPC service-name extraction must (a) preserve the canonical
+ * proto service name (FooServiceClient → FooService, not Foo) and (b) only
+ * match real stub/client types — ordinary receiver vars must NOT produce
+ * phantom __grpc__ Routes. */
+TEST(grpc_service_name_preserves_service_suffix_issue294) {
+ char svc[256];
+ char meth[256];
+
+ /* Generated client class keeps the "Service" part of the name. */
+ ASSERT_TRUE(extract_grpc_service_method("pb.NewFooServiceClient.GetBar", svc, sizeof(svc), meth,
+ sizeof(meth)));
+ ASSERT_STR_EQ(svc, "FooService");
+ ASSERT_STR_EQ(meth, "GetBar");
+
+ /* Java-style ...ServiceGrpc strips only "Grpc". */
+ ASSERT_TRUE(extract_grpc_service_method("CartServiceGrpc.getCart", svc, sizeof(svc), meth,
+ sizeof(meth)));
+ ASSERT_STR_EQ(svc, "CartService");
+
+ /* BlockingStub wins over Stub (longest-suffix-first). */
+ ASSERT_TRUE(extract_grpc_service_method("CartServiceBlockingStub.getCart", svc, sizeof(svc),
+ meth, sizeof(meth)));
+ ASSERT_STR_EQ(svc, "CartService");
+ PASS();
+}
+
+TEST(grpc_no_phantom_route_from_plain_var_issue294) {
+ char svc[256];
+ char meth[256];
+
+ /* Ordinary receiver vars carry no gRPC stub suffix → must NOT match,
+ * so no phantom __grpc__provider/... or __grpc__builder/... Route. */
+ ASSERT_FALSE(
+ extract_grpc_service_method("_provider.GetGroup", svc, sizeof(svc), meth, sizeof(meth)));
+ ASSERT_FALSE(extract_grpc_service_method("_builder.AddSomeService", svc, sizeof(svc), meth,
+ sizeof(meth)));
+ ASSERT_FALSE(extract_grpc_service_method("logger.Info", svc, sizeof(svc), meth, sizeof(meth)));
+ PASS();
+}
+
/* ── Suite Registration ──────────────────────────────────────────── */
SUITE(parallel) {
+ RUN_TEST(grpc_service_name_preserves_service_suffix_issue294);
+ RUN_TEST(grpc_no_phantom_route_from_plain_var_issue294);
/* Graph buffer merge/shared-ID tests */
RUN_TEST(gbuf_shared_ids_unique);
RUN_TEST(gbuf_merge_nodes);