lex-apollo 0.4.44 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +72 -14
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -41
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +4 -16
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +96 -41
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -51
- data/lib/legion/extensions/apollo/helpers/confidence.rb +8 -52
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +99 -43
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +64 -102
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +87 -136
- data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
- data/lib/legion/extensions/apollo/runners/maintenance.rb +151 -64
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -46
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +240 -8
- data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +32 -3
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +23 -7
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -77
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +138 -6
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +11 -2
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +33 -65
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +40 -14
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -39
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -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)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 '
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
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
|
-
|
|
34
|
-
expect(result
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
96
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
104
|
-
|
|
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, '
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
57
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
|
@@ -18,13 +18,24 @@ RSpec.describe 'Apollo GAS relation persistence' do
|
|
|
18
18
|
db.create_table(:apollo_entries) do
|
|
19
19
|
primary_key :id
|
|
20
20
|
String :content
|
|
21
|
-
String :content_hash
|
|
21
|
+
String :content_hash
|
|
22
22
|
String :content_type
|
|
23
23
|
String :status
|
|
24
24
|
String :access_scope
|
|
25
25
|
Integer :identity_principal_id
|
|
26
26
|
Float :confidence
|
|
27
|
+
String :embedding
|
|
28
|
+
String :embedding_space_id
|
|
29
|
+
Integer :embedding_dimensions
|
|
30
|
+
String :embedding_provenance
|
|
31
|
+
DateTime :updated_at
|
|
27
32
|
end
|
|
33
|
+
db.run <<~SQL
|
|
34
|
+
CREATE UNIQUE INDEX idx_apollo_content_hash_owner ON apollo_entries (
|
|
35
|
+
content_hash, access_scope,
|
|
36
|
+
(CASE WHEN access_scope = 'global' THEN 0 ELSE COALESCE(identity_principal_id, 0) END)
|
|
37
|
+
) WHERE status != 'archived' AND content_hash IS NOT NULL
|
|
38
|
+
SQL
|
|
28
39
|
db.create_table(:apollo_relations) do
|
|
29
40
|
primary_key :id
|
|
30
41
|
foreign_key :from_entry_id, :apollo_entries, null: false
|
|
@@ -44,18 +55,23 @@ RSpec.describe 'Apollo GAS relation persistence' do
|
|
|
44
55
|
end
|
|
45
56
|
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
46
57
|
input[:entries].each do |entry|
|
|
47
|
-
|
|
58
|
+
embedding = ApolloSpecEmbedding.result
|
|
59
|
+
db[:apollo_entries].insert(entry.merge(
|
|
60
|
+
content_hash: Legion::Extensions::Apollo::Helpers::Writeback.content_hash(content: entry[:content]),
|
|
61
|
+
embedding: ::JSON.generate(embedding[:vector]), embedding_dimensions: embedding[:dimensions],
|
|
62
|
+
embedding_space_id: embedding[:embedding_provenance][:space_id],
|
|
63
|
+
embedding_provenance: ::JSON.generate(embedding[:embedding_provenance])
|
|
64
|
+
))
|
|
48
65
|
end
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
input[:facts], [], input[:relations], [], { request_id: 'gas-regression' }
|
|
66
|
+
result = Legion::Extensions::Apollo::Runners::Gas.send(
|
|
67
|
+
:gas_phase_deposit, facts: input[:facts], relations: input[:relations], synthesis: [],
|
|
68
|
+
audit_event: { request_id: 'gas-regression' }
|
|
53
69
|
)
|
|
54
70
|
puts "APOLLO_RESULT=#{::JSON.generate(result: result, relations: db[:apollo_relations].all)}"
|
|
55
71
|
RUBY
|
|
56
72
|
end
|
|
57
73
|
|
|
58
|
-
def gas_deposit_state(entries:, facts:, relations
|
|
74
|
+
def gas_deposit_state(entries:, facts:, relations:, **)
|
|
59
75
|
stdout, stderr, status = Open3.capture3(
|
|
60
76
|
RbConfig.ruby, '-Ilib', '-Ispec', '-e', deposit_script,
|
|
61
77
|
chdir: File.expand_path('../../../../..', __dir__),
|
|
@@ -3,109 +3,67 @@
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
require 'legion/extensions/apollo/runners/gas'
|
|
5
5
|
|
|
6
|
-
RSpec.describe Legion::Extensions::Apollo::Runners::Gas, '
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::Gas, 'relation classification' do
|
|
7
7
|
let(:facts) do
|
|
8
|
-
[
|
|
9
|
-
|
|
10
|
-
{ content: 'cosine distance measures similarity', content_type: :concept }
|
|
11
|
-
]
|
|
8
|
+
[{ content: 'pgvector uses HNSW indexes', content_type: :fact },
|
|
9
|
+
{ content: 'cosine distance measures similarity', content_type: :concept }]
|
|
12
10
|
end
|
|
13
|
-
let(:
|
|
14
|
-
|
|
11
|
+
let(:similar_entries) do
|
|
12
|
+
{ success: true,
|
|
13
|
+
entries: [{ id: 'e1', content: 'HNSW is an approximate nearest neighbor algorithm', content_type: 'concept', confidence: 0.85 }],
|
|
14
|
+
count: 1 }
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
before do
|
|
18
|
+
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
19
|
+
allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant).and_return(similar_entries)
|
|
20
|
+
end
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
it 'uses mechanical similar-to relations when the LLM is not started' do
|
|
23
|
+
result = described_class.send(:gas_phase_relate, facts: facts)
|
|
24
|
+
|
|
25
|
+
expect(result.map { |relation| [relation[:from_content], relation[:to_id], relation[:relation_type]] }).to eq(
|
|
26
|
+
facts.map { |fact| [fact[:content], 'e1', 'similar_to'] }
|
|
27
|
+
)
|
|
26
28
|
end
|
|
27
29
|
|
|
28
|
-
context '
|
|
29
|
-
|
|
30
|
-
let(:similar_entries) do
|
|
31
|
-
{
|
|
32
|
-
success: true,
|
|
33
|
-
entries: [
|
|
34
|
-
{ id: 'e1', content: 'HNSW is an approximate nearest neighbor algorithm', content_type: 'concept', confidence: 0.85 }
|
|
35
|
-
],
|
|
36
|
-
count: 1
|
|
37
|
-
}
|
|
38
|
-
end
|
|
30
|
+
context 'with LLM classification available' do
|
|
31
|
+
before { allow(Legion::LLM).to receive(:started?).and_return(true) }
|
|
39
32
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
end
|
|
33
|
+
it 'uses a supported high-confidence classification' do
|
|
34
|
+
allow(Legion::LLM::Inference::GaiaCaller).to receive(:structured)
|
|
35
|
+
.and_return(data: { relations: [{ relation_type: 'depends_on', confidence: 0.85 }] })
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
it 'falls back to similar_to relations' do
|
|
47
|
-
result = described_class.phase_relate(facts, entities)
|
|
48
|
-
expect(result).to all(include(relation_type: 'similar_to'))
|
|
49
|
-
end
|
|
37
|
+
result = described_class.send(:gas_phase_relate, facts: facts)
|
|
50
38
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
39
|
+
expect(result).to contain_exactly(
|
|
40
|
+
include(from_content: facts[0][:content], to_id: 'e1', relation_type: 'depends_on', confidence: 0.85),
|
|
41
|
+
include(from_content: facts[1][:content], to_id: 'e1', relation_type: 'depends_on', confidence: 0.85)
|
|
42
|
+
)
|
|
55
43
|
end
|
|
56
44
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
before do
|
|
69
|
-
stub_const('Legion::LLM::Pipeline::GaiaCaller', gaia_caller)
|
|
70
|
-
allow(gaia_caller).to receive(:structured).and_return(mock_response)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
it 'classifies relations via LLM' do
|
|
74
|
-
result = described_class.phase_relate(facts, entities)
|
|
75
|
-
expect(result).not_to be_empty
|
|
76
|
-
expect(result.first[:relation_type]).to eq('depends_on')
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it 'gates on confidence > 0.7' do
|
|
80
|
-
low_conf_response = double(
|
|
81
|
-
'Response',
|
|
82
|
-
message: {
|
|
83
|
-
content: '{"relations":[{"relation_type":"contradicts","confidence":0.3}]}'
|
|
84
|
-
}
|
|
85
|
-
)
|
|
86
|
-
allow(gaia_caller).to receive(:structured).and_return(low_conf_response)
|
|
87
|
-
|
|
88
|
-
result = described_class.phase_relate(facts, entities)
|
|
89
|
-
# Low confidence relations should fall back to similar_to
|
|
90
|
-
classified = result.select { |r| r[:relation_type] == 'contradicts' }
|
|
91
|
-
expect(classified).to be_empty
|
|
92
|
-
end
|
|
45
|
+
it 'does not turn a weak contradiction classification into a contradiction edge' do
|
|
46
|
+
allow(Legion::LLM::Inference::GaiaCaller).to receive(:structured)
|
|
47
|
+
.and_return(data: { relations: [{ relation_type: 'contradicts', confidence: 0.3 }] })
|
|
48
|
+
|
|
49
|
+
result = described_class.send(:gas_phase_relate, facts: facts)
|
|
50
|
+
|
|
51
|
+
expect(result.map { |relation| relation[:relation_type] }).to eq(%w[similar_to similar_to])
|
|
93
52
|
end
|
|
94
53
|
end
|
|
95
54
|
|
|
96
|
-
|
|
97
|
-
|
|
55
|
+
it 'returns no relations when retrieval succeeds without similar entries' do
|
|
56
|
+
allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
|
|
57
|
+
.and_return(success: true, entries: [], count: 0)
|
|
98
58
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
allow(knowledge_runner).to receive(:retrieve_relevant).and_return(
|
|
102
|
-
{ success: true, entries: [], count: 0 }
|
|
103
|
-
)
|
|
104
|
-
end
|
|
59
|
+
expect(described_class.send(:gas_phase_relate, facts: facts)).to eq([])
|
|
60
|
+
end
|
|
105
61
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
62
|
+
it 'propagates failed retrieval rather than treating it as an empty knowledge store' do
|
|
63
|
+
allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
|
|
64
|
+
.and_return(success: false, error: 'database unavailable')
|
|
65
|
+
|
|
66
|
+
expect { described_class.send(:gas_phase_relate, facts: facts) }
|
|
67
|
+
.to raise_error(IOError, /database unavailable/)
|
|
110
68
|
end
|
|
111
69
|
end
|