trypema 2.0.0

High-performance rate limiting primitives in Rust, designed for concurrency safety, low overhead, and predictable latency.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
//! Redis Lua scripts used by the Redis and hybrid rate limiters.
//!
//! Keeping the scripts in one module makes their atomic state transitions easier to audit and
//! prevents provider implementations from mixing Redis commands with Rust orchestration.

use redis::Script;

/// Helpers shared by every Redis rate-limiter script.
const COMMON_LUA_HELPERS: &str = r#"
    local function now_ms()
        local time_array = redis.call("TIME")
        return tonumber(time_array[1]) * 1000 + math.floor(tonumber(time_array[2]) / 1000)
    end

    local function expired_bucket_keys(active_keys, timestamp_ms, window_size_ms)
        local expired_before = "(" .. tostring(timestamp_ms - window_size_ms)
        return redis.call("ZRANGE", active_keys, "-inf", expired_before, "BYSCORE")
    end

    local function sum_values(values)
        local total = 0
        for i = 1, #values do
            total = total + (tonumber(values[i]) or 0)
        end
        return total
    end

    local function comparator_matches(value, comparator_op, comparator_operand)
        if comparator_op == "nil" then
            return true
        elseif comparator_op == "eq" then
            return value == comparator_operand
        elseif comparator_op == "lt" then
            return value < comparator_operand
        elseif comparator_op == "gt" then
            return value > comparator_operand
        elseif comparator_op == "ne" then
            return value ~= comparator_operand
        end

        return nil
    end

    local function valid_history_mode(history_mode)
        return history_mode == "replace"
            or history_mode == "preserve_newest"
            or history_mode == "preserve_oldest"
    end

    local function write_bucket_timestamp(active_keys, timestamp_ms, rate_group_size_ms)
        local latest = redis.call("ZRANGE", active_keys, 0, 0, "REV", "WITHSCORES")

        if #latest == 0 then
            return timestamp_ms
        end

        local latest_timestamp_ms = tonumber(latest[2])
        local age_ms = timestamp_ms - latest_timestamp_ms

        if age_ms >= 0 and age_ms <= rate_group_size_ms then
            return tonumber(latest[1])
        end

        return timestamp_ms
    end

    local function oldest_bucket_metadata(hash_key, active_keys, timestamp_ms, window_size_ms)
        local oldest = redis.call("ZRANGE", active_keys, 0, 0, "WITHSCORES")
        if #oldest == 0 then
            return nil, nil
        end

        local count = tonumber(redis.call("HGET", hash_key, oldest[1])) or 0
        local retry_after_ms = window_size_ms - timestamp_ms + (tonumber(oldest[2]) or 0)
        return retry_after_ms, count
    end

    local function cleanup_stale_entities(prefix, rate_type, active_entities_key, timestamp_ms, stale_after_ms, suffixes)
        local stale_entities = redis.call(
            "ZRANGE",
            active_entities_key,
            "-inf",
            timestamp_ms - stale_after_ms,
            "BYSCORE"
        )

        if #stale_entities == 0 then
            return
        end

        local remove_keys = {}

        for i = 1, #stale_entities do
            local entity = stale_entities[i]

            for j = 1, #suffixes do
                table.insert(
                    remove_keys,
                    prefix .. ":" .. entity .. ":" .. rate_type .. ":" .. suffixes[j]
                )
            end
        end

        redis.call("DEL", unpack(remove_keys))
        redis.call("ZREM", active_entities_key, unpack(stale_entities))
    end
"#;

/// Helpers shared by absolute Redis and hybrid scripts.
const ABSOLUTE_LUA_HELPERS: &str = r#"
    local function cleanup_expired_absolute(hash_key, active_keys, total_count_key, timestamp_ms, window_size_ms, delete_empty_total)
        local expired_keys = expired_bucket_keys(active_keys, timestamp_ms, window_size_ms)
        local total_count = tonumber(redis.call("GET", total_count_key)) or 0

        if #expired_keys == 0 then
            return total_count, false
        end

        local expired_counts = redis.call("HMGET", hash_key, unpack(expired_keys))
        local expired_sum = sum_values(expired_counts)
        redis.call("HDEL", hash_key, unpack(expired_keys))
        redis.call("ZREM", active_keys, unpack(expired_keys))

        if expired_sum > 0 or not delete_empty_total then
            total_count = redis.call("DECRBY", total_count_key, expired_sum)
            if delete_empty_total and total_count <= 0 then
                redis.call("DEL", total_count_key)
                total_count = 0
            end
        end

        return total_count, true
    end
"#;

