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(semantic): allocate doc candidates through the memory core

Martin Vogel483fefedbc0a
diff --git a/src/pipeline/pass_semantic_edges.c b/src/pipeline/pass_semantic_edges.c
index c5caf3a83..b0cd4dd3f 100644
--- a/src/pipeline/pass_semantic_edges.c
+++ b/src/pipeline/pass_semantic_edges.c
@@ -2261,7 +2261,7 @@ static void doc_section_lines(FILE *out, const cbm_gbuf_t *gbuf, const cbm_gbuf_
}
/* The stored candidates of every section (rank <= CBM_SEM_DOC_TOP_K, tfidf >=
- * CBM_SEM_DOC_MIN_SCORE), malloc'd for cbm_store_doc_candidates_free; p at two
+ * CBM_SEM_DOC_MIN_SCORE), in CBM_MEM_CLASS_STORE for cbm_store_doc_candidates_free; p at two
* decimals, as on SEMANTICALLY_RELATED edges. */
static cbm_doc_candidate_t *doc_candidate_rows(const cbm_gbuf_t *gbuf, const cbm_gbuf_node_t **secs,
int nsec, const cbm_sem_func_t *funcs,
@@ -2274,7 +2274,8 @@ static cbm_doc_candidate_t *doc_candidate_rows(const cbm_gbuf_t *gbuf, const cbm
}
}
*out_count = 0;
- cbm_doc_candidate_t *rows = n > 0 ? calloc((size_t)n, sizeof(*rows)) : NULL;
+ cbm_doc_candidate_t *rows =
+ n > 0 ? cbm_calloc(CBM_MEM_CLASS_STORE, (size_t)n * sizeof(*rows)) : NULL;
if (!rows) {
return NULL;
}
@@ -2293,12 +2294,12 @@ static cbm_doc_candidate_t *doc_candidate_rows(const cbm_gbuf_t *gbuf, const cbm
snprintf(ev, sizeof(ev), "{\"shared_terms\":%d,\"name_tokens\":%d}", hit->shared,
hit->name_shared);
rows[k] = (cbm_doc_candidate_t){
- .section_qn = strdup(secs[s]->qualified_name),
- .target_qn = strdup(fn->qualified_name),
+ .section_qn = cbm_mem_strdup(CBM_MEM_CLASS_STORE, secs[s]->qualified_name),
+ .target_qn = cbm_mem_strdup(CBM_MEM_CLASS_STORE, fn->qualified_name),
.rank = hit->rank,
.score = hit->tfidf,
.p = (double)(int)(cbm_sem_doc_calibrated_p(hit->tfidf) * 100.0F + 0.5F) / 100.0,
- .evidence = strdup(ev)};
+ .evidence = cbm_mem_strdup(CBM_MEM_CLASS_STORE, ev)};
if (!rows[k].section_qn || !rows[k].target_qn || !rows[k].evidence) {
cbm_store_doc_candidates_free(rows, k + SKIP_ONE);
return NULL;
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 6bcbb6361..7ecddbb4e 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1393,8 +1393,8 @@ void cbm_pipeline_take_doc_link_rows(cbm_pipeline_t *p, cbm_doc_link_row_t **row
bool *failed, bool *ran);
/* Hand the run's doc -> code candidates (pass_semantic_edges) to the
- * pipeline. Takes ownership (rows and strings malloc'd, released with
- * cbm_store_doc_candidates_free); a NULL p frees them. Passing NULL/0 clears. */
+ * pipeline. Takes ownership (rows and strings in CBM_MEM_CLASS_STORE, released
+ * with cbm_store_doc_candidates_free); a NULL p frees them. NULL/0 clears. */
void cbm_pipeline_set_doc_candidates(cbm_pipeline_t *p, cbm_doc_candidate_t *rows, int count);
const cbm_doc_candidate_t *cbm_pipeline_doc_candidates(const cbm_pipeline_t *p, int *count);
diff --git a/src/store/store.c b/src/store/store.c
index aad3f79e6..7a64fd17b 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -10847,19 +10847,22 @@ int cbm_store_doc_candidates_get(cbm_store_t *s, const char *project, const char
bind_text(stmt, SKIP_ONE, project);
bind_text(stmt, ST_COL_2, section_qn ? section_qn : target_qn);
sqlite3_bind_int(stmt, ST_COL_3, limit);
- cbm_doc_candidate_t *rows = calloc((size_t)limit, sizeof(*rows));
+ cbm_doc_candidate_t *rows = cbm_calloc(CBM_MEM_CLASS_STORE, (size_t)limit * sizeof(*rows));
if (!rows) {
sqlite3_finalize(stmt);
return CBM_STORE_ERR;
}
int n = 0;
while (n < limit && sqlite3_step(stmt) == SQLITE_ROW) {
- rows[n].section_qn = heap_strdup((const char *)sqlite3_column_text(stmt, 0));
- rows[n].target_qn = heap_strdup((const char *)sqlite3_column_text(stmt, SKIP_ONE));
+ rows[n].section_qn =
+ cbm_mem_strdup(CBM_MEM_CLASS_STORE, (const char *)sqlite3_column_text(stmt, 0));
+ rows[n].target_qn =
+ cbm_mem_strdup(CBM_MEM_CLASS_STORE, (const char *)sqlite3_column_text(stmt, SKIP_ONE));
rows[n].rank = sqlite3_column_int(stmt, ST_COL_2);
rows[n].score = sqlite3_column_double(stmt, ST_COL_3);
rows[n].p = sqlite3_column_double(stmt, ST_COL_4);
- rows[n].evidence = heap_strdup((const char *)sqlite3_column_text(stmt, ST_COL_5));
+ rows[n].evidence =
+ cbm_mem_strdup(CBM_MEM_CLASS_STORE, (const char *)sqlite3_column_text(stmt, ST_COL_5));
n++;
}
sqlite3_finalize(stmt);
@@ -10870,11 +10873,11 @@ int cbm_store_doc_candidates_get(cbm_store_t *s, const char *project, const char
void cbm_store_doc_candidates_free(cbm_doc_candidate_t *rows, int count) {
for (int i = 0; rows && i < count; i++) {
- free((void *)rows[i].section_qn);
- free((void *)rows[i].target_qn);
- free((void *)rows[i].evidence);
+ cbm_free(CBM_MEM_CLASS_STORE, (void *)rows[i].section_qn);
+ cbm_free(CBM_MEM_CLASS_STORE, (void *)rows[i].target_qn);
+ cbm_free(CBM_MEM_CLASS_STORE, (void *)rows[i].evidence);
}
- free(rows);
+ cbm_free(CBM_MEM_CLASS_STORE, rows);
}
void cbm_adr_sections_free(cbm_adr_sections_t *s) {
diff --git a/src/store/store.h b/src/store/store.h
index 881ba3dc9..e7fb3142b 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -1105,7 +1105,8 @@ int cbm_store_doc_candidates_replace(cbm_store_t *s, const char *project,
/* The candidates of one section (section_qn) or for one function (target_qn;
* pass the other NULL), highest p first, then score, at most limit. *out is
- * allocated (cbm_store_doc_candidates_free). CBM_STORE_NOT_FOUND: no table. */
+ * allocated in CBM_MEM_CLASS_STORE (cbm_store_doc_candidates_free).
+ * CBM_STORE_NOT_FOUND: no table. */
int cbm_store_doc_candidates_get(cbm_store_t *s, const char *project, const char *section_qn,
const char *target_qn, int limit, cbm_doc_candidate_t **out,
int *count);