Class: Aidp::Harness::DeprecationCache

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/harness/deprecation_cache.rb

Overview

Manages a dynamic cache of deprecated models detected at runtime When deprecation errors are detected from provider APIs, models are added to this cache with metadata (replacement, detected date, etc.)

This class is designed to be extractable to the agent-harness gem. It uses dependency injection for logging to decouple from AIDP.

Defined Under Namespace

Classes: CacheError

Constant Summary collapse

COMPONENT =

Component name for logging

"deprecation_cache"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_path: nil, root_dir: nil, logger: nil) ⇒ DeprecationCache

Initialize the deprecation cache.

All parameters are optional for backward compatibility. Existing callers that don't pass a logger will use AIDP's built-in logging.

Parameters:

  • cache_path (String, nil) (defaults to: nil)

    custom cache file path (optional)

  • root_dir (String, nil) (defaults to: nil)

    root directory for default cache path (optional)

  • logger (#log_info, #log_warn, #log_error, nil) (defaults to: nil)

    logger instance (optional, falls back to Aidp.log_* methods if nil)



31
32
33
34
35
36
37
# File 'lib/aidp/harness/deprecation_cache.rb', line 31

def initialize(cache_path: nil, root_dir: nil, logger: nil)
  @root_dir = root_dir || safe_root_dir
  @cache_path = cache_path || default_cache_path
  @cache_data = nil
  @logger = logger
  ensure_cache_directory
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



20
21
22
# File 'lib/aidp/harness/deprecation_cache.rb', line 20

def cache_path
  @cache_path
end

Instance Method Details

#add_deprecated_model(provider:, model_id:, replacement: nil, reason: nil) ⇒ Object

Add a deprecated model to the cache

Parameters:

  • provider (String)

    Provider name (e.g., "anthropic")

  • model_id (String)

    Deprecated model ID

  • replacement (String, nil) (defaults to: nil)

    Replacement model ID (if known)

  • reason (String, nil) (defaults to: nil)

    Deprecation reason/message



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aidp/harness/deprecation_cache.rb', line 44

def add_deprecated_model(provider:, model_id:, replacement: nil, reason: nil)
  load_cache unless @cache_data

  @cache_data["providers"][provider] ||= {}
  @cache_data["providers"][provider][model_id] = {
    "deprecated_at" => Time.now.iso8601,
    "replacement" => replacement,
    "reason" => reason
  }.compact

  save_cache
  log_info("added_deprecated_model", provider: provider, model: model_id, replacement: replacement)
end

#clear!Object

Clear all cached deprecations



109
110
111
112
113
# File 'lib/aidp/harness/deprecation_cache.rb', line 109

def clear!
  @cache_data = default_cache_structure
  save_cache
  log_info("cleared_all_deprecations")
end

#deprecated?(provider:, model_id:) ⇒ Boolean

Check if a model is deprecated

Parameters:

  • provider (String)

    Provider name

  • model_id (String)

    Model ID to check

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/aidp/harness/deprecation_cache.rb', line 62

def deprecated?(provider:, model_id:)
  load_cache unless @cache_data
  @cache_data.dig("providers", provider, model_id) != nil
end

#deprecated_models(provider:) ⇒ Array<String>

Get all deprecated models for a provider

Parameters:

  • provider (String)

    Provider name

Returns:

  • (Array<String>)

    List of deprecated model IDs



79
80
81
82
# File 'lib/aidp/harness/deprecation_cache.rb', line 79

def deprecated_models(provider:)
  load_cache unless @cache_data
  (@cache_data.dig("providers", provider) || {}).keys
end

#info(provider:, model_id:) ⇒ Hash?

Get full deprecation info for a model

Parameters:

  • provider (String)

    Provider name

  • model_id (String)

    Model ID

Returns:

  • (Hash, nil)

    Deprecation metadata or nil



103
104
105
106
# File 'lib/aidp/harness/deprecation_cache.rb', line 103

def info(provider:, model_id:)
  load_cache unless @cache_data
  @cache_data.dig("providers", provider, model_id)
end

#remove_deprecated_model(provider:, model_id:) ⇒ Object

Remove a model from the deprecated cache Useful if a model comes back or was incorrectly marked

Parameters:

  • provider (String)

    Provider name

  • model_id (String)

    Model ID to remove



88
89
90
91
92
93
94
95
96
97
# File 'lib/aidp/harness/deprecation_cache.rb', line 88

def remove_deprecated_model(provider:, model_id:)
  load_cache unless @cache_data
  return unless @cache_data.dig("providers", provider, model_id)

  @cache_data["providers"][provider].delete(model_id)
  @cache_data["providers"].delete(provider) if @cache_data["providers"][provider].empty?

  save_cache
  log_info("removed_deprecated_model", provider: provider, model: model_id)
end

#replacement_for(provider:, model_id:) ⇒ String?

Get replacement model for a deprecated model

Parameters:

  • provider (String)

    Provider name

  • model_id (String)

    Deprecated model ID

Returns:

  • (String, nil)

    Replacement model ID or nil



71
72
73
74
# File 'lib/aidp/harness/deprecation_cache.rb', line 71

def replacement_for(provider:, model_id:)
  load_cache unless @cache_data
  @cache_data.dig("providers", provider, model_id, "replacement")
end

#statsHash

Get cache statistics

Returns:

  • (Hash)

    Statistics about cached deprecations



117
118
119
120
121
122
123
124
# File 'lib/aidp/harness/deprecation_cache.rb', line 117

def stats
  load_cache unless @cache_data
  {
    providers: @cache_data["providers"].keys.sort,
    total_deprecated: @cache_data["providers"].sum { |_, models| models.size },
    by_provider: @cache_data["providers"].transform_values(&:size)
  }
end