()
| 10 | validateEnvironment(); |
| 11 | |
| 12 | async function main() { |
| 13 | // Run migrations on startup if RUN_MIGRATIONS is set |
| 14 | if (process.env.RUN_MIGRATIONS === 'true') { |
| 15 | logger.info('RUN_MIGRATIONS=true, running database migrations'); |
| 16 | try { |
| 17 | const dbConfig = getDatabaseConfig(); |
| 18 | if (!dbConfig) { |
| 19 | throw new Error('DATABASE_URL not configured'); |
| 20 | } |
| 21 | await runMigrations(dbConfig); |
| 22 | } catch (error) { |
| 23 | logger.fatal({ err: error }, 'Migration failed'); |
| 24 | process.exit(1); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // GCP KMS signing provider initializes lazily on first signed AdCP call |
| 29 | // (see security/gcp-kms-signer.ts). Eager-init at boot was tried and |
| 30 | // pulled — when KMS auth is misconfigured, the gRPC client retries |
| 31 | // forever and the app never binds port 8080, taking down the whole |
| 32 | // deploy instead of just the signing path. Lazy is the safer default. |
| 33 | |
| 34 | // Start HTTP server first, then initialize Addie in the background. |
| 35 | // initializeAddieBolt uses execSync (git clone) which blocks the event loop, |
| 36 | // so it must not run before the server starts listening. |
| 37 | const httpServer = new HTTPServer(); |
| 38 | const port = parseInt(process.env.PORT || process.env.CONDUCTOR_PORT || "3000", 10); |
| 39 | await httpServer.start(port); |
| 40 | |
| 41 | // Periodic sweep of expired RFC 9421 nonce-cache rows. Postgres has no |
| 42 | // native TTL, so the replay-store table grows unboundedly without this. |
| 43 | // Safe after `httpServer.start` because the underlying pool is initialized |
| 44 | // by then; sweep failures log-warn but never crash. |
| 45 | const { startReplayCacheSweeper } = await import('./training-agent/request-signing.js'); |
| 46 | startReplayCacheSweeper(); |
| 47 | |
| 48 | // Log memory usage every 60s so PostHog/OTEL can chart heap trends. |
| 49 | // On 1GB Fly machines this is critical for spotting leaks early. |
| 50 | setInterval(() => { |
| 51 | const mem = process.memoryUsage(); |
| 52 | logger.info( |
| 53 | { |
| 54 | heap_used_mb: Math.round(mem.heapUsed / 1024 / 1024), |
| 55 | heap_total_mb: Math.round(mem.heapTotal / 1024 / 1024), |
| 56 | rss_mb: Math.round(mem.rss / 1024 / 1024), |
| 57 | external_mb: Math.round(mem.external / 1024 / 1024), |
| 58 | }, |
| 59 | "Memory usage" |
| 60 | ); |
| 61 | }, 60_000).unref(); |
| 62 | |
| 63 | // Addie (AAO Community Agent) with Bolt SDK — initialized after server is running. |
| 64 | // The router is stored in the Bolt app and retrieved via getAddieBoltRouter(). |
| 65 | initializeAddieBolt().then((result) => { |
| 66 | if (result) { |
| 67 | logger.info('Addie Bolt initialized successfully'); |
| 68 | } |
| 69 | }).catch((error) => { |
no test coverage detected