Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix: preserve watcher roots and refresh options
Martin Vogelb260a4bf70ab
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex 5573f4152..0981f6fc2 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -105,6 +105,12 @@ has not run yet).> narrower: it globally controls MCP session watch registration. On the next> refresh or registration request, `false` removes all watches; `true` allows> session roots and successful explicit `index_repository` targets to register.+> Watcher refreshes retain `mode`, `persistence`, and the resolved+> `doc_candidates` boolean from the latest successful explicit index registered+> for that target. This boolean can come from the request or its global setting.+> When neither supplied a boolean, refresh uses the current setting or mode default.+> A watcher callback also observes `auto_watch=false`: it removes all watches+> before admitting any new refresh work.> When `watcher_enabled=false`,> `auto_watch` has no effect — there is no watcher to register with.>diff --git a/src/daemon/application.c b/src/daemon/application.cindex d45fcabab..c431fe88f 100644--- a/src/daemon/application.c+++ b/src/daemon/application.c@@ -104,6 +104,8 @@ struct cbm_daemon_application_watch {char *root;char *index_mode;bool persistence;+ bool has_doc_candidates;+ bool doc_candidates;size_t subscribers;cbm_daemon_application_watch_t *next;};@@ -472,6 +474,35 @@ static bool application_canonical_directory_exists(const char *path) {return cbm_path_info_utf8(path, &info) == 0 && info.is_directory;}+/* Compare canonical roots without changing the native spelling used by workspace policy. */+static bool application_watch_roots_equal(const char *left, const char *right, bool windows_paths) {+ while (*left && *right) {+ char left_char = *left++;+ char right_char = *right++;+ if (windows_paths) {+ left_char = left_char == '\\' ? '/' : left_char;+ right_char = right_char == '\\' ? '/' : right_char;+ }+ if (left_char != right_char) {+ return false;+ }+ }+ return *left == *right;+}++static bool application_watch_root_matches(const char *left, const char *right) {+#ifdef _WIN32+ return application_watch_roots_equal(left, right, true);+#else+ return application_watch_roots_equal(left, right, false);+#endif+}++bool cbm_daemon_application_watch_roots_equal_for_test(const char *left, const char *right,+ bool windows_paths) {+ return application_watch_roots_equal(left, right, windows_paths);+}+static cbm_daemon_application_watch_t *application_find_watch_locked(cbm_daemon_application_t *application, const char *project) {for (cbm_daemon_application_watch_t *watch = application->watches; watch; watch = watch->next) {@@ -520,6 +551,19 @@ static void application_remove_watch_locked(cbm_daemon_application_t *applicatioapplication_remove_watch_entry_locked(application, watch, true);}+/* Registration and refresh can each be the first observer of the global+ * switch. Drain memberships and physical watches together under the mutex. */+static bool application_watches_disabled_locked(cbm_daemon_application_t *application) {+ if (!application->config ||+ cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) {+ return false;+ }+ while (application->watches) {+ application_remove_watch_locked(application, application->watches);+ }+ return true;+}+static void application_release_session_watch_locked(cbm_daemon_application_session_t *session) {application_watch_job_unsubscribe_session_locked(session, NULL);while (session->watches) {@@ -581,6 +625,9 @@ static bool application_watch_set_options(cbm_daemon_application_watch_t *watch,cbm_free(CBM_MEM_CLASS_OTHER, watch->index_mode);watch->index_mode = mode_copy;watch->persistence = yyjson_get_bool(yyjson_obj_get(args, "persistence"));+ yyjson_val *doc_candidates = yyjson_obj_get(args, "doc_candidates");+ watch->has_doc_candidates = yyjson_is_bool(doc_candidates);+ watch->doc_candidates = yyjson_get_bool(doc_candidates);yyjson_doc_free(doc);return true;}@@ -604,19 +651,18 @@ static void application_register_watch_locked(cbm_daemon_application_session_t *const char *project, const char *root,const char *args_json) {cbm_daemon_application_t *application = session->application;- if (!application_watch_registration_allowed(session, project, root)) {+ if (application_watches_disabled_locked(application) ||+ !application_watch_registration_allowed(session, project, root)) {return;}cbm_daemon_application_watch_t *watch = application_find_watch_locked(application, project);- if ((application->config &&- !cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) ||- !application_regular_db_exists(project)) {+ if (!application_regular_db_exists(project)) {if (watch) {application_remove_watch_locked(application, watch);}return;}- if (watch && strcmp(watch->root, root) != 0) {+ if (watch && !application_watch_root_matches(watch->root, root)) {cbm_log_warn("daemon.watch.project_collision", "project", project, "existing_root",watch->root, "requested_root", root);return;@@ -668,11 +714,7 @@ static void application_register_watch_locked(cbm_daemon_application_session_t *static void application_refresh_watch_locked(cbm_daemon_application_session_t *session) {cbm_daemon_application_t *application = session->application;- if (application->config &&- !cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) {- while (application->watches) {- application_remove_watch_locked(application, application->watches);- }+ if (application_watches_disabled_locked(application)) {return;}if (session->auto_index_subscribed || application_request_cancelled_locked(session)) {@@ -2641,7 +2683,8 @@ static bool application_watch_job_subscribe_late_session_locked(cbm_daemon_application_t *application = session->application;cbm_daemon_application_job_t *job =application_find_active_job_locked(application, watch->project);- if (!job || job->watcher_waiters == 0 || strcmp(job->root_path, watch->root) != 0 ||+ if (!job || job->watcher_waiters == 0 ||+ !application_watch_root_matches(job->root_path, watch->root) ||application_watch_job_subscription_exists_locked(application, session, job)) {return true;}@@ -2667,7 +2710,7 @@ static bool application_watch_job_subscribe_sessions_locked(cbm_daemon_applicatisize_t *matched_out) {*matched_out = 0;if (!watch || !job || strcmp(watch->project, job->project_key) != 0 ||- strcmp(watch->root, job->root_path) != 0) {+ !application_watch_root_matches(watch->root, job->root_path)) {return true;}@@ -2718,8 +2761,9 @@ static void application_watch_job_unsubscribe_session_locked(while (*cursor) {cbm_daemon_application_watch_job_subscription_t *subscription = *cursor;if (subscription->session != session ||- (watch && (strcmp(subscription->job->project_key, watch->project) != 0 ||- strcmp(subscription->job->root_path, watch->root) != 0))) {+ (watch &&+ (strcmp(subscription->job->project_key, watch->project) != 0 ||+ !application_watch_root_matches(subscription->job->root_path, watch->root)))) {cursor = &subscription->next;continue;}@@ -4201,6 +4245,11 @@ static void application_watch_encode_index_args_locked(const cbm_daemon_applicatbool encoded =!watch->index_mode || yyjson_mut_obj_add_strcpy(document, root, "mode", watch->index_mode);encoded = encoded && yyjson_mut_obj_add_bool(document, root, "persistence", watch->persistence);+ if (watch->has_doc_candidates) {+ (void)yyjson_mut_obj_remove_key(root, "doc_candidates");+ encoded = encoded &&+ yyjson_mut_obj_add_bool(document, root, "doc_candidates", watch->doc_candidates);+ }free(*args);*args = encoded ? yyjson_mut_write(document, 0, NULL) : NULL;}@@ -4212,10 +4261,14 @@ static cbm_daemon_application_job_t *application_background_index_admit(application_job_subscribe_status_t subscribe_status = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE;application_jobs_reap_completed(application);cbm_mutex_lock(&application->mutex);+ if (require_live_watch) {+ (void)application_watches_disabled_locked(application);+ }cbm_daemon_application_watch_t *watch =require_live_watch ? application_find_watch_locked(application, project_name) : NULL;- bool watch_live = !require_live_watch ||- (watch && watch->subscribers > 0 && strcmp(watch->root, canonical_root) == 0);+ bool watch_live =+ !require_live_watch || (watch && watch->subscribers > 0 &&+ application_watch_root_matches(watch->root, canonical_root));if (require_live_watch && watch_live) {application_watch_encode_index_args_locked(watch, document, root, args);}diff --git a/src/daemon/application_internal.h b/src/daemon/application_internal.hindex bd06e4efe..39b483898 100644--- a/src/daemon/application_internal.h+++ b/src/daemon/application_internal.h@@ -51,4 +51,9 @@ char *cbm_daemon_application_framable_response_for_test(char *response,* on Windows, while the fold itself runs on every platform. */bool cbm_daemon_application_index_args_equal_for_test(const char *left, const char *right);+/* Exercise the production root comparison's Windows branch on any host;+ * POSIX backslashes remain literal filename characters. */+bool cbm_daemon_application_watch_roots_equal_for_test(const char *left, const char *right,+ bool windows_paths);+#endif /* CBM_DAEMON_APPLICATION_INTERNAL_H */diff --git a/tests/test_daemon_application.c b/tests/test_daemon_application.cindex 2f38c31df..343750fbf 100644--- a/tests/test_daemon_application.c+++ b/tests/test_daemon_application.c@@ -4673,6 +4673,182 @@ static bool app_test_explicit_index(cbm_daemon_runtime_application_callbacks_t *return app_test_explicit_index_with_options(callbacks, session, root, name, NULL);}+TEST(daemon_application_watch_roots_match_windows_separators_only) {+ ASSERT_TRUE(+ cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm", "C:/repos/cbm", true));+ ASSERT_TRUE(cbm_daemon_application_watch_roots_equal_for_test("\\\\server\\share\\cbm",+ "//server/share/cbm", true));+ ASSERT_FALSE(+ cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm", "C:/repos/cbm2", true));+ ASSERT_FALSE(cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm",+ "C:/repos/cbm/sub", true));+ ASSERT_FALSE(+ cbm_daemon_application_watch_roots_equal_for_test("/repos/a\\b", "/repos/a/b", false));+ ASSERT_TRUE(+ cbm_daemon_application_watch_roots_equal_for_test("/repos/a\\b", "/repos/a\\b", false));+ PASS();+}++/* A held async worker completes after the global switch changes. No subsequent+ * request may hide target-only cleanup by refreshing the session root. */+TEST(daemon_application_async_completion_disables_all_watches) {+ app_watch_race_fixture_t fixture;+ bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9320, true);+ char target[APP_TEST_PATH_CAP];+ (void)snprintf(target, sizeof(target), "%s/cbm-app-disable-async-XXXXXX", fixture.root);+ bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;+ char db_path[APP_TEST_PATH_CAP];+ (void)snprintf(db_path, sizeof(db_path), "%s/disable-target.db", fixture.cache);+ bool db_ready = target_ready && app_test_create_empty_file(db_path);+ atomic_store(&fixture.fake.allow_completion, true);+ bool indexed = db_ready && app_test_explicit_index(&fixture.callbacks, fixture.session, target,+ "disable-target");+ int watches_before = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;+ atomic_store(&fixture.fake.allow_completion, false);+ uint8_t *request = NULL;+ uint32_t request_length = 0;+ uint8_t *response = NULL;+ uint32_t response_length = 0;+ bool encoded =+ indexed && app_test_mcp_index_request(target, "disable-target", ",\"async\":true", &request,+ &request_length);+ cbm_daemon_runtime_application_status_t status =+ encoded ? app_test_request(&fixture.callbacks, fixture.session, request, request_length,+ &response, &response_length)+ : CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR;+ bool started = status == CBM_DAEMON_RUNTIME_APPLICATION_OK && response &&+ strstr((char *)response, "\"async\":true") &&+ app_wait_for_atomic_int(&fixture.fake.starts, 2);+ bool disabled = started && cbm_config_set(fixture.config, CBM_CONFIG_AUTO_WATCH, "false") == 0;+ atomic_store(&fixture.fake.allow_completion, true);+ bool drained = started && app_wait_for_active_jobs(fixture.application, 0);+ int watches_after = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;+ bool cleaned = app_watch_race_fixture_finish(&fixture);+ free(request);+ free(response);++ ASSERT_TRUE(fixture_ready);+ ASSERT_TRUE(target_ready);+ ASSERT_TRUE(db_ready);+ ASSERT_TRUE(indexed);+ ASSERT_EQ(watches_before, 2);+ ASSERT_TRUE(started);+ ASSERT_TRUE(disabled);+ ASSERT_TRUE(drained);+ ASSERT_EQ(watches_after, 0);+ ASSERT_TRUE(cleaned);+ PASS();+}++/* The callback can be the first observer of a config change, without a+ * client request. It must drain every target before admitting refresh work. */+TEST(daemon_application_watcher_callback_disables_all_watches) {+ app_watch_race_fixture_t fixture;+ bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9322, true);+ char target[APP_TEST_PATH_CAP];+ (void)snprintf(target, sizeof(target), "%s/cbm-app-disable-callback-XXXXXX", fixture.root);+ bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;+ char db_path[APP_TEST_PATH_CAP];+ (void)snprintf(db_path, sizeof(db_path), "%s/callback-target.db", fixture.cache);+ bool db_ready = target_ready && app_test_create_empty_file(db_path);+ atomic_store(&fixture.fake.allow_completion, true);+ bool indexed = db_ready && app_test_explicit_index(&fixture.callbacks, fixture.session, target,+ "callback-target");+ int watches_before = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;+ bool disabled = indexed && cbm_config_set(fixture.config, CBM_CONFIG_AUTO_WATCH, "false") == 0;+ int refreshed = disabled ? cbm_daemon_application_watcher_index("callback-target", target,+ fixture.application)+ : -1;+ int watches_after = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;+ int starts = atomic_load(&fixture.fake.starts);+ bool cleaned = app_watch_race_fixture_finish(&fixture);++ ASSERT_TRUE(fixture_ready);+ ASSERT_TRUE(target_ready);+ ASSERT_TRUE(db_ready);+ ASSERT_TRUE(indexed);+ ASSERT_EQ(watches_before, 2);+ ASSERT_TRUE(disabled);+ ASSERT_EQ(refreshed, 1);+ ASSERT_EQ(watches_after, 0);+ ASSERT_EQ(starts, 1);+ ASSERT_TRUE(cleaned);+ PASS();+}++static int app_watch_preserves_doc_candidates(int explicit_value) {+ app_watch_race_fixture_t fixture;+ bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9321, true);+ char target[APP_TEST_PATH_CAP];+ (void)snprintf(target, sizeof(target), "%s/cbm-app-watch-options-XXXXXX", fixture.root);+ bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;+ char db_path[APP_TEST_PATH_CAP];+ (void)snprintf(db_path, sizeof(db_path), "%s/options-target.db", fixture.cache);+ bool db_ready = target_ready && app_test_create_empty_file(db_path);+ /* The opposite global setting must be replaced, not duplicated in JSON. */+ bool configured = db_ready && cbm_config_set(fixture.config, CBM_CONFIG_DOC_CANDIDATES,+ explicit_value < 0 ? "auto"+ : explicit_value ? "false"+ : "true") == 0;+ atomic_store(&fixture.fake.allow_completion, true);+ bool indexed =+ configured &&+ app_test_explicit_index_with_options(+ &fixture.callbacks, fixture.session, target, "options-target",+ explicit_value < 0 ? ",\"mode\":\"fast\",\"persistence\":true"+ : explicit_value ? ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":true"+ : ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":false");+ bool default_updated =+ explicit_value >= 0 ||+ (indexed && cbm_config_set(fixture.config, CBM_CONFIG_DOC_CANDIDATES, "true") == 0);+ int refreshed =+ indexed && default_updated+ ? cbm_daemon_application_watcher_index("options-target", target, fixture.application)+ : -1;+ const char *args = fixture.fake.args_json[1];+ yyjson_doc *doc = refreshed == 0 ? yyjson_read(args, strlen(args), 0) : NULL;+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;+ yyjson_val *choice = yyjson_obj_get(root, "doc_candidates");+ int forwarded = !choice ? -1 : !yyjson_is_bool(choice) ? -2 : yyjson_get_bool(choice) ? 1 : 0;+ bool mode_preserved = yyjson_equals_str(yyjson_obj_get(root, "mode"), "fast");+ bool persistence_preserved = yyjson_get_bool(yyjson_obj_get(root, "persistence"));+ size_t choices = 0;+ size_t idx, max;+ yyjson_val *key, *value;+ yyjson_obj_foreach(root, idx, max, key, value) {+ if (yyjson_equals_str(key, "doc_candidates")) {+ choices++;+ }+ }+ yyjson_doc_free(doc);+ bool cleaned = app_watch_race_fixture_finish(&fixture);+ ASSERT_TRUE(fixture_ready);+ ASSERT_TRUE(target_ready);+ ASSERT_TRUE(db_ready);+ ASSERT_TRUE(configured);+ ASSERT_TRUE(indexed);+ ASSERT_TRUE(default_updated);+ ASSERT_EQ(refreshed, 0);+ ASSERT_TRUE(cleaned);+ ASSERT_TRUE(mode_preserved);+ ASSERT_TRUE(persistence_preserved);+ ASSERT_EQ(choices, 1);+ ASSERT_EQ(forwarded, explicit_value != 0);+ PASS();+}++TEST(daemon_application_watch_refresh_uses_current_doc_candidates_default) {+ return app_watch_preserves_doc_candidates(-1);+}++TEST(daemon_application_watch_refresh_preserves_doc_candidates_true) {+ return app_watch_preserves_doc_candidates(true);+}++TEST(daemon_application_watch_refresh_preserves_doc_candidates_false) {+ return app_watch_preserves_doc_candidates(false);+}+TEST(daemon_application_explicit_index_registers_target_watch_for_live_session) {app_watch_race_fixture_t fixture;bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9301, true);@@ -7467,6 +7643,12 @@ SUITE(daemon_application) {RUN_TEST(daemon_application_final_cancel_drains_admitted_watcher_job);RUN_TEST(daemon_application_watcher_job_follows_exact_live_watch_owners);RUN_TEST(daemon_application_late_watcher_session_owns_active_watcher_job);+ RUN_TEST(daemon_application_watch_roots_match_windows_separators_only);+ RUN_TEST(daemon_application_async_completion_disables_all_watches);+ RUN_TEST(daemon_application_watcher_callback_disables_all_watches);+ RUN_TEST(daemon_application_watch_refresh_uses_current_doc_candidates_default);+ RUN_TEST(daemon_application_watch_refresh_preserves_doc_candidates_true);+ RUN_TEST(daemon_application_watch_refresh_preserves_doc_candidates_false);RUN_TEST(daemon_application_explicit_index_registers_target_watch_for_live_session);RUN_TEST(daemon_application_failed_explicit_index_does_not_register_target_watch);RUN_TEST(daemon_application_auto_watch_false_suppresses_explicit_target_watch);