| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "reasonix/internal/control" |
| 7 | ) |
| 8 | |
| 9 | func (s *Server) admitModelSubmissionLocked(w http.ResponseWriter, r *http.Request, choice *control.ModelApplicationChoice, input string, identity control.SubmissionRequest) bool { |
| 10 | if identified, ok := s.ctl().(*control.Controller); ok && identity.ID != "" && !isServeManagementCommand(input) { |
| 11 | if _, found, err := identified.LookupSubmission(identity); found || err != nil { |
| 12 | if err != nil { |
| 13 | writeSubmissionFailure(w, err) |
| 14 | } else { |
| 15 | w.WriteHeader(http.StatusAccepted) |
| 16 | } |
| 17 | return false |
| 18 | } |
| 19 | } |
| 20 | if choice != nil && choice.Mode != "latest" && choice.Mode != "applied_once" { |
| 21 | http.Error(w, "unsupported model application mode", http.StatusBadRequest) |
| 22 | return false |
| 23 | } |
| 24 | if choice != nil && choice.Mode == "applied_once" { |
| 25 | if s.rejectMirroredForegroundLocked(w) { |
| 26 | return false |
| 27 | } |
| 28 | if err := s.validateAppliedModelChoiceLocked(r.Context(), *choice); err != nil { |
| 29 | s.rejectModelApplication(w, r, err) |
| 30 | return false |
| 31 | } |
| 32 | return true |
| 33 | } |
| 34 | return s.admitModelSettingsRunLocked(w, r) |
| 35 | } |
| 36 | |
| 37 | // A returned error does not prove that durable admission failed. Receipt reads |
| 38 | // and post-admission flushes can fail after execution acquired its identity. |
| 39 | // Only the controller's explicit pre-admission sentinel permits a new ID. |
| 40 | func writeSubmissionFailure(w http.ResponseWriter, err error) { |
| 41 | outcome := "unknown" |
| 42 | if errors.Is(err, control.ErrSubmissionNotAccepted) { |
| 43 | outcome = "not_accepted" |
| 44 | } |
| 45 | w.Header().Set("Content-Type", "application/json") |
| 46 | w.WriteHeader(http.StatusConflict) |
| 47 | writeJSON(w, map[string]any{"message": err.Error(), "data": map[string]any{"submissionOutcome": outcome}}) |
| 48 | } |
| 49 |