Pi’s agent loop is a stateless function: it receives a context snapshot and a config, emits events, and returns new messages without remembering anything between calls. A separate Agent class owns the transcript, steering queues, and lifecycle, passing snapshots in and committing only completed messages out. The split makes the orchestration logic independently testable — tests drive it directly with a mock stream function — while stateful concerns like interruption and cancellation live in the wrapper.

Related: