mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 23:32:12 +03:00
fix: fully close MCP audit SQLite connections on shutdown (#1349)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
@@ -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<AuditDatabase | null> {
|
||||
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<AuditDatabase | null> {
|
||||
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<AuditDatabase | null> {
|
||||
}
|
||||
|
||||
export function closeAuditDb(): boolean {
|
||||
if (!db) return false;
|
||||
const database = getCachedAuditDb();
|
||||
if (!database) return false;
|
||||
|
||||
const database = db;
|
||||
db = null;
|
||||
setCachedAuditDb(null);
|
||||
|
||||
try {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user