use std::time::Instant;
use super::super::session::AccessError;
use super::super::watchdog::fence::{self, Activity, Late};
use super::super::{ChatEvent, ChatId, Event, EventQueue, Value, lock};
use super::{Chat, Env};
use crate::failure::{
FailureCategory, SEND_FAILED, STREAM_CLOSED, TurnContext, TurnFailure, log_failure,
log_turn_done, log_turn_started,
};
const ACCESS_CHECK: &str = "access_check";
const SEND_REJECTED: &str = "send_rejected";
const REJECTED: &str =
"Copilot rejected the message. Check authentication, model access and policy.";
const CHECK_CLOSED: &str =
"The connection to the Copilot runtime closed while Subspace checked the access mode.";
#[derive(Clone, Copy, Debug, Default)]
pub(in crate::runtime) struct Generation {
pub(in crate::runtime) current: u64,
pub(in crate::runtime) expired: bool,
}
pub(in crate::runtime) async fn report(
events: &EventQueue,
chat: ChatId,
mut failure: TurnFailure,
) {
events.clean_failure(&mut failure);
log_failure(chat, &failure);
events
.push(Event::of(chat, ChatEvent::Failure(Box::new(failure))))
.await;
}
pub(in crate::runtime) async fn end_reported(
events: &EventQueue,
chat: ChatId,
turn: &TurnContext,
failure: TurnFailure,
) {
let category = failure.category;
report(events, chat, failure).await;
log_turn_done(chat, turn, Some(category));
events.push(Event::of(chat, ChatEvent::TurnDone)).await;
}
impl Chat {
pub(in crate::runtime) fn begin_turn(
&mut self,
env: &Env<'_>,
steering: bool,
attachments: usize,
submitted: Vec<String>,
) {
self.busy = true;
self.last_event = Instant::now();
if steering {
self.turn.steer(attachments, submitted);
} else {
self.fence.current += 1;
self.fence.expired = false;
self.turn.begin(attachments, submitted);
log_turn_started(self.id, &self.turn);
}
self.publish(env);
}
pub(in crate::runtime) fn end_turn(&mut self, failure: Option<FailureCategory>) {
if self.turn.sent.is_some() {
log_turn_done(self.id, &self.turn, failure);
}
self.turn.finish();
}
pub(in crate::runtime) fn publish(&self, env: &Env<'_>) {
fence::update(
&mut lock(&env.setup.activity),
self.id,
Activity {
busy: self.busy,
last_event: self.last_event,
turn: self.turn.clone(),
generation: self.fence.current,
expired: None,
},
);
}
pub(in crate::runtime) fn reconcile(&mut self, env: &Env<'_>) {
let ended = fence::expired(&lock(&env.setup.activity), self.id, self.fence.current);
if self.busy && ended {
self.watchdog_ended();
}
}
pub(in crate::runtime) fn admit(&mut self, env: &Env<'_>, kind: &str) -> bool {
match fence::late(self.fence.expired, kind) {
Late::Ignore => return false,
Late::Rearm => self.rearm(env),
Late::Normal => {}
}
let ends = matches!(kind, "session.idle" | "session.error");
!ends || self.settle(env)
}
fn settle(&mut self, env: &Env<'_>) -> bool {
let settled = fence::settle(&mut lock(&env.setup.activity), self.id, self.fence.current);
if !settled {
self.watchdog_ended();
}
settled
}
fn watchdog_ended(&mut self) {
self.busy = false;
self.fence.expired = true;
self.turn.finish();
}
fn rearm(&mut self, env: &Env<'_>) {
self.fence.current += 1;
self.fence.expired = false;
self.busy = true;
self.last_event = Instant::now();
self.turn.resume();
self.publish(env);
}
pub(in crate::runtime) async fn session_failed(
&self,
env: &Env<'_>,
failed: bool,
data: &Value,
) -> Option<FailureCategory> {
if !failed {
return None;
}
let failure =
TurnFailure::from_session_error(data, &self.turn, env.setup.runtime_version.as_deref());
let category = failure.category;
report(env.events, self.id, failure).await;
Some(category)
}
pub(in crate::runtime) async fn recheck_policy(&mut self, failed: bool) {
if failed {
self.verified = super::super::ensure_managed_policy(&self.session)
.await
.is_ok();
}
}
pub(in crate::runtime) async fn stream_closed(&mut self, env: &Env<'_>) {
let failure = self.stopped(env, STREAM_CLOSED);
self.end_with(env, failure).await;
}
pub(in crate::runtime) async fn send_failed(
&mut self,
env: &Env<'_>,
steering: bool,
failure: TurnFailure,
) {
if steering {
report(env.events, self.id, failure).await;
return;
}
self.end_with(env, failure).await;
}
pub(in crate::runtime) fn access_failure(
&self,
env: &Env<'_>,
error: &AccessError,
) -> TurnFailure {
if error.transport {
return self.stopped(env, CHECK_CLOSED);
}
TurnFailure::rejected(
ACCESS_CHECK,
&error.message,
&self.turn,
env.setup.runtime_version.as_deref(),
)
}
pub(in crate::runtime) fn send_failure(
&self,
env: &Env<'_>,
error: &github_copilot_sdk::Error,
) -> TurnFailure {
if error.is_transport_failure() {
return self.stopped(env, SEND_FAILED);
}
let text = error.to_string();
let message = Some(text.as_str())
.filter(|text| !text.trim().is_empty())
.unwrap_or(REJECTED);
TurnFailure::rejected(
SEND_REJECTED,
message,
&self.turn,
env.setup.runtime_version.as_deref(),
)
}
async fn end_with(&mut self, env: &Env<'_>, failure: TurnFailure) {
if !self.busy {
report(env.events, self.id, failure).await;
return;
}
if !self.settle(env) {
return;
}
self.busy = false;
end_reported(env.events, self.id, &self.turn, failure).await;
self.turn.finish();
}
fn stopped(&self, env: &Env<'_>, message: &str) -> TurnFailure {
TurnFailure::stopped(
FailureCategory::Stopped,
message,
&self.turn,
env.setup.runtime_version.as_deref(),
)
}
}