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 32 33 34 |
# File 'lib/phronomy/context/context_version_cache.rb', line 29 def initialize @mutex = Mutex.new @fingerprint = nil @system_text = nil @system_tokens = 0 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).
64 65 66 67 68 69 70 |
# File 'lib/phronomy/context/context_version_cache.rb', line 64 def reset @mutex.synchronize do @fingerprint = nil @system_text = nil @system_tokens = 0 end end |
#update(fingerprint:, system_text:) ⇒ Object
Update the cache with a new fingerprint and system text. All three assignments are performed atomically under a mutex so that concurrent readers never observe a partial state (Issue #55).
55 56 57 58 59 60 61 |
# File 'lib/phronomy/context/context_version_cache.rb', line 55 def update(fingerprint:, system_text:) @mutex.synchronize do @fingerprint = fingerprint @system_text = system_text.to_s @system_tokens = TokenEstimator.estimate(@system_text) end end |
#valid?(fingerprint) ⇒ Boolean
Returns true when the given fingerprint matches the stored one. The check is performed under a mutex so that a concurrent #update cannot expose a partially-written state where fingerprint is new but system_text is still nil (Issue #55).
43 44 45 46 47 |
# File 'lib/phronomy/context/context_version_cache.rb', line 43 def valid?(fingerprint) @mutex.synchronize do !@fingerprint.nil? && !@system_text.nil? && @fingerprint == fingerprint end end |