Skip to main content

oxicode/
lib.rs

1// oxicode: CLI coding harness
2// Migrating to oxicode-vtui: relaxed linting for vendored code compatibility.
3#![allow(
4    missing_docs,
5    dead_code,
6    clippy::field_reassign_with_default,
7    clippy::unwrap_used,
8    clippy::let_and_return,
9    clippy::borrow_interior_mutable_const,
10    clippy::derivable_impls,
11    clippy::new_without_default,
12    unknown_lints
13)]
14//! oxicode: CLI coding harness
15//!
16//! This crate provides the main application logic for the oxicode CLI.
17
18// ─── Root-level entry modules ───────────────────────────────────────────────
19// cli must be pub for main.rs binary
20pub mod bootstrap;
21pub mod cli;
22pub mod internal_urls;
23pub mod lsp;
24pub mod main_dispatch;
25pub mod mcp_credentials;
26pub mod oauth_listener;
27pub mod oauth_refresh;
28pub mod print_mode;
29pub mod provider_oauth;
30pub mod services;
31pub mod setup_wizard;
32pub mod store;
33
34// ─── Directory groups ───────────────────────────────────────────────────────
35pub(crate) mod app;
36pub(crate) mod context;
37pub mod discovery;
38pub mod extensions; // public for main.rs
39pub(crate) mod infra;
40pub(crate) mod media;
41pub(crate) mod prompt;
42pub mod rpc_mode;
43pub(crate) mod skills;
44pub mod storage; // public for main.rs (packages)
45// Re-exports from storage for main.rs
46pub use storage::packages::PackageManager;
47pub use storage::packages::ResourceKind;
48pub mod tools;
49pub(crate) mod ui;
50pub(crate) mod util;
51
52///
53/// This is the **new entry point** for oxicode-cli run modes. It uses
54/// `oxicode-fs` adapters and `OxicodeBuilder::with_port_*` to construct an
55/// `Oxicode` with persistence, auth, config, and skills wired. The legacy
56/// `App::new` path is still used by the interactive TUI during the
57/// migration period.
58///
59/// ```
60/// use oxicode::build_oxicode_engine;
61/// # async fn _example() -> anyhow::Result<()> {
62/// let oxicode = build_oxicode_engine(None, None).await?;
63/// println!("providers: {}", oxicode.providers().names().len());
64/// # Ok(()) }
65/// ```
66pub async fn build_oxicode_engine(
67    embedding_provider: Option<std::sync::Arc<dyn oxicode_sdk::ports::EmbeddingProvider>>,
68    hook_runner: Option<std::sync::Arc<dyn oxicode_sdk::ports::HookRunner>>,
69) -> anyhow::Result<oxicode_sdk::Oxicode> {
70    let paths = services::OxicodePaths::default_paths()?;
71    services::build_oxicode(&paths, embedding_provider, hook_runner).await
72}
73
74/// Self-check the wired port implementations. Prints a one-line summary
75/// per port and returns `Ok(())` if all are reachable.
76///
77/// Triggered by the `OXICODE_PORT_CHECK=1` environment variable from
78/// `oxicode-cli/src/main.rs`. Useful for verifying the new composition root
79/// without disturbing the legacy `App::new` path.
80pub async fn run_port_check() -> anyhow::Result<()> {
81    let oxicode = build_oxicode_engine(None, None).await?;
82    let ports = oxicode.ports();
83
84    let entries = ports.state.list("").await?;
85    println!("[state]    entries: {}", entries.len());
86
87    // Auth
88    let providers = ports.auth.list_providers().await?;
89    println!("[auth]     providers with credentials: {:?}", providers);
90
91    // Config
92    let keys = ports.config.list()?;
93    println!("[config]   keys: {}", keys.len());
94
95    // Skills
96    let skills = ports.skills.list().await?;
97    println!("[skills]   {} skill(s) discovered", skills.len());
98    for s in &skills {
99        println!("           - {}: {}", s.name, s.description);
100    }
101
102    // Event bus / memory / etc — all noop unless registered
103    let _ = ports
104        .event_bus
105        .publish(&"port-check".to_string(), serde_json::json!({"ok": true}))
106        .await;
107    println!("[event-bus] publish ok (noop bus if not registered)");
108
109    println!("\nport check: ok");
110    Ok(())
111}
112
113/// Context for compaction operations, passed to extension hooks
114#[derive(Debug, Clone)]
115pub struct CompactionContext {
116    /// Messages being compacted
117    pub messages_count: usize,
118    /// Estimated tokens before compaction
119    pub tokens_before: usize,
120    /// Target token count after compaction
121    pub target_tokens: usize,
122    /// Strategy being used
123    pub strategy: String,
124}
125
126impl CompactionContext {
127    /// Create a new compaction context
128    pub fn new(
129        messages_count: usize,
130        tokens_before: usize,
131        target_tokens: usize,
132        strategy: impl Into<String>,
133    ) -> Self {
134        Self {
135            messages_count,
136            tokens_before,
137            target_tokens,
138            strategy: strategy.into(),
139        }
140    }
141
142    /// Get expected compression ratio
143    pub fn compression_ratio(&self) -> f32 {
144        if self.tokens_before == 0 {
145            return 1.0;
146        }
147        self.target_tokens as f32 / self.tokens_before as f32
148    }
149}
150
151// ─── Module-level imports ────────────────────────────────────────────────────
152use crate::store::settings::Settings;
153use anyhow::{Error, Result};
154use oxicode_agent::{Agent, AgentConfig, AgentEvent};
155use parking_lot::RwLock;
156use skills::SkillManager;
157use std::collections::VecDeque;
158use std::sync::Arc;
159
160/// Pre-built session state threaded into the agent hook chain.
161///
162/// Constructed by the cli BEFORE `oxicode.agent(...).build()` so the
163/// middleware pipeline (`with_port_hooks`) and session closures
164/// (`with_session_hooks`) are composed into a single `AgentHooks`
165/// instance — see the single-`set_hooks` invariant (only
166/// [`AgentBuilder::build`](oxicode_sdk::AgentBuilder::build) calls
167/// `set_hooks`). The same `Arc`s are cloned into `AgentSession` so the
168/// runtime queues, stop flag, and agent hooks all observe the same
169/// state across teardown/recreate cycles.
170#[derive(Clone)]
171pub struct SessionState {
172    /// Set when the user (or `Ctrl+C` handler) requests the agent to
173    /// stop after the current turn. Consulted by
174    /// [`oxicode_sdk::agent_builder::SessionHookClosures::should_stop_after_turn`].
175    pub should_stop: Arc<std::sync::atomic::AtomicBool>,
176    /// Steering messages — drained at the start of each turn (until empty).
177    pub steering: Arc<RwLock<VecDeque<oxicode_sdk::Message>>>,
178    /// Follow-up messages — drained after the agent has stopped.
179    pub follow_up: Arc<RwLock<VecDeque<oxicode_sdk::Message>>>,
180}
181
182impl Default for SessionState {
183    fn default() -> Self {
184        Self {
185            should_stop: Arc::new(std::sync::atomic::AtomicBool::new(false)),
186            steering: Arc::new(RwLock::new(VecDeque::new())),
187            follow_up: Arc::new(RwLock::new(VecDeque::new())),
188        }
189    }
190}
191
192// ─── Application state ───────────────────────────────────────────────────────
193
194/// Holds an `Oxicode` engine (composition root) and a single `Agent` built
195/// from it. The legacy `App::new(settings)` constructor is **gone**;
196/// use [`App::from_oxicode`] with a wired `Oxicode` from
197/// [`build_oxicode_engine`].
198pub struct App {
199    oxicode: oxicode_sdk::Oxicode,
200    agent: Arc<Agent>,
201    settings: Settings,
202    skills: RwLock<SkillManager>,
203    active_skills: RwLock<Vec<String>>,
204    wasm_ext: Option<std::sync::Arc<crate::extensions::WasmExtensionManager>>,
205    ask_bridge: Option<std::sync::Arc<oxicode_agent::tools::ask::AskBridge>>,
206    /// Shared local issue store (`.oxicode/issues/`). Cloned cheaply (inner `Arc`).
207    /// Used by the agent `issue` tool, the TUI indicator, and the `oxicode issue`
208    /// CLI subcommand.
209    issue_store: Option<crate::store::issues::FileIssueStore>,
210    /// Process-wide liveness identity used by every issue-ownership surface
211    /// in this process (agent tool's `ToolContext.session_id`, TUI panel,
212    /// slash-command `/issue` handlers). See
213    /// [`crate::store::issues::liveness::TUI_OWNERSHIP_ID`] for the TUI value.
214    ownership_session_id: String,
215    /// Alive-lock held for the lifetime of `App`. Dropped with `App`, releasing
216    /// the OS-held flock so any other process sees this session as dead once
217    /// we exit (including `kill -9` / crash / normal exit). Only held when
218    /// `issue_store` is available.
219    #[allow(dead_code)]
220    liveness_guard: Option<crate::store::issues::liveness::AliveGuard>,
221    /// Cached `default` persona body, resolved once in `from_oxicode` so
222    /// synchronous prompt rebuilds can reuse it without awaiting the port.
223    persona_body: RwLock<Option<String>>,
224    /// Pre-built session queues + stop flag. Cloned into `AgentSession` so
225    /// the runtime and the agent's session-level closures share the SAME
226    /// state (see [`SessionState`] doc).
227    session_state: SessionState,
228}
229// ─── System prompt builder ───────────────────────────────────────────────────
230fn build_system_prompt(
231    thinking_level: crate::store::settings::ThinkingLevel,
232    skill_contents: &[String],
233    persona_body: Option<&str>,
234) -> String {
235    let skills: Vec<prompt::system_prompt::Skill> = skill_contents
236        .iter()
237        .enumerate()
238        .map(|(i, content)| prompt::system_prompt::Skill {
239            name: format!("skill-{}", i),
240            content: content.clone(),
241        })
242        .collect();
243
244    let options = prompt::system_prompt::BuildSystemPromptOptions {
245        custom_prompt: prompt::system_prompt::thinking_level_prompt(thinking_level),
246        skills,
247        cwd: std::env::current_dir()
248            .map(|p| p.to_string_lossy().to_string())
249            .unwrap_or_default(),
250        persona_prompt: persona_body.map(|s| s.to_string()),
251        ..Default::default()
252    };
253
254    prompt::system_prompt::build_system_prompt(&options)
255}
256
257// ─── App implementation ─────────────────────────────────────────────────────
258
259impl App {
260    /// Build an `App` from a wired `Oxicode` engine and a settings object.
261    ///
262    /// The `Oxicode` should be created via [`build_oxicode_engine`] (or
263    /// `services::build_oxicode`) so that all 11 ports are wired. The
264    /// settings hold the user's runtime configuration (model, thinking
265    /// level, etc.).
266    ///
267    /// `ownership_session_id` is the per-process liveness identity used by
268    /// the agent's `issue` tool (`ToolContext.session_id`), the TUI panel,
269    /// and the `/issue` slash command. In TUI mode this MUST equal
270    /// [`crate::store::issues::liveness::TUI_OWNERSHIP_ID`] so the panel and
271    /// agent see the same flock holder. In print / RPC mode, a stable
272    /// process-scoped id (e.g. `proc-<pid>-<uuid>`) is appropriate.
273    ///
274    /// `session_state` is the pre-built [`SessionState`] passed into the
275    /// agent's `with_session_hooks` call. When `None`, fresh state is
276    /// constructed (the default for tests and most call sites — bootstrap
277    /// constructs it explicitly so the runtime and AgentSession share the
278    /// SAME queues + stop flag).
279    pub async fn from_oxicode(
280        oxicode: oxicode_sdk::Oxicode,
281        settings: Settings,
282        ownership_session_id: String,
283        session_state: Option<SessionState>,
284    ) -> Result<Self> {
285        let session_state = session_state.unwrap_or_default();
286        // Resolve the default persona once from the wired
287        // PersonaProvider port. The body flows into the system prompt;
288        // `preferred_model` overrides the settings default when no
289        // other override exists.
290        let persona = match oxicode.ports().personas.get("default").await {
291            Ok(Some(p)) if !p.system_prompt.trim().is_empty() => Some(p),
292            Ok(_) => None,
293            Err(e) => {
294                tracing::warn!(error = %e, "default persona lookup failed");
295                None
296            }
297        };
298
299        let model_id = persona
300            .as_ref()
301            .and_then(|p| p.preferred_model.clone())
302            .or_else(|| settings.effective_model(None))
303            .unwrap_or_default();
304        // Provider-name and api_key lookups removed in 0.55.0 — the SDK
305        // resolver consults the wired AuthProvider port directly.
306
307        let skills_dir = SkillManager::skills_dir().unwrap_or_else(|_| {
308            dirs::home_dir()
309                .unwrap_or_default()
310                .join(".oxicode")
311                .join("skills")
312        });
313        let skills = SkillManager::load_from_dir(&skills_dir).unwrap_or_else(|e| {
314            tracing::debug!("Skills not loaded: {}", e);
315            SkillManager::new()
316        });
317
318        let body_str = persona.as_ref().map(|p| p.system_prompt.clone());
319        let system_prompt = build_system_prompt(settings.thinking_level, &[], body_str.as_deref());
320        let compaction_strategy = if settings.auto_compaction {
321            oxicode_sdk::CompactionStrategy::Threshold(0.8)
322        } else {
323            oxicode_sdk::CompactionStrategy::Disabled
324        };
325
326        let config = AgentConfig {
327            name: "oxicode".to_string(),
328            description: Some("oxicode CLI agent".to_string()),
329            model_id: model_id.clone(),
330            system_prompt: Some(system_prompt),
331            timeout_seconds: settings.tool_timeout_seconds,
332            temperature: settings.effective_temperature(),
333            max_tokens: settings.effective_max_tokens(),
334            compaction_strategy,
335            compaction_instruction: None,
336            context_window: 128_000,
337            workspace_dir: Some(
338                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
339            ),
340            output_mode: None,
341            provider_options: None,
342            session_id: Some(ownership_session_id.clone()),
343            ttsr_engine: None,
344            memory: None,
345            todo: None,
346            agent_pool: None,
347            url_resolver: Some(Arc::new(oxicode_sdk::SdkUrlResolver::new(
348                oxicode.ports().url_router.clone(),
349            ))),
350            // LSP: lazy-spawn rust-analyzer (or other configured
351            // servers) on first request. When no servers are
352            // configured for the workspace, the field stays `None`
353            // and AgentBuilder.build() drops the `lsp` tool from
354            // the registry (see agent_builder.rs::build).
355            lsp: if crate::lsp::manager::default_servers().is_empty() {
356                None
357            } else {
358                Some(Arc::new(crate::lsp::CliLspProvider::with_defaults(
359                    std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
360                )))
361            },
362            ..Default::default()
363        };
364
365        // Build the agent via the SDK's AgentBuilder — no manual wiring.
366        //
367        // Single `set_hooks` invariant: the agent's hook chain is built
368        // EXACTLY ONCE here, composing the port-backed middleware pipeline
369        // (`with_port_hooks`) and the cli-owned session closures
370        // (`with_session_hooks`) into one `AgentHooks` value. NEVER call
371        // `agent.set_hooks(...)` elsewhere (it would wipe the
372        // before/after_tool_call slots the middleware populated).
373        let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
374
375        // Clone the shared session state into the closures. Each closure
376        // owns a fresh `Arc` clone — cheap, but essential so the agent
377        // and the runtime (which owns the `SessionState`) see mutations
378        // from either side.
379        // Build the shared AskBridge early. Its mode atomic is shared with
380        // the per-turn steering closure below so a runtime toggle
381        // (Shift+Tab in the TUI) takes effect immediately across the agent
382        // loop, the ask tool, and the render state.
383        let ask_timeout = if settings.ask_timeout_secs > 0 {
384            Some(std::time::Duration::from_secs(settings.ask_timeout_secs))
385        } else {
386            None
387        };
388        let bridge = std::sync::Arc::new(oxicode_agent::tools::ask::AskBridge::with_timeout(
389            ask_timeout,
390        ));
391        let mode_handle = bridge.mode_handle();
392
393        let stop_flag = Arc::clone(&session_state.should_stop);
394        let steering = Arc::clone(&session_state.steering);
395        let follow_up = Arc::clone(&session_state.follow_up);
396        let session_hooks = oxicode_sdk::agent_builder::SessionHookClosures {
397            should_stop_after_turn: Arc::new(move |_| {
398                stop_flag.load(std::sync::atomic::Ordering::SeqCst)
399            }),
400            get_steering_messages: Arc::new(move || {
401                let mut msgs: Vec<oxicode_sdk::Message> = steering.write().drain(..).collect();
402                // Auto mode: reinforce autonomous operation every turn so a
403                // mid-session Shift+Tab toggle takes effect immediately.
404                if oxicode_agent::config::Mode::load(&mode_handle).is_auto() {
405                    msgs.push(oxicode_sdk::Message::User(oxicode_sdk::UserMessage::new(
406                        "Autonomy mode (auto) is active: proceed autonomously to \
407                         completion without asking the user questions. Make \
408                         reasonable decisions on your own and keep working.",
409                    )));
410                }
411                msgs
412            }),
413            get_follow_up_messages: Arc::new(move || follow_up.write().drain(..).collect()),
414            tool_execution: oxicode_agent::config::ToolExecutionMode::Sequential,
415        };
416
417        let agent = oxicode
418            .agent(config)
419            .workspace(cwd)
420            .with_port_hooks()
421            .with_session_hooks(session_hooks)
422            .build()
423            .map_err(|e| Error::msg(format!("agent build failed: {e}")))?;
424        let agent = Arc::new(agent);
425
426        let ask_tool = oxicode_agent::tools::ask::AskTool::new(bridge.clone());
427        agent.tools().register_arc(std::sync::Arc::new(ask_tool));
428        // Open the local issue store rooted at the project (`.oxicode/issues/`).
429        // Best-effort: if the directory cannot be resolved, issues are simply
430        // unavailable — the app still works without them. The `/issue` slash
431        // command surfaces a clear error in that case.
432        let issue_store = std::env::current_dir()
433            .ok()
434            .map(|cwd| crate::store::issues::FileIssueStore::open_from_cwd(&cwd))
435            .and_then(|r| {
436                r.map_err(|e| tracing::warn!("issue store unavailable: {e}"))
437                    .ok()
438            });
439
440        // Register the `issue` agent tool when the store is available.
441        if let Some(store) = issue_store.clone() {
442            let tool = std::sync::Arc::new(crate::tools::IssueTool::new(store));
443            agent.tools().register_arc(tool);
444        }
445
446        Ok(Self {
447            oxicode,
448            agent,
449            settings,
450            skills: RwLock::new(skills),
451            active_skills: RwLock::new(Vec::new()),
452            wasm_ext: None,
453            ask_bridge: Some(bridge),
454            issue_store,
455            ownership_session_id,
456            liveness_guard: None, // set below once issue_store is known
457            persona_body: RwLock::new(persona.as_ref().map(|p| p.system_prompt.clone())),
458            session_state,
459        })
460        .map(|mut app| {
461            // Acquire the process-wide liveness flock now that issue_store exists.
462            // Best-effort: another live process already holds the lock is non-fatal;
463            // we still expose ownership_session_id so callers can detect the conflict.
464            app.liveness_guard =
465                acquire_ownership_guard(app.issue_store.as_ref(), &app.ownership_session_id);
466            app
467        })
468    }
469
470    /// Per-process liveness identity. Used by the agent's `issue` tool and any
471    /// other surface that gates on `is_session_alive`.
472    pub fn ownership_session_id(&self) -> &str {
473        &self.ownership_session_id
474    }
475
476    /// True iff `App` holds a live liveness flock under `ownership_session_id`.
477    /// False when there is no `issue_store` (e.g. headless test) or when another
478    /// live process already holds the lock (the assignment feature will surface
479    /// `Assigned` errors in that case — by design).
480    pub fn has_liveness_lock(&self) -> bool {
481        self.liveness_guard.is_some()
482    }
483
484    /// Get the current settings
485    pub fn settings(&self) -> &Settings {
486        &self.settings
487    }
488
489    /// Set the WASM extension manager
490    pub fn set_wasm_ext(
491        &mut self,
492        ext: Option<std::sync::Arc<crate::extensions::WasmExtensionManager>>,
493    ) {
494        self.wasm_ext = ext;
495    }
496
497    /// Get the WASM extension manager
498    pub fn wasm_ext(&self) -> Option<&std::sync::Arc<crate::extensions::WasmExtensionManager>> {
499        self.wasm_ext.as_ref()
500    }
501
502    /// Get a clone of the local issue store, if one was opened successfully.
503    pub fn issue_store(&self) -> Option<crate::store::issues::FileIssueStore> {
504        self.issue_store.clone()
505    }
506
507    /// Get a reference to the underlying `Oxicode` engine. The catalog port and
508    /// other ports are accessible through it.
509    pub fn oxicode(&self) -> &oxicode_sdk::Oxicode {
510        &self.oxicode
511    }
512
513    /// Get a clone of the model catalog port (the canonical provider/model
514    /// metadata source). Used by the TUI to browse the full catalog in-TUI.
515    pub fn catalog(&self) -> std::sync::Arc<dyn oxicode_sdk::ports::catalog::ModelCatalog> {
516        std::sync::Arc::clone(self.oxicode.catalog())
517    }
518
519    /// Get a reference to the underlying agent.
520    pub fn agent(&self) -> Arc<Agent> {
521        Arc::clone(&self.agent)
522    }
523
524    /// Get the tool registry (for registering extension tools)
525    pub fn agent_tools(&self) -> Arc<oxicode_agent::ToolRegistry> {
526        self.agent.tools()
527    }
528
529    /// Get the ask bridge, if initialized.
530    pub fn ask_bridge(&self) -> Option<&std::sync::Arc<oxicode_agent::tools::ask::AskBridge>> {
531        self.ask_bridge.as_ref()
532    }
533
534    /// Get a reference to the skill manager
535    pub fn skills(&self) -> parking_lot::RwLockReadGuard<'_, SkillManager> {
536        self.skills.read()
537    }
538
539    /// Activate a skill by name. Returns an error string if not found.
540    pub fn activate_skill(&self, name: &str) -> Result<(), String> {
541        {
542            let skills = self.skills.read();
543            if skills.get(name).is_none() {
544                return Err(format!("Skill '{}' not found", name));
545            }
546        }
547        let name_lower = name.to_lowercase();
548        {
549            let mut active = self.active_skills.write();
550            if !active.contains(&name_lower) {
551                active.push(name_lower);
552            }
553        }
554        self.rebuild_system_prompt();
555        Ok(())
556    }
557
558    /// Deactivate a skill by name.
559    pub fn deactivate_skill(&self, name: &str) {
560        let name_lower = name.to_lowercase();
561        {
562            let mut active = self.active_skills.write();
563            active.retain(|n| n != &name_lower);
564        }
565        self.rebuild_system_prompt();
566    }
567
568    /// List currently active skill names
569    pub fn active_skills(&self) -> Vec<String> {
570        self.active_skills.read().clone()
571    }
572
573    /// Rebuild the system prompt with current active skills
574    fn rebuild_system_prompt(&self) {
575        let active = self.active_skills.read();
576        let skills = self.skills.read();
577        let contents: Vec<String> = active
578            .iter()
579            .filter_map(|name| skills.get(name).map(|s| s.content.clone()))
580            .collect();
581        // The persona body was resolved once in `from_oxicode` and cached
582        // on `self.persona_body` so this sync rebuild can include it
583        // without re-awaiting the async PersonaProvider port.
584        let persona = self.persona_body.read().clone();
585        let prompt =
586            build_system_prompt(self.settings.thinking_level, &contents, persona.as_deref());
587        self.agent.set_system_prompt(prompt);
588    }
589
590    /// Get a clone of the current state
591    pub fn agent_state(&self) -> oxicode_agent::AgentState {
592        self.agent.state()
593    }
594
595    /// Run a single prompt and return the response
596    pub async fn run_prompt(&self, prompt: String) -> Result<String> {
597        let (response, _events) = self.agent.run(prompt).await?;
598        Ok(response.content)
599    }
600
601    /// Run a prompt with event callback
602    pub async fn run_prompt_with_events<F>(&self, prompt: String, on_event: F) -> Result<String>
603    where
604        F: FnMut(AgentEvent) + Send + 'static,
605    {
606        self.agent.run_streaming(prompt, on_event).await?;
607        let state = self.agent_state();
608        for msg in state.messages.iter().rev() {
609            if let oxicode_sdk::Message::Assistant(a) = msg {
610                return Ok(a.text_content());
611            }
612        }
613        Ok(String::new())
614    }
615
616    /// Reset the conversation
617    pub fn reset(&self) {
618        self.agent.reset();
619    }
620
621    /// Switch the model used for future LLM calls.
622    ///
623    /// The new provider is re-credentialed by the SDK resolver via the
624    /// wired AuthProvider port; the `api_key` parameter was removed in
625    /// 0.55.0 (issues #39/#40).
626    pub async fn switch_model(&self, model_id: &str) -> anyhow::Result<()> {
627        let _ = self.agent.switch_model(model_id);
628        Ok(())
629    }
630
631    /// Get the current model ID
632    pub fn model_id(&self) -> String {
633        self.agent.model_id()
634    }
635
636    /// Borrow the pre-built [`SessionState`] (stop flag + steering + follow-up
637    /// queues). The runtime clones the `Arc`s it needs into `AgentSession`
638    /// so the runtime and the agent's session-level closures share the SAME
639    /// state — required for Ctrl+C and `/steer` to take effect mid-run.
640    pub fn session_state(&self) -> &SessionState {
641        &self.session_state
642    }
643
644    /// Clone the shared stop flag. Cheap (single Arc bump).
645    pub fn should_stop_flag(&self) -> Arc<std::sync::atomic::AtomicBool> {
646        Arc::clone(&self.session_state.should_stop)
647    }
648
649    /// Clone the shared steering queue.
650    pub fn steering_queue(&self) -> Arc<RwLock<VecDeque<oxicode_sdk::Message>>> {
651        Arc::clone(&self.session_state.steering)
652    }
653
654    /// Clone the shared follow-up queue.
655    pub fn follow_up_queue(&self) -> Arc<RwLock<VecDeque<oxicode_sdk::Message>>> {
656        Arc::clone(&self.session_state.follow_up)
657    }
658}
659
660/// Acquire the process-wide liveness flock for `ownership_id` under the issue
661/// store's `.alive/` directory.
662///
663/// Returns `None` (no lock) when there is no issue store or when another live
664/// process already holds the lock — both non-fatal; the caller can still read
665/// `ownership_session_id` and the assignment feature will surface `Assigned`
666/// errors if contention actually occurs.
667///
668/// Extracted from `App::from_oxicode` so the single-lock invariant (defect #13 fix)
669/// can be unit-tested without standing up a full `Oxicode` engine.
670pub(crate) fn acquire_ownership_guard(
671    issue_store: Option<&crate::store::issues::FileIssueStore>,
672    ownership_id: &str,
673) -> Option<crate::store::issues::liveness::AliveGuard> {
674    let store = issue_store?;
675    if ownership_id.is_empty() {
676        // Defensive: never hold a lock under the empty string — that was the
677        // #13 bug shape (empty owner is never alive, so ownership was bypassed).
678        return None;
679    }
680    crate::store::issues::liveness::acquire(&store.issues_dir(), ownership_id).ok()
681}
682
683#[cfg(test)]
684mod tests {
685    //! P0 regression: `App` must hold exactly one liveness flock under its
686    //! ownership identity. We test the extracted `acquire_ownership_guard`
687    //! helper (the single chokepoint `from_oxicode` delegates to) rather than
688    //! standing up a full `Oxicode` engine.
689    use super::*;
690    use crate::store::issues::FileIssueStore;
691    use crate::store::issues::liveness;
692
693    fn tmp_store() -> (tempfile::TempDir, FileIssueStore) {
694        let tmp = tempfile::tempdir().unwrap();
695        let dir = tmp.path().join(".oxicode").join("issues");
696        std::fs::create_dir_all(&dir).unwrap();
697        (tmp, FileIssueStore::open(dir).unwrap())
698    }
699
700    #[test]
701    fn app_holds_single_liveness_lock() {
702        // The #13 invariant: acquiring the ownership guard makes the session
703        // live under that identity, and a second acquire under the SAME id
704        // fails (one flock per identity — single lock).
705        let (_tmp, store) = tmp_store();
706        let dir = store.issues_dir();
707        let id = "proc-test-app";
708
709        let guard = acquire_ownership_guard(Some(&store), id);
710        assert!(
711            guard.is_some(),
712            "App must acquire the liveness lock for its ownership id"
713        );
714        assert!(
715            liveness::is_session_alive(&dir, id),
716            "after acquire, the session must be live"
717        );
718
719        // While held, the same identity cannot be acquired again — single lock.
720        let second = liveness::acquire(&dir, id);
721        assert!(second.is_err(), "second acquire under same id must fail");
722
723        drop(guard);
724        assert!(
725            !liveness::is_session_alive(&dir, id),
726            "dropping App's guard releases the lock"
727        );
728    }
729
730    #[test]
731    fn acquire_returns_none_without_store() {
732        // No issue store (headless/test) → no lock. Not an error.
733        let dir = tempfile::tempdir().unwrap();
734        let id = "proc-x";
735        assert!(acquire_ownership_guard(None, id).is_none());
736        let _ = dir; // no store created
737    }
738
739    #[test]
740    fn acquire_rejects_empty_ownership_id() {
741        // Defensive guard against the #13 bug shape: never hold a lock under
742        // the empty string (it's never alive, so ownership would be bypassed).
743        let (_tmp, store) = tmp_store();
744        assert!(
745            acquire_ownership_guard(Some(&store), "").is_none(),
746            "empty ownership id must never acquire a lock (#13 guard)"
747        );
748    }
749}
750pub mod symbols;
751pub mod tui_vt;