standard_flags 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +229 -0
- data/README.md +837 -33
- data/lib/generators/standard_flags/admin/admin_generator.rb +115 -0
- data/lib/generators/standard_flags/admin/templates/controller.rb.tt +103 -0
- data/lib/generators/standard_flags/admin/templates/index.tsx.tt +64 -0
- data/lib/generators/standard_flags/admin/templates/show.tsx.tt +275 -0
- data/lib/generators/standard_flags/install/install_generator.rb +16 -1
- data/lib/generators/standard_flags/install/templates/flags.rb.tt +3 -2
- data/lib/generators/standard_flags/install/templates/initializer.rb.tt +10 -0
- data/lib/generators/standard_flags/migrations/migrations_generator.rb +57 -0
- data/lib/generators/standard_flags/migrations/templates/create_standard_flags_tables.rb.tt +78 -0
- data/lib/standard_flags/actor.rb +50 -0
- data/lib/standard_flags/audit_trail.rb +56 -0
- data/lib/standard_flags/config.rb +78 -0
- data/lib/standard_flags/context.rb +5 -1
- data/lib/standard_flags/definition.rb +85 -13
- data/lib/standard_flags/diagnostics.rb +35 -3
- data/lib/standard_flags/engine.rb +10 -2
- data/lib/standard_flags/env_pin.rb +15 -4
- data/lib/standard_flags/errors.rb +4 -0
- data/lib/standard_flags/evaluation.rb +5 -4
- data/lib/standard_flags/evaluator.rb +14 -1
- data/lib/standard_flags/events.rb +49 -0
- data/lib/standard_flags/health_check.rb +12 -2
- data/lib/standard_flags/health_contribution.rb +65 -0
- data/lib/standard_flags/mcp/cohorts.rb +65 -0
- data/lib/standard_flags/mcp/flags.rb +143 -0
- data/lib/standard_flags/mcp/members.rb +154 -0
- data/lib/standard_flags/mcp/writes.rb +98 -0
- data/lib/standard_flags/mcp.rb +238 -0
- data/lib/standard_flags/memo.rb +15 -1
- data/lib/standard_flags/notifiers/logger.rb +14 -0
- data/lib/standard_flags/open_feature/provider.rb +232 -0
- data/lib/standard_flags/open_feature.rb +26 -0
- data/lib/standard_flags/operations/audit.rb +218 -0
- data/lib/standard_flags/operations/base.rb +290 -0
- data/lib/standard_flags/operations/cohorts.rb +80 -0
- data/lib/standard_flags/operations/failure.rb +16 -0
- data/lib/standard_flags/operations/members.rb +164 -0
- data/lib/standard_flags/operations/restore.rb +330 -0
- data/lib/standard_flags/operations/restore_target.rb +197 -0
- data/lib/standard_flags/operations/rules.rb +233 -0
- data/lib/standard_flags/operations/validation.rb +154 -0
- data/lib/standard_flags/operations.rb +43 -0
- data/lib/standard_flags/parser.rb +16 -0
- data/lib/standard_flags/purge_job.rb +14 -0
- data/lib/standard_flags/registry.rb +38 -5
- data/lib/standard_flags/reporting.rb +51 -7
- data/lib/standard_flags/resolvers.rb +14 -7
- data/lib/standard_flags/result.rb +43 -0
- data/lib/standard_flags/rspec.rb +67 -13
- data/lib/standard_flags/runtime/active_record_store.rb +428 -0
- data/lib/standard_flags/runtime/cache.rb +98 -0
- data/lib/standard_flags/runtime/evaluation_tracker.rb +93 -0
- data/lib/standard_flags/runtime/memory_store.rb +408 -0
- data/lib/standard_flags/runtime/models.rb +43 -0
- data/lib/standard_flags/runtime/resolver.rb +256 -0
- data/lib/standard_flags/runtime/rule_snapshot.rb +60 -0
- data/lib/standard_flags/runtime/store.rb +161 -0
- data/lib/standard_flags/runtime/values.rb +181 -0
- data/lib/standard_flags/runtime.rb +266 -0
- data/lib/standard_flags/sensor.rb +12 -5
- data/lib/standard_flags/subscribers.rb +6 -0
- data/lib/standard_flags/testing.rb +100 -1
- data/lib/standard_flags/tooling/changes.rb +196 -0
- data/lib/standard_flags/tooling.rb +232 -19
- data/lib/standard_flags/version.rb +1 -1
- data/lib/standard_flags.rb +33 -5
- data/lib/tasks/standard_flags.rake +45 -1
- metadata +41 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
require "time"
|
|
2
|
+
|
|
3
|
+
module StandardFlags
|
|
4
|
+
# Operator tools for an agent or chat surface, one class per tool, with no
|
|
5
|
+
# dependency on any MCP framework. Each instance has `name`,
|
|
6
|
+
# `description`, `input_schema` (a JSON Schema Hash) and
|
|
7
|
+
# `call(arguments, actor:)`, which returns a JSON-safe Hash with String
|
|
8
|
+
# keys, or `{ "error" => { "code" =>, "message" => } }`:
|
|
9
|
+
#
|
|
10
|
+
# StandardFlags::MCP.tools.each do |tool|
|
|
11
|
+
# registry.register(name: tool.name, description: tool.description, schema: tool.input_schema) do |args, user|
|
|
12
|
+
# tool.call(args, actor: user)
|
|
13
|
+
# end
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Reads (ListFlags, GetFlag, ListMembers, ListCohorts, GetCohort) ask
|
|
17
|
+
# `config.authorize` for "flags:read"; writes go through
|
|
18
|
+
# StandardFlags::Operations, which authorise, refuse pins, validate and
|
|
19
|
+
# audit, and need `reason`. A failed operation's error code is the
|
|
20
|
+
# Result's (`pinned_by_env`, `stale_revision`, `unauthorized`, ...).
|
|
21
|
+
#
|
|
22
|
+
# Unit keys stay masked (`•••4567`) unless ListMembers is called with
|
|
23
|
+
# `reveal: true` by an actor allowed "flags:write" on the target; a reveal
|
|
24
|
+
# is audited, with counts only. GetFlag's `unit_key` (is this unit a
|
|
25
|
+
# member?) is a reveal too: flags:write, a reason, and a
|
|
26
|
+
# `standard_flags.flag.probed` row.
|
|
27
|
+
#
|
|
28
|
+
# One instance may serve every call, from any thread: the instance is
|
|
29
|
+
# frozen, and each call runs on its own copy, which holds that call's
|
|
30
|
+
# actor, arguments and store load. Operator reads never mark a rule as
|
|
31
|
+
# evaluated, so `flags:stale` still sees flags nothing uses.
|
|
32
|
+
module MCP
|
|
33
|
+
# A tool-level failure, returned as the error Hash.
|
|
34
|
+
class ToolError < StandardFlags::Error
|
|
35
|
+
attr_reader :code
|
|
36
|
+
|
|
37
|
+
def initialize(code, message)
|
|
38
|
+
@code = code
|
|
39
|
+
super(message)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
MASK = "•••".freeze
|
|
44
|
+
|
|
45
|
+
module_function
|
|
46
|
+
|
|
47
|
+
# One instance of every tool. `registry:` pins a registry (specs);
|
|
48
|
+
# by default each call reads StandardFlags.registry, so a code reload
|
|
49
|
+
# is picked up.
|
|
50
|
+
def tools(registry: nil)
|
|
51
|
+
TOOLS.map { |klass| klass.new(registry: registry) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The tool called `name`, or nil.
|
|
55
|
+
def tool(name, registry: nil)
|
|
56
|
+
klass = TOOLS.find { |candidate| candidate.tool_name == name.to_s }
|
|
57
|
+
klass&.new(registry: registry)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# A unit key as operators see it by default: the last four characters
|
|
61
|
+
# (fewer for short keys, so at least half stays hidden), after a mask.
|
|
62
|
+
def mask(unit_key)
|
|
63
|
+
text = unit_key.to_s
|
|
64
|
+
visible = [ 4, text.length / 2 ].min
|
|
65
|
+
visible.zero? ? MASK : "#{MASK}#{text[-visible..]}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Symbols become Strings, Times ISO 8601, Hash keys Strings.
|
|
69
|
+
def json(value)
|
|
70
|
+
case value
|
|
71
|
+
when Hash then value.to_h { |k, v| [ k.to_s, json(v) ] }
|
|
72
|
+
when Array then value.map { |v| json(v) }
|
|
73
|
+
when Symbol then value.to_s
|
|
74
|
+
when Time, DateTime then value.utc.iso8601
|
|
75
|
+
when Date then value.iso8601
|
|
76
|
+
when String, Integer, Float, true, false, nil then value
|
|
77
|
+
else value.to_s
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Shared by every tool: the class-level description, argument checks
|
|
82
|
+
# against the schema, actor resolution and the read policy.
|
|
83
|
+
class Tool
|
|
84
|
+
TYPES = {
|
|
85
|
+
"string" => [ String, Symbol ], "integer" => [ Integer ], "boolean" => [ true.class, false.class ],
|
|
86
|
+
"array" => [ Array ], "object" => [ Hash ], "null" => [ NilClass ]
|
|
87
|
+
}.freeze
|
|
88
|
+
|
|
89
|
+
class << self
|
|
90
|
+
def tool_name(name = nil)
|
|
91
|
+
@tool_name = name.to_s.freeze if name
|
|
92
|
+
@tool_name
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def description(text = nil)
|
|
96
|
+
@description = text.freeze if text
|
|
97
|
+
@description
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Properties, then which are required. additionalProperties is
|
|
101
|
+
# always false.
|
|
102
|
+
def schema(properties, required: [])
|
|
103
|
+
@input_schema = MCP.json({ type: "object", properties: properties, required: required,
|
|
104
|
+
additionalProperties: false }).freeze
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def input_schema
|
|
108
|
+
@input_schema
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def initialize(registry: nil)
|
|
113
|
+
@registry = registry
|
|
114
|
+
freeze
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def name = self.class.tool_name
|
|
118
|
+
def description = self.class.description
|
|
119
|
+
def input_schema = self.class.input_schema
|
|
120
|
+
|
|
121
|
+
# Runs on a copy (`dup` of a frozen object is not frozen), so nothing
|
|
122
|
+
# one call sets is seen by another.
|
|
123
|
+
def call(arguments, actor:)
|
|
124
|
+
dup.__send__(:invoke, arguments, actor)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
attr_reader :actor
|
|
130
|
+
|
|
131
|
+
def invoke(arguments, actor)
|
|
132
|
+
args = check_arguments!(arguments || {})
|
|
133
|
+
@actor = actor || StandardFlags.config.actor_resolver&.call
|
|
134
|
+
raise ToolError.new(:actor_missing, "no actor: pass actor:, or configure config.actor_resolver") if @actor.nil?
|
|
135
|
+
|
|
136
|
+
MCP.json(Runtime.tracker.untracked { perform(args) })
|
|
137
|
+
rescue ToolError => e
|
|
138
|
+
error(e.code, e.message)
|
|
139
|
+
rescue StandardError => e
|
|
140
|
+
Reporting.report(Error.new("#{name} failed: #{e.class}"), tool: name)
|
|
141
|
+
error(:error, "#{name} failed: #{e.class}")
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def registry
|
|
145
|
+
@registry || StandardFlags.registry
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def error(code, message)
|
|
149
|
+
{ "error" => { "code" => code.to_s, "message" => message.to_s } }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def check_arguments!(arguments)
|
|
153
|
+
raise ToolError.new(:invalid_arguments, "arguments must be an object") unless arguments.is_a?(Hash)
|
|
154
|
+
|
|
155
|
+
args = arguments.transform_keys(&:to_s)
|
|
156
|
+
properties = input_schema["properties"]
|
|
157
|
+
unknown = args.keys - properties.keys
|
|
158
|
+
raise ToolError.new(:invalid_arguments, "unknown argument(s): #{unknown.join(', ')}") unless unknown.empty?
|
|
159
|
+
|
|
160
|
+
missing = input_schema["required"] - args.keys
|
|
161
|
+
raise ToolError.new(:invalid_arguments, "missing argument(s): #{missing.join(', ')}") unless missing.empty?
|
|
162
|
+
|
|
163
|
+
args.each do |key, value|
|
|
164
|
+
types = Array(properties[key]["type"])
|
|
165
|
+
next if types.any? { |type| TYPES.fetch(type).any? { |klass| value.is_a?(klass) } }
|
|
166
|
+
|
|
167
|
+
raise ToolError.new(:invalid_arguments, "#{key} must be #{types.join(' or ')}")
|
|
168
|
+
end
|
|
169
|
+
args
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def allowed?(scope, key)
|
|
173
|
+
StandardFlags.config.authorize.call(actor, scope, key) ? true : false
|
|
174
|
+
rescue StandardError => e
|
|
175
|
+
Reporting.report(e, tool: name)
|
|
176
|
+
false
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def authorize!(scope, key)
|
|
180
|
+
return if allowed?(scope, key)
|
|
181
|
+
|
|
182
|
+
raise ToolError.new(:unauthorized, "#{Actor.label(actor)} may not #{scope} #{key.inspect}")
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def definition!(key)
|
|
186
|
+
key = key.to_s.to_sym
|
|
187
|
+
if registry.tombstoned?(key)
|
|
188
|
+
raise ToolError.new(:tombstoned_flag, "flag :#{key} was removed on #{registry.tombstones[key].removed.iso8601}")
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
registry[key] || raise(ToolError.new(:unknown_flag, "unknown flag :#{key}"))
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# The ENV pin deciding this flag, as operators see it: the variable's
|
|
195
|
+
# name, never its value.
|
|
196
|
+
def pin(definition)
|
|
197
|
+
pin = EnvPin.scan(definition, StandardFlags.config.env_source).pin
|
|
198
|
+
pin && { env_var: pin.var, label: "Pinned by environment (#{pin.var})" }
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The stored rules and cohorts, read fresh (not the process cache) once
|
|
202
|
+
# per call, or nil with runtime rules off.
|
|
203
|
+
def loaded
|
|
204
|
+
return @loaded if defined?(@loaded)
|
|
205
|
+
|
|
206
|
+
@loaded = Runtime.enabled? ? Runtime.store.load : nil
|
|
207
|
+
rescue StandardError => e
|
|
208
|
+
raise ToolError.new(:store_error, "could not read the store (#{(e.cause_class if e.is_a?(Runtime::StoreError)) || e.class})")
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def store_revision
|
|
212
|
+
Runtime.enabled? ? Runtime.store.revision : nil
|
|
213
|
+
rescue StandardError
|
|
214
|
+
nil
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def rule_for(key)
|
|
218
|
+
loaded&.rules&.find { |rule| rule.key == key.to_sym }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def rule_state(rule)
|
|
222
|
+
rule && rule.audit_state.merge(revision: rule.lock_version, last_evaluated_at: rule.last_evaluated_at)
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
require "standard_flags/mcp/flags"
|
|
229
|
+
require "standard_flags/mcp/writes"
|
|
230
|
+
require "standard_flags/mcp/members"
|
|
231
|
+
require "standard_flags/mcp/cohorts"
|
|
232
|
+
|
|
233
|
+
module StandardFlags
|
|
234
|
+
module MCP
|
|
235
|
+
TOOLS = [ ListFlags, GetFlag, StartFlag, PauseFlag, StopFlag, RampFlag, SetFlagValue,
|
|
236
|
+
ListMembers, AddMembers, RemoveMembers, ListCohorts, GetCohort ].freeze
|
|
237
|
+
end
|
|
238
|
+
end
|
data/lib/standard_flags/memo.rb
CHANGED
|
@@ -11,7 +11,9 @@ module StandardFlags
|
|
|
11
11
|
module Memo
|
|
12
12
|
KEY = :standard_flags_memo
|
|
13
13
|
|
|
14
|
-
# Registered with `app.executor.register_hook`.
|
|
14
|
+
# Registered with `app.executor.register_hook`. `complete` also runs
|
|
15
|
+
# the runtime rules' end-of-work step (Runtime.unit_of_work_complete!),
|
|
16
|
+
# which writes last_evaluated_at when it is due.
|
|
15
17
|
module Hook
|
|
16
18
|
def self.run
|
|
17
19
|
Memo.start!
|
|
@@ -20,6 +22,7 @@ module StandardFlags
|
|
|
20
22
|
|
|
21
23
|
def self.complete(_state)
|
|
22
24
|
Memo.stop!
|
|
25
|
+
Runtime.unit_of_work_complete!
|
|
23
26
|
end
|
|
24
27
|
end
|
|
25
28
|
|
|
@@ -47,6 +50,17 @@ module StandardFlags
|
|
|
47
50
|
memo.fetch(key) { memo[key] = yield }
|
|
48
51
|
end
|
|
49
52
|
|
|
53
|
+
def key?(key)
|
|
54
|
+
store&.key?(key) || false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Stores `value` under `key` when a unit of work is open; a no-op
|
|
58
|
+
# otherwise. Used to preload many entries with one query.
|
|
59
|
+
def write(key, value)
|
|
60
|
+
memo = store
|
|
61
|
+
memo[key] = value if memo
|
|
62
|
+
end
|
|
63
|
+
|
|
50
64
|
private
|
|
51
65
|
|
|
52
66
|
def store
|
|
@@ -33,9 +33,23 @@ module StandardFlags
|
|
|
33
33
|
[ :info, "sensor :#{payload[:key]} (#{payload[:mode]}) observed#{" #{payload[:error_class]}" if payload[:error_class]}" ]
|
|
34
34
|
when "evaluate"
|
|
35
35
|
[ :debug, "evaluated :#{payload[:key]} = #{payload[:value].inspect} (#{payload[:reason]})" ]
|
|
36
|
+
when "runtime_stale" then [ :warn, stale_message(payload) ]
|
|
37
|
+
when "runtime_refresh"
|
|
38
|
+
[ :debug, "loaded runtime rules at revision #{payload[:revision]} (#{payload[:rules]} rules)" ]
|
|
39
|
+
when "rule_invalid"
|
|
40
|
+
[ :warn, "runtime rule for :#{payload[:key]} #{payload[:error]}; serving its fail value" ]
|
|
41
|
+
when "report_registry_full"
|
|
42
|
+
[ :warn, "report registry full (#{payload[:limit]} entries); new warnings are no longer reported in this process" ]
|
|
43
|
+
when "targeting_key_missing"
|
|
44
|
+
[ :debug, "runtime rule for :#{payload[:key]} needs a :#{payload[:unit]} key and the context has none" ]
|
|
36
45
|
end
|
|
37
46
|
end
|
|
38
47
|
|
|
48
|
+
def stale_message(payload)
|
|
49
|
+
serving = payload[:loaded] ? "revision #{payload[:revision]} (#{payload[:age]}s old)" : "code defaults (never loaded)"
|
|
50
|
+
"runtime rules could not refresh (#{payload[:error_class]}); serving #{serving}"
|
|
51
|
+
end
|
|
52
|
+
|
|
39
53
|
def legacy_message(payload)
|
|
40
54
|
message = "legacy #{payload[:env_var]} for :#{payload[:key]} is deprecated; rename it to #{payload[:canonical]}"
|
|
41
55
|
payload[:shadowed] ? "#{message} (currently ignored: another pin wins)" : message
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
module StandardFlags
|
|
2
|
+
module OpenFeature
|
|
3
|
+
# An OpenFeature provider for the openfeature-sdk gem. Each fetch
|
|
4
|
+
# evaluates the flag through the same chain as `StandardFlags.evaluate`
|
|
5
|
+
# (test override, guard, ENV pin, runtime rule, code default) and returns
|
|
6
|
+
# the SDK's ResolutionDetails.
|
|
7
|
+
#
|
|
8
|
+
# * Booleans: `flag`, `kill_switch` and `compat` answer
|
|
9
|
+
# `fetch_boolean_value`; a mode (its value name) and an experiment (its
|
|
10
|
+
# variant) answer `fetch_string_value`. Any other pairing, and every
|
|
11
|
+
# number or object fetch, returns the caller's default with
|
|
12
|
+
# TYPE_MISMATCH.
|
|
13
|
+
# * Reasons and error codes map 1:1; ENV_PIN and KILL_SWITCH pass
|
|
14
|
+
# through as custom reasons. `kind`, `rule_id` and `source_env_var` go
|
|
15
|
+
# in `flag_metadata`.
|
|
16
|
+
# * An unknown key returns the caller's default with FLAG_NOT_FOUND, even
|
|
17
|
+
# when `config.strict` is on: a provider never raises.
|
|
18
|
+
#
|
|
19
|
+
# The EvaluationContext becomes a StandardFlags::Context:
|
|
20
|
+
#
|
|
21
|
+
# * `targeting_key` is the key of the flag's declared unit; a field named
|
|
22
|
+
# after a unit (`organization`, say) sets that unit's key, and wins over
|
|
23
|
+
# `targeting_key` for a flag on that unit. Only the flag's own unit key
|
|
24
|
+
# must be a scalar; another unit's field that isn't one is ignored.
|
|
25
|
+
# * `platform`, `appVersion` / `app_version` and `build` are the client.
|
|
26
|
+
# * `env` overrides the environment only with `allow_env_override: true`,
|
|
27
|
+
# and never relaxes a `forbidden_in:` guard (Resolvers::Guard also
|
|
28
|
+
# checks the registry's own environment).
|
|
29
|
+
#
|
|
30
|
+
# These keys are used as given: a unit's `from:` and `key:` are not
|
|
31
|
+
# applied to them, since they are already keys rather than records, and
|
|
32
|
+
# `config.segment_resolver` sees the same keys.
|
|
33
|
+
#
|
|
34
|
+
# There is no `init`: a provider without one is ready as soon as it is
|
|
35
|
+
# set (spec 2.8.5.1), and the registry and rules load on first use as
|
|
36
|
+
# they do outside OpenFeature. `shutdown` does nothing.
|
|
37
|
+
class Provider
|
|
38
|
+
NAME = "standard_flags".freeze
|
|
39
|
+
|
|
40
|
+
OF = ::OpenFeature::SDK::Provider
|
|
41
|
+
|
|
42
|
+
REASONS = {
|
|
43
|
+
Reason::STATIC => OF::Reason::STATIC,
|
|
44
|
+
Reason::DEFAULT => OF::Reason::DEFAULT,
|
|
45
|
+
Reason::TARGETING_MATCH => OF::Reason::TARGETING_MATCH,
|
|
46
|
+
Reason::SPLIT => OF::Reason::SPLIT,
|
|
47
|
+
Reason::DISABLED => OF::Reason::DISABLED,
|
|
48
|
+
Reason::ERROR => OF::Reason::ERROR,
|
|
49
|
+
Reason::ENV_PIN => Reason::ENV_PIN,
|
|
50
|
+
Reason::KILL_SWITCH => Reason::KILL_SWITCH
|
|
51
|
+
}.freeze
|
|
52
|
+
|
|
53
|
+
ERROR_CODES = {
|
|
54
|
+
ErrorCode::FLAG_NOT_FOUND => OF::ErrorCode::FLAG_NOT_FOUND,
|
|
55
|
+
ErrorCode::PARSE_ERROR => OF::ErrorCode::PARSE_ERROR,
|
|
56
|
+
ErrorCode::TYPE_MISMATCH => OF::ErrorCode::TYPE_MISMATCH,
|
|
57
|
+
ErrorCode::TARGETING_KEY_MISSING => OF::ErrorCode::TARGETING_KEY_MISSING,
|
|
58
|
+
ErrorCode::INVALID_CONTEXT => OF::ErrorCode::INVALID_CONTEXT,
|
|
59
|
+
ErrorCode::PROVIDER_NOT_READY => OF::ErrorCode::PROVIDER_NOT_READY,
|
|
60
|
+
ErrorCode::PROVIDER_FATAL => OF::ErrorCode::PROVIDER_FATAL,
|
|
61
|
+
ErrorCode::GENERAL => OF::ErrorCode::GENERAL
|
|
62
|
+
}.freeze
|
|
63
|
+
|
|
64
|
+
# Which kinds answer which fetch. Kinds missing from a list mismatch.
|
|
65
|
+
FETCHES = {
|
|
66
|
+
boolean: Definition::BOOLEAN_KINDS,
|
|
67
|
+
string: %i[mode experiment],
|
|
68
|
+
number: [],
|
|
69
|
+
integer: [],
|
|
70
|
+
float: [],
|
|
71
|
+
object: []
|
|
72
|
+
}.freeze
|
|
73
|
+
|
|
74
|
+
CLIENT_FIELDS = { platform: %w[platform], version: %w[appVersion app_version], build: %w[build] }.freeze
|
|
75
|
+
|
|
76
|
+
# A context field that can't be a unit key (a Hash, say).
|
|
77
|
+
class InvalidContext < StandardError; end
|
|
78
|
+
|
|
79
|
+
attr_reader :metadata
|
|
80
|
+
|
|
81
|
+
# `allow_env_override: true` lets the context's `env` field pick the
|
|
82
|
+
# environment for per-environment defaults. Off by default: `env` is a
|
|
83
|
+
# common attribute name in merged OpenFeature contexts.
|
|
84
|
+
def initialize(allow_env_override: false)
|
|
85
|
+
raise ArgumentError, "allow_env_override: must be true or false" unless [ true, false ].include?(allow_env_override)
|
|
86
|
+
|
|
87
|
+
@metadata = OF::ProviderMetadata.new(name: NAME).freeze
|
|
88
|
+
@allow_env_override = allow_env_override
|
|
89
|
+
@registries = nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def hooks
|
|
93
|
+
[]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def shutdown; end
|
|
97
|
+
|
|
98
|
+
FETCHES.each_key do |type|
|
|
99
|
+
define_method(:"fetch_#{type}_value") do |flag_key:, default_value:, evaluation_context: nil|
|
|
100
|
+
fetch(type, flag_key, default_value, evaluation_context)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
def fetch(type, flag_key, default_value, evaluation_context)
|
|
107
|
+
source = StandardFlags.registry
|
|
108
|
+
key = flag_key.to_s.to_sym
|
|
109
|
+
definition = source[key]
|
|
110
|
+
return not_found(source, key, default_value) unless definition
|
|
111
|
+
return mismatch(definition, type, default_value) unless FETCHES.fetch(type).include?(definition.kind)
|
|
112
|
+
|
|
113
|
+
registry = keyed_registry(source)
|
|
114
|
+
context = context_for(registry, definition, evaluation_context)
|
|
115
|
+
details(definition, Reader.new(registry, context).evaluate(key))
|
|
116
|
+
rescue InvalidContext => e
|
|
117
|
+
failure(default_value, ErrorCode::INVALID_CONTEXT, e.message, definition)
|
|
118
|
+
rescue StandardError => e
|
|
119
|
+
Reporting.once(:open_feature, key, e.class.name) { Reporting.report(e, flag: key.to_s) }
|
|
120
|
+
failure(default_value, ErrorCode::GENERAL, "#{e.class} while evaluating #{flag_key}", definition)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def details(definition, evaluation)
|
|
124
|
+
OF::ResolutionDetails.new(
|
|
125
|
+
value: evaluation.value.is_a?(Symbol) ? evaluation.value.to_s : evaluation.value,
|
|
126
|
+
variant: evaluation.variant,
|
|
127
|
+
reason: REASONS.fetch(evaluation.reason, evaluation.reason),
|
|
128
|
+
error_code: evaluation.error_code && ERROR_CODES.fetch(evaluation.error_code, OF::ErrorCode::GENERAL),
|
|
129
|
+
flag_metadata: metadata_for(definition, evaluation)
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def metadata_for(definition, evaluation = nil)
|
|
134
|
+
{
|
|
135
|
+
"kind" => definition&.kind&.to_s,
|
|
136
|
+
"rule_id" => evaluation&.rule_id,
|
|
137
|
+
"source_env_var" => evaluation&.source_env_var
|
|
138
|
+
}.compact
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def not_found(registry, key, default_value)
|
|
142
|
+
message =
|
|
143
|
+
if registry.tombstoned?(key)
|
|
144
|
+
"flag :#{key} was removed on #{registry.tombstones[key].removed.iso8601}"
|
|
145
|
+
else
|
|
146
|
+
"unknown flag :#{key}; declare it in config/flags.rb"
|
|
147
|
+
end
|
|
148
|
+
Reporting.once(:unknown, key) { Reporting.report(UnknownFlag.new(message), flag: key.to_s) }
|
|
149
|
+
failure(default_value, ErrorCode::FLAG_NOT_FOUND, message, nil)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def mismatch(definition, type, default_value)
|
|
153
|
+
message = "flag :#{definition.key} is a #{definition.kind}; fetch it as #{expected_type(definition)}, not #{type}"
|
|
154
|
+
Reporting.once(:type_mismatch, definition.key, type) { Reporting.report(TypeMismatch.new(message)) }
|
|
155
|
+
failure(default_value, ErrorCode::TYPE_MISMATCH, message, definition)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def expected_type(definition)
|
|
159
|
+
FETCHES.find { |_, kinds| kinds.include?(definition.kind) }.first
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def failure(default_value, error_code, message, definition)
|
|
163
|
+
OF::ResolutionDetails.new(
|
|
164
|
+
value: default_value,
|
|
165
|
+
reason: OF::Reason::ERROR,
|
|
166
|
+
error_code: ERROR_CODES.fetch(error_code),
|
|
167
|
+
error_message: message,
|
|
168
|
+
flag_metadata: metadata_for(definition)
|
|
169
|
+
)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# The app's registry with each unit reading its key straight from the
|
|
173
|
+
# context's actors, skipping `from:` and `key:`: OpenFeature callers
|
|
174
|
+
# pass keys, not records. Rebuilt only when the app's registry changes.
|
|
175
|
+
def keyed_registry(source)
|
|
176
|
+
cached = @registries
|
|
177
|
+
return cached.last if cached&.first.equal?(source)
|
|
178
|
+
|
|
179
|
+
units = source.units.to_h do |name, _|
|
|
180
|
+
[ name, Registry::Unit.new(name: name, resolver: ->(context) { context.actors[name] }, key: nil) ]
|
|
181
|
+
end
|
|
182
|
+
keyed = Registry.new(
|
|
183
|
+
flags: source.keys.zip(source.definitions).to_h,
|
|
184
|
+
tombstones: source.tombstones,
|
|
185
|
+
units: units,
|
|
186
|
+
env_resolver: -> { source.current_env }
|
|
187
|
+
)
|
|
188
|
+
@registries = [ source, keyed ].freeze
|
|
189
|
+
keyed
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Only the flag's own unit key (its field, else `targeting_key`) must be
|
|
193
|
+
# a scalar. Other units' fields only reach a segment resolver, so one
|
|
194
|
+
# that can't be a key (structured attributes from a merged context, say)
|
|
195
|
+
# is dropped rather than failing every flag.
|
|
196
|
+
def context_for(registry, definition, evaluation_context)
|
|
197
|
+
fields = evaluation_context&.fields || {}
|
|
198
|
+
actors = registry.units.each_key.with_object({}) do |unit, found|
|
|
199
|
+
key = unit_key(fields, unit.to_s, strict: unit == definition.unit)
|
|
200
|
+
found[unit] = key if key
|
|
201
|
+
end
|
|
202
|
+
if definition.unit && !actors.key?(definition.unit)
|
|
203
|
+
key = unit_key(fields, ::OpenFeature::SDK::EvaluationContext::TARGETING_KEY, strict: true)
|
|
204
|
+
actors[definition.unit] = key if key
|
|
205
|
+
end
|
|
206
|
+
env = present(fields["env"]) if @allow_env_override
|
|
207
|
+
Context.new(actors: actors, env: env, client: client(fields))
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def unit_key(fields, name, strict:)
|
|
211
|
+
value = fields[name]
|
|
212
|
+
case value
|
|
213
|
+
when nil then nil
|
|
214
|
+
when String, Symbol, Integer then present(value.to_s)
|
|
215
|
+
else
|
|
216
|
+
raise InvalidContext, "context field #{name} must be a string key, not a #{value.class}" if strict
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def client(fields)
|
|
221
|
+
CLIENT_FIELDS.each_with_object({}) do |(attribute, names), client|
|
|
222
|
+
value = names.lazy.map { |name| present(fields[name]) }.find(&:itself)
|
|
223
|
+
client[attribute] = value unless value.nil?
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def present(value)
|
|
228
|
+
value unless value.nil? || value.to_s.strip.empty?
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require "standard_flags"
|
|
2
|
+
|
|
3
|
+
begin
|
|
4
|
+
require "open_feature/sdk"
|
|
5
|
+
rescue LoadError => e
|
|
6
|
+
raise LoadError,
|
|
7
|
+
"standard_flags/open_feature needs the openfeature-sdk gem, which standard_flags does not declare; " \
|
|
8
|
+
"add `gem \"openfeature-sdk\"` to your Gemfile (#{e.message})"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require "standard_flags/open_feature/provider"
|
|
12
|
+
|
|
13
|
+
module StandardFlags
|
|
14
|
+
# An optional OpenFeature provider over the registry, for apps that read
|
|
15
|
+
# flags through the openfeature-sdk gem:
|
|
16
|
+
#
|
|
17
|
+
# require "standard_flags/open_feature"
|
|
18
|
+
#
|
|
19
|
+
# OpenFeature::SDK.configure do |config|
|
|
20
|
+
# config.set_provider(StandardFlags::OpenFeature::Provider.new)
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# Nothing in the gem requires this file; the app does, and brings the SDK.
|
|
24
|
+
module OpenFeature
|
|
25
|
+
end
|
|
26
|
+
end
|