Skip to content

Commit 57b93bf

Browse files
committed
fix(incremental): re-link inbound edges across an arity change
Arity is now part of a node's identity, so adding or removing a parameter renames the node. An edit that changed both a function's body and its signature dropped every inbound cross-file edge into it until each caller's file happened to be re-parsed. A full reindex and an incremental reindex of the same tree disagreed. incr_restore_inbound_edges keeps exact QN as its primary key, as it always was. When that misses and the captured target carries a fence, a node whose QN differs only by its fence is the same function, so the edge follows it. Ambiguity (several arities of the same name in the same container) keeps the old behaviour and drops the edge, because nothing says which arity the caller meant. The lookup goes through an index. Restoring N captured edges against a graph of M nodes scanned all M for every fenced miss, so the pass cost the product of the two largest quantities in an incremental reindex. One walk builds the table and each miss after it is a hash lookup. The walk is deferred to the first fenced miss, so a corpus with no arity fences (every language but Elixir today) builds nothing and pays nothing. An allocation failure mid-build marks the index unusable rather than partial. A partial index would answer "sole match" for a name whose rival arity never got inserted. Measured on a two-file Elixir fixture: caller calls `landing/1`, then the file declaring `landing` is edited to `landing/2` and reindexed incrementally. Before this commit the CALLS edge is gone from the incremental store and present in a full reindex of the same final tree; after, both stores bind the same `...landing#2`. With two surviving arities the incremental store drops the edge and so does the full reindex. The test there asserts that the two stores agree, not that the edge survives. tests/test_pipeline.c: pipeline_incremental_preserves_edges_across_arity_change and pipeline_incremental_ambiguous_arity_successor_matches_full_index. Both compare the target's qualified_name rather than its bare name, because cross_file_edge_exists matches nodes by name and every arity of one function looks alike to it. Depends on #N+6 (module/arity identity) and #N+9 (resolution), which make the pre-edit edge exist to be captured. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent 117b91d commit 57b93bf

2 files changed

Lines changed: 322 additions & 1 deletion

File tree

‎src/pipeline/pipeline_incremental.c‎

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,19 +988,153 @@ static void incr_capture_inbound_edge(const cbm_gbuf_edge_t *edge, void *userdat
988988
cap->count++;
989989
}
990990

