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

Instance Method Summary collapse

Constructor Details

#initializeContextVersionCache

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

#fingerprintString? (readonly)

Returns last stored fingerprint.

Returns:

  • (String, nil)

    last stored fingerprint



10
11
12
# File 'lib/phronomy/context/context_version_cache.rb', line 10

def fingerprint
  @fingerprint
end

#system_textString? (readonly)

Returns cached system prompt text.

Returns:

  • (String, nil)

    cached system prompt text



13
14
15
# File 'lib/phronomy/context/context_version_cache.rb', line 13

def system_text
  @system_text
end

#system_tokensInteger (readonly)

Returns estimated token count of #system_text.

Returns:

  • (Integer)

    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

#resetObject

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.

Parameters:

  • fingerprint (String)

    new SHA-256 hex digest

  • system_text (String)

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

Parameters:

  • fingerprint (String)

    SHA-256 hex digest to compare

Returns:

  • (Boolean)


28
29
30
# File 'lib/phronomy/context/context_version_cache.rb', line 28

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