Class: Riffer::Guardrails::Tripwire

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/guardrails/tripwire.rb

Overview

Captures information about a blocked guardrail execution.

When a guardrail blocks execution, a Tripwire is created to record the reason, which guardrail triggered it, and which phase it occurred in.

tripwire = Tripwire.new(
  reason: "PII detected in input",
  guardrail: PiiRedactor,
  phase: :before,
  metadata: { detected_types: [:email, :phone] }
)

Constant Summary collapse

PHASES =

: Array

Riffer::Guardrails::PHASES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reason:, guardrail:, phase:, metadata: nil) ⇒ Tripwire

Creates a new tripwire.

reason

the reason for blocking.

guardrail

the guardrail class that blocked.

phase

:before or :after.

metadata

optional additional information.

Raises Riffer::ArgumentError if the phase is invalid.

– : (reason: String, guardrail: singleton(Riffer::Guardrail), phase: Symbol, ?metadata: Hash[Symbol, untyped]?) -> void



41
42
43
44
45
46
47
48
# File 'lib/riffer/guardrails/tripwire.rb', line 41

def initialize(reason:, guardrail:, phase:, metadata: nil)
  raise Riffer::ArgumentError, "Invalid phase: #{phase}" unless PHASES.include?(phase)

  @reason = reason
  @guardrail = guardrail
  @phase = phase
  @metadata = 
end

Instance Attribute Details

#guardrailObject (readonly)

The guardrail class that triggered the block.



22
23
24
# File 'lib/riffer/guardrails/tripwire.rb', line 22

def guardrail
  @guardrail
end

#metadataObject (readonly)

Optional metadata about the block.



28
29
30
# File 'lib/riffer/guardrails/tripwire.rb', line 28

def 
  @metadata
end

#phaseObject (readonly)

The phase when the block occurred (:before or :after).



25
26
27
# File 'lib/riffer/guardrails/tripwire.rb', line 25

def phase
  @phase
end

#reasonObject (readonly)

The reason for blocking.



19
20
21
# File 'lib/riffer/guardrails/tripwire.rb', line 19

def reason
  @reason
end

Instance Method Details

#to_hObject

Converts the tripwire to a hash.

– : () -> Hash[Symbol, untyped]



54
55
56
57
58
59
60
61
# File 'lib/riffer/guardrails/tripwire.rb', line 54

def to_h
  {
    reason: reason,
    guardrail: guardrail.name,
    phase: phase,
    metadata: 
  }
end