Class: PromptSanitizer::SyntheticEngine

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

Overview

Generates realistic fake replacement values per EntityType.

When the faker gem is installed, each call produces a contextually appropriate fake (names, emails, IPs, …). Without faker the engine falls back to sequential placeholder tokens: [EMAIL_1], [PERSON_2], etc.

Determinism within a session is guaranteed by the Vault: the same original value always receives the same token/fake because Session#anonymize only calls generate once per unique original.

Usage:

engine = SyntheticEngine.new(locale: "en_US")
engine.generate(:email, "[email protected]")   # => "[email protected]"
engine.generate(:person, "Alice Smith")    # => "Carlos Rivera"

Constant Summary collapse

CHARS_ALPHA_NUM =
("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a

Instance Method Summary collapse

Constructor Details

#initialize(locale: "en") ⇒ SyntheticEngine

Returns a new instance of SyntheticEngine.

Parameters:

  • locale (String) (defaults to: "en")

    BCP-47 locale tag forwarded to Faker (e.g. "en", "fr", "de")



31
32
33
34
35
36
37
# File 'lib/prompt_sanitizer/synthetic.rb', line 31

def initialize(locale: "en")
  @locale   = locale
  @counters = Hash.new(0) # entity_type Symbol → Integer
  if HAS_FAKER
    Faker::Config.locale = locale
  end
end

Instance Method Details

#generate(entity_type, _original = "") ⇒ String

Returns a fake replacement string for entity_type.

Parameters:

  • entity_type (Symbol)

    one of the EntityType constants

  • _original (String) (defaults to: "")

    original text (unused here; determinism via Vault)

Returns:

  • (String)


44
45
46
47
48
49
50
# File 'lib/prompt_sanitizer/synthetic.rb', line 44

def generate(entity_type, _original = "")
  if HAS_FAKER
    faker_value(entity_type)
  else
    placeholder(entity_type)
  end
end

#placeholder(entity_type) ⇒ Object

Force a placeholder token regardless of faker availability. Used by Session when the replacement must survive round-trips.



54
55
56
57
# File 'lib/prompt_sanitizer/synthetic.rb', line 54

def placeholder(entity_type)
  @counters[entity_type] += 1
  "[#{entity_type.to_s.upcase}_#{@counters[entity_type]}]"
end

#reset!Object



59
60
61
# File 'lib/prompt_sanitizer/synthetic.rb', line 59

def reset!
  @counters.clear
end