Skip to content

Commit 1ac3c13

Browse files
committed
fix(pipeline): preserve websocket route registrations
Share the route path/handler scanner between sequential and parallel call passes, add FastAPI/Starlette websocket route-registration suffixes, and let sequential unresolved route-registration suffix calls emit Route registration facts when the existing route-literal guard accepts the path. Add a sequential/parallel parity canary for FastAPI websocket decorators. Validation: make -f Makefile.cbm -j16 build/c/test-runner; CBM_ONLY_SUITE=parallel build/c/test-runner; CBM_ONLY_SUITE=pipeline CBM_ONLY_TEST=fast_route build/c/test-runner; make -f Makefile.cbm -j16 cbm; bash scripts/check-source-safety.sh; git diff --check. FastAPI matrix evidence: guarded production remains safe by full fallback in /private/tmp/cbm-fastapi-matrix-websocket-parity-current-20260704T2215.json. Forced exact cap64 now preserves the previously missing tests/test_ws_router websocket Route nodes and 9 route-registration edges, but still fails canonical edge parity; keep scoped-LSP full fallback until the remaining edge-attribution mismatch is fixed. Signed-off-by: Andrew Hundt <[email protected]>
1 parent 56a24bb commit 1ac3c13

5 files changed

Lines changed: 161 additions & 62 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ static const method_suffix_t route_reg_suffixes[] = {
495495
/* Framework-specific route registration */
496496
{".Route", "ANY"},
497497
{".route", "ANY"},
498+
{".websocket_route", "ANY"},
499+
{".websocket", "ANY"},
498500
{"::get", "GET"},
499501
{"::post", "POST"},
500502
{"::put", "PUT"},

‎src/pipeline/pass_calls.c‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,35 @@ static const char *itoa_log(int val) {
7777

7878
/* Handle a route registration call: create Route node + HANDLES edge. */
7979
static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
80-
const cbm_gbuf_node_t *source_node, const char *module_qn,
80+
const cbm_gbuf_node_t *source_node, const char *route_path,
81+
const char *handler_ref, const char *module_qn,
8182
const char **imp_keys, const char **imp_vals, int imp_count) {
8283
const char *method = cbm_service_pattern_route_method(call->callee_name);
8384
/* Reject CLI slash-command args (e.g. "/ar:allow") that start with '/' and
8485
* so pass the caller's first_string_arg[0]=='/' check, but aren't valid
8586
* route paths. Same gate as pass_route_nodes.c — keeps command-syntax
8687
* strings from becoming spurious Route nodes. */
87-
if (!cbm_service_pattern_is_http_route_literal(call->first_string_arg, call->callee_name)) {
88+
if (!cbm_service_pattern_is_http_route_literal(route_path, call->callee_name)) {
8889
return;
8990
}
9091
int64_t route_id = cbm_pipeline_upsert_service_route(
91-
ctx->gbuf, call->first_string_arg, CBM_SVC_HTTP, method, NULL, NULL, NULL);
92+
ctx->gbuf, route_path, CBM_SVC_HTTP, method, NULL, NULL, NULL);
9293
if (route_id == 0) {
9394
return;
9495
}
9596
char esc_cn[CBM_SZ_256]; /* sliced source text: escape quotes/newlines */
9697
char esc_fa[CBM_SZ_256];
9798
cbm_json_escape(esc_cn, sizeof(esc_cn), call->callee_name);
98-
cbm_json_escape(esc_fa, sizeof(esc_fa), call->first_string_arg);
99+
cbm_json_escape(esc_fa, sizeof(esc_fa), route_path);
99100
char props[CBM_SZ_512];
100101
snprintf(props, sizeof(props),
101102
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}", esc_cn,
102103
esc_fa);
103104
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props);
104-
if (call->second_arg_name != NULL && call->second_arg_name[0] != '\0') {
105-
cbm_resolution_t hres = cbm_registry_resolve(ctx->registry, call->second_arg_name,
106-
module_qn, imp_keys, imp_vals, imp_count);
105+
if (handler_ref != NULL && handler_ref[0] != '\0') {
106+
cbm_resolution_t hres =
107+
cbm_registry_resolve(ctx->registry, handler_ref, module_qn, imp_keys, imp_vals,
108+
imp_count);
107109
if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
108110
const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);
109111
if (handler == NULL) {
@@ -199,9 +201,14 @@ static bool emit_classified_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
199201
const cbm_resolution_t *res, const char *module_qn,
200202
const char **imp_keys, const char **imp_vals, int imp_count) {
201203
cbm_svc_kind_t svc = cbm_service_pattern_match(res->qualified_name);
202-
if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg && call->first_string_arg[0] == '/') {
203-
handle_route_registration(ctx, call, source, module_qn, imp_keys, imp_vals, imp_count);
204-
return false;
204+
if (svc == CBM_SVC_ROUTE_REG) {
205+
const char *handler_ref = NULL;
206+
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
207+
if (route_path) {
208+
handle_route_registration(ctx, call, source, route_path, handler_ref, module_qn,
209+
imp_keys, imp_vals, imp_count);
210+
return false;
211+
}
205212
}
206213
if (svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) {
207214
emit_http_async_edge(ctx, call, source, target, res, svc);
@@ -300,6 +307,16 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
300307
}
301308
}
302309

310+
if (cbm_service_pattern_route_method(call->callee_name) != NULL) {
311+
const char *handler_ref = NULL;
312+
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
313+
if (route_path) {
314+
handle_route_registration(ctx, call, source_node, route_path, handler_ref, module_qn,
315+
imp_keys, imp_vals, imp_count);
316+
return SKIP_ONE;
317+
}
318+
}
319+
303320
cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
304321
imp_keys, imp_vals, imp_count);
305322
if (!res.qualified_name || res.qualified_name[0] == '\0') {

‎src/pipeline/pass_parallel.c‎

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -983,57 +983,6 @@ typedef struct {
983983
_Atomic uint64_t time_ns_rc_source; /* find_source_node */
984984
} resolve_ctx_t;
985985

986-
/* Scan call args for a URL-like route path and handler reference. */
987-
static bool is_path_keyword(const char *keyword) {
988-
static const char *path_keywords[] = {"prefix", "path", "route", "pattern",
989-
"url", "endpoint", "rule", "mount_path",
990-
"route_path", "url_path", NULL};
991-
for (const char **kw = path_keywords; *kw; kw++) {
992-
if (strcmp(keyword, *kw) == 0) {
993-
return true;
994-
}
995-
}
996-
return false;
997-
}
998-
999-
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler) {
1000-
*out_handler = NULL;
1001-
/* 1. First string arg starting with / */
1002-
if (call->first_string_arg && call->first_string_arg[0] == '/') {
1003-
*out_handler = call->second_arg_name;
1004-
return call->first_string_arg;
1005-
}
1006-
/* 2. Keyword args (prefix=, path=, route=, etc.) */
1007-
const char *found = NULL;
1008-
for (int ai = 0; ai < call->arg_count && !found; ai++) {
1009-
const CBMCallArg *ca = &call->args[ai];
1010-
const char *val = ca->value ? ca->value : ca->expr;
1011-
if (!val || val[0] != '/') {
1012-
continue;
1013-
}
1014-
if ((ca->keyword && is_path_keyword(ca->keyword)) || (!ca->keyword && ca->index == 0)) {
1015-
found = val;
1016-
}
1017-
}
1018-
if (!found) {
1019-
return NULL;
1020-
}
1021-
/* 3. Handler: first identifier arg that's not a path/keyword */
1022-
for (int ai = 0; ai < call->arg_count; ai++) {
1023-
const CBMCallArg *ca = &call->args[ai];
1024-
if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
1025-
continue;
1026-
}
1027-
if (ca->keyword && (strcmp(ca->keyword, "prefix") == 0 ||
1028-
strcmp(ca->keyword, "name") == 0 || strcmp(ca->keyword, "tags") == 0)) {
1029-
continue;
1030-
}
1031-
*out_handler = ca->expr;
1032-
break;
1033-
}
1034-
return found;
1035-
}
1036-
1037986
/* Build props JSON, append args, close brace, emit edge. */
1038987
static void finalize_and_emit(cbm_gbuf_t *gbuf, int64_t src_id, int64_t tgt_id,
1039988
const char *edge_type, char *props, int n, const CBMCall *call) {
@@ -1355,7 +1304,7 @@ static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
13551304

13561305
if (svc == CBM_SVC_ROUTE_REG) {
13571306
const char *handler_ref = NULL;
1358-
const char *route_path = find_route_path_in_args(call, &handler_ref);
1307+
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
13591308
if (route_path) {
13601309
emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
13611310
registry, main_gbuf, imp_keys, imp_vals, imp_count);

‎src/pipeline/pipeline_internal.h‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,74 @@ void cbm_pipeline_detect_url_arg_routes(cbm_gbuf_t *gb, const cbm_gbuf_node_t *s
5959
const CBMCall *call, const char *rel_path,
6060
CBMLanguage lang);
6161

62+
static inline bool cbm_pipeline_call_arg_is_route_path_keyword(const char *keyword) {
63+
static const char *const path_keywords[] = {"prefix", "path", "route",
64+
"pattern", "url", "endpoint",
65+
"rule", "mount_path", "route_path",
66+
"url_path", "uri", NULL};
67+
if (!keyword) {
68+
return false;
69+
}
70+
for (const char *const *kw = path_keywords; *kw; kw++) {
71+
if (strcmp(keyword, *kw) == 0) {
72+
return true;
73+
}
74+
}
75+
return false;
76+
}
77+
78+
/* Locate the path literal and optional handler reference for route registration
79+
* APIs. Shared by sequential and parallel call passes to keep FastAPI/Starlette,
80+
* Express-style, and keyword-argument registrations on the same semantics. */
81+
static inline const char *cbm_pipeline_call_route_path_and_handler(const CBMCall *call,
82+
const char **out_handler) {
83+
if (out_handler) {
84+
*out_handler = NULL;
85+
}
86+
if (!call) {
87+
return NULL;
88+
}
89+
if (call->first_string_arg && call->first_string_arg[0] == '/') {
90+
if (out_handler) {
91+
*out_handler = call->second_arg_name;
92+
}
93+
return call->first_string_arg;
94+
}
95+
96+
const char *found = NULL;
97+
for (int ai = 0; ai < call->arg_count && !found; ai++) {
98+
const CBMCallArg *ca = &call->args[ai];
99+
const char *val = ca->value ? ca->value : ca->expr;
100+
if (!val || val[0] != '/') {
101+
continue;
102+
}
103+
if ((ca->keyword && cbm_pipeline_call_arg_is_route_path_keyword(ca->keyword)) ||
104+
(!ca->keyword && ca->index == 0)) {
105+
found = val;
106+
}
107+
}
108+
if (!found) {
109+
return NULL;
110+
}
111+
112+
if (out_handler) {
113+
for (int ai = 0; ai < call->arg_count; ai++) {
114+
const CBMCallArg *ca = &call->args[ai];
115+
if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
116+
continue;
117+
}
118+
if (ca->keyword &&
119+
(strcmp(ca->keyword, "prefix") == 0 || strcmp(ca->keyword, "name") == 0 ||
120+
strcmp(ca->keyword, "tags") == 0)) {
121+
continue;
122+
}
123+
*out_handler = ca->expr;
124+
break;
125+
}
126+
}
127+
return found;
128+
}
129+
62130
static inline bool cbm_pipeline_label_is_registry_symbol(const char *label) {
63131
return label && (strcmp(label, "Function") == 0 || strcmp(label, "Method") == 0 ||
64132
cbm_label_is_type_like(label) || strcmp(label, "Variable") == 0 ||

‎tests/test_parallel.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,68 @@ TEST(parallel_unresolved_route_suffix_does_not_emit_self_call) {
478478
PASS();
479479
}
480480

481+
typedef struct {
482+
const char *url_path;
483+
int route_registration_calls;
484+
} route_registration_count_ctx_t;
485+
486+
static void count_route_registration_edges(const cbm_gbuf_edge_t *edge, void *ud) {
487+
route_registration_count_ctx_t *c = ud;
488+
if (!edge || !edge->type || strcmp(edge->type, "CALLS") != 0 || !edge->properties_json) {
489+
return;
490+
}
491+
if (strstr(edge->properties_json, "\"via\":\"route_registration\"") &&
492+
strstr(edge->properties_json, c->url_path)) {
493+
c->route_registration_calls++;
494+
}
495+
}
496+
497+
static int count_route_registration_for_path(cbm_gbuf_t *gbuf, const char *url_path) {
498+
route_registration_count_ctx_t c = {.url_path = url_path, .route_registration_calls = 0};
499+
cbm_gbuf_foreach_edge(gbuf, count_route_registration_edges, &c);
500+
return c.route_registration_calls;
501+
}
502+
503+
TEST(parallel_fastapi_websocket_route_registration_matches_sequential) {
504+
char dir[256];
505+
snprintf(dir, sizeof(dir), "/tmp/cbm_ws_routes_XXXXXX");
506+
ASSERT_TRUE(cbm_mkdtemp(dir) != NULL);
507+
508+
const char *source =
509+
"from fastapi import APIRouter, WebSocket\n\n"
510+
"router = APIRouter()\n\n"
511+
"@router.websocket('/custom_error/')\n"
512+
"async def router_ws_custom_error(websocket: WebSocket):\n"
513+
" raise RuntimeError('boom')\n\n"
514+
"@router.websocket_route('/router')\n"
515+
"async def routerindex(websocket: WebSocket):\n"
516+
" await websocket.accept()\n";
517+
518+
char path[512];
519+
snprintf(path, sizeof(path), "%s/app.py", dir);
520+
ASSERT_EQ(th_write_file(path, source), 0);
521+
522+
cbm_file_info_t files[1] = {0};
523+
files[0].path = path;
524+
files[0].rel_path = (char *)"app.py";
525+
files[0].language = CBM_LANG_PYTHON;
526+
527+
cbm_gbuf_t *seq = run_sequential("cbm_ws_routes", dir, files, 1);
528+
cbm_gbuf_t *par = run_parallel("cbm_ws_routes", dir, files, 1, 1);
529+
ASSERT_NOT_NULL(seq);
530+
ASSERT_NOT_NULL(par);
531+
532+
ASSERT_EQ(count_route_registration_for_path(seq, "/custom_error/"), 1);
533+
ASSERT_EQ(count_route_registration_for_path(seq, "/router"), 1);
534+
ASSERT_EQ(count_route_registration_for_path(par, "/custom_error/"), 1);
535+
ASSERT_EQ(count_route_registration_for_path(par, "/router"), 1);
536+
537+
cbm_gbuf_free(seq);
538+
cbm_gbuf_free(par);
539+
th_rmtree(dir);
540+
PASS();
541+
}
542+
481543
/* ── Production pipeline worker-count parity ─────────────────────── */
482544

483545
enum {
@@ -1194,6 +1256,7 @@ SUITE(parallel) {
11941256
RUN_TEST(parallel_empty_files);
11951257
RUN_TEST(parallel_args_json_no_overflow);
11961258
RUN_TEST(parallel_unresolved_route_suffix_does_not_emit_self_call);
1259+
RUN_TEST(parallel_fastapi_websocket_route_registration_matches_sequential);
11971260

11981261
/* Cleanup shared state */
11991262
parity_teardown();

0 commit comments

Comments
 (0)