Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

15.2 KB
Raw Blame
//! Agent session leg of the PostgreSQL Soup repository.
//!
//! Agent sessions live in MacroDB and are authorized through `entity_access`
//! like chats, but they are **opt-in**: a query that says nothing about them
//! (no `agent_session_filter` in its AST) gets none.

use chrono::{DateTime, Utc};
use filter_ast::Expr;
use item_filters::ast::{
    agent_session::{AgentSessionLiteral, AgentSessionSource, AgentSessionVisibility},
    properties::{PropertiesLiteral, properties_filter_matches_propertyless},
};
use model_entity::EntityType;
use model_owner::Owner;
use models_pagination::{Query, SimpleSortMethod};
use models_soup::{agent_session::SoupAgentSession, item::SoupItem};
use sqlx::{FromRow, PgPool, Postgres, QueryBuilder};
use uuid::Uuid;

use crate::domain::models::{AdvancedSortParams, SimpleSortQuery, SimpleSortRequest};

#[cfg(test)]
mod test;

/// [`AgentSessionLiteral::Source`] over the `agent_session` row `s`, shared
/// with the candidate gates. The value is a closed enum's, so it is inlined.
pub(super) fn source_sql(source: AgentSessionSource) -> String {
    format!("s.source = '{}'", <&str>::from(source))
}

/// [`AgentSessionLiteral::Visibility`] over the `agent_session` row `s`, shared
/// with the candidate gates. The value is a closed enum's, so it is inlined.
pub(super) fn visibility_sql(visibility: AgentSessionVisibility) -> String {
    format!("s.visibility = '{}'", <&str>::from(visibility))
}

#[derive(FromRow)]
struct AgentSessionRow {
    id: Uuid,
    name: String,
    archived_by: Option<String>,
    archived_at: Option<DateTime<Utc>>,
    owner_id: String,
    bot_id: Uuid,
    harness: String,
    repo_url: Option<String>,
    repo_branch: Option<String>,
    working_branch: Option<String>,
    pull_request_url: Option<String>,
    turn_state: Option<String>,
    thread_id: Option<Uuid>,
    source: String,
    visibility: String,
    status: String,
    status_event_name: Option<String>,
    created_at: DateTime<Utc>,
    modified_at: DateTime<Utc>,
    viewed_at: Option<DateTime<Utc>>,
}

struct CursorParts {
    sort: SimpleSortMethod,
    id: Option<Uuid>,
    timestamp: Option<DateTime<Utc>>,
    filter: Option<Expr<AgentSessionLiteral>>,
    property_filter: Option<Expr<PropertiesLiteral>>,
}

/// Whether the filter opts the query into agent sessions at all: an explicit
/// `Include`, or naming specific ids/owners. Fails closed on shapes that only
/// negate (`Not(Include)` is not an opt-in).
pub(super) fn opted_in(expr: &Expr<AgentSessionLiteral>) -> bool {
    /// Outcome of walking an `AgentSessionLiteral` AST for the opt-in decision.
    #[derive(Default)]
    struct OptIn {
        include: bool,
        named: bool,
    }

    fn walk(expr: &Expr<AgentSessionLiteral>, out: &mut OptIn) {
        match expr {
            Expr::Literal(AgentSessionLiteral::Include) => out.include = true,
            Expr::Literal(AgentSessionLiteral::Id(_) | AgentSessionLiteral::Owner(_)) => {
                out.named = true
            }
            // Narrows the sessions a query asks for; never asks for them.
            Expr::Literal(AgentSessionLiteral::Source(_) | AgentSessionLiteral::Visibility(_)) => {}
            Expr::And(a, b) | Expr::Or(a, b) => {
                walk(a, out);
                walk(b, out);
            }
            Expr::Not(_) => {}
        }
    }
    let mut out = OptIn::default();
    walk(expr, &mut out);
    out.include || out.named
}

