Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Seanaye/perf/scoped search (#7269)
Sean Aye2ca78f6a9847
diff --git a/Cargo.lock b/Cargo.lockindex c8895e6078..657faa7041 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3313,7 +3313,7 @@ dependencies = [[[package]]name = "cache-wasm"-version = "0.6.33"+version = "0.6.34"dependencies = ["async-lock","cache-core",diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/playwright.config.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/playwright.config.tsindex 769c77ea7a..f3b9d74b58 100644--- a/apps/web/src/lib/graphql-cache/worker/browser-test/playwright.config.ts+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/playwright.config.ts@@ -13,6 +13,7 @@ export default defineConfig({'mail-projection.browser.e2e.ts','mail-tabs.browser.e2e.ts','query-write-scope.browser.e2e.ts',+ 'search-buckets.browser.e2e.ts',],timeout: 90_000,fullyParallel: false,diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.browser.e2e.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.browser.e2e.tsnew file mode 100644index 0000000000..c1465d1e4e--- /dev/null+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.browser.e2e.ts@@ -0,0 +1,28 @@+import { expect, test } from '@playwright/test';++test('bucket-scoped cold/warm searches keep deterministic results with a large excluded bucket', async ({+ page,+}) => {+ await page.goto('search-buckets.html');+ const result = page.locator('#result');+ await expect(result).toHaveAttribute('data-status', 'ready');+ await page.getByRole('button', { name: 'Run scoped searches' }).click();+ await expect(result).toHaveAttribute('data-status', 'passed');+ const data = JSON.parse((await result.textContent())!);+ expect(data.cold).toEqual(+ Array.from(+ { length: 20 },+ (_, index) => `GraphqlSoupDocument:item-${String(index).padStart(5, '0')}`+ )+ );+ expect(data.email).toEqual(+ Array.from(+ { length: 20 },+ (_, index) =>+ `GraphqlSoupEmailThread:item-${String(index + 100).padStart(5, '0')}`+ )+ );+ expect(data.all).toEqual(data.cold);+ // Timings are diagnostic, not flaky wall-clock assertions.+ expect(data.measurements).toHaveLength(8);+});diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.html b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.htmlnew file mode 100644index 0000000000..9ace2f31ee--- /dev/null+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.html@@ -0,0 +1,9 @@+<!doctype html>+<html lang="en">+ <head><meta charset="utf-8" /><link rel="icon" href="data:," /><title>Bucket-scoped search regression</title></head>+ <body>+ <button id="search" disabled>Run scoped searches</button>+ <pre id="result" data-status="seeding"></pre>+ <script type="module" src="./search-buckets.ts"></script>+ </body>+</html>diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.tsnew file mode 100644index 0000000000..65e869bc79--- /dev/null+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/search-buckets.ts@@ -0,0 +1,73 @@+// Real production host/WASM/OPFS. Synthetic data only: no hosted data or auth.+import { createWorkerCacheHost } from '../../host/worker-host';++const result = document.querySelector<HTMLPreElement>('#result')!;+const button = document.querySelector<HTMLButtonElement>('#search')!;+const host = createWorkerCacheHost({+ scope: `search-buckets-${crypto.randomUUID()}`,+});+const query = `query Seed($input: SoupInput!) { user { id soup(input: $input) { items { __typename id ... on GraphqlSoupDocument { name } ... on GraphqlSoupEmailThread { name } } } } }`;+const emailCount = 10_000;+const documentCount = 100;+const measurements: { buckets: string[]; ms: number; count: number }[] = [];++async function search(buckets: string[]) {+ const start = performance.now();+ const page = await host.search({+ profile: 'quick-access-v1',+ buckets,+ query: 'needle',+ limit: 20,+ });+ measurements.push({+ buckets,+ ms: performance.now() - start,+ count: page.documents.length,+ });+ return page.documents.map((document) => document.recordKey);+}++try {+ for (let offset = 0; offset < emailCount + documentCount; offset += 500) {+ const items = Array.from(+ { length: Math.min(500, emailCount + documentCount - offset) },+ (_, index) => {+ const id = index + offset;+ return {+ __typename:+ id < documentCount+ ? 'GraphqlSoupDocument'+ : 'GraphqlSoupEmailThread',+ id: `item-${id.toString().padStart(5, '0')}`,+ name: `needle ${id}`,+ };+ }+ );+ await host.writeQuery({+ query,+ variables: { input: { initial: { limit: offset + 1 } } },+ data: { user: { id: 'fixture-viewer', soup: { items } } },+ });+ }+ result.dataset.status = 'ready';+ result.textContent = `${documentCount} documents and ${emailCount} emails ready`;+ button.disabled = false;+ button.addEventListener('click', async () => {+ button.disabled = true;+ try {+ const cold = await search(['document']);+ for (let i = 0; i < 5; i++) await search(['document']);+ const email = await search(['email']);+ const all = await search([]);+ result.textContent = JSON.stringify({ measurements, cold, email, all });+ result.dataset.status = 'passed';+ } catch (error) {+ result.textContent = String(error);+ result.dataset.status = 'failed';+ }+ });+} catch (error) {+ result.textContent = String(error);+ result.dataset.status = 'failed';+}+window.addEventListener('pagehide', () => host.dispose(), { once: true });diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/vite.config.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/vite.config.tsindex 9411ebabfa..693e278eb0 100644--- a/apps/web/src/lib/graphql-cache/worker/browser-test/vite.config.ts+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/vite.config.ts@@ -54,6 +54,7 @@ export default defineConfig(({ command }) => ({'notification-projection.html','mail-projection.html','query-write-scope.html',+ 'search-buckets.html',].map((name) => [name.replace('.html', ''), resolve(directory, name)])),},diff --git a/crates/client/README.md b/crates/client/README.mdindex 66eceedb2b..4938bf96c9 100644--- a/crates/client/README.md+++ b/crates/client/README.md@@ -97,6 +97,21 @@ sent as network revalidations. Callers should supply an explicit targeted queryfor recovery when the parent/field is missing; the exchange can then enqueueentity-only optimism and retain that recovery query for eventual commit/replay.+## Text search catalogs++Text search loads compact rows lazily per `(profile, bucket)` through the existing+`search_documents_browse_idx`. Searching documents does not load cached emails;+changing categories loads only newly requested buckets, including caching empty+buckets. Empty bucket selection still means every bucket in the profile.++Ranking borrows catalog and optimistic entries, normalizes the query once, and+retains at most `limit + 1` references in a heap. Only the final returned rows are+cloned. Fuzzy/freshness scoring, DM priority, recency/key tie breaks and browse+cursors are unchanged. Write-through updates remove old bucket membership before+updating already-loaded buckets; unopened buckets stay lazy. Optimistic shadows+replace durable hits without mutating the catalogs, so rollback restores them.+No storage schema, projection version, or database migration changes are needed.+## Browser OPFS writesThe OPFS adapter coalesces each Turso vectored write into batches of at mostdiff --git a/crates/client/cache-core/src/engine.rs b/crates/client/cache-core/src/engine.rsindex a6f2b5b5b5..f11093fbce 100644--- a/crates/client/cache-core/src/engine.rs+++ b/crates/client/cache-core/src/engine.rs@@ -48,8 +48,8 @@ use crate::record_selection::{};use crate::revision::{CacheRevision, Revisioned};use crate::search::{- SearchCursor, SearchDocument, SearchError, SearchPage, SearchProfile, SearchRequest,- collect_search_changes, compare_recent, fuzzy_freshness_score, project_search_documents,+ SearchCatalogs, SearchCursor, SearchDocument, SearchError, SearchPage, SearchProfile,+ SearchRequest, collect_search_changes, project_search_documents, rank_documents,snapshot_search_fields, validate_search_request,};use crate::store::{QueueDiagnostics, Storage};@@ -324,7 +324,7 @@ pub struct Engine<S: Storage> {optimistic_hydrated: bool,/// Compact durable catalogs are loaded lazily for text search. Empty/// queries use the storage index directly and do not populate this map.- search_catalogs: HashMap<SearchProfile, HashMap<EntityKey<'static>, SearchDocument>>,+ search_catalogs: SearchCatalogs,}impl<S: Storage> Engine<S> {@@ -342,7 +342,7 @@ impl<S: Storage> Engine<S> {identity: IdentityState::NotHydrated,optimistic: Vec::new(),optimistic_hydrated: false,- search_catalogs: HashMap::new(),+ search_catalogs: SearchCatalogs::default(),}}@@ -906,7 +906,7 @@ impl<S: Storage> Engine<S> {/// decoding normalized-record payloads.////// Empty queries fan out over the per-profile/per-bucket timestamp index.- /// Text queries lazily load one compact catalog and rank it in memory.+ /// Text queries lazily load only requested buckets and rank borrowed entries./// Active optimistic layers are projected from their fully composed record/// values and overlaid explicitly on either durable path.pub async fn search(@@ -929,7 +929,7 @@ impl<S: Storage> Engine<S> {let overlay = self.optimistic_search_overlay(request.profile).await?;let trimmed_query = request.query.trim();- let mut candidates: HashMap<EntityKey<'static>, SearchDocument> =+ let browse_candidates: HashMap<EntityKey<'static>, SearchDocument> =if trimmed_query.is_empty() {// Fetch enough extra durable rows to compensate for optimistic// replacements/removals without turning this into a record scan.@@ -955,71 +955,40 @@ impl<S: Storage> Engine<S> {}candidates} else {- if !self.search_catalogs.contains_key(&request.profile) {- let documents = self- .storage- .load_search_documents(request.profile)- .await- .map_err(EngineError::Storage)?;- self.search_catalogs.insert(- request.profile,- documents- .into_iter()- .map(|document| (document.record_key.clone(), document))- .collect(),- );+ for bucket in &buckets {+ // Unknown (but syntactically valid) buckets have no projection.+ // Do not grow the catalog map with arbitrary empty names.+ if !request.profile.buckets().contains(&bucket.as_str()) {+ continue;+ }+ if self.search_catalogs.get(request.profile, bucket).is_none() {+ let documents = self+ .storage+ .load_search_documents(request.profile, bucket)+ .await+ .map_err(EngineError::Storage)?;+ self.search_catalogs+ .insert(request.profile, bucket.clone(), documents);+ }}- self.search_catalogs[&request.profile].clone()+ HashMap::new()};- for (key, document) in overlay {- candidates.remove(&key);- if let Some(document) = document- && bucket_set.contains(document.bucket.as_str())- && cursor_allows(request.cursor.as_ref(), &document)- {- candidates.insert(key, document);- }- }-- let mut scored: Vec<(SearchDocument, f64)> = candidates- .into_values()+ let cached = buckets+ .iter()+ .filter(|_| !trimmed_query.is_empty())+ .filter_map(|bucket| self.search_catalogs.get(request.profile, bucket))+ .flat_map(|catalog| catalog.values());+ // A shadow removes its durable counterpart even when the optimistic+ // value is deleted, moved to another bucket, or excluded by the cursor.+ let candidates = browse_candidates+ .values()+ .chain(cached)+ .filter(|document| !overlay.contains_key(&document.record_key))+ .chain(overlay.values().filter_map(Option::as_ref)).filter(|document| bucket_set.contains(document.bucket.as_str()))- .filter(|document| cursor_allows(request.cursor.as_ref(), document))- .filter_map(|document| {- let score = if trimmed_query.is_empty() {- Some(0.0)- } else {- fuzzy_freshness_score(&document, trimmed_query, request.now_ms)- }?;- Some((document, score))- })- .collect();- if trimmed_query.is_empty() {- scored.sort_by(|(left, _), (right, _)| compare_recent(left, right));- } else {- scored.sort_by(|(left, left_score), (right, right_score)| {- right_score- .total_cmp(left_score)- .then_with(|| compare_recent(left, right))- });- }- let has_more = scored.len() > request.limit;- scored.truncate(request.limit);- let documents: Vec<_> = scored.into_iter().map(|(document, _)| document).collect();- let next_cursor = (trimmed_query.is_empty() && has_more).then(|| {- let last = documents- .last()- .expect("a truncated search page contains a document");- SearchCursor {- timestamp_ms: last.timestamp_ms,- record_key: last.record_key.clone(),- }- });- Ok(SearchPage {- documents,- next_cursor,- })+ .filter(|document| cursor_allows(request.cursor.as_ref(), document));+ Ok(rank_documents(request, candidates))}async fn optimistic_search_overlay(@@ -1045,19 +1014,7 @@ impl<S: Storage> Engine<S> {}fn update_loaded_search_catalogs(&mut self, entries: &[(EntityKey<'static>, Record)]) {- if self.search_catalogs.is_empty() {- return;- }- for (key, record) in entries {- for catalog in self.search_catalogs.values_mut() {- catalog.remove(key);- }- for document in project_search_documents(key, record) {- if let Some(catalog) = self.search_catalogs.get_mut(&document.profile) {- catalog.insert(key.clone(), document);- }- }- }+ self.search_catalogs.update(entries);}/// Normalizes and stores a network response. Returns changed records and@@ -2446,9 +2403,7 @@ impl<S: Storage> Engine<S> {.map_err(EngineError::Storage)?;for key in keys {self.hot.pop(key);- for catalog in self.search_catalogs.values_mut() {- catalog.remove(key);- }+ self.search_catalogs.remove(key);}self.advance_revision()?;Ok(self.revisioned(affected))@@ -2604,9 +2559,7 @@ impl<S: PredicateIndexStorage> Engine<S> {.map_err(EngineError::Storage)?;for key in keys {self.hot.pop(key);- for catalog in self.search_catalogs.values_mut() {- catalog.remove(key);- }+ self.search_catalogs.remove(key);}self.advance_revision()?;Ok(self.revisioned(affected))diff --git a/crates/client/cache-core/src/search.rs b/crates/client/cache-core/src/search.rsindex f9d5e118a5..3b79e6b1ca 100644--- a/crates/client/cache-core/src/search.rs+++ b/crates/client/cache-core/src/search.rs@@ -12,8 +12,12 @@ use std::cmp::Ordering;use std::collections::BTreeSet;use thiserror::Error;+mod catalog;mod changes;+mod ranking;+pub(crate) use catalog::SearchCatalogs;pub(crate) use changes::{collect_search_changes, snapshot_search_fields};+pub(crate) use ranking::rank_documents;/// Current compact projection profile used by Quick Access, Cmd-K and entity/// mention pickers. Profile names are persisted and therefore versioned.@@ -373,7 +377,10 @@ pub fn compare_recent(left: &SearchDocument, right: &SearchDocument) -> Ordering/// match as an ordered subsequence. Fuzzy relevance contributes 70%, freshness/// contributes 30%, and matching direct messages receive the legacy 1.8x boost.pub fn fuzzy_freshness_score(document: &SearchDocument, query: &str, now_ms: i64) -> Option<f64> {- let normalized = normalize_search_text(query);+ score_normalized_query(document, &normalize_search_text(query), now_ms)+}++fn score_normalized_query(document: &SearchDocument, normalized: &str, now_ms: i64) -> Option<f64> {if normalized.is_empty() {return Some(1.0);}diff --git a/crates/client/cache-core/src/search/catalog.rs b/crates/client/cache-core/src/search/catalog.rsnew file mode 100644index 0000000000..96b4b9926e--- /dev/null+++ b/crates/client/cache-core/src/search/catalog.rs@@ -0,0 +1,64 @@+//! Lazily loaded compact text-search catalogs, partitioned by profile/bucket.++use super::{SearchDocument, SearchProfile, project_search_documents};+use crate::value::{EntityKey, Record};+use std::collections::HashMap;++type Catalog = HashMap<EntityKey<'static>, SearchDocument>;++#[derive(Default)]+pub(crate) struct SearchCatalogs {+ profiles: HashMap<SearchProfile, HashMap<String, Catalog>>,+}++impl SearchCatalogs {+ pub fn get(&self, profile: SearchProfile, bucket: &str) -> Option<&Catalog> {+ self.profiles.get(&profile)?.get(bucket)+ }++ pub fn insert(+ &mut self,+ profile: SearchProfile,+ bucket: String,+ documents: Vec<SearchDocument>,+ ) {+ self.profiles.entry(profile).or_default().insert(+ bucket,+ documents+ .into_iter()+ .map(|document| (document.record_key.clone(), document))+ .collect(),+ );+ }++ pub fn clear(&mut self) {+ self.profiles.clear();+ }++ pub fn remove(&mut self, key: &EntityKey<'static>) {+ for buckets in self.profiles.values_mut() {+ for catalog in buckets.values_mut() {+ catalog.remove(key);+ }+ }+ }++ pub fn update(&mut self, entries: &[(EntityKey<'static>, Record)]) {+ if self.profiles.is_empty() {+ return;+ }+ for (key, record) in entries {+ // Remove from old buckets first, including subtype/channel-type moves.+ self.remove(key);+ for document in project_search_documents(key, record) {+ if let Some(catalog) = self+ .profiles+ .get_mut(&document.profile)+ .and_then(|buckets| buckets.get_mut(&document.bucket))+ {+ catalog.insert(key.clone(), document);+ }+ }+ }+ }+}diff --git a/crates/client/cache-core/src/search/ranking.rs b/crates/client/cache-core/src/search/ranking.rsnew file mode 100644index 0000000000..71b6bd435f--- /dev/null+++ b/crates/client/cache-core/src/search/ranking.rs@@ -0,0 +1,84 @@+//! Bounded ranking over borrowed catalog/optimistic entries. Only the final+//! results are cloned; the heap retains at most limit + 1 references.++use super::{+ SearchCursor, SearchDocument, SearchPage, SearchRequest, compare_recent, normalize_search_text,+ score_normalized_query,+};+use std::cmp::Ordering;+use std::collections::BinaryHeap;++struct Scored<'a> {+ document: &'a SearchDocument,+ score: f64,+}++impl Ord for Scored<'_> {+ fn cmp(&self, other: &Self) -> Ordering {+ // Exactly the original ascending sort comparator: better results are+ // smaller, so BinaryHeap keeps the worst retained result at its root.+ other+ .score+ .total_cmp(&self.score)+ .then_with(|| compare_recent(self.document, other.document))+ }+}+impl PartialOrd for Scored<'_> {+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {+ Some(self.cmp(other))+ }+}+impl PartialEq for Scored<'_> {+ fn eq(&self, other: &Self) -> bool {+ self.cmp(other).is_eq()+ }+}+impl Eq for Scored<'_> {}++pub(crate) fn rank_documents<'a>(+ request: &SearchRequest,+ documents: impl IntoIterator<Item = &'a SearchDocument>,+) -> SearchPage {+ let query = normalize_search_text(request.query.trim());+ let capacity = request.limit + 1;+ let mut best = BinaryHeap::with_capacity(capacity);+ for document in documents {+ let score = if query.is_empty() {+ Some(0.0)+ } else {+ score_normalized_query(document, &query, request.now_ms)+ };+ let Some(score) = score else {+ continue;+ };+ let candidate = Scored { document, score };+ if best.len() < capacity {+ best.push(candidate);+ } else if let Some(mut worst) = best.peek_mut()+ && candidate < *worst+ {+ *worst = candidate;+ }+ }+ let has_more = best.len() > request.limit;+ let documents: Vec<_> = best+ .into_sorted_vec()+ .into_iter()+ .take(request.limit)+ .map(|hit| hit.document.clone())+ .collect();+ let next_cursor = (query.is_empty() && has_more).then(|| {+ let last = documents.last().expect("validated nonzero search limit");+ SearchCursor {+ timestamp_ms: last.timestamp_ms,+ record_key: last.record_key.clone(),+ }+ });+ SearchPage {+ documents,+ next_cursor,+ }+}++#[cfg(test)]+mod test;diff --git a/crates/client/cache-core/src/search/ranking/test.rs b/crates/client/cache-core/src/search/ranking/test.rsnew file mode 100644index 0000000000..7cf6745d9a--- /dev/null+++ b/crates/client/cache-core/src/search/ranking/test.rs@@ -0,0 +1,95 @@+use super::*;+use crate::search::{SearchProfile, fuzzy_freshness_score};+use crate::value::EntityKey;++fn documents() -> Vec<SearchDocument> {+ (0..2_000)+ .map(|index| SearchDocument {+ profile: SearchProfile::QuickAccessV1,+ record_key: EntityKey(format!("Type{}:{index:04}", index % 3).into()),+ bucket: if index % 7 == 0 { "dm" } else { "document" }.into(),+ search_text: [+ "alpha plan",+ "aéxb plan",+ "café 🐢",+ "alpha beta",+ "",+ "计划",+ ][index % 6]+ .into(),+ timestamp_ms: if index % 11 == 0 {+ 0+ } else {+ 1_000 + (index % 23) as i64+ },+ source_hash: index.to_string(),+ })+ .collect()+}++#[test]+fn bounded_heap_matches_full_sort_for_text_browse_unicode_ties_and_dm_boost() {+ let mut corpus = documents();+ for backwards in [false, true] {+ if backwards {+ corpus.reverse();+ }+ for query in [+ "",+ " ",+ "alpha",+ "ALPHA plan",+ "ab",+ "café",+ "🐢",+ "计划",+ "unmatched",+ ] {+ for limit in [1, 7, 50, 500] {+ let request = SearchRequest {+ profile: SearchProfile::QuickAccessV1,+ buckets: vec![],+ query: query.into(),+ cursor: None,+ limit,+ now_ms: 2_000,+ };+ let mut full: Vec<_> = corpus+ .iter()+ .filter_map(|document| {+ let score = if query.trim().is_empty() {+ Some(0.0)+ } else {+ fuzzy_freshness_score(document, query, request.now_ms)+ }?;+ Some((document, score))+ })+ .collect();+ full.sort_by(|(left, left_score), (right, right_score)| {+ right_score+ .total_cmp(left_score)+ .then_with(|| compare_recent(left, right))+ });+ let has_more = full.len() > limit;+ let expected: Vec<_> = full+ .into_iter()+ .take(limit)+ .map(|(document, _)| document.clone())+ .collect();+ let actual = rank_documents(&request, &corpus);+ assert_eq!(+ actual.documents, expected,+ "query={query:?}, limit={limit}, backwards={backwards}"+ );+ let cursor = (query.trim().is_empty() && has_more).then(|| {+ let last = expected.last().unwrap();+ SearchCursor {+ timestamp_ms: last.timestamp_ms,+ record_key: last.record_key.clone(),+ }+ });+ assert_eq!(actual.next_cursor, cursor);+ }+ }+ }+}diff --git a/crates/client/cache-core/src/store.rs b/crates/client/cache-core/src/store.rsindex 32ee256714..fc609891e2 100644--- a/crates/client/cache-core/src/store.rs+++ b/crates/client/cache-core/src/store.rs@@ -82,11 +82,12 @@ pub trait Storage: MaybeSend {keys: &[EntityKey<'static>],) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;- /// Loads the compact catalog for text search. This must read only the- /// derived search table, never normalized record payloads.+ /// Loads one compact bucket for text search. Must use the profile/bucket+ /// index, never unrelated buckets or normalized record payloads.fn load_search_documents(&self,_profile: SearchProfile,+ _bucket: &str,) -> impl Future<Output = Result<Vec<SearchDocument>, Self::Error>> + MaybeSend {async { Ok(Vec::new()) }}@@ -282,6 +283,7 @@ pub struct InMemoryStorage {next_mutation_id: MutationId,record_get_count: Arc<AtomicUsize>,search_catalog_load_count: Arc<AtomicUsize>,+ search_catalog_rows_loaded: Arc<AtomicUsize>,mutation_queue_load_count: Arc<AtomicUsize>,}@@ -351,6 +353,11 @@ impl InMemoryStorage {self.search_catalog_load_count.load(Ordering::Relaxed)}+ /// Number of compact rows loaded across bucket reads (test diagnostics).+ pub fn search_catalog_rows_loaded(&self) -> usize {+ self.search_catalog_rows_loaded.load(Ordering::Relaxed)+ }+/// Number of full mutation-queue loads (test diagnostics).pub fn mutation_queue_load_count(&self) -> usize {self.mutation_queue_load_count.load(Ordering::Relaxed)@@ -408,15 +415,19 @@ impl Storage for InMemoryStorage {async fn load_search_documents(&self,profile: SearchProfile,+ bucket: &str,) -> Result<Vec<SearchDocument>, Self::Error> {self.search_catalog_load_count.fetch_add(1, Ordering::Relaxed);- Ok(self+ let documents: Vec<_> = self.search_documents- .iter()- .filter(|((candidate, _), _)| *candidate == profile)- .map(|(_, document)| document.clone())- .collect())+ .values()+ .filter(|document| document.profile == profile && document.bucket == bucket)+ .cloned()+ .collect();+ self.search_catalog_rows_loaded+ .fetch_add(documents.len(), Ordering::Relaxed);+ Ok(documents)}async fn browse_search_documents(diff --git a/crates/client/cache-core/tests/fixtures/search_move.graphql b/crates/client/cache-core/tests/fixtures/search_move.graphqlnew file mode 100644index 0000000000..41237bef82--- /dev/null+++ b/crates/client/cache-core/tests/fixtures/search_move.graphql@@ -0,0 +1,17 @@+mutation MoveFixture {+ renameEntities {+ results {+ ... on GraphqlMutationSuccess {+ effects {+ ... on SoupUpdated {+ item {+ __typename+ id+ ... on GraphqlSoupDocument { name fileType }+ }+ }+ }+ }+ }+ }+}diff --git a/crates/client/cache-core/tests/search_buckets.rs b/crates/client/cache-core/tests/search_buckets.rsnew file mode 100644index 0000000000..096eb7041e--- /dev/null+++ b/crates/client/cache-core/tests/search_buckets.rs@@ -0,0 +1,395 @@+use cache_core::engine::{BeginOptimisticWrite, Engine};+use cache_core::queue::{MutationClaimRequest, MutationClaimToken};+use cache_core::search::{SearchProfile, SearchRequest};+use cache_core::store::{InMemoryStorage, Storage};+use cache_core::value::{CacheValue, EntityKey, Record};+use serde_json::json;++fn record(typename: &str, name: &str, note: bool) -> Record {+ let mut record = Record {+ fields: [+ ("__typename".into(), CacheValue::String(typename.into())),+ ("name".into(), CacheValue::String(name.into())),+ ]+ .into(),+ };+ if note {+ record+ .fields+ .insert("fileType".into(), CacheValue::String("md".into()));+ }+ record+}+fn request(buckets: &[&str]) -> SearchRequest {+ SearchRequest {+ profile: SearchProfile::QuickAccessV1,+ buckets: buckets.iter().map(|b| (*b).into()).collect(),+ query: "needle".into(),+ limit: 20,+ cursor: None,+ now_ms: 1_000,+ }+}+async fn patch(+ engine: &mut Engine<InMemoryStorage>,+ key: &EntityKey<'static>,+ fields: &[(&str, CacheValue)],+) {+ engine+ .put_records_with_projections(+ None,+ vec![(+ key.clone(),+ Record {+ fields: fields+ .iter()+ .map(|(k, v)| ((*k).into(), v.clone()))+ .collect(),+ },+ )],+ vec![],+ )+ .await+ .unwrap();+}++#[test]+fn only_requested_buckets_load_and_empty_buckets_are_cached() {+ pollster::block_on(async {+ let mut storage = InMemoryStorage::new();+ let mut entries: Vec<_> = (0..1_000)+ .map(|i| {+ (+ EntityKey::entity("GraphqlSoupEmailThread", &[&i.to_string()]),+ record("GraphqlSoupEmailThread", "needle mail", false),+ )+ })+ .collect();+ entries.push((+ EntityKey::entity("GraphqlSoupDocument", &["note"]),+ record("GraphqlSoupDocument", "needle note", true),+ ));+ entries.push((+ EntityKey::entity("GraphqlSoupDocument", &["doc"]),+ record("GraphqlSoupDocument", "needle doc", false),+ ));+ storage.put_batch(entries).await.unwrap();+ let diagnostics = storage.clone();+ let mut engine = Engine::new(storage);+ for _ in 0..2 {+ assert_eq!(+ engine+ .search(&request(&["note", "note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ }+ assert_eq!(diagnostics.search_catalog_load_count(), 1);+ assert_eq!(diagnostics.search_catalog_rows_loaded(), 1);+ for _ in 0..2 {+ assert!(+ engine+ .search(&request(&["task", "unknown"]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ }+ assert_eq!(diagnostics.search_catalog_load_count(), 2);+ assert_eq!(+ engine+ .search(&request(&["document", "note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 2+ );+ assert_eq!(diagnostics.search_catalog_load_count(), 3);+ assert_eq!(diagnostics.search_catalog_rows_loaded(), 2);+ assert_eq!(diagnostics.record_get_count(), 0);+ assert_eq!(+ engine.search(&request(&[])).await.unwrap().documents.len(),+ 20+ );+ assert_eq!(+ diagnostics.search_catalog_load_count(),+ SearchProfile::QuickAccessV1.buckets().len()+ );+ assert_eq!(diagnostics.search_catalog_rows_loaded(), 1_002);+ });+}++#[test]+fn loaded_catalogs_handle_moves_hiding_deletion_and_external_invalidation() {+ pollster::block_on(async {+ let key = EntityKey::entity("GraphqlSoupDocument", &["doc"]);+ let mut storage = InMemoryStorage::new();+ storage+ .put_batch(vec![(+ key.clone(),+ record("GraphqlSoupDocument", "needle", false),+ )])+ .await+ .unwrap();+ let mut engine = Engine::new(storage);+ for buckets in [vec!["document"], vec!["note"]] {+ engine.search(&request(&buckets)).await.unwrap();+ }+ patch(+ &mut engine,+ &key,+ &[("fileType", CacheValue::String("md".into()))],+ )+ .await;+ assert!(+ engine+ .search(&request(&["document"]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ patch(&mut engine, &key, &[("hidden", CacheValue::Bool(true))]).await;+ assert!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ patch(&mut engine, &key, &[("hidden", CacheValue::Bool(false))]).await;+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ engine.invalidate_keys([&key]).unwrap();+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ engine.delete_keys(&[key]).await.unwrap();+ assert!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ engine.clear().await.unwrap();+ assert!(+ engine+ .search(&request(&[]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ });+}++#[test]+fn moving_to_an_unloaded_bucket_does_not_mark_its_other_rows_loaded() {+ pollster::block_on(async {+ let moved = EntityKey::entity("GraphqlSoupDocument", &["moved"]);+ let other = EntityKey::entity("GraphqlSoupDocument", &["existing-note"]);+ let mut storage = InMemoryStorage::new();+ storage+ .put_batch(vec![+ (+ moved.clone(),+ record("GraphqlSoupDocument", "needle", false),+ ),+ (other, record("GraphqlSoupDocument", "needle", true)),+ ])+ .await+ .unwrap();+ let diagnostics = storage.clone();+ let mut engine = Engine::new(storage);+ engine.search(&request(&["document"])).await.unwrap();+ patch(+ &mut engine,+ &moved,+ &[("fileType", CacheValue::String("md".into()))],+ )+ .await;+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 2+ );+ assert_eq!(diagnostics.search_catalog_load_count(), 2);+ });+}++#[test]+fn optimistic_bucket_moves_overlay_borrowed_catalogs_through_settlement() {+ pollster::block_on(async {+ for commit in [false, true] {+ let key = EntityKey::entity("GraphqlSoupDocument", &["doc"]);+ let mut storage = InMemoryStorage::new();+ storage+ .put_batch(vec![(key, record("GraphqlSoupDocument", "needle", false))])+ .await+ .unwrap();+ let mut engine = Engine::new(storage);+ engine+ .search(&request(&["document", "note"]))+ .await+ .unwrap();+ let variables = serde_json::Map::new();+ let mutation = include_str!("fixtures/search_move.graphql");+ let response = json!({"renameEntities":{"results":[{"__typename":"GraphqlMutationSuccess","effects":[{"__typename":"SoupUpdated","item":{"__typename":"GraphqlSoupDocument","id":"doc","name":"needle","fileType":"md"}}]}]}});+ let (id, _) = engine+ .begin_optimistic_write(+ None,+ BeginOptimisticWrite {+ uuid: "00000000-0000-4000-8000-000000000001",+ query: mutation,+ operation_name: None,+ variables: &variables,+ data: &response,+ link_patches: &[],+ revalidations: &[],+ created_at_ms: 1,+ },+ )+ .await+ .unwrap();+ assert!(+ engine+ .search(&request(&["document"]))+ .await+ .unwrap()+ .documents+ .is_empty()+ );+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ assert_eq!(+ engine+ .search(&request(&["document", "note"]))+ .await+ .unwrap()+ .documents+ .len(),+ 1+ );+ let claimed = engine+ .claim_next_mutation(MutationClaimRequest {+ owner: "test".into(),+ now_ms: 2,+ lease_expires_at_ms: 100,+ })+ .await+ .unwrap()+ .unwrap();+ let claim = MutationClaimToken {+ owner: "test".into(),+ generation: claimed.lease_generation,+ };+ if commit {+ engine+ .commit_optimistic_write(id, claim, mutation, None, &variables, &response)+ .await+ .unwrap();+ } else {+ engine.rollback_optimistic_write(id, claim).await.unwrap();+ }+ assert_eq!(+ engine+ .search(&request(&["document"]))+ .await+ .unwrap()+ .documents+ .len(),+ usize::from(!commit)+ );+ assert_eq!(+ engine+ .search(&request(&["note"]))+ .await+ .unwrap()+ .documents+ .len(),+ usize::from(commit)+ );+ }+ });+}++#[test]+fn bounded_browse_preserves_cross_bucket_cursor_order_and_ties() {+ pollster::block_on(async {+ let entries: Vec<_> = (0..17)+ .map(|i| {+ let key = EntityKey::entity("GraphqlSoupDocument", &[&format!("d{i:02}")]);+ let mut value = record("GraphqlSoupDocument", "needle", i % 2 == 0);+ value.fields.insert(+ "updatedAt".into(),+ CacheValue::Number(cache_core::value::CacheNumber::PosInt(i / 3)),+ );+ (key, value)+ })+ .collect();+ let mut expected: Vec<_> = entries+ .iter()+ .flat_map(|(key, record)| cache_core::search::project_search_documents(key, record))+ .collect();+ expected.sort_by(cache_core::search::compare_recent);+ let mut storage = InMemoryStorage::new();+ storage.put_batch(entries).await.unwrap();+ let diagnostics = storage.clone();+ let mut engine = Engine::new(storage);+ let mut req = request(&["document", "note"]);+ req.query.clear();+ req.limit = 3;+ let mut actual = Vec::new();+ for _ in 0..10 {+ let page = engine.search(&req).await.unwrap();+ actual.extend(page.documents);+ req.cursor = page.next_cursor;+ if req.cursor.is_none() {+ break;+ }+ }+ assert_eq!(actual, expected);+ assert_eq!(diagnostics.search_catalog_load_count(), 0);+ });+}diff --git a/crates/client/cache-turso/src/storage.rs b/crates/client/cache-turso/src/storage.rsindex d8bbb2c1c5..d1013a2f64 100644--- a/crates/client/cache-turso/src/storage.rs+++ b/crates/client/cache-turso/src/storage.rs@@ -114,7 +114,7 @@ const SEARCH_UPSERT_PREFIX: &str = "INSERT INTO search_documents (profile, __typconst SEARCH_UPSERT_ROW: &str = "(?, ?, ?, ?, ?, ?, ?)";const SEARCH_UPSERT_SUFFIX: &str = " ON CONFLICT (profile, __typename, id) DO UPDATE SET bucket = excluded.bucket, search_text = excluded.search_text, timestamp_ms = excluded.timestamp_ms, source_hash = excluded.source_hash";const SEARCH_WRITE_BATCH_SIZE: usize = 100;-const SEARCH_LOAD: &str = "SELECT __typename, id, bucket, search_text, timestamp_ms, source_hash FROM search_documents WHERE profile = ?1";+const SEARCH_LOAD: &str = "SELECT __typename, id, bucket, search_text, timestamp_ms, source_hash FROM search_documents INDEXED BY search_documents_browse_idx WHERE profile = ?1 AND bucket = ?2";const SEARCH_BROWSE: &str = "SELECT __typename, id, bucket, search_text, timestamp_ms, source_hash FROM search_documents INDEXED BY search_documents_browse_idx WHERE profile = ?1 AND bucket = ?2 ORDER BY timestamp_ms DESC, __typename ASC, id ASC LIMIT ?3";const SEARCH_BROWSE_AFTER: &str = "SELECT __typename, id, bucket, search_text, timestamp_ms, source_hash FROM search_documents INDEXED BY search_documents_browse_idx WHERE profile = ?1 AND bucket = ?2 AND (timestamp_ms < ?3 OR (timestamp_ms = ?3 AND (__typename > ?4 OR (__typename = ?4 AND id > ?5)))) ORDER BY timestamp_ms DESC, __typename ASC, id ASC LIMIT ?6";const INDEX_DOCUMENT_UPSERT: &str = "INSERT INTO index_documents (record_key, profile, partition, state) VALUES (?1, ?2, ?3, ?4) ON CONFLICT (record_key) DO UPDATE SET profile = excluded.profile, partition = excluded.partition, state = excluded.state RETURNING id";@@ -943,11 +943,17 @@ impl Storage for TursoStorage {async fn load_search_documents(&self,profile: SearchProfile,+ bucket: &str,) -> Result<Vec<SearchDocument>, Self::Error> {self.require_healthy()?;let result = {let connection = self.connection();- driver::query(&connection, SEARCH_LOAD, vec![text(profile.as_str())]).and_then(|rows| {+ driver::query(+ &connection,+ SEARCH_LOAD,+ vec![text(profile.as_str()), text(bucket)],+ )+ .and_then(|rows| {rows.into_iter().map(|row| parse_search_document(&row, profile)).collect()diff --git a/crates/client/cache-turso/src/storage/test.rs b/crates/client/cache-turso/src/storage/test.rsindex 8fa8a38ea2..62b231fe83 100644--- a/crates/client/cache-turso/src/storage/test.rs+++ b/crates/client/cache-turso/src/storage/test.rs@@ -9,6 +9,7 @@ mod filter_scope_semantics;mod page_retention;mod predicate_cost;mod projection_writes;+mod search_bucket_cost;mod search_projection;mod startup;use cache_core::normalize::RecordUpdates;@@ -220,7 +221,7 @@ fn search_projection_is_write_through_and_queries_use_projection_indexes() {);let loaded = storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap();assert_eq!(loaded.len(), 2);@@ -313,7 +314,7 @@ fn search_projection_is_write_through_and_queries_use_projection_indexes() {.unwrap();assert!(storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap().is_empty()@@ -345,7 +346,7 @@ fn search_projection_batches_large_writes_and_keeps_the_last_duplicate() {storage.put_batch(entries).await.unwrap();let loaded = storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap();assert_eq!(loaded.len(), record_count - 1);@@ -370,7 +371,7 @@ fn search_projection_batches_large_writes_and_keeps_the_last_duplicate() {.unwrap();assert!(storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap().is_empty()diff --git a/crates/client/cache-turso/src/storage/test/search_bucket_cost.rs b/crates/client/cache-turso/src/storage/test/search_bucket_cost.rsnew file mode 100644index 0000000000..e76b7b7d55--- /dev/null+++ b/crates/client/cache-turso/src/storage/test/search_bucket_cost.rs@@ -0,0 +1,80 @@+use super::*;++#[test]+fn text_catalog_loads_use_the_bucket_index_without_touching_unrelated_rows() {+ block_on(async {+ let mut storage = TursoStorage::open_in_memory("search-bucket-cost").unwrap();+ storage+ .put_batch(+ (0..3)+ .map(|n| {+ (+ key(&format!("GraphqlSoupDocument:{n}")),+ quick_access_document("needle", n),+ )+ })+ .collect(),+ )+ .await+ .unwrap();+ for unrelated_count in [0, 5_000] {+ storage+ .put_batch(+ (0..unrelated_count)+ .map(|n| {+ let mut record = quick_access_document("needle mail", n);+ record.fields.insert(+ "__typename".into(),+ cache_core::value::CacheValue::String(+ "GraphqlSoupEmailThread".into(),+ ),+ );+ (key(&format!("GraphqlSoupEmailThread:{n}")), record)+ })+ .collect(),+ )+ .await+ .unwrap();+ let mut statement = driver::prepare(&storage.connection(), SEARCH_LOAD).unwrap();+ let rows = driver::query_prepared(+ &mut statement,+ vec![+ text(SearchProfile::QuickAccessV1.as_str()),+ text("document"),+ ],+ )+ .unwrap();+ assert_eq!(rows.len(), 3);+ assert!(+ statement.metrics().vm_steps < 500,+ "{} VM steps with {unrelated_count} unrelated rows",+ statement.metrics().vm_steps+ );+ let documents = storage+ .load_search_documents(SearchProfile::QuickAccessV1, "document")+ .await+ .unwrap();+ assert_eq!(documents.len(), 3);+ assert!(+ documents+ .iter()+ .all(|document| document.bucket == "document")+ );+ }+ assert!(+ storage+ .load_search_documents(SearchProfile::QuickAccessV1, "note")+ .await+ .unwrap()+ .is_empty()+ );+ // Bucket text is bound, never interpolated into SQL.+ assert!(+ storage+ .load_search_documents(SearchProfile::QuickAccessV1, "document' OR 1=1 --")+ .await+ .unwrap()+ .is_empty()+ );+ });+}diff --git a/crates/client/cache-turso/src/storage/test/search_projection.rs b/crates/client/cache-turso/src/storage/test/search_projection.rsindex 4f95899482..05245c5168 100644--- a/crates/client/cache-turso/src/storage/test/search_projection.rs+++ b/crates/client/cache-turso/src/storage/test/search_projection.rs@@ -119,10 +119,16 @@ fn reopen_rebuilds_old_search_rows_once_without_replacing_durable_data() {storage.try_close().unwrap();let mut storage = database.open("scope").unwrap();- let rows = storage- .load_search_documents(SearchProfile::QuickAccessV1)+ let mut rows = storage+ .load_search_documents(SearchProfile::QuickAccessV1, "channel").await.unwrap();+ rows.extend(+ storage+ .load_search_documents(SearchProfile::QuickAccessV1, "document")+ .await+ .unwrap(),+ );assert_eq!(rows.len(), 602);assert_eq!(rows.iter().filter(|row| row.timestamp_ms == 1).count(), 600);assert_eq!(@@ -185,7 +191,7 @@ fn projection_rebuild_rolls_back_search_rows_and_version_on_failure() {vec![],);let before = storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "channel").await.unwrap();// Fail after the DELETE has executed; the enclosing transaction must@@ -197,7 +203,7 @@ fn projection_rebuild_rolls_back_search_rows_and_version_on_failure() {);assert_eq!(storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "channel").await.unwrap(),before@@ -212,7 +218,7 @@ fn projection_rebuild_rolls_back_search_rows_and_version_on_failure() {ensure_search_projection_version(&storage.connection()).unwrap();assert_eq!(storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "channel").await.unwrap()[0].timestamp_ms,diff --git a/crates/client/cache-turso/tests/shared_conformance.rs b/crates/client/cache-turso/tests/shared_conformance.rsindex 21e7668da4..cbef13471e 100644--- a/crates/client/cache-turso/tests/shared_conformance.rs+++ b/crates/client/cache-turso/tests/shared_conformance.rs@@ -221,7 +221,7 @@ async fn search_projection_contract<S: Storage>(storage: &mut S) {.await.unwrap();let loaded = storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap();assert!(@@ -242,7 +242,7 @@ async fn search_projection_contract<S: Storage>(storage: &mut S) {.unwrap();assert!(storage- .load_search_documents(SearchProfile::QuickAccessV1)+ .load_search_documents(SearchProfile::QuickAccessV1, "document").await.unwrap().iter()diff --git a/crates/client/cache-wasm/Cargo.toml b/crates/client/cache-wasm/Cargo.tomlindex bf6f2f0a36..191219d1eb 100644--- a/crates/client/cache-wasm/Cargo.toml+++ b/crates/client/cache-wasm/Cargo.toml@@ -2,8 +2,8 @@edition = "2024"name = "cache-wasm"publish = false-# Rebuild for scoped query-write notifications and viewer-field dependencies.-version = "0.6.33"+# Rebuild for bucket-scoped search catalogs and bounded borrowed ranking.+version = "0.6.34"[features]default = []diff --git a/crates/client/cache-wasm/src/shell/test_storage.rs b/crates/client/cache-wasm/src/shell/test_storage.rsindex e6b57cb925..63ac7a119c 100644--- a/crates/client/cache-wasm/src/shell/test_storage.rs+++ b/crates/client/cache-wasm/src/shell/test_storage.rs@@ -90,8 +90,9 @@ impl Storage for BrowserStorage {async fn load_search_documents(&self,profile: SearchProfile,+ bucket: &str,) -> Result<Vec<SearchDocument>, Self::Error> {- self.inner.load_search_documents(profile).await+ self.inner.load_search_documents(profile, bucket).await}async fn browse_search_documents(diff --git a/docs/AGENT_GUIDE/navigation.md b/docs/AGENT_GUIDE/navigation.mdindex 77d6f1160b..ac215c1f41 100644--- a/docs/AGENT_GUIDE/navigation.md+++ b/docs/AGENT_GUIDE/navigation.md@@ -496,7 +496,10 @@ another pagination attempt. Missing items may still be uncached, but should appeafter hydration without retyping, including channels outside the first cachedpage. Cmd+K'slocal search excludes unsupported email hits before limiting entity results;-email mentions keep their separate search-service path.+email mentions keep their separate search-service path. Text search loads only+its requested cache buckets, so a document-only category must not read the email+catalog. Switching categories still discovers previously unopened buckets;+renames, subtype moves, DM priority, and stable tie ordering remain unchanged.Cmd+K merges cached and locally available items before applying recency order.An empty query prefers when an item was last viewed, falling back to its update