|
18 | 18 | #include <cli/cli.h> |
19 | 19 | #include <cli/progress_sink.h> |
20 | 20 | #include <daemon/bootstrap.h> |
| 21 | +#include <daemon/ipc.h> |
21 | 22 | #include <daemon/runtime.h> |
22 | 23 | #include <daemon/version_cohort.h> |
23 | 24 | #include <foundation/constants.h> |
24 | 25 | #include <foundation/log.h> |
25 | 26 | #include <foundation/platform.h> |
| 27 | +#include <foundation/private_file_lock.h> |
26 | 28 | #include <foundation/sha256.h> |
27 | 29 | #include <mcp/mcp.h> |
28 | 30 | #include <mcp/index_supervisor.h> |
@@ -2037,6 +2039,139 @@ TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing) { |
2037 | 2039 | ASSERT_TRUE(nothing_drained); |
2038 | 2040 | PASS(); |
2039 | 2041 | } |
| 2042 | + |
| 2043 | +/* cli_scope_install with the activation's stdout captured into out. */ |
| 2044 | +static int cli_scope_install_captured(cli_scope_fixture_t *fixture, const char *home, |
| 2045 | + const char *cache, const char *bin_dir, bool skip_binary, |
| 2046 | + char *out, size_t out_size) { |
| 2047 | + out[0] = '\0'; |
| 2048 | + FILE *capture = tmpfile(); |
| 2049 | + int saved_stdout = capture ? dup(STDOUT_FILENO) : -1; |
| 2050 | + bool redirected = false; |
| 2051 | + if (capture && saved_stdout >= 0) { |
| 2052 | + fflush(stdout); |
| 2053 | + redirected = dup2(fileno(capture), STDOUT_FILENO) >= 0; |
| 2054 | + } |
| 2055 | + int rc = redirected ? cli_scope_install(fixture, home, cache, bin_dir, skip_binary) : -1; |
| 2056 | + if (redirected) { |
| 2057 | + fflush(stdout); |
| 2058 | + (void)dup2(saved_stdout, STDOUT_FILENO); |
| 2059 | + } |
| 2060 | + if (saved_stdout >= 0) { |
| 2061 | + close(saved_stdout); |
| 2062 | + } |
| 2063 | + if (capture) { |
| 2064 | + rewind(capture); |
| 2065 | + size_t count = fread(out, 1, out_size - 1U, capture); |
| 2066 | + out[count] = '\0'; |
| 2067 | + fclose(capture); |
| 2068 | + } |
| 2069 | + return rc; |
| 2070 | +} |
| 2071 | + |
| 2072 | +/* The notice an activation prints when it leaves a daemon it could not |
| 2073 | + * confirm as its own: names the daemon by pid and tells the user how to stop |
| 2074 | + * it themselves. Never the "Stopping ..." banner of a drain. */ |
| 2075 | +static bool cli_scope_kept_notice_names_host(const char *output, pid_t host) { |
| 2076 | + char pid_text[48]; |
| 2077 | + snprintf(pid_text, sizeof(pid_text), "(pid %ld, version %s)", (long)host, CBM_VERSION); |
| 2078 | + return strstr(output, "Leaving the running CBM daemon untouched") != NULL && |
| 2079 | + strstr(output, pid_text) != NULL && |
| 2080 | + strstr(output, "codebase-memory-mcp daemon stop") != NULL && |
| 2081 | + strstr(output, "Stopping active CBM sessions") == NULL; |
| 2082 | +} |
| 2083 | + |
| 2084 | +/* (e) USER DECISION 2026-09-28, "unknown = foreign, keep it": the scope read |
| 2085 | + * reaches the active cohort but cannot read its cache identity. Ownership is |
| 2086 | + * unconfirmed, so the sandbox install must leave the host daemon serving, |
| 2087 | + * stop no session, and say which daemon it left and how to stop it. The |
| 2088 | + * install publishes a binary (no --skip-binary): an activation that replaces |
| 2089 | + * nothing never consults the cohort at all (test d). */ |
| 2090 | +TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon) { |
| 2091 | + cli_scope_fixture_t fixture; |
| 2092 | + bool ready = cli_scope_fixture_start(&fixture, "unread"); |
| 2093 | + char foreign_home[512]; |
| 2094 | + char foreign_cache[576]; |
| 2095 | + char foreign_bin[640]; |
| 2096 | + char activation_log[704]; |
| 2097 | + cli_scope_foreign_paths(&fixture, foreign_home, foreign_cache, foreign_bin, activation_log); |
| 2098 | + bool prepared = ready && cbm_mkdir_p(foreign_home, 0700); |
| 2099 | + char output[16384]; |
| 2100 | + output[0] = '\0'; |
| 2101 | + cbm_cli_set_activation_scope_cache_unreadable_for_test(true); |
| 2102 | + int install_rc = prepared |
| 2103 | + ? cli_scope_install_captured(&fixture, foreign_home, foreign_cache, |
| 2104 | + foreign_bin, false, output, sizeof(output)) |
| 2105 | + : -1; |
| 2106 | + cbm_cli_set_activation_scope_cache_unreadable_for_test(false); |
| 2107 | + bool host_serving = prepared && cli_scope_host_serving(&fixture); |
| 2108 | + bool notice = cli_scope_kept_notice_names_host(output, fixture.host); |
| 2109 | + const char *events = read_test_file(activation_log); |
| 2110 | + bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL; |
| 2111 | + bool nothing_drained = events && strstr(events, "cohort drained") == NULL; |
| 2112 | + int host_exit = cli_scope_fixture_finish(&fixture); |
| 2113 | + |
| 2114 | + ASSERT_TRUE(ready); |
| 2115 | + ASSERT_TRUE(host_serving); |
| 2116 | + ASSERT_EQ(host_exit, 0); |
| 2117 | + ASSERT_TRUE(nothing_drained); |
| 2118 | + ASSERT_TRUE(notice); |
| 2119 | + ASSERT_EQ(install_rc, 0); |
| 2120 | + ASSERT_TRUE(completed); |
| 2121 | + PASS(); |
| 2122 | +} |
| 2123 | + |
| 2124 | +/* (f) Same decision, busy group: another activation holds the cohort |
| 2125 | + * maintenance gate, so the scope read cannot reach the active identity at |
| 2126 | + * all. Unconfirmed ownership keeps the host daemon; the sandbox install |
| 2127 | + * completes without draining anyone. The gate is held through the product's |
| 2128 | + * own lock-directory handle on the name version_cohort.c uses. */ |
| 2129 | +TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon) { |
| 2130 | + cli_scope_fixture_t fixture; |
| 2131 | + bool ready = cli_scope_fixture_start(&fixture, "busy"); |
| 2132 | + char foreign_home[512]; |
| 2133 | + char foreign_cache[576]; |
| 2134 | + char foreign_bin[640]; |
| 2135 | + char activation_log[704]; |
| 2136 | + cli_scope_foreign_paths(&fixture, foreign_home, foreign_cache, foreign_bin, activation_log); |
| 2137 | + cbm_private_lock_directory_t *lock_directory = NULL; |
| 2138 | + cbm_private_file_lock_t *maintenance = NULL; |
| 2139 | + bool held = ready && |
| 2140 | + cbm_daemon_ipc_private_lock_directory_new(fixture.endpoint, &lock_directory) == |
| 2141 | + CBM_PRIVATE_FILE_LOCK_OK && |
| 2142 | + cbm_private_file_lock_try_acquire( |
| 2143 | + lock_directory, "cbm-version-cohort-maintenance-v1.lock", |
| 2144 | + CBM_PRIVATE_FILE_LOCK_EX, &maintenance) == CBM_PRIVATE_FILE_LOCK_OK; |
| 2145 | + bool prepared = held && cbm_mkdir_p(foreign_home, 0700); |
| 2146 | + char output[16384]; |
| 2147 | + output[0] = '\0'; |
| 2148 | + int install_rc = prepared |
| 2149 | + ? cli_scope_install_captured(&fixture, foreign_home, foreign_cache, |
| 2150 | + foreign_bin, false, output, sizeof(output)) |
| 2151 | + : -1; |
| 2152 | + bool host_serving = prepared && cli_scope_host_serving(&fixture); |
| 2153 | + bool released = |
| 2154 | + maintenance && cbm_private_file_lock_release(&maintenance) == CBM_PRIVATE_FILE_LOCK_OK; |
| 2155 | + if (lock_directory) { |
| 2156 | + cbm_private_lock_directory_close(lock_directory); |
| 2157 | + } |
| 2158 | + bool notice = cli_scope_kept_notice_names_host(output, fixture.host); |
| 2159 | + const char *events = read_test_file(activation_log); |
| 2160 | + bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL; |
| 2161 | + bool nothing_drained = events && strstr(events, "cohort drained") == NULL; |
| 2162 | + int host_exit = cli_scope_fixture_finish(&fixture); |
| 2163 | + |
| 2164 | + ASSERT_TRUE(ready); |
| 2165 | + ASSERT_TRUE(held); |
| 2166 | + ASSERT_TRUE(host_serving); |
| 2167 | + ASSERT_EQ(host_exit, 0); |
| 2168 | + ASSERT_TRUE(nothing_drained); |
| 2169 | + ASSERT_TRUE(notice); |
| 2170 | + ASSERT_EQ(install_rc, 0); |
| 2171 | + ASSERT_TRUE(completed); |
| 2172 | + ASSERT_TRUE(released); |
| 2173 | + PASS(); |
| 2174 | +} |
2040 | 2175 | #endif |
2041 | 2176 |
|
2042 | 2177 | TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { |
@@ -16504,6 +16639,8 @@ SUITE(cli) { |
16504 | 16639 | RUN_TEST(cli_install_binary_into_foreign_home_never_drains_host_cohort); |
16505 | 16640 | RUN_TEST(cli_install_into_host_namespace_still_drains_host_cohort); |
16506 | 16641 | RUN_TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing); |
| 16642 | + RUN_TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon); |
| 16643 | + RUN_TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon); |
16507 | 16644 | #endif |
16508 | 16645 | RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary); |
16509 | 16646 | RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely); |
|
0 commit comments