//! What `log_session` leaves for a peer to fold — the handlers driven directly over //! a fake `ToolServices` rather than through the server's `/v1/mcp` endpoint. //! //! `list_agent_accounts` and its tests went with the AI accounts (OPEN TABLES //! step 1, cf0af626dc, 24 Nov 2026). //! //! This file was the session bus's (`mcp_session_bus.rs`). The bus went on //! 15 Nov 2026 (owner): `send_to_session`, `poll_session_inbox`, //! `get_session_message`, `update_session_message`, the poll's `listened_at` //! heartbeat and `list_sessions { live }` are gone, and their tests with them. mod support; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, mint_token}; /// The instant every stamp in this file is measured against — the harness's /// own fixed start, so nothing here reads a wall clock. const NOW: &str = "2026-09-08T10:00:00.000Z"; fn at(offset_ms: i64) -> chrono::DateTime { chrono::DateTime::parse_from_rfc3339(NOW) .expect("a literal instant") .with_timezone(&chrono::Utc) + chrono::Duration::milliseconds(offset_ms) } fn iso(offset_ms: i64) -> String { cotton_tools::clock::iso(at(offset_ms)) } /// A harness with the recording push rail installed — the default. fn bus_harness() -> (Harness, String) { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); (h, secret) } /// A session that has logged a turn — the row `log_session` upserts. fn log_in(h: &Harness, secret: &str, external_id: &str, machine: &str, project: Option<&str>) { let mut args = json!({ "tool": "claude-code", "externalId": external_id, "machine": machine, "lastActiveAt": iso(0), }); if let Some(project) = project { args["project"] = Value::from(project); } call_tool(h, secret, "log_session", args); } fn all(h: &Harness, sql: &str, params: &[SqlValue]) -> Vec> { h.with(|db| db.all(sql, params)) .expect("the read runs") .into_iter() .map(|row| { row.columns() .iter() .map(|(name, value)| { ( name.clone(), match value { SqlValue::Null => Value::Null, SqlValue::Integer(int) => Value::from(*int), SqlValue::Real(real) => Value::from(*real), SqlValue::Text(text) => Value::from(text.clone()), SqlValue::Blob(_) => Value::from(""), }, ) }) .collect() }) .collect() } fn column(h: &Harness, sql: &str, params: &[SqlValue], name: &str) -> Value { all(h, sql, params) .into_iter() .next() .and_then(|row| { row.into_iter() .find(|(column, _)| column == name) .map(|(_, value)| value) }) .unwrap_or(Value::Null) } /// How many ops a phone syncing from zero would be handed for one table. /// /// There is no log to count. The tables answer for an author, so this is one /// op per distinct STAMP a row's columns sit at — which is exactly the number /// the presence collapse used to keep down by deleting log entries, arrived at /// by never writing them. fn ops_for(h: &Harness, table: &str) -> i64 { i64::try_from(every_op(h).iter().filter(|op| op.table == table).count()).unwrap_or(-1) } /// Every op this file would hand a peer starting from nothing, page by page: /// the kit and the shell a harness holds fill more than one page. fn every_op(h: &Harness) -> Vec { let mut seen = std::collections::BTreeMap::new(); let mut ops = Vec::new(); loop { let page = cotton_store::exchange::answer(&h.db, &seen, 5_000).expect("the file answers a peer"); ops.extend(page.ops); seen = page.cursors; if !page.has_more { return ops; } } } // ── what a peer is handed ─────────────────────────────────────────────────── /// An op naming a column the next one does not is never superseded, whatever /// else it carries. `log_session` writes `transcript_ref` the one time a /// transcript is filed; a later turn's stamp must not stand in for it, or a /// replay from zero would rebuild the session without one. #[test] fn an_op_naming_a_column_the_next_one_does_not_survives() { let (h, secret) = bus_harness(); log_in(&h, &secret, "sess-a", "mac-b", None); let before = ops_for(&h, "cotton_sessions"); call_tool( &h, &secret, "log_session", json!({ "tool": "claude-code", "externalId": "sess-a", "machine": "mac-b", "lastActiveAt": iso(30_000), "transcriptRef": "transcripts/sess-a.jsonl", }), ); call_tool( &h, &secret, "log_session", json!({ "tool": "claude-code", "externalId": "sess-a", "machine": "mac-b", "lastActiveAt": iso(60_000), }), ); assert_eq!(ops_for(&h, "cotton_sessions"), before + 2); assert_eq!( column( &h, "SELECT transcript_ref FROM cotton_sessions WHERE external_id = 'sess-a'", &[], "transcript_ref", ), Value::from("transcripts/sess-a.jsonl") ); } /// A table nobody declared is never collapsed, whatever its patches look like. #[test] fn an_undeclared_table_still_lands_op_by_op() { let (h, secret) = bus_harness(); let before = ops_for(&h, "cotton_todos"); for title in ["buy cardamom", "buy cardamom and rice"] { call_tool(&h, &secret, "create_todo", json!({ "title": title })); } assert_eq!(ops_for(&h, "cotton_todos"), before + 2); } // ── list_agent_accounts ───────────────────────────────────────────────────── // // Quotas supplies usage; device reports identify signed-in logins. // Seed both stores to exercise the identity join without reading credentials.