Async `Compactor` protocol to replace sync↔async bridge in `LLMSummarizeCompactor`
维护者通常 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 内容生成。
描述
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 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.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 #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
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 161
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 45
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
generative-computing/mellea 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
generative-computing/mellea#1864 ·
维护者通常 3 天内回复
-
bug: instruct() and react() replay tool-call turns without tool_calls可能已有人在做 @simonyang08 于 21 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 85/100
generative-computing/mellea#1784 · 1 条评论 ·
维护者通常 3 天内回复
-
Silently mangled docstrings: lone backslash line-continuations in non-raw docstrings collapse command examples可能已有人在做 @madhusudhan1260 于 18 天前认领。 未关闭good first issue p2
难度 2/5 1-3 小时 新手友好度 82/100
generative-computing/mellea#1780 · 2 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
-
area/telemetry enhancement p2
难度 2/5 1-3 小时 新手友好度 66/100
generative-computing/mellea#1210 ·
维护者通常 3 天内回复
-
area/requirement area/stdlib enhancement p2
难度 2/5 1-3 小时 新手友好度 62/100
generative-computing/mellea#31 · 2 条评论 ·
维护者通常 3 天内回复
查看 generative-computing/mellea 的全部 Issue
相似的 Issue
-
flaky: test_fire_returns_thread_per_http_entry_on_head waits 5 s per dispatch worker on busy shards未关闭status/accepted type/bug
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
bug vector-store
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
EverMind-AI/Raven#1413 ·
维护者通常 1 天内回复
-
Tests: parallel pyinstallcheck runs oversubscribe the machine through nested TBB and BLAS pools可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 86/100
ACCESS-NRI/containerised-environments-infra#143 ·
维护者通常 2 天内回复