Skip to content

Commit bf4c476

Browse files
authored
Merge pull request DeusData#2227 from DeusData/fix/graph-quality-2026-09-16
fix(graph): quality round after the v0.11.0 probe — deterministic resolution, findability, coverage, names
2 parents d615088 + df83d25 commit bf4c476

25 files changed

Lines changed: 1642 additions & 142 deletions

‎internal/cbm/cbm.c‎

Lines changed: 192 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,79 @@ void cbm_reset_profile(void) {
125125
} \
126126
} while (0)
127127

128+
/* A definition name never spans lines. Config extractors hand over a node's
129+
* whole text — a YAML block key, a Makefile recipe (`endif\n\n$(obj)/x.h`), a
130+
* ktest .conf directive — and the line break rode into the graph (2026-09-16
131+
* probe: 8,301 Field nodes on elastic/elasticsearch, 91 kernel nodes). Cut at
132+
* the first line break and drop trailing blanks; the qualified name carries
133+
* the same text as its last segment and is cut the same way. */
134+
static const char *cbm_first_line(CBMArena *a, const char *text) {
135+
if (!text) {
136+
return text;
137+
}
138+
const char *nl = strpbrk(text, "\r\n");
139+
if (!nl) {
140+
return text;
141+
}
142+
size_t n = (size_t)(nl - text);
143+
while (n > 0 && (text[n - 1] == ' ' || text[n - 1] == '\t')) {
144+
n--;
145+
}
146+
char *cut = (char *)cbm_arena_alloc(a, n + 1);
147+
if (!cut) {
148+
return text;
149+
}
150+
memcpy(cut, text, n);
151+
cut[n] = '\0';
152+
return cut;
153+
}
154+
155+
/* JS/TS files: a definition named by a literal token is the walker naming a
156+
* function or variable from an object key or a numeric pattern element —
157+
* `{}` (1,534 incoming CALLS on microsoft/TypeScript's test baselines), `1`
158+
* (38 nodes), a quoted string. An identifier starts with a letter, `_`, `$`,
159+
* `#` (private members) or a non-ASCII byte; anything else is not a name. */
160+
static bool cbm_js_family_path(const char *path) {
161+
if (!path) {
162+
return false;
163+
}
164+
const char *dot = strrchr(path, '.');
165+
if (!dot) {
166+
return false;
167+
}
168+
static const char *const exts[] = {".js", ".mjs", ".cjs", ".jsx", ".ts",
169+
".mts", ".cts", ".tsx", ".ets", NULL};
170+
for (int i = 0; exts[i]; i++) {
171+
if (strcmp(dot, exts[i]) == 0) {
172+
return true;
173+
}
174+
}
175+
return false;
176+
}
177+
178+
static bool cbm_js_name_is_junk(const char *name) {
179+
if (!name || !name[0]) {
180+
return false; /* empty names are handled by the callers' own rules */
181+
}
182+
unsigned char c = (unsigned char)name[0];
183+
bool identifier_start = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' ||
184+
c == '$' || c == '#' || c >= 0x80;
185+
/* Member names JS spells without an identifier start are still names:
186+
* computed keys (`[Symbol.iterator]`), string-literal keys (`"my-key"`,
187+
* `'x'`) and escaped identifiers (`А`). Measured on the TypeScript
188+
* corpus: 1,531 `[…]` members, 97 quoted members, 154 escaped
189+
* identifiers — all real definitions. What remains ({…} patterns, numeric
190+
* literals, parenthesised types, `...rest`) is a token, not a name. */
191+
bool member_key_start = c == '[' || c == '"' || c == '\'' || c == '\\';
192+
return !identifier_start && !member_key_start;
193+
}
194+
128195
void cbm_defs_push(CBMDefArray *arr, CBMArena *a, CBMDefinition def) {
196+
def.name = cbm_first_line(a, def.name);
197+
def.qualified_name = cbm_first_line(a, def.qualified_name);
198+
if (cbm_js_family_path(def.file_path) && cbm_js_name_is_junk(def.name)) {
199+
return;
200+
}
129201
GROW_ARRAY(arr, a);
130202
arr->items[arr->count++] = def;
131203
}
@@ -1358,14 +1430,89 @@ static void cbm_mark_pp_error_rows(TSNode n, uint8_t *rows, uint32_t row_count,
13581430
/* Recovery subtraction (#963): tree-sitter error recovery plus the
13591431
* ERROR-descending def walker often still extract constructs INSIDE a failed
13601432
* region (verified: a function in an #ifdef-split ERROR region and even a
1361-
* `def broken(:` both came back as defs). A region whose every line is
1362-
* covered by definitions that START inside it is definitely recovered — its
1363-
* constructs ARE in the graph — so flagging it would be a false miss.
1364-
* Container defs (Module/Package) are ignored: a file-spanning Module node is
1365-
* not evidence the region's constructs survived. Conservative: partially
1366-
* covered regions stay flagged. */
1367-
static bool cbm_region_is_recovered(uint32_t rs, uint32_t re, const CBMDefArray *defs) {
1368-
enum { MAX_COVER_DEFS = 256 };
1433+
* `def broken(:` both came back as defs). The lines a definition that STARTS
1434+
* inside the region covers are therefore in the graph, and only the lines no
1435+
* definition covers can honestly be called missed. Container defs
1436+
* (Module/Package) are ignored: a file-spanning Module node is not evidence
1437+
* the region's constructs survived.
1438+
*
1439+
* This used to be all-or-nothing — a region stayed flagged whole unless every
1440+
* line was covered. On torvalds/linux (2026-09-16 probe) one ERROR node that
1441+
* swallowed the second half of kernel/sched/core.c (lines 5522–11284) survived
1442+
* because of the comment and macro lines between its 286 extracted functions,
1443+
* and the coverage report told a reader 68 % of the scheduler was unindexed.
1444+
* Now the uncovered gaps are reported instead, and a gap holding only blank,
1445+
* comment or preprocessor lines is not a miss at all. */
1446+
static uint32_t *cbm_line_offsets(const char *src, int src_len, uint32_t *out_lines) {
1447+
uint32_t lines = 1;
1448+
for (int i = 0; i < src_len; i++) {
1449+
if (src[i] == '\n') {
1450+
lines++;
1451+
}
1452+
}
1453+
uint32_t *offs =
1454+
(uint32_t *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)(lines + 1) * sizeof(uint32_t));
1455+
if (!offs) {
1456+
*out_lines = 0;
1457+
return NULL;
1458+
}
1459+
uint32_t n = 0;
1460+
offs[n++] = 0;
1461+
for (int i = 0; i < src_len && n < lines; i++) {
1462+
if (src[i] == '\n') {
1463+
offs[n++] = (uint32_t)i + 1;
1464+
}
1465+
}
1466+
offs[n] = (uint32_t)src_len;
1467+
*out_lines = n;
1468+
return offs;
1469+
}
1470+
1471+
/* A 1-based line that holds nothing a reader would call missed code: blank,
1472+
* a comment line, or a preprocessor directive (the import pass owns #include;
1473+
* #if/#endif carry no construct of their own). */
1474+
static bool cbm_line_is_inert(const char *src, const uint32_t *offs, uint32_t nlines,
1475+
uint32_t line) {
1476+
if (!src || !offs || line == 0 || line > nlines) {
1477+
return true;
1478+
}
1479+
const char *p = src + offs[line - 1];
1480+
const char *end = src + offs[line];
1481+
while (p < end && (*p == ' ' || *p == '\t' || *p == '\r')) {
1482+
p++;
1483+
}
1484+
if (p >= end || *p == '\n') {
1485+
return true;
1486+
}
1487+
if (p[0] == '/' && p + 1 < end && (p[1] == '/' || p[1] == '*')) {
1488+
return true;
1489+
}
1490+
return p[0] == '*' || p[0] == '#';
1491+
}
1492+
1493+
static void cbm_regions_emit_gap(cbm_error_regions_t *out, uint32_t gs, uint32_t ge,
1494+
const char *src, const uint32_t *offs, uint32_t nlines) {
1495+
bool live = false;
1496+
for (uint32_t l = gs; l <= ge && !live; l++) {
1497+
live = !cbm_line_is_inert(src, offs, nlines, l);
1498+
}
1499+
if (!live) {
1500+
return;
1501+
}
1502+
if (out->count < CBM_MAX_ERROR_REGIONS) {
1503+
out->starts[out->count] = gs;
1504+
out->ends[out->count] = ge;
1505+
out->count++;
1506+
} else {
1507+
out->dropped++;
1508+
}
1509+
}
1510+
1511+
/* Replace [rs, re] by the sub-ranges no in-region definition covers. */
1512+
static void cbm_region_uncovered_gaps(uint32_t rs, uint32_t re, const CBMDefArray *defs,
1513+
const char *src, const uint32_t *offs, uint32_t nlines,
1514+
cbm_error_regions_t *out) {
1515+
enum { MAX_COVER_DEFS = 1024 };
13691516
uint32_t starts[MAX_COVER_DEFS];
13701517
uint32_t ends[MAX_COVER_DEFS];
13711518
int n = 0;
@@ -1377,14 +1524,25 @@ static bool cbm_region_is_recovered(uint32_t rs, uint32_t re, const CBMDefArray
13771524
if (d->start_line < rs || d->start_line > re) {
13781525
continue; /* recovery evidence must originate inside the region */
13791526
}
1527+
/* A one-line definition salvaged from inside an error region is the
1528+
* walker's guess at broken text (`def x(:` comes back as a def, a
1529+
* mis-parsed `static _Thread_local int x` as a Variable named `int`,
1530+
* and Python's synthetic builtins all sit on line 1); only a construct
1531+
* whose body spans lines proves it was understood. Conservative: real
1532+
* one-line variables and macros inside a failed region keep their line
1533+
* flagged. */
1534+
if (d->end_line <= d->start_line) {
1535+
continue;
1536+
}
13801537
starts[n] = d->start_line;
1381-
ends[n] = d->end_line < d->start_line ? d->start_line : d->end_line;
1538+
ends[n] = d->end_line;
13821539
n++;
13831540
}
13841541
if (n == 0) {
1385-
return false;
1542+
cbm_regions_emit_gap(out, rs, re, src, offs, nlines);
1543+
return;
13861544
}
1387-
/* Insertion-sort by start, then sweep for gaps in [rs, re]. */
1545+
/* Insertion-sort by start, then sweep and emit the gaps in [rs, re]. */
13881546
for (int i = 1; i < n; i++) {
13891547
uint32_t s = starts[i];
13901548
uint32_t e = ends[i];
@@ -1397,16 +1555,18 @@ static bool cbm_region_is_recovered(uint32_t rs, uint32_t re, const CBMDefArray
13971555
starts[j + 1] = s;
13981556
ends[j + 1] = e;
13991557
}
1400-
uint32_t covered_to = rs - 1;
1558+
uint32_t cursor = rs;
14011559
for (int i = 0; i < n; i++) {
1402-
if (starts[i] > covered_to + 1) {
1403-
return false; /* uncovered gap */
1560+
if (starts[i] > cursor) {
1561+
cbm_regions_emit_gap(out, cursor, starts[i] - 1, src, offs, nlines);
14041562
}
1405-
if (ends[i] > covered_to) {
1406-
covered_to = ends[i];
1563+
if (ends[i] + 1 > cursor) {
1564+
cursor = ends[i] + 1;
14071565
}
14081566
}
1409-
return covered_to >= re;
1567+
if (cursor <= re) {
1568+
cbm_regions_emit_gap(out, cursor, re, src, offs, nlines);
1569+
}
14101570
}
14111571

14121572
/* #961: true when 1-based `line` of `src` contains `name` (used to verify a
@@ -1598,16 +1758,19 @@ static bool cbm_remap_preprocessed_def(CBMDefinition *def, const CBMPreprocessed
15981758
return true;
15991759
}
16001760

1601-
static void cbm_subtract_recovered_regions(cbm_error_regions_t *regs, const CBMDefArray *defs) {
1602-
int kept = 0;
1761+
static void cbm_subtract_recovered_regions(cbm_error_regions_t *regs, const CBMDefArray *defs,
1762+
const char *src, int src_len) {
1763+
if (regs->count <= 0) {
1764+
return;
1765+
}
1766+
uint32_t nlines = 0;
1767+
uint32_t *offs = cbm_line_offsets(src, src_len, &nlines);
1768+
cbm_error_regions_t out = {{0}, {0}, 0, regs->dropped};
16031769
for (int i = 0; i < regs->count; i++) {
1604-
if (!cbm_region_is_recovered(regs->starts[i], regs->ends[i], defs)) {
1605-
regs->starts[kept] = regs->starts[i];
1606-
regs->ends[kept] = regs->ends[i];
1607-
kept++;
1608-
}
1770+
cbm_region_uncovered_gaps(regs->starts[i], regs->ends[i], defs, src, offs, nlines, &out);
16091771
}
1610-
regs->count = kept;
1772+
cbm_free(CBM_MEM_CLASS_EXTRACT, offs);
1773+
*regs = out;
16111774
}
16121775

16131776
/* #1071: a function-like macro invocation whose argument is a type token
@@ -2019,6 +2182,9 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
20192182

20202183
TSTree *tree = ts_parser_parse_with_options(parser, NULL, ts_input, opts);
20212184
uint64_t t1 = now_ns();
2185+
#ifdef CBM_ENABLE_TEST_SEAMS
2186+
t1 += tl_parse_wall_seam_offset_ns; /* the stall seam inflates every wall reading */
2187+
#endif
20222188

20232189
if (!tree) {
20242190
result->has_error = true;
@@ -2669,7 +2835,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
26692835
* splitting a brace inside a recovered function looks unrecovered:
26702836
* the refinement keeps only the discarded branch, the function starts
26712837
* above it, and the evidence falls outside the range. */
2672-
cbm_subtract_recovered_regions(&regs, &result->defs);
2838+
cbm_subtract_recovered_regions(&regs, &result->defs, source, source_len);
26732839
/* Phase 2: cut what is left down to the lines the preprocessed parse
26742840
* could not explain. */
26752841
if (pp_line_map) {

‎internal/cbm/cbm.h‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,26 +269,31 @@ typedef struct {
269269
* calls at 251 MB with most of that empty slots. Readers index it exactly
270270
* as before; only `sizeof` changed. */
271271
CBMCallArg *args;
272-
int arg_count; // number of captured arguments (<= CBM_MAX_CALL_ARGS)
273-
int loop_depth; // enclosing loop nesting at the call site
274-
int branch_depth; // enclosing branch nesting at the call site
275-
int start_line; // 1-based source line of the call (for def range-match)
276-
uint32_t site_start_byte; // exact AST occurrence span; end > start when present
277-
uint32_t site_end_byte; // exclusive byte offset in the source file
278-
CBMSourceOrigin source_origin; // raw source or C-family preprocessed buffer
279-
bool is_method; // method/member call with an UNRESOLVED receiver. Perl:
280-
// arrow/method call ($obj->m). TS/JS/TSX: member call
281-
// x.foo() whose receiver is not this/super. Python:
282-
// x.foo() where x is not self/cls/super() and is not
283-
// rooted in an imported name. Read by the weak-member
284-
// guard and by the pxc synthetic-carrier dedup key in
285-
// pass_lsp_cross.c. Default false.
286-
bool requires_lsp_resolution; // synthetic semantic candidate (for example an implicit
287-
// C++ operator). Never fall back to textual resolution.
288-
bool callee_is_locally_bound; // bare call foo() whose callee identifier is bound as a
289-
// parameter of an enclosing function, so it cannot be the
290-
// module-level foo. Python only today. Read by the
291-
// weak-local-binding guard. Default false.
272+
int arg_count; // number of captured arguments (<= CBM_MAX_CALL_ARGS)
273+
int loop_depth; // enclosing loop nesting at the call site
274+
int branch_depth; // enclosing branch nesting at the call site
275+
int start_line; // 1-based source line of the call (for def range-match)
276+
uint32_t site_start_byte; // exact AST occurrence span; end > start when present
277+
uint32_t site_end_byte; // exclusive byte offset in the source file
278+
CBMSourceOrigin source_origin; // raw source or C-family preprocessed buffer
279+
bool is_method; // method/member call with an UNRESOLVED receiver. Perl:
280+
// arrow/method call ($obj->m). TS/JS/TSX: member call
281+
// x.foo() whose receiver is not this/super. Python:
282+
// x.foo() where x is not self/cls/super() and is not
283+
// rooted in an imported name. Read by the weak-member
284+
// guard and by the pxc synthetic-carrier dedup key in
285+
// pass_lsp_cross.c. Default false.
286+
bool requires_lsp_resolution; // synthetic semantic candidate (for example an implicit
287+
// C++ operator). Never fall back to textual resolution.
288+
bool callee_is_locally_bound; // bare call foo() whose callee identifier is bound as a
289+
// parameter of an enclosing function, so it cannot be the
290+
// module-level foo. Python only today. Read by the
291+
// weak-local-binding guard. Default false.
292+
bool receiver_is_self_attribute; // Python member call whose receiver is an attribute
293+
// chain rooted at self/cls but not self/cls itself
294+
// (self.compiler.apply_converters()). An object the
295+
// class owns, not a parameter: read by the weak-member
296+
// guard's unique-name exemption. Default false.
292297
} CBMCall;
293298

294299
typedef struct {

‎internal/cbm/extract_calls.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,14 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
20682068
for (uint32_t ai = 0; ai < argc && call->arg_count < CBM_MAX_CALL_ARGS; ai++) {
20692069
TSNode arg_node = ts_node_named_child(args, ai);
20702070
const char *ak = ts_node_type(arg_node);
2071+
/* tree-sitter lists a comment inside the argument list as a named
2072+
* child (Java block_comment/line_comment, C-family comment). It is
2073+
* not an argument: taken as one, a leading block comment became
2074+
* args[0] and the Route pass read its text as a URL. */
2075+
if (strcmp(ak, "comment") == 0 || strcmp(ak, "line_comment") == 0 ||
2076+
strcmp(ak, "block_comment") == 0) {
2077+
continue;
2078+
}
20712079
if (!call->args) {
20722080
call->args = cbm_arena_calloc(ctx->arena, CBM_MAX_CALL_ARGS * sizeof(CBMCallArg));
20732081
if (!call->args) {
@@ -2965,6 +2973,28 @@ static char *resolve_objectscript_instance_call(CBMArena *a, TSNode node, const
29652973
* statically-known type here, so the call must not bind by short name alone.
29662974
* Note `self.client.send()` is NOT exempt: the receiver is `self.client`, an
29672975
* attribute of unknown type, not `self` itself. */
2976+
/* True when a Python attribute-call receiver is an attribute chain ROOTED at
2977+
* self/cls but is not self/cls itself: `self.compiler.apply_converters()` has
2978+
* receiver `self.compiler`, an object the class owns. The weak-member guard's
2979+
* unique-name exemption keys on this shape (see cbm_weak_member_unique_name_
2980+
* exempt): a bare parameter (`accelerator.backward()`) carries no ownership
2981+
* evidence and stays suppressed. Direct `self.m()` is already exempt via
2982+
* python_receiver_is_exempt and is deliberately NOT flagged here. */
2983+
static bool python_receiver_rooted_at_self(CBMExtractCtx *ctx, TSNode receiver) {
2984+
if (ts_node_is_null(receiver) || strcmp(ts_node_type(receiver), "attribute") != 0) {
2985+
return false;
2986+
}
2987+
TSNode root = receiver;
2988+
while (!ts_node_is_null(root) && strcmp(ts_node_type(root), "attribute") == 0) {
2989+
root = ts_node_child_by_field_name(root, TS_FIELD("object"));
2990+
}
2991+
if (ts_node_is_null(root) || strcmp(ts_node_type(root), "identifier") != 0) {
2992+
return false;
2993+
}
2994+
char *name = cbm_node_text(ctx->arena, root, ctx->source);
2995+
return name && (strcmp(name, "self") == 0 || strcmp(name, "cls") == 0);
2996+
}
2997+
29682998
static bool python_receiver_is_exempt(CBMExtractCtx *ctx, TSNode receiver) {
29692999
if (ts_node_is_null(receiver)) {
29703000
return false;
@@ -3695,6 +3725,7 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
36953725
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "attribute") == 0) {
36963726
TSNode obj = ts_node_child_by_field_name(fn, TS_FIELD("object"));
36973727
call.is_method = !python_receiver_is_exempt(ctx, obj);
3728+
call.receiver_is_self_attribute = python_receiver_rooted_at_self(ctx, obj);
36983729
} else if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
36993730
call.callee_is_locally_bound = python_callee_is_bound_parameter(ctx, state, fn);
37003731
}

0 commit comments

Comments
 (0)