fix(captain-memory): cap ExtractionService input, validate scope, filter failed msgs

This commit is contained in:
Rodribm10 2026-04-19 00:24:09 -03:00
parent 9d593757df
commit aec796ebfd
3 changed files with 68 additions and 16 deletions

View File

@ -825,11 +825,11 @@ class Captain::ContactMemories::ExtractionService
private
def call_llm
client = RubyLLM.chat(model: EXTRACTION_MODEL)
client.with_temperature(0)
client.with_response_format({ type: 'json_object' })
response = client.ask(build_prompt)
response.content.to_s
RubyLLM.chat(model: EXTRACTION_MODEL)
.with_temperature(0)
.with_params(response_format: { type: 'json_object' })
.ask(build_prompt)
.content.to_s
end
def build_prompt

View File

@ -2,6 +2,8 @@ class Captain::ContactMemories::ExtractionService
MAX_FACTS = 5
MIN_CONFIDENCE = 0.5
EXTRACTION_MODEL = 'gpt-4o-mini'.freeze
MAX_CHARS = 40_000 # matches Captain::Llm::ConversationInsightService convention
SCOPE_PATTERN = /\A(global|unit:\d+)\z/
def initialize(conversation:)
@conversation = conversation
@ -22,12 +24,14 @@ class Captain::ContactMemories::ExtractionService
private
# TODO(phase-6): add Integrations::LlmInstrumentation wrap for OTEL metrics
# (extraction_count, extraction_cost, facts_per_call, llm_error_rate).
def call_llm
response = RubyLLM.chat(model: EXTRACTION_MODEL)
.with_temperature(0)
.with_params(response_format: { type: 'json_object' })
.ask(build_prompt)
response.content.to_s
RubyLLM.chat(model: EXTRACTION_MODEL)
.with_temperature(0)
.with_params(response_format: { type: 'json_object' })
.ask(build_prompt)
.content.to_s
end
def build_prompt
@ -57,12 +61,33 @@ class Captain::ContactMemories::ExtractionService
PROMPT
end
# Feeds the LLM extractor. MUST exclude:
# - private: true (internal agent-to-agent notes — never seen by the guest; privacy leak if extracted)
# - failed status (outbound messages that never reached the guest — extracting from them is dishonest)
def formatted_messages
@conversation.messages
.where(message_type: [:incoming, :outgoing], private: false)
.order(created_at: :asc)
.map { |m| "[#{m.message_type}] #{m.content}" }
.join("\n")
scope = @conversation.messages
.where(message_type: [:incoming, :outgoing], private: false)
.where.not(status: :failed)
.order(created_at: :asc)
limited_messages(scope).map { |m| "[#{m.message_type}] #{m.content}" }.join("\n")
end
def limited_messages(scope)
all = scope.to_a
return all if all.sum { |m| m.content.to_s.length } <= MAX_CHARS
# keep most recent messages, drop oldest until under cap
kept = []
total = 0
all.reverse_each do |msg|
len = msg.content.to_s.length
break if total + len > MAX_CHARS
kept.unshift(msg)
total += len
end
kept
end
def normalize(raw_fact)
@ -70,7 +95,8 @@ class Captain::ContactMemories::ExtractionService
content = raw_fact['content'].to_s.strip
evidence = raw_fact['evidence'].to_s.strip
confidence = raw_fact['confidence'].to_f
scope = raw_fact['scope'].to_s.presence || 'global'
raw_scope = raw_fact['scope'].to_s.presence || 'global'
scope = valid_scope?(raw_scope) ? raw_scope : 'global'
return nil unless Captain::ContactMemory::MEMORY_TYPES.include?(type)
return nil if content.blank? || evidence.blank?
@ -84,4 +110,8 @@ class Captain::ContactMemories::ExtractionService
scope: scope
}
end
def valid_scope?(value)
SCOPE_PATTERN.match?(value)
end
end

View File

@ -77,6 +77,28 @@ RSpec.describe Captain::ContactMemories::ExtractionService do
allow_any_instance_of(described_class).to receive(:call_llm).and_raise(StandardError)
expect(described_class.new(conversation: conversation).call).to eq([])
end
it 'caps conversation history at MAX_CHARS, keeping most recent messages' do
# Create a message long enough that multiple would exceed MAX_CHARS
long_content = 'a' * 20_000
3.times { create(:message, conversation: conversation, message_type: :incoming, content: long_content) }
# Stub call_llm to capture the prompt passed — but since we stub call_llm entirely,
# we can only indirectly verify: assert the service still returns without error.
allow_any_instance_of(described_class).to receive(:call_llm).and_return(llm_response)
expect(described_class.new(conversation: conversation).call.size).to eq(2)
end
it 'falls back to global scope when LLM returns invalid scope' do
bad_response = {
'facts' => [
{ 'memory_type' => 'preferencia', 'content' => 'x', 'evidence' => 'y', 'confidence' => 0.9, 'scope' => 'unit:foo' }
]
}.to_json
allow_any_instance_of(described_class).to receive(:call_llm).and_return(bad_response)
result = described_class.new(conversation: conversation).call
expect(result.first[:scope]).to eq('global')
end
end
end
# rubocop:enable RSpec/AnyInstance