Class: Phronomy::Context::ContextVersionCache
- Inherits:
-
Object
- Object
- Phronomy::Context::ContextVersionCache
- Defined in:
- lib/phronomy/context/context_version_cache.rb
Overview
Caches the assembled static system prompt text keyed by a SHA-256 fingerprint of the agent's instructions + static knowledge content. Each instance is owned by one thread (stored in +Thread.current+).
Instance Attribute Summary collapse
-
#fingerprint ⇒ String?
readonly
Last stored fingerprint.
-
#system_text ⇒ String?
readonly
Cached system prompt text.
-
#system_tokens ⇒ Integer
readonly
Estimated token count of #system_text.
Instance Method Summary collapse
-
#initialize ⇒ ContextVersionCache
constructor
A new instance of ContextVersionCache.
-
#reset ⇒ Object
Clear all cached values (used for testing and forced invalidation).
-
#update(fingerprint:, system_text:) ⇒ Object
private
Update the cache with a new fingerprint and system text.
-
#valid?(fingerprint) ⇒ Boolean
private
Returns true when the given fingerprint matches the stored one.
Constructor Details
#initialize ⇒ ContextVersionCache
Returns a new instance of ContextVersionCache.
18 19 20 21 22 |
# File 'lib/phronomy/context/context_version_cache.rb', line 18 def initialize @fingerprint = nil @system_text = nil @system_tokens = 0 end |
Instance Attribute Details
#fingerprint ⇒ String? (readonly)
Returns last stored fingerprint.
10 11 12 |
# File 'lib/phronomy/context/context_version_cache.rb', line 10 def fingerprint @fingerprint end |
#system_text ⇒ String? (readonly)
Returns cached system prompt text.
13 14 15 |
# File 'lib/phronomy/context/context_version_cache.rb', line 13 def system_text @system_text end |
#system_tokens ⇒ Integer (readonly)
Returns estimated token count of #system_text.
16 17 18 |
# File 'lib/phronomy/context/context_version_cache.rb', line 16 def system_tokens @system_tokens end |
Instance Method Details
#reset ⇒ Object
Clear all cached values (used for testing and forced invalidation).
45 46 47 48 49 |
# File 'lib/phronomy/context/context_version_cache.rb', line 45 def reset @fingerprint = nil @system_text = nil @system_tokens = 0 end |
#update(fingerprint:, system_text:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update the cache with a new fingerprint and system text.
38 39 40 41 42 |
# File 'lib/phronomy/context/context_version_cache.rb', line 38 def update(fingerprint:, system_text:) @fingerprint = fingerprint @system_text = system_text.to_s @system_tokens = TokenEstimator.estimate(@system_text) end |
#valid?(fingerprint) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true when the given fingerprint matches the stored one.
29 30 31 |
# File 'lib/phronomy/context/context_version_cache.rb', line 29 def valid?(fingerprint) !@fingerprint.nil? && !@system_text.nil? && @fingerprint == fingerprint end |