standard_flags 0.2.1 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ea97e7c004dfcf42fddb724fcbdc25349c7530e75ca16a984eecaa4c449f865
4
- data.tar.gz: ccae09c7320ebe740672c67c8f1643eb226109eb2e2de3351f04a423ae9ddd40
3
+ metadata.gz: 600d390b676bbbc27caf2f9892aa3fe9b039453870741c794b3d345d7308fdbe
4
+ data.tar.gz: 8d65cabbcbf4e6c0d2353571dd64903bd71fe06271ca559294712e5545d370de
5
5
  SHA512:
6
- metadata.gz: 28a9a4d041bd89a44714599a195926bf7c7329913b3687972ab2ea3e46e0501acf0dd7e631a94556e050c0842d20cd7d30056301d25f5b2771ab95e33f79bece
7
- data.tar.gz: fb84a1908b439f755a023351b5814cf60321007c6aeea062e160d080f9114091095744bd59062f55092c11b1b21d248ed78d3b5ca4bb7524d17d3e4ad7ce4774
6
+ metadata.gz: 129c7ee394532d99a9047e9356c14549649bbe6b05039f6a9917f74d7005e11f13372840888e317d043a5fe52a2b272b641deabfca21eb881553e9f90a4707c2
7
+ data.tar.gz: acee76d7a4f1c7e2094bf30d8147eb762c5ae23c1b5c9bc112bb91a523c6bf2db0b6d4c5e1e4d6a8eaa60f2665895005214388b9611dd11e08a54627679d0451
data/CHANGELOG.md CHANGED
@@ -7,6 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.2] - 2026-11-23
11
+
12
+ ### Changed
13
+
14
+ - **`SetValue` on a draft rule starts it.** A draft serves the code default,
15
+ so setting a value on a flag nobody had touched (through `SetValue`,
16
+ `flags:set` or the MCP `set_flag_value` tool) used to succeed and change
17
+ nothing until someone also ran `Start`; "set the kill switch off" did
18
+ nothing. Now `SetValue` with a value on a draft rule, including the one it
19
+ creates on first use, moves it to running in the same write: one
20
+ transaction, one revision, one `standard_flags.flag.value_set` event and
21
+ audit row whose before and after show both the status and the value. The
22
+ running rule is checked as `Start` checks it; a failure fails the whole
23
+ operation with `:invalid` and changes nothing. The Result's value, the
24
+ event and the audit row carry `started: true`; no separate `started` event
25
+ is published. Clearing the value (`nil`) never starts a rule.
26
+ - **`SetValue` on a paused rule warns.** The rule stays paused and the value
27
+ is stored; `StandardFlags::Result#warnings` (new, empty by default) carries
28
+ `rule is paused; the value is stored but not served until Start`. A stopped
29
+ rule keeps its existing behaviour.
30
+ - `flags:set` prints `started` or the paused warning, `set_flag_value`
31
+ returns `started` and any `warnings`, and the admin generator's flash
32
+ shows them. Ramp, SetVariants, SetConditions, member and cohort changes on
33
+ a draft still leave it a draft, and Restore still puts back exact statuses.
34
+
10
35
  ## [0.2.1] - 2026-11-23
11
36
 
12
37
  ### Fixed
data/README.md CHANGED
@@ -460,13 +460,14 @@ Every change to a rule, cohort or membership goes through an operation in
460
460
  `StandardFlags::Operations`. Each one is called as
461
461
  `.call(key_or_cohort, actor:, reason:, expected_revision: nil, **args)` and
462
462
  returns a `StandardFlags::Result` (`success?`, `value`, `errors`, plus a
463
- `code` Symbol on failure). It never raises for a refusal.
463
+ `code` Symbol on failure, and `warnings` for a change that committed but may
464
+ not do what you expect). It never raises for a refusal.
464
465
 
