lex-apollo 0.4.44 → 0.4.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +72 -14
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -41
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +4 -16
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +96 -41
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -51
- data/lib/legion/extensions/apollo/helpers/confidence.rb +8 -52
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +99 -43
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +64 -102
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +87 -136
- data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
- data/lib/legion/extensions/apollo/runners/maintenance.rb +151 -64
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -46
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +240 -8
- data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +32 -3
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +23 -7
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -77
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +138 -6
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +11 -2
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +33 -65
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +40 -14
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -39
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'sinatra/base'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require 'json'
|
|
4
7
|
|
|
5
8
|
require 'spec_helper'
|
|
6
9
|
require 'legion/extensions/apollo/api'
|
|
7
10
|
|
|
8
11
|
RSpec.describe Legion::Extensions::Apollo::Api do
|
|
9
12
|
describe '.stats_payload' do
|
|
10
|
-
let(:entry_model) { class_double('Legion::Data::Model::
|
|
11
|
-
let(:relation_model) { class_double('Legion::Data::Model::
|
|
13
|
+
let(:entry_model) { class_double('Legion::Data::Model::Apollo::Entry') }
|
|
14
|
+
let(:relation_model) { class_double('Legion::Data::Model::Apollo::Relation', count: 4) }
|
|
12
15
|
let(:status_counts) do
|
|
13
16
|
instance_double(
|
|
14
17
|
'StatusCounts',
|
|
@@ -32,8 +35,8 @@ RSpec.describe Legion::Extensions::Apollo::Api do
|
|
|
32
35
|
let(:recent_entries) { instance_double('RecentEntries', count: 2) }
|
|
33
36
|
|
|
34
37
|
before do
|
|
35
|
-
stub_const('Legion::Data::Model::
|
|
36
|
-
stub_const('Legion::Data::Model::
|
|
38
|
+
stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
|
|
39
|
+
stub_const('Legion::Data::Model::Apollo::Relation', relation_model)
|
|
37
40
|
allow(entry_model).to receive(:count).and_return(6)
|
|
38
41
|
allow(entry_model).to receive(:avg).with(:confidence).and_return(0.81234)
|
|
39
42
|
allow(entry_model).to receive(:exclude).with(status: 'archived').and_return(active_entries)
|
|
@@ -64,7 +67,6 @@ RSpec.describe Legion::Extensions::Apollo::Api do
|
|
|
64
67
|
end
|
|
65
68
|
|
|
66
69
|
it 'keeps entry statistics available when the optional relation model is absent' do
|
|
67
|
-
hide_const('Legion::Data::Model::ApolloRelation') if defined?(Legion::Data::Model::ApolloRelation)
|
|
68
70
|
hide_const('Legion::Data::Model::Apollo::Relation') if defined?(Legion::Data::Model::Apollo::Relation)
|
|
69
71
|
|
|
70
72
|
payload = described_class.stats_payload(now: Time.utc(2026, 4, 28, 12, 0, 0))
|
|
@@ -75,8 +77,8 @@ RSpec.describe Legion::Extensions::Apollo::Api do
|
|
|
75
77
|
|
|
76
78
|
context 'when legion-data exposes namespaced Apollo models' do
|
|
77
79
|
before do
|
|
78
|
-
hide_const('Legion::Data::Model::
|
|
79
|
-
hide_const('Legion::Data::Model::
|
|
80
|
+
hide_const('Legion::Data::Model::Apollo::Entry') if defined?(Legion::Data::Model::Apollo::Entry)
|
|
81
|
+
hide_const('Legion::Data::Model::Apollo::Relation') if defined?(Legion::Data::Model::Apollo::Relation)
|
|
80
82
|
stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
|
|
81
83
|
stub_const('Legion::Data::Model::Apollo::Relation', relation_model)
|
|
82
84
|
end
|
|
@@ -94,9 +96,239 @@ RSpec.describe Legion::Extensions::Apollo::Api do
|
|
|
94
96
|
end
|
|
95
97
|
|
|
96
98
|
it 'returns an apollo data error when the entry model is unavailable' do
|
|
97
|
-
hide_const('Legion::Data::Model::
|
|
99
|
+
hide_const('Legion::Data::Model::Apollo::Entry')
|
|
98
100
|
|
|
99
101
|
expect(described_class.stats_payload).to eq(error: 'apollo_data_not_available')
|
|
100
102
|
end
|
|
101
103
|
end
|
|
104
|
+
|
|
105
|
+
describe 'HTTP identity boundaries' do
|
|
106
|
+
let(:http_script) do
|
|
107
|
+
<<~'RUBY'
|
|
108
|
+
require 'sequel'
|
|
109
|
+
require 'sequel/extensions/pg_array'
|
|
110
|
+
require 'rspec'
|
|
111
|
+
require 'sinatra/base'
|
|
112
|
+
require 'rack/mock'
|
|
113
|
+
require 'spec_helper'
|
|
114
|
+
require 'json'
|
|
115
|
+
require 'legion/extensions/apollo/api'
|
|
116
|
+
module Legion
|
|
117
|
+
module LLM
|
|
118
|
+
module Call
|
|
119
|
+
module Embeddings
|
|
120
|
+
def self.generate(text:, task: :document, **)
|
|
121
|
+
ApolloSpecEmbedding.result(task: task)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
db = Sequel.sqlite
|
|
128
|
+
db.create_table(:apollo_entries) do
|
|
129
|
+
primary_key :id
|
|
130
|
+
%i[content content_type source_agent source_provider source_channel source_context tags status
|
|
131
|
+
knowledge_domain submitted_by submitted_from content_hash access_scope identity_canonical_name embedding
|
|
132
|
+
embedding_space_id embedding_provenance].each do |column|
|
|
133
|
+
String column
|
|
134
|
+
end
|
|
135
|
+
Float :confidence
|
|
136
|
+
Integer :identity_principal_id
|
|
137
|
+
Integer :identity_id
|
|
138
|
+
Integer :embedding_dimensions
|
|
139
|
+
DateTime :created_at
|
|
140
|
+
DateTime :updated_at
|
|
141
|
+
end
|
|
142
|
+
db.create_table(:apollo_expertise) do
|
|
143
|
+
primary_key :id
|
|
144
|
+
String :agent_id
|
|
145
|
+
String :domain
|
|
146
|
+
Float :proficiency
|
|
147
|
+
Integer :entry_count
|
|
148
|
+
DateTime :last_active_at
|
|
149
|
+
end
|
|
150
|
+
db.create_table(:apollo_access_log) do
|
|
151
|
+
primary_key :id
|
|
152
|
+
Integer :entry_id
|
|
153
|
+
String :agent_id
|
|
154
|
+
String :action
|
|
155
|
+
end
|
|
156
|
+
db.create_table(:identity_principals) do
|
|
157
|
+
primary_key :id
|
|
158
|
+
String :canonical_name
|
|
159
|
+
String :kind
|
|
160
|
+
TrueClass :active
|
|
161
|
+
end
|
|
162
|
+
db.create_table(:identity_providers) do
|
|
163
|
+
primary_key :id
|
|
164
|
+
String :name
|
|
165
|
+
end
|
|
166
|
+
db.create_table(:identities) do
|
|
167
|
+
primary_key :id
|
|
168
|
+
Integer :principal_id
|
|
169
|
+
Integer :provider_id
|
|
170
|
+
String :provider_identity_key
|
|
171
|
+
TrueClass :active
|
|
172
|
+
end
|
|
173
|
+
db.create_table(:identity_group_memberships) do
|
|
174
|
+
Integer :principal_id
|
|
175
|
+
Integer :group_id
|
|
176
|
+
end
|
|
177
|
+
Legion::Data.const_set(:Model, Module.new) unless defined?(Legion::Data::Model)
|
|
178
|
+
Legion::Data::Model.const_set(:Apollo, Module.new)
|
|
179
|
+
{ Entry: :apollo_entries, Expertise: :apollo_expertise, AccessLog: :apollo_access_log }.each do |name, table|
|
|
180
|
+
Legion::Data::Model::Apollo.const_set(name, Class.new(Sequel::Model(db[table])))
|
|
181
|
+
end
|
|
182
|
+
Legion::Data::Model.const_set(:Identity, Module.new)
|
|
183
|
+
{ Principal: :identity_principals, Provider: :identity_providers, Identity: :identities }.each do |name, table|
|
|
184
|
+
Legion::Data::Model::Identity.const_set(name, Class.new(Sequel::Model(db[table])))
|
|
185
|
+
end
|
|
186
|
+
Legion::Data.define_singleton_method(:connection) { db }
|
|
187
|
+
Legion.const_set(:Identity, Module.new) unless defined?(Legion::Identity)
|
|
188
|
+
Legion::Identity.const_set(:Process, Module.new)
|
|
189
|
+
Legion::Identity::Process.define_singleton_method(:db_principal_id) { 999 }
|
|
190
|
+
Legion::Identity::Process.define_singleton_method(:db_identity_id) { 998 }
|
|
191
|
+
Legion::Identity::Process.define_singleton_method(:canonical_name) { 'service-node' }
|
|
192
|
+
db[:identity_principals].insert(id: 42, canonical_name: 'alice', kind: 'human', active: true)
|
|
193
|
+
db[:identity_providers].insert(id: 3, name: 'jwt')
|
|
194
|
+
db[:identities].insert(id: 7, principal_id: 42, provider_id: 3, provider_identity_key: '900', active: true)
|
|
195
|
+
base = { content_type: 'fact', status: 'confirmed', confidence: 0.8, access_scope: 'global',
|
|
196
|
+
source_agent: 'human', knowledge_domain: 'engineering', source_context: '{}', created_at: Time.now }
|
|
197
|
+
[
|
|
198
|
+
{ id: 1, content: 'public evidence' },
|
|
199
|
+
{ id: 2, content: 'alice evidence', access_scope: 'private', identity_principal_id: 42 },
|
|
200
|
+
{ id: 3, content: 'mallory evidence', access_scope: 'private', identity_principal_id: 99 },
|
|
201
|
+
{ id: 4, content: 'server evidence', access_scope: 'private', identity_principal_id: 999 },
|
|
202
|
+
{ id: 5, content: 'weak evidence', confidence: 0.2 },
|
|
203
|
+
{ id: 6, content: 'excluded producer', source_agent: 'blocked-agent' },
|
|
204
|
+
{ id: 7, content: 'unconfirmed evidence', status: 'candidate' },
|
|
205
|
+
{ id: 8, content: 'other domain', knowledge_domain: 'other' }
|
|
206
|
+
].each { |row| db[:apollo_entries].insert(base.merge(row)) }
|
|
207
|
+
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
208
|
+
request_type = Struct.new(:canonical_name, :kind, :principal_id, :source, keyword_init: true)
|
|
209
|
+
http = Rack::MockRequest.new(Legion::Extensions::Apollo::Api)
|
|
210
|
+
before = db[:apollo_entries].order(:id).all
|
|
211
|
+
responses = input[:requests].map do |options|
|
|
212
|
+
env = { 'CONTENT_TYPE' => 'application/json' }
|
|
213
|
+
if options[:identity]
|
|
214
|
+
canonical = options[:identity] == 'unmapped' ? 'unknown' : 'alice'
|
|
215
|
+
env['legion.principal'] = request_type.new(canonical_name: canonical, kind: :human, principal_id: '900', source: :jwt)
|
|
216
|
+
env['legion.auth'] = { sub: '900' } unless options[:identity] == 'system'
|
|
217
|
+
end
|
|
218
|
+
env[:input] = ::JSON.generate(options[:body]) if options.key?(:body)
|
|
219
|
+
env[:input] = options[:raw_body] if options.key?(:raw_body)
|
|
220
|
+
response = http.post(options[:path], env)
|
|
221
|
+
{ status: response.status, body: ::JSON.parse(response.body) }
|
|
222
|
+
end
|
|
223
|
+
state = { responses: responses, before: before, entries: db[:apollo_entries].order(:id).all,
|
|
224
|
+
expertise: db[:apollo_expertise].count, access_logs: db[:apollo_access_log].count }
|
|
225
|
+
puts "APOLLO_RESULT=#{::JSON.generate(state)}"
|
|
226
|
+
RUBY
|
|
227
|
+
end
|
|
228
|
+
let(:forged_identity) do
|
|
229
|
+
{ identity_principal_id: 99, identity_id: 88, identity_canonical_name: 'mallory',
|
|
230
|
+
requesting_principal_id: 99, principal_id: 99 }
|
|
231
|
+
end
|
|
232
|
+
let(:browse_body) do
|
|
233
|
+
forged_identity.merge(query: '', min_confidence: 0.7, exclude_source_agents: ['blocked-agent'],
|
|
234
|
+
status: 'confirmed', domain: 'engineering')
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def http_state(requests:, **)
|
|
238
|
+
stdout, stderr, status = Open3.capture3(
|
|
239
|
+
RbConfig.ruby, '-Ilib', '-Ispec', '-e', http_script,
|
|
240
|
+
chdir: File.expand_path('../../../..', __dir__), stdin_data: JSON.generate(requests: requests)
|
|
241
|
+
)
|
|
242
|
+
line = stdout.lines.find { |output| output.start_with?('APOLLO_RESULT=') }
|
|
243
|
+
raise "HTTP identity process failed: #{stderr}\n#{stdout}" unless status.success? && line
|
|
244
|
+
|
|
245
|
+
JSON.parse(line.delete_prefix('APOLLO_RESULT='), symbolize_names: true)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it 'keeps anonymous, synthesized-system, and unmapped callers global-only despite forged ownership' do
|
|
249
|
+
requests = [nil, 'system', 'unmapped'].map do |identity|
|
|
250
|
+
{ path: '/api/apollo/query', body: browse_body.merge(access_scope: 'private'), identity: identity }
|
|
251
|
+
end
|
|
252
|
+
state = http_state(requests: requests)
|
|
253
|
+
|
|
254
|
+
expect(state[:responses].map { |response| response[:status] }).to eq([200, 200, 200])
|
|
255
|
+
state[:responses].each do |response|
|
|
256
|
+
expect(response[:body][:entries].map { |entry| entry[:content] }).to eq(['public evidence'])
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
it 'reads the authenticated canonical owners private content without admitting a forged owner or process identity' do
|
|
261
|
+
state = http_state(requests: [{ path: '/api/apollo/query', body: browse_body, identity: 'authenticated' }])
|
|
262
|
+
response = state[:responses].first
|
|
263
|
+
|
|
264
|
+
expect(response[:status]).to eq(200)
|
|
265
|
+
expect(response[:body][:entries].map { |entry| entry[:content] }).to contain_exactly('public evidence', 'alice evidence')
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
it 'denies anonymous restricted writes and invalid scopes without persisting rows or side effects' do
|
|
269
|
+
requests = %w[private team invalid].map do |scope|
|
|
270
|
+
{ path: '/api/apollo/ingest', body: forged_identity.merge(content: 'secret', content_type: 'fact', access_scope: scope) }
|
|
271
|
+
end
|
|
272
|
+
state = http_state(requests: requests)
|
|
273
|
+
|
|
274
|
+
expect(state[:responses].map { |response| response[:status] }).to eq([403, 403, 400])
|
|
275
|
+
expect(state[:entries]).to eq(state[:before])
|
|
276
|
+
expect(state).to include(expertise: 0, access_logs: 0)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it 'persists authenticated private ownership and provenance rather than forged body identity' do
|
|
280
|
+
provenance = {
|
|
281
|
+
content: 'new private knowledge', content_type: 'fact', access_scope: 'private',
|
|
282
|
+
source_agent: 'agent-a', source_provider: 'provider-a', source_channel: 'channel-a',
|
|
283
|
+
knowledge_domain: 'engineering', submitted_by: 'original author', submitted_from: 'document:12',
|
|
284
|
+
context: { citation: 'page 4' }
|
|
285
|
+
}
|
|
286
|
+
state = http_state(requests: [{ path: '/api/apollo/ingest', body: provenance.merge(forged_identity), identity: 'authenticated' }])
|
|
287
|
+
response = state[:responses].first
|
|
288
|
+
entry = state[:entries].find { |row| row[:id] == response[:body][:entry_id] }
|
|
289
|
+
|
|
290
|
+
expect(response[:status]).to eq(201)
|
|
291
|
+
expect(entry).to include(provenance.except(:context).merge(identity_principal_id: 42, identity_id: 7,
|
|
292
|
+
identity_canonical_name: 'alice'))
|
|
293
|
+
expect(JSON.parse(entry[:source_context], symbolize_names: true)).to include(citation: 'page 4')
|
|
294
|
+
expect(state).to include(expertise: 1, access_logs: 1)
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it 'persists anonymous global content without inheriting the receiving process identity' do
|
|
298
|
+
state = http_state(requests: [{ path: '/api/apollo/ingest',
|
|
299
|
+
body: forged_identity.merge(content: 'new public knowledge', content_type: 'fact') }])
|
|
300
|
+
response = state[:responses].first
|
|
301
|
+
entry = state[:entries].find { |row| row[:id] == response[:body][:entry_id] }
|
|
302
|
+
|
|
303
|
+
expect(response[:status]).to eq(201)
|
|
304
|
+
expect(entry).to include(content: 'new public knowledge', access_scope: 'global',
|
|
305
|
+
identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
it 'hides foreign deprecation targets and leaves their persisted contents unchanged' do
|
|
309
|
+
state = http_state(requests: [
|
|
310
|
+
{ path: '/api/apollo/entries/2/deprecate' },
|
|
311
|
+
{ path: '/api/apollo/entries/3/deprecate', identity: 'authenticated',
|
|
312
|
+
body: forged_identity.merge(reason: 'forged ownership') }
|
|
313
|
+
])
|
|
314
|
+
|
|
315
|
+
expect(state[:responses]).to all(include(status: 404, body: hash_including(error: 'entry_not_found')))
|
|
316
|
+
expect(state[:entries]).to eq(state[:before])
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
it 'retains owner deprecation and JSON errors for missing bodies and malformed JSON' do
|
|
320
|
+
state = http_state(requests: [
|
|
321
|
+
{ path: '/api/apollo/entries/2/deprecate', identity: 'authenticated' },
|
|
322
|
+
{ path: '/api/apollo/query' },
|
|
323
|
+
{ path: '/api/apollo/query', raw_body: '{' }
|
|
324
|
+
])
|
|
325
|
+
|
|
326
|
+
expect(state[:responses].map { |response| response[:status] }).to eq([200, 400, 400])
|
|
327
|
+
entry = state[:entries].find { |row| row[:id] == 2 }
|
|
328
|
+
expect(entry[:status]).to eq('archived')
|
|
329
|
+
expect(JSON.parse(entry[:source_context], symbolize_names: true)).to include(deprecation_reason: 'deprecated via API')
|
|
330
|
+
expect(state[:responses][1][:body]).to have_key(:error)
|
|
331
|
+
expect(state[:responses][2][:body]).to have_key(:error)
|
|
332
|
+
end
|
|
333
|
+
end
|
|
102
334
|
end
|
|
@@ -1,63 +1,41 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
|
+
require 'legion/extensions/apollo/runners/contradictions'
|
|
4
5
|
|
|
5
|
-
RSpec.describe
|
|
6
|
-
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Runners::Contradictions do
|
|
7
|
+
before { described_class.const_get(:PENDING_ENTRIES, false).clear }
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
it 'returns false when LLM unavailable' do
|
|
10
|
-
expect(knowledge.send(:llm_detects_conflict?, 'sky is blue', 'sky is red')).to be false
|
|
11
|
-
end
|
|
9
|
+
after { described_class.const_get(:PENDING_ENTRIES, false).clear }
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
it 'truncates content longer than CONFLICT_CHECK_MAX_CHARS' do
|
|
24
|
-
long_text = 'x' * 10_000
|
|
25
|
-
allow(llm_mod).to receive(:structured).and_return({ data: { contradicts: false } })
|
|
26
|
-
knowledge.send(:llm_detects_conflict?, long_text, long_text)
|
|
27
|
-
expect(llm_mod).to have_received(:structured) do |**kwargs|
|
|
28
|
-
user_msg = kwargs[:messages].find { |m| m[:role] == 'user' }[:content]
|
|
29
|
-
expect(user_msg.length).to be < 10_000
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
11
|
+
it 'queues entry IDs and consumes them after successful scanning' do
|
|
12
|
+
allow(described_class).to receive(:contradictions_detect).and_return(success: true, contradictions: [])
|
|
13
|
+
|
|
14
|
+
queued = described_class.enqueue(entry_id: 41)
|
|
15
|
+
result = described_class.scan_pending_contradictions
|
|
16
|
+
|
|
17
|
+
expect(queued).to include(success: true, queued: true, entry_id: 41, pending: 1)
|
|
18
|
+
expect(result).to eq(success: true, processed: 1, failed: 0)
|
|
19
|
+
expect(described_class).to have_received(:contradictions_detect).with(entry_id: 41)
|
|
20
|
+
expect(described_class.scan_pending_contradictions).to eq(success: true, processed: 0, failed: 0)
|
|
33
21
|
end
|
|
34
22
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
end
|
|
23
|
+
it 'requeues failed entries while consuming independent successful work' do
|
|
24
|
+
allow(described_class).to receive(:contradictions_detect) do |entry_id:|
|
|
25
|
+
raise Sequel::Error, 'temporary database failure' if entry_id == 41
|
|
39
26
|
|
|
40
|
-
|
|
41
|
-
expect(knowledge.send(:detect_contradictions, 'uuid-1', nil, 'test')).to eq([])
|
|
27
|
+
{ success: true, contradictions: [] }
|
|
42
28
|
end
|
|
29
|
+
described_class.enqueue(entry_id: 41)
|
|
30
|
+
described_class.enqueue(entry_id: 42)
|
|
43
31
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
stub_const('Legion::Data::Model::ApolloRelation', mock_relation_class)
|
|
53
|
-
allow(mock_entry_class).to receive(:db).and_return(mock_db)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'returns empty when no similar entries found' do
|
|
57
|
-
allow(mock_db).to receive(:fetch).and_return(double(all: []))
|
|
58
|
-
result = knowledge.send(:detect_contradictions, 'uuid-1', embedding, 'test')
|
|
59
|
-
expect(result).to eq([])
|
|
60
|
-
end
|
|
61
|
-
end
|
|
32
|
+
first = described_class.scan_pending_contradictions
|
|
33
|
+
|
|
34
|
+
expect(first).to eq(success: false, processed: 1, failed: 1)
|
|
35
|
+
expect(described_class.scan_pending_contradictions).to eq(success: false, processed: 0, failed: 1)
|
|
36
|
+
|
|
37
|
+
allow(described_class).to receive(:contradictions_detect).and_return(success: true, contradictions: [])
|
|
38
|
+
|
|
39
|
+
expect(described_class.scan_pending_contradictions).to eq(success: true, processed: 1, failed: 0)
|
|
62
40
|
end
|
|
63
41
|
end
|
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
require 'legion/extensions/apollo/gaia_integration'
|
|
5
|
-
|
|
5
|
+
require 'legion/extensions/apollo/runners/entity_watchdog'
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::GaiaIntegration do
|
|
7
|
+
before do
|
|
8
|
+
hide_const('Legion::Data::Model::Apollo::Expertise') if defined?(Legion::Data::Model::Apollo::Expertise)
|
|
9
|
+
end
|
|
7
10
|
describe '.publishable?' do
|
|
8
11
|
it 'returns true when above thresholds' do
|
|
9
12
|
expect(described_class.publishable?({ confidence: 0.8, novelty: 0.5 })).to be true
|
|
@@ -23,7 +26,7 @@ RSpec.describe Legion::Extensions::Apollo::GaiaIntegration do
|
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
describe '.handle_mesh_departure' do
|
|
26
|
-
it 'returns nil when
|
|
29
|
+
it 'returns nil when canonical expertise model is unavailable' do
|
|
27
30
|
expect(described_class.handle_mesh_departure(agent_id: 'test')).to be_nil
|
|
28
31
|
end
|
|
29
32
|
end
|
|
@@ -60,27 +63,13 @@ RSpec.describe Legion::Extensions::Apollo::GaiaIntegration do
|
|
|
60
63
|
end
|
|
61
64
|
end
|
|
62
65
|
|
|
63
|
-
describe 'entity watchdog
|
|
64
|
-
it '
|
|
65
|
-
|
|
66
|
-
tick_results = { content: 'Jane Doe deployed to https://api.example.com', tick_id: 'tick-1' }
|
|
67
|
-
entities = Legion::Extensions::Apollo::Helpers::EntityWatchdog.detect_entities(text: tick_results[:content])
|
|
68
|
-
expect(entities.size).to be >= 2
|
|
69
|
-
end
|
|
66
|
+
describe 'entity watchdog workflow' do
|
|
67
|
+
it 'reports missing canonical TaskLog instead of claiming a scan completed' do
|
|
68
|
+
hide_const('Legion::Data::Model::TaskLog') if defined?(Legion::Data::Model::TaskLog)
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
tick_results = { content: 'Jane Doe at LegionIO/lex-mesh', tick_id: 'tick-2' }
|
|
74
|
-
entities = Legion::Extensions::Apollo::Helpers::EntityWatchdog.detect_entities(text: tick_results[:content])
|
|
75
|
-
result = Legion::Extensions::Apollo::Helpers::EntityWatchdog.link_or_create(
|
|
76
|
-
entities: entities, source_context: tick_results[:tick_id]
|
|
70
|
+
expect(Legion::Extensions::Apollo::Runners::EntityWatchdog.scan_and_ingest).to include(
|
|
71
|
+
success: false, error: 'task_log_not_available', ingested: 0
|
|
77
72
|
)
|
|
78
|
-
expect(result[:success]).to be true
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it 'entry point documents entity watchdog actor' do
|
|
82
|
-
entry_point = File.read(File.expand_path('../../../../lib/legion/extensions/apollo.rb', __dir__))
|
|
83
|
-
expect(entry_point).to include('Entity watchdog runs as Actor::EntityWatchdog')
|
|
84
73
|
end
|
|
85
74
|
end
|
|
86
75
|
end
|
|
@@ -3,54 +3,51 @@
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
require 'legion/extensions/apollo/helpers/capability'
|
|
5
5
|
|
|
6
|
-
unless defined?(Legion::LLM)
|
|
7
|
-
module Legion
|
|
8
|
-
module LLM
|
|
9
|
-
def self.started? = false
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
6
|
RSpec.describe Legion::Extensions::Apollo::Helpers::Capability do
|
|
7
|
+
let(:host) { Object.new.extend(described_class) }
|
|
8
|
+
let(:settings) { Legion::Extensions::Apollo.default_settings }
|
|
9
|
+
|
|
15
10
|
before do
|
|
16
|
-
|
|
17
|
-
described_class.instance_variable_set(:@apollo_write_privilege, nil)
|
|
11
|
+
allow(host).to receive(:settings).and_return(settings)
|
|
18
12
|
end
|
|
19
13
|
|
|
20
14
|
describe '.can_embed?' do
|
|
21
15
|
it 'returns true when LLM is started and SSOT has an embedding lane' do
|
|
22
16
|
allow(Legion::LLM).to receive(:started?).and_return(true)
|
|
23
17
|
allow(Legion::LLM).to receive(:can_embed?).and_return(true)
|
|
24
|
-
expect(
|
|
18
|
+
expect(host.send(:capability_can_embed?)).to be true
|
|
25
19
|
end
|
|
26
20
|
|
|
27
21
|
it 'returns false when LLM is not started' do
|
|
28
|
-
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
29
|
-
expect(
|
|
22
|
+
allow(Legion::LLM).to receive(:started?).and_return(false)
|
|
23
|
+
expect(host.send(:capability_can_embed?)).to be false
|
|
30
24
|
end
|
|
31
25
|
end
|
|
32
26
|
|
|
27
|
+
it 'returns false when the running LLM has no embedding lane' do
|
|
28
|
+
allow(Legion::LLM).to receive(:started?).and_return(true)
|
|
29
|
+
allow(Legion::LLM).to receive(:can_embed?).and_return(false)
|
|
30
|
+
expect(host.send(:capability_can_embed?)).to be false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
33
|
describe '.can_write?' do
|
|
34
34
|
it 'returns false when apollo_write setting is false' do
|
|
35
|
-
|
|
36
|
-
expect(
|
|
35
|
+
settings[:data][:apollo_write] = false
|
|
36
|
+
expect(host.send(:capability_can_write?)).to be false
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
it 'returns false when Data is
|
|
40
|
-
|
|
41
|
-
allow(Legion::Data).to receive(:
|
|
42
|
-
expect(
|
|
39
|
+
it 'returns false when the native Data write capability is unavailable' do
|
|
40
|
+
settings[:data][:apollo_write] = true
|
|
41
|
+
allow(Legion::Data).to receive(:can_write?).with(:apollo_entries).and_return(false)
|
|
42
|
+
expect(host.send(:capability_can_write?)).to be false
|
|
43
43
|
end
|
|
44
|
-
end
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
expect(
|
|
50
|
-
end
|
|
45
|
+
it 'allows an adapter-approved SQLite writer without issuing PostgreSQL privilege SQL' do
|
|
46
|
+
settings[:data][:apollo_write] = true
|
|
47
|
+
allow(Legion::Data).to receive(:can_write?).with(:apollo_entries).and_return(true)
|
|
48
|
+
expect(Legion::Data).not_to receive(:connection)
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
expect(described_class.apollo_write_enabled?).to be false
|
|
50
|
+
expect(host.send(:capability_can_write?)).to be true
|
|
54
51
|
end
|
|
55
52
|
end
|
|
56
53
|
end
|
|
@@ -4,128 +4,43 @@ require 'spec_helper'
|
|
|
4
4
|
require 'legion/extensions/apollo/helpers/confidence'
|
|
5
5
|
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::Helpers::Confidence do
|
|
7
|
-
|
|
8
|
-
it 'defines INITIAL_CONFIDENCE' do
|
|
9
|
-
expect(described_class::INITIAL_CONFIDENCE).to eq(0.5)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'defines CORROBORATION_BOOST' do
|
|
13
|
-
expect(described_class::CORROBORATION_BOOST).to eq(0.3)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'defines RETRIEVAL_BOOST' do
|
|
17
|
-
expect(described_class::RETRIEVAL_BOOST).to eq(0.02)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it 'defines POWER_LAW_ALPHA' do
|
|
21
|
-
expect(described_class::POWER_LAW_ALPHA).to eq(0.05)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it 'defines DECAY_THRESHOLD' do
|
|
25
|
-
expect(described_class::DECAY_THRESHOLD).to eq(0.05)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'defines DECAY_MIN_AGE_HOURS' do
|
|
29
|
-
expect(described_class::DECAY_MIN_AGE_HOURS).to eq(168)
|
|
30
|
-
end
|
|
7
|
+
let(:host) { Object.new.extend(described_class) }
|
|
31
8
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it 'defines WRITE_CONFIDENCE_GATE' do
|
|
37
|
-
expect(described_class::WRITE_CONFIDENCE_GATE).to eq(0.6)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'defines WRITE_NOVELTY_GATE' do
|
|
41
|
-
expect(described_class::WRITE_NOVELTY_GATE).to eq(0.3)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'defines STALE_DAYS' do
|
|
45
|
-
expect(described_class::STALE_DAYS).to eq(90)
|
|
46
|
-
end
|
|
9
|
+
before do
|
|
10
|
+
allow(host).to receive(:settings).and_return(Legion::Extensions::Apollo.default_settings)
|
|
47
11
|
end
|
|
48
12
|
|
|
49
|
-
describe '
|
|
50
|
-
it '
|
|
51
|
-
result =
|
|
52
|
-
|
|
53
|
-
expect(result).to be_within(0.0001).of(expected)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'skips decay when age_hours is below minimum age' do
|
|
57
|
-
result = described_class.apply_decay(confidence: 1.0, age_hours: 10)
|
|
58
|
-
expect(result).to eq(1.0)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it 'applies age-based power-law decay when age_hours exceeds minimum' do
|
|
62
|
-
result = described_class.apply_decay(confidence: 1.0, age_hours: 500)
|
|
63
|
-
expect(result).to be > 0.0
|
|
64
|
-
expect(result).to be < 1.0
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it 'clamps to 0.0 minimum' do
|
|
68
|
-
result = described_class.apply_decay(confidence: 0.001)
|
|
69
|
-
expect(result).to be >= 0.0
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it 'accepts a custom alpha' do
|
|
73
|
-
result = described_class.apply_decay(confidence: 1.0, alpha: 0.5)
|
|
74
|
-
expected = 1.0 / (1.0 + 0.5) # ~0.6667
|
|
75
|
-
expect(result).to be_within(0.0001).of(expected)
|
|
13
|
+
describe 'retrieval confidence boost' do
|
|
14
|
+
it 'adds the configured retrieval boost to confidence' do
|
|
15
|
+
result = host.send(:confidence_apply_retrieval_boost, confidence: 0.5)
|
|
16
|
+
expect(result).to eq(0.52)
|
|
76
17
|
end
|
|
77
|
-
end
|
|
78
18
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
expect(result).to eq(0.52)
|
|
19
|
+
it 'uses the configured retrieval weight' do
|
|
20
|
+
host.send(:settings)[:confidence][:retrieval_boost] = 0.1
|
|
21
|
+
expect(host.send(:confidence_apply_retrieval_boost, confidence: 0.5)).to eq(0.6)
|
|
83
22
|
end
|
|
84
23
|
|
|
85
24
|
it 'clamps to 1.0 maximum' do
|
|
86
|
-
result =
|
|
25
|
+
result = host.send(:confidence_apply_retrieval_boost, confidence: 0.99)
|
|
87
26
|
expect(result).to eq(1.0)
|
|
88
27
|
end
|
|
89
28
|
end
|
|
90
29
|
|
|
91
|
-
describe '
|
|
92
|
-
it 'adds
|
|
93
|
-
result =
|
|
30
|
+
describe 'corroboration confidence boost' do
|
|
31
|
+
it 'adds the configured corroboration boost to confidence' do
|
|
32
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.5)
|
|
94
33
|
expect(result).to eq(0.8)
|
|
95
34
|
end
|
|
96
35
|
|
|
97
36
|
it 'clamps to 1.0 maximum' do
|
|
98
|
-
result =
|
|
37
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.9)
|
|
99
38
|
expect(result).to eq(1.0)
|
|
100
39
|
end
|
|
101
40
|
|
|
102
41
|
it 'applies half weight for same-source corroboration' do
|
|
103
|
-
result =
|
|
42
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.5, weight: 0.5)
|
|
104
43
|
expect(result).to eq(0.65)
|
|
105
44
|
end
|
|
106
45
|
end
|
|
107
|
-
|
|
108
|
-
describe '.decayed?' do
|
|
109
|
-
it 'returns true when confidence below threshold' do
|
|
110
|
-
expect(described_class.decayed?(confidence: 0.01)).to be true
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
it 'returns false when confidence at or above threshold' do
|
|
114
|
-
expect(described_class.decayed?(confidence: 0.05)).to be false
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
describe '.meets_write_gate?' do
|
|
119
|
-
it 'returns true when both gates met' do
|
|
120
|
-
expect(described_class.meets_write_gate?(confidence: 0.7, novelty: 0.4)).to be true
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it 'returns false when confidence below gate' do
|
|
124
|
-
expect(described_class.meets_write_gate?(confidence: 0.5, novelty: 0.4)).to be false
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
it 'returns false when novelty below gate' do
|
|
128
|
-
expect(described_class.meets_write_gate?(confidence: 0.7, novelty: 0.2)).to be false
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
46
|
end
|