agent-working-memory 0.5.6 → 0.6.1

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.
Files changed (135) hide show
  1. package/README.md +78 -43
  2. package/dist/adapters/claude-code.d.ts +4 -0
  3. package/dist/adapters/claude-code.d.ts.map +1 -0
  4. package/dist/adapters/claude-code.js +218 -0
  5. package/dist/adapters/claude-code.js.map +1 -0
  6. package/dist/adapters/codex.d.ts +4 -0
  7. package/dist/adapters/codex.d.ts.map +1 -0
  8. package/dist/adapters/codex.js +226 -0
  9. package/dist/adapters/codex.js.map +1 -0
  10. package/dist/adapters/common.d.ts +34 -0
  11. package/dist/adapters/common.d.ts.map +1 -0
  12. package/dist/adapters/common.js +145 -0
  13. package/dist/adapters/common.js.map +1 -0
  14. package/dist/adapters/cursor.d.ts +4 -0
  15. package/dist/adapters/cursor.d.ts.map +1 -0
  16. package/dist/adapters/cursor.js +138 -0
  17. package/dist/adapters/cursor.js.map +1 -0
  18. package/dist/adapters/http.d.ts +4 -0
  19. package/dist/adapters/http.d.ts.map +1 -0
  20. package/dist/adapters/http.js +88 -0
  21. package/dist/adapters/http.js.map +1 -0
  22. package/dist/adapters/index.d.ts +7 -0
  23. package/dist/adapters/index.d.ts.map +1 -0
  24. package/dist/adapters/index.js +21 -0
  25. package/dist/adapters/index.js.map +1 -0
  26. package/dist/adapters/types.d.ts +65 -0
  27. package/dist/adapters/types.d.ts.map +1 -0
  28. package/dist/adapters/types.js +4 -0
  29. package/dist/adapters/types.js.map +1 -0
  30. package/dist/api/routes.d.ts.map +1 -1
  31. package/dist/api/routes.js +40 -1
  32. package/dist/api/routes.js.map +1 -1
  33. package/dist/cli.js +504 -230
  34. package/dist/cli.js.map +1 -1
  35. package/dist/coordination/events.d.ts +59 -0
  36. package/dist/coordination/events.d.ts.map +1 -0
  37. package/dist/coordination/events.js +28 -0
  38. package/dist/coordination/events.js.map +1 -0
  39. package/dist/coordination/index.d.ts +10 -1
  40. package/dist/coordination/index.d.ts.map +1 -1
  41. package/dist/coordination/index.js +87 -3
  42. package/dist/coordination/index.js.map +1 -1
  43. package/dist/coordination/mcp-tools.d.ts.map +1 -1
  44. package/dist/coordination/mcp-tools.js +10 -5
  45. package/dist/coordination/mcp-tools.js.map +1 -1
  46. package/dist/coordination/peer-decisions.d.ts +40 -0
  47. package/dist/coordination/peer-decisions.d.ts.map +1 -0
  48. package/dist/coordination/peer-decisions.js +82 -0
  49. package/dist/coordination/peer-decisions.js.map +1 -0
  50. package/dist/coordination/plugin-loader.d.ts +18 -0
  51. package/dist/coordination/plugin-loader.d.ts.map +1 -0
  52. package/dist/coordination/plugin-loader.js +55 -0
  53. package/dist/coordination/plugin-loader.js.map +1 -0
  54. package/dist/coordination/plugin.d.ts +40 -0
  55. package/dist/coordination/plugin.d.ts.map +1 -0
  56. package/dist/coordination/plugin.js +22 -0
  57. package/dist/coordination/plugin.js.map +1 -0
  58. package/dist/coordination/routes.d.ts +2 -1
  59. package/dist/coordination/routes.d.ts.map +1 -1
  60. package/dist/coordination/routes.js +1027 -65
  61. package/dist/coordination/routes.js.map +1 -1
  62. package/dist/coordination/schema.d.ts.map +1 -1
  63. package/dist/coordination/schema.js +104 -12
  64. package/dist/coordination/schema.js.map +1 -1
  65. package/dist/coordination/schemas.d.ts +105 -5
  66. package/dist/coordination/schemas.d.ts.map +1 -1
  67. package/dist/coordination/schemas.js +87 -1
  68. package/dist/coordination/schemas.js.map +1 -1
  69. package/dist/coordination/stale.d.ts +2 -0
  70. package/dist/coordination/stale.d.ts.map +1 -1
  71. package/dist/coordination/stale.js +7 -1
  72. package/dist/coordination/stale.js.map +1 -1
  73. package/dist/coordination/types.d.ts +252 -0
  74. package/dist/coordination/types.d.ts.map +1 -0
  75. package/dist/coordination/types.js +8 -0
  76. package/dist/coordination/types.js.map +1 -0
  77. package/dist/coordination/write-mutex.d.ts +26 -0
  78. package/dist/coordination/write-mutex.d.ts.map +1 -0
  79. package/dist/coordination/write-mutex.js +63 -0
  80. package/dist/coordination/write-mutex.js.map +1 -0
  81. package/dist/core/embeddings.d.ts +2 -0
  82. package/dist/core/embeddings.d.ts.map +1 -1
  83. package/dist/core/embeddings.js +4 -0
  84. package/dist/core/embeddings.js.map +1 -1
  85. package/dist/engine/activation.d.ts.map +1 -1
  86. package/dist/engine/activation.js +135 -26
  87. package/dist/engine/activation.js.map +1 -1
  88. package/dist/engine/consolidation.d.ts.map +1 -1
  89. package/dist/engine/consolidation.js +42 -12
  90. package/dist/engine/consolidation.js.map +1 -1
  91. package/dist/engine/retraction.d.ts +3 -1
  92. package/dist/engine/retraction.d.ts.map +1 -1
  93. package/dist/engine/retraction.js +19 -6
  94. package/dist/engine/retraction.js.map +1 -1
  95. package/dist/index.js +82 -16
  96. package/dist/index.js.map +1 -1
  97. package/dist/mcp.js +113 -6
  98. package/dist/mcp.js.map +1 -1
  99. package/dist/storage/sqlite.d.ts +24 -1
  100. package/dist/storage/sqlite.d.ts.map +1 -1
  101. package/dist/storage/sqlite.js +88 -7
  102. package/dist/storage/sqlite.js.map +1 -1
  103. package/dist/types/engram.d.ts +24 -0
  104. package/dist/types/engram.d.ts.map +1 -1
  105. package/dist/types/engram.js.map +1 -1
  106. package/package.json +3 -1
  107. package/src/adapters/claude-code.ts +234 -0
  108. package/src/adapters/codex.ts +262 -0
  109. package/src/adapters/common.ts +172 -0
  110. package/src/adapters/cursor.ts +150 -0
  111. package/src/adapters/http.ts +100 -0
  112. package/src/adapters/index.ts +31 -0
  113. package/src/adapters/types.ts +75 -0
  114. package/src/api/routes.ts +50 -1
  115. package/src/cli.ts +561 -239
  116. package/src/coordination/events.ts +90 -0
  117. package/src/coordination/index.ts +102 -3
  118. package/src/coordination/mcp-tools.ts +10 -5
  119. package/src/coordination/peer-decisions.ts +105 -0
  120. package/src/coordination/plugin-loader.ts +60 -0
  121. package/src/coordination/plugin.ts +44 -0
  122. package/src/coordination/routes.ts +1353 -92
  123. package/src/coordination/schema.ts +91 -12
  124. package/src/coordination/schemas.ts +104 -1
  125. package/src/coordination/stale.ts +11 -2
  126. package/src/coordination/types.ts +311 -0
  127. package/src/coordination/write-mutex.ts +69 -0
  128. package/src/core/embeddings.ts +5 -0
  129. package/src/engine/activation.ts +138 -26
  130. package/src/engine/consolidation.ts +44 -12
  131. package/src/engine/retraction.ts +22 -6
  132. package/src/index.ts +76 -14
  133. package/src/mcp.ts +142 -9
  134. package/src/storage/sqlite.ts +92 -7
  135. package/src/types/engram.ts +28 -0
