From f99600e7b94991ef519b3aad33478a1729d4b43e Mon Sep 17 00:00:00 2001 From: hanlap0503 Date: Fri, 16 Oct 2026 06:41:19 +0000 Subject: [PATCH] fix: simplify rotation manager configuration and add test for model exclusion --- internal/rotation/state.go | 40 +++++---------------------- internal/rotation/state_test.go | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 internal/rotation/state_test.go diff --git a/internal/rotation/state.go b/internal/rotation/state.go index 9f42417..938993b 100644 --- a/internal/rotation/state.go +++ b/internal/rotation/state.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "slices" - "sort" "sync" "time" ) @@ -52,7 +51,6 @@ type Manager struct { mu sync.Mutex path string state Snapshot - cfg config.RotationConfig } func New(path string, cfg config.RotationConfig) (*Manager, error) { @@ -86,19 +84,7 @@ func (m *Manager) Configure(cfg config.RotationConfig) { m.state.Groups = append(m.state.Groups, &Group{ID: id}) } g := m.state.Groups[i] - previous := m.cfg.SOTA - if i == 1 { - previous = m.cfg.Sweet - } - order := slices.Clone(g.Order) - if len(order) == 0 || !slices.Equal(previous.Order, c.Order) { - order = slices.Clone(c.Order) - for _, v := range g.Order { - if !slices.Contains(order, v) { - order = append(order, v) - } - } - } + order := slices.Clone(c.Order) if !slices.Equal(order, g.Order) || g.Alias != c.Alias { g.Generation++ } @@ -107,12 +93,14 @@ func (m *Manager) Configure(cfg config.RotationConfig) { if g.Paused == nil { g.Paused = map[string]time.Time{} } - if !slices.Contains(g.Order, g.Current) && len(g.Order) > 0 { - g.Current = g.Order[0] + if !slices.Contains(g.Order, g.Current) { + g.Current = "" + if len(g.Order) > 0 { + g.Current = g.Order[0] + } g.Generation++ } } - m.cfg = cfg m.state.FirstOutputSeconds = cfg.FirstOutputSeconds m.saveLocked() } @@ -123,24 +111,10 @@ func (m *Manager) Sync(available map[string]bool) { for _, g := range m.state.Groups { g.Available = map[string]bool{} for model := range available { - if config.IsSOTA(model) == (g.ID == "sota") { + if config.IsSOTA(model) == (g.ID == "sota") && slices.Contains(g.Order, model) { g.Available[model] = true } } - added := []string{} - for model := range g.Available { - if !slices.Contains(g.Order, model) { - added = append(added, model) - } - } - sort.Strings(added) - if len(g.Order) == 0 && slices.Contains(added, "glm-5.3-flash") { - added = append([]string{"glm-5.3-flash"}, slices.Delete(added, slices.Index(added, "glm-5.3-flash"), slices.Index(added, "glm-5.3-flash")+1)...) - } - if len(added) > 0 { - g.Order = append(g.Order, added...) - changed = true - } if g.Current == "" && len(g.Order) > 0 { g.Current = g.Order[0] g.Generation++ diff --git a/internal/rotation/state_test.go b/internal/rotation/state_test.go new file mode 100644 index 0000000..39b2b1a --- /dev/null +++ b/internal/rotation/state_test.go @@ -0,0 +1,49 @@ +package rotation + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "testing" + + "opencode2api/internal/config" +) + +func TestConfiguredOrderExcludesDiscoveredAndPersistedModels(t *testing.T) { + path := filepath.Join(t.TempDir(), "rotation.json") + initial := Snapshot{Groups: []*Group{ + {ID: "sota", Alias: "sota", Order: config.SOTAModels, Current: config.SOTAModels[0]}, + {ID: "sweet", Alias: "sweet", Order: []string{"configured-free", "persisted-free"}, Current: "persisted-free"}, + }} + data, err := json.Marshal(initial) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, data, 0o600); err != nil { + t.Fatal(err) + } + + cfg := config.RotationConfig{ + Sweet: config.RotationGroup{Alias: "sweet", Order: []string{"configured-free"}}, + } + manager, err := New(path, cfg) + if err != nil { + t.Fatal(err) + } + manager.Sync(map[string]bool{ + "configured-free": true, + "discovered-free": true, + }) + + sweet := manager.Snapshot().Groups[1] + if !reflect.DeepEqual(sweet.Order, []string{"configured-free"}) { + t.Fatalf("Sweet order = %v, want only configured models", sweet.Order) + } + if sweet.Current != "configured-free" { + t.Fatalf("Sweet current = %q, want configured-free", sweet.Current) + } + if !reflect.DeepEqual(sweet.Available, map[string]bool{"configured-free": true}) { + t.Fatalf("Sweet available = %v, want only configured models", sweet.Available) + } +}