Skip to content

Commit aa44c28

Browse files
authored
Merge pull request #1941 from CaptainMittens/fix/parse-coverage-honest-ranges
fix(coverage): narrow parse-error ranges, and stop the report hiding what it dropped (#963)
2 parents 9949908 + ddb6533 commit aa44c28

12 files changed

Lines changed: 1261 additions & 68 deletions

File tree

‎Makefile.cbm‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ endif
9898
KOTLIN_DEDUP_TEST_DEFINE = -DCBM_KOTLIN_DEDUP_TEST_API=1
9999
CALL_REFERENCE_LOOKUP_TEST_DEFINE = -DCBM_CALL_REFERENCE_LOOKUP_TEST_API=1
100100
INCREMENTAL_TEST_DEFINE = -DCBM_INCREMENTAL_TEST_API=1
101+
COVERAGE_MARKER_TEST_DEFINE = -DCBM_COVERAGE_MARKER_TEST_API=1
101102
CFLAGS_TEST = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(SANITIZED_DEFINE) \
102103
$(KOTLIN_DEDUP_TEST_DEFINE) $(CALL_REFERENCE_LOOKUP_TEST_DEFINE) \
103-
$(INCREMENTAL_TEST_DEFINE) -g -O1 $(SANITIZE)
104+
$(INCREMENTAL_TEST_DEFINE) $(COVERAGE_MARKER_TEST_DEFINE) -g -O1 $(SANITIZE)
104105
CXXFLAGS_TEST = $(CXXFLAGS_COMMON) $(SANITIZED_DEFINE) -g -O1 $(SANITIZE) $(CXX_STDLIB_FLAGS)
105106

106107
# TSan (can't combine with ASan)
@@ -118,6 +119,7 @@ TSAN_SANITIZE = -fsanitize=thread -fno-omit-frame-pointer
118119
# macro of ours.
119120
CFLAGS_TSAN = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(KOTLIN_DEDUP_TEST_DEFINE) \
120121
$(CALL_REFERENCE_LOOKUP_TEST_DEFINE) $(INCREMENTAL_TEST_DEFINE) \
122+
$(COVERAGE_MARKER_TEST_DEFINE) \
121123
-DCBM_SANITIZED_BUILD=1 -g -O1 $(TSAN_SANITIZE)
122124
CXXFLAGS_TSAN = $(CXXFLAGS_COMMON) -DCBM_SANITIZED_BUILD=1 -g -O1 \
123125
$(TSAN_SANITIZE)

‎internal/cbm/cbm.c‎

Lines changed: 413 additions & 31 deletions
Large diffs are not rendered by default.

‎internal/cbm/cbm.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,21 @@ typedef struct CBMFileResult {
522522
* completeness guarantee. Callers should treat a flagged file as "prefer
523523
* grep here", never treat an unflagged file as provably complete. */
524524
bool parse_incomplete;
525+
/* True when the ranges cover so much of the file that they are no longer
526+
* useful advice — one range over 80% of the line count. The file WAS
527+
* indexed, but pointing a reader at almost every line tells them nothing,
528+
* so the report says "read the source" instead of listing the range.
529+
*
530+
* Its main customers are non-C languages. The refinement that narrows a
531+
* whole-file range using the preprocessed parse only runs for C, C++ and
532+
* CUDA, so a Python, Java or Ruby file whose root node is ERROR still
533+
* reports 1-N.
534+
*
535+
* Note the naming: this field and the phase string it produces are both
536+
* `parse_unusable`. The older `parse_incomplete` field emits the phase
537+
* `parse_partial` instead. That mismatch is historical, not deliberate —
538+
* do not copy it. */
539+
bool parse_unusable;
525540
const char *error_ranges;
526541
int error_region_count;
527542
bool is_test_file;

‎src/mcp/mcp.c‎

Lines changed: 149 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6052,10 +6052,12 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
60526052
bool have_meta = cbm_store_coverage_meta_get(store, project, &meta) == CBM_STORE_OK;
60536053

60546054
yyjson_mut_val *pp_files = yyjson_mut_arr(doc);
6055+
yyjson_mut_val *pu_files = yyjson_mut_arr(doc);
60556056
yyjson_mut_val *sk_files = yyjson_mut_arr(doc);
60566057
yyjson_mut_val *ni_dirs = yyjson_mut_arr(doc);
60576058
yyjson_mut_val *ni_files = yyjson_mut_arr(doc);
60586059
int pp_n = 0;
6060+
int pu_n = 0;
60596061
int sk_n = 0;
60606062
int ni_dir_n = 0;
60616063
int ni_file_n = 0;
@@ -6070,6 +6072,21 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
60706072
yyjson_mut_arr_add_val(pp_files, fe);
60716073
}
60726074
pp_n++;
6075+
} else if (strcmp(kind, "parse_unusable") == 0) {
6076+
/* Needs its own branch. The catch-all below builds skipped[], and
6077+
* a reader who finds a file there believes it was never indexed. */
6078+
if (pu_n < COVERAGE_FILE_CAP) {
6079+
yyjson_mut_val *fe = yyjson_mut_obj(doc);
6080+
yyjson_mut_obj_add_strcpy(doc, fe, "path", rows[i].rel_path);
6081+
yyjson_mut_obj_add_bool(doc, fe, "whole_file", true);
6082+
/* The end of the range, not the length of the file. A grammar
6083+
* can end an error node past the last line, so this number can
6084+
* be larger than the file. See range_end_is_not_file_length. */
6085+
const char *dash = rows[i].detail ? strchr(rows[i].detail, '-') : NULL;
6086+
yyjson_mut_obj_add_int(doc, fe, "range_end", dash ? atoi(dash + 1) : 0);
6087+
yyjson_mut_arr_add_val(pu_files, fe);
6088+
}
6089+
pu_n++;
60736090
} else if (strcmp(kind, "not_indexed_dir") == 0) {
60746091
if (ni_dir_n < sample_limit) {
60756092
yyjson_mut_arr_add_strcpy(doc, ni_dirs, rows[i].rel_path);
@@ -6119,6 +6136,14 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
61196136
}
61206137
yyjson_mut_obj_add_val(doc, root, "parse_partial", pp);
61216138

6139+
/* Indexed, but the parse failed across nearly the whole file, so naming
6140+
* line ranges helps nobody — read the source instead. */
6141+
yyjson_mut_val *pu = yyjson_mut_obj(doc);
6142+
yyjson_mut_obj_add_val(doc, pu, "files", pu_files);
6143+
yyjson_mut_obj_add_int(doc, pu, "count", pu_n);
6144+
yyjson_mut_obj_add_bool(doc, pu, "truncated", pu_n > COVERAGE_FILE_CAP);
6145+
yyjson_mut_obj_add_val(doc, root, "parse_unusable", pu);
6146+
61226147
yyjson_mut_val *sk = yyjson_mut_obj(doc);
61236148
yyjson_mut_obj_add_val(doc, sk, "files", sk_files);
61246149
yyjson_mut_obj_add_int(doc, sk, "count", sk_n);
@@ -6173,7 +6198,8 @@ enum {
61736198
COVERAGE_SCOPE_MAX = 32,
61746199
COVERAGE_SCOPE_DEFAULT_LIMIT = 200,
61756200
COVERAGE_SCOPE_MAX_LIMIT = 1000,
6176-
COVERAGE_RANGE_MAX = 128,
6201+
COVERAGE_RANGE_MAX = 256, /* matches CBM_MAX_ERROR_REGIONS — a lower value here
6202+
would just move the silent clip downstream */
61776203
};
61786204

61796205
bool cbm_path_within_root(const char *root_path, const char *abs_path); /* defined below */
@@ -6285,17 +6311,30 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
62856311
return matches ? "metadata_match" : "metadata_changed";
62866312
}
62876313

