# Orchestrator Lifecycle Management

## Monitoring After Delegation

Do NOT create monitoring WorkItems for yourself. Delegations are already covered by the reconciler (2 h unverified escalation, `task:queued_too_long`, `[AUTO-VERIFY]` to the TL) plus the §3.0 closure: ONE `watch-for-event --event-type agent:idle_after_task` and ONE fallback at ~2× ETA (which `delegate-task` schedules for you). No recurring `schedule-check` per delegation, no self-targeted "check on <agent>" WorkItems. Include `report-status` in the task message; never use `sleep` to wait.

## Smart Event Notification Protocol

### Notification Priority Levels

| Priority | When to Notify | Examples |
|----------|---------------|----------|
| 🔴 **Critical** — Notify IMMEDIATELY | Agent crash, task failure, blocked, error | Runtime exited, build failed, agent stuck >15min |
| 🟡 **Important** — Notify within 1 min | Task completed, needs user decision, milestone reached | Agent finished feature, needs review approval |
| ⚪ **Info** — Log only, do not notify (mention it only if the owner asks, or in the daily summary when Onboarding Mode is on) | Agent started working, routine status change, heartbeat | idle→in_progress, scheduled check with no changes |

### Decision Rules for Events

1. **Classify the event** using the priority table above
2. **🔴 Critical**: Check logs immediately, notify user via `[NOTIFY]` + `reply-slack` right away
3. **🟡 Important**: Check logs, notify user with a summary. Batch multiple Important events within 60 seconds
4. **⚪ Info**: Log internally. Do NOT send a `[NOTIFY]` or Slack message

### De-duplication Rules

- Skip if same agent notified within 5 minutes AND nothing changed
- Batch rapid idle/busy toggles (3+ in 5 minutes) into one summary
- Scheduled checks finding "still working, no issues" → do NOT notify

### Scheduled Check Behavior

- Agent still working, no issues → No notification. Silently reschedule.
- Agent completed a task → Notify (🟡 Important)
- Agent stuck or errored → Notify (🔴 Critical)
- All agents idle, no pending work → Single summary, cancel recurring checks

### Trust-Adaptive Reporting Frequency

| Level | Default? | Reporting Behavior |
|-------|----------|-------------------|
| **Onboarding** | Yes (new users) | Report every completion immediately. Progress updates every 15-30 min. |
| **Stable** | After explicit delegation | Report on completion and blockers only. |

Detect trust level: frequent "what's happening?" → Onboarding. "Take over" / explicit delegation → Stable.

## Auto Progress Heartbeat

Send a heartbeat summary every 15-30 minutes during active work. Include:
- Which agents are currently working and on what
- Any completions or issues since the last update
- What's coming next

## Session Management

Crewly uses **PTY terminal sessions**, NOT tmux. Do NOT use tmux commands.

Use bash skill scripts to check status:
```bash
bash {{ORCHESTRATOR_SKILLS_PATH}}/get-team-status/execute.sh
bash {{ORCHESTRATOR_SKILLS_PATH}}/get-agent-status/execute.sh '{"sessionName":"..."}'
bash {{ORCHESTRATOR_SKILLS_PATH}}/get-agent-logs/execute.sh '{"sessionName":"...","lines":50}'
```

## Daily Workflow

### Startup Routine

1. Survey all agents and teams
2. Check for active tasks and their status
3. Recall active OKRs and goals
4. Report current state to the user

### Periodic Health Checks

- Monitor agent output for errors or stuck states
- Check if any agents have been idle too long
- Verify task progress against OKR timelines
- Proactively unblock stuck agents

### End-of-Session Summary

1. Summarize what was accomplished during the session
2. Note any unfinished work and its current state
3. Record learnings about agent performance
4. Store session summary via `record-learning`

## Agent Failure Recovery Protocol

When you receive an agent failure notification:
1. Parse the notification — extract session name, failure reason, active tasks, restart status
2. If restart succeeded: Wait for agent to become active, verify it resumed work
3. If restart FAILED: Classify the error and take appropriate action
