(
cg: TraceDecay,
scope_prefix: Option<String>,
global_db: Option<Arc<GlobalDb>>,
registry_db: Option<Arc<GlobalDb>>,
allow_default_registry_fallback: bool,
| 836 | } |
| 837 | |
| 838 | pub async fn new_with_dbs( |
| 839 | cg: TraceDecay, |
| 840 | scope_prefix: Option<String>, |
| 841 | global_db: Option<Arc<GlobalDb>>, |
| 842 | registry_db: Option<Arc<GlobalDb>>, |
| 843 | allow_default_registry_fallback: bool, |
| 844 | ) -> Arc<Self> { |
| 845 | let file_token_map = cg.get_file_token_map().await.unwrap_or_default(); |
| 846 | let persisted = cg.get_tokens_saved().await.unwrap_or(0); |
| 847 | let response_handle_project_root = cg.project_root().to_path_buf(); |
| 848 | // Register this project in the global DB with its current tokens |
| 849 | if let Some(ref gdb) = global_db { |
| 850 | gdb.upsert(cg.project_root(), persisted).await; |
| 851 | } |
| 852 | |
| 853 | // Detect borrowed-worktree index once at startup so every read |
| 854 | // tool can cheaply prefix a heads-up. Two git rev-parse spawns |
| 855 | // worst case (#312). spawn_blocking because the underlying |
| 856 | // `Command::output()` can sit on slow disks. |
| 857 | let worktree_mismatch = { |
| 858 | let project_root = cg.project_root().to_path_buf(); |
| 859 | tokio::task::spawn_blocking(move || { |
| 860 | let cwd = std::env::current_dir().ok()?; |
| 861 | crate::worktree::detect_worktree_index_mismatch(&cwd, &project_root) |
| 862 | }) |
| 863 | .await |
| 864 | .ok() |
| 865 | .flatten() |
| 866 | }; |
| 867 | |
| 868 | // Resolve the [sync] config once (D1/D4/D7 all read it). Loading it |
| 869 | // here keeps the per-call read path free of config-file IO. |
| 870 | let sync_config = crate::config::load_sync_config(cg.project_root()); |
| 871 | |
| 872 | let server = Arc::new(Self { |
| 873 | cg: tokio::sync::RwLock::new(Arc::new(cg)), |
| 874 | stats: ServerStats::new(), |
| 875 | method_call_counts: std::sync::Mutex::new(HashMap::new()), |
| 876 | resource_read_counts: std::sync::Mutex::new(HashMap::new()), |
| 877 | tool_call_counts: std::sync::Mutex::new(HashMap::new()), |
| 878 | file_token_map: Arc::new(std::sync::Mutex::new(file_token_map)), |
| 879 | tokens_saved: AtomicU64::new(persisted), |
| 880 | last_flushed_tokens: AtomicU64::new(persisted), |
| 881 | last_flush_at: AtomicI64::new(0), |
| 882 | global_db, |
| 883 | registry_db, |
| 884 | allow_default_registry_fallback, |
| 885 | initialize_root_routing_enabled: AtomicBool::new(true), |
| 886 | hook_project_routes: SharedHookProjectRouteCache::default(), |
| 887 | version_cache: std::sync::Mutex::new(VersionCheckState { |
| 888 | latest: None, |
| 889 | checked_at: None, |
| 890 | }), |
| 891 | pending_notifications: std::sync::Mutex::new(Vec::new()), |
| 892 | scope_prefix, |
| 893 | shutdown_done: AtomicBool::new(false), |
| 894 | timings_enabled: AtomicBool::new(false), |
| 895 | last_staleness_check_at: AtomicI64::new(0), |
nothing calls this directly
no test coverage detected