fix(api): post the Slack agent's reply after its run budget is spent
Problem
When a Slack agent run uses its whole 240s budget, the user gets nothing back. No "ran out of time" message, no error message. The "Thinking..." placeholder and the 👀 reaction stay on the thread forever. The job returns a 500. In the production case, QStash had already re-sent the job about two minutes into the run. That retry found the first run's claim still unfinished, treated it as a duplicate, and exited. Once the first run fails, it marks the delivery failed, so nothing ever retries it.
This happened in production today: a model call timed out right at the budget edge.
Root cause
The handler sent every Slack call through run, a client bound to the run deadline. Since #7582, that client's request interceptor refuses to start any request once the deadline has passed.
A run that hits the budget always finishes after the deadline. The agent only returns its time-limit reply (or a model call only times out) once the deadline is reached. So the one message written for this case could never be sent. The production logs for that request show the same sequence:
- The final reply is refused.
- The handler's catch tries to post the fallback error, which is also refused. That error escapes the handler.
- The
finallycleanup (placeholder delete, reaction removal) is refused too.
The existing tests missed this because the Slack client mock ignored the deadline option.
Fix
The reply now goes through a second client, reply. It is bounded by the run deadline plus REPLY_BUDGET_MS (45s), which still ends inside the job route's 300s maxDuration. That keeps #7582's guarantee that no Slack call outlives the worker.
These go through reply: the final reply, its rate-limit wait check, the side-effects summary, the error reply, placeholder/status cleanup, reaction removal, and the stale-claim and mute/unmute confirmations.
These stay on the run budget: work and progress (showProgress, the 👀 add, image preflight, mention resolution, and everything inside runSlackAgent).
The Slack client mock in the handler test now refuses calls after its deadline, like the real interceptor. The new test moves Date.now past the run deadline inside the agent call and asserts that the reply is posted, the placeholder is deleted, the reaction is removed, and the delivery is marked succeeded.
Known limit, not addressed here
runSlackAgent gives its first model call maxRetries: 1 whenever more than 180s remain, and the Anthropic SDK (0.78.0, makeRequest) retries a timed-out attempt with the same timeout. That call can therefore run up to ~60s past the run deadline, which is more than the 45s reply budget. In the production event the overrun was a few seconds. A 429/5xx retry is worse, because it sleeps for Retry-After before the second attempt, and that can reach past the route's 300s limit. Either way this is a separate defect in run-agent.ts: until it is fixed, those runs still lose their reply and this Sentry error can still fire.
A slow or rate-limited reply can now run until about 285s. That leaves the finally work (finishThreadRun, the hand-back publish, finishAgentDelivery) the last ~15s instead of ~60s. #7592 touches that finally block and handBackQueued, so whichever PR lands second needs a rebase.
Verification
Before the fix, the new test fails with the same sequence as production (final post refused at the reply loop, then the error post refused in the catch):
(fail) a run that spends its whole budget still posts its reply and clears its indicators
29 pass
1 fail
Ran 30 tests across 1 file.
After:
$ bunx biome check apps/api/src/app/api/integrations/slack/events/process-agent-message/process-agent-message.ts apps/api/src/app/api/integrations/slack/events/process-agent-message/process-agent-message.test.ts
Checked 2 files in 10ms. No fixes applied.
$ cd apps/api && bun run typecheck
$ tsc --noEmit
(exit 0)
$ cd apps/api && bun test src/app/api/integrations/slack
94 pass
0 fail
Ran 94 tests across 9 files.
Refs API-2X