zeph-core 0.19.0

Core agent loop, configuration, context builder, metrics, and vault for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use zeph_llm::provider::Role;

use super::Agent;
use super::context;
use super::error;
use super::feedback_detector;

/// Convert a `FeedbackVerdict` (from `LlmClassifier`) into a `CorrectionSignal`.
///
/// Mirrors `JudgeVerdict::into_signal` to keep both code paths symmetric.
pub(super) fn feedback_verdict_into_signal(
    verdict: &zeph_llm::classifier::llm::FeedbackVerdict,
    user_message: &str,
) -> Option<feedback_detector::CorrectionSignal> {
    if !verdict.is_correction {
        return None;
    }
    let confidence = verdict.confidence.clamp(0.0, 1.0);
    let kind_raw = verdict.kind.trim().to_lowercase().replace(' ', "_");
    let kind = match kind_raw.as_str() {
        "explicit_rejection" => feedback_detector::CorrectionKind::ExplicitRejection,
        "alternative_request" => feedback_detector::CorrectionKind::AlternativeRequest,
        "repetition" => feedback_detector::CorrectionKind::Repetition,
        "self_correction" => feedback_detector::CorrectionKind::SelfCorrection,
        other => {
            tracing::warn!(
                kind = other,
                "llm-classifier returned unknown correction kind, discarding"
            );
            return None;
        }
    };
    Some(feedback_detector::CorrectionSignal {
        confidence,
        kind,
        feedback_text: user_message.to_owned(),
    })
}

/// Store a correction record in memory (shared by judge and llm-classifier paths).
pub(super) async fn store_correction_in_memory(
    memory: Option<std::sync::Arc<zeph_memory::semantic::SemanticMemory>>,
    conv_id: Option<zeph_memory::ConversationId>,
    assistant_snippet: &str,
    user_msg: &str,
    skill_name: String,
    kind_str: &str,
) {
    let Some(mem) = memory else { return };
    let correction_text = context::truncate_chars(user_msg, 500);
    match mem
        .sqlite()
        .store_user_correction(
            conv_id.map(|c| c.0),
            assistant_snippet,
            &correction_text,
            if skill_name.is_empty() {
                None
            } else {
                Some(skill_name.as_str())
            },
            kind_str,
        )
        .await
    {
        Ok(correction_id) => {
            if let Err(e) = mem
                .store_correction_embedding(correction_id, &correction_text)
                .await
            {
                tracing::warn!("failed to store correction embedding: {e:#}");
            }
        }
        Err(e) => {
            tracing::warn!("failed to store judge correction: {e:#}");
        }
    }
}

