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.

feat(graph): temporal properties on FILE_CHANGES_WITH edges and File nodes (#257)

Adam Schulte4e80d2fff751
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index ff9879243..f06873adc 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -2227,7 +2227,10 @@ static const char *binding_get_virtual(binding_t *b, const char *var, const char
/* Fall through to normal lookup */
cbm_edge_t *e = binding_get_edge(b, var);
if (e) {
- return prop ? edge_prop(e, prop) : "";
+ /* Bare `RETURN r` on an edge: surface the full properties JSON
+ * (or "{}" if none) so callers can inspect timestamps, weights,
+ * etc. without naming each property. */
+ return prop ? edge_prop(e, prop) : (e->properties_json ? e->properties_json : "{}");
}
cbm_node_t *n = binding_get(b, var);
if (n) {
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index a3dbbd8d9..b3a3d39d7 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -1278,10 +1278,25 @@ static char *bm25_search(cbm_store_t *store, const char *project, const char *qu
return json;
}
-/* Emit the cbm_store_search results as a JSON "results" array on the doc. */
+/* Forward declaration — defined later. enrich_node_properties parses the
+ * node's properties_json and grafts the parsed values onto the result item.
+ * It returns the parsed yyjson_doc which must outlive the serialization
+ * because yyjson_mut_obj_add_val uses zero-copy strings into that doc. */
+static yyjson_doc *enrich_node_properties(yyjson_mut_doc *doc, yyjson_mut_val *obj,
+ const char *properties_json);
+
+/* Emit the cbm_store_search results as a JSON "results" array on the doc.
+ * Property docs created via enrich_node_properties are collected in
+ * *out_pdocs (count in *out_pdoc_count) and must be freed by the caller
+ * AFTER serializing doc, since yyjson_mut strings are zero-copy pointers
+ * into those parsed docs. The caller also frees out_pdocs itself. */
static void emit_search_results(yyjson_mut_doc *doc, yyjson_mut_val *root,
const cbm_search_output_t *out, cbm_store_t *store,
- const char *relationship, bool include_connected, int offset) {
+ const char *relationship, bool include_connected, int offset,
+ yyjson_doc ***out_pdocs, int *out_pdoc_count) {
+ yyjson_doc **pdocs =
+ out->count > 0 ? malloc((size_t)out->count * sizeof(yyjson_doc *)) : NULL;
+ int pdoc_count = 0;
yyjson_mut_obj_add_int(doc, root, "total", out->total);
yyjson_mut_val *results = yyjson_mut_arr(doc);
for (int i = 0; i < out->count; i++) {
@@ -1298,10 +1313,16 @@ static void emit_search_results(yyjson_mut_doc *doc, yyjson_mut_val *root,
if (include_connected && sr->node.id > 0) {
enrich_connected(doc, item, store, sr->node.id, relationship);
}
+ yyjson_doc *pdoc = enrich_node_properties(doc, item, sr->node.properties_json);
+ if (pdoc && pdocs) {
+ pdocs[pdoc_count++] = pdoc;
+ }
yyjson_mut_arr_add_val(results, item);
}
yyjson_mut_obj_add_val(doc, root, "results", results);
yyjson_mut_obj_add_bool(doc, root, "has_more", out->total > offset + out->count);
+ *out_pdocs = pdocs;
+ *out_pdoc_count = pdoc_count;
}
/* Extract keyword strings from a yyjson array into `keywords`. Returns the
@@ -1444,7 +1465,10 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_val *root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
- emit_search_results(doc, root, &out, store, relationship, include_connected, offset);
+ yyjson_doc **props_docs = NULL;
+ int props_doc_count = 0;
+ emit_search_results(doc, root, &out, store, relationship, include_connected, offset,
+ &props_docs, &props_doc_count);
/* Add diagnostic hint when zero results */
if (out.total == 0) {
@@ -1467,6 +1491,10 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
bool sq_type_error = run_semantic_query(doc, root, args, store, project, limit);
if (sq_type_error) {
+ for (int pi = 0; pi < props_doc_count; pi++) {
+ yyjson_doc_free(props_docs[pi]);
+ }
+ free(props_docs);
yyjson_mut_doc_free(doc);
cbm_store_search_free(&out);
free(project);
@@ -1484,6 +1512,12 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
}
char *json = yy_doc_to_str(doc);
+ /* Property docs are zero-copy referenced by the mut doc — they must
+ * outlive yy_doc_to_str. Free them once serialization is complete. */
+ for (int pi = 0; pi < props_doc_count; pi++) {
+ yyjson_doc_free(props_docs[pi]);
+ }
+ free(props_docs);
yyjson_mut_doc_free(doc);
cbm_store_search_free(&out);
diff --git a/src/pipeline/pass_githistory.c b/src/pipeline/pass_githistory.c
index cd3ea4488..39ac394e9 100644
--- a/src/pipeline/pass_githistory.c
+++ b/src/pipeline/pass_githistory.c
@@ -87,6 +87,7 @@ typedef struct {
char **files;
int count;
int cap;
+ long long timestamp; /* unix epoch of this commit; 0 when unknown */
} commit_t;
static void commit_add_file(commit_t *c, const char *file) {
@@ -189,6 +190,7 @@ static int parse_git_log(const char *repo_path, commit_t **out, int *out_count)
cap *= PAIR_LEN;
commits = safe_realloc(commits, cap * sizeof(commit_t));
}
+ current.timestamp = (long long)ct;
commits[count++] = current;
} else {
commit_free(&current);
@@ -224,7 +226,7 @@ static int parse_git_log(const char *repo_path, commit_t **out, int *out_count)
char cmd[CBM_SZ_1K];
snprintf(cmd, sizeof(cmd),
- "cd '%s' && git log --name-only --pretty=format:COMMIT:%%H "
+ "cd '%s' && git log --name-only --pretty=format:COMMIT:%%H:%%ct "
"--since='1 year ago' --max-count=10000 2>/dev/null",
repo_path);
@@ -257,6 +259,12 @@ static int parse_git_log(const char *repo_path, commit_t **out, int *out_count)
commits[count++] = current;
memset(&current, 0, sizeof(current));
}
+ /* Parse the unix timestamp from "COMMIT:<hash>:<unix_epoch>".
+ * Older callers / stripped-down git output without %ct land on 0. */
+ const char *hash_end = strchr(line + SLEN("COMMIT:"), ':');
+ if (hash_end) {
+ current.timestamp = strtoll(hash_end + 1, NULL, 10);
+ }
continue;
}
@@ -294,6 +302,7 @@ static void free_counter(const char *key, void *val, void *ud) {
/* Context for collect_coupling_result callback. */
typedef struct {
CBMHashTable *file_counts;
+ CBMHashTable *pair_timestamps; /* pair_key → long long*: max commit ts */
cbm_change_coupling_t *out;
int out_count;
int max_out;
@@ -344,12 +353,18 @@ static void collect_coupling_cb(const char *pair_key, void *val, void *ud) {
snprintf(cc->file_b, sizeof(cc->file_b), "%s", file_b);
cc->co_change_count = co_count;
cc->coupling_score = score;
+ long long *ts = cbm_ht_get(cctx->pair_timestamps, pair_key);
+ cc->last_co_change = ts ? *ts : 0;
}
int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_count,
cbm_change_coupling_t *out, int max_out) {
CBMHashTable *file_counts = cbm_ht_create(CBM_SZ_1K);
CBMHashTable *pair_counts = cbm_ht_create(CBM_SZ_2K);
+ /* Parallel table mapping pair_key → max commit timestamp seen for that
+ * pair, so the resulting edge can carry last_co_change. The pair_counts
+ * table consumes its key on insert; pair_timestamps gets its own copy. */
+ CBMHashTable *pair_timestamps = cbm_ht_create(CBM_SZ_2K);
for (int c = 0; c < commit_count; c++) {
if (commits[c].count > GH_MAX_FILES) {
@@ -378,7 +393,8 @@ int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_co
}
size_t la = strlen(a);
size_t lb = strlen(b);
- char *pk = malloc(la + SKIP_ONE + lb + SKIP_ONE);
+ size_t pk_len = la + SKIP_ONE + lb + SKIP_ONE;
+ char *pk = malloc(pk_len);
memcpy(pk, a, la);
pk[la] = '\x01';
memcpy(pk + la + SKIP_ONE, b, lb + SKIP_ONE);
@@ -386,11 +402,22 @@ int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_co
int *val = cbm_ht_get(pair_counts, pk);
if (val) {
(*val)++;
+ long long *ts = cbm_ht_get(pair_timestamps, pk);
+ if (ts && commits[c].timestamp > *ts) {
+ *ts = commits[c].timestamp;
+ }
free(pk);
} else {
int *nv = malloc(sizeof(int));
*nv = SKIP_ONE;
+ /* pair_counts takes ownership of pk; pair_timestamps
+ * needs its own copy. */
+ char *pk2 = malloc(pk_len);
+ memcpy(pk2, pk, pk_len);
cbm_ht_set(pair_counts, pk, nv);
+ long long *nts = malloc(sizeof(long long));
+ *nts = commits[c].timestamp;
+ cbm_ht_set(pair_timestamps, pk2, nts);
}
}
}
@@ -398,6 +425,7 @@ int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_co
collect_coupling_ctx_t cctx = {
.file_counts = file_counts,
+ .pair_timestamps = pair_timestamps,
.out = out,
.out_count = 0,
.max_out = max_out,
@@ -406,6 +434,8 @@ int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_co
cbm_ht_foreach(pair_counts, free_counter, NULL);
cbm_ht_free(pair_counts);
+ cbm_ht_foreach(pair_timestamps, free_counter, NULL);
+ cbm_ht_free(pair_timestamps);
cbm_ht_foreach(file_counts, free_counter, NULL);
cbm_ht_free(file_counts);
@@ -416,6 +446,7 @@ int cbm_compute_change_coupling(const cbm_commit_files_t *commits, int commit_co
/* Pre-computed coupling result buffer for fused post-pass parallelism. */
#define MAX_COUPLINGS 8192
+#define MAX_FILE_TEMPORAL 16384
/* Compute change couplings without touching the graph buffer.
* Can run on a separate thread while other passes use the gbuf. */
@@ -423,6 +454,8 @@ int cbm_pipeline_githistory_compute(const char *repo_path, cbm_githistory_result
result->couplings = NULL;
result->count = 0;
result->commit_count = 0;
+ result->file_temporal = NULL;
+ result->file_temporal_count = 0;
commit_t *commits = NULL;
int commit_count = 0;
@@ -446,11 +479,50 @@ int cbm_pipeline_githistory_compute(const char *repo_path, cbm_githistory_result
for (int c = 0; c < commit_count; c++) {
cf[c].files = commits[c].files;
cf[c].count = commits[c].count;
+ cf[c].timestamp = commits[c].timestamp;
}
cbm_change_coupling_t *couplings = malloc(MAX_COUPLINGS * sizeof(cbm_change_coupling_t));
int coupling_count = cbm_compute_change_coupling(cf, commit_count, couplings, MAX_COUPLINGS);
+ /* Per-file temporal aggregation: change_count + last_modified.
+ * Single hash-table pass over the same commit set used for coupling so
+ * we don't re-scan history. NULL on OOM is fine — the caller still
+ * gets the couplings. */
+ cbm_file_temporal_t *ft_arr = malloc(MAX_FILE_TEMPORAL * sizeof(cbm_file_temporal_t));
+ int ft_count = 0;
+ if (ft_arr) {
+ CBMHashTable *file_idx = cbm_ht_create(CBM_SZ_1K);
+ for (int c = 0; c < commit_count; c++) {
+ if (cf[c].count > GH_MAX_FILES) {
+ continue;
+ }
+ for (int f = 0; f < cf[c].count; f++) {
+ const char *fp = cf[c].files[f];
+ int *idx = cbm_ht_get(file_idx, fp);
+ if (idx) {
+ ft_arr[*idx].change_count++;
+ if (cf[c].timestamp > ft_arr[*idx].last_modified) {
+ ft_arr[*idx].last_modified = cf[c].timestamp;
+ }
+ } else if (ft_count < MAX_FILE_TEMPORAL) {
+ int new_idx = ft_count++;
+ snprintf(ft_arr[new_idx].file_path, sizeof(ft_arr[new_idx].file_path), "%s",
+ fp);
+ ft_arr[new_idx].change_count = 1;
+ ft_arr[new_idx].last_modified = cf[c].timestamp;
+ int *nidx = malloc(sizeof(int));
+ *nidx = new_idx;
+ cbm_ht_set(file_idx, strdup(fp), nidx);
+ }
+ }
+ }
+ cbm_ht_foreach(file_idx, free_counter, NULL);
+ cbm_ht_free(file_idx);
+ result->file_temporal = ft_arr;
+ result->file_temporal_count = ft_count;
+ }
+
free(cf);
for (int c = 0; c < commit_count; c++) {
commit_free(&commits[c]);
@@ -483,13 +555,42 @@ int cbm_pipeline_githistory_apply(cbm_pipeline_ctx_t *ctx, const cbm_githistory_
}
char props[CBM_SZ_128];
- snprintf(props, sizeof(props), "{\"co_changes\":%d,\"coupling_score\":%.2f}",
- cc->co_change_count, cc->coupling_score);
+ snprintf(props, sizeof(props),
+ "{\"co_changes\":%d,\"coupling_score\":%.2f,\"last_co_change\":%lld}",
+ cc->co_change_count, cc->coupling_score, cc->last_co_change);
cbm_gbuf_insert_edge(ctx->gbuf, node_a->id, node_b->id, "FILE_CHANGES_WITH", props);
edge_count++;
}
+ /* Apply per-file temporal metadata to existing File nodes so callers
+ * can query change_count / last_modified for hotspot analysis. The
+ * extension is re-derived and JSON-escaped to keep the props blob
+ * well-formed even for paths with quotes or backslashes. */
+ for (int i = 0; i < result->file_temporal_count; i++) {
+ const cbm_file_temporal_t *ft = &result->file_temporal[i];
+ char *qn = cbm_pipeline_fqn_compute(ctx->project_name, ft->file_path, "__file__");
+ const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(ctx->gbuf, qn);
+ free(qn);
+ if (!node) {
+ continue;
+ }
+
+ const char *base = strrchr(ft->file_path, '/');
+ base = base ? base + SKIP_ONE : ft->file_path;
+ const char *ext = strrchr(base, '.');
+ char ext_escaped[CBM_SZ_64];
+ cbm_json_escape(ext_escaped, (int)sizeof(ext_escaped), ext ? ext : "");
+
+ char props[CBM_SZ_256];
+ snprintf(props, sizeof(props),
+ "{\"extension\":\"%s\",\"last_modified\":%lld,\"change_count\":%d}",
+ ext_escaped, ft->last_modified, ft->change_count);
+
+ cbm_gbuf_upsert_node(ctx->gbuf, node->label, node->name, node->qualified_name,
+ node->file_path, node->start_line, node->end_line, props);
+ }
+
return edge_count;
}
@@ -502,11 +603,12 @@ int cbm_pipeline_pass_githistory(cbm_pipeline_ctx_t *ctx) {
cbm_pipeline_githistory_compute(ctx->repo_path, &result);
int edge_count = 0;
- if (result.count > 0) {
+ if (result.count > 0 || result.file_temporal_count > 0) {
edge_count = cbm_pipeline_githistory_apply(ctx, &result);
}
free(result.couplings);
+ free(result.file_temporal);
cbm_log_info("pass.done", "pass", "githistory", "commits", itoa_log(result.commit_count),
"edges", itoa_log(edge_count));
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 789661b26..98da5bf74 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -733,12 +733,13 @@ static int run_githistory(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {
}
int gh_edges = 0;
- if (gh_result.count > 0) {
+ if (gh_result.count > 0 || gh_result.file_temporal_count > 0) {
gh_edges = cbm_pipeline_githistory_apply(ctx, &gh_result);
}
cbm_log_info("pass.done", "pass", "githistory", "commits", itoa_buf(gh_result.commit_count),
"edges", itoa_buf(gh_edges));
free(gh_result.couplings);
+ free(gh_result.file_temporal);
return 0;
}
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 4b655cb20..b586f17d2 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -117,14 +117,30 @@ typedef struct {
char file_b[CBM_SZ_512];
int co_change_count;
double coupling_score;
+ /* Unix epoch of the most recent commit that touched both files together.
+ * 0 when no timestamp was available (e.g. older callers / popen path
+ * without %ct). */
+ long long last_co_change;
} cbm_change_coupling_t;
/* Commit data for coupling analysis */
typedef struct {
char **files;
int count;
+ /* Unix epoch of the commit. 0 means unknown — coupling computation
+ * still works but last_co_change on the resulting edge will be 0. */
+ long long timestamp;
} cbm_commit_files_t;
+/* Per-file temporal metadata. Populated alongside change-coupling so File
+ * nodes can carry change_count and last_modified for hotspot / risk
+ * analysis queries. */
+typedef struct {
+ char file_path[CBM_SZ_512];
+ int change_count;
+ long long last_modified; /* unix epoch of most recent commit */
+} cbm_file_temporal_t;
+
/* Compute change coupling from commit history.
* Returns number of couplings written to out (up to max_out).
* Caller owns out[]. */
@@ -383,6 +399,10 @@ typedef struct {
cbm_change_coupling_t *couplings;
int count;
int commit_count;
+ /* Per-file temporal data (change_count + last_modified) for File nodes.
+ * NULL when the history pass had no commits to analyse. */
+ cbm_file_temporal_t *file_temporal;
+ int file_temporal_count;
} cbm_githistory_result_t;
/* Compute change couplings without touching the graph buffer.
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index a1694322b..0f7dc656e 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -1026,6 +1026,30 @@ TEST(cypher_edge_filter_numeric_gte) {
PASS();
}
+TEST(cypher_bare_edge_return_exposes_properties_json) {
+ /* `RETURN r` on an edge variable, with no property accessor, should
+ * surface the edge's full properties JSON (or "{}"). Before the fix,
+ * binding_get_virtual returned an empty string, which made bare edge
+ * returns useless for callers that wanted to inspect timestamps,
+ * weights, etc. without naming each property up front. */
+ cbm_store_t *s = setup_cypher_multi_edge_store();
+ cbm_cypher_result_t r = {0};
+
+ int rc = cbm_cypher_execute(
+ s, "MATCH (a)-[r:HTTP_CALLS]->(b) WHERE r.method = 'POST' RETURN r", "testproj", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ const char *r_val = cypher_get_col(&r, 0, "r");
+ ASSERT_NOT_NULL(r_val);
+ /* Expect JSON object content rather than the previous empty string. */
+ ASSERT_NOT_NULL(strstr(r_val, "url_path"));
+ ASSERT_NOT_NULL(strstr(r_val, "/api/orders"));
+
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_edge_return_without_filter) {
cbm_store_t *s = setup_cypher_multi_edge_store();
cbm_cypher_result_t r = {0};
@@ -2142,6 +2166,7 @@ SUITE(cypher) {
RUN_TEST(cypher_edge_type_prop);
RUN_TEST(cypher_edge_filter_contains);
RUN_TEST(cypher_edge_filter_numeric_gte);
+ RUN_TEST(cypher_bare_edge_return_exposes_properties_json);
RUN_TEST(cypher_edge_return_without_filter);
RUN_TEST(cypher_edge_filter_equals);
RUN_TEST(cypher_edge_filter_starts_with);
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index 72729f110..f182241f6 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -393,6 +393,41 @@ TEST(tool_search_graph_basic) {
PASS();
}
+/* Forward declarations for helpers defined later in this file */
+static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz);
+static void cleanup_snippet_dir(const char *tmp_dir);
+static char *extract_text_content(const char *mcp_result);
+
+TEST(tool_search_graph_includes_node_properties) {
+ /* search_graph results must surface each node's properties_json
+ * payload so callers don't have to round-trip through get_code_snippet
+ * just to read them. The setup_snippet_server inserts HandleRequest
+ * with a signature/return_type/is_exported property blob; this test
+ * pins that those keys reach the MCP response. */
+ char tmp[256];
+ cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
+ ASSERT_NOT_NULL(srv);
+
+ char *resp = cbm_mcp_server_handle(
+ srv, "{\"jsonrpc\":\"2.0\",\"id\":42,\"method\":\"tools/call\","
+ "\"params\":{\"name\":\"search_graph\","
+ "\"arguments\":{\"project\":\"test-project\",\"label\":\"Function\","
+ "\"name_pattern\":\"HandleRequest\",\"limit\":5}}}");
+ ASSERT_NOT_NULL(resp);
+ char *inner = extract_text_content(resp);
+ ASSERT_NOT_NULL(inner);
+ /* Properties from HandleRequest's properties_json must appear. */
+ ASSERT_NOT_NULL(strstr(inner, "signature"));
+ ASSERT_NOT_NULL(strstr(inner, "func HandleRequest"));
+ ASSERT_NOT_NULL(strstr(inner, "is_exported"));
+ free(inner);
+ free(resp);
+
+ cbm_mcp_server_free(srv);
+ cleanup_snippet_dir(tmp);
+ PASS();
+}
+
TEST(tool_query_graph_basic) {
cbm_mcp_server_t *srv = setup_mcp_with_data();
@@ -1695,6 +1730,7 @@ SUITE(mcp) {
RUN_TEST(tool_get_graph_schema_empty);
RUN_TEST(tool_unknown_tool);
RUN_TEST(tool_search_graph_basic);
+ RUN_TEST(tool_search_graph_includes_node_properties);
RUN_TEST(tool_query_graph_basic);
RUN_TEST(tool_index_status_no_project);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 3e46b6f7a..08f5dbafc 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -529,7 +529,7 @@ TEST(githistory_compute_coupling) {
char *files_4[] = {"d.go", "e.go"};
cbm_commit_files_t commits[] = {
- {files_0, 3}, {files_1, 2}, {files_2, 2}, {files_3, 2}, {files_4, 2},
+ {files_0, 3, 0}, {files_1, 2, 0}, {files_2, 2, 0}, {files_3, 2, 0}, {files_4, 2, 0},
};
cbm_change_coupling_t results[100];
@@ -556,6 +556,47 @@ TEST(githistory_compute_coupling) {
PASS();
}
+TEST(githistory_coupling_carries_last_co_change) {
+ /* Coupling output must surface the most recent commit timestamp at which
+ * a pair co-changed, so callers can score recency in addition to
+ * frequency. The pair (a.go, b.go) co-changes at three timestamps; the
+ * resulting last_co_change must be the maximum (newest) of those. */
+ char *files_old[] = {"a.go", "b.go"};
+ char *files_mid[] = {"a.go", "b.go"};
+ char *files_new[] = {"a.go", "b.go"};
+ /* Unrelated co-change in the middle to make sure we don't accidentally
+ * pick up that pair's timestamp by index. */
+ char *files_other[] = {"c.go", "d.go"};
+
+ cbm_commit_files_t commits[] = {
+ {files_old, 2, 1700000000LL}, /* oldest a.go/b.go co-change */
+ {files_other, 2, 1750000000LL}, /* unrelated pair */
+ {files_mid, 2, 1720000000LL},
+ {files_new, 2, 1800000000LL}, /* newest a.go/b.go co-change */
+ };
+
+ cbm_change_coupling_t results[16];
+ int n = cbm_compute_change_coupling(commits, 4, results, 16);
+ ASSERT_GTE(n, 1);
+
+ bool found_ab = false;
+ for (int i = 0; i < n; i++) {
+ bool is_ab = (strcmp(results[i].file_a, "a.go") == 0 &&
+ strcmp(results[i].file_b, "b.go") == 0) ||
+ (strcmp(results[i].file_a, "b.go") == 0 &&
+ strcmp(results[i].file_b, "a.go") == 0);
+ if (!is_ab) {
+ continue;
+ }
+ ASSERT_EQ(results[i].co_change_count, 3);
+ /* last_co_change must be the max of the three a.go/b.go timestamps. */
+ ASSERT_EQ(results[i].last_co_change, 1800000000LL);
+ found_ab = true;
+ }
+ ASSERT_TRUE(found_ab);
+ PASS();
+}
+
TEST(githistory_skip_large_commits) {
/* A single commit with 25 files → should be skipped (>20) */
char *files[25];
@@ -565,7 +606,7 @@ TEST(githistory_skip_large_commits) {
files[i] = bufs[i];
}
- cbm_commit_files_t commits[] = {{files, 25}};
+ cbm_commit_files_t commits[] = {{files, 25, 0}};
cbm_change_coupling_t results[100];
int n = cbm_compute_change_coupling(commits, 1, results, 100);
@@ -4138,7 +4179,8 @@ TEST(githistory_compute_change_coupling) {
char *files_eee[] = {"d.go", "e.go"};
cbm_commit_files_t commits[5] = {
- {files_aaa, 3}, {files_bbb, 2}, {files_ccc, 2}, {files_ddd, 2}, {files_eee, 2},
+ {files_aaa, 3, 0}, {files_bbb, 2, 0}, {files_ccc, 2, 0},
+ {files_ddd, 2, 0}, {files_eee, 2, 0},
};
cbm_change_coupling_t out[100];
@@ -4174,7 +4216,7 @@ TEST(githistory_coupling_skips_large_commits) {
snprintf(bufs[i], sizeof(bufs[i]), "file%d.go", i);
files[i] = bufs[i];
}
- cbm_commit_files_t commits[1] = {{files, 25}};
+ cbm_commit_files_t commits[1] = {{files, 25, 0}};
cbm_change_coupling_t out[100];
int count = cbm_compute_change_coupling(commits, 1, out, 100);
@@ -5229,7 +5271,7 @@ TEST(coupling_single_file_commit) {
/* Commits with single files → no pairs → zero couplings */
char *f1[] = {"a.go"};
char *f2[] = {"b.go"};
- cbm_commit_files_t commits[] = {{f1, 1}, {f2, 1}};
+ cbm_commit_files_t commits[] = {{f1, 1, 0}, {f2, 1, 0}};
cbm_change_coupling_t results[16];
int n = cbm_compute_change_coupling(commits, 2, results, 16);
ASSERT_EQ(n, 0);
@@ -5349,6 +5391,7 @@ SUITE(pipeline) {
/* Git history pass */
RUN_TEST(githistory_is_trackable);
RUN_TEST(githistory_compute_coupling);
+ RUN_TEST(githistory_coupling_carries_last_co_change);
RUN_TEST(githistory_skip_large_commits);
RUN_TEST(githistory_limits_to_max);
/* Test detection */