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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/README.md +72 -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 +4 -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 +110 -0
  30. data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
  31. data/lib/legion/extensions/apollo/runners/gas.rb +87 -136
  32. data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
  33. data/lib/legion/extensions/apollo/runners/maintenance.rb +151 -64
  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/version.rb +1 -1
  43. data/lib/legion/extensions/apollo.rb +26 -24
  44. data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
  45. data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
  46. data/spec/legion/extensions/apollo/api_spec.rb +240 -8
  47. data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
  48. data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
  49. data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
  50. data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
  51. data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
  52. data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
  53. data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
  54. data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
  55. data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
  56. data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
  57. data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +32 -3
  58. data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
  59. data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
  60. data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
  61. data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
  62. data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
  63. data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
  64. data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +23 -7
  65. data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
  66. data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -77
  67. data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
  68. data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +138 -6
  69. data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
  70. data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
  71. data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +11 -2
  72. data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +33 -65
  73. data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
  74. data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
  75. data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
  76. data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
  77. data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
  78. data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
  79. data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
  80. data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
  81. data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
  82. data/spec/spec_helper.rb +13 -15
  83. metadata +40 -14
  84. data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
  85. data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
  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 -39
  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
@@ -9,19 +9,92 @@ require 'legion/extensions/apollo/helpers/writeback'
9
9
  require 'legion/extensions/apollo/runners/knowledge'
10
10
 
11
11
  RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
12
- let(:runner) do
13
- obj = Object.new
14
- obj.extend(described_class)
15
- obj
16
- end
12
+ let(:runner) { described_class }
17
13
 
18
14
  before do
19
- embeddings_mod = Module.new do
20
- def self.generate(*, **)
21
- { vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 }
22
- end
15
+ hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo)
16
+ allow(Legion::Data).to receive(:connection).and_return(nil)
17
+ hide_const('Legion::Identity::Process') if defined?(Legion::Identity::Process)
18
+ stub_const('Legion::LLM::Call::Embeddings', Module.new)
19
+ allow(Legion::LLM::Call::Embeddings).to receive(:generate) do |task: :document, **|
20
+ ApolloSpecEmbedding.result(task: task)
21
+ end
22
+ allow(Legion::Extensions::Apollo).to receive(:settings).and_return(Legion::Extensions::Apollo.default_settings)
23
+ allow(runner).to receive(:settings).and_return(Legion::Extensions::Apollo.settings)
24
+ end
25
+
26
+ shared_context 'persisted knowledge' do
27
+ let(:database) { Sequel.sqlite }
28
+ let(:entry_model) { Class.new(Sequel::Model(database[:apollo_entries])) }
29
+
30
+ before do
31
+ database.create_table(:apollo_entries) do
32
+ primary_key :id
33
+ String :content
34
+ String :content_type
35
+ String :content_hash
36
+ String :status
37
+ String :source_agent
38
+ String :source_provider
39
+ String :source_channel
40
+ String :source_context
41
+ String :tags
42
+ String :knowledge_domain
43
+ String :submitted_by
44
+ String :submitted_from
45
+ String :access_scope
46
+ Integer :identity_principal_id
47
+ Integer :identity_id
48
+ String :identity_canonical_name
49
+ String :embedding
50
+ String :embedding_provenance
51
+ String :embedding_space_id
52
+ Integer :embedding_dimensions
53
+ Float :confidence
54
+ Integer :access_count, default: 0
55
+ DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
56
+ DateTime :updated_at
57
+ end
58
+ database.create_table(:apollo_expertise) do
59
+ primary_key :id
60
+ String :agent_id
61
+ String :domain
62
+ Float :proficiency
63
+ Integer :entry_count
64
+ DateTime :last_active_at
65
+ end
66
+ database.create_table(:apollo_access_logs) do
67
+ primary_key :id
68
+ Integer :entry_id
69
+ String :agent_id
70
+ String :action
71
+ end
72
+ database.create_table(:identities) do
73
+ primary_key :id
74
+ Integer :principal_id
75
+ end
76
+ database.create_table(:identity_group_memberships) do
77
+ Integer :principal_id
78
+ Integer :group_id
79
+ end
80
+ stub_const('Legion::Data::Model::Apollo::Entry', entry_model)
81
+ stub_const('Legion::Data::Model::Apollo::Expertise', Class.new(Sequel::Model(database[:apollo_expertise])))
82
+ stub_const('Legion::Data::Model::Apollo::AccessLog', Class.new(Sequel::Model(database[:apollo_access_logs])))
83
+ end
84
+
85
+ after { database.disconnect }
86
+
87
+ def persisted_entry(content: 'Ruby is interpreted', **attributes)
88
+ embedding = ApolloSpecEmbedding.result
89
+ entry_model.create(
90
+ { content: content, content_type: 'fact', content_hash: Digest::SHA256.hexdigest(content),
91
+ status: 'confirmed', source_agent: 'worker', tags: Legion::JSON.dump(['ruby']),
92
+ knowledge_domain: 'general', access_scope: 'global', confidence: 0.8,
93
+ embedding: Legion::JSON.dump(embedding[:vector]),
94
+ embedding_provenance: Legion::JSON.dump(embedding[:embedding_provenance]),
95
+ embedding_space_id: embedding[:embedding_provenance][:space_id], embedding_dimensions: 2 }.merge(attributes)
96
+ )
23
97
  end
24
- stub_const('Legion::LLM::Call::Embeddings', embeddings_mod)
25
98
  end
26
99
 
27
100
  describe '#store_knowledge' do
@@ -88,8 +161,6 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
88
161
  result = runner.query_knowledge(query: 'PKI configuration')
89
162
  expect(result[:action]).to eq(:query)
90
163
  expect(result[:query]).to eq('PKI configuration')
91
- expect(result[:limit]).to eq(10)
92
- expect(result[:min_confidence]).to eq(0.3)
93
164
  end
94
165
 
95
166
  it 'accepts custom limit and filters' do
@@ -109,7 +180,6 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
109
180
  result = runner.related_entries(entry_id: 'uuid-123')
110
181
  expect(result[:action]).to eq(:traverse)
111
182
  expect(result[:entry_id]).to eq('uuid-123')
112
- expect(result[:depth]).to eq(2)
113
183
  end
114
184
 
115
185
  it 'accepts relation_types filter' do
@@ -130,10 +200,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
130
200
  end
131
201
 
132
202
  describe '#handle_ingest' do
133
- let(:host) { Object.new.extend(described_class) }
203
+ let(:host) { runner }
134
204
 
135
205
  context 'when Apollo data is not available' do
136
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
206
+ before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
137
207
 
138
208
  it 'returns a structured error' do
139
209
  result = host.handle_ingest(content: 'test', content_type: 'fact')
@@ -143,233 +213,123 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
143
213
  end
144
214
 
145
215
  context 'when Apollo data is available' do
146
- let(:mock_entry_class) { double('ApolloEntry') }
147
- let(:mock_relation_class) { double('ApolloRelation') }
148
- let(:mock_expertise_class) { double('ApolloExpertise') }
149
- let(:mock_access_log_class) { double('ApolloAccessLog') }
150
- let(:mock_entry) { double('entry', id: 'uuid-123', embedding: nil) }
151
- let(:empty_dataset) { double('dataset', each: nil) }
216
+ include_context 'persisted knowledge'
152
217
 
153
- let(:mock_db) { double('db') }
218
+ it 'persists a novel candidate that can be retrieved in the generated embedding space' do
219
+ result = host.handle_ingest(content: 'Ruby is great', content_type: 'fact',
220
+ tags: ['Ruby'], source_agent: 'agent-1')
154
221
 
