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.

doc-links(cs): the type names of a refused scope are quarantined, in every run (R4)

Martin Vogel5fd1a0718ecd
diff --git a/src/pipeline/doc_links.c b/src/pipeline/doc_links.c
index fa4b0096a..8ac85fcb5 100644
--- a/src/pipeline/doc_links.c
+++ b/src/pipeline/doc_links.c
@@ -113,7 +113,8 @@ int cbm_doclinks_scope_delta(const char *stored, const char *fresh, cbm_doclink_
return R->scope_delta(stored, fresh, removed, ud);
}
-const char *cbm_doclinks_storable_scope(const char *scope) {
+const char *cbm_doclinks_storable_scope(const char *scope, char **owned) {
+ *owned = NULL;
const cbm_doclink_resolver_t *R = resolver_of_scope(scope);
if (!R || !R->scope_accepted || !R->rejected_scope) {
return scope;
@@ -122,7 +123,11 @@ const char *cbm_doclinks_storable_scope(const char *scope) {
if (accepted < 0) {
return NULL;
}
- return accepted ? scope : R->rejected_scope;
+ if (accepted) {
+ return scope;
+ }
+ *owned = R->rejected_scope(scope);
+ return *owned;
}
/* ── Run state ───────────────────────────────────────────────────── */
diff --git a/src/pipeline/doc_links.h b/src/pipeline/doc_links.h
index b175ebd76..be99cd509 100644
--- a/src/pipeline/doc_links.h
+++ b/src/pipeline/doc_links.h
@@ -214,13 +214,14 @@ typedef struct {
void *ud);
/* Optional, together: whether the reader takes a scope blob written in
* this run (1), refuses it (0), or could not tell for memory (-1); and
- * what is stored instead of a refused blob. A run refuses the blob of its
- * own file and goes on without the file's declarations; the marker makes
- * a later run that reads the file's scope back do the same, where the
- * blob itself would be a stored scope the reader refuses, which fails
+ * the marker stored instead of a refused blob (a memory-core block of
+ * CBM_MEM_CLASS_OTHER, NULL when memory ran out). A run refuses the blob
+ * of its own file and goes on without the file's declarations; the marker
+ * makes a later run that reads the file's scope back do the same, where
+ * the blob itself would be a stored scope the reader refuses, which fails
* the run. */
int (*scope_accepted)(const char *scope);
- const char *rejected_scope;
+ char *(*rejected_scope)(const char *scope);
} cbm_doclink_resolver_t;
extern const cbm_doclink_resolver_t cbm_doclink_cs_resolver;
@@ -255,8 +256,10 @@ int cbm_doclinks_scope_delta(const char *stored, const char *fresh, cbm_doclink_
/* What is stored for a scope blob written in this run (the surface row's
* `dl`, lsp_surface.c): the blob itself, or its language's rejected-scope
- * marker when that language's reader refuses it (scope_accepted). NULL when
- * memory ran out deciding. */
-const char *cbm_doclinks_storable_scope(const char *scope);
+ * marker when that language's reader refuses it (scope_accepted), made into
+ * *owned (release with cbm_free(CBM_MEM_CLASS_OTHER, *owned); NULL when the
+ * blob itself is returned). NULL when memory ran out deciding or making the
+ * marker. */
+const char *cbm_doclinks_storable_scope(const char *scope, char **owned);
#endif /* CBM_PIPELINE_DOC_LINKS_H */
diff --git a/src/pipeline/doc_links_cs.c b/src/pipeline/doc_links_cs.c
index 62b429b8b..e39726646 100644
--- a/src/pipeline/doc_links_cs.c
+++ b/src/pipeline/doc_links_cs.c
@@ -5926,16 +5926,131 @@ static void undo_scope(cs_index_t *ix) {
u->nmarks = 0;
}
+/* Scope line fields (0-based, the tag is field 0; internal/cbm/doclink_cs.c):
+ * `U region kind alias target`, `T region start end kind outer name tparams
+ * bases`, `M start kind explicit type name tparams sig` and `Q name`. */
+enum {
+ CS_SCOPE_Q_NAME = 1,
+ CS_SCOPE_U_KIND = 2,
+ CS_SCOPE_M_NAME = 5,
+ CS_SCOPE_T_NAME = 6,
+ CS_SCOPE_T_BASES = 8
+};
+
+static const char *delta_field(const char *line, size_t len, int idx, size_t *flen) {
+ int f = 0;
+ size_t s = 0;
+ for (size_t i = 0; i <= len; i++) {
+ if (i == len || line[i] == '\t') {
+ if (f == idx) {
+ *flen = i - s;
+ return line + s;
+ }
+ f++;
+ s = i + SKIP_ONE;
+ }
+ }
+ *flen = 0;
+ return NULL;
+}
+
+/* True when s[0, n) is a name a reference can carry (ident_ok). */
+static bool cs_name_ok(const char *s, size_t n) {
+ char buf[CS_NAME_BUF];
+ if (n == 0 || n >= sizeof(buf)) {
+ return false;
+ }
+ memcpy(buf, s, n);
+ buf[n] = '\0';
+ return ident_ok(buf);
+}
+
+/* The next type name a T or Q record of a scope blob carries, read leniently
+ * (the blob is one the reader refused): a line is taken when its name field
+ * is there and is a name, however the rest of it reads. Moves *cursor past
+ * the lines it read; false at the end of the blob. */
+static bool cs_next_type_name(const char **cursor, const char **name, size_t *len) {
+ while (**cursor) {
+ const char *line = *cursor;
+ const char *nl = strchr(line, '\n');
+ size_t n = nl ? (size_t)(nl - line) : strlen(line);
+ *cursor = line + n + (nl ? SKIP_ONE : 0);
+ int idx = line[0] == 'T' ? CS_SCOPE_T_NAME : line[0] == 'Q' ? CS_SCOPE_Q_NAME : CS_NONE;
+ *name = idx > 0 && n > SKIP_ONE && line[SKIP_ONE] == '\t' ? delta_field(line, n, idx, len)
+ : NULL;
+ if (*name && cs_name_ok(*name, *len)) {
+ return true;
+ }
+ }
+ return false;
+}
+
/* What is stored, in place of its scope blob, for a file whose scope the
* reader refused in the run that wrote it (cs_scope_accepted, lsp_surface.c):
- * the tag and one `!` record. A later run that reads it back treats the file
- * as that run did -- it declares nothing, its references are graph gaps --
- * where the refused blob itself would fail that run as a stored scope this
- * reader does not take. */
+ * the tag, one `!` record, and one Q record per type name the T and Q records
+ * of the refused blob carry (cs_next_type_name). A later run that reads it
+ * back treats the file as that run did -- it declares nothing, those names
+ * are quarantined, its references are graph gaps -- where the refused blob
+ * itself would fail that run as a stored scope this reader does not take. */
static const char CS_REJECTED_SCOPE[] = CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n";
+/* The marker, followed by nothing but Q records of names. */
static bool cs_scope_marked_rejected(const char *scope) {
- return scope && strcmp(scope, CS_REJECTED_SCOPE) == 0;
+ size_t n = sizeof(CS_REJECTED_SCOPE) - SKIP_ONE;
+ if (!scope || strncmp(scope, CS_REJECTED_SCOPE, n) != 0) {
+ return false;
+ }
+ for (const char *line = scope + n; *line;) {
+ const char *nl = strchr(line, '\n');
+ if (!nl || line[0] != 'Q' || line[SKIP_ONE] != '\t' ||
+ !cs_name_ok(line + PAIR_LEN, (size_t)(nl - line) - PAIR_LEN)) {
+ return false;
+ }
+ line = nl + SKIP_ONE;
+ }
+ return true;
+}
+
+/* The marker stored for the refused blob `scope` (the resolver's
+ * rejected_scope): a memory-core block, NULL when memory ran out. A Q record
+ * is never longer than the line its name is read from plus a newline. */
+static char *cs_rejected_scope(const char *scope) {
+ size_t w = sizeof(CS_REJECTED_SCOPE) - SKIP_ONE;
+ char *out = (char *)cbm_alloc(CBM_MEM_CLASS_OTHER, w + strlen(scope) + PAIR_LEN);
+ if (!out) {
+ return NULL;
+ }
+ memcpy(out, CS_REJECTED_SCOPE, w);
+ const char *cursor = scope;
+ const char *name = NULL;
+ size_t len = 0;
+ while (cs_next_type_name(&cursor, &name, &len)) {
+ out[w++] = 'Q';
+ out[w++] = '\t';
+ memcpy(out + w, name, len);
+ w += len;
+ out[w++] = '\n';
+ }
+ out[w] = '\0';
+ return out;
+}
+
+/* Quarantine the type names a refused scope blob, or its stored marker,
+ * carries: what the file declares is unknown, so a reference to one of them
+ * from any file is a graph gap. false when memory ran out. */
+static bool quarantine_refused(cs_index_t *ix, const cs_file_t *f, const char *scope) {
+ const char *cursor = scope;
+ const char *name = NULL;
+ size_t len = 0;
+ while (cs_next_type_name(&cursor, &name, &len)) {
+ char buf[CS_NAME_BUF];
+ memcpy(buf, name, len); /* shorter than the buffer: cs_name_ok */
+ buf[len] = '\0';
+ if (!quarantine_name(ix, f, buf)) {
+ return false;
+ }
+ }
+ return true;
}
/* Whether this reader takes a scope blob written in this run: its record
@@ -5989,7 +6104,10 @@ bool cbm_doclink_cs_test_scope_parses(const char *scope) {
* that is not known), and its path is returned. A scope written in this run
* that the reader refuses is the scanner's and this reader's disagreement,
* which costs only its own file: what its parse set is taken back, the file
- * declares nothing, and its references are graph gaps (counted, logged).
+ * declares nothing, and its references are graph gaps (counted, logged). The
+ * names of its types are quarantined: what it declares is unknown, so a
+ * reference to one of them from any file is a graph gap too. A scope stored
+ * as the rejected marker is read as in the run that refused it.
* Returns "" when memory ran out, NULL when all is well. */
static const char *build_files(cs_index_t *ix, const cbm_doclink_build_in_t *in) {
CBMHashTable *dir_unit = cbm_ht_create(CBM_SZ_1K);
@@ -6018,6 +6136,10 @@ static const char *build_files(cs_index_t *ix, const cbm_doclink_build_in_t *in)
.is_test = f->is_test,
.rejected = true};
ix->rejected++;
+ if (!quarantine_refused(ix, f, scope)) {
+ bad = ""; /* memory ran out */
+ break;
+ }
scope = NULL;
}
if (!scope) {
@@ -6252,28 +6374,6 @@ static void cs_resolve(const void *index, void *state, int run_file, const CBMDo
/* ── Incremental scope rules ─────────────────────────────────────── */
-/* Scope line fields (0-based, the tag is field 0; internal/cbm/doclink_cs.c):
- * `U region kind alias target`, `T region start end kind outer name tparams
- * bases` and `M start kind explicit type name tparams sig`. */
-enum { CS_SCOPE_U_KIND = 2, CS_SCOPE_M_NAME = 5, CS_SCOPE_T_BASES = 8 };
-
-static const char *delta_field(const char *line, size_t len, int idx, size_t *flen) {
- int f = 0;
- size_t s = 0;
- for (size_t i = 0; i <= len; i++) {
- if (i == len || line[i] == '\t') {
- if (f == idx) {
- *flen = i - s;
- return line + s;
- }
- f++;
- s = i + SKIP_ONE;
- }
- }
- *flen = 0;
- return NULL;
-}
-
/* A using directive only its own file sees: one that is not `global`. */
static bool delta_local_using(const char *line, size_t len) {
if (line[0] != 'U') {
@@ -6409,5 +6509,5 @@ const cbm_doclink_resolver_t cbm_doclink_cs_resolver = {
.resolve = cs_resolve,
.scope_delta = cs_scope_delta,
.scope_accepted = cs_scope_accepted,
- .rejected_scope = CS_REJECTED_SCOPE,
+ .rejected_scope = cs_rejected_scope,
};
diff --git a/src/pipeline/lsp_surface.c b/src/pipeline/lsp_surface.c
index 0d281f05d..c96d48ad9 100644
--- a/src/pipeline/lsp_surface.c
+++ b/src/pipeline/lsp_surface.c
@@ -165,8 +165,10 @@ static char *surface_file_to_json(const CBMFileResult *result, const CBMLSPDef *
/* a scope this run's reader refuses is stored as its language's
* rejected marker: a later run then treats the file as this one does
* (doc_links.h, scope_accepted) */
- const char *kept = cbm_doclinks_storable_scope(result->doc_scope);
+ char *marker = NULL;
+ const char *kept = cbm_doclinks_storable_scope(result->doc_scope, &marker);
char *portable = kept ? cbm_doclink_portable_scope(kept) : NULL;
+ cbm_free(CBM_MEM_CLASS_OTHER, marker);
if (!portable) {
yyjson_mut_doc_free(doc);
return NULL; /* a missing scope would diverge silently: fail the row */
diff --git a/tests/test_doc_mentions.c b/tests/test_doc_mentions.c
index 55f252cc8..43f253e53 100644
--- a/tests/test_doc_mentions.c
+++ b/tests/test_doc_mentions.c
@@ -10041,8 +10041,13 @@ TEST(doc_mentions_cs_scanner_output_reads_back) {
* file. The status stays ok and the other file's edges are there; the file's
* own reference is a graph gap; and nothing its parse set before the refusal
* stays -- a namespace it declares does not stand beside a type of that name
- * (Shadow, Good.Lurker), and a name it quarantined does not block another
- * file's declaration (Hidden2). */
+ * (Shadow, Good.Lurker).
+ * R4: the type names its T and Q records carry are quarantined, as the names
+ * of declarations no scope is known for are: a reference to one, from any
+ * file, is a graph gap -- never a binding to another type of that name the
+ * full picture would not choose (Widget: Good.Widget of Bad.cs comes before
+ * the imported Lib.Widget), nor to one the unplaced declaration would make
+ * uncertain (Hidden2, as when the scope is taken). */
/* S8: Bad.cs declares what would shadow and hide Healthy.cs's names; its
* scope is spoiled where it is written (cbm_doclink_cs_test_spoil_scope). */
static const dm_source_t dm_rejected_files[] = {
@@ -10056,12 +10061,14 @@ static const dm_source_t dm_rejected_files[] = {
"\n"
" /// <summary><see cref=\"Target\"/></summary>\n"
" public class FromBad { }\n"
+ " public class Widget { }\n"
"}\n"
"namespace .Broken\n"
"{\n"
" public class Hidden2 { }\n"
"}\n"},
- {"Healthy.cs", "public class Shadow { }\n"
+ {"Healthy.cs", "using Lib;\n"
+ "public class Shadow { }\n"
"namespace Good\n"
"{\n"
" public class Target { }\n"
@@ -10069,16 +10076,22 @@ static const dm_source_t dm_rejected_files[] = {
" public class Hidden2 { }\n"
"\n"
" /// <summary><see cref=\"Target\"/> <see cref=\"Shadow\"/>\n"
- " /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/></summary>\n"
+ " /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/>\n"
+ " /// <see cref=\"Widget\"/></summary>\n"
" public class Uses { }\n"
"}\n"},
+ {"Lib.cs", "namespace Lib\n"
+ "{\n"
+ " public class Widget { }\n"
+ "}\n"},
};
static const dm_want_t dm_rejected_wants[] = {
{"Healthy.cs", "Target", "Healthy.Uses", "Healthy.Target", NULL, NULL, NULL},
{"Healthy.cs", "Shadow", "Healthy.Uses", "Healthy.Shadow", NULL, NULL, NULL},
{"Healthy.cs", "Lurker", "Healthy.Uses", "Healthy.Lurker", NULL, NULL, NULL},
- {"Healthy.cs", "Hidden2", "Healthy.Uses", "Healthy.Hidden2", NULL, NULL, NULL},
+ {"Healthy.cs", "Hidden2", "Healthy.Uses", NULL, "graph_gap", "Healthy.Hidden2", NULL},
+ {"Healthy.cs", "Widget", "Healthy.Uses", NULL, "graph_gap", "Lib.Widget", NULL},
{"Bad.cs", "Target", "Bad.FromBad", NULL, "graph_gap", "Healthy.Target", NULL},
};
@@ -10151,7 +10164,21 @@ TEST(doc_mentions_cs_rejected_scope_across_runs) {
int indexed = dm_index(repo, db, NULL);
char stored[256];
dm_stored_scope(db, "Bad.cs", stored, sizeof(stored));
- bool marked = strcmp(stored, CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n") == 0;
+ /* the marker, then one Q record per type name of the refused blob (R4) */
+ static const char *const names[] = {"Inner", "Deep", "FromBad", "Widget", "Hidden2"};
+ static const char marker[] = CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n";
+ bool marked = strncmp(stored, marker, sizeof(marker) - 1) == 0;
+ size_t want_len = sizeof(marker) - 1;
+ for (size_t k = 0; k < sizeof(names) / sizeof(names[0]); k++) {
+ char q[64];
+ snprintf(q, sizeof(q), "\nQ\t%s\n", names[k]);
+ marked = marked && strstr(stored, q) != NULL;
+ want_len += strlen(q) - 1;
+ }
+ if (!marked || strlen(stored) != want_len) {
+ printf(" stored for Bad.cs: %s\n", stored);
+ marked = false;
+ }
char edited[2048];
snprintf(edited, sizeof(edited), "%s\n// body-only edit\n", dm_rejected_files[1].text);
th_write_file(TH_PATH(repo, "Healthy.cs"), edited);