Phase 3 implementation: - AI session analysis service that generates flow proposals from resolved sessions - APScheduler job for batch processing pending analyses (max_instances=1) - Knowledge gap detection (weak options, high escalation signals) - Flow proposals CRUD with team admin review workflow (approve/edit/dismiss/reject) - FlowPilot analytics dashboard with confidence tiers, PSA metrics, knowledge gaps - In-session script generator component - Review queue page with filtering and proposal detail panel Bug fixes from review (12 total): - Fix "Edit & Publish" navigating to non-existent /editor/new route - Hide Approve button for enhancement proposals (require Edit & Publish) - Add max_instances=1 to scheduler to prevent TOCTOU race - Fix eventual_success case() double-counting failed retries - Add tree_structure validation before creating tree from proposal - Simplify script generator rendering condition - Add severity style fallback, toFixed on rates, Link instead of <a href> - Add toast.warning on dismiss failure, fix dedup for domain-less sessions - Cast Decimal to int in knowledge gap evidence dicts Also updates CLAUDE.md with lessons 67-71 and Phase 3 project structure. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
73 lines
2.8 KiB
Python
73 lines
2.8 KiB
Python
"""Background scheduler for Knowledge Flywheel analysis.
|
|
|
|
Runs every 5 minutes via APScheduler, picks up AISession entries
|
|
with analysis_status='pending' and runs flow proposal analysis.
|
|
|
|
Each session is committed individually to prevent a single failure
|
|
from rolling back all progress or causing duplicate proposals.
|
|
"""
|
|
import logging
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.core.database import async_session_maker
|
|
from app.models.ai_session import AISession
|
|
from app.services.knowledge_flywheel import analyze_session
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def process_pending_analyses() -> None:
|
|
"""Process resolved sessions awaiting Knowledge Flywheel analysis."""
|
|
async with async_session_maker() as db:
|
|
try:
|
|
result = await db.execute(
|
|
select(AISession.id)
|
|
.where(AISession.analysis_status == "pending")
|
|
.order_by(AISession.resolved_at.asc())
|
|
.limit(10)
|
|
)
|
|
session_ids = [row[0] for row in result.all()]
|
|
except Exception as e:
|
|
logger.error("Knowledge Flywheel scheduler query error: %s", e)
|
|
return
|
|
|
|
if not session_ids:
|
|
return
|
|
|
|
logger.info("Processing %d pending Knowledge Flywheel analyses", len(session_ids))
|
|
|
|
# Process each session in its own DB session to isolate failures
|
|
for session_id in session_ids:
|
|
async with async_session_maker() as db:
|
|
try:
|
|
result = await db.execute(
|
|
select(AISession).where(AISession.id == session_id)
|
|
)
|
|
session = result.scalar_one_or_none()
|
|
if not session or session.analysis_status != "pending":
|
|
continue
|
|
|
|
await analyze_session(session, db)
|
|
session.analysis_status = "completed"
|
|
await db.commit()
|
|
logger.info("Knowledge Flywheel completed for session %s", session_id)
|
|
except Exception as e:
|
|
await db.rollback()
|
|
logger.warning(
|
|
"Knowledge Flywheel failed for session %s: %s",
|
|
session_id, e,
|
|
)
|
|
# Mark as failed in a separate transaction
|
|
try:
|
|
async with async_session_maker() as db2:
|
|
result = await db2.execute(
|
|
select(AISession).where(AISession.id == session_id)
|
|
)
|
|
s = result.scalar_one_or_none()
|
|
if s:
|
|
s.analysis_status = "failed"
|
|
await db2.commit()
|
|
except Exception:
|
|
logger.error("Failed to mark session %s as failed", session_id)
|