Repository navigation
refactor(tui): reduce AG-UI frames in the TUI over typed events - #1724
Merged
Merged
Conversation
edenreich
force-pushed
the
refactor/issue-1713-tui-reducer
branch
4 times, most recently
from
November 6, 2026 22:31
db66c55 to
42fcf99
Compare
The decoder that turns a thread's frames back into what the chat TUI renders lived in the headless package and read the wire through its own JSON structs. It is now tui/runview.Reducer, built on agui.Decode: it switches on the typed events, and the client side of the protocol sits with the client. It also reads the two activities the stream gained, so a daemon chat shows a cancelled tool call and a conversation optimization.
edenreich
force-pushed
the
refactor/issue-1713-tui-reducer
branch
from
November 6, 2026 22:31
42fcf99 to
ed437dc
Compare
edenreich
added a commit
that referenced
this pull request
Nov 6, 2026
…#1725) Part of #1713. Stacked on #1724. ## Summary A local chat can now render its agent runs through AG-UI, the path a daemon chat takes, behind an opt-in. With `chat.agui_loopback: true` (`INFER_CHAT_AGUI_LOOPBACK=true`) the in-process agent is wrapped: each run is written by the `RunEncoder` the serve worker uses and read back by `tui/runview.Reducer`. The default is unchanged. ## Changes - `internal/presentation/tui/loopback` (new): `Agent` decorates the in-process `AgentService`. `RunWithStream` pipes the agent's events through the encoder into the reducer, in memory and in the same goroutine as the encoder, one frame at a time. A suspended run is resumed with the user's answer to its approval or question. Everything else (cancel, metrics, system prompt, reasoning effort) goes straight to the agent. - A plan skips the run. The agent does not wait on it and the chat restarts the run itself, so the event goes straight to the chat's existing direct path. It can become an interrupt once the handlers stop deciding plans. - The encoder gets the background task registry's `Snapshot`, like the serve worker. That patch is what hands a step's tool results to the reducer. - `chat.agui_loopback` is a config key, read through the usual config layers and listed in `docs/configuration-reference.md`. - `container.NewLoopbackChatContainer` wraps the agent, and `infer chat` picks it when the opt-in is set. - e2e: `TestMain` reads the opt-in before it drops the caller's `INFER_*` variables and the local chat launches pass it on, so `INFER_CHAT_AGUI_LOOPBACK=true task test:e2e` runs the same local chat tests through the new path. - New e2e tests: an accepted plan runs, the status shows the model is working after a tool, and a background job followed through a chat (finishing while idle, finishing during a reply, and in a daemon chat). - The e2e `go test` timeout goes from 5 to 8 minutes in `ci.yml` and the Taskfile. The suite took 208s in CI and these tests add about 70s. ## Plan change The plan had a separate step that made the serve loop run over any reader and writer, so a local thread could host it. The decorator needs no turn loop at all: it wraps the agent's event channel. That step is dropped. ## Verification - `go test -race ./...`, and the full e2e suite with `-race` on both paths, at the top of the stack. - The accepted plan and working status tests fail against this PR's first commit under the opt-in and pass on the default path. - The background job tests pass on `main`, on this stack and under the opt-in. - A one-off sweep with tmux and tokenless compared final screens on `main`, this stack and the opt-in. Question form, Esc cancel, queued message, `!command`, todos, reasoning, provider error, auto-approve, plan reject and plan accept-standard all matched. ## Known gaps To close before the loopback can become the default: - An unanswered prompt holds the run open, because the loopback has no cancel path while the encoder is suspended. After the 5 minute approval timeout the direct path shows an error, while here the prompt stays open and the error arrives after the answer. - CI never runs with the opt-in on, so nothing guards this path between manual runs. - The sweep's flows have no permanent test under the opt-in, since CI's e2e budget is tight. - A run that hits max turns should end as a sticky error here and complete on the direct path. This comes from reading the code and has not been run. `TestChatTUIApprovalBoxFollowsTail` passes only 1 run in 8 under the opt-in on this PR alone. #1727 closes that. ## Downstream - no docs ticket: internal-only refactor
Contributor
|
🎉 This PR is included in version 0.246.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Part of #1713. Stacked on #1722 (which stacks on #1721 and #1719).
Summary
Phase 3 of the plan: the client side of the AG-UI stream moves into the TUI and onto typed events.
headless.RunDecoderis gone.tui/runview.Reducerreplaces it, built onagui.Decode, and the daemon chat uses it.Changes
internal/presentation/tui/runview(new):Reducerfolds a thread's AG-UI events into the chat events the TUI handlers render.Reducetakes a typedagui.Event,Decodetakes a raw frame. The step reconstruction, the held completion and the mid-run attach tolerance are unchanged.decodedFrame,decodedPatch) are deleted. The reducer switches on the SDK's event types through theaguialiases.internal/presentation/headless/run_decoder.goand its test are deleted fromheadless, which now only writes the protocol.agui.InterruptPlanApprovaljoins the other interrupt reasons inagui, replacing the unexportedheadlessconstant both sides needed.tui/daemonthread.Agentuses the reducer. Nothing else in the TUI changes.Deviation from the plan
The plan had the reducer return
tui.*messages directly and bypass the handlers for daemon chats. That would leave two run renderers in the TUI (handlers for local chats, reducer for daemon chats) until the last phase. The reducer keeps returning the chat events the handlers already take, so there is one renderer at every step. The handlers stop depending on agent-owned event types in the last phase, once local chats also arrive as frames.Verification
RunEncodermoved with it and pass, with one new case for the two activities.go test ./...,task precommit:run(0 issues) and the e2e suite with-race(ok tests/e2e 168s), which covers a daemon chat turn, reattach, direct commands and teleport.Downstream