#[tracing::instrument(err, skip_all, fields(limit = req.limit))]
pub(super) async fn cursor_soup(
    db: &PgPool,
    req: SimpleSortRequest<'_>,
) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
    let Some(parts) = cursor_parts(req.cursor) else {
        return Ok(Vec::new());
    };
    let Some(filter) = parts.filter.as_ref().filter(|filter| opted_in(filter)) else {
        return Ok(Vec::new());
    };
    // Agent sessions carry no properties, so a property constraint that a
    // property-less item can never satisfy removes the leg.
    if parts
        .property_filter
        .as_ref()
        .is_some_and(|filter| !properties_filter_matches_propertyless(filter))
    {
        return Ok(Vec::new());
    }

    let sort = sort_sql(parts.sort);
    let mut query = QueryBuilder::<Postgres>::new(SELECT_SQL);
    // `$1` is the requesting user for every clause in this query: the source
    // id CTE, the history join, and any `Owner` literal that binds it again.
    query.push_bind(req.user_id.as_ref().to_string());
    let named_ids = explicitly_named_ids(filter);
    query.push(ACCESS_SQL);
    // A session the query names, or one the viewer follows, is also readable
    // through its thread's parent, like the session's own REST read; GraphQL
    // `agentSession` loads this way. Other broad-list rows stay limited to
    // session grants so every document thread's sessions do not flood Home
    // and Recent.
    query.push("(");
    query.push(SESSION_GRANT_SQL);
    query.push(" OR ((s.id = ANY(");
    query.push_bind(named_ids.clone());
    query.push(") OR ");
    query.push(FOLLOWED_SQL);
    query.push(") AND ");
    query.push(PARENT_THREAD_GRANT_SQL);
    query.push(")) AND (");
    push_filter(&mut query, filter);
    query.push(")");
    // Hidden thread sessions and DM contexts are absent from broad lists,
    // except for the people who prompted them from the session itself. A
    // query that names the session still returns it, so favorites and direct
    // id lookups keep working.
    query.push(" AND (s.visibility <> 'hidden' OR ");
    query.push(FOLLOWED_SQL);
    query.push(" OR s.id = ANY(");
    query.push_bind(named_ids);
    query.push("))");
    if let (Some(timestamp), Some(id)) = (parts.timestamp, parts.id) {
        query.push(format!(" AND ({sort}, s.id) < ("));
        query.push_bind(timestamp);
        query.push(", ");
        query.push_bind(id);
        query.push(")");
    }
    query.push(format!(" ORDER BY {sort} DESC, s.id DESC LIMIT "));
    query.push_bind(i64::from(req.limit));

    query
        .build_query_as::<AgentSessionRow>()
        .fetch_all(db)
        .await?
        .into_iter()
        .map(row_to_item)
        .collect()
}

#[tracing::instrument(err, skip_all, fields(entity_count = req.entities.len()))]
pub(super) async fn by_ids(
    db: &PgPool,
    req: AdvancedSortParams<'_>,
) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
    let ids = req
        .entities
        .iter()
        .filter(|entity| entity.entity_type == EntityType::AgentSession)
        .filter_map(|entity| entity.entity_id.parse::<Uuid>().ok())
        .collect::<Vec<_>>();
    if ids.is_empty() {
        return Ok(Vec::new());
    }

    let mut query = QueryBuilder::<Postgres>::new(SELECT_SQL);
    query.push_bind(req.user_id.as_ref().to_string());
    query.push(ACCESS_SQL);
    // Every session here is named, so its thread's parent grants apply too.
    query.push("(");
    query.push(SESSION_GRANT_SQL);
    query.push(" OR ");
    query.push(PARENT_THREAD_GRANT_SQL);
    query.push(") AND s.id = ANY(");
    query.push_bind(ids);
    query.push(") ORDER BY s.modified_at DESC, s.id DESC");
    query
        .build_query_as::<AgentSessionRow>()
        .fetch_all(db)
        .await?
        .into_iter()
        .map(row_to_item)
        .collect()
}

