standard_flags 0.3.0 → 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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +188 -0
  3. data/README.md +531 -59
  4. data/conformance/README.md +152 -3
  5. data/conformance/legacy_env.json +7 -1
  6. data/conformance/schema/fixture.schema.json +390 -21
  7. data/conformance/schema/manifest.schema.json +347 -0
  8. data/conformance/upgrade/client_status_server.json +297 -0
  9. data/conformance/wire/client_state.json +2115 -2
  10. data/conformance/wire/client_status.json +359 -0
  11. data/conformance/wire/manifest_check.json +5713 -0
  12. data/lib/generators/standard_flags/install/templates/initializer.rb.tt +4 -1
  13. data/lib/generators/standard_flags/migrations/migrations_generator.rb +37 -9
  14. data/lib/generators/standard_flags/migrations/templates/create_standard_flags_compat_stats.rb.tt +17 -0
  15. data/lib/standard_flags/client_app.rb +229 -0
  16. data/lib/standard_flags/client_status/controller.rb +59 -0
  17. data/lib/standard_flags/client_status.rb +110 -0
  18. data/lib/standard_flags/config.rb +18 -1
  19. data/lib/standard_flags/controller.rb +110 -5
  20. data/lib/standard_flags/definition.rb +37 -35
  21. data/lib/standard_flags/diagnostics.rb +61 -3
  22. data/lib/standard_flags/env_pin.rb +5 -15
  23. data/lib/standard_flags/errors.rb +31 -0
  24. data/lib/standard_flags/evaluator.rb +1 -0
  25. data/lib/standard_flags/mcp/flags.rb +12 -1
  26. data/lib/standard_flags/open_feature/provider.rb +2 -1
  27. data/lib/standard_flags/parser.rb +0 -16
  28. data/lib/standard_flags/registry.rb +46 -4
  29. data/lib/standard_flags/runtime/active_record_store.rb +64 -0
  30. data/lib/standard_flags/runtime/compat_tracker.rb +228 -0
  31. data/lib/standard_flags/runtime/memory_store.rb +20 -0
  32. data/lib/standard_flags/runtime/models.rb +6 -0
  33. data/lib/standard_flags/runtime/store.rb +18 -0
  34. data/lib/standard_flags/runtime.rb +33 -3
  35. data/lib/standard_flags/stores/app_store.rb +172 -0
  36. data/lib/standard_flags/stores.rb +37 -0
  37. data/lib/standard_flags/tooling/kotlin.rb +319 -0
  38. data/lib/standard_flags/tooling/manifest.rb +401 -0
  39. data/lib/standard_flags/tooling/manifest_check.rb +355 -0
  40. data/lib/standard_flags/tooling.rb +150 -55
  41. data/lib/standard_flags/version.rb +1 -1
  42. data/lib/standard_flags.rb +3 -0
  43. data/lib/tasks/standard_flags.rake +47 -0
  44. metadata +15 -2
  45. data/conformance/legacy_exact.json +0 -90
@@ -19,9 +19,12 @@
19
19
  # # Where flags:refs looks for flag reads.
20
20
  # # config.refs_paths = %w[app lib config]
21
21
  #
22
- # # Outputs of standard_flags:manifest and standard_flags:typescript.
22
+ # # Outputs of standard_flags:manifest, standard_flags:typescript and
23
+ # # standard_flags:kotlin.
23
24
  # # config.manifest_path = "config/flags.manifest.json"
24
25
  # # config.typescript_path = "app/javascript/types/standard_flags.d.ts"
26
+ # # config.kotlin_path = "config/StandardFlags.kt"
27
+ # # config.kotlin_package = "com.example.flags"
25
28
  #
26
29
  # # Runtime rules: per-flag rules stored in this app's database, read
27
30
  # # between the ENV pin and the code default. Needs the migration from
@@ -3,15 +3,37 @@ require "rails/generators/active_record"
3
3
 
4
4
  module StandardFlags
5
5
  module Generators
6
- # `rails g standard_flags:migrations` writes one migration creating the
7
- # runtime-rule tables (rules, cohorts, memberships and the single-row
8
- # state). Keys are UUIDs when the app generates UUID primary keys.
6
+ # `rails g standard_flags:migrations` writes the migrations for runtime
7
+ # rules: one creating the runtime-rule tables (rules, cohorts,
8
+ # memberships and the single-row state), and one creating the compat
9
+ # stats table (config.track_compat). Keys are UUIDs when the app
10
+ # generates UUID primary keys.
9
11
  #