/// Helpers shared by suppressed Redis and hybrid scripts.
const SUPPRESSED_LUA_HELPERS: &str = r#"
    local function cleanup_expired_suppressed(hash_key, hash_declined_key, active_keys, total_count_key, total_declined_key, timestamp_ms, window_size_ms, delete_empty_totals)
        local expired_keys = expired_bucket_keys(active_keys, timestamp_ms, window_size_ms)
        if #expired_keys == 0 then
            return false
        end

        local expired_counts = redis.call("HMGET", hash_key, unpack(expired_keys))
        local expired_declines = redis.call("HMGET", hash_declined_key, unpack(expired_keys))
        local expired_count_sum = sum_values(expired_counts)
        local expired_declined_sum = sum_values(expired_declines)

        redis.call("HDEL", hash_key, unpack(expired_keys))
        redis.call("HDEL", hash_declined_key, unpack(expired_keys))
        redis.call("ZREM", active_keys, unpack(expired_keys))

        if expired_count_sum > 0 or not delete_empty_totals then
            local remaining_total = redis.call("DECRBY", total_count_key, expired_count_sum)
            if delete_empty_totals and remaining_total <= 0 then
                redis.call("DEL", total_count_key)
            end
        end
        if expired_declined_sum > 0 or not delete_empty_totals then
            local remaining_declined = redis.call("DECRBY", total_declined_key, expired_declined_sum)
            if delete_empty_totals and remaining_declined <= 0 then
                redis.call("DEL", total_declined_key)
            end
        end
        return true
    end

    local function calculate_suppression_factor(hash_key, active_keys, total_count, total_declined, timestamp_ms, window_size_seconds, hard_window_limit, hard_limit_factor)
        if hard_window_limit == nil then
            return 0
        end

        local hard_window_capacity = math.floor(hard_window_limit)
        local soft_window_limit = math.floor(hard_window_limit / hard_limit_factor)
        local accepted = total_count - total_declined

        if total_count >= hard_window_capacity then
            return 1
        elseif accepted < soft_window_limit then
            return 0
        elseif accepted == soft_window_limit and soft_window_limit == hard_window_capacity then
            return 1
        end

        local active_keys_in_1s = redis.call(
            "ZRANGE",
            active_keys,
            "+inf",
            timestamp_ms - 1000,
            "BYSCORE",
            "REV"
        )
        local total_in_last_second = 0
        if #active_keys_in_1s > 0 then
            total_in_last_second = sum_values(redis.call("HMGET", hash_key, unpack(active_keys_in_1s)))
        end

        local average_rate_in_window = total_count / window_size_seconds
        local perceived_rate_limit = math.max(average_rate_in_window, total_in_last_second)
        local rate_limit = soft_window_limit / window_size_seconds
        return 1 - (rate_limit / perceived_rate_limit)
    end
"#;

// Absolute rate-limiter scripts.

pub(crate) const ABSOLUTE_INC_LUA: &str = r#"
    local timestamp_ms = now_ms()
    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local requested_window_limit = tonumber(ARGV[3])
    local window_limit = tonumber(redis.call("GET", window_limit_key)) or requested_window_limit
    local window_capacity = math.floor(window_limit)
    local rate_group_size_ms = tonumber(ARGV[4])
    local count = tonumber(ARGV[5])

    redis.call("ZADD", active_entities_key, timestamp_ms, entity)


    local total_count = tonumber(redis.call("GET", total_count_key)) or 0

    if total_count + count > window_capacity then
        total_count = cleanup_expired_absolute(
            hash_key,
            active_keys,
            total_count_key,
            timestamp_ms,
            window_size_seconds * 1000,
            false
        )
    end

    if total_count + count > window_capacity then
        local retry_after_ms, oldest_count = oldest_bucket_metadata(
            hash_key,
            active_keys,
            timestamp_ms,
            window_size_seconds * 1000
        )

        if retry_after_ms == nil then
            return {"rejected", 0, 0}
        end

        return {"rejected", retry_after_ms, oldest_count}
    end

    timestamp_ms = write_bucket_timestamp(active_keys, timestamp_ms, rate_group_size_ms)

    local hash_field = tostring(timestamp_ms)

    local new_count = redis.call("HINCRBY", hash_key, hash_field, count)
    redis.call("INCRBY", total_count_key, count)

    if new_count == count then
        redis.call("ZADD", active_keys, timestamp_ms, hash_field)
        redis.call("SET", window_limit_key, window_limit)
    end

    redis.call("EXPIRE", window_limit_key, window_size_seconds)

    return {"allowed", 0, 0}
"#;

