Skip to content

Commit b260a4b

Browse files
committed
fix: preserve watcher roots and refresh options
Match canonical Windows separators across watch ownership and admission, drain all session watches when auto_watch is disabled, and retain the resolved doc_candidates choice for later refreshes. Add deterministic regressions for the five observed failures and an absent-option guard. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0325d6a commit b260a4b

4 files changed

Lines changed: 262 additions & 16 deletions

File tree

‎docs/CONFIGURATION.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ has not run yet).
105105
> narrower: it globally controls MCP session watch registration. On the next
106106
> refresh or registration request, `false` removes all watches; `true` allows
107107
> session roots and successful explicit `index_repository` targets to register.
108+
> Watcher refreshes retain `mode`, `persistence`, and the resolved
109+
> `doc_candidates` boolean from the latest successful explicit index registered
110+
> for that target. This boolean can come from the request or its global setting.
111+
> When neither supplied a boolean, refresh uses the current setting or mode default.
112+
> A watcher callback also observes `auto_watch=false`: it removes all watches
113+
> before admitting any new refresh work.
108114
> When `watcher_enabled=false`,
109115
> `auto_watch` has no effect — there is no watcher to register with.
110116
>

‎src/daemon/application.c‎

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ struct cbm_daemon_application_watch {
104104
char *root;
105105
char *index_mode;
106106
bool persistence;
107+
bool has_doc_candidates;
108+
bool doc_candidates;
107109
size_t subscribers;
108110
cbm_daemon_application_watch_t *next;
109111
};
@@ -472,6 +474,35 @@ static bool application_canonical_directory_exists(const char *path) {
472474
return cbm_path_info_utf8(path, &info) == 0 && info.is_directory;
473475
}
474476

477+
/* Compare canonical roots without changing the native spelling used by workspace policy. */
478+
static bool application_watch_roots_equal(const char *left, const char *right, bool windows_paths) {
479+
while (*left && *right) {
480+
char left_char = *left++;
481+
char right_char = *right++;
482+
if (windows_paths) {
483+
left_char = left_char == '\\' ? '/' : left_char;
484+
right_char = right_char == '\\' ? '/' : right_char;
485+
}
486+
if (left_char != right_char) {
487+
return false;
488+
}
489+
}
490+
return *left == *right;
491+
}
492+
493+
static bool application_watch_root_matches(const char *left, const char *right) {
494+
#ifdef _WIN32
495+
return application_watch_roots_equal(left, right, true);
496+
#else
497+
return application_watch_roots_equal(left, right, false);
498+
#endif
499+
}
500+
501+
bool cbm_daemon_application_watch_roots_equal_for_test(const char *left, const char *right,
502+
bool windows_paths) {
503+
return application_watch_roots_equal(left, right, windows_paths);
504+
}
505+
475506
static cbm_daemon_application_watch_t *application_find_watch_locked(
476507
cbm_daemon_application_t *application, const char *project) {
477508
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 *applicatio
520551
application_remove_watch_entry_locked(application, watch, true);
521552
}
522553

