//! The rails whose IMPLEMENTATION is somebody else's. //! //! A tool handler is synchronous, holds a driver, and reaches nothing outside //! the person's file except through a rail (design S7). Most rails are closures //! on [`crate::mcp::dispatch::ToolServices`]; the ones here are TRAITS, //! because what is behind them is machine-local state. //! //! They live in this crate and are FILLED IN by whoever is running. That is the //! P3 boundary in one sentence: a trait cannot drag axum, a rusqlite handle on //! `registry.db` or a tokio worker into a phone, and an absent rail is already //! a state every handler knows how to answer — `None`, and the tool refuses by //! name rather than half-working. //! //! THE TYPES IT EXCHANGES ARE DECLARED HERE, not beside the code that reads //! them, so the implementing crate `pub use`s these shapes rather than declaring //! a second copy that could drift a field. use std::fmt; // ── the vault ─────────────────────────────────────────────────────────────── /// What one sealed payload is, and which version of the master key sealed it. /// /// The two travel TOGETHER because they are written together: `value_enc` can /// only be opened by the version of the vault master key named beside it, and a /// row carrying one without the other is a secret nothing can read. #[derive(Debug, Clone, PartialEq, Eq)] pub struct SealedSecret { pub value_enc: String, pub vmk_version: u32, } /// Seal and open a secret's payload, under the workspace's vault master key /// (spec §2.2, Cotton note "The vault — the honest sentence"). /// /// A TRAIT rather than a function, for the reason every other rail here is one: /// the master key lives in the SERVER's memory, unwrapped at boot from this /// Mac's own vault key, and this crate cannot link the crate that holds it. /// What travels is the capability, not the key — and the key is never a /// parameter of anything here, so no handler can hold, log or answer with one. /// /// ABSENT IS A STATE. A build with no vault — a phone, a `cotton-server` /// started with no vault key, a Mac whose key cannot open this workspace — /// has `None` here, and every tool that would need it refuses BY NAME /// (`vault_unavailable`) rather than writing a plaintext credential into a /// column called `value_enc`. There is NO fallback to `COTTON_KEYS_SECRET`: /// the old scheme opens rows the re-seal sweep has not reached yet and seals /// nothing new, ever again. /// /// UNLIKE ITS PREDECESSOR, THIS RAIL HAS AN `open`. That is not a widening of /// what an assistant can see: there is still no `get_secret` and there will not /// be one, and the readers are named — `api_key_for`, the document-password /// pipeline, the placement applier and the import writer, all in Rust, none /// reachable from a tool call. The reveal belongs to the person's own device, /// against its own replica, with its own key. pub trait VaultRail: Send + Sync + fmt::Debug { /// Seal one payload for one row. `None` when the vault refuses, which is /// never a reason to store the plaintext instead. fn seal(&self, secret_id: &str, payload: &[u8]) -> Option; /// Open one sealed payload. `None` for a wrong version, a tampered body, a /// value sealed under the old scheme, or a vault that cannot open this /// workspace — the caller does the same thing in every case. fn open(&self, secret_id: &str, vmk_version: u32, sealed: &str) -> Option>; /// The four characters a list shows in place of a value. /// /// Derived from the sealed payload rather than taken from the `hint` /// column, so a caller re-deriving one (the re-seal sweep, the import /// writer) cannot disagree with what the vault would answer. The default /// body is the whole rule and no implementation overrides it. fn hint(&self, secret_id: &str, vmk_version: u32, sealed: &str) -> Option { let opened = self.open(secret_id, vmk_version, sealed)?; let payload: cotton_core::vault::SecretPayload = serde_json::from_slice(&opened).ok()?; let hint = payload.hint(); cotton_core::vault::seal::wipe(opened); Some(hint) } } // ── the developer tools ───────────────────────────────────────────────────── /// The developer switch, as a capability. /// /// **`Some` IS THE SWITCH.** `ToolServices::dev_tools` is `Some` only on a /// server whose `COTTON_DEV_TOOLS` is on, and `None` everywhere else: on a /// phone, on a Mac with the line cleared, and in every build that never heard /// of the switch. So one field answers both "may these tools be called" and /// "can this process act", and there is no second flag to disagree with it. A /// developer tool's first line reads it and refuses by name /// ([`crate::mcp::protocol::dev_tools_off`]) — which is what closes the phone's /// own door too, since the phone dispatches through `handler_for` and never /// through `call_tool`. pub trait DevToolsRail: Send + Sync + fmt::Debug {} // ── the file this process is reading ──────────────────────────────────────── /// How a process is holding the file it is reading. /// /// The Mac has both rails: it SERVES a file, and its client half keeps a /// replica of any workspace it has joined elsewhere. The phone only ever has a /// replica. Declared here so the FFI's own reads (`cotton_ffi::shell::file`) /// and the `activity` table's one record say it with one word, not two. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum FileHeld { /// A copy on this device, synced from the server that holds the file. Replica, /// The file itself, open under this process's server. Served, } impl FileHeld { /// The word the boundary answers. pub fn as_str(self) -> &'static str { match self { FileHeld::Replica => "replica", FileHeld::Served => "served", } } /// The sentence a person reads for it — WHICH file, never where it is on /// a disk (the path once printed here was another machine's home folder). pub fn sentence(self) -> &'static str { match self { FileHeld::Replica => "A copy on this device", FileHeld::Served => "The file this Mac is serving", } } } /// What the process running a call knows about the file it is reading and /// the file never says: which device reads it, and how it holds it. What the /// `activity` table's one record reads (`ToolServices::this_file`); `None` /// wherever no device is reading — an assistant's connection, a worker. #[derive(Clone, Debug, PartialEq, Eq)] pub struct ThisFile { pub held: FileHeld, /// This device's own id; `None` on a serving Mac with no identity yet. pub device_id: Option, /// NO MACHINE SERVES THIS COPY YET: a device that started its own /// database and has met no Mac (`ConnectionMode::Local`), or a database /// made here that no server has taken. Such a device is the file's /// writer of record, as the serving Mac is /// ([`crate::mcp::handlers::recurrence::mints_occurrences`]). pub unserved: bool, /// The kit the app on this device keeps to (`cotton_core::kit::AppKit`): /// a write the person did not ask for (the zone claim) touches no table /// it does not carry, since no copy of a kit app ever takes such a row. /// `None` is the whole object (Cotton), or no app at all. pub kit: Option>, } impl ThisFile { /// Whether the app on this device carries the table `id`: its kit's, or /// any where it keeps to the whole object. pub fn carries(&self, id: &str) -> bool { self.kit.as_ref().is_none_or(|kit| kit.keeps_table(id)) } }