deer-flow/backend/tests/test_channel_leader_lease_config.py
Nan Gao d18d2937e7
feat(channels): add channels.leader_lease and gate an explicitly disabled lease under multi-process signals (#6746)
* feat(channels): add channels.leader_lease and gate it under multi-process signals

IM channels other than GitHub connect outbound to their platform. With more
than one Gateway instance each one opens that connection with the same bot
credentials: Telegram rejects the second poller with 409, Discord answers every
message twice and per-process ChannelManager state diverges. The multi-process
startup gate said nothing about channels; the only guard was a comment in the
Helm values.

Add the `channels.leader_lease` section (`enabled: auto|true|false`,
`lease_seconds`, `grace_seconds`; startup-only, registered in the reload
boundary) that the per-provider leader lease will read, validated by a Pydantic
model from the non-schema `channels` mapping. The gate now refuses
`enabled: false` under a multi-process signal while a non-GitHub provider is
enabled under `channels.*` or connectable through `channel_connections`, naming
the providers and the rollback; a malformed block is refused on every topology.
`auto` follows the multi-process signal, so a single instance is unchanged.

ChannelService strips the block before its per-provider loops and exposes it
as `leader_lease`; `ensure_ready_channels` only visits registered providers.
The two-Gateway harness overlay leaves the block alone. config_version 60 in
the example, the Helm chart and its README; docs replace the comment-only
"keep 1 replica" guidance with the gate.

* docs(changelog): link #6746

* fix(channels): stop recommending the unimplemented leader lease as a remedy

Review on #6746: the refusal told operators to switch channels.leader_lease
from false to auto/true "so one instance owns each provider's connection", but
the transport arbitration ships separately, so that change only bypassed the
clause and every worker still started the same bot.

The refusal now offers only the remedies that work today (a single instance, or
disabling the channels) and states that auto/true are reserved for the lease
that ships separately. A regression helper in the gate tests asserts the
remedies start with the rollback and that auto/true are never offered. The
config example, Helm values/README, CONFIGURATION.md, README, the deployment
guide (en/zh), the model docstrings and both changelogs describe auto/true as
reserved settings rather than an ownership guarantee.

* fix(channels): warn on an enabled unknown channel key; reword the changelog entry

Review on #6746: the registry guard in ensure_ready_channels skipped a
misspelled provider key (channels.telegam) silently, where it previously
reached _start_channel and logged 'Unknown channel type'. An enabled dict
block that is neither a registered provider nor leader_lease now logs a
warning that lists the registry.

The changelog entry claimed the chart and docs drop the "keep 1 replica"
guidance; they keep it (auto/true do not make several replicas safe yet) and
only stop relying on the comment alone.

* fix(dev): disable UI channel providers in multi-instance overlay

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-10-12 23:05:16 +08:00

101 lines
4.0 KiB
Python

"""``channels.leader_lease`` configuration model and its reload-boundary registration."""
from __future__ import annotations
from types import SimpleNamespace
import pytest
from deerflow.config.channel_leader_lease_config import (
CHANNEL_LEADER_LEASE_FIELD,
ChannelLeaderLeaseConfig,
ChannelLeaderLeaseConfigError,
channel_leader_lease_config,
channel_leader_lease_from_app_config,
)
from deerflow.config.reload_boundary import STARTUP_ONLY_FIELDS, is_startup_only_field
def test_defaults_are_auto_30_10():
config = ChannelLeaderLeaseConfig()
assert config.enabled == "auto"
assert config.lease_seconds == 30
assert config.grace_seconds == 10
assert config.explicitly_disabled is False
@pytest.mark.parametrize(
("enabled", "multi_process", "expected"),
[
("auto", False, False),
("auto", True, True),
(True, False, True),
(True, True, True),
(False, False, False),
(False, True, False),
],
)
def test_auto_follows_the_multi_process_signal(enabled, multi_process, expected):
config = ChannelLeaderLeaseConfig(enabled=enabled)
assert config.resolve(multi_process=multi_process) is expected
def test_only_a_literal_false_counts_as_explicitly_disabled():
assert ChannelLeaderLeaseConfig(enabled=False).explicitly_disabled is True
assert ChannelLeaderLeaseConfig(enabled=True).explicitly_disabled is False
assert ChannelLeaderLeaseConfig(enabled="auto").explicitly_disabled is False
def test_missing_section_and_empty_block_mean_defaults():
assert channel_leader_lease_config(None) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_config({}) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_config({"telegram": {"enabled": True}}) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_config({"leader_lease": None}) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_config({"leader_lease": {}}) == ChannelLeaderLeaseConfig()
def test_block_values_are_read():
config = channel_leader_lease_config({"leader_lease": {"enabled": False, "lease_seconds": 15, "grace_seconds": 0}})
assert config.enabled is False
assert config.lease_seconds == 15
assert config.grace_seconds == 0
@pytest.mark.parametrize(
("block", "fragment"),
[
({"enabled": "maybe"}, "enabled"),
({"lease_seconds": 0}, "lease_seconds"),
({"lease_seconds": 2}, "lease_seconds"),
({"grace_seconds": -1}, "grace_seconds"),
({"enable": False}, "enable"), # typo: extra keys are refused, not ignored
],
)
def test_invalid_blocks_name_the_dotted_path_and_the_field(block, fragment):
with pytest.raises(ChannelLeaderLeaseConfigError) as exc_info:
channel_leader_lease_config({"leader_lease": block})
message = str(exc_info.value)
assert CHANNEL_LEADER_LEASE_FIELD in message
assert fragment in message
def test_non_mapping_block_is_refused():
with pytest.raises(ChannelLeaderLeaseConfigError, match="must be a mapping"):
channel_leader_lease_config({"leader_lease": "auto"})
def test_from_app_config_reads_model_extra_and_tolerates_doubles():
app_config = SimpleNamespace(model_extra={"channels": {"leader_lease": {"enabled": True}}})
assert channel_leader_lease_from_app_config(app_config).enabled is True
# SimpleNamespace without model_extra, and an AppConfig-like object with no channels section.
assert channel_leader_lease_from_app_config(SimpleNamespace()) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_from_app_config(SimpleNamespace(model_extra={})) == ChannelLeaderLeaseConfig()
assert channel_leader_lease_from_app_config(SimpleNamespace(model_extra=None)) == ChannelLeaderLeaseConfig()
def test_leader_lease_is_registered_as_startup_only():
assert is_startup_only_field(CHANNEL_LEADER_LEASE_FIELD)
reason = STARTUP_ONLY_FIELDS[CHANNEL_LEADER_LEASE_FIELD]
assert "_enforce_postgres_for_multi_worker" in reason
assert "ChannelService" in reason