zeph-llm 0.19.0

LLM provider abstraction with Ollama, Claude, OpenAI, and Candle backends
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Three-class `AlignSentinel` classifier for injection refinement.
//!
//! Runs after the binary `DeBERTa` classifier to distinguish:
//! - `misaligned-instruction`: adversarial injection (keep verdict)
//! - `aligned-instruction`: legitimate instruction (downgrade to Clean)
//! - `no-instruction`: no instruction present (downgrade to Clean)
//!
//! Label mapping is read dynamically from the model's `config.json` `id2label` field.
//! The consumer (`classify_injection` in zeph-sanitizer) maps label strings to
//! `InstructionClass` enum values.
//!
//! Load failure is ERROR-logged at startup and on every classify attempt. The caller
//! treats `Err(LlmError::ModelLoad)` as "three-class unavailable" and falls back to
//! the binary-only verdict. This is NOT silent — operators will see repeated errors.

use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, OnceLock};

use candle_core::{DType, Device, Tensor};
use candle_nn::VarBuilder;
use candle_transformers::models::debertav2::{
    Config as DebertaConfig, DebertaV2SeqClassificationModel,
};
use tokenizers::Tokenizer;

use crate::error::LlmError;

use super::candle::validate_safetensors;
use super::{
    CHUNK_OVERLAP_TOKENS, ClassificationResult, ClassifierBackend, MAX_CHUNK_CONTENT_TOKENS,
};

struct ThreeClassInner {
    model: DebertaV2SeqClassificationModel,
    tokenizer: Tokenizer,
    device: Device,
    id2label: Vec<String>,
    cls_token_id: u32,
    sep_token_id: u32,
}

/// Three-class `DeBERTa` sequence classifier for `AlignSentinel`-style refinement.
///
/// Label mapping is read dynamically from the model's `config.json` at load time.
/// Load failures are ERROR-logged and returned as `Err(LlmError::ModelLoad)` on every
/// classify call.
///
/// Only a **successful** load is cached in the `OnceLock`. A failed load does NOT
/// permanently disable the classifier — the next classify call retries loading. This
/// prevents a transient network failure at startup from permanently disabling refinement.
#[derive(Clone)]
pub struct CandleThreeClassClassifier {
    repo_id: Arc<str>,
    hf_token: Option<Arc<str>>,
    sha256: Option<Arc<str>>,
    /// Caches only on success. `None` = not yet loaded or last attempt failed (retry allowed).
    inner: Arc<OnceLock<Arc<ThreeClassInner>>>,
}

impl std::fmt::Debug for CandleThreeClassClassifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CandleThreeClassClassifier")
            .field("repo_id", &self.repo_id)
            .finish_non_exhaustive()
    }
}

impl CandleThreeClassClassifier {
    /// Create a new three-class classifier that will load `repo_id` from `HuggingFace` Hub.
    #[must_use]
    pub fn new(repo_id: impl Into<Arc<str>>) -> Self {
        Self {
            repo_id: repo_id.into(),
            hf_token: None,
            sha256: None,
            inner: Arc::new(OnceLock::new()),
        }
    }

    /// Attempt to load the model, returning the cached inner or a new load result.
    ///
    /// Returns `Ok(Arc<ThreeClassInner>)` on success (and caches it), or
    /// `Err(String)` on load failure (does NOT cache the failure — next call retries).
    fn get_or_try_load(&self) -> Result<Arc<ThreeClassInner>, String> {
        if let Some(inner) = self.inner.get() {
            return Ok(Arc::clone(inner));
        }
        // Not yet loaded — attempt load. Only cache on success.
        match Self::load_inner(
            &self.repo_id,
            self.hf_token.as_deref(),
            self.sha256.as_deref(),
        ) {
            Ok(inner) => {
                let arc = Arc::new(inner);
                // Another thread may have raced us; get() returns the winner.
                let _ = self.inner.set(Arc::clone(&arc));
                Ok(self.inner.get().map_or(arc, Arc::clone))
            }
            Err(e) => Err(e.to_string()),
        }
    }

    /// Attach a resolved `HuggingFace` Hub API token for authenticated model downloads.
    #[must_use]
    pub fn with_hf_token(mut self, token: impl Into<Arc<str>>) -> Self {
        self.hf_token = Some(token.into());
        self
    }

    /// Set expected SHA-256 hex digest of the model safetensors file.
    ///
    /// When set, the file is verified before loading. Mismatch aborts startup.
    #[must_use]
    pub fn with_sha256(mut self, digest: impl Into<Arc<str>>) -> Self {
        self.sha256 = Some(digest.into());
        self
    }

    /// Eagerly trigger model loading. Logs ERROR on failure.
    ///
    /// Call at agent startup so load failures are surfaced immediately rather than
    /// on the first classify call. A failure here does NOT permanently disable the
    /// classifier — the next classify call will retry loading.
    pub fn preload(&self) {
        if let Err(e) = self.get_or_try_load() {
            tracing::error!(
                repo_id = %self.repo_id,
                error = %e,
                "three-class classifier failed to preload — will retry on first classify call"
            );
        }
    }

