Skip to main content

subspace_agent/failure/
category.rs

1//! What kind of failure ended a turn, and how a `session.error` maps to one.
2
3use super::NO_ERROR_TYPE;
4
5const UNAUTHORIZED: i32 = 401;
6const PAYMENT_REQUIRED: i32 = 402;
7const FORBIDDEN: i32 = 403;
8const PROXY_AUTHENTICATION: i32 = 407;
9const TOO_MANY_REQUESTS: i32 = 429;
10const BAD_GATEWAY: i32 = 502;
11const UNAVAILABLE: i32 = 503;
12const GATEWAY_TIMEOUT: i32 = 504;
13
14/// What kind of failure ended a turn, which decides what the chat tells the
15/// user to do.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum FailureCategory {
18    NoResponse,
19    SignIn,
20    Limit,
21    NotAllowed,
22    ContextFull,
23    Network,
24    Stopped,
25    NotResponding,
26    Unknown,
27}
28
29impl FailureCategory {
30    pub const ALL: [Self; 9] = [
31        Self::NoResponse,
32        Self::SignIn,
33        Self::Limit,
34        Self::NotAllowed,
35        Self::ContextFull,
36        Self::Network,
37        Self::Stopped,
38        Self::NotResponding,
39        Self::Unknown,
40    ];
41
42    /// One line that says what happened.
43    pub fn title(self) -> &'static str {
44        match self {
45            Self::NoResponse => "Copilot returned no reply",
46            Self::SignIn => "Copilot did not accept the sign-in",
47            Self::Limit => "Copilot rate limit or quota reached",
48            Self::NotAllowed => "A Copilot policy blocked this request",
49            Self::ContextFull => "The conversation is too long for the model",
50            Self::Network => "Copilot could not reach its service",
51            Self::Stopped => "The Copilot runtime stopped",
52            Self::NotResponding => "The Copilot runtime is not responding",
53            Self::Unknown => "Copilot could not finish this turn",
54        }
55    }
56
57    /// One line that says what to do next.
58    pub fn action(self) -> &'static str {
59        match self {
60            Self::NoResponse => {
61                "Send the message again. If it happens again, pick another model or a lower reasoning effort."
62            }
63            Self::SignIn => {
64                "Sign in to GitHub again, or use an account with Copilot access, then retry."
65            }
66            Self::Limit => "Wait for the limit to reset or pick another model, then retry.",
67            Self::NotAllowed => "Pick another model, or ask your Copilot administrator for access.",
68            Self::ContextFull => "Start a new chat, or pick a model with a larger context.",
69            Self::Network => "Check the network and proxy settings, then retry.",
70            Self::Stopped | Self::NotResponding => "Reconnect to start Copilot again, then retry.",
71            Self::Unknown => "Retry. If it happens again, copy the details into a report.",
72        }
73    }
74
75    /// A stable lower-case name, for logs and the debug fixture.
76    pub fn slug(self) -> &'static str {
77        match self {
78            Self::NoResponse => "no-response",
79            Self::SignIn => "sign-in",
80            Self::Limit => "limit",
81            Self::NotAllowed => "not-allowed",
82            Self::ContextFull => "context-full",
83            Self::Network => "network",
84            Self::Stopped => "stopped",
85            Self::NotResponding => "not-responding",
86            Self::Unknown => "unknown",
87        }
88    }
89
90    /// The category a [`slug`](Self::slug) names.
91    pub fn from_slug(slug: &str) -> Option<Self> {
92        Self::ALL
93            .into_iter()
94            .find(|category| category.slug() == slug.trim())
95    }
96
97    /// The error type of a failure Subspace detected itself.
98    pub(super) fn runtime_error_type(self) -> &'static str {
99        match self {
100            Self::NotResponding => "runtime_not_responding",
101            Self::Stopped => "runtime_stopped",
102            _ => NO_ERROR_TYPE,
103        }
104    }
105
106    /// Whether only a new runtime can recover, so the chat offers Reconnect.
107    pub fn needs_reconnect(self) -> bool {
108        matches!(self, Self::Stopped | Self::NotResponding)
109    }
110}
111
112/// The category of a `session.error`, from the fields the SDK sends: the
113/// error type first, then the remediation, then the HTTP status.
114pub fn categorize(
115    error_type: &str,
116    status_code: Option<i32>,
117    remediation: Option<&str>,
118) -> FailureCategory {
119    match error_type {
120        "authentication" | "authorization" => return FailureCategory::SignIn,
121        "rate_limit" | "quota" => return FailureCategory::Limit,
122        "policy" => return FailureCategory::NotAllowed,
123        "context_limit" => return FailureCategory::ContextFull,
124        _ => {}
125    }
126    if matches!(
127        remediation,
128        Some("sign_in" | "switch_account" | "show_account")
129    ) {
130        return FailureCategory::SignIn;
131    }
132    match status_code {
133        Some(UNAUTHORIZED | FORBIDDEN) => FailureCategory::SignIn,
134        Some(PAYMENT_REQUIRED | TOO_MANY_REQUESTS) => FailureCategory::Limit,
135        Some(PROXY_AUTHENTICATION | BAD_GATEWAY | UNAVAILABLE | GATEWAY_TIMEOUT) => {
136            FailureCategory::Network
137        }
138        None if error_type == "query" => FailureCategory::NoResponse,
139        _ => FailureCategory::Unknown,
140    }
141}