feat: add MAESTRO_SESSION_RESUMED env var for hook control

Add environment variable that allows agent hooks to differentiate
between new sessions and resumed sessions. Since Maestro spawns a
new process per message (batch mode), session start hooks fire on
every turn. This env var lets users conditionally skip hooks.

- Set MAESTRO_SESSION_RESUMED=1 when --resume or --session flags present
- Document in README with example hook guard
- Add note in Agent Provider UI linking to documentation

Fixes #42

Co-authored-by: TQ White II (@tqwhite)
This commit is contained in:
Pedram Amini
2025-12-18 15:28:37 -06:00
parent 2ff50fb815
commit a68e6e85e8
3 changed files with 43 additions and 0 deletions

View File

@@ -437,6 +437,26 @@ Each task executes in a completely fresh AI session with its own unique session
This isolation is critical for playbooks with `Reset on Completion` documents that loop indefinitely. Without it, the AI might "remember" completing a task and skip re-execution on subsequent loops.
### Environment Variables {#environment-variables}
Maestro sets environment variables that your agent hooks can use to customize behavior:
| Variable | Value | Description |
|----------|-------|-------------|
| `MAESTRO_SESSION_RESUMED` | `1` | Set when resuming an existing session (not set for new sessions) |
**Example: Conditional Hook Execution**
Since Maestro spawns a new agent process for each message (batch mode), agent "session start" hooks will run on every turn. Use `MAESTRO_SESSION_RESUMED` to skip hooks on resumed sessions:
```bash
# In your agent's session start hook
[ "$MAESTRO_SESSION_RESUMED" = "1" ] && exit 0
# ... rest of your hook logic for new sessions only
```
This works with any agent provider (Claude Code, Codex, OpenCode) since the environment variable is set by Maestro before spawning the agent process.
### History & Tracking
Each completed task is logged to the History panel with:

View File

@@ -289,6 +289,14 @@ export class ProcessManager extends EventEmitter {
env.PATH = standardPaths;
}
// Set MAESTRO_SESSION_RESUMED env var when resuming an existing session
// This allows user hooks to differentiate between new sessions and resumed ones
// See: https://github.com/pedramamini/Maestro/issues/42
const isResuming = finalArgs.includes('--resume') || finalArgs.includes('--session');
if (isResuming) {
env.MAESTRO_SESSION_RESUMED = '1';
}
logger.debug('[ProcessManager] About to spawn child process', 'ProcessManager', {
command,
finalArgs,

View File

@@ -602,6 +602,21 @@ export function NewInstanceModal({ isOpen, onClose, onCreate, theme, existingSes
</div>
)}
{/* Hook behavior note */}
<p className="text-xs mt-2" style={{ color: theme.colors.textDim }}>
Agent hooks run per-message. Use{' '}
<a
href="https://github.com/pedramamini/Maestro#environment-variables"
target="_blank"
rel="noopener noreferrer"
className="underline hover:opacity-80"
style={{ color: theme.colors.accent }}
>
MAESTRO_SESSION_RESUMED
</a>
{' '}to skip on resumed sessions.
</p>
{/* Debug Info Display */}
{debugInfo && (
<div