155
- before do
156
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
157
- stub_const('Legion::Data::Model::ApolloRelation', mock_relation_class)
158
- stub_const('Legion::Data::Model::ApolloExpertise', mock_expertise_class)
159
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class)
160
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
161
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
162
-
163
- # Corroboration lookup chain
164
- allow(mock_entry_class).to receive(:where).and_return(double(exclude: double(limit: empty_dataset)))
165
- # Content hash dedup chain — no match by default
166
- dedup_chain = double('dedup_chain')
167
- allow(mock_entry_class).to receive(:where).with(content_hash: anything).and_return(dedup_chain)
168
- allow(dedup_chain).to receive(:exclude).with(status: 'archived').and_return(double(first: nil))
169
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
170
- allow(mock_db).to receive(:fetch).and_return(double(all: []))
171
- allow(mock_db).to receive(:transaction) { |**_options, &block| block.call }
172
- allow(mock_entry_class).to receive(:create).and_return(mock_entry)
173
- allow(mock_expertise_class).to receive(:where).and_return(double(first: nil))
174
- allow(mock_expertise_class).to receive(:create)
175
- allow(mock_access_log_class).to receive(:create)
222
+ expect(result).to include(success: true, status: 'candidate', corroborated: false)
223
+ retrieved = host.retrieve_relevant(query: 'Ruby', min_confidence: 0.0)
224
+ expect(retrieved).to include(success: true, entries: [include(id: result[:entry_id], content: 'Ruby is great', tags: ['ruby'])])
225
+ expect(database[:apollo_access_logs].all).to contain_exactly(
226
+ include(entry_id: result[:entry_id], agent_id: 'agent-1', action: 'ingest')
227
+ )
176
228
  end
177
229
 
178
- it 'creates a new candidate entry when novel' do
179
- result = host.handle_ingest(content: 'Ruby is great', content_type: 'fact',
180
- tags: ['ruby'], source_agent: 'agent-1')
181
- expect(result[:success]).to be true
182
- expect(result[:status]).to eq('candidate')
183
- expect(result[:corroborated]).to be false
184
- end
230
+ it 'sanitizes null bytes and invalid UTF-8 before content becomes queryable' do
231
+ content = "hello\x00\xC3(world".dup.force_encoding(Encoding::UTF_8)
232
+ result = host.handle_ingest(content: content, content_type: 'fact')
185
233
 
186
- it 'defaults content_type to :observation when nil' do
187
- expect(mock_entry_class).to receive(:create).with(
188
- hash_including(content_type: 'observation')
189
- ).and_return(mock_entry)
190
- result = host.handle_ingest(content: 'gaia tick result', content_type: nil,
191
- source_agent: 'gaia')
192
234
  expect(result[:success]).to be true
235
+ expect(host.handle_query(query: '', min_confidence: 0.0)[:entries]).to contain_exactly(include(content: 'hello(world'))
193
236
  end
194
237
 
195
- it 'creates expertise record for source agent' do
196
- expect(mock_expertise_class).to receive(:create).with(
197
- hash_including(agent_id: 'agent-1', domain: 'ruby')
198
- )
199
- host.handle_ingest(content: 'Ruby is great', content_type: 'fact',
200
- tags: ['ruby'], source_agent: 'agent-1')
201
- end
238
+ context 'content hash dedup' do
239
+ let!(:existing_entry) { persisted_entry(content: 'test', content_hash: 'same-hash') }
202
240
 
203
- it 'logs access' do
204
- expect(mock_access_log_class).to receive(:create).with(
205
- hash_including(agent_id: 'agent-1', action: 'ingest')
206
- )
207
- host.handle_ingest(content: 'Ruby is great', content_type: 'fact',
208
- tags: ['ruby'], source_agent: 'agent-1')
209
- end
241
+ it 'reuses a provenanced active entry without requiring an embedding provider' do
242
+ expect(Legion::LLM::Call::Embeddings).not_to receive(:generate)
210
243
 
211
- it 'defaults domain to general when no tags' do
212
- expect(mock_expertise_class).to receive(:create).with(
213
- hash_including(domain: 'general')
214
- )
215
- host.handle_ingest(content: 'test', content_type: 'fact', source_agent: 'agent-1')
216
- end
244
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash')
217
245
 
218
- it 'passes source_channel to create' do
219
- expect(mock_entry_class).to receive(:create).with(
220
- hash_including(source_channel: 'slack-alerts')
221
- ).and_return(mock_entry)
222
- host.handle_ingest(content: 'test', content_type: 'fact',
223
- source_agent: 'agent-1', source_channel: 'slack-alerts')
224
- end
246
+ expect(result).to include(success: true, deduped: true, entry_id: existing_entry.id)
247
+ expect(entry_model.count).to eq(1)
248
+ expect(existing_entry.refresh.confidence).to be > 0.8
249
+ end
225
250
 
226
- it 'passes knowledge_domain to create from explicit param' do
227
- expect(mock_entry_class).to receive(:create).with(
228
- hash_including(knowledge_domain: 'clinical')
229
- ).and_return(mock_entry)
230
- host.handle_ingest(content: 'test', content_type: 'fact',
231
- source_agent: 'agent-1', knowledge_domain: 'clinical')
232
- end
251
+ it 'regenerates legacy embeddings before reporting a duplicate as reusable' do
252
+ existing_entry.update(embedding_provenance: nil, embedding_space_id: nil, embedding_dimensions: nil)
233
253
 
234
- it 'defaults knowledge_domain to first tag' do
235
- expect(mock_entry_class).to receive(:create).with(
236
- hash_including(knowledge_domain: 'cardiology')
237
- ).and_return(mock_entry)
238
- host.handle_ingest(content: 'test', content_type: 'fact',
239
- tags: %w[cardiology treatment], source_agent: 'agent-1')
240
- end
254
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash')
241
255
 
242
- it 'defaults knowledge_domain to general when no tags and no explicit domain' do
243
- expect(mock_entry_class).to receive(:create).with(
244
- hash_including(knowledge_domain: 'general')
245
- ).and_return(mock_entry)
246
- host.handle_ingest(content: 'test', content_type: 'fact', source_agent: 'agent-1')
247
- end
256
+ expect(result).to include(success: true, deduped: true, entry_id: existing_entry.id)
257
+ expect(host.retrieve_relevant(query: 'test')[:entries]).to contain_exactly(include(id: existing_entry.id))
258
+ expect(entry_model.count).to eq(1)
259
+ end
248
260
 
249
- it 'passes submitted_by and submitted_from to create' do
250
- expect(mock_entry_class).to receive(:create).with(
251
- hash_including(submitted_by: 'user@example.com', submitted_from: 'node-1')
252
- ).and_return(mock_entry)
253
- host.handle_ingest(content: 'test', content_type: 'fact',
254
- source_agent: 'agent-1',
255
- submitted_by: 'user@example.com',
256
- submitted_from: 'node-1')
257
- end
261
+ it 'does not report dedup success or boost confidence when legacy regeneration fails' do
262
+ existing_entry.update(embedding_provenance: nil, embedding_space_id: nil)
263
+ allow(Legion::LLM::Call::Embeddings).to receive(:generate).and_raise(Legion::LLM::LLMError, 'provider unavailable')
258
264
 
259
- it 'passes content_hash to create' do
260
- expect(mock_entry_class).to receive(:create).with(
261
- hash_including(content_hash: 'abc123')
262
- ).and_return(mock_entry)
263
- host.handle_ingest(content: 'test', content_type: 'fact',
264
- source_agent: 'agent-1', content_hash: 'abc123')
265
- end
265
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash')
266
266
 
267
- it 'normalizes tags before storage' do
268
- expect(mock_entry_class).to receive(:create).with(
269
- hash_including(tags: Sequel.pg_array(%w[rabbitmq]))
270
- ).and_return(mock_entry)
271
- host.handle_ingest(content: 'test', content_type: 'fact',
272
- tags: ['RabbitMQ'], source_agent: 'agent-1')
273
- end
267
+ expect(result).to include(success: false, error: include('provider unavailable'))
268
+ expect(existing_entry.refresh.confidence).to eq(0.8)
269
+ expect(existing_entry.embedding_provenance).to be_nil
270
+ end
274
271
 