991+
/* Strip a QN's '#<digits>' arity fence into `out`. Local to this TU for the
992+
* same reason registry.c keeps its own copy: the pipeline does not pull in the
993+
* extraction header. */
994+
static void incr_strip_arity(char *out, size_t cap, const char *qn) {
995+
size_t len = qn ? strlen(qn) : 0;
996+
const char *hash = qn ? strrchr(qn, '#') : NULL;
997+
if (hash && hash[SKIP_ONE]) {
998+
bool digits = true;
999+
for (const char *p = hash + SKIP_ONE; *p; p++) {
1000+
if (*p < '0' || *p > '9') {
1001+
digits = false;
1002+
break;
1003+
}
1004+
}
1005+
if (digits) {
1006+
len = (size_t)(hash - qn);
1007+
}
1008+
}
1009+
if (len > cap - SKIP_ONE) {
1010+
len = cap - SKIP_ONE;
1011+
}
1012+
if (len > 0) {
1013+
memcpy(out, qn, len);
1014+
}
1015+
out[len] = '\0';
1016+
}
1017+
1018+
/* Arity-stripped QN -> the fenced node(s) that strip to it. `count` is kept,
1019+
* not just the node, so several arities of one name still decline to guess.
1020+
*
1021+
* This is an index because the lookup it serves is a per-edge one: restoring N
1022+
* captured edges against a graph of M nodes scanned all M for every fenced miss,
1023+
* so the pass cost the product of the two largest quantities in an incremental
1024+
* reindex. One walk builds the table; each miss after it is a hash lookup. The
1025+
* walk is deferred to the first fenced miss, so a language with no arity fences
1026+
* -- every language but Elixir today -- builds nothing. */
1027+
typedef struct {
1028+
char *key; /* heap copy; the table borrows keys, it does not own them */
1029+
const cbm_gbuf_node_t *node;
1030+
int count;
1031+
} incr_arity_slot_t;
1032+
1033+
typedef struct {
1034+
CBMHashTable *by_stripped;
1035+
bool built;
1036+
bool ok; /* an allocation failed mid-build: the index is partial, so it is
1037+
* not consulted at all -- a partial index would answer "sole
1038+
* match" for a name whose rival arity never got inserted. */
1039+
} incr_arity_index_t;
1040+
1041+
static void incr_arity_index_visit(const cbm_gbuf_node_t *node, void *userdata) {
1042+
incr_arity_index_t *idx = (incr_arity_index_t *)userdata;
1043+
if (!idx->ok || !node->qualified_name || !strchr(node->qualified_name, '#')) {
1044+
return;
1045+
}
1046+
char stripped[CBM_SZ_512];
1047+
incr_strip_arity(stripped, sizeof(stripped), node->qualified_name);
1048+
incr_arity_slot_t *slot = cbm_ht_get(idx->by_stripped, stripped);
1049+
if (slot) {
1050+
slot->count++;
1051+
return;
1052+
}
1053+
slot = calloc(CBM_ALLOC_ONE, sizeof(*slot));
1054+
char *key = strdup(stripped);
1055+
if (!slot || !key) {
1056+
free(slot);
1057+
free(key);
1058+
idx->ok = false;
1059+
cbm_log_warn("incremental.arity_index_oom", "qn", node->qualified_name);
1060+
return;
1061+
}
1062+
slot->key = key;
1063+
slot->node = node;
1064+
slot->count = CBM_COUNT_ONE;
1065+
cbm_ht_set(idx->by_stripped, key, slot);
1066+
}
1067+
1068+
static void incr_arity_index_free_visitor(const char *key, void *value, void *userdata) {
1069+
(void)key;
1070+
(void)userdata;
1071+
incr_arity_slot_t *slot = (incr_arity_slot_t *)value;
1072+
free(slot->key);
1073+
free(slot);
1074+
}
1075+
1076+
static void incr_arity_index_build(incr_arity_index_t *idx, const cbm_gbuf_t *gbuf) {
1077+
idx->built = true;
1078+
idx->by_stripped = cbm_ht_create((uint32_t)cbm_gbuf_node_count(gbuf));
1079+
idx->ok = idx->by_stripped != NULL;
1080+
if (!idx->ok) {
1081+
return;
1082+
}
1083+
cbm_gbuf_foreach_node(gbuf, incr_arity_index_visit, idx);
1084+
}
1085+
1086+
static void incr_arity_index_free(incr_arity_index_t *idx) {
1087+
if (!idx->by_stripped) {
1088+
return;
1089+
}
1090+
cbm_ht_foreach(idx->by_stripped, incr_arity_index_free_visitor, NULL);
1091+
cbm_ht_free(idx->by_stripped);
1092+
idx->by_stripped = NULL;
1093+
}
1094+
1095+
/* The sole node whose QN differs from `fenced_qn` only by its arity fence, or
1096+
* NULL when there is none or more than one. */
1097+
static const cbm_gbuf_node_t *incr_arity_sole_match(incr_arity_index_t *idx, const cbm_gbuf_t *gbuf,
1098+
const char *fenced_qn) {
1099+
if (!idx->built) {
1100+
incr_arity_index_build(idx, gbuf);
1101+
}
1102+
if (!idx->ok) {
1103+
return NULL;
1104+
}
1105+
char want[CBM_SZ_512];
1106+
incr_strip_arity(want, sizeof(want), fenced_qn);
1107+
const incr_arity_slot_t *slot = cbm_ht_get(idx->by_stripped, want);
1108+
return (slot && slot->count == CBM_COUNT_ONE) ? slot->node : NULL;
1109+
}
1110+
9911111
/* Re-link snapshotted inbound edges to the freshly re-created target nodes.
992-
* Returns the number of edges re-linked. */
1112+
* Returns the number of edges re-linked.
1113+
*
1114+
* Exact QN is the primary key, as it always was. The secondary key exists
1115+
* because arity is now part of identity for some languages: adding or removing
1116+
* a parameter RENAMES the node, so a body-and-signature edit would otherwise
1117+
* drop every inbound cross-file edge into that function until each caller's
1118+
* file happened to be re-parsed -- a full-vs-incremental divergence created by
1119+
* the identity change itself. A unique node that differs only in its fence is
1120+
* the same function, so the edge follows it. Ambiguity (several arities) keeps
1121+
* the old behaviour and drops the edge rather than guessing. */
9931122
static int incr_restore_inbound_edges(cbm_gbuf_t *gbuf, cbm_edge_capture_t *cap) {
9941123
int restored = 0;
1124+
incr_arity_index_t idx = {NULL, false, false};
9951125
for (int i = 0; i < cap->count; i++) {
9961126
cbm_saved_edge_t *s = &cap->items[i];
9971127
const cbm_gbuf_node_t *src = cbm_gbuf_find_by_qn(gbuf, s->source_qn);
9981128
const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(gbuf, s->target_qn);
1129+
if (src && !tgt && strchr(s->target_qn, '#')) {
1130+
tgt = incr_arity_sole_match(&idx, gbuf, s->target_qn);
1131+
}
9991132
if (src && tgt) {
10001133
cbm_gbuf_insert_edge(gbuf, src->id, tgt->id, s->type, s->props);
10011134
restored++;
10021135
}
10031136
}
1137+
incr_arity_index_free(&idx);
10041138
return restored;
10051139
}
10061140

