/**
 * AgentConfigLoader — hot-reload config cache for OODA agents.
 *
 * Reads per-agent runtime configuration from the `agent_config` table with an
 * in-memory TTL cache (default 30s). Each OODA cycle calls `get(agentId)` at
 * the start; after the TTL expires the next call transparently re-fetches from
 * the DB. No restart required for config changes.
 *
 * Column strategy:
 *   - `config` (new schema) — preferred when present.
 *   - `extra` (sprint-472 legacy) — backward-compat fallback.
 *
 * The `columnName` option allows the host to pin the column explicitly.
 *
 * @public
 */
// ─── Factory ──────────────────────────────────────────────────────────────────
/**
 * Create a hot-reload config loader backed by the `agent_config` table.
 *
 * @example
 * ```typescript
 * const loader = createAgentConfigLoader<MyConfig>({
 *   db,
 *   ttlMs: 30_000,
 *   defaultConfig: { kelly_cap: 0.2, conviction_min: 0.6 },
 * });
 *
 * // At cycle start:
 * const cfg = await loader.get(agentId);
 * ```
 */
export function createAgentConfigLoader(opts) {
    const ttlMs = opts.ttlMs ?? 30_000;
    const defaultConfig = (opts.defaultConfig ?? {});
    const column = opts.columnName ?? "extra";
    const cache = new Map();
    async function fetch(agentId) {
        const result = await opts.db.query(`SELECT ${column} AS cfg FROM agent_config WHERE agent_id = $1`, [agentId]);
        if (!result.rows.length) {
            return defaultConfig;
        }
        const raw = result.rows[0].cfg;
        if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
            return defaultConfig;
        }
        return raw;
    }
    return {
        async get(agentId) {
            const now = Date.now();
            const entry = cache.get(agentId);
            if (entry !== undefined && entry.expiresAt > now) {
                return entry.config;
            }
            const config = await fetch(agentId);
            cache.set(agentId, { config, expiresAt: now + ttlMs });
            return config;
        },
        invalidate(agentId) {
            cache.delete(agentId);
        },
    };
}
//# sourceMappingURL=agent-config-loader.js.map