fix(compaction): offload decision client initialization safely

This commit is contained in:
jinli.yl 2026-10-26 12:17:00 +08:00
parent 2430f0a6f4
commit 2aba468266
6 changed files with 89 additions and 10 deletions

View file

@ -176,3 +176,11 @@ keep the instance alive until ReMe has finished its calls, then close it itself.
replacing an internally constructed model or any model with active requests. Shutdown waits for active calls and
owned-client cleanup even when cancelled, then propagates cancellation. Component shutdown detaches the instance;
inject again before restarting a borrowed-model component. Standalone credential-based behavior is unchanged.
Owned clients initialize in a worker thread so certificate loading does not block the event loop. Cancelling a request
during construction waits for initialization to finish and leaves the client owned by the component until shutdown;
the cancelled request is not sent.
Hosts can import `CompactContextStep`, `ToolPair`, `collect_pairs`, and `preview_result` from `reme.steps.compaction`.
The pairing and preview helpers share the Step's rules for validating proposals. Archiving, resolving references,
and committing proposals remain host responsibilities.

View file

@ -162,3 +162,9 @@ YAML 中的 `model` 仍表示供应商模型名称;运行时 `model` 属性接
宿主应等待 ReMe 调用结束后自行关闭实例。替换 ReMe 自建模型或存在进行中请求的实例之前,须先关闭组件。
关闭过程即使被取消,也会等待活动请求和自建客户端清理完成,再传播取消信号。组件关闭时会解除实例引用;
重新启动使用外部模型的组件前需要再次注入。独立运行时按凭据创建模型的行为保持不变。
自建客户端在线程中初始化,证书加载不会阻塞事件循环。构造期间取消请求时,组件会等待构造完成并保留客户端所有权,
由后续组件关闭负责释放;此时不会发送该请求。
宿主可从 `reme.steps.compaction` 导入 `CompactContextStep`、`ToolPair`、`collect_pairs` 和 `preview_result`。
配对识别与预览构造函数和压缩 Step 共享相同规则,供宿主校验提案使用;归档、引用解析和提交仍由宿主负责。

View file

@ -10,6 +10,7 @@ from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter
from .base_as_decision_model import BaseAsDecisionModel
from ..component_registry import R
from ...utils.async_utils import complete_in_thread
if TYPE_CHECKING:
from agentscope.classifier import ClassifierModelBase, ClassifierResponse
@ -117,18 +118,19 @@ class AgentScopeAsDecisionModel(BaseAsDecisionModel):
raise ValueError("Decision model inference requires DECISION_MODEL_API_KEY or credential.api_key.")
if not credential.get("base_url"):
credential["base_url"] = None
# Reserve ownership before yielding so host injection cannot race
# initialization. The worker publishes the model to this component
# before cancellation can release the lifecycle lock.
self._owns_model = True
try:
credential_cls = import_module("agentscope.credential").TypeSafeCredential
self._model = model_cls(
credential=credential_cls(**credential),
**self.options.model_dump(exclude_none=True),
)
self._owns_model = True
await complete_in_thread(self._create_model, model_cls, credential)
except ImportError as exc:
raise ImportError(
"Decision model inference requires a compatible typesafe-sdk. "
"Install it with `pip install 'reme-ai[decision]'`.",
) from exc
finally:
self._owns_model = self._model is not None
model = self._model
self._active_calls += 1
self._idle.clear()
@ -141,6 +143,14 @@ class AgentScopeAsDecisionModel(BaseAsDecisionModel):
if self._active_calls == 0:
self._idle.set()
def _create_model(self, model_cls, credential) -> None:
"""Construct and retain the owned client, including synchronous TLS setup."""
credential_cls = import_module("agentscope.credential").TypeSafeCredential
self._model = model_cls(
credential=credential_cls(**credential),
**self.options.model_dump(exclude_none=True),
)
async def _close(self) -> None:
"""Finish draining and cleanup before propagating shutdown cancellation."""
cleanup = asyncio.create_task(self._release_model())

View file

@ -1,5 +1,6 @@
"""Tool-history compaction Steps."""
"""Tool-history compaction and shared host validation helpers."""
from .compact_context import CompactContextStep
from .messages import ToolPair, collect_pairs, preview_result
__all__ = ["CompactContextStep"]
__all__ = ["CompactContextStep", "ToolPair", "collect_pairs", "preview_result"]

View file

@ -14,8 +14,7 @@ from reme.components.as_decision_model import BaseAsDecisionModel
from reme.components.job import BaseJob
from reme.enumeration import ComponentEnum
from reme.schema.compaction import CompactionOptions, CompactionRequest
from reme.steps.compaction import CompactContextStep
from reme.steps.compaction.messages import collect_pairs
from reme.steps.compaction import CompactContextStep, collect_pairs
from reme.steps.compaction.state import build_batches, estimate_decision_tokens, serialize

View file

@ -4,6 +4,7 @@
import asyncio
import sys
import threading
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, Mock
@ -101,6 +102,13 @@ def test_valid_questions_reuse_model_and_restart_closes_clients(provider, provid
}
async def run():
loop_thread = threading.get_ident()
def construct(**_kwargs):
assert threading.get_ident() != loop_thread
return model
factory.side_effect = construct
component = AgentScopeAsDecisionModel(
credential={"api_key": "test-key", "base_url": ""},
model="decision-test",
@ -138,6 +146,53 @@ def test_valid_questions_reuse_model_and_restart_closes_clients(provider, provid
asyncio.run(run())
@pytest.mark.parametrize("construction_fails", [False, True])
def test_cancelled_construction_keeps_ownership_until_close(provider, construction_fails):
"""Cancellation cannot abandon a client or let shutdown overtake its worker."""
factory, model = provider
async def run():
loop = asyncio.get_running_loop()
entered, release = asyncio.Event(), threading.Event()
def construct(**_kwargs):
loop.call_soon_threadsafe(entered.set)
assert release.wait(5), "Construction was not released"
if construction_fails:
raise RuntimeError("construction failed")
return model
factory.side_effect = construct
component = AgentScopeAsDecisionModel(credential={"api_key": "test-key"})
call = asyncio.create_task(component(state="hello", questions={"q": {"type": "binary"}}))
closing = None
try:
await asyncio.wait_for(entered.wait(), 2)
with pytest.raises(RuntimeError, match="Close the decision component"):
component.model = AsyncMock()
for _ in range(2):
call.cancel()
await asyncio.sleep(0)
assert not call.done()
closing = asyncio.create_task(component.close())
await asyncio.sleep(0)
assert not closing.done()
release.set()
with pytest.raises(asyncio.CancelledError):
await asyncio.wait_for(call, 2)
await asyncio.wait_for(closing, 2)
assert component.model is None
assert not component._owns_model
model.assert_not_awaited()
assert model.client.aclose.await_count == (0 if construction_fails else 1)
finally:
release.set()
await asyncio.gather(call, *([closing] if closing is not None else []), return_exceptions=True)
await component.close()
asyncio.run(run())
@pytest.mark.parametrize(
"question",
[