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.

feat(favorites): let users favorite agent sessions (#8653)

Peter Chinman6d8c35570ad5
diff --git a/.sqlx/query-40455d8de584dbe7062a9890b4f5c5c52e756342a66a6977482a690c3d011ff3.json b/.sqlx/query-7c3b46360c39e26a6d6b76614ddad6342ae962f12417a75b6a760153b8119659.json
similarity index 74%
rename from .sqlx/query-40455d8de584dbe7062a9890b4f5c5c52e756342a66a6977482a690c3d011ff3.json
rename to .sqlx/query-7c3b46360c39e26a6d6b76614ddad6342ae962f12417a75b6a760153b8119659.json
index a37e81bbb5..20162e3c9c 100644
--- a/.sqlx/query-40455d8de584dbe7062a9890b4f5c5c52e756342a66a6977482a690c3d011ff3.json
+++ b/.sqlx/query-7c3b46360c39e26a6d6b76614ddad6342ae962f12417a75b6a760153b8119659.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n f.entity_type as \"entity_type!\",\n f.entity_id as \"entity_id!\",\n f.sort_order as \"sort_order!\",\n f.created_at as \"created_at!\",\n d.\"fileType\" as \"file_type?\",\n dt.sub_type::text as \"document_sub_type?\",\n ch.channel_type::text as \"channel_type?\",\n cm.parent_entity_id as \"channel_id?\"\n FROM favorite f\n -- The comms tables key on uuid while favorite.entity_id is text.\n -- Compare in uuid (casting the favorite side) so their\n -- primary-key indexes are usable; casting the table side to text\n -- forced a full scan of each table on every listing. The regex\n -- guard keeps non-uuid entity_ids from failing the cast: they\n -- yield NULL and simply don't hydrate.\n CROSS JOIN LATERAL (\n SELECT CASE\n WHEN f.entity_id ~ '^[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}$'\n THEN f.entity_id::uuid\n END AS entity_uuid\n ) fid\n LEFT JOIN \"Document\" d ON f.entity_type = 'document' AND d.id = f.entity_id\n LEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_id\n LEFT JOIN \"Chat\" c ON f.entity_type = 'chat' AND c.id = f.entity_id\n LEFT JOIN \"Project\" p ON f.entity_type = 'project' AND p.id = f.entity_id\n LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid\n LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid AND cm.parent_entity_type = 'channel'\n WHERE f.user_id = $1\n AND ($2::text[] IS NULL OR f.entity_type = ANY($2))\n AND ($3::text[] IS NULL OR f.entity_id = ANY($3))\n AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p.\"deletedAt\" IS NULL))\n ORDER BY f.sort_order ASC, f.created_at ASC\n ",
+ "query": "\n SELECT\n f.entity_type as \"entity_type!\",\n f.entity_id as \"entity_id!\",\n f.sort_order as \"sort_order!\",\n f.created_at as \"created_at!\",\n d.\"fileType\" as \"file_type?\",\n dt.sub_type::text as \"document_sub_type?\",\n ch.channel_type::text as \"channel_type?\",\n cm.parent_entity_id as \"channel_id?\"\n FROM favorite f\n -- The comms tables key on uuid while favorite.entity_id is text.\n -- Compare in uuid (casting the favorite side) so their\n -- primary-key indexes are usable; casting the table side to text\n -- forced a full scan of each table on every listing. The regex\n -- guard keeps non-uuid entity_ids from failing the cast: they\n -- yield NULL and simply don't hydrate.\n CROSS JOIN LATERAL (\n SELECT CASE\n WHEN f.entity_id ~ '^[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}$'\n THEN f.entity_id::uuid\n END AS entity_uuid\n ) fid\n LEFT JOIN \"Document\" d ON f.entity_type = 'document' AND d.id = f.entity_id\n LEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_id\n LEFT JOIN \"Chat\" c ON f.entity_type = 'chat' AND c.id = f.entity_id\n LEFT JOIN \"Project\" p ON f.entity_type = 'project' AND p.id = f.entity_id\n LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid\n LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid AND cm.parent_entity_type = 'channel'\n -- Agent sessions are hard-deleted, so the row's absence is the\n -- deletion check that `deletedAt` is for the item tables.\n LEFT JOIN agent_session ags ON f.entity_type = 'agent_session' AND ags.id = fid.entity_uuid\n WHERE f.user_id = $1\n AND ($2::text[] IS NULL OR f.entity_type = ANY($2))\n AND ($3::text[] IS NULL OR f.entity_id = ANY($3))\n AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'agent_session' OR ags.id IS NOT NULL)\n ORDER BY f.sort_order ASC, f.created_at ASC\n ",
"describe": {
"columns": [
{
@@ -62,5 +62,5 @@
false
]
},
- "hash": "40455d8de584dbe7062a9890b4f5c5c52e756342a66a6977482a690c3d011ff3"
+ "hash": "7c3b46360c39e26a6d6b76614ddad6342ae962f12417a75b6a760153b8119659"
}
diff --git a/crates/favorites/AGENTS.md b/crates/favorites/AGENTS.md
index 3cf7d35f38..c86bdf6dbe 100644
--- a/crates/favorites/AGENTS.md
+++ b/crates/favorites/AGENTS.md
@@ -30,8 +30,10 @@ hexagonal architecture pattern.
## Recipes
-- Add a favoritable entity type: `src/domain/models.rs` → `src/domain/service.rs`
- → `src/outbound/pg_favorites_repo.rs` → caller's feature wiring.
+- Add a favoritable entity type: `validate_favoritable` in
+ `src/domain/mutation_service.rs` → `list_favorites` in
+ `src/outbound/pg_favorites_repo.rs` if the kind can be deleted → caller's
+ feature wiring.
- Add an AI tool action: `src/inbound/toolset.rs` → `src/domain/mutation_service.rs`.
## Tool contracts
diff --git a/crates/favorites/src/domain/mutation_service.rs b/crates/favorites/src/domain/mutation_service.rs
index 2039a6f7ea..a272ba415a 100644
--- a/crates/favorites/src/domain/mutation_service.rs
+++ b/crates/favorites/src/domain/mutation_service.rs
@@ -98,7 +98,8 @@ fn validate_favoritable(entity_type: EntityType) -> Result<(), FavoritesError> {
| EntityType::ForeignEntity
| EntityType::CrmCompany
| EntityType::CrmContact
- | EntityType::WhatsAppGroup => Ok(()),
+ | EntityType::WhatsAppGroup
+ | EntityType::AgentSession => Ok(()),
EntityType::User
| EntityType::Team
| EntityType::ChannelMessage
@@ -106,7 +107,6 @@ fn validate_favoritable(entity_type: EntityType) -> Result<(), FavoritesError> {
| EntityType::CalendarEvent
| EntityType::Reminder
| EntityType::Skill
- | EntityType::AgentSession
| EntityType::ScheduledAction
| EntityType::Initiative
// Databases and forms are not favoritable yet, and rows never are.
diff --git a/crates/favorites/src/domain/mutation_service/test.rs b/crates/favorites/src/domain/mutation_service/test.rs
index b5ee1bf6ec..c365142160 100644
--- a/crates/favorites/src/domain/mutation_service/test.rs
+++ b/crates/favorites/src/domain/mutation_service/test.rs
@@ -359,6 +359,7 @@ async fn every_supported_interactive_entity_can_be_added_and_removed() {
EntityType::ForeignEntity,
EntityType::CrmCompany,
EntityType::CrmContact,
+ EntityType::AgentSession,
] {
let favorites = Arc::new(FakeFavoritesService::default());
let authorizer = Arc::new(FakeAuthorizer::default());
diff --git a/crates/favorites/src/outbound/pg_favorites_repo.rs b/crates/favorites/src/outbound/pg_favorites_repo.rs
index 6f5729f4e1..b916b611d4 100644
--- a/crates/favorites/src/outbound/pg_favorites_repo.rs
+++ b/crates/favorites/src/outbound/pg_favorites_repo.rs
@@ -169,12 +169,16 @@ impl FavoritesRepo for PgFavoritesRepo {
LEFT JOIN "Project" p ON f.entity_type = 'project' AND p.id = f.entity_id
LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid
LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid AND cm.parent_entity_type = 'channel'
+ -- Agent sessions are hard-deleted, so the row's absence is the
+ -- deletion check that `deletedAt` is for the item tables.
+ LEFT JOIN agent_session ags ON f.entity_type = 'agent_session' AND ags.id = fid.entity_uuid
WHERE f.user_id = $1
AND ($2::text[] IS NULL OR f.entity_type = ANY($2))
AND ($3::text[] IS NULL OR f.entity_id = ANY($3))
AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d."deletedAt" IS NULL))
AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c."deletedAt" IS NULL))
AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p."deletedAt" IS NULL))
+ AND (f.entity_type <> 'agent_session' OR ags.id IS NOT NULL)
ORDER BY f.sort_order ASC, f.created_at ASC
"#,
user_id.as_ref(),
diff --git a/crates/favorites/src/outbound/pg_favorites_repo/tests.rs b/crates/favorites/src/outbound/pg_favorites_repo/tests.rs
index 6adf1513fd..330ae8718e 100644
--- a/crates/favorites/src/outbound/pg_favorites_repo/tests.rs
+++ b/crates/favorites/src/outbound/pg_favorites_repo/tests.rs
@@ -195,6 +195,51 @@ async fn list_favorites_hydrates_uuid_keyed_entities(pool: PgPool) {
assert_eq!(favorites[1].channel_type, None);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn list_favorites_skips_agent_sessions_that_no_longer_exist(pool: PgPool) {
+ insert_user(&pool, USER_A).await;
+ let bot_id = Uuid::now_v7();
+ sqlx::query(
+ r#"INSERT INTO bots (id, kind, name, handle, has_agent, owner_user_id) VALUES ($1, 'owned', 'Coder', $2, true, $3)"#,
+ )
+ .bind(bot_id)
+ .bind(format!("coder-{}", bot_id.simple()))
+ .bind(USER_A)
+ .execute(&pool)
+ .await
+ .expect("bot should insert");
+ let live_session = Uuid::now_v7();
+ sqlx::query(
+ r#"
+ INSERT INTO agent_session (id, owner_id, bot_id, model, harness, repo_url, workspace)
+ VALUES ($1, $2, $3, 'claude', 'macrod', 'https://github.com/example/example', '/workspace')
+ "#,
+ )
+ .bind(live_session)
+ .bind(USER_A)
+ .bind(bot_id)
+ .execute(&pool)
+ .await
+ .expect("agent session should insert");
+
+ let repo = PgFavoritesRepo::new(pool);
+ for session_id in [live_session, Uuid::now_v7()] {
+ repo.add_favorite(
+ &user(USER_A),
+ &EntityType::AgentSession.with_entity_str(&session_id.to_string()),
+ )
+ .await
+ .expect("agent session favorite should insert");
+ }
+
+ let favorites = repo
+ .list_favorites(&user(USER_A), &FavoriteFilter::default())
+ .await
+ .expect("favorites should list");
+ assert_eq!(favorites.len(), 1);
+ assert_eq!(favorites[0].entity_id, live_session.to_string());
+}
+
async fn listed(repo: &PgFavoritesRepo, filter: &FavoriteFilter) -> Vec<(EntityType, String)> {
repo.list_favorites(&user(USER_A), filter)
.await
diff --git a/docs/agent-guide/navigation.md b/docs/agent-guide/navigation.md
index ef5b52d47e..407cad1674 100644
--- a/docs/agent-guide/navigation.md
+++ b/docs/agent-guide/navigation.md
@@ -600,8 +600,9 @@ uploading or moving anything.
## Favorites
Use an entity's command/context menu to add or remove it from Favorites. Documents, chats,
-projects, email threads, channels, calls, CRM companies, and CRM contacts support
-toggling. Individual channel messages are not favoritable.
+agent sessions, projects, email threads, channels, calls, CRM companies, and CRM
+contacts support toggling. Individual channel messages are not favoritable. A
+favorited agent session disappears from the list once the session is deleted.
With the GraphQL local cache enabled, cached favorites remain visible when offline
or when a background refresh fails. Toggle and reorder success while offline means
diff --git a/docs/guides/add-entity.md b/docs/guides/add-entity.md
index 60ecdf9c2a..529337f1e8 100644
--- a/docs/guides/add-entity.md
+++ b/docs/guides/add-entity.md
@@ -183,8 +183,11 @@ Postgres repo.
databases, and pipelines are not there yet, so their trash is never emptied.
- **Favorites (optional):** `crates/favorites/src/domain/mutation_service.rs`
`validate_favoritable` is exhaustive; recent kinds return unsupported. If you
- enable favorites, also update `crates/graphql_favorite` and clean up `favorite`
- rows on delete, since nothing else does.
+ enable favorites, join your table in `list_favorites`
+ (`crates/favorites/src/outbound/pg_favorites_repo.rs`) so a favorite of a
+ deleted entity stops listing, as documents, projects, and agent sessions do.
+ Nothing reaps the `favorite` rows themselves; a delete-time cleanup is still
+ worth adding when the kind is deleted in bulk.
## 6. Properties (optional)