Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! Transaction-aware canonical explicit team sharing.
//!
//! Acquire [`acquire_guard`] before accompanying metadata/ownership/topology writes.
//! All public operations acquire it again (transaction advisory locks are reentrant),
//! read fresh authoritative facts, and never commit the caller's transaction. On any
//! error the caller must roll back the whole transaction, including metadata changes.
//! Ordinary reads may use a short transaction and [`load_facts`] before domain policy.
pub use entity_access_db_utils::team_share::acquire_guard;
use entity_access_db_utils::team_share::{
delete_direct, direct_level, replace_project_contributions, upsert_direct,
};
use macro_user_id::cowlike::CowLike;
use macro_uuid::Uuid;
use model_entity::{Entity, EntityType};
use model_owner::{
Owner,
team::{OwnerTeamFacts, owner_team},
};
use models_permissions::share_permission::{
access_level::AccessLevel,
team_share::{
AuthorizedTeamShareCommand, TeamShareCreation, TeamShareFacts, TeamShareGrant,
TeamShareLevel, TeamShareMaintenance,
},
};
use rootcause::prelude::*;
use sqlx::{PgConnection, Postgres, Transaction};
#[cfg(test)]
mod test;
/// Conditional-write failures remain distinguishable from database/reporting failures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum TeamShareError {
/// No authoritative entity exists (permission associations alone are insufficient).
#[error("team-share entity not found")]
NotFound,
/// Domain-authorized ownership, membership, or canonical state changed.
#[error("team-share facts changed")]
ChangedFacts,
/// A normal mutation would overwrite an unexplained direct grant.
#[error("untracked direct team grant conflicts with team sharing")]
UntrackedGrant,
/// Reconciliation requires a matching reviewed direct grant and owner team.
#[error("team-share adoption candidate no longer matches")]
InvalidAdoption,
/// The entity kind or identifier is unsupported.
#[error("invalid team-share entity")]
InvalidEntity,
/// Stored permission state is incomplete/invalid, or its revision is exhausted.
#[error("invalid canonical team-share state")]
InvalidState,
/// Database or persisted-identity parsing failure; the report retains the cause.
#[error("team-share persistence failed")]
Infrastructure,
}
/// Typed report returned by canonical persistence operations.
pub type TeamShareResult<T> = Result<T, Report<TeamShareError>>;
/// Historical canonical consent for later conditional lifecycle compensation.
/// `revision` is the pre-cleanup revision; a successful clear writes revision + 1.
/// Restoration must check that revision, NULL state, owner eligibility and current
/// topology under the guard; this snapshot alone is not permission to restore.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TeamShareCleanupSnapshot {
/// Explicit sharing root, not a descendant receiving inherited access.
pub root: Entity<'static>,
/// Actual owner at snapshot time, for ownership-change eligibility checks.
pub owner: Owner,
/// Historical managed team, retained after membership removal.
pub managed_team_id: Uuid,
/// Exact historical level.
pub level: TeamShareLevel,
/// Revision before cleanup.
pub revision: i64,
}
/// Capture only canonical consent, never infer it from direct or inherited grants.
pub fn cleanup_snapshot(facts: &TeamShareFacts) -> Option<TeamShareCleanupSnapshot> {
facts.current.map(|grant| TeamShareCleanupSnapshot {
root: facts.entity.clone(),
owner: facts.owner.clone(),
managed_team_id: grant.team_id,
level: grant.level,
revision: facts.revision,
})
}
struct State {
facts: TeamShareFacts,
permission_id: Option<String>,
}
fn entity_uuid(entity: &Entity<'_>) -> TeamShareResult<Uuid> {
if !matches!(
entity.entity_type,
EntityType::Document
| EntityType::Project
| EntityType::Chat
| EntityType::EmailThread
| EntityType::Call
| EntityType::AgentSession
| EntityType::Initiative
| EntityType::Database
| EntityType::ExternalChat
) {
return Err(report!(TeamShareError::InvalidEntity));
}
Uuid::parse_str(&entity.entity_id).context(TeamShareError::InvalidEntity)
}
/// Load actual owner, current membership and canonical state under the shared guard.
/// Threads, sessions and databases without permissions read as NULL/revision zero
/// without creating rows.
/// Tasks and snippets use Document; calls resolve their durable entity.
pub async fn load_facts(
transaction: &mut Transaction<'_, Postgres>,
entity: &Entity<'_>,
) -> TeamShareResult<TeamShareFacts> {
acquire_guard(transaction)
.await
.context(TeamShareError::Infrastructure)?;
Ok(load_state(transaction.as_mut(), entity).await?.facts)
}
async fn load_state(connection: &mut PgConnection, entity: &Entity<'_>) -> TeamShareResult<State> {
let uuid = entity_uuid(entity)?;
let row = sqlx::query!(
r#"WITH entity AS (
SELECT d.owner, dp."sharePermissionId" AS permission_id
FROM "Document" d LEFT JOIN "DocumentPermission" dp ON dp."documentId" = d.id
WHERE $2 = 'document' AND d.id = $1
UNION ALL
SELECT p."userId", pp."sharePermissionId"
FROM "Project" p LEFT JOIN "ProjectPermission" pp ON pp."projectId" = p.id
WHERE $2 = 'project' AND p.id = $1
UNION ALL
SELECT c."userId", cp."sharePermissionId"
FROM "Chat" c LEFT JOIN "ChatPermission" cp ON cp."chatId" = c.id
WHERE $2 = 'chat' AND c.id = $1
UNION ALL
SELECT l.macro_id, tp."sharePermissionId"
FROM email_threads t JOIN email_links l ON l.id = t.link_id
LEFT JOIN "EmailThreadPermission" tp ON tp."threadId" = t.id::text
WHERE $2 = 'email_thread' AND t.id = $3
UNION ALL
SELECT c.user_id, c.share_permission_id FROM call_entities c
WHERE $2 = 'call' AND c.id = $3
UNION ALL
SELECT i.owner_user_id, i.share_permission_id FROM initiative i
WHERE $2 = 'initiative' AND i.id = $3
UNION ALL
SELECT g.user_id, g.share_permission_id FROM external_chat g
WHERE $2 = 'external_chat' AND g.id = $3
UNION ALL
SELECT s.owner_id, s.share_permission_id FROM agent_session s
WHERE $2 = 'agent_session' AND s.id = $3
UNION ALL
SELECT d.user_id, d.share_permission_id FROM database_entities d
WHERE $2 = 'database' AND d.database_id = $3
)
SELECT e.owner AS "owner!", e.permission_id,
sp.id AS "stored_permission_id?",
sp.team_share_access_level AS "level: AccessLevel",
sp.team_share_team_id AS team_id, sp.team_share_revision AS "revision?",
tu.team_id AS "user_team?", b.team_id AS "bot_team?", btu.team_id AS "bot_user_team?"
FROM entity e LEFT JOIN "SharePermission" sp ON sp.id = e.permission_id
LEFT JOIN team_user tu ON tu.user_id = e.owner
LEFT JOIN bots b ON b.id = CASE
WHEN e.owner ~ '^bot\|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
THEN substring(e.owner FROM 5)::uuid END
LEFT JOIN team_user btu ON btu.user_id = b.owner_user_id"#,
entity.entity_id.as_ref(),
entity.entity_type.as_ref(),
uuid,
)
.fetch_optional(connection)
.await
.context(TeamShareError::Infrastructure)?
.ok_or_else(|| report!(TeamShareError::NotFound))?;
if row.stored_permission_id.is_none()
&& (row.permission_id.is_some()
|| !matches!(
entity.entity_type,
EntityType::EmailThread | EntityType::AgentSession | EntityType::Database
))
{
return Err(report!(TeamShareError::InvalidState));
}
let current = match (row.level, row.team_id) {
(Some(level), Some(team_id)) => Some(TeamShareGrant {
team_id,
level: TeamShareLevel::try_from(level).context(TeamShareError::InvalidState)?,
}),
(None, None) => None,
_ => return Err(report!(TeamShareError::InvalidState)),
};
let revision = row.revision.unwrap_or(0);
if revision < 0 {
return Err(report!(TeamShareError::InvalidState));
}
let owner = Owner::from_principal_str(&row.owner).context(TeamShareError::Infrastructure)?;
let owner_team_id = owner_team(
&owner,
OwnerTeamFacts {
user_team: row.user_team,
bot_team: row.bot_team,
bot_user_team: row.bot_user_team,
},
);
Ok(State {
facts: TeamShareFacts {
entity: entity.clone().into_owned(),
owner,
owner_team_id,
current,
revision,
},
permission_id: row.permission_id,
})
}
async fn recheck(
transaction: &mut Transaction<'_, Postgres>,
expected: &TeamShareFacts,
) -> TeamShareResult<State> {
acquire_guard(transaction)
.await
.context(TeamShareError::Infrastructure)?;
let state = load_state(transaction.as_mut(), &expected.entity).await?;
if state.facts != *expected {
return Err(report!(TeamShareError::ChangedFacts));
}
Ok(state)
}
/// Apply an actual-owner-authorized supplied operation. Omission has no command and
/// must not call this function. Even same-value sets and repeated clears advance revision.
pub async fn apply(
transaction: &mut Transaction<'_, Postgres>,
command: &AuthorizedTeamShareCommand,
) -> TeamShareResult<()> {
let state = recheck(transaction, command.expected()).await?;
reject_untracked(transaction.as_mut(), &state.facts, command.target()).await?;
write_state(
transaction,
state,
command.target(),
command.next_revision(),
)
.await
}
async fn reject_untracked(
connection: &mut PgConnection,
facts: &TeamShareFacts,
target: Option<TeamShareGrant>,
) -> TeamShareResult<()> {
let Some(target) = target else {
return Ok(());
};
if facts.current.map(|g| g.team_id) == Some(target.team_id) {
return Ok(());
}
let existing = direct_level(
connection,
&entity_uuid(&facts.entity)?,
facts.entity.entity_type,
target.team_id,
)
.await
.context(TeamShareError::Infrastructure)?;
if existing.is_some() {
return Err(report!(TeamShareError::UntrackedGrant));
}
Ok(())
}
fn next_revision(facts: &TeamShareFacts) -> TeamShareResult<i64> {
facts
.revision
.checked_add(1)
.ok_or_else(|| report!(TeamShareError::InvalidState))
}
/// Clear by trusted lifecycle intent, without requiring an acting user or current
/// membership. Compare freshly loaded facts and retain the historical team attribution.
pub async fn maintain(
transaction: &mut Transaction<'_, Postgres>,
intent: &TeamShareMaintenance,
) -> TeamShareResult<()> {
match intent {
TeamShareMaintenance::Clear { expected } => {
let state = recheck(transaction, expected).await?;
let revision = next_revision(&state.facts)?;
write_state(transaction, state, None, revision).await
}
}
}
/// Conditionally restore lifecycle-cleared consent after membership compensation.
/// This is not a user-authorized edit: the historical owner/team and the exact cleared
/// revision must still match. Deleted roots and intervening explicit operations are
/// ineligible. Project contributions are rebuilt from the current tree by `write_state`.
/// Returns false for an ineligible snapshot, without changing any state.
pub async fn restore_cleared(
transaction: &mut Transaction<'_, Postgres>,
previous: &TeamShareFacts,
cleared_revision: i64,
) -> TeamShareResult<bool> {
acquire_guard(transaction)
.await
.context(TeamShareError::Infrastructure)?;
let Some(grant) = previous.current else {
return Ok(false);
};
if previous.revision.checked_add(1) != Some(cleared_revision) {
return Ok(false);
}
let state = match load_state(transaction.as_mut(), &previous.entity).await {
Ok(state) => state,
Err(error) if *error.current_context() == TeamShareError::NotFound => return Ok(false),
Err(error) => return Err(error),
};
if state.facts.owner != previous.owner
|| state.facts.owner_team_id != Some(grant.team_id)
|| state.facts.current.is_some()
|| state.facts.revision != cleared_revision
{
return Ok(false);
}
let deleted = sqlx::query_scalar!(
r#"SELECT EXISTS (
SELECT 1 FROM "Document" WHERE $2 = 'document' AND id = $1 AND "deletedAt" IS NOT NULL
UNION ALL
SELECT 1 FROM "Project" WHERE $2 = 'project' AND id = $1 AND "deletedAt" IS NOT NULL
UNION ALL
SELECT 1 FROM "Chat" WHERE $2 = 'chat' AND id = $1 AND "deletedAt" IS NOT NULL
UNION ALL
SELECT 1 FROM database_entities
WHERE $2 = 'database' AND database_id::text = $1 AND trashed_at IS NOT NULL
) AS "deleted!""#,
previous.entity.entity_id.as_ref(),
previous.entity.entity_type.as_ref(),
)
.fetch_one(transaction.as_mut())
.await
.context(TeamShareError::Infrastructure)?;
if deleted {
return Ok(false);
}
match reject_untracked(transaction.as_mut(), &state.facts, Some(grant)).await {
Ok(()) => {}
Err(error) if *error.current_context() == TeamShareError::UntrackedGrant => {
return Ok(false);
}
Err(error) => return Err(error),
}
let revision = next_revision(&state.facts)?;
write_state(transaction, state, Some(grant), revision).await?;
Ok(true)
}
/// Initialize a newly inserted entity in the caller's guarded creation transaction.
/// Never use this for existing entities or copies with inherited source consent.
/// Unshared creation does not advance revision; explicit task/call consent does.
pub async fn initialize(
transaction: &mut Transaction<'_, Postgres>,
entity: &Entity<'_>,
intent: TeamShareCreation,
) -> TeamShareResult<()> {
acquire_guard(transaction)
.await
.context(TeamShareError::Infrastructure)?;
let state = load_state(transaction.as_mut(), entity).await?;
if state.facts.current.is_some() || state.facts.revision != 0 {
return Err(report!(TeamShareError::ChangedFacts));
}
let target = intent
.resolve(state.facts.owner_team_id)
.context(TeamShareError::InvalidState)?;
if target.is_none() {
return Ok(());
}
if let Some(target) = target
&& state.facts.owner_team_id == Some(target.team_id)
&& direct_level(
transaction.as_mut(),
&entity_uuid(entity)?,
entity.entity_type,
target.team_id,
)
.await
.context(TeamShareError::Infrastructure)?
== Some(AccessLevel::Owner)
{
return Ok(());
}
reject_untracked(transaction.as_mut(), &state.facts, target).await?;
write_state(transaction, state, target, 1).await
}
/// Explicit reconciliation-only adoption of a reviewed historical direct grant.
/// Normal writes never adopt unknown rows. Both canonical facts and the exact
/// candidate level/team are rechecked; inherited rows cannot qualify for adoption.
pub async fn adopt(
transaction: &mut Transaction<'_, Postgres>,
expected: &TeamShareFacts,
reviewed: TeamShareGrant,
) -> TeamShareResult<()> {
let state = recheck(transaction, expected).await?;
if state.facts.current.is_some() || state.facts.owner_team_id != Some(reviewed.team_id) {
return Err(report!(TeamShareError::InvalidAdoption));
}
let existing = direct_level(
transaction.as_mut(),
&entity_uuid(&expected.entity)?,
expected.entity.entity_type,
reviewed.team_id,
)
.await
.context(TeamShareError::Infrastructure)?;
if existing != Some(reviewed.level.into()) {
return Err(report!(TeamShareError::InvalidAdoption));
}
let revision = next_revision(&state.facts)?;
write_state(transaction, state, Some(reviewed), revision).await
}
async fn write_state(
transaction: &mut Transaction<'_, Postgres>,
state: State,
target: Option<TeamShareGrant>,
revision: i64,
) -> TeamShareResult<()> {
let entity = &state.facts.entity;
let uuid = entity_uuid(entity)?;
let permission_id = state
.permission_id
.ok_or_else(|| report!(TeamShareError::InvalidState))?;
sqlx::query!(
r#"UPDATE "SharePermission" SET team_share_access_level = $2,
team_share_team_id = $3, team_share_revision = $4, "updatedAt" = NOW()
WHERE id = $1"#,
permission_id,
target.map(|g| AccessLevel::from(g.level)) as Option<AccessLevel>,
target.map(|g| g.team_id),
revision,
)
.execute(transaction.as_mut())
.await
.context(TeamShareError::Infrastructure)?;
if let Some(previous) = state.facts.current
&& target.map(|g| g.team_id) != Some(previous.team_id)
{
delete_direct(
transaction.as_mut(),
&uuid,
entity.entity_type,
previous.team_id,
)
.await
.context(TeamShareError::Infrastructure)?;
}
if let Some(target) = target {
upsert_direct(
transaction.as_mut(),
&uuid,
entity.entity_type,
target.team_id,
target.level.into(),
)
.await
.context(TeamShareError::Infrastructure)?;
}
if entity.entity_type == EntityType::Project {
replace_project_contributions(
transaction,
&uuid,
state.facts.current.map(|grant| grant.team_id),
target.map(|grant| (grant.team_id, grant.level.into())),
)
.await
.context(TeamShareError::Infrastructure)?;
}
Ok(())
}