Module: Legion::Extensions::Privatecore::Runners::Privatecore

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/privatecore/runners/privatecore.rb

Instance Method Summary collapse

Instance Method Details

#check_pii(text:, service_url: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 43

def check_pii(text:, service_url: nil, **)
  result = Helpers::Boundary.strip_pii(text, service_url: service_url)
  has_pii = !result[:detections].empty?
  log.debug "[privatecore] pii check: contains_pii=#{has_pii}"
  safe_detections = result[:detections].map { |d| d.except(:match) }
  {
    contains_pii: has_pii,
    stripped:     result[:cleaned],
    detections:   safe_detections
  }
end

#detect_probe(text:) ⇒ Object



55
56
57
58
59
60
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 55

def detect_probe(text:, **)
  probe = Helpers::Boundary.detect_probe(text)
  log.debug "[privatecore] probe check: detected=#{probe}"
  Legion::Events.emit('privatecore.probe_detected', text_length: text.is_a?(String) ? text.length : 0) if probe && defined?(Legion::Events)
  { probe_detected: probe }
end

#enforce_boundary(text:, direction: :outbound, mode: nil, service_url: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 11

def enforce_boundary(text:, direction: :outbound, mode: nil, service_url: nil, **)
  case direction
  when :outbound
    result = Helpers::Boundary.strip_pii(text, mode: mode, service_url: service_url)
    pii_found = !result[:detections].empty?
    text_length = text.is_a?(String) ? text.length : 0
    log.debug "[privatecore] boundary outbound: length=#{text_length} pii_found=#{pii_found}"
    log.warn '[privatecore] PII stripped from outbound text' if pii_found
    safe_detections = result[:detections].map { |d| d.except(:match) }
    {
      original_length: text_length,
      cleaned:         result[:cleaned],
      pii_found:       pii_found,
      direction:       direction,
      detections:      safe_detections,
      mapping:         result[:mapping],
      mapping_key:     result[:mapping_key]
    }
  when :inbound
    probe = Helpers::Boundary.detect_probe(text)
    action = probe ? :flag_and_log : :allow
    log.debug "[privatecore] boundary inbound: probe=#{probe} action=#{action}"
    log.warn '[privatecore] PROBE DETECTED in inbound text' if probe
    {
      text:      text,
      probe:     probe,
      direction: direction,
      action:    action
    }
  end
end

#erasure_auditObject



79
80
81
82
83
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 79

def erasure_audit(**)
  count = erasure_engine.audit_log.size
  log.debug "[privatecore] erasure audit: entries=#{count}"
  { audit_log: erasure_engine.audit_log, count: count }
end

#prune_audit_logObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 85

def prune_audit_log(**)
  audit = erasure_engine.audit_log
  cap = Helpers::Boundary::MAX_AUDIT_LOG_SIZE
  pruned = 0
  while audit.size > cap
    audit.shift
    pruned += 1
  end
  log.debug "[privatecore] audit prune: pruned=#{pruned} remaining=#{audit.size}"
  { pruned: pruned, remaining: audit.size }
end

#restore_text(text:, mapping: nil, mapping_key: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/extensions/privatecore/runners/privatecore.rb', line 62

def restore_text(text:, mapping: nil, mapping_key: nil, **)
  if mapping
    restored = Helpers::Redactor.restore(text: text, mapping: mapping)
    { restored: restored, success: true }
  elsif mapping_key
    retrieved = Helpers::Redactor.retrieve_mapping(key: mapping_key)
    if retrieved
      restored = Helpers::Redactor.restore(text: text, mapping: retrieved)
      { restored: restored, success: true }
    else
      { restored: nil, success: false, error: :mapping_not_found }
    end
  else
    { restored: nil, success: false, error: :no_mapping }
  end
end