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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -0
  3. data/README.md +113 -18
  4. data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
  5. data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
  6. data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
  7. data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
  8. data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
  9. data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
  10. data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
  11. data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
  12. data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
  13. data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
  14. data/lib/legion/extensions/apollo/api.rb +116 -51
  15. data/lib/legion/extensions/apollo/client.rb +20 -9
  16. data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
  17. data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
  18. data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
  19. data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
  20. data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
  21. data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
  22. data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
  23. data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
  24. data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
  25. data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
  26. data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
  27. data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
  28. data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
  29. data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
  30. data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
  31. data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
  32. data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
  33. data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
  34. data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
  35. data/lib/legion/extensions/apollo/runners/request.rb +51 -47
  36. data/lib/legion/extensions/apollo/runners/search.rb +88 -0
  37. data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
  38. data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
  39. data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
  40. data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
  41. data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
  42. data/lib/legion/extensions/apollo/transport.rb +20 -0
  43. data/lib/legion/extensions/apollo/version.rb +1 -1
  44. data/lib/legion/extensions/apollo.rb +26 -24
  45. data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
  46. data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
  47. data/spec/legion/extensions/apollo/api_spec.rb +250 -28
  48. data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
  49. data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
  50. data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
  51. data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
  52. data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
  53. data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
  54. data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
  55. data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
  56. data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
  57. data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
  58. data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
  59. data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
  60. data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
  61. data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
  62. data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
  63. data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
  64. data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
  65. data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
  66. data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
  67. data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
  68. data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
  69. data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
  70. data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
  71. data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
  72. data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
  73. data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
  74. data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
  75. data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
  76. data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
  77. data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
  78. data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
  79. data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
  80. data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
  81. data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
  82. data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
  83. data/spec/spec_helper.rb +13 -15
  84. metadata +71 -13
  85. data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
  86. data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
  87. data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
  88. data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
  89. data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
  90. data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
  91. data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
  92. 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
- describe 'constants' do
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
- it 'defines CORROBORATION_SIMILARITY_THRESHOLD' do
33
- expect(described_class::CORROBORATION_SIMILARITY_THRESHOLD).to eq(0.9)
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 '.apply_decay' do
50
- it 'applies power-law decay with default alpha when no age given' do
51
- result = described_class.apply_decay(confidence: 1.0)
52
- expected = 1.0 / (1.0 + 0.05) # ~0.9524
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
- describe '.apply_retrieval_boost' do
80
- it 'adds RETRIEVAL_BOOST to confidence' do
81
- result = described_class.apply_retrieval_boost(confidence: 0.5)
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 = described_class.apply_retrieval_boost(confidence: 0.99)
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 '.apply_corroboration_boost' do
92
- it 'adds CORROBORATION_BOOST to confidence' do
93
- result = described_class.apply_corroboration_boost(confidence: 0.5)
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 = described_class.apply_corroboration_boost(confidence: 0.9)
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 = described_class.apply_corroboration_boost(confidence: 0.5, weight: 0.5)
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
@@ -1,100 +1,108 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'sequel'
3
4
  require 'spec_helper'
4
5
  require 'legion/extensions/apollo/helpers/graph_query'
5
6
 
6
7
  RSpec.describe Legion::Extensions::Apollo::Helpers::GraphQuery do
7
- describe 'constants' do
8
- it 'defines SPREAD_FACTOR' do
9
- expect(described_class::SPREAD_FACTOR).to eq(0.6)
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
10
23
  end
11
-
12
- it 'defines DEFAULT_DEPTH' do
13
- expect(described_class::DEFAULT_DEPTH).to eq(2)
14
- end
15
-
16
- it 'defines MIN_ACTIVATION' do
17
- expect(described_class::MIN_ACTIVATION).to eq(0.1)
18
- end
19
- end
20
-
21
- describe '.build_traversal_sql' do
22
- it 'returns SQL string with entry_id placeholder' do
23
- sql = described_class.build_traversal_sql(depth: 2)
24
- expect(sql).to include('apollo_entries')
25
- expect(sql).to include('apollo_relations')
26
- expect(sql).to include('WITH RECURSIVE')
27
- expect(sql).to include(':entry_id')
24
+ db.create_table(:apollo_relations) do
25
+ Integer :from_entry_id
26
+ Integer :to_entry_id
27
+ String :relation_type
28
+ Float :weight
28
29
  end
29
-
30
- it 'includes relation type filter when specified' do
31
- sql = described_class.build_traversal_sql(depth: 2, relation_types: %w[causes depends_on])
32
- expect(sql).to include("'causes'")
33
- expect(sql).to include("'depends_on'")
34
- end
35
-
36
- it 'respects custom depth' do
37
- sql = described_class.build_traversal_sql(depth: 3)
38
- expect(sql).to include('g.depth < 3')
30
+ db.create_table(:identities) do
31
+ primary_key :id
32
+ Integer :principal_id
39
33
  end
40
-
41
- it 'applies spread factor and min activation' do
42
- sql = described_class.build_traversal_sql(depth: 2)
43
- expect(sql).to include('0.6')
44
- expect(sql).to include('0.1')
34
+ db.create_table(:identity_group_memberships) do
35
+ Integer :principal_id
36
+ Integer :group_id
45
37
  end
