Repository navigation
Description
Background
LLMSummarizeCompactor.compact() is sync but its body needs to call an async backend. We currently bridge via the private helper _run_coro_blocking in mellea/stdlib/context/compactor.py, which:
- Outside any event loop: calls
asyncio.run(coro)(fresh, disposable loop). - Inside a running event loop: spawns a worker thread running its own fresh
asyncio.run.
Surfaced during review of #996 — see the discussion thread on _run_coro_blocking for full context.
Problem
The bridge has two real failure modes that are surfaced in the docstring as Warning: paragraphs but cannot be fixed without changing the protocol:
-
Outside-loop case — cross-loop client mismatch. A backend whose
httpx.AsyncClientis bound to mellea's shared event loop (created viamellea.helpers._run_async_in_thread) cannot be used from_run_coro_blocking's fresh worker loop. The typical symptom isRuntimeError: This event loop is already runningor a hung request. -
Inside-loop case — outer loop blocking. When called from inside
react(), the worker thread blocks the calling thread — and therefore the outer event loop — for the full duration of the coroutine. Scheduled callbacks, telemetry flushers, cancellation signals, and any other concurrent tasks on the same loop are stalled until summarisation completes. Ac
Why we are not landing a smaller fix
@planetf1 and @markstur offered two interim suggestions that came up during PR #996 review and were deferred to this issue:
1. Route through _run_async_in_thread. bridge to mellea.helpers.event_loop_helper._run_async_in_thread, which would close failure mode 1 (the shared-loop bridge dispatches onto the loop where backend clients live). We tried it and reverted — it changes KeyboardInterrupt propagation semantics and broke `test_base_exc
2. Add a timeout= parameter.
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
future = pool.submit(asyncio.run, coro)
try:
return future.result(timeout=timeout)
except concurrent.futures.TimeoutError as exc:
raise TimeoutError(
f"Compactor backend call exceeded {timeout}s timeout"
) from excSame shape as the _allow_inside_loop discussion: it adds API surface that the async Compactor protocol obsoletes — once compact() is awaitable, callers wrap with asyncio.wait_for in one line. Backend calls already raise on network/HTTP timeouts, so the residual hang risk is small. We'd rather not land an intermediate timeout knob and remove it next cy
Proposed resolution
Add an async variant on the Compactor protocol:
class Compactor(Protocol):
def compact(self, ctx: T, *, backend: Backend | None = None) -> T: ...
async def acompact(self, ctx: T, *, backend: Backend | None = None) -> T: ...- Sync
compact()stays for sync callers (manual invocation between turns from a.pyscript, etc.). - Async
acompact()is whatreact()and any other async caller awaits — no bridge, no loop blocking, no cross-loop client issues. LLMSummarizeCompactor.acompact()becomes the natural home for what_async_compactalready does;compact()keeps the bridge as a thin sync wrapper for backwards compatibility (or deprecates it eventually).
This obsoletes _run_coro_blocking for the inside-loop path entirely and removes the cross-loop hazard for async callers.
Acceptance criteria
-
Compactorprotocol gainsasync def acompact(...). -
LLMSummarizeCompactorimplementsacompact()natively (no_run_coro_blocking). -
react()callsawait compactor.acompact(...)instead ofcompactor.compact(...). - Sync
compact()still works for manual usage from sync scripts. - Existing tests pass, including
test_base_exceptions_propagate(Ctrl-C semantics). - Docstring
Warning:paragraphs about loop blocking and cross-loop client mismatch can be removed from_run_coro_blocking/LLMSummarizeCompactor.
Related
- PR feat: context compaction strategies for the react loop #996 — original compaction work that introduced
_run_coro_blockingand surfaced these concerns mellea/stdlib/context/compactor.py:_run_coro_blocking— current bridgemellea/helpers/event_loop_helper.py:_run_async_in_thread— the shared bridge that motivated the discussion
Activity