pub(crate) const ABSOLUTE_IS_ALLOWED_LUA: &str = r#"
    local timestamp_ms = now_ms()
    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]

    local window_size_seconds = tonumber(ARGV[1])
    local window_limit = tonumber(redis.call("GET", window_limit_key))

    if window_limit == nil then
        return {"allowed", 0, 0}
    end

    local window_capacity = math.floor(window_limit)
    local total_count = tonumber(redis.call("GET", total_count_key)) or 0

    if total_count >= window_capacity then
        total_count = cleanup_expired_absolute(
            hash_key,
            active_keys,
            total_count_key,
            timestamp_ms,
            window_size_seconds * 1000,
            false
        )
    end

    if total_count >= window_capacity then
        local retry_after_ms, oldest_count = oldest_bucket_metadata(
            hash_key,
            active_keys,
            timestamp_ms,
            window_size_seconds * 1000
        )

        if retry_after_ms == nil then
            return {"rejected", 0, 0}
        end

        return {"rejected", retry_after_ms, oldest_count}
    end

    return {"allowed", 0, 0}
"#;

pub(crate) const ABSOLUTE_CLEANUP_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local prefix = KEYS[1]
    local rate_type = KEYS[2]
    local active_entities_key = KEYS[3]

    local stale_after_ms = tonumber(ARGV[1]) or 0
    local hash_suffix = ARGV[2]
    local window_limit_suffix = ARGV[3]
    local total_count_suffix = ARGV[4]
    local active_keys_suffix = ARGV[5]
    local suppression_factor_key_suffix = ARGV[6]


    local suffixes = {hash_suffix, window_limit_suffix, total_count_suffix, active_keys_suffix, suppression_factor_key_suffix}

    cleanup_stale_entities(
        prefix,
        rate_type,
        active_entities_key,
        timestamp_ms,
        stale_after_ms,
        suffixes
    )
"#;

/// Read an absolute sliding window's live total, evicting expired buckets first.
///
/// Existing entities are marked active so cleanup tracking stays consistent; unknown reads
/// remain absent.
pub(crate) const ABSOLUTE_GET_TOTAL_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local total_count_key = KEYS[3]
    local active_entities_key = KEYS[4]
    local window_limit_key = KEYS[5]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])

    local total_count = cleanup_expired_absolute(
        hash_key,
        active_keys,
        total_count_key,
        timestamp_ms,
        window_size_seconds * 1000,
        true
    )

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, total_count_key, window_limit_key) > 0

    if entity_exists then
        if total_count < 0 then
            redis.call("SET", total_count_key, 0)
        end

        redis.call("ZADD", active_entities_key, timestamp_ms, entity)
    end

    if total_count < 0 then
        total_count = 0
    end

    return total_count
"#;

