Class: LLM::LoopGuard
- Inherits:
-
Object
- Object
- LLM::LoopGuard
- Defined in:
- lib/llm/loop_guard.rb
Overview
LLM::LoopGuard is the built-in implementation of llm.rb’s ‘guard` capability.
A guard is a context-level supervisor for agentic execution. It can inspect the current runtime state and return a warning string when pending tool work should be blocked before the loop keeps going.
LLM::LoopGuard detects when a context is repeating the same tool-call pattern instead of making progress. It is directly inspired by General Intelligence Systems’ Brute runtime and its doom-loop detection approach.
The public interface is intentionally small:
-
‘call(ctx)` returns `nil` when no intervention is needed
-
‘call(ctx)` returns a warning string when pending tool execution should be blocked
LLM::Context can use that warning to return in-band LLM::GuardError tool errors, and LLM::Agent enables this guard by default through its wrapped context.
Brute is MIT licensed. The relevant license grant is:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
Constant Summary collapse
- DEFAULT_THRESHOLD =
The default number of repeated tool-call patterns required before the guard intervenes.
3
Instance Attribute Summary collapse
-
#threshold ⇒ Integer
readonly
Returns the repetition threshold.
Instance Method Summary collapse
-
#call(ctx) ⇒ String?
Checks the current context for repeated tool-call patterns.
-
#initialize(config = {}) ⇒ LoopGuard
constructor
A new instance of LoopGuard.
Constructor Details
#initialize(config = {}) ⇒ LoopGuard
Returns a new instance of LoopGuard.
50 51 52 |
# File 'lib/llm/loop_guard.rb', line 50 def initialize(config = {}) @threshold = config.fetch(:threshold, DEFAULT_THRESHOLD) end |
Instance Attribute Details
#threshold ⇒ Integer (readonly)
Returns the repetition threshold.
43 44 45 |
# File 'lib/llm/loop_guard.rb', line 43 def threshold @threshold end |
Instance Method Details
#call(ctx) ⇒ String?
Checks the current context for repeated tool-call patterns.
This method inspects assistant tool calls only. It reduces each call to a ‘[tool_name, arguments]` signature and checks whether the tail of the sequence is repeating.
65 66 67 68 |
# File 'lib/llm/loop_guard.rb', line 65 def call(ctx) repetitions = detect(ctx..to_a) repetitions ? warning(repetitions) : nil end |