|
| 1 | +/* |
| 2 | + * repro_issue514.c -- Reproduce-first case for OPEN bug #514. |
| 3 | + * |
| 4 | + * Issue: #514 -- "trace_path data_flow mode doesn't surface arg expressions; |
| 5 | + * NestJS DI patterns defeat ~70% of caller resolution" |
| 6 | + * |
| 7 | + * Sub-claim reproduced: (A) data_flow mode omits argument expressions. |
| 8 | + * |
| 9 | + * Why sub-claim A over sub-claim B (NestJS DI caller resolution): |
| 10 | + * (A) has a crisp binary assertion: the "e" field either appears in the JSON |
| 11 | + * output or it does not. (B) is a statistical claim (~70% failure rate) that |
| 12 | + * requires a NestJS-specific fixture and a headcount of resolved callers across |
| 13 | + * many call sites -- impossible to assert precisely in a unit test. (A) can |
| 14 | + * be reproduced with a small two-function Python fixture and one strstr check. |
| 15 | + * |
| 16 | + * Root cause: |
| 17 | + * The MCP schema for trace_path documents data_flow mode as "follow CALLS + |
| 18 | + * DATA_FLOWS with arg expressions" (mcp.c line 356-357 and 363-364). Argument |
| 19 | + * expressions at each call site ARE stored in the graph: pass_parallel.c:: |
| 20 | + * append_args_json serializes each CBMCallArg as {"i":<index>,"e":"<expr>,...} |
| 21 | + * into the CALLS edge properties_json column. However, |
| 22 | + * bfs_to_json_array() (mcp.c ~line 2283) only emits the node fields (name, |
| 23 | + * qualified_name, hop, risk, is_test) from cbm_node_hop_t. The edge that |
| 24 | + * carried the arg expressions is NOT propagated by cbm_store_bfs() into the |
| 25 | + * cbm_traverse_result_t (cbm_edge_info_t carries only from_name, to_name, |
| 26 | + * type, confidence -- no properties_json). So even if the user requests |
| 27 | + * mode="data_flow", every hop in the response lacks the "args" field and the |
| 28 | + * individual arg expression text ("e") is permanently absent from the output. |
| 29 | + * |
| 30 | + * Expected (correct) behaviour: |
| 31 | + * After indexing a two-function Python file where caller() passes a compound |
| 32 | + * expression (payload_info + 1) to callee(), a trace_path call with |
| 33 | + * mode="data_flow" and direction="outbound" on "caller" must include the |
| 34 | + * argument expression text "payload_info" in the response JSON -- either in an |
| 35 | + * "args" array inside the hop object, or as a standalone "e" field. |
| 36 | + * |
| 37 | + * Actual (buggy) behaviour: |
| 38 | + * The response is: |
| 39 | + * {"function":"caller","direction":"outbound","mode":"data_flow", |
| 40 | + * "callees":[{"name":"callee","qualified_name":"...","hop":1}]} |
| 41 | + * The hop object contains NO "args" and NO "e"/"arg_expr" field. |
| 42 | + * strstr(resp, "payload_info") returns NULL. |
| 43 | + * |
| 44 | + * Why RED on current code: |
| 45 | + * The precondition assertion (CALLS edges >= 1) passes -- edge creation |
| 46 | + * and arg serialisation in pass_parallel.c are correct. The final |
| 47 | + * ASSERT_NOT_NULL(strstr(resp, "payload_info")) FAILS because |
| 48 | + * bfs_to_json_array() never reads or re-emits edge properties_json, so the |
| 49 | + * arg expression "payload_info" stored in the CALLS edge is permanently |
| 50 | + * discarded before it reaches the MCP JSON output. |
| 51 | + * |
| 52 | + * Fix location (not implemented here): |
| 53 | + * cbm_store_bfs() in src/store/store.c must propagate edge properties_json |
| 54 | + * into the cbm_traverse_result_t (extend cbm_edge_info_t or cbm_node_hop_t). |
| 55 | + * bfs_to_json_array() in src/mcp/mcp.c must then emit an "args" field when |
| 56 | + * mode == "data_flow" and the incoming edge has a non-empty args array. |
| 57 | + */ |
| 58 | + |
| 59 | +#include <foundation/compat.h> |
| 60 | +#include "test_framework.h" |
| 61 | +#include "repro_harness.h" |
| 62 | + |
| 63 | +#include <string.h> |
| 64 | +#include <stdlib.h> |
| 65 | +#include <stdio.h> |
| 66 | + |
| 67 | +/* |
| 68 | + * Fixture: two Python functions in one file. |
| 69 | + * |
| 70 | + * def callee(x): |
| 71 | + * return x * 2 |
| 72 | + * |
| 73 | + * def caller(): |
| 74 | + * result = callee(payload_info + 1) |
| 75 | + * return result |
| 76 | + * |
| 77 | + * caller() passes the compound expression (payload_info + 1) as the first |
| 78 | + * positional argument to callee(). The extractor captures this as a CBMCallArg |
| 79 | + * with .expr == "payload_info + 1" (or a prefix thereof after sanitization). |
| 80 | + * append_args_json serializes it into the CALLS edge as: |
| 81 | + * {"args":[{"i":0,"e":"payload_info + 1"}]} |
| 82 | + * |
| 83 | + * The expression token "payload_info" is unique enough to identify in the |
| 84 | + * output: strstr(resp, "payload_info") is the assertion anchor. |
| 85 | + * |
| 86 | + * Python is used here because its CALLS extraction (including arg expressions) |
| 87 | + * is proven reliable -- see repro_issue480.c for the same fixture approach. |
| 88 | + */ |
| 89 | +static const RFile k_files[] = { |
| 90 | + { |
| 91 | + "service.py", |
| 92 | + "def callee(x):\n" |
| 93 | + " return x * 2\n" |
| 94 | + "\n" |
| 95 | + "def caller():\n" |
| 96 | + " result = callee(payload_info + 1)\n" |
| 97 | + " return result\n" |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +/* |
| 102 | + * TEST: repro_issue514_data_flow_surfaces_arg_expr |
| 103 | + * |
| 104 | + * Precondition (must be GREEN to prove this is a data_flow surfacing bug): |
| 105 | + * rh_count_edges(store, project, "CALLS") >= 1 |
| 106 | + * If this fires RED, the extractor has a regression unrelated to #514. |
| 107 | + * |
| 108 | + * Failing assertion (RED on current code): |
| 109 | + * strstr(resp, "payload_info") != NULL |
| 110 | + * i.e. the argument expression text must appear somewhere in the response. |
| 111 | + */ |
| 112 | +TEST(repro_issue514_data_flow_surfaces_arg_expr) { |
| 113 | + RProj lp; |
| 114 | + cbm_store_t *store = rh_index_files(&lp, k_files, |
| 115 | + (int)(sizeof(k_files) / sizeof(k_files[0]))); |
| 116 | + ASSERT_NOT_NULL(store); |
| 117 | + |
| 118 | + /* |
| 119 | + * Precondition: at least one CALLS edge must exist after indexing. |
| 120 | + * If this fires RED the fixture is broken, not data_flow mode. |
| 121 | + * The caller() -> callee(payload_info + 1) call must produce one edge. |
| 122 | + */ |
| 123 | + int calls_count = rh_count_edges(store, lp.project, "CALLS"); |
| 124 | + fprintf(stderr, |
| 125 | + " [514] CALLS edges=%d (expected>=1; 0=extraction regression)\n", |
| 126 | + calls_count); |
| 127 | + ASSERT_GT(calls_count, 0); |
| 128 | + |
| 129 | + /* |
| 130 | + * Invoke trace_path with mode="data_flow", direction="outbound" on "caller". |
| 131 | + * |
| 132 | + * Args (matching the trace_path JSON schema in mcp.c ~line 355-374): |
| 133 | + * function_name -- "caller": the function that passes the argument |
| 134 | + * project -- lp.project: derived from the temp dir |
| 135 | + * direction -- "outbound": follow callees (caller -> callee) |
| 136 | + * depth -- 2: one hop is enough |
| 137 | + * mode -- "data_flow": the mode that promises arg expressions |
| 138 | + * |
| 139 | + * Expected response (correct): |
| 140 | + * {"function":"caller","direction":"outbound","mode":"data_flow", |
| 141 | + * "callees":[{"name":"callee","qualified_name":"...","hop":1, |
| 142 | + * "args":[{"i":0,"e":"payload_info + 1"}]}]} |
| 143 | + * -- or any JSON structure that includes the string "payload_info". |
| 144 | + * |
| 145 | + * Buggy response: |
| 146 | + * {"function":"caller","direction":"outbound","mode":"data_flow", |
| 147 | + * "callees":[{"name":"callee","qualified_name":"...","hop":1}]} |
| 148 | + * -- no "args", no "e", no "payload_info" anywhere. |
| 149 | + */ |
| 150 | + char args[512]; |
| 151 | + snprintf(args, sizeof(args), |
| 152 | + "{\"function_name\":\"caller\"," |
| 153 | + "\"project\":\"%s\"," |
| 154 | + "\"direction\":\"outbound\"," |
| 155 | + "\"depth\":2," |
| 156 | + "\"mode\":\"data_flow\"}", |
| 157 | + lp.project); |
| 158 | + |
| 159 | + char *resp = cbm_mcp_handle_tool(lp.srv, "trace_path", args); |
| 160 | + ASSERT_NOT_NULL(resp); |
| 161 | + |
| 162 | + fprintf(stderr, " [514] trace_path data_flow response: %.400s\n", resp); |
| 163 | + |
| 164 | + /* The response must not be an error -- the node must be found. */ |
| 165 | + ASSERT_NULL(strstr(resp, "function not found")); |
| 166 | + |
| 167 | + /* The "callees" key must appear (direction=outbound always emits it). */ |
| 168 | + ASSERT_NOT_NULL(strstr(resp, "\"callees\"")); |
| 169 | + |
| 170 | + /* The callees array must be non-empty: "callee" must appear as a hop. |
| 171 | + * If this fires RED, the CALLS traversal is broken (separate from #514). */ |
| 172 | + ASSERT_NULL(strstr(resp, "\"callees\":[]")); |
| 173 | + ASSERT_NOT_NULL(strstr(resp, "\"callee\"")); |
| 174 | + |
| 175 | + /* |
| 176 | + * THE CORE ASSERTION FOR BUG #514: |
| 177 | + * |
| 178 | + * The argument expression "payload_info" (part of "payload_info + 1" passed |
| 179 | + * to callee()) must appear in the response JSON when mode="data_flow". |
| 180 | + * |
| 181 | + * WHY RED on current code: |
| 182 | + * bfs_to_json_array() (mcp.c ~line 2283) only emits cbm_node_hop_t fields |
| 183 | + * (name, qualified_name, hop). cbm_edge_info_t (store.h ~line 146) does |
| 184 | + * not carry properties_json, so the "e":"payload_info + 1" stored in the |
| 185 | + * CALLS edge never reaches the JSON output. strstr returns NULL. |
| 186 | + * |
| 187 | + * This assertion is the canonical RED line for bug #514. |
| 188 | + */ |
| 189 | + ASSERT_NOT_NULL(strstr(resp, "payload_info")); |
| 190 | + |
| 191 | + free(resp); |
| 192 | + rh_cleanup(&lp, store); |
| 193 | + PASS(); |
| 194 | +} |
| 195 | + |
| 196 | +/* ── Suite ─────────────────────────────────────────────────────────────────── */ |
| 197 | +SUITE(repro_issue514) { |
| 198 | + RUN_TEST(repro_issue514_data_flow_surfaces_arg_expr); |
| 199 | +} |
0 commit comments