feat(backend): persist memory to MongoDB

Replace the in-memory store with a real MongoDB-backed store. Adds memory/db.ts
(client singleton, typed collections, startup ping + indexes) and rewrites
record{Observation,Collision,Intervention,Outcome} to insert into Mongo
(best-effort — failures log, don't crash). Server connects on startup and
exposes GET /api/memory/stats for verification.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Kartikeya
2026-06-27 15:34:57 -07:00
parent e8fb91232f
commit 3fc19194f2
3 changed files with 95 additions and 33 deletions
+15 -2
View File
@@ -5,7 +5,8 @@ import { WebSocketServer } from 'ws';
import { AccessToken } from 'livekit-server-sdk';
import { env } from './env.js';
import { createSyncPr } from './github/client.js';
import { recordOutcome } from './memory/store.js';
import { recordOutcome, memoryStats } from './memory/store.js';
import { initMemory } from './memory/db.js';
import type { InterventionOutcome } from '@podman/shared';
const app = express();
@@ -44,6 +45,15 @@ app.post('/api/outcome', async (req, res) => {
res.json({ ok: true });
});
// Memory counts — quick way to confirm Mongo persistence is working.
app.get('/api/memory/stats', async (_req, res) => {
try {
res.json(await memoryStats());
} catch (e) {
res.status(500).json({ error: (e as Error).message });
}
});
const http = createServer(app);
// ws relay: the agent pushes collision/intervention JSON here; PWAs subscribed by pod receive it.
@@ -58,4 +68,7 @@ wss.on('connection', (ws) => {
});
});
http.listen(env.PORT, '0.0.0.0', () => console.log(`[server] :${env.PORT}`));
http.listen(env.PORT, '0.0.0.0', () => {
console.log(`[server] :${env.PORT}`);
initMemory().catch((e) => console.warn(`[memory] init failed: ${(e as Error).message}`));
});