fn cursor_parts(cursor: SimpleSortQuery) -> Option<CursorParts> {
    Some(match cursor {
        SimpleSortQuery::Property(_) => return None,
        // No AST means the query never mentioned agent sessions: opt-out.
        SimpleSortQuery::NoFilter(query) => parts_from_query(&query, None, None),
        SimpleSortQuery::FilterFrecency(query) => parts_from_query(&query, None, None),
        SimpleSortQuery::ItemsFilter(query) => {
            let ast = query.filter();
            parts_from_query(
                &query,
                ast.agent_session_filter.as_deref().cloned(),
                ast.properties_filter.as_deref().cloned(),
            )
        }
        SimpleSortQuery::ItemsAndFrecencyFilter(query) => {
            let ast = &query.filter().1;
            parts_from_query(
                &query,
                ast.agent_session_filter.as_deref().cloned(),
                ast.properties_filter.as_deref().cloned(),
            )
        }
    })
}

fn parts_from_query<F>(
    query: &Query<Uuid, SimpleSortMethod, F>,
    filter: Option<Expr<AgentSessionLiteral>>,
    property_filter: Option<Expr<PropertiesLiteral>>,
) -> CursorParts {
    let (id, timestamp) = query.vals();
    CursorParts {
        sort: *query.sort_method(),
        id: id.copied(),
        timestamp: timestamp.copied(),
        filter,
        property_filter,
    }
}

/// Selects the row shape and joins the requesting user's view history. Ends
/// right before the first bind so `$1` is the user id.
const SELECT_SQL: &str = r#"
    WITH user_source_ids AS (
        SELECT cp.channel_id::text AS source_id
        FROM comms_channel_participants cp
        WHERE cp.user_id = "#;

/// Continues [`SELECT_SQL`] after the user id bind: the remaining source-id
/// arms, the row projection, and the history join, ending at `WHERE` so the
/// caller appends its access predicate. Every `$1` here refers back to that
/// same bind.
const ACCESS_SQL: &str = r#" AND cp.left_at IS NULL
        UNION ALL
        SELECT t.team_id::text FROM team_user t WHERE t.user_id = $1
        UNION ALL
        SELECT $1
    )
    SELECT
        s.id,
        s.name,
        s.archived_by::TEXT AS archived_by,
        s.archived_at,
        s.owner_id,
        s.bot_id,
        s.harness,
        s.repo_url,
        s.repo_branch,
        s.working_branch,
        s.pull_request_url,
        s.turn_state,
        s.thread_id,
        s.source,
        s.visibility,
        s.status,
        s.status_event_name,
        s.created_at,
        s.modified_at,
        uh."updatedAt"::timestamptz AS viewed_at
    FROM agent_session s
    LEFT JOIN "UserHistory" uh
        ON uh."itemId" = s.id::text
        AND uh."itemType" = 'agent_session'
        AND uh."userId" = $1
    WHERE "#;

/// Continues [`ACCESS_SQL`]: the viewer holds a grant on the session itself.
const SESSION_GRANT_SQL: &str = r#"EXISTS (
        SELECT 1 FROM entity_access ea
        WHERE ea.entity_id = s.id
            AND ea.entity_type = 'agent_session'
            AND ea.source_id IN (SELECT source_id FROM user_source_ids)
    )"#;

/// The viewer (`$1`) prompted the session `s` from the session itself, which
/// lists it for them even when it is hidden. Grants nothing on its own.
pub(super) const FOLLOWED_SQL: &str = r#"EXISTS (
        SELECT 1 FROM agent_session_follower f
        WHERE f.agent_session_id = s.id AND f.user_id = $1
    )"#;

/// The viewer holds a grant on the document or call whose live thread the
/// session runs in. Mirrors the parent inheritance `entity_access` applies to
/// agent sessions, over the same stored grants Soup reads for documents.
pub(super) const PARENT_THREAD_GRANT_SQL: &str = r#"EXISTS (
        SELECT 1 FROM comms_messages m
        JOIN comms_message_threads t ON t.root_id = m.id AND t.deleted_at IS NULL
        JOIN entity_access ea
            ON ea.entity_id::text = m.parent_entity_id
            AND ea.entity_type = m.parent_entity_type
        WHERE m.id = s.thread_id
            AND m.parent_entity_type IN ('document', 'call')
            AND ea.source_id IN (SELECT source_id FROM user_source_ids)
    )"#;

