loop

package
v0.189.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 10 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 Ignored added in v0.150.0

func Ignored(tool string, input json.RawMessage) bool

Ignored reports whether the detector skips a call: a read-only tool, or a browser step that only looks or moves (browserObservation). input is the call's tool_input. A replay of stored calls uses it to skip exactly what the live alert skips.

func InputSignature added in v0.105.0

func InputSignature(tool string, input json.RawMessage) string

InputSignature is Signature for a call's tool_input alone, without the banner text: the same key Signature gives the payload that wraps it. A scan over history needs only the key, and reading tool_input out of the payload is what keeps the rest of it (a tool's whole response) out of memory.

func ReadOnly added in v0.70.0

func ReadOnly(tool string) bool

ReadOnly reports whether the detector ignores a tool, so a replay of stored calls (the Week card's longest loop) skips exactly what the live alert skips.

func ReadOnlyTools added in v0.70.0

func ReadOnlyTools() []string

ReadOnlyTools lists them, sorted.

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"`
	// TaxUSD is what the repeated calls paid to re-read the conversation --
	// sum of each call's context at cache-read rates, and nothing else.
	//
	// This is deliberately not "what the loop cost". That number was tried
	// twice and was wrong both times: the session total read as the loop's
	// price, and the spend inside the loop's window is mostly the useful work
	// happening alongside it. The tax is neither. It is one term, attributable
	// to these calls exactly, and it must stay labelled as the tax rather than
	// as the loop's cost (rule 6).
	//
	// Zero means not computed -- an alert whose calls carried no usage.
	TaxUSD float64 `json:"tax_usd,omitempty"`
	// IsolatedUSD is what those same calls would have paid for context inside
	// a subagent. Informational: Caprock never acts on it, because the model
	// took the delegation nudge once in eight tries (spec section 6.4).
	IsolatedUSD float64 `json:"isolated_usd,omitempty"`
	// TaxPricedCalls is how many of the repeated calls the tax above covers,
	// out of Count. A call that arrived on the hook plane carries no message
	// id and so cannot be attached to the turn that paid for it -- 13% of all
	// calls on the owner's archive. When this is below Count the figure is an
	// understatement, and the UI says so rather than presenting it as whole.
	TaxPricedCalls int `json:"tax_priced_calls,omitempty"`
}

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 thread: the session's main thread, and each of its subagents apart.

A subagent's calls arrive under its parent's session id (hooks fire inside subagents, and the transcript files a sidechain under its parent), so one window per session added a subagent's edits to the parent's: the parent and three subagents fixing the same import read as one session repeating itself, and the parent was flagged for a loop it was not in. Each thread now counts only its own calls (lane); a subagent that really loops still raises the alert, on the session it works for.

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 windows, its subagents' included (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