275
- it 'sanitizes null bytes before storing content' do
276
- expect(mock_entry_class).to receive(:create).with(
277
- hash_including(content: 'helloworld')
278
- ).and_return(mock_entry)
279
- host.handle_ingest(content: "hello\x00world", content_type: 'fact', source_agent: 'agent-1')
280
- end
272
+ it 'does not reuse another principal or scope with the same hash' do
273
+ existing_entry.update(access_scope: 'private', identity_principal_id: 7)
281
274
 
282
- it 'truncates short varchar metadata fields at the database boundary' do
283
- expect(mock_entry_class).to receive(:create).with(
284
- hash_including(
285
- source_agent: 'a' * 50,
286
- source_provider: 'p' * 50,
287
- source_channel: 'c' * 100,
288
- knowledge_domain: 'd' * 50,
289
- submitted_by: 'u' * 255,
290
- submitted_from: 'n' * 255
291
- )
292
- ).and_return(mock_entry)
293
- host.handle_ingest(
294
- content: 'test',
295
- content_type: 'fact',
296
- source_agent: 'a' * 60,
297
- source_provider: 'p' * 60,
298
- source_channel: 'c' * 120,
299
- knowledge_domain: 'd' * 60,
300
- submitted_by: 'u' * 300,
301
- submitted_from: 'n' * 300
302
- )
303
- end
275
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash',
276
+ access_scope: 'private', identity_principal_id: 8)
304
277
 
305
- context 'content hash dedup' do
306
- let(:existing_entry) do
307
- double('existing', id: 'uuid-existing', confidence: 0.6,
308
- update: true)
278
+ expect(result).to include(success: true, corroborated: false)
279
+ expect(result[:entry_id]).not_to eq(existing_entry.id)
280
+ expect(host.retrieve_relevant(query: 'test', min_confidence: 0.0,
281
+ requesting_principal_id: 8)[:entries]).to contain_exactly(include(id: result[:entry_id]))
309
282
  end
310
283
 
311
- it 'returns deduped result on hash collision with active entry' do
312
- dedup_dataset = double('dataset')
313
- allow(mock_entry_class).to receive(:where).with(content_hash: anything).and_return(dedup_dataset)
314
- allow(dedup_dataset).to receive(:exclude).with(status: 'archived').and_return(dedup_dataset)
315
- allow(dedup_dataset).to receive(:first).and_return(existing_entry)
316
-
317
- result = host.handle_ingest(content: 'test', content_type: 'fact',
318
- source_agent: 'agent-1',
319
- content_hash: 'deadbeef12345678deadbeef12345678')
320
- expect(result[:success]).to be true
321
- expect(result[:deduped]).to be true
322
- expect(result[:entry_id]).to eq('uuid-existing')
323
- end
284
+ it 'does not deduplicate against an archived entry' do
285
+ existing_entry.update(status: 'archived')
286
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash')
324
287
 
325
- it 'recovers gracefully when a concurrent ingest wins the content_hash unique constraint race' do
326
- # Simulate: dedup check passes (nil — no existing entry yet), then .create
327
- # raises UniqueConstraintViolation (another thread inserted between check and insert).
328
- # create_candidate_entry must rescue and return the existing entry's id so the
329
- # caller succeeds rather than propagating a database error.
330
- race_entry = double('race_entry', id: 'uuid-race-winner')
331
-
332
- allow(mock_entry_class).to receive(:create)
333
- .and_raise(Sequel::UniqueConstraintViolation, 'duplicate key value violates unique constraint "idx_apollo_content_hash"')
334
-
335
- collision_dataset = double('collision_dataset')
336
- allow(mock_entry_class).to receive(:where).with(content_hash: anything).and_return(collision_dataset)
337
- allow(collision_dataset).to receive(:exclude).with(status: 'archived').and_return(collision_dataset)
338
- # First call: dedup pre-check returns nil (not yet in DB).
339
- # Second call: post-collision lookup returns the winner inserted by the other thread.
340
- allow(collision_dataset).to receive(:first).and_return(nil, race_entry)
341
-
342
- result = host.handle_ingest(content: 'concurrent content', content_type: 'fact',
343
- source_agent: 'agent-1',
344
- content_hash: 'd3861b2862454c5a6a9e480829333841')
345
- expect(result[:success]).to be true
346
- expect(result[:entry_id]).to eq('uuid-race-winner')
288
+ expect(result).to include(success: true, status: 'candidate')
289
+ expect(result[:entry_id]).not_to eq(existing_entry.id)
347
290
  end
348
291
 
349
- it 'returns a failure when a rolled-back uniqueness conflict has no winner' do
350
- allow(mock_entry_class).to receive(:create)
351
- .and_raise(Sequel::UniqueConstraintViolation, 'duplicate key value violates unique constraint')
352
- collision_dataset = double('collision_dataset')
353
- allow(mock_entry_class).to receive(:where).with(content_hash: anything).and_return(collision_dataset)
354
- allow(collision_dataset).to receive(:exclude).with(status: 'archived').and_return(collision_dataset)
355
- allow(collision_dataset).to receive(:first).and_return(nil, nil)
292
+ it 'returns the concurrent winner after an insert uniqueness conflict' do
293
+ winner_values = existing_entry.values.except(:id)
294
+ existing_entry.destroy
295
+ winner_id = nil
296
+ allow(entry_model).to receive(:create).and_raise(Sequel::UniqueConstraintViolation, 'duplicate hash')
297
+ # Make the competing row visible after the pre-check but before the insert savepoint.
298
+ allow(database).to receive(:transaction).and_wrap_original do |original, **options, &block|
299
+ winner_id = database[:apollo_entries].insert(winner_values) if options[:savepoint]
300
+ original.call(**options, &block)
301
+ end
302
+
303
+ result = host.handle_ingest(content: 'test', content_hash: 'same-hash')
356
304
 
357
- result = host.handle_ingest(content: 'lost race content', content_type: 'fact',
358
- source_agent: 'agent-1', content_hash: 'no-winner')
305
+ expect(result).to include(success: true, entry_id: winner_id)
306
+ end
359
307
 
360
- expect(result).to include(success: false, error: 'duplicate key value violates unique constraint')
308
+ it 'returns failure when a uniqueness conflict has no visible winner' do
309
+ existing_entry.destroy
310
+ allow(entry_model).to receive(:create).and_raise(Sequel::UniqueConstraintViolation, 'duplicate hash')
311
+
312
+ expect(host.handle_ingest(content: 'test', content_hash: 'same-hash')).to include(
313
+ success: false, error: 'duplicate hash'
314
+ )
315
+ expect(entry_model.count).to eq(0)
361
316
  end
362
317
  end
318
+
319
+ it 'rejects private writes without an explicit usable owner' do
320
+ result = host.handle_ingest(content: 'private fact', access_scope: 'private', identity_principal_id: nil)
321
+
322
+ expect(result).to include(success: false, error: 'identity_required')
323
+ expect(entry_model.count).to eq(0)
324
+ end
363
325
  end
364
326
 
365
327
  context 'when Sequel raises an error' do
366
328
  before do
367
- stub_const('Legion::Data::Model::ApolloEntry', Class.new)
368
- stub_const('Legion::Data::Model::ApolloExpertise', Class.new)
369
- stub_const('Legion::Data::Model::ApolloAccessLog', Class.new)
370
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
371
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
372
- allow(Legion::Data::Model::ApolloEntry).to receive(:where)
329
+ stub_const('Legion::Data::Model::Apollo::Entry', Class.new)
330
+ stub_const('Legion::Data::Model::Apollo::Expertise', Class.new)
331
+ stub_const('Legion::Data::Model::Apollo::AccessLog', Class.new)
332
+ allow(Legion::Data::Model::Apollo::Entry).to receive(:where)
373
333
  .and_raise(Sequel::Error, 'connection lost')
374
334
  end
375
335
 
@@ -379,231 +339,62 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
379
339
  result = host.handle_ingest(content: 'test', content_type: 'fact', source_agent: 'a')
380
340
  expect(result[:success]).to be false
381
341
  expect(result[:error]).to eq('connection lost')
