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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -0
  3. data/README.md +113 -18
  4. data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
  5. data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
  6. data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
  7. data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
  8. data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
  9. data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
  10. data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
  11. data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
  12. data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
  13. data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
  14. data/lib/legion/extensions/apollo/api.rb +116 -51
  15. data/lib/legion/extensions/apollo/client.rb +20 -9
  16. data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
  17. data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
  18. data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
  19. data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
  20. data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
  21. data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
  22. data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
  23. data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
  24. data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
  25. data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
  26. data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
  27. data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
  28. data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
  29. data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
  30. data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
  31. data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
  32. data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
  33. data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
  34. data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
  35. data/lib/legion/extensions/apollo/runners/request.rb +51 -47
  36. data/lib/legion/extensions/apollo/runners/search.rb +88 -0
  37. data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
  38. data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
  39. data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
  40. data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
  41. data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
  42. data/lib/legion/extensions/apollo/transport.rb +20 -0
  43. data/lib/legion/extensions/apollo/version.rb +1 -1
  44. data/lib/legion/extensions/apollo.rb +26 -24
  45. data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
  46. data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
  47. data/spec/legion/extensions/apollo/api_spec.rb +250 -28
  48. data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
  49. data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
  50. data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
  51. data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
  52. data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
  53. data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
  54. data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
  55. data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
  56. data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
  57. data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
  58. data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
  59. data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
  60. data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
  61. data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
  62. data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
  63. data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
  64. data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
  65. data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
  66. data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
  67. data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
  68. data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
  69. data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
  70. data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
  71. data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
  72. data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
  73. data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
  74. data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
  75. data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
  76. data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
  77. data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
  78. data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
  79. data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
  80. data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
  81. data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
  82. data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
  83. data/spec/spec_helper.rb +13 -15
  84. metadata +71 -13
  85. data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
  86. data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
  87. data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
  88. data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
  89. data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
  90. data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
  91. data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
  92. data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
@@ -1,34 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Stub Sinatra before requiring api.rb so the guard `unless defined?(Sinatra)` fires.
4
- # This avoids a LoadError when sinatra is not in the bundle.
5
- unless defined?(Sinatra)
6
- module Sinatra
7
- class Base
8
- class << self
9
- def set(*, **); end
10
- def before(*, &); end
11
- def helpers(*, &); end
12
- def get(*, &); end
13
- def post(*, &); end
14
- def put(*, &); end
15
- def delete(*, &); end
16
- end
17
- end
18
- end
19
- end
3
+ require 'sinatra/base'
4
+ require 'open3'
5
+ require 'rbconfig'
6
+ require 'json'
20
7
 
21
8
  require 'spec_helper'
22
9
  require 'legion/extensions/apollo/api'
23
10
 
24
11
  RSpec.describe Legion::Extensions::Apollo::Api do
25
- it 'is defined as a Sinatra app' do
26
- expect(described_class.superclass).to eq(Sinatra::Base)
27
- end
28
-
29
12
  describe '.stats_payload' do
30
- let(:entry_model) { class_double('Legion::Data::Model::ApolloEntry') }
31
- let(:relation_model) { class_double('Legion::Data::Model::ApolloRelation', count: 4) }
13
+ let(:entry_model) { class_double('Legion::Data::Model::Apollo::Entry') }
14
+ let(:relation_model) { class_double('Legion::Data::Model::Apollo::Relation', count: 4) }
32
15
  let(:status_counts) do
33
16
  instance_double(
34
17
  'StatusCounts',
@@ -52,8 +35,8 @@ RSpec.describe Legion::Extensions::Apollo::Api do
52
35
  let(:recent_entries) { instance_double('RecentEntries', count: 2) }
53
36
 
54
37
  before do
55
- stub_const('Legion::Data::Model::ApolloEntry', entry_model)
56
- stub_const('Legion::Data::Model::ApolloRelation', relation_model)
38
+ stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
39
+ stub_const('Legion::Data::Model::Apollo::Relation', relation_model)
57
40
  allow(entry_model).to receive(:count).and_return(6)
58
41
  allow(entry_model).to receive(:avg).with(:confidence).and_return(0.81234)
59
42
  allow(entry_model).to receive(:exclude).with(status: 'archived').and_return(active_entries)
@@ -83,10 +66,19 @@ RSpec.describe Legion::Extensions::Apollo::Api do
83
66
  )
84
67
  end
85
68
 
69
+ it 'keeps entry statistics available when the optional relation model is absent' do
70
+ hide_const('Legion::Data::Model::Apollo::Relation') if defined?(Legion::Data::Model::Apollo::Relation)
71
+
72
+ payload = described_class.stats_payload(now: Time.utc(2026, 4, 28, 12, 0, 0))
73
+
74
+ expect(payload).to include(total_entries: 6, recent_24h: 2, avg_confidence: 0.812)
75
+ expect(payload).not_to have_key(:total_relations)
76
+ end
77
+
86
78
  context 'when legion-data exposes namespaced Apollo models' do
