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

未关闭
#1,258 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
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 小时
30 天内合并 PR
45

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

generative-computing/mellea 的其他 Issue

查看 generative-computing/mellea 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。