fn sort_sql(sort: SimpleSortMethod) -> &'static str {
    match sort {
        SimpleSortMethod::CreatedAt => "s.created_at",
        SimpleSortMethod::UpdatedAt => "s.modified_at",
        SimpleSortMethod::ViewedAt => {
            r#"COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')::timestamptz"#
        }
        SimpleSortMethod::ViewedUpdated => {
            r#"COALESCE(uh."updatedAt", s.modified_at)::timestamptz"#
        }
    }
}

/// Session ids the query asked for by name, ignoring ids under `NOT`.
/// Those stay visible even when the session is hidden from broad lists.
pub(super) fn explicitly_named_ids(expression: &Expr<AgentSessionLiteral>) -> Vec<Uuid> {
    fn walk(expression: &Expr<AgentSessionLiteral>, negated: bool, ids: &mut Vec<Uuid>) {
        match expression {
            Expr::Literal(AgentSessionLiteral::Id(id)) if !negated => ids.push(*id),
            Expr::Not(inner) => walk(inner, true, ids),
            Expr::And(left, right) | Expr::Or(left, right) => {
                walk(left, negated, ids);
                walk(right, negated, ids);
            }
            Expr::Literal(_) => {}
        }
    }
    let mut ids = Vec::new();
    walk(expression, false, &mut ids);
    ids
}

fn push_filter(builder: &mut QueryBuilder<'_, Postgres>, expression: &Expr<AgentSessionLiteral>) {
    match expression {
        Expr::And(left, right) => {
            builder.push("(");
            push_filter(builder, left);
            builder.push(" AND ");
            push_filter(builder, right);
            builder.push(")");
        }
        Expr::Or(left, right) => {
            builder.push("(");
            push_filter(builder, left);
            builder.push(" OR ");
            push_filter(builder, right);
            builder.push(")");
        }
        Expr::Not(inner) => {
            builder.push("NOT (");
            push_filter(builder, inner);
            builder.push(")");
        }
        // Include only opts the leg in; it constrains nothing.
        Expr::Literal(AgentSessionLiteral::Include) => {
            builder.push("TRUE");
        }
        Expr::Literal(AgentSessionLiteral::Id(id)) => {
            builder.push("s.id = ");
            builder.push_bind(*id);
        }
        Expr::Literal(AgentSessionLiteral::Owner(owner)) => {
            builder.push("s.owner_id = ");
            builder.push_bind(owner.principal_id());
        }
        Expr::Literal(AgentSessionLiteral::Source(source)) => {
            builder.push(source_sql(*source));
        }
        Expr::Literal(AgentSessionLiteral::Visibility(visibility)) => {
            builder.push(visibility_sql(*visibility));
        }
    }
}

fn row_to_item(row: AgentSessionRow) -> Result<SoupItem<()>, sqlx::Error> {
    let owner_id = Owner::from_principal_str(&row.owner_id).map_err(super::type_err)?;
    // Mirror `agent_session::domain::model::SessionStatus`'s wire shape: the
    // event name is the status once one has arrived.
    let status = match row.status.as_str() {
        "event" => row.status_event_name.unwrap_or(row.status),
        _ => row.status,
    };
    Ok(SoupItem::AgentSession(SoupAgentSession {
        id: row.id,
        name: row.name,
        archived_by: row
            .archived_by
            .as_deref()
            .map(str::parse)
            .transpose()
            .map_err(super::type_err)?,
        archived_at: row.archived_at,
        owner_id,
        bot_id: row.bot_id,
        harness: row.harness,
        repo_url: row.repo_url,
        repo_branch: row.repo_branch,
        pull_request_url: row.pull_request_url,
        working_branch: row.working_branch,
        pull_request_state: None,
        pull_request_id: None,
        turn_state: row.turn_state,
        thread_id: row.thread_id,
        source: row.source.parse().map_err(super::type_err)?,
        visibility: row.visibility.parse().map_err(super::type_err)?,
        status,
        created_at: row.created_at,
        updated_at: row.modified_at,
        viewed_at: row.viewed_at,
        extra: (),
    }))
}