fix(agent): report failed tool executions as tool_result, not error
response_type=error was being emitted when a tool execution failed, but that type is reserved for internal agent failures (LLM/pipeline errors), which clients treat as a crashed run. A failed tool call is still a tool result: both the chat SSE handler and the skill install transcript now emit response_type=tool_result with success=false and the error string in metadata, so clients render it as a failed tool card and let the agent recover instead of treating the run as dead. Frontend needs no change: useChatStreamHandler already resolves tool outcome from the success metadata field, history replay rebuilds from agent_steps, and the API playground stops failing a whole request on a recoverable tool error.main
parent
c8ebb9ed96
commit
e3ac49cace
|
|
@ -310,15 +310,13 @@ func (tr *installTranscript) onToolResult(_ context.Context, evt event.Event) er
|
|||
}
|
||||
tr.mu.Unlock()
|
||||
|
||||
// A failed command is surfaced as an error, matching the chat path, so the
|
||||
// console highlights it instead of filing it as one more quiet step.
|
||||
// A failed command is still a tool result: response_type=error is reserved
|
||||
// for internal failures (onError), matching the chat path. Failure is
|
||||
// carried by success=false in the metadata.
|
||||
responseType := types.ResponseTypeToolResult
|
||||
content := agenttools.StreamContentForToolResult(data.ToolName, data.Success, data.Error, data.Data)
|
||||
if !data.Success {
|
||||
responseType = types.ResponseTypeError
|
||||
if content == "" && data.Error != "" {
|
||||
content = data.Error
|
||||
}
|
||||
if !data.Success && content == "" && data.Error != "" {
|
||||
content = data.Error
|
||||
}
|
||||
|
||||
meta := map[string]interface{}{
|
||||
|
|
|
|||
|
|
@ -149,9 +149,9 @@ func TestInstallTranscriptProjectsTheAgentsWork(t *testing.T) {
|
|||
}
|
||||
|
||||
// A failed command is the single most useful thing in an install transcript,
|
||||
// so it must reach the stream as an error the console already knows how to
|
||||
// render, not be dropped for having Success=false.
|
||||
func TestInstallTranscriptReportsAFailedCommandAsAnError(t *testing.T) {
|
||||
// so it must reach the stream as a tool result carrying success=false —
|
||||
// response_type=error is reserved for internal failures, not tool outcomes.
|
||||
func TestInstallTranscriptReportsAFailedCommandAsAToolResult(t *testing.T) {
|
||||
_, bus, streams, _ := newTranscriptForTest(t)
|
||||
|
||||
require.NoError(t, bus.Emit(context.Background(), event.Event{
|
||||
|
|
@ -162,7 +162,7 @@ func TestInstallTranscriptReportsAFailedCommandAsAnError(t *testing.T) {
|
|||
},
|
||||
}))
|
||||
|
||||
require.Equal(t, []types.ResponseType{types.ResponseTypeError}, streams.types())
|
||||
require.Equal(t, []types.ResponseType{types.ResponseTypeToolResult}, streams.types())
|
||||
require.Equal(t, "exit status 1", streams.events[0].Content)
|
||||
require.Equal(t, false, streams.events[0].Data["success"])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -272,14 +272,14 @@ func (h *AgentStreamHandler) handleToolResult(ctx context.Context, evt event.Eve
|
|||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
// Send SSE response (both success and failure)
|
||||
// Send SSE response (both success and failure). A failed tool execution is
|
||||
// still a tool result: response_type=error is reserved for internal agent
|
||||
// failures (handleError), while tool failures are reported through
|
||||
// response_type=tool_result with success=false in the metadata.
|
||||
responseType := types.ResponseTypeToolResult
|
||||
content := agenttools.StreamContentForToolResult(data.ToolName, data.Success, data.Error, data.Data)
|
||||
if !data.Success {
|
||||
responseType = types.ResponseTypeError
|
||||
if content == "" && data.Error != "" {
|
||||
content = data.Error
|
||||
}
|
||||
if !data.Success && content == "" && data.Error != "" {
|
||||
content = data.Error
|
||||
}
|
||||
|
||||
// Build metadata including tool result data for rich frontend rendering
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// capturingStreamManager records every appended event so a handler's output
|
||||
// can be asserted without a real stream backend. Embedding the interface keeps
|
||||
// everything else nil-panicky so an un-stubbed call fails loudly.
|
||||
type capturingStreamManager struct {
|
||||
interfaces.StreamManager
|
||||
events []interfaces.StreamEvent
|
||||
}
|
||||
|
||||
func (s *capturingStreamManager) AppendEvent(
|
||||
_ context.Context, _, _ string, evt interfaces.StreamEvent,
|
||||
) error {
|
||||
s.events = append(s.events, evt)
|
||||
return nil
|
||||
}
|
||||
|
||||
// A failed tool execution is still a tool result: the SSE event must keep
|
||||
// response_type=tool_result with success=false in its metadata, because
|
||||
// response_type=error is reserved for internal agent failures (handleError).
|
||||
func TestAgentStreamHandlerToolResultFailureKeepsToolResultType(t *testing.T) {
|
||||
streams := &capturingStreamManager{}
|
||||
h := NewAgentStreamHandler(
|
||||
context.Background(), "sess-1", "msg-1", "req-1", 1, time.Now(),
|
||||
&types.Message{}, streams, nil, nil, nil, nil,
|
||||
)
|
||||
|
||||
err := h.handleToolResult(context.Background(), event.Event{
|
||||
ID: "evt-1", Type: event.EventAgentToolResult,
|
||||
Data: event.AgentToolResultData{
|
||||
ToolName: "shell_exec",
|
||||
ToolCallID: "call-1",
|
||||
Success: false,
|
||||
Error: "exit status 1",
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, streams.events, 1)
|
||||
|
||||
evt := streams.events[0]
|
||||
require.Equal(t, types.ResponseTypeToolResult, evt.Type)
|
||||
require.Equal(t, false, evt.Data["success"])
|
||||
require.Equal(t, "exit status 1", evt.Data["error"])
|
||||
require.Equal(t, "call-1", evt.Data["tool_call_id"])
|
||||
}
|
||||
|
||||
// Internal error events keep response_type=error with done=true, so clients
|
||||
// can still distinguish a crashed run from a failed tool call.
|
||||
func TestAgentStreamHandlerInternalErrorKeepsErrorType(t *testing.T) {
|
||||
streams := &capturingStreamManager{}
|
||||
h := NewAgentStreamHandler(
|
||||
context.Background(), "sess-1", "msg-1", "req-1", 1, time.Now(),
|
||||
&types.Message{}, streams, nil, nil, nil, nil,
|
||||
)
|
||||
|
||||
err := h.handleError(context.Background(), event.Event{
|
||||
ID: "evt-err", Type: event.EventError,
|
||||
Data: event.ErrorData{Stage: "think", Error: "llm unavailable"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, streams.events, 1)
|
||||
|
||||
evt := streams.events[0]
|
||||
require.Equal(t, types.ResponseTypeError, evt.Type)
|
||||
require.True(t, evt.Done)
|
||||
}
|
||||
Loading…
Reference in New Issue