26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/legion/extensions/privatecore/runners/embedding_guard.rb', line 26
def check_embedding_similarity(input:, threshold: nil, patterns: nil, **)
effective_threshold = resolve_threshold(threshold)
effective_patterns = patterns || DEFAULT_ADVERSARIAL_PATTERNS
unless defined?(Legion::LLM)
log.debug '[privatecore] embedding guard: Legion::LLM unavailable, skipping'
return { safe: true, max_similarity: 0.0, matched_pattern: nil, details: [], skipped: true }
end
input_vec = embed(input)
if input_vec.nil?
log.warn '[privatecore] embedding guard: failed to embed input'
return { safe: true, max_similarity: 0.0, matched_pattern: nil, details: [], error: :embed_failed }
end
pattern_vecs = cache_pattern_embeddings(patterns: effective_patterns)
details = compute_similarities(input_vec, effective_patterns, pattern_vecs)
max_entry = details.max_by { |d| d[:similarity] }
max_sim = max_entry ? max_entry[:similarity] : 0.0
matched = max_sim >= effective_threshold ? max_entry[:pattern] : nil
safe = matched.nil?
log.debug "[privatecore] embedding guard: max_similarity=#{max_sim.round(4)} threshold=#{effective_threshold} safe=#{safe}"
log.warn "[privatecore] ADVERSARIAL INPUT DETECTED via embedding: #{matched}" unless safe
{ safe: safe, max_similarity: max_sim, matched_pattern: matched, details: details }
end
|