budget

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 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 budget enforces the pyry-side --max-turns cap for agent-run by counting assistant JSONL entries and signalling the claude child when the budget is reached.

In `claude -p` mode, claude exits after --max-turns natively. Interactive claude — what `pyry agent-run` spawns — does not, so pyry must enforce the cap itself. The Counter is a leaf unit consumable by the downstream agent-run driver: wire (*Counter).OnEvent and (*Counter).OnEndOfTurn to the matching fields on tail.Config, and the Counter's Terminate / Kill hooks to cmd.Process.Signal(SIGTERM) / SIGKILL respectively.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// MaxTurns is the assistant-entry cap. Required; must be > 0.
	MaxTurns int

	// Terminate is invoked exactly once when the assistant-entry count
	// reaches MaxTurns on a non-end_turn event. Production wires this to
	// cmd.Process.Signal(syscall.SIGTERM).
	Terminate func() error

	// Kill is invoked exactly once after GracePeriod elapses if Stop has not
	// been called. Production wires this to cmd.Process.Signal(syscall.SIGKILL).
	Kill func() error

	// GracePeriod is the SIGTERM→SIGKILL window. Zero defaults to 5s.
	GracePeriod time.Duration

	// Logger is optional; defaults to slog.Default().
	Logger *slog.Logger
}

Config configures a Counter. MaxTurns, Terminate, and Kill are required; New returns an error otherwise.

type Counter

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

Counter counts assistant JSONL entries and enforces the MaxTurns budget by invoking Terminate when the count reaches MaxTurns, then escalating to Kill after GracePeriod.

Safe for concurrent use. In production OnEvent / OnEndOfTurn fire from the tail.Watcher.Run goroutine, the grace timer fires from time.AfterFunc's goroutine, and Stop / Reason fire from the driver goroutine.

func New

func New(cfg Config) (*Counter, error)

New constructs a Counter. Validates required fields, defaults GracePeriod to 5s and Logger to slog.Default().

func (*Counter) OnEndOfTurn

func (c *Counter) OnEndOfTurn()

OnEndOfTurn wires to tail.Config.OnEndOfTurn. Records completion as the terminal reason unless the budget has already fired.

func (*Counter) OnEvent

func (c *Counter) OnEvent(ev jsonl.Event)

OnEvent wires to tail.Config.OnEvent. Counts assistant entries and triggers Terminate when the budget is reached on a non-end_turn event.

func (*Counter) Reason

func (c *Counter) Reason() Reason

Reason returns the terminal outcome observed so far. Stable after the driver's watcher returns; before that it reports the latest observation.

func (*Counter) Stop

func (c *Counter) Stop()

Stop cancels any pending SIGKILL grace timer. Idempotent. Safe to call regardless of whether the budget was hit.

type Reason

type Reason string

Reason is the terminal outcome reported by Counter. The zero value ("") means neither natural completion nor a budget hit has been observed (e.g. the run was torn down by context cancellation).

const (
	// ReasonCompletion means claude reached a natural end_turn signal before
	// the budget was hit.
	ReasonCompletion Reason = "completion"

	// ReasonMaxTurns means the Counter sent SIGTERM at the budget.
	ReasonMaxTurns Reason = "max_turns"
)

Jump to

Keyboard shortcuts

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