Skip to content

The agent that grows with you

fix(relay): D6 in_channel capability gate resolves the destination platform's descriptor

Ben Barclay162b23c3e228
diff --git a/cron/scheduler.py b/cron/scheduler.py
index fca8b49c3a3..cf13a009d03 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -2810,16 +2810,34 @@ def _deliver_result(job: dict, content: str, adapters=None, loop=None) -> Option
# consumer; "first consumer ≠ definition").
surface_mode = _resolve_cron_surface_mode(pconfig, platform_name)
in_channel_surface = surface_mode == "in_channel"
- if in_channel_surface and runtime_adapter is not None and not getattr(
- runtime_adapter, "supports_inchannel_continuable", False
- ):
- # Fail safe (D6): platform has no in_channel continuation primitive.
- logger.debug(
- "Job '%s': cron_continuable_surface=in_channel not supported on "
- "%s, using thread",
- job.get("id", "?"), platform_name,
+ if in_channel_surface and runtime_adapter is not None:
+ # Per-platform capability first: one RelayAdapter fronts N
+ # platforms and the connector advertises the bit per platform at
+ # handshake — the scalar attr only carries the PRIMARY identity's
+ # bit. Native adapters (no per-platform query) keep the class
+ # attribute path unchanged.
+ per_platform_check = getattr(
+ runtime_adapter, "supports_inchannel_continuable_for_platform",
+ None,
)
- in_channel_surface = False
+ if callable(per_platform_check):
+ try:
+ surface_supported = bool(per_platform_check(platform_name))
+ except Exception:
+ surface_supported = False
+ else:
+ surface_supported = bool(getattr(
+ runtime_adapter, "supports_inchannel_continuable", False
+ ))
+ if not surface_supported:
+ # Fail safe (D6): platform has no in_channel continuation
+ # primitive.
+ logger.debug(
+ "Job '%s': cron_continuable_surface=in_channel not supported on "
+ "%s, using thread",
+ job.get("id", "?"), platform_name,
+ )
+ in_channel_surface = False
if in_channel_surface and origin_target and live_adapter_ready:
# Force flat delivery (D2): the continuable-channel target must
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.py
index dd5e05ea4f6..59b63e66fe2 100644
--- a/gateway/relay/adapter.py
+++ b/gateway/relay/adapter.py
@@ -716,6 +716,36 @@ class RelayAdapter(BasePlatformAdapter):
"""Backward-compatible internal alias for follow-up routing."""
return self.fronts_platform(platform)
+ def supports_inchannel_continuable_for_platform(self, platform: Any) -> bool:
+ """Whether ONE fronted logical platform can host the flat continuable
+ cron surface (the D6 gate in cron/scheduler.py).
+
+ The scalar ``supports_inchannel_continuable`` carries only the PRIMARY
+ identity's bit, but one RelayAdapter fronts N platforms and the
+ connector advertises the capability per platform at handshake. On a
+ multi-platform relay the scalar both leaks the primary's True onto
+ platforms whose own descriptor never advertised it and suppresses a
+ non-primary platform's advertised True. Resolve the platform's own
+ negotiated descriptor off the transport; fall back to the scalar only
+ when the per-platform descriptor is unavailable (single-platform
+ transport, or a transport predating descriptor_for_platform).
+ """
+ platform_value = str(getattr(platform, "value", platform) or "")
+ if platform_value and self._transport is not None:
+ resolve = getattr(self._transport, "descriptor_for_platform", None)
+ if callable(resolve):
+ try:
+ per_platform = resolve(platform_value)
+ except Exception: # noqa: BLE001 - capability lookup must never break delivery
+ per_platform = None
+ if per_platform is not None:
+ return bool(
+ getattr(
+ per_platform, "supports_inchannel_continuable", False
+ )
+ )
+ return bool(self.supports_inchannel_continuable)
+
async def on_interrupt(self, session_key: str, chat_id: str) -> None:
"""Bridge a connector-delivered /stop into the adapter's interrupt path.
diff --git a/tests/relay/test_relay_inchannel_continuable.py b/tests/relay/test_relay_inchannel_continuable.py
index 04ecf4b24dd..213a012331a 100644
--- a/tests/relay/test_relay_inchannel_continuable.py
+++ b/tests/relay/test_relay_inchannel_continuable.py
@@ -95,6 +95,54 @@ class TestRelayAdapterCapabilityMapping:
assert adapter.supports_inchannel_continuable is True
+class TestPerPlatformCapability:
+ """One RelayAdapter fronts N platforms; the D6 gate must read the
+ DESTINATION platform's negotiated descriptor, not the primary identity's
+ scalar — the connector advertises the bit per platform at handshake."""
+
+ class _Transport:
+ def __init__(self, by_platform):
+ self._by_platform = by_platform
+ self._identities = [(p, "hermes") for p in by_platform]
+
+ def descriptor_for_platform(self, platform):
+ return self._by_platform.get(platform)
+
+ def _adapter(self, primary, by_platform):
+ from gateway.config import PlatformConfig
+ from gateway.relay.adapter import RelayAdapter
+
+ config = PlatformConfig(enabled=True, extra={})
+ return RelayAdapter(config, primary, transport=self._Transport(by_platform))
+
+ def test_primary_true_does_not_leak_onto_other_platform(self):
+ """Slack-primary (capable) + Discord fronted (not advertised):
+ Discord must NOT inherit Slack's bit through the scalar."""
+ slack = _descriptor(supports_inchannel_continuable=True)
+ discord = _descriptor(platform="discord", label="Discord",
+ markdown_dialect="markdown")
+ adapter = self._adapter(slack, {"slack": slack, "discord": discord})
+ assert adapter.supports_inchannel_continuable_for_platform("slack") is True
+ assert adapter.supports_inchannel_continuable_for_platform("discord") is False
+
+ def test_nonprimary_advertised_bit_is_honored(self):
+ """Discord-primary (not capable) + Slack fronted (advertised):
+ Slack's own descriptor must win over the primary scalar False."""
+ discord = _descriptor(platform="discord", label="Discord",
+ markdown_dialect="markdown")
+ slack = _descriptor(supports_inchannel_continuable=True)
+ adapter = self._adapter(discord, {"slack": slack, "discord": discord})
+ assert adapter.supports_inchannel_continuable_for_platform("slack") is True
+ assert adapter.supports_inchannel_continuable_for_platform("discord") is False
+
+ def test_unknown_platform_falls_back_to_scalar(self):
+ """A platform the transport has no descriptor for (or a transport
+ predating descriptor_for_platform) keeps the scalar behavior."""
+ slack = _descriptor(supports_inchannel_continuable=True)
+ adapter = self._adapter(slack, {"slack": slack})
+ assert adapter.supports_inchannel_continuable_for_platform("matrix") is True
+
+
class TestSurfaceKnobResolution:
"""_resolve_cron_surface_mode reads the knob from BOTH config shapes."""