Skip to content

fix(ui): stabilize streaming text order in TUI#47

Open
townwish4git wants to merge 1 commit intovigo999:refactor-arch-4.31from
townwish4git:refactor-arch-4.31-b-03271509-streaming-output
Open

fix(ui): stabilize streaming text order in TUI#47
townwish4git wants to merge 1 commit intovigo999:refactor-arch-4.31from
townwish4git:refactor-arch-4.31-b-03271509-streaming-output

Conversation

@townwish4git
Copy link
Copy Markdown
Collaborator

PR Description

背景 / 问题

bcf6bcb5-f2e0-4d9e-8914-acb033e7e8a5

当前 TUI 在 agent 流式输出时,文本会出现阶段性乱序/错位;
在输出结束瞬间又会“恢复正确顺序”,影响可读性与信任感。

复现现象:

  1. 流式输出过程中语序经常跳动、错位
  2. 输出完成后文本又被整理为正常顺序

根因分析

UI 事件订阅存在重复注册路径,导致 eventCh 在某些时机可能出现并发 reader,
流式 AgentReplyDelta 的消费顺序不稳定,进而出现显示错序。

同时,interrupt 结束路径缺少统一终止收口,可能放大流式状态残留问题。

方案

1) 消除重复订阅(核心修复)

  • ui/app.go
  • Update(tea.KeyMsg) 分支不再强制包装 waitForEvent
  • 保留 handleEvent() 尾部的单一路径持续订阅

效果:保证同一时刻仅有一个稳定事件读取循环,流式 delta 顺序不再被并发读取打乱。

2) 中断收口补齐(配套)

  • internal/app/run.go

  • context.Canceled 路径显式 emit TaskDone

  • ui/app.go

  • TaskDone 时提交当前 streaming agent message(结束流式态)

效果:中断后不会把下一轮流式内容接到上一条未封口消息尾部。

测试

新增/更新测试覆盖:

  • interrupt 后必须收到 TaskDone,且无 ToolError
  • TaskDone 可触发 queued input 自动派发
  • TaskDone 后下一轮流式从新消息开始,不拼接旧消息
  • key update 不再强制 event re-subscribe

执行:

  • go test ./internal/app -run Interrupt
  • go test ./ui -run 'Test(TaskDoneDispatchesQueuedInputWhenTrainNotBusy|KeyUpdateDoesNotForceEventResubscribe| TaskDoneCommitsStreamingAgentBeforeNextTurn|CtrlCSendsInterruptTokenForActiveTask|BusyTrainQueuesInputInBannerInsteadOfChatStream)'
  • go test ./internal/app ./ui/...

全部通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant