Spaces:
Sleeping

File size: 22,965 Bytes
0a855cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package core

import (
	"context"
	"fmt"
	"net/netip"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/archnemix/archx-eval/relay/internal/bank"
	"github.com/archnemix/archx-eval/relay/internal/config"
	"github.com/archnemix/archx-eval/relay/internal/identity"
	"github.com/archnemix/archx-eval/relay/internal/ratelog"
	"github.com/archnemix/archx-eval/relay/internal/reputation"
	"github.com/archnemix/archx-eval/relay/internal/store"
)

type harness struct {
	svc   *Service
	bankM *bank.Manager
	cfg   config.Config
	now   time.Time
	store *store.Store
	rates *ratelog.Log
}

func newHarness(t *testing.T, examplesPerCat int) *harness {
	t.Helper()
	dir := t.TempDir()
	bankDir := filepath.Join(dir, "bank")
	if err := os.MkdirAll(filepath.Join(bankDir, "python"), 0o750); err != nil {
		t.Fatal(err)
	}
	var b strings.Builder
	for i := 0; i < examplesPerCat; i++ {
		fmt.Fprintf(&b, `{"id":"py-%03d","category":"python","difficulty":"easy","kind":"qa","prompt":"q%d","expected_answer":"a"}`+"\n", i, i)
	}
	if err := os.WriteFile(filepath.Join(bankDir, "python", "easy.jsonl"), []byte(b.String()), 0o640); err != nil {
		t.Fatal(err)
	}

	cfg := config.Config{
		BankDir:        bankDir,
		DataDir:        filepath.Join(dir, "data"),
		RateDir:        filepath.Join(dir, "rate"),
		DrawMax:        75,
		StaleAfterUses: 3,
		Window:         time.Hour,
		RateAllowance:  0.5,
		RateWindow:     time.Minute,
		RateSample:     time.Second,
		// A floor keeps two rapid test requests from looking like a flood; the
		// production default is 20.
		RateFloor:      10,
		BanWindow:      24 * time.Hour,
		MaxBan:         14 * 24 * time.Hour,
		FeedbackWindow: 24 * time.Hour,
		LedgerKeepDays: 7,
		SaltSecret:     "test-salt",
		AllowLoopback:  true,
		AccessToken:    "test-access-token",
		NetWindow:      time.Hour,
		NetMaxDraws:    1000,
		GlobalWindow:   time.Hour,
		GlobalMax:      100000,
	}
	st, err := store.Open(cfg.DataDir)
	if err != nil {
		t.Fatalf("store: %v", err)
	}
	bm, err := bank.Load(bankDir, cfg.StaleAfterUses)
	if err != nil {
		t.Fatalf("bank: %v", err)
	}
	rep, err := reputation.Load("")
	if err != nil {
		t.Fatalf("reputation: %v", err)
	}
	rates, err := ratelog.Open(cfg.RateDir, 96*time.Hour)
	if err != nil {
		t.Fatalf("ratelog: %v", err)
	}
	t.Cleanup(func() { _ = rates.Close() })
	svc := New(cfg, st, bm, rep, rates)
	h := &harness{svc: svc, bankM: bm, cfg: cfg, now: time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC), store: st, rates: rates}
	svc.SetClock(func() time.Time { return h.now })
	rates.SetClock(func() time.Time { return h.now })
	return h
}

func (h *harness) advance(d time.Duration) { h.now = h.now.Add(d) }

// reopen simulates a Space restart: fresh store, bank and service over the same
// directories, which is what the relay does on every wake.
func (h *harness) reopen() *harness {
	st, err := store.Open(h.cfg.DataDir)
	if err != nil {
		panic("reopen store: " + err.Error())
	}
	bm, err := bank.Load(h.cfg.BankDir, h.cfg.StaleAfterUses)
	if err != nil {
		panic("reopen bank: " + err.Error())
	}
	rep, _ := reputation.Load("")
	// Opened on the harness clock: load applies the retention cutoff, and the
	// harness date is frozen, so the wall clock would discard everything.
	now := h.now
	rates, err := ratelog.OpenAt(h.cfg.RateDir, 96*time.Hour, func() time.Time { return now })
	if err != nil {
		panic("reopen ratelog: " + err.Error())
	}
	svc := New(h.cfg, st, bm, rep, rates)
	h2 := &harness{svc: svc, bankM: bm, cfg: h.cfg, now: h.now, store: st, rates: rates}
	svc.SetClock(func() time.Time { return h2.now })
	rates.SetClock(func() time.Time { return h2.now })
	return h2
}