10
- # Skipped when a migration with the same name already exists.
12
+ # Each is skipped when a migration with the same name already exists, or
13
+ # when its table already exists in the app's database (an app that
14
+ # squashed or deleted the original migration), so running it again
15
+ # after an upgrade adds only the new, additive one. `--no-check-tables`
16
+ # skips the database check.
11
17
  class MigrationsGenerator < Rails::Generators::Base
12
18
  include ActiveRecord::Generators::Migration
13
19
 
14
20
  MIGRATION_NAME = "create_standard_flags_tables".freeze
21
+ COMPAT_MIGRATION_NAME = "create_standard_flags_compat_stats".freeze
22
+ MIGRATION_NAMES = [ MIGRATION_NAME, COMPAT_MIGRATION_NAME ].freeze
23
+ # The table each migration creates (the first of several, for the
24
+ # runtime-rule tables).
25
+ TABLES = { MIGRATION_NAME => "standard_flags_rules", COMPAT_MIGRATION_NAME => "standard_flags_compat_stats" }.freeze
26
+
27
+ class_option :check_tables, type: :boolean, default: true,
28
+ desc: "Skip a migration whose table already exists in the database"
29
+
30
+ # Whether `table` exists in the app's database; false when there is no
31
+ # connection to ask.
32
+ def self.table_present?(table)
33
+ ActiveRecord::Base.with_connection { |connection| connection.data_source_exists?(table) }
34
+ rescue StandardError
35
+ false
36
+ end
15
37
 
16
38
  source_root File.expand_path("templates", __dir__)
17
39
 
@@ -21,12 +43,18 @@ module StandardFlags
21
43
  DESC
22
44
 
23
45
  def create_migration_file
24
- if self.class.migration_exists?(File.join(destination_root, db_migrate_path), MIGRATION_NAME)
25
- say_status("skip", "#{MIGRATION_NAME} already present, skipping", :yellow)
26
- return
27
- end
46
+ MIGRATION_NAMES.each do |name|
47
+ if self.class.migration_exists?(File.join(destination_root, db_migrate_path), name)
48
+ say_status("skip", "#{name} already present, skipping", :yellow)
49
+ next
50
+ end
51
+ if options[:check_tables] && self.class.table_present?(TABLES.fetch(name))
52
+ say_status("skip", "#{TABLES.fetch(name)} already exists in the database, skipping #{name}", :yellow)
53
+ next
54
+ end
28
55
 
29
- migration_template "#{MIGRATION_NAME}.rb.tt", File.join(db_migrate_path, "#{MIGRATION_NAME}.rb")
56
+ migration_template "#{name}.rb.tt", File.join(db_migrate_path, "#{name}.rb")
57
+ end
30
58
  end
31
59
 
32
60
  private
