bug(daemon): failed scratch orchestrator launch leaves workspace state that blocks retries
Summary
A failed initial orchestrator launch in a scratch project can leave AO-created agent hook files in the managed workspace. The spawn seed row is rolled back, so the next attempt reuses the same session ID and workspace path. Scratch workspace creation then refuses the non-empty directory, making the retry fail with an unrelated internal error.
Reproduced through the Electron app. I also fetched current upstream/main at 4736485cf; the relevant session-manager and scratch-workspace paths are unchanged there.
Reproduction
- Start with a scratch project whose configured orchestrator is unavailable locally (Claude Code in this reproduction).
- Click Spawn Orchestrator.
- The initial launch fails before a runtime is created because the agent binary cannot be resolved.
- Click Spawn Orchestrator again.
Actual behavior
The retry returns 500 INTERNAL_ERROR. The daemon reports:
spawn scratch-1: workspace: scratch workspace: path ".../worktrees/scratch/orchestrators/scratch-1" already contains files: workspace: uncommitted changes present
The preserved directory contains only the workspace-local Claude hook files created during the failed preparation:
scratch-1/.claude/settings.local.json
scratch-1/.claude/.gitignore
The UI exposes only the generic internal error, and subsequent retries remain blocked.
Why it happens
The pre-launch path installs workspace-local agent hooks before validating the launch binary. On failure, rollbackPreparedSpawnWorkspace calls workspace destruction first and only calls agent cleanup if destruction succeeds.
Scratch Destroy correctly preserves every non-empty directory as potential user work. Because the AO-created hook files make the directory non-empty, destruction fails and agent cleanup never runs. The seed session row is still deleted, allowing the next spawn to reuse scratch-1 and collide with the preserved path.
Expected behavior
A failed pre-runtime launch should leave scratch spawn retryable without risking user files.
Possible safe approaches include:
- remove only agent-owned preparation artifacts before attempting the normal safe scratch destroy; or
- preserve the failed workspace/session identity so the next spawn does not reuse its path.
AO should not force-delete arbitrary non-empty scratch directories.
Suggested tests
- Use the real scratch workspace adapter with an agent that writes workspace-local hook files, then fail binary validation.
- Verify a second spawn does not fail with
ErrWorkspaceDirtyfrom the first attempt. - Verify unrelated user-created files are never deleted during rollback.
Related
#2551 fixed stale/non-git worktrees during replacement of an existing orchestrator. This reproduction is different: it occurs during rollback of the initial scratch spawn before a live session exists.
I’m willing to work on this once the expected rollback approach is confirmed.
Thanks for contributing to Agent Orchestrator.
This issue is being picked up by the current external contributor on-call pair:
- @illegalcall
- @pulkit7070
If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @illegalcall and @pulkit7070 here.
For faster context or live questions, you can also join the AO Discord.
Join the session here: https://discord.gg/H6ZDcUXmq
Come by if you want to see what is being built, ask questions, or just hang around with the community.