use std::path::Path;
use super::{
acp_background,
acp_tools::ServedTools,
acp_turns::AcpTurns,
background::SessionBackground,
cache_warm::KeepWarm,
child_questions::ChildQuestions,
engine::Engine,
feed::Feed,
idle_wake::IdleWatch,
options,
peer_wake::PeerWake,
prompt_keys::PromptKeys,
t3_wake::{T3Wake, WakeDir},
JobId, JobRecord,
};
use crate::{
app::{
daemon_host::wire::state_event,
session_events::{LiveEvents, SavedTurn},
session_mcp::SessionMcp,
session_runtime::{SavedHistory, SessionHandle, SessionRuntime},
},
config::Config,
session::tree::ProjectedMessage,
};
pub(super) fn saved_replay(
live: &mut LiveEvents,
runtime: &SessionRuntime,
job: &JobId,
) -> Option<SavedTurn> {
let saved = runtime.saved_history()?;
let rows = saved_rows(&saved, job)?;
live.replay(&rows, saved.cwd(), saved.tools())
}
fn saved_rows(saved: &SavedHistory<'_>, job: &JobId) -> Option<Vec<ProjectedMessage>> {
saved
.display_rows()
.inspect_err(|error| {
tracing::warn!(
%job,
error = format!("{error:#}"),
"could not read the session's saved rows; a reset replays only the turns since, live, until a later turn saves"
);
})
.ok()
}
pub(super) fn opening_feed(
job: &JobRecord,
runtime: &SessionRuntime,
config: &Config,
auths: &[String],
) -> (Feed, LiveEvents, PromptKeys) {
let mut live = LiveEvents::new(
runtime.workspace_root(),
runtime.tools(),
runtime.configured_context_window(),
);
let last = job.moved();
let feed = Feed::new(
(&last.state).into(),
last.turn.clone(),
runtime.stored_title(),
options::daemon_options(runtime, config, auths),
runtime.agent_id().to_string(),
);
feed.seed_started_session_defaults(options::started_session_defaults(
runtime.started_session_defaults(),
));
let saved = runtime.saved_history();
let rows = saved.as_ref().and_then(|saved| saved_rows(saved, &job.id));
let saved_keys = saved.as_ref().map_or_else(Vec::new, |saved| {
saved.message_keys().unwrap_or_else(|error| {
tracing::warn!(
job = %job.id,
error = format!("{error:#}"),
"could not read the session's saved message keys; a prompt sent again with the key of a saved message starts a turn"
);
Vec::new()
})
});
let keys = PromptKeys::loaded(job, &saved_keys);
if let Some(turn) = saved
.zip(rows)
.and_then(|(saved, rows)| live.replay(&rows, saved.cwd(), saved.tools()))
{
feed.end_turn(turn);
feed.seed_usage(&live.context_usage(runtime.context_estimate()));
}
if let (Some(outcome), Some(turn)) = (job.outcome.clone(), last.turn.clone()) {
feed.publish([
rho_protocol::SessionEvent::TurnEnded { turn, outcome },
state_event(&last),
]);
}
(feed, live, keys)
}
pub(super) struct EngineParts {
pub(super) wake: Option<tokio::sync::Mutex<T3Wake>>,
pub(super) idle_wake: Option<IdleWatch>,
pub(super) cache_warm: Option<KeepWarm>,
pub(super) peer_wake: PeerWake,
pub(super) child_questions: Option<ChildQuestions>,
pub(super) handle: SessionHandle,
pub(super) live: LiveEvents,
pub(super) mcp: SessionMcp,
pub(super) background: SessionBackground,
pub(super) feed: Feed,
pub(super) keys: PromptKeys,
pub(super) engine: Engine,
}
impl EngineParts {
pub(super) fn rho(
mut runtime: Box<SessionRuntime>,
job: &JobRecord,
config: &Config,
auths: &[String],
wakes: &WakeDir,
) -> Self {
let (feed, live, keys) = opening_feed(job, &runtime, config, auths);
Self {
wake: T3Wake::of(&runtime, wakes).map(tokio::sync::Mutex::new),
idle_wake: IdleWatch::of(&runtime),
cache_warm: KeepWarm::of(&runtime),
peer_wake: PeerWake::of(&runtime),
child_questions: ChildQuestions::of(&mut runtime),
handle: runtime.handle(),
live,
mcp: SessionMcp::of(&runtime),
background: SessionBackground::of(&runtime),
feed,
keys,
engine: Engine::Rho(runtime),
}
}
pub(super) fn acp(mut turns: Box<AcpTurns>, job: &JobRecord, config_path: &Path) -> Self {
let background = acp_background::background(turns.session_id().to_string(), turns.served());
let peers = turns.served().and_then(ServedTools::peers).cloned();
Self {
wake: None,
idle_wake: IdleWatch::for_wait(background.wait()),
cache_warm: None,
peer_wake: PeerWake::new(peers),
child_questions: turns
.served_mut()
.and_then(ServedTools::take_child_questions)
.map(ChildQuestions::served),
handle: SessionHandle::without_runtime(),
live: LiveEvents::without_tools(turns.workspace()),
mcp: SessionMcp::none(config_path.to_path_buf()),
background,
feed: turns.opening_feed(job),
keys: PromptKeys::loaded(job, &[]),
engine: Engine::Acp(turns),
}
}
}