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.

fix(coverage): stop the report hiding what it dropped, and name whole-file failures (#963)

Joshua Richtera953509275a4
diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c
index c5ec2c9b3..83ae75c51 100644
--- a/internal/cbm/cbm.c
+++ b/internal/cbm/cbm.c
@@ -778,16 +778,24 @@ static bool cbm_source_nesting_exceeds(const char *source, int source_len, int c
* nodes (does not descend into an error subtree — one range per failed region).
* Bounded by CBM_MAX_ERROR_REGIONS so pathological input can't blow up the
* output. The ranges mark where constructs were dropped; they are a detection
- * aid, never a completeness proof. */
-#define CBM_MAX_ERROR_REGIONS 64
+ * aid, never a completeness proof.
+ *
+ * `dropped` counts the ranges the cap threw away. It exists so a clipped list
+ * cannot read as a complete one: cbm_error_ranges_str turns a non-zero count
+ * into a trailing "+<N>" marker. Phase 2 split one whole-file range into many
+ * small ones, which pushed real files straight into a cap that used to be
+ * unreachable, so the clip is live behaviour and not a theoretical limit. */
+#define CBM_MAX_ERROR_REGIONS 256
typedef struct {
uint32_t starts[CBM_MAX_ERROR_REGIONS];
uint32_t ends[CBM_MAX_ERROR_REGIONS];
int count;
+ int dropped;
} cbm_error_regions_t;
static void cbm_error_regions_push(cbm_error_regions_t *acc, TSNode n) {
if (acc->count >= CBM_MAX_ERROR_REGIONS) {
+ acc->dropped++;
return;
}
acc->starts[acc->count] = ts_node_start_point(n).row + 1;
@@ -851,13 +859,14 @@ static bool cbm_is_eof_terminator_miss(TSNode n, const char *source, int source_
return true;
}
+/* Walks to the end even after the cap is full, so `dropped` is the real number
+ * of ranges lost rather than a lower bound. This costs little: the walk never
+ * descends into an ERROR subtree — it records the top-most node and moves on —
+ * so it only visits the spine of nodes that contain an error, plus one level. */
static void cbm_collect_error_regions(TSNode n, cbm_error_regions_t *acc, const char *source,
int source_len) {
- if (acc->count >= CBM_MAX_ERROR_REGIONS) {
- return;
- }
uint32_t k = ts_node_child_count(n);
- for (uint32_t i = 0; i < k && acc->count < CBM_MAX_ERROR_REGIONS; i++) {
+ for (uint32_t i = 0; i < k; i++) {
TSNode c = ts_node_child(n, i);
if (ts_node_is_missing(c) || strcmp(ts_node_type(c), "ERROR") == 0) {
if (cbm_is_eof_terminator_miss(c, source, source_len)) {
@@ -1298,7 +1307,11 @@ static void cbm_push_trimmed_run(cbm_error_regions_t *out, uint32_t start, uint3
while (end >= start && end <= line_count && (map[end] & CBM_LINE_NO_CODE)) {
end--;
}
- if (start > end || out->count >= CBM_MAX_ERROR_REGIONS) {
+ if (start > end) {
+ return; /* nothing but blank, comment or directive lines — no construct lost */
+ }
+ if (out->count >= CBM_MAX_ERROR_REGIONS) {
+ out->dropped++;
return;
}
out->starts[out->count] = start;
@@ -1330,7 +1343,7 @@ static bool cbm_line_is_toplevel_macro_call(const char *src, int src_len, uint32
static void cbm_refine_regions_with_pp_lines(cbm_error_regions_t *regs, const uint8_t *map,
uint32_t line_count, const char *src, int src_len,
const CBMDefArray *defs) {
- cbm_error_regions_t out = {{0}, {0}, 0};
+ cbm_error_regions_t out = {{0}, {0}, 0, regs->dropped};
for (int i = 0; i < regs->count; i++) {
uint32_t run_start = 0;
uint32_t run_end = 0;
@@ -1357,12 +1370,40 @@ static void cbm_refine_regions_with_pp_lines(cbm_error_regions_t *regs, const ui
}
/* Serialize collected regions as "start-end,start-end,..." into the arena. */
+/* Share of a file one range must cover before the range stops being advice and
+ * becomes noise. 80% is well clear of anything real: the widest single range in
+ * this repo covers 25.5% of its file, and the next widest 3.9%. */
+#define CBM_UNUSABLE_PCT 80
+
+/* Number of 1-based lines in `src`. A file that does not end with a newline
+ * still has a last line, so the count is separators plus one. */
+static uint32_t cbm_count_lines(const char *src, int src_len) {
+ uint32_t n = 1;
+ for (int i = 0; i < src_len; i++) {
+ if (src[i] == '\n' && i + 1 < src_len) {
+ n++;
+ }
+ }
+ return n;
+}
+
+/* Serialize collected regions as "start-end,start-end,...", with a trailing
+ * ",+<N>" when the cap threw N ranges away.
+ *
+ * The marker must stay a SUFFIX and nothing else. Every reader stops at the
+ * first token that is not a range, so a marker in the middle of a string
+ * silently hides everything after it. objectscript_export_append_error_ranges
+ * strips markers before joining two parts for exactly that reason.
+ *
+ * N can be non-zero while the kept list is short, because the recovery and
+ * macro rules run after collection and remove ranges the cap never saw. That
+ * still reports honestly: the cap bound, so what was lost is unknown. */
static const char *cbm_error_ranges_str(CBMArena *a, const cbm_error_regions_t *regs) {
- if (regs->count <= 0) {
+ if (regs->count <= 0 && regs->dropped <= 0) {
return NULL;
}
enum { RANGE_MAX = 24 }; /* "4294967295-4294967295," */
- char *buf = (char *)cbm_arena_alloc(a, (size_t)regs->count * RANGE_MAX);
+ char *buf = (char *)cbm_arena_alloc(a, (size_t)(regs->count + 1) * RANGE_MAX);
if (!buf) {
return NULL;
}
@@ -1371,6 +1412,9 @@ static const char *cbm_error_ranges_str(CBMArena *a, const cbm_error_regions_t *
off += (size_t)snprintf(buf + off, RANGE_MAX, "%s%u-%u", i ? "," : "", regs->starts[i],
regs->ends[i]);
}
+ if (regs->dropped > 0) {
+ snprintf(buf + off, RANGE_MAX, "%s+%d", off ? "," : "", regs->dropped);
+ }
return buf;
}
@@ -1706,7 +1750,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
* the raw source line, and whose QN the raw pass did not
* already extract. */
if (ts_node_has_error(root)) {
- cbm_error_regions_t raw_regs = {{0}, {0}, 0};
+ cbm_error_regions_t raw_regs = {{0}, {0}, 0, 0};
cbm_collect_error_regions(root, &raw_regs, source, source_len);
if (raw_regs.count > 0) {
int defs_before = result->defs.count;
@@ -1915,7 +1959,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
* miss, and a fully recovered file is not flagged at all. Detection aid
* only: the absence of this flag is NOT a completeness guarantee. */
if (ts_node_has_error(root)) {
- cbm_error_regions_t regs = {{0}, {0}, 0};
+ cbm_error_regions_t regs = {{0}, {0}, 0, 0};
if (strcmp(ts_node_type(root), "ERROR") == 0) {
cbm_error_regions_push(&regs, root); /* whole file unparseable */
} else {
@@ -1940,10 +1984,25 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
* refinement, because its evidence is per-line: a narrow range points at
* the call itself instead of the whole blob around it. */
cbm_subtract_macro_invocation_regions(&regs, &result->defs, source, source_len);
- if (regs.count > 0) {
+ /* A file whose kept list is empty but whose cap still bound is NOT clean:
+ * the ranges the cap threw away were never judged by the two rules
+ * above, so nothing proves they were recovered. Flag it. */
+ if (regs.count > 0 || regs.dropped > 0) {
result->parse_incomplete = true;
result->error_region_count = regs.count;
result->error_ranges = cbm_error_ranges_str(a, &regs);
+ /* One range covering nearly the whole file is not advice, it is
+ * noise: "look at lines 1 to 13047" of a 13046-line file tells a
+ * reader nothing they did not already know. Mark those separately
+ * so the report can say "read the source" instead. See
+ * parse_unusable in cbm.h for which files land here and why. */
+ if (regs.count == 1 && regs.dropped == 0) {
+ uint32_t total = cbm_count_lines(source, source_len);
+ uint32_t span = regs.ends[0] - regs.starts[0] + 1;
+ if (total > 0 && span * 100 >= total * CBM_UNUSABLE_PCT) {
+ result->parse_unusable = true;
+ }
+ }
}
}
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index f5636738d..a5bdba999 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -522,6 +522,21 @@ typedef struct CBMFileResult {
* completeness guarantee. Callers should treat a flagged file as "prefer
* grep here", never treat an unflagged file as provably complete. */
bool parse_incomplete;
+ /* True when the ranges cover so much of the file that they are no longer
+ * useful advice — one range over 80% of the line count. The file WAS
+ * indexed, but pointing a reader at almost every line tells them nothing,
+ * so the report says "read the source" instead of listing the range.
+ *
+ * Its main customers are non-C languages. The refinement that narrows a
+ * whole-file range using the preprocessed parse only runs for C, C++ and
+ * CUDA, so a Python, Java or Ruby file whose root node is ERROR still
+ * reports 1-N.
+ *
+ * Note the naming: this field and the phase string it produces are both
+ * `parse_unusable`. The older `parse_incomplete` field emits the phase
+ * `parse_partial` instead. That mismatch is historical, not deliberate —
+ * do not copy it. */
+ bool parse_unusable;
const char *error_ranges;
int error_region_count;
bool is_test_file;
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 3259221c4..d9f61ee2b 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -6032,10 +6032,12 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
bool have_meta = cbm_store_coverage_meta_get(store, project, &meta) == CBM_STORE_OK;
yyjson_mut_val *pp_files = yyjson_mut_arr(doc);
+ yyjson_mut_val *pu_files = yyjson_mut_arr(doc);
yyjson_mut_val *sk_files = yyjson_mut_arr(doc);
yyjson_mut_val *ni_dirs = yyjson_mut_arr(doc);
yyjson_mut_val *ni_files = yyjson_mut_arr(doc);
int pp_n = 0;
+ int pu_n = 0;
int sk_n = 0;
int ni_dir_n = 0;
int ni_file_n = 0;
@@ -6050,6 +6052,18 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
yyjson_mut_arr_add_val(pp_files, fe);
}
pp_n++;
+ } else if (strcmp(kind, "parse_unusable") == 0) {
+ /* Needs its own branch. The catch-all below builds skipped[], and
+ * a reader who finds a file there believes it was never indexed. */
+ if (pu_n < COVERAGE_FILE_CAP) {
+ yyjson_mut_val *fe = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_strcpy(doc, fe, "path", rows[i].rel_path);
+ yyjson_mut_obj_add_bool(doc, fe, "whole_file", true);
+ const char *dash = rows[i].detail ? strchr(rows[i].detail, '-') : NULL;
+ yyjson_mut_obj_add_int(doc, fe, "lines", dash ? atoi(dash + 1) : 0);
+ yyjson_mut_arr_add_val(pu_files, fe);
+ }
+ pu_n++;
} else if (strcmp(kind, "not_indexed_dir") == 0) {
if (ni_dir_n < sample_limit) {
yyjson_mut_arr_add_strcpy(doc, ni_dirs, rows[i].rel_path);
@@ -6099,6 +6113,14 @@ static void add_coverage_report(yyjson_mut_doc *doc, yyjson_mut_val *root, cbm_s
}
yyjson_mut_obj_add_val(doc, root, "parse_partial", pp);
+ /* Indexed, but the parse failed across nearly the whole file, so naming
+ * line ranges helps nobody — read the source instead. */
+ yyjson_mut_val *pu = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_val(doc, pu, "files", pu_files);
+ yyjson_mut_obj_add_int(doc, pu, "count", pu_n);
+ yyjson_mut_obj_add_bool(doc, pu, "truncated", pu_n > COVERAGE_FILE_CAP);
+ yyjson_mut_obj_add_val(doc, root, "parse_unusable", pu);
+
yyjson_mut_val *sk = yyjson_mut_obj(doc);
yyjson_mut_obj_add_val(doc, sk, "files", sk_files);
yyjson_mut_obj_add_int(doc, sk, "count", sk_n);
@@ -6153,7 +6175,8 @@ enum {
COVERAGE_SCOPE_MAX = 32,
COVERAGE_SCOPE_DEFAULT_LIMIT = 200,
COVERAGE_SCOPE_MAX_LIMIT = 1000,
- COVERAGE_RANGE_MAX = 128,
+ COVERAGE_RANGE_MAX = 256, /* matches CBM_MAX_ERROR_REGIONS — a lower value here
+ would just move the silent clip downstream */
};
bool cbm_path_within_root(const char *root_path, const char *abs_path); /* defined below */
@@ -6265,6 +6288,14 @@ static const char *coverage_path_freshness(cbm_store_t *store, const char *proje
return matches ? "metadata_match" : "metadata_changed";
}
+/* Read an "start-end,start-end,...[,+<N>]" string into a JSON ranges array.
+ *
+ * The optional trailing "+<N>" says the producer's own cap threw N ranges away.
+ * Without reading it, a clipped list arrives here looking complete: the loop
+ * below stops at the '+' with no error and no leftover, so the row would claim
+ * a short, tidy set of ranges that is in fact missing entries. Set
+ * "ranges_truncated": true whenever ranges were lost — either by that marker,
+ * or by COVERAGE_RANGE_MAX stopping this loop. */
static bool coverage_add_ranges(yyjson_mut_doc *doc, yyjson_mut_val *row, const char *detail) {
if (!detail || !detail[0]) {
return false;
@@ -6272,10 +6303,15 @@ static bool coverage_add_ranges(yyjson_mut_doc *doc, yyjson_mut_val *row, const
yyjson_mut_val *ranges = yyjson_mut_arr(doc);
const char *p = detail;
int emitted = 0;
+ bool truncated = false;
while (*p && emitted < COVERAGE_RANGE_MAX) {
while (*p == ' ' || *p == ',') {
p++;
}
+ if (*p == '+') {
+ truncated = true; /* the producer's cap dropped ranges we never saw */
+ break;
+ }
if (!isdigit((unsigned char)*p)) {
break;
}
@@ -6307,11 +6343,17 @@ static bool coverage_add_ranges(yyjson_mut_doc *doc, yyjson_mut_val *row, const
break;
}
}
+ if (emitted >= COVERAGE_RANGE_MAX && *p) {
+ truncated = true; /* our own limit stopped the loop with input left over */
+ }
if (emitted > 0) {
yyjson_mut_obj_add_val(doc, row, "ranges", ranges);
- if (emitted == COVERAGE_RANGE_MAX && *p) {
- yyjson_mut_obj_add_bool(doc, row, "ranges_truncated", true);
- }
+ }
+ /* Losing ranges to the producer's cap and losing them to this reader's cap
+ * are the same fact for a caller: the list is short and what is missing is
+ * unknown. One flag says so, whether or not any range survived. */
+ if (truncated) {
+ yyjson_mut_obj_add_bool(doc, row, "ranges_truncated", true);
}
return emitted > 0;
}
@@ -6328,7 +6370,8 @@ static void coverage_add_row_json(yyjson_mut_doc *doc, yyjson_mut_val *array,
row->rel_path && strcmp(row->rel_path, requested_path) == 0 ? "exact" : "ancestor");
}
bool ranges_added = false;
- if (row->kind && strcmp(row->kind, "parse_partial") == 0) {
+ if (row->kind && (strcmp(row->kind, "parse_partial") == 0 ||
+ strcmp(row->kind, "parse_unusable") == 0)) {
ranges_added = coverage_add_ranges(doc, item, row->detail);
}
/* A parse_partial detail such as "3-4,9" is byte-for-byte redundant once
@@ -6361,6 +6404,12 @@ static const char *coverage_status(const cbm_coverage_row_t *rows, int count,
continue;
}
const char *kind = rows[i].kind ? rows[i].kind : "";
+ /* "parse_unusable" must be named here. Without its own case it
+ * falls through to the catch-all below and reports "skipped",
+ * which is wrong in the way that matters: the file WAS indexed. */
+ if (pass == 0 && strcmp(kind, "parse_unusable") == 0) {
+ return "unusable";
+ }
if (pass == 0 && strcmp(kind, "parse_partial") == 0) {
return "partial";
}
@@ -6388,6 +6437,11 @@ static const char *coverage_recommended_action(const char *status, const char *f
if (strcmp(status, "partial") == 0) {
return "read_ranges_and_verify_scope";
}
+ if (strcmp(status, "unusable") == 0) {
+ /* The ranges cover nearly the whole file, so sending a reader to them
+ * is the same as sending them to the file. Say the useful thing. */
+ return "read_source_directly";
+ }
if (strcmp(status, "skipped") == 0) {
return "read_source_directly";
}
@@ -9967,6 +10021,19 @@ static bool is_parse_partial(const cbm_file_error_t *e) {
return e->phase && strcmp(e->phase, "parse_partial") == 0;
}
+/* The same, for the whole-file variant: one range covers 80% or more of the
+ * file, so listing the lines is useless advice. Also indexed, also not a skip. */
+static bool is_parse_unusable(const cbm_file_error_t *e) {
+ return e->phase && strcmp(e->phase, "parse_unusable") == 0;
+}
+
+/* Either coverage phase. Both mean the file WAS indexed, so both must stay out
+ * of skipped[] — a reader who sees a file there believes it is absent from the
+ * graph entirely. */
+static bool is_parse_coverage(const cbm_file_error_t *e) {
+ return is_parse_partial(e) || is_parse_unusable(e);
+}
+
/* Attach a summary of per-file skips (Stage 2 / Track B). Always emits a
* top-level "skipped_count" (0 on clean runs) so consumers can rely on it.
* When there are skips, also emits:
@@ -9974,13 +10041,13 @@ static bool is_parse_partial(const cbm_file_error_t *e) {
* and, if a per-run logfile was written, "logfile": "<path>".
* The run status stays "indexed" — a skipped file is the expected handled
* outcome, not a failure. errs[] is borrowed (copied into doc) and may contain
- * parse_partial entries, which are filtered out here (reported separately by
- * add_parse_partial_summary). */
+ * parse_partial and parse_unusable entries, which are filtered out here (both
+ * reported separately by add_parse_partial_summary). */
static void add_skipped_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
const cbm_file_error_t *errs, int count, const char *logfile) {
int skips = 0;
for (int i = 0; i < count; i++) {
- if (!is_parse_partial(&errs[i])) {
+ if (!is_parse_coverage(&errs[i])) {
skips++;
}
}
@@ -9995,7 +10062,7 @@ static void add_skipped_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
yyjson_mut_val *files = yyjson_mut_arr(doc);
int shown = 0;
for (int i = 0; i < count && shown < INDEX_SKIPPED_FILE_CAP; i++) {
- if (is_parse_partial(&errs[i])) {
+ if (is_parse_coverage(&errs[i])) {
continue;
}
yyjson_mut_val *fe = yyjson_mut_obj(doc);
@@ -10054,6 +10121,53 @@ static void add_parse_partial_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
yyjson_mut_obj_add_val(doc, root, "parse_partial", pp);
}
+/* Attach the whole-file half of the coverage summary. Always emits a top-level
+ * "parse_unusable_count" (0 on clean runs) so the CI coverage gate can read it
+ * without parsing anything else. When files were flagged:
+ * "parse_unusable": {"files":[{path,lines,whole_file}..(<=50)], "count":N,
+ * "truncated":bool, "note":"..."}
+ *
+ * These files WERE indexed, exactly like parse_partial ones. The difference is
+ * that their range covers 80% or more of the file, so the range is not worth
+ * printing — "lines" gives the size and "whole_file" says plainly that reading
+ * the ranges is the same as reading the file. */
+static void add_parse_unusable_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
+ const cbm_file_error_t *errs, int count) {
+ int unusable = 0;
+ for (int i = 0; i < count; i++) {
+ if (is_parse_unusable(&errs[i])) {
+ unusable++;
+ }
+ }
+ yyjson_mut_obj_add_int(doc, root, "parse_unusable_count", unusable);
+ if (!errs || unusable <= 0) {
+ return;
+ }
+ yyjson_mut_val *pu = yyjson_mut_obj(doc);
+ yyjson_mut_val *files = yyjson_mut_arr(doc);
+ int shown = 0;
+ for (int i = 0; i < count && shown < INDEX_SKIPPED_FILE_CAP; i++) {
+ if (!is_parse_unusable(&errs[i])) {
+ continue;
+ }
+ yyjson_mut_val *fe = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_strcpy(doc, fe, "path", errs[i].path ? errs[i].path : "");
+ yyjson_mut_obj_add_bool(doc, fe, "whole_file", true);
+ /* The range string is "start-end"; its end line is the file length. */
+ const char *dash = errs[i].reason ? strchr(errs[i].reason, '-') : NULL;
+ yyjson_mut_obj_add_int(doc, fe, "lines", dash ? atoi(dash + 1) : 0);
+ yyjson_mut_arr_add_val(files, fe);
+ shown++;
+ }
+ yyjson_mut_obj_add_val(doc, pu, "files", files);
+ yyjson_mut_obj_add_int(doc, pu, "count", unusable);
+ yyjson_mut_obj_add_bool(doc, pu, "truncated", unusable > INDEX_SKIPPED_FILE_CAP);
+ yyjson_mut_obj_add_str(doc, pu, "note",
+ "Indexed, but the parse failed across nearly the whole file, so line "
+ "ranges are not useful here — read the source directly.");
+ yyjson_mut_obj_add_val(doc, root, "parse_unusable", pu);
+}
+
/* The pipeline persists the complete current coverage set before this
* response is built. Prefer that set over the per-run errors so incremental
* runs that do not revisit a flagged file, and artifact bootstraps, do not
@@ -10097,6 +10211,7 @@ static bool add_persisted_failure_summaries(yyjson_mut_doc *doc, yyjson_mut_val
add_skipped_summary(doc, root, failures, failure_count, logfile);
add_parse_partial_summary(doc, root, failures, failure_count);
+ add_parse_unusable_summary(doc, root, failures, failure_count);
free(failures);
cbm_store_free_coverage(rows, row_count);
return true;
@@ -10174,6 +10289,7 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
if (!store || !add_persisted_failure_summaries(doc, root, store, project_name, logfile)) {
add_skipped_summary(doc, root, file_errors, file_error_count, logfile);
add_parse_partial_summary(doc, root, file_errors, file_error_count);
+ add_parse_unusable_summary(doc, root, file_errors, file_error_count);
}
int nodes = 0;
int edges = 0;
@@ -11335,10 +11451,10 @@ static void add_string_array(yyjson_mut_doc *doc, yyjson_mut_val *obj, const cha
}
/* get_code_snippet coverage note (#963): if the resolved node's file is
- * flagged parse_partial, warn that the graph may under-report this file.
- * Correlated by construction — the result names its file. (An entirely-
- * skipped file cannot appear here: it has no nodes to resolve a snippet
- * from.) */
+ * flagged parse_partial or parse_unusable, warn that the graph may
+ * under-report this file. Correlated by construction — the result names its
+ * file. (An entirely-skipped file cannot appear here: it has no nodes to
+ * resolve a snippet from.) */
static void add_snippet_coverage_note(yyjson_mut_doc *doc, yyjson_mut_val *root_obj,
cbm_store_t *store, const cbm_node_t *node) {
if (!node->file_path || !node->file_path[0] || !node->project) {
@@ -11351,18 +11467,28 @@ static void add_snippet_coverage_note(yyjson_mut_doc *doc, yyjson_mut_val *root_
return;
}
for (int i = 0; i < count; i++) {
- if (rows[i].rel_path && strcmp(rows[i].rel_path, node->file_path) == 0 && rows[i].kind &&
- strcmp(rows[i].kind, "parse_partial") == 0) {
- char note[CBM_SZ_1K];
+ if (!rows[i].rel_path || strcmp(rows[i].rel_path, node->file_path) != 0 || !rows[i].kind) {
+ continue;
+ }
+ char note[CBM_SZ_1K];
+ if (strcmp(rows[i].kind, "parse_unusable") == 0) {
+ snprintf(note, sizeof(note),
+ "The parse of this file failed across nearly the whole of it, so most "
+ "constructs are missing from the graph and naming line ranges would not "
+ "help. Read the source directly — the source above is ground truth. "
+ "(best-effort signal)");
+ } else if (strcmp(rows[i].kind, "parse_partial") == 0) {
snprintf(note, sizeof(note),
"This file was only PARTIALLY indexed — line range(s) %s could not be "
"parsed, so constructs there may be missing from the graph (callers/callees "
"and search results can under-report this file). The source above is ground "
"truth. (best-effort signal)",
rows[i].detail && rows[i].detail[0] ? rows[i].detail : "?");
- yyjson_mut_obj_add_strcpy(doc, root_obj, "coverage_note", note);
- break;
+ } else {
+ continue;
}
+ yyjson_mut_obj_add_strcpy(doc, root_obj, "coverage_note", note);
+ break;
}
cbm_store_free_coverage(rows, count);
}
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index 7ac98e9cd..b798cda5e 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -18,6 +18,7 @@ enum { PD_RING = 4, PD_RING_MASK = 3, PD_JSON_MARGIN = 10, PD_ESC_MARGIN = 3, PD
enum { PD_JSON_FIELD_OVERHEAD = 6 };
#include "pipeline/pipeline.h"
#include <stdint.h>
+#include <ctype.h>
#include "pipeline/pipeline_internal.h"
#include "graph_buffer/graph_buffer.h"
#include "foundation/log.h"
@@ -544,23 +545,79 @@ static bool objectscript_export_append_secondary_arrays(CBMFileResult *aggregate
/* Preserve every generated class's parse diagnostics. The generated UDL
* snippets all map back to one physical Studio Export file, so their compact
* range lists can be concatenated using the ordinary comma separator. */
+/* Read the trailing ",+<N>" truncation marker off a range string. Returns the
+ * number of dropped ranges the marker reports, or 0 when there is no marker,
+ * and writes the length of the part before the marker to `body_len`. */
+static int objectscript_export_split_range_marker(const char *ranges, size_t *body_len) {
+ size_t len = ranges ? strlen(ranges) : 0;
+ *body_len = len;
+ if (len == 0) {
+ return 0;
+ }
+ size_t i = len;
+ while (i > 0 && isdigit((unsigned char)ranges[i - 1])) {
+ i--;
+ }
+ if (i == len || i == 0 || ranges[i - 1] != '+') {
+ return 0;
+ }
+ size_t marker = i - 1; /* index of '+' */
+ if (marker > 0 && ranges[marker - 1] == ',') {
+ marker--; /* drop the separator too */
+ }
+ *body_len = marker;
+ return atoi(ranges + i);
+}
+
+/* Join one Studio Export part's ranges onto the aggregate.
+ *
+ * One export file can hold several <Class> elements, each parsed separately,
+ * so their range strings get concatenated. A ",+<N>" truncation marker must
+ * end up ONCE, at the very end: every reader stops at the first token that is
+ * not a range, so a marker left in the middle would silently hide every range
+ * after it. Strip the marker off both sides, join the plain ranges, then add
+ * one marker back carrying the summed count. */
static bool objectscript_export_append_error_ranges(CBMFileResult *aggregate,
const CBMFileResult *part) {
aggregate->parse_incomplete = aggregate->parse_incomplete || part->parse_incomplete;
+ aggregate->parse_unusable = aggregate->parse_unusable || part->parse_unusable;
aggregate->error_region_count += part->error_region_count;
if (!part->error_ranges || !part->error_ranges[0]) {
return true;
}
+
+ size_t agg_len = 0;
+ size_t part_len = 0;
+ int dropped = 0;
+ const char *agg_body = aggregate->error_ranges;
+ if (agg_body && agg_body[0]) {
+ dropped += objectscript_export_split_range_marker(agg_body, &agg_len);
+ } else {
+ agg_body = NULL;
+ }
+ dropped += objectscript_export_split_range_marker(part->error_ranges, &part_len);
+
const char *combined = NULL;
- if (aggregate->error_ranges && aggregate->error_ranges[0]) {
- combined = cbm_arena_sprintf(&aggregate->arena, "%s,%s", aggregate->error_ranges,
- part->error_ranges);
+ if (agg_body && agg_len > 0 && part_len > 0) {
+ combined = cbm_arena_sprintf(&aggregate->arena, "%.*s,%.*s", (int)agg_len, agg_body,
+ (int)part_len, part->error_ranges);
+ } else if (agg_body && agg_len > 0) {
+ combined = cbm_arena_sprintf(&aggregate->arena, "%.*s", (int)agg_len, agg_body);
+ } else if (part_len > 0) {
+ combined = cbm_arena_sprintf(&aggregate->arena, "%.*s", (int)part_len, part->error_ranges);
} else {
- combined = cbm_arena_strdup(&aggregate->arena, part->error_ranges);
+ combined = cbm_arena_strdup(&aggregate->arena, "");
}
if (!combined) {
return false;
}
+ if (dropped > 0) {
+ combined = cbm_arena_sprintf(&aggregate->arena, "%s%s+%d", combined,
+ combined[0] ? "," : "", dropped);
+ if (!combined) {
+ return false;
+ }
+ }
aggregate->error_ranges = combined;
return true;
}
@@ -787,7 +844,8 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
* ERROR/MISSING regions — see pass_parallel.c (keep in sync). */
cbm_pipeline_add_file_error(ctx->pipeline, rel,
result->error_ranges ? result->error_ranges : "unknown",
- "parse_partial");
+ result->parse_unusable ? "parse_unusable"
+ : "parse_partial");
}
/* Create nodes for each definition */
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index cdeafe9d8..9f572b2b7 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -889,11 +889,12 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
} else if (result->parse_incomplete) {
/* Best-effort parse-coverage signal (#963): the file WAS indexed,
* but its tree contains ERROR/MISSING regions whose constructs are
- * silently absent from the graph. Not a skip — recorded under the
- * distinct "parse_partial" phase (reason = the line-range list) so
- * the MCP layer reports it separately from skipped[]. */
+ * silently absent from the graph. Neither phase is a skip — both
+ * are recorded separately from skipped[] by the MCP layer.
+ * "parse_unusable" means one range covers so much of the file that
+ * naming the lines helps nobody; see parse_unusable in cbm.h. */
pp_err_add(errs, fi->rel_path, result->error_ranges ? result->error_ranges : "unknown",
- "parse_partial");
+ result->parse_unusable ? "parse_unusable" : "parse_partial");
}
/* Create definition nodes in local gbuf */
diff --git a/src/store/store.c b/src/store/store.c
index c36f00b44..abf0ed779 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -301,9 +301,25 @@ static int init_schema(cbm_store_t *s) {
" PRIMARY KEY (project, rel_path)"
");"
/* Best-effort indexing-coverage signal (#963). One row per file the
- * indexer could not fully cover: kind "parse_partial" (indexed, but the
- * parse tree had ERROR/MISSING regions — detail = 1-based line ranges)
- * or a skip phase ("read"/"extract"/"oversized" — detail = reason).
+ * indexer could not fully cover. `kind` says which of three things
+ * happened, and `detail` means something different in each:
+ *
+ * "parse_partial" the file WAS indexed, but the parse tree had
+ * ERROR/MISSING regions. detail = 1-based line
+ * ranges, "start-end,start-end", with an optional
+ * trailing "+<N>" saying N more ranges were dropped
+ * by the producer's cap. Read those lines.
+ * "parse_unusable" the file WAS indexed, but one range covers 80% or
+ * more of it, so naming the lines is useless advice.
+ * detail = the same range string. Read the source.
+ * a skip phase the file was NOT indexed at all: "read",
+ * "extract" or "oversized". detail = the reason.
+ *
+ * The first two are easy to confuse with the third, and the difference
+ * matters to a reader: a skipped file is absent from the graph, while
+ * the other two are present but incomplete. Name a new kind so that
+ * distinction stays obvious — "parse_failed" would read as a skip.
+ *
* Deliberately SEPARATE from the graph tables: coverage is metadata
* about the graph, not part of it. */
"CREATE TABLE IF NOT EXISTS index_coverage ("
diff --git a/tests/test_parse_coverage.c b/tests/test_parse_coverage.c
index 367353fd8..f9ada5717 100644
--- a/tests/test_parse_coverage.c
+++ b/tests/test_parse_coverage.c
@@ -33,6 +33,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
/* Convenience extract wrapper (same shape as test_extraction_imports.c). */
static CBMFileResult *do_extract(const char *src, CBMLanguage lang, const char *path) {
@@ -237,12 +238,22 @@ TEST(py_clean_file_not_flagged) {
PASS();
}
+/* Read the trailing "+<N>" truncation marker off a range string. Returns N, or
+ * 0 when the string carries no marker. */
+static int ranges_dropped_marker(const char *ranges) {
+ const char *plus = ranges ? strrchr(ranges, '+') : NULL;
+ if (!plus || !isdigit((unsigned char)plus[1])) {
+ return 0;
+ }
+ return atoi(plus + 1);
+}
+
TEST(error_region_cap_is_honored) {
/* Pathological input: many separate unrecoverable garbage blocks
* interleaved with valid defs. The collector must stay bounded by its
- * 64-region cap (matches CBM_MAX_ERROR_REGIONS in cbm.c) — pathological
+ * 256-region cap (matches CBM_MAX_ERROR_REGIONS in cbm.c) — pathological
* input can't blow up the report, and the flag itself still fires. */
- enum { GARBAGE_BLOCKS = 200, LINE_CAP = 64 };
+ enum { GARBAGE_BLOCKS = 400, LINE_CAP = 256 };
char *src = (char *)malloc(GARBAGE_BLOCKS * 96 + 1);
ASSERT_NOT_NULL(src);
size_t off = 0;
@@ -261,6 +272,55 @@ TEST(error_region_cap_is_honored) {
PASS();
}
+/* A clipped range list must say so. 400 garbage blocks overrun the 256-region
+ * cap, so the report keeps 256 ranges and ends with a "+<N>" marker naming the
+ * number thrown away. Without the marker the short list reads as a complete
+ * one, which is the whole defect this guards. */
+TEST(error_region_cap_reports_what_it_dropped) {
+ enum { GARBAGE_BLOCKS = 400, LINE_CAP = 256 };
+ char *src = (char *)malloc(GARBAGE_BLOCKS * 96 + 1);
+ ASSERT_NOT_NULL(src);
+ size_t off = 0;
+ for (int i = 0; i < GARBAGE_BLOCKS; i++) {
+ off += (size_t)snprintf(
+ src + off, 96, "def ok%d():\n return %d\n%%%%%% garbage%d ((( %%%%%%\n", i, i, i);
+ }
+ CBMFileResult *r = do_extract(src, CBM_LANG_PYTHON, "cap_marker.py");
+ free(src);
+ ASSERT_NOT_NULL(r);
+ ASSERT_NOT_NULL(r->error_ranges);
+ /* The cap bound, so the kept list is full and the marker is present. */
+ ASSERT_EQ(r->error_region_count, LINE_CAP);
+ int dropped = ranges_dropped_marker(r->error_ranges);
+ ASSERT_GTE(dropped, 1);
+ /* Every block produces at most one region, so the total cannot exceed the
+ * number of blocks — a marker that overcounts would fail here. */
+ ASSERT_LTE(r->error_region_count + dropped, GARBAGE_BLOCKS);
+ /* The marker is a SUFFIX: nothing follows it, or a reader stops early and
+ * silently loses every range after it. */
+ const char *plus = strrchr(r->error_ranges, '+');
+ ASSERT_NOT_NULL(plus);
+ for (const char *c = plus + 1; *c; c++) {
+ ASSERT_TRUE(isdigit((unsigned char)*c));
+ }
+ cbm_free_result(r);
+ PASS();
+}
+
+/* Inverse guard: a file that stays under the cap must carry NO marker, or
+ * every ordinary report would look clipped. */
+TEST(uncapped_ranges_carry_no_marker) {
+ const char *src = "def ok():\n return 1\n%%% garbage (((\ndef ok2():\n return 2\n";
+ CBMFileResult *r = do_extract(src, CBM_LANG_PYTHON, "small.py");
+ ASSERT_NOT_NULL(r);
+ ASSERT_TRUE(r->parse_incomplete);
+ ASSERT_NOT_NULL(r->error_ranges);
+ ASSERT_EQ(ranges_dropped_marker(r->error_ranges), 0);
+ ASSERT_NULL(strchr(r->error_ranges, '+'));
+ cbm_free_result(r);
+ PASS();
+}
+
/* Trailing recovered functions AFTER the failed #ifdef region must not
* unflag it: recovery evidence must originate INSIDE the region, and the
* unrecovered lines (the first branch's `guarded`) keep it flagged. */
@@ -675,6 +735,62 @@ TEST(c_clean_file_stays_unflagged_after_refinement) {
}
+/* The whole-file class, and the reason the parse_unusable kind exists.
+ *
+ * The Phase 2 refinement that narrows a whole-file range using the
+ * preprocessed parse only runs for C, C++ and CUDA. A Python file whose root
+ * node is ERROR gets no such help, so it still reports one range covering
+ * every line — and one range over 80% of a file is not advice worth printing. */
+TEST(python_whole_file_error_is_unusable) {
+ const char *src = ")))\n((( \n]]] [[[\ndef x(:\n";
+ CBMFileResult *r = do_extract(src, CBM_LANG_PYTHON, "unparseable.py");
+ ASSERT_NOT_NULL(r);
+ ASSERT_TRUE(r->parse_incomplete);
+ ASSERT_TRUE(r->parse_unusable);
+ ASSERT_EQ(r->error_region_count, 1);
+ ASSERT_NOT_NULL(r->error_ranges);
+ cbm_free_result(r);
+ PASS();
+}
+
+/* Inverse guard, and the one that keeps the kind meaningful: a file with a
+ * real but LOCAL parse failure must stay parse_partial. If this flipped, every
+ * flagged file would say "read the source" and the ranges would stop earning
+ * their keep. */
+TEST(local_error_stays_partial_not_unusable) {
+ const char *src = "def ok():\n return 1\n%%% garbage (((\ndef ok2():\n return 2\n"
+ "def ok3():\n return 3\ndef ok4():\n return 4\n"
+ "def ok5():\n return 5\ndef ok6():\n return 6\n";
+ CBMFileResult *r = do_extract(src, CBM_LANG_PYTHON, "local_error.py");
+ ASSERT_NOT_NULL(r);
+ ASSERT_TRUE(r->parse_incomplete);
+ ASSERT_FALSE(r->parse_unusable);
+ cbm_free_result(r);
+ PASS();
+}
+
+/* A clean file is neither. */
+TEST(clean_file_is_neither_partial_nor_unusable) {
+ CBMFileResult *r = do_extract(C_CLEAN, CBM_LANG_C, "clean_kinds.c");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->parse_incomplete);
+ ASSERT_FALSE(r->parse_unusable);
+ cbm_free_result(r);
+ PASS();
+}
+
+/* The C file that started this work must NOT land in the unusable class. Its
+ * whole-file range is exactly what Phase 2 broke up, so if this ever flips
+ * back to true the refinement has stopped working. */
+TEST(c_ifdef_split_is_partial_never_unusable) {
+ CBMFileResult *r = do_extract(C_IFDEF_SPLIT, CBM_LANG_C, "split_kind.c");
+ ASSERT_NOT_NULL(r);
+ ASSERT_TRUE(r->parse_incomplete);
+ ASSERT_FALSE(r->parse_unusable);
+ cbm_free_result(r);
+ PASS();
+}
+
SUITE(parse_coverage) {
RUN_TEST(c_ifdef_split_brace_sets_parse_incomplete);
RUN_TEST(c_ifdef_split_brace_neighbors_still_extracted);
@@ -684,6 +800,12 @@ SUITE(parse_coverage) {
RUN_TEST(py_recovered_def_not_flagged);
RUN_TEST(py_clean_file_not_flagged);
RUN_TEST(error_region_cap_is_honored);
+ RUN_TEST(error_region_cap_reports_what_it_dropped);
+ RUN_TEST(uncapped_ranges_carry_no_marker);
+ RUN_TEST(python_whole_file_error_is_unusable);
+ RUN_TEST(local_error_stays_partial_not_unusable);
+ RUN_TEST(clean_file_is_neither_partial_nor_unusable);
+ RUN_TEST(c_ifdef_split_is_partial_never_unusable);
RUN_TEST(c_trailing_recovered_defs_keep_flag);
RUN_TEST(dockerfile_missing_final_newline_not_flagged_issue1610);
RUN_TEST(dockerfile_with_final_newline_still_clean_issue1610);