554+
/* Registration and refresh can each be the first observer of the global
555+
* switch. Drain memberships and physical watches together under the mutex. */
556+
static bool application_watches_disabled_locked(cbm_daemon_application_t *application) {
557+
if (!application->config ||
558+
cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) {
559+
return false;
560+
}
561+
while (application->watches) {
562+
application_remove_watch_locked(application, application->watches);
563+
}
564+
return true;
565+
}
566+
523567
static void application_release_session_watch_locked(cbm_daemon_application_session_t *session) {
524568
application_watch_job_unsubscribe_session_locked(session, NULL);
525569
while (session->watches) {
@@ -581,6 +625,9 @@ static bool application_watch_set_options(cbm_daemon_application_watch_t *watch,
581625
cbm_free(CBM_MEM_CLASS_OTHER, watch->index_mode);
582626
watch->index_mode = mode_copy;
583627
watch->persistence = yyjson_get_bool(yyjson_obj_get(args, "persistence"));
628+
yyjson_val *doc_candidates = yyjson_obj_get(args, "doc_candidates");
629+
watch->has_doc_candidates = yyjson_is_bool(doc_candidates);
630+
watch->doc_candidates = yyjson_get_bool(doc_candidates);
584631
yyjson_doc_free(doc);
585632
return true;
586633
}
@@ -604,19 +651,18 @@ static void application_register_watch_locked(cbm_daemon_application_session_t *
604651
const char *project, const char *root,
605652
const char *args_json) {
606653
cbm_daemon_application_t *application = session->application;
607-
if (!application_watch_registration_allowed(session, project, root)) {
654+
if (application_watches_disabled_locked(application) ||
655+
!application_watch_registration_allowed(session, project, root)) {
608656
return;
609657
}
610658
cbm_daemon_application_watch_t *watch = application_find_watch_locked(application, project);
611-
if ((application->config &&
612-
!cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) ||
613-
!application_regular_db_exists(project)) {
659+
if (!application_regular_db_exists(project)) {
614660
if (watch) {
615661
application_remove_watch_locked(application, watch);
616662
}
617663
return;
618664
}
619-
if (watch && strcmp(watch->root, root) != 0) {
665+
if (watch && !application_watch_root_matches(watch->root, root)) {
620666
cbm_log_warn("daemon.watch.project_collision", "project", project, "existing_root",
621667
watch->root, "requested_root", root);
622668
return;
@@ -668,11 +714,7 @@ static void application_register_watch_locked(cbm_daemon_application_session_t *
668714

669715
static void application_refresh_watch_locked(cbm_daemon_application_session_t *session) {
670716
cbm_daemon_application_t *application = session->application;
671-
if (application->config &&
672-
!cbm_config_get_bool(application->config, CBM_CONFIG_AUTO_WATCH, true)) {
673-
while (application->watches) {
674-
application_remove_watch_locked(application, application->watches);
675-
}
717+
if (application_watches_disabled_locked(application)) {
676718
return;
677719
}
678720
if (session->auto_index_subscribed || application_request_cancelled_locked(session)) {
@@ -2641,7 +2683,8 @@ static bool application_watch_job_subscribe_late_session_locked(
26412683
cbm_daemon_application_t *application = session->application;
26422684
cbm_daemon_application_job_t *job =
26432685
application_find_active_job_locked(application, watch->project);
2644-
if (!job || job->watcher_waiters == 0 || strcmp(job->root_path, watch->root) != 0 ||
2686+
if (!job || job->watcher_waiters == 0 ||
2687+
!application_watch_root_matches(job->root_path, watch->root) ||
26452688
application_watch_job_subscription_exists_locked(application, session, job)) {
26462689
return true;
26472690
}
@@ -2667,7 +2710,7 @@ static bool application_watch_job_subscribe_sessions_locked(cbm_daemon_applicati
26672710
size_t *matched_out) {
26682711
*matched_out = 0;
26692712
if (!watch || !job || strcmp(watch->project, job->project_key) != 0 ||
2670-
strcmp(watch->root, job->root_path) != 0) {
2713+
!application_watch_root_matches(watch->root, job->root_path)) {
26712714
return true;
26722715
}
26732716

@@ -2718,8 +2761,9 @@ static void application_watch_job_unsubscribe_session_locked(
27182761
while (*cursor) {
27192762
cbm_daemon_application_watch_job_subscription_t *subscription = *cursor;
27202763
if (subscription->session != session ||
2721-
(watch && (strcmp(subscription->job->project_key, watch->project) != 0 ||
2722-
strcmp(subscription->job->root_path, watch->root) != 0))) {
2764+
(watch &&
2765+
(strcmp(subscription->job->project_key, watch->project) != 0 ||
2766+
!application_watch_root_matches(subscription->job->root_path, watch->root)))) {
27232767
cursor = &subscription->next;
27242768
continue;
27252769
}
@@ -4201,6 +4245,11 @@ static void application_watch_encode_index_args_locked(const cbm_daemon_applicat
42014245
bool encoded =
42024246
!watch->index_mode || yyjson_mut_obj_add_strcpy(document, root, "mode", watch->index_mode);
42034247
encoded = encoded && yyjson_mut_obj_add_bool(document, root, "persistence", watch->persistence);
4248+
if (watch->has_doc_candidates) {
4249+
(void)yyjson_mut_obj_remove_key(root, "doc_candidates");
4250+
encoded = encoded &&
4251+
yyjson_mut_obj_add_bool(document, root, "doc_candidates", watch->doc_candidates);
4252+
}
42044253
free(*args);
42054254
*args = encoded ? yyjson_mut_write(document, 0, NULL) : NULL;
42064255
}
@@ -4212,10 +4261,14 @@ static cbm_daemon_application_job_t *application_background_index_admit(
42124261
application_job_subscribe_status_t subscribe_status = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE;
42134262
application_jobs_reap_completed(application);
42144263
cbm_mutex_lock(&application->mutex);
4264+
if (require_live_watch) {
4265+
(void)application_watches_disabled_locked(application);
4266+
}
42154267
cbm_daemon_application_watch_t *watch =
42164268
require_live_watch ? application_find_watch_locked(application, project_name) : NULL;
4217-
bool watch_live = !require_live_watch ||
4218-
(watch && watch->subscribers > 0 && strcmp(watch->root, canonical_root) == 0);
4269+
bool watch_live =
4270+
!require_live_watch || (watch && watch->subscribers > 0 &&
4271+
application_watch_root_matches(watch->root, canonical_root));
42194272
if (require_live_watch && watch_live) {
42204273
application_watch_encode_index_args_locked(watch, document, root, args);
42214274
}

‎src/daemon/application_internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ char *cbm_daemon_application_framable_response_for_test(char *response,
5151
* on Windows, while the fold itself runs on every platform. */
5252
bool cbm_daemon_application_index_args_equal_for_test(const char *left, const char *right);
5353

54+
/* Exercise the production root comparison's Windows branch on any host;
55+
* POSIX backslashes remain literal filename characters. */
56+
bool cbm_daemon_application_watch_roots_equal_for_test(const char *left, const char *right,
57+
bool windows_paths);
58+
5459
#endif /* CBM_DAEMON_APPLICATION_INTERNAL_H */

‎tests/test_daemon_application.c‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4673,6 +4673,182 @@ static bool app_test_explicit_index(cbm_daemon_runtime_application_callbacks_t *
46734673
return app_test_explicit_index_with_options(callbacks, session, root, name, NULL);
46744674
}
46754675

4676+
TEST(daemon_application_watch_roots_match_windows_separators_only) {
4677+
ASSERT_TRUE(
4678+
cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm", "C:/repos/cbm", true));
4679+
ASSERT_TRUE(cbm_daemon_application_watch_roots_equal_for_test("\\\\server\\share\\cbm",
4680+
"//server/share/cbm", true));
4681+
ASSERT_FALSE(
4682+
cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm", "C:/repos/cbm2", true));
4683+
ASSERT_FALSE(cbm_daemon_application_watch_roots_equal_for_test("C:\\repos\\cbm",
4684+
"C:/repos/cbm/sub", true));
4685+
ASSERT_FALSE(
4686+
cbm_daemon_application_watch_roots_equal_for_test("/repos/a\\b", "/repos/a/b", false));
4687+
ASSERT_TRUE(
4688+
cbm_daemon_application_watch_roots_equal_for_test("/repos/a\\b", "/repos/a\\b", false));
4689+
PASS();
4690+
}
4691+
4692+
/* A held async worker completes after the global switch changes. No subsequent
4693+
* request may hide target-only cleanup by refreshing the session root. */
4694+
TEST(daemon_application_async_completion_disables_all_watches) {
4695+
app_watch_race_fixture_t fixture;
4696+
bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9320, true);
4697+
char target[APP_TEST_PATH_CAP];
4698+
(void)snprintf(target, sizeof(target), "%s/cbm-app-disable-async-XXXXXX", fixture.root);
4699+
bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;
4700+
char db_path[APP_TEST_PATH_CAP];
4701+
(void)snprintf(db_path, sizeof(db_path), "%s/disable-target.db", fixture.cache);
4702+
bool db_ready = target_ready && app_test_create_empty_file(db_path);
4703+
atomic_store(&fixture.fake.allow_completion, true);
4704+
bool indexed = db_ready && app_test_explicit_index(&fixture.callbacks, fixture.session, target,
4705+
"disable-target");
4706+
int watches_before = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;
4707+
atomic_store(&fixture.fake.allow_completion, false);
4708+
uint8_t *request = NULL;
4709+
uint32_t request_length = 0;
4710+
uint8_t *response = NULL;
4711+
uint32_t response_length = 0;
4712+
bool encoded =
4713+
indexed && app_test_mcp_index_request(target, "disable-target", ",\"async\":true", &request,
4714+
&request_length);
4715+
cbm_daemon_runtime_application_status_t status =
4716+
encoded ? app_test_request(&fixture.callbacks, fixture.session, request, request_length,
4717+
&response, &response_length)
4718+
: CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR;
4719+
bool started = status == CBM_DAEMON_RUNTIME_APPLICATION_OK && response &&
4720+
strstr((char *)response, "\"async\":true") &&
4721+
app_wait_for_atomic_int(&fixture.fake.starts, 2);
4722+
bool disabled = started && cbm_config_set(fixture.config, CBM_CONFIG_AUTO_WATCH, "false") == 0;
4723+
atomic_store(&fixture.fake.allow_completion, true);
4724+
bool drained = started && app_wait_for_active_jobs(fixture.application, 0);
4725+
int watches_after = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;
4726+
bool cleaned = app_watch_race_fixture_finish(&fixture);
4727+
free(request);
4728+
free(response);
4729+
4730+
ASSERT_TRUE(fixture_ready);
4731+
ASSERT_TRUE(target_ready);
4732+
ASSERT_TRUE(db_ready);
4733+
ASSERT_TRUE(indexed);
4734+
ASSERT_EQ(watches_before, 2);
4735+
ASSERT_TRUE(started);
4736+
ASSERT_TRUE(disabled);
4737+
ASSERT_TRUE(drained);
4738+
ASSERT_EQ(watches_after, 0);
4739+
ASSERT_TRUE(cleaned);
4740+
PASS();
4741+
}
4742+
4743+
/* The callback can be the first observer of a config change, without a
4744+
* client request. It must drain every target before admitting refresh work. */
4745+
TEST(daemon_application_watcher_callback_disables_all_watches) {
4746+
app_watch_race_fixture_t fixture;
4747+
bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9322, true);
4748+
char target[APP_TEST_PATH_CAP];
4749+
(void)snprintf(target, sizeof(target), "%s/cbm-app-disable-callback-XXXXXX", fixture.root);
4750+
bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;
4751+
char db_path[APP_TEST_PATH_CAP];
4752+
(void)snprintf(db_path, sizeof(db_path), "%s/callback-target.db", fixture.cache);
4753+
bool db_ready = target_ready && app_test_create_empty_file(db_path);
4754+
atomic_store(&fixture.fake.allow_completion, true);
4755+
bool indexed = db_ready && app_test_explicit_index(&fixture.callbacks, fixture.session, target,
4756+
"callback-target");
4757+
int watches_before = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;
4758+
bool disabled = indexed && cbm_config_set(fixture.config, CBM_CONFIG_AUTO_WATCH, "false") == 0;
4759+
int refreshed = disabled ? cbm_daemon_application_watcher_index("callback-target", target,
4760+
fixture.application)
4761+
: -1;
4762+
int watches_after = fixture.watcher ? cbm_watcher_watch_count(fixture.watcher) : -1;
4763+
int starts = atomic_load(&fixture.fake.starts);
4764+
bool cleaned = app_watch_race_fixture_finish(&fixture);
4765+
4766+
ASSERT_TRUE(fixture_ready);
4767+
ASSERT_TRUE(target_ready);
4768+
ASSERT_TRUE(db_ready);
4769+
ASSERT_TRUE(indexed);
4770+
ASSERT_EQ(watches_before, 2);
4771+
ASSERT_TRUE(disabled);
4772+
ASSERT_EQ(refreshed, 1);
4773+
ASSERT_EQ(watches_after, 0);
4774+
ASSERT_EQ(starts, 1);
4775+
ASSERT_TRUE(cleaned);
4776+
PASS();
4777+
}
4778+
4779+
static int app_watch_preserves_doc_candidates(int explicit_value) {
4780+
app_watch_race_fixture_t fixture;
4781+
bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9321, true);
4782+
char target[APP_TEST_PATH_CAP];
4783+
(void)snprintf(target, sizeof(target), "%s/cbm-app-watch-options-XXXXXX", fixture.root);
4784+
bool target_ready = fixture_ready && cbm_mkdtemp(target) != NULL;
4785+
char db_path[APP_TEST_PATH_CAP];
4786+
(void)snprintf(db_path, sizeof(db_path), "%s/options-target.db", fixture.cache);
4787+
bool db_ready = target_ready && app_test_create_empty_file(db_path);
4788+
/* The opposite global setting must be replaced, not duplicated in JSON. */
4789+
bool configured = db_ready && cbm_config_set(fixture.config, CBM_CONFIG_DOC_CANDIDATES,
4790+
explicit_value < 0 ? "auto"
4791+
: explicit_value ? "false"
4792+
: "true") == 0;
4793+
atomic_store(&fixture.fake.allow_completion, true);
4794+
bool indexed =
4795+
configured &&
4796+
app_test_explicit_index_with_options(
4797+
&fixture.callbacks, fixture.session, target, "options-target",
4798+
explicit_value < 0 ? ",\"mode\":\"fast\",\"persistence\":true"
4799+
: explicit_value ? ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":true"
4800+
: ",\"mode\":\"fast\",\"persistence\":true,\"doc_candidates\":false");
4801+
bool default_updated =
4802+
explicit_value >= 0 ||
4803+
(indexed && cbm_config_set(fixture.config, CBM_CONFIG_DOC_CANDIDATES, "true") == 0);
4804+
int refreshed =
4805+
indexed && default_updated
4806+
? cbm_daemon_application_watcher_index("options-target", target, fixture.application)
4807+
: -1;
4808+
const char *args = fixture.fake.args_json[1];
4809+
yyjson_doc *doc = refreshed == 0 ? yyjson_read(args, strlen(args), 0) : NULL;
4810+
yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
4811+
yyjson_val *choice = yyjson_obj_get(root, "doc_candidates");
4812+
int forwarded = !choice ? -1 : !yyjson_is_bool(choice) ? -2 : yyjson_get_bool(choice) ? 1 : 0;
4813+
bool mode_preserved = yyjson_equals_str(yyjson_obj_get(root, "mode"), "fast");
4814+
bool persistence_preserved = yyjson_get_bool(yyjson_obj_get(root, "persistence"));
4815+
size_t choices = 0;
4816+
size_t idx, max;
4817+
yyjson_val *key, *value;
4818+
yyjson_obj_foreach(root, idx, max, key, value) {
4819+
if (yyjson_equals_str(key, "doc_candidates")) {
4820+
choices++;
4821+
}
4822+
}
4823+
yyjson_doc_free(doc);
4824+
bool cleaned = app_watch_race_fixture_finish(&fixture);
4825+
ASSERT_TRUE(fixture_ready);
4826+
ASSERT_TRUE(target_ready);
4827+
ASSERT_TRUE(db_ready);
4828+
ASSERT_TRUE(configured);
4829+
ASSERT_TRUE(indexed);
4830+
ASSERT_TRUE(default_updated);
4831+
ASSERT_EQ(refreshed, 0);
4832+
ASSERT_TRUE(cleaned);
4833+
ASSERT_TRUE(mode_preserved);
4834+
ASSERT_TRUE(persistence_preserved);
4835+
ASSERT_EQ(choices, 1);
4836+
ASSERT_EQ(forwarded, explicit_value != 0);
4837+
PASS();
4838+
}
4839+
4840+
TEST(daemon_application_watch_refresh_uses_current_doc_candidates_default) {
4841+
return app_watch_preserves_doc_candidates(-1);
4842+
}
4843+
4844+
TEST(daemon_application_watch_refresh_preserves_doc_candidates_true) {
4845+
return app_watch_preserves_doc_candidates(true);
4846+
}
4847+
4848+
TEST(daemon_application_watch_refresh_preserves_doc_candidates_false) {
4849+
return app_watch_preserves_doc_candidates(false);
4850+
}
4851+
46764852
TEST(daemon_application_explicit_index_registers_target_watch_for_live_session) {
46774853
app_watch_race_fixture_t fixture;
46784854
bool fixture_ready = app_watch_race_fixture_init_mcp(&fixture, 9301, true);
@@ -7467,6 +7643,12 @@ SUITE(daemon_application) {
74677643
RUN_TEST(daemon_application_final_cancel_drains_admitted_watcher_job);
74687644
RUN_TEST(daemon_application_watcher_job_follows_exact_live_watch_owners);
74697645
RUN_TEST(daemon_application_late_watcher_session_owns_active_watcher_job);
7646+
RUN_TEST(daemon_application_watch_roots_match_windows_separators_only);
7647+
RUN_TEST(daemon_application_async_completion_disables_all_watches);
7648+
RUN_TEST(daemon_application_watcher_callback_disables_all_watches);
7649+
RUN_TEST(daemon_application_watch_refresh_uses_current_doc_candidates_default);
7650+
RUN_TEST(daemon_application_watch_refresh_preserves_doc_candidates_true);
7651+
RUN_TEST(daemon_application_watch_refresh_preserves_doc_candidates_false);
74707652
RUN_TEST(daemon_application_explicit_index_registers_target_watch_for_live_session);
74717653
RUN_TEST(daemon_application_failed_explicit_index_does_not_register_target_watch);
74727654
RUN_TEST(daemon_application_auto_watch_false_suppresses_explicit_target_watch);

0 commit comments

Comments
 (0)