fix(agent): treat end_turn as natural stop

This commit is contained in:
cao_j
2026-07-01 16:16:08 +08:00
committed by lyingbug
parent baada32cb6
commit b16cb0bb56
4 changed files with 66 additions and 18 deletions
+23
View File
@@ -338,6 +338,29 @@ func TestExecuteLoop_NaturalStop_DoesNotDuplicateAnswer(t *testing.T) {
assert.GreaterOrEqual(t, doneCount, 1, "a Done marker must close the answer stream") assert.GreaterOrEqual(t, doneCount, 1, "a Done marker must close the answer stream")
} }
// TestExecuteLoop_EndTurnTerminates ensures Anthropic-style end_turn is treated
// like OpenAI's stop when no tool calls are present. Otherwise the ReAct loop
// keeps asking the model again and streams repeated answer chunks.
func TestExecuteLoop_EndTurnTerminates(t *testing.T) {
mock := &mockChat{
responses: []mockResponse{
{chunks: []types.StreamResponse{
{ResponseType: types.ResponseTypeAnswer, Content: "The answer.", Done: true, FinishReason: "end_turn"},
}},
},
}
engine := newTestEngine(t, mock)
state := &types.AgentState{}
_, err := engine.executeLoop(context.Background(), state, "test query",
emptyMessages(), emptyTools(), "sess-1", "msg-1")
require.NoError(t, err)
assert.True(t, state.IsComplete)
assert.Equal(t, "The answer.", state.FinalAnswer)
assert.Equal(t, 1, mock.callCount, "end_turn must end the loop after the first model call")
}
func TestStreamFinalAnswerToEventBus_EmitsDoneWhenProviderEndsWithEmptyChunk(t *testing.T) { func TestStreamFinalAnswerToEventBus_EmitsDoneWhenProviderEndsWithEmptyChunk(t *testing.T) {
mock := &mockChat{ mock := &mockChat{
responses: []mockResponse{ responses: []mockResponse{
+14 -3
View File
@@ -58,8 +58,19 @@ type responseVerdict struct {
step types.AgentStep step types.AgentStep
} }
// isNaturalStopFinishReason reports whether a provider finish reason means the
// assistant has ended its message without requesting more tool work.
func isNaturalStopFinishReason(reason string) bool {
switch strings.ToLower(strings.TrimSpace(reason)) {
case "stop", "end_turn", "stop_sequence":
return true
default:
return false
}
}
// analyzeResponse inspects the LLM response for stop conditions: // analyzeResponse inspects the LLM response for stop conditions:
// - finish_reason == "stop" with no tool calls → agent is done (natural stop) // - natural finish reason with no tool calls → agent is done (natural stop)
// - finish_reason == "content_filter" with no tool calls → agent is done (content filtered) // - finish_reason == "content_filter" with no tool calls → agent is done (content filtered)
// //
// The agent ends a turn by stopping naturally with its answer as plain // The agent ends a turn by stopping naturally with its answer as plain
@@ -114,8 +125,8 @@ func (e *AgentEngine) analyzeResponse(
} }
} }
// Case 1: LLM stopped naturally without requesting any tool calls // Case 1: LLM stopped naturally without requesting any tool calls.
if response.FinishReason == "stop" && len(response.ToolCalls) == 0 { if isNaturalStopFinishReason(response.FinishReason) && len(response.ToolCalls) == 0 {
// Strip <think>…</think> blocks that some models embed in content // Strip <think>…</think> blocks that some models embed in content
// (DeepSeek, Qwen, etc.) before processing or displaying. // (DeepSeek, Qwen, etc.) before processing or displaying.
response.Content = agenttools.StripThinkBlocks(response.Content) response.Content = agenttools.StripThinkBlocks(response.Content)
+26 -12
View File
@@ -41,22 +41,36 @@ func TestAnalyzeResponse_ToolCall_DoesNotTerminate(t *testing.T) {
"non-terminal tool calls must keep the loop running") "non-terminal tool calls must keep the loop running")
} }
// TestAnalyzeResponse_NaturalStop_Terminates guards the sole termination path: // TestAnalyzeResponse_NaturalStop_Terminates guards the termination path:
// finish_reason == "stop" with no tool calls ends the loop and surfaces the // a natural finish reason with no tool calls ends the loop and surfaces the
// plain content as the final answer. // plain content as the final answer. Different providers use different labels
// for the same "assistant turn is done" state.
func TestAnalyzeResponse_NaturalStop_Terminates(t *testing.T) { func TestAnalyzeResponse_NaturalStop_Terminates(t *testing.T) {
engine := newTestEngine(t, &mockChat{}) tests := []struct {
resp := &types.ChatResponse{ name string
FinishReason: "stop", finishReason string
Content: "Here is the answer.", }{
{name: "openai_stop", finishReason: "stop"},
{name: "anthropic_end_turn", finishReason: "end_turn"},
{name: "anthropic_stop_sequence", finishReason: "stop_sequence"},
} }
verdict := engine.analyzeResponse( for _, tt := range tests {
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(), t.Run(tt.name, func(t *testing.T) {
) engine := newTestEngine(t, &mockChat{})
resp := &types.ChatResponse{
FinishReason: tt.finishReason,
Content: "Here is the answer.",
}
assert.True(t, verdict.isDone, "a natural stop with no tool calls must terminate the loop") verdict := engine.analyzeResponse(
assert.Equal(t, "Here is the answer.", verdict.finalAnswer) context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.True(t, verdict.isDone, "a natural stop with no tool calls must terminate the loop")
assert.Equal(t, "Here is the answer.", verdict.finalAnswer)
})
}
} }
// TestAppendToolResults_PreservesReasoningContent verifies that the assistant // TestAppendToolResults_PreservesReasoningContent verifies that the assistant
+3 -3
View File
@@ -437,9 +437,9 @@ func (e *AgentEngine) callLLMWithRetry(
round, response.FinishReason, len(response.Content)) round, response.FinishReason, len(response.Content))
// Early signal for natural-stop path: this round will be analyzed as a // Early signal for natural-stop path: this round will be analyzed as a
// likely final answer (no tool call branch). // likely final answer (no tool call branch).
if response.FinishReason == "stop" { if isNaturalStopFinishReason(response.FinishReason) {
logger.Infof(ctx, "[Agent][Round-%d] Natural-stop candidate detected (finish=stop, tool_calls=0, content=%d chars)", logger.Infof(ctx, "[Agent][Round-%d] Natural-stop candidate detected (finish=%s, tool_calls=0, content=%d chars)",
round, len(response.Content)) round, response.FinishReason, len(response.Content))
} }
} }
if response.Content != "" { if response.Content != "" {