Spaces:
Sleeping

File size: 4,700 Bytes
98cf5b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package core

import (
	"fmt"
	"net/netip"
	"strings"
	"time"

	"github.com/archnemix/archx-eval/relay/internal/apierr"
	"github.com/archnemix/archx-eval/relay/internal/identity"
	"github.com/archnemix/archx-eval/relay/internal/store"
)

// FeedbackInput is a benchmark report from a runner.
//
// It is verified rather than trusted. The runner is our code, so the contract is
// closed: an unknown shape is either a version mismatch or not our client, and
// both are refused instead of being coerced into something storable.
type FeedbackInput struct {
	RequestID string
	// IdentityID is what the client claims. It must match the ledger entry, so
	// a runner cannot report results under someone else's identity.
	IdentityID string
	ModelID    string
	RunnerVer  string
	OS         string
	Arch       string
	DurationS  float64
	// ExampleIDs is every question the runner is answering.
	ExampleIDs []string
	// Partial says the runner could not complete every issued question. Without
	// this flag a submission must cover everything that was issued; otherwise a
	// runner could silently drop the questions it did badly on.
	Partial bool

	RemoteAddr netip.Addr
	Headers    map[string][]string
	Device     identity.Device
}

// VerifyFeedback checks a report against the ledger and returns the result
// ready to store.
//
// The checks, cheapest first:
//
//  1. the request id is one we issued            -> invented ids refused
//  2. the identity matches the one it was issued to -> misattribution refused
//  3. it has not already been reported            -> replay refused
//  4. it is inside the feedback window            -> stale reports refused
//  5. every example was actually issued to them   -> fabricated answers refused
//  6. the submission is complete (or says it is not) -> cherry-picking refused
func (s *Service) VerifyFeedback(in FeedbackInput, now time.Time) (store.Result, error) {
	if strings.TrimSpace(in.RequestID) == "" {
		return store.Result{}, apierr.BadRequest("request_id is required: draw first, then report")
	}
	if strings.TrimSpace(in.ModelID) == "" {
		return store.Result{}, apierr.BadRequest("model_id is required")
	}
	if len(in.ExampleIDs) == 0 {
		return store.Result{}, apierr.BadRequest("items may not be empty")
	}

	entry, known := s.store.LeaseLedger(in.RequestID)
	if !known {
		return store.Result{}, apierr.Forbidden("UNKNOWN_REQUEST",
			"this request_id was not issued by this relay; call /v1/draw first")
	}

	// The identity must be the one the draw was issued to. This is what stops a
	// runner reporting results against somebody else's questions.
	if entry.IdentityID != "" && in.IdentityID != "" && entry.IdentityID != in.IdentityID {
		return store.Result{}, apierr.Forbidden("IDENTITY_MISMATCH",
			"this request_id was issued to a different identity")
	}

	if !entry.ReportedAt.IsZero() {
		return store.Result{}, apierr.Conflict("ALREADY_REPORTED",
			fmt.Sprintf("results for this request were already accepted at %s",
				entry.ReportedAt.UTC().Format(time.RFC3339)))
	}

	if win := s.cfg.FeedbackWindow; win > 0 && now.Sub(entry.IssuedAt) > win {
		return store.Result{}, apierr.Forbidden("REPORT_TOO_LATE",
			fmt.Sprintf("this request is older than the %s reporting window", win))
	}

	issued := make(map[string]bool, len(entry.ExampleIDs))
	for _, id := range entry.ExampleIDs {
		issued[id] = true
	}
	seen := make(map[string]bool, len(in.ExampleIDs))
	for _, id := range in.ExampleIDs {
		if seen[id] {
			return store.Result{}, apierr.BadRequest(fmt.Sprintf("duplicate answer for %q", id))
		}
		seen[id] = true
		if !issued[id] {
			return store.Result{}, apierr.Forbidden("QUESTION_NOT_ISSUED",
				fmt.Sprintf("%q was not issued to this identity under this request", id))
		}
	}
	if !in.Partial && len(seen) != len(issued) {
		missing := len(issued) - len(seen)
		return store.Result{}, apierr.BadRequest(fmt.Sprintf(
			"%d issued item(s) are missing from the report; set partial=true to submit an incomplete run",
			missing))
	}

	// Only now is the report accepted, and the ledger entry is closed so a
	// second submission for the same request id is a replay.
	if err := s.store.MarkLedgerReported(in.RequestID, now); err != nil {
		return store.Result{}, apierr.Forbidden("UNKNOWN_REQUEST", "this request_id is no longer valid")
	}

	return store.Result{
		TS:         now,
		RequestID:  in.RequestID,
		IdentityID: entry.IdentityID,
		ModelID:    in.ModelID,
		RunnerVer:  in.RunnerVer,
		OS:         in.OS,
		Arch:       in.Arch,
		Summary: store.ResultSummary{
			Total:     len(seen),
			ByCat:     map[string]float64{},
			DurationS: in.DurationS,
		},
		Partial: in.Partial,
		Issued:  len(issued),
	}, nil
}