//! Todos lost labels (architect ruling, 5 Oct 2026), and the groups the old //! label move made are taken back. //! //! ## What went wrong //! //! From 5 Oct the serving Mac's start gathered every distinct todo label into //! a todo group of its name (`move_labels_to_groups`, d546bdb00). But the //! labels were never the person's words: the summaries worker's night pass //! had filled every open todo's label from its title (or the model's //! shortening of it), so the move made one group per todo, named after the //! todo, and a repeating todo's occurrences (which shared the words) became //! one group of all of them — 273 groups in the owner's personal database //! (11 Oct 2026, "Call Dad" and "Medicines" with 16 members each), none of //! which anyone asked for. //! //! ## What a start does now — [`take_back_label_groups`] //! //! - It makes no group. A group is made only by `create_group`, a person's //! verb (the phone's group sheet, or a conversation acting for them); //! `tests/no_group_is_made_unasked.rs` holds that no other code calls it. //! - It bins every group the old move made that nobody has touched since: a //! live `cotton_groups` row of the todo table whose last writer is still //! this seed job ([`SEED_LABEL`], its `record_byline`) and that has no //! `create` row in `cotton_changes`. //! `create_group` always writes that row, so a person's group is never //! taken, even one the old move added todos to; and a group a person has //! since renamed or changed carries their byline and stays. It runs at //! every start, because a Mac on a build from between d546bdb00 and this //! change re-makes the groups if it finds labels; this build takes them //! back at its next start. Binned, not erased: the bin's restore still //! reaches one, and emptying the bin erases them. //! - It clears every todo label still stored, with `updated_at` written back //! as it was. Nothing reads the column; it is cleared because the words //! are the night pass's, never the person's, and a label left there is //! exactly what an older build turns into a group again. //! //! ## Once, and by whom //! //! Only the server runs it, in the start step //! (`cotton_server::definitions::start_database`): a replica's own writes //! would race the server's, and the server's reach every replica by sync. On //! a database with nothing to take back it reads and writes nothing. //! //! ## The column stays //! //! `cotton_todos.label` is not dropped: no schema step, so an older phone's //! write that still names it folds as before instead of being refused. use rusqlite::types::Value as SqlValue; use serde_json::{Map, Value}; use crate::definitions::SEED_LABEL; use crate::mcp::dispatch::Ctx; use crate::mcp::handlers::common::{now_iso, update_row}; use crate::mcp::handlers::tables::{groups, CHANGES}; use crate::mcp::protocol::ToolFailure; /// What one start took back. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct LabelGroups { /// Groups the old label move made, binned. pub groups_binned: usize, /// Todos whose label was cleared — every todo that had one, `""` too. pub labels_cleared: usize, } impl LabelGroups { /// Whether the run wrote anything. pub fn wrote(&self) -> bool { self.groups_binned > 0 || self.labels_cleared > 0 } } fn sql(error: impl std::fmt::Display) -> ToolFailure { ToolFailure::new(error.to_string()) } /// Bin the todo groups the old label move made and clear every todo label /// still stored; the caller commits. Makes no group. pub fn take_back_label_groups(ctx: &mut Ctx<'_>) -> Result { let workspace = SqlValue::Text(ctx.workspace_id.to_string()); let mut report = LabelGroups::default(); let made = ctx .read .driver() .all( &format!( "SELECT g.id FROM {GROUPS} g \ WHERE g.workspace_id = ? AND g.deleted_at IS NULL \ AND g.record_byline = ? AND g.table_id = 'todo' \ AND NOT EXISTS (SELECT 1 FROM {CHANGES} c \ WHERE c.table_id IN (?, ?) AND c.record_id = g.id AND c.verb = 'create') \ ORDER BY g.created_at, g.id", GROUPS = groups::GROUPS ), &[ workspace.clone(), SqlValue::Text(SEED_LABEL.into()), SqlValue::Text(groups::GROUPS.into()), SqlValue::Text(groups::TABLE.into()), ], ) .map_err(sql)?; let now = now_iso(ctx); for row in &made { let Some(id) = row.text("id") else { continue }; let mut patch = Map::new(); patch.insert("deleted_at".into(), Value::from(now.clone())); patch.insert("updated_at".into(), Value::from(now.clone())); patch.insert("record_byline".into(), Value::from(SEED_LABEL)); update_row(ctx, groups::GROUPS, id, patch)?; report.groups_binned += 1; } let labelled = ctx .read .driver() .all( "SELECT id, updated_at FROM cotton_todos WHERE workspace_id = ? AND label IS NOT NULL ORDER BY id", &[workspace], ) .map_err(sql)?; for row in &labelled { let Some(id) = row.text("id") else { continue }; // Cleared where it stands; `updated_at` is written back as it was, // because a list dated by the last touch (the dropped todos) must not // move every labelled todo to today. let mut patch = Map::new(); patch.insert("label".into(), Value::Null); patch.insert("updated_at".into(), row.text("updated_at").map_or(Value::Null, Value::from)); update_row(ctx, "cotton_todos", id, patch)?; report.labels_cleared += 1; } Ok(report) }