agent-working-memory 0.11.0 → 0.12.3

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 (115) hide show
  1. package/README.md +71 -297
  2. package/dist/adapters/claude-code.d.ts.map +1 -1
  3. package/dist/adapters/claude-code.js +63 -3
  4. package/dist/adapters/claude-code.js.map +1 -1
  5. package/dist/adapters/common.d.ts.map +1 -1
  6. package/dist/adapters/common.js +358 -306
  7. package/dist/adapters/common.js.map +1 -1
  8. package/dist/api/routes.d.ts.map +1 -1
  9. package/dist/api/routes.js +29 -7
  10. package/dist/api/routes.js.map +1 -1
  11. package/dist/coordination/routes.d.ts.map +1 -1
  12. package/dist/coordination/routes.js +174 -170
  13. package/dist/coordination/routes.js.map +1 -1
  14. package/dist/core/embeddings.d.ts.map +1 -1
  15. package/dist/core/embeddings.js +4 -1
  16. package/dist/core/embeddings.js.map +1 -1
  17. package/dist/core/entity-extract.d.ts +3 -0
  18. package/dist/core/entity-extract.d.ts.map +1 -0
  19. package/dist/core/entity-extract.js +47 -0
  20. package/dist/core/entity-extract.js.map +1 -0
  21. package/dist/core/format-recall.d.ts +16 -0
  22. package/dist/core/format-recall.d.ts.map +1 -0
  23. package/dist/core/format-recall.js +24 -0
  24. package/dist/core/format-recall.js.map +1 -0
  25. package/dist/core/query-expander.js +1 -1
  26. package/dist/core/query-expander.js.map +1 -1
  27. package/dist/core/reranker.js +1 -1
  28. package/dist/core/reranker.js.map +1 -1
  29. package/dist/core/salience.d.ts.map +1 -1
  30. package/dist/core/salience.js +14 -2
  31. package/dist/core/salience.js.map +1 -1
  32. package/dist/core/whoami.d.ts +24 -0
  33. package/dist/core/whoami.d.ts.map +1 -0
  34. package/dist/core/whoami.js +66 -0
  35. package/dist/core/whoami.js.map +1 -0
  36. package/dist/core/write-pipeline.d.ts +9 -0
  37. package/dist/core/write-pipeline.d.ts.map +1 -1
  38. package/dist/core/write-pipeline.js +109 -68
  39. package/dist/core/write-pipeline.js.map +1 -1
  40. package/dist/core/write-telemetry.d.ts +33 -0
  41. package/dist/core/write-telemetry.d.ts.map +1 -0
  42. package/dist/core/write-telemetry.js +110 -0
  43. package/dist/core/write-telemetry.js.map +1 -0
  44. package/dist/engine/activation.d.ts +22 -12
  45. package/dist/engine/activation.d.ts.map +1 -1
  46. package/dist/engine/activation.js +133 -17
  47. package/dist/engine/activation.js.map +1 -1
  48. package/dist/engine/consolidation-scheduler.d.ts +1 -1
  49. package/dist/engine/consolidation-scheduler.js +1 -1
  50. package/dist/engine/consolidation.d.ts +1 -0
  51. package/dist/engine/consolidation.d.ts.map +1 -1
  52. package/dist/engine/consolidation.js +18 -0
  53. package/dist/engine/consolidation.js.map +1 -1
  54. package/dist/engine/eval.d.ts.map +1 -1
  55. package/dist/engine/eval.js +5 -1
  56. package/dist/engine/eval.js.map +1 -1
  57. package/dist/hooks/sidecar.d.ts +26 -0
  58. package/dist/hooks/sidecar.d.ts.map +1 -1
  59. package/dist/hooks/sidecar.js +30 -0
  60. package/dist/hooks/sidecar.js.map +1 -1
  61. package/dist/index.js +20 -2
  62. package/dist/index.js.map +1 -1
  63. package/dist/mcp.d.ts +2 -1
  64. package/dist/mcp.d.ts.map +1 -1
  65. package/dist/mcp.js +222 -108
  66. package/dist/mcp.js.map +1 -1
  67. package/dist/recipes/index.d.ts +57 -0
  68. package/dist/recipes/index.d.ts.map +1 -0
  69. package/dist/recipes/index.js +81 -0
  70. package/dist/recipes/index.js.map +1 -0
  71. package/dist/storage/pglite-schema.d.ts.map +1 -1
  72. package/dist/storage/pglite-schema.js +27 -0
  73. package/dist/storage/pglite-schema.js.map +1 -1
  74. package/dist/storage/pglite.d.ts +5 -0
  75. package/dist/storage/pglite.d.ts.map +1 -1
  76. package/dist/storage/pglite.js +180 -138
  77. package/dist/storage/pglite.js.map +1 -1
  78. package/dist/storage/postgres.d.ts +5 -0
  79. package/dist/storage/postgres.d.ts.map +1 -1
  80. package/dist/storage/postgres.js +180 -138
  81. package/dist/storage/postgres.js.map +1 -1
  82. package/dist/storage/sqlite.d.ts +9 -0
  83. package/dist/storage/sqlite.d.ts.map +1 -1
  84. package/dist/storage/sqlite.js +394 -326
  85. package/dist/storage/sqlite.js.map +1 -1
  86. package/dist/types/engram.d.ts +14 -0
  87. package/dist/types/engram.d.ts.map +1 -1
  88. package/dist/types/engram.js.map +1 -1
  89. package/package.json +1 -1
  90. package/src/adapters/claude-code.ts +66 -3
  91. package/src/adapters/common.ts +567 -515
  92. package/src/api/routes.ts +999 -971
  93. package/src/coordination/routes.ts +2155 -2150
  94. package/src/core/embeddings.ts +4 -1
  95. package/src/core/entity-extract.ts +47 -0
  96. package/src/core/format-recall.ts +25 -0
  97. package/src/core/query-expander.ts +1 -1
  98. package/src/core/reranker.ts +1 -1
  99. package/src/core/salience.ts +529 -514
  100. package/src/core/whoami.ts +92 -0
  101. package/src/core/write-pipeline.ts +60 -8
  102. package/src/core/write-telemetry.ts +131 -0
  103. package/src/engine/activation.ts +1468 -1369
  104. package/src/engine/consolidation-scheduler.ts +1 -1
  105. package/src/engine/consolidation.ts +887 -869
  106. package/src/engine/eval.ts +6 -1
  107. package/src/hooks/sidecar.ts +55 -0
  108. package/src/index.ts +248 -227
  109. package/src/mcp.ts +1387 -1270
  110. package/src/recipes/index.ts +125 -0
  111. package/src/storage/pglite-schema.ts +27 -0
  112. package/src/storage/pglite.ts +1420 -1372
  113. package/src/storage/postgres.ts +1523 -1475
  114. package/src/storage/sqlite.ts +1936 -1861
  115. package/src/types/engram.ts +22 -0
