Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions pkgs/mcpx/internal/daemon/routes_feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -92,25 +93,47 @@ func (s *Server) handleInteractionsList(w http.ResponseWriter, r *http.Request)
hasFb := hasFbStr == "1" || strings.ToLower(hasFbStr) == "true"
filter.HasFeedback = &hasFb
}
if minStr := q.Get("minScore"); minStr != "" {
if val, err := strconv.ParseFloat(minStr, 64); err == nil {
filter.MinScore = &val
// Invalid query values are rejected with 400 naming the parameter
// (#2223): silently dropping a bound made a typo'd minScore return the
// unfiltered list with 200, and a NaN/Inf bound (which ParseFloat
// accepts) reached SQLite as NULL and matched nothing — both read as
// real answers. The same silent-drop shape is fixed for limit/offset.
parseScoreBound := func(name string) (*float64, bool) {
raw := q.Get(name)
if raw == "" {
return nil, true
}
}
if maxStr := q.Get("maxScore"); maxStr != "" {
if val, err := strconv.ParseFloat(maxStr, 64); err == nil {
filter.MaxScore = &val
val, err := strconv.ParseFloat(raw, 64)
if err != nil || math.IsNaN(val) || math.IsInf(val, 0) {
writeErr(w, http.StatusBadRequest, fmt.Errorf("invalid %s %q: must be a finite number", name, raw))
return nil, false
}
return &val, true
}
if limitStr := q.Get("limit"); limitStr != "" {
if l, err := strconv.Atoi(limitStr); err == nil {
filter.Limit = l
parseIntParam := func(name string) (int, bool) {
raw := q.Get(name)
if raw == "" {
return 0, true
}
}
if offsetStr := q.Get("offset"); offsetStr != "" {
if o, err := strconv.Atoi(offsetStr); err == nil {
filter.Offset = o
n, err := strconv.Atoi(raw)
if err != nil {
writeErr(w, http.StatusBadRequest, fmt.Errorf("invalid %s %q: must be an integer", name, raw))
return 0, false
}
return n, true
}
var ok bool
if filter.MinScore, ok = parseScoreBound("minScore"); !ok {
return
}
if filter.MaxScore, ok = parseScoreBound("maxScore"); !ok {
return
}
if filter.Limit, ok = parseIntParam("limit"); !ok {
return
}
if filter.Offset, ok = parseIntParam("offset"); !ok {
return
}

list, err := fbStore.List(filter)
Expand Down
112 changes: 112 additions & 0 deletions pkgs/mcpx/internal/daemon/routes_feedback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package daemon

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"

"github.com/dezren39/mcpx/internal/logstore"
)

// setupInteractionsListServer builds a Server backed by a real feedback
// store seeded with three interactions scored 0.2 / 0.5 / 0.9 (#2223).
func setupInteractionsListServer(t *testing.T) *Server {
t.Helper()
fs, err := logstore.NewFeedbackStore(filepath.Join(t.TempDir(), "feedback.db"))
if err != nil {
t.Fatalf("NewFeedbackStore: %v", err)
}
t.Cleanup(func() { _ = fs.Close() })
for i, score := range []float64{0.2, 0.5, 0.9} {
traceID := fmt.Sprintf("trc-2223-%d", i)
rec := logstore.InteractionRecord{
TraceID: traceID,
Timestamp: time.Now(),
Source: "proxy",
Input: "find bad responses",
Output: "an answer",
}
if err := fs.RecordInteraction(rec); err != nil {
t.Fatalf("RecordInteraction: %v", err)
}
if err := fs.ProvideFeedback(logstore.FeedbackParams{TraceID: traceID, Score: score}); err != nil {
t.Fatalf("ProvideFeedback: %v", err)
}
}
return &Server{reg: &Registry{feedbackStore: fs}}
}

func getInteractions(t *testing.T, s *Server, query string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodGet, "/v1/interactions?"+query, nil)
rec := httptest.NewRecorder()
s.handleInteractionsList(rec, req)
return rec
}

// TestInteractionsListRejectsInvalidScoreBounds pins #2223: a non-numeric,
// NaN, infinite, or out-of-range minScore/maxScore must be a 400 naming the
// parameter — never a silently unfiltered (or silently empty) 200.
func TestInteractionsListRejectsInvalidScoreBounds(t *testing.T) {
s := setupInteractionsListServer(t)
for _, param := range []string{"minScore", "maxScore"} {
for _, bad := range []string{"abc", "NaN", "Inf", "+Inf", "-Inf", "1e999"} {
rec := getInteractions(t, s, param+"="+bad)
if rec.Code != http.StatusBadRequest {
t.Errorf("%s=%s: status = %d, want 400 (body %s)", param, bad, rec.Code, rec.Body.String())
continue
}
if !strings.Contains(rec.Body.String(), param) {
t.Errorf("%s=%s: 400 body %q does not name the parameter", param, bad, rec.Body.String())
}
}
}
}

// TestInteractionsListRejectsInvalidLimitOffset pins the same 400 rule for
// this handler's limit/offset, which had the same silent-drop shape.
func TestInteractionsListRejectsInvalidLimitOffset(t *testing.T) {
s := setupInteractionsListServer(t)
for _, query := range []string{"limit=abc", "offset=1.5"} {
rec := getInteractions(t, s, query)
if rec.Code != http.StatusBadRequest {
t.Errorf("%s: status = %d, want 400 (body %s)", query, rec.Code, rec.Body.String())
}
}
}

// TestInteractionsListFiniteScoreBoundsUnchanged is the control: valid
// finite bounds behave exactly as before the 400 rule existed.
func TestInteractionsListFiniteScoreBoundsUnchanged(t *testing.T) {
s := setupInteractionsListServer(t)
cases := []struct {
query string
want int
}{
{"", 3},
{"minScore=0.4", 2},
{"maxScore=0.4", 1},
{"minScore=0.4&maxScore=0.6", 1},
{"limit=2", 2},
{"limit=2&offset=2", 1},
}
for _, tc := range cases {
rec := getInteractions(t, s, tc.query)
if rec.Code != http.StatusOK {
t.Errorf("%q: status = %d, want 200 (body %s)", tc.query, rec.Code, rec.Body.String())
continue
}
var list []logstore.InteractionRecord
if err := json.NewDecoder(rec.Body).Decode(&list); err != nil {
t.Fatalf("%q: decode list: %v", tc.query, err)
}
if len(list) != tc.want {
t.Errorf("%q: got %d rows, want %d", tc.query, len(list), tc.want)
}
}
}
Loading