mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-09-17 01:56:18 +00:00
* feat(extensions): let an out-of-tree extension observe what the agent did
DeerFlow's extension system can contribute middleware, services and routes,
but an extension cannot answer basic questions about a run without reaching
into host internals. Several of the facts it would need are destroyed by the
operations that produce them:
* The middleware chain injects and rewrites a lot of context — date
reminders, recalled memory, compaction summaries, durable-context data,
image payloads, activated skill bodies. Downstream, none of it is
attributable: at the model-call boundary an injected HumanMessage is
indistinguishable from the user's own, and anything wanting to tell them
apart has to pattern-match prompt wording, which breaks on the next copy
edit.
* Two runs of "the same agent" are only comparable if the chain enforced the
same limits, prompts and thresholds. Recovering that from outside means
reading private attributes and guessing which of them change behaviour — a
guess that rots silently as middlewares gain fields.
* The lead-agent factory resolves a model after runtime overrides, renders a
prompt, filters tools through authorization and composes a stack, all
inside one synchronous call, and none of it survives: a middleware sees its
neighbours but not the prompt, the run worker sees a graph but not what
went into it.
* Summarization is destructive by design. N messages leave the context and
one summary enters it; afterwards only the summary exists, so "which
messages became this?" is not reconstructible.
This adds seven neutral facilities so those facts are recorded where they are
still true, and releases the contract package as 0.2.0.
Message provenance
Producers stamp `deerflow_content_kind` / `deerflow_producer_kind` onto the
messages they inject or rewrite. Stamping is unconditional — a fact whose
presence depends on whether an observer is installed is not a fact — and the
keys are server-owned, so provenance cannot be forged from a request.
Middleware self-description
Twelve middlewares declare their own behaviour-affecting parameters through
a duck-typed `release_policy_parameters()`. Long text is hashed rather than
embedded: a declaration is an identity, not a copy of the prompt.
Agent assembly descriptor
`assemble_lead_agent()` returns the graph plus a descriptor whose fingerprint
answers "did anything about this agent change between these two runs?".
`make_lead_agent()` keeps its graph-only signature — it is the LangGraph
Server ABI declared in langgraph.json. Tools and skills are sorted before
hashing because their assembly order is incidental; middlewares are not,
because stack order decides what wraps what. Host build identity is reported
but excluded from the fingerprint, so a redeploy does not invalidate every
agent's identity.
Context compaction observation
Summarization emits the content hashes of the messages it is about to remove
joined to the summary that replaced them. Content is the only identity
available at that seam: the summary does not become a message, and what later
projects it into a request renders it bounded and escaped rather than
verbatim.
Neutral policy, transform and MCP-source facts
Guardrail decisions are published to runtime context under a `__`-prefixed
key; result-rewriting middlewares append a declared, ordered transform trail;
MCP tools carry their credential-free logical origin.
Extension route identity
Contributed routes are session-authenticated and cannot opt out, but
"logged in" and "administrator" are different questions. Extensions get a
neutral projection of the caller rather than the host's auth context, and
`require_admin` fails closed when identity cannot be determined.
Extension-owned tables
An extension that persists data owns its own MetaData and migration chain, so
its tables are absent from Base.metadata and `alembic revision --autogenerate`
proposes dropping them. Extensions declare a table prefix, which is rejected
at registration if it would shadow a host table.
The contract package stays dependency-free and imports no host code; every new
Protocol method has a default so later additions remain additive. The loader's
pre-1.0 rule requires an exact major.minor match, so extensions written against
0.1 are now refused at startup with an actionable install hint rather than
loading into a host that implements a different surface.
uv.lock records the contract package's new version, so `uv sync --locked` still
resolves on a fresh checkout.
* fix(backend): sort gateway service imports
132 lines
5.3 KiB
Python
132 lines
5.3 KiB
Python
"""Alembic environment for DeerFlow application tables.
|
|
|
|
ONLY manages DeerFlow's tables (runs, threads_meta, feedback, users,
|
|
run_events, channel_connections, channel_credentials, channel_oauth_states,
|
|
channel_conversations).
|
|
|
|
LangGraph's checkpointer tables (``checkpoints``, ``checkpoint_blobs``,
|
|
``checkpoint_writes``, ``checkpoint_migrations``) are managed by LangGraph
|
|
itself -- they have their own schema lifecycle and must not be touched by
|
|
Alembic. The ``include_object`` filter below explicitly excludes them so a
|
|
future ``alembic revision --autogenerate`` will not emit ``drop_table`` for
|
|
tables it does not own.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from deerflow.persistence.base import Base
|
|
from deerflow.persistence.migrations._env_filters import (
|
|
LANGGRAPH_OWNED_TABLES,
|
|
include_object,
|
|
register_configured_extension_table_prefixes,
|
|
)
|
|
|
|
# Re-export under the module namespace for any consumer that addresses them
|
|
# via ``env.LANGGRAPH_OWNED_TABLES`` / ``env.include_object``.
|
|
__all__ = ["LANGGRAPH_OWNED_TABLES", "include_object"]
|
|
|
|
# Import all models so metadata is populated.
|
|
try:
|
|
import deerflow.persistence.models as models # register ORM models with Base.metadata
|
|
|
|
_ = models
|
|
except ImportError:
|
|
# Models not available — migration will work with existing metadata only.
|
|
logging.getLogger(__name__).warning("Could not import deerflow.persistence.models; Alembic may not detect all tables")
|
|
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# This process never starts a Gateway, so ``load_extensions()`` has not run and
|
|
# ``EXTENSION_TABLE_PREFIXES`` would be empty in the one place ``include_object``
|
|
# reads it. Read the declarations straight from config instead; extension code
|
|
# is never imported here.
|
|
_extension_prefixes = register_configured_extension_table_prefixes()
|
|
if _extension_prefixes:
|
|
logging.getLogger(__name__).info("alembic: excluding extension-owned tables with prefixes %s", ", ".join(sorted(_extension_prefixes)))
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
render_as_batch=True,
|
|
include_object=include_object,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection):
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
render_as_batch=True, # Required for SQLite ALTER TABLE support
|
|
include_object=include_object,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
# When a custom Postgres schema is configured, pin the alembic-spawned
|
|
# engine's search_path to it. This engine is built from the bare URL and
|
|
# does NOT inherit the asyncpg ``server_settings`` the app engine sets via
|
|
# ``connect_args``, so without this both ``alembic_version`` and every
|
|
# migration's DDL would land in the default (``public``) schema while the
|
|
# ORM tables land in the custom schema. ``init_engine`` has already created
|
|
# the schema (``CREATE SCHEMA IF NOT EXISTS``) before bootstrap runs.
|
|
pg_schema = config.get_main_option("deerflow_pg_schema")
|
|
connect_args: dict = {}
|
|
# Accept both the canonical ``postgresql`` scheme and libpq's ``postgres``
|
|
# short scheme (with or without a SQLAlchemy ``+driver`` suffix) so a
|
|
# ``postgres://`` DSN still gets its search_path pinned instead of silently
|
|
# writing ``alembic_version`` + migration DDL to the default schema.
|
|
if pg_schema and url and url.split("+", 1)[0].split(":", 1)[0] in {"postgresql", "postgres"}:
|
|
from deerflow.persistence.postgres_schema import build_asyncpg_connect_args
|
|
|
|
connect_args = build_asyncpg_connect_args(pg_schema)
|
|
|
|
connectable = create_async_engine(url, connect_args=connect_args)
|
|
|
|
# Cross-process bootstrap safety for SQLite: every connection alembic
|
|
# opens needs a wide ``busy_timeout`` so that when another process holds
|
|
# the file write lock (e.g. mid-bootstrap), our writes wait instead of
|
|
# raising ``database is locked``. The production engine in
|
|
# ``deerflow.persistence.engine`` sets this on its own connections, but
|
|
# alembic spawns its OWN engine here -- those connections wouldn't inherit
|
|
# anything unless we wire the same hook on this one.
|
|
if connectable.url.drivername.startswith("sqlite"):
|
|
from sqlalchemy import event
|
|
|
|
@event.listens_for(connectable.sync_engine, "connect")
|
|
def _alembic_sqlite_busy_timeout(dbapi_conn, _record): # noqa: ARG001
|
|
cursor = dbapi_conn.cursor()
|
|
try:
|
|
cursor.execute("PRAGMA busy_timeout=30000;")
|
|
finally:
|
|
cursor.close()
|
|
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
await connectable.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_migrations_online())
|