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

@@ -54,7 +54,9 @@ def do_run_migrations(connection):
# = 1 year; innodb_lock_wait_timeout = 50s). Without this, an idle
# transaction on another connection can freeze startup indefinitely.
# Tunable via DEER_FLOW_ALEMBIC_LOCK_WAIT (seconds, default 15).
if connection.dialect.name == "mysql":
from deerflow.persistence.gauss_mysql_compat import is_gauss_mysql_product
if connection.dialect.name == "mysql" and not is_gauss_mysql_product():
try:
lock_wait = int(os.getenv("DEER_FLOW_ALEMBIC_LOCK_WAIT", "15"))
except ValueError:
@@ -0,0 +1,21 @@
"""Merge assistant-wiki metadata and report-collaboration heads.
Revision ID: 20260909_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-09
"""
from __future__ import annotations
revision = "20260909_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
pass
def downgrade() -> None:
pass
@@ -0,0 +1,74 @@
"""Create durable public-document library tables.
Revision ID: 20260914_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-14
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260914_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("gongwen_documents"):
op.create_table(
"gongwen_documents",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("user_id", sa.String(length=64), nullable=True),
sa.Column("title", sa.String(length=256), nullable=False, server_default=""),
sa.Column("document_type", sa.String(length=48), nullable=False, server_default="notice"),
sa.Column("source", sa.String(length=24), nullable=False, server_default="upload"),
sa.Column("status", sa.String(length=32), nullable=False, server_default="checking"),
sa.Column("template_key", sa.String(length=96), nullable=True),
sa.Column("research_session_id", sa.String(length=64), nullable=True),
sa.Column("original_version_id", sa.String(length=64), nullable=True),
sa.Column("latest_version_id", sa.String(length=64), nullable=True),
sa.Column("issue_counts", sa.Text(), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_gongwen_documents_user_id", "gongwen_documents", ["user_id"])
op.create_index("ix_gongwen_documents_document_type", "gongwen_documents", ["document_type"])
op.create_index("ix_gongwen_documents_source", "gongwen_documents", ["source"])
op.create_index("ix_gongwen_documents_status", "gongwen_documents", ["status"])
op.create_index("ix_gongwen_documents_research_session_id", "gongwen_documents", ["research_session_id"])
if not inspector.has_table("gongwen_document_versions"):
op.create_table(
"gongwen_document_versions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("document_id", sa.String(length=64), nullable=False),
sa.Column("version_no", sa.Integer(), nullable=False),
sa.Column("kind", sa.String(length=32), nullable=False),
sa.Column("file_name", sa.String(length=512), nullable=False),
sa.Column("storage_key", sa.String(length=1024), nullable=False),
sa.Column("content_type", sa.String(length=128), nullable=False, server_default="application/octet-stream"),
sa.Column("size_bytes", sa.Integer(), nullable=False, server_default="0"),
sa.Column("sha256", sa.String(length=64), nullable=False, server_default=""),
sa.Column("review_snapshot", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("document_id", "version_no", name="uq_gongwen_document_version_no"),
)
op.create_index("ix_gongwen_document_versions_document_id", "gongwen_document_versions", ["document_id"])
op.create_index(
"ix_gongwen_document_versions_document_created",
"gongwen_document_versions",
["document_id", "created_at"],
)
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("gongwen_document_versions"):
op.drop_table("gongwen_document_versions")
if inspector.has_table("gongwen_documents"):
op.drop_table("gongwen_documents")
@@ -0,0 +1,88 @@
"""Persist mapping-scoped links for local LLMWiki graph expansion.
Revision ID: 20260916_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-16
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260916_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("llmwiki_wiki_edges"):
return
op.create_table(
"llmwiki_wiki_edges",
sa.Column("id", sa.String(length=36), primary_key=True),
sa.Column(
"knowledge_base_mapping_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_knowledge_bases.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"source_page_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_wiki_pages.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"target_page_id",
sa.String(length=36),
sa.ForeignKey("llmwiki_wiki_pages.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"edge_type",
sa.String(length=32),
nullable=False,
server_default="wiki_link",
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint(
"knowledge_base_mapping_id",
"source_page_id",
"target_page_id",
"edge_type",
name="uq_llmwiki_wiki_edge_mapping_pages_type",
),
)
op.create_index(
"ix_llmwiki_wiki_edges_knowledge_base_mapping_id",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id"],
)
op.create_index(
"ix_llmwiki_wiki_edges_source_page_id",
"llmwiki_wiki_edges",
["source_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edges_target_page_id",
"llmwiki_wiki_edges",
["target_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edge_mapping_source",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id", "source_page_id"],
)
op.create_index(
"ix_llmwiki_wiki_edge_mapping_target",
"llmwiki_wiki_edges",
["knowledge_base_mapping_id", "target_page_id"],
)
def downgrade() -> None:
if sa.inspect(op.get_bind()).has_table("llmwiki_wiki_edges"):
op.drop_table("llmwiki_wiki_edges")
@@ -0,0 +1,240 @@
"""Add answer-level feedback reasons and agent score dimensions.
Revision ID: 20260916_02
Revises: 20260916_01
Create Date: 2026-09-16
"""
from __future__ import annotations
import hashlib
import sqlalchemy as sa
from alembic import op
revision = "20260916_02"
down_revision = "20260916_01"
branch_labels = None
depends_on = None
_TABLE = "feedback"
_OLD_INDEX = "uq_feedback_thread_run_user"
_TARGET_INDEX = "uq_feedback_target_key"
_AGENT_INDEX = "ix_feedback_agent_created"
def _columns() -> set[str]:
return {
str(column["name"])
for column in sa.inspect(op.get_bind()).get_columns(_TABLE)
}
def _indexes() -> set[str]:
return {
str(index["name"])
for index in sa.inspect(op.get_bind()).get_indexes(_TABLE)
if index.get("name")
}
def _json_type() -> sa.types.TypeEngine:
return sa.Text() if op.get_bind().dialect.name == "mysql" else sa.JSON()
def _empty_reasons_value() -> str | list[str]:
# PortableJSON is TEXT on MySQL, where binding a Python list directly to
# a raw migration Text column is invalid. SQLite/PostgreSQL JSON columns
# should receive the native list so their type processors serialize it.
return "[]" if op.get_bind().dialect.name == "mysql" else []
def _target_key(
*,
user_id: str | None,
thread_id: str,
run_id: str,
message_id: str | None,
agent_id: str,
) -> str:
message_key = message_id or f"run:{run_id}"
raw = "\x1f".join(
(user_id or "", thread_id, message_key, agent_id or "lead_agent")
)
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
def _backfill() -> None:
bind = op.get_bind()
feedback = sa.table(
_TABLE,
sa.column("feedback_id", sa.String(64)),
sa.column("run_id", sa.String(64)),
sa.column("thread_id", sa.String(64)),
sa.column("user_id", sa.String(64)),
sa.column("message_id", sa.String(64)),
sa.column("agent_id", sa.String(128)),
sa.column("target_key", sa.String(64)),
sa.column("reasons", _json_type()),
sa.column("created_at", sa.DateTime(timezone=True)),
sa.column("updated_at", sa.DateTime(timezone=True)),
)
rows = list(
bind.execute(
sa.select(
feedback.c.feedback_id,
feedback.c.run_id,
feedback.c.thread_id,
feedback.c.user_id,
feedback.c.message_id,
feedback.c.agent_id,
feedback.c.created_at,
)
).mappings()
)
run_agents: dict[str, str] = {}
inspector = sa.inspect(bind)
if rows and inspector.has_table("runs"):
run_columns = {str(column["name"]) for column in inspector.get_columns("runs")}
if "assistant_id" in run_columns:
runs = sa.table(
"runs",
sa.column("run_id", sa.String(64)),
sa.column("assistant_id", sa.String(128)),
)
run_ids = sorted({str(row["run_id"]) for row in rows})
run_agents = {
str(row.run_id): str(row.assistant_id)
for row in bind.execute(
sa.select(runs.c.run_id, runs.c.assistant_id).where(
runs.c.run_id.in_(run_ids)
)
)
if row.assistant_id
}
seen_target_keys: set[str] = set()
for row in rows:
agent_id = str(
run_agents.get(str(row["run_id"]))
or row.get("agent_id")
or "lead_agent"
)[:128]
target_key = _target_key(
user_id=row.get("user_id"),
thread_id=str(row["thread_id"]),
run_id=str(row["run_id"]),
message_id=row.get("message_id"),
agent_id=agent_id,
)
# SQL unique indexes historically allowed several NULL-user rows.
# Keep those legacy records without blocking the new unique digest;
# one canonical row retains the deterministic future-upsert key.
if target_key in seen_target_keys:
legacy_raw = f"{target_key}\x1flegacy:{row['feedback_id']}"
target_key = hashlib.sha256(legacy_raw.encode("utf-8")).hexdigest()
else:
seen_target_keys.add(target_key)
bind.execute(
sa.update(feedback)
.where(feedback.c.feedback_id == row["feedback_id"])
.values(
agent_id=agent_id,
target_key=target_key,
reasons=_empty_reasons_value(),
updated_at=row.get("created_at"),
)
)
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TABLE):
return
columns = _columns()
if "agent_id" not in columns:
op.add_column(
_TABLE,
sa.Column(
"agent_id",
sa.String(length=128),
nullable=True,
server_default="lead_agent",
),
)
if "target_key" not in columns:
op.add_column(
_TABLE,
sa.Column("target_key", sa.String(length=64), nullable=True),
)
if "reasons" not in columns:
op.add_column(
_TABLE,
sa.Column("reasons", _json_type(), nullable=True),
)
if "updated_at" not in columns:
op.add_column(
_TABLE,
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
)
_backfill()
# SQLite needs a batch table rebuild for nullability changes; Alembic uses
# native ALTER statements on MySQL/PostgreSQL where possible.
with op.batch_alter_table(_TABLE) as batch:
batch.alter_column(
"agent_id",
existing_type=sa.String(length=128),
nullable=False,
server_default=None,
)
batch.alter_column(
"target_key",
existing_type=sa.String(length=64),
nullable=False,
)
batch.alter_column(
"reasons",
existing_type=_json_type(),
nullable=False,
)
batch.alter_column(
"updated_at",
existing_type=sa.DateTime(timezone=True),
nullable=False,
)
indexes = _indexes()
if _OLD_INDEX in indexes:
op.drop_index(_OLD_INDEX, table_name=_TABLE)
indexes = _indexes()
if _TARGET_INDEX not in indexes:
op.create_index(_TARGET_INDEX, _TABLE, ["target_key"], unique=True)
if _AGENT_INDEX not in indexes:
op.create_index(_AGENT_INDEX, _TABLE, ["agent_id", "created_at"])
def downgrade() -> None:
if not sa.inspect(op.get_bind()).has_table(_TABLE):
return
indexes = _indexes()
if _AGENT_INDEX in indexes:
op.drop_index(_AGENT_INDEX, table_name=_TABLE)
if _TARGET_INDEX in indexes:
op.drop_index(_TARGET_INDEX, table_name=_TABLE)
if _OLD_INDEX not in _indexes():
op.create_index(
_OLD_INDEX,
_TABLE,
["thread_id", "run_id", "user_id"],
unique=True,
mysql_length={"user_id": 63},
)
columns = _columns()
with op.batch_alter_table(_TABLE) as batch:
for name in ("updated_at", "reasons", "target_key", "agent_id"):
if name in columns:
batch.drop_column(name)
@@ -0,0 +1,25 @@
"""Merge feedback, public-document, and assistant-knowledge migration heads.
Revision ID: 20260916_03
Revises: 20260916_02, 20260909_01, 20260914_01
Create Date: 2026-09-16
This merge used to share revision id ``20260916_01`` with the LLMWiki graph
edge migration. Giving the no-op merge its own id preserves the graph-edge
revision for already stamped databases while restoring an unambiguous graph.
"""
from __future__ import annotations
revision = "20260916_03"
down_revision = ("20260916_02", "20260909_01", "20260914_01")
branch_labels = None
depends_on = None
def upgrade() -> None:
"""Record the merge; all parent revisions already own their DDL."""
def downgrade() -> None:
"""Splitting the migration history requires no schema change."""
@@ -0,0 +1,49 @@
"""Add editable starter questions and retrieval settings to LLMWiki mappings.
Revision ID: 20260920_01
Revises: 20260903_01, 20260907_01
Create Date: 2026-09-20
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20260920_01"
down_revision = ("20260903_01", "20260907_01")
branch_labels = None
depends_on = None
def _columns() -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("llmwiki_knowledge_bases"):
return set()
return {str(column["name"]) for column in inspector.get_columns("llmwiki_knowledge_bases")}
def upgrade() -> None:
columns = _columns()
if not columns:
return
if "test_questions" not in columns:
op.add_column(
"llmwiki_knowledge_bases",
sa.Column("test_questions", PortableJSON(), nullable=True),
)
if "retrieval_settings" not in columns:
op.add_column(
"llmwiki_knowledge_bases",
sa.Column("retrieval_settings", PortableJSON(), nullable=True),
)
def downgrade() -> None:
columns = _columns()
if "retrieval_settings" in columns:
op.drop_column("llmwiki_knowledge_bases", "retrieval_settings")
if "test_questions" in columns:
op.drop_column("llmwiki_knowledge_bases", "test_questions")
@@ -0,0 +1,194 @@
"""Reconcile LLMWiki schema after historical duplicate revision ids.
Revision ID: 20260920_02
Revises: 20260920_01, 20260916_03
Create Date: 2026-09-20
Older source snapshots used ``20260916_01`` and ``20260920_01`` twice. A
database stamped with either value can therefore be missing the schema from
the other migration that carried the same id. This merge is deliberately
idempotent and restores every affected table, constraint, index, and JSON
column before recording the new unambiguous head.
"""
from __future__ import annotations
from collections.abc import Iterable
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20260920_02"
down_revision = ("20260920_01", "20260916_03")
branch_labels = None
depends_on = None
_EDGE_TABLE = "llmwiki_wiki_edges"
_EDGE_UNIQUE = "uq_llmwiki_wiki_edge_mapping_pages_type"
_EDGE_COLUMNS = (
"knowledge_base_mapping_id",
"source_page_id",
"target_page_id",
"edge_type",
)
_EDGE_FOREIGN_KEYS = (
(
"fk_llmwiki_wiki_edges_mapping",
"knowledge_base_mapping_id",
"llmwiki_knowledge_bases",
),
(
"fk_llmwiki_wiki_edges_source_page",
"source_page_id",
"llmwiki_wiki_pages",
),
(
"fk_llmwiki_wiki_edges_target_page",
"target_page_id",
"llmwiki_wiki_pages",
),
)
_EDGE_INDEXES = (
(
"ix_llmwiki_wiki_edges_knowledge_base_mapping_id",
("knowledge_base_mapping_id",),
),
("ix_llmwiki_wiki_edges_source_page_id", ("source_page_id",)),
("ix_llmwiki_wiki_edges_target_page_id", ("target_page_id",)),
(
"ix_llmwiki_wiki_edge_mapping_source",
("knowledge_base_mapping_id", "source_page_id"),
),
(
"ix_llmwiki_wiki_edge_mapping_target",
("knowledge_base_mapping_id", "target_page_id"),
),
)
def _create_edges_table() -> None:
op.create_table(
_EDGE_TABLE,
sa.Column("id", sa.String(length=36), primary_key=True),
sa.Column("knowledge_base_mapping_id", sa.String(length=36), nullable=False),
sa.Column("source_page_id", sa.String(length=36), nullable=False),
sa.Column("target_page_id", sa.String(length=36), nullable=False),
sa.Column(
"edge_type",
sa.String(length=32),
nullable=False,
server_default="wiki_link",
),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["knowledge_base_mapping_id"],
["llmwiki_knowledge_bases.id"],
name="fk_llmwiki_wiki_edges_mapping",
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["source_page_id"],
["llmwiki_wiki_pages.id"],
name="fk_llmwiki_wiki_edges_source_page",
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["target_page_id"],
["llmwiki_wiki_pages.id"],
name="fk_llmwiki_wiki_edges_target_page",
ondelete="CASCADE",
),
sa.UniqueConstraint(*_EDGE_COLUMNS, name=_EDGE_UNIQUE),
)
def _foreign_key_shape(item: dict) -> tuple[tuple[str, ...], str | None, tuple[str, ...]]:
return (
tuple(str(name) for name in item.get("constrained_columns") or ()),
item.get("referred_table"),
tuple(str(name) for name in item.get("referred_columns") or ()),
)
def _ensure_edge_constraints(inspector: sa.Inspector) -> None:
unique_names = {
str(item["name"])
for item in inspector.get_unique_constraints(_EDGE_TABLE)
if item.get("name")
}
foreign_key_shapes = {
_foreign_key_shape(item) for item in inspector.get_foreign_keys(_EDGE_TABLE)
}
missing_foreign_keys = [
(name, column, referred_table)
for name, column, referred_table in _EDGE_FOREIGN_KEYS
if ((column,), referred_table, ("id",)) not in foreign_key_shapes
]
if _EDGE_UNIQUE in unique_names and not missing_foreign_keys:
return
with op.batch_alter_table(_EDGE_TABLE) as batch:
if _EDGE_UNIQUE not in unique_names:
batch.create_unique_constraint(_EDGE_UNIQUE, list(_EDGE_COLUMNS))
for name, column, referred_table in missing_foreign_keys:
batch.create_foreign_key(
name,
referred_table,
[column],
["id"],
ondelete="CASCADE",
)
def _ensure_edge_indexes(inspector: sa.Inspector) -> None:
index_names = {
str(item["name"])
for item in inspector.get_indexes(_EDGE_TABLE)
if item.get("name")
}
for name, columns in _EDGE_INDEXES:
if name not in index_names:
op.create_index(name, _EDGE_TABLE, list(columns))
def _ensure_edges() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table(_EDGE_TABLE):
_create_edges_table()
else:
_ensure_edge_constraints(inspector)
_ensure_edge_indexes(sa.inspect(bind))
def _columns(table: str) -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(table):
return set()
return {str(item["name"]) for item in inspector.get_columns(table)}
def _ensure_json_columns(table: str, columns: Iterable[str]) -> None:
existing = _columns(table)
if not existing:
return
for name in columns:
if name not in existing:
op.add_column(table, sa.Column(name, PortableJSON(), nullable=True))
def upgrade() -> None:
_ensure_edges()
_ensure_json_columns(
"llmwiki_knowledge_bases",
("test_questions", "retrieval_settings"),
)
_ensure_json_columns("llmwiki_wiki_sync_states", ("last_sync_result",))
def downgrade() -> None:
columns = _columns("llmwiki_wiki_sync_states")
if "last_sync_result" in columns:
op.drop_column("llmwiki_wiki_sync_states", "last_sync_result")
@@ -0,0 +1,39 @@
"""Persist DeerFlow-owned fixed parent directories for LLMWiki mappings.
Revision ID: 20261006_01
Revises: 20260920_02
Create Date: 2026-10-06
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20261006_01"
down_revision = "20260920_02"
branch_labels = None
depends_on = None
_TABLE = "llmwiki_knowledge_bases"
_COLUMN = "wiki_parent_directories"
def _columns() -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TABLE):
return set()
return {str(column["name"]) for column in inspector.get_columns(_TABLE)}
def upgrade() -> None:
columns = _columns()
if columns and _COLUMN not in columns:
op.add_column(_TABLE, sa.Column(_COLUMN, PortableJSON(), nullable=True))
def downgrade() -> None:
if _COLUMN in _columns():
op.drop_column(_TABLE, _COLUMN)
@@ -0,0 +1,40 @@
"""Persist administrator-managed per-model runtime capabilities.
Revision ID: 20261009_01
Revises: 20261006_01
Create Date: 2026-10-09
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20261009_01"
down_revision = "20261006_01"
branch_labels = None
depends_on = None
_TABLE = "model_capabilities"
def upgrade() -> None:
if sa.inspect(op.get_bind()).has_table(_TABLE):
return
op.create_table(
_TABLE,
sa.Column("model_name", sa.String(length=160), primary_key=True),
sa.Column(
"supports_reasoning_effort",
sa.Boolean(),
nullable=False,
server_default=sa.false(),
),
sa.Column("updated_by", sa.String(length=64), nullable=True),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
def downgrade() -> None:
if sa.inspect(op.get_bind()).has_table(_TABLE):
op.drop_table(_TABLE)
@@ -0,0 +1,41 @@
"""Persist the global ordinary-knowledge smart-reasoning switch.
Revision ID: 20261014_01
Revises: 20261009_01
Create Date: 2026-10-14
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20261014_01"
down_revision = "20261009_01"
branch_labels = None
depends_on = None
_TABLE = "knowledge_retrieval_settings"
def upgrade() -> None:
if sa.inspect(op.get_bind()).has_table(_TABLE):
return
op.create_table(
_TABLE,
sa.Column("scope", sa.String(length=64), primary_key=True),
sa.Column(
"smart_reasoning_enabled",
sa.Boolean(),
nullable=False,
server_default=sa.false(),
),
sa.Column("revision", sa.Integer(), nullable=False, server_default="1"),
sa.Column("updated_by", sa.String(length=64), nullable=True),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
def downgrade() -> None:
if sa.inspect(op.get_bind()).has_table(_TABLE):
op.drop_table(_TABLE)
@@ -0,0 +1,54 @@
"""Persist hierarchical Wiki generation configuration.
Revision ID: 20261014_02
Revises: 20261014_01
Create Date: 2026-10-14
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from deerflow.persistence.types import PortableJSON
revision = "20261014_02"
down_revision = "20261014_01"
branch_labels = None
depends_on = None
_TABLE = "llmwiki_knowledge_bases"
_COLUMNS: tuple[sa.Column, ...] = (
sa.Column("wiki_directory_paths", PortableJSON(), nullable=True),
sa.Column("wiki_focus_instructions", sa.Text(), nullable=True),
sa.Column("wiki_content_instructions_raw", sa.Text(), nullable=True),
sa.Column("wiki_extraction_instructions_raw", sa.Text(), nullable=True),
sa.Column("wiki_generation_managed", sa.Boolean(), nullable=True),
sa.Column("wiki_config_schema_version", sa.Integer(), nullable=True),
sa.Column("wiki_config_revision", sa.Integer(), nullable=True),
sa.Column("wiki_config_apply_status", sa.String(length=32), nullable=True),
sa.Column("wiki_config_apply_error", sa.Text(), nullable=True),
)
def _columns() -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TABLE):
return set()
return {str(column["name"]) for column in inspector.get_columns(_TABLE)}
def upgrade() -> None:
existing = _columns()
if not existing:
return
for column in _COLUMNS:
if column.name not in existing:
op.add_column(_TABLE, column)
def downgrade() -> None:
existing = _columns()
for column in reversed(_COLUMNS):
if column.name in existing:
op.drop_column(_TABLE, column.name)
@@ -0,0 +1,105 @@
"""Index task deep-link thread lookups outside the metadata JSON blob.
Revision ID: 20261015_01
Revises: 20260920_02
Create Date: 2026-10-15
``threads_meta.metadata_json`` remains the public source of truth. The new
columns are maintained projections for the high-frequency task workspace
lookup, whose task rows are shared across users and therefore cannot use the
ordinary ``user_id`` index.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect, text
revision = "20261015_01"
down_revision = "20260920_02"
branch_labels = None
depends_on = None
_TABLE = "threads_meta"
_COLUMNS = (
("task_id", 128),
("agent_id", 128),
("action_id", 128),
)
_INDEXES = (
("ix_threads_meta_task_agent_updated", ("task_id", "agent_id", "updated_at", "thread_id")),
("ix_threads_meta_task_action_agent_updated", ("task_id", "action_id", "agent_id", "updated_at", "thread_id")),
("ix_threads_meta_user_updated", ("user_id", "updated_at", "thread_id")),
)
def _has_table(table_name: str) -> bool:
return table_name in inspect(op.get_bind()).get_table_names()
def _has_column(column_name: str) -> bool:
return any(column["name"] == column_name for column in inspect(op.get_bind()).get_columns(_TABLE))
def _has_index(index_name: str) -> bool:
return index_name in {index["name"] for index in inspect(op.get_bind()).get_indexes(_TABLE)}
def _json_scalar_expression(key: str) -> str | None:
"""Return a portable-enough JSON scalar extractor for supported engines."""
dialect = op.get_bind().dialect.name
if dialect == "sqlite":
return f"json_extract(metadata_json, '$.{key}')"
if dialect == "mysql":
# PortableJSON is LONGTEXT on MySQL, but JSON_EXTRACT accepts valid
# JSON text and returns the quoted JSON scalar until unquoted here.
return f"JSON_UNQUOTE(JSON_EXTRACT(metadata_json, '$.{key}'))"
if dialect in {"postgresql", "opengauss"}:
return f"metadata_json ->> '{key}'"
return None
def _backfill_projections() -> None:
expressions = {
"task_id": _json_scalar_expression("taskId"),
"agent_id": _json_scalar_expression("agent_id"),
"action_id": _json_scalar_expression("actionId"),
}
if any(expression is None for expression in expressions.values()):
return
assignments = ", ".join(
f"{column} = {expression}"
for column, expression in expressions.items()
)
# Only fresh projection columns are NULL. This makes a retry safe and
# never overwrites values maintained by a newer running application.
op.get_bind().execute(
text(
f"UPDATE {_TABLE} SET {assignments} "
"WHERE task_id IS NULL AND agent_id IS NULL AND action_id IS NULL"
)
)
def upgrade() -> None:
if not _has_table(_TABLE):
return
for column_name, length in _COLUMNS:
if not _has_column(column_name):
op.add_column(_TABLE, sa.Column(column_name, sa.String(length), nullable=True))
_backfill_projections()
for index_name, columns in _INDEXES:
if not _has_index(index_name):
op.create_index(index_name, _TABLE, list(columns))
def downgrade() -> None:
if not _has_table(_TABLE):
return
for index_name, _columns in reversed(_INDEXES):
if _has_index(index_name):
op.drop_index(index_name, table_name=_TABLE)
for column_name, _length in reversed(_COLUMNS):
if _has_column(column_name):
op.drop_column(_TABLE, column_name)
@@ -0,0 +1,58 @@
"""Add dedicated single-agent task deep-link conversation index.
Revision ID: 20261019_01
Revises: 20261015_01
Create Date: 2026-10-19
This is intentionally schema-only. Existing ``threads_meta`` task rows are not
backfilled; only conversations created/updated with the new ``taskGoPath``
metadata marker enter the dedicated history.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20261019_01"
down_revision = "20261015_01"
branch_labels = None
depends_on = None
_TABLE = "task_deeplink_threads"
_INDEX = "ix_task_deeplink_threads_scope"
def _has_table() -> bool:
return _TABLE in inspect(op.get_bind()).get_table_names()
def upgrade() -> None:
if _has_table():
return
op.create_table(
_TABLE,
sa.Column("thread_id", sa.String(64), primary_key=True),
sa.Column("task_id", sa.String(128), nullable=False),
sa.Column("source_task_id", sa.String(128), nullable=False),
sa.Column("go_path", sa.String(16), nullable=False),
sa.Column("agent_id", sa.String(128), nullable=True),
sa.Column("created_by", sa.String(64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(
_INDEX,
_TABLE,
["task_id", "go_path", "updated_at", "thread_id"],
)
def downgrade() -> None:
if not _has_table():
return
indexes = {index["name"] for index in inspect(op.get_bind()).get_indexes(_TABLE)}
if _INDEX in indexes:
op.drop_index(_INDEX, table_name=_TABLE)
op.drop_table(_TABLE)
@@ -0,0 +1,90 @@
"""Persist the knowledge-management advanced-entry switch.
Revision ID: 20261020_01
Revises: 20261019_01, 20261014_02
Create Date: 2026-10-20
An earlier release accidentally reused revision ``20261019_01`` for this
column and for the task deep-link table. A database that ran the column
migration can therefore report the task migration as applied even though its
table is absent. The upgrade reconciles that table and its scope index before
ensuring the advanced-management column, so both fresh and affected databases
converge on the same schema. The task objects belong to the parent revision and
are intentionally left intact by this revision's downgrade.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20261020_01"
down_revision = ("20261019_01", "20261014_02")
branch_labels = None
depends_on = None
_TABLE = "knowledge_retrieval_settings"
_COLUMN = "advanced_management_enabled"
_TASK_TABLE = "task_deeplink_threads"
_TASK_INDEX = "ix_task_deeplink_threads_scope"
def _columns() -> set[str]:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TABLE):
return set()
return {str(column["name"]) for column in inspector.get_columns(_TABLE)}
def _ensure_task_deeplink_schema() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table(_TASK_TABLE):
op.create_table(
_TASK_TABLE,
sa.Column("thread_id", sa.String(64), primary_key=True),
sa.Column("task_id", sa.String(128), nullable=False),
sa.Column("source_task_id", sa.String(128), nullable=False),
sa.Column("go_path", sa.String(16), nullable=False),
sa.Column("agent_id", sa.String(128), nullable=True),
sa.Column("created_by", sa.String(64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(
_TASK_INDEX,
_TASK_TABLE,
["task_id", "go_path", "updated_at", "thread_id"],
)
return
indexes = {
str(index["name"])
for index in inspector.get_indexes(_TASK_TABLE)
if index.get("name")
}
if _TASK_INDEX not in indexes:
op.create_index(
_TASK_INDEX,
_TASK_TABLE,
["task_id", "go_path", "updated_at", "thread_id"],
)
def upgrade() -> None:
_ensure_task_deeplink_schema()
if _COLUMN in _columns():
return
op.add_column(
_TABLE,
sa.Column(
_COLUMN,
sa.Boolean(),
nullable=False,
server_default=sa.false(),
),
)
def downgrade() -> None:
if _COLUMN in _columns():
op.drop_column(_TABLE, _COLUMN)