46
38
  end
47
39
 
48
- describe '.build_semantic_search_sql' do
49
- it 'returns SQL with vector placeholder' do
50
- sql = described_class.build_semantic_search_sql(limit: 5, min_confidence: 0.3)
51
- expect(sql).to include('apollo_entries')
52
- expect(sql).to include(':embedding')
53
- expect(sql).to include('<=>')
54
- expect(sql).to include('LIMIT 5')
55
- end
40
+ after { db.disconnect }
56
41
 
57
- it 'includes status filter' do
58
- sql = described_class.build_semantic_search_sql(limit: 10, statuses: %w[confirmed])
59
- expect(sql).to include("'confirmed'")
60
- end
61
-
62
- it 'includes tag filter when specified' do
63
- sql = described_class.build_semantic_search_sql(limit: 10, tags: %w[vault dns])
64
- expect(sql).to include("'vault'")
65
- expect(sql).to include("'dns'")
66
- expect(sql).to include('&&')
67
- end
68
-
69
- context 'without requesting_principal_id' do
70
- it 'includes no access_scope filter' do
71
- sql = described_class.build_semantic_search_sql(limit: 5, min_confidence: 0.3)
72
- expect(sql).not_to include('access_scope')
73
- end
74
- end
75
-
76
- context 'with requesting_principal_id' do
77
- let(:sql) { described_class.build_semantic_search_sql(limit: 5, min_confidence: 0.3, requesting_principal_id: 42) }
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
78
46
 
79
- it 'allows global entries unconditionally' do
80
- expect(sql).to include("access_scope = 'global'")
81
- end
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
82
50
 
83
- it 'allows private entries owned by the principal via principal FK' do
84
- expect(sql).to include('e.identity_principal_id = 42')
85
- end
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
86
56
 
87
- it 'allows private entries owned by the principal via identity subquery (multi-provider auth)' do
88
- expect(sql).to include('SELECT id FROM identities WHERE principal_id = 42')
89
- end
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
90
72
 
91
- it 'allows team entries when principal shares a group with the submitter' do
92
- expect(sql).to include('identity_group_memberships')
93
- end
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
94
91
 
95
- it 'wraps the entire access_scope condition in parentheses so it does not break AND chaining' do
96
- expect(sql).to match(/AND \(.*access_scope.*\)/m)
97
- end
98
- end
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])
99
107
  end
100
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
- describe '.cosine_similarity' do
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(described_class.cosine_similarity(vec_a: vec, vec_b: vec)).to be_within(0.001).of(1.0)
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(described_class.cosine_similarity(vec_a: vec_a, vec_b: vec_b)).to be_within(0.001).of(0.0)
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(described_class.cosine_similarity(vec_a: vec_a, vec_b: vec_b)).to be_within(0.001).of(0.9746)
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(described_class.cosine_similarity(vec_a: vec_a, vec_b: vec_b)).to eq(0.0)
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 '.above_corroboration_threshold?' do
49
+ describe 'corroboration threshold' do
33
50
  it 'returns true when similarity exceeds threshold' do
34
- expect(described_class.above_corroboration_threshold?(similarity: 0.95)).to be true
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(described_class.above_corroboration_threshold?(similarity: 0.85)).to be false
55
+ expect(host.send(:similarity_above_corroboration_threshold?, similarity: 0.85)).to be false
39
56
  end
40
57
  end
41
58
 
42
- describe '.classify_match' do
59
+ describe 'match classification' do
43
60
  it 'returns :corroboration for high similarity same type' do
44
- result = described_class.classify_match(similarity: 0.95, same_content_type: true)
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 = described_class.classify_match(similarity: 0.95, same_content_type: true, contradicts: true)
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 = described_class.classify_match(similarity: 0.5, same_content_type: true)
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
- describe '.normalize' do
7
+ let(:host) { Object.new.extend(described_class) }
8
+
9
+ describe 'tag normalization' do
8
10
  it 'lowercases tags' do
9
- expect(described_class.normalize('RabbitMQ')).to eq('rabbitmq')
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(described_class.normalize(' hello ')).to eq('hello')
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(described_class.normalize('message broker')).to eq('message-broker')
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(described_class.normalize('c#')).to eq('csharp')
22
- expect(described_class.normalize('hello!')).to eq('hello')
23
- expect(described_class.normalize('key=value')).to eq('keyvalue')
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(described_class.normalize('a--b---c')).to eq('a-b-c')
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(described_class.normalize('C++')).to eq('cplusplus')
32
- expect(described_class.normalize('.NET')).to eq('dotnet')
33
- expect(described_class.normalize('node.js')).to eq('nodejs')
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(described_class.normalize('!!!')).to be_nil
38
- expect(described_class.normalize('')).to be_nil
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 '.normalize_all' do
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 = described_class.normalize_all(tags, max: 5)
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(described_class.normalize_all(['!!!', 'valid'])).to eq(['valid'])
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(described_class.normalize_all(nil)).to eq([])
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(described_class.normalize_all(tags).length).to eq(5)
61
+ expect(host.send(:tag_normalize_all, tags: tags).length).to eq(5)
60
62
  end
61
63
  end
62
64
  end