Skip to content

Commit 6314186

Browse files
committed
fix(daemon): keep the workspace-policy release under daemon start
Under `daemon start` the session watch release kept every watch whose last subscriber left, so the daemon could keep watching after the session ended. The same release also runs when a session's root stops being allowed by workspace policy (an approval revoked, a boundary declared). That is not a disconnect: the session is still open and its root is refused, so keeping the watch there let a permanent daemon go on indexing a root the user had just withdrawn. application_release_session_watch_locked() now takes session_ending. The cancel and close paths pass true and keep the daemon-owned watch exactly as before; the workspace-policy branch of the watch refresh passes false and removes the watch in both modes, as it did before permanent watches. Test: daemon_application_permanent_workspace_release_still_ends_watch revokes the sensitive-root approval of a permanent session's root and sends one request; its synchronous refresh must end the watch (RED when the policy branch passes true). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 74e301d commit 6314186

2 files changed

Lines changed: 130 additions & 7 deletions

File tree

‎src/daemon/application.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ static bool application_watches_disabled_locked(cbm_daemon_application_t *applic
564564
return true;
565565
}
566566

567-
static void application_release_session_watch_locked(cbm_daemon_application_session_t *session) {
567+
static void application_release_session_watch_locked(cbm_daemon_application_session_t *session,
568+
bool session_ending) {
568569
application_watch_job_unsubscribe_session_locked(session, NULL);
569570

570571
/*
@@ -575,17 +576,22 @@ static void application_release_session_watch_locked(cbm_daemon_application_sess
575576
*
576577
* Non-permanent applications retain the historical session-scoped
577578
* lifecycle and remove the physical watch after the final subscriber.
578-
* Project deletion/pruning and daemon shutdown remove persistent watches
579-
* through their dedicated lifecycle paths.
579+
* A release decided by workspace policy (session_ending false: the
580+
* session's root is no longer allowed) is not a disconnect and removes
581+
* the watch in both modes. Daemon shutdown and auto_watch=false remove
582+
* persistent watches through their own paths; a deleted project's watch
583+
* ends at its next watcher callback
584+
* (application_permanent_watch_orphaned_locked).
580585
*/
586+
bool daemon_keeps_watch = session_ending && session->application->permanent;
581587
while (session->watches) {
582588
application_watch_membership_t *membership = session->watches;
583589
cbm_daemon_application_watch_t *watch = membership->watch;
584590
session->watches = membership->next;
585591
cbm_free(CBM_MEM_CLASS_OTHER, membership);
586592
if (watch->subscribers > 0) {
587593
watch->subscribers--;
588-
if (!session->application->permanent && watch->subscribers == 0) {
594+
if (!daemon_keeps_watch && watch->subscribers == 0) {
589595
application_remove_watch_locked(session->application, watch);
590596
}
591597
}
@@ -733,7 +739,7 @@ static void application_refresh_watch_locked(cbm_daemon_application_session_t *s
733739
return;
734740
}
735741
if (!application_session_workspace_allowed(session, "watch")) {
736-
application_release_session_watch_locked(session);
742+
application_release_session_watch_locked(session, false);
737743
return;
738744
}
739745
application_register_watch_locked(session, cbm_mcp_server_session_project(session->mcp),
@@ -3661,7 +3667,7 @@ static void application_session_cancel(void *context,
36613667
reap_update || (session->background_eligible && application->update_thread_started &&
36623668
application->update_owners == 0);
36633669
application_index_watch_unsubscribe_locked(session, NULL);
3664-
application_release_session_watch_locked(session);
3670+
application_release_session_watch_locked(session, true);
36653671
bool final_live_session = newly_cancelled;
36663672
for (cbm_daemon_application_session_t *other = application->sessions;
36673673
final_live_session && other; other = other->next) {
@@ -3721,7 +3727,7 @@ static void application_session_close(void *context,
37213727
reap_update || (session->background_eligible && application->update_thread_started &&
37223728
application->update_owners == 0);
37233729
application_index_watch_unsubscribe_locked(session, NULL);
3724-
application_release_session_watch_locked(session);
3730+
application_release_session_watch_locked(session, true);
37253731
cbm_mutex_unlock(&application->mutex);
37263732
application_auto_index_cancel_join(application, join_auto_index);
37273733
application_jobs_reap_completed(application);

‎tests/test_daemon_application.c‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
28232939
TEST(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

Comments
 (0)