Async `Compactor` protocol to replace sync↔async bridge in `LLMSummarizeCompactor`

オープン
#1,258 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python

調査の方向性

Start in mellea/stdlib/context/compactor.py, where _run_coro_blocking and LLMSummarizeCompactor.compact() live, then find the react() call site that invokes the compactor. The issue proposes an async acompact() on the Compactor protocol, so the change touches the protocol, the summarizer and react(), and the existing test_base_exceptions_propagate must keep passing. Done means react() awaits acompact(), compact() still works from sync scripts, and the Warning: docstrings can be removed. The design is still open, so confirm the approach with maintainers before coding.

索引モデルが issue の本文から書いたものです。

説明

p2

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:

  1. Outside-loop case — cross-loop client mismatch. A backend whose httpx.AsyncClient is bound to mellea's shared event loop (created via mellea.helpers._run_async_in_thread) cannot be used from _run_coro_blocking's fresh worker loop. The typical symptom is RuntimeError: This event loop is already running or a hung request.

  2. 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 exc

Same 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 .py script, etc.).
  • Async acompact() is what react() 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_compact already 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

  • Compactor protocol gains async def acompact(...).
  • LLMSummarizeCompactor implements acompact() natively (no _run_coro_blocking).
  • react() calls await compactor.acompact(...) instead of compactor.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 #996 — original compaction work that introduced _run_coro_blocking and surfaced these concerns
  • mellea/stdlib/context/compactor.py:_run_coro_blocking — current bridge
  • mellea/helpers/event_loop_helper.py:_run_async_in_thread — the shared bridge that motivated the discussion
主要言語
Python
スター
1.8k
フォーク
161
平均マージ
4日 3時間
マージ済み PR(30日)
45

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

generative-computing/mellea のほかの issue

generative-computing/mellea の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。