lex-apollo 0.4.30 → 0.4.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -0
- data/README.md +113 -18
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +116 -51
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
- data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
- data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
- data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -47
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/transport.rb +20 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +250 -28
- data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +71 -13
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
RSpec.describe 'Apollo ingest transaction' do
|
|
9
|
+
let(:ingest_script) do
|
|
10
|
+
<<~'RUBY'
|
|
11
|
+
require 'sequel'
|
|
12
|
+
require 'rspec'
|
|
13
|
+
require 'spec_helper'
|
|
14
|
+
require 'json'
|
|
15
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
16
|
+
db = Sequel.sqlite
|
|
17
|
+
db.create_table(:apollo_entries) do
|
|
18
|
+
primary_key :id
|
|
19
|
+
String :content
|
|
20
|
+
String :content_type
|
|
21
|
+
Float :confidence
|
|
22
|
+
String :source_agent
|
|
23
|
+
String :source_provider
|
|
24
|
+
String :source_channel
|
|
25
|
+
String :source_context
|
|
26
|
+
String :tags
|
|
27
|
+
String :status
|
|
28
|
+
String :knowledge_domain
|
|
29
|
+
String :submitted_by
|
|
30
|
+
String :submitted_from
|
|
31
|
+
String :content_hash
|
|
32
|
+
String :access_scope
|
|
33
|
+
Integer :identity_principal_id
|
|
34
|
+
Integer :identity_id
|
|
35
|
+
String :identity_canonical_name
|
|
36
|
+
String :embedding
|
|
37
|
+
String :embedding_space_id
|
|
38
|
+
Integer :embedding_dimensions
|
|
39
|
+
String :embedding_provenance
|
|
40
|
+
DateTime :created_at
|
|
41
|
+
DateTime :updated_at
|
|
42
|
+
check(Sequel.lit("content != 'entry failure'"))
|
|
43
|
+
end
|
|
44
|
+
db.run <<~SQL
|
|
45
|
+
CREATE UNIQUE INDEX idx_apollo_content_hash_owner ON apollo_entries (
|
|
46
|
+
content_hash, access_scope,
|
|
47
|
+
(CASE WHEN access_scope = 'global' THEN 0 ELSE COALESCE(identity_principal_id, 0) END)
|
|
48
|
+
) WHERE status != 'archived' AND content_hash IS NOT NULL
|
|
49
|
+
SQL
|
|
50
|
+
db.create_table(:apollo_expertise) do
|
|
51
|
+
primary_key :id
|
|
52
|
+
String :agent_id
|
|
53
|
+
String :domain
|
|
54
|
+
Float :proficiency
|
|
55
|
+
Integer :entry_count
|
|
56
|
+
DateTime :last_active_at
|
|
57
|
+
check(Sequel.lit("agent_id != 'fail-expertise'"))
|
|
58
|
+
end
|
|
59
|
+
db.create_table(:apollo_access_log) do
|
|
60
|
+
primary_key :id
|
|
61
|
+
Integer :entry_id
|
|
62
|
+
String :agent_id
|
|
63
|
+
String :action
|
|
64
|
+
check(Sequel.lit("agent_id != 'fail-audit'"))
|
|
65
|
+
end
|
|
66
|
+
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
67
|
+
Legion.const_set(:LLM, Module.new) unless defined?(Legion::LLM)
|
|
68
|
+
Legion::LLM.const_set(:Call, Module.new) unless defined?(Legion::LLM::Call)
|
|
69
|
+
Legion::LLM::Call.const_set(:Embeddings, Module.new) unless defined?(Legion::LLM::Call::Embeddings)
|
|
70
|
+
generated = []
|
|
71
|
+
Legion::LLM::Call::Embeddings.define_singleton_method(:generate) do |text:, task:, **|
|
|
72
|
+
generated << { text: text, task: task }
|
|
73
|
+
raise Legion::LLM::LLMError, 'embedding provider unavailable' if input[:generation_error]
|
|
74
|
+
|
|
75
|
+
ApolloSpecEmbedding.result(task: task)
|
|
76
|
+
end
|
|
77
|
+
Legion::Data.const_set(:Model, Module.new) unless defined?(Legion::Data::Model)
|
|
78
|
+
Legion::Data::Model.const_set(:Apollo, Module.new)
|
|
79
|
+
{ Entry: :apollo_entries, Expertise: :apollo_expertise, AccessLog: :apollo_access_log }.each do |name, table|
|
|
80
|
+
next if name == input[:missing]&.to_sym
|
|
81
|
+
|
|
82
|
+
Legion::Data::Model::Apollo.const_set(name, Class.new(Sequel::Model(db[table])))
|
|
83
|
+
end
|
|
84
|
+
if input[:process_identity]
|
|
85
|
+
Legion.const_set(:Identity, Module.new) unless defined?(Legion::Identity)
|
|
86
|
+
Legion::Identity.const_set(:Process, Module.new)
|
|
87
|
+
Legion::Identity::Process.define_singleton_method(:db_principal_id) { 999 }
|
|
88
|
+
Legion::Identity::Process.define_singleton_method(:db_identity_id) { 998 }
|
|
89
|
+
Legion::Identity::Process.define_singleton_method(:canonical_name) { 'service-node' }
|
|
90
|
+
end
|
|
91
|
+
if input[:existing]
|
|
92
|
+
existing = input[:existing]
|
|
93
|
+
unless input[:legacy]
|
|
94
|
+
embedding = ApolloSpecEmbedding.result
|
|
95
|
+
existing = existing.merge(embedding: ::JSON.generate(embedding[:vector]),
|
|
96
|
+
embedding_space_id: embedding[:embedding_provenance][:space_id],
|
|
97
|
+
embedding_dimensions: embedding[:dimensions],
|
|
98
|
+
embedding_provenance: ::JSON.generate(embedding[:embedding_provenance]))
|
|
99
|
+
end
|
|
100
|
+
db[:apollo_entries].insert(existing)
|
|
101
|
+
end
|
|
102
|
+
before = db[:apollo_entries].all
|
|
103
|
+
runner = Legion::Extensions::Apollo::Runners::Knowledge
|
|
104
|
+
result = runner.handle_ingest(
|
|
105
|
+
content: input[:content], content_type: :fact, source_agent: input[:source_agent], embedding: nil, **input.fetch(:options, {})
|
|
106
|
+
)
|
|
107
|
+
state = { result: result, entries: db[:apollo_entries].count,
|
|
108
|
+
expertise: db[:apollo_expertise].count, access_logs: db[:apollo_access_log].count,
|
|
109
|
+
records: db[:apollo_entries].order(:id).all, before: before, generated: generated }
|
|
110
|
+
puts "APOLLO_RESULT=#{::JSON.generate(state)}"
|
|
111
|
+
RUBY
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def ingest_state(content:, source_agent: 'success', missing: nil, **options)
|
|
115
|
+
stdout, stderr, status = Open3.capture3(
|
|
116
|
+
RbConfig.ruby, '-Ilib', '-Ispec', '-e', ingest_script,
|
|
117
|
+
chdir: File.expand_path('../../../../..', __dir__),
|
|
118
|
+
stdin_data: JSON.generate({ content: content, source_agent: source_agent, missing: missing }.merge(options))
|
|
119
|
+
)
|
|
120
|
+
line = stdout.lines.find { |output| output.start_with?('APOLLO_RESULT=') }
|
|
121
|
+
raise "Ingest transaction process failed: #{stderr}\n#{stdout}" unless status.success? && line
|
|
122
|
+
|
|
123
|
+
JSON.parse(line.delete_prefix('APOLLO_RESULT='), symbolize_names: true)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it 'commits the entry, expertise bookkeeping, and access log as one successful ingest' do
|
|
127
|
+
state = ingest_state(content: 'committed knowledge')
|
|
128
|
+
|
|
129
|
+
expect(state[:result]).to include(success: true, status: 'candidate')
|
|
130
|
+
expect(state).to include(entries: 1, expertise: 1, access_logs: 1)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it 'rolls back all persistence when any synchronous ingest side effect fails' do
|
|
134
|
+
entry_failure = ingest_state(content: 'entry failure')
|
|
135
|
+
expertise_failure = ingest_state(content: 'expertise failure', source_agent: 'fail-expertise')
|
|
136
|
+
audit_failure = ingest_state(content: 'audit failure', source_agent: 'fail-audit')
|
|
137
|
+
|
|
138
|
+
[entry_failure, expertise_failure, audit_failure].each do |state|
|
|
139
|
+
expect(state[:result]).to include(success: false)
|
|
140
|
+
expect(state).to include(entries: 0, expertise: 0, access_logs: 0)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it 'rejects missing required auxiliary models before creating the primary entry' do
|
|
145
|
+
missing_expertise = ingest_state(content: 'unavailable expertise', missing: :Expertise)
|
|
146
|
+
missing_access_log = ingest_state(content: 'unavailable access log', missing: :AccessLog)
|
|
147
|
+
|
|
148
|
+
[missing_expertise, missing_access_log].each do |state|
|
|
149
|
+
expect(state[:result]).to include(success: false, error: 'apollo_data_not_available')
|
|
150
|
+
expect(state).to include(entries: 0, expertise: 0, access_logs: 0)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
it 'retains process-owned ingestion for trusted direct calls without explicit producer identity' do
|
|
155
|
+
state = ingest_state(content: 'service-owned knowledge', process_identity: true, options: { access_scope: 'private' })
|
|
156
|
+
|
|
157
|
+
expect(state[:result]).to include(success: true)
|
|
158
|
+
expect(state[:records].first).to include(access_scope: 'private', identity_principal_id: 999,
|
|
159
|
+
identity_id: 998, identity_canonical_name: 'service-node')
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it 'persists the producer identity instead of the receiving service identity' do
|
|
163
|
+
state = ingest_state(content: 'private producer knowledge', process_identity: true,
|
|
164
|
+
options: { access_scope: 'private', identity_principal_id: 42, identity_id: 7,
|
|
165
|
+
identity_canonical_name: 'producer' })
|
|
166
|
+
|
|
167
|
+
expect(state[:result]).to include(success: true)
|
|
168
|
+
expect(state[:records].first).to include(access_scope: 'private', identity_principal_id: 42,
|
|
169
|
+
identity_id: 7, identity_canonical_name: 'producer')
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'does not substitute service identity when the caller explicitly has no identity' do
|
|
173
|
+
state = ingest_state(content: 'anonymous public knowledge', process_identity: true,
|
|
174
|
+
options: { identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil })
|
|
175
|
+
|
|
176
|
+
expect(state[:result]).to include(success: true)
|
|
177
|
+
expect(state[:records].first).to include(identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it 'rejects restricted ingestion without an owner before writing anything' do
|
|
181
|
+
state = ingest_state(content: 'unowned private knowledge',
|
|
182
|
+
options: { access_scope: 'private', identity_principal_id: nil })
|
|
183
|
+
|
|
184
|
+
expect(state[:result]).to include(success: false, error: 'identity_required')
|
|
185
|
+
expect(state).to include(entries: 0, expertise: 0, access_logs: 0)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it 'persists identical private hashes separately without touching the other owner' do
|
|
189
|
+
state = ingest_state(
|
|
190
|
+
content: 'identical private content',
|
|
191
|
+
existing: { content: 'identical private content', content_hash: 'shared-hash', status: 'candidate',
|
|
192
|
+
access_scope: 'private', identity_principal_id: 99, confidence: 0.5 },
|
|
193
|
+
options: { content_hash: 'shared-hash', access_scope: 'private', identity_principal_id: 42 }
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
expect(state[:result]).to include(success: true)
|
|
197
|
+
expect(state).to include(entries: 2, expertise: 1, access_logs: 1)
|
|
198
|
+
expect(state[:records].first).to eq(state[:before].first)
|
|
199
|
+
expect(state[:records].last).to include(id: state[:result][:entry_id], content: 'identical private content',
|
|
200
|
+
content_hash: 'shared-hash', identity_principal_id: 42, access_scope: 'private')
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it 'deduplicates within one owner without creating auxiliary rows or regenerating a valid vector' do
|
|
204
|
+
state = ingest_state(
|
|
205
|
+
content: 'same owner content',
|
|
206
|
+
existing: { id: 7, content: 'same owner content', content_hash: 'owned-hash', status: 'candidate',
|
|
207
|
+
access_scope: 'private', identity_principal_id: 42, confidence: 0.5 },
|
|
208
|
+
options: { content_hash: 'owned-hash', access_scope: 'private', identity_principal_id: 42 }
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
expect(state[:result]).to include(success: true, entry_id: 7, deduped: true)
|
|
212
|
+
expect(state).to include(entries: 1, expertise: 0, access_logs: 0, generated: [])
|
|
213
|
+
expect(state[:records].first[:confidence]).to be > 0.5
|
|
214
|
+
expect(state[:records].first.except(:confidence, :updated_at))
|
|
215
|
+
.to eq(state[:before].first.except(:confidence, :updated_at))
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
it 'regenerates legacy duplicates before success and leaves them unchanged if the provider fails' do
|
|
219
|
+
options = {
|
|
220
|
+
content: 'legacy duplicate',
|
|
221
|
+
legacy: true,
|
|
222
|
+
existing: { id: 7, content: 'legacy duplicate', content_hash: 'legacy-hash', status: 'candidate',
|
|
223
|
+
access_scope: 'private', identity_principal_id: 42, confidence: 0.5, embedding: '[0.6,0.8]' },
|
|
224
|
+
options: { content_hash: 'legacy-hash', access_scope: 'private', identity_principal_id: 42 }
|
|
225
|
+
}
|
|
226
|
+
repaired = ingest_state(**options)
|
|
227
|
+
failed = ingest_state(**options, generation_error: true)
|
|
228
|
+
|
|
229
|
+
expect(repaired[:result]).to include(success: true, entry_id: 7, deduped: true)
|
|
230
|
+
expect(repaired).to include(entries: 1, expertise: 0, access_logs: 0,
|
|
231
|
+
generated: [{ text: 'legacy duplicate', task: 'document' }])
|
|
232
|
+
expect(JSON.parse(repaired[:records].first[:embedding])).to eq([1.0, 0.0])
|
|
233
|
+
expect(JSON.parse(repaired[:records].first[:embedding_provenance], symbolize_names: true))
|
|
234
|
+
.to eq(ApolloSpecEmbedding.result[:embedding_provenance])
|
|
235
|
+
expect(failed[:result]).to include(success: false, error: 'embedding provider unavailable')
|
|
236
|
+
expect(failed[:records]).to eq(failed[:before])
|
|
237
|
+
expect(failed).to include(entries: 1, expertise: 0, access_logs: 0)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it 'writes nothing when embedding generation fails for new content' do
|
|
241
|
+
state = ingest_state(content: 'provider failure', generation_error: true)
|
|
242
|
+
|
|
243
|
+
expect(state[:result]).to include(success: false, error: 'embedding provider unavailable')
|
|
244
|
+
expect(state).to include(entries: 0, expertise: 0, access_logs: 0)
|
|
245
|
+
end
|
|
246
|
+
end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'open3'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
RSpec.describe 'Apollo knowledge access boundaries' do
|
|
9
|
+
let(:scope_script) do
|
|
10
|
+
<<~'RUBY'
|
|
11
|
+
require 'sequel'
|
|
12
|
+
require 'rspec'
|
|
13
|
+
require 'spec_helper'
|
|
14
|
+
require 'json'
|
|
15
|
+
db = Sequel.sqlite
|
|
16
|
+
db.create_table(:apollo_entries) do
|
|
17
|
+
primary_key :id
|
|
18
|
+
String :content
|
|
19
|
+
String :content_type
|
|
20
|
+
String :status
|
|
21
|
+
String :access_scope
|
|
22
|
+
Integer :identity_principal_id
|
|
23
|
+
Integer :identity_id
|
|
24
|
+
Float :confidence
|
|
25
|
+
String :tags
|
|
26
|
+
String :source_agent
|
|
27
|
+
String :knowledge_domain
|
|
28
|
+
String :source_context
|
|
29
|
+
String :embedding
|
|
30
|
+
String :embedding_space_id
|
|
31
|
+
Integer :embedding_dimensions
|
|
32
|
+
String :embedding_provenance
|
|
33
|
+
DateTime :created_at
|
|
34
|
+
DateTime :updated_at
|
|
35
|
+
end
|
|
36
|
+
db.create_table(:identities) do
|
|
37
|
+
Integer :id
|
|
38
|
+
Integer :principal_id
|
|
39
|
+
end
|
|
40
|
+
db.create_table(:identity_group_memberships) do
|
|
41
|
+
Integer :principal_id
|
|
42
|
+
Integer :group_id
|
|
43
|
+
end
|
|
44
|
+
Legion::Data.const_set(:Model, Module.new) unless defined?(Legion::Data::Model)
|
|
45
|
+
Legion::Data::Model.const_set(:Apollo, Module.new)
|
|
46
|
+
Legion::Data::Model::Apollo.const_set(:Entry, Class.new(Sequel::Model(db[:apollo_entries])))
|
|
47
|
+
Legion::Data.define_singleton_method(:connection) { db }
|
|
48
|
+
base = { content_type: 'fact', status: 'confirmed', confidence: 0.8, knowledge_domain: 'general', created_at: Time.now }
|
|
49
|
+
rows = [
|
|
50
|
+
{ id: 1, content: 'public evidence', access_scope: 'global', source_agent: 'human' },
|
|
51
|
+
{ id: 2, content: 'owner evidence', access_scope: 'private', identity_principal_id: 42 },
|
|
52
|
+
{ id: 3, content: 'other owner', access_scope: 'private', identity_principal_id: 99 },
|
|
53
|
+
{ id: 4, content: 'shared team', access_scope: 'team', identity_principal_id: 77 },
|
|
54
|
+
{ id: 5, content: 'unrelated team', access_scope: 'team', identity_principal_id: 88 },
|
|
55
|
+
{ id: 6, content: 'identity alias', access_scope: 'private', identity_id: 7 },
|
|
56
|
+
{ id: 7, content: 'archived evidence', access_scope: 'global', status: 'archived' },
|
|
57
|
+
{ id: 8, content: 'weak evidence', access_scope: 'global', confidence: 0.2 },
|
|
58
|
+
{ id: 9, content: 'excluded producer', access_scope: 'global', source_agent: 'blocked-agent' }
|
|
59
|
+
]
|
|
60
|
+
embedding = ApolloSpecEmbedding.result
|
|
61
|
+
base.merge!(embedding: ::JSON.generate(embedding[:vector]), embedding_dimensions: embedding[:dimensions],
|
|
62
|
+
embedding_space_id: embedding[:embedding_provenance][:space_id],
|
|
63
|
+
embedding_provenance: ::JSON.generate(embedding[:embedding_provenance]))
|
|
64
|
+
rows.each { |row| db[:apollo_entries].insert(base.merge(row)) }
|
|
65
|
+
db[:identities].insert(id: 7, principal_id: 42)
|
|
66
|
+
[{ principal_id: 42, group_id: 5 }, { principal_id: 77, group_id: 5 }, { principal_id: 88, group_id: 6 }].each do |row|
|
|
67
|
+
db[:identity_group_memberships].insert(row)
|
|
68
|
+
end
|
|
69
|
+
runner = Legion::Extensions::Apollo::Runners::Knowledge
|
|
70
|
+
input = ::JSON.parse($stdin.read, symbolize_names: true)
|
|
71
|
+
result = case input[:action]
|
|
72
|
+
when 'browse'
|
|
73
|
+
runner.handle_query(query: '', requesting_principal_id: input[:principal], min_confidence: 0.7,
|
|
74
|
+
exclude_source_agents: ['blocked-agent'])
|
|
75
|
+
when 'deprecate'
|
|
76
|
+
runner.deprecate_entry(entry_id: 2, reason: 'retired', requesting_principal_id: input[:principal])
|
|
77
|
+
when 'export'
|
|
78
|
+
runner.prepare_mesh_export(target_domain: 'general')
|
|
79
|
+
end
|
|
80
|
+
state = { result: result, private_status: db[:apollo_entries][id: 2][:status] }
|
|
81
|
+
puts "APOLLO_RESULT=#{::JSON.generate(state)}"
|
|
82
|
+
RUBY
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def scoped_result(action:, principal: nil, **)
|
|
86
|
+
stdout, stderr, status = Open3.capture3(
|
|
87
|
+
RbConfig.ruby, '-Ilib', '-Ispec', '-e', scope_script,
|
|
88
|
+
chdir: File.expand_path('../../../../..', __dir__), stdin_data: JSON.generate(action: action, principal: principal)
|
|
89
|
+
)
|
|
90
|
+
line = stdout.lines.find { |output| output.start_with?('APOLLO_RESULT=') }
|
|
91
|
+
raise "Scoped knowledge process failed: #{stderr}\n#{stdout}" unless status.success? && line
|
|
92
|
+
|
|
93
|
+
JSON.parse(line.delete_prefix('APOLLO_RESULT='), symbolize_names: true)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it 'keeps anonymous browse global-only while applying confidence and source exclusions' do
|
|
97
|
+
result = scoped_result(action: 'browse', principal: 0)[:result]
|
|
98
|
+
|
|
99
|
+
expect(result[:success]).to be true
|
|
100
|
+
expect(result[:entries].map { |entry| entry[:id] }).to eq([1])
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it 'includes owned private evidence and shared teams without admitting unrelated owners' do
|
|
104
|
+
result = scoped_result(action: 'browse', principal: 42)[:result]
|
|
105
|
+
|
|
106
|
+
expect(result[:success]).to be true
|
|
107
|
+
expect(result[:entries].map { |entry| entry[:id] }).to contain_exactly(1, 2, 4, 6)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'does not disclose or mutate a private entry when deprecation is requested by another principal' do
|
|
111
|
+
state = scoped_result(action: 'deprecate', principal: 99)
|
|
112
|
+
|
|
113
|
+
expect(state[:result]).to include(success: false, error: 'entry_not_found')
|
|
114
|
+
expect(state[:private_status]).to eq('confirmed')
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it 'allows the owner to deprecate its private entry' do
|
|
118
|
+
state = scoped_result(action: 'deprecate', principal: 42)
|
|
119
|
+
|
|
120
|
+
expect(state[:result]).to include(success: true, status: 'archived')
|
|
121
|
+
expect(state[:private_status]).to eq('archived')
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'exports only global confirmed evidence, even without a requesting principal' do
|
|
125
|
+
result = scoped_result(action: 'export')[:result]
|
|
126
|
+
|
|
127
|
+
expect(result[:success]).to be true
|
|
128
|
+
expect(result[:entries].map { |entry| entry[:id] }).to contain_exactly(1, 9)
|
|
129
|
+
end
|
|
130
|
+
end
|