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
|
@@ -4,128 +4,43 @@ require 'spec_helper'
|
|
|
4
4
|
require 'legion/extensions/apollo/helpers/confidence'
|
|
5
5
|
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::Helpers::Confidence do
|
|
7
|
-
|
|
8
|
-
it 'defines INITIAL_CONFIDENCE' do
|
|
9
|
-
expect(described_class::INITIAL_CONFIDENCE).to eq(0.5)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'defines CORROBORATION_BOOST' do
|
|
13
|
-
expect(described_class::CORROBORATION_BOOST).to eq(0.3)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'defines RETRIEVAL_BOOST' do
|
|
17
|
-
expect(described_class::RETRIEVAL_BOOST).to eq(0.02)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it 'defines POWER_LAW_ALPHA' do
|
|
21
|
-
expect(described_class::POWER_LAW_ALPHA).to eq(0.05)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it 'defines DECAY_THRESHOLD' do
|
|
25
|
-
expect(described_class::DECAY_THRESHOLD).to eq(0.05)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'defines DECAY_MIN_AGE_HOURS' do
|
|
29
|
-
expect(described_class::DECAY_MIN_AGE_HOURS).to eq(168)
|
|
30
|
-
end
|
|
7
|
+
let(:host) { Object.new.extend(described_class) }
|
|
31
8
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it 'defines WRITE_CONFIDENCE_GATE' do
|
|
37
|
-
expect(described_class::WRITE_CONFIDENCE_GATE).to eq(0.6)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'defines WRITE_NOVELTY_GATE' do
|
|
41
|
-
expect(described_class::WRITE_NOVELTY_GATE).to eq(0.3)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'defines STALE_DAYS' do
|
|
45
|
-
expect(described_class::STALE_DAYS).to eq(90)
|
|
46
|
-
end
|
|
9
|
+
before do
|
|
10
|
+
allow(host).to receive(:settings).and_return(Legion::Extensions::Apollo.default_settings)
|
|
47
11
|
end
|
|
48
12
|
|
|
49
|
-
describe '
|
|
50
|
-
it '
|
|
51
|
-
result =
|
|
52
|
-
|
|
53
|
-
expect(result).to be_within(0.0001).of(expected)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'skips decay when age_hours is below minimum age' do
|
|
57
|
-
result = described_class.apply_decay(confidence: 1.0, age_hours: 10)
|
|
58
|
-
expect(result).to eq(1.0)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it 'applies age-based power-law decay when age_hours exceeds minimum' do
|
|
62
|
-
result = described_class.apply_decay(confidence: 1.0, age_hours: 500)
|
|
63
|
-
expect(result).to be > 0.0
|
|
64
|
-
expect(result).to be < 1.0
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it 'clamps to 0.0 minimum' do
|
|
68
|
-
result = described_class.apply_decay(confidence: 0.001)
|
|
69
|
-
expect(result).to be >= 0.0
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it 'accepts a custom alpha' do
|
|
73
|
-
result = described_class.apply_decay(confidence: 1.0, alpha: 0.5)
|
|
74
|
-
expected = 1.0 / (1.0 + 0.5) # ~0.6667
|
|
75
|
-
expect(result).to be_within(0.0001).of(expected)
|
|
13
|
+
describe 'retrieval confidence boost' do
|
|
14
|
+
it 'adds the configured retrieval boost to confidence' do
|
|
15
|
+
result = host.send(:confidence_apply_retrieval_boost, confidence: 0.5)
|
|
16
|
+
expect(result).to eq(0.52)
|
|
76
17
|
end
|
|
77
|
-
end
|
|
78
18
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
expect(result).to eq(0.52)
|
|
19
|
+
it 'uses the configured retrieval weight' do
|
|
20
|
+
host.send(:settings)[:confidence][:retrieval_boost] = 0.1
|
|
21
|
+
expect(host.send(:confidence_apply_retrieval_boost, confidence: 0.5)).to eq(0.6)
|
|
83
22
|
end
|
|
84
23
|
|
|
85
24
|
it 'clamps to 1.0 maximum' do
|
|
86
|
-
result =
|
|
25
|
+
result = host.send(:confidence_apply_retrieval_boost, confidence: 0.99)
|
|
87
26
|
expect(result).to eq(1.0)
|
|
88
27
|
end
|
|
89
28
|
end
|
|
90
29
|
|
|
91
|
-
describe '
|
|
92
|
-
it 'adds
|
|
93
|
-
result =
|
|
30
|
+
describe 'corroboration confidence boost' do
|
|
31
|
+
it 'adds the configured corroboration boost to confidence' do
|
|
32
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.5)
|
|
94
33
|
expect(result).to eq(0.8)
|
|
95
34
|
end
|
|
96
35
|
|
|
97
36
|
it 'clamps to 1.0 maximum' do
|
|
98
|
-
result =
|
|
37
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.9)
|
|
99
38
|
expect(result).to eq(1.0)
|
|
100
39
|
end
|
|
101
40
|
|
|
102
41
|
it 'applies half weight for same-source corroboration' do
|
|
103
|
-
result =
|
|
42
|
+
result = host.send(:confidence_apply_corroboration_boost, confidence: 0.5, weight: 0.5)
|
|
104
43
|
expect(result).to eq(0.65)
|
|
105
44
|
end
|
|
106
45
|
end
|
|
107
|
-
|
|
108
|
-
describe '.decayed?' do
|
|
109
|
-
it 'returns true when confidence below threshold' do
|
|
110
|
-
expect(described_class.decayed?(confidence: 0.01)).to be true
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
it 'returns false when confidence at or above threshold' do
|
|
114
|
-
expect(described_class.decayed?(confidence: 0.05)).to be false
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
describe '.meets_write_gate?' do
|
|
119
|
-
it 'returns true when both gates met' do
|
|
120
|
-
expect(described_class.meets_write_gate?(confidence: 0.7, novelty: 0.4)).to be true
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it 'returns false when confidence below gate' do
|
|
124
|
-
expect(described_class.meets_write_gate?(confidence: 0.5, novelty: 0.4)).to be false
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
it 'returns false when novelty below gate' do
|
|
128
|
-
expect(described_class.meets_write_gate?(confidence: 0.7, novelty: 0.2)).to be false
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
46
|
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
require 'legion/extensions/apollo/helpers/embedding'
|
|
5
|
+
|
|
6
|
+
RSpec.describe Legion::Extensions::Apollo::Helpers::Embedding do
|
|
7
|
+
let(:host) { Object.new.extend(Legion::Logging::Helper).extend(described_class) }
|
|
8
|
+
|
|
9
|
+
let(:provenance) do
|
|
10
|
+
{ space_id: 'a' * 64, provider: 'test', model: 'embed', instance: 'local', model_revision: nil,
|
|
11
|
+
identity_source: 'instance', dimensions: 2, task: 'document', prefixes: { document: '', query: '' }, normalization: 'none' }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'allows document/query pairing only inside a known equal space and dimension' do
|
|
15
|
+
expect(host.send(:embedding_compatible?, left: provenance, right: provenance.merge(task: 'query'))).to be true
|
|
16
|
+
expect(host.send(:embedding_compatible?, left: provenance, right: provenance.merge(space_id: 'b' * 64))).to be false
|
|
17
|
+
expect(host.send(:embedding_compatible?, left: provenance, right: provenance.merge(dimensions: 3))).to be false
|
|
18
|
+
expect(host.send(:embedding_compatible?, left: nil, right: nil)).to be false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'rejects stale space IDs with different model metadata and isolates unknown-revision instances' do
|
|
22
|
+
expect(host.send(:embedding_compatible?, left: provenance, right: provenance.merge(model: 'different'))).to be false
|
|
23
|
+
expect(host.send(:embedding_compatible?, left: provenance, right: provenance.merge(instance: 'different'))).to be false
|
|
24
|
+
revision = provenance.merge(identity_source: 'revision', model_revision: 'immutable-revision')
|
|
25
|
+
expect(host.send(:embedding_compatible?, left: revision, right: revision.merge(instance: 'another-instance', task: 'query'))).to be true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'rejects incomplete provenance even when its space identifier looks valid' do
|
|
29
|
+
expect(host.send(:embedding_valid?, vector: [1.0, 2.0], provenance: provenance.except(:prefixes))).to be false
|
|
30
|
+
expect(host.send(:embedding_valid?, vector: [1.0, 2.0], provenance: provenance.merge(identity_source: 'revision'))).to be false
|
|
31
|
+
expect(host.send(:embedding_parse_provenance, value: '{bad json')).to be_nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sequel'
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
require 'legion/extensions/apollo/helpers/graph_query'
|
|
6
|
+
|
|
7
|
+
RSpec.describe Legion::Extensions::Apollo::Helpers::GraphQuery do
|
|
8
|
+
let(:host) { Object.new.extend(described_class) }
|
|
9
|
+
let(:db) { Sequel.sqlite }
|
|
10
|
+
|
|
11
|
+
before do
|
|
12
|
+
db.create_table(:apollo_entries) do
|
|
13
|
+
primary_key :id
|
|
14
|
+
String :content
|
|
15
|
+
String :content_type
|
|
16
|
+
Float :confidence
|
|
17
|
+
String :tags
|
|
18
|
+
String :source_agent
|
|
19
|
+
String :status
|
|
20
|
+
String :access_scope
|
|
21
|
+
Integer :identity_principal_id
|
|
22
|
+
Integer :identity_id
|
|
23
|
+
end
|
|
24
|
+
db.create_table(:apollo_relations) do
|
|
25
|
+
Integer :from_entry_id
|
|
26
|
+
Integer :to_entry_id
|
|
27
|
+
String :relation_type
|
|
28
|
+
Float :weight
|
|
29
|
+
end
|
|
30
|
+
db.create_table(:identities) do
|
|
31
|
+
primary_key :id
|
|
32
|
+
Integer :principal_id
|
|
33
|
+
end
|
|
34
|
+
db.create_table(:identity_group_memberships) do
|
|
35
|
+
Integer :principal_id
|
|
36
|
+
Integer :group_id
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
after { db.disconnect }
|
|
41
|
+
|
|
42
|
+
def entry(**attributes)
|
|
43
|
+
db[:apollo_entries].insert({ content: 'node', content_type: 'fact', confidence: 0.9,
|
|
44
|
+
status: 'confirmed', access_scope: 'global', tags: '[]' }.merge(attributes))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def relation(from:, to:, weight: 1.0, type: 'supports', **)
|
|
48
|
+
db[:apollo_relations].insert(from_entry_id: from, to_entry_id: to, weight: weight, relation_type: type)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def traverse(entry_id:, depth: 3, relation_types: nil, requesting_principal_id: 11, **)
|
|
52
|
+
sql = host.send(:graph_build_traversal_sql, relation_types: relation_types, requesting_principal_id: requesting_principal_id)
|
|
53
|
+
db.fetch(sql, entry_id: entry_id, depth: depth, relation_types: relation_types,
|
|
54
|
+
requesting_principal_id: requesting_principal_id, spread_factor: 0.6, min_activation: 0.01).all
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'keeps the maximum activation per native id across paths and bounded cycles' do
|
|
58
|
+
seed = entry
|
|
59
|
+
middle = entry
|
|
60
|
+
target = entry
|
|
61
|
+
relation(from: seed, to: target, weight: 0.1)
|
|
62
|
+
relation(from: seed, to: middle)
|
|
63
|
+
relation(from: middle, to: target)
|
|
64
|
+
relation(from: target, to: seed)
|
|
65
|
+
|
|
66
|
+
results = traverse(entry_id: seed)
|
|
67
|
+
expect(results.map { |row| row[:id] }).to eq([seed, middle, target])
|
|
68
|
+
expect(results.find { |row| row[:id] == seed }).to include(depth: 0, activation: 1.0)
|
|
69
|
+
expect(results.find { |row| row[:id] == target }).to include(depth: 2)
|
|
70
|
+
expect(results.find { |row| row[:id] == target }[:activation]).to be_within(0.00001).of(0.36)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'blocks private and archived nodes at every hop, including hidden bridges and the seed' do
|
|
74
|
+
seed = entry
|
|
75
|
+
private_bridge = entry(access_scope: 'private', identity_principal_id: 22)
|
|
76
|
+
archived_bridge = entry(status: 'archived')
|
|
77
|
+
hidden_private_child = entry
|
|
78
|
+
hidden_archived_child = entry
|
|
79
|
+
visible = entry(access_scope: 'private', identity_principal_id: 11)
|
|
80
|
+
relation(from: seed, to: private_bridge)
|
|
81
|
+
relation(from: seed, to: archived_bridge)
|
|
82
|
+
relation(from: private_bridge, to: hidden_private_child)
|
|
83
|
+
relation(from: archived_bridge, to: hidden_archived_child)
|
|
84
|
+
relation(from: seed, to: visible)
|
|
85
|
+
|
|
86
|
+
expect(traverse(entry_id: seed).map { |row| row[:id] }).to eq([seed, visible])
|
|
87
|
+
expect(traverse(entry_id: private_bridge)).to eq([])
|
|
88
|
+
expect(traverse(entry_id: archived_bridge)).to eq([])
|
|
89
|
+
expect(traverse(entry_id: seed, requesting_principal_id: 0).map { |row| row[:id] }).to eq([seed])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'uses the shortest depth to break equal-activation ties and restricts edge types' do
|
|
93
|
+
seed = entry
|
|
94
|
+
middle = entry
|
|
95
|
+
target = entry
|
|
96
|
+
relation(from: seed, to: target, weight: 0.6)
|
|
97
|
+
relation(from: seed, to: middle)
|
|
98
|
+
relation(from: middle, to: target)
|
|
99
|
+
excluded = entry
|
|
100
|
+
relation(from: seed, to: excluded, type: "other'quoted")
|
|
101
|
+
|
|
102
|
+
results = traverse(entry_id: seed, relation_types: ['supports'])
|
|
103
|
+
expect(results.map { |row| row[:id] }).to eq([seed, middle, target])
|
|
104
|
+
expect(results.find { |row| row[:id] == target }[:depth]).to eq(1)
|
|
105
|
+
expect(traverse(entry_id: seed, depth: 0).map { |row| row[:id] }).to eq([seed])
|
|
106
|
+
expect(traverse(entry_id: seed, relation_types: ["other'quoted"]).map { |row| row[:id] }).to eq([seed, excluded])
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -4,54 +4,71 @@ require 'spec_helper'
|
|
|
4
4
|
require 'legion/extensions/apollo/helpers/similarity'
|
|
5
5
|
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::Helpers::Similarity do
|
|
7
|
-
|
|
7
|
+
let(:host) { Object.new.extend(described_class) }
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
allow(host).to receive(:settings).and_return(Legion::Extensions::Apollo.default_settings)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe 'cosine similarity' do
|
|
8
14
|
it 'returns 1.0 for identical vectors' do
|
|
9
15
|
vec = [1.0, 0.0, 0.0]
|
|
10
|
-
expect(
|
|
16
|
+
expect(host.send(:similarity_cosine, vec_a: vec, vec_b: vec)).to be_within(0.001).of(1.0)
|
|
11
17
|
end
|
|
12
18
|
|
|
13
19
|
it 'returns 0.0 for orthogonal vectors' do
|
|
14
20
|
vec_a = [1.0, 0.0]
|
|
15
21
|
vec_b = [0.0, 1.0]
|
|
16
|
-
expect(
|
|
22
|
+
expect(host.send(:similarity_cosine, vec_a: vec_a, vec_b: vec_b)).to be_within(0.001).of(0.0)
|
|
17
23
|
end
|
|
18
24
|
|
|
19
25
|
it 'returns correct similarity for known vectors' do
|
|
20
26
|
vec_a = [1.0, 2.0, 3.0]
|
|
21
27
|
vec_b = [4.0, 5.0, 6.0]
|
|
22
|
-
expect(
|
|
28
|
+
expect(host.send(:similarity_cosine, vec_a: vec_a, vec_b: vec_b)).to be_within(0.001).of(0.9746)
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
it 'returns 0.0 for zero vectors' do
|
|
26
32
|
vec_a = [0.0, 0.0]
|
|
27
33
|
vec_b = [1.0, 1.0]
|
|
28
|
-
expect(
|
|
34
|
+
expect(host.send(:similarity_cosine, vec_a: vec_a, vec_b: vec_b)).to eq(0.0)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'returns no match for malformed or unequal persisted vectors' do
|
|
38
|
+
[nil, '', '{bad json', '[1,\"bad\"]', [1.0], [], [Float::NAN, 1.0], [Complex(1, 1), 1.0]].each do |invalid|
|
|
39
|
+
expect(host.send(:similarity_cosine, vec_a: invalid, vec_b: [1.0, 2.0])).to eq(0.0)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'compares JSON vectors and avoids overflowing large finite components' do
|
|
44
|
+
expect(host.send(:similarity_cosine, vec_a: '[1.0,2.0]', vec_b: [1.0, 2.0])).to be_within(0.001).of(1.0)
|
|
45
|
+
expect(host.send(:similarity_cosine, vec_a: [1e300, 1e300], vec_b: [1e300, 1e300])).to be_within(0.001).of(1.0)
|
|
29
46
|
end
|
|
30
47
|
end
|
|
31
48
|
|
|
32
|
-
describe '
|
|
49
|
+
describe 'corroboration threshold' do
|
|
33
50
|
it 'returns true when similarity exceeds threshold' do
|
|
34
|
-
expect(
|
|
51
|
+
expect(host.send(:similarity_above_corroboration_threshold?, similarity: 0.95)).to be true
|
|
35
52
|
end
|
|
36
53
|
|
|
37
54
|
it 'returns false when similarity below threshold' do
|
|
38
|
-
expect(
|
|
55
|
+
expect(host.send(:similarity_above_corroboration_threshold?, similarity: 0.85)).to be false
|
|
39
56
|
end
|
|
40
57
|
end
|
|
41
58
|
|
|
42
|
-
describe '
|
|
59
|
+
describe 'match classification' do
|
|
43
60
|
it 'returns :corroboration for high similarity same type' do
|
|
44
|
-
result =
|
|
61
|
+
result = host.send(:similarity_classify_match, similarity: 0.95, same_content_type: true)
|
|
45
62
|
expect(result).to eq(:corroboration)
|
|
46
63
|
end
|
|
47
64
|
|
|
48
65
|
it 'returns :contradiction for high similarity with contradicts relation' do
|
|
49
|
-
result =
|
|
66
|
+
result = host.send(:similarity_classify_match, similarity: 0.95, same_content_type: true, contradicts: true)
|
|
50
67
|
expect(result).to eq(:contradiction)
|
|
51
68
|
end
|
|
52
69
|
|
|
53
70
|
it 'returns :novel for low similarity' do
|
|
54
|
-
result =
|
|
71
|
+
result = host.send(:similarity_classify_match, similarity: 0.5, same_content_type: true)
|
|
55
72
|
expect(result).to eq(:novel)
|
|
56
73
|
end
|
|
57
74
|
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sequel'
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
require 'legion/extensions/apollo/helpers/storage'
|
|
6
|
+
|
|
7
|
+
RSpec.describe Legion::Extensions::Apollo::Helpers::Storage do
|
|
8
|
+
let(:db) { Sequel.sqlite }
|
|
9
|
+
let(:provenance) do
|
|
10
|
+
{ space_id: 'a' * 64, dimensions: 2, provider: 'test', model: 'embedding', instance: 'local',
|
|
11
|
+
model_revision: nil, identity_source: 'instance', task: 'document',
|
|
12
|
+
prefixes: { document: '', query: '' }, normalization: 'none' }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
let(:host) { Object.new.extend(described_class) }
|
|
16
|
+
|
|
17
|
+
before do
|
|
18
|
+
db.create_table(:apollo_entries) do
|
|
19
|
+
primary_key :id
|
|
20
|
+
String :tags
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
after { db.disconnect }
|
|
25
|
+
|
|
26
|
+
it 'filters any and all tags safely with quoted values' do
|
|
27
|
+
quoted = "x'); DROP TABLE apollo_entries; --"
|
|
28
|
+
both = db[:apollo_entries].insert(tags: Legion::JSON.dump(['ruby', quoted]))
|
|
29
|
+
one = db[:apollo_entries].insert(tags: Legion::JSON.dump(['ruby']))
|
|
30
|
+
db[:apollo_entries].insert(tags: Legion::JSON.dump(['else']))
|
|
31
|
+
db[:apollo_entries].insert(tags: '[]')
|
|
32
|
+
|
|
33
|
+
%i[any all].each do |mode|
|
|
34
|
+
expected = mode == :all ? [both] : [both, one]
|
|
35
|
+
dataset = host.send(:storage_filter_tags, dataset: db[:apollo_entries], tags: ['ruby', quoted], tag_match: mode)
|
|
36
|
+
expect(dataset.order(:id).select_map(:id)).to eq(expected)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'raises when tag filtering encounters malformed persisted JSON' do
|
|
41
|
+
db[:apollo_entries].insert(tags: 'malformed')
|
|
42
|
+
dataset = host.send(:storage_filter_tags, dataset: db[:apollo_entries], tags: ['ruby'])
|
|
43
|
+
expect { dataset.all }.to raise_error(Sequel::DatabaseError)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'raises for malformed JSON and non-array tag values' do
|
|
47
|
+
expect { host.send(:storage_deserialize_tags, tags: 'malformed') }.to raise_error(Legion::JSON::ParseError)
|
|
48
|
+
expect { host.send(:storage_deserialize_tags, tags: '{"tag":"ruby"}') }.to raise_error(ArgumentError)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'round trips SQLite arrays and provenance without PostgreSQL types' do
|
|
52
|
+
tags = ['comma,value', 'quote"value', 'unicode λ']
|
|
53
|
+
expect(host.send(:storage_deserialize_tags, tags: host.send(:storage_serialize_tags, db: db, tags: tags))).to eq(tags)
|
|
54
|
+
encoded = host.send(:storage_serialize_provenance, db: db, provenance: provenance)
|
|
55
|
+
expect(Legion::JSON.load(encoded)).to eq(provenance)
|
|
56
|
+
expect(host.send(:storage_serialize_embedding, db: db, vector: nil)).to be_nil
|
|
57
|
+
expect(host.send(:storage_serialize_provenance, db: db, provenance: nil)).to be_nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -4,59 +4,61 @@ require 'spec_helper'
|
|
|
4
4
|
require 'legion/extensions/apollo/helpers/tag_normalizer'
|
|
5
5
|
|
|
6
6
|
RSpec.describe Legion::Extensions::Apollo::Helpers::TagNormalizer do
|
|
7
|
-
|
|
7
|
+
let(:host) { Object.new.extend(described_class) }
|
|
8
|
+
|
|
9
|
+
describe 'tag normalization' do
|
|
8
10
|
it 'lowercases tags' do
|
|
9
|
-
expect(
|
|
11
|
+
expect(host.send(:tag_normalize, raw: 'RabbitMQ')).to eq('rabbitmq')
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
it 'strips leading/trailing whitespace' do
|
|
13
|
-
expect(
|
|
15
|
+
expect(host.send(:tag_normalize, raw: ' hello ')).to eq('hello')
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
it 'replaces spaces with hyphens' do
|
|
17
|
-
expect(
|
|
19
|
+
expect(host.send(:tag_normalize, raw: 'message broker')).to eq('message-broker')
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
it 'strips special characters except hyphens' do
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
23
|
-
expect(
|
|
23
|
+
expect(host.send(:tag_normalize, raw: 'c#')).to eq('csharp')
|
|
24
|
+
expect(host.send(:tag_normalize, raw: 'hello!')).to eq('hello')
|
|
25
|
+
expect(host.send(:tag_normalize, raw: 'key=value')).to eq('keyvalue')
|
|
24
26
|
end
|
|
25
27
|
|
|
26
28
|
it 'collapses multiple hyphens' do
|
|
27
|
-
expect(
|
|
29
|
+
expect(host.send(:tag_normalize, raw: 'a--b---c')).to eq('a-b-c')
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
it 'applies known aliases' do
|
|
31
|
-
expect(
|
|
32
|
-
expect(
|
|
33
|
-
expect(
|
|
33
|
+
expect(host.send(:tag_normalize, raw: 'C++')).to eq('cplusplus')
|
|
34
|
+
expect(host.send(:tag_normalize, raw: '.NET')).to eq('dotnet')
|
|
35
|
+
expect(host.send(:tag_normalize, raw: 'node.js')).to eq('nodejs')
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
it 'returns nil for empty results' do
|
|
37
|
-
expect(
|
|
38
|
-
expect(
|
|
39
|
+
expect(host.send(:tag_normalize, raw: '!!!')).to be_nil
|
|
40
|
+
expect(host.send(:tag_normalize, raw: '')).to be_nil
|
|
39
41
|
end
|
|
40
42
|
end
|
|
41
43
|
|
|
42
|
-
describe '
|
|
44
|
+
describe 'tag collection normalization' do
|
|
43
45
|
it 'normalizes, deduplicates, and caps at max' do
|
|
44
46
|
tags = %w[RabbitMQ rabbitmq AMQP message-broker extra-tag sixth-tag]
|
|
45
|
-
result =
|
|
47
|
+
result = host.send(:tag_normalize_all, tags: tags, max: 5)
|
|
46
48
|
expect(result).to eq(%w[rabbitmq amqp message-broker extra-tag sixth-tag])
|
|
47
49
|
end
|
|
48
50
|
|
|
49
51
|
it 'filters out nil results' do
|
|
50
|
-
expect(
|
|
52
|
+
expect(host.send(:tag_normalize_all, tags: ['!!!', 'valid'])).to eq(['valid'])
|
|
51
53
|
end
|
|
52
54
|
|
|
53
55
|
it 'handles nil input' do
|
|
54
|
-
expect(
|
|
56
|
+
expect(host.send(:tag_normalize_all, tags: nil)).to eq([])
|
|
55
57
|
end
|
|
56
58
|
|
|
57
59
|
it 'defaults max to 5' do
|
|
58
60
|
tags = %w[a b c d e f g]
|
|
59
|
-
expect(
|
|
61
|
+
expect(host.send(:tag_normalize_all, tags: tags).length).to eq(5)
|
|
60
62
|
end
|
|
61
63
|
end
|
|
62
64
|
end
|