Skip to content

refactor(tui): reduce AG-UI frames in the TUI over typed events - #1724

Merged
edenreich merged 1 commit into
mainfrom
refactor/issue-1713-tui-reducer
Nov 6, 2026
Merged

edenreich merged 1 commit into
mainfrom
refactor/issue-1713-tui-reducer

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

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.RunDecoder is gone. tui/runview.Reducer replaces it, built on agui.Decode, and the daemon chat uses it.

Changes

  • internal/presentation/tui/runview (new): Reducer folds a thread's AG-UI events into the chat events the TUI handlers render. Reduce takes a typed agui.Event, Decode takes a raw frame. The step reconstruction, the held completion and the mid-run attach tolerance are unchanged.
  • The hand-written frame structs (decodedFrame, decodedPatch) are deleted. The reducer switches on the SDK's event types through the agui aliases.
  • internal/presentation/headless/run_decoder.go and its test are deleted from headless, which now only writes the protocol.
  • agui.InterruptPlanApproval joins the other interrupt reasons in agui, replacing the unexported headless constant both sides needed.
  • The reducer reads the two activities feat(agui): report a cancelled tool call and conversation optimization #1722 added, so a daemon chat now shows a cancelled tool call and a conversation optimization.
  • tui/daemonthread.Agent uses 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

  • The reducer's round-trip tests against RunEncoder moved 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

  • no docs ticket: internal-only refactor

@edenreich
edenreich requested a review from a team as a code owner November 6, 2026 18:44
@github-project-automation github-project-automation Bot moved this to Todo in 2026 Nov 6, 2026
@edenreich
edenreich added this pull request to stack #1726 November 6, 2026 19:03
@edenreich
edenreich force-pushed the refactor/issue-1713-tui-reducer branch 4 times, most recently from db66c55 to 42fcf99 Compare November 6, 2026 22:31
Base automatically changed from feat/issue-1713-wire-gaps to main November 6, 2026 22:31
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
edenreich force-pushed the refactor/issue-1713-tui-reducer branch from 42fcf99 to ed437dc Compare November 6, 2026 22:31
@edenreich
edenreich merged commit 3d50e77 into main Nov 6, 2026
@edenreich
edenreich deleted the refactor/issue-1713-tui-reducer branch November 6, 2026 22:31
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
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.246.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Nov 7, 2026
@edenreich edenreich moved this from Todo to Done in 2026 Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant