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
Update the cache with a new fingerprint and system text.
-
#valid?(fingerprint) ⇒ Boolean
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).
43 44 45 46 47 |
# File 'lib/phronomy/context/context_version_cache.rb', line 43 def reset @fingerprint = nil @system_text = nil @system_tokens = 0 end |
#update(fingerprint:, system_text:) ⇒ Object
Update the cache with a new fingerprint and system text.
36 37 38 39 40 |
# File 'lib/phronomy/context/context_version_cache.rb', line 36 def update(fingerprint:, system_text:) @fingerprint = fingerprint @system_text = system_text.to_s @system_tokens = TokenEstimator.estimate(@system_text) end |
#valid?(fingerprint) ⇒ Boolean
Returns true when the given fingerprint matches the stored one.
28 29 30 |
# File 'lib/phronomy/context/context_version_cache.rb', line 28 def valid?(fingerprint) !@fingerprint.nil? && !@system_text.nil? && @fingerprint == fingerprint end |