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
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

#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).



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).

Parameters:

  • fingerprint (String)

    new SHA-256 hex digest

  • system_text (String)

    fully assembled system prompt text



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).

Parameters:

  • fingerprint (String)

    SHA-256 hex digest to compare

Returns:

  • (Boolean)


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