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
Merge origin/main into fix/permanent-daemon-watch
Martin Vogela728eeb0c43e
diff --cc src/daemon/application.cindex ce0cfdaf8,0026e8839..a4e8f8fd1--- a/src/daemon/application.c+++ b/src/daemon/application.c@@@ -149,11 -178,29 +178,29 @@@ struct cbm_daemon_application_jobcbm_daemon_application_job_t *next;};+ /* #2144: the last known index outcome per project, kept after the terminal+ * job itself is reaped so index_repository(status: true) can answer after+ * completion. sync_cut_short records that a synchronous caller gave up (client+ * cancel, deadline, disconnect) before its index finished; the next+ * index_repository or status call for that project carries the async advice.+ * This is job bookkeeping, not graph freshness (index_status owns that). */+ typedef struct cbm_daemon_application_index_record cbm_daemon_application_index_record_t;+ struct cbm_daemon_application_index_record {+ char *project_key;+ const char *state; /* static string: queued/succeeded/failed/cancelled */+ bool async;+ bool sync_cut_short;+ char started_at[32];+ char finished_at[32];+ char *error;+ cbm_daemon_application_index_record_t *next;+ };+-/* A watcher-triggered physical job is owned by the exact live sessions that- * currently subscribe to its project/root watch. The callback waiting for the- * job is only a storage waiter; it is deliberately not an ownership- * subscription, so the worker is cancelled when the last matching session- * disconnects even while unrelated daemon sessions remain alive. */+/* A watcher-triggered physical job is normally owned by the exact live+ * sessions subscribed to its project/root watch. In permanent daemon mode the+ * watcher itself additionally owns the physical job so background indexing can+ * continue while no client session is connected. The callback remains a+ * storage waiter in both modes and releases daemon ownership on completion. */struct cbm_daemon_application_watch_job_subscription {cbm_daemon_application_session_t *session;cbm_daemon_application_job_t *job;@@@ -414,30 -551,32 +551,44 @@@ static void application_remove_watch_loapplication_remove_watch_entry_locked(application, watch, true);}- static void application_release_session_watch_locked(cbm_daemon_application_session_t *session) {- cbm_daemon_application_watch_t *watch = session->watch;- if (!watch) {- return;+ /* 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;}- application_watch_job_unsubscribe_session_locked(session);- session->watch = NULL;- if (watch->subscribers > 0) {- watch->subscribers--;+ 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);++ /*+ * A permanent daemon owns the physical project watch independently of+ * individual client sessions. Session disconnect releases only that+ * session's logical subscription; the watcher remains available for+ * background change detection while the daemon generation is alive.+ *+ * Non-permanent applications retain the historical session-scoped+ * lifecycle and remove the physical watch after the final subscriber.+ * Project deletion/pruning and daemon shutdown remove persistent watches+ * through their dedicated lifecycle paths.+ */- if (!session->application->permanent && watch->subscribers == 0) {- application_remove_watch_locked(session->application, watch);+ while (session->watches) {+ application_watch_membership_t *membership = session->watches;+ cbm_daemon_application_watch_t *watch = membership->watch;+ session->watches = membership->next;+ cbm_free(CBM_MEM_CLASS_OTHER, membership);+ if (watch->subscribers > 0) {+ watch->subscribers--;- if (watch->subscribers == 0) {++ if (!session->application->permanent && watch->subscribers == 0) {+ application_remove_watch_locked(session->application, watch);+ }+ }}}@@@ -3344,85 -4234,66 +4246,79 @@@ cbm_daemon_runtime_application_status_timeout_ms);}- static int application_background_index(cbm_daemon_application_t *application,- const char *project_name, const char *root_path,- bool require_live_watch) {- if (!application || !project_name || !root_path) {- return -1;- }- char canonical_root[APPLICATION_PATH_CAP];- struct stat root_status;- if (!cbm_canonical_path(root_path, canonical_root, sizeof(canonical_root)) ||- stat(canonical_root, &root_status) != 0 || !S_ISDIR(root_status.st_mode)) {- return -1;- }- yyjson_mut_doc *document = yyjson_mut_doc_new(NULL);- yyjson_mut_val *root = document ? yyjson_mut_obj(document) : NULL;- if (!document || !root) {- yyjson_mut_doc_free(document);- return -1;- }- yyjson_mut_doc_set_root(document, root);- bool encoded = yyjson_mut_obj_add_strcpy(document, root, "repo_path", canonical_root);- char *default_project = cbm_project_name_from_path(canonical_root);- bool custom_project =- project_name[0] && (!default_project || strcmp(default_project, project_name) != 0);- if (encoded && custom_project) {- encoded = yyjson_mut_obj_add_strcpy(document, root, "name", project_name);- }- free(default_project);- char *args = encoded ? yyjson_mut_write(document, 0, NULL) : NULL;- yyjson_mut_doc_free(document);- if (!args) {- return -1;- }- char *project_key = application_index_project_key(canonical_root, args);- if (!project_key) {- free(args);- return -1;- }+ enum {+ APPLICATION_BACKGROUND_INDEX_FAILED = -1,+ APPLICATION_BACKGROUND_INDEX_SUCCESS = 0,+ APPLICATION_BACKGROUND_INDEX_SKIPPED = 1,+ };+ /* Caller holds application->mutex through option encoding and job admission. */+ static void application_watch_encode_index_args_locked(const cbm_daemon_application_watch_t *watch,+ yyjson_mut_doc *document,+ yyjson_mut_val *root, char **args) {+ bool 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;+ }++ static cbm_daemon_application_job_t *application_background_index_admit(+ cbm_daemon_application_t *application, const char *project_name, const char *canonical_root,+ const char *project_key, yyjson_mut_doc *document, yyjson_mut_val *root, char **args,- bool require_live_watch, int *no_job_result) {++ bool require_live_watch, bool *watcher_owns_job, int *no_job_result) {application_job_subscribe_status_t subscribe_status = APPLICATION_JOB_SUBSCRIBE_UNAVAILABLE;++ *watcher_owns_job = false;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 && strcmp(watch->root, canonical_root) == 0 &&- (watch->subscribers > 0 || application->permanent));- !require_live_watch || (watch && watch->subscribers > 0 &&- application_watch_root_matches(watch->root, canonical_root));++ bool watch_live = !require_live_watch ||++ (watch && application_watch_root_matches(watch->root, canonical_root) &&++ (watch->subscribers > 0 || application->permanent));+ if (require_live_watch && watch_live) {+ application_watch_encode_index_args_locked(watch, document, root, args);+ }size_t watch_owner_count = 0;bool watch_subscriptions_ok = true;- bool watcher_owns_job = false;cbm_daemon_application_job_t *job =- watch_live ? application_job_subscribe_locked(application, project_key, canonical_root,- args, &subscribe_status)- : NULL;+ watch_live && *args+ ? application_job_subscribe_locked(application, project_key, canonical_root, *args,+ &subscribe_status)+ : NULL;if (job && require_live_watch) {watch_subscriptions_ok = application_watch_job_subscribe_sessions_locked(application, watch, job, &watch_owner_count);- if (watch_subscriptions_ok && watch_owner_count > 0) {- job->watcher_waiters++;- } else if (!watch_subscriptions_ok) {++ if (!watch_subscriptions_ok) {subscribe_status = APPLICATION_JOB_SUBSCRIBE_ALLOCATION_FAILED;- }- /* application_job_subscribe_locked() lends the caller one ordinary- * subscriber. A watcher callback is only a storage waiter: exact live- * session subscriptions above own the physical work. */- application_job_unsubscribe_locked(job);- if (!watch_subscriptions_ok || watch_owner_count == 0) {+ application_job_unsubscribe_locked(job);+ job = NULL;+ } else if (application->permanent) {+ /*+ * The ordinary subscription acquired above is retained as daemon+ * ownership. This allows the physical watcher to run indexing even+ * while no client session currently owns the project watch.+ */- watcher_owns_job = true;++ *watcher_owns_job = true;+ job->watcher_waiters++;+ } else if (watch_owner_count > 0) {+ /*+ * Historical non-permanent behavior: exact live sessions own the+ * physical work; the watcher callback itself is only a waiter.+ */+ job->watcher_waiters++;+ application_job_unsubscribe_locked(job);+ } else {+ application_job_unsubscribe_locked(job);job = NULL;}}@@@ -3435,14 -4304,21 +4329,21 @@@/* The physical watcher can retain a poll snapshot after the last* owning session unwatches it. Treat that stale callback as a* harmless skip; no job was admitted. */- return 1;+ *no_job_result = APPLICATION_BACKGROUND_INDEX_SKIPPED;+ return NULL;}- return subscribe_status == APPLICATION_JOB_SUBSCRIBE_OPTIONS_CONFLICT ||- subscribe_status == APPLICATION_JOB_SUBSCRIBE_BUSY ||- subscribe_status == APPLICATION_JOB_SUBSCRIBE_CANCELLING- ? 1- : -1;+ *no_job_result = subscribe_status == APPLICATION_JOB_SUBSCRIBE_OPTIONS_CONFLICT ||+ subscribe_status == APPLICATION_JOB_SUBSCRIBE_BUSY ||+ subscribe_status == APPLICATION_JOB_SUBSCRIBE_CANCELLING+ ? APPLICATION_BACKGROUND_INDEX_SKIPPED+ : APPLICATION_BACKGROUND_INDEX_FAILED;}+ return job;+ }++ static int application_background_index_wait(cbm_daemon_application_t *application,+ cbm_daemon_application_job_t *job,- bool require_live_watch) {++ bool require_live_watch, bool watcher_owns_job) {bool successful = false;bool cancelled = false;for (;;) {@@@ -3455,10 -4331,6 +4356,9 @@@if (job->watcher_waiters > 0) {job->watcher_waiters--;}+ if (watcher_owns_job) {+ application_job_unsubscribe_locked(job);- watcher_owns_job = false;+ }} else {application_job_unsubscribe_locked(job);}@@@ -3469,7 -4341,60 +4369,62 @@@cbm_usleep(APPLICATION_JOB_POLL_US);}application_jobs_reap_completed(application);- return successful ? 0 : (cancelled ? 1 : -1);+ if (successful) {+ return APPLICATION_BACKGROUND_INDEX_SUCCESS;+ }+ return cancelled ? APPLICATION_BACKGROUND_INDEX_SKIPPED : APPLICATION_BACKGROUND_INDEX_FAILED;+ }++ static int application_background_index(cbm_daemon_application_t *application,+ const char *project_name, const char *root_path,+ bool require_live_watch) {+ if (!application || !project_name || !root_path) {+ return APPLICATION_BACKGROUND_INDEX_FAILED;+ }+ char canonical_root[APPLICATION_PATH_CAP];+ if (!cbm_canonical_path(root_path, canonical_root, sizeof(canonical_root)) ||+ !application_canonical_directory_exists(canonical_root)) {+ return APPLICATION_BACKGROUND_INDEX_FAILED;+ }+ yyjson_mut_doc *document = yyjson_mut_doc_new(NULL);+ yyjson_mut_val *root = document ? yyjson_mut_obj(document) : NULL;+ if (!document || !root) {+ yyjson_mut_doc_free(document);+ return APPLICATION_BACKGROUND_INDEX_FAILED;+ }+ yyjson_mut_doc_set_root(document, root);+ bool encoded = yyjson_mut_obj_add_strcpy(document, root, "repo_path", canonical_root) &&+ application_index_args_add_policy(application, document, root);+ char *default_project = cbm_project_name_from_path(canonical_root);+ bool custom_project =+ project_name[0] && (!default_project || strcmp(default_project, project_name) != 0);+ if (encoded && custom_project) {+ encoded = yyjson_mut_obj_add_strcpy(document, root, "name", project_name);+ }+ free(default_project);+ char *args = encoded ? yyjson_mut_write(document, 0, NULL) : NULL;+ if (!args) {+ yyjson_mut_doc_free(document);+ return APPLICATION_BACKGROUND_INDEX_FAILED;+ }+ char *project_key = application_index_project_key(canonical_root, args);+ if (!project_key) {+ free(args);+ yyjson_mut_doc_free(document);+ return APPLICATION_BACKGROUND_INDEX_FAILED;+ }++ int no_job_result = APPLICATION_BACKGROUND_INDEX_FAILED;++ bool watcher_owns_job = false;+ cbm_daemon_application_job_t *job = application_background_index_admit(+ application, project_name, canonical_root, project_key, document, root, &args,- require_live_watch, &no_job_result);++ require_live_watch, &watcher_owns_job, &no_job_result);+ free(project_key);+ free(args);+ yyjson_mut_doc_free(document);- return job ? application_background_index_wait(application, job, require_live_watch)++ return job ? application_background_index_wait(application, job, require_live_watch,++ watcher_owns_job)+ : no_job_result;}int cbm_daemon_application_index(cbm_daemon_application_t *application, const char *project_name,diff --cc tests/test_daemon_application.cindex b0b55e4f6,5bb032a5c..67de9002f--- a/tests/test_daemon_application.c+++ b/tests/test_daemon_application.c@@@ -5217,8 -7691,20 +7792,21 @@@ SUITE(daemon_application)RUN_TEST(daemon_application_stale_watcher_callback_is_rejected_at_job_admission);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_permanent_watch_survives_disconnect_and_indexes_without_session);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);+ RUN_TEST(daemon_application_one_shot_cli_index_does_not_register_target_watch);+ RUN_TEST(daemon_application_async_index_interest_survives_cancelled_sync_join);+ RUN_TEST(daemon_application_auto_watch_false_removes_existing_secondary_watches);+ RUN_TEST(daemon_application_async_index_drops_watch_interest_on_requester_disconnect);RUN_TEST(daemon_application_serializes_adr_mutation_with_index_job);RUN_TEST(daemon_application_reserved_mutation_delays_worker_start);RUN_TEST(daemon_application_mutation_does_not_block_other_project);