use std::sync::{Arc, Mutex, PoisonError, Weak};
use rho_protocol::{self as wire, ErrorCode, RpcError};
use rho_sdk::CancellationToken;
use tokio::sync::watch;
use super::{awaited::AwaitedWork, LoadedSession, Sessions};
use crate::{
app::{
pull_requests::PullRequestInbox,
session_runtime::{BackgroundWait, SessionRuntime},
subagent_manager::{BackgroundChild, SubagentManager},
},
subagent::RunState,
tools::{
process::{LiveProcessSummary, ProcessManager, State, STOP_GRACE},
ui::SurfaceHost,
workflow_tracker::{WorkflowRailSummary, WorkflowRunTracker},
},
workflow::{RunLifecycle, WorkflowOutcome},
};
pub(super) struct SessionBackground {
session_id: String,
wait: BackgroundWait,
sources: Mutex<Sources>,
workflows: WorkflowRunTracker,
replaced: watch::Sender<u64>,
dropped: CancellationToken,
}
#[derive(Clone)]
struct Sources {
processes: Option<ProcessManager>,
subagents: Option<SubagentManager>,
surfaces: Option<SurfaceHost>,
pull_requests: Option<PullRequestInbox>,
}
impl Sources {
fn of(runtime: &SessionRuntime) -> Self {
Self {
processes: runtime.tools().processes().cloned(),
subagents: runtime.tools().subagents().cloned(),
surfaces: runtime.tools().surfaces().cloned(),
pull_requests: runtime.tools().pull_requests().cloned(),
}
}
}
impl SessionBackground {
pub(super) fn of(runtime: &SessionRuntime) -> Self {
Self {
session_id: runtime.session_id().to_string(),
wait: runtime.background_wait(),
sources: Mutex::new(Sources::of(runtime)),
workflows: runtime.tools().workflow_tracker().clone(),
replaced: watch::Sender::new(0),
dropped: CancellationToken::new(),
}
}
#[cfg(test)]
pub(super) fn for_tests(
session_id: &str,
wait: BackgroundWait,
processes: Option<ProcessManager>,
subagents: Option<SubagentManager>,
) -> Self {
Self {
session_id: session_id.to_owned(),
wait,
sources: Mutex::new(Sources {
processes,
subagents,
surfaces: None,
pull_requests: None,
}),
workflows: WorkflowRunTracker::new(),
replaced: watch::Sender::new(0),
dropped: CancellationToken::new(),
}
}
pub(super) fn refresh(&self, runtime: &SessionRuntime) {
*self.sources.lock().unwrap_or_else(PoisonError::into_inner) = Sources::of(runtime);
self.replaced.send_modify(|count| *count += 1);
}
pub(super) fn replacements(&self) -> watch::Receiver<u64> {
self.replaced.subscribe()
}
pub(super) fn surfaces(&self) -> Option<SurfaceHost> {
self.sources().surfaces
}
pub(super) fn processes(&self) -> Option<ProcessManager> {
self.sources().processes
}
pub(super) fn workflows(&self) -> &WorkflowRunTracker {
&self.workflows
}
pub(super) fn awaited(&self) -> Option<AwaitedWork> {
let Sources {
processes,
subagents,
surfaces,
pull_requests,
} = self.sources();
AwaitedWork::new(
self.wait,
self.session_id.clone(),
processes,
self.workflows.clone(),
subagents,
surfaces,
pull_requests,
)
}
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 mut replaced = self.replacements();
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();
async move {
let replaced = async move {
let _ = replaced.changed().await;
};
tokio::select! {
() = or_pending(exited) => {}
() = or_pending(started) => {}
() = or_pending(subagents) => {}
() = finished => {}
() = launched => {}
() = replaced => {}
}
}
}
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) fn background_list(
&self,
) -> wire::SessionBackgroundListResponse {
let loaded: Vec<_> = self
.loaded()
.iter()
.map(|(session, loaded)| (session.clone(), Arc::clone(loaded)))
.collect();
let mut sessions: Vec<_> = loaded
.into_iter()
.map(|(session, loaded)| wire::SessionBackground {
session: wire::SessionId::new(session.to_string()),
work: loaded.feed.background(),
})
.filter(|listed| !listed.work.is_empty())
.collect();
sessions.sort_by(|a, b| a.session.cmp(&b.session));
wire::SessionBackgroundListResponse { sessions }
}
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();
session.report_activity();
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,
}
}