//! What is archived can be found again (architect's ruling S7, 5 Oct 2026)://! a list leaves its archived records out and `list_records { archived://! true }` answers just them, counted for the list's last link "Archived ·//! n"; each comes back with `update_record` clearing its archive column, or//! the Undo of its archive; and search finds an archived record, marked,//! after the live ones — never one in the bin.//!//! Every value is invented ([nothing-real-ever-goes-in-cotton-s-public-tree]).mod support;use rusqlite::types::ValueasSqlValue;use serde_json::{json, Value};use support::harness::Harness;use support::mcp::{call_tool_as_owner, mint_token};const PHONE: &str = "device:phone";const NOW: &str = "2026-10-05T09:00:00.000Z";const STAMP: &str = "2026-10-05T07:40:00.000Z";/// A table a person made, with an archive of its own: a bool column.fn kite() -> Value { json!({"id": "kite", "name": "Kite", "plural": "Kites", "picture": null,"builtin": false, "engine": null,"columns": [ { "key": "name", "type": "text", "required": true }, { "key": "put_away", "type": "bool" } ],"title": "name", "date": null, "project": null, "order": null,"slip": { "title": "name", "subtitle": null, "trailing": null, "photo": null, "toggle": null },"tile": { "face": "slip-list" },"page": [ { "heading": null, "columns": ["name"] } ],"archived": "put_away","instructions": "A kite in the shed." })}fn harness() -> Harness {letmut h = Harness::new(); mint_token(&mut h, &["read", "write"], "Claude");let kite = kite(); h.run("INSERT INTO cotton_table_definitions (id, workspace_id, definition, builtin, created_at, updated_at) \ VALUES (?, ?, ?, 0, ?, ?)", &[SqlValue::Text("kite".into()),SqlValue::Text(h.workspace_id.clone()),SqlValue::Text(kite.to_string()),SqlValue::Text(NOW.into()),SqlValue::Text(NOW.into()), ], ); h}fn owner(h: &Harness, tool: &str, args: Value) -> Value {let answer = call_tool_as_owner(h, PHONE, tool, args); assert!(!answer.is_error, "{tool} refused: {}", answer.text); answer.json()}fn refusal(h: &Harness, tool: &str, args: Value) -> String {let answer = call_tool_as_owner(h, PHONE, tool, args); assert!(answer.is_error, "{tool} was expected to refuse: {}", answer.text); answer.text}fn create(h: &Harness, table: &str, data: Value) -> String {let made = owner(h, "create_record", json!({ "table": table, "data": data })); made["record"]["id"].as_str().expect("the record's id").to_string()}fn ids(list: &Value) -> Vec<String> { list["records"] .as_array() .expect("records") .iter() .map(|record| record["id"].as_str().unwrap().to_string()) .collect()}/// The table's list and its archive: which ids each holds, and the archive's total.fn shelves(h: &Harness, table: &str) -> (Vec<String>, Vec<String>, Value) {let live = owner(h, "list_records", json!({ "table": table }));let archived = owner(h, "list_records", json!({ "table": table, "archived": true })); (ids(&live), ids(&archived), archived["total"].clone())}/// Archived by `put_away`, the record is out of the list and the whole of the/// archive, still read by its id; `bring_back` returns it to the list.fn round_trip(h: &Harness, table: &str, id: &str, put_away: Value, bring_back: Value) {let (live, archived, total) = shelves(h, table); assert!(live.contains(&id.to_string()) && archived.is_empty() && total == 0, "{table} before: {live:?} {archived:?} {total}"); owner(h, "update_record", json!({ "table": table, "id": id, "patch": put_away }));let (live, archived, total) = shelves(h, table); assert!(!live.contains(&id.to_string()), "{table}: an archived record stayed in the list"); assert_eq!(archived, vec![id.to_string()], "{table}: the archive"); assert_eq!(total, 1, "{table}: the archive's count");let count = owner(h, "list_records", json!({ "table": table, "archived": true, "limit": 0 })); assert_eq!((count["records"].clone(), count["total"].clone(), count["next_page"].clone()), (json!([]), json!(1), Value::Null), "{table}: {count}");let read = owner(h, "get_record_by_table", json!({ "table": table, "id": id })); assert_eq!(read["id"], id, "{table}: an archived record must read: {read}"); owner(h, "update_record", json!({ "table": table, "id": id, "patch": bring_back }));let (live, archived, total) = shelves(h, table); assert!(live.contains(&id.to_string()), "{table}: Bring back left it out of the list"); assert!(archived.is_empty() && total == 0, "{table}: Bring back left it archived");}/// RULE: An archived note, person or made-table record leaves the list and is the whole of `list_records {archived: true}` (total 1, limit 0 the count alone), still reads by id, and clearing its archive column brings it back.#[test]fn an_archived_record_is_listed_in_its_archive_and_brought_back() {let h = harness();let note = create(&h, "note", json!({ "title": "Spare key under the blue pot" })); round_trip(&h, "note", ¬e, json!({ "archived_at": STAMP }), json!({ "archived_at": null }));let person = create(&h, "person", json!({ "full_name": "Sela Porchwalker" })); round_trip(&h, "person", &person, json!({ "archived_at": STAMP }), json!({ "archived_at": null }));let kite = create(&h, "kite", json!({ "name": "Red delta", "put_away": false })); round_trip(&h, "kite", &kite, json!({ "put_away": true }), json!({ "put_away": false }));}/// RULE: A table whose records are never archived, an event's bin included, refuses archived: true by name.#[test]fn a_table_without_an_archive_refuses() {let h = harness(); assert_eq!(refusal(&h, "list_records", json!({ "table": "todo", "archived": true })), "Todos are never archived");// An event's bin is not an archive.let event = refusal(&h, "list_records", json!({ "table": "event", "archived": true })); assert!(event.ends_with("are never archived"), "{event}");}/// RULE: Search finds an archived note marked archived: true, after the live one, and never a note in the bin.#[test]fn search_finds_an_archived_note_marked_and_never_a_binned_one() {let h = harness();let archived = create(&h, "note", json!({ "title": "Lantern wick spares" })); owner(&h, "update_record", json!({ "table": "note", "id": archived, "patch": { "archived_at": STAMP } }));let live = create(&h, "note", json!({ "title": "Lantern glass" }));let binned = create(&h, "note", json!({ "title": "Lantern oil" })); owner(&h, "delete_record", json!({ "table": "note", "id": binned }));let found = owner(&h, "search_records", json!({ "query": "lantern", "kinds": ["note"] }));let hits: Vec<(String, Value)> = found["records"] .as_array() .expect("records") .iter() .map(|hit| (hit["id"].as_str().unwrap().to_string(), hit.get("archived").cloned().unwrap_or(Value::Null))) .collect(); assert_eq!(hits, vec![(live, Value::Null), (archived, json!(true))], "{found}");}/// RULE: Every built-in a tool in this database draws answers `list_records {archived: true}` with its archive, or refuses that its records are never archived — none with an archive it cannot list. A built-in no tool here draws (its tool left the kit, its table stayed: Quotas, Moods, Meetings, 3 Nov 2026) answers the typed no-tool refusal, never a list.#[test]fn every_builtin_lists_its_archive_or_has_none() {let h = harness();let drawn: Vec<String> = h .with(|db| cotton_tools::definitions::table_definitions_from_driver(db, &h.workspace_id).expect("lists")) .definitions .into_iter() .map(|definition| definition.id) .collect();for definition in cotton_core::definitions::builtin_table_definitions() {let answer = call_tool_as_owner(&h, PHONE, "list_records", json!({ "table": definition.id, "archived": true, "today": "2026-10-05" }));if !drawn.contains(&definition.id) { assert!(answer.is_error, "{} is drawn by no tool here, yet listed: {}", definition.id, answer.text); assert_eq!(answer.text, cotton_core::definitions::no_tool_draws_this_table(&[]), "{}", definition.id); } elseif answer.is_error { assert!(answer.text.ends_with("are never archived"), "{}: {}", definition.id, answer.text); } else { assert_eq!(answer.json()["total"], 0, "{}", definition.id); } }}/// RULE: Archiving a note and bringing it back — by clearing archived_at, by archive_note/unarchive_note, or by the Undo of the archive — is not an edit: updated_at never moves and the note keeps its place in the list.#[test]fn archiving_and_bringing_back_a_note_leaves_its_edited_time() {let h = harness();let older = create(&h, "note", json!({ "title": "Bicycle pump valve" }));let newer = create(&h, "note", json!({ "title": "Seed packets drawer" }));for (id, at) in [(&older, "2026-09-01T08:00:00.000Z"), (&newer, "2026-09-02T08:00:00.000Z")] { h.run("UPDATE cotton_notes SET updated_at = ? WHERE id = ?", &[SqlValue::Text(at.into()), SqlValue::Text(id.clone())]); }let edited = || h.one("SELECT updated_at FROM cotton_notes WHERE id = ?", &[SqlValue::Text(older.clone())]).unwrap()["updated_at"].clone();let order = || shelves(&h, "note").0;let before = order(); assert_eq!(before, vec![newer.clone(), older.clone()], "the newer note leads");// By the archive column, both ways. owner(&h, "update_record", json!({ "table": "note", "id": older, "patch": { "archived_at": STAMP } })); assert_eq!(edited(), "2026-09-01T08:00:00.000Z", "archiving is not an edit"); owner(&h, "update_record", json!({ "table": "note", "id": older, "patch": { "archived_at": null } })); assert_eq!(edited(), "2026-09-01T08:00:00.000Z", "update_record's Bring back is not an edit"); assert_eq!(order(), before, "update_record's Bring back moved the note");// By the notes engine's own verbs. owner(&h, "archive_note", json!({ "id": older })); assert_eq!(edited(), "2026-09-01T08:00:00.000Z", "archive_note is not an edit"); owner(&h, "unarchive_note", json!({ "id": older })); assert_eq!(edited(), "2026-09-01T08:00:00.000Z", "unarchive_note is not an edit"); assert_eq!(order(), before, "unarchive_note moved the note");// By the Undo of the archive.let archived = owner(&h, "update_record", json!({ "table": "note", "id": older, "patch": { "archived_at": STAMP } })); assert_eq!(archived["change"]["undoable"], true, "{archived}"); owner(&h, "undo", json!({ "change_id": archived["change"]["id"] })); assert_eq!(edited(), "2026-09-01T08:00:00.000Z", "the Undo of an archive is not an edit"); assert_eq!(order(), before, "the Undo of an archive moved the note");}/// The `(id, archived)` pairs a search answered for one kind, in its order.fn hits_of(found: &Value, kind: &str) -> Vec<(String, Value)> { found["records"] .as_array() .expect("records") .iter() .filter(|hit| hit["kind"] == kind) .map(|hit| (hit["id"].as_str().unwrap().to_string(), hit.get("archived").cloned().unwrap_or(Value::Null))) .collect()}/// One conversation of one email, in the inbox or out of it, or in the bin.fn mail(h: &Harness, key: &str, subject: &str, in_inbox: bool, binned: bool) -> (String, String) {let thread = support::harness::synthetic_uuid(&format!("{key} thread"));let email = support::harness::synthetic_uuid(&format!("{key} email"));let text = |value: &str| SqlValue::Text(value.to_string());let bin = if binned { text(STAMP) } else { SqlValue::Null }; h.run("INSERT INTO cotton_email_threads (id, workspace_id, external_key, subject, first_at, last_at, \ email_count, trashed_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?, ?)", &[text(&thread), text(&h.workspace_id), text(key), text(subject), text(NOW), text(NOW), bin.clone(), text(NOW), text(NOW)], ); h.run("INSERT INTO cotton_emails (id, workspace_id, thread_id, external_id, subject, from_email, from_name, \ recipients, sent_at, body, snippet, attachments, labels, unread, starred, in_inbox, trashed_at, created_at) \ VALUES (?, ?, ?, ?, ?, 'studio@example.com', 'Studio', '[]', ?, 'See you there.', 'See you there.', '[]', '[]', 0, 0, ?, ?, ?)", &[text(&email), text(&h.workspace_id), text(&thread), text(key), text(subject), text(NOW), SqlValue::Integer(in_inbox.into()), bin, text(NOW)], ); (thread, email)}/// RULE: Search finds mail archived out of the inbox marked archived: true, and never mail in the bin. Mail is ranked newest first, not inbox first (mailsearch brief, 12 Oct 2026: twelve years of mail are mostly archived), and a conversation is ONE hit — the email that matched, never a second row for its thread.#[test]fn search_marks_mail_archived_out_of_the_inbox_and_never_finds_the_binned() {let h = harness();let (_, live_email) = mail(&h, "kiln-firing", "Kiln firing schedule", true, false);let (_, gone_email) = mail(&h, "kiln-glaze", "Kiln glaze order", false, false); mail(&h, "kiln-repair", "Kiln repair quote", false, true);let found = owner(&h, "search_records", json!({ "query": "kiln", "kinds": ["email", "email_thread"] }));letmut emails = hits_of(&found, "email"); emails.sort_by(|a, b| a.0.cmp(&b.0));letmut want = vec![(live_email, Value::Null), (gone_email, json!(true))]; want.sort_by(|a, b| a.0.cmp(&b.0)); assert_eq!(emails, want, "{found}"); assert_eq!(hits_of(&found, "email_thread"), vec![], "{found}");}