star_gates 0.3.1 → 0.4.0

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.
@@ -0,0 +1,832 @@
1
+ # StarGates: feature gates over breaker_machines, chrono_machines and
2
+ # deadline_machines.
3
+ #
4
+ # Time is Integer nanoseconds throughout: monotonic for in-process instants
5
+ # and spans, wall-clock (since the Unix epoch) for event-sourced records.
6
+ # Sibling types are named in comments only, so these signatures validate on
7
+ # their own; the gem reads sibling values without depending on their RBS.
8
+ module StarGates
9
+ VERSION: String
10
+
11
+ type nanoseconds = Integer
12
+
13
+ # A gate's refusal: the feature is off for the context, the primary raised,
14
+ # the time budget ran out, or the circuit's own symbol carried whole.
15
+ type gate_refusal = :ineligible | :failed | :exceeded | :open | :busy | :full
16
+
17
+ type context = Hash[Symbol, untyped]
18
+
19
+ # BreakerMachines::Policy.
20
+ type breaker_policy = untyped
21
+
22
+ # ChronoMachines::Exponential, Constant or Fibonacci.
23
+ type backoff_strategy = untyped
24
+
25
+ # BreakerMachines::Circuit.
26
+ type breaker = untyped
27
+
28
+ def self.registry: () -> Registry
29
+ def self.flags: () -> Flags
30
+ # Evaluates the block in a DSL::DefineContext (instance_eval, which also
31
+ # yields the context); answers the block's value, or nil without a block.
32
+ def self.define: () ?{ (DSL::DefineContext) [self: DSL::DefineContext] -> untyped } -> untyped
33
+ # KeyError for a name with no gate.
34
+ def self.[]: (Symbol | String name) -> Gate
35
+ def self.activate: (Symbol | String name, ?context context) -> untyped
36
+ def self.status: (Symbol | String name) -> Hash[Symbol, untyped]
37
+ def self.gates: () -> Array[Symbol]
38
+ def self.test_mode!: () -> true
39
+ def self.test_mode?: () -> bool
40
+ def self.production_mode!: () -> false
41
+ def self.reset!: () -> untyped
42
+ def self.instrument: (String event, ?Hash[Symbol, untyped] payload) -> untyped
43
+ def self.monotonic_time: () -> nanoseconds
44
+ def self.wall_time: () -> nanoseconds
45
+ def self.feature_overview: () -> Hash[Symbol, untyped]
46
+ # `generated_at` is monotonic nanoseconds.
47
+ def self.rollout_statistics: () -> Hash[Symbol, untyped]
48
+ def self.state_distribution: () -> Hash[Symbol, untyped]
49
+ # One transition in either mode: `feature` the feature id (the gate name
50
+ # without event sourcing), `occurred_at` wall-clock nanoseconds.
51
+ type recent_transition = { feature: String, from: Symbol, to: Symbol, event: Symbol, occurred_at: nanoseconds }
52
+
53
+ # Newest first, one shape whether event sourcing is on or off;
54
+ # `generated_at` is monotonic nanoseconds. With event sourcing
55
+ # `total_found` counts the transitions the projection keeps, else the
56
+ # registered features' transitions.
57
+ def self.recent_transitions: (?limit: Integer) -> { transitions: Array[recent_transition], total_found: Integer,
58
+ limit: Integer, generated_at: nanoseconds }
59
+ private def self.projected_transitions: (Integer limit) -> [Array[recent_transition], Integer]
60
+ private def self.registered_transitions: (Integer limit) -> [Array[recent_transition], Integer]
61
+ private def self.transition_entry: (String | Symbol feature, Symbol from, Symbol to, Symbol event,
62
+ nanoseconds occurred_at) -> recent_transition
63
+ def self.configure_foundation: (?event_store: EventStore::EventStore?, ?adapter: Adapters::BaseAdapter?,
64
+ ?telemetry: untyped) -> Hexagonal::FeatureService
65
+ def self.feature_service: () -> Hexagonal::FeatureService
66
+ def self.feature_service_enabled?: () -> bool
67
+ def self.enable_event_sourcing!: () -> Hexagonal::FeatureService?
68
+ def self.telemetry: () -> untyped
69
+ def self.configure_telemetry: (untyped adapter) -> untyped
70
+ def self.configuration: () -> Configuration
71
+ def self.configure: () ?{ (Configuration) -> void } -> void
72
+ def self.load_config!: (?(String | _ToPath)? file_path) -> void
73
+ def self.default_config_file_path: () -> (String | _ToPath)
74
+
75
+ interface _ToPath
76
+ def to_path: () -> String
77
+ end
78
+
79
+ class Error < StandardError
80
+ end
81
+
82
+ class UnavailableError < Error
83
+ attr_reader gate: untyped
84
+ attr_reader refusal: gate_refusal
85
+ def initialize: (untyped gate, gate_refusal refusal, ?String? message) -> void
86
+ end
87
+
88
+ # A strategy and an attempt budget counting the first try (1..255 or nil),
89
+ # validated by ChronoMachines::Retry: TypeError or RangeError. Sealed.
90
+ class RetryPolicy < Data
91
+ attr_reader strategy: backoff_strategy
92
+ attr_reader max_attempts: Integer?
93
+ def self.new: (backoff_strategy strategy, Integer? max_attempts) -> RetryPolicy
94
+ | (strategy: backoff_strategy, max_attempts: Integer?) -> RetryPolicy
95
+ def self.[]: (backoff_strategy strategy, Integer? max_attempts) -> RetryPolicy
96
+ | (strategy: backoff_strategy, max_attempts: Integer?) -> RetryPolicy
97
+ def self.members: () -> [:strategy, :max_attempts]
98
+ def initialize: (strategy: backoff_strategy, max_attempts: Integer?) -> void
99
+ end
100
+
101
+ # The rollout rule Feature and Hexagonal::FeatureAggregate share.
102
+ module Rollout
103
+ # TypeError unless an Integer, RangeError outside 0..100.
104
+ def self.validated: (untyped rollout) -> Integer
105
+ end
106
+
107
+ # The keywords are runtime-only; the positionals are the cores'.
108
+ class Circuit
109
+ attr_reader breaker: breaker
110
+ attr_reader policy: breaker_policy
111
+ attr_reader retry_policy: RetryPolicy?
112
+ attr_reader feature: untyped
113
+ def initialize: (breaker_policy policy, ?RetryPolicy? retry_policy, ?feature: untyped, ?auto_rollback: bool) -> void
114
+ def call: [T] () { () -> T } -> T
115
+ def open?: () -> bool
116
+ def closed?: () -> bool
117
+ def half_open?: () -> bool
118
+ def last_failure_at: () -> nanoseconds?
119
+ def failure_count: () -> Integer
120
+ def success_count: () -> Integer
121
+ def to_h: () -> { state: Symbol, failure_count: Integer, success_count: Integer, last_failure_at: nanoseconds? }
122
+ def reset: () -> self
123
+
124
+ private
125
+
126
+ def rollback_hooks: () -> Hash[Symbol, ^() -> untyped]
127
+ # ChronoMachines' on_retry hook; `next_delay` nanoseconds.
128
+ def instrument_retry: (exception: StandardError, attempt: Integer, next_delay: nanoseconds) -> untyped
129
+ def feature_status: () -> String?
130
+ def handle_circuit_open: () -> untyped
131
+ def handle_circuit_half_open: () -> untyped
132
+ def handle_circuit_close: () -> untyped
133
+ end
134
+
135
+ class Gate
136
+ attr_reader name: untyped
137
+ attr_reader feature: untyped
138
+ attr_reader circuit: Circuit?
139
+ attr_reader run_block: Proc?
140
+ attr_reader fallback_block: Proc?
141
+ def initialize: (untyped name, ?feature: untyped, ?circuit: Circuit?, ?run_block: Proc?,
142
+ ?fallback_block: Proc?) -> void
143
+ # UnavailableError without a fallback, the primary's exception as cause.
144
+ def call: (?context context) -> untyped
145
+ def enabled_for?: (?context context) -> bool
146
+ def open?: () -> bool
147
+ def closed?: () -> bool
148
+ def status: () -> { name: untyped, feature_enabled: bool, feature_status: String?, circuit_open: bool,
149
+ last_failure_at: nanoseconds? }
150
+
151
+ private
152
+
153
+ # [nil, result] when the primary ran, [refusal, nil] when it did not.
154
+ def execute_primary: (context context) -> ([nil, untyped] | [:ineligible, nil])
155
+ def execute_feature: (context context) -> ([nil, untyped] | [:ineligible, nil])
156
+ def refusal_for: (StandardError error) -> gate_refusal
157
+ def with_circuit: [T] () { () -> T } -> T
158
+ def feature_enabled?: (context context) -> bool
159
+ def execute_run_block: (context context) -> untyped
160
+ def execute_fallback: (context context, gate_refusal refusal, StandardError? error) -> untyped
161
+ end
162
+
163
+ # `rollout:` an Integer 0..100, `group:` nil, a Symbol or a Proc:
164
+ # TypeError for another type, RangeError for a rollout outside 0..100.
165
+ class Feature
166
+ TRANSITIONS_KEPT: Integer
167
+
168
+ # `at` monotonic, `occurred_at` wall-clock nanoseconds.
169
+ type transition = { at: nanoseconds, occurred_at: nanoseconds, age: nanoseconds, from_state: Symbol?, to_state: Symbol?,
170
+ event: Symbol?, feature_name: String, feature_class: String? }
171
+
172
+ attr_reader rollout_percentage: Integer
173
+ attr_reader group_matcher: (Symbol | Proc)?
174
+ attr_reader created_at: nanoseconds
175
+ def initialize: (?rollout: Integer, ?group: (Symbol | Proc)?) -> void
176
+
177
+ # state_machines: the status machine and its events.
178
+ def status: () -> String
179
+ def status_name: () -> Symbol
180
+ def disabled?: () -> bool
181
+ def canary?: () -> bool
182
+ def enable: () -> bool
183
+ def disable: () -> bool
184
+ def rollout_canary: () -> bool
185
+ # State-dependent: nil when the feature does not run.
186
+ def execute: (?context context) ?{ () -> untyped } -> untyped
187
+ def ui_component: (?context context) -> String
188
+ def config_value: (untyped key, ?context context) -> untyped
189
+
190
+ def enabled_for?: (?context context) -> bool
191
+ def enabled?: () -> bool
192
+ def eligible_for_rollout?: (?context context) -> bool
193
+ def feature_config: () -> Hash[untyped, untyped]
194
+ def default_config: () -> Hash[untyped, untyped]
195
+ # `created_at` and `state_duration` are nanoseconds.
196
+ def stats: () -> Hash[Symbol, untyped]
197
+ def configuration: () -> Hash[Symbol, untyped]
198
+ def summary: () -> String
199
+ # `introspected_at` is monotonic nanoseconds.
200
+ def to_h: () -> Hash[Symbol, untyped]
201
+ def simulate: (key: String | Integer, ?groups: Array[Symbol], **untyped attributes) -> Hash[Symbol, untyped]
202
+ def eligibility_debug: (**untyped context) -> Hash[Symbol, untyped]
203
+ def test_rollout_scenarios: (Array[Integer] percentages, ?sample_keys: Array[String | Integer]) -> Hash[Integer, Hash[Symbol, untyped]]
204
+ # Newest first; `at`, `age` and `occurred_at` nanoseconds.
205
+ def transition_history: (?limit: Integer) -> { feature_name: String, total_transitions: Integer,
206
+ transitions: Array[transition], limit: Integer }
207
+ def feature_name: () -> String
208
+ def transition_count: () -> Integer
209
+
210
+ private
211
+
212
+ def group_matcher_info: () -> ({ type: String, value: Symbol | String })?
213
+ def state_duration: () -> nanoseconds
214
+ def state_machine_config: () -> Hash[Symbol, untyped]
215
+ def eligibility_reason: (context context) -> String
216
+ def validated_group: (untyped group) -> (Symbol | Proc)?
217
+ def group_matches?: (context context) -> bool
218
+ def rollout_key: (context context) -> (String | Integer)?
219
+ def hash_key: (String | Integer key) -> Integer
220
+ # A StateMachines::Transition.
221
+ def record_transition: (untyped transition) -> untyped
222
+ end
223
+
224
+ # `timeout` is Integer nanoseconds (or nil for none of its own); an
225
+ # execution past it raises UnavailableError with :exceeded.
226
+ class ApplicationStarGate < Feature
227
+ def self.feature_name: () -> String?
228
+ def self.feature_name=: (String? name) -> String?
229
+ def self.feature_name?: () -> bool
230
+ def self.options: () -> Hash[Symbol, untyped]
231
+ def self.options=: (Hash[Symbol, untyped] options) -> Hash[Symbol, untyped]
232
+ def self.options?: () -> bool
233
+ def self.telemetry_tags: () -> Hash[Symbol, untyped]
234
+ def self.telemetry_tags=: (Hash[Symbol, untyped] tags) -> Hash[Symbol, untyped]
235
+ def self.telemetry_tags?: () -> bool
236
+ def self.state_configs: () -> Hash[Symbol, untyped]
237
+ def self.state_configs=: (Hash[Symbol, untyped] configs) -> Hash[Symbol, untyped]
238
+ def self.state_configs?: () -> bool
239
+ def self.configure: (?String? name, **untyped options) -> untyped
240
+ def self.with_timeout: (nanoseconds timeout, **untyped options) -> untyped
241
+ def self.with_circuit_breaker: (?threshold: Integer, **untyped options) -> untyped
242
+ def self.for_team: (untyped team_name, **untyped options) -> Hash[Symbol, untyped]
243
+ def self.critical_feature: (**untyped options) -> untyped
244
+ def self.experimental_feature: (**untyped options) -> untyped
245
+
246
+ # ActiveSupport class_attribute storage. The setters return the value on
247
+ # the owner, but the redefinition's `private` result on a subclass.
248
+ private def self.__class_attr_feature_name: () -> String?
249
+ private def self.__class_attr_feature_name=: (String? name) -> void
250
+ private def self.__class_attr_options: () -> Hash[Symbol, untyped]
251
+ private def self.__class_attr_options=: (Hash[Symbol, untyped] options) -> void
252
+ private def self.__class_attr_telemetry_tags: () -> Hash[Symbol, untyped]
253
+ private def self.__class_attr_telemetry_tags=: (Hash[Symbol, untyped] tags) -> void
254
+ private def self.__class_attr_state_configs: () -> Hash[Symbol, untyped]
255
+ private def self.__class_attr_state_configs=: (Hash[Symbol, untyped] configs) -> void
256
+ private def self.extract_state_configs: (Hash[Symbol, untyped] options) -> { states: Hash[Symbol, untyped],
257
+ extracted_keys: Array[Symbol] }
258
+ private def self.extract_telemetry_tags: (Hash[Symbol, untyped] options) -> Hash[Symbol, untyped]
259
+
260
+ def initialize: (?rollout: Integer?, ?group: (Symbol | Proc)?, **untyped options) -> void
261
+ def feature_name: () -> String
262
+ def state_configs: () -> Hash[Symbol, untyped]
263
+ def state_configs=: (Hash[Symbol, untyped] configs) -> Hash[Symbol, untyped]
264
+ def state_configs?: () -> bool
265
+ def telemetry_tags=: (Hash[Symbol, untyped] tags) -> Hash[Symbol, untyped]
266
+ def telemetry_tags?: () -> bool
267
+ def timeout: () -> nanoseconds?
268
+ def retry_count: () -> Integer?
269
+ def circuit_breaker_threshold: () -> Integer?
270
+ def cache_ttl: () -> nanoseconds?
271
+ def performance_monitoring_enabled?: () -> bool?
272
+ def configuration_summary: () -> Hash[Symbol, untyped]
273
+ # Execution times and `last_execution` in nanoseconds, nil before any.
274
+ def performance_stats: () -> Hash[Symbol, untyped]
275
+
276
+ private
277
+
278
+ # Truthy when instrumentation is configured and a telemetry adapter set.
279
+ def telemetry_enabled?: () -> boolish
280
+ def error_reporting_enabled?: () -> boolish
281
+ def setup_counters: () -> nil
282
+ def instrument_execution: [T] (context context) { () -> T } -> T
283
+ def timed_execution: [T] (Hash[Symbol, untyped] payload, context context) { () -> T } -> T
284
+ def instrument_ui_access: (context context) { () -> String } -> String
285
+ def instrument_config_access: (untyped key, context context) { () -> untyped } -> untyped
286
+ # UnavailableError with :exceeded once `timeout` (or the ambient deadline)
287
+ # runs out. Under a budget the block runs inside Timeout.timeout, which
288
+ # yields the budget in seconds.
289
+ def with_timeout: [T] (nanoseconds? timeout) { (?Rational seconds) -> T } -> T
290
+ def timed_out: (nanoseconds budget) -> UnavailableError
291
+ # The new last-execution instant, nil without performance monitoring.
292
+ def record_successful_execution: (nanoseconds duration) -> nanoseconds?
293
+ def record_failed_execution: (nanoseconds duration) -> nanoseconds?
294
+ def record_execution: (nanoseconds duration) -> nanoseconds?
295
+ def average_execution_time: () -> nanoseconds?
296
+ def latency_quantile: (Float quantile) -> nanoseconds?
297
+ # ProbabilisticMachines::TDigest.
298
+ def latency: () -> untyped
299
+ def latency_total: () -> nanoseconds
300
+ # A percentage: 0 before any execution.
301
+ def calculate_error_rate: () -> (Float | Integer)
302
+ def telemetry_tags: () -> Hash[Symbol, untyped]
303
+ end
304
+
305
+ # Process-wide defaults; `default_timeout` and `cache_ttl` nanoseconds.
306
+ class Configuration
307
+ attr_accessor instrumentation: bool
308
+ attr_accessor telemetry_adapter: Symbol
309
+ attr_accessor default_timeout: nanoseconds
310
+ attr_accessor default_retry_count: Integer
311
+ attr_accessor circuit_breaker_threshold: Integer
312
+ attr_accessor cache_ttl: nanoseconds
313
+ attr_accessor environment: Symbol
314
+ # Keys are Symbols, whether set in Ruby or loaded from YAML.
315
+ attr_accessor feature_defaults: Hash[Symbol, Hash[Symbol, untyped]]
316
+ attr_accessor enable_performance_monitoring: bool
317
+ attr_accessor log_level: Symbol
318
+ attr_accessor error_reporting: bool
319
+ def initialize: () -> void
320
+ def development?: () -> bool
321
+ def test?: () -> bool
322
+ def production?: () -> bool
323
+ def staging?: () -> bool
324
+ def feature_config_for: (String | Symbol feature_name) -> Hash[Symbol, untyped]
325
+ # TypeError for a key that is neither a String nor a Symbol.
326
+ def configure_feature: (String | Symbol feature_name, ?Hash[Symbol | String, untyped] config)
327
+ -> Hash[Symbol, untyped]
328
+ def load_from_file: (String | _ToPath file_path) -> void
329
+ def apply_environment_defaults!: () -> untyped
330
+ def to_h: () -> Hash[Symbol, untyped]
331
+
332
+ private
333
+
334
+ def config_key: (String | Symbol key) -> Symbol
335
+ def detect_environment: () -> Symbol
336
+ def detect_default_telemetry_adapter: () -> Symbol
337
+ end
338
+
339
+ class Registry
340
+ def initialize: () -> void
341
+ def register: (Symbol | String name, Gate gate) -> Gate
342
+ def []: (Symbol | String name) -> Gate?
343
+ # KeyError for an unknown name unless a default or block is given.
344
+ def fetch: (Symbol | String name) -> Gate
345
+ | [D] (Symbol | String name, D default) -> (Gate | D)
346
+ | [D] (Symbol | String name) { (Symbol) -> D } -> (Gate | D)
347
+ def include?: (Symbol | String name) -> bool
348
+ def keys: () -> Array[Symbol]
349
+ def values: () -> Array[Gate]
350
+ def each: () { ([Symbol, Gate]) -> void } -> untyped
351
+ def clear: () -> untyped
352
+ def size: () -> Integer
353
+ def empty?: () -> bool
354
+ def to_h: () -> Hash[Symbol, Gate]
355
+ end
356
+
357
+ module DSL
358
+ class GateBuilder
359
+ attr_reader name: untyped
360
+ attr_reader feature_config: Hash[Symbol, untyped]
361
+ # [policy, retry_policy] once `circuit` is declared.
362
+ attr_reader circuit_config: [breaker_policy, RetryPolicy?]?
363
+ attr_reader run_block: Proc?
364
+ attr_reader fallback_block: Proc?
365
+ def initialize: (untyped name) -> void
366
+ def feature: (?rollout: Integer, ?group: (Symbol | Proc)?) -> Hash[Symbol, untyped]
367
+ def circuit: (breaker_policy policy, ?RetryPolicy? retry_policy) -> [breaker_policy, RetryPolicy?]
368
+ def run: () { (?context) -> untyped } -> Proc
369
+ def fallback: () { (?context) -> untyped } -> Proc
370
+ def build: () -> Gate
371
+
372
+ private
373
+
374
+ def build_feature: () -> Feature
375
+ def build_circuit: (Feature? feature) -> Circuit
376
+ end
377
+
378
+ class DefineContext
379
+ def initialize: (Registry registry) -> void
380
+ def gate: (untyped name) ?{ (GateBuilder) [self: GateBuilder] -> untyped } -> Gate
381
+ end
382
+ end
383
+
384
+ # Boolean flags: environment > configuration > runtime > false. A name is a
385
+ # String or Symbol of letters, digits, `_` or `-`: TypeError for another
386
+ # type, RangeError for another String.
387
+ class Flags
388
+ FIRST_SLOT: Integer
389
+ CLAIM_WINNER: Integer
390
+
391
+ type flag_name = String | Symbol
392
+ type configuration = Hash[untyped, untyped] | ^() -> Hash[untyped, untyped]
393
+
394
+ # ENV or a Hash of String to String.
395
+ interface _Env
396
+ def []: (String key) -> String?
397
+ def each_pair: () { (String, String) -> void } -> untyped
398
+ | () -> Enumerator[[String, String], untyped]
399
+ end
400
+
401
+ # Any store answering read/write/delete (Rails.cache). A store that also
402
+ # answers `increment` (and so `read(key, raw: true)`) gets an atomic name
403
+ # index; `read_multi` and `delete_multi` are used when present.
404
+ interface _Store
405
+ def read: (String key) -> untyped
406
+ def write: (String key, untyped value) -> untyped
407
+ def delete: (String key) -> untyped
408
+ end
409
+
410
+ attr_accessor configuration: configuration
411
+ attr_reader store: _Store?
412
+ def initialize: (?env: _Env, ?configuration: configuration, ?store: _Store?,
413
+ ?prefix: String) -> void
414
+ def store=: (_Store? store) -> _Store?
415
+ def enabled?: (flag_name name) -> bool
416
+ def set: (flag_name name, untyped value) -> bool
417
+ def enable: (flag_name name, ?metadata: Hash[untyped, untyped]) -> bool
418
+ def disable: (flag_name name) -> bool
419
+ def enable_if: (flag_name name) { () -> untyped } -> bool
420
+ def remove: (flag_name name) -> nil
421
+ def reset!: () -> nil
422
+ def all: () -> Hash[String, { enabled: bool, source: String }]
423
+ def enabled_flags: () -> Array[Symbol]
424
+ def summary: () -> Hash[Symbol, Array[Symbol]]
425
+ def debug: (flag_name name) -> Hash[Symbol, untyped]
426
+ def flag_status: (flag_name name) -> { name: Symbol, enabled: bool, source: String, metadata: Hash[untyped, untyped] }
427
+
428
+ private
429
+
430
+ # The folded name, or nil for anything that is not a valid name.
431
+ def normalize: (untyped name) -> String?
432
+ def flag_name: (untyped name) -> String
433
+ def boolean: (untyped value) -> bool
434
+ def update: (flag_name name, untyped value, ?metadata: Hash[untyped, untyped]?) -> bool
435
+ def runtime_value: (String? name) -> bool?
436
+ def runtime_values: () -> Hash[String, bool]
437
+ def runtime_metadata: (String? name) -> Hash[untyped, untyped]
438
+ def store_update: (String name, bool enabled, Hash[untyped, untyped]? metadata) -> bool
439
+ def keeping_indexed: (String name) { () -> void } -> nil
440
+ def store_remove: (String name) -> untyped
441
+ def store_reset: () -> untyped
442
+ def index_add: (String name) -> nil
443
+ def index_remove: (String name) -> Array[String]
444
+ def store_names: () -> Array[String]
445
+ def index_keys: () -> Array[String]
446
+ def slot_entries: () -> Hash[Integer, String]
447
+ def slot_range: () -> Range[Integer]
448
+ def claim_slot: (Hash[Integer, String] entries) -> Integer
449
+ def reclaimed_slot: (Hash[Integer, String] entries) -> Integer?
450
+ def appended_slot: () -> Integer
451
+ # Error when the store's increment answers no Integer (NullStore: nil).
452
+ def store_increment: (String key) -> Integer
453
+ def sequenced_index?: () -> bool
454
+ def legacy_index_add: (String name) -> nil
455
+ def legacy_index_remove: (String name) -> nil
456
+ def legacy_index: () -> Array[String]
457
+ def store_values: (Array[String] names) -> Hash[String, bool]
458
+ def store_read: (Array[String] keys) -> Hash[String, untyped]
459
+ # Error when the store does not persist the value.
460
+ def store_write: (String key, untyped value) -> nil
461
+ def store_delete: (Array[String] keys) -> untyped
462
+ def flag_key: (String name) -> String
463
+ def metadata_key: (String name) -> String
464
+ def index_key: () -> String
465
+ def index_sequence_key: () -> String
466
+ def index_slot_key: (Integer? slot) -> String
467
+ def slot_claim_key: (Integer? slot) -> String
468
+ def configuration_flags: () -> Hash[String, bool]
469
+ def environment_flags: () -> Hash[String, String]
470
+ def resolution: (String? name, Hash[String, bool] configured, bool? runtime_value)
471
+ -> { flag: Symbol?, enabled: bool, value: bool, source: String, environment_value: String?,
472
+ configuration_value: bool?, runtime_value: bool? }
473
+ end
474
+
475
+ module Adapters
476
+ # `expires_in:` is Integer nanoseconds.
477
+ class BaseAdapter
478
+ def initialize: (?Hash[Symbol, untyped] config) -> void
479
+ def store: (untyped key, untyped value) -> untyped
480
+ def fetch: (untyped key) -> untyped
481
+ def delete: (untyped key) -> untyped
482
+ def exists?: (untyped key) -> bool
483
+ def cache_fetch: (untyped key, ?expires_in: nanoseconds?) ?{ () -> untyped } -> untyped
484
+ def cache_write: (untyped key, untyped value, ?expires_in: nanoseconds?) -> untyped
485
+ def cache_delete: (untyped key) -> untyped
486
+ def publish: (untyped topic, untyped message) -> untyped
487
+ def subscribe: (untyped topic) { (untyped) -> void } -> untyped
488
+ def healthy?: () -> bool
489
+
490
+ attr_reader config: Hash[Symbol, untyped]
491
+ end
492
+
493
+ class MemoryAdapter < BaseAdapter
494
+ def clear!: () -> untyped
495
+ def size: () -> Integer
496
+ def topics_count: () -> Integer
497
+ def subscribers_count: (untyped topic) -> Integer
498
+ end
499
+
500
+ # `default_ttl` and `expires_in:` nanoseconds, rounded up to milliseconds.
501
+ class RedisAdapter < BaseAdapter
502
+ private
503
+
504
+ def write_expiring: (String key, untyped value, nanoseconds? expires_in) -> untyped
505
+ def prefixed_key: (untyped key) -> String
506
+ def topic_key: (untyped topic) -> String
507
+ def serialize: (untyped value) -> String
508
+ # The parsed JSON, or the String itself when it is not JSON.
509
+ def deserialize: (String value) -> untyped
510
+ # A Redis client.
511
+ def redis_from_config: () -> untyped
512
+ end
513
+ end
514
+
515
+ module Events
516
+ # `occurred_at` is wall-clock nanoseconds since the Unix epoch.
517
+ class BaseEvent
518
+ attr_reader event_id: String
519
+ attr_reader aggregate_id: untyped
520
+ attr_reader event_type: String
521
+ attr_reader data: Hash[Symbol, untyped]
522
+ attr_reader metadata: Hash[Symbol, untyped]
523
+ attr_reader occurred_at: nanoseconds
524
+ attr_reader version: Integer
525
+ def initialize: (aggregate_id: untyped, ?data: Hash[Symbol, untyped], ?metadata: Hash[Symbol, untyped],
526
+ ?version: Integer) -> void
527
+ def to_h: () -> Hash[Symbol, untyped]
528
+ def ==: (untyped other) -> bool
529
+ def hash: () -> Integer
530
+ end
531
+
532
+ class FeatureCreated < BaseEvent
533
+ end
534
+
535
+ class FeatureStateChanged < BaseEvent
536
+ def initialize: (aggregate_id: untyped, from_state: Symbol, to_state: Symbol, event_name: Symbol,
537
+ ?context: Hash[untyped, untyped], **untyped opts) -> void
538
+ def from_state: () -> Symbol
539
+ def to_state: () -> Symbol
540
+ def event_name: () -> Symbol
541
+ def context: () -> Hash[untyped, untyped]
542
+ end
543
+
544
+ class FeatureConfigurationChanged < BaseEvent
545
+ def initialize: (aggregate_id: untyped, configuration_changes: Hash[untyped, untyped], **untyped opts) -> void
546
+ def configuration_changes: () -> Hash[untyped, untyped]
547
+ end
548
+
549
+ # `execution_time` is nanoseconds.
550
+ class FeatureExecuted < BaseEvent
551
+ def initialize: (aggregate_id: untyped, context: untyped, result: untyped, execution_time: nanoseconds,
552
+ **untyped opts) -> void
553
+ def context: () -> untyped
554
+ def result: () -> untyped
555
+ def execution_time: () -> nanoseconds
556
+ end
557
+
558
+ class FeatureEvaluated < BaseEvent
559
+ def initialize: (aggregate_id: untyped, context: untyped, eligible: bool, reason: String, **untyped opts) -> void
560
+ def context: () -> untyped
561
+ def eligible: () -> bool
562
+ def reason: () -> String
563
+ end
564
+
565
+ class GateRegistered < BaseEvent
566
+ def initialize: (aggregate_id: untyped, gate_name: untyped, feature_class: untyped, **untyped opts) -> void
567
+ def gate_name: () -> untyped
568
+ def feature_class: () -> untyped
569
+ end
570
+
571
+ class SystemReset < BaseEvent
572
+ end
573
+ end
574
+
575
+ module EventStore
576
+ type append_refusal = :version_conflict
577
+
578
+ class EventStore
579
+ def initialize: (?adapter: MemoryAdapter?) -> void
580
+ # How many were stored, or :version_conflict with nothing stored.
581
+ def append: (untyped aggregate_id, Events::BaseEvent | Array[Events::BaseEvent] events,
582
+ ?expected_version: Integer?) -> (Integer | append_refusal)
583
+ def get_events: (untyped aggregate_id, ?from_version: Integer) -> Array[Events::BaseEvent]
584
+ # At or after `timestamp`, wall-clock nanoseconds; newest first.
585
+ def get_events_from: (nanoseconds timestamp, ?limit: Integer) -> Array[Events::BaseEvent]
586
+ def get_events_by_type: (String event_type, ?limit: Integer) -> Array[Events::BaseEvent]
587
+ def subscribe: (?String? event_type) { (Events::BaseEvent) -> void } -> untyped
588
+ def get_version: (untyped aggregate_id) -> Integer
589
+ def replay_all: () { (Events::BaseEvent) -> void } -> untyped
590
+
591
+ private
592
+
593
+ def notify_subscribers: (Events::BaseEvent event) -> untyped
594
+ end
595
+
596
+ class MemoryAdapter
597
+ def initialize: () -> void
598
+ # False, storing nothing, when `expected_version` is not current.
599
+ def append: (untyped aggregate_id, Array[Events::BaseEvent] events, ?Integer? expected_version) -> bool
600
+ def get_events: (untyped aggregate_id, ?from_version: Integer) -> Array[Events::BaseEvent]
601
+ def get_events_from: (nanoseconds timestamp, ?limit: Integer) -> Array[Events::BaseEvent]
602
+ def get_events_by_type: (String event_type, ?limit: Integer) -> Array[Events::BaseEvent]
603
+ def get_version: (untyped aggregate_id) -> Integer
604
+ def replay_all: () { (Events::BaseEvent) -> void } -> untyped
605
+ def clear!: () -> untyped
606
+ def size: () -> Integer
607
+ def aggregate_count: () -> Integer
608
+ end
609
+ end
610
+
611
+ module Hexagonal
612
+ # Commands answer their event, or a refusal symbol and change nothing.
613
+ class FeatureAggregate
614
+ type refusal = :already_created | :invalid_transition | :stale_state
615
+
616
+ TRANSITIONS: Hash[Symbol, Hash[Symbol, Symbol]]
617
+ TRANSITIONS_KEPT: Integer
618
+ RECORDS_KEPT: Integer
619
+
620
+ attr_reader id: untyped
621
+ attr_reader version: Integer
622
+ attr_reader uncommitted_events: Array[Events::BaseEvent]
623
+ def self.from_snapshot: (Hash[Symbol, untyped] snapshot, ?Array[Events::BaseEvent] events_since_snapshot) -> FeatureAggregate
624
+ def initialize: (untyped id, ?Array[Events::BaseEvent] events) -> void
625
+ def create: (?rollout: Integer, ?group: untyped, ?metadata: Hash[untyped, untyped]) -> (Events::FeatureCreated | :already_created)
626
+ # TypeError unless the states and the event name are Symbols.
627
+ def change_state: (Symbol from_state, Symbol to_state, Symbol event_name,
628
+ ?Hash[untyped, untyped] context) -> (Events::FeatureStateChanged | :invalid_transition | :stale_state)
629
+ def update_configuration: (Hash[untyped, untyped] changes) -> Events::FeatureConfigurationChanged
630
+ # `execution_time` nanoseconds.
631
+ def record_execution: (untyped context, untyped result, nanoseconds execution_time) -> Events::FeatureExecuted
632
+ def record_evaluation: (untyped context, bool eligible, String reason) -> Events::FeatureEvaluated
633
+ def created?: () -> bool
634
+ def current_state: () -> Symbol
635
+ def rollout_percentage: () -> Integer
636
+ def group_matcher: () -> untyped
637
+ def configuration: () -> Hash[untyped, untyped]
638
+ def transition_history: () -> Array[Hash[Symbol, untyped]]
639
+ def execution_history: () -> Array[Hash[Symbol, untyped]]
640
+ def evaluation_history: () -> Array[Hash[Symbol, untyped]]
641
+ def mark_events_as_committed: () -> Array[Events::BaseEvent]
642
+ def to_snapshot: () -> { id: untyped, version: Integer, state: Hash[Symbol, untyped] }
643
+
644
+ private
645
+
646
+ def restore: (Hash[Symbol, untyped] snapshot, Array[Events::BaseEvent] events) -> untyped
647
+ def apply_event: [E < Events::BaseEvent] (E event, ?from_history: bool) -> E
648
+ def apply_feature_created: (Events::FeatureCreated event) -> Hash[untyped, untyped]
649
+ def apply_state_changed: (Events::FeatureStateChanged event) -> Array[Hash[Symbol, untyped]]
650
+ def apply_configuration_changed: (Events::FeatureConfigurationChanged event) -> Hash[untyped, untyped]
651
+ def apply_execution_recorded: (Events::FeatureExecuted event) -> Array[Hash[Symbol, untyped]]
652
+ def apply_evaluation_recorded: (Events::FeatureEvaluated event) -> Array[Hash[Symbol, untyped]]
653
+ # A new list holding the last `kept` records.
654
+ def append: (Symbol key, Integer kept, Hash[Symbol, untyped] record) -> Array[Hash[Symbol, untyped]]
655
+ def symbol!: (Symbol name, untyped value) -> nil
656
+ end
657
+
658
+ class FeatureRepository
659
+ def initialize: (EventStore::EventStore event_store) -> void
660
+ def find: (untyped feature_id) -> FeatureAggregate?
661
+ # How many events were stored, or :version_conflict.
662
+ def save: (FeatureAggregate aggregate, ?expected_version: Integer?) -> (Integer | EventStore::append_refusal)
663
+ def find_or_create: (untyped feature_id) -> FeatureAggregate
664
+ def exists?: (untyped feature_id) -> bool
665
+ def healthy?: () -> bool
666
+ end
667
+
668
+ class FeatureService
669
+ type refusal = :not_found | :invalid_transition | :stale_state | :version_conflict
670
+
671
+ def initialize: (repository: FeatureRepository, projection: Projections::FeatureProjection,
672
+ event_store: EventStore::EventStore) -> void
673
+ def create_feature: (untyped feature_id, ?rollout: Integer, ?group: untyped, ?metadata: Hash[untyped, untyped]) -> FeatureAggregate
674
+ def change_feature_state: (untyped feature_id, Symbol from_state, Symbol to_state,
675
+ Symbol event_name, ?Hash[untyped, untyped] context) -> (FeatureAggregate | refusal)
676
+ def update_feature_configuration: (untyped feature_id, Hash[untyped, untyped] changes) -> (FeatureAggregate | refusal)
677
+ def record_feature_execution: (untyped feature_id, untyped context, untyped result,
678
+ nanoseconds execution_time) -> (FeatureAggregate | refusal)
679
+ def record_feature_evaluation: (untyped feature_id, untyped context, bool eligible, String reason) -> (FeatureAggregate | refusal)
680
+ def find_feature: (untyped feature_id) -> Hash[Symbol, untyped]?
681
+ def find_features_by_state: (Symbol state) -> Array[Hash[Symbol, untyped]]
682
+ def all_features: () -> Array[Hash[Symbol, untyped]]
683
+ def feature_stats: () -> Hash[Symbol, untyped]
684
+ def recent_transitions: (?limit: Integer) -> Array[Hash[Symbol, untyped]]
685
+ def transition_count: () -> Integer
686
+ def feature_exists?: (untyped feature_id) -> bool
687
+ def get_feature_aggregate: (untyped feature_id) -> FeatureAggregate?
688
+ def rebuild_projections!: () -> untyped
689
+ def healthy?: () -> bool
690
+
691
+ private
692
+
693
+ def command: (untyped feature_id) { (FeatureAggregate) -> (Events::BaseEvent | Symbol) } -> (FeatureAggregate | refusal)
694
+ def setup_event_subscriptions: () -> untyped
695
+ end
696
+ end
697
+
698
+ module Projections
699
+ # Instants are the events' wall-clock nanoseconds. Records read back with
700
+ # Symbol keys and Symbol state and event names, whatever the adapter.
701
+ class FeatureProjection
702
+ NAMED_FIELDS: Array[Symbol]
703
+ def initialize: (Adapters::BaseAdapter adapter) -> void
704
+ def handle_event: (Events::BaseEvent event) -> untyped
705
+ def find_feature: (untyped feature_id) -> Hash[Symbol, untyped]?
706
+ def find_features_by_state: (Symbol state) -> Array[Hash[Symbol, untyped]]
707
+ def all_features: () -> Array[Hash[Symbol, untyped]]
708
+ def feature_stats: () -> Hash[Symbol, untyped]
709
+ def recent_transitions: (?limit: Integer) -> Array[Hash[Symbol, untyped]]
710
+ # How many transitions are kept (the newest 100).
711
+ def transition_count: () -> Integer
712
+ def feature_exists?: (untyped feature_id) -> bool
713
+ def clear!: () -> untyped
714
+
715
+ private
716
+
717
+ def handle_feature_created: (Events::FeatureCreated event) -> untyped
718
+ def handle_state_changed: (Events::FeatureStateChanged event) -> untyped
719
+ def handle_configuration_changed: (Events::FeatureConfigurationChanged event) -> untyped
720
+ def handle_execution_recorded: (Events::FeatureExecuted event) -> untyped
721
+ def handle_evaluation_recorded: (Events::FeatureEvaluated event) -> untyped
722
+ def update_all_features_index: (untyped feature_id, :add | :remove operation) -> untyped
723
+ def update_state_index: (untyped feature_id, Symbol state, :add | :remove operation) -> untyped
724
+ def update_recent_transitions: (Events::FeatureStateChanged event) -> untyped
725
+ def features_by_state_count: () -> Hash[Symbol, Integer]
726
+ def recent_transitions_count: () -> Integer
727
+ def stored_transitions: () -> Array[Hash[Symbol, untyped]]
728
+ def normalized: (Hash[String | Symbol, untyped]? record) -> Hash[Symbol, untyped]?
729
+ def total_executions_count: () -> Integer
730
+ end
731
+ end
732
+
733
+ module Telemetry
734
+ # Durations handed in are nanoseconds; timestamps wall-clock nanoseconds.
735
+ class TelemetryAdapter
736
+ def initialize: (?Hash[Symbol, untyped] config) -> void
737
+ def record_metric: (String name, Numeric value, ?tags: Hash[Symbol, untyped], ?timestamp: nanoseconds) -> untyped
738
+ def record_counter: (String name, ?Numeric value, ?tags: Hash[Symbol, untyped]) -> untyped
739
+ def record_gauge: (String name, Numeric value, ?tags: Hash[Symbol, untyped]) -> untyped
740
+ def record_histogram: (String name, Numeric value, ?tags: Hash[Symbol, untyped]) -> untyped
741
+ def record_timing: (String name, nanoseconds duration, ?tags: Hash[Symbol, untyped]) -> untyped
742
+ def record_feature_evaluation: (untyped feature_id, bool eligible, untyped context, ?nanoseconds? execution_time) -> untyped
743
+ def record_feature_execution: (untyped feature_id, untyped state, bool success, nanoseconds execution_time,
744
+ ?untyped context) -> untyped
745
+ def record_state_transition: (untyped feature_id, untyped from_state, untyped to_state, untyped event_name) -> untyped
746
+ def record_circuit_breaker_event: (untyped feature_id, untyped event_type, untyped state) -> untyped
747
+ def record_system_health: (bool healthy, ?Hash[untyped, bool] component_healths) -> untyped
748
+ def record_event_store_metrics: (Integer event_count, Integer aggregate_count, ?nanoseconds? projection_lag) -> untyped
749
+ def time: [T] (String metric_name, ?tags: Hash[Symbol, untyped]) { () -> T } -> T
750
+ def healthy?: () -> bool
751
+ def enabled?: () -> bool
752
+ def disable!: () -> false
753
+ def enable!: () -> true
754
+
755
+ # Protected: a backend's sink.
756
+ def send_metric: (String name, Numeric value, Hash[Symbol, untyped] tags, nanoseconds timestamp) -> untyped
757
+
758
+ private
759
+
760
+ attr_reader config: Hash[Symbol, untyped]
761
+ end
762
+
763
+ # Converts durations to seconds, Prometheus's base unit.
764
+ class PrometheusAdapter < TelemetryAdapter
765
+ private
766
+
767
+ # The Prometheus module; ArgumentError without the gem.
768
+ def default_prometheus: () -> untyped
769
+ def setup_default_metrics: () -> untyped
770
+ def get_or_create_counter: (String name, Hash[Symbol, untyped] tags) -> untyped
771
+ def get_or_create_gauge: (String name, Hash[Symbol, untyped] tags) -> untyped
772
+ def get_or_create_histogram: (String name, Hash[Symbol, untyped] tags) -> untyped
773
+ def seconds: (nanoseconds nanoseconds) -> Float
774
+ end
775
+
776
+ # Records durations as nanoseconds, unit 'ns'.
777
+ class OpentelemetryAdapter < TelemetryAdapter
778
+ def trace_feature_evaluation: [T] (untyped feature_id, untyped context) { () -> T } -> T
779
+ def trace_feature_execution: [T] (untyped feature_id, untyped state) { () -> T } -> T
780
+ def trace_state_transition: [T] (untyped feature_id, untyped from_state, untyped to_state,
781
+ untyped event_name) { () -> T } -> T
782
+
783
+ private
784
+
785
+ # ArgumentError without the OpenTelemetry gem.
786
+ def default_tracer: () -> untyped
787
+ def default_meter: () -> untyped
788
+ def setup_instruments: () -> untyped
789
+ end
790
+ end
791
+
792
+ # Include in a Rails controller with jsonrpc-rails. A refused command
793
+ # answers invalid_params carrying the refusal symbol.
794
+ module JsonrpcRailsAdapter
795
+ # Truthy (a `defined?` description) when jsonrpc-rails and ActionController
796
+ # are loaded.
797
+ def self.available?: () -> String?
798
+ def self.included: (Module base) -> void
799
+ # The JSON-RPC action. Not `process`: that is Rails' dispatch entry point.
800
+ def handle_jsonrpc: () -> untyped
801
+
802
+ # JSON_RPC::JsonRpcError.
803
+ type json_rpc_error = untyped
804
+ type params = Hash[String, untyped]
805
+ type outcome = Hash[Symbol, untyped] | json_rpc_error
806
+
807
+ private
808
+
809
+ # A JSON_RPC::Response, or nil for a notification.
810
+ def handle_single_request: (untyped req) -> untyped
811
+ def dispatch_method: (String method, params params) -> outcome
812
+ def create_feature: (params params) -> outcome
813
+ def get_feature: (params params) -> outcome
814
+ def list_features: (params params) -> outcome
815
+ def change_state: (params params) -> outcome
816
+ def execute_feature: (params params) ?{ () -> untyped } -> outcome
817
+ def evaluate_feature: (params params) -> outcome
818
+ def feature_stats: (params params) -> outcome
819
+ def recent_transitions: (params params) -> outcome
820
+ def system_health: (params params) -> outcome
821
+ def rebuild_projections: (params params) -> outcome
822
+ def refused: (Symbol refusal) -> json_rpc_error
823
+ # ArgumentError naming the missing keys.
824
+ def validate_required_params: (params params, Array[String] required_keys) -> nil
825
+ def build_response: (untyped id, outcome outcome) -> untyped
826
+ def feature_service: () -> Hexagonal::FeatureService
827
+ end
828
+ end
829
+
830
+ class Numeric
831
+ def percent: () -> self
832
+ end