382
- expect(host).to have_received(:handle_exception).with(
383
- instance_of(Sequel::Error),
384
- level: :error,
385
- operation: 'apollo.knowledge.handle_ingest'
386
- )
387
342
  end
388
343
  end
389
344
 
390
- context 'identity from Legion::Identity::Process' do
391
- let(:mock_entry_class2) { double('ApolloEntry2') }
392
- let(:mock_expertise_class2) { double('ApolloExpertise2') }
393
- let(:mock_access_log_class2) { double('ApolloAccessLog2') }
394
- let(:mock_entry2) { double('entry2', id: 99, embedding: nil) }
395
- let(:mock_db2) { double('db2') }
396
-
397
- before do
398
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class2)
399
- stub_const('Legion::Data::Model::ApolloExpertise', mock_expertise_class2)
400
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class2)
401
- scoped_ds = double('scoped_ds2', first: nil, where: double('scoped_ds2b', first: nil))
402
- allow(mock_entry_class2).to receive(:where).and_return(scoped_ds)
403
- allow(scoped_ds).to receive(:exclude).and_return(scoped_ds)
404
- allow(mock_entry_class2).to receive(:db).and_return(mock_db2)
405
- allow(mock_db2).to receive(:transaction) { |**_options, &block| block.call }
406
- allow(mock_expertise_class2).to receive(:where).and_return(double(first: nil))
407
- allow(mock_expertise_class2).to receive(:create)
408
- allow(mock_access_log_class2).to receive(:create)
409
- allow(host).to receive(:embed_text).and_return(nil)
410
- allow(host).to receive(:find_corroboration).and_return([false, nil])
411
- allow(host).to receive(:detect_contradictions).and_return([])
412
- stub_const('Legion::Identity::Process', double(
413
- db_principal_id: 42,
414
- db_identity_id: 7,
415
- canonical_name: 'alice'
416
- ))
345
+ context 'invalid content' do
346
+ it 'returns an actionable failure for missing content' do
347
+ expect(host.handle_ingest(content: nil)).to include(success: false, error: 'content is required')
417
348
  end
418
349
 
419
- it 'derives identity from Legion::Identity::Process and persists it' do
420
- expect(mock_entry_class2).to receive(:create).with(
421
- hash_including(identity_principal_id: 42, identity_id: 7, identity_canonical_name: 'alice',
422
- access_scope: 'private')
423
- ).and_return(mock_entry2)
424
-
425
- host.handle_ingest(content: 'test fact', tags: [], access_scope: 'private')
426
- end
427
-
428
- it 'defaults access_scope to global when not provided' do
429
- expect(mock_entry_class2).to receive(:create).with(
430
- hash_including(access_scope: 'global')
431
- ).and_return(mock_entry2)
432
-
433
- host.handle_ingest(content: 'test fact', tags: [])
434
- end
350
+ it 'does not allow newline-bearing tags to forge warning log lines' do
351
+ logger = double('logger', debug: nil, warn: nil)
352
+ allow(host).to receive(:log).and_return(logger)
435
353
 
436
- it 'ignores identity kwargs passed by callers' do
437
- expect(mock_entry_class2).to receive(:create).with(
438
- hash_including(identity_principal_id: 42, identity_id: 7, identity_canonical_name: 'alice')
439
- ).and_return(mock_entry2)
354
+ host.handle_ingest(content: nil, tags: ["evil\nFAKE LOG LINE", 'normal'])
440
355
 
441
- host.handle_ingest(
442
- content: 'test fact', tags: [],
443
- identity_principal_id: 999, identity_id: 888, identity_canonical_name: 'mallory',
444
- access_scope: 'private'
445
- )
446
- end
447
- end
448
-
449
- context 'dedup: private entries from different principals are not deduplicated' do
450
- let(:mock_entry_class3) { double('ApolloEntry3') }
451
- let(:mock_expertise_class3) { double('ApolloExpertise3') }
452
- let(:mock_access_log_class3) { double('ApolloAccessLog3') }
453
- let(:mock_entry_a) { double('entry_a', id: 7, embedding: nil) }
454
- let(:mock_entry_b) { double('entry_b', id: 8, embedding: nil) }
455
- let(:mock_db3) { double('db3') }
456
-
457
- before do
458
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class3)
459
- stub_const('Legion::Data::Model::ApolloExpertise', mock_expertise_class3)
460
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class3)
461
- allow(mock_expertise_class3).to receive(:where).and_return(double(first: nil))
462
- allow(mock_expertise_class3).to receive(:create)
463
- allow(mock_access_log_class3).to receive(:create)
464
- allow(host).to receive(:embed_text).and_return(nil)
465
- allow(host).to receive(:find_corroboration).and_return([false, nil])
466
- allow(host).to receive(:detect_contradictions).and_return([])
467
- # default dedup returns nil (no duplicate) — supports chained .where for private scope
468
- scoped_ds3 = double('dedup_chain3', first: nil)
469
- allow(scoped_ds3).to receive(:where).and_return(double('scoped_ds3b', first: nil))
470
- allow(scoped_ds3).to receive(:exclude).and_return(scoped_ds3)
471
- allow(mock_entry_class3).to receive(:where).and_return(scoped_ds3)
472
- allow(mock_entry_class3).to receive(:db).and_return(mock_db3)
473
- allow(mock_db3).to receive(:transaction) { |**_options, &block| block.call }
474
- # two different principals each get their own entry
475
- allow(mock_entry_class3).to receive(:create).and_return(mock_entry_a, mock_entry_b)
476
- end
477
-
478
- it 'does not deduplicate private entries from different principals' do
479
- result1 = host.handle_ingest(content: 'same fact', content_type: :observation,
480
- access_scope: 'private', identity_principal_id: 1)
481
- result2 = host.handle_ingest(content: 'same fact', content_type: :observation,
482
- access_scope: 'private', identity_principal_id: 2)
483
- expect(result1[:entry_id]).not_to eq(result2[:entry_id])
484
- end
485
- end
486
-
487
- context 'early-return warn logs' do
488
- let(:logger) { instance_double('Logger', debug: nil, info: nil, warn: nil) }
489
-
490
- before { allow(host).to receive(:log).and_return(logger) }
491
-
492
- it 'emits a warn log when content is nil' do
493
- host.handle_ingest(content: nil, content_type: 'fact')
494
- expect(logger).to have_received(:warn).with(/early-return: content is required/)
495
- end
496
-
497
- it 'does not emit a content_type warn when content_type is nil (defaults to :observation)' do
498
- hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry)
499
- host.handle_ingest(content: 'something', content_type: nil)
500
- expect(logger).not_to have_received(:warn).with(/content_type is required/)
501
- end
502
-
503
- it 'emits a warn log when apollo_data_not_available' do
504
- hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry)
505
- host.handle_ingest(content: 'something', content_type: 'fact')
506
- expect(logger).to have_received(:warn).with(/early-return: apollo_data_not_available/)
507
- end
508
-
509
- it 'sanitizes newline-bearing tags in the warn log' do
510
- host.handle_ingest(content: nil, content_type: 'fact', tags: ["evil\nFAKE LOG LINE", 'normal'])
511
- expect(logger).to have_received(:warn) do |msg|
512
- expect(msg).to include('evil FAKE LOG LINE')
513
- expect(msg).not_to include("\n")
356
+ expect(logger).to have_received(:warn) do |message|
357
+ expect(message).to include('evil FAKE LOG LINE')
358
+ expect(message).not_to include("\n")
514
359
  end
515
360
  end
516
361
  end
517
362
 
518
- context 'contradiction detection flag' do
519
- let(:mock_entry_class) { double('ApolloEntry') }
520
- let(:mock_relation_class) { double('ApolloRelation') }
521
- let(:mock_expertise_class) { double('ApolloExpertise') }
522
- let(:mock_access_log_class) { double('ApolloAccessLog') }
523
- let(:mock_entry) { double('entry', id: 'uuid-new', embedding: nil) }
524
- let(:empty_dataset) { double('dataset', each: nil) }
525
- let(:mock_db) { double('db') }
526
-
527
- before do
528
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
529
- stub_const('Legion::Data::Model::ApolloRelation', mock_relation_class)
530
- stub_const('Legion::Data::Model::ApolloExpertise', mock_expertise_class)
531
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class)
532
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
533
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
534
- allow(mock_entry_class).to receive(:where).and_return(double(exclude: double(limit: empty_dataset)))
535
- dedup_chain = double('dedup_chain')
536
- allow(mock_entry_class).to receive(:where).with(content_hash: anything).and_return(dedup_chain)
537
- allow(dedup_chain).to receive(:exclude).with(status: 'archived').and_return(double(first: nil))
538
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
539
- allow(mock_db).to receive(:fetch).and_return(double(all: []))
540
- allow(mock_db).to receive(:transaction) { |**_options, &block| block.call }
541
- allow(mock_entry_class).to receive(:create).and_return(mock_entry)
542
- allow(mock_expertise_class).to receive(:where).and_return(double(first: nil))
543
- allow(mock_expertise_class).to receive(:create)
544
- allow(mock_access_log_class).to receive(:create)
545
- end
546
-
547
- it 'defaults contradiction detection to disabled' do
548
- expect(Legion::Extensions::Apollo.settings[:contradiction][:enabled]).to be false
549
- end
363
+ context 'contradiction detection' do
364
+ include_context 'persisted knowledge'
550
365
 
551
- it 'does not call detect_contradictions when flag is false' do
552
- expect(host).not_to receive(:detect_contradictions)
553
- result = host.handle_ingest(content: 'test fact', content_type: 'fact', source_agent: 'agent-1')
554
- expect(result[:success]).to be true
555
- expect(result[:contradictions]).to eq([])
366
+ around do |example|
367
+ scanner = Legion::Extensions::Apollo::Runners::Contradictions
368
+ previous_queue = scanner.instance_variable_get(:@contradictions_queue)
369
+ scanner.instance_variable_set(:@contradictions_queue, Queue.new)
370
+ example.run
371
+ ensure
372
+ scanner.instance_variable_set(:@contradictions_queue, previous_queue)
556
373
  end
557
374
 
558
- it 'returns stable contradictions key as empty array when disabled' do
559
- result = host.handle_ingest(content: 'test content', content_type: 'fact', source_agent: 'agent-1')
560
- expect(result).to have_key(:contradictions)
561
- expect(result[:contradictions]).to eq([])
375
+ it 'returns accepted ingestion without synchronous contradiction results when disabled' do
376
+ expect(host.handle_ingest(content: 'test fact')).to include(success: true, contradictions: [])
562
377
  end
563
378
 
564
- context 'when enabled' do
565
- before do
566
- Legion::Settings[:extensions][:apollo][:contradiction][:enabled] = true
567
- stub_const('Legion::Extensions::Apollo::Actor::ContradictionScanner',
568
- Class.new do
569
- @queue = Queue.new
570
- class << self
571
- attr_reader :queue
572
-
573
- def enqueue(entry_id:, embedding:, content:)
574
- queue.push({ entry_id: entry_id, embedding: embedding, content: content })
575
- end
576
-
577
- def pending_count = queue.size
578
-
579
- def drain
580
- items = []
581
- items << queue.pop until queue.empty?
582
- items
583
- end
584
- end
585
- end)
586
- end
587
-
588
- after do
589
- Legion::Settings[:extensions][:apollo][:contradiction][:enabled] = false
590
- end
379
+ it 'makes enabled ingestion available to the asynchronous contradiction scan' do
380
+ scanner = Legion::Extensions::Apollo::Runners::Contradictions
381
+ allow(scanner).to receive(:settings).and_return(Legion::Extensions::Apollo.settings)
382
+ Legion::Extensions::Apollo.settings[:contradiction][:enabled] = true
383
+ result = host.handle_ingest(content: 'enabled test')
591
384
 
592
- it 'enqueues contradiction work to the actor instead of running inline' do
593
- result = host.handle_ingest(content: 'enabled test', content_type: 'fact', source_agent: 'agent-1')
594
- expect(result[:success]).to be true
595
- expect(result[:contradictions]).to eq([])
596
- expect(Legion::Extensions::Apollo::Actor::ContradictionScanner.pending_count).to eq(1)
597
- end
385
+ expect(result).to include(success: true, contradictions: [])
386
+ expect(entry_model[result[:entry_id]].content).to eq('enabled test')
387
+ expect(scanner.scan_pending_contradictions).to include(success: true, processed: 1, failed: 0)
388
+ expect(scanner.scan_pending_contradictions).to include(success: true, processed: 0)
598
389
  end
599
390
  end
600
391
  end
601
392
 
602
393
  describe '#handle_query' do
603
- let(:host) { Object.new.extend(described_class) }
394
+ let(:host) { runner }
604
395
 
605
396
  context 'when Apollo data is not available' do
606
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
397
+ before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
607
398
 
608
399
  it 'returns a structured error' do
609
400
  result = host.handle_query(query: 'test')
@@ -613,11 +404,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
613
404
  end
614
405
 
615
406
  context 'when semantic query access logging is unavailable' do
616
- let(:mock_entry_class) { double('ApolloEntry') }
407
+ let(:mock_entry_class) { double('Apollo::Entry') }
617
408
 
618
409
  before do
619
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
620
- hide_const('Legion::Data::Model::ApolloAccessLog') if defined?(Legion::Data::Model::ApolloAccessLog)
410
+ stub_const('Legion::Data::Model::Apollo::Entry', mock_entry_class)
621
411
  hide_const('Legion::Data::Model::Apollo::AccessLog') if defined?(Legion::Data::Model::Apollo::AccessLog)
622
412
  end
623
413
 
@@ -632,25 +422,9 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
632
422
  end
633
423
 
634
424
  context 'when Apollo data is available' do
635
- let(:mock_entry_class) { double('ApolloEntry') }
636
- let(:mock_access_log_class) { double('ApolloAccessLog') }
637
- let(:mock_db) { double('db') }
638
- let(:sample_entries) do
639
- [{ id: 'uuid-1', content: 'Ruby is interpreted', content_type: 'fact',
640
- confidence: 0.8, distance: 0.15, tags: ['ruby'], source_agent: 'agent-1',
641
- access_count: 3 }]
642
- end
425
+ include_context 'persisted knowledge'
643
426
 
644
- before do
645
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
646
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class)
647
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
648
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
649
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
650
- allow(mock_db).to receive(:fetch).and_return(double(all: sample_entries))
651
- allow(mock_entry_class).to receive(:where).and_return(double(update: true))
652
- allow(mock_access_log_class).to receive(:create)
653
- end
427
+ before { persisted_entry }
654
428
 
655
429
  it 'returns matching entries' do
656
430
  result = host.handle_query(query: 'Ruby', agent_id: 'agent-2')
@@ -658,33 +432,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
658
432
  expect(result[:count]).to eq(1)
659
433
  expect(result[:entries].first[:content]).to eq('Ruby is interpreted')
660
434
  end
661
-
662
- it 'boosts access count on matched entries' do
663
- expect(mock_entry_class).to receive(:where).with(id: 'uuid-1')
664
- .and_return(double(update: true))
665
- host.handle_query(query: 'Ruby', agent_id: 'agent-2')
666
- end
667
-
668
- it 'logs query access' do
669
- expect(mock_access_log_class).to receive(:create).with(
670
- hash_including(agent_id: 'agent-2', action: 'query')
671
- )
672
- host.handle_query(query: 'Ruby', agent_id: 'agent-2')
673
- end
674
435
  end
675
436
 
676
437
  context 'when no results found' do
677
- let(:mock_entry_class) { double('ApolloEntry') }
678
- let(:mock_db) { double('db') }
679
-
680
- before do
681
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
682
- stub_const('Legion::Data::Model::ApolloAccessLog', Class.new)
683
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
684
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
685
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
686
- allow(mock_db).to receive(:fetch).and_return(double(all: []))
687
- end
438
+ include_context 'persisted knowledge'
688
439
 
