kernex-runtime 0.4.2

The Rust runtime for AI agents — composable engine with sandbox, providers, learning, and pipelines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! kernex-runtime: The facade crate that composes all Kernex components.
#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
//!
//! Provides `Runtime` for configuring and running an AI agent runtime
//! with sandboxed execution, multi-provider support, persistent memory,
//! skills, and multi-agent pipeline orchestration.
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use kernex_runtime::RuntimeBuilder;
//! use kernex_core::traits::Provider;
//! use kernex_core::message::Request;
//! use kernex_providers::ollama::OllamaProvider;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let runtime = RuntimeBuilder::new()
//!         .data_dir("~/.my-agent")
//!         .build()
//!         .await?;
//!
//!     let provider = OllamaProvider::from_config(
//!         "http://localhost:11434".into(),
//!         "llama3.2".into(),
//!         None,
//!     )?;
//!
//!     let request = Request::text("user-1", "Hello!");
//!     let response = runtime.complete(&provider, &request).await?;
//!     println!("{}", response.text);
//!
//!     Ok(())
//! }
//! ```

#[cfg(feature = "opentelemetry")]
pub mod telemetry;

#[cfg(feature = "sqlite-store")]
use kernex_core::config::MemoryConfig;
use kernex_core::context::{CompactionStrategy, Context, ContextNeeds};
use kernex_core::error::KernexError;
use kernex_core::guardrails::{GuardrailAction, GuardrailRunner};
use kernex_core::hooks::{HookRunner, NoopHookRunner};
use kernex_core::message::{CompletionMeta, Request, Response};
use kernex_core::permissions::PermissionRules;
use kernex_core::run::{RunConfig, RunOutcome};
use kernex_core::stream::StreamEvent;
use kernex_core::traits::Provider;
use kernex_core::traits::StreamingProvider;
use kernex_core::traits::Summarizer;
#[cfg(feature = "sqlite-store")]
use kernex_memory::{Store, UsageBreakdown};
use kernex_skills::{
    build_skill_prompt, match_skill_toolboxes, match_skill_triggers, Project, Skill,
};
use std::sync::Arc;

/// Re-export sub-crates for convenience.
pub use kernex_core as core;
#[cfg(feature = "sqlite-store")]
pub use kernex_memory as memory;
pub use kernex_pipelines as pipelines;
pub use kernex_providers as providers;
pub use kernex_sandbox as sandbox;
pub use kernex_skills as skills;

/// A configured Kernex runtime with all subsystems initialized.
pub struct Runtime {
    /// Persistent memory store.
    #[cfg(feature = "sqlite-store")]
    pub store: Store,
    /// Loaded skills from the data directory.
    pub skills: Vec<Skill>,
    /// Loaded projects from the data directory.
    pub projects: Vec<Project>,
    /// Data directory path (expanded).
    pub data_dir: String,
    /// Base system prompt prepended to every request.
    pub system_prompt: String,
    /// Communication channel identifier (e.g. "cli", "api", "slack").
    pub channel: String,
    /// Active project key for scoping memory and lessons.
    pub project: Option<String>,
    /// Hook runner for tool lifecycle events.
    pub hook_runner: Arc<dyn HookRunner>,
    /// Declarative allow/deny rules applied before each tool call.
    pub permission_rules: Option<Arc<PermissionRules>>,
    /// Optional guardrail applied to input before provider call and output after.
    pub guardrail_runner: Option<Arc<dyn GuardrailRunner>>,
    /// When true, conversations whose history exceeds `max_context_messages`
    /// have their overflow summarized via the active provider instead of
    /// silently dropped. See [`RuntimeBuilder::auto_compact`].
    pub auto_compact: bool,
}

/// Adapter that lets `Store::build_context` reuse the active provider as a
/// summarizer when [`Runtime::auto_compact`] is enabled.
///
/// Wraps a `&dyn Provider` and answers [`Summarizer::summarize`] by sending a
/// fixed instruction prompt through the same provider that is handling the
/// turn. Costs one extra round-trip per overflow event (not per turn). The
/// summarizer is constructed per-call inside the runtime; it does not persist
/// state across requests, so there's no risk of summary drift.
struct ProviderSummarizer<'a> {
    provider: &'a dyn Provider,
}