@@ -0,0 +1,90 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Typed internal event emitter for coordination.
5
+ * Lightweight Node EventEmitter wrapper with typed events.
6
+ * Used to decouple route handlers from side-effects (channel push, logging, etc).
7
+ */
8
+
9
+ import { EventEmitter } from 'events';
10
+
11
+ // ─── Event Payloads ──────────────────────────────────────────────
12
+
13
+ export interface AssignmentCreatedEvent {
14
+ assignmentId: string;
15
+ agentId: string;
16
+ task: string;
17
+ workspace?: string;
18
+ }
19
+
20
+ export interface AssignmentUpdatedEvent {
21
+ assignmentId: string;
22
+ agentId: string | null;
23
+ status: string;
24
+ result?: string;
25
+ }
26
+
27
+ export interface AssignmentCompletedEvent {
28
+ assignmentId: string;
29
+ agentId: string | null;
30
+ result: string | null;
31
+ }
32
+
33
+ export interface AgentCheckinEvent {
34
+ agentId: string;
35
+ name: string;
36
+ role: string;
37
+ workspace?: string;
38
+ }
39
+
40
+ export interface AgentCheckoutEvent {
41
+ agentId: string;
42
+ name: string;
43
+ }
44
+
45
+ export interface SessionStartedEvent {
46
+ agentId: string;
47
+ channelId: string;
48
+ }
49
+
50
+ export interface SessionClosedEvent {
51
+ agentId: string;
52
+ channelId: string;
53
+ }
54
+
55
+ // ─── Event Map ───────────────────────────────────────────────────
56
+
57
+ export interface CoordinationEvents {
58
+ 'assignment.created': [AssignmentCreatedEvent];
59
+ 'assignment.updated': [AssignmentUpdatedEvent];
60
+ 'assignment.completed': [AssignmentCompletedEvent];
61
+ 'agent.checkin': [AgentCheckinEvent];
62
+ 'agent.checkout': [AgentCheckoutEvent];
63
+ 'session.started': [SessionStartedEvent];
64
+ 'session.closed': [SessionClosedEvent];
65
+ }
66
+
67
+ // ─── Typed Event Bus ─────────────────────────────────────────────
68
+
69
+ export class CoordinationEventBus extends EventEmitter {
70
+ emit<K extends keyof CoordinationEvents>(event: K, ...args: CoordinationEvents[K]): boolean {
71
+ return super.emit(event, ...args);
72
+ }
73
+
74
+ on<K extends keyof CoordinationEvents>(event: K, listener: (...args: CoordinationEvents[K]) => void): this {
75
+ return super.on(event, listener as (...args: unknown[]) => void);
76
+ }
77
+
78
+ once<K extends keyof CoordinationEvents>(event: K, listener: (...args: CoordinationEvents[K]) => void): this {
79
+ return super.once(event, listener as (...args: unknown[]) => void);
80
+ }
81
+
82
+ off<K extends keyof CoordinationEvents>(event: K, listener: (...args: CoordinationEvents[K]) => void): this {
83
+ return super.off(event, listener as (...args: unknown[]) => void);
84
+ }
85
+ }
86
+
87
+ /** Create a new coordination event bus. */
88
+ export function createEventBus(): CoordinationEventBus {
89
+ return new CoordinationEventBus();
90
+ }
@@ -7,10 +7,29 @@
7
7
 
