loop

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package loop implements the loop detector: ≥ K tool.pre events with the same tool and normalized-similar input within T minutes ⇒ one alert per episode. This is the "a session in a loop drains your budget" feature (traceability #1).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Describe

func Describe(tool string, input map[string]json.RawMessage) string

Describe renders a short human-readable form of a tool call for banners.

func Signature

func Signature(tool string, payload json.RawMessage) (sig, sample string)

Signature normalizes a tool call into a similarity key: tool name + the tool_input with volatile parts (numbers, hashes, temp paths, whitespace) collapsed. Two calls that differ only in a retry counter, a line number, or a timestamp share a signature — which is exactly what a real loop looks like.

Types

type Alert

type Alert struct {
	Kind      string    `json:"kind"` // "loop"
	SessionID string    `json:"session_id"`
	Tool      string    `json:"tool"`
	Count     int       `json:"count"`
	WindowMin int       `json:"window_min"`
	Sample    string    `json:"sample"` // human-readable sample of the repeated input
	FirstTs   time.Time `json:"first_ts"`
	LastTs    time.Time `json:"last_ts"`
	Ts        time.Time `json:"ts"`
}

Alert is what the detector emits (WS "alert" frame).

type Detector

type Detector struct {
	K      int
	Window time.Duration
	// Cooldown suppresses repeat alerts for the same (session, signature) until
	// the loop stops for this long — one alert per episode, not per event.
	Cooldown time.Duration
	// contains filtered or unexported fields
}

Detector keeps a sliding window of recent tool.pre signatures per session.

func New

func New(k int, window time.Duration) *Detector

New creates a detector with the spec defaults K=5, T=3 minutes when zero.

func (*Detector) Forget

func (d *Detector) Forget(sessionID string)

Forget drops a session's window (session ended).

func (*Detector) Observe

func (d *Detector) Observe(ev event.Event) *Alert

Observe feeds a stored event. It returns an Alert (non-nil) when a loop is detected for the first time in an episode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL