//! Search a made table's own table (`tables::made_table`) over only the //! columns its definition declares as text (and its title), never engine //! credentials — through the core's defined lane //! (`search_everything::search_defined_table`). use std::collections::{BTreeMap, BTreeSet}; use cotton_core::definitions::TableDefinition; use cotton_core::rules::search_everything::search_defined_table_leaving; use rusqlite::types::Value as SqlValue; use serde_json::{json, Map, Value}; use super::search::SearchArgs; use crate::mcp::dispatch::Ctx; use crate::mcp::handlers::tables::{hidden, made_table}; use crate::mcp::protocol::ToolFailure; pub(super) fn append( ctx: &Ctx<'_>, args: &SearchArgs, all_tables: bool, records: &mut Vec, totals: &mut Map, skipped: &mut Vec, ) -> Result<(), ToolFailure> { if args.mode == "meaning" { return Ok(()); } let fail = |error: cotton_store::error::StoreError| ToolFailure::new(error.to_string()); let db = ctx.db(); // ONLY THE TABLES SEARCHED ARE RESOLVED, each on its own: one stored // definition this build cannot read is skipped and said in the answer, // never the refusal of the whole search. A retired built-in's row // (`order`, left with Stores) is no table at all, so nothing is said. A made // table's records are rows of its own table (`tables::made_table`); a // table this file does not hold yet has none. let shipped = cotton_core::definitions::builtin_table_definitions(); let mut definitions: BTreeMap = BTreeMap::new(); for row in crate::definitions::definition_rows(ctx, None)? { let id = row.id.clone(); if !all_tables && !args.kinds.iter().any(|k| *k == id) { continue; } // A shipped built-in is searched by its own engine's search. let built = shipped.iter().find(|definition| definition.id == id); if built.is_some() || crate::definitions::retired_builtin(&row, built) { continue; } match crate::definitions::resolve_table(&id, built, Some(&row)) { Ok(definition) if !definition.has_engine() && definition.engine.is_none() => { let present = db .get( "SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = ?", &[SqlValue::Text(made_table(&id))], ) .map_err(fail)? .is_some(); if present { definitions.insert(id, definition); } } Ok(_) => {} Err(reason) => skipped.push(json!({"table": id, "reason": reason})), } } if definitions.is_empty() { return Ok(()); } let tools = crate::definitions::tool_definitions(ctx)?; let referenced: BTreeSet = tools .definitions .into_iter() .flat_map(|tool| tool.tables) .collect(); // Read once, and only when one of his films' tables is searched. let hidden_ids: Vec = if definitions.keys().any(|id| hidden::FAMILY.contains(&id.as_str())) { hidden::hiding(ctx).map_err(ToolFailure::new)?.all().into_iter().collect() } else { Vec::new() }; for (id, definition) in &definitions { let id = id.as_str(); // The statement is the core's, built from the definition alone // (`search_everything::defined_table_query`): one way a defined table // is searched, on the phone and here. // One table whose stored shape its definition no longer matches (a column it names that the table lacks, // seen on a phone keeping the whole object: Cotton Data, 25 Nov 2026) is skipped and said so, as a table that // does not resolve is; it never fails the search of every other table. // HIDE FROM SCREEN (CORE 5, M1): his films' tables are searched without the hidden rows, in the SQL, so the hits, // the per-table limit and the total are all made without them (as Find reads them, `phone::reads::find`). let left_out: &[String] = if hidden::FAMILY.contains(&id) { &hidden_ids } else { &[] }; let read = match search_defined_table_leaving(&super::search::FileSearchDriver(db), definition, ctx.workspace_id, &args.query, args.limit, 0, left_out) { Ok(read) => read, Err(error) => { skipped.push(json!({"table": id, "reason": error.to_string()})); continue; } }; if !read.hits.is_empty() { let existing = totals.get(id).and_then(Value::as_i64).unwrap_or_default(); totals.insert(id.into(), Value::from(existing + read.total)); } for found in read.hits { if records.iter().any(|r| r["table"] == id && r["id"] == found.id.as_str()) { continue; } let mut hit = json!({"table":id,"table_name":definition.name,"orphaned":!referenced.contains(id),"kind":id,"id":found.id, "title":found.title,"snippet":found.snippet,"detail":found.snippet}); if found.archived { hit["archived"] = Value::Bool(true); } records.push(hit); } } Ok(()) }