Class: LLM::Tracer::Registry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/tracer/registry.rb

Overview

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

LLM::Tracer::Registry counts the scopes that are open for each tracer.

The count belongs to the tracer, not to the thread that opened a scope. A tool runs on a thread of its own and opens a scope there for the turn's tracer, and a count kept per thread would let that scope look like the outermost one: it would end the tracer while the turn is still running, and every write after it would fail.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ LLM::Tracer::Registry

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.



18
19
20
21
# File 'lib/llm/tracer/registry.rb', line 18

def initialize
  @counts = ObjectSpace::WeakMap.new
  @mutex  = Mutex.new
end

Instance Method Details

#enter(tracer) ⇒ LLM::Tracer::Registry

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.

Records that a scope was opened for a tracer.

Parameters:

Returns:



28
29
30
31
32
33
# File 'lib/llm/tracer/registry.rb', line 28

def enter(tracer)
  @mutex.synchronize do
    @counts[tracer] = count(tracer) + 1
  end
  self
end

#exit(tracer)

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.

This method returns an undefined value.

Records that a scope was closed, and tells the tracer it is finished when it was the last one that was open.

The count is brought up to date under the lock, and the tracer is told after the lock is released: a tracer that calls back into the provider from on_exit would otherwise deadlock against itself.

An exit without a matching enter is ignored: no scope is open, so there is nothing to end, and the tracer is not told it is finished when no scope was holding it.

Parameters:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/llm/tracer/registry.rb', line 48

def exit(tracer)
  last = @mutex.synchronize do
    open = count(tracer)
    if open.zero?
      false
    elsif open == 1
      forget(tracer)
      true
    else
      @counts[tracer] = open - 1
      false
    end
  end
  tracer.on_exit if last
end