//! The kit's tools given to a database the way `import_tool` gives one: the//! one door a synthetic household, a test's fixture or an assistant uses to//! put the kit tools in a file (72nd amendment, 21 Oct 2026). No//! database is made with them.usecrate::mcp::handlers::tool_packages::install_package;usecrate::mcp::{dispatch::Ctx, protocol::ToolFailure};/// Import every kit tool (`cotton_core::kit`) into the context's database,/// each through `import_tool`'s own checks and writes, and answer their ids./// Only on demand — a synthetic household or a test's fixture; a database is/// never made with them./// The caller commits.pubfn import_kit_tools(ctx: &mutCtx<'_>) -> Result<Vec<String>, ToolFailure> {letmut packages = Vec::new();for id in cotton_core::kit::ids() {let package = cotton_core::kit::package(id) .ok_or_else(|| ToolFailure::new(format!("the kit holds no {id}")))? .map_err(|error| ToolFailure::new(error.to_string()))?; packages.push((id, package)); }// A package of views alone reads tables another kit package brings// (Artifacts over Documents' document): the tools with tables go first. packages.sort_by_key(|(_, package)| package.package.tables.is_empty());letmut imported = Vec::new();for (id, package) in packages { install_package(ctx, &package.package, crate::mcp::handlers::tool_packages::Door::Import, false)?; imported.push(id.to_string()); }Ok(imported)}/// [`import_kit_tools`] on a whole file, as its own committed write: the/// form a fixture holding only a driver takes. Writes as the owner's/// `kit` job.pubfn import_kit_into( db: &cotton_store::Driver, now: chrono::DateTime<chrono::Utc>, random: std::sync::Arc<dyncrate::random::Random>,) -> Result<Vec<String>, ToolFailure> {let fail = |error: cotton_store::StoreError| ToolFailure::new(error.to_string());let file = cotton_store::file_identity::read_file_identity(db) .map_err(fail)? .ok_or_else(|| ToolFailure::new("the database has no identity yet"))?;let owner = db .get("SELECT owner_id FROM _meta", &[]) .map_err(fail)? .and_then(|row| row.text("owner_id").map(str::to_string)) .unwrap_or_default();let services = crate::mcp::dispatch::ToolServices::none();let audit = |_: &str, _: &str| {};let actor = cotton_store::mint::Actor::DeviceJob(cotton_store::mint::JobName::named("kit").map_err(fail)?);letmut ctx = Ctx::for_worker(db, &file, &owner, actor, "kit", now, random, &services, &audit);let imported = import_kit_tools(&mut ctx)?; ctx.commit()?;Ok(imported)}/// Add the tools `ids` names to a whole file, each by `add_tool` — a kit/// tool or a business package, as a person adds one by hand: a database is/// made with the shell only (owner, 21 Oct 2026), so a test's business or/// household that needs its tools asks for them. Writes as the owner's `kit`/// job; each add commits.pubfn add_tools_into( db: &cotton_store::Driver, now: chrono::DateTime<chrono::Utc>, random: std::sync::Arc<dyncrate::random::Random>, ids: &[&str],) -> Result<(), ToolFailure> { add_tools(db, now, random, None, ids)}/// Found a database with its app's genesis set (the manifest's `tools`,/// architect 22 Oct 2026): every package in it installed from the compiled/// kit through `add_tool` — the path `import_tool` takes — so each is/// ordinary data, removable, editable and upgradable; written under `held`,/// as the shell is. A built-in has nothing to write: its engine is on by/// being compiled in. The phone's Start and the Mac's birth both call this.pubfn install_genesis_tools( db: &cotton_store::Driver, now: chrono::DateTime<chrono::Utc>, random: std::sync::Arc<dyncrate::random::Random>, held: cotton_store::mint::Held, tools: &cotton_core::genesis::GenesisTools,) -> Result<(), ToolFailure> {// A database being born is whole or not at all (make_copy removes a file// it could not finish), so its genesis set lands in one transaction: each// tool is still its own add, act and commit (a savepoint inside it), and// the file takes the 21 tools' pages once rather than once a tool.// And as one batch of rows (`cotton_store::fold::as_one_batch`): the// file's shape is read once and again only after a write that moves it,// not once a receipt of every tool.letmut failed = None;let landed = cotton_store::fold::as_one_batch(db, || { db.transaction(|db| { add_tools(db, now, random, Some(held), &tools.packages()).map_err(|failure| {let refusal = cotton_store::StoreError::refused(failure.0.clone()); failed = Some(failure); refusal }) }) });match (landed, failed) { (Ok(()), _) => Ok(()), (Err(_), Some(failure)) => Err(failure), (Err(error), None) => Err(ToolFailure::new(error.to_string())), }}/// GENESIS MAKES A KIT'S TABLES (25 Nov 2026). A phone's own copy was made with only the tables its tools carry, so a/// table the app kit lists that no tool draws never reached it (Sites' `site`, Post's `email_account`: deliberately/// no tool, since tools are listed to assistants). Each of `ids` the copy lacks is laid from the kit's file as a/// package's table is, with its storage, in one transaction; nothing is written for one it holds. No tool, view or/// grant changes. At birth (`start_local`) and at every open of a local copy (`ClientController::attach`), so a copy/// made before this gains them with nothing lost. Answers the ids laid.pubfn lay_kit_tables( db: &cotton_store::Driver, now: chrono::DateTime<chrono::Utc>, random: std::sync::Arc<dyncrate::random::Random>, held: Option<cotton_store::mint::Held>, ids: &[String],) -> Result<Vec<String>, ToolFailure> {let fail = |error: cotton_store::StoreError| ToolFailure::new(error.to_string());let file = cotton_store::file_identity::read_file_identity(db) .map_err(fail)? .ok_or_else(|| ToolFailure::new("the database has no identity yet"))?;let owner = db .get("SELECT owner_id FROM _meta", &[]) .map_err(fail)? .and_then(|row| row.text("owner_id").map(str::to_string)) .unwrap_or_default();let services = crate::mcp::dispatch::ToolServices::none();let audit = |_: &str, _: &str| {};letmut failed = None;letmut laid = Vec::new();let landed = db.transaction(|db| {let actor = cotton_store::mint::Actor::DeviceJob(cotton_store::mint::JobName::named("kit")?);letmut ctx = Ctx::for_worker(db, &file, &owner, actor, "kit", now, random.clone(), &services, &audit);ifletSome(held) = held { ctx = ctx.held_as(held); }matchcrate::mcp::handlers::tool_packages::lay_kit_tables(&mut ctx, ids) {Ok(ids) => { laid = ids;Ok(()) }Err(failure) => {let refusal = cotton_store::StoreError::refused(failure.0.clone()); failed = Some(failure);Err(refusal) } } });match (landed, failed) { (Ok(()), _) => {} (Err(_), Some(failure)) => returnErr(failure), (Err(error), None) => returnErr(ToolFailure::new(error.to_string())), }if !laid.is_empty() {crate::derive::ensure_current(db).map_err(fail)?; }Ok(laid)}fn add_tools( db: &cotton_store::Driver, now: chrono::DateTime<chrono::Utc>, random: std::sync::Arc<dyncrate::random::Random>, held: Option<cotton_store::mint::Held>, ids: &[&str],) -> Result<(), ToolFailure> {let fail = |error: cotton_store::StoreError| ToolFailure::new(error.to_string());let file = cotton_store::file_identity::read_file_identity(db) .map_err(fail)? .ok_or_else(|| ToolFailure::new("the database has no identity yet"))?;let owner = db .get("SELECT owner_id FROM _meta", &[]) .map_err(fail)? .and_then(|row| row.text("owner_id").map(str::to_string)) .unwrap_or_default();let services = crate::mcp::dispatch::ToolServices::none();let audit = |_: &str, _: &str| {};// Each tool redefines tables, and a fold that redefines works the// computed index out again over every table the file holds: 21 tools// were 21 whole reworks (~1 s each in debug). The adds leave it to one// rebuild after the last (`Driver::with_index_beside`); a handle that// already leaves it to the background keeps doing so.// Each kit tool's package is decoded once a process (`kit::decoded`); a// birth is usually the first ask, and the decodes are independent, so// they run side by side here rather than one inside each add.if ids.len() > 1 {let _span = cotton_store::spans::span("kit::decode side by side"); std::thread::scope(|scope| {for id in ids { scope.spawn(move || {let _ = cotton_core::kit::decoded(id); }); } }); }let beside = db.indexes_beside(); db.with_index_beside(|| {for id in ids {let made = cotton_store::spans::span("kit::ctx");let actor = cotton_store::mint::Actor::DeviceJob(cotton_store::mint::JobName::named("kit").map_err(fail)?);letmut ctx = Ctx::for_worker(db, &file, &owner, actor, "kit", now, random.clone(), &services, &audit);ifletSome(held) = held { ctx = ctx.held_as(held); } drop(made);let _span = cotton_store::spans::span("kit::add_tool");let args = serde_json::json!({"tool_id": id});crate::mcp::handlers::tool_packages::add_tool(&mut ctx, args.as_object().expect("an object"))?; }Ok::<(), ToolFailure>(()) })?;if !beside {let _span = cotton_store::spans::span("kit::ensure_current");crate::derive::ensure_current(db).map_err(fail)?; }Ok(())}