Cotton code
91 lines · 3.6 KB · RustRaw
//! **A LANE'S TOKEN READS EVERYTHING AND WRITES ONLY ITS SESSION LOG**//! (architect, 15 Oct 2026; the Work tool and its lane rows left Cotton, 27//! Oct 2026). A lane is a Claude or Codex session//! the lane launcher starts under the lane's seatbelt profile; the//! profile denies the owner's own Cotton login, so the launcher has the app//! mint the lane a token of its own. That token carries `read` and the word//! `lane:<name>`: it is offered and admitted every read tool — `brief`,//! `get_project`, `get_record`, `recall` and the rest — and refused on every//! write door but one: `log_session` (the session's hand-off to the next one).//!//! The lane word NARROWS whatever else a row carries: a row holding `write`//! beside `lane:<name>` is still a lane token. `cotton_mcp_tokens` is a synced//! table, so the words are device-written data, and the direction that fails//! safe is the one where the lane word only ever takes power away.use crate::mcp::protocol::{McpScope, ToolDef, ToolFailure};/// How a lane token's scope word starts; the lane's name follows.pub const LANE_SCOPE_PREFIX: &str = "lane:";/// The only write tool a lane token reaches.pub const LANE_WRITE_DOORS: [&str; 1] = ["log_session"];/// The longest lane name a token carries.const MAX_LANE: usize = 60;/// A lane's name as the launcher allows it: letters, digits, `_` and `-`.pub fn is_lane_name(name: &str) -> bool {    !name.is_empty()        && name.len() <= MAX_LANE        && name.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')}/// The scope word for `lane`, or `None` for a name a lane may not have.pub fn lane_scope(lane: &str) -> Option<String> {    is_lane_name(lane).then(|| format!("{LANE_SCOPE_PREFIX}{lane}"))}/// Whether `word` is a well-formed lane scope word.pub fn is_lane_scope(word: &str) -> bool {    word.strip_prefix(LANE_SCOPE_PREFIX).is_some_and(is_lane_name)}/// The lane these scopes belong to — the first well-formed lane word.pub fn lane_of(scopes: &[String]) -> Option<&str> {    scopes        .iter()        .find_map(|word| word.strip_prefix(LANE_SCOPE_PREFIX).filter(|name| is_lane_name(name)))}/// Is `tool` the lane's write door, listed to a lane token beside the read/// tools so the lane sees the one write it may make?pub fn door_listed(scopes: &[String], tool: &ToolDef) -> bool {    lane_of(scopes).is_some() && tool.scope == McpScope::Write && LANE_WRITE_DOORS.contains(&tool.name)}/// May a caller holding `scopes` get a writer for `tool`? A write token for any/// tool that writes; a lane token only for its door.pub fn may_write(scopes: &[String], tool: &ToolDef) -> bool {    if tool.scope == McpScope::Read {        return false;    }    if lane_of(scopes).is_some() {        return door_listed(scopes, tool);    }    scopes.iter().any(|word| word == McpScope::Write.as_str())}fn refused(lane: &str, why: &str) -> ToolFailure {    ToolFailure::new(format!(        "this is lane {lane}'s token: it reads, and writes only its session log — {why}"    ))}/// Whether a lane token's call to a WRITE tool may run: `Ok(true)` for its/// door, `Err` for every other write a lane token tries, `Ok(false)` when the/// caller is not a lane or the tool reads (the scope check decides those).pub fn door(scopes: &[String], tool: &ToolDef) -> Result<bool, ToolFailure> {    let Some(lane) = lane_of(scopes) else { return Ok(false) };    if tool.scope == McpScope::Read {        return Ok(false);    }    if !door_listed(scopes, tool) {        return Err(refused(lane, &format!("`{}` is not log_session", tool.name)));    }    Ok(true)}#[cfg(test)]#[path = "lane_scope_tests.rs"]mod tests;