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



45
46
47
48
49
# File 'lib/phronomy/context/context_version_cache.rb', line 45

def reset
  @fingerprint = nil
  @system_text = nil
  @system_tokens = 0
end

#update(fingerprint:, system_text:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



38
39
40
41
42
# File 'lib/phronomy/context/context_version_cache.rb', line 38

def update(fingerprint:, system_text:)
  @fingerprint = fingerprint
  @system_text = system_text.to_s
  @system_tokens = TokenEstimator.estimate(@system_text)
end

#valid?(fingerprint) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true when the given fingerprint matches the stored one.

Parameters:

  • fingerprint (String)

    SHA-256 hex digest to compare

Returns:

  • (Boolean)


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

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