Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ use crate::error::Result;
use crate::filters::Filters;
use crate::types::{BranchPools, BranchQuery, ChunkDetail, MessageRow, NeighborRow};

/// The window (in chunk_idx units) the default
/// [`SearchBackend::message_chunks`] composes from
/// [`SearchBackend::neighbors`]: wide enough to cover any
/// message in the corpus — chunk_idx is sparse in the publish
/// corpus (observed span 0..=1730, measured 2026-11-22 for
/// #12624) — with orders of magnitude of headroom.
pub const MESSAGE_CHUNKS_WINDOW: i64 = 1_000_000;

pub trait SearchBackend {
/// `"turso"` or `"local"` (the envelope's `backend` field).
fn name(&self) -> &'static str;
Expand Down Expand Up @@ -60,6 +68,19 @@ pub trait SearchBackend {
/// `chunk_idx` in the window), ordered by chunk_idx.
fn neighbors(&self, message_id: &str, chunk_idx: i64, n: i64) -> Result<Vec<NeighborRow>>;

/// Every chunk row of one message, ordered by chunk_idx:
/// the enumeration the full-content span filter (#12624)
/// classifies before a `--with-message` / hydration payload
/// may cross the output boundary. The default composes
/// [`Self::neighbors`] with a window wide enough to cover
/// any message ([`MESSAGE_CHUNKS_WINDOW`]); backends with a
/// native unbounded query override it. Wrappers that route
/// `neighbors` (the shard fan-out) inherit correct routing
/// through the default.
fn message_chunks(&self, message_id: &str) -> Result<Vec<NeighborRow>> {
self.neighbors(message_id, 0, MESSAGE_CHUNKS_WINDOW)
}

/// One message row by id (for `--with-message` payloads).
fn message(&self, message_id: &str) -> Result<Option<MessageRow>>;
}
Expand Down Expand Up @@ -90,6 +111,9 @@ impl<T: SearchBackend + ?Sized> SearchBackend for Box<T> {
fn neighbors(&self, message_id: &str, chunk_idx: i64, n: i64) -> Result<Vec<NeighborRow>> {
(**self).neighbors(message_id, chunk_idx, n)
}
fn message_chunks(&self, message_id: &str) -> Result<Vec<NeighborRow>> {
(**self).message_chunks(message_id)
}
fn message(&self, message_id: &str) -> Result<Option<MessageRow>> {
(**self).message(message_id)
}
Expand Down
6 changes: 6 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ pub fn details_sql(shape: BackendShape, n_ids: usize) -> String {
pub const NEIGHBORS_SQL: &str = "SELECT id, chunk_idx, text FROM chunks \
WHERE message_id = ? AND chunk_idx BETWEEN ? AND ? ORDER BY chunk_idx";

/// Every chunk row of one message, ordered by chunk_idx — the
/// enumeration the full-content span filter (#12624) classifies
/// before a message payload may cross the output boundary.
pub const MESSAGE_CHUNKS_SQL: &str = "SELECT id, chunk_idx, text FROM chunks \
WHERE message_id = ? ORDER BY chunk_idx";

pub const MESSAGE_SQL: &str = "SELECT id, role, text FROM messages WHERE id = ?";

// ------------------------------------------------------------- dates ----
Expand Down
20 changes: 19 additions & 1 deletion pkgs/atlas-search/crates/atlas-core/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use rusqlite::{Connection, OpenFlags};
use crate::backend::SearchBackend;
use crate::error::{Error, Result};
use crate::filters::{
Filters, LOCAL_SHAPE, MESSAGE_SQL, NEIGHBORS_SQL, details_sql, fts_stmt, local_allowed_stmt,
Filters, LOCAL_SHAPE, MESSAGE_CHUNKS_SQL, MESSAGE_SQL, NEIGHBORS_SQL, details_sql, fts_stmt,
local_allowed_stmt,
};
use crate::types::{
BranchHit, BranchPools, BranchQuery, ChunkDetail, MessageRow, NeighborRow, Row, Stmt, Value,
Expand Down Expand Up @@ -684,6 +685,23 @@ impl SearchBackend for LocalBackend {
.collect())
}

fn message_chunks(&self, message_id: &str) -> Result<Vec<NeighborRow>> {
let rows = self.execute(&Stmt::new(
MESSAGE_CHUNKS_SQL,
vec![Value::text(message_id)],
))?;
Ok(rows
.iter()
.filter_map(|r| {
Some(NeighborRow {
chunk_id: r.get_i64("id")?,
chunk_idx: r.get_i64("chunk_idx")?,
text: r.get_text("text")?.to_string(),
})
})
.collect())
}

fn message(&self, message_id: &str) -> Result<Option<MessageRow>> {
let rows = self.execute(&Stmt::new(MESSAGE_SQL, vec![Value::text(message_id)]))?;
Ok(rows.first().and_then(|r| {
Expand Down
Loading