lex-apollo 0.4.30 → 0.4.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -0
  3. data/README.md +113 -18
  4. data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
  5. data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
  6. data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
  7. data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
  8. data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
  9. data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
  10. data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
  11. data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
  12. data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
  13. data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
  14. data/lib/legion/extensions/apollo/api.rb +116 -51
  15. data/lib/legion/extensions/apollo/client.rb +20 -9
  16. data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
  17. data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
  18. data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
  19. data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
  20. data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
  21. data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
  22. data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
  23. data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
  24. data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
  25. data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
  26. data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
  27. data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
  28. data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
  29. data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
  30. data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
  31. data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
  32. data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
  33. data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
  34. data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
  35. data/lib/legion/extensions/apollo/runners/request.rb +51 -47
  36. data/lib/legion/extensions/apollo/runners/search.rb +88 -0
  37. data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
  38. data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
  39. data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
  40. data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
  41. data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
  42. data/lib/legion/extensions/apollo/transport.rb +20 -0
  43. data/lib/legion/extensions/apollo/version.rb +1 -1
  44. data/lib/legion/extensions/apollo.rb +26 -24
  45. data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
  46. data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
  47. data/spec/legion/extensions/apollo/api_spec.rb +250 -28
  48. data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
  49. data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
  50. data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
  51. data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
  52. data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
  53. data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
  54. data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
  55. data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
  56. data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
  57. data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
  58. data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
  59. data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
  60. data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
  61. data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
  62. data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
  63. data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
  64. data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
  65. data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
  66. data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
  67. data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
  68. data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
  69. data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
  70. data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
  71. data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
  72. data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
  73. data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
  74. data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
  75. data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
  76. data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
  77. data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
  78. data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
  79. data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
  80. data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
  81. data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
  82. data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
  83. data/spec/spec_helper.rb +13 -15
  84. metadata +71 -13
  85. data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
  86. data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
  87. data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
  88. data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
  89. data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
  90. data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
  91. data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
  92. data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
@@ -1,124 +1,57 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
-
5
- require 'legion/extensions/apollo/helpers/confidence'
6
- require 'legion/extensions/apollo/helpers/similarity'
7
- require 'legion/extensions/apollo/helpers/graph_query'
8
4
  require 'legion/extensions/apollo/runners/entity_extractor'
9
5
 
10
6
  RSpec.describe Legion::Extensions::Apollo::Runners::EntityExtractor do
11
- let(:runner) do
12
- obj = Object.new
13
- obj.extend(described_class)
14
- obj
15
- end
7
+ describe '.extract_entities' do
8
+ it 'returns an explicit unavailable result when the LLM is not started' do
9
+ allow(Legion::LLM).to receive(:started?).and_return(false)
16
10
 
17
- describe '#extract_entities' do
18
- context 'when Legion::LLM is not available' do
19
- before { hide_const('Legion::LLM') if defined?(Legion::LLM) }
11
+ result = described_class.extract_entities(text: 'Jane works on lex-synapse')
20
12
 
21
- it 'returns an empty entity list' do
22
- result = runner.extract_entities(text: 'Jane works on lex-synapse')
23
- expect(result[:success]).to be true
24
- expect(result[:entities]).to eq([])
25
- expect(result[:source]).to eq(:unavailable)
26
- end
13
+ expect(result).to include(success: true, entities: [], source: :unavailable)
27
14
  end
28
15
 
29
- context 'when Legion::LLM is available' do
30
- let(:llm_result) do
31
- {
32
- data: {
33
- entities: [
34
- { name: 'lex-synapse', type: 'repository', confidence: 0.9 },
35
- { name: 'Jane Doe', type: 'person', confidence: 0.8 }
36
- ]
37
- }
38
- }
39
- end
40
-
41
- before do
42
- stub_const('Legion::LLM', Module.new do
43
- def self.started? = true
44
-
45
- def self.structured(**) = { data: { entities: [] } }
46
- end)
47
- allow(Legion::LLM).to receive(:structured).and_return(llm_result)
48
- end
49
-
50
- it 'returns extracted entities' do
51
- result = runner.extract_entities(text: 'Jane works on lex-synapse')
52
- expect(result[:success]).to be true
53
- expect(result[:entities].size).to eq(2)
54
- expect(result[:source]).to eq(:llm)
55
- end
16
+ it 'returns filtered entities from the structured LLM boundary' do
17
+ allow(Legion::LLM).to receive(:started?).and_return(true)
18
+ allow(Legion::LLM).to receive(:structured).and_return(
19
+ data: { entities: [
20
+ { name: 'lex-synapse', type: 'repository', confidence: 0.9 },
21
+ { name: 'Jane Doe', type: 'person', confidence: 0.8 },
22
+ { name: 'low', type: 'person', confidence: 0.2 }
23
+ ] }
24
+ )
56
25
 
57
- it 'filters to configured entity types' do
58
- result = runner.extract_entities(
59
- text: 'Jane works on lex-synapse',
60
- entity_types: ['repository']
61
- )
62
- expect(result[:entities].all? { |e| e[:type] == 'repository' }).to be true
63
- end
26
+ result = described_class.extract_entities(
27
+ text: 'Jane works on lex-synapse', entity_types: ['repository'], min_confidence: 0.85
28
+ )
64
29
 
65
- it 'applies minimum confidence filter' do
66
- result = runner.extract_entities(
67
- text: 'Jane works on lex-synapse',
68
- min_confidence: 0.85
69
- )
70
- expect(result[:entities].size).to eq(1)
71
- expect(result[:entities].first[:name]).to eq('lex-synapse')
72
- end
30
+ expect(result).to include(success: true, source: :llm)
31
+ expect(result[:entities]).to eq([{ name: 'lex-synapse', type: 'repository', confidence: 0.9 }])
73
32
  end
74
33
 
75
- context 'when LLM raises' do
76
- before do
77
- stub_const('Legion::LLM', Module.new do
78
- def self.started? = true
34
+ it 'returns a typed failure when the LLM boundary fails' do
35
+ allow(Legion::LLM).to receive(:started?).and_return(true)
36
+ allow(Legion::LLM).to receive(:structured).and_raise(StandardError, 'provider timeout')
79
37
 
80
- def self.structured(**) = raise(StandardError, 'timeout')
81
- end)
82
- end
38
+ result = described_class.extract_entities(text: 'anything')
83
39
 
84
- it 'returns success false with error message' do
85
- result = runner.extract_entities(text: 'anything')
86
- expect(result[:success]).to be false
87
- expect(result[:error]).to include('timeout')
88
- end
40
+ expect(result).to include(success: false, entities: [], source: :error)
41
+ expect(result[:error]).to include('provider timeout')
89
42
  end
90
43
 
91
- context 'with empty text' do
92
- it 'returns early with empty list' do
93
- result = runner.extract_entities(text: '')
94
- expect(result[:success]).to be true
95
- expect(result[:entities]).to eq([])
96
- end
44
+ it 'propagates programming errors instead of converting them into extraction failures' do
45
+ allow(Legion::LLM).to receive(:started?).and_return(true)
46
+ allow(Legion::LLM).to receive(:structured).and_raise(ArgumentError, 'bad schema')
97
47
 
98
- it 'handles nil text' do
99
- result = runner.extract_entities(text: nil)
100
- expect(result[:success]).to be true
101
- expect(result[:entities]).to eq([])
102
- end
48
+ expect { described_class.extract_entities(text: 'anything') }.to raise_error(ArgumentError, 'bad schema')
103
49
  end
104
- end
105
50
 
106
- describe '#entity_extraction_prompt' do
107
- it 'returns a non-empty string' do
108
- prompt = runner.entity_extraction_prompt(
109
- text: 'test text', entity_types: %w[person service]
110
- )
111
- expect(prompt).to be_a(String)
112
- expect(prompt).to include('person')
113
- expect(prompt).to include('service')
114
- end
115
- end
51
+ it 'returns an empty result for blank text' do
52
+ result = described_class.extract_entities(text: '')
116
53
 
117
- describe '#entity_schema' do
118
- it 'returns a JSON Schema hash' do
119
- schema = runner.entity_schema
120
- expect(schema[:type]).to eq('object')
121
- expect(schema[:properties]).to have_key(:entities)
54
+ expect(result).to include(success: true, entities: [], source: :empty)
122
55
  end
123
56
  end
124
57
  end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'sequel'
5
+ require 'legion/extensions/apollo/runners/entity_watchdog'
6
+
7
+ RSpec.describe Legion::Extensions::Apollo::Runners::EntityWatchdog do
8
+ let(:db) { Sequel.sqlite }
9
+ let(:published) { [] }
10
+
11
+ before do
12
+ db.create_table(:task_logs) do
13
+ primary_key :id
14
+ String :entry
15
+ DateTime :created_at
16
+ end
17
+ stub_const('Legion::Data::Model::TaskLog', Class.new(Sequel::Model(db[:task_logs])))
18
+
19
+ messages = published
20
+ message_class = Class.new do
21
+ define_singleton_method(:published) { messages }
22
+ define_method(:initialize) { |**attributes| @attributes = attributes }
23
+ define_method(:publish!) do
24
+ messages << @attributes
25
+ { accepted: true }
26
+ end
27
+ end
28
+ stub_const('Legion::Extensions::Apollo::Transport::Messages::Ingest', message_class)
29
+ described_class.instance_variable_set(:@entity_watchdog_processed_hashes, {})
30
+ end
31
+
32
+ after do
33
+ db.disconnect
34
+ described_class.remove_instance_variable(:@entity_watchdog_processed_hashes) if
35
+ described_class.instance_variable_defined?(:@entity_watchdog_processed_hashes)
36
+ end
37
+
38
+ def log_entry(text, offset: 0)
39
+ Legion::Data::Model::TaskLog.create(entry: text, created_at: Time.now - offset)
40
+ end
41
+
42
+ it 'reports missing TaskLog storage instead of treating it as an empty scan' do
43
+ hide_const('Legion::Data::Model::TaskLog')
44
+
45
+ result = described_class.scan_and_ingest
46
+
47
+ expect(result).to include(success: false, error: 'task_log_not_available', ingested: 0)
48
+ end
49
+
50
+ it 'ingests a new entity from a real TaskLog row and deduplicates only after success' do
51
+ log_entry('deploying lex-synapse')
52
+ allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities)
53
+ .and_return(success: true, source: :llm,
54
+ entities: [{ name: 'lex-synapse', type: 'repository', confidence: 0.95 }])
55
+ allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
56
+ .and_return(success: true, entries: [], count: 0)
57
+
58
+ first = described_class.scan_and_ingest
59
+ second = described_class.scan_and_ingest
60
+
61
+ expect(first).to include(success: true, ingested: 1, logs_scanned: 1, failed: 0)
62
+ expect(second).to include(success: true, ingested: 0, logs_scanned: 1, failed: 0)
63
+ expect(Legion::Extensions::Apollo::Runners::EntityExtractor).to have_received(:extract_entities).once
64
+ expect(published).to contain_exactly(hash_including(content: 'Repository: lex-synapse'))
65
+ end
66
+
67
+ it 'reports partial acceptance and retries failed texts while retaining successful deduplication' do
68
+ log_entry('successful text')
69
+ log_entry('retry text', offset: 1)
70
+ attempts = Hash.new(0)
71
+ allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities) do |text:, **|
72
+ attempts[text] += 1
73
+ if text == 'retry text' && attempts[text] == 1
74
+ { success: false, entities: [], source: :error }
75
+ else
76
+ { success: true, source: :llm,
77
+ entities: [{ name: text, type: 'concept', confidence: 0.9 }] }
78
+ end
79
+ end
80
+ allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
81
+ .and_return(success: true, entries: [], count: 0)
82
+
83
+ first = described_class.scan_and_ingest
84
+ second = described_class.scan_and_ingest
85
+
86
+ expect(first).to include(success: false, ingested: 1, logs_scanned: 2, failed: 1)
87
+ expect(second).to include(success: true, ingested: 1, logs_scanned: 2, failed: 0)
88
+ expect(attempts).to eq('successful text' => 1, 'retry text' => 2)
89
+ expect(published.size).to eq(2)
90
+ end
91
+
92
+ it 'does not treat a failed entity lookup as absence and retries that text' do
93
+ log_entry('lookup failure')
94
+ allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities)
95
+ .and_return(success: true, source: :llm,
96
+ entities: [{ name: 'unknown', type: 'concept', confidence: 0.9 }])
97
+ allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
98
+ .and_return(success: false, error: 'database unavailable')
99
+
100
+ first = described_class.scan_and_ingest
101
+ second = described_class.scan_and_ingest
102
+
103
+ expect(first).to include(success: false, ingested: 0, failed: 1)
104
+ expect(second).to include(success: false, ingested: 0, failed: 1)
105
+ expect(Legion::Extensions::Apollo::Runners::EntityExtractor).to have_received(:extract_entities).twice
106
+ expect(published).to be_empty
107
+ end
108
+
109
+ it 'keeps a publication failure retryable' do
110
+ log_entry('publication failure')
111
+ allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities)
112
+ .and_return(success: true, source: :llm,
113
+ entities: [{ name: 'broken', type: 'service', confidence: 0.9 }])
114
+ allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
115
+ .and_return(success: true, entries: [], count: 0)
116
+ message = Legion::Extensions::Apollo::Transport::Messages::Ingest
117
+ allow(message).to receive(:new).and_raise(IOError, 'broker unavailable')
118
+
119
+ result = described_class.scan_and_ingest
120
+
121
+ expect(result).to include(success: false, ingested: 0, failed: 1)
122
+ expect(Legion::Extensions::Apollo::Runners::EntityExtractor).to have_received(:extract_entities).once
123
+ end
124
+ end
@@ -4,108 +4,104 @@ require 'spec_helper'
4
4
  require 'legion/extensions/apollo/runners/expertise'