impl<C: crate::channel::Channel> Agent<C> {
    /// Spawn a background task to evaluate the user message with the LLM judge (or `LlmClassifier`)
    /// and store the correction result. Non-blocking: the task runs independently of the response
    /// pipeline.
    ///
    /// # Notes
    ///
    /// Tasks are supervised via [`BackgroundSupervisor`] (`TaskClass::Enrichment`).
    /// If the concurrency limit is reached, the correction check is silently dropped —
    /// corrections are non-critical lossy data.
    #[allow(clippy::too_many_lines)]
    pub(super) fn spawn_judge_correction_check(
        &mut self,
        trimmed: &str,
        conv_id: Option<zeph_memory::ConversationId>,
    ) {
        let assistant_snippet = self.last_assistant_response();
        let user_msg_owned = trimmed.to_owned();
        let memory_arc = self.memory_state.persistence.memory.clone();
        let skill_name = self
            .skill_state
            .active_skill_names
            .first()
            .cloned()
            .unwrap_or_default();
        let conv_id_bg = conv_id;
        let confidence_threshold = self
            .learning_engine
            .config
            .as_ref()
            .map_or(0.6, |c| c.correction_confidence_threshold);

        if let Some(llm_classifier) = self.feedback.llm_classifier.clone() {
            let user_msg = user_msg_owned.clone();
            let assistant = assistant_snippet.clone();
            let memory_arc2 = memory_arc.clone();
            let skill_name2 = skill_name.clone();
            let classifier_metrics_bg = self.metrics.classifier_metrics.clone();
            let metrics_tx_bg = self.metrics.metrics_tx.clone();
            self.lifecycle.supervisor.spawn(
                super::supervisor::TaskClass::Enrichment,
                "llm_classifier_correction",
                async move {
                    match llm_classifier
                        .classify_feedback(&user_msg, &assistant, confidence_threshold)
                        .await
                    {
                        Ok(verdict) => {
                            if let (Some(ref cm), Some(ref tx)) =
                                (classifier_metrics_bg, metrics_tx_bg)
                            {
                                let snap = cm.snapshot();
                                tx.send_modify(|ms| ms.classifier = snap);
                            }
                            if let Some(signal) = feedback_verdict_into_signal(&verdict, &user_msg)
                            {
                                let is_self_correction = signal.kind
                                    == feedback_detector::CorrectionKind::SelfCorrection;
                                tracing::info!(
                                    kind = signal.kind.as_str(),
                                    confidence = signal.confidence,
                                    source = "llm-classifier",
                                    is_self_correction,
                                    "correction signal detected"
                                );
                                store_correction_in_memory(
                                    memory_arc2,
                                    conv_id_bg,
                                    &assistant,
                                    &user_msg,
                                    skill_name2,
                                    signal.kind.as_str(),
                                )
                                .await;
                            }
                        }
                        Err(e) => {
                            tracing::warn!("llm-classifier failed: {e:#}");
                        }
                    }
                },
            );
        } else {
            let judge_provider = self
                .providers
                .judge_provider
                .clone()
                .unwrap_or_else(|| self.provider.clone());
            let user_msg = user_msg_owned.clone();
            let assistant = assistant_snippet.clone();
            self.lifecycle.supervisor.spawn(
                super::supervisor::TaskClass::Enrichment,
                "judge_correction",
                async move {
                    match feedback_detector::JudgeDetector::evaluate(
                        &judge_provider,
                        &user_msg,
                        &assistant,
                        confidence_threshold,
                    )
                    .await
                    {
                        Ok(verdict) => {
                            if let Some(signal) = verdict.into_signal(&user_msg) {
                                let is_self_correction = signal.kind
                                    == feedback_detector::CorrectionKind::SelfCorrection;
                                tracing::info!(
                                    kind = signal.kind.as_str(),
                                    confidence = signal.confidence,
                                    source = "judge",
                                    is_self_correction,
                                    "correction signal detected"
                                );
                                store_correction_in_memory(
                                    memory_arc,
                                    conv_id_bg,
                                    &assistant,
                                    &user_msg,
                                    skill_name,
                                    signal.kind.as_str(),
                                )
                                .await;
                            }
                        }
                        Err(e) => {
                            tracing::warn!("judge detector failed: {e:#}");
                        }
                    }
                },
            );
        }
    }

    /// Detect implicit corrections in the user's message and record them in the learning engine.
    ///
    /// Uses regex-based `FeedbackDetector` first. If a `JudgeDetector` is configured and the
    /// regex result is borderline, the LLM judge runs in a background task (non-blocking).
    /// When `DetectorMode::Model` and an `LlmClassifier` is attached, the LLM classifier is
    /// used instead of `JudgeDetector`, sharing the same adaptive thresholds and rate limiter.
    #[allow(clippy::too_many_lines)]
    pub(super) async fn detect_and_record_corrections(
        &mut self,
        trimmed: &str,
        conv_id: Option<zeph_memory::ConversationId>,
    ) {
        let correction_detection_enabled = self
            .learning_engine
            .config
            .as_ref()
            .is_none_or(|c| c.correction_detection);
        if !correction_detection_enabled {
            return;
        }

        let previous_user_messages: Vec<&str> = self
            .msg
            .messages
            .iter()
            .filter(|m| m.role == Role::User)
            .map(|m| m.content.as_str())
            .collect();

        let regex_signal = self
            .feedback
            .detector
            .detect(trimmed, &previous_user_messages);

        let judge_should_run = if self.feedback.llm_classifier.is_some() {
            let adaptive_low = self
                .learning_engine
                .config
                .as_ref()
                .map_or(0.5, |c| c.judge_adaptive_low);
            let adaptive_high = self
                .learning_engine
                .config
                .as_ref()
                .map_or(0.8, |c| c.judge_adaptive_high);
            let should_invoke = self
                .feedback
                .judge
                .get_or_insert_with(|| {
                    feedback_detector::JudgeDetector::new(adaptive_low, adaptive_high)
                })
                .should_invoke(regex_signal.as_ref());
            should_invoke
                && self
                    .feedback
                    .judge
                    .as_mut()
                    .is_some_and(feedback_detector::JudgeDetector::check_rate_limit)
        } else {
            self.feedback
                .judge
                .as_ref()
                .is_some_and(|jd| jd.should_invoke(regex_signal.as_ref()))
                && self
                    .feedback
                    .judge
                    .as_mut()
                    .is_some_and(feedback_detector::JudgeDetector::check_rate_limit)
        };

        let (signal, signal_source) = if judge_should_run {
            self.spawn_judge_correction_check(trimmed, conv_id);
            (None, "judge")
        } else {
            (regex_signal, "regex")
        };

        let Some(signal) = signal else { return };
        tracing::info!(
            kind = signal.kind.as_str(),
            confidence = signal.confidence,
            source = signal_source,
            "implicit correction detected"
        );
        let feedback_text = context::truncate_chars(&signal.feedback_text, 500);
        if self.is_learning_enabled()
            && signal.kind != feedback_detector::CorrectionKind::SelfCorrection
        {
            self.record_skill_outcomes(
                "user_rejection",
                Some(&feedback_text),
                Some(signal.kind.as_str()),
            )
            .await;
        }
        if let Some(memory) = &self.memory_state.persistence.memory {
            let correction_text = context::truncate_chars(trimmed, 500);
            match memory
                .sqlite()
                .store_user_correction(
                    conv_id.map(|c| c.0),
                    "",
                    &correction_text,
                    self.skill_state
                        .active_skill_names
                        .first()
                        .map(String::as_str),
                    signal.kind.as_str(),
                )
                .await
            {
                Ok(correction_id) => {
                    if let Err(e) = memory
                        .store_correction_embedding(correction_id, &correction_text)
                        .await
                    {
                        tracing::warn!("failed to store correction embedding: {e:#}");
                    }
                }
                Err(e) => tracing::warn!("failed to store user correction: {e:#}"),
            }
        }
    }