@@ -86,7 +86,12 @@ export class EvalEngine {
86
86
  promotedCount: stagingMetrics.promoted,
87
87
  discardedCount: stagingMetrics.discarded,
88
88
  promotionPrecision,
89
- discardRegret: 0, // Requires tracking discarded-then-rediscovered items
89
+ // D12 (2026-07-30): discarded writes persist as 'low-salience'-tagged
90
+ // engrams (confidence 0.25). Regret = the filter demoted something the
91
+ // agent later actually needed — measured as low-salience engrams that
92
+ // were subsequently accessed at least once.
93
+ discardRegret: activeEngrams.filter(e =>
94
+ e.tags.includes('low-salience') && e.accessCount > 0).length,
90
95
 
91
96
  activeEngramCount: activeEngrams.length,
92
97
  stagingEngramCount: stagingEngrams.length,
@@ -28,6 +28,28 @@ export interface SidecarDeps {
28
28
  secret: string | null;
29
29
  port: number;
30
30
  onConsolidate?: (agentId: string, reason: string) => void;
31
+ /**
32
+ * 0.12.2: warm recall for hooks. The sidecar runs in the same process as
33
+ * the MCP server's activation engine and its loaded ML models, so exposing
34
+ * recall here gives Claude Code hooks (e.g. a UserPromptSubmit PRIME hook)
35
+ * warm-latency recall with no standing server — the process lifecycle is
36
+ * owned by the session that spawned it. Optional so older callers and
37
+ * tests keep working unchanged.
38
+ */
39
+ activate?: (q: {
40
+ context: string;
41
+ limit?: number;
42
+ requireConfidence?: number;
43
+ granularity?: 'full' | 'compact' | 'auto';
44
+ }) => Promise<Array<{
45
+ engram: {
46
+ id: string; concept: string; content: string;
47
+ createdAt?: string; memoryClass?: string; validTo?: string | null;
48
+ };
49
+ score: number;
50
+ summary?: string;
51
+ confidence?: number;
52
+ }>>;
31
53
  }
32
54
 
33
55
  interface HookInput {
@@ -187,6 +209,39 @@ export function startSidecar(deps: SidecarDeps): { close: () => void } {
187
209
  return;
188
210
  }
189
211
 
212
+ // POST /memory/activate — warm recall for hooks (0.12.2). Behind the auth
213
+ // gate above. Returns a trimmed subset of the HTTP API's response shape
214
+ // ({results: [{engram, score, summary, confidence}]}) so a hook written
215
+ // against either endpoint needs no branching.
216
+ if (req.url === '/memory/activate' && req.method === 'POST') {
217
+ if (!deps.activate) {
218
+ json(res, 501, { error: 'activate not wired on this sidecar' });
219
+ return;
220
+ }
221
+ try {
222
+ const body = JSON.parse((await readBody(req)) || '{}') as {
223
+ context?: string; query?: string; limit?: number;
224
+ requireConfidence?: number; granularity?: 'full' | 'compact' | 'auto';
225
+ };
226
+ const context = body.context ?? body.query;
227
+ if (!context) {
228
+ json(res, 400, { error: 'context (or query) is required' });
229
+ return;
230
+ }
231
+ const results = await deps.activate({
232
+ context,
233
+ limit: body.limit,
234
+ requireConfidence: body.requireConfidence,
235
+ granularity: body.granularity,
236
+ });
237
+ log(agentId, 'hook:recall', `"${context.slice(0, 80)}" → ${results.length} results (sidecar)`);
238
+ json(res, 200, { results });
239
+ } catch (err) {
240
+ json(res, 500, { error: (err as Error).message });
241
+ }
242
+ return;
243
+ }
244
+
190
245
  // POST /hooks/checkpoint — auto-checkpoint from hook events
191
246
  if (req.url === '/hooks/checkpoint' && req.method === 'POST') {
192
247
  try {
package/src/index.ts CHANGED
@@ -1,227 +1,248 @@
1
- // Copyright 2026 Robert Winter / Complete Ideas
2
- // SPDX-License-Identifier: Apache-2.0
3
- import { readFileSync, copyFileSync, existsSync, mkdirSync, readdirSync, unlinkSync } from 'node:fs';
4
- import { resolve, dirname, basename } from 'node:path';
5
- import Fastify from 'fastify';
6
- import { VERSION } from './version.js';
7
-
8
- // Load .env file if present (no external dependency)
9
- try {
10
- const envPath = resolve(process.cwd(), '.env');
11
- const envContent = readFileSync(envPath, 'utf-8');
12
- for (const line of envContent.split('\n')) {
13
- const trimmed = line.trim();
14
- if (!trimmed || trimmed.startsWith('#')) continue;
15
- const eqIdx = trimmed.indexOf('=');
16
- if (eqIdx === -1) continue;
17
- const key = trimmed.slice(0, eqIdx).trim();
18
- const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
19
- if (!process.env[key]) process.env[key] = val; // Don't override existing env
20
- }
21
- } catch { /* No .env file — that's fine */ }
22
- import { EngramStore } from './storage/sqlite.js';
23
- import { ActivationEngine } from './engine/activation.js';
24
- import { ConnectionEngine } from './engine/connections.js';
25
- import { StagingBuffer } from './engine/staging.js';
26
- import { EvictionEngine } from './engine/eviction.js';
27
- import { RetractionEngine } from './engine/retraction.js';
28
- import { EvalEngine } from './engine/eval.js';
29
- import { ConsolidationEngine } from './engine/consolidation.js';
30
- import { ConsolidationScheduler } from './engine/consolidation-scheduler.js';
31
- import { registerRoutes } from './api/routes.js';
32
- import { DEFAULT_AGENT_CONFIG } from './types/agent.js';
33
- import { getEmbedder } from './core/embeddings.js';
34
- import { getReranker } from './core/reranker.js';
35
- import { getExpander } from './core/query-expander.js';
36
- import { initLogger } from './core/logger.js';
37
- import { openStore, getConfiguredBackend, type StoreBackend } from './storage/factory.js';
38
- import type { IEngramStore } from './storage/store.js';
39
-
40
- const PORT = parseInt(process.env.AWM_PORT ?? '8400', 10);
41
- const BACKEND: StoreBackend = getConfiguredBackend();
42
- const DB_PATH = process.env.AWM_DB_PATH ?? (BACKEND === 'pglite' ? 'memory-pglite' : 'memory.db');
43
- const API_KEY = process.env.AWM_API_KEY ?? null;
44
-
45
- async function main() {
46
- // Auto-backup: copy DB to backups/ on startup (SQLite-only; PGlite is a
47
- // directory and is backed up by snapshot/replication at the OS level).
48
- if (BACKEND === 'sqlite' && existsSync(DB_PATH)) {
49
- const dbDir = dirname(resolve(DB_PATH));
50
- const backupDir = resolve(dbDir, 'backups');
51
- mkdirSync(backupDir, { recursive: true });
52
- const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
53
- const backupPath = resolve(backupDir, `${basename(DB_PATH, '.db')}-${ts}.db`);
54
- try {
55
- copyFileSync(resolve(DB_PATH), backupPath);
56
- console.log(`Backup: ${backupPath}`);
57
- } catch (err) {
58
- console.log(`Backup skipped: ${(err as Error).message}`);
59
- }
60
- }
61
-
62
- // Logger write activity to awm.log alongside the DB
63
- initLogger(DB_PATH);
64
-
65
- // Storage backend-agnostic open via the factory.
66
- // Some routes (export, coordination health) are still SQLite-only and
67
- // gracefully return 501 / skip when running on PGlite.
68
- const { store: storeAny } = await openStore();
69
- const store = storeAny as unknown as EngramStore;
70
-
71
- // Integrity check — SQLite-only. PGlite has its own consistency model.
72
- const integrity = BACKEND === 'sqlite'
73
- ? store.integrityCheck()
74
- : { ok: true, result: 'pglite: skipped' };
75
- if (!integrity.ok) {
76
- console.error(`DB integrity check FAILED: ${integrity.result}`);
77
- // Close corrupt DB, restore from backup, and exit for process manager to restart
78
- store.close();
79
- const dbDir = dirname(resolve(DB_PATH));
80
- const backupDir = resolve(dbDir, 'backups');
81
- if (existsSync(backupDir)) {
82
- const backups = readdirSync(backupDir)
83
- .filter(f => f.endsWith('.db'))
84
- .sort()
85
- .reverse();
86
- if (backups.length > 0) {
87
- const restorePath = resolve(backupDir, backups[0]);
88
- console.error(`Attempting restore from: ${restorePath}`);
89
- try {
90
- copyFileSync(restorePath, resolve(DB_PATH));
91
- console.error('Restore complete exiting for restart with restored DB');
92
- process.exit(1);
93
- } catch (restoreErr) {
94
- console.error(`Restore failed: ${(restoreErr as Error).message}`);
95
- }
96
- }
97
- }
98
- console.error('No backup available — continuing with potentially corrupt DB');
99
- } else {
100
- console.log(' DB integrity check: ok');
101
- }
102
-
103
- // Engines
104
- const activationEngine = new ActivationEngine(store);
105
- const connectionEngine = new ConnectionEngine(store, activationEngine);
106
- const stagingBuffer = new StagingBuffer(store, activationEngine);
107
- const evictionEngine = new EvictionEngine(store);
108
- const retractionEngine = new RetractionEngine(store);
109
- const evalEngine = new EvalEngine(store);
110
- const consolidationEngine = new ConsolidationEngine(store, connectionEngine);
111
- const consolidationScheduler = new ConsolidationScheduler(store, consolidationEngine);
112
-
113
- // API — disable Fastify's default request logging (too noisy for hive polling)
114
- // bodyLimit: 512KB to prevent Content-Length mismatch errors with large task payloads
115
- const app = Fastify({ logger: false, bodyLimit: 512_000 });
116
-
117
- // Bearer token auth — only enforced when AWM_API_KEY is explicitly set and non-empty
118
- if (API_KEY && API_KEY !== 'NONE' && API_KEY.length > 1) {
119
- app.addHook('onRequest', async (req, reply) => {
120
- if (req.url === '/health') return; // Health check is always public
121
- const bearer = req.headers.authorization;
122
- const xApiKey = req.headers['x-api-key'] as string | undefined;
123
- if (bearer === `Bearer ${API_KEY}` || xApiKey === API_KEY) return;
124
- reply.code(401).send({ error: 'Unauthorized' });
125
- });
126
- console.log('API key auth enabled (AWM_API_KEY set)');
127
- }
128
-
129
- registerRoutes(app, {
130
- store, activationEngine, connectionEngine,
131
- evictionEngine, retractionEngine, evalEngine,
132
- consolidationEngine, consolidationScheduler,
133
- });
134
-
135
- // Coordination module (opt-in via AWM_COORDINATION=true) SQLite-only.
136
- const { isCoordinationEnabled, initCoordination, stopCoordinationCleanup } = await import('./coordination/index.js');
137
- if (isCoordinationEnabled() && BACKEND === 'sqlite') {
138
- initCoordination(app, store.getDb(), store);
139
- } else if (isCoordinationEnabled()) {
140
- console.log(` Coordination requested but disabled requires SQLite backend (current: ${BACKEND})`);
141
- } else {
142
- console.log(' Coordination module disabled (set AWM_COORDINATION=true to enable)');
143
- }
144
-
145
- // Background tasks
146
- stagingBuffer.start(DEFAULT_AGENT_CONFIG.stagingTtlMs);
147
- consolidationScheduler.start();
148
-
149
- // Periodic hot backup every 10 minutes (keep last 6 = 1hr coverage)
150
- const dbDir = dirname(resolve(DB_PATH));
151
- const backupDir = resolve(dbDir, 'backups');
152
- mkdirSync(backupDir, { recursive: true });
153
-
154
- // Cleanup old backups on startup (older than 2 hours)
155
- try {
156
- const TWO_HOURS_MS = 2 * 60 * 60 * 1000;
157
- const now = Date.now();
158
- for (const f of readdirSync(backupDir).filter(f => f.endsWith('.db'))) {
159
- const match = f.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})/);
160
- if (match) {
161
- const fileDate = new Date(`${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}:${match[6]}Z`);
162
- if (now - fileDate.getTime() > TWO_HOURS_MS) {
163
- unlinkSync(resolve(backupDir, f));
164
- }
165
- }
166
- }
167
- } catch { /* cleanup is non-fatal */ }
168
-
169
- // Periodic hot backup — SQLite-only. PGlite directory backups are an OS-level concern.
170
- const backupTimer: ReturnType<typeof setInterval> | null = BACKEND === 'sqlite' ? setInterval(() => {
171
- try {
172
- const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
173
- const backupPath = resolve(backupDir, `${basename(DB_PATH, '.db')}-${ts}.db`);
174
- store.backup(backupPath);
175
- // Prune: keep only last 6 backups
176
- const backups = readdirSync(backupDir).filter(f => f.endsWith('.db')).sort();
177
- while (backups.length > 6) {
178
- const old = backups.shift()!;
179
- try { unlinkSync(resolve(backupDir, old)); } catch { /* non-fatal */ }
180
- }
181
- } catch (err) {
182
- console.warn(`[backup] failed: ${(err as Error).message}`);
183
- }
184
- }, 10 * 60_000) : null;
185
-
186
- // Pre-load ML models (downloads on first run: embeddings ~22MB, reranker ~22MB, expander ~80MB)
187
- getEmbedder().catch(err => console.warn('Embedding model unavailable:', err.message));
188
- getReranker().catch(err => console.warn('Reranker model unavailable:', err.message));
189
- getExpander().catch(err => console.warn('Query expander model unavailable:', err.message));
190
-
191
- if (BACKEND === 'sqlite') {
192
- setImmediate(() => {
193
- try {
194
- const t0 = Date.now();
195
- store.warmSlimCache();
196
- const stats = store.getSlimCacheStats();
197
- console.log(` Slim cache warmed: ${stats.size} entries in ${Date.now() - t0}ms`);
198
- } catch (err) {
199
- console.warn(` Slim cache warm failed: ${(err as Error).message}`);
200
- }
201
- });
202
- }
203
-
204
- // Start server
205
- await app.listen({ port: PORT, host: '0.0.0.0' });
206
- console.log(`AgentWorkingMemory v${VERSION} listening on port ${PORT}`);
207
-
208
- // Graceful shutdown
209
- const shutdown = async () => {
210
- if (backupTimer) clearInterval(backupTimer);
211
- await stopCoordinationCleanup();
212
- consolidationScheduler.stop();
213
- stagingBuffer.stop();
214
- if (BACKEND === 'sqlite') {
215
- try { store.walCheckpoint(); } catch { /* non-fatal */ }
216
- }
217
- try { await (store as any).close?.(); } catch { /* best-effort */ }
218
- process.exit(0);
219
- };
220
- process.on('SIGINT', shutdown);
221
- process.on('SIGTERM', shutdown);
222
- }
223
-
224
- main().catch(err => {
225
- console.error('Failed to start:', err);
226
- process.exit(1);
227
- });
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { readFileSync, copyFileSync, existsSync, mkdirSync, readdirSync, unlinkSync } from 'node:fs';
4
+ import { resolve, dirname, basename } from 'node:path';
5
+ import Fastify from 'fastify';
6
+ import { VERSION } from './version.js';
7
+
8
+ // Load .env file if present (no external dependency)
9
+ try {
10
+ const envPath = resolve(process.cwd(), '.env');
11
+ const envContent = readFileSync(envPath, 'utf-8');
12
+ for (const line of envContent.split('\n')) {
13
+ const trimmed = line.trim();
14
+ if (!trimmed || trimmed.startsWith('#')) continue;
15
+ const eqIdx = trimmed.indexOf('=');
16
+ if (eqIdx === -1) continue;
17
+ const key = trimmed.slice(0, eqIdx).trim();
18
+ const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, '');
19
+ if (!process.env[key]) process.env[key] = val; // Don't override existing env
20
+ }
21
+ } catch { /* No .env file — that's fine */ }
22
+ import { EngramStore } from './storage/sqlite.js';
23
+ import { ActivationEngine } from './engine/activation.js';
24
+ import { ConnectionEngine } from './engine/connections.js';
25
+ import { StagingBuffer } from './engine/staging.js';
26
+ import { startLoopLagMonitor } from './core/write-telemetry.js';
27
+ import { EvictionEngine } from './engine/eviction.js';
28
+ import { RetractionEngine } from './engine/retraction.js';
29
+ import { EvalEngine } from './engine/eval.js';
30
+ import { ConsolidationEngine } from './engine/consolidation.js';
31
+ import { ConsolidationScheduler } from './engine/consolidation-scheduler.js';
32
+ import { registerRoutes } from './api/routes.js';
33
+ import { DEFAULT_AGENT_CONFIG } from './types/agent.js';
34
+ import { getEmbedder } from './core/embeddings.js';
35
+ import { getReranker } from './core/reranker.js';
36
+ import { getExpander } from './core/query-expander.js';
37
+ import { initLogger } from './core/logger.js';
38
+ import { openStore, getConfiguredBackend, type StoreBackend } from './storage/factory.js';
39
+ import type { IEngramStore } from './storage/store.js';
40
+
41
+ const PORT = parseInt(process.env.AWM_PORT ?? '8400', 10);
42
+ const BACKEND: StoreBackend = getConfiguredBackend();
43
+ const DB_PATH = process.env.AWM_DB_PATH ?? (BACKEND === 'pglite' ? 'memory-pglite' : 'memory.db');
44
+ const API_KEY = process.env.AWM_API_KEY ?? null;
45
+
46
+ // D2 (2026-07-30) security defaults: bind loopback unless AWM_BIND widens it.
47
+ // Widening without an API key is refused (fail-closed) a local-first store
48
+ // on hotel Wi-Fi must not be one env var away from an open network listener.
49
+ // Escape hatch for trusted networks: AWM_ALLOW_INSECURE=1.
50
+ const HOST = process.env.AWM_BIND ?? '127.0.0.1';
51
+ const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1']);
52
+ if (!LOOPBACK_HOSTS.has(HOST)
53
+ && !(API_KEY && API_KEY !== 'NONE' && API_KEY.length > 1)
54
+ && process.env.AWM_ALLOW_INSECURE !== '1') {
55
+ console.error(
56
+ `AWM refusing to start: AWM_BIND=${HOST} exposes the API beyond loopback with no AWM_API_KEY set. ` +
57
+ 'Set AWM_API_KEY, or AWM_ALLOW_INSECURE=1 to override on a trusted network.',
58
+ );
59
+ process.exit(1);
60
+ }
61
+
62
+ async function main() {
63
+ // Auto-backup: copy DB to backups/ on startup (SQLite-only; PGlite is a
64
+ // directory and is backed up by snapshot/replication at the OS level).
65
+ if (BACKEND === 'sqlite' && existsSync(DB_PATH)) {
66
+ const dbDir = dirname(resolve(DB_PATH));
67
+ const backupDir = resolve(dbDir, 'backups');
68
+ mkdirSync(backupDir, { recursive: true });
69
+ const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
70
+ const backupPath = resolve(backupDir, `${basename(DB_PATH, '.db')}-${ts}.db`);
71
+ try {
72
+ copyFileSync(resolve(DB_PATH), backupPath);
73
+ console.log(`Backup: ${backupPath}`);
74
+ } catch (err) {
75
+ console.log(`Backup skipped: ${(err as Error).message}`);
76
+ }
77
+ }
78
+
79
+ // Logger write activity to awm.log alongside the DB
80
+ initLogger(DB_PATH);
81
+
82
+ // Storage backend-agnostic open via the factory.
83
+ // Some routes (export, coordination health) are still SQLite-only and
84
+ // gracefully return 501 / skip when running on PGlite.
85
+ const { store: storeAny } = await openStore();
86
+ const store = storeAny as unknown as EngramStore;
87
+
88
+ // Integrity check — SQLite-only. PGlite has its own consistency model.
89
+ const integrity = BACKEND === 'sqlite'
90
+ ? store.integrityCheck()
91
+ : { ok: true, result: 'pglite: skipped' };
92
+ if (!integrity.ok) {
93
+ console.error(`DB integrity check FAILED: ${integrity.result}`);
94
+ // Close corrupt DB, restore from backup, and exit for process manager to restart
95
+ store.close();
96
+ const dbDir = dirname(resolve(DB_PATH));
97
+ const backupDir = resolve(dbDir, 'backups');
98
+ if (existsSync(backupDir)) {
99
+ const backups = readdirSync(backupDir)
100
+ .filter(f => f.endsWith('.db'))
101
+ .sort()
102
+ .reverse();
103
+ if (backups.length > 0) {
104
+ const restorePath = resolve(backupDir, backups[0]);
105
+ console.error(`Attempting restore from: ${restorePath}`);
106
+ try {
107
+ copyFileSync(restorePath, resolve(DB_PATH));
108
+ console.error('Restore complete exiting for restart with restored DB');
109
+ process.exit(1);
110
+ } catch (restoreErr) {
111
+ console.error(`Restore failed: ${(restoreErr as Error).message}`);
112
+ }
113
+ }
114
+ }
115
+ console.error('No backup available continuing with potentially corrupt DB');
116
+ } else {
117
+ console.log(' DB integrity check: ok');
118
+ }
119
+
120
+ // Engines
121
+ const activationEngine = new ActivationEngine(store);
122
+ const connectionEngine = new ConnectionEngine(store, activationEngine);
123
+ const stagingBuffer = new StagingBuffer(store, activationEngine);
124
+ const evictionEngine = new EvictionEngine(store);
125
+ const retractionEngine = new RetractionEngine(store);
126
+ const evalEngine = new EvalEngine(store);
127
+ const consolidationEngine = new ConsolidationEngine(store, connectionEngine);
128
+ const consolidationScheduler = new ConsolidationScheduler(store, consolidationEngine);
129
+
130
+ // API — disable Fastify's default request logging (too noisy for hive polling)
131
+ // bodyLimit: 512KB to prevent Content-Length mismatch errors with large task payloads
132
+ const app = Fastify({ logger: false, bodyLimit: 512_000 });
133
+
134
+ // Bearer token auth — only enforced when AWM_API_KEY is explicitly set and non-empty
135
+ if (API_KEY && API_KEY !== 'NONE' && API_KEY.length > 1) {
136
+ app.addHook('onRequest', async (req, reply) => {
137
+ if (req.url === '/health') return; // Health check is always public
138
+ const bearer = req.headers.authorization;
139
+ const xApiKey = req.headers['x-api-key'] as string | undefined;
140
+ if (bearer === `Bearer ${API_KEY}` || xApiKey === API_KEY) return;
141
+ reply.code(401).send({ error: 'Unauthorized' });
142
+ });
143
+ console.log('API key auth enabled (AWM_API_KEY set)');
144
+ }
145
+
146
+ registerRoutes(app, {
147
+ store, activationEngine, connectionEngine,
148
+ evictionEngine, retractionEngine, evalEngine,
149
+ consolidationEngine, consolidationScheduler,
150
+ });
151
+
152
+ // Coordination module (opt-in via AWM_COORDINATION=true) — SQLite-only.
153
+ const { isCoordinationEnabled, initCoordination, stopCoordinationCleanup } = await import('./coordination/index.js');
154
+ if (isCoordinationEnabled() && BACKEND === 'sqlite') {
155
+ initCoordination(app, store.getDb(), store);
156
+ if (process.env.AWM_COORD_REQUIRE_TOKENS !== '1') {
157
+ console.log(' Coordination: session tokens optional (set AWM_COORD_REQUIRE_TOKENS=1 to require them)');
158
+ }
159
+ } else if (isCoordinationEnabled()) {
160
+ console.log(` Coordination requested but disabled — requires SQLite backend (current: ${BACKEND})`);
161
+ } else {
162
+ console.log(' Coordination module disabled (set AWM_COORDINATION=true to enable)');
163
+ }
164
+
165
+ // Background tasks
166
+ stagingBuffer.start(DEFAULT_AGENT_CONFIG.stagingTtlMs);
167
+ consolidationScheduler.start();
168
+ startLoopLagMonitor();
169
+
170
+ // Periodic hot backup every 10 minutes (keep last 6 = 1hr coverage)
171
+ const dbDir = dirname(resolve(DB_PATH));
172
+ const backupDir = resolve(dbDir, 'backups');
173
+ mkdirSync(backupDir, { recursive: true });
174
+
175
+ // Cleanup old backups on startup (older than 2 hours)
176
+ try {
177
+ const TWO_HOURS_MS = 2 * 60 * 60 * 1000;
178
+ const now = Date.now();
179
+ for (const f of readdirSync(backupDir).filter(f => f.endsWith('.db'))) {
180
+ const match = f.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})/);
181
+ if (match) {
182
+ const fileDate = new Date(`${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}:${match[6]}Z`);
183
+ if (now - fileDate.getTime() > TWO_HOURS_MS) {
184
+ unlinkSync(resolve(backupDir, f));
185
+ }
186
+ }
187
+ }
188
+ } catch { /* cleanup is non-fatal */ }
189
+
190
+ // Periodic hot backup — SQLite-only. PGlite directory backups are an OS-level concern.
191
+ const backupTimer: ReturnType<typeof setInterval> | null = BACKEND === 'sqlite' ? setInterval(() => {
192
+ try {
193
+ const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
194
+ const backupPath = resolve(backupDir, `${basename(DB_PATH, '.db')}-${ts}.db`);
195
+ store.backup(backupPath);
196
+ // Prune: keep only last 6 backups
197
+ const backups = readdirSync(backupDir).filter(f => f.endsWith('.db')).sort();
198
+ while (backups.length > 6) {
199
+ const old = backups.shift()!;
200
+ try { unlinkSync(resolve(backupDir, old)); } catch { /* non-fatal */ }
201
+ }
202
+ } catch (err) {
203
+ console.warn(`[backup] failed: ${(err as Error).message}`);
204
+ }
205
+ }, 10 * 60_000) : null;
206
+
207
+ // Pre-load ML models (downloads on first run: embeddings ~22MB, reranker ~22MB, expander ~80MB)
208
+ getEmbedder().catch(err => console.warn('Embedding model unavailable:', err.message));
209
+ getReranker().catch(err => console.warn('Reranker model unavailable:', err.message));
210
+ getExpander().catch(err => console.warn('Query expander model unavailable:', err.message));
211
+
212
+ if (BACKEND === 'sqlite') {
213
+ setImmediate(() => {
214
+ try {
215
+ const t0 = Date.now();
216
+ store.warmSlimCache();
217
+ const stats = store.getSlimCacheStats();
218
+ console.log(` Slim cache warmed: ${stats.size} entries in ${Date.now() - t0}ms`);
219
+ } catch (err) {
220
+ console.warn(` Slim cache warm failed: ${(err as Error).message}`);
221
+ }
222
+ });
223
+ }
224
+
225
+ // Start server
226
+ await app.listen({ port: PORT, host: HOST });
227
+ console.log(`AgentWorkingMemory v${VERSION} listening on ${HOST}:${PORT}`);
228
+
229
+ // Graceful shutdown
230
+ const shutdown = async () => {
231
+ if (backupTimer) clearInterval(backupTimer);
232
+ await stopCoordinationCleanup();
233
+ consolidationScheduler.stop();
234
+ stagingBuffer.stop();
235
+ if (BACKEND === 'sqlite') {
236
+ try { store.walCheckpoint(); } catch { /* non-fatal */ }
237
+ }
238
+ try { await (store as any).close?.(); } catch { /* best-effort */ }
239
+ process.exit(0);
240
+ };
241
+ process.on('SIGINT', shutdown);
242
+ process.on('SIGTERM', shutdown);
243
+ }
244
+
245
+ main().catch(err => {
246
+ console.error('Failed to start:', err);
247
+ process.exit(1);
248
+ });