/** * Federated Memory System * 6-namespace persistent memory for swarm agents */ class FederatedMemory { constructor(swarm) { this.swarm = swarm; this.namespaces = new Map(); this.defaultNamespaces = [ 'coordination', 'project-context', 'patterns', 'knowledge', 'session', 'metrics' ]; this.initialize(); } initialize() { for (const ns of this.defaultNamespaces) { this.namespaces.set(ns, new Map()); } this.swarm.log('success', `Federated memory initialized: ${this.defaultNamespaces.length} namespaces`); } store(namespace, key, value) { if (!this.namespaces.has(namespace)) { this.namespaces.set(namespace, new Map()); } this.namespaces.get(namespace).set(key, { value, timestamp: Date.now(), version: 1 }); } get(namespace, key) { const ns = this.namespaces.get(namespace); return ns ? ns.get(key) : null; } query(namespace, pattern) { const ns = this.namespaces.get(namespace); if (!ns) return []; const results = []; for (const [key, entry] of ns.entries()) { if (key.includes(pattern)) { results.push({ key, ...entry }); } } return results; } delete(namespace, key) { const ns = this.namespaces.get(namespace); if (ns) ns.delete(key); } clear(namespace) { if (namespace) { this.namespaces.get(namespace)?.clear(); } else { for (const ns of this.namespaces.values()) ns.clear(); } } stats() { const stats = {}; for (const [name, ns] of this.namespaces.entries()) { stats[name] = ns.size; } return stats; } exportAll() { const data = {}; for (const [name, ns] of this.namespaces.entries()) { data[name] = Object.fromEntries(ns); } return data; } importData(data) { for (const [name, entries] of Object.entries(data)) { if (!this.namespaces.has(name)) { this.namespaces.set(name, new Map()); } for (const [key, value] of Object.entries(entries)) { this.namespaces.get(name).set(key, value); } } this.swarm.log('success', 'Memory data imported'); } } module.exports = FederatedMemory;