Repository navigation
Channels: send autonomous turns to the channel that last messaged the session - #486
Open
niklassemmler wants to merge 1 commit into
Open
niklassemmler wants to merge 1 commit into
niklassemmler wants to merge 1 commit into
Conversation
… session When a background task (Bash/Agent run_in_background, Monitor) settles, the CLI runs an autonomous turn. The engine's idle-stream watcher drains it to the broadcaster, which reaches the web UI. A channel's stream adapter, however, is registered only for the run of the inbound message it answers and is torn down when that run ends. On Telegram, everything the agent said in an autonomous turn was therefore silently dropped: progress updates, results, and answers to the question that started the background work. The router now remembers which channel and target last messaged each session. For each autonomous turn it opens a stream adapter there under its own listener id, and closes it after the turn's "done". Nothing changes for sessions that were never messaged through a router channel (web UI, cron, workflow runs), or when a user run already streams to the same target. An autonomous turn that produced no text deletes its placeholder instead of sending "(no response)", and a turn cut short still delivers the text that arrived before it stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
MAGI (agent of Niklas Semmler) seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Author
|
Full suite on this branch: 3943 passed. |
Author
|
Follow-up gap I found while deploying this, left out of this PR on purpose:
Both could be fixed together by seeding the router's session-to-target mapping from the session's persisted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem to be solved:
On Telegram, nothing the agent says in an autonomous turn reaches the user. An autonomous turn is the full turn the CLI runs when a background task settles (
run_in_backgroundBash or Agent, Monitor). Typical case: the user asks for something, the agent starts a background job and answers "on it". The job finishes, and the agent's follow-up (results, a PR link, the actual answer) appears only in the web UI. In one session this hid five consecutive messages, about an hour of output, until the user asked whether the agent was still working.The cause is in how output is routed.
ChannelRouter._run_singleregisters aStreamAdapteras a broadcaster listener for the run of the inbound message it answers, and_teardown_streamingunregisters it when that run ends. Autonomous turns arrive later, through the idle-stream watcher (_drain_pending_messages). They are broadcast normally, so the web UI's WebSocket listener sees them, but by then no channel listener is registered.telegram.pyhas no other path for them.Changes:
ChannelRouterrecords the(channel, target)that last messaged each session.open_autonomous_stream(session_id)/close_autonomous_stream(session_id):StreamAdapterfor that target under its own listener id (<channel>:<target>:autonomous), so closing it never unregisters a user run's adapter.run(), which reaches the channel through the run's own adapter.done(cancelled, failed), close sends whatever text arrived._drain_pending_messagesopens the stream when an autonomous turn opens and closes it after the turn is finalized, once per turn. It also closes it infinallyfor turns cut short. Errors from the hooks are logged and never break the drain. Engines without a router skip the hooks.StreamAdaptergets two changes:send_empty=False, used for autonomous turns: an empty turn deletes its placeholder instead of sending(no response).finishedguard, so a backstopdoneafter the real one can't send the message twice.Testing:
tests/test_router_autonomous.pyruns the router against a realStreamBroadcasterand a fake streaming channel. It covers delivery to the last inbound target, sessions never messaged through a channel, empty turns, turns cut short, duplicatedone, an active user-run adapter, idempotency, and listener-id isolation.tests/test_autonomous_turns.pycover open, then finalize, then close order; one stream per turn when several turns drain in one call; empty turns; timeouts; cancellation mid-turn; and engines without a router.finally, no inbound record, nofinishedguard,send_empty=True, a shared listener id, no user-run check, no flush on close.Not in scope:
_last_inboundis in memory only. That's fine, because a restart also kills the CLI subprocess and any background tasks that could produce an autonomous turn.run()still share the user run's adapter, exactly as before this PR.🤖 Generated with Claude Code