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.

chore(agents): hide inline @macro sessions from the agents list and search (#7096)

Wolf Mermelstein138abedd3e42
diff --git a/.sqlx/query-04a02e2ea3a0fda139f1f1bf0398760e96a367cb525d0a551639f12e2b39ef4f.json b/.sqlx/query-04a02e2ea3a0fda139f1f1bf0398760e96a367cb525d0a551639f12e2b39ef4f.json
deleted file mode 100644
index 3fe58816f6..0000000000
--- a/.sqlx/query-04a02e2ea3a0fda139f1f1bf0398760e96a367cb525d0a551639f12e2b39ef4f.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT DISTINCT entity_id\n FROM entity_access\n WHERE entity_type = 'agent_session'\n AND source_id = ANY($1)\n AND (cardinality($2::uuid[]) = 0 OR entity_id = ANY($2))\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "entity_id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "TextArray",
- "UuidArray"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "04a02e2ea3a0fda139f1f1bf0398760e96a367cb525d0a551639f12e2b39ef4f"
-}
diff --git a/.sqlx/query-15e02b53db3701c3b5ccf21336a9a67f5477ec629e173e58c67ea58219048c06.json b/.sqlx/query-15e02b53db3701c3b5ccf21336a9a67f5477ec629e173e58c67ea58219048c06.json
new file mode 100644
index 0000000000..850cc4de9b
--- /dev/null
+++ b/.sqlx/query-15e02b53db3701c3b5ccf21336a9a67f5477ec629e173e58c67ea58219048c06.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE agent_session SET list_hidden = TRUE WHERE id = $1",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "15e02b53db3701c3b5ccf21336a9a67f5477ec629e173e58c67ea58219048c06"
+}
diff --git a/.sqlx/query-29997f3c4d74e67781d4dc3691976cccc8554cf14567bf93a75fe4a3c7e333db.json b/.sqlx/query-29997f3c4d74e67781d4dc3691976cccc8554cf14567bf93a75fe4a3c7e333db.json
new file mode 100644
index 0000000000..fd49e0c2f1
--- /dev/null
+++ b/.sqlx/query-29997f3c4d74e67781d4dc3691976cccc8554cf14567bf93a75fe4a3c7e333db.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO agent_session (\n id, owner_id, bot_id, model, harness, repo_url, workspace, list_hidden\n )\n VALUES ($1, $2, '00000000-0000-0000-0000-00000000a9e7', 'model', 'harness', NULL, '/workspace', TRUE)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "29997f3c4d74e67781d4dc3691976cccc8554cf14567bf93a75fe4a3c7e333db"
+}
diff --git a/.sqlx/query-ca37ef6aa67efe87991aea962750cdebbd32a398d0c3d04e45ba5c26de1ea006.json b/.sqlx/query-ca37ef6aa67efe87991aea962750cdebbd32a398d0c3d04e45ba5c26de1ea006.json
new file mode 100644
index 0000000000..e62f23b5c1
--- /dev/null
+++ b/.sqlx/query-ca37ef6aa67efe87991aea962750cdebbd32a398d0c3d04e45ba5c26de1ea006.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT id, list_hidden FROM agent_session WHERE id = ANY($1) ORDER BY list_hidden DESC",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "list_hidden",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "ca37ef6aa67efe87991aea962750cdebbd32a398d0c3d04e45ba5c26de1ea006"
+}
diff --git a/.sqlx/query-dfd524b0379d7a27c24b97e7f46c7479e630c13fbdab5913954d20c420545116.json b/.sqlx/query-dfd524b0379d7a27c24b97e7f46c7479e630c13fbdab5913954d20c420545116.json
new file mode 100644
index 0000000000..b74b44b4be
--- /dev/null
+++ b/.sqlx/query-dfd524b0379d7a27c24b97e7f46c7479e630c13fbdab5913954d20c420545116.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT DISTINCT entity_id\n FROM entity_access\n WHERE entity_type = 'agent_session'\n AND source_id = ANY($1)\n AND (cardinality($2::uuid[]) = 0 OR entity_id = ANY($2))\n -- Inline @macro sessions stay out of search unless asked for by\n -- id. A grant with no session row (tests, a deleted session) is\n -- not hidden: the predicate only matches a `list_hidden` row.\n AND (\n entity_id = ANY($2)\n OR NOT EXISTS (\n SELECT 1 FROM agent_session s\n WHERE s.id = entity_access.entity_id\n AND s.list_hidden\n )\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "entity_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "TextArray",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "dfd524b0379d7a27c24b97e7f46c7479e630c13fbdab5913954d20c420545116"
+}
diff --git a/.sqlx/query-349c3a9085f7a4d136fea60c95d2a5da45a619527c60d1a5f743f4d81abc43f1.json b/.sqlx/query-eaa16a8742d41364bea76ce5aeeb7e5f62c6dbd59361a4507e9583841a760772.json
similarity index 76%
rename from .sqlx/query-349c3a9085f7a4d136fea60c95d2a5da45a619527c60d1a5f743f4d81abc43f1.json
rename to .sqlx/query-eaa16a8742d41364bea76ce5aeeb7e5f62c6dbd59361a4507e9583841a760772.json
index 9bc07d9f0b..49332f3303 100644
--- a/.sqlx/query-349c3a9085f7a4d136fea60c95d2a5da45a619527c60d1a5f743f4d81abc43f1.json
+++ b/.sqlx/query-eaa16a8742d41364bea76ce5aeeb7e5f62c6dbd59361a4507e9583841a760772.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO agent_session (\n id, owner_id, thread_id, originating_message_id, bot_id, model,\n harness, repo_url, workspace, sandbox_size, instructions,\n acp_session_id, status, status_event_name, egress_token_hash,\n mcp_scope, mcp_servers, repo_branch\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)\n RETURNING\n id, name, owner_id, thread_id, originating_message_id, bot_id,\n model, harness, repo_url, repo_branch, pull_request_url, workspace, sandbox_size, instructions,\n mcp_scope, mcp_servers, acp_session_id, status,\n status_event_name, created_at, modified_at,\n (SELECT jsonb_build_object('type', parent_entity_type, 'id', parent_entity_id)\n FROM comms_messages WHERE id = agent_session.thread_id)\n AS \"thread_parent?: Json<MessageParent>\",\n -- A row being created cannot have an external identity yet.\n NULL::TEXT AS \"external_provider?\", NULL::TEXT AS \"external_id?\",\n NULL::TEXT AS \"external_name?\", NULL::TEXT AS \"external_url?\",\n NULL::TEXT AS \"external_last_run_id?\"\n ",
+ "query": "\n INSERT INTO agent_session (\n id, owner_id, thread_id, originating_message_id, bot_id, model,\n harness, repo_url, workspace, sandbox_size, instructions,\n acp_session_id, status, status_event_name, egress_token_hash,\n mcp_scope, mcp_servers, repo_branch, list_hidden\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)\n RETURNING\n id, name, owner_id, thread_id, originating_message_id, bot_id,\n model, harness, repo_url, repo_branch, pull_request_url, workspace, sandbox_size, instructions,\n mcp_scope, mcp_servers, acp_session_id, status,\n status_event_name, created_at, modified_at,\n (SELECT jsonb_build_object('type', parent_entity_type, 'id', parent_entity_id)\n FROM comms_messages WHERE id = agent_session.thread_id)\n AS \"thread_parent?: Json<MessageParent>\",\n -- A row being created cannot have an external identity yet.\n NULL::TEXT AS \"external_provider?\", NULL::TEXT AS \"external_id?\",\n NULL::TEXT AS \"external_name?\", NULL::TEXT AS \"external_url?\",\n NULL::TEXT AS \"external_last_run_id?\"\n ",
"describe": {
"columns": [
{
@@ -158,7 +158,8 @@
"Text",
"Text",
"Jsonb",
- "Text"
+ "Text",
+ "Bool"
]
},
"nullable": [
@@ -191,5 +192,5 @@
null
]
},
- "hash": "349c3a9085f7a4d136fea60c95d2a5da45a619527c60d1a5f743f4d81abc43f1"
+ "hash": "eaa16a8742d41364bea76ce5aeeb7e5f62c6dbd59361a4507e9583841a760772"
}
diff --git a/crates/agent_session/Cargo.toml b/crates/agent_session/Cargo.toml
index c4afc4ec71..b039048eaf 100644
--- a/crates/agent_session/Cargo.toml
+++ b/crates/agent_session/Cargo.toml
@@ -80,6 +80,7 @@ sqlx = { workspace = true, features = ["json"], optional = true }
chrono = { workspace = true }
thiserror = { workspace = true }
anyhow = { workspace = true }
+bot_id = { path = "../bot_id" }
serde = { workspace = true }
serde_json = { workspace = true }
strum = { workspace = true }
diff --git a/crates/agent_session/src/outbound/postgres.rs b/crates/agent_session/src/outbound/postgres.rs
index d41d8bdefd..a8b7e4064c 100644
--- a/crates/agent_session/src/outbound/postgres.rs
+++ b/crates/agent_session/src/outbound/postgres.rs
@@ -31,6 +31,7 @@ use crate::outbound::connection_gateway_realtime::SessionAudience;
use agent_client_protocol::schema::v1::SessionId;
use agent_runtime_protocol::domain::schema::v0::{SystemEvent, ToRuntimeMessage, ToServerMessage};
use anyhow::Context;
+use bot_id::MACRO_NEW_BOT_ID;
use bots::domain::models::BotId;
use bots::domain::ports::BotRepo;
use bots::outbound::pg_bots_repo::PgBotsRepo;
@@ -314,6 +315,10 @@ impl AgentSessionRepo for PgAgentSessionRepo {
.context("begin agent session create")?;
let (status, status_event_name) = status_columns(&SessionStatus::NoMessages);
+ // An inline @macro mention is a one-shot on the message. It stays out
+ // of the agents list and search. Every other session — the agents
+ // composer, coding agents — is a list row.
+ let list_hidden = bot_id == MACRO_NEW_BOT_ID && thread_id.is_some();
let row = sqlx::query_as!(
AgentSessionRow,
r#"
@@ -321,9 +326,9 @@ impl AgentSessionRepo for PgAgentSessionRepo {
id, owner_id, thread_id, originating_message_id, bot_id, model,
harness, repo_url, workspace, sandbox_size, instructions,
acp_session_id, status, status_event_name, egress_token_hash,
- mcp_scope, mcp_servers, repo_branch
+ mcp_scope, mcp_servers, repo_branch, list_hidden
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
RETURNING
id, name, owner_id, thread_id, originating_message_id, bot_id,
model, harness, repo_url, repo_branch, pull_request_url, workspace, sandbox_size, instructions,
@@ -355,6 +360,7 @@ impl AgentSessionRepo for PgAgentSessionRepo {
mcp_servers.scope_str(),
mcp_servers_json,
repo_branch.as_ref().map(|branch| branch.as_str()),
+ list_hidden,
)
.fetch_one(&mut *transaction)
.await
diff --git a/crates/agent_session/src/outbound/postgres/test.rs b/crates/agent_session/src/outbound/postgres/test.rs
index a7e6642120..5d272a9294 100644
--- a/crates/agent_session/src/outbound/postgres/test.rs
+++ b/crates/agent_session/src/outbound/postgres/test.rs
@@ -1080,6 +1080,39 @@ async fn create_records_the_session_in_the_owners_history(pool: PgPool) {
assert_eq!(history[0].item_type, "agent_session");
}
+/// Inline `@macro` (the agent-session Macro bot, born on a thread) starts
+/// hidden from lists. The same bot started with no thread, and any other
+/// bot, is listed immediately.
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn inline_macro_sessions_start_hidden(pool: PgPool) {
+ let repo = PgAgentSessionRepo::new(pool.clone());
+ create_test_bot(&pool).await;
+ let (_channel_id, thread_id, originating_message_id) =
+ insert_originating_thread_fixture(&pool).await;
+
+ let inline = create_session(
+ &repo,
+ new_session(
+ bot_id::MACRO_NEW_BOT_ID,
+ Some(thread_id),
+ Some(originating_message_id),
+ ),
+ )
+ .await;
+ let composer = create_session(&repo, new_session(bot_id::MACRO_NEW_BOT_ID, None, None)).await;
+
+ let hidden = sqlx::query!(
+ "SELECT id, list_hidden FROM agent_session WHERE id = ANY($1) ORDER BY list_hidden DESC",
+ &[inline.id.as_uuid(), composer.id.as_uuid()] as &[Uuid],
+ )
+ .fetch_all(&pool)
+ .await
+ .expect("read list visibility");
+ assert!(hidden[0].list_hidden, "the thread session starts hidden");
+ assert_eq!(hidden[0].id, inline.id.as_uuid());
+ assert!(!hidden[1].list_hidden, "a composer session is listed");
+}
+
/// A preview reports every existing id with whether the viewer holds a grant:
/// the owner through their own row, a channel member through the channel's,
/// a stranger through none, and an unknown id not at all. Duplicates in the
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access.rs
index 45f9f9af61..7f121efd43 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access.rs
@@ -28,6 +28,17 @@ pub async fn accessible_session_ids(
WHERE entity_type = 'agent_session'
AND source_id = ANY($1)
AND (cardinality($2::uuid[]) = 0 OR entity_id = ANY($2))
+ -- Inline @macro sessions stay out of search unless asked for by
+ -- id. A grant with no session row (tests, a deleted session) is
+ -- not hidden: the predicate only matches a `list_hidden` row.
+ AND (
+ entity_id = ANY($2)
+ OR NOT EXISTS (
+ SELECT 1 FROM agent_session s
+ WHERE s.id = entity_access.entity_id
+ AND s.list_hidden
+ )
+ )
"#,
&source_ids.0,
requested_ids,
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access/test.rs
index 00de0baa4e..0c43688a33 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access/test.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/agent_session_access/test.rs
@@ -199,3 +199,64 @@ async fn session_allowlist_matches_grants_and_respects_requested_ids(pool: PgPoo
.is_empty()
);
}
+
+/// Search's allowlist drops an inline session unless the caller names its id.
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn hidden_sessions_leave_the_allowlist_unless_named(pool: PgPool) {
+ let session = uuid::Uuid::now_v7();
+ let viewer = "macro|[email protected]";
+ let macro_user_id = uuid::Uuid::now_v7();
+ sqlx::query!(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $2)",
+ macro_user_id,
+ viewer,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ r#"INSERT INTO "User" ("id", "email", "macro_user_id") VALUES ($1, $1, $2)"#,
+ viewer,
+ macro_user_id,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ r#"
+ INSERT INTO agent_session (
+ id, owner_id, bot_id, model, harness, repo_url, workspace, list_hidden
+ )
+ VALUES ($1, $2, '00000000-0000-0000-0000-00000000a9e7', 'model', 'harness', NULL, '/workspace', TRUE)
+ "#,
+ session,
+ viewer,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ sqlx::query!(
+ r#"
+ INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
+ VALUES ($1, 'agent_session', $2, 'user', 'owner')
+ "#,
+ session,
+ viewer,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ assert!(
+ accessible_session_ids(&pool, &SourceIds(vec![viewer.into()]), &[])
+ .await
+ .unwrap()
+ .is_empty()
+ );
+ assert_eq!(
+ accessible_session_ids(&pool, &SourceIds(vec![viewer.into()]), &[session])
+ .await
+ .unwrap(),
+ vec![session]
+ );
+}
diff --git a/crates/macro_db_client/migrations/20260928021331_agent_session_list_hidden.sql b/crates/macro_db_client/migrations/20260928021331_agent_session_list_hidden.sql
new file mode 100644
index 0000000000..a618f683d2
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260928021331_agent_session_list_hidden.sql
@@ -0,0 +1,5 @@
+-- Inline @macro sessions (a channel or document mention) stay out of the
+-- agents list and search. Sessions created anywhere else stay listed, and
+-- rows written before this column existed default to visible.
+ALTER TABLE agent_session
+ ADD COLUMN IF NOT EXISTS list_hidden BOOLEAN NOT NULL DEFAULT false;
diff --git a/crates/soup/src/outbound/pg_soup_repo/agent_session.rs b/crates/soup/src/outbound/pg_soup_repo/agent_session.rs
index 662fea22d4..fdfea39dcf 100644
--- a/crates/soup/src/outbound/pg_soup_repo/agent_session.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/agent_session.rs
@@ -107,6 +107,12 @@ pub(super) async fn cursor_soup(
query.push(" AND (");
push_filter(&mut query, filter);
query.push(")");
+ // Inline @macro sessions (`list_hidden`) are absent from broad lists. A
+ // query that names the session still returns it, so favorites and direct
+ // id lookups keep working.
+ query.push(" AND (s.list_hidden = FALSE OR s.id = ANY(");
+ query.push_bind(explicitly_named_ids(filter));
+ query.push("))");
if let (Some(timestamp), Some(id)) = (parts.timestamp, parts.id) {
query.push(format!(" AND ({sort}, s.id) < ("));
query.push_bind(timestamp);
@@ -253,6 +259,25 @@ fn sort_sql(sort: SimpleSortMethod) -> &'static str {
}
}
+/// Session ids the query asked for by name, ignoring ids under `NOT`.
+/// Those stay visible even when the session is hidden from broad lists.
+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) => {
diff --git a/crates/soup/src/outbound/pg_soup_repo/agent_session/test.rs b/crates/soup/src/outbound/pg_soup_repo/agent_session/test.rs
index 842647f2e6..e6f1e084fa 100644
--- a/crates/soup/src/outbound/pg_soup_repo/agent_session/test.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/agent_session/test.rs
@@ -400,3 +400,59 @@ async fn by_ids_respects_access(pool: PgPool) -> anyhow::Result<()> {
assert_eq!(ids(&member_items), vec![fixture.shared]);
Ok(())
}
+
+/// An inline `@macro` session (`list_hidden`) is missing from broad lists for
+/// everyone, but a named id still finds it and hydrating by id still works.
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn hidden_sessions_stay_out_of_broad_lists(pool: PgPool) -> anyhow::Result<()> {
+ let fixture = seed(&pool).await?;
+ sqlx::query!(
+ "UPDATE agent_session SET list_hidden = TRUE WHERE id = $1",
+ fixture.shared,
+ )
+ .execute(&pool)
+ .await?;
+
+ let owner_before = cursor_soup(
+ &pool,
+ request(OWNER, Some(Expr::val(AgentSessionLiteral::Include))),
+ )
+ .await?;
+ assert_eq!(
+ ids(&owner_before),
+ vec![fixture.private],
+ "the owner's list omits the unopened inline session"
+ );
+ let member_before = cursor_soup(
+ &pool,
+ request(MEMBER, Some(Expr::val(AgentSessionLiteral::Include))),
+ )
+ .await?;
+ assert!(
+ member_before.is_empty(),
+ "a channel member does not inherit the owner's list"
+ );
+
+ let named = cursor_soup(
+ &pool,
+ request(
+ OWNER,
+ Some(Expr::val(AgentSessionLiteral::Id(fixture.shared))),
+ ),
+ )
+ .await?;
+ assert_eq!(ids(&named), vec![fixture.shared]);
+
+ let entities = [EntityType::AgentSession.with_entity_string(fixture.shared.to_string())];
+ let hydrated = by_ids(
+ &pool,
+ AdvancedSortParams {
+ entities: &entities,
+ user_id: MacroUserIdStr::parse_from_str(MEMBER)?,
+ },
+ )
+ .await?;
+ assert_eq!(ids(&hydrated), vec![fixture.shared]);
+
+ Ok(())
+}