eli 0.5.1

Ease Lives Instantly — hook-first AI agent framework with multi-channel support
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
//! Hook-first Eli framework runtime.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use arc_swap::ArcSwap;
use serde_json::Value;
use tokio::sync::RwLock;

use nexil::CancellationToken;

use crate::builtin::config::EliConfig;
use crate::control_plane::{BudgetLedger, DispatchFn, TurnContext, turn_usage, with_turn_context};
use crate::envelope::{ValueExt, unpack_batch_vec};
use crate::evolution::{
    AutoEvolutionLoop, AutoEvolutionPolicy, EvolutionStore, load_runtime_policy_for_workspace,
};
use crate::hooks::{ChannelHook, EliHookSpec, HookRuntime, TapeStoreKind};
use crate::types::{
    Envelope, MessageHandler, PromptValue, RUNTIME_SYSTEM_PROMPT_KEY, RUNTIME_TAPES_DIR_KEY,
    RUNTIME_WORKSPACE_KEY, State, TurnResult, TurnUsageInfo,
};

// ---------------------------------------------------------------------------
// PluginStatus
// ---------------------------------------------------------------------------

/// Records whether a plugin loaded successfully.
#[derive(Debug, Clone)]
pub struct PluginStatus {
    pub is_success: bool,
    pub detail: Option<String>,
}

// ---------------------------------------------------------------------------
// EliFramework
// ---------------------------------------------------------------------------

/// Minimal framework core. Everything grows from hook plugins.
pub struct EliFramework {
    /// Working directory / project root.
    pub workspace: RwLock<PathBuf>,
    /// The hook runtime that dispatches to registered plugins.
    /// Uses `ArcSwap` for lock-free snapshot reads on the hot path.
    hook_runtime: ArcSwap<HookRuntime>,
    /// Status of each loaded plugin.
    plugin_status: RwLock<HashMap<String, PluginStatus>>,
    /// Token budget ledger (control plane infrastructure).
    pub budget: BudgetLedger,
}

impl EliFramework {
    /// Create a new framework instance rooted at the current working directory.
    pub fn new() -> Self {
        Self {
            workspace: RwLock::new(std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))),
            hook_runtime: ArcSwap::from_pointee(HookRuntime::new(Vec::new())),
            plugin_status: RwLock::new(HashMap::new()),

