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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Following another daemon session in this TUI: `/new`, `/resume`, and a
//! `/sessions` pick.
//!
//! The session to follow opens on a connection of its own before anything
//! here changes, so a refusal leaves the TUI on the session it had; a new
//! one only connects there, as its first prompt opens it. Then
//! the old session's connection closes, which detaches it without
//! cancelling anything, and the screen resets. These commands run only
//! while the TUI shows no turn, so work is left behind only in a session
//! the TUI had stopped following, or whose turn began unseen; it keeps
//! running in the daemon and can be resumed. A resumed session's attach
//! replays its conversation as a first attach does. A session of another
//! workspace, as `/jobs` attaches to, moves the TUI to that workspace
//! before its replay shows.
use std::path::PathBuf;
use super::{
daemon_launch::DaemonTarget, palette::PaletteCaches, App, ComposerMode, DefaultTerminal,
InputSubmissionMode,
};
use crate::session::{Session, SessionTarget};
impl App {
/// Clears what the screen holds of the session it showed, for one that
/// starts empty or is about to replay.
pub(super) fn reset_session_view(
&mut self,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
self.input_ui.set_composer(ComposerMode::Input);
self.input_ui.clear_text();
self.input_ui.clear_paste_segments();
self.input_ui.set_shell_mode(None);
self.input_ui
.set_submission_mode(InputSubmissionMode::ParseCommands);
self.input_ui.reset_history_navigation();
self.input_ui.set_cursor(0);
self.cancel_all_pending_attachments();
self.input_ui.clear_attachments();
self.input_ui.set_command_palette_dismissed(false);
self.clamp_command_selection();
self.start_follow_ups = false;
self.daemon_compact_handoff = false;
self.pending.clear_follow_ups();
self.pending.clear_steering();
self.pending.clear_input_action();
self.pending_input_changed();
self.reset_streams();
self.end_busy_ui();
self.turn.clear_tool_calls();
self.reset_usage();
self.usage.current_context = None;
self.turn.set_current_turn_start(None);
self.discard_side_chat();
self.history.clear_entries();
self.history.images_mut().clear();
self.history.set_images_dirty_from(None);
self.history.lines_mut().invalidate_from(0);
self.scroll_history_to_bottom();
self.clamp_history_scroll_for_terminal(terminal)?;
Ok(())
}
/// Follows `target` in place of the daemon session followed now, or
/// keeps that one when `target` cannot be opened here.
pub(super) async fn switch_daemon_session(
&mut self,
target: DaemonTarget,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
let opened = self.info.session.daemon.open_sibling(target).await?;
self.info.session.session_id = opened.saved_id().map(str::to_owned);
// The session followed until now detaches as its connection drops.
self.info.session.daemon = opened;
let workspace = self.info.session.daemon.workspace().to_path_buf();
if workspace != self.info.runtime.cwd {
self.move_to_workspace(workspace);
}
self.sessions_hub_state.clear();
self.daemon_option_requests.clear();
self.daemon_login_selected = None;
self.daemon_unshown_switch = None;
self.daemon_title = None;
self.awaited_daemon_report = None;
self.reset_daemon_mcp_offers();
// Held for the session left behind.
self.held_turns.clear();
self.clear_tool_sources_loading_activity();
self.reset_daemon_rail();
self.drop_daemon_login();
self.reset_session_view(terminal)?;
// A new session sends no reset replay to clear what the old one
// left, such as its checklist, until its first prompt opens it.
self.clear_daemon_transcript();
// What the attach answered with, the session's options among it:
// a session at rest sends nothing more until it is prompted.
self.show_taken_daemon_views(terminal).await?;
self.sync_herdr_session().await;
Ok(())
}
/// Points what this TUI resolves against its workspace at `workspace`,
/// the followed session's, keeping `info.runtime.cwd` equal to
/// `DaemonSession::workspace`: the files and skills it looks up, and
/// the status line's directory, branch, and pull request.
fn move_to_workspace(&mut self, workspace: PathBuf) {
self.info.runtime.cwd = workspace;
self.palette_caches = PaletteCaches::default();
self.refresh_workspace_on_move();
}
/// Follows the saved session `id` names, resolved as `--resume` does,
/// when it is of this TUI's workspace.
pub(super) async fn resume_daemon_session(
&mut self,
id: &str,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
let target = Session::resolve_target(&self.info.runtime.cwd, id)?;
// An id lookup can find a session saved under another project;
// refused before the daemon opens it.
self.ensure_this_project(&target.cwd)?;
self.resume_daemon_target(target, terminal).await
}
/// Follows the saved session `target`, in whichever workspace it was
/// saved.
pub(super) async fn resume_daemon_target(
&mut self,
target: SessionTarget,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
// It runs as the agent it was saved as, so a launch that named an
// agent resumes only that agent's sessions, as at startup.
if let Some(definition) = &self.info.session.resume_launch.definition {
Session::validate_agent_definition_identity_of(&target, definition)?;
}
let short = super::session_picker::short_session_id(&target.id);
self.switch_daemon_session(
DaemonTarget::Resume(rho_protocol::SessionId::new(target.id)),
terminal,
)
.await?;
self.set_status(format!("resumed session {short}"));
self.request_daemon_context_handoff();
Ok(())
}
}