|
13 | 13 | #include "pipeline/pipeline_internal.h" // cbm_route_canon_path |
14 | 14 | #include "foundation/constants.h" |
15 | 15 | #include "foundation/log.h" |
| 16 | +#include "foundation/mem_core.h" |
16 | 17 | #include "foundation/platform.h" |
17 | 18 | #include "foundation/compat.h" |
18 | 19 | #include "foundation/compat_fs.h" |
19 | 20 | #include "foundation/str_util.h" |
20 | 21 |
|
21 | 22 | #include <sqlite3/sqlite3.h> |
| 23 | +#include <yyjson/yyjson.h> |
22 | 24 | #include <stdint.h> |
23 | 25 | #include <stdio.h> |
24 | 26 | #include <stdlib.h> |
@@ -153,27 +155,38 @@ static bool cr_store_has_exact_project(cbm_store_t *store, const char *project) |
153 | 155 | return matches; |
154 | 156 | } |
155 | 157 |
|
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 |
158 | 166 | * cr_open_existing_project requires. Checking only the first let a pre-#768 |
159 | 167 | * store (still readable, so list_projects shows it) pass validation and the |
160 | 168 | * ["*"] enumeration, then abort the whole run at its write open — after the |
161 | 169 | * source's previous CROSS_* generation had already been deleted. The probe is |
162 | 170 | * 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) { |
164 | 172 | char path[CR_PATH_BUF]; |
165 | 173 | if (!cr_db_path(project, path, sizeof(path))) { |
166 | | - return false; |
| 174 | + return CR_PROJECT_MISSING; |
167 | 175 | } |
168 | 176 | 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)) { |
171 | 180 | cbm_log_warn("cross_repo.project_unusable", "project", project, "reason", |
172 | 181 | "pre_768_schema_reindex_required"); |
173 | | - exists = false; |
| 182 | + state = CR_PROJECT_LEGACY; |
174 | 183 | } |
175 | 184 | 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; |
177 | 190 | } |
178 | 191 |
|
179 | 192 | 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 * |
1139 | 1152 |
|
1140 | 1153 | static void free_project_list(char **projects, int count); |
1141 | 1154 |
|
| 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 | + |
1142 | 1179 | /* 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) { |
1144 | 1182 | *out = NULL; |
1145 | 1183 | const char *dir = cr_cache_dir(); |
1146 | 1184 | cbm_dir_t *d = cbm_opendir(dir); |
@@ -1188,7 +1226,15 @@ static int collect_all_projects(char ***out, cr_run_context_t *ctx) { |
1188 | 1226 | } |
1189 | 1227 | memcpy(project, ent->name, project_length); |
1190 | 1228 | 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) { |
1192 | 1238 | continue; |
1193 | 1239 | } |
1194 | 1240 | if (count >= cap) { |
@@ -1273,6 +1319,106 @@ static int collect_named_projects(const char **targets, int target_count, char * |
1273 | 1319 | return unique_count; |
1274 | 1320 | } |
1275 | 1321 |
|
| 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 | + |
1276 | 1422 | static cr_run_status_t add_match_count(int *total, cr_match_result_t matched) { |
1277 | 1423 | *total += matched.count; |
1278 | 1424 | return matched.status; |
@@ -1304,7 +1450,7 @@ cbm_cross_repo_result_t cbm_cross_repo_match_cancellable(const char *project, |
1304 | 1450 | int resolved_count = 0; |
1305 | 1451 |
|
1306 | 1452 | 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); |
1308 | 1454 | } else { |
1309 | 1455 | resolved_count = collect_named_projects(target_projects, target_count, &resolved, &run); |
1310 | 1456 | } |
@@ -1442,6 +1588,7 @@ cbm_cross_repo_result_t cbm_cross_repo_match_cancellable(const char *project, |
1442 | 1588 | result.projects_scanned++; |
1443 | 1589 | } |
1444 | 1590 |
|
| 1591 | + cr_record_last_run(src_store, project, target_projects, target_count, &result); |
1445 | 1592 | cbm_store_close(src_store); |
1446 | 1593 |
|
1447 | 1594 | free_project_list(resolved, resolved_count); |
|
0 commit comments