use rho_protocol::{ErrorCode, RpcError, SessionForkParams, SessionForkResponse};
use super::{
grant,
open::{config_for_new, open_session, OpenWith, OpenedSession, Opening},
reopen::{parse_session, saved_session, SavedFor},
Admission, JobId, JobRecord, Keeping, SessionJobRecord, Sessions,
};
use crate::{
app::{daemon_host::wire::session_to_wire, prompt_content::PromptContext},
session::{Session as StoredSession, SessionOrigin},
};
const MODEL_CONTEXT: &str = "\
You are in a side chat: a fork of the conversation above, which the user \
opened to ask about it. The original session may still be running in the same \
workspace, and nothing you do here reaches it. Answer the user's questions. Do \
not continue or redo the original session's task, and do not modify the \
workspace: no file edits and no commands that change files or state. Reading \
the workspace is fine.";
impl Sessions {
pub(in crate::app::daemon_host) async fn fork(
&self,
params: SessionForkParams,
) -> Result<SessionForkResponse, RpcError> {
let SessionForkParams {
session: source,
environment,
setup,
} = params;
self.refuse_when_stopping()?;
let source = parse_session(&source)?;
let _source = self.reopening.lock(&source).await;
let stored = saved_session(&source, SavedFor::Fork).await?;
let opening = grant::opening(grant::asked(None, &environment)?, None);
let with = OpenWith::under(opening, &environment, setup)?;
let global = config_for_new(&self.settings).await?;
let job = JobId::generate();
let opened = open_session(
Opening::Fork(stored),
&with,
&self.subagents,
&self.pull_requests,
&self.settings,
global,
&job,
)
.await?;
let opened = OpenedSession {
held_context: PromptContext::of(MODEL_CONTEXT),
..opened
};
let session = opened.runtime.session_id().clone();
let record = JobRecord {
admission: Admission::Immediate,
keeping: match opened.runtime.persistence() {
crate::session::SessionPersistence::Saved => Keeping::Stored,
crate::session::SessionPersistence::Ephemeral => Keeping::MemoryOnly,
},
grant_limit: (!with.grant.is_root()).then(|| with.grant.clone()),
grant: with.grant,
..JobRecord::new_session(
job.clone(),
opened.runtime.workspace_root().to_path_buf(),
session.clone(),
)
};
let added = self.add(SessionJobRecord::Register(record), opened).await?;
tracing::info!(%session, %job, %source, "forked session");
Ok(SessionForkResponse {
session: session_to_wire(session),
options: added.feed.options(),
})
}
}
pub(super) fn refuse_unforkable(
stored: &StoredSession,
origin: Option<SessionOrigin>,
) -> Result<(), RpcError> {
let refused = |why: String| {
Err(RpcError::new(
ErrorCode::SessionOpenFailed,
format!("could not fork session {}: {why}", stored.id()),
))
};
if origin == Some(SessionOrigin::Fork) {
return refused("it is a fork itself; fork the session it forks".to_owned());
}
match stored.delegated_agent_id() {
Ok(None) => Ok(()),
Ok(Some(agent)) => refused(format!(
"it was switched to agent {agent:?} with /agent:, which a fork cannot run as yet"
)),
Err(error) => refused(format!("{error:#}")),
}
}