diff --git a/open-sse/executors/codex.ts b/open-sse/executors/codex.ts index e0bf19ddd5..e04f7d172f 100644 --- a/open-sse/executors/codex.ts +++ b/open-sse/executors/codex.ts @@ -495,12 +495,18 @@ export class CodexExecutor extends BaseExecutor { // Codex still requires a non-empty instructions field. // Use a minimal placeholder if the client didn't provide one. - if (!body.instructions || (typeof body.instructions === "string" && body.instructions.trim() === "")) { + if ( + !body.instructions || + (typeof body.instructions === "string" && body.instructions.trim() === "") + ) { body.instructions = "Follow the developer instructions in the conversation."; } } else { // Translated path: hoist system messages to instructions (legacy behavior). - if (!body.instructions || (typeof body.instructions === "string" && body.instructions.trim() === "")) { + if ( + !body.instructions || + (typeof body.instructions === "string" && body.instructions.trim() === "") + ) { body.instructions = CODEX_DEFAULT_INSTRUCTIONS; } hoistSystemMessagesToInstructions(body); diff --git a/open-sse/mcp-server/__tests__/audit.test.ts b/open-sse/mcp-server/__tests__/audit.test.ts index e1c7a7a714..90e5713a58 100644 --- a/open-sse/mcp-server/__tests__/audit.test.ts +++ b/open-sse/mcp-server/__tests__/audit.test.ts @@ -24,6 +24,7 @@ describe("MCP audit shutdown", () => { beforeEach(() => { vi.resetModules(); + globalThis.__omnirouteMcpAuditDb = undefined; dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-mcp-audit-")); dbFile = path.join(dataDir, "storage.sqlite"); fs.writeFileSync(dbFile, ""); @@ -32,6 +33,7 @@ describe("MCP audit shutdown", () => { afterEach(() => { delete process.env.DATA_DIR; + globalThis.__omnirouteMcpAuditDb = undefined; vi.restoreAllMocks(); }); diff --git a/open-sse/mcp-server/audit.ts b/open-sse/mcp-server/audit.ts index 6ac75eb0a0..18897035e0 100644 --- a/open-sse/mcp-server/audit.ts +++ b/open-sse/mcp-server/audit.ts @@ -23,6 +23,10 @@ interface AuditDatabase { open?: boolean; } +declare global { + var __omnirouteMcpAuditDb: AuditDatabase | null | undefined; +} + interface AuditStatsRow { total: unknown; successRate: unknown; @@ -124,7 +128,13 @@ function buildAuditFilterSql(filters: McpAuditQuery): { whereSql: string; params }; } -let db: AuditDatabase | null = null; +function getCachedAuditDb(): AuditDatabase | null { + return globalThis.__omnirouteMcpAuditDb ?? null; +} + +function setCachedAuditDb(database: AuditDatabase | null): void { + globalThis.__omnirouteMcpAuditDb = database; +} function toNumber(value: unknown, fallback = 0): number { const parsed = @@ -145,7 +155,8 @@ function toString(value: unknown): string { * Uses the same SQLite database as the main OmniRoute app. */ async function getDb(): Promise { - if (db) return db; + const cachedDb = getCachedAuditDb(); + if (cachedDb) return cachedDb; try { // Try importing the db module from the main app @@ -165,8 +176,9 @@ async function getDb(): Promise { const Database = (await import("better-sqlite3")).default as unknown as new ( dbPath: string ) => AuditDatabase; - db = new Database(dbPath); - return db; + const database = new Database(dbPath); + setCachedAuditDb(database); + return database; } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); console.error("[MCP Audit] Failed to connect to database:", message); @@ -175,10 +187,10 @@ async function getDb(): Promise { } export function closeAuditDb(): boolean { - if (!db) return false; + const database = getCachedAuditDb(); + if (!database) return false; - const database = db; - db = null; + setCachedAuditDb(null); try { try {