// draws counts calls so every helper-built draw gets its own request id, the
// way a real client does. A fixed id is refused now that a repeated one is
// treated as a collision.
var draws int

func input(ip, machine, disk string) DrawInput {
	draws++
	return DrawInput{
		RequestID:  fmt.Sprintf("req-%d", draws),
		Categories: []string{"python"},
		Size:       10,
		Device: identity.Device{
			MachineID: machine,
			DiskUUID:  disk,
			OSFamily:  "linux",
			Arch:      "x86_64",
		},
		RemoteAddr: netip.MustParseAddr(ip),
		Headers:    map[string][]string{},
	}
}

const (
	machA = "4c4c4544-0044-3510-8054-b4c04f4e4e32"
	machB = "5d5d4544-0055-3510-8054-b4c04f4e4e33"
	diskA = "9f1b2c3d4e5f60718293a4b5c6d7e8f9"
	diskB = "aa2b3c4d5e6f708192a3b4c5d6e7f809"
)

func TestNewIdentityGetsDraw(t *testing.T) {
	h := newHarness(t, 50)
	g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err != nil {
		t.Fatalf("draw: %v", err)
	}
	if len(g.Examples) != 10 {
		t.Fatalf("expected 10 examples, got %d", len(g.Examples))
	}
	if g.IdentityID == "" {
		t.Fatal("expected an identity id")
	}
	if !g.NextDrawAt.After(g.IssuedAt) {
		t.Fatal("next draw must be in the future")
	}
}

func TestSameIdentityBlockedForOneHour(t *testing.T) {
	h := newHarness(t, 50)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("first draw: %v", err)
	}
	h.advance(20 * time.Minute)
	_, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err == nil {
		t.Fatal("expected the second draw to be refused")
	}
	if !strings.Contains(err.Error(), "WINDOW_ACTIVE") {
		t.Fatalf("expected WINDOW_ACTIVE, got %v", err)
	}
}

func TestChangingOneMarkerStillMatches(t *testing.T) {
	// The whole point of marker-set matching: rotating the IP alone is not a
	// new person.
	h := newHarness(t, 50)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("first draw: %v", err)
	}
	h.advance(5 * time.Minute)
	_, err := h.svc.RequestDraw(context.Background(), input("198.51.100.99", machA, diskA))
	if err == nil {
		t.Fatal("same machine_id must still be the same person")
	}

	// Same story for the disk, on a fresh IP and machine id.
	h2 := newHarness(t, 50)
	if _, err := h2.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("first draw: %v", err)
	}
	h2.advance(5 * time.Minute)
	if _, err := h2.svc.RequestDraw(context.Background(), input("198.51.100.99", machB, diskA)); err == nil {
		t.Fatal("shared disk uuid must still match")
	}
}

func TestAllMarkersNewIsNewPerson(t *testing.T) {
	h := newHarness(t, 50)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("first draw: %v", err)
	}
	h.advance(5 * time.Minute)
	// Every marker changed: this counts as a genuinely new caller.
	if _, err := h.svc.RequestDraw(context.Background(), input("198.51.100.99", machB, diskB)); err != nil {
		t.Fatalf("fully rotated identity should be allowed: %v", err)
	}
}

func TestWindowExpiryAllowsSecondDraw(t *testing.T) {
	h := newHarness(t, 50)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("first: %v", err)
	}
	h.advance(61 * time.Minute)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatalf("draw after window should succeed: %v", err)
	}
}

func TestSizeCapEnforced(t *testing.T) {
	h := newHarness(t, 200)
	in := input("203.0.113.10", machA, diskA)
	in.Size = 76
	if _, err := h.svc.RequestDraw(context.Background(), in); err == nil {
		t.Fatal("expected size above DrawMax to fail")
	}
	in.Size = 75
	if _, err := h.svc.RequestDraw(context.Background(), in); err != nil {
		t.Fatalf("75 should be allowed: %v", err)
	}
}