‎tests/test_pipeline.c‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,44 @@ static bool cross_file_edge_exists(cbm_store_t *s, const char *project, const ch
10171017
return found;
10181018
}
10191019

1020+
/* Copy the qualified_name of the node a named CALLS edge lands on, or NULL.
1021+
* Caller frees. Used to compare WHICH arity an edge bound to, which
1022+
* cross_file_edge_exists cannot see: it matches nodes by bare name, so every
1023+
* arity of one function looks alike to it. */
1024+
static char *cross_file_call_target_qn(cbm_store_t *s, const char *project, const char *src_name,
1025+
const char *tgt_name) {
1026+
cbm_node_t *srcs = NULL;
1027+
cbm_node_t *tgts = NULL;
1028+
int sc = 0;
1029+
int tc = 0;
1030+
cbm_store_find_nodes_by_name(s, project, src_name, &srcs, &sc);
1031+
cbm_store_find_nodes_by_name(s, project, tgt_name, &tgts, &tc);
1032+
char *qn = NULL;
1033+
for (int i = 0; i < sc && !qn; i++) {
1034+
cbm_edge_t *edges = NULL;
1035+
int ec = 0;
1036+
cbm_store_find_edges_by_source_type(s, srcs[i].id, "CALLS", &edges, &ec);
1037+
for (int j = 0; j < ec && !qn; j++) {
1038+
for (int k = 0; k < tc; k++) {
1039+
if (edges[j].target_id == tgts[k].id && tgts[k].qualified_name) {
1040+
qn = strdup(tgts[k].qualified_name);
1041+
break;
1042+
}
1043+
}
1044+
}
1045+
if (edges) {
1046+
cbm_store_free_edges(edges, ec);
1047+
}
1048+
}
1049+
if (srcs) {
1050+
cbm_store_free_nodes(srcs, sc);
1051+
}
1052+
if (tgts) {
1053+
cbm_store_free_nodes(tgts, tc);
1054+
}
1055+
return qn;
1056+
}
1057+
10201058
static bool cross_file_call_exists(cbm_store_t *s, const char *project, const char *src_name,
10211059
const char *tgt_name) {
10221060
return cross_file_edge_exists(s, project, src_name, tgt_name, "CALLS");
@@ -15107,6 +15145,153 @@ TEST(pipeline_objectscript_export_range_join_keeps_one_trailing_marker) {
1510715145
}
1510815146
#endif
1510915147

15148+
/* Arity is part of node identity for a language that overloads by it, so
15149+
* changing a function's signature RENAMES its node. The incremental pass
15150+
* snapshots inbound cross-file edges by the endpoint's exact qualified_name and
15151+
* drops any whose target QN no longer exists, so without a secondary key an
15152+
* ordinary edit -- add a parameter -- would silently drop every inbound edge
15153+
* into that function until each caller's file happened to be re-parsed. That is
15154+
* a full-vs-incremental divergence created by the identity change itself. */
15155+
TEST(pipeline_incremental_preserves_edges_across_arity_change) {
15156+
char *repo = th_mktempdir("cbm_arity_incr");
15157+
ASSERT_NOT_NULL(repo);
15158+
char tmpdir[512];
15159+
snprintf(tmpdir, sizeof(tmpdir), "%s", repo);
15160+
15161+
ASSERT_EQ(th_write_file(TH_PATH(tmpdir, "lib/target.ex"),
15162+
"defmodule Fx.Target do\n"
15163+
" def landing(a), do: a\n"
15164+
"end\n"),
15165+
0);
15166+
ASSERT_EQ(th_write_file(TH_PATH(tmpdir, "lib/caller.ex"),
15167+
"defmodule Fx.Caller do\n"
15168+
" def invoke(x), do: Fx.Target.landing(x)\n"
15169+
"end\n"),
15170+
0);
15171+
15172+
char db_path[600];
15173+
snprintf(db_path, sizeof(db_path), "%s/arity_incr.db", tmpdir);
15174+
15175+
cbm_pipeline_t *p1 = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
15176+
ASSERT_NOT_NULL(p1);
15177+
ASSERT_EQ(cbm_pipeline_run(p1), 0);
15178+
const char *project1 = cbm_pipeline_project_name(p1);
15179+
cbm_store_t *s1 = cbm_store_open_path(db_path);
15180+
ASSERT_NOT_NULL(s1);
15181+
ASSERT_TRUE(cross_file_edge_exists(s1, project1, "invoke", "landing", "CALLS"));
15182+
cbm_store_close(s1);
15183+
cbm_pipeline_free(p1);
15184+
15185+
/* Add a parameter: landing/1 becomes landing/2, a different node. Only the
15186+
* TARGET file changes, so caller.ex is never re-parsed. */
15187+
ASSERT_EQ(th_write_file(TH_PATH(tmpdir, "lib/target.ex"),
15188+
"defmodule Fx.Target do\n"
15189+
" def landing(a, b), do: {a, b}\n"
15190+
"end\n"),
15191+
0);
15192+
15193+
cbm_pipeline_t *p2 = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
15194+
ASSERT_NOT_NULL(p2);
15195+
ASSERT_EQ(cbm_pipeline_run(p2), 0);
15196+
const char *project2 = cbm_pipeline_project_name(p2);
15197+
cbm_store_t *s2 = cbm_store_open_path(db_path);
15198+
ASSERT_NOT_NULL(s2);
15199+
ASSERT_TRUE(cross_file_edge_exists(s2, project2, "invoke", "landing", "CALLS"));
15200+
cbm_store_close(s2);
15201+
cbm_pipeline_free(p2);
15202+
PASS();
15203+
}
15204+
/* The edge-restore secondary key exists to stop an arity edit from making the
15205+
* incremental graph differ from the full one. This pins that equality for the
15206+
* case the key itself declines: landing/1 becomes landing/2 AND landing/3, so
15207+
* no surviving arity is the unique successor of the captured edge and the
15208+
* restore drops it rather than guessing. What lands in the graph is then
15209+
* whatever the ordinary resolve produces -- and it must be what a from-scratch
15210+
* full index of the same tree produces, down to which arity the edge binds.
15211+
*
15212+
* MEASURED, not assumed: an incremental run over this change and a full index
15213+
* of the same post-change tree both bind invoke#1 -> landing#2. Asserting the
15214+
* edge is ABSENT here would be wrong; the restore declining is not the same as
15215+
* the edge being gone.
15216+
*
15217+
* This is a guard, not a red-green: it holds on both sides of the index that
15218+
* replaced the per-edge full node scan in incr_restore_inbound_edges. That is
15219+
* why it is here -- an index that recorded only "some node has this stripped
15220+
* name", and not how many do, would answer "sole match", restore the edge onto
15221+
* whichever arity it happened to store, and diverge from the full index. */
15222+
TEST(pipeline_incremental_ambiguous_arity_successor_matches_full_index) {
15223+
const char *before = "defmodule Fx.Target do\n"
15224+
" def landing(a), do: a\n"
15225+
"end\n";
15226+
const char *after = "defmodule Fx.Target do\n"
15227+
" def landing(a, b), do: {a, b}\n"
15228+
" def landing(a, b, c), do: {a, b, c}\n"
15229+
"end\n";
15230+
const char *caller = "defmodule Fx.Caller do\n"
15231+
" def invoke(x), do: Fx.Target.landing(x)\n"
15232+
"end\n";
15233+
15234+
/* Tree A: indexed at `before`, then re-indexed after the arity change. */
15235+
char *repo_a = th_mktempdir("cbm_arity_incr_amb");
15236+
ASSERT_NOT_NULL(repo_a);
15237+
char dir_a[512];
15238+
snprintf(dir_a, sizeof(dir_a), "%s", repo_a);
15239+
ASSERT_EQ(th_write_file(TH_PATH(dir_a, "lib/target.ex"), before), 0);
15240+
ASSERT_EQ(th_write_file(TH_PATH(dir_a, "lib/caller.ex"), caller), 0);
15241+
char db_a[600];
15242+
snprintf(db_a, sizeof(db_a), "%s/arity_incr_amb.db", dir_a);
15243+
15244+
cbm_pipeline_t *p1 = cbm_pipeline_new(dir_a, db_a, CBM_MODE_FULL);
15245+
ASSERT_NOT_NULL(p1);
15246+
ASSERT_EQ(cbm_pipeline_run(p1), 0);
15247+
cbm_pipeline_free(p1);
15248+
15249+
ASSERT_EQ(th_write_file(TH_PATH(dir_a, "lib/target.ex"), after), 0);
15250+
cbm_pipeline_t *p2 = cbm_pipeline_new(dir_a, db_a, CBM_MODE_FULL);
15251+
ASSERT_NOT_NULL(p2);
15252+
ASSERT_EQ(cbm_pipeline_run(p2), 0);
15253+
const char *project_a = cbm_pipeline_project_name(p2);
15254+
cbm_store_t *store_a = cbm_store_open_path(db_a);
15255+
ASSERT_NOT_NULL(store_a);
15256+
char *incremental_qn = cross_file_call_target_qn(store_a, project_a, "invoke", "landing");
15257+
cbm_store_close(store_a);
15258+
cbm_pipeline_free(p2);
15259+
ASSERT_NOT_NULL(incremental_qn);
15260+
15261+
/* Tree B: the same post-change sources, indexed once from scratch. */
15262+
char *repo_b = th_mktempdir("cbm_arity_full_amb");
15263+
ASSERT_NOT_NULL(repo_b);
15264+
char dir_b[512];
15265+
snprintf(dir_b, sizeof(dir_b), "%s", repo_b);
15266+
ASSERT_EQ(th_write_file(TH_PATH(dir_b, "lib/target.ex"), after), 0);
15267+
ASSERT_EQ(th_write_file(TH_PATH(dir_b, "lib/caller.ex"), caller), 0);
15268+
char db_b[600];
15269+
snprintf(db_b, sizeof(db_b), "%s/arity_full_amb.db", dir_b);
15270+
15271+
cbm_pipeline_t *p3 = cbm_pipeline_new(dir_b, db_b, CBM_MODE_FULL);
15272+
ASSERT_NOT_NULL(p3);
15273+
ASSERT_EQ(cbm_pipeline_run(p3), 0);
15274+
const char *project_b = cbm_pipeline_project_name(p3);
15275+
cbm_store_t *store_b = cbm_store_open_path(db_b);
15276+
ASSERT_NOT_NULL(store_b);
15277+
char *full_qn = cross_file_call_target_qn(store_b, project_b, "invoke", "landing");
15278+
cbm_store_close(store_b);
15279+
cbm_pipeline_free(p3);
15280+
ASSERT_NOT_NULL(full_qn);
15281+
15282+
/* Same arity on both sides. Compare the fenced tail, since the project
15283+
* prefix differs between the two temp trees. */
15284+
const char *incremental_tail = strrchr(incremental_qn, '.');
15285+
const char *full_tail = strrchr(full_qn, '.');
15286+
ASSERT_NOT_NULL(incremental_tail);
15287+
ASSERT_NOT_NULL(full_tail);
15288+
ASSERT_STR_EQ(incremental_tail, full_tail);
15289+
ASSERT_NOT_NULL(strchr(incremental_tail, '#'));
15290+
15291+
free(incremental_qn);
15292+
free(full_qn);
15293+
PASS();
15294+
}
1511015295
/* End-to-end identity + resolution guard for a source-named container.
1511115296
* Everything below is a measured defect the graph used to carry:
1511215297
* - fetch/1, fetch/2 and fetch/3 in one module collapsed to ONE node;
@@ -15230,6 +15415,8 @@ SUITE(pipeline) {
1523015415
RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);
1523115416
RUN_TEST(pipeline_hcl_block_reference_resolves_to_its_block);
1523215417
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
15418+
RUN_TEST(pipeline_incremental_preserves_edges_across_arity_change);
15419+
RUN_TEST(pipeline_incremental_ambiguous_arity_successor_matches_full_index);
1523315420
RUN_TEST(pipeline_elixir_container_and_arity_identity);
1523415421
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
1523515422
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);

0 commit comments

Comments
 (0)