Skip to content

Commit 2b4084f

Browse files
authored
Merge pull request #2247 from DeusData/distill/1789-artifact-error-surfacing
fix(mcp): name the artifact phase when a post-publish export fails
2 parents 13ce2c0 + fa22921 commit 2b4084f

4 files changed

Lines changed: 110 additions & 3 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11414,9 +11414,25 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1141411414
yyjson_mut_obj_add_strcpy(doc, root, "message", message);
1141511415
} else {
1141611416
yyjson_mut_obj_add_str(doc, root, "status", "error");
11417-
yyjson_mut_obj_add_str(doc, root, "hint",
11418-
"Pipeline failed. Check repo_path exists and contains source files. "
11419-
"Try mode='fast' for a quicker diagnostic run.");
11417+
/* #1665: a post-publish artifact export failure (read-only repo, etc.)
11418+
* must not be blamed on the repository path. The pipeline snapshots the
11419+
* export error of THIS run, so its presence names the phase exactly —
11420+
* the graph database was already published when the export ran. */
11421+
const char *export_error = cbm_pipeline_export_error(p);
11422+
if (export_error && export_error[0]) {
11423+
char hint[CBM_SZ_1K];
11424+
(void)snprintf(
11425+
hint, sizeof(hint),
11426+
"Index database was published, but the persistence artifact export failed for "
11427+
"repo_path/.codebase-memory (%s). Use a writable checkout, or re-run with "
11428+
"--persistence false if the shared artifact is not needed.",
11429+
export_error);
11430+
yyjson_mut_obj_add_strcpy(doc, root, "hint", hint);
11431+
} else {
11432+
yyjson_mut_obj_add_str(doc, root, "hint",
11433+
"Pipeline failed. Check repo_path exists and contains source "
11434+
"files. Try mode='fast' for a quicker diagnostic run.");
11435+
}
1142011436
}
1142111437

1142211438
char *json = yy_doc_to_str(doc);

‎src/pipeline/pipeline.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ struct cbm_pipeline {
193193
cbm_index_resource_policy_t resource_policy;
194194
cbm_index_resource_violation_t resource_violation;
195195

196+
/* Snapshot of the artifact export failure of THIS run (set only by
197+
* export_after_publish failure, zeroed at run start, cleared on success).
198+
* The MCP layer reads it to attribute a failed run to artifact export
199+
* without consulting cbm_artifact_export_last_error() directly — that
200+
* global can still hold a PREVIOUS run's error. */
201+
char export_error[CBM_SZ_1K];
202+
196203
/* Indexing state (set during run) */
197204
cbm_gbuf_t *gbuf;
198205
cbm_registry_t *registry;
@@ -389,6 +396,10 @@ void cbm_pipeline_get_resource_violation(const cbm_pipeline_t *p,
389396
}
390397
}
391398

