diff --git a/apps/server/src/services/auto-mode-service.ts b/apps/server/src/services/auto-mode-service.ts index ba5b6351..527a0e8d 100644 --- a/apps/server/src/services/auto-mode-service.ts +++ b/apps/server/src/services/auto-mode-service.ts @@ -152,22 +152,27 @@ export class AutoModeService { } this.autoLoopRunning = false; - this.emitAutoModeEvent("auto_mode_stopped", { - message: "Auto mode stopped", - projectPath: this.config?.projectPath, - }); } /** * Stop the auto mode loop */ async stopAutoLoop(): Promise { + const wasRunning = this.autoLoopRunning; this.autoLoopRunning = false; if (this.autoLoopAbortController) { this.autoLoopAbortController.abort(); this.autoLoopAbortController = null; } + // Emit stop event immediately when user explicitly stops + if (wasRunning) { + this.emitAutoModeEvent("auto_mode_stopped", { + message: "Auto mode stopped", + projectPath: this.config?.projectPath, + }); + } + return this.runningFeatures.size; } diff --git a/apps/server/tests/integration/services/auto-mode-service.integration.test.ts b/apps/server/tests/integration/services/auto-mode-service.integration.test.ts index 932417b3..5db48152 100644 --- a/apps/server/tests/integration/services/auto-mode-service.integration.test.ts +++ b/apps/server/tests/integration/services/auto-mode-service.integration.test.ts @@ -119,7 +119,10 @@ describe("auto-mode-service.ts (integration)", () => { ); // Verify feature status was updated to backlog (error status) - const feature = await featureLoader.get(testRepo.path, "test-feature-error"); + const feature = await featureLoader.get( + testRepo.path, + "test-feature-error" + ); expect(feature?.status).toBe("backlog"); }, 30000); @@ -154,7 +157,10 @@ describe("auto-mode-service.ts (integration)", () => { ); // Feature should be updated successfully - const feature = await featureLoader.get(testRepo.path, "test-no-worktree"); + const feature = await featureLoader.get( + testRepo.path, + "test-no-worktree" + ); expect(feature?.status).toBe("waiting_approval"); }, 30000); }); @@ -313,7 +319,9 @@ describe("auto-mode-service.ts (integration)", () => { ); // Should have used claude-sonnet-4-20250514 - expect(ProviderFactory.getProviderForModel).toHaveBeenCalledWith("claude-sonnet-4-20250514"); + expect(ProviderFactory.getProviderForModel).toHaveBeenCalledWith( + "claude-sonnet-4-20250514" + ); }, 30000); }); @@ -447,9 +455,11 @@ describe("auto-mode-service.ts (integration)", () => { await service.stopAutoLoop(); await startPromise.catch(() => {}); - // Check stop event was emitted (auto_mode_complete event) - const stopEvent = mockEvents.emit.mock.calls.find((call) => - call[1]?.type === "auto_mode_complete" || call[1]?.message?.includes("stopped") + // Check stop event was emitted (emitted immediately by stopAutoLoop) + const stopEvent = mockEvents.emit.mock.calls.find( + (call) => + call[1]?.type === "auto_mode_stopped" || + call[1]?.message?.includes("Auto mode stopped") ); expect(stopEvent).toBeTruthy(); }, 10000); @@ -476,12 +486,7 @@ describe("auto-mode-service.ts (integration)", () => { ); // Should not throw - await service.executeFeature( - testRepo.path, - "error-feature", - true, - false - ); + await service.executeFeature(testRepo.path, "error-feature", true, false); // Feature should be marked as backlog (error status) const feature = await featureLoader.get(testRepo.path, "error-feature");