feat(qa): add web search provider port (#60)
This commit is contained in:
132
backend/internal/summaryqa/citations.go
Normal file
132
backend/internal/summaryqa/citations.go
Normal file
@ -0,0 +1,132 @@
|
||||
package summaryqa
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const MaxCitations = 16
|
||||
const MaxCitationTitleRunes = 256
|
||||
const MaxCitationURLBytes = 2048
|
||||
|
||||
type Citation struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type AnswerResult struct {
|
||||
Answer string `json:"answer"`
|
||||
WebSearchPerformed bool `json:"web_search_performed"`
|
||||
Citations []Citation `json:"citations"`
|
||||
}
|
||||
|
||||
// NormalizeCitations accepts metadata from structured provider annotations only.
|
||||
// It never discovers URLs in answer text and never fetches a cited URL.
|
||||
func NormalizeCitations(raw []Citation) []Citation {
|
||||
result := make([]Citation, 0, min(len(raw), MaxCitations))
|
||||
seen := make(map[string]bool)
|
||||
for _, citation := range raw {
|
||||
value := strings.TrimSpace(citation.URL)
|
||||
if len(value) > MaxCitationURLBytes || !utf8.ValidString(value) || strings.ContainsAny(value, "\\") || strings.IndexFunc(value, unicode.IsControl) >= 0 {
|
||||
continue
|
||||
}
|
||||
u, err := url.Parse(value)
|
||||
if err != nil || !strings.EqualFold(u.Scheme, "https") || u.Opaque != "" || u.User != nil || u.Host == "" {
|
||||
continue
|
||||
}
|
||||
query, queryErr := url.QueryUnescape(u.RawQuery)
|
||||
if queryErr != nil || !utf8.ValidString(u.Path) || !utf8.ValidString(query) || strings.IndexFunc(u.Path, unicode.IsControl) >= 0 || strings.IndexFunc(query, unicode.IsControl) >= 0 {
|
||||
continue
|
||||
}
|
||||
host := strings.ToLower(u.Hostname())
|
||||
if !validCitationHost(host) {
|
||||
continue
|
||||
}
|
||||
port := u.Port()
|
||||
if port != "" {
|
||||
n, err := strconv.Atoi(port)
|
||||
if err != nil || n < 1 || n > 65535 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
u.Scheme = "https"
|
||||
u.Host = host
|
||||
if port != "" && port != "443" {
|
||||
u.Host = net.JoinHostPort(host, port)
|
||||
}
|
||||
u.Fragment = ""
|
||||
u.RawFragment = ""
|
||||
if u.Path == "" {
|
||||
u.Path = "/"
|
||||
}
|
||||
normalized := u.String()
|
||||
if len(normalized) > MaxCitationURLBytes || seen[normalized] {
|
||||
continue
|
||||
}
|
||||
if !utf8.ValidString(citation.Title) {
|
||||
continue
|
||||
}
|
||||
title := strings.Join(strings.FieldsFunc(citation.Title, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }), " ")
|
||||
if title == "" {
|
||||
continue
|
||||
}
|
||||
if utf8.RuneCountInString(title) > MaxCitationTitleRunes {
|
||||
title = string([]rune(title)[:MaxCitationTitleRunes])
|
||||
}
|
||||
seen[normalized] = true
|
||||
result = append(result, Citation{Title: title, URL: normalized})
|
||||
if len(result) == MaxCitations {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func validCitationHost(host string) bool {
|
||||
if host == "" || len(host) > 253 {
|
||||
return false
|
||||
}
|
||||
// Citation links accept qualified DNS names only. Reject every IP literal,
|
||||
// including public ones, rather than relying on incomplete IP range checks.
|
||||
// This is purely syntactic validation: never resolve or fetch citation URLs.
|
||||
if net.ParseIP(host) != nil {
|
||||
return false
|
||||
}
|
||||
host = strings.ToLower(host)
|
||||
labels := strings.Split(host, ".")
|
||||
if len(labels) < 2 {
|
||||
return false
|
||||
}
|
||||
tld := labels[len(labels)-1]
|
||||
switch tld {
|
||||
case "localhost", "local", "localdomain", "internal", "lan", "home", "corp", "intranet", "arpa", "test", "invalid", "example", "onion", "alt":
|
||||
return false
|
||||
}
|
||||
// Alphabetic (or IDNA) top-level labels also reject abbreviated, integer,
|
||||
// octal and hexadecimal IP spellings browsers may interpret as addresses.
|
||||
if !strings.HasPrefix(tld, "xn--") {
|
||||
if len(tld) < 2 {
|
||||
return false
|
||||
}
|
||||
for _, r := range tld {
|
||||
if r < 'a' || r > 'z' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, label := range labels {
|
||||
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
|
||||
return false
|
||||
}
|
||||
for _, r := range label {
|
||||
if !((r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
57
backend/internal/summaryqa/citations_ticket60_test.go
Normal file
57
backend/internal/summaryqa/citations_ticket60_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package summaryqa
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket60CitationNormalizationAndBounds(t *testing.T) {
|
||||
raw := []Citation{
|
||||
{Title: " Useful \n source ", URL: " HTTPS://EXAMPLE.COM:443/report#part "},
|
||||
{Title: "duplicate", URL: "https://example.com/report"},
|
||||
}
|
||||
for _, u := range []string{"http://example.com", "javascript:alert(1)", "//example.com", "https:///missing", "https://user:secret@example.com", "https://bad_host.test", "https://-bad.test", "https://999.999.999.999/", "https://example.com/%00", "https://example.com/?q=%0a", "https://example.com/?q=%zz", "https://example.com:99999", "https://example.com/\x00", "https://example.com/" + strings.Repeat("x", MaxCitationURLBytes)} {
|
||||
raw = append(raw, Citation{Title: "invalid", URL: u})
|
||||
}
|
||||
raw = append(raw, Citation{Title: strings.Repeat("é", MaxCitationTitleRunes+1), URL: "https://example.org"})
|
||||
got := NormalizeCitations(raw)
|
||||
if len(got) != 2 || got[0] != (Citation{Title: "Useful source", URL: "https://example.com/report"}) || len([]rune(got[1].Title)) != MaxCitationTitleRunes {
|
||||
t.Fatalf("unsafe or unnormalized citations: %+v", got)
|
||||
}
|
||||
many := []Citation{}
|
||||
for i := 0; i < MaxCitations+10; i++ {
|
||||
many = append(many, Citation{Title: "source", URL: "https://example.com/" + strings.Repeat("x", i)})
|
||||
}
|
||||
if len(NormalizeCitations(many)) != MaxCitations {
|
||||
t.Fatal("unbounded citations")
|
||||
}
|
||||
if NormalizeCitations(nil) == nil {
|
||||
t.Fatal("empty citations must encode as []")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket60CitationsRejectLocalHostsAndIPLiterals(t *testing.T) {
|
||||
for _, host := range []string{
|
||||
"127.0.0.1", "127.1.2.3", "0.0.0.0", "10.0.0.1", "172.16.0.1", "172.31.255.254", "192.168.1.1", "169.254.169.254",
|
||||
"[::1]", "[::]", "[fc00::1]", "[fd12:3456::1]", "[fe80::1]", "[::ffff:127.0.0.1]",
|
||||
"localhost", "LOCALHOST:443", "app.localhost", "host.local", "host.LOCAL:8443", "printer", "intranet", "host.localdomain", "host.internal", "host.lan", "host.home", "host.corp", "host.intranet", "router.home.arpa",
|
||||
"host.test", "host.invalid", "host.example", "host.onion", "host.alt",
|
||||
"2130706433", "0177.0.0.1", "0x7f000001", "0x7f.0x0.0x0.0x1", "127.1",
|
||||
// The policy rejects every literal IP, including public addresses.
|
||||
"8.8.8.8", "[2606:4700:4700::1111]",
|
||||
} {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
got := NormalizeCitations([]Citation{{Title: "source", URL: "https://" + host + "/report"}})
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("non-public host exposed as a citation: %s", host)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, host := range []string{"www.reuters.com", "news.bbc.co.uk", "example.com", "xn--bcher-kva.de", "localhost.example.com"} {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
if got := NormalizeCitations([]Citation{{Title: "source", URL: "https://" + host + "/report"}}); len(got) != 1 {
|
||||
t.Fatal("qualified DNS host rejected", host)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -9,14 +9,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("qa_not_found")
|
||||
ErrInvalid = errors.New("qa_invalid_request")
|
||||
ErrConflict = errors.New("qa_idempotency_conflict")
|
||||
ErrBusy = errors.New("qa_question_in_progress")
|
||||
ErrLimit = errors.New("qa_context_limit")
|
||||
ErrModel = errors.New("qa_model_unavailable")
|
||||
ErrProvider = errors.New("qa_provider_failed")
|
||||
ErrRateLimited = errors.New("qa_rate_limited")
|
||||
ErrWebSearchUnsupported = errors.New("web_search_unsupported")
|
||||
ErrNotFound = errors.New("qa_not_found")
|
||||
ErrInvalid = errors.New("qa_invalid_request")
|
||||
ErrConflict = errors.New("qa_idempotency_conflict")
|
||||
ErrBusy = errors.New("qa_question_in_progress")
|
||||
ErrLimit = errors.New("qa_context_limit")
|
||||
ErrModel = errors.New("qa_model_unavailable")
|
||||
ErrProvider = errors.New("qa_provider_failed")
|
||||
ErrRateLimited = errors.New("qa_rate_limited")
|
||||
)
|
||||
|
||||
const CallsPerMinute = 10
|
||||
@ -38,22 +39,27 @@ const MaxContextBytes = 128000
|
||||
const PendingLifetime = 2 * time.Minute
|
||||
|
||||
type Question struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
UseWebSearch bool `json:"use_web_search"`
|
||||
RequestID string `json:"request_id"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
}
|
||||
type Turn struct {
|
||||
ID string `json:"id"`
|
||||
RequestID string `json:"request_id"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
Status string `json:"status"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Replayed bool `json:"replayed"`
|
||||
UseWebSearch bool `json:"use_web_search"`
|
||||
WebSearchPerformed bool `json:"web_search_performed"`
|
||||
Citations []Citation `json:"citations"`
|
||||
ID string `json:"id"`
|
||||
RequestID string `json:"request_id"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
Status string `json:"status"`
|
||||
FailureCode string `json:"failure_code,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Replayed bool `json:"replayed"`
|
||||
}
|
||||
type Conversation struct {
|
||||
WebSearchSupported bool `json:"web_search_supported"`
|
||||
ID string `json:"id"`
|
||||
SummaryID string `json:"summary_id"`
|
||||
Mode string `json:"mode"`
|
||||
@ -72,20 +78,22 @@ type Exchange struct {
|
||||
|
||||
// StatelessRequest deliberately has no article/source field.
|
||||
type StatelessRequest struct {
|
||||
Summary string `json:"summary"`
|
||||
Language string `json:"language"`
|
||||
History []Exchange `json:"history"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
UseWebSearch bool `json:"use_web_search,omitempty"`
|
||||
Summary string `json:"summary"`
|
||||
Language string `json:"language"`
|
||||
History []Exchange `json:"history"`
|
||||
Passage string `json:"passage"`
|
||||
Question string `json:"question"`
|
||||
}
|
||||
type Capabilities struct {
|
||||
Create bool `json:"create"`
|
||||
Continue bool `json:"continue"`
|
||||
Purge bool `json:"purge"`
|
||||
WebSearch bool `json:"web_search"`
|
||||
Create bool `json:"create"`
|
||||
Continue bool `json:"continue"`
|
||||
Purge bool `json:"purge"`
|
||||
}
|
||||
type Provider interface {
|
||||
Capabilities() Capabilities
|
||||
Answer(context.Context, StatelessRequest) (string, error)
|
||||
Answer(context.Context, StatelessRequest) (AnswerResult, error)
|
||||
}
|
||||
|
||||
// A future session adapter must implement all three lifecycle operations before
|
||||
@ -111,7 +119,7 @@ type Repository interface {
|
||||
CancelQAQuestion(context.Context, string, string, string) error
|
||||
GetQAConversation(context.Context, string, string, time.Time) (Conversation, error)
|
||||
BeginQAQuestion(context.Context, string, string, Question, time.Time) (Work, error)
|
||||
FinishQAQuestion(context.Context, string, string, string, string, string, time.Time) (Turn, error)
|
||||
FinishQAQuestion(context.Context, string, string, string, AnswerResult, string, time.Time) (Turn, error)
|
||||
DeleteQAConversation(context.Context, string, string) error
|
||||
PurgeExpiredQASources(context.Context, time.Time) error
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ func (r *quota55Repo) CancelQAQuestion(context.Context, string, string, string)
|
||||
type quota55PanicProvider struct{}
|
||||
|
||||
func (quota55PanicProvider) Capabilities() Capabilities { return Capabilities{} }
|
||||
func (quota55PanicProvider) Answer(context.Context, StatelessRequest) (string, error) {
|
||||
func (quota55PanicProvider) Answer(context.Context, StatelessRequest) (AnswerResult, error) {
|
||||
panic("PRIVATE_PROVIDER_PAYLOAD")
|
||||
}
|
||||
func TestTicket55DeniedAdmissionIsRetryableWithoutFinishingOrCallingProvider(t *testing.T) {
|
||||
|
||||
@ -18,7 +18,13 @@ func New(repo Repository, resolver Resolver) *Service {
|
||||
return &Service{Repo: repo, Resolver: resolver, Clock: time.Now}
|
||||
}
|
||||
func (s *Service) Get(ctx context.Context, owner, summary string) (Conversation, error) {
|
||||
return s.Repo.GetQAConversation(ctx, owner, summary, s.Clock())
|
||||
c, err := s.Repo.GetQAConversation(ctx, owner, summary, s.Clock())
|
||||
if err == nil && s.Resolver != nil {
|
||||
if p, resolveErr := s.Resolver.ResolveQA(ctx, owner, summary); resolveErr == nil && p != nil {
|
||||
c.WebSearchSupported = p.Capabilities().WebSearch
|
||||
}
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
func (s *Service) Delete(ctx context.Context, owner, summary string) error {
|
||||
return s.Repo.DeleteQAConversation(ctx, owner, summary)
|
||||
@ -41,6 +47,8 @@ func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (T
|
||||
switch work.Turn.FailureCode {
|
||||
case "qa_model_unavailable":
|
||||
return work.Turn, ErrModel
|
||||
case "web_search_unsupported":
|
||||
return work.Turn, ErrWebSearchUnsupported
|
||||
case "qa_context_limit":
|
||||
return work.Turn, ErrLimit
|
||||
default:
|
||||
@ -49,19 +57,24 @@ func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (T
|
||||
}
|
||||
return work.Turn, nil
|
||||
}
|
||||
finish := func(answer, code string) (Turn, error) {
|
||||
finish := func(answer AnswerResult, code string) (Turn, error) {
|
||||
// Persist a bounded terminal result even when the HTTP request is canceled.
|
||||
saveCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
return s.Repo.FinishQAQuestion(saveCtx, owner, summary, work.Turn.ID, answer, code, s.Clock())
|
||||
}
|
||||
request := StatelessRequest{Summary: work.Summary, Language: work.Language, History: work.History, Passage: q.Passage, Question: q.Question}
|
||||
request := StatelessRequest{Summary: work.Summary, Language: work.Language, History: work.History, Passage: q.Passage, Question: q.Question, UseWebSearch: q.UseWebSearch}
|
||||
// Web-enabled requests deliberately exclude the full synthesis and history.
|
||||
if q.UseWebSearch {
|
||||
request.Summary = ""
|
||||
request.History = nil
|
||||
}
|
||||
size := len(request.Summary) + len(request.Passage) + len(request.Question)
|
||||
for _, turn := range request.History {
|
||||
size += len(turn.Passage) + len(turn.Question) + len(turn.Answer)
|
||||
}
|
||||
if size > MaxContextBytes {
|
||||
if _, err = finish("", ErrLimit.Error()); err != nil {
|
||||
if _, err = finish(AnswerResult{}, ErrLimit.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrLimit
|
||||
@ -73,11 +86,17 @@ func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (T
|
||||
err = ErrModel
|
||||
}
|
||||
if err != nil || provider == nil {
|
||||
if _, err = finish("", ErrModel.Error()); err != nil {
|
||||
if _, err = finish(AnswerResult{}, ErrModel.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrModel
|
||||
}
|
||||
if q.UseWebSearch && !provider.Capabilities().WebSearch {
|
||||
if _, err = finish(AnswerResult{}, ErrWebSearchUnsupported.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrWebSearchUnsupported
|
||||
}
|
||||
callCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||||
defer cancel()
|
||||
permit, admissionErr := s.Repo.AcquireQAProviderCall(callCtx, owner, summary, work.Turn.ID)
|
||||
@ -92,21 +111,29 @@ func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (T
|
||||
}
|
||||
// Release immediately after Answer returns, before persistence: the permit
|
||||
// represents an actual provider call, not the pending turn or an HTTP timeout.
|
||||
answer, providerErr := func() (answer string, err error) {
|
||||
answer, providerErr := func() (answer AnswerResult, err error) {
|
||||
defer permit.Release()
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
answer = ""
|
||||
answer = AnswerResult{}
|
||||
err = ErrProvider
|
||||
}
|
||||
}()
|
||||
return provider.Answer(callCtx, request)
|
||||
}()
|
||||
if providerErr != nil || callCtx.Err() != nil || strings.TrimSpace(answer) == "" || !utf8.ValidString(answer) || utf8.RuneCountInString(answer) > MaxAnswerRunes {
|
||||
if _, err = finish("", ErrProvider.Error()); err != nil {
|
||||
if providerErr != nil || callCtx.Err() != nil || strings.TrimSpace(answer.Answer) == "" || !utf8.ValidString(answer.Answer) || strings.ContainsRune(answer.Answer, 0) || utf8.RuneCountInString(answer.Answer) > MaxAnswerRunes {
|
||||
if _, err = finish(AnswerResult{}, ErrProvider.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrProvider
|
||||
}
|
||||
if !q.UseWebSearch {
|
||||
answer.WebSearchPerformed = false
|
||||
}
|
||||
if answer.WebSearchPerformed {
|
||||
answer.Citations = NormalizeCitations(answer.Citations)
|
||||
} else {
|
||||
answer.Citations = []Citation{}
|
||||
}
|
||||
return finish(answer, "")
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ type fixtureRepo struct {
|
||||
work Work
|
||||
begins, finishes int
|
||||
answer, code string
|
||||
result AnswerResult
|
||||
err error
|
||||
}
|
||||
|
||||
@ -23,11 +24,12 @@ func (r *fixtureRepo) BeginQAQuestion(context.Context, string, string, Question,
|
||||
r.begins++
|
||||
return r.work, r.err
|
||||
}
|
||||
func (r *fixtureRepo) FinishQAQuestion(_ context.Context, _, _, _, answer, code string, _ time.Time) (Turn, error) {
|
||||
func (r *fixtureRepo) FinishQAQuestion(_ context.Context, _, _, _ string, answer AnswerResult, code string, _ time.Time) (Turn, error) {
|
||||
r.finishes++
|
||||
r.answer = answer
|
||||
r.answer = answer.Answer
|
||||
r.result = answer
|
||||
r.code = code
|
||||
return Turn{Answer: answer, FailureCode: code}, nil
|
||||
return Turn{Answer: answer.Answer, WebSearchPerformed: answer.WebSearchPerformed, Citations: answer.Citations, FailureCode: code}, nil
|
||||
}
|
||||
func (r *fixtureRepo) DeleteQAConversation(context.Context, string, string) error { return nil }
|
||||
func (r *fixtureRepo) PurgeExpiredQASources(context.Context, time.Time) error { return nil }
|
||||
@ -39,9 +41,9 @@ type fixtureProvider struct {
|
||||
}
|
||||
|
||||
func (p *fixtureProvider) Capabilities() Capabilities { return Capabilities{} }
|
||||
func (p *fixtureProvider) Answer(_ context.Context, r StatelessRequest) (string, error) {
|
||||
func (p *fixtureProvider) Answer(_ context.Context, r StatelessRequest) (AnswerResult, error) {
|
||||
p.requests = append(p.requests, r)
|
||||
return p.answer, p.err
|
||||
return AnswerResult{Answer: p.answer}, p.err
|
||||
}
|
||||
|
||||
type fixtureResolver struct {
|
||||
|
||||
51
backend/internal/summaryqa/web_search_ticket60_test.go
Normal file
51
backend/internal/summaryqa/web_search_ticket60_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package summaryqa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type web60ServiceProvider struct {
|
||||
requests []StatelessRequest
|
||||
result AnswerResult
|
||||
}
|
||||
|
||||
func (*web60ServiceProvider) Capabilities() Capabilities { return Capabilities{WebSearch: true} }
|
||||
func (p *web60ServiceProvider) Answer(_ context.Context, r StatelessRequest) (AnswerResult, error) {
|
||||
p.requests = append(p.requests, r)
|
||||
return p.result, nil
|
||||
}
|
||||
|
||||
func TestTicket60ServiceProjectsMinimalWebContextAndNormalizesCitations(t *testing.T) {
|
||||
p := &web60ServiceProvider{result: AnswerResult{Answer: "answer", WebSearchPerformed: true, Citations: []Citation{{Title: " source ", URL: "https://EXAMPLE.COM:443/"}, {Title: "duplicate", URL: "https://example.com/"}}}}
|
||||
r := &fixtureRepo{work: Work{Summary: strings.Repeat("PRIVATE_SUMMARY", MaxContextBytes), History: []Exchange{{Answer: strings.Repeat("PRIVATE_HISTORY", MaxContextBytes)}}, Language: "fr", Turn: Turn{ID: "turn"}}}
|
||||
s := New(r, &fixtureResolver{p: p})
|
||||
_, err := s.Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000006001", Passage: "p", Question: "q", UseWebSearch: true})
|
||||
if err != nil || len(p.requests) != 1 || p.requests[0].Summary != "" || len(p.requests[0].History) != 0 || !p.requests[0].UseWebSearch || r.finishes != 1 || len(r.result.Citations) != 1 {
|
||||
t.Fatalf("non-minimal request or invalid result: %v %+v", err, p.requests)
|
||||
}
|
||||
}
|
||||
func TestTicket60UnsupportedSearchNeverAcquiresQuotaAndReplaysSameFailure(t *testing.T) {
|
||||
r := "a55Repo{fixtureRepo: fixtureRepo{work: Work{Turn: Turn{ID: "turn"}}}}
|
||||
p := &fixtureProvider{answer: "must not be used"}
|
||||
resolver := &fixtureResolver{p: p}
|
||||
s := New(r, resolver)
|
||||
q := Question{RequestID: "00000000-0000-0000-0000-000000006001", Passage: "p", Question: "q", UseWebSearch: true}
|
||||
if _, err := s.Ask(context.Background(), "owner", "summary", q); !errors.Is(err, ErrWebSearchUnsupported) || r.admissions != 0 || len(p.requests) != 0 || r.code != "web_search_unsupported" {
|
||||
t.Fatal("unsupported search consumed provider quota or fell back", err)
|
||||
}
|
||||
r.work = Work{Replay: true, Turn: Turn{Status: "failed", FailureCode: "web_search_unsupported"}}
|
||||
if _, err := s.Ask(context.Background(), "owner", "summary", q); !errors.Is(err, ErrWebSearchUnsupported) || resolver.calls != 1 || r.finishes != 1 {
|
||||
t.Fatal("failed replay called resolver/provider", err)
|
||||
}
|
||||
}
|
||||
func TestTicket60UnrequestedSearchCannotPersistPerformedOrCitations(t *testing.T) {
|
||||
p := &web60ServiceProvider{result: AnswerResult{Answer: "answer", WebSearchPerformed: true, Citations: []Citation{{Title: "source", URL: "https://example.com/"}}}}
|
||||
r := &fixtureRepo{work: Work{Turn: Turn{ID: "turn"}}}
|
||||
_, err := New(r, &fixtureResolver{p: p}).Ask(context.Background(), "o", "s", Question{RequestID: "00000000-0000-0000-0000-000000006001", Passage: "p", Question: "q"})
|
||||
if err != nil || r.result.WebSearchPerformed || r.result.Citations == nil || len(r.result.Citations) != 0 {
|
||||
t.Fatal("unrequested search state persisted", err, r.result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user