Skip to main content

Overview

The Session Manager orchestrates the Runtime, Agent, and Workspace plugins to create, manage, and destroy agent sessions. It handles the full session lifecycle from creation to cleanup. Core responsibilities:
  • Spawn new sessions (create workspace → runtime → launch agent)
  • Restore crashed/killed sessions
  • List all sessions with live state enrichment
  • Kill sessions and clean up resources
  • Send messages to running sessions
  • Automatic cleanup of completed work
The Session Manager is stateless - all session data is stored in flat metadata files. It rebuilds session state on every operation by reading these files and polling plugins.

Architecture

Methods

spawn

Create a new agent session.
string
required
Project ID from agent-orchestrator.yaml (e.g., “my-app”).
string
Issue identifier (e.g., “INT-1234”, “#42”). Can be tracker issue or free-text.
string
Git branch name. If not specified, inferred from issue or session ID.
string
Initial prompt for the agent. Combined with issue context if available.
string
Override agent plugin for this session (e.g., “codex”, “claude-code”).
Returns:
object
The newly created session object with all metadata.
Example:

Spawn Process

The spawn process follows these steps:
  1. Validate project - Check that projectId exists in config
  2. Validate issue - If issueId provided, fetch from tracker (fails fast on auth/network errors)
  3. Reserve session ID - Atomically create unique ID (e.g., “my-app-1”)
  4. Create workspace - Use Workspace plugin to create isolated environment
  5. Generate prompt - Combine user prompt + issue context + project rules
  6. Create runtime - Launch agent process in runtime environment
  7. Write metadata - Persist session state to disk
  8. Post-launch setup - Configure agent (optional plugin hook)
  9. Send initial prompt - For agents with promptDelivery: "post-launch"
If any step fails after workspace creation, the Session Manager automatically cleans up all created resources (workspace, runtime, metadata) to prevent resource leaks.

Branch Name Resolution

Branch names are determined by priority:

spawnOrchestrator

Create an orchestrator session that manages other sessions.
string
required
Project ID from config.
string
System prompt with orchestrator instructions. Automatically written to file to avoid shell truncation.
Returns:
object
The orchestrator session with role: "orchestrator" metadata.
Example:
Orchestrator sessions run in the project directory (not a worktree) and have permissions set to “skip” so they can autonomously run ao CLI commands.

list

List all sessions, optionally filtered by project.
string
Filter to sessions from this project only. If omitted, returns all sessions.
Returns:
array
Array of session objects enriched with live runtime state and agent activity.
Example:

Session Enrichment

The list() method enriches sessions with live state:
Enrichment includes subprocess calls (tmux/ps checks) which can be slow under load. Each session has a 2-second timeout. If enrichment times out, the session keeps its metadata values.

get

Get a single session by ID.
string
required
Session ID to retrieve (e.g., “my-app-1”).
Returns:
object
Session object if found, null otherwise.
Example:

restore

Restore a crashed or killed session.
string
required
Session ID to restore.
Returns:
object
The restored session with updated restoredAt timestamp.
Throws:
  • SessionNotRestorableError - Session is not in a terminal state or is merged
  • WorkspaceMissingError - Workspace doesn’t exist and can’t be recreated
Example:

Restore Process

  1. Find metadata - Check active sessions, fall back to archive
  2. Validate restorability - Must be terminal state (killed/errored) but not merged
  3. Check workspace - Verify workspace exists or attempt recreation
  4. Destroy old runtime - Kill any orphaned runtime processes
  5. Get launch command - Try agent’s restore command, fall back to fresh launch
  6. Create runtime - Launch agent with restored environment
  7. Update metadata - Set status to “spawning”, record restoredAt
Restorable:
  • killed - Agent process exited
  • errored - Session encountered an error
  • done - Session completed
  • terminated - Forcibly terminated
  • cleanup - Cleaned up but can be restored
Non-restorable:
  • merged - PR already merged (cannot reopen)
  • working, pr_open, etc. - Session is still active

kill

Terminate a session and clean up all resources.
string
required
Session ID to kill.
Example:

Kill Process

  1. Find session - Locate metadata across all projects
  2. Destroy runtime - Stop agent process (tmux kill, docker stop, etc.)
  3. Destroy workspace - Delete worktree or clone (unless it’s the project path)
  4. Archive metadata - Move to archive/ subdirectory with timestamp
Workspaces that match the project path are NOT deleted (orchestrator sessions run in the main repo).

cleanup

Automatically clean up completed sessions.
string
Limit cleanup to this project.
boolean
default:"false"
Preview what would be cleaned up without actually killing sessions.
Returns:
object
Example:

Cleanup Criteria

Sessions are killed if ANY of these conditions are met:
  1. PR is merged or closed - Work is complete
  2. Issue is completed - Work is done
  3. Runtime is dead - Agent process no longer running
Never cleaned up:
  • Orchestrator sessions (checked by role: "orchestrator" or ID ending in “-orchestrator”)
  • Sessions where checks fail (can’t verify state)

send

Send a message to a running session.
string
required
Session ID to send message to.
string
required
Message text to send to the agent.
Example:
How it works:
For tmux runtime, this uses tmux send-keys to type the message. For other runtimes, it may use stdin, API calls, or other mechanisms.

Session Object

The Session interface represents a running agent session:

Activity States

ActivityState
Agent is processing (thinking, writing code).
ActivityState
Agent finished its turn, alive and waiting for input.
ActivityState
Agent has been inactive for a while (stale).
ActivityState
Agent is asking a question / permission prompt.
ActivityState
Agent hit an error or is stuck.
ActivityState
Agent process is no longer running.

Metadata Files

Sessions are stored as flat key=value files: File path:
Example file (my-app-1):
Metadata files use user-facing session names (“my-app-1”), not tmux session names. The tmuxName field maps to the globally unique tmux session.

Error Handling

Resource Cleanup on Failure

The Session Manager automatically cleans up resources when spawn fails:

Issue Validation

Issue validation fails fast to prevent creating resources for invalid issues:

Graceful Degradation

Non-critical failures don’t stop the session:

Complete Example

See Also