Class: DSPy::Registry::SignatureRegistry

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/registry/signature_registry.rb

Overview

Registry for managing signature versions and deployments Provides version control, rollback capabilities, and deployment tracking

Defined Under Namespace

Classes: RegistryConfig, SignatureVersion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ SignatureRegistry

Returns a new instance of SignatureRegistry.



210
211
212
213
214
# File 'lib/dspy/registry/signature_registry.rb', line 210

def initialize(config: nil)
  @config = config || RegistryConfig.new
  setup_registry_directory
  load_or_create_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



207
208
209
# File 'lib/dspy/registry/signature_registry.rb', line 207

def config
  @config
end

Instance Method Details

#compare_versions(signature_name, version1, version2) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/dspy/registry/signature_registry.rb', line 444

def compare_versions(signature_name, version1, version2)
  versions = load_signature_versions(signature_name)
  v1 = versions.find { |v| v.version == version1 }
  v2 = versions.find { |v| v.version == version2 }

  return nil unless v1 && v2

  {
    version_1: {
      version: v1.version,
      created_at: v1.created_at.iso8601,
      performance_score: v1.performance_score,
      is_deployed: v1.is_deployed,
      configuration: v1.configuration
    },
    version_2: {
      version: v2.version,
      created_at: v2.created_at.iso8601,
      performance_score: v2.performance_score,
      is_deployed: v2.is_deployed,
      configuration: v2.configuration
    },
    comparison: {
      age_difference_hours: ((v1.created_at - v2.created_at) / 3600).round(2),
      performance_difference: (v1.performance_score || 0) - (v2.performance_score || 0),
      configuration_changes: compare_configurations(v1.configuration, v2.configuration)
    }
  }
end

#deploy_version(signature_name, version) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/dspy/registry/signature_registry.rb', line 270

def deploy_version(signature_name, version)
  emit_deploy_start_event(signature_name, version)

  begin
    versions = load_signature_versions(signature_name)
    target_version = versions.find { |v| v.version == version }

    return nil unless target_version

    # Backup current deployment if configured
    if @config.backup_on_deploy
      current_deployed = get_deployed_version(signature_name)
      if current_deployed
        create_deployment_backup(current_deployed)
      end
    end

    # Mark currently deployed version as previously deployed and undeploy all
    versions = versions.map do |v|
      if v.is_deployed
        # Add deployment history metadata
         = v..merge(was_deployed: true, last_deployed_at: Time.now.iso8601)
        SignatureVersion.new(
          signature_name: v.signature_name,
          version: v.version,
          configuration: v.configuration,
          metadata: ,
          program_id: v.program_id,
          is_deployed: false,
          performance_score: v.performance_score
        )
      else
        v.undeploy
      end
    end

    # Deploy target version
    target_index = versions.index { |v| v.version == version }
    versions[target_index] = target_version.deploy

    save_signature_versions(signature_name, versions)

    deployed_version = versions[target_index]
    emit_deploy_complete_event(deployed_version)
    deployed_version

  rescue => error
    emit_deploy_error_event(signature_name, version, error)
    nil
  end
end

#export_registry(export_path) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/dspy/registry/signature_registry.rb', line 476

def export_registry(export_path)
  registry_data = {
    exported_at: Time.now.iso8601,
    config: @config.to_h,
    signatures: {}
  }

  list_signatures.each do |signature_name|
    registry_data[:signatures][signature_name] = load_signature_versions(signature_name).map(&:to_h)
  end

  File.write(export_path, YAML.dump(registry_data))
  emit_export_event(export_path, list_signatures.size)
end

#get_deployed_version(signature_name) ⇒ Object



374
375
376
377
# File 'lib/dspy/registry/signature_registry.rb', line 374

def get_deployed_version(signature_name)
  versions = load_signature_versions(signature_name)
  versions.find(&:is_deployed)
end

#get_performance_history(signature_name) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/dspy/registry/signature_registry.rb', line 410

def get_performance_history(signature_name)
  versions = load_signature_versions(signature_name)
  versions_with_scores = versions.select { |v| v.performance_score }

  return { versions: [], trends: {} } if versions_with_scores.empty?

  sorted_versions = versions_with_scores.sort_by(&:created_at)

  {
    versions: sorted_versions.map do |v|
      {
        version: v.version,
        score: v.performance_score,
        created_at: v.created_at.iso8601,
        is_deployed: v.is_deployed
      }
    end,
    trends: {
      latest_score: sorted_versions.last.performance_score,
      best_score: versions_with_scores.map(&:performance_score).compact.max,
      worst_score: versions_with_scores.map(&:performance_score).compact.min,
      improvement_trend: calculate_improvement_trend(sorted_versions)
    }
  }
end

#import_registry(import_path) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/dspy/registry/signature_registry.rb', line 493

def import_registry(import_path)
  data = YAML.load_file(import_path, symbolize_names: true)
  imported_count = 0

  data[:signatures].each do |signature_name, versions_data|
    versions = versions_data.map { |v| SignatureVersion.from_h(v) }
    save_signature_versions(signature_name.to_s, versions)
    imported_count += 1
  end

  emit_import_event(import_path, imported_count)
end

#list_signaturesObject



387
388
389
390
391
# File 'lib/dspy/registry/signature_registry.rb', line 387

def list_signatures
  Dir.glob(File.join(@config.registry_path, "signatures", "*.yml")).map do |file|
    File.basename(file, ".yml")
  end
end

#list_versions(signature_name) ⇒ Object



381
382
383
# File 'lib/dspy/registry/signature_registry.rb', line 381

def list_versions(signature_name)
  load_signature_versions(signature_name)
end

#register_version(signature_name, configuration, metadata: {}, program_id: nil, version: nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/dspy/registry/signature_registry.rb', line 226

def register_version(signature_name, configuration, metadata: {}, program_id: nil, version: nil)
  emit_register_start_event(signature_name, version)

  begin
    version ||= generate_version_name if @config.auto_version

    signature_version = SignatureVersion.new(
      signature_name: signature_name,
      version: version,
      configuration: configuration,
      metadata: ,
      program_id: program_id
    )

    # Load existing versions
    versions = load_signature_versions(signature_name)
    
    # Check if version already exists
    if versions.any? { |v| v.version == version }
      raise ArgumentError, "Version #{version} already exists for signature #{signature_name}"
    end

    # Add new version
    versions << signature_version

    # Cleanup old versions if needed
    if @config.max_versions_per_signature > 0 && versions.size > @config.max_versions_per_signature
      versions = versions.sort_by(&:created_at).last(@config.max_versions_per_signature)
    end

    # Save versions
    save_signature_versions(signature_name, versions)

    emit_register_complete_event(signature_version)
    signature_version

  rescue => error
    emit_register_error_event(signature_name, version, error)
    raise
  end
end

#rollback(signature_name) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/dspy/registry/signature_registry.rb', line 324

def rollback(signature_name)
  emit_rollback_start_event(signature_name)

  begin
    versions = load_signature_versions(signature_name)
    
    # Find versions that have deployment history (previously deployed)
    # Look for versions with deployment metadata or that were deployed
    deployed_history = versions.select do |v|
      v.[:was_deployed] || v.is_deployed
    end.sort_by(&:created_at)

    # If we don't have deployment history, check if any versions exist
    if deployed_history.empty?
      # Look for the second newest version as fallback
      all_versions = versions.sort_by(&:created_at)
      if all_versions.size >= 2
        previous_version = all_versions[-2]
        result = deploy_version(signature_name, previous_version.version)
        if result
          emit_rollback_complete_event(result)
        end
        return result
      end
    elsif deployed_history.size >= 2
      # Get the previous deployed version (excluding currently deployed)
      current_deployed = versions.find(&:is_deployed)
      previous_versions = deployed_history.reject { |v| v.version == current_deployed&.version }
      
      if previous_versions.any?
        previous_version = previous_versions.last
        result = deploy_version(signature_name, previous_version.version)
        if result
          emit_rollback_complete_event(result)
        end
        return result
      end
    end

    emit_rollback_error_event(signature_name, "No previous version to rollback to")
    nil

  rescue => error
    emit_rollback_error_event(signature_name, error.message)
    nil
  end
end

#update_performance_score(signature_name, version, score) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/dspy/registry/signature_registry.rb', line 395

def update_performance_score(signature_name, version, score)
  versions = load_signature_versions(signature_name)
  target_index = versions.index { |v| v.version == version }

  return nil unless target_index

  versions[target_index] = versions[target_index].with_performance_score(score)
  save_signature_versions(signature_name, versions)

  emit_performance_update_event(versions[target_index])
  versions[target_index]
end