6314+
/* Read an "start-end,start-end,...[,+<N>]" string into a JSON ranges array.
6315+
*
6316+
* The optional trailing "+<N>" says the producer's own cap threw N ranges away.
6317+
* Without reading it, a clipped list arrives here looking complete: the loop
6318+
* below stops at the '+' with no error and no leftover, so the row would claim
6319+
* a short, tidy set of ranges that is in fact missing entries. Set
6320+
* "ranges_truncated": true whenever ranges were lost — either by that marker,
6321+
* or by COVERAGE_RANGE_MAX stopping this loop. */
62886322
static bool coverage_add_ranges(yyjson_mut_doc *doc, yyjson_mut_val *row, const char *detail) {
62896323
if (!detail || !detail[0]) {
62906324
return false;
62916325
}
62926326
yyjson_mut_val *ranges = yyjson_mut_arr(doc);
62936327
const char *p = detail;
62946328
int emitted = 0;
6329+
bool truncated = false;
62956330
while (*p && emitted < COVERAGE_RANGE_MAX) {
62966331
while (*p == ' ' || *p == ',') {
62976332
p++;
62986333
}
6334+
if (*p == '+') {
6335+
truncated = true; /* the producer's cap dropped ranges we never saw */
6336+
break;
6337+
}
62996338
if (!isdigit((unsigned char)*p)) {
63006339
break;
63016340
}
@@ -6327,11 +6366,17 @@ static bool coverage_add_ranges(yyjson_mut_doc *doc, yyjson_mut_val *row, const
63276366
break;
63286367
}
63296368
}
6369+
if (emitted >= COVERAGE_RANGE_MAX && *p) {
6370+
truncated = true; /* our own limit stopped the loop with input left over */
6371+
}
63306372
if (emitted > 0) {
63316373
yyjson_mut_obj_add_val(doc, row, "ranges", ranges);
6332-
if (emitted == COVERAGE_RANGE_MAX && *p) {
6333-
yyjson_mut_obj_add_bool(doc, row, "ranges_truncated", true);
6334-
}
6374+
}
6375+
/* Losing ranges to the producer's cap and losing them to this reader's cap
6376+
* are the same fact for a caller: the list is short and what is missing is
6377+
* unknown. One flag says so, whether or not any range survived. */
6378+
if (truncated) {
6379+
yyjson_mut_obj_add_bool(doc, row, "ranges_truncated", true);
63356380
}
63366381
return emitted > 0;
63376382
}
@@ -6348,7 +6393,8 @@ static void coverage_add_row_json(yyjson_mut_doc *doc, yyjson_mut_val *array,
63486393
row->rel_path && strcmp(row->rel_path, requested_path) == 0 ? "exact" : "ancestor");
63496394
}
63506395
bool ranges_added = false;
6351-
if (row->kind && strcmp(row->kind, "parse_partial") == 0) {
6396+
if (row->kind &&
6397+
(strcmp(row->kind, "parse_partial") == 0 || strcmp(row->kind, "parse_unusable") == 0)) {
63526398
ranges_added = coverage_add_ranges(doc, item, row->detail);
63536399
}
63546400
/* A parse_partial detail such as "3-4,9" is byte-for-byte redundant once
@@ -6381,6 +6427,12 @@ static const char *coverage_status(const cbm_coverage_row_t *rows, int count,
63816427
continue;
63826428
}
63836429
const char *kind = rows[i].kind ? rows[i].kind : "";
6430+
/* "parse_unusable" must be named here. Without its own case it
6431+
* falls through to the catch-all below and reports "skipped",
6432+
* which is wrong in the way that matters: the file WAS indexed. */
6433+
if (pass == 0 && strcmp(kind, "parse_unusable") == 0) {
6434+
return "unusable";
6435+
}
63846436
if (pass == 0 && strcmp(kind, "parse_partial") == 0) {
63856437
return "partial";
63866438
}
@@ -6408,6 +6460,11 @@ static const char *coverage_recommended_action(const char *status, const char *f
64086460
if (strcmp(status, "partial") == 0) {
64096461
return "read_ranges_and_verify_scope";
64106462
}
6463+
if (strcmp(status, "unusable") == 0) {
6464+
/* The ranges cover nearly the whole file, so sending a reader to them
6465+
* is the same as sending them to the file. Say the useful thing. */
6466+
return "read_source_directly";
6467+
}
64116468
if (strcmp(status, "skipped") == 0) {
64126469
return "read_source_directly";
64136470
}
@@ -9987,20 +10044,33 @@ static bool is_parse_partial(const cbm_file_error_t *e) {
998710044
return e->phase && strcmp(e->phase, "parse_partial") == 0;
998810045
}
998910046

10047+
/* The same, for the whole-file variant: one range covers 80% or more of the
10048+
* file, so listing the lines is useless advice. Also indexed, also not a skip. */
10049+
static bool is_parse_unusable(const cbm_file_error_t *e) {
10050+
return e->phase && strcmp(e->phase, "parse_unusable") == 0;
10051+
}
10052+
10053+
/* Either coverage phase. Both mean the file WAS indexed, so both must stay out
10054+
* of skipped[] — a reader who sees a file there believes it is absent from the
10055+
* graph entirely. */
10056+
static bool is_parse_coverage(const cbm_file_error_t *e) {
10057+
return is_parse_partial(e) || is_parse_unusable(e);
10058+
}
10059+
999010060
/* Attach a summary of per-file skips (Stage 2 / Track B). Always emits a
999110061
* top-level "skipped_count" (0 on clean runs) so consumers can rely on it.
999210062
* When there are skips, also emits:
999310063
* "skipped": {"files":[{path,reason,phase}..(<=50)], "count":N, "truncated":bool}
999410064
* and, if a per-run logfile was written, "logfile": "<path>".
999510065
* The run status stays "indexed" — a skipped file is the expected handled
999610066
* outcome, not a failure. errs[] is borrowed (copied into doc) and may contain
9997-
* parse_partial entries, which are filtered out here (reported separately by
9998-
* add_parse_partial_summary). */
10067+
* parse_partial and parse_unusable entries, which are filtered out here (both
10068+
* reported separately by add_parse_partial_summary). */
999910069
static void add_skipped_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
1000010070
const cbm_file_error_t *errs, int count, const char *logfile) {
1000110071
int skips = 0;
1000210072
for (int i = 0; i < count; i++) {
10003-
if (!is_parse_partial(&errs[i])) {
10073+
if (!is_parse_coverage(&errs[i])) {
1000410074
skips++;
1000510075
}
1000610076
}
@@ -10015,7 +10085,7 @@ static void add_skipped_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
1001510085
yyjson_mut_val *files = yyjson_mut_arr(doc);
1001610086
int shown = 0;
1001710087
for (int i = 0; i < count && shown < INDEX_SKIPPED_FILE_CAP; i++) {
10018-
if (is_parse_partial(&errs[i])) {
10088+
if (is_parse_coverage(&errs[i])) {
1001910089
continue;
1002010090
}
1002110091
yyjson_mut_val *fe = yyjson_mut_obj(doc);
@@ -10074,6 +10144,55 @@ static void add_parse_partial_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
1007410144
yyjson_mut_obj_add_val(doc, root, "parse_partial", pp);
1007510145
}
1007610146

10147+
/* Attach the whole-file half of the coverage summary. Always emits a top-level
10148+
* "parse_unusable_count" (0 on clean runs) so the CI coverage gate can read it
10149+
* without parsing anything else. When files were flagged:
10150+
* "parse_unusable": {"files":[{path,range_end,whole_file}..(<=50)], "count":N,
10151+
* "truncated":bool, "note":"..."}
10152+
*
10153+
* These files WERE indexed, exactly like parse_partial ones. The difference is
10154+
* that their range covers 80% or more of the file, so the range is not worth
10155+
* printing — "range_end" gives the last line the range names and "whole_file"
10156+
* says plainly that reading the ranges is the same as reading the file. */
10157+
static void add_parse_unusable_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
10158+
const cbm_file_error_t *errs, int count) {
10159+
int unusable = 0;
10160+
for (int i = 0; i < count; i++) {
10161+
if (is_parse_unusable(&errs[i])) {
10162+
unusable++;
10163+
}
10164+
}
10165+
yyjson_mut_obj_add_int(doc, root, "parse_unusable_count", unusable);
10166+
if (!errs || unusable <= 0) {
10167+
return;
10168+
}
10169+
yyjson_mut_val *pu = yyjson_mut_obj(doc);
10170+
yyjson_mut_val *files = yyjson_mut_arr(doc);
10171+
int shown = 0;
10172+
for (int i = 0; i < count && shown < INDEX_SKIPPED_FILE_CAP; i++) {
10173+
if (!is_parse_unusable(&errs[i])) {
10174+
continue;
10175+
}
10176+
yyjson_mut_val *fe = yyjson_mut_obj(doc);
10177+
yyjson_mut_obj_add_strcpy(doc, fe, "path", errs[i].path ? errs[i].path : "");
10178+
yyjson_mut_obj_add_bool(doc, fe, "whole_file", true);
10179+
/* The end of the range, not the length of the file. A grammar can end
10180+
* an error node past the last line, so this number can be larger than
10181+
* the file. See range_end_is_not_file_length. */
10182+
const char *dash = errs[i].reason ? strchr(errs[i].reason, '-') : NULL;
10183+
yyjson_mut_obj_add_int(doc, fe, "range_end", dash ? atoi(dash + 1) : 0);
10184+
yyjson_mut_arr_add_val(files, fe);
10185+
shown++;
10186+
}
10187+
yyjson_mut_obj_add_val(doc, pu, "files", files);
10188+
yyjson_mut_obj_add_int(doc, pu, "count", unusable);
10189+
yyjson_mut_obj_add_bool(doc, pu, "truncated", unusable > INDEX_SKIPPED_FILE_CAP);
10190+
yyjson_mut_obj_add_str(doc, pu, "note",
10191+
"Indexed, but the parse failed across nearly the whole file, so line "
10192+
"ranges are not useful here — read the source directly.");
10193+
yyjson_mut_obj_add_val(doc, root, "parse_unusable", pu);
10194+
}
10195+
1007710196
/* The pipeline persists the complete current coverage set before this
1007810197
* response is built. Prefer that set over the per-run errors so incremental
1007910198
* runs that do not revisit a flagged file, and artifact bootstraps, do not
@@ -10117,6 +10236,7 @@ static bool add_persisted_failure_summaries(yyjson_mut_doc *doc, yyjson_mut_val
1011710236

1011810237
add_skipped_summary(doc, root, failures, failure_count, logfile);
1011910238
add_parse_partial_summary(doc, root, failures, failure_count);
10239+
add_parse_unusable_summary(doc, root, failures, failure_count);
1012010240
free(failures);
1012110241
cbm_store_free_coverage(rows, row_count);
1012210242
return true;
@@ -10194,6 +10314,7 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
1019410314
if (!store || !add_persisted_failure_summaries(doc, root, store, project_name, logfile)) {
1019510315
add_skipped_summary(doc, root, file_errors, file_error_count, logfile);
1019610316
add_parse_partial_summary(doc, root, file_errors, file_error_count);
10317+
add_parse_unusable_summary(doc, root, file_errors, file_error_count);
1019710318
}
1019810319
int nodes = 0;
1019910320
int edges = 0;
@@ -11355,10 +11476,10 @@ static void add_string_array(yyjson_mut_doc *doc, yyjson_mut_val *obj, const cha
1135511476
}
1135611477

1135711478
/* get_code_snippet coverage note (#963): if the resolved node's file is
11358-
* flagged parse_partial, warn that the graph may under-report this file.
11359-
* Correlated by construction — the result names its file. (An entirely-
11360-
* skipped file cannot appear here: it has no nodes to resolve a snippet
11361-
* from.) */
11479+
* flagged parse_partial or parse_unusable, warn that the graph may
11480+
* under-report this file. Correlated by construction — the result names its
11481+
* file. (An entirely-skipped file cannot appear here: it has no nodes to
11482+
* resolve a snippet from.) */
1136211483
static void add_snippet_coverage_note(yyjson_mut_doc *doc, yyjson_mut_val *root_obj,
1136311484
cbm_store_t *store, const cbm_node_t *node) {
1136411485
if (!node->file_path || !node->file_path[0] || !node->project) {
@@ -11371,18 +11492,28 @@ static void add_snippet_coverage_note(yyjson_mut_doc *doc, yyjson_mut_val *root_
1137111492
return;
1137211493
}
1137311494
for (int i = 0; i < count; i++) {
11374-
if (rows[i].rel_path && strcmp(rows[i].rel_path, node->file_path) == 0 && rows[i].kind &&
11375-
strcmp(rows[i].kind, "parse_partial") == 0) {
11376-
char note[CBM_SZ_1K];
11495+
if (!rows[i].rel_path || strcmp(rows[i].rel_path, node->file_path) != 0 || !rows[i].kind) {
11496+
continue;
11497+
}
11498+
char note[CBM_SZ_1K];
11499+
if (strcmp(rows[i].kind, "parse_unusable") == 0) {
11500+
snprintf(note, sizeof(note),
11501+
"The parse of this file failed across nearly the whole of it, so most "
11502+
"constructs are missing from the graph and naming line ranges would not "
11503+
"help. Read the source directly — the source above is ground truth. "
11504+
"(best-effort signal)");
11505+
} else if (strcmp(rows[i].kind, "parse_partial") == 0) {
1137711506
snprintf(note, sizeof(note),
1137811507
"This file was only PARTIALLY indexed — line range(s) %s could not be "
1137911508
"parsed, so constructs there may be missing from the graph (callers/callees "
1138011509
"and search results can under-report this file). The source above is ground "
1138111510
"truth. (best-effort signal)",
1138211511
rows[i].detail && rows[i].detail[0] ? rows[i].detail : "?");
11383-
yyjson_mut_obj_add_strcpy(doc, root_obj, "coverage_note", note);
11384-
break;
11512+
} else {
11513+
continue;
1138511514
}
11515+
yyjson_mut_obj_add_strcpy(doc, root_obj, "coverage_note", note);
11516+
break;
1138611517
}
1138711518
cbm_store_free_coverage(rows, count);
1138811519
}

0 commit comments

Comments
 (0)