#[async_trait::async_trait]
impl Summarizer for ProviderSummarizer<'_> {
    async fn summarize(&self, text: &str) -> Result<String, KernexError> {
        // Tight, role-stable prompt. The model never sees the original system
        // prompt (we deliberately call with an empty one) so its output is
        // a pure summary, not another agent reply.
        let instruction = format!(
            "You are a conversation summarizer. Summarize the following \
             exchange in 200 words or fewer. Focus on: decisions made, files \
             touched, errors encountered, and unresolved questions. Skip \
             greetings and small talk. Output the summary only — no preamble.\n\n\
             ---\n{text}\n---"
        );
        let mut ctx = Context::new(&instruction);
        ctx.system_prompt.clear();
        let response = self.provider.complete(&ctx).await?;
        Ok(response.text)
    }
}

impl Runtime {
    /// Send a request through the full runtime pipeline:
    /// build context from memory → enrich with skills → complete via provider → save exchange.
    ///
    /// This is the high-level convenience method that wires together all
    /// Kernex subsystems in a single call.
    pub async fn complete(
        &self,
        provider: &dyn Provider,
        request: &Request,
    ) -> Result<Response, KernexError> {
        self.complete_with_needs(provider, request, &ContextNeeds::default())
            .await
    }

    /// Like [`complete`](Self::complete), but with explicit control over which
    /// context blocks are loaded from memory.
    #[tracing::instrument(
        name = "kernex.complete",
        skip_all,
        fields(provider = provider.name(), sender = %request.sender_id)
    )]
    pub async fn complete_with_needs(
        &self,
        provider: &dyn Provider,
        request: &Request,
        #[allow(unused_variables)] needs: &ContextNeeds,
    ) -> Result<Response, KernexError> {
        let project_ref = self.project.as_deref();

        // Input guardrail: check (and optionally sanitize) the request text
        // before it reaches the provider or is stored in memory.
        let owned_req;
        let request = if let Some(gr) = &self.guardrail_runner {
            match gr.check_input(&request.text).await {
                GuardrailAction::Allow => request,
                GuardrailAction::Block(reason) => return Err(KernexError::Guardrail(reason)),
                GuardrailAction::Sanitize(clean) => {
                    owned_req = Request {
                        text: clean,
                        ..request.clone()
                    };
                    &owned_req
                }
            }
        } else {
            request
        };

        // Build skill context (prompt block + optional model override).
        let skill_ctx = build_skill_prompt(&self.skills);
        let full_system_prompt = if skill_ctx.prompt.is_empty() {
            self.system_prompt.clone()
        } else if self.system_prompt.is_empty() {
            skill_ctx.prompt.clone()
        } else {
            format!("{}\n\n{}", self.system_prompt, skill_ctx.prompt)
        };

        // Build context from memory (history, recall, facts, lessons, etc).
        #[cfg(feature = "sqlite-store")]
        let mut context = {
            let (effective_needs, summarizer): (
                std::borrow::Cow<'_, ContextNeeds>,
                Option<ProviderSummarizer<'_>>,
            ) = if self.auto_compact {
                let mut owned = needs.clone();
                owned.compact = CompactionStrategy::Summarize;
                (
                    std::borrow::Cow::Owned(owned),
                    Some(ProviderSummarizer { provider }),
                )
            } else {
                (std::borrow::Cow::Borrowed(needs), None)
            };
            self.store
                .build_context(
                    &self.channel,
                    request,
                    &full_system_prompt,
                    &effective_needs,
                    project_ref,
                    summarizer.as_ref().map(|s| s as &dyn Summarizer),
                )
                .await?
        };

        #[cfg(not(feature = "sqlite-store"))]
        let mut context = {
            let mut ctx = kernex_core::context::Context::new(&request.text);
            ctx.system_prompt = full_system_prompt;
            ctx
        };

        // Apply skill model override when no model was already set on context.
        if context.model.is_none() {
            context.model = skill_ctx.model;
        }

        // Enrich context with triggered MCP servers.
        let mcp_servers = match_skill_triggers(&self.skills, &request.text);
        if !mcp_servers.is_empty() {
            context.mcp_servers = mcp_servers;
        }

        // Enrich context with triggered toolboxes.
        let toolboxes = match_skill_toolboxes(&self.skills, &request.text);
        if !toolboxes.is_empty() {
            context.toolboxes = toolboxes;
        }

        // Wire hooks and permission rules into context.
        context.hook_runner = Some(self.hook_runner.clone());
        context.permission_rules = self.permission_rules.clone();

        // Send to provider.
        let raw_response = provider.complete(&context).await?;

        // Output guardrail: check (and optionally sanitize) the response text.
        let response = if let Some(gr) = &self.guardrail_runner {
            match gr.check_output(&raw_response.text).await {
                GuardrailAction::Allow => raw_response,
                GuardrailAction::Block(reason) => return Err(KernexError::Guardrail(reason)),
                GuardrailAction::Sanitize(clean) => Response {
                    text: clean,
                    metadata: raw_response.metadata,
                },
            }
        } else {
            raw_response
        };

        // Persist exchange in memory.
        #[allow(unused_variables)]
        let project_key = project_ref.unwrap_or("default");

        #[cfg(feature = "sqlite-store")]
        self.store
            .store_exchange(&self.channel, request, &response, project_key)
            .await?;

        // Record token usage if the provider reported a count.
        #[cfg(feature = "sqlite-store")]
        if let Some(tokens) = response.metadata.tokens_used {
            let model = response.metadata.model.as_deref().unwrap_or("unknown");
            let session = response.metadata.session_id.as_deref().unwrap_or("default");
            let breakdown = UsageBreakdown {
                input_tokens: response.metadata.input_tokens,
                output_tokens: response.metadata.output_tokens,
                cache_read_tokens: response.metadata.cache_read_tokens,
                cache_creation_tokens: response.metadata.cache_creation_tokens,
            };
            if let Err(e) = self
                .store
                .record_usage_full(&request.sender_id, session, tokens, model, breakdown)
                .await
            {
                tracing::warn!("failed to record token usage: {e}");
            }
        }

        Ok(response)
    }

    /// Stream a request through the runtime pipeline, returning events as they arrive.
    ///
    /// Builds context from memory, enriches with skills, opens a streaming connection
    /// to the provider, and persists the exchange to memory after the stream completes.
    /// Returns a channel receiver that yields [`StreamEvent`]s until `Done` or `Error`.
    pub async fn complete_stream(
        &self,
        provider: &dyn StreamingProvider,
        request: &Request,
    ) -> Result<tokio::sync::mpsc::Receiver<StreamEvent>, KernexError> {
        self.complete_stream_with_needs(provider, request, &ContextNeeds::default())
            .await
    }

    /// Like [`complete_stream`](Self::complete_stream), but with explicit control over which
    /// context blocks are loaded from memory.
    #[tracing::instrument(
        name = "kernex.stream",
        skip_all,
        fields(provider = provider.name(), sender = %request.sender_id)
    )]
    pub async fn complete_stream_with_needs(
        &self,
        provider: &dyn StreamingProvider,
        request: &Request,
        #[allow(unused_variables)] needs: &ContextNeeds,
    ) -> Result<tokio::sync::mpsc::Receiver<StreamEvent>, KernexError> {
        let project_ref = self.project.as_deref();

        // Input guardrail: check (and optionally sanitize) the request text
        // before the stream is opened. Block returns early; Sanitize clones the request.
        let owned_req;
        let request = if let Some(gr) = &self.guardrail_runner {
            match gr.check_input(&request.text).await {
                GuardrailAction::Allow => request,
                GuardrailAction::Block(reason) => return Err(KernexError::Guardrail(reason)),
                GuardrailAction::Sanitize(clean) => {
                    owned_req = Request {
                        text: clean,
                        ..request.clone()
                    };
                    &owned_req
                }
            }
        } else {
            request
        };

        let skill_ctx = build_skill_prompt(&self.skills);
        let full_system_prompt = if skill_ctx.prompt.is_empty() {
            self.system_prompt.clone()
        } else if self.system_prompt.is_empty() {
            skill_ctx.prompt.clone()
        } else {
            format!("{}\n\n{}", self.system_prompt, skill_ctx.prompt)
        };

        #[cfg(feature = "sqlite-store")]
        let mut context = {
            let (effective_needs, summarizer): (
                std::borrow::Cow<'_, ContextNeeds>,
                Option<ProviderSummarizer<'_>>,
            ) = if self.auto_compact {
                let mut owned = needs.clone();
                owned.compact = CompactionStrategy::Summarize;
                (
                    std::borrow::Cow::Owned(owned),
                    Some(ProviderSummarizer { provider }),
                )
            } else {
                (std::borrow::Cow::Borrowed(needs), None)
            };
            self.store
                .build_context(
                    &self.channel,
                    request,
                    &full_system_prompt,
                    &effective_needs,
                    project_ref,
                    summarizer.as_ref().map(|s| s as &dyn Summarizer),
                )
                .await?
        };

        #[cfg(not(feature = "sqlite-store"))]
        let mut context = {
            let mut ctx = kernex_core::context::Context::new(&request.text);
            ctx.system_prompt = full_system_prompt;
            ctx
        };

        if context.model.is_none() {
            context.model = skill_ctx.model;
        }

        let mcp_servers = match_skill_triggers(&self.skills, &request.text);
        if !mcp_servers.is_empty() {
            context.mcp_servers = mcp_servers;
        }
        let toolboxes = match_skill_toolboxes(&self.skills, &request.text);
        if !toolboxes.is_empty() {
            context.toolboxes = toolboxes;
        }

        context.hook_runner = Some(self.hook_runner.clone());
        context.permission_rules = self.permission_rules.clone();

        // Open streaming connection to provider.
        let provider_name = provider.name().to_string();
        let mut upstream = provider.complete_stream(&context).await?;

        // Forwarding channel returned to the caller.
        let (tx, rx) = tokio::sync::mpsc::channel::<StreamEvent>(64);

        // Background task: forward events and persist exchange when done.
        #[cfg(feature = "sqlite-store")]
        let store = self.store.clone();
        let channel = self.channel.clone();
        let request_clone = request.clone();
        #[allow(unused_variables)]
        let project_key = project_ref.unwrap_or("default").to_string();
        let guardrail_runner = self.guardrail_runner.clone();

        tokio::spawn(async move {
            use kernex_core::stream::{StreamAccumulator, StreamEvent as SE};
            let mut acc = StreamAccumulator::new();
            let started = std::time::Instant::now();

            while let Some(event) = upstream.recv().await {
                acc.push(&event);
                let is_terminal = matches!(event, SE::Done | SE::Error(_));
                // Best-effort forward; drop silently if receiver was dropped.
                let _ = tx.send(event).await;
                if is_terminal {
                    break;
                }
            }

            // Persist accumulated exchange to memory.
            // Output guardrail runs on the full accumulated text before storage.
            // The stream has already been forwarded to the caller so the guardrail
            // only affects what is persisted — it does not modify the streamed tokens.
            #[cfg(feature = "sqlite-store")]
            {
                let elapsed_ms = started.elapsed().as_millis() as u64;
                let accumulated = acc.into_text();
                let persisted_text = if let Some(gr) = &guardrail_runner {
                    match gr.check_output(&accumulated).await {
                        GuardrailAction::Allow => accumulated,
                        GuardrailAction::Block(_) => String::new(),
                        GuardrailAction::Sanitize(clean) => clean,
                    }
                } else {
                    accumulated
                };
                let response = Response {
                    text: persisted_text,
                    metadata: CompletionMeta {
                        provider_used: provider_name,
                        tokens_used: None,
                        processing_time_ms: elapsed_ms,
                        model: None,
                        session_id: None,
                        ..Default::default()
                    },
                };
                if let Err(e) = store
                    .store_exchange(&channel, &request_clone, &response, &project_key)
                    .await
                {
                    tracing::warn!("failed to persist streaming exchange: {e}");
                }
            }
            #[cfg(not(feature = "sqlite-store"))]
            {
                let _ = acc;
                let _ = started;
                let _ = provider_name;
                let _ = guardrail_runner;
            }
        });

        Ok(rx)
    }

    /// Run the agent with explicit lifecycle control.
    ///
    /// Sets `max_turns` in context so the provider's agentic loop respects it,
    /// wires the runtime hook runner, calls the provider, fires the `on_stop`
    /// hook, and wraps the outcome in [`RunOutcome`].
    #[tracing::instrument(
        name = "kernex.run",
        skip_all,
        fields(provider = provider.name(), sender = %request.sender_id, turns = config.max_turns)
    )]
    pub async fn run(
        &self,
        provider: &dyn Provider,
        request: &Request,
        config: &RunConfig,
    ) -> Result<RunOutcome, KernexError> {
        let needs = ContextNeeds::default();
        let project_ref = self.project.as_deref();

        // Input guardrail.
        let owned_req;
        let request = if let Some(gr) = &self.guardrail_runner {
            match gr.check_input(&request.text).await {
                GuardrailAction::Allow => request,
                GuardrailAction::Block(reason) => return Err(KernexError::Guardrail(reason)),
                GuardrailAction::Sanitize(clean) => {
                    owned_req = Request {
                        text: clean,
                        ..request.clone()
                    };
                    &owned_req
                }
            }
        } else {
            request
        };

        let skill_ctx = build_skill_prompt(&self.skills);
        let full_system_prompt = if skill_ctx.prompt.is_empty() {
            self.system_prompt.clone()
        } else if self.system_prompt.is_empty() {
            skill_ctx.prompt.clone()
        } else {
            format!("{}\n\n{}", self.system_prompt, skill_ctx.prompt)
        };

        #[cfg(feature = "sqlite-store")]
        let mut context = {
            let (effective_needs, summarizer): (
                std::borrow::Cow<'_, ContextNeeds>,
                Option<ProviderSummarizer<'_>>,
            ) = if self.auto_compact {
                let mut owned = needs.clone();
                owned.compact = CompactionStrategy::Summarize;
                (
                    std::borrow::Cow::Owned(owned),
                    Some(ProviderSummarizer { provider }),
                )
            } else {
                (std::borrow::Cow::Borrowed(&needs), None)
            };
            self.store
                .build_context(
                    &self.channel,
                    request,
                    &full_system_prompt,
                    &effective_needs,
                    project_ref,
                    summarizer.as_ref().map(|s| s as &dyn Summarizer),
                )
                .await?
        };

        #[cfg(not(feature = "sqlite-store"))]
        let mut context = {
            let mut ctx = kernex_core::context::Context::new(&request.text);
            ctx.system_prompt = full_system_prompt;
            ctx
        };

        // Apply skill model override when no model was already set on context.
        if context.model.is_none() {
            context.model = skill_ctx.model;
        }

        let mcp_servers = match_skill_triggers(&self.skills, &request.text);
        if !mcp_servers.is_empty() {
            context.mcp_servers = mcp_servers;
        }
        let toolboxes = match_skill_toolboxes(&self.skills, &request.text);
        if !toolboxes.is_empty() {
            context.toolboxes = toolboxes;
        }

        // Set max_turns, hooks, and permission rules.
        context.max_turns = Some(config.max_turns);
        context.hook_runner = Some(self.hook_runner.clone());
        context.permission_rules = self.permission_rules.clone();

        let raw_response = provider.complete(&context).await?;

        // Output guardrail.
        let response = if let Some(gr) = &self.guardrail_runner {
            match gr.check_output(&raw_response.text).await {
                GuardrailAction::Allow => raw_response,
                GuardrailAction::Block(reason) => return Err(KernexError::Guardrail(reason)),
                GuardrailAction::Sanitize(clean) => Response {
                    text: clean,
                    metadata: raw_response.metadata,
                },
            }
        } else {
            raw_response
        };

        // Fire on_stop hook.
        self.hook_runner.on_stop(&response.text).await;

        // Persist exchange.
        #[allow(unused_variables)]
        let project_key = project_ref.unwrap_or("default");
        #[cfg(feature = "sqlite-store")]
        self.store
            .store_exchange(&self.channel, request, &response, project_key)
            .await?;

        // Record token usage if the provider reported a count.
        #[cfg(feature = "sqlite-store")]
        if let Some(tokens) = response.metadata.tokens_used {
            let model = response.metadata.model.as_deref().unwrap_or("unknown");
            let session = response.metadata.session_id.as_deref().unwrap_or("default");
            let breakdown = UsageBreakdown {
                input_tokens: response.metadata.input_tokens,
                output_tokens: response.metadata.output_tokens,
                cache_read_tokens: response.metadata.cache_read_tokens,
                cache_creation_tokens: response.metadata.cache_creation_tokens,
            };
            if let Err(e) = self
                .store
                .record_usage_full(&request.sender_id, session, tokens, model, breakdown)
                .await
            {
                tracing::warn!("failed to record token usage: {e}");
            }
        }

        Ok(RunOutcome::EndTurn(response))
    }
}

/// Builder for constructing a `Runtime` with the desired configuration.
pub struct RuntimeBuilder {
    data_dir: String,
    #[cfg(feature = "sqlite-store")]
    db_path: Option<String>,
    system_prompt: String,
    channel: String,
    project: Option<String>,
    hook_runner: Option<Arc<dyn HookRunner>>,
    permission_rules: Option<Arc<PermissionRules>>,
    guardrail_runner: Option<Arc<dyn GuardrailRunner>>,
    auto_compact: bool,
}

impl RuntimeBuilder {
    /// Create a new builder with default settings.
    pub fn new() -> Self {
        Self {
            data_dir: "~/.kernex".to_string(),
            #[cfg(feature = "sqlite-store")]
            db_path: None,
            system_prompt: String::new(),
            channel: "cli".to_string(),
            project: None,
            hook_runner: None,
            permission_rules: None,
            guardrail_runner: None,
            // Default off for backward compatibility with v0.4.0 callers.
            // kernex-agent flips it on; future major versions may default it on.
            auto_compact: false,
        }
    }

    /// Create a new builder pre-populated from a declarative agent definition file.
    ///
    /// The file format is detected by extension: `.yaml` / `.yml` use YAML
    /// (requires the `yaml` feature on `kernex-core`); all other extensions
    /// use TOML. Missing files silently fall back to defaults.
    ///
    /// Maps `[runtime]` fields (`data_dir`, `system_prompt`, `channel`,
    /// `project`) and `[memory]` → `db_path` into the builder. Provider
    /// selection is left to the caller.
    ///
    /// # Example (agent.toml)
    ///
    /// ```toml
    /// [runtime]
    /// name       = "my-agent"
    /// data_dir   = "~/.my-agent"
    /// channel    = "api"
    /// project    = "acme"
    /// system_prompt = "You are a helpful coding assistant."
    ///
    /// [memory]
    /// db_path = "~/.my-agent/memory.db"
    /// ```
    pub fn from_file(path: &str) -> Result<Self, kernex_core::error::KernexError> {
        let config = kernex_core::config::load_file(path)?;
        Ok(Self::from_config(&config))
    }

    /// Populate a builder from a pre-parsed [`KernexConfig`].
    ///
    /// Maps `runtime.{data_dir, system_prompt, channel, project}` and
    /// `memory.db_path` into builder fields. Provider selection is left
    /// to the caller.
    ///
    /// [`KernexConfig`]: kernex_core::config::KernexConfig
    pub fn from_config(config: &kernex_core::config::KernexConfig) -> Self {
        let mut builder = Self::new()
            .data_dir(&config.runtime.data_dir)
            .system_prompt(&config.runtime.system_prompt)
            .channel(&config.runtime.channel);

        if let Some(proj) = &config.runtime.project {
            builder = builder.project(proj);
        }

        #[cfg(feature = "sqlite-store")]
        {
            builder = builder.db_path(&config.memory.db_path);
        }

        builder
    }

    /// Create a new builder configured from environment variables.
    ///
    /// Recognizes:
    /// - `KERNEX_DATA_DIR`
    /// - `KERNEX_DB_PATH` (when `sqlite-store` feature is enabled)
    /// - `KERNEX_SYSTEM_PROMPT`
    /// - `KERNEX_CHANNEL`
    /// - `KERNEX_PROJECT`
    pub fn from_env() -> Self {
        let mut builder = Self::new();

        if let Ok(dir) = std::env::var("KERNEX_DATA_DIR") {
            warn_if_data_dir_unusual(&dir);
            builder = builder.data_dir(&dir);
        }
        #[cfg(feature = "sqlite-store")]
        if let Ok(path) = std::env::var("KERNEX_DB_PATH") {
            builder = builder.db_path(&path);
        }
        if let Ok(prompt) = std::env::var("KERNEX_SYSTEM_PROMPT") {
            builder = builder.system_prompt(&prompt);
        }
        if let Ok(channel) = std::env::var("KERNEX_CHANNEL") {
            builder = builder.channel(&channel);
        }
        if let Ok(project) = std::env::var("KERNEX_PROJECT") {
            builder = builder.project(&project);
        }

        builder
    }

    /// Set the data directory (default: `~/.kernex`).
    pub fn data_dir(mut self, path: &str) -> Self {
        self.data_dir = path.to_string();
        self
    }

    /// Set a custom database path (default: `{data_dir}/memory.db`).
    #[cfg(feature = "sqlite-store")]
    pub fn db_path(mut self, path: &str) -> Self {
        self.db_path = Some(path.to_string());
        self
    }

    /// Set the base system prompt.
    pub fn system_prompt(mut self, prompt: &str) -> Self {
        self.system_prompt = prompt.to_string();
        self
    }

    /// Set the channel identifier (default: `"cli"`).
    pub fn channel(mut self, channel: &str) -> Self {
        self.channel = channel.to_string();
        self
    }

    /// Set the active project for scoping memory.
    pub fn project(mut self, project: &str) -> Self {
        self.project = Some(project.to_string());
        self
    }

    /// Set a hook runner for tool lifecycle events.
    pub fn hook_runner(mut self, runner: Arc<dyn HookRunner>) -> Self {
        self.hook_runner = Some(runner);
        self
    }

    /// Set declarative allow/deny permission rules for tool calls.
    pub fn permission_rules(mut self, rules: PermissionRules) -> Self {
        self.permission_rules = Some(Arc::new(rules));
        self
    }

