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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! `rho/session/search` over the socket: the sessions `rho/session/list`
//! lists whose conversation holds every word of a query, from the session
//! store's search index, the one the agent's `sessions` tool searches
//! (`session::search_index`).
//!
//! A search never waits for a full read of the transcripts. Until the index
//! has read every one, as on a daemon's first search or after an upgrade
//! changed the index's layout, a background thread builds it, committing a
//! file at a time, and each search answers from what it holds so far with
//! `indexing` set. Once it is built, a search first reads the transcripts
//! changed since the last one, on its own blocking thread, retrying those
//! whose read failed, and says `indexing` while one of those has not
//! failed [`search_index::MAX_COUNTED_ATTEMPTS`] times yet.
use std::{
num::{NonZeroU32, NonZeroUsize},
path::PathBuf,
sync::{Arc, Mutex},
thread::JoinHandle,
time::Instant,
};
use rho_protocol::{
ErrorCode, RpcError, SessionSearchHit, SessionSearchParams, SessionSearchResponse,
};
use rho_sdk::CancellationToken;
use super::{catalog::summary_to_wire, Sessions};
use crate::{
app::row_locations,
session::{
search_index,
search_page::{self, SearchError, SearchRequest},
},
};
/// Sessions per page unless a client asks for another size. A hit with two
/// excerpts of ~320 characters is under a kilobyte, so a page is ~15 KB.
const DEFAULT_PAGE_SIZE: NonZeroUsize = NonZeroUsize::new(20).unwrap();
/// The most sessions one page holds: what a client shows at once, ~75 KB.
/// A client that wants more follows `next_cursor`.
const MAX_PAGE_SIZE: usize = 100;
/// The daemon's background build of the search index, so searches start
/// at most one at a time.
#[derive(Clone, Default)]
pub(super) struct IndexBuild(Arc<Mutex<Option<JoinHandle<()>>>>);
impl IndexBuild {
fn running(&self) -> bool {
self.lock()
.as_ref()
.is_some_and(|build| !build.is_finished())
}
/// Builds the index of session root `root` on a thread of its own,
/// unless a build runs already; `stopping` cancels it between files.
fn start(&self, root: PathBuf, stopping: CancellationToken) {
let mut build = self.lock();
if build.as_ref().is_some_and(|build| !build.is_finished()) {
return;
}
let spawned = std::thread::Builder::new()
.name("session-search-index".into())
.spawn(move || {
let started = Instant::now();
let built = search_index::open(&root).and_then(|mut connection| {
search_index::refresh(
&mut connection,
&root,
/*reconcile*/ false,
row_locations,
&stopping,
)
});
match built {
Ok(report) => tracing::info!(
elapsed = ?started.elapsed(),
files = report.files_updated,
bytes = report.bytes_read,
skipped = report.skipped_files,
"built the sessions search index"
),
Err(error) => {
tracing::warn!(%error, "could not build the sessions search index");
}
}
});
match spawned {
Ok(spawned) => *build = Some(spawned),
Err(error) => {
tracing::warn!(%error, "could not start building the sessions search index");
}
}
}
/// Waits for the build under way, if one is.
#[cfg(test)]
fn finish(&self) {
if let Some(build) = self.lock().take() {
build.join().unwrap();
}
}
fn lock(&self) -> std::sync::MutexGuard<'_, Option<JoinHandle<()>>> {
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
impl Sessions {
/// One page of the listed sessions whose conversation matches
/// `params.query`, newest first.
pub(in crate::app::daemon_host) async fn search(
&self,
params: SessionSearchParams,
) -> Result<SessionSearchResponse, RpcError> {
let limit = page_size(params.limit)?;
let request = search_page::check(SearchRequest {
query: ¶ms.query,
cwd: params.cwd.as_deref(),
cursor: params.cursor.as_deref(),
limit,
})
.map_err(refused)?;
let root = crate::session::session_root_in(&self.rho_home);
let build = self.index_build.clone();
let stopping = self.stopping.clone();
let searched = tokio::task::spawn_blocking(move || {
// Listing reconciles the catalog, which journals transcripts it
// only now found for the refresh below to read.
let listed = search_page::listed(&root, params.cwd.as_deref())?;
let mut connection = search_index::open(&root).map_err(SearchError::Store)?;
// Only the first read of every transcript is left to the
// build; afterwards a search reads changes itself.
let building = build.running()
|| !search_index::bootstrapped(&connection)
.map_err(|error| SearchError::Store(error.into()))?;
let indexing = if building {
build.start(root.clone(), stopping);
true
} else {
match search_index::refresh(
&mut connection,
&root,
/*reconcile*/ false,
row_locations,
&stopping,
) {
Ok(_) => !search_index::complete(&connection)
.map_err(|error| SearchError::Store(error.into()))?,
// Another process may hold the index a while; answer
// from what it holds, saying it may miss matches.
Err(error) => {
tracing::warn!(%error, "could not refresh the sessions search index");
true
}
}
};
let page = search_page::search(&connection, listed, request)?;
Ok::<_, SearchError>((page, indexing))
})
.await
.map_err(|error| failed(&error.into()))?;
let (page, indexing) = searched.map_err(refused)?;
Ok(SessionSearchResponse::new(
page.hits
.into_iter()
.map(|hit| {
SessionSearchHit::new(
summary_to_wire(hit.summary),
hit.matching_messages,
hit.excerpts,
)
})
.collect(),
page.next_cursor,
page.total,
indexing,
))
}
}
/// `limit` as a page size: [`DEFAULT_PAGE_SIZE`] when absent, and refused
/// over [`MAX_PAGE_SIZE`].
fn page_size(limit: Option<NonZeroU32>) -> Result<NonZeroUsize, RpcError> {
let Some(limit) = limit else {
return Ok(DEFAULT_PAGE_SIZE);
};
NonZeroUsize::try_from(limit)
.ok()
.filter(|size| size.get() <= MAX_PAGE_SIZE)
.ok_or_else(|| {
RpcError::new(
ErrorCode::InvalidParams,
format!(
"limit {limit} is over the session search's page limit (MAX_PAGE_SIZE = {MAX_PAGE_SIZE})"
),
)
})
}
/// The error a search answers for `error`: the request's own mistakes are
/// `InvalidParams`, the store's failures `InternalError`.
fn refused(error: SearchError) -> RpcError {
match error {
SearchError::BlankQuery
| SearchError::LongQuery(_)
| SearchError::RelativeCwd
| SearchError::UnknownCursor(_) => {
RpcError::new(ErrorCode::InvalidParams, error.to_string())
}
SearchError::Store(error) => failed(&error),
}
}
fn failed(error: &anyhow::Error) -> RpcError {
RpcError::new(
ErrorCode::InternalError,
format!("could not search the sessions: {error:#}"),
)
}
#[cfg(test)]
#[path = "search_tests.rs"]
mod tests;