fix(ai): diagnose summary provider failures (#37)
This commit is contained in:
152
backend/internal/ai/summary_incident_ticket37_test.go
Normal file
152
backend/internal/ai/summary_incident_ticket37_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
)
|
||||
|
||||
type incident37Repo struct{ saved []generation.GenerationRecord }
|
||||
|
||||
func (*incident37Repo) LoadUserInput(context.Context, string) (generation.UserInput, error) {
|
||||
key := models.SourceYahooFinance
|
||||
return generation.UserInput{Language: "fr", RankingMode: "title_content", GlobalLimit: 90, Assets: []models.UserAsset{{Name: "Followed Company"}}, Sources: []models.Source{{ID: "s", SourceKey: &key, Enabled: true, ArticleLimit: 90}}}, nil
|
||||
}
|
||||
func (r *incident37Repo) SaveGeneration(_ context.Context, _ string, v generation.GenerationRecord) error {
|
||||
r.saved = append(r.saved, v)
|
||||
return nil
|
||||
}
|
||||
|
||||
type incident37Feed struct{ oversized bool }
|
||||
|
||||
func (incident37Feed) Name() string { return "fixture" }
|
||||
func (f incident37Feed) Scrape(context.Context, []string) ([]scraper.Article, error) {
|
||||
out := make([]scraper.Article, 90)
|
||||
for i := range out {
|
||||
title := fmt.Sprintf("PRIVATE_TITLE_%03d", i)
|
||||
if f.oversized {
|
||||
title += strings.Repeat("x", 500)
|
||||
}
|
||||
out[i] = scraper.Article{Title: title, URL: fmt.Sprintf("https://PRIVATE_URL.invalid/%d", i), Content: strings.Repeat("PRIVATE_CONTENT漢<&", 1024)}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type incident37Laya struct{ direct int }
|
||||
|
||||
func (d *incident37Laya) DecideMentions(_ context.Context, _, pass string, _ []models.UserAsset, c []generation.Candidate) ([]generation.Candidate, error) {
|
||||
out := []generation.Candidate{}
|
||||
if pass == generation.MentionDirect {
|
||||
for _, v := range c {
|
||||
if d.direct%3 != 0 {
|
||||
out = append(out, v)
|
||||
}
|
||||
d.direct++
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type incident37Provider struct {
|
||||
contentProvider
|
||||
err error
|
||||
empty bool
|
||||
options GenOptions
|
||||
}
|
||||
|
||||
func (p *incident37Provider) Summarize(ctx context.Context, prompt string, opts GenOptions) (string, error) {
|
||||
p.options = opts
|
||||
result, _ := p.contentProvider.Summarize(ctx, prompt, opts)
|
||||
if p.empty {
|
||||
result = ""
|
||||
}
|
||||
return result, p.err
|
||||
}
|
||||
|
||||
// This is an offline characterization, not a reconstruction of production's
|
||||
// unknown provider response. All scenario labels and assertions are sanitized.
|
||||
func TestTicket37NinetyCandidatesSixtySelectedSummary(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, code string
|
||||
err error
|
||||
oversized, empty bool
|
||||
}{
|
||||
{name: "success"},
|
||||
{name: "provider_rejection", code: "summary_failed", err: errors.New("PRIVATE_BODY context limit PRIVATE_SECRET")},
|
||||
{name: "timeout", code: "summary_timeout", err: fmt.Errorf("PRIVATE_BODY: %w", context.DeadlineExceeded)},
|
||||
{name: "canceled", code: "summary_canceled", err: fmt.Errorf("PRIVATE_BODY: %w", context.Canceled)},
|
||||
{name: "empty_response", code: "summary_failed", empty: true},
|
||||
{name: "immutable_metadata_over_budget", code: "summary_failed", oversized: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
repo := &incident37Repo{}
|
||||
laya := &incident37Laya{}
|
||||
provider := &incident37Provider{err: tc.err, empty: tc.empty}
|
||||
uc := generation.NewUseCase(generation.Dependencies{Repository: repo, Decider: laya, Summarizer: generationSummarizer{provider: provider}, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: incident37Feed{oversized: tc.oversized}}})
|
||||
result, err := uc.Generate(context.Background(), "fixture")
|
||||
if err != nil {
|
||||
t.Fatal("unexpected repository/use-case error")
|
||||
}
|
||||
o := result.Outcome
|
||||
if o.TotalCandidates != 90 || o.LayaSelected != 60 || o.Selected != 60 || laya.direct != 90 {
|
||||
t.Fatal("90 -> 60 -> 60 invariant broken")
|
||||
}
|
||||
if o.FailureCode != tc.code || (result.Summary != nil) != (tc.code == "") {
|
||||
t.Fatal("incorrect terminal classification")
|
||||
}
|
||||
calls := 1
|
||||
if tc.oversized {
|
||||
calls = 0
|
||||
}
|
||||
if provider.calls != calls {
|
||||
t.Fatal("unexpected provider call count or fallback")
|
||||
}
|
||||
if calls == 1 {
|
||||
if len(provider.prompt) > 24*1024 || provider.options.NumCtx != 32768 {
|
||||
t.Fatal("prompt budget/context contract broken")
|
||||
}
|
||||
var payload struct {
|
||||
Articles []struct {
|
||||
ID, Title, Content string
|
||||
Truncated bool `json:"content_truncated"`
|
||||
}
|
||||
}
|
||||
at := strings.Index(provider.prompt, "\n")
|
||||
if at < 0 || json.Unmarshal([]byte(provider.prompt[at+1:]), &payload) != nil || len(payload.Articles) != 60 {
|
||||
t.Fatal("invalid summary payload/count")
|
||||
}
|
||||
selected := 0
|
||||
for i := 0; i < 90; i++ {
|
||||
if i%3 == 0 {
|
||||
continue
|
||||
}
|
||||
a := payload.Articles[selected]
|
||||
if a.Title != fmt.Sprintf("PRIVATE_TITLE_%03d", i) || a.ID == "" || !a.Truncated || a.Content == "" {
|
||||
t.Fatal("identity/order/excerpt contract broken")
|
||||
}
|
||||
selected++
|
||||
}
|
||||
}
|
||||
if len(repo.saved) != 1 {
|
||||
t.Fatal("terminal record missing")
|
||||
}
|
||||
raw, e := json.Marshal(repo.saved)
|
||||
if e != nil {
|
||||
t.Fatal("cannot encode terminal record")
|
||||
}
|
||||
for _, secret := range []string{"PRIVATE_TITLE", "PRIVATE_CONTENT", "PRIVATE_URL", "PRIVATE_BODY", "PRIVATE_SECRET"} {
|
||||
if strings.Contains(string(raw), secret) {
|
||||
t.Fatal("sensitive data persisted")
|
||||
}
|
||||
}
|
||||
t.Logf("candidates=90 laya_selected=60 selected=60 provider_calls=%d prompt_bytes=%d failure_code=%s", provider.calls, len(provider.prompt), o.FailureCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user