feat: support postgres autonomy runtime

This commit is contained in:
ImBenji
2026-08-17 13:43:50 +01:00
parent 4aa2367971
commit f0b598a3b8
11 changed files with 301 additions and 24 deletions
+6 -5
View File
@@ -5,6 +5,7 @@ const { Worker } = require('node:worker_threads');
const db = require('../db');
const config = require('../config');
const Database = require('better-sqlite3');
const { openRuntimeDb, isPostgresEnabled } = require('../db/runtime');
let idb = null;
let statsSummaryCache = null;
@@ -46,9 +47,9 @@ function getIntelligenceDb() {
? (path.isAbsolute(config.intelligence_db) ? config.intelligence_db : path.resolve(configDir, config.intelligence_db))
: path.resolve(configDir, 'intelligence.sqlite'));
if (!fs.existsSync(rawPath)) return null;
if (!isPostgresEnabled() && !fs.existsSync(rawPath)) return null;
idb = new Database(rawPath);
idb = isPostgresEnabled() ? openRuntimeDb(rawPath, { schema: 'intelligence' }) : new Database(rawPath);
return idb;
}
@@ -153,9 +154,9 @@ async function adminRoutes(fastify) {
if (!checkAuth(request, reply)) return;
const intelligenceDb = getIntelligenceDb();
if (!intelligenceDb) return { enabled: false, reason: 'intelligence database unavailable' };
const hasSchema = intelligenceDb.prepare(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='autonomy_jobs'"
).get();
const hasSchema = isPostgresEnabled()
? intelligenceDb.prepare("SELECT 1 FROM information_schema.tables WHERE table_schema = ? AND table_name = ?").get('intelligence', 'autonomy_jobs')
: intelligenceDb.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='autonomy_jobs'").get();
if (!hasSchema) return { enabled: false, reason: 'autonomy schema is not initialized' };
const jobs = intelligenceDb.prepare(`
+5 -3
View File
@@ -1,14 +1,16 @@
const path = require('path');
const Database = require('better-sqlite3');
const { openRuntimeDb, isPostgresEnabled } = require('../db/runtime');
const intelligencePath = process.env.INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite');
const db = new Database(intelligencePath, { readonly: true });
const db = openRuntimeDb(intelligencePath, { schema: 'intelligence', readonly: true });
async function autonomyRoutes(fastify) {
fastify.get('/health', async () => ({ ok: true, service: 'duriin-api' }));
fastify.get('/autonomy/status', async () => {
const exists = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='autonomy_jobs'").get();
const exists = isPostgresEnabled()
? db.prepare("SELECT 1 FROM information_schema.tables WHERE table_schema = ? AND table_name = ?").get('intelligence', 'autonomy_jobs')
: db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='autonomy_jobs'").get();
if (!exists) return { enabled: false, reason: 'autonomy schema is not initialized' };
const jobs = db.prepare(`
SELECT lane, status, COUNT(*) AS count