| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | |
| 12 | "reasonix/internal/boot" |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/control" |
| 15 | ) |
| 16 | |
| 17 | // The injected build is the deterministic interleaving boundary: a second save |
| 18 | // can commit after the first offer is read but before its publication finishes. |
| 19 | func TestModelSettingsSourceFencesOvertakenBuildAndUncertainFinish(t *testing.T) { |
| 20 | for _, scenario := range []string{"overtaken", "failed_build", "lost_finish"} { |
| 21 | t.Run(scenario, func(t *testing.T) { |
| 22 | var mu sync.Mutex |
| 23 | desired := "second" |
| 24 | var requests []config.ModelSettingsSourceRequest |
| 25 | finishFailed := false |
| 26 | var sourceURL string |
| 27 | bundle := func(revision, offerID string) *config.ModelRuntimeSettings { |
| 28 | return &config.ModelRuntimeSettings{ |
| 29 | Revision: revision, OfferID: offerID, SourceToken: "virtual-source", ProxyURL: sourceURL, |
| 30 | Providers: []config.ProviderEntry{{Name: "p", Kind: "openai", BaseURL: "http://localhost", Model: "m"}}, |
| 31 | Credentials: map[string]string{"p": "virtual-model"}, |
| 32 | } |
| 33 | } |
| 34 | source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 | var request config.ModelSettingsSourceRequest |
| 36 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { |
| 37 | http.Error(w, "invalid request", http.StatusBadRequest) |
| 38 | return |
| 39 | } |
| 40 | mu.Lock() |
| 41 | defer mu.Unlock() |
| 42 | requests = append(requests, request) |
| 43 | if scenario == "lost_finish" && request.Mode == "finish" && !finishFailed { |
| 44 | finishFailed = true |
| 45 | http.Error(w, "injected acknowledgement failure", http.StatusServiceUnavailable) |
| 46 | return |
| 47 | } |
| 48 | response := config.ModelSettingsSourceResponse{Version: 1, Revision: desired} |
| 49 | if request.Mode == "prepare" && request.AppliedRevision != desired { |
| 50 | response.Settings, response.Ref = bundle(desired, request.OfferID), "p/m" |
| 51 | } |
| 52 | _ = json.NewEncoder(w).Encode(response) |
| 53 | })) |
| 54 | defer source.Close() |
| 55 | sourceURL = source.URL |
| 56 | old := control.New(control.Options{ModelRef: "p/m", ModelSettingsSourceRevision: "first"}) |
| 57 | s := New(old, NewBroadcaster(), config.ServeConfig{AuthMode: "none"}) |
| 58 | s.SetControllerBuildOptions(boot.Options{ModelSettings: bundle("first", "")}) |
| 59 | defer s.Close() |
| 60 | builds := 0 |
| 61 | s.buildControllerWithOptions = func(_ context.Context, ref string, opts boot.Options) (*control.Controller, error) { |
| 62 | builds++ |
| 63 | if scenario == "failed_build" { |
| 64 | return nil, fmt.Errorf("injected build failure") |
| 65 | } |
| 66 | if scenario == "overtaken" && builds == 1 { |
| 67 | mu.Lock() |
| 68 | desired = "third" |
| 69 | mu.Unlock() |
| 70 | } |
| 71 | return control.New(control.Options{ModelRef: ref, Sink: opts.Sink, ModelSettingsSourceRevision: opts.ModelSettings.Revision}), nil |
| 72 | } |
| 73 | refresh := func() error { |
| 74 | s.bindMu.Lock() |
| 75 | defer s.bindMu.Unlock() |
| 76 | return s.refreshRunModelSettingsLocked(context.Background()) |
| 77 | } |
| 78 | err := refresh() |
| 79 | if scenario == "overtaken" { |
| 80 | if err != nil || builds != 2 || s.managedModels.Revision != "third" { |
| 81 | t.Fatalf("overtaken offer admitted: builds=%d revision=%s err=%v", builds, s.managedModels.Revision, err) |
| 82 | } |
| 83 | } else if err == nil { |
| 84 | t.Fatal("injected failure admitted a new run") |
| 85 | } |
| 86 | if scenario == "failed_build" && (s.ctl() != old || s.managedModels.Revision != "first") { |
| 87 | t.Fatal("failed build replaced the original runtime") |
| 88 | } |
| 89 | if scenario == "lost_finish" { |
| 90 | published := s.ctl() |
| 91 | if s.managedModels.Revision != "second" || s.modelSettingsOfferID == "" { |
| 92 | t.Fatal("uncertain finish lost the published revision or its reservation") |
| 93 | } |
| 94 | if err := refresh(); err != nil || builds != 1 || s.ctl() != published { |
| 95 | t.Fatalf("finish recovery replayed a build: builds=%d err=%v", builds, err) |
| 96 | } |
| 97 | } |
| 98 | if s.modelSettingsOfferID != "" { |
| 99 | t.Fatal("confirmed finish retained the offer") |
| 100 | } |
| 101 | mu.Lock() |
| 102 | defer mu.Unlock() |
| 103 | last := requests[len(requests)-1] |
| 104 | if last.Mode != "finish" || len(last.OwnedRevisions) != 1 || last.OwnedRevisions[0] != s.managedModels.Revision { |
| 105 | t.Fatalf("finish did not report actual ownership: %+v", last) |
| 106 | } |
| 107 | if scenario == "lost_finish" && requests[2].PreviousOfferID != requests[0].OfferID { |
| 108 | t.Fatal("recovery did not release the unacknowledged reservation") |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 |