Session management

From josstei/maestro-orchestrate · View interactive version

Session management is an AI agent skill from the GitHub repository josstei/maestro-orchestrate. Manages orchestration session state, tracking, and resumption

How to use this skill

Skill file

Session 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:

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 keep a separate, ephemeral per-session directory (${MAESTRO_HOOKS_DIR:-<os.tmpdir()>/maestro-hooks-<uid>}/<session-id>/, holding only the active-agent file). The orchestrator never reads or writes it and interacts only with <MAESTRO_STATE_DIR> paths.

Session Creation Protocol

When to Create

For Standard workflow, create a new session at execution setup after the design document and implementation plan are approved and the execution mode gate has resolved. For Express workflow, create a session after the structured brief is approved (see EXPRESS WORKFLOW step 34 in orchestration-steps).

Session ID Format

YYYY-MM-DD-<topic-slug>

Where:

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.

Initialization Steps

  1. Resolve state directory from MAESTRO_STATE_DIR
  2. Create <state_dir>/state/ directory if it does not exist (defense-in-depth fallback — workspace readiness startup check is the primary mechanism)
  3. Verify no existing active-session.md — if one exists, alert the user and offer to archive or resume
  4. Generate session state from the session-state template (load it with get_skill_content(["session-state"]) if not already loaded), setting workflow_mode to standard or express and task_complexity from the design document frontmatter
  5. Initialize all phases as pending
  6. Set overall status to in_progress
  7. Set current_phase to 1
  8. Record design document and implementation plan paths
  9. Initialize empty token_usage, file manifests, downstream_context, and errors sections

State Update Protocol

Update Triggers

Update session state on every meaningful state change:

Update Rules

  1. Timestamp: Update updated field on every state change
  2. Phase Status: Transition phase status following valid transitions:
  1. Current Phase: Update current_phase to the ID of the currently executing phase
  2. File Manifest: Append to files_created, files_modified, or files_deleted as subagents report changes
  3. Downstream Context: Persist parsed Handoff Report Part 2 fields into phase downstream_context
  4. Token Usage: Aggregate token counts from subagent responses into both total_* and by_agent sections
  5. Error Recording: Append to phase errors array with complete metadata

Error Recording Format

errors:
  - agent: "<agent-name>"
    timestamp: "<ISO 8601>"
    type: "<validation|timeout|file_conflict|runtime|dependency|recovery>"
    message: "<full error description>"
    resolution: "<what was done to resolve, or 'pending'>"
    resolved: false

Retry Tracking

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]
- Deleted: [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:

Archive Protocol

When to Archive

Archive session state when:

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:

  1. Call archive_session with the session ID. The MCP tool atomically:
  1. Confirm archival to user with summary of what was archived (use the archived_files array in the response)

If archive_session is not available, fall back to manual file operations:

  1. Create <state_dir>/plans/archive/ directory if it does not exist
  2. Create <state_dir>/state/archive/ directory if it does not exist
  3. MOVE (not copy) design document from <state_dir>/plans/ to <state_dir>/plans/archive/ — the original MUST be deleted. Use the shell-command tool from runtime context with mv or read+write+delete. Do NOT leave the file in both locations. Skip this step if design_document is null (Express sessions).
  4. MOVE (not copy) implementation plan from <state_dir>/plans/ to <state_dir>/plans/archive/ — same: delete the original. Skip this step if implementation_plan is null (Express sessions).
  5. Update session state status to completed
  6. Update updated timestamp
  7. MOVE (not copy) active-session.md from <state_dir>/state/ to <state_dir>/state/archive/<session-id>.md — delete the original.
  8. 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):

Resume Protocol

When to Resume

Resume is triggered by the /maestro:resume command or when /maestro:orchestrate detects an existing active session.

Resume Steps

  1. Read State: If session state was already injected into the prompt (e.g., via /maestro:resume), use that injected content instead of calling get_session_status. Otherwise, if get_session_status appears in your available tools, call it to read the active session. Otherwise, read state via run_shell_command: node <runtime-script-root>/read-active-session.js (resolves MAESTRO_STATE_DIR internally)
  2. Parse Frontmatter: Extract YAML frontmatter for session metadata
  3. Identify Position: Determine:
  1. Check Errors: Identify unresolved errors from previous execution
  2. Present Summary: Display a status summary to the user covering the position and errors identified in steps 3-4
  3. Handle Errors: If unresolved errors exist:
  1. Continue Execution: Resume from the first pending or failed phase
  2. Update State: Mark resumed phase as in_progress and update timestamps

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:

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:

Report any detected conflicts to the user before proceeding.

Token Usage Tracking

Collection

After each subagent invocation, record:

Aggregation

Maintain two levels of aggregation:

  1. Total: Sum across all agents and phases
  2. 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
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.