Class: Braintrust::Contrib::Patcher
- Inherits:
-
Object
- Object
- Braintrust::Contrib::Patcher
- Defined in:
- lib/braintrust/contrib/patcher.rb
Overview
Base class for all patchers. Provides thread-safe, idempotent patching with error handling.
Direct Known Subclasses
Anthropic::BetaMessagesPatcher, Anthropic::MessagesPatcher, OpenAI::ChatPatcher, OpenAI::ModerationsPatcher, OpenAI::ResponsesPatcher, RubyLLM::ChatPatcher, RubyOpenAI::ChatPatcher, RubyOpenAI::ModerationsPatcher, RubyOpenAI::ResponsesPatcher
Class Method Summary collapse
-
.applicable? ⇒ Boolean
Override in subclasses to check if patcher should apply.
- .inherited(subclass) ⇒ Object
-
.patch!(**options) ⇒ Boolean
Apply the patch (thread-safe and idempotent).
-
.patched?(**options) ⇒ Boolean
Has this patcher already been applied?.
-
.perform_patch(**options) ⇒ void
Subclasses implement this to perform the actual patching.
-
.reset! ⇒ Object
Reset patched state (primarily for testing).
Class Method Details
.applicable? ⇒ Boolean
Override in subclasses to check if patcher should apply. Called after patcher loads but before perform_patch.
28 29 30 |
# File 'lib/braintrust/contrib/patcher.rb', line 28 def applicable? true # Default: always applicable end |
.inherited(subclass) ⇒ Object
14 15 16 |
# File 'lib/braintrust/contrib/patcher.rb', line 14 def self.inherited(subclass) subclass.instance_variable_set(:@patch_mutex, Mutex.new) end |
.patch!(**options) ⇒ Boolean
Apply the patch (thread-safe and idempotent).
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/braintrust/contrib/patcher.rb', line 37 def patch!(**) return false unless applicable? return true if patched?(**) # Fast path @patch_mutex.synchronize do unless applicable? Braintrust::Log.debug("Skipping #{name} - not applicable") return false end return true if patched?(**) # Double-check under lock perform_patch(**) @patched = true end Braintrust::Log.debug("Patched #{name}") true rescue => e Braintrust::Log.error("Failed to patch #{name}: #{e.}") false end |
.patched?(**options) ⇒ Boolean
Has this patcher already been applied?
21 22 23 |
# File 'lib/braintrust/contrib/patcher.rb', line 21 def patched?(**) @patched == true end |
.perform_patch(**options) ⇒ void
This method returns an undefined value.
Subclasses implement this to perform the actual patching. This method is called under lock after applicable? returns true.
65 66 67 |
# File 'lib/braintrust/contrib/patcher.rb', line 65 def perform_patch(**) raise NotImplementedError, "#{self} must implement perform_patch" end |
.reset! ⇒ Object
Reset patched state (primarily for testing).
70 71 72 |
# File 'lib/braintrust/contrib/patcher.rb', line 70 def reset! @patched = false end |