Module: Legion::Extensions::Apollo::Runners::Gas

Extended by:
Helpers::Lex, JSON::Helper, Logging::Helper
Includes:
Helpers::Lex, JSON::Helper, Logging::Helper
Defined in:
lib/legion/extensions/apollo/runners/gas.rb

Constant Summary collapse

RELATION_TYPES =
%w[
  similar_to contradicts depends_on causes
  part_of supersedes supports_by extends
].freeze
RELATE_CONFIDENCE_GATE =
0.7
SYNTHESIS_CONFIDENCE_CAP =
0.7
MAX_ANTICIPATIONS =
3

Class Method Summary collapse

Class Method Details

.build_synthesis_entry(item, facts) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 315

def build_synthesis_entry(item, facts)
  source_indices = item[:source_indices] || item['source_indices'] || []
  source_confs = source_indices.filter_map { |i| facts[i]&.dig(:confidence) }
  fb = fallback_confidence
  geo_mean = source_confs.empty? ? fb : geometric_mean(source_confs)

  {
    content:        item[:content] || item['content'],
    content_type:   (item[:content_type] || item['content_type'] || 'inference').to_sym,
    status:         :candidate,
    confidence:     [geo_mean, synthesis_confidence_cap].min,
    source_indices: source_indices
  }
end

.classify_relation(fact, entry) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 203

def classify_relation(fact, entry)
  fb_conf = fallback_confidence
  if llm_available?
    llm_classify_relation(fact, entry)
  else
    { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fb_conf }
  end
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.classify_relation')
  { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence }
end

.fallback_confidenceObject



34
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 34

def fallback_confidence     = Helpers::Confidence.apollo_setting(:gas, :fallback_confidence, default: 0.5)

.fallback_relation(fact, entry) ⇒ Object



265
266
267
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 265

def fallback_relation(fact, entry)
  { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence }
end

.fetch_similar_entries(facts) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 187

def fetch_similar_entries(facts)
  lim = similar_entries_limit
  min_conf = Helpers::GraphQuery.default_query_min_confidence
  entries = []
  facts.each do |fact|
    result = Runners::Knowledge.retrieve_relevant(query: fact[:content], limit: lim, min_confidence: min_conf)
    entries.concat(result[:entries]) if result[:success] && result[:entries]&.any?
  rescue StandardError => e
    handle_exception(e, level: :warn, operation: 'apollo.gas.fetch_similar_entries')
    next
  end
  unique = entries.uniq { |e| e[:id] }
  log.debug("GAS fetch_similar_entries facts=#{facts.size} entries=#{unique.size}")
  unique
end

.geometric_mean(values) ⇒ Object



330
331
332
333
334
335
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 330

def geometric_mean(values)
  return 0.0 if values.empty?

  product = values.reduce(1.0) { |acc, v| acc * v }
  product**(1.0 / values.length)
end

.json_load(str) ⇒ Object



26
27
28
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 26

def json_load(str)
  json_parse(str)
end

.llm_anticipate(facts) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 337

def llm_anticipate(facts)
  facts_text = facts.map { |f| "(#{f[:content_type]}) #{f[:content]}" }.join("\n")

  prompt = <<~PROMPT
    Given these knowledge entries, generate 1-3 likely follow-up questions a user might ask.

    Knowledge:
    #{facts_text}

    Return JSON with a "questions" array of question strings.
  PROMPT

  result = Legion::LLM::Pipeline::GaiaCaller.structured(
    message: prompt.strip,
    schema:  {
      type:       :object,
      properties: {
        questions: { type: :array, items: { type: :string } }
      },
      required:   ['questions']
    },
    phase:   'gas_anticipate'
  )

  content = result.respond_to?(:message) ? result.message[:content] : result.to_s
  parsed = json_load(content)
  questions = parsed.is_a?(Hash) ? (parsed[:questions] || parsed['questions'] || []) : []
  questions = questions.first(max_anticipations)

  questions.map do |q|
    promote_to_pattern_store(question: q, facts: facts)
    { question: q }
  end
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.llm_anticipate')
  []
end

.llm_available?Boolean

Returns:

  • (Boolean)


388
389
390
391
392
393
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 388

def llm_available?
  defined?(Legion::LLM::Pipeline::GaiaCaller)
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.llm_available')
  false
end

.llm_classify_relation(fact, entry) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 215

def llm_classify_relation(fact, entry)
  prompt = <<~PROMPT
    Classify the relationship between these two knowledge entries.
    Valid types: #{RELATION_TYPES.join(', ')}

    Entry A (new): #{fact[:content]}
    Entry B (existing): #{entry[:content]}

    Return JSON with relation_type and confidence (0.0-1.0).
  PROMPT

  result = Legion::LLM::Pipeline::GaiaCaller.structured(
    message: prompt.strip,
    schema:  {
      type:       :object,
      properties: {
        relations: {
          type:  :array,
          items: {
            type:       :object,
            properties: {
              relation_type: { type: :string },
              confidence:    { type: :number }
            },
            required:   %w[relation_type confidence]
          }
        }
      },
      required:   ['relations']
    },
    phase:   'gas_relate'
  )

  content = result.respond_to?(:message) ? result.message[:content] : result.to_s
  parsed = json_load(content)
  rels = parsed.is_a?(Hash) ? (parsed[:relations] || parsed['relations'] || []) : []
  best = rels.max_by { |r| r[:confidence] || r['confidence'] || 0 }

  return fallback_relation(fact, entry) unless best

  conf = best[:confidence] || best['confidence'] || 0
  rtype = best[:relation_type] || best['relation_type']
  return fallback_relation(fact, entry) if conf < relate_confidence_gate || !RELATION_TYPES.include?(rtype)

  { from_content: fact[:content], to_id: entry[:id], relation_type: rtype, confidence: conf }
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.llm_classify_relation')
  fallback_relation(fact, entry)
end

.llm_comprehend(messages, response) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 399

def llm_comprehend(messages, response)
  prompt = <<~PROMPT
    Extract distinct facts from this exchange. Return JSON array of {content:, content_type:} where content_type is one of: fact, concept, procedure, association.

    User: #{messages.last&.dig(:content)}
    Assistant: #{response}
  PROMPT

  result = Legion::LLM::Pipeline::GaiaCaller.structured(
    message: prompt.strip,
    schema:  {
      type:       :object,
      properties: {
        facts: {
          type:  :array,
          items: {
            type:       :object,
            properties: {
              content:      { type: :string },
              content_type: { type: :string }
            },
            required:   %w[content content_type]
          }
        }
      },
      required:   ['facts']
    },
    phase:   'gas_comprehend'
  )

  content = result.respond_to?(:message) ? result.message[:content] : result.to_s
  parsed = json_load(content)
  facts_array = parsed.is_a?(Hash) ? (parsed[:facts] || parsed['facts'] || []) : Array(parsed)
  facts_array.map do |f|
    {
      content:      f[:content] || f['content'],
      content_type: (f[:content_type] || f['content_type'] || 'fact').to_sym
    }
  end
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.llm_comprehend')
  mechanical_comprehend(messages, response)
end

.llm_synthesize(facts) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 269

def llm_synthesize(facts)
  facts_text = facts.each_with_index.map { |f, i| "[#{i}] (#{f[:content_type]}) #{f[:content]}" }.join("\n")

  prompt = <<~PROMPT
    Given these knowledge entries, generate derivative insights (inferences, implications, or connections).
    Each synthesis should combine information from multiple sources.

    Entries:
    #{facts_text}

    Return JSON with a "synthesis" array where each item has: content (string), content_type (inference/implication/connection), source_indices (array of entry indices used).
  PROMPT

  result = Legion::LLM::Pipeline::GaiaCaller.structured(
    message: prompt.strip,
    schema:  {
      type:       :object,
      properties: {
        synthesis: {
          type:  :array,
          items: {
            type:       :object,
            properties: {
              content:        { type: :string },
              content_type:   { type: :string },
              source_indices: { type: :array, items: { type: :integer } }
            },
            required:   %w[content content_type source_indices]
          }
        }
      },
      required:   ['synthesis']
    },
    phase:   'gas_synthesize'
  )

  content = result.respond_to?(:message) ? result.message[:content] : result.to_s
  parsed = json_load(content)
  items = parsed.is_a?(Hash) ? (parsed[:synthesis] || parsed['synthesis'] || []) : []

  items.map { |item| build_synthesis_entry(item, facts) }
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.llm_synthesize')
  []
end

.max_anticipationsObject



32
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 32

def max_anticipations       = Helpers::Confidence.apollo_setting(:gas, :max_anticipations, default: MAX_ANTICIPATIONS)

.mechanical_comprehend(_messages, response) ⇒ Object



395
396
397
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 395

def mechanical_comprehend(_messages, response)
  [{ content: response, content_type: :observation }]
end

.phase_anticipate(facts, _synthesis) ⇒ Object

Phase 6: Anticipate - pre-cache likely follow-up questions



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 169

def phase_anticipate(facts, _synthesis)
  if facts.empty?
    log.debug('GAS phase_anticipate skipped reason=no_facts')
    return []
  end
  unless llm_available?
    log.debug('GAS phase_anticipate skipped reason=llm_unavailable')
    return []
  end

  anticipations = llm_anticipate(facts)
  log.debug("GAS phase_anticipate anticipations=#{anticipations.size}")
  anticipations
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.phase_anticipate')
  []
end

.phase_comprehend(audit_event) ⇒ Object

Phase 1: Comprehend - extract typed facts from the exchange



72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 72

def phase_comprehend(audit_event)
  messages = audit_event[:messages]
  response = audit_event[:response_content]

  mode = llm_available? ? :llm : :mechanical
  log.debug("GAS phase_comprehend mode=#{mode} messages=#{Array(messages).size} response_length=#{response.to_s.length}")
  facts = mode == :llm ? llm_comprehend(messages, response) : mechanical_comprehend(messages, response)
  log.debug("GAS phase_comprehend facts=#{facts.size}")
  facts
end

.phase_deposit(facts, _entities, _relations, _synthesis, audit_event) ⇒ Object

Phase 5: Deposit - atomic write to Apollo



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 143

def phase_deposit(facts, _entities, _relations, _synthesis, audit_event)
  unless defined?(Runners::Knowledge)
    log.debug('GAS phase_deposit skipped reason=knowledge_runner_unavailable')
    return { deposited: 0 }
  end

  deposited = 0
  facts.each do |fact|
    Runners::Knowledge.handle_ingest(
      content:          fact[:content],
      content_type:     fact[:content_type].to_s,
      tags:             %w[gas auto_extracted],
      source_agent:     'gas_pipeline',
      source_provider:  audit_event.dig(:routing, :provider)&.to_s,
      knowledge_domain: 'general',
      context:          { source_request_id: audit_event[:request_id] }
    )
    deposited += 1
  rescue StandardError => e
    handle_exception(e, level: :warn, operation: 'apollo.gas.phase_deposit_fact')
  end
  log.info("GAS phase_deposit deposited=#{deposited} facts=#{facts.size}")
  { deposited: deposited }
end

.phase_extract(audit_event, _facts) ⇒ Object

Phase 2: Extract - entity extraction (delegates to existing EntityExtractor)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 84

def phase_extract(audit_event, _facts)
  unless defined?(Runners::EntityExtractor)
    log.debug('GAS phase_extract skipped reason=entity_extractor_unavailable')
    return []
  end

  result = Runners::EntityExtractor.extract_entities(text: audit_event[:response_content])
  entities = result[:success] ? (result[:entities] || []) : []
  log.debug("GAS phase_extract success=#{result[:success]} entities=#{entities.size}")
  entities
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.phase_extract')
  []
