* fix(agent): end the turn when the completion cap truncates an answer
A round that came back with finish_reason=length and no tool calls used to
fall through analyzeResponse as non-terminal. With no tool calls to run,
appendToolResults pushed the half-written answer back into the history as a
plain assistant message with nothing telling the model to continue, so the
next round rewrote the answer from the top, hit the same cap, and repeated
until the round budget ran out or the user cancelled.
Three guards, each independently load-bearing:
- analyzeResponse gains a truncation case: deliver the partial text and end
the turn. A cap hit that produced no text at all has nothing to hand over
and defers to the existing bounded empty-content retry. No continuation
nudge is sent — it only grows the prompt with the discarded fragment, and
assistant prefill, the reliable form, is unavailable on most
OpenAI-compatible endpoints.
- A consecutive-length breaker stops the loop when truncation keeps landing
inside tool-call arguments, the one shape that still legitimately loops.
- The stuck-loop detector now counts an identical lack of content. A model
burning its whole budget on reasoning returns empty every round, which used
to land in the else branch and reset the counter, so the guard could never
fire on the case that needed it most.
The answer event and its Done marker carry a truncated flag through the SSE
metadata, and the chat UI shows the same style of notice it already uses for
fallback answers.
Fixes#3446
* fix(agent): treat unterminated reasoning as no answer and keep the truncation marker
Review follow-up on three ways the truncation path could still mislead.
An inline <think> block that the cap cut off never gets its closing tag, so
the pair regex left it whole and the round looked like it had produced an
answer. The truncation case then skipped the empty-content retry it exists
for and emitted the chain of thought as the final answer. StripThinkBlocks
now drops a dangling <think> through end of input, which is what the
streaming splitter already does with the same text, so the two agree on what
counts as an answer.
The truncation marker only rode on the SSE metadata, and a finished
conversation is rebuilt from agent_steps rather than replayed from stream
events. A reloaded, shared or re-opened turn therefore lost the notice and
its half-written answer looked complete. AgentStep carries the flag now — it
is a jsonb column, so no migration — and the frontend marks the rebuilt
answer event and message from it.
The consecutive-length guard ran before the round was recorded, so a final
round of "let me look that up" plus a truncated tool call handed that
preamble over as the final answer, with its tool calls never refused and the
preamble never retracted from the answer area. The guard now records the
round and fails its tool calls the way every other truncated round does, and
a response carrying tool calls never donates its text as the answer.