    /// Post-dispatch learning hook called from the agent loop after a registry command sends its
    /// `Message` response. Triggers `generate_improved_skill` for `/skill reject` and `/feedback`
    /// commands — these require `&mut self` and cannot run inside the `Send` future in
    /// `agent_access_impl.rs`.
    pub(super) async fn maybe_trigger_post_command_learning(&mut self, trimmed: &str) {
        if !self.is_learning_enabled() {
            return;
        }
        let rest = if let Some(r) = trimmed.strip_prefix("/feedback ") {
            // "/feedback <skill_name> <message>" — pass "<skill_name> <message>" to split
            let r = r.trim();
            if let Some((name, feedback_rest)) = r.split_once(' ') {
                let feedback = feedback_rest.trim().trim_matches('"');
                if self.feedback.detector.detect(feedback, &[]).is_some() {
                    self.generate_improved_skill(name.trim(), feedback, "", Some(feedback))
                        .await
                        .ok();
                }
            }
            return;
        } else if let Some(r) = trimmed.strip_prefix("/skill reject ") {
            r.trim()
        } else {
            return;
        };
        // "/skill reject <name> <reason>" path
        let mut parts = rest.splitn(2, ' ');
        let Some(name) = parts.next() else { return };
        let reason = parts.next().unwrap_or("").trim();
        if !reason.is_empty() {
            self.generate_improved_skill(name, reason, "", Some(reason))
                .await
                .ok();
        }
    }

    /// Return the `/feedback` command output as a `String` without sending via channel.
    ///
    /// Used by the `AgentAccess::handle_feedback_command` implementation to satisfy the
    /// `Send` bound on the returned future.
    pub(super) async fn handle_feedback_as_string(
        &mut self,
        input: &str,
    ) -> Result<String, error::AgentError> {
        let Some((name, rest)) = input.split_once(' ') else {
            return Ok("Usage: /feedback <skill_name> <message>".to_owned());
        };
        let (skill_name, feedback) = (name.trim(), rest.trim().trim_matches('"'));

        if feedback.is_empty() {
            return Ok("Usage: /feedback <skill_name> <message>".to_owned());
        }

        // Clone Arc before .await to avoid holding &self across suspension points.
        let memory = self.memory_state.persistence.memory.clone();
        let Some(memory) = memory else {
            return Ok("Memory not available.".to_owned());
        };
        let conversation_id = self.memory_state.persistence.conversation_id;

        let outcome_type = if self.feedback.detector.detect(feedback, &[]).is_some() {
            "user_rejection"
        } else {
            "user_approval"
        };

        memory
            .sqlite()
            .record_skill_outcome(
                skill_name,
                None,
                conversation_id,
                outcome_type,
                None,
                Some(feedback),
            )
            .await?;

        Ok(format!("Feedback recorded for \"{skill_name}\"."))
    }
}