agent-working-memory 0.6.0 → 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 (122) hide show
  1. package/README.md +15 -9
  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/cli.js +104 -230
  31. package/dist/cli.js.map +1 -1
  32. package/dist/coordination/events.d.ts +59 -0
  33. package/dist/coordination/events.d.ts.map +1 -0
  34. package/dist/coordination/events.js +28 -0
  35. package/dist/coordination/events.js.map +1 -0
  36. package/dist/coordination/index.d.ts +10 -1
  37. package/dist/coordination/index.d.ts.map +1 -1
  38. package/dist/coordination/index.js +87 -3
  39. package/dist/coordination/index.js.map +1 -1
  40. package/dist/coordination/peer-decisions.d.ts +40 -0
  41. package/dist/coordination/peer-decisions.d.ts.map +1 -0
  42. package/dist/coordination/peer-decisions.js +82 -0
  43. package/dist/coordination/peer-decisions.js.map +1 -0
  44. package/dist/coordination/plugin-loader.d.ts +18 -0
  45. package/dist/coordination/plugin-loader.d.ts.map +1 -0
  46. package/dist/coordination/plugin-loader.js +55 -0
  47. package/dist/coordination/plugin-loader.js.map +1 -0
  48. package/dist/coordination/plugin.d.ts +40 -0
  49. package/dist/coordination/plugin.d.ts.map +1 -0
  50. package/dist/coordination/plugin.js +22 -0
  51. package/dist/coordination/plugin.js.map +1 -0
  52. package/dist/coordination/routes.d.ts +2 -1
  53. package/dist/coordination/routes.d.ts.map +1 -1
  54. package/dist/coordination/routes.js +899 -76
  55. package/dist/coordination/routes.js.map +1 -1
  56. package/dist/coordination/schema.d.ts.map +1 -1
  57. package/dist/coordination/schema.js +72 -14
  58. package/dist/coordination/schema.js.map +1 -1
  59. package/dist/coordination/schemas.d.ts +84 -3
  60. package/dist/coordination/schemas.d.ts.map +1 -1
  61. package/dist/coordination/schemas.js +71 -1
  62. package/dist/coordination/schemas.js.map +1 -1
  63. package/dist/coordination/stale.d.ts.map +1 -1
  64. package/dist/coordination/stale.js +2 -1
  65. package/dist/coordination/stale.js.map +1 -1
  66. package/dist/coordination/types.d.ts +252 -0
  67. package/dist/coordination/types.d.ts.map +1 -0
  68. package/dist/coordination/types.js +8 -0
  69. package/dist/coordination/types.js.map +1 -0
  70. package/dist/coordination/write-mutex.d.ts +26 -0
  71. package/dist/coordination/write-mutex.d.ts.map +1 -0
  72. package/dist/coordination/write-mutex.js +63 -0
  73. package/dist/coordination/write-mutex.js.map +1 -0
  74. package/dist/core/embeddings.d.ts +2 -0
  75. package/dist/core/embeddings.d.ts.map +1 -1
  76. package/dist/core/embeddings.js +4 -0
  77. package/dist/core/embeddings.js.map +1 -1
  78. package/dist/engine/activation.d.ts.map +1 -1
  79. package/dist/engine/activation.js +16 -3
  80. package/dist/engine/activation.js.map +1 -1
  81. package/dist/engine/consolidation.d.ts.map +1 -1
  82. package/dist/engine/consolidation.js +15 -6
  83. package/dist/engine/consolidation.js.map +1 -1
  84. package/dist/engine/retraction.d.ts +3 -1
  85. package/dist/engine/retraction.d.ts.map +1 -1
  86. package/dist/engine/retraction.js +19 -6
  87. package/dist/engine/retraction.js.map +1 -1
  88. package/dist/index.js +6 -18
  89. package/dist/index.js.map +1 -1
  90. package/dist/mcp.js +52 -3
  91. package/dist/mcp.js.map +1 -1
  92. package/dist/storage/sqlite.d.ts +6 -1
  93. package/dist/storage/sqlite.d.ts.map +1 -1
  94. package/dist/storage/sqlite.js +39 -3
  95. package/dist/storage/sqlite.js.map +1 -1
  96. package/package.json +1 -1
  97. package/src/adapters/claude-code.ts +234 -0
  98. package/src/adapters/codex.ts +262 -0
  99. package/src/adapters/common.ts +172 -0
  100. package/src/adapters/cursor.ts +150 -0
  101. package/src/adapters/http.ts +100 -0
  102. package/src/adapters/index.ts +31 -0
  103. package/src/adapters/types.ts +75 -0
  104. package/src/cli.ts +107 -238
  105. package/src/coordination/events.ts +90 -0
  106. package/src/coordination/index.ts +102 -3
  107. package/src/coordination/peer-decisions.ts +105 -0
  108. package/src/coordination/plugin-loader.ts +60 -0
  109. package/src/coordination/plugin.ts +44 -0
  110. package/src/coordination/routes.ts +1176 -105
  111. package/src/coordination/schema.ts +67 -14
  112. package/src/coordination/schemas.ts +85 -1
  113. package/src/coordination/stale.ts +3 -2
  114. package/src/coordination/types.ts +311 -0
  115. package/src/coordination/write-mutex.ts +69 -0
  116. package/src/core/embeddings.ts +5 -0
  117. package/src/engine/activation.ts +13 -3
  118. package/src/engine/consolidation.ts +15 -6
  119. package/src/engine/retraction.ts +22 -6
  120. package/src/index.ts +6 -15
  121. package/src/mcp.ts +73 -9
  122. package/src/storage/sqlite.ts +39 -3
@@ -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
+ }