end

.phase_relate(facts, _entities) ⇒ Object

Phase 3: Relate - classify relationships between new and existing entries



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 100

def phase_relate(facts, _entities)
  unless defined?(Runners::Knowledge)
    log.debug('GAS phase_relate skipped reason=knowledge_runner_unavailable')
    return []
  end

  existing = fetch_similar_entries(facts)
  if existing.empty?
    log.debug("GAS phase_relate skipped reason=no_existing_entries facts=#{facts.size}")
    return []
  end

  relations = []
  facts.each do |fact|
    existing.each do |entry|
      relation = classify_relation(fact, entry)
      relations << relation if relation
    end
  end
  log.debug("GAS phase_relate facts=#{facts.size} existing=#{existing.size} relations=#{relations.size}")
  relations
end

.phase_synthesize(facts, _relations) ⇒ Object

Phase 4: Synthesize - generate derivative knowledge



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 124

def phase_synthesize(facts, _relations)
  if facts.length < 2
    log.debug("GAS phase_synthesize skipped reason=insufficient_facts facts=#{facts.length}")
    return []
  end
  unless llm_available?
    log.debug('GAS phase_synthesize skipped reason=llm_unavailable')
    return []
  end

  synthesis = llm_synthesize(facts)
  log.debug("GAS phase_synthesize synthesis=#{synthesis.size}")
  synthesis
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.phase_synthesize')
  []
end

.process(audit_event) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 36

def process(audit_event)
  unless processable?(audit_event)
    log.debug('GAS process skipped reason=no_content')
    return { phases_completed: 0, reason: 'no content' }
  end

  log.debug("GAS process start request_id=#{audit_event[:request_id] || 'nil'} messages=#{Array(audit_event[:messages]).size} response_length=#{audit_event[:response_content].to_s.length}") # rubocop:disable Layout/LineLength

  facts = phase_comprehend(audit_event)
  entities = phase_extract(audit_event, facts)
  relations = phase_relate(facts, entities)
  synthesis = phase_synthesize(facts, relations)
  deposit_result = phase_deposit(facts, entities, relations, synthesis, audit_event)
  anticipations = phase_anticipate(facts, synthesis)

  result = {
    phases_completed: 6,
    facts:            facts.length,
    entities:         entities.length,
    relations:        relations.length,
    synthesis:        synthesis.length,
    deposited:        deposit_result,
    anticipations:    anticipations.length
  }
  log.info("GAS process complete facts=#{result[:facts]} entities=#{result[:entities]} relations=#{result[:relations]} synthesis=#{result[:synthesis]} anticipations=#{result[:anticipations]}") # rubocop:disable Layout/LineLength
  result
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'apollo.gas.process')
  { phases_completed: 0, error: e.message }
end

.processable?(event) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 67

def processable?(event)
  event[:messages]&.any? == true && !event[:response_content].nil?
end

.promote_to_pattern_store(question:, facts:) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 375

def promote_to_pattern_store(question:, facts:)
  return unless defined?(Legion::Extensions::Agentic::TBI::PatternStore)

  Legion::Extensions::Agentic::TBI::PatternStore.promote_candidate(
    intent:     question,
    resolution: { source: 'gas_anticipate', facts: facts.map { |f| f[:content] } },
    confidence: fallback_confidence
  )
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'apollo.gas.promote_to_pattern_store')
  nil
end

.relate_confidence_gateObject



30
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 30

def relate_confidence_gate = Helpers::Confidence.apollo_setting(:gas, :relate_confidence_gate, default: RELATE_CONFIDENCE_GATE)

.similar_entries_limitObject



33
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 33

def similar_entries_limit   = Helpers::Confidence.apollo_setting(:gas, :similar_entries_limit, default: 3)

.synthesis_confidence_capObject



31
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 31

def synthesis_confidence_cap = Helpers::Confidence.apollo_setting(:gas, :synthesis_confidence_cap, default: SYNTHESIS_CONFIDENCE_CAP)