Skip to main content
Version: MVP

Agent lifecycle

4 min readReferenceUpdated 2026-05-22

What you'll find here

The persistent agent session lifecycle introduced in v0.9.0: command surface, status values, transition rules, prompt-loop records, operator gating, and the relationship to one-shot task runs.

Command boundary

SurfacePurposePersistence
craik run executeExecute one bounded task run.craik.task_run, outputs, receipts, handoffs.
craik agent launchCreate a persistent agent session.craik.agent_session_state.
craik agent promptSend one prompt through the session provider.Task, run, outputs, receipts, handoff, session links, and craik.agent_session_event.
craik agent recoverMark a recoverable failure or perform reconnect/resume.Updates session status and redacted recovery metadata.
craik agent statusInspect one persistent session.Reports session HMAC status and may mark stale pid sessions failed.
craik agent stopStop an active persistent session.Clears pid and records stopped_at.
craik agent restartRestart a stopped or failed session.Clears stopped_at and records a new start time.
craik agent listList persisted sessions.Read-only.

Status values

craik agent status includes hmac_status for the persisted session record. verified means the stored bytes match the local receipt HMAC, tampered means a signed row failed verification, and unverified marks legacy rows written before session HMACs.

StatusMeaning
startingA session launch has started but is not ready.
runningA session is available for work.
idleA session is alive and waiting for work.
stoppingStop has begun but cleanup is not finished.
stoppedThe session was intentionally stopped.
failedThe session failed or a stored pid became stale.
auth_expiredThe provider credential context can no longer be used.
provider_unavailableThe provider route is temporarily unavailable.
sandbox_failedThe sandbox route failed before safe execution.

Active statuses are starting, running, idle, and stopping. Restartable statuses are stopped, failed, auth_expired, provider_unavailable, and sandbox_failed.

Transition rules

Launch
active state
Creates a new running foreground session. Launch refuses to replace an existing active session id.
Status
read with recovery check
Reads one session and marks stale pid-backed active sessions failed with a supervision note.
Stop
active to stopped
Only active sessions can stop. Stop clears pid, sets stopped_at, and appends the operator reason.
Restart
recoverable to running
Active sessions cannot restart. Restart clears stopped_at, refreshes lifecycle timestamps, and preserves provider references.
Recover
failure to reconnect/resume
Auth, provider, sandbox, stale pid, and stale endpoint failures record redacted recovery metadata before reconnect or resume.

Prompt loop

craik agent prompt SESSION_ID "prompt text" accepts prompts only for active sessions. The command checks that the active operator session matches the stored session subject and issuer before creating work. A prompt run creates a task in the session project, executes the provider-backed runner path, records provider receipts, writes a handoff, and updates the session with the active task id, active run id, receipt ids, handoff ids, and recovery metadata.

Prompt completion returns the session to idle. Interruption is explicit: max-iteration and token-budget interruptions keep the session idle, store exit_behavior: interrupted, and link the incomplete handoff for recovery. /exit, exit, /quit, and quit stop the session without creating a provider run.

Event records

craik.agent_session_event is the append-only event record for persistent sessions. Events are redacted and store references rather than credential material:

EventWhen it is emitted
prompt_receivedA non-exit prompt is accepted and converted into a task.
run_completedThe provider-backed run finishes without interruption.
run_interruptedThe provider-backed run writes an incomplete handoff for recovery.
exitedThe operator sends an explicit exit prompt.

Events include session, operator, project, provider, model, policy, task, run, handoff, receipt, metadata, and recovery references. Prompt text is represented by a short hash in event metadata.

Operator gate

Every craik agent lifecycle command requires an active operator session. The persisted session stores the operator subject and issuer, and prompt execution rejects sessions owned by a different operator. Provider sessions, recovery records, and receipts can therefore link runtime state to an authenticated operator without storing credentials or tokens in the local store.

Foreground and background state

v0.9.0 starts with foreground lifecycle state and a provider-backed prompt loop. The pid and endpoint_url fields are optional because foreground launch does not require a daemon pid. When background execution is added, pid-backed sessions use the same status path and stale pid recovery semantics.

Validation

uv run --extra dev pytest tests/test_cli_agents.py tests/test_agent_sessions.py

Expected output: lifecycle commands, prompt execution, event persistence, receipt and handoff linkage, interruption handling, explicit exits, and typed session helpers enforce operator gates and valid transitions.