converge

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package converge implements convergence monitoring for agent loops. The Monitor is a pure function over events — it takes no I/O and emits Verdicts. The runtime consults it each iteration to detect:

  1. Oscillation: the same (tool, argsHash) signature appears k times within a sliding window of w iterations.

  2. ZeroProgress: the state hash (from LoopFinished/IterationStarted events) is unchanged for k consecutive iterations.

  3. DepthRunaway: spawn depth exceeds max (reserved for SP5).

Responses:

  • Warn: record a warning; continue.
  • Redirect: inject a ConvergenceWarning event that the transition can inspect.
  • Terminate: return Stuck outcome.

Index

Constants

View Source
const (
	DetectorOscillation  = "oscillation"
	DetectorZeroProgress = "zero-progress"
	DetectorDepth        = "depth"
)

Detector names identify which detector fired.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// OscillationK is the number of identical (tool,args) signatures in the window before firing.
	// Default: 3.
	OscillationK int
	// OscillationWindow is the size of the sliding window in iterations.
	// Default: 10.
	OscillationWindow int
	// ZeroProgressK is how many consecutive iterations with the same state hash before firing.
	// Default: 5.
	ZeroProgressK int
	// MaxDepth is the maximum spawn depth (SP5). 0 = disabled.
	MaxDepth int
	// Response controls what happens when any detector fires.
	// Default: Terminate.
	Response Response

	// OscillationResponse overrides Response for oscillation specifically (optional).
	// Zero value means use Response.
	OscillationResponse *Response
	// ZeroProgressResponse overrides Response for zero-progress specifically (optional).
	ZeroProgressResponse *Response
}

Config configures the convergence thresholds.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}

Monitor tracks convergence state. It is NOT thread-safe; the runner calls it sequentially from within the main loop.

func NewMonitor

func NewMonitor(cfg Config) *Monitor

NewMonitor creates a Monitor with the given config. Zero-value Config fields are filled from DefaultConfig.

func (*Monitor) ObserveIteration

func (m *Monitor) ObserveIteration(stateHash [32]byte) Verdict

ObserveIteration is called each time an iteration starts. stateHash is the SHA-256 of the serialized loop state at iteration start. Returns a Verdict if a zero-progress condition fires; empty Verdict otherwise.

func (*Monitor) ObserveToolCall

func (m *Monitor) ObserveToolCall(tool string, args []byte) Verdict

ObserveToolCall is called each time a tool is called. It checks for oscillation (repeated identical (tool,args) signatures). Returns a Verdict if the oscillation detector fires; empty Verdict otherwise.

func (*Monitor) Reset

func (m *Monitor) Reset()

Reset clears all detector state. Used for replay or fork.

type Response

type Response int

Response describes what the monitor instructs the runner to do.

const (
	// Warn logs a warning but takes no action.
	Warn Response = iota
	// Redirect injects a ConvergenceWarning event into the log so the transition
	// can inspect it and change strategy.
	Redirect
	// Terminate causes the runner to finish the loop with a Stuck outcome.
	Terminate
)

type ToolSignature

type ToolSignature struct {
	Tool     string
	ArgsHash [32]byte
}

ToolSignature is the key used by the oscillation detector.

type Verdict

type Verdict struct {
	// Fired is true when any detector has fired.
	Fired bool
	// Detector is the name of the detector that fired.
	Detector string
	// Reason is a human-readable explanation.
	Reason string
	// Count is how many times the condition has been observed.
	Count int
	// Response is the action the runtime should take.
	Response Response
}

Verdict is returned by Monitor.Observe when a detector fires.

Jump to

Keyboard shortcuts

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