Skip to content

The agent that grows with you

fix(cron): in_channel thread-flatten uses the seed's gate (origin_target)

Ben Barclay20c56f82b86c
diff --git a/cron/scheduler.py b/cron/scheduler.py
index fb4926c4c51..fca8b49c3a3 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -2821,7 +2821,7 @@ def _deliver_result(job: dict, content: str, adapters=None, loop=None) -> Option
)
in_channel_surface = False
- if in_channel_surface and mirror_this_target and live_adapter_ready:
+ if in_channel_surface and origin_target and live_adapter_ready:
# Force flat delivery (D2): the continuable-channel target must
# ignore any inherited origin/target thread_id, or the flat
# continuable session seeded below (thread_id=None, via
@@ -2830,6 +2830,14 @@ def _deliver_result(job: dict, content: str, adapters=None, loop=None) -> Option
# reads `thread_id` and would otherwise route into the origin
# thread instead of flat into the channel.
#
+ # Gated on `origin_target`, NOT `mirror_this_target`: the seed
+ # below fires on origin-match alone (in_channel is the
+ # continuation surface, independent of the attach_to_session /
+ # mirror opt-in), so the flatten must use the SAME gate — with
+ # the default knobs off, a mirror-gated flatten kept delivering
+ # into the origin thread while the flat session got seeded,
+ # leaving the brief and its continuation surface in different
+ # places.
# Gated on `live_adapter_ready` (adapter present AND a running loop)
# so the clear fires ONLY on the live-send path that actually seeds
# the flat session — the SAME condition as the live-send block
diff --git a/tests/cron/test_scheduler.py b/tests/cron/test_scheduler.py
index 5fb30cc47c3..d35e87014b2 100644
--- a/tests/cron/test_scheduler.py
+++ b/tests/cron/test_scheduler.py
@@ -2468,6 +2468,44 @@ class TestCronContinuableSurfaceInChannel:
# session key includes it on per-user-isolated chats.
assert seed_mock.call_args.kwargs.get("user_id") == "U_HUMAN"
+ def test_in_channel_flattens_thread_without_attach_to_session(self):
+ """REGRESSION: the thread-id-clearing gate was `mirror_this_target`
+ while the seed below had been decoupled to origin-match alone. With
+ the default knobs off (attach_to_session=False, cron.mirror_delivery
+ unset) and an origin carrying a REAL thread_id, the brief still
+ delivered INTO the origin thread while the flat (thread_id=None)
+ session got seeded — brief and continuation surface in different
+ places. The flatten must use the same gate as the seed: origin_target.
+ Asserts on the routed DeliveryTarget, not just that the seed ran."""
+ captured = {}
+
+ class _SpyRouter:
+ def __init__(self, *a, **k):
+ pass
+
+ async def _deliver_to_platform(self, target, text, metadata):
+ captured["target"] = target
+ return {"success": True, "message_id": "msg_1"}
+
+ adapter = self._slack_adapter(supports_inchannel=True)
+ origin_with_thread = {
+ "platform": "slack", "chat_id": "C123", "user_id": "U_HUMAN",
+ # Genuine origin thread (job created from inside a thread).
+ "thread_id": "1787188000.000100",
+ }
+ with patch("gateway.delivery.DeliveryRouter", _SpyRouter), \
+ patch("cron.scheduler._seed_cron_channel_session", return_value=True) as seed_mock:
+ self._run_inchannel_delivery(
+ {"slack": {"cron_continuable_surface": "in_channel"}}, adapter,
+ attach_to_session=False, origin=origin_with_thread,
+ )
+ seed_mock.assert_called_once()
+ assert captured["target"].thread_id is None, (
+ "in_channel delivery routed into the origin thread "
+ f"({captured['target'].thread_id}) — the flat seeded session "
+ "does not match where the brief actually landed"
+ )
+
def test_seed_mirrors_into_exact_created_session_on_populated_chat(self):
"""REGRESSION (live, Alice 2026-08-19 19:17): 'in_channel seed did NOT
land'. The seed created the flat session row, then mirror_to_session