← Back to Pull requests

Chat: the answer after a reply's last tool shows when its deltas didn't arrive (bugfix) #550

Closed
unknown closed this pull request without merging on Nov 27, 2026 11:27 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 10:36 UTC
Author

What and why

A reply that runs tools and then answers can lose that answer in Kyllene. The text before and between the tools shows, but the response after the last tool doesn't, when its message.delta frames never reach the app. This happens with a non-streaming provider, deltas that fall in a gap the replay ring can't fill, or a turn that ends while the chat is out of sight. The gateway sends that response again in message.complete text, but Kyllene threw it away.

Fixes #546

Cause: ChatReducer.kt:234 (on master) is text = base.text.ifBlank { finalText }. It uses the completion text only when nothing streamed at all. Once any earlier segment has streamed, the final response from message.complete is dropped.

Fix: follows what message.complete text means in hermes-agent. From v0.22.0 to v0.23.4 it is the last model response, the text after the last tool (_turn_outcome in tui_gateway/prompt_turn.py; on the session authority, the settled response). Hermes Desktop merges it the same way, as the current response after the last tool row.

  • ChatState.responseStart records where the running reply's current response begins. It is set at tool.start, at a non-streamed message.interim, and when a reply opens. It is cleared when the turn ends.

  • For a complete turn, withFinalResponse merges the completion text into that current response:

  • - Already streamed: stays as it is.
    - Partly streamed: completed.
    - Didn't stream: appended after the earlier segments, so it shows once.
    - response_previewed / response_reused: not added a second time.
    - response_transformed: replaces what streamed of it.
    - A cumulative final that repeats the earlier segments: only the new part is added.
    - With no known boundary (a reply rebuilt from history, or v0.22.0's shape with no segment stamps): still added only once, as the last paragraph.
  • Turns that are interrupted or error keep today's behaviour, because their text can be the whole reply kept so far.

  • No new request params, no new gateway calls, and no fallback. This only changes how existing events are read, in legacy mode and on the session authority.
  • Gateway

    No new or changed calls. It reads message.complete text and the response_previewed / response_reused / response_transformed flags. Those flags are optional, and when they're missing the text is compared directly.

    How I tested it

    Unit tests (shared/core/src/commonTest/.../chat/)

  • FinalReplyAfterToolsTest (new, 19 tests): the original repro (2 of its first 5 tests failed on master), plus reducer cases for:

  • - streamed and interim segments followed by a final that came only in message.complete
    - partly streamed, reused, previewed, transformed and cumulative finals
    - a silence marker the delivery dropped
    - the v0.22.0 shape with no segment stamps
    - the authority shape: an empty completion keeps what streamed, and a final that didn't stream shows
    - stopped and failed turns keep what streamed
    - a turn with no tools that streamed nothing
    - a reply rebuilt with no boundary
    - a boundary past the end of the text is clamped
    - captured v0.23.4 Bot Chat frames and rows
  • ChatSessionResumeTest (legacy, FakeTransport):

  • - aFinalWhoseDeltasFellInAGapTheRingCantFillStillShows
    - aFinalMissedWhileOutOfSightShowsWhenTheTurnEnds: resync with events.since truncated.
  • ReplayGapTest.aFinalLostInTheGapShowsOnceWhenOurTurnEnds (authority, replay_gap then a history reload): checks that the final shows exactly once, not twice.

  • With the merge forced off, 10 tests fail, including both legacy ChatSession tests and the original repros. With the fix they all pass.

  • The CI command from CONTRIBUTING.md, plus :desktopApp:compileKotlinJvm, is green on the rebased branch. That includes 1616 :shared:core:testAndroidHostTest tests with 0 failures.

  • :shared:core:jvmTest (not CI) has one failure on Windows: LinuxClientCertificatesTest, a POSIX file-permission test. It is unrelated, and it fails on master on Windows too.
  • On a device: version matrix

  • Setup:

  • - Emulator, Kyllene Dev, Bot Chat on a named profile.
    - A local test gateway with a fake model that writes text, runs a tool, writes more text, runs a second tool, then gives the final answer.
    - A test-only proxy between the app and the gateway drops every message.delta after the turn's 2nd tool (tool.complete, or tool.generating on versions that send only that) and renumbers seq, so the app sees no gap. The final answer then arrives only in message.complete.
  • Each version ran one turn with the deltas dropped and one normal turn.
  • | Hermes Agent | Mode | Build | Deltas dropped | Final after the last tool |
    |---|---|---|---|---|
    | v0.23.4 | legacy (dashboard) | master | 6 | missing (bug reproduced) |
    | v0.23.4 | legacy (dashboard) | this PR | 6 | shows once; normal turn unchanged |
    | v0.23.4 | session authority (gateway run) | this PR | 6 | shows once; normal turn unchanged |
    | v0.22.7 | session authority (gateway run) | this PR | 6 | shows once; normal turn unchanged |
    | v0.22.7 | legacy (dashboard) | this PR | 6 | shows once; normal turn unchanged |
    | v0.22.3 | legacy (dashboard) | this PR | 6 | shows once; normal turn unchanged |
    | v0.22.3 | session authority | — | — | not tested (see below) |
    | v0.22.0 | legacy (dashboard) | this PR | 6 | shows once; normal turn unchanged |

  • v0.22.3 session authority wasn't tested on a device.

  • - Before v0.22.6, gateway run has no webapp: listener setting. It hosts its authority socket only on a random loopback port, with no bind or auth option, and the app couldn't sign in through it.
    - The authority path is covered by v0.22.7 and v0.23.4 on the device, and by the ReplayGapTest and authority-shape reducer tests.
  • Only the fake model was used, not a real provider.
  • Screenshots

    No UI change. The answer just shows where it was missing. The device checks above used test-gateway data only.

    Checklist

  • One feature or fix, and its ROADMAP item is ticked

  • Up to date with master, and the command under "Tests" in CONTRIBUTING.md passes

  • A late answer after a profile, gateway or chat switch is dropped; reconnect, rotation and a second tap work

  • Anything cached or stored is keyed by gateway and profile (nothing new is stored)

  • Tested with unusual text, if it shows replies or other gateway text: whitespace and paragraph boundaries, an empty completion, a boundary past the end of the text

  • No tokens in URLs, and no fetches of an address on the user's own network that a reply or page links to

  • Reuses the app's components, fits at 360 dp in both themes, and icon buttons have content descriptions (no UI change)

  • The desktop app builds (./gradlew :desktopApp:compileKotlinJvm)

  • "Allow edits by maintainers" is on
  • 🤖 Generated with Claude Code

    Review follow-ups

    Three commits after review. Each case below has a FinalReplyAfterToolsTest test that failed before:

  • Steer mid-final (the CI review's finding): the part below the steer got the whole final appended. The final now drops the start the reply above holds, across two steers too. A session.redirect has the model answer again, so its final carries nothing. Shown on the emulator against a v0.23.4 dashboard, with /steer sent after "FINAL answer after " streamed. Before: "the last tool." then the whole final again below the steer. After: "the last tool." only.

  • A gap inside the streamed final: the final replaces the streamed start from where they agree, instead of following it.

  • Resume, catch-up, a stale reply, or a new turn's message.start: these forget the response start, so nothing stale is replaced or carried.

  • Interims: an interim added after the final began doesn't move the start, and one right after a steer keeps the carried start.

  • Already shown: only text added beside the calls after the last tool counts as the final already shown. A response_previewed final above a steer opens no second reply.

  • No known start: a final of several paragraphs that partly streamed replaces its streamed start.

  • Spacing: CRLF and LF compare alike, and a filled-in final stays its own paragraph after a list item.
  • Retest on the emulator (v0.23.4 legacy, this branch): with 6 final deltas dropped, the final shows once, and normal turns are unchanged. The CI command and :desktopApp:compileKotlinJvm pass, with 1637 core tests and 0 failures.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Giton22 Review duration: 50m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #550"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%