fix(ai): diagnose summary provider failures (#37)
This commit is contained in:
110
backend/internal/ai/summary_diagnostics.go
Normal file
110
backend/internal/ai/summary_diagnostics.go
Normal file
@ -0,0 +1,110 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
// SummaryDiagnostic contains only bounded categories and numeric aggregates.
|
||||
// Never add model inputs, outputs, errors, identities or configuration here.
|
||||
type SummaryDiagnostic struct {
|
||||
Category string `json:"category"`
|
||||
HTTPStatus int `json:"http_status"`
|
||||
NetworkCategory string `json:"network_category"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
PromptBytes int `json:"prompt_bytes"`
|
||||
PromptBudgetBytes int `json:"prompt_budget_bytes"`
|
||||
ArticleCount int `json:"article_count"`
|
||||
TruncatedArticles int `json:"truncated_articles"`
|
||||
}
|
||||
|
||||
// SummaryDiagnosticCollector is optional. Install during composition, before use.
|
||||
// Returned errors and panics are ignored; collectors should return promptly.
|
||||
type SummaryDiagnosticCollector func(SummaryDiagnostic) error
|
||||
|
||||
func (p *Pipeline) SetSummaryDiagnosticCollector(collector SummaryDiagnosticCollector) {
|
||||
p.summaryDiagnostics = collector
|
||||
}
|
||||
func emitSummaryDiagnostic(collector SummaryDiagnosticCollector, d SummaryDiagnostic) {
|
||||
if collector == nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = recover() }()
|
||||
_ = collector(d)
|
||||
}
|
||||
|
||||
// LogSummaryDiagnostic is an explicit opt-in collector; it never receives an error.
|
||||
func LogSummaryDiagnostic(d SummaryDiagnostic) error {
|
||||
log.Printf("summary diagnostics category=%s http_status=%d network_category=%s duration_ms=%d prompt_bytes=%d prompt_budget_bytes=%d article_count=%d truncated_articles=%d", d.Category, d.HTTPStatus, d.NetworkCategory, d.DurationMS, d.PromptBytes, d.PromptBudgetBytes, d.ArticleCount, d.TruncatedArticles)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Preserve the original provider error and its chain, while exposing only status
|
||||
// to diagnostics. Do not extract categories by parsing error messages/bodies.
|
||||
type summaryHTTPError struct {
|
||||
status int
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *summaryHTTPError) Error() string { return e.err.Error() }
|
||||
func (e *summaryHTTPError) Unwrap() error { return e.err }
|
||||
|
||||
func classifySummaryDiagnostic(ctx context.Context, err error, result string, d *SummaryDiagnostic) {
|
||||
var timeout interface{ Timeout() bool }
|
||||
switch {
|
||||
case errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded):
|
||||
d.Category = "timeout"
|
||||
return
|
||||
case errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled):
|
||||
d.Category = "canceled"
|
||||
return
|
||||
case errors.As(err, &timeout) && timeout.Timeout():
|
||||
d.Category = "timeout"
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
d.Category = "success"
|
||||
if strings.TrimSpace(result) == "" {
|
||||
d.Category = "empty_response"
|
||||
}
|
||||
return
|
||||
}
|
||||
d.Category = "provider_response_error"
|
||||
var local *summaryHTTPError
|
||||
var api *openai.APIError
|
||||
var request *openai.RequestError
|
||||
status := 0
|
||||
switch {
|
||||
case errors.As(err, &local):
|
||||
status = local.status
|
||||
case errors.As(err, &api):
|
||||
status = api.HTTPStatusCode
|
||||
case errors.As(err, &request):
|
||||
status = request.HTTPStatusCode
|
||||
}
|
||||
if status >= 100 && status <= 599 {
|
||||
d.Category = "provider_http_error"
|
||||
d.HTTPStatus = status
|
||||
return
|
||||
}
|
||||
var dns *net.DNSError
|
||||
var op *net.OpError
|
||||
var u *url.Error
|
||||
switch {
|
||||
case errors.As(err, &dns):
|
||||
d.NetworkCategory = "dns"
|
||||
case errors.As(err, &op):
|
||||
d.NetworkCategory = "connection"
|
||||
case errors.As(err, &u):
|
||||
d.NetworkCategory = "transport"
|
||||
}
|
||||
if d.NetworkCategory != "" {
|
||||
d.Category = "provider_http_error"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user