Skip to main content

oxi/
lib.rs

1#![warn(missing_docs)]
2// Relax two test-idiom lints under `cfg(test)` so `cargo clippy --all-targets`
3// stays clean without weakening the shipped library:
4//   - `clippy::unwrap_used` — `unwrap()`/`unwrap_err()` are idiomatic in tests;
5//     shipped (non-test) code still `warn`s on it (see the line below).
6//   - `clippy::field_reassign_with_default` — the `let mut x = X::default();
7//     x.f = ..;` test-setup pattern.
8#![warn(clippy::unwrap_used)]
9#![cfg_attr(test, allow(clippy::unwrap_used, clippy::field_reassign_with_default))]
10#![allow(unknown_lints)]
11
12//! oxi: CLI coding harness
13//!
14//! This crate provides the main application logic for the oxi CLI.
15
16// ─── Root-level entry modules ───────────────────────────────────────────────
17// cli must be pub for main.rs binary
18pub mod bootstrap;
19pub mod cli;
20pub mod internal_urls;
21pub mod main_dispatch;
22pub mod mcp_credentials;
23pub mod print_mode;
24pub mod services;
25pub mod setup_wizard;
26pub mod store;
27
28// ─── Directory groups ───────────────────────────────────────────────────────
29pub(crate) mod app;
30pub(crate) mod context;
31pub mod discovery;
32pub mod extensions; // public for main.rs
33pub(crate) mod infra;
34pub(crate) mod media;
35pub(crate) mod prompt;
36pub(crate) mod rpc_mode;
37pub(crate) mod skills;
38pub mod storage; // public for main.rs (packages)
39// Re-exports from storage for main.rs
40pub use storage::packages::PackageManager;
41pub use storage::packages::ResourceKind;
42pub mod tools;
43pub mod tui; // public for main.rs
44pub(crate) mod ui;
45pub(crate) mod util;
46
47///
48/// This is the **new entry point** for oxi-cli run modes. It uses
49/// `oxi-fs` adapters and `OxiBuilder::with_port_*` to construct an
50/// `Oxi` with persistence, auth, config, and skills wired. The legacy
51/// `App::new` path is still used by the interactive TUI during the
52/// migration period.
53///
54/// # Example
55///
56/// ```no_run
57/// use oxi::build_oxi_engine;
58/// # async fn _example() -> anyhow::Result<()> {
59/// let oxi = build_oxi_engine().await?;
60/// println!("providers: {}", oxi.providers().names().len());
61/// # Ok(()) }
62/// ```
63pub async fn build_oxi_engine() -> anyhow::Result<oxi_sdk::Oxi> {
64    let paths = services::OxiPaths::default_paths()?;
65    services::build_oxi(&paths).await
66}
67
68/// Self-check the wired port implementations. Prints a one-line summary
69/// per port and returns `Ok(())` if all are reachable.
70///
71/// Triggered by the `OXI_PORT_CHECK=1` environment variable from
72/// `oxi-cli/src/main.rs`. Useful for verifying the new composition root
73/// without disturbing the legacy `App::new` path.
74pub async fn run_port_check() -> anyhow::Result<()> {
75    let oxi = build_oxi_engine().await?;
76    let ports = oxi.ports();
77
78    // State
79    let entries = ports.state.list("").await?;
80    println!("[state]    entries: {}", entries.len());
81
82    // Auth
83    let providers = ports.auth.list_providers().await?;
84    println!("[auth]     providers with credentials: {:?}", providers);
85
86    // Config
87    let keys = ports.config.list()?;
88    println!("[config]   keys: {}", keys.len());
89
90    // Skills
91    let skills = ports.skills.list().await?;
92    println!("[skills]   {} skill(s) discovered", skills.len());
93    for s in &skills {
94        println!("           - {}: {}", s.name, s.description);
95    }
96
97    // Event bus / memory / etc — all noop unless registered
98    let _ = ports
99        .event_bus
100        .publish(&"port-check".to_string(), serde_json::json!({"ok": true}))
101        .await;
102    println!("[event-bus] publish ok (noop bus if not registered)");
103
104    println!("\nport check: ok");
105    Ok(())
106}
107
108/// Context for compaction operations, passed to extension hooks
109#[derive(Debug, Clone)]
110pub struct CompactionContext {
111    /// Messages being compacted
112    pub messages_count: usize,
113    /// Estimated tokens before compaction
114    pub tokens_before: usize,
115    /// Target token count after compaction
116    pub target_tokens: usize,
117    /// Strategy being used
118    pub strategy: String,
119}
120
121impl CompactionContext {
122    /// Create a new compaction context
123    pub fn new(
124        messages_count: usize,
125        tokens_before: usize,
126        target_tokens: usize,
127        strategy: impl Into<String>,
128    ) -> Self {
129        Self {
130            messages_count,
131            tokens_before,
132            target_tokens,
133            strategy: strategy.into(),
134        }
135    }
136
137    /// Get expected compression ratio
138    pub fn compression_ratio(&self) -> f32 {
139        if self.tokens_before == 0 {
140            return 1.0;
141        }
142        self.target_tokens as f32 / self.tokens_before as f32
143    }
144}
145
146// ─── Module-level imports ────────────────────────────────────────────────────
147use crate::store::settings::Settings;
148use anyhow::{Error, Result};
149use oxi_agent::{Agent, AgentConfig, AgentEvent};
150use parking_lot::RwLock;
151use skills::SkillManager;
152use std::sync::Arc;
153
154// ─── Application state ───────────────────────────────────────────────────────
155
156/// Application state and entry point.
157///
158/// Holds an `Oxi` engine (composition root) and a single `Agent` built
159/// from it. The legacy `App::new(settings)` constructor is **gone**;
160/// use [`App::from_oxi`] with a wired `Oxi` from
161/// [`build_oxi_engine`].
162pub struct App {
163    oxi: oxi_sdk::Oxi,
164    agent: Arc<Agent>,
165    settings: Settings,
166    skills: RwLock<SkillManager>,
167    active_skills: RwLock<Vec<String>>,
168    wasm_ext: Option<std::sync::Arc<crate::extensions::WasmExtensionManager>>,
169    questionnaire_bridge:
170        Option<std::sync::Arc<oxi_agent::tools::questionnaire::QuestionnaireBridge>>,
171    /// Shared local issue store (`.oxi/issues/`). Cloned cheaply (inner `Arc`).
172    /// Used by the agent `issue` tool, the TUI indicator, and the `oxi issue`
173    /// CLI subcommand.
174    issue_store: Option<crate::store::issues::FileIssueStore>,
175    /// Process-wide liveness identity used by every issue-ownership surface
176    /// in this process (agent tool's `ToolContext.session_id`, TUI panel,
177    /// slash-command `/issue` handlers). See
178    /// [`crate::store::issues::liveness::TUI_OWNERSHIP_ID`] for the TUI value.
179    ownership_session_id: String,
180    /// Alive-lock held for the lifetime of `App`. Dropped with `App`, releasing
181    /// the OS-held flock so any other process sees this session as dead once
182    /// we exit (including `kill -9` / crash / normal exit). Only held when
183    /// `issue_store` is available.
184    #[allow(dead_code)]
185    liveness_guard: Option<crate::store::issues::liveness::AliveGuard>,
186}
187
188/// Context for compaction operations, passed to extension hooks
189// ─── System prompt builder ───────────────────────────────────────────────────
190fn build_system_prompt(
191    thinking_level: crate::store::settings::ThinkingLevel,
192    skill_contents: &[String],
193) -> String {
194    let skills: Vec<prompt::system_prompt::Skill> = skill_contents
195        .iter()
196        .enumerate()
197        .map(|(i, content)| prompt::system_prompt::Skill {
198            name: format!("skill-{}", i),
199            content: content.clone(),
200        })
201        .collect();
202
203    let options = prompt::system_prompt::BuildSystemPromptOptions {
204        custom_prompt: prompt::system_prompt::thinking_level_prompt(thinking_level),
205        skills,
206        cwd: std::env::current_dir()
207            .map(|p| p.to_string_lossy().to_string())
208            .unwrap_or_default(),
209        ..Default::default()
210    };
211
212    prompt::system_prompt::build_system_prompt(&options)
213}
214
215// ─── App implementation ─────────────────────────────────────────────────────
216
217impl App {
218    /// Build an `App` from a wired `Oxi` engine and a settings object.
219    ///
220    /// The `Oxi` should be created via [`build_oxi_engine`] (or
221    /// `services::build_oxi`) so that all 11 ports are wired. The
222    /// settings hold the user's runtime configuration (model, thinking
223    /// level, etc.).
224    ///
225    /// `ownership_session_id` is the per-process liveness identity used by
226    /// the agent's `issue` tool (`ToolContext.session_id`), the TUI panel,
227    /// and the `/issue` slash command. In TUI mode this MUST equal
228    /// [`crate::store::issues::liveness::TUI_OWNERSHIP_ID`] so the panel and
229    /// agent see the same flock holder. In print / RPC mode, a stable
230    /// process-scoped id (e.g. `proc-<pid>-<uuid>`) is appropriate.
231    /// When `issue_store` is available, an `flock` is acquired under this
232    /// id for the lifetime of the returned `App`.
233    pub async fn from_oxi(
234        oxi: oxi_sdk::Oxi,
235        settings: Settings,
236        ownership_session_id: String,
237    ) -> Result<Self> {
238        let model_id = settings.effective_model(None).unwrap_or_default();
239        let provider_name = settings
240            .effective_provider(None)
241            .unwrap_or_else(|| model_id.split('/').next().unwrap_or("").to_string());
242
243        // Pull the API key from the wired port, not from oxi_store.
244        let api_key = oxi.ports().auth.get_api_key(&provider_name).await?;
245
246        let skills_dir = SkillManager::skills_dir().unwrap_or_else(|_| {
247            dirs::home_dir()
248                .unwrap_or_default()
249                .join(".oxi")
250                .join("skills")
251        });
252        let skills = SkillManager::load_from_dir(&skills_dir).unwrap_or_else(|e| {
253            tracing::debug!("Skills not loaded: {}", e);
254            SkillManager::new()
255        });
256
257        let system_prompt = build_system_prompt(settings.thinking_level, &[]);
258        let compaction_strategy = if settings.auto_compaction {
259            oxi_sdk::CompactionStrategy::Threshold(0.8)
260        } else {
261            oxi_sdk::CompactionStrategy::Disabled
262        };
263
264        let config = AgentConfig {
265            name: "oxi".to_string(),
266            description: Some("oxi CLI agent".to_string()),
267            model_id: model_id.clone(),
268            system_prompt: Some(system_prompt),
269            timeout_seconds: settings.tool_timeout_seconds,
270            temperature: settings.effective_temperature(),
271            max_tokens: settings.effective_max_tokens(),
272            compaction_strategy,
273            compaction_instruction: None,
274            context_window: 128_000,
275            api_key,
276            workspace_dir: Some(
277                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
278            ),
279            output_mode: None,
280            provider_options: None,
281            session_id: Some(ownership_session_id.clone()),
282            ttsr_engine: None,
283            memory: None,
284            todo: None,
285            agent_pool: None,
286        };
287
288        // Build the agent via the SDK's AgentBuilder — no manual wiring.
289        let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
290        let agent = oxi
291            .agent(config)
292            .workspace(cwd)
293            .build()
294            .map_err(|e| Error::msg(format!("agent build failed: {e}")))?;
295        let agent = Arc::new(agent);
296
297        let questionnaire_timeout = if settings.questionnaire_timeout_secs > 0 {
298            Some(std::time::Duration::from_secs(
299                settings.questionnaire_timeout_secs,
300            ))
301        } else {
302            None
303        };
304        let bridge = std::sync::Arc::new(
305            oxi_agent::tools::questionnaire::QuestionnaireBridge::with_timeout(
306                questionnaire_timeout,
307            ),
308        );
309        let questionnaire_tool =
310            oxi_agent::tools::questionnaire::QuestionnaireTool::new(bridge.clone());
311        agent
312            .tools()
313            .register_arc(std::sync::Arc::new(questionnaire_tool));
314
315        // Open the local issue store rooted at the project (`.oxi/issues/`).
316        // Best-effort: if the directory cannot be resolved, issues are simply
317        // unavailable — the app still works without them. The `/issue` slash
318        // command surfaces a clear error in that case.
319        let issue_store = std::env::current_dir()
320            .ok()
321            .map(|cwd| crate::store::issues::FileIssueStore::open_from_cwd(&cwd))
322            .and_then(|r| {
323                r.map_err(|e| tracing::warn!("issue store unavailable: {e}"))
324                    .ok()
325            });
326
327        // Register the `issue` agent tool when the store is available.
328        if let Some(store) = issue_store.clone() {
329            let tool = std::sync::Arc::new(crate::tools::IssueTool::new(store));
330            agent.tools().register_arc(tool);
331        }
332
333        Ok(Self {
334            oxi,
335            agent,
336            settings,
337            skills: RwLock::new(skills),
338            active_skills: RwLock::new(Vec::new()),
339            wasm_ext: None,
340            questionnaire_bridge: Some(bridge),
341            issue_store,
342            ownership_session_id,
343            liveness_guard: None, // set below once issue_store is known
344        })
345        .map(|mut app| {
346            // Acquire the process-wide liveness flock now that issue_store exists.
347            // Best-effort: another live process already holds the lock is non-fatal;
348            // we still expose ownership_session_id so callers can detect the conflict.
349            app.liveness_guard =
350                acquire_ownership_guard(app.issue_store.as_ref(), &app.ownership_session_id);
351            app
352        })
353    }
354
355    /// Per-process liveness identity. Used by the agent's `issue` tool and any
356    /// other surface that gates on `is_session_alive`.
357    pub fn ownership_session_id(&self) -> &str {
358        &self.ownership_session_id
359    }
360
361    /// True iff `App` holds a live liveness flock under `ownership_session_id`.
362    /// False when there is no `issue_store` (e.g. headless test) or when another
363    /// live process already holds the lock (the assignment feature will surface
364    /// `Assigned` errors in that case — by design).
365    pub fn has_liveness_lock(&self) -> bool {
366        self.liveness_guard.is_some()
367    }
368
369    /// Get the current settings
370    pub fn settings(&self) -> &Settings {
371        &self.settings
372    }
373
374    /// Set the WASM extension manager
375    pub fn set_wasm_ext(
376        &mut self,
377        ext: Option<std::sync::Arc<crate::extensions::WasmExtensionManager>>,
378    ) {
379        self.wasm_ext = ext;
380    }
381
382    /// Get the WASM extension manager
383    pub fn wasm_ext(&self) -> Option<&std::sync::Arc<crate::extensions::WasmExtensionManager>> {
384        self.wasm_ext.as_ref()
385    }
386
387    /// Get a clone of the local issue store, if one was opened successfully.
388    pub fn issue_store(&self) -> Option<crate::store::issues::FileIssueStore> {
389        self.issue_store.clone()
390    }
391
392    /// Get a reference to the underlying `Oxi` engine. The catalog port and
393    /// other ports are accessible through it.
394    pub fn oxi(&self) -> &oxi_sdk::Oxi {
395        &self.oxi
396    }
397
398    /// Get a reference to the underlying agent.
399    pub fn agent(&self) -> Arc<Agent> {
400        Arc::clone(&self.agent)
401    }
402
403    /// Get the tool registry (for registering extension tools)
404    pub fn agent_tools(&self) -> Arc<oxi_agent::ToolRegistry> {
405        self.agent.tools()
406    }
407
408    /// Get the questionnaire bridge, if initialized.
409    pub fn questionnaire_bridge(
410        &self,
411    ) -> Option<&std::sync::Arc<oxi_agent::tools::questionnaire::QuestionnaireBridge>> {
412        self.questionnaire_bridge.as_ref()
413    }
414
415    /// Get a reference to the skill manager
416    pub fn skills(&self) -> parking_lot::RwLockReadGuard<'_, SkillManager> {
417        self.skills.read()
418    }
419
420    /// Activate a skill by name. Returns an error string if not found.
421    pub fn activate_skill(&self, name: &str) -> Result<(), String> {
422        {
423            let skills = self.skills.read();
424            if skills.get(name).is_none() {
425                return Err(format!("Skill '{}' not found", name));
426            }
427        }
428        let name_lower = name.to_lowercase();
429        {
430            let mut active = self.active_skills.write();
431            if !active.contains(&name_lower) {
432                active.push(name_lower);
433            }
434        }
435        self.rebuild_system_prompt();
436        Ok(())
437    }
438
439    /// Deactivate a skill by name.
440    pub fn deactivate_skill(&self, name: &str) {
441        let name_lower = name.to_lowercase();
442        {
443            let mut active = self.active_skills.write();
444            active.retain(|n| n != &name_lower);
445        }
446        self.rebuild_system_prompt();
447    }
448
449    /// List currently active skill names
450    pub fn active_skills(&self) -> Vec<String> {
451        self.active_skills.read().clone()
452    }
453
454    /// Rebuild the system prompt with current active skills
455    fn rebuild_system_prompt(&self) {
456        let active = self.active_skills.read();
457        let skills = self.skills.read();
458        let contents: Vec<String> = active
459            .iter()
460            .filter_map(|name| skills.get(name).map(|s| s.content.clone()))
461            .collect();
462        let prompt = build_system_prompt(self.settings.thinking_level, &contents);
463        self.agent.set_system_prompt(prompt);
464    }
465
466    /// Get a clone of the current state
467    pub fn agent_state(&self) -> oxi_agent::AgentState {
468        self.agent.state()
469    }
470
471    /// Run a single prompt and return the response
472    pub async fn run_prompt(&self, prompt: String) -> Result<String> {
473        let (response, _events) = self.agent.run(prompt).await?;
474        Ok(response.content)
475    }
476
477    /// Run a prompt with event callback
478    pub async fn run_prompt_with_events<F>(&self, prompt: String, on_event: F) -> Result<String>
479    where
480        F: FnMut(AgentEvent) + Send + 'static,
481    {
482        self.agent.run_streaming(prompt, on_event).await?;
483        let state = self.agent_state();
484        for msg in state.messages.iter().rev() {
485            if let oxi_sdk::Message::Assistant(a) = msg {
486                return Ok(a.text_content());
487            }
488        }
489        Ok(String::new())
490    }
491
492    /// Reset the conversation
493    pub fn reset(&self) {
494        self.agent.reset();
495    }
496
497    /// Switch the model used for future LLM calls.
498    pub async fn switch_model(&self, model_id: &str) -> anyhow::Result<()> {
499        let parts: Vec<&str> = model_id.split('/').collect();
500        let provider = parts
501            .first()
502            .map(|s| s.to_string())
503            .unwrap_or_else(|| "anthropic".to_string());
504        let api_key = self.oxi.ports().auth.get_api_key(&provider).await?;
505        let _ = self.agent.switch_model(model_id, api_key);
506        Ok(())
507    }
508
509    /// Get the current model ID
510    pub fn model_id(&self) -> String {
511        self.agent.model_id()
512    }
513}
514
515/// Acquire the process-wide liveness flock for `ownership_id` under the issue
516/// store's `.alive/` directory.
517///
518/// Returns `None` (no lock) when there is no issue store or when another live
519/// process already holds the lock — both non-fatal; the caller can still read
520/// `ownership_session_id` and the assignment feature will surface `Assigned`
521/// errors if contention actually occurs.
522///
523/// Extracted from `App::from_oxi` so the single-lock invariant (defect #13 fix)
524/// can be unit-tested without standing up a full `Oxi` engine.
525pub(crate) fn acquire_ownership_guard(
526    issue_store: Option<&crate::store::issues::FileIssueStore>,
527    ownership_id: &str,
528) -> Option<crate::store::issues::liveness::AliveGuard> {
529    let store = issue_store?;
530    if ownership_id.is_empty() {
531        // Defensive: never hold a lock under the empty string — that was the
532        // #13 bug shape (empty owner is never alive, so ownership was bypassed).
533        return None;
534    }
535    crate::store::issues::liveness::acquire(&store.issues_dir(), ownership_id).ok()
536}
537
538#[cfg(test)]
539mod tests {
540    //! P0 regression: `App` must hold exactly one liveness flock under its
541    //! ownership identity. We test the extracted `acquire_ownership_guard`
542    //! helper (the single chokepoint `from_oxi` delegates to) rather than
543    //! standing up a full `Oxi` engine.
544    use super::*;
545    use crate::store::issues::FileIssueStore;
546    use crate::store::issues::liveness;
547
548    fn tmp_store() -> (tempfile::TempDir, FileIssueStore) {
549        let tmp = tempfile::tempdir().unwrap();
550        let dir = tmp.path().join(".oxi").join("issues");
551        std::fs::create_dir_all(&dir).unwrap();
552        (tmp, FileIssueStore::open(dir).unwrap())
553    }
554
555    #[test]
556    fn app_holds_single_liveness_lock() {
557        // The #13 invariant: acquiring the ownership guard makes the session
558        // live under that identity, and a second acquire under the SAME id
559        // fails (one flock per identity — single lock).
560        let (_tmp, store) = tmp_store();
561        let dir = store.issues_dir();
562        let id = "proc-test-app";
563
564        let guard = acquire_ownership_guard(Some(&store), id);
565        assert!(
566            guard.is_some(),
567            "App must acquire the liveness lock for its ownership id"
568        );
569        assert!(
570            liveness::is_session_alive(&dir, id),
571            "after acquire, the session must be live"
572        );
573
574        // While held, the same identity cannot be acquired again — single lock.
575        let second = liveness::acquire(&dir, id);
576        assert!(second.is_err(), "second acquire under same id must fail");
577
578        drop(guard);
579        assert!(
580            !liveness::is_session_alive(&dir, id),
581            "dropping App's guard releases the lock"
582        );
583    }
584
585    #[test]
586    fn acquire_returns_none_without_store() {
587        // No issue store (headless/test) → no lock. Not an error.
588        let dir = tempfile::tempdir().unwrap();
589        let id = "proc-x";
590        assert!(acquire_ownership_guard(None, id).is_none());
591        let _ = dir; // no store created
592    }
593
594    #[test]
595    fn acquire_rejects_empty_ownership_id() {
596        // Defensive guard against the #13 bug shape: never hold a lock under
597        // the empty string (it's never alive, so ownership would be bypassed).
598        let (_tmp, store) = tmp_store();
599        assert!(
600            acquire_ownership_guard(Some(&store), "").is_none(),
601            "empty ownership id must never acquire a lock (#13 guard)"
602        );
603    }
604}