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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +229 -0
  3. data/README.md +837 -33
  4. data/lib/generators/standard_flags/admin/admin_generator.rb +115 -0
  5. data/lib/generators/standard_flags/admin/templates/controller.rb.tt +103 -0
  6. data/lib/generators/standard_flags/admin/templates/index.tsx.tt +64 -0
  7. data/lib/generators/standard_flags/admin/templates/show.tsx.tt +275 -0
  8. data/lib/generators/standard_flags/install/install_generator.rb +16 -1
  9. data/lib/generators/standard_flags/install/templates/flags.rb.tt +3 -2
  10. data/lib/generators/standard_flags/install/templates/initializer.rb.tt +10 -0
  11. data/lib/generators/standard_flags/migrations/migrations_generator.rb +57 -0
  12. data/lib/generators/standard_flags/migrations/templates/create_standard_flags_tables.rb.tt +78 -0
  13. data/lib/standard_flags/actor.rb +50 -0
  14. data/lib/standard_flags/audit_trail.rb +56 -0
  15. data/lib/standard_flags/config.rb +78 -0
  16. data/lib/standard_flags/context.rb +5 -1
  17. data/lib/standard_flags/definition.rb +85 -13
  18. data/lib/standard_flags/diagnostics.rb +35 -3
  19. data/lib/standard_flags/engine.rb +10 -2
  20. data/lib/standard_flags/env_pin.rb +15 -4
  21. data/lib/standard_flags/errors.rb +4 -0
  22. data/lib/standard_flags/evaluation.rb +5 -4
  23. data/lib/standard_flags/evaluator.rb +14 -1
  24. data/lib/standard_flags/events.rb +49 -0
  25. data/lib/standard_flags/health_check.rb +12 -2
  26. data/lib/standard_flags/health_contribution.rb +65 -0
  27. data/lib/standard_flags/mcp/cohorts.rb +65 -0
  28. data/lib/standard_flags/mcp/flags.rb +143 -0
  29. data/lib/standard_flags/mcp/members.rb +154 -0
  30. data/lib/standard_flags/mcp/writes.rb +98 -0
  31. data/lib/standard_flags/mcp.rb +238 -0
  32. data/lib/standard_flags/memo.rb +15 -1
  33. data/lib/standard_flags/notifiers/logger.rb +14 -0
  34. data/lib/standard_flags/open_feature/provider.rb +232 -0
  35. data/lib/standard_flags/open_feature.rb +26 -0
  36. data/lib/standard_flags/operations/audit.rb +218 -0
  37. data/lib/standard_flags/operations/base.rb +290 -0
  38. data/lib/standard_flags/operations/cohorts.rb +80 -0
  39. data/lib/standard_flags/operations/failure.rb +16 -0
  40. data/lib/standard_flags/operations/members.rb +164 -0
  41. data/lib/standard_flags/operations/restore.rb +330 -0
  42. data/lib/standard_flags/operations/restore_target.rb +197 -0
  43. data/lib/standard_flags/operations/rules.rb +233 -0
  44. data/lib/standard_flags/operations/validation.rb +154 -0
  45. data/lib/standard_flags/operations.rb +43 -0
  46. data/lib/standard_flags/parser.rb +16 -0
  47. data/lib/standard_flags/purge_job.rb +14 -0
  48. data/lib/standard_flags/registry.rb +38 -5
  49. data/lib/standard_flags/reporting.rb +51 -7
  50. data/lib/standard_flags/resolvers.rb +14 -7
  51. data/lib/standard_flags/result.rb +43 -0
  52. data/lib/standard_flags/rspec.rb +67 -13
  53. data/lib/standard_flags/runtime/active_record_store.rb +428 -0
  54. data/lib/standard_flags/runtime/cache.rb +98 -0
  55. data/lib/standard_flags/runtime/evaluation_tracker.rb +93 -0
  56. data/lib/standard_flags/runtime/memory_store.rb +408 -0
  57. data/lib/standard_flags/runtime/models.rb +43 -0
  58. data/lib/standard_flags/runtime/resolver.rb +256 -0
  59. data/lib/standard_flags/runtime/rule_snapshot.rb +60 -0
  60. data/lib/standard_flags/runtime/store.rb +161 -0
  61. data/lib/standard_flags/runtime/values.rb +181 -0
  62. data/lib/standard_flags/runtime.rb +266 -0
  63. data/lib/standard_flags/sensor.rb +12 -5
  64. data/lib/standard_flags/subscribers.rb +6 -0
  65. data/lib/standard_flags/testing.rb +100 -1
  66. data/lib/standard_flags/tooling/changes.rb +196 -0
  67. data/lib/standard_flags/tooling.rb +232 -19
  68. data/lib/standard_flags/version.rb +1 -1
  69. data/lib/standard_flags.rb +33 -5
  70. data/lib/tasks/standard_flags.rake +45 -1
  71. metadata +41 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ed8742c4b0516b2939f0ec26ecd61fd5d133172ec67b0aed28fdc0c76d87e9e
