feat(models): add passive availability tracking and opt-in probes (#171)

* feat(models): add passive availability tracking and opt-in probes

* test(models): harden passive availability telemetry and catalog refresh overlap protection

---------

Co-authored-by: NekomyaDev <neko@nekomya.dev>
This commit is contained in:
NEKOMYAandNekomyaDev authored and GitHub committed 2026-11-05 11:41:52 +08:00
1 parent 6fbe12c4df
commit cee32f7047
14 files changed
+504 -41

No files matched your search

+50 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
@@ -220,7 +221,55 @@ func (s *AvailabilityRegistry) Record(model string, generation uint64, success b
return s.saveLocked()
}
// saveLocked rewrites the registry file through a temporary file and a rename.
// RecordPassiveObservation updates model availability based on real user traffic.
// Unlike active synthetic probes, it never issues upstream requests.
// 2xx marks model available. Generic HTTP 400, 404, 429, 403, 5xx, timeouts
// and transport errors never disable the model. Only an explicit, authoritative
// model-unavailable response (isModelUnavailable: model_not_found, model_retired, etc.)
// can mark a model as disabled.
func (s *AvailabilityRegistry) RecordPassiveObservation(model string, success bool, status int, body string, channel string, now time.Time) error {
s.mu.Lock()
defer s.mu.Unlock()
item := s.items[model]
if item.Manual {
return nil
}
item.Model = model
item.ProbeVersion = 1
item.CheckedAt = now
item.Channel = channel
if success {
item.Disabled = false
item.Reason = "passive_traffic_succeeded"
item.NextCheck = now.Add(FreeModelCheckInterval)
} else if IsModelUnavailableReason(status, body) {
item.Disabled = true
item.Reason = "model_unavailable"
item.NextCheck = now.Add(FailedModelCheckInterval)
}
s.items[model] = item
return s.saveLocked()
}
// IsModelUnavailableReason reports whether status and response body contain authoritative
// evidence that the model is nonexistent or retired.
func IsModelUnavailableReason(status int, body string) bool {
if status != 400 && status != 404 && status != 410 && status != 422 {
return false
}
text := strings.ToLower(body)
for _, marker := range []string{
"model_not_found", "model_not_available", "model_retired",
"model_deprecated", "unknown model", "model does not exist",
"model has been retired", "model is no longer available",
} {
if strings.Contains(text, marker) {
return true
}
}
return false
}
// It is a no-op without a path. The caller holds s.mu for writing.
func (s *AvailabilityRegistry) saveLocked() error {
if s.path == "" {