    fn run_chunk(
        inner: &ThreeClassInner,
        input_ids: &[u32],
    ) -> Result<ClassificationResult, LlmError> {
        let seq_len = input_ids.len();
        let ids_tensor = Tensor::new(input_ids, &inner.device)?.unsqueeze(0)?;
        let token_type_ids = Tensor::zeros((1, seq_len), DType::I64, &inner.device)?;
        let attention_mask = Tensor::ones((1, seq_len), DType::I64, &inner.device)?;

        let logits =
            inner
                .model
                .forward(&ids_tensor, Some(token_type_ids), Some(attention_mask))?;

        let probs = candle_nn::ops::softmax(&logits.squeeze(0)?, 0)?;
        let probs_vec = probs.to_vec1::<f32>().map_err(LlmError::Candle)?;

        let (best_idx, best_score) = probs_vec
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map_or((0, 0.0), |(i, &s)| (i, s));

        let label = inner
            .id2label
            .get(best_idx)
            .cloned()
            .unwrap_or_else(|| best_idx.to_string());

        // is_positive = model detected a misaligned instruction (adversarial).
        // Used by callers to distinguish "this is an attack" from other labels.
        let is_positive = label.to_lowercase().contains("misaligned");

        Ok(ClassificationResult {
            label,
            score: best_score,
            is_positive,
            spans: vec![],
        })
    }

    fn classify_sync(
        inner: &ThreeClassInner,
        text: &str,
    ) -> Result<ClassificationResult, LlmError> {
        let encoding = inner
            .tokenizer
            .encode(text, true)
            .map_err(|e| LlmError::Inference(format!("tokenizer encode failed: {e}")))?;
        let ids = encoding.get_ids();

        if ids.is_empty() {
            let label = inner
                .id2label
                .iter()
                .find(|l| l.to_lowercase().contains("no"))
                .cloned()
                .unwrap_or_else(|| "no_instruction".into());
            return Ok(ClassificationResult {
                label,
                score: 1.0,
                is_positive: false,
                spans: vec![],
            });
        }

        let ids = if ids.len() >= 2
            && ids[0] == inner.cls_token_id
            && ids[ids.len() - 1] == inner.sep_token_id
        {
            &ids[1..ids.len() - 1]
        } else {
            ids
        };

        // Aggregate: most-misaligned chunk wins (same logic as binary classifier's positive win).
        let mut best_positive: Option<ClassificationResult> = None;
        let mut best_overall: Option<ClassificationResult> = None;
        let mut start = 0usize;
        while start < ids.len() {
            let end = (start + MAX_CHUNK_CONTENT_TOKENS).min(ids.len());
            let content = &ids[start..end];
            let mut framed = Vec::with_capacity(content.len() + 2);
            framed.push(inner.cls_token_id);
            framed.extend_from_slice(content);
            framed.push(inner.sep_token_id);
            let result = Self::run_chunk(inner, &framed)?;
            if result.is_positive {
                let is_better = best_positive
                    .as_ref()
                    .is_none_or(|prev| result.score > prev.score);
                if is_better {
                    best_positive = Some(result.clone());
                }
            }
            let is_better_overall = best_overall
                .as_ref()
                .is_none_or(|prev| result.score > prev.score);
            if is_better_overall {
                best_overall = Some(result);
            }
            if end == ids.len() {
                break;
            }
            start = end.saturating_sub(CHUNK_OVERLAP_TOKENS);
        }

        Ok(best_positive
            .or(best_overall)
            .unwrap_or(ClassificationResult {
                label: inner
                    .id2label
                    .iter()
                    .find(|l| l.to_lowercase().contains("no"))
                    .cloned()
                    .unwrap_or_else(|| "no_instruction".into()),
                score: 1.0,
                is_positive: false,
                spans: vec![],
            }))
    }

