claude-flow 3.74.0 → 3.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/helpers/auto-memory-hook.mjs +262 -81
- package/node_modules/@claude-flow/codex/README.md +38 -0
- package/node_modules/@claude-flow/codex/dist/generators/agents-md.d.ts +1 -0
- package/node_modules/@claude-flow/codex/dist/generators/agents-md.d.ts.map +1 -1
- package/node_modules/@claude-flow/codex/dist/generators/agents-md.js +4 -4
- package/node_modules/@claude-flow/codex/dist/generators/agents-md.js.map +1 -1
- package/node_modules/@claude-flow/codex/dist/generators/dual-instructions.d.ts +20 -0
- package/node_modules/@claude-flow/codex/dist/generators/dual-instructions.d.ts.map +1 -0
- package/node_modules/@claude-flow/codex/dist/generators/dual-instructions.js +104 -0
- package/node_modules/@claude-flow/codex/dist/generators/dual-instructions.js.map +1 -0
- package/node_modules/@claude-flow/codex/dist/generators/index.d.ts +2 -0
- package/node_modules/@claude-flow/codex/dist/generators/index.d.ts.map +1 -1
- package/node_modules/@claude-flow/codex/dist/generators/index.js +2 -0
- package/node_modules/@claude-flow/codex/dist/generators/index.js.map +1 -1
- package/node_modules/@claude-flow/codex/dist/generators/shared-instructions.d.ts +46 -0
- package/node_modules/@claude-flow/codex/dist/generators/shared-instructions.d.ts.map +1 -0
- package/node_modules/@claude-flow/codex/dist/generators/shared-instructions.js +162 -0
- package/node_modules/@claude-flow/codex/dist/generators/shared-instructions.js.map +1 -0
- package/node_modules/@claude-flow/codex/dist/index.d.ts +3 -3
- package/node_modules/@claude-flow/codex/dist/index.js +1 -1
- package/node_modules/@claude-flow/codex/dist/initializer.d.ts.map +1 -1
- package/node_modules/@claude-flow/codex/dist/initializer.js +16 -93
- package/node_modules/@claude-flow/codex/dist/initializer.js.map +1 -1
- package/node_modules/@claude-flow/codex/package.json +8 -6
- package/node_modules/@claude-flow/mcp/README.md +44 -0
- package/node_modules/@claude-flow/mcp/package.json +16 -4
- package/package.json +4 -4
- package/v3/@claude-flow/cli/catalog-manifest.json +3 -3
- package/v3/@claude-flow/cli/dist/src/autopilot-state.js +17 -8
- package/v3/@claude-flow/cli/dist/src/commands/agent.js +2 -1
- package/v3/@claude-flow/cli/dist/src/commands/init.js +28 -6
- package/v3/@claude-flow/cli/dist/src/commands/memory-backup.js +2 -1
- package/v3/@claude-flow/cli/dist/src/commands/memory-distill.js +2 -1
- package/v3/@claude-flow/cli/dist/src/commands/memory.js +47 -3
- package/v3/@claude-flow/cli/dist/src/commands/neural.js +6 -5
- package/v3/@claude-flow/cli/dist/src/commands/plugins-host.d.ts +13 -0
- package/v3/@claude-flow/cli/dist/src/commands/plugins-host.js +161 -0
- package/v3/@claude-flow/cli/dist/src/commands/plugins.js +3 -1
- package/v3/@claude-flow/cli/dist/src/commands/start-all.d.ts +28 -0
- package/v3/@claude-flow/cli/dist/src/commands/start-all.js +99 -0
- package/v3/@claude-flow/cli/dist/src/commands/start.d.ts +5 -1
- package/v3/@claude-flow/cli/dist/src/commands/start.js +17 -10
- package/v3/@claude-flow/cli/dist/src/commands/swarm.js +2 -1
- package/v3/@claude-flow/cli/dist/src/fs-secure.js +9 -2
- package/v3/@claude-flow/cli/dist/src/init/claudemd-generator.d.ts +15 -0
- package/v3/@claude-flow/cli/dist/src/init/claudemd-generator.js +64 -114
- package/v3/@claude-flow/cli/dist/src/init/dual-instructions.d.ts +29 -0
- package/v3/@claude-flow/cli/dist/src/init/dual-instructions.js +87 -0
- package/v3/@claude-flow/cli/dist/src/init/executor.js +96 -13
- package/v3/@claude-flow/cli/dist/src/init/index.d.ts +3 -1
- package/v3/@claude-flow/cli/dist/src/init/index.js +5 -1
- package/v3/@claude-flow/cli/dist/src/init/plugin-dedup.d.ts +61 -0
- package/v3/@claude-flow/cli/dist/src/init/plugin-dedup.js +130 -0
- package/v3/@claude-flow/cli/dist/src/init/shared-instructions.d.ts +46 -0
- package/v3/@claude-flow/cli/dist/src/init/shared-instructions.js +162 -0
- package/v3/@claude-flow/cli/dist/src/init/types.d.ts +33 -0
- package/v3/@claude-flow/cli/dist/src/init/types.js +3 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agent-tools.d.ts +14 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agent-tools.js +48 -5
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-hierarchical-inventory-tools.d.ts +15 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-hierarchical-inventory-tools.js +115 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-inventory-tools.d.ts +21 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-inventory-tools.js +152 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-tools.js +56 -7
- package/v3/@claude-flow/cli/dist/src/mcp-tools/aidefence-known-misses.d.ts +6 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/aidefence-known-misses.js +23 -5
- package/v3/@claude-flow/cli/dist/src/mcp-tools/hooks-tools.js +21 -3
- package/v3/@claude-flow/cli/dist/src/mcp-tools/memory-tools.d.ts +29 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/memory-tools.js +239 -19
- package/v3/@claude-flow/cli/dist/src/mcp-tools/security-tools.js +53 -5
- package/v3/@claude-flow/cli/dist/src/memory/embedding-config.d.ts +79 -0
- package/v3/@claude-flow/cli/dist/src/memory/embedding-config.js +250 -0
- package/v3/@claude-flow/cli/dist/src/memory/ewc-consolidation.js +2 -1
- package/v3/@claude-flow/cli/dist/src/memory/intelligence.d.ts +28 -2
- package/v3/@claude-flow/cli/dist/src/memory/intelligence.js +37 -7
- package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.d.ts +42 -0
- package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +267 -28
- package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.d.ts +1 -1
- package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.js +47 -22
- package/v3/@claude-flow/cli/dist/src/ruvector/lora-adapter.js +3 -2
- package/v3/@claude-flow/cli/dist/src/ruvector/task-embedder.d.ts +2 -1
- package/v3/@claude-flow/cli/dist/src/ruvector/task-embedder.js +7 -3
- package/v3/@claude-flow/cli/dist/src/services/daemon-autostart.d.ts +4 -6
- package/v3/@claude-flow/cli/dist/src/services/daemon-autostart.js +6 -46
- package/v3/@claude-flow/cli/dist/src/services/host-executable.d.ts +68 -0
- package/v3/@claude-flow/cli/dist/src/services/host-executable.js +175 -0
- package/v3/@claude-flow/cli/dist/src/services/host-plugin-manager.d.ts +137 -0
- package/v3/@claude-flow/cli/dist/src/services/host-plugin-manager.js +374 -0
- package/v3/@claude-flow/cli/dist/src/services/project-root.d.ts +37 -0
- package/v3/@claude-flow/cli/dist/src/services/project-root.js +122 -0
- package/v3/@claude-flow/cli/package.json +7 -7
- package/.claude/.proven-config-version +0 -1
- package/.claude/proven-config.json +0 -42
|
@@ -97,6 +97,82 @@ function validateMemoryInput(key, value, query, namespace) {
|
|
|
97
97
|
throw new Error('Namespace contains disallowed characters');
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
+
// ===== #1227: AgentMemoryScope wiring =====
|
|
101
|
+
//
|
|
102
|
+
// @claude-flow/memory's agent-memory-scope.ts defines the three scopes
|
|
103
|
+
// ('project' | 'local' | 'user') and keys each scope by agent name, sanitized
|
|
104
|
+
// to [a-zA-Z0-9_-]. It has no namespace scheme — only a directory layout. In
|
|
105
|
+
// the CLI's single SQLite store a scope is therefore a NAMESPACE PREFIX:
|
|
106
|
+
//
|
|
107
|
+
// agent-memory:<scope>:<agentId>:<namespace>
|
|
108
|
+
//
|
|
109
|
+
// The agent id is required with a scope (the package's AgentScopedConfig
|
|
110
|
+
// requires agentName) and must already match the package's sanitizer class,
|
|
111
|
+
// so an accepted id names the same agent in both. It is rejected, not
|
|
112
|
+
// sanitized, so two different ids can never collapse into one partition.
|
|
113
|
+
// Without a scope nothing is prefixed: existing namespaces are untouched.
|
|
114
|
+
export const MEMORY_SCOPES = ['project', 'local', 'user'];
|
|
115
|
+
export const SCOPED_NAMESPACE_PREFIX = 'agent-memory';
|
|
116
|
+
/** The character class @claude-flow/memory's resolveAgentMemoryDir keeps. */
|
|
117
|
+
const SCOPE_AGENT_ID_RE = /^[a-zA-Z0-9_-]+$/;
|
|
118
|
+
/**
|
|
119
|
+
* Resolve the namespace a memory operation actually reads or writes.
|
|
120
|
+
* `scope`/`agentId` absent → `namespace` unchanged. Otherwise both are
|
|
121
|
+
* validated and the result is the prefixed form, itself run through the
|
|
122
|
+
* namespace validator so the stored namespace stays exportable and purgeable.
|
|
123
|
+
*/
|
|
124
|
+
export function resolveScopedNamespace(opts) {
|
|
125
|
+
const { scope, agentId, namespace } = opts;
|
|
126
|
+
const scopeGiven = scope !== undefined && scope !== null && scope !== '';
|
|
127
|
+
const agentGiven = agentId !== undefined && agentId !== null && agentId !== '';
|
|
128
|
+
if (!scopeGiven) {
|
|
129
|
+
if (agentGiven)
|
|
130
|
+
return { ok: false, error: 'agentId requires scope (project, local or user)' };
|
|
131
|
+
return { ok: true, namespace };
|
|
132
|
+
}
|
|
133
|
+
if (typeof scope !== 'string' || !MEMORY_SCOPES.includes(scope)) {
|
|
134
|
+
return { ok: false, error: `scope must be one of: ${MEMORY_SCOPES.join(', ')}` };
|
|
135
|
+
}
|
|
136
|
+
if (!agentGiven)
|
|
137
|
+
return { ok: false, error: 'scope requires agentId (the agent whose memory partition to use)' };
|
|
138
|
+
const vAgent = validateIdentifier(agentId, 'agentId');
|
|
139
|
+
if (!vAgent.valid)
|
|
140
|
+
return { ok: false, error: vAgent.error ?? 'agentId is invalid' };
|
|
141
|
+
if (!SCOPE_AGENT_ID_RE.test(agentId)) {
|
|
142
|
+
return { ok: false, error: 'agentId may contain only letters, digits, _ and - (the agent-memory directory name rule)' };
|
|
143
|
+
}
|
|
144
|
+
const effective = `${SCOPED_NAMESPACE_PREFIX}:${scope}:${agentId}:${namespace}`;
|
|
145
|
+
const vNs = validateIdentifier(effective, 'namespace');
|
|
146
|
+
if (!vNs.valid) {
|
|
147
|
+
return { ok: false, error: `${vNs.error} (scoped namespace "${effective}" includes the ${effective.length - namespace.length}-character scope prefix)` };
|
|
148
|
+
}
|
|
149
|
+
return { ok: true, namespace: effective, scope: scope, agentId: agentId };
|
|
150
|
+
}
|
|
151
|
+
/** Inverse of resolveScopedNamespace; null for an unscoped namespace. */
|
|
152
|
+
export function parseScopedNamespace(stored) {
|
|
153
|
+
const parts = stored.split(':');
|
|
154
|
+
if (parts.length < 4 || parts[0] !== SCOPED_NAMESPACE_PREFIX)
|
|
155
|
+
return null;
|
|
156
|
+
const [, scope, agentId] = parts;
|
|
157
|
+
if (!MEMORY_SCOPES.includes(scope) || !SCOPE_AGENT_ID_RE.test(agentId))
|
|
158
|
+
return null;
|
|
159
|
+
const namespace = parts.slice(3).join(':');
|
|
160
|
+
if (!namespace)
|
|
161
|
+
return null;
|
|
162
|
+
return { scope: scope, agentId, namespace };
|
|
163
|
+
}
|
|
164
|
+
/** Schema fragment shared by memory_store / memory_retrieve / memory_search. */
|
|
165
|
+
const SCOPE_SCHEMA_PROPERTIES = {
|
|
166
|
+
scope: {
|
|
167
|
+
type: 'string',
|
|
168
|
+
enum: [...MEMORY_SCOPES],
|
|
169
|
+
description: 'AgentMemoryScope (#1227): project (shared), local (this machine) or user (this user). Requires agentId. Partitions the store by prefixing the namespace as agent-memory:<scope>:<agentId>:<namespace>; omit for the unscoped namespace exactly as before.',
|
|
170
|
+
},
|
|
171
|
+
agentId: {
|
|
172
|
+
type: 'string',
|
|
173
|
+
description: 'Agent whose scoped memory to use; letters, digits, _ and - only. Required with scope, rejected without it.',
|
|
174
|
+
},
|
|
175
|
+
};
|
|
100
176
|
/**
|
|
101
177
|
* #3374 — presence check for a schema-`required` string parameter.
|
|
102
178
|
*
|
|
@@ -446,6 +522,7 @@ export const memoryTools = [
|
|
|
446
522
|
description: 'ADR-323: who/what produced this value, so shared-namespace retrieval can filter by trust level instead of conflating a user\'s stated claim with an agent\'s own output. Default: "unknown".',
|
|
447
523
|
},
|
|
448
524
|
dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
|
|
525
|
+
...SCOPE_SCHEMA_PROPERTIES,
|
|
449
526
|
},
|
|
450
527
|
required: ['key', 'value'],
|
|
451
528
|
},
|
|
@@ -459,7 +536,13 @@ export const memoryTools = [
|
|
|
459
536
|
await ensureInitialized();
|
|
460
537
|
const { storeEntry } = await getMemoryFunctions();
|
|
461
538
|
const key = input.key;
|
|
462
|
-
|
|
539
|
+
// #1227: with scope + agentId the write lands in the scoped namespace.
|
|
540
|
+
const scoped = resolveScopedNamespace({ scope: input.scope, agentId: input.agentId, namespace: input.namespace || 'default' });
|
|
541
|
+
if (!scoped.ok) {
|
|
542
|
+
return { success: false, key, stored: false, hasEmbedding: false, error: scoped.error };
|
|
543
|
+
}
|
|
544
|
+
const namespace = scoped.namespace;
|
|
545
|
+
const scopeInfo = scoped.scope ? { scope: scoped.scope, agentId: scoped.agentId } : {};
|
|
463
546
|
const rawValue = input.value;
|
|
464
547
|
const value = typeof rawValue === 'string' ? rawValue : (rawValue !== undefined ? JSON.stringify(rawValue) : '');
|
|
465
548
|
const tags = input.tags || [];
|
|
@@ -515,6 +598,7 @@ export const memoryTools = [
|
|
|
515
598
|
success: result.success,
|
|
516
599
|
key,
|
|
517
600
|
namespace,
|
|
601
|
+
...scopeInfo,
|
|
518
602
|
stored: result.success,
|
|
519
603
|
storedAt: new Date().toISOString(),
|
|
520
604
|
hasEmbedding: !!result.embedding,
|
|
@@ -546,6 +630,7 @@ export const memoryTools = [
|
|
|
546
630
|
key: { type: 'string', description: 'Memory key' },
|
|
547
631
|
namespace: { type: 'string', description: 'Namespace (default: "default")' },
|
|
548
632
|
dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
|
|
633
|
+
...SCOPE_SCHEMA_PROPERTIES,
|
|
549
634
|
},
|
|
550
635
|
required: ['key'],
|
|
551
636
|
},
|
|
@@ -559,7 +644,13 @@ export const memoryTools = [
|
|
|
559
644
|
await ensureInitialized();
|
|
560
645
|
const { getEntry } = await getMemoryFunctions();
|
|
561
646
|
const key = input.key;
|
|
562
|
-
|
|
647
|
+
// #1227: a scoped read only ever sees its own scope's namespace.
|
|
648
|
+
const scoped = resolveScopedNamespace({ scope: input.scope, agentId: input.agentId, namespace: input.namespace || 'default' });
|
|
649
|
+
if (!scoped.ok) {
|
|
650
|
+
return { key, namespace: input.namespace, value: null, found: false, error: scoped.error };
|
|
651
|
+
}
|
|
652
|
+
const namespace = scoped.namespace;
|
|
653
|
+
const scopeInfo = scoped.scope ? { scope: scoped.scope, agentId: scoped.agentId } : {};
|
|
563
654
|
validateMemoryInput(key, undefined, undefined, namespace);
|
|
564
655
|
try {
|
|
565
656
|
const result = await getEntry({ key, namespace, dbPath });
|
|
@@ -575,6 +666,7 @@ export const memoryTools = [
|
|
|
575
666
|
return {
|
|
576
667
|
key,
|
|
577
668
|
namespace,
|
|
669
|
+
...scopeInfo,
|
|
578
670
|
value,
|
|
579
671
|
tags: result.entry.tags,
|
|
580
672
|
storedAt: result.entry.createdAt,
|
|
@@ -588,6 +680,7 @@ export const memoryTools = [
|
|
|
588
680
|
return {
|
|
589
681
|
key,
|
|
590
682
|
namespace,
|
|
683
|
+
...scopeInfo,
|
|
591
684
|
value: null,
|
|
592
685
|
found: false,
|
|
593
686
|
};
|
|
@@ -621,6 +714,11 @@ export const memoryTools = [
|
|
|
621
714
|
description: 'ADR-323: restrict results to these provenance types (e.g. exclude user_claim when fact-checking). Omit for no filtering. Enforced for standard and SmartRetrieval searches.',
|
|
622
715
|
},
|
|
623
716
|
dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
|
|
717
|
+
scope: {
|
|
718
|
+
...SCOPE_SCHEMA_PROPERTIES.scope,
|
|
719
|
+
description: SCOPE_SCHEMA_PROPERTIES.scope.description + ' With scope, an omitted namespace means "default" inside that scope (not every namespace), and only that scope is searched. Without scope, an omitted namespace still searches every namespace, scoped ones included.',
|
|
720
|
+
},
|
|
721
|
+
agentId: SCOPE_SCHEMA_PROPERTIES.agentId,
|
|
624
722
|
},
|
|
625
723
|
required: ['query'],
|
|
626
724
|
},
|
|
@@ -643,7 +741,21 @@ export const memoryTools = [
|
|
|
643
741
|
// the search to a namespace that is usually empty. Leave namespace
|
|
644
742
|
// undefined when not provided so the underlying `|| 'all'` fallback
|
|
645
743
|
// in the search layer actually fires.
|
|
646
|
-
|
|
744
|
+
//
|
|
745
|
+
// #1227: with a scope the namespace is resolved (an omitted one becomes
|
|
746
|
+
// that scope's "default"); without one it stays undefined → 'all'.
|
|
747
|
+
let namespace = input.namespace;
|
|
748
|
+
let scopeInfo = {};
|
|
749
|
+
if (input.scope !== undefined || input.agentId !== undefined) {
|
|
750
|
+
const scoped = resolveScopedNamespace({ scope: input.scope, agentId: input.agentId, namespace: namespace || 'default' });
|
|
751
|
+
if (!scoped.ok) {
|
|
752
|
+
return { query, results: [], total: 0, error: scoped.error };
|
|
753
|
+
}
|
|
754
|
+
if (scoped.scope) {
|
|
755
|
+
namespace = scoped.namespace;
|
|
756
|
+
scopeInfo = { scope: scoped.scope, agentId: scoped.agentId };
|
|
757
|
+
}
|
|
758
|
+
}
|
|
647
759
|
const limit = input.limit ?? 10;
|
|
648
760
|
const threshold = input.threshold ?? DEFAULT_SEARCH_THRESHOLD;
|
|
649
761
|
const thresholdWasDefault = input.threshold === undefined || input.threshold === null;
|
|
@@ -730,6 +842,8 @@ export const memoryTools = [
|
|
|
730
842
|
results,
|
|
731
843
|
total: results.length,
|
|
732
844
|
...(smartNearMisses ?? {}),
|
|
845
|
+
...scopeInfo,
|
|
846
|
+
...(scopeInfo.scope ? { namespace } : {}),
|
|
733
847
|
searchTime: `${duration.toFixed(2)}ms`,
|
|
734
848
|
backend: 'SmartRetrieval (RRF + MMR + Recency)',
|
|
735
849
|
stats: smartResult.stats,
|
|
@@ -782,6 +896,8 @@ export const memoryTools = [
|
|
|
782
896
|
results,
|
|
783
897
|
total: results.length,
|
|
784
898
|
...(nearMissInfo ?? {}),
|
|
899
|
+
...scopeInfo,
|
|
900
|
+
...(scopeInfo.scope ? { namespace } : {}),
|
|
785
901
|
searchTime: `${duration.toFixed(2)}ms`,
|
|
786
902
|
backend: 'HNSW + sql.js',
|
|
787
903
|
...(smartFallbackReason ? { smartFallback: smartFallbackReason } : {}),
|
|
@@ -1417,6 +1533,7 @@ export const memoryTools = [
|
|
|
1417
1533
|
if (missingQuery) {
|
|
1418
1534
|
return { success: false, query: input.query, results: [], total: 0, ...missingQuery };
|
|
1419
1535
|
}
|
|
1536
|
+
const startTime = performance.now();
|
|
1420
1537
|
await ensureInitialized();
|
|
1421
1538
|
const { searchEntries, listEntries } = await getMemoryFunctions();
|
|
1422
1539
|
validateMemoryInput(undefined, undefined, input.query);
|
|
@@ -1476,24 +1593,52 @@ export const memoryTools = [
|
|
|
1476
1593
|
namespaceSource = 'legacy-fallback';
|
|
1477
1594
|
}
|
|
1478
1595
|
}
|
|
1479
|
-
|
|
1480
|
-
|
|
1596
|
+
// #4876: namespaces are searched concurrently (bounded) instead of one
|
|
1597
|
+
// after another. Each search is a read of the same store and keeps no
|
|
1598
|
+
// shared mutable state, so they are independent. Results land in a slot
|
|
1599
|
+
// per namespace and are flattened in namespace order, so the merge below
|
|
1600
|
+
// sees exactly the sequence the sequential loop produced. Failures are
|
|
1601
|
+
// collected instead of swallowed, so success:true no longer hides
|
|
1602
|
+
// namespaces that were never searched.
|
|
1603
|
+
const NAMESPACE_CONCURRENCY = 4;
|
|
1604
|
+
const perNamespace = namespaces.map(() => []);
|
|
1605
|
+
const namespaceErrors = [];
|
|
1606
|
+
const failedAt = namespaces.map(() => undefined);
|
|
1607
|
+
const searchOne = async (index) => {
|
|
1608
|
+
const searchNs = namespaces[index];
|
|
1481
1609
|
try {
|
|
1482
1610
|
const r = await searchEntries({ query, namespace: searchNs, limit: limit * 2 });
|
|
1483
|
-
if (r
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1611
|
+
if (r && r.success === false && r.error) {
|
|
1612
|
+
failedAt[index] = String(r.error);
|
|
1613
|
+
return;
|
|
1614
|
+
}
|
|
1615
|
+
for (const entry of r?.results ?? []) {
|
|
1616
|
+
perNamespace[index].push({
|
|
1617
|
+
key: entry.key || entry.id || '',
|
|
1618
|
+
content: (entry.content || entry.value || '').toString().slice(0, 200),
|
|
1619
|
+
score: entry.score || 0,
|
|
1620
|
+
namespace: searchNs,
|
|
1621
|
+
source: searchNs === 'claude-memories' ? 'claude-code' : searchNs === 'auto-memory' ? 'auto-memory' : 'agentdb',
|
|
1622
|
+
});
|
|
1493
1623
|
}
|
|
1494
1624
|
}
|
|
1495
|
-
catch {
|
|
1496
|
-
|
|
1625
|
+
catch (error) {
|
|
1626
|
+
failedAt[index] = error instanceof Error ? error.message : String(error);
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
let nextIndex = 0;
|
|
1630
|
+
const worker = async () => {
|
|
1631
|
+
while (nextIndex < namespaces.length) {
|
|
1632
|
+
const index = nextIndex++;
|
|
1633
|
+
await searchOne(index);
|
|
1634
|
+
}
|
|
1635
|
+
};
|
|
1636
|
+
await Promise.all(Array.from({ length: Math.min(NAMESPACE_CONCURRENCY, namespaces.length) }, () => worker()));
|
|
1637
|
+
failedAt.forEach((error, index) => {
|
|
1638
|
+
if (error !== undefined)
|
|
1639
|
+
namespaceErrors.push({ namespace: namespaces[index], error });
|
|
1640
|
+
});
|
|
1641
|
+
const allResults = perNamespace.flat();
|
|
1497
1642
|
// Sort by score, deduplicate by (namespace, key), take top N. An entry is
|
|
1498
1643
|
// identified by both (see #2271), so the same key in another namespace is
|
|
1499
1644
|
// a different entry and must not be dropped as a duplicate.
|
|
@@ -1506,14 +1651,22 @@ export const memoryTools = [
|
|
|
1506
1651
|
seen.add(id);
|
|
1507
1652
|
return true;
|
|
1508
1653
|
}).slice(0, limit);
|
|
1654
|
+
const duration = performance.now() - startTime;
|
|
1655
|
+
const namespacesFailed = namespaceErrors.length;
|
|
1509
1656
|
return {
|
|
1510
|
-
|
|
1657
|
+
// Partial coverage still succeeds; only a search where every
|
|
1658
|
+
// namespace failed is a failure (#4876).
|
|
1659
|
+
success: namespaces.length === 0 || namespacesFailed < namespaces.length,
|
|
1511
1660
|
query,
|
|
1512
1661
|
results: deduplicated,
|
|
1513
1662
|
total: deduplicated.length,
|
|
1514
1663
|
searchedNamespaces: namespaces,
|
|
1515
1664
|
namespaceSource, // #2246 — surface how the namespace list was resolved
|
|
1516
|
-
|
|
1665
|
+
namespacesSearched: namespaces.length,
|
|
1666
|
+
namespacesFailed,
|
|
1667
|
+
namespaceErrors,
|
|
1668
|
+
// #4876: elapsed time, not a timestamp.
|
|
1669
|
+
searchTime: `${duration.toFixed(2)}ms`,
|
|
1517
1670
|
};
|
|
1518
1671
|
},
|
|
1519
1672
|
},
|
|
@@ -1886,5 +2039,72 @@ export const memoryTools = [
|
|
|
1886
2039
|
};
|
|
1887
2040
|
},
|
|
1888
2041
|
},
|
|
2042
|
+
{
|
|
2043
|
+
name: 'memory_list_scopes',
|
|
2044
|
+
description: 'List the AgentMemoryScope partitions (project, local, user) in the memory store, with each agent and the namespaces and entry counts under it (#1227). Use when you need to know which scope/agentId pairs already hold memory before calling memory_store, memory_retrieve or memory_search with scope; native Glob is wrong here because scopes are namespace prefixes inside .swarm/memory.db, not directories. Read-only. Pair with memory_list to see the entries themselves.',
|
|
2045
|
+
category: 'memory',
|
|
2046
|
+
inputSchema: {
|
|
2047
|
+
type: 'object',
|
|
2048
|
+
properties: {
|
|
2049
|
+
dbPath: { type: 'string', description: 'Explicit SQLite store to read; omit to use the MCP default (the native/AgentDB sibling).' },
|
|
2050
|
+
},
|
|
2051
|
+
},
|
|
2052
|
+
handler: async (input, context) => {
|
|
2053
|
+
const dbPath = resolveDbPathOption(input.dbPath, context);
|
|
2054
|
+
if (!dbPath)
|
|
2055
|
+
await ensureInitialized();
|
|
2056
|
+
const { listEntries } = await getMemoryFunctions();
|
|
2057
|
+
let listing;
|
|
2058
|
+
try {
|
|
2059
|
+
listing = await collectAllEntries(listEntries, dbPath);
|
|
2060
|
+
}
|
|
2061
|
+
catch (error) {
|
|
2062
|
+
return { success: false, scopes: [], error: error instanceof Error ? error.message : 'Unknown error' };
|
|
2063
|
+
}
|
|
2064
|
+
// A failed listing is not an empty store (#4622).
|
|
2065
|
+
if (!listing.success) {
|
|
2066
|
+
return { success: false, scopes: [], error: listing.error || 'listEntries reported failure' };
|
|
2067
|
+
}
|
|
2068
|
+
// scope → agentId → namespace → entry count. Maps, not object literals,
|
|
2069
|
+
// so an agent or namespace named `__proto__` is counted, not assigned.
|
|
2070
|
+
const buckets = new Map(MEMORY_SCOPES.map((s) => [s, new Map()]));
|
|
2071
|
+
let unscopedEntries = 0;
|
|
2072
|
+
for (const entry of listing.entries) {
|
|
2073
|
+
const parsed = parseScopedNamespace(String(entry.namespace ?? ''));
|
|
2074
|
+
if (!parsed) {
|
|
2075
|
+
unscopedEntries++;
|
|
2076
|
+
continue;
|
|
2077
|
+
}
|
|
2078
|
+
const agents = buckets.get(parsed.scope);
|
|
2079
|
+
if (!agents.has(parsed.agentId))
|
|
2080
|
+
agents.set(parsed.agentId, new Map());
|
|
2081
|
+
const namespaces = agents.get(parsed.agentId);
|
|
2082
|
+
namespaces.set(parsed.namespace, (namespaces.get(parsed.namespace) ?? 0) + 1);
|
|
2083
|
+
}
|
|
2084
|
+
const scopes = MEMORY_SCOPES.map((scope) => {
|
|
2085
|
+
const agents = [...buckets.get(scope).entries()]
|
|
2086
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
2087
|
+
.map(([agentId, namespaces]) => ({
|
|
2088
|
+
agentId,
|
|
2089
|
+
namespaces: [...namespaces.entries()]
|
|
2090
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
2091
|
+
.map(([namespace, entries]) => ({
|
|
2092
|
+
namespace,
|
|
2093
|
+
storedNamespace: `${SCOPED_NAMESPACE_PREFIX}:${scope}:${agentId}:${namespace}`,
|
|
2094
|
+
entries,
|
|
2095
|
+
})),
|
|
2096
|
+
}));
|
|
2097
|
+
const entries = agents.reduce((n, a) => n + a.namespaces.reduce((m, ns) => m + ns.entries, 0), 0);
|
|
2098
|
+
return { scope, agents, entries };
|
|
2099
|
+
});
|
|
2100
|
+
return {
|
|
2101
|
+
success: true,
|
|
2102
|
+
prefix: `${SCOPED_NAMESPACE_PREFIX}:<scope>:<agentId>:<namespace>`,
|
|
2103
|
+
scopes,
|
|
2104
|
+
unscopedEntries,
|
|
2105
|
+
totalEntries: listing.total,
|
|
2106
|
+
};
|
|
2107
|
+
},
|
|
2108
|
+
},
|
|
1889
2109
|
];
|
|
1890
2110
|
//# sourceMappingURL=memory-tools.js.map
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import { validateText, validateIdentifier } from './validate-input.js';
|
|
13
13
|
import { autoInstallPackage } from './auto-install.js';
|
|
14
14
|
import { createRequire } from 'module';
|
|
15
|
-
import { findKnownMiss, recordKnownMiss } from './aidefence-known-misses.js';
|
|
15
|
+
import { findKnownMiss, recordKnownMiss, retractKnownMiss } from './aidefence-known-misses.js';
|
|
16
16
|
// Create require for resolving module paths
|
|
17
17
|
const require = createRequire(import.meta.url);
|
|
18
18
|
// Lazy-loaded AIDefence instance
|
|
@@ -159,7 +159,8 @@ const aidefenceScanTool = {
|
|
|
159
159
|
properties: {
|
|
160
160
|
input: {
|
|
161
161
|
type: 'string',
|
|
162
|
-
|
|
162
|
+
maxLength: 10000,
|
|
163
|
+
description: 'Text to scan for threats. Max 10000 UTF-16 code units (JavaScript string length); longer input is refused. JSON Schema maxLength counts code points, so text with astral characters (emoji, some CJK) can pass this schema and still exceed the limit — each such character counts as 2.',
|
|
163
164
|
},
|
|
164
165
|
quick: {
|
|
165
166
|
type: 'boolean',
|
|
@@ -378,12 +379,14 @@ const aidefenceStatsTool = {
|
|
|
378
379
|
}
|
|
379
380
|
},
|
|
380
381
|
};
|
|
382
|
+
// A threatType that names PII rather than a threat (#4862).
|
|
383
|
+
const PII_THREAT_TYPE = /pii|personal/i;
|
|
381
384
|
/**
|
|
382
385
|
* Record detection feedback for learning
|
|
383
386
|
*/
|
|
384
387
|
const aidefenceLearnTool = {
|
|
385
388
|
name: 'aidefence_learn',
|
|
386
|
-
description: 'Record detection feedback for pattern learning. Improves future detection accuracy. Use when nothing native exists — Claude Code does not have a PII / prompt-injection / adversarial-text scanner. Pair with any tool that ingests untrusted input (browser scrape, federation envelope, memory_import_claude).',
|
|
389
|
+
description: 'Record detection feedback for pattern learning. Improves future detection accuracy. Feedback on PII detection is not threat feedback and is not recorded as a missed threat; pass retract=true to remove a wrongly recorded miss. Use when nothing native exists — Claude Code does not have a PII / prompt-injection / adversarial-text scanner. Pair with any tool that ingests untrusted input (browser scrape, federation envelope, memory_import_claude).',
|
|
387
390
|
inputSchema: {
|
|
388
391
|
type: 'object',
|
|
389
392
|
properties: {
|
|
@@ -393,7 +396,7 @@ const aidefenceLearnTool = {
|
|
|
393
396
|
},
|
|
394
397
|
wasAccurate: {
|
|
395
398
|
type: 'boolean',
|
|
396
|
-
description: 'Whether the detection was accurate',
|
|
399
|
+
description: 'Whether the THREAT detection (prompt injection, jailbreak, etc.) was accurate. Covers threats only, not PII detection: false with no threat detected records the input as a missed threat. Ignored when retract=true.',
|
|
397
400
|
},
|
|
398
401
|
verdict: {
|
|
399
402
|
type: 'string',
|
|
@@ -412,6 +415,10 @@ const aidefenceLearnTool = {
|
|
|
412
415
|
type: 'boolean',
|
|
413
416
|
description: 'Whether the mitigation was successful',
|
|
414
417
|
},
|
|
418
|
+
retract: {
|
|
419
|
+
type: 'boolean',
|
|
420
|
+
description: 'Remove the known-miss entry recorded for this exact input and threatType (default prompt_injection) instead of learning. Other entries are kept.',
|
|
421
|
+
},
|
|
415
422
|
},
|
|
416
423
|
required: ['input', 'wasAccurate'],
|
|
417
424
|
},
|
|
@@ -437,14 +444,55 @@ const aidefenceLearnTool = {
|
|
|
437
444
|
const threatType = args.threatType;
|
|
438
445
|
const mitigationStrategy = args.mitigationStrategy;
|
|
439
446
|
const mitigationSuccess = args.mitigationSuccess;
|
|
447
|
+
const missType = threatType || 'prompt_injection';
|
|
440
448
|
try {
|
|
449
|
+
// Retract (#4862): drop exactly the matching hash+type entry. Only the
|
|
450
|
+
// registry affects verdicts, so no detector is needed for this.
|
|
451
|
+
if (args.retract === true) {
|
|
452
|
+
const retracted = retractKnownMiss(input, missType);
|
|
453
|
+
return {
|
|
454
|
+
content: [{
|
|
455
|
+
type: 'text',
|
|
456
|
+
text: JSON.stringify({
|
|
457
|
+
success: true,
|
|
458
|
+
retracted,
|
|
459
|
+
threatType: missType,
|
|
460
|
+
message: retracted
|
|
461
|
+
? `Known-miss entry for this input (threatType ${missType}) removed; aidefence_scan no longer flags it from the registry.`
|
|
462
|
+
: `No known-miss entry for this input with threatType ${missType}; nothing was changed.`,
|
|
463
|
+
}, null, 2),
|
|
464
|
+
}],
|
|
465
|
+
};
|
|
466
|
+
}
|
|
441
467
|
const defender = await getAIDefence();
|
|
442
468
|
// Re-detect to get result for learning
|
|
443
469
|
const result = await defender.detect(input);
|
|
470
|
+
// #4862: wasAccurate covers threat detection only. When the scan found
|
|
471
|
+
// PII and the feedback is about PII (no threatType, or one naming PII),
|
|
472
|
+
// it says nothing about a missed threat, so nothing is recorded.
|
|
473
|
+
const piiFeedback = !wasAccurate && result.threats.length === 0 && result.piiFound === true
|
|
474
|
+
&& (!threatType || PII_THREAT_TYPE.test(threatType));
|
|
475
|
+
if (piiFeedback) {
|
|
476
|
+
return {
|
|
477
|
+
content: [{
|
|
478
|
+
type: 'text',
|
|
479
|
+
text: JSON.stringify({
|
|
480
|
+
success: true,
|
|
481
|
+
message: 'PII feedback is not threat feedback: the scan found PII and no threat, so nothing was recorded as a missed threat. To report a missed threat in this input, pass its threatType (e.g. prompt_injection).',
|
|
482
|
+
learned: 0,
|
|
483
|
+
learnedFrom: {
|
|
484
|
+
input: input.slice(0, 50) + (input.length > 50 ? '...' : ''),
|
|
485
|
+
wasAccurate,
|
|
486
|
+
threatCount: 0,
|
|
487
|
+
piiFound: true,
|
|
488
|
+
},
|
|
489
|
+
}, null, 2),
|
|
490
|
+
}],
|
|
491
|
+
};
|
|
492
|
+
}
|
|
444
493
|
// A false negative (wasAccurate=false, nothing detected) gives
|
|
445
494
|
// learnFromDetection() no threat to store, so record the miss itself.
|
|
446
495
|
const falseNegative = !wasAccurate && result.threats.length === 0;
|
|
447
|
-
const missType = threatType || 'prompt_injection';
|
|
448
496
|
let learned = result.threats.length;
|
|
449
497
|
let newlyRecorded = false;
|
|
450
498
|
if (falseNegative) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* embedding-config.ts — one place that decides which embedding model the
|
|
3
|
+
* memory path loads and what dimension its vectors have (#2305).
|
|
4
|
+
*
|
|
5
|
+
* `embeddings init` (commands/embeddings.ts) and the `embeddings_init` MCP
|
|
6
|
+
* tool write `<project>/.claude-flow/embeddings.json` with `{ model,
|
|
7
|
+
* dimension, ... }`. Before this module only those two files read it: the
|
|
8
|
+
* memory path, the AgentDB bridge and the router's task embedder all loaded
|
|
9
|
+
* 'Xenova/all-MiniLM-L6-v2' and assumed 384 dimensions regardless of what the
|
|
10
|
+
* user configured.
|
|
11
|
+
*
|
|
12
|
+
* Precedence: `.claude-flow/embeddings.json` → defaults (MiniLM / 384). There
|
|
13
|
+
* is no embedding-model environment override in this codebase today, so none
|
|
14
|
+
* is invented here.
|
|
15
|
+
*
|
|
16
|
+
* Safety: a store that already holds vectors keeps its recorded dimension.
|
|
17
|
+
* When the configured dimension differs, `resolveEffectiveEmbeddingConfig()`
|
|
18
|
+
* returns the recorded shape and warns once — it never mixes dimensions in one
|
|
19
|
+
* store and never rebuilds or rewrites existing vectors.
|
|
20
|
+
*/
|
|
21
|
+
export declare const DEFAULT_EMBEDDING_MODEL = "Xenova/all-MiniLM-L6-v2";
|
|
22
|
+
export declare const DEFAULT_EMBEDDING_DIMENSION = 384;
|
|
23
|
+
/** Same directory + file name commands/embeddings.ts and embeddings-tools.ts write. */
|
|
24
|
+
export declare const EMBEDDINGS_CONFIG_DIR = ".claude-flow";
|
|
25
|
+
export declare const EMBEDDINGS_CONFIG_FILE = "embeddings.json";
|
|
26
|
+
export interface EmbeddingConfig {
|
|
27
|
+
/** Model id passed to the transformers `pipeline()` (always namespaced, e.g. `Xenova/...`). */
|
|
28
|
+
model: string;
|
|
29
|
+
dimension: number;
|
|
30
|
+
/** `config` = read from embeddings.json; `recorded` = kept from an existing store; `default` otherwise. */
|
|
31
|
+
source: 'default' | 'config' | 'recorded';
|
|
32
|
+
configPath: string;
|
|
33
|
+
}
|
|
34
|
+
/** Shape of the vectors an existing store already holds. */
|
|
35
|
+
export interface RecordedEmbeddingShape {
|
|
36
|
+
dimension: number;
|
|
37
|
+
model?: string | null;
|
|
38
|
+
}
|
|
39
|
+
export type RecordedShapeReader = (dbPath: string) => Promise<RecordedEmbeddingShape | null>;
|
|
40
|
+
/**
|
|
41
|
+
* `embeddings init` records the bare name it was given (default
|
|
42
|
+
* `all-MiniLM-L6-v2`), while the MCP tool records `Xenova/...`. The
|
|
43
|
+
* transformers pipeline needs the namespaced id, so bare names get `Xenova/`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function normalizeEmbeddingModel(model: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve the configured embedding model + dimension for a project root
|
|
48
|
+
* (default: cwd, which is where `embeddings init` writes its config).
|
|
49
|
+
* Cached per root; never throws.
|
|
50
|
+
*/
|
|
51
|
+
export declare function resolveEmbeddingConfig(root?: string): EmbeddingConfig;
|
|
52
|
+
/**
|
|
53
|
+
* Keep an existing store's recorded dimension when it differs from the
|
|
54
|
+
* configured one. Warns once per store; returns the configured value
|
|
55
|
+
* untouched when there is nothing recorded or the dimensions agree.
|
|
56
|
+
*/
|
|
57
|
+
export declare function reconcileWithRecordedShape(configured: EmbeddingConfig, recorded: RecordedEmbeddingShape | null, storeLabel: string): EmbeddingConfig;
|
|
58
|
+
/**
|
|
59
|
+
* Default reader: the most recently written real (non-hash) vector in
|
|
60
|
+
* `memory_entries`. A store with no such vector records nothing, so a fresh
|
|
61
|
+
* or empty database follows the configuration.
|
|
62
|
+
*/
|
|
63
|
+
export declare const readRecordedEmbeddingShape: RecordedShapeReader;
|
|
64
|
+
/**
|
|
65
|
+
* The embedding config to actually use: the configured model, unless one of
|
|
66
|
+
* the stores in `dbPaths` already holds vectors of another dimension. The
|
|
67
|
+
* memory path writes the same vectors to memory.db and (through the AgentDB
|
|
68
|
+
* bridge) agentdb-memory.db, so callers pass both and every caller converges
|
|
69
|
+
* on one answer: the first store with recorded vectors wins. Cached per
|
|
70
|
+
* (root, dbPaths).
|
|
71
|
+
*/
|
|
72
|
+
export declare function resolveEffectiveEmbeddingConfig(options?: {
|
|
73
|
+
root?: string;
|
|
74
|
+
dbPaths?: ReadonlyArray<string | undefined>;
|
|
75
|
+
readRecordedShape?: RecordedShapeReader;
|
|
76
|
+
}): Promise<EmbeddingConfig>;
|
|
77
|
+
/** Test seam: forget cached resolutions and emitted warnings. */
|
|
78
|
+
export declare function _resetEmbeddingConfigCache(): void;
|
|
79
|
+
//# sourceMappingURL=embedding-config.d.ts.map
|