lex-apollo 0.4.44 → 0.4.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +72 -14
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -41
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +4 -16
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +96 -41
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -51
- data/lib/legion/extensions/apollo/helpers/confidence.rb +8 -52
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +99 -43
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +64 -102
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +87 -136
- data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
- data/lib/legion/extensions/apollo/runners/maintenance.rb +151 -64
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -46
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +240 -8
- data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +32 -3
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +23 -7
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -77
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +138 -6
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +11 -2
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +33 -65
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +40 -14
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -39
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -1,55 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/extensions/actors/subscription'
|
|
4
|
+
require 'legion/extensions/apollo/runners/writeback'
|
|
5
|
+
require 'legion/extensions/apollo/transport/subscription_delivery'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
7
9
|
module Apollo
|
|
8
10
|
module Actor
|
|
9
11
|
class WritebackVectorize < Legion::Extensions::Actors::Subscription
|
|
10
|
-
|
|
12
|
+
include Legion::Extensions::Apollo::Helpers::Capability
|
|
13
|
+
include Legion::Extensions::Apollo::Transport::SubscriptionDelivery
|
|
14
|
+
|
|
15
|
+
def runner_class(**) = Legion::Extensions::Apollo::Runners::Writeback
|
|
11
16
|
def runner_function = 'handle_vectorize'
|
|
17
|
+
def use_runner? = false
|
|
12
18
|
def check_subtask? = false
|
|
13
19
|
def generate_task? = false
|
|
14
20
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
log.debug("WritebackVectorize handle_vectorize content_length=#{payload[:content].to_s.length} content_type=#{payload[:content_type] || 'nil'}")
|
|
18
|
-
result = Legion::LLM::Call::Embeddings.generate(text: payload[:content])
|
|
19
|
-
vector = result.is_a?(Hash) ? result[:vector] : result
|
|
20
|
-
embedding = vector.is_a?(Array) && vector.any? ? vector : Array.new(1024, 0.0)
|
|
21
|
-
log.debug("WritebackVectorize embedding_dimensions=#{embedding.length} vector_generated=#{vector.is_a?(Array) && vector.any?}")
|
|
22
|
-
enriched = payload.merge(embedding: embedding)
|
|
23
|
-
|
|
24
|
-
if Helpers::Capability.can_write?
|
|
25
|
-
log.debug('WritebackVectorize route=direct_ingest')
|
|
26
|
-
Runners::Knowledge.handle_ingest(**enriched)
|
|
27
|
-
else
|
|
28
|
-
log.debug('WritebackVectorize route=transport_writeback')
|
|
29
|
-
Transport::Messages::Writeback.new(
|
|
30
|
-
**enriched, has_embedding: true
|
|
31
|
-
).publish
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
log.info('WritebackVectorize completed action=vectorized')
|
|
35
|
-
{ success: true, action: :vectorized }
|
|
36
|
-
rescue StandardError => e
|
|
37
|
-
handle_exception(e, level: :error, operation: 'apollo.writeback_vectorize.handle_vectorize')
|
|
38
|
-
{ success: false, error: e.message }
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def enabled? # rubocop:disable Legion/Extension/ActorEnabledSideEffects
|
|
42
|
-
Legion.const_defined?(:Transport, false) && Helpers::Capability.can_embed?
|
|
21
|
+
def enabled?(**) # rubocop:disable Legion/Extension/ActorEnabledSideEffects
|
|
22
|
+
capability_can_embed?
|
|
43
23
|
rescue StandardError => e
|
|
44
24
|
handle_exception(e, level: :warn, operation: 'apollo.writeback_vectorize.enabled')
|
|
45
25
|
false
|
|
46
26
|
end
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
def symbolize(hash)
|
|
51
|
-
hash.transform_keys(&:to_sym)
|
|
52
|
-
end
|
|
53
27
|
end
|
|
54
28
|
end
|
|
55
29
|
end
|
|
@@ -10,12 +10,14 @@ module Legion
|
|
|
10
10
|
module Apollo
|
|
11
11
|
class Api < Sinatra::Base
|
|
12
12
|
set :host_authorization, permitted: :any
|
|
13
|
+
include Legion::Extensions::Apollo::Helpers::DataModels
|
|
14
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
13
15
|
|
|
14
16
|
class << self
|
|
15
17
|
def stats_payload(now: Time.now)
|
|
16
|
-
return { error: 'apollo_data_not_available' }
|
|
18
|
+
return { error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
17
19
|
|
|
18
|
-
entries =
|
|
20
|
+
entries = data_model_entry
|
|
19
21
|
by_status = grouped_counts(entries, :status)
|
|
20
22
|
by_status['active'] = entries.exclude(status: 'archived').count
|
|
21
23
|
|
|
@@ -26,7 +28,7 @@ module Legion
|
|
|
26
28
|
by_status: by_status,
|
|
27
29
|
by_content_type: grouped_counts(entries, :content_type)
|
|
28
30
|
}
|
|
29
|
-
stats[:total_relations] =
|
|
31
|
+
stats[:total_relations] = data_model_relation.count unless data_model_relation.nil?
|
|
30
32
|
stats
|
|
31
33
|
end
|
|
32
34
|
|
|
@@ -60,26 +62,56 @@ module Legion
|
|
|
60
62
|
halt 400, json_dump({ error: "invalid JSON: #{e.message}" })
|
|
61
63
|
end
|
|
62
64
|
|
|
65
|
+
def apollo_request_identity(resolve_identity_id: false, **)
|
|
66
|
+
identity = { identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }
|
|
67
|
+
claims = env['legion.auth']
|
|
68
|
+
caller = env['legion.principal']
|
|
69
|
+
return identity unless claims.is_a?(Hash) && !claims.empty? && caller
|
|
70
|
+
return identity if caller.canonical_name.to_s.empty?
|
|
71
|
+
|
|
72
|
+
return identity unless defined?(Legion::Data::Model::Identity::Principal)
|
|
73
|
+
|
|
74
|
+
principal = Legion::Data::Model::Identity::Principal[
|
|
75
|
+
canonical_name: caller.canonical_name, kind: caller.kind.to_s, active: true
|
|
76
|
+
]
|
|
77
|
+
return identity unless principal && principal[:id].is_a?(Integer) && principal[:id].positive?
|
|
78
|
+
|
|
79
|
+
identity[:identity_principal_id] = principal[:id]
|
|
80
|
+
identity[:identity_canonical_name] = caller.canonical_name
|
|
81
|
+
identity[:identity_id] = apollo_request_identity_id(caller: caller, principal_id: principal[:id]) if resolve_identity_id
|
|
82
|
+
identity
|
|
83
|
+
rescue Sequel::Error => e
|
|
84
|
+
handle_exception(e, level: :warn, operation: 'apollo.api.request_identity')
|
|
85
|
+
{ identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def apollo_request_identity_id(caller:, principal_id:, **)
|
|
89
|
+
return unless defined?(Legion::Data::Model::Identity::Provider) && defined?(Legion::Data::Model::Identity::Identity)
|
|
90
|
+
return if caller.source.nil? || caller.principal_id.nil?
|
|
91
|
+
|
|
92
|
+
provider = Legion::Data::Model::Identity::Provider[name: caller.source.to_s]
|
|
93
|
+
return unless provider
|
|
94
|
+
|
|
95
|
+
identity = Legion::Data::Model::Identity::Identity[
|
|
96
|
+
principal_id: principal_id, provider_id: provider[:id], provider_identity_key: caller.principal_id.to_s, active: true
|
|
97
|
+
]
|
|
98
|
+
identity && identity[:id]
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
helpers do
|
|
63
103
|
def runner
|
|
64
|
-
|
|
65
|
-
obj = Object.new
|
|
66
|
-
obj.extend(Runners::Knowledge)
|
|
67
|
-
obj
|
|
68
|
-
end
|
|
104
|
+
Legion::Extensions::Apollo::Runners::Knowledge
|
|
69
105
|
end
|
|
70
106
|
|
|
71
107
|
def expertise_runner
|
|
72
|
-
|
|
73
|
-
obj = Object.new
|
|
74
|
-
obj.extend(Runners::Expertise)
|
|
75
|
-
obj
|
|
76
|
-
end
|
|
108
|
+
Legion::Extensions::Apollo::Runners::Expertise
|
|
77
109
|
end
|
|
78
110
|
end
|
|
79
111
|
|
|
80
112
|
# Health check
|
|
81
113
|
get '/api/apollo/health' do
|
|
82
|
-
available =
|
|
114
|
+
available = !data_model_entry.nil?
|
|
83
115
|
json_dump({ status: available ? 'ok' : 'degraded', data_available: available })
|
|
84
116
|
end
|
|
85
117
|
|
|
@@ -89,12 +121,15 @@ module Legion
|
|
|
89
121
|
halt 400, json_dump({ error: 'query is required' }) unless req[:query]
|
|
90
122
|
|
|
91
123
|
query_options = {
|
|
92
|
-
query:
|
|
93
|
-
limit:
|
|
94
|
-
min_confidence:
|
|
95
|
-
tags:
|
|
96
|
-
|
|
97
|
-
|
|
124
|
+
query: req[:query],
|
|
125
|
+
limit: req[:limit] || 10,
|
|
126
|
+
min_confidence: req[:min_confidence] || 0.3,
|
|
127
|
+
tags: req[:tags],
|
|
128
|
+
tag_match: req[:tag_match] || :any,
|
|
129
|
+
exclude_source_agents: req[:exclude_source_agents] || [],
|
|
130
|
+
domain: req[:domain],
|
|
131
|
+
agent_id: req[:agent_id] || 'api',
|
|
132
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
98
133
|
}
|
|
99
134
|
query_options[:status] = req[:status] if req.key?(:status)
|
|
100
135
|
|
|
@@ -108,16 +143,28 @@ module Legion
|
|
|
108
143
|
req = json_body
|
|
109
144
|
halt 400, json_dump({ error: 'content is required' }) unless req[:content]
|
|
110
145
|
halt 400, json_dump({ error: 'content_type is required' }) unless req[:content_type]
|
|
146
|
+
scope = req.fetch(:access_scope, 'global')
|
|
147
|
+
halt 400, json_dump({ error: 'invalid access_scope' }) unless %w[global private team].include?(scope)
|
|
148
|
+
identity = apollo_request_identity(resolve_identity_id: true)
|
|
149
|
+
if scope != 'global' && identity[:identity_principal_id].nil?
|
|
150
|
+
halt 403, json_dump({ error: 'authenticated principal is required for restricted writes' })
|
|
151
|
+
end
|
|
111
152
|
|
|
112
153
|
result = runner.handle_ingest(
|
|
113
|
-
content:
|
|
114
|
-
content_type:
|
|
115
|
-
tags:
|
|
116
|
-
source_agent:
|
|
117
|
-
source_provider:
|
|
118
|
-
source_channel:
|
|
119
|
-
knowledge_domain:
|
|
120
|
-
|
|
154
|
+
content: req[:content],
|
|
155
|
+
content_type: req[:content_type],
|
|
156
|
+
tags: req[:tags] || [],
|
|
157
|
+
source_agent: req[:source_agent] || 'api',
|
|
158
|
+
source_provider: req[:source_provider],
|
|
159
|
+
source_channel: req[:source_channel],
|
|
160
|
+
knowledge_domain: req[:knowledge_domain],
|
|
161
|
+
submitted_by: req[:submitted_by],
|
|
162
|
+
submitted_from: req[:submitted_from],
|
|
163
|
+
access_scope: scope,
|
|
164
|
+
context: req[:context] || {},
|
|
165
|
+
embedding: req[:embedding],
|
|
166
|
+
embedding_provenance: req[:embedding_provenance],
|
|
167
|
+
**identity
|
|
121
168
|
)
|
|
122
169
|
status result[:success] ? 201 : 500
|
|
123
170
|
json_dump(result)
|
|
@@ -129,10 +176,11 @@ module Legion
|
|
|
129
176
|
halt 400, json_dump({ error: 'entry_id is required' }) unless req[:entry_id]
|
|
130
177
|
|
|
131
178
|
result = runner.handle_traverse(
|
|
132
|
-
entry_id:
|
|
133
|
-
depth:
|
|
134
|
-
relation_types:
|
|
135
|
-
agent_id:
|
|
179
|
+
entry_id: req[:entry_id],
|
|
180
|
+
depth: req[:depth] || 2,
|
|
181
|
+
relation_types: req[:relation_types],
|
|
182
|
+
agent_id: req[:agent_id] || 'api',
|
|
183
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
136
184
|
)
|
|
137
185
|
status result[:success] ? 200 : 500
|
|
138
186
|
json_dump(result)
|
|
@@ -143,13 +191,19 @@ module Legion
|
|
|
143
191
|
req = json_body
|
|
144
192
|
halt 400, json_dump({ error: 'query is required' }) unless req[:query]
|
|
145
193
|
|
|
146
|
-
|
|
147
|
-
query:
|
|
148
|
-
limit:
|
|
149
|
-
min_confidence:
|
|
150
|
-
tags:
|
|
151
|
-
|
|
152
|
-
|
|
194
|
+
retrieve_options = {
|
|
195
|
+
query: req[:query],
|
|
196
|
+
limit: req[:limit] || 5,
|
|
197
|
+
min_confidence: req[:min_confidence] || 0.3,
|
|
198
|
+
tags: req[:tags],
|
|
199
|
+
tag_match: req[:tag_match] || :any,
|
|
200
|
+
exclude_source_agents: req[:exclude_source_agents] || [],
|
|
201
|
+
domain: req[:domain],
|
|
202
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
203
|
+
}
|
|
204
|
+
retrieve_options[:status] = req[:status] if req.key?(:status)
|
|
205
|
+
|
|
206
|
+
result = runner.retrieve_relevant(**retrieve_options)
|
|
153
207
|
status result[:success] ? 200 : 500
|
|
154
208
|
json_dump(result)
|
|
155
209
|
end
|
|
@@ -157,8 +211,9 @@ module Legion
|
|
|
157
211
|
# Deprecate entry
|
|
158
212
|
post '/api/apollo/entries/:id/deprecate' do
|
|
159
213
|
result = runner.deprecate_entry(
|
|
160
|
-
entry_id:
|
|
161
|
-
reason:
|
|
214
|
+
entry_id: params[:id],
|
|
215
|
+
reason: json_body[:reason] || 'deprecated via API',
|
|
216
|
+
requesting_principal_id: apollo_request_identity[:identity_principal_id] || 0
|
|
162
217
|
)
|
|
163
218
|
halt 503, json_dump({ success: false, error: 'apollo_data_not_available' }) if result[:action] == :deprecate
|
|
164
219
|
|
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
require 'legion/extensions/apollo/helpers/confidence'
|
|
4
|
+
require 'legion/extensions/apollo/helpers/similarity'
|
|
5
|
+
require 'legion/extensions/apollo/helpers/graph_query'
|
|
6
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
7
|
+
require 'legion/extensions/apollo/runners/expertise'
|
|
8
|
+
require 'legion/extensions/apollo/runners/maintenance'
|
|
9
9
|
|
|
10
10
|
module Legion
|
|
11
11
|
module Extensions
|
|
12
12
|
module Apollo
|
|
13
13
|
class Client
|
|
14
|
-
include
|
|
15
|
-
include
|
|
16
|
-
include
|
|
14
|
+
include Legion::Extensions::Apollo::Helpers::DataModels
|
|
15
|
+
include Legion::Extensions::Apollo::Helpers::Confidence
|
|
16
|
+
include Legion::Extensions::Apollo::Helpers::TagNormalizer
|
|
17
|
+
include Legion::Extensions::Apollo::Helpers::Storage
|
|
18
|
+
include Legion::Extensions::Apollo::Helpers::Embedding
|
|
19
|
+
include Legion::Extensions::Apollo::Helpers::Similarity
|
|
20
|
+
include Legion::Extensions::Apollo::Helpers::GraphQuery
|
|
21
|
+
include Legion::Extensions::Apollo::Runners::Knowledge
|
|
22
|
+
include Legion::Extensions::Apollo::Runners::Expertise
|
|
23
|
+
include Legion::Extensions::Apollo::Runners::Maintenance
|
|
17
24
|
|
|
18
25
|
attr_reader :agent_id
|
|
19
26
|
|
|
@@ -21,6 +28,10 @@ module Legion
|
|
|
21
28
|
@agent_id = agent_id
|
|
22
29
|
end
|
|
23
30
|
|
|
31
|
+
def segments
|
|
32
|
+
[:apollo]
|
|
33
|
+
end
|
|
34
|
+
|
|
24
35
|
def store_knowledge(source_agent: nil, **)
|
|
25
36
|
super(**, source_agent: source_agent || @agent_id)
|
|
26
37
|
end
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'legion/extensions/apollo/helpers/data_models'
|
|
4
4
|
|
|
5
5
|
module Legion
|
|
6
6
|
module Extensions
|
|
7
7
|
module Apollo
|
|
8
8
|
module GaiaIntegration
|
|
9
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
10
|
+
|
|
9
11
|
PUBLISH_CONFIDENCE_THRESHOLD = 0.6
|
|
10
12
|
PUBLISH_NOVELTY_THRESHOLD = 0.3
|
|
11
13
|
|
|
@@ -30,13 +32,13 @@ module Legion
|
|
|
30
32
|
end
|
|
31
33
|
|
|
32
34
|
def handle_mesh_departure(agent_id:)
|
|
33
|
-
return nil
|
|
35
|
+
return nil if data_model_expertise.nil?
|
|
34
36
|
|
|
35
|
-
sole_expert_domains =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
sole_expert_domains = data_model_expertise
|
|
38
|
+
.where(agent_id: agent_id)
|
|
39
|
+
.all
|
|
40
|
+
.select { |e| sole_expert?(e.domain, agent_id) }
|
|
41
|
+
.map(&:domain)
|
|
40
42
|
|
|
41
43
|
return nil if sole_expert_domains.empty?
|
|
42
44
|
|
|
@@ -51,12 +53,12 @@ module Legion
|
|
|
51
53
|
private
|
|
52
54
|
|
|
53
55
|
def sole_expert?(domain, agent_id)
|
|
54
|
-
return false
|
|
56
|
+
return false if data_model_expertise.nil?
|
|
55
57
|
|
|
56
|
-
count =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
count = data_model_expertise
|
|
59
|
+
.where(domain: domain)
|
|
60
|
+
.exclude(agent_id: agent_id)
|
|
61
|
+
.count
|
|
60
62
|
count.zero?
|
|
61
63
|
end
|
|
62
64
|
end
|
|
@@ -1,65 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'legion/data'
|
|
4
|
+
require 'legion/llm'
|
|
5
|
+
require 'legion/settings/helper'
|
|
6
|
+
|
|
3
7
|
module Legion
|
|
4
8
|
module Extensions
|
|
5
9
|
module Apollo
|
|
6
10
|
module Helpers
|
|
7
11
|
module Capability
|
|
8
|
-
|
|
9
|
-
extend Legion::Settings::Helper
|
|
10
|
-
|
|
11
|
-
PRIVILEGE_MUTEX = Mutex.new
|
|
12
|
-
|
|
13
|
-
module_function
|
|
14
|
-
|
|
15
|
-
# SSOT gate: the sole selection authority for embeddings is the
|
|
16
|
-
# router (Call::Embeddings.generate with no provider arg →
|
|
17
|
-
# Router.next_lane). can_embed? asks the one capability fact that
|
|
18
|
-
# matches that selection — is there an embedding-type lane the
|
|
19
|
-
# router can select? — against the same registry lanes the router
|
|
20
|
-
# reads. Provider-agnostic (no ollama pin, no hardcoded model
|
|
21
|
-
# list); no parallel settings-based "second domain".
|
|
22
|
-
def can_embed?
|
|
23
|
-
return false unless defined?(Legion::LLM) && Legion::LLM.started?
|
|
24
|
-
|
|
25
|
-
Legion::LLM.can_embed?
|
|
26
|
-
rescue StandardError => e
|
|
27
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.can_embed')
|
|
28
|
-
false
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def can_write?
|
|
32
|
-
return false unless apollo_write_enabled?
|
|
33
|
-
return false unless defined?(Legion::Data) && Legion::Data.connected?
|
|
34
|
-
|
|
35
|
-
check_db_write_privilege
|
|
36
|
-
rescue StandardError => e
|
|
37
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.can_write')
|
|
38
|
-
false
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def apollo_write_enabled?
|
|
42
|
-
settings[:data][:apollo_write] == true
|
|
43
|
-
rescue StandardError => e
|
|
44
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.apollo_write_enabled')
|
|
45
|
-
false
|
|
46
|
-
end
|
|
12
|
+
include Legion::Settings::Helper
|
|
47
13
|
|
|
48
|
-
|
|
49
|
-
PRIVILEGE_MUTEX.synchronize do
|
|
50
|
-
return @apollo_write_privilege unless @apollo_write_privilege.nil?
|
|
14
|
+
private
|
|
51
15
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.first[:can_insert] == true
|
|
55
|
-
end
|
|
56
|
-
rescue StandardError => e
|
|
57
|
-
handle_exception(e, level: :warn, operation: 'apollo.capability.check_db_write_privilege')
|
|
58
|
-
@apollo_write_privilege = false
|
|
16
|
+
def capability_can_embed?
|
|
17
|
+
Legion::LLM.started? && Legion::LLM.can_embed?
|
|
59
18
|
end
|
|
60
19
|
|
|
61
|
-
def
|
|
62
|
-
|
|
20
|
+
def capability_can_write?
|
|
21
|
+
settings[:data][:apollo_write] == true && Legion::Data.can_write?(:apollo_entries)
|
|
63
22
|
end
|
|
64
23
|
end
|
|
65
24
|
end
|
|
@@ -1,69 +1,25 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'legion/settings/helper'
|
|
4
|
+
|
|
3
5
|
module Legion
|
|
4
6
|
module Extensions
|
|
5
7
|
module Apollo
|
|
6
8
|
module Helpers
|
|
7
9
|
module Confidence
|
|
8
|
-
|
|
9
|
-
extend Legion::Settings::Helper
|
|
10
|
+
include Legion::Settings::Helper
|
|
10
11
|
|
|
11
|
-
INITIAL_CONFIDENCE = 0.5
|
|
12
|
-
CORROBORATION_BOOST = 0.3
|
|
13
|
-
RETRIEVAL_BOOST = 0.02
|
|
14
|
-
POWER_LAW_ALPHA = 0.05
|
|
15
|
-
DECAY_THRESHOLD = 0.05
|
|
16
|
-
CORROBORATION_SIMILARITY_THRESHOLD = 0.9
|
|
17
|
-
WRITE_CONFIDENCE_GATE = 0.6
|
|
18
|
-
WRITE_NOVELTY_GATE = 0.3
|
|
19
|
-
STALE_DAYS = 90
|
|
20
|
-
DECAY_MIN_AGE_HOURS = 168
|
|
21
12
|
CONTENT_TYPES = %i[fact concept procedure association observation].freeze
|
|
22
|
-
STATUSES = %w[candidate confirmed disputed decayed archived].freeze
|
|
23
13
|
RELATION_TYPES = %w[is_a has_a part_of causes similar_to contradicts supersedes depends_on supports_by extends].freeze
|
|
24
14
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def initial_confidence = settings[:confidence][:initial]
|
|
28
|
-
def corroboration_boost = settings[:confidence][:corroboration_boost]
|
|
29
|
-
def retrieval_boost = settings[:confidence][:retrieval_boost]
|
|
30
|
-
def power_law_alpha = settings[:power_law_alpha]
|
|
31
|
-
def decay_threshold = settings[:decay_threshold]
|
|
32
|
-
def write_confidence_gate = settings[:confidence][:write_gate]
|
|
33
|
-
def write_novelty_gate = settings[:confidence][:novelty_gate]
|
|
34
|
-
def stale_days = settings[:stale_days]
|
|
35
|
-
def decay_min_age_hours = settings[:decay_min_age_hours]
|
|
36
|
-
|
|
37
|
-
def corroboration_similarity_threshold
|
|
38
|
-
settings[:confidence][:corroboration_similarity]
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def apply_decay(confidence:, age_hours: nil, alpha: power_law_alpha, **)
|
|
42
|
-
return confidence if age_hours && age_hours < decay_min_age_hours
|
|
43
|
-
|
|
44
|
-
if age_hours
|
|
45
|
-
age_days = age_hours / 24.0
|
|
46
|
-
[confidence * ((age_days.clamp(1, Float::INFINITY) + 1.0)**(-alpha)) / (age_days.clamp(1, Float::INFINITY)**(-alpha)), 0.0].max
|
|
47
|
-
else
|
|
48
|
-
factor = 1.0 / (1.0 + alpha)
|
|
49
|
-
[confidence * factor, 0.0].max
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def apply_retrieval_boost(confidence:, **)
|
|
54
|
-
[confidence + retrieval_boost, 1.0].min
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def apply_corroboration_boost(confidence:, weight: 1.0, **)
|
|
58
|
-
[confidence + (corroboration_boost * weight), 1.0].min
|
|
59
|
-
end
|
|
15
|
+
private
|
|
60
16
|
|
|
61
|
-
def
|
|
62
|
-
confidence
|
|
17
|
+
def confidence_apply_retrieval_boost(confidence:, **)
|
|
18
|
+
[confidence + settings[:confidence][:retrieval_boost], 1.0].min
|
|
63
19
|
end
|
|
64
20
|
|
|
65
|
-
def
|
|
66
|
-
confidence
|
|
21
|
+
def confidence_apply_corroboration_boost(confidence:, weight: 1.0, **)
|
|
22
|
+
[confidence + (settings[:confidence][:corroboration_boost] * weight), 1.0].min
|
|
67
23
|
end
|
|
68
24
|
end
|
|
69
25
|
end
|
|
@@ -1,58 +1,42 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'legion/data'
|
|
4
|
+
|
|
3
5
|
module Legion
|
|
4
6
|
module Extensions
|
|
5
7
|
module Apollo
|
|
6
8
|
module Helpers
|
|
7
9
|
module DataModels
|
|
8
|
-
|
|
9
|
-
def apollo_entry
|
|
10
|
-
namespaced_apollo_model(:Entry) || legacy_model(:ApolloEntry)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def apollo_relation
|
|
14
|
-
namespaced_apollo_model(:Relation) || legacy_model(:ApolloRelation)
|
|
15
|
-
end
|
|
10
|
+
private
|
|
16
11
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
end
|
|
12
|
+
def data_model_entry
|
|
13
|
+
return unless defined?(Legion::Data::Model::Apollo::Entry)
|
|
20
14
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def apollo_entry_available?
|
|
26
|
-
!apollo_entry.nil?
|
|
27
|
-
end
|
|
15
|
+
Legion::Data::Model::Apollo::Entry
|
|
16
|
+
end
|
|
28
17
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
18
|
+
def data_model_relation
|
|
19
|
+
return unless defined?(Legion::Data::Model::Apollo::Relation)
|
|
32
20
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
end
|
|
21
|
+
Legion::Data::Model::Apollo::Relation
|
|
22
|
+
end
|
|
36
23
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
24
|
+
def data_model_access_log
|
|
25
|
+
return unless defined?(Legion::Data::Model::Apollo::AccessLog)
|
|
40
26
|
|
|
41
|
-
|
|
27
|
+
Legion::Data::Model::Apollo::AccessLog
|
|
28
|
+
end
|
|
42
29
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return nil unless Legion::Data::Model::Apollo.const_defined?(name, false)
|
|
30
|
+
def data_model_expertise
|
|
31
|
+
return unless defined?(Legion::Data::Model::Apollo::Expertise)
|
|
46
32
|
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
Legion::Data::Model::Apollo::Expertise
|
|
34
|
+
end
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return nil unless Legion::Data::Model.const_defined?(name, false)
|
|
36
|
+
def data_model_task_log
|
|
37
|
+
return unless defined?(Legion::Data::Model::TaskLog)
|
|
53
38
|
|
|
54
|
-
|
|
55
|
-
end
|
|
39
|
+
Legion::Data::Model::TaskLog
|
|
56
40
|
end
|
|
57
41
|
end
|
|
58
42
|
end
|