@@ -18,6 +18,7 @@ int tf_skip_count = 0;
1818#include "foundation/mem.h" /* cbm_mem_init — worker budget */
1919#include "foundation/log.h" /* worker liveness heartbeat probe */
2020#include "foundation/platform.h" /* cbm_file_exists — blocking-git marker */
21+ #include "daemon/bootstrap.h" /* runner rendezvous isolation */
2122#include "daemon/runtime.h" /* bounded worker response probe */
2223#include "daemon/ipc.h" /* Windows private-lock re-exec probe */
2324#include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */
@@ -187,11 +188,15 @@ static int tf_maybe_run_blocking_git_probe(int argc, char **argv) {
187188 * this sentinel and restore it; atexit removes anything left behind by an
188189 * assertion that returns before fixture cleanup. */
189190static char tf_home_sentinel [512 ];
191+ static char tf_runtime_sentinel [512 ];
190192
191193static void tf_cleanup_cache_sentinel (void ) {
192194 if (tf_home_sentinel [0 ]) {
193195 th_rmtree (tf_home_sentinel );
194196 }
197+ if (tf_runtime_sentinel [0 ]) {
198+ th_rmtree (tf_runtime_sentinel );
199+ }
195200}
196201
197202/* Client home overrides the CLI honours BEFORE $HOME, so redirecting HOME alone
@@ -243,9 +248,108 @@ static bool tf_setup_cache_sentinel(void) {
243248 cbm_unsetenv (tf_client_home_overrides [i ]);
244249 }
245250 atexit (tf_cleanup_cache_sentinel );
251+ /* HOME and CBM_CACHE_DIR do not move the daemon rendezvous: its key is a
252+ * constant product hash under the system temp root. Without this, an
253+ * install/uninstall/update test or any endpoint resolved with no explicit
254+ * parent reaches the developer's live daemon. The run always gets a fresh
255+ * private directory; an inherited value is replaced, never reused (the
256+ * rule scripts/test-runtime.sh applies to the shell harness).
257+ * th_secure_runtime_parent_new anchors it where the IPC ancestry check
258+ * accepts it (LocalAppData on Windows). */
259+ if (!th_secure_runtime_parent_new (tf_runtime_sentinel , sizeof (tf_runtime_sentinel ), "run" )) {
260+ tf_runtime_sentinel [0 ] = '\0' ;
261+ return false;
262+ }
263+ if (cbm_setenv ("CBM_RUNTIME_DIR" , tf_runtime_sentinel , 1 ) != 0 ) {
264+ return false;
265+ }
266+ #ifdef CBM_ENABLE_TEST_SEAMS
267+ /* Belt and braces: a fixture that later drops the variable gets a loud
268+ * refusal instead of the default rendezvous. */
269+ cbm_daemon_bootstrap_forbid_default_runtime_for_test (true);
270+ #endif
246271 return true;
247272}
248273
274+ static bool tf_path_has_parent (const char * path , const char * parent ) {
275+ size_t length = parent ? strlen (parent ) : 0 ;
276+ return path && length > 0 && strncmp (path , parent , length ) == 0 &&
277+ (path [length ] == '/' || path [length ] == '\\' );
278+ }
279+
280+ /* The daemon rendezvous is not derived from HOME or the cache: its key is a
281+ * constant product hash and its default parent is the system temp root. With
282+ * CBM_RUNTIME_DIR unset, every endpoint a test resolves is therefore the
283+ * developer's live daemon socket (<tmp>/cbm-daemon-<uid>/cbm-<key>.sock).
284+ * Every check runs before anything is resolved, so a run without isolation
285+ * fails here and this test never reaches that socket itself. */
286+ TEST (runner_isolation_daemon_runtime_dir_is_private ) {
287+ char runtime_env [sizeof (tf_runtime_sentinel )] = {0 };
288+ const char * value = cbm_safe_getenv ("CBM_RUNTIME_DIR" , runtime_env , sizeof (runtime_env ), NULL );
289+ ASSERT_NOT_NULL (value );
290+ ASSERT_TRUE (value [0 ] != '\0' );
291+ /* The fresh per-run directory, never a value inherited from the caller. */
292+ ASSERT_STR_EQ (value , tf_runtime_sentinel );
293+
294+ const char * root = th_secure_runtime_base ();
295+ ASSERT_NOT_NULL (root );
296+ char canonical_root [CBM_SZ_4K ] = {0 };
297+ char canonical_run [CBM_SZ_4K ] = {0 };
298+ ASSERT_TRUE (cbm_canonical_path (root , canonical_root , sizeof (canonical_root )) != 0 );
299+ ASSERT_TRUE (cbm_canonical_path (value , canonical_run , sizeof (canonical_run )) != 0 );
300+ /* Strictly below the temp root: the root itself is the default rendezvous
301+ * parent (/tmp on POSIX, LocalAppData on Windows). */
302+ ASSERT_TRUE (tf_path_has_parent (canonical_run , canonical_root ));
303+
304+ /* Only now resolve through the product resolver. A NULL parent is what
305+ * every product call site passes: daemon, MCP client, CLI, activation. */
306+ cbm_daemon_ipc_endpoint_t * endpoint = cbm_daemon_bootstrap_endpoint_new (NULL );
307+ const char * resolved = endpoint ? cbm_daemon_ipc_endpoint_runtime_dir (endpoint ) : NULL ;
308+ char canonical_resolved [CBM_SZ_4K ] = {0 };
309+ bool resolved_ok = resolved && cbm_canonical_path (resolved , canonical_resolved ,
310+ sizeof (canonical_resolved )) != 0 ;
311+ cbm_daemon_ipc_endpoint_free (endpoint );
312+ ASSERT_TRUE (resolved_ok );
313+ ASSERT_TRUE (tf_path_has_parent (canonical_resolved , canonical_run ));
314+ #ifndef _WIN32
315+ char live_default [CBM_SZ_4K ];
316+ (void )snprintf (live_default , sizeof (live_default ), "%s/cbm-daemon-%lu" , canonical_root ,
317+ (unsigned long )geteuid ());
318+ ASSERT_TRUE (strcmp (canonical_resolved , live_default ) != 0 );
319+ #endif
320+ PASS ();
321+ }
322+
323+ #ifdef CBM_ENABLE_TEST_SEAMS
324+ /* The runner arms the bootstrap guard, so a fixture that drops the variable
325+ * gets a refusal instead of the default rendezvous. Only the decision is
326+ * exercised: no endpoint is ever built while the variable is absent. */
327+ TEST (runner_isolation_refuses_default_daemon_runtime ) {
328+ ASSERT_TRUE (tf_runtime_sentinel [0 ] != '\0' );
329+ bool allowed_with_run_dir = !cbm_daemon_bootstrap_default_runtime_refused_for_test (NULL );
330+
331+ (void )cbm_unsetenv ("CBM_RUNTIME_DIR" );
332+ bool refused_without = cbm_daemon_bootstrap_default_runtime_refused_for_test (NULL );
333+ bool explicit_allowed =
334+ !cbm_daemon_bootstrap_default_runtime_refused_for_test (tf_runtime_sentinel );
335+ /* Restore before asserting: a failed assertion returns immediately. */
336+ bool restored = cbm_setenv ("CBM_RUNTIME_DIR" , tf_runtime_sentinel , 1 ) == 0 ;
337+
338+ ASSERT_TRUE (restored );
339+ ASSERT_TRUE (allowed_with_run_dir );
340+ ASSERT_TRUE (refused_without );
341+ ASSERT_TRUE (explicit_allowed );
342+ PASS ();
343+ }
344+ #endif
345+
346+ SUITE (runner_isolation ) {
347+ RUN_TEST (runner_isolation_daemon_runtime_dir_is_private );
348+ #ifdef CBM_ENABLE_TEST_SEAMS
349+ RUN_TEST (runner_isolation_refuses_default_daemon_runtime );
350+ #endif
351+ }
352+
249353/* Fast real-process probes for the async index-supervisor contract. They run
250354 * only in a child admitted by the exact build-bound worker grammar. */
251355static void tf_index_worker_probe (const char * args_json , const char * response_out ) {
@@ -1056,7 +1160,7 @@ int main(int argc, char **argv) {
10561160 * A test that exercises the supervisor must explicitly re-enable it. */
10571161 cbm_setenv ("CBM_INDEX_SUPERVISOR" , "0" , 1 );
10581162 if (!tf_setup_cache_sentinel ()) {
1059- fprintf (stderr , "failed to create isolated test cache\n" );
1163+ fprintf (stderr , "failed to create isolated test cache and daemon runtime \n" );
10601164 return 2 ;
10611165 }
10621166
@@ -1080,6 +1184,9 @@ int main(int argc, char **argv) {
10801184 printf ("\n codebase-memory-mcp C test suite\n" );
10811185 }
10821186
1187+ /* Runner isolation first: nothing below may reach the developer's daemon. */
1188+ RUN_SELECTED_SUITE (runner_isolation );
1189+
10831190 /* Foundation */
10841191 RUN_SELECTED_SUITE (arena );
10851192 RUN_SELECTED_SUITE (hash_table );
0 commit comments