#4495: Fix: emit final assistant event when reply tags hide stream
app: macos
scripts
agents
Cluster:
TUI and Tool Execution Fixes
## Problem
When reply_to tags appear mid-stream, cleaned text can rewind and onAgentEvent never emits the final assistant text. TUI then shows no realtime reply until reconnect/history refresh.
## Fix
Emit a final assistant event on message_end when the final cleaned text differs from the last streamed text.
## Testing
- pnpm vitest src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.subscribeembeddedpisession.test.ts
- CI: full matrix on this PR
## AI-assisted
- AI-assisted: Yes (OpenAI Codex)
- Testing level: lightly tested locally + CI
- Prompt summary: Fix TUI realtime reply missing when reply_to tags hide stream; ensure final assistant event is emitted on message_end.
- Understanding: This change compares the final cleaned text against the last streamed text and emits a final assistant event so UI consumers receive the complete reply after reply tag stripping.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes a TUI streaming edge case where `[[reply_to: …]]` directives can cause the cleaned/visible assistant text to “rewind” mid-stream and prevent downstream consumers from ever receiving the final visible assistant message.
The implementation adds a `message_end` fallback in `src/agents/pi-embedded-subscribe.handlers.messages.ts` to emit one last assistant agent event when the final cleaned text differs from what was last streamed, and adds a regression test covering the reply-tag-hidden scenario. The changelog is updated to reflect the user-facing TUI behavior change.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and addresses a real streaming/UI edge case with limited surface area.
- Changes are localized and add a targeted regression test. Main remaining risk is subtle normalization differences between streaming vs final parsing that could cause an extra final event (or miss one) in some whitespace/directive-boundary scenarios; no evidence of broader behavioral breakage beyond event emission timing.
- src/agents/pi-embedded-subscribe.handlers.messages.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#9220: Fix: TUI drops API responses silently when runID already finalized
by vishaltandale00 · 2026-02-05
82.4%
#23073: fix(ui): strip reply directive tags from assistant messages in WebC...
by x4v13r1120 · 2026-02-22
82.2%
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
81.7%
#12974: fix: intermittent (no output) reported by users
by vincentkoc · 2026-02-10
81.6%
#3721: fix(ui): webchat not displaying chat responses
by maxmaxrouge-rgb · 2026-01-29
80.8%
#6502: fix(tui): skip empty text for tool-only assistant turns
by douvy · 2026-02-01
80.6%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
80.3%
#15110: fix: enable auto-scroll during assistant response streaming
by jwchmodx · 2026-02-13
80.2%
#23144: fix(ui): strip reply directive tags from assistant messages in WebC...
by echoVic · 2026-02-22
79.7%
#8353: fix(ui): display tool calls during webchat streaming
by MarvinDontPanic · 2026-02-03
78.9%