5
5
 
6
6
  RSpec.describe Legion::Extensions::Apollo::Runners::Expertise do
7
- let(:runner) do
8
- obj = Object.new
9
- obj.extend(described_class)
10
- obj
7
+ let(:runner) { described_class }
8
+ let(:database) { Sequel.sqlite }
9
+ let(:entry_model) { Class.new(Sequel::Model(database[:apollo_entries])) }
10
+ let(:expertise_model) { Class.new(Sequel::Model(database[:apollo_expertise])) }
11
+
12
+ before do
13
+ allow(runner).to receive(:settings).and_return(Legion::Extensions::Apollo.settings)
14
+ database.create_table(:apollo_entries) do
15
+ primary_key :id
16
+ String :source_agent
17
+ String :knowledge_domain
18
+ String :tags
19
+ Float :confidence
20
+ end
21
+ database.create_table(:apollo_expertise) do
22
+ primary_key :id
23
+ String :agent_id
24
+ String :domain
25
+ Float :proficiency
26
+ Integer :entry_count
27
+ DateTime :last_active_at
28
+ end
29
+ stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
30
+ stub_const('Legion::Data::Model::Apollo::Expertise', expertise_model)
11
31
  end
12
32
 
33
+ after { database.disconnect }
34
+
13
35
  describe '#get_expertise' do
