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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(daemon): address QA round 2
Shane McCarron1133ba324656
diff --git a/src/daemon/ipc.c b/src/daemon/ipc.cindex bedc5884d..a6259e1dd 100644--- a/src/daemon/ipc.c+++ b/src/daemon/ipc.c@@ -3229,9 +3229,29 @@ static int posix_touch_fold(int result, int next) {return result == 0 || next == 0 ? 0 : (result < 0 || next < 0 ? -1 : 1);}+/* Whether base_name still names the regular file (device, inode): 1 yes,+ * 0 proven not (absent, another inode, a symlink or other non-regular+ * entry), -1 undetermined (any other lookup failure, e.g. EIO or ESTALE). */+static int posix_path_names_inode(int directory_fd, const char *base_name, dev_t device,+ ino_t inode) {+ struct stat by_path;+ if (fstatat(directory_fd, base_name, &by_path, AT_SYMLINK_NOFOLLOW) != 0) {+ return errno == ENOENT ? 0 : -1;+ }+ return S_ISREG(by_path.st_mode) && by_path.st_dev == device && by_path.st_ino == inode ? 1 : 0;+}+static int posix_held_file_touch(int directory_fd, const char *base_name, int fd) {- if (!private_regular_file_snapshot(directory_fd, base_name, fd, 1, NULL)) {- return 0;+ struct stat by_handle;+ if (fstat(fd, &by_handle) != 0) {+ return -1;+ }+ int identity =+ by_handle.st_nlink == 0+ ? 0+ : posix_path_names_inode(directory_fd, base_name, by_handle.st_dev, by_handle.st_ino);+ if (identity <= 0) {+ return identity;}return futimens(fd, NULL) == 0 ? 1 : -1;}@@ -3241,20 +3261,27 @@ static int posix_held_lock_touch(int directory_fd, const process_lock_entry_t *e}static int posix_identity_marker_touch(const cbm_daemon_ipc_listener_t *listener) {- /* The marker is not held open; reopen it and require the published inode.- * Only ENOENT proves loss: EMFILE and friends say nothing about the file. */+ /* The marker is not held open. Classify by path first, so a symlink or+ * other replacement is loss; only then reopen it to refresh, where a+ * failure (EMFILE, EACCES) says nothing about the file and is transient. */+ int identity = posix_path_names_inode(listener->dir_fd, listener->socket_identity_name,+ listener->identity_device, listener->identity_inode);+ if (identity <= 0) {+ return identity;+ }int marker_fd = openat(listener->dir_fd, listener->socket_identity_name,O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);if (marker_fd < 0) {return errno == ENOENT ? 0 : -1;}struct stat marker_status;- int result = 0;- if (private_regular_file_snapshot(listener->dir_fd, listener->socket_identity_name, marker_fd,- 1, &marker_status) &&- marker_status.st_dev == listener->identity_device &&- marker_status.st_ino == listener->identity_inode) {- result = futimens(marker_fd, NULL) == 0 ? 1 : -1;+ int result = -1;+ if (fstat(marker_fd, &marker_status) == 0) {+ /* Replaced between the lookup and the open. */+ result = marker_status.st_dev != listener->identity_device ||+ marker_status.st_ino != listener->identity_inode+ ? 0+ : (futimens(marker_fd, NULL) == 0 ? 1 : -1);}(void)close(marker_fd);return result;diff --git a/src/foundation/private_file_lock.c b/src/foundation/private_file_lock.cindex a5fdf797d..46111cc1a 100644--- a/src/foundation/private_file_lock.c+++ b/src/foundation/private_file_lock.c@@ -487,6 +487,39 @@ cbm_private_file_lock_status_t cbm_private_file_lock_payload_write(cbm_private_freturn valid ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_IO;}+/* Whether the lock's canonical path still names the held inode: 1 yes, 0 no+ * (proven lost), -1 undetermined. Only proof counts as loss: unlink leaves+ * st_nlink 0, a rename-away or replacement makes the path name another inode+ * or nothing, and a replaced directory changes the directory's identity. Any+ * other syscall failure (EIO, ESTALE, ENOMEM) says nothing about the file and+ * is reported as undetermined so the caller retries instead of exiting. */+static int private_held_identity(const cbm_private_file_lock_t *lock) {+ const cbm_private_lock_directory_t *directory = lock->directory;+ struct stat by_handle;+ struct stat directory_by_path;+ struct stat by_path;+ if (fstat(lock->fd, &by_handle) != 0) {+ return -1;+ }+ if (by_handle.st_nlink == 0) {+ return 0;+ }+ if (lstat(directory->path, &directory_by_path) != 0) {+ return errno == ENOENT || errno == ENOTDIR ? 0 : -1;+ }+ if (!S_ISDIR(directory_by_path.st_mode) || directory_by_path.st_dev != directory->device ||+ directory_by_path.st_ino != directory->inode) {+ return 0;+ }+ if (fstatat(directory->fd, lock->base_name, &by_path, AT_SYMLINK_NOFOLLOW) != 0) {+ return errno == ENOENT ? 0 : -1;+ }+ return S_ISREG(by_path.st_mode) && by_path.st_dev == by_handle.st_dev &&+ by_path.st_ino == by_handle.st_ino+ ? 1+ : 0;+}+cbm_private_file_lock_status_t cbm_private_file_lock_touch(cbm_private_file_lock_t *lock) {if (!lock) {return CBM_PRIVATE_FILE_LOCK_UNSAFE;@@ -494,17 +527,17 @@ cbm_private_file_lock_status_t cbm_private_file_lock_touch(cbm_private_file_lockif (!cbm_private_file_lock_fork_guard_enter()) {return CBM_PRIVATE_FILE_LOCK_IO;}- /* Unlink leaves st_nlink 0 on the held inode, but a rename-away or a- * replaced directory keeps it linked elsewhere; only the path-vs-handle- * identity check proves the canonical path still names this lock. */- bool linked = private_payload_fd_valid(lock, NULL) && lock->directory &&- private_file_revalidate(lock->directory, lock->base_name, lock->fd, NULL);- bool touched = linked && futimens(lock->fd, NULL) == 0;- cbm_private_file_lock_fork_guard_leave();- if (!linked) {- return CBM_PRIVATE_FILE_LOCK_UNSAFE;+ cbm_private_file_lock_status_t status = CBM_PRIVATE_FILE_LOCK_UNSAFE;+ if (lock->fd >= 0 && !lock->unlocked && lock->owner_pid == getpid() && lock->directory &&+ private_lock_is_tracked(lock)) {+ int identity = private_held_identity(lock);+ status = identity > 0 ? (futimens(lock->fd, NULL) == 0 ? CBM_PRIVATE_FILE_LOCK_OK+ : CBM_PRIVATE_FILE_LOCK_IO)+ : identity < 0 ? CBM_PRIVATE_FILE_LOCK_IO+ : CBM_PRIVATE_FILE_LOCK_UNSAFE;}- return touched ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_IO;+ cbm_private_file_lock_fork_guard_leave();+ return status;}cbm_private_file_lock_status_t cbm_private_file_lock_release(cbm_private_file_lock_t **lock_io) {@@ -1478,15 +1511,21 @@ cbm_private_file_lock_status_t cbm_private_file_lock_payload_write(cbm_private_fcbm_private_file_lock_status_t cbm_private_file_lock_touch(cbm_private_file_lock_t *lock) {/* Windows has no age-based cleaner of the private lock directory; only* confirm the held handle still names a linked file. */- if (!lock) {+ if (!lock || lock->handle == INVALID_HANDLE_VALUE || lock->unlocked) {return CBM_PRIVATE_FILE_LOCK_UNSAFE;}if (!cbm_private_file_lock_fork_guard_enter()) {return CBM_PRIVATE_FILE_LOCK_IO;}- bool linked = private_win_payload_handle_valid(lock);+ BY_HANDLE_FILE_INFORMATION information;+ bool queried = GetFileInformationByHandle(lock->handle, &information) != 0;cbm_private_file_lock_fork_guard_leave();- return linked ? CBM_PRIVATE_FILE_LOCK_OK : CBM_PRIVATE_FILE_LOCK_UNSAFE;+ /* A failed query says nothing about the file; only a proven unlink is loss. */+ if (!queried) {+ return CBM_PRIVATE_FILE_LOCK_IO;+ }+ return information.nNumberOfLinks == 0 ? CBM_PRIVATE_FILE_LOCK_UNSAFE+ : CBM_PRIVATE_FILE_LOCK_OK;}static bool private_win_release_unlock(cbm_private_file_lock_t *lock) {diff --git a/tests/test_daemon_ipc.c b/tests/test_daemon_ipc.cindex 8107a0ef5..5fcf8c1fc 100644--- a/tests/test_daemon_ipc.c+++ b/tests/test_daemon_ipc.c@@ -2072,16 +2072,18 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {#ifdef _WIN32SKIP_PLATFORM("unlink-while-held of runtime files is POSIX behavior");#else- enum { TOUCH_CASES = 4 };+ enum { TOUCH_CASES = 5 };+ enum { LOSS_UNLINK, LOSS_REPLACE_FILE, LOSS_REPLACE_SYMLINK };static const char key[] = "2178a2178a2178a0";static const struct {const char *suffix;- bool replace;+ int loss;} cases[TOUCH_CASES] = {- {"lifetime.lock", false}, /* lifetime reservation */- {"lock", false}, /* listener-owned participant guard */- {"sock.identity", false}, /* identity marker, unlinked */- {"sock.identity", true}, /* identity marker, replaced by a fresh inode */+ {"lifetime.lock", LOSS_UNLINK}, /* lifetime reservation */+ {"lock", LOSS_UNLINK}, /* listener-owned participant guard */+ {"sock.identity", LOSS_UNLINK}, /* identity marker, unlinked */+ {"sock.identity", LOSS_REPLACE_FILE}, /* ... replaced by a fresh inode */+ {"sock.identity", LOSS_REPLACE_SYMLINK} /* ... replaced by a symlink */,};const time_t stale = 1000000000;char parent[TEST_PATH_CAP] = {0};@@ -2091,7 +2093,9 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {int touched_intact[TOUCH_CASES] = {0};bool refreshed[TOUCH_CASES] = {false};bool lost[TOUCH_CASES] = {false};- int touched_after_loss[TOUCH_CASES] = {1, 1, 1, 1};+ int touched_after_loss[TOUCH_CASES] = {1, 1, 1, 1, 1};+ int unreadable_touch = 0;+ int readable_again_touch = 0;if (ipc_test_parent_new(parent, "listener-touch")) {endpoint = cbm_daemon_ipc_endpoint_new(key, parent);@@ -2099,6 +2103,19 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {if (endpoint) {ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));}+ /* A still-valid marker that merely cannot be reopened (EACCES) is a+ * transient refresh failure, not loss: the daemon must keep serving. */+ if (endpoint) {+ cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(endpoint);+ char marker[TEST_PATH_CAP];+ int written = snprintf(marker, sizeof(marker), "%s/cbm-%s.sock.identity", runtime_dir, key);+ if (listener && written > 0 && written < (int)sizeof(marker) && chmod(marker, 0) == 0) {+ unreadable_touch = cbm_daemon_ipc_listener_touch(listener, NULL);+ readable_again_touch =+ chmod(marker, 0600) == 0 ? cbm_daemon_ipc_listener_touch(listener, NULL) : -2;+ }+ cbm_daemon_ipc_listener_close(listener);+ }for (size_t i = 0; endpoint && i < TOUCH_CASES; i++) {cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(endpoint);started[i] = listener != NULL;@@ -2113,16 +2130,30 @@ TEST(daemon_ipc_listener_touch_detects_lost_runtime_files) {refreshed[i] = touched_intact[i] == 1 && stat(path, &after) == 0 &&after.st_atime > stale && after.st_mtime > stale;lost[i] = refreshed[i] && unlink(path) == 0;- if (lost[i] && cases[i].replace) {+ if (lost[i] && cases[i].loss == LOSS_REPLACE_FILE) {int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0600);lost[i] = fd >= 0 && close(fd) == 0;+ } else if (lost[i] && cases[i].loss == LOSS_REPLACE_SYMLINK) {+ lost[i] = symlink("/dev/null", path) == 0;}touched_after_loss[i] = lost[i] ? cbm_daemon_ipc_listener_touch(listener, NULL) : -2;cbm_daemon_ipc_listener_close(listener);+ /* A tampered marker makes close (correctly) keep the socket pair; clear+ * this case's simulated leftovers so the next case can listen. */+ static const char *const leftovers[] = {"sock.identity", "sock", "anc"};+ for (size_t j = 0; j < sizeof(leftovers) / sizeof(leftovers[0]); j++) {+ written = snprintf(path, sizeof(path), "%s/cbm-%s.%s", runtime_dir, key, leftovers[j]);+ if (written > 0 && written < (int)sizeof(path)) {+ (void)unlink(path);+ }+ }}cbm_daemon_ipc_endpoint_free(endpoint);ipc_test_remove_tree(runtime_dir, parent);+ /* Root bypasses mode 0, so the reopen succeeds there and the touch is 1. */+ ASSERT_EQ(unreadable_touch, geteuid() == 0 ? 1 : -1);+ ASSERT_EQ(readable_again_touch, 1);for (size_t i = 0; i < TOUCH_CASES; i++) {ASSERT_TRUE(started[i]);ASSERT_EQ(touched_intact[i], 1);diff --git a/tests/test_version_cohort.c b/tests/test_version_cohort.cindex f40e05c5e..d5b6d4c5a 100644--- a/tests/test_version_cohort.c+++ b/tests/test_version_cohort.c@@ -22,6 +22,8 @@#endif#include <windows.h>#else+#include <fcntl.h>+#include <sys/stat.h>#include <sys/wait.h>#include <unistd.h>#endif@@ -690,9 +692,6 @@ TEST(version_cohort_touch_reports_each_lost_file) {CBM_VERSION_COHORT_OK);ASSERT_EQ(cbm_version_cohort_daemon_claim_acquire(manager, &claim), CBM_VERSION_COHORT_OK);- ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);- ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_OK);-const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(fixture.endpoint);char marker[VERSION_COHORT_TEST_PATH_CAP];char lifetime[VERSION_COHORT_TEST_PATH_CAP];@@ -701,6 +700,20 @@ TEST(version_cohort_touch_reports_each_lost_file) {ASSERT_TRUE(snprintf(lifetime, sizeof(lifetime), "%s/cbm-version-cohort-lifetime-v1.lock",runtime_dir) < (int)sizeof(lifetime));+ /* Age both held files the way a cleaner would see them, then prove the+ * touch refreshed them through the held handles. */+ const time_t stale = 1000000000;+ struct timespec stale_times[2] = {{.tv_sec = stale}, {.tv_sec = stale}};+ ASSERT_EQ(utimensat(AT_FDCWD, marker, stale_times, 0), 0);+ ASSERT_EQ(utimensat(AT_FDCWD, lifetime, stale_times, 0), 0);+ ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);+ ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_OK);+ struct stat refreshed;+ ASSERT_EQ(stat(marker, &refreshed), 0);+ ASSERT_TRUE(refreshed.st_mtime > stale && refreshed.st_atime > stale);+ ASSERT_EQ(stat(lifetime, &refreshed), 0);+ ASSERT_TRUE(refreshed.st_mtime > stale && refreshed.st_atime > stale);+ASSERT_EQ(unlink(marker), 0);ASSERT_EQ(cbm_version_cohort_daemon_claim_touch(claim), CBM_VERSION_COHORT_UNSAFE);ASSERT_EQ(cbm_version_cohort_lease_touch(lease), CBM_VERSION_COHORT_OK);