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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
doc-links(cs): a scope its run refused is stored as a rejected marker (S8 across runs)
Martin Vogelaba240b34dd5
diff --git a/internal/cbm/doclink.h b/internal/cbm/doclink.hindex bb3ffa46f..3588db1b0 100644--- a/internal/cbm/doclink.h+++ b/internal/cbm/doclink.h@@ -157,6 +157,12 @@ void cbm_doclink_test_fail_alloc_after(int kind, int nth);bool cbm_doclink_test_fail_alloc(int kind);void cbm_doclink_test_reset_alloc(void);+/* The C# scope scan of the file at `rel_path` writes, after its records, one+ * the resolver refuses -- as if writer and reader disagreed (NULL or "":+ * none). What is stored for the file and what this run resolves both carry+ * it. */+void cbm_doclink_cs_test_spoil_scope(const char *rel_path);+/* Actual comment memcpy bytes, input bytes submitted to the C# lexical parser,* and bytes allocated for cleaned reference values; separate from token output. */void cbm_doclink_test_doc_work_reset(void);diff --git a/internal/cbm/doclink_cs.c b/internal/cbm/doclink_cs.cindex 23db08f5a..db09f6182 100644--- a/internal/cbm/doclink_cs.c+++ b/internal/cbm/doclink_cs.c@@ -3027,6 +3027,14 @@ char *cbm_doclink_cs_portable_scope(const char *scope) {return out;}+#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS+static char cs_test_spoiled_path[CBM_SZ_512];++void cbm_doclink_cs_test_spoil_scope(const char *rel_path) {+ snprintf(cs_test_spoiled_path, sizeof(cs_test_spoiled_path), "%s", rel_path ? rel_path : "");+}+#endif+const char *cbm_doclink_cs_scan_scope(CBMExtractCtx *ctx) {if (ts_node_is_null(ctx->root)) {return NULL;@@ -3056,6 +3064,10 @@ const char *cbm_doclink_cs_scan_scope(CBMExtractCtx *ctx) {if (cbm_doclink_test_fail_alloc(CBM_DOCLINK_ALLOC_SCOPE)) {s.failed = true;}+ if (cs_test_spoiled_path[0] && ctx->rel_path &&+ strcmp(ctx->rel_path, cs_test_spoiled_path) == 0) {+ sb_puts(&s.sb, "Z\tspoiled\n");+ }#endifif (s.failed || s.sb.failed || !s.sb.buf) {/* memory ran out: no scope would read as a file that declaresdiff --git a/src/pipeline/doc_links.c b/src/pipeline/doc_links.cindex fd2b32a32..fa4b0096a 100644--- a/src/pipeline/doc_links.c+++ b/src/pipeline/doc_links.c@@ -113,6 +113,18 @@ 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 cbm_doclink_resolver_t *R = resolver_of_scope(scope);+ if (!R || !R->scope_accepted || !R->rejected_scope) {+ return scope;+ }+ int accepted = R->scope_accepted(scope);+ if (accepted < 0) {+ return NULL;+ }+ return accepted ? scope : R->rejected_scope;+}+/* ── Run state ───────────────────────────────────────────────────── */typedef struct {diff --git a/src/pipeline/doc_links.h b/src/pipeline/doc_links.hindex 2f08f5c22..b175ebd76 100644--- a/src/pipeline/doc_links.h+++ b/src/pipeline/doc_links.h@@ -212,6 +212,15 @@ typedef struct {bool (*scope_input)(const char *rel_path);int (*scope_delta)(const char *stored, const char *fresh, cbm_doclink_name_fn removed,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 run. */+ int (*scope_accepted)(const char *scope);+ const char *rejected_scope;} cbm_doclink_resolver_t;extern const cbm_doclink_resolver_t cbm_doclink_cs_resolver;@@ -229,10 +238,8 @@ uint64_t cbm_doclink_cs_test_scratch_work(void);/* Test seam (doc_links.c): the nth MENTIONS edge insert from now on fails as* if memory ran out (0: none). */void cbm_doclinks_test_fail_edge_insert_after(int nth);-/* Test seams (doc_links_cs.c): spoil the scope of the file at `rel_path`- * (every record, then one the reader refuses; NULL or "": none), and ask the- * reader whether it takes a scope blob. */-void cbm_doclink_cs_test_spoil_scope(const char *rel_path);+/* Test seam (doc_links_cs.c): ask the reader whether it takes a scope blob.+ * (The scope of one file is spoiled where it is written: doclink.h.) */bool cbm_doclink_cs_test_scope_parses(const char *scope);#endif@@ -246,4 +253,10 @@ bool cbm_doclinks_is_scope_input(const char *rel_path);int cbm_doclinks_scope_delta(const char *stored, const char *fresh, cbm_doclink_name_fn removed,void *ud);+/* 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);+#endif /* CBM_PIPELINE_DOC_LINKS_H */diff --git a/src/pipeline/doc_links_cs.c b/src/pipeline/doc_links_cs.cindex 70057b376..0995c134c 100644--- a/src/pipeline/doc_links_cs.c+++ b/src/pipeline/doc_links_cs.c@@ -5925,45 +5925,44 @@ static void undo_scope(cs_index_t *ix) {u->nmarks = 0;}-#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS-/* Test seam: the scope of the file at this path is spoiled -- every record- * it has, then one this reader refuses -- as if its writer and this reader- * disagreed after the file's declarations were read. "" or NULL: none. */-static char cs_test_spoiled_path[CBM_SZ_512];--void cbm_doclink_cs_test_spoil_scope(const char *rel_path) {- snprintf(cs_test_spoiled_path, sizeof(cs_test_spoiled_path), "%s", rel_path ? rel_path : "");-}--static const char *cs_test_spoiled(cs_index_t *ix, const char *rel_path, const char *scope) {- if (!scope || !cs_test_spoiled_path[0] || strcmp(rel_path, cs_test_spoiled_path) != 0) {- return scope;+/* 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. */+static const char CS_REJECTED_SCOPE[] = CBM_DOCLINK_CS_SCOPE_TAG "\n!\trejected\n";++static bool cs_scope_marked_rejected(const char *scope) {+ return scope && strcmp(scope, CS_REJECTED_SCOPE) == 0;+}++/* Whether this reader takes a scope blob written in this run: its record+ * checks, on an index of the blob's own (what makes a blob refused depends on+ * the blob alone). 1 taken, 0 refused, -1 memory ran out. A project file's+ * blob goes to the MSBuild evaluator, which takes every blob. */+static int cs_scope_accepted(const char *scope) {+ if (!scope || cbm_msb_is_project_scope(scope) || cs_scope_marked_rejected(scope)) {+ return SKIP_ONE;}- const char *spoiled = cbm_arena_sprintf(&ix->arena, "%sZ\tspoiled\n", scope);- return spoiled ? spoiled : scope;-}--/* Test seam: true when this reader takes the scope blob `scope` (its record- * checks, on an index of its own). A test holds every blob the scanner- * writes against it. */-bool cbm_doclink_cs_test_scope_parses(const char *scope) {cs_index_t *ix = (cs_index_t *)cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*ix));if (!ix) {- return false;- }- cbm_arena_init(&ix->arena);- ix->ns_by_key = cbm_ht_create(CBM_SZ_64);- ix->quarantine = cbm_ht_create(CBM_SZ_64);- ix->quarantine_test = cbm_ht_create(CBM_SZ_64);- ix->nss = (cs_ns_t *)ix_zalloc(ix, CBM_SZ_256 * sizeof(cs_ns_t));- bool ok = ix->ns_by_key && ix->quarantine && ix->quarantine_test && ix->nss;- if (ok) {- ix->nscap = CBM_SZ_256;+ return CBM_NOT_FOUND;+ }+ cbm_arena_init_lazy(&ix->arena, CBM_ARENA_APPEND_BLOCK);+ ix->ns_by_key = cbm_ht_create(CBM_SZ_16);+ ix->quarantine = cbm_ht_create(CBM_SZ_16);+ ix->quarantine_test = cbm_ht_create(CBM_SZ_16);+ ix->nss = (cs_ns_t *)ix_zalloc(ix, CBM_SZ_16 * sizeof(cs_ns_t));+ int accepted = CBM_NOT_FOUND;+ if (ix->ns_by_key && ix->quarantine && ix->quarantine_test && ix->nss) {+ ix->nscap = CBM_SZ_16;ix->nnss = SKIP_ONE;ix->nss[0] = (cs_ns_t){.parent = CS_NONE, .name = ""};cs_file_t f = {.rel_path = "Scope.cs"};undo_begin(ix);- ok = parse_scope(ix, &f, scope) && !ix->oom;+ bool parsed = parse_scope(ix, &f, scope);+ accepted = ix->oom ? CBM_NOT_FOUND : (parsed ? SKIP_ONE : 0);}cbm_ht_free(ix->ns_by_key);cbm_ht_free(ix->quarantine);@@ -5972,7 +5971,14 @@ bool cbm_doclink_cs_test_scope_parses(const char *scope) {cbm_free(CBM_MEM_CLASS_OTHER, ix->undo.marks);cbm_arena_destroy(&ix->arena);cbm_free(CBM_MEM_CLASS_OTHER, ix);- return ok;+ return accepted;+}++#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS+/* Test seam: true when this reader takes the scope blob `scope`. A test holds+ * every blob the scanner writes against it. */+bool cbm_doclink_cs_test_scope_parses(const char *scope) {+ return cs_scope_accepted(scope) == SKIP_ONE;}#endif@@ -5996,13 +6002,12 @@ static const char *build_files(cs_index_t *ix, const cbm_doclink_build_in_t *in)f->is_test = cs_is_test_path(src->rel_path);/* a project file declares nothing: its blob went to the evaluator */const char *scope = cbm_msb_is_project_scope(src->scope) ? NULL : src->scope;-#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS- scope = cs_test_spoiled(ix, src->rel_path, scope);-#endif+ /* a scope its own run refused, stored as the marker: as in that run */+ bool marked = cs_scope_marked_rejected(scope);undo_begin(ix);- if (scope && !parse_scope(ix, f, scope)) {+ if (marked || (scope && !parse_scope(ix, f, scope))) {bool fresh = src->run_file >= 0 && src->run_file < in->run_file_count;- if (ix->oom || !fresh) {+ if (!marked && (ix->oom || !fresh)) {bad = ix->oom ? "" : src->rel_path;break;}@@ -6354,7 +6359,9 @@ static bool delta_has_bases(const char *scope) {* nobody's scope. */static int cs_scope_delta(const char *stored, const char *fresh, cbm_doclink_name_fn removed,void *ud) {- if (cbm_msb_is_project_scope(stored) || cbm_msb_is_project_scope(fresh)) {+ /* a rejected file declares nothing: unchanged while it stays rejected */+ if (cbm_msb_is_project_scope(stored) || cbm_msb_is_project_scope(fresh) ||+ cs_scope_marked_rejected(stored) || cs_scope_marked_rejected(fresh)) {return strcmp(stored, fresh) == 0 ? CBM_DOCLINK_DELTA_LOCAL : CBM_DOCLINK_DELTA_GLOBAL;}if (!delta_same_usings(stored, fresh) && (delta_has_bases(stored) || delta_has_bases(fresh))) {@@ -6400,4 +6407,6 @@ const cbm_doclink_resolver_t cbm_doclink_cs_resolver = {.file_end = cs_file_end,.resolve = cs_resolve,.scope_delta = cs_scope_delta,+ .scope_accepted = cs_scope_accepted,+ .rejected_scope = CS_REJECTED_SCOPE,};diff --git a/src/pipeline/lsp_surface.c b/src/pipeline/lsp_surface.cindex 5acd61b8b..0d281f05d 100644--- a/src/pipeline/lsp_surface.c+++ b/src/pipeline/lsp_surface.c@@ -28,6 +28,7 @@#include "foundation/log.h"#include "foundation/mem_core.h"#include "foundation/sha256.h"+#include "pipeline/doc_links.h" /* cbm_doclinks_storable_scope */#include "pipeline/worker_pool.h"#include "yyjson/yyjson.h"@@ -161,7 +162,11 @@ static char *surface_file_to_json(const CBMFileResult *result, const CBMLSPDef ** The defs decoder ignores this key; cbm_doclinks_scopes_from_surfaces* reads it back for the files an incremental run does not re-extract. */if (result && result->doc_scope) {- char *portable = cbm_doclink_portable_scope(result->doc_scope);+ /* 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 *portable = kept ? cbm_doclink_portable_scope(kept) : NULL;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.cindex 99c4090eb..6e9fe9bf5 100644--- a/tests/test_doc_mentions.c+++ b/tests/test_doc_mentions.c@@ -9962,47 +9962,51 @@ TEST(doc_mentions_cs_scanner_output_reads_back) {* 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). */-TEST(doc_mentions_cs_rejected_scope_contained) {- static const dm_source_t files[] = {- {"Bad.cs", "namespace Shadow\n"- "{\n"- " public class Inner { }\n"- "}\n"+/* 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[] = {+ {"Bad.cs", "namespace Shadow\n"+ "{\n"+ " public class Inner { }\n"+ "}\n"+ "namespace Good\n"+ "{\n"+ " namespace Lurker { public class Deep { } }\n"+ "\n"+ " /// <summary><see cref=\"Target\"/></summary>\n"+ " public class FromBad { }\n"+ "}\n"+ "namespace .Broken\n"+ "{\n"+ " public class Hidden2 { }\n"+ "}\n"},+ {"Healthy.cs", "public class Shadow { }\n""namespace Good\n""{\n"- " namespace Lurker { public class Deep { } }\n"- "\n"- " /// <summary><see cref=\"Target\"/></summary>\n"- " public class FromBad { }\n"- "}\n"- "namespace .Broken\n"- "{\n"+ " public class Target { }\n"+ " public class Lurker { }\n"" public class Hidden2 { }\n"+ "\n"+ " /// <summary><see cref=\"Target\"/> <see cref=\"Shadow\"/>\n"+ " /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/></summary>\n"+ " public class Uses { }\n""}\n"},- {"Healthy.cs", "public class Shadow { }\n"- "namespace Good\n"- "{\n"- " public class Target { }\n"- " public class Lurker { }\n"- " public class Hidden2 { }\n"- "\n"- " /// <summary><see cref=\"Target\"/> <see cref=\"Shadow\"/>\n"- " /// <see cref=\"Lurker\"/> <see cref=\"Hidden2\"/></summary>\n"- " public class Uses { }\n"- "}\n"},- };- static const dm_want_t 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},- {"Bad.cs", "Target", "Bad.FromBad", NULL, "graph_gap", "Healthy.Target", NULL},- };+};++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},+ {"Bad.cs", "Target", "Bad.FromBad", NULL, "graph_gap", "Healthy.Target", NULL},+};++TEST(doc_mentions_cs_rejected_scope_contained) {char tmp[256];snprintf(tmp, sizeof(tmp), "/tmp/cbm_dm_rejected_XXXXXX");ASSERT_NOT_NULL(cbm_mkdtemp(tmp));- for (int i = 0; i < DM_COUNT(files); i++) {- th_write_file(TH_PATH(tmp, files[i].path), files[i].text);+ for (int i = 0; i < DM_COUNT(dm_rejected_files); i++) {+ th_write_file(TH_PATH(tmp, dm_rejected_files[i].path), dm_rejected_files[i].text);}char db[512];snprintf(db, sizeof(db), "%s/rejected.db", tmp);@@ -10012,11 +10016,83 @@ TEST(doc_mentions_cs_rejected_scope_contained) {int errors = bad == 0 ? dm_count(db, "SELECT COUNT(*) FROM doc_link_unresolved ""WHERE reason = 'error'"): -1;- for (int i = 0; bad >= 0 && i < DM_COUNT(wants); i++) {- bad += dm_want_failed(db, &wants[i]);+ for (int i = 0; bad >= 0 && i < DM_COUNT(dm_rejected_wants); i++) {+ bad += dm_want_failed(db, &dm_rejected_wants[i]);+ }+ dm_unlink_db(db);+ th_rmtree(tmp);+ ASSERT_EQ(errors, 0);+ ASSERT_EQ(bad, 0);+ PASS();+}++/* The scope blob stored for `rel_path` (the surface row's `dl`), into out;+ * "" when there is none. */+static void dm_stored_scope(const char *db, const char *rel_path, char *out, size_t cap) {+ out[0] = '\0';+ sqlite3 *h = NULL;+ if (sqlite3_open_v2(db, &h, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK) {+ sqlite3_stmt *st = NULL;+ if (sqlite3_prepare_v2(h,+ "SELECT json_extract(defs_json, '$.dl') FROM lsp_surface "+ "WHERE rel_path = ?1",+ -1, &st, NULL) == SQLITE_OK) {+ sqlite3_bind_text(st, 1, rel_path, -1, SQLITE_TRANSIENT);+ if (sqlite3_step(st) == SQLITE_ROW && sqlite3_column_text(st, 0)) {+ snprintf(out, cap, "%s", (const char *)sqlite3_column_text(st, 0));+ }+ }+ sqlite3_finalize(st);+ }+ sqlite3_close(h);+}++/* S8 across runs: the scope its own run refused is stored as the rejected+ * marker, so a later incremental run that reads it back treats the file as+ * the full run does -- it declares nothing, its references are graph gaps --+ * instead of failing the layer over a stored scope the reader refuses. A+ * body-only edit elsewhere reads the marker back; one in the rejected file+ * compares its marker with the fresh one. Each step equals a full run. */+TEST(doc_mentions_cs_rejected_scope_across_runs) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_dm_rejected_runs_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ char repo[400];+ char db[512];+ char full_db[512];+ snprintf(repo, sizeof(repo), "%s/repo", tmp);+ snprintf(db, sizeof(db), "%s/inc.db", tmp);+ snprintf(full_db, sizeof(full_db), "%s/full.db", tmp);+ for (int i = 0; i < DM_COUNT(dm_rejected_files); i++) {+ th_write_file(TH_PATH(repo, dm_rejected_files[i].path), dm_rejected_files[i].text);+ }+ cbm_doclink_cs_test_spoil_scope("Bad.cs");+ 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;+ 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);+ int read_back = dm_step(repo, db, full_db, "the rejected scope read back",+ CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);+ snprintf(edited, sizeof(edited), "%s\n// body-only edit\n", dm_rejected_files[0].text);+ th_write_file(TH_PATH(repo, "Bad.cs"), edited);+ int again = dm_step(repo, db, full_db, "the rejected file edited",+ CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);+ cbm_doclink_cs_test_spoil_scope(NULL);+ int errors = dm_count(db, "SELECT COUNT(*) FROM doc_link_unresolved WHERE reason = 'error'");+ int bad = 0;+ for (int i = 0; i < DM_COUNT(dm_rejected_wants); i++) {+ bad += dm_want_failed(db, &dm_rejected_wants[i]);}dm_unlink_db(db);+ dm_unlink_db(full_db);th_rmtree(tmp);+ ASSERT_EQ(indexed, 0);+ ASSERT_TRUE(marked);+ ASSERT_EQ(read_back, 0);+ ASSERT_EQ(again, 0);ASSERT_EQ(errors, 0);ASSERT_EQ(bad, 0);PASS();@@ -10162,4 +10238,5 @@ SUITE(doc_mentions) {RUN_TEST(doc_mentions_alloc_failure_status);RUN_TEST(doc_mentions_cs_scanner_output_reads_back);RUN_TEST(doc_mentions_cs_rejected_scope_contained);+ RUN_TEST(doc_mentions_cs_rejected_scope_across_runs);}