Skip to content

Commit a84c59c

Browse files
committed
test(repro): cross-file + trace reproductions for #408, #56, #480
First users of the shared repro_harness.h (validates the multi-file + MCP-tool harness end to end): - #408: STRONGER than the existing weak test_lang_contract.c guard (which any IMPORTS edge satisfies) — fixture has zero relative imports + no dependencies field, so the only possible IMPORTS edge is the workspace cross-package one packages/b -> @org/a; asserts >=1. - #56: Rust multi-crate workspace; crate_b::run calls crate_a::helper; asserts CALLS >= 2 so an intra-crate edge can't mask the missing cross-crate edge. - #480: precondition asserts CALLS>0 (edges exist), then drives the trace_path MCP tool and asserts the caller appears + the result is not the empty "callers":[] shape — isolating it as a traversal bug, not extraction. All assert correct behaviour; RED on current code (board verifies). Signed-off-by: Martin Vogel <[email protected]>
1 parent 463de27 commit a84c59c

5 files changed

Lines changed: 585 additions & 1 deletion

File tree

‎Makefile.cbm‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ TEST_REPRO_SRCS = \
397397
tests/repro/repro_extraction.c \
398398
tests/repro/repro_issue495.c \
399399
tests/repro/repro_issue521.c \
400-
tests/repro/repro_issue382.c
400+
tests/repro/repro_issue382.c \
401+
tests/repro/repro_issue408.c \
402+
tests/repro/repro_issue56.c \
403+
tests/repro/repro_issue480.c
401404

402405
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
403406

