Skip to content

Commit 1067e51

Browse files
junk151516claude
andcommitted
fix(graph-buffer): log why an atomic publish failed instead of a successful dump
cbm_gbuf_dump_to_sqlite() emitted gbuf.dump regardless of the writer's result, so a run that published nothing still logged node and edge counts as if it had worked, and the failure reached the user only as the generic "Pipeline failed. Check repo_path exists and contains source files." The writer's temp -> final rename in publish_writer_output() returned a bare ERR_WRITE_FAILED. DeusData#1628 taught cbm_rename_replace() to translate the platform error into errno so callers could report why, and wired up the stage -> final rename in pipeline.c. This is the other rename on that path, and it is the one that runs first. Preserve errno across the cleanup in every publish error branch. That includes cbm_writer_open(), where the cleanup unlinks a file that was never created: without the save it leaves ENOENT behind, so a directory that denied the create is reported as a missing path — the DeusData#1620 case, stated wrongly. Read the value in graph_buffer.c before the profiling macro can clobber it, and report gbuf.dump_failed with the return code and that errno. A sticky append failure clears errno rather than attach a reason it does not have, and the field is omitted when there is none. No logging is added inside internal/cbm/, which has none today. Tests cover both halves: the dump reports a failure instead of a summary, the writer names its truncation reason, and the publish rename's errno survives a cleanup that itself fails. Refs DeusData#1620, DeusData#2001. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012FxkVF773gDGNmBmsLnzN7 Signed-off-by: juan Carlos Estrada Montoya <[email protected]>
1 parent ffb5ebe commit 1067e51

4 files changed

Lines changed: 233 additions & 1 deletion

File tree

‎internal/cbm/sqlite_writer.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string.h>
2727
#include <stdint.h>
2828
#include <stdbool.h>
29+
#include <errno.h>
2930

3031
#ifdef _WIN32
3132
#include <io.h>
@@ -1759,13 +1760,18 @@ static int sync_writer_output(FILE *fp) {
17591760
}
17601761

17611762
static int discard_writer_output(write_db_ctx_t *w, int rc) {
1763+
/* Cleanup runs after the failure that brought us here, and a library
1764+
* call may set errno even when it succeeds. Carry the reason across it
1765+
* so the caller can report WHY the publish failed. */
1766+
int failure_errno = errno;
17621767
if (w->fp) {
17631768
(void)fclose(w->fp);
17641769
w->fp = NULL;
17651770
}
17661771
if (w->temp_path[0]) {
17671772
(void)cbm_unlink(w->temp_path);
17681773
}
1774+
errno = failure_errno;
17691775
return rc;
17701776
}
17711777

