agent-working-memory 0.5.6 → 0.6.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/README.md +73 -44
- package/dist/api/routes.d.ts.map +1 -1
- package/dist/api/routes.js +40 -1
- package/dist/api/routes.js.map +1 -1
- package/dist/cli.js +401 -1
- package/dist/cli.js.map +1 -1
- package/dist/coordination/mcp-tools.d.ts.map +1 -1
- package/dist/coordination/mcp-tools.js +10 -5
- package/dist/coordination/mcp-tools.js.map +1 -1
- package/dist/coordination/routes.d.ts.map +1 -1
- package/dist/coordination/routes.js +155 -16
- package/dist/coordination/routes.js.map +1 -1
- package/dist/coordination/schema.d.ts.map +1 -1
- package/dist/coordination/schema.js +35 -1
- package/dist/coordination/schema.js.map +1 -1
- package/dist/coordination/schemas.d.ts +21 -2
- package/dist/coordination/schemas.d.ts.map +1 -1
- package/dist/coordination/schemas.js +16 -0
- package/dist/coordination/schemas.js.map +1 -1
- package/dist/coordination/stale.d.ts +2 -0
- package/dist/coordination/stale.d.ts.map +1 -1
- package/dist/coordination/stale.js +5 -0
- package/dist/coordination/stale.js.map +1 -1
- package/dist/engine/activation.d.ts.map +1 -1
- package/dist/engine/activation.js +119 -23
- package/dist/engine/activation.js.map +1 -1
- package/dist/engine/consolidation.d.ts.map +1 -1
- package/dist/engine/consolidation.js +27 -6
- package/dist/engine/consolidation.js.map +1 -1
- package/dist/index.js +81 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +61 -3
- package/dist/mcp.js.map +1 -1
- package/dist/storage/sqlite.d.ts +18 -0
- package/dist/storage/sqlite.d.ts.map +1 -1
- package/dist/storage/sqlite.js +50 -5
- package/dist/storage/sqlite.js.map +1 -1
- package/dist/types/engram.d.ts +24 -0
- package/dist/types/engram.d.ts.map +1 -1
- package/dist/types/engram.js.map +1 -1
- package/package.json +3 -1
- package/src/api/routes.ts +50 -1
- package/src/cli.ts +454 -1
- package/src/coordination/mcp-tools.ts +10 -5
- package/src/coordination/routes.ts +209 -19
- package/src/coordination/schema.ts +27 -1
- package/src/coordination/schemas.ts +19 -0
- package/src/coordination/stale.ts +8 -0
- package/src/engine/activation.ts +125 -23
- package/src/engine/consolidation.ts +29 -6
- package/src/index.ts +74 -3
- package/src/mcp.ts +72 -3
- package/src/storage/sqlite.ts +54 -5
- package/src/types/engram.ts +28 -0
package/src/cli.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
16
16
|
import { resolve, basename, join, dirname } from 'node:path';
|
|
17
17
|
import { execSync } from 'node:child_process';
|
|
18
|
-
import { randomBytes } from 'node:crypto';
|
|
18
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
19
19
|
import { fileURLToPath } from 'node:url';
|
|
20
20
|
import { homedir as osHomedir } from 'node:os';
|
|
21
21
|
|
|
@@ -50,6 +50,13 @@ Usage:
|
|
|
50
50
|
awm mcp Start MCP server (used by Claude Code)
|
|
51
51
|
awm serve [--port <port>] Start HTTP API server
|
|
52
52
|
awm health [--port <port>] Check server health
|
|
53
|
+
awm export --db <path> [--agent <id>] [--output <file>] [--active-only]
|
|
54
|
+
Export memories to JSON
|
|
55
|
+
awm import <file> --db <path> [--remap-agent <id>] [--dedupe] [--dry-run]
|
|
56
|
+
Import memories from JSON
|
|
57
|
+
awm merge --target <db> --source <db> [--source ...]
|
|
58
|
+
[--remap uuid=name] [--remap-all-uuids <name>]
|
|
59
|
+
[--dedupe] [--dry-run] Merge multiple memory DBs
|
|
53
60
|
|
|
54
61
|
Setup:
|
|
55
62
|
awm setup --global Recommended. Writes ~/.mcp.json so AWM is available
|
|
@@ -370,6 +377,443 @@ function health() {
|
|
|
370
377
|
}
|
|
371
378
|
}
|
|
372
379
|
|
|
380
|
+
// ─── EXPORT ──────────────────────────────────────
|
|
381
|
+
|
|
382
|
+
async function exportMemories() {
|
|
383
|
+
let dbPath = '';
|
|
384
|
+
let agentFilter: string | null = null;
|
|
385
|
+
let outputPath: string | null = null;
|
|
386
|
+
let activeOnly = false;
|
|
387
|
+
|
|
388
|
+
for (let i = 1; i < args.length; i++) {
|
|
389
|
+
if (args[i] === '--db' && args[i + 1]) dbPath = args[++i];
|
|
390
|
+
else if (args[i] === '--agent' && args[i + 1]) agentFilter = args[++i];
|
|
391
|
+
else if (args[i] === '--output' && args[i + 1]) outputPath = args[++i];
|
|
392
|
+
else if (args[i] === '--active-only') activeOnly = true;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (!dbPath) {
|
|
396
|
+
console.error('Error: --db <path> is required');
|
|
397
|
+
process.exit(1);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (!existsSync(dbPath)) {
|
|
401
|
+
console.error(`Error: database not found: ${dbPath}`);
|
|
402
|
+
process.exit(1);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Dynamic import to avoid loading better-sqlite3 for other commands
|
|
406
|
+
const Database = (await import('better-sqlite3')).default;
|
|
407
|
+
const db = new Database(dbPath, { readonly: true });
|
|
408
|
+
|
|
409
|
+
// Build memory query
|
|
410
|
+
let memQuery = 'SELECT * FROM engrams';
|
|
411
|
+
const conditions: string[] = [];
|
|
412
|
+
const params: any[] = [];
|
|
413
|
+
|
|
414
|
+
if (agentFilter) {
|
|
415
|
+
conditions.push('agent_id = ?');
|
|
416
|
+
params.push(agentFilter);
|
|
417
|
+
}
|
|
418
|
+
if (activeOnly) {
|
|
419
|
+
conditions.push('retracted = 0');
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (conditions.length > 0) {
|
|
423
|
+
memQuery += ' WHERE ' + conditions.join(' AND ');
|
|
424
|
+
}
|
|
425
|
+
memQuery += ' ORDER BY created_at ASC';
|
|
426
|
+
|
|
427
|
+
const rows = db.prepare(memQuery).all(...params) as any[];
|
|
428
|
+
|
|
429
|
+
// Build memory objects (exclude embedding blobs)
|
|
430
|
+
const memories = rows.map((r: any) => ({
|
|
431
|
+
id: r.id,
|
|
432
|
+
agent_id: r.agent_id,
|
|
433
|
+
concept: r.concept,
|
|
434
|
+
content: r.content,
|
|
435
|
+
confidence: r.confidence,
|
|
436
|
+
salience: r.salience,
|
|
437
|
+
access_count: r.access_count,
|
|
438
|
+
last_accessed: r.last_accessed,
|
|
439
|
+
created_at: r.created_at,
|
|
440
|
+
stage: r.stage,
|
|
441
|
+
tags: r.tags ? JSON.parse(r.tags) : [],
|
|
442
|
+
memory_class: r.memory_class ?? 'working',
|
|
443
|
+
episode_id: r.episode_id ?? null,
|
|
444
|
+
task_status: r.task_status ?? null,
|
|
445
|
+
task_priority: r.task_priority ?? null,
|
|
446
|
+
supersedes: r.supersedes ?? null,
|
|
447
|
+
superseded_by: r.superseded_by ?? null,
|
|
448
|
+
retracted: r.retracted ?? 0,
|
|
449
|
+
}));
|
|
450
|
+
|
|
451
|
+
// Get memory IDs for association filtering
|
|
452
|
+
const memIds = new Set(memories.map((m: any) => m.id));
|
|
453
|
+
|
|
454
|
+
// Build associations
|
|
455
|
+
let assocQuery = 'SELECT * FROM associations';
|
|
456
|
+
const allAssocs = db.prepare(assocQuery).all() as any[];
|
|
457
|
+
const associations = allAssocs
|
|
458
|
+
.filter((a: any) => memIds.has(a.from_engram_id) && memIds.has(a.to_engram_id))
|
|
459
|
+
.map((a: any) => ({
|
|
460
|
+
from_id: a.from_engram_id,
|
|
461
|
+
to_id: a.to_engram_id,
|
|
462
|
+
weight: a.weight,
|
|
463
|
+
type: a.type ?? 'hebbian',
|
|
464
|
+
activation_count: a.activation_count ?? 0,
|
|
465
|
+
}));
|
|
466
|
+
|
|
467
|
+
// Collect unique agents
|
|
468
|
+
const agents = [...new Set(memories.map((m: any) => m.agent_id))];
|
|
469
|
+
|
|
470
|
+
const exportData = {
|
|
471
|
+
version: '0.6.0',
|
|
472
|
+
exported_at: new Date().toISOString(),
|
|
473
|
+
source_db: dbPath,
|
|
474
|
+
agent_filter: agentFilter,
|
|
475
|
+
memories,
|
|
476
|
+
associations,
|
|
477
|
+
stats: {
|
|
478
|
+
total_memories: memories.length,
|
|
479
|
+
total_associations: associations.length,
|
|
480
|
+
agents,
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const json = JSON.stringify(exportData, null, 2);
|
|
485
|
+
|
|
486
|
+
if (outputPath) {
|
|
487
|
+
writeFileSync(outputPath, json + '\n');
|
|
488
|
+
console.error(`Exported ${memories.length} memories, ${associations.length} associations → ${outputPath}`);
|
|
489
|
+
} else {
|
|
490
|
+
process.stdout.write(json + '\n');
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
db.close();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// ─── IMPORT ──────────────────────────────────────
|
|
497
|
+
|
|
498
|
+
async function importMemories() {
|
|
499
|
+
let filePath = '';
|
|
500
|
+
let dbPath = '';
|
|
501
|
+
let remapAgent: string | null = null;
|
|
502
|
+
let dedupe = false;
|
|
503
|
+
let dryRun = false;
|
|
504
|
+
let includeRetracted = false;
|
|
505
|
+
|
|
506
|
+
// First non-flag arg after 'import' is the file path
|
|
507
|
+
for (let i = 1; i < args.length; i++) {
|
|
508
|
+
if (args[i] === '--db' && args[i + 1]) dbPath = args[++i];
|
|
509
|
+
else if (args[i] === '--remap-agent' && args[i + 1]) remapAgent = args[++i];
|
|
510
|
+
else if (args[i] === '--dedupe') dedupe = true;
|
|
511
|
+
else if (args[i] === '--dry-run') dryRun = true;
|
|
512
|
+
else if (args[i] === '--include-retracted') includeRetracted = true;
|
|
513
|
+
else if (!args[i].startsWith('--') && !filePath) filePath = args[i];
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (!filePath) {
|
|
517
|
+
console.error('Error: <file> is required');
|
|
518
|
+
process.exit(1);
|
|
519
|
+
}
|
|
520
|
+
if (!dbPath) {
|
|
521
|
+
console.error('Error: --db <path> is required');
|
|
522
|
+
process.exit(1);
|
|
523
|
+
}
|
|
524
|
+
if (!existsSync(filePath)) {
|
|
525
|
+
console.error(`Error: import file not found: ${filePath}`);
|
|
526
|
+
process.exit(1);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const importData = JSON.parse(readFileSync(filePath, 'utf-8'));
|
|
530
|
+
if (!importData.memories || !Array.isArray(importData.memories)) {
|
|
531
|
+
console.error('Error: invalid export file — missing memories array');
|
|
532
|
+
process.exit(1);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const Database = (await import('better-sqlite3')).default;
|
|
536
|
+
const db = new Database(dbPath);
|
|
537
|
+
|
|
538
|
+
// Ensure tables exist in target
|
|
539
|
+
db.exec(`
|
|
540
|
+
CREATE TABLE IF NOT EXISTS engrams (
|
|
541
|
+
id TEXT PRIMARY KEY, agent_id TEXT NOT NULL, concept TEXT NOT NULL, content TEXT NOT NULL,
|
|
542
|
+
embedding BLOB, confidence REAL NOT NULL DEFAULT 0.5, salience REAL NOT NULL DEFAULT 0.5,
|
|
543
|
+
access_count INTEGER NOT NULL DEFAULT 0, last_accessed TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
544
|
+
salience_features TEXT NOT NULL DEFAULT '{}', reason_codes TEXT NOT NULL DEFAULT '[]',
|
|
545
|
+
stage TEXT NOT NULL DEFAULT 'active', ttl INTEGER, retracted INTEGER NOT NULL DEFAULT 0,
|
|
546
|
+
retracted_by TEXT, retracted_at TEXT, tags TEXT NOT NULL DEFAULT '[]',
|
|
547
|
+
episode_id TEXT, task_status TEXT, task_priority TEXT, blocked_by TEXT,
|
|
548
|
+
memory_class TEXT NOT NULL DEFAULT 'working', superseded_by TEXT, supersedes TEXT
|
|
549
|
+
);
|
|
550
|
+
CREATE TABLE IF NOT EXISTS associations (
|
|
551
|
+
id TEXT PRIMARY KEY, from_engram_id TEXT NOT NULL, to_engram_id TEXT NOT NULL,
|
|
552
|
+
weight REAL NOT NULL DEFAULT 0.1, confidence REAL NOT NULL DEFAULT 0.5,
|
|
553
|
+
type TEXT NOT NULL DEFAULT 'hebbian', activation_count INTEGER NOT NULL DEFAULT 0,
|
|
554
|
+
created_at TEXT NOT NULL, last_activated TEXT
|
|
555
|
+
);
|
|
556
|
+
`);
|
|
557
|
+
|
|
558
|
+
// Build dedup set if needed
|
|
559
|
+
const existingHashes = new Set<string>();
|
|
560
|
+
if (dedupe) {
|
|
561
|
+
const existing = db.prepare('SELECT concept, content FROM engrams').all() as any[];
|
|
562
|
+
for (const row of existing) {
|
|
563
|
+
const hash = (row.concept ?? '').toLowerCase().trim() + '||' + (row.content ?? '').toLowerCase().trim();
|
|
564
|
+
existingHashes.add(hash);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
const idMap = new Map<string, string>();
|
|
568
|
+
let imported = 0;
|
|
569
|
+
let skippedDupes = 0;
|
|
570
|
+
let skippedRetracted = 0;
|
|
571
|
+
|
|
572
|
+
const insertMem = db.prepare(`
|
|
573
|
+
INSERT INTO engrams (id, agent_id, concept, content, confidence, salience,
|
|
574
|
+
access_count, last_accessed, created_at, stage, tags, memory_class,
|
|
575
|
+
episode_id, task_status, task_priority, supersedes, superseded_by, retracted)
|
|
576
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
577
|
+
`);
|
|
578
|
+
|
|
579
|
+
const insertAssoc = db.prepare(`
|
|
580
|
+
INSERT INTO associations (id, from_engram_id, to_engram_id, weight, type, activation_count, created_at)
|
|
581
|
+
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))
|
|
582
|
+
`);
|
|
583
|
+
|
|
584
|
+
const importTx = db.transaction(() => {
|
|
585
|
+
// Import memories
|
|
586
|
+
for (const mem of importData.memories) {
|
|
587
|
+
// Skip retracted unless --include-retracted
|
|
588
|
+
if (mem.retracted && !includeRetracted) {
|
|
589
|
+
skippedRetracted++;
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Dedupe check
|
|
594
|
+
if (dedupe) {
|
|
595
|
+
const hash = (mem.concept ?? '').toLowerCase().trim() + '||' + (mem.content ?? '').toLowerCase().trim();
|
|
596
|
+
if (existingHashes.has(hash)) {
|
|
597
|
+
skippedDupes++;
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const newId = randomUUID();
|
|
603
|
+
idMap.set(mem.id, newId);
|
|
604
|
+
|
|
605
|
+
const agentId = remapAgent ?? mem.agent_id;
|
|
606
|
+
const tags = Array.isArray(mem.tags) ? JSON.stringify(mem.tags) : (mem.tags ?? '[]');
|
|
607
|
+
|
|
608
|
+
if (!dryRun) {
|
|
609
|
+
insertMem.run(
|
|
610
|
+
newId, agentId, mem.concept, mem.content,
|
|
611
|
+
mem.confidence ?? 0.5, mem.salience ?? 0.5,
|
|
612
|
+
mem.access_count ?? 0, mem.last_accessed ?? mem.created_at,
|
|
613
|
+
mem.created_at, mem.stage ?? 'active', tags,
|
|
614
|
+
mem.memory_class ?? 'working', mem.episode_id ?? null,
|
|
615
|
+
mem.task_status ?? null, mem.task_priority ?? null,
|
|
616
|
+
mem.supersedes ?? null, mem.superseded_by ?? null,
|
|
617
|
+
mem.retracted ?? 0
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
imported++;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Import associations (using remapped IDs)
|
|
624
|
+
let assocImported = 0;
|
|
625
|
+
const associations = importData.associations ?? [];
|
|
626
|
+
for (const assoc of associations) {
|
|
627
|
+
const fromId = idMap.get(assoc.from_id);
|
|
628
|
+
const toId = idMap.get(assoc.to_id);
|
|
629
|
+
if (!fromId || !toId) continue; // skip if either memory was skipped
|
|
630
|
+
|
|
631
|
+
if (!dryRun) {
|
|
632
|
+
insertAssoc.run(
|
|
633
|
+
randomUUID(), fromId, toId,
|
|
634
|
+
assoc.weight ?? 0.5, assoc.type ?? 'hebbian',
|
|
635
|
+
assoc.activation_count ?? 0
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
assocImported++;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return assocImported;
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
const assocCount = importTx();
|
|
645
|
+
|
|
646
|
+
const prefix = dryRun ? '[DRY RUN] Would import' : 'Imported';
|
|
647
|
+
console.log(`${prefix} ${imported} memories, ${assocCount} associations` +
|
|
648
|
+
(skippedDupes > 0 ? `, ${skippedDupes} skipped (dupes)` : '') +
|
|
649
|
+
(skippedRetracted > 0 ? `, ${skippedRetracted} skipped (retracted)` : '') +
|
|
650
|
+
(remapAgent ? ` (agent remapped to: ${remapAgent})` : ''));
|
|
651
|
+
|
|
652
|
+
db.close();
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// ─── MERGE ──────────────────────────────────────
|
|
656
|
+
|
|
657
|
+
async function mergeMemories() {
|
|
658
|
+
const Database = (await import('better-sqlite3')).default;
|
|
659
|
+
const { createHash, randomUUID } = await import('node:crypto');
|
|
660
|
+
|
|
661
|
+
let target = '';
|
|
662
|
+
const sources: string[] = [];
|
|
663
|
+
const remapEntries = new Map<string, string>();
|
|
664
|
+
let remapAllUuids = '';
|
|
665
|
+
let dedupe = false;
|
|
666
|
+
let dryRun = false;
|
|
667
|
+
|
|
668
|
+
for (let i = 1; i < args.length; i++) {
|
|
669
|
+
if (args[i] === '--target' && args[i + 1]) {
|
|
670
|
+
target = args[++i];
|
|
671
|
+
} else if (args[i] === '--source' && args[i + 1]) {
|
|
672
|
+
sources.push(args[++i]);
|
|
673
|
+
} else if (args[i] === '--remap' && args[i + 1]) {
|
|
674
|
+
const val = args[++i];
|
|
675
|
+
const eqIdx = val.indexOf('=');
|
|
676
|
+
if (eqIdx > 0) remapEntries.set(val.slice(0, eqIdx), val.slice(eqIdx + 1));
|
|
677
|
+
} else if (args[i] === '--remap-all-uuids' && args[i + 1]) {
|
|
678
|
+
remapAllUuids = args[++i];
|
|
679
|
+
} else if (args[i] === '--dedupe') {
|
|
680
|
+
dedupe = true;
|
|
681
|
+
} else if (args[i] === '--dry-run') {
|
|
682
|
+
dryRun = true;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (!target || sources.length === 0) {
|
|
687
|
+
console.error('Usage: awm merge --target <path> --source <path> [--source <path>...] [--remap uuid=name] [--remap-all-uuids name] [--dedupe] [--dry-run]');
|
|
688
|
+
process.exit(1);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
692
|
+
|
|
693
|
+
function remapAgentId(agentId: string): string {
|
|
694
|
+
if (remapEntries.has(agentId)) return remapEntries.get(agentId)!;
|
|
695
|
+
if (remapAllUuids && UUID_RE.test(agentId)) return remapAllUuids;
|
|
696
|
+
return agentId;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function contentHash(concept: string, content: string): string {
|
|
700
|
+
return createHash('sha256').update((concept + '\n' + content).toLowerCase().trim()).digest('hex');
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
console.log(`Target: ${target}${dryRun ? ' (DRY RUN)' : ''}`);
|
|
704
|
+
|
|
705
|
+
const targetDb = new Database(target);
|
|
706
|
+
targetDb.pragma('journal_mode = WAL');
|
|
707
|
+
targetDb.pragma('foreign_keys = ON');
|
|
708
|
+
|
|
709
|
+
// Ensure tables exist in target
|
|
710
|
+
targetDb.exec(`
|
|
711
|
+
CREATE TABLE IF NOT EXISTS engrams (
|
|
712
|
+
id TEXT PRIMARY KEY, agent_id TEXT NOT NULL, concept TEXT NOT NULL, content TEXT NOT NULL,
|
|
713
|
+
embedding BLOB, confidence REAL NOT NULL DEFAULT 0.5, salience REAL NOT NULL DEFAULT 0.5,
|
|
714
|
+
access_count INTEGER NOT NULL DEFAULT 0, last_accessed TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
715
|
+
salience_features TEXT NOT NULL DEFAULT '{}', reason_codes TEXT NOT NULL DEFAULT '[]',
|
|
716
|
+
stage TEXT NOT NULL DEFAULT 'active', ttl INTEGER, retracted INTEGER NOT NULL DEFAULT 0,
|
|
717
|
+
retracted_by TEXT, retracted_at TEXT, tags TEXT NOT NULL DEFAULT '[]'
|
|
718
|
+
);
|
|
719
|
+
CREATE TABLE IF NOT EXISTS associations (
|
|
720
|
+
id TEXT PRIMARY KEY, from_engram_id TEXT NOT NULL, to_engram_id TEXT NOT NULL,
|
|
721
|
+
weight REAL NOT NULL DEFAULT 0.1, confidence REAL NOT NULL DEFAULT 0.5,
|
|
722
|
+
type TEXT NOT NULL DEFAULT 'hebbian', activation_count INTEGER NOT NULL DEFAULT 0,
|
|
723
|
+
created_at TEXT NOT NULL, last_activated TEXT NOT NULL
|
|
724
|
+
);
|
|
725
|
+
`);
|
|
726
|
+
|
|
727
|
+
// Build dedupe hash set from existing target memories
|
|
728
|
+
const existingHashes = new Set<string>();
|
|
729
|
+
if (dedupe) {
|
|
730
|
+
const rows = targetDb.prepare('SELECT concept, content FROM engrams').all() as { concept: string; content: string }[];
|
|
731
|
+
for (const row of rows) existingHashes.add(contentHash(row.concept, row.content));
|
|
732
|
+
console.log(`Target has ${existingHashes.size} unique memories (for dedupe)\n`);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const insertEngram = targetDb.prepare(`
|
|
736
|
+
INSERT OR IGNORE INTO engrams (id, agent_id, concept, content, confidence, salience, access_count,
|
|
737
|
+
last_accessed, created_at, salience_features, reason_codes, stage, ttl,
|
|
738
|
+
retracted, retracted_by, retracted_at, tags)
|
|
739
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
740
|
+
`);
|
|
741
|
+
const insertAssoc = targetDb.prepare(`
|
|
742
|
+
INSERT OR IGNORE INTO associations (id, from_engram_id, to_engram_id, weight, confidence, type,
|
|
743
|
+
activation_count, created_at, last_activated)
|
|
744
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
745
|
+
`);
|
|
746
|
+
|
|
747
|
+
let totalMemories = 0, totalAssociations = 0, totalSkipped = 0;
|
|
748
|
+
|
|
749
|
+
for (const sourcePath of sources) {
|
|
750
|
+
if (!existsSync(sourcePath)) {
|
|
751
|
+
console.error(` Source not found: ${sourcePath}`);
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
const sourceDb = new Database(sourcePath, { readonly: true });
|
|
756
|
+
const engrams = sourceDb.prepare(
|
|
757
|
+
`SELECT id, agent_id, concept, content, confidence, salience, access_count,
|
|
758
|
+
last_accessed, created_at, salience_features, reason_codes, stage, ttl,
|
|
759
|
+
retracted, retracted_by, retracted_at, tags FROM engrams`
|
|
760
|
+
).all() as any[];
|
|
761
|
+
const assocs = sourceDb.prepare(
|
|
762
|
+
`SELECT id, from_engram_id, to_engram_id, weight, confidence, type,
|
|
763
|
+
activation_count, created_at, last_activated FROM associations`
|
|
764
|
+
).all() as any[];
|
|
765
|
+
|
|
766
|
+
const idMap = new Map<string, string>();
|
|
767
|
+
const skippedIds = new Set<string>();
|
|
768
|
+
|
|
769
|
+
const result = targetDb.transaction(() => {
|
|
770
|
+
let imported = 0, skipped = 0;
|
|
771
|
+
for (const e of engrams) {
|
|
772
|
+
const hash = contentHash(e.concept, e.content);
|
|
773
|
+
if (dedupe && existingHashes.has(hash)) { skippedIds.add(e.id); skipped++; continue; }
|
|
774
|
+
const newId = randomUUID();
|
|
775
|
+
idMap.set(e.id, newId);
|
|
776
|
+
existingHashes.add(hash);
|
|
777
|
+
if (!dryRun) {
|
|
778
|
+
insertEngram.run(newId, remapAgentId(e.agent_id), e.concept, e.content, e.confidence,
|
|
779
|
+
e.salience, e.access_count, e.last_accessed, e.created_at, e.salience_features,
|
|
780
|
+
e.reason_codes, e.stage, e.ttl, e.retracted, e.retracted_by, e.retracted_at, e.tags);
|
|
781
|
+
}
|
|
782
|
+
imported++;
|
|
783
|
+
}
|
|
784
|
+
let assocImported = 0;
|
|
785
|
+
for (const a of assocs) {
|
|
786
|
+
if (skippedIds.has(a.from_engram_id) || skippedIds.has(a.to_engram_id)) continue;
|
|
787
|
+
const fromId = idMap.get(a.from_engram_id);
|
|
788
|
+
const toId = idMap.get(a.to_engram_id);
|
|
789
|
+
if (!fromId || !toId) continue;
|
|
790
|
+
if (!dryRun) {
|
|
791
|
+
insertAssoc.run(randomUUID(), fromId, toId, a.weight, a.confidence, a.type,
|
|
792
|
+
a.activation_count, a.created_at, a.last_activated);
|
|
793
|
+
}
|
|
794
|
+
assocImported++;
|
|
795
|
+
}
|
|
796
|
+
return { imported, skipped, assocImported };
|
|
797
|
+
})();
|
|
798
|
+
|
|
799
|
+
sourceDb.close();
|
|
800
|
+
|
|
801
|
+
const agentSet = new Set(engrams.map((e: any) => remapAgentId(e.agent_id)));
|
|
802
|
+
console.log(` Source: ${sourcePath}`);
|
|
803
|
+
console.log(` Engrams: ${engrams.length} total, ${result.imported} imported, ${result.skipped} skipped`);
|
|
804
|
+
console.log(` Associations: ${assocs.length} total, ${result.assocImported} imported`);
|
|
805
|
+
console.log(` Agents: ${agentSet.size} (${[...agentSet].slice(0, 5).join(', ')}${agentSet.size > 5 ? '...' : ''})\n`);
|
|
806
|
+
|
|
807
|
+
totalMemories += result.imported;
|
|
808
|
+
totalAssociations += result.assocImported;
|
|
809
|
+
totalSkipped += result.skipped;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
targetDb.close();
|
|
813
|
+
console.log(`\nTotal: ${totalMemories} memories, ${totalAssociations} associations imported. ${totalSkipped} skipped.`);
|
|
814
|
+
if (dryRun) console.log('(dry run — no data written)');
|
|
815
|
+
}
|
|
816
|
+
|
|
373
817
|
// ─── Dispatch ──────────────────────────────────────
|
|
374
818
|
|
|
375
819
|
switch (command) {
|
|
@@ -385,6 +829,15 @@ switch (command) {
|
|
|
385
829
|
case 'health':
|
|
386
830
|
health();
|
|
387
831
|
break;
|
|
832
|
+
case 'export':
|
|
833
|
+
exportMemories();
|
|
834
|
+
break;
|
|
835
|
+
case 'import':
|
|
836
|
+
importMemories();
|
|
837
|
+
break;
|
|
838
|
+
case 'merge':
|
|
839
|
+
mergeMemories();
|
|
840
|
+
break;
|
|
388
841
|
case '--help':
|
|
389
842
|
case '-h':
|
|
390
843
|
case undefined:
|
|
@@ -27,14 +27,19 @@ export function registerCoordinationTools(server: McpServer, db: Database.Databa
|
|
|
27
27
|
async ({ agent_name, role, pid, capabilities, workspace }) => {
|
|
28
28
|
const capsJson = capabilities ? JSON.stringify(capabilities) : null;
|
|
29
29
|
|
|
30
|
+
// Look up ANY existing agent with same name+workspace — including dead ones (upsert, reuse UUID)
|
|
30
31
|
const existing = workspace
|
|
31
|
-
? db.prepare(`SELECT id, status FROM coord_agents WHERE name = ? AND workspace = ?
|
|
32
|
-
: db.prepare(`SELECT id, status FROM coord_agents WHERE name = ? AND workspace IS NULL
|
|
32
|
+
? db.prepare(`SELECT id, status FROM coord_agents WHERE name = ? AND workspace = ? ORDER BY last_seen DESC LIMIT 1`).get(agent_name, workspace) as { id: string; status: string } | undefined
|
|
33
|
+
: db.prepare(`SELECT id, status FROM coord_agents WHERE name = ? AND workspace IS NULL ORDER BY last_seen DESC LIMIT 1`).get(agent_name) as { id: string; status: string } | undefined;
|
|
33
34
|
|
|
34
35
|
if (existing) {
|
|
35
|
-
|
|
36
|
-
db.prepare(`
|
|
37
|
-
|
|
36
|
+
const wasDead = existing.status === 'dead';
|
|
37
|
+
db.prepare(`UPDATE coord_agents SET last_seen = datetime('now'), status = CASE WHEN status = 'dead' THEN 'idle' ELSE status END, pid = COALESCE(?, pid), capabilities = COALESCE(?, capabilities) WHERE id = ?`).run(pid ?? null, capsJson, existing.id);
|
|
38
|
+
const action = wasDead ? 'reconnected' : 'heartbeat';
|
|
39
|
+
const eventType = wasDead ? 'reconnected' : 'heartbeat';
|
|
40
|
+
const detail = wasDead ? `${agent_name} reconnected via MCP (was dead)` : `heartbeat from ${agent_name}`;
|
|
41
|
+
db.prepare(`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, ?, ?)`).run(existing.id, eventType, detail);
|
|
42
|
+
return { content: [{ type: 'text' as const, text: JSON.stringify({ agentId: existing.id, action, status: wasDead ? 'idle' : existing.status }) }] };
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
const id = randomUUID();
|