//! `log_session` reads the turns of the session it logs, not every turn in //! the file. //! //! A session's hook calls it once a turn. Every one of its reads of //! `cotton_prompts` names the workspace and the session, a file holds one //! workspace, and until schema 99 the only index a two-term read could use //! led with `workspace_id` alone — every turn the file holds. //! //! Every value is invented ([nothing-real-ever-goes-in-cotton-s-public-tree]). mod support; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool_in_prepared_file, mint_token}; const NOW: &str = "2026-10-05T09:00:00.000Z"; fn log(h: &Harness, secret: &str, external_id: &str, turns: &[i64]) -> Value { let prompts: Vec = turns .iter() .map(|seq| json!({ "seq": seq, "ts": NOW, "text": format!("an invented turn {seq}") })) .collect(); call_tool_in_prepared_file( h, secret, "log_session", json!({ "tool": "claude-code", "externalId": external_id, "machine": "a-synthetic-hostname", "lastActiveAt": NOW, "prompts": prompts }), ).json() } /// A file whose one long-lived session already holds `others` turns. fn file_with(others: i64) -> (Harness, String) { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); log(&h, &secret, "the-long-session", &[]); let session = h .db .get("SELECT id FROM cotton_sessions", &[]) .expect("reads") .expect("the session") .text("id") .expect("its id") .to_string(); // Unrelated historical turns are read-only inputs to this guard. Do not // mint 20,000 fixture receipts: the measured log_session never touches // them, and reconciling them dominated the ten-minute timeout on 4786. h.db.run( "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i + 1 < ?1) \ INSERT INTO cotton_prompts (id, workspace_id, session_id, seq, ts, text, created_at, updated_at) \ SELECT printf('turn-%08d', i), ?2, ?3, i, ?4, 'an invented turn', ?4, ?4 FROM n", &[SqlValue::Integer(others), SqlValue::Text(h.workspace_id.clone()), SqlValue::Text(session), SqlValue::Text(NOW.into())], ).expect("the unrelated turns"); (h, secret) } /// Steps to log two turns of a session the file has not met — the call's own /// statements. The harness's bookkeeping after a call (its reconcile, which /// reads each table for rows with no `_writes` receipt) is not the server's /// and is left out. fn steps_to_log_a_turn(h: &Harness, secret: &str) -> u64 { h.db.watch_statements(); log(h, secret, "a-new-session", &[0, 1]); assert_eq!( h.db.get("SELECT count(*) AS n FROM cotton_prompts p JOIN cotton_sessions s ON s.id = p.session_id WHERE s.external_id = 'a-new-session'", &[]) .expect("reads") .and_then(|row| row.int("n")), Some(2) ); h.db.steps_by_statement() .into_iter() .filter(|(_, sql)| !sql.contains("NOT EXISTS (SELECT 1 FROM _writes")) .map(|(steps, _)| steps) .sum() } /// RULE: Logging a session's turn reads that session's turns through an index that leads with the session, so it costs the same in a file of 20,000 turns as in one of 200 — never a read of every turn in the file. #[test] fn logging_a_turn_reads_its_own_sessions_turns_and_never_every_turn() { let (small_file, small_secret) = file_with(200); let small = steps_to_log_a_turn(&small_file, &small_secret); let (large_file, large_secret) = file_with(20_000); let large = steps_to_log_a_turn(&large_file, &large_secret); assert!(large <= small + small / 2, "logging a turn read {large} steps among 20,000 turns and {small} among 200"); // And the measure sees the read it guards against. let (unindexed, secret) = file_with(20_000); unindexed.db.exec("DROP INDEX idx_cotton_prompts_session").expect("the index is there to drop"); let scanned = steps_to_log_a_turn(&unindexed, &secret); assert!(scanned > large + 20_000, "without the index logging a turn read {scanned} steps, with it {large}"); }