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,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/settings/helper'
|
|
4
|
+
require 'legion/logging/helper'
|
|
5
|
+
require 'legion/extensions/apollo/helpers/embedding'
|
|
6
|
+
require 'legion/extensions/apollo/helpers/graph_query'
|
|
7
|
+
require 'legion/extensions/apollo/helpers/similarity'
|
|
8
|
+
require 'legion/extensions/apollo/helpers/storage'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Apollo
|
|
13
|
+
module Runners
|
|
14
|
+
module Search
|
|
15
|
+
include Legion::Settings::Helper
|
|
16
|
+
include Legion::Logging::Helper
|
|
17
|
+
extend Legion::Extensions::Apollo::Helpers::Embedding
|
|
18
|
+
extend Legion::Extensions::Apollo::Helpers::GraphQuery
|
|
19
|
+
extend Legion::Extensions::Apollo::Helpers::Similarity
|
|
20
|
+
extend Legion::Extensions::Apollo::Helpers::Storage
|
|
21
|
+
extend self
|
|
22
|
+
|
|
23
|
+
def semantic_search(db:, embedding:, provenance:, limit: settings[:query][:default_limit], **filters)
|
|
24
|
+
provenance = embedding_parse_provenance(value: provenance)
|
|
25
|
+
unless embedding_valid?(vector: embedding, provenance: provenance)
|
|
26
|
+
raise ArgumentError, 'Apollo semantic search requires a valid embedding and provenance'
|
|
27
|
+
end
|
|
28
|
+
raise ArgumentError, "unsupported Apollo adapter: #{db.database_type}" unless %i[postgres sqlite].include?(db.database_type)
|
|
29
|
+
raise ArgumentError, 'Apollo search limit must be a positive integer' unless limit.is_a?(Integer) && limit.positive?
|
|
30
|
+
|
|
31
|
+
options = {
|
|
32
|
+
adapter: db.database_type, limit: limit,
|
|
33
|
+
min_confidence: filters.fetch(:min_confidence) { settings[:query][:default_min_confidence] },
|
|
34
|
+
statuses: filters.fetch(:statuses) { %w[confirmed candidate] },
|
|
35
|
+
tags: filters[:tags], tag_match: filters.fetch(:tag_match, :any), domain: filters[:domain],
|
|
36
|
+
exclude_source_agents: filters.fetch(:exclude_source_agents) { [] },
|
|
37
|
+
requesting_principal_id: filters[:requesting_principal_id],
|
|
38
|
+
access_scope: filters[:access_scope]&.to_s, identity_principal_id: filters[:identity_principal_id],
|
|
39
|
+
embedding_space_id: provenance[:space_id], embedding_dimensions: provenance[:dimensions],
|
|
40
|
+
embedding_identity_source: provenance[:identity_source]
|
|
41
|
+
}
|
|
42
|
+
sql = graph_build_semantic_search_sql(**options)
|
|
43
|
+
%i[statuses tags exclude_source_agents].each do |key|
|
|
44
|
+
options[key] = storage_serialize_tags(db: db, tags: options[key])
|
|
45
|
+
end
|
|
46
|
+
comparison = provenance.reject { |key, _| key == :task || (key == :instance && provenance[:identity_source] == 'revision') }
|
|
47
|
+
options[:embedding_provenance] = storage_serialize_provenance(db: db, provenance: comparison)
|
|
48
|
+
options[:embedding] = storage_serialize_embedding(db: db, vector: embedding)
|
|
49
|
+
candidates = db.fetch(sql, options)
|
|
50
|
+
entries = if db.database_type == :postgres
|
|
51
|
+
candidates.all.map { |row| row.merge(tags: storage_deserialize_tags(tags: row[:tags])) }
|
|
52
|
+
else
|
|
53
|
+
search_sqlite_top_matches(candidates: candidates, embedding: embedding, provenance: provenance, limit: limit)
|
|
54
|
+
end
|
|
55
|
+
{ success: true, entries: entries, count: entries.size }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def search_sqlite_top_matches(candidates:, embedding:, provenance:, limit:)
|
|
61
|
+
matches = []
|
|
62
|
+
candidates.each do |row|
|
|
63
|
+
stored_provenance = embedding_parse_provenance(value: row[:embedding_provenance])
|
|
64
|
+
next unless embedding_compatible?(left: provenance, right: stored_provenance)
|
|
65
|
+
|
|
66
|
+
vector = similarity_parse_vector(vector: row[:embedding])
|
|
67
|
+
next unless embedding_valid?(vector: vector, provenance: stored_provenance, task: :document)
|
|
68
|
+
|
|
69
|
+
similarity = similarity_cosine(vec_a: embedding, vec_b: vector)
|
|
70
|
+
row.delete(:embedding)
|
|
71
|
+
row.delete(:embedding_provenance)
|
|
72
|
+
row[:distance] = 1.0 - similarity.clamp(-1.0, 1.0)
|
|
73
|
+
row[:tags] = storage_deserialize_tags(tags: row[:tags])
|
|
74
|
+
index = matches.bsearch_index do |match|
|
|
75
|
+
([match[:distance], match[:id]] <=> [row[:distance], row[:id]]) >= 0
|
|
76
|
+
end || matches.length
|
|
77
|
+
next if index >= limit
|
|
78
|
+
|
|
79
|
+
matches.insert(index, row)
|
|
80
|
+
matches.pop if matches.length > limit
|
|
81
|
+
end
|
|
82
|
+
matches
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/extensions/apollo/helpers/capability'
|
|
5
|
+
require 'legion/extensions/apollo/runners/embedding'
|
|
6
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
7
|
+
require 'legion/extensions/apollo/transport/messages/writeback'
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Extensions
|
|
11
|
+
module Apollo
|
|
12
|
+
module Runners
|
|
13
|
+
module Writeback
|
|
14
|
+
include Legion::Logging::Helper
|
|
15
|
+
extend Legion::Extensions::Apollo::Helpers::Capability
|
|
16
|
+
|
|
17
|
+
extend self
|
|
18
|
+
|
|
19
|
+
def handle_vectorize(content:, embedding: nil, embedding_provenance: nil, identity_principal_id: nil,
|
|
20
|
+
identity_id: nil, identity_canonical_name: nil, **payload)
|
|
21
|
+
generated = Legion::Extensions::Apollo::Runners::Embedding.document(
|
|
22
|
+
text: content, vector: embedding, provenance: embedding_provenance
|
|
23
|
+
)
|
|
24
|
+
writeback_deliver(
|
|
25
|
+
payload: payload.merge(
|
|
26
|
+
content: content, identity_principal_id: identity_principal_id,
|
|
27
|
+
identity_id: identity_id, identity_canonical_name: identity_canonical_name,
|
|
28
|
+
embedding: generated[:vector], embedding_provenance: generated[:provenance]
|
|
29
|
+
),
|
|
30
|
+
has_embedding: true
|
|
31
|
+
)
|
|
32
|
+
{ success: true, action: :vectorized }
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
handle_exception(e, level: :warn, operation: 'apollo.writeback.handle_vectorize')
|
|
35
|
+
raise
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def writeback_deliver(payload:, has_embedding:, **)
|
|
39
|
+
can_write = has_embedding && capability_can_write?
|
|
40
|
+
result = if can_write
|
|
41
|
+
Legion::Extensions::Apollo::Runners::Knowledge.handle_ingest(**payload)
|
|
42
|
+
else
|
|
43
|
+
publication = payload.except(:routing_key, :exchange)
|
|
44
|
+
Legion::Extensions::Apollo::Transport::Messages::Writeback.new(
|
|
45
|
+
**publication, has_embedding: has_embedding
|
|
46
|
+
).publish!
|
|
47
|
+
end
|
|
48
|
+
accepted = can_write ? result.is_a?(Hash) && result[:success] == true : result.is_a?(Hash) && result[:accepted] == true
|
|
49
|
+
raise IOError, "Apollo writeback delivery failed: #{result.inspect}" unless accepted
|
|
50
|
+
|
|
51
|
+
{ success: true, result: result }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -9,21 +9,25 @@ module Legion
|
|
|
9
9
|
module Messages
|
|
10
10
|
class Ingest < Legion::Transport::Message
|
|
11
11
|
def exchange
|
|
12
|
-
Exchanges::Apollo
|
|
12
|
+
Legion::Extensions::Apollo::Transport::Exchanges::Apollo
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def routing_key
|
|
16
16
|
'legion.apollo.ingest'
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
def message
|
|
20
|
-
{
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
def message(**)
|
|
20
|
+
{ context: {} }.merge(
|
|
21
|
+
@options.slice(:content, :content_type, :tags, :source_agent, :source_provider,
|
|
22
|
+
:source_channel, :knowledge_domain, :submitted_by, :submitted_from,
|
|
23
|
+
:content_hash, :context, :access_scope, :identity_principal_id,
|
|
24
|
+
:identity_id, :identity_canonical_name, :embedding, :embedding_provenance,
|
|
25
|
+
:source_model, :model, :provider, :instance, :dimensions, :reply_to, :correlation_id)
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def publish_envelope_options(options:, **)
|
|
30
|
+
super(options: options.except(:content_type))
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
def type
|
|
@@ -9,7 +9,7 @@ module Legion
|
|
|
9
9
|
module Messages
|
|
10
10
|
class Query < Legion::Transport::Message
|
|
11
11
|
def exchange
|
|
12
|
-
Exchanges::Apollo
|
|
12
|
+
Legion::Extensions::Apollo::Transport::Exchanges::Apollo
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def routing_key
|
|
@@ -17,19 +17,9 @@ module Legion
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def message
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
entry_id: @options[:entry_id],
|
|
24
|
-
limit: @options[:limit],
|
|
25
|
-
min_confidence: @options[:min_confidence],
|
|
26
|
-
status: @options[:status],
|
|
27
|
-
tags: @options[:tags],
|
|
28
|
-
relation_types: @options[:relation_types],
|
|
29
|
-
depth: @options[:depth],
|
|
30
|
-
reply_to: @options[:reply_to],
|
|
31
|
-
correlation_id: @options[:correlation_id]
|
|
32
|
-
}.compact
|
|
20
|
+
@options.slice(:action, :query, :entry_id, :limit, :min_confidence, :status, :tags,
|
|
21
|
+
:domain, :agent_id, :requesting_principal_id, :tag_match, :exclude_source_agents,
|
|
22
|
+
:relation_types, :depth, :reply_to, :correlation_id)
|
|
33
23
|
end
|
|
34
24
|
|
|
35
25
|
def type
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/transport/message'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'timeout'
|
|
4
6
|
|
|
5
7
|
module Legion
|
|
6
8
|
module Extensions
|
|
@@ -9,7 +11,7 @@ module Legion
|
|
|
9
11
|
module Messages
|
|
10
12
|
class Writeback < Legion::Transport::Message
|
|
11
13
|
def exchange
|
|
12
|
-
Exchanges::Apollo
|
|
14
|
+
Legion::Extensions::Apollo::Transport::Exchanges::Apollo
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
def routing_key
|
|
@@ -20,19 +22,39 @@ module Legion
|
|
|
20
22
|
'apollo_writeback'
|
|
21
23
|
end
|
|
22
24
|
|
|
23
|
-
def message
|
|
24
|
-
{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
25
|
+
def message(**)
|
|
26
|
+
{ context: {} }.merge(
|
|
27
|
+
@options.slice(:content, :content_type, :tags, :source_agent, :source_provider,
|
|
28
|
+
:source_channel, :knowledge_domain, :submitted_by, :submitted_from,
|
|
29
|
+
:content_hash, :context, :access_scope, :identity_principal_id,
|
|
30
|
+
:identity_id, :identity_canonical_name, :embedding, :embedding_provenance,
|
|
31
|
+
:source_model, :model, :provider, :instance, :dimensions, :reply_to, :correlation_id)
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def publish_envelope_options(options:, **)
|
|
36
|
+
super(options: options.except(:content_type))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def publish(options = nil, **)
|
|
40
|
+
raise ArgumentError, 'Invalid Apollo writeback message' unless @valid
|
|
41
|
+
|
|
42
|
+
publication = options || {}
|
|
43
|
+
timeout_ms = publication.fetch(:publish_confirm_timeout_ms) { @options.fetch(:publish_confirm_timeout_ms, 5000) }
|
|
44
|
+
unless timeout_ms.is_a?(Numeric) && timeout_ms.real? && timeout_ms.finite? && timeout_ms.positive?
|
|
45
|
+
raise ArgumentError, 'publish_confirm_timeout_ms must be finite and positive'
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
result = super(publication.merge(message_id: SecureRandom.uuid, mandatory: true,
|
|
49
|
+
publisher_confirm: true, spool: false, return_result: true))
|
|
50
|
+
return result if result[:accepted] == true
|
|
51
|
+
|
|
52
|
+
raise Timeout::Error, 'Apollo writeback confirmation timed out' if result[:status] == :confirm_timeout
|
|
53
|
+
|
|
54
|
+
raise IOError, result[:error] || "Apollo writeback publication was not accepted: #{result[:status]}"
|
|
55
|
+
rescue StandardError => e
|
|
56
|
+
handle_exception(e, level: :warn, operation: 'apollo.writeback.publish')
|
|
57
|
+
raise
|
|
36
58
|
end
|
|
37
59
|
|
|
38
60
|
def validate
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Apollo
|
|
6
|
+
module Transport
|
|
7
|
+
module SubscriptionDelivery
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def dispatch_runner(message, runner_cls, function, _check_subtask, _generate_task)
|
|
11
|
+
raise IOError, 'Apollo is not accepting new work' unless extension_dispatch_allowed?
|
|
12
|
+
|
|
13
|
+
result = runner_cls.public_send(function, **message)
|
|
14
|
+
raise IOError, "Apollo delivery failed: #{result.inspect}" unless result.is_a?(Hash) && result[:success] != false && !result[:error]
|
|
15
|
+
|
|
16
|
+
result
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def reject_or_retry(delivery_info, metadata, payload)
|
|
20
|
+
retry_count = Legion::Extensions::Actors::RetryPolicy.extract_retry_count(headers: metadata&.headers)
|
|
21
|
+
threshold = Legion::Extensions::Actors::RetryPolicy.retry_threshold
|
|
22
|
+
unless Legion::Extensions::Actors::RetryPolicy.should_retry?(retry_count: retry_count, threshold: threshold)
|
|
23
|
+
log.warn("Apollo delivery exhausted #{retry_count} retries")
|
|
24
|
+
return @queue.reject(delivery_info.delivery_tag, requeue: false)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
base_delay = Legion::Settings.dig(:fleet, :transport, :retry_base_delay_seconds) || 1
|
|
28
|
+
max_delay = Legion::Settings.dig(:fleet, :transport, :retry_max_delay_seconds) || 30
|
|
29
|
+
sleep([base_delay * (2**retry_count), max_delay].min)
|
|
30
|
+
if republish_with_retry_count(delivery_info, metadata, payload, retry_count + 1)
|
|
31
|
+
@queue.acknowledge(delivery_info.delivery_tag)
|
|
32
|
+
else
|
|
33
|
+
@queue.reject(delivery_info.delivery_tag, requeue: false)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -5,6 +5,26 @@ module Legion
|
|
|
5
5
|
module Apollo
|
|
6
6
|
module Transport
|
|
7
7
|
extend Legion::Extensions::Transport
|
|
8
|
+
|
|
9
|
+
def self.additional_e_to_q
|
|
10
|
+
[
|
|
11
|
+
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
12
|
+
to: Legion::Extensions::Apollo::Transport::Queues::Ingest,
|
|
13
|
+
routing_key: 'legion.apollo.ingest' },
|
|
14
|
+
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
15
|
+
to: Legion::Extensions::Apollo::Transport::Queues::QueryResponder,
|
|
16
|
+
routing_key: 'legion.apollo.query' },
|
|
17
|
+
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
18
|
+
to: Legion::Extensions::Apollo::Transport::Queues::WritebackStore,
|
|
19
|
+
routing_key: 'legion.apollo.writeback.store' },
|
|
20
|
+
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
21
|
+
to: Legion::Extensions::Apollo::Transport::Queues::WritebackVectorize,
|
|
22
|
+
routing_key: 'legion.apollo.writeback.vectorize' },
|
|
23
|
+
{ from: Legion::Extensions::Apollo::Transport::Exchanges::LlmAudit,
|
|
24
|
+
to: Legion::Extensions::Apollo::Transport::Queues::GasSubscriber,
|
|
25
|
+
routing_key: 'llm.audit.complete' }
|
|
26
|
+
]
|
|
27
|
+
end
|
|
8
28
|
end
|
|
9
29
|
end
|
|
10
30
|
end
|
|
@@ -3,35 +3,41 @@
|
|
|
3
3
|
require 'legion/logging'
|
|
4
4
|
require 'legion/settings'
|
|
5
5
|
require 'legion/json'
|
|
6
|
+
require 'legion/transport'
|
|
6
7
|
require 'legion/extensions/apollo/version'
|
|
7
8
|
require 'legion/extensions/apollo/helpers/confidence'
|
|
8
9
|
require 'legion/extensions/apollo/helpers/similarity'
|
|
10
|
+
require 'legion/extensions/apollo/helpers/embedding'
|
|
11
|
+
require 'legion/extensions/apollo/helpers/storage'
|
|
9
12
|
require 'legion/extensions/apollo/helpers/graph_query'
|
|
10
13
|
require 'legion/extensions/apollo/helpers/tag_normalizer'
|
|
11
14
|
require 'legion/extensions/apollo/helpers/capability'
|
|
12
15
|
require 'legion/extensions/apollo/helpers/writeback'
|
|
13
16
|
require 'legion/extensions/apollo/helpers/data_models'
|
|
14
17
|
require 'legion/extensions/apollo/runners/knowledge'
|
|
18
|
+
require 'legion/extensions/apollo/runners/writeback'
|
|
15
19
|
require 'legion/extensions/apollo/runners/expertise'
|
|
16
20
|
require 'legion/extensions/apollo/runners/maintenance'
|
|
17
21
|
require 'legion/extensions/apollo/runners/entity_extractor'
|
|
22
|
+
require 'legion/extensions/apollo/runners/embedding'
|
|
23
|
+
require 'legion/extensions/apollo/runners/search'
|
|
24
|
+
require 'legion/extensions/apollo/runners/entity_watchdog'
|
|
25
|
+
require 'legion/extensions/apollo/runners/contradictions'
|
|
18
26
|
require 'legion/extensions/apollo/runners/gas'
|
|
19
27
|
require 'legion/extensions/apollo/runners/request'
|
|
20
|
-
|
|
28
|
+
require 'legion/extensions/apollo/runners/query_response'
|
|
21
29
|
require 'legion/extensions/apollo/api' if defined?(Sinatra)
|
|
22
30
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
require 'legion/extensions/apollo/transport/queues/writeback_vectorize'
|
|
34
|
-
end
|
|
31
|
+
require 'legion/extensions/apollo/transport/exchanges/apollo'
|
|
32
|
+
require 'legion/extensions/apollo/transport/exchanges/llm_audit'
|
|
33
|
+
require 'legion/extensions/apollo/transport/queues/ingest'
|
|
34
|
+
require 'legion/extensions/apollo/transport/queues/query'
|
|
35
|
+
require 'legion/extensions/apollo/transport/queues/gas'
|
|
36
|
+
require 'legion/extensions/apollo/transport/messages/ingest'
|
|
37
|
+
require 'legion/extensions/apollo/transport/messages/query'
|
|
38
|
+
require 'legion/extensions/apollo/transport/messages/writeback'
|
|
39
|
+
require 'legion/extensions/apollo/transport/queues/writeback_store'
|
|
40
|
+
require 'legion/extensions/apollo/transport/queues/writeback_vectorize'
|
|
35
41
|
|
|
36
42
|
module Legion
|
|
37
43
|
module Extensions
|
|
@@ -44,7 +50,7 @@ module Legion
|
|
|
44
50
|
false
|
|
45
51
|
end
|
|
46
52
|
|
|
47
|
-
def self.default_settings # rubocop:disable Metrics/MethodLength
|
|
53
|
+
def self.default_settings(**) # rubocop:disable Metrics/MethodLength
|
|
48
54
|
{
|
|
49
55
|
confidence: {
|
|
50
56
|
initial: 0.5,
|
|
@@ -85,7 +91,8 @@ module Legion
|
|
|
85
91
|
enabled: false,
|
|
86
92
|
similar_limit: 10,
|
|
87
93
|
similarity_threshold: 0.7,
|
|
88
|
-
relation_weight: 0.8
|
|
94
|
+
relation_weight: 0.8,
|
|
95
|
+
max_content_chars: 4000
|
|
89
96
|
},
|
|
90
97
|
corroboration: {
|
|
91
98
|
relation_weight: 1.0,
|
|
@@ -98,7 +105,8 @@ module Legion
|
|
|
98
105
|
proficiency_cap: 1.0
|
|
99
106
|
},
|
|
100
107
|
entity_extractor: {
|
|
101
|
-
min_confidence: 0.7
|
|
108
|
+
min_confidence: 0.7,
|
|
109
|
+
types: %w[person service repository concept]
|
|
102
110
|
},
|
|
103
111
|
entity_watchdog: {
|
|
104
112
|
concept_keywords: [],
|
|
@@ -115,16 +123,13 @@ module Legion
|
|
|
115
123
|
'clinical_care' => %w[clinical_care general],
|
|
116
124
|
'general' => :all
|
|
117
125
|
},
|
|
118
|
-
embedding: {
|
|
119
|
-
provider: nil,
|
|
120
|
-
model: nil
|
|
121
|
-
},
|
|
122
126
|
data: {
|
|
123
127
|
apollo_write: false
|
|
124
128
|
},
|
|
125
129
|
writeback: {
|
|
126
130
|
enabled: true,
|
|
127
|
-
min_content_length: 50
|
|
131
|
+
min_content_length: 50,
|
|
132
|
+
max_content_length: 4000
|
|
128
133
|
},
|
|
129
134
|
actors: {
|
|
130
135
|
decay_interval: 3600,
|
|
@@ -138,6 +143,3 @@ module Legion
|
|
|
138
143
|
end
|
|
139
144
|
end
|
|
140
145
|
end
|
|
141
|
-
|
|
142
|
-
# Entity watchdog runs as Actor::EntityWatchdog (Every actor, 120s interval).
|
|
143
|
-
# PhaseWiring.register_handler was removed — the watchdog scans task logs independently.
|
|
@@ -2,32 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
|
-
unless defined?(Legion::Extensions::Actors::Subscription)
|
|
6
|
-
module Legion
|
|
7
|
-
module Extensions
|
|
8
|
-
module Actors
|
|
9
|
-
class Subscription; end # rubocop:disable Lint/EmptyClass
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
5
|
$LOADED_FEATURES << 'legion/extensions/actors/subscription' unless $LOADED_FEATURES.include?('legion/extensions/actors/subscription')
|
|
15
|
-
|
|
16
|
-
require 'legion/extensions/apollo/runners/knowledge'
|
|
17
6
|
require 'legion/extensions/apollo/actors/ingest'
|
|
7
|
+
require 'legion/extensions/apollo/actors/writeback_store'
|
|
18
8
|
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
[Legion::Extensions::Apollo::Actor::Ingest, Legion::Extensions::Apollo::Actor::WritebackStore].each do |actor_class|
|
|
10
|
+
RSpec.describe actor_class do
|
|
11
|
+
subject(:actor) { described_class.new }
|
|
21
12
|
|
|
22
|
-
|
|
23
|
-
expect(actor.runner_class).to eq('Legion::Extensions::Apollo::Runners::Knowledge')
|
|
24
|
-
end
|
|
13
|
+
let(:payload) { { content: 'remote fact', content_type: 'fact' } }
|
|
25
14
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
it 'uses explicit null ownership for an unidentified producer rather than the service process identity' do
|
|
16
|
+
allow_any_instance_of(described_class.superclass).to receive(:process_message).and_return(payload)
|
|
17
|
+
|
|
18
|
+
expect(actor.process_message('payload', nil, nil)).to eq(
|
|
19
|
+
**payload, identity_principal_id: nil, identity_id: nil, identity_canonical_name: nil
|
|
20
|
+
)
|
|
21
|
+
end
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
it 'preserves producer identity and explicit null identity fields after superclass decoding' do
|
|
24
|
+
identity = { identity_principal_id: 31, identity_id: nil, identity_canonical_name: 'producer@example.test' }
|
|
25
|
+
allow_any_instance_of(described_class.superclass).to receive(:process_message).and_return(payload.merge(identity))
|
|
26
|
+
|
|
27
|
+
expect(actor.process_message('payload', nil, nil)).to include(identity)
|
|
28
|
+
end
|
|
32
29
|
end
|
|
33
30
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
$LOADED_FEATURES << 'legion/extensions/actors/subscription' unless $LOADED_FEATURES.include?('legion/extensions/actors/subscription')
|
|
6
|
+
require 'legion/extensions/apollo/actors/query_responder'
|
|
7
|
+
|
|
8
|
+
RSpec.describe Legion::Extensions::Apollo::Actor::QueryResponder do
|
|
9
|
+
subject(:actor) { described_class.new }
|
|
10
|
+
|
|
11
|
+
let(:payload) { Legion::JSON.dump({ action: 'retrieve', query: '' }) }
|
|
12
|
+
let(:delivery) { { routing_key: 'legion.apollo.query' } }
|
|
13
|
+
|
|
14
|
+
it 'preserves superclass decoding and takes reply routing from AMQP properties over body values' do
|
|
15
|
+
metadata = double('properties', reply_to: 'envelope-reply', correlation_id: 'envelope-correlation')
|
|
16
|
+
decoded = { action: 'retrieve', query: '', routing_key: 'legion.apollo.query', reply_to: 'body-reply', correlation_id: 'body-correlation' }
|
|
17
|
+
allow_any_instance_of(described_class.superclass).to receive(:process_message).with(payload, metadata, delivery).and_return(decoded)
|
|
18
|
+
|
|
19
|
+
expect(actor.process_message(payload, metadata, delivery)).to eq(
|
|
20
|
+
action: 'retrieve', query: '', routing_key: 'legion.apollo.query', reply_to: 'envelope-reply', correlation_id: 'envelope-correlation'
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'retains payload reply metadata when a broker producer did not supply envelope properties' do
|
|
25
|
+
metadata = double('properties', reply_to: nil, correlation_id: nil)
|
|
26
|
+
decoded = { action: 'retrieve', query: '', reply_to: 'body-reply', correlation_id: 'body-correlation' }
|
|
27
|
+
allow_any_instance_of(described_class.superclass).to receive(:process_message).and_return(decoded)
|
|
28
|
+
|
|
29
|
+
expect(actor.process_message(payload, metadata, delivery)).to include(reply_to: 'body-reply', correlation_id: 'body-correlation')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'does not add reply fields to fire-and-forget requests' do
|
|
33
|
+
metadata = double('properties', reply_to: nil, correlation_id: nil)
|
|
34
|
+
allow_any_instance_of(described_class.superclass).to receive(:process_message).and_return(action: 'retrieve', query: '')
|
|
35
|
+
|
|
36
|
+
request = actor.process_message(payload, metadata, delivery)
|
|
37
|
+
expect(request).not_to have_key(:reply_to)
|
|
38
|
+
expect(request).not_to have_key(:correlation_id)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'retains the RPC envelope and opaque payload when publishing a retry' do
|
|
42
|
+
stub_const('Legion::Extensions::Actors::RetryPolicy::RETRY_COUNT_HEADER', 'x-retry-count')
|
|
43
|
+
exchange = double('default exchange')
|
|
44
|
+
queue = double('query queue', name: 'legion.apollo.query', channel: double(default_exchange: exchange))
|
|
45
|
+
actor.instance_variable_set(:@queue, queue)
|
|
46
|
+
metadata = double('properties', reply_to: 'reply-queue', correlation_id: 'request-123',
|
|
47
|
+
content_type: 'application/json', content_encoding: 'encrypted/cs', headers: { 'iv' => 'cipher-iv' })
|
|
48
|
+
publications = []
|
|
49
|
+
allow(exchange).to receive(:publish) { |body, **envelope| publications << [body, envelope] }
|
|
50
|
+
|
|
51
|
+
actor.send(:republish_with_retry_count, delivery, metadata, 'opaque-encrypted-bytes', 1)
|
|
52
|
+
|
|
53
|
+
expect(publications).to eq([
|
|
54
|
+
['opaque-encrypted-bytes', { routing_key: 'legion.apollo.query', reply_to: 'reply-queue', correlation_id: 'request-123',
|
|
55
|
+
content_type: 'application/json', content_encoding: 'encrypted/cs', persistent: true,
|
|
56
|
+
headers: { 'iv' => 'cipher-iv', 'x-retry-count' => 1 } }]
|
|
57
|
+
])
|
|
58
|
+
end
|
|
59
|
+
end
|