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
|
@@ -1,75 +1,34 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
|
-
|
|
5
|
-
unless defined?(Legion::Transport::Message)
|
|
6
|
-
module Legion
|
|
7
|
-
module Transport
|
|
8
|
-
class Message
|
|
9
|
-
attr_reader :options
|
|
10
|
-
|
|
11
|
-
def initialize(**opts)
|
|
12
|
-
@options = opts
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def publish
|
|
16
|
-
{ published: true }
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
class Exchange
|
|
21
|
-
def exchange_name
|
|
22
|
-
'mock'
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
$LOADED_FEATURES << 'legion/transport/message' unless $LOADED_FEATURES.include?('legion/transport/message')
|
|
28
|
-
$LOADED_FEATURES << 'legion/transport/exchange' unless $LOADED_FEATURES.include?('legion/transport/exchange')
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
require 'legion/extensions/apollo/transport/exchanges/apollo'
|
|
32
4
|
require 'legion/extensions/apollo/transport/messages/query'
|
|
33
5
|
|
|
34
6
|
RSpec.describe Legion::Extensions::Apollo::Transport::Messages::Query do
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
action:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
it 'serializes caller filters, identity, action, and reply metadata on the canonical wire' do
|
|
8
|
+
payload = {
|
|
9
|
+
action: 'retrieve', query: 'PKI configuration', limit: 5, min_confidence: 0.3,
|
|
10
|
+
status: ['confirmed'], tags: %w[vault pki], tag_match: 'all', domain: 'infrastructure',
|
|
11
|
+
agent_id: 'worker-1', requesting_principal_id: 42, exclude_source_agents: ['worker-1'],
|
|
12
|
+
reply_to: 'reply-queue', correlation_id: 'corr-123'
|
|
13
|
+
}
|
|
14
|
+
message = described_class.new(**payload, encrypt: false)
|
|
15
|
+
|
|
16
|
+
expect(Legion::JSON.load(message.encode_message)).to eq(payload)
|
|
17
|
+
expect(message.publish_envelope_options(options: payload)).to include(
|
|
18
|
+
routing_key: 'legion.apollo.query', reply_to: 'reply-queue', correlation_id: 'corr-123'
|
|
43
19
|
)
|
|
44
20
|
end
|
|
45
21
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
50
|
-
end
|
|
22
|
+
it 'preserves explicit null status and principal without manufacturing omitted fields' do
|
|
23
|
+
message = described_class.new(action: 'query', query: '', status: nil, requesting_principal_id: nil, encrypt: false)
|
|
24
|
+
payload = Legion::JSON.load(message.encode_message)
|
|
51
25
|
|
|
52
|
-
|
|
53
|
-
it 'includes query params' do
|
|
54
|
-
msg = message.message
|
|
55
|
-
expect(msg[:action]).to eq(:query)
|
|
56
|
-
expect(msg[:query]).to eq('PKI configuration')
|
|
57
|
-
expect(msg[:limit]).to eq(5)
|
|
58
|
-
expect(msg[:reply_to]).to eq('reply-queue')
|
|
59
|
-
expect(msg[:correlation_id]).to eq('corr-123')
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'compacts nil values' do
|
|
63
|
-
simple = described_class.new(action: :query, query: 'test')
|
|
64
|
-
msg = simple.message
|
|
65
|
-
expect(msg).not_to have_key(:tags)
|
|
66
|
-
expect(msg).not_to have_key(:relation_types)
|
|
67
|
-
end
|
|
26
|
+
expect(payload).to eq(action: 'query', query: '', status: nil, requesting_principal_id: nil)
|
|
68
27
|
end
|
|
69
28
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
29
|
+
it 'carries traversal parameters without synthesizing a query' do
|
|
30
|
+
payload = { action: 'traverse', entry_id: 'entry-1', depth: 3, relation_types: ['supports'], agent_id: 'worker-1' }
|
|
31
|
+
|
|
32
|
+
expect(Legion::JSON.load(described_class.new(**payload, encrypt: false).encode_message)).to eq(payload)
|
|
74
33
|
end
|
|
75
34
|
end
|
|
@@ -1,32 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
module Legion
|
|
7
|
-
module Transport
|
|
8
|
-
class Message
|
|
9
|
-
attr_reader :options
|
|
10
|
-
|
|
11
|
-
def initialize(**opts)
|
|
12
|
-
@options = opts
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def publish
|
|
16
|
-
{ published: true }
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
class Exchange
|
|
21
|
-
def exchange_name
|
|
22
|
-
'mock'
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
$LOADED_FEATURES << 'legion/transport/message' unless $LOADED_FEATURES.include?('legion/transport/message')
|
|
28
|
-
$LOADED_FEATURES << 'legion/transport/exchange' unless $LOADED_FEATURES.include?('legion/transport/exchange')
|
|
29
|
-
end
|
|
4
|
+
require 'legion/crypt'
|
|
5
|
+
require 'open3'
|
|
30
6
|
|
|
31
7
|
require 'legion/extensions/apollo/transport/exchanges/apollo'
|
|
32
8
|
require 'legion/extensions/apollo/transport/messages/writeback'
|
|
@@ -51,37 +27,140 @@ RSpec.describe Legion::Extensions::Apollo::Transport::Messages::Writeback do
|
|
|
51
27
|
end
|
|
52
28
|
|
|
53
29
|
describe '#message' do
|
|
54
|
-
it '
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
30
|
+
it 'serializes producer ownership and provenance with a JSON envelope on both routes' do
|
|
31
|
+
metadata = {
|
|
32
|
+
access_scope: 'private', identity_principal_id: 31, identity_id: nil,
|
|
33
|
+
identity_canonical_name: 'producer@example.test', source_provider: 'anthropic',
|
|
34
|
+
source_channel: 'chat', source_model: 'producer-model', content_hash: 'digest', context: { request_id: 'request' },
|
|
35
|
+
embedding: [0.6, 0.8], embedding_provenance: { space_id: 'a' * 64, dimensions: 2, model: 'embed-model' }
|
|
36
|
+
}
|
|
37
|
+
[true, false].each do |has_embedding|
|
|
38
|
+
options = base_opts.merge(metadata).merge(has_embedding: has_embedding, encrypt: false)
|
|
39
|
+
message = described_class.new(**options)
|
|
61
40
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
expect(msg.message).not_to have_key(:knowledge_domain)
|
|
41
|
+
expect(Legion::JSON.load(message.encode_message)).to include(base_opts.merge(metadata))
|
|
42
|
+
expect(message.publish_envelope_options(options: options)).to include(content_type: 'application/json')
|
|
43
|
+
end
|
|
66
44
|
end
|
|
67
45
|
end
|
|
68
46
|
|
|
69
|
-
describe '#
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
47
|
+
describe '#publish' do
|
|
48
|
+
let(:channel) { double('channel', using_publisher_confirmations?: false, confirm_select: true, wait_for_confirms: true, open?: true, close: true) }
|
|
49
|
+
let(:exchange) { double('exchange', name: 'legion.apollo', channel: channel) }
|
|
50
|
+
let(:publications) { [] }
|
|
51
|
+
let(:message) { described_class.new(**base_opts, encrypt: false, correlation_id: 'request-123') }
|
|
52
|
+
let(:return_state) { {} }
|
|
53
|
+
|
|
54
|
+
before do
|
|
55
|
+
allow(Legion::Extensions::Apollo::Transport::Exchanges::Apollo).to receive(:cached_instance).and_return(exchange)
|
|
56
|
+
allow(exchange).to receive(:on_return) { |&callback| return_state[:callback] = callback }
|
|
57
|
+
allow(exchange).to receive(:publish) { |body, **envelope| publications << [body, envelope] }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'reports success only after confirmed publication with the inherited JSON envelope' do
|
|
61
|
+
result = message.publish
|
|
62
|
+
body, envelope = publications.fetch(0)
|
|
63
|
+
expect(result).to include(accepted: true, status: :accepted)
|
|
64
|
+
expect(Legion::JSON.load(body)).to include(base_opts)
|
|
65
|
+
expect(envelope).to include(content_type: 'application/json', content_encoding: 'identity', mandatory: true,
|
|
66
|
+
correlation_id: 'request-123', message_id: result[:message_id])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'publishes ciphertext that decrypts with the emitted IV' do
|
|
70
|
+
encrypted = described_class.new(**base_opts, encrypt: true)
|
|
71
|
+
allow(Legion::Settings).to receive(:dig).and_call_original
|
|
72
|
+
allow(Legion::Settings).to receive(:dig).with(:crypt, :cs_encrypt_ready).and_return(true)
|
|
73
|
+
allow(Legion::Crypt).to receive(:cs).and_return('a' * 32)
|
|
74
|
+
encrypted.publish
|
|
75
|
+
body, envelope = publications.fetch(0)
|
|
76
|
+
expect(envelope).to include(content_encoding: 'encrypted/cs')
|
|
77
|
+
plaintext = Legion::Crypt.decrypt(body, envelope[:headers][:iv])
|
|
78
|
+
expect(Legion::JSON.load(plaintext)).to include(base_opts)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'rejects requested encryption without publishing plaintext when the secret is not ready' do
|
|
82
|
+
allow(Legion::Settings).to receive(:dig).and_call_original
|
|
83
|
+
allow(Legion::Settings).to receive(:dig).with(:crypt, :cs_encrypt_ready).and_return(false)
|
|
84
|
+
encrypted = described_class.new(**base_opts, encrypt: true)
|
|
85
|
+
|
|
86
|
+
expect { encrypted.publish }.to raise_error(Legion::Transport::EncryptionNotReady)
|
|
87
|
+
expect(publications).to eq([])
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it 'raises on mandatory returns even when the broker confirms receipt' do
|
|
91
|
+
allow(exchange).to receive(:publish) do |_body, **envelope|
|
|
92
|
+
return_info = double(reply_code: 312, reply_text: 'NO_ROUTE')
|
|
93
|
+
return_state[:callback].call(return_info, double(headers: envelope[:headers]), nil)
|
|
94
|
+
end
|
|
95
|
+
expect { message.publish }.to raise_error(IOError)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'does not mistake another publication return for this message' do
|
|
99
|
+
allow(exchange).to receive(:publish) do
|
|
100
|
+
return_state[:callback].call(nil, double(headers: {}), nil)
|
|
101
|
+
end
|
|
102
|
+
expect(message.publish[:accepted]).to be true
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'rejects negative and absent broker confirms' do
|
|
106
|
+
[false, nil].each do |confirmation|
|
|
107
|
+
allow(channel).to receive(:wait_for_confirms).and_return(confirmation)
|
|
108
|
+
expect { message.publish }.to raise_error(IOError)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it 'bounds confirm waiting and closes the timed-out channel before retry' do
|
|
113
|
+
closed = false
|
|
114
|
+
allow(channel).to receive(:wait_for_confirms) { sleep 1 }
|
|
115
|
+
allow(channel).to receive(:close) { closed = true }
|
|
116
|
+
expect { message.publish({ publish_confirm_timeout_ms: 10 }) }.to raise_error(Timeout::Error)
|
|
117
|
+
expect(closed).to be true
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'propagates a network failure rather than reporting queued or spooled success' do
|
|
121
|
+
allow(exchange).to receive(:publish).and_raise(IOError, 'disconnected')
|
|
122
|
+
expect { message.publish }.to raise_error(IOError, 'disconnected')
|
|
73
123
|
end
|
|
74
124
|
end
|
|
75
125
|
|
|
126
|
+
it 'delivers confirmed writeback through real in-process transport without a database' do
|
|
127
|
+
script = <<~'RUBY'
|
|
128
|
+
require 'bundler/setup'
|
|
129
|
+
require 'legion/transport'
|
|
130
|
+
require 'legion/extensions/apollo/transport/exchanges/apollo'
|
|
131
|
+
require 'legion/extensions/apollo/transport/messages/writeback'
|
|
132
|
+
Legion::Transport::Connection.setup
|
|
133
|
+
channel = Legion::Transport::Connection.session.create_channel
|
|
134
|
+
queue = Legion::Transport::CONNECTOR::Queue.new(channel, 'writeback-smoke')
|
|
135
|
+
queue.bind('legion.apollo', routing_key: 'legion.apollo.writeback.store')
|
|
136
|
+
deliveries = []
|
|
137
|
+
queue.subscribe(manual_ack: false) do |_delivery, _properties, payload|
|
|
138
|
+
deliveries << Legion::JSON.load(payload)
|
|
139
|
+
end
|
|
140
|
+
result = Legion::Extensions::Apollo::Transport::Messages::Writeback.new(
|
|
141
|
+
content: 'local fact', content_type: 'fact', has_embedding: true,
|
|
142
|
+
embedding: [0.6, 0.8], identity_principal_id: 41, encrypt: false
|
|
143
|
+
).publish
|
|
144
|
+
Legion::Transport::Connection.shutdown
|
|
145
|
+
puts "APOLLO_RESULT=#{Legion::JSON.dump({ result: result, deliveries: deliveries })}"
|
|
146
|
+
RUBY
|
|
147
|
+
stdout, stderr, status = Open3.capture3(
|
|
148
|
+
{ 'LEGION_MODE' => 'lite' }, RbConfig.ruby, '-Ilib', '-e', script,
|
|
149
|
+
chdir: File.expand_path('../../../../../..', __dir__)
|
|
150
|
+
)
|
|
151
|
+
marker = 'APOLLO_RESULT='
|
|
152
|
+
raise "Writeback process failed: #{stderr}\n#{stdout}" unless status.success? && stdout.include?(marker)
|
|
153
|
+
|
|
154
|
+
state = Legion::JSON.load(stdout.split(marker, 2).last)
|
|
155
|
+
expect(state[:result]).to include(accepted: true, status: 'accepted')
|
|
156
|
+
expect(state[:deliveries]).to contain_exactly(
|
|
157
|
+
include(content: 'local fact', content_type: 'fact', embedding: [0.6, 0.8], identity_principal_id: 41)
|
|
158
|
+
)
|
|
159
|
+
end
|
|
160
|
+
|
|
76
161
|
describe '#validate' do
|
|
77
162
|
it 'raises on missing content' do
|
|
78
163
|
expect { described_class.new(**base_opts, content: nil) }.to raise_error(TypeError)
|
|
79
164
|
end
|
|
80
|
-
|
|
81
|
-
it 'passes with valid content' do
|
|
82
|
-
msg = described_class.new(**base_opts)
|
|
83
|
-
msg.validate
|
|
84
|
-
expect(msg.instance_variable_get(:@valid)).to be true
|
|
85
|
-
end
|
|
86
165
|
end
|
|
87
166
|
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
5
|
+
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Transport::SubscriptionDelivery do
|
|
7
|
+
subject(:delivery) { Object.new.extend(described_class) }
|
|
8
|
+
|
|
9
|
+
let(:runner) { double('ingest runner') }
|
|
10
|
+
|
|
11
|
+
before { allow(delivery).to receive(:extension_dispatch_allowed?).and_return(true) }
|
|
12
|
+
|
|
13
|
+
it 'raises on an unsuccessful write so the subscriber cannot acknowledge normal completion' do
|
|
14
|
+
allow(runner).to receive(:handle_ingest).and_return(success: false, error: 'database unavailable')
|
|
15
|
+
|
|
16
|
+
expect { delivery.send(:dispatch_runner, { content: 'retry me' }, runner, :handle_ingest, false, false) }
|
|
17
|
+
.to raise_error(IOError, /database unavailable/)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'rejects a reported error even when a runner supplies a contradictory success flag' do
|
|
21
|
+
allow(runner).to receive(:handle_ingest).and_return(success: true, error: 'write rejected')
|
|
22
|
+
|
|
23
|
+
expect { delivery.send(:dispatch_runner, {}, runner, :handle_ingest, false, false) }
|
|
24
|
+
.to raise_error(IOError, /write rejected/)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'rejects a missing result rather than treating it as a completed write' do
|
|
28
|
+
allow(runner).to receive(:handle_ingest).and_return(nil)
|
|
29
|
+
|
|
30
|
+
expect { delivery.send(:dispatch_runner, {}, runner, :handle_ingest, false, false) }
|
|
31
|
+
.to raise_error(IOError, /Apollo delivery failed/)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'leaves quiescing deliveries retryable without invoking the runner' do
|
|
35
|
+
allow(delivery).to receive(:extension_dispatch_allowed?).and_return(false)
|
|
36
|
+
expect(runner).not_to receive(:handle_ingest)
|
|
37
|
+
|
|
38
|
+
expect { delivery.send(:dispatch_runner, {}, runner, :handle_ingest, false, false) }
|
|
39
|
+
.to raise_error(IOError, /not accepting new work/)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
unless defined?(Legion::Extensions::Transport)
|
|
6
|
+
module Legion
|
|
7
|
+
module Extensions
|
|
8
|
+
module Transport; end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
require 'legion/extensions/apollo/transport'
|
|
14
|
+
|
|
15
|
+
RSpec.describe Legion::Extensions::Apollo::Transport do
|
|
16
|
+
describe '.additional_e_to_q' do
|
|
17
|
+
it 'omits the raw audit binding when GAS is disabled' do
|
|
18
|
+
allow(Legion::Extensions::Apollo).to receive(:settings).and_return(gas: { enabled: false })
|
|
19
|
+
|
|
20
|
+
expect(described_class.additional_e_to_q.map { |binding| binding[:routing_key] }).not_to include('llm.audit.complete')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'binds the audit queue when GAS is enabled' do
|
|
24
|
+
allow(Legion::Extensions::Apollo).to receive(:settings).and_return(gas: { enabled: true })
|
|
25
|
+
|
|
26
|
+
expect(described_class.additional_e_to_q).to include(
|
|
27
|
+
from: Legion::Extensions::Apollo::Transport::Exchanges::LlmAudit,
|
|
28
|
+
to: Legion::Extensions::Apollo::Transport::Queues::GasSubscriber,
|
|
29
|
+
routing_key: 'llm.audit.complete'
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -9,21 +9,50 @@ require 'legion/data/helper'
|
|
|
9
9
|
require 'legion/json/helper'
|
|
10
10
|
require 'legion/transport'
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class UniqueConstraintViolation < Error; end
|
|
12
|
+
require 'sequel'
|
|
13
|
+
require 'sequel/extensions/pg_array'
|
|
14
|
+
require 'digest'
|
|
15
|
+
require 'json'
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
module ApolloSpecOutput
|
|
18
|
+
def self.parse(stdout)
|
|
19
|
+
payload = stdout.split('APOLLO_RESULT=', 2).last
|
|
20
|
+
depth = 0
|
|
21
|
+
in_string = false
|
|
22
|
+
escaped = false
|
|
23
|
+
|
|
24
|
+
payload.each_char.with_index do |character, index|
|
|
25
|
+
if in_string
|
|
26
|
+
if escaped
|
|
27
|
+
escaped = false
|
|
28
|
+
elsif character == '\\'
|
|
29
|
+
escaped = true
|
|
30
|
+
elsif character == '"'
|
|
31
|
+
in_string = false
|
|
32
|
+
end
|
|
33
|
+
elsif character == '"'
|
|
34
|
+
in_string = true
|
|
35
|
+
elsif ['{', '['].include?(character)
|
|
36
|
+
depth += 1
|
|
37
|
+
elsif ['}', ']'].include?(character)
|
|
38
|
+
depth -= 1
|
|
39
|
+
return ::JSON.parse(payload[0..index], symbolize_names: true) if depth.zero?
|
|
40
|
+
end
|
|
24
41
|
end
|
|
25
|
-
|
|
26
|
-
|
|
42
|
+
|
|
43
|
+
::JSON.parse(payload, symbolize_names: true)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
module ApolloSpecEmbedding
|
|
48
|
+
def self.result(vector: [1.0, 0.0], task: :document, model: 'test', provider: 'test', instance: 'test', **)
|
|
49
|
+
provenance = {
|
|
50
|
+
space_id: Digest::SHA256.hexdigest([provider, model, instance, vector.length].join(':')),
|
|
51
|
+
provider: provider, model: model, instance: instance, model_revision: nil, identity_source: 'instance',
|
|
52
|
+
dimensions: vector.length, task: task.to_s, prefixes: { document: '', query: '' }, normalization: 'none'
|
|
53
|
+
}
|
|
54
|
+
{ vector: vector, model: model, provider: provider, instance: instance,
|
|
55
|
+
dimensions: vector.length, tokens: 0, embedding_provenance: provenance }
|
|
27
56
|
end
|
|
28
57
|
end
|
|
29
58
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lex-apollo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.49
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Esity
|
|
@@ -43,14 +43,14 @@ dependencies:
|
|
|
43
43
|
requirements:
|
|
44
44
|
- - ">="
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: 1.
|
|
46
|
+
version: 1.10.29
|
|
47
47
|
type: :runtime
|
|
48
48
|
prerelease: false
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
51
|
- - ">="
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: 1.
|
|
53
|
+
version: 1.10.29
|
|
54
54
|
- !ruby/object:Gem::Dependency
|
|
55
55
|
name: legion-json
|
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -65,6 +65,20 @@ dependencies:
|
|
|
65
65
|
- - ">="
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
67
|
version: 1.2.1
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: legion-llm
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: 0.16.9
|
|
75
|
+
type: :runtime
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: 0.16.9
|
|
68
82
|
- !ruby/object:Gem::Dependency
|
|
69
83
|
name: legion-logging
|
|
70
84
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -99,14 +113,14 @@ dependencies:
|
|
|
99
113
|
requirements:
|
|
100
114
|
- - ">="
|
|
101
115
|
- !ruby/object:Gem::Version
|
|
102
|
-
version: 1.
|
|
116
|
+
version: 1.4.31
|
|
103
117
|
type: :runtime
|
|
104
118
|
prerelease: false
|
|
105
119
|
version_requirements: !ruby/object:Gem::Requirement
|
|
106
120
|
requirements:
|
|
107
121
|
- - ">="
|
|
108
122
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: 1.
|
|
123
|
+
version: 1.4.31
|
|
110
124
|
- !ruby/object:Gem::Dependency
|
|
111
125
|
name: rspec
|
|
112
126
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -204,17 +218,24 @@ files:
|
|
|
204
218
|
- lib/legion/extensions/apollo/helpers/capability.rb
|
|
205
219
|
- lib/legion/extensions/apollo/helpers/confidence.rb
|
|
206
220
|
- lib/legion/extensions/apollo/helpers/data_models.rb
|
|
207
|
-
- lib/legion/extensions/apollo/helpers/
|
|
221
|
+
- lib/legion/extensions/apollo/helpers/embedding.rb
|
|
208
222
|
- lib/legion/extensions/apollo/helpers/graph_query.rb
|
|
209
223
|
- lib/legion/extensions/apollo/helpers/similarity.rb
|
|
224
|
+
- lib/legion/extensions/apollo/helpers/storage.rb
|
|
210
225
|
- lib/legion/extensions/apollo/helpers/tag_normalizer.rb
|
|
211
226
|
- lib/legion/extensions/apollo/helpers/writeback.rb
|
|
227
|
+
- lib/legion/extensions/apollo/runners/contradictions.rb
|
|
228
|
+
- lib/legion/extensions/apollo/runners/embedding.rb
|
|
212
229
|
- lib/legion/extensions/apollo/runners/entity_extractor.rb
|
|
230
|
+
- lib/legion/extensions/apollo/runners/entity_watchdog.rb
|
|
213
231
|
- lib/legion/extensions/apollo/runners/expertise.rb
|
|
214
232
|
- lib/legion/extensions/apollo/runners/gas.rb
|
|
215
233
|
- lib/legion/extensions/apollo/runners/knowledge.rb
|
|
216
234
|
- lib/legion/extensions/apollo/runners/maintenance.rb
|
|
235
|
+
- lib/legion/extensions/apollo/runners/query_response.rb
|
|
217
236
|
- lib/legion/extensions/apollo/runners/request.rb
|
|
237
|
+
- lib/legion/extensions/apollo/runners/search.rb
|
|
238
|
+
- lib/legion/extensions/apollo/runners/writeback.rb
|
|
218
239
|
- lib/legion/extensions/apollo/transport.rb
|
|
219
240
|
- lib/legion/extensions/apollo/transport/exchanges/apollo.rb
|
|
220
241
|
- lib/legion/extensions/apollo/transport/exchanges/llm_audit.rb
|
|
@@ -226,28 +247,28 @@ files:
|
|
|
226
247
|
- lib/legion/extensions/apollo/transport/queues/query.rb
|
|
227
248
|
- lib/legion/extensions/apollo/transport/queues/writeback_store.rb
|
|
228
249
|
- lib/legion/extensions/apollo/transport/queues/writeback_vectorize.rb
|
|
250
|
+
- lib/legion/extensions/apollo/transport/subscription_delivery.rb
|
|
229
251
|
- lib/legion/extensions/apollo/version.rb
|
|
230
|
-
- spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb
|
|
231
|
-
- spec/legion/extensions/apollo/actors/decay_spec.rb
|
|
232
|
-
- spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb
|
|
233
|
-
- spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb
|
|
234
252
|
- spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb
|
|
235
253
|
- spec/legion/extensions/apollo/actors/ingest_spec.rb
|
|
236
|
-
- spec/legion/extensions/apollo/actors/
|
|
237
|
-
- spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb
|
|
254
|
+
- spec/legion/extensions/apollo/actors/query_responder_spec.rb
|
|
238
255
|
- spec/legion/extensions/apollo/api_spec.rb
|
|
239
256
|
- spec/legion/extensions/apollo/client_spec.rb
|
|
240
257
|
- spec/legion/extensions/apollo/contradiction_spec.rb
|
|
241
258
|
- spec/legion/extensions/apollo/gaia_integration_spec.rb
|
|
242
259
|
- spec/legion/extensions/apollo/helpers/capability_spec.rb
|
|
243
260
|
- spec/legion/extensions/apollo/helpers/confidence_spec.rb
|
|
244
|
-
- spec/legion/extensions/apollo/helpers/
|
|
261
|
+
- spec/legion/extensions/apollo/helpers/embedding_spec.rb
|
|
262
|
+
- spec/legion/extensions/apollo/helpers/graph_query_spec.rb
|
|
245
263
|
- spec/legion/extensions/apollo/helpers/similarity_spec.rb
|
|
264
|
+
- spec/legion/extensions/apollo/helpers/storage_spec.rb
|
|
246
265
|
- spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb
|
|
247
266
|
- spec/legion/extensions/apollo/helpers/writeback_spec.rb
|
|
248
267
|
- spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb
|
|
249
268
|
- spec/legion/extensions/apollo/runners/decay_cycle_spec.rb
|
|
269
|
+
- spec/legion/extensions/apollo/runners/embedding_spec.rb
|
|
250
270
|
- spec/legion/extensions/apollo/runners/entity_extractor_spec.rb
|
|
271
|
+
- spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb
|
|
251
272
|
- spec/legion/extensions/apollo/runners/expertise_spec.rb
|
|
252
273
|
- spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb
|
|
253
274
|
- spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb
|
|
@@ -255,13 +276,20 @@ files:
|
|
|
255
276
|
- spec/legion/extensions/apollo/runners/gas_spec.rb
|
|
256
277
|
- spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb
|
|
257
278
|
- spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb
|
|
279
|
+
- spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb
|
|
258
280
|
- spec/legion/extensions/apollo/runners/knowledge_spec.rb
|
|
259
281
|
- spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb
|
|
260
282
|
- spec/legion/extensions/apollo/runners/maintenance_spec.rb
|
|
283
|
+
- spec/legion/extensions/apollo/runners/query_response_spec.rb
|
|
284
|
+
- spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb
|
|
261
285
|
- spec/legion/extensions/apollo/runners/request_spec.rb
|
|
286
|
+
- spec/legion/extensions/apollo/runners/search_spec.rb
|
|
287
|
+
- spec/legion/extensions/apollo/runners/writeback_spec.rb
|
|
262
288
|
- spec/legion/extensions/apollo/transport/messages/ingest_spec.rb
|
|
263
289
|
- spec/legion/extensions/apollo/transport/messages/query_spec.rb
|
|
264
290
|
- spec/legion/extensions/apollo/transport/messages/writeback_spec.rb
|
|
291
|
+
- spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb
|
|
292
|
+
- spec/legion/extensions/apollo/transport_spec.rb
|
|
265
293
|
- spec/spec_helper.rb
|
|
266
294
|
homepage: https://github.com/LegionIO/lex-apollo
|
|
267
295
|
licenses:
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Legion
|
|
4
|
-
module Extensions
|
|
5
|
-
module Apollo
|
|
6
|
-
module Helpers
|
|
7
|
-
module EntityWatchdog
|
|
8
|
-
ENTITY_PATTERNS = {
|
|
9
|
-
person: /\b[A-Z][a-z]+(?:\s[A-Z][a-z]+)+\b/,
|
|
10
|
-
service: %r{\bhttps?://[^\s]+\b},
|
|
11
|
-
repo: %r{\b[a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+\b}
|
|
12
|
-
}.freeze
|
|
13
|
-
|
|
14
|
-
class << self
|
|
15
|
-
include Legion::Logging::Helper
|
|
16
|
-
include Legion::Settings::Helper
|
|
17
|
-
|
|
18
|
-
def detect_entities(text:, types: nil)
|
|
19
|
-
return [] if text.nil? || text.empty?
|
|
20
|
-
|
|
21
|
-
types = (types || default_types).map(&:to_sym)
|
|
22
|
-
entities = []
|
|
23
|
-
|
|
24
|
-
types.each do |type_sym|
|
|
25
|
-
entity_type = type_sym == :repository ? :repo : type_sym
|
|
26
|
-
pattern = entity_type == :concept ? concept_pattern : ENTITY_PATTERNS[entity_type]
|
|
27
|
-
next unless pattern
|
|
28
|
-
|
|
29
|
-
text.scan(pattern).each do |match|
|
|
30
|
-
entities << { type: entity_type, value: match.strip,
|
|
31
|
-
confidence: settings[:entity_watchdog][:detect_confidence] }
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
entities.uniq { |e| [e[:type], e[:value].downcase] }
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def link_or_create(entities:, source_context: nil)
|
|
39
|
-
return { success: true, linked: 0, created: 0 } if entities.nil? || entities.empty?
|
|
40
|
-
|
|
41
|
-
linked = 0
|
|
42
|
-
created = 0
|
|
43
|
-
|
|
44
|
-
entities.each do |entity|
|
|
45
|
-
existing = find_existing(entity)
|
|
46
|
-
if existing
|
|
47
|
-
bump_confidence(existing, source_context)
|
|
48
|
-
linked += 1
|
|
49
|
-
else
|
|
50
|
-
create_candidate(entity, source_context)
|
|
51
|
-
created += 1
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
{ success: true, linked: linked, created: created }
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def concept_pattern
|
|
59
|
-
keywords = settings[:entity_watchdog][:concept_keywords]
|
|
60
|
-
return nil if keywords.empty?
|
|
61
|
-
|
|
62
|
-
Regexp.new("\\b(?:#{keywords.map { |k| Regexp.escape(k) }.join('|')})\\b", Regexp::IGNORECASE)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
private
|
|
66
|
-
|
|
67
|
-
def default_types
|
|
68
|
-
settings[:entity_watchdog][:types]
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def find_existing(_entity)
|
|
72
|
-
return nil unless defined?(Runners::Knowledge) && respond_to?(:retrieve_relevant, true)
|
|
73
|
-
|
|
74
|
-
nil
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def bump_confidence(_entry, _source_context)
|
|
78
|
-
# Increment retrieval confidence on existing Apollo entry
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def create_candidate(entity, _source_context)
|
|
82
|
-
return unless defined?(Runners::Knowledge)
|
|
83
|
-
|
|
84
|
-
log.debug "[entity_watchdog] candidate: #{entity[:type]}=#{entity[:value]}"
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|