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,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sequel'
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
require 'legion/extensions/apollo/runners/search'
|
|
6
|
+
|
|
7
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::Search do
|
|
8
|
+
let(:storage) { Object.new.extend(Legion::Extensions::Apollo::Helpers::Storage) }
|
|
9
|
+
let(:db) { Sequel.sqlite }
|
|
10
|
+
let(:provenance) do
|
|
11
|
+
{ space_id: 'a' * 64, dimensions: 2, provider: 'test', model: 'embedding', instance: 'local',
|
|
12
|
+
model_revision: nil, identity_source: 'instance', task: 'document',
|
|
13
|
+
prefixes: { document: '', query: '' }, normalization: 'none' }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
before do
|
|
17
|
+
allow(described_class).to receive(:settings).and_return(Legion::Extensions::Apollo.default_settings)
|
|
18
|
+
db.create_table(:apollo_entries) do
|
|
19
|
+
primary_key :id
|
|
20
|
+
String :content
|
|
21
|
+
String :content_type
|
|
22
|
+
Float :confidence
|
|
23
|
+
String :tags
|
|
24
|
+
String :source_agent
|
|
25
|
+
Integer :access_count, default: 0
|
|
26
|
+
DateTime :created_at
|
|
27
|
+
String :knowledge_domain
|
|
28
|
+
String :status
|
|
29
|
+
String :access_scope
|
|
30
|
+
Integer :identity_principal_id
|
|
31
|
+
Integer :identity_id
|
|
32
|
+
String :embedding
|
|
33
|
+
String :embedding_space_id
|
|
34
|
+
Integer :embedding_dimensions
|
|
35
|
+
String :embedding_provenance
|
|
36
|
+
end
|
|
37
|
+
db.create_table(:identities) do
|
|
38
|
+
primary_key :id
|
|
39
|
+
Integer :principal_id
|
|
40
|
+
end
|
|
41
|
+
db.create_table(:identity_group_memberships) do
|
|
42
|
+
Integer :principal_id
|
|
43
|
+
Integer :group_id
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
after { db.disconnect }
|
|
48
|
+
|
|
49
|
+
def insert_entry(vector: [1.0, 0.0], vector_provenance: provenance, tags: [], **attributes)
|
|
50
|
+
db[:apollo_entries].insert({ content: 'entry', content_type: 'fact', confidence: 0.9,
|
|
51
|
+
status: 'confirmed', access_scope: 'global',
|
|
52
|
+
embedding: storage.send(:storage_serialize_embedding, db: db, vector: vector),
|
|
53
|
+
embedding_space_id: vector_provenance&.dig(:space_id),
|
|
54
|
+
embedding_dimensions: vector_provenance&.dig(:dimensions),
|
|
55
|
+
embedding_provenance: storage.send(:storage_serialize_provenance, db: db, provenance: vector_provenance),
|
|
56
|
+
tags: storage.send(:storage_serialize_tags, db: db, tags: tags) }.merge(attributes))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def search(**filters)
|
|
60
|
+
described_class.semantic_search(db: db, embedding: [1.0, 0.0], provenance: provenance.merge(task: 'query'), **filters).fetch(:entries)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'ranks every eligible row before taking top-k and preserves native ids and decoded tags' do
|
|
64
|
+
12.times { insert_entry(vector: [0.0, 1.0]) }
|
|
65
|
+
winner = insert_entry(tags: ['best'])
|
|
66
|
+
tied = insert_entry(tags: ['tie'])
|
|
67
|
+
|
|
68
|
+
results = search(limit: 2)
|
|
69
|
+
expect(results.map { |row| row[:id] }).to eq([winner, tied])
|
|
70
|
+
expect(results.map { |row| row[:distance] }).to eq([0.0, 0.0])
|
|
71
|
+
expect(results.first[:tags]).to eq(['best'])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'applies status, confidence, domain, source and visibility filters before top-k' do
|
|
75
|
+
insert_entry(confidence: 0.1, knowledge_domain: 'facts')
|
|
76
|
+
insert_entry(status: 'archived', knowledge_domain: 'facts')
|
|
77
|
+
insert_entry(knowledge_domain: 'other')
|
|
78
|
+
insert_entry(knowledge_domain: 'facts', source_agent: 'excluded')
|
|
79
|
+
insert_entry(knowledge_domain: 'facts', access_scope: 'private', identity_principal_id: 22)
|
|
80
|
+
winner = insert_entry(vector: [0.8, 0.6], knowledge_domain: 'facts')
|
|
81
|
+
|
|
82
|
+
results = search(limit: 1, min_confidence: 0.5, domain: 'facts', exclude_source_agents: ['excluded'], requesting_principal_id: 11)
|
|
83
|
+
expect(results.map { |row| row[:id] }).to eq([winner])
|
|
84
|
+
expect(results.first[:distance]).to be_within(0.00001).of(0.2)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'ranks contradiction candidates within the exact scope and owner before limiting' do
|
|
88
|
+
insert_entry(access_scope: 'global')
|
|
89
|
+
insert_entry(access_scope: 'private', identity_principal_id: 22)
|
|
90
|
+
insert_entry(access_scope: 'team', identity_principal_id: 11)
|
|
91
|
+
own = insert_entry(vector: [0.8, 0.6], access_scope: 'private', identity_principal_id: 11)
|
|
92
|
+
|
|
93
|
+
results = described_class.semantic_search(db: db, embedding: [1.0, 0.0], provenance: provenance,
|
|
94
|
+
limit: 1, access_scope: 'private', identity_principal_id: 11).fetch(:entries)
|
|
95
|
+
expect(results).to contain_exactly(include(id: own, access_scope: 'private', identity_principal_id: 11))
|
|
96
|
+
end
|
|
97
|
+
it 'honors private ownership, identity ownership and team membership without exposing other owners' do
|
|
98
|
+
global = insert_entry
|
|
99
|
+
private_entry = insert_entry(access_scope: 'private', identity_principal_id: 11)
|
|
100
|
+
identity = db[:identities].insert(principal_id: 11)
|
|
101
|
+
identity_entry = insert_entry(access_scope: 'private', identity_id: identity)
|
|
102
|
+
team = insert_entry(access_scope: 'team', identity_principal_id: 22)
|
|
103
|
+
db[:identity_group_memberships].multi_insert([{ principal_id: 11, group_id: 1 }, { principal_id: 22, group_id: 1 }])
|
|
104
|
+
insert_entry(access_scope: 'private', identity_principal_id: 22)
|
|
105
|
+
insert_entry(access_scope: 'team', identity_principal_id: 33)
|
|
106
|
+
insert_entry(access_scope: 'private')
|
|
107
|
+
|
|
108
|
+
expect(search(requesting_principal_id: 11).map { |row| row[:id] }).to eq([global, private_entry, identity_entry, team])
|
|
109
|
+
expect(search(requesting_principal_id: 0).map { |row| row[:id] }).to eq([global])
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it 'matches any and all tags safely with quoted values' do
|
|
113
|
+
quoted = "x'); DROP TABLE apollo_entries; --"
|
|
114
|
+
both = insert_entry(tags: ['ruby', quoted])
|
|
115
|
+
one = insert_entry(tags: ['ruby'])
|
|
116
|
+
insert_entry(tags: ['else'])
|
|
117
|
+
insert_entry(tags: [])
|
|
118
|
+
|
|
119
|
+
%i[any all].each do |mode|
|
|
120
|
+
expected = mode == :all ? [both] : [both, one]
|
|
121
|
+
expect(search(tags: ['ruby', quoted], tag_match: mode).map { |row| row[:id] }).to eq(expected)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it 'excludes legacy, different-space, inconsistent and malformed vectors without comparison' do
|
|
126
|
+
insert_entry(vector_provenance: nil)
|
|
127
|
+
insert_entry(vector_provenance: provenance.merge(space_id: 'b' * 64))
|
|
128
|
+
insert_entry(vector: [1.0], vector_provenance: provenance.merge(dimensions: 1))
|
|
129
|
+
insert_entry(vector: [1.0])
|
|
130
|
+
insert_entry(vector: [0.0, 0.0])
|
|
131
|
+
insert_entry(embedding: 'broken')
|
|
132
|
+
insert_entry(vector_provenance: provenance.merge(space_id: 'b' * 64), embedding_space_id: provenance[:space_id])
|
|
133
|
+
valid = insert_entry(vector: [0.8, 0.6])
|
|
134
|
+
|
|
135
|
+
expect(search(limit: 1).map { |row| row[:id] }).to eq([valid])
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it 'rejects a query without valid provenance rather than searching a guessed vector space' do
|
|
139
|
+
expect { described_class.semantic_search(db: db, embedding: [1.0, 0.0], provenance: nil) }.to raise_error(ArgumentError)
|
|
140
|
+
expect { search(limit: 0) }.to raise_error(ArgumentError)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'returns the successful result envelope with an accurate count' do
|
|
144
|
+
winner = insert_entry
|
|
145
|
+
expect(described_class.semantic_search(db: db, embedding: [1.0, 0.0], provenance: provenance))
|
|
146
|
+
.to include(success: true, count: 1, entries: [include(id: winner)])
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it 'raises for malformed stored tags instead of reporting an empty successful search' do
|
|
150
|
+
insert_entry(tags: ['valid'])
|
|
151
|
+
id = insert_entry
|
|
152
|
+
db[:apollo_entries].where(id: id).update(tags: 'malformed')
|
|
153
|
+
expect { search }.to raise_error(Legion::JSON::ParseError)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'legion/extensions/apollo/runners/writeback'
|
|
5
|
+
require 'open3'
|
|
6
|
+
require 'rbconfig'
|
|
7
|
+
require 'tmpdir'
|
|
8
|
+
|
|
9
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::Writeback do
|
|
10
|
+
let(:provenance) do
|
|
11
|
+
{ space_id: 'a' * 64, provider: 'embed-provider', model: 'embed-model', instance: 'local',
|
|
12
|
+
model_revision: nil, identity_source: 'instance', dimensions: 2, task: 'document',
|
|
13
|
+
prefixes: { document: '', query: '' }, normalization: 'none' }
|
|
14
|
+
end
|
|
15
|
+
let(:payload) do
|
|
16
|
+
{ content: 'test content', content_type: 'observation', tags: ['test'], source_provider: 'producer',
|
|
17
|
+
context: { request_id: 'request' }, source_model: 'producer-model' }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'forwards supplied document vectors to the store route through real isolated transport' do
|
|
21
|
+
script = <<~'RUBY'
|
|
22
|
+
require 'legion/settings'
|
|
23
|
+
Legion::Settings.load
|
|
24
|
+
require 'legion/extensions/apollo'
|
|
25
|
+
Legion::Settings.loader.settings[:extensions] = { apollo: Legion::Extensions::Apollo.default_settings }
|
|
26
|
+
Legion::Transport::Connection.setup
|
|
27
|
+
channel = Legion::Transport::Connection.session.create_channel
|
|
28
|
+
queue = Legion::Transport::CONNECTOR::Queue.new(channel, 'apollo-vectorized-writeback')
|
|
29
|
+
queue.bind('legion.apollo', routing_key: 'legion.apollo.writeback.store')
|
|
30
|
+
received = []
|
|
31
|
+
queue.subscribe(manual_ack: false) { |_, _, body| received << Legion::JSON.load(body) }
|
|
32
|
+
payload = Legion::JSON.load(STDIN.read)
|
|
33
|
+
result = Legion::Extensions::Apollo::Runners::Writeback.handle_vectorize(**payload)
|
|
34
|
+
Legion::Transport::Connection.shutdown
|
|
35
|
+
puts "APOLLO_RESULT=#{Legion::JSON.dump({ result: result, received: received })}"
|
|
36
|
+
RUBY
|
|
37
|
+
input = payload.merge(identity_principal_id: 41, identity_id: nil, identity_canonical_name: 'producer@example.test',
|
|
38
|
+
embedding: [0.6, 0.8], embedding_provenance: provenance,
|
|
39
|
+
routing_key: 'legion.apollo.writeback.vectorize', exchange: 'wrong-inbound-exchange')
|
|
40
|
+
Dir.mktmpdir('apollo-writeback-') do |home|
|
|
41
|
+
stdout, stderr, status = Open3.capture3(
|
|
42
|
+
{ 'HOME' => home, 'LEGION_SETTINGS_DIRS' => home, 'LEGION_DNS_BOOTSTRAP' => 'false', 'LEGION_MODE' => 'lite' },
|
|
43
|
+
RbConfig.ruby, '-I', File.expand_path('../../../../../lib', __dir__), '-e', script,
|
|
44
|
+
stdin_data: Legion::JSON.dump(input), chdir: home
|
|
45
|
+
)
|
|
46
|
+
marker = 'APOLLO_RESULT='
|
|
47
|
+
raise "Writeback process failed: #{stderr}\n#{stdout}" unless status.success? && stdout.include?(marker)
|
|
48
|
+
|
|
49
|
+
state = Legion::JSON.load(stdout.split(marker, 2).last)
|
|
50
|
+
expect(state[:result]).to include(success: true, action: 'vectorized')
|
|
51
|
+
expect(state[:received]).to contain_exactly(
|
|
52
|
+
hash_including(content: payload[:content], identity_principal_id: 41, identity_id: nil,
|
|
53
|
+
identity_canonical_name: 'producer@example.test', context: payload[:context],
|
|
54
|
+
embedding: [0.6, 0.8], embedding_provenance: provenance)
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'raises an embedding failure rather than acknowledging the delivery' do
|
|
60
|
+
error = Legion::Extensions::Apollo::Helpers::Embedding::Error
|
|
61
|
+
allow(Legion::Extensions::Apollo::Runners::Embedding).to receive(:document).and_raise(error, 'invalid vector')
|
|
62
|
+
|
|
63
|
+
expect { described_class.handle_vectorize(**payload) }.to raise_error(error, 'invalid vector')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'raises when publication is unchecked or rejected' do
|
|
67
|
+
allow(Legion::Extensions::Apollo::Runners::Embedding).to receive(:document)
|
|
68
|
+
.and_return(vector: [0.6, 0.8], provenance: provenance)
|
|
69
|
+
allow(described_class).to receive(:capability_can_write?).and_return(false)
|
|
70
|
+
message = instance_double(Legion::Extensions::Apollo::Transport::Messages::Writeback)
|
|
71
|
+
allow(Legion::Extensions::Apollo::Transport::Messages::Writeback).to receive(:new).and_return(message)
|
|
72
|
+
|
|
73
|
+
[nil, { accepted: false, status: :unroutable }].each do |publication_result|
|
|
74
|
+
allow(message).to receive(:publish!).and_return(publication_result)
|
|
75
|
+
|
|
76
|
+
expect { described_class.handle_vectorize(**payload) }.to raise_error(IOError)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'raises when local ingestion rejects the vectorized payload' do
|
|
81
|
+
allow(Legion::Extensions::Apollo::Runners::Embedding).to receive(:document)
|
|
82
|
+
.and_return(vector: [0.6, 0.8], provenance: provenance)
|
|
83
|
+
allow(described_class).to receive(:capability_can_write?).and_return(true)
|
|
84
|
+
knowledge = Module.new do
|
|
85
|
+
def self.handle_ingest(**)
|
|
86
|
+
{ success: false, error: :invalid_access_scope }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
stub_const('Legion::Extensions::Apollo::Runners::Knowledge', knowledge)
|
|
90
|
+
|
|
91
|
+
expect { described_class.handle_vectorize(**payload) }.to raise_error(IOError)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -1,65 +1,40 @@
|
|
|
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/ingest'
|
|
33
5
|
|
|
34
6
|
RSpec.describe Legion::Extensions::Apollo::Transport::Messages::Ingest do
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
content:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
7
|
+
it 'serializes ownership, provenance, context, and a supplied embedding without losing fields' do
|
|
8
|
+
payload = {
|
|
9
|
+
content: 'test fact', content_type: 'fact', tags: ['vault'], source_agent: 'worker-1',
|
|
10
|
+
source_provider: 'anthropic', source_channel: 'chat', knowledge_domain: 'security',
|
|
11
|
+
submitted_by: 'operator', submitted_from: 'terminal', content_hash: 'digest',
|
|
12
|
+
context: { request_id: 'request-1' }, access_scope: 'private', identity_principal_id: 17,
|
|
13
|
+
identity_id: 23, identity_canonical_name: 'operator@example.test', embedding: [0.1, 0.2],
|
|
14
|
+
embedding_provenance: { space_id: 'a' * 64, dimensions: 2, model: 'embed-model' }, source_model: 'producer-model',
|
|
15
|
+
reply_to: 'reply-queue', correlation_id: 'ingest-123'
|
|
16
|
+
}
|
|
17
|
+
message = described_class.new(**payload, encrypt: false)
|
|
18
|
+
|
|
19
|
+
expect(Legion::JSON.load(message.encode_message)).to eq(payload)
|
|
20
|
+
expect(message.publish_envelope_options(options: payload)).to include(
|
|
21
|
+
routing_key: 'legion.apollo.ingest', content_type: 'application/json', reply_to: 'reply-queue', correlation_id: 'ingest-123'
|
|
41
22
|
)
|
|
42
23
|
end
|
|
43
24
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
end
|
|
25
|
+
it 'does not synthesize identity keys for an omitted producer identity' do
|
|
26
|
+
message = described_class.new(content: 'test', content_type: 'fact', encrypt: false)
|
|
27
|
+
payload = Legion::JSON.load(message.encode_message)
|
|
49
28
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
expect(msg[:content]).to eq('test fact')
|
|
54
|
-
expect(msg[:content_type]).to eq(:fact)
|
|
55
|
-
expect(msg[:tags]).to eq(%w[vault])
|
|
56
|
-
expect(msg[:source_agent]).to eq('worker-1')
|
|
57
|
-
end
|
|
29
|
+
expect(payload).not_to have_key(:identity_principal_id)
|
|
30
|
+
expect(payload).not_to have_key(:identity_id)
|
|
31
|
+
expect(payload).not_to have_key(:identity_canonical_name)
|
|
58
32
|
end
|
|
59
33
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
34
|
+
it 'preserves explicit null identity so service defaults cannot replace the producer choice' do
|
|
35
|
+
identity = { identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }
|
|
36
|
+
message = described_class.new(content: 'test', content_type: 'fact', **identity, encrypt: false)
|
|
37
|
+
|
|
38
|
+
expect(Legion::JSON.load(message.encode_message)).to include(identity)
|
|
64
39
|
end
|
|
65
40
|
end
|
|
@@ -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
|