689
440
  it 'returns empty entries array' do
690
441
  result = host.handle_query(query: 'nonexistent')
@@ -695,66 +446,51 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
695
446
  end
696
447
 
697
448
  context 'when query is browse-shaped' do
698
- let(:mock_entry_class) { double('ApolloEntry') }
699
- let(:dataset) { double('dataset') }
700
- let(:entries) do
701
- [{ id: 'uuid-1', content: 'Candidate fact', content_type: 'fact',
702
- confidence: 0.7, tags: ['ruby'], source_agent: 'agent-1',
703
- knowledge_domain: 'general' }]
704
- end
449
+ include_context 'persisted knowledge'
705
450
 
706
- before do
707
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
708
- allow(mock_entry_class).to receive(:exclude).with(status: 'archived').and_return(dataset)
709
- allow(dataset).to receive(:where).and_return(dataset)
710
- allow(dataset).to receive(:order).with(:created_at).and_return(dataset)
711
- allow(dataset).to receive(:limit).with(50).and_return(dataset)
712
- allow(dataset).to receive(:all).and_return(entries)
713
- end
451
+ let!(:candidate) { persisted_entry(content: 'Candidate fact', status: 'candidate') }
452
+ let!(:confirmed) { persisted_entry(content: 'Confirmed fact', tags: '["clinical"]', knowledge_domain: 'clinical') }
714
453
 
715
- it 'lists recent non-archived entries without generating an embedding' do
454
+ it 'includes candidates without generating an embedding' do
716
455
  expect(Legion::LLM::Call::Embeddings).not_to receive(:generate)
717
456
 
718
- result = host.handle_query(query: 'x', limit: 50)
457
+ result = host.handle_query(query: 'x')
719
458
 
720
- expect(result[:success]).to be true
721
- expect(result[:mode]).to eq(:browse)
722
- expect(result[:count]).to eq(1)
723
- expect(result[:entries].first[:content]).to eq('Candidate fact')
724
- expect(dataset).not_to have_received(:where).with(status: ['confirmed'])
459
+ expect(result).to include(success: true, mode: :browse)
460
+ expect(result[:entries].map { |entry| entry[:id] }).to contain_exactly(candidate.id, confirmed.id)
725
461
  end
726
462
 
727
- it 'respects an explicit confirmed status filter' do
728
- host.handle_query(query: 'x', limit: 50, status: [:confirmed])
463
+ it 'honors an explicit confirmed status filter' do
464
+ result = host.handle_query(query: 'x', status: [:confirmed])
729
465
 
730
- expect(dataset).to have_received(:where).with(status: ['confirmed'])
466
+ expect(result[:entries]).to contain_exactly(include(id: confirmed.id))
731
467
  end
732
468
 
733
- it 'applies tags and domain filters when provided' do
734
- host.handle_query(query: 'x', limit: 50, tags: ['ruby'], domain: 'general')
735
-
736
- expect(dataset).to have_received(:where).with('tags && ?')
737
- expect(dataset).to have_received(:where).with(knowledge_domain: 'general')
469
+ it 'intersects tags and domain filters' do
470
+ expect(host.handle_query(query: 'x', tags: ['clinical'], domain: 'clinical')[:entries])
471
+ .to contain_exactly(include(id: confirmed.id))
472
+ expect(host.handle_query(query: 'x', tags: ['ruby'], domain: 'clinical')[:entries]).to eq([])
738
473
  end
739
- end
740
- end
741
474
 
742
- describe '#normalize_text_input' do
743
- let(:host) { Object.new.extend(described_class) }
475
+ it 'does not disguise provider failure as browse results' do
476
+ allow(Legion::LLM::Call::Embeddings).to receive(:generate).and_raise(Legion::LLM::LLMError, 'provider unavailable')
744
477
 
745
- it 'strips null bytes in the local fallback path' do
746
- expect(host.send(:normalize_text_input, "hello\x00world")).to eq('helloworld')
747
- end
478
+ expect(host.handle_query(query: 'clinical evidence')).to include(success: false, error: 'provider unavailable')
479
+ expect(candidate.refresh.confidence).to eq(0.8)
480
+ expect(confirmed.refresh.confidence).to eq(0.8)
481
+ expect(database[:apollo_access_logs].count).to eq(0)
482
+ end
748
483
 
749
- it 'scrubs invalid UTF-8 in the local fallback path' do
750
- invalid = "hello\xC3(world".dup.force_encoding(Encoding::UTF_8)
484
+ it 'propagates programming errors instead of presenting empty search results' do
485
+ allow(Legion::LLM::Call::Embeddings).to receive(:generate).and_raise(NoMethodError, 'provider bug')
751
486
 
752
- expect(host.send(:normalize_text_input, invalid)).to eq('hello(world')
487
+ expect { host.handle_query(query: 'clinical evidence') }.to raise_error(NoMethodError, 'provider bug')
488
+ end
753
489
  end
754
490
  end
755
491
 
756
492
  describe '#retrieve_relevant' do
757
- let(:host) { Object.new.extend(described_class) }
493
+ let(:host) { runner }
758
494
 
759
495
  it 'returns skipped when skip is true' do
760
496
  result = host.retrieve_relevant(skip: true)
@@ -762,7 +498,7 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
762
498
  end
763
499
 
764
500
  context 'when Apollo data is not available' do
765
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
501
+ before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
766
502
 
767
503
  it 'returns a structured error' do
768
504
  result = host.retrieve_relevant(query: 'test')
@@ -770,50 +506,45 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
770
506
  end
771
507
  end
772
508
 
773
- it 'returns empty when query is nil' do
774
- stub_const('Legion::Data::Model::ApolloEntry', Class.new)
775
- result = host.retrieve_relevant(query: nil)
776
- expect(result[:success]).to be true
777
- expect(result[:entries]).to eq([])
778
- end
509
+ context 'with a canonical database' do
510
+ include_context 'persisted knowledge'
779
511
 
780
- it 'returns empty when query is blank' do
781
- stub_const('Legion::Data::Model::ApolloEntry', Class.new)
782
- result = host.retrieve_relevant(query: ' ')
783
- expect(result[:success]).to be true
784
- expect(result[:entries]).to eq([])
785
- end
512
+ it 'returns empty when query is nil' do
513
+ result = host.retrieve_relevant(query: nil)
786
514
 
787
- context 'with valid query and data available' do
788
- let(:mock_entry_class) { double('ApolloEntry') }
789
- let(:mock_db) { double('db') }
790
- let(:sample_entries) do
791
- [{ id: 'uuid-1', content: 'fact', content_type: 'fact',
792
- confidence: 0.7, distance: 0.2, tags: ['ruby'], source_agent: 'agent-1' }]
515
+ expect(result[:success]).to be true
516
+ expect(result[:entries]).to eq([])
793
517
  end
794
518
 
795
- before do
796
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
797
- allow(Legion::LLM::Call::Embeddings).to receive(:generate)
798
- .and_return({ vector: Array.new(1024, 0.1), model: 'test', provider: :ollama, dimensions: 1024, tokens: 0 })
799
- allow(mock_entry_class).to receive(:db).and_return(mock_db)
800
- allow(mock_db).to receive(:fetch).and_return(double(all: sample_entries))
801
- allow(mock_entry_class).to receive(:where).and_return(double(update: true))
519
+ it 'returns empty when query is blank' do
520
+ result = host.retrieve_relevant(query: ' ')
521
+
522
+ expect(result[:success]).to be true
523
+ expect(result[:entries]).to eq([])
802
524
  end
525
+ end
526
+
527
+ context 'with valid query and data available' do
528
+ include_context 'persisted knowledge'
529
+
530
+ let!(:entry) { persisted_entry }
803
531
 
804
532
  it 'returns entries without access logging' do
805
533
  result = host.retrieve_relevant(query: 'Ruby facts')
806
534
  expect(result[:success]).to be true
807
535
  expect(result[:count]).to eq(1)
