Skip to content

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(llm): Fallback configuration for adaptation exceptions

wncbdj6b75f478985f
diff --git a/jiuwenswarm/resources/config.team.distributed.leader.yaml b/jiuwenswarm/resources/config.team.distributed.leader.yaml
index 77922f775e..bbcc1f00b2 100644
--- a/jiuwenswarm/resources/config.team.distributed.leader.yaml
+++ b/jiuwenswarm/resources/config.team.distributed.leader.yaml
@@ -255,8 +255,11 @@ react:
reasoning_tool_loop_compact_config:
enabled: true
consecutive_threshold: 3
+ tool_args_consecutive_threshold: 5
reasoning_min_chars: 4
reasoning_preview_max_chars: 512
+ bailout_threshold: 3
+ tool_args_bailout_threshold: 2
# Skills 在线自演进配置
evolution:
enabled: true # true = 启用在线 Skills 自演进
diff --git a/jiuwenswarm/resources/config.team.distributed.teammate.yaml b/jiuwenswarm/resources/config.team.distributed.teammate.yaml
index 21e1525414..761922e1b5 100644
--- a/jiuwenswarm/resources/config.team.distributed.teammate.yaml
+++ b/jiuwenswarm/resources/config.team.distributed.teammate.yaml
@@ -260,8 +260,11 @@ react:
reasoning_tool_loop_compact_config:
enabled: true
consecutive_threshold: 3
+ tool_args_consecutive_threshold: 5
reasoning_min_chars: 4
reasoning_preview_max_chars: 512
+ bailout_threshold: 3
+ tool_args_bailout_threshold: 2
# Skills 在线自演进配置
evolution:
enabled: true # true = 启用在线 Skills 自演进
diff --git a/jiuwenswarm/resources/config.yaml b/jiuwenswarm/resources/config.yaml
index 50e93a23a6..71cb821f23 100644
--- a/jiuwenswarm/resources/config.yaml
+++ b/jiuwenswarm/resources/config.yaml
@@ -210,7 +210,12 @@ models:
api_key: ${API_KEY}
model_name: ${MODEL_NAME}
client_provider: ${MODEL_PROVIDER}
- timeout: 1800
+ # HTTP 请求超时(秒)。需严格 > stream_first_chunk_timeout,确保先触发应用层流超时(可被 LLMRetryRail 重试)
+ timeout: 360
+ # 流式:等待首个分片的最大时间(秒)。放宽以兼容 reasoning/思考模型思考期的静默
+ stream_first_chunk_timeout: 300
+ # 流式:分片之间的最大空闲等待时间(秒)
+ stream_idle_timeout: 60
verify_ssl: false
custom_headers: {}
model_config_obj:
@@ -503,12 +508,15 @@ react:
third_pass_target_tokens: 10000
truncate_head_ratio: 0.2
offload_writeback_enabled: false
- # 连续相同 reasoning + 相同工具集时,折叠较早重复轮次(始终保留最近 1 轮完整内容)
+ # 连续相同 reasoning+工具集 / 相同工具入参时,折叠重复轮次;超阈值后兜底中止
reasoning_tool_loop_compact_config:
- enabled: true # 是否启用 reasoning+工具循环压缩
- consecutive_threshold: 3 # 尾部连续几轮相同才触发压缩
+ enabled: true # 是否启用循环压缩
+ consecutive_threshold: 3 # reasoning+工具名连续相同几轮触发压缩
+ tool_args_consecutive_threshold: 5 # 工具名+入参连续相同几轮触发压缩(与 reasoning 无关)
reasoning_min_chars: 4 # reasoning 低于此长度(strip 后)的轮次不参与匹配
reasoning_preview_max_chars: 512 # 摘要里 reasoning 预览的最大字符数
+ bailout_threshold: 3 # reasoning 规则压缩次数达此值后中止
+ tool_args_bailout_threshold: 2 # 工具入参规则压缩次数达此值后终止
# Skills 在线自演进配置
evolution:
enabled: true # true = 启用在线 Skills 自演进
diff --git a/jiuwenswarm/server/runtime/agent_manager.py b/jiuwenswarm/server/runtime/agent_manager.py
index 402a24210f..a787d2d32b 100644
--- a/jiuwenswarm/server/runtime/agent_manager.py
+++ b/jiuwenswarm/server/runtime/agent_manager.py
@@ -14,7 +14,7 @@ from weakref import WeakValueDictionary
from jiuwenswarm.common.e2a.acp.protocol import build_acp_initialize_result
from jiuwenswarm.agents.harness.team import get_team_manager
-from jiuwenswarm.common.config import get_config
+from jiuwenswarm.common.config import get_config, get_default_models
if TYPE_CHECKING:
from jiuwenswarm.server.runtime.agent_adapter.interface import JiuWenSwarm
@@ -101,6 +101,9 @@ class AgentManager:
self._agent_create_locks: WeakValueDictionary[
tuple[str, str], asyncio.Lock
] = WeakValueDictionary()
+ # 上一次默认模型的连接身份快照 (diff_key -> ModelClientConfig), 用于
+ # 模型热更新后关闭"已被删除/改掉凭证"的 LLM 连接 (增量关闭)。
+ self._last_model_conn_configs: dict[tuple, Any] = {}
def _get_agent_create_lock(
self,
@@ -317,6 +320,7 @@ class AgentManager:
self._agent_create_params.pop("tui", None)
return True
+
@staticmethod
def _reload_fingerprint(
config: Any,
@@ -866,6 +870,63 @@ class AgentManager:
logger.info(f"channel {channel_id} reload agent config success.")
if reload_completed:
self._last_reload_fingerprint = fingerprint
+ # 模型配置变更时, 关闭已被删除/改掉凭证的旧 LLM 连接 (增量关闭)。
+ if model_scope:
+ await self._evict_stale_llm_clients(effective_config)
+
+ async def _evict_stale_llm_clients(self, effective_config: Any) -> None:
+ """模型热更新后, 关闭"已从 models.defaults 删除/改掉凭证"的 LLM 连接。
+
+ agent-core 的 HTTP client 缓存是进程级、被所有组件共享的, 因此这里只做
+ **增量关闭**(上次默认集 - 本次默认集), 绝不碰其它组件仍在使用的连接。
+ 被删除/更新的模型即使有在途调用也会立即断开——用户既然不再使用它, 就不该
+ 让它继续偷偷消耗 token。被误伤的连接(若有)也会在下次调用时自愈重建。
+
+ 注意: 进程启动后的第一次模型变更, 因无历史快照, 只登记不关闭; 之后的变更
+ 才做真正的 diff 关闭。
+ """
+ try:
+ from openjiuwen.core.foundation.llm import ModelClientConfig
+ from openjiuwen.core.foundation.llm.model_clients.openai_model_client import (
+ OpenAIModelClient,
+ )
+ from openjiuwen.core.foundation.llm.model_clients.anthropic_model_client import (
+ AnthropicModelClient,
+ )
+ except Exception as exc: # noqa: BLE001
+ logger.warning("[AgentManager] LLM client evict skipped (import failed): %s", exc)
+ return
+
+ def _diff_key(cfg: Any) -> tuple:
+ return (str(cfg.client_provider), cfg.api_key, cfg.api_base, cfg.verify_ssl, cfg.ssl_cert)
+
+ new_configs: dict[tuple, Any] = {}
+ try:
+ entries = get_default_models(effective_config if isinstance(effective_config, dict) else None)
+ except Exception as exc: # noqa: BLE001
+ logger.warning("[AgentManager] build live model configs failed: %s", exc)
+ return
+ for entry in entries or []:
+ mcc = (entry or {}).get("model_client_config") or {}
+ mcc_fields = {k: v for k, v in mcc.items() if k != "model_name"}
+ if not mcc_fields.get("client_provider"):
+ mcc_fields["client_provider"] = "OpenAI"
+ try:
+ cfg = ModelClientConfig(**mcc_fields)
+ except Exception as exc: # noqa: BLE001
+ logger.warning("[AgentManager] skip invalid model config during evict: %s", exc)
+ continue
+ new_configs[_diff_key(cfg)] = cfg
+
+ removed = [cfg for key, cfg in self._last_model_conn_configs.items() if key not in new_configs]
+ self._last_model_conn_configs = new_configs
+ if not removed:
+ return
+ for client_cls in (OpenAIModelClient, AnthropicModelClient):
+ try:
+ await client_cls.aclose_connections(removed)
+ except Exception as exc: # noqa: BLE001
+ logger.warning("[AgentManager] %s.aclose_connections failed: %s", client_cls.__name__, exc)
async def recreate_agent(self, channel_id: str, *, immediate: bool = True) -> None:
"""重建指定 channel 的所有 agent 实例.