fix(gateway): a failed chat turn shows plain wording, not the provider's error (#1902)
deploy / plan (push) Canceled after 0s
deploy / gate (push) Canceled after 0s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
ci/woodpecker/push/deploy Pipeline was canceled

This commit is contained in:
sam committed 2026-11-12 23:11:07 +11:00
1 parent 6a7f8b4847
commit fe8afb18d0
4 files changed
+83 -3

No files matched your search

+2 -2
View File
@@ -230,7 +230,7 @@ func (a *LocalChatAdapter) runStreamedTurn(ctx context.Context, in Inbound, id s
run := func(turnCtx context.Context) ChatEvent {
reply, err := a.Router.streamTurn(turnCtx, in, stream)
if err != nil {
return ChatEvent{Kind: "error", Text: err.Error(), SessionID: id}
return ChatEvent{Kind: "error", Text: turnFailureText(err), SessionID: id}
}
resolvedID := id
if resolved, err := a.Router.ResolveSessionKey(turnCtx, in); err == nil {
@@ -306,7 +306,7 @@ func (a *LocalChatAdapter) prelude(ctx context.Context, in Inbound, id string) s
}
ready, mine, err := a.Router.CollectTurn(ctx, in)
if err != nil {
return streamPrelude{event: &ChatEvent{Kind: "error", Text: err.Error(), SessionID: id}}
return streamPrelude{event: &ChatEvent{Kind: "error", Text: turnFailureText(err), SessionID: id}}
}
return streamPrelude{ready: ready, proceeds: mine}
}
+1 -1
View File
@@ -580,7 +580,7 @@ func (r *TurnRunner) failTurn(ctx context.Context, turn TurnRecord, cause error)
} else {
turn.Status = TurnStatusFailed
}
turn.Error = cause.Error()
turn.Error = turnFailureText(cause)
turn.UpdatedAt = time.Now().UTC()
if r.Log != nil {
level := slog.LevelError
+30
View File
@@ -0,0 +1,30 @@
package gateway
import (
"context"
"errors"
"github.com/samcharles93/ai-sdk/chat"
)
// turnFailureText is what a chat reader sees when a turn fails. The cause
// carries provider bodies, URLs and wrapping that belong in the log, which
// failTurn writes, not in front of whoever is chatting.
func turnFailureText(err error) string {
switch {
case errors.Is(err, chat.ErrRateLimited):
return "the model provider is refusing requests (rate limit or spending cap). Try again later, or switch model with /model."
case errors.Is(err, chat.ErrAuthFailed):
return "the model provider rejected Archie's credentials. Check the provider key in settings."
case errors.Is(err, chat.ErrProviderUnavailable):
return "the model provider is unavailable. Try again in a moment."
case errors.Is(err, chat.ErrContextLength):
return "this conversation is too long for the model. Start a new chat, or shorten it with /compress."
case errors.Is(err, chat.ErrUnsupportedContent):
return "the model can't read one of the attachments. Remove it, or switch model with /model."
case errors.Is(err, context.DeadlineExceeded):
return "the model took too long to answer. Try again."
default:
return "something went wrong on Archie's side. Try again; the gateway log has the cause."
}
}
+50
View File
@@ -0,0 +1,50 @@
package gateway
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// A failed turn shows the reader what to do, never the provider's response
// body or the error's wrapping.
func TestStreamedTurnFailureHidesCause(t *testing.T) {
for _, tc := range []struct {
name string
cause error
want string
}{
{name: "spending cap", cause: chat.ErrRateLimited, want: "rate limit or spending cap"},
{name: "bad key", cause: chat.ErrAuthFailed, want: "credentials"},
{name: "unknown", cause: errors.New("dial tcp 10.0.0.1:5432: refused"), want: "gateway log"},
} {
t.Run(tc.name, func(t *testing.T) {
raw := fmt.Errorf(`llm chat stream: gemini: status 429: {"message":"https://ai.studio/spend"}: %w`, tc.cause)
router := &Router{LLMStream: func(context.Context, Inbound, TurnStream) (string, error) { return "", raw }}
adapter := &LocalChatAdapter{Router: router}
events := make(chan ChatEvent, 32)
go adapter.runStreamedTurn(context.Background(), Inbound{Message: messaging.Message{Text: "hi"}}, "s1", events)
for event := range events {
if event.Kind != "error" {
continue
}
for _, leak := range []string{"gemini", "429", "ai.studio", "10.0.0.1", "llm chat stream"} {
if strings.Contains(event.Text, leak) {
t.Fatalf("error text %q exposes %q", event.Text, leak)
}
}
if !strings.Contains(event.Text, tc.want) {
t.Fatalf("error text %q, want it to mention %q", event.Text, tc.want)
}
return
}
t.Fatal("no error event")
})
}
}