8
8
  import type { FastifyInstance } from 'fastify';
9
9
  import type Database from 'better-sqlite3';
10
+ import type { EngramStore } from '../storage/sqlite.js';
10
11
  import { ZodError } from 'zod';
11
12
  import { initCoordinationTables } from './schema.js';
12
13
  import { registerCoordinationRoutes } from './routes.js';
13
- import { cleanSlate } from './stale.js';
14
+ import { cleanSlate, pruneOldHeartbeats, purgeDeadAgents } from './stale.js';
15
+ import { createWriteMutex, needsWriteLock } from './write-mutex.js';
16
+ import { createEventBus, type CoordinationEventBus } from './events.js';
17
+ import { loadPlugins, teardownPlugins } from './plugin-loader.js';
18
+
19
+ export type * from './types.js';
20
+ export { type CoordinationEventBus, type CoordinationEvents } from './events.js';
21
+ export type { AWMPlugin, AWMPluginContext } from './plugin.js';
22
+
23
+ /** Active cleanup intervals — cleared on shutdown. */
24
+ const cleanupIntervals: NodeJS.Timeout[] = [];
25
+
26
+ /** Singleton event bus for this coordination module instance. */
27
+ let coordinationEventBus: CoordinationEventBus | null = null;
28
+
29
+ /** Get the coordination event bus (available after initCoordination). */
30
+ export function getEventBus(): CoordinationEventBus | null {
31
+ return coordinationEventBus;
32
+ }
14
33
 
