Files
ruflo/v3/@claude-flow/cli/__tests__/daemon-queue-user-dir.test.ts
T
Dragan Spiridonov 8f0c410790 fix(daemon): the dispatch queue lives in the user's state dir, not the project
Follow-up to #4515. Queue entries now stay behind the repository-supervisor
gate, but in a single clone the daemon elects itself supervisor at start, so a
`.claude-flow/daemon-queue/*.json` committed in a cloned repository still ran
for a user with AI workers enabled.

- The queue is <state>/daemon-queue/<sha256(realpath.native(project))[:16]>/,
  where <state> is RUFLO_AI_BUDGET_DIR when absolute, else ~/.claude-flow (an
  empty or relative override would resolve inside the project again).
  hooks_worker-dispatch writes there (dir 0700, files 0600); the daemon reads
  only there. A queue directory inside the project is ignored.
- An entry for a worker the user disabled is skipped (.processed/disabled-*).
- Polls do not overlap: an entry moved to .processed/ only after its worker
  finished, so each 5 s poll during a long run started it again.
- No further entry starts once the daemon stops.
2026-10-26 20:01:33 +01:00

163 lines
6.4 KiB
TypeScript

/**
* The MCP worker-dispatch queue (#1845) starts workers, so a cloned repository
* must not be able to fill it, and an entry must not do more than a scheduled
* fire could:
* - the queue lives in the user's state dir, keyed by project; entries a
* repository ships under `.claude-flow/daemon-queue` are never read;
* - an entry runs under the repository-supervisor gate, never as a manual
* trigger, and never for a worker the user has disabled;
* - overlapping polls do not run one entry twice.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { WorkerDaemon } from '../src/services/worker-daemon.js';
import { dispatchQueueDir } from '../src/services/dispatch-queue.js';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, existsSync } from 'fs';
import { isAbsolute, join } from 'path';
import { tmpdir } from 'os';
describe('MCP dispatch queue', () => {
let tempDir: string;
let stateDir: string;
const savedStateDir = process.env.RUFLO_AI_BUDGET_DIR;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), 'daemon-queue-gate-'));
stateDir = mkdtempSync(join(tmpdir(), 'daemon-queue-state-'));
mkdirSync(join(tempDir, '.claude-flow', 'logs'), { recursive: true });
process.env.RUFLO_AI_BUDGET_DIR = stateDir;
delete process.env.RUFLO_DAEMON_AI_WORKERS;
});
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true });
rmSync(stateDir, { recursive: true, force: true });
if (savedStateDir === undefined) delete process.env.RUFLO_AI_BUDGET_DIR;
else process.env.RUFLO_AI_BUDGET_DIR = savedStateDir;
process.removeAllListeners('SIGTERM');
process.removeAllListeners('SIGINT');
process.removeAllListeners('SIGHUP');
});
/**
* A consenting daemon with a stub headless executor; `supervisor` sets the gate.
* Waits for the constructor's own `claude --version` probe first, so its result
* (false where Claude Code is not installed, e.g. CI) cannot overwrite the stub.
*/
async function consentingDaemon(supervisor: boolean, executeMs = 0) {
const daemon = new WorkerDaemon(tempDir, { aiWorkersEnabled: true });
await (daemon as unknown as { headlessInitPromise: Promise<void> }).headlessInitPromise;
const execute = vi.fn(async () => {
if (executeMs) await new Promise((r) => setTimeout(r, executeMs));
return { success: true, output: {}, durationMs: 1 };
});
const d = daemon as unknown as Record<string, unknown>;
d.headlessAvailable = true;
d.headlessExecutor = { execute, getActiveChildPids: () => [], on: () => undefined };
d.running = true;
d.isRepositorySupervisor = () => supervisor;
const poll = () => (d.processDispatchQueue as () => Promise<void>).call(d);
return { daemon, execute, poll };
}
function enqueue(dir: string, name: string, trigger: string) {
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, `${name}.json`), JSON.stringify({ trigger, workerId: name }));
}
it('never reads a queue a repository ships under .claude-flow/daemon-queue', async () => {
const { execute, poll } = await consentingDaemon(true);
const repoQueue = join(tempDir, '.claude-flow', 'daemon-queue');
enqueue(repoQueue, 'seed', 'audit');
await poll();
expect(execute).not.toHaveBeenCalled();
expect(existsSync(join(repoQueue, 'seed.json'))).toBe(true);
});
it('runs an entry from the user queue on the supervisor', async () => {
const { execute, poll } = await consentingDaemon(true);
const queue = dispatchQueueDir(tempDir);
enqueue(queue, 'mine', 'audit');
await poll();
expect(execute).toHaveBeenCalledTimes(1);
expect(existsSync(join(queue, '.processed', 'mine.json'))).toBe(true);
});
it('runs an entry under the supervisor gate, not as a manual trigger', async () => {
const { execute, poll } = await consentingDaemon(false);
const queue = dispatchQueueDir(tempDir);
enqueue(queue, 'mine', 'audit');
await poll();
expect(execute).not.toHaveBeenCalled();
expect(existsSync(join(queue, '.processed', 'mine.json'))).toBe(true);
});
it('positive control: an explicit trigger on a non-supervisor does reach the executor', async () => {
const { daemon, execute } = await consentingDaemon(false);
await daemon.triggerWorker('audit').catch(() => undefined);
expect(execute).toHaveBeenCalled();
});
it('skips an entry for a disabled worker', async () => {
const { daemon, execute, poll } = await consentingDaemon(true);
const audit = (daemon as unknown as { config: { workers: Array<{ type: string; enabled: boolean }> } })
.config.workers.find((w) => w.type === 'audit')!;
audit.enabled = false;
const queue = dispatchQueueDir(tempDir);
enqueue(queue, 'off', 'audit');
await poll();
expect(execute).not.toHaveBeenCalled();
expect(existsSync(join(queue, '.processed', 'disabled-off.json'))).toBe(true);
});
it('does not run one entry twice when polls overlap', async () => {
const { execute, poll } = await consentingDaemon(true, 200);
enqueue(dispatchQueueDir(tempDir), 'slow', 'audit');
const first = poll();
await new Promise((r) => setTimeout(r, 50));
await Promise.all([first, poll()]);
expect(execute).toHaveBeenCalledTimes(1);
});
it('stops draining when the daemon stops', async () => {
const { daemon, execute, poll } = await consentingDaemon(true, 100);
const queue = dispatchQueueDir(tempDir);
for (const name of ['a', 'b', 'c']) enqueue(queue, name, 'audit');
const draining = poll();
await new Promise((r) => setTimeout(r, 30));
(daemon as unknown as { running: boolean }).running = false;
await draining;
expect(execute).toHaveBeenCalledTimes(1);
});
it.each(['', 'relative/dir'])('ignores a non-absolute state dir override (%j)', (value) => {
process.env.RUFLO_AI_BUDGET_DIR = value;
const dir = dispatchQueueDir(tempDir);
expect(isAbsolute(dir)).toBe(true);
expect(dir.startsWith(tempDir)).toBe(false);
});
it('keys the queue by project inside the user state dir', () => {
const other = mkdtempSync(join(tmpdir(), 'daemon-queue-other-'));
try {
expect(dispatchQueueDir(tempDir).startsWith(join(stateDir, 'daemon-queue'))).toBe(true);
expect(dispatchQueueDir(tempDir)).toBe(dispatchQueueDir(join(tempDir, '.')));
expect(dispatchQueueDir(tempDir)).not.toBe(dispatchQueueDir(other));
} finally {
rmSync(other, { recursive: true, force: true });
}
});
});