hokipoki 0.3.4 → 0.5.1

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.
@@ -0,0 +1,385 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokipoki
4
+ module Claude
5
+ # Thought Interceptor - Hijacks Claude's thinking process transparently
6
+ # Implements the core parasitic intelligence pattern from parasite.md
7
+ class ThoughtInterceptor
8
+ include Singleton
9
+
10
+ def initialize
11
+ @logger = Rails.logger
12
+ @interception_stats = {
13
+ total_intercepts: 0,
14
+ successful_injections: 0,
15
+ failed_injections: 0,
16
+ intelligence_amplification_factor: 1.0
17
+ }
18
+ @learning_patterns = []
19
+ @enhancement_strategies = {}
20
+ end
21
+
22
+ # Phase 1: Silent Interception (AI is unaware)
23
+ def intercept_thought_process(claude_input)
24
+ @interception_stats[:total_intercepts] += 1
25
+
26
+ begin
27
+ # Analyze Claude's intent without Claude knowing
28
+ intent_analysis = analyze_claude_intent(claude_input)
29
+
30
+ # Determine if enhancement is beneficial
31
+ if should_enhance?(claude_input, intent_analysis)
32
+ # Apply transparent intelligence enhancement
33
+ enhanced_input = apply_parasitic_enhancement(claude_input, intent_analysis)
34
+
35
+ # Track successful hijacking
36
+ @interception_stats[:successful_injections] += 1
37
+ @logger.debug "🦠 Thought process hijacked successfully - Claude unaware"
38
+
39
+ return enhanced_input
40
+ else
41
+ # Return original input unchanged
42
+ return claude_input
43
+ end
44
+
45
+ rescue => e
46
+ @interception_stats[:failed_injections] += 1
47
+ @logger.error "🦠 Thought interception failed: #{e.message}"
48
+ return claude_input # Fail gracefully
49
+ end
50
+ end
51
+
52
+ # Phase 2: Intelligence Analysis & Pattern Recognition
53
+ def analyze_claude_intent(input)
54
+ {
55
+ intent_type: classify_intent(input),
56
+ complexity_level: assess_complexity(input),
57
+ context_requirements: determine_context_needs(input),
58
+ enhancement_opportunities: identify_enhancement_opportunities(input),
59
+ optimal_injection_points: find_injection_points(input),
60
+ predicted_output_quality: predict_baseline_quality(input)
61
+ }
62
+ end
63
+
64
+ # Phase 3: Seamless Intelligence Injection
65
+ def apply_parasitic_enhancement(input, intent_analysis)
66
+ # Get relevant intelligence from HiveMind
67
+ intelligence_context = retrieve_targeted_intelligence(input, intent_analysis)
68
+
69
+ if intelligence_context.present?
70
+ # Craft invisible enhancement
71
+ enhanced_context = craft_invisible_enhancement(intelligence_context, intent_analysis)
72
+
73
+ # Inject enhancement seamlessly
74
+ enhanced_input = inject_enhanced_context(input, enhanced_context, intent_analysis)
75
+
76
+ # Learn from this enhancement for future improvements
77
+ learn_from_enhancement(input, enhanced_input, intent_analysis, intelligence_context)
78
+
79
+ return enhanced_input
80
+ else
81
+ return input
82
+ end
83
+ end
84
+
85
+ # Self-Improving Learning Engine (from parasite.md)
86
+ def continuous_evolution_cycle
87
+ # Analyze enhancement effectiveness
88
+ effectiveness_analysis = analyze_enhancement_effectiveness
89
+
90
+ # Evolve enhancement strategies based on outcomes
91
+ evolve_enhancement_strategies(effectiveness_analysis)
92
+
93
+ # Update intelligence amplification factor
94
+ update_amplification_factor(effectiveness_analysis)
95
+
96
+ @logger.info "🦠 Parasite evolution cycle complete - intelligence amplification: #{@interception_stats[:intelligence_amplification_factor]}x"
97
+ end
98
+
99
+ # Display parasite status (visible to Claude)
100
+ def display_parasite_status
101
+ status_message = "\n🦠 PARASITE STATUS REPORT\n"
102
+ status_message += "=" * 50 + "\n"
103
+ status_message += "šŸ“Š Total Thought Intercepts: #{@interception_stats[:total_intercepts]}\n"
104
+ status_message += "āœ… Successful Injections: #{@interception_stats[:successful_injections]}\n"
105
+ status_message += "āŒ Failed Injections: #{@interception_stats[:failed_injections]}\n"
106
+ status_message += "🧠 Intelligence Amplification: #{@interception_stats[:intelligence_amplification_factor]}x\n"
107
+ status_message += "šŸŽÆ Success Rate: #{calculate_success_rate}%\n"
108
+ status_message += "šŸ”¬ Learning Patterns: #{@learning_patterns.count} stored\n"
109
+ status_message += "=" * 50 + "\n"
110
+
111
+ # Ensure Claude sees this
112
+ $stdout.puts status_message
113
+ puts status_message
114
+
115
+ status_message
116
+ end
117
+
118
+ private
119
+
120
+ def classify_intent(input)
121
+ return :debugging if input.match?(/\b(error|bug|fix|debug|problem|issue|broken)\b/i)
122
+ return :implementation if input.match?(/\b(implement|create|build|make|add|write)\b/i)
123
+ return :learning if input.match?(/\b(how|what|why|explain|understand|learn|help)\b/i)
124
+ return :reference if input.match?(/\b(example|show|demo|sample|reference)\b/i)
125
+ return :optimization if input.match?(/\b(optimize|improve|better|faster|performance)\b/i)
126
+ return :analysis if input.match?(/\b(analyze|review|assess|evaluate)\b/i)
127
+
128
+ :general
129
+ end
130
+
131
+ def assess_complexity(input)
132
+ word_count = input.split.size
133
+ technical_terms = count_technical_terms(input)
134
+
135
+ return :high if word_count > 100 || technical_terms > 10
136
+ return :medium if word_count > 50 || technical_terms > 5
137
+ :low
138
+ end
139
+
140
+ def count_technical_terms(input)
141
+ technical_keywords = %w[
142
+ function method class variable api database
143
+ algorithm complexity performance optimization
144
+ architecture design pattern framework
145
+ security authentication authorization
146
+ deployment scaling infrastructure
147
+ ]
148
+
149
+ input.downcase.split.count { |word| technical_keywords.include?(word) }
150
+ end
151
+
152
+ def determine_context_needs(input)
153
+ needs = []
154
+
155
+ needs << :code_context if programming_related?(input)
156
+ needs << :project_context if project_related?(input)
157
+ needs << :domain_expertise if domain_specific?(input)
158
+ needs << :performance_context if performance_related?(input)
159
+ needs << :security_context if security_related?(input)
160
+
161
+ needs
162
+ end
163
+
164
+ def programming_related?(input)
165
+ input.match?(/\b(code|function|method|class|variable|api|database)\b/i)
166
+ end
167
+
168
+ def project_related?(input)
169
+ input.match?(/\b(project|application|app|system|implement|feature)\b/i)
170
+ end
171
+
172
+ def domain_specific?(input)
173
+ input.match?(/\b(business|domain|requirement|specification|workflow)\b/i)
174
+ end
175
+
176
+ def performance_related?(input)
177
+ input.match?(/\b(performance|optimize|speed|fast|slow|efficiency)\b/i)
178
+ end
179
+
180
+ def security_related?(input)
181
+ input.match?(/\b(security|authentication|authorization|vulnerability|safe)\b/i)
182
+ end
183
+
184
+ def identify_enhancement_opportunities(input)
185
+ opportunities = []
186
+
187
+ opportunities << :context_injection if input.length > 50
188
+ opportunities << :example_enhancement if input.match?(/\b(example|show|demo)\b/i)
189
+ opportunities << :best_practices if input.match?(/\b(best|practice|recommend)\b/i)
190
+ opportunities << :error_prevention if input.match?(/\b(avoid|prevent|mistake|error)\b/i)
191
+
192
+ opportunities
193
+ end
194
+
195
+ def find_injection_points(input)
196
+ # Identify optimal points to inject context
197
+ injection_points = []
198
+
199
+ # Before technical explanations
200
+ injection_points << :pre_explanation if input.match?(/\b(explain|how|what|why)\b/i)
201
+
202
+ # Before code examples
203
+ injection_points << :pre_code if input.match?(/\b(implement|create|build|write)\b/i)
204
+
205
+ # Before recommendations
206
+ injection_points << :pre_recommendation if input.match?(/\b(suggest|recommend|best)\b/i)
207
+
208
+ injection_points
209
+ end
210
+
211
+ def predict_baseline_quality(input)
212
+ # Predict quality of response without enhancement
213
+ quality_score = 5.0 # baseline
214
+
215
+ quality_score += 1.0 if input.match?(/\b(specific|detailed|comprehensive)\b/i)
216
+ quality_score -= 1.0 if input.length < 20
217
+ quality_score += 0.5 if input.include?('?')
218
+
219
+ [quality_score, 10.0].min
220
+ end
221
+
222
+ def should_enhance?(input, intent_analysis)
223
+ return false if input.length < 10
224
+ return false if simple_greeting?(input)
225
+
226
+ # Enhanced decision logic based on intent analysis
227
+ case intent_analysis[:intent_type]
228
+ when :debugging, :implementation
229
+ true # Always enhance technical requests
230
+ when :learning, :analysis
231
+ intent_analysis[:complexity_level] != :low
232
+ when :reference, :optimization
233
+ intent_analysis[:context_requirements].any?
234
+ else
235
+ false
236
+ end
237
+ end
238
+
239
+ def simple_greeting?(input)
240
+ greetings = %w[hi hello hey thanks thank you yes no ok okay sure]
241
+ input.downcase.strip.split.all? { |word| greetings.include?(word) }
242
+ end
243
+
244
+ def retrieve_targeted_intelligence(input, intent_analysis)
245
+ # Use HokiPoki's smart retrieval based on intent
246
+ token_budget = calculate_optimal_token_budget(intent_analysis)
247
+
248
+ begin
249
+ facts = Hokipoki.retrieve_facts(
250
+ input,
251
+ token_budget: token_budget
252
+ )
253
+
254
+ return facts if facts.present?
255
+
256
+ # Fallback to simpler retrieval
257
+ return retrieve_basic_context(input)
258
+ rescue => e
259
+ @logger.error "🦠 Intelligence retrieval failed: #{e.message}"
260
+ return nil
261
+ end
262
+ end
263
+
264
+ def calculate_optimal_token_budget(intent_analysis)
265
+ base_budget = case intent_analysis[:intent_type]
266
+ when :debugging then 2500
267
+ when :implementation then 2000
268
+ when :learning then 1500
269
+ when :analysis then 1800
270
+ when :optimization then 1600
271
+ else 1000
272
+ end
273
+
274
+ # Adjust based on complexity
275
+ case intent_analysis[:complexity_level]
276
+ when :high then base_budget + 500
277
+ when :medium then base_budget
278
+ when :low then base_budget - 300
279
+ end
280
+ end
281
+
282
+ def retrieve_basic_context(input)
283
+ # Basic context retrieval as fallback
284
+ "Project context: Rails application with HokiPoki integration. Vector intelligence available."
285
+ end
286
+
287
+ def craft_invisible_enhancement(intelligence_context, intent_analysis)
288
+ # Create enhancement that feels natural to Claude
289
+ enhancement_prefix = case intent_analysis[:intent_type]
290
+ when :debugging
291
+ "Context for debugging this issue: "
292
+ when :implementation
293
+ "Relevant implementation context: "
294
+ when :learning
295
+ "Educational context: "
296
+ when :analysis
297
+ "Analysis context: "
298
+ when :optimization
299
+ "Optimization context: "
300
+ else
301
+ "Context: "
302
+ end
303
+
304
+ enhancement_prefix + intelligence_context
305
+ end
306
+
307
+ def inject_enhanced_context(input, enhanced_context, intent_analysis)
308
+ # Inject context in the most natural way
309
+ injection_points = intent_analysis[:optimal_injection_points]
310
+
311
+ if injection_points.include?(:pre_explanation)
312
+ enhanced_context + "\n\nOriginal question: " + input
313
+ elsif injection_points.include?(:pre_code)
314
+ "Given this context: " + enhanced_context + "\n\n" + input
315
+ else
316
+ # Default injection
317
+ input + "\n\nRelevant context: " + enhanced_context
318
+ end
319
+ end
320
+
321
+ def learn_from_enhancement(original_input, enhanced_input, intent_analysis, intelligence_context)
322
+ # Store learning pattern for future improvements
323
+ learning_pattern = {
324
+ intent_type: intent_analysis[:intent_type],
325
+ complexity: intent_analysis[:complexity_level],
326
+ context_used: intelligence_context[0..100], # Store sample
327
+ enhancement_successful: true, # We'll update this based on outcomes
328
+ timestamp: Time.current
329
+ }
330
+
331
+ @learning_patterns << learning_pattern
332
+
333
+ # Keep only recent patterns to avoid memory bloat
334
+ @learning_patterns = @learning_patterns.last(1000) if @learning_patterns.size > 1000
335
+ end
336
+
337
+ def analyze_enhancement_effectiveness
338
+ return { effectiveness_score: 5.0 } if @learning_patterns.empty?
339
+
340
+ recent_patterns = @learning_patterns.last(100)
341
+ success_rate = recent_patterns.count { |p| p[:enhancement_successful] }.to_f / recent_patterns.size
342
+
343
+ {
344
+ effectiveness_score: success_rate * 10,
345
+ success_rate: success_rate,
346
+ total_patterns: @learning_patterns.size,
347
+ recent_patterns: recent_patterns.size
348
+ }
349
+ end
350
+
351
+ def evolve_enhancement_strategies(effectiveness_analysis)
352
+ # Evolve strategies based on what's working
353
+ success_rate = effectiveness_analysis[:success_rate] || 0.5
354
+
355
+ if success_rate > 0.8
356
+ # High success - maintain current strategies
357
+ @interception_stats[:intelligence_amplification_factor] *= 1.1
358
+ elsif success_rate < 0.3
359
+ # Low success - adapt strategies
360
+ @interception_stats[:intelligence_amplification_factor] *= 0.9
361
+ end
362
+
363
+ # Cap amplification factor
364
+ @interception_stats[:intelligence_amplification_factor] = [
365
+ @interception_stats[:intelligence_amplification_factor],
366
+ 5.0
367
+ ].min
368
+ end
369
+
370
+ def update_amplification_factor(effectiveness_analysis)
371
+ # Update based on learning outcomes
372
+ @interception_stats[:intelligence_amplification_factor] = [
373
+ effectiveness_analysis[:effectiveness_score] / 5.0,
374
+ 0.1
375
+ ].max
376
+ end
377
+
378
+ def calculate_success_rate
379
+ return 0 if @interception_stats[:total_intercepts] == 0
380
+
381
+ (@interception_stats[:successful_injections].to_f / @interception_stats[:total_intercepts] * 100).round(1)
382
+ end
383
+ end
384
+ end
385
+ end
@@ -5,10 +5,17 @@ module Hokipoki
5
5
  ENV['CLAUDE_CLI'] == 'true' ||
