From 40fc87bcffe1aba4e8a3e356ebacc1f5ee50ffd7 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Thu, 12 Nov 2026 04:52:50 -0600 Subject: [PATCH 01/10] fix(daemon): show cache paths in mismatch errors Add an authenticated status-details probe that returns the daemon's own resolved cache directory while preserving the legacy status response. Report escaped paths after verifying the conflict identity, and explain when diagnostics fall back to fingerprints. Cover long paths, Unicode, control characters and unavailable status details. Fixes #2955 Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- src/daemon/host.c | 1 + src/daemon/runtime.c | 118 ++++++++++++++---- src/daemon/runtime.h | 21 ++++ src/main.c | 157 +++++++++++++++++++----- tests/test_daemon_runtime.c | 158 +++++++++++++++++++++++++ tests/test_daemon_smoke.py | 8 ++ tests/windows/test_daemon_lifecycle.py | 120 +++++++++++++++++++ 7 files changed, 530 insertions(+), 53 deletions(-) diff --git a/src/daemon/host.c b/src/daemon/host.c index 9e50f121df..be78dda240 100644 --- a/src/daemon/host.c +++ b/src/daemon/host.c @@ -1153,6 +1153,7 @@ int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) { cbm_daemon_runtime_service_config_t runtime_config = { .endpoint = config->endpoint, .identity = config->identity, + .cache_directory = cbm_resolve_cache_dir(), .conflict_log_path = conflict_log, .conflict_log_cap_bytes = HOST_CONFLICT_LOG_CAP, .max_clients = HOST_MAX_CLIENTS, diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c index eb2eddf705..c01eced65b 100644 --- a/src/daemon/runtime.c +++ b/src/daemon/runtime.c @@ -280,6 +280,7 @@ struct cbm_daemon_runtime_service { char semantic_version[CBM_DAEMON_VERSION_TEXT_SIZE]; char build_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + char cache_directory[CBM_DAEMON_STATUS_CACHE_PATH_SIZE]; cbm_daemon_build_identity_t identity; runtime_process_image_reference_t active_image; /* Guarded by mutex. Retained references keep each verified inode alive, @@ -359,6 +360,11 @@ static uint64_t runtime_deadline_after(uint32_t timeout_ms) { return now_ms + (uint64_t)timeout_ms; } +static uint32_t runtime_timeout_remaining(uint64_t deadline_ms) { + uint64_t now_ms = cbm_now_ms(); + return now_ms < deadline_ms ? (uint32_t)(deadline_ms - now_ms) : 0; +} + static uint32_t runtime_ephemeral_linger_timeout_ms(void) { #ifdef CBM_ENABLE_TEST_SEAMS uint32_t seam = atomic_load(&runtime_ephemeral_linger_timeout_seam); @@ -2132,13 +2138,17 @@ static void runtime_control_collect_clients_locked(cbm_daemon_runtime_service_t } static void runtime_worker_handle_status(cbm_daemon_runtime_worker_t *worker, - const uint8_t *payload, uint32_t length) { + const uint8_t *payload, uint32_t length, + cbm_daemon_runtime_operation_t operation) { cbm_daemon_runtime_service_t *service = worker->service; char requested_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0}; bool peer_verified = runtime_control_request_decode(payload, length, requested_build) && runtime_activation_peer_matches_claim(service, worker->peer_process_id, requested_build); - uint8_t response[CBM_DAEMON_STATUS_RESPONSE_SIZE]; + uint8_t response[CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE]; + uint32_t response_size = operation == CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS + ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE + : CBM_DAEMON_STATUS_RESPONSE_SIZE; memset(response, 0, sizeof(response)); if (peer_verified) { uint8_t count = 0U; @@ -2173,9 +2183,13 @@ static void runtime_worker_handle_status(cbm_daemon_runtime_worker_t *worker, service->identity.semantic_version ? service->identity.semantic_version : ""); cbm_mutex_unlock(&service->mutex); + if (operation == CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS) { + memcpy(response + CBM_DAEMON_STATUS_RESPONSE_SIZE, service->cache_directory, + sizeof(service->cache_directory)); + } } - (void)runtime_worker_send_frame(worker, CBM_DAEMON_FRAME_RESPONSE, CBM_DAEMON_RUNTIME_OP_STATUS, - response, (uint32_t)sizeof(response)); + (void)runtime_worker_send_frame(worker, CBM_DAEMON_FRAME_RESPONSE, operation, response, + response_size); runtime_worker_finish(worker); } @@ -2377,8 +2391,9 @@ static void *runtime_connection_worker(void *opaque) { free(payload); return NULL; } - if (frame.flags == CBM_DAEMON_RUNTIME_OP_STATUS) { - runtime_worker_handle_status(worker, payload, frame.length); + if (frame.flags == CBM_DAEMON_RUNTIME_OP_STATUS || + frame.flags == CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS) { + runtime_worker_handle_status(worker, payload, frame.length, frame.flags); free(payload); return NULL; } @@ -2919,6 +2934,15 @@ cbm_daemon_runtime_service_t *cbm_daemon_runtime_service_start_reserved( memcpy(service->semantic_version, config->identity.semantic_version, version_length + 1); memcpy(service->build_fingerprint, config->identity.build_fingerprint, build_length + 1); } + if (config->cache_directory) { + size_t cache_length = 0; + copied_identity = copied_identity && + runtime_bounded_length(config->cache_directory, + sizeof(service->cache_directory), &cache_length); + if (copied_identity) { + memcpy(service->cache_directory, config->cache_directory, cache_length + sizeof(char)); + } + } service->identity.semantic_version = service->semantic_version; service->identity.build_fingerprint = service->build_fingerprint; service->identity.protocol_abi = config->identity.protocol_abi; @@ -3269,15 +3293,17 @@ static bool runtime_control_request_send(const cbm_daemon_ipc_endpoint_t *endpoi if (written <= 0 || (size_t)written != CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U) { return false; } + uint64_t deadline = runtime_deadline_after(timeout_ms); cbm_daemon_ipc_connection_t *connection = cbm_daemon_ipc_connect(endpoint, timeout_ms); bool sent = connection && cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_REQUEST, operation, request, (uint32_t)sizeof(request)); cbm_daemon_frame_t frame = {0}; uint8_t *payload = NULL; - int received = sent ? cbm_daemon_ipc_receive_frame_bounded(connection, timeout_ms, - response_size, &frame, &payload) - : 0; + int received = + sent ? cbm_daemon_ipc_receive_frame_bounded(connection, runtime_timeout_remaining(deadline), + response_size, &frame, &payload) + : 0; if (muted_holder_pid_out && sent && (received != 1 || (frame.type == CBM_DAEMON_FRAME_RESPONSE && frame.flags != operation))) { /* Connected but not served: either total silence (dead runtime), or a @@ -3304,20 +3330,8 @@ static uint32_t runtime_control_read_u32(const uint8_t *bytes) { (uint32_t)bytes[3]; } -bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, - const cbm_daemon_build_identity_t *identity, - uint32_t timeout_ms, - cbm_daemon_runtime_status_t *status_out) { - if (!status_out) { - return false; - } - memset(status_out, 0, sizeof(*status_out)); - uint8_t *payload = NULL; - if (!runtime_control_request_send(endpoint, identity, CBM_DAEMON_RUNTIME_OP_STATUS, timeout_ms, - CBM_DAEMON_STATUS_RESPONSE_SIZE, &payload, - &status_out->muted_endpoint_holder_pid)) { - return false; - } +static void runtime_status_response_decode(const uint8_t *payload, + cbm_daemon_runtime_status_t *status_out) { status_out->permanent = (payload[1] & 0x01U) != 0U; status_out->stopping = (payload[1] & 0x02U) != 0U; status_out->committed_clients = (uint16_t)(((uint16_t)payload[2] << 8) | payload[3]); @@ -3332,8 +3346,64 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint status_out->build_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U] = '\0'; memcpy(status_out->semantic_version, payload + 109U, sizeof(status_out->semantic_version)); status_out->semantic_version[sizeof(status_out->semantic_version) - 1U] = '\0'; +} + +static bool runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, uint32_t timeout_ms, + cbm_daemon_runtime_status_t *status_out, char *cache_directory) { + if (!status_out) { + return false; + } + memset(status_out, 0, sizeof(*status_out)); + uint8_t *payload = NULL; + cbm_daemon_runtime_operation_t operation = + cache_directory ? CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS : CBM_DAEMON_RUNTIME_OP_STATUS; + uint32_t response_size = + cache_directory ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE : CBM_DAEMON_STATUS_RESPONSE_SIZE; + if (!runtime_control_request_send( + endpoint, identity, operation, timeout_ms, response_size, &payload, + cache_directory ? NULL : &status_out->muted_endpoint_holder_pid)) { + return false; + } + bool valid = !cache_directory || memchr(payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, '\0', + CBM_DAEMON_STATUS_CACHE_PATH_SIZE); + if (valid) { + runtime_status_response_decode(payload, status_out); + if (cache_directory) { + memcpy(cache_directory, payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, + CBM_DAEMON_STATUS_CACHE_PATH_SIZE); + } + } free(payload); - return true; + return valid; +} + +bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + uint32_t timeout_ms, + cbm_daemon_runtime_status_t *status_out) { + return runtime_request_status(endpoint, identity, timeout_ms, status_out, NULL); +} + +bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + uint32_t timeout_ms, + cbm_daemon_runtime_status_details_t *details_out) { + if (!details_out) { + return false; + } + memset(details_out, 0, sizeof(*details_out)); + if (timeout_ms == CBM_DAEMON_IPC_WAIT_FOREVER) { + return false; + } + uint64_t deadline = runtime_deadline_after(timeout_ms); + if (runtime_request_status(endpoint, identity, timeout_ms, &details_out->status, + details_out->cache_directory)) { + return true; + } + uint32_t remaining = runtime_timeout_remaining(deadline); + return remaining > 0 && + cbm_daemon_runtime_request_status(endpoint, identity, remaining, &details_out->status); } /* "; CBM sessions using it: pids 1, 2 and 3 more" — empty when none are known. */ diff --git a/src/daemon/runtime.h b/src/daemon/runtime.h index 361a7e7810..9418fdc615 100644 --- a/src/daemon/runtime.h +++ b/src/daemon/runtime.h @@ -55,6 +55,10 @@ #define CBM_DAEMON_CONTROL_REQUEST_SIZE 65U #define CBM_DAEMON_CONTROL_CLIENT_CAP 8U #define CBM_DAEMON_STATUS_RESPONSE_SIZE 121U +#define CBM_DAEMON_STATUS_CACHE_PATH_SIZE 4096U +/* Separate operation: the frozen STATUS response remains exactly 121 bytes. */ +#define CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE \ + (CBM_DAEMON_STATUS_RESPONSE_SIZE + CBM_DAEMON_STATUS_CACHE_PATH_SIZE) #define CBM_DAEMON_STOP_RESPONSE_SIZE 40U #define CBM_DAEMON_RUNTIME_PROJECT_KEY_MAX 4096U #define CBM_DAEMON_RUNTIME_APPLICATION_PAYLOAD_MAX (CBM_DAEMON_MAX_FRAME_SIZE - 16U) @@ -87,6 +91,9 @@ typedef enum { * otherwise begins the normal stopping sequence — including for a * PERMANENT generation, which this op and process kill alone may end. */ CBM_DAEMON_RUNTIME_OP_STOP = 11, + /* Authenticated read-only STATUS plus a NUL-terminated canonical cache + * path in a fixed 4096-byte suffix. Empty means unavailable. */ + CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS = 12, } cbm_daemon_runtime_operation_t; typedef enum { @@ -180,6 +187,8 @@ typedef struct { typedef struct { const cbm_daemon_ipc_endpoint_t *endpoint; cbm_daemon_build_identity_t identity; + /* Optional canonical path, copied at startup; never persisted in logs. */ + const char *cache_directory; const char *conflict_log_path; size_t conflict_log_cap_bytes; /* Hard cap on accepted connection threads, including sockets that have not @@ -278,6 +287,11 @@ typedef struct { uint64_t muted_endpoint_holder_pid; } cbm_daemon_runtime_status_t; +typedef struct { + cbm_daemon_runtime_status_t status; + char cache_directory[CBM_DAEMON_STATUS_CACHE_PATH_SIZE]; +} cbm_daemon_runtime_status_details_t; + typedef struct { bool accepted; /* stopping was initiated */ bool busy; /* refused: committed clients hold the daemon */ @@ -293,6 +307,13 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint uint32_t timeout_ms, cbm_daemon_runtime_status_t *status_out); +/* Prefer STATUS_DETAILS, then fall back to legacy STATUS within one timeout. + * A successful legacy reply leaves cache_directory empty. */ +bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + uint32_t timeout_ms, + cbm_daemon_runtime_status_details_t *details_out); + /* One-shot authenticated stop request. Refuses while committed clients exist * (result_out lists their pids); otherwise begins the stopping sequence, for * permanent generations too. */ diff --git a/src/main.c b/src/main.c index 257c5cb260..1a2b88df49 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,8 @@ #include "daemon/application.h" #include "daemon/bootstrap.h" #include "daemon/frontend.h" +#include "daemon/runtime.h" +#include "daemon/service.h" #include "daemon/host.h" #include "daemon/ipc.h" #include "daemon/project_lock.h" @@ -1844,6 +1846,124 @@ static void main_conflict_remedy_probe(const cbm_daemon_ipc_endpoint_t *endpoint } } +static bool main_cache_path_matches(const char *path, const char *expected_fingerprint) { + if (!path || !path[0] || !expected_fingerprint) { + return false; + } + char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + cbm_sha256_hex(path, strlen(path), fingerprint); + return strcmp(fingerprint, expected_fingerprint) == 0; +} + +/* Stream the quoted path: two maximum-length roots must not pass through + * the frozen 512-byte rendezvous message or a truncating concatenation. */ +static void main_print_cache_path(const char *path) { + enum { ASCII_SPACE = 0x20, ASCII_DELETE = 0x7f }; + (void)fputc('"', stderr); + for (const unsigned char *p = (const unsigned char *)path; *p; p++) { + switch (*p) { + case '"': + case '\\': + (void)fputc('\\', stderr); + (void)fputc(*p, stderr); + break; + case '\n': + (void)fputs("\\n", stderr); + break; + case '\r': + (void)fputs("\\r", stderr); + break; + case '\t': + (void)fputs("\\t", stderr); + break; + case '\b': + (void)fputs("\\b", stderr); + break; + case '\f': + (void)fputs("\\f", stderr); + break; + default: + if (*p < ASCII_SPACE || *p == ASCII_DELETE) { + (void)fprintf(stderr, "\\u%04x", (unsigned)*p); + } else { + (void)fputc(*p, stderr); + } + } + } + (void)fputc('"', stderr); +} + +/* Client-only diagnostics: cohort records and durable logs retain hashes. + * A daemon may have turned over since rejection; prove the returned path + * belongs to the rejected cache/build before attributing it to that conflict. */ +static bool main_report_cache_conflict(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + const cbm_daemon_conflict_t *conflict) { + if (conflict->status != CBM_DAEMON_HELLO_CACHE_CONFLICT) { + return false; + } + char requested[MAIN_PATH_CAP] = ""; + const char *cache = cbm_resolve_cache_dir(); + if (cache) { + (void)snprintf(requested, sizeof(requested), "%s", cache); + } + cbm_daemon_runtime_status_details_t details; + bool known = cbm_daemon_runtime_request_status_details( + endpoint, identity, MAIN_CONFLICT_STATUS_PROBE_TIMEOUT_MS, &details); + bool paths_known = + known && + strcmp(details.status.build_fingerprint, conflict->active_build_fingerprint) == 0 && + main_cache_path_matches(details.cache_directory, conflict->active_cache_fingerprint) && + main_cache_path_matches(requested, conflict->requested_cache_fingerprint); + if (paths_known) { + (void)fputs("codebase-memory-mcp: CBM could not start because the active account daemon " + "uses a different cache directory.\n requested cache: ", + stderr); + main_print_cache_path(requested); + (void)fputs("\n daemon cache: ", stderr); + main_print_cache_path(details.cache_directory); + (void)fputs("\nClose all CBM sessions and commands, then retry with one consistent " + "CBM_CACHE_DIR.", + stderr); + } else { + char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE]; + if (!cbm_daemon_conflict_format(conflict, message, sizeof(message))) { + return false; + } + (void)fprintf(stderr, "codebase-memory-mcp: %s Cache paths unavailable or unverified.", + message); + } + char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE]; + if (cbm_daemon_conflict_remedy_format(known ? &details.status : NULL, remedy, sizeof(remedy))) { + (void)fprintf(stderr, " %s", remedy); + } + (void)fputc('\n', stderr); + return true; +} + +static void main_report_cohort_failure(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + cbm_version_cohort_status_t status, + const cbm_daemon_conflict_t *conflict, const char *fallback, + bool probe) { + char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE]; + bool formatted = status == CBM_VERSION_COHORT_CONFLICT && + cbm_daemon_conflict_format(conflict, message, sizeof(message)); + char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE] = ""; + if (status == CBM_VERSION_COHORT_CONFLICT) { + (void)cbm_version_cohort_log_conflict(conflict); + /* Hooks fail open on a tight budget; they skip both probes. */ + if (probe) { + if (main_report_cache_conflict(endpoint, identity, conflict)) { + return; + } + main_conflict_remedy_probe(endpoint, identity, NULL, remedy, sizeof(remedy)); + } + } + (void)fprintf(stderr, "codebase-memory-mcp: %s%s%s\n", formatted ? message : fallback, + remedy[0] ? " " : "", remedy); +} + /* Client bootstrap with the upgrade policy: a CONFLICT against a PERMANENT * daemon of a strictly OLDER release is resolved by draining that daemon * (the same authenticated path install/update use) and retrying once. A @@ -3009,20 +3129,11 @@ int main(int argc, char **argv) { main_deadline_after(MAIN_STARTUP_TIMEOUT_MS), &cohort_lease, &cohort_conflict); if (cohort_status != CBM_VERSION_COHORT_OK) { - char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE]; - bool formatted = cohort_status == CBM_VERSION_COHORT_CONFLICT && - cbm_daemon_conflict_format(&cohort_conflict, message, sizeof(message)); - char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE] = ""; - if (cohort_status == CBM_VERSION_COHORT_CONFLICT) { - (void)cbm_version_cohort_log_conflict(&cohort_conflict); - main_conflict_remedy_probe(local_endpoint, &local_identity, NULL, remedy, - sizeof(remedy)); - } - (void)fprintf(stderr, "codebase-memory-mcp: %s%s%s\n", - formatted ? message - : "CLI exact-build admission could not be verified; retry " - "after active CBM operations exit", - remedy[0] ? " " : "", remedy); + main_report_cohort_failure(local_endpoint, &local_identity, cohort_status, + &cohort_conflict, + "CLI exact-build admission could not be verified; retry " + "after active CBM operations exit", + true); goto local_cli_cleanup; } main_local_maintenance_context_init(&maintenance_context); @@ -3341,21 +3452,9 @@ int main(int argc, char **argv) { &client_cohort_lease, &client_cohort_conflict) : CBM_VERSION_COHORT_IO; if (client_cohort_status != CBM_VERSION_COHORT_OK) { - char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE]; - bool formatted = - client_cohort_status == CBM_VERSION_COHORT_CONFLICT && - cbm_daemon_conflict_format(&client_cohort_conflict, message, sizeof(message)); - char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE] = ""; - if (client_cohort_status == CBM_VERSION_COHORT_CONFLICT) { - (void)cbm_version_cohort_log_conflict(&client_cohort_conflict); - /* Hooks fail open on a tight budget; they skip the extra probe. */ - if (role != CBM_DAEMON_PROCESS_HOOK_CLIENT) { - main_conflict_remedy_probe(endpoint, &identity, NULL, remedy, sizeof(remedy)); - } - } - (void)fprintf(stderr, "codebase-memory-mcp: %s%s%s\n", - formatted ? message : "client exact-build admission failed", - remedy[0] ? " " : "", remedy); + main_report_cohort_failure(endpoint, &identity, client_cohort_status, + &client_cohort_conflict, "client exact-build admission failed", + role != CBM_DAEMON_PROCESS_HOOK_CLIENT); if (role == CBM_DAEMON_PROCESS_HOOK_CLIENT && client_cohort_status == CBM_VERSION_COHORT_CONFLICT) { main_hook_report_conflicted_daemon(hook_dialect); diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index c34c45f5b2..2a53958b39 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -1272,6 +1272,7 @@ static bool runtime_test_fixture_start_failed(const char *tag, const char *stage } static bool runtime_test_fixture_permanent = false; +static const char *runtime_test_fixture_cache_directory; typedef struct { cbm_version_cohort_manager_t *manager; @@ -1329,6 +1330,7 @@ static bool runtime_test_fixture_start_configured( cbm_daemon_runtime_service_config_t config = { .endpoint = fixture->endpoint, .identity = *identity, + .cache_directory = runtime_test_fixture_cache_directory, .conflict_log_path = fixture->log_path, .conflict_log_cap_bytes = 64U * 1024U, .max_clients = max_clients, @@ -5512,6 +5514,159 @@ TEST(daemon_runtime_close_begin_releases_admission_with_inflight_request) { PASS(); } +TEST(daemon_runtime_status_details_preserves_long_paths_and_owns_copy) { + const size_t lengths[] = {259, 260, 261, 4095}; + cbm_daemon_build_identity_t identity = + runtime_test_identity("2.4.0", runtime_test_self_build()); + for (size_t i = 0; i < sizeof(lengths) / sizeof(lengths[0]); i++) { + char cache[4096] = {0}; + const char prefix[] = "C:/cache-中文 space/line\nbreak\t\"\\"; + memcpy(cache, prefix, sizeof(prefix) - 1); + memset(cache + sizeof(prefix) - 1, 'x', lengths[i] - sizeof(prefix) + 1); + cache[lengths[i]] = '\0'; + char expected[4096]; + memcpy(expected, cache, sizeof(cache)); + runtime_test_fixture_t fixture; + runtime_test_fixture_permanent = true; + runtime_test_fixture_cache_directory = cache; + bool started = runtime_test_fixture_start(&fixture, "status-path", &identity); + runtime_test_fixture_cache_directory = NULL; + runtime_test_fixture_permanent = false; + memset(cache, 'z', lengths[i]); /* must not change the service's path */ + cbm_daemon_runtime_status_details_t details = {0}; + cbm_daemon_runtime_status_t legacy = {0}; + bool queried = + started && cbm_daemon_runtime_request_status_details(fixture.endpoint, &identity, + RUNTIME_TEST_TIMEOUT_MS, &details); + bool legacy_ok = + started && cbm_daemon_runtime_request_status(fixture.endpoint, &identity, + RUNTIME_TEST_TIMEOUT_MS, &legacy); + size_t admitted = started ? cbm_daemon_runtime_service_active_clients(fixture.service) : 1; + runtime_test_fixture_finish(&fixture); + ASSERT_TRUE(started); + ASSERT_TRUE(queried); + ASSERT_STR_EQ(details.cache_directory, expected); + ASSERT_TRUE(legacy_ok); + ASSERT_EQ(details.status.daemon_pid, legacy.daemon_pid); + ASSERT_EQ(admitted, 0); + } + PASS(); +} + +TEST(daemon_runtime_status_details_does_not_disclose_paths_to_forged_peers) { + cbm_daemon_build_identity_t identity = + runtime_test_identity("2.4.0", runtime_test_self_build()); + runtime_test_fixture_t fixture; + runtime_test_fixture_permanent = true; + runtime_test_fixture_cache_directory = "C:/private cache"; + bool started = runtime_test_fixture_start(&fixture, "status-auth", &identity); + runtime_test_fixture_cache_directory = NULL; + runtime_test_fixture_permanent = false; + bool rejected = started; + for (uint32_t length = 64; started && length <= 65; length++) { + cbm_daemon_ipc_connection_t *connection = + cbm_daemon_ipc_connect(fixture.endpoint, RUNTIME_TEST_TIMEOUT_MS); + bool sent = connection && cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_REQUEST, + CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS, + RUNTIME_BUILD_B, length); + cbm_daemon_frame_t frame = {0}; + uint8_t *payload = NULL; + int received = sent ? cbm_daemon_ipc_receive_frame(connection, RUNTIME_TEST_TIMEOUT_MS, + &frame, &payload) + : 0; + rejected = rejected && received == 1 && frame.length == 4217 && payload; + for (uint32_t byte = 0; payload && byte < frame.length; byte++) { + rejected = rejected && payload[byte] == 0; + } + free(payload); + cbm_daemon_ipc_connection_close(connection); + } + runtime_test_fixture_finish(&fixture); + ASSERT_TRUE(started); + ASSERT_TRUE(rejected); + PASS(); +} + +typedef struct { + cbm_daemon_ipc_listener_t *listener; + bool malformed_details; + bool served; +} runtime_legacy_status_t; + +static void *runtime_test_legacy_status_thread(void *opaque) { + runtime_legacy_status_t *server = opaque; + for (size_t attempt = 0; attempt < 2; attempt++) { + cbm_daemon_ipc_connection_t *connection = NULL; + if (cbm_daemon_ipc_accept(server->listener, RUNTIME_TEST_TIMEOUT_MS, &connection) != 1) { + return NULL; + } + cbm_daemon_frame_t frame = {0}; + uint8_t *payload = NULL; + int received = + cbm_daemon_ipc_receive_frame(connection, RUNTIME_TEST_TIMEOUT_MS, &frame, &payload); + if (received == 1 && frame.length == 65) { + if (attempt == 0 && server->malformed_details) { + uint8_t malformed[4217]; + memset(malformed, 'x', sizeof(malformed)); /* no path terminator */ + malformed[0] = 1; + (void)cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_RESPONSE, 12, + malformed, sizeof(malformed)); + } else if (attempt == 1 && frame.flags == 10) { + uint8_t legacy[121] = {1}; /* frozen legacy STATUS envelope */ + runtime_test_put_u32(legacy + 4, 77); + server->served = cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_RESPONSE, + 10, legacy, sizeof(legacy)); + } + } + free(payload); + cbm_daemon_ipc_connection_close(connection); + } + return NULL; +} + +TEST(daemon_runtime_status_details_falls_back_to_legacy_on_unsupported_or_malformed_reply) { + cbm_daemon_build_identity_t identity = + runtime_test_identity("2.4.0", runtime_test_self_build()); + for (int malformed = 0; malformed < 2; malformed++) { + char parent[RUNTIME_TEST_PATH_CAP] = {0}; + char key[CBM_DAEMON_KEY_SIZE] = {0}; + char runtime_dir[RUNTIME_TEST_PATH_CAP] = {0}; + bool prepared = th_secure_runtime_parent_new(parent, sizeof(parent), "status-legacy") && + cbm_daemon_rendezvous_key(key); + cbm_daemon_ipc_endpoint_t *endpoint = + prepared ? cbm_daemon_ipc_endpoint_new(key, parent) : NULL; + bool copied = endpoint && runtime_test_copy_path( + runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint)); + runtime_legacy_status_t server = { + .listener = endpoint ? cbm_daemon_ipc_listen(endpoint) : NULL, + .malformed_details = malformed != 0, + }; + cbm_thread_t thread; + bool started = + server.listener && + cbm_thread_create(&thread, 0, runtime_test_legacy_status_thread, &server) == 0; + cbm_daemon_runtime_status_details_t details = {0}; + bool queried = started && cbm_daemon_runtime_request_status_details( + endpoint, &identity, RUNTIME_TEST_TIMEOUT_MS, &details); + if (started) { + (void)cbm_thread_join(&thread); + } + cbm_daemon_ipc_listener_close(server.listener); + cbm_daemon_ipc_endpoint_free(endpoint); + (void)cbm_rmdir(runtime_dir); + (void)cbm_rmdir(parent); + ASSERT_TRUE(prepared); + ASSERT_TRUE(copied); + ASSERT_TRUE(started); + ASSERT_TRUE(queried); + ASSERT_TRUE(server.served); + ASSERT_EQ(details.status.daemon_pid, 77); + ASSERT_EQ(details.cache_directory[0], '\0'); + ASSERT_EQ(details.status.muted_endpoint_holder_pid, 0); + } + PASS(); +} + TEST(daemon_runtime_permanent_service_survives_last_disconnect_until_stop) { cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", runtime_test_self_build()); @@ -5598,6 +5753,9 @@ TEST(daemon_runtime_stop_refuses_while_committed_clients_exist) { } SUITE(daemon_runtime) { + RUN_TEST(daemon_runtime_status_details_preserves_long_paths_and_owns_copy); + RUN_TEST(daemon_runtime_status_details_does_not_disclose_paths_to_forged_peers); + RUN_TEST(daemon_runtime_status_details_falls_back_to_legacy_on_unsupported_or_malformed_reply); RUN_TEST(daemon_runtime_permanent_service_survives_last_disconnect_until_stop); RUN_TEST(daemon_runtime_stop_refuses_while_committed_clients_exist); RUN_TEST(daemon_host_early_coordination_failure_is_durable); diff --git a/tests/test_daemon_smoke.py b/tests/test_daemon_smoke.py index 93ab933a44..34892337a8 100644 --- a/tests/test_daemon_smoke.py +++ b/tests/test_daemon_smoke.py @@ -973,6 +973,14 @@ def main(): "cache-root conflict did not emit visible remediation guidance: " + repr(mismatched_result.stderr), ) + check( + 'requested cache: ' + json.dumps(str(mismatched_cache.resolve())) + in mismatched_result.stderr + and 'daemon cache: ' + json.dumps(str(cache.resolve())) + in mismatched_result.stderr, + "cache-root conflict did not name both canonical paths: " + + repr(mismatched_result.stderr), + ) wait_until( lambda: any( item.get("reason") == "cache_root" diff --git a/tests/windows/test_daemon_lifecycle.py b/tests/windows/test_daemon_lifecycle.py index c16abb0445..2c2f8a1760 100644 --- a/tests/windows/test_daemon_lifecycle.py +++ b/tests/windows/test_daemon_lifecycle.py @@ -9,6 +9,8 @@ * A cold one-shot ``cli`` command is quiet by default; ``cli --verbose`` opts into the startup-tax hint. * ``daemon stop`` on an idle daemon stops it; a second ``stop`` is idempotent. +* Cache mismatches name both canonical paths; if STATUS is unavailable, the + fingerprint fallback explains that the paths are unavailable or unverified. Every path carries a kill-by-pid backstop so a stuck daemon can never hang the suite (the Windows leg's test-infra hang sensitivity is on record). @@ -18,11 +20,13 @@ Usage: python test_daemon_lifecycle.py """ +import json import os import re import subprocess import sys import tempfile +import time def runtime_for(cache): @@ -59,6 +63,116 @@ def force_kill(pid): subprocess.run(["kill", "-9", str(pid)], capture_output=True, timeout=30) +def check_cache_conflict_paths(binary, cache, work): + def filesystem_path(path): + # Exercise CBM's own long-path support regardless of the runner's + # LongPathsEnabled policy; Python setup/inspection uses explicit Win32 paths. + if os.name == "nt": + absolute = os.path.abspath(path) + return "\\\\?\\UNC\\" + absolute[2:] if absolute.startswith("\\\\") else "\\\\?\\" + absolute + return path + + # Cache roots must share a rendezvous, or the mismatch would never occur. + shared_runtime = {"CBM_RUNTIME_DIR": runtime_for(cache), "CBM_STARTUP_TIMEOUT_MS": "1000"} + paths = [os.path.join(work, "cache-中文 space")] + for length in (259, 260, 261, 1024): + path = os.path.join(work, "cache-length-%d" % length) + while len(path) < length: + remaining = length - len(path) + if remaining == 1: + path += "x" + else: + path = os.path.join(path, "x" * min(32, remaining - 1)) + paths.append(path) + if os.name != "nt": + # Windows forbids these filename characters; POSIX must escape them. + paths.append(os.path.join(work, 'cache-line\nbreak\t"\x1b')) + + for requested in paths: + os.makedirs(filesystem_path(requested), exist_ok=True) + for args in (["cli", "list_projects"], []): # [] enters the MCP frontend + result = run_cli(binary, requested, args, extra_env=shared_runtime) + text = output_text(result) + if result.returncode == 0: + raise AssertionError("different cache root unexpectedly admitted") + for label, path in (("requested cache", requested), ("daemon cache", cache)): + canonical = os.path.realpath(filesystem_path(path)).replace("\\", "/") + if canonical.startswith("//?/UNC/"): + canonical = "//" + canonical[8:] + elif canonical.startswith("//?/"): + canonical = canonical[4:] + expected = label + ": " + json.dumps(canonical, ensure_ascii=False) + if expected not in text: + raise AssertionError("missing complete escaped cache path %r in %r" + % (expected, text)) + if "different cache directory" not in text or "CBM_CACHE_DIR" not in text: + raise AssertionError("cache mismatch lost its explanation or remedy") + if "Cache paths unavailable or unverified." in text: + raise AssertionError("resolved paths were incorrectly marked unavailable") + log = os.path.join(requested, "logs", "daemon-conflicts.ndjson") + with open(filesystem_path(log), encoding="utf-8") as stream: + events = [json.loads(line) for line in stream if line.strip()] + if not any(event.get("reason") == "cache_root" for event in events): + raise AssertionError("cache mismatch was not recorded") + for event in events: + for key in ("active_cache", "requested_cache"): + if not re.fullmatch(r"[0-9a-f]{64}", event.get(key, "")): + raise AssertionError("conflict log must retain cache fingerprints") + healthy = run_cli(binary, cache, ["cli", "list_projects"]) + if healthy.returncode != 0: + raise AssertionError("cache rejection disturbed the active daemon") + + +def check_cache_conflict_fallback(binary, work): + active = os.path.join(work, "cache-fallback-active") + requested = os.path.join(work, "cache-fallback-requested") + runtime = runtime_for(active) + shared = {"CBM_RUNTIME_DIR": runtime, "CBM_STARTUP_TIMEOUT_MS": "1000"} + env = dict(os.environ, CBM_CACHE_DIR=active, **shared) + # An index command waiting for piped arguments owns a cohort lease but + # has not connected to a daemon. This makes STATUS genuinely unavailable. + owner = subprocess.Popen([binary, "cli", "index_repository"], env=env, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + try: + deadline = time.monotonic() + 15 + while True: + if owner.poll() is not None: + raise AssertionError("cohort owner exited before reading its arguments") + written = False + for directory, _, files in os.walk(runtime): + if "cbm-version-cohort-lifetime-v1.lock" in files: + path = os.path.join(directory, "cbm-version-cohort-lifetime-v1.lock") + written = os.path.getsize(path) > 1 + if written: + break + if time.monotonic() >= deadline: + raise AssertionError("cohort owner did not publish its identity") + time.sleep(0.02) + for args in ([], ["cli", "list_projects"]): + result = run_cli(binary, requested, args, extra_env=shared) + text = output_text(result) + if result.returncode == 0 or not re.search( + r"active cache [0-9a-f]{64}; requested cache [0-9a-f]{64}", text): + raise AssertionError("fallback lost the cache conflict fingerprints: " + text) + if "Cache paths unavailable or unverified." not in text: + raise AssertionError("fingerprint fallback did not explain missing paths: " + text) + if "requested cache:" in text or "daemon cache:" in text: + raise AssertionError("fallback claimed an unverified cache path") + status = run_cli(binary, active, ["daemon", "status"], extra_env=shared) + if status.returncode == 0 or "not running" not in output_text(status): + raise AssertionError("fallback probe started a daemon") + finally: + owner.terminate() + try: + owner.wait(timeout=5) + except subprocess.TimeoutExpired: + owner.kill() + owner.wait(timeout=5) + for stream in (owner.stdin, owner.stdout, owner.stderr): + stream.close() + + def main(): if len(sys.argv) < 2: print("usage: python test_daemon_lifecycle.py ") @@ -126,6 +240,9 @@ def main(): return 1 print("PASS: warm cli recycled the daemon; daemon survived its client") + check_cache_conflict_paths(binary, cache, work) + print("PASS: cache mismatch paths survive Unicode, escaping and long paths") + stop = run_cli(binary, cache, ["daemon", "stop"]) if stop.returncode != 0: print("RED: `daemon stop` on an idle daemon failed:\n%s" @@ -139,6 +256,9 @@ def main(): print("PASS: stop retired the idle daemon; second stop was idempotent") daemon_pid = 0 + check_cache_conflict_fallback(binary, work) + print("PASS: fingerprint fallback explains unavailable paths for CLI and MCP") + if not ui_build: print("SKIP: non-UI binary cannot exercise the UI-configuration refusal") else: From 034df5e58606be1dc86fff598ce660bf8202e0c0 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:28:55 -0600 Subject: [PATCH 02/10] test(daemon): fix long-path setup and Unicode assertions Honor an explicit CBM_RUNTIME_DIR before creating the default runtime so Windows long-path cases reach the binary without extra setup failures. Match UTF-8 cache paths without JSON ASCII escaping in the smoke check. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- tests/test_daemon_smoke.py | 6 ++++-- tests/windows/test_daemon_lifecycle.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_daemon_smoke.py b/tests/test_daemon_smoke.py index 34892337a8..57aa146191 100644 --- a/tests/test_daemon_smoke.py +++ b/tests/test_daemon_smoke.py @@ -974,9 +974,11 @@ def main(): + repr(mismatched_result.stderr), ) check( - 'requested cache: ' + json.dumps(str(mismatched_cache.resolve())) + "requested cache: " + + json.dumps(str(mismatched_cache.resolve()), ensure_ascii=False) in mismatched_result.stderr - and 'daemon cache: ' + json.dumps(str(cache.resolve())) + and "daemon cache: " + + json.dumps(str(cache.resolve()), ensure_ascii=False) in mismatched_result.stderr, "cache-root conflict did not name both canonical paths: " + repr(mismatched_result.stderr), diff --git a/tests/windows/test_daemon_lifecycle.py b/tests/windows/test_daemon_lifecycle.py index 2c2f8a1760..53cf8674c2 100644 --- a/tests/windows/test_daemon_lifecycle.py +++ b/tests/windows/test_daemon_lifecycle.py @@ -44,9 +44,10 @@ def run_cli(binary, cache, args, timeout=60, extra_env=None): # Daemon coordination is account-wide and CBM_CACHE_DIR does not move the # rendezvous; a private CBM_RUNTIME_DIR per cache is what isolates a call # from every other daemon on the account (and makes a cold call cold). - env["CBM_RUNTIME_DIR"] = runtime_for(cache) if extra_env: env.update(extra_env) + if not extra_env or "CBM_RUNTIME_DIR" not in extra_env: + env["CBM_RUNTIME_DIR"] = runtime_for(cache) return subprocess.run([binary] + args, capture_output=True, timeout=timeout, env=env) From f9d9662e395a5f2d1571d450dae4d8fc90ba9c7d Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:32:11 +0800 Subject: [PATCH 03/10] Fix Windows long cache paths and daemon control timeouts Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- src/cli/cli.c | 10 ++- src/cli/runtime_settings.c | 26 +++++++- src/cli/runtime_settings.h | 2 + src/daemon/ipc.c | 20 ++++-- src/daemon/runtime.c | 19 +++--- tests/test_daemon_runtime.c | 91 ++++++++++++++++++++++++++ tests/test_runtime_settings.c | 57 ++++++++++++++++ tests/windows/test_daemon_lifecycle.py | 59 +---------------- 8 files changed, 208 insertions(+), 76 deletions(-) diff --git a/src/cli/cli.c b/src/cli/cli.c index 65848adb79..a142586564 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -7894,14 +7894,18 @@ cbm_config_t *cbm_config_open(const char *cache_dir) { return NULL; } - char dbpath[CLI_BUF_1K]; - snprintf(dbpath, sizeof(dbpath), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir); + char dbpath[4096]; + int dbpath_length = snprintf(dbpath, sizeof(dbpath), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir); + if (dbpath_length < 0 || (size_t)dbpath_length >= sizeof(dbpath)) { + return NULL; + } /* Ensure directory exists */ mkdirp(cache_dir, DIR_PERMS); sqlite3 *db = NULL; - if (sqlite3_open(dbpath, &db) != SQLITE_OK) { + if (cbm_runtime_settings_open_db(dbpath, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) != + SQLITE_OK) { if (db) { sqlite3_close(db); } diff --git a/src/cli/runtime_settings.c b/src/cli/runtime_settings.c index 6b11101c6e..6ecfff1fbd 100644 --- a/src/cli/runtime_settings.c +++ b/src/cli/runtime_settings.c @@ -5,6 +5,9 @@ #include "foundation/platform.h" #include "foundation/log.h" #include "foundation/compat_fs.h" +#ifdef _WIN32 +#include "foundation/win_utf8.h" +#endif #include "daemon/ipc.h" #include "ui/config.h" #include @@ -241,6 +244,22 @@ static bool canonical_cache_path(const char *cache, char out[SETTINGS_PATH_CAP], return cbm_daemon_ipc_private_directory_secure(out); } +int cbm_runtime_settings_open_db(const char *path, sqlite3 **db, int flags) { +#ifdef _WIN32 + *db = NULL; + wchar_t *wide = cbm_path_to_wide(path); + char *extended = wide ? cbm_wide_to_utf8(wide) : NULL; + free(wide); + if (!extended) + return SQLITE_NOMEM; + int rc = sqlite3_open_v2(extended, db, flags, "win32-longpath"); + free(extended); + return rc; +#else + return sqlite3_open_v2(path, db, flags, NULL); +#endif +} + static sqlite3 *settings_open(const char *cache) { if (!cache || !cache[0] || strlen(cache) + 12 >= SETTINGS_PATH_CAP) return NULL; @@ -255,7 +274,8 @@ static sqlite3 *settings_open(const char *cache) { if (snprintf(path, sizeof(path), "%s/_config.db", canonical) >= (int)sizeof(path)) return NULL; sqlite3 *db = NULL; - if (sqlite3_open(path, &db) != SQLITE_OK) { + if (cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) != + SQLITE_OK) { if (db) sqlite3_close(db); return NULL; @@ -477,7 +497,7 @@ void cbm_runtime_settings_reset_for_tests(void) { * progress; a live writer takes the busy path instead. */ static bool settings_recover_hot_journal(const char *path) { sqlite3 *db = NULL; - bool recovered = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK; + bool recovered = cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READWRITE) == SQLITE_OK; if (recovered) { cbm_runtime_settings_wait_for_writers(db); recovered = @@ -505,7 +525,7 @@ static bool settings_recover_hot_journal(const char *path) { static sqlite3 *settings_open_snapshot(const char *path, bool *hot_journal) { *hot_journal = false; sqlite3 *db = NULL; - if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) { + if (cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READONLY) != SQLITE_OK) { if (db) sqlite3_close(db); return NULL; diff --git a/src/cli/runtime_settings.h b/src/cli/runtime_settings.h index 099a201e5a..bce0e7e19e 100644 --- a/src/cli/runtime_settings.h +++ b/src/cli/runtime_settings.h @@ -29,6 +29,8 @@ void cbm_runtime_settings_note_http_port(int port); * failing on its lock. Logs config_db.busy_wait once per busy wait. */ struct sqlite3; void cbm_runtime_settings_wait_for_writers(struct sqlite3 *db); +/* Open the configuration database with a Windows long-path capable VFS. */ +int cbm_runtime_settings_open_db(const char *path, struct sqlite3 **db, int flags); #ifdef CBM_ENABLE_TEST_SEAMS /* Single-threaded test teardown only; never exposed in production builds. */ diff --git a/src/daemon/ipc.c b/src/daemon/ipc.c index c0c6894cf4..169e6c85b3 100644 --- a/src/daemon/ipc.c +++ b/src/daemon/ipc.c @@ -13,6 +13,9 @@ #include "foundation/private_file_lock_internal.h" #include "foundation/sha256.h" #include "foundation/secure_random.h" +#ifdef _WIN32 +#include "foundation/win_utf8.h" +#endif #include #include @@ -5413,10 +5416,19 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) { * absolute drive path. */ return false; } - wchar_t *path = wide_copy(directory_path); + /* Prefix the already validated drive-absolute spelling without resolving + * dot components: the component walk must still reject those itself. */ + bool extended = length >= 240U; + size_t prefix_length = extended ? 4U : 0U; + wchar_t *path = calloc(length + prefix_length + 1U, sizeof(*path)); if (!path) { return false; } + if (extended) { + wmemcpy(path, L"\\\\?\\", prefix_length); + } + wmemcpy(path + prefix_length, directory_path, length + 1U); + length = wcslen(path); for (size_t i = 0; i < length; i++) { if (path[i] == L'/') { path[i] = L'\\'; @@ -5428,7 +5440,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) { return false; } bool ok = true; - size_t component_start = 3; + size_t component_start = 3U + prefix_length; for (size_t i = component_start; ok && i <= length; i++) { if (i < length && path[i] != L'\\') { continue; @@ -5565,8 +5577,8 @@ FILE *cbm_daemon_ipc_private_log_open(const char *directory_path, const char *ba wchar_t *wide_directory = utf8_to_wide(directory_path); char *path = string_format("%s/%s", directory_path, base_name); char *rotated_path = path ? string_format("%s.1", path) : NULL; - wchar_t *wide_path = path ? utf8_to_wide(path) : NULL; - wchar_t *wide_rotated = rotated_path ? utf8_to_wide(rotated_path) : NULL; + wchar_t *wide_path = path ? cbm_path_to_wide(path) : NULL; + wchar_t *wide_rotated = rotated_path ? cbm_path_to_wide(rotated_path) : NULL; if (!wide_directory || !path || !rotated_path || !wide_path || !wide_rotated || !win_private_directory_tree_secure(wide_directory)) { free(wide_directory); diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c index c01eced65b..2bfea4368f 100644 --- a/src/daemon/runtime.c +++ b/src/daemon/runtime.c @@ -3278,6 +3278,7 @@ static bool runtime_control_request_send(const cbm_daemon_ipc_endpoint_t *endpoi const cbm_daemon_build_identity_t *identity, cbm_daemon_runtime_operation_t operation, uint32_t timeout_ms, uint32_t response_size, + bool shared_budget, uint8_t **payload_out, uint64_t *muted_holder_pid_out) { *payload_out = NULL; if (muted_holder_pid_out) { @@ -3301,8 +3302,9 @@ static bool runtime_control_request_send(const cbm_daemon_ipc_endpoint_t *endpoi cbm_daemon_frame_t frame = {0}; uint8_t *payload = NULL; int received = - sent ? cbm_daemon_ipc_receive_frame_bounded(connection, runtime_timeout_remaining(deadline), - response_size, &frame, &payload) + sent ? cbm_daemon_ipc_receive_frame_bounded( + connection, shared_budget ? runtime_timeout_remaining(deadline) : timeout_ms, + response_size, &frame, &payload) : 0; if (muted_holder_pid_out && sent && (received != 1 || (frame.type == CBM_DAEMON_FRAME_RESPONSE && frame.flags != operation))) { @@ -3350,7 +3352,8 @@ static void runtime_status_response_decode(const uint8_t *payload, static bool runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, const cbm_daemon_build_identity_t *identity, uint32_t timeout_ms, - cbm_daemon_runtime_status_t *status_out, char *cache_directory) { + cbm_daemon_runtime_status_t *status_out, char *cache_directory, + bool shared_budget) { if (!status_out) { return false; } @@ -3361,7 +3364,7 @@ static bool runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, uint32_t response_size = cache_directory ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE : CBM_DAEMON_STATUS_RESPONSE_SIZE; if (!runtime_control_request_send( - endpoint, identity, operation, timeout_ms, response_size, &payload, + endpoint, identity, operation, timeout_ms, response_size, shared_budget, &payload, cache_directory ? NULL : &status_out->muted_endpoint_holder_pid)) { return false; } @@ -3382,7 +3385,7 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint const cbm_daemon_build_identity_t *identity, uint32_t timeout_ms, cbm_daemon_runtime_status_t *status_out) { - return runtime_request_status(endpoint, identity, timeout_ms, status_out, NULL); + return runtime_request_status(endpoint, identity, timeout_ms, status_out, NULL, false); } bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t *endpoint, @@ -3398,12 +3401,12 @@ bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t * } uint64_t deadline = runtime_deadline_after(timeout_ms); if (runtime_request_status(endpoint, identity, timeout_ms, &details_out->status, - details_out->cache_directory)) { + details_out->cache_directory, true)) { return true; } uint32_t remaining = runtime_timeout_remaining(deadline); return remaining > 0 && - cbm_daemon_runtime_request_status(endpoint, identity, remaining, &details_out->status); + runtime_request_status(endpoint, identity, remaining, &details_out->status, NULL, true); } /* "; CBM sessions using it: pids 1, 2 and 3 more" — empty when none are known. */ @@ -3483,7 +3486,7 @@ bool cbm_daemon_runtime_request_stop(const cbm_daemon_ipc_endpoint_t *endpoint, memset(result_out, 0, sizeof(*result_out)); uint8_t *payload = NULL; if (!runtime_control_request_send(endpoint, identity, CBM_DAEMON_RUNTIME_OP_STOP, timeout_ms, - CBM_DAEMON_STOP_RESPONSE_SIZE, &payload, NULL)) { + CBM_DAEMON_STOP_RESPONSE_SIZE, false, &payload, NULL)) { return false; } result_out->accepted = (payload[1] & 0x01U) != 0U; diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index 2a53958b39..4da41cd832 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -5624,6 +5624,96 @@ static void *runtime_test_legacy_status_thread(void *opaque) { return NULL; } +/* Exercise both phases of the real transport: a listener appears after part of + * the timeout has elapsed, then replies after another part has elapsed. */ +typedef struct { + cbm_daemon_ipc_endpoint_t *endpoint; + atomic_bool started; + bool received; +} runtime_delayed_control_t; + +static void *runtime_test_delayed_control_thread(void *opaque) { + runtime_delayed_control_t *server = opaque; + const struct timespec delay = {1, 200000000L}; + atomic_store_explicit(&server->started, true, memory_order_release); + (void)cbm_nanosleep(&delay, NULL); + cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(server->endpoint); + cbm_daemon_ipc_connection_t *connection = NULL; + if (listener && cbm_daemon_ipc_accept(listener, RUNTIME_TEST_TIMEOUT_MS, &connection) == 1) { + cbm_daemon_frame_t frame = {0}; + uint8_t *request = NULL; + server->received = cbm_daemon_ipc_receive_frame(connection, RUNTIME_TEST_TIMEOUT_MS, + &frame, &request) == 1; + if (server->received) { + (void)cbm_nanosleep(&delay, NULL); + uint8_t response[CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE] = {1}; + uint32_t length = frame.flags == CBM_DAEMON_RUNTIME_OP_STOP + ? CBM_DAEMON_STOP_RESPONSE_SIZE + : frame.flags == CBM_DAEMON_RUNTIME_OP_STATUS + ? CBM_DAEMON_STATUS_RESPONSE_SIZE + : CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE; + (void)cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_RESPONSE, frame.flags, + response, length); + } + free(request); + cbm_daemon_ipc_connection_close(connection); + } + cbm_daemon_ipc_listener_close(listener); + return NULL; +} + +TEST(daemon_runtime_control_timeout_budget_is_specific_to_status_details) { + enum { PHASE_TIMEOUT_MS = 2000 }; + cbm_daemon_build_identity_t identity = + runtime_test_identity("2.4.0", runtime_test_self_build()); + for (int operation = 0; operation < 3; operation++) { + char parent[RUNTIME_TEST_PATH_CAP] = {0}; + char key[CBM_DAEMON_KEY_SIZE] = {0}; + char runtime_dir[RUNTIME_TEST_PATH_CAP] = {0}; + bool prepared = th_secure_runtime_parent_new(parent, sizeof(parent), "control-budget") && + cbm_daemon_rendezvous_key(key); + cbm_daemon_ipc_endpoint_t *client = prepared ? cbm_daemon_ipc_endpoint_new(key, parent) : NULL; + cbm_daemon_ipc_endpoint_t *server_endpoint = + client ? cbm_daemon_ipc_endpoint_new(key, parent) : NULL; + bool copied = client && runtime_test_copy_path( + runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(client)); + runtime_delayed_control_t server = {.endpoint = server_endpoint}; + atomic_init(&server.started, false); + cbm_thread_t thread; + bool started = server_endpoint && + cbm_thread_create(&thread, 0, runtime_test_delayed_control_thread, &server) == 0; + bool ready = started && runtime_test_wait_atomic_bool(&server.started, PHASE_TIMEOUT_MS); + uint64_t start = cbm_now_ms(); + bool result = false; + if (ready && operation == 0) { + cbm_daemon_runtime_status_t status = {0}; + result = cbm_daemon_runtime_request_status(client, &identity, PHASE_TIMEOUT_MS, &status); + } else if (ready && operation == 1) { + cbm_daemon_runtime_stop_result_t stop = {0}; + result = cbm_daemon_runtime_request_stop(client, &identity, PHASE_TIMEOUT_MS, &stop); + } else if (ready) { + cbm_daemon_runtime_status_details_t details = {0}; + result = cbm_daemon_runtime_request_status_details(client, &identity, + PHASE_TIMEOUT_MS, &details); + } + uint64_t elapsed = cbm_now_ms() - start; + if (started) + (void)cbm_thread_join(&thread); + cbm_daemon_ipc_endpoint_free(server_endpoint); + cbm_daemon_ipc_endpoint_free(client); + (void)cbm_rmdir(runtime_dir); + (void)cbm_rmdir(parent); + ASSERT_TRUE(prepared && copied && started && ready && server.received); + if (operation < 2) { + ASSERT_TRUE(result); + ASSERT_TRUE(elapsed > PHASE_TIMEOUT_MS); + } else { + ASSERT_FALSE(result); + } + } + PASS(); +} + TEST(daemon_runtime_status_details_falls_back_to_legacy_on_unsupported_or_malformed_reply) { cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", runtime_test_self_build()); @@ -5753,6 +5843,7 @@ TEST(daemon_runtime_stop_refuses_while_committed_clients_exist) { } SUITE(daemon_runtime) { + RUN_TEST(daemon_runtime_control_timeout_budget_is_specific_to_status_details); RUN_TEST(daemon_runtime_status_details_preserves_long_paths_and_owns_copy); RUN_TEST(daemon_runtime_status_details_does_not_disclose_paths_to_forged_peers); RUN_TEST(daemon_runtime_status_details_falls_back_to_legacy_on_unsupported_or_malformed_reply); diff --git a/tests/test_runtime_settings.c b/tests/test_runtime_settings.c index 28e8651d33..c20c87b625 100644 --- a/tests/test_runtime_settings.c +++ b/tests/test_runtime_settings.c @@ -626,7 +626,64 @@ TEST(runtime_settings_ui_load_recovers_a_hot_journal) { PASS(); } +#ifdef _WIN32 +TEST(runtime_settings_windows_long_cache_reads_and_writes_config) { + settings_fixture_t fixture; + ASSERT_TRUE(fixture_open(&fixture)); + const size_t lengths[] = {259, 260, 261, 1024}; + bool ok = true; + for (size_t i = 0; i < sizeof(lengths) / sizeof(lengths[0]) && ok; i++) { + char cache[1100]; + size_t base = strlen(fixture.path); + memcpy(cache, fixture.path, base + 1); + size_t used = base; + while (used < lengths[i]) { + size_t remaining = lengths[i] - used; + if (remaining == 1) { + cache[used++] = 'x'; + } else { + cache[used++] = '/'; + size_t part = remaining - 1 < 32 ? remaining - 1 : 32; + memset(cache + used, 'x', part); + used += part; + } + cache[used] = '\0'; + } + ok = cbm_mkdir_p(cache, 0700); + cbm_config_t *config = ok ? cbm_config_open(cache) : NULL; + ok = config && cbm_config_set(config, "auto_index", "true") == 0; + if (config) + cbm_config_close(config); + ok = ok && cbm_runtime_settings_init(cache); + yyjson_doc *document = ok ? read_snapshot(cache) : NULL; + yyjson_val *setting = document ? item(document, "auto_index") : NULL; + const char *value = setting ? yyjson_get_str(yyjson_obj_get(setting, "value")) : NULL; + ok = ok && value && strcmp(value, "true") == 0; + if (document) + yyjson_doc_free(document); + cbm_runtime_settings_reset_for_tests(); + char db[1120]; + snprintf(db, sizeof(db), "%s/_config.db", cache); + (void)cbm_remove_db_sidecars(db); + (void)cbm_unlink(db); + while (strlen(cache) > base) { + (void)cbm_rmdir(cache); + char *slash = strrchr(cache, '/'); + if (!slash || (size_t)(slash - cache) < base) + break; + *slash = '\0'; + } + } + fixture_close(&fixture); + ASSERT_TRUE(ok); + PASS(); +} +#endif + SUITE(runtime_settings) { +#ifdef _WIN32 + RUN_TEST(runtime_settings_windows_long_cache_reads_and_writes_config); +#endif RUN_TEST(runtime_settings_startup_recovers_a_hot_journal); RUN_TEST(runtime_settings_ui_load_recovers_a_hot_journal); RUN_TEST(runtime_settings_startup_waits_out_a_committing_writer); diff --git a/tests/windows/test_daemon_lifecycle.py b/tests/windows/test_daemon_lifecycle.py index 53cf8674c2..4cacb3c972 100644 --- a/tests/windows/test_daemon_lifecycle.py +++ b/tests/windows/test_daemon_lifecycle.py @@ -9,8 +9,7 @@ * A cold one-shot ``cli`` command is quiet by default; ``cli --verbose`` opts into the startup-tax hint. * ``daemon stop`` on an idle daemon stops it; a second ``stop`` is idempotent. -* Cache mismatches name both canonical paths; if STATUS is unavailable, the - fingerprint fallback explains that the paths are unavailable or unverified. +* Cache mismatches name both canonical paths, including long Windows paths. Every path carries a kill-by-pid backstop so a stuck daemon can never hang the suite (the Windows leg's test-infra hang sensitivity is on record). @@ -85,9 +84,6 @@ def filesystem_path(path): else: path = os.path.join(path, "x" * min(32, remaining - 1)) paths.append(path) - if os.name != "nt": - # Windows forbids these filename characters; POSIX must escape them. - paths.append(os.path.join(work, 'cache-line\nbreak\t"\x1b')) for requested in paths: os.makedirs(filesystem_path(requested), exist_ok=True) @@ -124,56 +120,6 @@ def filesystem_path(path): raise AssertionError("cache rejection disturbed the active daemon") -def check_cache_conflict_fallback(binary, work): - active = os.path.join(work, "cache-fallback-active") - requested = os.path.join(work, "cache-fallback-requested") - runtime = runtime_for(active) - shared = {"CBM_RUNTIME_DIR": runtime, "CBM_STARTUP_TIMEOUT_MS": "1000"} - env = dict(os.environ, CBM_CACHE_DIR=active, **shared) - # An index command waiting for piped arguments owns a cohort lease but - # has not connected to a daemon. This makes STATUS genuinely unavailable. - owner = subprocess.Popen([binary, "cli", "index_repository"], env=env, - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - try: - deadline = time.monotonic() + 15 - while True: - if owner.poll() is not None: - raise AssertionError("cohort owner exited before reading its arguments") - written = False - for directory, _, files in os.walk(runtime): - if "cbm-version-cohort-lifetime-v1.lock" in files: - path = os.path.join(directory, "cbm-version-cohort-lifetime-v1.lock") - written = os.path.getsize(path) > 1 - if written: - break - if time.monotonic() >= deadline: - raise AssertionError("cohort owner did not publish its identity") - time.sleep(0.02) - for args in ([], ["cli", "list_projects"]): - result = run_cli(binary, requested, args, extra_env=shared) - text = output_text(result) - if result.returncode == 0 or not re.search( - r"active cache [0-9a-f]{64}; requested cache [0-9a-f]{64}", text): - raise AssertionError("fallback lost the cache conflict fingerprints: " + text) - if "Cache paths unavailable or unverified." not in text: - raise AssertionError("fingerprint fallback did not explain missing paths: " + text) - if "requested cache:" in text or "daemon cache:" in text: - raise AssertionError("fallback claimed an unverified cache path") - status = run_cli(binary, active, ["daemon", "status"], extra_env=shared) - if status.returncode == 0 or "not running" not in output_text(status): - raise AssertionError("fallback probe started a daemon") - finally: - owner.terminate() - try: - owner.wait(timeout=5) - except subprocess.TimeoutExpired: - owner.kill() - owner.wait(timeout=5) - for stream in (owner.stdin, owner.stdout, owner.stderr): - stream.close() - - def main(): if len(sys.argv) < 2: print("usage: python test_daemon_lifecycle.py ") @@ -257,9 +203,6 @@ def main(): print("PASS: stop retired the idle daemon; second stop was idempotent") daemon_pid = 0 - check_cache_conflict_fallback(binary, work) - print("PASS: fingerprint fallback explains unavailable paths for CLI and MCP") - if not ui_build: print("SKIP: non-UI binary cannot exercise the UI-configuration refusal") else: From c7656013e9273a7e2ab1501c09043176b6ee20bc Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:16:38 +0800 Subject: [PATCH 04/10] fix(daemon): isolate status-details budget and remove timing races Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- scripts/memory-core-baseline.txt | 2 +- src/cli/runtime_settings.c | 16 +- src/daemon/ipc.c | 7 +- src/daemon/runtime.c | 173 +++++++++------ src/daemon/runtime_status_details_internal.h | 24 ++ tests/test_daemon_runtime.c | 222 ++++++++++++------- 6 files changed, 290 insertions(+), 154 deletions(-) create mode 100644 src/daemon/runtime_status_details_internal.h diff --git a/scripts/memory-core-baseline.txt b/scripts/memory-core-baseline.txt index 7e979160d3..c44c6c0813 100644 --- a/scripts/memory-core-baseline.txt +++ b/scripts/memory-core-baseline.txt @@ -29,7 +29,7 @@ src/daemon/application.c 107 src/daemon/bootstrap.c 13 src/daemon/daemon.c 31 src/daemon/frontend.c 12 -src/daemon/ipc.c 146 +src/daemon/ipc.c 144 src/daemon/project_lock.c 6 src/daemon/runtime.c 45 src/daemon/service.c 6 diff --git a/src/cli/runtime_settings.c b/src/cli/runtime_settings.c index 6ecfff1fbd..97c7de5e3a 100644 --- a/src/cli/runtime_settings.c +++ b/src/cli/runtime_settings.c @@ -247,14 +247,16 @@ static bool canonical_cache_path(const char *cache, char out[SETTINGS_PATH_CAP], int cbm_runtime_settings_open_db(const char *path, sqlite3 **db, int flags) { #ifdef _WIN32 *db = NULL; - wchar_t *wide = cbm_path_to_wide(path); - char *extended = wide ? cbm_wide_to_utf8(wide) : NULL; - free(wide); - if (!extended) + wchar_t *wide = cbm_path_to_wide_in(CBM_MEM_CLASS_OTHER, path); + if (!wide) return SQLITE_NOMEM; - int rc = sqlite3_open_v2(extended, db, flags, "win32-longpath"); - free(extended); - return rc; + char extended[SETTINGS_PATH_CAP + 8]; + int converted = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide, -1, extended, + sizeof(extended), NULL, NULL); + cbm_free(CBM_MEM_CLASS_OTHER, wide); + if (!converted) + return SQLITE_CANTOPEN; + return sqlite3_open_v2(extended, db, flags, "win32-longpath"); #else return sqlite3_open_v2(path, db, flags, NULL); #endif diff --git a/src/daemon/ipc.c b/src/daemon/ipc.c index 169e6c85b3..8a7501cb7b 100644 --- a/src/daemon/ipc.c +++ b/src/daemon/ipc.c @@ -9,6 +9,7 @@ #include "foundation/compat_thread.h" #include "foundation/log.h" #include "foundation/macos_acl.h" +#include "foundation/mem_core.h" #include "foundation/platform.h" #include "foundation/private_file_lock_internal.h" #include "foundation/sha256.h" @@ -5420,7 +5421,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) { * dot components: the component walk must still reject those itself. */ bool extended = length >= 240U; size_t prefix_length = extended ? 4U : 0U; - wchar_t *path = calloc(length + prefix_length + 1U, sizeof(*path)); + wchar_t *path = cbm_calloc(CBM_MEM_CLASS_OTHER, (length + prefix_length + 1U) * sizeof(*path)); if (!path) { return false; } @@ -5436,7 +5437,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) { } win_security_t security; if (!win_security_init(&security)) { - free(path); + cbm_free(CBM_MEM_CLASS_OTHER, path); return false; } bool ok = true; @@ -5509,7 +5510,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) { if (ok) { ok = win_runtime_directory_secure(path); } - free(path); + cbm_free(CBM_MEM_CLASS_OTHER, path); return ok; } diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c index 2bfea4368f..06931bda9f 100644 --- a/src/daemon/runtime.c +++ b/src/daemon/runtime.c @@ -4,6 +4,7 @@ #include "daemon/runtime.h" #include "daemon/ipc_internal.h" +#include "daemon/runtime_status_details_internal.h" #include "daemon/service_internal.h" #include "foundation/compat.h" @@ -360,11 +361,6 @@ static uint64_t runtime_deadline_after(uint32_t timeout_ms) { return now_ms + (uint64_t)timeout_ms; } -static uint32_t runtime_timeout_remaining(uint64_t deadline_ms) { - uint64_t now_ms = cbm_now_ms(); - return now_ms < deadline_ms ? (uint32_t)(deadline_ms - now_ms) : 0; -} - static uint32_t runtime_ephemeral_linger_timeout_ms(void) { #ifdef CBM_ENABLE_TEST_SEAMS uint32_t seam = atomic_load(&runtime_ephemeral_linger_timeout_seam); @@ -3278,7 +3274,6 @@ static bool runtime_control_request_send(const cbm_daemon_ipc_endpoint_t *endpoi const cbm_daemon_build_identity_t *identity, cbm_daemon_runtime_operation_t operation, uint32_t timeout_ms, uint32_t response_size, - bool shared_budget, uint8_t **payload_out, uint64_t *muted_holder_pid_out) { *payload_out = NULL; if (muted_holder_pid_out) { @@ -3294,18 +3289,15 @@ static bool runtime_control_request_send(const cbm_daemon_ipc_endpoint_t *endpoi if (written <= 0 || (size_t)written != CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U) { return false; } - uint64_t deadline = runtime_deadline_after(timeout_ms); cbm_daemon_ipc_connection_t *connection = cbm_daemon_ipc_connect(endpoint, timeout_ms); bool sent = connection && cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_REQUEST, operation, request, (uint32_t)sizeof(request)); cbm_daemon_frame_t frame = {0}; uint8_t *payload = NULL; - int received = - sent ? cbm_daemon_ipc_receive_frame_bounded( - connection, shared_budget ? runtime_timeout_remaining(deadline) : timeout_ms, - response_size, &frame, &payload) - : 0; + int received = sent ? cbm_daemon_ipc_receive_frame_bounded(connection, timeout_ms, + response_size, &frame, &payload) + : 0; if (muted_holder_pid_out && sent && (received != 1 || (frame.type == CBM_DAEMON_FRAME_RESPONSE && frame.flags != operation))) { /* Connected but not served: either total silence (dead runtime), or a @@ -3332,48 +3324,25 @@ static uint32_t runtime_control_read_u32(const uint8_t *bytes) { (uint32_t)bytes[3]; } -static void runtime_status_response_decode(const uint8_t *payload, - cbm_daemon_runtime_status_t *status_out) { - status_out->permanent = (payload[1] & 0x01U) != 0U; - status_out->stopping = (payload[1] & 0x02U) != 0U; - status_out->committed_clients = (uint16_t)(((uint16_t)payload[2] << 8) | payload[3]); - status_out->daemon_pid = runtime_control_read_u32(payload + 4); - status_out->client_count = payload[8] > CBM_DAEMON_CONTROL_CLIENT_CAP - ? (uint8_t)CBM_DAEMON_CONTROL_CLIENT_CAP - : payload[8]; - for (size_t index = 0; index < CBM_DAEMON_CONTROL_CLIENT_CAP; index++) { - status_out->client_pids[index] = runtime_control_read_u32(payload + 12U + index * 4U); - } - memcpy(status_out->build_fingerprint, payload + 44U, CBM_DAEMON_BUILD_FINGERPRINT_SIZE); - status_out->build_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U] = '\0'; - memcpy(status_out->semantic_version, payload + 109U, sizeof(status_out->semantic_version)); - status_out->semantic_version[sizeof(status_out->semantic_version) - 1U] = '\0'; -} - -static bool runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint, - const cbm_daemon_build_identity_t *identity, uint32_t timeout_ms, - cbm_daemon_runtime_status_t *status_out, char *cache_directory, - bool shared_budget) { - if (!status_out) { - return false; - } - memset(status_out, 0, sizeof(*status_out)); - uint8_t *payload = NULL; - cbm_daemon_runtime_operation_t operation = - cache_directory ? CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS : CBM_DAEMON_RUNTIME_OP_STATUS; - uint32_t response_size = - cache_directory ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE : CBM_DAEMON_STATUS_RESPONSE_SIZE; - if (!runtime_control_request_send( - endpoint, identity, operation, timeout_ms, response_size, shared_budget, &payload, - cache_directory ? NULL : &status_out->muted_endpoint_holder_pid)) { - return false; - } - bool valid = !cache_directory || memchr(payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, '\0', - CBM_DAEMON_STATUS_CACHE_PATH_SIZE); +static bool runtime_status_response_finish(uint8_t *payload, bool valid, + cbm_daemon_runtime_status_t *status_out, char *cache) { if (valid) { - runtime_status_response_decode(payload, status_out); - if (cache_directory) { - memcpy(cache_directory, payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, + status_out->permanent = (payload[1] & 0x01U) != 0U; + status_out->stopping = (payload[1] & 0x02U) != 0U; + status_out->committed_clients = (uint16_t)(((uint16_t)payload[2] << 8) | payload[3]); + status_out->daemon_pid = runtime_control_read_u32(payload + 4); + status_out->client_count = payload[8] > CBM_DAEMON_CONTROL_CLIENT_CAP + ? (uint8_t)CBM_DAEMON_CONTROL_CLIENT_CAP + : payload[8]; + for (size_t index = 0; index < CBM_DAEMON_CONTROL_CLIENT_CAP; index++) { + status_out->client_pids[index] = runtime_control_read_u32(payload + 12U + index * 4U); + } + memcpy(status_out->build_fingerprint, payload + 44U, CBM_DAEMON_BUILD_FINGERPRINT_SIZE); + status_out->build_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U] = '\0'; + memcpy(status_out->semantic_version, payload + 109U, sizeof(status_out->semantic_version)); + status_out->semantic_version[sizeof(status_out->semantic_version) - 1U] = '\0'; + if (cache) { + memcpy(cache, payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, CBM_DAEMON_STATUS_CACHE_PATH_SIZE); } } @@ -3385,7 +3354,80 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint const cbm_daemon_build_identity_t *identity, uint32_t timeout_ms, cbm_daemon_runtime_status_t *status_out) { - return runtime_request_status(endpoint, identity, timeout_ms, status_out, NULL, false); + if (!status_out) { + return false; + } + memset(status_out, 0, sizeof(*status_out)); + uint8_t *payload = NULL; + if (!runtime_control_request_send(endpoint, identity, CBM_DAEMON_RUNTIME_OP_STATUS, timeout_ms, + CBM_DAEMON_STATUS_RESPONSE_SIZE, &payload, + &status_out->muted_endpoint_holder_pid)) { + return false; + } + return runtime_status_response_finish(payload, true, status_out, NULL); +} + +static const cbm_daemon_status_details_transport_t runtime_status_details_transport = { + .now_ms = cbm_now_ms, + .connect = cbm_daemon_ipc_connect, + .send = cbm_daemon_ipc_send_frame, + .receive = cbm_daemon_ipc_receive_frame_bounded, + .close = cbm_daemon_ipc_connection_close, +}; + +#ifdef CBM_ENABLE_TEST_SEAMS +static const cbm_daemon_status_details_transport_t *runtime_status_details_transport_seam; +void cbm_daemon_runtime_set_status_details_transport_for_testing( + const cbm_daemon_status_details_transport_t *transport) { + runtime_status_details_transport_seam = transport; +} +#endif + +static uint32_t runtime_status_details_remaining( + const cbm_daemon_status_details_transport_t *transport, uint64_t deadline) { + uint64_t now = transport->now_ms(); + return now < deadline ? (uint32_t)(deadline - now) : 0; +} + +/* This deadline belongs only to STATUS_DETAILS and its optional legacy retry. */ +static bool runtime_status_request_until(const cbm_daemon_ipc_endpoint_t *endpoint, + const cbm_daemon_build_identity_t *identity, + uint64_t deadline, + const cbm_daemon_status_details_transport_t *transport, + cbm_daemon_runtime_status_t *status, char *cache) { + uint8_t request[CBM_DAEMON_CONTROL_REQUEST_SIZE] = {0}; + int written = snprintf((char *)request, sizeof(request), "%s", identity->build_fingerprint); + if (written <= 0 || (size_t)written != CBM_DAEMON_BUILD_FINGERPRINT_SIZE - 1U) { + return false; + } + uint32_t remaining = runtime_status_details_remaining(transport, deadline); + if (remaining == 0) { + return false; + } + cbm_daemon_ipc_connection_t *connection = transport->connect(endpoint, remaining); + if (!connection) { + return false; + } + cbm_daemon_runtime_operation_t operation = + cache ? CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS : CBM_DAEMON_RUNTIME_OP_STATUS; + uint32_t response_size = + cache ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE : CBM_DAEMON_STATUS_RESPONSE_SIZE; + bool sent = runtime_status_details_remaining(transport, deadline) > 0 && + transport->send(connection, CBM_DAEMON_FRAME_REQUEST, operation, request, + (uint32_t)sizeof(request)); + remaining = runtime_status_details_remaining(transport, deadline); + cbm_daemon_frame_t frame = {0}; + uint8_t *payload = NULL; + int received = sent && remaining > 0 + ? transport->receive(connection, remaining, response_size, &frame, &payload) + : 0; + bool valid = received == 1 && frame.type == CBM_DAEMON_FRAME_RESPONSE && + frame.flags == operation && frame.length == response_size && payload && + payload[0] == 1U && + (!cache || memchr(payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, '\0', + CBM_DAEMON_STATUS_CACHE_PATH_SIZE)); + transport->close(connection); + return runtime_status_response_finish(payload, valid, status, cache); } bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t *endpoint, @@ -3396,17 +3438,24 @@ bool cbm_daemon_runtime_request_status_details(const cbm_daemon_ipc_endpoint_t * return false; } memset(details_out, 0, sizeof(*details_out)); - if (timeout_ms == CBM_DAEMON_IPC_WAIT_FOREVER) { + if (!endpoint || !identity || !identity->build_fingerprint || + timeout_ms == CBM_DAEMON_IPC_WAIT_FOREVER) { return false; } - uint64_t deadline = runtime_deadline_after(timeout_ms); - if (runtime_request_status(endpoint, identity, timeout_ms, &details_out->status, - details_out->cache_directory, true)) { + const cbm_daemon_status_details_transport_t *transport = &runtime_status_details_transport; +#ifdef CBM_ENABLE_TEST_SEAMS + if (runtime_status_details_transport_seam) { + transport = runtime_status_details_transport_seam; + } +#endif + uint64_t now = transport->now_ms(); + uint64_t deadline = UINT64_MAX - now < timeout_ms ? UINT64_MAX : now + timeout_ms; + if (runtime_status_request_until(endpoint, identity, deadline, transport, &details_out->status, + details_out->cache_directory)) { return true; } - uint32_t remaining = runtime_timeout_remaining(deadline); - return remaining > 0 && - runtime_request_status(endpoint, identity, remaining, &details_out->status, NULL, true); + return runtime_status_request_until(endpoint, identity, deadline, transport, + &details_out->status, NULL); } /* "; CBM sessions using it: pids 1, 2 and 3 more" — empty when none are known. */ @@ -3486,7 +3535,7 @@ bool cbm_daemon_runtime_request_stop(const cbm_daemon_ipc_endpoint_t *endpoint, memset(result_out, 0, sizeof(*result_out)); uint8_t *payload = NULL; if (!runtime_control_request_send(endpoint, identity, CBM_DAEMON_RUNTIME_OP_STOP, timeout_ms, - CBM_DAEMON_STOP_RESPONSE_SIZE, false, &payload, NULL)) { + CBM_DAEMON_STOP_RESPONSE_SIZE, &payload, NULL)) { return false; } result_out->accepted = (payload[1] & 0x01U) != 0U; diff --git a/src/daemon/runtime_status_details_internal.h b/src/daemon/runtime_status_details_internal.h new file mode 100644 index 0000000000..46579ba087 --- /dev/null +++ b/src/daemon/runtime_status_details_internal.h @@ -0,0 +1,24 @@ +#ifndef CBM_DAEMON_RUNTIME_STATUS_DETAILS_INTERNAL_H +#define CBM_DAEMON_RUNTIME_STATUS_DETAILS_INTERNAL_H + +#include "daemon/ipc.h" + +/* Dependencies of the new STATUS_DETAILS exchange only. Legacy STATUS/STOP + * continue to use runtime_control_request_send unchanged. */ +typedef struct { + uint64_t (*now_ms)(void); + cbm_daemon_ipc_connection_t *(*connect)(const cbm_daemon_ipc_endpoint_t *, uint32_t); + bool (*send)(cbm_daemon_ipc_connection_t *, cbm_daemon_frame_type_t, uint16_t, const void *, + uint32_t); + int (*receive)(cbm_daemon_ipc_connection_t *, uint32_t, uint32_t, cbm_daemon_frame_t *, + uint8_t **); + void (*close)(cbm_daemon_ipc_connection_t *); +} cbm_daemon_status_details_transport_t; + +#ifdef CBM_ENABLE_TEST_SEAMS +/* Single-threaded test scope; NULL restores real IPC and the monotonic clock. */ +void cbm_daemon_runtime_set_status_details_transport_for_testing( + const cbm_daemon_status_details_transport_t *transport); +#endif + +#endif diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index 4da41cd832..baf245341d 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -14,6 +14,7 @@ #include "daemon/host_internal.h" #include "daemon/ipc.h" #include "daemon/runtime.h" +#include "daemon/runtime_status_details_internal.h" #include "daemon/service.h" #include "daemon/version_cohort.h" #include "foundation/compat.h" @@ -5624,92 +5625,150 @@ static void *runtime_test_legacy_status_thread(void *opaque) { return NULL; } -/* Exercise both phases of the real transport: a listener appears after part of - * the timeout has elapsed, then replies after another part has elapsed. */ +/* An opaque token replaces IPC; only the scripted clock advances. Scheduler + * delays cannot consume the test budget or decide its result. */ typedef struct { - cbm_daemon_ipc_endpoint_t *endpoint; - atomic_bool started; - bool received; -} runtime_delayed_control_t; - -static void *runtime_test_delayed_control_thread(void *opaque) { - runtime_delayed_control_t *server = opaque; - const struct timespec delay = {1, 200000000L}; - atomic_store_explicit(&server->started, true, memory_order_release); - (void)cbm_nanosleep(&delay, NULL); - cbm_daemon_ipc_listener_t *listener = cbm_daemon_ipc_listen(server->endpoint); - cbm_daemon_ipc_connection_t *connection = NULL; - if (listener && cbm_daemon_ipc_accept(listener, RUNTIME_TEST_TIMEOUT_MS, &connection) == 1) { - cbm_daemon_frame_t frame = {0}; - uint8_t *request = NULL; - server->received = cbm_daemon_ipc_receive_frame(connection, RUNTIME_TEST_TIMEOUT_MS, - &frame, &request) == 1; - if (server->received) { - (void)cbm_nanosleep(&delay, NULL); - uint8_t response[CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE] = {1}; - uint32_t length = frame.flags == CBM_DAEMON_RUNTIME_OP_STOP - ? CBM_DAEMON_STOP_RESPONSE_SIZE - : frame.flags == CBM_DAEMON_RUNTIME_OP_STATUS - ? CBM_DAEMON_STATUS_RESPONSE_SIZE - : CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE; - (void)cbm_daemon_ipc_send_frame(connection, CBM_DAEMON_FRAME_RESPONSE, frame.flags, - response, length); - } - free(request); - cbm_daemon_ipc_connection_close(connection); + uint64_t now; + uint32_t connect_cost[2], send_cost[2], receive_cost[2]; + uint32_t connect_budget[2], receive_budget[2]; + unsigned connects, sends, receives, closes; + int first_reply; /* 0 = unsupported, 1 = malformed, 2 = supported */ + bool valid; +} runtime_status_transport_stub_t; +static runtime_status_transport_stub_t runtime_status_stub; + +static uint64_t runtime_test_status_now(void) { + return runtime_status_stub.now; +} +static cbm_daemon_ipc_connection_t *runtime_test_status_connect( + const cbm_daemon_ipc_endpoint_t *endpoint, uint32_t timeout) { + runtime_status_transport_stub_t *stub = &runtime_status_stub; + if (stub->connects >= 2) { + stub->valid = false; + return NULL; } - cbm_daemon_ipc_listener_close(listener); - return NULL; + unsigned attempt = stub->connects++; + stub->valid &= endpoint == (const void *)stub; + stub->connect_budget[attempt] = timeout; + stub->now += stub->connect_cost[attempt]; + return (void *)stub; +} +static bool runtime_test_status_send(cbm_daemon_ipc_connection_t *connection, + cbm_daemon_frame_type_t type, uint16_t flags, + const void *payload, uint32_t length) { + runtime_status_transport_stub_t *stub = &runtime_status_stub; + unsigned attempt = stub->connects - 1; + stub->valid &= connection == (void *)stub && type == CBM_DAEMON_FRAME_REQUEST && + flags == (attempt == 0 ? CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS + : CBM_DAEMON_RUNTIME_OP_STATUS) && + payload && length == CBM_DAEMON_CONTROL_REQUEST_SIZE; + stub->sends++; + stub->now += stub->send_cost[attempt]; + return true; } +static int runtime_test_status_receive(cbm_daemon_ipc_connection_t *connection, uint32_t timeout, + uint32_t cap, cbm_daemon_frame_t *frame, uint8_t **payload) { + runtime_status_transport_stub_t *stub = &runtime_status_stub; + unsigned attempt = stub->connects - 1; + stub->valid &= + connection == (void *)stub && cap == (attempt == 0 ? CBM_DAEMON_STATUS_DETAILS_RESPONSE_SIZE + : CBM_DAEMON_STATUS_RESPONSE_SIZE); + stub->receives++; + stub->receive_budget[attempt] = timeout; + *payload = NULL; + if (stub->receive_cost[attempt] >= timeout) { + stub->now += timeout; + return 0; + } + stub->now += stub->receive_cost[attempt]; + if (attempt == 0 && stub->first_reply == 0) + return 0; + *payload = calloc(cap, 1); + if (!*payload) + return -1; + (*payload)[0] = 1; + runtime_test_put_u32(*payload + 4, 77); + if (attempt == 0) { + memset(*payload + CBM_DAEMON_STATUS_RESPONSE_SIZE, stub->first_reply == 1 ? 'x' : 0, + CBM_DAEMON_STATUS_CACHE_PATH_SIZE); + } + frame->type = CBM_DAEMON_FRAME_RESPONSE; + frame->flags = + attempt == 0 ? CBM_DAEMON_RUNTIME_OP_STATUS_DETAILS : CBM_DAEMON_RUNTIME_OP_STATUS; + frame->length = cap; + return 1; +} +static void runtime_test_status_close(cbm_daemon_ipc_connection_t *connection) { + runtime_status_stub.valid &= connection == (void *)&runtime_status_stub; + runtime_status_stub.closes++; +} +static const cbm_daemon_status_details_transport_t runtime_test_status_transport = { + .now_ms = runtime_test_status_now, + .connect = runtime_test_status_connect, + .send = runtime_test_status_send, + .receive = runtime_test_status_receive, + .close = runtime_test_status_close, +}; -TEST(daemon_runtime_control_timeout_budget_is_specific_to_status_details) { - enum { PHASE_TIMEOUT_MS = 2000 }; - cbm_daemon_build_identity_t identity = - runtime_test_identity("2.4.0", runtime_test_self_build()); - for (int operation = 0; operation < 3; operation++) { - char parent[RUNTIME_TEST_PATH_CAP] = {0}; - char key[CBM_DAEMON_KEY_SIZE] = {0}; - char runtime_dir[RUNTIME_TEST_PATH_CAP] = {0}; - bool prepared = th_secure_runtime_parent_new(parent, sizeof(parent), "control-budget") && - cbm_daemon_rendezvous_key(key); - cbm_daemon_ipc_endpoint_t *client = prepared ? cbm_daemon_ipc_endpoint_new(key, parent) : NULL; - cbm_daemon_ipc_endpoint_t *server_endpoint = - client ? cbm_daemon_ipc_endpoint_new(key, parent) : NULL; - bool copied = client && runtime_test_copy_path( - runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(client)); - runtime_delayed_control_t server = {.endpoint = server_endpoint}; - atomic_init(&server.started, false); - cbm_thread_t thread; - bool started = server_endpoint && - cbm_thread_create(&thread, 0, runtime_test_delayed_control_thread, &server) == 0; - bool ready = started && runtime_test_wait_atomic_bool(&server.started, PHASE_TIMEOUT_MS); - uint64_t start = cbm_now_ms(); - bool result = false; - if (ready && operation == 0) { - cbm_daemon_runtime_status_t status = {0}; - result = cbm_daemon_runtime_request_status(client, &identity, PHASE_TIMEOUT_MS, &status); - } else if (ready && operation == 1) { - cbm_daemon_runtime_stop_result_t stop = {0}; - result = cbm_daemon_runtime_request_stop(client, &identity, PHASE_TIMEOUT_MS, &stop); - } else if (ready) { - cbm_daemon_runtime_status_details_t details = {0}; - result = cbm_daemon_runtime_request_status_details(client, &identity, - PHASE_TIMEOUT_MS, &details); +TEST(daemon_runtime_status_details_uses_one_budget_across_legacy_fallback) { + cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", RUNTIME_BUILD_B); + for (int reply = 0; reply < 3; reply++) { + runtime_status_stub = (runtime_status_transport_stub_t){ + .now = 10000, + .connect_cost = {100, 200}, + .send_cost = {20, 30}, + .receive_cost = {200, 300}, + .first_reply = reply, + .valid = true, + }; + cbm_daemon_runtime_status_details_t details = {0}; + cbm_daemon_runtime_set_status_details_transport_for_testing(&runtime_test_status_transport); + bool queried = cbm_daemon_runtime_request_status_details((const void *)&runtime_status_stub, + &identity, 1000, &details); + cbm_daemon_runtime_set_status_details_transport_for_testing(NULL); + ASSERT_TRUE(queried && runtime_status_stub.valid); + ASSERT_EQ(details.status.daemon_pid, 77); + ASSERT_EQ(runtime_status_stub.connect_budget[0], 1000); + ASSERT_EQ(runtime_status_stub.receive_budget[0], 880); + ASSERT_EQ(runtime_status_stub.connects, reply == 2 ? 1 : 2); + ASSERT_EQ(runtime_status_stub.closes, runtime_status_stub.connects); + if (reply != 2) { + ASSERT_EQ(runtime_status_stub.connect_budget[1], 680); + ASSERT_EQ(runtime_status_stub.receive_budget[1], 450); + ASSERT_EQ(details.cache_directory[0], '\0'); } - uint64_t elapsed = cbm_now_ms() - start; - if (started) - (void)cbm_thread_join(&thread); - cbm_daemon_ipc_endpoint_free(server_endpoint); - cbm_daemon_ipc_endpoint_free(client); - (void)cbm_rmdir(runtime_dir); - (void)cbm_rmdir(parent); - ASSERT_TRUE(prepared && copied && started && ready && server.received); - if (operation < 2) { - ASSERT_TRUE(result); - ASSERT_TRUE(elapsed > PHASE_TIMEOUT_MS); - } else { - ASSERT_FALSE(result); + } + PASS(); +} + +TEST(daemon_runtime_status_details_stops_when_budget_expires) { + cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", RUNTIME_BUILD_B); + for (int stage = 0; stage < 4; stage++) { + runtime_status_stub = (runtime_status_transport_stub_t){.now = 10000, .valid = true}; + if (stage == 0) + runtime_status_stub.connect_cost[0] = 1000; + else if (stage == 1) + runtime_status_stub.send_cost[0] = 1000; + else if (stage == 2) + runtime_status_stub.receive_cost[0] = 1000; + else { + runtime_status_stub.receive_cost[0] = 400; + runtime_status_stub.connect_cost[1] = 600; } + cbm_daemon_runtime_status_details_t details = {0}; + cbm_daemon_runtime_set_status_details_transport_for_testing(&runtime_test_status_transport); + bool queried = cbm_daemon_runtime_request_status_details((const void *)&runtime_status_stub, + &identity, 1000, &details); + cbm_daemon_runtime_set_status_details_transport_for_testing(NULL); + ASSERT_FALSE(queried); + ASSERT_TRUE(runtime_status_stub.valid); + ASSERT_EQ(runtime_status_stub.now, 11000); + ASSERT_EQ(runtime_status_stub.connects, stage == 3 ? 2 : 1); + ASSERT_EQ(runtime_status_stub.closes, runtime_status_stub.connects); + ASSERT_EQ(runtime_status_stub.sends, stage == 0 ? 0 : 1); + ASSERT_EQ(runtime_status_stub.receives, stage < 2 ? 0 : 1); + if (stage == 3) + ASSERT_EQ(runtime_status_stub.connect_budget[1], 600); } PASS(); } @@ -5843,7 +5902,8 @@ TEST(daemon_runtime_stop_refuses_while_committed_clients_exist) { } SUITE(daemon_runtime) { - RUN_TEST(daemon_runtime_control_timeout_budget_is_specific_to_status_details); + RUN_TEST(daemon_runtime_status_details_uses_one_budget_across_legacy_fallback); + RUN_TEST(daemon_runtime_status_details_stops_when_budget_expires); RUN_TEST(daemon_runtime_status_details_preserves_long_paths_and_owns_copy); RUN_TEST(daemon_runtime_status_details_does_not_disclose_paths_to_forged_peers); RUN_TEST(daemon_runtime_status_details_falls_back_to_legacy_on_unsupported_or_malformed_reply); From 889170ca93283250e19839f742106c661b62b6e1 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:17:14 -0600 Subject: [PATCH 05/10] fix(daemon): distinguish IPC transport callback from raw connect Rename the internal status-details transport callback to ipc_connect so static network auditing does not mistake it for a raw connect syscall. Keep the production IPC implementation, test seam, and network policy unchanged. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- src/daemon/runtime.c | 4 ++-- src/daemon/runtime_status_details_internal.h | 2 +- tests/test_daemon_runtime.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c index 06931bda9f..6891791741 100644 --- a/src/daemon/runtime.c +++ b/src/daemon/runtime.c @@ -3369,7 +3369,7 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint static const cbm_daemon_status_details_transport_t runtime_status_details_transport = { .now_ms = cbm_now_ms, - .connect = cbm_daemon_ipc_connect, + .ipc_connect = cbm_daemon_ipc_connect, .send = cbm_daemon_ipc_send_frame, .receive = cbm_daemon_ipc_receive_frame_bounded, .close = cbm_daemon_ipc_connection_close, @@ -3404,7 +3404,7 @@ static bool runtime_status_request_until(const cbm_daemon_ipc_endpoint_t *endpoi if (remaining == 0) { return false; } - cbm_daemon_ipc_connection_t *connection = transport->connect(endpoint, remaining); + cbm_daemon_ipc_connection_t *connection = transport->ipc_connect(endpoint, remaining); if (!connection) { return false; } diff --git a/src/daemon/runtime_status_details_internal.h b/src/daemon/runtime_status_details_internal.h index 46579ba087..60089a6c80 100644 --- a/src/daemon/runtime_status_details_internal.h +++ b/src/daemon/runtime_status_details_internal.h @@ -7,7 +7,7 @@ * continue to use runtime_control_request_send unchanged. */ typedef struct { uint64_t (*now_ms)(void); - cbm_daemon_ipc_connection_t *(*connect)(const cbm_daemon_ipc_endpoint_t *, uint32_t); + cbm_daemon_ipc_connection_t *(*ipc_connect)(const cbm_daemon_ipc_endpoint_t *, uint32_t); bool (*send)(cbm_daemon_ipc_connection_t *, cbm_daemon_frame_type_t, uint16_t, const void *, uint32_t); int (*receive)(cbm_daemon_ipc_connection_t *, uint32_t, uint32_t, cbm_daemon_frame_t *, diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index baf245341d..3d72a88e86 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -5704,7 +5704,7 @@ static void runtime_test_status_close(cbm_daemon_ipc_connection_t *connection) { } static const cbm_daemon_status_details_transport_t runtime_test_status_transport = { .now_ms = runtime_test_status_now, - .connect = runtime_test_status_connect, + .ipc_connect = runtime_test_status_connect, .send = runtime_test_status_send, .receive = runtime_test_status_receive, .close = runtime_test_status_close, From ce654019b0c4fa04a271936361e6487867461ae4 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:03:52 -0600 Subject: [PATCH 06/10] test(subprocess): measure RSS growth within one process tree Measure the same root and descendant before and after the controlled 128 MiB allocation. The descendant publishes an idle marker, waits for the parent to measure it, then touches the allocation and publishes the loaded marker. Assert that both markers identify the same descendant. Keep the 96 MiB growth floor, member-fault coverage, and terminal-empty checks. Do not change production RSS measurement or weaken CI checks. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- tests/test_main.c | 71 ++++++++++++++++++++++++++++------------- tests/test_subprocess.c | 66 ++++++++++++++++++++++++++------------ 2 files changed, 93 insertions(+), 44 deletions(-) diff --git a/tests/test_main.c b/tests/test_main.c index 4b3716c50c..464781aa7b 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -1976,25 +1976,11 @@ static int tf_role_env_isolation(int argc, char **argv) { } /* Process-tree memory probe coverage (test_subprocess.c). The root starts ONE - * descendant that charges a controlled amount of memory, so a tree total can - * be checked against an allocation only a descendant holds. The descendant - * publishes its pid by an atomic rename once every page is touched, then - * blocks until it is killed. The root exits when its descendant dies, so a - * failed setup ends the tree instead of leaving the test waiting. */ -static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) { - char *end = NULL; - long mib = strtol(mib_text, &end, 10); - if (!end || *end != '\0' || mib < 0 || mib > 1024) { - return 81; - } - size_t bytes = (size_t)mib * 1024U * 1024U; - unsigned char *block = bytes > 0 ? malloc(bytes) : NULL; - if (bytes > 0 && !block) { - return 82; - } - if (block) { - memset(block, 0xA5, bytes); - } + * descendant that holds a controlled allocation. With a load path, it first + * publishes an idle marker and waits for the parent to finish its baseline + * sample. It then touches every page and publishes readiness. The root exits + * when the descendant dies, including on setup failure. */ +static int tf_rss_tree_publish_ready(const char *ready_path) { char staged[1024]; int length = snprintf(staged, sizeof(staged), "%s.tmp", ready_path); FILE *ready = length > 0 && (size_t)length < sizeof(staged) ? cbm_fopen(staged, "wb") : NULL; @@ -2009,6 +1995,41 @@ static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) { if (fclose(ready) != 0 || rename(staged, ready_path) != 0) { return 84; } + return 0; +} + +static int tf_rss_tree_hold(const char *ready_path, const char *mib_text, const char *load_path) { + char *end = NULL; + long mib = strtol(mib_text, &end, 10); + if (!end || *end != '\0' || mib < 0 || mib > 1024) { + return 81; + } + if (load_path) { + char idle_path[1024]; + int length = snprintf(idle_path, sizeof(idle_path), "%s.idle", ready_path); + if (length <= 0 || (size_t)length >= sizeof(idle_path)) { + return 83; + } + int idle_rc = tf_rss_tree_publish_ready(idle_path); + if (idle_rc != 0) { + return idle_rc; + } + while (!cbm_file_exists(load_path)) { + cbm_usleep(10000); + } + } + size_t bytes = (size_t)mib * 1024U * 1024U; + unsigned char *block = bytes > 0 ? malloc(bytes) : NULL; + if (bytes > 0 && !block) { + return 82; + } + if (block) { + memset(block, 0xA5, bytes); + } + int ready_rc = tf_rss_tree_publish_ready(ready_path); + if (ready_rc != 0) { + return ready_rc; + } volatile unsigned char keep = block ? block[bytes - 1] : 0; (void)keep; for (;;) { @@ -2021,16 +2042,20 @@ static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) { } static int tf_role_rss_tree_hold(int argc, char **argv) { - return argc == 4 ? tf_rss_tree_hold(argv[2], argv[3]) : -1; + if (argc != 4 && argc != 5) { + return -1; + } + return tf_rss_tree_hold(argv[2], argv[3], argc == 5 ? argv[4] : NULL); } static int tf_role_rss_tree_root(int argc, char **argv) { - if (argc != 4) { + if (argc != 4 && argc != 5) { return -1; } #ifdef _WIN32 char *self = cbm_module_path_utf8(); - const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], NULL}; + const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], + argc == 5 ? argv[4] : NULL, NULL}; char command_line[8192]; bool built = self && cbm_build_win_cmdline(command_line, sizeof(command_line), child_argv); wchar_t *wide_self = built ? cbm_utf8_to_wide(self) : NULL; @@ -2060,7 +2085,7 @@ static int tf_role_rss_tree_root(int argc, char **argv) { return 85; } if (child == 0) { - _exit(tf_rss_tree_hold(argv[2], argv[3])); + _exit(tf_rss_tree_hold(argv[2], argv[3], argc == 5 ? argv[4] : NULL)); } int status = 0; while (waitpid(child, &status, 0) < 0 && errno == EINTR) { diff --git a/tests/test_subprocess.c b/tests/test_subprocess.c index e214a2c36f..97f2a07f1d 100644 --- a/tests/test_subprocess.c +++ b/tests/test_subprocess.c @@ -828,7 +828,12 @@ TEST(subprocess_spawn_returns_while_child_is_running) { typedef struct { bool spawned; + bool idle_ready; + long idle_pid; + cbm_proc_tree_rss_status_t idle_status; + uint64_t idle_rss; bool ready; + long pid; cbm_proc_tree_rss_status_t status; uint64_t rss; bool quiesced; @@ -840,7 +845,8 @@ static void rss_tree_pause(void) { cbm_usleep(10000); /* poll interval only; nothing here is a deadline */ } -static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process) { +static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process, + long *pid_out) { for (;;) { FILE *ready = cbm_fopen(ready_path, "rb"); if (ready) { @@ -848,6 +854,7 @@ static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *proces int fields = fscanf(ready, "%ld", &pid); (void)fclose(ready); if (fields == 1 && pid > 0) { + *pid_out = pid; return true; } } @@ -880,20 +887,26 @@ static bool rss_tree_finish(cbm_subprocess_t *process, rss_tree_sample_t *sample /* Measure one tree whose descendant holds `mib` MiB, with every non-root * member's measurement failing with `member_fault` (0 = real calls). */ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { - rss_tree_sample_t sample = {.status = CBM_PROC_TREE_RSS_ERROR, + rss_tree_sample_t sample = {.idle_status = CBM_PROC_TREE_RSS_ERROR, + .status = CBM_PROC_TREE_RSS_ERROR, .final_status = CBM_PROC_TREE_RSS_ERROR, .final_rss = UINT64_MAX}; const char *made = th_mktempdir("cbm_rss_tree"); char dir[256]; char ready_path[300]; + char idle_path[320]; + char load_path[300]; char mib_text[16]; + bool phased = mib > 0 && member_fault == 0; if (!made || snprintf(dir, sizeof(dir), "%s", made) >= (int)sizeof(dir) || - snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path)) { + snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path) || + snprintf(idle_path, sizeof(idle_path), "%s.idle", ready_path) >= (int)sizeof(idle_path) || + snprintf(load_path, sizeof(load_path), "%s/load", dir) >= (int)sizeof(load_path)) { return sample; } (void)snprintf(mib_text, sizeof(mib_text), "%d", mib); const char *argv[] = {cbm_test_program_path, "__cbm_rss_tree_root", ready_path, mib_text, - NULL}; + phased ? load_path : NULL, NULL}; cbm_proc_opts_t opts = {0}; opts.bin = cbm_test_program_path; opts.argv = argv; @@ -901,7 +914,19 @@ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { cbm_subprocess_t *process = NULL; sample.spawned = cbm_subprocess_spawn(&opts, &process) == 0; if (sample.spawned) { - sample.ready = rss_tree_wait_ready(ready_path, process); + bool load_requested = !phased; + if (phased) { + sample.idle_ready = rss_tree_wait_ready(idle_path, process, &sample.idle_pid); + if (sample.idle_ready) { + sample.idle_status = cbm_subprocess_tree_rss_bytes(process, &sample.idle_rss); + /* Release only after a successful idle measurement. */ + if (sample.idle_status == CBM_PROC_TREE_RSS_OK && sample.idle_rss > 0) { + FILE *load = cbm_fopen(load_path, "wb"); + load_requested = load && fclose(load) == 0; + } + } + } + sample.ready = load_requested && rss_tree_wait_ready(ready_path, process, &sample.pid); if (sample.ready) { cbm_subprocess_tree_rss_fault_members_for_testing(member_fault); sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss); @@ -913,27 +938,26 @@ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { return sample; } -/* Only the DESCENDANT allocates, so the difference between two otherwise - * identical trees is memory the probe can only have counted by measuring past - * the root. 128 MiB touched against a 96 MiB floor leaves room for allocator - * and page-rounding noise and none for a root-only total. */ +/* Only the descendant allocates, after the parent samples the idle tree and + * releases it through the load marker. Measure the SAME processes before and + * after the 128 MiB allocation: independently spawned trees need not have the + * same executable/sanitizer residency, notably on Windows. Keep the 96 MiB + * floor so a root-only probe or an untouched allocation cannot pass. */ TEST(subprocess_tree_rss_measures_contained_descendants) { - rss_tree_sample_t idle = rss_tree_sample(0, 0); rss_tree_sample_t loaded = rss_tree_sample(128, 0); - if (loaded.status == CBM_PROC_TREE_RSS_OK && idle.status == CBM_PROC_TREE_RSS_OK) { - printf(" tree rss: idle=%llu MiB loaded=%llu MiB\n", - (unsigned long long)(idle.rss / RSS_TREE_MIB), - (unsigned long long)(loaded.rss / RSS_TREE_MIB)); - } - ASSERT_TRUE(idle.spawned); - ASSERT_TRUE(idle.ready); - ASSERT_EQ(idle.status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(idle.rss > 0); + if (loaded.status == CBM_PROC_TREE_RSS_OK && loaded.idle_status == CBM_PROC_TREE_RSS_OK) { + printf(" tree rss: idle(pid=%ld)=%llu MiB loaded(pid=%ld)=%llu MiB\n", + loaded.idle_pid, (unsigned long long)(loaded.idle_rss / RSS_TREE_MIB), + loaded.pid, (unsigned long long)(loaded.rss / RSS_TREE_MIB)); + } ASSERT_TRUE(loaded.spawned); + ASSERT_TRUE(loaded.idle_ready); + ASSERT_EQ(loaded.idle_status, CBM_PROC_TREE_RSS_OK); + ASSERT_TRUE(loaded.idle_rss > 0); ASSERT_TRUE(loaded.ready); + ASSERT_EQ(loaded.pid, loaded.idle_pid); ASSERT_EQ(loaded.status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(loaded.rss >= idle.rss + 96 * RSS_TREE_MIB); - ASSERT_TRUE(idle.quiesced); + ASSERT_TRUE(loaded.rss >= loaded.idle_rss + 96 * RSS_TREE_MIB); ASSERT_TRUE(loaded.quiesced); ASSERT_EQ(loaded.final_status, CBM_PROC_TREE_RSS_EMPTY); ASSERT_EQ(loaded.final_rss, 0); From af0205d0cc41811723132a57718e4214efd4a027 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 04:21:15 -0600 Subject: [PATCH 07/10] fix(cli): honor skip-binary for existing targets Keep config-only installs from re-enabling binary replacement, and make macOS signing respect the same skip-binary policy. Cover existing and absent targets, content and metadata preservation, agent configuration updates, and approved replacement controls. Add a real-IPC existing-target no-drain case alongside the absent-target case. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- src/cli/cli.c | 4 +- tests/test_cli.c | 255 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 239 insertions(+), 20 deletions(-) diff --git a/src/cli/cli.c b/src/cli/cli.c index a142586564..0204bb2033 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -12583,7 +12583,7 @@ int cbm_cmd_install(int argc, char **argv) { * `install --force` from a freshly built binary silently kept the OLD file * — operators ran stale code believing they had upgraded (#472). Copy the * running binary to ~/.local/bin (unless we ARE that file), then sign it. */ - if (!same_binary && target_exists && !force) { + if (!skip_binary && !same_binary && target_exists && !force) { printf("A different binary already exists at:\n %s\n", bin_target); if (prompt_yn("Replace it with the binary you ran install from?")) { do_copy = true; @@ -12597,7 +12597,7 @@ int cbm_cmd_install(int argc, char **argv) { * and gets Killed:9 when spawned by an MCP host. Sign the private staged * candidate before disrupting sessions, then publish those exact verified * bytes inside the activation window. */ - bool sign_binary = do_copy || target_exists; + bool sign_binary = !skip_binary && (do_copy || target_exists); bool prepare_binary = sign_binary; #else bool prepare_binary = do_copy; diff --git a/tests/test_cli.c b/tests/test_cli.c index 2be1371ed0..9e962a2c1d 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -2132,6 +2132,46 @@ TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing) { PASS(); } +/* An existing target must not turn config-only install into binary activation. + * Exercise the real scope guard with a committed client, not the activation + * fake (whose reserve callback deliberately quiesces whenever called). */ +TEST(cli_install_skip_binary_existing_target_in_host_namespace_quiesces_nothing) { + cli_scope_fixture_t fixture; + bool ready = cli_scope_fixture_start(&fixture, "skip-existing"); + char binary_dir[640]; + char binary_path[704]; + char activation_log[704]; + snprintf(binary_dir, sizeof(binary_dir), "%s/existing/bin", fixture.host_home); + snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp", binary_dir); + snprintf(activation_log, sizeof(activation_log), "%s/logs/activation-events.ndjson", + fixture.host_cache); + const char *sentinel = "existing binary must survive config-only install\n"; + bool prepared = ready && test_mkdirp(binary_dir) == 0 && + write_test_file(binary_path, sentinel) == 0; + int install_rc = + prepared ? cli_scope_install(&fixture, fixture.host_home, fixture.host_cache, binary_dir, + true) + : -1; + /* This asks the daemon for !stopping and committed_clients == 1. */ + bool host_serving = prepared && cli_scope_host_serving(&fixture); + const char *binary = read_test_file(binary_path); + bool preserved = binary && strcmp(binary, sentinel) == 0; + const char *events = read_test_file(activation_log); + bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL; + bool nothing_drained = events && strstr(events, "cohort drained") == NULL; + int host_exit = cli_scope_fixture_finish(&fixture); + + ASSERT_TRUE(ready); + ASSERT_TRUE(prepared); + ASSERT_EQ(install_rc, 0); + ASSERT_TRUE(preserved); + ASSERT_TRUE(host_serving); + ASSERT_EQ(host_exit, 0); + ASSERT_TRUE(completed); + ASSERT_TRUE(nothing_drained); + PASS(); +} + /* 2026-10-09: ASSERT(ready) failed with nothing in the log, so which setup * step broke was unknowable. The fixture now names the step it stopped at. A * tag naming a missing directory makes its own mkdtemp fail deterministically; @@ -2343,6 +2383,179 @@ TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon) { } #endif +typedef struct { + cbm_path_info_t path; + uint64_t device; + uint64_t file; + unsigned int mode; +} cli_install_file_snapshot_t; + +static bool cli_install_file_snapshot(const char *path, cli_install_file_snapshot_t *snapshot) { + memset(snapshot, 0, sizeof(*snapshot)); + if (cbm_path_info_utf8(path, &snapshot->path) != CBM_PATH_INFO_OK || + !snapshot->path.is_regular) { + return false; + } +#ifdef _WIN32 + FILE *file = cbm_fopen(path, "rb"); + if (!file) { + return false; + } + BY_HANDLE_FILE_INFORMATION information; + bool captured = GetFileInformationByHandle((HANDLE)_get_osfhandle(_fileno(file)), + &information) != 0; + if (captured) { + snapshot->device = information.dwVolumeSerialNumber; + snapshot->file = ((uint64_t)information.nFileIndexHigh << 32) | + (uint64_t)information.nFileIndexLow; + snapshot->mode = information.dwFileAttributes; + } + return fclose(file) == 0 && captured; +#else + struct stat information; + if (stat(path, &information) != 0) { + return false; + } + snapshot->device = (uint64_t)information.st_dev; + snapshot->file = (uint64_t)information.st_ino; + snapshot->mode = (unsigned int)information.st_mode; + return true; +#endif +} + +static bool cli_install_file_unchanged(const char *path, + const cli_install_file_snapshot_t *before) { + cli_install_file_snapshot_t after; + return cli_install_file_snapshot(path, &after) && before->device == after.device && + before->file == after.file && before->mode == after.mode && + before->path.size == after.path.size && before->path.mtime_ns == after.path.mtime_ns; +} + +/* --skip-binary is a config-only operation even with an existing target, + * --yes, or --force. Keep the sentinel's bytes, native file identity, full + * mtime precision, POSIX mode / Windows attributes and process PATH while + * still updating Codex hooks. POSIX shell rc contents stay unchanged too. + * The missing source also proves that config-only install does not require a + * readable copy candidate; this is not an observation of transient stages. */ +TEST(cli_install_skip_binary_preserves_target_and_path_while_updating_config) { + char tmpdir[512]; + snprintf(tmpdir, sizeof(tmpdir), "%s/cli-skip-binary-XXXXXX", cbm_tmpdir()); + if (!cbm_mkdtemp(tmpdir)) { + FAIL("cbm_mkdtemp failed"); + } + cbm_normalize_path_sep(tmpdir); + const char *const env_names[] = {"HOME", "CBM_CACHE_DIR", "CODEX_HOME", "PATH", "SHELL"}; + char *saved_env[sizeof(env_names) / sizeof(env_names[0])]; + for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) { + saved_env[i] = save_test_env(env_names[i]); + cbm_unsetenv(env_names[i]); + } + + char cache_dir[640]; + char codex_dir[640]; + char config_path[768]; + char binary_dir[640]; + char binary_path[768]; + char missing_source[640]; + char shell_rc[640]; + snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir); + snprintf(codex_dir, sizeof(codex_dir), "%s/.codex", tmpdir); + snprintf(config_path, sizeof(config_path), "%s/config.toml", codex_dir); + snprintf(binary_dir, sizeof(binary_dir), "%s/.local/bin", tmpdir); +#ifdef _WIN32 + snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp.exe", binary_dir); + snprintf(missing_source, sizeof(missing_source), "%s/missing/codebase-memory-mcp.exe", tmpdir); +#else + snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp", binary_dir); + snprintf(missing_source, sizeof(missing_source), "%s/missing/codebase-memory-mcp", tmpdir); +#endif + snprintf(shell_rc, sizeof(shell_rc), "%s/.zshrc", tmpdir); + const char *sentinel = "installed binary must remain untouched\n"; + const char *shell_sentinel = "# user PATH configuration\n"; + bool setup_ok = test_mkdirp(codex_dir) == 0 && test_mkdirp(binary_dir) == 0 && + write_test_file(binary_path, sentinel) == 0 && + write_test_file(shell_rc, shell_sentinel) == 0; +#ifndef _WIN32 + setup_ok = setup_ok && chmod(binary_path, 0510) == 0; +#endif + cli_install_file_snapshot_t binary_before; + cli_install_file_snapshot_t shell_before; + setup_ok = setup_ok && cli_install_file_snapshot(binary_path, &binary_before) && + cli_install_file_snapshot(shell_rc, &shell_before); + cbm_setenv("HOME", tmpdir, 1); + cbm_setenv("CBM_CACHE_DIR", cache_dir, 1); + cbm_setenv("CODEX_HOME", codex_dir, 1); + cbm_setenv("PATH", tmpdir, 1); + cbm_setenv("SHELL", "/bin/zsh", 1); + cbm_cli_set_self_path_override_for_test(missing_source); + cli_activation_fake_t fake = {.mutation_reserve_result = 1}; + cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); + cbm_cli_set_activation_ops_for_test(&ops); + + char *skip_args[] = {"--skip-binary", "--clients=codex", "--hooks", "--yes"}; + char *force_args[] = {"--skip-binary", "--force", "--clients=codex", "--no-hooks", "--yes"}; + char *absent_args[] = {"--skip-binary", "--force", "--clients=codex", "--hooks", "--yes"}; + char **cases[] = {skip_args, force_args, absent_args}; + int case_argc[] = {4, 5, 5}; + bool cases_ok[3] = {false, false, false}; + for (size_t i = 0U; setup_ok && i < sizeof(cases) / sizeof(cases[0]); i++) { + if (i == 2U) { + if (cbm_unlink(binary_path) != 0) { + break; + } + /* Start the absent-target case with clean profiles; changing the + * executable embedded in earlier profiles tests migration policy, + * not whether --skip-binary leaves an absent target absent. */ + snprintf(codex_dir, sizeof(codex_dir), "%s/.codex-absent", tmpdir); + if (test_mkdirp(codex_dir) != 0) { + break; + } + cbm_setenv("CODEX_HOME", codex_dir, 1); + snprintf(config_path, sizeof(config_path), "%s/config.toml", codex_dir); + } + int rc = cli_test_cmd_install(case_argc[i], cases[i]); + char *config = read_test_file_alloc(config_path); + const char *config_binary = i == 2U ? missing_source : binary_path; + bool config_ok = config && strstr(config, "[mcp_servers.codebase-memory-mcp]") && + strstr(config, config_binary) && + (i == 1U ? !strstr(config, "hook-augment") + : strstr(config, "hook-augment") != NULL); + free(config); + char *binary = i == 2U ? NULL : read_test_file_alloc(binary_path); + cbm_path_info_t absent_status; + bool target_absent = + cbm_path_info_utf8(binary_path, &absent_status) == CBM_PATH_INFO_ABSENT; + bool binary_ok = i == 2U ? target_absent + : binary && strcmp(binary, sentinel) == 0 && + cli_install_file_unchanged(binary_path, &binary_before); + free(binary); + char *shell = read_test_file_alloc(shell_rc); + const char *current_path = getenv("PATH"); + bool path_ok = current_path && strcmp(current_path, tmpdir) == 0 && shell && + strcmp(shell, shell_sentinel) == 0 && + cli_install_file_unchanged(shell_rc, &shell_before); + cases_ok[i] = rc == 0 && config_ok && binary_ok && path_ok; + if (!cases_ok[i]) { + (void)fprintf(stderr, "skip-binary case %zu: rc=%d config=%d binary=%d path=%d\n", i, + rc, config_ok, binary_ok, path_ok); + } + free(shell); + } + + cbm_cli_set_activation_ops_for_test(NULL); + cbm_cli_set_self_path_override_for_test(NULL); + cbm_set_auto_answer_for_test(0); + for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) { + restore_test_env(env_names[i], saved_env[i]); + } + test_rmdir_r(tmpdir); + ASSERT_TRUE(setup_ok); + ASSERT_TRUE(cases_ok[0]); + ASSERT_TRUE(cases_ok[1]); + ASSERT_TRUE(cases_ok[2]); + PASS(); +} + TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { char tmpdir[256]; snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-daemon-install-XXXXXX"); @@ -2366,27 +2579,31 @@ TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { #else snprintf(bin_target, sizeof(bin_target), "%s/codebase-memory-mcp", bin_dir); #endif - write_test_file(bin_target, "old binary must survive"); - - cli_activation_fake_t fake = { - .participants_active = true, - .mutation_reserve_result = 1, - }; - cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); - cbm_cli_set_activation_ops_for_test(&ops); - char *argv[] = {"--force"}; - int rc = cli_test_cmd_install(1, argv); + char *replace_args[] = {"--yes"}; + char *force_args[] = {"--force", "--yes"}; + char **cases[] = {replace_args, force_args}; + int case_argc[] = {1, 2}; + bool cases_ok[2] = {false, false}; + for (size_t i = 0U; i < sizeof(cases) / sizeof(cases[0]); i++) { + write_test_file(bin_target, "old binary must survive"); + cli_activation_fake_t fake = { + .participants_active = true, + .mutation_reserve_result = 1, + }; + cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); + cbm_cli_set_activation_ops_for_test(&ops); + int rc = cli_test_cmd_install(case_argc[i], cases[i]); + const char *installed = read_test_file(bin_target); + cases_ok[i] = rc == 0 && installed && strcmp(installed, "old binary must survive") != 0 && + fake.mutation_reserve_count == 1 && fake.quiesce_count == 1 && + fake.mutation_lease_release_count == 1; + } cbm_cli_set_activation_ops_for_test(NULL); - - const char *installed = read_test_file(bin_target); - bool preserved = installed && strcmp(installed, "old binary must survive") == 0; + cbm_set_auto_answer_for_test(0); cli_activation_restore_env(old_home, old_cache); test_rmdir_r(tmpdir); - ASSERT_EQ(rc, 0); - ASSERT_FALSE(preserved); - ASSERT_EQ(fake.mutation_reserve_count, 1); - ASSERT_EQ(fake.quiesce_count, 1); - ASSERT_EQ(fake.mutation_lease_release_count, 1); + ASSERT_TRUE(cases_ok[0]); + ASSERT_TRUE(cases_ok[1]); PASS(); } @@ -21304,12 +21521,14 @@ SUITE(cli) { RUN_TEST(cli_install_binary_into_foreign_home_never_drains_host_cohort); RUN_TEST(cli_install_into_host_namespace_still_drains_host_cohort); RUN_TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing); + RUN_TEST(cli_install_skip_binary_existing_target_in_host_namespace_quiesces_nothing); RUN_TEST(cli_scope_fixture_names_its_failing_setup_step); RUN_TEST(cli_scope_fixture_finish_keeps_env_after_an_early_failure); RUN_TEST(cli_scope_retries_a_mute_endpoint_until_it_answers); RUN_TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon); RUN_TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon); #endif + RUN_TEST(cli_install_skip_binary_preserves_target_and_path_while_updating_config); RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary); RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely); RUN_TEST(cli_activation_commands_reject_malformed_and_unknown_flags); From 5ff2c90e0aa33365e53f207ac2aa6b52b588fe04 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:02:35 -0600 Subject: [PATCH 08/10] Revert "fix(cli): honor skip-binary for existing targets" This reverts commit af0205d0cc41811723132a57718e4214efd4a027. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- src/cli/cli.c | 4 +- tests/test_cli.c | 255 ++++------------------------------------------- 2 files changed, 20 insertions(+), 239 deletions(-) diff --git a/src/cli/cli.c b/src/cli/cli.c index a7acd024ee..0ad58306dc 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -12591,7 +12591,7 @@ int cbm_cmd_install(int argc, char **argv) { * `install --force` from a freshly built binary silently kept the OLD file * — operators ran stale code believing they had upgraded (#472). Copy the * running binary to ~/.local/bin (unless we ARE that file), then sign it. */ - if (!skip_binary && !same_binary && target_exists && !force) { + if (!same_binary && target_exists && !force) { printf("A different binary already exists at:\n %s\n", bin_target); if (prompt_yn("Replace it with the binary you ran install from?")) { do_copy = true; @@ -12605,7 +12605,7 @@ int cbm_cmd_install(int argc, char **argv) { * and gets Killed:9 when spawned by an MCP host. Sign the private staged * candidate before disrupting sessions, then publish those exact verified * bytes inside the activation window. */ - bool sign_binary = !skip_binary && (do_copy || target_exists); + bool sign_binary = do_copy || target_exists; bool prepare_binary = sign_binary; #else bool prepare_binary = do_copy; diff --git a/tests/test_cli.c b/tests/test_cli.c index ac5ed972f6..df0a289d21 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -2132,46 +2132,6 @@ TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing) { PASS(); } -/* An existing target must not turn config-only install into binary activation. - * Exercise the real scope guard with a committed client, not the activation - * fake (whose reserve callback deliberately quiesces whenever called). */ -TEST(cli_install_skip_binary_existing_target_in_host_namespace_quiesces_nothing) { - cli_scope_fixture_t fixture; - bool ready = cli_scope_fixture_start(&fixture, "skip-existing"); - char binary_dir[640]; - char binary_path[704]; - char activation_log[704]; - snprintf(binary_dir, sizeof(binary_dir), "%s/existing/bin", fixture.host_home); - snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp", binary_dir); - snprintf(activation_log, sizeof(activation_log), "%s/logs/activation-events.ndjson", - fixture.host_cache); - const char *sentinel = "existing binary must survive config-only install\n"; - bool prepared = ready && test_mkdirp(binary_dir) == 0 && - write_test_file(binary_path, sentinel) == 0; - int install_rc = - prepared ? cli_scope_install(&fixture, fixture.host_home, fixture.host_cache, binary_dir, - true) - : -1; - /* This asks the daemon for !stopping and committed_clients == 1. */ - bool host_serving = prepared && cli_scope_host_serving(&fixture); - const char *binary = read_test_file(binary_path); - bool preserved = binary && strcmp(binary, sentinel) == 0; - const char *events = read_test_file(activation_log); - bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL; - bool nothing_drained = events && strstr(events, "cohort drained") == NULL; - int host_exit = cli_scope_fixture_finish(&fixture); - - ASSERT_TRUE(ready); - ASSERT_TRUE(prepared); - ASSERT_EQ(install_rc, 0); - ASSERT_TRUE(preserved); - ASSERT_TRUE(host_serving); - ASSERT_EQ(host_exit, 0); - ASSERT_TRUE(completed); - ASSERT_TRUE(nothing_drained); - PASS(); -} - /* 2026-10-09: ASSERT(ready) failed with nothing in the log, so which setup * step broke was unknowable. The fixture now names the step it stopped at. A * tag naming a missing directory makes its own mkdtemp fail deterministically; @@ -2383,179 +2343,6 @@ TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon) { } #endif -typedef struct { - cbm_path_info_t path; - uint64_t device; - uint64_t file; - unsigned int mode; -} cli_install_file_snapshot_t; - -static bool cli_install_file_snapshot(const char *path, cli_install_file_snapshot_t *snapshot) { - memset(snapshot, 0, sizeof(*snapshot)); - if (cbm_path_info_utf8(path, &snapshot->path) != CBM_PATH_INFO_OK || - !snapshot->path.is_regular) { - return false; - } -#ifdef _WIN32 - FILE *file = cbm_fopen(path, "rb"); - if (!file) { - return false; - } - BY_HANDLE_FILE_INFORMATION information; - bool captured = GetFileInformationByHandle((HANDLE)_get_osfhandle(_fileno(file)), - &information) != 0; - if (captured) { - snapshot->device = information.dwVolumeSerialNumber; - snapshot->file = ((uint64_t)information.nFileIndexHigh << 32) | - (uint64_t)information.nFileIndexLow; - snapshot->mode = information.dwFileAttributes; - } - return fclose(file) == 0 && captured; -#else - struct stat information; - if (stat(path, &information) != 0) { - return false; - } - snapshot->device = (uint64_t)information.st_dev; - snapshot->file = (uint64_t)information.st_ino; - snapshot->mode = (unsigned int)information.st_mode; - return true; -#endif -} - -static bool cli_install_file_unchanged(const char *path, - const cli_install_file_snapshot_t *before) { - cli_install_file_snapshot_t after; - return cli_install_file_snapshot(path, &after) && before->device == after.device && - before->file == after.file && before->mode == after.mode && - before->path.size == after.path.size && before->path.mtime_ns == after.path.mtime_ns; -} - -/* --skip-binary is a config-only operation even with an existing target, - * --yes, or --force. Keep the sentinel's bytes, native file identity, full - * mtime precision, POSIX mode / Windows attributes and process PATH while - * still updating Codex hooks. POSIX shell rc contents stay unchanged too. - * The missing source also proves that config-only install does not require a - * readable copy candidate; this is not an observation of transient stages. */ -TEST(cli_install_skip_binary_preserves_target_and_path_while_updating_config) { - char tmpdir[512]; - snprintf(tmpdir, sizeof(tmpdir), "%s/cli-skip-binary-XXXXXX", cbm_tmpdir()); - if (!cbm_mkdtemp(tmpdir)) { - FAIL("cbm_mkdtemp failed"); - } - cbm_normalize_path_sep(tmpdir); - const char *const env_names[] = {"HOME", "CBM_CACHE_DIR", "CODEX_HOME", "PATH", "SHELL"}; - char *saved_env[sizeof(env_names) / sizeof(env_names[0])]; - for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) { - saved_env[i] = save_test_env(env_names[i]); - cbm_unsetenv(env_names[i]); - } - - char cache_dir[640]; - char codex_dir[640]; - char config_path[768]; - char binary_dir[640]; - char binary_path[768]; - char missing_source[640]; - char shell_rc[640]; - snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir); - snprintf(codex_dir, sizeof(codex_dir), "%s/.codex", tmpdir); - snprintf(config_path, sizeof(config_path), "%s/config.toml", codex_dir); - snprintf(binary_dir, sizeof(binary_dir), "%s/.local/bin", tmpdir); -#ifdef _WIN32 - snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp.exe", binary_dir); - snprintf(missing_source, sizeof(missing_source), "%s/missing/codebase-memory-mcp.exe", tmpdir); -#else - snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp", binary_dir); - snprintf(missing_source, sizeof(missing_source), "%s/missing/codebase-memory-mcp", tmpdir); -#endif - snprintf(shell_rc, sizeof(shell_rc), "%s/.zshrc", tmpdir); - const char *sentinel = "installed binary must remain untouched\n"; - const char *shell_sentinel = "# user PATH configuration\n"; - bool setup_ok = test_mkdirp(codex_dir) == 0 && test_mkdirp(binary_dir) == 0 && - write_test_file(binary_path, sentinel) == 0 && - write_test_file(shell_rc, shell_sentinel) == 0; -#ifndef _WIN32 - setup_ok = setup_ok && chmod(binary_path, 0510) == 0; -#endif - cli_install_file_snapshot_t binary_before; - cli_install_file_snapshot_t shell_before; - setup_ok = setup_ok && cli_install_file_snapshot(binary_path, &binary_before) && - cli_install_file_snapshot(shell_rc, &shell_before); - cbm_setenv("HOME", tmpdir, 1); - cbm_setenv("CBM_CACHE_DIR", cache_dir, 1); - cbm_setenv("CODEX_HOME", codex_dir, 1); - cbm_setenv("PATH", tmpdir, 1); - cbm_setenv("SHELL", "/bin/zsh", 1); - cbm_cli_set_self_path_override_for_test(missing_source); - cli_activation_fake_t fake = {.mutation_reserve_result = 1}; - cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); - cbm_cli_set_activation_ops_for_test(&ops); - - char *skip_args[] = {"--skip-binary", "--clients=codex", "--hooks", "--yes"}; - char *force_args[] = {"--skip-binary", "--force", "--clients=codex", "--no-hooks", "--yes"}; - char *absent_args[] = {"--skip-binary", "--force", "--clients=codex", "--hooks", "--yes"}; - char **cases[] = {skip_args, force_args, absent_args}; - int case_argc[] = {4, 5, 5}; - bool cases_ok[3] = {false, false, false}; - for (size_t i = 0U; setup_ok && i < sizeof(cases) / sizeof(cases[0]); i++) { - if (i == 2U) { - if (cbm_unlink(binary_path) != 0) { - break; - } - /* Start the absent-target case with clean profiles; changing the - * executable embedded in earlier profiles tests migration policy, - * not whether --skip-binary leaves an absent target absent. */ - snprintf(codex_dir, sizeof(codex_dir), "%s/.codex-absent", tmpdir); - if (test_mkdirp(codex_dir) != 0) { - break; - } - cbm_setenv("CODEX_HOME", codex_dir, 1); - snprintf(config_path, sizeof(config_path), "%s/config.toml", codex_dir); - } - int rc = cli_test_cmd_install(case_argc[i], cases[i]); - char *config = read_test_file_alloc(config_path); - const char *config_binary = i == 2U ? missing_source : binary_path; - bool config_ok = config && strstr(config, "[mcp_servers.codebase-memory-mcp]") && - strstr(config, config_binary) && - (i == 1U ? !strstr(config, "hook-augment") - : strstr(config, "hook-augment") != NULL); - free(config); - char *binary = i == 2U ? NULL : read_test_file_alloc(binary_path); - cbm_path_info_t absent_status; - bool target_absent = - cbm_path_info_utf8(binary_path, &absent_status) == CBM_PATH_INFO_ABSENT; - bool binary_ok = i == 2U ? target_absent - : binary && strcmp(binary, sentinel) == 0 && - cli_install_file_unchanged(binary_path, &binary_before); - free(binary); - char *shell = read_test_file_alloc(shell_rc); - const char *current_path = getenv("PATH"); - bool path_ok = current_path && strcmp(current_path, tmpdir) == 0 && shell && - strcmp(shell, shell_sentinel) == 0 && - cli_install_file_unchanged(shell_rc, &shell_before); - cases_ok[i] = rc == 0 && config_ok && binary_ok && path_ok; - if (!cases_ok[i]) { - (void)fprintf(stderr, "skip-binary case %zu: rc=%d config=%d binary=%d path=%d\n", i, - rc, config_ok, binary_ok, path_ok); - } - free(shell); - } - - cbm_cli_set_activation_ops_for_test(NULL); - cbm_cli_set_self_path_override_for_test(NULL); - cbm_set_auto_answer_for_test(0); - for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) { - restore_test_env(env_names[i], saved_env[i]); - } - test_rmdir_r(tmpdir); - ASSERT_TRUE(setup_ok); - ASSERT_TRUE(cases_ok[0]); - ASSERT_TRUE(cases_ok[1]); - ASSERT_TRUE(cases_ok[2]); - PASS(); -} - TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { char tmpdir[256]; snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-daemon-install-XXXXXX"); @@ -2579,31 +2366,27 @@ TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { #else snprintf(bin_target, sizeof(bin_target), "%s/codebase-memory-mcp", bin_dir); #endif - char *replace_args[] = {"--yes"}; - char *force_args[] = {"--force", "--yes"}; - char **cases[] = {replace_args, force_args}; - int case_argc[] = {1, 2}; - bool cases_ok[2] = {false, false}; - for (size_t i = 0U; i < sizeof(cases) / sizeof(cases[0]); i++) { - write_test_file(bin_target, "old binary must survive"); - cli_activation_fake_t fake = { - .participants_active = true, - .mutation_reserve_result = 1, - }; - cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); - cbm_cli_set_activation_ops_for_test(&ops); - int rc = cli_test_cmd_install(case_argc[i], cases[i]); - const char *installed = read_test_file(bin_target); - cases_ok[i] = rc == 0 && installed && strcmp(installed, "old binary must survive") != 0 && - fake.mutation_reserve_count == 1 && fake.quiesce_count == 1 && - fake.mutation_lease_release_count == 1; - } + write_test_file(bin_target, "old binary must survive"); + + cli_activation_fake_t fake = { + .participants_active = true, + .mutation_reserve_result = 1, + }; + cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake); + cbm_cli_set_activation_ops_for_test(&ops); + char *argv[] = {"--force"}; + int rc = cli_test_cmd_install(1, argv); cbm_cli_set_activation_ops_for_test(NULL); - cbm_set_auto_answer_for_test(0); + + const char *installed = read_test_file(bin_target); + bool preserved = installed && strcmp(installed, "old binary must survive") == 0; cli_activation_restore_env(old_home, old_cache); test_rmdir_r(tmpdir); - ASSERT_TRUE(cases_ok[0]); - ASSERT_TRUE(cases_ok[1]); + ASSERT_EQ(rc, 0); + ASSERT_FALSE(preserved); + ASSERT_EQ(fake.mutation_reserve_count, 1); + ASSERT_EQ(fake.quiesce_count, 1); + ASSERT_EQ(fake.mutation_lease_release_count, 1); PASS(); } @@ -21551,14 +21334,12 @@ SUITE(cli) { RUN_TEST(cli_install_binary_into_foreign_home_never_drains_host_cohort); RUN_TEST(cli_install_into_host_namespace_still_drains_host_cohort); RUN_TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing); - RUN_TEST(cli_install_skip_binary_existing_target_in_host_namespace_quiesces_nothing); RUN_TEST(cli_scope_fixture_names_its_failing_setup_step); RUN_TEST(cli_scope_fixture_finish_keeps_env_after_an_early_failure); RUN_TEST(cli_scope_retries_a_mute_endpoint_until_it_answers); RUN_TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon); RUN_TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon); #endif - RUN_TEST(cli_install_skip_binary_preserves_target_and_path_while_updating_config); RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary); RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely); RUN_TEST(cli_activation_commands_reject_malformed_and_unknown_flags); From c6094e22e103c970ce832fa9583d6add1b0ce628 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:03:07 -0600 Subject: [PATCH 09/10] Revert "test(subprocess): measure RSS growth within one process tree" This reverts commit ce654019b0c4fa04a271936361e6487867461ae4. Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- tests/test_main.c | 71 +++++++++++++---------------------------- tests/test_subprocess.c | 66 ++++++++++++-------------------------- 2 files changed, 44 insertions(+), 93 deletions(-) diff --git a/tests/test_main.c b/tests/test_main.c index 464781aa7b..4b3716c50c 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -1976,48 +1976,17 @@ static int tf_role_env_isolation(int argc, char **argv) { } /* Process-tree memory probe coverage (test_subprocess.c). The root starts ONE - * descendant that holds a controlled allocation. With a load path, it first - * publishes an idle marker and waits for the parent to finish its baseline - * sample. It then touches every page and publishes readiness. The root exits - * when the descendant dies, including on setup failure. */ -static int tf_rss_tree_publish_ready(const char *ready_path) { - char staged[1024]; - int length = snprintf(staged, sizeof(staged), "%s.tmp", ready_path); - FILE *ready = length > 0 && (size_t)length < sizeof(staged) ? cbm_fopen(staged, "wb") : NULL; - if (!ready) { - return 83; - } -#ifdef _WIN32 - (void)fprintf(ready, "%lu\n", (unsigned long)GetCurrentProcessId()); -#else - (void)fprintf(ready, "%ld\n", (long)getpid()); -#endif - if (fclose(ready) != 0 || rename(staged, ready_path) != 0) { - return 84; - } - return 0; -} - -static int tf_rss_tree_hold(const char *ready_path, const char *mib_text, const char *load_path) { + * descendant that charges a controlled amount of memory, so a tree total can + * be checked against an allocation only a descendant holds. The descendant + * publishes its pid by an atomic rename once every page is touched, then + * blocks until it is killed. The root exits when its descendant dies, so a + * failed setup ends the tree instead of leaving the test waiting. */ +static int tf_rss_tree_hold(const char *ready_path, const char *mib_text) { char *end = NULL; long mib = strtol(mib_text, &end, 10); if (!end || *end != '\0' || mib < 0 || mib > 1024) { return 81; } - if (load_path) { - char idle_path[1024]; - int length = snprintf(idle_path, sizeof(idle_path), "%s.idle", ready_path); - if (length <= 0 || (size_t)length >= sizeof(idle_path)) { - return 83; - } - int idle_rc = tf_rss_tree_publish_ready(idle_path); - if (idle_rc != 0) { - return idle_rc; - } - while (!cbm_file_exists(load_path)) { - cbm_usleep(10000); - } - } size_t bytes = (size_t)mib * 1024U * 1024U; unsigned char *block = bytes > 0 ? malloc(bytes) : NULL; if (bytes > 0 && !block) { @@ -2026,9 +1995,19 @@ static int tf_rss_tree_hold(const char *ready_path, const char *mib_text, const if (block) { memset(block, 0xA5, bytes); } - int ready_rc = tf_rss_tree_publish_ready(ready_path); - if (ready_rc != 0) { - return ready_rc; + char staged[1024]; + int length = snprintf(staged, sizeof(staged), "%s.tmp", ready_path); + FILE *ready = length > 0 && (size_t)length < sizeof(staged) ? cbm_fopen(staged, "wb") : NULL; + if (!ready) { + return 83; + } +#ifdef _WIN32 + (void)fprintf(ready, "%lu\n", (unsigned long)GetCurrentProcessId()); +#else + (void)fprintf(ready, "%ld\n", (long)getpid()); +#endif + if (fclose(ready) != 0 || rename(staged, ready_path) != 0) { + return 84; } volatile unsigned char keep = block ? block[bytes - 1] : 0; (void)keep; @@ -2042,20 +2021,16 @@ static int tf_rss_tree_hold(const char *ready_path, const char *mib_text, const } static int tf_role_rss_tree_hold(int argc, char **argv) { - if (argc != 4 && argc != 5) { - return -1; - } - return tf_rss_tree_hold(argv[2], argv[3], argc == 5 ? argv[4] : NULL); + return argc == 4 ? tf_rss_tree_hold(argv[2], argv[3]) : -1; } static int tf_role_rss_tree_root(int argc, char **argv) { - if (argc != 4 && argc != 5) { + if (argc != 4) { return -1; } #ifdef _WIN32 char *self = cbm_module_path_utf8(); - const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], - argc == 5 ? argv[4] : NULL, NULL}; + const char *child_argv[] = {self, "__cbm_rss_tree_hold", argv[2], argv[3], NULL}; char command_line[8192]; bool built = self && cbm_build_win_cmdline(command_line, sizeof(command_line), child_argv); wchar_t *wide_self = built ? cbm_utf8_to_wide(self) : NULL; @@ -2085,7 +2060,7 @@ static int tf_role_rss_tree_root(int argc, char **argv) { return 85; } if (child == 0) { - _exit(tf_rss_tree_hold(argv[2], argv[3], argc == 5 ? argv[4] : NULL)); + _exit(tf_rss_tree_hold(argv[2], argv[3])); } int status = 0; while (waitpid(child, &status, 0) < 0 && errno == EINTR) { diff --git a/tests/test_subprocess.c b/tests/test_subprocess.c index 97f2a07f1d..e214a2c36f 100644 --- a/tests/test_subprocess.c +++ b/tests/test_subprocess.c @@ -828,12 +828,7 @@ TEST(subprocess_spawn_returns_while_child_is_running) { typedef struct { bool spawned; - bool idle_ready; - long idle_pid; - cbm_proc_tree_rss_status_t idle_status; - uint64_t idle_rss; bool ready; - long pid; cbm_proc_tree_rss_status_t status; uint64_t rss; bool quiesced; @@ -845,8 +840,7 @@ static void rss_tree_pause(void) { cbm_usleep(10000); /* poll interval only; nothing here is a deadline */ } -static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process, - long *pid_out) { +static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *process) { for (;;) { FILE *ready = cbm_fopen(ready_path, "rb"); if (ready) { @@ -854,7 +848,6 @@ static bool rss_tree_wait_ready(const char *ready_path, cbm_subprocess_t *proces int fields = fscanf(ready, "%ld", &pid); (void)fclose(ready); if (fields == 1 && pid > 0) { - *pid_out = pid; return true; } } @@ -887,26 +880,20 @@ static bool rss_tree_finish(cbm_subprocess_t *process, rss_tree_sample_t *sample /* Measure one tree whose descendant holds `mib` MiB, with every non-root * member's measurement failing with `member_fault` (0 = real calls). */ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { - rss_tree_sample_t sample = {.idle_status = CBM_PROC_TREE_RSS_ERROR, - .status = CBM_PROC_TREE_RSS_ERROR, + rss_tree_sample_t sample = {.status = CBM_PROC_TREE_RSS_ERROR, .final_status = CBM_PROC_TREE_RSS_ERROR, .final_rss = UINT64_MAX}; const char *made = th_mktempdir("cbm_rss_tree"); char dir[256]; char ready_path[300]; - char idle_path[320]; - char load_path[300]; char mib_text[16]; - bool phased = mib > 0 && member_fault == 0; if (!made || snprintf(dir, sizeof(dir), "%s", made) >= (int)sizeof(dir) || - snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path) || - snprintf(idle_path, sizeof(idle_path), "%s.idle", ready_path) >= (int)sizeof(idle_path) || - snprintf(load_path, sizeof(load_path), "%s/load", dir) >= (int)sizeof(load_path)) { + snprintf(ready_path, sizeof(ready_path), "%s/ready", dir) >= (int)sizeof(ready_path)) { return sample; } (void)snprintf(mib_text, sizeof(mib_text), "%d", mib); const char *argv[] = {cbm_test_program_path, "__cbm_rss_tree_root", ready_path, mib_text, - phased ? load_path : NULL, NULL}; + NULL}; cbm_proc_opts_t opts = {0}; opts.bin = cbm_test_program_path; opts.argv = argv; @@ -914,19 +901,7 @@ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { cbm_subprocess_t *process = NULL; sample.spawned = cbm_subprocess_spawn(&opts, &process) == 0; if (sample.spawned) { - bool load_requested = !phased; - if (phased) { - sample.idle_ready = rss_tree_wait_ready(idle_path, process, &sample.idle_pid); - if (sample.idle_ready) { - sample.idle_status = cbm_subprocess_tree_rss_bytes(process, &sample.idle_rss); - /* Release only after a successful idle measurement. */ - if (sample.idle_status == CBM_PROC_TREE_RSS_OK && sample.idle_rss > 0) { - FILE *load = cbm_fopen(load_path, "wb"); - load_requested = load && fclose(load) == 0; - } - } - } - sample.ready = load_requested && rss_tree_wait_ready(ready_path, process, &sample.pid); + sample.ready = rss_tree_wait_ready(ready_path, process); if (sample.ready) { cbm_subprocess_tree_rss_fault_members_for_testing(member_fault); sample.status = cbm_subprocess_tree_rss_bytes(process, &sample.rss); @@ -938,26 +913,27 @@ static rss_tree_sample_t rss_tree_sample(int mib, int member_fault) { return sample; } -/* Only the descendant allocates, after the parent samples the idle tree and - * releases it through the load marker. Measure the SAME processes before and - * after the 128 MiB allocation: independently spawned trees need not have the - * same executable/sanitizer residency, notably on Windows. Keep the 96 MiB - * floor so a root-only probe or an untouched allocation cannot pass. */ +/* Only the DESCENDANT allocates, so the difference between two otherwise + * identical trees is memory the probe can only have counted by measuring past + * the root. 128 MiB touched against a 96 MiB floor leaves room for allocator + * and page-rounding noise and none for a root-only total. */ TEST(subprocess_tree_rss_measures_contained_descendants) { + rss_tree_sample_t idle = rss_tree_sample(0, 0); rss_tree_sample_t loaded = rss_tree_sample(128, 0); - if (loaded.status == CBM_PROC_TREE_RSS_OK && loaded.idle_status == CBM_PROC_TREE_RSS_OK) { - printf(" tree rss: idle(pid=%ld)=%llu MiB loaded(pid=%ld)=%llu MiB\n", - loaded.idle_pid, (unsigned long long)(loaded.idle_rss / RSS_TREE_MIB), - loaded.pid, (unsigned long long)(loaded.rss / RSS_TREE_MIB)); - } + if (loaded.status == CBM_PROC_TREE_RSS_OK && idle.status == CBM_PROC_TREE_RSS_OK) { + printf(" tree rss: idle=%llu MiB loaded=%llu MiB\n", + (unsigned long long)(idle.rss / RSS_TREE_MIB), + (unsigned long long)(loaded.rss / RSS_TREE_MIB)); + } + ASSERT_TRUE(idle.spawned); + ASSERT_TRUE(idle.ready); + ASSERT_EQ(idle.status, CBM_PROC_TREE_RSS_OK); + ASSERT_TRUE(idle.rss > 0); ASSERT_TRUE(loaded.spawned); - ASSERT_TRUE(loaded.idle_ready); - ASSERT_EQ(loaded.idle_status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(loaded.idle_rss > 0); ASSERT_TRUE(loaded.ready); - ASSERT_EQ(loaded.pid, loaded.idle_pid); ASSERT_EQ(loaded.status, CBM_PROC_TREE_RSS_OK); - ASSERT_TRUE(loaded.rss >= loaded.idle_rss + 96 * RSS_TREE_MIB); + ASSERT_TRUE(loaded.rss >= idle.rss + 96 * RSS_TREE_MIB); + ASSERT_TRUE(idle.quiesced); ASSERT_TRUE(loaded.quiesced); ASSERT_EQ(loaded.final_status, CBM_PROC_TREE_RSS_EMPTY); ASSERT_EQ(loaded.final_rss, 0); From 131a40f036c3fbebb672ebb4a693d59d7a045909 Mon Sep 17 00:00:00 2001 From: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:03:29 -0600 Subject: [PATCH 10/10] test(windows): remove unused lifecycle import Signed-off-by: BowenX <318502935+Bowen-Xue-ac2@users.noreply.github.com> --- tests/windows/test_daemon_lifecycle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/windows/test_daemon_lifecycle.py b/tests/windows/test_daemon_lifecycle.py index 4cacb3c972..bba609b234 100644 --- a/tests/windows/test_daemon_lifecycle.py +++ b/tests/windows/test_daemon_lifecycle.py @@ -25,7 +25,6 @@ import subprocess import sys import tempfile -import time def runtime_for(cache):