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 per agent instance.
The cache is keyed by a SHA-256 fingerprint computed from the agent's instruction text and the content of all registered static knowledge sources. When the fingerprint matches the stored value the previously assembled system_text is reused without re-fetching any sources.
A cache miss (fingerprint changed or first call) triggers a full rebuild: instruction + static-knowledge XML tags are concatenated and the result is stored alongside the new fingerprint.
Each agent instance holds one cache object. The cache persists across
invoke calls on the same instance, which is the typical usage pattern
for long-running agents.
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.
29 30 31 |
# File 'lib/phronomy/context/context_version_cache.rb', line 29 def initialize reset end |
Instance Attribute Details
#fingerprint ⇒ String? (readonly)
Returns last stored fingerprint.
21 22 23 |
# File 'lib/phronomy/context/context_version_cache.rb', line 21 def fingerprint @fingerprint end |
#system_text ⇒ String? (readonly)
Returns cached system prompt text.
24 25 26 |
# File 'lib/phronomy/context/context_version_cache.rb', line 24 def system_text @system_text end |
#system_tokens ⇒ Integer (readonly)
Returns estimated token count of #system_text.
27 28 29 |
# File 'lib/phronomy/context/context_version_cache.rb', line 27 def system_tokens @system_tokens end |
Instance Method Details
#reset ⇒ Object
Clear all cached values (used for testing and forced invalidation).
52 53 54 55 56 |
# File 'lib/phronomy/context/context_version_cache.rb', line 52 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.
45 46 47 48 49 |
# File 'lib/phronomy/context/context_version_cache.rb', line 45 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.
37 38 39 |
# File 'lib/phronomy/context/context_version_cache.rb', line 37 def valid?(fingerprint) !@fingerprint.nil? && @fingerprint == fingerprint end |