feat(api): !stop ends the Slack agent's running turn

Merged
AV
2mo ago
+11116-8

Summary

  • !stop (or !cancel) in a thread or DM ends the agent's running turn. The reply is "Stopping.", then the turn posts "Stopped. Anything I completed before that is listed below." with its Changes footer. With nothing running: "Nothing is running in this thread."
  • Deleting the "Working..." placeholder never did anything; the job runs on the API until it finishes or hits its deadline. This is the way to end it.

How It Works

  • !stop upserts stop_requested_at on the thread session, stamped with the stop message's own Slack timestamp, so it works before the thread's first session row exists and while a turn is still in preflight.
  • A turn's claim keeps a stop request only if it is newer than the message that started the turn; older ones were aimed at an earlier turn and are cleared. So !stop followed by a fresh mention does not stop the fresh one.
  • The agent loop checks the flag before each model request and before each tool call, comparing against the running turn's message. A set flag ends the turn through the same static-copy path as the time limit, so completed actions are kept and reported, and the placeholder and πŸ‘€ are cleared.
  • Cost: one small row read per step, at most a few dozen per turn.

Migration

0118_slack_thread_stop_requested: one nullable column. PR D (#7589) also generates an 0118; whichever merges second regenerates its migration.

Manual QA (Superset workspace)

  • Slow request β†’ while πŸ‘€ is on, reply !stop β†’ "Stopping.", then the stopped reply within a step, placeholder gone, πŸ‘€ cleared
  • Same in a DM
  • !stop in an idle thread β†’ "Nothing is running in this thread."; a mention right after runs normally
  • Two-tool request, !stop during the first tool β†’ second tool never runs, footer lists the first

Testing

  • bun test in apps/api (169 pass) and packages/db (13 pass)
  • bunx tsc --noEmit --pretty false in apps/api and packages/db (exit 0)
  • bunx biome check apps packages
  • The claim's ordering SQL and the upsert verified against a local Postgres

https://claude.ai/code/session_01YDDSQYG6WSZn4jXWTR3Pid

Summary by CodeRabbit

  • New Features

    • Added !stop and !cancel commands to stop active Slack agent runs.
    • Stop commands are also recognized in direct messages.
    • Stop requests apply based on message timing, including requests sent before a run begins.
    • Added clear responses indicating whether a run was stopped or none was active.
  • Bug Fixes

    • Prevented additional model requests and tool actions after a stop request is detected.
    • Direct-message quieting commands remain ordinary text and do not disable agent responses.