Background auto-index thread function */
| 5872 | |
| 5873 | /* Background auto-index thread function */ |
| 5874 | static void *autoindex_thread(void *arg) { |
| 5875 | cbm_mcp_server_t *srv = (cbm_mcp_server_t *)arg; |
| 5876 | |
| 5877 | cbm_log_info("autoindex.start", "project", srv->session_project, "path", srv->session_root); |
| 5878 | |
| 5879 | /* #832: prefer the supervised worker subprocess. Indexing the whole session in |
| 5880 | * this long-lived server thread ratchets RSS (mimalloc v3 does not reclaim the |
| 5881 | * pages worker threads abandon at exit); running it in a child that exits hands |
| 5882 | * 100% of that memory back to the OS every cycle. Degrade to the in-process |
| 5883 | * pipeline below when the supervisor is off (kill switch) or the spawn fails. */ |
| 5884 | if (cbm_index_supervisor_should_wrap()) { |
| 5885 | char *resp = index_run_supervised_path(srv, srv->session_root); |
| 5886 | if (resp) { |
| 5887 | free(resp); |
| 5888 | cbm_log_info("autoindex.done", "project", srv->session_project, "mode", "supervised"); |
| 5889 | /* Register with watcher for ongoing change detection — gated on |
| 5890 | * auto_watch (#849), same as the in-process branch below. A bare |
| 5891 | * `if (srv->watcher)` would register even when the user set |
| 5892 | * `config set auto_watch false`, since srv->watcher is always set. */ |
| 5893 | register_watcher_if_enabled(srv); |
| 5894 | return NULL; |
| 5895 | } |
| 5896 | /* resp == NULL → spawn-failure degrade → fall through to in-process. */ |
| 5897 | } |
| 5898 | |
| 5899 | cbm_pipeline_t *p = cbm_pipeline_new(srv->session_root, NULL, CBM_MODE_FULL); |
| 5900 | if (!p) { |
| 5901 | cbm_log_warn("autoindex.err", "msg", "pipeline_create_failed"); |
| 5902 | return NULL; |
| 5903 | } |
| 5904 | |
| 5905 | /* Block until any concurrent pipeline finishes */ |
| 5906 | cbm_pipeline_lock(); |
| 5907 | int rc = cbm_pipeline_run(p); |
| 5908 | cbm_pipeline_unlock(); |
| 5909 | |
| 5910 | cbm_pipeline_free(p); |
| 5911 | cbm_mem_collect(); /* return mimalloc pages to OS after indexing (in-process only) */ |
| 5912 | |
| 5913 | if (rc == 0) { |
| 5914 | cbm_log_info("autoindex.done", "project", srv->session_project); |
| 5915 | register_watcher_if_enabled(srv); |
| 5916 | } else { |
| 5917 | cbm_log_warn("autoindex.err", "msg", "pipeline_run_failed"); |
| 5918 | } |
| 5919 | return NULL; |
| 5920 | } |
| 5921 | |
| 5922 | /* Start auto-indexing if configured and project not yet indexed. */ |
| 5923 | static void maybe_auto_index(cbm_mcp_server_t *srv) { |
nothing calls this directly
no test coverage detected