func TestBurstSpamBans(t *testing.T) {
	h := newHarness(t, 200)
	var lastErr error
	for i := 0; i < 20; i++ {
		in := input("203.0.113.10", machA, diskA)
		in.RequestID = fmt.Sprintf("req-%d", i)
		h.advance(200 * time.Millisecond)
		if _, err := h.svc.RequestDraw(context.Background(), in); err != nil {
			lastErr = err
			if strings.Contains(err.Error(), "SPAM") {
				break
			}
		}
	}
	if lastErr == nil || !strings.Contains(lastErr.Error(), "SPAM") {
		t.Fatalf("expected a spam ban, got %v", lastErr)
	}
}

// A ban is stamped with a real duration and enforced until it expires, not just
// for the length of the draw window. The old version of this test asserted a
// fixed multi-hour ban; bans are now proportional to the offence, so a modest
// overshoot earns minutes and the test checks the ban is actually in force
// rather than checking a particular number.
func TestBanEnforcedUntilItExpires(t *testing.T) {
	h := newHarness(t, 200)
	for i := 0; i < 20; i++ {
		in := input("203.0.113.10", machA, diskA)
		in.RequestID = fmt.Sprintf("r%d", i)
		h.advance(200 * time.Millisecond)
		h.svc.RequestDraw(context.Background(), in)
	}

	// Partway through the ban the caller is still refused, and the refusal
	// reports roughly the time left rather than the original duration.
	h.advance(5 * time.Minute)
	_, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err == nil {
		t.Fatal("expected a refusal during the ban")
	}
	if !strings.Contains(err.Error(), "SPAM_RATE") && !strings.Contains(err.Error(), "BANNED") {
		t.Fatalf("expected a ban refusal, got %v", err)
	}
}

// After the ban expires the caller is served again, which is what makes a short
// proportional ban acceptable: it is a delay, not a life sentence.
func TestShortBanExpiresAndAllows(t *testing.T) {
	h := newHarness(t, 200)
	for i := 0; i < 20; i++ {
		in := input("203.0.113.11", machA, diskA)
		in.RequestID = fmt.Sprintf("r%d", i)
		h.advance(200 * time.Millisecond)
		h.svc.RequestDraw(context.Background(), in)
	}
	// Past both the ban and the draw window.
	h.advance(2 * time.Hour)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.11", machA, diskA)); err != nil {
		t.Fatalf("an expired ban must stop blocking: %v", err)
	}
}

func TestStockShortfallReported(t *testing.T) {
	h := newHarness(t, 4)
	g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err != nil {
		t.Fatalf("draw: %v", err)
	}
	if len(g.Examples) != 4 {
		t.Fatalf("expected 4 examples, got %d", len(g.Examples))
	}
	if g.Shortfall != 6 {
		t.Fatalf("expected shortfall 6, got %d", g.Shortfall)
	}
}

func TestUnknownCategoryRejected(t *testing.T) {
	h := newHarness(t, 10)
	in := input("203.0.113.10", machA, diskA)
	in.Categories = []string{"cobol"}
	if _, err := h.svc.RequestDraw(context.Background(), in); err == nil {
		t.Fatal("expected unknown category to fail")
	}
}

func TestUsageCountedAndStaleAfterThree(t *testing.T) {
	h := newHarness(t, 1)
	identities := []struct{ ip, machine, disk string }{
		{"203.0.113.10", machA, diskA},
	}
	if _, err := h.svc.RequestDraw(context.Background(), input(identities[0].ip, identities[0].machine, identities[0].disk)); err != nil {
		t.Fatalf("first: %v", err)
	}
	if got := h.bankM.Uses("py-000"); got != 1 {
		t.Fatalf("expected 1 use, got %d", got)
	}
	// Two more distinct callers consume the remaining allowed uses.
	others := [][3]string{
		{"203.0.114.11", machB, diskB},
		{"203.0.115.12", "6e6e4544-0066-3510-8054-b4c04f4e4e34", "bb3b4c5d6e7f8091a2b3c4d5e6f70819"},
	}
	for _, o := range others {
		h.advance(2 * time.Hour)
		if _, err := h.svc.RequestDraw(context.Background(), input(o[0], o[1], o[2])); err != nil {
			t.Fatalf("draw for %s: %v", o[0], err)
		}
	}
	if got := h.bankM.Uses("py-000"); got != 3 {
		t.Fatalf("expected 3 uses, got %d", got)
	}
	if stale := h.svc.StaleExamples(); len(stale) != 1 || stale[0].ID != "py-000" {
		t.Fatalf("py-000 should be stale: %+v", stale)
	}
	// A further caller gets nothing: the only example is out of circulation.
	h.advance(2 * time.Hour)
	_, err := h.svc.RequestDraw(context.Background(), input("203.0.116.13", "7f7f4544-0077-3510-8054-b4c04f4e4e35", "cc4c5d6e7f8091a2b3c4d5e6f70819aa"))
	if err == nil {
		t.Fatal("expected no stock once every example is stale")
	}
}

