@@ -2820,6 +2820,122 @@ TEST(daemon_application_sensitive_root_blocks_watch_but_preserves_controls) {
28202820 PASS ();
28212821}
28222822
2823+ /* Under `daemon start` a session that ENDS leaves its watch to the daemon, but
2824+ * a workspace-policy release is not a disconnect: once the session's root is
2825+ * no longer allowed (here its approval is revoked), the next request's
2826+ * refresh must still end the watch in a permanent generation too. */
2827+ TEST (daemon_application_permanent_workspace_release_still_ends_watch ) {
2828+ app_env_backup_t cache_environment ;
2829+ app_env_backup_t home_environment ;
2830+ bool environments_saved = app_env_backup_capture (& cache_environment , "CBM_CACHE_DIR" ) &&
2831+ app_env_backup_capture (& home_environment , "HOME" );
2832+ char sensitive_raw [APP_TEST_PATH_CAP ];
2833+ char cache_raw [APP_TEST_PATH_CAP ];
2834+ (void )snprintf (sensitive_raw , sizeof (sensitive_raw ),
2835+ "%s/cbm-app-permanent-release-home-XXXXXX" , cbm_tmpdir ());
2836+ (void )snprintf (cache_raw , sizeof (cache_raw ), "%s/cbm-app-permanent-release-cache-XXXXXX" ,
2837+ cbm_tmpdir ());
2838+ bool dirs_ok = cbm_mkdtemp (sensitive_raw ) != NULL && cbm_mkdtemp (cache_raw ) != NULL ;
2839+ char sensitive [APP_TEST_PATH_CAP ] = {0 };
2840+ char cache [APP_TEST_PATH_CAP ] = {0 };
2841+ bool canonical = dirs_ok && cbm_canonical_path (sensitive_raw , sensitive , sizeof (sensitive )) &&
2842+ cbm_canonical_path (cache_raw , cache , sizeof (cache ));
2843+ bool environments_set = environments_saved && canonical &&
2844+ cbm_setenv ("CBM_CACHE_DIR" , cache , 1 ) == 0 &&
2845+ cbm_setenv ("HOME" , sensitive , 1 ) == 0 ;
2846+ cbm_config_t * stored_config = environments_set ? cbm_config_open (cache ) : NULL ;
2847+ bool config_ready = stored_config &&
2848+ cbm_config_set (stored_config , CBM_CONFIG_AUTO_INDEX , "false" ) == 0 &&
2849+ cbm_config_set (stored_config , CBM_CONFIG_AUTO_WATCH , "true" ) == 0 ;
2850+ char * project = canonical ? cbm_project_name_from_path (sensitive ) : NULL ;
2851+ char db_path [APP_TEST_PATH_CAP ] = {0 };
2852+ if (project ) {
2853+ (void )snprintf (db_path , sizeof (db_path ), "%s/%s.db" , cache , project );
2854+ }
2855+ bool db_ready = config_ready && project && app_test_create_empty_file (db_path );
2856+ /* The root is $HOME, so only an explicit sensitive approval allows it. */
2857+ char grant_error [APP_TEST_PATH_CAP ];
2858+ bool approved = db_ready && cbm_workspace_grant_add (cache , sensitive , sensitive , true,
2859+ grant_error , sizeof (grant_error ));
2860+ char grant_path [APP_TEST_PATH_CAP ] = {0 };
2861+ bool grant_path_ok =
2862+ approved && cbm_workspace_grant_path (cache , grant_path , sizeof (grant_path ));
2863+
2864+ cbm_store_t * store = cbm_store_open_memory ();
2865+ cbm_watcher_t * watcher = cbm_watcher_new (store , app_test_index_noop , NULL );
2866+ cbm_daemon_application_config_t config = {
2867+ .watcher = watcher ,
2868+ .config = stored_config ,
2869+ };
2870+ cbm_daemon_application_t * application =
2871+ grant_path_ok && watcher ? cbm_daemon_application_new (& config ) : NULL ;
2872+ if (application ) {
2873+ cbm_daemon_application_set_permanent (application , true);
2874+ }
2875+ cbm_daemon_runtime_application_callbacks_t callbacks =
2876+ cbm_daemon_application_runtime_callbacks (application );
2877+ cbm_daemon_runtime_application_session_t * session =
2878+ application ? app_test_open (& callbacks , 4036 ) : NULL ;
2879+
2880+ bool initialized = app_test_initialize_profile (& callbacks , session , sensitive ,
2881+ CBM_MCP_TOOL_PROFILE_ALL , NULL , NULL );
2882+ int watch_while_approved = watcher ? cbm_watcher_watch_count (watcher ) : -1 ;
2883+
2884+ /* Revoke the approval. Every request ends with a synchronous watch
2885+ * refresh, so one ping is the point where the policy is re-read. */
2886+ bool revoked = initialized && cbm_unlink (grant_path ) == 0 ;
2887+ uint8_t * ping = NULL ;
2888+ uint32_t ping_length = 0 ;
2889+ uint8_t * response = NULL ;
2890+ uint32_t response_length = 0 ;
2891+ bool pinged = revoked &&
2892+ app_test_text_request (CBM_DAEMON_APPLICATION_REQUEST_MCP ,
2893+ "{\"jsonrpc\":\"2.0\",\"id\":4136,\"method\":\"ping\"}" ,
2894+ & ping , & ping_length ) &&
2895+ app_test_request (& callbacks , session , ping , ping_length , & response ,
2896+ & response_length ) == CBM_DAEMON_RUNTIME_APPLICATION_OK ;
2897+ free (response );
2898+ free (ping );
2899+ int watch_after_revoke = watcher ? cbm_watcher_watch_count (watcher ) : -1 ;
2900+
2901+ if (session ) {
2902+ callbacks .session_cancel (callbacks .context , session );
2903+ callbacks .session_close (callbacks .context , session );
2904+ }
2905+ bool stopped = application && cbm_daemon_application_shutdown (application , APP_TEST_TIMEOUT_MS );
2906+ cbm_daemon_application_free (application );
2907+ if (watcher ) {
2908+ cbm_watcher_stop (watcher );
2909+ cbm_watcher_free (watcher );
2910+ }
2911+ cbm_store_close (store );
2912+ cbm_config_close (stored_config );
2913+ free (project );
2914+ (void )th_rmtree (sensitive );
2915+ (void )th_rmtree (cache );
2916+ bool cache_restored = app_env_backup_restore (& cache_environment );
2917+ bool home_restored = app_env_backup_restore (& home_environment );
2918+
2919+ ASSERT_TRUE (environments_saved );
2920+ ASSERT_TRUE (dirs_ok );
2921+ ASSERT_TRUE (canonical );
2922+ ASSERT_TRUE (environments_set );
2923+ ASSERT_TRUE (config_ready );
2924+ ASSERT_TRUE (db_ready );
2925+ ASSERT_TRUE (approved );
2926+ ASSERT_TRUE (grant_path_ok );
2927+ ASSERT_TRUE (initialized );
2928+ ASSERT_EQ (watch_while_approved , 1 );
2929+ ASSERT_TRUE (revoked );
2930+ ASSERT_TRUE (pinged );
2931+ /* A policy release, not a disconnect: the permanent watch ends as well. */
2932+ ASSERT_EQ (watch_after_revoke , 0 );
2933+ ASSERT_TRUE (stopped );
2934+ ASSERT_TRUE (cache_restored );
2935+ ASSERT_TRUE (home_restored );
2936+ PASS ();
2937+ }
2938+
28232939TEST (daemon_application_programmatic_index_injects_resource_policy ) {
28242940 char * root = th_mktempdir ("cbm_app_policy_root" );
28252941 char * cache = th_mktempdir ("cbm_app_policy_cache" );
@@ -7904,6 +8020,7 @@ SUITE(daemon_application) {
79048020 RUN_TEST (daemon_application_initialize_coalesces_auto_index_for_full_sessions );
79058021 RUN_TEST (daemon_application_sensitive_root_blocks_auto_index_but_preserves_controls );
79068022 RUN_TEST (daemon_application_sensitive_root_blocks_watch_but_preserves_controls );
8023+ RUN_TEST (daemon_application_permanent_workspace_release_still_ends_watch );
79078024 RUN_TEST (daemon_application_ui_index_accepts_non_ascii_directory );
79088025 RUN_TEST (daemon_application_index_args_compare_repo_path_separator_equivalently );
79098026 RUN_TEST (daemon_application_async_index_returns_before_completion_and_status_polls );
0 commit comments