feat(generation): enforce prompt budget with whole article eviction (#47)
This commit is contained in:
@ -3,7 +3,6 @@ package ai
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -11,7 +10,6 @@ import (
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/summarybudget"
|
||||
)
|
||||
|
||||
func TestTicket39FinalPromptBudgetFullBodyWatchlistContextAndDates(t *testing.T) {
|
||||
@ -21,8 +19,8 @@ func TestTicket39FinalPromptBudgetFullBodyWatchlistContextAndDates(t *testing.T)
|
||||
date := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
body := strings.Repeat("Full synthetic editorial é漢🙂.\n", 1000)
|
||||
req := generation.SummaryRequest{Language: "en", Budget: 250000, Context: "Existing generation context", GeneratedAt: date, Assets: []models.UserAsset{{Name: "Followed company", Symbol: "INTERNAL"}}, Candidates: []generation.Candidate{{ID: "first", Article: scraper.Article{Title: "First title", Content: body, Source: "yahoo_finance", PublishedAt: &date}}, {ID: "second", Article: scraper.Article{Title: "Second title", Content: "Entire second body.", Source: "cnbc_business"}}}}
|
||||
_, usage, err := a.SummarizeWithBudget(context.Background(), req)
|
||||
if err != nil || p.calls != 1 || usage.EffectiveTokens != 123904 || usage.RequestedTokens != 250000 || p.options.MaxOutputTokens != 4096 || p.options.NumCtx != 128000 {
|
||||
_, usage, _, err := a.SummarizeWithBudget(context.Background(), req)
|
||||
if err != nil || p.calls != 1 || usage.EffectiveTokens != 250000 || usage.RequestedTokens != 250000 || p.options.MaxOutputTokens != 4096 || p.options.NumCtx != 0 {
|
||||
t.Fatalf("budget/forwarding incorrect: %+v %v", usage, err)
|
||||
}
|
||||
var payload struct {
|
||||
@ -50,14 +48,13 @@ func TestTicket39FinalPromptBudgetFullBodyWatchlistContextAndDates(t *testing.T)
|
||||
t.Fatal("whole prompt not counted")
|
||||
}
|
||||
req.Budget = usage.PromptTokens - 1
|
||||
_, _, err = a.SummarizeWithBudget(context.Background(), req)
|
||||
if !errors.Is(err, summarybudget.ErrExceeded) || p.calls != 1 {
|
||||
t.Fatal("overflow reached provider")
|
||||
_, _, removed, err := a.SummarizeWithBudget(context.Background(), req)
|
||||
if err != nil || removed != 1 || p.calls != 2 {
|
||||
t.Fatal("overflow did not remove oldest/unknown article", err)
|
||||
}
|
||||
a.model = "unknown"
|
||||
_, _, err = a.SummarizeWithBudget(context.Background(), req)
|
||||
if !errors.Is(err, summarybudget.ErrUnknown) || p.calls != 1 {
|
||||
t.Fatal("unknown capacity reached provider")
|
||||
if _, _, _, err = a.SummarizeWithBudget(context.Background(), req); err != nil || p.calls != 3 {
|
||||
t.Fatal("unknown model blocked", err)
|
||||
}
|
||||
raw, _ := json.Marshal(diagnostics)
|
||||
for _, secret := range []string{"Full synthetic", "Followed company", "Existing generation"} {
|
||||
|
||||
Reference in New Issue
Block a user