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.

fix(mcp): preserve deletion snapshot cursors

Martin Vogelf30ae39eb7f2
diff --git a/src/foundation/compat_fs.c b/src/foundation/compat_fs.c
index 2610c421f..bc1b06ac3 100644
--- a/src/foundation/compat_fs.c
+++ b/src/foundation/compat_fs.c
@@ -123,17 +123,20 @@ cbm_dirent_t *cbm_readdir(cbm_dir_t *d) {
int cbm_path_info_utf8(const char *path, cbm_path_info_t *out) {
if (!path || !out) {
- return CBM_NOT_FOUND;
+ return CBM_PATH_INFO_UNAVAILABLE;
}
wchar_t *wpath = cbm_path_to_wide(path);
if (!wpath) {
- return CBM_NOT_FOUND;
+ return CBM_PATH_INFO_UNAVAILABLE;
}
WIN32_FILE_ATTRIBUTE_DATA data;
BOOL ok = GetFileAttributesExW(wpath, GetFileExInfoStandard, &data);
+ DWORD path_error = ok ? ERROR_SUCCESS : GetLastError();
free(wpath);
if (!ok) {
- return CBM_NOT_FOUND;
+ return path_error == ERROR_FILE_NOT_FOUND || path_error == ERROR_PATH_NOT_FOUND
+ ? CBM_PATH_INFO_ABSENT
+ : CBM_PATH_INFO_UNAVAILABLE;
}
memset(out, 0, sizeof(*out));
out->is_directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
@@ -154,7 +157,7 @@ int cbm_path_info_utf8(const char *path, cbm_path_info_t *out) {
written >= windows_to_unix_ticks
? (int64_t)((written - windows_to_unix_ticks) * NANOSECONDS_PER_WINDOWS_TICK)
: 0;
- return 0;
+ return CBM_PATH_INFO_OK;
}
void cbm_closedir(cbm_dir_t *d) {
@@ -795,11 +798,12 @@ cbm_dirent_t *cbm_readdir(cbm_dir_t *d) {
int cbm_path_info_utf8(const char *path, cbm_path_info_t *out) {
if (!path || !out) {
- return CBM_NOT_FOUND;
+ return CBM_PATH_INFO_UNAVAILABLE;
}
struct stat state;
if (lstat(path, &state) != 0) {
- return CBM_NOT_FOUND;
+ return errno == ENOENT || errno == ENOTDIR ? CBM_PATH_INFO_ABSENT
+ : CBM_PATH_INFO_UNAVAILABLE;
}
memset(out, 0, sizeof(*out));
out->is_regular = S_ISREG(state.st_mode);
@@ -813,7 +817,7 @@ int cbm_path_info_utf8(const char *path, cbm_path_info_t *out) {
out->mtime_ns =
((int64_t)state.st_mtim.tv_sec * INT64_C(1000000000)) + (int64_t)state.st_mtim.tv_nsec;
#endif
- return 0;
+ return CBM_PATH_INFO_OK;
}
void cbm_closedir(cbm_dir_t *d) {
diff --git a/src/foundation/compat_fs.h b/src/foundation/compat_fs.h
index 240ebceae..8f0d16388 100644
--- a/src/foundation/compat_fs.h
+++ b/src/foundation/compat_fs.h
@@ -36,7 +36,15 @@ typedef struct {
int64_t mtime_ns;
} cbm_path_info_t;
-/* Returns 0 on success and -1 when the path cannot be inspected. */
+enum {
+ CBM_PATH_INFO_OK = 0,
+ CBM_PATH_INFO_UNAVAILABLE = -1,
+ CBM_PATH_INFO_ABSENT = -2,
+};
+
+/* Distinguishes a proven absent path from permission, encoding, and transient
+ * inspection failures. Existing callers that only test zero/nonzero retain
+ * their behavior. */
int cbm_path_info_utf8(const char *path, cbm_path_info_t *out);
/* Open a directory for iteration. Returns NULL on error. */
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 768a6c232..939aa3226 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -14921,12 +14921,15 @@ static bool detect_snapshot_add_changed_file(cbm_mcp_server_t *srv, cbm_sha256_c
memcpy(absolute + root_len + 1U, relative_path, relative_len + 1U);
cbm_path_info_t info = {0};
- if (cbm_path_info_utf8(absolute, &info) != 0) {
- /* A missing path can be a legitimate deletion, but the portable
- * metadata API cannot distinguish it from permission and transient
- * inspection failures on every supported platform. Hash the observed
- * state for diagnostics, but do not issue a cursor from incomplete
- * evidence. Offset paging remains available. */
+ int info_status = cbm_path_info_utf8(absolute, &info);
+ if (info_status == CBM_PATH_INFO_ABSENT) {
+ detect_snapshot_add_field(hash, "absent");
+ free(absolute);
+ return true;
+ }
+ if (info_status != CBM_PATH_INFO_OK) {
+ /* Hash the observed failure state for diagnostics, but do not issue a
+ * cursor from incomplete evidence. Offset paging remains available. */
detect_snapshot_add_field(hash, "metadata_unavailable");
free(absolute);
return false;
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index 4fcb14690..ffc65d245 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -13398,7 +13398,35 @@ TEST(tool_detect_changes_pages_changed_files_and_honors_semantic_budget) {
yyjson_doc_free(doc);
free(inner);
free(response);
+ ASSERT_EQ(cbm_unlink(changed_link), 0);
#endif
+
+ /* A tracked deletion is complete snapshot evidence, not an inspection
+ * failure. Keep enough other changes to require pagination and prove a
+ * deleted path can still mint the opaque continuation. */
+ char deleted_path[CBM_SZ_4K];
+ snprintf(deleted_path, sizeof(deleted_path), "%s/deleted.c", repo);
+ ASSERT_EQ(th_write_file(deleted_path, "int deleted;\n"), 0);
+ const char *const add_deleted_args[] = {"add", "--", "deleted.c", NULL};
+ ASSERT_EQ(mcp_test_git(repo, add_deleted_args), 0);
+ ASSERT_EQ(mcp_test_git(repo, commit_args), 0);
+ ASSERT_EQ(cbm_unlink(deleted_path), 0);
+ response = cbm_mcp_handle_tool(
+ srv, "detect_changes",
+ "{\"project\":\"detect-pages-project\",\"base_branch\":\"HEAD\","
+ "\"scope\":\"files\",\"format\":\"json\"}");
+ ASSERT_NOT_NULL(response);
+ inner = extract_text_content(response);
+ ASSERT_NOT_NULL(inner);
+ doc = yyjson_read(inner, strlen(inner), 0);
+ ASSERT_NOT_NULL(doc);
+ root = yyjson_doc_get_root(doc);
+ ASSERT_EQ(yyjson_get_int(yyjson_obj_get(root, "changed_total")), 26);
+ ASSERT_NULL(yyjson_obj_get(root, "snapshot_cursor_unavailable"));
+ ASSERT_TRUE(yyjson_is_str(yyjson_obj_get(root, "changed_next_cursor")));
+ yyjson_doc_free(doc);
+ free(inner);
+ free(response);
free(changed_cursor);
cbm_mcp_server_free(srv);