Repository navigation
fix(messaging): stop a Slack or Telegram turn that makes no progress - #5382
Conversation
Both bridges answer one message at a time and waited for each answer with no deadline, so one agent that went quiet while still alive blocked every later chat on the bridge. Slack (AgentChannel): a turn with no progress for 75 minutes is ended for the user and the agent is sent a cancel, mirroring the Agent UI's rule: a running tool or a pending approval is not idle. If the agent does not end the turn within 30 s it is shut down, and queued and later messages get an error naming the restart instead of waiting. Late events from a stopped turn are not rendered. Telegram: a reply that streams nothing new for 75 minutes is stopped, the partial text is kept with a note, and the stream is closed when it next yields. A second message from the same user while the stopped stream is still unwinding is told to wait instead of racing it on the same session.
… a Telegram user - Slack: the reader claims a turn's answer before rendering it, so a stop racing the answer cannot render over it; a message refused after the child is shut down is never made the active turn; the stop message no longer promises partial text the Slack reply replaces. - Telegram: a stopped reply drops the user's session so the next message starts fresh, instead of a busy guard that locked the user out for good when the stream never yielded again; a failing stream close is logged.
|
Verdict: Approve with suggestions Before this PR, a Slack or Telegram reply that went quiet without ending held every later message on that bridge forever. Now a reply that makes no progress for 75 minutes is stopped and the user is told why. On Slack, an agent that ignores the stop is shut down, and later messages say to restart the bridge. I found no blocking issues. The races I looked for are covered: a stop landing at the same moment as a real answer, late output from a stopped reply, and output the shut-down agent flushes late. The Slack and Telegram guides match what the code does. Two small optional cleanups are below. Real-world evidenceN/A: no evidence bundle was produced. Running this for real would mean holding a live Slack or Telegram reply silent for 75 minutes. The verdict rests on static review and the PR's fake-clock unit tests, which the author reports passing (243 passed). I could not re-run them here because pytest isn't installed in this review environment. 🔍 Technical detailsIssues found 🟢 Child isn't reaped after the forced kill ( 🟢 Checked, not an issue
Strengths
|
…gram turn late Review follow-ups. - Telegram: a turn stopped before its thread ran, or while its session was being made, sent the old question once it got going, inside the session the next message would use. The thread now checks the stop before making the session and again before sending, and only a session the stopped turn actually used is dropped. - Slack: one wedged agent took the bridge down until a restart, and the restart hint named a command that needs --allowed-users and would open a second connection beside the running bridge. A child that ignores a stop is now replaced, up to three times per bridge, and logged; output its killed predecessor flushes late is dropped. After that, each message says to stop the bridge and how to start it again. - GAIA_STREAM_TIMEOUT sets the no-progress window for both bridges, as it does for the Agent UI. - A killed child is waited on, so none is left behind.
Two tests imported pytest inside the function body; the module now imports it with the rest.
…nd close the restart races Round-2 review follow-ups. - Closing the bridge while a wedged agent was being replaced left the new child running. The replacement checks for close after it starts and stops the child it made, and close() stops whichever child is current. - A stopped turn was told to send "continue" even when the agent was then replaced and had lost the conversation. The message is now decided once the outcome is known: "continue" when the agent acknowledged the stop, "restarted, send your message again" when it was replaced. Anyone else the old agent had talked to gets a note on their next answer. - The restart budget never reset. It is now three restarts per 24 hours. - A late line from a replaced child could still be matched to the next turn between the identity check and reading the active turn; both now happen under one lock. - The no-progress window is read through one shared gaia.stream_timeout, also used by the Agent UI. `gaia slack start` and `gaia telegram start` turn a bad GAIA_STREAM_TIMEOUT into a clean error, and the Slack restart hint keeps --background when the bridge was started that way. - Tests cover each of these and the earlier fixes: late output from a replaced child whose reader is still alive, the terminal claim during a slow render, dropped late tokens, terminate-then-kill, the default budget, Telegram's GAIA_STREAM_TIMEOUT, and the Slack hint wiring.
|
🟡 When a stuck Slack agent is restarted, the user will probably get the wrong message. Before the replacement starts, the old agent's output closes. The bridge reads that as a crash, so the user is told "it most likely crashed, check the logs, resend" and never hears that the agent was restarted and lost the conversation. The restart message the last commit added would then be dropped, because the turn has already ended. The new test misses this: its fake process exits instantly and has no 🔍 Technical details
One fix: under Test: give |
…rash While the stuck agent was being shut down, its output closed before the replacement took over, so the bridge read that as a crash and the user got "it most likely crashed" instead of the restart message. The old child is now marked as retiring before it is stopped, and nothing it sends after that, including its end of stream, can end the turn.
…imit (#5456) Three fixes on main could be reverted with every test still passing. That covers a scheduled email marked failed while it is still sending (#5380), the Slack/Telegram notice after an agent restart telling users to "continue" a conversation the agent forgot (#5382), and nothing proving a hung tool under the messaging bridge ever ends. Each new test fails on the user-visible symptom when its fix is undone. - **Scheduler lease, both stores.** Two claims with the same timestamp are told apart only by `claim_id`. Without that half of the recovery guard, a stale read fails a live send or re-queues a running job. - **Restart notice.** The existing check only caught a quoted `"continue"`. An unquoted "Send continue" that also dropped "no longer remembers this conversation" passed everything. - **Hung tool under the bridge.** A running tool keeps the bridge's idle clock fresh, so only the agent's own tool limit (`GAIA_AGENT_TOOL_TIMEOUT`, 180 s) ends it. This drives the real `gaia-agent` turn loop behind a real `AgentChannel` and checks the user still gets an answer. It needs the flagship wheel, so it runs in the gaia-agent lane. <details> <summary>🔍 Technical details</summary> Reverts run locally, each against the new test: | Revert | Result | |---|---| | Drop `claim_id = :claim` from `recover_claim` (daemon + email store) | 4 fail: status `failed`/`pending` != `firing` | | Restart notice → `restarted. Send continue to pick up where it left off.` | new test fails; all existing messaging tests pass | | Restart notice → `... Send "continue" to pick up where it left off.` | new test fails (existing test also catches this one) | | `_call_tool_bounded` timeout → `inf` | fails: "the hung tool was never ended", user stuck at the `tool_call` | | Bridge idle refresh ignores `tool_running` | fails: `error` != `final` | The email schedule tests skip from the repo root unless `gaia_agent_email` is installed, because the root conftest only redirects hub packages that are already installed. CI does run them: `test_email_agent_unit.yml` installs the wheel and gates wholesale skips. Main push run 39538913521 shows all 13 stale-firing tests and 16 `test_email_schedule.py` tests PASSED. </details> ## Test plan - [x] `python -m pytest tests/unit/test_daemon_scheduler_stale_firing.py tests/unit/messaging/ -q`: passes locally and in Unit Tests run 39541345649. That run executed the new daemon and restart tests and skipped `test_bridge_hung_tool.py` as allowlisted. - [x] `python -m pytest hub/agents/email/python/tests/test_email_schedule_stale_firing.py -q` with `gaia_agent_email` installed: passes locally, and both new cases (`scheduled_send`, `snooze`) passed in Email Agent Unit Tests run 39541345667. - [x] `python util/check_test_lane_coverage.py` passes (run locally). - [x] CI: Gaia Agent Tests run 39541345605 executed `test_bridge_hung_tool.py` and it passed (not skipped). --------- Co-authored-by: Kalin Ovtcharov <[email protected]>
The Slack and Telegram bridges answer one message at a time and waited for each answer with no deadline. If the agent went quiet while still running, every later message on the bridge waited behind it forever. Now a turn that makes no progress for 75 minutes is stopped. A running tool or an approval waiting on you doesn't count as no progress, and
GAIA_STREAM_TIMEOUTchanges the window, as in the Agent UI (#5200). If the agent acknowledges the stop, the user is told and can send "continue". If it doesn't, the agent is restarted: the user is told the conversation was reset, and anyone else it was talking to sees a note on their next answer. After three restarts in a day, each message says how to restart the bridge. On Telegram the partial reply is kept, and a stopped turn never sends its question later.Reproduced by
tests/unit/messaging/test_bridge_turn_idle.py,tests/unit/messaging/test_bridge_restart.pyand the Telegram teststest_a_reply_that_stops_streaming_is_stopped_and_frees_the_botandtest_a_turn_stopped_before_its_thread_ran_never_sends_later; they fail on main and pass here.🔍 Technical details
AgentChanneltakes:idle_limit: fromGAIA_STREAM_TIMEOUT, else 4500 s, read through the new sharedgaia.stream_timeoutthat the Agent UI now uses too;cancel_grace: 30 s;max_restartsandrestart_window: 3 per 24 h;restart_hint,clockandpoll_seconds.Between
tool_callandtool_result, and while aneeds_confirmationis pending, the idle clock does not run. Both have deadlines of their own: the tool timeout, and Slack's 10-minute approval deny.When a turn goes idle:
cancelis sent;If the child does not end the turn within the grace period, it is terminated, waited on, and killed if needed, then respawned. The stopped user gets the "reset" message, and
RESTART_NOTICEis put in front of the next answer for every other sender the old child had served.Children and turns are swapped under one lock. A reader drops lines once its child has been replaced, and matches every line to the turn current for that child. Closing during a respawn stops the new child, and
close()stops whichever child is current.Once the budget is used up, queued turns get an error naming the restart without becoming the active turn. Slack's hint,
slack_restart_hint(background), is "Ctrl+C, thengaia slack start --allowed-users …", or "gaia slack stop, then …--background" when the bridge was started in the background.gaia slack startandgaia telegram startreport a badGAIA_STREAM_TIMEOUTas a clean error.Telegram uses
wait_for(queue.get(), idle_limit). The generation thread checks the stop before it creates the session and again before it sends. On a stop, only the session the turn actually used is dropped.Each stuck Telegram generation keeps one default-executor thread until its model call ends. That pool is bounded; the LLM layer's own stall timeout is what frees them.
Docs:
guides/slack.mdxandguides/telegram-adapter.mdx. There is no messaging CHANGELOG to update.Test plan
python -m pytest tests/unit/messaging tests/unit/test_telegram_adapter.py tests/unit/test_telegram_allowlist.py tests/unit/test_stream_timeout_env.py tests/unit/chat/ui/test_turn_idle_limit.py: 295 passed, three runs in a row. The bridge tests run on a fake clock against fake children.Each fix was reverted by mutation and its test failed:
_stop_child, the terminate-then-kill test fails;Against the previous head, the close-during-respawn, reset-message, rolling-budget and default-budget tests fail.
python util/lint.py --black --isort --flake8 --pylint --test-lanes