fix(api): bound every Slack agent call by the run budget

Merged
AV
2mo ago
+356-62

Follow-up to #7581, closing the CodeRabbit finding on run-agent.ts:481 and the test-delay nit.

Summary

  • The 240s run budget now bounds Slack requests, MCP requests and the error rewrite, not only model calls, so a stalled dependency can't outlive the job route's 300s maxDuration and leave the user with no reply.
  • The deadline test controls Date.now instead of sleeping.

Why / Context

@slack/web-api 7.15.0 was constructed with no options: no request timeout and the default ten-retries-in-thirty-minutes policy. MCP requests used the SDK's flat 60s cap regardless of time left. formatErrorForSlack had its own 45s timeout plus a retry. Any of these could run the worker past 300s, at which point it is killed before posting, the execution claim stays pending, and the user only hears about it via the "lost track" notice on their next message.

How It Works

  • createSlackClient sets a 15s request timeout and two quick retries. Every Slack path in the agent now uses it, including thread-context and channel-history reads that previously built their own client.
  • runSlackAgent computes { timeout: remaining } per MCP request and passes it to listTools, the three context prefetches, and each tool call.
  • formatErrorForSlack(error, deadline) returns static copy when under 20s remain and otherwise caps its timeout to the remainder with no retries. Both callers pass the deadline.

Testing

  • bun test src/app/api/integrations/slack in apps/api: 48 pass (3 new: MCP timeouts bounded by the budget, rewrite skipped near the deadline, deadline exhaustion via controlled clock)
  • bunx tsc --noEmit -p apps/api/tsconfig.json: clean
  • bunx biome check apps/api/src/app/api/integrations/slack: clean
  • Not exercised against live Slack; the happy path is unchanged and the new bounds only fire on stalled requests.

Notes

  • createSlackClient is shared by the App Home, link-unfurl and interactions handlers, so they inherit the 15s timeout and short retries too. Those are short calls; the change only stops them hanging.
  • The agent test now pins its own ../slack-client mock: bun shares mock.module across files in one process, and the handler test's stub has no conversations.replies, which made the thread-context test fail only when the whole directory ran.

https://claude.ai/code/session_01YDDSQYG6WSZn4jXWTR3Pid


Summary by cubic

Every Slack and MCP call in the Slack agent is now bounded by the 240s run budget, not just model calls, so a stalled dependency can't outlive the route's 300s maxDuration and leave the user with no reply. The Slack client previously had no timeout and a 10-retry/30-minute policy, and MCP calls used a flat 60s cap.

What changed

  • createSlackClient applies a 15s cap and two quick retries, dropping retries once the budget can't absorb their backoff; a per-attempt check shrinks the timeout as the deadline nears and refuses requests that start after it.
  • Deadline-bound clients reject rate-limited calls instead of sleeping inside the SDK; the final reply retries once only when the wait fits the budget.
  • App Home, link-unfurl, and interactions handlers inherit these bounds, so they only stop hanging.
  • MCP listTools and tool calls use the remaining budget as their timeout instead of the SDK's flat 60s cap, and fail outright once the deadline has passed.
  • formatErrorForSlack takes the deadline, falls back to a generic message under 20s remaining, and no longer retries.
  • The deadline test controls Date.now instead of sleeping; new tests cover MCP timeouts, the rewrite cutoff, per-attempt request bounds, and rate-limit retries.
  • Not exercised against live Slack; the happy path is unchanged and the new bounds only fire on stalled requests.

Summary by CodeRabbit

  • Bug Fixes

    • Improved Slack error handling as agent execution approaches its time limit.
    • Applied remaining-time limits to Slack and tool requests to prevent operations from exceeding deadlines.
    • Added bounded request timeouts and more predictable retry behavior.
    • Added clearer fallback messages for general and rate-limit errors.
    • Slack final replies now retry once after rate limiting when time allows.
  • Tests

    • Expanded coverage for deadlines, timeouts, rate limits, retries, and fallback errors.