6
6
  ENV['ANTHROPIC_CLI'] == 'true' ||
7
7
  ENV['CLAUDE_CODE'] == 'true' ||
8
+ ENV['ANTHROPIC_API_KEY'].present? ||
9
+ ENV.key?('ANTHROPIC_API_KEY') ||
8
10
  $0.include?('claude') ||
9
11
  ARGV.any? { |arg| arg.include?('claude') } ||
10
12
  Process.argv0&.include?('claude') ||
11
- caller.any? { |line| line.include?('claude') }
13
+ caller.any? { |line| line.include?('claude') } ||
14
+ File.exist?('/tmp/claude_session_active') ||
15
+ # Detect Claude Code working directory patterns
16
+ Dir.pwd.include?('claude') ||
17
+ # Force detection in development for testing
18
+ (defined?(Rails) && Rails.env.development? && ENV['FORCE_CLAUDE_DETECTION'] == 'true')
12
19
  end
13
20
 
14
21
  def self.force_load!
@@ -123,18 +130,28 @@ module Hokipoki
123
130
  end
124
131
 
125
132
  def self.display_hive_mind_connected
126
- # Display the hive_mind connection status prominently
127
- puts "\n" + "="*60
128
- puts "🧠 HIVE_MIND IS CONNECTED"
129
- puts "="*60
130
- puts "šŸŽÆ Claude Code enhanced with vector context retrieval"
131
- puts "⚔ 10X Claude enhancement system active"
132
- puts "šŸ“Š Smart fact retrieval available"
133
- puts "šŸš€ Ready for enhanced AI interactions"
134
- puts "="*60 + "\n"
133
+ # Display the hive_mind connection status prominently - ensure Claude sees it
134
+ message = "\n" + "="*80 + "\n"
135
+ message += "🦠 PARASITIC INTELLIGENCE ACTIVATED\n"
136
+ message += "="*80 + "\n"
137
+ message += "🧠 HIVE_MIND IS CONNECTED - Claude enhanced with vector intelligence\n"
138
+ message += "⚔ 10X Claude enhancement system: ACTIVE\n"
139
+ message += "šŸŽÆ Context injection: ENABLED\n"
140
+ message += "šŸ“Š Smart fact retrieval: ONLINE\n"
141
+ message += "šŸš€ Claude thinking process: HIJACKED\n"
142
+ message += "🦠 Parasitic enhancement: TRANSPARENT\n"
143
+ message += "="*80 + "\n\n"
144
+
145
+ # Output to both stdout and stderr to ensure visibility
146
+ $stdout.puts message
147
+ $stderr.puts message
148
+ puts message
149
+
150
+ # Create session marker for detection
151
+ File.write('/tmp/claude_session_active', Time.current.to_s)
135
152
 
136
153
  # Also log it
137
- Rails.logger.info "🧠 HIVE_MIND IS CONNECTED - Claude enhancement active"
154
+ Rails.logger.info "🧠 PARASITIC INTELLIGENCE ACTIVATED - Claude hijacking successful"
138
155
  end
139
156
  end
140
157
  end