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
|
@@ -2,19 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
require 'legion/json'
|
|
4
4
|
require 'legion/settings'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
require 'legion/logging/helper'
|
|
6
|
+
require 'legion/extensions/apollo/helpers/confidence'
|
|
7
|
+
require 'legion/extensions/apollo/helpers/data_models'
|
|
8
|
+
require 'legion/extensions/apollo/helpers/embedding'
|
|
9
|
+
require 'legion/extensions/apollo/helpers/storage'
|
|
10
|
+
require 'legion/extensions/apollo/helpers/tag_normalizer'
|
|
11
|
+
require 'legion/extensions/apollo/runners/contradictions'
|
|
12
|
+
require 'legion/extensions/apollo/runners/embedding'
|
|
13
|
+
require 'legion/extensions/apollo/runners/search'
|
|
8
14
|
module Legion
|
|
9
15
|
module Extensions
|
|
10
16
|
module Apollo
|
|
11
17
|
module Runners
|
|
12
18
|
module Knowledge
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
include Legion::Logging::Helper
|
|
20
|
+
include Legion::JSON::Helper
|
|
21
|
+
include Legion::Settings::Helper
|
|
22
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
23
|
+
extend Legion::Extensions::Apollo::Helpers::Confidence
|
|
24
|
+
extend Legion::Extensions::Apollo::Helpers::TagNormalizer
|
|
25
|
+
extend Legion::Extensions::Apollo::Helpers::Storage
|
|
26
|
+
extend Legion::Extensions::Apollo::Helpers::Embedding
|
|
27
|
+
extend Legion::Extensions::Apollo::Helpers::Similarity
|
|
28
|
+
extend Legion::Extensions::Apollo::Helpers::GraphQuery
|
|
29
|
+
extend self
|
|
18
30
|
|
|
19
31
|
CONTENT_TYPE_ALIASES = {
|
|
20
32
|
reasoning: :concept, analysis: :concept, explanation: :concept,
|
|
@@ -27,10 +39,10 @@ module Legion
|
|
|
27
39
|
UNSET = Object.new.freeze
|
|
28
40
|
|
|
29
41
|
def store_knowledge(content:, content_type:, tags: [], source_agent: nil, context: {}, **)
|
|
30
|
-
content_type =
|
|
31
|
-
log.debug("Apollo Knowledge.store_knowledge content_type=#{content_type} tags=#{Array(tags).size} source_agent=#{source_agent || 'nil'} data_available=#{
|
|
42
|
+
content_type = knowledge_normalize_content_type(content_type)
|
|
43
|
+
log.debug("Apollo Knowledge.store_knowledge content_type=#{content_type} tags=#{Array(tags).size} source_agent=#{source_agent || 'nil'} data_available=#{!data_model_entry.nil?}") # rubocop:disable Layout/LineLength
|
|
32
44
|
|
|
33
|
-
|
|
45
|
+
unless data_model_entry.nil?
|
|
34
46
|
return handle_ingest(content: content, content_type: content_type,
|
|
35
47
|
tags: Array(tags), source_agent: source_agent, context: context, **)
|
|
36
48
|
end
|
|
@@ -45,9 +57,9 @@ module Legion
|
|
|
45
57
|
}
|
|
46
58
|
end
|
|
47
59
|
|
|
48
|
-
def query_knowledge(query:, limit:
|
|
49
|
-
log.debug("Apollo Knowledge.query_knowledge query_length=#{query.to_s.length} limit=#{limit} statuses=#{Array(status).join(',')} tags=#{Array(tags).size} data_available=#{
|
|
50
|
-
|
|
60
|
+
def query_knowledge(query:, limit: settings[:query][:default_limit], min_confidence: settings[:query][:default_min_confidence], status: %i[confirmed candidate], tags: nil, **) # rubocop:disable Layout/LineLength
|
|
61
|
+
log.debug("Apollo Knowledge.query_knowledge query_length=#{query.to_s.length} limit=#{limit} statuses=#{Array(status).join(',')} tags=#{Array(tags).size} data_available=#{!data_model_entry.nil?}") # rubocop:disable Layout/LineLength
|
|
62
|
+
unless data_model_entry.nil?
|
|
51
63
|
return handle_query(query: query, limit: limit, min_confidence: min_confidence,
|
|
52
64
|
status: status, tags: tags, **)
|
|
53
65
|
end
|
|
@@ -62,9 +74,9 @@ module Legion
|
|
|
62
74
|
}
|
|
63
75
|
end
|
|
64
76
|
|
|
65
|
-
def related_entries(entry_id:, relation_types: nil, depth:
|
|
66
|
-
log.debug("Apollo Knowledge.related_entries entry_id=#{entry_id} depth=#{depth} relation_types=#{Array(relation_types).join(',')} data_available=#{
|
|
67
|
-
return handle_traverse(entry_id: entry_id, depth: depth, relation_types: relation_types, **)
|
|
77
|
+
def related_entries(entry_id:, relation_types: nil, depth: settings[:graph][:default_depth], **)
|
|
78
|
+
log.debug("Apollo Knowledge.related_entries entry_id=#{entry_id} depth=#{depth} relation_types=#{Array(relation_types).join(',')} data_available=#{!data_model_entry.nil?}") # rubocop:disable Layout/LineLength
|
|
79
|
+
return handle_traverse(entry_id: entry_id, depth: depth, relation_types: relation_types, **) unless data_model_entry.nil?
|
|
68
80
|
|
|
69
81
|
{
|
|
70
82
|
action: :traverse,
|
|
@@ -74,154 +86,178 @@ module Legion
|
|
|
74
86
|
}
|
|
75
87
|
end
|
|
76
88
|
|
|
77
|
-
def deprecate_entry(entry_id:, reason:, **)
|
|
78
|
-
{
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
def deprecate_entry(entry_id:, reason:, requesting_principal_id: nil, **)
|
|
90
|
+
return { action: :deprecate, entry_id: entry_id, reason: reason } if data_model_entry.nil?
|
|
91
|
+
|
|
92
|
+
entry_model = data_model_entry
|
|
93
|
+
requesting_principal_id = knowledge_resolve_requesting_principal_id(requesting_principal_id)
|
|
94
|
+
entry_model.db.transaction do
|
|
95
|
+
entries = entry_model.for_update
|
|
96
|
+
if requesting_principal_id
|
|
97
|
+
entries = entries.where(Sequel.lit(graph_access_scope_sql(global_only: requesting_principal_id.zero?),
|
|
98
|
+
requesting_principal_id: requesting_principal_id))
|
|
99
|
+
end
|
|
100
|
+
entry = entries.with_pk(entry_id)
|
|
101
|
+
next({ success: false, error: 'entry_not_found', entry_id: entry_id }) unless entry
|
|
102
|
+
|
|
103
|
+
context = entry[:source_context]
|
|
104
|
+
context = Legion::JSON.load(context) if context.is_a?(String)
|
|
105
|
+
context ||= {}
|
|
106
|
+
raise Sequel::Error, 'invalid_source_context' unless context.respond_to?(:transform_keys)
|
|
107
|
+
|
|
108
|
+
context = context.transform_keys(&:to_sym)
|
|
109
|
+
unchanged = entry.status == 'archived' && context[:deprecation_reason] == reason
|
|
110
|
+
unless unchanged
|
|
111
|
+
entry.update(status: 'archived', source_context: Legion::JSON.dump(context.merge(deprecation_reason: reason)),
|
|
112
|
+
updated_at: Time.now)
|
|
113
|
+
end
|
|
114
|
+
{ success: true, entry_id: entry.id, status: 'archived', updated: !unchanged, reason: reason }
|
|
115
|
+
end
|
|
116
|
+
rescue Sequel::Error, Legion::JSON::ParseError => e
|
|
117
|
+
handle_exception(e, level: :error, operation: 'apollo.knowledge.deprecate_entry')
|
|
118
|
+
{ success: false, error: e.message, entry_id: entry_id }
|
|
83
119
|
end
|
|
84
120
|
|
|
85
|
-
def handle_ingest(content
|
|
86
|
-
source_provider: nil, source_channel: nil, knowledge_domain: nil,
|
|
121
|
+
def handle_ingest(content:, content_type: :observation, tags: [], source_agent: 'unknown', # rubocop:disable Metrics/ParameterLists
|
|
122
|
+
source_provider: nil, source_model: nil, source_channel: nil, knowledge_domain: nil,
|
|
87
123
|
submitted_by: nil, submitted_from: nil, content_hash: nil, context: {},
|
|
88
|
-
skip: false, access_scope: 'global',
|
|
124
|
+
skip: false, access_scope: 'global', embedding: nil, embedding_provenance: nil,
|
|
125
|
+
identity_principal_id: UNSET, identity_id: UNSET, identity_canonical_name: UNSET, **)
|
|
89
126
|
return { status: :skipped } if skip
|
|
90
127
|
|
|
91
|
-
identity =
|
|
92
|
-
|
|
93
|
-
|
|
128
|
+
identity = knowledge_resolve_process_identity(principal_id: identity_principal_id, identity_id: identity_id,
|
|
129
|
+
canonical_name: identity_canonical_name)
|
|
130
|
+
access_scope = (access_scope || 'global').to_s
|
|
131
|
+
return { success: false, error: 'invalid_access_scope' } unless %w[global private team].include?(access_scope)
|
|
132
|
+
if access_scope != 'global' && !(identity[:principal_id].is_a?(Integer) && identity[:principal_id].positive?)
|
|
133
|
+
return { success: false, error: 'identity_required' }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
content = knowledge_normalize_text_input(content)
|
|
137
|
+
content_type = knowledge_normalize_content_type(content_type.nil? ? :observation : content_type)
|
|
94
138
|
log.debug("Apollo Knowledge.handle_ingest content_length=#{content.length} content_type=#{content_type} tags=#{Array(tags).size} source_agent=#{source_agent} source_channel=#{source_channel || 'nil'}") # rubocop:disable Layout/LineLength
|
|
95
|
-
early_error =
|
|
139
|
+
early_error = knowledge_ingest_early_return_error(content: content, content_type: content_type, tags: tags)
|
|
96
140
|
return early_error if early_error
|
|
97
141
|
|
|
98
|
-
hash = content_hash ||
|
|
99
|
-
existing =
|
|
100
|
-
|
|
142
|
+
hash = content_hash || Legion::Extensions::Apollo::Helpers::Writeback.content_hash(content: content)
|
|
143
|
+
existing = knowledge_active_duplicate_for_hash(content_hash: hash, access_scope: access_scope,
|
|
144
|
+
identity_principal_id: identity[:principal_id])
|
|
101
145
|
if existing
|
|
146
|
+
knowledge_refresh_duplicate_embedding(entry: existing)
|
|
102
147
|
log.info("Apollo Knowledge.handle_ingest deduped entry_id=#{existing.id} source_agent=#{source_agent}")
|
|
103
148
|
return { success: true, entry_id: existing.id, deduped: true }
|
|
104
149
|
end
|
|
105
150
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
metadata = ingest_metadata(tags: tags, knowledge_domain: knowledge_domain, source_agent: source_agent,
|
|
109
|
-
source_provider: source_provider, source_channel: source_channel,
|
|
110
|
-
submitted_by: submitted_by, submitted_from: submitted_from,
|
|
111
|
-
access_scope: access_scope,
|
|
112
|
-
identity_principal_id: identity[:principal_id],
|
|
113
|
-
identity_id: identity[:identity_id],
|
|
114
|
-
identity_canonical_name: identity[:canonical_name])
|
|
115
|
-
|
|
116
|
-
corroborated, existing_id = find_corroboration(
|
|
117
|
-
embedding, content_type_sym, metadata[:source_agent], metadata[:source_channel]
|
|
151
|
+
document = Legion::Extensions::Apollo::Runners::Embedding.document(
|
|
152
|
+
text: content, vector: embedding, provenance: embedding_provenance
|
|
118
153
|
)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
schedule_contradiction_detection(entry_id: existing_id, embedding: embedding, content: content)
|
|
136
|
-
log.debug("Apollo Knowledge.handle_ingest complete entry_id=#{existing_id} corroborated=#{corroborated}")
|
|
137
|
-
|
|
138
|
-
{ success: true, entry_id: existing_id, status: corroborated ? 'corroborated' : 'candidate',
|
|
139
|
-
corroborated: corroborated, contradictions: [] }
|
|
140
|
-
rescue Sequel::Error => e
|
|
154
|
+
metadata = knowledge_ingest_metadata(tags: tags, knowledge_domain: knowledge_domain, source_agent: source_agent,
|
|
155
|
+
source_provider: source_provider, source_channel: source_channel,
|
|
156
|
+
submitted_by: submitted_by, submitted_from: submitted_from,
|
|
157
|
+
access_scope: access_scope,
|
|
158
|
+
identity_principal_id: identity[:principal_id],
|
|
159
|
+
identity_id: identity[:identity_id],
|
|
160
|
+
identity_canonical_name: identity[:canonical_name])
|
|
161
|
+
|
|
162
|
+
context = (context.is_a?(Hash) ? context : {}).merge(source_model: source_model) unless source_model.nil?
|
|
163
|
+
result = knowledge_persist_ingest(content: content, content_type: content_type.to_s, context: context,
|
|
164
|
+
metadata: metadata, content_hash: hash,
|
|
165
|
+
embedding: document[:vector], provenance: document[:provenance])
|
|
166
|
+
knowledge_schedule_contradiction_detection(entry_id: result[:entry_id])
|
|
167
|
+
log.debug("Apollo Knowledge.handle_ingest complete entry_id=#{result[:entry_id]} corroborated=#{result[:corroborated]}")
|
|
168
|
+
result
|
|
169
|
+
rescue Sequel::Error, Legion::LLM::LLMError, Legion::Extensions::Apollo::Helpers::Embedding::Error => e
|
|
141
170
|
handle_exception(e, level: :error, operation: 'apollo.knowledge.handle_ingest')
|
|
142
171
|
{ success: false, error: e.message }
|
|
143
172
|
end
|
|
144
173
|
|
|
145
|
-
def handle_query(query:, limit:
|
|
146
|
-
min_confidence:
|
|
147
|
-
status: UNSET, tags: nil, domain: nil, agent_id: 'unknown',
|
|
174
|
+
def handle_query(query:, limit: settings[:query][:default_limit], # rubocop:disable Metrics/ParameterLists
|
|
175
|
+
min_confidence: settings[:query][:default_min_confidence],
|
|
176
|
+
status: UNSET, tags: nil, tag_match: :any, exclude_source_agents: [], domain: nil, agent_id: 'unknown',
|
|
148
177
|
requesting_principal_id: nil, **)
|
|
149
|
-
return { success: false, error: 'apollo_data_not_available' }
|
|
178
|
+
return { success: false, error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
179
|
+
return { success: false, error: 'invalid_tag_match' } unless %w[any all].include?(tag_match.to_s)
|
|
150
180
|
|
|
151
|
-
requesting_principal_id =
|
|
152
|
-
entry_model =
|
|
153
|
-
query =
|
|
181
|
+
requesting_principal_id = knowledge_resolve_requesting_principal_id(requesting_principal_id)
|
|
182
|
+
entry_model = data_model_entry
|
|
183
|
+
query = knowledge_normalize_text_input(query)
|
|
154
184
|
status_defaulted = status.equal?(UNSET)
|
|
155
185
|
requested_status = status_defaulted ? DEFAULT_QUERY_STATUS : status
|
|
156
|
-
log.debug("Apollo Knowledge.handle_query
|
|
157
|
-
if
|
|
158
|
-
return
|
|
159
|
-
|
|
160
|
-
|
|
186
|
+
log.debug("Apollo Knowledge.handle_query query_length=#{query.length} limit=#{limit} statuses=#{requested_status.inspect} status_defaulted=#{status_defaulted} domain=#{domain.inspect} agent_id=#{agent_id}") # rubocop:disable Layout/LineLength
|
|
187
|
+
if knowledge_browse_query?(query)
|
|
188
|
+
return knowledge_list_entries_chronologically(query: query, limit: limit, status: requested_status,
|
|
189
|
+
status_defaulted: status_defaulted, tags: tags, domain: domain,
|
|
190
|
+
requesting_principal_id: requesting_principal_id, min_confidence: min_confidence,
|
|
191
|
+
tag_match: tag_match, exclude_source_agents: exclude_source_agents)
|
|
161
192
|
end
|
|
162
193
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return list_entries_chronologically(query: query, limit: limit, status: requested_status,
|
|
167
|
-
status_defaulted: status_defaulted, tags: tags, domain: domain,
|
|
168
|
-
requesting_principal_id: requesting_principal_id)
|
|
194
|
+
if data_model_access_log.nil?
|
|
195
|
+
log.warn('Apollo Knowledge.handle_query skipped: apollo_data_not_available missing=access_log')
|
|
196
|
+
return { success: false, error: 'apollo_data_not_available' }
|
|
169
197
|
end
|
|
170
198
|
|
|
171
|
-
|
|
172
|
-
limit: limit, min_confidence: min_confidence,
|
|
173
|
-
statuses: Array(requested_status).map(&:to_s), tags: tags, domain: domain,
|
|
174
|
-
requesting_principal_id: requesting_principal_id
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
db = entry_model.db
|
|
178
|
-
entries = db.fetch(sql, embedding: Sequel.lit("'[#{embedding.join(',')}]'::vector")).all
|
|
199
|
+
embedding = Legion::Extensions::Apollo::Runners::Embedding.generate(text: query, task: :query)
|
|
179
200
|
|
|
180
|
-
|
|
201
|
+
query_options = {
|
|
202
|
+
limit: limit, min_confidence: min_confidence,
|
|
203
|
+
statuses: Array(requested_status).map(&:to_s), tags: Array(tags).map(&:to_s),
|
|
204
|
+
tag_match: tag_match, exclude_source_agents: Array(exclude_source_agents).map(&:to_s),
|
|
205
|
+
domain: domain&.to_s, requesting_principal_id: requesting_principal_id&.to_i
|
|
206
|
+
}
|
|
207
|
+
entries = Legion::Extensions::Apollo::Runners::Search.semantic_search(
|
|
208
|
+
db: entry_model.db, embedding: embedding[:vector], provenance: embedding[:provenance], **query_options
|
|
209
|
+
).fetch(:entries)
|
|
181
210
|
|
|
182
211
|
entries.each do |entry|
|
|
183
|
-
|
|
212
|
+
knowledge_boost_entry_after_query(entry_model, entry)
|
|
184
213
|
end
|
|
185
214
|
|
|
186
|
-
|
|
215
|
+
knowledge_record_query_access(entry_id: entries.first[:id], agent_id: agent_id) if entries.any?
|
|
187
216
|
|
|
188
|
-
formatted = entries.map
|
|
189
|
-
{ id: entry[:id], content: entry[:content], content_type: entry[:content_type],
|
|
190
|
-
confidence: entry[:confidence], distance: entry[:distance]&.to_f,
|
|
191
|
-
tags: entry[:tags], source_agent: entry[:source_agent],
|
|
192
|
-
knowledge_domain: entry[:knowledge_domain] }
|
|
193
|
-
end
|
|
217
|
+
formatted = entries.map { |entry| knowledge_format_entry(entry: entry) }
|
|
194
218
|
|
|
195
219
|
log.info("Apollo Knowledge.handle_query results=#{formatted.size} mode=semantic agent_id=#{agent_id}")
|
|
196
220
|
{ success: true, entries: formatted, count: formatted.size }
|
|
197
|
-
rescue Sequel::Error => e
|
|
221
|
+
rescue Sequel::Error, Legion::JSON::ParseError, Legion::LLM::LLMError, Legion::Extensions::Apollo::Helpers::Embedding::Error => e
|
|
198
222
|
handle_exception(e, level: :error, operation: 'apollo.knowledge.handle_query')
|
|
199
223
|
{ success: false, error: e.message }
|
|
200
224
|
end
|
|
201
225
|
|
|
202
|
-
def handle_traverse(entry_id:, depth:
|
|
203
|
-
|
|
226
|
+
def handle_traverse(entry_id:, depth: settings[:graph][:default_depth], relation_types: nil,
|
|
227
|
+
agent_id: 'unknown', requesting_principal_id: nil, **)
|
|
228
|
+
return { success: false, error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
229
|
+
|
|
230
|
+
if agent_id != 'unknown' && data_model_access_log.nil?
|
|
231
|
+
log.warn('Apollo Knowledge.handle_traverse skipped: apollo_data_not_available missing=access_log')
|
|
232
|
+
return { success: false, error: 'apollo_data_not_available' }
|
|
233
|
+
end
|
|
204
234
|
|
|
205
235
|
log.debug("Apollo Knowledge.handle_traverse entry_id=#{entry_id} depth=#{depth} relation_types=#{Array(relation_types).join(',')} agent_id=#{agent_id}") # rubocop:disable Layout/LineLength
|
|
206
|
-
#
|
|
236
|
+
# Keep traversal limited to the supported relation vocabulary.
|
|
207
237
|
if relation_types
|
|
208
|
-
allowed = Helpers::Confidence::RELATION_TYPES
|
|
209
|
-
relation_types = relation_types.select { |
|
|
238
|
+
allowed = Legion::Extensions::Apollo::Helpers::Confidence::RELATION_TYPES
|
|
239
|
+
relation_types = relation_types.map(&:to_s).select { |type| allowed.include?(type) }
|
|
210
240
|
end
|
|
211
241
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
242
|
+
requesting_principal_id = knowledge_resolve_requesting_principal_id(requesting_principal_id)
|
|
243
|
+
sql = graph_build_traversal_sql(depth: depth, relation_types: relation_types,
|
|
244
|
+
requesting_principal_id: requesting_principal_id)
|
|
245
|
+
db = data_model_entry.db
|
|
246
|
+
entries = db.fetch(sql, entry_id: entry_id, depth: depth, relation_types: relation_types,
|
|
247
|
+
requesting_principal_id: requesting_principal_id,
|
|
248
|
+
spread_factor: settings[:graph][:spread_factor],
|
|
249
|
+
min_activation: settings[:graph][:min_activation]).all
|
|
215
250
|
|
|
216
251
|
if entries.any? && agent_id != 'unknown'
|
|
217
|
-
|
|
252
|
+
data_model_access_log.create(
|
|
218
253
|
entry_id: entry_id, agent_id: agent_id, action: 'query'
|
|
219
254
|
)
|
|
220
255
|
end
|
|
221
256
|
|
|
222
257
|
formatted = entries.map do |entry|
|
|
223
258
|
{ id: entry[:id], content: entry[:content], content_type: entry[:content_type],
|
|
224
|
-
confidence: entry[:confidence], tags:
|
|
259
|
+
confidence: entry[:confidence], tags: storage_deserialize_tags(tags: entry[:tags]),
|
|
260
|
+
source_agent: entry[:source_agent],
|
|
225
261
|
depth: entry[:depth], activation: entry[:activation] }
|
|
226
262
|
end
|
|
227
263
|
|
|
@@ -233,27 +269,29 @@ module Legion
|
|
|
233
269
|
end
|
|
234
270
|
|
|
235
271
|
def redistribute_knowledge(agent_id:, min_confidence: settings[:query][:redistribute_min_confidence], **)
|
|
236
|
-
return { success: false, error: 'apollo_data_not_available' }
|
|
272
|
+
return { success: false, error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
237
273
|
|
|
238
274
|
log.debug("Apollo Knowledge.redistribute_knowledge agent_id=#{agent_id} min_confidence=#{min_confidence}")
|
|
239
|
-
entries =
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
275
|
+
entries = data_model_entry
|
|
276
|
+
.where(source_agent: agent_id, status: 'confirmed')
|
|
277
|
+
.where { confidence > min_confidence }
|
|
278
|
+
.all
|
|
243
279
|
|
|
244
280
|
return { success: true, redistributed: 0 } if entries.empty?
|
|
245
281
|
|
|
246
282
|
store = (Legion::Extensions::Agentic::Memory::Trace.shared_store if defined?(Legion::Extensions::Agentic::Memory::Trace))
|
|
283
|
+
return { success: false, error: 'memory_trace_not_available', redistributed: 0 } unless store
|
|
247
284
|
|
|
248
285
|
redistributed = 0
|
|
249
286
|
entries.each do |entry|
|
|
250
287
|
if store
|
|
288
|
+
tags = storage_deserialize_tags(tags: entry.tags)
|
|
251
289
|
trace = Legion::Extensions::Agentic::Memory::Trace::Helpers::Trace.new_trace(
|
|
252
290
|
type: :semantic,
|
|
253
291
|
content_payload: { content: entry.content, source_agent: agent_id,
|
|
254
|
-
content_type: entry.content_type, tags:
|
|
292
|
+
content_type: entry.content_type, tags: tags },
|
|
255
293
|
strength: entry.confidence.to_f,
|
|
256
|
-
domain_tag:
|
|
294
|
+
domain_tag: tags.first || 'general'
|
|
257
295
|
)
|
|
258
296
|
store.store(trace)
|
|
259
297
|
end
|
|
@@ -267,66 +305,58 @@ module Legion
|
|
|
267
305
|
{ success: false, error: e.message }
|
|
268
306
|
end
|
|
269
307
|
|
|
270
|
-
def retrieve_relevant(query: nil, limit: settings[:query][:retrieval_limit],
|
|
271
|
-
min_confidence:
|
|
272
|
-
tags: nil,
|
|
308
|
+
def retrieve_relevant(query: nil, limit: settings[:query][:retrieval_limit], # rubocop:disable Metrics/ParameterLists
|
|
309
|
+
min_confidence: settings[:query][:default_min_confidence],
|
|
310
|
+
status: %i[confirmed candidate], tags: nil, tag_match: :any, exclude_source_agents: [], domain: nil,
|
|
311
|
+
skip: false, requesting_principal_id: nil, **)
|
|
273
312
|
return { status: :skipped } if skip
|
|
274
313
|
|
|
275
|
-
return { success: false, error: 'apollo_data_not_available' }
|
|
314
|
+
return { success: false, error: 'apollo_data_not_available' } if data_model_entry.nil?
|
|
315
|
+
return { success: false, error: 'invalid_tag_match' } unless %w[any all].include?(tag_match.to_s)
|
|
276
316
|
|
|
277
|
-
requesting_principal_id =
|
|
278
|
-
query =
|
|
279
|
-
log.debug("Apollo Knowledge.retrieve_relevant query_length=#{query.length} limit=#{limit} min_confidence=#{min_confidence} tags=#{Array(tags).size} domain=#{domain
|
|
280
|
-
return { success: true, entries: [], count: 0 } if query.
|
|
317
|
+
requesting_principal_id = knowledge_resolve_requesting_principal_id(requesting_principal_id)
|
|
318
|
+
query = knowledge_normalize_text_input(query)
|
|
319
|
+
log.debug("Apollo Knowledge.retrieve_relevant query_length=#{query.length} limit=#{limit} min_confidence=#{min_confidence} tags=#{Array(tags).size} domain=#{domain.inspect}") # rubocop:disable Layout/LineLength
|
|
320
|
+
return { success: true, entries: [], count: 0 } if query.to_s.strip.empty?
|
|
281
321
|
|
|
282
|
-
embedding =
|
|
283
|
-
if embedding.nil?
|
|
284
|
-
log.warn('Apollo Knowledge.retrieve_relevant embedding unavailable; returning empty result')
|
|
285
|
-
return { success: true, entries: [], count: 0, degraded: :no_embedding }
|
|
286
|
-
end
|
|
322
|
+
embedding = Legion::Extensions::Apollo::Runners::Embedding.generate(text: query, task: :query)
|
|
287
323
|
|
|
288
|
-
|
|
324
|
+
query_options = {
|
|
289
325
|
limit: limit, min_confidence: min_confidence,
|
|
290
|
-
statuses:
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
326
|
+
statuses: Array(status).map(&:to_s), tags: Array(tags).map(&:to_s),
|
|
327
|
+
tag_match: tag_match, exclude_source_agents: Array(exclude_source_agents).map(&:to_s),
|
|
328
|
+
domain: domain&.to_s, requesting_principal_id: requesting_principal_id&.to_i
|
|
329
|
+
}
|
|
330
|
+
entries = Legion::Extensions::Apollo::Runners::Search.semantic_search(
|
|
331
|
+
db: data_model_entry.db,
|
|
332
|
+
embedding: embedding[:vector], provenance: embedding[:provenance], **query_options
|
|
333
|
+
).fetch(:entries)
|
|
297
334
|
|
|
298
335
|
entries.each do |entry|
|
|
299
|
-
|
|
300
|
-
confidence:
|
|
336
|
+
data_model_entry.where(id: entry[:id]).update(
|
|
337
|
+
confidence: confidence_apply_retrieval_boost(confidence: entry[:confidence]),
|
|
301
338
|
updated_at: Time.now
|
|
302
339
|
)
|
|
303
340
|
end
|
|
304
341
|
|
|
305
|
-
formatted = entries.map
|
|
306
|
-
{ id: entry[:id], content: entry[:content], content_type: entry[:content_type],
|
|
307
|
-
confidence: entry[:confidence], distance: entry[:distance]&.to_f,
|
|
308
|
-
tags: entry[:tags], source_agent: entry[:source_agent],
|
|
309
|
-
knowledge_domain: entry[:knowledge_domain] }
|
|
310
|
-
end
|
|
342
|
+
formatted = entries.map { |entry| knowledge_format_entry(entry: entry) }
|
|
311
343
|
|
|
312
344
|
log.info("Apollo Knowledge.retrieve_relevant results=#{formatted.size} limit=#{limit}")
|
|
313
345
|
{ success: true, entries: formatted, count: formatted.size }
|
|
314
|
-
rescue Sequel::Error => e
|
|
346
|
+
rescue Sequel::Error, Legion::JSON::ParseError, Legion::LLM::LLMError, Legion::Extensions::Apollo::Helpers::Embedding::Error => e
|
|
315
347
|
handle_exception(e, level: :error, operation: 'apollo.knowledge.retrieve_relevant')
|
|
316
348
|
{ success: false, error: e.message }
|
|
317
349
|
end
|
|
318
350
|
|
|
319
351
|
def prepare_mesh_export(target_domain:, min_confidence: settings[:query][:mesh_export_min_confidence], limit: settings[:query][:mesh_export_limit], **) # rubocop:disable Layout/LineLength
|
|
320
|
-
|
|
321
|
-
return { success: false, error: 'apollo_data_not_available' }
|
|
322
|
-
end
|
|
352
|
+
return { success: false, error: 'apollo_data_not_available' } unless Legion::Data.connection
|
|
323
353
|
|
|
324
354
|
log.debug("Apollo Knowledge.prepare_mesh_export target_domain=#{target_domain} min_confidence=#{min_confidence} limit=#{limit}")
|
|
325
355
|
conn = Legion::Data.connection
|
|
326
|
-
allowed =
|
|
356
|
+
allowed = knowledge_allowed_domains_for(target_domain)
|
|
327
357
|
|
|
328
358
|
dataset = conn[:apollo_entries]
|
|
329
|
-
.where(status: 'confirmed')
|
|
359
|
+
.where(status: 'confirmed', access_scope: 'global')
|
|
330
360
|
.where { confidence >= min_confidence }
|
|
331
361
|
.limit(limit)
|
|
332
362
|
|
|
@@ -337,7 +367,7 @@ module Legion
|
|
|
337
367
|
formatted = entries.map do |entry|
|
|
338
368
|
{ id: entry[:id], content: entry[:content], content_type: entry[:content_type],
|
|
339
369
|
confidence: entry[:confidence], knowledge_domain: entry[:knowledge_domain],
|
|
340
|
-
tags: entry[:tags], source_agent: entry[:source_agent] }
|
|
370
|
+
tags: storage_deserialize_tags(tags: entry[:tags]), source_agent: entry[:source_agent] }
|
|
341
371
|
end
|
|
342
372
|
|
|
343
373
|
{ success: true, entries: formatted, count: formatted.size, target_domain: target_domain }
|
|
@@ -348,9 +378,7 @@ module Legion
|
|
|
348
378
|
end
|
|
349
379
|
|
|
350
380
|
def handle_erasure_request(agent_id:, **)
|
|
351
|
-
|
|
352
|
-
return { deleted: 0, redacted: 0, error: 'apollo_data_not_available' }
|
|
353
|
-
end
|
|
381
|
+
return { deleted: 0, redacted: 0, error: 'apollo_data_not_available' } unless Legion::Data.connection
|
|
354
382
|
|
|
355
383
|
log.warn("Apollo Knowledge.handle_erasure_request agent_id=#{agent_id}")
|
|
356
384
|
conn = Legion::Data.connection
|
|
@@ -379,11 +407,31 @@ module Legion
|
|
|
379
407
|
{ deleted: 0, redacted: 0, error: e.message }
|
|
380
408
|
end
|
|
381
409
|
|
|
382
|
-
CONFLICT_CHECK_MAX_CHARS = 4000
|
|
383
|
-
|
|
384
410
|
private
|
|
385
411
|
|
|
386
|
-
def
|
|
412
|
+
def knowledge_persist_ingest(content:, content_type:, context:, metadata:, content_hash:, embedding:, provenance:, **)
|
|
413
|
+
data_model_entry.db.transaction do
|
|
414
|
+
corroborated, existing_id = knowledge_find_corroboration(
|
|
415
|
+
embedding: embedding, provenance: provenance, content_type: content_type, metadata: metadata
|
|
416
|
+
)
|
|
417
|
+
unless corroborated
|
|
418
|
+
existing_id = knowledge_create_candidate_entry(
|
|
419
|
+
content: content, content_type: content_type, context: context,
|
|
420
|
+
metadata: metadata, content_hash: content_hash, embedding: embedding, provenance: provenance
|
|
421
|
+
)
|
|
422
|
+
end
|
|
423
|
+
raise Sequel::Error, 'entry_persistence_failed' unless existing_id
|
|
424
|
+
|
|
425
|
+
knowledge_upsert_expertise(source_agent: metadata[:source_agent], domain: metadata[:domain])
|
|
426
|
+
data_model_access_log.create(
|
|
427
|
+
entry_id: existing_id, agent_id: metadata[:source_agent], action: 'ingest'
|
|
428
|
+
)
|
|
429
|
+
{ success: true, entry_id: existing_id, status: corroborated ? 'corroborated' : 'candidate',
|
|
430
|
+
corroborated: corroborated, contradictions: [] }
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def knowledge_ingest_early_return_error(content:, content_type:, tags:)
|
|
387
435
|
if content.strip.empty?
|
|
388
436
|
safe_tags = Array(tags).map(&:to_s).map { |t| t.gsub(/[\r\n]+/, ' ') }
|
|
389
437
|
log.warn('[apollo][handle_ingest] early-return: content is required ' \
|
|
@@ -391,50 +439,34 @@ module Legion
|
|
|
391
439
|
return { success: false, error: 'content is required' }
|
|
392
440
|
end
|
|
393
441
|
|
|
394
|
-
|
|
442
|
+
missing = []
|
|
443
|
+
missing << 'entry' if data_model_entry.nil?
|
|
444
|
+
missing << 'expertise' if data_model_expertise.nil?
|
|
445
|
+
missing << 'access_log' if data_model_access_log.nil?
|
|
446
|
+
return nil if missing.empty?
|
|
395
447
|
|
|
396
448
|
log.warn('[apollo][handle_ingest] early-return: apollo_data_not_available ' \
|
|
397
|
-
"content_type=#{content_type}")
|
|
449
|
+
"content_type=#{content_type} missing=#{missing.join(',')}")
|
|
398
450
|
{ success: false, error: 'apollo_data_not_available' }
|
|
399
451
|
end
|
|
400
452
|
|
|
401
|
-
def
|
|
453
|
+
def knowledge_normalize_content_type(raw)
|
|
402
454
|
sym = raw.to_s.delete_prefix(':').gsub(%r{[/\s]}, '_').strip.downcase.to_sym
|
|
403
455
|
sym = CONTENT_TYPE_ALIASES.fetch(sym, sym)
|
|
404
|
-
Helpers::Confidence::CONTENT_TYPES.include?(sym) ? sym : :observation
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
def embed_text(text)
|
|
408
|
-
text = normalize_text_input(text)
|
|
409
|
-
log.debug("Apollo Knowledge.embed_text text_length=#{text.length}")
|
|
410
|
-
result = Legion::LLM::Call::Embeddings.generate(text: text)
|
|
411
|
-
vector = result.is_a?(Hash) ? result[:vector] : result
|
|
412
|
-
if vector.is_a?(Array) && vector.any? && vector.any? { |v| v != 0.0 }
|
|
413
|
-
log.debug("Apollo Knowledge.embed_text vector_dimensions=#{vector.length}")
|
|
414
|
-
vector
|
|
415
|
-
else
|
|
416
|
-
log.warn('Apollo Knowledge.embed_text returned no usable vector; skipping embedding')
|
|
417
|
-
nil
|
|
418
|
-
end
|
|
419
|
-
rescue StandardError => e
|
|
420
|
-
handle_exception(e, level: :warn, operation: 'apollo.knowledge.embed_text')
|
|
421
|
-
nil
|
|
456
|
+
Legion::Extensions::Apollo::Helpers::Confidence::CONTENT_TYPES.include?(sym) ? sym : :observation
|
|
422
457
|
end
|
|
423
458
|
|
|
424
|
-
def
|
|
459
|
+
def knowledge_normalize_text_input(value)
|
|
425
460
|
result = if defined?(Legion::Apollo) && Legion::Apollo.respond_to?(:normalize_text_input, true)
|
|
426
461
|
Legion::Apollo.send(:normalize_text_input, value)
|
|
427
462
|
else
|
|
428
463
|
value.to_s
|
|
429
464
|
end
|
|
430
465
|
|
|
431
|
-
|
|
432
|
-
rescue StandardError => e
|
|
433
|
-
handle_exception(e, level: :warn, operation: 'apollo.knowledge.normalize_text_input')
|
|
434
|
-
''
|
|
466
|
+
knowledge_sanitize_for_postgres(result)
|
|
435
467
|
end
|
|
436
468
|
|
|
437
|
-
def
|
|
469
|
+
def knowledge_sanitize_for_postgres(value)
|
|
438
470
|
return value unless value.is_a?(String)
|
|
439
471
|
|
|
440
472
|
string = value.encoding == Encoding::UTF_8 ? value.dup : value.dup.force_encoding(Encoding::UTF_8)
|
|
@@ -445,113 +477,129 @@ module Legion
|
|
|
445
477
|
sanitized
|
|
446
478
|
end
|
|
447
479
|
|
|
448
|
-
def
|
|
480
|
+
def knowledge_truncate_for_column(value, max_length)
|
|
449
481
|
return nil if value.nil?
|
|
450
482
|
|
|
451
|
-
|
|
483
|
+
knowledge_normalize_text_input(value)[0, max_length]
|
|
452
484
|
end
|
|
453
485
|
|
|
454
|
-
def
|
|
455
|
-
return nil unless
|
|
486
|
+
def knowledge_active_duplicate_for_hash(content_hash:, access_scope: nil, identity_principal_id: nil, **)
|
|
487
|
+
return nil unless content_hash
|
|
456
488
|
|
|
457
|
-
dataset =
|
|
458
|
-
|
|
459
|
-
|
|
489
|
+
dataset = data_model_entry
|
|
490
|
+
.where(content_hash: content_hash, access_scope: access_scope)
|
|
491
|
+
.exclude(status: 'archived')
|
|
460
492
|
|
|
461
|
-
dataset = dataset.where(identity_principal_id: identity_principal_id) if access_scope
|
|
493
|
+
dataset = dataset.where(identity_principal_id: identity_principal_id) if access_scope != 'global'
|
|
462
494
|
|
|
463
495
|
existing = dataset.first
|
|
464
|
-
existing&.update(confidence: [existing.confidence + Helpers::Confidence.retrieval_boost, 1.0].min)
|
|
465
496
|
log.debug("Apollo Knowledge.active_duplicate_for_hash matched entry_id=#{existing.id}") if existing
|
|
466
497
|
existing
|
|
467
498
|
end
|
|
468
499
|
|
|
469
|
-
def
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
500
|
+
def knowledge_refresh_duplicate_embedding(entry:, **)
|
|
501
|
+
provenance = embedding_parse_provenance(value: entry[:embedding_provenance])
|
|
502
|
+
vector = similarity_parse_vector(vector: entry[:embedding])
|
|
503
|
+
updates = { confidence: [entry.confidence + settings[:confidence][:retrieval_boost], 1.0].min, updated_at: Time.now }
|
|
504
|
+
unless embedding_valid?(vector: vector, provenance: provenance, task: :document)
|
|
505
|
+
document = Legion::Extensions::Apollo::Runners::Embedding.generate(text: entry.content, task: :document)
|
|
506
|
+
db = data_model_entry.db
|
|
507
|
+
updates.merge!(
|
|
508
|
+
embedding: storage_serialize_embedding(db: db, vector: document[:vector]),
|
|
509
|
+
embedding_provenance: storage_serialize_provenance(db: db, provenance: document[:provenance]),
|
|
510
|
+
embedding_space_id: document[:provenance][:space_id], embedding_dimensions: document[:vector].length
|
|
511
|
+
)
|
|
512
|
+
end
|
|
513
|
+
data_model_entry.db.transaction { entry.update(updates) }
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def knowledge_ingest_metadata(tags:, knowledge_domain:, source_agent:, source_provider:, source_channel:, # rubocop:disable Metrics/ParameterLists
|
|
517
|
+
submitted_by:, submitted_from:, access_scope: 'global',
|
|
518
|
+
identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil)
|
|
519
|
+
tag_array = tag_normalize_all(tags: tags)
|
|
520
|
+
agent = knowledge_truncate_for_column(source_agent, 50) || 'unknown'
|
|
474
521
|
|
|
475
522
|
{ tags: tag_array,
|
|
476
|
-
domain:
|
|
523
|
+
domain: knowledge_truncate_for_column(knowledge_domain || tag_array.first || 'general', 50),
|
|
477
524
|
source_agent: agent,
|
|
478
|
-
source_provider:
|
|
479
|
-
source_channel:
|
|
480
|
-
submitted_by:
|
|
481
|
-
submitted_from:
|
|
482
|
-
access_scope: access_scope || 'global',
|
|
525
|
+
source_provider: knowledge_truncate_for_column(source_provider || knowledge_derive_provider_from_agent(agent), 50),
|
|
526
|
+
source_channel: knowledge_truncate_for_column(source_channel, 100),
|
|
527
|
+
submitted_by: knowledge_truncate_for_column(submitted_by, 255),
|
|
528
|
+
submitted_from: knowledge_truncate_for_column(submitted_from, 255),
|
|
529
|
+
access_scope: (access_scope || 'global').to_s,
|
|
483
530
|
identity_principal_id: identity_principal_id.is_a?(Integer) ? identity_principal_id : nil,
|
|
484
531
|
identity_id: identity_id.is_a?(Integer) ? identity_id : nil,
|
|
485
532
|
identity_canonical_name: identity_canonical_name&.to_s&.slice(0, 255) }
|
|
486
533
|
end
|
|
487
534
|
|
|
488
|
-
def
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
535
|
+
def knowledge_create_candidate_entry(content:, content_type:, context:, metadata:, content_hash:, embedding:, provenance:, **)
|
|
536
|
+
entry_model = data_model_entry
|
|
537
|
+
new_entry = entry_model.db.transaction(savepoint: true) do
|
|
538
|
+
entry_model.create(
|
|
539
|
+
content: content,
|
|
540
|
+
content_type: content_type,
|
|
541
|
+
confidence: settings[:confidence][:initial],
|
|
542
|
+
source_agent: metadata[:source_agent],
|
|
543
|
+
source_provider: metadata[:source_provider],
|
|
544
|
+
source_channel: metadata[:source_channel],
|
|
545
|
+
source_context: json_dump(context.is_a?(Hash) ? context : {}),
|
|
546
|
+
tags: storage_serialize_tags(db: entry_model.db, tags: metadata[:tags]),
|
|
547
|
+
status: 'candidate',
|
|
548
|
+
knowledge_domain: metadata[:domain],
|
|
549
|
+
submitted_by: metadata[:submitted_by],
|
|
550
|
+
submitted_from: metadata[:submitted_from],
|
|
551
|
+
content_hash: content_hash,
|
|
552
|
+
access_scope: metadata[:access_scope] || 'global',
|
|
553
|
+
identity_principal_id: metadata[:identity_principal_id],
|
|
554
|
+
identity_id: metadata[:identity_id],
|
|
555
|
+
identity_canonical_name: metadata[:identity_canonical_name],
|
|
556
|
+
embedding: storage_serialize_embedding(db: entry_model.db, vector: embedding),
|
|
557
|
+
embedding_provenance: storage_serialize_provenance(db: entry_model.db, provenance: provenance),
|
|
558
|
+
embedding_space_id: provenance[:space_id],
|
|
559
|
+
embedding_dimensions: embedding.length
|
|
560
|
+
)
|
|
561
|
+
end
|
|
509
562
|
log.info("Apollo Knowledge.handle_ingest created entry_id=#{new_entry.id} status=candidate domain=#{metadata[:domain]} source_agent=#{metadata[:source_agent]} access_scope=#{metadata[:access_scope]}") # rubocop:disable Layout/LineLength
|
|
510
563
|
new_entry.id
|
|
511
564
|
rescue Sequel::UniqueConstraintViolation => e
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
winner = Helpers::DataModels.apollo_entry
|
|
516
|
-
.where(content_hash: content_hash)
|
|
517
|
-
.exclude(status: 'archived')
|
|
518
|
-
.first
|
|
565
|
+
winner_scope = entry_model.where(content_hash: content_hash, access_scope: metadata[:access_scope]).exclude(status: 'archived')
|
|
566
|
+
winner_scope = winner_scope.where(identity_principal_id: metadata[:identity_principal_id]) if metadata[:access_scope] != 'global'
|
|
567
|
+
winner = winner_scope.first
|
|
519
568
|
if winner
|
|
520
|
-
log.warn("Apollo Knowledge.create_candidate_entry race_dedup entry_id=#{winner.id} content_hash=#{content_hash}
|
|
521
|
-
winner.id
|
|
522
|
-
else
|
|
523
|
-
handle_exception(e, level: :warn, handled: true, operation: 'apollo.knowledge.create_candidate_entry',
|
|
524
|
-
content_hash: content_hash)
|
|
525
|
-
nil
|
|
569
|
+
log.warn("Apollo Knowledge.create_candidate_entry race_dedup entry_id=#{winner.id} content_hash=#{content_hash} error=#{e.message}")
|
|
570
|
+
return winner.id
|
|
526
571
|
end
|
|
572
|
+
|
|
573
|
+
handle_exception(e, level: :error, operation: 'apollo.knowledge.create_candidate_entry', content_hash: content_hash)
|
|
574
|
+
raise
|
|
527
575
|
end
|
|
528
576
|
|
|
529
|
-
def
|
|
577
|
+
def knowledge_browse_query?(query)
|
|
530
578
|
query.to_s.strip.length < 3
|
|
531
579
|
end
|
|
532
580
|
|
|
533
|
-
def
|
|
581
|
+
def knowledge_list_entries_chronologically(query:, limit:, status:, status_defaulted:, tags:, domain:, # rubocop:disable Metrics/ParameterLists
|
|
582
|
+
min_confidence:, requesting_principal_id: nil, tag_match: :any, exclude_source_agents: [], **)
|
|
534
583
|
log.debug("Apollo Knowledge.list_entries_chronologically limit=#{limit} statuses=#{Array(status).join(',')} status_defaulted=#{status_defaulted} tags=#{Array(tags).size} domain=#{domain || 'nil'}") # rubocop:disable Layout/LineLength
|
|
535
|
-
dataset =
|
|
584
|
+
dataset = data_model_entry.exclude(status: 'archived')
|
|
536
585
|
requested = Array(status).map(&:to_s).reject(&:empty?)
|
|
537
586
|
dataset = dataset.where(status: requested) unless status_defaulted || requested.empty?
|
|
538
|
-
dataset = dataset
|
|
539
|
-
dataset = dataset.where
|
|
587
|
+
dataset = storage_filter_tags(dataset: dataset, tags: tags, tag_match: tag_match)
|
|
588
|
+
dataset = dataset.where { confidence >= min_confidence }
|
|
589
|
+
if Array(exclude_source_agents).any?
|
|
590
|
+
dataset = dataset.where(Sequel.lit('(source_agent IS NULL OR source_agent NOT IN ?)', Array(exclude_source_agents).map(&:to_s)))
|
|
591
|
+
end
|
|
592
|
+
dataset = dataset.where(knowledge_domain: domain) unless domain.to_s.empty?
|
|
540
593
|
|
|
541
594
|
if requesting_principal_id
|
|
542
|
-
pid = requesting_principal_id.to_i
|
|
543
595
|
dataset = dataset.where(
|
|
544
|
-
Sequel.lit(
|
|
545
|
-
|
|
546
|
-
"OR (access_scope = 'private' AND (identity_principal_id = ? OR identity_id IN (SELECT id FROM identities WHERE principal_id = ?))) " \
|
|
547
|
-
"OR (access_scope = 'team' AND EXISTS (SELECT 1 FROM identity_group_memberships igm1 JOIN identity_group_memberships igm2 ON igm1.group_id = igm2.group_id WHERE igm1.principal_id = ? AND igm2.principal_id = identity_principal_id)))", # rubocop:disable Layout/LineLength
|
|
548
|
-
pid, pid, pid
|
|
549
|
-
)
|
|
596
|
+
Sequel.lit(graph_access_scope_sql(global_only: requesting_principal_id.zero?),
|
|
597
|
+
requesting_principal_id: requesting_principal_id.to_i)
|
|
550
598
|
)
|
|
551
599
|
end
|
|
552
600
|
|
|
553
601
|
entries = dataset.order(Sequel.desc(:created_at)).limit(limit).all.map do |entry|
|
|
554
|
-
|
|
602
|
+
knowledge_format_entry(entry: entry.is_a?(Hash) ? entry : entry.values)
|
|
555
603
|
end
|
|
556
604
|
log.info("Apollo Knowledge.list_entries_chronologically results=#{entries.size}")
|
|
557
605
|
{ success: true, mode: :browse, query: query, entries: entries, count: entries.size }
|
|
@@ -560,34 +608,28 @@ module Legion
|
|
|
560
608
|
{ success: false, error: e.message }
|
|
561
609
|
end
|
|
562
610
|
|
|
563
|
-
def
|
|
611
|
+
def knowledge_format_entry(entry:, **)
|
|
564
612
|
{ id: entry[:id], content: entry[:content], content_type: entry[:content_type],
|
|
565
613
|
confidence: entry[:confidence], distance: entry[:distance]&.to_f,
|
|
566
|
-
tags: entry[:tags], source_agent: entry[:source_agent],
|
|
614
|
+
tags: storage_deserialize_tags(tags: entry[:tags]), source_agent: entry[:source_agent],
|
|
567
615
|
knowledge_domain: entry[:knowledge_domain] }
|
|
568
616
|
end
|
|
569
617
|
|
|
570
|
-
def
|
|
571
|
-
|
|
618
|
+
def knowledge_record_query_access(entry_id:, agent_id:)
|
|
619
|
+
data_model_access_log.create(
|
|
572
620
|
entry_id: entry_id, agent_id: agent_id, action: 'query'
|
|
573
621
|
)
|
|
574
622
|
end
|
|
575
623
|
|
|
576
|
-
def
|
|
624
|
+
def knowledge_boost_entry_after_query(entry_model, entry)
|
|
577
625
|
entry_model.where(id: entry[:id]).update(
|
|
578
626
|
access_count: Sequel.expr(:access_count) + 1,
|
|
579
|
-
confidence:
|
|
580
|
-
confidence: entry[:confidence]
|
|
581
|
-
),
|
|
627
|
+
confidence: confidence_apply_retrieval_boost(confidence: entry[:confidence]),
|
|
582
628
|
updated_at: Time.now
|
|
583
629
|
)
|
|
584
630
|
end
|
|
585
631
|
|
|
586
|
-
def
|
|
587
|
-
Legion::Extensions::Apollo.settings
|
|
588
|
-
end
|
|
589
|
-
|
|
590
|
-
def allowed_domains_for(target_domain)
|
|
632
|
+
def knowledge_allowed_domains_for(target_domain)
|
|
591
633
|
rules = settings[:domain_isolation]
|
|
592
634
|
|
|
593
635
|
allowed = rules[target_domain]
|
|
@@ -596,99 +638,43 @@ module Legion
|
|
|
596
638
|
Array(allowed)
|
|
597
639
|
end
|
|
598
640
|
|
|
599
|
-
def
|
|
600
|
-
settings[:contradiction][:enabled] == true
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
def schedule_contradiction_detection(entry_id:, embedding:, content:)
|
|
604
|
-
return unless contradiction_detection_enabled?
|
|
605
|
-
return unless embedding && Helpers::DataModels.apollo_entry_available?
|
|
641
|
+
def knowledge_schedule_contradiction_detection(entry_id:, **)
|
|
642
|
+
return unless settings[:contradiction][:enabled] == true
|
|
643
|
+
return if data_model_entry.nil?
|
|
606
644
|
|
|
607
|
-
Legion::Extensions::Apollo::
|
|
608
|
-
entry_id: entry_id, embedding: embedding, content: content
|
|
609
|
-
)
|
|
645
|
+
Legion::Extensions::Apollo::Runners::Contradictions.enqueue(entry_id: entry_id)
|
|
610
646
|
log.debug("Apollo Knowledge.schedule_contradiction_detection enqueued entry_id=#{entry_id}")
|
|
611
647
|
rescue StandardError => e
|
|
612
648
|
handle_exception(e, level: :warn, operation: 'apollo.knowledge.schedule_contradiction_detection')
|
|
613
649
|
end
|
|
614
650
|
|
|
615
|
-
def
|
|
616
|
-
return [] unless embedding && Helpers::DataModels.apollo_entry_available?
|
|
617
|
-
|
|
618
|
-
sim_limit = settings[:contradiction][:similar_limit]
|
|
619
|
-
sim_threshold = settings[:contradiction][:similarity_threshold]
|
|
620
|
-
rel_weight = settings[:contradiction][:relation_weight]
|
|
621
|
-
|
|
622
|
-
db = Helpers::DataModels.apollo_entry.db
|
|
623
|
-
log.debug("Apollo Knowledge.detect_contradictions entry_id=#{entry_id} similar_limit=#{sim_limit} threshold=#{sim_threshold}")
|
|
624
|
-
similar = db.fetch(
|
|
625
|
-
"SELECT id, content, embedding FROM apollo_entries WHERE id != :entry_id AND embedding IS NOT NULL ORDER BY embedding <=> :embedding LIMIT #{sim_limit}", # rubocop:disable Layout/LineLength
|
|
626
|
-
entry_id: entry_id,
|
|
627
|
-
embedding: Sequel.lit("'[#{embedding.join(',')}]'::vector")
|
|
628
|
-
).all
|
|
629
|
-
|
|
630
|
-
contradictions = []
|
|
631
|
-
similar.each do |existing|
|
|
632
|
-
existing_embedding = existing[:embedding]
|
|
633
|
-
next unless existing_embedding
|
|
634
|
-
|
|
635
|
-
sim = Helpers::Similarity.cosine_similarity(vec_a: embedding, vec_b: existing_embedding)
|
|
636
|
-
next unless sim > sim_threshold
|
|
637
|
-
next unless llm_detects_conflict?(content, existing[:content])
|
|
638
|
-
|
|
639
|
-
Helpers::DataModels.apollo_relation.create(
|
|
640
|
-
from_entry_id: entry_id, to_entry_id: existing[:id],
|
|
641
|
-
relation_type: 'contradicts', source_agent: 'system:contradiction',
|
|
642
|
-
weight: rel_weight
|
|
643
|
-
)
|
|
644
|
-
|
|
645
|
-
Helpers::DataModels.apollo_entry.where(id: [entry_id, existing[:id]]).update(status: 'disputed')
|
|
646
|
-
contradictions << existing[:id]
|
|
647
|
-
end
|
|
648
|
-
log.info("Apollo Knowledge.detect_contradictions entry_id=#{entry_id} contradictions=#{contradictions.size}") if contradictions.any?
|
|
649
|
-
contradictions
|
|
650
|
-
rescue Sequel::Error => e
|
|
651
|
-
handle_exception(e, level: :error, operation: 'apollo.knowledge.detect_contradictions')
|
|
652
|
-
[]
|
|
653
|
-
end
|
|
654
|
-
|
|
655
|
-
def llm_detects_conflict?(content_a, content_b)
|
|
656
|
-
return false unless defined?(Legion::LLM) && Legion::LLM.respond_to?(:structured)
|
|
657
|
-
|
|
658
|
-
a = content_a.to_s[0, CONFLICT_CHECK_MAX_CHARS]
|
|
659
|
-
b = content_b.to_s[0, CONFLICT_CHECK_MAX_CHARS]
|
|
660
|
-
result = Legion::LLM.structured(
|
|
661
|
-
messages: [
|
|
662
|
-
{ role: 'system', content: 'Do these two statements contradict each other? Return JSON.' },
|
|
663
|
-
{ role: 'user', content: "A: #{a}\n\nB: #{b}" }
|
|
664
|
-
],
|
|
665
|
-
schema: { type: 'object', properties: { contradicts: { type: 'boolean' } } },
|
|
666
|
-
caller: { extension: 'lex-apollo', runner: 'knowledge' }
|
|
667
|
-
)
|
|
668
|
-
result[:data]&.dig(:contradicts) == true
|
|
669
|
-
rescue StandardError => e
|
|
670
|
-
handle_exception(e, level: :warn, operation: 'apollo.knowledge.llm_detects_conflict')
|
|
671
|
-
false
|
|
672
|
-
end
|
|
673
|
-
|
|
674
|
-
def find_corroboration(embedding, content_type_sym, source_agent, source_channel = nil)
|
|
651
|
+
def knowledge_find_corroboration(embedding:, provenance:, content_type:, metadata:, **)
|
|
675
652
|
return [false, nil] unless embedding
|
|
653
|
+
return [false, nil] if metadata[:access_scope] != 'global' && metadata[:identity_principal_id].nil?
|
|
676
654
|
|
|
655
|
+
source_agent = metadata[:source_agent]
|
|
656
|
+
source_channel = metadata[:source_channel]
|
|
677
657
|
scan_limit = settings[:corroboration][:scan_limit]
|
|
678
|
-
|
|
679
|
-
existing =
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
658
|
+
# Candidate evidence is retained for the bounded maintenance bootstrap scan.
|
|
659
|
+
existing = data_model_entry
|
|
660
|
+
.where(content_type: content_type, knowledge_domain: metadata[:domain],
|
|
661
|
+
access_scope: metadata[:access_scope], status: 'confirmed',
|
|
662
|
+
embedding_space_id: provenance[:space_id], embedding_dimensions: provenance[:dimensions])
|
|
663
|
+
.exclude(embedding: nil)
|
|
664
|
+
existing = existing.where(identity_principal_id: metadata[:identity_principal_id]) if metadata[:access_scope] != 'global'
|
|
665
|
+
existing = existing.limit(scan_limit)
|
|
683
666
|
|
|
684
667
|
existing.each do |entry|
|
|
685
|
-
|
|
668
|
+
stored_provenance = embedding_parse_provenance(value: entry[:embedding_provenance])
|
|
669
|
+
stored_vector = similarity_parse_vector(vector: entry.embedding)
|
|
670
|
+
next unless embedding_valid?(vector: stored_vector, provenance: stored_provenance, task: :document)
|
|
671
|
+
next unless embedding_compatible?(left: provenance, right: stored_provenance)
|
|
686
672
|
|
|
687
|
-
sim =
|
|
688
|
-
next unless
|
|
673
|
+
sim = similarity_cosine(vec_a: embedding, vec_b: stored_vector)
|
|
674
|
+
next unless similarity_above_corroboration_threshold?(similarity: sim)
|
|
689
675
|
|
|
690
676
|
same_provider_wt = settings[:corroboration][:same_provider_weight]
|
|
691
|
-
weight =
|
|
677
|
+
weight = knowledge_same_source_provider?(source_agent, entry) ? same_provider_wt : 1.0
|
|
692
678
|
|
|
693
679
|
# Reject corroboration entirely if same channel (same data source)
|
|
694
680
|
if source_channel && entry.respond_to?(:source_channel)
|
|
@@ -698,7 +684,7 @@ module Legion
|
|
|
698
684
|
next if weight.zero?
|
|
699
685
|
|
|
700
686
|
entry.update(
|
|
701
|
-
confidence:
|
|
687
|
+
confidence: confidence_apply_corroboration_boost(confidence: entry.confidence, weight: weight),
|
|
702
688
|
updated_at: Time.now
|
|
703
689
|
)
|
|
704
690
|
log.info("Apollo Knowledge.find_corroboration matched entry_id=#{entry.id} source_agent=#{source_agent} similarity=#{sim}")
|
|
@@ -709,28 +695,28 @@ module Legion
|
|
|
709
695
|
[false, nil]
|
|
710
696
|
end
|
|
711
697
|
|
|
712
|
-
def
|
|
698
|
+
def knowledge_same_source_provider?(submitting_agent, entry)
|
|
713
699
|
stored = entry.respond_to?(:source_provider) ? entry.source_provider : nil
|
|
714
700
|
return false if stored.nil? || stored.to_s.empty? || stored.to_s == 'unknown'
|
|
715
701
|
|
|
716
|
-
|
|
702
|
+
knowledge_derive_provider_from_agent(submitting_agent) == stored.to_s
|
|
717
703
|
end
|
|
718
704
|
|
|
719
|
-
def
|
|
705
|
+
def knowledge_derive_provider_from_agent(source_agent)
|
|
720
706
|
return 'unknown' if source_agent.nil? || source_agent == 'unknown'
|
|
721
707
|
|
|
722
708
|
provider = source_agent.to_s.split(/[-_]/).first.downcase
|
|
723
709
|
%w[claude openai gemini human system].include?(provider) ? provider : 'unknown'
|
|
724
710
|
end
|
|
725
711
|
|
|
726
|
-
def
|
|
712
|
+
def knowledge_upsert_expertise(source_agent:, domain:)
|
|
727
713
|
log.debug("Apollo Knowledge.upsert_expertise source_agent=#{source_agent} domain=#{domain}")
|
|
728
|
-
expertise =
|
|
729
|
-
|
|
714
|
+
expertise = data_model_expertise
|
|
715
|
+
.where(agent_id: source_agent, domain: domain).first
|
|
730
716
|
if expertise
|
|
731
717
|
expertise.update(entry_count: expertise.entry_count + 1, last_active_at: Time.now)
|
|
732
718
|
else
|
|
733
|
-
|
|
719
|
+
data_model_expertise.create(
|
|
734
720
|
agent_id: source_agent, domain: domain,
|
|
735
721
|
proficiency: settings[:expertise][:initial_proficiency],
|
|
736
722
|
entry_count: 1, last_active_at: Time.now
|
|
@@ -738,7 +724,12 @@ module Legion
|
|
|
738
724
|
end
|
|
739
725
|
end
|
|
740
726
|
|
|
741
|
-
def
|
|
727
|
+
def knowledge_resolve_process_identity(principal_id: UNSET, identity_id: UNSET, canonical_name: UNSET, **)
|
|
728
|
+
unless principal_id.equal?(UNSET)
|
|
729
|
+
return { principal_id: principal_id,
|
|
730
|
+
identity_id: identity_id.equal?(UNSET) ? nil : identity_id,
|
|
731
|
+
canonical_name: canonical_name.equal?(UNSET) ? nil : canonical_name }
|
|
732
|
+
end
|
|
742
733
|
return { principal_id: nil, identity_id: nil, canonical_name: nil } unless defined?(Legion::Identity::Process)
|
|
743
734
|
|
|
744
735
|
{
|
|
@@ -746,27 +737,14 @@ module Legion
|
|
|
746
737
|
identity_id: Legion::Identity::Process.db_identity_id,
|
|
747
738
|
canonical_name: Legion::Identity::Process.canonical_name
|
|
748
739
|
}
|
|
749
|
-
rescue StandardError => e
|
|
750
|
-
handle_exception(e, level: :warn, operation: 'apollo.knowledge.resolve_process_identity')
|
|
751
|
-
{ principal_id: nil, identity_id: nil, canonical_name: nil }
|
|
752
740
|
end
|
|
753
741
|
|
|
754
|
-
def
|
|
742
|
+
def knowledge_resolve_requesting_principal_id(explicit_value)
|
|
755
743
|
return explicit_value if explicit_value
|
|
756
744
|
return nil unless defined?(Legion::Identity::Process)
|
|
757
745
|
|
|
758
746
|
Legion::Identity::Process.db_principal_id
|
|
759
|
-
rescue StandardError => e
|
|
760
|
-
handle_exception(e, level: :warn, operation: 'apollo.knowledge.resolve_requesting_principal_id')
|
|
761
|
-
nil
|
|
762
747
|
end
|
|
763
|
-
|
|
764
|
-
include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
|
|
765
|
-
include Legion::JSON::Helper
|
|
766
|
-
include Legion::Settings::Helper
|
|
767
|
-
extend Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
|
|
768
|
-
extend Legion::JSON::Helper
|
|
769
|
-
extend Legion::Settings::Helper
|
|
770
748
|
end
|
|
771
749
|
end
|
|
772
750
|
end
|