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(memory): reject a missing required query/key at the memory tool boundary (#3374) (#3400)

rUv7b4e97a316d5
diff --git a/v3/@claude-flow/cli/__tests__/issue-3374-memory-missing-required-param.test.ts b/v3/@claude-flow/cli/__tests__/issue-3374-memory-missing-required-param.test.ts
new file mode 100644
index 0000000000..3bbac34db5
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/issue-3374-memory-missing-required-param.test.ts
@@ -0,0 +1,137 @@
+/**
+ * Regression guard for issue #3374 — `memory_search` declares
+ * `required: ['query']`, but nothing enforced it. A request without `query`
+ * (e.g. `{"pattern": "...", "limit": 5}`) was accepted, `validateMemoryInput`
+ * let the `undefined` through (every check in it is truthiness-guarded), and
+ * the value was carried down to `generateHashEmbedding`'s `text.toLowerCase()`
+ * — surfacing as the unhelpful
+ * {"results":[],"total":0,"error":"Cannot read properties of undefined (reading 'toLowerCase')"}
+ *
+ * The fix rejects a missing/non-string/empty required parameter at the tool
+ * boundary with an error that names the parameter and carries a stable code.
+ * The sibling tools that share the same shape (memory_search_unified with
+ * `query`, memory_store / memory_retrieve / memory_delete with `key`) get the
+ * same guard.
+ *
+ * The discriminating assertion is that the memory layer is NEVER called for a
+ * rejected request: pre-fix, each handler forwarded `undefined` to it.
+ */
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// Mock fs so ensureInitialized()'s legacy-migration check is a no-op.
+vi.mock('fs', () => ({
+ existsSync: vi.fn(() => false),
+ mkdirSync: vi.fn(),
+ readdirSync: vi.fn(() => []),
+ readFileSync: vi.fn(() => '{}'),
+ unlinkSync: vi.fn(),
+ writeFileSync: vi.fn(),
+}));
+
+const layer = vi.hoisted(() => ({
+ searchEntries: vi.fn(async () => ({ success: true, results: [], searchTime: 1 })),
+ storeEntry: vi.fn(async () => ({ success: true, id: 'mock-id' })),
+ getEntry: vi.fn(async () => ({ found: false })),
+ deleteEntry: vi.fn(async () => ({ deleted: true })),
+ listEntries: vi.fn(async () => ({ success: true, entries: [], total: 0 })),
+}));
+
+vi.mock('../src/memory/memory-initializer.js', () => ({
+ generateEmbedding: vi.fn(async () => ({ embedding: new Array(384).fill(0.1), dimensions: 384, model: 'mock' })),
+ storeEntry: layer.storeEntry,
+ searchEntries: layer.searchEntries,
+ listEntries: layer.listEntries,
+ getEntry: layer.getEntry,
+ deleteEntry: layer.deleteEntry,
+ getStats: vi.fn(async () => ({ totalEntries: 0 })),
+ initializeDatabase: vi.fn(async () => ({ success: true })),
+ initializeMemoryDatabase: vi.fn(async () => ({ success: true })),
+ checkMemoryInitialization: vi.fn(async () => ({ initialized: true, version: '3.0.0' })),
+ migrateFromLegacy: vi.fn(async () => ({ success: true, migrated: 0 })),
+}));
+
+import { memoryTools } from '../src/mcp-tools/memory-tools.js';
+
+function tool(name: string) {
+ const t = memoryTools.find((x) => x.name === name);
+ if (!t) throw new Error(`tool ${name} not registered`);
+ return t;
+}
+
+type Res = { error?: string; code?: string; results?: unknown[]; total?: number; success?: boolean };
+
+describe('#3374 memory tools reject a missing required parameter at the boundary', () => {
+ beforeEach(() => {
+ Object.values(layer).forEach((fn) => fn.mockClear());
+ });
+
+ describe('memory_search', () => {
+ it('rejects the issue payload ({pattern, limit}) without reaching searchEntries', async () => {
+ const r = (await tool('memory_search').handler({ pattern: 'cross-agent proof bridge', limit: 5 })) as Res;
+ expect(layer.searchEntries).not.toHaveBeenCalled();
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ expect(r.error).toContain('"query"');
+ expect(r.error).not.toContain('toLowerCase');
+ expect(r.results).toEqual([]);
+ expect(r.total).toBe(0);
+ });
+
+ it.each([
+ ['empty string', ''],
+ ['non-string', 42],
+ ['null', null],
+ ])('rejects query=%s', async (_label, query) => {
+ const r = (await tool('memory_search').handler({ query })) as Res;
+ expect(layer.searchEntries).not.toHaveBeenCalled();
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ });
+
+ it('a well-formed query still reaches the search layer', async () => {
+ const r = (await tool('memory_search').handler({ query: 'cross-agent proof bridge', limit: 5 })) as Res;
+ expect(layer.searchEntries).toHaveBeenCalledTimes(1);
+ expect(r.code).toBeUndefined();
+ expect(r.error).toBeUndefined();
+ });
+ });
+
+ it('memory_search_unified rejects a missing query without searching', async () => {
+ const r = (await tool('memory_search_unified').handler({ pattern: 'x' })) as Res;
+ expect(layer.searchEntries).not.toHaveBeenCalled();
+ expect(layer.listEntries).not.toHaveBeenCalled();
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ expect(r.error).toContain('"query"');
+ });
+
+ it('memory_store rejects a missing key without storing', async () => {
+ const r = (await tool('memory_store').handler({ value: 'some value' })) as Res;
+ expect(layer.storeEntry).not.toHaveBeenCalled();
+ expect(r.success).toBe(false);
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ expect(r.error).toContain('"key"');
+ });
+
+ it('memory_retrieve rejects a missing key without reading', async () => {
+ const r = (await tool('memory_retrieve').handler({ namespace: 'default' })) as Res;
+ expect(layer.getEntry).not.toHaveBeenCalled();
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ expect(r.error).toContain('"key"');
+ });
+
+ it('memory_delete rejects a missing key without deleting', async () => {
+ const r = (await tool('memory_delete').handler({ namespace: 'default' })) as Res;
+ expect(layer.deleteEntry).not.toHaveBeenCalled();
+ expect(r.success).toBe(false);
+ expect(r.code).toBe('MISSING_REQUIRED_PARAM');
+ expect(r.error).toContain('"key"');
+ });
+
+ it('well-formed store/retrieve/delete still reach the memory layer', async () => {
+ await tool('memory_store').handler({ key: 'k', value: 'v' });
+ await tool('memory_retrieve').handler({ key: 'k' });
+ await tool('memory_delete').handler({ key: 'k' });
+ expect(layer.storeEntry).toHaveBeenCalledTimes(1);
+ expect(layer.getEntry).toHaveBeenCalledTimes(1);
+ expect(layer.deleteEntry).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
index 8836fc0fbc..2ca7fae315 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
@@ -87,6 +87,31 @@ function validateMemoryInput(key?: string, value?: string, query?: string, names
}
}
+/**
+ * #3374 — presence check for a schema-`required` string parameter.
+ *
+ * `validateMemoryInput` above is a bounds-and-charset validator: every branch
+ * is truthiness-guarded, so an omitted parameter passes it silently. Nothing
+ * else enforces `inputSchema.required` for these tools, so without this an
+ * omitted `query` travelled down to `generateHashEmbedding`'s
+ * `text.toLowerCase()` and came back as an unrelated TypeError.
+ */
+const MISSING_REQUIRED_PARAM = 'MISSING_REQUIRED_PARAM' as const;
+
+function missingRequiredString(
+ input: Record<string, unknown>,
+ param: string,
+ tool: string,
+): { error: string; code: typeof MISSING_REQUIRED_PARAM } | null {
+ const v = input[param];
+ if (typeof v === 'string' && v.length > 0) return null;
+ const got = v === undefined ? 'it was omitted' : v === '' ? 'it was an empty string' : `got ${v === null ? 'null' : typeof v}`;
+ return {
+ error: `${tool}: required parameter "${param}" must be a non-empty string (${got})`,
+ code: MISSING_REQUIRED_PARAM,
+ };
+}
+
// #1884 — sanitize a key produced from arbitrary input (markdown headings,
// frontmatter names, file names) so it survives validateMemoryInput on the
// read/delete path. Replaces every dangerous char with `_`. Truncates to
@@ -428,6 +453,11 @@ export const memoryTools: MCPTool[] = [
required: ['key', 'value'],
},
handler: async (input) => {
+ const missingKey = missingRequiredString(input, 'key', 'memory_store');
+ if (missingKey) {
+ return { success: false, key: input.key, stored: false, hasEmbedding: false, ...missingKey };
+ }
+
await ensureInitialized();
const { storeEntry } = await getMemoryFunctions();
@@ -506,6 +536,11 @@ export const memoryTools: MCPTool[] = [
required: ['key'],
},
handler: async (input) => {
+ const missingKey = missingRequiredString(input, 'key', 'memory_retrieve');
+ if (missingKey) {
+ return { key: input.key, namespace: input.namespace, value: null, found: false, ...missingKey };
+ }
+
await ensureInitialized();
const { getEntry } = await getMemoryFunctions();
@@ -578,6 +613,11 @@ export const memoryTools: MCPTool[] = [
required: ['query'],
},
handler: async (input) => {
+ const missingQuery = missingRequiredString(input, 'query', 'memory_search');
+ if (missingQuery) {
+ return { query: input.query, results: [], total: 0, ...missingQuery };
+ }
+
await ensureInitialized();
const { searchEntries } = await getMemoryFunctions();
@@ -752,6 +792,11 @@ export const memoryTools: MCPTool[] = [
required: ['key'],
},
handler: async (input) => {
+ const missingKey = missingRequiredString(input, 'key', 'memory_delete');
+ if (missingKey) {
+ return { success: false, key: input.key, namespace: input.namespace, deleted: false, ...missingKey };
+ }
+
await ensureInitialized();
const { deleteEntry } = await getMemoryFunctions();
@@ -1234,6 +1279,11 @@ export const memoryTools: MCPTool[] = [
required: ['query'],
},
handler: async (input) => {
+ const missingQuery = missingRequiredString(input, 'query', 'memory_search_unified');
+ if (missingQuery) {
+ return { success: false, query: input.query, results: [], total: 0, ...missingQuery };
+ }
+
await ensureInitialized();
const { searchEntries, listEntries } = await getMemoryFunctions();
validateMemoryInput(undefined, undefined, input.query as string);