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
perf(rails): task_plan 磁盘刷新跳过 + context.usage 后台发射(轻量开关扩展)
wangmingqie039e5449042
diff --git a/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py b/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.pyindex 79ba5b2c57..8e38c23614 100644--- a/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py+++ b/jiuwenswarm/agents/harness/common/rails/concurrent_safe_rails.py@@ -16,6 +16,7 @@ the agent can use them.from __future__ import annotationsimport logging+import timeimport hashlibimport jsonfrom copy import copy@@ -103,6 +104,21 @@ class ConcurrentSafeSysOperationRail(SysOperationRail):ConcurrentSafeFileSystemRail = ConcurrentSafeSysOperationRail+def _tp_light_skip_refresh(ctx) -> bool:+ """[PERF] 轻量开关下,内存无 task_plan 时跳过磁盘 todo 刷新。"""+ import os as _os+ raw = (_os.getenv("JIUWENCLAW_ADAPTER_CKPT_LIGHT") or "").strip().lower()+ if raw not in {"1", "true", "yes", "on"}:+ return False+ try:+ if ctx.session is None or ctx.agent is None:+ return False+ state = ctx.agent.load_state(ctx.session)+ return getattr(state, "task_plan", None) is None+ except Exception: # noqa: BLE001 - 判定失败走原路径+ return False++class ConcurrentSafeTaskPlanningRail(TaskPlanningRail):"""TaskPlanningRail subclass that checks resource_mgr before add_tool.@@ -237,6 +253,12 @@ class ConcurrentSafeTaskPlanningRail(TaskPlanningRail):returnawait super().after_task_iteration(ctx)+ # [PERF] 轻量模式:内存中无 task_plan 时跳过磁盘 todo.json 刷新+ # (实测 _refresh 每迭代 62ms 均值/739ms 峰值,PVC 文件读)。+ # 语义:todo 流(有 plan)不受影响;仅"无 plan 且高并发"场景跳过+ # 一次本会返回空的磁盘读。env JIUWENCLAW_ADAPTER_CKPT_LIGHT=1 开。+ if _tp_light_skip_refresh(ctx):+ returnawait self._refresh_task_plan_from_todos(ctx)async def _sync_todos_from_plan(self, ctx: AgentCallbackContext) -> None:diff --git a/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py b/jiuwenswarm/agents/harness/common/rails/stream_event_rail.pyindex 2dc184a76c..54219493e0 100644--- a/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/stream_event_rail.py@@ -317,6 +317,8 @@ from jiuwenswarm.server.runtime.skill_turbo.context_binding import ( # noqa: E4class JiuSwarmStreamEventRail(DeepAgentRail):++ _bg_emit_tasks: set = set()"""Emit frontend stream events and enforce pause/abort checkpoints.Pause/abort state is owned by this Rail (not DeepAgent) so that@@ -1097,11 +1099,25 @@ class JiuSwarmStreamEventRail(DeepAgentRail):logger.warning("[StreamEventRail] inject call_goal prompt failed: %s", exc)async def after_model_call(self, ctx: AgentCallbackContext) -> None:- await self._emit_context_usage(+ # [PERF] 轻量模式:context.usage 是信息性统计事件,高并发下+ # write_stream 背压会把本钩子拖到秒级(100 并发实测 ×12-26 次+ # >1s 慢回调)。改为后台发射,不阻塞回复路径;背压时晚到但不丢。+ import os as _os+ _light = (_os.getenv("JIUWENCLAW_ADAPTER_CKPT_LIGHT") or "").strip().lower() in {"1", "true", "yes", "on"}+ coro = self._emit_context_usage(ctx,member_name=self._member_name or None,role=self._role or None,)+ if _light:+ try:+ task = asyncio.create_task(coro, name="ctx-usage-emit-bg")+ self._bg_emit_tasks.add(task)+ task.add_done_callback(self._bg_emit_tasks.discard)+ return+ except RuntimeError:+ pass # 无 loop 时退回同步+ await coroasync def before_tool_call(self, ctx: AgentCallbackContext) -> None:sid = self._resolve_sid(ctx, ctx.session)