Skip to content

Commit 1b581f1

Browse files
authored
Merge pull request DeusData#2346 from DeusData/feat/cross-repo-status
feat(cross-repo): report run status and skipped legacy stores (DeusData#2133)
2 parents ec47e40 + 515670b commit 1b581f1

10 files changed

Lines changed: 506 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ The install script placed beside the binary is **reported, not deleted** — uni
222222
- **`CROSS_*` edges** link nodes across multiple repos indexed under the same store
223223
- **Multi-galaxy 3D UI layout** for cross-repo architecture visualization
224224
- **Cross-repo architecture summary** combining services, routes, and dependencies across the indexed fleet
225+
- **Run status**: `index_status` (and the UI layout next to `linked_projects`) carries a `cross_repo` object, `{"status":"never_run"}` until `index_repository(mode="cross-repo-intelligence")` has run with that project as the source, then `"ran"` with `last_run_at`, `outcome`, `targets`, `projects_scanned`, `total_cross_edges` and `skipped_projects`. That is how you tell "never linked" apart from "linked, nothing matched". The status is stored in the project's own index next to its `CROSS_*` edges, so a reindex that rebuilds the index drops both at once and the status goes back to `never_run`.
226+
- **Skipped stores**: `target_projects: ["*"]` skips an index built before the #768 schema change rather than failing the whole run. The response lists each one as `skipped_projects: [{"name", "reason": "pre_768_schema", "hint": "reindex this project"}]`.
225227

226228
### Edge types (selected)
227229
- `CALLS` — a callable is invoked at the source site

‎src/mcp/mcp.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6940,6 +6940,7 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
69406940
}
69416941
add_coverage_report(doc, root, store, project, have_proj_info ? proj_info.indexed_at : NULL,
69426942
coverage_samples);
6943+
(void)cbm_cross_repo_add_status_json(doc, root, store, project);
69436944
safe_str_free(&proj_info.name);
69446945
safe_str_free(&proj_info.indexed_at);
69456946
safe_str_free(&proj_info.root_path);
@@ -10098,6 +10099,7 @@ static char *handle_cross_repo_mode(cbm_mcp_server_t *srv, const char *repo_path
1009810099
yyjson_doc_free(jdoc);
1009910100

1010010101
if (result.no_targets || result.failed) {
10102+
cbm_cross_repo_result_free(&result);
1010110103
free(project);
1010210104
return cbm_mcp_text_result(
1010310105
result.no_targets
@@ -10135,9 +10137,23 @@ static char *handle_cross_repo_mode(cbm_mcp_server_t *srv, const char *repo_path
1013510137
yyjson_mut_obj_add_int(doc, root, "cross_trpc_calls", result.trpc_edges);
1013610138
yyjson_mut_obj_add_int(doc, root, "total_cross_edges", total);
1013710139
yyjson_mut_obj_add_real(doc, root, "elapsed_ms", result.elapsed_ms);
10140+
/* ["*"] skips stores it cannot link (#2133); name them so a partial fleet
10141+
* is visible instead of silently smaller. Only emitted when non-empty. */
10142+
if (result.skipped_count > 0) {
10143+
yyjson_mut_val *skipped = yyjson_mut_arr(doc);
10144+
for (int i = 0; i < result.skipped_count; i++) {
10145+
yyjson_mut_val *item = yyjson_mut_obj(doc);
10146+
yyjson_mut_obj_add_strcpy(doc, item, "name", result.skipped_projects[i].project);
10147+
yyjson_mut_obj_add_str(doc, item, "reason", result.skipped_projects[i].reason);
10148+
yyjson_mut_obj_add_str(doc, item, "hint", "reindex this project");
10149+
yyjson_mut_arr_append(skipped, item);
10150+
}
10151+
yyjson_mut_obj_add_val(doc, root, "skipped_projects", skipped);
10152+
}
1013810153

1013910154
char *json = yy_doc_to_str(doc);
1014010155
yyjson_mut_doc_free(doc);
10156+
cbm_cross_repo_result_free(&result);
1014110157
free(project);
1014210158
char *out = cbm_mcp_text_result(json, result.cancelled);
1014310159
free(json);

‎src/pipeline/pass_cross_repo.c‎

Lines changed: 158 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
#include "pipeline/pipeline_internal.h" // cbm_route_canon_path
1414
#include "foundation/constants.h"
1515
#include "foundation/log.h"
16+
#include "foundation/mem_core.h"
1617
#include "foundation/platform.h"
1718
#include "foundation/compat.h"
1819
#include "foundation/compat_fs.h"
1920
#include "foundation/str_util.h"
2021

2122
#include <sqlite3/sqlite3.h>
23+
#include <yyjson/yyjson.h>
2224
#include <stdint.h>
2325
#include <stdio.h>
2426
#include <stdlib.h>
@@ -153,27 +155,38 @@ static bool cr_store_has_exact_project(cbm_store_t *store, const char *project)
153155
return matches;
154156
}
155157

156-
/* True when `project` is a usable cross-repo input: its store holds exactly
157-
* that project AND carries the schema the read-write open in
158+
typedef enum {
159+
CR_PROJECT_USABLE = 0,
160+
CR_PROJECT_MISSING, /* no store, or the store is not exactly this project */
161+
CR_PROJECT_LEGACY, /* exact project, but a pre-#768 schema (reindex required) */
162+
} cr_project_state_t;
163+
164+
/* Classify `project` as a cross-repo input: usable when its store holds
165+
* exactly that project AND carries the schema the read-write open in
158166
* cr_open_existing_project requires. Checking only the first let a pre-#768
159167
* store (still readable, so list_projects shows it) pass validation and the
160168
* ["*"] enumeration, then abort the whole run at its write open — after the
161169
* source's previous CROSS_* generation had already been deleted. The probe is
162170
* read-only, so the legacy store is left untouched for its reindex. (#2133) */
163-
static bool cr_project_exists(const char *project) {
171+
static cr_project_state_t cr_project_state(const char *project) {
164172
char path[CR_PATH_BUF];
165173
if (!cr_db_path(project, path, sizeof(path))) {
166-
return false;
174+
return CR_PROJECT_MISSING;
167175
}
168176
cbm_store_t *store = cbm_store_open_path_query(path);
169-
bool exists = cr_store_has_exact_project(store, project);
170-
if (exists && !cbm_store_edges_schema_current(store)) {
177+
cr_project_state_t state =
178+
cr_store_has_exact_project(store, project) ? CR_PROJECT_USABLE : CR_PROJECT_MISSING;
179+
if (state == CR_PROJECT_USABLE && !cbm_store_edges_schema_current(store)) {
171180
cbm_log_warn("cross_repo.project_unusable", "project", project, "reason",
172181
"pre_768_schema_reindex_required");
173-
exists = false;
182+
state = CR_PROJECT_LEGACY;
174183
}
175184
cbm_store_close(store);
176-
return exists;
185+
return state;
186+
}
187+
188+
static bool cr_project_exists(const char *project) {
189+
return cr_project_state(project) == CR_PROJECT_USABLE;
177190
}
178191

179192
static cbm_store_t *cr_open_existing_project(const char *project) {
@@ -1139,8 +1152,33 @@ static cr_match_result_t match_typed_routes(cbm_store_t *src_store, const char *
11391152

11401153
static void free_project_list(char **projects, int count);
11411154

1155+
/* Remember a store the ["*"] enumeration skipped, so the caller can name it
1156+
* instead of the run silently covering fewer projects than exist. */
1157+
static bool cr_record_skip(cbm_cross_repo_result_t *result, const char *project,
1158+
const char *reason) {
1159+
if (!result) {
1160+
return true;
1161+
}
1162+
cbm_cross_repo_skip_t *grown =
1163+
cbm_realloc(CBM_MEM_CLASS_OTHER, result->skipped_projects,
1164+
((size_t)result->skipped_count + 1U) * sizeof(*result->skipped_projects));
1165+
if (!grown) {
1166+
return false;
1167+
}
1168+
result->skipped_projects = grown;
1169+
char *name = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, project);
1170+
if (!name) {
1171+
return false;
1172+
}
1173+
grown[result->skipped_count].project = name;
1174+
grown[result->skipped_count].reason = reason;
1175+
result->skipped_count++;
1176+
return true;
1177+
}
1178+
11421179
/* When target_projects = ["*"], scan the cache directory for all .db files. */
1143-
static int collect_all_projects(char ***out, cr_run_context_t *ctx) {
1180+
static int collect_all_projects(char ***out, cr_run_context_t *ctx,
1181+
cbm_cross_repo_result_t *result) {
11441182
*out = NULL;
11451183
const char *dir = cr_cache_dir();
11461184
cbm_dir_t *d = cbm_opendir(dir);
@@ -1188,7 +1226,15 @@ static int collect_all_projects(char ***out, cr_run_context_t *ctx) {
11881226
}
11891227
memcpy(project, ent->name, project_length);
11901228
project[project_length] = '\0';
1191-
if (!cbm_validate_project_name(project) || !cr_project_exists(project)) {
1229+
if (!cbm_validate_project_name(project)) {
1230+
continue;
1231+
}
1232+
cr_project_state_t state = cr_project_state(project);
1233+
if (state == CR_PROJECT_LEGACY && !cr_record_skip(result, project, "pre_768_schema")) {
1234+
failed = true;
1235+
break;
1236+
}
1237+
if (state != CR_PROJECT_USABLE) {
11921238
continue;
11931239
}
11941240
if (count >= cap) {
@@ -1273,6 +1319,106 @@ static int collect_named_projects(const char **targets, int target_count, char *
12731319
return unique_count;
12741320
}
12751321

1322+
/* ── Last-run marker ─────────────────────────────────────────────── */
1323+
1324+
static bool cr_status_key(const char *project, char *buf, size_t bufsz) {
1325+
int written = snprintf(buf, bufsz, "cross_repo_last_run:%s", project);
1326+
return written > 0 && (size_t)written < bufsz;
1327+
}
1328+
1329+
/* Record this run in the SOURCE store's store_meta, next to the CROSS_*
1330+
* edges it just rewrote: a reindex that rebuilds the store drops both
1331+
* together, so the marker can never outlive the links it describes. Written
1332+
* once the source's previous CROSS_* generation was cleaned, whatever the
1333+
* outcome, because from then on the old summary no longer matches the graph.
1334+
* Project names are validated ([A-Za-z0-9._-]) or "*", so no JSON escaping is
1335+
* needed. Best-effort: a marker failure is logged, never fails the run. */
1336+
static void cr_record_last_run(cbm_store_t *src_store, const char *project, const char **targets,
1337+
int target_count, const cbm_cross_repo_result_t *result) {
1338+
char key[CR_QN_BUF];
1339+
if (!cr_status_key(project, key, sizeof(key))) {
1340+
return;
1341+
}
1342+
size_t cap = CR_PROPS_BUF;
1343+
for (int i = 0; i < target_count; i++) {
1344+
cap += (targets[i] ? strlen(targets[i]) : 0) + CR_COL_4;
1345+
}
1346+
char *json = cbm_alloc(CBM_MEM_CLASS_OTHER, cap);
1347+
if (!json) {
1348+
return;
1349+
}
1350+
char when[CBM_SZ_32];
1351+
time_t now = time(NULL);
1352+
struct tm tm_now;
1353+
if (!cbm_gmtime_r(&now, &tm_now) ||
1354+
strftime(when, sizeof(when), "%Y-%m-%dT%H:%M:%SZ", &tm_now) == 0) {
1355+
when[0] = '\0';
1356+
}
1357+
const char *outcome = result->failed ? "failed" : result->cancelled ? "cancelled" : "complete";
1358+
size_t len = (size_t)snprintf(
1359+
json, cap, "{\"last_run_at\":\"%s\",\"outcome\":\"%s\",\"targets\":[", when, outcome);
1360+
for (int i = 0; i < target_count && len < cap; i++) {
1361+
len += (size_t)snprintf(json + len, cap - len, "%s\"%s\"", i ? "," : "",
1362+
targets[i] ? targets[i] : "");
1363+
}
1364+
if (len < cap) {
1365+
int total = result->http_edges + result->async_edges + result->channel_edges +
1366+
result->grpc_edges + result->graphql_edges + result->trpc_edges;
1367+
len += (size_t)snprintf(json + len, cap - len,
1368+
"],\"projects_scanned\":%d,\"total_cross_edges\":%d,"
1369+
"\"skipped_projects\":%d}",
1370+
result->projects_scanned, total, result->skipped_count);
1371+
}
1372+
if (len >= cap || cbm_store_meta_put(src_store, key, json) != CBM_STORE_OK) {
1373+
cbm_log_warn("cross_repo.status_unrecorded", "project", project);
1374+
}
1375+
cbm_free(CBM_MEM_CLASS_OTHER, json);
1376+
}
1377+
1378+
bool cbm_cross_repo_add_status_json(struct yyjson_mut_doc *doc, struct yyjson_mut_val *parent,
1379+
cbm_store_t *store, const char *project) {
1380+
char key[CR_QN_BUF];
1381+
if (!doc || !parent || !store || !project || !cr_status_key(project, key, sizeof(key))) {
1382+
return false;
1383+
}
1384+
char *raw = NULL;
1385+
int rc = cbm_store_meta_get(store, key, &raw);
1386+
yyjson_doc *recorded = rc == CBM_STORE_OK ? yyjson_read(raw, strlen(raw), 0) : NULL;
1387+
cbm_free(CBM_MEM_CLASS_STORE, raw);
1388+
yyjson_val *recorded_root = recorded ? yyjson_doc_get_root(recorded) : NULL;
1389+
yyjson_mut_val *status = NULL;
1390+
if (yyjson_is_obj(recorded_root)) {
1391+
status = yyjson_val_mut_copy(doc, recorded_root);
1392+
}
1393+
if (!status) {
1394+
status = yyjson_mut_obj(doc);
1395+
}
1396+
yyjson_doc_free(recorded);
1397+
if (!status) {
1398+
return false;
1399+
}
1400+
/* "never_run" is the honest reading of an absent marker: no run with this
1401+
* project as the source has completed its cleanup since the store was
1402+
* (re)built. A target-side run still leaves its CROSS_* edges here. */
1403+
const char *state = rc == CBM_STORE_NOT_FOUND ? "never_run"
1404+
: yyjson_mut_obj_size(status) > 0 ? "ran"
1405+
: "unavailable";
1406+
yyjson_mut_obj_add_str(doc, status, "status", state);
1407+
return yyjson_mut_obj_add_val(doc, parent, "cross_repo", status);
1408+
}
1409+
1410+
void cbm_cross_repo_result_free(cbm_cross_repo_result_t *result) {
1411+
if (!result) {
1412+
return;
1413+
}
1414+
for (int i = 0; i < result->skipped_count; i++) {
1415+
cbm_free(CBM_MEM_CLASS_OTHER, result->skipped_projects[i].project);
1416+
}
1417+
cbm_free(CBM_MEM_CLASS_OTHER, result->skipped_projects);
1418+
result->skipped_projects = NULL;
1419+
result->skipped_count = 0;
1420+
}
1421+
12761422
static cr_run_status_t add_match_count(int *total, cr_match_result_t matched) {
12771423
*total += matched.count;
12781424
return matched.status;
@@ -1304,7 +1450,7 @@ cbm_cross_repo_result_t cbm_cross_repo_match_cancellable(const char *project,
13041450
int resolved_count = 0;
13051451

13061452
if (target_count == SKIP_ONE && target_projects[0] && strcmp(target_projects[0], "*") == 0) {
1307-
resolved_count = collect_all_projects(&resolved, &run);
1453+
resolved_count = collect_all_projects(&resolved, &run, &result);
13081454
} else {
13091455
resolved_count = collect_named_projects(target_projects, target_count, &resolved, &run);
13101456
}
@@ -1442,6 +1588,7 @@ cbm_cross_repo_result_t cbm_cross_repo_match_cancellable(const char *project,
14421588
result.projects_scanned++;
14431589
}
14441590

1591+
cr_record_last_run(src_store, project, target_projects, target_count, &result);
14451592
cbm_store_close(src_store);
14461593

14471594
free_project_list(resolved, resolved_count);

‎src/pipeline/pass_cross_repo.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
#include <stdatomic.h>
1111

12+
/* A store the ["*"] enumeration skipped instead of linking. */
13+
typedef struct {
14+
char *project; /* heap-owned */
15+
const char *reason; /* static, e.g. "pre_768_schema" (reindex required) */
16+
} cbm_cross_repo_skip_t;
17+
1218
/* Result of a cross-repo matching run. */
1319
typedef struct {
1420
int http_edges; /* CROSS_HTTP_CALLS edges created */
@@ -23,8 +29,26 @@ typedef struct {
2329
bool no_targets; /* failed: nothing but the source project resolved (#1133) */
2430
bool cancelled; /* stopped at a bounded cancellation checkpoint */
2531
bool partial_results; /* committed writes before cancellation were retained */
32+
cbm_cross_repo_skip_t *skipped_projects; /* ["*"] only; free with ..._result_free */
33+
int skipped_count;
2634
} cbm_cross_repo_result_t;
2735

36+
/* Release the heap-owned parts of a result (safe on a zeroed result). */
37+
void cbm_cross_repo_result_free(cbm_cross_repo_result_t *result);
38+
39+
struct yyjson_mut_doc;
40+
struct yyjson_mut_val;
41+
42+
/* Add a "cross_repo" object to `parent` describing the last cross-repo run
43+
* with `project` as the source, as recorded in that project's store:
44+
* {"status":"never_run"} -- no run recorded since the store was (re)built
45+
* {"status":"ran","last_run_at","outcome","targets","projects_scanned",
46+
* "total_cross_edges","skipped_projects"}
47+
* Read-only (works on a query store). Returns false only on invalid input or
48+
* allocation failure. */
49+
bool cbm_cross_repo_add_status_json(struct yyjson_mut_doc *doc, struct yyjson_mut_val *parent,
50+
cbm_store_t *store, const char *project);
51+
2852
/* Run cross-repo matching for `project` against `target_projects`.
2953
* If target_count == 1 and target_projects[0] == "*", matches against all
3054
* indexed projects. Writes CROSS_* edges bidirectionally into both the

0 commit comments

Comments
 (0)