feat(api): Slack thread sessions — replies reach the agent without a mention

Merged
AV
2mo ago
+11549-15

Links

Summary

  • A thread the agent has replied in becomes a durable session. Replies in it reach the agent without an @-mention.
  • Each run records what the agent created, and the next turn reads it, so "spawn an agent on that workspace" resolves.
  • "Only respond when I mention you" quiets a thread. A direct mention still works there.
  • One new table and migration. Follow-ups are behind a per-team PostHog flag that fails closed.

Why / Context

Today every turn needs an @-mention and the agent has no memory of what it did a message ago. This is the first of four Phase 1 PRs; it introduces the thread-session record that the follow-up queue, plugin tools and completion callback build on.

How It Works

  • slack_thread_sessions (packages/db/src/schema/slack.ts): one row per team/channel/thread with status, quiet flag, the newest Slack ts the agent has read, an entity log of things created, and a queued_events column reserved for the next PR so it doesn't need a second migration.
  • Events route: a human, threaded, non-mention reply in a channel is forwarded to the mention job when threadFollowUpTarget finds an unquieted session and the team's slack-thread-follow-ups flag is on. A reply that mentions the bot is left to the app_mention path, which already dedupes on the execution claim. DMs are unchanged.
  • Handler: the quiet phrase short-circuits before any run; otherwise beginThreadRun upserts the row as running, its entity log is rendered into the agent's contextual system block, and finishThreadRun appends this run's actions (newest 30 kept) and marks it idle.
  • The mention job's payload schema now accepts type: "message" with a channel type.

Manual QA Checklist

Needs the flag on for the team (see Rollout).

  • Mention the bot in a channel, then reply in that thread without a mention: it answers
  • Ask it to create a task, then reply "spawn an agent on that": it uses the task it just made
  • Reply "only respond when I mention you": it confirms and stops answering unprompted; a later @-mention still works
  • A thread it was never in: replies are ignored
  • Flag off: behaviour identical to today
  • select * from slack_thread_sessions shows one row per thread, status back to idle after each run

Testing

  • bun test src/app/api/integrations/slack in apps/api: 67 pass (11 new across route, handler and agent)
  • bunx tsc --noEmit for apps/api, packages/db, packages/shared: clean
  • bunx biome check on changed paths: clean
  • Migration generated with drizzle-kit generate; not applied locally because this worktree's Neon credentials are stale. The preview deploy applies it on its own branch.

Rollout

  • Create the PostHog flag slack-thread-follow-ups and enable it for distinct id slack-team:<Superset team id> first, then widen. Off or missing means mention-only, exactly today's behaviour.
  • Rollback: revert; the table is inert if unread.

https://claude.ai/code/session_01YDDSQYG6WSZn4jXWTR3Pid


Summary by cubic

Previously every Slack reply needed an @-mention and the agent had no memory of what it had just created; now a thread the agent has replied in becomes a durable session, later replies reach it without a mention, each run records what it created so a later "that workspace" resolves, and "only respond when I mention you" quiets a thread while a direct mention still works. The feature is gated by the per-team slack-thread-follow-ups PostHog flag.

Behavior

  • Sessions are scoped by organization as well as team, channel, and thread, so a Slack team reconnected by another organization doesn't inherit the prior entity log.
  • Thread memory is rendered into the user turn as a marked data block, never into the system prompt, because labels are user-chosen text.
  • A thread reply that mentions the bot is still handled by the app_mention path, which already dedupes on the execution claim; broadcast replies to the channel still count as thread replies.

Rollout

  • Create slack-thread-follow-ups and enable it for slack-team:<team id> before widening; off or missing keeps today's mention-only behavior.
  • The slack_thread_sessions migration is applied by the preview deploy; rollback is a revert and the table is inert if unread.

Summary by CodeRabbit

  • New Features

    • Slack agents can continue conversations in existing threads without requiring an @mention when enabled for the team.
    • Thread context and previously created items are retained to improve follow-up responses.
    • Users can ask the agent to remain quiet in a thread.
    • Slack message handling supports replies across channels, group conversations, and direct messages.
  • Bug Fixes

    • Improved filtering prevents automated or irrelevant Slack events from triggering agent responses.
    • Thread replies are routed consistently through the appropriate processing flow.