            budget: BudgetLedger::new(),
        }
    }

    /// Create a framework with a given workspace path.
    pub fn with_workspace(workspace: PathBuf) -> Self {
        Self {
            workspace: RwLock::new(workspace),
            hook_runtime: ArcSwap::from_pointee(HookRuntime::new(Vec::new())),
            plugin_status: RwLock::new(HashMap::new()),

            budget: BudgetLedger::new(),
        }
    }

    // -- Plugin registration ------------------------------------------------

    /// Register a plugin with the framework.
    pub async fn register_plugin(&self, name: &str, plugin: Arc<dyn EliHookSpec>) {
        self.hook_runtime.rcu(|current| {
            let mut new_rt = (**current).clone();
            new_rt.register(plugin.clone());
            new_rt
        });
        let mut status = self.plugin_status.write().await;
        status.insert(
            name.to_string(),
            PluginStatus {
                is_success: true,
                detail: None,
            },
        );
    }

    /// Record a plugin load failure.
    pub async fn record_plugin_failure(&self, name: &str, detail: String) {
        let mut status = self.plugin_status.write().await;
        status.insert(
            name.to_string(),
            PluginStatus {
                is_success: false,
                detail: Some(detail),
            },
        );
    }

    /// Load hooks from a list of plugins. This is the Rust equivalent of
    /// the Python `load_hooks` which discovers plugins via entry points.
    /// In Rust, callers explicitly provide the plugin list.
    pub async fn load_hooks(&self, plugins: Vec<(String, Arc<dyn EliHookSpec>)>) {
        for (name, plugin) in plugins {
            self.register_plugin(&name, plugin).await;
        }
    }

    // -- Main orchestration loop --------------------------------------------

    /// Run one inbound message through all hooks and return the turn result.
    pub async fn process_inbound(&self, mut inbound: Envelope) -> anyhow::Result<TurnResult> {
        use std::time::Instant;

        let turn_start = Instant::now();
        Self::strip_internal_context(&mut inbound);

        let t0 = Instant::now();
        let rt = self.hook_runtime_snapshot();
        let workspace = self.workspace.read().await.clone();
        let snapshot_ms = t0.elapsed().as_micros();

        if let Some(result) = Self::try_greet_shortcircuit(&rt, &inbound) {
            for outbound in &result.outbounds {
                rt.call_dispatch_outbound(outbound).await;
            }
            return Ok(result);
        }

        // Build turn context: cancellation token + tool wrapping from hooks.
        let rt_clone = rt.clone();
        let dispatch: DispatchFn = Arc::new(move |envelope| {
            let rt = rt_clone.clone();
            Box::pin(async move {
                rt.call_dispatch_outbound(&envelope).await;
            })
        });
        let turn_ctx = TurnContext {
            cancellation: CancellationToken::new(),
            wrap_tools: Some(rt.wrap_tools_fn()),
            usage: Default::default(),
            save_events: Default::default(),
            dispatch: Some(dispatch),
            outbound_media: Default::default(),
        };

        with_turn_context(turn_ctx, async {
            let t0 = Instant::now();
            let session_id = self.resolve_session_id(&rt, &inbound).await;
            let resolve_us = t0.elapsed().as_micros();
            Self::inject_session_id(&mut inbound, &session_id);

            let t0 = Instant::now();
            let state = self
                .build_state(&rt, &inbound, &session_id, &workspace)
                .await;
            let load_state_us = t0.elapsed().as_micros();

            // --- Greeting on join / new session ---
            let is_join = inbound.get("kind").and_then(|v| v.as_str()) == Some("join");
            let is_new_session = state
                .get("_is_new_session")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);

            if is_join || is_new_session {
                if let Some(greeting) = EliConfig::load().greeting_message() {
                    let outbound = Self::build_greeting_outbound(&inbound, &session_id, &greeting);
                    rt.call_dispatch_outbound(&outbound).await;
                }

                if is_join {
                    return Ok(TurnResult {
                        session_id,
                        prompt: PromptValue::Text(String::new()),
                        model_output: String::new(),
                        outbounds: Vec::new(),
                        usage: TurnUsageInfo::default(),
                    });
                }
            }

            let t0 = Instant::now();
            let prompt = Self::build_prompt(&rt, &inbound, &session_id, &state).await;
            let build_prompt_us = t0.elapsed().as_micros();

            let t0 = Instant::now();
            let state = Self::state_with_system_prompt(&rt, &prompt, state);
            let sys_prompt_us = t0.elapsed().as_micros();

            let t0 = Instant::now();
            let model_output = Self::run_model(&rt, &prompt, &session_id, &state, &inbound).await;
            let run_model_us = t0.elapsed().as_micros();

            let preview_len = model_output.floor_char_boundary(120);
            tracing::info!(
                session_id = %session_id,
                output_len = model_output.len(),
                output_preview = %&model_output[..preview_len],
                "run_model completed"
            );

            let t0 = Instant::now();
            rt.call_save_state(&session_id, &state, &inbound, &model_output)
                .await;
            self.spawn_auto_evolution(&session_id, &state);
            let save_state_us = t0.elapsed().as_micros();

            let t0 = Instant::now();
            let outbounds = self
                .collect_outbounds(&rt, &inbound, &session_id, &state, &model_output)
                .await;
            let render_us = t0.elapsed().as_micros();

            let t0 = Instant::now();
            for outbound in &outbounds {
                rt.call_dispatch_outbound(outbound).await;
            }
            let dispatch_us = t0.elapsed().as_micros();

            let total_us = turn_start.elapsed().as_micros();

            tracing::info!(
                session_id = %session_id,
                total_ms = total_us / 1000,
                snapshot_us = snapshot_ms,
                resolve_us,
                load_state_us,
                build_prompt_us,
                sys_prompt_us,
                run_model_ms = run_model_us / 1000,
                save_state_us,
                render_us,
                dispatch_us,
                "turn.timing"
            );

            let usage = turn_usage()
                .map(|u| TurnUsageInfo {
                    input_tokens: u.input_tokens(),
                    output_tokens: u.output_tokens(),
                    total_tokens: u.total_tokens(),
                })
                .unwrap_or_default();

            Ok(TurnResult {
                session_id,
                prompt,
                model_output,
                outbounds,
                usage,
            })
        })
        .await
    }

    fn spawn_auto_evolution(&self, session_id: &str, state: &State) {
        let Some(ctx) = auto_evolution_context(session_id, state) else {
            return;
        };
        tokio::spawn(async move {
            let result = AutoEvolutionLoop::new(ctx.policy.clone()).run(
                &EvolutionStore::new(&ctx.workspace),
                &ctx.tapes_dir,
                &ctx.tape_name,
            );
            log_auto_evolution_result(&ctx, result);
        });
    }

    /// Load a snapshot of the hook runtime. Lock-free via `ArcSwap`.
    fn hook_runtime_snapshot(&self) -> Arc<HookRuntime> {
        self.hook_runtime.load_full()
    }

    fn strip_internal_context(inbound: &mut Envelope) {
        if let Some(ctx) = inbound.get_mut("context").and_then(|v| v.as_object_mut()) {
            ctx.remove("_eli_cleanup_only");
        }
    }

    fn try_greet_shortcircuit(rt: &HookRuntime, inbound: &Envelope) -> Option<TurnResult> {
        let crate::smart_router::RouteDecision::Greet(reply) = rt.call_classify_inbound(inbound)?;

        let session_id = Self::default_session_id(inbound);
        let channel = inbound.field_str("channel", "");
        let chat_id = inbound.field_str("chat_id", "");
        let output_channel = inbound
            .get("output_channel")
            .and_then(|v| v.as_str())
            .unwrap_or(&channel)
            .to_owned();

        let outbound = serde_json::json!({
            "content": reply,
            "session_id": session_id,
            "channel": channel,
            "chat_id": chat_id,
            "output_channel": output_channel,
        });
        Some(TurnResult {
            session_id,
            prompt: PromptValue::Text(String::new()),
            model_output: reply.clone(),
            outbounds: vec![outbound],
            usage: TurnUsageInfo::default(),
        })
    }

    /// Build an outbound envelope for a greeting message.
    fn build_greeting_outbound(inbound: &Envelope, session_id: &str, greeting: &str) -> Envelope {
        let channel = inbound.field_str("channel", "");
        let chat_id = inbound.field_str("chat_id", "");
        let output_channel = inbound
            .get("output_channel")
            .and_then(|v| v.as_str())
            .unwrap_or(&channel)
            .to_owned();

        serde_json::json!({
            "content": greeting,
            "session_id": session_id,
            "channel": channel,
            "chat_id": chat_id,
            "output_channel": output_channel,
        })
    }

    async fn resolve_session_id(&self, rt: &HookRuntime, inbound: &Envelope) -> String {
        match rt.call_resolve_session(inbound).await {
            Ok(Some(id)) => id,
            Ok(None) => Self::default_session_id(inbound),
            Err(e) => {
                tracing::warn!(error = %e, "resolve_session failed, using default session id");
                Self::default_session_id(inbound)
            }
        }
    }

    fn inject_session_id(inbound: &mut Envelope, session_id: &str) {
        if let Some(obj) = inbound.as_object_mut() {
            obj.entry("session_id")
                .or_insert_with(|| Value::String(session_id.to_owned()));
        }
    }

    /// Build the merged state for one turn.
    ///
    /// **State merge precedence**: `call_load_state` collects results in
    /// forward registration order, then this method iterates them in
    /// *reverse* and uses `or_insert` — so **last-registered plugin's
    /// value wins** for duplicate keys, consistent with first-result
    /// hooks (e.g. `resolve_session`, `build_system_prompt`).
    async fn build_state(
        &self,
        rt: &HookRuntime,
        inbound: &Envelope,
        session_id: &str,
        workspace: &std::path::Path,
    ) -> State {
        let mut state: State = HashMap::new();
        state.insert(
            RUNTIME_WORKSPACE_KEY.to_string(),
            Value::String(workspace.to_string_lossy().to_string()),
        );

        let hook_states = match rt.call_load_state(inbound, session_id).await {
            Ok(states) => states,
            Err(e) => {
                tracing::warn!(error = %e, session_id = %session_id, "load_state failed, using empty state");
                Vec::new()
            }
        };
        for hs in hook_states.into_iter().rev().flatten() {
            for (k, v) in hs {
                state.entry(k).or_insert(v);
            }
        }
        state
    }

    async fn build_prompt(
        rt: &HookRuntime,
        inbound: &Envelope,
        session_id: &str,
        state: &State,
    ) -> PromptValue {
        rt.call_build_user_prompt(inbound, session_id, state)
            .await
            .unwrap_or_else(|| PromptValue::Text(inbound.content_text()))
    }

    fn state_with_system_prompt(rt: &HookRuntime, prompt: &PromptValue, mut state: State) -> State {
        let system_prompt = rt.call_build_system_prompt(&prompt.as_text(), &state);
        if let Some(system_prompt) = system_prompt.filter(|text| !text.is_empty()) {
            state.insert(
                RUNTIME_SYSTEM_PROMPT_KEY.to_owned(),
                Value::String(system_prompt),
            );
        }
        state
    }

    async fn run_model(
        rt: &HookRuntime,
        prompt: &PromptValue,
        session_id: &str,
        state: &State,
        inbound: &Envelope,
    ) -> String {
        match rt.call_run_model(prompt, session_id, state).await {
            Ok(Some(ref output)) if output.is_empty() => {
                tracing::warn!(
                    session_id,
                    "run_model returned empty output — possible content filter"
                );
                "(model returned empty response)".to_owned()
            }
            Ok(Some(output)) => output,
            Ok(None) => {
                let err = anyhow::anyhow!("no model skill returned output");
                rt.notify_error("run_model:fallback", &err, Some(inbound))
                    .await;
                "[Error: no model plugin available]".to_owned()
            }
            Err(e) => {
                let err_msg = format!("{e}");
                let err = anyhow::anyhow!("run_model hook failed: {}", err_msg);
                rt.notify_error("run_model", &err, Some(inbound)).await;
                format!("[Error: {err_msg}]")
            }
        }
    }

    // -- Diagnostics --------------------------------------------------------

    /// Return hook implementation summary for diagnostics.
    pub async fn hook_report(&self) -> HashMap<String, Vec<String>> {
        let rt = self.hook_runtime.load();
        rt.hook_report()
    }

    /// Return the plugin status map.
    pub async fn plugin_status(&self) -> HashMap<String, PluginStatus> {
        self.plugin_status.read().await.clone()
    }

    // -- Channel and tape store accessors -----------------------------------

    /// Collect channels from all plugins, deduplicating by name.
    pub async fn get_channels(
        &self,
        message_handler: MessageHandler,
    ) -> HashMap<String, Box<dyn ChannelHook>> {
        let rt = self.hook_runtime.load();
        let all_channels = rt.call_provide_channels(message_handler);
        let mut map: HashMap<String, Box<dyn ChannelHook>> = HashMap::new();
        for ch in all_channels {
            let name = ch.name().to_string();
            map.entry(name).or_insert(ch);
        }
        map
    }

    /// Get the first provided tape store.
    pub async fn get_tape_store(&self) -> Option<TapeStoreKind> {
        let rt = self.hook_runtime.load();
        rt.call_provide_tape_store()
    }

    /// Build the system prompt via the hook chain.
    pub async fn get_system_prompt(&self, prompt: &PromptValue, state: &State) -> String {
        let rt = self.hook_runtime.load();
        rt.call_build_system_prompt(&prompt.as_text(), state)
            .unwrap_or_default()
    }

    // -- Internal helpers ---------------------------------------------------

    /// Compute a default session ID from the envelope fields.
    fn default_session_id(message: &Envelope) -> String {
        // Try session_id field first
        if let Some(obj) = message.as_object()
            && let Some(Value::String(sid)) = obj.get("session_id")
        {
            return sid.clone();
        }
        let channel = message.field_str("channel", "default");
        let chat_id = message.field_str("chat_id", "default");
        format!("{channel}:{chat_id}")
    }

    /// Collect outbound envelopes from render hooks, falling back to a default
    /// envelope containing the model output.
    async fn collect_outbounds(
        &self,
        rt: &HookRuntime,
        message: &Envelope,
        session_id: &str,
        state: &State,
        model_output: &str,
    ) -> Vec<Envelope> {
        let batches = rt
            .call_render_outbound(message, session_id, state, model_output)
            .await;
        if !batches.is_empty() {
            return unpack_batch_vec(batches);
        }

        // Fallback: build a default outbound envelope
        let mut fallback = serde_json::Map::new();
        fallback.insert(
            "content".to_string(),
            Value::String(model_output.to_string()),
        );
        fallback.insert(
            "session_id".to_string(),
            Value::String(session_id.to_string()),
        );

        if let Some(channel) = message.field("channel", None) {
            fallback.insert("channel".to_string(), channel);
        }
        if let Some(chat_id) = message.field("chat_id", None) {
            fallback.insert("chat_id".to_string(), chat_id);
        }

        vec![Value::Object(fallback)]
    }
}

struct AutoEvolutionContext {
    workspace: PathBuf,
    tapes_dir: PathBuf,
    tape_name: String,
    policy: AutoEvolutionPolicy,
}

fn auto_evolution_context(session_id: &str, state: &State) -> Option<AutoEvolutionContext> {
    if auto_evolution_disabled() {
        return None;
    }
    let workspace = runtime_path(state, RUNTIME_WORKSPACE_KEY)?;
    let policy = auto_evolution_policy(&workspace)?;
    Some(AutoEvolutionContext {
        workspace: workspace.clone(),
        tapes_dir: runtime_path(state, RUNTIME_TAPES_DIR_KEY)?,
        tape_name: crate::builtin::tape::TapeService::session_tape_name(session_id, &workspace),
        policy,
    })
}

fn runtime_path(state: &State, key: &str) -> Option<PathBuf> {
    state.get(key).and_then(Value::as_str).map(PathBuf::from)
}

fn auto_evolution_disabled() -> bool {
    matches!(
        std::env::var("ELI_EVOLUTION_DISABLED").ok().as_deref(),
        Some("1" | "true" | "TRUE" | "yes" | "YES")
    )
}

fn auto_evolution_policy(workspace: &std::path::Path) -> Option<AutoEvolutionPolicy> {
    let policy = load_runtime_policy_for_workspace(workspace).ok()?;
    policy.apply_to_auto_policy(AutoEvolutionPolicy::default())
}

fn log_auto_evolution_result(
    ctx: &AutoEvolutionContext,
    result: anyhow::Result<crate::evolution::AutoEvolutionReport>,
) {
    match result {
        Ok(report) => tracing::info!(
            tape = %ctx.tape_name,
            distilled = report.distill.candidates.len(),
            observed = report.observed.len(),
            staged = report.staged.len(),
            promoted = report.promoted.len(),
            expired = report.expired.len(),
            "auto evolution completed"
        ),
        Err(error) => {
            tracing::warn!(tape = %ctx.tape_name, error = %error, "auto evolution failed")
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hooks::{EliHookSpec, HookError};
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::Arc;

    struct SessionPlugin;

    #[async_trait]
    impl EliHookSpec for SessionPlugin {
        fn plugin_name(&self) -> &str {
            "session-plugin"
        }

        async fn resolve_session(&self, message: &Envelope) -> Result<Option<String>, HookError> {
            Ok(message
                .as_object()
                .and_then(|o| o.get("session_id"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_owned()))
        }
    }

    struct PromptPlugin {
        fragment: String,
    }

    #[async_trait]
    impl EliHookSpec for PromptPlugin {
        fn plugin_name(&self) -> &str {
            "prompt-plugin"
        }

        fn build_system_prompt(&self, _prompt_text: &str, _state: &State) -> Option<String> {
            Some(self.fragment.clone())
        }
    }

    struct ModelPlugin;

    #[async_trait]
    impl EliHookSpec for ModelPlugin {
        fn plugin_name(&self) -> &str {
            "model-plugin"
        }

        async fn run_model(
            &self,
            prompt: &PromptValue,
            _session_id: &str,
            _state: &State,
        ) -> Result<Option<String>, HookError> {
            Ok(Some(format!("echo: {}", prompt.as_text())))
        }
    }

    struct SystemPromptStateModelPlugin {
        fragment: String,
    }

    #[async_trait]
    impl EliHookSpec for SystemPromptStateModelPlugin {
        fn plugin_name(&self) -> &str {
            "system-prompt-state-model-plugin"
        }

        fn build_system_prompt(&self, _prompt_text: &str, _state: &State) -> Option<String> {
            Some(self.fragment.clone())
        }

        async fn run_model(
            &self,
            _prompt: &PromptValue,
            _session_id: &str,
            state: &State,
        ) -> Result<Option<String>, HookError> {
            Ok(Some(
                state
                    .get(RUNTIME_SYSTEM_PROMPT_KEY)
                    .and_then(|v| v.as_str())
                    .unwrap_or("missing")
                    .to_owned(),
            ))
        }
    }

    struct BlockingModelPlugin {
        started: Arc<tokio::sync::Notify>,
        release: Arc<tokio::sync::Notify>,
    }

    struct UnicodeBoundaryModelPlugin;

    #[async_trait]
    impl EliHookSpec for UnicodeBoundaryModelPlugin {
        fn plugin_name(&self) -> &str {
            "unicode-boundary-model-plugin"
        }

        async fn run_model(
            &self,
            _prompt: &PromptValue,
            _session_id: &str,
            _state: &State,
        ) -> Result<Option<String>, HookError> {
            Ok(Some(format!("{}完继续", "a".repeat(118))))
        }
    }

    #[async_trait]
    impl EliHookSpec for BlockingModelPlugin {
        fn plugin_name(&self) -> &str {
            "blocking-model-plugin"
        }

        async fn run_model(
            &self,
            prompt: &PromptValue,
            _session_id: &str,
            _state: &State,
        ) -> Result<Option<String>, HookError> {
            self.started.notify_one();
            self.release.notified().await;
            Ok(Some(format!("echo: {}", prompt.as_text())))
        }
    }

    // -- Creation tests -------------------------------------------------------

    #[tokio::test]
    async fn test_framework_creation_default() {
        let fw = EliFramework::new();
        let ws = fw.workspace.read().await;
        assert!(ws.is_absolute() || *ws == std::path::PathBuf::from("."));
    }

    #[tokio::test]
    async fn test_framework_with_workspace() {
        let fw = EliFramework::with_workspace("/tmp/test".into());
        let ws = fw.workspace.read().await;
        assert_eq!(*ws, std::path::PathBuf::from("/tmp/test"));
    }

    // -- Plugin registration --------------------------------------------------

    #[tokio::test]
    async fn test_register_plugin_records_status() {
        let fw = EliFramework::new();
        fw.register_plugin("test", Arc::new(SessionPlugin)).await;
        let status = fw.plugin_status().await;
        assert!(status.contains_key("test"));
        assert!(status["test"].is_success);
    }

    #[tokio::test]
    async fn test_record_plugin_failure() {
        let fw = EliFramework::new();
        fw.record_plugin_failure("bad", "failed to load".into())
            .await;
        let status = fw.plugin_status().await;
        assert!(!status["bad"].is_success);
        assert_eq!(status["bad"].detail.as_deref(), Some("failed to load"));
    }

    #[tokio::test]
    async fn test_load_hooks_registers_multiple_plugins() {
        let fw = EliFramework::new();
        fw.load_hooks(vec![
            (
                "session".into(),
                Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>,
            ),
            (
                "model".into(),
                Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>,
            ),
        ])
        .await;
        let status = fw.plugin_status().await;
        assert_eq!(status.len(), 2);
        assert!(status["session"].is_success);
        assert!(status["model"].is_success);
    }

    // -- get_system_prompt ----------------------------------------------------

    #[tokio::test]
    async fn test_get_system_prompt_returns_first_result() {
        let fw = EliFramework::new();
        fw.register_plugin(
            "low",
            Arc::new(PromptPlugin {
                fragment: "low".into(),
            }),
        )
        .await;
        fw.register_plugin(
            "high",
            Arc::new(PromptPlugin {
                fragment: "high".into(),
            }),
        )
        .await;
        let prompt = PromptValue::Text("hello".into());
        let state = State::new();
        let result = fw.get_system_prompt(&prompt, &state).await;
        // Last-registered (high) wins
        assert_eq!(result, "high");
    }

    // -- process_inbound (full pipeline) --------------------------------------

    #[tokio::test]
    async fn test_process_inbound_full_pipeline() {
        let fw = EliFramework::new();
        fw.register_plugin("session", Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>)
            .await;
        fw.register_plugin("model", Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>)
            .await;

        let msg = json!({"content": "ping", "session_id": "test-session"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert_eq!(result.session_id, "test-session");
        assert!(result.model_output.starts_with("echo: "));
        assert!(!result.outbounds.is_empty());
    }

    #[tokio::test]
    async fn test_process_inbound_default_session_id() {
        let fw = EliFramework::new();
        fw.register_plugin("model", Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>)
            .await;

        let msg = json!({"content": "hello", "channel": "telegram", "chat_id": "42"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert_eq!(result.session_id, "telegram:42");
    }

    #[tokio::test]
    async fn test_process_inbound_uses_hook_system_prompt_in_run_model_hot_path() {
        let fw = EliFramework::new();
        fw.register_plugin(
            "model",
            Arc::new(SystemPromptStateModelPlugin {
                fragment: "from-hook".into(),
            }) as Arc<dyn EliHookSpec>,
        )
        .await;

        let msg = json!({"content": "hello", "session_id": "system-prompt"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert_eq!(result.model_output, "from-hook");
    }

    #[tokio::test]
    async fn test_process_inbound_logs_utf8_safe_preview() {
        let fw = EliFramework::new();
        fw.register_plugin(
            "model",
            Arc::new(UnicodeBoundaryModelPlugin) as Arc<dyn EliHookSpec>,
        )
        .await;

        let msg = json!({"content": "hello", "session_id": "utf8-preview"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert!(result.model_output.starts_with(&"a".repeat(118)));
    }

    #[tokio::test]
    async fn test_process_inbound_snapshots_hook_runtime() {
        let fw = Arc::new(EliFramework::new());
        let started = Arc::new(tokio::sync::Notify::new());
        let release = Arc::new(tokio::sync::Notify::new());
        fw.register_plugin(
            "model",
            Arc::new(BlockingModelPlugin {
                started: started.clone(),
                release: release.clone(),
            }) as Arc<dyn EliHookSpec>,
        )
        .await;

        let inbound = json!({"content": "ping", "session_id": "lock-test"});
        let framework = fw.clone();
        let turn = tokio::spawn(async move { framework.process_inbound(inbound).await });
        started.notified().await;

        tokio::time::timeout(
            std::time::Duration::from_millis(100),
            fw.register_plugin("late", Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>),
        )
        .await
        .expect("register_plugin should not wait for an in-flight turn");

        release.notify_waiters();
        let result = turn.await.unwrap().unwrap();
        assert_eq!(result.session_id, "lock-test");
        assert!(fw.plugin_status().await.contains_key("late"));
    }

    // -- state merge precedence ------------------------------------------------

    /// Plugin that provides a state key "shared" = "first" and "only_first" = "yes".
    struct FirstStatePlugin;

    #[async_trait]
    impl EliHookSpec for FirstStatePlugin {
        fn plugin_name(&self) -> &str {
            "first-state"
        }

        async fn load_state(
            &self,
            _message: &Envelope,
            _session_id: &str,
        ) -> Result<Option<State>, HookError> {
            let mut s = State::new();
            s.insert("shared".into(), Value::String("first".into()));
            s.insert("only_first".into(), Value::String("yes".into()));
            Ok(Some(s))
        }
    }

    /// Plugin that provides a state key "shared" = "second" and "only_second" = "yes".
    struct SecondStatePlugin;

    #[async_trait]
    impl EliHookSpec for SecondStatePlugin {
        fn plugin_name(&self) -> &str {
            "second-state"
        }

        async fn load_state(
            &self,
            _message: &Envelope,
            _session_id: &str,
        ) -> Result<Option<State>, HookError> {
            let mut s = State::new();
            s.insert("shared".into(), Value::String("second".into()));
            s.insert("only_second".into(), Value::String("yes".into()));
            Ok(Some(s))
        }
    }

    #[tokio::test]
    async fn test_build_state_last_registered_wins_for_duplicate_keys() {
        let fw = EliFramework::new();
        // FirstStatePlugin registered first, SecondStatePlugin registered second.
        fw.register_plugin("first", Arc::new(FirstStatePlugin) as Arc<dyn EliHookSpec>)
            .await;
        fw.register_plugin(
            "second",
            Arc::new(SecondStatePlugin) as Arc<dyn EliHookSpec>,
        )
        .await;

        let rt = fw.hook_runtime_snapshot();
        let msg = json!({"content": "hello"});
        let state = fw
            .build_state(&rt, &msg, "test-session", std::path::Path::new("/tmp"))
            .await;

        // Last-registered plugin wins for the duplicate "shared" key.
        assert_eq!(state["shared"], Value::String("second".into()));
        // Non-overlapping keys are preserved from both plugins.
        assert_eq!(state["only_first"], Value::String("yes".into()));
        assert_eq!(state["only_second"], Value::String("yes".into()));
    }

    #[test]
    fn test_auto_evolution_context_uses_runtime_paths() {
        let mut state = State::new();
        let tmp = tempfile::tempdir().unwrap();
        state.insert(
            RUNTIME_WORKSPACE_KEY.to_owned(),
            Value::String(tmp.path().display().to_string()),
        );
        state.insert(
            RUNTIME_TAPES_DIR_KEY.to_owned(),
            Value::String("/tmp/tapes".into()),
        );
        let ctx = auto_evolution_context("cli:test", &state).unwrap();
        assert_eq!(ctx.workspace, tmp.path());
        assert_eq!(ctx.tapes_dir, std::path::PathBuf::from("/tmp/tapes"));
        assert_eq!(
            ctx.tape_name,
            crate::builtin::tape::TapeService::session_tape_name("cli:test", tmp.path(),)
        );
        assert_eq!(
            ctx.policy.min_score,
            AutoEvolutionPolicy::default().min_score
        );
    }

    #[test]
    fn test_auto_evolution_context_honors_kill_switch() {
        unsafe { std::env::set_var("ELI_EVOLUTION_DISABLED", "1") };
        let ctx = auto_evolution_context("cli:test", &State::new());
        unsafe { std::env::remove_var("ELI_EVOLUTION_DISABLED") };
        assert!(ctx.is_none());
    }

    #[test]
    fn test_auto_evolution_context_honors_runtime_policy_disable() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join(".agents/evolution/runtime-policies");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("auto-evolution.json"),
            "{\n  \"auto_evolution\": {\"enabled\": false}\n}",
        )
        .unwrap();
        let mut state = State::new();
        state.insert(
            RUNTIME_WORKSPACE_KEY.to_owned(),
            Value::String(tmp.path().display().to_string()),
        );
        state.insert(
            RUNTIME_TAPES_DIR_KEY.to_owned(),
            Value::String("/tmp/tapes".into()),
        );
        assert!(auto_evolution_context("cli:test", &state).is_none());
    }

    #[test]
    fn test_auto_evolution_context_applies_runtime_policy_overrides() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join(".agents/evolution/runtime-policies");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("auto-evolution.json"),
            "{\n  \"auto_evolution\": {\"min_score\": 91, \"canary_observations\": 4}\n}",
        )
        .unwrap();
        let mut state = State::new();
        state.insert(
            RUNTIME_WORKSPACE_KEY.to_owned(),
            Value::String(tmp.path().display().to_string()),
        );
        state.insert(
            RUNTIME_TAPES_DIR_KEY.to_owned(),
            Value::String("/tmp/tapes".into()),
        );
        let ctx = auto_evolution_context("cli:test", &state).unwrap();
        assert_eq!(ctx.policy.min_score, 91);
        assert_eq!(ctx.policy.canary_observations, 4);
    }

    // -- hook_report ----------------------------------------------------------

    #[tokio::test]
    async fn test_hook_report_reflects_registered_plugins() {
        let fw = EliFramework::new();
        fw.register_plugin("session", Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>)
            .await;
        let report = fw.hook_report().await;
        assert!(report.contains_key("resolve_session"));
        assert!(report["resolve_session"].contains(&"session-plugin".to_owned()));
    }
}