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
162 lines (150 loc) · 4.11 KB
/
Copy pathfallback.rs
File metadata and controls
162 lines (150 loc) · 4.11 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
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
//! Shared fallback / resilience telemetry for the desktop Rust backend.
//!
//! Same field contract as Python `record_fallback` and Swift
//! `DesktopDiagnosticsManager.recordFallback`. Prometheus is not wired in this
//! service yet, so we emit a fixed-field tracing event that scrapers/log
//! pipelines can aggregate. Call sites must still use this helper — do not
//! invent ad-hoc warn strings for new fallbacks.
/// Closed outcome set matching the cross-platform contract.
#[allow(dead_code)] // Recovered/Exhausted used by call sites in later phases
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FallbackOutcome {
Recovered,
Degraded,
Exhausted,
}
impl FallbackOutcome {
pub 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",
];
/// Record a fallback / resilience transition.
///
/// Never panics. Unknown components/reasons bucket to `other`.
pub fn record_fallback(
component: &str,
from_mode: &str,
to_mode: &str,
reason: &str,
outcome: FallbackOutcome,
) {
let component = bucket_component(component);
let from_mode = safe_label(from_mode, "none");
let to_mode = safe_label(to_mode, "none");
let reason = bucket_reason(reason);
tracing::warn!(
event = "fallback",
component = %component,
from = %from_mode,
to = %to_mode,
reason = %reason,
outcome = outcome.as_str(),
"omi_fallback_event"
);
}
pub fn bucket_reason(reason: &str) -> String {
let label = safe_label(reason, "other");
if ALLOWED_REASONS.contains(&label.as_str()) {
label
} else {
"other".to_string()
}
}
pub fn bucket_component(component: &str) -> String {
let label = safe_label(component, "other");
if ALLOWED_COMPONENTS.contains(&label.as_str()) {
label
} else {
"other".to_string()
}
}
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.as_str()
};
let normalized: String = source
.chars()
.map(|ch| {
if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | ':' | '-') {
ch
} else {
'_'
}
})
.collect();
let clipped: String = normalized.chars().take(64).collect();
if clipped.is_empty() {
default.to_string()
} else {
clipped
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn buckets_unknown_reason_and_component() {
assert_eq!(bucket_reason("enqueue_failed"), "enqueue_failed");
assert_eq!(bucket_reason("totally_novel"), "other");
assert_eq!(bucket_component("gemini_proxy"), "gemini_proxy");
assert_eq!(bucket_component("brand_new"), "other");
}
#[test]
fn safe_label_normalizes_and_defaults() {
assert_eq!(safe_label("Cloud Tasks!", "none"), "cloud_tasks_");
assert_eq!(safe_label(" ", "none"), "none");
assert_eq!(safe_label("openai", "none"), "openai");
}
#[test]
fn record_fallback_does_not_panic() {
record_fallback(
"gemini_proxy",
"pro",
"flash",
"quota",
FallbackOutcome::Degraded,
);
record_fallback("not_real", "", "x", "weird", FallbackOutcome::Exhausted);
}
}