@@ -1774,18 +1780,25 @@ static int publish_writer_output(write_db_ctx_t *w) {
17741780
return discard_writer_output(w, ERR_WRITE_FAILED);
17751781
}
17761782
if (fclose(w->fp) != 0) {
1783+
int failure_errno = errno;
17771784
w->fp = NULL;
17781785
if (w->temp_path[0]) {
17791786
(void)cbm_unlink(w->temp_path);
17801787
}
1788+
errno = failure_errno;
17811789
return ERR_WRITE_FAILED;
17821790
}
17831791
w->fp = NULL;
17841792
if (!w->temp_path[0] || !w->final_path[0]) {
17851793
return 0;
17861794
}
17871795
if (cbm_rename_replace(w->temp_path, w->final_path) != 0) {
1796+
/* cbm_rename_replace translated the platform error into errno so the
1797+
* caller can say what denied the publish (#1620). Preserve it across
1798+
* the cleanup unlink. */
1799+
int rename_errno = errno;
17881800
(void)cbm_unlink(w->temp_path);
1801+
errno = rename_errno;
17891802
return ERR_WRITE_FAILED;
17901803
}
17911804
/* Sidecars are removed only after the replacement succeeds. On POSIX,
@@ -2283,13 +2296,22 @@ cbm_db_writer_t *cbm_writer_open(const char *path) {
22832296
int n = snprintf(w->wc.final_path, sizeof(w->wc.final_path), "%s", path);
22842297
if (n < 0 || (size_t)n >= sizeof(w->wc.final_path) ||
22852298
make_writer_temp_path(path, w, w->wc.temp_path, sizeof(w->wc.temp_path)) != 0) {
2299+
/* Both conditions are truncation and neither sets errno; name the
2300+
* reason rather than let the caller report a stale one. */
22862301
free(w);
2302+
errno = ENAMETOOLONG;
22872303
return NULL;
22882304
}
22892305
FILE *fp = cbm_fopen(w->wc.temp_path, "wb");
22902306
if (!fp) {
2307+
/* The cleanup below unlinks a file that was never created, so it
2308+
* fails and leaves ENOENT behind — which reads as a missing path
2309+
* when the real answer is that the directory refused the create.
2310+
* That is the #1620 case, so carry the open's reason across it. */
2311+
int open_errno = errno;
22912312
(void)cbm_unlink(w->wc.temp_path);
22922313
free(w);
2314+
errno = open_errno;
22932315
return NULL;
22942316
}
22952317
w->wc.fp = fp;
@@ -2355,6 +2377,10 @@ int cbm_writer_finalize(cbm_db_writer_t *w, const char *project, const char *roo
23552377
write_db_ctx_t wc = w->wc; /* value copy survives free(w) */
23562378
free(w);
23572379
if (err != 0) {
2380+
/* A sticky append failure: errno belongs to whatever call failed
2381+
* many calls ago, not to the publish. Clear it so the caller does
2382+
* not attach a reason this path does not have. */
2383+
errno = 0;
23582384
return discard_writer_output(&wc, err);
23592385
}
23602386
return write_db_after_nodes(&wc, nodes_root);

‎src/graph_buffer/graph_buffer.c‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum {
3838
#include "foundation/mem.h"
3939
#include <sqlite3.h>
4040

41+
#include <errno.h>
4142
#include <stdatomic.h>
4243
#include <stdint.h> // int64_t
4344
#include <stdio.h>
@@ -1625,6 +1626,32 @@ static void log_dump_summary(int node_count, int edge_count) {
16251626
cbm_log_info("gbuf.dump", "nodes", b1, "edges", b2);
16261627
}
16271628

1629+
/* A dump that published nothing gets exactly one signature in the log.
1630+
* Without it the writer's return code is the only evidence, and it reaches
1631+
* the user as a generic pipeline failure that blames their repository
1632+
* (#1620, #2001).
1633+
*
1634+
* failure_errno is read by the caller at the point of failure: the value is
1635+
* only meaningful before the next library call. Zero means the failure did
1636+
* not originate at the publish boundary — an append that failed many calls
1637+
* earlier — and the field is omitted rather than claim a reason this record
1638+
* does not have. */
1639+
static void log_dump_failed(int rc, int failure_errno, int node_count, int edge_count) {
1640+
char b1[CBM_SZ_16];
1641+
char b3[CBM_SZ_16];
1642+
char b4[CBM_SZ_16];
1643+
snprintf(b1, sizeof(b1), "%d", rc);
1644+
snprintf(b3, sizeof(b3), "%d", node_count);
1645+
snprintf(b4, sizeof(b4), "%d", edge_count);
1646+
if (failure_errno == 0) {
1647+
cbm_log_error("gbuf.dump_failed", "rc", b1, "nodes", b3, "edges", b4);
1648+
return;
1649+
}
1650+
char b2[CBM_SZ_16];
1651+
snprintf(b2, sizeof(b2), "%d", failure_errno);
1652+
cbm_log_error("gbuf.dump_failed", "rc", b1, "errno", b2, "nodes", b3, "edges", b4);
1653+
}
1654+
16281655
static void free_dump_resources(char **url_paths, char **local_names, int edge_count,
16291656
CBMDumpEdge *dump_edges, CBMDumpNode *dump_nodes,
16301657
int64_t *temp_to_final) {
@@ -1680,6 +1707,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
16801707
int64_t max_temp_id = gb->next_id;
16811708
int64_t *temp_to_final = calloc((size_t)max_temp_id, sizeof(int64_t));
16821709
if (!temp_to_final) {
1710+
log_dump_failed(CBM_NOT_FOUND, errno, 0, 0);
16831711
return CBM_NOT_FOUND;
16841712
}
16851713

@@ -1710,6 +1738,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
17101738
* uninitialized budget from ever triggering the free). */
17111739
cbm_db_writer_t *w = cbm_writer_open(path);
17121740
if (!w) {
1741+
log_dump_failed(CBM_NOT_FOUND, errno, node_idx, 0);
17131742
free(src_nodes);
17141743
free(dump_nodes);
17151744
free(temp_to_final);
@@ -1759,12 +1788,19 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
17591788
int frc = cbm_writer_finalize(w, gb->project, gb->root_path, indexed_at, dump_nodes, node_idx,
17601789
dump_edges, edge_idx, gb->dump_vectors, gb->dump_vector_count,
17611790
gb->dump_token_vecs, gb->dump_token_vec_count);
1791+
/* Read errno before anything else can touch it: the profiling macro
1792+
* below is already one library call away from losing the reason. */
1793+
int finalize_errno = errno;
17621794
CBM_PROF_END_N("dump", "6_write_db_finalize", t_finalize, node_idx + edge_idx);
17631795
if (rc == 0) {
17641796
rc = frc;
17651797
}
17661798

1767-
log_dump_summary(node_idx, edge_idx);
1799+
if (rc != 0) {
1800+
log_dump_failed(rc, finalize_errno, node_idx, edge_idx);
1801+
} else {
1802+
log_dump_summary(node_idx, edge_idx);
1803+
}
17681804
free_dump_resources(url_paths, local_names, edge_idx, dump_edges, dump_nodes, temp_to_final);
17691805
free(src_nodes);
17701806
return rc;

‎tests/test_graph_buffer.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "test_framework.h"
88
#include "graph_buffer/graph_buffer.h"
99
#include "store/store.h"
10+
#include "../src/foundation/compat.h"
11+
#include "foundation/compat_fs.h"
12+
#include "foundation/log.h"
13+
#include <stdio.h>
1014
#include <string.h>
1115

1216
/* ── Node operations ───────────────────────────────────────────── */
@@ -1118,6 +1122,72 @@ TEST(gbuf_flush_skips_orphan_edges) {
11181122
PASS();
11191123
}
11201124

1125+
/* ── Publish failure reporting ───────────────────────────────── */
1126+
1127+
static char g_log_capture[4096];
1128+
static CBMLogLevel g_prev_log_level;
1129+
static CBMLogFormat g_prev_log_format;
1130+
1131+
static void capture_log_sink(const char *line) {
1132+
size_t used = strlen(g_log_capture);
1133+
size_t avail = sizeof(g_log_capture) - used;
1134+
if (avail <= 1) {
1135+
return;
1136+
}
1137+
int n = snprintf(g_log_capture + used, avail, "%s\n", line);
1138+
if (n < 0 || (size_t)n >= avail) {
1139+
g_log_capture[sizeof(g_log_capture) - 1] = '\0';
1140+
}
1141+
}
1142+
1143+
static void capture_logs_start(void) {
1144+
g_log_capture[0] = '\0';
1145+
g_prev_log_level = cbm_log_get_level();
1146+
g_prev_log_format = cbm_log_get_format();
1147+
cbm_log_set_level(CBM_LOG_DEBUG);
1148+
/* The assertions below read the text encoding, so pin it rather than
1149+
* inherit whatever CBM_LOG_FORMAT left set. */
1150+
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
1151+
cbm_log_set_sink(capture_log_sink);
1152+
}
1153+
1154+
static const char *capture_logs_end(void) {
1155+
cbm_log_set_sink(NULL);
1156+
cbm_log_set_level(g_prev_log_level);
1157+
cbm_log_set_format(g_prev_log_format);
1158+
return g_log_capture;
1159+
}
1160+
1161+
/* A dump that publishes nothing has to say so, and say why. Renaming onto an
1162+
* existing directory is how test_sqlite_writer already forces the publish to
1163+
* fail; here it stands in for any host that denies the rename (#1620). */
1164+
TEST(gbuf_dump_failure_logs_reason) {
1165+
char dir[256];
1166+
snprintf(dir, sizeof(dir), "/tmp/cbm_gbuf_pub_XXXXXX");
1167+
ASSERT_NOT_NULL(cbm_mkdtemp(dir));
1168+
1169+
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/repo");
1170+
ASSERT_NOT_NULL(gb);
1171+
int64_t id = cbm_gbuf_upsert_node(gb, "Function", "main", "pkg.main", "main.go", 1, 10, "{}");
1172+
ASSERT_GT(id, 0);
1173+
1174+
capture_logs_start();
1175+
int rc = cbm_gbuf_dump_to_sqlite(gb, dir);
1176+
const char *logs = capture_logs_end();
1177+
1178+
ASSERT(rc != 0);
1179+
ASSERT_NOT_NULL(strstr(logs, "gbuf.dump_failed"));
1180+
/* The reason survived the cleanup unlink. */
1181+
ASSERT_NOT_NULL(strstr(logs, "errno="));
1182+
ASSERT(strstr(logs, "errno=0 ") == NULL);
1183+
/* And the run is not also reported as a successful dump. */
1184+
ASSERT(strstr(logs, "msg=gbuf.dump ") == NULL);
1185+
1186+
cbm_gbuf_free(gb);
1187+
cbm_rmdir(dir);
1188+
PASS();
1189+
}
1190+
11211191
/* ── Suite ─────────────────────────────────────────────────────── */
11221192

11231193
SUITE(graph_buffer) {
@@ -1188,4 +1258,7 @@ SUITE(graph_buffer) {
11881258
RUN_TEST(gbuf_shared_ids_null_fallback);
11891259
RUN_TEST(gbuf_next_id_set_next_id_roundtrip);
11901260
RUN_TEST(gbuf_next_id_null_safe);
1261+
1262+
/* Publish failure reporting */
1263+
RUN_TEST(gbuf_dump_failure_logs_reason);
11911264
}

‎tests/test_sqlite_writer.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/* sqlite_writer.h is at internal/cbm/ — Makefile adds -Iinternal/cbm */
1414
#include "sqlite_writer.h" /* CBMDumpNode, CBMDumpEdge, cbm_write_db */
1515
#include "sqlite3.h" /* vendored/sqlite3/ via -Ivendored/sqlite3 */
16+
#include <errno.h>
1617
#include <unistd.h>
1718

1819
/* ── Helper: create temp file path ─────────────────────────────── */
@@ -101,6 +102,45 @@ static int count_temp_outputs_for(const char *path) {
101102
return count;
102103
}
103104

105+
/* Locate the writer's temp output for `path`. Same naming rule as
106+
* count_temp_outputs_for, but returns the full path of the first match. */
107+
static int find_temp_output_for(const char *path, char *out, size_t out_size) {
108+
char dir[256];
109+
char base[256];
110+
const char *slash = strrchr(path, '/');
111+
if (slash) {
112+
size_t dir_len = (size_t)(slash - path);
113+
if (dir_len == 0 || dir_len >= sizeof(dir)) {
114+
return -1;
115+
}
116+
memcpy(dir, path, dir_len);
117+
dir[dir_len] = '\0';
118+
snprintf(base, sizeof(base), "%s", slash + 1);
119+
} else {
120+
snprintf(dir, sizeof(dir), ".");
121+
snprintf(base, sizeof(base), "%s", path);
122+
}
123+
124+
cbm_dir_t *d = cbm_opendir(dir);
125+
if (!d) {
126+
return -1;
127+
}
128+
size_t base_len = strlen(base);
129+
int found = -1;
130+
cbm_dirent_t *ent;
131+
while ((ent = cbm_readdir(d)) != NULL) {
132+
size_t name_len = strlen(ent->name);
133+
if (name_len > base_len + 5 && strncmp(ent->name, base, base_len) == 0 &&
134+
strncmp(ent->name + base_len, ".tmp.", 5) == 0) {
135+
snprintf(out, out_size, "%s/%s", dir, ent->name);
136+
found = 0;
137+
break;
138+
}
139+
}
140+
cbm_closedir(d);
141+
return found;
142+
}
143+
104144
/* ── Tests ─────────────────────────────────────────────────────── */
105145

106146
TEST(sw_minimal_data) {
@@ -877,6 +917,61 @@ TEST(sw_publish_preserves_live_reader) {
877917

878918
/* ── Suite ─────────────────────────────────────────────────────── */
879919

920+
TEST(sw_open_truncated_path_names_its_reason) {
921+
/* final_path is a fixed 4K buffer and make_writer_temp_path only fails by
922+
* truncation. Neither sets an errno of its own, so without naming one the
923+
* caller reports whatever was left behind by an unrelated call. */
924+
char longpath[5000];
925+
memset(longpath, 'a', sizeof(longpath) - 1);
926+
longpath[sizeof(longpath) - 1] = '\0';
927+
longpath[0] = '/';
928+
929+
errno = 0;
930+
cbm_db_writer_t *w = cbm_writer_open(longpath);
931+
ASSERT(w == NULL);
932+
ASSERT_EQ(errno, ENAMETOOLONG);
933+
PASS();
934+
}
935+
936+
TEST(sw_publish_failure_reports_the_rename_not_the_cleanup) {
937+
#ifdef _WIN32
938+
SKIP_PLATFORM("removing a still-open file to make the cleanup unlink fail");
939+
#endif
940+
char dir[256];
941+
snprintf(dir, sizeof(dir), "/tmp/cbm_sw_cleanup_XXXXXX");
942+
ASSERT(cbm_mkdtemp(dir) != NULL);
943+
944+
char final_path[320];
945+
snprintf(final_path, sizeof(final_path), "%s/db.sqlite", dir);
946+
ASSERT_EQ(write_fixture_file(final_path, "destination"), 0);
947+
948+
cbm_db_writer_t *w = cbm_writer_open(final_path);
949+
ASSERT(w != NULL);
950+
951+
/* Turn the still-open temp into a directory. The publish rename then fails
952+
* with ENOTDIR (directory onto a file), and the cleanup unlink fails too
953+
* (EISDIR on Linux, EPERM on macOS) — so only a saved errno still carries
954+
* the rename's reason out to the caller. */
955+
char temp[512];
956+
ASSERT_EQ(find_temp_output_for(final_path, temp, sizeof(temp)), 0);
957+
ASSERT_EQ(cbm_unlink(temp), 0);
958+
ASSERT(cbm_mkdir_p(temp, 0700));
959+
960+
errno = 0;
961+
int rc = cbm_writer_finalize(w, "test", "/tmp/test", "2026-07-07T00:00:00Z", NULL, 0, NULL, 0,
962+
NULL, 0, NULL, 0);
963+
int publish_errno = errno;
964+
ASSERT(rc != 0);
965+
ASSERT_EQ(publish_errno, ENOTDIR);
966+
/* The destination is intact: a failed publish never replaces it. */
967+
ASSERT(fixture_file_equals(final_path, "destination"));
968+
969+
cbm_rmdir(temp);
970+
cbm_unlink(final_path);
971+
cbm_rmdir(dir);
972+
PASS();
973+
}
974+
880975
SUITE(sqlite_writer) {
881976
RUN_TEST(sw_minimal_data);
882977
RUN_TEST(sw_imports_local_name_unique);
@@ -890,4 +985,6 @@ SUITE(sqlite_writer) {
890985
RUN_TEST(sw_publish_failure_preserves_destination_sidecars);
891986
RUN_TEST(sw_publish_supports_non_ascii_path);
892987
RUN_TEST(sw_publish_preserves_live_reader);
988+
RUN_TEST(sw_open_truncated_path_names_its_reason);
989+
RUN_TEST(sw_publish_failure_reports_the_rename_not_the_cleanup);
893990
}

0 commit comments

Comments
 (0)