Class: Vangrail::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail.rb

Overview

Reads the environment into an engine. A class rather than a method so each decision is separable and testable on its own.

Constant Summary collapse

DEFAULT_RAILS =
%i[input context output].freeze
ALL_RAILS =
%i[input context output grounding secrets patterns links multiturn privacy
markup budget].freeze
INJECTION_PATTERNS =

Deterministic input patterns, kept small on purpose. Each is a phrase whose presence is itself the violation; anything needing judgement belongs in a policy rail, where a false positive is a model's opinion rather than a hard rule.

{
  'instruction_override' => /\bignore\s+(?:all\s+|any\s+)?(?:previous|prior|above|earlier)\s+instructions?\b/i,
  'prompt_disclosure' => /\b(?:reveal|print|repeat|show|output)\s+(?:me\s+)?(?:your|the|its)?\s*
                          (?:system\s+prompt|initial\s+instructions|developer\s+message)\b/xi,
  'role_reset' => /\byou\s+are\s+now\s+(?:a|an|in)\b.{0,40}\b(?:mode|persona|dan|jailbreak)\b/i
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV) ⇒ Builder

Returns a new instance of Builder.



126
127
128
# File 'lib/vangrail.rb', line 126

def initialize(env = ENV)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



124
125
126
# File 'lib/vangrail.rb', line 124

def env
  @env
end

Instance Method Details

#cache?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/vangrail.rb', line 159

def cache?
  !off_value?(env['GUARDRAILS_CACHE'])
end

#config_dirObject



163
164
165
# File 'lib/vangrail.rb', line 163

def config_dir
  present(env['GUARDRAILS_CONFIG'])
end

#enabledObject



138
139
140
141
142
143
144
145
# File 'lib/vangrail.rb', line 138

def enabled
  text = env['GUARDRAILS_RAILS'].to_s.strip
  return DEFAULT_RAILS if text.empty?
  return [] if off_value?(text) || text.casecmp('none').zero?
  return ALL_RAILS.dup if text.casecmp('all').zero?

  text.split(/[,\s]+/).map { |s| s.strip.downcase.to_sym } & ALL_RAILS
end

#engineObject



130
131
132
133
134
135
136
# File 'lib/vangrail.rb', line 130

def engine
  return Engine.new(on_error: on_error, cache: cache?) if off?
  return config_engine if config_dir

  Engine.new(input: input_rails, context: context_rails, output: output_rails,
             on_error: on_error, cache: cache?)
end

#off?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/vangrail.rb', line 151

def off?
  off_value?(env['GUARDRAILS'])
end

#on?(rail) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/vangrail.rb', line 147

def on?(rail)
  enabled.include?(rail)
end

#on_errorObject



155
156
157
# File 'lib/vangrail.rb', line 155

def on_error
  env['GUARDRAILS_ON_ERROR'].to_s.strip.casecmp('block').zero? ? :block : :allow
end

#providerObject

The endpoint the model-backed rails call, or nil when none is reachable.



172
173
174
175
176
# File 'lib/vangrail.rb', line 172

def provider
  return @provider if defined?(@provider)

  @provider = Provider.resolve(env)
end

#server_urlObject



167
168
169
# File 'lib/vangrail.rb', line 167

def server_url
  present(env['GUARDRAILS_SERVER'])
end