399+
const char *cbm_pipeline_export_error(const cbm_pipeline_t *p) {
400+
return p ? p->export_error : "";
401+
}
402+
392403
bool cbm_pipeline_set_project_name(cbm_pipeline_t *p, const char *name) {
393404
if (!p || !name || !name[0]) {
394405
return false;
@@ -3162,6 +3173,12 @@ static int export_after_publish(cbm_pipeline_t *p, const char *final_path) {
31623173
if (rc != 0) {
31633174
const char *err = cbm_artifact_export_last_error();
31643175
cbm_log_error("pipeline.err", "phase", "artifact_export", "err", err ? err : "unknown");
3176+
/* #1665: snapshot the error of THIS run so the MCP layer can
3177+
* attribute the failure truthfully, instead of re-reading the
3178+
* process-global export error (which may describe a previous run)
3179+
* and instead of the generic "Pipeline failed" hint that blames
3180+
* repo_path for a write-permission failure. */
3181+
(void)snprintf(p->export_error, sizeof(p->export_error), "%s", err ? err : "unknown");
31653182
}
31663183
return rc;
31673184
}
@@ -3397,6 +3414,7 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
33973414
if (!p) {
33983415
return CBM_NOT_FOUND;
33993416
}
3417+
p->export_error[0] = '\0';
34003418
char *final_path = resolve_db_path(p);
34013419
if (!final_path || !ensure_db_parent(final_path)) {
34023420
free(final_path);

‎src/pipeline/pipeline.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ void cbm_pipeline_set_resource_policy(cbm_pipeline_t *p, const cbm_index_resourc
6060
void cbm_pipeline_get_resource_violation(const cbm_pipeline_t *p,
6161
cbm_index_resource_violation_t *violation);
6262

63+
/* Snapshot of the artifact export failure of the last cbm_pipeline_run, or ""
64+
* when the run succeeded / did not reach post-publish export. Used to
65+
* truthfully attribute a failed run to the persistence export (#1665) instead
66+
* of the generic pipeline-error hint. Valid until the next cbm_pipeline_run or
67+
* cbm_pipeline_free(). Returns "" for NULL p. */
68+
const char *cbm_pipeline_export_error(const cbm_pipeline_t *p);
69+
6370
/* Free a pipeline and all its internal state. NULL-safe. */
6471
void cbm_pipeline_free(cbm_pipeline_t *p);
6572

‎tests/test_pipeline.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,71 @@ TEST(pipeline_structure_nodes) {
637637
* ADR before the delete and restores it after the rebuild. Reproduce-first:
638638
* index, store an ADR, force a full re-index by adding files, assert the ADR
639639
* is still present and unchanged. */
640+
/* #1665: a persistence artifact export failure must not be reported as a
641+
* repo_path problem. Create the artifact directory as a regular FILE so
642+
* cbm_mkdir_p(.codebase-memory) fails deterministically (works as root too),
643+
* run with persistence enabled, and assert: the run fails, the DB was
644+
* published (publish happens before export), the pipeline snapshots the export
645+
* error, and a clean re-run leaves the snapshot empty. */
646+
TEST(pipeline_export_error_snapshot_on_artifact_failure) {
647+
char tmp[256];
648+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_export1665_XXXXXX");
649+
if (!cbm_mkdtemp(tmp)) {
650+
FAIL("failed to create temp dir");
651+
}
652+
653+
char path[512];
654+
snprintf(path, sizeof(path), "%s/main.py", tmp);
655+
FILE *f = fopen(path, "w");
656+
ASSERT_NOT_NULL(f);
657+
fprintf(f, "def foo():\n pass\n");
658+
fclose(f);
659+
660+
char db_path[512];
661+
snprintf(db_path, sizeof(db_path), "%s/test.db", tmp);
662+
663+
/* Block the artifact directory: a FILE at .codebase-memory makes the
664+
* export's mkdir_p fail with a deterministic error on every platform. */
665+
char art_block[512];
666+
snprintf(art_block, sizeof(art_block), "%s/.codebase-memory", tmp);
667+
f = fopen(art_block, "w");
668+
ASSERT_NOT_NULL(f);
669+
fprintf(f, "block\n");
670+
fclose(f);
671+
672+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
673+
ASSERT_NOT_NULL(p);
674+
cbm_pipeline_set_persistence(p, true);
675+
676+
int rc = cbm_pipeline_run(p);
677+
ASSERT_NEQ(rc, 0);
678+
679+
/* The export runs after publish, so the DB must exist despite the failure. */
680+
struct stat db_st;
681+
ASSERT_EQ(stat(db_path, &db_st), 0);
682+
683+
const char *export_error = cbm_pipeline_export_error(p);
684+
ASSERT_NOT_NULL(export_error);
685+
ASSERT_TRUE(export_error[0] != '\0');
686+
ASSERT_NOT_NULL(strstr(export_error, "prepare_artifact_dir"));
687+
cbm_pipeline_free(p);
688+
689+
/* Control: remove the blocker; a fresh run succeeds and leaves no snapshot. */
690+
(void)cbm_unlink(art_block);
691+
cbm_pipeline_t *p2 = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
692+
ASSERT_NOT_NULL(p2);
693+
cbm_pipeline_set_persistence(p2, true);
694+
rc = cbm_pipeline_run(p2);
695+
ASSERT_EQ(rc, 0);
696+
const char *export_error2 = cbm_pipeline_export_error(p2);
697+
ASSERT_NOT_NULL(export_error2);
698+
ASSERT_TRUE(export_error2[0] == '\0');
699+
cbm_pipeline_free(p2);
700+
701+
rm_rf(tmp);
702+
PASS();
703+
}
704+
640705
TEST(pipeline_adr_survives_full_reindex) {
641706
char tmp[256];
642707
snprintf(tmp, sizeof(tmp), "/tmp/cbm_adr_XXXXXX");
@@ -15780,6 +15845,7 @@ SUITE(pipeline) {
1578015845
RUN_TEST(pipeline_structure_nodes);
1578115846
RUN_TEST(pipeline_committed_counts_match_persisted);
1578215847
RUN_TEST(pipeline_adr_survives_full_reindex);
15848+
RUN_TEST(pipeline_export_error_snapshot_on_artifact_failure);
1578315849
RUN_TEST(pipeline_structure_edges);
1578415850
RUN_TEST(pipeline_branch_root_structure);
1578515851
RUN_TEST(pipeline_project_name_derived);

0 commit comments

Comments
 (0)