sync update from LAN repo (source dhk-1 @ 3e87be55)

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -13,6 +13,8 @@
from __future__ import annotations
from contextlib import AsyncExitStack, asynccontextmanager
import pytest
from sqlalchemy import create_engine, inspect, text
@@ -123,6 +125,105 @@ 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")
@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)
database_path = (tmp_path / "schema-failure.db").as_posix()
try:
with caplog.at_level("ERROR"):
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 +362,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: