ekg m2: rank fallback providers by recent success/error history and record provider outcomes

This commit is contained in:
DBT
2026-03-01 04:27:24 +00:00
parent 0e8d267d78
commit 1d5454066a
2 changed files with 67 additions and 13 deletions

View File

@@ -6,19 +6,22 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
type Event struct {
Time string `json:"time"`
TaskID string `json:"task_id,omitempty"`
Session string `json:"session,omitempty"`
Channel string `json:"channel,omitempty"`
Source string `json:"source,omitempty"`
Status string `json:"status"` // success|error|suppressed
ErrSig string `json:"errsig,omitempty"`
Log string `json:"log,omitempty"`
Time string `json:"time"`
TaskID string `json:"task_id,omitempty"`
Session string `json:"session,omitempty"`
Channel string `json:"channel,omitempty"`
Source string `json:"source,omitempty"`
Status string `json:"status"` // success|error|suppressed
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
ErrSig string `json:"errsig,omitempty"`
Log string `json:"log,omitempty"`
}
type SignalContext struct {
@@ -67,6 +70,8 @@ func (e *Engine) Record(ev Event) {
ev.Channel = strings.TrimSpace(ev.Channel)
ev.Source = strings.TrimSpace(ev.Source)
ev.Status = strings.TrimSpace(strings.ToLower(ev.Status))
ev.Provider = strings.TrimSpace(ev.Provider)
ev.Model = strings.TrimSpace(ev.Model)
if ev.ErrSig == "" && ev.Log != "" {
ev.ErrSig = NormalizeErrorSignature(ev.Log)
}
@@ -169,6 +174,43 @@ var (
reSpace = regexp.MustCompile(`\s+`)
)
func (e *Engine) RankProviders(candidates []string) []string {
if len(candidates) <= 1 || e == nil {
return append([]string(nil), candidates...)
}
events := e.readRecentEvents()
score := map[string]float64{}
for _, c := range candidates {
score[c] = 0
}
for _, ev := range events {
p := strings.TrimSpace(ev.Provider)
if p == "" {
continue
}
if _, ok := score[p]; !ok {
continue
}
switch strings.ToLower(strings.TrimSpace(ev.Status)) {
case "success":
score[p] += 1.0
case "suppressed":
score[p] += 0.2
case "error":
score[p] -= 1.0
}
}
ordered := append([]string(nil), candidates...)
sort.SliceStable(ordered, func(i, j int) bool {
si, sj := score[ordered[i]], score[ordered[j]]
if si == sj {
return ordered[i] < ordered[j]
}
return si > sj
})
return ordered
}
func NormalizeErrorSignature(s string) string {
s = strings.TrimSpace(strings.ToLower(s))
if s == "" {