func TestRespectsProxyHeadersForDifferentiation(t *testing.T) {
	h := newHarness(t, 50)
	in := input("10.0.0.1", machA, diskA)
	in.Headers = map[string][]string{identity.HeaderForwardedFor: {"203.0.113.50"}}
	in.RemoteAddr = netip.MustParseAddr("198.51.100.7")
	g, err := h.svc.RequestDraw(context.Background(), in)
	if err != nil {
		t.Fatalf("draw: %v", err)
	}
	// The record must be keyed on the socket address, not the forwarded header
	// (which the client controls), and it must contain no raw value at all.
	rec, ok := h.store.Get(g.IdentityID)
	if !ok {
		t.Fatal("expected a record")
	}
	wantIP := h.svc.Hash(identity.MarkerIP, "198.51.100.7")
	found := false
	for _, m := range rec.Markers {
		if m == wantIP {
			found = true
		}
	}
	if !found {
		t.Fatalf("record is not keyed on the socket address; markers=%v", rec.Markers)
	}
}

// Identity is "IP OR machine_id OR disk_uuid": any single marker must be enough
// to serve and to recognise the caller. Requiring both device fields locked out
// any runner that could only read one of them.
func TestAnySingleMarkerIsEnough(t *testing.T) {
	cases := []struct {
		name          string
		machine, disk string
		wantMarkers   int // 1 for IP alone, 3 for IP + both
	}{
		{"ip only", "", "", 1},
		{"machine only", machA, "", 2},
		{"disk only", "", diskA, 2},
		{"both", machA, diskA, 3},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			h := newHarness(t, 10)
			g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.20", c.machine, c.disk))
			if err != nil {
				t.Fatalf("draw with %s: %v", c.name, err)
			}
			rec, ok := h.store.Get(g.IdentityID)
			if !ok {
				t.Fatal("expected a record")
			}
			if len(rec.Markers) != c.wantMarkers {
				t.Fatalf("markers = %d, want %d: %v", len(rec.Markers), c.wantMarkers, rec.Markers)
			}
		})
	}
}

// An absent device field must contribute no marker at all. If it were hashed,
// every caller without that field would share one hash and collapse into a
// single identity, which would be both wrong and a trivial denial of service.
func TestAbsentFieldProducesNoMarker(t *testing.T) {
	h := newHarness(t, 10)
	g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.21", "", ""))
	if err != nil {
		t.Fatal(err)
	}
	rec, _ := h.store.Get(g.IdentityID)
	empty := h.svc.Hash(identity.MarkerMachineID, "")
	for _, m := range rec.Markers {
		if m == empty {
			t.Fatal("an absent machine_id produced a marker; every such caller would share it")
		}
	}
}

func TestInvalidDeviceRejected(t *testing.T) {
	h := newHarness(t, 10)
	// Present but fabricated: refused rather than hashed into a marker that
	// would never match again.
	for _, bad := range []string{"unknown", "none", "00000000-0000-0000-0000-000000000000"} {
		if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", bad, diskA)); err == nil {
			t.Fatalf("expected placeholder machine id %q to be rejected", bad)
		}
	}
	// Too short to be a real identifier.
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", "abc", "")); err == nil {
		t.Fatal("expected an implausibly short machine id to be rejected")
	}
}

func TestReputationBlocksTor(t *testing.T) {
	h := newHarness(t, 10)
	dir := t.TempDir()
	path := filepath.Join(dir, "rep.json")
	body := `{"entries":[{"cidr":"203.0.113.0/24","tor":true}]}`
	if err := os.WriteFile(path, []byte(body), 0o640); err != nil {
		t.Fatal(err)
	}
	rep, err := reputation.Load(path)
	if err != nil {
		t.Fatal(err)
	}
	h.svc.rep = rep
	_, err = h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err == nil || !strings.Contains(err.Error(), "PRIVACY_BLOCKED") {
		t.Fatalf("expected tor exit to be blocked, got %v", err)
	}
}

