use std::{
collections::BTreeSet,
fs::{self, File},
io::{BufRead, BufReader, Read, Seek, SeekFrom},
path::{Path, PathBuf},
time::UNIX_EPOCH,
};
use rho_protocol::SearchLocation;
use rho_sdk::CancellationToken;
use rusqlite::{params, Connection, OptionalExtension, Transaction};
use serde::Serialize;
use sha2::{Digest, Sha256};
use crate::sqlite_support::{OwnerOnlySqlite, ParentDirectoryPrivacy};
use super::{
layout::{self, SessionUnit},
persistence::active_display_from_tree,
search_evidence,
tree::{ProjectedMessage, SessionTree},
workspace_scope::Workspace,
SessionOrigin,
};
pub(crate) type Locate = fn(&[ProjectedMessage]) -> Vec<Option<SearchLocation>>;
#[cfg(test)]
#[path = "search_index_tests.rs"]
mod tests;
#[derive(Default, Debug, Serialize)]
pub(crate) struct Refresh {
pub reconciled: bool,
pub files_checked: usize,
pub files_updated: usize,
pub bytes_read: u64,
pub skipped_files: usize,
pub omitted_records: usize,
}
pub(crate) fn open(root: &Path) -> anyhow::Result<Connection> {
let path = root.join("search.sqlite3");
for database in [&path, &root.join("index.sqlite3")] {
anyhow::ensure!(
!fs::symlink_metadata(database).is_ok_and(|meta| meta.file_type().is_symlink()),
"refusing a symlinked sessions index"
);
}
let database = OwnerOnlySqlite::open(path, ParentDirectoryPrivacy::EnforcePrivate, migrate)?;
Ok(database.into_connection())
}
const SCHEMA_VERSION: u32 = 7;
#[derive(Debug, thiserror::Error)]
enum OpenError {
#[error("sessions search I/O failed: {0}")]
Io(#[from] std::io::Error),
#[error("sessions search SQLite operation failed: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("unsupported sessions search schema {0}; this build supports schema {SCHEMA_VERSION}")]
UnsupportedSchema(u32),
}
fn migrate(connection: &mut Connection) -> Result<(), OpenError> {
connection.pragma_update(None, "foreign_keys", true)?;
let version: u32 = connection.query_row("pragma user_version", [], |row| row.get(0))?;
if version == SCHEMA_VERSION {
return Ok(());
}
let transaction =
connection.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
let version: u32 = transaction.query_row("pragma user_version", [], |row| row.get(0))?;
if version > SCHEMA_VERSION {
return Err(OpenError::UnsupportedSchema(version));
}
if version == SCHEMA_VERSION {
return Ok(());
}
if version > 0 {
transaction.execute_batch(
"drop trigger if exists evidence_insert;
drop trigger if exists evidence_delete;
drop table if exists evidence_fts;
drop table if exists evidence;
drop table if exists pending;
drop table if exists cursor;
drop table if exists files;",
)?;
}
transaction.execute_batch(
"create table files (
key text primary key, path text not null, id text not null,
cwd text not null, worktree text not null, repo text not null,
size integer not null, stamp text not null, omitted integer not null,
person_facing integer not null);
create index files_repo on files(repo);
create unique index files_path on files(path);
create index files_worktree on files(worktree);
create table cursor (identity text not null, seq integer not null);
create table pending (path text primary key, attempts integer not null);
create table evidence (
rowid integer primary key, session text not null references files(key) on delete cascade,
anchor text not null, role text not null, text text not null,
omitted integer not null, at_kind text, at_id text, saved_at integer,
unique(session, anchor));
create index evidence_session_order on evidence(session,rowid);
create virtual table evidence_fts using fts5(
text, content='evidence', content_rowid='rowid', tokenize='porter unicode61');
create trigger evidence_insert after insert on evidence begin
insert into evidence_fts(rowid,text) values(new.rowid,new.text);
end;
create trigger evidence_delete after delete on evidence begin
insert into evidence_fts(evidence_fts,rowid,text) values('delete',old.rowid,old.text);
end;",
)?;
transaction.pragma_update(None, "user_version", SCHEMA_VERSION)?;
transaction.commit()?;
Ok(())
}
#[derive(Debug)]
struct Stamp {
size: u64,
modified: String,
}
impl Stamp {
fn read(path: &Path) -> anyhow::Result<Self> {
let metadata = fs::symlink_metadata(path)?;
anyhow::ensure!(
metadata.is_file(),
"session transcript is not a regular file"
);
Ok(Self {
size: metadata.len(),
modified: metadata
.modified()?
.duration_since(UNIX_EPOCH)?
.as_nanos()
.to_string(),
})
}
}
struct IndexedFile {
stamp: Stamp,
cwd: PathBuf,
workspace: Workspace,
}
fn resolve_workspace(cwd: &Path, indexed: Option<&IndexedFile>) -> Workspace {
if let Some(existing) = cwd.canonicalize().ok().filter(|path| path.is_dir()) {
return Workspace::resolve(&existing);
}
if let Some(indexed) = indexed.filter(|indexed| indexed.cwd == cwd) {
return indexed.workspace.clone();
}
Workspace::resolve(cwd)
}
pub(crate) fn refresh(
connection: &mut Connection,
root: &Path,
reconcile: bool,
locate: Locate,
cancellation: &CancellationToken,
) -> anyhow::Result<Refresh> {
let cursor = connection
.query_row("select identity,seq from cursor", [], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.optional()?;
let changes = super::search_journal::since(root, cursor.as_ref().map_or(0, |(_, seq)| *seq))?;
let reconcile = reconcile
|| cursor
.as_ref()
.is_none_or(|(identity, seq)| identity != &changes.identity || *seq > changes.through);
let mut paths: BTreeSet<PathBuf> = if reconcile {
discover(root)?
} else {
changes.paths.into_iter().collect()
};
if reconcile {
paths.extend(
connection
.prepare("select path from files")?
.query_map([], |row| row.get::<_, String>(0).map(PathBuf::from))?
.collect::<rusqlite::Result<Vec<_>>>()?,
);
}
paths.extend(
connection
.prepare("select path from pending")?
.query_map([], |row| row.get::<_, String>(0).map(PathBuf::from))?
.collect::<rusqlite::Result<Vec<_>>>()?,
);
let mut report = Refresh {
reconciled: reconcile,
..Refresh::default()
};
for path in paths {
anyhow::ensure!(!cancellation.is_cancelled(), "sessions indexing cancelled");
let transaction =
connection.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
refresh_file(&transaction, root, &path, locate, cancellation, &mut report)?;
anyhow::ensure!(!cancellation.is_cancelled(), "sessions indexing cancelled");
transaction.commit()?;
}
let transaction =
connection.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
report.omitted_records =
transaction.query_row("select coalesce(sum(omitted),0) from files", [], |row| {
row.get(0)
})?;
let stored = transaction
.query_row("select identity,seq from cursor", [], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.optional()?;
if !stored.is_some_and(|(identity, seq)| identity == changes.identity && seq >= changes.through)
{
transaction.execute("delete from cursor", [])?;
transaction.execute(
"insert into cursor values(?1,?2)",
params![changes.identity, changes.through],
)?;
}
transaction.commit()?;
if let Err(error) = vacuum_if_bloated(connection, VACUUM_MIN_FREE_BYTES) {
tracing::warn!(%error, "could not vacuum the sessions search index");
}
Ok(report)
}
pub(crate) const MAX_COUNTED_ATTEMPTS: u32 = 3;
pub(crate) fn bootstrapped(connection: &Connection) -> rusqlite::Result<bool> {
connection.query_row("select exists(select 1 from cursor)", [], |row| row.get(0))
}
pub(crate) fn complete(connection: &Connection) -> rusqlite::Result<bool> {
connection.query_row(
"select exists(select 1 from cursor)
and not exists(select 1 from pending where attempts<?1)",
[MAX_COUNTED_ATTEMPTS],
|row| row.get(0),
)
}
fn refresh_file(
transaction: &Transaction<'_>,
root: &Path,
path: &Path,
locate: Locate,
cancellation: &CancellationToken,
report: &mut Refresh,
) -> anyhow::Result<()> {
let path_string = path.to_string_lossy();
let attempts: u32 = transaction
.query_row(
"select attempts from pending where path=?1",
[&path_string],
|row| row.get(0),
)
.optional()?
.unwrap_or(0);
transaction.execute("delete from pending where path=?1", [&path_string])?;
report.files_checked += 1;
if !path.try_exists()? {
transaction.execute("delete from files where path=?1", [&path_string])?;
return Ok(());
}
let indexed = transaction
.query_row(
"select size,stamp,cwd,worktree,repo from files where path=?1",
[&path_string],
|row| {
Ok(IndexedFile {
stamp: Stamp {
size: row.get(0)?,
modified: row.get(1)?,
},
cwd: PathBuf::from(row.get::<_, String>(2)?),
workspace: Workspace {
worktree: PathBuf::from(row.get::<_, String>(3)?),
repo: PathBuf::from(row.get::<_, String>(4)?),
},
})
},
)
.optional()?;
let stamp = Stamp::read(path);
if stamp.as_ref().is_ok_and(|stamp| {
indexed.as_ref().is_some_and(|indexed| {
indexed.stamp.size == stamp.size && indexed.stamp.modified == stamp.modified
})
}) {
if let Some(indexed) = &indexed {
let workspace = resolve_workspace(&indexed.cwd, Some(indexed));
transaction.execute(
"update files set worktree=?1,repo=?2 where path=?3",
params![
workspace.worktree.to_string_lossy(),
workspace.repo.to_string_lossy(),
path_string,
],
)?;
}
return Ok(());
}
transaction.execute("delete from files where path=?1", [&path_string])?;
let result = (|| {
let relative = path.strip_prefix(root)?;
let mut checked = root.to_path_buf();
for component in relative.components() {
anyhow::ensure!(
matches!(component, std::path::Component::Normal(_)),
"invalid session path"
);
checked.push(component);
anyhow::ensure!(
!fs::symlink_metadata(&checked)?.file_type().is_symlink(),
"symlinked session path"
);
}
let id = SessionUnit::from_path(path)
.and_then(|unit| unit.id())
.ok_or_else(|| anyhow::anyhow!("invalid session unit"))?;
index_file(
transaction,
root,
path,
&id,
&stamp?,
indexed.as_ref(),
locate,
)
})();
match result {
Ok(bytes) => {
report.files_updated += 1;
report.bytes_read += bytes;
}
Err(error) => {
anyhow::ensure!(!cancellation.is_cancelled(), "{error}");
transaction.execute("delete from files where path=?1", [&path_string])?;
if transient(&error) {
transaction.execute(
"insert or ignore into pending(path,attempts) values(?1,?2)",
params![path_string, attempts.saturating_add(1)],
)?;
} else {
tracing::debug!(%error, path = %path.display(), "not a session transcript; not indexed");
}
report.skipped_files += 1;
}
}
Ok(())
}
const VACUUM_MIN_FREE_BYTES: u64 = 50 * 1024 * 1024;
#[derive(Debug)]
struct Pages {
size: u64,
total: u64,
free: u64,
}
impl Pages {
fn read(connection: &Connection) -> rusqlite::Result<Self> {
let pragma = |name| connection.pragma_query_value(None, name, |row| row.get(0));
Ok(Self {
size: pragma("page_size")?,
total: pragma("page_count")?,
free: pragma("freelist_count")?,
})
}
fn worth_vacuuming(&self, min_free_bytes: u64) -> bool {
self.free * self.size >= min_free_bytes && self.free * 4 >= self.total
}
}
fn vacuum_if_bloated(connection: &Connection, min_free_bytes: u64) -> rusqlite::Result<()> {
let pages = Pages::read(connection)?;
if !pages.worth_vacuuming(min_free_bytes) {
return Ok(());
}
connection.execute_batch("vacuum")?;
let checkpoint_blocked: bool =
connection.query_row("pragma wal_checkpoint(truncate)", [], |row| row.get(0))?;
tracing::debug!(
?pages,
checkpoint_blocked,
"vacuumed the sessions search index"
);
Ok(())
}
fn discover(root: &Path) -> anyhow::Result<BTreeSet<PathBuf>> {
let mut paths = BTreeSet::new();
for workspace in fs::read_dir(root)? {
let workspace = workspace?;
if !workspace.file_type()?.is_dir() {
continue;
}
for entry in fs::read_dir(workspace.path())? {
let entry = entry?;
if entry.file_type()?.is_symlink() {
continue;
}
let Some(unit) = SessionUnit::from_path(&entry.path()) else {
continue;
};
paths.insert(unit.transcript_path());
}
}
Ok(paths)
}
fn index_file(
connection: &Connection,
root: &Path,
path: &Path,
id: &str,
stamp: &Stamp,
indexed: Option<&IndexedFile>,
locate: Locate,
) -> anyhow::Result<u64> {
let transcript = match Transcript::load(root, path, id, stamp) {
Ok(transcript) => transcript,
Err(error) if transient(&error) => return Err(error),
Err(error) => {
tracing::debug!(%error, path = %path.display(), "session does not load; not indexed");
Transcript::unloadable()
}
};
let workspace = if transcript.cwd.as_os_str().is_empty() {
Workspace {
worktree: PathBuf::new(),
repo: PathBuf::new(),
}
} else {
resolve_workspace(&transcript.cwd, indexed)
};
let path_string = path.to_string_lossy();
let key = hex::encode(Sha256::digest(path_string.as_bytes()));
connection.execute(
"insert into files(key,path,id,cwd,worktree,repo,size,stamp,omitted,person_facing) values(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10)",
params![key, path_string, id, transcript.cwd.to_string_lossy(), workspace.worktree.to_string_lossy(), workspace.repo.to_string_lossy(), stamp.size, stamp.modified, transcript.omitted, transcript.person_facing],
)?;
let mut insert = connection.prepare(
"insert into evidence(session,anchor,role,text,omitted,at_kind,at_id,saved_at)
values(?1,?2,?3,?4,?5,?6,?7,?8)",
)?;
let locations = locate(&transcript.rows);
for evidence in search_evidence::extract(&transcript.rows, locations) {
let (at_kind, at_id) = match &evidence.at {
Some(SearchLocation::Message { id }) => (Some("message"), Some(id.as_str())),
Some(SearchLocation::ToolCall { id }) => (Some("tool_call"), Some(id.as_str())),
Some(_) | None => (None, None),
};
insert.execute(params![
key,
evidence.anchor,
evidence.role,
evidence.text,
evidence.omitted_blocks,
at_kind,
at_id,
evidence.saved_at,
])?;
}
let after = Stamp::read(path)?;
if after.size != stamp.size || after.modified != stamp.modified {
return Err(ChangedWhileRead.into());
}
Ok(stamp.size)
}
struct Transcript {
cwd: PathBuf,
person_facing: bool,
rows: Vec<ProjectedMessage>,
omitted: usize,
}
impl Transcript {
fn load(root: &Path, path: &Path, id: &str, stamp: &Stamp) -> anyhow::Result<Self> {
let mut reader = BufReader::new(File::open(path)?);
let mut line = String::new();
reader.read_line(&mut line)?;
let header: serde_json::Value = serde_json::from_str(&line)?;
anyhow::ensure!(header["type"] == "session", "missing session header");
let version = header["version"].as_u64().unwrap_or(0);
anyhow::ensure!(
super::persistence::SUPPORTED_SESSION_VERSIONS.contains(&version),
"unsupported session format {version}"
);
anyhow::ensure!(
header["id"].as_str() == Some(id),
"session identity mismatch"
);
let cwd = PathBuf::from(
header["cwd"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("missing session workspace"))?,
);
anyhow::ensure!(
path.starts_with(layout::session_dir_in_root(root, &cwd)),
"session workspace mismatch"
);
let person_facing = serde_json::from_value::<SessionOrigin>(header["origin"].clone())
.ok()
.is_none_or(SessionOrigin::is_person_facing);
let rows = active_display_from_tree(&SessionTree::load(path)?)?;
let torn = torn_tail(&mut reader.into_inner(), stamp.size)?;
Ok(Self {
cwd,
person_facing,
rows,
omitted: usize::from(torn),
})
}
fn unloadable() -> Self {
Self {
cwd: PathBuf::new(),
person_facing: false,
rows: Vec::new(),
omitted: 1,
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("session changed during indexing; retry")]
struct ChangedWhileRead;
fn transient(error: &anyhow::Error) -> bool {
error.chain().any(|cause| {
cause.is::<ChangedWhileRead>()
|| cause
.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() != std::io::ErrorKind::InvalidData)
})
}
fn torn_tail(file: &mut File, size: u64) -> std::io::Result<bool> {
const CHUNK: u64 = 64 * 1024;
let mut start = 0;
let mut end = size;
let mut chunk = Vec::new();
while end > 0 {
let from = end.saturating_sub(CHUNK);
chunk.resize((end - from) as usize, 0);
file.seek(SeekFrom::Start(from))?;
file.read_exact(&mut chunk)?;
if let Some(newline) = chunk.iter().rposition(|&byte| byte == b'\n') {
start = from + newline as u64 + 1;
break;
}
end = from;
}
let mut tail = vec![0; (size - start) as usize];
file.seek(SeekFrom::Start(start))?;
file.read_exact(&mut tail)?;
Ok(!tail.trim_ascii().is_empty()
&& serde_json::from_slice::<serde::de::IgnoredAny>(&tail)
.is_err_and(|error| error.is_eof()))
}