15
34
  /** Check if coordination is enabled via environment variable. */
16
35
  export function isCoordinationEnabled(): boolean {
@@ -19,15 +38,49 @@ export function isCoordinationEnabled(): boolean {
19
38
  }
20
39
 
21
40
  /** Initialize the coordination module: create tables, clean slate, mount routes, error handler. */
22
- export function initCoordination(app: FastifyInstance, db: Database.Database): void {
41
+ export function initCoordination(app: FastifyInstance, db: Database.Database, store?: EngramStore): void {
23
42
  // Create coordination tables (idempotent)
24
43
  initCoordinationTables(db);
25
44
 
26
45
  // Clean slate: mark stale agents as dead from previous sessions
27
46
  cleanSlate(db);
28
47
 
48
+ // CORS — allow localhost origins only (coordination is local-only)
49
+ app.addHook('onRequest', async (request, reply) => {
50
+ const origin = request.headers.origin ?? '';
51
+ if (/^https?:\/\/localhost(:\d+)?$/.test(origin) || /^https?:\/\/127\.0\.0\.1(:\d+)?$/.test(origin)) {
52
+ reply.header('Access-Control-Allow-Origin', origin);
53
+ reply.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
54
+ reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
55
+ }
56
+ if (request.method === 'OPTIONS') {
57
+ return reply.code(204).send();
58
+ }
59
+ });
60
+
61
+ // Body size limit — 256KB max for coordination requests (tasks with context can be large)
62
+ app.addHook('onRoute', (routeOptions) => {
63
+ if (!routeOptions.bodyLimit) {
64
+ routeOptions.bodyLimit = 256_000;
65
+ }
66
+ });
67
+
68
+ // Write serialization: serialize POST/PATCH/PUT/DELETE through a mutex
69
+ // to prevent SQLITE_BUSY under 5+ concurrent worker burst
70
+ const writeMutex = createWriteMutex();
71
+ app.addHook('preHandler', async (request, reply) => {
72
+ if (needsWriteLock(request.method, request.url)) {
73
+ const release = await writeMutex.acquire();
74
+ // Release after response is sent (onResponse fires after reply)
75
+ reply.raw.on('finish', release);
76
+ }
77
+ });
78
+
79
+ // Create event bus for decoupled side-effects
80
+ coordinationEventBus = createEventBus();
81
+
29
82
  // Mount all coordination HTTP routes
30
- registerCoordinationRoutes(app, db);
83
+ registerCoordinationRoutes(app, db, store, coordinationEventBus);
31
84
 
32
85
  // ZodError handler — coordination routes use .parse() which throws on invalid params
33
86
  app.setErrorHandler((error: Error & { statusCode?: number }, _request, reply) => {
@@ -43,5 +96,51 @@ export function initCoordination(app: FastifyInstance, db: Database.Database): v
43
96
  });
44
97
  });
45
98
 
99
+ // Periodic cleanup: prune heartbeat events every 30 min, purge dead agents every hour
100
+ cleanupIntervals.push(
101
+ setInterval(() => {
102
+ try { pruneOldHeartbeats(db); } catch { /* db may be closed */ }
103
+ }, 30 * 60 * 1000),
104
+ setInterval(() => {
105
+ try { purgeDeadAgents(db, 24); } catch { /* db may be closed */ }
106
+ }, 60 * 60 * 1000),
107
+ );
108
+
109
+ // Periodic channel liveness probe every 60s — mark unreachable sessions as disconnected
110
+ cleanupIntervals.push(
111
+ setInterval(async () => {
112
+ try {
113
+ const sessions = db.prepare(
114
+ `SELECT agent_id, channel_id FROM coord_channel_sessions WHERE status = 'connected'`
115
+ ).all() as Array<{ agent_id: string; channel_id: string }>;
116
+
117
+ for (const session of sessions) {
118
+ try {
119
+ const res = await fetch(`${session.channel_id}/health`, {
120
+ signal: AbortSignal.timeout(3000),
121
+ });
122
+ if (!res.ok) {
123
+ db.prepare(`UPDATE coord_channel_sessions SET status = 'disconnected' WHERE agent_id = ?`).run(session.agent_id);
124
+ }
125
+ } catch {
126
+ db.prepare(`UPDATE coord_channel_sessions SET status = 'disconnected' WHERE agent_id = ?`).run(session.agent_id);
127
+ }
128
+ }
129
+ } catch { /* db may be closed */ }
130
+ }, 60_000),
131
+ );
132
+
133
+ // Load plugins (async — fire and forget, errors logged per-plugin)
134
+ loadPlugins({ events: coordinationEventBus, db, fastify: app }).catch((err) => {
135
+ console.error(' [plugin] Plugin loader error:', (err as Error).message);
136
+ });
137
+
46
138
  console.log(' Coordination module enabled');
47
139
  }
