lex-apollo 0.4.44 → 0.4.49
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 +54 -0
- data/README.md +76 -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 +16 -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 +146 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +120 -142
- data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
- data/lib/legion/extensions/apollo/runners/maintenance.rb +154 -65
- 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/transport.rb +8 -5
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +28 -24
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +9 -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 +16 -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 +35 -6
- 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 +205 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +14 -91
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +28 -11
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +87 -77
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +14 -89
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +141 -9
- 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 +14 -5
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +48 -64
- 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/legion/extensions/apollo/transport_spec.rb +33 -0
- data/spec/spec_helper.rb +42 -13
- metadata +41 -13
- 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/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
|
@@ -21,8 +21,13 @@ RSpec.describe 'Apollo service lifecycle mutations' do
|
|
|
21
21
|
String :content
|
|
22
22
|
String :content_type
|
|
23
23
|
String :status
|
|
24
|
+
String :access_scope, default: 'global'
|
|
24
25
|
Float :confidence
|
|
25
26
|
String :source_context
|
|
27
|
+
String :embedding
|
|
28
|
+
String :embedding_space_id
|
|
29
|
+
Integer :embedding_dimensions
|
|
30
|
+
String :embedding_provenance
|
|
26
31
|
DateTime :created_at
|
|
27
32
|
DateTime :updated_at
|
|
28
33
|
DateTime :confirmed_at
|
|
@@ -36,6 +41,10 @@ RSpec.describe 'Apollo service lifecycle mutations' do
|
|
|
36
41
|
old = now - (100 * 86_400)
|
|
37
42
|
attributes = { content_type: 'fact', status: 'candidate', confidence: 0.8,
|
|
38
43
|
source_context: ::JSON.generate(origin: 'preserve'), created_at: now, updated_at: nil }
|
|
44
|
+
embedding = ApolloSpecEmbedding.result
|
|
45
|
+
attributes.merge!(embedding: ::JSON.generate(embedding[:vector]), embedding_dimensions: embedding[:dimensions],
|
|
46
|
+
embedding_space_id: embedding[:embedding_provenance][:space_id],
|
|
47
|
+
embedding_provenance: ::JSON.generate(embedding[:embedding_provenance]))
|
|
39
48
|
db[:apollo_entries].insert(attributes.merge(id: 1, content: 'target'))
|
|
40
49
|
db[:apollo_entries].insert(attributes.merge(id: 2, content: 'protected'))
|
|
41
50
|
db[:apollo_entries].insert(attributes.merge(id: 3, content: 'stale', confidence: 0.04, created_at: old))
|
|
@@ -44,7 +53,7 @@ RSpec.describe 'Apollo service lifecycle mutations' do
|
|
|
44
53
|
db[:apollo_entries].insert(attributes.merge(id: 5, content: 'already archived', status: 'archived', confidence: 0.7,
|
|
45
54
|
created_at: old))
|
|
46
55
|
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
47
|
-
maintenance =
|
|
56
|
+
maintenance = Legion::Extensions::Apollo::Runners::Maintenance
|
|
48
57
|
case input[:operation]
|
|
49
58
|
when 'deprecate'
|
|
50
59
|
api = Rack::MockRequest.new(Legion::Extensions::Apollo::Api)
|
|
@@ -96,16 +105,16 @@ RSpec.describe 'Apollo service lifecycle mutations' do
|
|
|
96
105
|
RUBY
|
|
97
106
|
end
|
|
98
107
|
|
|
99
|
-
def lifecycle_state(operation
|
|
108
|
+
def lifecycle_state(operation:, **)
|
|
100
109
|
stdout, stderr, status = Open3.capture3(
|
|
101
110
|
RbConfig.ruby, '-Ilib', '-Ispec', '-e', lifecycle_script,
|
|
102
111
|
chdir: File.expand_path('../../../../..', __dir__),
|
|
103
112
|
stdin_data: JSON.generate(operation: operation)
|
|
104
113
|
)
|
|
105
|
-
|
|
106
|
-
raise "Lifecycle process failed: #{stderr}\n#{stdout}" unless status.success? &&
|
|
114
|
+
marker = 'APOLLO_RESULT='
|
|
115
|
+
raise "Lifecycle process failed: #{stderr}\n#{stdout}" unless status.success? && stdout.include?(marker)
|
|
107
116
|
|
|
108
|
-
|
|
117
|
+
ApolloSpecOutput.parse(stdout)
|
|
109
118
|
end
|
|
110
119
|
|
|
111
120
|
it 'archives through HTTP with retained metadata and truthful repeat, missing, unavailable, and rollback outcomes' do
|
|
@@ -6,10 +6,11 @@ require 'legion/extensions/apollo/helpers/similarity'
|
|
|
6
6
|
require 'legion/extensions/apollo/runners/maintenance'
|
|
7
7
|
|
|
8
8
|
RSpec.describe Legion::Extensions::Apollo::Runners::Maintenance do
|
|
9
|
-
let(:runner)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
let(:runner) { described_class }
|
|
10
|
+
|
|
11
|
+
before do
|
|
12
|
+
hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo)
|
|
13
|
+
allow(runner).to receive(:settings).and_return(Legion::Extensions::Apollo.settings)
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
it 'retains client action envelopes without claiming completed service mutations when data is unavailable' do
|
|
@@ -21,62 +22,68 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Maintenance do
|
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
describe '#run_decay_cycle' do
|
|
24
|
-
let(:host) {
|
|
25
|
+
let(:host) { runner }
|
|
25
26
|
|
|
26
27
|
context 'when Legion::Data is not available' do
|
|
27
|
-
before {
|
|
28
|
+
before { allow(Legion::Data).to receive(:connection).and_return(nil) }
|
|
28
29
|
|
|
29
|
-
it 'returns
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
expect(result[:archived]).to eq(0)
|
|
30
|
+
it 'returns an explicit unavailable error' do
|
|
31
|
+
expect(host.run_decay_cycle).to include(success: false, decayed: 0, archived: 0,
|
|
32
|
+
error: 'apollo_data_not_available')
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
context '
|
|
37
|
-
let(:
|
|
38
|
-
let(:mock_dataset) { double('dataset') }
|
|
36
|
+
context 'with SQLite records spanning a batch boundary' do
|
|
37
|
+
let(:db) { Sequel.sqlite }
|
|
39
38
|
|
|
40
39
|
before do
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
db.create_table(:apollo_entries) do
|
|
41
|
+
primary_key :id
|
|
42
|
+
String :status
|
|
43
|
+
Float :confidence
|
|
44
|
+
DateTime :created_at
|
|
45
|
+
DateTime :updated_at
|
|
47
46
|
end
|
|
48
|
-
|
|
49
|
-
allow(Legion::Data).to receive(:connection).and_return(mock_conn)
|
|
50
|
-
allow(mock_conn).to receive(:[]).with(:apollo_entries).and_return(mock_dataset)
|
|
51
|
-
allow(mock_dataset).to receive(:exclude).and_return(mock_dataset)
|
|
52
|
-
allow(mock_dataset).to receive(:where).and_return(mock_dataset)
|
|
53
|
-
allow(mock_dataset).to receive(:update).and_return(5)
|
|
47
|
+
allow(Legion::Data).to receive(:connection).and_return(db)
|
|
54
48
|
end
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
result = host.run_decay_cycle
|
|
58
|
-
expect(result[:alpha]).to eq(0.05)
|
|
59
|
-
expect(result).not_to have_key(:rate)
|
|
60
|
-
end
|
|
50
|
+
after { db.disconnect }
|
|
61
51
|
|
|
62
|
-
it '
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
52
|
+
it 'decays all eligible rows without skipping records as archived rows leave the batch scope' do
|
|
53
|
+
old = Time.now - (30 * 86_400)
|
|
54
|
+
501.times { db[:apollo_entries].insert(status: 'candidate', confidence: 0.01, created_at: old) }
|
|
55
|
+
retained = db[:apollo_entries].insert(status: 'confirmed', confidence: 1.0, created_at: old)
|
|
56
|
+
young = db[:apollo_entries].insert(status: 'candidate', confidence: 0.01, created_at: Time.now)
|
|
57
|
+
|
|
58
|
+
expect(host.run_decay_cycle(alpha: 0.3, min_confidence: 0.05)).to include(decayed: 502, archived: 501)
|
|
59
|
+
expect(db[:apollo_entries].where(status: 'archived').count).to eq(501)
|
|
60
|
+
expect(db[:apollo_entries][id: retained][:confidence]).to be_between(0.9, 1.0).exclusive
|
|
61
|
+
expect(db[:apollo_entries][id: young]).to include(status: 'candidate', confidence: 0.01)
|
|
66
62
|
end
|
|
67
63
|
|
|
68
|
-
it '
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
it 'applies approximately one day of power-law decay across 24 hourly cycles' do
|
|
65
|
+
start_time = Time.now
|
|
66
|
+
initial_confidence = 0.5
|
|
67
|
+
entry_id = db[:apollo_entries].insert(
|
|
68
|
+
status: 'confirmed', confidence: initial_confidence, created_at: start_time - (8 * 86_400)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
24.times do |hour|
|
|
72
|
+
allow(Time).to receive(:now).and_return(start_time + (hour * 3600))
|
|
73
|
+
expect(host.run_decay_cycle).to include(success: true, decayed: 1, archived: 0)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
expected = initial_confidence * ((8.0 / 9.0)**Legion::Extensions::Apollo.settings[:power_law_alpha])
|
|
77
|
+
expect(db[:apollo_entries][id: entry_id][:confidence]).to be_within(0.001).of(expected)
|
|
71
78
|
end
|
|
72
79
|
end
|
|
73
80
|
end
|
|
74
81
|
|
|
75
82
|
describe '#check_corroboration' do
|
|
76
|
-
let(:host) {
|
|
83
|
+
let(:host) { runner }
|
|
77
84
|
|
|
78
85
|
context 'when Apollo data is not available' do
|
|
79
|
-
before { hide_const('Legion::Data::Model::
|
|
86
|
+
before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
|
|
80
87
|
|
|
81
88
|
it 'returns a structured error' do
|
|
82
89
|
result = host.check_corroboration
|
|
@@ -86,11 +93,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Maintenance do
|
|
|
86
93
|
end
|
|
87
94
|
|
|
88
95
|
context 'when relation persistence is unavailable' do
|
|
89
|
-
let(:mock_entry_class) { double('
|
|
96
|
+
let(:mock_entry_class) { double('Apollo::Entry') }
|
|
90
97
|
|
|
91
98
|
before do
|
|
92
|
-
stub_const('Legion::Data::Model::
|
|
93
|
-
hide_const('Legion::Data::Model::ApolloRelation') if defined?(Legion::Data::Model::ApolloRelation)
|
|
99
|
+
stub_const('Legion::Data::Model::Apollo::Entry', mock_entry_class)
|
|
94
100
|
hide_const('Legion::Data::Model::Apollo::Relation') if defined?(Legion::Data::Model::Apollo::Relation)
|
|
95
101
|
end
|
|
96
102
|
|
|
@@ -100,27 +106,5 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Maintenance do
|
|
|
100
106
|
expect(host.check_corroboration).to include(success: false, error: 'apollo_data_not_available')
|
|
101
107
|
end
|
|
102
108
|
end
|
|
103
|
-
|
|
104
|
-
context 'when no candidates exist' do
|
|
105
|
-
let(:mock_entry_class) { double('ApolloEntry') }
|
|
106
|
-
|
|
107
|
-
before do
|
|
108
|
-
stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
|
|
109
|
-
stub_const('Legion::Data::Model::ApolloRelation', Class.new)
|
|
110
|
-
candidate_dataset = double(
|
|
111
|
-
exclude: double(order: double(limit: double(all: [])))
|
|
112
|
-
)
|
|
113
|
-
confirmed_dataset = double(select_map: [])
|
|
114
|
-
allow(mock_entry_class).to receive(:where).with(status: 'candidate').and_return(candidate_dataset)
|
|
115
|
-
allow(mock_entry_class).to receive(:where).with(status: 'confirmed').and_return(confirmed_dataset)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
it 'returns zero promoted and scanned' do
|
|
119
|
-
result = host.check_corroboration
|
|
120
|
-
expect(result[:success]).to be true
|
|
121
|
-
expect(result[:promoted]).to eq(0)
|
|
122
|
-
expect(result[:scanned]).to eq(0)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
109
|
end
|
|
126
110
|
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'legion/extensions/apollo/runners/query_response'
|
|
5
|
+
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::QueryResponse do
|
|
7
|
+
let(:exchange) { double('default exchange') }
|
|
8
|
+
let(:channel) { double('channel', default_exchange: exchange, using_publisher_confirmations?: true, wait_for_confirms: true) }
|
|
9
|
+
let(:reply_queue) { double('reply queue', name: 'apollo-reply') }
|
|
10
|
+
let(:replies) { [] }
|
|
11
|
+
let(:dataset) { double('entries') }
|
|
12
|
+
let(:database) { double('database', database_type: :postgres) }
|
|
13
|
+
let(:entry_model) { double('ApolloEntry', db: database) }
|
|
14
|
+
let(:entry) do
|
|
15
|
+
{ id: 'entry-1', content: 'A shared fact', content_type: 'fact', confidence: 0.8,
|
|
16
|
+
tags: ['ruby'], source_agent: 'worker', depth: 0, activation: 1.0 }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
before do
|
|
20
|
+
hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo)
|
|
21
|
+
hide_const('Legion::Identity::Process') if defined?(Legion::Identity::Process)
|
|
22
|
+
stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
|
|
23
|
+
allow(entry_model).to receive(:exclude).and_return(dataset)
|
|
24
|
+
allow(dataset).to receive_messages(where: dataset, order: dataset, limit: dataset, all: [entry])
|
|
25
|
+
allow(database).to receive(:fetch).and_return(dataset)
|
|
26
|
+
allow(Legion::Transport::Connection).to receive(:channel).and_return(channel)
|
|
27
|
+
allow(exchange).to receive(:on_return) { |&handler| @return_handler = handler }
|
|
28
|
+
allow(exchange).to receive(:publish) do |payload, **properties|
|
|
29
|
+
if properties[:routing_key].start_with?('missing-')
|
|
30
|
+
@return_handler.call(nil, Struct.new(:message_id).new(properties[:message_id]), payload)
|
|
31
|
+
else
|
|
32
|
+
replies << { properties: properties, result: Legion::JSON.load(payload) }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'publishes the actual browse result with the AMQP correlation on the default exchange' do
|
|
38
|
+
result = described_class.handle_request(query: '', reply_to: reply_queue.name, correlation_id: 'query-123')
|
|
39
|
+
|
|
40
|
+
expect(result).to include(success: true, mode: :browse, entries: [include(id: 'entry-1')])
|
|
41
|
+
expect(replies.one?).to be true
|
|
42
|
+
expect(replies.first[:result]).to eq(Legion::JSON.load(Legion::JSON.dump(result)))
|
|
43
|
+
expect(replies.first[:properties]).to include(routing_key: 'apollo-reply', correlation_id: 'query-123', content_type: 'application/json')
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'uses empty-query retrieval semantics instead of returning browse entries' do
|
|
47
|
+
described_class.handle_request(action: 'retrieve', query: '', reply_to: reply_queue.name)
|
|
48
|
+
|
|
49
|
+
expect(replies.first[:result]).to eq(success: true, entries: [], count: 0)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'executes traversal and publishes graph depth and activation' do
|
|
53
|
+
described_class.handle_request(action: 'traverse', entry_id: 'entry-1', reply_to: reply_queue.name)
|
|
54
|
+
|
|
55
|
+
expect(replies.first[:result]).to include(success: true, entries: [include(id: 'entry-1', depth: 0, activation: 1.0)])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'keeps requests without a reply destination fire-and-forget' do
|
|
59
|
+
expect(exchange).not_to receive(:publish)
|
|
60
|
+
|
|
61
|
+
expect(described_class.handle_request(action: 'retrieve', query: '')).to eq(success: true, entries: [], count: 0)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'publishes a structured error instead of dispatching an unsupported action' do
|
|
65
|
+
described_class.handle_request(action: 'delete_all', query: '', reply_to: reply_queue.name)
|
|
66
|
+
|
|
67
|
+
expect(replies.first[:result]).to eq(success: false, error: 'invalid_action')
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'publishes a structured error when a query body is missing' do
|
|
71
|
+
described_class.handle_request(action: 'query', reply_to: reply_queue.name)
|
|
72
|
+
|
|
73
|
+
expect(replies.first[:result]).to include(success: false, error: 'invalid_request')
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it 'rejects nonpositive and malformed traversal identities rather than executing a graph lookup' do
|
|
77
|
+
expect(database).not_to receive(:fetch)
|
|
78
|
+
|
|
79
|
+
[0, -1, nil, '', [], {}, 1.5, true].each do |entry_id|
|
|
80
|
+
result = described_class.handle_request(action: 'traverse', entry_id: entry_id, reply_to: reply_queue.name)
|
|
81
|
+
|
|
82
|
+
expect(result).to include(success: false, error: :invalid_request)
|
|
83
|
+
expect(replies.last[:result]).to include(success: false, error: 'invalid_request')
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context 'with native SQLite entry identities' do
|
|
88
|
+
let(:sqlite_database) { Sequel.sqlite }
|
|
89
|
+
|
|
90
|
+
before do
|
|
91
|
+
sqlite_database.create_table(:apollo_entries) do
|
|
92
|
+
primary_key :id
|
|
93
|
+
String :content
|
|
94
|
+
String :content_type
|
|
95
|
+
Float :confidence
|
|
96
|
+
String :tags
|
|
97
|
+
String :source_agent
|
|
98
|
+
String :status
|
|
99
|
+
String :access_scope
|
|
100
|
+
end
|
|
101
|
+
sqlite_database.create_table(:apollo_relations) do
|
|
102
|
+
primary_key :id
|
|
103
|
+
Integer :from_entry_id
|
|
104
|
+
Integer :to_entry_id
|
|
105
|
+
String :relation_type
|
|
106
|
+
Float :weight
|
|
107
|
+
end
|
|
108
|
+
sqlite_database[:apollo_entries].insert(id: 1, content: 'SQLite root', content_type: 'fact',
|
|
109
|
+
confidence: 0.8, tags: '[]', source_agent: 'worker',
|
|
110
|
+
status: 'confirmed', access_scope: 'global')
|
|
111
|
+
allow(entry_model).to receive(:db).and_return(sqlite_database)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
after { sqlite_database.disconnect }
|
|
115
|
+
|
|
116
|
+
it 'traverses a positive integer and preserves its native type in the reply' do
|
|
117
|
+
result = described_class.handle_request(action: 'traverse', entry_id: 1, reply_to: reply_queue.name)
|
|
118
|
+
|
|
119
|
+
expect(result).to include(success: true, entries: [include(id: 1, content: 'SQLite root', depth: 0, activation: 1.0)])
|
|
120
|
+
expect(replies.first[:result]).to include(success: true, entries: [include(id: 1)])
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it 'returns an empty successful graph for a valid identity that is not present' do
|
|
124
|
+
described_class.handle_request(action: 'traverse', entry_id: 2, reply_to: reply_queue.name)
|
|
125
|
+
|
|
126
|
+
expect(replies.first[:result]).to eq(success: true, entries: [], count: 0)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it 'raises when the broker cannot route the reply instead of acknowledging success' do
|
|
131
|
+
expect do
|
|
132
|
+
described_class.handle_request(action: 'retrieve', query: '', reply_to: "missing-#{SecureRandom.uuid}")
|
|
133
|
+
end.to raise_error(IOError, /reply publication failed/)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it 'propagates publication exceptions to the subscription retry boundary' do
|
|
137
|
+
allow(exchange).to receive(:publish).and_raise(IOError, 'connection lost')
|
|
138
|
+
|
|
139
|
+
expect do
|
|
140
|
+
described_class.handle_request(action: 'retrieve', query: '', reply_to: reply_queue.name)
|
|
141
|
+
end.to raise_error(IOError, 'connection lost')
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it 'raises on a negative publisher confirmation so the request can retry' do
|
|
145
|
+
allow(channel).to receive(:wait_for_confirms).and_return(false)
|
|
146
|
+
|
|
147
|
+
expect do
|
|
148
|
+
described_class.handle_request(action: 'retrieve', query: '', reply_to: reply_queue.name)
|
|
149
|
+
end.to raise_error(IOError, /reply publication failed/)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it 'retires an ambiguously timed-out channel before propagating the failure' do
|
|
153
|
+
allow(channel).to receive(:wait_for_confirms).and_raise(Timeout::Error)
|
|
154
|
+
allow(channel).to receive(:open?).and_return(true)
|
|
155
|
+
expect(channel).to receive(:close)
|
|
156
|
+
|
|
157
|
+
expect do
|
|
158
|
+
described_class.handle_request(action: 'retrieve', query: '', reply_to: reply_queue.name)
|
|
159
|
+
end.to raise_error(Timeout::Error)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
RSpec.describe 'Apollo reembedding and space isolation' do
|
|
9
|
+
let(:reembedding_script) do
|
|
10
|
+
<<~'RUBY'
|
|
11
|
+
require 'sequel'
|
|
12
|
+
require 'rspec'
|
|
13
|
+
require 'spec_helper'
|
|
14
|
+
require 'json'
|
|
15
|
+
db = Sequel.sqlite
|
|
16
|
+
db.create_table(:apollo_entries) do
|
|
17
|
+
primary_key :id
|
|
18
|
+
String :content
|
|
19
|
+
String :content_hash
|
|
20
|
+
String :content_type
|
|
21
|
+
String :status
|
|
22
|
+
String :access_scope
|
|
23
|
+
Integer :identity_principal_id
|
|
24
|
+
Integer :identity_id
|
|
25
|
+
String :identity_canonical_name
|
|
26
|
+
Float :confidence
|
|
27
|
+
String :tags
|
|
28
|
+
String :source_agent
|
|
29
|
+
String :source_context
|
|
30
|
+
String :knowledge_domain
|
|
31
|
+
Integer :access_count, default: 0
|
|
32
|
+
String :embedding
|
|
33
|
+
String :embedding_space_id
|
|
34
|
+
Integer :embedding_dimensions
|
|
35
|
+
String :embedding_provenance
|
|
36
|
+
DateTime :created_at
|
|
37
|
+
DateTime :updated_at
|
|
38
|
+
end
|
|
39
|
+
db.run <<~SQL
|
|
40
|
+
CREATE UNIQUE INDEX idx_apollo_content_hash_owner ON apollo_entries (
|
|
41
|
+
content_hash, access_scope,
|
|
42
|
+
(CASE WHEN access_scope = 'global' THEN 0 ELSE COALESCE(identity_principal_id, 0) END)
|
|
43
|
+
) WHERE status != 'archived' AND content_hash IS NOT NULL
|
|
44
|
+
SQL
|
|
45
|
+
db.create_table(:apollo_access_log) do
|
|
46
|
+
primary_key :id
|
|
47
|
+
Integer :entry_id
|
|
48
|
+
String :agent_id
|
|
49
|
+
String :action
|
|
50
|
+
end
|
|
51
|
+
db.create_table(:identities) do
|
|
52
|
+
Integer :id
|
|
53
|
+
Integer :principal_id
|
|
54
|
+
end
|
|
55
|
+
db.create_table(:identity_group_memberships) do
|
|
56
|
+
Integer :principal_id
|
|
57
|
+
Integer :group_id
|
|
58
|
+
end
|
|
59
|
+
Legion::Data.const_set(:Model, Module.new) unless defined?(Legion::Data::Model)
|
|
60
|
+
Legion::Data::Model.const_set(:Apollo, Module.new)
|
|
61
|
+
{ Entry: :apollo_entries, AccessLog: :apollo_access_log }.each do |name, table|
|
|
62
|
+
Legion::Data::Model::Apollo.const_set(name, Class.new(Sequel::Model(db[table])))
|
|
63
|
+
end
|
|
64
|
+
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
65
|
+
input[:entries].each do |entry|
|
|
66
|
+
profile = entry.delete(:embedding_profile)
|
|
67
|
+
attributes = {
|
|
68
|
+
content_type: 'fact', status: 'confirmed', access_scope: 'global', confidence: 0.8,
|
|
69
|
+
tags: '[]', source_agent: 'migration-fixture', knowledge_domain: 'general',
|
|
70
|
+
source_context: ::JSON.generate(origin: 'preserve'), created_at: Time.now,
|
|
71
|
+
content_hash: Legion::Extensions::Apollo::Helpers::Writeback.content_hash(content: entry[:content])
|
|
72
|
+
}.merge(entry)
|
|
73
|
+
if profile
|
|
74
|
+
embedding = ApolloSpecEmbedding.result(**profile)
|
|
75
|
+
attributes.merge!(embedding: ::JSON.generate(embedding[:vector]), embedding_dimensions: embedding[:dimensions],
|
|
76
|
+
embedding_space_id: embedding[:embedding_provenance][:space_id],
|
|
77
|
+
embedding_provenance: ::JSON.generate(embedding[:embedding_provenance]))
|
|
78
|
+
end
|
|
79
|
+
db[:apollo_entries].insert(attributes)
|
|
80
|
+
end
|
|
81
|
+
Legion.const_set(:LLM, Module.new) unless defined?(Legion::LLM)
|
|
82
|
+
Legion::LLM.const_set(:Call, Module.new) unless defined?(Legion::LLM::Call)
|
|
83
|
+
Legion::LLM::Call.const_set(:Embeddings, Module.new) unless defined?(Legion::LLM::Call::Embeddings)
|
|
84
|
+
selected_profile = input.fetch(:generated_profile, {})
|
|
85
|
+
Legion::LLM::Call::Embeddings.define_singleton_method(:generate) do |text:, task:, **|
|
|
86
|
+
raise 'provider call inside persistence transaction' if db.in_transaction?
|
|
87
|
+
raise Legion::LLM::LLMError, 'provider unavailable for document' if Array(input[:provider_failures]).include?(text)
|
|
88
|
+
|
|
89
|
+
if task.to_s == 'document' && input[:concurrent_update]
|
|
90
|
+
update = input.delete(:concurrent_update)
|
|
91
|
+
db[:apollo_entries].where(id: update[:id]).update(
|
|
92
|
+
content: update[:content],
|
|
93
|
+
content_hash: Legion::Extensions::Apollo::Helpers::Writeback.content_hash(content: update[:content])
|
|
94
|
+
)
|
|
95
|
+
end
|
|
96
|
+
ApolloSpecEmbedding.result(task: task, **selected_profile)
|
|
97
|
+
end
|
|
98
|
+
before = db[:apollo_entries].order(:id).all
|
|
99
|
+
maintenance = Legion::Extensions::Apollo::Runners::Maintenance
|
|
100
|
+
results = input.fetch(:batches, []).map { |options| maintenance.reembed_entries(**options) }
|
|
101
|
+
persisted = db[:apollo_entries].order(:id).all
|
|
102
|
+
knowledge = Legion::Extensions::Apollo::Runners::Knowledge
|
|
103
|
+
queries = input.fetch(:query_profiles, []).map do |profile|
|
|
104
|
+
selected_profile = profile
|
|
105
|
+
knowledge.handle_query(query: 'migration evidence', requesting_principal_id: 42, min_confidence: 0.0)
|
|
106
|
+
end
|
|
107
|
+
state = { results: results, queries: queries, before: before, persisted: persisted, entries: db[:apollo_entries].order(:id).all }
|
|
108
|
+
puts "APOLLO_RESULT=#{::JSON.generate(state)}"
|
|
109
|
+
RUBY
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def reembedding_state(entries:, **)
|
|
113
|
+
stdout, stderr, status = Open3.capture3(
|
|
114
|
+
RbConfig.ruby, '-Ilib', '-Ispec', '-e', reembedding_script,
|
|
115
|
+
chdir: File.expand_path('../../../../..', __dir__),
|
|
116
|
+
stdin_data: JSON.generate(entries: entries, **)
|
|
117
|
+
)
|
|
118
|
+
marker = 'APOLLO_RESULT='
|
|
119
|
+
raise "Reembedding process failed: #{stderr}\n#{stdout}" unless status.success? && stdout.include?(marker)
|
|
120
|
+
|
|
121
|
+
ApolloSpecOutput.parse(stdout)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'regenerates legacy documents in resumable bounded batches without changing archived or known-space entries' do
|
|
125
|
+
state = reembedding_state(
|
|
126
|
+
entries: [
|
|
127
|
+
{ id: 1, content: 'legacy vector', embedding: '[0.6,0.8]', embedding_dimensions: 2 },
|
|
128
|
+
{ id: 2, content: 'legacy missing vector', status: 'candidate' },
|
|
129
|
+
{ id: 3, content: 'archived legacy', status: 'archived', embedding: '[0.6,0.8]' },
|
|
130
|
+
{ id: 4, content: 'known space', embedding_profile: { model: 'retained', vector: [0.6, 0.8] } }
|
|
131
|
+
],
|
|
132
|
+
batches: [{ limit: 1 }, { limit: 1, after_id: 1 }, { limit: 1, after_id: 2 }]
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
expect(state[:results]).to eq([
|
|
136
|
+
{ success: true, scanned: 1, updated: 1, skipped: 0, failures: [], next_after_id: 1 },
|
|
137
|
+
{ success: true, scanned: 1, updated: 1, skipped: 0, failures: [], next_after_id: 2 },
|
|
138
|
+
{ success: true, scanned: 0, updated: 0, skipped: 0, failures: [], next_after_id: nil }
|
|
139
|
+
])
|
|
140
|
+
state[:entries].first(2).each_with_index do |entry, index|
|
|
141
|
+
expect(JSON.parse(entry[:embedding])).to eq([1.0, 0.0])
|
|
142
|
+
expect(JSON.parse(entry[:embedding_provenance], symbolize_names: true)).to eq(ApolloSpecEmbedding.result[:embedding_provenance])
|
|
143
|
+
expect(entry.except(:embedding, :embedding_space_id, :embedding_dimensions, :embedding_provenance))
|
|
144
|
+
.to eq(state[:before][index].except(:embedding, :embedding_space_id, :embedding_dimensions, :embedding_provenance))
|
|
145
|
+
end
|
|
146
|
+
expect(state[:entries].last(2)).to eq(state[:before].last(2))
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it 'migrates the selected model space and dimensions without altering entry identity, ownership, or content' do
|
|
150
|
+
old_profile = { model: 'old-model', vector: [0.6, 0.8] }
|
|
151
|
+
new_profile = { provider: 'new-provider', model: 'new-model', instance: 'new-instance', vector: [0.2, 0.4, 0.8] }
|
|
152
|
+
old_space = ApolloSpecEmbedding.result(**old_profile)[:embedding_provenance][:space_id]
|
|
153
|
+
state = reembedding_state(
|
|
154
|
+
entries: [
|
|
155
|
+
{ id: 7, content: 'private retained content', content_hash: 'original-hash', access_scope: 'private',
|
|
156
|
+
identity_principal_id: 42, identity_id: 9, identity_canonical_name: 'owner', embedding_profile: old_profile },
|
|
157
|
+
{ id: 8, content: 'archived old content', status: 'archived', embedding_profile: old_profile },
|
|
158
|
+
{ id: 9, content: 'unselected space', embedding_profile: { model: 'unselected' } }
|
|
159
|
+
],
|
|
160
|
+
generated_profile: new_profile,
|
|
161
|
+
batches: [{ legacy_only: false, from_space_id: old_space, provider: 'new-provider', model: 'new-model',
|
|
162
|
+
instance: 'new-instance', dimensions: 3 }]
|
|
163
|
+
)
|
|
164
|
+
migrated = state[:entries].first
|
|
165
|
+
expected = ApolloSpecEmbedding.result(**new_profile)
|
|
166
|
+
|
|
167
|
+
expect(state[:results].first).to include(success: true, scanned: 1, updated: 1, skipped: 0, failures: [])
|
|
168
|
+
expect(JSON.parse(migrated[:embedding])).to eq(expected[:vector])
|
|
169
|
+
expect(JSON.parse(migrated[:embedding_provenance], symbolize_names: true)).to eq(expected[:embedding_provenance])
|
|
170
|
+
expect(migrated).to include(embedding_dimensions: 3, embedding_space_id: expected[:embedding_provenance][:space_id])
|
|
171
|
+
expect(migrated.except(:embedding, :embedding_space_id, :embedding_dimensions, :embedding_provenance))
|
|
172
|
+
.to eq(state[:before].first.except(:embedding, :embedding_space_id, :embedding_dimensions, :embedding_provenance))
|
|
173
|
+
expect(state[:entries].last(2)).to eq(state[:before].last(2))
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it 'reports partial provider failure and keeps the failed vector usable while continuing the batch' do
|
|
177
|
+
old_profile = { model: 'old-model', vector: [0.6, 0.8] }
|
|
178
|
+
state = reembedding_state(
|
|
179
|
+
entries: [{ id: 1, content: 'provider failure', embedding_profile: old_profile },
|
|
180
|
+
{ id: 2, content: 'provider success', embedding_profile: old_profile }],
|
|
181
|
+
generated_profile: { model: 'replacement', vector: [0.2, 0.4, 0.8] },
|
|
182
|
+
provider_failures: ['provider failure'], batches: [{ legacy_only: false, limit: 2 }],
|
|
183
|
+
query_profiles: [old_profile, { model: 'replacement', vector: [0.2, 0.4, 0.8] }]
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
expect(state[:results].first).to eq(success: false, scanned: 2, updated: 1, skipped: 0, next_after_id: 2,
|
|
187
|
+
failures: [{ entry_id: 1, error: 'provider unavailable for document' }])
|
|
188
|
+
expect(state[:persisted].first).to eq(state[:before].first)
|
|
189
|
+
expect(state[:queries]).to all(include(success: true))
|
|
190
|
+
expect(state[:queries].map { |result| result[:entries].map { |entry| entry[:id] } }).to eq([[1], [2]])
|
|
191
|
+
expect(JSON.parse(state[:entries].last[:embedding])).to eq([0.2, 0.4, 0.8])
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it 'does not compare identical coordinates from different models or legacy and different-dimensional spaces' do
|
|
195
|
+
first_profile = { model: 'first-model', vector: [1.0, 0.0] }
|
|
196
|
+
other_profile = { model: 'other-model', vector: [1.0, 0.0] }
|
|
197
|
+
dimension_profile = { model: 'first-model', vector: [1.0, 0.0, 0.0] }
|
|
198
|
+
state = reembedding_state(
|
|
199
|
+
entries: [{ id: 1, content: 'first space', embedding_profile: first_profile },
|
|
200
|
+
{ id: 2, content: 'other space', embedding_profile: other_profile },
|
|
201
|
+
{ id: 3, content: 'unknown space', embedding: '[1.0,0.0]', embedding_dimensions: 2 },
|
|
202
|
+
{ id: 4, content: 'other dimension', embedding_profile: dimension_profile }],
|
|
203
|
+
query_profiles: [first_profile, other_profile, dimension_profile]
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
expect(state[:queries]).to all(include(success: true))
|
|
207
|
+
expect(state[:queries].map { |result| result[:entries].map { |entry| entry[:id] } }).to eq([[1], [2], [4]])
|
|
208
|
+
expect(state[:entries][2]).to eq(state[:before][2])
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
it 'does not attach a generated vector to content changed while the provider was working' do
|
|
212
|
+
old_profile = { model: 'old-model', vector: [0.6, 0.8] }
|
|
213
|
+
state = reembedding_state(
|
|
214
|
+
entries: [{ id: 1, content: 'original content', embedding_profile: old_profile }],
|
|
215
|
+
generated_profile: { model: 'replacement', vector: [0.2, 0.4, 0.8] },
|
|
216
|
+
concurrent_update: { id: 1, content: 'concurrently changed content' },
|
|
217
|
+
batches: [{ legacy_only: false }]
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
expect(state[:results].first).to include(success: true, scanned: 1, updated: 0, skipped: 1, failures: [])
|
|
221
|
+
expect(state[:entries].first).to include(
|
|
222
|
+
content: 'concurrently changed content',
|
|
223
|
+
content_hash: Legion::Extensions::Apollo::Helpers::Writeback.content_hash(content: 'concurrently changed content')
|
|
224
|
+
)
|
|
225
|
+
expect(state[:entries].first.except(:content, :content_hash)).to eq(state[:before].first.except(:content, :content_hash))
|
|
226
|
+
end
|
|
227
|
+
end
|