recapi 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -1
  3. data/lib/recapi.rb +15 -7
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddfbe7da7e2ca36f680b013e06c795a516645d415329f1dc61892024e129d477
4
- data.tar.gz: dacd7f80f58ecda60c653380d73e63a396315f906339d9940941922a1ed6333e
3
+ metadata.gz: 5494f67d90d8ce8686f9036fd05c9f1549a2af21a3af16734d82415bcf98fcde
4
+ data.tar.gz: e41d02f51ea550051320c17a5d2c93917caa9b9e87ae8b9cf1e626f2ff4ff353
5
5
  SHA512:
6
- metadata.gz: f5309158f3114f5d2d314045358364a9467a867aa4324e8650b6dc6269583de306c74e0f5a288f81cbfdf27adc75ca778a0f8d7d12a63710ac1c7c98158d4cfc
7
- data.tar.gz: 82124ece99593007209d5a4e040c7d1f6768d47ae6ee93a0892f734d83c964b5d10e34d7198badb53453cab39be36ee1bb2bf18d43ac802f62806ae8e525c780
6
+ metadata.gz: bf77268b88e6b47414b83d527afab3a6766683d06d6793298b8a246a0d613b4a51fc8116ffc42ae78ff9e4af20ee28f0177cc7cb1fd60cad3e49b9ec488176c8
7
+ data.tar.gz: a69a0f1965f86bfeb71b33696cf5dd945b74c3cdbacab24c2e80bd0009129edb54ac92dba2a197d2dad5f7752ff0159dc190afc504dd452398f919d8af1c2233
data/README.md CHANGED
@@ -30,7 +30,7 @@ line with your address): save it as `lib/recapi.rb` and
30
30
  `require Rails.root.join("lib/recapi").to_s` instead.
31
31
 
32
32
  ```sh
33
- curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.11.rb
33
+ curl -fLo lib/recapi.rb https://api.<your-domain>/sdk/ruby/recapi-0.1.12.rb
34
34
  ```
35
35
 
36
36
  ## Quick start
@@ -124,6 +124,10 @@ so a visitor who never returns stays on disk (about 8 KB each): run
124
124
 
125
125
  ## Changes
126
126
 
127
+ - 2026-11-07: version 0.1.12. A saved history time ahead of the clock reads as now,
128
+ and a damaged weight is sent as the API's limit (1e6). A time months ahead, or a
129
+ weight past that limit, used to get a 400 on every list, and far enough past it
130
+ raised `FloatDomainError`: the note below now holds for those too.
127
131
  - 2026-11-07: version 0.1.11. A damaged stored state can't raise into your page
128
132
  or stop a visitor's history from saving. A shown entry of the wrong types
129
133
  costs that item's click credit, and a history time too big for a number
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.11"
28
+ VERSION = "0.1.12"
29
29
 
30
30
  # Decayed user history, exactly as docs/history-spec.md (times in unix ms).
31
31
  # A port of sdk/python's history.py, snapshot merging included.
@@ -35,6 +35,8 @@ module Recapi
35
35
  RECENT_SIZE = 20
36
36
  REJECTED_SIZE = 200
37
37
  MAX_ID_BYTES = 256
38
+ # The API answers 400 above this, which fails the list: a damaged score is sent as this.
39
+ MAX_REQUEST_WEIGHT = 1e6
38
40
  RATE = Math.log(2) / HALF_LIFE_SECONDS
39
41
  # Go's unicode.IsControl, which the API applies to every id: C0, DEL and C1.
40
42
  CONTROL = /[\u0000-\u001f\u007f-\u009f]/.freeze
@@ -108,9 +110,11 @@ module Recapi
108
110
  prune(t)
109
111
  end
110
112
 
111
- # [item, weight at now, t] by weight desc, ties by item id asc.
113
+ # [item, weight at now, t] by weight desc, ties by item id asc. A time ahead
114
+ # of now reads as now: a clock that ran ahead must not grow the weight past
115
+ # what the API takes.
112
116
  def ranked(now)
113
- entries = @top.map { |item, (s, t)| [item, s * History.decay(now - t), t] }
117
+ entries = @top.map { |item, (s, t)| [item, s * History.decay([0, now - t].max), t] }
114
118
  entries.sort_by { |item, w, _| [-w, item] }
115
119
  end
116
120
 
@@ -120,11 +124,12 @@ module Recapi
120
124
  end
121
125
 
122
126
  # The request's history and recent, and the rejected items. Weights are
123
- # decayed to now and rounded half up to 4 decimals like JS Math.round
124
- # (Ruby's round rounds half away from zero); ages are whole seconds, never negative.
127
+ # decayed to now, at most what the API accepts, and rounded half up to 4
128
+ # decimals like JS Math.round (Ruby's round rounds half away from zero); ages
129
+ # are whole seconds, never negative.
125
130
  def wire(now)
126
131
  history = ranked(now).first(TOP_K).map do |item, weight, t|
127
- [item, (weight * 1e4 + 0.5).floor / 1e4, [0, ((now - t) / 1000.0).floor].max]
132
+ [item, ([weight, MAX_REQUEST_WEIGHT].min * 1e4 + 0.5).floor / 1e4, [0, ((now - t) / 1000.0).floor].max]
128
133
  end
129
134
  [history, @recent.last(RECENT_SIZE).map(&:first), rejected_order]
130
135
  end
@@ -268,7 +273,10 @@ module Recapi
268
273
  def prune(now)
269
274
  return unless @top.size > TOP_K
270
275
 
271
- @top = ranked(now).first(TOP_K).to_h { |item, _, _| [item, @top[item]] }
276
+ # Read at the newest entry when an event arrives out of order: at the old
277
+ # event's time ranked would flatten the newer entries to their score.
278
+ at = [now, *@top.values.map(&:last)].max
279
+ @top = ranked(at).first(TOP_K).to_h { |item, _, _| [item, @top[item]] }
272
280
  end
273
281
 
274
282
  def prune_rejected
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - recapi