465
466
  ```ruby
466
467
  ops = StandardFlags::Operations
467
468
  ops::Ramp.call(:new_checkout, bps: 500, actor: current_user, reason: "5% canary")
468
469
  ops::SetValue.call(:strict_gates, value: :sensor, actor: current_user, reason: "observe before enforcing")
469
- ops::SetValue.call(:ai_assistant, value: false, actor: current_user, reason: "provider outage") # kill switch
470
+ ops::SetValue.call(:ai_assistant, value: false, actor: current_user, reason: "provider outage") # kill switch, served at once
470
471
  ops::Start.call(:new_checkout, actor: current_user, reason: "go")
471
472
 
472
473
  ops::CreateCohort.call("staff", unit: :account, name: "Staff", actor: current_user, reason: "dogfood")
@@ -484,6 +485,33 @@ checked against, so it can be passed straight back: a rule's `lock_version`,
484
485
  or for cohort operations the store's revision. `store_revision` is always
485
486
  `standard_flags_state.revision`.
486
487
 
488
+ **`SetValue` on a draft starts it.** A rule starts as a draft, and a draft
489
+ serves the code default, so a value set on a flag nobody had touched would
490
+ otherwise change nothing until someone also ran `Start`. Instead, `SetValue`
491
+ with a value on a draft rule (including the one it creates on first use)
492
+ moves it to running in the same write: one transaction, one revision, one
493
+ `standard_flags.flag.value_set` event and audit row, whose `before` and
494
+ `after` show both the status and the value. The running rule is checked as
495
+ `Start` checks it; if that fails, the whole operation fails with `:invalid`
496
+ and nothing changes. Anything staged on the draft (a ramp, variants,
497
+ conditions, members, cohorts) serves from then on too. The Result's value,
498
+ the event and the audit row carry `started: true`; no separate
499
+ `standard_flags.flag.started` event is published.
500
+
501
+ ```ruby
502
+ result = ops::SetValue.call(:ai_assistant, value: false, actor: current_user, reason: "provider outage")
503
+ result.value[:started] # => true when it started a draft
504
+ result.warnings # => [] here; on a paused rule:
505
+ # ["rule is paused; the value is stored but not served until Start"]
506
+ ```
507
+
508
+ On a **paused** rule the value is stored but the rule stays paused (someone
509
+ stood it down on purpose), and the Result carries that warning. A running
510
+ or **stopped** rule keeps its status. Clearing the value (`value: nil`)
511
+ never starts a rule. Every other operation (`Ramp`, `SetVariants`,
512
+ `SetConditions`, `AddMembers`/`RemoveMembers`, `AttachCohort`/`DetachCohort`)
513
+ leaves a draft a draft, so changes can be staged and started together.
514
+
487
515
  | Operation | Scope | Does |
488
516
  |-----------|-------|------|
489
517
  | `Start` | `flags:write` | Draft or paused → running, after checking everything the rule would serve. |
@@ -491,7 +519,7 @@ or for cohort operations the store's revision. `store_revision` is always
491
519
  | `Stop(serve:)` | `flags:write` | → stopped, serving `serve:` to everyone (the winner, or the flag's `final:`) until the code drops the flag. |
492
520
  | `Ramp(bps:, value: nil)` | `flags:ramp` | Sets the percentage (0–10,000 basis points) and, for modes and experiments, the value it serves. At 10,000 it records `full_since`. |
493
521
  | `SetVariants(weights:)` | `flags:ramp` | Weights over the flag's variants or mode values, summing to 10,000. `{}` clears them. |
494
- | `SetValue(value:)` | `flags:write` | The fallthrough value, served to every unit nothing else matched. Moves a mode off → sensor → on and throws a kill switch. `nil` clears it. |
522
+ | `SetValue(value:)` | `flags:write` | The fallthrough value, served to every unit nothing else matched. Moves a mode off → sensor → on and throws a kill switch. Starts a draft rule (above); a paused one stays paused, with a warning. `nil` clears it. |
495
523
  | `SetConditions(platforms:, versions:, segments:)` | `flags:write` | Replaces the conditions; omitted ones are cleared. |
496
524
  | `AddMembers(keys:, value:, unit:, expires_at:)` | `flags:write` | Adds units (records or keys) to a rule or a cohort. Adding an existing member with a new value or expiry keeps the old row as removed and adds a new one, so [Restore](#restore) can read it back; adding it unchanged writes nothing. |
497
525
  | `RemoveMembers(keys:, unit:)` | `flags:write` | Removes them (soft delete, see retention below). |
@@ -632,6 +660,9 @@ and `config.add_notifier`), always after the commit.
632
660
  For a console with no admin UI. Each task needs `REASON=` (written to the
633
661
  audit log), runs as `StandardFlags::Actor.system("rake")`, prints the outcome
634
662
  and the new revision, and refuses pinned flags with the variable's name.
663
+ `flags:set` on a draft rule starts it and says so
664
+ (`flags:set ai_assistant: ok, revision 1 (status running, started)`); on a
665
+ paused rule it prints the warning on the next line.
635
666
 
636
667
  ```bash
