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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -0
- data/README.md +113 -18
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
- 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 +5 -19
- 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 +116 -51
- 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 -63
- data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
- 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 +107 -67
- 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 +67 -109
- 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 +165 -144
- data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
- data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -47
- 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/transport.rb +20 -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 +250 -28
- data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
- 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 +88 -80
- 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 +98 -71
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
- 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 +108 -0
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
- 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 +146 -107
- 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 +71 -13
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- 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 -49
- 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
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
RSpec.describe 'Apollo GAS relation persistence' do
|
|
9
|
+
let(:deposit_script) do
|
|
10
|
+
<<~'RUBY'
|
|
11
|
+
require 'sequel'
|
|
12
|
+
require 'rspec'
|
|
13
|
+
require 'spec_helper'
|
|
14
|
+
require 'json'
|
|
15
|
+
require 'legion/extensions/apollo/helpers/writeback'
|
|
16
|
+
require 'legion/extensions/apollo/runners/gas'
|
|
17
|
+
db = Sequel.sqlite
|
|
18
|
+
db.create_table(:apollo_entries) do
|
|
19
|
+
primary_key :id
|
|
20
|
+
String :content
|
|
21
|
+
String :content_hash
|
|
22
|
+
String :content_type
|
|
23
|
+
String :status
|
|
24
|
+
String :access_scope
|
|
25
|
+
Integer :identity_principal_id
|
|
26
|
+
Float :confidence
|
|
27
|
+
String :embedding
|
|
28
|
+
String :embedding_space_id
|
|
29
|
+
Integer :embedding_dimensions
|
|
30
|
+
String :embedding_provenance
|
|
31
|
+
DateTime :updated_at
|
|
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
|
|
39
|
+
db.create_table(:apollo_relations) do
|
|
40
|
+
primary_key :id
|
|
41
|
+
foreign_key :from_entry_id, :apollo_entries, null: false
|
|
42
|
+
foreign_key :to_entry_id, :apollo_entries, null: false
|
|
43
|
+
String :relation_type
|
|
44
|
+
String :source_agent
|
|
45
|
+
Float :weight
|
|
46
|
+
check(Sequel.~(relation_type: 'causes'))
|
|
47
|
+
end
|
|
48
|
+
db.create_table(:apollo_expertise) { primary_key :id }
|
|
49
|
+
db.create_table(:apollo_access_logs) { primary_key :id }
|
|
50
|
+
Legion::Data.const_set(:Model, Module.new) unless defined?(Legion::Data::Model)
|
|
51
|
+
Legion::Data::Model.const_set(:Apollo, Module.new)
|
|
52
|
+
{ Entry: :apollo_entries, Relation: :apollo_relations, Expertise: :apollo_expertise,
|
|
53
|
+
AccessLog: :apollo_access_logs }.each do |name, table|
|
|
54
|
+
Legion::Data::Model::Apollo.const_set(name, Class.new(Sequel::Model(db[table])))
|
|
55
|
+
end
|
|
56
|
+
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
57
|
+
input[:entries].each do |entry|
|
|
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
|
+
))
|
|
65
|
+
end
|
|
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' }
|
|
69
|
+
)
|
|
70
|
+
puts "APOLLO_RESULT=#{::JSON.generate(result: result, relations: db[:apollo_relations].all)}"
|
|
71
|
+
RUBY
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def gas_deposit_state(entries:, facts:, relations:, **)
|
|
75
|
+
stdout, stderr, status = Open3.capture3(
|
|
76
|
+
RbConfig.ruby, '-Ilib', '-Ispec', '-e', deposit_script,
|
|
77
|
+
chdir: File.expand_path('../../../../..', __dir__),
|
|
78
|
+
stdin_data: JSON.generate(entries: entries, facts: facts, relations: relations)
|
|
79
|
+
)
|
|
80
|
+
line = stdout.lines.find { |output| output.start_with?('APOLLO_RESULT=') }
|
|
81
|
+
raise "GAS deposit process failed: #{stderr}\n#{stdout}" unless status.success? && line
|
|
82
|
+
|
|
83
|
+
JSON.parse(line.delete_prefix('APOLLO_RESULT='), symbolize_names: true)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'links successful deduplicated facts safely and continues after an independent relation write fails' do
|
|
87
|
+
entry = { content_type: 'fact', status: 'candidate', access_scope: 'global', confidence: 0.5 }
|
|
88
|
+
entries = [entry.merge(id: 1, content: 'stored fact'), entry.merge(id: 2, content: 'target evidence'),
|
|
89
|
+
entry.merge(id: 3, content: 'private evidence', access_scope: 'private', identity_principal_id: 99),
|
|
90
|
+
entry.merge(id: 4, content: 'second fact')]
|
|
91
|
+
facts = [{ content: 'stored fact', content_type: :fact }, { content: '', content_type: :fact },
|
|
92
|
+
{ content: 'second fact', content_type: :fact }]
|
|
93
|
+
relation = { from_content: 'stored fact', to_id: 2, relation_type: 'supports_by', confidence: 0.9 }
|
|
94
|
+
relations = [relation, relation.dup, relation.merge(from_content: ''), relation.merge(to_id: 999),
|
|
95
|
+
relation.merge(to_id: 1), relation.merge(to_id: 3), relation.merge(confidence: 0.2),
|
|
96
|
+
relation.merge(relation_type: 'unsupported'), relation.merge(from_content: 'second fact', relation_type: 'causes'),
|
|
97
|
+
relation.merge(from_content: 'second fact', relation_type: 'depends_on')]
|
|
98
|
+
|
|
99
|
+
state = gas_deposit_state(entries: entries, facts: facts, relations: relations)
|
|
100
|
+
|
|
101
|
+
expect(state[:result]).to include(deposited: 2, failed: 1, facts: 2, synthesis: 0,
|
|
102
|
+
relations: 2, relations_failed: 3, relations_skipped: 5)
|
|
103
|
+
expect(state[:relations]).to contain_exactly(
|
|
104
|
+
hash_including(from_entry_id: 1, to_entry_id: 2, relation_type: 'supports_by'),
|
|
105
|
+
hash_including(from_entry_id: 4, to_entry_id: 2, relation_type: 'depends_on')
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -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
|
|
@@ -4,105 +4,73 @@ require 'spec_helper'
|
|
|
4
4
|
require 'legion/extensions/apollo/runners/gas'
|
|
5
5
|
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::Runners::Gas do
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
caller: { requested_by: { identity: 'user:matt', type: :user } },
|
|
16
|
-
timestamp: Time.now
|
|
17
|
-
}
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it 'runs all 6 phases in order' do
|
|
21
|
-
allow(described_class).to receive(:phase_comprehend).and_return([{ content: 'fact', content_type: :fact }])
|
|
22
|
-
allow(described_class).to receive(:phase_extract).and_return([{ name: 'pgvector', type: 'technology' }])
|
|
23
|
-
allow(described_class).to receive(:phase_relate).and_return([])
|
|
24
|
-
allow(described_class).to receive(:phase_synthesize).and_return([])
|
|
25
|
-
allow(described_class).to receive(:phase_deposit).and_return({ deposited: 1 })
|
|
26
|
-
allow(described_class).to receive(:phase_anticipate).and_return([])
|
|
27
|
-
|
|
28
|
-
result = described_class.process(audit_event)
|
|
29
|
-
expect(result).to be_a(Hash)
|
|
30
|
-
expect(result[:phases_completed]).to eq(6)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it 'skips when audit event has no content' do
|
|
34
|
-
result = described_class.process({ request_id: 'req_abc' })
|
|
35
|
-
expect(result[:phases_completed]).to eq(0)
|
|
36
|
-
end
|
|
7
|
+
let(:audit_event) do
|
|
8
|
+
{
|
|
9
|
+
request_id: 'req-1',
|
|
10
|
+
messages: [{ role: :user, content: 'What is pgvector?' }],
|
|
11
|
+
response_content: 'pgvector uses HNSW indexes.',
|
|
12
|
+
routing: { provider: :test }
|
|
13
|
+
}
|
|
14
|
+
end
|
|
37
15
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
16
|
+
before do
|
|
17
|
+
allow(Legion::Extensions::Apollo::Runners::EntityExtractor).to receive(:extract_entities)
|
|
18
|
+
.and_return(success: true, entities: [{ name: 'pgvector', type: 'repository' }])
|
|
19
|
+
end
|
|
42
20
|
|
|
43
|
-
|
|
44
|
-
|
|
21
|
+
it 'skips events without an exchange response' do
|
|
22
|
+
result = described_class.process(request_id: 'req-1', messages: [])
|
|
45
23
|
|
|
46
|
-
|
|
47
|
-
expect(result[:phases_completed]).to eq(0)
|
|
48
|
-
expect(result[:error]).to eq('boom')
|
|
49
|
-
end
|
|
24
|
+
expect(result).to include(success: true, phases_completed: 0, reason: 'no content')
|
|
50
25
|
end
|
|
51
26
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
27
|
+
it 'runs the mechanical pipeline and counts a successful deposit' do
|
|
28
|
+
knowledge = Module.new do
|
|
29
|
+
def self.retrieve_relevant(**)
|
|
30
|
+
{ success: true, entries: [], count: 0 }
|
|
31
|
+
end
|
|
57
32
|
|
|
58
|
-
|
|
59
|
-
|
|
33
|
+
def self.handle_ingest(content:, **)
|
|
34
|
+
{ success: true, entry_id: content.hash }
|
|
35
|
+
end
|
|
60
36
|
end
|
|
37
|
+
stub_const('Legion::Extensions::Apollo::Runners::Knowledge', knowledge)
|
|
38
|
+
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
61
39
|
|
|
62
|
-
|
|
63
|
-
expect(described_class.processable?({ messages: [{ role: :user, content: 'hi' }] })).to be false
|
|
64
|
-
end
|
|
65
|
-
end
|
|
40
|
+
result = described_class.process(**audit_event)
|
|
66
41
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
result = described_class.mechanical_comprehend(messages, 'pgvector is fast')
|
|
71
|
-
expect(result).to eq([{ content: 'pgvector is fast', content_type: :observation }])
|
|
72
|
-
end
|
|
42
|
+
expect(result).to include(success: true, phases_completed: 6, facts: 1, entities: 1,
|
|
43
|
+
relations: 0, synthesis: 0, anticipations: 0)
|
|
44
|
+
expect(result[:deposited]).to include(deposited: 1, failed: 0, facts: 1)
|
|
73
45
|
end
|
|
74
46
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
end
|
|
81
|
-
end
|
|
47
|
+
it 'reports partial deposit failure without claiming success' do
|
|
48
|
+
knowledge = Module.new do
|
|
49
|
+
def self.retrieve_relevant(**)
|
|
50
|
+
{ success: true, entries: [], count: 0 }
|
|
51
|
+
end
|
|
82
52
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
53
|
+
def self.handle_ingest(**)
|
|
54
|
+
{ success: false, error: 'write rejected' }
|
|
55
|
+
end
|
|
86
56
|
end
|
|
87
|
-
|
|
57
|
+
stub_const('Legion::Extensions::Apollo::Runners::Knowledge', knowledge)
|
|
58
|
+
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
88
59
|
|
|
89
|
-
|
|
90
|
-
it 'returns empty array (stub)' do
|
|
91
|
-
expect(described_class.phase_synthesize([], [])).to eq([])
|
|
92
|
-
end
|
|
93
|
-
end
|
|
60
|
+
result = described_class.process(**audit_event)
|
|
94
61
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
end
|
|
62
|
+
expect(result[:success]).to be false
|
|
63
|
+
expect(result[:phases_completed]).to eq(6)
|
|
64
|
+
expect(result[:deposited]).to include(deposited: 0, failed: 1)
|
|
99
65
|
end
|
|
100
66
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
67
|
+
it 'returns a structured failure when a dependency raises' do
|
|
68
|
+
allow(Legion::Extensions::Apollo::Runners::Knowledge).to receive(:retrieve_relevant)
|
|
69
|
+
.and_raise(IOError, 'retrieval unavailable')
|
|
70
|
+
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
71
|
+
|
|
72
|
+
result = described_class.process(**audit_event)
|
|
73
|
+
|
|
74
|
+
expect(result).to include(success: false, phases_completed: 0, error: 'retrieval unavailable')
|
|
107
75
|
end
|
|
108
76
|
end
|
|
@@ -3,94 +3,27 @@
|
|
|
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(:relations) do
|
|
15
|
-
[
|
|
16
|
-
{ from_content: 'pgvector uses HNSW indexes', to_id: 'e1', relation_type: 'depends_on', confidence: 0.8 }
|
|
17
|
-
]
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
context 'when GaiaCaller is unavailable' do
|
|
21
|
-
it 'returns empty array' do
|
|
22
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
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: '{"synthesis":[{"content":"pgvector achieves fast search via HNSW",' \
|
|
34
|
-
'"content_type":"inference","source_indices":[0,1]}]}'
|
|
35
|
-
}
|
|
36
|
-
)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
before do
|
|
40
|
-
stub_const('Legion::LLM::Pipeline::GaiaCaller', gaia_caller)
|
|
41
|
-
allow(gaia_caller).to receive(:structured).and_return(mock_response)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'generates derivative knowledge entries' do
|
|
45
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
46
|
-
expect(result).not_to be_empty
|
|
47
|
-
expect(result.first[:content]).to include('pgvector')
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it 'marks entries as candidate status' do
|
|
51
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
52
|
-
expect(result.first[:status]).to eq(:candidate)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it 'caps confidence at 0.7' do
|
|
56
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
57
|
-
result.each do |entry|
|
|
58
|
-
expect(entry[:confidence]).to be <= 0.7
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::Gas, 'synthesis phase' do
|
|
7
|
+
it 'does not fabricate derivative entries from a single mechanical observation' 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 }
|
|
59
14
|
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'includes depends_on source indices' do
|
|
63
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
64
|
-
expect(result.first[:source_indices]).to eq([0, 1])
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
context 'when LLM call fails' do
|
|
69
|
-
let(:gaia_caller) { double('GaiaCaller') }
|
|
70
15
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it 'returns empty array on error' do
|
|
77
|
-
result = described_class.phase_synthesize(facts, relations)
|
|
78
|
-
expect(result).to eq([])
|
|
16
|
+
def self.handle_ingest(**)
|
|
17
|
+
{ success: true, entry_id: 8 }
|
|
18
|
+
end
|
|
79
19
|
end
|
|
80
|
-
|
|
20
|
+
stub_const('Legion::Extensions::Apollo::Runners::Knowledge', knowledge)
|
|
81
21
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
expect(result).to eq([])
|
|
86
|
-
end
|
|
22
|
+
result = described_class.process(
|
|
23
|
+
request_id: 'req-synthesize', messages: [{ content: 'question' }], response_content: 'observation'
|
|
24
|
+
)
|
|
87
25
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
[{ content: 'one fact', content_type: :fact, confidence: 0.9 }],
|
|
91
|
-
[]
|
|
92
|
-
)
|
|
93
|
-
expect(result).to eq([])
|
|
94
|
-
end
|
|
26
|
+
expect(result).to include(success: true, phases_completed: 6, facts: 1, synthesis: 0)
|
|
27
|
+
expect(result[:deposited]).to include(synthesis: 0)
|
|
95
28
|
end
|
|
96
29
|
end
|