536
+ expect(result[:entries]).to contain_exactly(include(id: entry.id, content: entry.content))
537
+ expect(database[:apollo_access_logs].count).to eq(0)
538
+ expect(entry.refresh.confidence).to be > 0.8
808
539
  end
809
540
  end
810
541
  end
811
542
 
812
543
  describe '#handle_traverse' do
813
- let(:host) { Object.new.extend(described_class) }
544
+ let(:host) { runner }
814
545
 
815
546
  context 'when Apollo data is not available' do
816
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
547
+ before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
817
548
 
818
549
  it 'returns a structured error' do
819
550
  result = host.handle_traverse(entry_id: 'uuid-123')
@@ -823,8 +554,8 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
823
554
  end
824
555
 
825
556
  context 'when Apollo data is available' do
826
- let(:mock_entry_class) { double('ApolloEntry') }
827
- let(:mock_access_log_class) { double('ApolloAccessLog') }
557
+ let(:mock_entry_class) { double('Apollo::Entry') }
558
+ let(:mock_access_log_class) { double('Apollo::AccessLog') }
828
559
  let(:mock_db) { double('db') }
829
560
  let(:traversal_results) do
830
561
  [{ id: 'uuid-1', content: 'root', content_type: 'fact',
@@ -836,8 +567,8 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
836
567
  end
837
568
 
838
569
  before do
839
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
840
- stub_const('Legion::Data::Model::ApolloAccessLog', mock_access_log_class)
570
+ stub_const('Legion::Data::Model::Apollo::Entry', mock_entry_class)
571
+ stub_const('Legion::Data::Model::Apollo::AccessLog', mock_access_log_class)
841
572
  allow(mock_entry_class).to receive(:db).and_return(mock_db)
842
573
  allow(mock_db).to receive(:fetch).and_return(double(all: traversal_results))
843
574
  allow(mock_access_log_class).to receive(:create)
@@ -850,24 +581,12 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
850
581
  expect(result[:entries].first[:depth]).to eq(0)
851
582
  expect(result[:entries].last[:activation]).to eq(0.48)
852
583
  end
853
-
854
- it 'logs access for known agents' do
855
- expect(mock_access_log_class).to receive(:create).with(
856
- hash_including(agent_id: 'agent-x', action: 'query')
857
- )
858
- host.handle_traverse(entry_id: 'uuid-1', agent_id: 'agent-x')
859
- end
860
-
861
- it 'skips access logging for unknown agents' do
862
- expect(mock_access_log_class).not_to receive(:create)
863
- host.handle_traverse(entry_id: 'uuid-1')
864
- end
865
584
  end
866
585
 
867
586
  context 'when Sequel raises an error' do
868
587
  before do
869
- stub_const('Legion::Data::Model::ApolloEntry', Class.new)
870
- allow(Legion::Data::Model::ApolloEntry).to receive(:db)
588
+ stub_const('Legion::Data::Model::Apollo::Entry', Class.new)
589
+ allow(Legion::Data::Model::Apollo::Entry).to receive(:db)
871
590
  .and_raise(Sequel::Error, 'connection lost')
872
591
  end
873
592
 
@@ -880,10 +599,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
880
599
  end
881
600
 
882
601
  describe '#redistribute_knowledge' do
883
- let(:host) { Object.new.extend(described_class) }
602
+ let(:host) { runner }
884
603
 
885
604
  context 'when Apollo data is not available' do
886
- before { hide_const('Legion::Data::Model::ApolloEntry') if defined?(Legion::Data::Model::ApolloEntry) }
605
+ before { hide_const('Legion::Data::Model::Apollo') if defined?(Legion::Data::Model::Apollo) }
887
606
 
888
607
  it 'returns a structured error' do
889
608
  result = host.redistribute_knowledge(agent_id: 'agent-x')
@@ -893,14 +612,7 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
893
612
  end
894
613
 
895
614
  context 'when the departing agent has no confirmed entries' do
896
- let(:mock_entry_class) { double('ApolloEntry') }
897
-
898
- before do
899
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
900
- chain = double('chain')
901
- allow(mock_entry_class).to receive(:where).and_return(chain)
902
- allow(chain).to receive(:where).and_return(double(all: []))
903
- end
615
+ include_context 'persisted knowledge'
904
616
 
905
617
  it 'returns success with zero redistributed' do
906
618
  result = host.redistribute_knowledge(agent_id: 'departed-1')
@@ -910,17 +622,18 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
910
622
  end
911
623
 
912
624
  context 'when the departing agent has confirmed entries' do
913
- let(:mock_entry_class) { double('ApolloEntry') }
914
- let(:mock_entry) do
915
- double('entry', content: 'Ruby is fast', content_type: 'fact',
916
- confidence: 0.8, tags: ['ruby'])
917
- end
625
+ include_context 'persisted knowledge'
626
+
627
+ let(:stored_traces) { [] }
918
628
 
919
629
  before do
920
- stub_const('Legion::Data::Model::ApolloEntry', mock_entry_class)
921
- chain = double('chain')
922
- allow(mock_entry_class).to receive(:where).and_return(chain)
923
- allow(chain).to receive(:where).and_return(double(all: [mock_entry]))
630
+ persisted_entry(source_agent: 'departed-2')
631
+ store = double('trace store')
632
+ allow(store).to receive(:store) { |trace| stored_traces << trace }
633
+ stub_const('Legion::Extensions::Agentic::Memory::Trace', Module.new)
634
+ allow(Legion::Extensions::Agentic::Memory::Trace).to receive(:shared_store).and_return(store)
635
+ stub_const('Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace', Module.new)
636
+ allow(Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace).to receive(:new_trace) { |**attributes| attributes }
924
637
  end
925
638
 
926
639
  it 'returns the count of redistributed entries' do
@@ -928,30 +641,22 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
928
641
  expect(result[:success]).to be true
929
642
  expect(result[:redistributed]).to eq(1)
930
643
  expect(result[:agent_id]).to eq('departed-2')
644
+ expect(stored_traces).to contain_exactly(include(content_payload: include(content: 'Ruby is interpreted', source_agent: 'departed-2')))
931
645
  end
932
646
 
933
- it 'stores into trace shared_store when Memory::Trace is available' do
934
- mock_store = double('store')
935
- mock_trace_helpers = Module.new do
936
- def self.new_trace(type:, **kwargs)
937
- { trace_id: 'trace-abc', trace_type: type, strength: kwargs[:strength] || 0.5 }
938
- end
939
- end
940
- stub_const('Legion::Extensions::Agentic::Memory::Trace', Module.new)
941
- stub_const('Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace', mock_trace_helpers)
942
- allow(Legion::Extensions::Agentic::Memory::Trace).to receive(:shared_store).and_return(mock_store)
943
- allow(mock_store).to receive(:store)
647
+ it 'does not report redistribution when the memory sink is unavailable' do
648
+ allow(Legion::Extensions::Agentic::Memory::Trace).to receive(:shared_store).and_return(nil)
944
649
 
945
- result = host.redistribute_knowledge(agent_id: 'departed-3')
946
- expect(result[:redistributed]).to eq(1)
947
- expect(mock_store).to have_received(:store).once
650
+ expect(host.redistribute_knowledge(agent_id: 'departed-2')).to include(
651
+ success: false, error: 'memory_trace_not_available', redistributed: 0
652
+ )
948
653
  end
949
654
  end
950
655
 
951
656
  context 'when Sequel raises an error' do
952
657
  before do
953
- stub_const('Legion::Data::Model::ApolloEntry', Class.new)
954
- allow(Legion::Data::Model::ApolloEntry).to receive(:where)
658
+ stub_const('Legion::Data::Model::Apollo::Entry', Class.new)
659
+ allow(Legion::Data::Model::Apollo::Entry).to receive(:where)
955
660
  .and_raise(Sequel::Error, 'db error')
956
661
  end
957
662
 
@@ -964,10 +669,10 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
964
669
  end
965
670
 
966
671
  describe '#prepare_mesh_export' do
967
- let(:host) { Object.new.extend(described_class) }
672
+ let(:host) { runner }
968
673
 
969
674
  context 'when Legion::Data is not available' do
970
- before { hide_const('Legion::Data') if defined?(Legion::Data) }
675
+ before { allow(Legion::Data).to receive(:connection).and_return(nil) }
971
676
 
972
677
  it 'returns a structured error' do
973
678
  result = host.prepare_mesh_export(target_domain: 'clinical_care')
@@ -977,61 +682,43 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
977
682
  end
978
683
 
979
684
  context 'when data is available' do
980
- let(:mock_conn) { double('connection') }
981
- let(:mock_dataset) { double('dataset') }
982
- let(:clinical_entry) do
983
- { id: 'e1', content: 'treatment', content_type: 'fact',
984
- confidence: 0.8, knowledge_domain: 'clinical_care',
985
- tags: ['clinical'], source_agent: 'agent-1' }
986
- end
987
- let(:claims_entry) do
988
- { id: 'e2', content: 'denial', content_type: 'fact',
989
- confidence: 0.7, knowledge_domain: 'claims_optimization',
990
- tags: ['claims'], source_agent: 'agent-2' }
991
- end
685
+ include_context 'persisted knowledge'
992
686
 
993
- before do
994
- data_mod = Module.new do
995
- def self.connection; end
687
+ let!(:clinical_entry) { persisted_entry(content: 'treatment', knowledge_domain: 'clinical_care') }
688
+ let!(:claims_entry) { persisted_entry(content: 'denial', knowledge_domain: 'claims_optimization') }
996
689
 
997
- def self.respond_to?(method, *args)
998
- method == :connection || super
999
- end
1000
- end
1001
- stub_const('Legion::Data', data_mod)
1002
- allow(Legion::Data).to receive(:connection).and_return(mock_conn)
1003
- allow(mock_conn).to receive(:[]).with(:apollo_entries).and_return(mock_dataset)
1004
- allow(mock_dataset).to receive(:where).and_return(mock_dataset)
1005
- allow(mock_dataset).to receive(:limit).and_return(mock_dataset)
690
+ before do
691
+ allow(Legion::Data).to receive(:connection).and_return(database)
692
+ persisted_entry(content: 'private treatment', knowledge_domain: 'clinical_care',
693
+ access_scope: 'private', identity_principal_id: 7)
1006
694
  end
1007
695
 
1008
- it 'filters entries by domain compatibility for clinical_care' do
1009
- allow(mock_dataset).to receive(:all).and_return([clinical_entry])
696
+ it 'filters entries by domain compatibility and excludes private knowledge' do
1010
697
  result = host.prepare_mesh_export(target_domain: 'clinical_care')
1011
- expect(result[:success]).to be true
1012
- expect(result[:target_domain]).to eq('clinical_care')
698
+
699
+ expect(result).to include(success: true, target_domain: 'clinical_care')
700
+ expect(result[:entries]).to contain_exactly(include(id: clinical_entry.id))
1013
701
  end
1014
702
 
1015
- it 'allows all domains when target is general' do
1016
- allow(mock_dataset).to receive(:all).and_return([clinical_entry, claims_entry])
703
+ it 'allows all public domains when target is general' do
1017
704
  result = host.prepare_mesh_export(target_domain: 'general')
1018
- expect(result[:success]).to be true
1019
- expect(result[:count]).to eq(2)
705
+
706
+ expect(result[:entries].map { |entry| entry[:id] }).to contain_exactly(clinical_entry.id, claims_entry.id)
1020
707
  end
1021
708
 
1022
709
  it 'restricts claims_optimization to only claims entries' do
1023
- allow(mock_dataset).to receive(:all).and_return([claims_entry])
1024
710
  result = host.prepare_mesh_export(target_domain: 'claims_optimization')
1025
- expect(result[:success]).to be true
711
+
712
+ expect(result[:entries]).to contain_exactly(include(id: claims_entry.id))
1026
713
  end
1027
714
  end
1028
715
  end
1029
716
 
1030
717
  describe '#handle_erasure_request' do
1031
- let(:host) { Object.new.extend(described_class) }
718
+ let(:host) { runner }
1032
719
 
1033
720
  context 'when Legion::Data is not available' do
1034
- before { hide_const('Legion::Data') if defined?(Legion::Data) }
721
+ before { allow(Legion::Data).to receive(:connection).and_return(nil) }
1035
722
 
1036
723
  it 'returns zero counts with error' do
1037
724
  result = host.handle_erasure_request(agent_id: 'agent-dead')
@@ -1042,56 +729,28 @@ RSpec.describe Legion::Extensions::Apollo::Runners::Knowledge do
1042
729
  end
1043
730
 
1044
731
  context 'when data is available' do
1045
- let(:mock_conn) { double('connection') }
1046
- let(:mock_dataset) { double('dataset') }
732
+ include_context 'persisted knowledge'
1047
733
 
1048
- before do
1049
- data_mod = Module.new do
1050
- def self.connection; end
734
+ before { allow(Legion::Data).to receive(:connection).and_return(database) }
1051
735
 
1052
- def self.respond_to?(method, *args)
1053
- method == :connection || super
1054
- end
1055
- end
1056
- stub_const('Legion::Data', data_mod)
1057
- allow(Legion::Data).to receive(:connection).and_return(mock_conn)
1058
- allow(mock_conn).to receive(:[]).with(:apollo_entries).and_return(mock_dataset)
1059
- allow(mock_dataset).to receive(:where).and_return(mock_dataset)
1060
- allow(mock_dataset).to receive(:exclude).and_return(mock_dataset)
1061
- allow(mock_dataset).to receive(:delete).and_return(3)
1062
- allow(mock_dataset).to receive(:update).and_return(2)
1063
- end
736
+ it 'deletes unconfirmed knowledge and redacts identity without erasing corroborated content or other agents' do
737
+ candidate = persisted_entry(content: 'unconfirmed', status: 'candidate', source_agent: 'agent-dead')
738
+ confirmed = persisted_entry(content: 'corroborated', source_agent: 'agent-dead',
739
+ identity_principal_id: 7, identity_id: 9, identity_canonical_name: 'private-name')
740
+ other = persisted_entry(content: 'other agent', source_agent: 'agent-alive')
1064
741
 
1065
- it 'deletes non-confirmed entries and redacts confirmed ones' do
1066
742
  result = host.handle_erasure_request(agent_id: 'agent-dead')
1067
- expect(result[:deleted]).to eq(3)
1068
- expect(result[:redacted]).to eq(2)
1069
- expect(result[:agent_id]).to eq('agent-dead')
1070
- end
1071
743
 
1072
- it 'clears identity columns on confirmed (redacted) entries' do
1073
- expect(mock_dataset).to receive(:update).with(
1074
- hash_including(
1075
- identity_principal_id: nil,
1076
- identity_id: nil,
1077
- identity_canonical_name: nil
1078
- )
1079
- ).and_return(2)
1080
-
1081
- host.handle_erasure_request(agent_id: 'agent-dead')
744
+ expect(result).to include(deleted: 1, redacted: 1, agent_id: 'agent-dead')
745
+ expect(entry_model[candidate.id]).to be_nil
746
+ expect(confirmed.refresh.values).to include(content: 'corroborated', source_agent: 'redacted',
747
+ identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil)
748
+ expect(other.refresh.source_agent).to eq('agent-alive')
1082
749
  end
1083
750
  end
1084
751
 
1085
752
  context 'when Sequel raises an error' do
1086
753
  before do
1087
- data_mod = Module.new do
1088
- def self.connection; end
1089
-
1090
- def self.respond_to?(method, *args)
1091
- method == :connection || super
1092
- end
1093
- end
1094
- stub_const('Legion::Data', data_mod)
1095
754
  allow(Legion::Data).to receive(:connection).and_return(double('conn').tap do |c|
1096
755
  allow(c).to receive(:[]).and_raise(Sequel::Error, 'db gone')
1097
756
  end)