|
| 1 | +/* |
| 2 | + * repro_issue480.c — Reproduce-first case for OPEN bug #480. |
| 3 | + * |
| 4 | + * Issue: #480 — "trace_path returns empty for all functions despite |
| 5 | + * traversable CALLS edges (v0.8.1, macOS arm64)" |
| 6 | + * |
| 7 | + * Root cause (identified by maintainer DeusData + reporter halindrome): |
| 8 | + * handle_trace_call_path() calls cbm_store_find_nodes_by_name() to locate |
| 9 | + * the start node for BFS. On the affected build, the name-to-node lookup |
| 10 | + * returns node_count == 0 for EVERY function name — even names that the |
| 11 | + * graph clearly contains (confirmed by query_graph Cypher returning the same |
| 12 | + * function with 5–8 inbound CALLS edges). The fallback to |
| 13 | + * cbm_store_find_node_by_qn() also returns nothing, so the handler exits |
| 14 | + * with a "function not found" error OR (when the node IS found by name) |
| 15 | + * the BFS start-node id does not match any edge endpoint stored in the |
| 16 | + * graph, so cbm_store_bfs() returns visited_count == 0 and the "callers" |
| 17 | + * / "callees" JSON arrays are serialised empty. |
| 18 | + * |
| 19 | + * The split: query_graph Cypher (direct SQL) traverses the same edges |
| 20 | + * correctly, while trace_path (BFS via start-node id) yields nothing. |
| 21 | + * This isolates the bug to trace_path's own start-node lookup or to how |
| 22 | + * the resolved node id is passed to cbm_store_bfs(), NOT to edge creation. |
| 23 | + * |
| 24 | + * Expected (correct) behaviour: |
| 25 | + * After indexing a two-function Python file where caller() calls callee(), |
| 26 | + * trace_path for "callee" with direction="inbound" must return a non-empty |
| 27 | + * "callers" array that contains a node named "caller". |
| 28 | + * |
| 29 | + * Actual (buggy) behaviour: |
| 30 | + * trace_path returns {"function":"callee","direction":"inbound","callers":[]} |
| 31 | + * — an empty "callers" array — even though CALLS edges exist in the graph |
| 32 | + * and are walkable via query_graph. |
| 33 | + * |
| 34 | + * Why RED on current code: |
| 35 | + * The precondition assertion (CALLS edges > 0) passes because edge creation |
| 36 | + * is correct. The subsequent assertion that resp contains the string |
| 37 | + * "\"caller\"" (the caller function's name embedded in the callers array) |
| 38 | + * FAILS because cbm_store_bfs() finds no hops from the resolved start node. |
| 39 | + * |
| 40 | + * How this isolates the traversal bug from an extraction bug: |
| 41 | + * If CALLS edges were the problem, rh_count_edges(store, …, "CALLS") would |
| 42 | + * return 0 and the ASSERT_GT precondition would fire RED — visibly flagging |
| 43 | + * an extraction failure instead. By asserting the precondition GREEN and |
| 44 | + * the trace_path result RED, we prove the edges exist and the fault lies |
| 45 | + * exclusively in trace_path's traversal layer. |
| 46 | + * |
| 47 | + * Fix location (not implemented here): |
| 48 | + * cbm_store_find_nodes_by_name() or cbm_store_bfs() in |
| 49 | + * src/store/store.c — the node id returned by name lookup must match |
| 50 | + * the source/target ids stored in the edges table. |
| 51 | + */ |
| 52 | + |
| 53 | +#include <foundation/compat.h> |
| 54 | +#include "test_framework.h" |
| 55 | +#include "repro_harness.h" |
| 56 | + |
| 57 | +#include <string.h> |
| 58 | +#include <stdlib.h> |
| 59 | +#include <stdio.h> |
| 60 | + |
| 61 | +/* ── Fixture ──────────────────────────────────────────────────────────────── |
| 62 | + * |
| 63 | + * Two Python functions in one file: |
| 64 | + * |
| 65 | + * def callee(): |
| 66 | + * return 42 |
| 67 | + * |
| 68 | + * def caller(): |
| 69 | + * return callee() |
| 70 | + * |
| 71 | + * Python has proven reliable CALLS extraction (test_extraction.c:python_calls |
| 72 | + * asserts calls.count > 0 for a simpler fixture; the integration suite's |
| 73 | + * main.py fixture yields CALLS edges that are visible via query_graph). |
| 74 | + * caller() → callee() is a simple, unambiguous intra-file call: the extractor |
| 75 | + * sees exactly one callee() call expression inside caller(), so the graph |
| 76 | + * must have ≥ 1 CALLS edge after indexing. |
| 77 | + */ |
| 78 | +static const RFile k_files[] = { |
| 79 | + { |
| 80 | + "main.py", |
| 81 | + "def callee():\n" |
| 82 | + " return 42\n" |
| 83 | + "\n" |
| 84 | + "def caller():\n" |
| 85 | + " return callee()\n" |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +/* ───────────────────────────────────────────────────────────────────────── |
| 90 | + * repro_issue480_trace_path_nonempty_with_calls |
| 91 | + * |
| 92 | + * Precondition (must be GREEN to prove this is a traversal bug): |
| 93 | + * rh_count_edges(store, project, "CALLS") > 0 |
| 94 | + * |
| 95 | + * The failing assertion (RED on buggy code): |
| 96 | + * The "callers" array in the trace_path response is non-empty and contains |
| 97 | + * the string "caller" (the name of the caller function). |
| 98 | + * ───────────────────────────────────────────────────────────────────────── */ |
| 99 | +TEST(repro_issue480_trace_path_nonempty_with_calls) { |
| 100 | + RProj lp; |
| 101 | + cbm_store_t *store = rh_index_files(&lp, k_files, |
| 102 | + (int)(sizeof(k_files) / sizeof(k_files[0]))); |
| 103 | + ASSERT_NOT_NULL(store); |
| 104 | + |
| 105 | + /* ── Precondition: extraction must have produced ≥ 1 CALLS edge ────── |
| 106 | + * If this fires RED, the fixture or language has an extraction bug — |
| 107 | + * that is a different problem from #480. Switch to a different |
| 108 | + * language fixture (e.g. Go utils.go with Multiply→Add) in that case. */ |
| 109 | + int calls_count = rh_count_edges(store, lp.project, "CALLS"); |
| 110 | + ASSERT_GT(calls_count, 0); |
| 111 | + |
| 112 | + /* ── Invoke trace_path for "callee" with direction="inbound" ───────── |
| 113 | + * |
| 114 | + * Args match the trace_path schema (required: function_name, project): |
| 115 | + * function_name — bare name "callee"; also tested by the reporter with |
| 116 | + * the fully-qualified name, both yield empty on buggy code |
| 117 | + * project — lp.project (derived from tmpdir by cbm_project_name_from_path) |
| 118 | + * direction — "inbound": ask for callers of callee() |
| 119 | + * depth — 2: enough to reach one hop (caller → callee) |
| 120 | + * |
| 121 | + * Expected response shape (correct): |
| 122 | + * {"function":"callee","direction":"inbound","callers":[{"name":"caller",...},...]} |
| 123 | + * |
| 124 | + * Buggy response shape: |
| 125 | + * {"function":"callee","direction":"inbound","callers":[]} |
| 126 | + * (or: {"error":"function not found",...} if the name lookup fails entirely) |
| 127 | + */ |
| 128 | + char args[512]; |
| 129 | + snprintf(args, sizeof(args), |
| 130 | + "{\"function_name\":\"callee\"," |
| 131 | + "\"project\":\"%s\"," |
| 132 | + "\"direction\":\"inbound\"," |
| 133 | + "\"depth\":2}", |
| 134 | + lp.project); |
| 135 | + |
| 136 | + char *resp = cbm_mcp_handle_tool(lp.srv, "trace_path", args); |
| 137 | + ASSERT_NOT_NULL(resp); |
| 138 | + |
| 139 | + /* The response must NOT be a "function not found" error. |
| 140 | + * If the name lookup itself fails, this fires first and pinpoints the |
| 141 | + * start-node lookup as the breakage site. */ |
| 142 | + ASSERT_NULL(strstr(resp, "function not found")); |
| 143 | + |
| 144 | + /* The "callers" key must appear — it is always emitted when direction |
| 145 | + * includes "inbound" (even as an empty array), confirming we reached |
| 146 | + * the BFS step. */ |
| 147 | + ASSERT_NOT_NULL(strstr(resp, "\"callers\"")); |
| 148 | + |
| 149 | + /* The "callers" array must be NON-EMPTY: the string "caller" must |
| 150 | + * appear inside the response as the name of the calling function. |
| 151 | + * |
| 152 | + * WHY RED on buggy code: |
| 153 | + * cbm_store_bfs() starts from nodes[sel].id but that id does not |
| 154 | + * resolve to any edge endpoint in the graph, so visited_count == 0. |
| 155 | + * bfs_to_json_array() serialises an empty JSON array []. |
| 156 | + * strstr(resp, "\"caller\"") returns NULL → ASSERT_NOT_NULL fails. |
| 157 | + * |
| 158 | + * The two-part check below distinguishes the two bug symptoms: |
| 159 | + * 1. empty array → "callers\":[]" present, no "\"caller\"" entry |
| 160 | + * 2. name missing → "callers" array populated with wrong names |
| 161 | + * Either way the final assertion is the one that must turn RED. */ |
| 162 | + ASSERT_NULL(strstr(resp, "\"callers\":[]")); /* empty array = traversal bug */ |
| 163 | + ASSERT_NOT_NULL(strstr(resp, "\"caller\"")); /* caller name in results */ |
| 164 | + |
| 165 | + free(resp); |
| 166 | + rh_cleanup(&lp, store); |
| 167 | + PASS(); |
| 168 | +} |
| 169 | + |
| 170 | +/* ── Suite ─────────────────────────────────────────────────────────────── */ |
| 171 | +SUITE(repro_issue480) { |
| 172 | + RUN_TEST(repro_issue480_trace_path_nonempty_with_calls); |
| 173 | +} |
0 commit comments