recapi 0.1.10 → 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 +9 -1
  3. data/lib/recapi.rb +26 -9
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9d8c7e28e6929bfef64625ac5ba48ba5bec683d27b3d4c91646b67f189cadeb
4
- data.tar.gz: b17b8ad7945fac43147ce43aca7cd27a1b62105a155fdc5ce15d8eccc7e6fca9
3
+ metadata.gz: 5494f67d90d8ce8686f9036fd05c9f1549a2af21a3af16734d82415bcf98fcde
4
+ data.tar.gz: e41d02f51ea550051320c17a5d2c93917caa9b9e87ae8b9cf1e626f2ff4ff353
5
5
  SHA512:
6
- metadata.gz: 01a65170536dc4f5f33596f509fc7102670ba72a566954b1443d7d4dc29f01137206c4a3b3c49822c4bf85bc6b3200751655a49480a2afa46fe0e10ba3fa45e5
7
- data.tar.gz: d6d63d879a0eed194f410983abef9770b3296606184cf822a4824c56c2a0dd5a6350922595af45257c9ddf3bb3905be35de694ba9f230a9cc2d5d7a7b6676e7e
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.10.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,14 @@ 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.
131
+ - 2026-11-07: version 0.1.11. A damaged stored state can't raise into your page
132
+ or stop a visitor's history from saving. A shown entry of the wrong types
133
+ costs that item's click credit, and a history time too big for a number
134
+ discards the state. Same in every library.
127
135
  - 2026-11-05: version 0.1.10. `track` stays queued and in the background in a
128
136
  classic Sinatra app
129
137
  (`ruby app.rb`). The exit hook used to close the client before the server
data/lib/recapi.rb CHANGED
@@ -25,7 +25,7 @@ rescue LoadError
25
25
  end
26
26
 
27
27
  module Recapi
28
- VERSION = "0.1.10"
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
@@ -51,8 +53,10 @@ module Recapi
51
53
  Math.exp(-RATE * (dt_ms / 1000.0))
52
54
  end
53
55
 
56
+ # Compared, not converted: a stored 400-digit integer would become Infinity,
57
+ # which JSON can't save. NaN and Infinity fail the bound.
54
58
  def self.time?(value)
55
- (value.is_a?(Integer) || value.is_a?(Float)) && value.finite?
59
+ (value.is_a?(Integer) || value.is_a?(Float)) && value.abs <= Float::MAX
56
60
  end
57
61
 
58
62
  def self.weight?(value)
@@ -106,9 +110,11 @@ module Recapi
106
110
  prune(t)
107
111
  end
108
112
 
109
- # [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.
110
116
  def ranked(now)
111
- 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] }
112
118
  entries.sort_by { |item, w, _| [-w, item] }
113
119
  end
114
120
 
@@ -118,11 +124,12 @@ module Recapi
118
124
  end
119
125
 
120
126
  # The request's history and recent, and the rejected items. Weights are
121
- # decayed to now and rounded half up to 4 decimals like JS Math.round
122
- # (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.
123
130
  def wire(now)
124
131
  history = ranked(now).first(TOP_K).map do |item, weight, t|
125
- [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]
126
133
  end
127
134
  [history, @recent.last(RECENT_SIZE).map(&:first), rejected_order]
128
135
  end
@@ -266,7 +273,10 @@ module Recapi
266
273
  def prune(now)
267
274
  return unless @top.size > TOP_K
268
275
 
269
- @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]] }
270
280
  end
271
281
 
272
282
  def prune_rejected
@@ -739,13 +749,20 @@ module Recapi
739
749
 
740
750
  shown = {}
741
751
  data["shown"].each do |e|
742
- shown[e[0]] = e[1..] if e.is_a?(Array) && e.size.between?(3, 4) && History.id?(e[0])
752
+ shown[e[0]] = e[1..] if shown_entry?(e)
743
753
  end
744
754
  anon_id = data["anon_id"]
745
755
  new(history, shown, data["restored"] == true, History.id?(anon_id) ? anon_id : nil)
746
756
  rescue JSON::ParserError, TypeError
747
757
  nil
748
758
  end
759
+
760
+ # Bad types here would raise much later, in track on the shop's page, or
761
+ # refuse a whole batch of events at the API.
762
+ def self.shown_entry?(entry)
763
+ entry.is_a?(Array) && entry.size.between?(3, 4) && History.id?(entry[0]) && entry[1].is_a?(String) &&
764
+ entry[2..].all? { |t| t.is_a?(Integer) && t.abs < 2**63 }
765
+ end
749
766
  end
750
767
 
751
768
  def self.attributable?(entry, t)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - recapi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-05 00:00:00.000000000 Z
11
+ date: 2026-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Ruby client for the recapi recommendation API: per-user history, event
14
14
  batching, retries, popular fallback. Standard library only.'