diff --git a/v3/@claude-flow/cli/__tests__/mcp-corrupt-store-4737.test.ts b/v3/@claude-flow/cli/__tests__/mcp-corrupt-store-4737.test.ts new file mode 100644 index 0000000000..77eb4bea51 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/mcp-corrupt-store-4737.test.ts @@ -0,0 +1,273 @@ +/** + * #4737: a corrupt workflow, hive-mind, or swarm store is preserved and + * reported. A parse error used to return an empty store, and the next save + * overwrote the file (#4685 class). + */ +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { + chmodSync, + existsSync, + mkdirSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, + statSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { basename, dirname, join } from 'node:path'; +import { hiveMindTools } from '../src/mcp-tools/hive-mind-tools.js'; +import { readPreservedJsonStore } from '../src/mcp-tools/record-store.js'; +import { loadSwarmStore, swarmTools } from '../src/mcp-tools/swarm-tools.js'; +import { workflowTools } from '../src/mcp-tools/workflow-tools.js'; + +const tool = (tools: { name: string; handler: (input: Record) => Promise }[], name: string) => { + const found = tools.find(entry => entry.name === name); + if (!found) throw new Error(`MCP tool not registered: ${name}`); + return found; +}; + +const workflowList = tool(workflowTools, 'workflow_list'); +const workflowCreate = tool(workflowTools, 'workflow_create'); +const hiveStatus = tool(hiveMindTools, 'hive-mind_status'); +const hiveInit = tool(hiveMindTools, 'hive-mind_init'); +const swarmStatus = tool(swarmTools, 'swarm_status'); +const swarmInit = tool(swarmTools, 'swarm_init'); + +const CORRUPT = '{"workflows":{"keep":true}, broken'; + +function corruptCopies(file: string): string[] { + const dir = dirname(file); + if (!existsSync(dir)) return []; + const prefix = `${basename(file)}.corrupt-`; + return readdirSync(dir).filter(name => name.startsWith(prefix)).sort(); +} + +describe('readPreservedJsonStore (#4737)', () => { + let dir: string; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'ruflo-4737-store-')); + }); + + afterEach(() => { + rmSync(dir, { recursive: true, force: true }); + }); + + it('treats a missing file as empty and writes nothing', () => { + const file = join(dir, 'store.json'); + const store = readPreservedJsonStore(file, 'workflow', () => ({ workflows: {}, version: '3.0.0' })); + expect(store).toEqual({ workflows: {}, version: '3.0.0' }); + expect(existsSync(file)).toBe(false); + expect(corruptCopies(file)).toEqual([]); + }); + + it('backs up unparseable JSON, reports it, and leaves the original bytes', () => { + const file = join(dir, 'store.json'); + writeFileSync(file, CORRUPT, { mode: 0o600 }); + const before = readFileSync(file); + + expect(() => readPreservedJsonStore(file, 'workflow', () => ({ workflows: {} }))) + .toThrow(/Invalid workflow store at .*store\.json.*corrupt file backed up to .*store\.json\.corrupt-\d+.*existing data was preserved/); + + expect(readFileSync(file)).toEqual(before); + expect(statSync(file).mode & 0o777).toBe(0o600); + const copies = corruptCopies(file); + expect(copies).toHaveLength(1); + expect(copies[0]).toMatch(/\.corrupt-\d+$/); + const backup = join(dir, copies[0]); + expect(readFileSync(backup)).toEqual(before); + expect(statSync(backup).mode & 0o777).toBe(0o600); + }); + + it('backs up a non-object document instead of treating it as an empty store', () => { + for (const text of ['[]', 'null', '"nope"', '1']) { + const file = join(dir, `store-${text.length}.json`); + writeFileSync(file, text); + expect(() => readPreservedJsonStore(file, 'hive-mind', () => ({ initialized: false }))).toThrow(/existing data was preserved/); + expect(readFileSync(file, 'utf8')).toBe(text); + expect(corruptCopies(file)).toHaveLength(1); + } + }); + + it('backs up an object whose record map is the wrong type', () => { + const file = join(dir, 'store.json'); + const text = JSON.stringify({ swarms: [], version: '3.0.0' }); + writeFileSync(file, text); + expect(() => readPreservedJsonStore( + file, + 'swarm', + () => ({ swarms: {}, version: '3.0.0' }), + (store) => { + const swarms = (store as { swarms?: unknown }).swarms; + return swarms === undefined || (!!swarms && typeof swarms === 'object' && !Array.isArray(swarms)); + }, + )).toThrow(/Invalid swarm store/); + expect(readFileSync(file, 'utf8')).toBe(text); + expect(corruptCopies(file)).toHaveLength(1); + }); + + it('returns a valid object and does not create a backup', () => { + const file = join(dir, 'store.json'); + const text = JSON.stringify({ swarms: { a: { swarmId: 'a' } }, version: '3.0.0' }); + writeFileSync(file, text); + const store = readPreservedJsonStore<{ swarms: Record; version: string }>( + file, + 'swarm', + () => ({ swarms: {}, version: '3.0.0' }), + ); + expect(store.swarms.a).toEqual({ swarmId: 'a' }); + expect(corruptCopies(file)).toEqual([]); + expect(readFileSync(file, 'utf8')).toBe(text); + }); + + it('reports an unreadable path without backing it up or replacing it', () => { + const file = join(dir, 'store.json'); + mkdirSync(file); + expect(() => readPreservedJsonStore(file, 'workflow', () => ({ workflows: {} }))).toThrow(); + expect(statSync(file).isDirectory()).toBe(true); + expect(corruptCopies(file)).toEqual([]); + }); +}); + +describe('workflow, hive-mind, and swarm tools preserve a corrupt store (#4737)', () => { + let dir: string; + let prevCwd: string | undefined; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'ruflo-4737-tools-')); + prevCwd = process.env.CLAUDE_FLOW_CWD; + process.env.CLAUDE_FLOW_CWD = dir; + }); + + afterEach(() => { + if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD; + else process.env.CLAUDE_FLOW_CWD = prevCwd; + rmSync(dir, { recursive: true, force: true }); + }); + + const cases: Array<{ + label: string; + file: (root: string) => string; + text: string; + read: () => Promise; + write: () => Promise; + }> = [ + { + label: 'workflow', + file: root => join(root, '.claude-flow', 'workflows', 'store.json'), + text: CORRUPT, + read: () => workflowList.handler({}), + write: () => workflowCreate.handler({ name: 'should-not-land' }), + }, + { + label: 'hive-mind', + file: root => join(root, '.claude-flow', 'hive-mind', 'state.json'), + text: '{"initialized":true,"hiveToken":"secret-token", oops', + read: () => hiveStatus.handler({}), + write: () => hiveInit.handler({ topology: 'mesh' }), + }, + { + label: 'swarm', + file: root => join(root, '.claude-flow', 'swarm', 'swarm-state.json'), + text: '{"swarms":{"swarm-1":{"status":"running"}}, trailing', + read: () => swarmStatus.handler({}), + write: () => swarmInit.handler({ topology: 'mesh' }), + }, + ]; + + for (const entry of cases) { + it(`${entry.label}: a read and a later save report the error and do not overwrite the file`, async () => { + const file = entry.file(dir); + mkdirSync(dirname(file), { recursive: true }); + writeFileSync(file, entry.text, { mode: 0o600 }); + chmodSync(file, 0o600); + + await expect(entry.read()).rejects.toThrow(new RegExp(`Invalid ${entry.label} store`)); + await expect(entry.write()).rejects.toThrow(/existing data was preserved/); + + expect(readFileSync(file, 'utf8')).toBe(entry.text); + expect(statSync(file).mode & 0o777).toBe(0o600); + const copies = corruptCopies(file); + expect(copies.length).toBeGreaterThan(0); + expect(copies.every(name => new RegExp(`^${basename(file).replace('.', '\\.')}\\.corrupt-\\d+(-\\d+)?$`).test(name))).toBe(true); + for (const name of copies) { + expect(readFileSync(join(dirname(file), name), 'utf8')).toBe(entry.text); + } + }); + } + + it('a missing store is still empty, and a valid store still loads', async () => { + const listed = await workflowList.handler({}) as { workflows: unknown[]; total: number }; + expect(listed.total).toBe(0); + expect(existsSync(join(dir, '.claude-flow', 'workflows', 'store.json'))).toBe(false); + + const created = await workflowCreate.handler({ name: 'kept' }) as { workflowId: string }; + const storePath = join(dir, '.claude-flow', 'workflows', 'store.json'); + const again = await workflowList.handler({}) as { workflows: Array<{ workflowId: string }>; total: number }; + expect(again.total).toBe(1); + expect(again.workflows[0].workflowId).toBe(created.workflowId); + expect(corruptCopies(storePath)).toEqual([]); + + const hivePath = join(dir, '.claude-flow', 'hive-mind', 'state.json'); + mkdirSync(dirname(hivePath), { recursive: true }); + writeFileSync(hivePath, JSON.stringify({ + initialized: true, + topology: 'mesh', + workers: [], + consensus: { pending: [], history: [] }, + sharedMemory: {}, + createdAt: '2020-01-01T00:00:00.000Z', + updatedAt: '2020-01-01T00:00:00.000Z', + })); + const hive = await hiveStatus.handler({}) as { status: string; topology: string }; + expect(hive.status).toBe('active'); + expect(hive.topology).toBe('mesh'); + expect(corruptCopies(hivePath)).toEqual([]); + + const swarmPath = join(dir, '.claude-flow', 'swarm', 'swarm-state.json'); + mkdirSync(dirname(swarmPath), { recursive: true }); + writeFileSync(swarmPath, JSON.stringify({ + swarms: { + 'swarm-1': { + swarmId: 'swarm-1', + topology: 'mesh', + maxAgents: 3, + status: 'paused', + agents: [], + tasks: [], + config: {}, + createdAt: '2020-01-01T00:00:00.000Z', + updatedAt: '2020-01-01T00:00:00.000Z', + }, + }, + version: '3.0.0', + })); + const swarm = await swarmStatus.handler({}) as { swarmId: string; status: string }; + expect(swarm.swarmId).toBe('swarm-1'); + expect(swarm.status).toBe('paused'); + expect(corruptCopies(swarmPath)).toEqual([]); + expect(Object.getPrototypeOf(loadSwarmStore().swarms)).toBeNull(); + }); + + it('gives a missing swarm record map a null prototype and does not create the file', () => { + const swarmPath = join(dir, '.claude-flow', 'swarm', 'swarm-state.json'); + const store = loadSwarmStore(); + expect(Object.getPrototypeOf(store.swarms)).toBeNull(); + expect(store.swarms.constructor).toBeUndefined(); + expect(existsSync(swarmPath)).toBe(false); + }); + + it('a workflow store whose workflows map is an array is not replaced by defaults', async () => { + const file = join(dir, '.claude-flow', 'workflows', 'store.json'); + mkdirSync(dirname(file), { recursive: true }); + const text = JSON.stringify({ workflows: ['not-a-map'], templates: {}, version: '3.0.0' }); + writeFileSync(file, text); + + await expect(workflowList.handler({})).rejects.toThrow(/Invalid workflow store/); + await expect(workflowCreate.handler({ name: 'should-not-land' })).rejects.toThrow(/existing data was preserved/); + expect(readFileSync(file, 'utf8')).toBe(text); + expect(corruptCopies(file).length).toBeGreaterThan(0); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts index f920f62406..909ae55842 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts @@ -9,6 +9,7 @@ import { join } from 'node:path'; import { randomBytes, createHash, timingSafeEqual } from 'node:crypto'; import { type MCPTool, getProjectCwd } from './types.js'; import { validateIdentifier, validateText } from './validate-input.js'; +import { readPreservedJsonStore } from './record-store.js'; // Storage paths const STORAGE_DIR = '.claude-flow'; @@ -316,16 +317,10 @@ function ensureHiveDir(): void { } function loadHiveState(): HiveState { - try { - const path = getHivePath(); - if (existsSync(path)) { - const data = readFileSync(path, 'utf-8'); - return JSON.parse(data); - } - } catch { - // Return default state on error - } - return { + // #4737: a parse error used to return a fresh hive, and the next save + // overwrote state.json (including its capability token). Only a missing + // file is empty; corrupt bytes are copied aside and the error is reported. + return readPreservedJsonStore(getHivePath(), 'hive-mind', () => ({ initialized: false, topology: 'mesh', workers: [], @@ -333,7 +328,7 @@ function loadHiveState(): HiveState { sharedMemory: {}, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), - }; + })); } function saveHiveState(state: HiveState): void { diff --git a/v3/@claude-flow/cli/src/mcp-tools/record-store.ts b/v3/@claude-flow/cli/src/mcp-tools/record-store.ts index e309a374ff..1d2c8e8577 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/record-store.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/record-store.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs'; +import { chmodSync, copyFileSync, existsSync, readFileSync, statSync } from 'node:fs'; /** * Give a record map a null prototype. Record keys come from MCP tool input; on @@ -36,3 +36,67 @@ export function readRecordStore(path: string, collection: string, empty: () = nullPrototypeRecords(records); return store as T; } + +/** + * JSON object store read for workflow, hive-mind, and swarm (#4737). + * Same rule as {@link readRecordStore}: only a missing file is empty. + * A parse failure, a non-object document, or a document `accept` rejects is + * copied to `${path}.corrupt-` and thrown, so a later save cannot + * replace the original. + */ +export function readPreservedJsonStore( + path: string, + label: string, + empty: () => T, + accept: (store: T) => boolean = () => true, +): T { + let text: string; + try { + text = readFileSync(path, 'utf-8'); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return empty(); + throw error; + } + + let parsed: unknown; + try { + parsed = JSON.parse(text); + } catch (error) { + const why = error instanceof Error ? error.message : String(error); + refuseCorruptStore(path, label, why); + } + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || !accept(parsed as T)) { + refuseCorruptStore(path, label); + } + return parsed as T; +} + +function refuseCorruptStore(path: string, label: string, detail?: string): never { + let where = 'existing data was preserved'; + try { + where = `corrupt file backed up to ${backupCorruptStore(path)}; existing data was preserved`; + } catch (error) { + const why = error instanceof Error ? error.message : String(error); + where = `backup failed (${why}); existing data was preserved`; + } + const suffix = detail ? ` (${detail})` : ''; + throw new Error(`Invalid ${label} store at ${path}${suffix}; ${where}`); +} + +/** Copy `path` to `${path}.corrupt-` without removing the original. */ +function backupCorruptStore(path: string): string { + const stamp = Date.now(); + let backup = `${path}.corrupt-${stamp}`; + let n = 0; + while (existsSync(backup)) { + n += 1; + backup = `${path}.corrupt-${stamp}-${n}`; + } + copyFileSync(path, backup); + try { + chmodSync(backup, statSync(path).mode & 0o777); + } catch { + /* content is already copied; mode is best-effort */ + } + return backup; +} diff --git a/v3/@claude-flow/cli/src/mcp-tools/swarm-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/swarm-tools.ts index 02afc94d41..783a987c0a 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/swarm-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/swarm-tools.ts @@ -10,14 +10,13 @@ import { existsSync, mkdirSync, openSync, - readFileSync, renameSync, statSync, unlinkSync, writeFileSync, } from 'node:fs'; import { join } from 'node:path'; -import { nullPrototypeRecords } from './record-store.js'; +import { nullPrototypeRecords, readPreservedJsonStore } from './record-store.js'; import { type MCPTool, getProjectCwd } from './types.js'; import { validateIdentifier } from './validate-input.js'; import { @@ -135,14 +134,21 @@ function reconcileOrphanSwarms(store: SwarmStore): number { // #2085 — exported so `agent-tools.ts agent_spawn` can push into // `swarm.agents` (the field `swarm_status` reads). export function loadSwarmStore(): SwarmStore { - let store: SwarmStore = { swarms: {}, version: '3.0.0' }; - try { - const path = getSwarmStatePath(); - if (existsSync(path)) { - store = JSON.parse(readFileSync(path, 'utf-8')); - } - } catch { /* fall through with default */ } - if (!store || typeof store !== 'object' || Array.isArray(store)) store = { swarms: {}, version: '3.0.0' }; + // #4737: a parse error used to fall through to an empty store, and the next + // save (or orphan reconcile) overwrote swarm-state.json. Only a missing + // file is empty; corrupt bytes are copied aside and the error is reported. + const store = readPreservedJsonStore( + getSwarmStatePath(), + 'swarm', + () => ({ swarms: {}, version: '3.0.0' }), + (candidate) => { + const swarms = candidate.swarms as unknown; + return swarms === undefined || (!!swarms && typeof swarms === 'object' && !Array.isArray(swarms)); + }, + ); + if (!store.swarms) store.swarms = {}; + // Record ids come from MCP input. A null prototype stops `__proto__` and + // `constructor` from resolving onto Object.prototype. store.swarms = nullPrototypeRecords(store.swarms); // #1799 — reconcile orphans on every load and persist if anything changed. diff --git a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts index 92f44df2c0..42996c16cb 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts @@ -9,6 +9,7 @@ import { join } from 'node:path'; import { type MCPTool, getProjectCwd } from './types.js'; import { validateIdentifier, validatePath, validateText } from './validate-input.js'; import { executeAgentTask } from './agent-execute-core.js'; +import { readPreservedJsonStore } from './record-store.js'; // Storage paths const STORAGE_DIR = '.claude-flow'; @@ -112,17 +113,25 @@ function withRecordMaps(store: S, keys: readonly string[]): S const WORKFLOW_MAPS = ['workflows', 'templates'] as const; +function isRecordMap(value: unknown): boolean { + return !!value && typeof value === 'object' && !Array.isArray(value); +} + function loadWorkflowStore(): WorkflowStore { - try { - const path = getWorkflowPath(); - if (existsSync(path)) { - const data = JSON.parse(readFileSync(path, 'utf-8')); - if (data && typeof data === 'object' && !Array.isArray(data)) return withRecordMaps(data, WORKFLOW_MAPS); - } - } catch { - // Return default store on error - } - return withRecordMaps({ workflows: {}, templates: {}, version: '3.0.0' } as WorkflowStore, WORKFLOW_MAPS); + // #4737: a parse error used to return an empty store, and the next save + // overwrote the file. Only a missing file is empty; corrupt bytes are + // copied aside and the error is reported. + const path = getWorkflowPath(); + const data = readPreservedJsonStore( + path, + 'workflow', + () => ({ workflows: {}, templates: {}, version: '3.0.0' }) as WorkflowStore, + (store) => WORKFLOW_MAPS.every((key) => { + const map = (store as unknown as Record)[key]; + return map === undefined || isRecordMap(map); + }), + ); + return withRecordMaps(data, WORKFLOW_MAPS); } function saveWorkflowStore(store: WorkflowStore): void {