4
- data.tar.gz: '08e1e0101f9e67874ffb6fbabdc1916795129371cb4d426b2eb1c82390d4b5ae'
3
+ metadata.gz: cc29678f25dbc06d48749a9f203131df8ac5ce1c67740c59b32a658bfad23c49
4
+ data.tar.gz: 00e160c90ab46bff75740b02b748f5fde6fc5300a6c03b13ba30a6d44a947fa0
5
5
  SHA512:
6
- metadata.gz: 43af8495cd0e8f0d4781b1afd3719f3546cf3004179a054e9b79b2c70830506eaf1675c2b33d0a77668f2f60461dfb89915c139f2eb1caf0c353c2ccc1c8d7ab
7
- data.tar.gz: b3f64247ac6c9c61841dea4ca556b205807b04c255ac200b1a40314b8f32e3ae3d9b111dd8e8629e02c33c6fc43e46c32cd015ee9dddc650863bef19cb889a41
6
+ metadata.gz: a8797e0cc12e6d0ab5aa561b1f39124f09852440ccfffe56eea6778c07a8df758bf2076c07a5f25ada18236b09181b7c33bc05eb584579a72d932bd72136ac6a
7
+ data.tar.gz: 372b1f38a162bd6dfef0cab9ffe371fd04a1b3d61b8013df123ef429c78edc1b709a5d6286065cba2f015a56dde3ae7eb2cfa94edb824cc38b8bd9307fe0522a
data/CHANGELOG.md CHANGED
@@ -7,6 +7,235 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2026-11-23
11
+
12
+ ### Added
13
+
14
+ - **Runtime rules (read path), opt-in.** `config.runtime_rules` (default
15
+ false) and `config.refresh_interval` (default 5 seconds). With runtime rules
16
+ off, behaviour is unchanged and the gem never loads ActiveRecord.
17
+ - **Migrations generator.** `bin/rails g standard_flags:migrations`, and
18
+ `standard_flags:install --runtime`, write one migration creating
19
+ `standard_flags_rules`, `standard_flags_cohorts`,
20
+ `standard_flags_memberships` and the single-row `standard_flags_state`, with
21
+ UUID keys when the app generates them. It passes StrongMigrations.
22
+ - **Runtime resolver** between the ENV pin and the code default. Draft and
23
+ paused rules serve the code default, stopped rules their stop value, and
24
+ running rules walk deny, allow, cohorts, segments, client conditions,
25
+ percentage and variants, then the fallthrough. `Evaluation#rule_id` names
26
+ the step that decided. A missing unit key returns `TARGETING_KEY_MISSING`;
27
+ an unservable stored value fails only its flag with `PARSE_ERROR`.
28
+ - **Process cache** of the rules table: an immutable snapshot refreshed
29
+ lazily on evaluation when the revision moves, no background thread, last
30
+ good snapshot (marked stale) on database errors, code defaults before the
31
+ first load.
32
+ - **Memberships** read in one query per unit key per unit of work, skipped
33
+ when no rule could match; `StandardFlags.preload(unit, records_or_keys)`
34
+ reads many in one query and never raises. A failed lookup is remembered
35
+ for the unit of work; its flags skip members and cohorts and carry on with
36
+ reason `ERROR` / `GENERAL`.
37
+ - **One rules snapshot per unit of work**, pinned by the first evaluation,
38
+ so a request or job never mixes revisions.
39
+ - **Stores.** `StandardFlags::Runtime::ActiveRecordStore` (the default) and
40
+ `MemoryStore` (specs, apps without a database), via `config.runtime_store`.
41
+ - **Registry.** `unit ..., key: ->(actor) { ... }` normalises unit keys
42
+ (default `record.id.to_s`, else the value; blank is missing).
43
+ `bucketing: { version: 1 | 2, salt: }` on flags, modes and experiments.
44
+ - **`config.segment_resolver`** answers segment conditions.
45
+ - **Test helpers** `with_rule`, `with_members` and `with_cohort` (and
46
+ `StandardFlags::Testing.rule/members/cohort`), backed by `MemoryStore` and
47
+ reset after each example.
48
+ - **Health and diagnostics.** The health check fails when runtime rules are
49
+ on and their tables are missing. It warns when the store can't be reached,
50
+ on a stale snapshot, when prepared statements are off (unit keys would be
51
+ inlined into SQL text), and on rules for undeclared or tombstoned keys.
52
+ Diagnostics and `flags:list` show each rule's status and the snapshot's
53
+ revision and age.
54
+ - **Privacy.** With runtime rules on, `unit_key` is added to
55
+ `ActiveRecord::Base.filter_attributes` so SQL logs filter it; `Membership`
56
+ hides it from `inspect` and `pp`; read errors are reported by class only.
57
+ - **standard_health contributions.** On standard_health 0.9+, the gem
58
+ contributes its check (`:standard_flags`, non-critical) to
59
+ `register_default_checks` and declares the pins of `forbidden_in:` flags in
60
+ the env audit. Registering `StandardFlags::HealthCheck` by hand still works.
61
+ - **`config.error_reporter`** = `->(error, context) { ... }` replaces
62
+ `Rails.error` for every report the gem makes, falling back to `Rails.error`
63
+ if it raises.
64
+ - **Notifications** `runtime_refresh`, `runtime_stale`, `rule_invalid` and
65
+ `targeting_key_missing` (`.standard_flags`), with Logger lines.
66
+ - **OpenFeature provider (optional).** `require "standard_flags/open_feature"`
67
+ defines `StandardFlags::OpenFeature::Provider` for the `openfeature-sdk`
68
+ gem, which the app brings (it is not a dependency; requiring the file
69
+ without it raises a `LoadError` that says so). Booleans answer
70
+ `fetch_boolean_value`; modes and experiments answer `fetch_string_value`.
71
+ Other pairings return the caller's default with `TYPE_MISMATCH`, and
72
+ unknown keys return it with `FLAG_NOT_FOUND`, even when strict. Reasons and
73
+ error codes map one to one, `ENV_PIN` and `KILL_SWITCH` pass through, and
74
+ `kind`, `rule_id` and `source_env_var` go in `flag_metadata`. The
75
+ evaluation context maps to a `StandardFlags::Context`: `targeting_key` and
76
+ unit-named fields are unit keys, used as given (no `from:` or `key:`);
77
+ `platform`, `appVersion`/`app_version` and `build` become the client. Only
78
+ the flag's own unit key (or `targeting_key`) must be a scalar; other units'
79
+ non-scalar fields are dropped. `env` is ignored unless the provider is built
80
+ with `allow_env_override: true`. The segment resolver receives keys, not
81
+ records, under OpenFeature.
82
+ - **Bounded report registry.** The once-per-process report memory holds at
83
+ most 1,000 tokens. Past that it stops recording (and reporting) new ones
84
+ and emits one `report_registry_full.standard_flags` notification, so
85
+ distinct unknown keys can't grow memory without limit.
86
+ - **Conformance** fixtures for runtime rules
87
+ (`spec/fixtures/conformance/runtime/`), and a Postgres CI job that runs the
88
+ database specs, including `Bucket.v2` against `Bucket.sql_v2`.
89
+ - **Write path.** `StandardFlags::Operations`: `Start`, `Pause`, `Stop`,
90
+ `Ramp`, `SetVariants`, `SetValue`, `SetConditions`, `AddMembers`,
91
+ `RemoveMembers`, `CreateCohort`, `ArchiveCohort`, `AttachCohort` and
92
+ `DetachCohort`, each called as
93
+ `.call(key_or_cohort, actor:, reason:, expected_revision: nil, **args)` and
94
+ returning a `StandardFlags::Result` (`success?`, `value`, `errors`, `code`).
95
+ Each authorises through `config.authorize` (scopes `flags:read`,
96
+ `flags:write`, `flags:ramp`; the default allows only
97
+ `StandardFlags::Actor.system` actors), refuses unknown, tombstoned and
98
+ ENV-pinned flags (`:pinned_by_env`, naming the variable), and writes in one
99
+ transaction: the rule is created on first use (salt and hash version from
100
+ `bucketing:`, else random and v2) and locked, `expected_revision` is
101
+ checked against its `lock_version`, the change is validated (a rule left
102
+ running is checked whole, as `Start` checks it), and the state revision
103
+ moves on. Results carry `revision` (what `expected_revision` is checked
104
+ against: a rule's `lock_version`, the store's revision for cohorts) and
105
+ `store_revision`. A success means the write committed: a transaction that
106
+ a swallowed error aborted fails with `:store_error`. Locks are taken in one
107
+ order (owner, cohorts by id, memberships, state row). `CreateCohort` on a
108
+ taken key, even a concurrent one, fails with `:already_exists`.
109
+ `AttachCohort` and `DetachCohort` authorise the cohort as well as the flag.
110
+ `config.actor_resolver` defaults to `Current.user`.
111
+ - **Validation on write**, with configurable limits: `max_members` (10,000
112
+ live members per rule or cohort), `max_keys_per_operation` (1,000),
113
+ `max_cohorts_per_rule` (20) and `max_conditions_bytes` (64 KB).
114
+ - **Audit.** Every operation records the actor, the reason and the full rule
115
+ or cohort state before and after, and publishes
116
+ `standard_flags.flag.*` / `standard_flags.cohort.*`
117
+ (`StandardFlags::Events.auditable`) plus `change.standard_flags`. With
118
+ standard_audit loaded, one audit row per change, in a savepoint inside the
119
+ write, through its subscriber when the app subscribes to the events, else
120
+ through `StandardAudit::Operation#audit!`; `raise_on_audit_write_error`
121
+ fails the operation on either path. Everything else (the events when
122
+ standard_audit doesn't need them in the transaction,
123
+ `change.standard_flags`, notifiers, cache expiry) runs after the commit.
124
+ Membership changes record the unit, the value and counts, never a unit
125
+ key, and memberships are inserted with bound values, never inlined SQL.
126
+ - **Soft-deleted memberships and purge.** `RemoveMembers` sets `removed_at`;
127
+ `flags:purge` and `StandardFlags::PurgeJob` delete removed and expired
128
+ memberships older than `config.membership_retention` (90 days), locking
129
+ the rules and cohorts it recounts first.
130
+ - **Rake tasks** `flags:show`, `flags:set`, `flags:ramp`, `flags:start`,
131
+ `flags:pause`, `flags:stop`, `flags:members:add`, `flags:members:remove`
132
+ and `flags:purge`, each with a required `REASON=`, run as the system actor.
133
+ - **`last_evaluated_at` write-behind.** Each process records the flags it
134
+ evaluates and writes them when a unit of work ends (the executor's
135
+ `complete` hook), at most once per `config.evaluation_tracking_interval`
136
+ (an hour) per process, in one `UPDATE` that skips locked rows and leaves
137
+ `lock_version` and `updated_at` alone. Never during an evaluation or
138
+ inside an open transaction; `config.track_evaluations = false` turns it
139
+ off.
140
+ - **`flags:stale`** also lists rules at 100% or stopped for 30 days, rules
141
+ not evaluated in 14 days, rules for undeclared or tombstoned keys, and
142
+ memberships due for purge.
143
+ - **Revisions never repeat.** `standard_flags_state.revision` is a
144
+ `bigserial`, and every write sets it to the sequence's next value
145
+ (`StandardFlags::Runtime.bump_revision!`), so a revision taken by a write
146
+ that rolled back is never committed again for different rules; a process
147
+ that loaded the rolled-back rules reloads at its next check. A missing
148
+ state row (a database built from `schema.rb`) is created by the next
149
+ write; until then the revision reads as none, every check reloads, and the
150
+ health check warns.
151
+ - **Conformance** fixtures for the write path
152
+ (`spec/fixtures/conformance/operations/`).
153
+ - **Operator tools (`StandardFlags::MCP`).** `list_flags`, `get_flag`,
154
+ `start_flag`, `pause_flag`, `stop_flag`, `ramp_flag`, `set_flag_value`,
155
+ `list_members`, `add_members`, `remove_members`, `list_cohorts` and
156
+ `get_cohort`: plain objects with `name`, `description`, `input_schema` and
157
+ `call(arguments, actor:)`, returning JSON-safe Hashes or
158
+ `{ "error" => { "code", "message" } }`, with no MCP framework dependency.
159
+ Reads need `flags:read`, filter cross-references (a cohort's attached
160
+ flags, a flag's cohorts) the same way, and never mark a rule as evaluated;
161
+ writes go through the operations and need a reason. `get_flag` names an
162
+ ENV pin's variable, never its value. Unit keys are masked (`•••4567`)
163
+ unless `list_members` is called with `reveal: true`, a reason and
164
+ `flags:write`; a reveal publishes and audits
165
+ `standard_flags.members.revealed` with counts only. `get_flag` with a
166
+ `unit_key` is a reveal too (`flags:write`, a reason,
167
+ `standard_flags.flag.probed`). Both fail closed under standard_audit's
168
+ `raise_on_audit_write_error`, including when its subscriber would swallow
169
+ the failure. One tool instance can serve concurrent calls from different
170
+ actors.
171
+ - **Admin page generator.** `bin/rails g standard_flags:admin` writes a
172
+ controller (index, show, one action per operation; configurable base class,
173
+ path and signed-in-user method, answering 401 when nobody is signed in),
174
+ routes, and two Inertia pages in plain markup typed from the generated
175
+ TypeScript. Unit keys are posted as `unit_keys`, which Rails' default
176
+ parameter filter keeps out of logs. Pinned flags are read-only, labelled
177
+ "Pinned by environment (VAR)".
178
+ - **TypeScript** output adds `FlagKey`, `FlagValues` and the shapes of the
179
+ MCP tools' results (`FlagList`, `FlagDetail`, `FlagMemberPage`,
180
+ `FlagAuditEntry`, ...).
181
+ - **Restore.** `StandardFlags::Operations::Restore` and `flags:restore AT=
182
+ [KEYS=] [APPLY=1] REASON=` put rules (from standard_audit's audit log) and
183
+ memberships (from the memberships table) back as they were at a past time:
184
+ a dry run by default; applied as the system actor, each flag (settings,
185
+ cohorts, members) and each cohort's members in one write with one
186
+ revision bump and one `standard_flags.flag.restored` /
187
+ `standard_flags.cohort.restored` row, cohorts first, removals before
188
+ additions, expecting each rule's planned revision, with partial failure
189
+ reported per target. Skips flags with no audit history at T, pinned flags
190
+ and times older than `config.membership_retention`; `AT=` must name its
191
+ zone.
192
+ - **Operator store reads.** `Store#members` (a page of an owner's
193
+ memberships) and `Store#members_at` (as of a time), on both stores;
194
+ `StandardFlags::AuditTrail` reads rule history and recent entries from
195
+ standard_audit's `audit_logs`; `Registry#with_unit_key`.
196
+ - **`legacy_parser: :exact`** (with `legacy_true:`, default `%w[on]`) reads
197
+ legacy `env:` names the way an exact `ENV["X"] == "on"` reader did: on/off
198
+ kinds are on only for a `legacy_true:` value and pinned off by any other
199
+ value, including a set-but-blank one (an unset variable still falls
200
+ through; inverted names keep blank as unset); modes and experiments match
201
+ a value exactly and treat blank as unset. The
202
+ canonical `STANDARD_FLAGS_<KEY>` pin stays strict. Conformance fixtures in
203
+ `legacy_exact.json`.
204
+ - **Mode lifecycle.** `mode` accepts `remove_by:` with `final:` (one of its
205
+ values), or `permanent: true`, with the same rules as `flag`. Stale modes
206
+ appear in `flags:stale`, Diagnostics and the health check's warnings.
207
+ - **`StandardFlags.sensor(..., report: false)`** swallows an observe error in
208
+ sensor mode without reporting it; the notification still carries the error
209
+ class. The default still reports.
210
+
211
+ ### Changed
212
+
213
+ - **A changed membership is versioned.** `AddMembers` on a live member with
214
+ a different value or expiry marks the old row removed and inserts a new
215
+ one (re-adding it unchanged writes nothing), so Restore can read the value
216
+ a member had at a past time. Purge deletes the old rows after the
217
+ retention as usual.
218
+ - The manifest's `permanent` key is now on entries of kind `mode` as well as
219
+ `flag`. Regenerate a committed manifest after upgrading.
220
+
221
+ ### Fixed
222
+
223
+ - **Overrides set in an `around` hook were discarded.** `standard_flags/rspec`
224
+ reset overrides in `config.before`, which runs inside `example.run`, so
225
+ `around { |ex| with_flag(:x, true) { ex.run } }` read the code default. The
226
+ reset now runs in a config-level `around` hook, outside every group-level
227
+ one, and overrides set before an example starts fail it with a clear error
228
+ instead of being dropped. `flags:` metadata and `before` hooks work as
229
+ before. `StandardFlags::RSpecSupport.install(config)` wires the helpers into
230
+ a custom configuration.
231
+
232
+ ### Changed
233
+
234
+ - **`forbidden_in:` guards can't be relaxed by a context environment.** A
235
+ flag is forced off when either the context's `env` or the process's own
236
+ environment (`registry.current_env`) is forbidden. Before, a
237
+ `Context.new(env:)` naming an allowed environment skipped the guard.
238
+
10
239
  ## [0.1.0] - 2026-11-22
11
240
 
12
241
  ### Added