Skip to main content

Overview

The Lifecycle Manager is the core orchestration engine that polls sessions, detects state transitions, emits events, and triggers automated reactions. It implements a state machine that tracks sessions through their entire lifecycle from spawning to completion. Key responsibilities:
  • Periodically poll all active sessions
  • Detect state transitions (spawning → working → pr_open → etc.)
  • Emit events on transitions
  • Execute automated reactions (auto-fix CI failures, review comments, etc.)
  • Escalate to human notification when auto-handling fails
The Lifecycle Manager runs as a background polling loop, typically checking sessions every 30 seconds. It’s the primary automation layer between agents and humans.

Architecture

Methods

start

Start the lifecycle polling loop.
number
default:"30000"
Polling interval in milliseconds. Default is 30 seconds.
Example:
The polling loop includes a re-entrancy guard - if a previous poll is still running, subsequent polls are skipped until it completes.

stop

Stop the lifecycle polling loop.
Example:
Always call stop() before shutting down the orchestrator to clean up the polling interval.

check

Force an immediate state check for a specific session, bypassing the polling schedule.
string
required
The session ID to check (e.g., “my-app-1”).
Example:
Use cases:
  • After manually updating session metadata
  • When you need immediate reaction to state changes
  • For testing and debugging

getStates

Get a snapshot of all tracked session states.
Map<SessionId, SessionStatus>
Map of session IDs to their current status.
Example:

State Machine

The lifecycle manager implements a state machine with the following transitions:

Session Statuses

Status Detection

The lifecycle manager determines session status by polling multiple sources:

1. Runtime Liveness

2. Agent Activity

Prefers JSONL-based detection (reads agent’s session files directly):
Falls back to terminal output parsing if JSONL is unavailable.

3. PR Detection

4. CI Status

5. Review State

Reaction System

Reactions are automated responses to state transitions. They can send messages to agents, notify humans, or trigger actions like auto-merge.

Reaction Configuration

Reaction Actions

send-to-agent

Sends a message to the agent runtime:
  • Retries on failure (configurable via retries)
  • Escalates to human after max retries or timeout
  • Non-blocking (session continues running)

notify

Sends a push notification to configured notifiers:

auto-merge

Triggers automatic PR merge:

Escalation

Reactions can escalate to human notification after:
number
Maximum number of retry attempts before escalating.
number | string
Escalate after N attempts (number) or duration (string like “30m”).
Example:

Reaction Tracking

The lifecycle manager tracks attempts per session:
  • Reset when session transitions to a new state
  • Persists across polling cycles
  • Cleaned up when session is killed/merged

Events

The lifecycle manager emits events on state transitions. Events are routed to notifiers based on priority.

Event Types

Event Structure

Example:

Notification Routing

Events are routed to notifiers based on priority:
Priority levels:
EventPriority
Critical issues requiring immediate attention (stuck, needs_input, exited).
EventPriority
Actionable events (approved, ready to merge, merged).
EventPriority
Issues that may need attention (CI failed, changes requested).
EventPriority
Informational updates (session spawned, working).

Error Handling

Graceful Degradation

The lifecycle manager continues operating even when individual checks fail:

Re-entrancy Guard

Prevents overlapping poll cycles:

State Cleanup

Prunes stale entries when sessions are deleted:

Complete Example

See Also