/// Conditionally update an absolute sliding window's total and history.
///
/// Shared by the Redis and hybrid absolute limiters. Comparator misses perform no writes, and
/// hybrid callers may include pending local counts in the atomic comparison.
pub(crate) const ABSOLUTE_SET_IF_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local window_limit = tonumber(ARGV[3])
    local comparator_op = ARGV[4]
    local comparator_operand = tonumber(ARGV[5])
    local count = tonumber(ARGV[6])
    local history_mode = ARGV[7]
    local pending_count = tonumber(ARGV[8]) or 0

    local expired_keys = expired_bucket_keys(active_keys, timestamp_ms, window_size_seconds * 1000)
    local expired_sum = 0

    if #expired_keys > 0 then
        local expired_counts = redis.call("HMGET", hash_key, unpack(expired_keys))
        expired_sum = sum_values(expired_counts)
    end

    local redis_total = (tonumber(redis.call("GET", total_count_key)) or 0) - expired_sum

    if redis_total < 0 then
        redis_total = 0
    end

    local old_total = redis_total + pending_count
    local matched = comparator_matches(old_total, comparator_op, comparator_operand)

    if matched == nil then
        return redis.error_reply("trypema: unknown comparator op: " .. tostring(comparator_op))
    end

    if not matched then
        return {old_total, old_total, 0}
    end

    if not valid_history_mode(history_mode) then
        return redis.error_reply("trypema: unknown history mode: " .. tostring(history_mode))
    end

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, window_limit_key, total_count_key) > 0
        or redis.call("ZSCORE", active_entities_key, entity) ~= false

    if not entity_exists and pending_count == 0 and count == 0 then
        return {0, 0, 0}
    end

    if count == 0 then
        redis.call("DEL", hash_key, active_keys, window_limit_key, total_count_key)
        redis.call("ZREM", active_entities_key, entity)

        return {0, old_total, 1}
    end

    local existing_limit = tonumber(redis.call("GET", window_limit_key))

    if history_mode ~= "replace" and #expired_keys == 0 and pending_count == 0 and count == old_total and existing_limit == window_limit then
        return {old_total, old_total, 0}
    end

    if history_mode == "replace" then
        redis.call("DEL", hash_key, active_keys)

        if count > 0 then
            local field = tostring(timestamp_ms)

            redis.call("HSET", hash_key, field, count)
            redis.call("ZADD", active_keys, timestamp_ms, field)
        end
    else
        if #expired_keys > 0 then
            redis.call("HDEL", hash_key, unpack(expired_keys))
            redis.call("ZREM", active_keys, unpack(expired_keys))
        end

        if pending_count > 0 then
            local pending_field = tostring(timestamp_ms)

            redis.call("HINCRBY", hash_key, pending_field, pending_count)
            redis.call("ZADD", active_keys, timestamp_ms, pending_field)
        end

        if count > old_total then
            local delta = count - old_total
            local edge

            if history_mode == "preserve_newest" then
                edge = redis.call("ZRANGE", active_keys, 0, 0, "REV")[1]
            else
                edge = redis.call("ZRANGE", active_keys, 0, 0)[1]
            end

            if not edge then
                edge = tostring(timestamp_ms)
                redis.call("ZADD", active_keys, timestamp_ms, edge)
            end

            redis.call("HINCRBY", hash_key, edge, delta)
        elseif count < old_total then
            local remaining = old_total - count
            local ordered

            if history_mode == "preserve_newest" then
                ordered = redis.call("ZRANGE", active_keys, 0, -1)
            else
                ordered = redis.call("ZRANGE", active_keys, 0, -1, "REV")
            end

            for i = 1, #ordered do
                if remaining <= 0 then break end

                local field = ordered[i]
                local bucket_count = tonumber(redis.call("HGET", hash_key, field)) or 0

                if bucket_count <= remaining then
                    remaining = remaining - bucket_count

                    redis.call("HDEL", hash_key, field)
                    redis.call("ZREM", active_keys, field)
                else
                    redis.call("HSET", hash_key, field, bucket_count - remaining)
                    remaining = 0
                end
            end
        end
    end

    redis.call("SET", total_count_key, count)

    redis.call("SET", window_limit_key, window_limit)
    redis.call("EXPIRE", window_limit_key, window_size_seconds)
    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    return {count, old_total, 1}
"#;

pub(crate) const ABSOLUTE_HYBRID_COMMIT_STATE_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local window_limit = tonumber(ARGV[3])
    local rate_group_size_ms = tonumber(ARGV[4])
    local count = tonumber(ARGV[5])


    cleanup_expired_absolute(
        hash_key,
        active_keys,
        total_count_key,
        timestamp_ms,
        window_size_seconds * 1000,
        false
    )
    timestamp_ms = write_bucket_timestamp(active_keys, timestamp_ms, rate_group_size_ms)

    local hash_field = tostring(timestamp_ms)
    local new_count = redis.call("HINCRBY", hash_key, hash_field, count)
    redis.call("INCRBY", total_count_key, count)

    if new_count == count then
        redis.call("ZADD", active_keys, timestamp_ms, hash_field)
        redis.call("SET", window_limit_key, window_limit)
    end


    local oldest_ttl, oldest_count = oldest_bucket_metadata(
        hash_key,
        active_keys,
        timestamp_ms,
        window_size_seconds * 1000
    )

    redis.call("EXPIRE", window_limit_key, window_size_seconds)
    redis.call("ZADD", active_entities_key, timestamp_ms, entity)
"#;

pub(crate) const ABSOLUTE_HYBRID_READ_STATE_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]

    local entity = ARGV[1]
    local window_size_ms = tonumber(ARGV[2])

    local window_limit = tonumber(redis.call("GET", window_limit_key))
    local total_count = cleanup_expired_absolute(
        hash_key,
        active_keys,
        total_count_key,
        timestamp_ms,
        window_size_ms,
        true
    )
    local oldest_ttl, oldest_count = oldest_bucket_metadata(
        hash_key,
        active_keys,
        timestamp_ms,
        window_size_ms
    )

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, window_limit_key, total_count_key) > 0
    if entity_exists then
        redis.call("ZADD", active_entities_key, timestamp_ms, entity)
    end

    return {entity, total_count, window_limit or -1, oldest_ttl or -1, oldest_count or -1}
"#;

// Suppressed rate-limiter scripts.

