WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: Empty agent retries emit premature terminal answer events

Open LeonPure opened this issue 3 months ago • 1 comments

Affected Component

Backend Service & API / Agent event lifecycle

Bug Description

When an Agent LLM call stops naturally with empty content and no tool calls, WeKnora correctly decides to retry with a nudge. However, analyzeResponse emits EventAgentFinalAnswer with Done=true before returning emptyContent=true to the retry loop.

Downstream consumers therefore observe a terminal answer while the Agent is still running. The IM consumer closes its answer-done channel on that event and waits only a bounded time for EventAgentComplete; if the retry is slower than that window, IM can finalize a fallback response and cancel the still-running Agent.

This is still present at upstream commit af0aeb7cc284d76e2c59b4eaf686132e0d5b8caa: internal/agent/observe.go emits Done=true before returning emptyContent: response.Content == "".

This is related to #818 / #819, which added empty-response retries, and to the terminal-event semantics discussed in #2339, but it is a distinct downstream race.

Steps to Reproduce

  1. Run an Agent-mode QA request through an event consumer such as IM.
  2. Make the first model response contain no content and no tool calls with a natural stop finish reason.
  3. Make the subsequent retry take longer than the consumer's completion wait window.
  4. Observe EventAgentFinalAnswer{Content:"", Done:true} before the retry completes.

A deterministic unit reproduction can provide an empty first response followed by a non-empty response and count terminal EventAgentFinalAnswer events.

Expected Behavior

Retryable empty responses should emit no terminal answer event. Exactly one terminal answer event should be emitted after a non-empty retry succeeds or after retries are exhausted and a fallback answer is selected.

Actual Behavior

Each empty natural stop emits Done=true before the retry decision, allowing downstream consumers to terminate early.

WeKnora Version

  • v0.7.2
  • Confirmed in upstream main at af0aeb7cc284d76e2c59b4eaf686132e0d5b8caa

Deployment Method

Built from source; reproducible in Agent unit tests and IM event consumption

Operating System

Platform-independent backend behavior

Relevant Logs

No specific error log is required. The failure is an ordering problem between the final-answer event and the empty-response retry decision.

Proposed Fix

Detect an empty natural stop before emitting final-answer content or Done. If retries are exhausted, emit the selected fallback as the sole terminal answer. Add regression assertions that retry attempts produce exactly one terminal event in total.

Confirmation

  • [x] I searched existing issues and confirmed this exact retry/terminal ordering bug is not already reported.

LeonPure avatar Aug 31 '26 12:08 LeonPure

Confirmed still present on current main and submitted a fix: #3104.

Verification at the verdict layer (internal/agent/observe.go, Case 1): the EventAgentFinalAnswer{Done: true} marker is emitted unconditionally for a natural stop — including the Content == "" path whose verdict the caller then retries, exactly as you described.

Fix shape follows your proposal: an empty natural stop now emits no answer event at all (the verdict alone signals retryable); when retries are exhausted, the loop emits the selected fallback as the turn's sole terminal answer. A turn therefore always ends with exactly one terminal answer event.

The PR includes the deterministic unit reproduction you suggested — empty first response followed by a non-empty retry, asserting exactly one terminal EventAgentFinalAnswer per turn (plus the exhausted-retry variant). All three new tests are red on the pre-fix code.

vicfei avatar Sep 08 '26 05:09 vicfei