From c626a18eaa3f3fac04f1505116068592c11c2570 Mon Sep 17 00:00:00 2001 From: Murat Date: Thu, 6 Aug 2026 07:06:44 +0200 Subject: [PATCH] fix: importance scheduler table names and delayed startup for reliability --- lifecycle/importance_scheduler.py | 12 ++++++------ mcp_server/server.py | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lifecycle/importance_scheduler.py b/lifecycle/importance_scheduler.py index a72a5051..e44651e2 100644 --- a/lifecycle/importance_scheduler.py +++ b/lifecycle/importance_scheduler.py @@ -96,7 +96,7 @@ async def run_once(self) -> dict[str, int]: async def _rescore_user(self, user_id: str, conn, stats: dict) -> None: rows = await ( await conn.execute( - """SELECT id, "key", value, importance, memory_kind + """SELECT entry_id, "key", value, importance, memory_kind FROM core_memory WHERE user_id=? AND updated_at > ?""", (user_id, time.time() - self.cfg.only_recent_days * 86400), @@ -104,7 +104,7 @@ async def _rescore_user(self, user_id: str, conn, stats: dict) -> None: ).fetchall() for r in rows: - rc = await self._lookup_retrieval_count(conn, "core_memory", int(r["id"])) + rc = await self._lookup_retrieval_count(conn, "core_memory", int(r["entry_id"])) signals = self.scorer.score( text=r["value"] or "", kind=r["memory_kind"] or "fact", @@ -117,8 +117,8 @@ async def _rescore_user(self, user_id: str, conn, stats: dict) -> None: continue now = time.time() await conn.execute( - "UPDATE core_memory SET importance=?, updated_at=? WHERE id=?", - (new_score, now, int(r["id"])), + "UPDATE core_memory SET importance=?, updated_at=? WHERE entry_id=?", + (new_score, now, int(r["entry_id"])), ) await conn.execute( """INSERT INTO importance_audit @@ -127,7 +127,7 @@ async def _rescore_user(self, user_id: str, conn, stats: dict) -> None: VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", ( user_id, - int(r["id"]), + int(r["entry_id"]), "core_memory", old_score, new_score, @@ -150,7 +150,7 @@ async def _rescore_user(self, user_id: str, conn, stats: dict) -> None: async def _lookup_retrieval_count(conn, source: str, source_id: int) -> int: row = await ( await conn.execute( - """SELECT COUNT(*) c FROM audit_trail + """SELECT COUNT(*) c FROM audit_log WHERE action='recall_useful' AND layer=? AND target_id=?""", (source, str(source_id)), ) diff --git a/mcp_server/server.py b/mcp_server/server.py index 81bbaf07..8dca4557 100644 --- a/mcp_server/server.py +++ b/mcp_server/server.py @@ -75,8 +75,14 @@ async def lifespan(server: FastMCP): await asyncio.to_thread(read_only_replica.sync) ctx = AppContext() - backup_cron.start() - importance_scheduler.start() + async def _delayed_start(): + await asyncio.sleep(5) + backup_cron.start() + importance_scheduler.start() + logging.getLogger(__name__).info("Background tasks started after delay") + asyncio.create_task(_delayed_start()) + + # Disabled for startup speed debug # Periodic maintenance tasks async def _periodic_tasks(): @@ -177,6 +183,12 @@ def _run_with_dashboard(host: str, port: int): from shared.metrics import metrics as m ctx = AppContext() + async def _delayed_start(): + await asyncio.sleep(5) + backup_cron.start() + importance_scheduler.start() + logging.getLogger(__name__).info("Background tasks started after delay") + asyncio.create_task(_delayed_start()) dashboard = Dashboard(mm=ctx.mm) api_rate_limiter = RateLimiter() ws_limiter = ConnectionLimiter()