func TestEventsRecorded(t *testing.T) {
	h := newHarness(t, 50)
	if _, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA)); err != nil {
		t.Fatal(err)
	}
	entries, err := os.ReadDir(filepath.Join(h.cfg.DataDir, "events"))
	if err != nil {
		t.Fatalf("events dir: %v", err)
	}
	if len(entries) == 0 {
		t.Fatal("expected an event log file")
	}
	buf, err := os.ReadFile(filepath.Join(h.cfg.DataDir, "events", entries[0].Name()))
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(buf), `"decision":"allow"`) {
		t.Fatalf("expected an allow event, got %s", buf)
	}
}

// The strongest privacy guarantee the system can make: after a full request
// cycle, not one byte on disk contains the IP or the device identifiers.
func TestNoRawIdentityAnywhereOnDisk(t *testing.T) {
	h := newHarness(t, 50)
	g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err != nil {
		t.Fatal(err)
	}
	if err := h.svc.Flush(); err != nil {
		t.Fatal(err)
	}

	secrets := []string{"203.0.113.10", machA, diskA, machA[:8], diskA[:8]}
	err = filepath.WalkDir(h.cfg.DataDir, func(path string, d os.DirEntry, err error) error {
		if err != nil || d.IsDir() {
			return nil
		}
		buf, err := os.ReadFile(path)
		if err != nil {
			return nil
		}
		for _, secret := range secrets {
			if strings.Contains(string(buf), secret) {
				t.Errorf("%s contains raw identity value %q", path, secret)
			}
		}
		// Also assert the shard tree has no raw subtree at all.
		if strings.Contains(filepath.ToSlash(path), "/raw/") {
			t.Errorf("unexpected raw subtree entry: %s", path)
		}
		return nil
	})
	if err != nil {
		t.Fatalf("walk: %v", err)
	}
	if _, ok := h.store.Get(g.IdentityID); !ok {
		t.Fatal("the hashed record should still exist")
	}
}

func TestHealthReportsBank(t *testing.T) {
	h := newHarness(t, 5)
	health := h.svc.Health()
	if !health.OK {
		t.Fatal("health should be OK with stock")
	}
	if health.Inventory.Total != 5 {
		t.Fatalf("expected 5 total, got %d", health.Inventory.Total)
	}
}

func TestPruneClearsBansButKeepsMemory(t *testing.T) {
	h := newHarness(t, 50)
	g, err := h.svc.RequestDraw(context.Background(), input("203.0.113.10", machA, diskA))
	if err != nil {
		t.Fatal(err)
	}
	rec, _ := h.store.Get(g.IdentityID)
	rec.BannedUntil = h.now.Add(-time.Minute)
	rec.BanReason = "SPAM_BURST"
	if err := h.store.Put(rec); err != nil {
		t.Fatal(err)
	}

	cleared, err := h.svc.Prune()
	if err != nil {
		t.Fatalf("prune: %v", err)
	}
	if cleared != 1 {
		t.Fatalf("expected 1 ban cleared, got %d", cleared)
	}
	after, ok := h.store.Get(g.IdentityID)
	if !ok {
		t.Fatal("hashed record must survive pruning")
	}
	if !after.BannedUntil.IsZero() {
		t.Fatal("expired ban should be cleared")
	}
}

func shardOf(id string) string { return store.ShardOf(id) }

// The server is the only source of a draw id. A caller that supplies one is
// not told "no" -- it is told to think again, and the id it sent is never used.
// This is what makes two callers unable to collide on a handle: neither one can
// choose it.
func TestCallerSuppliedRequestIDIsIgnored(t *testing.T) {
	h := newHarness(t, 20)

	first := input("203.0.113.10", machA, diskA)
	first.RequestID = "i-picked-this"
	grant, err := h.svc.RequestDraw(context.Background(), first)
	if err != nil {
		t.Fatalf("draw: %v", err)
	}
	if grant.RequestID == "i-picked-this" {
		t.Fatal("the caller's request_id became the draw id")
	}
	if grant.RequestID == "" {
		t.Fatal("the server must mint a draw id")
	}
	if len(grant.RequestID) != 32 {
		t.Fatalf("expected a 128-bit hex id, got %q", grant.RequestID)
	}

	// The caller's value must not exist in the ledger either: it was not used.
	if _, ok := h.store.LeaseLedger("i-picked-this"); ok {
		t.Fatal("the caller's id leaked into the ledger")
	}
	if _, ok := h.store.LeaseLedger(grant.RequestID); !ok {
		t.Fatal("the minted id must be in the ledger")
	}
}

// Two callers sending the SAME id must not collide, because neither id is used.
func TestTwoCallersCannotShareADrawID(t *testing.T) {
	h := newHarness(t, 20)

	first := input("203.0.113.10", machA, diskA)
	first.RequestID = "same-value"
	a, err := h.svc.RequestDraw(context.Background(), first)
	if err != nil {
		t.Fatalf("first draw: %v", err)
	}

	h.advance(2 * time.Hour)
	second := input("198.51.100.7", machB, diskB)
	second.RequestID = "same-value"
	b, err := h.svc.RequestDraw(context.Background(), second)
	if err != nil {
		t.Fatalf("second draw must not be refused: %v", err)
	}

	if a.RequestID == b.RequestID {
		t.Fatal("two draws were issued the same id")
	}
	// Both must be independently reportable: that is the whole point.
	if _, ok := h.store.LeaseLedger(a.RequestID); !ok {
		t.Fatal("the first draw is no longer in the ledger")
	}
	if _, ok := h.store.LeaseLedger(b.RequestID); !ok {
		t.Fatal("the second draw is not in the ledger")
	}
}

// A minted id must not be predictable from another one.
func TestRequestIDsAreNotSequential(t *testing.T) {
	seen := map[string]bool{}
	for i := 0; i < 100; i++ {
		id := NewRequestID()
		if len(id) != 32 {
			t.Fatalf("expected 32 hex chars, got %d (%q)", len(id), id)
		}
		if seen[id] {
			t.Fatalf("minted a duplicate id: %q", id)
		}
		seen[id] = true
	}
}

// Omitting the id is the normal path and must keep working: the relay mints one.
func TestMissingRequestIDIsMinted(t *testing.T) {
	h := newHarness(t, 5)
	in := input("203.0.113.10", machA, diskA)
	in.RequestID = ""
	grant, err := h.svc.RequestDraw(context.Background(), in)
	if err != nil {
		t.Fatalf("draw: %v", err)
	}
	if grant.RequestID == "" {
		t.Fatal("the relay must mint a request id")
	}
	if _, ok := h.store.LeaseLedger(grant.RequestID); !ok {
		t.Fatal("the minted id must be in the ledger")
	}
}

// The lifeline belongs to the example, not to the person who drew it. Two
// different machines asking for the same question spend one shared budget, and
// a grant reports what is left so a runner can show it before it goes stale.
func TestLifelineIsSharedBetweenDifferentCallers(t *testing.T) {
	h := newHarness(t, 1) // exactly one example: every draw must take it

	for i := 0; i < 3; i++ {
		h.advance(2 * time.Hour) // step past the draw window each time
		in := input("203.0.113."+fmt.Sprint(10+i), machA, diskA)
		in.Size = 1
		grant, err := h.svc.RequestDraw(context.Background(), in)
		if err != nil {
			t.Fatalf("draw %d: %v", i, err)
		}
		if len(grant.Examples) != 1 {
			t.Fatalf("draw %d served %d examples", i, len(grant.Examples))
		}
		life := grant.Lifeline[grant.Examples[0].ID]
		if life.Used != i+1 {
			t.Fatalf("draw %d: expected used=%d, got %d", i, i+1, life.Used)
		}
		if life.Max != 3 {
			t.Fatalf("draw %d: expected max=3, got %d", i, life.Max)
		}
		if life.Remaining != 2-i {
			t.Fatalf("draw %d: expected remaining=%d, got %d", i, 2-i, life.Remaining)
		}
		if grant.StaleAfter != 3 {
			t.Fatalf("draw %d: expected stale_after=3, got %d", i, grant.StaleAfter)
		}
	}

	// The third serve exhausted it, so a fourth caller gets nothing usable.
	h.advance(2 * time.Hour)
	in := input("198.51.100.99", machA, diskA)
	in.Size = 1
	if _, err := h.svc.RequestDraw(context.Background(), in); err == nil {
		t.Fatal("a stale example must not be served again")
	}
}