pub(crate) const SUPPRESSED_CLEANUP_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local prefix = KEYS[1]
    local rate_type = KEYS[2]
    local active_entities_key = KEYS[3]

    local stale_after_ms = tonumber(ARGV[1]) or 0
    local hash_suffix = ARGV[2]
    local window_limit_suffix = ARGV[3]
    local total_count_suffix = ARGV[4]
    local active_keys_suffix = ARGV[5]
    local suppression_factor_key_suffix = ARGV[6]
    local total_declined_suffix = ARGV[7]
    local hash_declined_suffix = ARGV[8]


    local suffixes = {hash_suffix, window_limit_suffix, total_count_suffix, active_keys_suffix, suppression_factor_key_suffix, total_declined_suffix, hash_declined_suffix}
    cleanup_stale_entities(
        prefix,
        rate_type,
        active_entities_key,
        timestamp_ms,
        stale_after_ms,
        suffixes
    )
"#;

pub(crate) const SUPPRESSED_INC_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]
    local suppression_factor_key = KEYS[6]
    local total_declined_key = KEYS[7]
    local hash_declined_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local hard_window_limit_to_consider = tonumber(ARGV[3])
    local rate_group_size_ms = tonumber(ARGV[4])
    local suppression_factor_cache_ms = tonumber(ARGV[5])
    local hard_limit_factor = tonumber(ARGV[6])
    local count = tonumber(ARGV[7])

    local hard_window_limit = tonumber(redis.call("GET", window_limit_key)) or hard_window_limit_to_consider

    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    local evicted = cleanup_expired_suppressed(
        hash_key,
        hash_declined_key,
        active_keys,
        total_count_key,
        total_declined_key,
        timestamp_ms,
        window_size_seconds * 1000,
        false
    )
    if evicted then
        redis.call("DEL", suppression_factor_key)
    end

    local total_count = tonumber(redis.call("GET", total_count_key)) or 0
    local total_declined = tonumber(redis.call("GET", total_declined_key)) or 0
    local hard_window_capacity = math.floor(hard_window_limit)
    local soft_window_limit = math.floor(hard_window_limit / hard_limit_factor)
    local forecasted_allowed = (total_count - total_declined) + count
    local reached_hard_window_limit = forecasted_allowed == hard_window_capacity
    local should_return_allowed = forecasted_allowed <= soft_window_limit or reached_hard_window_limit
    local suppression_factor
    local should_allow = true

    if should_return_allowed then
        should_allow = true

        if reached_hard_window_limit then
            suppression_factor = 1
            redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
        else
            suppression_factor = 0

        end
    else
        if forecasted_allowed > hard_window_capacity then
            suppression_factor = 1
            redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
        else
            suppression_factor = tonumber(redis.call("GET", suppression_factor_key))

            if suppression_factor == nil or suppression_factor < 0 or suppression_factor > 1 then
                suppression_factor = calculate_suppression_factor(hash_key, active_keys, total_count, total_declined, timestamp_ms, window_size_seconds, hard_window_limit, hard_limit_factor)
                redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
            end
        end

        if suppression_factor == 0 then
            should_allow = true
        elseif suppression_factor == 1 then
            should_allow = false
        else
            should_allow = math.random() < (1 - suppression_factor)
        end
    end

    timestamp_ms = write_bucket_timestamp(active_keys, timestamp_ms, rate_group_size_ms)

    local hash_field = tostring(timestamp_ms)

    local new_count = redis.call("HINCRBY", hash_key, hash_field, count)
    redis.call("INCRBY", total_count_key, count)

    if not should_allow then
        redis.call("HINCRBY", hash_declined_key, hash_field, count)
        redis.call("INCRBY", total_declined_key, count)
    end

    if new_count == count then
        redis.call("ZADD", active_keys, timestamp_ms, hash_field)
        redis.call("SET", window_limit_key, hard_window_limit)
    end

    redis.call("EXPIRE", window_limit_key, window_size_seconds)

    if should_return_allowed then
        return {"allowed", 0, 0}
    else
        return {"suppressed", tostring(suppression_factor), should_allow and "1" or "0"}
    end
"#;

pub(crate) const SUPPRESSED_GET_FACTOR_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]
    local suppression_factor_key = KEYS[6]
    local total_declined_key = KEYS[7]
    local hash_declined_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local suppression_factor_cache_ms = tonumber(ARGV[4])
    local hard_limit_factor = tonumber(ARGV[5])

    local evicted = cleanup_expired_suppressed(
        hash_key,
        hash_declined_key,
        active_keys,
        total_count_key,
        total_declined_key,
        timestamp_ms,
        window_size_seconds * 1000,
        false
    )
    if evicted then
        redis.call("DEL", suppression_factor_key)
    end

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, window_limit_key, total_count_key, total_declined_key, hash_declined_key) > 0
    if not entity_exists then
        return "0"
    end

    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    local total_count = tonumber(redis.call("GET", total_count_key)) or 0
    local total_declined = tonumber(redis.call("GET", total_declined_key)) or 0

    local suppression_factor = tonumber(redis.call("GET", suppression_factor_key))

    if suppression_factor == nil or suppression_factor < 0 or suppression_factor > 1 then
        local hard_window_limit = tonumber(redis.call("GET", window_limit_key))
        if hard_window_limit == nil then
            suppression_factor = 0
        else
            suppression_factor = calculate_suppression_factor(hash_key, active_keys, total_count, total_declined, timestamp_ms, window_size_seconds, hard_window_limit, hard_limit_factor)
        end

        redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
    end

    return tostring(suppression_factor)
