state

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package state holds the per-agent runtime state machine. The machine is a tiny FSM driven by AgentEvent.Type values; consumers (UI, dashboard, idle timer) ask for the current state via Current().

Transitions are intentionally lenient: unexpected events don't reject — they just keep the current state. Real CLIs sometimes reorder events (delta before start, etc.), and crashing on every surprise would make agents fragile.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lifecycle

type Lifecycle int

Lifecycle is the high-level subprocess view that the Backends UI renders. Orthogonal to State (substate within an active spawn): State answers "what is the agent doing right now"; Lifecycle answers "is the subprocess alive, and if so what shape".

Transitions:

(zero)   → Spawning   when the pool starts a spawn
Spawning → Working    on the first event from the CLI
Working  → Idle       on Done / Error (substate flips to Idle too)
Idle     → Working    on the next event
any      → Killed     on subprocess exit (idle TTL, Stop, crash)

The Idle state runs an auto-kill countdown (LastActive + IdleTimeout → process killed); the UI renders that as a remaining-time badge.

const (
	// LifecycleSpawning: subprocess started, waiting for first stream
	// event. Renders "loading" in the UI.
	LifecycleSpawning Lifecycle = iota
	// LifecycleWorking: subprocess alive and currently processing a
	// turn. Substate (Thinking / RunningTool / Responding) gives the
	// detail.
	LifecycleWorking
	// LifecycleIdle: subprocess alive but no active turn — auto-kill
	// countdown is running.
	LifecycleIdle
	// LifecycleKilled: subprocess no longer alive. Reason is on the
	// spawn log's exit event.
	LifecycleKilled
)

func (Lifecycle) String

func (l Lifecycle) String() string

String returns the canonical short label used in JSON payloads, log lines, and CSS class hooks. Stable across the codebase.

type Machine

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

Machine is goroutine-safe. Apply is called from the parser-reader goroutine; Current / LastActive can be read from any goroutine.

func New

func New(now func() time.Time) *Machine

New returns a fresh machine in Idle. Caller can inject a clock for deterministic tests; pass nil for time.Now.

func (*Machine) Apply

func (m *Machine) Apply(ev event.AgentEvent) State

Apply transitions the machine based on an incoming event. Unknown event types are no-ops on state but still bump LastActive — the CLI is producing output, which means it's not idle.

Returns the resulting state for callers that want to log transitions.

func (*Machine) Current

func (m *Machine) Current() State

Current returns the current state.

func (*Machine) LastActive

func (m *Machine) LastActive() time.Time

LastActive returns the timestamp of the most recent event applied. The pool's idle timer compares this against IdleTimeoutSec to decide when to kill the subprocess.

func (*Machine) Lifecycle

func (m *Machine) Lifecycle() Lifecycle

Lifecycle returns the current high-level lifecycle. Read-only.

func (*Machine) MarkIdle

func (m *Machine) MarkIdle()

MarkIdle forces the machine back to Idle without touching LastActive. Used when the subprocess is killed externally (TTL, shutdown) so a stale Responding state doesn't linger in the UI.

func (*Machine) MarkKilled

func (m *Machine) MarkKilled()

MarkKilled flips the lifecycle to Killed. Pool calls this from the OnExit hook regardless of exit reason; the reason is recorded on the spawn log's exit event, not here.

func (*Machine) MarkSpawning

func (m *Machine) MarkSpawning()

MarkSpawning resets the machine to the Spawning lifecycle. Pool calls this when (re-)spawning a subprocess; idempotent.

type State

type State int

State is the per-agent runtime view. See agents-design.md §4.6 step 3.

const (
	// Idle: subprocess waiting for input, or not yet started.
	Idle State = iota
	// Thinking: parser saw a Thinking event.
	Thinking
	// RunningTool: parser saw a ToolUse event; gate is checking it.
	RunningTool
	// Responding: parser saw TextDelta — text is streaming back.
	Responding
)

func (State) String

func (s State) String() string

String for log lines / debug.

Jump to

Keyboard shortcuts

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