Class: Phronomy::Context::ContextVersionCache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeContextVersionCache

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

#fingerprintString? (readonly)

Returns last stored fingerprint.

Returns:

  • (String, nil)

    last stored fingerprint



21
22
23
# File 'lib/phronomy/context/context_version_cache.rb', line 21

def fingerprint
  @fingerprint
end

#system_textString? (readonly)

Returns cached system prompt text.

Returns:

  • (String, nil)

    cached system prompt text



24
25
26
# File 'lib/phronomy/context/context_version_cache.rb', line 24

def system_text
  @system_text
end

#system_tokensInteger (readonly)

Returns estimated token count of #system_text.

Returns:

  • (Integer)

    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

#resetObject

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.

Parameters:

  • fingerprint (String)

    new SHA-256 hex digest

  • system_text (String)

    fully assembled system prompt 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.

Parameters:

  • fingerprint (String)

    SHA-256 hex digest to compare

Returns:

  • (Boolean)


37
38
39
# File 'lib/phronomy/context/context_version_cache.rb', line 37

def valid?(fingerprint)
  !@fingerprint.nil? && @fingerprint == fingerprint
end