Repository navigation
Description
Stop can return successfully during first-turn preparation and the agent still launches afterward. Track pending launches in the same lifecycle used by Stop and active-run guards.
Reproduced
The lead exercised the real createRunner and Store in disposable directories with a deferred bootstrapMemory callback and injected runAgentFn. No real CLI was executed.
- Call startRun. While bootstrap is pending, the Store thread is working, but runner.isRunning is false and no agent handle exists.
- Call stopRun. It returns without stopping or changing that working state.
- Resolve bootstrap. The injected agent starts once and runner.isRunning becomes true, after Stop has already completed.
A second fixture called real services.deleteThread with the runner's isRunning guard during the same await. Deletion succeeded; resolving bootstrap caused the pending launch to reject with a null-thread TypeError. Both assertions passed. This demonstrates an API lifecycle race; it does not claim a particular Delete menu is visible while working or that a live user process was killed.
Cause
At 9fc8645a2472e493a94c2e4c4c8a942974bff305, runner.js:7768 marks the thread working before the awaited prefetch at line7872. Provider dispatch happens afterward, and active.set occurs inside the provider start functions. stopRun at line8020 returns on a missing active entry; isRunning also checks only active. prefetchBootstrapNote awaits memory on a first turn, even though it correctly tolerates service errors. There is no cancellation/generation check before dispatch resumes.
Smallest scope
Give each pending launch a cancellable identity at the shared startRun boundary. Include it in lifecycle guards and recheck it after asynchronous preparation before any provider spawn. Stop must invalidate this identity, settle the visible run once, and prevent its continuation from dispatching. Reuse existing terminal-state and pending-work cleanup; do not independently patch every provider or impose a new archive policy.
Acceptance
- Hold bootstrap, start, Stop, then release: zero provider spawns, no warm session, stopped/idle state, and no stuck run guard.
- A cancelled older launch cannot later overwrite or spawn alongside a newer run for the same thread.
- Deletion while preparation is pending is either rejected by the existing active-run contract or explicitly cancels preparation before removal; no null-thread exception or late spawn.
- stopAll invalidates pending starts too, so shutdown cannot create a new provider after cleanup began.
- Normal first-turn preparation starts exactly once; unavailable memory still uses the existing fail-open path when the user has not cancelled.
- Exercise the shared preparation path with injected launch functions and a deferred bootstrap callback; do not spend model tokens to prove the race.
Dedup
#511 handles worktree setup failure; #314 transactional turn/watchdog and #315 absolute stop are related lifecycle work. The missing pre-dispatch cancellation identity is a specific uncovered await window. #1195 is a quit-confirmation proposal, not this race. Archive-as-hide semantics remain outside this ticket.
Activity
currentbits commentedon Sep 10, 2026
Pending launch identity at the shared startRun boundary. After status=working, startRun records {id, runId} in pendingLaunches; isRunning/activeRunId/already-active include it; stopRun settles idle + Run stopped without a provider handle; stopAll clears pending first so shutdown cannot spawn. After prefetchBootstrapNote the launch id is rechecked before any provider start. deleteThread is unchanged and now refuses because isRunning is true during prepare.
Tests: electron/test/stop-during-prepare.test.js (injected runAgentFn + deferred bootstrapMemory). Related runner/ask/quota/stall/absolute-stop tests also passed.
Honor Stop while a run is preparing to launch (#1228) (#1269)
fix: honor Stop during first-turn launch preparation (#1228)