Class: RubynCode::Skills::TtlManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/skills/ttl_manager.rb

Overview

Manages skill TTL (time-to-live) and size caps. Skills injected into context are tracked with a turn counter; once a skill exceeds its TTL it is marked for ejection during the next compaction pass.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULT_TTL =

turns

5
MAX_SKILL_TOKENS =

tokens

800
CHARS_PER_TOKEN =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTtlManager

Returns a new instance of TtlManager.



21
22
23
24
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 21

def initialize
  @entries = {}
  @current_turn = 0
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



19
20
21
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 19

def entries
  @entries
end

Instance Method Details

#eject_expired!Object

Remove expired skills and return their names.



65
66
67
68
69
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 65

def eject_expired!
  expired = expired_skills
  expired.each { |name| @entries.delete(name) }
  expired
end

#expired_skillsObject

Returns names of skills that have exceeded their TTL.



60
61
62
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 60

def expired_skills
  @entries.select { |_, entry| entry.expired?(@current_turn) }.keys
end

#register(name, content, ttl: DEFAULT_TTL) ⇒ String

Register a loaded skill with optional TTL override.

Parameters:

  • name (String)

    skill name

  • content (String)

    skill content

  • ttl (Integer) (defaults to: DEFAULT_TTL)

    turns before expiry (default 5)

Returns:

  • (String)

    content, possibly truncated to size cap



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 37

def register(name, content, ttl: DEFAULT_TTL)
  truncated = enforce_size_cap(content)
  token_count = estimate_tokens(truncated)

  @entries[name] = Entry.new(
    name: name,
    loaded_at_turn: @current_turn,
    ttl: ttl,
    token_count: token_count,
    last_referenced_turn: @current_turn
  )

  truncated
end

#statsObject

Returns stats for the analytics dashboard.



77
78
79
80
81
82
83
84
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 77

def stats
  {
    loaded_skills: @entries.size,
    total_tokens: total_tokens,
    expired: expired_skills.size,
    current_turn: @current_turn
  }
end

#tick!Object

Advance the turn counter. Call this once per agent loop iteration.



27
28
29
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 27

def tick!
  @current_turn += 1
end

#total_tokensObject

Returns total tokens used by currently loaded skills.



72
73
74
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 72

def total_tokens
  @entries.values.sum(&:token_count)
end

#touch(name) ⇒ Object

Mark a skill as recently referenced (resets its TTL countdown).



53
54
55
56
57
# File 'lib/rubyn_code/skills/ttl_manager.rb', line 53

def touch(name)
  return unless @entries.key?(name)

  @entries[name] = @entries[name].with(last_referenced_turn: @current_turn)
end