harden: address adversarial review of Pods CRUD

Backend: atomic create with insert-retry-on-duplicate-key (removes slug
TOCTOU race + wrong 400s), resilient per-index creation so a unique-index
failure can't silently drop the constraint or block seeding, idempotent
race-safe seeding via $setOnInsert, type validation + size caps on all pod
fields/members, removeMember no-ops without a write, /api/outcome guarded.
Frontend: per-pod busy state (one mutation no longer disables every card),
joined pod derived from the list (can't go stale), create keeps inputs on error.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Kartikeya
2026-06-27 16:02:40 -07:00
parent 4ddc59be08
commit 7ff750ad5e
5 changed files with 163 additions and 78 deletions
+18 -8
View File
@@ -48,13 +48,23 @@ export async function initMemory(): Promise<void> {
const db = await getDb();
await db.command({ ping: 1 });
const c = await collections();
await Promise.all([
c.pods.createIndex({ id: 1 }, { unique: true }),
c.observations.createIndex({ podId: 1, observedAt: -1 }),
c.observations.createIndex({ engineerId: 1 }),
c.collisions.createIndex({ podId: 1, detectedAt: -1 }),
c.interventions.createIndex({ collisionId: 1 }),
c.outcomes.createIndex({ interventionId: 1 }),
]);
// Create each index independently so one failure (e.g. the unique pods index
// failing on pre-existing duplicate ids) doesn't abort the others or block
// seeding. Failures are logged loudly rather than silently swallowed.
const indexes: Array<[string, () => Promise<unknown>]> = [
['pods.id (unique)', () => c.pods.createIndex({ id: 1 }, { unique: true })],
['observations.podId', () => c.observations.createIndex({ podId: 1, observedAt: -1 })],
['observations.engineerId', () => c.observations.createIndex({ engineerId: 1 })],
['collisions.podId', () => c.collisions.createIndex({ podId: 1, detectedAt: -1 })],
['interventions.collisionId', () => c.interventions.createIndex({ collisionId: 1 })],
['outcomes.interventionId', () => c.outcomes.createIndex({ interventionId: 1 })],
];
for (const [name, make] of indexes) {
try {
await make();
} catch (err) {
console.error(`[memory] index "${name}" failed: ${(err as Error).message}`);
}
}
console.log(`[memory] mongo connected -> ${db.databaseName}`);
}