The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(backend): the worker starts when the database user may not set an index expiry (#2238)
0xken1557174cc38e70
diff --git a/backend/src/lib/db/indexes.test.ts b/backend/src/lib/db/indexes.test.tsnew file mode 100644index 000000000..5aa7ff778--- /dev/null+++ b/backend/src/lib/db/indexes.test.ts@@ -0,0 +1,41 @@+import { describe, expect, it, vi } from 'vitest'+import { ensureIndex } from './indexes.js'+import { logger } from '../../utils/logger.js'++const SPEC = { key: { updatedAt: 1 }, name: 'read_mark_expiry', expireAfterSeconds: 15_552_000 }++/** A database whose index exists without expiry (as db push builds it), and whose collMod answers `collMod`. */+function db(collMod: () => unknown) {+ const calls: Record<string, unknown>[] = []+ const run = vi.fn(async (command: Record<string, unknown>) => {+ calls.push(command)+ if ('listIndexes' in command) return { cursor: { firstBatch: [{ key: { updatedAt: 1 }, name: 'read_mark_expiry' }] } }+ if ('collMod' in command) return collMod()+ return {}+ })+ return { db: { $runCommandRaw: run } as never, calls }+}++describe('ensureIndex', () => {+ it('sets a missing expiry with collMod and builds nothing', async () => {+ const { db: fake, calls } = db(() => ({ ok: 1 }))+ await ensureIndex(fake, 'SuperTabReadMark', SPEC)+ expect(calls.map(call => Object.keys(call)[0])).toEqual(['listIndexes', 'collMod'])+ })++ it('warns and carries on when the database user may not run collMod (10-29: the worker crash-looped)', async () => {+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => logger)+ const { db: fake, calls } = db(() => {+ throw new Error('Raw query failed. Code: `13`. Message: `not authorized on harness to execute command { collMod: "SuperTabReadMark" }`')+ })+ await expect(ensureIndex(fake, 'SuperTabReadMark', SPEC)).resolves.toBeUndefined()+ expect(calls.map(call => Object.keys(call)[0])).toEqual(['listIndexes', 'collMod'])+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('collMod'), expect.objectContaining({ collection: 'SuperTabReadMark', index: 'read_mark_expiry' }))+ warn.mockRestore()+ })++ it('still fails on any other collMod error', async () => {+ const { db: fake } = db(() => { throw new Error('Raw query failed. Code: `27`. Message: `index not found`') })+ await expect(ensureIndex(fake, 'SuperTabReadMark', SPEC)).rejects.toThrow('index not found')+ })+})diff --git a/backend/src/lib/db/indexes.ts b/backend/src/lib/db/indexes.tsindex 22c5028c3..eb7c2f9d4 100644--- a/backend/src/lib/db/indexes.ts+++ b/backend/src/lib/db/indexes.ts@@ -1,4 +1,8 @@import type { PrismaClient } from '@prisma/client'+import { logger } from '../../utils/logger.js'++/** Mongo's "not authorized" (code 13), as Prisma's raw command error carries it. */+const notAuthorized = (error: unknown): boolean => /not authorized|Unauthorized|Code: `13`/.test(String((error as Error)?.message ?? error))/** One index as `createIndexes` takes it. */export type IndexSpec = { name: string; key: Record<string, number>; unique?: boolean; expireAfterSeconds?: number }@@ -26,7 +30,14 @@ export async function ensureIndex(db: Pick<PrismaClient, '$runCommandRaw'>, collif (found && sameKey(found.key, index.key) && !!found.unique === !!index.unique) {if (found.expireAfterSeconds === index.expireAfterSeconds) returnif (index.expireAfterSeconds !== undefined) {- await db.$runCommandRaw({ collMod: collection, index: { name: index.name, expireAfterSeconds: index.expireAfterSeconds } })+ try {+ await db.$runCommandRaw({ collMod: collection, index: { name: index.name, expireAfterSeconds: index.expireAfterSeconds } })+ } catch (error) {+ // Production's app user may not run collMod (10-29: the worker crash-looped on the read-mark expiry). The index+ // exists and serves reads; only its expiry waits for someone with the right to set it, so warn and carry on.+ if (!notAuthorized(error)) throw error+ logger.warn('index expiry not set: the database user may not run collMod', { collection, index: index.name, expireAfterSeconds: index.expireAfterSeconds })+ }return}}