Files
Enterprise-Knowledge-Base/internal/agent/observe_test.go
T
wizardchen a6d4266c93 fix(agent): terminate ReAct loop on final_answer regardless of arg parse success
The agent loop previously kept running when the LLM emitted final_answer with
malformed JSON arguments: observe.analyzeResponse strictly json.Unmarshal'd the
raw args and returned isDone=false on failure, while act.runToolCall separately
repaired the JSON and executed the tool. The mismatch made final_answer
non-terminal, so the LLM saw its own answer in the next round's tool result
and re-emitted final_answer, surfacing near-identical answers multiple times
to the user (issue #1008).

Fix by unifying parsing in a new tools.ParseFinalAnswerArgs helper with three
fallbacks (strict -> RepairJSON -> regex extraction) and making final_answer
always terminal in analyzeResponse. When no answer can be recovered, emit a
user-visible fallback message and a Done=true event so the UI still resolves.

Also route final_answer.Execute through the same helper so tool execution and
loop termination stay consistent.

Adds regression tests in observe_test.go and final_answer_test.go covering
valid args, RepairJSON recovery, unrecoverable args, and garbage input.

Refs: Tencent/WeKnora#1008
2026-04-23 19:36:28 +08:00

127 lines
4.6 KiB
Go

package agent
import (
"context"
"testing"
"time"
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
)
// newFinalAnswerResponse builds a ChatResponse that carries a single
// final_answer tool call with the given raw JSON arguments.
func newFinalAnswerResponse(rawArgs string) *types.ChatResponse {
return &types.ChatResponse{
FinishReason: "tool_calls",
ToolCalls: []types.LLMToolCall{
{
ID: "call-1",
Type: "function",
Function: types.FunctionCall{
Name: agenttools.ToolFinalAnswer,
Arguments: rawArgs,
},
},
},
}
}
// TestAnalyzeResponse_FinalAnswer_ValidArgs guards the happy path: well-formed
// arguments must be extracted into the final answer and terminate the loop.
func TestAnalyzeResponse_FinalAnswer_ValidArgs(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
resp := newFinalAnswerResponse(`{"answer": "Here is the answer."}`)
verdict := engine.analyzeResponse(
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.True(t, verdict.isDone, "final_answer must terminate the loop")
assert.Equal(t, "Here is the answer.", verdict.finalAnswer)
}
// TestAnalyzeResponse_FinalAnswer_MalformedJSON_RecoveredViaRepair covers the
// common case reported in issue #1008: the LLM emits final_answer with a
// trailing comma / missing brace. RepairJSON should recover the answer and
// the loop must still terminate in this single round (not re-invoke
// final_answer in the next round).
func TestAnalyzeResponse_FinalAnswer_MalformedJSON_RecoveredViaRepair(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
resp := newFinalAnswerResponse(`{"answer": "repaired"`) // missing closing brace
verdict := engine.analyzeResponse(
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.True(t, verdict.isDone,
"final_answer must terminate the loop even when JSON repair is needed")
assert.Equal(t, "repaired", verdict.finalAnswer)
}
// TestAnalyzeResponse_FinalAnswer_UnrecoverableArgs_StillTerminates is the
// direct regression test for issue #1008: when the arguments are so malformed
// that even RepairJSON + regex cannot recover an answer, the loop MUST still
// terminate (with a user-visible fallback message) rather than continuing and
// letting the LLM re-emit final_answer on the next round.
func TestAnalyzeResponse_FinalAnswer_UnrecoverableArgs_StillTerminates(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
// No `answer` key at all — strict parse succeeds (returns zero-value
// answer), RepairJSON is a no-op on already-valid JSON, regex finds
// nothing. All three tiers fail to recover an answer.
resp := newFinalAnswerResponse(`{"unexpected": "field"}`)
verdict := engine.analyzeResponse(
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.True(t, verdict.isDone,
"final_answer must terminate the loop even when args are unrecoverable — "+
"otherwise the LLM re-emits final_answer and duplicates the answer (issue #1008)")
assert.Equal(t, finalAnswerParseFallback, verdict.finalAnswer,
"unrecoverable final_answer should surface the parse-failure fallback message")
}
// TestAnalyzeResponse_FinalAnswer_Garbage_StillTerminates exercises the most
// hostile case: completely non-JSON arguments. The loop must still terminate
// — protecting against the duplicate-answer loop reported in issue #1008.
func TestAnalyzeResponse_FinalAnswer_Garbage_StillTerminates(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
resp := newFinalAnswerResponse(`not json at all`)
verdict := engine.analyzeResponse(
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.True(t, verdict.isDone)
assert.Equal(t, finalAnswerParseFallback, verdict.finalAnswer)
}
// TestAnalyzeResponse_NonFinalAnswerTool_DoesNotTerminate is a regression
// guard: only final_answer is terminal. Other tool calls (e.g. thinking,
// knowledge_search) must keep the loop running.
func TestAnalyzeResponse_NonFinalAnswerTool_DoesNotTerminate(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
resp := &types.ChatResponse{
FinishReason: "tool_calls",
ToolCalls: []types.LLMToolCall{
{
ID: "call-1",
Type: "function",
Function: types.FunctionCall{
Name: agenttools.ToolKnowledgeSearch,
Arguments: `{"query": "hi"}`,
},
},
},
}
verdict := engine.analyzeResponse(
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
)
assert.False(t, verdict.isDone,
"non-terminal tool calls must keep the loop running")
}