14
- it 'returns an expertise query payload' do
15
- result = runner.get_expertise(domain: 'vault')
16
- expect(result[:action]).to eq(:expertise_query)
17
- expect(result[:domain]).to eq('vault')
18
- expect(result[:min_proficiency]).to eq(0.0)
36
+ it 'returns persisted expertise ordered by proficiency' do
37
+ expertise_model.create(agent_id: 'worker-1', domain: 'vault', proficiency: 0.8, entry_count: 2)
38
+ expertise_model.create(agent_id: 'worker-2', domain: 'vault', proficiency: 0.6, entry_count: 1)
39
+
40
+ result = runner.get_expertise(domain: 'vault', min_proficiency: 0.0)
41
+
42
+ expect(result).to include(success: true, domain: 'vault', count: 2)
43
+ expect(result[:entries].map { |entry| entry[:agent_id] }).to eq(%w[worker-1 worker-2])
19
44
  end
20
45
  end
21
46
 
22
47
  describe '#domains_at_risk' do
23
- it 'returns an at-risk query payload' do
24
- result = runner.domains_at_risk(min_agents: 2)
25
- expect(result[:action]).to eq(:domains_at_risk)
26
- expect(result[:min_agents]).to eq(2)
48
+ it 'counts distinct agents per domain using SQL aggregation' do
49
+ expertise_model.create(agent_id: 'worker-1', domain: 'vault', proficiency: 0.8, entry_count: 2)
50
+ expertise_model.create(agent_id: 'worker-2', domain: 'vault', proficiency: 0.7, entry_count: 1)
51
+ expertise_model.create(agent_id: 'worker-1', domain: 'claims', proficiency: 0.7, entry_count: 1)
52
+
53
+ result = runner.domains_at_risk(min_agents: 3)
54
+
55
+ expect(result).to include(success: true, min_agents: 3, count: 2)
56
+ expect(result[:domains]).to contain_exactly(
57
+ include(domain: 'claims', agent_count: 1), include(domain: 'vault', agent_count: 2)
58
+ )
27
59
  end
28
60
  end
29
61
 
30
62
  describe '#agent_profile' do
31
- it 'returns a profile query payload' do
63
+ it 'returns one agent profile ordered by domain' do
64
+ expertise_model.create(agent_id: 'worker-1', domain: 'vault', proficiency: 0.8, entry_count: 2)
65
+ expertise_model.create(agent_id: 'worker-1', domain: 'claims', proficiency: 0.7, entry_count: 1)
66
+ expertise_model.create(agent_id: 'worker-2', domain: 'vault', proficiency: 0.6, entry_count: 1)
67
+
32
68
  result = runner.agent_profile(agent_id: 'worker-1')
33
- expect(result[:action]).to eq(:agent_profile)
34
- expect(result[:agent_id]).to eq('worker-1')
69
+
70
+ expect(result).to include(success: true, agent_id: 'worker-1', count: 2)
71
+ expect(result[:entries].map { |entry| entry[:domain] }).to eq(%w[claims vault])
35
72
  end
36
73
  end
37
74
 
38
75
  describe '#aggregate' do
39
- let(:host) { Object.new.extend(described_class) }
40
-
41
- context 'when Apollo data is not available' do
42
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
76
+ it 'returns an explicit error when canonical Apollo models are unavailable' do
77
+ hide_const('Legion::Data::Model::Apollo')
43
78
 
44
- it 'returns a structured error' do
45
- result = host.aggregate
46
- expect(result[:success]).to be false
47
- expect(result[:error]).to eq('apollo_data_not_available')
48
- end
79
+ expect(runner.aggregate).to include(success: false, error: 'apollo_data_not_available')
49
80
  end
50
81
 
