Backs the schema added in 210d310 with SQLAlchemy 2.0 models.
- SessionFact: "What we know" facts with polymorphic source_ref pointing
at task-lane item UUIDs inside ai_sessions.pending_task_lane (not a FK
per Section 4.2).
- SessionSuggestedFix: AI-proposed resolutions with supersession tracking
and the full user_decision state machine.
- DraftTemplate: post-resolve templatization queue with promotion to
script_templates.
- AccountSettings: per-account JSONB preferences grab-bag with async
classmethod helpers — get_setting(db, account_id, key, default) reads
without creating, set_setting(db, account_id, key, value) upserts via
Postgres ON CONFLICT + jsonb `||` merge so existing keys are preserved.
Lazy row creation matches the Phase 1 design.
Column additions on existing models to mirror the migration:
- AISession: resolution_note_* / escalation_package_* / state_version
(the preview-cache-invalidation counter consumed by Phase 3).
- ScriptTemplate: source_session_id / source_user_id / source_ticket_ref
(provenance for templates promoted from DraftTemplate).
All four new models registered in app.models.__init__ and __all__.
TYPE_CHECKING-guarded relationship imports throughout, matching the
repo's existing model style.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
"""Session fact model — the "What we know" backing store for a FlowPilot session.
|
|
|
|
A fact is an atomic, engineer-readable statement of what has been confirmed
|
|
during troubleshooting. Facts accumulate across the session and drive the
|
|
resolution note preview.
|
|
|
|
`source_ref` is a polymorphic pointer to a task-lane item inside
|
|
`ai_sessions.pending_task_lane` JSON — it is NOT a FK. Integrity is enforced
|
|
at the service layer per the FLOWPILOT-MIGRATION design doc Section 4.2.
|
|
Phase 2 assigns stable UUIDs to those task-lane items so `source_ref` has
|
|
something reliable to point to.
|
|
"""
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import Text, DateTime, ForeignKey, String, CheckConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from app.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.ai_session import AISession
|
|
from app.models.account import Account
|
|
from app.models.user import User
|
|
|
|
|
|
class SessionFact(Base):
|
|
"""A single fact in the What-we-know section of a session's task lane."""
|
|
__tablename__ = "session_facts"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"source_type IN ('question', 'diagnostic_check', 'user_note', 'ai_synthesis')",
|
|
name="ck_session_facts_source_type",
|
|
),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
session_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("ai_sessions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
account_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("accounts.id"),
|
|
nullable=False,
|
|
)
|
|
text: Mapped[str] = mapped_column(Text, nullable=False)
|
|
source_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
# Pointer to a task-lane item UUID inside ai_sessions.pending_task_lane.
|
|
# NOT a FK. Null for `user_note` and `ai_synthesis` sources.
|
|
source_ref: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), nullable=True
|
|
)
|
|
source_summary: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
created_by: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("users.id"),
|
|
nullable=False,
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(timezone.utc),
|
|
onupdate=lambda: datetime.now(timezone.utc),
|
|
)
|
|
deleted_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
|
|
session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id])
|
|
account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
|
|
creator: Mapped["User"] = relationship("User", foreign_keys=[created_by])
|