forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback.rs
More file actions
125 lines (115 loc) · 3.1 KB
/
Copy pathfallback.rs
File metadata and controls
125 lines (115 loc) · 3.1 KB
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
//! Shared fallback-label normalization.
/// Closed fallback outcome set shared by desktop hosts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FallbackOutcome {
/// Primary path recovered after a fallback.
Recovered,
/// A lower-quality path completed.
Degraded,
/// No fallback path completed.
Exhausted,
}
impl FallbackOutcome {
/// Stable telemetry value for this outcome.
pub const fn as_str(self) -> &'static str {
match self {
Self::Recovered => "recovered",
Self::Degraded => "degraded",
Self::Exhausted => "exhausted",
}
}
}
const ALLOWED_COMPONENTS: &[&str] = &[
"sync_dispatch",
"pusher",
"stt_selection",
"vad",
"audio_merge",
"webhook",
"realtime_hub",
"ptt_cascade",
"chat_retrieval",
"gemini_model",
"gemini_proxy",
"gemini_stream_proxy",
"redis_ratelimit",
"silent_mic",
"other",
];
const ALLOWED_REASONS: &[&str] = &[
"timeout",
"provider_5xx",
"provider_429",
"enqueue_failed",
"config_incomplete",
"circuit_open",
"capability_mismatch",
"auth",
"quota",
"local_heal",
"policy",
"dispatch_disabled",
"byok",
"other",
"none",
];
/// Maps unknown reasons into the stable `other` bucket.
pub fn bucket_reason(reason: &str) -> String {
let label = safe_label(reason, "other");
if ALLOWED_REASONS.contains(&label.as_str()) {
label
} else {
"other".to_owned()
}
}
/// Maps unknown components into the stable `other` bucket.
pub fn bucket_component(component: &str) -> String {
let label = safe_label(component, "other");
if ALLOWED_COMPONENTS.contains(&label.as_str()) {
label
} else {
"other".to_owned()
}
}
/// Normalizes a bounded telemetry label without retaining unsafe characters.
pub fn safe_label(value: &str, default: &str) -> String {
let trimmed = value.trim().to_ascii_lowercase();
let source = if trimmed.is_empty() {
default
} else {
&trimmed
};
let normalized: String = source
.chars()
.map(|ch| {
if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | ':' | '-') {
ch
} else {
'_'
}
})
.take(64)
.collect();
if normalized.is_empty() {
default.to_owned()
} else {
normalized
}
}
#[cfg(test)]
mod tests {
use super::{bucket_component, bucket_reason, safe_label};
#[test]
fn safe_label_should_normalize_and_bound_untrusted_input() {
assert_eq!(safe_label("Cloud Tasks!", "none"), "cloud_tasks_");
assert_eq!(safe_label(" ", "none"), "none");
assert_eq!(safe_label(&"a".repeat(65), "none"), "a".repeat(64));
}
#[test]
fn buckets_should_preserve_known_values_and_hide_unknown_ones() {
assert_eq!(bucket_reason("enqueue_failed"), "enqueue_failed");
assert_eq!(bucket_reason("novel"), "other");
assert_eq!(bucket_component("gemini_proxy"), "gemini_proxy");
assert_eq!(bucket_component("novel"), "other");
}
}