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(store): skip test-registered routes and rank indexed entry points first

Bernhard Jackiewicz7c81674f9b2b
diff --git a/src/store/architecture_projection.c b/src/store/architecture_projection.c
index c79a766e1..5d76944bc 100644
--- a/src/store/architecture_projection.c
+++ b/src/store/architecture_projection.c
@@ -73,13 +73,13 @@ static const char *const ap_types[] = {
"IMPLEMENTS", "INHERITS", "CONFIGURES", "WRITES", "READS", "USAGE",
"PUBLISHES", "SUBSCRIBES", "DEFINES", "DEFINES_METHOD", "CONTAINS", "EMITS",
"LISTENS_ON", "HANDLES", "DATA_FLOWS", "INFRA_MAPS"};
-enum { AP_DEFINES = 14, AP_METHOD = 15, AP_CONTAINS = 16, AP_HANDLES = 19 };
+enum { AP_CALLS = 1, AP_DEFINES = 14, AP_METHOD = 15, AP_CONTAINS = 16, AP_HANDLES = 19 };
typedef struct {
int64_t id;
char *name, *qn, *file, *label;
int line, end, atom, component, degree;
- bool entry, structural, declared, test, test_property;
+ bool entry, handler, route, callable, structural, declared, test, test_property;
} ap_node;
typedef struct {
int64_t id;
@@ -254,6 +254,35 @@ static int ap_counts(ap_context *c, const char *project) {
return CBM_STORE_OK;
}
+/* Web code enters through route handlers, not main: HANDLES runs from a
+ * Function/Method to its Route (atlas_flows.c scores them alike). A CALLS edge
+ * into a Route is the call that registered it, as pass_route_nodes.c reads it.
+ * Handlers in test code are fixtures, and so are the handlers of routes that
+ * only test code registers: test-client calls like self.client.get("/x/") are
+ * indexed as registrations. Routes without a registration call (decorators)
+ * keep their handlers. Indexed entry points stay ahead of handlers. */
+static int ap_route_handlers(ap_context *c) {
+ enum { AP_PRODUCTION_REGISTRAR = 1, AP_TEST_REGISTRAR = 2 };
+ unsigned char *registrars = calloc((size_t)c->n + 1, sizeof(*registrars));
+ if (!registrars)
+ return CBM_STORE_ERR;
+ for (int i = 0; i < c->m; i++) {
+ const ap_edge *e = &c->edges[i];
+ if (e->type == AP_CALLS && c->nodes[e->target].route)
+ registrars[e->target] |=
+ c->nodes[e->source].test ? AP_TEST_REGISTRAR : AP_PRODUCTION_REGISTRAR;
+ }
+ for (int i = 0; i < c->m; i++) {
+ const ap_edge *e = &c->edges[i];
+ ap_node *handler = &c->nodes[e->source];
+ if (e->type == AP_HANDLES && c->nodes[e->target].route && handler->callable &&
+ !handler->test && !handler->entry && registrars[e->target] != AP_TEST_REGISTRAR)
+ handler->handler = true;
+ }
+ free(registrars);
+ return CBM_STORE_OK;
+}
+
static int ap_load(ap_context *c, const char *project) {
c->nodes = calloc((size_t)c->total_nodes + 1, sizeof(*c->nodes));
c->edges = calloc((size_t)c->total_edges + 1, sizeof(*c->edges));
@@ -290,6 +319,8 @@ static int ap_load(ap_context *c, const char *project) {
n->declared = !strcmp(n->label, "Module") || !strcmp(n->label, "Package") ||
!strcmp(n->label, "Namespace") || !strcmp(n->label, "Class") ||
!strcmp(n->label, "Struct") || !strcmp(n->label, "Interface");
+ n->route = !strcmp(n->label, "Route");
+ n->callable = !strcmp(n->label, "Function") || !strcmp(n->label, "Method");
n->atom = c->n - 1;
n->component = -1;
}
@@ -314,13 +345,6 @@ static int ap_load(ap_context *c, const char *project) {
if (source < 0 || target < 0)
continue;
c->edges[c->m++] = (ap_edge){sqlite3_column_int64(stmt, 0), source, target, type};
- /* Web code enters through route handlers, not main: HANDLES runs from
- * a Function/Method to its Route (atlas_flows.c scores them alike).
- * Handlers in test code are fixtures, not entry points. */
- ap_node *handler = &c->nodes[source];
- if (type == AP_HANDLES && !strcmp(c->nodes[target].label, "Route") && !handler->test &&
- (!strcmp(handler->label, "Function") || !strcmp(handler->label, "Method")))
- handler->entry = true;
if (ap_dependency(type)) {
c->nodes[source].degree++;
c->nodes[target].degree++;
@@ -328,7 +352,7 @@ static int ap_load(ap_context *c, const char *project) {
}
}
sqlite3_finalize(stmt);
- return step == SQLITE_DONE ? CBM_STORE_OK : CBM_STORE_ERR;
+ return step == SQLITE_DONE ? ap_route_handlers(c) : CBM_STORE_ERR;
}
/* Ownership is grounded in declared module/type names and explicit definition
@@ -1873,11 +1897,14 @@ static int ap_render(ap_context *c, const char *project, char **out_json) {
rc = CBM_STORE_ERR;
goto done;
}
- for (int i = 0; i < c->n; i++) {
- ap_node *node = &c->nodes[i];
- if (node->component < 0)
- continue;
- if (node->entry) {
+ /* Indexed entry points first, then route handlers, each in id order:
+ * paths take their slots in this order, so many handlers cannot crowd
+ * main out of the default paths. */
+ for (int pass = 0; pass < 2; pass++)
+ for (int i = 0; i < c->n; i++) {
+ ap_node *node = &c->nodes[i];
+ if (node->component < 0 || !(pass ? node->handler : node->entry))
+ continue;
entry_total++;
if (entry_count < AP_ENTRYPOINTS) {
entry_indexes[entry_count++] = i;
@@ -1887,7 +1914,6 @@ static int ap_render(ap_context *c, const char *project, char **out_json) {
c->evidence_scope = AP_EVIDENCE_CONTEXT;
}
}
- }
c->evidence_scope = AP_EVIDENCE_PATH;
if (c->options.target_node_id) {
/* The target query produces exact endpoint witnesses below. */
diff --git a/tests/test_architecture_projection.c b/tests/test_architecture_projection.c
index 9e2414426..1468b3323 100644
--- a/tests/test_architecture_projection.c
+++ b/tests/test_architecture_projection.c
@@ -1265,7 +1265,9 @@ TEST(projection_python_test_files_follow_name_conventions) {
PASS();
}
-/* Django views are never named main; their HANDLES edge is the entry evidence. */
+/* Django views are never named main; their HANDLES edge is the entry evidence.
+ * Test-client calls like self.client.get("/probe/") are indexed as route
+ * registrations, so a route that only test code registers proves nothing. */
TEST(projection_route_handlers_are_entry_points) {
cbm_store_t *store = projection_store();
ASSERT_NOT_NULL(store);
@@ -1279,25 +1281,49 @@ TEST(projection_route_handlers_are_entry_points) {
int64_t query =
projection_node(store, "Function", "projection.shop.query.orders", "shop/query.py", false);
projection_node(store, "Function", "projection.shop.views.unrouted", "shop/views.py", false);
+ int64_t cart_route =
+ projection_node(store, "Route", "projection.__route__GET__/cart", "", false);
+ int64_t urls =
+ projection_node(store, "Function", "projection.shop.urls.register", "shop/urls.py", false);
+ int64_t cart =
+ projection_node(store, "Function", "projection.shop.views.cart", "shop/views.py", false);
+ int64_t probe_route =
+ projection_node(store, "Route", "projection.__route__GET__/probe", "", false);
+ int64_t probe = projection_node(store, "Method", "projection.tests.test_views.Tests.test_probe",
+ "tests/test_views.py", false);
+ int64_t text = projection_node(store, "Method", "projection.shop.models.Order.text",
+ "shop/models.py", false);
+ /* Created last, so only the ranking can put the indexed entry first. */
+ int64_t launcher =
+ projection_node(store, "Function", "projection.manage.main", "manage.py", true);
+ int64_t setup =
+ projection_node(store, "Function", "projection.manage.setup", "manage.py", false);
projection_edge(store, view, route, "HANDLES");
projection_edge(store, method, route, "HANDLES");
projection_edge(store, fixture, route, "HANDLES");
projection_edge(store, view, query, "CALLS");
+ projection_edge(store, urls, cart_route, "CALLS");
+ projection_edge(store, cart, cart_route, "HANDLES");
+ projection_edge(store, probe, probe_route, "CALLS");
+ projection_edge(store, text, probe_route, "HANDLES");
+ projection_edge(store, launcher, setup, "CALLS");
char *json = NULL;
ASSERT_EQ(cbm_store_architecture_projection(store, "projection", NULL, &json), CBM_STORE_OK);
yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *entries = projection_field(doc, "entrypoints");
- ASSERT_EQ(yyjson_arr_size(entries), 2);
- ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(yyjson_arr_get(entries, 0), "id")), view);
- ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(yyjson_arr_get(entries, 1), "id")), method);
- ASSERT_EQ(yyjson_get_int(yyjson_obj_get(projection_field(doc, "totals"), "entrypoints")), 2);
+ const int64_t expected[] = {launcher, view, method, cart};
+ ASSERT_EQ(yyjson_arr_size(entries), sizeof(expected) / sizeof(expected[0]));
+ for (size_t i = 0; i < sizeof(expected) / sizeof(expected[0]); i++)
+ ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(yyjson_arr_get(entries, i), "id")), expected[i]);
+ ASSERT_EQ(yyjson_get_int(yyjson_obj_get(projection_field(doc, "totals"), "entrypoints")), 4);
yyjson_val *paths = projection_field(doc, "paths");
- ASSERT_EQ(yyjson_arr_size(paths), 1);
- yyjson_val *path = yyjson_arr_get(paths, 0);
- ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(path, "entrypoint_id")), view);
+ ASSERT_EQ(yyjson_arr_size(paths), 2);
+ yyjson_val *first = yyjson_arr_get(paths, 0), *second = yyjson_arr_get(paths, 1);
+ ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(first, "entrypoint_id")), launcher);
+ ASSERT_EQ(yyjson_get_sint(yyjson_obj_get(second, "entrypoint_id")), view);
ASSERT_EQ(
- yyjson_get_sint(yyjson_obj_get(yyjson_arr_get(yyjson_obj_get(path, "nodes"), 1), "id")),
+ yyjson_get_sint(yyjson_obj_get(yyjson_arr_get(yyjson_obj_get(second, "nodes"), 1), "id")),
query);
yyjson_doc_free(doc);
free(json);