51
- context 'when entries exist' do
52
- let(:mock_entry_class) { double('ApolloEntry') }
53
- let(:mock_expertise_class) { double('ApolloExpertise') }
54
- let(:entries) do
55
- [
56
- double('e1', source_agent: 'agent-1', tags: ['ruby'], confidence: 0.8),
57
- double('e2', source_agent: 'agent-1', tags: ['ruby'], confidence: 0.6),
58
- double('e3', source_agent: 'agent-2', tags: ['python'], confidence: 0.9)
59
- ]
60
- end
61
-
62
- before do
63
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
64
- stub_const('Legion::Data::Model::ApolloExpertise', mock_expertise_class)
65
- allow(mock_entry_class).to receive(:select).and_return(double(exclude: double(all: entries)))
66
- allow(mock_expertise_class).to receive(:where).and_return(double(first: nil))
67
- allow(mock_expertise_class).to receive(:create)
68
- end
69
-
70
- it 'returns agent and domain counts' do
71
- result = host.aggregate
72
- expect(result[:success]).to be true
73
- expect(result[:agents]).to eq(2)
74
- expect(result[:domains]).to eq(2)
75
- end
76
-
77
- it 'creates expertise records' do
78
- expect(mock_expertise_class).to receive(:create).twice
79
- host.aggregate
80
- end
81
-
82
- it 'computes proficiency using log2 formula' do
83
- # agent-1 ruby: avg=0.7, count=2, proficiency = 0.7 * log2(3) = 0.7 * 1.585 = 1.109 -> capped at 1.0
84
- expect(mock_expertise_class).to receive(:create).with(
85
- hash_including(agent_id: 'agent-1', domain: 'ruby', proficiency: 1.0)
86
- )
87
- # agent-2 python: avg=0.9, count=1, proficiency = 0.9 * log2(2) = 0.9 * 1.0 = 0.9
88
- expect(mock_expertise_class).to receive(:create).with(
89
- hash_including(agent_id: 'agent-2', domain: 'python', proficiency: 0.9)
90
- )
91
- host.aggregate
92
- end
82
+ it 'groups by agent and domain without collapsing domain collisions' do
83
+ entry_model.create(source_agent: 'agent-1', knowledge_domain: 'ruby', confidence: 0.8)
84
+ entry_model.create(source_agent: 'agent-1', knowledge_domain: 'ruby', confidence: 0.6)
85
+ entry_model.create(source_agent: 'agent-1', knowledge_domain: 'python', confidence: 0.9)
86
+ entry_model.create(source_agent: 'agent-2', knowledge_domain: 'ruby', confidence: 0.7)
87
+
88
+ result = runner.aggregate
89
+
90
+ expect(result).to include(success: true, agents: 2, domains: 2)
91
+ rows = expertise_model.order(:agent_id, :domain).all.map { |row| [row.agent_id, row.domain, row.entry_count] }
92
+ expect(rows).to eq([['agent-1', 'python', 1], ['agent-1', 'ruby', 2], ['agent-2', 'ruby', 1]])
93
+ expect(expertise_model.where(agent_id: 'agent-1', domain: 'ruby').count).to eq(1)
93
94
  end
94
95
 
95
- context 'when no entries exist' do
96
- let(:mock_entry_class) { double('ApolloEntry') }
96
+ it 'derives a missing domain from the first serialized tag' do
97
+ entry_model.create(source_agent: 'agent-1', knowledge_domain: nil, tags: '["vault"]', confidence: 0.8)
97
98
 
98
- before do
99
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
100
- allow(mock_entry_class).to receive(:select).and_return(double(exclude: double(all: [])))
101
- end
99
+ expect(runner.aggregate).to include(success: true, agents: 1, domains: 1)
100
+ expect(expertise_model.first.values).to include(agent_id: 'agent-1', domain: 'vault', entry_count: 1)
101
+ end
102
102
 
103
- it 'returns zero counts' do
104
- result = host.aggregate
105
- expect(result[:success]).to be true
106
- expect(result[:agents]).to eq(0)
107
- expect(result[:domains]).to eq(0)
108
- end
103
+ it 'returns zero counts for an empty canonical entry table' do
104
+ expect(runner.aggregate).to include(success: true, agents: 0, domains: 0)
109
105
  end
110
106
  end
111
107
  end
@@ -3,96 +3,26 @@
3
3
  require 'spec_helper'
4
4
  require 'legion/extensions/apollo/runners/gas'
5
5
 
6
- RSpec.describe Legion::Extensions::Apollo::Runners::Gas, '.phase_anticipate' do
7
- let(:facts) do
8
- [
9
- { content: 'pgvector uses HNSW indexes', content_type: :fact },
10
- { content: 'cosine distance measures similarity', content_type: :concept }
11
- ]
12
- end
13
-
14
- let(:synthesis) do
15
- [
16
- { content: 'pgvector achieves fast search via HNSW', content_type: :inference, status: :candidate }
17
- ]
18
- end
19
-
20
- context 'when GaiaCaller is unavailable' do
21
- it 'returns empty array' do
22
- result = described_class.phase_anticipate(facts, synthesis)
23
- expect(result).to eq([])
24
- end
25
- end
26
-
27
- context 'when GaiaCaller is available' do
28
- let(:gaia_caller) { double('GaiaCaller') }
29
- let(:mock_response) do
30
- double(
31
- 'Response',
32
- message: {
33
- content: '{"questions":["How fast is pgvector HNSW search?","What distance metrics does pgvector support?"]}'
34
- }
35
- )
36
- end
37
-
38
- before do
39
- stub_const('Legion::LLM::Pipeline::GaiaCaller', gaia_caller)
40
- allow(gaia_caller).to receive(:structured).and_return(mock_response)
41
- end
42
-
43
- it 'generates anticipated questions' do
44
- result = described_class.phase_anticipate(facts, synthesis)
45
- expect(result).not_to be_empty
46
- expect(result.length).to be <= 3
47
- end
48
-
49
- it 'returns question strings' do
50
- result = described_class.phase_anticipate(facts, synthesis)
51
- result.each do |item|
52
- expect(item[:question]).to be_a(String)
6
+ RSpec.describe Legion::Extensions::Apollo::Runners::Gas, 'anticipation phase' do
7
+ it 'reports a completed mechanical process without fabricated anticipations when LLM is unavailable' do
8
+ allow(Legion::LLM).to receive(:started?).and_return(false)
9
+ allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities)
10
+ .and_return(success: true, entities: [])
11
+ knowledge = Module.new do
12
+ def self.retrieve_relevant(**)
13
+ { success: true, entries: [], count: 0 }
53
14
  end
54
- end
55
15
 
56
- context 'when PatternStore is available' do
57
- let(:pattern_store) { double('PatternStore') }
58
-
59
- before do
60
- stub_const('Legion::Extensions::Agentic::TBI::PatternStore', pattern_store)
61
- allow(pattern_store).to receive(:promote_candidate)
62
- end
63
-
64
- it 'promotes candidates to PatternStore' do
65
- described_class.phase_anticipate(facts, synthesis)
66
- expect(pattern_store).to have_received(:promote_candidate).at_least(:once)
16
+ def self.handle_ingest(**)
17
+ { success: true, entry_id: 7 }
67
18
  end
68
19
  end
20
+ stub_const('Legion::Extensions::Apollo::Runners::Knowledge', knowledge)
69
21
 
70
- context 'when PatternStore is not available' do
71
- it 'still returns questions without error' do
72
- result = described_class.phase_anticipate(facts, synthesis)
73
- expect(result).not_to be_empty
74
- end
75
- end
76
- end
77
-
78
- context 'when LLM call fails' do
79
- let(:gaia_caller) { double('GaiaCaller') }
22
+ result = described_class.process(
23
+ request_id: 'req-anticipate', messages: [{ content: 'question' }], response_content: 'observation'
24
+ )
80
25
 
81
- before do
82
- stub_const('Legion::LLM::Pipeline::GaiaCaller', gaia_caller)
83
- allow(gaia_caller).to receive(:structured).and_raise(StandardError, 'timeout')
84
- end
85
-
86
- it 'returns empty array on error' do
87
- result = described_class.phase_anticipate(facts, synthesis)
88
- expect(result).to eq([])
89
- end
90
- end
91
-
92
- context 'with empty facts' do
93
- it 'returns empty array' do
94
- result = described_class.phase_anticipate([], [])
95
- expect(result).to eq([])
96
- end
26
+ expect(result).to include(success: true, phases_completed: 6, anticipations: 0)
97
27
  end
98
28
  end