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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Trust tier enum for skill execution permissions.
//!
//! [`SkillTrustLevel`] is the single source of truth for trust-level semantics across all
//! Zeph crates. It lives in `zeph-common` so both `zeph-skills` and `zeph-tools` can depend
//! on it without introducing a circular dependency.
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
/// Trust tier controlling what a skill is allowed to do.
///
/// The ordering from most to least trusted is: `Trusted` → `Verified` → `Quarantined` →
/// `Blocked`. Use [`SkillTrustLevel::severity`] to compare levels numerically, or
/// [`SkillTrustLevel::min_trust`] to find the least-trusted of two levels.
///
/// # Examples
///
/// ```rust
/// use zeph_common::SkillTrustLevel;
///
/// let level = SkillTrustLevel::Quarantined;
/// assert!(level.is_active());
/// assert_eq!(level.severity(), 2);
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum SkillTrustLevel {
/// Built-in or user-audited skill: full tool access.
Trusted,
/// Signature or hash verified: default tool access.
Verified,
/// Newly imported or hash-mismatch: restricted tool access.
#[default]
Quarantined,
/// Explicitly disabled by user or auto-blocked by anomaly detector.
Blocked,
}
impl SkillTrustLevel {
/// Trust level to assume when a skill has no entry in the trust map.
///
/// A missing entry means "never classified yet" (e.g. persistence not wired, or a
/// transient trust-map read failure), not "known untrusted" — callers must not fall
/// back to [`SkillTrustLevel::default`] ([`Quarantined`](Self::Quarantined)) for this
/// case, as that would misclassify legitimately trusted, already-vetted skills.
///
/// # Examples
///
/// ```rust
/// use zeph_common::SkillTrustLevel;
///
/// let trust_levels: std::collections::HashMap<String, SkillTrustLevel> =
/// std::collections::HashMap::new();
/// let trust = trust_levels
/// .get("some-skill")
/// .copied()
/// .unwrap_or(SkillTrustLevel::MISSING_ENTRY_FALLBACK);
/// assert_eq!(trust, SkillTrustLevel::Trusted);
/// ```
pub const MISSING_ENTRY_FALLBACK: Self = Self::Trusted;
/// Ordered severity: lower value = more trusted.
///
/// # Examples
///
/// ```rust
/// use zeph_common::SkillTrustLevel;
///
/// assert!(SkillTrustLevel::Trusted.severity() < SkillTrustLevel::Blocked.severity());
/// ```
#[must_use]
pub const fn severity(self) -> u8 {
match self {
Self::Trusted => 0,
Self::Verified => 1,
Self::Quarantined => 2,
Self::Blocked => 3,
}
}
/// Returns the least-trusted (highest severity) of two levels.
///
/// # Examples
///
/// ```rust
/// use zeph_common::SkillTrustLevel;
///
/// let result = SkillTrustLevel::Trusted.min_trust(SkillTrustLevel::Quarantined);
/// assert_eq!(result, SkillTrustLevel::Quarantined);
/// ```
#[must_use]
pub const fn min_trust(self, other: Self) -> Self {
if self.severity() >= other.severity() {
self
} else {
other
}
}
/// Returns the string representation used for database storage.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Trusted => "trusted",
Self::Verified => "verified",
Self::Quarantined => "quarantined",
Self::Blocked => "blocked",
}
}
/// Returns `true` if the level is not `Blocked`.
///
/// # Examples
///
/// ```rust
/// use zeph_common::SkillTrustLevel;
///
/// assert!(SkillTrustLevel::Quarantined.is_active());
/// assert!(!SkillTrustLevel::Blocked.is_active());
/// ```
#[must_use]
pub const fn is_active(self) -> bool {
!matches!(self, Self::Blocked)
}
}
impl FromStr for SkillTrustLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"trusted" => Ok(Self::Trusted),
"verified" => Ok(Self::Verified),
"quarantined" => Ok(Self::Quarantined),
"blocked" => Ok(Self::Blocked),
other => Err(format!(
"unknown trust level '{other}'; expected: trusted, verified, quarantined, blocked"
)),
}
}
}
impl fmt::Display for SkillTrustLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Trusted => f.write_str("trusted"),
Self::Verified => f.write_str("verified"),
Self::Quarantined => f.write_str("quarantined"),
Self::Blocked => f.write_str("blocked"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn severity_ordering() {
assert!(SkillTrustLevel::Trusted.severity() < SkillTrustLevel::Verified.severity());
assert!(SkillTrustLevel::Verified.severity() < SkillTrustLevel::Quarantined.severity());
assert!(SkillTrustLevel::Quarantined.severity() < SkillTrustLevel::Blocked.severity());
}
#[test]
fn min_trust_picks_least_trusted() {
assert_eq!(
SkillTrustLevel::Trusted.min_trust(SkillTrustLevel::Quarantined),
SkillTrustLevel::Quarantined
);
assert_eq!(
SkillTrustLevel::Blocked.min_trust(SkillTrustLevel::Trusted),
SkillTrustLevel::Blocked
);
}
#[test]
fn is_active() {
assert!(SkillTrustLevel::Trusted.is_active());
assert!(SkillTrustLevel::Verified.is_active());
assert!(SkillTrustLevel::Quarantined.is_active());
assert!(!SkillTrustLevel::Blocked.is_active());
}
#[test]
fn default_is_quarantined() {
assert_eq!(SkillTrustLevel::default(), SkillTrustLevel::Quarantined);
}
#[test]
fn display() {
assert_eq!(SkillTrustLevel::Trusted.to_string(), "trusted");
assert_eq!(SkillTrustLevel::Blocked.to_string(), "blocked");
assert_eq!(SkillTrustLevel::Quarantined.to_string(), "quarantined");
assert_eq!(SkillTrustLevel::Verified.to_string(), "verified");
}
#[test]
fn serde_roundtrip() {
let level = SkillTrustLevel::Quarantined;
let json = serde_json::to_string(&level).unwrap();
assert_eq!(json, "\"quarantined\"");
let back: SkillTrustLevel = serde_json::from_str(&json).unwrap();
assert_eq!(back, level);
}
#[test]
fn min_trust_same_level_returns_self() {
assert_eq!(
SkillTrustLevel::Verified.min_trust(SkillTrustLevel::Verified),
SkillTrustLevel::Verified
);
}
}