| name | architecture | ||||
|---|---|---|---|---|---|
| description | Use when implementing new orchestration features or understanding how the lifecycle tracking system works. Covers hook integration, request scoping, and file-based state. | ||||
| categories |
|
||||
| tags |
|
||||
| related_docs |
|
||||
| complexity | intermediate |
Last Updated: 2025-12-12
Claude Code Toolbox provides infrastructure for agent coordination. The system uses Claude Code hooks to observe agent execution and persist context, enabling agents to coordinate across sessions without tight coupling.
Capture what happens during a user request: what the user asked for, which agents ran, what context they produced, and what work they generated.
The lifecycle system follows a layered architecture separating concerns:
graph TB
Hook[Claude Code Hooks] -->|JSON stdin| Interface[Hook Interface Layer]
Interface -->|EventData| Orchestrator[Lifecycle Orchestrator]
Orchestrator -->|read/write| State[State Management]
Orchestrator -->|parse| Processing[Data Processing]
Orchestrator -.->|extension point| Handlers[Event Handlers]
Orchestrator -->|files| Storage[Request Directory]
Hook Interface (agent-lifecycle.py): Translates hook protocol (stdin/exit codes) to Python exceptions. Minimal layer with no business logic.
Orchestrator (lifecycle/orchestrator.py): Coordinates event processing, manages request lifecycle, executes side effects. Contains all framework logic for capturing user prompts, tracking agents, extracting outputs, and persisting state.
State Management (lifecycle/state.py): Dual-file storage (global + request-scoped) with dirty tracking and atomic writes.
Data Processing (lifecycle/processing.py): Transforms hook inputs, parses agent tags, extracts session log snapshots, validates fields.
Event Handlers (lifecycle/handlers.py): Extension point for future business logic. Currently unused—all logic lives in orchestrator framework methods.
Request Scoping: Each user prompt creates a request directory under .toolbox/events/. Everything related to that request—user prompt, agent outputs, session logs, hook events—goes there.
Hook Integration: Five hook events drive the lifecycle:
- SessionStart - Creates
.toolbox/events/directory - UserPromptSubmit - Generates request ID, creates request directory, writes prompt to
context.md - SubagentStart - Records agent type in state
- SubagentStop - Extracts
<context>and<work>tags from agent output, appends tocontext.md, copies transcript - Stop - Extracts request-specific session log snapshot
State Management: Two-tier state storage:
- Global state (
.toolbox/events/.global-state.json) - Maps session_id → request_id for concurrent sessions - Request state (
{request_dir}/.state.json) - Stores start_uuid and agent_types mapping
Processing Flow:
- Hook fires → JSON stdin → Interface layer
- Interface creates EventData, invokes orchestrator
- Orchestrator determines request context (create or lookup request_id)
- Orchestrator executes event-specific framework method
- Framework method writes files immediately (context.md, transcripts, session logs)
- Orchestrator persists state changes
- Orchestrator logs hook event to audit trail
- Interface exits with appropriate code (0/1/2)
Why Hooks: Hooks run outside agent context, allowing passive observation. Agents don't need to know about the orchestration system—they just output tags if they want to share context or create artifacts.
Each request directory contains:
context.md- User prompt + agent context extractswork/- Files agents create via<work filename="...">tagssession-logs/- Agent transcripts (agent-{id}.jsonl) and pruned session log ({session_id}-pruned.jsonl)hook-events.jsonl- Complete audit trail.state.json- Request-scoped state (start_uuid, agent_types)
Two error types with distinct semantics:
- BlockingError (exit 2) - Validation failures that should block the operation and notify the agent
- NonBlockingError (exit 1) - I/O failures logged but allow operation to continue
Best-effort semantics: lifecycle tracking is observational and never breaks user workflow.
System designed for future enhancement without restructuring:
- Event Handlers: Add domain-specific logic by implementing EventHandler interface
- File Operations: FileOperation model supports declarative file operations (currently unused)
- Agent-Specific Routing:
_get_handler()can route based on agent type - New Events: Add event-specific framework methods to orchestrator
Agent Independence: Agents remain simple. They output text tags; the system extracts them. No special tools or coordination logic required.
Request Isolation: Each request is self-contained. No cross-contamination between requests, easy to analyze or replay individual requests.
File-Based State: No databases or external services. Works anywhere Claude Code runs. Human-inspectable for debugging.
Testability: Clear separation between framework (orchestrator methods) and business logic (handlers). Pure functions enable easy testing.
Extensibility: Extension points established without implementation. Add handlers when needed, no restructuring required.
As new orchestration capabilities are added (multi-agent workflows, work queues, session resumption), they will be documented here.