"#;

/// Read a suppressed sliding window's live state, evicting expired buckets first.
///
/// Count and declined-count metadata are evicted together. Existing entities are marked active;
/// unknown reads remain absent.
pub(crate) const SUPPRESSED_GET_STATE_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local total_count_key = KEYS[3]
    local active_entities_key = KEYS[4]
    local total_declined_key = KEYS[5]
    local hash_declined_key = KEYS[6]
    local window_limit_key = KEYS[7]
    local suppression_factor_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local suppression_factor_cache_ms = tonumber(ARGV[3])
    local hard_limit_factor = tonumber(ARGV[4])


    local evicted = cleanup_expired_suppressed(
        hash_key,
        hash_declined_key,
        active_keys,
        total_count_key,
        total_declined_key,
        timestamp_ms,
        window_size_seconds * 1000,
        true
    )

    if evicted then
        redis.call("DEL", suppression_factor_key)
    end

    local total_count = tonumber(redis.call("GET", total_count_key)) or 0

    if total_count < 0 then
        total_count = 0
    end

    local total_declined = tonumber(redis.call("GET", total_declined_key)) or 0

    if total_declined < 0 then
        total_declined = 0
    elseif total_declined > total_count then
        total_declined = total_count
    end

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, total_count_key, total_declined_key, hash_declined_key, window_limit_key, suppression_factor_key) > 0

    if not entity_exists then
        return {0, 0, "0"}
    end

    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    local suppression_factor = tonumber(redis.call("GET", suppression_factor_key))

    if suppression_factor == nil or suppression_factor < 0 or suppression_factor > 1 then
        local hard_window_limit = tonumber(redis.call("GET", window_limit_key))
        suppression_factor = calculate_suppression_factor(hash_key, active_keys, total_count, total_declined, timestamp_ms, window_size_seconds, hard_window_limit, hard_limit_factor)

        redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
    end

    return {total_count, total_declined, tostring(suppression_factor)}
"#;

