Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

3.5 KB
Raw Blame
mod activation;
mod agent_capabilities;
mod artifacts;
mod audit;
mod background_tasks;
mod computer;
mod config_sync;
mod data_relocation;
mod db;
mod file_batches;
mod file_resources;
mod keyboard;
mod mcp_servers;
mod mode_runs;
mod network_policy;
mod network_proxy;
mod notifications;
mod permissions;
mod plans;
mod plugin_sessions;
mod plugin_usage;
mod plugins;
mod providers;
mod review;
mod rpc;
mod scheduled;
mod scratch;
mod secrets;
mod session_collaboration;
mod session_search;
mod session_worktrees;
mod sessions;
mod state;
mod todos;
mod tool_budget;
mod tools;
mod transcripts;
mod turn_permissions;
mod turn_queue;
mod user_skills;
mod user_subagents;
mod visualizations;
#[cfg(windows)]
mod windows_relative;
mod workspace;

use std::sync::Arc;
use tokio::sync::Mutex;
use tracing_subscriber::EnvFilter;

use crate::state::AppState;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    if data_relocation::run_cli()? {
        return Ok(());
    }
    if std::env::args().any(|arg| arg == tools::INTERNAL_TOOL_RUNNER_FLAG) {
        let exit_code = match tools::run_internal_tool_runner().await {
            Ok(exit_code) => exit_code,
            Err(error) => {
                eprintln!("internal tool runner failed: {error:#}");
                1
            }
        };
        std::process::exit(exit_code);
    }

    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
        )
        .with_writer(std::io::stderr)
        .init();

    let data_dir = std::env::var("PI_DESKTOP_DATA_DIR")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| {
            dirs::home_dir()
                .unwrap_or_else(|| std::path::PathBuf::from("."))
                .join(".pi-desktop")
        });

    std::fs::create_dir_all(&data_dir)?;
    std::fs::create_dir_all(data_dir.join("logs"))?;
    std::fs::create_dir_all(data_dir.join("plugins/installed"))?;
    std::fs::create_dir_all(data_dir.join("plugins/data"))?;
    std::fs::create_dir_all(data_dir.join("plugins/market"))?;
    std::fs::create_dir_all(data_dir.join("plugins/cache/download"))?;
    std::fs::create_dir_all(data_dir.join("cache"))?;
    std::fs::create_dir_all(data_dir.join("scratch"))?;

    let state = Arc::new(Mutex::new(AppState::open(&data_dir)?));
    tracing::info!(path = %data_dir.display(), "host-core starting");
    {
        // Sweep orphaned/stale session scratch dirs left behind by crashes or
        // deletions that bypassed session.delete (D114).
        let st = state.lock().await;
        // Only sweep with a real session list: an empty fallback on a db
        // error would wipe scratch dirs of sessions that still exist.
        if let Ok(list) = sessions::list_sessions(&st.db) {
            let live: std::collections::HashSet<String> = list.into_iter().map(|s| s.id).collect();
            scratch::sweep(&data_dir, &live);
            // Visualization dirs are swept for orphans only, never by age
            // (D671): live transcripts still reference their cards.
            visualizations::sweep(&data_dir, &live);
            review::sweep(&data_dir, &live);
        }
    }
    // Drop half-created worktrees, mark vanished ones, repair links after a
    // moved data directory, and remove orphans (ADR 0329).
    tokio::spawn(session_worktrees::lifecycle::reconcile(
        state.clone(),
        session_worktrees::git::GitRunner::default(),
    ));
    rpc::serve(state).await
}