Session Pool

How AgentDesk manages Claude Code SDK sessions across agents.

The session pool is a process-level singleton that manages Claude Code SDK sessions for all agents. It handles session creation, resumption, concurrency, and event streaming.

Architecture

The session pool does not keep long-lived subprocesses between turns. Instead, it uses the Claude Code SDK’s resume feature:

  1. First turn — the SDK spawns a new session and emits a system/init event with a session ID
  2. Session ID saved — the ID is stored in agents.cc_session_id in the database
  3. Subsequent turns — each query() call passes resume: sessionId, loading the conversation history from disk

Session state lives as .jsonl files in ~/.claude/projects/. This means sessions survive process restarts — the agent picks up where it left off.

Concurrency Model

  • Turns to the same agent are serialized — one at a time
  • Turns to different agents run in parallel
  • The dispatcher’s running Set coordinates this

Event Types

Each turn emits a stream of typed events:

EventDescription
initSession initialized, contains session ID
assistant-textToken-by-token text from the agent
tool-useAgent invoked a tool (file read, shell, etc.)
tool-progressProgress update for a running tool
tool-resultTool execution completed
result-successTurn completed successfully
result-errorTurn failed with an error
statusAgent status change
command-outputShell command output

Events are streamed to the WebSocket hub for real-time display in the chat UI.

Effort Levels

The SDK supports effort levels that control response depth:

LevelUsage
lowQuick answers, simple lookups
mediumModerate analysis
highDefault for interactive chat
maxMost thorough analysis

Interactive chat defaults to high. Dispatcher and scheduler turns use the SDK’s default effort level to avoid silent cost overruns.

Isolated Turns

The dispatcher uses sendIsolatedTurnAndCollect for heartbeat and cron turns. These create temporary sessions that don’t pollute the agent’s main chat history — the main session remains clean for human interaction.