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.

routes: HANDLES no longer depend on worker order or on substring paths

Martin Vogelac72d189fa07
diff --git a/src/pipeline/pass_route_nodes.c b/src/pipeline/pass_route_nodes.c
index 878dd7f50..11fe8c7d7 100644
--- a/src/pipeline/pass_route_nodes.c
+++ b/src/pipeline/pass_route_nodes.c
@@ -236,6 +236,44 @@ static const char *url_path(const char *url) {
return path ? path : "/";
}
+/* Route path `needle` inside URL path `hay`, ending where a segment does:
+ * `/items` is in `/api/items` and `/items/42`; `/a` is not in `/acidjunk`
+ * (fastapi: a `/a` prefix route matched every github.com/a... URL). */
+static bool path_has_route(const char *hay, const char *needle) {
+ size_t n = strlen(needle);
+ if (n == 0) {
+ return false;
+ }
+ for (const char *p = strstr(hay, needle); p; p = strstr(p + SKIP_ONE, needle)) {
+ char c = p[n];
+ bool start = needle[0] == '/' || p == hay || p[-SKIP_ONE] == '/';
+ bool end = needle[n - SKIP_ONE] == '/' || c == '\0' || c == '/' || c == '?' || c == '#';
+ if (start && end) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* A route prefix's segments (`items`, `api/v1`) as whole segments of a file
+ * path: `app/routers/items.py`, `items/views.py`; `a` is not in
+ * `docs_src/app_testing/main.py`. */
+static bool file_has_segments(const char *path, const char *segs) {
+ size_t n = strlen(segs);
+ if (n == 0) {
+ return false;
+ }
+ for (const char *p = strstr(path, segs); p; p = strstr(p + SKIP_ONE, segs)) {
+ char c = p[n];
+ bool start = p == path || p[-SKIP_ONE] == '/';
+ bool end = segs[n - SKIP_ONE] == '/' || c == '\0' || c == '/' || c == '.';
+ if (start && end) {
+ return true;
+ }
+ }
+ return false;
+}
+
/* Check if a trailing dash-segment is a hash/project-number (strippable).
* Returns true if the segment is short alphanumeric but not a meaningful word. */
static bool is_hash_segment(const char *seg, size_t slen) {
@@ -373,9 +411,12 @@ static int match_one_infra_route(cbm_gbuf_t *gb, const cbm_gbuf_node_t *infra,
}
int path_match =
- (strlen(handler_path) > SKIP_ONE && (strstr(infra_path, handler_path) != NULL ||
- strstr(handler_path, infra_path) != NULL));
- int root_svc_match = (strcmp(handler_path, "/") == 0);
+ (strlen(handler_path) > SKIP_ONE && path_has_route(infra_path, handler_path)) ||
+ (strlen(infra_path) > SKIP_ONE && path_has_route(handler_path, infra_path));
+ /* a root route stands for a URL's service only when its file is that
+ * service's; any prefix or ANY route at `/` matched every URL (a docs
+ * site's chat link took the root handlers) */
+ int root_svc_match = file_matches && strcmp(handler_path, "/") == 0;
if (path_match || root_svc_match) {
if (infra_candidate_is_better(handler_route, best)) {
best = handler_route;
@@ -564,7 +605,7 @@ static int bridge_funcs_to_prefix(cbm_gbuf_t *gb, const cbm_gbuf_node_t *prefix_
if (!func->properties_json || !strstr(func->properties_json, "\"route_path\"")) {
continue;
}
- if (prefix_segs && prefix_segs[0] && !strstr(func->file_path, prefix_segs)) {
+ if (prefix_segs && prefix_segs[0] && !file_has_segments(func->file_path, prefix_segs)) {
continue;
}
cbm_gbuf_insert_edge(gb, func->id, prefix_route->id, "HANDLES",
@@ -592,29 +633,37 @@ static void connect_prefix_to_decorators(cbm_gbuf_t *gb) {
continue;
}
+ const char *prefix_path = prefix_route->name;
+ const char *prefix_segs =
+ (prefix_path && prefix_path[0] == '/') ? prefix_path + SKIP_ONE : prefix_path;
+ /* A root mount (`app.mount("/", ...)`) names no part of a handler's
+ * path, so nothing tells its handlers from the rest of the directory:
+ * bridging it linked every decorated function there (fastapi: 702
+ * handlers to `/`, then to every infra URL through it). */
+ if (!prefix_segs || !prefix_segs[0]) {
+ continue;
+ }
+
const cbm_gbuf_edge_t **calls_in = NULL;
int calls_count = 0;
cbm_gbuf_find_edges_by_target_type(gb, prefix_route->id, "CALLS", &calls_in, &calls_count);
- if (calls_count == 0) {
- continue;
- }
- const cbm_gbuf_node_t *registrar = cbm_gbuf_find_by_id(gb, calls_in[0]->source_id);
- if (!registrar || !registrar->file_path) {
- continue;
- }
- const char *last_slash = strrchr(registrar->file_path, '/');
- if (!last_slash) {
- continue;
+ /* Every registrar's directory, not the first CALLS edge's: their
+ * order is the workers' merge order, so the first one moved between
+ * runs of the same corpus. Edges dedup, so the union is order-free. */
+ for (int ci = 0; ci < calls_count; ci++) {
+ const cbm_gbuf_node_t *registrar = cbm_gbuf_find_by_id(gb, calls_in[ci]->source_id);
+ if (!registrar || !registrar->file_path) {
+ continue;
+ }
+ const char *last_slash = strrchr(registrar->file_path, '/');
+ if (!last_slash) {
+ continue;
+ }
+ int dir_len = (int)(last_slash - registrar->file_path) + SKIP_ONE;
+ connected += bridge_funcs_to_prefix(gb, prefix_route, registrar->file_path, dir_len,
+ prefix_segs);
}
- int dir_len = (int)(last_slash - registrar->file_path) + SKIP_ONE;
-
- const char *prefix_path = prefix_route->name;
- const char *prefix_segs =
- (prefix_path && prefix_path[0] == '/') ? prefix_path + SKIP_ONE : prefix_path;
-
- connected +=
- bridge_funcs_to_prefix(gb, prefix_route, registrar->file_path, dir_len, prefix_segs);
}
if (connected > 0) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index a2861d054..77647762e 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -9073,6 +9073,137 @@ TEST(pipeline_swift_nested_url_makes_route_issue1892) {
PASS();
}
+/* HANDLES edges from the function named `source_name` to Routes whose
+ * qualified name equals `route_qn`. */
+static int handles_to_route(cbm_store_t *s, const char *project, const char *source_name,
+ const char *route_qn) {
+ cbm_edge_t *edges = NULL;
+ int edge_count = 0;
+ if (cbm_store_find_edges_by_type(s, project, "HANDLES", &edges, &edge_count) != CBM_STORE_OK) {
+ return -1;
+ }
+ int matches = 0;
+ for (int i = 0; i < edge_count; i++) {
+ cbm_node_t source = {0};
+ cbm_node_t target = {0};
+ int source_ok = cbm_store_find_node_by_id(s, edges[i].source_id, &source) == CBM_STORE_OK;
+ int target_ok = cbm_store_find_node_by_id(s, edges[i].target_id, &target) == CBM_STORE_OK;
+ if (source_ok && target_ok && source.name && target.qualified_name &&
+ (!source_name || strcmp(source.name, source_name) == 0) &&
+ strcmp(target.qualified_name, route_qn) == 0) {
+ matches++;
+ }
+ cbm_node_free_fields(&source);
+ cbm_node_free_fields(&target);
+ }
+ if (edges) {
+ cbm_store_free_edges(edges, edge_count);
+ }
+ return matches;
+}
+
+/* fastapi (2026-10-05): one binary indexed the same tree to 3,253 or to
+ * 1,119,241 HANDLES edges. A prefix Route took the directory of whichever
+ * registrar's CALLS edge came first (worker order) and linked every decorated
+ * handler under it, with no filter for the root prefix `/`; every infra URL
+ * then copied the handlers of the root route. Now: every registrar counts, a
+ * root mount bridges nothing, and an infra URL matches a root route only of
+ * its own service. */
+TEST(pipeline_route_prefix_bridge_is_order_free_and_root_bridges_nothing) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_routebridge_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ /* two apps mounted at the root and under /api, from two directories */
+ write_temp_file(tmp, "svc/a/main.py",
+ "from fastapi import FastAPI\n"
+ "app = FastAPI()\n"
+ "app.mount(\"/\", None)\n"
+ "app.mount(\"/api\", None)\n"
+ "\n"
+ "@app.get(\"/items\")\n"
+ "def read_items():\n"
+ " return []\n");
+ write_temp_file(tmp, "svc/a/api/routes.py",
+ "from fastapi import APIRouter\n"
+ "router = APIRouter()\n"
+ "\n"
+ "@router.get(\"/ax\")\n"
+ "def api_a():\n"
+ " return 1\n");
+ write_temp_file(tmp, "svc/b/main.py",
+ "from fastapi import FastAPI\n"
+ "app = FastAPI()\n"
+ "app.mount(\"/\", None)\n"
+ "app.mount(\"/api\", None)\n"
+ "app.mount(\"/c\", None)\n"
+ "\n"
+ "@app.get(\"/users\")\n"
+ "def read_users():\n"
+ " return []\n");
+ write_temp_file(tmp, "svc/b/api/routes.py",
+ "from fastapi import APIRouter\n"
+ "router = APIRouter()\n"
+ "\n"
+ "@router.get(\"/by\")\n"
+ "def api_b():\n"
+ " return 2\n");
+ /* a real root handler, and a documentation site's link to a chat server */
+ write_temp_file(tmp, "web/site.py",
+ "from flask import Flask\n"
+ "app = Flask(__name__)\n"
+ "\n"
+ "@app.route(\"/\")\n"
+ "def index():\n"
+ " return \"ok\"\n"
+ "\n"
+ "@app.route(\"/c\")\n"
+ "def contact():\n"
+ " return \"c\"\n");
+ write_temp_file(tmp, "mkdocs.yml",
+ "site_name: demo\n"
+ "extra:\n"
+ " social:\n"
+ " - icon: fontawesome/brands/discord\n"
+ " link: https://discord.com/invite/abc123\n"
+ " - icon: fontawesome/brands/github\n"
+ " link: https://github.com/carol\n");
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/routebridge.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);
+
+ /* decorator routes are untouched */
+ ASSERT_EQ(handles_to_route(s, project, "read_items", "__route__GET__/items"), 1);
+ ASSERT_EQ(handles_to_route(s, project, "index", "__route__ANY__/"), 1);
+ /* a root mount names no part of a handler's path: nothing is bridged */
+ ASSERT_EQ(handles_to_route(s, project, "read_items", "__route__ANY__/"), 0);
+ ASSERT_EQ(handles_to_route(s, project, "read_users", "__route__ANY__/"), 0);
+ /* /api is registered from both directories: both api handlers, whatever
+ * order the registrations' CALLS edges landed in */
+ ASSERT_EQ(handles_to_route(s, project, "api_a", "__route__ANY__/api"), 1);
+ ASSERT_EQ(handles_to_route(s, project, "api_b", "__route__ANY__/api"), 1);
+ /* a prefix and a URL path match on whole segments only: `c` is no segment
+ * of svc/b/main.py, and `/c` is no route of github.com/carol */
+ ASSERT_EQ(handles_to_route(s, project, "read_users", "__route__ANY__/c"), 0);
+ ASSERT_EQ(handles_to_route(s, project, "contact", "__route__ANY__/c"), 1);
+ ASSERT_EQ(handles_to_route(s, project, NULL, "__route__infra__https://github.com/carol"), 0);
+ /* the chat link is no route of this repository's root handler */
+ ASSERT_EQ(
+ handles_to_route(s, project, NULL, "__route__infra__https://discord.com/invite/abc123"), 0);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+}
+
TEST(pipeline_swift_http_call_makes_route_issue1892) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_swifthttp_XXXXXX");
@@ -19431,6 +19562,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);
+ RUN_TEST(pipeline_route_prefix_bridge_is_order_free_and_root_bridges_nothing);
RUN_TEST(pipeline_native_fetch_parallel_classified_as_http_calls);
RUN_TEST(pipeline_local_fetch_shadow_not_classified_as_http);
RUN_TEST(pipeline_typed_http_call_has_no_arg_url_twin);