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.

test(repro): -Itests fix + reproductions for #495, #521, #382

Martin Vogel8423b0b2748a
diff --git a/Makefile.cbm b/Makefile.cbm
index 1ad8b4861..99d2956fd 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -394,7 +394,10 @@ TEST_STACK_OVERFLOW_SRCS = tests/test_stack_overflow.c
# Kept out of the gating `make test` so `ci-ok` stays green; run via `make test-repro`.
TEST_REPRO_SRCS = \
tests/repro/repro_main.c \
- tests/repro/repro_extraction.c
+ tests/repro/repro_extraction.c \
+ tests/repro/repro_issue495.c \
+ tests/repro/repro_issue521.c \
+ tests/repro/repro_issue382.c
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)
@@ -517,7 +520,7 @@ test: $(BUILD_DIR)/test-runner
# and TEST_REPRO_SRCS instead of ALL_TEST_SRCS. Exits non-zero while any bug is
# still reproduced (the expected state); bug-repro.yml surfaces it as a board.
$(BUILD_DIR)/test-repro-runner: $(TEST_REPRO_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) | $(BUILD_DIR)
- $(CC) $(CFLAGS_TEST) -o $@ \
+ $(CC) $(CFLAGS_TEST) -Itests -o $@ \
$(TEST_REPRO_SRCS) $(PROD_SRCS) \
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
$(OBJS_VENDORED_TEST) \
diff --git a/tests/repro/repro_issue382.c b/tests/repro/repro_issue382.c
new file mode 100644
index 000000000..c4669c316
--- /dev/null
+++ b/tests/repro/repro_issue382.c
@@ -0,0 +1,189 @@
+/*
+ * repro_issue382.c — Reproduce-first case for OPEN bug #382.
+ *
+ * Bug #382: "Java: @Annotation, signatures, and all AST properties missing
+ * from graph nodes"
+ *
+ * Root cause (confirmed by maintainer + reporter re-open):
+ * extract_decorators() in internal/cbm/extract_defs.c first scans
+ * ts_node_prev_sibling() looking for nodes of type "annotation" /
+ * "marker_annotation". In the Java AST emitted by tree-sitter-java, those
+ * nodes are NOT prev-siblings of either the class_declaration or the
+ * method_declaration — they live INSIDE the node's own `modifiers` child:
+ *
+ * class_declaration
+ * modifiers
+ * marker_annotation <- @Entity
+ * marker_annotation <- @RestController
+ * type_identifier: "User"
+ * class_body
+ * method_declaration
+ * modifiers
+ * marker_annotation <- @Override
+ * annotation <- @GetMapping("/users")
+ * type_identifier: "String"
+ * ...
+ *
+ * The code does have a fallback that calls find_jvm_modifiers() to search
+ * the `modifiers` child when prev-sibling count == 0, which covers the
+ * simple @GetMapping-on-method case already tested in test_extraction.c
+ * (extract_java_method_annotations_issue382, which passes green on v0.7.0).
+ *
+ * What is NOT covered by that existing test:
+ * a) CLASS-LEVEL annotations (@Entity, @RestController) on the class node
+ * itself — the existing test only extracts Method nodes; it never
+ * checks the Class node's .decorators.
+ * b) marker_annotation (no-arg form, e.g. @Override, @Entity) on methods
+ * — the existing test uses @GetMapping("/x") which is a full
+ * `annotation` node with arguments and does a substring match against
+ * the whole text "@GetMapping(\"/x\")". marker_annotations have a
+ * different tree-sitter node type and are historically mis-counted.
+ * c) Multiple stacked annotations on a single method/class.
+ *
+ * These cases regress when the fallback path is absent or broken (e.g. the
+ * fix only wired the method path, not the class path, or it works for
+ * `annotation` nodes but not `marker_annotation`).
+ *
+ * Expected (correct) behaviour:
+ * - The Class def for "User" carries decorators:
+ * decorators[0] contains "Entity"
+ * decorators[1] contains "RestController" (or vice-versa)
+ * - The Method def for "getUser" carries decorators:
+ * at least one entry contains "Override"
+ * at least one entry contains "GetMapping"
+ * - method "getUser" has a non-empty signature.
+ *
+ * Actual (buggy) behaviour:
+ * - Class def for "User": decorators == NULL (no annotations extracted)
+ * - Method def for "getUser": marker_annotation @Override is dropped;
+ * decorators may be NULL or miss @Override.
+ * → assertions below are RED on current code if either path is broken.
+ *
+ * Why this is STRONGER than the existing test_extraction.c #382 reference:
+ * 1. It asserts decorators on the CLASS node — never checked before.
+ * 2. It specifically asserts that a marker_annotation (@Override, @Entity)
+ * is captured, not just a full annotation with arguments.
+ * 3. It asserts BOTH annotations on a multi-annotated class, exercising the
+ * count loop that must find > 1 entry.
+ * 4. It uses ASSERT_NOT_NULL(m->decorators) before touching decorators[i],
+ * so a NULL decorators field fails loudly rather than crashing/skipping.
+ */
+
+#include "test_framework.h"
+#include "cbm.h"
+
+/* Convenience: extract one file, return result (caller frees). */
+static CBMFileResult *rx(const char *src, CBMLanguage lang,
+ const char *proj, const char *path) {
+ return cbm_extract_file(src, (int)strlen(src), lang, proj, path,
+ 0, NULL, NULL);
+}
+
+/* Return the first definition whose label AND name both match (either may be
+ * NULL to wildcard). Mirrors the helper in repro_extraction.c. */
+static CBMDefinition *find_def(CBMFileResult *r, const char *label,
+ const char *name) {
+ for (int i = 0; i < r->defs.count; i++) {
+ CBMDefinition *d = &r->defs.items[i];
+ if (label && (!d->label || strcmp(d->label, label) != 0))
+ continue;
+ if (name && (!d->name || strcmp(d->name, name) != 0))
+ continue;
+ return d;
+ }
+ return NULL;
+}
+
+/* Return 1 if any entry in the NULL-terminated decorators array contains
+ * needle as a substring. */
+static int decorators_contain(const CBMDefinition *d, const char *needle) {
+ if (!d || !d->decorators)
+ return 0;
+ for (int i = 0; d->decorators[i]; i++) {
+ if (strstr(d->decorators[i], needle))
+ return 1;
+ }
+ return 0;
+}
+
+/* ───────────────────────────────────────────────────────────────────
+ * repro_issue382_java_annotations_on_nodes
+ *
+ * Asserts that BOTH the Class node AND the Method node produced by
+ * cbm_extract_file carry their Java annotations in .decorators:
+ *
+ * @Entity
+ * @RestController
+ * public class User {
+ * @Override
+ * @GetMapping("/users")
+ * public String getUser(String id) { return id; }
+ * }
+ *
+ * RED if:
+ * • The Class "User" has decorators == NULL (class-level annots dropped)
+ * • The Class "User" decorators do not contain "Entity"
+ * • The Class "User" decorators do not contain "RestController"
+ * • The Method "getUser" has decorators == NULL (method-level annots dropped)
+ * • The Method "getUser" decorators do not contain "Override" ← marker_annotation
+ * • The Method "getUser" decorators do not contain "GetMapping" ← annotation
+ * • The Method "getUser" has NULL or empty signature
+ * ─────────────────────────────────────────────────────────────────── */
+TEST(repro_issue382_java_annotations_on_nodes) {
+ CBMFileResult *r = rx(
+ "@Entity\n"
+ "@RestController\n"
+ "public class User {\n"
+ " @Override\n"
+ " @GetMapping(\"/users\")\n"
+ " public String getUser(String id) { return id; }\n"
+ "}\n",
+ CBM_LANG_JAVA, "t", "User.java");
+
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* ── Class node: two class-level marker_annotations ── */
+ CBMDefinition *cls = find_def(r, "Class", "User");
+ ASSERT_NOT_NULL(cls);
+
+ /* The Class def MUST carry a non-NULL decorators array.
+ * RED if class-level annotations are silently dropped. */
+ ASSERT_NOT_NULL(cls->decorators);
+
+ /* @Entity (marker_annotation) must be present on the Class. */
+ ASSERT_TRUE(decorators_contain(cls, "Entity"));
+
+ /* @RestController (marker_annotation) must also be present. */
+ ASSERT_TRUE(decorators_contain(cls, "RestController"));
+
+ /* ── Method node: one marker_annotation + one annotation ── */
+ CBMDefinition *method = find_def(r, "Method", "getUser");
+ ASSERT_NOT_NULL(method);
+
+ /* Method decorators must be non-NULL. */
+ ASSERT_NOT_NULL(method->decorators);
+
+ /* @Override is a marker_annotation (no argument list) — historically
+ * the most likely to be missed if the extractor only handles the
+ * `annotation` node type but not `marker_annotation`. */
+ ASSERT_TRUE(decorators_contain(method, "Override"));
+
+ /* @GetMapping("/users") is a full annotation (with argument) — this is
+ * what the existing test_extraction.c case checks; include it here too
+ * so we catch any regression. */
+ ASSERT_TRUE(decorators_contain(method, "GetMapping"));
+
+ /* Signature must be extracted: Java method_declaration has a `parameters`
+ * field that the extractor reads into def.signature. */
+ ASSERT_NOT_NULL(method->signature);
+ ASSERT_TRUE(method->signature[0] != '\0');
+
+ cbm_free_result(r);
+ PASS();
+}
+
+/* ── Suite ──────────────────────────────────────────────────────── */
+SUITE(repro_issue382) {
+ RUN_TEST(repro_issue382_java_annotations_on_nodes);
+}
diff --git a/tests/repro/repro_issue495.c b/tests/repro/repro_issue495.c
new file mode 100644
index 000000000..82e06b87c
--- /dev/null
+++ b/tests/repro/repro_issue495.c
@@ -0,0 +1,212 @@
+/*
+ * repro_issue495.c — Reproduce-first case for issue #495:
+ * "cfg-gated twin functions collapse into one node; get_code_snippet
+ * returns the inactive branch's body"
+ *
+ * ROOT CAUSE (extraction layer):
+ * extract_func_def() computes:
+ * def.qualified_name = cbm_fqn_compute(project, rel_path, name)
+ * for every Rust function_item it visits. Two same-named functions
+ * guarded by mutually-exclusive #[cfg(...)] attributes both parse as
+ * distinct function_item nodes and both pass through extract_func_def,
+ * but they receive the SAME qualified_name (no cfg predicate is folded
+ * in). When the graph store upserts them it hits the UNIQUE(project,
+ * qualified_name) constraint and the second write silently overwrites
+ * the first — one branch is lost entirely.
+ *
+ * EXPECTED (correct) behavior:
+ * Each cfg-gated twin must receive a DISTINCT qualified_name that
+ * encodes its cfg predicate, e.g.
+ * "t.src.try_extract_pdf_text" (active / feature branch)
+ * "t.src.try_extract_pdf_text#cfg(not(feature=\"rag-pdf\"))" (stub)
+ * So that the graph can keep BOTH nodes and get_code_snippet can return
+ * the correct body for the requested cfg context.
+ *
+ * ACTUAL (buggy) behavior:
+ * Both defs carry identical qualified_name "t.src.try_extract_pdf_text".
+ * The assertion `qn_a != qn_b` FAILS (both equal the same string), so
+ * this test is RED on unpatched code.
+ *
+ * SECONDARY assertions (also RED until fixed, targeting the same root
+ * cause from different angles):
+ * • The REAL-body function has param name "bytes" (no underscore);
+ * the STUB has "_bytes". Each def's signature must correspond to its
+ * own branch — i.e. BOTH signatures must appear in the result, one
+ * containing "bytes" without a leading underscore and one with "_bytes".
+ * • Each def's decorators[0] must contain the cfg predicate of ITS OWN
+ * branch (not the other's), so that a fixer can easily scope-qualify
+ * the QN from the already-captured decorator text.
+ *
+ * Why these assertions are RED on current code:
+ * All three assertions require distinguishing the two defs by their QN.
+ * Since both QNs are currently identical, any loop looking for "the
+ * active branch" finds the SAME node twice, and the body-token /
+ * decorator checks collapse to checking ONE def against itself.
+ */
+
+#include "test_framework.h"
+#include "cbm.h"
+
+/* ── Helpers ──────────────────────────────────────────────────────── */
+
+/* Extract a Rust source string and return the raw CBMFileResult.
+ * Caller must cbm_free_result() the returned pointer. */
+static CBMFileResult *rx(const char *src, const char *proj, const char *path) {
+ return cbm_extract_file(src, (int)strlen(src), CBM_LANG_RUST, proj, path, 0, NULL, NULL);
+}
+
+/* Count how many defs in r have exactly this label AND name. */
+static int count_defs_named(CBMFileResult *r, const char *label, const char *name) {
+ int n = 0;
+ for (int i = 0; i < r->defs.count; i++) {
+ CBMDefinition *d = &r->defs.items[i];
+ if (label && (!d->label || strcmp(d->label, label) != 0))
+ continue;
+ if (name && (!d->name || strcmp(d->name, name) != 0))
+ continue;
+ n++;
+ }
+ return n;
+}
+
+/* Return the Nth (0-based) def matching label + name, or NULL. */
+static CBMDefinition *nth_def_named(CBMFileResult *r, const char *label, const char *name, int nth) {
+ int seen = 0;
+ for (int i = 0; i < r->defs.count; i++) {
+ CBMDefinition *d = &r->defs.items[i];
+ if (label && (!d->label || strcmp(d->label, label) != 0))
+ continue;
+ if (name && (!d->name || strcmp(d->name, name) != 0))
+ continue;
+ if (seen == nth)
+ return d;
+ seen++;
+ }
+ return NULL;
+}
+
+/* ── Test ─────────────────────────────────────────────────────────── */
+
+/*
+ * Rust source with two mutually-exclusive cfg-gated definitions of the
+ * same function. Tree-sitter sees both function_item nodes regardless
+ * of which cfg is active (it does not preprocess). The correct fix must
+ * emit two DISTINCT graph nodes — one per branch — so that
+ * get_code_snippet can return the right body for the right build.
+ *
+ * The "real" branch (feature = "rag-pdf") has:
+ * - parameter name "bytes" (no underscore)
+ * - a non-trivial body (returns Some(String::new()))
+ * - starts at line 2
+ *
+ * The "stub" branch (not(feature = "rag-pdf")) has:
+ * - parameter name "_bytes" (underscore = unused)
+ * - a trivial body (returns None)
+ * - starts at line 7
+ */
+TEST(repro_issue495_cfg_gated_twins_distinct) {
+ static const char *src =
+ "#[cfg(feature = \"rag-pdf\")]\n"
+ "fn try_extract_pdf_text(bytes: &[u8]) -> Option<String> {\n"
+ " if bytes.is_empty() { return None; }\n"
+ " Some(String::new())\n"
+ "}\n"
+ "\n"
+ "#[cfg(not(feature = \"rag-pdf\"))]\n"
+ "fn try_extract_pdf_text(_bytes: &[u8]) -> Option<String> { None }\n";
+
+ CBMFileResult *r = rx(src, "t", "src.rs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* ── Part 1: both defs must be present in the extraction output ── */
+
+ int twin_count = count_defs_named(r, "Function", "try_extract_pdf_text");
+
+ /* Both function_item nodes are in the tree-sitter parse; both must
+ * be emitted. This should already pass on current code (extraction
+ * visits both nodes) and acts as a precondition for Parts 2 & 3. */
+ ASSERT_GTE(twin_count, 2);
+
+ /* ── Part 2 (PRIMARY RED): distinct qualified_names per twin ───── */
+
+ /* Retrieve the two defs. On buggy code both have the same QN, so
+ * even picking them by index 0 and 1 is meaningful: the pair MUST
+ * carry two DIFFERENT qualified_name strings. */
+ CBMDefinition *d0 = nth_def_named(r, "Function", "try_extract_pdf_text", 0);
+ CBMDefinition *d1 = nth_def_named(r, "Function", "try_extract_pdf_text", 1);
+ ASSERT_NOT_NULL(d0);
+ ASSERT_NOT_NULL(d1);
+ ASSERT_NOT_NULL(d0->qualified_name);
+ ASSERT_NOT_NULL(d1->qualified_name);
+
+ /* ROOT CAUSE ASSERTION: the two cfg-gated twins must have DISTINCT
+ * qualified_names so the graph upsert can store them as separate
+ * nodes. On current (buggy) code both equal "t.src.try_extract_pdf_text"
+ * and this assertion FAILS → RED. */
+ ASSERT_STR_NEQ(d0->qualified_name, d1->qualified_name);
+
+ /* ── Part 3 (SECONDARY RED): each def carries its own cfg predicate */
+
+ /* The decorator text for each function_item is already captured by
+ * extract_decorators() into def.decorators[0]. The fix can use this
+ * captured text to build the disambiguating QN suffix. We verify
+ * that the right predicate lives on the right def:
+ *
+ * - the def whose signature contains "bytes" (no underscore, real
+ * body) must have a decorator containing "feature" but NOT "not("
+ * - the def whose signature contains "_bytes" (stub) must have a
+ * decorator containing "not("
+ *
+ * On buggy code: d0 and d1 have identical QN so we cannot distinguish
+ * which is the real and which is the stub — the pair-identity check
+ * in Part 2 already failed. Parts 2 and 3 together pin the root
+ * cause at extract_func_def() failing to fold the cfg predicate into
+ * the qualified_name. */
+ CBMDefinition *real_def = NULL; /* #[cfg(feature = "rag-pdf")] */
+ CBMDefinition *stub_def = NULL; /* #[cfg(not(feature = "rag-pdf"))] */
+
+ for (int i = 0; i < r->defs.count; i++) {
+ CBMDefinition *d = &r->defs.items[i];
+ if (!d->name || strcmp(d->name, "try_extract_pdf_text") != 0)
+ continue;
+ if (!d->qualified_name)
+ continue;
+ /* Identify by the cfg predicate baked into the (fixed) QN.
+ * On unpatched code both QNs are identical so neither branch
+ * is reachable via a unique QN → real_def / stub_def stay NULL
+ * → the ASSERT_NOT_NULLs below fire as a second RED signal. */
+ if (strstr(d->qualified_name, "not(") != NULL) {
+ stub_def = d;
+ } else {
+ real_def = d;
+ }
+ }
+
+ /* On fixed code: two distinct QNs → both pointers set. */
+ ASSERT_NOT_NULL(real_def); /* RED on current code */
+ ASSERT_NOT_NULL(stub_def); /* RED on current code */
+
+ /* Decorator text must survive and identify each branch. */
+ ASSERT_NOT_NULL(real_def->decorators);
+ ASSERT_NOT_NULL(real_def->decorators[0]);
+ ASSERT_TRUE(strstr(real_def->decorators[0], "cfg") != NULL);
+ ASSERT_TRUE(strstr(real_def->decorators[0], "not(") == NULL);
+
+ ASSERT_NOT_NULL(stub_def->decorators);
+ ASSERT_NOT_NULL(stub_def->decorators[0]);
+ ASSERT_TRUE(strstr(stub_def->decorators[0], "not(") != NULL);
+
+ /* Line ranges must not overlap (both trees are in-source). */
+ ASSERT_TRUE(real_def->start_line != stub_def->start_line);
+ ASSERT_TRUE(real_def->end_line < stub_def->start_line ||
+ stub_def->end_line < real_def->start_line);
+
+ cbm_free_result(r);
+ PASS();
+}
+
+/* ── Suite ────────────────────────────────────────────────────────── */
+SUITE(repro_issue495) {
+ RUN_TEST(repro_issue495_cfg_gated_twins_distinct);
+}
diff --git a/tests/repro/repro_issue521.c b/tests/repro/repro_issue521.c
new file mode 100644
index 000000000..7701dcd6c
--- /dev/null
+++ b/tests/repro/repro_issue521.c
@@ -0,0 +1,216 @@
+/*
+ * repro_issue521.c — Reproduce-first case for issue #521.
+ *
+ * BUG: "Route nodes created from URL strings in config / non-source files"
+ *
+ * Root cause (pipeline.c:try_upsert_infra_route + helpers.c:is_url_like):
+ *
+ * 1. extract_unified.c:handle_string_refs() walks every string node in a
+ * YAML file. Any value containing "://" passes cbm_classify_string()
+ * as CBM_STRREF_URL, landing in CBMFileResult.string_refs.
+ *
+ * 2. pipeline.c:cbm_pipeline_extract_infra_routes() iterates files that
+ * match is_infra_file() — which includes ".yaml" / ".yml" — and calls
+ * try_upsert_infra_route() for every CBM_STRREF_URL entry whose value
+ * contains "://".
+ *
+ * 3. try_upsert_infra_route() unconditionally mints a "Route" node:
+ * cbm_gbuf_upsert_node(gbuf, "Route", sr->value, route_qn, ...)
+ * with no check for whether the URL is an upstream-config value (e.g.
+ * an auth-server JWKS URL, a Terraform registry URL, a healthcheck
+ * target) versus an actual route this service exposes.
+ *
+ * Correct behaviour: a YAML/config file that only contains upstream URL
+ * strings (no route-registration syntax, no handler definitions) MUST NOT
+ * yield any Route node in the graph.
+ *
+ * Why RED on current code: try_upsert_infra_route has no guard that
+ * prevents minting Route nodes from arbitrary CBM_STRREF_URL values in
+ * config files. Indexing the fixture below produces ≥ 2 Route nodes
+ * (one per upstream URL string), so ASSERT_EQ(route_count, 0) FAILS.
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+#include "test_helpers.h"
+#include "cbm.h"
+#include <mcp/mcp.h>
+#include <store/store.h>
+#include <pipeline/pipeline.h>
+#include <foundation/log.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+/* ── Minimal pipeline harness (mirrors test_grammar_probe_b.c) ───────────── */
+
+typedef struct {
+ char tmpdir[256];
+ char dbpath[512];
+ char *project;
+ cbm_mcp_server_t *srv;
+} R521Proj;
+
+static void r521_fwd_slashes(char *p) {
+ for (; *p; p++) {
+ if (*p == '\\') *p = '/';
+ }
+}
+
+typedef struct {
+ const char *name;
+ const char *content;
+} R521File;
+
+static cbm_store_t *r521_index_files(R521Proj *lp, const R521File *files, int nfiles) {
+ memset(lp, 0, sizeof(*lp));
+ snprintf(lp->tmpdir, sizeof(lp->tmpdir), "/tmp/cbm_r521_XXXXXX");
+ if (!cbm_mkdtemp(lp->tmpdir)) return NULL;
+ r521_fwd_slashes(lp->tmpdir);
+
+ for (int i = 0; i < nfiles; i++) {
+ char path[700];
+ snprintf(path, sizeof(path), "%s/%s", lp->tmpdir, files[i].name);
+ /* create any intermediate directories */
+ char *slash = strrchr(path, '/');
+ if (slash && slash > path + (int)strlen(lp->tmpdir)) {
+ *slash = '\0';
+ cbm_mkdir_p(path, 0755);
+ *slash = '/';
+ }
+ FILE *f = fopen(path, "wb");
+ if (!f) return NULL;
+ fputs(files[i].content, f);
+ fclose(f);
+ }
+
+ lp->project = cbm_project_name_from_path(lp->tmpdir);
+ if (!lp->project) return NULL;
+
+ const char *home = getenv("HOME");
+ if (!home) home = "/tmp";
+ char cache_dir[512];
+ snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
+ cbm_mkdir(cache_dir);
+ snprintf(lp->dbpath, sizeof(lp->dbpath), "%s/%s.db", cache_dir, lp->project);
+ unlink(lp->dbpath);
+
+ lp->srv = cbm_mcp_server_new(NULL);
+ if (!lp->srv) return NULL;
+
+ char args[700];
+ snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", lp->tmpdir);
+ char *resp = cbm_mcp_handle_tool(lp->srv, "index_repository", args);
+ if (resp) free(resp);
+
+ return cbm_store_open_path(lp->dbpath);
+}
+
+static void r521_cleanup(R521Proj *lp, cbm_store_t *store) {
+ if (store) cbm_store_close(store);
+ if (lp->srv) { cbm_mcp_server_free(lp->srv); lp->srv = NULL; }
+ free(lp->project); lp->project = NULL;
+ th_rmtree(lp->tmpdir);
+ unlink(lp->dbpath);
+ char wal[600], shm[600];
+ snprintf(wal, sizeof(wal), "%s-wal", lp->dbpath);
+ snprintf(shm, sizeof(shm), "%s-shm", lp->dbpath);
+ unlink(wal); unlink(shm);
+}
+
+/* Count Route nodes in the indexed project. Returns -1 on error. */
+static int r521_count_routes(cbm_store_t *store, const char *project) {
+ cbm_node_t *nodes = NULL;
+ int count = 0;
+ if (cbm_store_find_nodes_by_label(store, project, "Route", &nodes, &count) != CBM_STORE_OK)
+ return -1;
+ cbm_store_free_nodes(nodes, count);
+ return count;
+}
+
+/* ── Reproduction test ───────────────────────────────────────────────────── */
+
+/*
+ * Fixture: a three-file repo containing ONLY config files.
+ *
+ * config.yaml — application config; values are upstream/external URLs
+ * (auth server, downstream service). No handler code.
+ * dependabot.yml — Dependabot config; "registries" block holds a Terraform
+ * registry URL. Purely a CI config — no route handlers.
+ * compose.yaml — Docker Compose; "healthcheck" contains a curl command
+ * with a localhost URL. No route-serving code.
+ *
+ * All three files match is_infra_file() (.yaml / .yml). Their URL strings
+ * pass cbm_classify_string() as CBM_STRREF_URL. On buggy code,
+ * try_upsert_infra_route() mints a Route node for each URL string that
+ * contains "://", so the graph gets ≥ 2 spurious Route nodes.
+ *
+ * Correct behaviour: 0 Route nodes (no route handler exists anywhere).
+ * Actual (buggy): ≥ 2 Route nodes — assertion below is RED.
+ */
+TEST(repro_issue521_no_route_from_config_url) {
+ static const R521File files[] = {
+ {
+ "config.yaml",
+ "auth:\n"
+ " jwks_url: \"https://auth.example.com/.well-known/jwks.json\"\n"
+ "upstream:\n"
+ " order_service_url: \"http://order-service:8080/v2/orders/{id}\"\n"
+ },
+ {
+ "dependabot.yml",
+ "version: 2\n"
+ "registries:\n"
+ " terraform-registry:\n"
+ " type: terraform-registry\n"
+ " url: https://app.terraform.io\n"
+ "updates:\n"
+ " - package-ecosystem: terraform\n"
+ " directory: \"/\"\n"
+ " schedule:\n"
+ " interval: weekly\n"
+ },
+ {
+ "compose.yaml",
+ "services:\n"
+ " app:\n"
+ " image: myapp:latest\n"
+ " healthcheck:\n"
+ " test: [\"CMD-SHELL\", \"curl --fail http://localhost:9000/ || exit 1\"]\n"
+ " interval: 30s\n"
+ },
+ };
+
+ R521Proj lp;
+ cbm_store_t *store = r521_index_files(&lp, files, 3);
+ ASSERT_NOT_NULL(store);
+
+ int route_count = r521_count_routes(store, lp.project);
+
+ /*
+ * CORRECT behaviour: no Route node must exist.
+ * Upstream/config/healthcheck URLs are not routes this service serves.
+ *
+ * WHY RED on current code:
+ * pipeline.c:try_upsert_infra_route() calls cbm_gbuf_upsert_node(…,"Route",…)
+ * for every CBM_STRREF_URL string_ref extracted from files matching
+ * is_infra_file() — which includes all three YAML files above.
+ * The function has no guard to reject upstream/config URL values, so
+ * it mints Route nodes for "https://auth.example.com/…", "https://app.terraform.io",
+ * "http://order-service:8080/…", and "http://localhost:9000/" — at
+ * least 2 spurious Route nodes, so route_count > 0, and this ASSERT_EQ
+ * FAILS (RED).
+ */
+ ASSERT_EQ(route_count, 0);
+
+ r521_cleanup(&lp, store);
+ PASS();
+}
+
+/* ── Suite ───────────────────────────────────────────────────────────────── */
+SUITE(repro_issue521) {
+ RUN_TEST(repro_issue521_no_route_from_config_url);
+}
diff --git a/tests/repro/repro_main.c b/tests/repro/repro_main.c
index 3d3c90c81..a559ca3f1 100644
--- a/tests/repro/repro_main.c
+++ b/tests/repro/repro_main.c
@@ -27,8 +27,11 @@ int tf_skip_count = 0;
#include "test_framework.h"
-/* ── Repro suites (one per bug cluster) ─────────────────────────── */
+/* ── Repro suites (one per bug cluster / issue) ─────────────────── */
extern void suite_repro_extraction(void);
+extern void suite_repro_issue495(void);
+extern void suite_repro_issue521(void);
+extern void suite_repro_issue382(void);
int main(void) {
printf("\n");
@@ -40,6 +43,9 @@ int main(void) {
printf("════════════════════════════════════════════════════════════\n");
RUN_SUITE(repro_extraction);
+ RUN_SUITE(repro_issue495);
+ RUN_SUITE(repro_issue521);
+ RUN_SUITE(repro_issue382);
TEST_SUMMARY();
}