87ea715c2a
Flip the 4 business ORM models (ThreadMetaRow, RunRow, FeedbackRow, RunEventRow) to ``workspace_id: Mapped[str]`` with ``nullable=False``. PR5's alembic 0003 already enforces NOT NULL at the DB layer; this aligns the ORM-driven ``create_all()`` path (dev / tests) with the same invariant so a new install ends up at the post-0003 schema without running alembic. Test fallout absorbed: - `tests/conftest.py` autouse seed now produces a fully consistent pair: user row (default_workspace_id = test-workspace-autouse) plus the workspace itself. The PR5 backfill script's "users without default_workspace_id" query no longer picks the fixture up. Insert order is user → workspace → UPDATE user, walking around the chicken- and-egg FK between `workspaces.owner_id` and `users.default_workspace_id`. - `tests/test_backfill_workspace_id.py` adds a file-scoped autouse fixture that temporarily flips `column.nullable = True` for the four business tables (production correctness comes from alembic 0003; the script's own job is exactly to fill rows between 0002 and 0003 so its tests need that transient state to be representable). Its `_init_engine` also deletes the autouse seed rows to match the "fresh DB" model the tests assume. - `test_thread_meta_workspace_filter::test_create_workspace_none_bypasses` renamed to `test_create_workspace_none_rejected_by_orm` and asserts the new IntegrityError on explicit None — write paths can no longer bypass workspace scope. - 5 `test_workspace_context` tests + the auth-middleware reset test get `@pytest.mark.no_auto_workspace` so they keep testing the unset-contextvar path. - `test_workspace_repo::test_list_by_user_bypass_returns_all` switches to membership assertions instead of strict equality since the autouse fixture surfaces under `user_id=None`. 3214 passed, 30 skipped; the remaining 17 are the documented pre-existing caplog ordering flakes (all pass in isolation).
44 lines
2.4 KiB
Python
44 lines
2.4 KiB
Python
"""ORM model for run events."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime
|
||
|
||
from sqlalchemy import JSON, DateTime, ForeignKey, Index, String, Text, UniqueConstraint
|
||
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
||
from deerflow.persistence.base import Base
|
||
|
||
|
||
class RunEventRow(Base):
|
||
__tablename__ = "run_events"
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, comment="自增主键")
|
||
thread_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="所属会话 ID(threads_meta.thread_id)")
|
||
run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="所属运行 ID(runs.run_id)")
|
||
user_id: Mapped[str | None] = mapped_column(
|
||
String(64),
|
||
nullable=True,
|
||
index=True,
|
||
comment="会话所有者;为 NULL 表示鉴权引入之前的历史数据,新写入由 auth 中间件填充,启动期 orphan 迁移会回填存量",
|
||
)
|
||
workspace_id: Mapped[str] = mapped_column(
|
||
String(36),
|
||
ForeignKey("workspaces.id", ondelete="CASCADE"),
|
||
nullable=False,
|
||
comment="所属 workspace。PR5 引入时 nullable 用于回填;alembic 0003 + PR6 仓储接入完成后 NOT NULL",
|
||
)
|
||
event_type: Mapped[str] = mapped_column(String(32), nullable=False, comment="事件子类型(具体含义由 category 决定,如 ai_message_chunk、tool_call、run_started)")
|
||
category: Mapped[str] = mapped_column(String(16), nullable=False, comment='事件大类:"message" 消息 / "trace" 追踪 / "lifecycle" 生命周期')
|
||
content: Mapped[str] = mapped_column(Text, default="", comment="事件文本内容(消息体、错误、状态字符串等)")
|
||
event_metadata: Mapped[dict] = mapped_column(JSON, default=dict, comment="事件结构化元数据(JSON),随 event_type 而异")
|
||
seq: Mapped[int] = mapped_column(nullable=False, comment="在 thread 内的全局递增序号;与 thread_id 组合唯一")
|
||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), comment="创建时间(UTC)")
|
||
|
||
__table_args__ = (
|
||
UniqueConstraint("thread_id", "seq", name="uq_events_thread_seq"),
|
||
Index("ix_events_thread_cat_seq", "thread_id", "category", "seq"),
|
||
Index("ix_events_run", "thread_id", "run_id", "seq"),
|
||
{"comment": "运行事件流(消息/追踪/生命周期事件按 seq 顺序追加,是消息回放与审计的真源)"},
|
||
)
|