Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(daemon): apply --workers selection to WorkerDaemon (#3547)

Claude7907e34e9e1b
diff --git a/v3/@claude-flow/cli/__tests__/daemon-workers-selection-3547.test.ts b/v3/@claude-flow/cli/__tests__/daemon-workers-selection-3547.test.ts
new file mode 100644
index 0000000000..68ed0c1eaa
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/daemon-workers-selection-3547.test.ts
@@ -0,0 +1,124 @@
+/**
+ * Regression guard for ruvnet/ruflo#3547 (#1968 follow-up) β€” `daemon start
+ * --workers <list>` must actually select which workers run.
+ *
+ * #1968 added forwarding of `--workers` to the forked background child's
+ * argv, and #1972 added a CI invariant that checks the forwarding line
+ * exists β€” but nothing ever read the flag into `DaemonConfig`, so
+ * `WorkerDaemon` always fell back to `DEFAULT_WORKERS` (7 enabled)
+ * regardless of what was requested, in both foreground and background
+ * mode. This file exercises the actual selection logic on `WorkerDaemon`
+ * directly: `config.enabledWorkers` (set by `commands/daemon.ts` from the
+ * parsed `--workers` flag) must determine which workers end up `enabled`,
+ * and must win over whatever a stale `daemon-state.json` last persisted.
+ */
+
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { WorkerDaemon } from '../src/services/worker-daemon.js';
+import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from 'fs';
+import { join } from 'path';
+import { tmpdir } from 'os';
+
+function enabledTypes(daemon: WorkerDaemon): string[] {
+ return daemon
+ .getStatus()
+ .config.workers.filter((w) => w.enabled)
+ .map((w) => w.type)
+ .sort();
+}
+
+describe('#3547 β€” daemon start --workers actually selects workers', () => {
+ let tempDir: string;
+
+ beforeEach(() => {
+ tempDir = mkdtempSync(join(tmpdir(), 'daemon-3547-test-'));
+ mkdirSync(join(tempDir, '.claude-flow', 'logs'), { recursive: true });
+ });
+
+ afterEach(() => {
+ rmSync(tempDir, { recursive: true, force: true });
+ process.removeAllListeners('SIGTERM');
+ process.removeAllListeners('SIGINT');
+ process.removeAllListeners('SIGHUP');
+ });
+
+ it('without an explicit selection, falls back to the full default worker set (pre-existing behavior)', () => {
+ const daemon = new WorkerDaemon(tempDir);
+ const types = enabledTypes(daemon);
+ expect(types.length).toBe(7);
+ expect(types).toContain('map');
+ expect(types).toContain('audit');
+ });
+
+ it('enables ONLY the requested workers when enabledWorkers is set', () => {
+ const daemon = new WorkerDaemon(tempDir, { enabledWorkers: ['map', 'audit'] });
+ expect(enabledTypes(daemon)).toEqual(['audit', 'map']);
+ });
+
+ it('disables every worker not in the selection, including ones DEFAULT_WORKERS turns on', () => {
+ const daemon = new WorkerDaemon(tempDir, { enabledWorkers: ['optimize'] });
+ const all = daemon.getStatus().config.workers;
+ expect(all.find((w) => w.type === 'optimize')?.enabled).toBe(true);
+ expect(all.find((w) => w.type === 'consolidate')?.enabled).toBe(false);
+ expect(all.find((w) => w.type === 'testgaps')?.enabled).toBe(false);
+ });
+
+ it('an unknown worker name in the selection simply enables nothing extra (no match, no crash)', () => {
+ const daemon = new WorkerDaemon(tempDir, { enabledWorkers: ['not-a-real-worker'] });
+ expect(enabledTypes(daemon)).toEqual([]);
+ });
+
+ it('wins over a stale daemon-state.json that previously enabled a different set', () => {
+ // Simulate an earlier daemon run that had the full default set enabled,
+ // persisted to disk β€” the exact "stale saved state" scenario #3547
+ // calls out: a plain restart must not silently resurrect it over an
+ // explicit new --workers selection.
+ writeFileSync(
+ join(tempDir, '.claude-flow', 'daemon-state.json'),
+ JSON.stringify({
+ running: false,
+ config: {
+ workers: [
+ { type: 'map', enabled: true },
+ { type: 'audit', enabled: true },
+ { type: 'optimize', enabled: true },
+ { type: 'consolidate', enabled: true },
+ { type: 'testgaps', enabled: true },
+ { type: 'backup', enabled: true },
+ { type: 'harness', enabled: true },
+ ],
+ },
+ workers: {},
+ })
+ );
+ const daemon = new WorkerDaemon(tempDir, { enabledWorkers: ['map'] });
+ expect(enabledTypes(daemon)).toEqual(['map']);
+ });
+
+ it('a later plain `daemon start` (no --workers) restores the selection from daemon-state.json (sticky)', () => {
+ // First run with an explicit selection; its result is what gets persisted
+ // to daemon-state.json by the real save path. We simulate that saved
+ // shape directly here rather than depending on saveState()'s timing.
+ writeFileSync(
+ join(tempDir, '.claude-flow', 'daemon-state.json'),
+ JSON.stringify({
+ running: false,
+ config: {
+ workers: [
+ { type: 'map', enabled: true },
+ { type: 'audit', enabled: false },
+ { type: 'optimize', enabled: false },
+ { type: 'consolidate', enabled: false },
+ { type: 'testgaps', enabled: false },
+ { type: 'backup', enabled: false },
+ { type: 'harness', enabled: false },
+ ],
+ },
+ workers: {},
+ })
+ );
+ // No enabledWorkers passed this time β€” a plain restart.
+ const daemon = new WorkerDaemon(tempDir);
+ expect(enabledTypes(daemon)).toEqual(['map']);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/daemon.ts b/v3/@claude-flow/cli/src/commands/daemon.ts
index dbbdbc67f3..a2e626067f 100644
--- a/v3/@claude-flow/cli/src/commands/daemon.ts
+++ b/v3/@claude-flow/cli/src/commands/daemon.ts
@@ -13,6 +13,12 @@ import { dirname, join, resolve, isAbsolute } from 'path';
import { homedir } from 'os';
import * as fs from 'fs';
+// #1968 / #3547: shared validator for `--workers <list>`, used both when
+// reading the flag into DaemonConfig (below) and when forwarding it to the
+// forked background child's argv (startBackgroundDaemon). One pattern, one
+// place, so the two paths can't drift out of sync on what they accept.
+const WORKERS_RE = /^[a-z][a-z0-9_-]*(,[a-z][a-z0-9_-]*)*$/;
+
// Start daemon subcommand
const startCommand: Command = {
name: 'start',
@@ -125,6 +131,22 @@ const startCommand: Command = {
}
}
+ // #3547 (#1968 follow-up): `--workers` was forwarded to the forked
+ // background child's argv (below, in startBackgroundDaemon) but never
+ // read into DaemonConfig here, so WorkerDaemon always fell back to
+ // DEFAULT_WORKERS (7 enabled) regardless of what was requested β€” in
+ // both foreground mode (this same action, run directly) and background
+ // mode (this same action, re-run in the forked child with --foreground
+ // and the forwarded --workers flag).
+ const rawWorkers = ctx.flags.workers as string | undefined;
+ if (typeof rawWorkers === 'string' && rawWorkers.length > 0) {
+ if (WORKERS_RE.test(rawWorkers)) {
+ config.enabledWorkers = rawWorkers.split(',');
+ } else if (!quiet) {
+ output.printWarning(`Ignoring invalid --workers value: ${sanitize(rawWorkers)}`);
+ }
+ }
+
// Check if background daemon already running (skip if we ARE the daemon process)
//
// #2407 β€” without an atomic lockfile, N concurrent `daemon start` calls
@@ -532,7 +554,6 @@ async function startBackgroundDaemon(projectRoot: string, quiet: boolean, forwar
// through β€” argv goes straight to a forked process so reject anything
// that doesn't look like a comma-separated worker-name list or one of
// the allowed sandbox modes.
- const WORKERS_RE = /^[a-z][a-z0-9_-]*(,[a-z][a-z0-9_-]*)*$/;
if (typeof workers === 'string' && workers.length > 0 && WORKERS_RE.test(workers)) {
forkArgs.push('--workers', workers);
}
diff --git a/v3/@claude-flow/cli/src/services/worker-daemon.ts b/v3/@claude-flow/cli/src/services/worker-daemon.ts
index d781dfe2bc..2bcb490eb1 100644
--- a/v3/@claude-flow/cli/src/services/worker-daemon.ts
+++ b/v3/@claude-flow/cli/src/services/worker-daemon.ts
@@ -134,6 +134,12 @@ export interface DaemonConfig {
// .claude-flow/config.json, or RUFLO_DAEMON_AI_WORKERS=1.
aiWorkersEnabled: boolean;
workers: WorkerConfig[];
+ // #3547 (#1968 follow-up): explicit `daemon start --workers <list>`
+ // selection. When set, only these worker types are enabled β€” applied
+ // after daemon-state.json restore (initializeWorkerStates) so a stale
+ // saved state can never re-enable a worker the operator just deselected.
+ // Undefined means "no explicit selection": DEFAULT_WORKERS/state decide.
+ enabledWorkers?: string[];
}
// Worker configuration with staggered offsets to prevent overlap
@@ -318,6 +324,7 @@ export class WorkerDaemon extends EventEmitter {
?? fileConfig.aiWorkersEnabled
?? (process.env.RUFLO_DAEMON_AI_WORKERS === '1'),
workers: config?.workers ?? DEFAULT_WORKERS,
+ enabledWorkers: config?.enabledWorkers,
};
// #2935 β€” deferred from readDaemonConfigFromFile(): this.config (and
@@ -855,6 +862,18 @@ export class WorkerDaemon extends EventEmitter {
}
}
+ // #3547 (#1968 follow-up): an explicit `--workers` selection wins over
+ // both DEFAULT_WORKERS and whatever daemon-state.json just restored
+ // above β€” otherwise `daemon start --workers map,audit` always ran the
+ // full default set (or a stale saved selection) because nothing ever
+ // applied the requested list to `enabled`.
+ if (this.config.enabledWorkers) {
+ const selected = new Set(this.config.enabledWorkers);
+ for (const workerConfig of this.config.workers) {
+ workerConfig.enabled = selected.has(workerConfig.type);
+ }
+ }
+
// Initialize any missing workers
for (const workerConfig of this.config.workers) {
if (!this.workers.has(workerConfig.type)) {