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
//! Reading a saved session the daemon has not loaded, without loading it:
//! opened only to be read, starting none of its MCP servers, OpenAPI
//! sources, or hooks, its worktree restored for the read and removed again,
//! and shut down again. An observe replays what the read found
//! (`observe.rs`), and history pages through it (`history.rs`).
use std::path::PathBuf;
use rho_protocol::{DetachReason, ErrorCode, RpcError};
use rho_sdk::SessionId;
use super::{
engine::Engine,
engine_replay::saved_engine,
open::open_to_read,
opening::opening_feed,
options::available_auths,
reopen::{saved_to_read, saved_workspace, ReopenGuard},
worktree::{Restore, Restored},
writing, Feed, Sessions,
};
use crate::{
app::daemon_host::jobs::{JobId, JobRecord, SessionJob},
session::SessionEngine,
};
impl Sessions {
/// The saved session whose reopen lock `reopening` holds through the
/// read, which the daemon has not loaded, read from what its run left:
/// opened only to be read (see [`open_to_read`]) and shut down again,
/// its worktree restored for the read and removed again.
pub(super) async fn read_unloaded(
&self,
reopening: &ReopenGuard<'_>,
) -> Result<Unloaded, RpcError> {
let id = reopening.id();
let mut last = match self.jobs.session_job(id) {
SessionJob::Busy(job) => return Err(writing(&job, id)),
SessionJob::Last(record) => Some(*record),
SessionJob::New => None,
};
// Read from what its run left, with no worktree to restore.
let workspace = saved_workspace(id).await?;
if let Some(engine) = saved_engine(workspace.clone(), id).await? {
let run = self.jobs.last_writer(id).map(Box::new);
return Ok(Unloaded::Engine {
workspace,
engine: Box::new(engine),
run,
});
}
let restored = match &mut last {
Some(record) => self.restore_worktree(record, Restore::Reading).await?,
None => Restored::Nothing,
};
let read = self.read_saved(id, last.clone()).await;
// Removed again however the read went.
if let (Restored::ForReading, Some(record)) = (restored, &last) {
Self::end_read_checkout(record).await;
}
read
}
/// Saved session `id` whose last job is `last`, as
/// [`Self::read_unloaded`] reads it once its worktree is on disk.
async fn read_saved(
&self,
id: &SessionId,
last: Option<JobRecord>,
) -> Result<Unloaded, RpcError> {
let stored = saved_to_read(id).await?;
// A session no daemon has run is idle, with no last turn. Its job
// is not registered: reading it records nothing.
let job = last.unwrap_or_else(|| {
JobRecord::new_session(JobId::generate(), stored.cwd().to_path_buf(), id.clone())
});
let opened = open_to_read(stored, &self.subagents, &self.settings, &job.id).await?;
// Read through `engine_replay` before this when an agent ran it.
let Engine::Rho(runtime) = opened.engine else {
return Err(RpcError::new(
ErrorCode::SessionOpenFailed,
format!("session {id} is run by an agent, whose journal shows it"),
));
};
let (feed, _, _) = opening_feed(
&job,
&runtime,
&opened.config,
&available_auths(runtime.credentials()).await,
);
// Unloaded, so nothing of it runs or wakes it: it is settled.
feed.update_activity(|| false);
feed.close(DetachReason::NotLoaded);
let workspace = runtime.workspace_root().to_path_buf();
runtime.shutdown().await;
Ok(Unloaded::Saved {
workspace,
feed: Box::new(feed),
})
}
}
/// A saved session the daemon has not loaded, as [`Sessions::read_unloaded`]
/// read it.
pub(super) enum Unloaded {
/// An agent runtime ran it, and its journal shows it.
Engine {
workspace: PathBuf,
engine: Box<SessionEngine>,
/// The job that last wrote it.
run: Option<Box<JobRecord>>,
},
/// Rho ran it: its feed, closed, which replays it as a reopen would.
Saved { workspace: PathBuf, feed: Box<Feed> },
}