Skip to content

Commit 6f00af3

Browse files
committed
fix(routes): scope DATA_FLOWS pairing at the test-file boundary (R1 + R2)
Design decision 2026-10-18 (R1 + R2): a handler defined in a test file pairs only with callers in that same file, and a caller in a test file pairs only with handlers in that same file. A test's request to the application, a production caller matched to a test's mock server, and the private mock servers of two tests pairing across files are not data flows of the application. #2689 links every caller of a route to every handler of it, so these pairs were made on main. try_create_data_flow now refuses a pair whose caller or handler is in a test file (cbm_is_test_file, the per-language rule extraction uses) when the two files differ. HTTP_CALLS and HANDLES are unchanged; only the pairing is scoped. The pass log counts the refused pairs (skipped_test_scope). Signed-off-by: Martin Vogel <[email protected]>
1 parent 30bd5aa commit 6f00af3

1 file changed

Lines changed: 64 additions & 13 deletions

File tree

‎src/pipeline/pass_route_nodes.c‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ enum {
3333
#include <stdint.h>
3434
#include "graph_buffer/graph_buffer.h"
3535
#include "foundation/log.h"
36-
#include "service_patterns.h" /* cbm_service_pattern_is_http_route_literal */
36+
#include "service_patterns.h" /* cbm_service_pattern_is_http_route_literal */
37+
#include "helpers.h" /* cbm_is_test_file */
38+
#include "discover/discover.h" /* cbm_language_for_filename */
3739

3840
#include <stdio.h>
3941
#include <string.h>
@@ -686,14 +688,51 @@ static bool http_call_edge_has_valid_route(const cbm_gbuf_edge_t *edge) {
686688
return cbm_service_pattern_is_http_route_literal(url, callee);
687689
}
688690

691+
/* try_create_data_flow's refusal for a pair across the test-file boundary. */
692+
enum { RN_FLOW_TEST_SCOPED = -2 };
693+
694+
/* Is this node defined in a test file, by the extraction's own per-language rule? */
695+
static bool node_in_test_file(const cbm_gbuf_node_t *n) {
696+
if (!n || !n->file_path || !n->file_path[0]) {
697+
return false;
698+
}
699+
const char *slash = strrchr(n->file_path, '/');
700+
const char *base = slash ? slash + SKIP_ONE : n->file_path;
701+
return cbm_is_test_file(n->file_path, cbm_language_for_filename(base));
702+
}
703+
704+
/* R1 + R2 (user decision, 2026-10-18): a handler defined in a test file pairs only with
705+
* callers in that same file, and a caller in a test file pairs only with handlers in that same
706+
* file. A test's request to the application, a production caller matched to a test's mock
707+
* server, and two tests' private mock servers pairing across files are not data flows of the
708+
* application. The HTTP_CALLS / HANDLES edges themselves stay; only the pairing is scoped. */
709+
static bool data_flow_crosses_test_scope(const cbm_gbuf_t *gb, int64_t caller_id,
710+
int64_t handler_id) {
711+
const cbm_gbuf_node_t *caller = cbm_gbuf_find_by_id(gb, caller_id);
712+
const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_id(gb, handler_id);
713+
if (!caller || !handler) {
714+
return false;
715+
}
716+
const char *cf = caller->file_path ? caller->file_path : "";
717+
const char *hf = handler->file_path ? handler->file_path : "";
718+
if (strcmp(cf, hf) == 0) {
719+
return false;
720+
}
721+
return node_in_test_file(caller) || node_in_test_file(handler);
722+
}
723+
689724
/* Try to create a DATA_FLOWS edge between caller and handler via a route.
690-
* Returns: 1=created, 0=skipped (self/duplicate), -1=skipped (has direct call). */
725+
* Returns: 1=created, 0=skipped (self/duplicate), -1=skipped (has direct call),
726+
* RN_FLOW_TEST_SCOPED=skipped (the pair crosses the test-file boundary, R1 + R2). */
691727
static int try_create_data_flow(cbm_gbuf_t *gb, int64_t caller_id, int64_t handler_id,
692728
const cbm_gbuf_node_t *route, const char *edge_type,
693729
const char *caller_props, bool via_infra) {
694730
if (caller_id == handler_id) {
695731
return 0;
696732
}
733+
if (data_flow_crosses_test_scope(gb, caller_id, handler_id)) {
734+
return RN_FLOW_TEST_SCOPED;
735+
}
697736
if (has_direct_call(gb, caller_id, handler_id)) {
698737
return CBM_NOT_FOUND;
699738
}
@@ -755,14 +794,23 @@ typedef struct {
755794
int infra_count;
756795
int flows;
757796
int skipped;
797+
int test_scoped;
758798
} route_flows_t;
759799

800+
typedef struct {
801+
int flows;
802+
int skipped; /* the caller already CALLS the handler */
803+
int test_scoped; /* the pair crosses the test-file boundary (R1 + R2) */
804+
} data_flow_stats_t;
805+
760806
static void route_flow_add(route_flows_t *rf, const cbm_gbuf_edge_t *caller, int64_t handler_id,
761807
const char *edge_type, bool via_infra) {
762808
int rc = try_create_data_flow(rf->gb, caller->source_id, handler_id, rf->route, edge_type,
763809
caller->properties_json, via_infra);
764810
if (rc > 0) {
765811
rf->flows++;
812+
} else if (rc == RN_FLOW_TEST_SCOPED) {
813+
rf->test_scoped++;
766814
} else if (rc < 0) {
767815
rf->skipped++;
768816
}
@@ -795,8 +843,8 @@ static void caller_data_flows(route_flows_t *rf, const cbm_gbuf_edge_t *caller,
795843
* is now the full caller x handler product. A (caller, handler) pair reached twice -- through two
796844
* routes, or directly and via infra -- keeps the order-free property winner of
797845
* cbm_gbuf_insert_edge. */
798-
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route, int *flows,
799-
int *skipped) {
846+
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route,
847+
data_flow_stats_t *st) {
800848
route_flows_t rf = {.gb = gb, .route = route};
801849
cbm_gbuf_find_edges_by_target_type(gb, route->id, "HANDLES", &rf.handles, &rf.handles_count);
802850
cbm_gbuf_find_edges_by_source_type(gb, route->id, "INFRA_MAPS", &rf.infra, &rf.infra_count);
@@ -815,8 +863,9 @@ static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route
815863
for (int i = 0; i < async_count; i++) {
816864
caller_data_flows(&rf, async_edges[i], "ASYNC_CALLS");
817865
}
818-
*flows += rf.flows;
819-
*skipped += rf.skipped;
866+
st->flows += rf.flows;
867+
st->skipped += rf.skipped;
868+
st->test_scoped += rf.test_scoped;
820869
}
821870

822871
/* Find the enclosing proto service for a function by file + line range. */
@@ -919,19 +968,21 @@ static void create_data_flows(cbm_gbuf_t *gb) {
919968
return;
920969
}
921970

922-
int flows = 0;
923-
int skipped = 0;
971+
data_flow_stats_t st = {0};
924972

925973
for (int ri = 0; ri < route_count; ri++) {
926-
create_route_data_flows(gb, routes[ri], &flows, &skipped);
974+
create_route_data_flows(gb, routes[ri], &st);
927975
}
928976

929-
if (flows > 0 || skipped > 0) {
977+
if (st.flows > 0 || st.skipped > 0 || st.test_scoped > 0) {
930978
char buf1[CBM_SZ_16];
931979
char buf2[CBM_SZ_16];
932-
snprintf(buf1, sizeof(buf1), "%d", flows);
933-
snprintf(buf2, sizeof(buf2), "%d", skipped);
934-
cbm_log_info("pass.data_flows", "created", buf1, "skipped_has_call", buf2);
980+
char buf3[CBM_SZ_16];
981+
snprintf(buf1, sizeof(buf1), "%d", st.flows);
982+
snprintf(buf2, sizeof(buf2), "%d", st.skipped);
983+
snprintf(buf3, sizeof(buf3), "%d", st.test_scoped);
984+
cbm_log_info("pass.data_flows", "created", buf1, "skipped_has_call", buf2,
985+
"skipped_test_scope", buf3);
935986
}
936987
}
937988

0 commit comments

Comments
 (0)