lex-apollo 0.4.44 → 0.4.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -0
  3. data/README.md +76 -14
  4. data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -41
  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 +16 -16
  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 +96 -41
  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 -51
  18. data/lib/legion/extensions/apollo/helpers/confidence.rb +8 -52
  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 +99 -43
  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 +64 -102
  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 +146 -0
  30. data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
  31. data/lib/legion/extensions/apollo/runners/gas.rb +120 -142
  32. data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
  33. data/lib/legion/extensions/apollo/runners/maintenance.rb +154 -65
  34. data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
  35. data/lib/legion/extensions/apollo/runners/request.rb +51 -46
  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 +8 -5
  43. data/lib/legion/extensions/apollo/version.rb +1 -1
  44. data/lib/legion/extensions/apollo.rb +28 -24
  45. data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +9 -24
  46. data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
  47. data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
  48. data/spec/legion/extensions/apollo/api_spec.rb +240 -8
  49. data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
  50. data/spec/legion/extensions/apollo/gaia_integration_spec.rb +16 -21
  51. data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
  52. data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
  53. data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
  54. data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
  55. data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
  56. data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
  57. data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
  58. data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
  59. data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +35 -6
  60. data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
  61. data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
  62. data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
  63. data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +205 -0
  64. data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
  65. data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +14 -91
  66. data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +28 -11
  67. data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
  68. data/spec/legion/extensions/apollo/runners/gas_spec.rb +87 -77
  69. data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +14 -89
  70. data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +141 -9
  71. data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
  72. data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
  73. data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +14 -5
  74. data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +48 -64
  75. data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
  76. data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
  77. data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
  78. data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
  79. data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
  80. data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
  81. data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
  82. data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
  83. data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
  84. data/spec/legion/extensions/apollo/transport_spec.rb +33 -0
  85. data/spec/spec_helper.rb +42 -13
  86. metadata +41 -13
  87. data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
  88. data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
  89. data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
  90. data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
  91. data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
  92. data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
  93. data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
  94. 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::ApolloEntry') }
11
- 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) }
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::ApolloEntry', entry_model)
36
- 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)
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::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry)
79
- 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)
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::ApolloEntry')
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
+ marker = 'APOLLO_RESULT='
243
+ raise "HTTP identity process failed: #{stderr}\n#{stdout}" unless status.success? && stdout.include?(marker)
244
+
245
+ ApolloSpecOutput.parse(stdout)
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 '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
20
-
21
- before { stub_const('Legion::LLM', llm_mod) }
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
- 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) }
49
-
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
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 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,19 @@ 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
70
-
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]
66
+ describe 'entity watchdog workflow' do
67
+ it 'reports missing canonical TaskLog instead of claiming a scan completed' do
68
+ watchdog = Legion::Extensions::Apollo::Runners::EntityWatchdog
69
+ allow(watchdog).to receive(:settings).and_return(
70
+ Legion::Extensions::Apollo.default_settings.merge(
71
+ entity_watchdog: Legion::Extensions::Apollo.default_settings[:entity_watchdog].merge(enabled: true)
72
+ )
77
73
  )
78
- expect(result[:success]).to be true
79
- end
74
+ hide_const('Legion::Data::Model::TaskLog') if defined?(Legion::Data::Model::TaskLog)
80
75
 
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')
76
+ expect(watchdog.scan_and_ingest).to include(
77
+ success: false, error: 'task_log_not_available', ingested: 0
78
+ )
84
79
  end
85
80
  end
86
81
  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
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(described_class.can_embed?).to be 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