From 2c1aeb7ea644327eaa840c4f766f97cf007393e5 Mon Sep 17 00:00:00 2001 From: Will McLemore Date: Mon, 23 Nov 2026 14:42:15 -0600 Subject: [PATCH] feat(engine): add opt-in soft-close extension on leader/price-changing void --- .../usd-soft-close-extend-on-void.yaml | 12 + ...id-extends-soft-close-when-configured.yaml | 24 ++ rfcs/0001-timed-online-proxy-bidding.md | 5 +- ruby/engine/lib/rbbb/configuration.rb | 7 +- ruby/engine/lib/rbbb/engine.rb | 17 +- ruby/engine/test/bid_void_test.rb | 275 ++++++++++++++++++ .../configuration/timed-proxy.schema.json | 5 + 7 files changed, 342 insertions(+), 3 deletions(-) create mode 100644 conformance/fixtures/usd-soft-close-extend-on-void.yaml create mode 100644 conformance/scenarios/timed-proxy/044-void-extends-soft-close-when-configured.yaml diff --git a/conformance/fixtures/usd-soft-close-extend-on-void.yaml b/conformance/fixtures/usd-soft-close-extend-on-void.yaml new file mode 100644 index 0000000..157f3f4 --- /dev/null +++ b/conformance/fixtures/usd-soft-close-extend-on-void.yaml @@ -0,0 +1,12 @@ +currency: USD +opening_minor_units: 10000 +reserve_minor_units: +increments: + - from_minor_units: 0 + amount_minor_units: 1000 +opens_at: "2026-09-01T12:00:00Z" +closes_at: "2026-09-01T13:00:00Z" +extension: + trigger_window_seconds: 300 + duration_seconds: 300 + extend_on_void: true diff --git a/conformance/scenarios/timed-proxy/044-void-extends-soft-close-when-configured.yaml b/conformance/scenarios/timed-proxy/044-void-extends-soft-close-when-configured.yaml new file mode 100644 index 0000000..8e76a91 --- /dev/null +++ b/conformance/scenarios/timed-proxy/044-void-extends-soft-close-when-configured.yaml @@ -0,0 +1,24 @@ +name: Operator void inside soft-close window extends closing when extend_on_void is enabled +specification_version: 0.2.0-draft +rfcs: ["0001", "0005"] +configuration: {fixture: usd-soft-close-extend-on-void} +starting_state: {events: []} +commands: + - {command_id: command-1, type: place_bid, bid_id: bid-a, bidder_id: bidder-a, maximum_minor_units: 30000, effective_at: "2026-09-01T12:10:00Z"} + - {command_id: command-2, type: place_bid, bid_id: bid-b, bidder_id: bidder-b, maximum_minor_units: 50000, effective_at: "2026-09-01T12:11:00Z"} + - {command_id: command-3, type: void_bid, bid_id: bid-b, operator_id: operator-1, reason: bidder_entry_error, notification_policy: affected, effective_at: "2026-09-01T12:58:00Z"} +expected: + public_events: + - {type: standing_bid_changed, standing_minor_units: 10000, next_required_minor_units: 11000, leader_changed: true} + - {type: standing_bid_changed, standing_minor_units: 31000, next_required_minor_units: 32000, leader_changed: true} + - {type: standing_bid_changed, standing_minor_units: 10000, next_required_minor_units: 11000, leader_changed: true} + - {type: closing_time_changed, closes_at: "2026-09-01T13:03:00Z"} + privileged_events: + - {type: bid_voided, bid_id: bid-b, operator_id: operator-1, reason: bidder_entry_error, closes_at: "2026-09-01T13:03:00Z"} + - {type: executed_amount_changed, bidder_id: bidder-a, old_minor_units: 30000, new_minor_units: 10000} + - {type: notification_requested, policy: affected, bidder_ids: [bidder-a, bidder-b]} + final_state: + leader_id: bidder-a + standing_minor_units: 10000 + closes_at: "2026-09-01T13:03:00Z" + voided_bid_ids: [bid-b] diff --git a/rfcs/0001-timed-online-proxy-bidding.md b/rfcs/0001-timed-online-proxy-bidding.md index edc0b9b..c4fa026 100644 --- a/rfcs/0001-timed-online-proxy-bidding.md +++ b/rfcs/0001-timed-online-proxy-bidding.md @@ -199,7 +199,10 @@ before the current closing time, closing time is unchanged and no A qualifying bid is an accepted command that changes the public standing amount or leader. Increasing one's own private maximum without changing the public -result does not extend closing. +result does not extend closing. By default, operator bid voids do not extend +closing; when `extension.extend_on_void` is explicitly enabled (`true`), a +`void_bid` that changes the leader or standing amount inside the trigger window +extends closing time using the same rule and emits `closing_time_changed`. Example: with a 1:00 PM close, five-minute trigger window, and five-minute extension, a qualifying bid at 12:58 resets close to 1:03. Another at 1:02 diff --git a/ruby/engine/lib/rbbb/configuration.rb b/ruby/engine/lib/rbbb/configuration.rb index 429236a..8ce81bf 100644 --- a/ruby/engine/lib/rbbb/configuration.rb +++ b/ruby/engine/lib/rbbb/configuration.rb @@ -73,6 +73,7 @@ def normalize_extension(extension) values = extension.transform_keys(&:to_s) trigger = values["trigger_window_seconds"] duration = values["duration_seconds"] + extend_on_void = values.fetch("extend_on_void", false) # Seconds share the interoperable integer bound so closing-time # arithmetic (command time plus duration) stays representable in every # implementation and inside the RFC 3339 four-digit-year range. @@ -84,10 +85,14 @@ def normalize_extension(extension) raise InvalidConfiguration, "extension duration must be a positive integer no greater than #{MAX_SAFE_INTEGER}" end + unless extend_on_void == true || extend_on_void == false + raise InvalidConfiguration, "extension extend_on_void must be a boolean" + end { "trigger_window_seconds" => trigger, - "duration_seconds" => duration + "duration_seconds" => duration, + "extend_on_void" => extend_on_void }.freeze end end diff --git a/ruby/engine/lib/rbbb/engine.rb b/ruby/engine/lib/rbbb/engine.rb index e04b862..4af8c8c 100644 --- a/ruby/engine/lib/rbbb/engine.rb +++ b/ruby/engine/lib/rbbb/engine.rb @@ -569,6 +569,13 @@ def build_void_decision(state, command, target, effective_at) voided_bid_ids, state.reserve_history ) + public_result_changed = recomputed.fetch("standing_minor_units") != state.standing_minor_units || + recomputed.fetch("leader_id") != state.leader_id + closes_at = if configuration.extension&.fetch("extend_on_void", false) + extended_closing_time(state, effective_at, public_result_changed) + else + state.closes_at + end common_data = { "command_id" => command.fetch("command_id"), "aggregate_version" => aggregate_version @@ -589,7 +596,7 @@ def build_void_decision(state, command, target, effective_at) "next_required_minor_units" => recomputed.fetch("next_required_minor_units"), "reserve_status" => recomputed.fetch("reserve_status"), "reserve_minor_units" => state.reserve_minor_units, - "closes_at" => Timestamp.dump(state.closes_at), + "closes_at" => Timestamp.dump(closes_at), "positions" => recomputed.fetch("positions"), "authorization_history" => state.authorization_history, "reserve_history" => state.reserve_history, @@ -620,6 +627,14 @@ def build_void_decision(state, command, target, effective_at) ) end + if closes_at != state.closes_at + events << Event.new( + type: "closing_time_changed", + visibility: :public, + data: common_data.merge("closes_at" => Timestamp.dump(closes_at)) + ) + end + events << Event.new( type: "notification_requested", visibility: :privileged, diff --git a/ruby/engine/test/bid_void_test.rb b/ruby/engine/test/bid_void_test.rb index 2ba598f..6b193ad 100644 --- a/ruby/engine/test/bid_void_test.rb +++ b/ruby/engine/test/bid_void_test.rb @@ -178,8 +178,283 @@ def test_bid_cannot_be_voided_at_or_after_close assert_equal "bidding_closed", decision.rejection.fetch("reason") end + def test_extend_on_void_defaults_to_false_and_matches_explicit_false_byte_for_byte + default_engine = soft_close_engine + explicit_false_engine = soft_close_engine(extend_on_void: false) + + assert_equal false, default_engine.configuration.extension.fetch("extend_on_void") + assert_equal false, explicit_false_engine.configuration.extension.fetch("extend_on_void") + + default_state = place(default_engine, default_engine.initial_state, "command-1", "bid-a", "bidder-a", 30_000, + "2026-09-01T12:10:00Z") + default_state = place(default_engine, default_state, "command-2", "bid-b", "bidder-b", 50_000, + "2026-09-01T12:11:00Z") + default_decision = void(default_engine, default_state, bid_id: "bid-b", effective_at: "2026-09-01T12:58:00Z") + default_after = default_engine.apply(default_state, default_decision.events) + + false_state = place(explicit_false_engine, explicit_false_engine.initial_state, "command-1", "bid-a", "bidder-a", 30_000, + "2026-09-01T12:10:00Z") + false_state = place(explicit_false_engine, false_state, "command-2", "bid-b", "bidder-b", 50_000, + "2026-09-01T12:11:00Z") + false_decision = void(explicit_false_engine, false_state, bid_id: "bid-b", effective_at: "2026-09-01T12:58:00Z") + false_after = explicit_false_engine.apply(false_state, false_decision.events) + + assert_equal CLOSES_AT, default_after.to_h.fetch("closes_at") + refute default_decision.events.any? { |candidate| candidate.type == "closing_time_changed" } + assert_equal default_decision.events.map(&:to_h), false_decision.events.map(&:to_h) + assert_equal default_after.to_h, false_after.to_h + end + + def test_extend_on_void_rejects_non_boolean_values + ["true", 1, nil, []].each do |invalid| + error = assert_raises(RBBB::InvalidConfiguration) do + RBBB::Configuration.new( + currency: "USD", + opening_minor_units: 10_000, + increments: [{from_minor_units: 0, amount_minor_units: 1_000}], + opens_at: OPENS_AT, + closes_at: CLOSES_AT, + extension: {trigger_window_seconds: 300, duration_seconds: 300, extend_on_void: invalid} + ) + end + assert_match(/extend_on_void must be a boolean/, error.message) + end + end + + def test_extend_on_void_extends_when_void_changes_leader_and_price_inside_window + engine = soft_close_engine(extend_on_void: true) + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 30_000, + "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "bid-b", "bidder-b", 50_000, + "2026-09-01T12:11:00Z") + + decision = void(engine, state, bid_id: "bid-b", effective_at: "2026-09-01T12:58:00.25Z") + recomputed = engine.apply(state, decision.events) + + assert_equal "bidder-a", recomputed.leader_id + assert_equal 10_000, recomputed.standing_minor_units + assert_equal "2026-09-01T13:03:00.25Z", recomputed.to_h.fetch("closes_at") + + closing_event = decision.events.find { |candidate| candidate.type == "closing_time_changed" } + refute_nil closing_event + assert closing_event.public? + assert_equal "2026-09-01T13:03:00.25Z", closing_event.data.fetch("closes_at") + assert_equal recomputed.version, closing_event.data.fetch("aggregate_version") + assert_equal "2026-09-01T13:03:00.25Z", event(decision, "bid_voided").fetch("closes_at") + end + + def test_extend_on_void_extends_when_void_changes_only_price_inside_window + engine = soft_close_engine(extend_on_void: true) + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 50_000, + "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "bid-b", "bidder-b", 30_000, + "2026-09-01T12:11:00Z") + + decision = void(engine, state, bid_id: "bid-b", effective_at: "2026-09-01T12:58:00Z") + recomputed = engine.apply(state, decision.events) + + assert_equal "bidder-a", recomputed.leader_id + assert_equal 10_000, recomputed.standing_minor_units + assert_equal "2026-09-01T13:03:00Z", recomputed.to_h.fetch("closes_at") + assert decision.events.any? { |candidate| candidate.public? && candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_extends_when_void_changes_only_leader_inside_window + engine = RBBB::Engine.new(RBBB::Configuration.new( + currency: "USD", + opening_minor_units: 1_000, + increments: [{from_minor_units: 0, amount_minor_units: 1_000}], + opens_at: OPENS_AT, + closes_at: CLOSES_AT, + extension: {trigger_window_seconds: 300, duration_seconds: 300, extend_on_void: true} + )) + state = place(engine, engine.initial_state, "command-1", "x-1", "bidder-x", 2_000, "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "y-1", "bidder-y", 2_000, "2026-09-01T12:11:00Z") + state = place(engine, state, "command-3", "x-2", "bidder-x", 3_000, "2026-09-01T12:12:00Z") + reduce_x = engine.decide(state, {command_id: "command-4", type: "reduce_maximum", bidder_id: "bidder-x", + maximum_minor_units: 2_000, effective_at: "2026-09-01T12:13:00Z"}) + state = engine.apply(state, reduce_x.events) + state = place(engine, state, "command-5", "y-2", "bidder-y", 4_500, "2026-09-01T12:14:00Z") + reduce_y = engine.decide(state, {command_id: "command-6", type: "reduce_maximum", bidder_id: "bidder-y", + maximum_minor_units: 2_600, effective_at: "2026-09-01T12:15:00Z"}) + state = engine.apply(state, reduce_y.events) + state = place(engine, state, "command-7", "y-3", "bidder-y", 2_700, "2026-09-01T12:16:00Z") + state = place(engine, state, "command-8", "w-1", "bidder-w", 4_500, "2026-09-01T12:17:00Z") + state = place(engine, state, "command-9", "y-4", "bidder-y", 4_000, "2026-09-01T12:18:00Z") + + assert_equal "bidder-w", state.leader_id + assert_equal 4_000, state.standing_minor_units + + decision = void(engine, state, bid_id: "x-2", effective_at: "2026-09-01T12:58:00Z") + recomputed = engine.apply(state, decision.events) + + assert_equal "bidder-y", recomputed.leader_id + assert_equal 4_000, recomputed.standing_minor_units + assert_equal "2026-09-01T13:03:00Z", recomputed.to_h.fetch("closes_at") + assert decision.events.any? { |candidate| candidate.public? && candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_extends_when_voiding_only_bid_inside_window + engine = soft_close_engine(extend_on_void: true) + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 50_000, + "2026-09-01T12:10:00Z") + + decision = void(engine, state, bid_id: "bid-a", effective_at: "2026-09-01T12:58:00Z") + recomputed = engine.apply(state, decision.events) + + assert_nil recomputed.leader_id + assert_nil recomputed.standing_minor_units + assert_equal "2026-09-01T13:03:00Z", recomputed.to_h.fetch("closes_at") + assert decision.events.any? { |candidate| candidate.public? && candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_does_not_extend_when_neither_leader_nor_price_changes + engine = soft_close_engine(extend_on_void: true) + + # Case 1: voiding a losing bid that did not set the standing price. + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 50_000, + "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "bid-b", "bidder-b", 20_000, + "2026-09-01T12:11:00Z") + state = place(engine, state, "command-3", "bid-c", "bidder-c", 30_000, + "2026-09-01T12:12:00Z") + + losing_void = void(engine, state, bid_id: "bid-b", effective_at: "2026-09-01T12:58:00Z") + after_losing = engine.apply(state, losing_void.events) + + assert_equal "bidder-a", after_losing.leader_id + assert_equal 31_000, after_losing.standing_minor_units + assert_equal CLOSES_AT, after_losing.to_h.fetch("closes_at") + refute losing_void.events.any? { |candidate| candidate.type == "closing_time_changed" } + + # Case 2: voiding the leader's unexecuted maximum increase. + raised = place(engine, after_losing, "command-4", "bid-a-2", "bidder-a", 80_000, + "2026-09-01T12:58:30Z") + raise_void = void(engine, raised, bid_id: "bid-a-2", effective_at: "2026-09-01T12:59:00Z") + after_raise_void = engine.apply(raised, raise_void.events) + + assert_equal "bidder-a", after_raise_void.leader_id + assert_equal 31_000, after_raise_void.standing_minor_units + assert_equal CLOSES_AT, after_raise_void.to_h.fetch("closes_at") + refute raise_void.events.any? { |candidate| candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_does_not_extend_outside_soft_close_window + engine = soft_close_engine(extend_on_void: true) + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 30_000, + "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "bid-b", "bidder-b", 50_000, + "2026-09-01T12:11:00Z") + + decision = void(engine, state, bid_id: "bid-b", effective_at: "2026-09-01T12:54:59.999Z") + recomputed = engine.apply(state, decision.events) + + assert_equal "bidder-a", recomputed.leader_id + assert_equal 10_000, recomputed.standing_minor_units + assert_equal CLOSES_AT, recomputed.to_h.fetch("closes_at") + refute decision.events.any? { |candidate| candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_never_shortens_closing_time + engine = soft_close_engine(trigger_window_seconds: 300, duration_seconds: 60, extend_on_void: true) + state = place(engine, engine.initial_state, "command-1", "bid-a", "bidder-a", 30_000, + "2026-09-01T12:10:00Z") + state = place(engine, state, "command-2", "bid-b", "bidder-b", 50_000, + "2026-09-01T12:11:00Z") + + early_in_window = void(engine, state, bid_id: "bid-b", effective_at: "2026-09-01T12:56:00Z") + after_early = engine.apply(state, early_in_window.events) + + assert_equal CLOSES_AT, after_early.to_h.fetch("closes_at") + refute early_in_window.events.any? { |candidate| candidate.type == "closing_time_changed" } + + late_in_window = void(engine, after_early, bid_id: "bid-a", effective_at: "2026-09-01T12:59:30Z") + after_late = engine.apply(after_early, late_in_window.events) + + assert_equal "2026-09-01T13:00:30Z", after_late.to_h.fetch("closes_at") + assert late_in_window.events.any? { |candidate| candidate.public? && candidate.type == "closing_time_changed" } + end + + def test_extend_on_void_replay_and_checkpoint_reproduce_exact_closing_time + config = RBBB::Configuration.from_h( + currency: "USD", + opening_minor_units: 10_000, + increments: [{from_minor_units: 0, amount_minor_units: 1_000}], + opens_at: OPENS_AT, + closes_at: CLOSES_AT, + extension: {trigger_window_seconds: 300, duration_seconds: 300, extend_on_void: true} + ) + engine = RBBB::Engine.new(config) + commands = [ + {command_id: "command-1", type: "place_bid", bid_id: "bid-a", bidder_id: "bidder-a", + maximum_minor_units: 30_000, effective_at: "2026-09-01T12:10:00Z"}, + {command_id: "command-2", type: "place_bid", bid_id: "bid-b", bidder_id: "bidder-b", + maximum_minor_units: 50_000, effective_at: "2026-09-01T12:11:00Z"}, + {command_id: "command-3", type: "void_bid", bid_id: "bid-b", operator_id: "operator-1", + reason: "bidder_entry_error", notification_policy: "affected", effective_at: "2026-09-01T12:58:00Z"}, + {command_id: "command-4", type: "place_bid", bid_id: "bid-c", bidder_id: "bidder-c", + maximum_minor_units: 40_000, effective_at: "2026-09-01T13:01:00Z"} + ] + + state = engine.initial_state + recorded_event_batches = [] + commands.each do |command| + decision = engine.decide(state, command) + assert decision.accepted?, decision.rejection.inspect + recorded_event_batches << decision.events + state = engine.apply(state, decision.events) + end + + assert_equal "2026-09-01T13:06:00Z", state.to_h.fetch("closes_at") + assert_equal "bidder-c", state.leader_id + assert_equal 31_000, state.standing_minor_units + + # Event-stream replay reproduces the exact same state and closing time. + replayed_from_events = recorded_event_batches.reduce(engine.initial_state) do |acc, batch| + engine.apply(acc, batch) + end + assert_equal state.to_h, replayed_from_events.to_h + + # Command-stream replay on a fresh engine reproduces the exact same closing time. + fresh_engine = RBBB::Engine.new(config) + replayed_from_commands = commands.reduce(fresh_engine.initial_state) do |acc, command| + decision = fresh_engine.decide(acc, command) + assert decision.accepted?, decision.rejection.inspect + fresh_engine.apply(acc, decision.events) + end + assert_equal state.to_h, replayed_from_commands.to_h + + # Checkpoint restore from the bid_voided transition event carries the void-extended close. + void_transition = recorded_event_batches.fetch(2).find { |candidate| candidate.type == "bid_voided" } + restored = fresh_engine.restore(void_transition) + assert_equal "2026-09-01T13:03:00Z", restored.to_h.fetch("closes_at") + + continued_decision = fresh_engine.decide(restored, commands.last) + assert continued_decision.accepted?, continued_decision.rejection.inspect + continued_state = fresh_engine.apply(restored, continued_decision.events) + assert_equal state.to_h, continued_state.to_h + end + private + def soft_close_engine(trigger_window_seconds: 300, duration_seconds: 300, reserve_minor_units: nil, + extend_on_void: :omit) + extension = { + trigger_window_seconds: trigger_window_seconds, + duration_seconds: duration_seconds + } + extension[:extend_on_void] = extend_on_void unless extend_on_void == :omit + RBBB::Engine.new(RBBB::Configuration.new( + currency: "USD", + opening_minor_units: 10_000, + reserve_minor_units: reserve_minor_units, + increments: [{from_minor_units: 0, amount_minor_units: 1_000}], + opens_at: OPENS_AT, + closes_at: CLOSES_AT, + extension: extension + )) + end + def competed_state state = place(@engine, @engine.initial_state, "command-1", "bid-a", "bidder-a", 50_000) place(@engine, state, "command-2", "bid-b", "bidder-b", 30_000) diff --git a/specification/configuration/timed-proxy.schema.json b/specification/configuration/timed-proxy.schema.json index d97d35b..0d65536 100644 --- a/specification/configuration/timed-proxy.schema.json +++ b/specification/configuration/timed-proxy.schema.json @@ -62,6 +62,11 @@ "duration_seconds": { "$ref": "../common/integer.schema.json#/$defs/positiveInteger", "description": "Seconds added to the triggering command time; bounded so closing-time arithmetic stays representable in every implementation." + }, + "extend_on_void": { + "type": "boolean", + "default": false, + "description": "When true, an operator bid void that changes the leader or standing amount inside the trigger window extends closing time like an accepted bid." } } }