mod category;
mod log;
mod turn;
pub use category::{FailureCategory, categorize};
use std::time::Duration;
use chrono::{DateTime, Local};
use serde_json::Value;
pub(crate) use log::{log_failure, log_turn_done, log_turn_started};
pub use turn::TurnContext;
pub const SDK_VERSION: &str = "1.0.34";
pub const STREAM_CLOSED: &str = "The Copilot event stream closed. The runtime process exited.";
pub const SEND_FAILED: &str =
"The Copilot runtime could not take the message. The connection to the runtime is closed.";
pub const NOT_RESPONDING: &str =
"The Copilot runtime sent nothing for 90 seconds and did not answer a ping within 15 seconds.";
const NO_MESSAGE: &str = "No error details were supplied by the SDK.";
const NO_ERROR_TYPE: &str = "runtime";
const REMOVED: &str = "[message text removed]";
const SCRUB_MIN_CHARS: usize = 4;
const YES_NO: [&str; 2] = ["no", "yes"];
#[derive(Clone, Debug, PartialEq)]
pub struct TurnFailure {
pub category: FailureCategory,
pub error_type: String,
pub message: String,
pub status_code: Option<i32>,
pub error_code: Option<String>,
pub provider_call_id: Option<String>,
pub service_request_id: Option<String>,
pub originating_message_id: Option<String>,
pub remediation: Option<String>,
pub url: Option<String>,
pub model: Option<String>,
pub effort: Option<String>,
pub runtime_version: Option<String>,
pub sdk_version: &'static str,
pub elapsed: Option<Duration>,
pub delta_received: bool,
pub attachments: usize,
pub occurred_at: DateTime<Local>,
}
struct Basis<'a> {
category: FailureCategory,
error_type: String,
message: String,
turn: &'a TurnContext,
runtime: Option<&'a str>,
}
impl TurnFailure {
pub fn from_session_error(data: &Value, turn: &TurnContext, runtime: Option<&str>) -> Self {
let error_type = text(data, "errorType").unwrap_or_else(|| NO_ERROR_TYPE.to_owned());
let status_code = data["statusCode"]
.as_i64()
.and_then(|code| i32::try_from(code).ok());
let remediation = text(data, "remediation");
let category = categorize(&error_type, status_code, remediation.as_deref());
let message = text(data, "message").unwrap_or_else(|| NO_MESSAGE.to_owned());
Self {
status_code,
remediation,
error_code: text(data, "errorCode"),
provider_call_id: text(data, "providerCallId"),
service_request_id: text(data, "serviceRequestId"),
originating_message_id: text(data, "originatingMessageId"),
url: text(data, "url"),
..Self::new(Basis {
category,
error_type,
message,
turn,
runtime,
})
}
.scrubbed(turn)
}
pub fn stopped(
category: FailureCategory,
message: &str,
turn: &TurnContext,
runtime: Option<&str>,
) -> Self {
Self::new(Basis {
category,
error_type: category.runtime_error_type().to_owned(),
message: clean(message),
turn,
runtime,
})
.scrubbed(turn)
}
fn scrubbed(mut self, turn: &TurnContext) -> Self {
self.redact_with(|text| scrub(text, &turn.submitted));
self
}
pub fn rejected(
error_type: &str,
message: &str,
turn: &TurnContext,
runtime: Option<&str>,
) -> Self {
Self {
error_type: error_type.to_owned(),
..Self::stopped(FailureCategory::Unknown, message, turn, runtime)
}
}
pub fn redact_with(&mut self, redact: impl Fn(&str) -> String) {
self.message = redact(&self.message);
self.error_type = redact(&self.error_type);
for field in [
&mut self.error_code,
&mut self.provider_call_id,
&mut self.service_request_id,
&mut self.originating_message_id,
&mut self.remediation,
&mut self.url,
]
.into_iter()
.flatten()
{
*field = redact(field);
}
}
fn new(basis: Basis<'_>) -> Self {
let Basis {
category,
error_type,
message,
turn,
runtime,
} = basis;
Self {
category,
error_type,
message,
status_code: None,
error_code: None,
provider_call_id: None,
service_request_id: None,
originating_message_id: None,
remediation: None,
url: None,
model: turn.model.clone(),
effort: turn.effort.clone(),
runtime_version: runtime.map(str::to_owned),
sdk_version: SDK_VERSION,
elapsed: turn.sent.map(|sent| sent.elapsed()),
delta_received: turn.delta_received,
attachments: turn.attachments,
occurred_at: Local::now(),
}
}
pub fn details(&self) -> Vec<(&'static str, String)> {
present([
("Type", Some(self.error_type.clone())),
("Message", Some(self.message.clone())),
("Status", self.status_code.map(|code| code.to_string())),
("Error code", self.error_code.clone()),
("Request ID", self.provider_call_id.clone()),
("Service request ID", self.service_request_id.clone()),
("Message ID", self.originating_message_id.clone()),
("Model", self.model.clone()),
("Reasoning effort", self.effort.clone()),
("Runtime", self.runtime_version.clone()),
("SDK", Some(self.sdk_version.to_owned())),
(
"Time",
Some(self.occurred_at.format("%Y-%m-%d %H:%M:%S %:z").to_string()),
),
(
"Elapsed",
self.elapsed
.map(|elapsed| format!("{:.1} s", elapsed.as_secs_f64())),
),
(
"Reply started",
Some(YES_NO[usize::from(self.delta_received)].to_owned()),
),
("Attachments", Some(self.attachments.to_string())),
])
}
pub fn details_text(&self) -> String {
lines(&self.details())
}
}
fn lines(rows: &[(&'static str, String)]) -> String {
rows.iter()
.map(|(label, value)| format!("{label}: {value}"))
.collect::<Vec<_>>()
.join("\n")
}
fn present<const N: usize>(
rows: [(&'static str, Option<String>); N],
) -> Vec<(&'static str, String)> {
rows.into_iter()
.filter_map(|(label, value)| value.map(|value| (label, value)))
.collect()
}
fn text(data: &Value, field: &str) -> Option<String> {
data[field]
.as_str()
.filter(|value| !value.is_empty())
.map(clean)
}
fn scrub(text: &str, submitted: &[String]) -> String {
submitted
.iter()
.map(|sent| sent.trim())
.filter(|sent| sent.chars().count() >= SCRUB_MIN_CHARS)
.fold(text.to_owned(), |text, sent| text.replace(sent, REMOVED))
}
pub(crate) fn clean(text: &str) -> String {
subspace_core::log::redact(&crate::runtime::redact(text))
}
#[cfg(test)]
mod record_tests;
#[cfg(test)]
mod tests;