/// Conditionally update a suppressed sliding window's total and history.
///
/// Shared by the Redis and hybrid suppressed limiters. This mirrors the absolute conditional-set
/// semantics while preserving proportional declined history on partial reductions.
pub(crate) const SUPPRESSED_SET_IF_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]
    local suppression_factor_key = KEYS[6]
    local total_declined_key = KEYS[7]
    local hash_declined_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local hard_window_limit = tonumber(ARGV[3])
    local comparator_op = ARGV[4]
    local comparator_operand = tonumber(ARGV[5])
    local count = tonumber(ARGV[6])
    local history_mode = ARGV[7]
    local pending_count = tonumber(ARGV[8]) or 0
    local pending_declined = tonumber(ARGV[9]) or 0

    local expired_keys = expired_bucket_keys(active_keys, timestamp_ms, window_size_seconds * 1000)
    local expired_sum = 0
    local expired_declined = 0
    if #expired_keys > 0 then
        local expired_counts = redis.call("HMGET", hash_key, unpack(expired_keys))
        local expired_declines = redis.call("HMGET", hash_declined_key, unpack(expired_keys))
        expired_sum = sum_values(expired_counts)
        expired_declined = sum_values(expired_declines)
    end

    local redis_total = (tonumber(redis.call("GET", total_count_key)) or 0) - expired_sum
    if redis_total < 0 then redis_total = 0 end
    local redis_declined = (tonumber(redis.call("GET", total_declined_key)) or 0) - expired_declined
    if redis_declined < 0 then redis_declined = 0 end
    local old_total = redis_total + pending_count
    local old_declined = redis_declined + pending_declined
    if old_declined > old_total then
        old_declined = old_total
    end

    local matched = comparator_matches(old_total, comparator_op, comparator_operand)
    if matched == nil then
        return redis.error_reply("trypema: unknown comparator op: " .. tostring(comparator_op))
    end

    if not matched then
        return {old_total, old_total, 0}
    end

    if not valid_history_mode(history_mode) then
        return redis.error_reply("trypema: unknown history mode: " .. tostring(history_mode))
    end

    local entity_exists = redis.call("EXISTS", hash_key, active_keys, window_limit_key, total_count_key, suppression_factor_key, total_declined_key, hash_declined_key) > 0
        or redis.call("ZSCORE", active_entities_key, entity) ~= false
    if not entity_exists and pending_count == 0 and count == 0 then
        return {0, 0, 0}
    end

    if count == 0 then
        redis.call("DEL", hash_key, hash_declined_key, active_keys, window_limit_key, total_count_key, total_declined_key, suppression_factor_key)
        redis.call("ZREM", active_entities_key, entity)
        return {0, old_total, 1}
    end

    local existing_limit = tonumber(redis.call("GET", window_limit_key))
    if history_mode ~= "replace" and #expired_keys == 0 and pending_count == 0 and pending_declined == 0 and count == old_total and existing_limit == hard_window_limit then
        return {old_total, old_total, 0}
    end

    local new_declined = 0
    if history_mode == "replace" then
        redis.call("DEL", hash_key, hash_declined_key, active_keys)
        if count > 0 then
            local field = tostring(timestamp_ms)
            redis.call("HSET", hash_key, field, count)
            redis.call("ZADD", active_keys, timestamp_ms, field)
        end
    else
        if #expired_keys > 0 then
            redis.call("HDEL", hash_key, unpack(expired_keys))
            redis.call("HDEL", hash_declined_key, unpack(expired_keys))
            redis.call("ZREM", active_keys, unpack(expired_keys))
        end

        if pending_count > 0 then
            local pending_field = tostring(timestamp_ms)
            redis.call("HINCRBY", hash_key, pending_field, pending_count)
            if pending_declined > 0 then
                redis.call("HINCRBY", hash_declined_key, pending_field, pending_declined)
            end
            redis.call("ZADD", active_keys, timestamp_ms, pending_field)
        end

        new_declined = old_declined
        if count > old_total then
            local delta = count - old_total
            local edge
            if history_mode == "preserve_newest" then
                edge = redis.call("ZRANGE", active_keys, 0, 0, "REV")[1]
            else
                edge = redis.call("ZRANGE", active_keys, 0, 0)[1]
            end
            if not edge then
                edge = tostring(timestamp_ms)
                redis.call("ZADD", active_keys, timestamp_ms, edge)
            end
            redis.call("HINCRBY", hash_key, edge, delta)
        elseif count < old_total then
            local remaining = old_total - count
            local ordered
            if history_mode == "preserve_newest" then
                ordered = redis.call("ZRANGE", active_keys, 0, -1)
            else
                ordered = redis.call("ZRANGE", active_keys, 0, -1, "REV")
            end

            for i = 1, #ordered do
                if remaining <= 0 then break end
                local field = ordered[i]
                local bucket_count = tonumber(redis.call("HGET", hash_key, field)) or 0
                local bucket_declined = tonumber(redis.call("HGET", hash_declined_key, field)) or 0
                if bucket_count <= remaining then
                    remaining = remaining - bucket_count
                    new_declined = new_declined - bucket_declined
                    redis.call("HDEL", hash_key, field)
                    redis.call("HDEL", hash_declined_key, field)
                    redis.call("ZREM", active_keys, field)
                else
                    local retained = bucket_count - remaining
                    local retained_declined = math.floor(bucket_declined * retained / bucket_count)
                    new_declined = new_declined - bucket_declined + retained_declined
                    redis.call("HSET", hash_key, field, retained)
                    if retained_declined > 0 then
                        redis.call("HSET", hash_declined_key, field, retained_declined)
                    else
                        redis.call("HDEL", hash_declined_key, field)
                    end
                    remaining = 0
                end
            end
        end
    end

    redis.call("SET", total_count_key, count)
    if new_declined > 0 then
        redis.call("SET", total_declined_key, new_declined)
    else
        redis.call("DEL", total_declined_key)
    end

    redis.call("DEL", suppression_factor_key)
    redis.call("SET", window_limit_key, hard_window_limit)
    redis.call("EXPIRE", window_limit_key, window_size_seconds)
    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    return {count, old_total, 1}
"#;

