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 foundation;
23pub mod internal_urls;
24pub mod lsp;
25pub mod main_dispatch;
26pub mod mcp_credentials;
27pub mod oauth_listener;
28pub mod oauth_refresh;
29pub mod print_mode;
30pub mod provider_oauth;
31pub mod services;
32pub mod setup_wizard;
33pub mod store;
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<oxicode_sdk::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). Unique per process in every mode:
213    /// `tui-<pid>-<uuid>` in TUI, `proc-<pid>-<uuid>` in headless runs.
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<oxicode_sdk::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. It must be unique per process in
270    /// every mode (`tui-<pid>-<uuid>` / `proc-<pid>-<uuid>`) so two parallel
271    /// sessions never share one flock name — a shared name silently broke
272    /// ownership exclusivity between them.
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            oxicode_catalog::product_env::home_dir()
309                .unwrap_or_default()
310                .join("skills")
311        });
312        let skills = SkillManager::load_from_dir(&skills_dir).unwrap_or_else(|e| {
313            tracing::debug!("Skills not loaded: {}", e);
314            SkillManager::new()
315        });
316
317        let body_str = persona.as_ref().map(|p| p.system_prompt.clone());
318        let system_prompt = build_system_prompt(settings.thinking_level, &[], body_str.as_deref());
319        let compaction_strategy = if settings.auto_compaction {
320            oxicode_sdk::CompactionStrategy::Threshold(0.8)
321        } else {
322            oxicode_sdk::CompactionStrategy::Disabled
323        };
324
325        let config = AgentConfig {
326            name: "oxicode".to_string(),
327            description: Some("oxicode CLI agent".to_string()),
328            model_id: model_id.clone(),
329            system_prompt: Some(system_prompt),
330            timeout_seconds: settings.tool_timeout_seconds,
331            temperature: settings.effective_temperature(),
332            max_tokens: settings.effective_max_tokens(),
333            compaction_strategy,
334            compaction_instruction: None,
335            context_window: 128_000,
336            workspace_dir: Some(
337                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
338            ),
339            output_mode: None,
340            provider_options: None,
341            session_id: Some(ownership_session_id.clone()),
342            ttsr_engine: None,
343            memory: None,
344            todo: None,
345            agent_pool: None,
346            url_resolver: Some(Arc::new(oxicode_sdk::SdkUrlResolver::new(
347                oxicode.ports().url_router.clone(),
348            ))),
349            // LSP: lazy-spawn rust-analyzer (or other configured
350            // servers) on first request. When no servers are
351            // configured for the workspace, the field stays `None`
352            // and AgentBuilder.build() drops the `lsp` tool from
353            // the registry (see agent_builder.rs::build).
354            lsp: if crate::lsp::manager::default_servers().is_empty() {
355                None
356            } else {
357                Some(Arc::new(crate::lsp::CliLspProvider::with_defaults(
358                    std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
359                )))
360            },
361            ..Default::default()
362        };
363
364        // Build the agent via the SDK's AgentBuilder — no manual wiring.
365        //
366        // Single `set_hooks` invariant: the agent's hook chain is built
367        // EXACTLY ONCE here, composing the port-backed middleware pipeline
368        // (`with_port_hooks`) and the cli-owned session closures
369        // (`with_session_hooks`) into one `AgentHooks` value. NEVER call
370        // `agent.set_hooks(...)` elsewhere (it would wipe the
371        // before/after_tool_call slots the middleware populated).
372        let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
373
374        // Clone the shared session state into the closures. Each closure
375        // owns a fresh `Arc` clone — cheap, but essential so the agent
376        // and the runtime (which owns the `SessionState`) see mutations
377        // from either side.
378        // Build the shared AskBridge early. Its mode atomic is shared with
379        // the per-turn steering closure below so a runtime toggle
380        // (Shift+Tab in the TUI) takes effect immediately across the agent
381        // loop, the ask tool, and the render state.
382        let ask_timeout = if settings.ask_timeout_secs > 0 {
383            Some(std::time::Duration::from_secs(settings.ask_timeout_secs))
384        } else {
385            None
386        };
387        let bridge = std::sync::Arc::new(oxicode_agent::tools::ask::AskBridge::with_timeout(
388            ask_timeout,
389        ));
390        let mode_handle = bridge.mode_handle();
391
392        let stop_flag = Arc::clone(&session_state.should_stop);
393        let steering = Arc::clone(&session_state.steering);
394        let follow_up = Arc::clone(&session_state.follow_up);
395        let session_hooks = oxicode_sdk::agent_builder::SessionHookClosures {
396            should_stop_after_turn: Arc::new(move |_| {
397                stop_flag.load(std::sync::atomic::Ordering::SeqCst)
398            }),
399            get_steering_messages: Arc::new(move || {
400                let mut msgs: Vec<oxicode_sdk::Message> = steering.write().drain(..).collect();
401                // Auto mode: reinforce autonomous operation every turn so a
402                // mid-session Shift+Tab toggle takes effect immediately.
403                if oxicode_agent::config::Mode::load(&mode_handle).is_auto() {
404                    msgs.push(oxicode_sdk::Message::User(oxicode_sdk::UserMessage::new(
405                        "Autonomy mode (auto) is active: proceed autonomously to \
406                         completion without asking the user questions. Make \
407                         reasonable decisions on your own and keep working.",
408                    )));
409                }
410                msgs
411            }),
412            get_follow_up_messages: Arc::new(move || follow_up.write().drain(..).collect()),
413            tool_execution: oxicode_agent::config::ToolExecutionMode::Sequential,
414        };
415
416        let agent = oxicode
417            .agent(config)
418            .workspace(cwd)
419            .with_port_hooks()
420            .with_session_hooks(session_hooks)
421            .build()
422            .map_err(|e| Error::msg(format!("agent build failed: {e}")))?;
423        let agent = Arc::new(agent);
424
425        let ask_tool = oxicode_agent::tools::ask::AskTool::new(bridge.clone());
426        agent.tools().register_arc(std::sync::Arc::new(ask_tool));
427        // Open the local issue store rooted at the project (`.oxicode/issues/`).
428        // Best-effort: if the directory cannot be resolved, issues are simply
429        // unavailable — the app still works without them. The `/issue` slash
430        // command surfaces a clear error in that case.
431        let issue_store = std::env::current_dir()
432            .ok()
433            .map(|cwd| oxicode_sdk::FileIssueStore::open_from_cwd(&cwd))
434            .and_then(|r| {
435                r.map_err(|e| tracing::warn!("issue store unavailable: {e}"))
436                    .ok()
437            });
438
439        // Register the `issue` agent tool when the store is available.
440        if let Some(store) = issue_store.clone() {
441            let tool = std::sync::Arc::new(oxicode_sdk::IssueTool::new(store));
442            agent.tools().register_arc(tool);
443        }
444
445        Ok(Self {
446            oxicode,
447            agent,
448            settings,
449            skills: RwLock::new(skills),
450            active_skills: RwLock::new(Vec::new()),
451            wasm_ext: None,
452            ask_bridge: Some(bridge),
453            issue_store,
454            ownership_session_id,
455            liveness_guard: None, // set below once issue_store is known
456            persona_body: RwLock::new(persona.as_ref().map(|p| p.system_prompt.clone())),
457            session_state,
458        })
459        .map(|mut app| {
460            // Acquire the process-wide liveness flock now that issue_store exists.
461            // Best-effort: another live process already holds the lock is non-fatal;
462            // we still expose ownership_session_id so callers can detect the conflict.
463            app.liveness_guard =
464                acquire_ownership_guard(app.issue_store.as_ref(), &app.ownership_session_id);
465            app
466        })
467    }
468
469    /// Per-process liveness identity. Used by the agent's `issue` tool and any
470    /// other surface that gates on `is_session_alive`.
471    pub fn ownership_session_id(&self) -> &str {
472        &self.ownership_session_id
473    }
474
475    /// True iff `App` holds a live liveness flock under `ownership_session_id`.
476    /// False when there is no `issue_store` (e.g. headless test) or when another
477    /// live process already holds the lock (the assignment feature will surface
478    /// `Assigned` errors in that case — by design).
479    pub fn has_liveness_lock(&self) -> bool {
480        self.liveness_guard.is_some()
481    }
482
483    /// Get the current settings
484    pub fn settings(&self) -> &Settings {
485        &self.settings
486    }
487
488    /// Set the WASM extension manager
489    pub fn set_wasm_ext(
490        &mut self,
491        ext: Option<std::sync::Arc<crate::extensions::WasmExtensionManager>>,
492    ) {
493        self.wasm_ext = ext;
494    }
495
496    /// Get the WASM extension manager
497    pub fn wasm_ext(&self) -> Option<&std::sync::Arc<crate::extensions::WasmExtensionManager>> {
498        self.wasm_ext.as_ref()
499    }
500
501    /// Get a clone of the local issue store, if one was opened successfully.
502    pub fn issue_store(&self) -> Option<oxicode_sdk::FileIssueStore> {
503        self.issue_store.clone()
504    }
505
506    /// Get a reference to the underlying `Oxicode` engine. The catalog port and
507    /// other ports are accessible through it.
508    pub fn oxicode(&self) -> &oxicode_sdk::Oxicode {
509        &self.oxicode
510    }
511
512    /// Get a clone of the model catalog port (the canonical provider/model
513    /// metadata source). Used by the TUI to browse the full catalog in-TUI.
514    pub fn catalog(&self) -> std::sync::Arc<dyn oxicode_sdk::ports::catalog::ModelCatalog> {
515        std::sync::Arc::clone(self.oxicode.catalog())
516    }
517
518    /// Get a reference to the underlying agent.
519    pub fn agent(&self) -> Arc<Agent> {
520        Arc::clone(&self.agent)
521    }
522
523    /// Get the tool registry (for registering extension tools)
524    pub fn agent_tools(&self) -> Arc<oxicode_agent::ToolRegistry> {
525        self.agent.tools()
526    }
527
528    /// Get the ask bridge, if initialized.
529    pub fn ask_bridge(&self) -> Option<&std::sync::Arc<oxicode_agent::tools::ask::AskBridge>> {
530        self.ask_bridge.as_ref()
531    }
532
533    /// Get a reference to the skill manager
534    pub fn skills(&self) -> parking_lot::RwLockReadGuard<'_, SkillManager> {
535        self.skills.read()
536    }
537
538    /// Activate a skill by name. Returns an error string if not found.
539    pub fn activate_skill(&self, name: &str) -> Result<(), String> {
540        {
541            let skills = self.skills.read();
542            if skills.get(name).is_none() {
543                return Err(format!("Skill '{}' not found", name));
544            }
545        }
546        let name_lower = name.to_lowercase();
547        {
548            let mut active = self.active_skills.write();
549            if !active.contains(&name_lower) {
550                active.push(name_lower);
551            }
552        }
553        self.rebuild_system_prompt();
554        Ok(())
555    }
556
557    /// Deactivate a skill by name.
558    pub fn deactivate_skill(&self, name: &str) {
559        let name_lower = name.to_lowercase();
560        {
561            let mut active = self.active_skills.write();
562            active.retain(|n| n != &name_lower);
563        }
564        self.rebuild_system_prompt();
565    }
566
567    /// List currently active skill names
568    pub fn active_skills(&self) -> Vec<String> {
569        self.active_skills.read().clone()
570    }
571
572    /// Rebuild the system prompt with current active skills
573    fn rebuild_system_prompt(&self) {
574        let active = self.active_skills.read();
575        let skills = self.skills.read();
576        let contents: Vec<String> = active
577            .iter()
578            .filter_map(|name| skills.get(name).map(|s| s.content.clone()))
579            .collect();
580        // The persona body was resolved once in `from_oxicode` and cached
581        // on `self.persona_body` so this sync rebuild can include it
582        // without re-awaiting the async PersonaProvider port.
583        let persona = self.persona_body.read().clone();
584        let prompt =
585            build_system_prompt(self.settings.thinking_level, &contents, persona.as_deref());
586        self.agent.set_system_prompt(prompt);
587    }
588
589    /// Get a clone of the current state
590    pub fn agent_state(&self) -> oxicode_agent::AgentState {
591        self.agent.state()
592    }
593
594    /// Run a single prompt and return the response
595    pub async fn run_prompt(&self, prompt: String) -> Result<String> {
596        let (response, _events) = self.agent.run(prompt).await?;
597        Ok(response.content)
598    }
599
600    /// Run a prompt with event callback
601    pub async fn run_prompt_with_events<F>(&self, prompt: String, on_event: F) -> Result<String>
602    where
603        F: FnMut(AgentEvent) + Send + 'static,
604    {
605        self.agent.run_streaming(prompt, on_event).await?;
606        let state = self.agent_state();
607        for msg in state.messages.iter().rev() {
608            if let oxicode_sdk::Message::Assistant(a) = msg {
609                return Ok(a.text_content());
610            }
611        }
612        Ok(String::new())
613    }
614
615    /// Reset the conversation
616    pub fn reset(&self) {
617        self.agent.reset();
618    }
619
620    /// Switch the model used for future LLM calls.
621    ///
622    /// The new provider is re-credentialed by the SDK resolver via the
623    /// wired AuthProvider port; the `api_key` parameter was removed in
624    /// 0.55.0 (issues #39/#40).
625    pub async fn switch_model(&self, model_id: &str) -> anyhow::Result<()> {
626        let _ = self.agent.switch_model(model_id);
627        Ok(())
628    }
629
630    /// Get the current model ID
631    pub fn model_id(&self) -> String {
632        self.agent.model_id()
633    }
634
635    /// Borrow the pre-built [`SessionState`] (stop flag + steering + follow-up
636    /// queues). The runtime clones the `Arc`s it needs into `AgentSession`
637    /// so the runtime and the agent's session-level closures share the SAME
638    /// state — required for Ctrl+C and `/steer` to take effect mid-run.
639    pub fn session_state(&self) -> &SessionState {
640        &self.session_state
641    }
642
643    /// Clone the shared stop flag. Cheap (single Arc bump).
644    pub fn should_stop_flag(&self) -> Arc<std::sync::atomic::AtomicBool> {
645        Arc::clone(&self.session_state.should_stop)
646    }
647
648    /// Clone the shared steering queue.
649    pub fn steering_queue(&self) -> Arc<RwLock<VecDeque<oxicode_sdk::Message>>> {
650        Arc::clone(&self.session_state.steering)
651    }
652
653    /// Clone the shared follow-up queue.
654    pub fn follow_up_queue(&self) -> Arc<RwLock<VecDeque<oxicode_sdk::Message>>> {
655        Arc::clone(&self.session_state.follow_up)
656    }
657}
658
659/// Acquire the process-wide liveness flock for `ownership_id` under the issue
660/// store's `.alive/` directory.
661///
662/// Returns `None` (no lock) when there is no issue store or when another live
663/// process already holds the lock — both non-fatal; the caller can still read
664/// `ownership_session_id` and the assignment feature will surface `Assigned`
665/// errors if contention actually occurs.
666///
667/// Extracted from `App::from_oxicode` so the single-lock invariant (defect #13 fix)
668/// can be unit-tested without standing up a full `Oxicode` engine.
669pub(crate) fn acquire_ownership_guard(
670    issue_store: Option<&oxicode_sdk::FileIssueStore>,
671    ownership_id: &str,
672) -> Option<oxicode_sdk::liveness::AliveGuard> {
673    let store = issue_store?;
674    if ownership_id.is_empty() {
675        // Defensive: never hold a lock under the empty string — that was the
676        // #13 bug shape (empty owner is never alive, so ownership was bypassed).
677        return None;
678    }
679    match oxicode_sdk::liveness::acquire(&store.issues_dir(), ownership_id) {
680        Ok(guard) => Some(guard),
681        Err(e) => {
682            // Non-fatal (reads and ownership checks still work), but the
683            // process is not recognized as a live flock holder while the
684            // lock is missing — its assignments stay contestable. Surface
685            // it loudly instead of the historical silent `.ok()`.
686            tracing::warn!(
687                ownership_id,
688                error = %e,
689                "issue liveness flock acquisition failed; this session's \
690                 ownership claims are contestable while the lock is missing"
691            );
692            None
693        }
694    }
695}
696
697#[cfg(test)]
698mod tests {
699    //! P0 regression: `App` must hold exactly one liveness flock under its
700    //! ownership identity. We test the extracted `acquire_ownership_guard`
701    //! helper (the single chokepoint `from_oxicode` delegates to) rather than
702    //! standing up a full `Oxicode` engine.
703    use super::*;
704    use oxicode_sdk::FileIssueStore;
705    use oxicode_sdk::liveness;
706
707    fn tmp_store() -> (tempfile::TempDir, FileIssueStore) {
708        let tmp = tempfile::tempdir().unwrap();
709        let dir = tmp.path().join(".oxicode").join("issues");
710        std::fs::create_dir_all(&dir).unwrap();
711        (tmp, FileIssueStore::open(dir).unwrap())
712    }
713
714    #[test]
715    fn app_holds_single_liveness_lock() {
716        // The #13 invariant: acquiring the ownership guard makes the session
717        // live under that identity, and a second acquire under the SAME id
718        // fails (one flock per identity — single lock).
719        let (_tmp, store) = tmp_store();
720        let dir = store.issues_dir();
721        let id = "proc-test-app";
722
723        let guard = acquire_ownership_guard(Some(&store), id);
724        assert!(
725            guard.is_some(),
726            "App must acquire the liveness lock for its ownership id"
727        );
728        assert!(
729            liveness::is_session_alive(&dir, id),
730            "after acquire, the session must be live"
731        );
732
733        // While held, the same identity cannot be acquired again — single lock.
734        let second = liveness::acquire(&dir, id);
735        assert!(second.is_err(), "second acquire under same id must fail");
736
737        drop(guard);
738        assert!(
739            !liveness::is_session_alive(&dir, id),
740            "dropping App's guard releases the lock"
741        );
742    }
743
744    #[test]
745    fn acquire_returns_none_without_store() {
746        // No issue store (headless/test) → no lock. Not an error.
747        let dir = tempfile::tempdir().unwrap();
748        let id = "proc-x";
749        assert!(acquire_ownership_guard(None, id).is_none());
750        let _ = dir; // no store created
751    }
752
753    #[test]
754    fn acquire_rejects_empty_ownership_id() {
755        // Defensive guard against the #13 bug shape: never hold a lock under
756        // the empty string (it's never alive, so ownership would be bypassed).
757        let (_tmp, store) = tmp_store();
758        assert!(
759            acquire_ownership_guard(Some(&store), "").is_none(),
760            "empty ownership id must never acquire a lock (#13 guard)"
761        );
762    }
763}
764pub mod symbols;
765pub mod tui_vt;