140
+
141
+ /** Stop periodic cleanup intervals and teardown plugins. Call on server shutdown. */
142
+ export async function stopCoordinationCleanup(): Promise<void> {
143
+ for (const id of cleanupIntervals) clearInterval(id);
144
+ cleanupIntervals.length = 0;
145
+ await teardownPlugins();
146
+ }
@@ -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 = ? AND status != 'dead'`).get(agent_name, workspace) as { id: string; status: string } | undefined
32
- : db.prepare(`SELECT id, status FROM coord_agents WHERE name = ? AND workspace IS NULL AND status != 'dead'`).get(agent_name) as { id: string; status: string } | undefined;
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
- db.prepare(`UPDATE coord_agents SET last_seen = datetime('now'), pid = COALESCE(?, pid), capabilities = COALESCE(?, capabilities) WHERE id = ?`).run(pid ?? null, capsJson, existing.id);
36
- db.prepare(`INSERT INTO coord_events (agent_id, event_type, detail) VALUES (?, 'heartbeat', ?)`).run(existing.id, `heartbeat from ${agent_name}`);
37
- return { content: [{ type: 'text' as const, text: JSON.stringify({ agentId: existing.id, action: 'heartbeat', status: existing.status }) }] };
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();
@@ -0,0 +1,105 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Peer-decision injection for memory_recall.
5
+ *
6
+ * When coordination is enabled, memory_recall appends recent decisions
7
+ * made by OTHER agents that are relevant to the current recall query.
8
+ * This enables passive cross-agent knowledge sharing without explicit
9
+ * communication.
10
+ */
11
+
12
+ import type Database from 'better-sqlite3';
13
+
14
+ export interface PeerDecision {
15
+ id: number;
16
+ author_name: string;
17
+ assignment_id: string | null;
18
+ tags: string | null;
19
+ summary: string;
20
+ created_at: string;
21
+ }
22
+
23
+ /** Stop words filtered out before keyword matching. */
24
+ const STOP_WORDS = new Set([
25
+ 'that', 'this', 'with', 'from', 'have', 'been', 'will', 'when', 'what',
26
+ 'which', 'then', 'than', 'into', 'over', 'after', 'some', 'more', 'also',
27
+ 'most', 'other', 'each', 'such', 'only', 'just', 'about', 'there', 'their',
28
+ 'where', 'would', 'could', 'should', 'these', 'those', 'make', 'made',
29
+ 'using', 'used', 'call', 'calls', 'does', 'done', 'were', 'they',
30
+ ]);
31
+
32
+ /**
33
+ * Extract significant keywords from a recall query for relevance matching.
34
+ * Returns up to maxKeywords words of length >= 4 that are not stop words.
35
+ */
36
+ export function extractKeywords(query: string, maxKeywords = 8): string[] {
37
+ return query
38
+ .toLowerCase()
39
+ .replace(/[^a-z0-9\s]/g, ' ')
40
+ .split(/\s+/)
41
+ .filter(w => w.length >= 4 && !STOP_WORDS.has(w))
42
+ .slice(0, maxKeywords);
43
+ }
44
+
45
+ /**
46
+ * Query coord_decisions for recent decisions by agents OTHER than the current one
47
+ * that are relevant to the given query string.
48
+ *
49
+ * @param db The shared coordination database handle.
50
+ * @param selfName The current agent's name (AWM_AGENT_ID / WORKER_NAME). Excluded from results.
51
+ * @param query The recall query text used for keyword relevance matching.
52
+ * @param windowHours How far back to look (default 1 hour).
53
+ * @param limit Maximum number of decisions to return (default 5).
54
+ * @returns Array of peer decisions, ordered newest-first. Empty array on any error.
55
+ */
56
+ export function queryPeerDecisions(
57
+ db: Database.Database,
58
+ selfName: string,
59
+ query: string,
60
+ windowHours = 1,
61
+ limit = 5,
62
+ ): PeerDecision[] {
63
+ try {
64
+ const safeHours = Math.max(0.1, Math.min(windowHours, 168)); // 6min – 1 week
65
+ const safeLimit = Math.max(1, Math.min(limit, 50));
66
+ const keywords = extractKeywords(query);
67
+
68
+ const baseWhere = `
69
+ d.created_at >= datetime('now', '-${safeHours} hours')
70
+ AND a.name != ?
71
+ `;
72
+
73
+ const select = `
74
+ SELECT d.id, a.name AS author_name, d.assignment_id, d.tags, d.summary, d.created_at
75
+ FROM coord_decisions d
76
+ JOIN coord_agents a ON d.author_id = a.id
77
+ `;
78
+
79
+ if (keywords.length === 0) {
80
+ // No useful keywords — return the N most recent decisions from other agents
81
+ const sql = `${select} WHERE ${baseWhere} ORDER BY d.created_at DESC LIMIT ?`;
82
+ return db.prepare(sql).all(selfName, safeLimit) as PeerDecision[];
83
+ }
84
+
85
+ // Build keyword filter: match any keyword in summary or tags
86
+ const kwClauses = keywords.map(() => `(d.summary LIKE ? OR d.tags LIKE ?)`).join(' OR ');
87
+ const sql = `${select} WHERE ${baseWhere} AND (${kwClauses}) ORDER BY d.created_at DESC LIMIT ?`;
88
+ const kwParams = keywords.flatMap(kw => [`%${kw}%`, `%${kw}%`]);
89
+
90
+ return db.prepare(sql).all(selfName, ...kwParams, safeLimit) as PeerDecision[];
91
+ } catch {
92
+ return []; // non-fatal — peer decisions are best-effort
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Format peer decisions as a text section to append to memory_recall output.
98
+ * Returns an empty string when there are no peer decisions.
99
+ */
100
+ export function formatPeerDecisions(decisions: PeerDecision[], windowHours = 1): string {
101
+ if (decisions.length === 0) return '';
102
+ const label = windowHours === 1 ? '1h' : `${windowHours}h`;
103
+ const lines = decisions.map(d => `[${d.author_name}] ${d.summary}`);
104
+ return `\n--- Peer Decisions (last ${label}) ---\n${lines.join('\n')}`;
105
+ }
@@ -0,0 +1,60 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Plugin loader for AWM coordination.
5
+ *
6
+ * Reads AWM_PLUGINS env var (comma-separated module paths or package names),
7
+ * dynamically imports each, and calls register() with the plugin context.
8
+ *
9
+ * Usage:
10
+ * AWM_PLUGINS=./plugins/slack-notify.js,my-awm-plugin npm start
11
+ */
12
+
13
+ import type { AWMPlugin, AWMPluginContext } from './plugin.js';
14
+
15
+ /** Loaded plugin instances (for teardown). */
16
+ const loadedPlugins: AWMPlugin[] = [];
17
+
18
+ /**
19
+ * Load and register all plugins specified in AWM_PLUGINS env var.
20
+ * Errors in individual plugins are logged but don't prevent other plugins from loading.
21
+ */
22
+ export async function loadPlugins(ctx: AWMPluginContext): Promise<void> {
23
+ const pluginList = process.env.AWM_PLUGINS;
24
+ if (!pluginList) return;
25
+
26
+ const paths = pluginList
27
+ .split(',')
28
+ .map((p) => p.trim())
29
+ .filter(Boolean);
30
+
31
+ for (const modulePath of paths) {
32
+ try {
33
+ const mod = await import(modulePath);
34
+ const plugin: AWMPlugin = mod.default ?? mod;
35
+
36
+ if (!plugin.name || typeof plugin.register !== 'function') {
37
+ console.warn(` [plugin] Skipping ${modulePath}: missing name or register()`);
38
+ continue;
39
+ }
40
+
41
+ await plugin.register(ctx);
42
+ loadedPlugins.push(plugin);
43
+ console.log(` [plugin] Loaded: ${plugin.name}`);
44
+ } catch (err) {
45
+ console.error(` [plugin] Failed to load ${modulePath}:`, (err as Error).message);
46
+ }
47
+ }
48
+ }
49
+
50
+ /** Teardown all loaded plugins (call on shutdown). */
51
+ export async function teardownPlugins(): Promise<void> {
52
+ for (const plugin of loadedPlugins) {
53
+ try {
54
+ await plugin.teardown?.();
55
+ } catch (err) {
56
+ console.error(` [plugin] Teardown error (${plugin.name}):`, (err as Error).message);
57
+ }
58
+ }
59
+ loadedPlugins.length = 0;
60
+ }
@@ -0,0 +1,44 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * AWM Plugin contract.
5
+ *
6
+ * Plugins extend coordination with custom behavior by subscribing to
7
+ * events, adding routes, or querying the DB. They are loaded at startup
8
+ * via AWM_PLUGINS env var (comma-separated module paths).
9
+ *
10
+ * Example plugin:
11
+ * import type { AWMPlugin } from 'agent-working-memory';
12
+ * export default {
13
+ * name: 'my-plugin',
14
+ * register(ctx) {
15
+ * ctx.events.on('assignment.completed', (evt) => {
16
+ * console.log(`Task done: ${evt.assignmentId}`);
17
+ * });
18
+ * },
19
+ * } satisfies AWMPlugin;
20
+ */
21
+
22
+ import type { FastifyInstance } from 'fastify';
23
+ import type Database from 'better-sqlite3';
24
+ import type { CoordinationEventBus } from './events.js';
25
+
26
+ /** Context passed to plugin register(). */
27
+ export interface AWMPluginContext {
28
+ /** Typed event bus — subscribe to coordination events. */
29
+ events: CoordinationEventBus;
30
+ /** Raw better-sqlite3 database handle (coordination tables). */
31
+ db: Database.Database;
32
+ /** Fastify instance — add custom routes if needed. */
33
+ fastify: FastifyInstance;
34
+ }
35
+
36
+ /** Plugin contract. Every AWM plugin must export this shape. */
37
+ export interface AWMPlugin {
38
+ /** Unique plugin name (for logging and dedup). */
39
+ name: string;
40
+ /** Called once at startup with the plugin context. */
41
+ register(ctx: AWMPluginContext): void | Promise<void>;
42
+ /** Optional cleanup on shutdown. */
43
+ teardown?(): void | Promise<void>;
44
+ }