Maestro-orchestrate session-management
Manages orchestration session state, tracking, and resumption
git clone https://github.com/josstei/maestro-orchestrate
T=$(mktemp -d) && git clone --depth=1 https://github.com/josstei/maestro-orchestrate "$T" && mkdir -p ~/.claude/skills && cp -r "$T/claude/src/skills/shared/session-management" ~/.claude/skills/josstei-maestro-orchestrate-session-management-384ec8 && rm -rf "$T"
claude/src/skills/shared/session-management/SKILL.mdSession Management Skill
Activate this skill for all session state operations during Maestro orchestration. This skill defines the protocols for creating, updating, resuming, and archiving orchestration sessions.
State Access Protocol
When MCP state tools are available, prefer them for state operations:
- Preferred: MCP tools (
,initialize_workspace
,create_session
,update_session
,transition_phase
,get_session_status
) — structured I/O, atomic operations.archive_session - Fallback:
/write_file
directly on state files — when MCP tools are not in the available tool list.replace - Legacy: Shell scripts (
,write-state.js
) — remain available but are not the recommended path.read-state.js
Detection: check whether MCP state tools appear in your available tools. If they do, use them. If they do not, use
write_file/replace.
Hook-Level Session State
Maestro hooks maintain a separate, transient state directory at
/tmp/maestro-hooks/<session-id>/ that is distinct from orchestration state in <MAESTRO_STATE_DIR>:
| Concern | Orchestration State | Hook State |
|---|---|---|
| Location | | (Unix) or (Windows) |
| Lifecycle | Created in Phase 2, archived in Phase 4 | Directory created by the session-start hook when an active session exists; active-agent file written by the pre-delegation hook and cleared by the post-delegation hook; stale directories pruned by both session-start and pre-delegation hooks |
| Contents | Session metadata, phase tracking, token usage, file manifests | Active agent tracking file () |
| Persistence | Survives session restarts (supports ) | Ephemeral — lost on session end or system reboot |
| Managed by | Orchestrator via session-management skill | The runtime's pre-delegation and post-delegation hooks |
The pre-delegation hook prunes stale hook state directories older than 2 hours to prevent accumulation from abnormal session terminations.
The orchestrator does not read or write hook-level state directly. It interacts only with
<MAESTRO_STATE_DIR> paths. The two state systems are independent and serve different concerns.
Session Creation Protocol
When to Create
For Standard workflow, create a new session when beginning Phase 2 (Team Assembly & Planning) of orchestration, after the design document has been approved. For Express workflow, create a session after the structured brief is approved (see Express Workflow section in the orchestrator template).
Session ID Format
YYYY-MM-DD-<topic-slug>
Where:
is the orchestration start dateYYYY-MM-DD
is a lowercase, hyphenated summary matching the design document topic<topic-slug>
File Location
<MAESTRO_STATE_DIR>/state/active-session.md
All state paths in this skill use
<MAESTRO_STATE_DIR> as their base directory (default: docs/maestro). In procedural steps, <state_dir> represents the resolved value of this variable.
State File Access
Both
read_file and write_file work on state paths inside <MAESTRO_STATE_DIR>. The runtime's file-access configuration makes state paths accessible.
Use the runtime's bundled
scripts/ directory for these helper commands so they still work when the extension is installed outside the workspace root.
Reading state files: Use
read_file directly. The read-state.js script remains available as an alternative for TOML shell blocks that inject state before the model's first turn:
run_shell_command: node <runtime-script-root>/read-state.js <relative-path>
Writing state files: Use
write_file directly. When content must be piped from a shell command, use the atomic write script:
run_shell_command: echo '...' | node <runtime-script-root>/write-state.js <relative-path>
Rules:
- The
script writes atomically (temp file + rename) to prevent partial writeswrite-state.js - Both scripts validate against absolute paths and path traversal
Initialization Steps
- Resolve state directory from
MAESTRO_STATE_DIR - Create
directory if it does not exist (defense-in-depth fallback — workspace readiness startup check is the primary mechanism)<state_dir>/state/ - Verify no existing
— if one exists, alert the user and offer to archive or resumeactive-session.md - Generate session state using the
template loaded viasession-stateget_skill_content - Initialize all phases as
pending - Set overall status to
in_progress - Set
to 1current_phase - Record design document and implementation plan paths
- Initialize empty token_usage, file manifests, downstream_context, and errors sections
Initial State Template
--- session_id: "<YYYY-MM-DD-topic-slug>" task: "<user's original task description>" created: "<ISO 8601 timestamp>" updated: "<ISO 8601 timestamp>" status: "in_progress" workflow_mode: "<standard|express>" design_document: "<state_dir>/plans/<design-doc-filename>" implementation_plan: "<state_dir>/plans/<impl-plan-filename>" current_phase: 1 total_phases: <integer from impl plan> execution_mode: null execution_backend: null task_complexity: null token_usage: total_input: 0 total_output: 0 total_cached: 0 by_agent: {} phases: - id: 1 name: "<phase name from impl plan>" status: "pending" agents: [] parallel: false started: null completed: null blocked_by: [] files_created: [] files_modified: [] files_deleted: [] downstream_context: key_interfaces_introduced: [] patterns_established: [] integration_points: [] assumptions: [] warnings: [] errors: [] retry_count: 0 --- # <Topic> Orchestration Log
Include
task_complexity (from design document frontmatter) in the session state. Place after execution_backend, before token_usage. Default: null.
State Update Protocol
Update Triggers
Update session state on every meaningful state change:
- Phase status transitions
- File manifest changes
- Downstream context extraction from completed phases
- Error occurrences
- Token usage increments
- Phase completion or failure
Update Rules
- Timestamp: Update
field on every state changeupdated - Phase Status: Transition phase status following valid transitions:
->pendingin_progress
->in_progresscompleted
->in_progressfailed
->failed
(retry)in_progress
->pending
(user decision only)skipped
- Current Phase: Update
to the ID of the currently executing phasecurrent_phase - File Manifest: Append to
,files_created
, orfiles_modified
as subagents report changesfiles_deleted - Downstream Context: Persist parsed Handoff Report Part 2 fields into phase
downstream_context - Token Usage: Aggregate token counts from subagent responses into both
andtotal_*
sectionsby_agent - Error Recording: Append to phase
array with complete metadataerrors
Error Recording Format
errors: - agent: "<agent-name>" timestamp: "<ISO 8601>" type: "<validation|timeout|file_conflict|runtime|dependency>" message: "<full error description>" resolution: "<what was done to resolve, or 'pending'>" resolved: false
Retry Tracking
- Increment
on each retry attemptretry_count - Maximum 2 retries per phase before escalating to user
- Record each retry as a separate error entry with resolution details
Markdown Body Updates
After updating YAML frontmatter, append to the Markdown body:
## Phase N: <Phase Name> <status indicator> ### <Agent Name> Output [Summary of agent output or full content] ### Files Changed - Created: [list] - Modified: [list] ### Downstream Context - Key Interfaces Introduced: [list] - Patterns Established: [list] - Integration Points: [list] - Assumptions: [list] - Warnings: [list] ### Validation Result [Pass/Fail with details]
Status indicators:
- Completed: checkmark
- In Progress: circle
- Failed: cross
- Pending: square
- Skipped: dash
Archive Protocol
When to Archive
Archive session state when:
- All phases are completed successfully AND
isMAESTRO_AUTO_ARCHIVE
(default)true - User explicitly requests archival (regardless of
setting)MAESTRO_AUTO_ARCHIVE - User starts a new orchestration (previous session must be archived first, regardless of setting)
When
MAESTRO_AUTO_ARCHIVE is false, prompt the user after successful completion: "Session complete. Auto-archive is disabled. Would you like to archive this session?"
Archive Steps
If
archive_session appears in your available tools, use it — a single call handles all archival:
- Call
with the session ID. The MCP tool atomically:archive_session- Updates session status to
completed - Moves
toactive-session.md<state_dir>/state/archive/<session-id>.md - Moves design document to
(if it exists and is non-null)<state_dir>/plans/archive/ - Moves implementation plan to
(if it exists and is non-null)<state_dir>/plans/archive/
- Updates session status to
- Confirm archival to user with summary of what was archived (use the
array in the response)archived_files
If
archive_session is not available, fall back to manual file operations:
- Create
directory if it does not exist<state_dir>/plans/archive/ - Create
directory if it does not exist<state_dir>/state/archive/ - MOVE (not copy) design document from
to<state_dir>/plans/
— the original MUST be deleted. Use the shell-command tool from runtime context with<state_dir>/plans/archive/
or read+write+delete. Do NOT leave the file in both locations. Skip this step ifmv
isdesign_document
(Express sessions).null - MOVE (not copy) implementation plan from
to<state_dir>/plans/
— same: delete the original. Skip this step if<state_dir>/plans/archive/
isimplementation_plan
(Express sessions).null - Update session state
tostatuscompleted - Update
timestampupdated - MOVE (not copy)
fromactive-session.md
to<state_dir>/state/
— delete the original.<state_dir>/state/archive/<session-id>.md - Confirm archival to user with summary of what was archived
Archive Verification
After archival, verify ALL of the following (archive is incomplete if any check fails):
- No
exists inactive-session.md<state_dir>/state/ - No plan files remain in
(only the<state_dir>/plans/
subdirectory should be present)archive/ - Archived files are readable at their new locations in
archive/ - If files still exist in the original locations, delete them now — the archive step used copy instead of move
Resume Protocol
When to Resume
Resume is triggered by the
/maestro:resume command or when /maestro:orchestrate detects an existing active session.
Resume Steps
- Read State: If session state was already injected into the prompt (e.g., via
), use that injected content instead of calling/maestro:resume
. Otherwise, ifget_session_status
appears in your available tools, call it to read the active session. Otherwise, read state viaget_session_status
:run_shell_command
(resolvesnode <runtime-script-root>/read-active-session.js
internally)MAESTRO_STATE_DIR - Parse Frontmatter: Extract YAML frontmatter for session metadata
- Identify Position: Determine:
- Last completed phase (highest ID with
)status: completed - Current active phase (first phase with
orstatus: in_progress
)pending - Any failed phases with unresolved errors
- Last completed phase (highest ID with
- Check Errors: Identify unresolved errors from previous execution
- Present Summary: Display status summary to user using the resume format defined in the orchestrator instructions
- Handle Errors: If unresolved errors exist:
- Present each error with context
- Offer options: retry, skip, abort, or adjust parameters
- Wait for user guidance before proceeding
- Continue Execution: Resume from the first pending or failed phase
- Update State: Mark resumed phase as
and update timestampsin_progress
Express Resume Branch
When resuming a session with
workflow_mode: "express" (read from session state via get_session_status), follow the Express workflow's resume protocol instead of the standard resume steps above:
- If phase status is
: re-generate and present the structured brief for approval. On approval, proceed to delegation.pending - If phase status is
: the implementing agent was interrupted. Re-delegate with the same scope. Use thein_progress
array to identify which agent was running.agents - If phase status is
but session status iscompleted
: code review or archival was interrupted. Run the code review step, then archive.in_progress
Express sessions have a single phase. The phase status combined with the
agents array contents determines the resume position.
Conflict Detection
When resuming, check for potential conflicts:
- Files that were partially modified (phase started but not completed)
- External modifications to files in the manifest since last session
- Changes to the implementation plan since last execution
Report any detected conflicts to the user before proceeding.
Token Usage Tracking
Collection
After each subagent invocation, record:
- Input tokens consumed
- Output tokens generated
- Cached tokens used (if available)
Aggregation
Maintain two levels of aggregation:
- Total: Sum across all agents and phases
- By Agent: Per-agent totals across all their invocations
Format
token_usage: total_input: 15000 total_output: 8000 total_cached: 3000 by_agent: coder: input: 8000 output: 4000 cached: 2000 tester: input: 7000 output: 4000 cached: 1000