    /// Set a guardrail runner that intercepts and filters input/output text.
    pub fn guardrail_runner(mut self, runner: Arc<dyn GuardrailRunner>) -> Self {
        self.guardrail_runner = Some(runner);
        self
    }

    /// Enable automatic context compaction on long conversations.
    ///
    /// When enabled, every call into the runtime that builds context (e.g.
    /// [`Runtime::complete`], [`Runtime::complete_stream`], [`Runtime::run`])
    /// will, on overflow past `max_context_messages`, summarize the dropped
    /// rows via the active provider and prepend the summary to the system
    /// prompt under `[Earlier conversation summary]`. The summarization
    /// adds one extra provider round-trip per overflow event (not per turn),
    /// uses a fixed instruction prompt that never reveals the agent's own
    /// system prompt, and falls back to the default Drop behavior if the
    /// provider call fails.
    ///
    /// Default is **off** to preserve v0.4.0 behavior. Recommended for any
    /// long-running interactive session; the default exists only so existing
    /// callers do not silently change billing characteristics.
    pub fn auto_compact(mut self, enable: bool) -> Self {
        self.auto_compact = enable;
        self
    }

    /// Build and initialize the runtime.
    pub async fn build(self) -> Result<Runtime, KernexError> {
        let expanded_dir = kernex_core::shellexpand(&self.data_dir);

        // Ensure data directory exists.
        tokio::fs::create_dir_all(&expanded_dir)
            .await
            .map_err(|e| KernexError::Config(format!("failed to create data dir: {e}")))?;

        // Initialize store.
        #[cfg(feature = "sqlite-store")]
        let store = {
            let db_path = self
                .db_path
                .unwrap_or_else(|| format!("{expanded_dir}/memory.db"));
            let mem_config = MemoryConfig {
                db_path: db_path.clone(),
                ..Default::default()
            };
            Store::new(&mem_config).await?
        };

        // Load skills and projects. These functions use synchronous std::fs
        // internally; offload to a blocking thread so we do not stall the
        // tokio executor on cold start (especially relevant for projects with
        // large skills/ trees).
        let skills_data_dir = self.data_dir.clone();
        let skills =
            tokio::task::spawn_blocking(move || kernex_skills::load_skills(&skills_data_dir))
                .await
                .map_err(|e| KernexError::Skill(format!("load_skills task failed: {e}")))?;
        let projects_data_dir = self.data_dir.clone();
        let projects =
            tokio::task::spawn_blocking(move || kernex_skills::load_projects(&projects_data_dir))
                .await
                .map_err(|e| KernexError::Skill(format!("load_projects task failed: {e}")))?;

        tracing::info!(
            "runtime initialized: {} skills, {} projects",
            skills.len(),
            projects.len()
        );

        let hook_runner: Arc<dyn HookRunner> =
            self.hook_runner.unwrap_or_else(|| Arc::new(NoopHookRunner));

        Ok(Runtime {
            #[cfg(feature = "sqlite-store")]
            store,
            skills,
            projects,
            data_dir: expanded_dir,
            system_prompt: self.system_prompt,
            channel: self.channel,
            project: self.project,
            hook_runner,
            permission_rules: self.permission_rules,
            guardrail_runner: self.guardrail_runner,
            auto_compact: self.auto_compact,
        })
    }
}

