diff --git a/src/index.ts b/src/index.ts index 286730de8..60aea31e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ import { generateBuildClarificationMicrocopy, llm, type BuildClarificationMicroc import { sanitizeAndSplitTelegramText } from './outboundSanitize'; import { applyPlainWordsSurfaceRequest } from './telegramSurface'; import { installConsoleRedaction, redactIdentifier, redactText } from './redaction'; -import { readJsonFile } from './jsonState'; +import { readJsonFile, closeJsonState } from './jsonState'; import { formatCreatorMissionExecutionSummary, formatCreatorMissionStatusSummary, @@ -11630,6 +11630,7 @@ bot.on(message('audio'), handleVoiceMessage); process.once('SIGINT', () => { console.log('Shutting down...'); void releaseGatewayOwnership(); + closeJsonState(); if (pollingActive) { bot.stop('SIGINT'); } @@ -11637,6 +11638,7 @@ process.once('SIGINT', () => { process.once('SIGTERM', () => { console.log('Shutting down...'); void releaseGatewayOwnership(); + closeJsonState(); if (pollingActive) { bot.stop('SIGTERM'); } diff --git a/src/jsonState.ts b/src/jsonState.ts index 853b40a34..e96bcc41a 100644 --- a/src/jsonState.ts +++ b/src/jsonState.ts @@ -70,6 +70,18 @@ export function resolveStatePath(filename: string): string { return path.join(stateDir || process.cwd(), filename); } +export function closeJsonState(): void { + if (!db) return; + try { + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); + db.close(); + } catch { + // Best-effort cleanup + } finally { + db = null; + } +} + export function resetJsonStateForTests(): void { if (!db) return; try {