87
79
  before do
88
- hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry)
89
- hide_const('Legion::Data::Model::ApolloRelation') if defined?(Legion::Data::Model::ApolloRelation)
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)
90
82
  stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
91
83
  stub_const('Legion::Data::Model::Apollo::Relation', relation_model)
92
84
  end
@@ -104,9 +96,239 @@ RSpec.describe Legion::Extensions::Apollo::Api do
104
96
  end
105
97
 
106
98
  it 'returns an apollo data error when the entry model is unavailable' do
107
- hide_const('Legion::Data::Model::ApolloEntry')
99
+ hide_const('Legion::Data::Model::Apollo::Entry')
108
100
 
109
101
  expect(described_class.stats_payload).to eq(error: 'apollo_data_not_available')
110
102
  end
111
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
112
334
  end
@@ -1,72 +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 'Apollo Contradiction Detection' do
6
- let(:knowledge) { Object.new.extend(Legion::Extensions::Apollo::Runners::Knowledge) }
6
+ RSpec.describe Legion::Extensions::Apollo::Runners::Contradictions do
7
+ before { described_class.const_get(:PENDING_ENTRIES, false).clear }
7
8
 
8
- describe '#llm_detects_conflict?' do
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
- context 'when LLM is available' do
14
- let(:llm_mod) do
15
- Module.new do
16
- def self.respond_to?(*) = true
17
- def self.structured(**) = { data: { contradicts: true } }
18
- end
19
- 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: [])
20
13
 
21
- before { stub_const('Legion::LLM', llm_mod) }
14
+ queued = described_class.enqueue(entry_id: 41)
15
+ result = described_class.scan_pending_contradictions
22
16
 
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
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
- describe '#detect_contradictions' do
36
- it 'returns empty when ApolloEntry model unavailable' do
37
- expect(knowledge.send(:detect_contradictions, 1, nil, 'test')).to eq([])
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
- it 'returns empty when embedding is nil' do
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
- context 'when entries exist' do
45
- let(:mock_entry_class) { double('ApolloEntry') }
46
- let(:mock_relation_class) { double('ApolloRelation') }
47
- let(:mock_db) { double('db') }
48
- let(:embedding) { Array.new(1536, 0.1) }
32
+ first = described_class.scan_pending_contradictions
49
33
 
50
- before do
51
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
52
- stub_const('Legion::Data::Model::ApolloRelation', mock_relation_class)
53
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
54
- end
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)
55
36
 
56
- it 'queries with ORDER BY embedding distance' do
57
- allow(mock_db).to receive(:fetch).and_return(double(all: []))
58
- knowledge.send(:detect_contradictions, 'uuid-1', embedding, 'test')
59
- expect(mock_db).to have_received(:fetch).with(
60
- a_string_including('ORDER BY embedding <=> :embedding'),
61
- hash_including(:entry_id, :embedding)
62
- )
63
- end
37
+ allow(described_class).to receive(:contradictions_detect).and_return(success: true, contradictions: [])
64
38
 
65
- it 'returns empty when no similar entries found' do
66
- allow(mock_db).to receive(:fetch).and_return(double(all: []))
67
- result = knowledge.send(:detect_contradictions, 'uuid-1', embedding, 'test')
68
- expect(result).to eq([])
69
- end
70
- end
39
+ expect(described_class.scan_pending_contradictions).to eq(success: true, processed: 1, failed: 0)
71
40
  end
72
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 ApolloExpertise unavailable' do
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 phase handler' do
64
- it 'detects entities from tick results' do
65
- require 'legion/extensions/apollo/helpers/entity_watchdog'
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
- it 'links or creates entities from tick results' do
72
- require 'legion/extensions/apollo/helpers/entity_watchdog'
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
- Legion::Settings[:extensions][:apollo] = Legion::Extensions::Apollo.default_settings
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
- it 'returns true when LLM is started and Ollama has a model' do
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
- allow(described_class).to receive(:ollama_embedding_available?).and_return(true)
24
- expect(described_class.can_embed?).to be true
17
+ allow(Legion::LLM).to receive(:can_embed?).and_return(true)
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) if defined?(Legion::LLM)
29
- expect(described_class.can_embed?).to be false
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
- described_class.settings[:data][:apollo_write] = false
36
- expect(described_class.can_write?).to be false
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 not connected' do
40
- described_class.settings[:data][:apollo_write] = true
41
- allow(Legion::Data).to receive(:connected?).and_return(false) if defined?(Legion::Data)
42
- expect(described_class.can_write?).to be false
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
- describe '.apollo_write_enabled?' do
47
- it 'reads from settings' do
48
- described_class.settings[:data][:apollo_write] = true
49
- expect(described_class.apollo_write_enabled?).to be true
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
- it 'defaults to false' do
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