Skip to content

Commit aedb979

Browse files
authored
Merge pull request DeusData#673 from DeusData/qa/bug-repro-suite
qa: Python-builtin + kotlin.Any LSP fixes; false-guard audit (honest DeusData#581 known-red)
2 parents 63ed678 + 10ab207 commit aedb979

13 files changed

Lines changed: 255 additions & 91 deletions

‎internal/cbm/extract_calls.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,11 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
18731873

18741874
if (cbm_kind_in_set(node, spec->call_node_types)) {
18751875
char *callee = extract_callee_name(ctx->arena, node, ctx->source, ctx->language);
1876-
if (callee && callee[0] && !cbm_is_keyword(callee, ctx->language)) {
1876+
// Keyword-filter callees, but keep builtins we mint a node for (len, str,
1877+
// ...) so the LSP-resolved builtin call still forms a CALLS edge.
1878+
if (callee && callee[0] &&
1879+
(!cbm_is_keyword(callee, ctx->language) ||
1880+
cbm_is_resolvable_builtin(callee, ctx->language))) {
18771881
CBMCall call = {0};
18781882
call.callee_name = callee;
18791883
call.enclosing_func_qn = state->enclosing_func_qn;

‎internal/cbm/helpers.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ bool cbm_is_keyword(const char *name, CBMLanguage lang) {
210210
return false;
211211
}
212212

213+
// Builtins that appear in the keyword set above (so they are suppressed as bare
214+
// usages) but for which we mint a real graph node and an LSP resolution, so a
215+
// CALL to them must still be extracted. MUST stay in sync with kPyBuiltinNodes
216+
// in internal/cbm/lsp/py_builtins.c — every entry here has a "builtins.<name>"
217+
// node, so the resulting CALLS edge always has a target (never Module-sourced).
218+
static const char *python_resolvable_builtins[] = {"len", "print", "str", "int",
219+
"list", "dict", "range", NULL};
220+
221+
bool cbm_is_resolvable_builtin(const char *name, CBMLanguage lang) {
222+
if (!name || !name[0]) {
223+
return false;
224+
}
225+
if (lang != CBM_LANG_PYTHON) {
226+
return false;
227+
}
228+
for (const char **b = python_resolvable_builtins; *b; b++) {
229+
if (strcmp(name, *b) == 0) {
230+
return true;
231+
}
232+
}
233+
return false;
234+
}
235+
213236
// --- Export detection ---
214237

215238
bool cbm_is_exported(const char *name, CBMLanguage lang) {

‎internal/cbm/helpers.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ char *cbm_node_text(CBMArena *a, TSNode node, const char *source);
1515
// Check if a string is a language keyword (should be skipped as callee/usage).
1616
bool cbm_is_keyword(const char *name, CBMLanguage lang);
1717

18+
// Check if a name is a builtin we mint a real graph node for, so a CALL to it
19+
// must NOT be keyword-filtered out of call extraction (the LSP resolves it to
20+
// the injected builtin node and forms a CALLS edge). Narrower than
21+
// cbm_is_keyword: it only covers builtins with a target node, so un-filtering
22+
// them cannot produce a node-less / Module-sourced edge. The Python set MUST
23+
// stay in sync with kPyBuiltinNodes in internal/cbm/lsp/py_builtins.c.
24+
bool cbm_is_resolvable_builtin(const char *name, CBMLanguage lang);
25+
1826
// Classify a string literal as URL, config, or neither.
1927
// Returns CBM_STRREF_URL (0), CBM_STRREF_CONFIG (1), or -1 for neither.
2028
int cbm_classify_string(const char *str, int len);

‎internal/cbm/lsp/kotlin_builtins.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* kotlin_builtins.c — Minimal Kotlin universal builtins as real graph nodes.
3+
*
4+
* When a method call lands on an unknown-typed receiver and the member is one
5+
* of the universal kotlin.Any methods (toString / equals / hashCode), the
6+
* Kotlin LSP resolves it to "kotlin.Any.<member>" and emits the lsp_kt_any
7+
* strategy (kotlin_lsp.c, kt_emit_resolved). Any is the supertype of every
8+
* Kotlin reference, so this is the same target the fwcd LSP resolves to.
9+
*
10+
* The missing piece is downstream: pass_calls.c only writes a CALLS edge when
11+
* cbm_pipeline_lsp_target_node() resolves the callee_qn to a graph node
12+
* (src/pipeline/lsp_resolve.h). There is no "kotlin.Any" node in the graph, so
13+
* the resolved call is dropped and the strategy never lands on an edge
14+
* (callable=0).
15+
*
16+
* Fix: inject a small, fixed set of kotlin.Any definitions into result->defs
17+
* during the per-file Kotlin LSP run (cbm_run_kotlin_lsp, which executes inside
18+
* cbm_extract_file, BEFORE the parallel pipeline mints def nodes from
19+
* result->defs). The graph therefore gains real "kotlin.Any[.<method>]" nodes
20+
* that the lsp_kt_any edges target. The QNs here MUST match what kt_emit_resolved
21+
* emits as callee_qn ("kotlin.Any.<member>").
22+
*
23+
* Node minting upserts by QN (cbm_gbuf_upsert_node), so injecting the same
24+
* builtins per Kotlin file collapses to one node per QN — no duplicates.
25+
*
26+
* Self-contained: #included from kotlin_lsp.c only (amalgamation pattern; see
27+
* lsp_all.c). Not a standalone translation unit. Mirror of py_builtins.c.
28+
*/
29+
30+
/* A single builtin entry to mint as a graph node. */
31+
typedef struct {
32+
const char *qn; /* graph QN — MUST equal the kt_emit_resolved callee_qn */
33+
const char *name; /* short name (last segment of qn) */
34+
const char *label; /* "Class" | "Method" */
35+
} KtBuiltinNode;
36+
37+
/*
38+
* Universal kotlin.Any members the LSP falls back to (kt_any_methods in
39+
* kotlin_lsp.c). The Any class node anchors the three methods.
40+
*/
41+
static const KtBuiltinNode kKtBuiltinNodes[] = {
42+
{"kotlin.Any", "Any", "Class"},
43+
{"kotlin.Any.toString", "toString", "Method"},
44+
{"kotlin.Any.equals", "equals", "Method"},
45+
{"kotlin.Any.hashCode", "hashCode", "Method"},
46+
};
47+
48+
/*
49+
* Inject the builtin definitions into result->defs so the pipeline mints them
50+
* as graph nodes. All fields beyond name/qn/label are left zero/NULL: builtins
51+
* have no body, so complexity/line-range/etc. are irrelevant, and a synthetic
52+
* file_path keeps them out of any real source file's def list.
53+
*/
54+
static void kt_builtins_inject_defs(CBMFileResult *result, CBMArena *arena) {
55+
if (!result || !arena) {
56+
return;
57+
}
58+
const int n = (int)(sizeof(kKtBuiltinNodes) / sizeof(kKtBuiltinNodes[0]));
59+
for (int i = 0; i < n; i++) {
60+
const KtBuiltinNode *b = &kKtBuiltinNodes[i];
61+
CBMDefinition def;
62+
memset(&def, 0, sizeof(def));
63+
def.name = b->name;
64+
def.qualified_name = b->qn;
65+
def.label = b->label;
66+
def.file_path = "<kotlin-builtins>";
67+
def.start_line = 1;
68+
def.end_line = 1;
69+
cbm_defs_push(&result->defs, arena, def);
70+
}
71+
}

‎internal/cbm/lsp/kotlin_lsp.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#include <stdlib.h>
4343
#include <string.h>
4444

45+
/* Minimal Kotlin universal builtins as real graph nodes (kt_builtins_inject_defs).
46+
* Amalgamation-included (see lsp_all.c); mirror of py_builtins.c. */
47+
#include "kotlin_builtins.c"
48+
4549
#define KT_EVAL_MAX_DEPTH 32
4650
#define KT_IMPORT_INITIAL_CAP 16
4751

@@ -4117,6 +4121,10 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
41174121

41184122
kotlin_lsp_process_file(&ctx, use_root);
41194123

4124+
/* Inject kotlin.Any universal-method nodes (toString/equals/hashCode) so the
4125+
* lsp_kt_any fallback emitted above has a target node to form a CALLS edge. */
4126+
kt_builtins_inject_defs(result, arena);
4127+
41204128
if (patched_tree) {
41214129
ts_tree_delete(patched_tree);
41224130
}

‎tests/repro/repro_issue431.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,17 @@ TEST(repro_issue431_vscode_profile_inherits_mcp_json) {
109109
/* --- Precondition: VSCode is detected --- */
110110
cbm_detected_agents_t agents = cbm_detect_agents(tmpdir);
111111
if (!agents.vscode) {
112-
/* Detection failed in the temp tree — adjust path derivation.
113-
* On non-Apple Linux the detection reads cbm_app_config_dir() which
114-
* is process-global (not home-relative), so detection may return false
115-
* for a synthetic tmpdir home. The bug still exists, but we cannot
116-
* demonstrate it via the plan-based oracle without detection firing.
117-
* Mark the test as an expected skip on this platform/config. */
112+
/* #431 IS FIXED: install_vscode_profile_configs() (cli.c:3211) scans
113+
* Code/User/profiles/ and plans a per-profile mcp.json, so the assertion
114+
* below passes as a genuine regression guard whenever detection fires
115+
* (which it does for this fixture). This branch is only reached if
116+
* cbm_detect_agents() cannot see the fixture home on some platform — in
117+
* which case the fix cannot be VERIFIED here. Skip honestly rather than
118+
* vacuously PASS (would hide a future regression) or FAIL (would red a
119+
* fixed bug). */
118120
th_rmtree(tmpdir);
119-
PASS(); /* precondition unmet — non-blocking; bug still open */
121+
SKIP_PLATFORM("VSCode detection did not fire for the synthetic fixture "
122+
"home; cannot verify the #431 per-profile install here");
120123
}
121124

122125
/* --- Run the install plan oracle (dry-run, no mutations) --- */

‎tests/repro/repro_issue581.c‎

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -245,46 +245,41 @@ TEST(repro_issue581_query_rss_stable) {
245245
free(args);
246246
rh_cleanup(&lp, store);
247247

248-
// If RSS is not measurable (cbm_mem_rss() returns 0 and no Linux fallback),
249-
// skip the growth assertion -- an unmeasurable RSS cannot produce a
250-
// meaningful signal. This avoids a false PASS masking a real leak on
251-
// platforms where our RSS API is unavailable.
252-
if (rss_warmup == 0 || rss_end == 0) {
253-
printf(" NOTE: RSS not measurable on this platform/build; "
254-
"growth assertion skipped (inconclusive, not a pass)\n");
255-
PASS();
248+
if (rss_warmup > 0 && rss_end > 0) {
249+
printf(" rss_warmup_kb=%zu rss_end_kb=%zu factor=%.2f threshold=%.1f\n", rss_warmup / 1024,
250+
rss_end / 1024, (double)rss_end / (double)rss_warmup, LEAK_FACTOR);
251+
} else {
252+
printf(" NOTE: RSS not measurable on this platform/build\n");
256253
}
257254

258-
printf(" rss_warmup_kb=%zu rss_end_kb=%zu factor=%.2f threshold=%.1f\n",
259-
rss_warmup / 1024, rss_end / 1024,
260-
(double)rss_end / (double)rss_warmup,
261-
LEAK_FACTOR);
262-
263-
// PRIMARY assertion: end RSS must not exceed LEAK_FACTOR x warmup RSS.
255+
// HONEST RED — this guard is currently VACUOUS and #581 is OPEN.
264256
//
265-
// RED condition (current code):
266-
// SQLite WAL + mimalloc retained pages grow each iteration.
267-
// Over 150 iterations the cumulative growth pushes rss_end above
268-
// LEAK_FACTOR * rss_warmup.
269-
// ASSERT fires -> RED.
257+
// This fixture CANNOT reproduce the leak: a 3-node graph over 150
258+
// search_graph calls allocates far too little to move process RSS (observed
259+
// factor=1.00), so the old "rss_end <= 3.0 x rss_warmup" assertion passed
260+
// even on the leaking build. A green here would mean "leak fixed" while the
261+
// leak is unfixed — a false guard that violates the tests-are-guards rule
262+
// (green <=> fixed). So it stays RED.
270263
//
271-
// GREEN condition (after fix):
272-
// Periodic compaction (cbm_mem_collect + WAL TRUNCATE checkpoint) keeps
273-
// rss_end near rss_warmup. factor stays <1.5 comfortably.
264+
// Turning this GREEN legitimately requires BOTH:
265+
// (a) a real reproduction tier — a long-running MCP session issuing
266+
// thousands of ops against a LARGE graph, measuring the SQLite WAL
267+
// file size and mimalloc committed pages DIRECTLY (not process-RSS
268+
// jitter) so the monotonic growth is actually observable; AND
269+
// (b) the fix — periodic SQLITE_CHECKPOINT_TRUNCATE + cbm_mem_collect() in
270+
// the MCP query loop / idle eviction (see the header + #581).
274271
//
275-
// We report the ratio in the failure message so the fixer can see the
276-
// growth slope without needing a profiler.
277-
size_t rss_limit = (size_t)(rss_warmup * LEAK_FACTOR);
278-
if (rss_end > rss_limit) {
279-
printf(" BUG #581 reproduced: RSS grew %.2fx after %d search_graph calls "
280-
"(warmup=%zu kB end=%zu kB limit=%zu kB)\n",
281-
(double)rss_end / (double)rss_warmup,
282-
ITER_TOTAL - ITER_WARMUP,
283-
rss_warmup / 1024, rss_end / 1024, rss_limit / 1024);
284-
}
285-
ASSERT(rss_end <= rss_limit);
286-
287-
PASS();
272+
// Until both land this is an honest "not fixed / not provable here" RED, not
273+
// a false green.
274+
/* TODO(#581): whitelisted known-red on the non-gating bug-repro board. The
275+
* leak is a real OPEN bug; this fixture cannot yet reproduce it, so the test
276+
* stays RED (honest "not fixed") rather than vacuously green. Turning it
277+
* green requires a real WAL-size / mimalloc-committed-pages reproduction tier
278+
* plus the query-path compaction fix (see header). Tracked, not skipped. */
279+
FAIL("TODO(#581) whitelisted known-red: query-path memory leak is OPEN and "
280+
"cannot be reproduced in this fixture (RSS factor ~1.0 even when "
281+
"leaking) — needs a real WAL/committed-pages reproduction tier plus the "
282+
"query-path compaction fix");
288283
}
289284

290285
// -- Suite ------------------------------------------------------------------

‎tests/repro/repro_lsp_c_cpp.c‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ static int assert_no_resolvable_edge(const char *filename, const char *src,
156156
return 1;
157157
}
158158
int rc = 0;
159+
/* Exercised-check: the fixture MUST produce at least one callable-sourced
160+
* CALLS edge (its in-fixture control call). Without it the "no edge to
161+
* <callee>" invariant is VACUOUS — it also passes when extraction silently
162+
* produced nothing, so a green would not prove the unresolvable call was
163+
* actually processed and correctly dropped. */
164+
int module_sourced = -1;
165+
int callable_sourced = -1;
166+
inv_count_calls_by_source(store, lp.project, &module_sourced, &callable_sourced);
167+
(void)module_sourced;
168+
if (callable_sourced <= 0) {
169+
printf(" %sFAIL%s %s:%d: no callable-sourced CALLS edge — fixture not "
170+
"exercised; the no-edge invariant for %s is vacuous\n",
171+
tf_red(), tf_reset(), __FILE__, __LINE__, callee_substr);
172+
rc = 1;
173+
}
159174
if (!inv_no_calls_edge_to_qn(store, lp.project, callee_substr)) {
160175
printf(" %sFAIL%s %s:%d: a CALLS edge unexpectedly targets %s "
161176
"(expected NONE — callee is unresolvable)\n",
@@ -302,12 +317,12 @@ static const char kFuncPtr[] =
302317
* (RED) — it documents that the DLL-resolution path needs an external binding
303318
* the single-file harness can't synthesize. The fixture below at least exercises
304319
* a pointer assigned from an extern declaration. */
305-
static const char kDllResolve[] =
306-
"extern int plugin_entry(int x);\n"
307-
"int caller(int v) {\n"
308-
" int (*fp)(int) = plugin_entry;\n"
309-
" return fp(v);\n"
310-
"}\n";
320+
static const char kDllResolve[] = "extern int plugin_entry(int x);\n"
321+
"int known(int x) { return x + 1; }\n"
322+
"int caller(int v) {\n"
323+
" int (*fp)(int) = plugin_entry;\n"
324+
" return known(v) + fp(v);\n"
325+
"}\n";
311326

312327
/* lsp_operator — overloaded binary operator+ on a custom type (c_lsp.c:3771-3789:
313328
* binary_expression, lhs is a custom type, operator+ member found). */
@@ -387,10 +402,10 @@ static const char kAdl[] =
387402
* called with a NULL callee_qn; the more common unresolved path is
388403
* c_emit_unresolved_call (a different marker). This fixture exercises a call to
389404
* an undeclared function and documents whether "lsp_unresolved" surfaces. */
390-
static const char kUnresolved[] =
391-
"int caller(int v) {\n"
392-
" return totally_unknown_fn(v);\n"
393-
"}\n";
405+
static const char kUnresolved[] = "int known(int x) { return x + 1; }\n"
406+
"int caller(int v) {\n"
407+
" return known(v) + totally_unknown_fn(v);\n"
408+
"}\n";
394409

395410
/* ── Per-strategy tests ──────────────────────────────────────────────────── */
396411

‎tests/repro/repro_lsp_go_py.c‎

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ static int assert_no_resolvable_edge_files(const RFile *files, int nfiles,
201201
return 1;
202202
}
203203
int rc = 0;
204+
/* Exercised-check: the fixture MUST produce at least one callable-sourced
205+
* CALLS edge (its in-fixture control call). Without it the "no edge to
206+
* <callee>" invariant is VACUOUS — it also passes when extraction silently
207+
* produced nothing, so a green would not prove the unresolvable call was
208+
* actually processed and correctly dropped. */
209+
int module_sourced = -1;
210+
int callable_sourced = -1;
211+
inv_count_calls_by_source(store, lp.project, &module_sourced, &callable_sourced);
212+
(void)module_sourced;
213+
if (callable_sourced <= 0) {
214+
printf(" %sFAIL%s %s:%d: no callable-sourced CALLS edge — fixture not "
215+
"exercised; the no-edge invariant for %s is vacuous\n",
216+
tf_red(), tf_reset(), __FILE__, __LINE__, callee_substr);
217+
rc = 1;
218+
}
204219
if (!inv_no_calls_edge_to_qn(store, lp.project, callee_substr)) {
205220
printf(" %sFAIL%s %s:%d: a CALLS edge unexpectedly targets %s "
206221
"(expected NONE — callee is unresolvable)\n",
@@ -309,11 +324,11 @@ static const RFile kGoCrossFile[] = {
309324
* "lsp_unresolved"). NOTE: emit_unresolved_call uses confidence 0.0, so the
310325
* pipeline may not promote it into a CALLS edge with the strategy tag — this
311326
* fixture documents whether "lsp_unresolved" surfaces in the graph. */
312-
static const char kGoUnresolved[] =
313-
"package main\n"
314-
"func caller(v int) int {\n"
315-
" return totallyUnknownFn(v)\n"
316-
"}\n";
327+
static const char kGoUnresolved[] = "package main\n"
328+
"func known(x int) int { return x + 1 }\n"
329+
"func caller(v int) int {\n"
330+
" return known(v) + totallyUnknownFn(v)\n"
331+
"}\n";
317332

318333
/* ── Python fixtures ───────────────────────────────────────────────────────── */
319334

@@ -386,13 +401,13 @@ static const RFile kPyModuleAttr[] = {
386401
* symbol lookup misses → best-effort "module.attr" QN, low confidence). helpers
387402
* defines nothing named missing_fn. */
388403
static const RFile kPyModuleAttrUnresolved[] = {
389-
{"helpers.py",
390-
"def do_work(x):\n"
391-
" return x + 9\n"},
392-
{"main.py",
393-
"import helpers\n"
394-
"def caller(v):\n"
395-
" return helpers.missing_fn(v)\n"},
404+
{"helpers.py", "def do_work(x):\n"
405+
" return x + 9\n"},
406+
{"main.py", "import helpers\n"
407+
"def known(x):\n"
408+
" return x + 1\n"
409+
"def caller(v):\n"
410+
" return known(v) + helpers.missing_fn(v)\n"},
396411
};
397412

398413
/* lsp_dict_dispatch — funcs["key"]() where funcs is a dict-literal dispatch
@@ -571,23 +586,14 @@ TEST(repro_lsp_py_operator_dunder) {
571586
}
572587

573588
TEST(repro_lsp_py_builtin) {
574-
/* PARKED for release: lsp_builtin (len(v)) needs a typeshed/builtins registry
575-
* so builtin functions have target nodes; without it the resolution has no
576-
* node to form a CALLS edge to (callable=0). Tracked for a future builtins
577-
* registry. */
578-
printf(" %sSKIP%s parked: needs builtins/typeshed registry (len has no node)\n", tf_dim(),
579-
tf_reset());
580-
return -1; /* skip — not counted as pass or fail */
589+
/* len(v) resolves to the injected builtins.len node (py_builtins.c) and
590+
* emits lsp_builtin with a real CALLS edge. */
581591
return assert_lsp_strategy("main.py", kPyBuiltin, "lsp_builtin");
582592
}
583593

584594
TEST(repro_lsp_py_builtin_constructor) {
585-
/* PARKED for release: lsp_builtin_constructor (str(v)) needs a builtins/
586-
* typeshed registry so the builtin type str has a node to target. Tracked
587-
* for a future builtins registry. */
588-
printf(" %sSKIP%s parked: needs builtins/typeshed registry (str type has no node)\n", tf_dim(),
589-
tf_reset());
590-
return -1; /* skip — not counted as pass or fail */
595+
/* str(v) resolves to the injected builtins.str type node (py_builtins.c)
596+
* and emits lsp_builtin_constructor with a real CALLS edge. */
591597
return assert_lsp_strategy("main.py", kPyBuiltinConstructor,
592598
"lsp_builtin_constructor");
593599
}

0 commit comments

Comments
 (0)