637
668
  bin/rails flags:show KEY=new_checkout # declaration, value, pin, stored rule
@@ -753,7 +784,7 @@ end
753
784
  |------|-------|------|
754
785
  | `list_flags` | `flags:read` (per flag) | Every flag the actor may read: kind, owner, values, value with no unit, source, ENV pin, rule summary. |
755
786
  | `get_flag` | `flags:read`; with `unit_key`, `flags:write` and `reason` | One flag: value, reason, `rule_id` and source (`env_pin`, `runtime_rule`, `guard`, `code_default`, `error`, `test_override`), the ENV pin and the stored rule in full. With `unit_key` (normalised through the unit's `key:`, masked in the answer) it evaluates for that unit, which is a reveal (below). |
756
- | `start_flag`, `pause_flag`, `stop_flag`, `ramp_flag`, `set_flag_value` | as the operation | Run Start, Pause, Stop, Ramp and SetValue. |
787
+ | `start_flag`, `pause_flag`, `stop_flag`, `ramp_flag`, `set_flag_value` | as the operation | Run Start, Pause, Stop, Ramp and SetValue. `set_flag_value` returns `started: true` when it started a draft, and `warnings` when the rule is paused. |
757
788
  | `list_members` | `flags:read` | One page (`offset`, `limit` up to 200) of a flag's (`key`) or a cohort's (`cohort`) members. |
758
789
  | `add_members`, `remove_members` | `flags:write` | Run AddMembers and RemoveMembers on a flag or a cohort. |
759
790
  | `list_cohorts`, `get_cohort` | `flags:read` (per cohort) | Cohorts with their unit, member count, archived state and the flags they're attached to. |
@@ -78,7 +78,8 @@ class <%= controller_class %> < <%= base_controller %>
78
78
  result = operation.call(params[:id], actor: standard_flags_actor, reason: params[:reason].to_s,
79
79
  expected_revision: params[:expected_revision].presence&.to_i, **args)
80
80
  if result.success?
81
- flash[:notice] = "#{operation.name.demodulize}: done (revision #{result.value[:revision]})"
81
+ flash[:notice] = "#{operation.name.demodulize}: done (revision #{result.value[:revision]}#{', started' if result.value[:started]})" \
82
+ "#{result.warnings.map { |warning| "; #{warning}" }.join}"
82
83
  else
83
84
  flash[:alert] = "#{operation.name.demodulize} refused (#{result.code}): #{result.errors.join('; ')}"
84
85
  end
@@ -3,7 +3,8 @@ module StandardFlags
3
3
  # A tool that runs one operation. `reason` is required and
4
4
  # `expected_revision` (the rule's revision from get_flag) optional; the
5
5
  # operation authorises, refuses pins, validates and audits, and its
6
- # failure code becomes the error code.
6
+ # failure code becomes the error code. A success returns the Result's
7
+ # value, plus `warnings` when it has any.
7
8
  class OperationTool < Tool
8
9
  COMMON = {
9
10
  key: { type: "string", description: "The flag key" },
@@ -31,7 +32,7 @@ module StandardFlags
31
32
  registry: registry, **operation_args(args))
32
33
  raise ToolError.new(result.code, result.errors.join("; ")) unless result.success?
33
34
 
34
- result.value
35
+ result.warnings.empty? ? result.value : result.value.merge(warnings: result.warnings)
35
36
  end
36
37
 
37
38
  def target(args)
@@ -85,7 +86,8 @@ module StandardFlags
85
86
  class SetFlagValue < OperationTool
86
87
  tool_name "set_flag_value"
87
88
  description "Set a runtime rule's fallthrough value, served to every unit nothing else matched " \
88
- "(how a mode moves off → sensor → on, or a kill switch is thrown). null clears it."
89
+ "(how a mode moves off → sensor → on, or a kill switch is thrown). null clears it. " \
90
+ "A draft rule starts (the result says started: true); a paused one stays paused, with a warning."
89
91
  operation Operations::SetValue
90
92
  arguments({ value: { type: %w[string null], description: "on/off, a mode value or a variant; null clears" } },
91
93
  required: %w[value])
@@ -28,10 +28,12 @@ module StandardFlags
28
28
  # store_revision: }` plus what the operation changed. `revision` is the
29
29
  # number `expected_revision` is checked against: a rule's lock_version,
30
30
  # or for cohort operations the store's revision. `store_revision` is
31
- # always the store's. Success means the write committed (or, inside a
32
- # host transaction, will commit with it). Every failure is a Result,
33
- # never an exception; store and unexpected errors are reported through
34
- # `config.error_reporter` / Rails.error (:store_error, :error).
31
+ # always the store's. `warnings` lists anything about a committed change
32
+ # the caller should know (SetValue on a paused rule, say). Success
33
+ # means the write committed (or, inside a host transaction, will commit
34
+ # with it). Every failure is a Result, never an exception; store and
35
+ # unexpected errors are reported through `config.error_reporter` /
36
+ # Rails.error (:store_error, :error).
35
37
  class Base
36
38
  class << self
37
39
  def call(target, actor: nil, reason: nil, expected_revision: nil, registry: StandardFlags.registry, **args)
@@ -84,11 +86,12 @@ module StandardFlags
84
86
  check_target!
85
87
  check_runtime!
86
88
  @announcements = []
89
+ @warnings = []
87
90
  value = Runtime.store.write do |tx|
88
91
  @tx = tx
89
92
  run.tap { tx.after_commit { committed } }
90
93
  end
91
- Result.success(value)
94
+ Result.new(success: true, value: value, warnings: @warnings)
92
95
  rescue Failure => e
93
96
  Result.failure(e.code, e.message)
94
97
  rescue Runtime::StoreError => e
@@ -254,6 +257,11 @@ module StandardFlags
254
257
  @rule = tx.save_rule(@rule, attributes)
255
258
  end
256
259
 
260
+ # Adds a warning to the Result; the change still commits.
261
+ def warn_caller(message)
262
+ @warnings << message
263
+ end
264
+
257
265
  # -- helpers -----------------------------------------------------------
258
266
 
259
267
  def arg(name)
@@ -101,17 +101,45 @@ module StandardFlags
101
101
  # running rule matched. This is how a mode moves off → sensor → on at
102
102
  # runtime, and how a kill switch is thrown from the database.
103
103
  # `value: nil` clears it, so the code default serves.
104
+ #
105
+ # Setting a value on a draft rule (including the one created on first
106
+ # use) also starts it, in the same write: "set the kill switch off"
107
+ # serves at once rather than waiting for a Start. The running rule is
108
+ # checked as Start checks it, and anything staged on the draft (a ramp,
109
+ # variants, conditions, members, cohorts) serves from then on too. The
110
+ # Result's value and the `value_set` event carry `started: true`; no
111
+ # separate `started` event is published, and the one audit row's before
112
+ # and after show both the status and the value. Clearing (`nil`) never
113
+ # starts a rule.
114
+ #
115
+ # A paused rule stays paused (someone stood it down on purpose): the
116
+ # value is stored, and the Result warns that it isn't served until
117
+ # Start. A running or stopped rule keeps its status.
104
118
  class SetValue < Base
119
+ PAUSED = "rule is paused; the value is stored but not served until Start".freeze
120
+
105
121
  arguments required: %i[value]
106
122
 
107
123
  private
108
124
 
109
125
  def event = Events::FLAG_EVENTS[:value_set]
110
126
 
127
+ # The cohorts a draft attaches, which the check of the started rule
128
+ # reads.
129
+ def extra_cohort_keys
130
+ starts? ? validate.attachments(definition, @rule) : []
131
+ end
132
+
111
133
  def perform
112
134
  value = arg(:value).nil? ? nil : validate.value!(definition, arg(:value), "value")
113
- save!(fallthrough_value: value)
114
- nil
135
+ started = starts?
136
+ warn_caller(PAUSED) if @rule.paused? && !value.nil?
137
+ save!(fallthrough_value: value, **(started ? { status: "running" } : {}))
138
+ { started: started }
139
+ end
140
+
141
+ def starts?
142
+ @rule.draft? && !arg(:value).nil?
115
143
  end
116
144
  end
117
145
 
@@ -1,7 +1,8 @@
1
1
  module StandardFlags
2
2
  # What every operation returns, in the shape the host apps' own operations
3
3
  # use: `success?`, `value` and `errors` (human-readable messages), plus
4
- # `code`, a Symbol a caller can branch on:
4
+ # `code`, a Symbol a caller can branch on, and `warnings`, messages about
5
+ # a change that succeeded but may not do what the caller expects:
5
6
  #
6
7
  # result = StandardFlags::Operations::Ramp.call(:new_checkout, bps: 500, actor: user, reason: "canary")
7
8
  # result.success? # => true
@@ -9,8 +10,10 @@ module StandardFlags
9
10
  #
10
11
  # result.code # on failure, e.g. :pinned_by_env, :stale_revision,
11
12
  # # :unauthorized, :invalid, :limit_exceeded
13
+ # result.warnings # e.g. SetValue on a paused rule:
14
+ # # ["rule is paused; the value is stored but not served until Start"]
12
15
  class Result
13
- attr_reader :value, :errors, :code
16
+ attr_reader :value, :errors, :code, :warnings
14
17
 
15
18
  def self.success(value)
16
19
  new(success: true, value: value)
@@ -20,11 +23,12 @@ module StandardFlags
20
23
  new(success: false, code: code, errors: Array(errors))
21
24
  end
22
25
 
23
- def initialize(success:, value: nil, errors: [], code: nil)
26
+ def initialize(success:, value: nil, errors: [], code: nil, warnings: [])
24
27
  @success = success
25
28
  @value = value
26
29
  @errors = errors.map(&:to_s).freeze
27
30
  @code = code
31
+ @warnings = warnings.map(&:to_s).freeze
28
32
  freeze
29
33
  end
30
34
 
@@ -67,8 +67,9 @@ module StandardFlags
67
67
  end
68
68
 
69
69
  # The rake tasks that change flags. Each needs REASON= and runs as
70
- # `StandardFlags::Actor.system("rake")`; it prints the outcome and the
71
- # new revision.
70
+ # `StandardFlags::Actor.system("rake")`; it prints the outcome, the new
71
+ # revision, "started" when the change started a draft rule, and any
72
+ # warnings.
72
73
  class Command
73
74
  ACTOR = Actor.system("rake")
74
75
  # How an AT= ends: Z or an offset. A bare local time would be read in
@@ -156,8 +157,10 @@ module StandardFlags
156
157
 
157
158
  value = result.value
158
159
  state = value[:rule] || value[:state]
159
- details = [ ("status #{state[:status]}" if state&.key?(:status)), *value.slice(:added, :updated, :removed).map { |k, v| "#{v} #{k}" } ]
160
- Outcome.new(ok: true, message: "#{name}: ok, revision #{value[:revision]}#{" (#{details.compact.join(', ')})" unless details.compact.empty?}")
160
+ details = [ ("status #{state[:status]}" if state&.key?(:status)), ("started" if value[:started]),
161
+ *value.slice(:added, :updated, :removed).map { |k, v| "#{v} #{k}" } ]
162
+ lines = [ "#{name}: ok, revision #{value[:revision]}#{" (#{details.compact.join(', ')})" unless details.compact.empty?}" ]
163
+ Outcome.new(ok: true, message: (lines + result.warnings.map { |w| " warning: #{w}" }).join("\n"))
161
164
  end
162
165
 
163
166
  def refuse_result(name, result)
@@ -1,3 +1,3 @@
1
1
  module StandardFlags
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rarebit One