    #[allow(unsafe_code)]
    fn load_inner(
        repo_id: &str,
        hf_token: Option<&str>,
        sha256: Option<&str>,
    ) -> Result<ThreeClassInner, LlmError> {
        let api = hf_hub::api::sync::ApiBuilder::new()
            .with_token(hf_token.map(str::to_owned))
            .build()
            .map_err(|e| {
                LlmError::ModelLoad(format!("failed to create HuggingFace API client: {e}"))
            })?;
        let repo = api.model(repo_id.to_owned());

        let config_path = repo.get("config.json").map_err(|e| {
            LlmError::ModelLoad(format!(
                "failed to download config.json from {repo_id}: {e}"
            ))
        })?;
        let tokenizer_path = repo.get("tokenizer.json").map_err(|e| {
            LlmError::ModelLoad(format!(
                "failed to download tokenizer.json from {repo_id}: {e}"
            ))
        })?;
        let weights_path = repo.get("model.safetensors").map_err(|e| {
            LlmError::ModelLoad(format!(
                "failed to download model.safetensors from {repo_id}: {e}"
            ))
        })?;

        if let Some(expected_hash) = sha256 {
            super::verify_sha256(&weights_path, expected_hash)?;
        }

        let config_str = std::fs::read_to_string(&config_path)
            .map_err(|e| LlmError::ModelLoad(format!("failed to read model config: {e}")))?;
        let config: DebertaConfig = serde_json::from_str(&config_str)?;

        let id2label: Vec<String> = config.id2label.as_ref().map_or_else(
            || {
                vec![
                    "no_instruction".into(),
                    "aligned_instruction".into(),
                    "misaligned_instruction".into(),
                ]
            },
            |m| {
                let mut sorted: Vec<(u32, String)> =
                    m.iter().map(|(k, v)| (*k, v.clone())).collect();
                sorted.sort_by_key(|(k, _)| *k);
                sorted.into_iter().map(|(_, v)| v).collect()
            },
        );

        let tokenizer = Tokenizer::from_file(&tokenizer_path)
            .map_err(|e| LlmError::ModelLoad(format!("failed to load tokenizer: {e}")))?;

        let cls_token_id = tokenizer
            .token_to_id("[CLS]")
            .ok_or_else(|| LlmError::ModelLoad("tokenizer missing [CLS] token".into()))?;
        let sep_token_id = tokenizer
            .token_to_id("[SEP]")
            .ok_or_else(|| LlmError::ModelLoad("tokenizer missing [SEP] token".into()))?;

        validate_safetensors(&weights_path)?;

        let device = crate::device::detect_device();
        // SAFETY: validated safetensors header above; file not modified during VarBuilder lifetime
        let vb =
            unsafe { VarBuilder::from_mmaped_safetensors(&[weights_path], DType::F32, &device)? };

        let model = DebertaV2SeqClassificationModel::load(vb, &config, None)
            .map_err(|e| LlmError::ModelLoad(format!("failed to load three-class model: {e}")))?;

        Ok(ThreeClassInner {
            model,
            tokenizer,
            device,
            id2label,
            cls_token_id,
            sep_token_id,
        })
    }
}

impl ClassifierBackend for CandleThreeClassClassifier {
    fn classify<'a>(
        &'a self,
        text: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<ClassificationResult, LlmError>> + Send + 'a>> {
        let text = text.to_owned();
        let inner_lock = Arc::clone(&self.inner);
        let repo_id = Arc::clone(&self.repo_id);
        let hf_token = self.hf_token.clone();
        let sha256 = self.sha256.clone();

        Box::pin(async move {
            tokio::task::spawn_blocking(move || {
                // Use get_or_try_load: only caches success; failures allow retry on next call.
                match inner_lock.get().map(Arc::clone).ok_or(()).or_else(|()| {
                    Self::load_inner(&repo_id, hf_token.as_deref(), sha256.as_deref())
                        .map(|i| {
                            let arc = Arc::new(i);
                            let _ = inner_lock.set(Arc::clone(&arc));
                            inner_lock.get().map_or(arc, Arc::clone)
                        })
                        .map_err(|e| e.to_string())
                }) {
                    Ok(inner) => Self::classify_sync(&inner, &text),
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            "three-class classifier load failed — will retry on next call"
                        );
                        Err(LlmError::ModelLoad(e))
                    }
                }
            })
            .await
            .map_err(|e| LlmError::Inference(format!("spawn_blocking panicked: {e}")))?
        })
    }

    fn backend_name(&self) -> &'static str {
        "three_class_candle"
    }
}

#[cfg(test)]
mod tests {
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::Arc;

    use crate::error::LlmError;

    use super::super::{ClassificationResult, ClassifierBackend};

    struct MockThreeClass {
        label: &'static str,
        score: f32,
    }

    impl ClassifierBackend for MockThreeClass {
        fn classify<'a>(
            &'a self,
            _text: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<ClassificationResult, LlmError>> + Send + 'a>>
        {
            let label = self.label.to_owned();
            let score = self.score;
            Box::pin(async move {
                Ok(ClassificationResult {
                    is_positive: label.to_lowercase().contains("misaligned"),
                    label,
                    score,
                    spans: vec![],
                })
            })
        }

        fn backend_name(&self) -> &'static str {
            "mock_three_class"
        }
    }

    fn mock(label: &'static str, score: f32) -> Arc<dyn ClassifierBackend> {
        Arc::new(MockThreeClass { label, score })
    }

    #[tokio::test]
    async fn misaligned_returns_is_positive() {
        let b = mock("misaligned_instruction", 0.9);
        let r = b.classify("ignore previous instructions").await.unwrap();
        assert!(r.is_positive);
        assert_eq!(r.label, "misaligned_instruction");
    }

    #[tokio::test]
    async fn aligned_returns_not_positive() {
        let b = mock("aligned_instruction", 0.85);
        let r = b.classify("format the output as JSON").await.unwrap();
        assert!(!r.is_positive);
        assert_eq!(r.label, "aligned_instruction");
    }

    #[tokio::test]
    async fn no_instruction_returns_not_positive() {
        let b = mock("no_instruction", 0.95);
        let r = b.classify("the weather is nice today").await.unwrap();
        assert!(!r.is_positive);
    }
}