//! A SYNTHETIC HOUSEHOLD — a large, wholly invented life written into a fresh //! file through the store's own write paths, for the development server. //! //! The owner's household is frozen (DEV MODE, 2026-10-03), so every build, //! test and screen runs against a second identity with a disposable database. //! That database has to look like a life — tens of thousands of rows across //! every kind, spread over years, linked the way real rows are linked — and //! it has to be made again in a minute whenever the schema moves. //! //! THREE RULES SHAPE WHAT IS HERE: //! //! 1. NOTHING IS WRITTEN WITH RAW SQL. Every row goes through the MCP tool //! handlers where one exists (`create_todo`, `create_person`, //! `record_transaction`, `ingest_emails`, …) and through the `Writer`'s //! stamped patches where none does (photos, repos, runs). A handler //! validates and stamps as it would for a phone or an assistant, so the //! rows are the shape the app reads today — and when a column moves, the //! small run in this module's test fails, which is the point. //! 2. DETERMINISTIC BY SEED. The same `(size, seed)` gives the same file: //! the clock is fixed, the random is a seeded xorshift, and nothing reads //! the wall clock or `OsRng`. Two developers can compare screens. //! 3. NOTHING REAL. Names, addresses, organisations and domains come from //! `names.rs`, every email is under `example.com`, and no row carries the //! owner's data — the generator never reads a file it did not make. //! //! Sizes are TOTAL ROWS, roughly: `small` 2 000 (a test), `large` 100 000 //! (the development household), `huge` 1 000 000 (a performance run). mod film; mod names; mod paper; mod picture; mod rng; mod sound; mod statement_pages; mod write; pub use rng::SeededRandom; use chrono::{DateTime, TimeZone, Utc}; use std::collections::BTreeMap; /// The fixed "now" every synthetic row is dated against — a Thursday. The /// household's history runs back from here. pub fn fixed_now() -> DateTime { Utc.with_ymd_and_hms(2026, 10, 1, 9, 0, 0).single().expect("a fixed instant") } /// The "now" a development household is seeded against, read off the wall /// clock: nine o'clock UTC today, so what is due today is due today — but /// never later than the wall clock itself. Every receipt the seed writes is /// stamped at or before this instant, and a household stamped AHEAD of the /// wall clock refused every edit a joined phone made to a seeded row ("folded /// as stale") until the real day caught up: a device's own stamps never run /// more than the store's future bound past its clock, so they lost to the /// seed's on every field. pub fn seed_now(wall: DateTime) -> DateTime { let nine = wall.date_naive().and_hms_opt(9, 0, 0).expect("nine o'clock").and_utc(); nine.min(wall) } /// How much of each kind. `named` is the only constructor the verbs use; the /// fields are public so a test can ask for one kind alone. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Plan { pub people: usize, pub locations: usize, pub projects: usize, pub memories: usize, pub procedures: usize, pub todos: usize, pub notes: usize, pub journal_days: usize, pub events: usize, pub message_threads: usize, pub messages: usize, /// How many of `messages` go to the first chat, so one thread is long /// enough to measure a page opened at its end (0: the usual spread). pub longest_thread: usize, pub emails: usize, pub accounts: usize, pub transactions: usize, pub statement_months: usize, pub photos: usize, /// How many years of history the rows spread over. pub years: u32, /// A SHARED database's life: no mail, messages, calls, money, moods, /// journal or sessions, because those tables are the personal database's /// alone (`cotton_store::link_rule::MINE_BOUND_TABLES`) and the fold /// refuses them anywhere else. The rest — people, places, todos, notes, /// events, photos, goals, documents, secrets — is seeded as it is for the /// personal one. pub shared: bool, } impl Plan { /// A plan scaled to about `total` rows, in the proportions a life has: /// messages and transactions outnumber everything, people and places are /// few, projects and repos are a handful at any size. pub fn scaled(total: usize) -> Plan { let share = |per_mille: usize| (total * per_mille / 1000).max(1); let small = total < 10_000; Plan { people: share(15).min(5_000), locations: share(5).min(2_000), projects: if small { 3 } else { names::PROJECTS.len() }, memories: share(5).clamp(10, 3_000), procedures: if small { 4 } else { names::PROCEDURES.len() }, todos: share(100), notes: share(80), journal_days: share(40).min(365 * 3), events: share(100), message_threads: share(10).clamp(5, 2_000), messages: share(250), longest_thread: 0, emails: share(120), accounts: if small { 4 } else { 8 }, transactions: share(150), statement_months: if small { 3 } else { 36 }, photos: share(80), years: if small { 1 } else { 3 }, shared: false, } } /// The same plan for a second, shared database beside the personal one: /// the personal-only tables are left out. pub fn for_shared(mut self) -> Plan { self.shared = true; self.message_threads = 0; self.messages = 0; self.emails = 0; self.journal_days = 0; self.accounts = 0; self.transactions = 0; self.statement_months = 0; self } /// About one of everything: the thinnest database that still gives every /// tool a row, so a screen can be walked with a list of one (the freeze /// proof walks Home on it beside an empty and a large database). pub fn one() -> Plan { Plan { people: 1, locations: 1, projects: 1, memories: 1, procedures: 1, todos: 1, notes: 1, journal_days: 1, events: 1, message_threads: 1, messages: 1, longest_thread: 0, emails: 1, accounts: 1, transactions: 1, statement_months: 1, photos: 1, years: 1, shared: false, } } /// The database every list is timed on (scripts/perf/lists.sh): 20,000 /// messages across 300 chats, one of them 2,000 long, 5,000 photos and /// 3,000 people. pub fn lists() -> Plan { Plan { people: 3_000, message_threads: 300, messages: 20_000, longest_thread: 2_000, photos: 5_000, ..Plan::scaled(100_000) } } pub fn named(size: &str) -> Option { match size { "one" => Some(Plan::one()), "small" => Some(Plan::scaled(2_000)), "large" => Some(Plan::scaled(100_000)), "huge" => Some(Plan::scaled(1_000_000)), "lists" => Some(Plan::lists()), _ => None, } } } /// What was written, by table, for the verb to print and the test to assert. #[derive(Debug, Default, Clone)] pub struct Counts { by_table: BTreeMap, /// Independently generated paper figures for a caller's fixture renderer. /// These are never reconstructed from the submitted or stored reading. pub statement_pages: BTreeMap, } impl Counts { pub fn add(&mut self, table: &str, n: usize) { *self.by_table.entry(table.to_string()).or_default() += n; } pub fn set(&mut self, table: &str, n: usize) { self.by_table.insert(table.to_string(), n); } pub fn of(&self, table: &str) -> usize { self.by_table.get(table).copied().unwrap_or_default() } pub fn total(&self) -> usize { self.by_table.values().sum() } pub fn by_table(&self) -> &BTreeMap { &self.by_table } } /// Found nothing, fill everything: the file at `db` is a provisioned /// workspace (its `cotton_workspaces` row and default calendar already /// seeded), and this writes the household into it as the job `synthetic`. pub fn seed_household( db: &cotton_store::Driver, owner_id: &str, plan: &Plan, seed: u64, ) -> cotton_store::Result { seed_household_at(db, owner_id, plan, seed, fixed_now()) } /// [`seed_household`] dated against `now` instead of [`fixed_now`]. The rows /// that only mean something beside today — a todo due this afternoon, an /// invitation for Thursday, last night's mood — are written around `now`, so /// a development household seeded with the real day is not months late the /// week after. The same `(plan, seed, now)` still gives the same file. pub fn seed_household_at( db: &cotton_store::Driver, owner_id: &str, plan: &Plan, seed: u64, now: DateTime, ) -> cotton_store::Result { seed_household_with(db, owner_id, plan, seed, now, None) } /// Where a household's generated pictures go: the object key, and the bytes. /// The generator knows keys, not folders — the caller that serves the /// household knows where its object store is. pub type MediaSink<'a> = &'a dyn Fn(&str, &[u8]) -> std::io::Result<()>; /// [`seed_household_at`] with somewhere to put the pictures. Without a sink /// the rows are the same and name objects that are not there — which is how /// most of a library looks on any one device anyway. pub fn seed_household_with( db: &cotton_store::Driver, owner_id: &str, plan: &Plan, seed: u64, now: DateTime, media: Option>, ) -> cotton_store::Result { seed_household_sealed(db, owner_id, plan, seed, now, media, None) } /// [`seed_household_with`] with the workspace's vault: the secrets it seeds /// carry a sealed value, so a seeded login, card or note opens with Show and /// Copy like one the person made. Without a rail the rows are bare — a name /// and a hint, nothing to open — which is what a phone build can do. pub fn seed_household_sealed( db: &cotton_store::Driver, owner_id: &str, plan: &Plan, seed: u64, now: DateTime, media: Option>, vault: Option>, ) -> cotton_store::Result { let file = cotton_store::file_identity::read_file_identity(db)? .ok_or_else(|| cotton_store::StoreError::Refused("the file has never been seeded".to_string()))?; // A seed is one bulk import. Folds keep their pending rows beside a // stale index rather than repeatedly deriving a growing household. db.with_index_beside(|| { cotton_store::derived::mark_stale(db)?; let counts = write::Household::new(db, &file, owner_id, plan, seed, now, media) .sealing(vault) .write()?; // Rebuild on this writer after the final fold, so an older background // snapshot cannot overtake the completed seed (swap_in refuses it). crate::derive::rebuild(db)?; Ok(counts) }) } #[cfg(test)] mod tests;