sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -13,6 +13,9 @@
from __future__ import annotations
import logging
from contextlib import AsyncExitStack, asynccontextmanager
import pytest
from sqlalchemy import create_engine, inspect, text
@@ -123,6 +126,150 @@ def test_sync_idempotent_repeated_runs_do_not_duplicate_index():
assert idx_names.count("uq_roundtable_jobs_active_dedupe_key") == 1
def test_legacy_thread_metadata_gains_and_backfills_task_lookup_projections():
"""Startup sync must make historical task chats immediately searchable."""
from deerflow.persistence.engine import (
_backfill_thread_meta_task_projections_sync,
_ensure_orm_columns_sync,
)
engine = create_engine("sqlite:///:memory:")
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE threads_meta ("
"thread_id VARCHAR(64) PRIMARY KEY, assistant_id VARCHAR(128), user_id VARCHAR(64), "
"display_name VARCHAR(256), status VARCHAR(20) NOT NULL, metadata_json JSON, "
"created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL)"
)
)
conn.execute(
text(
"INSERT INTO threads_meta (thread_id, status, metadata_json, created_at, updated_at) VALUES "
"('legacy-task', 'idle', '{\"taskId\":\"108\",\"agent_id\":\"rwfx-agent\",\"actionId\":\"A-9\"}', "
"CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
)
)
_ensure_orm_columns_sync(conn)
_backfill_thread_meta_task_projections_sync(conn)
with engine.connect() as conn:
columns = {column["name"] for column in inspect(conn).get_columns("threads_meta")}
indexes = {index["name"] for index in inspect(conn).get_indexes("threads_meta")}
assert {"task_id", "agent_id", "action_id"} <= columns
assert {
"ix_threads_meta_task_agent_updated",
"ix_threads_meta_task_action_agent_updated",
"ix_threads_meta_user_updated",
} <= indexes
assert conn.execute(
text("SELECT task_id, agent_id, action_id FROM threads_meta WHERE thread_id = 'legacy-task'")
).one() == ("108", "rwfx-agent", "A-9")
def test_legacy_knowledge_retrieval_settings_gains_advanced_management_flag():
"""既有智能检索设置表在启动时补齐高级管理开关,且旧行默认关闭。"""
from deerflow.persistence.engine import _ensure_orm_columns_sync
engine = create_engine("sqlite:///:memory:")
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE knowledge_retrieval_settings ("
"scope VARCHAR(64) PRIMARY KEY,"
"smart_reasoning_enabled BOOLEAN NOT NULL DEFAULT 0,"
"revision INTEGER NOT NULL DEFAULT 1,"
"updated_by VARCHAR(64),"
"updated_at DATETIME NOT NULL"
")"
)
)
conn.execute(
text(
"INSERT INTO knowledge_retrieval_settings "
"(scope, smart_reasoning_enabled, revision, updated_at) "
"VALUES ('global', 1, 1, '2026-10-19 00:00:00')"
)
)
_ensure_orm_columns_sync(conn)
columns = {
item["name"]
for item in inspect(engine).get_columns("knowledge_retrieval_settings")
}
assert "advanced_management_enabled" in columns
with engine.connect() as conn:
row = conn.execute(
text(
"SELECT smart_reasoning_enabled, advanced_management_enabled "
"FROM knowledge_retrieval_settings WHERE scope = 'global'"
)
).one()
assert int(row[0]) == 1
assert int(row[1]) == 0
@pytest.mark.asyncio
async def test_schema_creation_failure_does_not_abort_engine_startup(
tmp_path, monkeypatch, caplog
):
"""建表 DDL 失败只降级并告警,不能让 Gateway lifespan 启动失败。"""
from deerflow.persistence.base import Base
from deerflow.persistence.engine import (
close_engine,
get_session_factory,
init_engine,
)
def fail_create_all(*_args, **_kwargs):
raise RuntimeError("simulated incompatible table DDL")
await close_engine()
monkeypatch.setattr(Base.metadata, "create_all", fail_create_all)
persistence_logger = logging.getLogger("deerflow.persistence.engine")
monkeypatch.setattr(persistence_logger, "disabled", False)
monkeypatch.setattr(persistence_logger, "propagate", True)
database_path = (tmp_path / "schema-failure.db").as_posix()
try:
with caplog.at_level("ERROR", logger=persistence_logger.name):
await init_engine(
"sqlite",
url=f"sqlite+aiosqlite:///{database_path}",
sqlite_dir=str(tmp_path),
)
assert get_session_factory() is not None
assert "continuing Gateway startup" in caplog.text
finally:
await close_engine()
@pytest.mark.asyncio
async def test_runtime_table_setup_failure_uses_in_memory_fallback(caplog):
"""Checkpointer/Store 自己的 setup 建表失败时也必须允许服务启动。"""
from app.gateway.deps import _enter_runtime_backend_or_fallback
fallback = object()
@asynccontextmanager
async def broken_backend():
raise RuntimeError("simulated setup DDL failure")
yield # pragma: no cover - makes this an async context manager
with caplog.at_level("ERROR"):
async with AsyncExitStack() as stack:
value = await _enter_runtime_backend_or_fallback(
stack,
broken_backend(),
label="test backend",
fallback_factory=lambda: fallback,
)
assert value is fallback
assert "in-memory fallback" in caplog.text
def _legacy_task_buttons_ddl() -> str:
"""线上旧版 task_buttons(login_params_json / append_auth 之前)的最小列集。"""
return (
@@ -261,9 +408,9 @@ def _legacy_report_collaboration_003_ddl() -> list[str]:
def test_legacy_report_collaboration_gains_015_016_columns_and_tables():
"""既有 003 表缺模板/预算/幂等列 → 启动 sync 补列;create_all 建改写/审计新表。"""
import deerflow.persistence.models # noqa: F401
from deerflow.persistence.base import Base
from deerflow.persistence.engine import _ensure_orm_columns_sync
import deerflow.persistence.models # noqa: F401
engine = create_engine("sqlite:///:memory:")
with engine.begin() as conn: