Files
LangBot/tests/unit_tests/plugin/test_runner_stream_context.py
532b8ce204 feat(agent): add cross-platform interactive cards for QQ, WeCom, Feishu and DingTalk (#2787)
* feat(agent): add cross-platform interactive card tool

* fix(agent): create pipelines with selected runner defaults

* fix(agent): enable reliable cross-platform card interactions

* feat(dingtalk): deliver A2UI cards and restore agent replies

* Fix DingTalk A2UI form submissions and agent replies

* Restore LangBot branding on DingTalk A2UI cards

* Use compatible Markdown link in DingTalk A2UI branding

* Place LangBot branding above DingTalk card content

* Stream DingTalk replies without a source message

* Move DingTalk branding into compact card footer

* fix(wecombot): handle native select card callbacks

* fix(wecombot): stream replies after HITL cards

* fix(dingtalk): show submitted HITL choices in cards

* fix(lark): stream replies after HITL submissions

* fix(plugins): refresh LocalAgent source hashes

* fix(dify): merge HITL resume streaming into in-tree plugin

* feat(interactions): resume runner in place after card submission

* fix(interactions): respect live adapter APIs and preserve deadlines

* fix(ci): lock blocking interaction SDK for dependent checks

* test(runner): keep legacy SDK probe importable

* fix(interactions): align adapter manifests and close timed-out streams

* fix(agent): avoid duplicate replies and enable LocalAgent cards

* chore(plugins): sync LocalAgent catalog version

* refactor(agent): unify blocking interaction callbacks and adapters

* chore(deps): pin blocking interaction SDK revision

* test(agent): remove obsolete async fixture mark

* test(interaction): update legacy admission assertions

* fix(qqofficial): keep card callbacks responsive

* fix(runner): keep blocking card interactions alive

* ci(plugins): expect SDK 0.7.20 in Runner workflows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* ci(plugins): test coding Runners against SDK 0.7.20

Claude Code and Codex now call ctx.request_interaction, first released in 0.7.20.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* fix(discord): deliver guild interaction cards to the originating channel

Pipeline group conversations are keyed by guild id, so send_interaction
targeted the guild and Discord returned Unknown Channel. Resolve the channel
from the message being answered, edit cards in that channel, and report the
card's conversation on callbacks so the Host scope check matches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: RockChinQ <rockchinq@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-11-03 21:45:49 +08:00

175 lines
6.7 KiB
Python

"""Regression coverage for installation scopes across async-generator resumes."""
import asyncio
import contextvars
import time
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from langbot.pkg.agent.runner.errors import RunnerExecutionError
from langbot.pkg.agent.runner.invoker import RunnerInvoker
from langbot.pkg.plugin.handler import RuntimeConnectionHandler
from langbot.pkg.plugin.connector import PluginRuntimeConnector
from tests.unit_tests.plugin.test_connector_methods import (
TEST_EXECUTION_CONTEXT,
TEST_INSTALLATION_BINDING,
create_mock_connector,
)
def make_stream(*, deadline=True, failure=None, blocked=False):
handler = object.__new__(RuntimeConnectionHandler)
handler._outbound_installation_context = contextvars.ContextVar('test_installation', default=None)
entered = asyncio.Event()
closed = []
observed = []
async def wire_stream(*args, **kwargs):
try:
for index in range(3):
observed.append(handler._outbound_installation_context.get())
if index == 1:
entered.set()
if blocked:
await asyncio.Event().wait()
if failure:
raise failure
yield {'type': 'message.delta', 'sequence': index}
finally:
closed.append(handler._outbound_installation_context.get())
handler.call_action_generator = wire_stream
connector = create_mock_connector()
connector.handler = handler
invoker = RunnerInvoker(SimpleNamespace(plugin_connector=connector, logger=Mock()))
descriptor = SimpleNamespace(
id='plugin:qa/runner/default', plugin_author='qa', plugin_name='runner', runner_name='default'
)
context = {
'conversation': {'workspace_id': TEST_EXECUTION_CONTEXT.workspace_uuid},
'runtime': {'deadline_at': time.time() + 10 if deadline else None},
}
return invoker.invoke(descriptor, context), handler, context, entered, closed, observed
@pytest.mark.asyncio
@pytest.mark.parametrize('deadline', [True, False])
async def test_scope_is_reset_before_yield_and_stream_finishes(deadline):
stream, handler, _, _, closed, observed = make_stream(deadline=deadline)
frames = []
async for frame in stream:
frames.append(frame)
assert handler._outbound_installation_context.get() is None
await asyncio.sleep(0)
assert [frame['sequence'] for frame in frames] == [0, 1, 2]
assert observed == [TEST_INSTALLATION_BINDING] * 3
assert closed == [TEST_INSTALLATION_BINDING]
@pytest.mark.asyncio
async def test_runner_transport_wait_has_no_stale_idle_deadline():
stream, handler, _, _, _, _ = make_stream()
original = handler.call_action_generator
timeouts = []
async def capture_timeout(*args, **kwargs):
timeouts.append(kwargs['timeout'])
async for frame in original(*args, **kwargs):
yield frame
handler.call_action_generator = capture_timeout
assert [frame['sequence'] async for frame in stream] == [0, 1, 2]
assert timeouts == [None]
@pytest.mark.asyncio
@pytest.mark.parametrize('deadline', [True, False])
async def test_early_close_releases_wire_stream_in_scope(deadline):
stream, handler, _, _, closed, _ = make_stream(deadline=deadline)
await anext(stream)
await stream.aclose()
assert closed == [TEST_INSTALLATION_BINDING]
assert handler._outbound_installation_context.get() is None
@pytest.mark.asyncio
async def test_deadline_expired_between_frames_closes_in_scope():
stream, handler, context, _, closed, _ = make_stream()
await anext(stream)
context['runtime']['deadline_at'] = time.time() - 1
with pytest.raises(RunnerExecutionError) as exc:
await anext(stream)
assert exc.value.error_code == 'runner.timeout'
assert closed == [TEST_INSTALLATION_BINDING]
assert handler._outbound_installation_context.get() is None
@pytest.mark.asyncio
async def test_timeout_during_next_frame_keeps_timeout_error():
stream, handler, context, _, closed, _ = make_stream(blocked=True)
await anext(stream)
context['runtime']['deadline_at'] = time.time() + 0.05
with pytest.raises(RunnerExecutionError) as exc:
await anext(stream)
assert exc.value.error_code == 'runner.timeout'
assert closed == [TEST_INSTALLATION_BINDING]
assert handler._outbound_installation_context.get() is None
@pytest.mark.asyncio
async def test_cancellation_preserves_cancelled_error_and_cleans_up():
stream, handler, _, entered, closed, _ = make_stream(blocked=True)
await anext(stream)
task = asyncio.create_task(anext(stream))
await asyncio.wait_for(entered.wait(), 1)
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
assert closed == [TEST_INSTALLATION_BINDING]
assert handler._outbound_installation_context.get() is None
@pytest.mark.asyncio
async def test_transport_error_is_not_masked_by_context_reset():
stream, handler, _, _, closed, _ = make_stream(failure=RuntimeError('wire failed'))
await anext(stream)
with pytest.raises(RunnerExecutionError, match='wire failed'):
await anext(stream)
assert closed == [TEST_INSTALLATION_BINDING]
assert handler._outbound_installation_context.get() is None
@pytest.mark.asyncio
async def test_interleaved_installations_share_handler_without_scope_leakage():
handler = object.__new__(RuntimeConnectionHandler)
handler._outbound_installation_context = contextvars.ContextVar('shared_installation', default=None)
other_binding = TEST_INSTALLATION_BINDING.model_copy(
update={'installation_uuid': '00000000-0000-4000-8000-000000000002'}
)
closed = []
async def wire(binding):
try:
for index in range(3):
assert handler._outbound_installation_context.get() == binding
await asyncio.sleep(0)
assert handler._outbound_installation_context.get() == binding
yield index
finally:
assert handler._outbound_installation_context.get() == binding
closed.append(binding)
async def consume(binding):
stream = PluginRuntimeConnector._installation_scoped_stream(handler, binding, wire(binding))
for index in range(3):
assert await asyncio.wait_for(anext(stream), 1) == index
assert handler._outbound_installation_context.get() is None
# Closing in yet another task must also restore that task's context.
await asyncio.wait_for(stream.aclose(), 1)
await asyncio.gather(consume(TEST_INSTALLATION_BINDING), consume(other_binding))
assert set(closed) == {TEST_INSTALLATION_BINDING, other_binding}
assert handler._outbound_installation_context.get() is None