JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(todo): 没送达的 drop/release 在下一轮模型调用前重试
n993577028c554340b7bf7
diff --git a/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py b/jiuwenswarm/agents/harness/common/rails/task_execution_rail.pyindex 41760abf04..fb096aebec 100644--- a/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/task_execution_rail.py@@ -1380,6 +1380,8 @@ class TaskExecutionRail(DeepAgentRail):self._todo_close_nudge_used: bool = False# 本轮模型调用已经通知适配器挂起外层正文。after 必须 drop 或 release。self._todo_close_hold_pending: bool = False+ # 上一帧 drop/release 没写进流。下一次 before_model_call 先重试这一帧。+ self._todo_close_undelivered: str | None = Nonedef set_skill_turbo_request_metadata(self, metadata: dict | None) -> None:"""注入当前请求 metadata 副本,供 before_tool_call 解析请求级工作区。"""@@ -1467,6 +1469,7 @@ class TaskExecutionRail(DeepAgentRail):self._todo_start_deferred = set()self._todo_close_nudge_used = Falseself._todo_close_hold_pending = False+ self._todo_close_undelivered = Noneself._tool_start_times = {}_ACTIVE_TASK_ID.set(None)if isinstance(ctx.inputs, InvokeInputs):@@ -1515,10 +1518,6 @@ class TaskExecutionRail(DeepAgentRail):dropped = Falsetry:dropped = self._attach_todo_close_nudge(ctx)- if dropped and self._todo_close_hold_pending:- await self._write_todo_close_control(- ctx.session, TODO_CLOSE_DROP_STREAM_TYPE,- )finally:await self._finish_todo_close_text_hold(ctx, dropped=dropped)@@ -1565,7 +1564,10 @@ class TaskExecutionRail(DeepAgentRail):known until the call returns, so a truncated turn is still held andreleased afterwards."""- if self._todo_close_hold_pending or ctx.session is None:+ await self._retry_undelivered_todo_close(ctx)+ if self._todo_close_undelivered or ctx.session is None:+ return+ if self._todo_close_hold_pending:returnif self._todo_close_nudge_used:return@@ -1585,24 +1587,59 @@ class TaskExecutionRail(DeepAgentRail):*,dropped: bool,) -> None:- """Drop already told the adapter to discard. Otherwise release.+ """Drop the held text, or release it.- A failed release keeps the pending flag. The next model call must not- send another hold: that frame would drop the text still sitting in the- adapter. A later successful release flushes it. A failed drop still- clears the flag so the next hold discards the buffer; releasing on- that failure would put the discarded draft into the open task segment.+ A failed write is remembered as ``drop`` or ``release``. The next+ ``before_model_call`` retries that frame before arming again. Drop is+ never rewritten as release: that would show the discarded draft."""- if not self._todo_close_hold_pending:- return- if dropped or ctx.session is None:+ if ctx.session is None:self._todo_close_hold_pending = False+ self._todo_close_undelivered = None+ return+ want_drop = dropped or self._todo_close_undelivered == "drop"+ if want_drop:+ if not self._todo_close_hold_pending and self._todo_close_undelivered != "drop":+ return+ wrote = await self._write_todo_close_control(+ ctx.session, TODO_CLOSE_DROP_STREAM_TYPE,+ )+ self._mark_todo_close_delivered("drop", wrote)+ return+ if not self._todo_close_hold_pending and self._todo_close_undelivered != "release":returnwrote = await self._write_todo_close_control(ctx.session, TODO_CLOSE_RELEASE_STREAM_TYPE,)+ self._mark_todo_close_delivered("release", wrote)++ async def _retry_undelivered_todo_close(self, ctx: AgentCallbackContext) -> None:+ """Resend a drop or release the adapter never received."""+ if ctx.session is None:+ return+ kind = self._todo_close_undelivered+ if kind is None and self._todo_close_hold_pending:+ kind = "release"+ if kind not in ("drop", "release"):+ return+ stream_type = (+ TODO_CLOSE_DROP_STREAM_TYPE+ if kind == "drop"+ else TODO_CLOSE_RELEASE_STREAM_TYPE+ )+ wrote = await self._write_todo_close_control(ctx.session, stream_type)if wrote:+ self._todo_close_undelivered = Noneself._todo_close_hold_pending = False+ return+ self._todo_close_undelivered = kind++ def _mark_todo_close_delivered(self, kind: str, wrote: bool) -> None:+ if wrote:+ self._todo_close_hold_pending = False+ self._todo_close_undelivered = None+ return+ self._todo_close_undelivered = kindasync def _write_todo_close_control(self, session: Any, stream_type: str) -> bool:try:diff --git a/tests/unit_tests/agentserver/rails/test_task_execution_todo_close_nudge.py b/tests/unit_tests/agentserver/rails/test_task_execution_todo_close_nudge.pyindex 359f837e5f..869d089ffd 100644--- a/tests/unit_tests/agentserver/rails/test_task_execution_todo_close_nudge.py+++ b/tests/unit_tests/agentserver/rails/test_task_execution_todo_close_nudge.py@@ -136,24 +136,23 @@ async def test_text_only_ending_keeps_real_tool_calls() -> None:assert ctx.session.events == ["todo_close_hold", "todo_close_release"]-class _ReleaseFails(_FakeSession):- async def write_stream(self, schema: object) -> None:- if str(getattr(schema, "type", "")) == "todo_close_release":- raise RuntimeError("stream down")- await super().write_stream(schema)-+class _FailTimes(_FakeSession):+ def __init__(self, fail_type: str, times: int) -> None:+ super().__init__()+ self.fail_type = fail_type+ self.times = times-class _DropFails(_FakeSession):async def write_stream(self, schema: object) -> None:- if str(getattr(schema, "type", "")) == "todo_close_drop":+ if str(getattr(schema, "type", "")) == self.fail_type and self.times > 0:+ self.times -= 1raise RuntimeError("stream down")await super().write_stream(schema)@pytest.mark.asyncio-async def test_failed_release_keeps_hold_pending() -> None:+async def test_failed_release_is_retried_before_the_next_hold() -> None:rail = _rail_with_open_task()- session = _ReleaseFails()+ session = _FailTimes("todo_close_release", 1)existing = ToolCall(id="call-1", type="function", name="read_file", arguments="{}",)@@ -163,17 +162,28 @@ async def test_failed_release_keeps_hold_pending() -> None:await rail.before_model_call(ctx)await rail.after_model_call(ctx)- assert rail._todo_close_hold_pending is True+ assert rail._todo_close_undelivered == "release"assert session.events == ["todo_close_hold"]- await rail.before_model_call(ctx)- assert session.events == ["todo_close_hold"]+ follow = _text_ctx("下一轮纯文本,必须还能作废。")+ follow.session = session+ await rail.before_model_call(follow)+ await rail.after_model_call(follow)++ assert session.events == [+ "todo_close_hold",+ "todo_close_release",+ "todo_close_hold",+ "todo_close_drop",+ ]+ assert rail._todo_close_undelivered is None+ assert rail._todo_close_hold_pending is False@pytest.mark.asyncio-async def test_failed_drop_does_not_release() -> None:+async def test_failed_drop_is_retried_and_not_released() -> None:rail = _rail_with_open_task()- session = _DropFails()+ session = _FailTimes("todo_close_drop", 1)ctx = _text_ctx("这段应该作废。")ctx.session = session@@ -181,9 +191,18 @@ async def test_failed_drop_does_not_release() -> None:await rail.after_model_call(ctx)assert rail._todo_close_nudge_used is True- assert rail._todo_close_hold_pending is False+ assert rail._todo_close_undelivered == "drop"assert session.events == ["todo_close_hold"]+ follow = _text_ctx("补刀已经用过。")+ follow.session = session+ await rail.before_model_call(follow)++ assert session.events == ["todo_close_hold", "todo_close_drop"]+ assert "todo_close_release" not in session.events+ assert rail._todo_close_undelivered is None+ assert rail._todo_close_hold_pending is False+@pytest.mark.asyncioasync def test_text_only_ending_skipped_without_open_task() -> None:@@ -354,9 +373,11 @@ async def test_before_invoke_resets_nudge_budget() -> None:rail = _rail_with_open_task()rail._todo_close_nudge_used = Truerail._todo_close_hold_pending = True+ rail._todo_close_undelivered = "drop"ctx = SimpleNamespace(session=None, inputs=object(), extra={})await rail.before_invoke(ctx)assert rail._todo_close_nudge_used is Falseassert rail._todo_close_hold_pending is False+ assert rail._todo_close_undelivered is None