//! WHO left a version behind, as far as a version row can honestly say — the //! port of `packages/core/src/engine/history/actor.ts`. //! //! A history row's actor is a provenance stamp, not an identity. //! The file's own triggers write `local` — they run below the op layer and have //! no request to look at — while the server names the hand it is writing on //! behalf of: `owner`/`user` for the person, `agent` for an assistant over MCP, //! `system`/`server` for Cotton's own workers, `peer` for a relayed chat write. //! Rows that came across from Postgres keep the JWT role they were written //! with (`authenticated`, `service_role`). //! //! This returns a TOKEN, never a sentence: the reader's words belong to the //! app's catalog, and a pure function that returned English would put user copy //! in a package the i18n extractor never reads. use cotton_store::protocol::js::is_js_whitespace; use serde::{Deserialize, Serialize}; /// Every role a writer in this repo actually stamps. /// /// Declared here rather than in `constants.rs` because the TypeScript declares /// it here too — `VERSION_ACTOR_ROLES` is the history engine's own list, not /// one of `constants.ts`'s unions, and moving it would make this file's /// authority over the mapping below less than obvious. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum VersionActorRole { /// The file's own triggers — an edit, with nothing further to say about it. #[serde(rename = "local")] Local, /// The person, in the app or through their own session. #[serde(rename = "authenticated")] Authenticated, #[serde(rename = "owner")] Owner, #[serde(rename = "user")] User, /// An assistant: the MCP server today, a PostgREST service role historically. #[serde(rename = "agent")] Agent, #[serde(rename = "service_role")] ServiceRole, /// Cotton's own workers — the sweeps, the pipelines, the nightly passes. #[serde(rename = "system")] System, #[serde(rename = "server")] Server, /// Another member's write, mirrored into this file by the relay. #[serde(rename = "peer")] Peer, } impl VersionActorRole { pub const fn as_str(self) -> &'static str { match self { VersionActorRole::Local => "local", VersionActorRole::Authenticated => "authenticated", VersionActorRole::Owner => "owner", VersionActorRole::User => "user", VersionActorRole::Agent => "agent", VersionActorRole::ServiceRole => "service_role", VersionActorRole::System => "system", VersionActorRole::Server => "server", VersionActorRole::Peer => "peer", } } /// In the TypeScript's declared order. pub const ALL: [VersionActorRole; 9] = [ VersionActorRole::Local, VersionActorRole::Authenticated, VersionActorRole::Owner, VersionActorRole::User, VersionActorRole::Agent, VersionActorRole::ServiceRole, VersionActorRole::System, VersionActorRole::Server, VersionActorRole::Peer, ]; pub fn parse(value: &str) -> Option { Self::ALL.into_iter().find(|role| role.as_str() == value) } /// The reader's token for this role. Exhaustive, so a role added to the /// enum without a token fails the BUILD rather than reading as `Direct`. pub const fn actor(self) -> VersionActor { match self { VersionActorRole::Local => VersionActor::Direct, VersionActorRole::Authenticated => VersionActor::You, VersionActorRole::Owner => VersionActor::You, VersionActorRole::User => VersionActor::You, VersionActorRole::Agent => VersionActor::Assistant, VersionActorRole::ServiceRole => VersionActor::Assistant, VersionActorRole::System => VersionActor::System, VersionActorRole::Server => VersionActor::System, VersionActorRole::Peer => VersionActor::Peer, } } } impl std::fmt::Display for VersionActorRole { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } /// `Direct` is the honest answer for `local` and for anything unrecognised /// alike: an edit landed, and the row does not say whose hand it was. Guessing /// "you" there would put words in somebody's mouth on the one screen whose /// whole job is saying what really happened. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum VersionActor { #[serde(rename = "you")] You, #[serde(rename = "assistant")] Assistant, #[serde(rename = "system")] System, #[serde(rename = "peer")] Peer, #[serde(rename = "direct")] Direct, } impl VersionActor { pub const fn as_str(self) -> &'static str { match self { VersionActor::You => "you", VersionActor::Assistant => "assistant", VersionActor::System => "system", VersionActor::Peer => "peer", VersionActor::Direct => "direct", } } pub const ALL: [VersionActor; 5] = [ VersionActor::You, VersionActor::Assistant, VersionActor::System, VersionActor::Peer, VersionActor::Direct, ]; } impl std::fmt::Display for VersionActor { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } /// The reader's token for a raw `actor_role` stamp. /// /// The stamp is free text — a column, not an enum, because a row migrated from /// Postgres carries whatever JWT role wrote it — so the input is trimmed and /// lower-cased before it is matched, and anything unrecognised is `Direct`. /// /// The trim is the ECMAScript one (`is_js_whitespace`), NOT Rust's /// `str::trim`. The two disagree in both directions and both directions are /// pinned by vector: a stamp wrapped in byte-order marks reads as its role /// here and as nothing under Rust's trim, and a stamp preceded by U+0085 (NEL) /// reads as `Direct` here and as its role under Rust's. pub fn version_actor(actor_role: &str) -> VersionActor { let role = actor_role.trim_matches(is_js_whitespace).to_lowercase(); VersionActorRole::parse(&role) .map(VersionActorRole::actor) .unwrap_or(VersionActor::Direct) } #[cfg(test)] #[path = "actor_tests.rs"] mod tests;