Skip to content

Slack: scope chat session to thread, not channel #903

Closed

Description

@pavshulin

Summary

A Slack chat session in Trinity is currently scoped to team_id : sender_id : channel_id (SlackAdapter.get_session_identifier, slack_adapter.py:44). The thread (thread_ts) is not part of the key. As a result, every @mention and every thread reply from the same user in the same channel share one channel-wide conversation transcript. Two concurrent threads in one channel cross-contaminate each other's context, and a fresh @mention does not start a fresh conversation — it inherits the channel's last-10-turn transcript.

This issue scopes the session to the thread so that a new top-level @mention starts a fresh chat and each thread continues only its own context.

Context

Surfaced while reviewing how Slack messages reach the agent. The router (ChannelMessageRouter) builds the agent prompt via db.build_public_chat_context(session_id, ...), which replays the last 10 turns for session_id as a plain-text transcript — there is no persistent Claude Code session, so the channel-scoped key is the only thing defining "the conversation."

Concrete failure: a user with two open threads in one channel —

Thread A: "summarize the Q3 report"
Thread B: "what's the weather in Tokyo"
Thread A: "translate it to French"   ← prompt contains the Tokyo exchange

The agent sees both threads interleaved chronologically, with no thread attribution.

Slack already gives us a stable per-thread id for free: a top-level @mention has no thread_ts, so _parse_mention sets thread_id = event.ts (a unique new value); every reply under it carries that same root thread_ts. So thread-scoping is mostly a key change.

Desired behavior

  • A new top-level @mention → fresh conversation (empty context).
  • A reply within a thread (mention or not) → continues that thread's own context only.
  • A re-@mention inside an existing thread → continues that thread (not a new chat).
  • DMs are unaffected — they have no threads and stay one continuous per-user conversation.

Acceptance Criteria

  • SlackAdapter.get_session_identifier scopes channel messages to the thread: team_id : channel_id : thread_id (drop sender_id for channel threads so multi-participant threads share one context).
  • DMs keep the existing team_id : sender_id : channel_id key.
  • A new top-level @mention resolves to a brand-new session_id → build_context_prompt returns empty history.
  • Two concurrent threads in the same channel resolve to distinct session_ids — no transcript cross-talk.
  • A thread reply and a re-@mention inside the same thread resolve to that thread's session_id.
  • Unit test covering thread-vs-mention session isolation and DM continuity.
  • Slack memory file / feature-flow note updated to record the session-granularity change.

Technical Notes

Primary change is contained to src/backend/adapters/slack_adapter.py:

def get_session_identifier(self, message: NormalizedMessage) -> str:
    team_id = message.metadata.get("team_id", "unknown")
    is_dm = message.metadata.get("is_dm", False)
    if is_dm:
        return f"{team_id}:{message.sender_id}:{message.channel_id}"
    return f"{team_id}:{message.channel_id}:{message.thread_id}"
  • No DB schema change — public_chat_sessions is keyed by the opaque session identifier already.
  • max_turns=10 in build_context_prompt now caps history within a thread, which is the intended behavior.
  • Out of scope: passive "observe mode" (bot absorbing non-mention thread messages into context without replying). The bot currently responds to every reply in a bot-active thread (_parse_thread_reply). Absorbing-without-replying is a separate, larger feature ([NO_REPLY] is the existing primitive).
  • Group chats (is_group) are a separate code path that already uses fresh context — unaffected.

Activity

  1. added
    theme-channels
    on Jun 3, 2026
  2. added
    complexity-medium
    status-ready
    on Jun 12, 2026
  3. AndriiPasternak31 commented on Jul 4, 2026

    @AndriiPasternak31
    Contributor

    /claim

  4. added
    status-in-progress
    and removed
    status-ready
    on Jul 4, 2026
  5. github-actions commented on Jul 4, 2026

    @github-actions

    @AndriiPasternak31 This issue is already assigned to AndriiPasternak31. Use /unclaim first if you want to take over.

  6. added
    status-in-dev
    and removed
    status-in-progress
    on Jul 6, 2026
  7. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

complexity-mediumpriority-p2theme-channelstype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions