-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathgateway_store.go
More file actions
1586 lines (1519 loc) · 59.4 KB
/
Copy pathgateway_store.go
File metadata and controls
1586 lines (1519 loc) · 59.4 KB
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
_ "modernc.org/sqlite"
)
type GatewaySettings struct {
ChainREST string `json:"chain_rest"`
PublicAPI string `json:"public_api"`
DefaultModel string `json:"default_model"`
DefaultRequestMaxTokens uint64 `json:"default_request_max_tokens"`
RequestMaxTokensCap uint64 `json:"request_max_tokens_cap"`
MaxConcurrentRequests int64 `json:"max_concurrent_requests"`
MaxConcurrentPer10000Weight float64 `json:"max_concurrent_requests_per_10000_weight"`
PoCMaxConcurrentPer10000Weight float64 `json:"poc_max_concurrent_requests_per_10000_weight"`
MaxInputTokensInFlight int64 `json:"max_input_tokens_in_flight"`
ModelLimits []GatewayModelLimitSettings `json:"model_limits,omitempty"`
TxGasLimit uint64 `json:"tx_gas_limit,omitempty"`
Disabled GatewayDisabledSettings `json:"disabled"`
ParticipantThrottle ParticipantThrottleSettings `json:"participant_throttle"`
Redundancy RedundancySettings `json:"redundancy"`
Perf PerfSettings `json:"perf"`
EscrowRotation EscrowRotationSettings `json:"escrow_rotation"`
}
type GatewayModelLimitSettings struct {
ModelID string `json:"model_id"`
MaxConcurrentRequests int64 `json:"max_concurrent_requests"`
MaxInputTokensInFlight int64 `json:"max_input_tokens_in_flight"`
DefaultRequestMaxTokens uint64 `json:"default_request_max_tokens,omitempty"`
RequestMaxTokensCap uint64 `json:"request_max_tokens_cap,omitempty"`
AccessMode string `json:"access_mode,omitempty"`
AccessMessage string `json:"access_message,omitempty"`
}
type GatewayModelAccessSettings struct {
ModelID string `json:"model_id"`
Enabled bool `json:"enabled"`
Message string `json:"message,omitempty"`
}
type ParticipantThrottleSettings struct {
RequestBurst int `json:"request_burst"`
RecoveryPerMinute int `json:"recovery_per_minute"`
HTTPQuarantineMS int64 `json:"http_quarantine_ms"`
TransportFailureQuarantineMS int64 `json:"transport_failure_quarantine_ms"`
EmptyStreamQuarantineMS int64 `json:"empty_stream_quarantine_ms"`
StalledWinnerQuarantineMS int64 `json:"stalled_winner_quarantine_ms"`
EmptyStreamQuarantineThreshold int `json:"empty_stream_threshold"`
EOFTransportFailureThreshold int `json:"eof_transport_failure_threshold"`
}
type RedundancySettings struct {
ReceiptTimeoutMS int64 `json:"receipt_timeout_ms"`
FirstTokenTimeoutFloorMS int64 `json:"first_token_timeout_floor_ms"`
PerInputTokenFirstTokenLagMS int64 `json:"per_input_token_first_token_lag_ms"`
InterChunkStallTimeoutMS int64 `json:"inter_chunk_stall_timeout_ms"`
StreamingAttemptHardTimeoutMS int64 `json:"streaming_attempt_hard_timeout_ms"`
NonStreamResponseFloorMS int64 `json:"non_stream_response_floor_ms"`
NonStreamNoContentTimeoutMS int64 `json:"non_stream_no_content_timeout_ms"`
NonStreamMaxAttemptWaitMS int64 `json:"non_stream_max_attempt_wait_ms"`
PerInputTokenResponseLagMS int64 `json:"per_input_token_response_lag_ms"`
SecondaryWaitAfterWinnerMS int64 `json:"secondary_wait_after_winner_ms"`
ParallelAdvantageThreshold float64 `json:"parallel_advantage_threshold"`
UnresponsiveThreshold float64 `json:"unresponsive_threshold"`
SpeedPolicy string `json:"speed_policy"`
PairwiseBudgetPercentile float64 `json:"pairwise_budget_percentile"`
PairwiseMaxProactiveAttempts int `json:"pairwise_max_proactive_attempts"`
PairwiseMinDirectComparisons int `json:"pairwise_min_direct_comparisons"`
PairwiseWinnerHoldMS int64 `json:"pairwise_winner_hold_ms"`
PairwiseWinnerHoldMinSpeedup float64 `json:"pairwise_winner_hold_min_speedup"`
PairwiseWinnerHoldMinSamples int `json:"pairwise_winner_hold_min_samples"`
}
type PerfSettings struct {
SampleSize int `json:"sample_size"`
WindowMS int64 `json:"window_ms"`
}
type EscrowRotationSettings struct {
Enabled bool `json:"enabled"`
SettlementEnabled bool `json:"settlement_enabled"`
PrePoCBlocks int64 `json:"pre_poc_blocks"`
Models []EscrowRotationModelSettings `json:"models,omitempty"`
}
type EscrowRotationModelSettings struct {
ModelID string `json:"model_id"`
TempCount int `json:"temp_count"`
TargetCount int `json:"target_count"`
Amount uint64 `json:"amount"`
PrivateKeyEnv string `json:"private_key_env"` // trimmed by WithTuningDefaults on settings load
}
const (
defaultMaxConcurrentPer10000Weight = 5.0
defaultPoCMaxConcurrentPer10000Weight = 10.0
)
func DefaultGatewaySettingsTuning() (ParticipantThrottleSettings, RedundancySettings, PerfSettings) {
return DefaultParticipantThrottleSettings(), DefaultRedundancySettings(), PerfSettings{
SampleSize: 256,
WindowMS: int64(time.Hour / time.Millisecond),
}
}
func (s GatewaySettings) WithTuningDefaults() GatewaySettings {
participantDefaults, redundancyDefaults, perfDefaults := DefaultGatewaySettingsTuning()
if s.DefaultRequestMaxTokens == 0 {
s.DefaultRequestMaxTokens = 3_072
}
if s.RequestMaxTokensCap == 0 {
s.RequestMaxTokensCap = 4_096
}
if s.MaxConcurrentPer10000Weight == 0 {
s.MaxConcurrentPer10000Weight = defaultMaxConcurrentPer10000Weight
}
if s.PoCMaxConcurrentPer10000Weight == 0 {
s.PoCMaxConcurrentPer10000Weight = s.MaxConcurrentPer10000Weight
if s.PoCMaxConcurrentPer10000Weight == defaultMaxConcurrentPer10000Weight {
s.PoCMaxConcurrentPer10000Weight = defaultPoCMaxConcurrentPer10000Weight
}
}
s.Disabled = s.Disabled.WithDefaults()
if s.ParticipantThrottle == (ParticipantThrottleSettings{}) {
s.ParticipantThrottle = participantDefaults
}
if s.Redundancy == (RedundancySettings{}) {
s.Redundancy = redundancyDefaults
}
if s.Redundancy.StreamingAttemptHardTimeoutMS == 0 {
s.Redundancy.StreamingAttemptHardTimeoutMS = redundancyDefaults.StreamingAttemptHardTimeoutMS
}
if s.Redundancy.NonStreamNoContentTimeoutMS == 0 {
s.Redundancy.NonStreamNoContentTimeoutMS = redundancyDefaults.NonStreamNoContentTimeoutMS
}
if s.Redundancy.NonStreamMaxAttemptWaitMS == 0 {
s.Redundancy.NonStreamMaxAttemptWaitMS = redundancyDefaults.NonStreamMaxAttemptWaitMS
}
if s.Redundancy.SpeedPolicy == "" {
s.Redundancy.SpeedPolicy = redundancyDefaults.SpeedPolicy
}
if s.Redundancy.PairwiseBudgetPercentile == 0 {
s.Redundancy.PairwiseBudgetPercentile = redundancyDefaults.PairwiseBudgetPercentile
}
if s.Redundancy.PairwiseMaxProactiveAttempts == 0 {
s.Redundancy.PairwiseMaxProactiveAttempts = redundancyDefaults.PairwiseMaxProactiveAttempts
}
if s.Redundancy.PairwiseMinDirectComparisons == 0 {
s.Redundancy.PairwiseMinDirectComparisons = redundancyDefaults.PairwiseMinDirectComparisons
}
if s.Redundancy.PairwiseWinnerHoldMS == 0 {
s.Redundancy.PairwiseWinnerHoldMS = redundancyDefaults.PairwiseWinnerHoldMS
}
if s.Redundancy.PairwiseWinnerHoldMinSpeedup == 0 {
s.Redundancy.PairwiseWinnerHoldMinSpeedup = redundancyDefaults.PairwiseWinnerHoldMinSpeedup
}
if s.Redundancy.PairwiseWinnerHoldMinSamples == 0 {
s.Redundancy.PairwiseWinnerHoldMinSamples = redundancyDefaults.PairwiseWinnerHoldMinSamples
}
if s.Perf == (PerfSettings{}) {
s.Perf = perfDefaults
}
if s.EscrowRotation.PrePoCBlocks == 0 {
s.EscrowRotation.PrePoCBlocks = 300
}
for i := range s.EscrowRotation.Models {
model := &s.EscrowRotation.Models[i]
model.ModelID = strings.TrimSpace(model.ModelID)
model.PrivateKeyEnv = strings.TrimSpace(model.PrivateKeyEnv)
}
s.ModelLimits = normalizeGatewayModelLimits(s.ModelLimits)
return s
}
func normalizeGatewayModelLimits(limits []GatewayModelLimitSettings) []GatewayModelLimitSettings {
if len(limits) == 0 {
return nil
}
normalized := make([]GatewayModelLimitSettings, 0, len(limits))
seen := make(map[string]int, len(limits))
for _, limit := range limits {
limit.ModelID = strings.TrimSpace(limit.ModelID)
limit.AccessMode = normalizeGatewayAccessMode(limit.AccessMode)
limit.AccessMessage = strings.TrimSpace(limit.AccessMessage)
if limit.ModelID == "" {
continue
}
if idx, ok := seen[limit.ModelID]; ok {
normalized[idx] = limit
continue
}
seen[limit.ModelID] = len(normalized)
normalized = append(normalized, limit)
}
return normalized
}
func normalizeGatewayAccessMode(mode string) string {
mode = strings.ToLower(strings.TrimSpace(mode))
mode = strings.ReplaceAll(mode, "-", "_")
mode = strings.ReplaceAll(mode, " ", "_")
switch mode {
case "":
return ""
case "open", "public", "none":
return string(gatewayAccessModeOpen)
case "api_key", "apikey", "api_keys", "api", "key":
return string(gatewayAccessModeAPIKey)
case "admin", "admin_only", "admin_key":
return string(gatewayAccessModeAdminOnly)
default:
return mode
}
}
func gatewayModelAccessModeLabel(mode string) string {
mode = normalizeGatewayAccessMode(mode)
if mode == "" {
return string(gatewayAccessModeAdminOnly)
}
return mode
}
func applyLegacyModelAccessToLimits(limits []GatewayModelLimitSettings, access []GatewayModelAccessSettings) []GatewayModelLimitSettings {
if len(access) == 0 {
return limits
}
limits = normalizeGatewayModelLimits(limits)
byModel := make(map[string]int, len(limits))
for i, limit := range limits {
byModel[limit.ModelID] = i
}
for _, entry := range normalizeGatewayModelAccess(access) {
mode := string(gatewayAccessModeOpen)
if !entry.Enabled {
mode = string(gatewayAccessModeAdminOnly)
}
if idx, ok := byModel[entry.ModelID]; ok {
if limits[idx].AccessMode == "" {
limits[idx].AccessMode = mode
}
if limits[idx].AccessMessage == "" {
limits[idx].AccessMessage = entry.Message
}
continue
}
byModel[entry.ModelID] = len(limits)
limits = append(limits, GatewayModelLimitSettings{
ModelID: entry.ModelID,
AccessMode: mode,
AccessMessage: entry.Message,
})
}
return normalizeGatewayModelLimits(limits)
}
func normalizeGatewayModelAccess(access []GatewayModelAccessSettings) []GatewayModelAccessSettings {
if len(access) == 0 {
return nil
}
normalized := make([]GatewayModelAccessSettings, 0, len(access))
seen := make(map[string]int, len(access))
for _, entry := range access {
entry.ModelID = strings.TrimSpace(entry.ModelID)
entry.Message = strings.TrimSpace(entry.Message)
if entry.ModelID == "" {
continue
}
if idx, ok := seen[entry.ModelID]; ok {
normalized[idx] = entry
continue
}
seen[entry.ModelID] = len(normalized)
normalized = append(normalized, entry)
}
return normalized
}
type GatewayDevshardState struct {
RuntimeConfig
Active bool `json:"active"`
SettlementPending bool `json:"settlement_pending,omitempty"`
RotationRole string `json:"rotation_role,omitempty"`
RotationEpoch uint64 `json:"rotation_epoch,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
type GatewaySuspiciousHost struct {
ParticipantKey string `json:"participant_key"`
Note string `json:"note,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
type GatewayState struct {
Settings GatewaySettings `json:"settings"`
Devshards []GatewayDevshardState `json:"devshards"`
SuspiciousHosts []GatewaySuspiciousHost `json:"suspicious_hosts,omitempty"`
}
type GatewayStore struct {
db *sql.DB
}
func NewGatewayStore(path string) (*GatewayStore, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("open gateway store: %w", err)
}
// Serialize access and wait on contention instead of failing with "database is
// locked". Mirrors storage/sqlite.go.
db.SetMaxOpenConns(1)
for _, pragma := range []string{
"PRAGMA journal_mode=WAL",
"PRAGMA synchronous=NORMAL",
"PRAGMA busy_timeout=5000",
} {
if _, err := db.Exec(pragma); err != nil {
db.Close()
return nil, fmt.Errorf("apply gateway store pragma %q: %w", pragma, err)
}
}
stmts := []string{
`CREATE TABLE IF NOT EXISTS gateway_settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
chain_rest TEXT NOT NULL,
public_api TEXT NOT NULL DEFAULT '',
default_model TEXT NOT NULL,
default_request_max_tokens INTEGER NOT NULL,
request_max_tokens_cap INTEGER NOT NULL DEFAULT 4096,
max_concurrent_requests INTEGER NOT NULL DEFAULT 512,
max_concurrent_requests_per_10000_weight REAL NOT NULL DEFAULT 5.0,
poc_max_concurrent_requests_per_10000_weight REAL NOT NULL DEFAULT 10.0,
max_input_tokens_in_flight INTEGER NOT NULL,
model_limits_json TEXT NOT NULL DEFAULT '',
model_access_json TEXT NOT NULL DEFAULT '',
tx_gas_limit INTEGER NOT NULL DEFAULT 0,
participant_request_burst INTEGER NOT NULL DEFAULT 600,
participant_recovery_per_minute INTEGER NOT NULL DEFAULT 10,
participant_http_quarantine_ms INTEGER NOT NULL DEFAULT 3600000,
participant_transport_failure_quarantine_ms INTEGER NOT NULL DEFAULT 1800000,
participant_empty_stream_quarantine_ms INTEGER NOT NULL DEFAULT 1800000,
participant_stalled_winner_quarantine_ms INTEGER NOT NULL DEFAULT 1800000,
participant_empty_stream_threshold INTEGER NOT NULL DEFAULT 3,
participant_eof_transport_failure_threshold INTEGER NOT NULL DEFAULT 3,
redundancy_receipt_timeout_ms INTEGER NOT NULL DEFAULT 5000,
redundancy_first_token_timeout_floor_ms INTEGER NOT NULL DEFAULT 1000,
redundancy_per_input_token_first_token_lag_ms INTEGER NOT NULL DEFAULT 10,
redundancy_inter_chunk_stall_timeout_ms INTEGER NOT NULL DEFAULT 60000,
redundancy_streaming_attempt_hard_timeout_ms INTEGER NOT NULL DEFAULT 1200000,
redundancy_non_stream_response_floor_ms INTEGER NOT NULL DEFAULT 20000,
redundancy_non_stream_no_content_timeout_ms INTEGER NOT NULL DEFAULT 1200000,
redundancy_non_stream_max_attempt_wait_ms INTEGER NOT NULL DEFAULT 1800000,
redundancy_per_input_token_response_lag_ms INTEGER NOT NULL DEFAULT 20,
redundancy_secondary_wait_after_winner_ms INTEGER NOT NULL DEFAULT 600000,
redundancy_parallel_advantage_threshold REAL NOT NULL DEFAULT 0.5,
redundancy_unresponsive_threshold REAL NOT NULL DEFAULT 1.0,
redundancy_speed_policy TEXT NOT NULL DEFAULT 'hybrid',
redundancy_pairwise_budget_percentile REAL NOT NULL DEFAULT 0.9,
redundancy_pairwise_max_proactive_attempts INTEGER NOT NULL DEFAULT 3,
redundancy_pairwise_min_direct_comparisons INTEGER NOT NULL DEFAULT 4,
redundancy_pairwise_winner_hold_ms INTEGER NOT NULL DEFAULT 500,
redundancy_pairwise_winner_hold_min_speedup REAL NOT NULL DEFAULT 0.1,
redundancy_pairwise_winner_hold_min_samples INTEGER NOT NULL DEFAULT 6,
perf_sample_size INTEGER NOT NULL DEFAULT 256,
perf_window_ms INTEGER NOT NULL DEFAULT 3600000,
escrow_rotation_enabled INTEGER NOT NULL DEFAULT 0,
escrow_rotation_settlement_enabled INTEGER NOT NULL DEFAULT 0,
escrow_rotation_pre_poc_blocks INTEGER NOT NULL DEFAULT 300,
escrow_rotation_models_json TEXT NOT NULL DEFAULT '',
gateway_disabled_enabled INTEGER NOT NULL DEFAULT 0,
gateway_disabled_message TEXT NOT NULL DEFAULT '',
gateway_disabled_new_url TEXT NOT NULL DEFAULT '',
updated_at TEXT NOT NULL
)`,
`CREATE TABLE IF NOT EXISTS gateway_devshards (
id TEXT PRIMARY KEY,
private_key_hex TEXT NOT NULL,
private_key_env TEXT NOT NULL DEFAULT '',
model TEXT NOT NULL DEFAULT '',
storage_path TEXT NOT NULL DEFAULT '',
active INTEGER NOT NULL DEFAULT 1,
rotation_role TEXT NOT NULL DEFAULT '',
rotation_epoch INTEGER NOT NULL DEFAULT 0,
settlement_pending INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
)`,
}
for _, stmt := range stmts {
if _, err := db.Exec(stmt); err != nil {
db.Close()
return nil, fmt.Errorf("init gateway store: %w", err)
}
}
if err := ensureGatewaySettingsColumn(db, "public_api", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway store: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "tx_gas_limit", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway tx settings: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "model_limits_json", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway model limits: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "model_access_json", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway model access: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "request_max_tokens_cap", "INTEGER NOT NULL DEFAULT 4096"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway max token cap: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "max_concurrent_requests_per_10000_weight", "REAL NOT NULL DEFAULT 5.0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway weight concurrency: %w", err)
}
if err := ensureGatewaySettingsColumn(db, "poc_max_concurrent_requests_per_10000_weight", "REAL NOT NULL DEFAULT 10.0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway poc weight concurrency: %w", err)
}
if err := ensureGatewaySettingsTuningColumns(db); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway tuning settings: %w", err)
}
if err := ensureGatewaySettingsRotationColumns(db); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway rotation settings: %w", err)
}
if err := ensureGatewaySettingsDisabledColumns(db); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway disabled settings: %w", err)
}
if err := ensureGatewayDevshardsColumn(db, "protocol_version", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway devshards: %w", err)
}
if err := ensureGatewayDevshardsColumn(db, "rotation_role", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway devshard role: %w", err)
}
if err := ensureGatewayDevshardsColumn(db, "rotation_epoch", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway devshard epoch: %w", err)
}
if err := ensureGatewayDevshardsColumn(db, "settlement_pending", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate gateway devshard settlement_pending: %w", err)
}
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS participant_throttle_state (
participant_key TEXT PRIMARY KEY,
tokens REAL NOT NULL DEFAULT 0,
last_refill_at TEXT NOT NULL,
last_throttle_status INTEGER NOT NULL DEFAULT 0,
empty_stream_streak INTEGER NOT NULL DEFAULT 0
)`); err != nil {
db.Close()
return nil, fmt.Errorf("init participant throttle table: %w", err)
}
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS gateway_rotation_status (
model_id TEXT NOT NULL,
stage TEXT NOT NULL,
epoch INTEGER NOT NULL,
role TEXT NOT NULL DEFAULT '',
target_count INTEGER NOT NULL DEFAULT 0,
existing_count INTEGER NOT NULL DEFAULT 0,
created_count INTEGER NOT NULL DEFAULT 0,
promoted_count INTEGER NOT NULL DEFAULT 0,
settled_count INTEGER NOT NULL DEFAULT 0,
settle_failed_count INTEGER NOT NULL DEFAULT 0,
create_error TEXT NOT NULL DEFAULT '',
completed INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL,
PRIMARY KEY (model_id, stage, epoch)
)`); err != nil {
db.Close()
return nil, fmt.Errorf("init gateway rotation status table: %w", err)
}
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS gateway_suspicious_hosts (
participant_key TEXT PRIMARY KEY,
note TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL
)`); err != nil {
db.Close()
return nil, fmt.Errorf("init gateway suspicious hosts table: %w", err)
}
// Write-ahead intent for an escrow create (written before the on-chain tx).
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS escrow_rotation_commitments (
tx_hash TEXT PRIMARY KEY,
model TEXT NOT NULL,
role TEXT NOT NULL DEFAULT '',
epoch INTEGER NOT NULL DEFAULT 0,
private_key_env TEXT NOT NULL DEFAULT '',
block_height INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL
)`); err != nil {
db.Close()
return nil, fmt.Errorf("init escrow rotation commitments table: %w", err)
}
if err := ensureColumn(db, "escrow_rotation_commitments", "protocol_version", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate escrow rotation commitments protocol version: %w", err)
}
if err := ensureColumn(db, "participant_throttle_state", "quarantine_until_utc", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle: %w", err)
}
if err := ensureColumn(db, "participant_throttle_state", "empty_stream_streak", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle streak: %w", err)
}
if err := ensureColumn(db, "participant_throttle_state", "eof_transport_failure_streak", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle eof streak: %w", err)
}
if err := ensureColumn(db, "participant_throttle_state", "model_ids", "TEXT NOT NULL DEFAULT ''"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle models: %w", err)
}
if err := ensureColumn(db, "participant_throttle_state", "failure_strikes", "INTEGER NOT NULL DEFAULT 0"); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle strikes: %w", err)
}
if _, err := db.Exec(`
UPDATE participant_throttle_state
SET failure_strikes = MAX(IFNULL(empty_stream_streak, 0), IFNULL(eof_transport_failure_streak, 0))
WHERE IFNULL(failure_strikes, 0) = 0
AND (IFNULL(empty_stream_streak, 0) > 0 OR IFNULL(eof_transport_failure_streak, 0) > 0)`); err != nil {
db.Close()
return nil, fmt.Errorf("migrate participant throttle strike values: %w", err)
}
return &GatewayStore{db: db}, nil
}
func (s *GatewayStore) Close() error {
if s == nil || s.db == nil {
return nil
}
return s.db.Close()
}
func (s *GatewayStore) LoadState() (GatewayState, bool, error) {
var state GatewayState
row := s.db.QueryRow(`
SELECT chain_rest, public_api, default_model, default_request_max_tokens, request_max_tokens_cap,
max_concurrent_requests, max_concurrent_requests_per_10000_weight,
poc_max_concurrent_requests_per_10000_weight, max_input_tokens_in_flight,
model_limits_json, model_access_json, tx_gas_limit,
participant_request_burst, participant_recovery_per_minute,
participant_http_quarantine_ms, participant_transport_failure_quarantine_ms,
participant_empty_stream_quarantine_ms, participant_stalled_winner_quarantine_ms,
participant_empty_stream_threshold, participant_eof_transport_failure_threshold,
redundancy_receipt_timeout_ms, redundancy_first_token_timeout_floor_ms,
redundancy_per_input_token_first_token_lag_ms, redundancy_inter_chunk_stall_timeout_ms,
redundancy_streaming_attempt_hard_timeout_ms,
redundancy_non_stream_response_floor_ms, redundancy_non_stream_no_content_timeout_ms,
redundancy_non_stream_max_attempt_wait_ms, redundancy_per_input_token_response_lag_ms,
redundancy_secondary_wait_after_winner_ms, redundancy_parallel_advantage_threshold,
redundancy_unresponsive_threshold, redundancy_speed_policy, redundancy_pairwise_budget_percentile,
redundancy_pairwise_max_proactive_attempts, redundancy_pairwise_min_direct_comparisons,
redundancy_pairwise_winner_hold_ms, redundancy_pairwise_winner_hold_min_speedup,
redundancy_pairwise_winner_hold_min_samples,
perf_sample_size, perf_window_ms,
escrow_rotation_enabled, escrow_rotation_settlement_enabled,
escrow_rotation_pre_poc_blocks, escrow_rotation_models_json,
gateway_disabled_enabled, gateway_disabled_message, gateway_disabled_new_url
FROM gateway_settings
WHERE id = 1`)
var rotationEnabled int
var rotationSettlementEnabled int
var disabledEnabled int
var rotationModelsJSON string
var modelLimitsJSON string
var modelAccessJSON string
err := row.Scan(
&state.Settings.ChainREST,
&state.Settings.PublicAPI,
&state.Settings.DefaultModel,
&state.Settings.DefaultRequestMaxTokens,
&state.Settings.RequestMaxTokensCap,
&state.Settings.MaxConcurrentRequests,
&state.Settings.MaxConcurrentPer10000Weight,
&state.Settings.PoCMaxConcurrentPer10000Weight,
&state.Settings.MaxInputTokensInFlight,
&modelLimitsJSON,
&modelAccessJSON,
&state.Settings.TxGasLimit,
&state.Settings.ParticipantThrottle.RequestBurst,
&state.Settings.ParticipantThrottle.RecoveryPerMinute,
&state.Settings.ParticipantThrottle.HTTPQuarantineMS,
&state.Settings.ParticipantThrottle.TransportFailureQuarantineMS,
&state.Settings.ParticipantThrottle.EmptyStreamQuarantineMS,
&state.Settings.ParticipantThrottle.StalledWinnerQuarantineMS,
&state.Settings.ParticipantThrottle.EmptyStreamQuarantineThreshold,
&state.Settings.ParticipantThrottle.EOFTransportFailureThreshold,
&state.Settings.Redundancy.ReceiptTimeoutMS,
&state.Settings.Redundancy.FirstTokenTimeoutFloorMS,
&state.Settings.Redundancy.PerInputTokenFirstTokenLagMS,
&state.Settings.Redundancy.InterChunkStallTimeoutMS,
&state.Settings.Redundancy.StreamingAttemptHardTimeoutMS,
&state.Settings.Redundancy.NonStreamResponseFloorMS,
&state.Settings.Redundancy.NonStreamNoContentTimeoutMS,
&state.Settings.Redundancy.NonStreamMaxAttemptWaitMS,
&state.Settings.Redundancy.PerInputTokenResponseLagMS,
&state.Settings.Redundancy.SecondaryWaitAfterWinnerMS,
&state.Settings.Redundancy.ParallelAdvantageThreshold,
&state.Settings.Redundancy.UnresponsiveThreshold,
&state.Settings.Redundancy.SpeedPolicy,
&state.Settings.Redundancy.PairwiseBudgetPercentile,
&state.Settings.Redundancy.PairwiseMaxProactiveAttempts,
&state.Settings.Redundancy.PairwiseMinDirectComparisons,
&state.Settings.Redundancy.PairwiseWinnerHoldMS,
&state.Settings.Redundancy.PairwiseWinnerHoldMinSpeedup,
&state.Settings.Redundancy.PairwiseWinnerHoldMinSamples,
&state.Settings.Perf.SampleSize,
&state.Settings.Perf.WindowMS,
&rotationEnabled,
&rotationSettlementEnabled,
&state.Settings.EscrowRotation.PrePoCBlocks,
&rotationModelsJSON,
&disabledEnabled,
&state.Settings.Disabled.Message,
&state.Settings.Disabled.NewURL,
)
if err == sql.ErrNoRows {
return GatewayState{}, false, nil
}
if err != nil {
return GatewayState{}, false, fmt.Errorf("load gateway settings: %w", err)
}
state.Settings.EscrowRotation.Enabled = rotationEnabled != 0
state.Settings.EscrowRotation.SettlementEnabled = rotationSettlementEnabled != 0
if strings.TrimSpace(rotationModelsJSON) != "" {
if err := json.Unmarshal([]byte(rotationModelsJSON), &state.Settings.EscrowRotation.Models); err != nil {
return GatewayState{}, false, fmt.Errorf("load gateway rotation models: %w", err)
}
}
if strings.TrimSpace(modelLimitsJSON) != "" {
if err := json.Unmarshal([]byte(modelLimitsJSON), &state.Settings.ModelLimits); err != nil {
return GatewayState{}, false, fmt.Errorf("load gateway model limits: %w", err)
}
}
if strings.TrimSpace(modelAccessJSON) != "" {
var legacyModelAccess []GatewayModelAccessSettings
if err := json.Unmarshal([]byte(modelAccessJSON), &legacyModelAccess); err != nil {
return GatewayState{}, false, fmt.Errorf("load gateway model access: %w", err)
}
state.Settings.ModelLimits = applyLegacyModelAccessToLimits(state.Settings.ModelLimits, legacyModelAccess)
}
state.Settings.Disabled.Enabled = disabledEnabled != 0
state.Settings = state.Settings.WithTuningDefaults()
rows, err := s.db.Query(`
SELECT id, private_key_hex, private_key_env, model, storage_path, active, created_at, updated_at, protocol_version,
rotation_role, rotation_epoch, settlement_pending
FROM gateway_devshards
ORDER BY id`)
if err != nil {
return GatewayState{}, false, fmt.Errorf("load gateway devshards: %w", err)
}
defer rows.Close()
for rows.Next() {
var devshard GatewayDevshardState
var active int
var settlementPending int
if err := rows.Scan(
&devshard.ID,
&devshard.PrivateKeyHex,
&devshard.PrivateKeyEnv,
&devshard.Model,
&devshard.StoragePath,
&active,
&devshard.CreatedAt,
&devshard.UpdatedAt,
&devshard.ProtocolVersion,
&devshard.RotationRole,
&devshard.RotationEpoch,
&settlementPending,
); err != nil {
return GatewayState{}, false, fmt.Errorf("scan gateway devshard: %w", err)
}
devshard.Active = active != 0
devshard.SettlementPending = settlementPending != 0
state.Devshards = append(state.Devshards, devshard)
}
if err := rows.Err(); err != nil {
return GatewayState{}, false, fmt.Errorf("iterate gateway devshards: %w", err)
}
suspiciousHosts, err := s.LoadSuspiciousHosts()
if err != nil {
return GatewayState{}, false, err
}
state.SuspiciousHosts = suspiciousHosts
return state, true, nil
}
func (s *GatewayStore) Initialize(settings GatewaySettings, devshards []GatewayDevshardState) error {
settings = settings.WithTuningDefaults()
now := time.Now().UTC().Format(time.RFC3339Nano)
tx, err := s.db.Begin()
if err != nil {
return fmt.Errorf("begin gateway init: %w", err)
}
defer tx.Rollback()
var count int
if err := tx.QueryRow(`SELECT COUNT(*) FROM gateway_settings WHERE id = 1`).Scan(&count); err != nil {
return fmt.Errorf("count gateway settings: %w", err)
}
if count > 0 {
return nil
}
if _, err := tx.Exec(`
INSERT INTO gateway_settings (
id, chain_rest, public_api, default_model, default_request_max_tokens, request_max_tokens_cap,
max_concurrent_requests, max_concurrent_requests_per_10000_weight,
poc_max_concurrent_requests_per_10000_weight, max_input_tokens_in_flight,
model_limits_json, model_access_json, tx_gas_limit,
participant_request_burst, participant_recovery_per_minute,
participant_http_quarantine_ms, participant_transport_failure_quarantine_ms,
participant_empty_stream_quarantine_ms, participant_stalled_winner_quarantine_ms,
participant_empty_stream_threshold, participant_eof_transport_failure_threshold,
redundancy_receipt_timeout_ms, redundancy_first_token_timeout_floor_ms,
redundancy_per_input_token_first_token_lag_ms, redundancy_inter_chunk_stall_timeout_ms,
redundancy_streaming_attempt_hard_timeout_ms,
redundancy_non_stream_response_floor_ms, redundancy_non_stream_no_content_timeout_ms,
redundancy_non_stream_max_attempt_wait_ms, redundancy_per_input_token_response_lag_ms,
redundancy_secondary_wait_after_winner_ms, redundancy_parallel_advantage_threshold,
redundancy_unresponsive_threshold, redundancy_speed_policy, redundancy_pairwise_budget_percentile,
redundancy_pairwise_max_proactive_attempts, redundancy_pairwise_min_direct_comparisons,
redundancy_pairwise_winner_hold_ms, redundancy_pairwise_winner_hold_min_speedup,
redundancy_pairwise_winner_hold_min_samples,
perf_sample_size, perf_window_ms,
escrow_rotation_enabled, escrow_rotation_settlement_enabled,
escrow_rotation_pre_poc_blocks, escrow_rotation_models_json,
gateway_disabled_enabled, gateway_disabled_message, gateway_disabled_new_url,
updated_at
) VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
strings.TrimSpace(settings.ChainREST),
strings.TrimSpace(settings.PublicAPI),
strings.TrimSpace(settings.DefaultModel),
settings.DefaultRequestMaxTokens,
settings.RequestMaxTokensCap,
settings.MaxConcurrentRequests,
settings.MaxConcurrentPer10000Weight,
settings.PoCMaxConcurrentPer10000Weight,
settings.MaxInputTokensInFlight,
mustMarshalGatewayModelLimits(settings.ModelLimits),
"",
settings.TxGasLimit,
settings.ParticipantThrottle.RequestBurst,
settings.ParticipantThrottle.RecoveryPerMinute,
settings.ParticipantThrottle.HTTPQuarantineMS,
settings.ParticipantThrottle.TransportFailureQuarantineMS,
settings.ParticipantThrottle.EmptyStreamQuarantineMS,
settings.ParticipantThrottle.StalledWinnerQuarantineMS,
settings.ParticipantThrottle.EmptyStreamQuarantineThreshold,
settings.ParticipantThrottle.EOFTransportFailureThreshold,
settings.Redundancy.ReceiptTimeoutMS,
settings.Redundancy.FirstTokenTimeoutFloorMS,
settings.Redundancy.PerInputTokenFirstTokenLagMS,
settings.Redundancy.InterChunkStallTimeoutMS,
settings.Redundancy.StreamingAttemptHardTimeoutMS,
settings.Redundancy.NonStreamResponseFloorMS,
settings.Redundancy.NonStreamNoContentTimeoutMS,
settings.Redundancy.NonStreamMaxAttemptWaitMS,
settings.Redundancy.PerInputTokenResponseLagMS,
settings.Redundancy.SecondaryWaitAfterWinnerMS,
settings.Redundancy.ParallelAdvantageThreshold,
settings.Redundancy.UnresponsiveThreshold,
settings.Redundancy.SpeedPolicy,
settings.Redundancy.PairwiseBudgetPercentile,
settings.Redundancy.PairwiseMaxProactiveAttempts,
settings.Redundancy.PairwiseMinDirectComparisons,
settings.Redundancy.PairwiseWinnerHoldMS,
settings.Redundancy.PairwiseWinnerHoldMinSpeedup,
settings.Redundancy.PairwiseWinnerHoldMinSamples,
settings.Perf.SampleSize,
settings.Perf.WindowMS,
gatewayBoolToInt(settings.EscrowRotation.Enabled),
gatewayBoolToInt(settings.EscrowRotation.SettlementEnabled),
settings.EscrowRotation.PrePoCBlocks,
mustMarshalEscrowRotationModels(settings.EscrowRotation.Models),
gatewayBoolToInt(settings.Disabled.Enabled),
strings.TrimSpace(settings.Disabled.Message),
strings.TrimSpace(settings.Disabled.NewURL),
now,
); err != nil {
return fmt.Errorf("insert gateway settings: %w", err)
}
for _, devshard := range devshards {
if err := s.upsertDevshardTx(tx, devshard, now); err != nil {
return err
}
}
return tx.Commit()
}
func (s *GatewayStore) UpdateSettings(settings GatewaySettings) error {
settings = settings.WithTuningDefaults()
res, err := s.db.Exec(`
UPDATE gateway_settings
SET chain_rest = ?,
public_api = ?,
default_model = ?,
default_request_max_tokens = ?,
request_max_tokens_cap = ?,
max_concurrent_requests = ?,
max_concurrent_requests_per_10000_weight = ?,
poc_max_concurrent_requests_per_10000_weight = ?,
max_input_tokens_in_flight = ?,
model_limits_json = ?,
model_access_json = ?,
tx_gas_limit = ?,
participant_request_burst = ?,
participant_recovery_per_minute = ?,
participant_http_quarantine_ms = ?,
participant_transport_failure_quarantine_ms = ?,
participant_empty_stream_quarantine_ms = ?,
participant_stalled_winner_quarantine_ms = ?,
participant_empty_stream_threshold = ?,
participant_eof_transport_failure_threshold = ?,
redundancy_receipt_timeout_ms = ?,
redundancy_first_token_timeout_floor_ms = ?,
redundancy_per_input_token_first_token_lag_ms = ?,
redundancy_inter_chunk_stall_timeout_ms = ?,
redundancy_streaming_attempt_hard_timeout_ms = ?,
redundancy_non_stream_response_floor_ms = ?,
redundancy_non_stream_no_content_timeout_ms = ?,
redundancy_non_stream_max_attempt_wait_ms = ?,
redundancy_per_input_token_response_lag_ms = ?,
redundancy_secondary_wait_after_winner_ms = ?,
redundancy_parallel_advantage_threshold = ?,
redundancy_unresponsive_threshold = ?,
redundancy_speed_policy = ?,
redundancy_pairwise_budget_percentile = ?,
redundancy_pairwise_max_proactive_attempts = ?,
redundancy_pairwise_min_direct_comparisons = ?,
redundancy_pairwise_winner_hold_ms = ?,
redundancy_pairwise_winner_hold_min_speedup = ?,
redundancy_pairwise_winner_hold_min_samples = ?,
perf_sample_size = ?,
perf_window_ms = ?,
escrow_rotation_enabled = ?,
escrow_rotation_settlement_enabled = ?,
escrow_rotation_pre_poc_blocks = ?,
escrow_rotation_models_json = ?,
gateway_disabled_enabled = ?,
gateway_disabled_message = ?,
gateway_disabled_new_url = ?,
updated_at = ?
WHERE id = 1`,
strings.TrimSpace(settings.ChainREST),
strings.TrimSpace(settings.PublicAPI),
strings.TrimSpace(settings.DefaultModel),
settings.DefaultRequestMaxTokens,
settings.RequestMaxTokensCap,
settings.MaxConcurrentRequests,
settings.MaxConcurrentPer10000Weight,
settings.PoCMaxConcurrentPer10000Weight,
settings.MaxInputTokensInFlight,
mustMarshalGatewayModelLimits(settings.ModelLimits),
"",
settings.TxGasLimit,
settings.ParticipantThrottle.RequestBurst,
settings.ParticipantThrottle.RecoveryPerMinute,
settings.ParticipantThrottle.HTTPQuarantineMS,
settings.ParticipantThrottle.TransportFailureQuarantineMS,
settings.ParticipantThrottle.EmptyStreamQuarantineMS,
settings.ParticipantThrottle.StalledWinnerQuarantineMS,
settings.ParticipantThrottle.EmptyStreamQuarantineThreshold,
settings.ParticipantThrottle.EOFTransportFailureThreshold,
settings.Redundancy.ReceiptTimeoutMS,
settings.Redundancy.FirstTokenTimeoutFloorMS,
settings.Redundancy.PerInputTokenFirstTokenLagMS,
settings.Redundancy.InterChunkStallTimeoutMS,
settings.Redundancy.StreamingAttemptHardTimeoutMS,
settings.Redundancy.NonStreamResponseFloorMS,
settings.Redundancy.NonStreamNoContentTimeoutMS,
settings.Redundancy.NonStreamMaxAttemptWaitMS,
settings.Redundancy.PerInputTokenResponseLagMS,
settings.Redundancy.SecondaryWaitAfterWinnerMS,
settings.Redundancy.ParallelAdvantageThreshold,
settings.Redundancy.UnresponsiveThreshold,
settings.Redundancy.SpeedPolicy,
settings.Redundancy.PairwiseBudgetPercentile,
settings.Redundancy.PairwiseMaxProactiveAttempts,
settings.Redundancy.PairwiseMinDirectComparisons,
settings.Redundancy.PairwiseWinnerHoldMS,
settings.Redundancy.PairwiseWinnerHoldMinSpeedup,
settings.Redundancy.PairwiseWinnerHoldMinSamples,
settings.Perf.SampleSize,
settings.Perf.WindowMS,
gatewayBoolToInt(settings.EscrowRotation.Enabled),
gatewayBoolToInt(settings.EscrowRotation.SettlementEnabled),
settings.EscrowRotation.PrePoCBlocks,
mustMarshalEscrowRotationModels(settings.EscrowRotation.Models),
gatewayBoolToInt(settings.Disabled.Enabled),
strings.TrimSpace(settings.Disabled.Message),
strings.TrimSpace(settings.Disabled.NewURL),
time.Now().UTC().Format(time.RFC3339Nano),
)
if err != nil {
return fmt.Errorf("update gateway settings: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("rows affected for gateway settings update: %w", err)
}
if n == 0 {
return fmt.Errorf("gateway settings not initialized")
}
return nil
}
type GatewayRotationStatus struct {
ModelID string `json:"model_id"`
Stage string `json:"stage"`
Epoch uint64 `json:"epoch"`
Role string `json:"role,omitempty"`
TargetCount int `json:"target_count"`
ExistingCount int `json:"existing_count"`
CreatedCount int `json:"created_count"`
PromotedCount int `json:"promoted_count"`
SettledCount int `json:"settled_count"`
SettleFailedCount int `json:"settle_failed_count"`
CreateError string `json:"create_error,omitempty"`
Completed bool `json:"completed"`
UpdatedAt string `json:"updated_at"`
}
func (s *GatewayStore) SaveRotationStatus(status GatewayRotationStatus) error {
if s == nil || s.db == nil {
return nil
}
now := time.Now().UTC().Format(time.RFC3339Nano)
if strings.TrimSpace(status.UpdatedAt) != "" {
now = strings.TrimSpace(status.UpdatedAt)
}
_, err := s.db.Exec(`
INSERT OR REPLACE INTO gateway_rotation_status (
model_id, stage, epoch, role, target_count, existing_count, created_count,
promoted_count, settled_count, settle_failed_count, create_error, completed, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
strings.TrimSpace(status.ModelID),
strings.TrimSpace(status.Stage),
status.Epoch,
strings.TrimSpace(status.Role),
status.TargetCount,
status.ExistingCount,
status.CreatedCount,
status.PromotedCount,
status.SettledCount,
status.SettleFailedCount,
strings.TrimSpace(status.CreateError),
gatewayBoolToInt(status.Completed),
now,
)
if err != nil {
return fmt.Errorf("save gateway rotation status model=%q stage=%q epoch=%d: %w", status.ModelID, status.Stage, status.Epoch, err)
}
return nil
}
func (s *GatewayStore) LoadRotationStatuses(limit int) ([]GatewayRotationStatus, error) {
if s == nil || s.db == nil {
return nil, nil
}
query := `
SELECT model_id, stage, epoch, role, target_count, existing_count, created_count,
promoted_count, settled_count, settle_failed_count, create_error, completed, updated_at
FROM gateway_rotation_status
ORDER BY updated_at DESC`
args := []any{}
if limit > 0 {
query += ` LIMIT ?`
args = append(args, limit)
}
rows, err := s.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("load gateway rotation statuses: %w", err)
}
defer rows.Close()
var statuses []GatewayRotationStatus
for rows.Next() {
var status GatewayRotationStatus
var completed int
if err := rows.Scan(