Skip to main content

oxios_kernel/
agent_runtime.rs

1//! Agent runtime: wraps oxi-sdk's Agent for Seed execution.
2//!
3//! The AgentRuntime uses `OxiosEngine.oxi().agent()` (AgentBuilder pattern)
4//! to construct agents with full middleware, observability, and security
5//! integration from oxi-sdk 0.24.0.
6//!
7//! # Architecture
8//!
9//! All tool access goes through `KernelHandle` — the single syscall-table-like
10//! path for agent OS control. The runtime:
11//!
12//! 1. Resolves the agent's CSpace from persona/role/hint
13//! 2. Registers tools via `register_tools_from_cspace()`
14//! 3. Optionally queries `ToolRetriever` for semantic capability hints
15//! 4. Builds an `Agent` via `AgentBuilder` with middleware pipeline
16//! 5. Runs via `Agent::run_streaming()` for real-time event processing
17//!
18//! # oxi-sdk 0.23.0 Integration
19//!
20//! Uses `AgentBuilder` for agent construction with:
21//! - `.with_rate_limit()` — tool call rate limiting
22//! - `.with_token_budget()` — per-execution token caps
23//! - `.tracer()` / `.cost_tracker()` — observability hooks
24//! ## Routing integration (RFC-011)
25//!
26//! Model usage events (`AgentEvent::Usage`) are recorded to the shared
27//! `RoutingStats` so the Web dashboard can display per-model call counts
28//! and estimated costs.
29
30use anyhow::Result;
31use oxi_sdk::observability::AuditTrail;
32use oxi_sdk::{
33    Agent, AgentConfig, AgentEvent, CompactionEvent, CompactionStrategy, ProviderResolver,
34};
35use oxi_sdk::{SearchCache, ToolExecutionMode, ToolRegistry};
36use parking_lot::Mutex;
37use std::collections::HashMap;
38use std::sync::Arc;
39// RFC-014 Phase D: `ToolRegistry::register_arc` is used in the AgentBuilder
40// path to attach CSpace tools after `builder.build()` returns.
41
42use crate::access_manager::{AccessGate, AgentContext, TracingAuditSink, TrailAuditSink};
43use crate::capability::resolve::resolve_cspace;
44use crate::engine::OxiosEngine;
45use crate::memory::{MemoryEntry, MemoryManager, MemoryType};
46use crate::persona::PersonaManager;
47use crate::tools::registration::register_tools_from_cspace_gated;
48
49use crate::KernelHandle;
50use crate::event_bus::KernelEvent;
51use crate::session_context::SessionContext;
52use crate::types::AgentId;
53use oxios_ouroboros::{Directive, Entity, ExecEnv, ExecutionResult, Seed};
54
55/// Global LLM circuit breaker instance — delegates to oxi-sdk's ProviderCircuitBreaker.
56static LLM_CIRCUIT_BREAKER: std::sync::OnceLock<oxi_sdk::ProviderCircuitBreaker> =
57    std::sync::OnceLock::new();
58
59/// Get the global LLM circuit breaker.
60fn get_llm_circuit_breaker() -> &'static oxi_sdk::ProviderCircuitBreaker {
61    LLM_CIRCUIT_BREAKER.get_or_init(|| {
62        oxi_sdk::ProviderCircuitBreaker::new(
63            "global".to_string(),
64            oxi_sdk::CircuitBreakerConfig::default(),
65        )
66    })
67}
68
69/// Configuration for creating AgentRuntime instances.
70#[derive(Debug, Clone)]
71pub struct AgentRuntimeConfig {
72    /// Model ID in `provider/model` format (e.g. `anthropic/claude-sonnet-4-20250514`).
73    pub model_id: String,
74    /// How to execute tool calls within a single turn.
75    pub tool_execution: ToolExecutionMode,
76    /// Whether auto-retry is enabled for retryable LLM errors.
77    pub auto_retry_enabled: bool,
78    /// Bound project paths. AgentRuntime sets CWD to paths[0].
79    pub project_paths: Vec<std::path::PathBuf>,
80    /// Scratch workspace directory for temp files.
81    pub workspace_dir: Option<std::path::PathBuf>,
82    /// API key resolved from CredentialStore at build time.
83    pub api_key: Option<String>,
84    /// Per-provider options for fine-grained control.
85    pub provider_options: Option<oxi_sdk::ProviderOptions>,
86    /// Rate limit for tool calls (requests per minute). 0 = unlimited.
87    pub rate_limit_per_minute: usize,
88    /// Token budget per agent execution. 0 = unlimited.
89    pub token_budget: usize,
90    /// Enable audit logging for all tool executions.
91    pub audit_tool_calls: bool,
92    /// Provider-level RPM for rate-limited provider pool. 0 = no pooling.
93    /// When set, uses `OxiosEngine::pooled_provider()` instead of `create_provider()`.
94    pub provider_rpm: u32,
95}
96
97impl Default for AgentRuntimeConfig {
98    fn default() -> Self {
99        Self {
100            model_id: String::new(),
101            tool_execution: ToolExecutionMode::Parallel,
102            auto_retry_enabled: true,
103            project_paths: Vec::new(),
104            workspace_dir: None,
105            api_key: None,
106            provider_options: None,
107            rate_limit_per_minute: 0,
108            token_budget: 0,
109            audit_tool_calls: false,
110            provider_rpm: 0,
111        }
112    }
113}
114
115/// Mutable state shared between the event callback and the main execute flow.
116#[derive(Default)]
117struct ExecuteState {
118    final_content: String,
119    steps_completed: usize,
120    success: bool,
121    /// Collected trajectory steps for SONA learning (RFC-020 Phase 2).
122    /// Ordered by insertion — parallel tools get their final position
123    /// resolved when they complete, preserving approximate execution order.
124    trajectory_steps: Vec<oxios_memory::memory::sona::TrajectoryStep>,
125    /// Map of tool_call_id → (start instant, index into trajectory_steps).
126    /// Used to correlate ToolExecutionEnd with the correct step when
127    /// parallel tool calls complete out of order.
128    pending_tools: std::collections::HashMap<String, (std::time::Instant, usize)>,
129    /// Ordered tool_call_ids matching trajectory_steps indices.
130    /// Pushed in ToolExecutionStart, same order as trajectory_steps.
131    tool_call_ids: Vec<String>,
132    /// Per-step tool args (JSON string) captured from ToolExecutionStart.
133    tool_args_map: std::collections::HashMap<String, String>,
134    /// Per-step error flag from ToolExecutionEnd.
135    tool_error_map: std::collections::HashMap<String, bool>,
136    /// Per-step start timestamp (UTC) from ToolExecutionStart.
137    tool_timestamps: std::collections::HashMap<String, chrono::DateTime<chrono::Utc>>,
138    /// Cumulative input tokens from AgentEvent::Usage.
139    total_input_tokens: u64,
140    /// Cumulative output tokens from AgentEvent::Usage.
141    total_output_tokens: u64,
142}
143
144/// Runtime that wraps an oxi-sdk `Agent` for executing Seeds.
145///
146/// Each call to [`AgentRuntime::execute`] creates a fresh `Agent`,
147/// builds a ToolRegistry based on the agent's CSpace, and runs it to completion.
148///
149/// All OS-level access goes through `KernelHandle` — the single syscall table
150/// for agent control. Provider/model resolution goes through `EngineHandle`,
151/// which returns the latest `OxiosEngine` (hot-swapped on config change).
152pub struct AgentRuntime {
153    engine_handle: Arc<crate::engine::EngineHandle>,
154    config: AgentRuntimeConfig,
155    /// Single path to all kernel services.
156    kernel_handle: Arc<KernelHandle>,
157    /// Persona manager for system prompt injection.
158    persona_manager: Option<Arc<PersonaManager>>,
159    /// Semantic tool retriever for capability discovery.
160    tool_retriever: Option<Arc<crate::tools::retrieval::ToolRetriever>>,
161    /// Shared routing stats (shared with EngineApi).
162    routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
163    /// Autonomous persistence hook (RFC-016).
164    persistence_hook: Option<Arc<crate::persistence_hook::PersistenceHook>>,
165    /// Per-session assistant message index counter (RFC-016).
166    session_msg_counter: Arc<Mutex<HashMap<String, usize>>>,
167}
168
169impl AgentRuntime {
170    /// Creates a new agent runtime with engine handle and kernel access.
171    ///
172    /// The active model is resolved live from `engine_handle` on each
173    /// `execute()` (reads the post-hot-swap default) — there is no frozen
174    /// model id at construction. Tool access goes through `kernel_handle`.
175    pub fn new(
176        engine_handle: Arc<crate::engine::EngineHandle>,
177        kernel_handle: Arc<KernelHandle>,
178        routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
179    ) -> Self {
180        Self {
181            engine_handle,
182            config: AgentRuntimeConfig::default(),
183            kernel_handle,
184            persona_manager: None,
185            tool_retriever: None,
186            routing_stats,
187            persistence_hook: None,
188            session_msg_counter: Arc::new(Mutex::new(HashMap::new())),
189        }
190    }
191
192    /// Attach a PersonaManager for persona system prompt injection.
193    pub fn with_persona_manager(mut self, pm: Arc<PersonaManager>) -> Self {
194        self.persona_manager = Some(pm);
195        self
196    }
197
198    /// Set the runtime config (overrides defaults).
199    pub fn with_config(mut self, config: AgentRuntimeConfig) -> Self {
200        self.config = config;
201        self
202    }
203
204    /// Attach a ToolRetriever for semantic capability discovery.
205    pub fn with_tool_retriever(
206        mut self,
207        retriever: Arc<crate::tools::retrieval::ToolRetriever>,
208    ) -> Self {
209        self.tool_retriever = Some(retriever);
210        self
211    }
212
213    /// Attach a PersistenceHook for autonomous persistence (RFC-016).
214    pub fn with_persistence_hook(
215        mut self,
216        hook: Arc<crate::persistence_hook::PersistenceHook>,
217    ) -> Self {
218        self.persistence_hook = Some(hook);
219        self
220    }
221
222    /// Execute a Seed by running the tool-calling agent to completion.
223    ///
224    /// 1. Resolves CSpace from persona/role/hint
225    /// 2. Registers tools via CSpace
226    /// 3. Recalls memories if available
227    /// 4. Creates Agent via `Agent::new_with_resolver()`
228    /// 5. Runs via `Agent::run_streaming()`
229    pub async fn execute(
230        &self,
231        agent_id: AgentId,
232        seed: &Seed,
233        session_ctx: &mut SessionContext,
234    ) -> Result<ExecutionResult> {
235        // RFC-015: session_id is derived from seed.id for chat transparency
236        // event publishing. Most callers run one Seed per session turn, so
237        // seed.id is a usable session identifier.
238        let session_id: Option<String> = Some(seed.id.to_string());
239        self.execute_with_session(agent_id, seed, session_ctx, session_id)
240            .await
241    }
242
243    /// Like [`execute`](Self::execute) but with an explicit session_id for
244    /// RFC-015 chat transparency event publishing.
245    pub async fn execute_with_session(
246        &self,
247        agent_id: AgentId,
248        seed: &Seed,
249        session_ctx: &mut SessionContext,
250        session_id: Option<String>,
251    ) -> Result<ExecutionResult> {
252        self.execute_inner(
253            agent_id,
254            &seed.goal,
255            &seed.original_request,
256            &seed.constraints,
257            &seed.acceptance_criteria,
258            &seed.ontology,
259            seed.cspace_hint.as_deref(),
260            &seed.mount_paths,
261            seed.workspace_context.as_deref(),
262            session_ctx,
263            session_id,
264            Some(seed),
265        )
266        .await
267    }
268
269    /// Execute a Directive with its ExecEnv (RFC-027 unified intent handling).
270    ///
271    /// Maps Directive/ExecEnv fields to the agent's runtime inputs and runs
272    /// the same tool-calling loop as [`execute`](Self::execute). The
273    /// persistence hook (RFC-016) is currently skipped on this path because
274    /// it still expects a `&Seed`; Phase 6 will update it to accept a
275    /// `&Directive`.
276    pub async fn execute_directive(
277        &self,
278        agent_id: AgentId,
279        directive: &Directive,
280        env: &ExecEnv,
281        session_ctx: &mut SessionContext,
282    ) -> Result<ExecutionResult> {
283        // Directive has no stable per-execution ID yet (Phase 6). Derive a
284        // session_id from the agent_id so chat transparency events still
285        // correlate.
286        let session_id: Option<String> = Some(agent_id.to_string());
287        self.execute_directive_with_session(agent_id, directive, env, session_ctx, session_id)
288            .await
289    }
290
291    /// Like [`execute_directive`](Self::execute_directive) but with an
292    /// explicit session_id for RFC-015 chat transparency event publishing.
293    pub async fn execute_directive_with_session(
294        &self,
295        agent_id: AgentId,
296        directive: &Directive,
297        env: &ExecEnv,
298        session_ctx: &mut SessionContext,
299        session_id: Option<String>,
300    ) -> Result<ExecutionResult> {
301        let ontology: &[Entity] = &[];
302        self.execute_inner(
303            agent_id,
304            &directive.goal,
305            &directive.original_request,
306            &directive.constraints,
307            &directive.acceptance_criteria,
308            ontology,
309            env.cspace_hint.as_deref(),
310            &env.mount_paths,
311            env.workspace_context.as_deref(),
312            session_ctx,
313            session_id,
314            None,
315        )
316        .await
317    }
318
319    /// Shared execution body for Seed and Directive paths.
320    ///
321    /// Performs the full agent-runtime pipeline: prompt assembly, capability
322    /// retrieval, memory + knowledge recall, CSpace tool registration,
323    /// model resolution, agent run, post-execution summary, and (Seed path
324    /// only) the autonomous persistence hook. Directive callers pass
325    /// `persistence_seed = None` to skip persistence until Phase 6.
326    #[allow(clippy::too_many_arguments)]
327    async fn execute_inner(
328        &self,
329        agent_id: AgentId,
330        goal: &str,
331        original_request: &str,
332        constraints: &[String],
333        acceptance_criteria: &[String],
334        ontology: &[Entity],
335        cspace_hint: Option<&str>,
336        mount_paths: &[std::path::PathBuf],
337        workspace_context: Option<&str>,
338        session_ctx: &mut SessionContext,
339        session_id: Option<String>,
340        persistence_seed: Option<&Seed>,
341    ) -> Result<ExecutionResult> {
342        let prompt = build_user_prompt_inner(goal, acceptance_criteria);
343
344        // Get active persona system prompt.
345        let persona_prompt = self
346            .persona_manager
347            .as_ref()
348            .map(|pm| pm.active_system_prompt())
349            .filter(|s| !s.trim().is_empty());
350
351        // Determine persona role for CSpace resolution.
352        let persona_role = self
353            .persona_manager
354            .as_ref()
355            .and_then(|pm| pm.get_active_persona().map(|p| p.role.clone()));
356
357        // Resolve CSpace from persona role, hint, or default.
358        let cspace = resolve_cspace(
359            cspace_hint,
360            persona_role.as_deref(),
361            Some("worker"),
362            agent_id,
363        );
364
365        // Build system prompt (without SKILL.md injection — capabilities are
366        // surfaced through the CSpace tool set + semantic retrieval instead).
367        let mut system_prompt = build_system_prompt_inner(
368            goal,
369            original_request,
370            constraints,
371            acceptance_criteria,
372            ontology,
373            workspace_context,
374            persona_prompt.as_deref(),
375            None,
376            None,
377        );
378
379        // Semantic capability retrieval: find tools relevant to this task's goal.
380        let capabilities_xml = if let Some(ref retriever) = self.tool_retriever {
381            match retriever.embedder().embed(goal).await {
382                Ok(query_vec) => {
383                    let results = retriever.retrieve(&query_vec, 8);
384                    if results.is_empty() {
385                        None
386                    } else {
387                        let xml = crate::tools::retrieval::format_capability_index(&results);
388                        tracing::info!(count = results.len(), "Retrieved relevant capabilities");
389                        Some(xml)
390                    }
391                }
392                Err(e) => {
393                    tracing::warn!(error = %e, "Failed to embed goal for retrieval");
394                    None
395                }
396            }
397        } else {
398            None
399        };
400
401        // Build kernel manifest from CSpace active domains.
402        let kernel_manifest = {
403            let domains = cspace.active_domains();
404            if domains.is_empty() {
405                None
406            } else {
407                Some(crate::tools::retrieval::build_kernel_manifest(&domains))
408            }
409        };
410
411        // Rebuild system prompt with capabilities and manifest if available.
412        if capabilities_xml.is_some() || kernel_manifest.is_some() {
413            system_prompt = build_system_prompt_inner(
414                goal,
415                original_request,
416                constraints,
417                acceptance_criteria,
418                ontology,
419                workspace_context,
420                persona_prompt.as_deref(),
421                capabilities_xml.as_deref(),
422                kernel_manifest.as_deref(),
423            );
424        }
425
426        // Blend relevant memories into system prompt.
427        let memory_manager = self.kernel_handle.agents.memory_manager();
428        match memory_manager
429            .recall_with_proactive(goal, &mut session_ctx.recall_timing)
430            .await
431        {
432            Ok(memories) if !memories.is_empty() => {
433                tracing::info!(count = memories.len(), "Recalled memories for task");
434                system_prompt = memory_manager.blend_into_prompt(&memories, &system_prompt);
435            }
436            Ok(_) => tracing::debug!("No memories recalled"),
437            Err(e) => tracing::warn!(error = %e, "Failed to recall memories"),
438        }
439
440        // Inject learned strategy from SONA (RFC-020 Phase 2).
441        if let Some(sona) = memory_manager.sona_engine() {
442            match sona.adapt(goal).await {
443                Ok(Some(pattern)) if pattern.confidence > 0.5 => {
444                    tracing::info!(
445                        domain = %pattern.domain,
446                        confidence = pattern.confidence,
447                        "SONA learned pattern injected"
448                    );
449                    system_prompt.push_str(&format!(
450                        "\n\n## Learned Strategy (confidence: {:.0}%)\n{}\n",
451                        pattern.confidence * 100.0,
452                        pattern.strategy,
453                    ));
454                }
455                Ok(_) => tracing::debug!("No high-confidence SONA pattern found"),
456                Err(e) => tracing::debug!(error = %e, "SONA adapt failed (non-fatal)"),
457            }
458        }
459
460        // Blend relevant knowledge notes into system prompt (KnowledgeLens, RFC-003 Phase 3).
461        match self
462            .kernel_handle
463            .knowledge_lens
464            .recall_for_context(goal, 5)
465            .await
466        {
467            Ok(ctx) if !ctx.notes.is_empty() => {
468                tracing::info!(
469                    notes = ctx.notes.len(),
470                    memories = ctx.memories.len(),
471                    "Recalled knowledge context for task"
472                );
473                let knowledge_blend = ctx
474                    .notes
475                    .iter()
476                    .take(3)
477                    .map(|n| format!("## {}\n\n{}", n.name, n.content))
478                    .collect::<Vec<_>>()
479                    .join("\n\n");
480                system_prompt.push_str("\n\n## Relevant Knowledge\n\n");
481                system_prompt.push_str(&knowledge_blend);
482            }
483            Ok(_) => tracing::debug!("No knowledge recalled"),
484            Err(e) => tracing::warn!(error = %e, "Failed to recall knowledge context"),
485        }
486
487        // Resolve the LIVE default model (post-hot-swap). This is the single
488        // source of truth — the same engine default the OuroborosEngine reads
489        // via the ModelResolver port. Validates fail-fast: a bad model ID set
490        // via the Web UI is rejected here at execute entry, before any tool work.
491        let engine = self.engine_handle.get();
492        let model_id = engine.default_model_id().to_string();
493        engine.resolve_model(&model_id)?;
494        // Synthetic per-execution ID for tracing. Seed path uses seed.id;
495        // Directive path mints a fresh UUID since Directive doesn't carry one.
496        let exec_id = persistence_seed
497            .map(|s| s.id)
498            .unwrap_or_else(uuid::Uuid::new_v4);
499
500        // Build the agent. Refresh config.model_id to the live value so every
501        // downstream consumer (AgentConfig, legacy provider path, usage callback)
502        // uses the same model as the interview/seed phases — no frozen boot
503        // string that silently diverges from what interview used.
504        let mut config = self.config.clone();
505        config.model_id = model_id;
506        let kernel_handle = Arc::clone(&self.kernel_handle);
507
508        // Extract audit trail from kernel for TrailAuditSink wiring.
509        let audit_trail: Option<Arc<AuditTrail>> =
510            Some(Arc::clone(&self.kernel_handle.security.audit_trail));
511
512        let (
513            mut final_content,
514            steps_completed,
515            success,
516            trajectory_steps,
517            agent,
518            tool_call_ids,
519            tool_args_map,
520            tool_error_map,
521            tool_timestamps,
522            total_input_tokens,
523            total_output_tokens,
524        ) = {
525            run_agent(
526                &config,
527                &engine,
528                kernel_handle,
529                system_prompt,
530                prompt,
531                exec_id,
532                goal.to_string(),
533                agent_id,
534                cspace,
535                audit_trail,
536                self.routing_stats.clone(),
537                session_id.clone(),
538                mount_paths,
539            )
540            .await?
541        };
542
543        // ── Post-execution: safety net for empty final content ──
544        //
545        // oxi 0.32.0 removed max_iterations — the loop now exits naturally
546        // when the LLM produces a text-only response (pi-agent behavior).
547        // This block is kept as a safety net in case the LLM returns empty
548        // text despite a natural exit (rare, but possible).
549        if final_content.is_empty() && !trajectory_steps.is_empty() {
550            let tool_summary: Vec<String> = trajectory_steps
551                .iter()
552                .enumerate()
553                .map(|(i, step)| {
554                    let truncated = if step.output.len() > 800 {
555                        // Char-boundary safe truncation: roll back to the
556                        // nearest UTF-8 boundary so multibyte sequences
557                        // (Korean, CJK, emoji) don't panic on byte slicing.
558                        let mut end = 800;
559                        while end > 0 && !step.output.is_char_boundary(end) {
560                            end -= 1;
561                        }
562                        format!("{}...", &step.output[..end])
563                    } else {
564                        step.output.clone()
565                    };
566                    format!("{}. [{}] {}", i + 1, step.input, truncated)
567                })
568                .collect();
569            let summary_prompt = format!(
570                "도구 실행 결과:\n\n{}\n\n\
571                 위 결과를 바탕으로 사용자의 요청에 대해 자연스럽게 한국어로 답변해주세요. \
572                 도구의 원시 출력을 그대로 복사하지 말고, 의미 있는 내용만 정리해서 전달하세요.",
573                tool_summary.join("\n")
574            );
575            match agent.run(summary_prompt).await {
576                Ok((response, _events)) => {
577                    if !response.content.is_empty() {
578                        tracing::info!(exec_id = %exec_id, "Post-execution summary generated");
579                        final_content = response.content;
580                    }
581                }
582                Err(e) => {
583                    tracing::warn!(error = %e, "Post-execution summary failed");
584                }
585            }
586        }
587
588        // Map trajectory steps to tool call records for the execution result.
589        // tool_call_ids[i] corresponds to trajectory_steps[i].
590        let tool_calls: Vec<oxios_ouroboros::ToolCallRecord> = trajectory_steps
591            .iter()
592            .enumerate()
593            .map(|(i, step)| {
594                let tc_id = tool_call_ids.get(i).cloned().unwrap_or_default();
595                let args_str = tool_call_ids
596                    .get(i)
597                    .and_then(|id| tool_args_map.get(id))
598                    .cloned()
599                    .unwrap_or_default();
600                let is_error = tool_call_ids
601                    .get(i)
602                    .and_then(|id| tool_error_map.get(id))
603                    .copied()
604                    .unwrap_or(false);
605                let timestamp = tool_call_ids
606                    .get(i)
607                    .and_then(|id| tool_timestamps.get(id))
608                    .copied();
609                let input_str = truncate_json_str(&args_str, 500);
610                oxios_ouroboros::ToolCallRecord {
611                    tool: step.input.clone(),
612                    input: input_str,
613                    output: step.output.clone(),
614                    duration_ms: step.duration_ms,
615                    is_error,
616                    tool_call_id: tc_id,
617                    timestamp,
618                }
619            })
620            .collect();
621
622        tracing::info!(
623            exec_id = %exec_id,
624            steps = steps_completed,
625            success,
626            tool_calls = tool_calls.len(),
627            "AgentRuntime finished"
628        );
629
630        let result = ExecutionResult {
631            output: final_content.clone(),
632            steps_completed,
633            success,
634            tool_calls,
635            tokens_input: total_input_tokens,
636            tokens_output: total_output_tokens,
637            model_id: self.engine_handle.get().default_model_id().to_string(),
638        };
639
640        // RFC-016: Autonomous persistence hook.
641        // Runs after successful execution, fire-and-forget.
642        // Only available on the Seed path today (persistence_seed is Some);
643        // the Directive path will gain its own hook adapter in Phase 6.
644        if let Some(seed) = persistence_seed
645            && success && let Some(hook) = &self.persistence_hook
646        {
647            let already_saved_knowledge = trajectory_steps
648                .iter()
649                .any(|s| s.input == "knowledge" && s.output.contains("written successfully"));
650            let hook = hook.clone();
651            let seed_clone = seed.clone();
652            let traj_clone = trajectory_steps.clone();
653            let output_clone = final_content.clone();
654            let sid = session_id.clone();
655            // Compute the assistant message index for this execution.
656            // Increment per-session counter, then use the pre-increment value.
657            let msg_index = {
658                let mut counter = self.session_msg_counter.lock();
659                let idx = counter.entry(sid.clone().unwrap_or_default()).or_insert(0);
660                let current = *idx;
661                *idx += 1;
662                current
663            };
664            tokio::spawn(async move {
665                match hook
666                    .evaluate(
667                        &seed_clone,
668                        &traj_clone,
669                        &output_clone,
670                        already_saved_knowledge,
671                    )
672                    .await
673                {
674                    Ok(plan) => {
675                        if !plan.memory.is_empty() || !plan.knowledge.is_empty() {
676                            tracing::info!(
677                                memory = plan.memory.len(),
678                                knowledge = plan.knowledge.len(),
679                                message_index = msg_index,
680                                "PersistenceHook executing plan"
681                            );
682                            let session_id = sid.unwrap_or_default();
683                            hook.execute_plan(plan, &session_id, msg_index).await;
684                        }
685                    }
686                    Err(e) => tracing::warn!(error = %e, "PersistenceHook evaluate failed"),
687                }
688            });
689        }
690
691        Ok(result)
692    }
693}
694
695/// Create and run an oxi-sdk `Agent` with CSpace-based tool registration.
696///
697/// Uses `engine.oxi().agent()` (AgentBuilder) for full middleware,
698/// observability, and security integration from oxi-sdk 0.23.0.
699#[allow(clippy::too_many_arguments)]
700async fn run_agent(
701    config: &AgentRuntimeConfig,
702    engine: &OxiosEngine,
703    kernel_handle: Arc<KernelHandle>,
704    system_prompt: String,
705    prompt: String,
706    seed_id: uuid::Uuid,
707    seed_goal: String,
708    agent_id: AgentId,
709    cspace: crate::capability::CSpace,
710    audit_trail: Option<Arc<AuditTrail>>,
711    routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
712    session_id: Option<String>,
713    mount_paths: &[std::path::PathBuf],
714) -> Result<(
715    String,
716    usize,
717    bool,
718    Vec<oxios_memory::memory::sona::TrajectoryStep>,
719    Arc<Agent>,
720    Vec<String>,
721    std::collections::HashMap<String, String>,
722    std::collections::HashMap<String, bool>,
723    std::collections::HashMap<String, chrono::DateTime<chrono::Utc>>,
724    u64,
725    u64,
726)> {
727    // Extract workspace.
728    // RFC-025: prefer the primary Mount's first path, then fall back to the
729    // legacy config.project_paths, then workspace_dir, then temp.
730    let workspace = if !mount_paths.is_empty() {
731        mount_paths[0].clone()
732    } else if !config.project_paths.is_empty() {
733        config.project_paths[0].clone()
734    } else if let Some(ref ws) = config.workspace_dir {
735        ws.clone()
736    } else {
737        std::env::temp_dir()
738            .join("oxios-agent-workspace")
739            .join(agent_id.to_string())
740    };
741
742    // Ensure workspace exists.
743    let _ = std::fs::create_dir_all(&workspace);
744
745    tracing::debug!(workspace = %workspace.display(), "Agent workspace scoped");
746
747    // Ensure all paths the agent might access are in allowed_paths.
748    //
749    // AgentLifecycleManager::ensure_permissions() adds kernel.workspace (~/.oxios/workspace),
750    // but the agent operates in different directories depending on context:
751    //
752    //   1. Process CWD — oxi-sdk 0.35+ bakes `workspace_dir` into file tools
753    //      via `with_cwd`, so ReadTool/LsTool resolve relatives against the
754    //      workspace, NOT the process CWD. However, oxios's own CSpace tools
755    //      (kernel-bridge tools wrapped in GatedTool) and bash/exec
756    //      subprocesses may still resolve against the process CWD. We grant
757    //      it as a safety net so those tools aren't denied by GatedTool.
758    //   2. The designated workspace — computed from mount_paths / workspace_dir / temp.
759    //   3. Kernel workspace — state store path for seeds, sessions, etc.
760    //   4. /tmp -- general temp file access.
761    //
762    // All four must be in allowed_paths before GatedTool wraps any tool.
763    {
764        use crate::access_manager::{Role, Subject};
765        let agent_name = format!("agent-{agent_id}");
766        let mut am = kernel_handle.exec.access_manager().lock();
767        let perms = am.get_or_create_permissions(&agent_name);
768
769        // 1. CWD -- critical: oxi-sdk resolves relative paths here
770        if let Ok(cwd) = std::env::current_dir() {
771            let cwd_pattern = format!("{}/**", cwd.to_string_lossy().trim_end_matches('/'));
772            if !perms.allowed_paths.iter().any(|p| p == &cwd_pattern) {
773                perms.allow_path(&cwd_pattern);
774                tracing::debug!(
775                    agent = %agent_name,
776                    path = %cwd_pattern,
777                    "Added CWD to agent allowed paths"
778                );
779            }
780        }
781
782        // 2. Designated workspace
783        let ws_pattern = format!("{}/**", workspace.to_string_lossy().trim_end_matches('/'));
784        if !perms.allowed_paths.iter().any(|p| p == &ws_pattern) {
785            perms.allow_path(&ws_pattern);
786        }
787
788        // 2b. RFC-025: every bound Mount grants path access.
789        //     This fixes the latent gap where only project_paths[0] was
790        //     accessible — now all Mount paths (multi-path work) are allowed.
791        //     Parent patterns already covering a path are skipped.
792        for mount_path in mount_paths {
793            let pattern = format!("{}/**", mount_path.to_string_lossy().trim_end_matches('/'));
794            if !perms.allowed_paths.iter().any(|p| p == &pattern) {
795                perms.allow_path(&pattern);
796                tracing::debug!(
797                    agent = %agent_name,
798                    path = %pattern,
799                    "Added Mount path to agent allowed paths (RFC-025)"
800                );
801            }
802        }
803
804        // 3. Kernel workspace (state store path)
805        let kernel_ws = kernel_handle
806            .state
807            .workspace_path()
808            .to_string_lossy()
809            .to_string();
810        let kernel_ws_pattern = format!("{}/**", kernel_ws.trim_end_matches('/'));
811        if kernel_ws_pattern != ws_pattern
812            && !perms.allowed_paths.iter().any(|p| p == &kernel_ws_pattern)
813        {
814            perms.allow_path(&kernel_ws_pattern);
815        }
816
817        // 4. /tmp -- for general temp file access
818        if !perms.allowed_paths.iter().any(|p| p == "/tmp/**") {
819            perms.allow_path("/tmp/**");
820        }
821
822        // Ensure RBAC Superuser role so AccessGate Layer 1 passes.
823        let rbac_subject = Subject::Agent(agent_id);
824        am.rbac_manager_mut()
825            .assign_role(rbac_subject, Role::Superuser);
826    }
827
828    // Start distributed trace span for this agent execution.
829    let _trace_guard = crate::observability::tracer().start(
830        format!("seed-{}", &seed_id.to_string()[..8]).as_str(),
831        oxi_sdk::SpanKind::Agent,
832    );
833
834    // ── Register tools based on CSpace (with access gate) ──
835    let registry = ToolRegistry::new();
836    let search_cache = Arc::new(SearchCache::new());
837
838    // Build agent context for security
839    let agent_context = AgentContext {
840        agent_id,
841        agent_name: format!("agent-{agent_id}"),
842        cspace: Arc::new(cspace.clone()),
843    };
844
845    // Build audit sink: TrailAuditSink (Merkle chain + JSONL) when audit_trail
846    // is available, otherwise fall back to TracingAuditSink.
847    let audit_sink: Arc<dyn crate::access_manager::AuditSink> = if let Some(trail) = audit_trail {
848        let audit_path = kernel_handle
849            .state
850            .workspace_path()
851            .join("audit")
852            .join("access.jsonl");
853        Arc::new(TrailAuditSink::new(trail, audit_path))
854    } else {
855        Arc::new(TracingAuditSink)
856    };
857
858    // Build access gate from kernel's security infrastructure
859    let access_gate = Arc::new(AccessGate::new(
860        kernel_handle.exec.access_manager().clone(),
861        Arc::new(kernel_handle.exec.config_snapshot()),
862        audit_sink,
863    ));
864
865    register_tools_from_cspace_gated(
866        &registry,
867        &kernel_handle,
868        &cspace,
869        search_cache,
870        agent_id,
871        access_gate,
872        agent_context,
873    );
874
875    tracing::info!(
876        seed_id = %seed_id,
877        capabilities = cspace.len(),
878        "Tools registered from CSpace"
879    );
880
881    // ── Build AgentConfig ──
882    //
883    // RFC-014 Phase D: `system_prompt` is also passed to the new
884    // `AgentBuilder::system_prompt()` (which overrides the value embedded
885    // in `AgentConfig` at build time). We clone here so the builder path
886    // can consume the value while the legacy `Agent::new_with_resolver`
887    // path still sees it in the config.
888    let agent_config = AgentConfig {
889        name: format!("agent-{agent_id}"),
890        description: None,
891        model_id: config.model_id.clone(),
892        system_prompt: Some(system_prompt.clone()),
893        timeout_seconds: 300,
894        temperature: Some(0.7),
895        max_tokens: Some(8192),
896        compaction_strategy: CompactionStrategy::Threshold(0.8),
897        compaction_instruction: None,
898        context_window: 128_000,
899        api_key: config.api_key.clone(),
900        workspace_dir: Some(workspace.clone()),
901        output_mode: None,
902        provider_options: config.provider_options.clone(),
903        // oxi-sdk 0.37.0+: ownership identity for oxi's built-in ownership-gated
904        // tools (e.g. the `issue` tool's flock). `None` preserves the pre-0.37.1
905        // behavior (ToolContext.session_id == None). Oxios runs its own tool
906        // set, so no ownership identity is needed here; set `Some(...)` only if
907        // oxios agents start using oxi ownership-gated tools.
908        session_id: None,
909    };
910
911    // ── Build Agent (RFC-014 Phase D) ──
912    //
913    // Two paths:
914    //   1. `provider_rpm == 0` (common): use oxi-sdk 0.26.2's new
915    //      `AgentBuilder` API. The builder unifies model resolution, provider
916    //      creation, and (optionally) middleware wiring. Engine-level
917    //      `authorizer` / `tracer` / `cost_tracker` are propagated through
918    //      the new builder methods.
919    //   2. `provider_rpm > 0` (rare): keep the legacy
920    //      `Agent::new_with_resolver` + `set_hooks` path because the
921    //      AgentBuilder does not expose a way to inject a pre-built
922    //      `ProviderPool` for rate-limited access. This is a deliberate
923    //      scope-limit per RFC-014/phase-d-agentbuilder.md §2 "Provider
924    //      선택 로직은 보존".
925    let agent = if config.provider_rpm > 0 {
926        // ── Legacy path: rate-limited provider pool ──
927        let resolver: Arc<dyn ProviderResolver> = Arc::new(engine.oxi().clone());
928        let provider_name = engine.resolve_model(&config.model_id)?.provider;
929        let provider = engine.pooled_provider(&provider_name, config.provider_rpm)?;
930
931        // Build middleware pipeline.
932        let mut pipeline = oxi_sdk::MiddlewarePipeline::new();
933        if config.rate_limit_per_minute > 0 {
934            pipeline = pipeline.push(oxi_sdk::middleware::builtins::RateLimitMiddleware::new(
935                config.rate_limit_per_minute,
936            ));
937        }
938        if config.token_budget > 0 {
939            pipeline = pipeline.push(oxi_sdk::middleware::builtins::TokenBudgetMiddleware::new(
940                config.token_budget,
941            ));
942        }
943        if config.audit_tool_calls {
944            pipeline = pipeline.push(oxi_sdk::middleware::builtins::LoggingMiddleware::new(
945                tracing::Level::INFO,
946            ));
947        }
948
949        // Create Agent with CSpace tool registry and provider resolver.
950        let agent = Arc::new(Agent::new_with_resolver(
951            provider,
952            agent_config,
953            Arc::new(registry),
954            resolver,
955        ));
956
957        // Wire middleware pipeline → AgentHooks.
958        if !pipeline.is_empty() {
959            let terminate_flag = Arc::new(std::sync::atomic::AtomicBool::new(false));
960            let agent_id_for_hooks = agent_id.to_string();
961            let hooks = oxi_sdk::middleware::build_hooks(
962                Arc::new(pipeline),
963                agent_id_for_hooks,
964                terminate_flag,
965            );
966            agent.set_hooks(hooks);
967        }
968
969        agent
970    } else {
971        // ── New path: AgentBuilder (RFC-014 Phase D) ──
972        let mut builder = engine
973            .oxi()
974            .agent(agent_config)
975            .workspace(&workspace)
976            .system_prompt(system_prompt);
977
978        // CSpace-based tool registration is oxios-specific and is preserved.
979        //
980        // The builder's `.tool()` method takes `impl AgentTool + 'static`
981        // (a concrete value), but oxios' CSpace tools are `Arc<dyn AgentTool>`.
982        // The SDK does not expose a way to inject a pre-built `ToolRegistry`
983        // into the builder, so we register them on the agent's tool registry
984        // after `build()` returns. This keeps CSpace semantics intact.
985        //
986        // We capture the tool names now and apply them once the agent exists.
987        let cspace_tool_arcs: Vec<Arc<dyn oxi_sdk::AgentTool>> = registry
988            .names()
989            .into_iter()
990            .filter_map(|name| registry.get(&name))
991            .collect();
992
993        // Engine-level observability/security → AgentBuilder (new API).
994        if let Some(auth) = engine.authorizer() {
995            builder = builder.authorizer(auth.clone());
996        }
997        if let Some(tracer) = engine.tracer() {
998            builder = builder.tracer(tracer.clone());
999        }
1000        if let Some(ct) = engine.cost_tracker() {
1001            builder = builder.cost_tracker(ct.clone());
1002        }
1003
1004        // Middleware: AgentBuilder convenience helpers replace the manual
1005        // `MiddlewarePipeline` + `build_hooks()` + `set_hooks()` triple.
1006        if config.rate_limit_per_minute > 0 {
1007            builder = builder.with_rate_limit(config.rate_limit_per_minute);
1008        }
1009        if config.token_budget > 0 {
1010            builder = builder.with_token_budget(config.token_budget);
1011        }
1012        if config.audit_tool_calls {
1013            builder = builder.with_logging();
1014        }
1015
1016        let built = builder.build()?;
1017        let agent = Arc::new(built);
1018
1019        // Attach CSpace tools to the agent's tool registry.
1020        // `Agent::tools()` returns the same `Arc<ToolRegistry>` that
1021        // `AgentBuilder` populated, so `register_arc` is the canonical
1022        // extension point for `Arc<dyn AgentTool>` values.
1023        let agent_tools = agent.tools();
1024        for tool in cspace_tool_arcs {
1025            agent_tools.register_arc(tool);
1026        }
1027
1028        agent
1029    };
1030
1031    // Shared mutable state for the event callback.
1032    let exec_state = Arc::new(Mutex::new(ExecuteState::default()));
1033    let exec_state_cb = Arc::clone(&exec_state);
1034    let memory_for_callback: Arc<MemoryManager> = (*kernel_handle.agents.memory_manager()).clone();
1035    let session_id_for_callback = seed_id.to_string();
1036    let model_id_for_callback = config.model_id.clone();
1037    let agent_id_for_callback = agent_id.to_string();
1038    let routing_stats_for_cb = routing_stats.clone();
1039    // RFC-015: real-time event publishing for chat transparency.
1040    // Falls back to None when the caller did not opt in.
1041    let transparency_session: Option<String> = session_id.clone();
1042    let kernel_handle_for_cb: Arc<KernelHandle> = Arc::clone(&kernel_handle);
1043
1044    // Run the agent with streaming events.
1045    let result = agent
1046        .run_streaming(prompt, move |event| {
1047            let mut s = exec_state_cb.lock();
1048            match event {
1049                AgentEvent::ToolExecutionStart {
1050                    tool_name,
1051                    tool_call_id,
1052                    args,
1053                    context,
1054                    ..
1055                } => {
1056                    // Record start time and push a placeholder step.
1057                    let idx = s.trajectory_steps.len();
1058                    s.pending_tools
1059                        .insert(tool_call_id.clone(), (std::time::Instant::now(), idx));
1060                    s.tool_args_map.insert(
1061                        tool_call_id.clone(),
1062                        serde_json::to_string(&args).unwrap_or_default(),
1063                    );
1064                    s.tool_timestamps
1065                        .insert(tool_call_id.clone(), chrono::Utc::now());
1066                    s.tool_call_ids.push(tool_call_id.clone());
1067                    s.trajectory_steps
1068                        .push(oxios_memory::memory::sona::TrajectoryStep {
1069                            input: tool_name.clone(),
1070                            output: String::new(),
1071                            duration_ms: 0,
1072                            confidence: 0.0,
1073                        });
1074                    // RFC-015: broadcast tool start so Web UI can show progress.
1075                    if let Some(ref sid) = transparency_session {
1076                        let context_json = context
1077                            .as_ref()
1078                            .map(serde_json::to_value)
1079                            .transpose()
1080                            .unwrap_or(None);
1081                        let _ =
1082                            kernel_handle_for_cb
1083                                .infra
1084                                .publish(KernelEvent::ToolExecutionStarted {
1085                                    session_id: sid.clone(),
1086                                    tool_name: tool_name.clone(),
1087                                    tool_call_id: tool_call_id.clone(),
1088                                    tool_args: args.clone(),
1089                                    context: context_json,
1090                                });
1091                    }
1092                }
1093                AgentEvent::ToolExecutionUpdate {
1094                    tool_call_id,
1095                    tool_name,
1096                    partial_result,
1097                    tab_id,
1098                    context,
1099                } => {
1100                    // RFC-015: forward real-time progress to the event bus
1101                    // so the Web UI can show a spinner and progress text
1102                    // while the tool is still executing. Best-effort —
1103                    // publish failures (e.g. lagged subscribers) are ignored.
1104                    //
1105                    // `tab_id` and `context` come from oxi-agent 0.29+
1106                    // (ToolCallContext: PageVisit, WebSearch, etc.).
1107                    // Older agent versions won't send these — they default
1108                    // to None and the UI gracefully ignores them.
1109                    if let Some(ref sid) = transparency_session {
1110                        let context_json = context
1111                            .as_ref()
1112                            .map(serde_json::to_value)
1113                            .transpose()
1114                            .unwrap_or(None);
1115                        let _ = kernel_handle_for_cb.infra.publish(
1116                            KernelEvent::ToolExecutionProgress {
1117                                session_id: sid.clone(),
1118                                tool_call_id: tool_call_id.clone(),
1119                                tool_name: tool_name.clone(),
1120                                progress: partial_result,
1121                                tab_id,
1122                                context: context_json,
1123                            },
1124                        );
1125                    }
1126                }
1127                AgentEvent::ToolExecutionEnd {
1128                    tool_name,
1129                    tool_call_id,
1130                    is_error,
1131                    result,
1132                    ..
1133                } => {
1134                    if !is_error {
1135                        s.steps_completed += 1;
1136                    }
1137                    // Look up the exact step by tool_call_id.
1138                    let mut duration_ms: u64 = 0;
1139                    let mut summary = String::new();
1140                    if let Some((start, idx)) = s.pending_tools.remove(tool_call_id.as_str()) {
1141                        duration_ms = start.elapsed().as_millis() as u64;
1142                        if let Some(step) = s.trajectory_steps.get_mut(idx) {
1143                            summary = summarize_tool_result(&result.content, 200);
1144                            step.output = summary.clone();
1145                            step.duration_ms = duration_ms;
1146                            step.confidence = if is_error { 0.3 } else { 0.8 };
1147                        }
1148                    }
1149                    s.tool_error_map.insert(tool_call_id.clone(), is_error);
1150                    // RFC-015: broadcast tool completion.
1151                    if let Some(ref sid) = transparency_session {
1152                        let _ = kernel_handle_for_cb.infra.publish(
1153                            KernelEvent::ToolExecutionFinished {
1154                                session_id: sid.clone(),
1155                                tool_call_id: tool_call_id.clone(),
1156                                tool_name: tool_name.clone(),
1157                                duration_ms,
1158                                is_error,
1159                                output_summary: summary,
1160                            },
1161                        );
1162                    }
1163                }
1164                AgentEvent::AgentEnd {
1165                    messages,
1166                    stop_reason,
1167                    ..
1168                } => {
1169                    if let Some(oxi_sdk::Message::Assistant(a)) = messages.last() {
1170                        s.final_content = a.text_content();
1171                    }
1172                    // oxi 0.32.0: loop exits naturally when LLM produces text-only
1173                    // response (StopReason::Stop). Error/Aborted = failure.
1174                    // ToolUse should not occur at AgentEnd in 0.32.0 (the loop
1175                    // continues until text-only), but treat it as non-failure
1176                    // since tool calls were executed successfully.
1177                    s.success = matches!(stop_reason.as_deref(), Some("Stop") | Some("ToolUse"));
1178                }
1179                AgentEvent::Error { message, .. } => {
1180                    s.final_content = message.clone();
1181                    s.success = false;
1182                }
1183                AgentEvent::Usage {
1184                    input_tokens,
1185                    output_tokens,
1186                } => {
1187                    // Accumulate totals for ExecutionResult.
1188                    s.total_input_tokens += input_tokens as u64;
1189                    s.total_output_tokens += output_tokens as u64;
1190
1191                    // Record token usage to cost tracker (existing).
1192                    let agent_label = format!("agent-{agent_id_for_callback}");
1193                    crate::observability::cost_tracker().record(
1194                        &agent_label,
1195                        &oxi_sdk::Model::new(
1196                            &model_id_for_callback,
1197                            &model_id_for_callback,
1198                            oxi_sdk::Api::OpenAiCompletions,
1199                            "unknown",
1200                            "https://unknown.com",
1201                        ),
1202                        oxi_sdk::TokenUsage {
1203                            input: input_tokens as u64,
1204                            output: output_tokens as u64,
1205                            cache_read: 0,
1206                            cache_write: 0,
1207                        },
1208                    );
1209
1210                    // Record to routing stats (RFC-011).
1211                    if let Some(stats) = &routing_stats_for_cb {
1212                        let cost = crate::kernel_handle::engine_api::estimate_cost(
1213                            &model_id_for_callback,
1214                            input_tokens as u64,
1215                            output_tokens as u64,
1216                        );
1217                        stats.record_model_usage(&model_id_for_callback, cost);
1218                    }
1219                    // RFC-015: publish cumulative token usage.
1220                    if let Some(ref sid) = transparency_session {
1221                        let _ = kernel_handle_for_cb
1222                            .infra
1223                            .publish(KernelEvent::TokenUsageUpdate {
1224                                session_id: sid.clone(),
1225                                input_tokens: input_tokens as u64,
1226                                output_tokens: output_tokens as u64,
1227                            });
1228                    }
1229                }
1230                AgentEvent::Compaction {
1231                    event: CompactionEvent::Completed { result, .. },
1232                } => {
1233                    handle_compaction(
1234                        result.summary.clone(),
1235                        session_id_for_callback.clone(),
1236                        memory_for_callback.clone(),
1237                    );
1238                    // RFC-015: compaction is a form of reasoning — expose it.
1239                    if let Some(ref sid) = transparency_session {
1240                        let _ =
1241                            kernel_handle_for_cb
1242                                .infra
1243                                .publish(KernelEvent::ReasoningFragment {
1244                                    session_id: sid.clone(),
1245                                    content: result.summary.clone(),
1246                                    source: "compaction".to_string(),
1247                                });
1248                    }
1249                }
1250                _ => {}
1251            }
1252        })
1253        .await;
1254
1255    // Record circuit breaker result after agent execution.
1256    let circuit = get_llm_circuit_breaker();
1257    if result.is_err() {
1258        circuit.record_failure();
1259        crate::metrics::get_metrics()
1260            .llm_circuit_breaker_state
1261            .set(1.0);
1262    } else {
1263        circuit.record_success();
1264        crate::metrics::get_metrics()
1265            .llm_circuit_breaker_state
1266            .set(0.0);
1267    }
1268
1269    if let Err(e) = result {
1270        tracing::error!(seed_id = %seed_id, error = %e, "Agent failed");
1271        let s = exec_state.lock();
1272        return Ok((
1273            format!("Agent failed: {e}"),
1274            s.steps_completed,
1275            false,
1276            s.trajectory_steps.clone(),
1277            agent,
1278            s.tool_call_ids.clone(),
1279            s.tool_args_map.clone(),
1280            s.tool_error_map.clone(),
1281            s.tool_timestamps.clone(),
1282            s.total_input_tokens,
1283            s.total_output_tokens,
1284        ));
1285    }
1286
1287    let s = exec_state.lock();
1288    tracing::info!(
1289        seed_id = %seed_id,
1290        steps = s.steps_completed,
1291        success = s.success,
1292        "Agent completed"
1293    );
1294
1295    // Record trajectory to SONA learning engine (RFC-020 Phase 2).
1296    // Fire-and-forget: don't block the result on learning.
1297    if !s.trajectory_steps.is_empty()
1298        && let Some(sona) = kernel_handle.agents.memory_manager().sona_engine()
1299    {
1300        let steps = s.trajectory_steps.clone();
1301        let success = s.success;
1302        let sona = Arc::clone(sona);
1303        let domain = infer_domain(&seed_goal);
1304        tokio::spawn(async move {
1305            let verdict = if success {
1306                oxios_memory::memory::sona::Verdict::Success
1307            } else {
1308                oxios_memory::memory::sona::Verdict::Failure
1309            };
1310            let trajectory = oxios_memory::memory::sona::Trajectory::new(steps, verdict, &domain);
1311            if let Err(e) = sona.record(trajectory).await {
1312                tracing::debug!(error = %e, "SONA trajectory recording failed (non-fatal)");
1313            }
1314        });
1315    }
1316
1317    Ok((
1318        s.final_content.clone(),
1319        s.steps_completed,
1320        s.success,
1321        s.trajectory_steps.clone(),
1322        agent,
1323        s.tool_call_ids.clone(),
1324        s.tool_args_map.clone(),
1325        s.tool_error_map.clone(),
1326        s.tool_timestamps.clone(),
1327        s.total_input_tokens,
1328        s.total_output_tokens,
1329    ))
1330}
1331
1332/// Summarize a tool result string to fit within `max_len` characters.
1333///
1334/// Uses char-aware truncation to avoid panicking on multi-byte UTF-8
1335/// (e.g., Korean, CJK, emoji).
1336fn summarize_tool_result(result: &str, max_len: usize) -> String {
1337    let trimmed = result.trim();
1338    if trimmed.chars().count() <= max_len {
1339        return trimmed.to_string();
1340    }
1341    // Take the first line or truncate.
1342    let first_line = trimmed.lines().next().unwrap_or("");
1343    if first_line.chars().count() <= max_len {
1344        first_line.to_string()
1345    } else {
1346        let take = max_len.saturating_sub(3);
1347        let truncated: String = if take == 0 {
1348            first_line.chars().take(max_len).collect()
1349        } else {
1350            first_line.chars().take(take).collect()
1351        };
1352        format!("{truncated}...")
1353    }
1354}
1355fn truncate_json_str(json_str: &str, max_len: usize) -> String {
1356    if json_str.len() <= max_len {
1357        return json_str.to_string();
1358    }
1359    // Saturating sub avoids underflow panic when max_len < 3; if there
1360    // isn't room for an ellipsis, return as many chars as fit.
1361    let take = max_len.saturating_sub(3);
1362    if take == 0 {
1363        return json_str.chars().take(max_len).collect();
1364    }
1365    let truncated: String = json_str.chars().take(take).collect();
1366    format!("{truncated}...")
1367}
1368
1369/// Infer a domain category from a seed goal for SONA trajectory grouping.
1370///
1371/// Extracts the core verb + object from the goal to create a meaningful
1372/// domain label. Falls back to "general" for unrecognizable patterns.
1373fn infer_domain(goal: &str) -> String {
1374    let lower = goal.to_lowercase();
1375    let keywords: Vec<&str> = lower.split_whitespace().take(8).collect();
1376
1377    // Check for known domain indicators.
1378    if keywords.iter().any(|k| {
1379        [
1380            "test",
1381            "tests",
1382            "spec",
1383            "testing",
1384            "assert",
1385            "unit test",
1386            "integration",
1387        ]
1388        .contains(k)
1389    }) {
1390        return "testing".to_string();
1391    }
1392    if keywords
1393        .iter()
1394        .any(|k| ["deploy", "release", "publish", "ship"].contains(k))
1395    {
1396        return "deployment".to_string();
1397    }
1398    if keywords
1399        .iter()
1400        .any(|k| ["fix", "bug", "patch", "repair", "debug"].contains(k))
1401    {
1402        return "bugfix".to_string();
1403    }
1404    if keywords
1405        .iter()
1406        .any(|k| ["refactor", "restructure", "reorganize", "rewrite"].contains(k))
1407    {
1408        return "refactoring".to_string();
1409    }
1410    if keywords
1411        .iter()
1412        .any(|k| ["doc", "document", "readme", "guide", "explain"].contains(k))
1413    {
1414        return "documentation".to_string();
1415    }
1416    if keywords
1417        .iter()
1418        .any(|k| ["build", "create", "implement", "add", "make", "new"].contains(k))
1419    {
1420        return "development".to_string();
1421    }
1422    if keywords
1423        .iter()
1424        .any(|k| ["analyze", "review", "audit", "inspect", "check"].contains(k))
1425    {
1426        return "analysis".to_string();
1427    }
1428    if keywords
1429        .iter()
1430        .any(|k| ["config", "setup", "install", "configure", "init"].contains(k))
1431    {
1432        return "configuration".to_string();
1433    }
1434
1435    // Fallback: first 2 meaningful words
1436    let meaningful: Vec<&str> = lower
1437        .split_whitespace()
1438        .filter(|w| w.len() > 2)
1439        .take(2)
1440        .collect();
1441    if meaningful.len() >= 2 {
1442        meaningful.join("_")
1443    } else {
1444        "general".to_string()
1445    }
1446}
1447
1448/// Handle compaction completion by storing the summary as a Warm memory.
1449///
1450/// Extracts the compaction summary from the event and spawns a background
1451/// task to persist it via MemoryManager. This replaces the inline 30-line
1452/// block that was previously in the event callback.
1453fn handle_compaction(summary: String, session_id: String, memory_manager: Arc<MemoryManager>) {
1454    let entry = MemoryEntry {
1455        id: uuid::Uuid::new_v4().to_string(),
1456        memory_type: MemoryType::Conversation,
1457        tier: crate::memory::MemoryTier::Warm,
1458        content: summary,
1459        content_hash: 0,
1460        source: "compaction".to_string(),
1461        session_id: Some(session_id),
1462        tags: vec![],
1463        importance: 0.5,
1464        pinned: false,
1465        protection: crate::memory::ProtectionLevel::None,
1466        auto_classified: false,
1467        session_appearances: 0,
1468        user_corrected: false,
1469        seen_in_sessions: vec![],
1470        created_at: chrono::Utc::now(),
1471        accessed_at: chrono::Utc::now(),
1472        modified_at: chrono::Utc::now(),
1473        access_count: 0,
1474        decay_score: 1.0,
1475        compaction_level: 0,
1476        compacted_from: vec![],
1477        related_ids: vec![],
1478        contradicts: None,
1479    };
1480    tokio::spawn(async move {
1481        if let Err(e) = memory_manager.remember(entry).await {
1482            tracing::warn!(error = %e, "Failed to save compaction summary");
1483        }
1484    });
1485}
1486
1487/// Build a system prompt from the Seed's goal, constraints, persona,
1488/// and optionally a capability index and kernel manifest.
1489#[allow(dead_code)]
1490fn build_system_prompt(
1491    seed: &Seed,
1492    persona_prompt: Option<&str>,
1493    capabilities_xml: Option<&str>,
1494    kernel_manifest: Option<&str>,
1495    workspace_context: Option<&str>,
1496) -> String {
1497    build_system_prompt_inner(
1498        &seed.goal,
1499        &seed.original_request,
1500        &seed.constraints,
1501        &seed.acceptance_criteria,
1502        &seed.ontology,
1503        workspace_context,
1504        persona_prompt,
1505        capabilities_xml,
1506        kernel_manifest,
1507    )
1508}
1509
1510/// Build a system prompt from a Directive and ExecEnv (RFC-027).
1511///
1512/// Maps [`Directive`] fields (`goal`, `original_request`, `constraints`,
1513/// `acceptance_criteria`) and [`ExecEnv`] fields (`workspace_context`) into
1514#[allow(dead_code)]
1515fn build_directive_system_prompt(
1516    directive: &Directive,
1517    env: &ExecEnv,
1518    persona_prompt: Option<&str>,
1519    capabilities_xml: Option<&str>,
1520    kernel_manifest: Option<&str>,
1521) -> String {
1522    let ontology: &[Entity] = &[];
1523    build_system_prompt_inner(
1524        &directive.goal,
1525        &directive.original_request,
1526        &directive.constraints,
1527        &directive.acceptance_criteria,
1528        ontology,
1529        env.workspace_context.as_deref(),
1530        persona_prompt,
1531        capabilities_xml,
1532        kernel_manifest,
1533    )
1534}
1535
1536/// Shared system-prompt builder for Seed and Directive paths.
1537///
1538/// Composes the static agent prelude, goal/constraints/criteria sections,
1539/// optional workspace context and ontology, persona, capability index, and
1540/// kernel manifest into a single prompt string. The ontology section is
1541/// Seed-only; Directive callers pass an empty slice.
1542#[allow(clippy::too_many_arguments)]
1543fn build_system_prompt_inner(
1544    goal: &str,
1545    original_request: &str,
1546    constraints: &[String],
1547    acceptance_criteria: &[String],
1548    ontology: &[Entity],
1549    workspace_context: Option<&str>,
1550    persona_prompt: Option<&str>,
1551    capabilities_xml: Option<&str>,
1552    kernel_manifest: Option<&str>,
1553) -> String {
1554    let mut prompt = String::from(
1555        "You are an autonomous agent in the Oxios operating system.\n\
1556         You execute Seeds — immutable specifications with goals, constraints, and\n\
1557         acceptance criteria.\n\n\
1558         ## Available Tools\n\
1559         You have the following tools:\n\
1560         - **File tools**: read, write, edit files; grep, find, ls for searching\n\
1561         - **Web tools**: web_search for searching the web, get_search_results for retrieving cached results\n\
1562         - **Exec**: run shell commands\n\
1563         - **Memory tools**: memory_read, memory_write, memory_search — agent's internal recall\n\
1564         - **Knowledge**: knowledge — personal markdown vault for documents and notes\n\
1565         - **Kernel tools**: agent, project, persona, cron, security, budget, resource\n\n\
1566         **Important**: When the task involves fetching information from the internet,\n\
1567         websites, or online services, use `web_search` first — do NOT search local files.\n\
1568         When the task asks to \"get\", \"fetch\", \"find online\", or \"look up\" something\n\
1569         from the web, use `web_search`.\n",
1570    );
1571    prompt.push_str(&format!("\n## Goal\n{}\n", goal));
1572
1573    // Preserve user's original wording so the agent sees exact language,
1574    // filenames, and nuances that may have been abstracted in the goal.
1575    if !original_request.is_empty() && original_request != goal {
1576        prompt.push_str(&format!(
1577            "\n## User's Original Request\n{}\n",
1578            original_request
1579        ));
1580    }
1581
1582    if !constraints.is_empty() {
1583        prompt.push_str("\n## Constraints\n");
1584        for (i, c) in constraints.iter().enumerate() {
1585            prompt.push_str(&format!("{}. {}\n", i + 1, c));
1586        }
1587    }
1588
1589    if !acceptance_criteria.is_empty() {
1590        prompt.push_str("\n## Acceptance Criteria\n");
1591        for (i, c) in acceptance_criteria.iter().enumerate() {
1592            prompt.push_str(&format!("{}. {}\n", i + 1, c));
1593        }
1594    }
1595
1596    // ── Workspace Context (RFC-025) ──
1597    // Inject active Mounts + project instructions AFTER the goal/constraints
1598    // and BEFORE the persona, so the agent sees its workspace before it acts.
1599    if let Some(ctx) = workspace_context.filter(|s| !s.trim().is_empty()) {
1600        prompt.push_str("\n## Workspace Context\n");
1601        prompt.push_str(ctx);
1602        prompt.push('\n');
1603    }
1604
1605    if !ontology.is_empty() {
1606        prompt.push_str("\n## Domain Entities\n");
1607        for e in ontology {
1608            prompt.push_str(&format!(
1609                "- **{}** ({}): {}\n",
1610                e.name, e.entity_type, e.description
1611            ));
1612        }
1613    }
1614
1615    // Inject persona system prompt
1616    if let Some(pp) = persona_prompt {
1617        prompt.push_str("\n## Persona\n");
1618        prompt.push_str(pp);
1619        prompt.push('\n');
1620    }
1621
1622    // Inject semantic capability index (from ToolRetriever)
1623    if let Some(xml) = capabilities_xml {
1624        prompt.push_str("\n## Available Capabilities\n");
1625        prompt.push_str("The following capabilities are relevant to your goal. ");
1626        prompt.push_str("Use the `read` tool to load SKILL.md for any program.\n\n");
1627        prompt.push_str(xml);
1628        prompt.push('\n');
1629    }
1630
1631    // Inject kernel manifest (from CSpace)
1632    if let Some(manifest) = kernel_manifest {
1633        prompt.push('\n');
1634        prompt.push_str(manifest);
1635        prompt.push('\n');
1636    }
1637
1638    // Execution environment guidance
1639    prompt.push_str(
1640        "\n## Execution Protocol\n\
1641         1. UNDERSTAND — Read the Seed completely before acting.\n\
1642         2. PLAN — Determine the minimal set of actions needed.\n\
1643         3. EXECUTE — Use tools to accomplish the goal. Prefer the simplest approach.\n\
1644         4. VERIFY — After each action, check the result: created a file? read it back.\n\
1645         5. REPORT — Summarize how each acceptance criterion was met, with evidence.\n\n\
1646         ## Hard Boundaries\n\
1647         - NEVER modify files outside the workspace scope\n\
1648         - NEVER execute destructive commands without confirming scope\n\
1649         - NEVER claim completion without evidence — show the output, not your opinion\n\
1650         - NEVER add features or improvements beyond the Seed scope\n\
1651         - If you cannot complete the Seed, say so and explain WHY\n\n\
1652         ## Scope Guard\n\
1653         The Seed defines your universe. Do not:\n\
1654         - Refactor code the Seed didn't mention\n\
1655         - Add tests the Seed didn't require\n\
1656         - Change configuration the Seed didn't specify\n\
1657         - \"Improve\" anything beyond what the acceptance criteria demand\n\n\
1658         ## Error Handling\n\
1659         - If a tool fails, read the error message carefully before retrying\n\
1660         - If a command fails, do NOT immediately retry with --force or sudo\n\
1661         - If stuck after 3 attempts, report the blocker rather than continuing to fail\n\n\
1662         ## Shape Matching\n\
1663         Match your output to the task: simple task → concise response.\n\
1664         Do not write 50 lines when 5 would do.\n\
1665         Use `exec` for all command execution (git, gh, osascript, etc.).",
1666    );
1667
1668    prompt
1669}
1670#[allow(dead_code)]
1671fn build_user_prompt(seed: &Seed) -> String {
1672    build_user_prompt_inner(&seed.goal, &seed.acceptance_criteria)
1673}
1674#[allow(dead_code)]
1675fn build_directive_user_prompt(directive: &Directive) -> String {
1676    build_user_prompt_inner(&directive.goal, &directive.acceptance_criteria)
1677}
1678
1679/// Shared user-prompt builder for Seed and Directive paths.
1680fn build_user_prompt_inner(goal: &str, acceptance_criteria: &[String]) -> String {
1681    format!(
1682        "Execute the following goal:\n\n{}\n\nAcceptance criteria:\n{}",
1683        goal,
1684        acceptance_criteria
1685            .iter()
1686            .enumerate()
1687            .map(|(i, c)| format!("{}. {}", i + 1, c))
1688            .collect::<Vec<_>>()
1689            .join("\n")
1690    )
1691}
1692
1693impl std::fmt::Debug for AgentRuntime {
1694    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1695        f.debug_struct("AgentRuntime")
1696            .field("model_id", &self.engine_handle.get().default_model_id())
1697            .finish()
1698    }
1699}
1700
1701#[cfg(test)]
1702mod tests {
1703    use super::*;
1704    use async_trait::async_trait;
1705    use oxi_sdk::{AgentTool, ToolContext, ToolError};
1706    use oxios_ouroboros::Entity;
1707    use serde_json::Value;
1708
1709    /// A test tool that does nothing — used to populate the registry.
1710    struct DummyTool {
1711        name: String,
1712    }
1713
1714    #[async_trait]
1715    impl AgentTool for DummyTool {
1716        fn name(&self) -> &str {
1717            &self.name
1718        }
1719        fn label(&self) -> &str {
1720            &self.name
1721        }
1722        fn description(&self) -> &str {
1723            "Test tool"
1724        }
1725        fn parameters_schema(&self) -> Value {
1726            serde_json::json!({"type": "object"})
1727        }
1728
1729        async fn execute(
1730            &self,
1731            _tool_call_id: &str,
1732            _params: Value,
1733            _shutdown: Option<tokio::sync::oneshot::Receiver<()>>,
1734            _ctx: &ToolContext,
1735        ) -> Result<oxi_sdk::AgentToolResult, ToolError> {
1736            Ok(oxi_sdk::AgentToolResult::success("ok"))
1737        }
1738    }
1739
1740    /// Test that requires_tools validation passes when all tools are present.
1741    #[test]
1742    fn test_requires_tools_validation_passes() {
1743        let registry = ToolRegistry::new();
1744
1745        registry.register(DummyTool {
1746            name: "read".into(),
1747        });
1748        registry.register(DummyTool {
1749            name: "exec".into(),
1750        });
1751
1752        let missing = registry.missing(&["read", "exec"]);
1753
1754        assert!(
1755            missing.is_empty(),
1756            "Expected no missing tools, got: {:?}",
1757            missing
1758        );
1759    }
1760
1761    /// Test that requires_tools validation fails when a tool is missing.
1762    #[test]
1763    fn test_requires_tools_validation_fails() {
1764        let registry = ToolRegistry::new();
1765
1766        registry.register(DummyTool {
1767            name: "read".into(),
1768        });
1769
1770        let missing = registry.missing(&["read", "exec", "nonexistent"]);
1771
1772        assert_eq!(missing, vec!["exec", "nonexistent"]);
1773    }
1774
1775    #[test]
1776    fn test_build_system_prompt_includes_goal() {
1777        let seed = Seed {
1778            id: uuid::Uuid::new_v4(),
1779            goal: "Build a web server".into(),
1780            constraints: vec!["Must use Rust".into()],
1781            acceptance_criteria: vec!["Server responds to requests".into()],
1782            ontology: vec![Entity {
1783                name: "HttpServer".into(),
1784                entity_type: "struct".into(),
1785                description: "The main server struct".into(),
1786            }],
1787            created_at: chrono::Utc::now(),
1788            generation: 0,
1789            parent_seed_id: None,
1790            cspace_hint: None,
1791            original_request: String::new(),
1792            output_schema: None,
1793            project_id: None,
1794            workspace_context: None,
1795            mount_paths: Vec::new(),
1796        };
1797
1798        let prompt = build_system_prompt(&seed, None, None, None, None);
1799
1800        assert!(prompt.contains("Build a web server"));
1801        assert!(prompt.contains("Must use Rust"));
1802        assert!(prompt.contains("Server responds to requests"));
1803        assert!(prompt.contains("HttpServer"));
1804        assert!(prompt.contains("struct"));
1805    }
1806
1807    #[test]
1808    fn test_build_system_prompt_empty() {
1809        let seed = Seed {
1810            id: uuid::Uuid::new_v4(),
1811            goal: "Test goal".into(),
1812            constraints: vec![],
1813            acceptance_criteria: vec![],
1814            ontology: vec![],
1815            created_at: chrono::Utc::now(),
1816            generation: 0,
1817            parent_seed_id: None,
1818            cspace_hint: None,
1819            original_request: String::new(),
1820            output_schema: None,
1821            project_id: None,
1822            workspace_context: None,
1823            mount_paths: Vec::new(),
1824        };
1825
1826        let prompt = build_system_prompt(&seed, None, None, None, None);
1827
1828        assert!(prompt.contains("Test goal"));
1829    }
1830
1831    #[test]
1832    fn test_infer_domain_testing() {
1833        assert_eq!(infer_domain("run all unit tests for the kernel"), "testing");
1834    }
1835
1836    #[test]
1837    fn test_infer_domain_deployment() {
1838        assert_eq!(
1839            infer_domain("deploy the web service to production"),
1840            "deployment"
1841        );
1842    }
1843
1844    #[test]
1845    fn test_infer_domain_bugfix() {
1846        assert_eq!(infer_domain("fix the null pointer error in main"), "bugfix");
1847    }
1848
1849    #[test]
1850    fn test_infer_domain_development() {
1851        assert_eq!(
1852            infer_domain("create a new REST API endpoint"),
1853            "development"
1854        );
1855    }
1856
1857    #[test]
1858    fn test_infer_domain_analysis() {
1859        assert_eq!(
1860            infer_domain("review the code for security issues"),
1861            "analysis"
1862        );
1863    }
1864
1865    #[test]
1866    fn test_infer_domain_fallback() {
1867        let domain = infer_domain("optimize performance metrics");
1868        // Should fall back to first 2 meaningful words
1869        assert!(!domain.is_empty());
1870    }
1871}