MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / host_state_prepare

Function host_state_prepare

src/daemon/host.c:582–643  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

580}
581
582static bool host_state_prepare(host_state_t *host, const cbm_daemon_ipc_endpoint_t *endpoint) {
583 host->http_ops = &g_host_http_default_ops;
584 host->http_assets_available = CBM_EMBEDDED_FILE_COUNT > 0;
585 if (!cbm_secure_random(host->ui_readiness_secret, sizeof(host->ui_readiness_secret))) {
586 cbm_log_error("daemon.readiness_secret_failed", "reason", "system_rng_unavailable");
587 return false;
588 }
589 size_t aggregate_memory_budget_bytes = cbm_mem_budget();
590 const char *cache = cbm_resolve_cache_dir();
591 if (!cache || !cache[0]) {
592 cbm_log_error("daemon.runtime_config_open_failed", "reason", "cache_dir_unavailable");
593 return false;
594 }
595 host->runtime_config = cbm_config_open(cache);
596 if (!host->runtime_config) {
597 cbm_log_error("daemon.runtime_config_open_failed", "reason", "config_db_unavailable");
598 return false;
599 }
600 host->project_locks = cbm_project_lock_manager_new(endpoint);
601 /* #335: watcher_enabled (default true) is the master switch for the
602 * background watcher. When false the daemon never builds the watcher or the
603 * in-memory store backing it, so the poll thread never starts
604 * (host_background_start has nothing to run) and no project ever registers
605 * (register_watcher_if_enabled early-returns on a NULL watcher). The daemon
606 * still starts and still owns everything else: IPC, HTTP/UI, project locks,
607 * and manual index_repository. Read once here at daemon startup, so a change
608 * takes effect when the daemon next starts — see docs/CONFIGURATION.md. */
609 bool watcher_enabled = cbm_config_watcher_enabled(host->runtime_config);
610 if (watcher_enabled) {
611 host->watch_store = cbm_store_open_memory();
612 host->watcher = cbm_watcher_new(host->watch_store, host_watcher_index, host);
613 /* #1948: non-git roots stay unwatched unless watch_non_git opts in.
614 * Same read-once-at-startup contract as watcher_enabled. */
615 if (host->watcher && cbm_config_watch_non_git(host->runtime_config)) {
616 cbm_watcher_set_poll_non_git(host->watcher, true);
617 cbm_log_info("watcher.non_git", "mode", "tree_poll");
618 }
619 } else {
620 cbm_log_info("watcher.disabled", "reason", "config");
621 }
622 cbm_daemon_application_config_t application_config = {
623 .watcher = host->watcher,
624 .config = host->runtime_config,
625 .aggregate_memory_budget_bytes = aggregate_memory_budget_bytes,
626 .project_locks = host->project_locks,
627 .ui_readiness_secret = host->ui_readiness_secret,
628 .ui_readiness_secret_length = sizeof(host->ui_readiness_secret),
629 };
630 host->application = cbm_daemon_application_new(&application_config);
631 if (host->application && host->permanent) {
632 cbm_daemon_application_set_permanent(host->application, true);
633 }
634 if (!host->project_locks || !host->application) {
635 return false;
636 }
637 /* A watcher deliberately left unbuilt by watcher_enabled=false is not a
638 * startup failure; an allocation failure while it is enabled still is. */
639 if (watcher_enabled && (!host->watch_store || !host->watcher)) {

Callers 2

cbm_daemon_host_runFunction · 0.85

Calls 12

cbm_secure_randomFunction · 0.85
cbm_mem_budgetFunction · 0.85
cbm_resolve_cache_dirFunction · 0.85
cbm_config_openFunction · 0.85
cbm_store_open_memoryFunction · 0.85
cbm_watcher_newFunction · 0.85
cbm_config_watch_non_gitFunction · 0.85

Tested by

no test coverage detected