From c5a3c6eae944bb863bd9310faf6a995f4a7365d9 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 20 Sep 2026 18:16:02 +0200 Subject: [PATCH] fix(routes): register FastAPI and Starlette WebSocket decorators Distilled from #1245 by Andrew Hundt (a slice of 1ac3c136). The route-registration suffix table knew .add_api_websocket_route but not the decorator forms FastAPI and Starlette actually use: @router.websocket("/ws") @app.websocket_route("/events") so a WebSocket endpoint produced no Route node on either resolver path and was invisible to the graph. Two entries join the table, both recorded as ANY like the existing WebSocket entry -- a socket upgrade is not an HTTP verb. Both are needed: matching is a pure suffix compare and .route does not match ...websocket_route, whose tail is _route. Upstream also changed pass_calls.c, pass_parallel.c and pipeline_internal.h. None of that is taken: main already carries the #952 sequential unresolved-callee route fallback its July base lacked, so the table entries are the only missing piece. RED before the fix, each form recorded on its own (the harness stops at the first failing assert, so the two were swapped and re-run): pipeline_python_websocket_decorators_register_routes FAIL tests/test_pipeline.c:6163: count_nodes_named(s, project, "/ws") (0) not >= 1 (1) FAIL tests/test_pipeline.c:6163: count_nodes_named(s, project, "/events") (0) not >= 1 (1) The @app.get("/health") control above those lines passed before and after. GREEN after: pipeline 282 passed; pipeline parallel extraction edge_types_probe route_canon infrascan 779 passed, 0 failed. The test drives the sequential resolver (one file). The parallel resolver reads the same table through cbm_service_pattern_route_method and its suite is green; a CBM_WORKERS=4 twin of this test is a small follow-up. Co-authored-by: Andrew Hundt Signed-off-by: Martin Vogel --- internal/cbm/service_patterns.c | 5 ++++ tests/test_pipeline.c | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/internal/cbm/service_patterns.c b/internal/cbm/service_patterns.c index 6da74d4784..2626b52ca8 100644 --- a/internal/cbm/service_patterns.c +++ b/internal/cbm/service_patterns.c @@ -577,6 +577,11 @@ static const method_suffix_t route_reg_suffixes[] = { {".add_route", "ANY"}, {".add_api_route", "ANY"}, {".add_api_websocket_route", "ANY"}, + /* FastAPI/Starlette WebSocket decorators (`@router.websocket('/ws')`, + * `@app.websocket_route('/events')`): not an HTTP verb, so ANY — mirrors + * .add_api_websocket_route above (#1245). */ + {".websocket", "ANY"}, + {".websocket_route", "ANY"}, {NULL, NULL}, }; diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index 44bb49e9c2..889bffbe10 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -7487,6 +7487,56 @@ TEST(pipeline_capitalized_axios_import_is_http_client_sequential) { return capitalized_axios_case(0); } +/* FastAPI/Starlette WebSocket route registration (distilled from #1245). The + * route-registration suffix table knew `.add_api_websocket_route` but not the + * decorator forms `.websocket` / `.websocket_route`, so a WebSocket endpoint + * registered via `@router.websocket('/ws')` produced no Route node at all — + * the endpoint was invisible to the graph. `@app.get('/health')` is the + * positive control (already a route suffix). Fewer than 50 files exercises + * pass_calls.c; the same suffix table drives the parallel resolver. */ +TEST(pipeline_python_websocket_decorators_register_routes) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_ws_routes_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + + write_temp_file(tmp, "app.py", + "from fastapi import APIRouter, FastAPI, WebSocket\n\n" + "app = FastAPI()\n" + "router = APIRouter()\n\n" + "@app.get('/health')\n" + "def health():\n" + " return {'ok': True}\n\n" + "@router.websocket('/ws')\n" + "async def ws_endpoint(websocket: WebSocket):\n" + " await websocket.accept()\n\n" + "@app.websocket_route('/events')\n" + "async def events_endpoint(websocket: WebSocket):\n" + " await websocket.accept()\n"); + + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/py_ws.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + const char *project = cbm_pipeline_project_name(p); + + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + + /* Positive control: the HTTP decorator mints its Route node. */ + ASSERT_GTE(count_nodes_named(s, project, "/health"), 1); + /* The two WebSocket decorator forms must mint Route nodes too. */ + ASSERT_GTE(count_nodes_named(s, project, "/ws"), 1); + ASSERT_GTE(count_nodes_named(s, project, "/events"), 1); + + cbm_store_close(s); + cbm_pipeline_free(p); + th_rmtree(tmp); + PASS(); +} + /* Python bare-call local-binding suppression, sequential path. The bare-call * counterpart of the receiver guard above: `run` is a PARAMETER, so `run()` * cannot be the module-level `run` and must not bind SatoriLive.run. @@ -17568,6 +17618,7 @@ SUITE(pipeline) { RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges); RUN_TEST(pipeline_capitalized_axios_import_is_http_client); RUN_TEST(pipeline_capitalized_axios_import_is_http_client_sequential); + RUN_TEST(pipeline_python_websocket_decorators_register_routes); RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges); RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge); RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);