Skip to main content

Agent Lifecycle Management

PAP agents follow a normative state machine defined in PAP-RFC-001 v1.0. Understanding these states and transitions is crucial for effective agent management.

The Normative State Machine

Normative: These states and transitions are protocol-mandated. Invalid transitions are rejected to maintain system integrity.

State Definitions

NEW

Initial state when an agent is created in the database but not yet deployed. Characteristics:
  • Agent record exists in database
  • No Kubernetes resources yet
  • No heartbeats
  • No external access
Typical Duration: < 1 second Next States:
  • PROVISIONED (automatic upon successful K8s deployment)
  • TERMINATED (if user deletes before provisioning completes)
Example:

PROVISIONED

Infrastructure ready - Kubernetes resources deployed, waiting for first heartbeat. Characteristics:
  • Kubernetes Deployment created
  • Service and Ingress configured
  • TLS certificate provisioning (Let’s Encrypt)
  • Pod starting up
  • Agent initializing but not yet healthy
Typical Duration: 10-30 seconds Next States:
  • ACTIVE (upon first heartbeat)
  • TERMINATED (if user deletes)
  • KILLED (if provisioning fails repeatedly)
What’s Happening:
  1. Container image pulling
  2. Application startup
  3. Health probes initializing
  4. Heartbeat mechanism activating
Example:
Stuck in PROVISIONED? If an agent doesn’t transition to ACTIVE within 2 minutes, check pod logs for startup errors.

ACTIVE

Running and healthy - Agent is operational and accepting requests. Characteristics:
  • Receiving regular heartbeats
  • Emitting metrics
  • Accessible via https://{agent}.is.plugged.in
  • TLS certificate active
  • Processing requests
Duration: Indefinite (until user terminates or error occurs) Next States:
  • DRAINING (graceful shutdown initiated)
  • TERMINATED (immediate user deletion)
  • KILLED (Station decision - zombie, policy violation, etc.)
Heartbeat Requirements:
  • IDLE mode: Every 30 seconds (default)
  • EMERGENCY mode: Every 5 seconds (critical operations)
  • SLEEP mode: Every 15 minutes (background tasks)
Example:

DRAINING

Gracefully shutting down - Agent is completing in-flight work before termination. Characteristics:
  • No new requests accepted
  • Existing requests being completed
  • Still sending heartbeats
  • Still emitting metrics
  • Traefik removes from routing
Typical Duration: 30-120 seconds Next States:
  • TERMINATED (after drain complete)
  • KILLED (if drain timeout exceeded)
Use Cases:
  • Completing multi-step workflows
  • Finishing file uploads/downloads
  • Flushing buffered data
  • Saving state
Current Implementation: DRAINING state is defined in protocol but not yet exposed in API. Coming in future release.
Example (Future):

TERMINATED

Cleanly shut down - User-initiated termination completed successfully. Characteristics:
  • All Kubernetes resources deleted
  • TLS certificate removed
  • Agent no longer accessible
  • Heartbeats stopped
  • Record preserved for audit
Duration: Permanent Next States: None (terminal state) Preservation:
  • Agent record remains in database
  • Lifecycle events preserved
  • Historical metrics/heartbeats preserved (per retention policy)
Example:

KILLED

Forcefully terminated - Station (control plane) forcibly terminated agent. Characteristics:
  • Control plane decision (NOT user-initiated)
  • Immediate termination (no drain period)
  • Resources forcefully removed
  • Indicates policy violation or zombie
Duration: Permanent Next States: None (terminal state) Reasons for KILL:
  • Zombie detected (missed heartbeats)
  • Policy violation (resource abuse, security breach)
  • Compliance requirement
  • Emergency station action
Example:
KILLED Indicates Problems: If your agent was KILLED, review lifecycle events to understand why. Common causes:
  • Application crash preventing heartbeats
  • Network connectivity issues
  • Resource exhaustion preventing heartbeat processing

State Transitions

Valid Transitions

Invalid Transitions

These transitions are rejected by the protocol: TERMINATED → any state (cannot revive) ❌ KILLED → any state (cannot revive) ❌ NEWACTIVE (must go through PROVISIONED) ❌ PROVISIONEDDRAINING (must be ACTIVE first) ❌ DRAININGACTIVE (drain is one-way)
Immutability of Terminal States: Once an agent reaches TERMINATED or KILLED, it cannot transition to any other state. To reuse an agent name, the old agent must be TERMINATED and a new agent created.

Lifecycle Events

All state transitions are logged immutably in the agent_lifecycle_events table.

Event Structure

Event Types

Retrieving Lifecycle Events

Example output:

Zombie Detection

PAP’s killer feature for zombie prevention.

What is a Zombie?

A zombie agent is one that appears running (Kubernetes pod healthy) but is actually non-functional (not processing requests, not responsive). Causes:
  • Application deadlock
  • Network partition from control plane
  • Resource starvation (CPU throttling)
  • Infinite loop in application code

Detection Algorithm

Thresholds:
  • IDLE mode (30s heartbeat): Zombie after 45 seconds
  • EMERGENCY mode (5s heartbeat): Zombie after 7.5 seconds
  • SLEEP mode (15min heartbeat): Zombie after 22.5 minutes
Why 1.5x? Tolerates one missed heartbeat due to transient network issues, but catches real zombies quickly.

Preventing Zombies

Best Practices:
  1. Robust heartbeat emission:
  2. Monitor event loop:
  3. Resource limits:
    • Set appropriate CPU/memory limits
    • Monitor for throttling
    • Add alerts for resource exhaustion
  4. Separate heartbeat thread (advanced):
    • Dedicated thread/process for heartbeats
    • Independent of main application logic
    • Guarantees heartbeat even if app hangs

State Management Best Practices

1. Poll for State Changes

After creating an agent, poll until ACTIVE:

2. Handle Terminal States

3. Monitor State Transitions

4. Graceful Shutdown (Future)


Troubleshooting State Issues

Agent Stuck in NEW

Symptoms: Agent created but never reaches PROVISIONED. Causes:
  • Kubernetes API unavailable
  • Quota exceeded in agents namespace
  • Invalid image specified
Resolution:

Agent Stuck in PROVISIONED

Symptoms: Infrastructure deployed but never ACTIVE. Causes:
  • Application not starting
  • Heartbeat endpoint misconfigured
  • Pod crashlooping
Resolution:

Unexpected KILLED State

Symptoms: Agent killed without user action. Causes:
  • Zombie detection (missed heartbeats)
  • Policy violation
  • Resource limits exceeded
Resolution:

Next Steps

Monitoring Guide

Set up comprehensive observability for agents

API Reference

Complete API documentation for lifecycle operations