use std::sync::{Arc, Mutex, PoisonError, Weak};
use rho_protocol::{self as wire, ErrorCode, RpcError};
use rho_sdk::CancellationToken;
use tokio::sync::Notify;
use super::{LoadedSession, Sessions};
use crate::{
app::{
session_runtime::SessionRuntime,
subagent_manager::{BackgroundChild, SubagentManager},
},
subagent::RunState,
tools::{
process::{LiveProcessSummary, ProcessManager, State, STOP_GRACE},
workflow_tracker::{WorkflowRailSummary, WorkflowRunTracker},
},
workflow::{RunLifecycle, WorkflowOutcome},
};
pub(super) struct SessionBackground {
session_id: String,
sources: Mutex<Sources>,
workflows: WorkflowRunTracker,
replaced: Arc<Notify>,
dropped: CancellationToken,
}
#[derive(Clone)]
struct Sources {
processes: Option<ProcessManager>,
subagents: Option<SubagentManager>,
}
impl Sources {
fn of(runtime: &SessionRuntime) -> Self {
Self {
processes: runtime.tools().processes().cloned(),
subagents: runtime.tools().subagents().cloned(),
}
}
}
impl SessionBackground {
pub(super) fn of(runtime: &SessionRuntime) -> Self {
Self {
session_id: runtime.session_id().to_string(),
sources: Mutex::new(Sources::of(runtime)),
workflows: runtime.tools().workflow_tracker().clone(),
replaced: Arc::new(Notify::new()),
dropped: CancellationToken::new(),
}
}
pub(super) fn refresh(&self, runtime: &SessionRuntime) {
*self.sources.lock().unwrap_or_else(PoisonError::into_inner) = Sources::of(runtime);
self.replaced.notify_one();
}
pub(super) fn processes(&self) -> Option<ProcessManager> {
self.sources().processes
}
pub(super) fn workflows(&self) -> &WorkflowRunTracker {
&self.workflows
}
fn sources(&self) -> Sources {
self.sources
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone()
}
fn snapshot(&self) -> Vec<wire::BackgroundWork> {
let sources = self.sources();
let processes = sources
.processes
.iter()
.flat_map(ProcessManager::live_summaries)
.map(process_work);
let workflows = self
.workflows
.rail_summaries(&self.session_id)
.into_iter()
.map(workflow_work);
let subagents = sources
.subagents
.iter()
.flat_map(|subagents| subagents.background(&self.session_id))
.map(subagent_work);
processes.chain(workflows).chain(subagents).collect()
}
fn changes(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
let sources = self.sources();
let exited = sources
.processes
.as_ref()
.map(ProcessManager::notified_owned);
let started = sources
.processes
.as_ref()
.map(ProcessManager::started_owned);
let subagents = sources.subagents.as_ref().map(SubagentManager::changed);
let finished = self.workflows.notified_owned();
let launched = self.workflows.started_owned();
let replaced = Arc::clone(&self.replaced);
async move {
tokio::select! {
() = or_pending(exited) => {}
() = or_pending(started) => {}
() = or_pending(subagents) => {}
() = finished => {}
() = launched => {}
() = replaced.notified() => {}
}
}
}
async fn stop(&self, item: &wire::BackgroundItem) -> Result<(), String> {
let sources = self.sources();
match item {
wire::BackgroundItem::Process { id } => {
let processes = sources.processes.ok_or("the session runs no processes")?;
processes.stop(id, STOP_GRACE).await
}
wire::BackgroundItem::Workflow { run } => {
let row = self
.workflows
.rail_summaries(&self.session_id)
.into_iter()
.find(|row| row.run_id == *run && row.is_live())
.ok_or_else(|| format!("no running workflow run '{run}'"))?;
let run_id = run.parse().map_err(|error| format!("{error}"))?;
let rho_home = crate::paths::rho_dir().map_err(|error| format!("{error:#}"))?;
crate::app::workflow_cli::request_cancellation(&rho_home, run_id, row.lifecycle)
.await
.map(drop)
.map_err(|error| error.to_string())
}
wire::BackgroundItem::Subagent { run, .. } => sources
.subagents
.ok_or("the session runs no subagents")?
.cancel(&self.session_id, run)
.map_err(|error| error.to_string()),
_ => Err("this daemon does not know that kind of work".into()),
}
}
}
impl Drop for SessionBackground {
fn drop(&mut self) {
self.dropped.cancel();
}
}
async fn or_pending(future: Option<impl std::future::Future<Output = ()>>) {
match future {
Some(future) => future.await,
None => std::future::pending().await,
}
}
impl LoadedSession {
pub(super) fn report_background(&self) {
self.feed.update_background(|| self.background.snapshot());
}
}
impl Sessions {
pub(super) fn watch_background(&self, session: &Arc<LoadedSession>) {
let watcher = watch(Arc::downgrade(session));
let dropped = session.background.dropped.clone();
tokio::spawn(async move {
tokio::select! {
() = dropped.cancelled() => {}
() = watcher => {}
}
});
}
pub(in crate::app::daemon_host) async fn background_stop(
&self,
params: wire::SessionBackgroundStopParams,
) -> Result<wire::SessionBackgroundStopResponse, RpcError> {
self.refuse_when_stopping()?;
let not_loaded = || {
RpcError::new(
ErrorCode::SessionNotLoaded,
"the daemon has not loaded the session; attach to it first",
)
};
let session = self
.loaded_session(¶ms.session)
.await?
.ok_or_else(not_loaded)?;
session
.background
.stop(¶ms.work)
.await
.map_err(|message| {
RpcError::new(
ErrorCode::InvalidParams,
format!("could not stop background work: {message}"),
)
})?;
Ok(wire::SessionBackgroundStopResponse {})
}
}
async fn watch(session: Weak<LoadedSession>) {
loop {
let changes = {
let Some(session) = session.upgrade() else {
return;
};
let changes = session.background.changes();
session.report_background();
changes
};
changes.await;
}
}
fn process_work(process: LiveProcessSummary) -> wire::BackgroundWork {
let status = match process.state {
State::Starting | State::Running => wire::BackgroundStatus::Running,
State::Exited => wire::BackgroundStatus::Completed,
State::Terminated => wire::BackgroundStatus::Cancelled,
State::TimedOut | State::FailedToStart => wire::BackgroundStatus::Failed,
};
wire::BackgroundWork {
item: wire::BackgroundItem::Process {
id: process.process_id,
},
title: process.command,
status,
exit_code: process.exit_code,
}
}
fn workflow_work(run: WorkflowRailSummary) -> wire::BackgroundWork {
let status = if run.is_live() {
wire::BackgroundStatus::Running
} else if run.outcome == Some(WorkflowOutcome::Cancellation) {
wire::BackgroundStatus::Cancelled
} else if run.is_failure() || run.lifecycle != RunLifecycle::Completed {
wire::BackgroundStatus::Failed
} else {
wire::BackgroundStatus::Completed
};
wire::BackgroundWork {
item: wire::BackgroundItem::Workflow { run: run.run_id },
title: run.workflow_name,
status,
exit_code: None,
}
}
fn subagent_work(child: BackgroundChild) -> wire::BackgroundWork {
let status = match child.state {
RunState::Queued | RunState::Starting | RunState::Running => {
wire::BackgroundStatus::Running
}
RunState::Ok => wire::BackgroundStatus::Completed,
RunState::Error => wire::BackgroundStatus::Failed,
RunState::Stopped => wire::BackgroundStatus::Cancelled,
};
wire::BackgroundWork {
item: wire::BackgroundItem::Subagent {
run: child.run,
session: child
.session
.map(|session| wire::SessionId::new(session.to_string())),
},
title: child.title,
status,
exit_code: None,
}
}