@@ -0,0 +1,17 @@
1
+ # Compat window counts for StandardFlags (config.track_compat): one row per
2
+ # compat flag and client platform, written behind by each process. Additive:
3
+ # apps that already ran create_standard_flags_tables get only this table.
4
+ class CreateStandardFlagsCompatStats < ActiveRecord::Migration<%= migration_version %>
5
+ def change
6
+ create_table :standard_flags_compat_stats<%= table_options %> do |t|
7
+ t.string :key, null: false
8
+ t.string :platform, null: false
9
+ t.bigint :hits, null: false, default: 0
10
+ t.string :oldest_version
11
+ t.datetime :last_hit_at
12
+ t.datetime :first_seen_at, null: false
13
+
14
+ t.index %i[key platform], unique: true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,229 @@
1
+ require "date"
2
+ require "uri"
3
+
4
+ module StandardFlags
5
+ # One app build channel the server knows about, declared in
6
+ # config/flags.rb:
7
+ #
8
+ # client :android_app, platform: :android, min_version: "0.4.0", since: "2026-12-01",
9
+ # recommended: "0.5.0", available: "0.5.0", store_url: "https://example.com/app", grace: 7.days
10
+ #
11
+ # * `min_version:` is the oldest build allowed to keep working. `since:`
12
+ # (required with it) is the date it was raised; it is enforced from
13
+ # `since + grace` (default 7 days, whole days, UTC dates).
14
+ # * `available:` is the newest version known to be in that store. The
15
+ # enforced minimum (and the recommended version) is never above it, so a
16
+ # minimum raised before a release has passed review locks nobody out.
17
+ # `store_lookup:` can supply it live for this client (the App Store; see
18
+ # StandardFlags::Stores), falling back to `available:`.
19
+ # * `recommended:` nudges builds below it without blocking them.
20
+ # * `group:` lets two apps share a platform: clients are unique per
21
+ # (group, platform), and the status endpoint and the 426 guard pick a
22
+ # group (nil, the default group, unless told otherwise).
23
+ #
24
+ # Raising `min_version:` means moving `since:` to the day of the raise;
25
+ # otherwise the old date makes the new minimum enforced at once.
26
+ #
27
+ # ENV break-glass pins beat the code and any store lookup, as a flag's
28
+ # pin does: STANDARD_FLAGS_CLIENT_<NAME>_MIN_VERSION, _SINCE,
29
+ # _RECOMMENDED and _AVAILABLE. A pin that doesn't parse is ignored (and
30
+ # reported, by name only). So is a pinned minimum that raises the code's
31
+ # one (or sets one where the code has none) without a pinned `_SINCE`: a
32
+ # break-glass raise can't skip the grace window. Lowering the minimum
33
+ # needs no `_SINCE`; a pin lowers it only when every version that meets
34
+ # the code's minimum also meets the pin (so `1.0.0-rc.1` against `1.0.0`
35
+ # is a raise: it stops `1.0.0-beta.1`, which `1.0.0` lets through).
36
+ ClientApp = Data.define(:name, :platform, :group, :min_version, :since, :grace_days, :recommended, :available,
37
+ :store_url, :store_lookup)
38
+
39
+ class ClientApp
40
+ # Pin suffix => attribute.
41
+ PINS = { "MIN_VERSION" => :min_version, "SINCE" => :since, "RECOMMENDED" => :recommended,
42
+ "AVAILABLE" => :available }.freeze
43
+ DEFAULT_GRACE_DAYS = 7
44
+ DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}\z/
45
+
46
+ # A ClientApp with its ENV pins applied: what the status endpoint and the
47
+ # 426 guard work from. `pinned` and `rejected` name the ENV variables
48
+ # that applied and that were ignored.
49
+ Policy = Data.define(:app, :min_version, :since, :recommended, :available, :pinned, :rejected) do
50
+ # The first day the minimum is enforced: since + grace.
51
+ def enforce_on
52
+ since && since + app.grace_days
53
+ end
54
+
55
+ # The newest version known to be in the store: a pinned `_AVAILABLE`,
56
+ # else the client's store lookup's last answer, else `available:`.
57
+ # Never waits on a store.
58
+ def ceiling
59
+ return available if available_pinned?
60
+
61
+ Stores.available(app) || available
62
+ end
63
+
64
+ def available_pinned?
65
+ pinned.include?(app.env_var(:available))
66
+ end
67
+ end
68
+
69
+ class << self
70
+ # Builds a ClientApp from a `client` declaration, raising
71
+ # DefinitionError for anything that can't work.
72
+ def declare(name, platform:, min_version: nil, since: nil, recommended: nil, available: nil, store_url: nil,
73
+ grace: nil, group: nil, store_lookup: nil)
74
+ name = name.to_s
75
+ fail = ->(message) { raise DefinitionError, "client :#{name} #{message}" }
76
+ fail.call("name must match #{Definition::KEY_FORMAT.inspect}") unless name.match?(Definition::KEY_FORMAT)
77
+ group = group&.to_s
78
+ fail.call("group: must match #{Definition::KEY_FORMAT.inspect}") if group && !group.match?(Definition::KEY_FORMAT)
79
+
80
+ versions = { min_version: min_version, recommended: recommended, available: available }
81
+ .to_h { |attribute, value| [ attribute, declared_version(attribute, value, fail) ] }
82
+ since_date = since.nil? ? nil : date(since)
83
+ fail.call("since: #{since.inspect} is not a YYYY-MM-DD date") if !since.nil? && since_date.nil?
84
+ fail.call("min_version: needs since: (the date the minimum was raised)") if versions[:min_version] && since_date.nil?
85
+ fail.call("since: needs min_version:") if since_date && versions[:min_version].nil?
86
+ if versions[:recommended] && versions[:min_version] && versions[:recommended] < versions[:min_version]
87
+ fail.call("recommended: #{versions[:recommended]} is below min_version: #{versions[:min_version]}")
88
+ end
89
+
90
+ unless store_lookup.nil? || store_lookup.respond_to?(:call)
91
+ fail.call("store_lookup: must respond to call(client_app) (StandardFlags::Stores::AppStore, say)")
92
+ end
93
+
94
+ new(name: name.to_sym, platform: declared_platform(platform, fail), group: group&.to_sym, **versions,
95
+ since: since_date, grace_days: grace_days(grace, fail), store_url: declared_url(store_url, fail),
96
+ store_lookup: store_lookup)
97
+ end
98
+
99
+ # A YYYY-MM-DD String, a Date, or a Time or DateTime (as its UTC date)
100
+ # as a Date, or nil.
101
+ def date(value)
102
+ return value.to_time.utc.to_date if value.is_a?(DateTime) || value.is_a?(Time)
103
+ return value if value.is_a?(Date)
104
+
105
+ text = Version.clean(value)
106
+ text&.match?(DATE_FORMAT) ? Date.iso8601(text) : nil
107
+ rescue Date::Error
108
+ nil
109
+ end
110
+
111
+ private
112
+
113
+ def declared_platform(platform, fail)
114
+ platforms = StandardFlags.config.client_platforms.map { |known| known.to_s.downcase } - [ Client::UNKNOWN_PLATFORM ]
115
+ name = platform.to_s.downcase
116
+ fail.call("platform: #{platform.inspect} is not one of config.client_platforms #{platforms.inspect}") unless platforms.include?(name)
117
+ name.freeze
118
+ end
119
+
120
+ def declared_version(attribute, value, fail)
121
+ return nil if value.nil?
122
+
123
+ parsed = value.is_a?(String) ? Version.parse(value) : nil
124
+ fail.call("#{attribute}: #{value.inspect} is not a version (\"0.4.0\")") if parsed.nil?
125
+ parsed
126
+ end
127
+
128
+ # `grace:` as whole days: an Integer number of days or a Duration of
129
+ # whole days (7.days).
130
+ def grace_days(grace, fail)
131
+ return DEFAULT_GRACE_DAYS if grace.nil?
132
+
133
+ # A Duration answers is_a?(Integer) for its value, so it goes first.
134
+ seconds =
135
+ if grace.is_a?(ActiveSupport::Duration) then grace.to_i
136
+ elsif grace.is_a?(Integer) then grace * 86_400
137
+ end
138
+ unless seconds.is_a?(Integer) && seconds >= 0 && (seconds % 86_400).zero?
139
+ fail.call("grace: must be a whole number of days (7 or 7.days), not #{grace.inspect}")
140
+ end
141
+ seconds / 86_400
142
+ end
143
+
144
+ def declared_url(url, fail)
145
+ return nil if url.nil?
146
+
147
+ uri = url.is_a?(String) ? URI.parse(url) : nil
148
+ fail.call("store_url: must be an http(s) URL") unless uri && %w[http https].include?(uri.scheme) && uri.host
149
+ url.dup.freeze
150
+ rescue URI::InvalidURIError
151
+ fail.call("store_url: must be an http(s) URL")
152
+ end
153
+ end
154
+
155
+ # The ENV pin for `attribute` (:min_version, :since, :recommended or
156
+ # :available).
157
+ def env_var(attribute)
158
+ suffix = PINS.key(attribute.to_sym) or raise ArgumentError, "no pin for #{attribute.inspect}"
159
+ "STANDARD_FLAGS_CLIENT_#{name.to_s.upcase}_#{suffix}"
160
+ end
161
+
162
+ def env_vars
163
+ PINS.values.map { |attribute| env_var(attribute) }
164
+ end
165
+
166
+ # This client with its ENV pins applied (a Policy). Reads `env`
167
+ # (anything that answers `[]`); never raises.
168
+ def resolve(env = StandardFlags.config.env_source)
169
+ values = { min_version: min_version, since: since, recommended: recommended, available: available }
170
+ pinned = []
171
+ rejected = []
172
+ PINS.each_value do |attribute|
173
+ var = env_var(attribute)
174
+ raw = read(env, var)
175
+ next if raw.nil? || raw.strip.empty?
176
+
177
+ text = Version.clean(raw)
178
+ value = text && (attribute == :since ? ClientApp.date(text) : Version.parse(text))
179
+ if value.nil?
180
+ rejected << var
181
+ else
182
+ pinned << var
183
+ values[attribute] = value
184
+ end
185
+ end
186
+ refuse_unscheduled_minimum(values, pinned, rejected)
187
+ Policy.new(app: self, **values, pinned: pinned.freeze, rejected: rejected.freeze)
188
+ end
189
+
190
+ def store_lookup?
191
+ !store_lookup.nil?
192
+ end
193
+
194
+ def inspect
195
+ "#<StandardFlags::ClientApp :#{name} #{platform}#{" (#{group})" if group}>"
196
+ end
197
+
198
+ private
199
+
200
+ # A pinned minimum that raises the code's (or sets one where it has
201
+ # none) needs a pinned since, so a break-glass raise gets its own grace
202
+ # window instead of reusing the code's old date.
203
+ def refuse_unscheduled_minimum(values, pinned, rejected)
204
+ var = env_var(:min_version)
205
+ return unless pinned.include?(var) && !pinned.include?(env_var(:since))
206
+ return if min_version && relaxes?(values[:min_version], min_version)
207
+
208
+ pinned.delete(var)
209
+ rejected << var
210
+ values[:min_version] = min_version
211
+ end
212
+
213
+ # Whether every version that meets `code` (by ClientStatus.meets?) also
214
+ # meets `pin`. Meeting is monotonic, so it is enough to check the
215
+ # lowest version that meets `code`: `code` itself when it is a
216
+ # prerelease, else the lowest prerelease of its release (`1.0.0-0`).
217
+ def relaxes?(pin, code)
218
+ lowest = code.prerelease? ? code : Version.parse("#{code.major}.#{code.minor}.#{code.patch}-0")
219
+ ClientStatus.meets?(lowest, pin)
220
+ end
221
+
222
+ def read(env, var)
223
+ value = env[var]
224
+ value&.to_s
225
+ rescue StandardError
226
+ nil
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,59 @@
1
+ require "active_support/concern"
2
+ require "active_support/core_ext/class/attribute"
3
+
4
+ module StandardFlags
5
+ module ClientStatus
6
+ # `GET /client/status` as a controller mixin. It runs before
7
+ # authentication and says nothing about the caller. It reads the client
8
+ # exactly as `requires_client_version` and the flag guards do
9
+ # (`standard_flags_upgrade_client`: the client `standard_flags_context`
10
+ # names, whole, else the X-Client-* headers' client), so a build the
11
+ # guard refuses is always told why here. Keep that context safe to call
12
+ # before sign-in (nil, the default, is).
13
+ #
14
+ # # config/routes.rb
15
+ # get "client/status", to: "client_status#client_status"
16
+ #
17
+ # class ClientStatusController < ActionController::API # or ActionController::Base
18
+ # include StandardFlags::ClientStatus::Controller
19
+ # standard_flags_client_status # group: :driver for a second app
20
+ # end
21
+ #
22
+ # It answers 200 with StandardFlags::ClientStatus's payload, whatever
23
+ # the status (`upgrade_required` included: the app decides how to show
24
+ # it), with `Cache-Control: max-age=60, private` (the age is `max_age:`)
25
+ # and a Vary on the client headers. It is a GET, so forgery protection
26
+ # never applies. With `config.read_client_headers` off it can't see the
27
+ # client and answers `unknown`.
28
+ module Controller
29
+ extend ActiveSupport::Concern
30
+ include StandardFlags::Controller
31
+
32
+ DEFAULT_MAX_AGE = 60
33
+
34
+ included do
35
+ class_attribute :standard_flags_client_status_options, instance_writer: false,
36
+ default: { group: nil, max_age: DEFAULT_MAX_AGE }.freeze
37
+ end
38
+
39
+ class_methods do
40
+ def standard_flags_client_status(group: nil, max_age: DEFAULT_MAX_AGE)
41
+ raise ArgumentError, "max_age: must be a non-negative Integer of seconds" unless max_age.is_a?(Integer) && max_age >= 0
42
+
43
+ self.standard_flags_client_status_options = { group: group&.to_sym, max_age: max_age }.freeze
44
+ end
45
+ end
46
+
47
+ # GET /client/status
48
+ def client_status
49
+ options = standard_flags_client_status_options
50
+ result = ClientStatus.for(standard_flags_upgrade_client, group: options[:group])
51
+ response.cache_control.merge!(max_age: options[:max_age], public: false)
52
+ vary = response.headers["Vary"].to_s.split(",").map(&:strip).reject(&:empty?)
53
+ vary += StandardFlags.config.client_headers.values.compact.map(&:to_s)
54
+ response.headers["Vary"] = vary.uniq(&:downcase).join(", ") unless vary.empty?
55
+ render json: result.to_h
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,110 @@
1
+ module StandardFlags
2
+ # Whether the calling client should, or must, upgrade, from the client
3
+ # registry (`client` in config/flags.rb; see StandardFlags::ClientApp):
4
+ #
5
+ # StandardFlags::ClientStatus.for(client).to_h
6
+ # # => { "status" => "upgrade_recommended", "platform" => "android", "version" => "0.3.2",
7
+ # # "min_version" => "0.4.0", "recommended" => "0.5.0", "enforce_at" => "2026-12-08",
8
+ # # "store_url" => "https://example.com/app" }
9
+ #
10
+ # * `unknown`: no platform, a platform this app doesn't recognise, or no
11
+ # usable version. An unknown build is never told to upgrade.
12
+ # * `upgrade_required`: the version is below the enforced minimum and
13
+ # enforcement has started (on `enforce_at`, which is `since` plus the
14
+ # grace window).
15
+ # * `upgrade_recommended`: below the minimum during its grace window (with
16
+ # `enforce_at`), or below `recommended`.
17
+ # * `ok`: anything else, including a recognised platform with no client
18
+ # declared for it.
19
+ #
20
+ # `min_version` and `recommended` are what applies after the store
21
+ # ceiling: never above the newest version known to be in the store.
22
+ # `enforce_at` is set whenever the version is below that minimum (a date
23
+ # still to come during the grace window, a past one once enforced). Dates
24
+ # are UTC.
25
+ #
26
+ # Versions compare by SemVer precedence, with one exception: a prerelease
27
+ # of exactly the minimum (or recommended) version meets it, so a beta or
28
+ # TestFlight build of the required release (`1.5.0-beta.2` against
29
+ # `1.5.0`) isn't locked out. A prerelease of an older version
30
+ # (`1.4.9-rc.1`) is still below it.
31
+ #
32
+ # StandardFlags::ClientStatus::Controller serves it as `GET
33
+ # /client/status`; StandardFlags::Controller's `requires_client_version`
34
+ # answers 426 when it is `upgrade_required`.
35
+ module ClientStatus
36
+ autoload :Controller, "standard_flags/client_status/controller"
37
+
38
+ STATUSES = %w[ok upgrade_recommended upgrade_required unknown].freeze
39
+
40
+ Result = Data.define(:status, :platform, :version, :min_version, :recommended, :enforce_at, :store_url, :client_app) do
41
+ def upgrade_required?
42
+ status == "upgrade_required"
43
+ end
44
+
45
+ def unknown?
46
+ status == "unknown"
47
+ end
48
+
49
+ # The status payload, with String keys; absent values are nil.
50
+ def to_h
51
+ { "status" => status, "platform" => platform, "version" => version&.to_s, "min_version" => min_version&.to_s,
52
+ "recommended" => recommended&.to_s, "enforce_at" => enforce_at&.iso8601, "store_url" => store_url }
53
+ end
54
+ end
55
+
56
+ module_function
57
+
58
+ # The status of `client` (a Client or a Hash) against the clients
59
+ # declared in `group:` (nil is the default group).
60
+ def for(client, registry: StandardFlags.registry, group: nil, today: Delivery.utc_today,
61
+ env: StandardFlags.config.env_source)
62
+ client = Client.wrap(client)
63
+ platform = client.platform
64
+ app = platform && registry.client_for(platform, group: group)
65
+ return result("unknown", platform, client.version, app&.resolve(env)) if platform.nil? || client.version.nil?
66
+ return Result.new(status: "ok", platform: platform, version: client.version, min_version: nil, recommended: nil,
67
+ enforce_at: nil, store_url: nil, client_app: nil) unless app
68
+
69
+ decide(client.version, app.resolve(env), today)
70
+ end
71
+
72
+ # Whether `version` meets `target`: at or above it, or a prerelease of
73
+ # exactly that release.
74
+ def meets?(version, target)
75
+ return true if version >= target
76
+
77
+ version.prerelease? && !target.prerelease? &&
78
+ [ version.major, version.minor, version.patch ] == [ target.major, target.minor, target.patch ]
79
+ end
80
+
81
+ def decide(version, policy, today)
82
+ ceiling = policy.ceiling
83
+ minimum = capped(policy.min_version, ceiling)
84
+ if minimum && !meets?(version, minimum)
85
+ status = today >= policy.enforce_on ? "upgrade_required" : "upgrade_recommended"
86
+ return result(status, policy.app.platform, version, policy, ceiling: ceiling, enforce_at: policy.enforce_on)
87
+ end
88
+
89
+ recommended = capped(policy.recommended, ceiling)
90
+ status = recommended && !meets?(version, recommended) ? "upgrade_recommended" : "ok"
91
+ result(status, policy.app.platform, version, policy, ceiling: ceiling)
92
+ end
93
+
94
+ def result(status, platform, version, policy, ceiling: :ask, enforce_at: nil)
95
+ ceiling = policy&.ceiling if ceiling == :ask
96
+ Result.new(status: status, platform: platform, version: version,
97
+ min_version: policy && capped(policy.min_version, ceiling),
98
+ recommended: policy && capped(policy.recommended, ceiling),
99
+ enforce_at: enforce_at, store_url: policy&.app&.store_url, client_app: policy&.app)
100
+ end
101
+
102
+ # `version`, never above `ceiling`.
103
+ def capped(version, ceiling)
104
+ return version if version.nil? || ceiling.nil?
105
+
106
+ [ version, ceiling ].min
107
+ end
108
+ private_class_method :decide, :result, :capped
109
+ end
110
+ end
@@ -23,9 +23,12 @@ module StandardFlags
23
23
  # Where `flags:refs` looks for flag reads.
24
24
  attr_accessor :refs_paths
25
25
  attr_accessor :refs_extensions
26
- # Outputs of `standard_flags:manifest` and `standard_flags:typescript`.
26
+ # Outputs of `standard_flags:manifest`, `standard_flags:typescript` and
27
+ # `standard_flags:kotlin`. `kotlin_package` is the generated file's
28
+ # package (nil writes none).
27
29
  attr_accessor :manifest_path
28
30
  attr_accessor :typescript_path
31
+ attr_accessor :kotlin_path, :kotlin_package
29
32
  # `call(controller, denial)` replaces the default `requires_flag` response.
30
33
  attr_accessor :controller_responder
31
34
  # Whose wire name `requires_flag` reports in `meta.feature`.
@@ -83,6 +86,16 @@ module StandardFlags
83
86
  # standard_flags_rules.last_evaluated_at for `flags:stale`, and at most
84
87
  # how often (seconds) it writes.
85
88
  attr_accessor :track_evaluations, :evaluation_tracking_interval
89
+ # Whether each process counts evaluations of compat flags that declare
90
+ # `until_clients:` (hits and the oldest client version per platform),
91
+ # writing them to standard_flags_compat_stats at most every
92
+ # `evaluation_tracking_interval` seconds. Off by default; needs runtime
93
+ # rules and the compat stats migration.
94
+ attr_accessor :track_compat
95
+ # When true, `requires_client_version` also answers 426 to a request
96
+ # whose platform or version is unknown. Off by default: an unknown build
97
+ # is never forced to upgrade.
98
+ attr_accessor :require_client_version
86
99
 
87
100
  DEFAULT_CLIENT_HEADERS = { platform: "X-Client-Platform", version: "X-Client-Version", build: "X-Client-Build" }.freeze
88
101
  DEFAULT_CLIENT_PLATFORMS = %w[android ios web desktop].freeze
@@ -103,6 +116,8 @@ module StandardFlags
103
116
  @refs_extensions = %w[rb erb rake haml slim jbuilder]
104
117
  @manifest_path = "config/flags.manifest.json"
105
118
  @typescript_path = "app/javascript/types/standard_flags.d.ts"
119
+ @kotlin_path = "config/StandardFlags.kt"
120
+ @kotlin_package = nil
106
121
  @controller_responder = nil
107
122
  @default_audience = :web
108
123
  @client_headers = DEFAULT_CLIENT_HEADERS
@@ -123,6 +138,8 @@ module StandardFlags
123
138
  @membership_retention = 90 * 86_400
124
139
  @track_evaluations = true
125
140
  @evaluation_tracking_interval = 3_600
141
+ @track_compat = false
142
+ @require_client_version = false
126
143
  @strict = nil
127
144
  @env_source = nil
128
145
  @extra_notifiers = []