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 into the loop draining the tuidriver.TailJSONL channel, call (*Counter).OnEndOfTurn when tuidriver.IsEndTurn fires for that entry, and wire 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 agent-run caller's goroutine that drains the tuidriver.TailJSONL channel, the grace timer fires from time.AfterFunc's goroutine, and Stop / Reason fire from the driver goroutine.
func New ¶
New constructs a Counter. Validates required fields, defaults GracePeriod to 5s and Logger to slog.Default().
func (*Counter) OnEndOfTurn ¶
func (c *Counter) OnEndOfTurn()
OnEndOfTurn records completion as the terminal reason unless the budget has already fired. Caller invokes once per entry for which tuidriver.IsEndTurn returns true, after OnEvent has been called for the same entry.
func (*Counter) OnEvent ¶
func (c *Counter) OnEvent(entry tuidriver.JSONLEntry)
OnEvent counts assistant entries and triggers Terminate when the budget is reached. Caller invokes OnEvent once per entry surfaced by tuidriver.TailJSONL. End-of-turn classification is the caller's job — it calls OnEndOfTurn separately when tuidriver.IsEndTurn(entry) is true. The budget check fires synchronously here for every assistant entry, including one that is itself end-of-turn: at the boundary (MaxTurns reached exactly at the end-of-turn entry), max_turns wins because OnEvent grabs the reason first and the subsequent OnEndOfTurn cannot overwrite a non-empty reason.