lex-apollo 0.4.44 → 0.4.49
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 +54 -0
- data/README.md +76 -14
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -41
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +16 -16
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +96 -41
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -51
- data/lib/legion/extensions/apollo/helpers/confidence.rb +8 -52
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +99 -43
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +64 -102
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +146 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +120 -142
- data/lib/legion/extensions/apollo/runners/knowledge.rb +276 -346
- data/lib/legion/extensions/apollo/runners/maintenance.rb +154 -65
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -46
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/transport.rb +8 -5
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +28 -24
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +9 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +240 -8
- data/spec/legion/extensions/apollo/contradiction_spec.rb +28 -50
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +16 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +24 -27
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +108 -0
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +73 -74
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +35 -6
- 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 +205 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +14 -91
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +28 -11
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +87 -77
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +14 -89
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +141 -9
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +314 -655
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +14 -5
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +48 -64
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +120 -98
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/legion/extensions/apollo/transport_spec.rb +33 -0
- data/spec/spec_helper.rb +42 -13
- metadata +41 -13
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/contradiction_scanner_spec.rb +0 -38
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/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
|
|
@@ -7,7 +7,7 @@ module Legion
|
|
|
7
7
|
extend Legion::Extensions::Transport
|
|
8
8
|
|
|
9
9
|
def self.additional_e_to_q
|
|
10
|
-
[
|
|
10
|
+
bindings = [
|
|
11
11
|
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
12
12
|
to: Legion::Extensions::Apollo::Transport::Queues::Ingest,
|
|
13
13
|
routing_key: 'legion.apollo.ingest' },
|
|
@@ -19,11 +19,14 @@ module Legion
|
|
|
19
19
|
routing_key: 'legion.apollo.writeback.store' },
|
|
20
20
|
{ from: Legion::Extensions::Apollo::Transport::Exchanges::Apollo,
|
|
21
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' }
|
|
22
|
+
routing_key: 'legion.apollo.writeback.vectorize' }
|
|
26
23
|
]
|
|
24
|
+
if Legion::Extensions::Apollo.settings[:gas][:enabled] == true
|
|
25
|
+
bindings << { from: Legion::Extensions::Apollo::Transport::Exchanges::LlmAudit,
|
|
26
|
+
to: Legion::Extensions::Apollo::Transport::Queues::GasSubscriber,
|
|
27
|
+
routing_key: 'llm.audit.complete' }
|
|
28
|
+
end
|
|
29
|
+
bindings
|
|
27
30
|
end
|
|
28
31
|
end
|
|
29
32
|
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,
|
|
@@ -72,6 +78,7 @@ module Legion
|
|
|
72
78
|
mesh_export_limit: 100
|
|
73
79
|
},
|
|
74
80
|
gas: {
|
|
81
|
+
enabled: false,
|
|
75
82
|
relate_confidence_gate: 0.7,
|
|
76
83
|
synthesis_confidence_cap: 0.7,
|
|
77
84
|
max_anticipations: 3,
|
|
@@ -85,7 +92,8 @@ module Legion
|
|
|
85
92
|
enabled: false,
|
|
86
93
|
similar_limit: 10,
|
|
87
94
|
similarity_threshold: 0.7,
|
|
88
|
-
relation_weight: 0.8
|
|
95
|
+
relation_weight: 0.8,
|
|
96
|
+
max_content_chars: 4000
|
|
89
97
|
},
|
|
90
98
|
corroboration: {
|
|
91
99
|
relation_weight: 1.0,
|
|
@@ -98,9 +106,11 @@ module Legion
|
|
|
98
106
|
proficiency_cap: 1.0
|
|
99
107
|
},
|
|
100
108
|
entity_extractor: {
|
|
101
|
-
min_confidence: 0.7
|
|
109
|
+
min_confidence: 0.7,
|
|
110
|
+
types: %w[person service repository concept]
|
|
102
111
|
},
|
|
103
112
|
entity_watchdog: {
|
|
113
|
+
enabled: false,
|
|
104
114
|
concept_keywords: [],
|
|
105
115
|
types: %w[person service repository concept],
|
|
106
116
|
detect_confidence: 0.5,
|
|
@@ -115,16 +125,13 @@ module Legion
|
|
|
115
125
|
'clinical_care' => %w[clinical_care general],
|
|
116
126
|
'general' => :all
|
|
117
127
|
},
|
|
118
|
-
embedding: {
|
|
119
|
-
provider: nil,
|
|
120
|
-
model: nil
|
|
121
|
-
},
|
|
122
128
|
data: {
|
|
123
129
|
apollo_write: false
|
|
124
130
|
},
|
|
125
131
|
writeback: {
|
|
126
132
|
enabled: true,
|
|
127
|
-
min_content_length: 50
|
|
133
|
+
min_content_length: 50,
|
|
134
|
+
max_content_length: 4000
|
|
128
135
|
},
|
|
129
136
|
actors: {
|
|
130
137
|
decay_interval: 3600,
|
|
@@ -138,6 +145,3 @@ module Legion
|
|
|
138
145
|
end
|
|
139
146
|
end
|
|
140
147
|
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,38 +2,23 @@
|
|
|
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
6
|
require 'legion/extensions/apollo/actors/gas_subscriber'
|
|
17
7
|
|
|
18
8
|
RSpec.describe Legion::Extensions::Apollo::Actor::GasSubscriber do
|
|
19
|
-
subject(:actor) { described_class.
|
|
9
|
+
subject(:actor) { described_class.allocate }
|
|
10
|
+
|
|
11
|
+
before { allow(actor).to receive(:settings).and_return(gas: { enabled: false }) }
|
|
20
12
|
|
|
21
|
-
it '
|
|
22
|
-
|
|
13
|
+
it 'does not prepare a consumer when GAS is disabled' do
|
|
14
|
+
expect(actor).not_to receive(:create_dedicated_channel)
|
|
23
15
|
|
|
24
|
-
expect(
|
|
25
|
-
expect(result).not_to have_key(:error)
|
|
16
|
+
expect(actor.prepare).to be_nil
|
|
26
17
|
end
|
|
27
18
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
stub_const('Legion::Transport', Module.new)
|
|
31
|
-
expect(actor.enabled?).to be_truthy
|
|
32
|
-
end
|
|
19
|
+
it 'does not activate a consumer when GAS is disabled' do
|
|
20
|
+
expect(actor).not_to receive(:subscribe)
|
|
33
21
|
|
|
34
|
-
|
|
35
|
-
hide_const('Legion::Transport') if defined?(Legion::Transport)
|
|
36
|
-
expect(actor.enabled?).to be_falsey
|
|
37
|
-
end
|
|
22
|
+
expect(actor.activate).to be_nil
|
|
38
23
|
end
|
|
39
24
|
end
|
|
@@ -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
|