1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Attaching a child that a `/jobs` row, an agent rail row, or an
//! `/attach` row names: a subagent run opens read-only in place, beside
//! any turn; a session is followed, between turns only.
use rho_protocol::SessionId;
use super::{agent_rail::ChildTarget, daemon_launch::DaemonTarget, App, DefaultTerminal};
use crate::session::{Session, SessionTarget};
/// Whether a turn runs here while a child is attached: following a session
/// switches sessions, which would leave the turn under way behind.
pub(super) enum AttachPhase<'a> {
Idle { terminal: &'a mut DefaultTerminal },
DuringTurn,
}
impl App {
/// Attaches `child` in `phase`, saying why when it cannot.
pub(super) async fn attach_child(&mut self, child: ChildTarget, phase: AttachPhase<'_>) {
match (child, phase) {
(ChildTarget::Run(run), _) => self.activate_subagent_row(&run),
(ChildTarget::Session(session), AttachPhase::Idle { terminal }) => {
self.attach_session(session, terminal).await;
}
(ChildTarget::Session(_), AttachPhase::DuringTurn) => {
self.set_status("could not attach session: a model turn is running here");
}
}
}
/// Follows `session`, between turns, saying why when it cannot. A draft
/// in the composer refuses it, since following a session clears the
/// composer: a click attaches with one, where `/jobs` took its place.
async fn attach_session(&mut self, session: SessionId, terminal: &mut DefaultTerminal) {
if !self.input_ui.text().is_empty() || !self.input_ui.attachments().is_empty() {
self.set_status("could not attach session: send or clear the draft first");
return;
}
// A saved session attaches as `/resume` does, with its checks, but
// from any workspace. One the daemon has open and has not saved
// yet, as a session just started, has no file to find: it is
// followed by its id.
let attached = match Session::workspace_of(session.as_str()) {
Ok(Some(workspace)) => {
let target = SessionTarget::new(session.as_str(), workspace);
self.resume_daemon_target(target, terminal).await
}
Ok(None) => self.follow_unsaved_session(session, terminal).await,
// The store could not say, so no check below would hold.
Err(error) => Err(error),
};
if let Err(error) = attached {
self.set_status(format!("could not attach session: {error:#}"));
}
}
/// Follows `session`, which the daemon has loaded and has not saved. A
/// launch that named an agent follows only that agent's sessions, and
/// an unsaved session's agent is recorded nowhere this TUI reads, so
/// such a launch refuses it until it saves.
async fn follow_unsaved_session(
&mut self,
session: SessionId,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
anyhow::ensure!(
self.info.session.resume_launch.definition.is_none(),
"the session has saved nothing yet, so its agent cannot be checked against --agent; \
attach once its first turn saves"
);
let short = super::session_picker::short_session_id(session.as_str());
self.switch_daemon_session(DaemonTarget::Resume(session), terminal)
.await?;
self.set_status(format!("attached session {short}"));
Ok(())
}
}