mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-12-06 04:56:45 +08:00
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 <ATHundt@gmail.com>
This commit is contained in:
@@ -495,6 +495,8 @@ static const method_suffix_t route_reg_suffixes[] = {
|
||||
/* Framework-specific route registration */
|
||||
{".Route", "ANY"},
|
||||
{".route", "ANY"},
|
||||
{".websocket_route", "ANY"},
|
||||
{".websocket", "ANY"},
|
||||
{"::get", "GET"},
|
||||
{"::post", "POST"},
|
||||
{"::put", "PUT"},
|
||||
|
||||
+27
-10
@@ -77,33 +77,35 @@ static const char *itoa_log(int val) {
|
||||
|
||||
/* Handle a route registration call: create Route node + HANDLES edge. */
|
||||
static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
|
||||
const cbm_gbuf_node_t *source_node, const char *module_qn,
|
||||
const cbm_gbuf_node_t *source_node, const char *route_path,
|
||||
const char *handler_ref, const char *module_qn,
|
||||
const char **imp_keys, const char **imp_vals, int imp_count) {
|
||||
const char *method = cbm_service_pattern_route_method(call->callee_name);
|
||||
/* Reject CLI slash-command args (e.g. "/ar:allow") that start with '/' and
|
||||
* so pass the caller's first_string_arg[0]=='/' check, but aren't valid
|
||||
* route paths. Same gate as pass_route_nodes.c — keeps command-syntax
|
||||
* strings from becoming spurious Route nodes. */
|
||||
if (!cbm_service_pattern_is_http_route_literal(call->first_string_arg, call->callee_name)) {
|
||||
if (!cbm_service_pattern_is_http_route_literal(route_path, call->callee_name)) {
|
||||
return;
|
||||
}
|
||||
int64_t route_id = cbm_pipeline_upsert_service_route(
|
||||
ctx->gbuf, call->first_string_arg, CBM_SVC_HTTP, method, NULL, NULL, NULL);
|
||||
ctx->gbuf, route_path, CBM_SVC_HTTP, method, NULL, NULL, NULL);
|
||||
if (route_id == 0) {
|
||||
return;
|
||||
}
|
||||
char esc_cn[CBM_SZ_256]; /* sliced source text: escape quotes/newlines */
|
||||
char esc_fa[CBM_SZ_256];
|
||||
cbm_json_escape(esc_cn, sizeof(esc_cn), call->callee_name);
|
||||
cbm_json_escape(esc_fa, sizeof(esc_fa), call->first_string_arg);
|
||||
cbm_json_escape(esc_fa, sizeof(esc_fa), route_path);
|
||||
char props[CBM_SZ_512];
|
||||
snprintf(props, sizeof(props),
|
||||
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}", esc_cn,
|
||||
esc_fa);
|
||||
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props);
|
||||
if (call->second_arg_name != NULL && call->second_arg_name[0] != '\0') {
|
||||
cbm_resolution_t hres = cbm_registry_resolve(ctx->registry, call->second_arg_name,
|
||||
module_qn, imp_keys, imp_vals, imp_count);
|
||||
if (handler_ref != NULL && handler_ref[0] != '\0') {
|
||||
cbm_resolution_t hres =
|
||||
cbm_registry_resolve(ctx->registry, handler_ref, module_qn, imp_keys, imp_vals,
|
||||
imp_count);
|
||||
if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
|
||||
const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);
|
||||
if (handler == NULL) {
|
||||
@@ -199,9 +201,14 @@ static bool emit_classified_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
|
||||
const cbm_resolution_t *res, const char *module_qn,
|
||||
const char **imp_keys, const char **imp_vals, int imp_count) {
|
||||
cbm_svc_kind_t svc = cbm_service_pattern_match(res->qualified_name);
|
||||
if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg && call->first_string_arg[0] == '/') {
|
||||
handle_route_registration(ctx, call, source, module_qn, imp_keys, imp_vals, imp_count);
|
||||
return false;
|
||||
if (svc == CBM_SVC_ROUTE_REG) {
|
||||
const char *handler_ref = NULL;
|
||||
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
|
||||
if (route_path) {
|
||||
handle_route_registration(ctx, call, source, route_path, handler_ref, module_qn,
|
||||
imp_keys, imp_vals, imp_count);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
if (cbm_service_pattern_route_method(call->callee_name) != NULL) {
|
||||
const char *handler_ref = NULL;
|
||||
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
|
||||
if (route_path) {
|
||||
handle_route_registration(ctx, call, source_node, route_path, handler_ref, module_qn,
|
||||
imp_keys, imp_vals, imp_count);
|
||||
return SKIP_ONE;
|
||||
}
|
||||
}
|
||||
|
||||
cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
|
||||
imp_keys, imp_vals, imp_count);
|
||||
if (!res.qualified_name || res.qualified_name[0] == '\0') {
|
||||
|
||||
@@ -983,57 +983,6 @@ typedef struct {
|
||||
_Atomic uint64_t time_ns_rc_source; /* find_source_node */
|
||||
} resolve_ctx_t;
|
||||
|
||||
/* Scan call args for a URL-like route path and handler reference. */
|
||||
static bool is_path_keyword(const char *keyword) {
|
||||
static const char *path_keywords[] = {"prefix", "path", "route", "pattern",
|
||||
"url", "endpoint", "rule", "mount_path",
|
||||
"route_path", "url_path", NULL};
|
||||
for (const char **kw = path_keywords; *kw; kw++) {
|
||||
if (strcmp(keyword, *kw) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler) {
|
||||
*out_handler = NULL;
|
||||
/* 1. First string arg starting with / */
|
||||
if (call->first_string_arg && call->first_string_arg[0] == '/') {
|
||||
*out_handler = call->second_arg_name;
|
||||
return call->first_string_arg;
|
||||
}
|
||||
/* 2. Keyword args (prefix=, path=, route=, etc.) */
|
||||
const char *found = NULL;
|
||||
for (int ai = 0; ai < call->arg_count && !found; ai++) {
|
||||
const CBMCallArg *ca = &call->args[ai];
|
||||
const char *val = ca->value ? ca->value : ca->expr;
|
||||
if (!val || val[0] != '/') {
|
||||
continue;
|
||||
}
|
||||
if ((ca->keyword && is_path_keyword(ca->keyword)) || (!ca->keyword && ca->index == 0)) {
|
||||
found = val;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return NULL;
|
||||
}
|
||||
/* 3. Handler: first identifier arg that's not a path/keyword */
|
||||
for (int ai = 0; ai < call->arg_count; ai++) {
|
||||
const CBMCallArg *ca = &call->args[ai];
|
||||
if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
|
||||
continue;
|
||||
}
|
||||
if (ca->keyword && (strcmp(ca->keyword, "prefix") == 0 ||
|
||||
strcmp(ca->keyword, "name") == 0 || strcmp(ca->keyword, "tags") == 0)) {
|
||||
continue;
|
||||
}
|
||||
*out_handler = ca->expr;
|
||||
break;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/* Build props JSON, append args, close brace, emit edge. */
|
||||
static void finalize_and_emit(cbm_gbuf_t *gbuf, int64_t src_id, int64_t tgt_id,
|
||||
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,
|
||||
|
||||
if (svc == CBM_SVC_ROUTE_REG) {
|
||||
const char *handler_ref = NULL;
|
||||
const char *route_path = find_route_path_in_args(call, &handler_ref);
|
||||
const char *route_path = cbm_pipeline_call_route_path_and_handler(call, &handler_ref);
|
||||
if (route_path) {
|
||||
emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
|
||||
registry, main_gbuf, imp_keys, imp_vals, imp_count);
|
||||
|
||||
@@ -59,6 +59,74 @@ void cbm_pipeline_detect_url_arg_routes(cbm_gbuf_t *gb, const cbm_gbuf_node_t *s
|
||||
const CBMCall *call, const char *rel_path,
|
||||
CBMLanguage lang);
|
||||
|
||||
static inline bool cbm_pipeline_call_arg_is_route_path_keyword(const char *keyword) {
|
||||
static const char *const path_keywords[] = {"prefix", "path", "route",
|
||||
"pattern", "url", "endpoint",
|
||||
"rule", "mount_path", "route_path",
|
||||
"url_path", "uri", NULL};
|
||||
if (!keyword) {
|
||||
return false;
|
||||
}
|
||||
for (const char *const *kw = path_keywords; *kw; kw++) {
|
||||
if (strcmp(keyword, *kw) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Locate the path literal and optional handler reference for route registration
|
||||
* APIs. Shared by sequential and parallel call passes to keep FastAPI/Starlette,
|
||||
* Express-style, and keyword-argument registrations on the same semantics. */
|
||||
static inline const char *cbm_pipeline_call_route_path_and_handler(const CBMCall *call,
|
||||
const char **out_handler) {
|
||||
if (out_handler) {
|
||||
*out_handler = NULL;
|
||||
}
|
||||
if (!call) {
|
||||
return NULL;
|
||||
}
|
||||
if (call->first_string_arg && call->first_string_arg[0] == '/') {
|
||||
if (out_handler) {
|
||||
*out_handler = call->second_arg_name;
|
||||
}
|
||||
return call->first_string_arg;
|
||||
}
|
||||
|
||||
const char *found = NULL;
|
||||
for (int ai = 0; ai < call->arg_count && !found; ai++) {
|
||||
const CBMCallArg *ca = &call->args[ai];
|
||||
const char *val = ca->value ? ca->value : ca->expr;
|
||||
if (!val || val[0] != '/') {
|
||||
continue;
|
||||
}
|
||||
if ((ca->keyword && cbm_pipeline_call_arg_is_route_path_keyword(ca->keyword)) ||
|
||||
(!ca->keyword && ca->index == 0)) {
|
||||
found = val;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (out_handler) {
|
||||
for (int ai = 0; ai < call->arg_count; ai++) {
|
||||
const CBMCallArg *ca = &call->args[ai];
|
||||
if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
|
||||
continue;
|
||||
}
|
||||
if (ca->keyword &&
|
||||
(strcmp(ca->keyword, "prefix") == 0 || strcmp(ca->keyword, "name") == 0 ||
|
||||
strcmp(ca->keyword, "tags") == 0)) {
|
||||
continue;
|
||||
}
|
||||
*out_handler = ca->expr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
static inline bool cbm_pipeline_label_is_registry_symbol(const char *label) {
|
||||
return label && (strcmp(label, "Function") == 0 || strcmp(label, "Method") == 0 ||
|
||||
cbm_label_is_type_like(label) || strcmp(label, "Variable") == 0 ||
|
||||
|
||||
@@ -478,6 +478,68 @@ TEST(parallel_unresolved_route_suffix_does_not_emit_self_call) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const char *url_path;
|
||||
int route_registration_calls;
|
||||
} route_registration_count_ctx_t;
|
||||
|
||||
static void count_route_registration_edges(const cbm_gbuf_edge_t *edge, void *ud) {
|
||||
route_registration_count_ctx_t *c = ud;
|
||||
if (!edge || !edge->type || strcmp(edge->type, "CALLS") != 0 || !edge->properties_json) {
|
||||
return;
|
||||
}
|
||||
if (strstr(edge->properties_json, "\"via\":\"route_registration\"") &&
|
||||
strstr(edge->properties_json, c->url_path)) {
|
||||
c->route_registration_calls++;
|
||||
}
|
||||
}
|
||||
|
||||
static int count_route_registration_for_path(cbm_gbuf_t *gbuf, const char *url_path) {
|
||||
route_registration_count_ctx_t c = {.url_path = url_path, .route_registration_calls = 0};
|
||||
cbm_gbuf_foreach_edge(gbuf, count_route_registration_edges, &c);
|
||||
return c.route_registration_calls;
|
||||
}
|
||||
|
||||
TEST(parallel_fastapi_websocket_route_registration_matches_sequential) {
|
||||
char dir[256];
|
||||
snprintf(dir, sizeof(dir), "/tmp/cbm_ws_routes_XXXXXX");
|
||||
ASSERT_TRUE(cbm_mkdtemp(dir) != NULL);
|
||||
|
||||
const char *source =
|
||||
"from fastapi import APIRouter, WebSocket\n\n"
|
||||
"router = APIRouter()\n\n"
|
||||
"@router.websocket('/custom_error/')\n"
|
||||
"async def router_ws_custom_error(websocket: WebSocket):\n"
|
||||
" raise RuntimeError('boom')\n\n"
|
||||
"@router.websocket_route('/router')\n"
|
||||
"async def routerindex(websocket: WebSocket):\n"
|
||||
" await websocket.accept()\n";
|
||||
|
||||
char path[512];
|
||||
snprintf(path, sizeof(path), "%s/app.py", dir);
|
||||
ASSERT_EQ(th_write_file(path, source), 0);
|
||||
|
||||
cbm_file_info_t files[1] = {0};
|
||||
files[0].path = path;
|
||||
files[0].rel_path = (char *)"app.py";
|
||||
files[0].language = CBM_LANG_PYTHON;
|
||||
|
||||
cbm_gbuf_t *seq = run_sequential("cbm_ws_routes", dir, files, 1);
|
||||
cbm_gbuf_t *par = run_parallel("cbm_ws_routes", dir, files, 1, 1);
|
||||
ASSERT_NOT_NULL(seq);
|
||||
ASSERT_NOT_NULL(par);
|
||||
|
||||
ASSERT_EQ(count_route_registration_for_path(seq, "/custom_error/"), 1);
|
||||
ASSERT_EQ(count_route_registration_for_path(seq, "/router"), 1);
|
||||
ASSERT_EQ(count_route_registration_for_path(par, "/custom_error/"), 1);
|
||||
ASSERT_EQ(count_route_registration_for_path(par, "/router"), 1);
|
||||
|
||||
cbm_gbuf_free(seq);
|
||||
cbm_gbuf_free(par);
|
||||
th_rmtree(dir);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── Production pipeline worker-count parity ─────────────────────── */
|
||||
|
||||
enum {
|
||||
@@ -1194,6 +1256,7 @@ SUITE(parallel) {
|
||||
RUN_TEST(parallel_empty_files);
|
||||
RUN_TEST(parallel_args_json_no_overflow);
|
||||
RUN_TEST(parallel_unresolved_route_suffix_does_not_emit_self_call);
|
||||
RUN_TEST(parallel_fastapi_websocket_route_registration_matches_sequential);
|
||||
|
||||
/* Cleanup shared state */
|
||||
parity_teardown();
|
||||
|
||||
Reference in New Issue
Block a user