‎tests/repro/repro_issue408.c‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* repro_issue408.c — Reproduce-first case for OPEN bug #408.
3+
*
4+
* Issue #408: "package.json `workspaces` cross-repo IMPORTS still produce
5+
* zero edges"
6+
*
7+
* Root cause (pass_pkgmap.c / pipeline.c):
8+
* In a Yarn/Lerna-style JS/TS monorepo, `packages/b` imports a sibling by
9+
* its declared package name (`import { x } from '@org/a'`). pass_pkgmap.c
10+
* is supposed to:
11+
* 1. Walk the repo filesystem for package.json manifests (cbm_pkgmap_scan_repo).
12+
* 2. Parse each sibling package.json, mapping its `"name"` field to its
13+
* entry-point QN (parse_package_json → pkg_entries_push).
14+
* 3. On import resolution (cbm_pipeline_resolve_module), perform an exact
15+
* lookup of `"@org/a"` in the pkgmap hash table to obtain the sibling's
16+
* QN, then produce an IMPORTS edge to that node.
17+
*
18+
* The reporter's debug trace (macOS arm64, v0.7.0) shows that the pkgmap
19+
* pass never emits any `pkgmap.*` log lines:
20+
* pipeline.done nodes=12 edges=9 elapsed_ms=71
21+
* — zero IMPORTS edges despite a bare-specifier workspace import. The
22+
* maintainer confirmed: on macOS/Linux cbm_pkgmap_scan_repo may resolve
23+
* workspace names at the manifest-parse level (cbm_pkgmap_try_parse), but
24+
* the resolved entry-QN is never matched against the in-graph node produced
25+
* by indexing `packages/a/index.js`. The mismatch means the exact-lookup
26+
* in cbm_pipeline_resolve_module (step 3) silently falls through to
27+
* default (unresolved) QN resolution, and no cross-package IMPORTS edge is
28+
* ever produced.
29+
*
30+
* Expected (correct) behaviour:
31+
* Indexing a minimal monorepo:
32+
* root/package.json { "workspaces": ["packages/*"] }
33+
* packages/a/package.json { "name": "@org/a", "main": "index.js" }
34+
* packages/a/index.js export function fromA() { return 1; }
35+
* packages/b/package.json { "name": "@org/b", "main": "index.js" }
36+
* packages/b/index.js import { fromA } from '@org/a';
37+
* export function useA() { return fromA(); }
38+
* must produce AT LEAST ONE IMPORTS edge in the graph.
39+
* (The only possible target of `import … from '@org/a'` is the sibling
40+
* package — there are no relative imports in this fixture.)
41+
*
42+
* Actual (buggy) behaviour:
43+
* rh_count_edges(store, project, "IMPORTS") == 0
44+
* The assertion ASSERT_GTE(imports, 1) FAILS → RED.
45+
*
46+
* Why STRONGER than the existing weak test
47+
* (`contract_edge_workspaces_imports_issue408` in tests/test_lang_contract.c):
48+
*
49+
* The existing test asserts `edge_present(f, 5, "IMPORTS", 1)`, which
50+
* succeeds whenever ANY IMPORTS edge exists in the indexed project. In the
51+
* original test_lang_contract.c fixture this is satisfied trivially by a
52+
* relative import or a self-import resolved within a single package — the
53+
* cross-package bare-specifier resolution is never exercised.
54+
*
55+
* This repro fixture is DESIGNED so the only source of IMPORTS edges is the
56+
* bare-specifier cross-package import in packages/b/index.js:
57+
* import { fromA } from '@org/a';
58+
* Neither packages/a/index.js nor packages/b/index.js contains any
59+
* relative import ("./…") or intra-package import. Therefore:
60+
* rh_count_edges(..., "IMPORTS") >= 1
61+
* is ONLY satisfiable if the cross-package workspace resolution succeeded.
62+
* On current (buggy) code this count is 0, so the assertion is RED.
63+
*
64+
* In addition, the fixture omits `"dependencies"` from packages/b/package.json
65+
* on purpose: workspace resolution must be driven purely by the monorepo
66+
* `"workspaces"` glob, not by an explicit `dependencies` field — matching
67+
* the reporter's minimal repro from the issue comments.
68+
*/
69+
70+
#include "test_framework.h"
71+
#include "repro_harness.h"
72+
73+
/* ── Test ──────────────────────────────────────────────────────────── */
74+
75+
/*
76+
* repro_issue408_workspace_crosspkg_import
77+
*
78+
* Indexes a minimal Yarn-style JS monorepo where packages/b imports
79+
* sibling packages/a by its package.json `"name"` (@org/a). This is
80+
* a PURE CROSS-PACKAGE bare-specifier import: no relative imports exist
81+
* anywhere in the fixture. Therefore the only possible source of an
82+
* IMPORTS edge is the workspace-resolved @org/a reference.
83+
*
84+
* RED if:
85+
* • rh_count_edges(store, project, "IMPORTS") == 0
86+
* (workspace resolution did not produce a cross-package IMPORTS edge)
87+
*/
88+
TEST(repro_issue408_workspace_crosspkg_import) {
89+
/*
90+
* Fixture layout mirrors the reporter's /tmp/cbm-issue408-repro tree
91+
* (issue #408 comment, macOS arm64 canonical repro). Five files:
92+
*
93+
* package.json — root workspace manifest; workspaces glob
94+
* packages/a/package.json — sibling A's manifest; name = "@org/a"
95+
* packages/a/index.js — sibling A; exports fromA (no imports)
96+
* packages/b/package.json — sibling B's manifest; name = "@org/b"
97+
* packages/b/index.js — sibling B; bare-specifier import of @org/a
98+
*
99+
* Note: packages/b/package.json deliberately omits "dependencies" so
100+
* that workspace resolution cannot be driven by that field.
101+
*
102+
* Note: neither .js file contains any relative import; the ONLY import
103+
* statement is `import { fromA } from '@org/a'` in packages/b/index.js.
104+
* Therefore rh_count_edges(..., "IMPORTS") >= 1 is satisfied ONLY if
105+
* the cross-package workspace bare-specifier resolution worked.
106+
*/
107+
static const RFile files[] = {
108+
/* Root workspace manifest */
109+
{
110+
"package.json",
111+
"{\"name\":\"monorepo-root\",\"private\":true,"
112+
"\"workspaces\":[\"packages/*\"]}\n"
113+
},
114+
/* Sibling A — the imported package */
115+
{
116+
"packages/a/package.json",
117+
"{\"name\":\"@org/a\",\"version\":\"1.0.0\","
118+
"\"main\":\"index.js\"}\n"
119+
},
120+
{
121+
"packages/a/index.js",
122+
"export function fromA() {\n"
123+
" return 1;\n"
124+
"}\n"
125+
},
126+
/* Sibling B — the importing package; NO relative imports */
127+
{
128+
"packages/b/package.json",
129+
"{\"name\":\"@org/b\",\"version\":\"1.0.0\","
130+
"\"main\":\"index.js\"}\n"
131+
},
132+
{
133+
"packages/b/index.js",
134+
"import { fromA } from '@org/a';\n"
135+
"\n"
136+
"export function useA() {\n"
137+
" return fromA();\n"
138+
"}\n"
139+
}
140+
};
141+
142+
RProj lp;
143+
cbm_store_t *store = rh_index_files(&lp, files, 5);
144+
ASSERT_NOT_NULL(store);
145+
146+
/*
147+
* Count ALL IMPORTS edges in the project graph.
148+
*
149+
* Because this fixture contains ONLY one import statement and it is a
150+
* bare-specifier workspace reference (`import { fromA } from '@org/a'`),
151+
* the count is:
152+
* ≥ 1 → cross-package workspace resolution worked (correct behaviour)
153+
* 0 → workspace resolution is broken (bug #408, RED)
154+
*
155+
* On current (unfixed) code, pass_pkgmap resolves "@org/a" to a QN that
156+
* does not match any graph node, so cbm_pipeline_resolve_import_node
157+
* falls through to default resolution, producing zero IMPORTS edges.
158+
* This assertion therefore FAILS → RED.
159+
*/
160+
int imports = rh_count_edges(store, lp.project, "IMPORTS");
161+
ASSERT_GTE(imports, 1);
162+
163+
rh_cleanup(&lp, store);
164+
PASS();
165+
}
166+
167+
/* ── Suite ──────────────────────────────────────────────────────────── */
168+
SUITE(repro_issue408) {
169+
RUN_TEST(repro_issue408_workspace_crosspkg_import);
170+
}

‎tests/repro/repro_issue480.c‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)