impl Default for RuntimeBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Emit a warning when `KERNEX_DATA_DIR` resolves to a path outside the
/// usual locations. Misconfigured env on a shared host (e.g.
/// `KERNEX_DATA_DIR=/etc`) means writes happen in places the operator
/// likely didn't intend; the sandbox's blocklist policy still allows
/// writes outside the configured data dir, so the agent could end up
/// writing `/etc/cron.d/` before anyone notices.
fn warn_if_data_dir_unusual(dir: &str) {
    // Only act on absolute paths; relative paths are project-scoped and
    // resolve under cwd, which is always operator-chosen.
    let path = std::path::Path::new(dir);
    if !path.is_absolute() {
        return;
    }
    let s = dir;
    let in_home = std::env::var("HOME")
        .ok()
        .map(|h| !h.is_empty() && s.starts_with(&h))
        .unwrap_or(false);
    let usual = in_home
        || s.starts_with("/tmp/")
        || s.starts_with("/var/")
        || s.starts_with("/Users/")
        || s.starts_with("/home/")
        || s == "/tmp"
        || s == "/var";
    if !usual {
        tracing::warn!(
            data_dir = %dir,
            "KERNEX_DATA_DIR resolves outside $HOME / /tmp / /var — \
             writes may land in unexpected locations"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_runtime_builder_creates_runtime() {
        let tmp = std::env::temp_dir().join("__kernex_test_runtime__");
        let _ = std::fs::remove_dir_all(&tmp);

        let runtime = RuntimeBuilder::new()
            .data_dir(tmp.to_str().unwrap())
            .build()
            .await
            .unwrap();

        assert!(runtime.skills.is_empty());
        assert!(runtime.projects.is_empty());
        assert!(runtime.system_prompt.is_empty());
        assert_eq!(runtime.channel, "cli");
        assert!(runtime.project.is_none());
        assert!(std::path::Path::new(&runtime.data_dir).exists());

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[tokio::test]
    async fn test_runtime_builder_custom_db_path() {
        let tmp = std::env::temp_dir().join("__kernex_test_runtime_db__");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        let db = tmp.join("custom.db");
        let runtime = RuntimeBuilder::new()
            .data_dir(tmp.to_str().unwrap())
            .db_path(db.to_str().unwrap())
            .build()
            .await
            .unwrap();

        assert!(db.exists());
        drop(runtime);
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[tokio::test]
    async fn test_runtime_builder_with_config() {
        let tmp = std::env::temp_dir().join("__kernex_test_runtime_cfg__");
        let _ = std::fs::remove_dir_all(&tmp);

        let runtime = RuntimeBuilder::new()
            .data_dir(tmp.to_str().unwrap())
            .system_prompt("You are helpful.")
            .channel("api")
            .project("my-project")
            .build()
            .await
            .unwrap();

        assert_eq!(runtime.system_prompt, "You are helpful.");
        assert_eq!(runtime.channel, "api");
        assert_eq!(runtime.project, Some("my-project".to_string()));

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[tokio::test]
    async fn test_runtime_builder_from_config() {
        use kernex_core::config::{KernexConfig, RuntimeConfig};

        let tmp = std::env::temp_dir().join("__kernex_test_from_config__");
        let _ = std::fs::remove_dir_all(&tmp);

        let cfg = KernexConfig {
            runtime: RuntimeConfig {
                name: "test-agent".to_string(),
                data_dir: tmp.to_str().unwrap().to_string(),
                channel: "slack".to_string(),
                project: Some("my-proj".to_string()),
                system_prompt: "Be concise.".to_string(),
                ..RuntimeConfig::default()
            },
            ..KernexConfig::default()
        };

        let runtime = RuntimeBuilder::from_config(&cfg).build().await.unwrap();

        assert_eq!(runtime.channel, "slack");
        assert_eq!(runtime.project, Some("my-proj".to_string()));
        assert_eq!(runtime.system_prompt, "Be concise.");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[tokio::test]
    async fn test_runtime_builder_from_file_toml() {
        use std::io::Write;

        let tmp = std::env::temp_dir().join("__kernex_test_from_file__");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        let cfg_path = tmp.join("agent.toml");
        let mut f = std::fs::File::create(&cfg_path).unwrap();
        writeln!(
            f,
            r#"[runtime]
name = "file-agent"
data_dir = "{}"
channel = "api"
project = "file-proj"
system_prompt = "From file."
"#,
            tmp.to_str().unwrap().replace('\\', "\\\\")
        )
        .unwrap();

        let runtime = RuntimeBuilder::from_file(cfg_path.to_str().unwrap())
            .unwrap()
            .build()
            .await
            .unwrap();

        assert_eq!(runtime.channel, "api");
        assert_eq!(runtime.project, Some("file-proj".to_string()));
        assert_eq!(runtime.system_prompt, "From file.");

        let _ = std::fs::remove_dir_all(&tmp);
    }
}