pub(crate) const SUPPRESSED_HYBRID_COMMIT_STATE_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]
    local suppression_factor_key = KEYS[6]
    local total_declined_key = KEYS[7]
    local hash_declined_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local hard_window_limit_to_consider = tonumber(ARGV[3])
    local rate_group_size_ms = tonumber(ARGV[4])
    local suppression_factor_cache_ms = tonumber(ARGV[5])
    local hard_limit_factor = tonumber(ARGV[6])
    local count = tonumber(ARGV[7])
    local declined_count = tonumber(ARGV[8])

    local hard_window_limit = tonumber(redis.call("GET", window_limit_key)) or hard_window_limit_to_consider

    redis.call("ZADD", active_entities_key, timestamp_ms, entity)

    local evicted = cleanup_expired_suppressed(
        hash_key,
        hash_declined_key,
        active_keys,
        total_count_key,
        total_declined_key,
        timestamp_ms,
        window_size_seconds * 1000,
        true
    )
    if evicted then
        redis.call("DEL", suppression_factor_key)
    end

    timestamp_ms = write_bucket_timestamp(active_keys, timestamp_ms, rate_group_size_ms)

    local hash_field = tostring(timestamp_ms)

    local new_count = redis.call("HINCRBY", hash_key, hash_field, count)
    local new_total_count = redis.call("INCRBY", total_count_key, count)
    local new_total_declined = tonumber(redis.call("GET", total_declined_key)) or 0

    if declined_count > 0 then
        redis.call("HINCRBY", hash_declined_key, hash_field, declined_count)
        new_total_declined = redis.call("INCRBY", total_declined_key, declined_count)
    end

    if (new_total_count - new_total_declined) >= math.floor(hard_window_limit) then
        redis.call("SET", suppression_factor_key, 1, "PX", suppression_factor_cache_ms)
    end

    if new_count == count then
        redis.call("ZADD", active_keys, timestamp_ms, hash_field)
        redis.call("SET", window_limit_key, hard_window_limit)
    end

    redis.call("EXPIRE", window_limit_key, window_size_seconds)
"#;

pub(crate) const SUPPRESSED_HYBRID_READ_STATE_LUA: &str = r#"
    local timestamp_ms = now_ms()

    local hash_key = KEYS[1]
    local active_keys = KEYS[2]
    local window_limit_key = KEYS[3]
    local total_count_key = KEYS[4]
    local active_entities_key = KEYS[5]
    local suppression_factor_key = KEYS[6]
    local total_declined_key = KEYS[7]
    local hash_declined_key = KEYS[8]

    local entity = ARGV[1]
    local window_size_seconds = tonumber(ARGV[2])
    local suppression_factor_cache_ms = tonumber(ARGV[3])
    local hard_limit_factor = tonumber(ARGV[4])

    local hard_window_limit = tonumber(redis.call("GET", window_limit_key))

    local evicted = cleanup_expired_suppressed(
        hash_key,
        hash_declined_key,
        active_keys,
        total_count_key,
        total_declined_key,
        timestamp_ms,
        window_size_seconds * 1000,
        true
    )
    if evicted then
        redis.call("DEL", suppression_factor_key)
    end

    local total_count = tonumber(redis.call("GET", total_count_key)) or 0
    local total_declined = tonumber(redis.call("GET", total_declined_key)) or 0
    local entity_exists = redis.call("EXISTS", hash_key, active_keys, window_limit_key, total_count_key, suppression_factor_key, total_declined_key, hash_declined_key) > 0

    local suppression_factor = tonumber(redis.call("GET", suppression_factor_key))

    if suppression_factor == nil or suppression_factor < 0 or suppression_factor > 1 then
        suppression_factor = calculate_suppression_factor(hash_key, active_keys, total_count, total_declined, timestamp_ms, window_size_seconds, hard_window_limit, hard_limit_factor)

        if entity_exists then
            redis.call("SET", suppression_factor_key, suppression_factor, "PX", suppression_factor_cache_ms)
        end
    end

    local suppression_factor_ttl_ms = redis.call("PTTL", suppression_factor_key)

    if suppression_factor_ttl_ms < 0 then
        suppression_factor_ttl_ms = -1
    end

    if entity_exists then
        redis.call("ZADD", active_entities_key, timestamp_ms, entity)
    end

    return {entity, tostring(suppression_factor), total_count, total_declined, tostring(hard_window_limit or -1), suppression_factor_ttl_ms }
"#;

/// Build a script with the common helper prelude.
pub(crate) fn lua_script(body: &str) -> Script {
    Script::new(&format!("{COMMON_LUA_HELPERS}\n{body}"))
}

/// Build an absolute script with the common and absolute helper preludes.
pub(crate) fn absolute_lua_script(body: &str) -> Script {
    Script::new(&format!(
        "{COMMON_LUA_HELPERS}\n{ABSOLUTE_LUA_HELPERS}\n{body}"
    ))
}

/// Build a suppressed script with the common and suppressed helper preludes.
pub(crate) fn suppressed_lua_script(body: &str) -> Script {
    Script::new(&format!(
        "{COMMON_LUA_HELPERS}\n{SUPPRESSED_LUA_HELPERS}\n{body}"
    ))
}