from __future__ import annotations import logging from datetime import datetime, timedelta logger = logging.getLogger(__name__) class OperationalStore: def __init__(self, pool): self._pool = pool async def store(self, scope, summary, raw_data=None, ttl_days=90, source_directive_id=None): expires_at = datetime.utcnow() + timedelta(days=ttl_days) async with self._pool.acquire(timeout=10) as conn: await conn.execute( """INSERT INTO ab_operational_memory (scope, summary, raw_data, source_directive_id, expires_at) VALUES ($1, $2, $3, $4, $5)""", scope, summary, raw_data, source_directive_id, expires_at) async def get_recent(self, scope, limit=10): async with self._pool.acquire(timeout=10) as conn: rows = await conn.fetch( """SELECT id, scope, summary, raw_data, created_at FROM ab_operational_memory WHERE scope = $1 AND (expires_at IS NULL OR expires_at > NOW()) ORDER BY created_at DESC LIMIT $2""", scope, limit) return [dict(r) for r in rows] async def prune_expired(self): async with self._pool.acquire(timeout=10) as conn: result = await conn.execute( 'DELETE FROM ab_operational_memory WHERE expires_at IS NOT NULL AND expires_at < NOW()') logger.info('OperationalStore.prune_expired: %s', result)