Module: Lucerna::Gates::Evaluate
- Defined in:
- lib/lucerna/gates/evaluate.rb
Overview
The Gates evaluation engine — pure functions over a compiled runtime, no I/O. Ported line-for-line from @lucerna/gates-core (sdks/core/src/evaluate.ts) onto the frozen bucketing contract in Hashing; the golden vectors in sdks/core/src/vectors.json pin parity.
The runtime document keeps its wire shape (string keys, camelCase); decisions come out symbol-keyed with reason strings identical to the wire codes.
Fail-safe rule: a gate carrying any rule element this engine version doesn't recognize (unknown condition operator, unknown override kind) is answered whole with "unsupported_rule" — flag off, experiment unassigned. An engine never half-evaluates a document it doesn't fully understand.
Constant Summary collapse
- OPERATORS =
%w[is is_not contains gt lt is_set].freeze
- AUDIENCE_OPERATORS =
Flag conditions may additionally reference a saved audience: the compiled condition carries the audience id in "value" ("trait" is the "audience" sentinel) and matches when the identity's traits satisfy the audience's rule set.
%w[in_audience not_in_audience].freeze
- OVERRIDE_BY =
%w[user_id domain].freeze
- OVERRIDE_SERVE =
%w[on off].freeze
- OVERRIDE_TRAIT_PREFIX =
Overrides can also pin by any trait the workspace enabled as an override dimension: "by" carries "trait:
" and matches when the identity trait equals "who" exactly (same value-based compare as the "is" operator; "domain" keeps its lowercase compare). "trait:"
Class Method Summary collapse
-
.all(runtime, user_id: nil, traits: nil) ⇒ Object
One identity's decisions over the entire runtime.
- .assigned(variant, iteration) ⇒ Object
-
.compare_values(a, b) ⇒ Object
Ordering for gt/lt: numeric when both sides parse as numbers, otherwise lexicographic by UTF-16 code units (ISO dates sort correctly).
-
.experiment(experiment, audiences, user_id: nil, traits: nil, trace: nil) ⇒ Object
Experiment assignment, sticky per (salt, user): eligibility gates first (running, environment, audience), then the holdout and traffic dice, then an allocation-weighted walk over the variants.
-
.flag(flag, user_id: nil, traits: nil, trace: nil, audiences: {}) ⇒ Object
Flag precedence, first hit wins: 1.
-
.identity_domain(traits) ⇒ Object
The domain an override's by: "domain" matches: an explicit
domaintrait, falling back to the part of theemailtrait after the last "@". -
.js_number(value) ⇒ Object
Mirrors JavaScript Number(string) for the realistic trait space: whitespace-trimmed, empty string is 0, otherwise Float parsing.
- .matches_audience?(traits, audience) ⇒ Boolean
- .matches_condition?(traits, condition) ⇒ Boolean
-
.no_variant(iteration, reason) ⇒ Object
-- internals ------------------------------------------------------.
- .note(trace, step, detail) ⇒ Object
- .supported_override_by?(by) ⇒ Boolean
-
.unsupported_flag_operator(conditions) ⇒ Object
Same check for flag conditions, which may also reference audiences.
-
.unsupported_operator(conditions) ⇒ Object
The first operator in the list this engine doesn't know, if any.
Class Method Details
.all(runtime, user_id: nil, traits: nil) ⇒ Object
One identity's decisions over the entire runtime.
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/lucerna/gates/evaluate.rb', line 252 def all(runtime, user_id: nil, traits: nil) { kills: runtime["kills"].dup, flags: runtime["flags"].to_h do |key, value| [key, flag(value, user_id: user_id, traits: traits, audiences: runtime["audiences"])] end, experiments: runtime["experiments"].to_h do |key, value| [key, experiment(value, runtime["audiences"], user_id: user_id, traits: traits)] end, } end |
.assigned(variant, iteration) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/lucerna/gates/evaluate.rb', line 270 def assigned(variant, iteration) { variant: { id: variant["id"], name: variant["name"], is_control: variant["isControl"] }, iteration: iteration, reason: "assigned", } end |
.compare_values(a, b) ⇒ Object
Ordering for gt/lt: numeric when both sides parse as numbers, otherwise lexicographic by UTF-16 code units (ISO dates sort correctly). Code-unit comparison — not locale collation — is part of the frozen contract.
282 283 284 285 286 287 288 |
# File 'lib/lucerna/gates/evaluate.rb', line 282 def compare_values(a, b) numeric_a = js_number(a) numeric_b = js_number(b) return numeric_a <=> numeric_b if numeric_a && numeric_b a.encode(Encoding::UTF_16BE).b <=> b.encode(Encoding::UTF_16BE).b end |
.experiment(experiment, audiences, user_id: nil, traits: nil, trace: nil) ⇒ Object
Experiment assignment, sticky per (salt, user): eligibility gates first (running, environment, audience), then the holdout and traffic dice, then an allocation-weighted walk over the variants. The salt is per iteration, so restarting an iteration reshuffles every bucket by design.
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 |
# File 'lib/lucerna/gates/evaluate.rb', line 169 def experiment(experiment, audiences, user_id: nil, traits: nil, trace: nil) iteration = experiment["iteration"] unless experiment["running"] note(trace, "status", "experiment is not running") return no_variant(iteration, "not_running") end note(trace, "status", "experiment is running") unless experiment["enabled"] note(trace, "environment", "experiment is off in this environment") return no_variant(iteration, "environment_off") end note(trace, "environment", "experiment is on in this environment") traits ||= {} audience_id = experiment["audienceId"] if audience_id.nil? note(trace, "audience", "targets everyone") else audience = audiences[audience_id] unless audience note(trace, "audience", %(audience "#{audience_id}" is not in the runtime)) return no_variant(iteration, "audience_not_found") end bad_operator = unsupported_operator(audience["conditions"]) if bad_operator note( trace, "unsupported", %(audience condition operator "#{bad_operator}" is not supported by this engine), ) return no_variant(iteration, "unsupported_rule") end in_audience = matches_audience?(traits, audience) note( trace, "audience", %(audience "#{audience_id}" (#{audience["match"]}): #{in_audience ? "matched" : "no match"}), ) return no_variant(iteration, "not_in_audience") unless in_audience end if user_id.nil? || user_id.empty? note(trace, "identity", "no userId — assignments have nothing to be sticky by") return no_variant(iteration, "missing_user_id") end holdout = experiment["holdout"] if holdout.positive? roll = Hashing.bucket("#{experiment["salt"]}:holdout", user_id) held = roll < holdout * 100 note(trace, "holdout", "bucket #{roll} vs holdout #{holdout * 100}: #{held ? "held out" : "in"}") return no_variant(iteration, "holdout") if held end traffic = experiment["traffic"] if traffic < 100 roll = Hashing.bucket("#{experiment["salt"]}:traffic", user_id) out = roll >= traffic * 100 note(trace, "traffic", "bucket #{roll} vs traffic #{traffic * 100}: #{out ? "out" : "in"}") return no_variant(iteration, "not_in_traffic") if out end # Allocations sum to 100 (enforced on write); the last variant # absorbs any rounding gap so the walk always lands. roll = Hashing.bucket("#{experiment["salt"]}:variant", user_id) cumulative = 0 experiment["variants"].each do |variant| cumulative += variant["allocation"] * 100 if roll < cumulative note(trace, "variant", %(bucket #{roll} lands in "#{variant["name"]}")) return assigned(variant, iteration) end end last = experiment["variants"].last unless last note(trace, "variant", "experiment has no variants") return no_variant(iteration, "not_running") end note(trace, "variant", %(bucket #{roll} absorbed by last variant "#{last["name"]}")) assigned(last, iteration) end |
.flag(flag, user_id: nil, traits: nil, trace: nil, audiences: {}) ⇒ Object
Flag precedence, first hit wins:
1. environment master switch off -> off
2. overrides, in stored order (by user id or email domain)
3. conditions — all must match (a flag's rule is a conjunction)
4. percentage rollout, sticky by user id via the stored salt
A rollout between 0 and 100 with no user id serves off — there is nothing to be sticky by.
audiences resolves the flag's audience conditions (in_audience /
not_in_audience); a referenced id that is absent evaluates the
condition to no-match — never to a wider serve.
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 |
# File 'lib/lucerna/gates/evaluate.rb', line 49 def flag(flag, user_id: nil, traits: nil, trace: nil, audiences: {}) unless flag["enabled"] note(trace, "enabled", "flag is off in this environment") return { on: false, reason: "flag_off" } end note(trace, "enabled", "flag is on in this environment") bad_override = flag["overrides"].find do |override| !supported_override_by?(override["by"]) || !OVERRIDE_SERVE.include?(override["serve"]) end bad_operator = unsupported_flag_operator(flag["conditions"]) if bad_override || bad_operator note( trace, "unsupported", if bad_operator %(condition operator "#{bad_operator}" is not supported by this engine) else %(override kind "#{bad_override["by"]}/#{bad_override["serve"]}" is not supported by this engine) end, ) return { on: false, reason: "unsupported_rule" } end traits ||= {} domain = identity_domain(traits)&.downcase flag["overrides"].each do |override| matched = case override["by"] when "user_id" !user_id.nil? && override["who"] == user_id when "domain" !domain.nil? && override["who"].downcase == domain else raw = traits[override["by"].delete_prefix(OVERRIDE_TRAIT_PREFIX)] !raw.nil? && raw == override["who"] end note( trace, "override", %(#{override["by"]} "#{override["who"]}" serves #{override["serve"]}: #{matched ? "matched" : "no match"}), ) return { on: override["serve"] == "on", reason: "override" } if matched end unless flag["conditions"].empty? all = true flag["conditions"].each do |condition| if AUDIENCE_OPERATORS.include?(condition["operator"]) audience = audiences[condition["value"]] if audience bad_audience_operator = unsupported_operator(audience["conditions"]) if bad_audience_operator note( trace, "unsupported", %(audience condition operator "#{bad_audience_operator}" is not supported by this engine), ) return { on: false, reason: "unsupported_rule" } end end # A dangling audience id never matches — for both operators the # condition fails closed rather than widening the serve. in_audience = !audience.nil? && matches_audience?(traits, audience) matched = !audience.nil? && (condition["operator"] == "in_audience" ? in_audience : !in_audience) note( trace, "condition", if audience %(audience "#{condition["value"]}" #{condition["operator"] == "in_audience" ? "includes" : "excludes"} user: #{matched ? "matched" : "no match"}) else %(audience "#{condition["value"]}" is not in the runtime: no match) end, ) else matched = matches_condition?(traits, condition) note( trace, "condition", %(#{condition["trait"]} #{condition["operator"]} "#{condition["value"]}": #{matched ? "matched" : "no match"}), ) end unless matched all = false break end end return { on: true, reason: "conditions" } if all end percentage = flag["rollout"]["percentage"] if percentage >= 100 note(trace, "rollout", "rollout at 100% — everyone is in") return { on: true, reason: "rollout" } end if percentage <= 0 note(trace, "rollout", "rollout at 0% — no one is in") return { on: false, reason: "rollout" } end if user_id.nil? || user_id.empty? note(trace, "rollout", "partial rollout with no userId — nothing to be sticky by") return { on: false, reason: "missing_user_id" } end roll = Hashing.bucket(flag["rollout"]["salt"], user_id) on = roll < percentage * 100 note( trace, "rollout", "bucket #{roll} #{on ? "<" : ">="} threshold #{percentage * 100} (basis points of 10000)", ) { on: on, reason: "rollout" } end |
.identity_domain(traits) ⇒ Object
The domain an override's by: "domain" matches: an explicit domain
trait, falling back to the part of the email trait after the
last "@".
344 345 346 347 348 349 350 351 352 353 |
# File 'lib/lucerna/gates/evaluate.rb', line 344 def identity_domain(traits) domain = traits["domain"] return domain if domain && !domain.empty? email = traits["email"] return nil if email.nil? || email.empty? at = email.rindex("@") at ? email[(at + 1)..] : nil end |
.js_number(value) ⇒ Object
Mirrors JavaScript Number(string) for the realistic trait space: whitespace-trimmed, empty string is 0, otherwise Float parsing.
292 293 294 295 296 297 298 299 |
# File 'lib/lucerna/gates/evaluate.rb', line 292 def js_number(value) stripped = value.strip return 0.0 if stripped.empty? Float(stripped) rescue ArgumentError nil end |
.matches_audience?(traits, audience) ⇒ Boolean
355 356 357 358 359 360 361 362 363 364 |
# File 'lib/lucerna/gates/evaluate.rb', line 355 def matches_audience?(traits, audience) conditions = audience["conditions"] return true if conditions.empty? if audience["match"] == "all" conditions.all? { |condition| matches_condition?(traits, condition) } else conditions.any? { |condition| matches_condition?(traits, condition) } end end |
.matches_condition?(traits, condition) ⇒ Boolean
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/lucerna/gates/evaluate.rb', line 301 def matches_condition?(traits, condition) raw = traits[condition["trait"]] value = condition["value"] case condition["operator"] when "is_set" !raw.nil? && raw != "" when "is" raw == value when "is_not" raw != value when "contains" (raw || "").downcase.include?(value.downcase) when "gt" !raw.nil? && compare_values(raw, value).positive? when "lt" !raw.nil? && compare_values(raw, value).negative? else false # unreachable — callers gate on unsupported_operator end end |
.no_variant(iteration, reason) ⇒ Object
-- internals ------------------------------------------------------
266 267 268 |
# File 'lib/lucerna/gates/evaluate.rb', line 266 def no_variant(iteration, reason) { variant: nil, iteration: iteration, reason: reason } end |
.note(trace, step, detail) ⇒ Object
366 367 368 |
# File 'lib/lucerna/gates/evaluate.rb', line 366 def note(trace, step, detail) trace << { step: step, detail: detail } if trace end |
.supported_override_by?(by) ⇒ Boolean
327 328 329 330 331 |
# File 'lib/lucerna/gates/evaluate.rb', line 327 def supported_override_by?(by) return true if OVERRIDE_BY.include?(by) by.start_with?(OVERRIDE_TRAIT_PREFIX) && by.length > OVERRIDE_TRAIT_PREFIX.length end |
.unsupported_flag_operator(conditions) ⇒ Object
Same check for flag conditions, which may also reference audiences.
334 335 336 337 338 339 |
# File 'lib/lucerna/gates/evaluate.rb', line 334 def unsupported_flag_operator(conditions) conditions.find do |condition| !OPERATORS.include?(condition["operator"]) && !AUDIENCE_OPERATORS.include?(condition["operator"]) end&.fetch("operator") end |
.unsupported_operator(conditions) ⇒ Object
The first operator in the list this engine doesn't know, if any.
323 324 325 |
# File 'lib/lucerna/gates/evaluate.rb', line 323 def unsupported_operator(conditions) conditions.find { |condition| !OPERATORS.include?(condition["operator"]) }&.fetch("operator") end |