🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(daemon): cancel pending auto-restart on stop (#4413)
ordinary beingaa26e0d8c87f
diff --git a/v3/@claude-flow/hooks/src/__tests__/daemon-restart-timer-4410.test.ts b/v3/@claude-flow/hooks/src/__tests__/daemon-restart-timer-4410.test.tsnew file mode 100644index 0000000000..fcc7fa099f--- /dev/null+++ b/v3/@claude-flow/hooks/src/__tests__/daemon-restart-timer-4410.test.ts@@ -0,0 +1,44 @@+/**+ * #4410 automation scan: the auto-restart backoff timer was a local closure+ * variable, so stop() could not cancel it and the daemon restarted ~1s later.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { DaemonManager } from '../daemons/index.js';++let manager: DaemonManager;++beforeEach(() => {+ vi.useFakeTimers();+ manager = new DaemonManager({ autoRestart: true, maxRestartAttempts: 3 });+});++afterEach(() => {+ vi.clearAllTimers();+ vi.useRealTimers();+});++describe('DaemonManager restart backoff cancellation (#4410)', () => {+ it('does not restart after stop() cancels the pending backoff', async () => {+ let attempts = 0;+ manager.register(+ { name: 'flaky', interval: 60_000, enabled: true },+ async () => {+ attempts++;+ throw new Error('boom');+ },+ );++ await manager.start('flaky');+ expect(attempts).toBe(1);+ expect(manager.getState('flaky')?.failureCount).toBe(1);++ await manager.stop('flaky');+ expect(manager.isRunning('flaky')).toBe(false);++ await vi.advanceTimersByTimeAsync(1_500);++ expect(attempts).toBe(1);+ expect(manager.getState('flaky')?.failureCount).toBe(1);+ expect(manager.isRunning('flaky')).toBe(false);+ });+});diff --git a/v3/@claude-flow/hooks/src/daemons/index.ts b/v3/@claude-flow/hooks/src/daemons/index.tsindex 5690974115..a0f1d91381 100644--- a/v3/@claude-flow/hooks/src/daemons/index.ts+++ b/v3/@claude-flow/hooks/src/daemons/index.ts@@ -43,6 +43,9 @@ export class DaemonManager {private config: DaemonManagerConfig;private daemons: Map<string, DaemonInstance> = new Map();private restartCounts: Map<string, number> = new Map();+ // #4410: backoff timers must be cancellable. Keeping them only in a local+ // closure let an explicit stop() be undone ~1s later by a pending restart.+ private restartTimers: Map<string, ReturnType<typeof setTimeout>> = new Map();constructor(config?: Partial<DaemonManagerConfig>) {this.config = { ...DEFAULT_CONFIG, ...config };@@ -86,6 +89,13 @@ export class DaemonManager {daemon.state.status = 'starting';daemon.state.startedAt = new Date();+ // A manual/normal start supersedes any pending failure backoff.+ const pendingRestart = this.restartTimers.get(name);+ if (pendingRestart) {+ clearTimeout(pendingRestart);+ this.restartTimers.delete(name);+ }+try {// Start interval timerdaemon.timer = setInterval(async () => {@@ -119,6 +129,12 @@ export class DaemonManager {daemon.state.status = 'stopping';+ const pendingRestart = this.restartTimers.get(name);+ if (pendingRestart) {+ clearTimeout(pendingRestart);+ this.restartTimers.delete(name);+ }+if (daemon.timer) {clearInterval(daemon.timer);daemon.timer = undefined;@@ -262,10 +278,19 @@ export class DaemonManager {this.restartCounts.set(name, restartCount);if (restartCount <= this.config.maxRestartAttempts) {- // Schedule restart- setTimeout(() => {+ // Schedule restart, keeping the handle so stop() can cancel it.+ const existing = this.restartTimers.get(name);+ if (existing) clearTimeout(existing);+ const timer = setTimeout(() => {+ this.restartTimers.delete(name);+ const current = this.daemons.get(name);+ if (!current || current.state.status === 'stopped' || current.state.status === 'stopping' || !current.config.enabled) {+ return;+ }this.restart(name).catch(() => {});}, 1000 * restartCount); // Exponential backoff+ timer.unref?.();+ this.restartTimers.set(name, timer);} else {daemon.state.status = 'error';}